Merged revisions 208012,208018-208019,208021,208023-208030,208033,208037,208040-20804...
[official-gcc.git] / main / gcc / cp / pt.c
blobe86fb2cb78209b11392cb478a58e5b784dbe49d9
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "pointer-set.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "cp-objcp-common.h"
43 #include "tree-inline.h"
44 #include "decl.h"
45 #include "toplev.h"
46 #include "opts.h"
47 #include "timevar.h"
48 #include "tree-iterator.h"
49 #include "cgraph.h"
50 #include "type-utils.h"
51 #include "gimplify.h"
53 /* The type of functions taking a tree, and some additional data, and
54 returning an int. */
55 typedef int (*tree_fn_t) (tree, void*);
57 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
58 instantiations have been deferred, either because their definitions
59 were not yet available, or because we were putting off doing the work. */
60 struct GTY ((chain_next ("%h.next"))) pending_template {
61 struct pending_template *next;
62 struct tinst_level *tinst;
65 static GTY(()) struct pending_template *pending_templates;
66 static GTY(()) struct pending_template *last_pending_template;
68 void
69 clear_pending_templates (void)
71 pending_templates = NULL;
72 last_pending_template = NULL;
75 int processing_template_parmlist;
76 static int template_header_count;
78 static GTY(()) tree saved_trees;
79 static vec<int> inline_parm_levels;
81 static GTY(()) struct tinst_level *current_tinst_level;
83 static GTY(()) tree saved_access_scope;
85 /* Live only within one (recursive) call to tsubst_expr. We use
86 this to pass the statement expression node from the STMT_EXPR
87 to the EXPR_STMT that is its result. */
88 static tree cur_stmt_expr;
90 /* True if we've recursed into fn_type_unification too many times. */
91 static bool excessive_deduction_depth;
93 typedef struct GTY(()) spec_entry
95 tree tmpl;
96 tree args;
97 tree spec;
98 } spec_entry;
100 static GTY ((param_is (spec_entry)))
101 htab_t decl_specializations;
103 static GTY ((param_is (spec_entry)))
104 htab_t type_specializations;
106 /* Contains canonical template parameter types. The vector is indexed by
107 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
108 TREE_LIST, whose TREE_VALUEs contain the canonical template
109 parameters of various types and levels. */
110 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
112 #define UNIFY_ALLOW_NONE 0
113 #define UNIFY_ALLOW_MORE_CV_QUAL 1
114 #define UNIFY_ALLOW_LESS_CV_QUAL 2
115 #define UNIFY_ALLOW_DERIVED 4
116 #define UNIFY_ALLOW_INTEGER 8
117 #define UNIFY_ALLOW_OUTER_LEVEL 16
118 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
119 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
121 enum template_base_result {
122 tbr_incomplete_type,
123 tbr_ambiguous_baseclass,
124 tbr_success
127 static void push_access_scope (tree);
128 static void pop_access_scope (tree);
129 static bool resolve_overloaded_unification (tree, tree, tree, tree,
130 unification_kind_t, int,
131 bool);
132 static int try_one_overload (tree, tree, tree, tree, tree,
133 unification_kind_t, int, bool, bool);
134 static int unify (tree, tree, tree, tree, int, bool);
135 static void add_pending_template (tree);
136 static tree reopen_tinst_level (struct tinst_level *);
137 static tree tsubst_initializer_list (tree, tree);
138 static tree get_class_bindings (tree, tree, tree, tree);
139 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
140 bool, bool);
141 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
142 bool, bool);
143 static void tsubst_enum (tree, tree, tree);
144 static tree add_to_template_args (tree, tree);
145 static tree add_outermost_template_args (tree, tree);
146 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
147 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
148 tree);
149 static int type_unification_real (tree, tree, tree, const tree *,
150 unsigned int, int, unification_kind_t, int,
151 vec<deferred_access_check, va_gc> **,
152 bool);
153 static void note_template_header (int);
154 static tree convert_nontype_argument_function (tree, tree);
155 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
156 static tree convert_template_argument (tree, tree, tree,
157 tsubst_flags_t, int, tree);
158 static int for_each_template_parm (tree, tree_fn_t, void*,
159 struct pointer_set_t*, bool);
160 static tree expand_template_argument_pack (tree);
161 static tree build_template_parm_index (int, int, int, tree, tree);
162 static bool inline_needs_template_parms (tree, bool);
163 static void push_inline_template_parms_recursive (tree, int);
164 static tree retrieve_local_specialization (tree);
165 static void register_local_specialization (tree, tree);
166 static hashval_t hash_specialization (const void *p);
167 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
168 static int mark_template_parm (tree, void *);
169 static int template_parm_this_level_p (tree, void *);
170 static tree tsubst_friend_function (tree, tree);
171 static tree tsubst_friend_class (tree, tree);
172 static int can_complete_type_without_circularity (tree);
173 static tree get_bindings (tree, tree, tree, bool);
174 static int template_decl_level (tree);
175 static int check_cv_quals_for_unify (int, tree, tree);
176 static void template_parm_level_and_index (tree, int*, int*);
177 static int unify_pack_expansion (tree, tree, tree,
178 tree, unification_kind_t, bool, bool);
179 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
180 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
181 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
182 static void regenerate_decl_from_template (tree, tree);
183 static tree most_specialized_class (tree, tsubst_flags_t);
184 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
185 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
186 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
187 static bool check_specialization_scope (void);
188 static tree process_partial_specialization (tree);
189 static void set_current_access_from_decl (tree);
190 static enum template_base_result get_template_base (tree, tree, tree, tree,
191 bool , tree *);
192 static tree try_class_unification (tree, tree, tree, tree, bool);
193 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
194 tree, tree);
195 static bool template_template_parm_bindings_ok_p (tree, tree);
196 static int template_args_equal (tree, tree);
197 static void tsubst_default_arguments (tree, tsubst_flags_t);
198 static tree for_each_template_parm_r (tree *, int *, void *);
199 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
200 static void copy_default_args_to_explicit_spec (tree);
201 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
202 static bool dependent_template_arg_p (tree);
203 static bool any_template_arguments_need_structural_equality_p (tree);
204 static bool dependent_type_p_r (tree);
205 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
206 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
207 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
208 static tree tsubst_decl (tree, tree, tsubst_flags_t);
209 static void perform_typedefs_access_check (tree tmpl, tree targs);
210 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
211 location_t);
212 static tree listify (tree);
213 static tree listify_autos (tree, tree);
214 static tree template_parm_to_arg (tree t);
215 static tree current_template_args (void);
216 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
217 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
219 /* Make the current scope suitable for access checking when we are
220 processing T. T can be FUNCTION_DECL for instantiated function
221 template, VAR_DECL for static member variable, or TYPE_DECL for
222 alias template (needed by instantiate_decl). */
224 static void
225 push_access_scope (tree t)
227 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
228 || TREE_CODE (t) == TYPE_DECL);
230 if (DECL_FRIEND_CONTEXT (t))
231 push_nested_class (DECL_FRIEND_CONTEXT (t));
232 else if (DECL_CLASS_SCOPE_P (t))
233 push_nested_class (DECL_CONTEXT (t));
234 else
235 push_to_top_level ();
237 if (TREE_CODE (t) == FUNCTION_DECL)
239 saved_access_scope = tree_cons
240 (NULL_TREE, current_function_decl, saved_access_scope);
241 current_function_decl = t;
245 /* Restore the scope set up by push_access_scope. T is the node we
246 are processing. */
248 static void
249 pop_access_scope (tree t)
251 if (TREE_CODE (t) == FUNCTION_DECL)
253 current_function_decl = TREE_VALUE (saved_access_scope);
254 saved_access_scope = TREE_CHAIN (saved_access_scope);
257 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
258 pop_nested_class ();
259 else
260 pop_from_top_level ();
263 /* Do any processing required when DECL (a member template
264 declaration) is finished. Returns the TEMPLATE_DECL corresponding
265 to DECL, unless it is a specialization, in which case the DECL
266 itself is returned. */
268 tree
269 finish_member_template_decl (tree decl)
271 if (decl == error_mark_node)
272 return error_mark_node;
274 gcc_assert (DECL_P (decl));
276 if (TREE_CODE (decl) == TYPE_DECL)
278 tree type;
280 type = TREE_TYPE (decl);
281 if (type == error_mark_node)
282 return error_mark_node;
283 if (MAYBE_CLASS_TYPE_P (type)
284 && CLASSTYPE_TEMPLATE_INFO (type)
285 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
287 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
288 check_member_template (tmpl);
289 return tmpl;
291 return NULL_TREE;
293 else if (TREE_CODE (decl) == FIELD_DECL)
294 error ("data member %qD cannot be a member template", decl);
295 else if (DECL_TEMPLATE_INFO (decl))
297 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
299 check_member_template (DECL_TI_TEMPLATE (decl));
300 return DECL_TI_TEMPLATE (decl);
302 else
303 return decl;
305 else
306 error ("invalid member template declaration %qD", decl);
308 return error_mark_node;
311 /* Create a template info node. */
313 tree
314 build_template_info (tree template_decl, tree template_args)
316 tree result = make_node (TEMPLATE_INFO);
317 TI_TEMPLATE (result) = template_decl;
318 TI_ARGS (result) = template_args;
319 return result;
322 /* Return the template info node corresponding to T, whatever T is. */
324 tree
325 get_template_info (const_tree t)
327 tree tinfo = NULL_TREE;
329 if (!t || t == error_mark_node)
330 return NULL;
332 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
333 tinfo = DECL_TEMPLATE_INFO (t);
335 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
336 t = TREE_TYPE (t);
338 if (OVERLOAD_TYPE_P (t))
339 tinfo = TYPE_TEMPLATE_INFO (t);
340 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
341 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
343 return tinfo;
346 /* Returns the template nesting level of the indicated class TYPE.
348 For example, in:
349 template <class T>
350 struct A
352 template <class U>
353 struct B {};
356 A<T>::B<U> has depth two, while A<T> has depth one.
357 Both A<T>::B<int> and A<int>::B<U> have depth one, if
358 they are instantiations, not specializations.
360 This function is guaranteed to return 0 if passed NULL_TREE so
361 that, for example, `template_class_depth (current_class_type)' is
362 always safe. */
365 template_class_depth (tree type)
367 int depth;
369 for (depth = 0;
370 type && TREE_CODE (type) != NAMESPACE_DECL;
371 type = (TREE_CODE (type) == FUNCTION_DECL)
372 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
374 tree tinfo = get_template_info (type);
376 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
377 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
378 ++depth;
381 return depth;
384 /* Subroutine of maybe_begin_member_template_processing.
385 Returns true if processing DECL needs us to push template parms. */
387 static bool
388 inline_needs_template_parms (tree decl, bool nsdmi)
390 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
391 return false;
393 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
394 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
397 /* Subroutine of maybe_begin_member_template_processing.
398 Push the template parms in PARMS, starting from LEVELS steps into the
399 chain, and ending at the beginning, since template parms are listed
400 innermost first. */
402 static void
403 push_inline_template_parms_recursive (tree parmlist, int levels)
405 tree parms = TREE_VALUE (parmlist);
406 int i;
408 if (levels > 1)
409 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
411 ++processing_template_decl;
412 current_template_parms
413 = tree_cons (size_int (processing_template_decl),
414 parms, current_template_parms);
415 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
417 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
418 NULL);
419 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
421 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
423 if (parm == error_mark_node)
424 continue;
426 gcc_assert (DECL_P (parm));
428 switch (TREE_CODE (parm))
430 case TYPE_DECL:
431 case TEMPLATE_DECL:
432 pushdecl (parm);
433 break;
435 case PARM_DECL:
437 /* Make a CONST_DECL as is done in process_template_parm.
438 It is ugly that we recreate this here; the original
439 version built in process_template_parm is no longer
440 available. */
441 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
442 CONST_DECL, DECL_NAME (parm),
443 TREE_TYPE (parm));
444 DECL_ARTIFICIAL (decl) = 1;
445 TREE_CONSTANT (decl) = 1;
446 TREE_READONLY (decl) = 1;
447 DECL_INITIAL (decl) = DECL_INITIAL (parm);
448 SET_DECL_TEMPLATE_PARM_P (decl);
449 pushdecl (decl);
451 break;
453 default:
454 gcc_unreachable ();
459 /* Restore the template parameter context for a member template, a
460 friend template defined in a class definition, or a non-template
461 member of template class. */
463 void
464 maybe_begin_member_template_processing (tree decl)
466 tree parms;
467 int levels = 0;
468 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
470 if (nsdmi)
471 decl = (CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
472 ? CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (decl))
473 : NULL_TREE);
475 if (inline_needs_template_parms (decl, nsdmi))
477 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
478 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
480 if (DECL_TEMPLATE_SPECIALIZATION (decl))
482 --levels;
483 parms = TREE_CHAIN (parms);
486 push_inline_template_parms_recursive (parms, levels);
489 /* Remember how many levels of template parameters we pushed so that
490 we can pop them later. */
491 inline_parm_levels.safe_push (levels);
494 /* Undo the effects of maybe_begin_member_template_processing. */
496 void
497 maybe_end_member_template_processing (void)
499 int i;
500 int last;
502 if (inline_parm_levels.length () == 0)
503 return;
505 last = inline_parm_levels.pop ();
506 for (i = 0; i < last; ++i)
508 --processing_template_decl;
509 current_template_parms = TREE_CHAIN (current_template_parms);
510 poplevel (0, 0, 0);
514 /* Return a new template argument vector which contains all of ARGS,
515 but has as its innermost set of arguments the EXTRA_ARGS. */
517 static tree
518 add_to_template_args (tree args, tree extra_args)
520 tree new_args;
521 int extra_depth;
522 int i;
523 int j;
525 if (args == NULL_TREE || extra_args == error_mark_node)
526 return extra_args;
528 extra_depth = TMPL_ARGS_DEPTH (extra_args);
529 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
531 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
532 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
534 for (j = 1; j <= extra_depth; ++j, ++i)
535 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
537 return new_args;
540 /* Like add_to_template_args, but only the outermost ARGS are added to
541 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
542 (EXTRA_ARGS) levels are added. This function is used to combine
543 the template arguments from a partial instantiation with the
544 template arguments used to attain the full instantiation from the
545 partial instantiation. */
547 static tree
548 add_outermost_template_args (tree args, tree extra_args)
550 tree new_args;
552 /* If there are more levels of EXTRA_ARGS than there are ARGS,
553 something very fishy is going on. */
554 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
556 /* If *all* the new arguments will be the EXTRA_ARGS, just return
557 them. */
558 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
559 return extra_args;
561 /* For the moment, we make ARGS look like it contains fewer levels. */
562 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
564 new_args = add_to_template_args (args, extra_args);
566 /* Now, we restore ARGS to its full dimensions. */
567 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
569 return new_args;
572 /* Return the N levels of innermost template arguments from the ARGS. */
574 tree
575 get_innermost_template_args (tree args, int n)
577 tree new_args;
578 int extra_levels;
579 int i;
581 gcc_assert (n >= 0);
583 /* If N is 1, just return the innermost set of template arguments. */
584 if (n == 1)
585 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
587 /* If we're not removing anything, just return the arguments we were
588 given. */
589 extra_levels = TMPL_ARGS_DEPTH (args) - n;
590 gcc_assert (extra_levels >= 0);
591 if (extra_levels == 0)
592 return args;
594 /* Make a new set of arguments, not containing the outer arguments. */
595 new_args = make_tree_vec (n);
596 for (i = 1; i <= n; ++i)
597 SET_TMPL_ARGS_LEVEL (new_args, i,
598 TMPL_ARGS_LEVEL (args, i + extra_levels));
600 return new_args;
603 /* The inverse of get_innermost_template_args: Return all but the innermost
604 EXTRA_LEVELS levels of template arguments from the ARGS. */
606 static tree
607 strip_innermost_template_args (tree args, int extra_levels)
609 tree new_args;
610 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
611 int i;
613 gcc_assert (n >= 0);
615 /* If N is 1, just return the outermost set of template arguments. */
616 if (n == 1)
617 return TMPL_ARGS_LEVEL (args, 1);
619 /* If we're not removing anything, just return the arguments we were
620 given. */
621 gcc_assert (extra_levels >= 0);
622 if (extra_levels == 0)
623 return args;
625 /* Make a new set of arguments, not containing the inner arguments. */
626 new_args = make_tree_vec (n);
627 for (i = 1; i <= n; ++i)
628 SET_TMPL_ARGS_LEVEL (new_args, i,
629 TMPL_ARGS_LEVEL (args, i));
631 return new_args;
634 /* We've got a template header coming up; push to a new level for storing
635 the parms. */
637 void
638 begin_template_parm_list (void)
640 /* We use a non-tag-transparent scope here, which causes pushtag to
641 put tags in this scope, rather than in the enclosing class or
642 namespace scope. This is the right thing, since we want
643 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
644 global template class, push_template_decl handles putting the
645 TEMPLATE_DECL into top-level scope. For a nested template class,
646 e.g.:
648 template <class T> struct S1 {
649 template <class T> struct S2 {};
652 pushtag contains special code to call pushdecl_with_scope on the
653 TEMPLATE_DECL for S2. */
654 begin_scope (sk_template_parms, NULL);
655 ++processing_template_decl;
656 ++processing_template_parmlist;
657 note_template_header (0);
660 /* This routine is called when a specialization is declared. If it is
661 invalid to declare a specialization here, an error is reported and
662 false is returned, otherwise this routine will return true. */
664 static bool
665 check_specialization_scope (void)
667 tree scope = current_scope ();
669 /* [temp.expl.spec]
671 An explicit specialization shall be declared in the namespace of
672 which the template is a member, or, for member templates, in the
673 namespace of which the enclosing class or enclosing class
674 template is a member. An explicit specialization of a member
675 function, member class or static data member of a class template
676 shall be declared in the namespace of which the class template
677 is a member. */
678 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
680 error ("explicit specialization in non-namespace scope %qD", scope);
681 return false;
684 /* [temp.expl.spec]
686 In an explicit specialization declaration for a member of a class
687 template or a member template that appears in namespace scope,
688 the member template and some of its enclosing class templates may
689 remain unspecialized, except that the declaration shall not
690 explicitly specialize a class member template if its enclosing
691 class templates are not explicitly specialized as well. */
692 if (current_template_parms)
694 error ("enclosing class templates are not explicitly specialized");
695 return false;
698 return true;
701 /* We've just seen template <>. */
703 bool
704 begin_specialization (void)
706 begin_scope (sk_template_spec, NULL);
707 note_template_header (1);
708 return check_specialization_scope ();
711 /* Called at then end of processing a declaration preceded by
712 template<>. */
714 void
715 end_specialization (void)
717 finish_scope ();
718 reset_specialization ();
721 /* Any template <>'s that we have seen thus far are not referring to a
722 function specialization. */
724 void
725 reset_specialization (void)
727 processing_specialization = 0;
728 template_header_count = 0;
731 /* We've just seen a template header. If SPECIALIZATION is nonzero,
732 it was of the form template <>. */
734 static void
735 note_template_header (int specialization)
737 processing_specialization = specialization;
738 template_header_count++;
741 /* We're beginning an explicit instantiation. */
743 void
744 begin_explicit_instantiation (void)
746 gcc_assert (!processing_explicit_instantiation);
747 processing_explicit_instantiation = true;
751 void
752 end_explicit_instantiation (void)
754 gcc_assert (processing_explicit_instantiation);
755 processing_explicit_instantiation = false;
758 /* An explicit specialization or partial specialization of TMPL is being
759 declared. Check that the namespace in which the specialization is
760 occurring is permissible. Returns false iff it is invalid to
761 specialize TMPL in the current namespace. */
763 static bool
764 check_specialization_namespace (tree tmpl)
766 tree tpl_ns = decl_namespace_context (tmpl);
768 /* [tmpl.expl.spec]
770 An explicit specialization shall be declared in the namespace of
771 which the template is a member, or, for member templates, in the
772 namespace of which the enclosing class or enclosing class
773 template is a member. An explicit specialization of a member
774 function, member class or static data member of a class template
775 shall be declared in the namespace of which the class template is
776 a member. */
777 if (current_scope() != DECL_CONTEXT (tmpl)
778 && !at_namespace_scope_p ())
780 error ("specialization of %qD must appear at namespace scope", tmpl);
781 return false;
783 if (is_associated_namespace (current_namespace, tpl_ns))
784 /* Same or super-using namespace. */
785 return true;
786 else
788 permerror (input_location, "specialization of %qD in different namespace", tmpl);
789 permerror (input_location, " from definition of %q+#D", tmpl);
790 return false;
794 /* SPEC is an explicit instantiation. Check that it is valid to
795 perform this explicit instantiation in the current namespace. */
797 static void
798 check_explicit_instantiation_namespace (tree spec)
800 tree ns;
802 /* DR 275: An explicit instantiation shall appear in an enclosing
803 namespace of its template. */
804 ns = decl_namespace_context (spec);
805 if (!is_ancestor (current_namespace, ns))
806 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
807 "(which does not enclose namespace %qD)",
808 spec, current_namespace, ns);
811 /* The TYPE is being declared. If it is a template type, that means it
812 is a partial specialization. Do appropriate error-checking. */
814 tree
815 maybe_process_partial_specialization (tree type)
817 tree context;
819 if (type == error_mark_node)
820 return error_mark_node;
822 /* A lambda that appears in specialization context is not itself a
823 specialization. */
824 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
825 return type;
827 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
829 error ("name of class shadows template template parameter %qD",
830 TYPE_NAME (type));
831 return error_mark_node;
834 context = TYPE_CONTEXT (type);
836 if (TYPE_ALIAS_P (type))
838 if (TYPE_TEMPLATE_INFO (type)
839 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
840 error ("specialization of alias template %qD",
841 TYPE_TI_TEMPLATE (type));
842 else
843 error ("explicit specialization of non-template %qT", type);
844 return error_mark_node;
846 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
848 /* This is for ordinary explicit specialization and partial
849 specialization of a template class such as:
851 template <> class C<int>;
855 template <class T> class C<T*>;
857 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
859 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
860 && !COMPLETE_TYPE_P (type))
862 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
863 && !at_namespace_scope_p ())
864 return error_mark_node;
865 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
866 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
867 if (processing_template_decl)
869 if (push_template_decl (TYPE_MAIN_DECL (type))
870 == error_mark_node)
871 return error_mark_node;
874 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
875 error ("specialization of %qT after instantiation", type);
876 else if (errorcount && !processing_specialization
877 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
878 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
879 /* Trying to define a specialization either without a template<> header
880 or in an inappropriate place. We've already given an error, so just
881 bail now so we don't actually define the specialization. */
882 return error_mark_node;
884 else if (CLASS_TYPE_P (type)
885 && !CLASSTYPE_USE_TEMPLATE (type)
886 && CLASSTYPE_TEMPLATE_INFO (type)
887 && context && CLASS_TYPE_P (context)
888 && CLASSTYPE_TEMPLATE_INFO (context))
890 /* This is for an explicit specialization of member class
891 template according to [temp.expl.spec/18]:
893 template <> template <class U> class C<int>::D;
895 The context `C<int>' must be an implicit instantiation.
896 Otherwise this is just a member class template declared
897 earlier like:
899 template <> class C<int> { template <class U> class D; };
900 template <> template <class U> class C<int>::D;
902 In the first case, `C<int>::D' is a specialization of `C<T>::D'
903 while in the second case, `C<int>::D' is a primary template
904 and `C<T>::D' may not exist. */
906 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
907 && !COMPLETE_TYPE_P (type))
909 tree t;
910 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
912 if (current_namespace
913 != decl_namespace_context (tmpl))
915 permerror (input_location, "specializing %q#T in different namespace", type);
916 permerror (input_location, " from definition of %q+#D", tmpl);
919 /* Check for invalid specialization after instantiation:
921 template <> template <> class C<int>::D<int>;
922 template <> template <class U> class C<int>::D; */
924 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
925 t; t = TREE_CHAIN (t))
927 tree inst = TREE_VALUE (t);
928 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
929 || !COMPLETE_OR_OPEN_TYPE_P (inst))
931 /* We already have a full specialization of this partial
932 instantiation, or a full specialization has been
933 looked up but not instantiated. Reassign it to the
934 new member specialization template. */
935 spec_entry elt;
936 spec_entry *entry;
937 void **slot;
939 elt.tmpl = most_general_template (tmpl);
940 elt.args = CLASSTYPE_TI_ARGS (inst);
941 elt.spec = inst;
943 htab_remove_elt (type_specializations, &elt);
945 elt.tmpl = tmpl;
946 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
948 slot = htab_find_slot (type_specializations, &elt, INSERT);
949 entry = ggc_alloc_spec_entry ();
950 *entry = elt;
951 *slot = entry;
953 else
954 /* But if we've had an implicit instantiation, that's a
955 problem ([temp.expl.spec]/6). */
956 error ("specialization %qT after instantiation %qT",
957 type, inst);
960 /* Mark TYPE as a specialization. And as a result, we only
961 have one level of template argument for the innermost
962 class template. */
963 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
964 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
965 CLASSTYPE_TI_ARGS (type)
966 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
969 else if (processing_specialization)
971 /* Someday C++0x may allow for enum template specialization. */
972 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
973 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
974 pedwarn (input_location, OPT_Wpedantic, "template specialization "
975 "of %qD not allowed by ISO C++", type);
976 else
978 error ("explicit specialization of non-template %qT", type);
979 return error_mark_node;
983 return type;
986 /* Returns nonzero if we can optimize the retrieval of specializations
987 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
988 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
990 static inline bool
991 optimize_specialization_lookup_p (tree tmpl)
993 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
994 && DECL_CLASS_SCOPE_P (tmpl)
995 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
996 parameter. */
997 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
998 /* The optimized lookup depends on the fact that the
999 template arguments for the member function template apply
1000 purely to the containing class, which is not true if the
1001 containing class is an explicit or partial
1002 specialization. */
1003 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1004 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1005 && !DECL_CONV_FN_P (tmpl)
1006 /* It is possible to have a template that is not a member
1007 template and is not a member of a template class:
1009 template <typename T>
1010 struct S { friend A::f(); };
1012 Here, the friend function is a template, but the context does
1013 not have template information. The optimized lookup relies
1014 on having ARGS be the template arguments for both the class
1015 and the function template. */
1016 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1019 /* Retrieve the specialization (in the sense of [temp.spec] - a
1020 specialization is either an instantiation or an explicit
1021 specialization) of TMPL for the given template ARGS. If there is
1022 no such specialization, return NULL_TREE. The ARGS are a vector of
1023 arguments, or a vector of vectors of arguments, in the case of
1024 templates with more than one level of parameters.
1026 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1027 then we search for a partial specialization matching ARGS. This
1028 parameter is ignored if TMPL is not a class template.
1030 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1031 result is a NONTYPE_ARGUMENT_PACK. */
1033 static tree
1034 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1036 if (tmpl == NULL_TREE)
1037 return NULL_TREE;
1039 if (args == error_mark_node)
1040 return NULL_TREE;
1042 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1043 || TREE_CODE (tmpl) == FIELD_DECL);
1045 /* There should be as many levels of arguments as there are
1046 levels of parameters. */
1047 gcc_assert (TMPL_ARGS_DEPTH (args)
1048 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1049 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1050 : template_class_depth (DECL_CONTEXT (tmpl))));
1052 if (optimize_specialization_lookup_p (tmpl))
1054 tree class_template;
1055 tree class_specialization;
1056 vec<tree, va_gc> *methods;
1057 tree fns;
1058 int idx;
1060 /* The template arguments actually apply to the containing
1061 class. Find the class specialization with those
1062 arguments. */
1063 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1064 class_specialization
1065 = retrieve_specialization (class_template, args, 0);
1066 if (!class_specialization)
1067 return NULL_TREE;
1068 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1069 for the specialization. */
1070 idx = class_method_index_for_fn (class_specialization, tmpl);
1071 if (idx == -1)
1072 return NULL_TREE;
1073 /* Iterate through the methods with the indicated name, looking
1074 for the one that has an instance of TMPL. */
1075 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1076 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1078 tree fn = OVL_CURRENT (fns);
1079 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1080 /* using-declarations can add base methods to the method vec,
1081 and we don't want those here. */
1082 && DECL_CONTEXT (fn) == class_specialization)
1083 return fn;
1085 return NULL_TREE;
1087 else
1089 spec_entry *found;
1090 spec_entry elt;
1091 htab_t specializations;
1093 elt.tmpl = tmpl;
1094 elt.args = args;
1095 elt.spec = NULL_TREE;
1097 if (DECL_CLASS_TEMPLATE_P (tmpl))
1098 specializations = type_specializations;
1099 else
1100 specializations = decl_specializations;
1102 if (hash == 0)
1103 hash = hash_specialization (&elt);
1104 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1105 if (found)
1106 return found->spec;
1109 return NULL_TREE;
1112 /* Like retrieve_specialization, but for local declarations. */
1114 static tree
1115 retrieve_local_specialization (tree tmpl)
1117 void **slot;
1119 if (local_specializations == NULL)
1120 return NULL_TREE;
1122 slot = pointer_map_contains (local_specializations, tmpl);
1123 return slot ? (tree) *slot : NULL_TREE;
1126 /* Returns nonzero iff DECL is a specialization of TMPL. */
1129 is_specialization_of (tree decl, tree tmpl)
1131 tree t;
1133 if (TREE_CODE (decl) == FUNCTION_DECL)
1135 for (t = decl;
1136 t != NULL_TREE;
1137 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1138 if (t == tmpl)
1139 return 1;
1141 else
1143 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1145 for (t = TREE_TYPE (decl);
1146 t != NULL_TREE;
1147 t = CLASSTYPE_USE_TEMPLATE (t)
1148 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1149 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1150 return 1;
1153 return 0;
1156 /* Returns nonzero iff DECL is a specialization of friend declaration
1157 FRIEND_DECL according to [temp.friend]. */
1159 bool
1160 is_specialization_of_friend (tree decl, tree friend_decl)
1162 bool need_template = true;
1163 int template_depth;
1165 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1166 || TREE_CODE (decl) == TYPE_DECL);
1168 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1169 of a template class, we want to check if DECL is a specialization
1170 if this. */
1171 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1172 && DECL_TEMPLATE_INFO (friend_decl)
1173 && !DECL_USE_TEMPLATE (friend_decl))
1175 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1176 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1177 need_template = false;
1179 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1180 && !PRIMARY_TEMPLATE_P (friend_decl))
1181 need_template = false;
1183 /* There is nothing to do if this is not a template friend. */
1184 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1185 return false;
1187 if (is_specialization_of (decl, friend_decl))
1188 return true;
1190 /* [temp.friend/6]
1191 A member of a class template may be declared to be a friend of a
1192 non-template class. In this case, the corresponding member of
1193 every specialization of the class template is a friend of the
1194 class granting friendship.
1196 For example, given a template friend declaration
1198 template <class T> friend void A<T>::f();
1200 the member function below is considered a friend
1202 template <> struct A<int> {
1203 void f();
1206 For this type of template friend, TEMPLATE_DEPTH below will be
1207 nonzero. To determine if DECL is a friend of FRIEND, we first
1208 check if the enclosing class is a specialization of another. */
1210 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1211 if (template_depth
1212 && DECL_CLASS_SCOPE_P (decl)
1213 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1214 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1216 /* Next, we check the members themselves. In order to handle
1217 a few tricky cases, such as when FRIEND_DECL's are
1219 template <class T> friend void A<T>::g(T t);
1220 template <class T> template <T t> friend void A<T>::h();
1222 and DECL's are
1224 void A<int>::g(int);
1225 template <int> void A<int>::h();
1227 we need to figure out ARGS, the template arguments from
1228 the context of DECL. This is required for template substitution
1229 of `T' in the function parameter of `g' and template parameter
1230 of `h' in the above examples. Here ARGS corresponds to `int'. */
1232 tree context = DECL_CONTEXT (decl);
1233 tree args = NULL_TREE;
1234 int current_depth = 0;
1236 while (current_depth < template_depth)
1238 if (CLASSTYPE_TEMPLATE_INFO (context))
1240 if (current_depth == 0)
1241 args = TYPE_TI_ARGS (context);
1242 else
1243 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1244 current_depth++;
1246 context = TYPE_CONTEXT (context);
1249 if (TREE_CODE (decl) == FUNCTION_DECL)
1251 bool is_template;
1252 tree friend_type;
1253 tree decl_type;
1254 tree friend_args_type;
1255 tree decl_args_type;
1257 /* Make sure that both DECL and FRIEND_DECL are templates or
1258 non-templates. */
1259 is_template = DECL_TEMPLATE_INFO (decl)
1260 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1261 if (need_template ^ is_template)
1262 return false;
1263 else if (is_template)
1265 /* If both are templates, check template parameter list. */
1266 tree friend_parms
1267 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1268 args, tf_none);
1269 if (!comp_template_parms
1270 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1271 friend_parms))
1272 return false;
1274 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1276 else
1277 decl_type = TREE_TYPE (decl);
1279 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1280 tf_none, NULL_TREE);
1281 if (friend_type == error_mark_node)
1282 return false;
1284 /* Check if return types match. */
1285 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1286 return false;
1288 /* Check if function parameter types match, ignoring the
1289 `this' parameter. */
1290 friend_args_type = TYPE_ARG_TYPES (friend_type);
1291 decl_args_type = TYPE_ARG_TYPES (decl_type);
1292 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1293 friend_args_type = TREE_CHAIN (friend_args_type);
1294 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1295 decl_args_type = TREE_CHAIN (decl_args_type);
1297 return compparms (decl_args_type, friend_args_type);
1299 else
1301 /* DECL is a TYPE_DECL */
1302 bool is_template;
1303 tree decl_type = TREE_TYPE (decl);
1305 /* Make sure that both DECL and FRIEND_DECL are templates or
1306 non-templates. */
1307 is_template
1308 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1309 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1311 if (need_template ^ is_template)
1312 return false;
1313 else if (is_template)
1315 tree friend_parms;
1316 /* If both are templates, check the name of the two
1317 TEMPLATE_DECL's first because is_friend didn't. */
1318 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1319 != DECL_NAME (friend_decl))
1320 return false;
1322 /* Now check template parameter list. */
1323 friend_parms
1324 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1325 args, tf_none);
1326 return comp_template_parms
1327 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1328 friend_parms);
1330 else
1331 return (DECL_NAME (decl)
1332 == DECL_NAME (friend_decl));
1335 return false;
1338 /* Register the specialization SPEC as a specialization of TMPL with
1339 the indicated ARGS. IS_FRIEND indicates whether the specialization
1340 is actually just a friend declaration. Returns SPEC, or an
1341 equivalent prior declaration, if available.
1343 We also store instantiations of field packs in the hash table, even
1344 though they are not themselves templates, to make lookup easier. */
1346 static tree
1347 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1348 hashval_t hash)
1350 tree fn;
1351 void **slot = NULL;
1352 spec_entry elt;
1354 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1355 || (TREE_CODE (tmpl) == FIELD_DECL
1356 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1358 if (TREE_CODE (spec) == FUNCTION_DECL
1359 && uses_template_parms (DECL_TI_ARGS (spec)))
1360 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1361 register it; we want the corresponding TEMPLATE_DECL instead.
1362 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1363 the more obvious `uses_template_parms (spec)' to avoid problems
1364 with default function arguments. In particular, given
1365 something like this:
1367 template <class T> void f(T t1, T t = T())
1369 the default argument expression is not substituted for in an
1370 instantiation unless and until it is actually needed. */
1371 return spec;
1373 if (optimize_specialization_lookup_p (tmpl))
1374 /* We don't put these specializations in the hash table, but we might
1375 want to give an error about a mismatch. */
1376 fn = retrieve_specialization (tmpl, args, 0);
1377 else
1379 elt.tmpl = tmpl;
1380 elt.args = args;
1381 elt.spec = spec;
1383 if (hash == 0)
1384 hash = hash_specialization (&elt);
1386 slot =
1387 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1388 if (*slot)
1389 fn = ((spec_entry *) *slot)->spec;
1390 else
1391 fn = NULL_TREE;
1394 /* We can sometimes try to re-register a specialization that we've
1395 already got. In particular, regenerate_decl_from_template calls
1396 duplicate_decls which will update the specialization list. But,
1397 we'll still get called again here anyhow. It's more convenient
1398 to simply allow this than to try to prevent it. */
1399 if (fn == spec)
1400 return spec;
1401 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1403 if (DECL_TEMPLATE_INSTANTIATION (fn))
1405 if (DECL_ODR_USED (fn)
1406 || DECL_EXPLICIT_INSTANTIATION (fn))
1408 error ("specialization of %qD after instantiation",
1409 fn);
1410 return error_mark_node;
1412 else
1414 tree clone;
1415 /* This situation should occur only if the first
1416 specialization is an implicit instantiation, the
1417 second is an explicit specialization, and the
1418 implicit instantiation has not yet been used. That
1419 situation can occur if we have implicitly
1420 instantiated a member function and then specialized
1421 it later.
1423 We can also wind up here if a friend declaration that
1424 looked like an instantiation turns out to be a
1425 specialization:
1427 template <class T> void foo(T);
1428 class S { friend void foo<>(int) };
1429 template <> void foo(int);
1431 We transform the existing DECL in place so that any
1432 pointers to it become pointers to the updated
1433 declaration.
1435 If there was a definition for the template, but not
1436 for the specialization, we want this to look as if
1437 there were no definition, and vice versa. */
1438 DECL_INITIAL (fn) = NULL_TREE;
1439 duplicate_decls (spec, fn, is_friend);
1440 /* The call to duplicate_decls will have applied
1441 [temp.expl.spec]:
1443 An explicit specialization of a function template
1444 is inline only if it is explicitly declared to be,
1445 and independently of whether its function template
1448 to the primary function; now copy the inline bits to
1449 the various clones. */
1450 FOR_EACH_CLONE (clone, fn)
1452 DECL_DECLARED_INLINE_P (clone)
1453 = DECL_DECLARED_INLINE_P (fn);
1454 DECL_SOURCE_LOCATION (clone)
1455 = DECL_SOURCE_LOCATION (fn);
1456 DECL_DELETED_FN (clone)
1457 = DECL_DELETED_FN (fn);
1459 check_specialization_namespace (tmpl);
1461 return fn;
1464 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1466 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1467 /* Dup decl failed, but this is a new definition. Set the
1468 line number so any errors match this new
1469 definition. */
1470 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1472 return fn;
1475 else if (fn)
1476 return duplicate_decls (spec, fn, is_friend);
1478 /* A specialization must be declared in the same namespace as the
1479 template it is specializing. */
1480 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1481 && !check_specialization_namespace (tmpl))
1482 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1484 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1486 spec_entry *entry = ggc_alloc_spec_entry ();
1487 gcc_assert (tmpl && args && spec);
1488 *entry = elt;
1489 *slot = entry;
1490 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1491 && PRIMARY_TEMPLATE_P (tmpl)
1492 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1493 /* TMPL is a forward declaration of a template function; keep a list
1494 of all specializations in case we need to reassign them to a friend
1495 template later in tsubst_friend_function. */
1496 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1497 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1500 return spec;
1503 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1504 TMPL and ARGS members, ignores SPEC. */
1506 int comparing_specializations;
1508 static int
1509 eq_specializations (const void *p1, const void *p2)
1511 const spec_entry *e1 = (const spec_entry *)p1;
1512 const spec_entry *e2 = (const spec_entry *)p2;
1513 int equal;
1515 ++comparing_specializations;
1516 equal = (e1->tmpl == e2->tmpl
1517 && comp_template_args (e1->args, e2->args));
1518 --comparing_specializations;
1520 return equal;
1523 /* Returns a hash for a template TMPL and template arguments ARGS. */
1525 static hashval_t
1526 hash_tmpl_and_args (tree tmpl, tree args)
1528 hashval_t val = DECL_UID (tmpl);
1529 return iterative_hash_template_arg (args, val);
1532 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1533 ignoring SPEC. */
1535 static hashval_t
1536 hash_specialization (const void *p)
1538 const spec_entry *e = (const spec_entry *)p;
1539 return hash_tmpl_and_args (e->tmpl, e->args);
1542 /* Recursively calculate a hash value for a template argument ARG, for use
1543 in the hash tables of template specializations. */
1545 hashval_t
1546 iterative_hash_template_arg (tree arg, hashval_t val)
1548 unsigned HOST_WIDE_INT i;
1549 enum tree_code code;
1550 char tclass;
1552 if (arg == NULL_TREE)
1553 return iterative_hash_object (arg, val);
1555 if (!TYPE_P (arg))
1556 STRIP_NOPS (arg);
1558 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1559 /* We can get one of these when re-hashing a previous entry in the middle
1560 of substituting into a pack expansion. Just look through it. */
1561 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1563 code = TREE_CODE (arg);
1564 tclass = TREE_CODE_CLASS (code);
1566 val = iterative_hash_object (code, val);
1568 switch (code)
1570 case ERROR_MARK:
1571 return val;
1573 case IDENTIFIER_NODE:
1574 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1576 case TREE_VEC:
1578 int i, len = TREE_VEC_LENGTH (arg);
1579 for (i = 0; i < len; ++i)
1580 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1581 return val;
1584 case TYPE_PACK_EXPANSION:
1585 case EXPR_PACK_EXPANSION:
1586 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1587 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1589 case TYPE_ARGUMENT_PACK:
1590 case NONTYPE_ARGUMENT_PACK:
1591 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1593 case TREE_LIST:
1594 for (; arg; arg = TREE_CHAIN (arg))
1595 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1596 return val;
1598 case OVERLOAD:
1599 for (; arg; arg = OVL_NEXT (arg))
1600 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1601 return val;
1603 case CONSTRUCTOR:
1605 tree field, value;
1606 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1608 val = iterative_hash_template_arg (field, val);
1609 val = iterative_hash_template_arg (value, val);
1611 return val;
1614 case PARM_DECL:
1615 if (!DECL_ARTIFICIAL (arg))
1617 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1618 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1620 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1622 case TARGET_EXPR:
1623 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1625 case PTRMEM_CST:
1626 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1627 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1629 case TEMPLATE_PARM_INDEX:
1630 val = iterative_hash_template_arg
1631 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1632 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1633 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1635 case TRAIT_EXPR:
1636 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1637 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1638 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1640 case BASELINK:
1641 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1642 val);
1643 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1644 val);
1646 case MODOP_EXPR:
1647 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1648 code = TREE_CODE (TREE_OPERAND (arg, 1));
1649 val = iterative_hash_object (code, val);
1650 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1652 case LAMBDA_EXPR:
1653 /* A lambda can't appear in a template arg, but don't crash on
1654 erroneous input. */
1655 gcc_assert (seen_error ());
1656 return val;
1658 case CAST_EXPR:
1659 case IMPLICIT_CONV_EXPR:
1660 case STATIC_CAST_EXPR:
1661 case REINTERPRET_CAST_EXPR:
1662 case CONST_CAST_EXPR:
1663 case DYNAMIC_CAST_EXPR:
1664 case NEW_EXPR:
1665 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1666 /* Now hash operands as usual. */
1667 break;
1669 default:
1670 break;
1673 switch (tclass)
1675 case tcc_type:
1676 if (TYPE_CANONICAL (arg))
1677 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1678 val);
1679 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1680 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1681 /* Otherwise just compare the types during lookup. */
1682 return val;
1684 case tcc_declaration:
1685 case tcc_constant:
1686 return iterative_hash_expr (arg, val);
1688 default:
1689 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1691 unsigned n = cp_tree_operand_length (arg);
1692 for (i = 0; i < n; ++i)
1693 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1694 return val;
1697 gcc_unreachable ();
1698 return 0;
1701 /* Unregister the specialization SPEC as a specialization of TMPL.
1702 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1703 if the SPEC was listed as a specialization of TMPL.
1705 Note that SPEC has been ggc_freed, so we can't look inside it. */
1707 bool
1708 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1710 spec_entry *entry;
1711 spec_entry elt;
1713 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1714 elt.args = TI_ARGS (tinfo);
1715 elt.spec = NULL_TREE;
1717 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1718 if (entry != NULL)
1720 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1721 gcc_assert (new_spec != NULL_TREE);
1722 entry->spec = new_spec;
1723 return 1;
1726 return 0;
1729 /* Like register_specialization, but for local declarations. We are
1730 registering SPEC, an instantiation of TMPL. */
1732 static void
1733 register_local_specialization (tree spec, tree tmpl)
1735 void **slot;
1737 slot = pointer_map_insert (local_specializations, tmpl);
1738 *slot = spec;
1741 /* TYPE is a class type. Returns true if TYPE is an explicitly
1742 specialized class. */
1744 bool
1745 explicit_class_specialization_p (tree type)
1747 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1748 return false;
1749 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1752 /* Print the list of functions at FNS, going through all the overloads
1753 for each element of the list. Alternatively, FNS can not be a
1754 TREE_LIST, in which case it will be printed together with all the
1755 overloads.
1757 MORE and *STR should respectively be FALSE and NULL when the function
1758 is called from the outside. They are used internally on recursive
1759 calls. print_candidates manages the two parameters and leaves NULL
1760 in *STR when it ends. */
1762 static void
1763 print_candidates_1 (tree fns, bool more, const char **str)
1765 tree fn, fn2;
1766 char *spaces = NULL;
1768 for (fn = fns; fn; fn = OVL_NEXT (fn))
1769 if (TREE_CODE (fn) == TREE_LIST)
1771 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1772 print_candidates_1 (TREE_VALUE (fn2),
1773 TREE_CHAIN (fn2) || more, str);
1775 else
1777 tree cand = OVL_CURRENT (fn);
1778 if (!*str)
1780 /* Pick the prefix string. */
1781 if (!more && !OVL_NEXT (fns))
1783 inform (DECL_SOURCE_LOCATION (cand),
1784 "candidate is: %#D", cand);
1785 continue;
1788 *str = _("candidates are:");
1789 spaces = get_spaces (*str);
1791 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1792 *str = spaces ? spaces : *str;
1795 if (!more)
1797 free (spaces);
1798 *str = NULL;
1802 /* Print the list of candidate FNS in an error message. FNS can also
1803 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1805 void
1806 print_candidates (tree fns)
1808 const char *str = NULL;
1809 print_candidates_1 (fns, false, &str);
1810 gcc_assert (str == NULL);
1813 /* Returns the template (one of the functions given by TEMPLATE_ID)
1814 which can be specialized to match the indicated DECL with the
1815 explicit template args given in TEMPLATE_ID. The DECL may be
1816 NULL_TREE if none is available. In that case, the functions in
1817 TEMPLATE_ID are non-members.
1819 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1820 specialization of a member template.
1822 The TEMPLATE_COUNT is the number of references to qualifying
1823 template classes that appeared in the name of the function. See
1824 check_explicit_specialization for a more accurate description.
1826 TSK indicates what kind of template declaration (if any) is being
1827 declared. TSK_TEMPLATE indicates that the declaration given by
1828 DECL, though a FUNCTION_DECL, has template parameters, and is
1829 therefore a template function.
1831 The template args (those explicitly specified and those deduced)
1832 are output in a newly created vector *TARGS_OUT.
1834 If it is impossible to determine the result, an error message is
1835 issued. The error_mark_node is returned to indicate failure. */
1837 static tree
1838 determine_specialization (tree template_id,
1839 tree decl,
1840 tree* targs_out,
1841 int need_member_template,
1842 int template_count,
1843 tmpl_spec_kind tsk)
1845 tree fns;
1846 tree targs;
1847 tree explicit_targs;
1848 tree candidates = NULL_TREE;
1849 /* A TREE_LIST of templates of which DECL may be a specialization.
1850 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1851 corresponding TREE_PURPOSE is the set of template arguments that,
1852 when used to instantiate the template, would produce a function
1853 with the signature of DECL. */
1854 tree templates = NULL_TREE;
1855 int header_count;
1856 cp_binding_level *b;
1858 *targs_out = NULL_TREE;
1860 if (template_id == error_mark_node || decl == error_mark_node)
1861 return error_mark_node;
1863 /* We shouldn't be specializing a member template of an
1864 unspecialized class template; we already gave an error in
1865 check_specialization_scope, now avoid crashing. */
1866 if (template_count && DECL_CLASS_SCOPE_P (decl)
1867 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1869 gcc_assert (errorcount);
1870 return error_mark_node;
1873 fns = TREE_OPERAND (template_id, 0);
1874 explicit_targs = TREE_OPERAND (template_id, 1);
1876 if (fns == error_mark_node)
1877 return error_mark_node;
1879 /* Check for baselinks. */
1880 if (BASELINK_P (fns))
1881 fns = BASELINK_FUNCTIONS (fns);
1883 if (!is_overloaded_fn (fns))
1885 error ("%qD is not a function template", fns);
1886 return error_mark_node;
1889 /* Count the number of template headers specified for this
1890 specialization. */
1891 header_count = 0;
1892 for (b = current_binding_level;
1893 b->kind == sk_template_parms;
1894 b = b->level_chain)
1895 ++header_count;
1897 for (; fns; fns = OVL_NEXT (fns))
1899 tree fn = OVL_CURRENT (fns);
1901 if (TREE_CODE (fn) == TEMPLATE_DECL)
1903 tree decl_arg_types;
1904 tree fn_arg_types;
1905 tree insttype;
1907 /* In case of explicit specialization, we need to check if
1908 the number of template headers appearing in the specialization
1909 is correct. This is usually done in check_explicit_specialization,
1910 but the check done there cannot be exhaustive when specializing
1911 member functions. Consider the following code:
1913 template <> void A<int>::f(int);
1914 template <> template <> void A<int>::f(int);
1916 Assuming that A<int> is not itself an explicit specialization
1917 already, the first line specializes "f" which is a non-template
1918 member function, whilst the second line specializes "f" which
1919 is a template member function. So both lines are syntactically
1920 correct, and check_explicit_specialization does not reject
1921 them.
1923 Here, we can do better, as we are matching the specialization
1924 against the declarations. We count the number of template
1925 headers, and we check if they match TEMPLATE_COUNT + 1
1926 (TEMPLATE_COUNT is the number of qualifying template classes,
1927 plus there must be another header for the member template
1928 itself).
1930 Notice that if header_count is zero, this is not a
1931 specialization but rather a template instantiation, so there
1932 is no check we can perform here. */
1933 if (header_count && header_count != template_count + 1)
1934 continue;
1936 /* Check that the number of template arguments at the
1937 innermost level for DECL is the same as for FN. */
1938 if (current_binding_level->kind == sk_template_parms
1939 && !current_binding_level->explicit_spec_p
1940 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1941 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1942 (current_template_parms))))
1943 continue;
1945 /* DECL might be a specialization of FN. */
1946 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1947 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1949 /* For a non-static member function, we need to make sure
1950 that the const qualification is the same. Since
1951 get_bindings does not try to merge the "this" parameter,
1952 we must do the comparison explicitly. */
1953 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1954 && !same_type_p (TREE_VALUE (fn_arg_types),
1955 TREE_VALUE (decl_arg_types)))
1956 continue;
1958 /* Skip the "this" parameter and, for constructors of
1959 classes with virtual bases, the VTT parameter. A
1960 full specialization of a constructor will have a VTT
1961 parameter, but a template never will. */
1962 decl_arg_types
1963 = skip_artificial_parms_for (decl, decl_arg_types);
1964 fn_arg_types
1965 = skip_artificial_parms_for (fn, fn_arg_types);
1967 /* Function templates cannot be specializations; there are
1968 no partial specializations of functions. Therefore, if
1969 the type of DECL does not match FN, there is no
1970 match. */
1971 if (tsk == tsk_template)
1973 if (compparms (fn_arg_types, decl_arg_types))
1974 candidates = tree_cons (NULL_TREE, fn, candidates);
1975 continue;
1978 /* See whether this function might be a specialization of this
1979 template. Suppress access control because we might be trying
1980 to make this specialization a friend, and we have already done
1981 access control for the declaration of the specialization. */
1982 push_deferring_access_checks (dk_no_check);
1983 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1984 pop_deferring_access_checks ();
1986 if (!targs)
1987 /* We cannot deduce template arguments that when used to
1988 specialize TMPL will produce DECL. */
1989 continue;
1991 /* Make sure that the deduced arguments actually work. */
1992 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1993 if (insttype == error_mark_node)
1994 continue;
1995 fn_arg_types
1996 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1997 if (!compparms (fn_arg_types, decl_arg_types))
1998 continue;
2000 /* Save this template, and the arguments deduced. */
2001 templates = tree_cons (targs, fn, templates);
2003 else if (need_member_template)
2004 /* FN is an ordinary member function, and we need a
2005 specialization of a member template. */
2007 else if (TREE_CODE (fn) != FUNCTION_DECL)
2008 /* We can get IDENTIFIER_NODEs here in certain erroneous
2009 cases. */
2011 else if (!DECL_FUNCTION_MEMBER_P (fn))
2012 /* This is just an ordinary non-member function. Nothing can
2013 be a specialization of that. */
2015 else if (DECL_ARTIFICIAL (fn))
2016 /* Cannot specialize functions that are created implicitly. */
2018 else
2020 tree decl_arg_types;
2022 /* This is an ordinary member function. However, since
2023 we're here, we can assume its enclosing class is a
2024 template class. For example,
2026 template <typename T> struct S { void f(); };
2027 template <> void S<int>::f() {}
2029 Here, S<int>::f is a non-template, but S<int> is a
2030 template class. If FN has the same type as DECL, we
2031 might be in business. */
2033 if (!DECL_TEMPLATE_INFO (fn))
2034 /* Its enclosing class is an explicit specialization
2035 of a template class. This is not a candidate. */
2036 continue;
2038 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2039 TREE_TYPE (TREE_TYPE (fn))))
2040 /* The return types differ. */
2041 continue;
2043 /* Adjust the type of DECL in case FN is a static member. */
2044 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2045 if (DECL_STATIC_FUNCTION_P (fn)
2046 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2047 decl_arg_types = TREE_CHAIN (decl_arg_types);
2049 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2050 decl_arg_types))
2051 /* They match! */
2052 candidates = tree_cons (NULL_TREE, fn, candidates);
2056 if (templates && TREE_CHAIN (templates))
2058 /* We have:
2060 [temp.expl.spec]
2062 It is possible for a specialization with a given function
2063 signature to be instantiated from more than one function
2064 template. In such cases, explicit specification of the
2065 template arguments must be used to uniquely identify the
2066 function template specialization being specialized.
2068 Note that here, there's no suggestion that we're supposed to
2069 determine which of the candidate templates is most
2070 specialized. However, we, also have:
2072 [temp.func.order]
2074 Partial ordering of overloaded function template
2075 declarations is used in the following contexts to select
2076 the function template to which a function template
2077 specialization refers:
2079 -- when an explicit specialization refers to a function
2080 template.
2082 So, we do use the partial ordering rules, at least for now.
2083 This extension can only serve to make invalid programs valid,
2084 so it's safe. And, there is strong anecdotal evidence that
2085 the committee intended the partial ordering rules to apply;
2086 the EDG front end has that behavior, and John Spicer claims
2087 that the committee simply forgot to delete the wording in
2088 [temp.expl.spec]. */
2089 tree tmpl = most_specialized_instantiation (templates);
2090 if (tmpl != error_mark_node)
2092 templates = tmpl;
2093 TREE_CHAIN (templates) = NULL_TREE;
2097 if (templates == NULL_TREE && candidates == NULL_TREE)
2099 error ("template-id %qD for %q+D does not match any template "
2100 "declaration", template_id, decl);
2101 if (header_count && header_count != template_count + 1)
2102 inform (input_location, "saw %d %<template<>%>, need %d for "
2103 "specializing a member function template",
2104 header_count, template_count + 1);
2105 return error_mark_node;
2107 else if ((templates && TREE_CHAIN (templates))
2108 || (candidates && TREE_CHAIN (candidates))
2109 || (templates && candidates))
2111 error ("ambiguous template specialization %qD for %q+D",
2112 template_id, decl);
2113 candidates = chainon (candidates, templates);
2114 print_candidates (candidates);
2115 return error_mark_node;
2118 /* We have one, and exactly one, match. */
2119 if (candidates)
2121 tree fn = TREE_VALUE (candidates);
2122 *targs_out = copy_node (DECL_TI_ARGS (fn));
2123 /* DECL is a re-declaration or partial instantiation of a template
2124 function. */
2125 if (TREE_CODE (fn) == TEMPLATE_DECL)
2126 return fn;
2127 /* It was a specialization of an ordinary member function in a
2128 template class. */
2129 return DECL_TI_TEMPLATE (fn);
2132 /* It was a specialization of a template. */
2133 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2134 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2136 *targs_out = copy_node (targs);
2137 SET_TMPL_ARGS_LEVEL (*targs_out,
2138 TMPL_ARGS_DEPTH (*targs_out),
2139 TREE_PURPOSE (templates));
2141 else
2142 *targs_out = TREE_PURPOSE (templates);
2143 return TREE_VALUE (templates);
2146 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2147 but with the default argument values filled in from those in the
2148 TMPL_TYPES. */
2150 static tree
2151 copy_default_args_to_explicit_spec_1 (tree spec_types,
2152 tree tmpl_types)
2154 tree new_spec_types;
2156 if (!spec_types)
2157 return NULL_TREE;
2159 if (spec_types == void_list_node)
2160 return void_list_node;
2162 /* Substitute into the rest of the list. */
2163 new_spec_types =
2164 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2165 TREE_CHAIN (tmpl_types));
2167 /* Add the default argument for this parameter. */
2168 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2169 TREE_VALUE (spec_types),
2170 new_spec_types);
2173 /* DECL is an explicit specialization. Replicate default arguments
2174 from the template it specializes. (That way, code like:
2176 template <class T> void f(T = 3);
2177 template <> void f(double);
2178 void g () { f (); }
2180 works, as required.) An alternative approach would be to look up
2181 the correct default arguments at the call-site, but this approach
2182 is consistent with how implicit instantiations are handled. */
2184 static void
2185 copy_default_args_to_explicit_spec (tree decl)
2187 tree tmpl;
2188 tree spec_types;
2189 tree tmpl_types;
2190 tree new_spec_types;
2191 tree old_type;
2192 tree new_type;
2193 tree t;
2194 tree object_type = NULL_TREE;
2195 tree in_charge = NULL_TREE;
2196 tree vtt = NULL_TREE;
2198 /* See if there's anything we need to do. */
2199 tmpl = DECL_TI_TEMPLATE (decl);
2200 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2201 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2202 if (TREE_PURPOSE (t))
2203 break;
2204 if (!t)
2205 return;
2207 old_type = TREE_TYPE (decl);
2208 spec_types = TYPE_ARG_TYPES (old_type);
2210 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2212 /* Remove the this pointer, but remember the object's type for
2213 CV quals. */
2214 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2215 spec_types = TREE_CHAIN (spec_types);
2216 tmpl_types = TREE_CHAIN (tmpl_types);
2218 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2220 /* DECL may contain more parameters than TMPL due to the extra
2221 in-charge parameter in constructors and destructors. */
2222 in_charge = spec_types;
2223 spec_types = TREE_CHAIN (spec_types);
2225 if (DECL_HAS_VTT_PARM_P (decl))
2227 vtt = spec_types;
2228 spec_types = TREE_CHAIN (spec_types);
2232 /* Compute the merged default arguments. */
2233 new_spec_types =
2234 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2236 /* Compute the new FUNCTION_TYPE. */
2237 if (object_type)
2239 if (vtt)
2240 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2241 TREE_VALUE (vtt),
2242 new_spec_types);
2244 if (in_charge)
2245 /* Put the in-charge parameter back. */
2246 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2247 TREE_VALUE (in_charge),
2248 new_spec_types);
2250 new_type = build_method_type_directly (object_type,
2251 TREE_TYPE (old_type),
2252 new_spec_types);
2254 else
2255 new_type = build_function_type (TREE_TYPE (old_type),
2256 new_spec_types);
2257 new_type = cp_build_type_attribute_variant (new_type,
2258 TYPE_ATTRIBUTES (old_type));
2259 new_type = build_exception_variant (new_type,
2260 TYPE_RAISES_EXCEPTIONS (old_type));
2261 TREE_TYPE (decl) = new_type;
2264 /* Return the number of template headers we expect to see for a definition
2265 or specialization of CTYPE or one of its non-template members. */
2268 num_template_headers_for_class (tree ctype)
2270 int num_templates = 0;
2272 while (ctype && CLASS_TYPE_P (ctype))
2274 /* You're supposed to have one `template <...>' for every
2275 template class, but you don't need one for a full
2276 specialization. For example:
2278 template <class T> struct S{};
2279 template <> struct S<int> { void f(); };
2280 void S<int>::f () {}
2282 is correct; there shouldn't be a `template <>' for the
2283 definition of `S<int>::f'. */
2284 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2285 /* If CTYPE does not have template information of any
2286 kind, then it is not a template, nor is it nested
2287 within a template. */
2288 break;
2289 if (explicit_class_specialization_p (ctype))
2290 break;
2291 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2292 ++num_templates;
2294 ctype = TYPE_CONTEXT (ctype);
2297 return num_templates;
2300 /* Do a simple sanity check on the template headers that precede the
2301 variable declaration DECL. */
2303 void
2304 check_template_variable (tree decl)
2306 tree ctx = CP_DECL_CONTEXT (decl);
2307 int wanted = num_template_headers_for_class (ctx);
2308 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2309 permerror (DECL_SOURCE_LOCATION (decl),
2310 "%qD is not a static data member of a class template", decl);
2311 else if (template_header_count > wanted)
2313 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2314 "too many template headers for %D (should be %d)",
2315 decl, wanted);
2316 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2317 inform (DECL_SOURCE_LOCATION (decl),
2318 "members of an explicitly specialized class are defined "
2319 "without a template header");
2323 /* Check to see if the function just declared, as indicated in
2324 DECLARATOR, and in DECL, is a specialization of a function
2325 template. We may also discover that the declaration is an explicit
2326 instantiation at this point.
2328 Returns DECL, or an equivalent declaration that should be used
2329 instead if all goes well. Issues an error message if something is
2330 amiss. Returns error_mark_node if the error is not easily
2331 recoverable.
2333 FLAGS is a bitmask consisting of the following flags:
2335 2: The function has a definition.
2336 4: The function is a friend.
2338 The TEMPLATE_COUNT is the number of references to qualifying
2339 template classes that appeared in the name of the function. For
2340 example, in
2342 template <class T> struct S { void f(); };
2343 void S<int>::f();
2345 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2346 classes are not counted in the TEMPLATE_COUNT, so that in
2348 template <class T> struct S {};
2349 template <> struct S<int> { void f(); }
2350 template <> void S<int>::f();
2352 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2353 invalid; there should be no template <>.)
2355 If the function is a specialization, it is marked as such via
2356 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2357 is set up correctly, and it is added to the list of specializations
2358 for that template. */
2360 tree
2361 check_explicit_specialization (tree declarator,
2362 tree decl,
2363 int template_count,
2364 int flags)
2366 int have_def = flags & 2;
2367 int is_friend = flags & 4;
2368 int specialization = 0;
2369 int explicit_instantiation = 0;
2370 int member_specialization = 0;
2371 tree ctype = DECL_CLASS_CONTEXT (decl);
2372 tree dname = DECL_NAME (decl);
2373 tmpl_spec_kind tsk;
2375 if (is_friend)
2377 if (!processing_specialization)
2378 tsk = tsk_none;
2379 else
2380 tsk = tsk_excessive_parms;
2382 else
2383 tsk = current_tmpl_spec_kind (template_count);
2385 switch (tsk)
2387 case tsk_none:
2388 if (processing_specialization)
2390 specialization = 1;
2391 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2393 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2395 if (is_friend)
2396 /* This could be something like:
2398 template <class T> void f(T);
2399 class S { friend void f<>(int); } */
2400 specialization = 1;
2401 else
2403 /* This case handles bogus declarations like template <>
2404 template <class T> void f<int>(); */
2406 error ("template-id %qD in declaration of primary template",
2407 declarator);
2408 return decl;
2411 break;
2413 case tsk_invalid_member_spec:
2414 /* The error has already been reported in
2415 check_specialization_scope. */
2416 return error_mark_node;
2418 case tsk_invalid_expl_inst:
2419 error ("template parameter list used in explicit instantiation");
2421 /* Fall through. */
2423 case tsk_expl_inst:
2424 if (have_def)
2425 error ("definition provided for explicit instantiation");
2427 explicit_instantiation = 1;
2428 break;
2430 case tsk_excessive_parms:
2431 case tsk_insufficient_parms:
2432 if (tsk == tsk_excessive_parms)
2433 error ("too many template parameter lists in declaration of %qD",
2434 decl);
2435 else if (template_header_count)
2436 error("too few template parameter lists in declaration of %qD", decl);
2437 else
2438 error("explicit specialization of %qD must be introduced by "
2439 "%<template <>%>", decl);
2441 /* Fall through. */
2442 case tsk_expl_spec:
2443 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2444 if (ctype)
2445 member_specialization = 1;
2446 else
2447 specialization = 1;
2448 break;
2450 case tsk_template:
2451 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2453 /* This case handles bogus declarations like template <>
2454 template <class T> void f<int>(); */
2456 if (uses_template_parms (declarator))
2457 error ("function template partial specialization %qD "
2458 "is not allowed", declarator);
2459 else
2460 error ("template-id %qD in declaration of primary template",
2461 declarator);
2462 return decl;
2465 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2466 /* This is a specialization of a member template, without
2467 specialization the containing class. Something like:
2469 template <class T> struct S {
2470 template <class U> void f (U);
2472 template <> template <class U> void S<int>::f(U) {}
2474 That's a specialization -- but of the entire template. */
2475 specialization = 1;
2476 break;
2478 default:
2479 gcc_unreachable ();
2482 if (specialization || member_specialization)
2484 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2485 for (; t; t = TREE_CHAIN (t))
2486 if (TREE_PURPOSE (t))
2488 permerror (input_location,
2489 "default argument specified in explicit specialization");
2490 break;
2494 if (specialization || member_specialization || explicit_instantiation)
2496 tree tmpl = NULL_TREE;
2497 tree targs = NULL_TREE;
2499 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2500 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2502 tree fns;
2504 gcc_assert (identifier_p (declarator));
2505 if (ctype)
2506 fns = dname;
2507 else
2509 /* If there is no class context, the explicit instantiation
2510 must be at namespace scope. */
2511 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2513 /* Find the namespace binding, using the declaration
2514 context. */
2515 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2516 false, true);
2517 if (fns == error_mark_node || !is_overloaded_fn (fns))
2519 error ("%qD is not a template function", dname);
2520 fns = error_mark_node;
2522 else
2524 tree fn = OVL_CURRENT (fns);
2525 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2526 CP_DECL_CONTEXT (fn)))
2527 error ("%qD is not declared in %qD",
2528 decl, current_namespace);
2532 declarator = lookup_template_function (fns, NULL_TREE);
2535 if (declarator == error_mark_node)
2536 return error_mark_node;
2538 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2540 if (!explicit_instantiation)
2541 /* A specialization in class scope. This is invalid,
2542 but the error will already have been flagged by
2543 check_specialization_scope. */
2544 return error_mark_node;
2545 else
2547 /* It's not valid to write an explicit instantiation in
2548 class scope, e.g.:
2550 class C { template void f(); }
2552 This case is caught by the parser. However, on
2553 something like:
2555 template class C { void f(); };
2557 (which is invalid) we can get here. The error will be
2558 issued later. */
2562 return decl;
2564 else if (ctype != NULL_TREE
2565 && (identifier_p (TREE_OPERAND (declarator, 0))))
2567 /* Find the list of functions in ctype that have the same
2568 name as the declared function. */
2569 tree name = TREE_OPERAND (declarator, 0);
2570 tree fns = NULL_TREE;
2571 int idx;
2573 if (constructor_name_p (name, ctype))
2575 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2577 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2578 : !CLASSTYPE_DESTRUCTORS (ctype))
2580 /* From [temp.expl.spec]:
2582 If such an explicit specialization for the member
2583 of a class template names an implicitly-declared
2584 special member function (clause _special_), the
2585 program is ill-formed.
2587 Similar language is found in [temp.explicit]. */
2588 error ("specialization of implicitly-declared special member function");
2589 return error_mark_node;
2592 name = is_constructor ? ctor_identifier : dtor_identifier;
2595 if (!DECL_CONV_FN_P (decl))
2597 idx = lookup_fnfields_1 (ctype, name);
2598 if (idx >= 0)
2599 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2601 else
2603 vec<tree, va_gc> *methods;
2604 tree ovl;
2606 /* For a type-conversion operator, we cannot do a
2607 name-based lookup. We might be looking for `operator
2608 int' which will be a specialization of `operator T'.
2609 So, we find *all* the conversion operators, and then
2610 select from them. */
2611 fns = NULL_TREE;
2613 methods = CLASSTYPE_METHOD_VEC (ctype);
2614 if (methods)
2615 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2616 methods->iterate (idx, &ovl);
2617 ++idx)
2619 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2620 /* There are no more conversion functions. */
2621 break;
2623 /* Glue all these conversion functions together
2624 with those we already have. */
2625 for (; ovl; ovl = OVL_NEXT (ovl))
2626 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2630 if (fns == NULL_TREE)
2632 error ("no member function %qD declared in %qT", name, ctype);
2633 return error_mark_node;
2635 else
2636 TREE_OPERAND (declarator, 0) = fns;
2639 /* Figure out what exactly is being specialized at this point.
2640 Note that for an explicit instantiation, even one for a
2641 member function, we cannot tell apriori whether the
2642 instantiation is for a member template, or just a member
2643 function of a template class. Even if a member template is
2644 being instantiated, the member template arguments may be
2645 elided if they can be deduced from the rest of the
2646 declaration. */
2647 tmpl = determine_specialization (declarator, decl,
2648 &targs,
2649 member_specialization,
2650 template_count,
2651 tsk);
2653 if (!tmpl || tmpl == error_mark_node)
2654 /* We couldn't figure out what this declaration was
2655 specializing. */
2656 return error_mark_node;
2657 else
2659 tree gen_tmpl = most_general_template (tmpl);
2661 if (explicit_instantiation)
2663 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2664 is done by do_decl_instantiation later. */
2666 int arg_depth = TMPL_ARGS_DEPTH (targs);
2667 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2669 if (arg_depth > parm_depth)
2671 /* If TMPL is not the most general template (for
2672 example, if TMPL is a friend template that is
2673 injected into namespace scope), then there will
2674 be too many levels of TARGS. Remove some of them
2675 here. */
2676 int i;
2677 tree new_targs;
2679 new_targs = make_tree_vec (parm_depth);
2680 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2681 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2682 = TREE_VEC_ELT (targs, i);
2683 targs = new_targs;
2686 return instantiate_template (tmpl, targs, tf_error);
2689 /* If we thought that the DECL was a member function, but it
2690 turns out to be specializing a static member function,
2691 make DECL a static member function as well. */
2692 if (DECL_STATIC_FUNCTION_P (tmpl)
2693 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2694 revert_static_member_fn (decl);
2696 /* If this is a specialization of a member template of a
2697 template class, we want to return the TEMPLATE_DECL, not
2698 the specialization of it. */
2699 if (tsk == tsk_template)
2701 tree result = DECL_TEMPLATE_RESULT (tmpl);
2702 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2703 DECL_INITIAL (result) = NULL_TREE;
2704 if (have_def)
2706 tree parm;
2707 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2708 DECL_SOURCE_LOCATION (result)
2709 = DECL_SOURCE_LOCATION (decl);
2710 /* We want to use the argument list specified in the
2711 definition, not in the original declaration. */
2712 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2713 for (parm = DECL_ARGUMENTS (result); parm;
2714 parm = DECL_CHAIN (parm))
2715 DECL_CONTEXT (parm) = result;
2717 return register_specialization (tmpl, gen_tmpl, targs,
2718 is_friend, 0);
2721 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2722 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2724 /* Inherit default function arguments from the template
2725 DECL is specializing. */
2726 copy_default_args_to_explicit_spec (decl);
2728 /* This specialization has the same protection as the
2729 template it specializes. */
2730 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2731 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2733 /* 7.1.1-1 [dcl.stc]
2735 A storage-class-specifier shall not be specified in an
2736 explicit specialization...
2738 The parser rejects these, so unless action is taken here,
2739 explicit function specializations will always appear with
2740 global linkage.
2742 The action recommended by the C++ CWG in response to C++
2743 defect report 605 is to make the storage class and linkage
2744 of the explicit specialization match the templated function:
2746 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2748 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2750 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2751 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2753 /* This specialization has the same linkage and visibility as
2754 the function template it specializes. */
2755 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2756 if (! TREE_PUBLIC (decl))
2758 DECL_INTERFACE_KNOWN (decl) = 1;
2759 DECL_NOT_REALLY_EXTERN (decl) = 1;
2761 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2762 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2764 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2765 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2769 /* If DECL is a friend declaration, declared using an
2770 unqualified name, the namespace associated with DECL may
2771 have been set incorrectly. For example, in:
2773 template <typename T> void f(T);
2774 namespace N {
2775 struct S { friend void f<int>(int); }
2778 we will have set the DECL_CONTEXT for the friend
2779 declaration to N, rather than to the global namespace. */
2780 if (DECL_NAMESPACE_SCOPE_P (decl))
2781 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2783 if (is_friend && !have_def)
2784 /* This is not really a declaration of a specialization.
2785 It's just the name of an instantiation. But, it's not
2786 a request for an instantiation, either. */
2787 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2789 /* Register this specialization so that we can find it
2790 again. */
2791 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2793 /* A 'structor should already have clones. */
2794 gcc_assert (decl == error_mark_node
2795 || !(DECL_CONSTRUCTOR_P (decl)
2796 || DECL_DESTRUCTOR_P (decl))
2797 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
2801 return decl;
2804 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2805 parameters. These are represented in the same format used for
2806 DECL_TEMPLATE_PARMS. */
2809 comp_template_parms (const_tree parms1, const_tree parms2)
2811 const_tree p1;
2812 const_tree p2;
2814 if (parms1 == parms2)
2815 return 1;
2817 for (p1 = parms1, p2 = parms2;
2818 p1 != NULL_TREE && p2 != NULL_TREE;
2819 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2821 tree t1 = TREE_VALUE (p1);
2822 tree t2 = TREE_VALUE (p2);
2823 int i;
2825 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2826 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2828 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2829 return 0;
2831 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2833 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2834 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2836 /* If either of the template parameters are invalid, assume
2837 they match for the sake of error recovery. */
2838 if (parm1 == error_mark_node || parm2 == error_mark_node)
2839 return 1;
2841 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2842 return 0;
2844 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2845 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2846 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2847 continue;
2848 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2849 return 0;
2853 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2854 /* One set of parameters has more parameters lists than the
2855 other. */
2856 return 0;
2858 return 1;
2861 /* Determine whether PARM is a parameter pack. */
2863 bool
2864 template_parameter_pack_p (const_tree parm)
2866 /* Determine if we have a non-type template parameter pack. */
2867 if (TREE_CODE (parm) == PARM_DECL)
2868 return (DECL_TEMPLATE_PARM_P (parm)
2869 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2870 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2871 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2873 /* If this is a list of template parameters, we could get a
2874 TYPE_DECL or a TEMPLATE_DECL. */
2875 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2876 parm = TREE_TYPE (parm);
2878 /* Otherwise it must be a type template parameter. */
2879 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2880 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2881 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2884 /* Determine if T is a function parameter pack. */
2886 bool
2887 function_parameter_pack_p (const_tree t)
2889 if (t && TREE_CODE (t) == PARM_DECL)
2890 return DECL_PACK_P (t);
2891 return false;
2894 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2895 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2897 tree
2898 get_function_template_decl (const_tree primary_func_tmpl_inst)
2900 if (! primary_func_tmpl_inst
2901 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2902 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2903 return NULL;
2905 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2908 /* Return true iff the function parameter PARAM_DECL was expanded
2909 from the function parameter pack PACK. */
2911 bool
2912 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2914 if (DECL_ARTIFICIAL (param_decl)
2915 || !function_parameter_pack_p (pack))
2916 return false;
2918 /* The parameter pack and its pack arguments have the same
2919 DECL_PARM_INDEX. */
2920 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2923 /* Determine whether ARGS describes a variadic template args list,
2924 i.e., one that is terminated by a template argument pack. */
2926 static bool
2927 template_args_variadic_p (tree args)
2929 int nargs;
2930 tree last_parm;
2932 if (args == NULL_TREE)
2933 return false;
2935 args = INNERMOST_TEMPLATE_ARGS (args);
2936 nargs = TREE_VEC_LENGTH (args);
2938 if (nargs == 0)
2939 return false;
2941 last_parm = TREE_VEC_ELT (args, nargs - 1);
2943 return ARGUMENT_PACK_P (last_parm);
2946 /* Generate a new name for the parameter pack name NAME (an
2947 IDENTIFIER_NODE) that incorporates its */
2949 static tree
2950 make_ith_pack_parameter_name (tree name, int i)
2952 /* Munge the name to include the parameter index. */
2953 #define NUMBUF_LEN 128
2954 char numbuf[NUMBUF_LEN];
2955 char* newname;
2956 int newname_len;
2958 if (name == NULL_TREE)
2959 return name;
2960 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2961 newname_len = IDENTIFIER_LENGTH (name)
2962 + strlen (numbuf) + 2;
2963 newname = (char*)alloca (newname_len);
2964 snprintf (newname, newname_len,
2965 "%s#%i", IDENTIFIER_POINTER (name), i);
2966 return get_identifier (newname);
2969 /* Return true if T is a primary function, class or alias template
2970 instantiation. */
2972 bool
2973 primary_template_instantiation_p (const_tree t)
2975 if (!t)
2976 return false;
2978 if (TREE_CODE (t) == FUNCTION_DECL)
2979 return DECL_LANG_SPECIFIC (t)
2980 && DECL_TEMPLATE_INSTANTIATION (t)
2981 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2982 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2983 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2984 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2985 else if (alias_template_specialization_p (t))
2986 return true;
2987 return false;
2990 /* Return true if PARM is a template template parameter. */
2992 bool
2993 template_template_parameter_p (const_tree parm)
2995 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2998 /* Return true iff PARM is a DECL representing a type template
2999 parameter. */
3001 bool
3002 template_type_parameter_p (const_tree parm)
3004 return (parm
3005 && (TREE_CODE (parm) == TYPE_DECL
3006 || TREE_CODE (parm) == TEMPLATE_DECL)
3007 && DECL_TEMPLATE_PARM_P (parm));
3010 /* Return the template parameters of T if T is a
3011 primary template instantiation, NULL otherwise. */
3013 tree
3014 get_primary_template_innermost_parameters (const_tree t)
3016 tree parms = NULL, template_info = NULL;
3018 if ((template_info = get_template_info (t))
3019 && primary_template_instantiation_p (t))
3020 parms = INNERMOST_TEMPLATE_PARMS
3021 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3023 return parms;
3026 /* Return the template parameters of the LEVELth level from the full list
3027 of template parameters PARMS. */
3029 tree
3030 get_template_parms_at_level (tree parms, int level)
3032 tree p;
3033 if (!parms
3034 || TREE_CODE (parms) != TREE_LIST
3035 || level > TMPL_PARMS_DEPTH (parms))
3036 return NULL_TREE;
3038 for (p = parms; p; p = TREE_CHAIN (p))
3039 if (TMPL_PARMS_DEPTH (p) == level)
3040 return p;
3042 return NULL_TREE;
3045 /* Returns the template arguments of T if T is a template instantiation,
3046 NULL otherwise. */
3048 tree
3049 get_template_innermost_arguments (const_tree t)
3051 tree args = NULL, template_info = NULL;
3053 if ((template_info = get_template_info (t))
3054 && TI_ARGS (template_info))
3055 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3057 return args;
3060 /* Return the argument pack elements of T if T is a template argument pack,
3061 NULL otherwise. */
3063 tree
3064 get_template_argument_pack_elems (const_tree t)
3066 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3067 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3068 return NULL;
3070 return ARGUMENT_PACK_ARGS (t);
3073 /* Structure used to track the progress of find_parameter_packs_r. */
3074 struct find_parameter_pack_data
3076 /* TREE_LIST that will contain all of the parameter packs found by
3077 the traversal. */
3078 tree* parameter_packs;
3080 /* Set of AST nodes that have been visited by the traversal. */
3081 struct pointer_set_t *visited;
3084 /* Identifies all of the argument packs that occur in a template
3085 argument and appends them to the TREE_LIST inside DATA, which is a
3086 find_parameter_pack_data structure. This is a subroutine of
3087 make_pack_expansion and uses_parameter_packs. */
3088 static tree
3089 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3091 tree t = *tp;
3092 struct find_parameter_pack_data* ppd =
3093 (struct find_parameter_pack_data*)data;
3094 bool parameter_pack_p = false;
3096 /* Handle type aliases/typedefs. */
3097 if (TYPE_ALIAS_P (t))
3099 if (TYPE_TEMPLATE_INFO (t))
3100 cp_walk_tree (&TYPE_TI_ARGS (t),
3101 &find_parameter_packs_r,
3102 ppd, ppd->visited);
3103 *walk_subtrees = 0;
3104 return NULL_TREE;
3107 /* Identify whether this is a parameter pack or not. */
3108 switch (TREE_CODE (t))
3110 case TEMPLATE_PARM_INDEX:
3111 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3112 parameter_pack_p = true;
3113 break;
3115 case TEMPLATE_TYPE_PARM:
3116 t = TYPE_MAIN_VARIANT (t);
3117 case TEMPLATE_TEMPLATE_PARM:
3118 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3119 parameter_pack_p = true;
3120 break;
3122 case FIELD_DECL:
3123 case PARM_DECL:
3124 if (DECL_PACK_P (t))
3126 /* We don't want to walk into the type of a PARM_DECL,
3127 because we don't want to see the type parameter pack. */
3128 *walk_subtrees = 0;
3129 parameter_pack_p = true;
3131 break;
3133 /* Look through a lambda capture proxy to the field pack. */
3134 case VAR_DECL:
3135 if (DECL_HAS_VALUE_EXPR_P (t))
3137 tree v = DECL_VALUE_EXPR (t);
3138 cp_walk_tree (&v,
3139 &find_parameter_packs_r,
3140 ppd, ppd->visited);
3141 *walk_subtrees = 0;
3143 break;
3145 case BASES:
3146 parameter_pack_p = true;
3147 break;
3148 default:
3149 /* Not a parameter pack. */
3150 break;
3153 if (parameter_pack_p)
3155 /* Add this parameter pack to the list. */
3156 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3159 if (TYPE_P (t))
3160 cp_walk_tree (&TYPE_CONTEXT (t),
3161 &find_parameter_packs_r, ppd, ppd->visited);
3163 /* This switch statement will return immediately if we don't find a
3164 parameter pack. */
3165 switch (TREE_CODE (t))
3167 case TEMPLATE_PARM_INDEX:
3168 return NULL_TREE;
3170 case BOUND_TEMPLATE_TEMPLATE_PARM:
3171 /* Check the template itself. */
3172 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3173 &find_parameter_packs_r, ppd, ppd->visited);
3174 /* Check the template arguments. */
3175 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3176 ppd->visited);
3177 *walk_subtrees = 0;
3178 return NULL_TREE;
3180 case TEMPLATE_TYPE_PARM:
3181 case TEMPLATE_TEMPLATE_PARM:
3182 return NULL_TREE;
3184 case PARM_DECL:
3185 return NULL_TREE;
3187 case RECORD_TYPE:
3188 if (TYPE_PTRMEMFUNC_P (t))
3189 return NULL_TREE;
3190 /* Fall through. */
3192 case UNION_TYPE:
3193 case ENUMERAL_TYPE:
3194 if (TYPE_TEMPLATE_INFO (t))
3195 cp_walk_tree (&TYPE_TI_ARGS (t),
3196 &find_parameter_packs_r, ppd, ppd->visited);
3198 *walk_subtrees = 0;
3199 return NULL_TREE;
3201 case CONSTRUCTOR:
3202 case TEMPLATE_DECL:
3203 cp_walk_tree (&TREE_TYPE (t),
3204 &find_parameter_packs_r, ppd, ppd->visited);
3205 return NULL_TREE;
3207 case TYPENAME_TYPE:
3208 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3209 ppd, ppd->visited);
3210 *walk_subtrees = 0;
3211 return NULL_TREE;
3213 case TYPE_PACK_EXPANSION:
3214 case EXPR_PACK_EXPANSION:
3215 *walk_subtrees = 0;
3216 return NULL_TREE;
3218 case INTEGER_TYPE:
3219 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3220 ppd, ppd->visited);
3221 *walk_subtrees = 0;
3222 return NULL_TREE;
3224 case IDENTIFIER_NODE:
3225 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3226 ppd->visited);
3227 *walk_subtrees = 0;
3228 return NULL_TREE;
3230 default:
3231 return NULL_TREE;
3234 return NULL_TREE;
3237 /* Determines if the expression or type T uses any parameter packs. */
3238 bool
3239 uses_parameter_packs (tree t)
3241 tree parameter_packs = NULL_TREE;
3242 struct find_parameter_pack_data ppd;
3243 ppd.parameter_packs = &parameter_packs;
3244 ppd.visited = pointer_set_create ();
3245 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3246 pointer_set_destroy (ppd.visited);
3247 return parameter_packs != NULL_TREE;
3250 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3251 representation a base-class initializer into a parameter pack
3252 expansion. If all goes well, the resulting node will be an
3253 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3254 respectively. */
3255 tree
3256 make_pack_expansion (tree arg)
3258 tree result;
3259 tree parameter_packs = NULL_TREE;
3260 bool for_types = false;
3261 struct find_parameter_pack_data ppd;
3263 if (!arg || arg == error_mark_node)
3264 return arg;
3266 if (TREE_CODE (arg) == TREE_LIST)
3268 /* The only time we will see a TREE_LIST here is for a base
3269 class initializer. In this case, the TREE_PURPOSE will be a
3270 _TYPE node (representing the base class expansion we're
3271 initializing) and the TREE_VALUE will be a TREE_LIST
3272 containing the initialization arguments.
3274 The resulting expansion looks somewhat different from most
3275 expansions. Rather than returning just one _EXPANSION, we
3276 return a TREE_LIST whose TREE_PURPOSE is a
3277 TYPE_PACK_EXPANSION containing the bases that will be
3278 initialized. The TREE_VALUE will be identical to the
3279 original TREE_VALUE, which is a list of arguments that will
3280 be passed to each base. We do not introduce any new pack
3281 expansion nodes into the TREE_VALUE (although it is possible
3282 that some already exist), because the TREE_PURPOSE and
3283 TREE_VALUE all need to be expanded together with the same
3284 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3285 resulting TREE_PURPOSE will mention the parameter packs in
3286 both the bases and the arguments to the bases. */
3287 tree purpose;
3288 tree value;
3289 tree parameter_packs = NULL_TREE;
3291 /* Determine which parameter packs will be used by the base
3292 class expansion. */
3293 ppd.visited = pointer_set_create ();
3294 ppd.parameter_packs = &parameter_packs;
3295 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3296 &ppd, ppd.visited);
3298 if (parameter_packs == NULL_TREE)
3300 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3301 pointer_set_destroy (ppd.visited);
3302 return error_mark_node;
3305 if (TREE_VALUE (arg) != void_type_node)
3307 /* Collect the sets of parameter packs used in each of the
3308 initialization arguments. */
3309 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3311 /* Determine which parameter packs will be expanded in this
3312 argument. */
3313 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3314 &ppd, ppd.visited);
3318 pointer_set_destroy (ppd.visited);
3320 /* Create the pack expansion type for the base type. */
3321 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3322 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3323 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3325 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3326 they will rarely be compared to anything. */
3327 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3329 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3332 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3333 for_types = true;
3335 /* Build the PACK_EXPANSION_* node. */
3336 result = for_types
3337 ? cxx_make_type (TYPE_PACK_EXPANSION)
3338 : make_node (EXPR_PACK_EXPANSION);
3339 SET_PACK_EXPANSION_PATTERN (result, arg);
3340 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3342 /* Propagate type and const-expression information. */
3343 TREE_TYPE (result) = TREE_TYPE (arg);
3344 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3346 else
3347 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3348 they will rarely be compared to anything. */
3349 SET_TYPE_STRUCTURAL_EQUALITY (result);
3351 /* Determine which parameter packs will be expanded. */
3352 ppd.parameter_packs = &parameter_packs;
3353 ppd.visited = pointer_set_create ();
3354 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3355 pointer_set_destroy (ppd.visited);
3357 /* Make sure we found some parameter packs. */
3358 if (parameter_packs == NULL_TREE)
3360 if (TYPE_P (arg))
3361 error ("expansion pattern %<%T%> contains no argument packs", arg);
3362 else
3363 error ("expansion pattern %<%E%> contains no argument packs", arg);
3364 return error_mark_node;
3366 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3368 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3370 return result;
3373 /* Checks T for any "bare" parameter packs, which have not yet been
3374 expanded, and issues an error if any are found. This operation can
3375 only be done on full expressions or types (e.g., an expression
3376 statement, "if" condition, etc.), because we could have expressions like:
3378 foo(f(g(h(args)))...)
3380 where "args" is a parameter pack. check_for_bare_parameter_packs
3381 should not be called for the subexpressions args, h(args),
3382 g(h(args)), or f(g(h(args))), because we would produce erroneous
3383 error messages.
3385 Returns TRUE and emits an error if there were bare parameter packs,
3386 returns FALSE otherwise. */
3387 bool
3388 check_for_bare_parameter_packs (tree t)
3390 tree parameter_packs = NULL_TREE;
3391 struct find_parameter_pack_data ppd;
3393 if (!processing_template_decl || !t || t == error_mark_node)
3394 return false;
3396 if (TREE_CODE (t) == TYPE_DECL)
3397 t = TREE_TYPE (t);
3399 ppd.parameter_packs = &parameter_packs;
3400 ppd.visited = pointer_set_create ();
3401 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3402 pointer_set_destroy (ppd.visited);
3404 if (parameter_packs)
3406 error ("parameter packs not expanded with %<...%>:");
3407 while (parameter_packs)
3409 tree pack = TREE_VALUE (parameter_packs);
3410 tree name = NULL_TREE;
3412 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3413 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3414 name = TYPE_NAME (pack);
3415 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3416 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3417 else
3418 name = DECL_NAME (pack);
3420 if (name)
3421 inform (input_location, " %qD", name);
3422 else
3423 inform (input_location, " <anonymous>");
3425 parameter_packs = TREE_CHAIN (parameter_packs);
3428 return true;
3431 return false;
3434 /* Expand any parameter packs that occur in the template arguments in
3435 ARGS. */
3436 tree
3437 expand_template_argument_pack (tree args)
3439 tree result_args = NULL_TREE;
3440 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3441 int num_result_args = -1;
3442 int non_default_args_count = -1;
3444 /* First, determine if we need to expand anything, and the number of
3445 slots we'll need. */
3446 for (in_arg = 0; in_arg < nargs; ++in_arg)
3448 tree arg = TREE_VEC_ELT (args, in_arg);
3449 if (arg == NULL_TREE)
3450 return args;
3451 if (ARGUMENT_PACK_P (arg))
3453 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3454 if (num_result_args < 0)
3455 num_result_args = in_arg + num_packed;
3456 else
3457 num_result_args += num_packed;
3459 else
3461 if (num_result_args >= 0)
3462 num_result_args++;
3466 /* If no expansion is necessary, we're done. */
3467 if (num_result_args < 0)
3468 return args;
3470 /* Expand arguments. */
3471 result_args = make_tree_vec (num_result_args);
3472 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3473 non_default_args_count =
3474 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3475 for (in_arg = 0; in_arg < nargs; ++in_arg)
3477 tree arg = TREE_VEC_ELT (args, in_arg);
3478 if (ARGUMENT_PACK_P (arg))
3480 tree packed = ARGUMENT_PACK_ARGS (arg);
3481 int i, num_packed = TREE_VEC_LENGTH (packed);
3482 for (i = 0; i < num_packed; ++i, ++out_arg)
3483 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3484 if (non_default_args_count > 0)
3485 non_default_args_count += num_packed - 1;
3487 else
3489 TREE_VEC_ELT (result_args, out_arg) = arg;
3490 ++out_arg;
3493 if (non_default_args_count >= 0)
3494 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3495 return result_args;
3498 /* Checks if DECL shadows a template parameter.
3500 [temp.local]: A template-parameter shall not be redeclared within its
3501 scope (including nested scopes).
3503 Emits an error and returns TRUE if the DECL shadows a parameter,
3504 returns FALSE otherwise. */
3506 bool
3507 check_template_shadow (tree decl)
3509 tree olddecl;
3511 /* If we're not in a template, we can't possibly shadow a template
3512 parameter. */
3513 if (!current_template_parms)
3514 return true;
3516 /* Figure out what we're shadowing. */
3517 if (TREE_CODE (decl) == OVERLOAD)
3518 decl = OVL_CURRENT (decl);
3519 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3521 /* If there's no previous binding for this name, we're not shadowing
3522 anything, let alone a template parameter. */
3523 if (!olddecl)
3524 return true;
3526 /* If we're not shadowing a template parameter, we're done. Note
3527 that OLDDECL might be an OVERLOAD (or perhaps even an
3528 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3529 node. */
3530 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3531 return true;
3533 /* We check for decl != olddecl to avoid bogus errors for using a
3534 name inside a class. We check TPFI to avoid duplicate errors for
3535 inline member templates. */
3536 if (decl == olddecl
3537 || (DECL_TEMPLATE_PARM_P (decl)
3538 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3539 return true;
3541 /* Don't complain about the injected class name, as we've already
3542 complained about the class itself. */
3543 if (DECL_SELF_REFERENCE_P (decl))
3544 return false;
3546 error ("declaration of %q+#D", decl);
3547 error (" shadows template parm %q+#D", olddecl);
3548 return false;
3551 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3552 ORIG_LEVEL, DECL, and TYPE. */
3554 static tree
3555 build_template_parm_index (int index,
3556 int level,
3557 int orig_level,
3558 tree decl,
3559 tree type)
3561 tree t = make_node (TEMPLATE_PARM_INDEX);
3562 TEMPLATE_PARM_IDX (t) = index;
3563 TEMPLATE_PARM_LEVEL (t) = level;
3564 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3565 TEMPLATE_PARM_DECL (t) = decl;
3566 TREE_TYPE (t) = type;
3567 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3568 TREE_READONLY (t) = TREE_READONLY (decl);
3570 return t;
3573 /* Find the canonical type parameter for the given template type
3574 parameter. Returns the canonical type parameter, which may be TYPE
3575 if no such parameter existed. */
3577 static tree
3578 canonical_type_parameter (tree type)
3580 tree list;
3581 int idx = TEMPLATE_TYPE_IDX (type);
3582 if (!canonical_template_parms)
3583 vec_alloc (canonical_template_parms, idx+1);
3585 while (canonical_template_parms->length () <= (unsigned)idx)
3586 vec_safe_push (canonical_template_parms, NULL_TREE);
3588 list = (*canonical_template_parms)[idx];
3589 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3590 list = TREE_CHAIN (list);
3592 if (list)
3593 return TREE_VALUE (list);
3594 else
3596 (*canonical_template_parms)[idx]
3597 = tree_cons (NULL_TREE, type,
3598 (*canonical_template_parms)[idx]);
3599 return type;
3603 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3604 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3605 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3606 new one is created. */
3608 static tree
3609 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3610 tsubst_flags_t complain)
3612 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3613 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3614 != TEMPLATE_PARM_LEVEL (index) - levels)
3615 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3617 tree orig_decl = TEMPLATE_PARM_DECL (index);
3618 tree decl, t;
3620 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3621 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3622 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3623 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3624 DECL_ARTIFICIAL (decl) = 1;
3625 SET_DECL_TEMPLATE_PARM_P (decl);
3627 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3628 TEMPLATE_PARM_LEVEL (index) - levels,
3629 TEMPLATE_PARM_ORIG_LEVEL (index),
3630 decl, type);
3631 TEMPLATE_PARM_DESCENDANTS (index) = t;
3632 TEMPLATE_PARM_PARAMETER_PACK (t)
3633 = TEMPLATE_PARM_PARAMETER_PACK (index);
3635 /* Template template parameters need this. */
3636 if (TREE_CODE (decl) == TEMPLATE_DECL)
3637 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3638 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3639 args, complain);
3642 return TEMPLATE_PARM_DESCENDANTS (index);
3645 /* Process information from new template parameter PARM and append it
3646 to the LIST being built. This new parameter is a non-type
3647 parameter iff IS_NON_TYPE is true. This new parameter is a
3648 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3649 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3650 parameter list PARM belongs to. This is used used to create a
3651 proper canonical type for the type of PARM that is to be created,
3652 iff PARM is a type. If the size is not known, this parameter shall
3653 be set to 0. */
3655 tree
3656 process_template_parm (tree list, location_t parm_loc, tree parm,
3657 bool is_non_type, bool is_parameter_pack)
3659 tree decl = 0;
3660 tree defval;
3661 tree err_parm_list;
3662 int idx = 0;
3664 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3665 defval = TREE_PURPOSE (parm);
3667 if (list)
3669 tree p = tree_last (list);
3671 if (p && TREE_VALUE (p) != error_mark_node)
3673 p = TREE_VALUE (p);
3674 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3675 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3676 else
3677 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3680 ++idx;
3682 else
3683 idx = 0;
3685 if (is_non_type)
3687 parm = TREE_VALUE (parm);
3689 SET_DECL_TEMPLATE_PARM_P (parm);
3691 if (TREE_TYPE (parm) == error_mark_node)
3693 err_parm_list = build_tree_list (defval, parm);
3694 TREE_VALUE (err_parm_list) = error_mark_node;
3695 return chainon (list, err_parm_list);
3697 else
3699 /* [temp.param]
3701 The top-level cv-qualifiers on the template-parameter are
3702 ignored when determining its type. */
3703 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3704 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3706 err_parm_list = build_tree_list (defval, parm);
3707 TREE_VALUE (err_parm_list) = error_mark_node;
3708 return chainon (list, err_parm_list);
3711 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack
3712 /* If we're in a nested template parameter list, the template
3713 template parameter could be a parameter pack. */
3714 && processing_template_parmlist == 1)
3716 /* This template parameter is not a parameter pack, but it
3717 should be. Complain about "bare" parameter packs. */
3718 check_for_bare_parameter_packs (TREE_TYPE (parm));
3720 /* Recover by calling this a parameter pack. */
3721 is_parameter_pack = true;
3725 /* A template parameter is not modifiable. */
3726 TREE_CONSTANT (parm) = 1;
3727 TREE_READONLY (parm) = 1;
3728 decl = build_decl (parm_loc,
3729 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3730 TREE_CONSTANT (decl) = 1;
3731 TREE_READONLY (decl) = 1;
3732 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3733 = build_template_parm_index (idx, processing_template_decl,
3734 processing_template_decl,
3735 decl, TREE_TYPE (parm));
3737 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3738 = is_parameter_pack;
3740 else
3742 tree t;
3743 parm = TREE_VALUE (TREE_VALUE (parm));
3745 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3747 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3748 /* This is for distinguishing between real templates and template
3749 template parameters */
3750 TREE_TYPE (parm) = t;
3751 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3752 decl = parm;
3754 else
3756 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3757 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3758 decl = build_decl (parm_loc,
3759 TYPE_DECL, parm, t);
3762 TYPE_NAME (t) = decl;
3763 TYPE_STUB_DECL (t) = decl;
3764 parm = decl;
3765 TEMPLATE_TYPE_PARM_INDEX (t)
3766 = build_template_parm_index (idx, processing_template_decl,
3767 processing_template_decl,
3768 decl, TREE_TYPE (parm));
3769 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3770 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3772 DECL_ARTIFICIAL (decl) = 1;
3773 SET_DECL_TEMPLATE_PARM_P (decl);
3774 pushdecl (decl);
3775 parm = build_tree_list (defval, parm);
3776 return chainon (list, parm);
3779 /* The end of a template parameter list has been reached. Process the
3780 tree list into a parameter vector, converting each parameter into a more
3781 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3782 as PARM_DECLs. */
3784 tree
3785 end_template_parm_list (tree parms)
3787 int nparms;
3788 tree parm, next;
3789 tree saved_parmlist = make_tree_vec (list_length (parms));
3791 current_template_parms
3792 = tree_cons (size_int (processing_template_decl),
3793 saved_parmlist, current_template_parms);
3795 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3797 next = TREE_CHAIN (parm);
3798 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3799 TREE_CHAIN (parm) = NULL_TREE;
3802 --processing_template_parmlist;
3804 return saved_parmlist;
3807 /* end_template_decl is called after a template declaration is seen. */
3809 void
3810 end_template_decl (void)
3812 reset_specialization ();
3814 if (! processing_template_decl)
3815 return;
3817 /* This matches the pushlevel in begin_template_parm_list. */
3818 finish_scope ();
3820 --processing_template_decl;
3821 current_template_parms = TREE_CHAIN (current_template_parms);
3824 /* Takes a TREE_LIST representing a template parameter and convert it
3825 into an argument suitable to be passed to the type substitution
3826 functions. Note that If the TREE_LIST contains an error_mark
3827 node, the returned argument is error_mark_node. */
3829 static tree
3830 template_parm_to_arg (tree t)
3833 if (t == NULL_TREE
3834 || TREE_CODE (t) != TREE_LIST)
3835 return t;
3837 if (error_operand_p (TREE_VALUE (t)))
3838 return error_mark_node;
3840 t = TREE_VALUE (t);
3842 if (TREE_CODE (t) == TYPE_DECL
3843 || TREE_CODE (t) == TEMPLATE_DECL)
3845 t = TREE_TYPE (t);
3847 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3849 /* Turn this argument into a TYPE_ARGUMENT_PACK
3850 with a single element, which expands T. */
3851 tree vec = make_tree_vec (1);
3852 #ifdef ENABLE_CHECKING
3853 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3854 (vec, TREE_VEC_LENGTH (vec));
3855 #endif
3856 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3858 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3859 SET_ARGUMENT_PACK_ARGS (t, vec);
3862 else
3864 t = DECL_INITIAL (t);
3866 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3868 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3869 with a single element, which expands T. */
3870 tree vec = make_tree_vec (1);
3871 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3872 #ifdef ENABLE_CHECKING
3873 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3874 (vec, TREE_VEC_LENGTH (vec));
3875 #endif
3876 t = convert_from_reference (t);
3877 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3879 t = make_node (NONTYPE_ARGUMENT_PACK);
3880 SET_ARGUMENT_PACK_ARGS (t, vec);
3881 TREE_TYPE (t) = type;
3883 else
3884 t = convert_from_reference (t);
3886 return t;
3889 /* Given a set of template parameters, return them as a set of template
3890 arguments. The template parameters are represented as a TREE_VEC, in
3891 the form documented in cp-tree.h for template arguments. */
3893 static tree
3894 template_parms_to_args (tree parms)
3896 tree header;
3897 tree args = NULL_TREE;
3898 int length = TMPL_PARMS_DEPTH (parms);
3899 int l = length;
3901 /* If there is only one level of template parameters, we do not
3902 create a TREE_VEC of TREE_VECs. Instead, we return a single
3903 TREE_VEC containing the arguments. */
3904 if (length > 1)
3905 args = make_tree_vec (length);
3907 for (header = parms; header; header = TREE_CHAIN (header))
3909 tree a = copy_node (TREE_VALUE (header));
3910 int i;
3912 TREE_TYPE (a) = NULL_TREE;
3913 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3914 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3916 #ifdef ENABLE_CHECKING
3917 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3918 #endif
3920 if (length > 1)
3921 TREE_VEC_ELT (args, --l) = a;
3922 else
3923 args = a;
3926 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3927 /* This can happen for template parms of a template template
3928 parameter, e.g:
3930 template<template<class T, class U> class TT> struct S;
3932 Consider the level of the parms of TT; T and U both have
3933 level 2; TT has no template parm of level 1. So in this case
3934 the first element of full_template_args is NULL_TREE. If we
3935 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3936 of 2. This will make tsubst wrongly consider that T and U
3937 have level 1. Instead, let's create a dummy vector as the
3938 first element of full_template_args so that TMPL_ARGS_DEPTH
3939 returns the correct depth for args. */
3940 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3941 return args;
3944 /* Within the declaration of a template, return the currently active
3945 template parameters as an argument TREE_VEC. */
3947 static tree
3948 current_template_args (void)
3950 return template_parms_to_args (current_template_parms);
3953 /* Update the declared TYPE by doing any lookups which were thought to be
3954 dependent, but are not now that we know the SCOPE of the declarator. */
3956 tree
3957 maybe_update_decl_type (tree orig_type, tree scope)
3959 tree type = orig_type;
3961 if (type == NULL_TREE)
3962 return type;
3964 if (TREE_CODE (orig_type) == TYPE_DECL)
3965 type = TREE_TYPE (type);
3967 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3968 && dependent_type_p (type)
3969 /* Don't bother building up the args in this case. */
3970 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3972 /* tsubst in the args corresponding to the template parameters,
3973 including auto if present. Most things will be unchanged, but
3974 make_typename_type and tsubst_qualified_id will resolve
3975 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3976 tree args = current_template_args ();
3977 tree auto_node = type_uses_auto (type);
3978 tree pushed;
3979 if (auto_node)
3981 tree auto_vec = make_tree_vec (1);
3982 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3983 args = add_to_template_args (args, auto_vec);
3985 pushed = push_scope (scope);
3986 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3987 if (pushed)
3988 pop_scope (scope);
3991 if (type == error_mark_node)
3992 return orig_type;
3994 if (TREE_CODE (orig_type) == TYPE_DECL)
3996 if (same_type_p (type, TREE_TYPE (orig_type)))
3997 type = orig_type;
3998 else
3999 type = TYPE_NAME (type);
4001 return type;
4004 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4005 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4006 a member template. Used by push_template_decl below. */
4008 static tree
4009 build_template_decl (tree decl, tree parms, bool member_template_p)
4011 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4012 DECL_TEMPLATE_PARMS (tmpl) = parms;
4013 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4014 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4015 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4017 return tmpl;
4020 struct template_parm_data
4022 /* The level of the template parameters we are currently
4023 processing. */
4024 int level;
4026 /* The index of the specialization argument we are currently
4027 processing. */
4028 int current_arg;
4030 /* An array whose size is the number of template parameters. The
4031 elements are nonzero if the parameter has been used in any one
4032 of the arguments processed so far. */
4033 int* parms;
4035 /* An array whose size is the number of template arguments. The
4036 elements are nonzero if the argument makes use of template
4037 parameters of this level. */
4038 int* arg_uses_template_parms;
4041 /* Subroutine of push_template_decl used to see if each template
4042 parameter in a partial specialization is used in the explicit
4043 argument list. If T is of the LEVEL given in DATA (which is
4044 treated as a template_parm_data*), then DATA->PARMS is marked
4045 appropriately. */
4047 static int
4048 mark_template_parm (tree t, void* data)
4050 int level;
4051 int idx;
4052 struct template_parm_data* tpd = (struct template_parm_data*) data;
4054 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4056 level = TEMPLATE_PARM_LEVEL (t);
4057 idx = TEMPLATE_PARM_IDX (t);
4059 else
4061 level = TEMPLATE_TYPE_LEVEL (t);
4062 idx = TEMPLATE_TYPE_IDX (t);
4065 if (level == tpd->level)
4067 tpd->parms[idx] = 1;
4068 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4071 /* Return zero so that for_each_template_parm will continue the
4072 traversal of the tree; we want to mark *every* template parm. */
4073 return 0;
4076 /* Process the partial specialization DECL. */
4078 static tree
4079 process_partial_specialization (tree decl)
4081 tree type = TREE_TYPE (decl);
4082 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4083 tree specargs = CLASSTYPE_TI_ARGS (type);
4084 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4085 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4086 tree inner_parms;
4087 tree inst;
4088 int nargs = TREE_VEC_LENGTH (inner_args);
4089 int ntparms;
4090 int i;
4091 bool did_error_intro = false;
4092 struct template_parm_data tpd;
4093 struct template_parm_data tpd2;
4095 gcc_assert (current_template_parms);
4097 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4098 ntparms = TREE_VEC_LENGTH (inner_parms);
4100 /* We check that each of the template parameters given in the
4101 partial specialization is used in the argument list to the
4102 specialization. For example:
4104 template <class T> struct S;
4105 template <class T> struct S<T*>;
4107 The second declaration is OK because `T*' uses the template
4108 parameter T, whereas
4110 template <class T> struct S<int>;
4112 is no good. Even trickier is:
4114 template <class T>
4115 struct S1
4117 template <class U>
4118 struct S2;
4119 template <class U>
4120 struct S2<T>;
4123 The S2<T> declaration is actually invalid; it is a
4124 full-specialization. Of course,
4126 template <class U>
4127 struct S2<T (*)(U)>;
4129 or some such would have been OK. */
4130 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4131 tpd.parms = XALLOCAVEC (int, ntparms);
4132 memset (tpd.parms, 0, sizeof (int) * ntparms);
4134 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4135 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4136 for (i = 0; i < nargs; ++i)
4138 tpd.current_arg = i;
4139 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4140 &mark_template_parm,
4141 &tpd,
4142 NULL,
4143 /*include_nondeduced_p=*/false);
4145 for (i = 0; i < ntparms; ++i)
4146 if (tpd.parms[i] == 0)
4148 /* One of the template parms was not used in the
4149 specialization. */
4150 if (!did_error_intro)
4152 error ("template parameters not used in partial specialization:");
4153 did_error_intro = true;
4156 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4159 if (did_error_intro)
4160 return error_mark_node;
4162 /* [temp.class.spec]
4164 The argument list of the specialization shall not be identical to
4165 the implicit argument list of the primary template. */
4166 if (comp_template_args
4167 (inner_args,
4168 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4169 (maintmpl)))))
4170 error ("partial specialization %qT does not specialize any template arguments", type);
4172 /* A partial specialization that replaces multiple parameters of the
4173 primary template with a pack expansion is less specialized for those
4174 parameters. */
4175 if (nargs < DECL_NTPARMS (maintmpl))
4177 error ("partial specialization is not more specialized than the "
4178 "primary template because it replaces multiple parameters "
4179 "with a pack expansion");
4180 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4181 return decl;
4184 /* [temp.class.spec]
4186 A partially specialized non-type argument expression shall not
4187 involve template parameters of the partial specialization except
4188 when the argument expression is a simple identifier.
4190 The type of a template parameter corresponding to a specialized
4191 non-type argument shall not be dependent on a parameter of the
4192 specialization.
4194 Also, we verify that pack expansions only occur at the
4195 end of the argument list. */
4196 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4197 tpd2.parms = 0;
4198 for (i = 0; i < nargs; ++i)
4200 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4201 tree arg = TREE_VEC_ELT (inner_args, i);
4202 tree packed_args = NULL_TREE;
4203 int j, len = 1;
4205 if (ARGUMENT_PACK_P (arg))
4207 /* Extract the arguments from the argument pack. We'll be
4208 iterating over these in the following loop. */
4209 packed_args = ARGUMENT_PACK_ARGS (arg);
4210 len = TREE_VEC_LENGTH (packed_args);
4213 for (j = 0; j < len; j++)
4215 if (packed_args)
4216 /* Get the Jth argument in the parameter pack. */
4217 arg = TREE_VEC_ELT (packed_args, j);
4219 if (PACK_EXPANSION_P (arg))
4221 /* Pack expansions must come at the end of the
4222 argument list. */
4223 if ((packed_args && j < len - 1)
4224 || (!packed_args && i < nargs - 1))
4226 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4227 error ("parameter pack argument %qE must be at the "
4228 "end of the template argument list", arg);
4229 else
4230 error ("parameter pack argument %qT must be at the "
4231 "end of the template argument list", arg);
4235 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4236 /* We only care about the pattern. */
4237 arg = PACK_EXPANSION_PATTERN (arg);
4239 if (/* These first two lines are the `non-type' bit. */
4240 !TYPE_P (arg)
4241 && TREE_CODE (arg) != TEMPLATE_DECL
4242 /* This next two lines are the `argument expression is not just a
4243 simple identifier' condition and also the `specialized
4244 non-type argument' bit. */
4245 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4246 && !(REFERENCE_REF_P (arg)
4247 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4249 if ((!packed_args && tpd.arg_uses_template_parms[i])
4250 || (packed_args && uses_template_parms (arg)))
4251 error ("template argument %qE involves template parameter(s)",
4252 arg);
4253 else
4255 /* Look at the corresponding template parameter,
4256 marking which template parameters its type depends
4257 upon. */
4258 tree type = TREE_TYPE (parm);
4260 if (!tpd2.parms)
4262 /* We haven't yet initialized TPD2. Do so now. */
4263 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4264 /* The number of parameters here is the number in the
4265 main template, which, as checked in the assertion
4266 above, is NARGS. */
4267 tpd2.parms = XALLOCAVEC (int, nargs);
4268 tpd2.level =
4269 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4272 /* Mark the template parameters. But this time, we're
4273 looking for the template parameters of the main
4274 template, not in the specialization. */
4275 tpd2.current_arg = i;
4276 tpd2.arg_uses_template_parms[i] = 0;
4277 memset (tpd2.parms, 0, sizeof (int) * nargs);
4278 for_each_template_parm (type,
4279 &mark_template_parm,
4280 &tpd2,
4281 NULL,
4282 /*include_nondeduced_p=*/false);
4284 if (tpd2.arg_uses_template_parms [i])
4286 /* The type depended on some template parameters.
4287 If they are fully specialized in the
4288 specialization, that's OK. */
4289 int j;
4290 int count = 0;
4291 for (j = 0; j < nargs; ++j)
4292 if (tpd2.parms[j] != 0
4293 && tpd.arg_uses_template_parms [j])
4294 ++count;
4295 if (count != 0)
4296 error_n (input_location, count,
4297 "type %qT of template argument %qE depends "
4298 "on a template parameter",
4299 "type %qT of template argument %qE depends "
4300 "on template parameters",
4301 type,
4302 arg);
4309 /* We should only get here once. */
4310 gcc_assert (!COMPLETE_TYPE_P (type));
4312 tree tmpl = build_template_decl (decl, current_template_parms,
4313 DECL_MEMBER_TEMPLATE_P (maintmpl));
4314 TREE_TYPE (tmpl) = type;
4315 DECL_TEMPLATE_RESULT (tmpl) = decl;
4316 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4317 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4318 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4320 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4321 = tree_cons (specargs, tmpl,
4322 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4323 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4325 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4326 inst = TREE_CHAIN (inst))
4328 tree inst_type = TREE_VALUE (inst);
4329 if (COMPLETE_TYPE_P (inst_type)
4330 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4332 tree spec = most_specialized_class (inst_type, tf_none);
4333 if (spec && TREE_TYPE (spec) == type)
4334 permerror (input_location,
4335 "partial specialization of %qT after instantiation "
4336 "of %qT", type, inst_type);
4340 return decl;
4343 /* PARM is a template parameter of some form; return the corresponding
4344 TEMPLATE_PARM_INDEX. */
4346 static tree
4347 get_template_parm_index (tree parm)
4349 if (TREE_CODE (parm) == PARM_DECL
4350 || TREE_CODE (parm) == CONST_DECL)
4351 parm = DECL_INITIAL (parm);
4352 else if (TREE_CODE (parm) == TYPE_DECL
4353 || TREE_CODE (parm) == TEMPLATE_DECL)
4354 parm = TREE_TYPE (parm);
4355 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4356 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4357 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4358 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4359 return parm;
4362 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4363 parameter packs used by the template parameter PARM. */
4365 static void
4366 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4368 /* A type parm can't refer to another parm. */
4369 if (TREE_CODE (parm) == TYPE_DECL)
4370 return;
4371 else if (TREE_CODE (parm) == PARM_DECL)
4373 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4374 ppd, ppd->visited);
4375 return;
4378 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4380 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4381 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4382 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4385 /* PARM is a template parameter pack. Return any parameter packs used in
4386 its type or the type of any of its template parameters. If there are
4387 any such packs, it will be instantiated into a fixed template parameter
4388 list by partial instantiation rather than be fully deduced. */
4390 tree
4391 fixed_parameter_pack_p (tree parm)
4393 /* This can only be true in a member template. */
4394 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4395 return NULL_TREE;
4396 /* This can only be true for a parameter pack. */
4397 if (!template_parameter_pack_p (parm))
4398 return NULL_TREE;
4399 /* A type parm can't refer to another parm. */
4400 if (TREE_CODE (parm) == TYPE_DECL)
4401 return NULL_TREE;
4403 tree parameter_packs = NULL_TREE;
4404 struct find_parameter_pack_data ppd;
4405 ppd.parameter_packs = &parameter_packs;
4406 ppd.visited = pointer_set_create ();
4408 fixed_parameter_pack_p_1 (parm, &ppd);
4410 pointer_set_destroy (ppd.visited);
4411 return parameter_packs;
4414 /* Check that a template declaration's use of default arguments and
4415 parameter packs is not invalid. Here, PARMS are the template
4416 parameters. IS_PRIMARY is true if DECL is the thing declared by
4417 a primary template. IS_PARTIAL is true if DECL is a partial
4418 specialization.
4420 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4421 declaration (but not a definition); 1 indicates a declaration, 2
4422 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4423 emitted for extraneous default arguments.
4425 Returns TRUE if there were no errors found, FALSE otherwise. */
4427 bool
4428 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4429 bool is_partial, int is_friend_decl)
4431 const char *msg;
4432 int last_level_to_check;
4433 tree parm_level;
4434 bool no_errors = true;
4436 /* [temp.param]
4438 A default template-argument shall not be specified in a
4439 function template declaration or a function template definition, nor
4440 in the template-parameter-list of the definition of a member of a
4441 class template. */
4443 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4444 /* You can't have a function template declaration in a local
4445 scope, nor you can you define a member of a class template in a
4446 local scope. */
4447 return true;
4449 if (TREE_CODE (decl) == TYPE_DECL
4450 && TREE_TYPE (decl)
4451 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4452 /* A lambda doesn't have an explicit declaration; don't complain
4453 about the parms of the enclosing class. */
4454 return true;
4456 if (current_class_type
4457 && !TYPE_BEING_DEFINED (current_class_type)
4458 && DECL_LANG_SPECIFIC (decl)
4459 && DECL_DECLARES_FUNCTION_P (decl)
4460 /* If this is either a friend defined in the scope of the class
4461 or a member function. */
4462 && (DECL_FUNCTION_MEMBER_P (decl)
4463 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4464 : DECL_FRIEND_CONTEXT (decl)
4465 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4466 : false)
4467 /* And, if it was a member function, it really was defined in
4468 the scope of the class. */
4469 && (!DECL_FUNCTION_MEMBER_P (decl)
4470 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4471 /* We already checked these parameters when the template was
4472 declared, so there's no need to do it again now. This function
4473 was defined in class scope, but we're processing its body now
4474 that the class is complete. */
4475 return true;
4477 /* Core issue 226 (C++0x only): the following only applies to class
4478 templates. */
4479 if (is_primary
4480 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4482 /* [temp.param]
4484 If a template-parameter has a default template-argument, all
4485 subsequent template-parameters shall have a default
4486 template-argument supplied. */
4487 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4489 tree inner_parms = TREE_VALUE (parm_level);
4490 int ntparms = TREE_VEC_LENGTH (inner_parms);
4491 int seen_def_arg_p = 0;
4492 int i;
4494 for (i = 0; i < ntparms; ++i)
4496 tree parm = TREE_VEC_ELT (inner_parms, i);
4498 if (parm == error_mark_node)
4499 continue;
4501 if (TREE_PURPOSE (parm))
4502 seen_def_arg_p = 1;
4503 else if (seen_def_arg_p
4504 && !template_parameter_pack_p (TREE_VALUE (parm)))
4506 error ("no default argument for %qD", TREE_VALUE (parm));
4507 /* For better subsequent error-recovery, we indicate that
4508 there should have been a default argument. */
4509 TREE_PURPOSE (parm) = error_mark_node;
4510 no_errors = false;
4512 else if (!is_partial
4513 && !is_friend_decl
4514 /* Don't complain about an enclosing partial
4515 specialization. */
4516 && parm_level == parms
4517 && TREE_CODE (decl) == TYPE_DECL
4518 && i < ntparms - 1
4519 && template_parameter_pack_p (TREE_VALUE (parm))
4520 /* A fixed parameter pack will be partially
4521 instantiated into a fixed length list. */
4522 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4524 /* A primary class template can only have one
4525 parameter pack, at the end of the template
4526 parameter list. */
4528 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4529 error ("parameter pack %qE must be at the end of the"
4530 " template parameter list", TREE_VALUE (parm));
4531 else
4532 error ("parameter pack %qT must be at the end of the"
4533 " template parameter list",
4534 TREE_TYPE (TREE_VALUE (parm)));
4536 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4537 = error_mark_node;
4538 no_errors = false;
4544 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4545 || is_partial
4546 || !is_primary
4547 || is_friend_decl)
4548 /* For an ordinary class template, default template arguments are
4549 allowed at the innermost level, e.g.:
4550 template <class T = int>
4551 struct S {};
4552 but, in a partial specialization, they're not allowed even
4553 there, as we have in [temp.class.spec]:
4555 The template parameter list of a specialization shall not
4556 contain default template argument values.
4558 So, for a partial specialization, or for a function template
4559 (in C++98/C++03), we look at all of them. */
4561 else
4562 /* But, for a primary class template that is not a partial
4563 specialization we look at all template parameters except the
4564 innermost ones. */
4565 parms = TREE_CHAIN (parms);
4567 /* Figure out what error message to issue. */
4568 if (is_friend_decl == 2)
4569 msg = G_("default template arguments may not be used in function template "
4570 "friend re-declaration");
4571 else if (is_friend_decl)
4572 msg = G_("default template arguments may not be used in function template "
4573 "friend declarations");
4574 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4575 msg = G_("default template arguments may not be used in function templates "
4576 "without -std=c++11 or -std=gnu++11");
4577 else if (is_partial)
4578 msg = G_("default template arguments may not be used in "
4579 "partial specializations");
4580 else
4581 msg = G_("default argument for template parameter for class enclosing %qD");
4583 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4584 /* If we're inside a class definition, there's no need to
4585 examine the parameters to the class itself. On the one
4586 hand, they will be checked when the class is defined, and,
4587 on the other, default arguments are valid in things like:
4588 template <class T = double>
4589 struct S { template <class U> void f(U); };
4590 Here the default argument for `S' has no bearing on the
4591 declaration of `f'. */
4592 last_level_to_check = template_class_depth (current_class_type) + 1;
4593 else
4594 /* Check everything. */
4595 last_level_to_check = 0;
4597 for (parm_level = parms;
4598 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4599 parm_level = TREE_CHAIN (parm_level))
4601 tree inner_parms = TREE_VALUE (parm_level);
4602 int i;
4603 int ntparms;
4605 ntparms = TREE_VEC_LENGTH (inner_parms);
4606 for (i = 0; i < ntparms; ++i)
4608 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4609 continue;
4611 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4613 if (msg)
4615 no_errors = false;
4616 if (is_friend_decl == 2)
4617 return no_errors;
4619 error (msg, decl);
4620 msg = 0;
4623 /* Clear out the default argument so that we are not
4624 confused later. */
4625 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4629 /* At this point, if we're still interested in issuing messages,
4630 they must apply to classes surrounding the object declared. */
4631 if (msg)
4632 msg = G_("default argument for template parameter for class "
4633 "enclosing %qD");
4636 return no_errors;
4639 /* Worker for push_template_decl_real, called via
4640 for_each_template_parm. DATA is really an int, indicating the
4641 level of the parameters we are interested in. If T is a template
4642 parameter of that level, return nonzero. */
4644 static int
4645 template_parm_this_level_p (tree t, void* data)
4647 int this_level = *(int *)data;
4648 int level;
4650 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4651 level = TEMPLATE_PARM_LEVEL (t);
4652 else
4653 level = TEMPLATE_TYPE_LEVEL (t);
4654 return level == this_level;
4657 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4658 parameters given by current_template_args, or reuses a
4659 previously existing one, if appropriate. Returns the DECL, or an
4660 equivalent one, if it is replaced via a call to duplicate_decls.
4662 If IS_FRIEND is true, DECL is a friend declaration. */
4664 tree
4665 push_template_decl_real (tree decl, bool is_friend)
4667 tree tmpl;
4668 tree args;
4669 tree info;
4670 tree ctx;
4671 bool is_primary;
4672 bool is_partial;
4673 int new_template_p = 0;
4674 /* True if the template is a member template, in the sense of
4675 [temp.mem]. */
4676 bool member_template_p = false;
4678 if (decl == error_mark_node || !current_template_parms)
4679 return error_mark_node;
4681 /* See if this is a partial specialization. */
4682 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4683 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4684 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4686 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4687 is_friend = true;
4689 if (is_friend)
4690 /* For a friend, we want the context of the friend function, not
4691 the type of which it is a friend. */
4692 ctx = CP_DECL_CONTEXT (decl);
4693 else if (CP_DECL_CONTEXT (decl)
4694 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4695 /* In the case of a virtual function, we want the class in which
4696 it is defined. */
4697 ctx = CP_DECL_CONTEXT (decl);
4698 else
4699 /* Otherwise, if we're currently defining some class, the DECL
4700 is assumed to be a member of the class. */
4701 ctx = current_scope ();
4703 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4704 ctx = NULL_TREE;
4706 if (!DECL_CONTEXT (decl))
4707 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4709 /* See if this is a primary template. */
4710 if (is_friend && ctx
4711 && uses_template_parms_level (ctx, processing_template_decl))
4712 /* A friend template that specifies a class context, i.e.
4713 template <typename T> friend void A<T>::f();
4714 is not primary. */
4715 is_primary = false;
4716 else
4717 is_primary = template_parm_scope_p ();
4719 if (is_primary)
4721 if (DECL_CLASS_SCOPE_P (decl))
4722 member_template_p = true;
4723 if (TREE_CODE (decl) == TYPE_DECL
4724 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4726 error ("template class without a name");
4727 return error_mark_node;
4729 else if (TREE_CODE (decl) == FUNCTION_DECL)
4731 if (DECL_DESTRUCTOR_P (decl))
4733 /* [temp.mem]
4735 A destructor shall not be a member template. */
4736 error ("destructor %qD declared as member template", decl);
4737 return error_mark_node;
4739 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4740 && (!prototype_p (TREE_TYPE (decl))
4741 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4742 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4743 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4744 == void_list_node)))
4746 /* [basic.stc.dynamic.allocation]
4748 An allocation function can be a function
4749 template. ... Template allocation functions shall
4750 have two or more parameters. */
4751 error ("invalid template declaration of %qD", decl);
4752 return error_mark_node;
4755 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4756 && CLASS_TYPE_P (TREE_TYPE (decl)))
4757 /* OK */;
4758 else if (TREE_CODE (decl) == TYPE_DECL
4759 && TYPE_DECL_ALIAS_P (decl))
4760 /* alias-declaration */
4761 gcc_assert (!DECL_ARTIFICIAL (decl));
4762 else
4764 error ("template declaration of %q#D", decl);
4765 return error_mark_node;
4769 /* Check to see that the rules regarding the use of default
4770 arguments are not being violated. */
4771 check_default_tmpl_args (decl, current_template_parms,
4772 is_primary, is_partial, /*is_friend_decl=*/0);
4774 /* Ensure that there are no parameter packs in the type of this
4775 declaration that have not been expanded. */
4776 if (TREE_CODE (decl) == FUNCTION_DECL)
4778 /* Check each of the arguments individually to see if there are
4779 any bare parameter packs. */
4780 tree type = TREE_TYPE (decl);
4781 tree arg = DECL_ARGUMENTS (decl);
4782 tree argtype = TYPE_ARG_TYPES (type);
4784 while (arg && argtype)
4786 if (!DECL_PACK_P (arg)
4787 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4789 /* This is a PARM_DECL that contains unexpanded parameter
4790 packs. We have already complained about this in the
4791 check_for_bare_parameter_packs call, so just replace
4792 these types with ERROR_MARK_NODE. */
4793 TREE_TYPE (arg) = error_mark_node;
4794 TREE_VALUE (argtype) = error_mark_node;
4797 arg = DECL_CHAIN (arg);
4798 argtype = TREE_CHAIN (argtype);
4801 /* Check for bare parameter packs in the return type and the
4802 exception specifiers. */
4803 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4804 /* Errors were already issued, set return type to int
4805 as the frontend doesn't expect error_mark_node as
4806 the return type. */
4807 TREE_TYPE (type) = integer_type_node;
4808 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4809 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4811 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4812 && TYPE_DECL_ALIAS_P (decl))
4813 ? DECL_ORIGINAL_TYPE (decl)
4814 : TREE_TYPE (decl)))
4816 TREE_TYPE (decl) = error_mark_node;
4817 return error_mark_node;
4820 if (is_partial)
4821 return process_partial_specialization (decl);
4823 args = current_template_args ();
4825 if (!ctx
4826 || TREE_CODE (ctx) == FUNCTION_DECL
4827 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4828 || (TREE_CODE (decl) == TYPE_DECL
4829 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4830 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4832 if (DECL_LANG_SPECIFIC (decl)
4833 && DECL_TEMPLATE_INFO (decl)
4834 && DECL_TI_TEMPLATE (decl))
4835 tmpl = DECL_TI_TEMPLATE (decl);
4836 /* If DECL is a TYPE_DECL for a class-template, then there won't
4837 be DECL_LANG_SPECIFIC. The information equivalent to
4838 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4839 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4840 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4841 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4843 /* Since a template declaration already existed for this
4844 class-type, we must be redeclaring it here. Make sure
4845 that the redeclaration is valid. */
4846 redeclare_class_template (TREE_TYPE (decl),
4847 current_template_parms);
4848 /* We don't need to create a new TEMPLATE_DECL; just use the
4849 one we already had. */
4850 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4852 else
4854 tmpl = build_template_decl (decl, current_template_parms,
4855 member_template_p);
4856 new_template_p = 1;
4858 if (DECL_LANG_SPECIFIC (decl)
4859 && DECL_TEMPLATE_SPECIALIZATION (decl))
4861 /* A specialization of a member template of a template
4862 class. */
4863 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4864 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4865 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4869 else
4871 tree a, t, current, parms;
4872 int i;
4873 tree tinfo = get_template_info (decl);
4875 if (!tinfo)
4877 error ("template definition of non-template %q#D", decl);
4878 return error_mark_node;
4881 tmpl = TI_TEMPLATE (tinfo);
4883 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4884 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4885 && DECL_TEMPLATE_SPECIALIZATION (decl)
4886 && DECL_MEMBER_TEMPLATE_P (tmpl))
4888 tree new_tmpl;
4890 /* The declaration is a specialization of a member
4891 template, declared outside the class. Therefore, the
4892 innermost template arguments will be NULL, so we
4893 replace them with the arguments determined by the
4894 earlier call to check_explicit_specialization. */
4895 args = DECL_TI_ARGS (decl);
4897 new_tmpl
4898 = build_template_decl (decl, current_template_parms,
4899 member_template_p);
4900 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4901 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4902 DECL_TI_TEMPLATE (decl) = new_tmpl;
4903 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4904 DECL_TEMPLATE_INFO (new_tmpl)
4905 = build_template_info (tmpl, args);
4907 register_specialization (new_tmpl,
4908 most_general_template (tmpl),
4909 args,
4910 is_friend, 0);
4911 return decl;
4914 /* Make sure the template headers we got make sense. */
4916 parms = DECL_TEMPLATE_PARMS (tmpl);
4917 i = TMPL_PARMS_DEPTH (parms);
4918 if (TMPL_ARGS_DEPTH (args) != i)
4920 error ("expected %d levels of template parms for %q#D, got %d",
4921 i, decl, TMPL_ARGS_DEPTH (args));
4922 DECL_INTERFACE_KNOWN (decl) = 1;
4923 return error_mark_node;
4925 else
4926 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4928 a = TMPL_ARGS_LEVEL (args, i);
4929 t = INNERMOST_TEMPLATE_PARMS (parms);
4931 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4933 if (current == decl)
4934 error ("got %d template parameters for %q#D",
4935 TREE_VEC_LENGTH (a), decl);
4936 else
4937 error ("got %d template parameters for %q#T",
4938 TREE_VEC_LENGTH (a), current);
4939 error (" but %d required", TREE_VEC_LENGTH (t));
4940 /* Avoid crash in import_export_decl. */
4941 DECL_INTERFACE_KNOWN (decl) = 1;
4942 return error_mark_node;
4945 if (current == decl)
4946 current = ctx;
4947 else if (current == NULL_TREE)
4948 /* Can happen in erroneous input. */
4949 break;
4950 else
4951 current = get_containing_scope (current);
4954 /* Check that the parms are used in the appropriate qualifying scopes
4955 in the declarator. */
4956 if (!comp_template_args
4957 (TI_ARGS (tinfo),
4958 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4960 error ("\
4961 template arguments to %qD do not match original template %qD",
4962 decl, DECL_TEMPLATE_RESULT (tmpl));
4963 if (!uses_template_parms (TI_ARGS (tinfo)))
4964 inform (input_location, "use template<> for an explicit specialization");
4965 /* Avoid crash in import_export_decl. */
4966 DECL_INTERFACE_KNOWN (decl) = 1;
4967 return error_mark_node;
4971 DECL_TEMPLATE_RESULT (tmpl) = decl;
4972 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4974 /* Push template declarations for global functions and types. Note
4975 that we do not try to push a global template friend declared in a
4976 template class; such a thing may well depend on the template
4977 parameters of the class. */
4978 if (new_template_p && !ctx
4979 && !(is_friend && template_class_depth (current_class_type) > 0))
4981 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4982 if (tmpl == error_mark_node)
4983 return error_mark_node;
4985 /* Hide template friend classes that haven't been declared yet. */
4986 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4988 DECL_ANTICIPATED (tmpl) = 1;
4989 DECL_FRIEND_P (tmpl) = 1;
4993 if (is_primary)
4995 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4996 int i;
4998 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4999 if (DECL_CONV_FN_P (tmpl))
5001 int depth = TMPL_PARMS_DEPTH (parms);
5003 /* It is a conversion operator. See if the type converted to
5004 depends on innermost template operands. */
5006 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5007 depth))
5008 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5011 /* Give template template parms a DECL_CONTEXT of the template
5012 for which they are a parameter. */
5013 parms = INNERMOST_TEMPLATE_PARMS (parms);
5014 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5016 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5017 if (TREE_CODE (parm) == TEMPLATE_DECL)
5018 DECL_CONTEXT (parm) = tmpl;
5022 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5023 back to its most general template. If TMPL is a specialization,
5024 ARGS may only have the innermost set of arguments. Add the missing
5025 argument levels if necessary. */
5026 if (DECL_TEMPLATE_INFO (tmpl))
5027 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5029 info = build_template_info (tmpl, args);
5031 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5032 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5033 else
5035 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5036 retrofit_lang_decl (decl);
5037 if (DECL_LANG_SPECIFIC (decl))
5038 DECL_TEMPLATE_INFO (decl) = info;
5041 return DECL_TEMPLATE_RESULT (tmpl);
5044 tree
5045 push_template_decl (tree decl)
5047 return push_template_decl_real (decl, false);
5050 /* FN is an inheriting constructor that inherits from the constructor
5051 template INHERITED; turn FN into a constructor template with a matching
5052 template header. */
5054 tree
5055 add_inherited_template_parms (tree fn, tree inherited)
5057 tree inner_parms
5058 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5059 inner_parms = copy_node (inner_parms);
5060 tree parms
5061 = tree_cons (size_int (processing_template_decl + 1),
5062 inner_parms, current_template_parms);
5063 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5064 tree args = template_parms_to_args (parms);
5065 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5066 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5067 DECL_TEMPLATE_RESULT (tmpl) = fn;
5068 DECL_ARTIFICIAL (tmpl) = true;
5069 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5070 return tmpl;
5073 /* Called when a class template TYPE is redeclared with the indicated
5074 template PARMS, e.g.:
5076 template <class T> struct S;
5077 template <class T> struct S {}; */
5079 bool
5080 redeclare_class_template (tree type, tree parms)
5082 tree tmpl;
5083 tree tmpl_parms;
5084 int i;
5086 if (!TYPE_TEMPLATE_INFO (type))
5088 error ("%qT is not a template type", type);
5089 return false;
5092 tmpl = TYPE_TI_TEMPLATE (type);
5093 if (!PRIMARY_TEMPLATE_P (tmpl))
5094 /* The type is nested in some template class. Nothing to worry
5095 about here; there are no new template parameters for the nested
5096 type. */
5097 return true;
5099 if (!parms)
5101 error ("template specifiers not specified in declaration of %qD",
5102 tmpl);
5103 return false;
5106 parms = INNERMOST_TEMPLATE_PARMS (parms);
5107 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5109 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5111 error_n (input_location, TREE_VEC_LENGTH (parms),
5112 "redeclared with %d template parameter",
5113 "redeclared with %d template parameters",
5114 TREE_VEC_LENGTH (parms));
5115 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5116 "previous declaration %q+D used %d template parameter",
5117 "previous declaration %q+D used %d template parameters",
5118 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5119 return false;
5122 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5124 tree tmpl_parm;
5125 tree parm;
5126 tree tmpl_default;
5127 tree parm_default;
5129 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5130 || TREE_VEC_ELT (parms, i) == error_mark_node)
5131 continue;
5133 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5134 if (tmpl_parm == error_mark_node)
5135 return false;
5137 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5138 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5139 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5141 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5142 TEMPLATE_DECL. */
5143 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5144 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5145 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5146 || (TREE_CODE (tmpl_parm) != PARM_DECL
5147 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5148 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5149 || (TREE_CODE (tmpl_parm) == PARM_DECL
5150 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5151 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5153 error ("template parameter %q+#D", tmpl_parm);
5154 error ("redeclared here as %q#D", parm);
5155 return false;
5158 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5160 /* We have in [temp.param]:
5162 A template-parameter may not be given default arguments
5163 by two different declarations in the same scope. */
5164 error_at (input_location, "redefinition of default argument for %q#D", parm);
5165 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5166 "original definition appeared here");
5167 return false;
5170 if (parm_default != NULL_TREE)
5171 /* Update the previous template parameters (which are the ones
5172 that will really count) with the new default value. */
5173 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5174 else if (tmpl_default != NULL_TREE)
5175 /* Update the new parameters, too; they'll be used as the
5176 parameters for any members. */
5177 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5180 return true;
5183 /* Simplify EXPR if it is a non-dependent expression. Returns the
5184 (possibly simplified) expression. */
5186 tree
5187 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5189 if (expr == NULL_TREE)
5190 return NULL_TREE;
5192 /* If we're in a template, but EXPR isn't value dependent, simplify
5193 it. We're supposed to treat:
5195 template <typename T> void f(T[1 + 1]);
5196 template <typename T> void f(T[2]);
5198 as two declarations of the same function, for example. */
5199 if (processing_template_decl
5200 && !instantiation_dependent_expression_p (expr)
5201 && potential_constant_expression (expr))
5203 HOST_WIDE_INT saved_processing_template_decl;
5205 saved_processing_template_decl = processing_template_decl;
5206 processing_template_decl = 0;
5207 expr = tsubst_copy_and_build (expr,
5208 /*args=*/NULL_TREE,
5209 complain,
5210 /*in_decl=*/NULL_TREE,
5211 /*function_p=*/false,
5212 /*integral_constant_expression_p=*/true);
5213 processing_template_decl = saved_processing_template_decl;
5215 return expr;
5218 tree
5219 fold_non_dependent_expr (tree expr)
5221 return fold_non_dependent_expr_sfinae (expr, tf_error);
5224 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5225 template declaration, or a TYPE_DECL for an alias declaration. */
5227 bool
5228 alias_type_or_template_p (tree t)
5230 if (t == NULL_TREE)
5231 return false;
5232 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5233 || (TYPE_P (t)
5234 && TYPE_NAME (t)
5235 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5236 || DECL_ALIAS_TEMPLATE_P (t));
5239 /* Return TRUE iff is a specialization of an alias template. */
5241 bool
5242 alias_template_specialization_p (const_tree t)
5244 if (t == NULL_TREE)
5245 return false;
5247 return (TYPE_P (t)
5248 && TYPE_TEMPLATE_INFO (t)
5249 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5250 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5253 /* Return the number of innermost template parameters in TMPL. */
5255 static int
5256 num_innermost_template_parms (tree tmpl)
5258 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5259 return TREE_VEC_LENGTH (parms);
5262 /* Return either TMPL or another template that it is equivalent to under DR
5263 1286: An alias that just changes the name of a template is equivalent to
5264 the other template. */
5266 static tree
5267 get_underlying_template (tree tmpl)
5269 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5270 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5272 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5273 if (TYPE_TEMPLATE_INFO (result))
5275 tree sub = TYPE_TI_TEMPLATE (result);
5276 if (PRIMARY_TEMPLATE_P (sub)
5277 && (num_innermost_template_parms (tmpl)
5278 == num_innermost_template_parms (sub)))
5280 tree alias_args = INNERMOST_TEMPLATE_ARGS
5281 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5282 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5283 break;
5284 /* The alias type is equivalent to the pattern of the
5285 underlying template, so strip the alias. */
5286 tmpl = sub;
5287 continue;
5290 break;
5292 return tmpl;
5295 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5296 must be a function or a pointer-to-function type, as specified
5297 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5298 and check that the resulting function has external linkage. */
5300 static tree
5301 convert_nontype_argument_function (tree type, tree expr)
5303 tree fns = expr;
5304 tree fn, fn_no_ptr;
5305 linkage_kind linkage;
5307 fn = instantiate_type (type, fns, tf_none);
5308 if (fn == error_mark_node)
5309 return error_mark_node;
5311 fn_no_ptr = fn;
5312 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5313 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5314 if (BASELINK_P (fn_no_ptr))
5315 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5317 /* [temp.arg.nontype]/1
5319 A template-argument for a non-type, non-template template-parameter
5320 shall be one of:
5321 [...]
5322 -- the address of an object or function with external [C++11: or
5323 internal] linkage. */
5325 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5327 error ("%qE is not a valid template argument for type %qT", expr, type);
5328 if (TYPE_PTR_P (type))
5329 error ("it must be the address of a function with external linkage");
5330 else
5331 error ("it must be the name of a function with external linkage");
5332 return NULL_TREE;
5335 linkage = decl_linkage (fn_no_ptr);
5336 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5338 if (cxx_dialect >= cxx11)
5339 error ("%qE is not a valid template argument for type %qT "
5340 "because %qD has no linkage",
5341 expr, type, fn_no_ptr);
5342 else
5343 error ("%qE is not a valid template argument for type %qT "
5344 "because %qD does not have external linkage",
5345 expr, type, fn_no_ptr);
5346 return NULL_TREE;
5349 return fn;
5352 /* Subroutine of convert_nontype_argument.
5353 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5354 Emit an error otherwise. */
5356 static bool
5357 check_valid_ptrmem_cst_expr (tree type, tree expr,
5358 tsubst_flags_t complain)
5360 STRIP_NOPS (expr);
5361 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5362 return true;
5363 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5364 return true;
5365 if (complain & tf_error)
5367 error ("%qE is not a valid template argument for type %qT",
5368 expr, type);
5369 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5371 return false;
5374 /* Returns TRUE iff the address of OP is value-dependent.
5376 14.6.2.4 [temp.dep.temp]:
5377 A non-integral non-type template-argument is dependent if its type is
5378 dependent or it has either of the following forms
5379 qualified-id
5380 & qualified-id
5381 and contains a nested-name-specifier which specifies a class-name that
5382 names a dependent type.
5384 We generalize this to just say that the address of a member of a
5385 dependent class is value-dependent; the above doesn't cover the
5386 address of a static data member named with an unqualified-id. */
5388 static bool
5389 has_value_dependent_address (tree op)
5391 /* We could use get_inner_reference here, but there's no need;
5392 this is only relevant for template non-type arguments, which
5393 can only be expressed as &id-expression. */
5394 if (DECL_P (op))
5396 tree ctx = CP_DECL_CONTEXT (op);
5397 if (TYPE_P (ctx) && dependent_type_p (ctx))
5398 return true;
5401 return false;
5404 /* The next set of functions are used for providing helpful explanatory
5405 diagnostics for failed overload resolution. Their messages should be
5406 indented by two spaces for consistency with the messages in
5407 call.c */
5409 static int
5410 unify_success (bool /*explain_p*/)
5412 return 0;
5415 static int
5416 unify_parameter_deduction_failure (bool explain_p, tree parm)
5418 if (explain_p)
5419 inform (input_location,
5420 " couldn't deduce template parameter %qD", parm);
5421 return 1;
5424 static int
5425 unify_invalid (bool /*explain_p*/)
5427 return 1;
5430 static int
5431 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5433 if (explain_p)
5434 inform (input_location,
5435 " types %qT and %qT have incompatible cv-qualifiers",
5436 parm, arg);
5437 return 1;
5440 static int
5441 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5443 if (explain_p)
5444 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5445 return 1;
5448 static int
5449 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5451 if (explain_p)
5452 inform (input_location,
5453 " template parameter %qD is not a parameter pack, but "
5454 "argument %qD is",
5455 parm, arg);
5456 return 1;
5459 static int
5460 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5462 if (explain_p)
5463 inform (input_location,
5464 " template argument %qE does not match "
5465 "pointer-to-member constant %qE",
5466 arg, parm);
5467 return 1;
5470 static int
5471 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5473 if (explain_p)
5474 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5475 return 1;
5478 static int
5479 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5481 if (explain_p)
5482 inform (input_location,
5483 " inconsistent parameter pack deduction with %qT and %qT",
5484 old_arg, new_arg);
5485 return 1;
5488 static int
5489 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5491 if (explain_p)
5493 if (TYPE_P (parm))
5494 inform (input_location,
5495 " deduced conflicting types for parameter %qT (%qT and %qT)",
5496 parm, first, second);
5497 else
5498 inform (input_location,
5499 " deduced conflicting values for non-type parameter "
5500 "%qE (%qE and %qE)", parm, first, second);
5502 return 1;
5505 static int
5506 unify_vla_arg (bool explain_p, tree arg)
5508 if (explain_p)
5509 inform (input_location,
5510 " variable-sized array type %qT is not "
5511 "a valid template argument",
5512 arg);
5513 return 1;
5516 static int
5517 unify_method_type_error (bool explain_p, tree arg)
5519 if (explain_p)
5520 inform (input_location,
5521 " member function type %qT is not a valid template argument",
5522 arg);
5523 return 1;
5526 static int
5527 unify_arity (bool explain_p, int have, int wanted)
5529 if (explain_p)
5530 inform_n (input_location, wanted,
5531 " candidate expects %d argument, %d provided",
5532 " candidate expects %d arguments, %d provided",
5533 wanted, have);
5534 return 1;
5537 static int
5538 unify_too_many_arguments (bool explain_p, int have, int wanted)
5540 return unify_arity (explain_p, have, wanted);
5543 static int
5544 unify_too_few_arguments (bool explain_p, int have, int wanted)
5546 return unify_arity (explain_p, have, wanted);
5549 static int
5550 unify_arg_conversion (bool explain_p, tree to_type,
5551 tree from_type, tree arg)
5553 if (explain_p)
5554 inform (EXPR_LOC_OR_LOC (arg, input_location),
5555 " cannot convert %qE (type %qT) to type %qT",
5556 arg, from_type, to_type);
5557 return 1;
5560 static int
5561 unify_no_common_base (bool explain_p, enum template_base_result r,
5562 tree parm, tree arg)
5564 if (explain_p)
5565 switch (r)
5567 case tbr_ambiguous_baseclass:
5568 inform (input_location, " %qT is an ambiguous base class of %qT",
5569 parm, arg);
5570 break;
5571 default:
5572 inform (input_location, " %qT is not derived from %qT", arg, parm);
5573 break;
5575 return 1;
5578 static int
5579 unify_inconsistent_template_template_parameters (bool explain_p)
5581 if (explain_p)
5582 inform (input_location,
5583 " template parameters of a template template argument are "
5584 "inconsistent with other deduced template arguments");
5585 return 1;
5588 static int
5589 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5591 if (explain_p)
5592 inform (input_location,
5593 " can't deduce a template for %qT from non-template type %qT",
5594 parm, arg);
5595 return 1;
5598 static int
5599 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5601 if (explain_p)
5602 inform (input_location,
5603 " template argument %qE does not match %qD", arg, parm);
5604 return 1;
5607 static int
5608 unify_overload_resolution_failure (bool explain_p, tree arg)
5610 if (explain_p)
5611 inform (input_location,
5612 " could not resolve address from overloaded function %qE",
5613 arg);
5614 return 1;
5617 /* Attempt to convert the non-type template parameter EXPR to the
5618 indicated TYPE. If the conversion is successful, return the
5619 converted value. If the conversion is unsuccessful, return
5620 NULL_TREE if we issued an error message, or error_mark_node if we
5621 did not. We issue error messages for out-and-out bad template
5622 parameters, but not simply because the conversion failed, since we
5623 might be just trying to do argument deduction. Both TYPE and EXPR
5624 must be non-dependent.
5626 The conversion follows the special rules described in
5627 [temp.arg.nontype], and it is much more strict than an implicit
5628 conversion.
5630 This function is called twice for each template argument (see
5631 lookup_template_class for a more accurate description of this
5632 problem). This means that we need to handle expressions which
5633 are not valid in a C++ source, but can be created from the
5634 first call (for instance, casts to perform conversions). These
5635 hacks can go away after we fix the double coercion problem. */
5637 static tree
5638 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5640 tree expr_type;
5642 /* Detect immediately string literals as invalid non-type argument.
5643 This special-case is not needed for correctness (we would easily
5644 catch this later), but only to provide better diagnostic for this
5645 common user mistake. As suggested by DR 100, we do not mention
5646 linkage issues in the diagnostic as this is not the point. */
5647 /* FIXME we're making this OK. */
5648 if (TREE_CODE (expr) == STRING_CST)
5650 if (complain & tf_error)
5651 error ("%qE is not a valid template argument for type %qT "
5652 "because string literals can never be used in this context",
5653 expr, type);
5654 return NULL_TREE;
5657 /* Add the ADDR_EXPR now for the benefit of
5658 value_dependent_expression_p. */
5659 if (TYPE_PTROBV_P (type)
5660 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5662 expr = decay_conversion (expr, complain);
5663 if (expr == error_mark_node)
5664 return error_mark_node;
5667 /* If we are in a template, EXPR may be non-dependent, but still
5668 have a syntactic, rather than semantic, form. For example, EXPR
5669 might be a SCOPE_REF, rather than the VAR_DECL to which the
5670 SCOPE_REF refers. Preserving the qualifying scope is necessary
5671 so that access checking can be performed when the template is
5672 instantiated -- but here we need the resolved form so that we can
5673 convert the argument. */
5674 if (TYPE_REF_OBJ_P (type)
5675 && has_value_dependent_address (expr))
5676 /* If we want the address and it's value-dependent, don't fold. */;
5677 else if (!type_unknown_p (expr))
5678 expr = fold_non_dependent_expr_sfinae (expr, complain);
5679 if (error_operand_p (expr))
5680 return error_mark_node;
5681 expr_type = TREE_TYPE (expr);
5682 if (TREE_CODE (type) == REFERENCE_TYPE)
5683 expr = mark_lvalue_use (expr);
5684 else
5685 expr = mark_rvalue_use (expr);
5687 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5688 to a non-type argument of "nullptr". */
5689 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5690 expr = convert (type, expr);
5692 /* In C++11, integral or enumeration non-type template arguments can be
5693 arbitrary constant expressions. Pointer and pointer to
5694 member arguments can be general constant expressions that evaluate
5695 to a null value, but otherwise still need to be of a specific form. */
5696 if (cxx_dialect >= cxx11)
5698 if (TREE_CODE (expr) == PTRMEM_CST)
5699 /* A PTRMEM_CST is already constant, and a valid template
5700 argument for a parameter of pointer to member type, we just want
5701 to leave it in that form rather than lower it to a
5702 CONSTRUCTOR. */;
5703 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5704 expr = maybe_constant_value (expr);
5705 else if (TYPE_PTR_OR_PTRMEM_P (type))
5707 tree folded = maybe_constant_value (expr);
5708 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5709 : null_member_pointer_value_p (folded))
5710 expr = folded;
5714 /* HACK: Due to double coercion, we can get a
5715 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5716 which is the tree that we built on the first call (see
5717 below when coercing to reference to object or to reference to
5718 function). We just strip everything and get to the arg.
5719 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5720 for examples. */
5721 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5723 tree probe_type, probe = expr;
5724 if (REFERENCE_REF_P (probe))
5725 probe = TREE_OPERAND (probe, 0);
5726 probe_type = TREE_TYPE (probe);
5727 if (TREE_CODE (probe) == NOP_EXPR)
5729 /* ??? Maybe we could use convert_from_reference here, but we
5730 would need to relax its constraints because the NOP_EXPR
5731 could actually change the type to something more cv-qualified,
5732 and this is not folded by convert_from_reference. */
5733 tree addr = TREE_OPERAND (probe, 0);
5734 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5735 && TREE_CODE (addr) == ADDR_EXPR
5736 && TYPE_PTR_P (TREE_TYPE (addr))
5737 && (same_type_ignoring_top_level_qualifiers_p
5738 (TREE_TYPE (probe_type),
5739 TREE_TYPE (TREE_TYPE (addr)))))
5741 expr = TREE_OPERAND (addr, 0);
5742 expr_type = TREE_TYPE (probe_type);
5747 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5748 parameter is a pointer to object, through decay and
5749 qualification conversion. Let's strip everything. */
5750 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5752 tree probe = expr;
5753 STRIP_NOPS (probe);
5754 if (TREE_CODE (probe) == ADDR_EXPR
5755 && TYPE_PTR_P (TREE_TYPE (probe)))
5757 /* Skip the ADDR_EXPR only if it is part of the decay for
5758 an array. Otherwise, it is part of the original argument
5759 in the source code. */
5760 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5761 probe = TREE_OPERAND (probe, 0);
5762 expr = probe;
5763 expr_type = TREE_TYPE (expr);
5767 /* [temp.arg.nontype]/5, bullet 1
5769 For a non-type template-parameter of integral or enumeration type,
5770 integral promotions (_conv.prom_) and integral conversions
5771 (_conv.integral_) are applied. */
5772 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5774 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5775 t = maybe_constant_value (t);
5776 if (t != error_mark_node)
5777 expr = t;
5779 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5780 return error_mark_node;
5782 /* Notice that there are constant expressions like '4 % 0' which
5783 do not fold into integer constants. */
5784 if (TREE_CODE (expr) != INTEGER_CST)
5786 if (complain & tf_error)
5788 int errs = errorcount, warns = warningcount + werrorcount;
5789 if (processing_template_decl
5790 && !require_potential_constant_expression (expr))
5791 return NULL_TREE;
5792 expr = cxx_constant_value (expr);
5793 if (errorcount > errs || warningcount + werrorcount > warns)
5794 inform (EXPR_LOC_OR_LOC (expr, input_location),
5795 "in template argument for type %qT ", type);
5796 if (expr == error_mark_node)
5797 return NULL_TREE;
5798 /* else cxx_constant_value complained but gave us
5799 a real constant, so go ahead. */
5800 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5802 else
5803 return NULL_TREE;
5806 /* Avoid typedef problems. */
5807 if (TREE_TYPE (expr) != type)
5808 expr = fold_convert (type, expr);
5810 /* [temp.arg.nontype]/5, bullet 2
5812 For a non-type template-parameter of type pointer to object,
5813 qualification conversions (_conv.qual_) and the array-to-pointer
5814 conversion (_conv.array_) are applied. */
5815 else if (TYPE_PTROBV_P (type))
5817 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5819 A template-argument for a non-type, non-template template-parameter
5820 shall be one of: [...]
5822 -- the name of a non-type template-parameter;
5823 -- the address of an object or function with external linkage, [...]
5824 expressed as "& id-expression" where the & is optional if the name
5825 refers to a function or array, or if the corresponding
5826 template-parameter is a reference.
5828 Here, we do not care about functions, as they are invalid anyway
5829 for a parameter of type pointer-to-object. */
5831 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5832 /* Non-type template parameters are OK. */
5834 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5835 /* Null pointer values are OK in C++11. */;
5836 else if (TREE_CODE (expr) != ADDR_EXPR
5837 && TREE_CODE (expr_type) != ARRAY_TYPE)
5839 if (VAR_P (expr))
5841 error ("%qD is not a valid template argument "
5842 "because %qD is a variable, not the address of "
5843 "a variable",
5844 expr, expr);
5845 return NULL_TREE;
5847 if (POINTER_TYPE_P (expr_type))
5849 error ("%qE is not a valid template argument for %qT "
5850 "because it is not the address of a variable",
5851 expr, type);
5852 return NULL_TREE;
5854 /* Other values, like integer constants, might be valid
5855 non-type arguments of some other type. */
5856 return error_mark_node;
5858 else
5860 tree decl;
5862 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5863 ? TREE_OPERAND (expr, 0) : expr);
5864 if (!VAR_P (decl))
5866 error ("%qE is not a valid template argument of type %qT "
5867 "because %qE is not a variable",
5868 expr, type, decl);
5869 return NULL_TREE;
5871 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
5873 error ("%qE is not a valid template argument of type %qT "
5874 "because %qD does not have external linkage",
5875 expr, type, decl);
5876 return NULL_TREE;
5878 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
5880 error ("%qE is not a valid template argument of type %qT "
5881 "because %qD has no linkage",
5882 expr, type, decl);
5883 return NULL_TREE;
5887 expr = decay_conversion (expr, complain);
5888 if (expr == error_mark_node)
5889 return error_mark_node;
5891 expr = perform_qualification_conversions (type, expr);
5892 if (expr == error_mark_node)
5893 return error_mark_node;
5895 /* [temp.arg.nontype]/5, bullet 3
5897 For a non-type template-parameter of type reference to object, no
5898 conversions apply. The type referred to by the reference may be more
5899 cv-qualified than the (otherwise identical) type of the
5900 template-argument. The template-parameter is bound directly to the
5901 template-argument, which must be an lvalue. */
5902 else if (TYPE_REF_OBJ_P (type))
5904 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5905 expr_type))
5906 return error_mark_node;
5908 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5910 error ("%qE is not a valid template argument for type %qT "
5911 "because of conflicts in cv-qualification", expr, type);
5912 return NULL_TREE;
5915 if (!real_lvalue_p (expr))
5917 error ("%qE is not a valid template argument for type %qT "
5918 "because it is not an lvalue", expr, type);
5919 return NULL_TREE;
5922 /* [temp.arg.nontype]/1
5924 A template-argument for a non-type, non-template template-parameter
5925 shall be one of: [...]
5927 -- the address of an object or function with external linkage. */
5928 if (INDIRECT_REF_P (expr)
5929 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5931 expr = TREE_OPERAND (expr, 0);
5932 if (DECL_P (expr))
5934 error ("%q#D is not a valid template argument for type %qT "
5935 "because a reference variable does not have a constant "
5936 "address", expr, type);
5937 return NULL_TREE;
5941 if (!DECL_P (expr))
5943 error ("%qE is not a valid template argument for type %qT "
5944 "because it is not an object with external linkage",
5945 expr, type);
5946 return NULL_TREE;
5949 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5951 error ("%qE is not a valid template argument for type %qT "
5952 "because object %qD has not external linkage",
5953 expr, type, expr);
5954 return NULL_TREE;
5957 expr = build_nop (type, build_address (expr));
5959 /* [temp.arg.nontype]/5, bullet 4
5961 For a non-type template-parameter of type pointer to function, only
5962 the function-to-pointer conversion (_conv.func_) is applied. If the
5963 template-argument represents a set of overloaded functions (or a
5964 pointer to such), the matching function is selected from the set
5965 (_over.over_). */
5966 else if (TYPE_PTRFN_P (type))
5968 /* If the argument is a template-id, we might not have enough
5969 context information to decay the pointer. */
5970 if (!type_unknown_p (expr_type))
5972 expr = decay_conversion (expr, complain);
5973 if (expr == error_mark_node)
5974 return error_mark_node;
5977 if (cxx_dialect >= cxx11 && integer_zerop (expr))
5978 /* Null pointer values are OK in C++11. */
5979 return perform_qualification_conversions (type, expr);
5981 expr = convert_nontype_argument_function (type, expr);
5982 if (!expr || expr == error_mark_node)
5983 return expr;
5985 /* [temp.arg.nontype]/5, bullet 5
5987 For a non-type template-parameter of type reference to function, no
5988 conversions apply. If the template-argument represents a set of
5989 overloaded functions, the matching function is selected from the set
5990 (_over.over_). */
5991 else if (TYPE_REFFN_P (type))
5993 if (TREE_CODE (expr) == ADDR_EXPR)
5995 error ("%qE is not a valid template argument for type %qT "
5996 "because it is a pointer", expr, type);
5997 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5998 return NULL_TREE;
6001 expr = convert_nontype_argument_function (type, expr);
6002 if (!expr || expr == error_mark_node)
6003 return expr;
6005 expr = build_nop (type, build_address (expr));
6007 /* [temp.arg.nontype]/5, bullet 6
6009 For a non-type template-parameter of type pointer to member function,
6010 no conversions apply. If the template-argument represents a set of
6011 overloaded member functions, the matching member function is selected
6012 from the set (_over.over_). */
6013 else if (TYPE_PTRMEMFUNC_P (type))
6015 expr = instantiate_type (type, expr, tf_none);
6016 if (expr == error_mark_node)
6017 return error_mark_node;
6019 /* [temp.arg.nontype] bullet 1 says the pointer to member
6020 expression must be a pointer-to-member constant. */
6021 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6022 return error_mark_node;
6024 /* There is no way to disable standard conversions in
6025 resolve_address_of_overloaded_function (called by
6026 instantiate_type). It is possible that the call succeeded by
6027 converting &B::I to &D::I (where B is a base of D), so we need
6028 to reject this conversion here.
6030 Actually, even if there was a way to disable standard conversions,
6031 it would still be better to reject them here so that we can
6032 provide a superior diagnostic. */
6033 if (!same_type_p (TREE_TYPE (expr), type))
6035 error ("%qE is not a valid template argument for type %qT "
6036 "because it is of type %qT", expr, type,
6037 TREE_TYPE (expr));
6038 /* If we are just one standard conversion off, explain. */
6039 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6040 inform (input_location,
6041 "standard conversions are not allowed in this context");
6042 return NULL_TREE;
6045 /* [temp.arg.nontype]/5, bullet 7
6047 For a non-type template-parameter of type pointer to data member,
6048 qualification conversions (_conv.qual_) are applied. */
6049 else if (TYPE_PTRDATAMEM_P (type))
6051 /* [temp.arg.nontype] bullet 1 says the pointer to member
6052 expression must be a pointer-to-member constant. */
6053 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6054 return error_mark_node;
6056 expr = perform_qualification_conversions (type, expr);
6057 if (expr == error_mark_node)
6058 return expr;
6060 else if (NULLPTR_TYPE_P (type))
6062 if (expr != nullptr_node)
6064 error ("%qE is not a valid template argument for type %qT "
6065 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6066 return NULL_TREE;
6068 return expr;
6070 /* A template non-type parameter must be one of the above. */
6071 else
6072 gcc_unreachable ();
6074 /* Sanity check: did we actually convert the argument to the
6075 right type? */
6076 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6077 (type, TREE_TYPE (expr)));
6078 return expr;
6081 /* Subroutine of coerce_template_template_parms, which returns 1 if
6082 PARM_PARM and ARG_PARM match using the rule for the template
6083 parameters of template template parameters. Both PARM and ARG are
6084 template parameters; the rest of the arguments are the same as for
6085 coerce_template_template_parms.
6087 static int
6088 coerce_template_template_parm (tree parm,
6089 tree arg,
6090 tsubst_flags_t complain,
6091 tree in_decl,
6092 tree outer_args)
6094 if (arg == NULL_TREE || arg == error_mark_node
6095 || parm == NULL_TREE || parm == error_mark_node)
6096 return 0;
6098 if (TREE_CODE (arg) != TREE_CODE (parm))
6099 return 0;
6101 switch (TREE_CODE (parm))
6103 case TEMPLATE_DECL:
6104 /* We encounter instantiations of templates like
6105 template <template <template <class> class> class TT>
6106 class C; */
6108 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6109 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6111 if (!coerce_template_template_parms
6112 (parmparm, argparm, complain, in_decl, outer_args))
6113 return 0;
6115 /* Fall through. */
6117 case TYPE_DECL:
6118 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6119 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6120 /* Argument is a parameter pack but parameter is not. */
6121 return 0;
6122 break;
6124 case PARM_DECL:
6125 /* The tsubst call is used to handle cases such as
6127 template <int> class C {};
6128 template <class T, template <T> class TT> class D {};
6129 D<int, C> d;
6131 i.e. the parameter list of TT depends on earlier parameters. */
6132 if (!uses_template_parms (TREE_TYPE (arg))
6133 && !same_type_p
6134 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6135 TREE_TYPE (arg)))
6136 return 0;
6138 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6139 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6140 /* Argument is a parameter pack but parameter is not. */
6141 return 0;
6143 break;
6145 default:
6146 gcc_unreachable ();
6149 return 1;
6153 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6154 template template parameters. Both PARM_PARMS and ARG_PARMS are
6155 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6156 or PARM_DECL.
6158 Consider the example:
6159 template <class T> class A;
6160 template<template <class U> class TT> class B;
6162 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6163 the parameters to A, and OUTER_ARGS contains A. */
6165 static int
6166 coerce_template_template_parms (tree parm_parms,
6167 tree arg_parms,
6168 tsubst_flags_t complain,
6169 tree in_decl,
6170 tree outer_args)
6172 int nparms, nargs, i;
6173 tree parm, arg;
6174 int variadic_p = 0;
6176 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6177 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6179 nparms = TREE_VEC_LENGTH (parm_parms);
6180 nargs = TREE_VEC_LENGTH (arg_parms);
6182 /* Determine whether we have a parameter pack at the end of the
6183 template template parameter's template parameter list. */
6184 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6186 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6188 if (parm == error_mark_node)
6189 return 0;
6191 switch (TREE_CODE (parm))
6193 case TEMPLATE_DECL:
6194 case TYPE_DECL:
6195 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6196 variadic_p = 1;
6197 break;
6199 case PARM_DECL:
6200 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6201 variadic_p = 1;
6202 break;
6204 default:
6205 gcc_unreachable ();
6209 if (nargs != nparms
6210 && !(variadic_p && nargs >= nparms - 1))
6211 return 0;
6213 /* Check all of the template parameters except the parameter pack at
6214 the end (if any). */
6215 for (i = 0; i < nparms - variadic_p; ++i)
6217 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6218 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6219 continue;
6221 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6222 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6224 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6225 outer_args))
6226 return 0;
6230 if (variadic_p)
6232 /* Check each of the template parameters in the template
6233 argument against the template parameter pack at the end of
6234 the template template parameter. */
6235 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6236 return 0;
6238 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6240 for (; i < nargs; ++i)
6242 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6243 continue;
6245 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6247 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6248 outer_args))
6249 return 0;
6253 return 1;
6256 /* Verifies that the deduced template arguments (in TARGS) for the
6257 template template parameters (in TPARMS) represent valid bindings,
6258 by comparing the template parameter list of each template argument
6259 to the template parameter list of its corresponding template
6260 template parameter, in accordance with DR150. This
6261 routine can only be called after all template arguments have been
6262 deduced. It will return TRUE if all of the template template
6263 parameter bindings are okay, FALSE otherwise. */
6264 bool
6265 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6267 int i, ntparms = TREE_VEC_LENGTH (tparms);
6268 bool ret = true;
6270 /* We're dealing with template parms in this process. */
6271 ++processing_template_decl;
6273 targs = INNERMOST_TEMPLATE_ARGS (targs);
6275 for (i = 0; i < ntparms; ++i)
6277 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6278 tree targ = TREE_VEC_ELT (targs, i);
6280 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6282 tree packed_args = NULL_TREE;
6283 int idx, len = 1;
6285 if (ARGUMENT_PACK_P (targ))
6287 /* Look inside the argument pack. */
6288 packed_args = ARGUMENT_PACK_ARGS (targ);
6289 len = TREE_VEC_LENGTH (packed_args);
6292 for (idx = 0; idx < len; ++idx)
6294 tree targ_parms = NULL_TREE;
6296 if (packed_args)
6297 /* Extract the next argument from the argument
6298 pack. */
6299 targ = TREE_VEC_ELT (packed_args, idx);
6301 if (PACK_EXPANSION_P (targ))
6302 /* Look at the pattern of the pack expansion. */
6303 targ = PACK_EXPANSION_PATTERN (targ);
6305 /* Extract the template parameters from the template
6306 argument. */
6307 if (TREE_CODE (targ) == TEMPLATE_DECL)
6308 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6309 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6310 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6312 /* Verify that we can coerce the template template
6313 parameters from the template argument to the template
6314 parameter. This requires an exact match. */
6315 if (targ_parms
6316 && !coerce_template_template_parms
6317 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6318 targ_parms,
6319 tf_none,
6320 tparm,
6321 targs))
6323 ret = false;
6324 goto out;
6330 out:
6332 --processing_template_decl;
6333 return ret;
6336 /* Since type attributes aren't mangled, we need to strip them from
6337 template type arguments. */
6339 static tree
6340 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6342 tree mv;
6343 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6344 return arg;
6345 mv = TYPE_MAIN_VARIANT (arg);
6346 arg = strip_typedefs (arg);
6347 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6348 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6350 if (complain & tf_warning)
6351 warning (0, "ignoring attributes on template argument %qT", arg);
6352 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6353 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6355 return arg;
6358 /* Convert the indicated template ARG as necessary to match the
6359 indicated template PARM. Returns the converted ARG, or
6360 error_mark_node if the conversion was unsuccessful. Error and
6361 warning messages are issued under control of COMPLAIN. This
6362 conversion is for the Ith parameter in the parameter list. ARGS is
6363 the full set of template arguments deduced so far. */
6365 static tree
6366 convert_template_argument (tree parm,
6367 tree arg,
6368 tree args,
6369 tsubst_flags_t complain,
6370 int i,
6371 tree in_decl)
6373 tree orig_arg;
6374 tree val;
6375 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6377 if (TREE_CODE (arg) == TREE_LIST
6378 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6380 /* The template argument was the name of some
6381 member function. That's usually
6382 invalid, but static members are OK. In any
6383 case, grab the underlying fields/functions
6384 and issue an error later if required. */
6385 orig_arg = TREE_VALUE (arg);
6386 TREE_TYPE (arg) = unknown_type_node;
6389 orig_arg = arg;
6391 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6392 requires_type = (TREE_CODE (parm) == TYPE_DECL
6393 || requires_tmpl_type);
6395 /* When determining whether an argument pack expansion is a template,
6396 look at the pattern. */
6397 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6398 arg = PACK_EXPANSION_PATTERN (arg);
6400 /* Deal with an injected-class-name used as a template template arg. */
6401 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6403 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6404 if (TREE_CODE (t) == TEMPLATE_DECL)
6406 if (cxx_dialect >= cxx11)
6407 /* OK under DR 1004. */;
6408 else if (complain & tf_warning_or_error)
6409 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6410 " used as template template argument", TYPE_NAME (arg));
6411 else if (flag_pedantic_errors)
6412 t = arg;
6414 arg = t;
6418 is_tmpl_type =
6419 ((TREE_CODE (arg) == TEMPLATE_DECL
6420 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6421 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6422 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6423 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6425 if (is_tmpl_type
6426 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6427 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6428 arg = TYPE_STUB_DECL (arg);
6430 is_type = TYPE_P (arg) || is_tmpl_type;
6432 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6433 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6435 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6437 if (complain & tf_error)
6438 error ("invalid use of destructor %qE as a type", orig_arg);
6439 return error_mark_node;
6442 permerror (input_location,
6443 "to refer to a type member of a template parameter, "
6444 "use %<typename %E%>", orig_arg);
6446 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6447 TREE_OPERAND (arg, 1),
6448 typename_type,
6449 complain);
6450 arg = orig_arg;
6451 is_type = 1;
6453 if (is_type != requires_type)
6455 if (in_decl)
6457 if (complain & tf_error)
6459 error ("type/value mismatch at argument %d in template "
6460 "parameter list for %qD",
6461 i + 1, in_decl);
6462 if (is_type)
6463 error (" expected a constant of type %qT, got %qT",
6464 TREE_TYPE (parm),
6465 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6466 else if (requires_tmpl_type)
6467 error (" expected a class template, got %qE", orig_arg);
6468 else
6469 error (" expected a type, got %qE", orig_arg);
6472 return error_mark_node;
6474 if (is_tmpl_type ^ requires_tmpl_type)
6476 if (in_decl && (complain & tf_error))
6478 error ("type/value mismatch at argument %d in template "
6479 "parameter list for %qD",
6480 i + 1, in_decl);
6481 if (is_tmpl_type)
6482 error (" expected a type, got %qT", DECL_NAME (arg));
6483 else
6484 error (" expected a class template, got %qT", orig_arg);
6486 return error_mark_node;
6489 if (is_type)
6491 if (requires_tmpl_type)
6493 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6494 val = orig_arg;
6495 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6496 /* The number of argument required is not known yet.
6497 Just accept it for now. */
6498 val = TREE_TYPE (arg);
6499 else
6501 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6502 tree argparm;
6504 /* Strip alias templates that are equivalent to another
6505 template. */
6506 arg = get_underlying_template (arg);
6507 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6509 if (coerce_template_template_parms (parmparm, argparm,
6510 complain, in_decl,
6511 args))
6513 val = arg;
6515 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6516 TEMPLATE_DECL. */
6517 if (val != error_mark_node)
6519 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6520 val = TREE_TYPE (val);
6521 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6522 val = make_pack_expansion (val);
6525 else
6527 if (in_decl && (complain & tf_error))
6529 error ("type/value mismatch at argument %d in "
6530 "template parameter list for %qD",
6531 i + 1, in_decl);
6532 error (" expected a template of type %qD, got %qT",
6533 parm, orig_arg);
6536 val = error_mark_node;
6540 else
6541 val = orig_arg;
6542 /* We only form one instance of each template specialization.
6543 Therefore, if we use a non-canonical variant (i.e., a
6544 typedef), any future messages referring to the type will use
6545 the typedef, which is confusing if those future uses do not
6546 themselves also use the typedef. */
6547 if (TYPE_P (val))
6548 val = canonicalize_type_argument (val, complain);
6550 else
6552 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6554 if (invalid_nontype_parm_type_p (t, complain))
6555 return error_mark_node;
6557 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6559 if (same_type_p (t, TREE_TYPE (orig_arg)))
6560 val = orig_arg;
6561 else
6563 /* Not sure if this is reachable, but it doesn't hurt
6564 to be robust. */
6565 error ("type mismatch in nontype parameter pack");
6566 val = error_mark_node;
6569 else if (!dependent_template_arg_p (orig_arg)
6570 && !uses_template_parms (t))
6571 /* We used to call digest_init here. However, digest_init
6572 will report errors, which we don't want when complain
6573 is zero. More importantly, digest_init will try too
6574 hard to convert things: for example, `0' should not be
6575 converted to pointer type at this point according to
6576 the standard. Accepting this is not merely an
6577 extension, since deciding whether or not these
6578 conversions can occur is part of determining which
6579 function template to call, or whether a given explicit
6580 argument specification is valid. */
6581 val = convert_nontype_argument (t, orig_arg, complain);
6582 else
6583 val = strip_typedefs_expr (orig_arg);
6585 if (val == NULL_TREE)
6586 val = error_mark_node;
6587 else if (val == error_mark_node && (complain & tf_error))
6588 error ("could not convert template argument %qE to %qT", orig_arg, t);
6590 if (TREE_CODE (val) == SCOPE_REF)
6592 /* Strip typedefs from the SCOPE_REF. */
6593 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6594 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6595 complain);
6596 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6597 QUALIFIED_NAME_IS_TEMPLATE (val));
6601 return val;
6604 /* Coerces the remaining template arguments in INNER_ARGS (from
6605 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6606 Returns the coerced argument pack. PARM_IDX is the position of this
6607 parameter in the template parameter list. ARGS is the original
6608 template argument list. */
6609 static tree
6610 coerce_template_parameter_pack (tree parms,
6611 int parm_idx,
6612 tree args,
6613 tree inner_args,
6614 int arg_idx,
6615 tree new_args,
6616 int* lost,
6617 tree in_decl,
6618 tsubst_flags_t complain)
6620 tree parm = TREE_VEC_ELT (parms, parm_idx);
6621 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6622 tree packed_args;
6623 tree argument_pack;
6624 tree packed_parms = NULL_TREE;
6626 if (arg_idx > nargs)
6627 arg_idx = nargs;
6629 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
6631 /* When the template parameter is a non-type template parameter pack
6632 or template template parameter pack whose type or template
6633 parameters use parameter packs, we know exactly how many arguments
6634 we are looking for. Build a vector of the instantiated decls for
6635 these template parameters in PACKED_PARMS. */
6636 /* We can't use make_pack_expansion here because it would interpret a
6637 _DECL as a use rather than a declaration. */
6638 tree decl = TREE_VALUE (parm);
6639 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
6640 SET_PACK_EXPANSION_PATTERN (exp, decl);
6641 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
6642 SET_TYPE_STRUCTURAL_EQUALITY (exp);
6644 TREE_VEC_LENGTH (args)--;
6645 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
6646 TREE_VEC_LENGTH (args)++;
6648 if (packed_parms == error_mark_node)
6649 return error_mark_node;
6651 /* If we're doing a partial instantiation of a member template,
6652 verify that all of the types used for the non-type
6653 template parameter pack are, in fact, valid for non-type
6654 template parameters. */
6655 if (arg_idx < nargs
6656 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6658 int j, len = TREE_VEC_LENGTH (packed_parms);
6659 for (j = 0; j < len; ++j)
6661 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
6662 if (invalid_nontype_parm_type_p (t, complain))
6663 return error_mark_node;
6667 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
6669 else
6670 packed_args = make_tree_vec (nargs - arg_idx);
6672 /* Convert the remaining arguments, which will be a part of the
6673 parameter pack "parm". */
6674 for (; arg_idx < nargs; ++arg_idx)
6676 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6677 tree actual_parm = TREE_VALUE (parm);
6678 int pack_idx = arg_idx - parm_idx;
6680 if (packed_parms)
6682 /* Once we've packed as many args as we have types, stop. */
6683 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
6684 break;
6685 else if (PACK_EXPANSION_P (arg))
6686 /* We don't know how many args we have yet, just
6687 use the unconverted ones for now. */
6688 return NULL_TREE;
6689 else
6690 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
6693 if (arg == error_mark_node)
6695 if (complain & tf_error)
6696 error ("template argument %d is invalid", arg_idx + 1);
6698 else
6699 arg = convert_template_argument (actual_parm,
6700 arg, new_args, complain, parm_idx,
6701 in_decl);
6702 if (arg == error_mark_node)
6703 (*lost)++;
6704 TREE_VEC_ELT (packed_args, pack_idx) = arg;
6707 if (arg_idx - parm_idx < TREE_VEC_LENGTH (packed_args)
6708 && TREE_VEC_LENGTH (packed_args) > 0)
6710 error ("wrong number of template arguments (%d, should be %d)",
6711 arg_idx - parm_idx, TREE_VEC_LENGTH (packed_args));
6712 return error_mark_node;
6715 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6716 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6717 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6718 else
6720 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6721 TREE_TYPE (argument_pack)
6722 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6723 TREE_CONSTANT (argument_pack) = 1;
6726 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6727 #ifdef ENABLE_CHECKING
6728 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6729 TREE_VEC_LENGTH (packed_args));
6730 #endif
6731 return argument_pack;
6734 /* Returns the number of pack expansions in the template argument vector
6735 ARGS. */
6737 static int
6738 pack_expansion_args_count (tree args)
6740 int i;
6741 int count = 0;
6742 if (args)
6743 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6745 tree elt = TREE_VEC_ELT (args, i);
6746 if (elt && PACK_EXPANSION_P (elt))
6747 ++count;
6749 return count;
6752 /* Convert all template arguments to their appropriate types, and
6753 return a vector containing the innermost resulting template
6754 arguments. If any error occurs, return error_mark_node. Error and
6755 warning messages are issued under control of COMPLAIN.
6757 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6758 for arguments not specified in ARGS. Otherwise, if
6759 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6760 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6761 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6762 ARGS. */
6764 static tree
6765 coerce_template_parms (tree parms,
6766 tree args,
6767 tree in_decl,
6768 tsubst_flags_t complain,
6769 bool require_all_args,
6770 bool use_default_args)
6772 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6773 tree orig_inner_args;
6774 tree inner_args;
6775 tree new_args;
6776 tree new_inner_args;
6777 int saved_unevaluated_operand;
6778 int saved_inhibit_evaluation_warnings;
6780 /* When used as a boolean value, indicates whether this is a
6781 variadic template parameter list. Since it's an int, we can also
6782 subtract it from nparms to get the number of non-variadic
6783 parameters. */
6784 int variadic_p = 0;
6785 int variadic_args_p = 0;
6786 int post_variadic_parms = 0;
6788 if (args == error_mark_node)
6789 return error_mark_node;
6791 nparms = TREE_VEC_LENGTH (parms);
6793 /* Determine if there are any parameter packs. */
6794 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6796 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6797 if (variadic_p)
6798 ++post_variadic_parms;
6799 if (template_parameter_pack_p (tparm))
6800 ++variadic_p;
6803 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
6804 /* If there are no parameters that follow a parameter pack, we need to
6805 expand any argument packs so that we can deduce a parameter pack from
6806 some non-packed args followed by an argument pack, as in variadic85.C.
6807 If there are such parameters, we need to leave argument packs intact
6808 so the arguments are assigned properly. This can happen when dealing
6809 with a nested class inside a partial specialization of a class
6810 template, as in variadic92.C, or when deducing a template parameter pack
6811 from a sub-declarator, as in variadic114.C. */
6812 if (!post_variadic_parms)
6813 inner_args = expand_template_argument_pack (inner_args);
6815 /* Count any pack expansion args. */
6816 variadic_args_p = pack_expansion_args_count (inner_args);
6818 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6819 if ((nargs > nparms && !variadic_p)
6820 || (nargs < nparms - variadic_p
6821 && require_all_args
6822 && !variadic_args_p
6823 && (!use_default_args
6824 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6825 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6827 if (complain & tf_error)
6829 if (variadic_p)
6831 nparms -= variadic_p;
6832 error ("wrong number of template arguments "
6833 "(%d, should be %d or more)", nargs, nparms);
6835 else
6836 error ("wrong number of template arguments "
6837 "(%d, should be %d)", nargs, nparms);
6839 if (in_decl)
6840 error ("provided for %q+D", in_decl);
6843 return error_mark_node;
6845 /* We can't pass a pack expansion to a non-pack parameter of an alias
6846 template (DR 1430). */
6847 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
6848 && variadic_args_p
6849 && nargs - variadic_args_p < nparms - variadic_p)
6851 if (complain & tf_error)
6853 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
6855 tree arg = TREE_VEC_ELT (inner_args, i);
6856 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6858 if (PACK_EXPANSION_P (arg)
6859 && !template_parameter_pack_p (parm))
6861 error ("pack expansion argument for non-pack parameter "
6862 "%qD of alias template %qD", parm, in_decl);
6863 inform (DECL_SOURCE_LOCATION (parm), "declared here");
6864 goto found;
6867 gcc_unreachable ();
6868 found:;
6870 return error_mark_node;
6873 /* We need to evaluate the template arguments, even though this
6874 template-id may be nested within a "sizeof". */
6875 saved_unevaluated_operand = cp_unevaluated_operand;
6876 cp_unevaluated_operand = 0;
6877 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6878 c_inhibit_evaluation_warnings = 0;
6879 new_inner_args = make_tree_vec (nparms);
6880 new_args = add_outermost_template_args (args, new_inner_args);
6881 int pack_adjust = 0;
6882 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6884 tree arg;
6885 tree parm;
6887 /* Get the Ith template parameter. */
6888 parm = TREE_VEC_ELT (parms, parm_idx);
6890 if (parm == error_mark_node)
6892 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6893 continue;
6896 /* Calculate the next argument. */
6897 if (arg_idx < nargs)
6898 arg = TREE_VEC_ELT (inner_args, arg_idx);
6899 else
6900 arg = NULL_TREE;
6902 if (template_parameter_pack_p (TREE_VALUE (parm))
6903 && !(arg && ARGUMENT_PACK_P (arg)))
6905 /* Some arguments will be placed in the
6906 template parameter pack PARM. */
6907 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6908 inner_args, arg_idx,
6909 new_args, &lost,
6910 in_decl, complain);
6912 if (arg == NULL_TREE)
6914 /* We don't know how many args we have yet, just use the
6915 unconverted (and still packed) ones for now. */
6916 new_inner_args = orig_inner_args;
6917 arg_idx = nargs;
6918 break;
6921 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6923 /* Store this argument. */
6924 if (arg == error_mark_node)
6926 lost++;
6927 /* We are done with all of the arguments. */
6928 arg_idx = nargs;
6930 else
6932 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
6933 arg_idx += pack_adjust;
6936 continue;
6938 else if (arg)
6940 if (PACK_EXPANSION_P (arg))
6942 /* We don't know how many args we have yet, just
6943 use the unconverted ones for now. */
6944 new_inner_args = inner_args;
6945 arg_idx = nargs;
6946 break;
6949 else if (require_all_args)
6951 /* There must be a default arg in this case. */
6952 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6953 complain, in_decl);
6954 /* The position of the first default template argument,
6955 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6956 Record that. */
6957 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6958 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6959 arg_idx - pack_adjust);
6961 else
6962 break;
6964 if (arg == error_mark_node)
6966 if (complain & tf_error)
6967 error ("template argument %d is invalid", arg_idx + 1);
6969 else if (!arg)
6970 /* This only occurs if there was an error in the template
6971 parameter list itself (which we would already have
6972 reported) that we are trying to recover from, e.g., a class
6973 template with a parameter list such as
6974 template<typename..., typename>. */
6975 ++lost;
6976 else
6977 arg = convert_template_argument (TREE_VALUE (parm),
6978 arg, new_args, complain,
6979 parm_idx, in_decl);
6981 if (arg == error_mark_node)
6982 lost++;
6983 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
6985 cp_unevaluated_operand = saved_unevaluated_operand;
6986 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6988 if (variadic_p && arg_idx < nargs)
6990 if (complain & tf_error)
6992 error ("wrong number of template arguments "
6993 "(%d, should be %d)", nargs, arg_idx);
6994 if (in_decl)
6995 error ("provided for %q+D", in_decl);
6997 return error_mark_node;
7000 if (lost)
7001 return error_mark_node;
7003 #ifdef ENABLE_CHECKING
7004 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7005 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7006 TREE_VEC_LENGTH (new_inner_args));
7007 #endif
7009 return new_inner_args;
7012 /* Like coerce_template_parms. If PARMS represents all template
7013 parameters levels, this function returns a vector of vectors
7014 representing all the resulting argument levels. Note that in this
7015 case, only the innermost arguments are coerced because the
7016 outermost ones are supposed to have been coerced already.
7018 Otherwise, if PARMS represents only (the innermost) vector of
7019 parameters, this function returns a vector containing just the
7020 innermost resulting arguments. */
7022 static tree
7023 coerce_innermost_template_parms (tree parms,
7024 tree args,
7025 tree in_decl,
7026 tsubst_flags_t complain,
7027 bool require_all_args,
7028 bool use_default_args)
7030 int parms_depth = TMPL_PARMS_DEPTH (parms);
7031 int args_depth = TMPL_ARGS_DEPTH (args);
7032 tree coerced_args;
7034 if (parms_depth > 1)
7036 coerced_args = make_tree_vec (parms_depth);
7037 tree level;
7038 int cur_depth;
7040 for (level = parms, cur_depth = parms_depth;
7041 parms_depth > 0 && level != NULL_TREE;
7042 level = TREE_CHAIN (level), --cur_depth)
7044 tree l;
7045 if (cur_depth == args_depth)
7046 l = coerce_template_parms (TREE_VALUE (level),
7047 args, in_decl, complain,
7048 require_all_args,
7049 use_default_args);
7050 else
7051 l = TMPL_ARGS_LEVEL (args, cur_depth);
7053 if (l == error_mark_node)
7054 return error_mark_node;
7056 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7059 else
7060 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7061 args, in_decl, complain,
7062 require_all_args,
7063 use_default_args);
7064 return coerced_args;
7067 /* Returns 1 if template args OT and NT are equivalent. */
7069 static int
7070 template_args_equal (tree ot, tree nt)
7072 if (nt == ot)
7073 return 1;
7074 if (nt == NULL_TREE || ot == NULL_TREE)
7075 return false;
7077 if (TREE_CODE (nt) == TREE_VEC)
7078 /* For member templates */
7079 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7080 else if (PACK_EXPANSION_P (ot))
7081 return (PACK_EXPANSION_P (nt)
7082 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7083 PACK_EXPANSION_PATTERN (nt))
7084 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7085 PACK_EXPANSION_EXTRA_ARGS (nt)));
7086 else if (ARGUMENT_PACK_P (ot))
7088 int i, len;
7089 tree opack, npack;
7091 if (!ARGUMENT_PACK_P (nt))
7092 return 0;
7094 opack = ARGUMENT_PACK_ARGS (ot);
7095 npack = ARGUMENT_PACK_ARGS (nt);
7096 len = TREE_VEC_LENGTH (opack);
7097 if (TREE_VEC_LENGTH (npack) != len)
7098 return 0;
7099 for (i = 0; i < len; ++i)
7100 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7101 TREE_VEC_ELT (npack, i)))
7102 return 0;
7103 return 1;
7105 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7107 /* We get here probably because we are in the middle of substituting
7108 into the pattern of a pack expansion. In that case the
7109 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7110 interested in. So we want to use the initial pack argument for
7111 the comparison. */
7112 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7113 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7114 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7115 return template_args_equal (ot, nt);
7117 else if (TYPE_P (nt))
7118 return TYPE_P (ot) && same_type_p (ot, nt);
7119 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7120 return 0;
7121 else
7122 return cp_tree_equal (ot, nt);
7125 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7126 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7127 NEWARG_PTR with the offending arguments if they are non-NULL. */
7129 static int
7130 comp_template_args_with_info (tree oldargs, tree newargs,
7131 tree *oldarg_ptr, tree *newarg_ptr)
7133 int i;
7135 if (oldargs == newargs)
7136 return 1;
7138 if (!oldargs || !newargs)
7139 return 0;
7141 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7142 return 0;
7144 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7146 tree nt = TREE_VEC_ELT (newargs, i);
7147 tree ot = TREE_VEC_ELT (oldargs, i);
7149 if (! template_args_equal (ot, nt))
7151 if (oldarg_ptr != NULL)
7152 *oldarg_ptr = ot;
7153 if (newarg_ptr != NULL)
7154 *newarg_ptr = nt;
7155 return 0;
7158 return 1;
7161 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7162 of template arguments. Returns 0 otherwise. */
7165 comp_template_args (tree oldargs, tree newargs)
7167 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7170 static void
7171 add_pending_template (tree d)
7173 tree ti = (TYPE_P (d)
7174 ? CLASSTYPE_TEMPLATE_INFO (d)
7175 : DECL_TEMPLATE_INFO (d));
7176 struct pending_template *pt;
7177 int level;
7179 if (TI_PENDING_TEMPLATE_FLAG (ti))
7180 return;
7182 /* We are called both from instantiate_decl, where we've already had a
7183 tinst_level pushed, and instantiate_template, where we haven't.
7184 Compensate. */
7185 level = !current_tinst_level || current_tinst_level->decl != d;
7187 if (level)
7188 push_tinst_level (d);
7190 pt = ggc_alloc_pending_template ();
7191 pt->next = NULL;
7192 pt->tinst = current_tinst_level;
7193 if (last_pending_template)
7194 last_pending_template->next = pt;
7195 else
7196 pending_templates = pt;
7198 last_pending_template = pt;
7200 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7202 if (level)
7203 pop_tinst_level ();
7207 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7208 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7209 documentation for TEMPLATE_ID_EXPR. */
7211 tree
7212 lookup_template_function (tree fns, tree arglist)
7214 tree type;
7216 if (fns == error_mark_node || arglist == error_mark_node)
7217 return error_mark_node;
7219 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7221 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7223 error ("%q#D is not a function template", fns);
7224 return error_mark_node;
7227 if (BASELINK_P (fns))
7229 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7230 unknown_type_node,
7231 BASELINK_FUNCTIONS (fns),
7232 arglist);
7233 return fns;
7236 type = TREE_TYPE (fns);
7237 if (TREE_CODE (fns) == OVERLOAD || !type)
7238 type = unknown_type_node;
7240 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7243 /* Within the scope of a template class S<T>, the name S gets bound
7244 (in build_self_reference) to a TYPE_DECL for the class, not a
7245 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7246 or one of its enclosing classes, and that type is a template,
7247 return the associated TEMPLATE_DECL. Otherwise, the original
7248 DECL is returned.
7250 Also handle the case when DECL is a TREE_LIST of ambiguous
7251 injected-class-names from different bases. */
7253 tree
7254 maybe_get_template_decl_from_type_decl (tree decl)
7256 if (decl == NULL_TREE)
7257 return decl;
7259 /* DR 176: A lookup that finds an injected-class-name (10.2
7260 [class.member.lookup]) can result in an ambiguity in certain cases
7261 (for example, if it is found in more than one base class). If all of
7262 the injected-class-names that are found refer to specializations of
7263 the same class template, and if the name is followed by a
7264 template-argument-list, the reference refers to the class template
7265 itself and not a specialization thereof, and is not ambiguous. */
7266 if (TREE_CODE (decl) == TREE_LIST)
7268 tree t, tmpl = NULL_TREE;
7269 for (t = decl; t; t = TREE_CHAIN (t))
7271 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7272 if (!tmpl)
7273 tmpl = elt;
7274 else if (tmpl != elt)
7275 break;
7277 if (tmpl && t == NULL_TREE)
7278 return tmpl;
7279 else
7280 return decl;
7283 return (decl != NULL_TREE
7284 && DECL_SELF_REFERENCE_P (decl)
7285 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7286 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7289 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7290 parameters, find the desired type.
7292 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7294 IN_DECL, if non-NULL, is the template declaration we are trying to
7295 instantiate.
7297 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7298 the class we are looking up.
7300 Issue error and warning messages under control of COMPLAIN.
7302 If the template class is really a local class in a template
7303 function, then the FUNCTION_CONTEXT is the function in which it is
7304 being instantiated.
7306 ??? Note that this function is currently called *twice* for each
7307 template-id: the first time from the parser, while creating the
7308 incomplete type (finish_template_type), and the second type during the
7309 real instantiation (instantiate_template_class). This is surely something
7310 that we want to avoid. It also causes some problems with argument
7311 coercion (see convert_nontype_argument for more information on this). */
7313 static tree
7314 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7315 int entering_scope, tsubst_flags_t complain)
7317 tree templ = NULL_TREE, parmlist;
7318 tree t;
7319 void **slot;
7320 spec_entry *entry;
7321 spec_entry elt;
7322 hashval_t hash;
7324 if (identifier_p (d1))
7326 tree value = innermost_non_namespace_value (d1);
7327 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7328 templ = value;
7329 else
7331 if (context)
7332 push_decl_namespace (context);
7333 templ = lookup_name (d1);
7334 templ = maybe_get_template_decl_from_type_decl (templ);
7335 if (context)
7336 pop_decl_namespace ();
7338 if (templ)
7339 context = DECL_CONTEXT (templ);
7341 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7343 tree type = TREE_TYPE (d1);
7345 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7346 an implicit typename for the second A. Deal with it. */
7347 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7348 type = TREE_TYPE (type);
7350 if (CLASSTYPE_TEMPLATE_INFO (type))
7352 templ = CLASSTYPE_TI_TEMPLATE (type);
7353 d1 = DECL_NAME (templ);
7356 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7357 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7359 templ = TYPE_TI_TEMPLATE (d1);
7360 d1 = DECL_NAME (templ);
7362 else if (TREE_CODE (d1) == TEMPLATE_DECL
7363 && DECL_TEMPLATE_RESULT (d1)
7364 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7366 templ = d1;
7367 d1 = DECL_NAME (templ);
7368 context = DECL_CONTEXT (templ);
7370 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7372 templ = d1;
7373 d1 = DECL_NAME (templ);
7376 /* Issue an error message if we didn't find a template. */
7377 if (! templ)
7379 if (complain & tf_error)
7380 error ("%qT is not a template", d1);
7381 return error_mark_node;
7384 if (TREE_CODE (templ) != TEMPLATE_DECL
7385 /* Make sure it's a user visible template, if it was named by
7386 the user. */
7387 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7388 && !PRIMARY_TEMPLATE_P (templ)))
7390 if (complain & tf_error)
7392 error ("non-template type %qT used as a template", d1);
7393 if (in_decl)
7394 error ("for template declaration %q+D", in_decl);
7396 return error_mark_node;
7399 complain &= ~tf_user;
7401 /* An alias that just changes the name of a template is equivalent to the
7402 other template, so if any of the arguments are pack expansions, strip
7403 the alias to avoid problems with a pack expansion passed to a non-pack
7404 alias template parameter (DR 1430). */
7405 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7406 templ = get_underlying_template (templ);
7408 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7410 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7411 template arguments */
7413 tree parm;
7414 tree arglist2;
7415 tree outer;
7417 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7419 /* Consider an example where a template template parameter declared as
7421 template <class T, class U = std::allocator<T> > class TT
7423 The template parameter level of T and U are one level larger than
7424 of TT. To proper process the default argument of U, say when an
7425 instantiation `TT<int>' is seen, we need to build the full
7426 arguments containing {int} as the innermost level. Outer levels,
7427 available when not appearing as default template argument, can be
7428 obtained from the arguments of the enclosing template.
7430 Suppose that TT is later substituted with std::vector. The above
7431 instantiation is `TT<int, std::allocator<T> >' with TT at
7432 level 1, and T at level 2, while the template arguments at level 1
7433 becomes {std::vector} and the inner level 2 is {int}. */
7435 outer = DECL_CONTEXT (templ);
7436 if (outer)
7437 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7438 else if (current_template_parms)
7439 /* This is an argument of the current template, so we haven't set
7440 DECL_CONTEXT yet. */
7441 outer = current_template_args ();
7443 if (outer)
7444 arglist = add_to_template_args (outer, arglist);
7446 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7447 complain,
7448 /*require_all_args=*/true,
7449 /*use_default_args=*/true);
7450 if (arglist2 == error_mark_node
7451 || (!uses_template_parms (arglist2)
7452 && check_instantiated_args (templ, arglist2, complain)))
7453 return error_mark_node;
7455 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7456 return parm;
7458 else
7460 tree template_type = TREE_TYPE (templ);
7461 tree gen_tmpl;
7462 tree type_decl;
7463 tree found = NULL_TREE;
7464 int arg_depth;
7465 int parm_depth;
7466 int is_dependent_type;
7467 int use_partial_inst_tmpl = false;
7469 if (template_type == error_mark_node)
7470 /* An error occurred while building the template TEMPL, and a
7471 diagnostic has most certainly been emitted for that
7472 already. Let's propagate that error. */
7473 return error_mark_node;
7475 gen_tmpl = most_general_template (templ);
7476 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7477 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7478 arg_depth = TMPL_ARGS_DEPTH (arglist);
7480 if (arg_depth == 1 && parm_depth > 1)
7482 /* We've been given an incomplete set of template arguments.
7483 For example, given:
7485 template <class T> struct S1 {
7486 template <class U> struct S2 {};
7487 template <class U> struct S2<U*> {};
7490 we will be called with an ARGLIST of `U*', but the
7491 TEMPLATE will be `template <class T> template
7492 <class U> struct S1<T>::S2'. We must fill in the missing
7493 arguments. */
7494 arglist
7495 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7496 arglist);
7497 arg_depth = TMPL_ARGS_DEPTH (arglist);
7500 /* Now we should have enough arguments. */
7501 gcc_assert (parm_depth == arg_depth);
7503 /* From here on, we're only interested in the most general
7504 template. */
7506 /* Calculate the BOUND_ARGS. These will be the args that are
7507 actually tsubst'd into the definition to create the
7508 instantiation. */
7509 if (parm_depth > 1)
7511 /* We have multiple levels of arguments to coerce, at once. */
7512 int i;
7513 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7515 tree bound_args = make_tree_vec (parm_depth);
7517 for (i = saved_depth,
7518 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7519 i > 0 && t != NULL_TREE;
7520 --i, t = TREE_CHAIN (t))
7522 tree a;
7523 if (i == saved_depth)
7524 a = coerce_template_parms (TREE_VALUE (t),
7525 arglist, gen_tmpl,
7526 complain,
7527 /*require_all_args=*/true,
7528 /*use_default_args=*/true);
7529 else
7530 /* Outer levels should have already been coerced. */
7531 a = TMPL_ARGS_LEVEL (arglist, i);
7533 /* Don't process further if one of the levels fails. */
7534 if (a == error_mark_node)
7536 /* Restore the ARGLIST to its full size. */
7537 TREE_VEC_LENGTH (arglist) = saved_depth;
7538 return error_mark_node;
7541 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7543 /* We temporarily reduce the length of the ARGLIST so
7544 that coerce_template_parms will see only the arguments
7545 corresponding to the template parameters it is
7546 examining. */
7547 TREE_VEC_LENGTH (arglist)--;
7550 /* Restore the ARGLIST to its full size. */
7551 TREE_VEC_LENGTH (arglist) = saved_depth;
7553 arglist = bound_args;
7555 else
7556 arglist
7557 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7558 INNERMOST_TEMPLATE_ARGS (arglist),
7559 gen_tmpl,
7560 complain,
7561 /*require_all_args=*/true,
7562 /*use_default_args=*/true);
7564 if (arglist == error_mark_node)
7565 /* We were unable to bind the arguments. */
7566 return error_mark_node;
7568 /* In the scope of a template class, explicit references to the
7569 template class refer to the type of the template, not any
7570 instantiation of it. For example, in:
7572 template <class T> class C { void f(C<T>); }
7574 the `C<T>' is just the same as `C'. Outside of the
7575 class, however, such a reference is an instantiation. */
7576 if ((entering_scope
7577 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7578 || currently_open_class (template_type))
7579 /* comp_template_args is expensive, check it last. */
7580 && comp_template_args (TYPE_TI_ARGS (template_type),
7581 arglist))
7582 return template_type;
7584 /* If we already have this specialization, return it. */
7585 elt.tmpl = gen_tmpl;
7586 elt.args = arglist;
7587 hash = hash_specialization (&elt);
7588 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7589 &elt, hash);
7591 if (entry)
7592 return entry->spec;
7594 is_dependent_type = uses_template_parms (arglist);
7596 /* If the deduced arguments are invalid, then the binding
7597 failed. */
7598 if (!is_dependent_type
7599 && check_instantiated_args (gen_tmpl,
7600 INNERMOST_TEMPLATE_ARGS (arglist),
7601 complain))
7602 return error_mark_node;
7604 if (!is_dependent_type
7605 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7606 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7607 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7609 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7610 DECL_NAME (gen_tmpl),
7611 /*tag_scope=*/ts_global);
7612 return found;
7615 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7616 complain, in_decl);
7617 if (context == error_mark_node)
7618 return error_mark_node;
7620 if (!context)
7621 context = global_namespace;
7623 /* Create the type. */
7624 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7626 /* The user referred to a specialization of an alias
7627 template represented by GEN_TMPL.
7629 [temp.alias]/2 says:
7631 When a template-id refers to the specialization of an
7632 alias template, it is equivalent to the associated
7633 type obtained by substitution of its
7634 template-arguments for the template-parameters in the
7635 type-id of the alias template. */
7637 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7638 /* Note that the call above (by indirectly calling
7639 register_specialization in tsubst_decl) registers the
7640 TYPE_DECL representing the specialization of the alias
7641 template. So next time someone substitutes ARGLIST for
7642 the template parms into the alias template (GEN_TMPL),
7643 she'll get that TYPE_DECL back. */
7645 if (t == error_mark_node)
7646 return t;
7648 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7650 if (!is_dependent_type)
7652 set_current_access_from_decl (TYPE_NAME (template_type));
7653 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7654 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7655 arglist, complain, in_decl),
7656 SCOPED_ENUM_P (template_type), NULL);
7658 if (t == error_mark_node)
7659 return t;
7661 else
7663 /* We don't want to call start_enum for this type, since
7664 the values for the enumeration constants may involve
7665 template parameters. And, no one should be interested
7666 in the enumeration constants for such a type. */
7667 t = cxx_make_type (ENUMERAL_TYPE);
7668 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7670 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7671 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7672 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7674 else if (CLASS_TYPE_P (template_type))
7676 t = make_class_type (TREE_CODE (template_type));
7677 CLASSTYPE_DECLARED_CLASS (t)
7678 = CLASSTYPE_DECLARED_CLASS (template_type);
7679 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7680 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7682 /* A local class. Make sure the decl gets registered properly. */
7683 if (context == current_function_decl)
7684 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7686 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7687 /* This instantiation is another name for the primary
7688 template type. Set the TYPE_CANONICAL field
7689 appropriately. */
7690 TYPE_CANONICAL (t) = template_type;
7691 else if (any_template_arguments_need_structural_equality_p (arglist))
7692 /* Some of the template arguments require structural
7693 equality testing, so this template class requires
7694 structural equality testing. */
7695 SET_TYPE_STRUCTURAL_EQUALITY (t);
7697 else
7698 gcc_unreachable ();
7700 /* If we called start_enum or pushtag above, this information
7701 will already be set up. */
7702 if (!TYPE_NAME (t))
7704 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7706 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7707 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7708 DECL_SOURCE_LOCATION (type_decl)
7709 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7711 else
7712 type_decl = TYPE_NAME (t);
7714 if (CLASS_TYPE_P (template_type))
7716 TREE_PRIVATE (type_decl)
7717 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7718 TREE_PROTECTED (type_decl)
7719 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7720 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7722 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7723 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7727 /* Let's consider the explicit specialization of a member
7728 of a class template specialization that is implicitly instantiated,
7729 e.g.:
7730 template<class T>
7731 struct S
7733 template<class U> struct M {}; //#0
7736 template<>
7737 template<>
7738 struct S<int>::M<char> //#1
7740 int i;
7742 [temp.expl.spec]/4 says this is valid.
7744 In this case, when we write:
7745 S<int>::M<char> m;
7747 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7748 the one of #0.
7750 When we encounter #1, we want to store the partial instantiation
7751 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7753 For all cases other than this "explicit specialization of member of a
7754 class template", we just want to store the most general template into
7755 the CLASSTYPE_TI_TEMPLATE of M.
7757 This case of "explicit specialization of member of a class template"
7758 only happens when:
7759 1/ the enclosing class is an instantiation of, and therefore not
7760 the same as, the context of the most general template, and
7761 2/ we aren't looking at the partial instantiation itself, i.e.
7762 the innermost arguments are not the same as the innermost parms of
7763 the most general template.
7765 So it's only when 1/ and 2/ happens that we want to use the partial
7766 instantiation of the member template in lieu of its most general
7767 template. */
7769 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7770 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7771 /* the enclosing class must be an instantiation... */
7772 && CLASS_TYPE_P (context)
7773 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7775 tree partial_inst_args;
7776 TREE_VEC_LENGTH (arglist)--;
7777 ++processing_template_decl;
7778 partial_inst_args =
7779 tsubst (INNERMOST_TEMPLATE_ARGS
7780 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7781 arglist, complain, NULL_TREE);
7782 --processing_template_decl;
7783 TREE_VEC_LENGTH (arglist)++;
7784 use_partial_inst_tmpl =
7785 /*...and we must not be looking at the partial instantiation
7786 itself. */
7787 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7788 partial_inst_args);
7791 if (!use_partial_inst_tmpl)
7792 /* This case is easy; there are no member templates involved. */
7793 found = gen_tmpl;
7794 else
7796 /* This is a full instantiation of a member template. Find
7797 the partial instantiation of which this is an instance. */
7799 /* Temporarily reduce by one the number of levels in the ARGLIST
7800 so as to avoid comparing the last set of arguments. */
7801 TREE_VEC_LENGTH (arglist)--;
7802 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7803 TREE_VEC_LENGTH (arglist)++;
7804 /* FOUND is either a proper class type, or an alias
7805 template specialization. In the later case, it's a
7806 TYPE_DECL, resulting from the substituting of arguments
7807 for parameters in the TYPE_DECL of the alias template
7808 done earlier. So be careful while getting the template
7809 of FOUND. */
7810 found = TREE_CODE (found) == TYPE_DECL
7811 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7812 : CLASSTYPE_TI_TEMPLATE (found);
7815 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7817 elt.spec = t;
7818 slot = htab_find_slot_with_hash (type_specializations,
7819 &elt, hash, INSERT);
7820 entry = ggc_alloc_spec_entry ();
7821 *entry = elt;
7822 *slot = entry;
7824 /* Note this use of the partial instantiation so we can check it
7825 later in maybe_process_partial_specialization. */
7826 DECL_TEMPLATE_INSTANTIATIONS (found)
7827 = tree_cons (arglist, t,
7828 DECL_TEMPLATE_INSTANTIATIONS (found));
7830 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
7831 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7832 /* Now that the type has been registered on the instantiations
7833 list, we set up the enumerators. Because the enumeration
7834 constants may involve the enumeration type itself, we make
7835 sure to register the type first, and then create the
7836 constants. That way, doing tsubst_expr for the enumeration
7837 constants won't result in recursive calls here; we'll find
7838 the instantiation and exit above. */
7839 tsubst_enum (template_type, t, arglist);
7841 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7842 /* If the type makes use of template parameters, the
7843 code that generates debugging information will crash. */
7844 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
7846 /* Possibly limit visibility based on template args. */
7847 TREE_PUBLIC (type_decl) = 1;
7848 determine_visibility (type_decl);
7850 return t;
7854 /* Wrapper for lookup_template_class_1. */
7856 tree
7857 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7858 int entering_scope, tsubst_flags_t complain)
7860 tree ret;
7861 timevar_push (TV_TEMPLATE_INST);
7862 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7863 entering_scope, complain);
7864 timevar_pop (TV_TEMPLATE_INST);
7865 return ret;
7868 struct pair_fn_data
7870 tree_fn_t fn;
7871 void *data;
7872 /* True when we should also visit template parameters that occur in
7873 non-deduced contexts. */
7874 bool include_nondeduced_p;
7875 struct pointer_set_t *visited;
7878 /* Called from for_each_template_parm via walk_tree. */
7880 static tree
7881 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7883 tree t = *tp;
7884 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7885 tree_fn_t fn = pfd->fn;
7886 void *data = pfd->data;
7888 if (TYPE_P (t)
7889 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7890 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7891 pfd->include_nondeduced_p))
7892 return error_mark_node;
7894 switch (TREE_CODE (t))
7896 case RECORD_TYPE:
7897 if (TYPE_PTRMEMFUNC_P (t))
7898 break;
7899 /* Fall through. */
7901 case UNION_TYPE:
7902 case ENUMERAL_TYPE:
7903 if (!TYPE_TEMPLATE_INFO (t))
7904 *walk_subtrees = 0;
7905 else if (for_each_template_parm (TYPE_TI_ARGS (t),
7906 fn, data, pfd->visited,
7907 pfd->include_nondeduced_p))
7908 return error_mark_node;
7909 break;
7911 case INTEGER_TYPE:
7912 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7913 fn, data, pfd->visited,
7914 pfd->include_nondeduced_p)
7915 || for_each_template_parm (TYPE_MAX_VALUE (t),
7916 fn, data, pfd->visited,
7917 pfd->include_nondeduced_p))
7918 return error_mark_node;
7919 break;
7921 case METHOD_TYPE:
7922 /* Since we're not going to walk subtrees, we have to do this
7923 explicitly here. */
7924 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7925 pfd->visited, pfd->include_nondeduced_p))
7926 return error_mark_node;
7927 /* Fall through. */
7929 case FUNCTION_TYPE:
7930 /* Check the return type. */
7931 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7932 pfd->include_nondeduced_p))
7933 return error_mark_node;
7935 /* Check the parameter types. Since default arguments are not
7936 instantiated until they are needed, the TYPE_ARG_TYPES may
7937 contain expressions that involve template parameters. But,
7938 no-one should be looking at them yet. And, once they're
7939 instantiated, they don't contain template parameters, so
7940 there's no point in looking at them then, either. */
7942 tree parm;
7944 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7945 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7946 pfd->visited, pfd->include_nondeduced_p))
7947 return error_mark_node;
7949 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7950 want walk_tree walking into them itself. */
7951 *walk_subtrees = 0;
7953 break;
7955 case TYPEOF_TYPE:
7956 case UNDERLYING_TYPE:
7957 if (pfd->include_nondeduced_p
7958 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7959 pfd->visited,
7960 pfd->include_nondeduced_p))
7961 return error_mark_node;
7962 break;
7964 case FUNCTION_DECL:
7965 case VAR_DECL:
7966 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7967 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7968 pfd->visited, pfd->include_nondeduced_p))
7969 return error_mark_node;
7970 /* Fall through. */
7972 case PARM_DECL:
7973 case CONST_DECL:
7974 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7975 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7976 pfd->visited, pfd->include_nondeduced_p))
7977 return error_mark_node;
7978 if (DECL_CONTEXT (t)
7979 && pfd->include_nondeduced_p
7980 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7981 pfd->visited, pfd->include_nondeduced_p))
7982 return error_mark_node;
7983 break;
7985 case BOUND_TEMPLATE_TEMPLATE_PARM:
7986 /* Record template parameters such as `T' inside `TT<T>'. */
7987 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7988 pfd->include_nondeduced_p))
7989 return error_mark_node;
7990 /* Fall through. */
7992 case TEMPLATE_TEMPLATE_PARM:
7993 case TEMPLATE_TYPE_PARM:
7994 case TEMPLATE_PARM_INDEX:
7995 if (fn && (*fn)(t, data))
7996 return error_mark_node;
7997 else if (!fn)
7998 return error_mark_node;
7999 break;
8001 case TEMPLATE_DECL:
8002 /* A template template parameter is encountered. */
8003 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
8004 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8005 pfd->include_nondeduced_p))
8006 return error_mark_node;
8008 /* Already substituted template template parameter */
8009 *walk_subtrees = 0;
8010 break;
8012 case TYPENAME_TYPE:
8013 if (!fn
8014 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
8015 data, pfd->visited,
8016 pfd->include_nondeduced_p))
8017 return error_mark_node;
8018 break;
8020 case CONSTRUCTOR:
8021 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8022 && pfd->include_nondeduced_p
8023 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8024 (TREE_TYPE (t)), fn, data,
8025 pfd->visited, pfd->include_nondeduced_p))
8026 return error_mark_node;
8027 break;
8029 case INDIRECT_REF:
8030 case COMPONENT_REF:
8031 /* If there's no type, then this thing must be some expression
8032 involving template parameters. */
8033 if (!fn && !TREE_TYPE (t))
8034 return error_mark_node;
8035 break;
8037 case MODOP_EXPR:
8038 case CAST_EXPR:
8039 case IMPLICIT_CONV_EXPR:
8040 case REINTERPRET_CAST_EXPR:
8041 case CONST_CAST_EXPR:
8042 case STATIC_CAST_EXPR:
8043 case DYNAMIC_CAST_EXPR:
8044 case ARROW_EXPR:
8045 case DOTSTAR_EXPR:
8046 case TYPEID_EXPR:
8047 case PSEUDO_DTOR_EXPR:
8048 if (!fn)
8049 return error_mark_node;
8050 break;
8052 default:
8053 break;
8056 /* We didn't find any template parameters we liked. */
8057 return NULL_TREE;
8060 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8061 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8062 call FN with the parameter and the DATA.
8063 If FN returns nonzero, the iteration is terminated, and
8064 for_each_template_parm returns 1. Otherwise, the iteration
8065 continues. If FN never returns a nonzero value, the value
8066 returned by for_each_template_parm is 0. If FN is NULL, it is
8067 considered to be the function which always returns 1.
8069 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8070 parameters that occur in non-deduced contexts. When false, only
8071 visits those template parameters that can be deduced. */
8073 static int
8074 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8075 struct pointer_set_t *visited,
8076 bool include_nondeduced_p)
8078 struct pair_fn_data pfd;
8079 int result;
8081 /* Set up. */
8082 pfd.fn = fn;
8083 pfd.data = data;
8084 pfd.include_nondeduced_p = include_nondeduced_p;
8086 /* Walk the tree. (Conceptually, we would like to walk without
8087 duplicates, but for_each_template_parm_r recursively calls
8088 for_each_template_parm, so we would need to reorganize a fair
8089 bit to use walk_tree_without_duplicates, so we keep our own
8090 visited list.) */
8091 if (visited)
8092 pfd.visited = visited;
8093 else
8094 pfd.visited = pointer_set_create ();
8095 result = cp_walk_tree (&t,
8096 for_each_template_parm_r,
8097 &pfd,
8098 pfd.visited) != NULL_TREE;
8100 /* Clean up. */
8101 if (!visited)
8103 pointer_set_destroy (pfd.visited);
8104 pfd.visited = 0;
8107 return result;
8110 /* Returns true if T depends on any template parameter. */
8113 uses_template_parms (tree t)
8115 bool dependent_p;
8116 int saved_processing_template_decl;
8118 saved_processing_template_decl = processing_template_decl;
8119 if (!saved_processing_template_decl)
8120 processing_template_decl = 1;
8121 if (TYPE_P (t))
8122 dependent_p = dependent_type_p (t);
8123 else if (TREE_CODE (t) == TREE_VEC)
8124 dependent_p = any_dependent_template_arguments_p (t);
8125 else if (TREE_CODE (t) == TREE_LIST)
8126 dependent_p = (uses_template_parms (TREE_VALUE (t))
8127 || uses_template_parms (TREE_CHAIN (t)));
8128 else if (TREE_CODE (t) == TYPE_DECL)
8129 dependent_p = dependent_type_p (TREE_TYPE (t));
8130 else if (DECL_P (t)
8131 || EXPR_P (t)
8132 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8133 || TREE_CODE (t) == OVERLOAD
8134 || BASELINK_P (t)
8135 || identifier_p (t)
8136 || TREE_CODE (t) == TRAIT_EXPR
8137 || TREE_CODE (t) == CONSTRUCTOR
8138 || CONSTANT_CLASS_P (t))
8139 dependent_p = (type_dependent_expression_p (t)
8140 || value_dependent_expression_p (t));
8141 else
8143 gcc_assert (t == error_mark_node);
8144 dependent_p = false;
8147 processing_template_decl = saved_processing_template_decl;
8149 return dependent_p;
8152 /* Returns true iff current_function_decl is an incompletely instantiated
8153 template. Useful instead of processing_template_decl because the latter
8154 is set to 0 during fold_non_dependent_expr. */
8156 bool
8157 in_template_function (void)
8159 tree fn = current_function_decl;
8160 bool ret;
8161 ++processing_template_decl;
8162 ret = (fn && DECL_LANG_SPECIFIC (fn)
8163 && DECL_TEMPLATE_INFO (fn)
8164 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8165 --processing_template_decl;
8166 return ret;
8169 /* Returns true if T depends on any template parameter with level LEVEL. */
8172 uses_template_parms_level (tree t, int level)
8174 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8175 /*include_nondeduced_p=*/true);
8178 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8179 ill-formed translation unit, i.e. a variable or function that isn't
8180 usable in a constant expression. */
8182 static inline bool
8183 neglectable_inst_p (tree d)
8185 return (DECL_P (d)
8186 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8187 : decl_maybe_constant_var_p (d)));
8190 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8191 neglectable and instantiated from within an erroneous instantiation. */
8193 static bool
8194 limit_bad_template_recursion (tree decl)
8196 struct tinst_level *lev = current_tinst_level;
8197 int errs = errorcount + sorrycount;
8198 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8199 return false;
8201 for (; lev; lev = lev->next)
8202 if (neglectable_inst_p (lev->decl))
8203 break;
8205 return (lev && errs > lev->errors);
8208 static int tinst_depth;
8209 extern int max_tinst_depth;
8210 int depth_reached;
8212 static GTY(()) struct tinst_level *last_error_tinst_level;
8214 /* We're starting to instantiate D; record the template instantiation context
8215 for diagnostics and to restore it later. */
8218 push_tinst_level (tree d)
8220 struct tinst_level *new_level;
8222 if (tinst_depth >= max_tinst_depth)
8224 last_error_tinst_level = current_tinst_level;
8225 if (TREE_CODE (d) == TREE_LIST)
8226 error ("template instantiation depth exceeds maximum of %d (use "
8227 "-ftemplate-depth= to increase the maximum) substituting %qS",
8228 max_tinst_depth, d);
8229 else
8230 error ("template instantiation depth exceeds maximum of %d (use "
8231 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8232 max_tinst_depth, d);
8234 print_instantiation_context ();
8236 return 0;
8239 /* If the current instantiation caused problems, don't let it instantiate
8240 anything else. Do allow deduction substitution and decls usable in
8241 constant expressions. */
8242 if (limit_bad_template_recursion (d))
8243 return 0;
8245 new_level = ggc_alloc_tinst_level ();
8246 new_level->decl = d;
8247 new_level->locus = input_location;
8248 new_level->errors = errorcount+sorrycount;
8249 new_level->in_system_header_p = in_system_header_at (input_location);
8250 new_level->next = current_tinst_level;
8251 current_tinst_level = new_level;
8253 ++tinst_depth;
8254 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8255 depth_reached = tinst_depth;
8257 return 1;
8260 /* We're done instantiating this template; return to the instantiation
8261 context. */
8263 void
8264 pop_tinst_level (void)
8266 /* Restore the filename and line number stashed away when we started
8267 this instantiation. */
8268 input_location = current_tinst_level->locus;
8269 current_tinst_level = current_tinst_level->next;
8270 --tinst_depth;
8273 /* We're instantiating a deferred template; restore the template
8274 instantiation context in which the instantiation was requested, which
8275 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8277 static tree
8278 reopen_tinst_level (struct tinst_level *level)
8280 struct tinst_level *t;
8282 tinst_depth = 0;
8283 for (t = level; t; t = t->next)
8284 ++tinst_depth;
8286 current_tinst_level = level;
8287 pop_tinst_level ();
8288 if (current_tinst_level)
8289 current_tinst_level->errors = errorcount+sorrycount;
8290 return level->decl;
8293 /* Returns the TINST_LEVEL which gives the original instantiation
8294 context. */
8296 struct tinst_level *
8297 outermost_tinst_level (void)
8299 struct tinst_level *level = current_tinst_level;
8300 if (level)
8301 while (level->next)
8302 level = level->next;
8303 return level;
8306 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8307 vector of template arguments, as for tsubst.
8309 Returns an appropriate tsubst'd friend declaration. */
8311 static tree
8312 tsubst_friend_function (tree decl, tree args)
8314 tree new_friend;
8316 if (TREE_CODE (decl) == FUNCTION_DECL
8317 && DECL_TEMPLATE_INSTANTIATION (decl)
8318 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8319 /* This was a friend declared with an explicit template
8320 argument list, e.g.:
8322 friend void f<>(T);
8324 to indicate that f was a template instantiation, not a new
8325 function declaration. Now, we have to figure out what
8326 instantiation of what template. */
8328 tree template_id, arglist, fns;
8329 tree new_args;
8330 tree tmpl;
8331 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8333 /* Friend functions are looked up in the containing namespace scope.
8334 We must enter that scope, to avoid finding member functions of the
8335 current class with same name. */
8336 push_nested_namespace (ns);
8337 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8338 tf_warning_or_error, NULL_TREE,
8339 /*integral_constant_expression_p=*/false);
8340 pop_nested_namespace (ns);
8341 arglist = tsubst (DECL_TI_ARGS (decl), args,
8342 tf_warning_or_error, NULL_TREE);
8343 template_id = lookup_template_function (fns, arglist);
8345 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8346 tmpl = determine_specialization (template_id, new_friend,
8347 &new_args,
8348 /*need_member_template=*/0,
8349 TREE_VEC_LENGTH (args),
8350 tsk_none);
8351 return instantiate_template (tmpl, new_args, tf_error);
8354 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8356 /* The NEW_FRIEND will look like an instantiation, to the
8357 compiler, but is not an instantiation from the point of view of
8358 the language. For example, we might have had:
8360 template <class T> struct S {
8361 template <class U> friend void f(T, U);
8364 Then, in S<int>, template <class U> void f(int, U) is not an
8365 instantiation of anything. */
8366 if (new_friend == error_mark_node)
8367 return error_mark_node;
8369 DECL_USE_TEMPLATE (new_friend) = 0;
8370 if (TREE_CODE (decl) == TEMPLATE_DECL)
8372 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8373 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8374 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8377 /* The mangled name for the NEW_FRIEND is incorrect. The function
8378 is not a template instantiation and should not be mangled like
8379 one. Therefore, we forget the mangling here; we'll recompute it
8380 later if we need it. */
8381 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8383 SET_DECL_RTL (new_friend, NULL);
8384 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8387 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8389 tree old_decl;
8390 tree new_friend_template_info;
8391 tree new_friend_result_template_info;
8392 tree ns;
8393 int new_friend_is_defn;
8395 /* We must save some information from NEW_FRIEND before calling
8396 duplicate decls since that function will free NEW_FRIEND if
8397 possible. */
8398 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8399 new_friend_is_defn =
8400 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8401 (template_for_substitution (new_friend)))
8402 != NULL_TREE);
8403 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8405 /* This declaration is a `primary' template. */
8406 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8408 new_friend_result_template_info
8409 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8411 else
8412 new_friend_result_template_info = NULL_TREE;
8414 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8415 if (new_friend_is_defn)
8416 DECL_INITIAL (new_friend) = error_mark_node;
8418 /* Inside pushdecl_namespace_level, we will push into the
8419 current namespace. However, the friend function should go
8420 into the namespace of the template. */
8421 ns = decl_namespace_context (new_friend);
8422 push_nested_namespace (ns);
8423 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8424 pop_nested_namespace (ns);
8426 if (old_decl == error_mark_node)
8427 return error_mark_node;
8429 if (old_decl != new_friend)
8431 /* This new friend declaration matched an existing
8432 declaration. For example, given:
8434 template <class T> void f(T);
8435 template <class U> class C {
8436 template <class T> friend void f(T) {}
8439 the friend declaration actually provides the definition
8440 of `f', once C has been instantiated for some type. So,
8441 old_decl will be the out-of-class template declaration,
8442 while new_friend is the in-class definition.
8444 But, if `f' was called before this point, the
8445 instantiation of `f' will have DECL_TI_ARGS corresponding
8446 to `T' but not to `U', references to which might appear
8447 in the definition of `f'. Previously, the most general
8448 template for an instantiation of `f' was the out-of-class
8449 version; now it is the in-class version. Therefore, we
8450 run through all specialization of `f', adding to their
8451 DECL_TI_ARGS appropriately. In particular, they need a
8452 new set of outer arguments, corresponding to the
8453 arguments for this class instantiation.
8455 The same situation can arise with something like this:
8457 friend void f(int);
8458 template <class T> class C {
8459 friend void f(T) {}
8462 when `C<int>' is instantiated. Now, `f(int)' is defined
8463 in the class. */
8465 if (!new_friend_is_defn)
8466 /* On the other hand, if the in-class declaration does
8467 *not* provide a definition, then we don't want to alter
8468 existing definitions. We can just leave everything
8469 alone. */
8471 else
8473 tree new_template = TI_TEMPLATE (new_friend_template_info);
8474 tree new_args = TI_ARGS (new_friend_template_info);
8476 /* Overwrite whatever template info was there before, if
8477 any, with the new template information pertaining to
8478 the declaration. */
8479 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8481 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8483 /* We should have called reregister_specialization in
8484 duplicate_decls. */
8485 gcc_assert (retrieve_specialization (new_template,
8486 new_args, 0)
8487 == old_decl);
8489 /* Instantiate it if the global has already been used. */
8490 if (DECL_ODR_USED (old_decl))
8491 instantiate_decl (old_decl, /*defer_ok=*/true,
8492 /*expl_inst_class_mem_p=*/false);
8494 else
8496 tree t;
8498 /* Indicate that the old function template is a partial
8499 instantiation. */
8500 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8501 = new_friend_result_template_info;
8503 gcc_assert (new_template
8504 == most_general_template (new_template));
8505 gcc_assert (new_template != old_decl);
8507 /* Reassign any specializations already in the hash table
8508 to the new more general template, and add the
8509 additional template args. */
8510 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8511 t != NULL_TREE;
8512 t = TREE_CHAIN (t))
8514 tree spec = TREE_VALUE (t);
8515 spec_entry elt;
8517 elt.tmpl = old_decl;
8518 elt.args = DECL_TI_ARGS (spec);
8519 elt.spec = NULL_TREE;
8521 htab_remove_elt (decl_specializations, &elt);
8523 DECL_TI_ARGS (spec)
8524 = add_outermost_template_args (new_args,
8525 DECL_TI_ARGS (spec));
8527 register_specialization
8528 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8531 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8535 /* The information from NEW_FRIEND has been merged into OLD_DECL
8536 by duplicate_decls. */
8537 new_friend = old_decl;
8540 else
8542 tree context = DECL_CONTEXT (new_friend);
8543 bool dependent_p;
8545 /* In the code
8546 template <class T> class C {
8547 template <class U> friend void C1<U>::f (); // case 1
8548 friend void C2<T>::f (); // case 2
8550 we only need to make sure CONTEXT is a complete type for
8551 case 2. To distinguish between the two cases, we note that
8552 CONTEXT of case 1 remains dependent type after tsubst while
8553 this isn't true for case 2. */
8554 ++processing_template_decl;
8555 dependent_p = dependent_type_p (context);
8556 --processing_template_decl;
8558 if (!dependent_p
8559 && !complete_type_or_else (context, NULL_TREE))
8560 return error_mark_node;
8562 if (COMPLETE_TYPE_P (context))
8564 tree fn = new_friend;
8565 /* do_friend adds the TEMPLATE_DECL for any member friend
8566 template even if it isn't a member template, i.e.
8567 template <class T> friend A<T>::f();
8568 Look through it in that case. */
8569 if (TREE_CODE (fn) == TEMPLATE_DECL
8570 && !PRIMARY_TEMPLATE_P (fn))
8571 fn = DECL_TEMPLATE_RESULT (fn);
8572 /* Check to see that the declaration is really present, and,
8573 possibly obtain an improved declaration. */
8574 fn = check_classfn (context, fn, NULL_TREE);
8576 if (fn)
8577 new_friend = fn;
8581 return new_friend;
8584 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8585 template arguments, as for tsubst.
8587 Returns an appropriate tsubst'd friend type or error_mark_node on
8588 failure. */
8590 static tree
8591 tsubst_friend_class (tree friend_tmpl, tree args)
8593 tree friend_type;
8594 tree tmpl;
8595 tree context;
8597 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8599 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8600 return TREE_TYPE (t);
8603 context = CP_DECL_CONTEXT (friend_tmpl);
8605 if (context != global_namespace)
8607 if (TREE_CODE (context) == NAMESPACE_DECL)
8608 push_nested_namespace (context);
8609 else
8610 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8613 /* Look for a class template declaration. We look for hidden names
8614 because two friend declarations of the same template are the
8615 same. For example, in:
8617 struct A {
8618 template <typename> friend class F;
8620 template <typename> struct B {
8621 template <typename> friend class F;
8624 both F templates are the same. */
8625 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8626 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8628 /* But, if we don't find one, it might be because we're in a
8629 situation like this:
8631 template <class T>
8632 struct S {
8633 template <class U>
8634 friend struct S;
8637 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8638 for `S<int>', not the TEMPLATE_DECL. */
8639 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8641 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8642 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8645 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8647 /* The friend template has already been declared. Just
8648 check to see that the declarations match, and install any new
8649 default parameters. We must tsubst the default parameters,
8650 of course. We only need the innermost template parameters
8651 because that is all that redeclare_class_template will look
8652 at. */
8653 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8654 > TMPL_ARGS_DEPTH (args))
8656 tree parms;
8657 location_t saved_input_location;
8658 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8659 args, tf_warning_or_error);
8661 saved_input_location = input_location;
8662 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8663 redeclare_class_template (TREE_TYPE (tmpl), parms);
8664 input_location = saved_input_location;
8668 friend_type = TREE_TYPE (tmpl);
8670 else
8672 /* The friend template has not already been declared. In this
8673 case, the instantiation of the template class will cause the
8674 injection of this template into the global scope. */
8675 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8676 if (tmpl == error_mark_node)
8677 return error_mark_node;
8679 /* The new TMPL is not an instantiation of anything, so we
8680 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8681 the new type because that is supposed to be the corresponding
8682 template decl, i.e., TMPL. */
8683 DECL_USE_TEMPLATE (tmpl) = 0;
8684 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8685 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8686 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8687 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8689 /* Inject this template into the global scope. */
8690 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8693 if (context != global_namespace)
8695 if (TREE_CODE (context) == NAMESPACE_DECL)
8696 pop_nested_namespace (context);
8697 else
8698 pop_nested_class ();
8701 return friend_type;
8704 /* Returns zero if TYPE cannot be completed later due to circularity.
8705 Otherwise returns one. */
8707 static int
8708 can_complete_type_without_circularity (tree type)
8710 if (type == NULL_TREE || type == error_mark_node)
8711 return 0;
8712 else if (COMPLETE_TYPE_P (type))
8713 return 1;
8714 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8715 return can_complete_type_without_circularity (TREE_TYPE (type));
8716 else if (CLASS_TYPE_P (type)
8717 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8718 return 0;
8719 else
8720 return 1;
8723 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8725 /* Apply any attributes which had to be deferred until instantiation
8726 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8727 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8729 static void
8730 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8731 tree args, tsubst_flags_t complain, tree in_decl)
8733 tree last_dep = NULL_TREE;
8734 tree t;
8735 tree *p;
8737 for (t = attributes; t; t = TREE_CHAIN (t))
8738 if (ATTR_IS_DEPENDENT (t))
8740 last_dep = t;
8741 attributes = copy_list (attributes);
8742 break;
8745 if (DECL_P (*decl_p))
8747 if (TREE_TYPE (*decl_p) == error_mark_node)
8748 return;
8749 p = &DECL_ATTRIBUTES (*decl_p);
8751 else
8752 p = &TYPE_ATTRIBUTES (*decl_p);
8754 if (last_dep)
8756 tree late_attrs = NULL_TREE;
8757 tree *q = &late_attrs;
8759 for (*p = attributes; *p; )
8761 t = *p;
8762 if (ATTR_IS_DEPENDENT (t))
8764 *p = TREE_CHAIN (t);
8765 TREE_CHAIN (t) = NULL_TREE;
8766 if ((flag_openmp || flag_cilkplus)
8767 && is_attribute_p ("omp declare simd",
8768 get_attribute_name (t))
8769 && TREE_VALUE (t))
8771 tree clauses = TREE_VALUE (TREE_VALUE (t));
8772 clauses = tsubst_omp_clauses (clauses, true, args,
8773 complain, in_decl);
8774 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
8775 clauses = finish_omp_clauses (clauses);
8776 tree parms = DECL_ARGUMENTS (*decl_p);
8777 clauses
8778 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
8779 if (clauses)
8780 TREE_VALUE (TREE_VALUE (t)) = clauses;
8781 else
8782 TREE_VALUE (t) = NULL_TREE;
8784 /* If the first attribute argument is an identifier, don't
8785 pass it through tsubst. Attributes like mode, format,
8786 cleanup and several target specific attributes expect it
8787 unmodified. */
8788 else if (attribute_takes_identifier_p (get_attribute_name (t))
8789 && TREE_VALUE (t))
8791 tree chain
8792 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8793 in_decl,
8794 /*integral_constant_expression_p=*/false);
8795 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8796 TREE_VALUE (t)
8797 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8798 chain);
8800 else
8801 TREE_VALUE (t)
8802 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8803 /*integral_constant_expression_p=*/false);
8804 *q = t;
8805 q = &TREE_CHAIN (t);
8807 else
8808 p = &TREE_CHAIN (t);
8811 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8815 /* Perform (or defer) access check for typedefs that were referenced
8816 from within the template TMPL code.
8817 This is a subroutine of instantiate_decl and instantiate_class_template.
8818 TMPL is the template to consider and TARGS is the list of arguments of
8819 that template. */
8821 static void
8822 perform_typedefs_access_check (tree tmpl, tree targs)
8824 location_t saved_location;
8825 unsigned i;
8826 qualified_typedef_usage_t *iter;
8828 if (!tmpl
8829 || (!CLASS_TYPE_P (tmpl)
8830 && TREE_CODE (tmpl) != FUNCTION_DECL))
8831 return;
8833 saved_location = input_location;
8834 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8836 tree type_decl = iter->typedef_decl;
8837 tree type_scope = iter->context;
8839 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8840 continue;
8842 if (uses_template_parms (type_decl))
8843 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8844 if (uses_template_parms (type_scope))
8845 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8847 /* Make access check error messages point to the location
8848 of the use of the typedef. */
8849 input_location = iter->locus;
8850 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8851 type_decl, type_decl,
8852 tf_warning_or_error);
8854 input_location = saved_location;
8857 static tree
8858 instantiate_class_template_1 (tree type)
8860 tree templ, args, pattern, t, member;
8861 tree typedecl;
8862 tree pbinfo;
8863 tree base_list;
8864 unsigned int saved_maximum_field_alignment;
8865 tree fn_context;
8867 if (type == error_mark_node)
8868 return error_mark_node;
8870 if (COMPLETE_OR_OPEN_TYPE_P (type)
8871 || uses_template_parms (type))
8872 return type;
8874 /* Figure out which template is being instantiated. */
8875 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8876 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8878 /* Determine what specialization of the original template to
8879 instantiate. */
8880 t = most_specialized_class (type, tf_warning_or_error);
8881 if (t == error_mark_node)
8883 TYPE_BEING_DEFINED (type) = 1;
8884 return error_mark_node;
8886 else if (t)
8888 /* This TYPE is actually an instantiation of a partial
8889 specialization. We replace the innermost set of ARGS with
8890 the arguments appropriate for substitution. For example,
8891 given:
8893 template <class T> struct S {};
8894 template <class T> struct S<T*> {};
8896 and supposing that we are instantiating S<int*>, ARGS will
8897 presently be {int*} -- but we need {int}. */
8898 pattern = TREE_TYPE (t);
8899 args = TREE_PURPOSE (t);
8901 else
8903 pattern = TREE_TYPE (templ);
8904 args = CLASSTYPE_TI_ARGS (type);
8907 /* If the template we're instantiating is incomplete, then clearly
8908 there's nothing we can do. */
8909 if (!COMPLETE_TYPE_P (pattern))
8910 return type;
8912 /* If we've recursively instantiated too many templates, stop. */
8913 if (! push_tinst_level (type))
8914 return type;
8916 /* Now we're really doing the instantiation. Mark the type as in
8917 the process of being defined. */
8918 TYPE_BEING_DEFINED (type) = 1;
8920 /* We may be in the middle of deferred access check. Disable
8921 it now. */
8922 push_deferring_access_checks (dk_no_deferred);
8924 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8925 if (!fn_context)
8926 push_to_top_level ();
8927 /* Use #pragma pack from the template context. */
8928 saved_maximum_field_alignment = maximum_field_alignment;
8929 maximum_field_alignment = TYPE_PRECISION (pattern);
8931 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8933 /* Set the input location to the most specialized template definition.
8934 This is needed if tsubsting causes an error. */
8935 typedecl = TYPE_MAIN_DECL (pattern);
8936 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8937 DECL_SOURCE_LOCATION (typedecl);
8939 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8940 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8941 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8942 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8943 if (ANON_AGGR_TYPE_P (pattern))
8944 SET_ANON_AGGR_TYPE_P (type);
8945 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8947 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8948 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8949 /* Adjust visibility for template arguments. */
8950 determine_visibility (TYPE_MAIN_DECL (type));
8952 if (CLASS_TYPE_P (type))
8953 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8955 pbinfo = TYPE_BINFO (pattern);
8957 /* We should never instantiate a nested class before its enclosing
8958 class; we need to look up the nested class by name before we can
8959 instantiate it, and that lookup should instantiate the enclosing
8960 class. */
8961 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8962 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8964 base_list = NULL_TREE;
8965 if (BINFO_N_BASE_BINFOS (pbinfo))
8967 tree pbase_binfo;
8968 tree pushed_scope;
8969 int i;
8971 /* We must enter the scope containing the type, as that is where
8972 the accessibility of types named in dependent bases are
8973 looked up from. */
8974 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8976 /* Substitute into each of the bases to determine the actual
8977 basetypes. */
8978 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8980 tree base;
8981 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8982 tree expanded_bases = NULL_TREE;
8983 int idx, len = 1;
8985 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8987 expanded_bases =
8988 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8989 args, tf_error, NULL_TREE);
8990 if (expanded_bases == error_mark_node)
8991 continue;
8993 len = TREE_VEC_LENGTH (expanded_bases);
8996 for (idx = 0; idx < len; idx++)
8998 if (expanded_bases)
8999 /* Extract the already-expanded base class. */
9000 base = TREE_VEC_ELT (expanded_bases, idx);
9001 else
9002 /* Substitute to figure out the base class. */
9003 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9004 NULL_TREE);
9006 if (base == error_mark_node)
9007 continue;
9009 base_list = tree_cons (access, base, base_list);
9010 if (BINFO_VIRTUAL_P (pbase_binfo))
9011 TREE_TYPE (base_list) = integer_type_node;
9015 /* The list is now in reverse order; correct that. */
9016 base_list = nreverse (base_list);
9018 if (pushed_scope)
9019 pop_scope (pushed_scope);
9021 /* Now call xref_basetypes to set up all the base-class
9022 information. */
9023 xref_basetypes (type, base_list);
9025 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9026 (int) ATTR_FLAG_TYPE_IN_PLACE,
9027 args, tf_error, NULL_TREE);
9028 fixup_attribute_variants (type);
9030 /* Now that our base classes are set up, enter the scope of the
9031 class, so that name lookups into base classes, etc. will work
9032 correctly. This is precisely analogous to what we do in
9033 begin_class_definition when defining an ordinary non-template
9034 class, except we also need to push the enclosing classes. */
9035 push_nested_class (type);
9037 /* Now members are processed in the order of declaration. */
9038 for (member = CLASSTYPE_DECL_LIST (pattern);
9039 member; member = TREE_CHAIN (member))
9041 tree t = TREE_VALUE (member);
9043 if (TREE_PURPOSE (member))
9045 if (TYPE_P (t))
9047 /* Build new CLASSTYPE_NESTED_UTDS. */
9049 tree newtag;
9050 bool class_template_p;
9052 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9053 && TYPE_LANG_SPECIFIC (t)
9054 && CLASSTYPE_IS_TEMPLATE (t));
9055 /* If the member is a class template, then -- even after
9056 substitution -- there may be dependent types in the
9057 template argument list for the class. We increment
9058 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9059 that function will assume that no types are dependent
9060 when outside of a template. */
9061 if (class_template_p)
9062 ++processing_template_decl;
9063 newtag = tsubst (t, args, tf_error, NULL_TREE);
9064 if (class_template_p)
9065 --processing_template_decl;
9066 if (newtag == error_mark_node)
9067 continue;
9069 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9071 tree name = TYPE_IDENTIFIER (t);
9073 if (class_template_p)
9074 /* Unfortunately, lookup_template_class sets
9075 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9076 instantiation (i.e., for the type of a member
9077 template class nested within a template class.)
9078 This behavior is required for
9079 maybe_process_partial_specialization to work
9080 correctly, but is not accurate in this case;
9081 the TAG is not an instantiation of anything.
9082 (The corresponding TEMPLATE_DECL is an
9083 instantiation, but the TYPE is not.) */
9084 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9086 /* Now, we call pushtag to put this NEWTAG into the scope of
9087 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9088 pushtag calling push_template_decl. We don't have to do
9089 this for enums because it will already have been done in
9090 tsubst_enum. */
9091 if (name)
9092 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9093 pushtag (name, newtag, /*tag_scope=*/ts_current);
9096 else if (DECL_DECLARES_FUNCTION_P (t))
9098 /* Build new TYPE_METHODS. */
9099 tree r;
9101 if (TREE_CODE (t) == TEMPLATE_DECL)
9102 ++processing_template_decl;
9103 r = tsubst (t, args, tf_error, NULL_TREE);
9104 if (TREE_CODE (t) == TEMPLATE_DECL)
9105 --processing_template_decl;
9106 set_current_access_from_decl (r);
9107 finish_member_declaration (r);
9108 /* Instantiate members marked with attribute used. */
9109 if (r != error_mark_node && DECL_PRESERVE_P (r))
9110 mark_used (r);
9111 if (TREE_CODE (r) == FUNCTION_DECL
9112 && DECL_OMP_DECLARE_REDUCTION_P (r))
9113 cp_check_omp_declare_reduction (r);
9115 else
9117 /* Build new TYPE_FIELDS. */
9118 if (TREE_CODE (t) == STATIC_ASSERT)
9120 tree condition;
9122 ++c_inhibit_evaluation_warnings;
9123 condition =
9124 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9125 tf_warning_or_error, NULL_TREE,
9126 /*integral_constant_expression_p=*/true);
9127 --c_inhibit_evaluation_warnings;
9129 finish_static_assert (condition,
9130 STATIC_ASSERT_MESSAGE (t),
9131 STATIC_ASSERT_SOURCE_LOCATION (t),
9132 /*member_p=*/true);
9134 else if (TREE_CODE (t) != CONST_DECL)
9136 tree r;
9137 tree vec = NULL_TREE;
9138 int len = 1;
9140 /* The file and line for this declaration, to
9141 assist in error message reporting. Since we
9142 called push_tinst_level above, we don't need to
9143 restore these. */
9144 input_location = DECL_SOURCE_LOCATION (t);
9146 if (TREE_CODE (t) == TEMPLATE_DECL)
9147 ++processing_template_decl;
9148 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9149 if (TREE_CODE (t) == TEMPLATE_DECL)
9150 --processing_template_decl;
9152 if (TREE_CODE (r) == TREE_VEC)
9154 /* A capture pack became multiple fields. */
9155 vec = r;
9156 len = TREE_VEC_LENGTH (vec);
9159 for (int i = 0; i < len; ++i)
9161 if (vec)
9162 r = TREE_VEC_ELT (vec, i);
9163 if (VAR_P (r))
9165 /* In [temp.inst]:
9167 [t]he initialization (and any associated
9168 side-effects) of a static data member does
9169 not occur unless the static data member is
9170 itself used in a way that requires the
9171 definition of the static data member to
9172 exist.
9174 Therefore, we do not substitute into the
9175 initialized for the static data member here. */
9176 finish_static_data_member_decl
9178 /*init=*/NULL_TREE,
9179 /*init_const_expr_p=*/false,
9180 /*asmspec_tree=*/NULL_TREE,
9181 /*flags=*/0);
9182 /* Instantiate members marked with attribute used. */
9183 if (r != error_mark_node && DECL_PRESERVE_P (r))
9184 mark_used (r);
9186 else if (TREE_CODE (r) == FIELD_DECL)
9188 /* Determine whether R has a valid type and can be
9189 completed later. If R is invalid, then its type
9190 is replaced by error_mark_node. */
9191 tree rtype = TREE_TYPE (r);
9192 if (can_complete_type_without_circularity (rtype))
9193 complete_type (rtype);
9195 if (!COMPLETE_TYPE_P (rtype))
9197 cxx_incomplete_type_error (r, rtype);
9198 TREE_TYPE (r) = error_mark_node;
9202 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9203 such a thing will already have been added to the field
9204 list by tsubst_enum in finish_member_declaration in the
9205 CLASSTYPE_NESTED_UTDS case above. */
9206 if (!(TREE_CODE (r) == TYPE_DECL
9207 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9208 && DECL_ARTIFICIAL (r)))
9210 set_current_access_from_decl (r);
9211 finish_member_declaration (r);
9217 else
9219 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9220 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9222 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9224 tree friend_type = t;
9225 bool adjust_processing_template_decl = false;
9227 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9229 /* template <class T> friend class C; */
9230 friend_type = tsubst_friend_class (friend_type, args);
9231 adjust_processing_template_decl = true;
9233 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9235 /* template <class T> friend class C::D; */
9236 friend_type = tsubst (friend_type, args,
9237 tf_warning_or_error, NULL_TREE);
9238 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9239 friend_type = TREE_TYPE (friend_type);
9240 adjust_processing_template_decl = true;
9242 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9243 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9245 /* This could be either
9247 friend class T::C;
9249 when dependent_type_p is false or
9251 template <class U> friend class T::C;
9253 otherwise. */
9254 friend_type = tsubst (friend_type, args,
9255 tf_warning_or_error, NULL_TREE);
9256 /* Bump processing_template_decl for correct
9257 dependent_type_p calculation. */
9258 ++processing_template_decl;
9259 if (dependent_type_p (friend_type))
9260 adjust_processing_template_decl = true;
9261 --processing_template_decl;
9263 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9264 && hidden_name_p (TYPE_NAME (friend_type)))
9266 /* friend class C;
9268 where C hasn't been declared yet. Let's lookup name
9269 from namespace scope directly, bypassing any name that
9270 come from dependent base class. */
9271 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9273 /* The call to xref_tag_from_type does injection for friend
9274 classes. */
9275 push_nested_namespace (ns);
9276 friend_type =
9277 xref_tag_from_type (friend_type, NULL_TREE,
9278 /*tag_scope=*/ts_current);
9279 pop_nested_namespace (ns);
9281 else if (uses_template_parms (friend_type))
9282 /* friend class C<T>; */
9283 friend_type = tsubst (friend_type, args,
9284 tf_warning_or_error, NULL_TREE);
9285 /* Otherwise it's
9287 friend class C;
9289 where C is already declared or
9291 friend class C<int>;
9293 We don't have to do anything in these cases. */
9295 if (adjust_processing_template_decl)
9296 /* Trick make_friend_class into realizing that the friend
9297 we're adding is a template, not an ordinary class. It's
9298 important that we use make_friend_class since it will
9299 perform some error-checking and output cross-reference
9300 information. */
9301 ++processing_template_decl;
9303 if (friend_type != error_mark_node)
9304 make_friend_class (type, friend_type, /*complain=*/false);
9306 if (adjust_processing_template_decl)
9307 --processing_template_decl;
9309 else
9311 /* Build new DECL_FRIENDLIST. */
9312 tree r;
9314 /* The file and line for this declaration, to
9315 assist in error message reporting. Since we
9316 called push_tinst_level above, we don't need to
9317 restore these. */
9318 input_location = DECL_SOURCE_LOCATION (t);
9320 if (TREE_CODE (t) == TEMPLATE_DECL)
9322 ++processing_template_decl;
9323 push_deferring_access_checks (dk_no_check);
9326 r = tsubst_friend_function (t, args);
9327 add_friend (type, r, /*complain=*/false);
9328 if (TREE_CODE (t) == TEMPLATE_DECL)
9330 pop_deferring_access_checks ();
9331 --processing_template_decl;
9337 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9339 tree decl = lambda_function (type);
9340 if (decl)
9342 if (!DECL_TEMPLATE_INFO (decl)
9343 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9344 instantiate_decl (decl, false, false);
9346 /* We need to instantiate the capture list from the template
9347 after we've instantiated the closure members, but before we
9348 consider adding the conversion op. Also keep any captures
9349 that may have been added during instantiation of the op(). */
9350 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9351 tree tmpl_cap
9352 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9353 args, tf_warning_or_error, NULL_TREE,
9354 false, false);
9356 LAMBDA_EXPR_CAPTURE_LIST (expr)
9357 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9359 maybe_add_lambda_conv_op (type);
9361 else
9362 gcc_assert (errorcount);
9365 /* Set the file and line number information to whatever is given for
9366 the class itself. This puts error messages involving generated
9367 implicit functions at a predictable point, and the same point
9368 that would be used for non-template classes. */
9369 input_location = DECL_SOURCE_LOCATION (typedecl);
9371 unreverse_member_declarations (type);
9372 finish_struct_1 (type);
9373 TYPE_BEING_DEFINED (type) = 0;
9375 /* We don't instantiate default arguments for member functions. 14.7.1:
9377 The implicit instantiation of a class template specialization causes
9378 the implicit instantiation of the declarations, but not of the
9379 definitions or default arguments, of the class member functions,
9380 member classes, static data members and member templates.... */
9382 /* Some typedefs referenced from within the template code need to be access
9383 checked at template instantiation time, i.e now. These types were
9384 added to the template at parsing time. Let's get those and perform
9385 the access checks then. */
9386 perform_typedefs_access_check (pattern, args);
9387 perform_deferred_access_checks (tf_warning_or_error);
9388 pop_nested_class ();
9389 maximum_field_alignment = saved_maximum_field_alignment;
9390 if (!fn_context)
9391 pop_from_top_level ();
9392 pop_deferring_access_checks ();
9393 pop_tinst_level ();
9395 /* The vtable for a template class can be emitted in any translation
9396 unit in which the class is instantiated. When there is no key
9397 method, however, finish_struct_1 will already have added TYPE to
9398 the keyed_classes list. */
9399 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9400 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9402 return type;
9405 /* Wrapper for instantiate_class_template_1. */
9407 tree
9408 instantiate_class_template (tree type)
9410 tree ret;
9411 timevar_push (TV_TEMPLATE_INST);
9412 ret = instantiate_class_template_1 (type);
9413 timevar_pop (TV_TEMPLATE_INST);
9414 return ret;
9417 static tree
9418 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9420 tree r;
9422 if (!t)
9423 r = t;
9424 else if (TYPE_P (t))
9425 r = tsubst (t, args, complain, in_decl);
9426 else
9428 if (!(complain & tf_warning))
9429 ++c_inhibit_evaluation_warnings;
9430 r = tsubst_expr (t, args, complain, in_decl,
9431 /*integral_constant_expression_p=*/true);
9432 if (!(complain & tf_warning))
9433 --c_inhibit_evaluation_warnings;
9435 return r;
9438 /* Given a function parameter pack TMPL_PARM and some function parameters
9439 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9440 and set *SPEC_P to point at the next point in the list. */
9442 static tree
9443 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9445 /* Collect all of the extra "packed" parameters into an
9446 argument pack. */
9447 tree parmvec;
9448 tree parmtypevec;
9449 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9450 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9451 tree spec_parm = *spec_p;
9452 int i, len;
9454 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9455 if (tmpl_parm
9456 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9457 break;
9459 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9460 parmvec = make_tree_vec (len);
9461 parmtypevec = make_tree_vec (len);
9462 spec_parm = *spec_p;
9463 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9465 TREE_VEC_ELT (parmvec, i) = spec_parm;
9466 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9469 /* Build the argument packs. */
9470 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9471 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9472 TREE_TYPE (argpack) = argtypepack;
9473 *spec_p = spec_parm;
9475 return argpack;
9478 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9479 NONTYPE_ARGUMENT_PACK. */
9481 static tree
9482 make_fnparm_pack (tree spec_parm)
9484 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9487 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9488 pack expansion. */
9490 static bool
9491 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9493 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9494 if (i >= TREE_VEC_LENGTH (vec))
9495 return false;
9496 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9500 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9502 static tree
9503 make_argument_pack_select (tree arg_pack, unsigned index)
9505 tree aps = make_node (ARGUMENT_PACK_SELECT);
9507 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9508 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9510 return aps;
9513 /* This is a subroutine of tsubst_pack_expansion.
9515 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9516 mechanism to store the (non complete list of) arguments of the
9517 substitution and return a non substituted pack expansion, in order
9518 to wait for when we have enough arguments to really perform the
9519 substitution. */
9521 static bool
9522 use_pack_expansion_extra_args_p (tree parm_packs,
9523 int arg_pack_len,
9524 bool has_empty_arg)
9526 /* If one pack has an expansion and another pack has a normal
9527 argument or if one pack has an empty argument and an another
9528 one hasn't then tsubst_pack_expansion cannot perform the
9529 substitution and need to fall back on the
9530 PACK_EXPANSION_EXTRA mechanism. */
9531 if (parm_packs == NULL_TREE)
9532 return false;
9533 else if (has_empty_arg)
9534 return true;
9536 bool has_expansion_arg = false;
9537 for (int i = 0 ; i < arg_pack_len; ++i)
9539 bool has_non_expansion_arg = false;
9540 for (tree parm_pack = parm_packs;
9541 parm_pack;
9542 parm_pack = TREE_CHAIN (parm_pack))
9544 tree arg = TREE_VALUE (parm_pack);
9546 if (argument_pack_element_is_expansion_p (arg, i))
9547 has_expansion_arg = true;
9548 else
9549 has_non_expansion_arg = true;
9552 if (has_expansion_arg && has_non_expansion_arg)
9553 return true;
9555 return false;
9558 /* [temp.variadic]/6 says that:
9560 The instantiation of a pack expansion [...]
9561 produces a list E1,E2, ..., En, where N is the number of elements
9562 in the pack expansion parameters.
9564 This subroutine of tsubst_pack_expansion produces one of these Ei.
9566 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9567 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9568 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9569 INDEX is the index 'i' of the element Ei to produce. ARGS,
9570 COMPLAIN, and IN_DECL are the same parameters as for the
9571 tsubst_pack_expansion function.
9573 The function returns the resulting Ei upon successful completion,
9574 or error_mark_node.
9576 Note that this function possibly modifies the ARGS parameter, so
9577 it's the responsibility of the caller to restore it. */
9579 static tree
9580 gen_elem_of_pack_expansion_instantiation (tree pattern,
9581 tree parm_packs,
9582 unsigned index,
9583 tree args /* This parm gets
9584 modified. */,
9585 tsubst_flags_t complain,
9586 tree in_decl)
9588 tree t;
9589 bool ith_elem_is_expansion = false;
9591 /* For each parameter pack, change the substitution of the parameter
9592 pack to the ith argument in its argument pack, then expand the
9593 pattern. */
9594 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9596 tree parm = TREE_PURPOSE (pack);
9597 tree arg_pack = TREE_VALUE (pack);
9598 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9600 ith_elem_is_expansion |=
9601 argument_pack_element_is_expansion_p (arg_pack, index);
9603 /* Select the Ith argument from the pack. */
9604 if (TREE_CODE (parm) == PARM_DECL
9605 || TREE_CODE (parm) == FIELD_DECL)
9607 if (index == 0)
9609 aps = make_argument_pack_select (arg_pack, index);
9610 mark_used (parm);
9611 register_local_specialization (aps, parm);
9613 else
9614 aps = retrieve_local_specialization (parm);
9616 else
9618 int idx, level;
9619 template_parm_level_and_index (parm, &level, &idx);
9621 if (index == 0)
9623 aps = make_argument_pack_select (arg_pack, index);
9624 /* Update the corresponding argument. */
9625 TMPL_ARG (args, level, idx) = aps;
9627 else
9628 /* Re-use the ARGUMENT_PACK_SELECT. */
9629 aps = TMPL_ARG (args, level, idx);
9631 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9634 /* Substitute into the PATTERN with the (possibly altered)
9635 arguments. */
9636 if (pattern == in_decl)
9637 /* Expanding a fixed parameter pack from
9638 coerce_template_parameter_pack. */
9639 t = tsubst_decl (pattern, args, complain);
9640 else if (!TYPE_P (pattern))
9641 t = tsubst_expr (pattern, args, complain, in_decl,
9642 /*integral_constant_expression_p=*/false);
9643 else
9644 t = tsubst (pattern, args, complain, in_decl);
9646 /* If the Ith argument pack element is a pack expansion, then
9647 the Ith element resulting from the substituting is going to
9648 be a pack expansion as well. */
9649 if (ith_elem_is_expansion)
9650 t = make_pack_expansion (t);
9652 return t;
9655 /* Substitute ARGS into T, which is an pack expansion
9656 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9657 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9658 (if only a partial substitution could be performed) or
9659 ERROR_MARK_NODE if there was an error. */
9660 tree
9661 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9662 tree in_decl)
9664 tree pattern;
9665 tree pack, packs = NULL_TREE;
9666 bool unsubstituted_packs = false;
9667 int i, len = -1;
9668 tree result;
9669 struct pointer_map_t *saved_local_specializations = NULL;
9670 bool need_local_specializations = false;
9671 int levels;
9673 gcc_assert (PACK_EXPANSION_P (t));
9674 pattern = PACK_EXPANSION_PATTERN (t);
9676 /* Add in any args remembered from an earlier partial instantiation. */
9677 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9679 levels = TMPL_ARGS_DEPTH (args);
9681 /* Determine the argument packs that will instantiate the parameter
9682 packs used in the expansion expression. While we're at it,
9683 compute the number of arguments to be expanded and make sure it
9684 is consistent. */
9685 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9686 pack = TREE_CHAIN (pack))
9688 tree parm_pack = TREE_VALUE (pack);
9689 tree arg_pack = NULL_TREE;
9690 tree orig_arg = NULL_TREE;
9691 int level = 0;
9693 if (TREE_CODE (parm_pack) == BASES)
9695 if (BASES_DIRECT (parm_pack))
9696 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9697 args, complain, in_decl, false));
9698 else
9699 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9700 args, complain, in_decl, false));
9702 if (TREE_CODE (parm_pack) == PARM_DECL)
9704 if (PACK_EXPANSION_LOCAL_P (t))
9705 arg_pack = retrieve_local_specialization (parm_pack);
9706 else
9708 /* We can't rely on local_specializations for a parameter
9709 name used later in a function declaration (such as in a
9710 late-specified return type). Even if it exists, it might
9711 have the wrong value for a recursive call. Just make a
9712 dummy decl, since it's only used for its type. */
9713 arg_pack = tsubst_decl (parm_pack, args, complain);
9714 if (arg_pack && DECL_PACK_P (arg_pack))
9715 /* Partial instantiation of the parm_pack, we can't build
9716 up an argument pack yet. */
9717 arg_pack = NULL_TREE;
9718 else
9719 arg_pack = make_fnparm_pack (arg_pack);
9720 need_local_specializations = true;
9723 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9724 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9725 else
9727 int idx;
9728 template_parm_level_and_index (parm_pack, &level, &idx);
9730 if (level <= levels)
9731 arg_pack = TMPL_ARG (args, level, idx);
9734 orig_arg = arg_pack;
9735 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9736 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9738 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9739 /* This can only happen if we forget to expand an argument
9740 pack somewhere else. Just return an error, silently. */
9742 result = make_tree_vec (1);
9743 TREE_VEC_ELT (result, 0) = error_mark_node;
9744 return result;
9747 if (arg_pack)
9749 int my_len =
9750 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9752 /* Don't bother trying to do a partial substitution with
9753 incomplete packs; we'll try again after deduction. */
9754 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9755 return t;
9757 if (len < 0)
9758 len = my_len;
9759 else if (len != my_len)
9761 if (!(complain & tf_error))
9762 /* Fail quietly. */;
9763 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9764 error ("mismatched argument pack lengths while expanding "
9765 "%<%T%>",
9766 pattern);
9767 else
9768 error ("mismatched argument pack lengths while expanding "
9769 "%<%E%>",
9770 pattern);
9771 return error_mark_node;
9774 /* Keep track of the parameter packs and their corresponding
9775 argument packs. */
9776 packs = tree_cons (parm_pack, arg_pack, packs);
9777 TREE_TYPE (packs) = orig_arg;
9779 else
9781 /* We can't substitute for this parameter pack. We use a flag as
9782 well as the missing_level counter because function parameter
9783 packs don't have a level. */
9784 unsubstituted_packs = true;
9788 /* We cannot expand this expansion expression, because we don't have
9789 all of the argument packs we need. */
9790 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9792 /* We got some full packs, but we can't substitute them in until we
9793 have values for all the packs. So remember these until then. */
9795 t = make_pack_expansion (pattern);
9796 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9797 return t;
9799 else if (unsubstituted_packs)
9801 /* There were no real arguments, we're just replacing a parameter
9802 pack with another version of itself. Substitute into the
9803 pattern and return a PACK_EXPANSION_*. The caller will need to
9804 deal with that. */
9805 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9806 t = tsubst_expr (pattern, args, complain, in_decl,
9807 /*integral_constant_expression_p=*/false);
9808 else
9809 t = tsubst (pattern, args, complain, in_decl);
9810 t = make_pack_expansion (t);
9811 return t;
9814 gcc_assert (len >= 0);
9816 if (need_local_specializations)
9818 /* We're in a late-specified return type, so create our own local
9819 specializations map; the current map is either NULL or (in the
9820 case of recursive unification) might have bindings that we don't
9821 want to use or alter. */
9822 saved_local_specializations = local_specializations;
9823 local_specializations = pointer_map_create ();
9826 /* For each argument in each argument pack, substitute into the
9827 pattern. */
9828 result = make_tree_vec (len);
9829 for (i = 0; i < len; ++i)
9831 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9833 args, complain,
9834 in_decl);
9835 TREE_VEC_ELT (result, i) = t;
9836 if (t == error_mark_node)
9838 result = error_mark_node;
9839 break;
9843 /* Update ARGS to restore the substitution from parameter packs to
9844 their argument packs. */
9845 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9847 tree parm = TREE_PURPOSE (pack);
9849 if (TREE_CODE (parm) == PARM_DECL
9850 || TREE_CODE (parm) == FIELD_DECL)
9851 register_local_specialization (TREE_TYPE (pack), parm);
9852 else
9854 int idx, level;
9856 if (TREE_VALUE (pack) == NULL_TREE)
9857 continue;
9859 template_parm_level_and_index (parm, &level, &idx);
9861 /* Update the corresponding argument. */
9862 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9863 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9864 TREE_TYPE (pack);
9865 else
9866 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9870 if (need_local_specializations)
9872 pointer_map_destroy (local_specializations);
9873 local_specializations = saved_local_specializations;
9876 return result;
9879 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9880 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9881 parameter packs; all parms generated from a function parameter pack will
9882 have the same DECL_PARM_INDEX. */
9884 tree
9885 get_pattern_parm (tree parm, tree tmpl)
9887 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9888 tree patparm;
9890 if (DECL_ARTIFICIAL (parm))
9892 for (patparm = DECL_ARGUMENTS (pattern);
9893 patparm; patparm = DECL_CHAIN (patparm))
9894 if (DECL_ARTIFICIAL (patparm)
9895 && DECL_NAME (parm) == DECL_NAME (patparm))
9896 break;
9898 else
9900 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9901 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9902 gcc_assert (DECL_PARM_INDEX (patparm)
9903 == DECL_PARM_INDEX (parm));
9906 return patparm;
9909 /* Substitute ARGS into the vector or list of template arguments T. */
9911 static tree
9912 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9914 tree orig_t = t;
9915 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9916 tree *elts;
9918 if (t == error_mark_node)
9919 return error_mark_node;
9921 len = TREE_VEC_LENGTH (t);
9922 elts = XALLOCAVEC (tree, len);
9924 for (i = 0; i < len; i++)
9926 tree orig_arg = TREE_VEC_ELT (t, i);
9927 tree new_arg;
9929 if (TREE_CODE (orig_arg) == TREE_VEC)
9930 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9931 else if (PACK_EXPANSION_P (orig_arg))
9933 /* Substitute into an expansion expression. */
9934 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9936 if (TREE_CODE (new_arg) == TREE_VEC)
9937 /* Add to the expanded length adjustment the number of
9938 expanded arguments. We subtract one from this
9939 measurement, because the argument pack expression
9940 itself is already counted as 1 in
9941 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9942 the argument pack is empty. */
9943 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9945 else if (ARGUMENT_PACK_P (orig_arg))
9947 /* Substitute into each of the arguments. */
9948 new_arg = TYPE_P (orig_arg)
9949 ? cxx_make_type (TREE_CODE (orig_arg))
9950 : make_node (TREE_CODE (orig_arg));
9952 SET_ARGUMENT_PACK_ARGS (
9953 new_arg,
9954 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9955 args, complain, in_decl));
9957 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9958 new_arg = error_mark_node;
9960 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9961 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9962 complain, in_decl);
9963 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9965 if (TREE_TYPE (new_arg) == error_mark_node)
9966 new_arg = error_mark_node;
9969 else
9970 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9972 if (new_arg == error_mark_node)
9973 return error_mark_node;
9975 elts[i] = new_arg;
9976 if (new_arg != orig_arg)
9977 need_new = 1;
9980 if (!need_new)
9981 return t;
9983 /* Make space for the expanded arguments coming from template
9984 argument packs. */
9985 t = make_tree_vec (len + expanded_len_adjust);
9986 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9987 arguments for a member template.
9988 In that case each TREE_VEC in ORIG_T represents a level of template
9989 arguments, and ORIG_T won't carry any non defaulted argument count.
9990 It will rather be the nested TREE_VECs that will carry one.
9991 In other words, ORIG_T carries a non defaulted argument count only
9992 if it doesn't contain any nested TREE_VEC. */
9993 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9995 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9996 count += expanded_len_adjust;
9997 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9999 for (i = 0, out = 0; i < len; i++)
10001 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
10002 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
10003 && TREE_CODE (elts[i]) == TREE_VEC)
10005 int idx;
10007 /* Now expand the template argument pack "in place". */
10008 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
10009 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
10011 else
10013 TREE_VEC_ELT (t, out) = elts[i];
10014 out++;
10018 return t;
10021 /* Return the result of substituting ARGS into the template parameters
10022 given by PARMS. If there are m levels of ARGS and m + n levels of
10023 PARMS, then the result will contain n levels of PARMS. For
10024 example, if PARMS is `template <class T> template <class U>
10025 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10026 result will be `template <int*, double, class V>'. */
10028 static tree
10029 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
10031 tree r = NULL_TREE;
10032 tree* new_parms;
10034 /* When substituting into a template, we must set
10035 PROCESSING_TEMPLATE_DECL as the template parameters may be
10036 dependent if they are based on one-another, and the dependency
10037 predicates are short-circuit outside of templates. */
10038 ++processing_template_decl;
10040 for (new_parms = &r;
10041 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
10042 new_parms = &(TREE_CHAIN (*new_parms)),
10043 parms = TREE_CHAIN (parms))
10045 tree new_vec =
10046 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
10047 int i;
10049 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
10051 tree tuple;
10053 if (parms == error_mark_node)
10054 continue;
10056 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
10058 if (tuple == error_mark_node)
10059 continue;
10061 TREE_VEC_ELT (new_vec, i) =
10062 tsubst_template_parm (tuple, args, complain);
10065 *new_parms =
10066 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
10067 - TMPL_ARGS_DEPTH (args)),
10068 new_vec, NULL_TREE);
10071 --processing_template_decl;
10073 return r;
10076 /* Return the result of substituting ARGS into one template parameter
10077 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10078 parameter and which TREE_PURPOSE is the default argument of the
10079 template parameter. */
10081 static tree
10082 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
10084 tree default_value, parm_decl;
10086 if (args == NULL_TREE
10087 || t == NULL_TREE
10088 || t == error_mark_node)
10089 return t;
10091 gcc_assert (TREE_CODE (t) == TREE_LIST);
10093 default_value = TREE_PURPOSE (t);
10094 parm_decl = TREE_VALUE (t);
10096 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
10097 if (TREE_CODE (parm_decl) == PARM_DECL
10098 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
10099 parm_decl = error_mark_node;
10100 default_value = tsubst_template_arg (default_value, args,
10101 complain, NULL_TREE);
10103 return build_tree_list (default_value, parm_decl);
10106 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10107 type T. If T is not an aggregate or enumeration type, it is
10108 handled as if by tsubst. IN_DECL is as for tsubst. If
10109 ENTERING_SCOPE is nonzero, T is the context for a template which
10110 we are presently tsubst'ing. Return the substituted value. */
10112 static tree
10113 tsubst_aggr_type (tree t,
10114 tree args,
10115 tsubst_flags_t complain,
10116 tree in_decl,
10117 int entering_scope)
10119 if (t == NULL_TREE)
10120 return NULL_TREE;
10122 switch (TREE_CODE (t))
10124 case RECORD_TYPE:
10125 if (TYPE_PTRMEMFUNC_P (t))
10126 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
10128 /* Else fall through. */
10129 case ENUMERAL_TYPE:
10130 case UNION_TYPE:
10131 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
10133 tree argvec;
10134 tree context;
10135 tree r;
10136 int saved_unevaluated_operand;
10137 int saved_inhibit_evaluation_warnings;
10139 /* In "sizeof(X<I>)" we need to evaluate "I". */
10140 saved_unevaluated_operand = cp_unevaluated_operand;
10141 cp_unevaluated_operand = 0;
10142 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10143 c_inhibit_evaluation_warnings = 0;
10145 /* First, determine the context for the type we are looking
10146 up. */
10147 context = TYPE_CONTEXT (t);
10148 if (context && TYPE_P (context))
10150 context = tsubst_aggr_type (context, args, complain,
10151 in_decl, /*entering_scope=*/1);
10152 /* If context is a nested class inside a class template,
10153 it may still need to be instantiated (c++/33959). */
10154 context = complete_type (context);
10157 /* Then, figure out what arguments are appropriate for the
10158 type we are trying to find. For example, given:
10160 template <class T> struct S;
10161 template <class T, class U> void f(T, U) { S<U> su; }
10163 and supposing that we are instantiating f<int, double>,
10164 then our ARGS will be {int, double}, but, when looking up
10165 S we only want {double}. */
10166 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10167 complain, in_decl);
10168 if (argvec == error_mark_node)
10169 r = error_mark_node;
10170 else
10172 r = lookup_template_class (t, argvec, in_decl, context,
10173 entering_scope, complain);
10174 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10177 cp_unevaluated_operand = saved_unevaluated_operand;
10178 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10180 return r;
10182 else
10183 /* This is not a template type, so there's nothing to do. */
10184 return t;
10186 default:
10187 return tsubst (t, args, complain, in_decl);
10191 /* Substitute into the default argument ARG (a default argument for
10192 FN), which has the indicated TYPE. */
10194 tree
10195 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10197 tree saved_class_ptr = NULL_TREE;
10198 tree saved_class_ref = NULL_TREE;
10199 int errs = errorcount + sorrycount;
10201 /* This can happen in invalid code. */
10202 if (TREE_CODE (arg) == DEFAULT_ARG)
10203 return arg;
10205 /* This default argument came from a template. Instantiate the
10206 default argument here, not in tsubst. In the case of
10207 something like:
10209 template <class T>
10210 struct S {
10211 static T t();
10212 void f(T = t());
10215 we must be careful to do name lookup in the scope of S<T>,
10216 rather than in the current class. */
10217 push_access_scope (fn);
10218 /* The "this" pointer is not valid in a default argument. */
10219 if (cfun)
10221 saved_class_ptr = current_class_ptr;
10222 cp_function_chain->x_current_class_ptr = NULL_TREE;
10223 saved_class_ref = current_class_ref;
10224 cp_function_chain->x_current_class_ref = NULL_TREE;
10227 push_deferring_access_checks(dk_no_deferred);
10228 /* The default argument expression may cause implicitly defined
10229 member functions to be synthesized, which will result in garbage
10230 collection. We must treat this situation as if we were within
10231 the body of function so as to avoid collecting live data on the
10232 stack. */
10233 ++function_depth;
10234 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10235 complain, NULL_TREE,
10236 /*integral_constant_expression_p=*/false);
10237 --function_depth;
10238 pop_deferring_access_checks();
10240 /* Restore the "this" pointer. */
10241 if (cfun)
10243 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10244 cp_function_chain->x_current_class_ref = saved_class_ref;
10247 if (errorcount+sorrycount > errs
10248 && (complain & tf_warning_or_error))
10249 inform (input_location,
10250 " when instantiating default argument for call to %D", fn);
10252 /* Make sure the default argument is reasonable. */
10253 arg = check_default_argument (type, arg, complain);
10255 pop_access_scope (fn);
10257 return arg;
10260 /* Substitute into all the default arguments for FN. */
10262 static void
10263 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10265 tree arg;
10266 tree tmpl_args;
10268 tmpl_args = DECL_TI_ARGS (fn);
10270 /* If this function is not yet instantiated, we certainly don't need
10271 its default arguments. */
10272 if (uses_template_parms (tmpl_args))
10273 return;
10274 /* Don't do this again for clones. */
10275 if (DECL_CLONED_FUNCTION_P (fn))
10276 return;
10278 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10279 arg;
10280 arg = TREE_CHAIN (arg))
10281 if (TREE_PURPOSE (arg))
10282 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10283 TREE_VALUE (arg),
10284 TREE_PURPOSE (arg),
10285 complain);
10288 /* Substitute the ARGS into the T, which is a _DECL. Return the
10289 result of the substitution. Issue error and warning messages under
10290 control of COMPLAIN. */
10292 static tree
10293 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10295 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10296 location_t saved_loc;
10297 tree r = NULL_TREE;
10298 tree in_decl = t;
10299 hashval_t hash = 0;
10301 /* Set the filename and linenumber to improve error-reporting. */
10302 saved_loc = input_location;
10303 input_location = DECL_SOURCE_LOCATION (t);
10305 switch (TREE_CODE (t))
10307 case TEMPLATE_DECL:
10309 /* We can get here when processing a member function template,
10310 member class template, or template template parameter. */
10311 tree decl = DECL_TEMPLATE_RESULT (t);
10312 tree spec;
10313 tree tmpl_args;
10314 tree full_args;
10316 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10318 /* Template template parameter is treated here. */
10319 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10320 if (new_type == error_mark_node)
10321 RETURN (error_mark_node);
10322 /* If we get a real template back, return it. This can happen in
10323 the context of most_specialized_class. */
10324 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10325 return new_type;
10327 r = copy_decl (t);
10328 DECL_CHAIN (r) = NULL_TREE;
10329 TREE_TYPE (r) = new_type;
10330 DECL_TEMPLATE_RESULT (r)
10331 = build_decl (DECL_SOURCE_LOCATION (decl),
10332 TYPE_DECL, DECL_NAME (decl), new_type);
10333 DECL_TEMPLATE_PARMS (r)
10334 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10335 complain);
10336 TYPE_NAME (new_type) = r;
10337 break;
10340 /* We might already have an instance of this template.
10341 The ARGS are for the surrounding class type, so the
10342 full args contain the tsubst'd args for the context,
10343 plus the innermost args from the template decl. */
10344 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10345 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10346 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10347 /* Because this is a template, the arguments will still be
10348 dependent, even after substitution. If
10349 PROCESSING_TEMPLATE_DECL is not set, the dependency
10350 predicates will short-circuit. */
10351 ++processing_template_decl;
10352 full_args = tsubst_template_args (tmpl_args, args,
10353 complain, in_decl);
10354 --processing_template_decl;
10355 if (full_args == error_mark_node)
10356 RETURN (error_mark_node);
10358 /* If this is a default template template argument,
10359 tsubst might not have changed anything. */
10360 if (full_args == tmpl_args)
10361 RETURN (t);
10363 hash = hash_tmpl_and_args (t, full_args);
10364 spec = retrieve_specialization (t, full_args, hash);
10365 if (spec != NULL_TREE)
10367 r = spec;
10368 break;
10371 /* Make a new template decl. It will be similar to the
10372 original, but will record the current template arguments.
10373 We also create a new function declaration, which is just
10374 like the old one, but points to this new template, rather
10375 than the old one. */
10376 r = copy_decl (t);
10377 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10378 DECL_CHAIN (r) = NULL_TREE;
10380 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10382 if (TREE_CODE (decl) == TYPE_DECL
10383 && !TYPE_DECL_ALIAS_P (decl))
10385 tree new_type;
10386 ++processing_template_decl;
10387 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10388 --processing_template_decl;
10389 if (new_type == error_mark_node)
10390 RETURN (error_mark_node);
10392 TREE_TYPE (r) = new_type;
10393 /* For a partial specialization, we need to keep pointing to
10394 the primary template. */
10395 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10396 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10397 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10398 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10399 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10401 else
10403 tree new_decl;
10404 ++processing_template_decl;
10405 new_decl = tsubst (decl, args, complain, in_decl);
10406 --processing_template_decl;
10407 if (new_decl == error_mark_node)
10408 RETURN (error_mark_node);
10410 DECL_TEMPLATE_RESULT (r) = new_decl;
10411 DECL_TI_TEMPLATE (new_decl) = r;
10412 TREE_TYPE (r) = TREE_TYPE (new_decl);
10413 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10414 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10417 SET_DECL_IMPLICIT_INSTANTIATION (r);
10418 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10419 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10421 /* The template parameters for this new template are all the
10422 template parameters for the old template, except the
10423 outermost level of parameters. */
10424 DECL_TEMPLATE_PARMS (r)
10425 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10426 complain);
10428 if (PRIMARY_TEMPLATE_P (t))
10429 DECL_PRIMARY_TEMPLATE (r) = r;
10431 if (TREE_CODE (decl) != TYPE_DECL)
10432 /* Record this non-type partial instantiation. */
10433 register_specialization (r, t,
10434 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10435 false, hash);
10437 break;
10439 case FUNCTION_DECL:
10441 tree ctx;
10442 tree argvec = NULL_TREE;
10443 tree *friends;
10444 tree gen_tmpl;
10445 tree type;
10446 int member;
10447 int args_depth;
10448 int parms_depth;
10450 /* Nobody should be tsubst'ing into non-template functions. */
10451 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10453 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10455 tree spec;
10456 bool dependent_p;
10458 /* If T is not dependent, just return it. We have to
10459 increment PROCESSING_TEMPLATE_DECL because
10460 value_dependent_expression_p assumes that nothing is
10461 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10462 ++processing_template_decl;
10463 dependent_p = value_dependent_expression_p (t);
10464 --processing_template_decl;
10465 if (!dependent_p)
10466 RETURN (t);
10468 /* Calculate the most general template of which R is a
10469 specialization, and the complete set of arguments used to
10470 specialize R. */
10471 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10472 argvec = tsubst_template_args (DECL_TI_ARGS
10473 (DECL_TEMPLATE_RESULT
10474 (DECL_TI_TEMPLATE (t))),
10475 args, complain, in_decl);
10476 if (argvec == error_mark_node)
10477 RETURN (error_mark_node);
10479 /* Check to see if we already have this specialization. */
10480 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10481 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10483 if (spec)
10485 r = spec;
10486 break;
10489 /* We can see more levels of arguments than parameters if
10490 there was a specialization of a member template, like
10491 this:
10493 template <class T> struct S { template <class U> void f(); }
10494 template <> template <class U> void S<int>::f(U);
10496 Here, we'll be substituting into the specialization,
10497 because that's where we can find the code we actually
10498 want to generate, but we'll have enough arguments for
10499 the most general template.
10501 We also deal with the peculiar case:
10503 template <class T> struct S {
10504 template <class U> friend void f();
10506 template <class U> void f() {}
10507 template S<int>;
10508 template void f<double>();
10510 Here, the ARGS for the instantiation of will be {int,
10511 double}. But, we only need as many ARGS as there are
10512 levels of template parameters in CODE_PATTERN. We are
10513 careful not to get fooled into reducing the ARGS in
10514 situations like:
10516 template <class T> struct S { template <class U> void f(U); }
10517 template <class T> template <> void S<T>::f(int) {}
10519 which we can spot because the pattern will be a
10520 specialization in this case. */
10521 args_depth = TMPL_ARGS_DEPTH (args);
10522 parms_depth =
10523 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10524 if (args_depth > parms_depth
10525 && !DECL_TEMPLATE_SPECIALIZATION (t))
10526 args = get_innermost_template_args (args, parms_depth);
10528 else
10530 /* This special case arises when we have something like this:
10532 template <class T> struct S {
10533 friend void f<int>(int, double);
10536 Here, the DECL_TI_TEMPLATE for the friend declaration
10537 will be an IDENTIFIER_NODE. We are being called from
10538 tsubst_friend_function, and we want only to create a
10539 new decl (R) with appropriate types so that we can call
10540 determine_specialization. */
10541 gen_tmpl = NULL_TREE;
10544 if (DECL_CLASS_SCOPE_P (t))
10546 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10547 member = 2;
10548 else
10549 member = 1;
10550 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10551 complain, t, /*entering_scope=*/1);
10553 else
10555 member = 0;
10556 ctx = DECL_CONTEXT (t);
10558 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10559 if (type == error_mark_node)
10560 RETURN (error_mark_node);
10562 /* If we hit excessive deduction depth, the type is bogus even if
10563 it isn't error_mark_node, so don't build a decl. */
10564 if (excessive_deduction_depth)
10565 RETURN (error_mark_node);
10567 /* We do NOT check for matching decls pushed separately at this
10568 point, as they may not represent instantiations of this
10569 template, and in any case are considered separate under the
10570 discrete model. */
10571 r = copy_decl (t);
10572 DECL_USE_TEMPLATE (r) = 0;
10573 TREE_TYPE (r) = type;
10574 /* Clear out the mangled name and RTL for the instantiation. */
10575 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10576 SET_DECL_RTL (r, NULL);
10577 /* Leave DECL_INITIAL set on deleted instantiations. */
10578 if (!DECL_DELETED_FN (r))
10579 DECL_INITIAL (r) = NULL_TREE;
10580 DECL_CONTEXT (r) = ctx;
10582 /* OpenMP UDRs have the only argument a reference to the declared
10583 type. We want to diagnose if the declared type is a reference,
10584 which is invalid, but as references to references are usually
10585 quietly merged, diagnose it here. */
10586 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10588 tree argtype
10589 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10590 argtype = tsubst (argtype, args, complain, in_decl);
10591 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10592 error_at (DECL_SOURCE_LOCATION (t),
10593 "reference type %qT in "
10594 "%<#pragma omp declare reduction%>", argtype);
10595 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10596 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10597 argtype);
10600 if (member && DECL_CONV_FN_P (r))
10601 /* Type-conversion operator. Reconstruct the name, in
10602 case it's the name of one of the template's parameters. */
10603 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10605 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10606 complain, t);
10607 DECL_RESULT (r) = NULL_TREE;
10609 TREE_STATIC (r) = 0;
10610 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10611 DECL_EXTERNAL (r) = 1;
10612 /* If this is an instantiation of a function with internal
10613 linkage, we already know what object file linkage will be
10614 assigned to the instantiation. */
10615 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10616 DECL_DEFER_OUTPUT (r) = 0;
10617 DECL_CHAIN (r) = NULL_TREE;
10618 DECL_PENDING_INLINE_INFO (r) = 0;
10619 DECL_PENDING_INLINE_P (r) = 0;
10620 DECL_SAVED_TREE (r) = NULL_TREE;
10621 DECL_STRUCT_FUNCTION (r) = NULL;
10622 TREE_USED (r) = 0;
10623 /* We'll re-clone as appropriate in instantiate_template. */
10624 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10626 /* If we aren't complaining now, return on error before we register
10627 the specialization so that we'll complain eventually. */
10628 if ((complain & tf_error) == 0
10629 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10630 && !grok_op_properties (r, /*complain=*/false))
10631 RETURN (error_mark_node);
10633 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10634 this in the special friend case mentioned above where
10635 GEN_TMPL is NULL. */
10636 if (gen_tmpl)
10638 DECL_TEMPLATE_INFO (r)
10639 = build_template_info (gen_tmpl, argvec);
10640 SET_DECL_IMPLICIT_INSTANTIATION (r);
10642 tree new_r
10643 = register_specialization (r, gen_tmpl, argvec, false, hash);
10644 if (new_r != r)
10645 /* We instantiated this while substituting into
10646 the type earlier (template/friend54.C). */
10647 RETURN (new_r);
10649 /* We're not supposed to instantiate default arguments
10650 until they are called, for a template. But, for a
10651 declaration like:
10653 template <class T> void f ()
10654 { extern void g(int i = T()); }
10656 we should do the substitution when the template is
10657 instantiated. We handle the member function case in
10658 instantiate_class_template since the default arguments
10659 might refer to other members of the class. */
10660 if (!member
10661 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10662 && !uses_template_parms (argvec))
10663 tsubst_default_arguments (r, complain);
10665 else
10666 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10668 /* Copy the list of befriending classes. */
10669 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10670 *friends;
10671 friends = &TREE_CHAIN (*friends))
10673 *friends = copy_node (*friends);
10674 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10675 args, complain,
10676 in_decl);
10679 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10681 maybe_retrofit_in_chrg (r);
10682 if (DECL_CONSTRUCTOR_P (r))
10683 grok_ctor_properties (ctx, r);
10684 if (DECL_INHERITED_CTOR_BASE (r))
10685 deduce_inheriting_ctor (r);
10686 /* If this is an instantiation of a member template, clone it.
10687 If it isn't, that'll be handled by
10688 clone_constructors_and_destructors. */
10689 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10690 clone_function_decl (r, /*update_method_vec_p=*/0);
10692 else if ((complain & tf_error) != 0
10693 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10694 && !grok_op_properties (r, /*complain=*/true))
10695 RETURN (error_mark_node);
10697 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10698 SET_DECL_FRIEND_CONTEXT (r,
10699 tsubst (DECL_FRIEND_CONTEXT (t),
10700 args, complain, in_decl));
10702 /* Possibly limit visibility based on template args. */
10703 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10704 if (DECL_VISIBILITY_SPECIFIED (t))
10706 DECL_VISIBILITY_SPECIFIED (r) = 0;
10707 DECL_ATTRIBUTES (r)
10708 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10710 determine_visibility (r);
10711 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10712 && !processing_template_decl)
10713 defaulted_late_check (r);
10715 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10716 args, complain, in_decl);
10718 break;
10720 case PARM_DECL:
10722 tree type = NULL_TREE;
10723 int i, len = 1;
10724 tree expanded_types = NULL_TREE;
10725 tree prev_r = NULL_TREE;
10726 tree first_r = NULL_TREE;
10728 if (DECL_PACK_P (t))
10730 /* If there is a local specialization that isn't a
10731 parameter pack, it means that we're doing a "simple"
10732 substitution from inside tsubst_pack_expansion. Just
10733 return the local specialization (which will be a single
10734 parm). */
10735 tree spec = retrieve_local_specialization (t);
10736 if (spec
10737 && TREE_CODE (spec) == PARM_DECL
10738 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10739 RETURN (spec);
10741 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10742 the parameters in this function parameter pack. */
10743 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10744 complain, in_decl);
10745 if (TREE_CODE (expanded_types) == TREE_VEC)
10747 len = TREE_VEC_LENGTH (expanded_types);
10749 /* Zero-length parameter packs are boring. Just substitute
10750 into the chain. */
10751 if (len == 0)
10752 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10753 TREE_CHAIN (t)));
10755 else
10757 /* All we did was update the type. Make a note of that. */
10758 type = expanded_types;
10759 expanded_types = NULL_TREE;
10763 /* Loop through all of the parameters we'll build. When T is
10764 a function parameter pack, LEN is the number of expanded
10765 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10766 r = NULL_TREE;
10767 for (i = 0; i < len; ++i)
10769 prev_r = r;
10770 r = copy_node (t);
10771 if (DECL_TEMPLATE_PARM_P (t))
10772 SET_DECL_TEMPLATE_PARM_P (r);
10774 if (expanded_types)
10775 /* We're on the Ith parameter of the function parameter
10776 pack. */
10778 /* Get the Ith type. */
10779 type = TREE_VEC_ELT (expanded_types, i);
10781 /* Rename the parameter to include the index. */
10782 DECL_NAME (r)
10783 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10785 else if (!type)
10786 /* We're dealing with a normal parameter. */
10787 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10789 type = type_decays_to (type);
10790 TREE_TYPE (r) = type;
10791 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10793 if (DECL_INITIAL (r))
10795 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10796 DECL_INITIAL (r) = TREE_TYPE (r);
10797 else
10798 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10799 complain, in_decl);
10802 DECL_CONTEXT (r) = NULL_TREE;
10804 if (!DECL_TEMPLATE_PARM_P (r))
10805 DECL_ARG_TYPE (r) = type_passed_as (type);
10807 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10808 args, complain, in_decl);
10810 /* Keep track of the first new parameter we
10811 generate. That's what will be returned to the
10812 caller. */
10813 if (!first_r)
10814 first_r = r;
10816 /* Build a proper chain of parameters when substituting
10817 into a function parameter pack. */
10818 if (prev_r)
10819 DECL_CHAIN (prev_r) = r;
10822 /* If cp_unevaluated_operand is set, we're just looking for a
10823 single dummy parameter, so don't keep going. */
10824 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10825 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10826 complain, DECL_CHAIN (t));
10828 /* FIRST_R contains the start of the chain we've built. */
10829 r = first_r;
10831 break;
10833 case FIELD_DECL:
10835 tree type = NULL_TREE;
10836 tree vec = NULL_TREE;
10837 tree expanded_types = NULL_TREE;
10838 int len = 1;
10840 if (PACK_EXPANSION_P (TREE_TYPE (t)))
10842 /* This field is a lambda capture pack. Return a TREE_VEC of
10843 the expanded fields to instantiate_class_template_1 and
10844 store them in the specializations hash table as a
10845 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10846 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10847 complain, in_decl);
10848 if (TREE_CODE (expanded_types) == TREE_VEC)
10850 len = TREE_VEC_LENGTH (expanded_types);
10851 vec = make_tree_vec (len);
10853 else
10855 /* All we did was update the type. Make a note of that. */
10856 type = expanded_types;
10857 expanded_types = NULL_TREE;
10861 for (int i = 0; i < len; ++i)
10863 r = copy_decl (t);
10864 if (expanded_types)
10866 type = TREE_VEC_ELT (expanded_types, i);
10867 DECL_NAME (r)
10868 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10870 else if (!type)
10871 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10873 if (type == error_mark_node)
10874 RETURN (error_mark_node);
10875 TREE_TYPE (r) = type;
10876 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10878 if (DECL_C_BIT_FIELD (r))
10879 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10880 non-bit-fields DECL_INITIAL is a non-static data member
10881 initializer, which gets deferred instantiation. */
10882 DECL_INITIAL (r)
10883 = tsubst_expr (DECL_INITIAL (t), args,
10884 complain, in_decl,
10885 /*integral_constant_expression_p=*/true);
10886 else if (DECL_INITIAL (t))
10888 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10889 NSDMI in perform_member_init. Still set DECL_INITIAL
10890 so that we know there is one. */
10891 DECL_INITIAL (r) = void_zero_node;
10892 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10893 retrofit_lang_decl (r);
10894 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10896 /* We don't have to set DECL_CONTEXT here; it is set by
10897 finish_member_declaration. */
10898 DECL_CHAIN (r) = NULL_TREE;
10900 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10901 args, complain, in_decl);
10903 if (vec)
10904 TREE_VEC_ELT (vec, i) = r;
10907 if (vec)
10909 r = vec;
10910 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
10911 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
10912 SET_ARGUMENT_PACK_ARGS (pack, vec);
10913 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
10914 TREE_TYPE (pack) = tpack;
10915 register_specialization (pack, t, args, false, 0);
10918 break;
10920 case USING_DECL:
10921 /* We reach here only for member using decls. We also need to check
10922 uses_template_parms because DECL_DEPENDENT_P is not set for a
10923 using-declaration that designates a member of the current
10924 instantiation (c++/53549). */
10925 if (DECL_DEPENDENT_P (t)
10926 || uses_template_parms (USING_DECL_SCOPE (t)))
10928 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10929 complain, in_decl);
10930 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10931 r = do_class_using_decl (inst_scope, name);
10932 if (!r)
10933 r = error_mark_node;
10934 else
10936 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10937 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10940 else
10942 r = copy_node (t);
10943 DECL_CHAIN (r) = NULL_TREE;
10945 break;
10947 case TYPE_DECL:
10948 case VAR_DECL:
10950 tree argvec = NULL_TREE;
10951 tree gen_tmpl = NULL_TREE;
10952 tree spec;
10953 tree tmpl = NULL_TREE;
10954 tree ctx;
10955 tree type = NULL_TREE;
10956 bool local_p;
10958 if (TREE_TYPE (t) == error_mark_node)
10959 RETURN (error_mark_node);
10961 if (TREE_CODE (t) == TYPE_DECL
10962 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10964 /* If this is the canonical decl, we don't have to
10965 mess with instantiations, and often we can't (for
10966 typename, template type parms and such). Note that
10967 TYPE_NAME is not correct for the above test if
10968 we've copied the type for a typedef. */
10969 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10970 if (type == error_mark_node)
10971 RETURN (error_mark_node);
10972 r = TYPE_NAME (type);
10973 break;
10976 /* Check to see if we already have the specialization we
10977 need. */
10978 spec = NULL_TREE;
10979 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10981 /* T is a static data member or namespace-scope entity.
10982 We have to substitute into namespace-scope variables
10983 (even though such entities are never templates) because
10984 of cases like:
10986 template <class T> void f() { extern T t; }
10988 where the entity referenced is not known until
10989 instantiation time. */
10990 local_p = false;
10991 ctx = DECL_CONTEXT (t);
10992 if (DECL_CLASS_SCOPE_P (t))
10994 ctx = tsubst_aggr_type (ctx, args,
10995 complain,
10996 in_decl, /*entering_scope=*/1);
10997 /* If CTX is unchanged, then T is in fact the
10998 specialization we want. That situation occurs when
10999 referencing a static data member within in its own
11000 class. We can use pointer equality, rather than
11001 same_type_p, because DECL_CONTEXT is always
11002 canonical... */
11003 if (ctx == DECL_CONTEXT (t)
11004 && (TREE_CODE (t) != TYPE_DECL
11005 /* ... unless T is a member template; in which
11006 case our caller can be willing to create a
11007 specialization of that template represented
11008 by T. */
11009 || !(DECL_TI_TEMPLATE (t)
11010 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
11011 spec = t;
11014 if (!spec)
11016 tmpl = DECL_TI_TEMPLATE (t);
11017 gen_tmpl = most_general_template (tmpl);
11018 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
11019 if (argvec == error_mark_node)
11020 RETURN (error_mark_node);
11021 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11022 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11025 else
11027 /* A local variable. */
11028 local_p = true;
11029 /* Subsequent calls to pushdecl will fill this in. */
11030 ctx = NULL_TREE;
11031 spec = retrieve_local_specialization (t);
11033 /* If we already have the specialization we need, there is
11034 nothing more to do. */
11035 if (spec)
11037 r = spec;
11038 break;
11041 /* Create a new node for the specialization we need. */
11042 r = copy_decl (t);
11043 if (type == NULL_TREE)
11045 if (is_typedef_decl (t))
11046 type = DECL_ORIGINAL_TYPE (t);
11047 else
11048 type = TREE_TYPE (t);
11049 if (VAR_P (t)
11050 && VAR_HAD_UNKNOWN_BOUND (t)
11051 && type != error_mark_node)
11052 type = strip_array_domain (type);
11053 type = tsubst (type, args, complain, in_decl);
11055 if (VAR_P (r))
11057 /* Even if the original location is out of scope, the
11058 newly substituted one is not. */
11059 DECL_DEAD_FOR_LOCAL (r) = 0;
11060 DECL_INITIALIZED_P (r) = 0;
11061 DECL_TEMPLATE_INSTANTIATED (r) = 0;
11062 if (type == error_mark_node)
11063 RETURN (error_mark_node);
11064 if (TREE_CODE (type) == FUNCTION_TYPE)
11066 /* It may seem that this case cannot occur, since:
11068 typedef void f();
11069 void g() { f x; }
11071 declares a function, not a variable. However:
11073 typedef void f();
11074 template <typename T> void g() { T t; }
11075 template void g<f>();
11077 is an attempt to declare a variable with function
11078 type. */
11079 error ("variable %qD has function type",
11080 /* R is not yet sufficiently initialized, so we
11081 just use its name. */
11082 DECL_NAME (r));
11083 RETURN (error_mark_node);
11085 type = complete_type (type);
11086 /* Wait until cp_finish_decl to set this again, to handle
11087 circular dependency (template/instantiate6.C). */
11088 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
11089 type = check_var_type (DECL_NAME (r), type);
11091 if (DECL_HAS_VALUE_EXPR_P (t))
11093 tree ve = DECL_VALUE_EXPR (t);
11094 ve = tsubst_expr (ve, args, complain, in_decl,
11095 /*constant_expression_p=*/false);
11096 if (REFERENCE_REF_P (ve))
11098 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
11099 ve = TREE_OPERAND (ve, 0);
11101 SET_DECL_VALUE_EXPR (r, ve);
11104 else if (DECL_SELF_REFERENCE_P (t))
11105 SET_DECL_SELF_REFERENCE_P (r);
11106 TREE_TYPE (r) = type;
11107 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11108 DECL_CONTEXT (r) = ctx;
11109 /* Clear out the mangled name and RTL for the instantiation. */
11110 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11111 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11112 SET_DECL_RTL (r, NULL);
11113 /* The initializer must not be expanded until it is required;
11114 see [temp.inst]. */
11115 DECL_INITIAL (r) = NULL_TREE;
11116 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11117 SET_DECL_RTL (r, NULL);
11118 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
11119 if (VAR_P (r))
11121 /* Possibly limit visibility based on template args. */
11122 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11123 if (DECL_VISIBILITY_SPECIFIED (t))
11125 DECL_VISIBILITY_SPECIFIED (r) = 0;
11126 DECL_ATTRIBUTES (r)
11127 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11129 determine_visibility (r);
11132 if (!local_p)
11134 /* A static data member declaration is always marked
11135 external when it is declared in-class, even if an
11136 initializer is present. We mimic the non-template
11137 processing here. */
11138 DECL_EXTERNAL (r) = 1;
11140 register_specialization (r, gen_tmpl, argvec, false, hash);
11141 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
11142 SET_DECL_IMPLICIT_INSTANTIATION (r);
11144 else if (!cp_unevaluated_operand)
11145 register_local_specialization (r, t);
11147 DECL_CHAIN (r) = NULL_TREE;
11149 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
11150 /*flags=*/0,
11151 args, complain, in_decl);
11153 /* Preserve a typedef that names a type. */
11154 if (is_typedef_decl (r))
11156 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
11157 set_underlying_type (r);
11160 layout_decl (r, 0);
11162 break;
11164 default:
11165 gcc_unreachable ();
11167 #undef RETURN
11169 out:
11170 /* Restore the file and line information. */
11171 input_location = saved_loc;
11173 return r;
11176 /* Substitute into the ARG_TYPES of a function type.
11177 If END is a TREE_CHAIN, leave it and any following types
11178 un-substituted. */
11180 static tree
11181 tsubst_arg_types (tree arg_types,
11182 tree args,
11183 tree end,
11184 tsubst_flags_t complain,
11185 tree in_decl)
11187 tree remaining_arg_types;
11188 tree type = NULL_TREE;
11189 int i = 1;
11190 tree expanded_args = NULL_TREE;
11191 tree default_arg;
11193 if (!arg_types || arg_types == void_list_node || arg_types == end)
11194 return arg_types;
11196 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11197 args, end, complain, in_decl);
11198 if (remaining_arg_types == error_mark_node)
11199 return error_mark_node;
11201 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11203 /* For a pack expansion, perform substitution on the
11204 entire expression. Later on, we'll handle the arguments
11205 one-by-one. */
11206 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11207 args, complain, in_decl);
11209 if (TREE_CODE (expanded_args) == TREE_VEC)
11210 /* So that we'll spin through the parameters, one by one. */
11211 i = TREE_VEC_LENGTH (expanded_args);
11212 else
11214 /* We only partially substituted into the parameter
11215 pack. Our type is TYPE_PACK_EXPANSION. */
11216 type = expanded_args;
11217 expanded_args = NULL_TREE;
11221 while (i > 0) {
11222 --i;
11224 if (expanded_args)
11225 type = TREE_VEC_ELT (expanded_args, i);
11226 else if (!type)
11227 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11229 if (type == error_mark_node)
11230 return error_mark_node;
11231 if (VOID_TYPE_P (type))
11233 if (complain & tf_error)
11235 error ("invalid parameter type %qT", type);
11236 if (in_decl)
11237 error ("in declaration %q+D", in_decl);
11239 return error_mark_node;
11241 /* DR 657. */
11242 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11243 return error_mark_node;
11245 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11246 top-level qualifiers as required. */
11247 type = cv_unqualified (type_decays_to (type));
11249 /* We do not substitute into default arguments here. The standard
11250 mandates that they be instantiated only when needed, which is
11251 done in build_over_call. */
11252 default_arg = TREE_PURPOSE (arg_types);
11254 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11256 /* We've instantiated a template before its default arguments
11257 have been parsed. This can happen for a nested template
11258 class, and is not an error unless we require the default
11259 argument in a call of this function. */
11260 remaining_arg_types =
11261 tree_cons (default_arg, type, remaining_arg_types);
11262 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11264 else
11265 remaining_arg_types =
11266 hash_tree_cons (default_arg, type, remaining_arg_types);
11269 return remaining_arg_types;
11272 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11273 *not* handle the exception-specification for FNTYPE, because the
11274 initial substitution of explicitly provided template parameters
11275 during argument deduction forbids substitution into the
11276 exception-specification:
11278 [temp.deduct]
11280 All references in the function type of the function template to the
11281 corresponding template parameters are replaced by the specified tem-
11282 plate argument values. If a substitution in a template parameter or
11283 in the function type of the function template results in an invalid
11284 type, type deduction fails. [Note: The equivalent substitution in
11285 exception specifications is done only when the function is instanti-
11286 ated, at which point a program is ill-formed if the substitution
11287 results in an invalid type.] */
11289 static tree
11290 tsubst_function_type (tree t,
11291 tree args,
11292 tsubst_flags_t complain,
11293 tree in_decl)
11295 tree return_type;
11296 tree arg_types;
11297 tree fntype;
11299 /* The TYPE_CONTEXT is not used for function/method types. */
11300 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11302 /* Substitute the return type. */
11303 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11304 if (return_type == error_mark_node)
11305 return error_mark_node;
11306 /* DR 486 clarifies that creation of a function type with an
11307 invalid return type is a deduction failure. */
11308 if (TREE_CODE (return_type) == ARRAY_TYPE
11309 || TREE_CODE (return_type) == FUNCTION_TYPE)
11311 if (complain & tf_error)
11313 if (TREE_CODE (return_type) == ARRAY_TYPE)
11314 error ("function returning an array");
11315 else
11316 error ("function returning a function");
11318 return error_mark_node;
11320 /* And DR 657. */
11321 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11322 return error_mark_node;
11324 /* Substitute the argument types. */
11325 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11326 complain, in_decl);
11327 if (arg_types == error_mark_node)
11328 return error_mark_node;
11330 /* Construct a new type node and return it. */
11331 if (TREE_CODE (t) == FUNCTION_TYPE)
11333 fntype = build_function_type (return_type, arg_types);
11334 fntype = apply_memfn_quals (fntype,
11335 type_memfn_quals (t),
11336 type_memfn_rqual (t));
11338 else
11340 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11341 /* Don't pick up extra function qualifiers from the basetype. */
11342 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
11343 if (! MAYBE_CLASS_TYPE_P (r))
11345 /* [temp.deduct]
11347 Type deduction may fail for any of the following
11348 reasons:
11350 -- Attempting to create "pointer to member of T" when T
11351 is not a class type. */
11352 if (complain & tf_error)
11353 error ("creating pointer to member function of non-class type %qT",
11355 return error_mark_node;
11358 fntype = build_method_type_directly (r, return_type,
11359 TREE_CHAIN (arg_types));
11360 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11362 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11364 return fntype;
11367 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11368 ARGS into that specification, and return the substituted
11369 specification. If there is no specification, return NULL_TREE. */
11371 static tree
11372 tsubst_exception_specification (tree fntype,
11373 tree args,
11374 tsubst_flags_t complain,
11375 tree in_decl,
11376 bool defer_ok)
11378 tree specs;
11379 tree new_specs;
11381 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11382 new_specs = NULL_TREE;
11383 if (specs && TREE_PURPOSE (specs))
11385 /* A noexcept-specifier. */
11386 tree expr = TREE_PURPOSE (specs);
11387 if (TREE_CODE (expr) == INTEGER_CST)
11388 new_specs = expr;
11389 else if (defer_ok)
11391 /* Defer instantiation of noexcept-specifiers to avoid
11392 excessive instantiations (c++/49107). */
11393 new_specs = make_node (DEFERRED_NOEXCEPT);
11394 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11396 /* We already partially instantiated this member template,
11397 so combine the new args with the old. */
11398 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11399 = DEFERRED_NOEXCEPT_PATTERN (expr);
11400 DEFERRED_NOEXCEPT_ARGS (new_specs)
11401 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11403 else
11405 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11406 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11409 else
11410 new_specs = tsubst_copy_and_build
11411 (expr, args, complain, in_decl, /*function_p=*/false,
11412 /*integral_constant_expression_p=*/true);
11413 new_specs = build_noexcept_spec (new_specs, complain);
11415 else if (specs)
11417 if (! TREE_VALUE (specs))
11418 new_specs = specs;
11419 else
11420 while (specs)
11422 tree spec;
11423 int i, len = 1;
11424 tree expanded_specs = NULL_TREE;
11426 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11428 /* Expand the pack expansion type. */
11429 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11430 args, complain,
11431 in_decl);
11433 if (expanded_specs == error_mark_node)
11434 return error_mark_node;
11435 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11436 len = TREE_VEC_LENGTH (expanded_specs);
11437 else
11439 /* We're substituting into a member template, so
11440 we got a TYPE_PACK_EXPANSION back. Add that
11441 expansion and move on. */
11442 gcc_assert (TREE_CODE (expanded_specs)
11443 == TYPE_PACK_EXPANSION);
11444 new_specs = add_exception_specifier (new_specs,
11445 expanded_specs,
11446 complain);
11447 specs = TREE_CHAIN (specs);
11448 continue;
11452 for (i = 0; i < len; ++i)
11454 if (expanded_specs)
11455 spec = TREE_VEC_ELT (expanded_specs, i);
11456 else
11457 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11458 if (spec == error_mark_node)
11459 return spec;
11460 new_specs = add_exception_specifier (new_specs, spec,
11461 complain);
11464 specs = TREE_CHAIN (specs);
11467 return new_specs;
11470 /* Take the tree structure T and replace template parameters used
11471 therein with the argument vector ARGS. IN_DECL is an associated
11472 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11473 Issue error and warning messages under control of COMPLAIN. Note
11474 that we must be relatively non-tolerant of extensions here, in
11475 order to preserve conformance; if we allow substitutions that
11476 should not be allowed, we may allow argument deductions that should
11477 not succeed, and therefore report ambiguous overload situations
11478 where there are none. In theory, we could allow the substitution,
11479 but indicate that it should have failed, and allow our caller to
11480 make sure that the right thing happens, but we don't try to do this
11481 yet.
11483 This function is used for dealing with types, decls and the like;
11484 for expressions, use tsubst_expr or tsubst_copy. */
11486 tree
11487 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11489 enum tree_code code;
11490 tree type, r = NULL_TREE;
11492 if (t == NULL_TREE || t == error_mark_node
11493 || t == integer_type_node
11494 || t == void_type_node
11495 || t == char_type_node
11496 || t == unknown_type_node
11497 || TREE_CODE (t) == NAMESPACE_DECL
11498 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11499 return t;
11501 if (DECL_P (t))
11502 return tsubst_decl (t, args, complain);
11504 if (args == NULL_TREE)
11505 return t;
11507 code = TREE_CODE (t);
11509 if (code == IDENTIFIER_NODE)
11510 type = IDENTIFIER_TYPE_VALUE (t);
11511 else
11512 type = TREE_TYPE (t);
11514 gcc_assert (type != unknown_type_node);
11516 /* Reuse typedefs. We need to do this to handle dependent attributes,
11517 such as attribute aligned. */
11518 if (TYPE_P (t)
11519 && typedef_variant_p (t))
11521 tree decl = TYPE_NAME (t);
11523 if (alias_template_specialization_p (t))
11525 /* DECL represents an alias template and we want to
11526 instantiate it. */
11527 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11528 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11529 r = instantiate_alias_template (tmpl, gen_args, complain);
11531 else if (DECL_CLASS_SCOPE_P (decl)
11532 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11533 && uses_template_parms (DECL_CONTEXT (decl)))
11535 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11536 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11537 r = retrieve_specialization (tmpl, gen_args, 0);
11539 else if (DECL_FUNCTION_SCOPE_P (decl)
11540 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11541 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11542 r = retrieve_local_specialization (decl);
11543 else
11544 /* The typedef is from a non-template context. */
11545 return t;
11547 if (r)
11549 r = TREE_TYPE (r);
11550 r = cp_build_qualified_type_real
11551 (r, cp_type_quals (t) | cp_type_quals (r),
11552 complain | tf_ignore_bad_quals);
11553 return r;
11555 else
11557 /* We don't have an instantiation yet, so drop the typedef. */
11558 int quals = cp_type_quals (t);
11559 t = DECL_ORIGINAL_TYPE (decl);
11560 t = cp_build_qualified_type_real (t, quals,
11561 complain | tf_ignore_bad_quals);
11565 if (type
11566 && code != TYPENAME_TYPE
11567 && code != TEMPLATE_TYPE_PARM
11568 && code != IDENTIFIER_NODE
11569 && code != FUNCTION_TYPE
11570 && code != METHOD_TYPE)
11571 type = tsubst (type, args, complain, in_decl);
11572 if (type == error_mark_node)
11573 return error_mark_node;
11575 switch (code)
11577 case RECORD_TYPE:
11578 case UNION_TYPE:
11579 case ENUMERAL_TYPE:
11580 return tsubst_aggr_type (t, args, complain, in_decl,
11581 /*entering_scope=*/0);
11583 case ERROR_MARK:
11584 case IDENTIFIER_NODE:
11585 case VOID_TYPE:
11586 case REAL_TYPE:
11587 case COMPLEX_TYPE:
11588 case VECTOR_TYPE:
11589 case BOOLEAN_TYPE:
11590 case NULLPTR_TYPE:
11591 case LANG_TYPE:
11592 return t;
11594 case INTEGER_TYPE:
11595 if (t == integer_type_node)
11596 return t;
11598 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11599 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11600 return t;
11603 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11605 max = tsubst_expr (omax, args, complain, in_decl,
11606 /*integral_constant_expression_p=*/false);
11608 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11609 needed. */
11610 if (TREE_CODE (max) == NOP_EXPR
11611 && TREE_SIDE_EFFECTS (omax)
11612 && !TREE_TYPE (max))
11613 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11615 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11616 with TREE_SIDE_EFFECTS that indicates this is not an integral
11617 constant expression. */
11618 if (processing_template_decl
11619 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11621 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11622 TREE_SIDE_EFFECTS (max) = 1;
11625 return compute_array_index_type (NULL_TREE, max, complain);
11628 case TEMPLATE_TYPE_PARM:
11629 case TEMPLATE_TEMPLATE_PARM:
11630 case BOUND_TEMPLATE_TEMPLATE_PARM:
11631 case TEMPLATE_PARM_INDEX:
11633 int idx;
11634 int level;
11635 int levels;
11636 tree arg = NULL_TREE;
11638 r = NULL_TREE;
11640 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11641 template_parm_level_and_index (t, &level, &idx);
11643 levels = TMPL_ARGS_DEPTH (args);
11644 if (level <= levels)
11646 arg = TMPL_ARG (args, level, idx);
11648 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11650 /* See through ARGUMENT_PACK_SELECT arguments. */
11651 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11652 /* If the selected argument is an expansion E, that most
11653 likely means we were called from
11654 gen_elem_of_pack_expansion_instantiation during the
11655 substituting of pack an argument pack (which Ith
11656 element is a pack expansion, where I is
11657 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11658 In this case, the Ith element resulting from this
11659 substituting is going to be a pack expansion, which
11660 pattern is the pattern of E. Let's return the
11661 pattern of E, and
11662 gen_elem_of_pack_expansion_instantiation will
11663 build the resulting pack expansion from it. */
11664 if (PACK_EXPANSION_P (arg))
11665 arg = PACK_EXPANSION_PATTERN (arg);
11669 if (arg == error_mark_node)
11670 return error_mark_node;
11671 else if (arg != NULL_TREE)
11673 if (ARGUMENT_PACK_P (arg))
11674 /* If ARG is an argument pack, we don't actually want to
11675 perform a substitution here, because substitutions
11676 for argument packs are only done
11677 element-by-element. We can get to this point when
11678 substituting the type of a non-type template
11679 parameter pack, when that type actually contains
11680 template parameter packs from an outer template, e.g.,
11682 template<typename... Types> struct A {
11683 template<Types... Values> struct B { };
11684 }; */
11685 return t;
11687 if (code == TEMPLATE_TYPE_PARM)
11689 int quals;
11690 gcc_assert (TYPE_P (arg));
11692 quals = cp_type_quals (arg) | cp_type_quals (t);
11694 return cp_build_qualified_type_real
11695 (arg, quals, complain | tf_ignore_bad_quals);
11697 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11699 /* We are processing a type constructed from a
11700 template template parameter. */
11701 tree argvec = tsubst (TYPE_TI_ARGS (t),
11702 args, complain, in_decl);
11703 if (argvec == error_mark_node)
11704 return error_mark_node;
11706 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11707 || TREE_CODE (arg) == TEMPLATE_DECL
11708 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11710 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11711 /* Consider this code:
11713 template <template <class> class Template>
11714 struct Internal {
11715 template <class Arg> using Bind = Template<Arg>;
11718 template <template <class> class Template, class Arg>
11719 using Instantiate = Template<Arg>; //#0
11721 template <template <class> class Template,
11722 class Argument>
11723 using Bind =
11724 Instantiate<Internal<Template>::template Bind,
11725 Argument>; //#1
11727 When #1 is parsed, the
11728 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11729 parameter `Template' in #0 matches the
11730 UNBOUND_CLASS_TEMPLATE representing the argument
11731 `Internal<Template>::template Bind'; We then want
11732 to assemble the type `Bind<Argument>' that can't
11733 be fully created right now, because
11734 `Internal<Template>' not being complete, the Bind
11735 template cannot be looked up in that context. So
11736 we need to "store" `Bind<Argument>' for later
11737 when the context of Bind becomes complete. Let's
11738 store that in a TYPENAME_TYPE. */
11739 return make_typename_type (TYPE_CONTEXT (arg),
11740 build_nt (TEMPLATE_ID_EXPR,
11741 TYPE_IDENTIFIER (arg),
11742 argvec),
11743 typename_type,
11744 complain);
11746 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11747 are resolving nested-types in the signature of a
11748 member function templates. Otherwise ARG is a
11749 TEMPLATE_DECL and is the real template to be
11750 instantiated. */
11751 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11752 arg = TYPE_NAME (arg);
11754 r = lookup_template_class (arg,
11755 argvec, in_decl,
11756 DECL_CONTEXT (arg),
11757 /*entering_scope=*/0,
11758 complain);
11759 return cp_build_qualified_type_real
11760 (r, cp_type_quals (t) | cp_type_quals (r), complain);
11762 else
11763 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11764 return convert_from_reference (unshare_expr (arg));
11767 if (level == 1)
11768 /* This can happen during the attempted tsubst'ing in
11769 unify. This means that we don't yet have any information
11770 about the template parameter in question. */
11771 return t;
11773 /* Early in template argument deduction substitution, we don't
11774 want to reduce the level of 'auto', or it will be confused
11775 with a normal template parm in subsequent deduction. */
11776 if (is_auto (t) && (complain & tf_partial))
11777 return t;
11779 /* If we get here, we must have been looking at a parm for a
11780 more deeply nested template. Make a new version of this
11781 template parameter, but with a lower level. */
11782 switch (code)
11784 case TEMPLATE_TYPE_PARM:
11785 case TEMPLATE_TEMPLATE_PARM:
11786 case BOUND_TEMPLATE_TEMPLATE_PARM:
11787 if (cp_type_quals (t))
11789 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11790 r = cp_build_qualified_type_real
11791 (r, cp_type_quals (t),
11792 complain | (code == TEMPLATE_TYPE_PARM
11793 ? tf_ignore_bad_quals : 0));
11795 else
11797 r = copy_type (t);
11798 TEMPLATE_TYPE_PARM_INDEX (r)
11799 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11800 r, levels, args, complain);
11801 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11802 TYPE_MAIN_VARIANT (r) = r;
11803 TYPE_POINTER_TO (r) = NULL_TREE;
11804 TYPE_REFERENCE_TO (r) = NULL_TREE;
11806 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11807 /* We have reduced the level of the template
11808 template parameter, but not the levels of its
11809 template parameters, so canonical_type_parameter
11810 will not be able to find the canonical template
11811 template parameter for this level. Thus, we
11812 require structural equality checking to compare
11813 TEMPLATE_TEMPLATE_PARMs. */
11814 SET_TYPE_STRUCTURAL_EQUALITY (r);
11815 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11816 SET_TYPE_STRUCTURAL_EQUALITY (r);
11817 else
11818 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11820 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11822 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11823 complain, in_decl);
11824 if (argvec == error_mark_node)
11825 return error_mark_node;
11827 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11828 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11831 break;
11833 case TEMPLATE_PARM_INDEX:
11834 r = reduce_template_parm_level (t, type, levels, args, complain);
11835 break;
11837 default:
11838 gcc_unreachable ();
11841 return r;
11844 case TREE_LIST:
11846 tree purpose, value, chain;
11848 if (t == void_list_node)
11849 return t;
11851 purpose = TREE_PURPOSE (t);
11852 if (purpose)
11854 purpose = tsubst (purpose, args, complain, in_decl);
11855 if (purpose == error_mark_node)
11856 return error_mark_node;
11858 value = TREE_VALUE (t);
11859 if (value)
11861 value = tsubst (value, args, complain, in_decl);
11862 if (value == error_mark_node)
11863 return error_mark_node;
11865 chain = TREE_CHAIN (t);
11866 if (chain && chain != void_type_node)
11868 chain = tsubst (chain, args, complain, in_decl);
11869 if (chain == error_mark_node)
11870 return error_mark_node;
11872 if (purpose == TREE_PURPOSE (t)
11873 && value == TREE_VALUE (t)
11874 && chain == TREE_CHAIN (t))
11875 return t;
11876 return hash_tree_cons (purpose, value, chain);
11879 case TREE_BINFO:
11880 /* We should never be tsubsting a binfo. */
11881 gcc_unreachable ();
11883 case TREE_VEC:
11884 /* A vector of template arguments. */
11885 gcc_assert (!type);
11886 return tsubst_template_args (t, args, complain, in_decl);
11888 case POINTER_TYPE:
11889 case REFERENCE_TYPE:
11891 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11892 return t;
11894 /* [temp.deduct]
11896 Type deduction may fail for any of the following
11897 reasons:
11899 -- Attempting to create a pointer to reference type.
11900 -- Attempting to create a reference to a reference type or
11901 a reference to void.
11903 Core issue 106 says that creating a reference to a reference
11904 during instantiation is no longer a cause for failure. We
11905 only enforce this check in strict C++98 mode. */
11906 if ((TREE_CODE (type) == REFERENCE_TYPE
11907 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11908 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
11910 static location_t last_loc;
11912 /* We keep track of the last time we issued this error
11913 message to avoid spewing a ton of messages during a
11914 single bad template instantiation. */
11915 if (complain & tf_error
11916 && last_loc != input_location)
11918 if (VOID_TYPE_P (type))
11919 error ("forming reference to void");
11920 else if (code == POINTER_TYPE)
11921 error ("forming pointer to reference type %qT", type);
11922 else
11923 error ("forming reference to reference type %qT", type);
11924 last_loc = input_location;
11927 return error_mark_node;
11929 else if (TREE_CODE (type) == FUNCTION_TYPE
11930 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11931 || type_memfn_rqual (type) != REF_QUAL_NONE))
11933 if (complain & tf_error)
11935 if (code == POINTER_TYPE)
11936 error ("forming pointer to qualified function type %qT",
11937 type);
11938 else
11939 error ("forming reference to qualified function type %qT",
11940 type);
11942 return error_mark_node;
11944 else if (code == POINTER_TYPE)
11946 r = build_pointer_type (type);
11947 if (TREE_CODE (type) == METHOD_TYPE)
11948 r = build_ptrmemfunc_type (r);
11950 else if (TREE_CODE (type) == REFERENCE_TYPE)
11951 /* In C++0x, during template argument substitution, when there is an
11952 attempt to create a reference to a reference type, reference
11953 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11955 "If a template-argument for a template-parameter T names a type
11956 that is a reference to a type A, an attempt to create the type
11957 'lvalue reference to cv T' creates the type 'lvalue reference to
11958 A,' while an attempt to create the type type rvalue reference to
11959 cv T' creates the type T"
11961 r = cp_build_reference_type
11962 (TREE_TYPE (type),
11963 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11964 else
11965 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11966 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11968 if (cxx_dialect >= cxx1y
11969 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
11970 && array_of_runtime_bound_p (type)
11971 && (flag_iso || warn_vla > 0))
11973 if (complain & tf_warning_or_error)
11974 pedwarn
11975 (input_location, OPT_Wvla,
11976 code == REFERENCE_TYPE
11977 ? G_("cannot declare reference to array of runtime bound")
11978 : G_("cannot declare pointer to array of runtime bound"));
11979 else
11980 r = error_mark_node;
11983 if (r != error_mark_node)
11984 /* Will this ever be needed for TYPE_..._TO values? */
11985 layout_type (r);
11987 return r;
11989 case OFFSET_TYPE:
11991 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11992 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11994 /* [temp.deduct]
11996 Type deduction may fail for any of the following
11997 reasons:
11999 -- Attempting to create "pointer to member of T" when T
12000 is not a class type. */
12001 if (complain & tf_error)
12002 error ("creating pointer to member of non-class type %qT", r);
12003 return error_mark_node;
12005 if (TREE_CODE (type) == REFERENCE_TYPE)
12007 if (complain & tf_error)
12008 error ("creating pointer to member reference type %qT", type);
12009 return error_mark_node;
12011 if (VOID_TYPE_P (type))
12013 if (complain & tf_error)
12014 error ("creating pointer to member of type void");
12015 return error_mark_node;
12017 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12018 if (TREE_CODE (type) == FUNCTION_TYPE)
12020 /* The type of the implicit object parameter gets its
12021 cv-qualifiers from the FUNCTION_TYPE. */
12022 tree memptr;
12023 tree method_type
12024 = build_memfn_type (type, r, type_memfn_quals (type),
12025 type_memfn_rqual (type));
12026 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
12027 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
12028 complain);
12030 else
12031 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
12032 cp_type_quals (t),
12033 complain);
12035 case FUNCTION_TYPE:
12036 case METHOD_TYPE:
12038 tree fntype;
12039 tree specs;
12040 fntype = tsubst_function_type (t, args, complain, in_decl);
12041 if (fntype == error_mark_node)
12042 return error_mark_node;
12044 /* Substitute the exception specification. */
12045 specs = tsubst_exception_specification (t, args, complain,
12046 in_decl, /*defer_ok*/true);
12047 if (specs == error_mark_node)
12048 return error_mark_node;
12049 if (specs)
12050 fntype = build_exception_variant (fntype, specs);
12051 return fntype;
12053 case ARRAY_TYPE:
12055 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
12056 if (domain == error_mark_node)
12057 return error_mark_node;
12059 /* As an optimization, we avoid regenerating the array type if
12060 it will obviously be the same as T. */
12061 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
12062 return t;
12064 /* These checks should match the ones in grokdeclarator.
12066 [temp.deduct]
12068 The deduction may fail for any of the following reasons:
12070 -- Attempting to create an array with an element type that
12071 is void, a function type, or a reference type, or [DR337]
12072 an abstract class type. */
12073 if (VOID_TYPE_P (type)
12074 || TREE_CODE (type) == FUNCTION_TYPE
12075 || TREE_CODE (type) == REFERENCE_TYPE)
12077 if (complain & tf_error)
12078 error ("creating array of %qT", type);
12079 return error_mark_node;
12082 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
12083 return error_mark_node;
12085 r = build_cplus_array_type (type, domain);
12087 if (TYPE_USER_ALIGN (t))
12089 TYPE_ALIGN (r) = TYPE_ALIGN (t);
12090 TYPE_USER_ALIGN (r) = 1;
12093 return r;
12096 case TYPENAME_TYPE:
12098 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12099 in_decl, /*entering_scope=*/1);
12100 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
12101 complain, in_decl);
12103 if (ctx == error_mark_node || f == error_mark_node)
12104 return error_mark_node;
12106 if (!MAYBE_CLASS_TYPE_P (ctx))
12108 if (complain & tf_error)
12109 error ("%qT is not a class, struct, or union type", ctx);
12110 return error_mark_node;
12112 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
12114 /* Normally, make_typename_type does not require that the CTX
12115 have complete type in order to allow things like:
12117 template <class T> struct S { typename S<T>::X Y; };
12119 But, such constructs have already been resolved by this
12120 point, so here CTX really should have complete type, unless
12121 it's a partial instantiation. */
12122 ctx = complete_type (ctx);
12123 if (!COMPLETE_TYPE_P (ctx))
12125 if (complain & tf_error)
12126 cxx_incomplete_type_error (NULL_TREE, ctx);
12127 return error_mark_node;
12131 f = make_typename_type (ctx, f, typename_type,
12132 complain | tf_keep_type_decl);
12133 if (f == error_mark_node)
12134 return f;
12135 if (TREE_CODE (f) == TYPE_DECL)
12137 complain |= tf_ignore_bad_quals;
12138 f = TREE_TYPE (f);
12141 if (TREE_CODE (f) != TYPENAME_TYPE)
12143 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
12145 if (complain & tf_error)
12146 error ("%qT resolves to %qT, which is not an enumeration type",
12147 t, f);
12148 else
12149 return error_mark_node;
12151 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
12153 if (complain & tf_error)
12154 error ("%qT resolves to %qT, which is is not a class type",
12155 t, f);
12156 else
12157 return error_mark_node;
12161 return cp_build_qualified_type_real
12162 (f, cp_type_quals (f) | cp_type_quals (t), complain);
12165 case UNBOUND_CLASS_TEMPLATE:
12167 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12168 in_decl, /*entering_scope=*/1);
12169 tree name = TYPE_IDENTIFIER (t);
12170 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12172 if (ctx == error_mark_node || name == error_mark_node)
12173 return error_mark_node;
12175 if (parm_list)
12176 parm_list = tsubst_template_parms (parm_list, args, complain);
12177 return make_unbound_class_template (ctx, name, parm_list, complain);
12180 case TYPEOF_TYPE:
12182 tree type;
12184 ++cp_unevaluated_operand;
12185 ++c_inhibit_evaluation_warnings;
12187 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12188 complain, in_decl,
12189 /*integral_constant_expression_p=*/false);
12191 --cp_unevaluated_operand;
12192 --c_inhibit_evaluation_warnings;
12194 type = finish_typeof (type);
12195 return cp_build_qualified_type_real (type,
12196 cp_type_quals (t)
12197 | cp_type_quals (type),
12198 complain);
12201 case DECLTYPE_TYPE:
12203 tree type;
12205 ++cp_unevaluated_operand;
12206 ++c_inhibit_evaluation_warnings;
12208 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12209 complain|tf_decltype, in_decl,
12210 /*function_p*/false,
12211 /*integral_constant_expression*/false);
12213 --cp_unevaluated_operand;
12214 --c_inhibit_evaluation_warnings;
12216 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12217 type = lambda_capture_field_type (type,
12218 DECLTYPE_FOR_INIT_CAPTURE (t));
12219 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12220 type = lambda_proxy_type (type);
12221 else
12223 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12224 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12225 && EXPR_P (type))
12226 /* In a template ~id could be either a complement expression
12227 or an unqualified-id naming a destructor; if instantiating
12228 it produces an expression, it's not an id-expression or
12229 member access. */
12230 id = false;
12231 type = finish_decltype_type (type, id, complain);
12233 return cp_build_qualified_type_real (type,
12234 cp_type_quals (t)
12235 | cp_type_quals (type),
12236 complain);
12239 case UNDERLYING_TYPE:
12241 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12242 complain, in_decl);
12243 return finish_underlying_type (type);
12246 case TYPE_ARGUMENT_PACK:
12247 case NONTYPE_ARGUMENT_PACK:
12249 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12250 tree packed_out =
12251 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12252 args,
12253 complain,
12254 in_decl);
12255 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12257 /* For template nontype argument packs, also substitute into
12258 the type. */
12259 if (code == NONTYPE_ARGUMENT_PACK)
12260 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12262 return r;
12264 break;
12266 case INTEGER_CST:
12267 case REAL_CST:
12268 case STRING_CST:
12269 case PLUS_EXPR:
12270 case MINUS_EXPR:
12271 case NEGATE_EXPR:
12272 case NOP_EXPR:
12273 case INDIRECT_REF:
12274 case ADDR_EXPR:
12275 case CALL_EXPR:
12276 case ARRAY_REF:
12277 case SCOPE_REF:
12278 /* We should use one of the expression tsubsts for these codes. */
12279 gcc_unreachable ();
12281 default:
12282 sorry ("use of %qs in template", get_tree_code_name (code));
12283 return error_mark_node;
12287 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12288 type of the expression on the left-hand side of the "." or "->"
12289 operator. */
12291 static tree
12292 tsubst_baselink (tree baselink, tree object_type,
12293 tree args, tsubst_flags_t complain, tree in_decl)
12295 tree name;
12296 tree qualifying_scope;
12297 tree fns;
12298 tree optype;
12299 tree template_args = 0;
12300 bool template_id_p = false;
12301 bool qualified = BASELINK_QUALIFIED_P (baselink);
12303 /* A baselink indicates a function from a base class. Both the
12304 BASELINK_ACCESS_BINFO and the base class referenced may
12305 indicate bases of the template class, rather than the
12306 instantiated class. In addition, lookups that were not
12307 ambiguous before may be ambiguous now. Therefore, we perform
12308 the lookup again. */
12309 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12310 qualifying_scope = tsubst (qualifying_scope, args,
12311 complain, in_decl);
12312 fns = BASELINK_FUNCTIONS (baselink);
12313 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12314 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12316 template_id_p = true;
12317 template_args = TREE_OPERAND (fns, 1);
12318 fns = TREE_OPERAND (fns, 0);
12319 if (template_args)
12320 template_args = tsubst_template_args (template_args, args,
12321 complain, in_decl);
12323 name = DECL_NAME (get_first_fn (fns));
12324 if (IDENTIFIER_TYPENAME_P (name))
12325 name = mangle_conv_op_name_for_type (optype);
12326 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12327 if (!baselink)
12328 return error_mark_node;
12330 /* If lookup found a single function, mark it as used at this
12331 point. (If it lookup found multiple functions the one selected
12332 later by overload resolution will be marked as used at that
12333 point.) */
12334 if (BASELINK_P (baselink))
12335 fns = BASELINK_FUNCTIONS (baselink);
12336 if (!template_id_p && !really_overloaded_fn (fns))
12337 mark_used (OVL_CURRENT (fns));
12339 /* Add back the template arguments, if present. */
12340 if (BASELINK_P (baselink) && template_id_p)
12341 BASELINK_FUNCTIONS (baselink)
12342 = build_nt (TEMPLATE_ID_EXPR,
12343 BASELINK_FUNCTIONS (baselink),
12344 template_args);
12345 /* Update the conversion operator type. */
12346 BASELINK_OPTYPE (baselink) = optype;
12348 if (!object_type)
12349 object_type = current_class_type;
12351 if (qualified)
12352 baselink = adjust_result_of_qualified_name_lookup (baselink,
12353 qualifying_scope,
12354 object_type);
12355 return baselink;
12358 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12359 true if the qualified-id will be a postfix-expression in-and-of
12360 itself; false if more of the postfix-expression follows the
12361 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12362 of "&". */
12364 static tree
12365 tsubst_qualified_id (tree qualified_id, tree args,
12366 tsubst_flags_t complain, tree in_decl,
12367 bool done, bool address_p)
12369 tree expr;
12370 tree scope;
12371 tree name;
12372 bool is_template;
12373 tree template_args;
12374 location_t loc = UNKNOWN_LOCATION;
12376 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12378 /* Figure out what name to look up. */
12379 name = TREE_OPERAND (qualified_id, 1);
12380 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12382 is_template = true;
12383 loc = EXPR_LOCATION (name);
12384 template_args = TREE_OPERAND (name, 1);
12385 if (template_args)
12386 template_args = tsubst_template_args (template_args, args,
12387 complain, in_decl);
12388 name = TREE_OPERAND (name, 0);
12390 else
12392 is_template = false;
12393 template_args = NULL_TREE;
12396 /* Substitute into the qualifying scope. When there are no ARGS, we
12397 are just trying to simplify a non-dependent expression. In that
12398 case the qualifying scope may be dependent, and, in any case,
12399 substituting will not help. */
12400 scope = TREE_OPERAND (qualified_id, 0);
12401 if (args)
12403 scope = tsubst (scope, args, complain, in_decl);
12404 expr = tsubst_copy (name, args, complain, in_decl);
12406 else
12407 expr = name;
12409 if (dependent_scope_p (scope))
12411 if (is_template)
12412 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12413 return build_qualified_name (NULL_TREE, scope, expr,
12414 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12417 if (!BASELINK_P (name) && !DECL_P (expr))
12419 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12421 /* A BIT_NOT_EXPR is used to represent a destructor. */
12422 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12424 error ("qualifying type %qT does not match destructor name ~%qT",
12425 scope, TREE_OPERAND (expr, 0));
12426 expr = error_mark_node;
12428 else
12429 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12430 /*is_type_p=*/0, false);
12432 else
12433 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12434 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12435 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12437 if (complain & tf_error)
12439 error ("dependent-name %qE is parsed as a non-type, but "
12440 "instantiation yields a type", qualified_id);
12441 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12443 return error_mark_node;
12447 if (DECL_P (expr))
12449 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12450 scope);
12451 /* Remember that there was a reference to this entity. */
12452 mark_used (expr);
12455 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12457 if (complain & tf_error)
12458 qualified_name_lookup_error (scope,
12459 TREE_OPERAND (qualified_id, 1),
12460 expr, input_location);
12461 return error_mark_node;
12464 if (is_template)
12465 expr = lookup_template_function (expr, template_args);
12467 if (expr == error_mark_node && complain & tf_error)
12468 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12469 expr, input_location);
12470 else if (TYPE_P (scope))
12472 expr = (adjust_result_of_qualified_name_lookup
12473 (expr, scope, current_nonlambda_class_type ()));
12474 expr = (finish_qualified_id_expr
12475 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12476 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12477 /*template_arg_p=*/false, complain));
12480 /* Expressions do not generally have reference type. */
12481 if (TREE_CODE (expr) != SCOPE_REF
12482 /* However, if we're about to form a pointer-to-member, we just
12483 want the referenced member referenced. */
12484 && TREE_CODE (expr) != OFFSET_REF)
12485 expr = convert_from_reference (expr);
12487 return expr;
12490 /* Like tsubst, but deals with expressions. This function just replaces
12491 template parms; to finish processing the resultant expression, use
12492 tsubst_copy_and_build or tsubst_expr. */
12494 static tree
12495 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12497 enum tree_code code;
12498 tree r;
12500 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12501 return t;
12503 code = TREE_CODE (t);
12505 switch (code)
12507 case PARM_DECL:
12508 r = retrieve_local_specialization (t);
12510 if (r == NULL_TREE)
12512 /* We get here for a use of 'this' in an NSDMI. */
12513 if (DECL_NAME (t) == this_identifier
12514 && at_function_scope_p ()
12515 && DECL_CONSTRUCTOR_P (current_function_decl))
12516 return current_class_ptr;
12518 /* This can happen for a parameter name used later in a function
12519 declaration (such as in a late-specified return type). Just
12520 make a dummy decl, since it's only used for its type. */
12521 gcc_assert (cp_unevaluated_operand != 0);
12522 r = tsubst_decl (t, args, complain);
12523 /* Give it the template pattern as its context; its true context
12524 hasn't been instantiated yet and this is good enough for
12525 mangling. */
12526 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12529 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12530 r = ARGUMENT_PACK_SELECT_ARG (r);
12531 mark_used (r);
12532 return r;
12534 case CONST_DECL:
12536 tree enum_type;
12537 tree v;
12539 if (DECL_TEMPLATE_PARM_P (t))
12540 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12541 /* There is no need to substitute into namespace-scope
12542 enumerators. */
12543 if (DECL_NAMESPACE_SCOPE_P (t))
12544 return t;
12545 /* If ARGS is NULL, then T is known to be non-dependent. */
12546 if (args == NULL_TREE)
12547 return integral_constant_value (t);
12549 /* Unfortunately, we cannot just call lookup_name here.
12550 Consider:
12552 template <int I> int f() {
12553 enum E { a = I };
12554 struct S { void g() { E e = a; } };
12557 When we instantiate f<7>::S::g(), say, lookup_name is not
12558 clever enough to find f<7>::a. */
12559 enum_type
12560 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12561 /*entering_scope=*/0);
12563 for (v = TYPE_VALUES (enum_type);
12564 v != NULL_TREE;
12565 v = TREE_CHAIN (v))
12566 if (TREE_PURPOSE (v) == DECL_NAME (t))
12567 return TREE_VALUE (v);
12569 /* We didn't find the name. That should never happen; if
12570 name-lookup found it during preliminary parsing, we
12571 should find it again here during instantiation. */
12572 gcc_unreachable ();
12574 return t;
12576 case FIELD_DECL:
12577 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12579 /* Check for a local specialization set up by
12580 tsubst_pack_expansion. */
12581 if (tree r = retrieve_local_specialization (t))
12583 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12584 r = ARGUMENT_PACK_SELECT_ARG (r);
12585 return r;
12588 /* When retrieving a capture pack from a generic lambda, remove the
12589 lambda call op's own template argument list from ARGS. Only the
12590 template arguments active for the closure type should be used to
12591 retrieve the pack specialization. */
12592 if (LAMBDA_FUNCTION_P (current_function_decl)
12593 && (template_class_depth (DECL_CONTEXT (t))
12594 != TMPL_ARGS_DEPTH (args)))
12595 args = strip_innermost_template_args (args, 1);
12597 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12598 tsubst_decl put in the hash table. */
12599 return retrieve_specialization (t, args, 0);
12602 if (DECL_CONTEXT (t))
12604 tree ctx;
12606 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12607 /*entering_scope=*/1);
12608 if (ctx != DECL_CONTEXT (t))
12610 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12611 if (!r)
12613 if (complain & tf_error)
12614 error ("using invalid field %qD", t);
12615 return error_mark_node;
12617 return r;
12621 return t;
12623 case VAR_DECL:
12624 case FUNCTION_DECL:
12625 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12626 r = tsubst (t, args, complain, in_decl);
12627 else if (local_variable_p (t))
12629 r = retrieve_local_specialization (t);
12630 if (r == NULL_TREE)
12632 if (DECL_ANON_UNION_VAR_P (t))
12634 /* Just use name lookup to find a member alias for an
12635 anonymous union, but then add it to the hash table. */
12636 r = lookup_name (DECL_NAME (t));
12637 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12638 register_local_specialization (r, t);
12640 else
12642 /* This can happen for a variable used in a late-specified
12643 return type of a local lambda. Just make a dummy decl
12644 since it's only used for its type. */
12645 if (cp_unevaluated_operand)
12646 return tsubst_decl (t, args, complain);
12647 gcc_assert (errorcount || sorrycount);
12648 return error_mark_node;
12652 else
12653 r = t;
12654 mark_used (r);
12655 return r;
12657 case NAMESPACE_DECL:
12658 return t;
12660 case OVERLOAD:
12661 /* An OVERLOAD will always be a non-dependent overload set; an
12662 overload set from function scope will just be represented with an
12663 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12664 gcc_assert (!uses_template_parms (t));
12665 return t;
12667 case BASELINK:
12668 return tsubst_baselink (t, current_nonlambda_class_type (),
12669 args, complain, in_decl);
12671 case TEMPLATE_DECL:
12672 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12673 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12674 args, complain, in_decl);
12675 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12676 return tsubst (t, args, complain, in_decl);
12677 else if (DECL_CLASS_SCOPE_P (t)
12678 && uses_template_parms (DECL_CONTEXT (t)))
12680 /* Template template argument like the following example need
12681 special treatment:
12683 template <template <class> class TT> struct C {};
12684 template <class T> struct D {
12685 template <class U> struct E {};
12686 C<E> c; // #1
12688 D<int> d; // #2
12690 We are processing the template argument `E' in #1 for
12691 the template instantiation #2. Originally, `E' is a
12692 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12693 have to substitute this with one having context `D<int>'. */
12695 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12696 return lookup_field (context, DECL_NAME(t), 0, false);
12698 else
12699 /* Ordinary template template argument. */
12700 return t;
12702 case CAST_EXPR:
12703 case REINTERPRET_CAST_EXPR:
12704 case CONST_CAST_EXPR:
12705 case STATIC_CAST_EXPR:
12706 case DYNAMIC_CAST_EXPR:
12707 case IMPLICIT_CONV_EXPR:
12708 case CONVERT_EXPR:
12709 case NOP_EXPR:
12710 return build1
12711 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12712 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12714 case SIZEOF_EXPR:
12715 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12718 tree expanded, op = TREE_OPERAND (t, 0);
12719 int len = 0;
12721 if (SIZEOF_EXPR_TYPE_P (t))
12722 op = TREE_TYPE (op);
12724 ++cp_unevaluated_operand;
12725 ++c_inhibit_evaluation_warnings;
12726 /* We only want to compute the number of arguments. */
12727 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12728 --cp_unevaluated_operand;
12729 --c_inhibit_evaluation_warnings;
12731 if (TREE_CODE (expanded) == TREE_VEC)
12732 len = TREE_VEC_LENGTH (expanded);
12734 if (expanded == error_mark_node)
12735 return error_mark_node;
12736 else if (PACK_EXPANSION_P (expanded)
12737 || (TREE_CODE (expanded) == TREE_VEC
12738 && len > 0
12739 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12741 if (TREE_CODE (expanded) == TREE_VEC)
12742 expanded = TREE_VEC_ELT (expanded, len - 1);
12744 if (TYPE_P (expanded))
12745 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12746 complain & tf_error);
12747 else
12748 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12749 complain & tf_error);
12751 else
12752 return build_int_cst (size_type_node, len);
12754 if (SIZEOF_EXPR_TYPE_P (t))
12756 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12757 args, complain, in_decl);
12758 r = build1 (NOP_EXPR, r, error_mark_node);
12759 r = build1 (SIZEOF_EXPR,
12760 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12761 SIZEOF_EXPR_TYPE_P (r) = 1;
12762 return r;
12764 /* Fall through */
12766 case INDIRECT_REF:
12767 case NEGATE_EXPR:
12768 case TRUTH_NOT_EXPR:
12769 case BIT_NOT_EXPR:
12770 case ADDR_EXPR:
12771 case UNARY_PLUS_EXPR: /* Unary + */
12772 case ALIGNOF_EXPR:
12773 case AT_ENCODE_EXPR:
12774 case ARROW_EXPR:
12775 case THROW_EXPR:
12776 case TYPEID_EXPR:
12777 case REALPART_EXPR:
12778 case IMAGPART_EXPR:
12779 case PAREN_EXPR:
12780 return build1
12781 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12782 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12784 case COMPONENT_REF:
12786 tree object;
12787 tree name;
12789 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12790 name = TREE_OPERAND (t, 1);
12791 if (TREE_CODE (name) == BIT_NOT_EXPR)
12793 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12794 complain, in_decl);
12795 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12797 else if (TREE_CODE (name) == SCOPE_REF
12798 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12800 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12801 complain, in_decl);
12802 name = TREE_OPERAND (name, 1);
12803 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12804 complain, in_decl);
12805 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12806 name = build_qualified_name (/*type=*/NULL_TREE,
12807 base, name,
12808 /*template_p=*/false);
12810 else if (BASELINK_P (name))
12811 name = tsubst_baselink (name,
12812 non_reference (TREE_TYPE (object)),
12813 args, complain,
12814 in_decl);
12815 else
12816 name = tsubst_copy (name, args, complain, in_decl);
12817 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12820 case PLUS_EXPR:
12821 case MINUS_EXPR:
12822 case MULT_EXPR:
12823 case TRUNC_DIV_EXPR:
12824 case CEIL_DIV_EXPR:
12825 case FLOOR_DIV_EXPR:
12826 case ROUND_DIV_EXPR:
12827 case EXACT_DIV_EXPR:
12828 case BIT_AND_EXPR:
12829 case BIT_IOR_EXPR:
12830 case BIT_XOR_EXPR:
12831 case TRUNC_MOD_EXPR:
12832 case FLOOR_MOD_EXPR:
12833 case TRUTH_ANDIF_EXPR:
12834 case TRUTH_ORIF_EXPR:
12835 case TRUTH_AND_EXPR:
12836 case TRUTH_OR_EXPR:
12837 case RSHIFT_EXPR:
12838 case LSHIFT_EXPR:
12839 case RROTATE_EXPR:
12840 case LROTATE_EXPR:
12841 case EQ_EXPR:
12842 case NE_EXPR:
12843 case MAX_EXPR:
12844 case MIN_EXPR:
12845 case LE_EXPR:
12846 case GE_EXPR:
12847 case LT_EXPR:
12848 case GT_EXPR:
12849 case COMPOUND_EXPR:
12850 case DOTSTAR_EXPR:
12851 case MEMBER_REF:
12852 case PREDECREMENT_EXPR:
12853 case PREINCREMENT_EXPR:
12854 case POSTDECREMENT_EXPR:
12855 case POSTINCREMENT_EXPR:
12856 return build_nt
12857 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12858 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12860 case SCOPE_REF:
12861 return build_qualified_name (/*type=*/NULL_TREE,
12862 tsubst_copy (TREE_OPERAND (t, 0),
12863 args, complain, in_decl),
12864 tsubst_copy (TREE_OPERAND (t, 1),
12865 args, complain, in_decl),
12866 QUALIFIED_NAME_IS_TEMPLATE (t));
12868 case ARRAY_REF:
12869 return build_nt
12870 (ARRAY_REF,
12871 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12872 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12873 NULL_TREE, NULL_TREE);
12875 case CALL_EXPR:
12877 int n = VL_EXP_OPERAND_LENGTH (t);
12878 tree result = build_vl_exp (CALL_EXPR, n);
12879 int i;
12880 for (i = 0; i < n; i++)
12881 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12882 complain, in_decl);
12883 return result;
12886 case COND_EXPR:
12887 case MODOP_EXPR:
12888 case PSEUDO_DTOR_EXPR:
12889 case VEC_PERM_EXPR:
12891 r = build_nt
12892 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12893 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12894 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12895 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12896 return r;
12899 case NEW_EXPR:
12901 r = build_nt
12902 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12903 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12904 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12905 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12906 return r;
12909 case DELETE_EXPR:
12911 r = build_nt
12912 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12913 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12914 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12915 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12916 return r;
12919 case TEMPLATE_ID_EXPR:
12921 /* Substituted template arguments */
12922 tree fn = TREE_OPERAND (t, 0);
12923 tree targs = TREE_OPERAND (t, 1);
12925 fn = tsubst_copy (fn, args, complain, in_decl);
12926 if (targs)
12927 targs = tsubst_template_args (targs, args, complain, in_decl);
12929 return lookup_template_function (fn, targs);
12932 case TREE_LIST:
12934 tree purpose, value, chain;
12936 if (t == void_list_node)
12937 return t;
12939 purpose = TREE_PURPOSE (t);
12940 if (purpose)
12941 purpose = tsubst_copy (purpose, args, complain, in_decl);
12942 value = TREE_VALUE (t);
12943 if (value)
12944 value = tsubst_copy (value, args, complain, in_decl);
12945 chain = TREE_CHAIN (t);
12946 if (chain && chain != void_type_node)
12947 chain = tsubst_copy (chain, args, complain, in_decl);
12948 if (purpose == TREE_PURPOSE (t)
12949 && value == TREE_VALUE (t)
12950 && chain == TREE_CHAIN (t))
12951 return t;
12952 return tree_cons (purpose, value, chain);
12955 case RECORD_TYPE:
12956 case UNION_TYPE:
12957 case ENUMERAL_TYPE:
12958 case INTEGER_TYPE:
12959 case TEMPLATE_TYPE_PARM:
12960 case TEMPLATE_TEMPLATE_PARM:
12961 case BOUND_TEMPLATE_TEMPLATE_PARM:
12962 case TEMPLATE_PARM_INDEX:
12963 case POINTER_TYPE:
12964 case REFERENCE_TYPE:
12965 case OFFSET_TYPE:
12966 case FUNCTION_TYPE:
12967 case METHOD_TYPE:
12968 case ARRAY_TYPE:
12969 case TYPENAME_TYPE:
12970 case UNBOUND_CLASS_TEMPLATE:
12971 case TYPEOF_TYPE:
12972 case DECLTYPE_TYPE:
12973 case TYPE_DECL:
12974 return tsubst (t, args, complain, in_decl);
12976 case USING_DECL:
12977 t = DECL_NAME (t);
12978 /* Fall through. */
12979 case IDENTIFIER_NODE:
12980 if (IDENTIFIER_TYPENAME_P (t))
12982 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12983 return mangle_conv_op_name_for_type (new_type);
12985 else
12986 return t;
12988 case CONSTRUCTOR:
12989 /* This is handled by tsubst_copy_and_build. */
12990 gcc_unreachable ();
12992 case VA_ARG_EXPR:
12993 return build_x_va_arg (EXPR_LOCATION (t),
12994 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12995 in_decl),
12996 tsubst (TREE_TYPE (t), args, complain, in_decl));
12998 case CLEANUP_POINT_EXPR:
12999 /* We shouldn't have built any of these during initial template
13000 generation. Instead, they should be built during instantiation
13001 in response to the saved STMT_IS_FULL_EXPR_P setting. */
13002 gcc_unreachable ();
13004 case OFFSET_REF:
13005 r = build2
13006 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
13007 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
13008 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
13009 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
13010 mark_used (TREE_OPERAND (r, 1));
13011 return r;
13013 case EXPR_PACK_EXPANSION:
13014 error ("invalid use of pack expansion expression");
13015 return error_mark_node;
13017 case NONTYPE_ARGUMENT_PACK:
13018 error ("use %<...%> to expand argument pack");
13019 return error_mark_node;
13021 case INTEGER_CST:
13022 case REAL_CST:
13023 case STRING_CST:
13024 case COMPLEX_CST:
13026 /* Instantiate any typedefs in the type. */
13027 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13028 r = fold_convert (type, t);
13029 gcc_assert (TREE_CODE (r) == code);
13030 return r;
13033 case PTRMEM_CST:
13034 /* These can sometimes show up in a partial instantiation, but never
13035 involve template parms. */
13036 gcc_assert (!uses_template_parms (t));
13037 return t;
13039 default:
13040 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13041 gcc_checking_assert (false);
13042 return t;
13046 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13048 static tree
13049 tsubst_omp_clauses (tree clauses, bool declare_simd,
13050 tree args, tsubst_flags_t complain, tree in_decl)
13052 tree new_clauses = NULL, nc, oc;
13054 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
13056 nc = copy_node (oc);
13057 OMP_CLAUSE_CHAIN (nc) = new_clauses;
13058 new_clauses = nc;
13060 switch (OMP_CLAUSE_CODE (nc))
13062 case OMP_CLAUSE_LASTPRIVATE:
13063 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
13065 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
13066 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
13067 in_decl, /*integral_constant_expression_p=*/false);
13068 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
13069 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
13071 /* FALLTHRU */
13072 case OMP_CLAUSE_PRIVATE:
13073 case OMP_CLAUSE_SHARED:
13074 case OMP_CLAUSE_FIRSTPRIVATE:
13075 case OMP_CLAUSE_COPYIN:
13076 case OMP_CLAUSE_COPYPRIVATE:
13077 case OMP_CLAUSE_IF:
13078 case OMP_CLAUSE_NUM_THREADS:
13079 case OMP_CLAUSE_SCHEDULE:
13080 case OMP_CLAUSE_COLLAPSE:
13081 case OMP_CLAUSE_FINAL:
13082 case OMP_CLAUSE_DEPEND:
13083 case OMP_CLAUSE_FROM:
13084 case OMP_CLAUSE_TO:
13085 case OMP_CLAUSE_UNIFORM:
13086 case OMP_CLAUSE_MAP:
13087 case OMP_CLAUSE_DEVICE:
13088 case OMP_CLAUSE_DIST_SCHEDULE:
13089 case OMP_CLAUSE_NUM_TEAMS:
13090 case OMP_CLAUSE_THREAD_LIMIT:
13091 case OMP_CLAUSE_SAFELEN:
13092 case OMP_CLAUSE_SIMDLEN:
13093 OMP_CLAUSE_OPERAND (nc, 0)
13094 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13095 in_decl, /*integral_constant_expression_p=*/false);
13096 break;
13097 case OMP_CLAUSE_REDUCTION:
13098 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
13100 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
13101 if (TREE_CODE (placeholder) == SCOPE_REF)
13103 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
13104 complain, in_decl);
13105 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
13106 = build_qualified_name (NULL_TREE, scope,
13107 TREE_OPERAND (placeholder, 1),
13108 false);
13110 else
13111 gcc_assert (identifier_p (placeholder));
13113 OMP_CLAUSE_OPERAND (nc, 0)
13114 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13115 in_decl, /*integral_constant_expression_p=*/false);
13116 break;
13117 case OMP_CLAUSE_LINEAR:
13118 case OMP_CLAUSE_ALIGNED:
13119 OMP_CLAUSE_OPERAND (nc, 0)
13120 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13121 in_decl, /*integral_constant_expression_p=*/false);
13122 OMP_CLAUSE_OPERAND (nc, 1)
13123 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
13124 in_decl, /*integral_constant_expression_p=*/false);
13125 break;
13127 case OMP_CLAUSE_NOWAIT:
13128 case OMP_CLAUSE_ORDERED:
13129 case OMP_CLAUSE_DEFAULT:
13130 case OMP_CLAUSE_UNTIED:
13131 case OMP_CLAUSE_MERGEABLE:
13132 case OMP_CLAUSE_INBRANCH:
13133 case OMP_CLAUSE_NOTINBRANCH:
13134 case OMP_CLAUSE_PROC_BIND:
13135 case OMP_CLAUSE_FOR:
13136 case OMP_CLAUSE_PARALLEL:
13137 case OMP_CLAUSE_SECTIONS:
13138 case OMP_CLAUSE_TASKGROUP:
13139 break;
13140 default:
13141 gcc_unreachable ();
13145 new_clauses = nreverse (new_clauses);
13146 if (!declare_simd)
13147 new_clauses = finish_omp_clauses (new_clauses);
13148 return new_clauses;
13151 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13153 static tree
13154 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
13155 tree in_decl)
13157 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13159 tree purpose, value, chain;
13161 if (t == NULL)
13162 return t;
13164 if (TREE_CODE (t) != TREE_LIST)
13165 return tsubst_copy_and_build (t, args, complain, in_decl,
13166 /*function_p=*/false,
13167 /*integral_constant_expression_p=*/false);
13169 if (t == void_list_node)
13170 return t;
13172 purpose = TREE_PURPOSE (t);
13173 if (purpose)
13174 purpose = RECUR (purpose);
13175 value = TREE_VALUE (t);
13176 if (value)
13178 if (TREE_CODE (value) != LABEL_DECL)
13179 value = RECUR (value);
13180 else
13182 value = lookup_label (DECL_NAME (value));
13183 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13184 TREE_USED (value) = 1;
13187 chain = TREE_CHAIN (t);
13188 if (chain && chain != void_type_node)
13189 chain = RECUR (chain);
13190 return tree_cons (purpose, value, chain);
13191 #undef RECUR
13194 /* Substitute one OMP_FOR iterator. */
13196 static void
13197 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13198 tree condv, tree incrv, tree *clauses,
13199 tree args, tsubst_flags_t complain, tree in_decl,
13200 bool integral_constant_expression_p)
13202 #define RECUR(NODE) \
13203 tsubst_expr ((NODE), args, complain, in_decl, \
13204 integral_constant_expression_p)
13205 tree decl, init, cond, incr;
13207 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13208 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13209 decl = TREE_OPERAND (init, 0);
13210 init = TREE_OPERAND (init, 1);
13211 tree decl_expr = NULL_TREE;
13212 if (init && TREE_CODE (init) == DECL_EXPR)
13214 /* We need to jump through some hoops to handle declarations in the
13215 for-init-statement, since we might need to handle auto deduction,
13216 but we need to keep control of initialization. */
13217 decl_expr = init;
13218 init = DECL_INITIAL (DECL_EXPR_DECL (init));
13219 decl = tsubst_decl (decl, args, complain);
13221 else
13222 decl = RECUR (decl);
13223 init = RECUR (init);
13225 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13226 if (auto_node && init)
13227 TREE_TYPE (decl)
13228 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
13230 gcc_assert (!type_dependent_expression_p (decl));
13232 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13234 if (decl_expr)
13236 /* Declare the variable, but don't let that initialize it. */
13237 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
13238 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
13239 RECUR (decl_expr);
13240 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
13243 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13244 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13245 if (TREE_CODE (incr) == MODIFY_EXPR)
13246 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13247 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13248 RECUR (TREE_OPERAND (incr, 1)),
13249 complain);
13250 else
13251 incr = RECUR (incr);
13252 TREE_VEC_ELT (declv, i) = decl;
13253 TREE_VEC_ELT (initv, i) = init;
13254 TREE_VEC_ELT (condv, i) = cond;
13255 TREE_VEC_ELT (incrv, i) = incr;
13256 return;
13259 if (decl_expr)
13261 /* Declare and initialize the variable. */
13262 RECUR (decl_expr);
13263 init = NULL_TREE;
13265 else if (init)
13267 tree c;
13268 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13270 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13271 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13272 && OMP_CLAUSE_DECL (c) == decl)
13273 break;
13274 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13275 && OMP_CLAUSE_DECL (c) == decl)
13276 error ("iteration variable %qD should not be firstprivate", decl);
13277 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13278 && OMP_CLAUSE_DECL (c) == decl)
13279 error ("iteration variable %qD should not be reduction", decl);
13281 if (c == NULL)
13283 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13284 OMP_CLAUSE_DECL (c) = decl;
13285 c = finish_omp_clauses (c);
13286 if (c)
13288 OMP_CLAUSE_CHAIN (c) = *clauses;
13289 *clauses = c;
13293 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13294 if (COMPARISON_CLASS_P (cond))
13295 cond = build2 (TREE_CODE (cond), boolean_type_node,
13296 RECUR (TREE_OPERAND (cond, 0)),
13297 RECUR (TREE_OPERAND (cond, 1)));
13298 else
13299 cond = RECUR (cond);
13300 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13301 switch (TREE_CODE (incr))
13303 case PREINCREMENT_EXPR:
13304 case PREDECREMENT_EXPR:
13305 case POSTINCREMENT_EXPR:
13306 case POSTDECREMENT_EXPR:
13307 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13308 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13309 break;
13310 case MODIFY_EXPR:
13311 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13312 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13314 tree rhs = TREE_OPERAND (incr, 1);
13315 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13316 RECUR (TREE_OPERAND (incr, 0)),
13317 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13318 RECUR (TREE_OPERAND (rhs, 0)),
13319 RECUR (TREE_OPERAND (rhs, 1))));
13321 else
13322 incr = RECUR (incr);
13323 break;
13324 case MODOP_EXPR:
13325 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13326 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13328 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13329 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13330 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13331 TREE_TYPE (decl), lhs,
13332 RECUR (TREE_OPERAND (incr, 2))));
13334 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13335 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13336 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13338 tree rhs = TREE_OPERAND (incr, 2);
13339 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13340 RECUR (TREE_OPERAND (incr, 0)),
13341 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13342 RECUR (TREE_OPERAND (rhs, 0)),
13343 RECUR (TREE_OPERAND (rhs, 1))));
13345 else
13346 incr = RECUR (incr);
13347 break;
13348 default:
13349 incr = RECUR (incr);
13350 break;
13353 TREE_VEC_ELT (declv, i) = decl;
13354 TREE_VEC_ELT (initv, i) = init;
13355 TREE_VEC_ELT (condv, i) = cond;
13356 TREE_VEC_ELT (incrv, i) = incr;
13357 #undef RECUR
13360 /* Like tsubst_copy for expressions, etc. but also does semantic
13361 processing. */
13363 static tree
13364 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13365 bool integral_constant_expression_p)
13367 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13368 #define RECUR(NODE) \
13369 tsubst_expr ((NODE), args, complain, in_decl, \
13370 integral_constant_expression_p)
13372 tree stmt, tmp;
13373 tree r;
13374 location_t loc;
13376 if (t == NULL_TREE || t == error_mark_node)
13377 return t;
13379 loc = input_location;
13380 if (EXPR_HAS_LOCATION (t))
13381 input_location = EXPR_LOCATION (t);
13382 if (STATEMENT_CODE_P (TREE_CODE (t)))
13383 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13385 switch (TREE_CODE (t))
13387 case STATEMENT_LIST:
13389 tree_stmt_iterator i;
13390 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13391 RECUR (tsi_stmt (i));
13392 break;
13395 case CTOR_INITIALIZER:
13396 finish_mem_initializers (tsubst_initializer_list
13397 (TREE_OPERAND (t, 0), args));
13398 break;
13400 case RETURN_EXPR:
13401 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13402 break;
13404 case EXPR_STMT:
13405 tmp = RECUR (EXPR_STMT_EXPR (t));
13406 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13407 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13408 else
13409 finish_expr_stmt (tmp);
13410 break;
13412 case USING_STMT:
13413 do_using_directive (USING_STMT_NAMESPACE (t));
13414 break;
13416 case DECL_EXPR:
13418 tree decl, pattern_decl;
13419 tree init;
13421 pattern_decl = decl = DECL_EXPR_DECL (t);
13422 if (TREE_CODE (decl) == LABEL_DECL)
13423 finish_label_decl (DECL_NAME (decl));
13424 else if (TREE_CODE (decl) == USING_DECL)
13426 tree scope = USING_DECL_SCOPE (decl);
13427 tree name = DECL_NAME (decl);
13428 tree decl;
13430 scope = tsubst (scope, args, complain, in_decl);
13431 decl = lookup_qualified_name (scope, name,
13432 /*is_type_p=*/false,
13433 /*complain=*/false);
13434 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13435 qualified_name_lookup_error (scope, name, decl, input_location);
13436 else
13437 do_local_using_decl (decl, scope, name);
13439 else if (DECL_PACK_P (decl))
13441 /* Don't build up decls for a variadic capture proxy, we'll
13442 instantiate the elements directly as needed. */
13443 break;
13445 else
13447 init = DECL_INITIAL (decl);
13448 decl = tsubst (decl, args, complain, in_decl);
13449 if (decl != error_mark_node)
13451 /* By marking the declaration as instantiated, we avoid
13452 trying to instantiate it. Since instantiate_decl can't
13453 handle local variables, and since we've already done
13454 all that needs to be done, that's the right thing to
13455 do. */
13456 if (VAR_P (decl))
13457 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13458 if (VAR_P (decl)
13459 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13460 /* Anonymous aggregates are a special case. */
13461 finish_anon_union (decl);
13462 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13464 DECL_CONTEXT (decl) = current_function_decl;
13465 if (DECL_NAME (decl) == this_identifier)
13467 tree lam = DECL_CONTEXT (current_function_decl);
13468 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13469 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13471 insert_capture_proxy (decl);
13473 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13474 /* We already did a pushtag. */;
13475 else if (TREE_CODE (decl) == FUNCTION_DECL
13476 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13477 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13479 DECL_CONTEXT (decl) = NULL_TREE;
13480 pushdecl (decl);
13481 DECL_CONTEXT (decl) = current_function_decl;
13482 cp_check_omp_declare_reduction (decl);
13484 else
13486 int const_init = false;
13487 maybe_push_decl (decl);
13488 if (VAR_P (decl)
13489 && DECL_PRETTY_FUNCTION_P (decl))
13491 /* For __PRETTY_FUNCTION__ we have to adjust the
13492 initializer. */
13493 const char *const name
13494 = cxx_printable_name (current_function_decl, 2);
13495 init = cp_fname_init (name, &TREE_TYPE (decl));
13497 else
13499 tree t = RECUR (init);
13501 if (init && !t)
13503 /* If we had an initializer but it
13504 instantiated to nothing,
13505 value-initialize the object. This will
13506 only occur when the initializer was a
13507 pack expansion where the parameter packs
13508 used in that expansion were of length
13509 zero. */
13510 init = build_value_init (TREE_TYPE (decl),
13511 complain);
13512 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13513 init = get_target_expr_sfinae (init, complain);
13515 else
13516 init = t;
13519 if (VAR_P (decl))
13520 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13521 (pattern_decl));
13522 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13527 break;
13530 case FOR_STMT:
13531 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13532 RECUR (FOR_INIT_STMT (t));
13533 finish_for_init_stmt (stmt);
13534 tmp = RECUR (FOR_COND (t));
13535 finish_for_cond (tmp, stmt, false);
13536 tmp = RECUR (FOR_EXPR (t));
13537 finish_for_expr (tmp, stmt);
13538 RECUR (FOR_BODY (t));
13539 finish_for_stmt (stmt);
13540 break;
13542 case RANGE_FOR_STMT:
13544 tree decl, expr;
13545 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13546 decl = RANGE_FOR_DECL (t);
13547 decl = tsubst (decl, args, complain, in_decl);
13548 maybe_push_decl (decl);
13549 expr = RECUR (RANGE_FOR_EXPR (t));
13550 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13551 RECUR (RANGE_FOR_BODY (t));
13552 finish_for_stmt (stmt);
13554 break;
13556 case WHILE_STMT:
13557 stmt = begin_while_stmt ();
13558 tmp = RECUR (WHILE_COND (t));
13559 finish_while_stmt_cond (tmp, stmt, false);
13560 RECUR (WHILE_BODY (t));
13561 finish_while_stmt (stmt);
13562 break;
13564 case DO_STMT:
13565 stmt = begin_do_stmt ();
13566 RECUR (DO_BODY (t));
13567 finish_do_body (stmt);
13568 tmp = RECUR (DO_COND (t));
13569 finish_do_stmt (tmp, stmt, false);
13570 break;
13572 case IF_STMT:
13573 stmt = begin_if_stmt ();
13574 tmp = RECUR (IF_COND (t));
13575 finish_if_stmt_cond (tmp, stmt);
13576 RECUR (THEN_CLAUSE (t));
13577 finish_then_clause (stmt);
13579 if (ELSE_CLAUSE (t))
13581 begin_else_clause (stmt);
13582 RECUR (ELSE_CLAUSE (t));
13583 finish_else_clause (stmt);
13586 finish_if_stmt (stmt);
13587 break;
13589 case BIND_EXPR:
13590 if (BIND_EXPR_BODY_BLOCK (t))
13591 stmt = begin_function_body ();
13592 else
13593 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13594 ? BCS_TRY_BLOCK : 0);
13596 RECUR (BIND_EXPR_BODY (t));
13598 if (BIND_EXPR_BODY_BLOCK (t))
13599 finish_function_body (stmt);
13600 else
13601 finish_compound_stmt (stmt);
13602 break;
13604 case BREAK_STMT:
13605 finish_break_stmt ();
13606 break;
13608 case CONTINUE_STMT:
13609 finish_continue_stmt ();
13610 break;
13612 case SWITCH_STMT:
13613 stmt = begin_switch_stmt ();
13614 tmp = RECUR (SWITCH_STMT_COND (t));
13615 finish_switch_cond (tmp, stmt);
13616 RECUR (SWITCH_STMT_BODY (t));
13617 finish_switch_stmt (stmt);
13618 break;
13620 case CASE_LABEL_EXPR:
13621 finish_case_label (EXPR_LOCATION (t),
13622 RECUR (CASE_LOW (t)),
13623 RECUR (CASE_HIGH (t)));
13624 break;
13626 case LABEL_EXPR:
13628 tree decl = LABEL_EXPR_LABEL (t);
13629 tree label;
13631 label = finish_label_stmt (DECL_NAME (decl));
13632 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13633 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13635 break;
13637 case GOTO_EXPR:
13638 tmp = GOTO_DESTINATION (t);
13639 if (TREE_CODE (tmp) != LABEL_DECL)
13640 /* Computed goto's must be tsubst'd into. On the other hand,
13641 non-computed gotos must not be; the identifier in question
13642 will have no binding. */
13643 tmp = RECUR (tmp);
13644 else
13645 tmp = DECL_NAME (tmp);
13646 finish_goto_stmt (tmp);
13647 break;
13649 case ASM_EXPR:
13650 tmp = finish_asm_stmt
13651 (ASM_VOLATILE_P (t),
13652 RECUR (ASM_STRING (t)),
13653 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13654 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13655 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13656 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13658 tree asm_expr = tmp;
13659 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13660 asm_expr = TREE_OPERAND (asm_expr, 0);
13661 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13663 break;
13665 case TRY_BLOCK:
13666 if (CLEANUP_P (t))
13668 stmt = begin_try_block ();
13669 RECUR (TRY_STMTS (t));
13670 finish_cleanup_try_block (stmt);
13671 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13673 else
13675 tree compound_stmt = NULL_TREE;
13677 if (FN_TRY_BLOCK_P (t))
13678 stmt = begin_function_try_block (&compound_stmt);
13679 else
13680 stmt = begin_try_block ();
13682 RECUR (TRY_STMTS (t));
13684 if (FN_TRY_BLOCK_P (t))
13685 finish_function_try_block (stmt);
13686 else
13687 finish_try_block (stmt);
13689 RECUR (TRY_HANDLERS (t));
13690 if (FN_TRY_BLOCK_P (t))
13691 finish_function_handler_sequence (stmt, compound_stmt);
13692 else
13693 finish_handler_sequence (stmt);
13695 break;
13697 case HANDLER:
13699 tree decl = HANDLER_PARMS (t);
13701 if (decl)
13703 decl = tsubst (decl, args, complain, in_decl);
13704 /* Prevent instantiate_decl from trying to instantiate
13705 this variable. We've already done all that needs to be
13706 done. */
13707 if (decl != error_mark_node)
13708 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13710 stmt = begin_handler ();
13711 finish_handler_parms (decl, stmt);
13712 RECUR (HANDLER_BODY (t));
13713 finish_handler (stmt);
13715 break;
13717 case TAG_DEFN:
13718 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13719 if (CLASS_TYPE_P (tmp))
13721 /* Local classes are not independent templates; they are
13722 instantiated along with their containing function. And this
13723 way we don't have to deal with pushing out of one local class
13724 to instantiate a member of another local class. */
13725 tree fn;
13726 /* Closures are handled by the LAMBDA_EXPR. */
13727 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13728 complete_type (tmp);
13729 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13730 if (!DECL_ARTIFICIAL (fn))
13731 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13733 break;
13735 case STATIC_ASSERT:
13737 tree condition;
13739 ++c_inhibit_evaluation_warnings;
13740 condition =
13741 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13742 args,
13743 complain, in_decl,
13744 /*integral_constant_expression_p=*/true);
13745 --c_inhibit_evaluation_warnings;
13747 finish_static_assert (condition,
13748 STATIC_ASSERT_MESSAGE (t),
13749 STATIC_ASSERT_SOURCE_LOCATION (t),
13750 /*member_p=*/false);
13752 break;
13754 case OMP_PARALLEL:
13755 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
13756 args, complain, in_decl);
13757 stmt = begin_omp_parallel ();
13758 RECUR (OMP_PARALLEL_BODY (t));
13759 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13760 = OMP_PARALLEL_COMBINED (t);
13761 break;
13763 case OMP_TASK:
13764 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
13765 args, complain, in_decl);
13766 stmt = begin_omp_task ();
13767 RECUR (OMP_TASK_BODY (t));
13768 finish_omp_task (tmp, stmt);
13769 break;
13771 case OMP_FOR:
13772 case OMP_SIMD:
13773 case CILK_SIMD:
13774 case OMP_DISTRIBUTE:
13776 tree clauses, body, pre_body;
13777 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
13778 tree incrv = NULL_TREE;
13779 int i;
13781 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
13782 args, complain, in_decl);
13783 if (OMP_FOR_INIT (t) != NULL_TREE)
13785 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13786 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13787 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13788 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13791 stmt = begin_omp_structured_block ();
13793 pre_body = push_stmt_list ();
13794 RECUR (OMP_FOR_PRE_BODY (t));
13795 pre_body = pop_stmt_list (pre_body);
13797 if (OMP_FOR_INIT (t) != NULL_TREE)
13798 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13799 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13800 &clauses, args, complain, in_decl,
13801 integral_constant_expression_p);
13803 body = push_stmt_list ();
13804 RECUR (OMP_FOR_BODY (t));
13805 body = pop_stmt_list (body);
13807 if (OMP_FOR_INIT (t) != NULL_TREE)
13808 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
13809 condv, incrv, body, pre_body, clauses);
13810 else
13812 t = make_node (TREE_CODE (t));
13813 TREE_TYPE (t) = void_type_node;
13814 OMP_FOR_BODY (t) = body;
13815 OMP_FOR_PRE_BODY (t) = pre_body;
13816 OMP_FOR_CLAUSES (t) = clauses;
13817 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
13818 add_stmt (t);
13821 add_stmt (finish_omp_structured_block (stmt));
13823 break;
13825 case OMP_SECTIONS:
13826 case OMP_SINGLE:
13827 case OMP_TEAMS:
13828 case OMP_TARGET_DATA:
13829 case OMP_TARGET:
13830 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13831 args, complain, in_decl);
13832 stmt = push_stmt_list ();
13833 RECUR (OMP_BODY (t));
13834 stmt = pop_stmt_list (stmt);
13836 t = copy_node (t);
13837 OMP_BODY (t) = stmt;
13838 OMP_CLAUSES (t) = tmp;
13839 add_stmt (t);
13840 break;
13842 case OMP_TARGET_UPDATE:
13843 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
13844 args, complain, in_decl);
13845 t = copy_node (t);
13846 OMP_CLAUSES (t) = tmp;
13847 add_stmt (t);
13848 break;
13850 case OMP_SECTION:
13851 case OMP_CRITICAL:
13852 case OMP_MASTER:
13853 case OMP_TASKGROUP:
13854 case OMP_ORDERED:
13855 stmt = push_stmt_list ();
13856 RECUR (OMP_BODY (t));
13857 stmt = pop_stmt_list (stmt);
13859 t = copy_node (t);
13860 OMP_BODY (t) = stmt;
13861 add_stmt (t);
13862 break;
13864 case OMP_ATOMIC:
13865 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13866 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13868 tree op1 = TREE_OPERAND (t, 1);
13869 tree rhs1 = NULL_TREE;
13870 tree lhs, rhs;
13871 if (TREE_CODE (op1) == COMPOUND_EXPR)
13873 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13874 op1 = TREE_OPERAND (op1, 1);
13876 lhs = RECUR (TREE_OPERAND (op1, 0));
13877 rhs = RECUR (TREE_OPERAND (op1, 1));
13878 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13879 NULL_TREE, NULL_TREE, rhs1,
13880 OMP_ATOMIC_SEQ_CST (t));
13882 else
13884 tree op1 = TREE_OPERAND (t, 1);
13885 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13886 tree rhs1 = NULL_TREE;
13887 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13888 enum tree_code opcode = NOP_EXPR;
13889 if (code == OMP_ATOMIC_READ)
13891 v = RECUR (TREE_OPERAND (op1, 0));
13892 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13894 else if (code == OMP_ATOMIC_CAPTURE_OLD
13895 || code == OMP_ATOMIC_CAPTURE_NEW)
13897 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13898 v = RECUR (TREE_OPERAND (op1, 0));
13899 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13900 if (TREE_CODE (op11) == COMPOUND_EXPR)
13902 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13903 op11 = TREE_OPERAND (op11, 1);
13905 lhs = RECUR (TREE_OPERAND (op11, 0));
13906 rhs = RECUR (TREE_OPERAND (op11, 1));
13907 opcode = TREE_CODE (op11);
13908 if (opcode == MODIFY_EXPR)
13909 opcode = NOP_EXPR;
13911 else
13913 code = OMP_ATOMIC;
13914 lhs = RECUR (TREE_OPERAND (op1, 0));
13915 rhs = RECUR (TREE_OPERAND (op1, 1));
13917 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
13918 OMP_ATOMIC_SEQ_CST (t));
13920 break;
13922 case TRANSACTION_EXPR:
13924 int flags = 0;
13925 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13926 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13928 if (TRANSACTION_EXPR_IS_STMT (t))
13930 tree body = TRANSACTION_EXPR_BODY (t);
13931 tree noex = NULL_TREE;
13932 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13934 noex = MUST_NOT_THROW_COND (body);
13935 if (noex == NULL_TREE)
13936 noex = boolean_true_node;
13937 body = TREE_OPERAND (body, 0);
13939 stmt = begin_transaction_stmt (input_location, NULL, flags);
13940 RECUR (body);
13941 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13943 else
13945 stmt = build_transaction_expr (EXPR_LOCATION (t),
13946 RECUR (TRANSACTION_EXPR_BODY (t)),
13947 flags, NULL_TREE);
13948 RETURN (stmt);
13951 break;
13953 case MUST_NOT_THROW_EXPR:
13954 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13955 RECUR (MUST_NOT_THROW_COND (t))));
13957 case EXPR_PACK_EXPANSION:
13958 error ("invalid use of pack expansion expression");
13959 RETURN (error_mark_node);
13961 case NONTYPE_ARGUMENT_PACK:
13962 error ("use %<...%> to expand argument pack");
13963 RETURN (error_mark_node);
13965 case CILK_SPAWN_STMT:
13966 cfun->calls_cilk_spawn = 1;
13967 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
13969 case CILK_SYNC_STMT:
13970 RETURN (build_cilk_sync ());
13972 case COMPOUND_EXPR:
13973 tmp = RECUR (TREE_OPERAND (t, 0));
13974 if (tmp == NULL_TREE)
13975 /* If the first operand was a statement, we're done with it. */
13976 RETURN (RECUR (TREE_OPERAND (t, 1)));
13977 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13978 RECUR (TREE_OPERAND (t, 1)),
13979 complain));
13981 case ANNOTATE_EXPR:
13982 tmp = RECUR (TREE_OPERAND (t, 0));
13983 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
13984 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
13986 default:
13987 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13989 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13990 /*function_p=*/false,
13991 integral_constant_expression_p));
13994 RETURN (NULL_TREE);
13995 out:
13996 input_location = loc;
13997 return r;
13998 #undef RECUR
13999 #undef RETURN
14002 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
14003 function. For description of the body see comment above
14004 cp_parser_omp_declare_reduction_exprs. */
14006 static void
14007 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14009 if (t == NULL_TREE || t == error_mark_node)
14010 return;
14012 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
14014 tree_stmt_iterator tsi;
14015 int i;
14016 tree stmts[7];
14017 memset (stmts, 0, sizeof stmts);
14018 for (i = 0, tsi = tsi_start (t);
14019 i < 7 && !tsi_end_p (tsi);
14020 i++, tsi_next (&tsi))
14021 stmts[i] = tsi_stmt (tsi);
14022 gcc_assert (tsi_end_p (tsi));
14024 if (i >= 3)
14026 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
14027 && TREE_CODE (stmts[1]) == DECL_EXPR);
14028 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
14029 args, complain, in_decl);
14030 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
14031 args, complain, in_decl);
14032 DECL_CONTEXT (omp_out) = current_function_decl;
14033 DECL_CONTEXT (omp_in) = current_function_decl;
14034 keep_next_level (true);
14035 tree block = begin_omp_structured_block ();
14036 tsubst_expr (stmts[2], args, complain, in_decl, false);
14037 block = finish_omp_structured_block (block);
14038 block = maybe_cleanup_point_expr_void (block);
14039 add_decl_expr (omp_out);
14040 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
14041 TREE_NO_WARNING (omp_out) = 1;
14042 add_decl_expr (omp_in);
14043 finish_expr_stmt (block);
14045 if (i >= 6)
14047 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
14048 && TREE_CODE (stmts[4]) == DECL_EXPR);
14049 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
14050 args, complain, in_decl);
14051 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
14052 args, complain, in_decl);
14053 DECL_CONTEXT (omp_priv) = current_function_decl;
14054 DECL_CONTEXT (omp_orig) = current_function_decl;
14055 keep_next_level (true);
14056 tree block = begin_omp_structured_block ();
14057 tsubst_expr (stmts[5], args, complain, in_decl, false);
14058 block = finish_omp_structured_block (block);
14059 block = maybe_cleanup_point_expr_void (block);
14060 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
14061 add_decl_expr (omp_priv);
14062 add_decl_expr (omp_orig);
14063 finish_expr_stmt (block);
14064 if (i == 7)
14065 add_decl_expr (omp_orig);
14069 /* T is a postfix-expression that is not being used in a function
14070 call. Return the substituted version of T. */
14072 static tree
14073 tsubst_non_call_postfix_expression (tree t, tree args,
14074 tsubst_flags_t complain,
14075 tree in_decl)
14077 if (TREE_CODE (t) == SCOPE_REF)
14078 t = tsubst_qualified_id (t, args, complain, in_decl,
14079 /*done=*/false, /*address_p=*/false);
14080 else
14081 t = tsubst_copy_and_build (t, args, complain, in_decl,
14082 /*function_p=*/false,
14083 /*integral_constant_expression_p=*/false);
14085 return t;
14088 /* Sentinel to disable certain warnings during template substitution. */
14090 struct warning_sentinel {
14091 int &flag;
14092 int val;
14093 warning_sentinel(int& flag, bool suppress=true)
14094 : flag(flag), val(flag) { if (suppress) flag = 0; }
14095 ~warning_sentinel() { flag = val; }
14098 /* Like tsubst but deals with expressions and performs semantic
14099 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14101 tree
14102 tsubst_copy_and_build (tree t,
14103 tree args,
14104 tsubst_flags_t complain,
14105 tree in_decl,
14106 bool function_p,
14107 bool integral_constant_expression_p)
14109 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14110 #define RECUR(NODE) \
14111 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14112 /*function_p=*/false, \
14113 integral_constant_expression_p)
14115 tree retval, op1;
14116 location_t loc;
14118 if (t == NULL_TREE || t == error_mark_node)
14119 return t;
14121 loc = input_location;
14122 if (EXPR_HAS_LOCATION (t))
14123 input_location = EXPR_LOCATION (t);
14125 /* N3276 decltype magic only applies to calls at the top level or on the
14126 right side of a comma. */
14127 tsubst_flags_t decltype_flag = (complain & tf_decltype);
14128 complain &= ~tf_decltype;
14130 switch (TREE_CODE (t))
14132 case USING_DECL:
14133 t = DECL_NAME (t);
14134 /* Fall through. */
14135 case IDENTIFIER_NODE:
14137 tree decl;
14138 cp_id_kind idk;
14139 bool non_integral_constant_expression_p;
14140 const char *error_msg;
14142 if (IDENTIFIER_TYPENAME_P (t))
14144 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14145 t = mangle_conv_op_name_for_type (new_type);
14148 /* Look up the name. */
14149 decl = lookup_name (t);
14151 /* By convention, expressions use ERROR_MARK_NODE to indicate
14152 failure, not NULL_TREE. */
14153 if (decl == NULL_TREE)
14154 decl = error_mark_node;
14156 decl = finish_id_expression (t, decl, NULL_TREE,
14157 &idk,
14158 integral_constant_expression_p,
14159 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
14160 &non_integral_constant_expression_p,
14161 /*template_p=*/false,
14162 /*done=*/true,
14163 /*address_p=*/false,
14164 /*template_arg_p=*/false,
14165 &error_msg,
14166 input_location);
14167 if (error_msg)
14168 error (error_msg);
14169 if (!function_p && identifier_p (decl))
14170 decl = unqualified_name_lookup_error (decl);
14171 RETURN (decl);
14174 case TEMPLATE_ID_EXPR:
14176 tree object;
14177 tree templ = RECUR (TREE_OPERAND (t, 0));
14178 tree targs = TREE_OPERAND (t, 1);
14180 if (targs)
14181 targs = tsubst_template_args (targs, args, complain, in_decl);
14183 if (TREE_CODE (templ) == COMPONENT_REF)
14185 object = TREE_OPERAND (templ, 0);
14186 templ = TREE_OPERAND (templ, 1);
14188 else
14189 object = NULL_TREE;
14190 templ = lookup_template_function (templ, targs);
14192 if (object)
14193 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
14194 object, templ, NULL_TREE));
14195 else
14196 RETURN (baselink_for_fns (templ));
14199 case INDIRECT_REF:
14201 tree r = RECUR (TREE_OPERAND (t, 0));
14203 if (REFERENCE_REF_P (t))
14205 /* A type conversion to reference type will be enclosed in
14206 such an indirect ref, but the substitution of the cast
14207 will have also added such an indirect ref. */
14208 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
14209 r = convert_from_reference (r);
14211 else
14212 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
14213 complain|decltype_flag);
14214 RETURN (r);
14217 case NOP_EXPR:
14218 RETURN (build_nop
14219 (tsubst (TREE_TYPE (t), args, complain, in_decl),
14220 RECUR (TREE_OPERAND (t, 0))));
14222 case IMPLICIT_CONV_EXPR:
14224 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14225 tree expr = RECUR (TREE_OPERAND (t, 0));
14226 int flags = LOOKUP_IMPLICIT;
14227 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14228 flags = LOOKUP_NORMAL;
14229 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14230 flags));
14233 case CONVERT_EXPR:
14234 RETURN (build1
14235 (CONVERT_EXPR,
14236 tsubst (TREE_TYPE (t), args, complain, in_decl),
14237 RECUR (TREE_OPERAND (t, 0))));
14239 case CAST_EXPR:
14240 case REINTERPRET_CAST_EXPR:
14241 case CONST_CAST_EXPR:
14242 case DYNAMIC_CAST_EXPR:
14243 case STATIC_CAST_EXPR:
14245 tree type;
14246 tree op, r = NULL_TREE;
14248 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14249 if (integral_constant_expression_p
14250 && !cast_valid_in_integral_constant_expression_p (type))
14252 if (complain & tf_error)
14253 error ("a cast to a type other than an integral or "
14254 "enumeration type cannot appear in a constant-expression");
14255 RETURN (error_mark_node);
14258 op = RECUR (TREE_OPERAND (t, 0));
14260 warning_sentinel s(warn_useless_cast);
14261 switch (TREE_CODE (t))
14263 case CAST_EXPR:
14264 r = build_functional_cast (type, op, complain);
14265 break;
14266 case REINTERPRET_CAST_EXPR:
14267 r = build_reinterpret_cast (type, op, complain);
14268 break;
14269 case CONST_CAST_EXPR:
14270 r = build_const_cast (type, op, complain);
14271 break;
14272 case DYNAMIC_CAST_EXPR:
14273 r = build_dynamic_cast (type, op, complain);
14274 break;
14275 case STATIC_CAST_EXPR:
14276 r = build_static_cast (type, op, complain);
14277 break;
14278 default:
14279 gcc_unreachable ();
14282 RETURN (r);
14285 case POSTDECREMENT_EXPR:
14286 case POSTINCREMENT_EXPR:
14287 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14288 args, complain, in_decl);
14289 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14290 complain|decltype_flag));
14292 case PREDECREMENT_EXPR:
14293 case PREINCREMENT_EXPR:
14294 case NEGATE_EXPR:
14295 case BIT_NOT_EXPR:
14296 case ABS_EXPR:
14297 case TRUTH_NOT_EXPR:
14298 case UNARY_PLUS_EXPR: /* Unary + */
14299 case REALPART_EXPR:
14300 case IMAGPART_EXPR:
14301 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14302 RECUR (TREE_OPERAND (t, 0)),
14303 complain|decltype_flag));
14305 case FIX_TRUNC_EXPR:
14306 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14307 0, complain));
14309 case ADDR_EXPR:
14310 op1 = TREE_OPERAND (t, 0);
14311 if (TREE_CODE (op1) == LABEL_DECL)
14312 RETURN (finish_label_address_expr (DECL_NAME (op1),
14313 EXPR_LOCATION (op1)));
14314 if (TREE_CODE (op1) == SCOPE_REF)
14315 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14316 /*done=*/true, /*address_p=*/true);
14317 else
14318 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14319 in_decl);
14320 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14321 complain|decltype_flag));
14323 case PLUS_EXPR:
14324 case MINUS_EXPR:
14325 case MULT_EXPR:
14326 case TRUNC_DIV_EXPR:
14327 case CEIL_DIV_EXPR:
14328 case FLOOR_DIV_EXPR:
14329 case ROUND_DIV_EXPR:
14330 case EXACT_DIV_EXPR:
14331 case BIT_AND_EXPR:
14332 case BIT_IOR_EXPR:
14333 case BIT_XOR_EXPR:
14334 case TRUNC_MOD_EXPR:
14335 case FLOOR_MOD_EXPR:
14336 case TRUTH_ANDIF_EXPR:
14337 case TRUTH_ORIF_EXPR:
14338 case TRUTH_AND_EXPR:
14339 case TRUTH_OR_EXPR:
14340 case RSHIFT_EXPR:
14341 case LSHIFT_EXPR:
14342 case RROTATE_EXPR:
14343 case LROTATE_EXPR:
14344 case EQ_EXPR:
14345 case NE_EXPR:
14346 case MAX_EXPR:
14347 case MIN_EXPR:
14348 case LE_EXPR:
14349 case GE_EXPR:
14350 case LT_EXPR:
14351 case GT_EXPR:
14352 case MEMBER_REF:
14353 case DOTSTAR_EXPR:
14355 warning_sentinel s1(warn_type_limits);
14356 warning_sentinel s2(warn_div_by_zero);
14357 tree r = build_x_binary_op
14358 (input_location, TREE_CODE (t),
14359 RECUR (TREE_OPERAND (t, 0)),
14360 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14361 ? ERROR_MARK
14362 : TREE_CODE (TREE_OPERAND (t, 0))),
14363 RECUR (TREE_OPERAND (t, 1)),
14364 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14365 ? ERROR_MARK
14366 : TREE_CODE (TREE_OPERAND (t, 1))),
14367 /*overload=*/NULL,
14368 complain|decltype_flag);
14369 if (EXPR_P (r) && TREE_NO_WARNING (t))
14370 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14372 RETURN (r);
14375 case POINTER_PLUS_EXPR:
14376 return fold_build_pointer_plus (RECUR (TREE_OPERAND (t, 0)),
14377 RECUR (TREE_OPERAND (t, 1)));
14379 case SCOPE_REF:
14380 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14381 /*address_p=*/false));
14382 case ARRAY_REF:
14383 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14384 args, complain, in_decl);
14385 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14386 RECUR (TREE_OPERAND (t, 1)),
14387 complain|decltype_flag));
14389 case ARRAY_NOTATION_REF:
14391 tree start_index, length, stride;
14392 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14393 args, complain, in_decl);
14394 start_index = RECUR (ARRAY_NOTATION_START (t));
14395 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14396 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14397 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14398 length, stride, TREE_TYPE (op1)));
14400 case SIZEOF_EXPR:
14401 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14402 RETURN (tsubst_copy (t, args, complain, in_decl));
14403 /* Fall through */
14405 case ALIGNOF_EXPR:
14407 tree r;
14409 op1 = TREE_OPERAND (t, 0);
14410 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14411 op1 = TREE_TYPE (op1);
14412 if (!args)
14414 /* When there are no ARGS, we are trying to evaluate a
14415 non-dependent expression from the parser. Trying to do
14416 the substitutions may not work. */
14417 if (!TYPE_P (op1))
14418 op1 = TREE_TYPE (op1);
14420 else
14422 ++cp_unevaluated_operand;
14423 ++c_inhibit_evaluation_warnings;
14424 if (TYPE_P (op1))
14425 op1 = tsubst (op1, args, complain, in_decl);
14426 else
14427 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14428 /*function_p=*/false,
14429 /*integral_constant_expression_p=*/
14430 false);
14431 --cp_unevaluated_operand;
14432 --c_inhibit_evaluation_warnings;
14434 if (TYPE_P (op1))
14435 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14436 complain & tf_error);
14437 else
14438 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14439 complain & tf_error);
14440 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14442 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14444 if (!processing_template_decl && TYPE_P (op1))
14446 r = build_min (SIZEOF_EXPR, size_type_node,
14447 build1 (NOP_EXPR, op1, error_mark_node));
14448 SIZEOF_EXPR_TYPE_P (r) = 1;
14450 else
14451 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14452 TREE_SIDE_EFFECTS (r) = 0;
14453 TREE_READONLY (r) = 1;
14455 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14457 RETURN (r);
14460 case AT_ENCODE_EXPR:
14462 op1 = TREE_OPERAND (t, 0);
14463 ++cp_unevaluated_operand;
14464 ++c_inhibit_evaluation_warnings;
14465 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14466 /*function_p=*/false,
14467 /*integral_constant_expression_p=*/false);
14468 --cp_unevaluated_operand;
14469 --c_inhibit_evaluation_warnings;
14470 RETURN (objc_build_encode_expr (op1));
14473 case NOEXCEPT_EXPR:
14474 op1 = TREE_OPERAND (t, 0);
14475 ++cp_unevaluated_operand;
14476 ++c_inhibit_evaluation_warnings;
14477 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14478 /*function_p=*/false,
14479 /*integral_constant_expression_p=*/false);
14480 --cp_unevaluated_operand;
14481 --c_inhibit_evaluation_warnings;
14482 RETURN (finish_noexcept_expr (op1, complain));
14484 case MODOP_EXPR:
14486 warning_sentinel s(warn_div_by_zero);
14487 tree r = build_x_modify_expr
14488 (EXPR_LOCATION (t),
14489 RECUR (TREE_OPERAND (t, 0)),
14490 TREE_CODE (TREE_OPERAND (t, 1)),
14491 RECUR (TREE_OPERAND (t, 2)),
14492 complain|decltype_flag);
14493 /* TREE_NO_WARNING must be set if either the expression was
14494 parenthesized or it uses an operator such as >>= rather
14495 than plain assignment. In the former case, it was already
14496 set and must be copied. In the latter case,
14497 build_x_modify_expr sets it and it must not be reset
14498 here. */
14499 if (TREE_NO_WARNING (t))
14500 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14502 RETURN (r);
14505 case ARROW_EXPR:
14506 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14507 args, complain, in_decl);
14508 /* Remember that there was a reference to this entity. */
14509 if (DECL_P (op1))
14510 mark_used (op1);
14511 RETURN (build_x_arrow (input_location, op1, complain));
14513 case NEW_EXPR:
14515 tree placement = RECUR (TREE_OPERAND (t, 0));
14516 tree init = RECUR (TREE_OPERAND (t, 3));
14517 vec<tree, va_gc> *placement_vec;
14518 vec<tree, va_gc> *init_vec;
14519 tree ret;
14521 if (placement == NULL_TREE)
14522 placement_vec = NULL;
14523 else
14525 placement_vec = make_tree_vector ();
14526 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14527 vec_safe_push (placement_vec, TREE_VALUE (placement));
14530 /* If there was an initializer in the original tree, but it
14531 instantiated to an empty list, then we should pass a
14532 non-NULL empty vector to tell build_new that it was an
14533 empty initializer() rather than no initializer. This can
14534 only happen when the initializer is a pack expansion whose
14535 parameter packs are of length zero. */
14536 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14537 init_vec = NULL;
14538 else
14540 init_vec = make_tree_vector ();
14541 if (init == void_zero_node)
14542 gcc_assert (init_vec != NULL);
14543 else
14545 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14546 vec_safe_push (init_vec, TREE_VALUE (init));
14550 ret = build_new (&placement_vec,
14551 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14552 RECUR (TREE_OPERAND (t, 2)),
14553 &init_vec,
14554 NEW_EXPR_USE_GLOBAL (t),
14555 complain);
14557 if (placement_vec != NULL)
14558 release_tree_vector (placement_vec);
14559 if (init_vec != NULL)
14560 release_tree_vector (init_vec);
14562 RETURN (ret);
14565 case DELETE_EXPR:
14566 RETURN (delete_sanity
14567 (RECUR (TREE_OPERAND (t, 0)),
14568 RECUR (TREE_OPERAND (t, 1)),
14569 DELETE_EXPR_USE_VEC (t),
14570 DELETE_EXPR_USE_GLOBAL (t),
14571 complain));
14573 case COMPOUND_EXPR:
14575 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14576 complain & ~tf_decltype, in_decl,
14577 /*function_p=*/false,
14578 integral_constant_expression_p);
14579 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14580 op0,
14581 RECUR (TREE_OPERAND (t, 1)),
14582 complain|decltype_flag));
14585 case CALL_EXPR:
14587 tree function;
14588 vec<tree, va_gc> *call_args;
14589 unsigned int nargs, i;
14590 bool qualified_p;
14591 bool koenig_p;
14592 tree ret;
14594 function = CALL_EXPR_FN (t);
14595 /* When we parsed the expression, we determined whether or
14596 not Koenig lookup should be performed. */
14597 koenig_p = KOENIG_LOOKUP_P (t);
14598 if (TREE_CODE (function) == SCOPE_REF)
14600 qualified_p = true;
14601 function = tsubst_qualified_id (function, args, complain, in_decl,
14602 /*done=*/false,
14603 /*address_p=*/false);
14605 else if (koenig_p && identifier_p (function))
14607 /* Do nothing; calling tsubst_copy_and_build on an identifier
14608 would incorrectly perform unqualified lookup again.
14610 Note that we can also have an IDENTIFIER_NODE if the earlier
14611 unqualified lookup found a member function; in that case
14612 koenig_p will be false and we do want to do the lookup
14613 again to find the instantiated member function.
14615 FIXME but doing that causes c++/15272, so we need to stop
14616 using IDENTIFIER_NODE in that situation. */
14617 qualified_p = false;
14619 else
14621 if (TREE_CODE (function) == COMPONENT_REF)
14623 tree op = TREE_OPERAND (function, 1);
14625 qualified_p = (TREE_CODE (op) == SCOPE_REF
14626 || (BASELINK_P (op)
14627 && BASELINK_QUALIFIED_P (op)));
14629 else
14630 qualified_p = false;
14632 if (TREE_CODE (function) == ADDR_EXPR
14633 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14634 /* Avoid error about taking the address of a constructor. */
14635 function = TREE_OPERAND (function, 0);
14637 function = tsubst_copy_and_build (function, args, complain,
14638 in_decl,
14639 !qualified_p,
14640 integral_constant_expression_p);
14642 if (BASELINK_P (function))
14643 qualified_p = true;
14646 nargs = call_expr_nargs (t);
14647 call_args = make_tree_vector ();
14648 for (i = 0; i < nargs; ++i)
14650 tree arg = CALL_EXPR_ARG (t, i);
14652 if (!PACK_EXPANSION_P (arg))
14653 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
14654 else
14656 /* Expand the pack expansion and push each entry onto
14657 CALL_ARGS. */
14658 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
14659 if (TREE_CODE (arg) == TREE_VEC)
14661 unsigned int len, j;
14663 len = TREE_VEC_LENGTH (arg);
14664 for (j = 0; j < len; ++j)
14666 tree value = TREE_VEC_ELT (arg, j);
14667 if (value != NULL_TREE)
14668 value = convert_from_reference (value);
14669 vec_safe_push (call_args, value);
14672 else
14674 /* A partial substitution. Add one entry. */
14675 vec_safe_push (call_args, arg);
14680 /* We do not perform argument-dependent lookup if normal
14681 lookup finds a non-function, in accordance with the
14682 expected resolution of DR 218. */
14683 if (koenig_p
14684 && ((is_overloaded_fn (function)
14685 /* If lookup found a member function, the Koenig lookup is
14686 not appropriate, even if an unqualified-name was used
14687 to denote the function. */
14688 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
14689 || identifier_p (function))
14690 /* Only do this when substitution turns a dependent call
14691 into a non-dependent call. */
14692 && type_dependent_expression_p_push (t)
14693 && !any_type_dependent_arguments_p (call_args))
14694 function = perform_koenig_lookup (function, call_args, tf_none);
14696 if (identifier_p (function)
14697 && !any_type_dependent_arguments_p (call_args))
14699 if (koenig_p && (complain & tf_warning_or_error))
14701 /* For backwards compatibility and good diagnostics, try
14702 the unqualified lookup again if we aren't in SFINAE
14703 context. */
14704 tree unq = (tsubst_copy_and_build
14705 (function, args, complain, in_decl, true,
14706 integral_constant_expression_p));
14707 if (unq == error_mark_node)
14708 RETURN (error_mark_node);
14710 if (unq != function)
14712 tree fn = unq;
14713 if (INDIRECT_REF_P (fn))
14714 fn = TREE_OPERAND (fn, 0);
14715 if (TREE_CODE (fn) == COMPONENT_REF)
14716 fn = TREE_OPERAND (fn, 1);
14717 if (is_overloaded_fn (fn))
14718 fn = get_first_fn (fn);
14719 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
14720 "%qD was not declared in this scope, "
14721 "and no declarations were found by "
14722 "argument-dependent lookup at the point "
14723 "of instantiation", function))
14725 if (!DECL_P (fn))
14726 /* Can't say anything more. */;
14727 else if (DECL_CLASS_SCOPE_P (fn))
14729 location_t loc = EXPR_LOC_OR_LOC (t,
14730 input_location);
14731 inform (loc,
14732 "declarations in dependent base %qT are "
14733 "not found by unqualified lookup",
14734 DECL_CLASS_CONTEXT (fn));
14735 if (current_class_ptr)
14736 inform (loc,
14737 "use %<this->%D%> instead", function);
14738 else
14739 inform (loc,
14740 "use %<%T::%D%> instead",
14741 current_class_name, function);
14743 else
14744 inform (0, "%q+D declared here, later in the "
14745 "translation unit", fn);
14747 function = unq;
14750 if (identifier_p (function))
14752 if (complain & tf_error)
14753 unqualified_name_lookup_error (function);
14754 release_tree_vector (call_args);
14755 RETURN (error_mark_node);
14759 /* Remember that there was a reference to this entity. */
14760 if (DECL_P (function))
14761 mark_used (function);
14763 /* Put back tf_decltype for the actual call. */
14764 complain |= decltype_flag;
14766 if (TREE_CODE (function) == OFFSET_REF)
14767 ret = build_offset_ref_call_from_tree (function, &call_args,
14768 complain);
14769 else if (TREE_CODE (function) == COMPONENT_REF)
14771 tree instance = TREE_OPERAND (function, 0);
14772 tree fn = TREE_OPERAND (function, 1);
14774 if (processing_template_decl
14775 && (type_dependent_expression_p (instance)
14776 || (!BASELINK_P (fn)
14777 && TREE_CODE (fn) != FIELD_DECL)
14778 || type_dependent_expression_p (fn)
14779 || any_type_dependent_arguments_p (call_args)))
14780 ret = build_nt_call_vec (function, call_args);
14781 else if (!BASELINK_P (fn))
14782 ret = finish_call_expr (function, &call_args,
14783 /*disallow_virtual=*/false,
14784 /*koenig_p=*/false,
14785 complain);
14786 else
14787 ret = (build_new_method_call
14788 (instance, fn,
14789 &call_args, NULL_TREE,
14790 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14791 /*fn_p=*/NULL,
14792 complain));
14794 else
14795 ret = finish_call_expr (function, &call_args,
14796 /*disallow_virtual=*/qualified_p,
14797 koenig_p,
14798 complain);
14800 release_tree_vector (call_args);
14802 RETURN (ret);
14805 case COND_EXPR:
14807 tree cond = RECUR (TREE_OPERAND (t, 0));
14808 tree exp1, exp2;
14810 if (TREE_CODE (cond) == INTEGER_CST)
14812 if (integer_zerop (cond))
14814 ++c_inhibit_evaluation_warnings;
14815 exp1 = RECUR (TREE_OPERAND (t, 1));
14816 --c_inhibit_evaluation_warnings;
14817 exp2 = RECUR (TREE_OPERAND (t, 2));
14819 else
14821 exp1 = RECUR (TREE_OPERAND (t, 1));
14822 ++c_inhibit_evaluation_warnings;
14823 exp2 = RECUR (TREE_OPERAND (t, 2));
14824 --c_inhibit_evaluation_warnings;
14827 else
14829 exp1 = RECUR (TREE_OPERAND (t, 1));
14830 exp2 = RECUR (TREE_OPERAND (t, 2));
14833 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14834 cond, exp1, exp2, complain));
14837 case PSEUDO_DTOR_EXPR:
14838 RETURN (finish_pseudo_destructor_expr
14839 (RECUR (TREE_OPERAND (t, 0)),
14840 RECUR (TREE_OPERAND (t, 1)),
14841 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14842 input_location));
14844 case TREE_LIST:
14846 tree purpose, value, chain;
14848 if (t == void_list_node)
14849 RETURN (t);
14851 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14852 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14854 /* We have pack expansions, so expand those and
14855 create a new list out of it. */
14856 tree purposevec = NULL_TREE;
14857 tree valuevec = NULL_TREE;
14858 tree chain;
14859 int i, len = -1;
14861 /* Expand the argument expressions. */
14862 if (TREE_PURPOSE (t))
14863 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14864 complain, in_decl);
14865 if (TREE_VALUE (t))
14866 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14867 complain, in_decl);
14869 /* Build the rest of the list. */
14870 chain = TREE_CHAIN (t);
14871 if (chain && chain != void_type_node)
14872 chain = RECUR (chain);
14874 /* Determine the number of arguments. */
14875 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14877 len = TREE_VEC_LENGTH (purposevec);
14878 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14880 else if (TREE_CODE (valuevec) == TREE_VEC)
14881 len = TREE_VEC_LENGTH (valuevec);
14882 else
14884 /* Since we only performed a partial substitution into
14885 the argument pack, we only RETURN (a single list
14886 node. */
14887 if (purposevec == TREE_PURPOSE (t)
14888 && valuevec == TREE_VALUE (t)
14889 && chain == TREE_CHAIN (t))
14890 RETURN (t);
14892 RETURN (tree_cons (purposevec, valuevec, chain));
14895 /* Convert the argument vectors into a TREE_LIST */
14896 i = len;
14897 while (i > 0)
14899 /* Grab the Ith values. */
14900 i--;
14901 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14902 : NULL_TREE;
14903 value
14904 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14905 : NULL_TREE;
14907 /* Build the list (backwards). */
14908 chain = tree_cons (purpose, value, chain);
14911 RETURN (chain);
14914 purpose = TREE_PURPOSE (t);
14915 if (purpose)
14916 purpose = RECUR (purpose);
14917 value = TREE_VALUE (t);
14918 if (value)
14919 value = RECUR (value);
14920 chain = TREE_CHAIN (t);
14921 if (chain && chain != void_type_node)
14922 chain = RECUR (chain);
14923 if (purpose == TREE_PURPOSE (t)
14924 && value == TREE_VALUE (t)
14925 && chain == TREE_CHAIN (t))
14926 RETURN (t);
14927 RETURN (tree_cons (purpose, value, chain));
14930 case COMPONENT_REF:
14932 tree object;
14933 tree object_type;
14934 tree member;
14935 tree r;
14937 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14938 args, complain, in_decl);
14939 /* Remember that there was a reference to this entity. */
14940 if (DECL_P (object))
14941 mark_used (object);
14942 object_type = TREE_TYPE (object);
14944 member = TREE_OPERAND (t, 1);
14945 if (BASELINK_P (member))
14946 member = tsubst_baselink (member,
14947 non_reference (TREE_TYPE (object)),
14948 args, complain, in_decl);
14949 else
14950 member = tsubst_copy (member, args, complain, in_decl);
14951 if (member == error_mark_node)
14952 RETURN (error_mark_node);
14954 if (type_dependent_expression_p (object))
14955 /* We can't do much here. */;
14956 else if (!CLASS_TYPE_P (object_type))
14958 if (scalarish_type_p (object_type))
14960 tree s = NULL_TREE;
14961 tree dtor = member;
14963 if (TREE_CODE (dtor) == SCOPE_REF)
14965 s = TREE_OPERAND (dtor, 0);
14966 dtor = TREE_OPERAND (dtor, 1);
14968 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14970 dtor = TREE_OPERAND (dtor, 0);
14971 if (TYPE_P (dtor))
14972 RETURN (finish_pseudo_destructor_expr
14973 (object, s, dtor, input_location));
14977 else if (TREE_CODE (member) == SCOPE_REF
14978 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14980 /* Lookup the template functions now that we know what the
14981 scope is. */
14982 tree scope = TREE_OPERAND (member, 0);
14983 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14984 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14985 member = lookup_qualified_name (scope, tmpl,
14986 /*is_type_p=*/false,
14987 /*complain=*/false);
14988 if (BASELINK_P (member))
14990 BASELINK_FUNCTIONS (member)
14991 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14992 args);
14993 member = (adjust_result_of_qualified_name_lookup
14994 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14995 object_type));
14997 else
14999 qualified_name_lookup_error (scope, tmpl, member,
15000 input_location);
15001 RETURN (error_mark_node);
15004 else if (TREE_CODE (member) == SCOPE_REF
15005 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
15006 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
15008 if (complain & tf_error)
15010 if (TYPE_P (TREE_OPERAND (member, 0)))
15011 error ("%qT is not a class or namespace",
15012 TREE_OPERAND (member, 0));
15013 else
15014 error ("%qD is not a class or namespace",
15015 TREE_OPERAND (member, 0));
15017 RETURN (error_mark_node);
15019 else if (TREE_CODE (member) == FIELD_DECL)
15021 r = finish_non_static_data_member (member, object, NULL_TREE);
15022 if (TREE_CODE (r) == COMPONENT_REF)
15023 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15024 RETURN (r);
15027 r = finish_class_member_access_expr (object, member,
15028 /*template_p=*/false,
15029 complain);
15030 if (TREE_CODE (r) == COMPONENT_REF)
15031 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15032 RETURN (r);
15035 case THROW_EXPR:
15036 RETURN (build_throw
15037 (RECUR (TREE_OPERAND (t, 0))));
15039 case CONSTRUCTOR:
15041 vec<constructor_elt, va_gc> *n;
15042 constructor_elt *ce;
15043 unsigned HOST_WIDE_INT idx;
15044 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15045 bool process_index_p;
15046 int newlen;
15047 bool need_copy_p = false;
15048 tree r;
15050 if (type == error_mark_node)
15051 RETURN (error_mark_node);
15053 /* digest_init will do the wrong thing if we let it. */
15054 if (type && TYPE_PTRMEMFUNC_P (type))
15055 RETURN (t);
15057 /* We do not want to process the index of aggregate
15058 initializers as they are identifier nodes which will be
15059 looked up by digest_init. */
15060 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
15062 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
15063 newlen = vec_safe_length (n);
15064 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
15066 if (ce->index && process_index_p
15067 /* An identifier index is looked up in the type
15068 being initialized, not the current scope. */
15069 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
15070 ce->index = RECUR (ce->index);
15072 if (PACK_EXPANSION_P (ce->value))
15074 /* Substitute into the pack expansion. */
15075 ce->value = tsubst_pack_expansion (ce->value, args, complain,
15076 in_decl);
15078 if (ce->value == error_mark_node
15079 || PACK_EXPANSION_P (ce->value))
15081 else if (TREE_VEC_LENGTH (ce->value) == 1)
15082 /* Just move the argument into place. */
15083 ce->value = TREE_VEC_ELT (ce->value, 0);
15084 else
15086 /* Update the length of the final CONSTRUCTOR
15087 arguments vector, and note that we will need to
15088 copy.*/
15089 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
15090 need_copy_p = true;
15093 else
15094 ce->value = RECUR (ce->value);
15097 if (need_copy_p)
15099 vec<constructor_elt, va_gc> *old_n = n;
15101 vec_alloc (n, newlen);
15102 FOR_EACH_VEC_ELT (*old_n, idx, ce)
15104 if (TREE_CODE (ce->value) == TREE_VEC)
15106 int i, len = TREE_VEC_LENGTH (ce->value);
15107 for (i = 0; i < len; ++i)
15108 CONSTRUCTOR_APPEND_ELT (n, 0,
15109 TREE_VEC_ELT (ce->value, i));
15111 else
15112 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
15116 r = build_constructor (init_list_type_node, n);
15117 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
15119 if (TREE_HAS_CONSTRUCTOR (t))
15120 RETURN (finish_compound_literal (type, r, complain));
15122 TREE_TYPE (r) = type;
15123 RETURN (r);
15126 case TYPEID_EXPR:
15128 tree operand_0 = TREE_OPERAND (t, 0);
15129 if (TYPE_P (operand_0))
15131 operand_0 = tsubst (operand_0, args, complain, in_decl);
15132 RETURN (get_typeid (operand_0, complain));
15134 else
15136 operand_0 = RECUR (operand_0);
15137 RETURN (build_typeid (operand_0, complain));
15141 case VAR_DECL:
15142 if (!args)
15143 RETURN (t);
15144 else if (DECL_PACK_P (t))
15146 /* We don't build decls for an instantiation of a
15147 variadic capture proxy, we instantiate the elements
15148 when needed. */
15149 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
15150 return RECUR (DECL_VALUE_EXPR (t));
15152 /* Fall through */
15154 case PARM_DECL:
15156 tree r = tsubst_copy (t, args, complain, in_decl);
15158 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
15159 /* If the original type was a reference, we'll be wrapped in
15160 the appropriate INDIRECT_REF. */
15161 r = convert_from_reference (r);
15162 RETURN (r);
15165 case VA_ARG_EXPR:
15166 RETURN (build_x_va_arg (EXPR_LOCATION (t),
15167 RECUR (TREE_OPERAND (t, 0)),
15168 tsubst (TREE_TYPE (t), args, complain, in_decl)));
15170 case OFFSETOF_EXPR:
15171 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
15173 case TRAIT_EXPR:
15175 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
15176 complain, in_decl);
15178 tree type2 = TRAIT_EXPR_TYPE2 (t);
15179 if (type2)
15180 type2 = tsubst (type2, args, complain, in_decl);
15182 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
15185 case STMT_EXPR:
15187 tree old_stmt_expr = cur_stmt_expr;
15188 tree stmt_expr = begin_stmt_expr ();
15190 cur_stmt_expr = stmt_expr;
15191 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
15192 integral_constant_expression_p);
15193 stmt_expr = finish_stmt_expr (stmt_expr, false);
15194 cur_stmt_expr = old_stmt_expr;
15196 /* If the resulting list of expression statement is empty,
15197 fold it further into void_zero_node. */
15198 if (empty_expr_stmt_p (stmt_expr))
15199 stmt_expr = void_zero_node;
15201 RETURN (stmt_expr);
15204 case LAMBDA_EXPR:
15206 tree r = build_lambda_expr ();
15208 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
15209 LAMBDA_EXPR_CLOSURE (r) = type;
15210 CLASSTYPE_LAMBDA_EXPR (type) = r;
15212 LAMBDA_EXPR_LOCATION (r)
15213 = LAMBDA_EXPR_LOCATION (t);
15214 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
15215 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
15216 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
15217 LAMBDA_EXPR_DISCRIMINATOR (r)
15218 = (LAMBDA_EXPR_DISCRIMINATOR (t));
15219 /* For a function scope, we want to use tsubst so that we don't
15220 complain about referring to an auto function before its return
15221 type has been deduced. Otherwise, we want to use tsubst_copy so
15222 that we look up the existing field/parameter/variable rather
15223 than build a new one. */
15224 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15225 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15226 scope = tsubst (scope, args, complain, in_decl);
15227 else if (scope && TREE_CODE (scope) == PARM_DECL)
15229 /* Look up the parameter we want directly, as tsubst_copy
15230 doesn't do what we need. */
15231 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15232 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15233 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15234 parm = DECL_CHAIN (parm);
15235 scope = parm;
15236 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15237 if (DECL_CONTEXT (scope) == NULL_TREE)
15238 DECL_CONTEXT (scope) = fn;
15240 else
15241 scope = RECUR (scope);
15242 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15243 LAMBDA_EXPR_RETURN_TYPE (r)
15244 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15246 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15247 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15249 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15250 determine_visibility (TYPE_NAME (type));
15251 /* Now that we know visibility, instantiate the type so we have a
15252 declaration of the op() for later calls to lambda_function. */
15253 complete_type (type);
15255 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15257 RETURN (build_lambda_object (r));
15260 case TARGET_EXPR:
15261 /* We can get here for a constant initializer of non-dependent type.
15262 FIXME stop folding in cp_parser_initializer_clause. */
15264 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15265 complain);
15266 RETURN (r);
15269 case TRANSACTION_EXPR:
15270 RETURN (tsubst_expr(t, args, complain, in_decl,
15271 integral_constant_expression_p));
15273 case PAREN_EXPR:
15274 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15276 case VEC_PERM_EXPR:
15277 RETURN (build_x_vec_perm_expr (input_location,
15278 RECUR (TREE_OPERAND (t, 0)),
15279 RECUR (TREE_OPERAND (t, 1)),
15280 RECUR (TREE_OPERAND (t, 2)),
15281 complain));
15283 default:
15284 /* Handle Objective-C++ constructs, if appropriate. */
15286 tree subst
15287 = objcp_tsubst_copy_and_build (t, args, complain,
15288 in_decl, /*function_p=*/false);
15289 if (subst)
15290 RETURN (subst);
15292 RETURN (tsubst_copy (t, args, complain, in_decl));
15295 #undef RECUR
15296 #undef RETURN
15297 out:
15298 input_location = loc;
15299 return retval;
15302 /* Verify that the instantiated ARGS are valid. For type arguments,
15303 make sure that the type's linkage is ok. For non-type arguments,
15304 make sure they are constants if they are integral or enumerations.
15305 Emit an error under control of COMPLAIN, and return TRUE on error. */
15307 static bool
15308 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15310 if (dependent_template_arg_p (t))
15311 return false;
15312 if (ARGUMENT_PACK_P (t))
15314 tree vec = ARGUMENT_PACK_ARGS (t);
15315 int len = TREE_VEC_LENGTH (vec);
15316 bool result = false;
15317 int i;
15319 for (i = 0; i < len; ++i)
15320 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15321 result = true;
15322 return result;
15324 else if (TYPE_P (t))
15326 /* [basic.link]: A name with no linkage (notably, the name
15327 of a class or enumeration declared in a local scope)
15328 shall not be used to declare an entity with linkage.
15329 This implies that names with no linkage cannot be used as
15330 template arguments
15332 DR 757 relaxes this restriction for C++0x. */
15333 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15334 : no_linkage_check (t, /*relaxed_p=*/false));
15336 if (nt)
15338 /* DR 488 makes use of a type with no linkage cause
15339 type deduction to fail. */
15340 if (complain & tf_error)
15342 if (TYPE_ANONYMOUS_P (nt))
15343 error ("%qT is/uses anonymous type", t);
15344 else
15345 error ("template argument for %qD uses local type %qT",
15346 tmpl, t);
15348 return true;
15350 /* In order to avoid all sorts of complications, we do not
15351 allow variably-modified types as template arguments. */
15352 else if (variably_modified_type_p (t, NULL_TREE))
15354 if (complain & tf_error)
15355 error ("%qT is a variably modified type", t);
15356 return true;
15359 /* Class template and alias template arguments should be OK. */
15360 else if (DECL_TYPE_TEMPLATE_P (t))
15362 /* A non-type argument of integral or enumerated type must be a
15363 constant. */
15364 else if (TREE_TYPE (t)
15365 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15366 && !TREE_CONSTANT (t))
15368 if (complain & tf_error)
15369 error ("integral expression %qE is not constant", t);
15370 return true;
15372 return false;
15375 static bool
15376 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15378 int ix, len = DECL_NTPARMS (tmpl);
15379 bool result = false;
15381 for (ix = 0; ix != len; ix++)
15383 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15384 result = true;
15386 if (result && (complain & tf_error))
15387 error (" trying to instantiate %qD", tmpl);
15388 return result;
15391 /* We're out of SFINAE context now, so generate diagnostics for the access
15392 errors we saw earlier when instantiating D from TMPL and ARGS. */
15394 static void
15395 recheck_decl_substitution (tree d, tree tmpl, tree args)
15397 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15398 tree type = TREE_TYPE (pattern);
15399 location_t loc = input_location;
15401 push_access_scope (d);
15402 push_deferring_access_checks (dk_no_deferred);
15403 input_location = DECL_SOURCE_LOCATION (pattern);
15404 tsubst (type, args, tf_warning_or_error, d);
15405 input_location = loc;
15406 pop_deferring_access_checks ();
15407 pop_access_scope (d);
15410 /* Instantiate the indicated variable, function, or alias template TMPL with
15411 the template arguments in TARG_PTR. */
15413 static tree
15414 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15416 tree targ_ptr = orig_args;
15417 tree fndecl;
15418 tree gen_tmpl;
15419 tree spec;
15420 bool access_ok = true;
15422 if (tmpl == error_mark_node)
15423 return error_mark_node;
15425 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15427 /* If this function is a clone, handle it specially. */
15428 if (DECL_CLONED_FUNCTION_P (tmpl))
15430 tree spec;
15431 tree clone;
15433 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15434 DECL_CLONED_FUNCTION. */
15435 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15436 targ_ptr, complain);
15437 if (spec == error_mark_node)
15438 return error_mark_node;
15440 /* Look for the clone. */
15441 FOR_EACH_CLONE (clone, spec)
15442 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15443 return clone;
15444 /* We should always have found the clone by now. */
15445 gcc_unreachable ();
15446 return NULL_TREE;
15449 if (targ_ptr == error_mark_node)
15450 return error_mark_node;
15452 /* Check to see if we already have this specialization. */
15453 gen_tmpl = most_general_template (tmpl);
15454 if (tmpl != gen_tmpl)
15455 /* The TMPL is a partial instantiation. To get a full set of
15456 arguments we must add the arguments used to perform the
15457 partial instantiation. */
15458 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15459 targ_ptr);
15461 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15462 but it doesn't seem to be on the hot path. */
15463 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15465 gcc_assert (tmpl == gen_tmpl
15466 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15467 == spec)
15468 || fndecl == NULL_TREE);
15470 if (spec != NULL_TREE)
15472 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15474 if (complain & tf_error)
15475 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15476 return error_mark_node;
15478 return spec;
15481 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15482 complain))
15483 return error_mark_node;
15485 /* We are building a FUNCTION_DECL, during which the access of its
15486 parameters and return types have to be checked. However this
15487 FUNCTION_DECL which is the desired context for access checking
15488 is not built yet. We solve this chicken-and-egg problem by
15489 deferring all checks until we have the FUNCTION_DECL. */
15490 push_deferring_access_checks (dk_deferred);
15492 /* Instantiation of the function happens in the context of the function
15493 template, not the context of the overload resolution we're doing. */
15494 push_to_top_level ();
15495 /* If there are dependent arguments, e.g. because we're doing partial
15496 ordering, make sure processing_template_decl stays set. */
15497 if (uses_template_parms (targ_ptr))
15498 ++processing_template_decl;
15499 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15501 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15502 complain, gen_tmpl);
15503 push_nested_class (ctx);
15505 /* Substitute template parameters to obtain the specialization. */
15506 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15507 targ_ptr, complain, gen_tmpl);
15508 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15509 pop_nested_class ();
15510 pop_from_top_level ();
15512 if (fndecl == error_mark_node)
15514 pop_deferring_access_checks ();
15515 return error_mark_node;
15518 /* The DECL_TI_TEMPLATE should always be the immediate parent
15519 template, not the most general template. */
15520 DECL_TI_TEMPLATE (fndecl) = tmpl;
15522 /* Now we know the specialization, compute access previously
15523 deferred. */
15524 push_access_scope (fndecl);
15525 if (!perform_deferred_access_checks (complain))
15526 access_ok = false;
15527 pop_access_scope (fndecl);
15528 pop_deferring_access_checks ();
15530 /* If we've just instantiated the main entry point for a function,
15531 instantiate all the alternate entry points as well. We do this
15532 by cloning the instantiation of the main entry point, not by
15533 instantiating the template clones. */
15534 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15535 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15537 if (!access_ok)
15539 if (!(complain & tf_error))
15541 /* Remember to reinstantiate when we're out of SFINAE so the user
15542 can see the errors. */
15543 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15545 return error_mark_node;
15547 return fndecl;
15550 /* Wrapper for instantiate_template_1. */
15552 tree
15553 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15555 tree ret;
15556 timevar_push (TV_TEMPLATE_INST);
15557 ret = instantiate_template_1 (tmpl, orig_args, complain);
15558 timevar_pop (TV_TEMPLATE_INST);
15559 return ret;
15562 /* Instantiate the alias template TMPL with ARGS. Also push a template
15563 instantiation level, which instantiate_template doesn't do because
15564 functions and variables have sufficient context established by the
15565 callers. */
15567 static tree
15568 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15570 struct pending_template *old_last_pend = last_pending_template;
15571 struct tinst_level *old_error_tinst = last_error_tinst_level;
15572 if (tmpl == error_mark_node || args == error_mark_node)
15573 return error_mark_node;
15574 tree tinst = build_tree_list (tmpl, args);
15575 if (!push_tinst_level (tinst))
15577 ggc_free (tinst);
15578 return error_mark_node;
15581 args =
15582 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15583 args, tmpl, complain,
15584 /*require_all_args=*/true,
15585 /*use_default_args=*/true);
15587 tree r = instantiate_template (tmpl, args, complain);
15588 pop_tinst_level ();
15589 /* We can't free this if a pending_template entry or last_error_tinst_level
15590 is pointing at it. */
15591 if (last_pending_template == old_last_pend
15592 && last_error_tinst_level == old_error_tinst)
15593 ggc_free (tinst);
15595 return r;
15598 /* PARM is a template parameter pack for FN. Returns true iff
15599 PARM is used in a deducible way in the argument list of FN. */
15601 static bool
15602 pack_deducible_p (tree parm, tree fn)
15604 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15605 for (; t; t = TREE_CHAIN (t))
15607 tree type = TREE_VALUE (t);
15608 tree packs;
15609 if (!PACK_EXPANSION_P (type))
15610 continue;
15611 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15612 packs; packs = TREE_CHAIN (packs))
15613 if (TREE_VALUE (packs) == parm)
15615 /* The template parameter pack is used in a function parameter
15616 pack. If this is the end of the parameter list, the
15617 template parameter pack is deducible. */
15618 if (TREE_CHAIN (t) == void_list_node)
15619 return true;
15620 else
15621 /* Otherwise, not. Well, it could be deduced from
15622 a non-pack parameter, but doing so would end up with
15623 a deduction mismatch, so don't bother. */
15624 return false;
15627 /* The template parameter pack isn't used in any function parameter
15628 packs, but it might be used deeper, e.g. tuple<Args...>. */
15629 return true;
15632 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15633 NARGS elements of the arguments that are being used when calling
15634 it. TARGS is a vector into which the deduced template arguments
15635 are placed.
15637 Returns either a FUNCTION_DECL for the matching specialization of FN or
15638 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
15639 true, diagnostics will be printed to explain why it failed.
15641 If FN is a conversion operator, or we are trying to produce a specific
15642 specialization, RETURN_TYPE is the return type desired.
15644 The EXPLICIT_TARGS are explicit template arguments provided via a
15645 template-id.
15647 The parameter STRICT is one of:
15649 DEDUCE_CALL:
15650 We are deducing arguments for a function call, as in
15651 [temp.deduct.call].
15653 DEDUCE_CONV:
15654 We are deducing arguments for a conversion function, as in
15655 [temp.deduct.conv].
15657 DEDUCE_EXACT:
15658 We are deducing arguments when doing an explicit instantiation
15659 as in [temp.explicit], when determining an explicit specialization
15660 as in [temp.expl.spec], or when taking the address of a function
15661 template, as in [temp.deduct.funcaddr]. */
15663 tree
15664 fn_type_unification (tree fn,
15665 tree explicit_targs,
15666 tree targs,
15667 const tree *args,
15668 unsigned int nargs,
15669 tree return_type,
15670 unification_kind_t strict,
15671 int flags,
15672 bool explain_p,
15673 bool decltype_p)
15675 tree parms;
15676 tree fntype;
15677 tree decl = NULL_TREE;
15678 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15679 bool ok;
15680 static int deduction_depth;
15681 struct pending_template *old_last_pend = last_pending_template;
15682 struct tinst_level *old_error_tinst = last_error_tinst_level;
15683 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15684 tree tinst;
15685 tree r = error_mark_node;
15687 if (decltype_p)
15688 complain |= tf_decltype;
15690 /* In C++0x, it's possible to have a function template whose type depends
15691 on itself recursively. This is most obvious with decltype, but can also
15692 occur with enumeration scope (c++/48969). So we need to catch infinite
15693 recursion and reject the substitution at deduction time; this function
15694 will return error_mark_node for any repeated substitution.
15696 This also catches excessive recursion such as when f<N> depends on
15697 f<N-1> across all integers, and returns error_mark_node for all the
15698 substitutions back up to the initial one.
15700 This is, of course, not reentrant. */
15701 if (excessive_deduction_depth)
15702 return error_mark_node;
15703 tinst = build_tree_list (fn, NULL_TREE);
15704 ++deduction_depth;
15706 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15708 fntype = TREE_TYPE (fn);
15709 if (explicit_targs)
15711 /* [temp.deduct]
15713 The specified template arguments must match the template
15714 parameters in kind (i.e., type, nontype, template), and there
15715 must not be more arguments than there are parameters;
15716 otherwise type deduction fails.
15718 Nontype arguments must match the types of the corresponding
15719 nontype template parameters, or must be convertible to the
15720 types of the corresponding nontype parameters as specified in
15721 _temp.arg.nontype_, otherwise type deduction fails.
15723 All references in the function type of the function template
15724 to the corresponding template parameters are replaced by the
15725 specified template argument values. If a substitution in a
15726 template parameter or in the function type of the function
15727 template results in an invalid type, type deduction fails. */
15728 int i, len = TREE_VEC_LENGTH (tparms);
15729 location_t loc = input_location;
15730 bool incomplete = false;
15732 /* Adjust any explicit template arguments before entering the
15733 substitution context. */
15734 explicit_targs
15735 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15736 complain,
15737 /*require_all_args=*/false,
15738 /*use_default_args=*/false));
15739 if (explicit_targs == error_mark_node)
15740 goto fail;
15742 /* Substitute the explicit args into the function type. This is
15743 necessary so that, for instance, explicitly declared function
15744 arguments can match null pointed constants. If we were given
15745 an incomplete set of explicit args, we must not do semantic
15746 processing during substitution as we could create partial
15747 instantiations. */
15748 for (i = 0; i < len; i++)
15750 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15751 bool parameter_pack = false;
15752 tree targ = TREE_VEC_ELT (explicit_targs, i);
15754 /* Dig out the actual parm. */
15755 if (TREE_CODE (parm) == TYPE_DECL
15756 || TREE_CODE (parm) == TEMPLATE_DECL)
15758 parm = TREE_TYPE (parm);
15759 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15761 else if (TREE_CODE (parm) == PARM_DECL)
15763 parm = DECL_INITIAL (parm);
15764 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15767 if (!parameter_pack && targ == NULL_TREE)
15768 /* No explicit argument for this template parameter. */
15769 incomplete = true;
15771 if (parameter_pack && pack_deducible_p (parm, fn))
15773 /* Mark the argument pack as "incomplete". We could
15774 still deduce more arguments during unification.
15775 We remove this mark in type_unification_real. */
15776 if (targ)
15778 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15779 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15780 = ARGUMENT_PACK_ARGS (targ);
15783 /* We have some incomplete argument packs. */
15784 incomplete = true;
15788 TREE_VALUE (tinst) = explicit_targs;
15789 if (!push_tinst_level (tinst))
15791 excessive_deduction_depth = true;
15792 goto fail;
15794 processing_template_decl += incomplete;
15795 input_location = DECL_SOURCE_LOCATION (fn);
15796 /* Ignore any access checks; we'll see them again in
15797 instantiate_template and they might have the wrong
15798 access path at this point. */
15799 push_deferring_access_checks (dk_deferred);
15800 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15801 complain | tf_partial, NULL_TREE);
15802 pop_deferring_access_checks ();
15803 input_location = loc;
15804 processing_template_decl -= incomplete;
15805 pop_tinst_level ();
15807 if (fntype == error_mark_node)
15808 goto fail;
15810 /* Place the explicitly specified arguments in TARGS. */
15811 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15812 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15815 /* Never do unification on the 'this' parameter. */
15816 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15818 if (return_type)
15820 tree *new_args;
15822 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15823 new_args = XALLOCAVEC (tree, nargs + 1);
15824 new_args[0] = return_type;
15825 memcpy (new_args + 1, args, nargs * sizeof (tree));
15826 args = new_args;
15827 ++nargs;
15830 /* We allow incomplete unification without an error message here
15831 because the standard doesn't seem to explicitly prohibit it. Our
15832 callers must be ready to deal with unification failures in any
15833 event. */
15835 TREE_VALUE (tinst) = targs;
15836 /* If we aren't explaining yet, push tinst context so we can see where
15837 any errors (e.g. from class instantiations triggered by instantiation
15838 of default template arguments) come from. If we are explaining, this
15839 context is redundant. */
15840 if (!explain_p && !push_tinst_level (tinst))
15842 excessive_deduction_depth = true;
15843 goto fail;
15846 /* type_unification_real will pass back any access checks from default
15847 template argument substitution. */
15848 vec<deferred_access_check, va_gc> *checks;
15849 checks = NULL;
15851 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15852 targs, parms, args, nargs, /*subr=*/0,
15853 strict, flags, &checks, explain_p);
15854 if (!explain_p)
15855 pop_tinst_level ();
15856 if (!ok)
15857 goto fail;
15859 /* Now that we have bindings for all of the template arguments,
15860 ensure that the arguments deduced for the template template
15861 parameters have compatible template parameter lists. We cannot
15862 check this property before we have deduced all template
15863 arguments, because the template parameter types of a template
15864 template parameter might depend on prior template parameters
15865 deduced after the template template parameter. The following
15866 ill-formed example illustrates this issue:
15868 template<typename T, template<T> class C> void f(C<5>, T);
15870 template<int N> struct X {};
15872 void g() {
15873 f(X<5>(), 5l); // error: template argument deduction fails
15876 The template parameter list of 'C' depends on the template type
15877 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15878 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15879 time that we deduce 'C'. */
15880 if (!template_template_parm_bindings_ok_p
15881 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15883 unify_inconsistent_template_template_parameters (explain_p);
15884 goto fail;
15887 /* All is well so far. Now, check:
15889 [temp.deduct]
15891 When all template arguments have been deduced, all uses of
15892 template parameters in nondeduced contexts are replaced with
15893 the corresponding deduced argument values. If the
15894 substitution results in an invalid type, as described above,
15895 type deduction fails. */
15896 TREE_VALUE (tinst) = targs;
15897 if (!push_tinst_level (tinst))
15899 excessive_deduction_depth = true;
15900 goto fail;
15903 /* Also collect access checks from the instantiation. */
15904 reopen_deferring_access_checks (checks);
15906 decl = instantiate_template (fn, targs, complain);
15908 checks = get_deferred_access_checks ();
15909 pop_deferring_access_checks ();
15911 pop_tinst_level ();
15913 if (decl == error_mark_node)
15914 goto fail;
15916 /* Now perform any access checks encountered during substitution. */
15917 push_access_scope (decl);
15918 ok = perform_access_checks (checks, complain);
15919 pop_access_scope (decl);
15920 if (!ok)
15921 goto fail;
15923 /* If we're looking for an exact match, check that what we got
15924 is indeed an exact match. It might not be if some template
15925 parameters are used in non-deduced contexts. But don't check
15926 for an exact match if we have dependent template arguments;
15927 in that case we're doing partial ordering, and we already know
15928 that we have two candidates that will provide the actual type. */
15929 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
15931 tree substed = TREE_TYPE (decl);
15932 unsigned int i;
15934 tree sarg
15935 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15936 if (return_type)
15937 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15938 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15939 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15941 unify_type_mismatch (explain_p, args[i],
15942 TREE_VALUE (sarg));
15943 goto fail;
15947 r = decl;
15949 fail:
15950 --deduction_depth;
15951 if (excessive_deduction_depth)
15953 if (deduction_depth == 0)
15954 /* Reset once we're all the way out. */
15955 excessive_deduction_depth = false;
15958 /* We can't free this if a pending_template entry or last_error_tinst_level
15959 is pointing at it. */
15960 if (last_pending_template == old_last_pend
15961 && last_error_tinst_level == old_error_tinst)
15962 ggc_free (tinst);
15964 return r;
15967 /* Adjust types before performing type deduction, as described in
15968 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15969 sections are symmetric. PARM is the type of a function parameter
15970 or the return type of the conversion function. ARG is the type of
15971 the argument passed to the call, or the type of the value
15972 initialized with the result of the conversion function.
15973 ARG_EXPR is the original argument expression, which may be null. */
15975 static int
15976 maybe_adjust_types_for_deduction (unification_kind_t strict,
15977 tree* parm,
15978 tree* arg,
15979 tree arg_expr)
15981 int result = 0;
15983 switch (strict)
15985 case DEDUCE_CALL:
15986 break;
15988 case DEDUCE_CONV:
15990 /* Swap PARM and ARG throughout the remainder of this
15991 function; the handling is precisely symmetric since PARM
15992 will initialize ARG rather than vice versa. */
15993 tree* temp = parm;
15994 parm = arg;
15995 arg = temp;
15996 break;
15999 case DEDUCE_EXACT:
16000 /* Core issue #873: Do the DR606 thing (see below) for these cases,
16001 too, but here handle it by stripping the reference from PARM
16002 rather than by adding it to ARG. */
16003 if (TREE_CODE (*parm) == REFERENCE_TYPE
16004 && TYPE_REF_IS_RVALUE (*parm)
16005 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16006 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16007 && TREE_CODE (*arg) == REFERENCE_TYPE
16008 && !TYPE_REF_IS_RVALUE (*arg))
16009 *parm = TREE_TYPE (*parm);
16010 /* Nothing else to do in this case. */
16011 return 0;
16013 default:
16014 gcc_unreachable ();
16017 if (TREE_CODE (*parm) != REFERENCE_TYPE)
16019 /* [temp.deduct.call]
16021 If P is not a reference type:
16023 --If A is an array type, the pointer type produced by the
16024 array-to-pointer standard conversion (_conv.array_) is
16025 used in place of A for type deduction; otherwise,
16027 --If A is a function type, the pointer type produced by
16028 the function-to-pointer standard conversion
16029 (_conv.func_) is used in place of A for type deduction;
16030 otherwise,
16032 --If A is a cv-qualified type, the top level
16033 cv-qualifiers of A's type are ignored for type
16034 deduction. */
16035 if (TREE_CODE (*arg) == ARRAY_TYPE)
16036 *arg = build_pointer_type (TREE_TYPE (*arg));
16037 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
16038 *arg = build_pointer_type (*arg);
16039 else
16040 *arg = TYPE_MAIN_VARIANT (*arg);
16043 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16044 of the form T&&, where T is a template parameter, and the argument
16045 is an lvalue, T is deduced as A& */
16046 if (TREE_CODE (*parm) == REFERENCE_TYPE
16047 && TYPE_REF_IS_RVALUE (*parm)
16048 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16049 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16050 && (arg_expr ? real_lvalue_p (arg_expr)
16051 /* try_one_overload doesn't provide an arg_expr, but
16052 functions are always lvalues. */
16053 : TREE_CODE (*arg) == FUNCTION_TYPE))
16054 *arg = build_reference_type (*arg);
16056 /* [temp.deduct.call]
16058 If P is a cv-qualified type, the top level cv-qualifiers
16059 of P's type are ignored for type deduction. If P is a
16060 reference type, the type referred to by P is used for
16061 type deduction. */
16062 *parm = TYPE_MAIN_VARIANT (*parm);
16063 if (TREE_CODE (*parm) == REFERENCE_TYPE)
16065 *parm = TREE_TYPE (*parm);
16066 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16069 /* DR 322. For conversion deduction, remove a reference type on parm
16070 too (which has been swapped into ARG). */
16071 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
16072 *arg = TREE_TYPE (*arg);
16074 return result;
16077 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16078 template which does contain any deducible template parameters; check if
16079 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16080 unify_one_argument. */
16082 static int
16083 check_non_deducible_conversion (tree parm, tree arg, int strict,
16084 int flags, bool explain_p)
16086 tree type;
16088 if (!TYPE_P (arg))
16089 type = TREE_TYPE (arg);
16090 else
16091 type = arg;
16093 if (same_type_p (parm, type))
16094 return unify_success (explain_p);
16096 if (strict == DEDUCE_CONV)
16098 if (can_convert_arg (type, parm, NULL_TREE, flags,
16099 explain_p ? tf_warning_or_error : tf_none))
16100 return unify_success (explain_p);
16102 else if (strict != DEDUCE_EXACT)
16104 if (can_convert_arg (parm, type,
16105 TYPE_P (arg) ? NULL_TREE : arg,
16106 flags, explain_p ? tf_warning_or_error : tf_none))
16107 return unify_success (explain_p);
16110 if (strict == DEDUCE_EXACT)
16111 return unify_type_mismatch (explain_p, parm, arg);
16112 else
16113 return unify_arg_conversion (explain_p, parm, type, arg);
16116 static bool uses_deducible_template_parms (tree type);
16118 /* Returns true iff the expression EXPR is one from which a template
16119 argument can be deduced. In other words, if it's an undecorated
16120 use of a template non-type parameter. */
16122 static bool
16123 deducible_expression (tree expr)
16125 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
16128 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16129 deducible way; that is, if it has a max value of <PARM> - 1. */
16131 static bool
16132 deducible_array_bound (tree domain)
16134 if (domain == NULL_TREE)
16135 return false;
16137 tree max = TYPE_MAX_VALUE (domain);
16138 if (TREE_CODE (max) != MINUS_EXPR)
16139 return false;
16141 return deducible_expression (TREE_OPERAND (max, 0));
16144 /* Returns true iff the template arguments ARGS use a template parameter
16145 in a deducible way. */
16147 static bool
16148 deducible_template_args (tree args)
16150 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
16152 bool deducible;
16153 tree elt = TREE_VEC_ELT (args, i);
16154 if (ARGUMENT_PACK_P (elt))
16155 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
16156 else
16158 if (PACK_EXPANSION_P (elt))
16159 elt = PACK_EXPANSION_PATTERN (elt);
16160 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
16161 deducible = true;
16162 else if (TYPE_P (elt))
16163 deducible = uses_deducible_template_parms (elt);
16164 else
16165 deducible = deducible_expression (elt);
16167 if (deducible)
16168 return true;
16170 return false;
16173 /* Returns true iff TYPE contains any deducible references to template
16174 parameters, as per 14.8.2.5. */
16176 static bool
16177 uses_deducible_template_parms (tree type)
16179 if (PACK_EXPANSION_P (type))
16180 type = PACK_EXPANSION_PATTERN (type);
16182 /* T
16183 cv-list T
16184 TT<T>
16185 TT<i>
16186 TT<> */
16187 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16188 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16189 return true;
16191 /* T*
16193 T&& */
16194 if (POINTER_TYPE_P (type))
16195 return uses_deducible_template_parms (TREE_TYPE (type));
16197 /* T[integer-constant ]
16198 type [i] */
16199 if (TREE_CODE (type) == ARRAY_TYPE)
16200 return (uses_deducible_template_parms (TREE_TYPE (type))
16201 || deducible_array_bound (TYPE_DOMAIN (type)));
16203 /* T type ::*
16204 type T::*
16205 T T::*
16206 T (type ::*)()
16207 type (T::*)()
16208 type (type ::*)(T)
16209 type (T::*)(T)
16210 T (type ::*)(T)
16211 T (T::*)()
16212 T (T::*)(T) */
16213 if (TYPE_PTRMEM_P (type))
16214 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
16215 || (uses_deducible_template_parms
16216 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
16218 /* template-name <T> (where template-name refers to a class template)
16219 template-name <i> (where template-name refers to a class template) */
16220 if (CLASS_TYPE_P (type)
16221 && CLASSTYPE_TEMPLATE_INFO (type)
16222 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
16223 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16224 (CLASSTYPE_TI_ARGS (type)));
16226 /* type (T)
16228 T(T) */
16229 if (TREE_CODE (type) == FUNCTION_TYPE
16230 || TREE_CODE (type) == METHOD_TYPE)
16232 if (uses_deducible_template_parms (TREE_TYPE (type)))
16233 return true;
16234 tree parm = TYPE_ARG_TYPES (type);
16235 if (TREE_CODE (type) == METHOD_TYPE)
16236 parm = TREE_CHAIN (parm);
16237 for (; parm; parm = TREE_CHAIN (parm))
16238 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16239 return true;
16242 return false;
16245 /* Subroutine of type_unification_real and unify_pack_expansion to
16246 handle unification of a single P/A pair. Parameters are as
16247 for those functions. */
16249 static int
16250 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16251 int subr, unification_kind_t strict, int flags,
16252 bool explain_p)
16254 tree arg_expr = NULL_TREE;
16255 int arg_strict;
16257 if (arg == error_mark_node || parm == error_mark_node)
16258 return unify_invalid (explain_p);
16259 if (arg == unknown_type_node)
16260 /* We can't deduce anything from this, but we might get all the
16261 template args from other function args. */
16262 return unify_success (explain_p);
16264 /* Implicit conversions (Clause 4) will be performed on a function
16265 argument to convert it to the type of the corresponding function
16266 parameter if the parameter type contains no template-parameters that
16267 participate in template argument deduction. */
16268 if (TYPE_P (parm) && !uses_template_parms (parm))
16269 /* For function parameters that contain no template-parameters at all,
16270 we have historically checked for convertibility in order to shortcut
16271 consideration of this candidate. */
16272 return check_non_deducible_conversion (parm, arg, strict, flags,
16273 explain_p);
16274 else if (strict == DEDUCE_CALL
16275 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16276 /* For function parameters with only non-deducible template parameters,
16277 just return. */
16278 return unify_success (explain_p);
16280 switch (strict)
16282 case DEDUCE_CALL:
16283 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16284 | UNIFY_ALLOW_MORE_CV_QUAL
16285 | UNIFY_ALLOW_DERIVED);
16286 break;
16288 case DEDUCE_CONV:
16289 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16290 break;
16292 case DEDUCE_EXACT:
16293 arg_strict = UNIFY_ALLOW_NONE;
16294 break;
16296 default:
16297 gcc_unreachable ();
16300 /* We only do these transformations if this is the top-level
16301 parameter_type_list in a call or declaration matching; in other
16302 situations (nested function declarators, template argument lists) we
16303 won't be comparing a type to an expression, and we don't do any type
16304 adjustments. */
16305 if (!subr)
16307 if (!TYPE_P (arg))
16309 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16310 if (type_unknown_p (arg))
16312 /* [temp.deduct.type] A template-argument can be
16313 deduced from a pointer to function or pointer
16314 to member function argument if the set of
16315 overloaded functions does not contain function
16316 templates and at most one of a set of
16317 overloaded functions provides a unique
16318 match. */
16320 if (resolve_overloaded_unification
16321 (tparms, targs, parm, arg, strict,
16322 arg_strict, explain_p))
16323 return unify_success (explain_p);
16324 return unify_overload_resolution_failure (explain_p, arg);
16327 arg_expr = arg;
16328 arg = unlowered_expr_type (arg);
16329 if (arg == error_mark_node)
16330 return unify_invalid (explain_p);
16333 arg_strict |=
16334 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16336 else
16337 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16338 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16340 /* For deduction from an init-list we need the actual list. */
16341 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16342 arg = arg_expr;
16343 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16346 /* Most parms like fn_type_unification.
16348 If SUBR is 1, we're being called recursively (to unify the
16349 arguments of a function or method parameter of a function
16350 template).
16352 CHECKS is a pointer to a vector of access checks encountered while
16353 substituting default template arguments. */
16355 static int
16356 type_unification_real (tree tparms,
16357 tree targs,
16358 tree xparms,
16359 const tree *xargs,
16360 unsigned int xnargs,
16361 int subr,
16362 unification_kind_t strict,
16363 int flags,
16364 vec<deferred_access_check, va_gc> **checks,
16365 bool explain_p)
16367 tree parm, arg;
16368 int i;
16369 int ntparms = TREE_VEC_LENGTH (tparms);
16370 int saw_undeduced = 0;
16371 tree parms;
16372 const tree *args;
16373 unsigned int nargs;
16374 unsigned int ia;
16376 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16377 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16378 gcc_assert (ntparms > 0);
16380 /* Reset the number of non-defaulted template arguments contained
16381 in TARGS. */
16382 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16384 again:
16385 parms = xparms;
16386 args = xargs;
16387 nargs = xnargs;
16389 ia = 0;
16390 while (parms && parms != void_list_node
16391 && ia < nargs)
16393 parm = TREE_VALUE (parms);
16395 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16396 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16397 /* For a function parameter pack that occurs at the end of the
16398 parameter-declaration-list, the type A of each remaining
16399 argument of the call is compared with the type P of the
16400 declarator-id of the function parameter pack. */
16401 break;
16403 parms = TREE_CHAIN (parms);
16405 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16406 /* For a function parameter pack that does not occur at the
16407 end of the parameter-declaration-list, the type of the
16408 parameter pack is a non-deduced context. */
16409 continue;
16411 arg = args[ia];
16412 ++ia;
16414 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16415 flags, explain_p))
16416 return 1;
16419 if (parms
16420 && parms != void_list_node
16421 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16423 /* Unify the remaining arguments with the pack expansion type. */
16424 tree argvec;
16425 tree parmvec = make_tree_vec (1);
16427 /* Allocate a TREE_VEC and copy in all of the arguments */
16428 argvec = make_tree_vec (nargs - ia);
16429 for (i = 0; ia < nargs; ++ia, ++i)
16430 TREE_VEC_ELT (argvec, i) = args[ia];
16432 /* Copy the parameter into parmvec. */
16433 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16434 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16435 /*subr=*/subr, explain_p))
16436 return 1;
16438 /* Advance to the end of the list of parameters. */
16439 parms = TREE_CHAIN (parms);
16442 /* Fail if we've reached the end of the parm list, and more args
16443 are present, and the parm list isn't variadic. */
16444 if (ia < nargs && parms == void_list_node)
16445 return unify_too_many_arguments (explain_p, nargs, ia);
16446 /* Fail if parms are left and they don't have default values. */
16447 if (parms && parms != void_list_node
16448 && TREE_PURPOSE (parms) == NULL_TREE)
16450 unsigned int count = nargs;
16451 tree p = parms;
16452 while (p && p != void_list_node)
16454 count++;
16455 p = TREE_CHAIN (p);
16457 return unify_too_few_arguments (explain_p, ia, count);
16460 if (!subr)
16462 tsubst_flags_t complain = (explain_p
16463 ? tf_warning_or_error
16464 : tf_none);
16466 for (i = 0; i < ntparms; i++)
16468 tree targ = TREE_VEC_ELT (targs, i);
16469 tree tparm = TREE_VEC_ELT (tparms, i);
16471 /* Clear the "incomplete" flags on all argument packs now so that
16472 substituting them into later default arguments works. */
16473 if (targ && ARGUMENT_PACK_P (targ))
16475 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16476 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16479 if (targ || tparm == error_mark_node)
16480 continue;
16481 tparm = TREE_VALUE (tparm);
16483 /* If this is an undeduced nontype parameter that depends on
16484 a type parameter, try another pass; its type may have been
16485 deduced from a later argument than the one from which
16486 this parameter can be deduced. */
16487 if (TREE_CODE (tparm) == PARM_DECL
16488 && uses_template_parms (TREE_TYPE (tparm))
16489 && !saw_undeduced++)
16490 goto again;
16492 /* Core issue #226 (C++0x) [temp.deduct]:
16494 If a template argument has not been deduced, its
16495 default template argument, if any, is used.
16497 When we are in C++98 mode, TREE_PURPOSE will either
16498 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16499 to explicitly check cxx_dialect here. */
16500 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16502 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16503 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16504 reopen_deferring_access_checks (*checks);
16505 location_t save_loc = input_location;
16506 if (DECL_P (parm))
16507 input_location = DECL_SOURCE_LOCATION (parm);
16508 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16509 arg = convert_template_argument (parm, arg, targs, complain,
16510 i, NULL_TREE);
16511 input_location = save_loc;
16512 *checks = get_deferred_access_checks ();
16513 pop_deferring_access_checks ();
16514 if (arg == error_mark_node)
16515 return 1;
16516 else
16518 TREE_VEC_ELT (targs, i) = arg;
16519 /* The position of the first default template argument,
16520 is also the number of non-defaulted arguments in TARGS.
16521 Record that. */
16522 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16523 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16524 continue;
16528 /* If the type parameter is a parameter pack, then it will
16529 be deduced to an empty parameter pack. */
16530 if (template_parameter_pack_p (tparm))
16532 tree arg;
16534 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16536 arg = make_node (NONTYPE_ARGUMENT_PACK);
16537 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16538 TREE_CONSTANT (arg) = 1;
16540 else
16541 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16543 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16545 TREE_VEC_ELT (targs, i) = arg;
16546 continue;
16549 return unify_parameter_deduction_failure (explain_p, tparm);
16552 #ifdef ENABLE_CHECKING
16553 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16554 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16555 #endif
16557 return unify_success (explain_p);
16560 /* Subroutine of type_unification_real. Args are like the variables
16561 at the call site. ARG is an overloaded function (or template-id);
16562 we try deducing template args from each of the overloads, and if
16563 only one succeeds, we go with that. Modifies TARGS and returns
16564 true on success. */
16566 static bool
16567 resolve_overloaded_unification (tree tparms,
16568 tree targs,
16569 tree parm,
16570 tree arg,
16571 unification_kind_t strict,
16572 int sub_strict,
16573 bool explain_p)
16575 tree tempargs = copy_node (targs);
16576 int good = 0;
16577 tree goodfn = NULL_TREE;
16578 bool addr_p;
16580 if (TREE_CODE (arg) == ADDR_EXPR)
16582 arg = TREE_OPERAND (arg, 0);
16583 addr_p = true;
16585 else
16586 addr_p = false;
16588 if (TREE_CODE (arg) == COMPONENT_REF)
16589 /* Handle `&x' where `x' is some static or non-static member
16590 function name. */
16591 arg = TREE_OPERAND (arg, 1);
16593 if (TREE_CODE (arg) == OFFSET_REF)
16594 arg = TREE_OPERAND (arg, 1);
16596 /* Strip baselink information. */
16597 if (BASELINK_P (arg))
16598 arg = BASELINK_FUNCTIONS (arg);
16600 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16602 /* If we got some explicit template args, we need to plug them into
16603 the affected templates before we try to unify, in case the
16604 explicit args will completely resolve the templates in question. */
16606 int ok = 0;
16607 tree expl_subargs = TREE_OPERAND (arg, 1);
16608 arg = TREE_OPERAND (arg, 0);
16610 for (; arg; arg = OVL_NEXT (arg))
16612 tree fn = OVL_CURRENT (arg);
16613 tree subargs, elem;
16615 if (TREE_CODE (fn) != TEMPLATE_DECL)
16616 continue;
16618 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16619 expl_subargs, NULL_TREE, tf_none,
16620 /*require_all_args=*/true,
16621 /*use_default_args=*/true);
16622 if (subargs != error_mark_node
16623 && !any_dependent_template_arguments_p (subargs))
16625 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
16626 if (try_one_overload (tparms, targs, tempargs, parm,
16627 elem, strict, sub_strict, addr_p, explain_p)
16628 && (!goodfn || !same_type_p (goodfn, elem)))
16630 goodfn = elem;
16631 ++good;
16634 else if (subargs)
16635 ++ok;
16637 /* If no templates (or more than one) are fully resolved by the
16638 explicit arguments, this template-id is a non-deduced context; it
16639 could still be OK if we deduce all template arguments for the
16640 enclosing call through other arguments. */
16641 if (good != 1)
16642 good = ok;
16644 else if (TREE_CODE (arg) != OVERLOAD
16645 && TREE_CODE (arg) != FUNCTION_DECL)
16646 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16647 -- but the deduction does not succeed because the expression is
16648 not just the function on its own. */
16649 return false;
16650 else
16651 for (; arg; arg = OVL_NEXT (arg))
16652 if (try_one_overload (tparms, targs, tempargs, parm,
16653 TREE_TYPE (OVL_CURRENT (arg)),
16654 strict, sub_strict, addr_p, explain_p)
16655 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16657 goodfn = OVL_CURRENT (arg);
16658 ++good;
16661 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16662 to function or pointer to member function argument if the set of
16663 overloaded functions does not contain function templates and at most
16664 one of a set of overloaded functions provides a unique match.
16666 So if we found multiple possibilities, we return success but don't
16667 deduce anything. */
16669 if (good == 1)
16671 int i = TREE_VEC_LENGTH (targs);
16672 for (; i--; )
16673 if (TREE_VEC_ELT (tempargs, i))
16674 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
16676 if (good)
16677 return true;
16679 return false;
16682 /* Core DR 115: In contexts where deduction is done and fails, or in
16683 contexts where deduction is not done, if a template argument list is
16684 specified and it, along with any default template arguments, identifies
16685 a single function template specialization, then the template-id is an
16686 lvalue for the function template specialization. */
16688 tree
16689 resolve_nondeduced_context (tree orig_expr)
16691 tree expr, offset, baselink;
16692 bool addr;
16694 if (!type_unknown_p (orig_expr))
16695 return orig_expr;
16697 expr = orig_expr;
16698 addr = false;
16699 offset = NULL_TREE;
16700 baselink = NULL_TREE;
16702 if (TREE_CODE (expr) == ADDR_EXPR)
16704 expr = TREE_OPERAND (expr, 0);
16705 addr = true;
16707 if (TREE_CODE (expr) == OFFSET_REF)
16709 offset = expr;
16710 expr = TREE_OPERAND (expr, 1);
16712 if (BASELINK_P (expr))
16714 baselink = expr;
16715 expr = BASELINK_FUNCTIONS (expr);
16718 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16720 int good = 0;
16721 tree goodfn = NULL_TREE;
16723 /* If we got some explicit template args, we need to plug them into
16724 the affected templates before we try to unify, in case the
16725 explicit args will completely resolve the templates in question. */
16727 tree expl_subargs = TREE_OPERAND (expr, 1);
16728 tree arg = TREE_OPERAND (expr, 0);
16729 tree badfn = NULL_TREE;
16730 tree badargs = NULL_TREE;
16732 for (; arg; arg = OVL_NEXT (arg))
16734 tree fn = OVL_CURRENT (arg);
16735 tree subargs, elem;
16737 if (TREE_CODE (fn) != TEMPLATE_DECL)
16738 continue;
16740 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16741 expl_subargs, NULL_TREE, tf_none,
16742 /*require_all_args=*/true,
16743 /*use_default_args=*/true);
16744 if (subargs != error_mark_node
16745 && !any_dependent_template_arguments_p (subargs))
16747 elem = instantiate_template (fn, subargs, tf_none);
16748 if (elem == error_mark_node)
16750 badfn = fn;
16751 badargs = subargs;
16753 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16755 goodfn = elem;
16756 ++good;
16760 if (good == 1)
16762 mark_used (goodfn);
16763 expr = goodfn;
16764 if (baselink)
16765 expr = build_baselink (BASELINK_BINFO (baselink),
16766 BASELINK_ACCESS_BINFO (baselink),
16767 expr, BASELINK_OPTYPE (baselink));
16768 if (offset)
16770 tree base
16771 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16772 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16774 if (addr)
16775 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16776 return expr;
16778 else if (good == 0 && badargs)
16779 /* There were no good options and at least one bad one, so let the
16780 user know what the problem is. */
16781 instantiate_template (badfn, badargs, tf_warning_or_error);
16783 return orig_expr;
16786 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16787 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16788 different overloads deduce different arguments for a given parm.
16789 ADDR_P is true if the expression for which deduction is being
16790 performed was of the form "& fn" rather than simply "fn".
16792 Returns 1 on success. */
16794 static int
16795 try_one_overload (tree tparms,
16796 tree orig_targs,
16797 tree targs,
16798 tree parm,
16799 tree arg,
16800 unification_kind_t strict,
16801 int sub_strict,
16802 bool addr_p,
16803 bool explain_p)
16805 int nargs;
16806 tree tempargs;
16807 int i;
16809 if (arg == error_mark_node)
16810 return 0;
16812 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16813 to function or pointer to member function argument if the set of
16814 overloaded functions does not contain function templates and at most
16815 one of a set of overloaded functions provides a unique match.
16817 So if this is a template, just return success. */
16819 if (uses_template_parms (arg))
16820 return 1;
16822 if (TREE_CODE (arg) == METHOD_TYPE)
16823 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16824 else if (addr_p)
16825 arg = build_pointer_type (arg);
16827 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
16829 /* We don't copy orig_targs for this because if we have already deduced
16830 some template args from previous args, unify would complain when we
16831 try to deduce a template parameter for the same argument, even though
16832 there isn't really a conflict. */
16833 nargs = TREE_VEC_LENGTH (targs);
16834 tempargs = make_tree_vec (nargs);
16836 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
16837 return 0;
16839 /* First make sure we didn't deduce anything that conflicts with
16840 explicitly specified args. */
16841 for (i = nargs; i--; )
16843 tree elt = TREE_VEC_ELT (tempargs, i);
16844 tree oldelt = TREE_VEC_ELT (orig_targs, i);
16846 if (!elt)
16847 /*NOP*/;
16848 else if (uses_template_parms (elt))
16849 /* Since we're unifying against ourselves, we will fill in
16850 template args used in the function parm list with our own
16851 template parms. Discard them. */
16852 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
16853 else if (oldelt && !template_args_equal (oldelt, elt))
16854 return 0;
16857 for (i = nargs; i--; )
16859 tree elt = TREE_VEC_ELT (tempargs, i);
16861 if (elt)
16862 TREE_VEC_ELT (targs, i) = elt;
16865 return 1;
16868 /* PARM is a template class (perhaps with unbound template
16869 parameters). ARG is a fully instantiated type. If ARG can be
16870 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16871 TARGS are as for unify. */
16873 static tree
16874 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
16875 bool explain_p)
16877 tree copy_of_targs;
16879 if (!CLASSTYPE_TEMPLATE_INFO (arg)
16880 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
16881 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
16882 return NULL_TREE;
16884 /* We need to make a new template argument vector for the call to
16885 unify. If we used TARGS, we'd clutter it up with the result of
16886 the attempted unification, even if this class didn't work out.
16887 We also don't want to commit ourselves to all the unifications
16888 we've already done, since unification is supposed to be done on
16889 an argument-by-argument basis. In other words, consider the
16890 following pathological case:
16892 template <int I, int J, int K>
16893 struct S {};
16895 template <int I, int J>
16896 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16898 template <int I, int J, int K>
16899 void f(S<I, J, K>, S<I, I, I>);
16901 void g() {
16902 S<0, 0, 0> s0;
16903 S<0, 1, 2> s2;
16905 f(s0, s2);
16908 Now, by the time we consider the unification involving `s2', we
16909 already know that we must have `f<0, 0, 0>'. But, even though
16910 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16911 because there are two ways to unify base classes of S<0, 1, 2>
16912 with S<I, I, I>. If we kept the already deduced knowledge, we
16913 would reject the possibility I=1. */
16914 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
16916 /* If unification failed, we're done. */
16917 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
16918 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
16919 return NULL_TREE;
16921 return arg;
16924 /* Given a template type PARM and a class type ARG, find the unique
16925 base type in ARG that is an instance of PARM. We do not examine
16926 ARG itself; only its base-classes. If there is not exactly one
16927 appropriate base class, return NULL_TREE. PARM may be the type of
16928 a partial specialization, as well as a plain template type. Used
16929 by unify. */
16931 static enum template_base_result
16932 get_template_base (tree tparms, tree targs, tree parm, tree arg,
16933 bool explain_p, tree *result)
16935 tree rval = NULL_TREE;
16936 tree binfo;
16938 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
16940 binfo = TYPE_BINFO (complete_type (arg));
16941 if (!binfo)
16943 /* The type could not be completed. */
16944 *result = NULL_TREE;
16945 return tbr_incomplete_type;
16948 /* Walk in inheritance graph order. The search order is not
16949 important, and this avoids multiple walks of virtual bases. */
16950 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
16952 tree r = try_class_unification (tparms, targs, parm,
16953 BINFO_TYPE (binfo), explain_p);
16955 if (r)
16957 /* If there is more than one satisfactory baseclass, then:
16959 [temp.deduct.call]
16961 If they yield more than one possible deduced A, the type
16962 deduction fails.
16964 applies. */
16965 if (rval && !same_type_p (r, rval))
16967 *result = NULL_TREE;
16968 return tbr_ambiguous_baseclass;
16971 rval = r;
16975 *result = rval;
16976 return tbr_success;
16979 /* Returns the level of DECL, which declares a template parameter. */
16981 static int
16982 template_decl_level (tree decl)
16984 switch (TREE_CODE (decl))
16986 case TYPE_DECL:
16987 case TEMPLATE_DECL:
16988 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16990 case PARM_DECL:
16991 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16993 default:
16994 gcc_unreachable ();
16996 return 0;
16999 /* Decide whether ARG can be unified with PARM, considering only the
17000 cv-qualifiers of each type, given STRICT as documented for unify.
17001 Returns nonzero iff the unification is OK on that basis. */
17003 static int
17004 check_cv_quals_for_unify (int strict, tree arg, tree parm)
17006 int arg_quals = cp_type_quals (arg);
17007 int parm_quals = cp_type_quals (parm);
17009 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17010 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17012 /* Although a CVR qualifier is ignored when being applied to a
17013 substituted template parameter ([8.3.2]/1 for example), that
17014 does not allow us to unify "const T" with "int&" because both
17015 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
17016 It is ok when we're allowing additional CV qualifiers
17017 at the outer level [14.8.2.1]/3,1st bullet. */
17018 if ((TREE_CODE (arg) == REFERENCE_TYPE
17019 || TREE_CODE (arg) == FUNCTION_TYPE
17020 || TREE_CODE (arg) == METHOD_TYPE)
17021 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
17022 return 0;
17024 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
17025 && (parm_quals & TYPE_QUAL_RESTRICT))
17026 return 0;
17029 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17030 && (arg_quals & parm_quals) != parm_quals)
17031 return 0;
17033 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
17034 && (parm_quals & arg_quals) != arg_quals)
17035 return 0;
17037 return 1;
17040 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17041 void
17042 template_parm_level_and_index (tree parm, int* level, int* index)
17044 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17045 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17046 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17048 *index = TEMPLATE_TYPE_IDX (parm);
17049 *level = TEMPLATE_TYPE_LEVEL (parm);
17051 else
17053 *index = TEMPLATE_PARM_IDX (parm);
17054 *level = TEMPLATE_PARM_LEVEL (parm);
17058 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17059 do { \
17060 if (unify (TP, TA, P, A, S, EP)) \
17061 return 1; \
17062 } while (0);
17064 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17065 expansion at the end of PACKED_PARMS. Returns 0 if the type
17066 deduction succeeds, 1 otherwise. STRICT is the same as in
17067 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17068 call argument list. We'll need to adjust the arguments to make them
17069 types. SUBR tells us if this is from a recursive call to
17070 type_unification_real, or for comparing two template argument
17071 lists. */
17073 static int
17074 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
17075 tree packed_args, unification_kind_t strict,
17076 bool subr, bool explain_p)
17078 tree parm
17079 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
17080 tree pattern = PACK_EXPANSION_PATTERN (parm);
17081 tree pack, packs = NULL_TREE;
17082 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
17084 packed_args = expand_template_argument_pack (packed_args);
17086 int len = TREE_VEC_LENGTH (packed_args);
17088 /* Determine the parameter packs we will be deducing from the
17089 pattern, and record their current deductions. */
17090 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
17091 pack; pack = TREE_CHAIN (pack))
17093 tree parm_pack = TREE_VALUE (pack);
17094 int idx, level;
17096 /* Determine the index and level of this parameter pack. */
17097 template_parm_level_and_index (parm_pack, &level, &idx);
17099 /* Keep track of the parameter packs and their corresponding
17100 argument packs. */
17101 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
17102 TREE_TYPE (packs) = make_tree_vec (len - start);
17105 /* Loop through all of the arguments that have not yet been
17106 unified and unify each with the pattern. */
17107 for (i = start; i < len; i++)
17109 tree parm;
17110 bool any_explicit = false;
17111 tree arg = TREE_VEC_ELT (packed_args, i);
17113 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17114 or the element of its argument pack at the current index if
17115 this argument was explicitly specified. */
17116 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17118 int idx, level;
17119 tree arg, pargs;
17120 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17122 arg = NULL_TREE;
17123 if (TREE_VALUE (pack)
17124 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
17125 && (i - start < TREE_VEC_LENGTH (pargs)))
17127 any_explicit = true;
17128 arg = TREE_VEC_ELT (pargs, i - start);
17130 TMPL_ARG (targs, level, idx) = arg;
17133 /* If we had explicit template arguments, substitute them into the
17134 pattern before deduction. */
17135 if (any_explicit)
17137 /* Some arguments might still be unspecified or dependent. */
17138 bool dependent;
17139 ++processing_template_decl;
17140 dependent = any_dependent_template_arguments_p (targs);
17141 if (!dependent)
17142 --processing_template_decl;
17143 parm = tsubst (pattern, targs,
17144 explain_p ? tf_warning_or_error : tf_none,
17145 NULL_TREE);
17146 if (dependent)
17147 --processing_template_decl;
17148 if (parm == error_mark_node)
17149 return 1;
17151 else
17152 parm = pattern;
17154 /* Unify the pattern with the current argument. */
17155 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
17156 LOOKUP_IMPLICIT, explain_p))
17157 return 1;
17159 /* For each parameter pack, collect the deduced value. */
17160 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17162 int idx, level;
17163 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17165 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
17166 TMPL_ARG (targs, level, idx);
17170 /* Verify that the results of unification with the parameter packs
17171 produce results consistent with what we've seen before, and make
17172 the deduced argument packs available. */
17173 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17175 tree old_pack = TREE_VALUE (pack);
17176 tree new_args = TREE_TYPE (pack);
17177 int i, len = TREE_VEC_LENGTH (new_args);
17178 int idx, level;
17179 bool nondeduced_p = false;
17181 /* By default keep the original deduced argument pack.
17182 If necessary, more specific code is going to update the
17183 resulting deduced argument later down in this function. */
17184 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17185 TMPL_ARG (targs, level, idx) = old_pack;
17187 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17188 actually deduce anything. */
17189 for (i = 0; i < len && !nondeduced_p; ++i)
17190 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
17191 nondeduced_p = true;
17192 if (nondeduced_p)
17193 continue;
17195 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
17197 /* If we had fewer function args than explicit template args,
17198 just use the explicits. */
17199 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17200 int explicit_len = TREE_VEC_LENGTH (explicit_args);
17201 if (len < explicit_len)
17202 new_args = explicit_args;
17205 if (!old_pack)
17207 tree result;
17208 /* Build the deduced *_ARGUMENT_PACK. */
17209 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
17211 result = make_node (NONTYPE_ARGUMENT_PACK);
17212 TREE_TYPE (result) =
17213 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
17214 TREE_CONSTANT (result) = 1;
17216 else
17217 result = cxx_make_type (TYPE_ARGUMENT_PACK);
17219 SET_ARGUMENT_PACK_ARGS (result, new_args);
17221 /* Note the deduced argument packs for this parameter
17222 pack. */
17223 TMPL_ARG (targs, level, idx) = result;
17225 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
17226 && (ARGUMENT_PACK_ARGS (old_pack)
17227 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
17229 /* We only had the explicitly-provided arguments before, but
17230 now we have a complete set of arguments. */
17231 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17233 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17234 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17235 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17237 else
17239 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17240 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17242 if (!comp_template_args_with_info (old_args, new_args,
17243 &bad_old_arg, &bad_new_arg))
17244 /* Inconsistent unification of this parameter pack. */
17245 return unify_parameter_pack_inconsistent (explain_p,
17246 bad_old_arg,
17247 bad_new_arg);
17251 return unify_success (explain_p);
17254 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17255 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17256 parameters and return value are as for unify. */
17258 static int
17259 unify_array_domain (tree tparms, tree targs,
17260 tree parm_dom, tree arg_dom,
17261 bool explain_p)
17263 tree parm_max;
17264 tree arg_max;
17265 bool parm_cst;
17266 bool arg_cst;
17268 /* Our representation of array types uses "N - 1" as the
17269 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17270 not an integer constant. We cannot unify arbitrarily
17271 complex expressions, so we eliminate the MINUS_EXPRs
17272 here. */
17273 parm_max = TYPE_MAX_VALUE (parm_dom);
17274 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17275 if (!parm_cst)
17277 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17278 parm_max = TREE_OPERAND (parm_max, 0);
17280 arg_max = TYPE_MAX_VALUE (arg_dom);
17281 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17282 if (!arg_cst)
17284 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17285 trying to unify the type of a variable with the type
17286 of a template parameter. For example:
17288 template <unsigned int N>
17289 void f (char (&) [N]);
17290 int g();
17291 void h(int i) {
17292 char a[g(i)];
17293 f(a);
17296 Here, the type of the ARG will be "int [g(i)]", and
17297 may be a SAVE_EXPR, etc. */
17298 if (TREE_CODE (arg_max) != MINUS_EXPR)
17299 return unify_vla_arg (explain_p, arg_dom);
17300 arg_max = TREE_OPERAND (arg_max, 0);
17303 /* If only one of the bounds used a MINUS_EXPR, compensate
17304 by adding one to the other bound. */
17305 if (parm_cst && !arg_cst)
17306 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17307 integer_type_node,
17308 parm_max,
17309 integer_one_node);
17310 else if (arg_cst && !parm_cst)
17311 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17312 integer_type_node,
17313 arg_max,
17314 integer_one_node);
17316 return unify (tparms, targs, parm_max, arg_max,
17317 UNIFY_ALLOW_INTEGER, explain_p);
17320 /* Deduce the value of template parameters. TPARMS is the (innermost)
17321 set of template parameters to a template. TARGS is the bindings
17322 for those template parameters, as determined thus far; TARGS may
17323 include template arguments for outer levels of template parameters
17324 as well. PARM is a parameter to a template function, or a
17325 subcomponent of that parameter; ARG is the corresponding argument.
17326 This function attempts to match PARM with ARG in a manner
17327 consistent with the existing assignments in TARGS. If more values
17328 are deduced, then TARGS is updated.
17330 Returns 0 if the type deduction succeeds, 1 otherwise. The
17331 parameter STRICT is a bitwise or of the following flags:
17333 UNIFY_ALLOW_NONE:
17334 Require an exact match between PARM and ARG.
17335 UNIFY_ALLOW_MORE_CV_QUAL:
17336 Allow the deduced ARG to be more cv-qualified (by qualification
17337 conversion) than ARG.
17338 UNIFY_ALLOW_LESS_CV_QUAL:
17339 Allow the deduced ARG to be less cv-qualified than ARG.
17340 UNIFY_ALLOW_DERIVED:
17341 Allow the deduced ARG to be a template base class of ARG,
17342 or a pointer to a template base class of the type pointed to by
17343 ARG.
17344 UNIFY_ALLOW_INTEGER:
17345 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17346 case for more information.
17347 UNIFY_ALLOW_OUTER_LEVEL:
17348 This is the outermost level of a deduction. Used to determine validity
17349 of qualification conversions. A valid qualification conversion must
17350 have const qualified pointers leading up to the inner type which
17351 requires additional CV quals, except at the outer level, where const
17352 is not required [conv.qual]. It would be normal to set this flag in
17353 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17354 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17355 This is the outermost level of a deduction, and PARM can be more CV
17356 qualified at this point.
17357 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17358 This is the outermost level of a deduction, and PARM can be less CV
17359 qualified at this point. */
17361 static int
17362 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17363 bool explain_p)
17365 int idx;
17366 tree targ;
17367 tree tparm;
17368 int strict_in = strict;
17370 /* I don't think this will do the right thing with respect to types.
17371 But the only case I've seen it in so far has been array bounds, where
17372 signedness is the only information lost, and I think that will be
17373 okay. */
17374 while (TREE_CODE (parm) == NOP_EXPR)
17375 parm = TREE_OPERAND (parm, 0);
17377 if (arg == error_mark_node)
17378 return unify_invalid (explain_p);
17379 if (arg == unknown_type_node
17380 || arg == init_list_type_node)
17381 /* We can't deduce anything from this, but we might get all the
17382 template args from other function args. */
17383 return unify_success (explain_p);
17385 /* If PARM uses template parameters, then we can't bail out here,
17386 even if ARG == PARM, since we won't record unifications for the
17387 template parameters. We might need them if we're trying to
17388 figure out which of two things is more specialized. */
17389 if (arg == parm && !uses_template_parms (parm))
17390 return unify_success (explain_p);
17392 /* Handle init lists early, so the rest of the function can assume
17393 we're dealing with a type. */
17394 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17396 tree elt, elttype;
17397 unsigned i;
17398 tree orig_parm = parm;
17400 /* Replace T with std::initializer_list<T> for deduction. */
17401 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17402 && flag_deduce_init_list)
17403 parm = listify (parm);
17405 if (!is_std_init_list (parm)
17406 && TREE_CODE (parm) != ARRAY_TYPE)
17407 /* We can only deduce from an initializer list argument if the
17408 parameter is std::initializer_list or an array; otherwise this
17409 is a non-deduced context. */
17410 return unify_success (explain_p);
17412 if (TREE_CODE (parm) == ARRAY_TYPE)
17413 elttype = TREE_TYPE (parm);
17414 else
17415 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17417 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17419 int elt_strict = strict;
17421 if (elt == error_mark_node)
17422 return unify_invalid (explain_p);
17424 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17426 tree type = TREE_TYPE (elt);
17427 /* It should only be possible to get here for a call. */
17428 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17429 elt_strict |= maybe_adjust_types_for_deduction
17430 (DEDUCE_CALL, &elttype, &type, elt);
17431 elt = type;
17434 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17435 explain_p);
17438 if (TREE_CODE (parm) == ARRAY_TYPE
17439 && deducible_array_bound (TYPE_DOMAIN (parm)))
17441 /* Also deduce from the length of the initializer list. */
17442 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17443 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17444 if (idx == error_mark_node)
17445 return unify_invalid (explain_p);
17446 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17447 idx, explain_p);
17450 /* If the std::initializer_list<T> deduction worked, replace the
17451 deduced A with std::initializer_list<A>. */
17452 if (orig_parm != parm)
17454 idx = TEMPLATE_TYPE_IDX (orig_parm);
17455 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17456 targ = listify (targ);
17457 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17459 return unify_success (explain_p);
17462 /* Immediately reject some pairs that won't unify because of
17463 cv-qualification mismatches. */
17464 if (TREE_CODE (arg) == TREE_CODE (parm)
17465 && TYPE_P (arg)
17466 /* It is the elements of the array which hold the cv quals of an array
17467 type, and the elements might be template type parms. We'll check
17468 when we recurse. */
17469 && TREE_CODE (arg) != ARRAY_TYPE
17470 /* We check the cv-qualifiers when unifying with template type
17471 parameters below. We want to allow ARG `const T' to unify with
17472 PARM `T' for example, when computing which of two templates
17473 is more specialized, for example. */
17474 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17475 && !check_cv_quals_for_unify (strict_in, arg, parm))
17476 return unify_cv_qual_mismatch (explain_p, parm, arg);
17478 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17479 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17480 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17481 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17482 strict &= ~UNIFY_ALLOW_DERIVED;
17483 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17484 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17486 switch (TREE_CODE (parm))
17488 case TYPENAME_TYPE:
17489 case SCOPE_REF:
17490 case UNBOUND_CLASS_TEMPLATE:
17491 /* In a type which contains a nested-name-specifier, template
17492 argument values cannot be deduced for template parameters used
17493 within the nested-name-specifier. */
17494 return unify_success (explain_p);
17496 case TEMPLATE_TYPE_PARM:
17497 case TEMPLATE_TEMPLATE_PARM:
17498 case BOUND_TEMPLATE_TEMPLATE_PARM:
17499 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17500 if (tparm == error_mark_node)
17501 return unify_invalid (explain_p);
17503 if (TEMPLATE_TYPE_LEVEL (parm)
17504 != template_decl_level (tparm))
17505 /* The PARM is not one we're trying to unify. Just check
17506 to see if it matches ARG. */
17508 if (TREE_CODE (arg) == TREE_CODE (parm)
17509 && (is_auto (parm) ? is_auto (arg)
17510 : same_type_p (parm, arg)))
17511 return unify_success (explain_p);
17512 else
17513 return unify_type_mismatch (explain_p, parm, arg);
17515 idx = TEMPLATE_TYPE_IDX (parm);
17516 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17517 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17518 if (tparm == error_mark_node)
17519 return unify_invalid (explain_p);
17521 /* Check for mixed types and values. */
17522 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17523 && TREE_CODE (tparm) != TYPE_DECL)
17524 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17525 && TREE_CODE (tparm) != TEMPLATE_DECL))
17526 gcc_unreachable ();
17528 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17530 /* ARG must be constructed from a template class or a template
17531 template parameter. */
17532 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17533 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17534 return unify_template_deduction_failure (explain_p, parm, arg);
17536 tree parmvec = TYPE_TI_ARGS (parm);
17537 /* An alias template name is never deduced. */
17538 if (TYPE_ALIAS_P (arg))
17539 arg = strip_typedefs (arg);
17540 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17541 tree full_argvec = add_to_template_args (targs, argvec);
17542 tree parm_parms
17543 = DECL_INNERMOST_TEMPLATE_PARMS
17544 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17545 int i, len;
17546 int parm_variadic_p = 0;
17548 /* The resolution to DR150 makes clear that default
17549 arguments for an N-argument may not be used to bind T
17550 to a template template parameter with fewer than N
17551 parameters. It is not safe to permit the binding of
17552 default arguments as an extension, as that may change
17553 the meaning of a conforming program. Consider:
17555 struct Dense { static const unsigned int dim = 1; };
17557 template <template <typename> class View,
17558 typename Block>
17559 void operator+(float, View<Block> const&);
17561 template <typename Block,
17562 unsigned int Dim = Block::dim>
17563 struct Lvalue_proxy { operator float() const; };
17565 void
17566 test_1d (void) {
17567 Lvalue_proxy<Dense> p;
17568 float b;
17569 b + p;
17572 Here, if Lvalue_proxy is permitted to bind to View, then
17573 the global operator+ will be used; if they are not, the
17574 Lvalue_proxy will be converted to float. */
17575 if (coerce_template_parms (parm_parms,
17576 full_argvec,
17577 TYPE_TI_TEMPLATE (parm),
17578 (explain_p
17579 ? tf_warning_or_error
17580 : tf_none),
17581 /*require_all_args=*/true,
17582 /*use_default_args=*/false)
17583 == error_mark_node)
17584 return 1;
17586 /* Deduce arguments T, i from TT<T> or TT<i>.
17587 We check each element of PARMVEC and ARGVEC individually
17588 rather than the whole TREE_VEC since they can have
17589 different number of elements. */
17591 parmvec = expand_template_argument_pack (parmvec);
17592 argvec = expand_template_argument_pack (argvec);
17594 len = TREE_VEC_LENGTH (parmvec);
17596 /* Check if the parameters end in a pack, making them
17597 variadic. */
17598 if (len > 0
17599 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17600 parm_variadic_p = 1;
17602 for (i = 0; i < len - parm_variadic_p; ++i)
17603 /* If the template argument list of P contains a pack
17604 expansion that is not the last template argument, the
17605 entire template argument list is a non-deduced
17606 context. */
17607 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17608 return unify_success (explain_p);
17610 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17611 return unify_too_few_arguments (explain_p,
17612 TREE_VEC_LENGTH (argvec), len);
17614 for (i = 0; i < len - parm_variadic_p; ++i)
17616 RECUR_AND_CHECK_FAILURE (tparms, targs,
17617 TREE_VEC_ELT (parmvec, i),
17618 TREE_VEC_ELT (argvec, i),
17619 UNIFY_ALLOW_NONE, explain_p);
17622 if (parm_variadic_p
17623 && unify_pack_expansion (tparms, targs,
17624 parmvec, argvec,
17625 DEDUCE_EXACT,
17626 /*subr=*/true, explain_p))
17627 return 1;
17629 arg = TYPE_TI_TEMPLATE (arg);
17631 /* Fall through to deduce template name. */
17634 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17635 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17637 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17639 /* Simple cases: Value already set, does match or doesn't. */
17640 if (targ != NULL_TREE && template_args_equal (targ, arg))
17641 return unify_success (explain_p);
17642 else if (targ)
17643 return unify_inconsistency (explain_p, parm, targ, arg);
17645 else
17647 /* If PARM is `const T' and ARG is only `int', we don't have
17648 a match unless we are allowing additional qualification.
17649 If ARG is `const int' and PARM is just `T' that's OK;
17650 that binds `const int' to `T'. */
17651 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17652 arg, parm))
17653 return unify_cv_qual_mismatch (explain_p, parm, arg);
17655 /* Consider the case where ARG is `const volatile int' and
17656 PARM is `const T'. Then, T should be `volatile int'. */
17657 arg = cp_build_qualified_type_real
17658 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17659 if (arg == error_mark_node)
17660 return unify_invalid (explain_p);
17662 /* Simple cases: Value already set, does match or doesn't. */
17663 if (targ != NULL_TREE && same_type_p (targ, arg))
17664 return unify_success (explain_p);
17665 else if (targ)
17666 return unify_inconsistency (explain_p, parm, targ, arg);
17668 /* Make sure that ARG is not a variable-sized array. (Note
17669 that were talking about variable-sized arrays (like
17670 `int[n]'), rather than arrays of unknown size (like
17671 `int[]').) We'll get very confused by such a type since
17672 the bound of the array is not constant, and therefore
17673 not mangleable. Besides, such types are not allowed in
17674 ISO C++, so we can do as we please here. We do allow
17675 them for 'auto' deduction, since that isn't ABI-exposed. */
17676 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17677 return unify_vla_arg (explain_p, arg);
17679 /* Strip typedefs as in convert_template_argument. */
17680 arg = canonicalize_type_argument (arg, tf_none);
17683 /* If ARG is a parameter pack or an expansion, we cannot unify
17684 against it unless PARM is also a parameter pack. */
17685 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17686 && !template_parameter_pack_p (parm))
17687 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17689 /* If the argument deduction results is a METHOD_TYPE,
17690 then there is a problem.
17691 METHOD_TYPE doesn't map to any real C++ type the result of
17692 the deduction can not be of that type. */
17693 if (TREE_CODE (arg) == METHOD_TYPE)
17694 return unify_method_type_error (explain_p, arg);
17696 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17697 return unify_success (explain_p);
17699 case TEMPLATE_PARM_INDEX:
17700 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17701 if (tparm == error_mark_node)
17702 return unify_invalid (explain_p);
17704 if (TEMPLATE_PARM_LEVEL (parm)
17705 != template_decl_level (tparm))
17707 /* The PARM is not one we're trying to unify. Just check
17708 to see if it matches ARG. */
17709 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17710 && cp_tree_equal (parm, arg));
17711 if (result)
17712 unify_expression_unequal (explain_p, parm, arg);
17713 return result;
17716 idx = TEMPLATE_PARM_IDX (parm);
17717 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17719 if (targ)
17721 int x = !cp_tree_equal (targ, arg);
17722 if (x)
17723 unify_inconsistency (explain_p, parm, targ, arg);
17724 return x;
17727 /* [temp.deduct.type] If, in the declaration of a function template
17728 with a non-type template-parameter, the non-type
17729 template-parameter is used in an expression in the function
17730 parameter-list and, if the corresponding template-argument is
17731 deduced, the template-argument type shall match the type of the
17732 template-parameter exactly, except that a template-argument
17733 deduced from an array bound may be of any integral type.
17734 The non-type parameter might use already deduced type parameters. */
17735 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17736 if (!TREE_TYPE (arg))
17737 /* Template-parameter dependent expression. Just accept it for now.
17738 It will later be processed in convert_template_argument. */
17740 else if (same_type_p (TREE_TYPE (arg), tparm))
17741 /* OK */;
17742 else if ((strict & UNIFY_ALLOW_INTEGER)
17743 && CP_INTEGRAL_TYPE_P (tparm))
17744 /* Convert the ARG to the type of PARM; the deduced non-type
17745 template argument must exactly match the types of the
17746 corresponding parameter. */
17747 arg = fold (build_nop (tparm, arg));
17748 else if (uses_template_parms (tparm))
17749 /* We haven't deduced the type of this parameter yet. Try again
17750 later. */
17751 return unify_success (explain_p);
17752 else
17753 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17755 /* If ARG is a parameter pack or an expansion, we cannot unify
17756 against it unless PARM is also a parameter pack. */
17757 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17758 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17759 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17761 arg = strip_typedefs_expr (arg);
17762 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17763 return unify_success (explain_p);
17765 case PTRMEM_CST:
17767 /* A pointer-to-member constant can be unified only with
17768 another constant. */
17769 if (TREE_CODE (arg) != PTRMEM_CST)
17770 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17772 /* Just unify the class member. It would be useless (and possibly
17773 wrong, depending on the strict flags) to unify also
17774 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17775 arg refer to the same variable, even if through different
17776 classes. For instance:
17778 struct A { int x; };
17779 struct B : A { };
17781 Unification of &A::x and &B::x must succeed. */
17782 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17783 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17786 case POINTER_TYPE:
17788 if (!TYPE_PTR_P (arg))
17789 return unify_type_mismatch (explain_p, parm, arg);
17791 /* [temp.deduct.call]
17793 A can be another pointer or pointer to member type that can
17794 be converted to the deduced A via a qualification
17795 conversion (_conv.qual_).
17797 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17798 This will allow for additional cv-qualification of the
17799 pointed-to types if appropriate. */
17801 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17802 /* The derived-to-base conversion only persists through one
17803 level of pointers. */
17804 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17806 return unify (tparms, targs, TREE_TYPE (parm),
17807 TREE_TYPE (arg), strict, explain_p);
17810 case REFERENCE_TYPE:
17811 if (TREE_CODE (arg) != REFERENCE_TYPE)
17812 return unify_type_mismatch (explain_p, parm, arg);
17813 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17814 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17816 case ARRAY_TYPE:
17817 if (TREE_CODE (arg) != ARRAY_TYPE)
17818 return unify_type_mismatch (explain_p, parm, arg);
17819 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17820 != (TYPE_DOMAIN (arg) == NULL_TREE))
17821 return unify_type_mismatch (explain_p, parm, arg);
17822 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17823 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17824 if (TYPE_DOMAIN (parm) != NULL_TREE)
17825 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17826 TYPE_DOMAIN (arg), explain_p);
17827 return unify_success (explain_p);
17829 case REAL_TYPE:
17830 case COMPLEX_TYPE:
17831 case VECTOR_TYPE:
17832 case INTEGER_TYPE:
17833 case BOOLEAN_TYPE:
17834 case ENUMERAL_TYPE:
17835 case VOID_TYPE:
17836 case NULLPTR_TYPE:
17837 if (TREE_CODE (arg) != TREE_CODE (parm))
17838 return unify_type_mismatch (explain_p, parm, arg);
17840 /* We have already checked cv-qualification at the top of the
17841 function. */
17842 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
17843 return unify_type_mismatch (explain_p, parm, arg);
17845 /* As far as unification is concerned, this wins. Later checks
17846 will invalidate it if necessary. */
17847 return unify_success (explain_p);
17849 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17850 /* Type INTEGER_CST can come from ordinary constant template args. */
17851 case INTEGER_CST:
17852 while (TREE_CODE (arg) == NOP_EXPR)
17853 arg = TREE_OPERAND (arg, 0);
17855 if (TREE_CODE (arg) != INTEGER_CST)
17856 return unify_template_argument_mismatch (explain_p, parm, arg);
17857 return (tree_int_cst_equal (parm, arg)
17858 ? unify_success (explain_p)
17859 : unify_template_argument_mismatch (explain_p, parm, arg));
17861 case TREE_VEC:
17863 int i, len, argslen;
17864 int parm_variadic_p = 0;
17866 if (TREE_CODE (arg) != TREE_VEC)
17867 return unify_template_argument_mismatch (explain_p, parm, arg);
17869 len = TREE_VEC_LENGTH (parm);
17870 argslen = TREE_VEC_LENGTH (arg);
17872 /* Check for pack expansions in the parameters. */
17873 for (i = 0; i < len; ++i)
17875 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
17877 if (i == len - 1)
17878 /* We can unify against something with a trailing
17879 parameter pack. */
17880 parm_variadic_p = 1;
17881 else
17882 /* [temp.deduct.type]/9: If the template argument list of
17883 P contains a pack expansion that is not the last
17884 template argument, the entire template argument list
17885 is a non-deduced context. */
17886 return unify_success (explain_p);
17890 /* If we don't have enough arguments to satisfy the parameters
17891 (not counting the pack expression at the end), or we have
17892 too many arguments for a parameter list that doesn't end in
17893 a pack expression, we can't unify. */
17894 if (parm_variadic_p
17895 ? argslen < len - parm_variadic_p
17896 : argslen != len)
17897 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
17899 /* Unify all of the parameters that precede the (optional)
17900 pack expression. */
17901 for (i = 0; i < len - parm_variadic_p; ++i)
17903 RECUR_AND_CHECK_FAILURE (tparms, targs,
17904 TREE_VEC_ELT (parm, i),
17905 TREE_VEC_ELT (arg, i),
17906 UNIFY_ALLOW_NONE, explain_p);
17908 if (parm_variadic_p)
17909 return unify_pack_expansion (tparms, targs, parm, arg,
17910 DEDUCE_EXACT,
17911 /*subr=*/true, explain_p);
17912 return unify_success (explain_p);
17915 case RECORD_TYPE:
17916 case UNION_TYPE:
17917 if (TREE_CODE (arg) != TREE_CODE (parm))
17918 return unify_type_mismatch (explain_p, parm, arg);
17920 if (TYPE_PTRMEMFUNC_P (parm))
17922 if (!TYPE_PTRMEMFUNC_P (arg))
17923 return unify_type_mismatch (explain_p, parm, arg);
17925 return unify (tparms, targs,
17926 TYPE_PTRMEMFUNC_FN_TYPE (parm),
17927 TYPE_PTRMEMFUNC_FN_TYPE (arg),
17928 strict, explain_p);
17931 if (CLASSTYPE_TEMPLATE_INFO (parm))
17933 tree t = NULL_TREE;
17935 if (strict_in & UNIFY_ALLOW_DERIVED)
17937 /* First, we try to unify the PARM and ARG directly. */
17938 t = try_class_unification (tparms, targs,
17939 parm, arg, explain_p);
17941 if (!t)
17943 /* Fallback to the special case allowed in
17944 [temp.deduct.call]:
17946 If P is a class, and P has the form
17947 template-id, then A can be a derived class of
17948 the deduced A. Likewise, if P is a pointer to
17949 a class of the form template-id, A can be a
17950 pointer to a derived class pointed to by the
17951 deduced A. */
17952 enum template_base_result r;
17953 r = get_template_base (tparms, targs, parm, arg,
17954 explain_p, &t);
17956 if (!t)
17957 return unify_no_common_base (explain_p, r, parm, arg);
17960 else if (CLASSTYPE_TEMPLATE_INFO (arg)
17961 && (CLASSTYPE_TI_TEMPLATE (parm)
17962 == CLASSTYPE_TI_TEMPLATE (arg)))
17963 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17964 Then, we should unify `int' and `U'. */
17965 t = arg;
17966 else
17967 /* There's no chance of unification succeeding. */
17968 return unify_type_mismatch (explain_p, parm, arg);
17970 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
17971 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
17973 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
17974 return unify_type_mismatch (explain_p, parm, arg);
17975 return unify_success (explain_p);
17977 case METHOD_TYPE:
17978 case FUNCTION_TYPE:
17980 unsigned int nargs;
17981 tree *args;
17982 tree a;
17983 unsigned int i;
17985 if (TREE_CODE (arg) != TREE_CODE (parm))
17986 return unify_type_mismatch (explain_p, parm, arg);
17988 /* CV qualifications for methods can never be deduced, they must
17989 match exactly. We need to check them explicitly here,
17990 because type_unification_real treats them as any other
17991 cv-qualified parameter. */
17992 if (TREE_CODE (parm) == METHOD_TYPE
17993 && (!check_cv_quals_for_unify
17994 (UNIFY_ALLOW_NONE,
17995 class_of_this_parm (arg),
17996 class_of_this_parm (parm))))
17997 return unify_cv_qual_mismatch (explain_p, parm, arg);
17999 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
18000 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
18002 nargs = list_length (TYPE_ARG_TYPES (arg));
18003 args = XALLOCAVEC (tree, nargs);
18004 for (a = TYPE_ARG_TYPES (arg), i = 0;
18005 a != NULL_TREE && a != void_list_node;
18006 a = TREE_CHAIN (a), ++i)
18007 args[i] = TREE_VALUE (a);
18008 nargs = i;
18010 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
18011 args, nargs, 1, DEDUCE_EXACT,
18012 LOOKUP_NORMAL, NULL, explain_p);
18015 case OFFSET_TYPE:
18016 /* Unify a pointer to member with a pointer to member function, which
18017 deduces the type of the member as a function type. */
18018 if (TYPE_PTRMEMFUNC_P (arg))
18020 /* Check top-level cv qualifiers */
18021 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
18022 return unify_cv_qual_mismatch (explain_p, parm, arg);
18024 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18025 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
18026 UNIFY_ALLOW_NONE, explain_p);
18028 /* Determine the type of the function we are unifying against. */
18029 tree fntype = static_fn_type (arg);
18031 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
18034 if (TREE_CODE (arg) != OFFSET_TYPE)
18035 return unify_type_mismatch (explain_p, parm, arg);
18036 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18037 TYPE_OFFSET_BASETYPE (arg),
18038 UNIFY_ALLOW_NONE, explain_p);
18039 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18040 strict, explain_p);
18042 case CONST_DECL:
18043 if (DECL_TEMPLATE_PARM_P (parm))
18044 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
18045 if (arg != integral_constant_value (parm))
18046 return unify_template_argument_mismatch (explain_p, parm, arg);
18047 return unify_success (explain_p);
18049 case FIELD_DECL:
18050 case TEMPLATE_DECL:
18051 /* Matched cases are handled by the ARG == PARM test above. */
18052 return unify_template_argument_mismatch (explain_p, parm, arg);
18054 case VAR_DECL:
18055 /* A non-type template parameter that is a variable should be a
18056 an integral constant, in which case, it whould have been
18057 folded into its (constant) value. So we should not be getting
18058 a variable here. */
18059 gcc_unreachable ();
18061 case TYPE_ARGUMENT_PACK:
18062 case NONTYPE_ARGUMENT_PACK:
18063 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
18064 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
18066 case TYPEOF_TYPE:
18067 case DECLTYPE_TYPE:
18068 case UNDERLYING_TYPE:
18069 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18070 or UNDERLYING_TYPE nodes. */
18071 return unify_success (explain_p);
18073 case ERROR_MARK:
18074 /* Unification fails if we hit an error node. */
18075 return unify_invalid (explain_p);
18077 case INDIRECT_REF:
18078 if (REFERENCE_REF_P (parm))
18079 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
18080 strict, explain_p);
18081 /* FALLTHRU */
18083 default:
18084 /* An unresolved overload is a nondeduced context. */
18085 if (is_overloaded_fn (parm) || type_unknown_p (parm))
18086 return unify_success (explain_p);
18087 gcc_assert (EXPR_P (parm));
18089 /* We must be looking at an expression. This can happen with
18090 something like:
18092 template <int I>
18093 void foo(S<I>, S<I + 2>);
18095 This is a "nondeduced context":
18097 [deduct.type]
18099 The nondeduced contexts are:
18101 --A type that is a template-id in which one or more of
18102 the template-arguments is an expression that references
18103 a template-parameter.
18105 In these cases, we assume deduction succeeded, but don't
18106 actually infer any unifications. */
18108 if (!uses_template_parms (parm)
18109 && !template_args_equal (parm, arg))
18110 return unify_expression_unequal (explain_p, parm, arg);
18111 else
18112 return unify_success (explain_p);
18115 #undef RECUR_AND_CHECK_FAILURE
18117 /* Note that DECL can be defined in this translation unit, if
18118 required. */
18120 static void
18121 mark_definable (tree decl)
18123 tree clone;
18124 DECL_NOT_REALLY_EXTERN (decl) = 1;
18125 FOR_EACH_CLONE (clone, decl)
18126 DECL_NOT_REALLY_EXTERN (clone) = 1;
18129 /* Called if RESULT is explicitly instantiated, or is a member of an
18130 explicitly instantiated class. */
18132 void
18133 mark_decl_instantiated (tree result, int extern_p)
18135 SET_DECL_EXPLICIT_INSTANTIATION (result);
18137 /* If this entity has already been written out, it's too late to
18138 make any modifications. */
18139 if (TREE_ASM_WRITTEN (result))
18140 return;
18142 /* For anonymous namespace we don't need to do anything. */
18143 if (decl_anon_ns_mem_p (result))
18145 gcc_assert (!TREE_PUBLIC (result));
18146 return;
18149 if (TREE_CODE (result) != FUNCTION_DECL)
18150 /* The TREE_PUBLIC flag for function declarations will have been
18151 set correctly by tsubst. */
18152 TREE_PUBLIC (result) = 1;
18154 /* This might have been set by an earlier implicit instantiation. */
18155 DECL_COMDAT (result) = 0;
18157 if (extern_p)
18158 DECL_NOT_REALLY_EXTERN (result) = 0;
18159 else
18161 mark_definable (result);
18162 mark_needed (result);
18163 /* Always make artificials weak. */
18164 if (DECL_ARTIFICIAL (result) && flag_weak)
18165 comdat_linkage (result);
18166 /* For WIN32 we also want to put explicit instantiations in
18167 linkonce sections. */
18168 else if (TREE_PUBLIC (result))
18169 maybe_make_one_only (result);
18172 /* If EXTERN_P, then this function will not be emitted -- unless
18173 followed by an explicit instantiation, at which point its linkage
18174 will be adjusted. If !EXTERN_P, then this function will be
18175 emitted here. In neither circumstance do we want
18176 import_export_decl to adjust the linkage. */
18177 DECL_INTERFACE_KNOWN (result) = 1;
18180 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18181 important template arguments. If any are missing, we check whether
18182 they're important by using error_mark_node for substituting into any
18183 args that were used for partial ordering (the ones between ARGS and END)
18184 and seeing if it bubbles up. */
18186 static bool
18187 check_undeduced_parms (tree targs, tree args, tree end)
18189 bool found = false;
18190 int i;
18191 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
18192 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
18194 found = true;
18195 TREE_VEC_ELT (targs, i) = error_mark_node;
18197 if (found)
18199 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
18200 if (substed == error_mark_node)
18201 return true;
18203 return false;
18206 /* Given two function templates PAT1 and PAT2, return:
18208 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18209 -1 if PAT2 is more specialized than PAT1.
18210 0 if neither is more specialized.
18212 LEN indicates the number of parameters we should consider
18213 (defaulted parameters should not be considered).
18215 The 1998 std underspecified function template partial ordering, and
18216 DR214 addresses the issue. We take pairs of arguments, one from
18217 each of the templates, and deduce them against each other. One of
18218 the templates will be more specialized if all the *other*
18219 template's arguments deduce against its arguments and at least one
18220 of its arguments *does* *not* deduce against the other template's
18221 corresponding argument. Deduction is done as for class templates.
18222 The arguments used in deduction have reference and top level cv
18223 qualifiers removed. Iff both arguments were originally reference
18224 types *and* deduction succeeds in both directions, an lvalue reference
18225 wins against an rvalue reference and otherwise the template
18226 with the more cv-qualified argument wins for that pairing (if
18227 neither is more cv-qualified, they both are equal). Unlike regular
18228 deduction, after all the arguments have been deduced in this way,
18229 we do *not* verify the deduced template argument values can be
18230 substituted into non-deduced contexts.
18232 The logic can be a bit confusing here, because we look at deduce1 and
18233 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18234 can find template arguments for pat1 to make arg1 look like arg2, that
18235 means that arg2 is at least as specialized as arg1. */
18238 more_specialized_fn (tree pat1, tree pat2, int len)
18240 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18241 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18242 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18243 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18244 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18245 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18246 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18247 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18248 tree origs1, origs2;
18249 bool lose1 = false;
18250 bool lose2 = false;
18252 /* Remove the this parameter from non-static member functions. If
18253 one is a non-static member function and the other is not a static
18254 member function, remove the first parameter from that function
18255 also. This situation occurs for operator functions where we
18256 locate both a member function (with this pointer) and non-member
18257 operator (with explicit first operand). */
18258 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18260 len--; /* LEN is the number of significant arguments for DECL1 */
18261 args1 = TREE_CHAIN (args1);
18262 if (!DECL_STATIC_FUNCTION_P (decl2))
18263 args2 = TREE_CHAIN (args2);
18265 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18267 args2 = TREE_CHAIN (args2);
18268 if (!DECL_STATIC_FUNCTION_P (decl1))
18270 len--;
18271 args1 = TREE_CHAIN (args1);
18275 /* If only one is a conversion operator, they are unordered. */
18276 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18277 return 0;
18279 /* Consider the return type for a conversion function */
18280 if (DECL_CONV_FN_P (decl1))
18282 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18283 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18284 len++;
18287 processing_template_decl++;
18289 origs1 = args1;
18290 origs2 = args2;
18292 while (len--
18293 /* Stop when an ellipsis is seen. */
18294 && args1 != NULL_TREE && args2 != NULL_TREE)
18296 tree arg1 = TREE_VALUE (args1);
18297 tree arg2 = TREE_VALUE (args2);
18298 int deduce1, deduce2;
18299 int quals1 = -1;
18300 int quals2 = -1;
18301 int ref1 = 0;
18302 int ref2 = 0;
18304 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18305 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18307 /* When both arguments are pack expansions, we need only
18308 unify the patterns themselves. */
18309 arg1 = PACK_EXPANSION_PATTERN (arg1);
18310 arg2 = PACK_EXPANSION_PATTERN (arg2);
18312 /* This is the last comparison we need to do. */
18313 len = 0;
18316 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18318 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18319 arg1 = TREE_TYPE (arg1);
18320 quals1 = cp_type_quals (arg1);
18323 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18325 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18326 arg2 = TREE_TYPE (arg2);
18327 quals2 = cp_type_quals (arg2);
18330 arg1 = TYPE_MAIN_VARIANT (arg1);
18331 arg2 = TYPE_MAIN_VARIANT (arg2);
18333 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18335 int i, len2 = list_length (args2);
18336 tree parmvec = make_tree_vec (1);
18337 tree argvec = make_tree_vec (len2);
18338 tree ta = args2;
18340 /* Setup the parameter vector, which contains only ARG1. */
18341 TREE_VEC_ELT (parmvec, 0) = arg1;
18343 /* Setup the argument vector, which contains the remaining
18344 arguments. */
18345 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18346 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18348 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18349 argvec, DEDUCE_EXACT,
18350 /*subr=*/true, /*explain_p=*/false)
18351 == 0);
18353 /* We cannot deduce in the other direction, because ARG1 is
18354 a pack expansion but ARG2 is not. */
18355 deduce2 = 0;
18357 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18359 int i, len1 = list_length (args1);
18360 tree parmvec = make_tree_vec (1);
18361 tree argvec = make_tree_vec (len1);
18362 tree ta = args1;
18364 /* Setup the parameter vector, which contains only ARG1. */
18365 TREE_VEC_ELT (parmvec, 0) = arg2;
18367 /* Setup the argument vector, which contains the remaining
18368 arguments. */
18369 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18370 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18372 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18373 argvec, DEDUCE_EXACT,
18374 /*subr=*/true, /*explain_p=*/false)
18375 == 0);
18377 /* We cannot deduce in the other direction, because ARG2 is
18378 a pack expansion but ARG1 is not.*/
18379 deduce1 = 0;
18382 else
18384 /* The normal case, where neither argument is a pack
18385 expansion. */
18386 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18387 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18388 == 0);
18389 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18390 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18391 == 0);
18394 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18395 arg2, then arg2 is not as specialized as arg1. */
18396 if (!deduce1)
18397 lose2 = true;
18398 if (!deduce2)
18399 lose1 = true;
18401 /* "If, for a given type, deduction succeeds in both directions
18402 (i.e., the types are identical after the transformations above)
18403 and both P and A were reference types (before being replaced with
18404 the type referred to above):
18405 - if the type from the argument template was an lvalue reference and
18406 the type from the parameter template was not, the argument type is
18407 considered to be more specialized than the other; otherwise,
18408 - if the type from the argument template is more cv-qualified
18409 than the type from the parameter template (as described above),
18410 the argument type is considered to be more specialized than the other;
18411 otherwise,
18412 - neither type is more specialized than the other." */
18414 if (deduce1 && deduce2)
18416 if (ref1 && ref2 && ref1 != ref2)
18418 if (ref1 > ref2)
18419 lose1 = true;
18420 else
18421 lose2 = true;
18423 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18425 if ((quals1 & quals2) == quals2)
18426 lose2 = true;
18427 if ((quals1 & quals2) == quals1)
18428 lose1 = true;
18432 if (lose1 && lose2)
18433 /* We've failed to deduce something in either direction.
18434 These must be unordered. */
18435 break;
18437 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18438 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18439 /* We have already processed all of the arguments in our
18440 handing of the pack expansion type. */
18441 len = 0;
18443 args1 = TREE_CHAIN (args1);
18444 args2 = TREE_CHAIN (args2);
18447 /* "In most cases, all template parameters must have values in order for
18448 deduction to succeed, but for partial ordering purposes a template
18449 parameter may remain without a value provided it is not used in the
18450 types being used for partial ordering."
18452 Thus, if we are missing any of the targs1 we need to substitute into
18453 origs1, then pat2 is not as specialized as pat1. This can happen when
18454 there is a nondeduced context. */
18455 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18456 lose2 = true;
18457 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18458 lose1 = true;
18460 processing_template_decl--;
18462 /* All things being equal, if the next argument is a pack expansion
18463 for one function but not for the other, prefer the
18464 non-variadic function. FIXME this is bogus; see c++/41958. */
18465 if (lose1 == lose2
18466 && args1 && TREE_VALUE (args1)
18467 && args2 && TREE_VALUE (args2))
18469 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18470 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18473 if (lose1 == lose2)
18474 return 0;
18475 else if (!lose1)
18476 return 1;
18477 else
18478 return -1;
18481 /* Determine which of two partial specializations of TMPL is more
18482 specialized.
18484 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18485 to the first partial specialization. The TREE_VALUE is the
18486 innermost set of template parameters for the partial
18487 specialization. PAT2 is similar, but for the second template.
18489 Return 1 if the first partial specialization is more specialized;
18490 -1 if the second is more specialized; 0 if neither is more
18491 specialized.
18493 See [temp.class.order] for information about determining which of
18494 two templates is more specialized. */
18496 static int
18497 more_specialized_class (tree tmpl, tree pat1, tree pat2)
18499 tree targs;
18500 tree tmpl1, tmpl2;
18501 int winner = 0;
18502 bool any_deductions = false;
18504 tmpl1 = TREE_TYPE (pat1);
18505 tmpl2 = TREE_TYPE (pat2);
18507 /* Just like what happens for functions, if we are ordering between
18508 different class template specializations, we may encounter dependent
18509 types in the arguments, and we need our dependency check functions
18510 to behave correctly. */
18511 ++processing_template_decl;
18512 targs = get_class_bindings (tmpl, TREE_VALUE (pat1),
18513 CLASSTYPE_TI_ARGS (tmpl1),
18514 CLASSTYPE_TI_ARGS (tmpl2));
18515 if (targs)
18517 --winner;
18518 any_deductions = true;
18521 targs = get_class_bindings (tmpl, TREE_VALUE (pat2),
18522 CLASSTYPE_TI_ARGS (tmpl2),
18523 CLASSTYPE_TI_ARGS (tmpl1));
18524 if (targs)
18526 ++winner;
18527 any_deductions = true;
18529 --processing_template_decl;
18531 /* In the case of a tie where at least one of the class templates
18532 has a parameter pack at the end, the template with the most
18533 non-packed parameters wins. */
18534 if (winner == 0
18535 && any_deductions
18536 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18537 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18539 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18540 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18541 int len1 = TREE_VEC_LENGTH (args1);
18542 int len2 = TREE_VEC_LENGTH (args2);
18544 /* We don't count the pack expansion at the end. */
18545 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18546 --len1;
18547 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18548 --len2;
18550 if (len1 > len2)
18551 return 1;
18552 else if (len1 < len2)
18553 return -1;
18556 return winner;
18559 /* Return the template arguments that will produce the function signature
18560 DECL from the function template FN, with the explicit template
18561 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18562 also match. Return NULL_TREE if no satisfactory arguments could be
18563 found. */
18565 static tree
18566 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18568 int ntparms = DECL_NTPARMS (fn);
18569 tree targs = make_tree_vec (ntparms);
18570 tree decl_type = TREE_TYPE (decl);
18571 tree decl_arg_types;
18572 tree *args;
18573 unsigned int nargs, ix;
18574 tree arg;
18576 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18578 /* Never do unification on the 'this' parameter. */
18579 decl_arg_types = skip_artificial_parms_for (decl,
18580 TYPE_ARG_TYPES (decl_type));
18582 nargs = list_length (decl_arg_types);
18583 args = XALLOCAVEC (tree, nargs);
18584 for (arg = decl_arg_types, ix = 0;
18585 arg != NULL_TREE && arg != void_list_node;
18586 arg = TREE_CHAIN (arg), ++ix)
18587 args[ix] = TREE_VALUE (arg);
18589 if (fn_type_unification (fn, explicit_args, targs,
18590 args, ix,
18591 (check_rettype || DECL_CONV_FN_P (fn)
18592 ? TREE_TYPE (decl_type) : NULL_TREE),
18593 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18594 /*decltype*/false)
18595 == error_mark_node)
18596 return NULL_TREE;
18598 return targs;
18601 /* Return the innermost template arguments that, when applied to a partial
18602 specialization of TMPL whose innermost template parameters are
18603 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18604 ARGS.
18606 For example, suppose we have:
18608 template <class T, class U> struct S {};
18609 template <class T> struct S<T*, int> {};
18611 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18612 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18613 int}. The resulting vector will be {double}, indicating that `T'
18614 is bound to `double'. */
18616 static tree
18617 get_class_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
18619 int i, ntparms = TREE_VEC_LENGTH (tparms);
18620 tree deduced_args;
18621 tree innermost_deduced_args;
18623 innermost_deduced_args = make_tree_vec (ntparms);
18624 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18626 deduced_args = copy_node (args);
18627 SET_TMPL_ARGS_LEVEL (deduced_args,
18628 TMPL_ARGS_DEPTH (deduced_args),
18629 innermost_deduced_args);
18631 else
18632 deduced_args = innermost_deduced_args;
18634 if (unify (tparms, deduced_args,
18635 INNERMOST_TEMPLATE_ARGS (spec_args),
18636 INNERMOST_TEMPLATE_ARGS (args),
18637 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18638 return NULL_TREE;
18640 for (i = 0; i < ntparms; ++i)
18641 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18642 return NULL_TREE;
18644 /* Verify that nondeduced template arguments agree with the type
18645 obtained from argument deduction.
18647 For example:
18649 struct A { typedef int X; };
18650 template <class T, class U> struct C {};
18651 template <class T> struct C<T, typename T::X> {};
18653 Then with the instantiation `C<A, int>', we can deduce that
18654 `T' is `A' but unify () does not check whether `typename T::X'
18655 is `int'. */
18656 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18657 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18658 spec_args, tmpl,
18659 tf_none, false, false);
18660 if (spec_args == error_mark_node
18661 /* We only need to check the innermost arguments; the other
18662 arguments will always agree. */
18663 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18664 INNERMOST_TEMPLATE_ARGS (args)))
18665 return NULL_TREE;
18667 /* Now that we have bindings for all of the template arguments,
18668 ensure that the arguments deduced for the template template
18669 parameters have compatible template parameter lists. See the use
18670 of template_template_parm_bindings_ok_p in fn_type_unification
18671 for more information. */
18672 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18673 return NULL_TREE;
18675 return deduced_args;
18678 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18679 Return the TREE_LIST node with the most specialized template, if
18680 any. If there is no most specialized template, the error_mark_node
18681 is returned.
18683 Note that this function does not look at, or modify, the
18684 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18685 returned is one of the elements of INSTANTIATIONS, callers may
18686 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18687 and retrieve it from the value returned. */
18689 tree
18690 most_specialized_instantiation (tree templates)
18692 tree fn, champ;
18694 ++processing_template_decl;
18696 champ = templates;
18697 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18699 int fate = 0;
18701 if (get_bindings (TREE_VALUE (champ),
18702 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18703 NULL_TREE, /*check_ret=*/true))
18704 fate--;
18706 if (get_bindings (TREE_VALUE (fn),
18707 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18708 NULL_TREE, /*check_ret=*/true))
18709 fate++;
18711 if (fate == -1)
18712 champ = fn;
18713 else if (!fate)
18715 /* Equally specialized, move to next function. If there
18716 is no next function, nothing's most specialized. */
18717 fn = TREE_CHAIN (fn);
18718 champ = fn;
18719 if (!fn)
18720 break;
18724 if (champ)
18725 /* Now verify that champ is better than everything earlier in the
18726 instantiation list. */
18727 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18728 if (get_bindings (TREE_VALUE (champ),
18729 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18730 NULL_TREE, /*check_ret=*/true)
18731 || !get_bindings (TREE_VALUE (fn),
18732 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18733 NULL_TREE, /*check_ret=*/true))
18735 champ = NULL_TREE;
18736 break;
18739 processing_template_decl--;
18741 if (!champ)
18742 return error_mark_node;
18744 return champ;
18747 /* If DECL is a specialization of some template, return the most
18748 general such template. Otherwise, returns NULL_TREE.
18750 For example, given:
18752 template <class T> struct S { template <class U> void f(U); };
18754 if TMPL is `template <class U> void S<int>::f(U)' this will return
18755 the full template. This function will not trace past partial
18756 specializations, however. For example, given in addition:
18758 template <class T> struct S<T*> { template <class U> void f(U); };
18760 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18761 `template <class T> template <class U> S<T*>::f(U)'. */
18763 tree
18764 most_general_template (tree decl)
18766 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18767 an immediate specialization. */
18768 if (TREE_CODE (decl) == FUNCTION_DECL)
18770 if (DECL_TEMPLATE_INFO (decl)) {
18771 decl = DECL_TI_TEMPLATE (decl);
18773 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18774 template friend. */
18775 if (TREE_CODE (decl) != TEMPLATE_DECL)
18776 return NULL_TREE;
18777 } else
18778 return NULL_TREE;
18781 /* Look for more and more general templates. */
18782 while (DECL_TEMPLATE_INFO (decl))
18784 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18785 (See cp-tree.h for details.) */
18786 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18787 break;
18789 if (CLASS_TYPE_P (TREE_TYPE (decl))
18790 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18791 break;
18793 /* Stop if we run into an explicitly specialized class template. */
18794 if (!DECL_NAMESPACE_SCOPE_P (decl)
18795 && DECL_CONTEXT (decl)
18796 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18797 break;
18799 decl = DECL_TI_TEMPLATE (decl);
18802 return decl;
18805 /* Return the most specialized of the class template partial
18806 specializations which can produce TYPE, a specialization of some class
18807 template. The value returned is actually a TREE_LIST; the TREE_TYPE is
18808 a _TYPE node corresponding to the partial specialization, while the
18809 TREE_PURPOSE is the set of template arguments that must be
18810 substituted into the TREE_TYPE in order to generate TYPE.
18812 If the choice of partial specialization is ambiguous, a diagnostic
18813 is issued, and the error_mark_node is returned. If there are no
18814 partial specializations matching TYPE, then NULL_TREE is
18815 returned, indicating that the primary template should be used. */
18817 static tree
18818 most_specialized_class (tree type, tsubst_flags_t complain)
18820 tree list = NULL_TREE;
18821 tree t;
18822 tree champ;
18823 int fate;
18824 bool ambiguous_p;
18825 tree outer_args = NULL_TREE;
18827 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
18828 tree main_tmpl = most_general_template (tmpl);
18829 tree args = CLASSTYPE_TI_ARGS (type);
18831 /* For determining which partial specialization to use, only the
18832 innermost args are interesting. */
18833 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18835 outer_args = strip_innermost_template_args (args, 1);
18836 args = INNERMOST_TEMPLATE_ARGS (args);
18839 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
18841 tree partial_spec_args;
18842 tree spec_args;
18843 tree spec_tmpl = TREE_VALUE (t);
18844 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18846 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
18848 ++processing_template_decl;
18850 if (outer_args)
18852 /* Discard the outer levels of args, and then substitute in the
18853 template args from the enclosing class. */
18854 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
18855 partial_spec_args = tsubst_template_args
18856 (partial_spec_args, outer_args, tf_none, NULL_TREE);
18858 /* And the same for the partial specialization TEMPLATE_DECL. */
18859 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
18862 partial_spec_args =
18863 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18864 partial_spec_args,
18865 tmpl, tf_none,
18866 /*require_all_args=*/true,
18867 /*use_default_args=*/true);
18869 --processing_template_decl;
18871 if (partial_spec_args == error_mark_node)
18872 return error_mark_node;
18873 if (spec_tmpl == error_mark_node)
18874 return error_mark_node;
18876 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18877 spec_args = get_class_bindings (tmpl, parms,
18878 partial_spec_args,
18879 args);
18880 if (spec_args)
18882 if (outer_args)
18883 spec_args = add_to_template_args (outer_args, spec_args);
18884 list = tree_cons (spec_args, orig_parms, list);
18885 TREE_TYPE (list) = TREE_TYPE (t);
18889 if (! list)
18890 return NULL_TREE;
18892 ambiguous_p = false;
18893 t = list;
18894 champ = t;
18895 t = TREE_CHAIN (t);
18896 for (; t; t = TREE_CHAIN (t))
18898 fate = more_specialized_class (tmpl, champ, t);
18899 if (fate == 1)
18901 else
18903 if (fate == 0)
18905 t = TREE_CHAIN (t);
18906 if (! t)
18908 ambiguous_p = true;
18909 break;
18912 champ = t;
18916 if (!ambiguous_p)
18917 for (t = list; t && t != champ; t = TREE_CHAIN (t))
18919 fate = more_specialized_class (tmpl, champ, t);
18920 if (fate != 1)
18922 ambiguous_p = true;
18923 break;
18927 if (ambiguous_p)
18929 const char *str;
18930 char *spaces = NULL;
18931 if (!(complain & tf_error))
18932 return error_mark_node;
18933 error ("ambiguous class template instantiation for %q#T", type);
18934 str = ngettext ("candidate is:", "candidates are:", list_length (list));
18935 for (t = list; t; t = TREE_CHAIN (t))
18937 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
18938 spaces = spaces ? spaces : get_spaces (str);
18940 free (spaces);
18941 return error_mark_node;
18944 return champ;
18947 /* Explicitly instantiate DECL. */
18949 void
18950 do_decl_instantiation (tree decl, tree storage)
18952 tree result = NULL_TREE;
18953 int extern_p = 0;
18955 if (!decl || decl == error_mark_node)
18956 /* An error occurred, for which grokdeclarator has already issued
18957 an appropriate message. */
18958 return;
18959 else if (! DECL_LANG_SPECIFIC (decl))
18961 error ("explicit instantiation of non-template %q#D", decl);
18962 return;
18964 else if (VAR_P (decl))
18966 /* There is an asymmetry here in the way VAR_DECLs and
18967 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18968 the latter, the DECL we get back will be marked as a
18969 template instantiation, and the appropriate
18970 DECL_TEMPLATE_INFO will be set up. This does not happen for
18971 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18972 should handle VAR_DECLs as it currently handles
18973 FUNCTION_DECLs. */
18974 if (!DECL_CLASS_SCOPE_P (decl))
18976 error ("%qD is not a static data member of a class template", decl);
18977 return;
18979 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
18980 if (!result || !VAR_P (result))
18982 error ("no matching template for %qD found", decl);
18983 return;
18985 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
18987 error ("type %qT for explicit instantiation %qD does not match "
18988 "declared type %qT", TREE_TYPE (result), decl,
18989 TREE_TYPE (decl));
18990 return;
18993 else if (TREE_CODE (decl) != FUNCTION_DECL)
18995 error ("explicit instantiation of %q#D", decl);
18996 return;
18998 else
18999 result = decl;
19001 /* Check for various error cases. Note that if the explicit
19002 instantiation is valid the RESULT will currently be marked as an
19003 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
19004 until we get here. */
19006 if (DECL_TEMPLATE_SPECIALIZATION (result))
19008 /* DR 259 [temp.spec].
19010 Both an explicit instantiation and a declaration of an explicit
19011 specialization shall not appear in a program unless the explicit
19012 instantiation follows a declaration of the explicit specialization.
19014 For a given set of template parameters, if an explicit
19015 instantiation of a template appears after a declaration of an
19016 explicit specialization for that template, the explicit
19017 instantiation has no effect. */
19018 return;
19020 else if (DECL_EXPLICIT_INSTANTIATION (result))
19022 /* [temp.spec]
19024 No program shall explicitly instantiate any template more
19025 than once.
19027 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19028 the first instantiation was `extern' and the second is not,
19029 and EXTERN_P for the opposite case. */
19030 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
19031 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
19032 /* If an "extern" explicit instantiation follows an ordinary
19033 explicit instantiation, the template is instantiated. */
19034 if (extern_p)
19035 return;
19037 else if (!DECL_IMPLICIT_INSTANTIATION (result))
19039 error ("no matching template for %qD found", result);
19040 return;
19042 else if (!DECL_TEMPLATE_INFO (result))
19044 permerror (input_location, "explicit instantiation of non-template %q#D", result);
19045 return;
19048 if (storage == NULL_TREE)
19050 else if (storage == ridpointers[(int) RID_EXTERN])
19052 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
19053 pedwarn (input_location, OPT_Wpedantic,
19054 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19055 "instantiations");
19056 extern_p = 1;
19058 else
19059 error ("storage class %qD applied to template instantiation", storage);
19061 check_explicit_instantiation_namespace (result);
19062 mark_decl_instantiated (result, extern_p);
19063 if (! extern_p)
19064 instantiate_decl (result, /*defer_ok=*/1,
19065 /*expl_inst_class_mem_p=*/false);
19068 static void
19069 mark_class_instantiated (tree t, int extern_p)
19071 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
19072 SET_CLASSTYPE_INTERFACE_KNOWN (t);
19073 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
19074 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
19075 if (! extern_p)
19077 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
19078 rest_of_type_compilation (t, 1);
19082 /* Called from do_type_instantiation through binding_table_foreach to
19083 do recursive instantiation for the type bound in ENTRY. */
19084 static void
19085 bt_instantiate_type_proc (binding_entry entry, void *data)
19087 tree storage = *(tree *) data;
19089 if (MAYBE_CLASS_TYPE_P (entry->type)
19090 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
19091 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
19094 /* Called from do_type_instantiation to instantiate a member
19095 (a member function or a static member variable) of an
19096 explicitly instantiated class template. */
19097 static void
19098 instantiate_class_member (tree decl, int extern_p)
19100 mark_decl_instantiated (decl, extern_p);
19101 if (! extern_p)
19102 instantiate_decl (decl, /*defer_ok=*/1,
19103 /*expl_inst_class_mem_p=*/true);
19106 /* Perform an explicit instantiation of template class T. STORAGE, if
19107 non-null, is the RID for extern, inline or static. COMPLAIN is
19108 nonzero if this is called from the parser, zero if called recursively,
19109 since the standard is unclear (as detailed below). */
19111 void
19112 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
19114 int extern_p = 0;
19115 int nomem_p = 0;
19116 int static_p = 0;
19117 int previous_instantiation_extern_p = 0;
19119 if (TREE_CODE (t) == TYPE_DECL)
19120 t = TREE_TYPE (t);
19122 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
19124 tree tmpl =
19125 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
19126 if (tmpl)
19127 error ("explicit instantiation of non-class template %qD", tmpl);
19128 else
19129 error ("explicit instantiation of non-template type %qT", t);
19130 return;
19133 complete_type (t);
19135 if (!COMPLETE_TYPE_P (t))
19137 if (complain & tf_error)
19138 error ("explicit instantiation of %q#T before definition of template",
19140 return;
19143 if (storage != NULL_TREE)
19145 if (!in_system_header_at (input_location))
19147 if (storage == ridpointers[(int) RID_EXTERN])
19149 if (cxx_dialect == cxx98)
19150 pedwarn (input_location, OPT_Wpedantic,
19151 "ISO C++ 1998 forbids the use of %<extern%> on "
19152 "explicit instantiations");
19154 else
19155 pedwarn (input_location, OPT_Wpedantic,
19156 "ISO C++ forbids the use of %qE"
19157 " on explicit instantiations", storage);
19160 if (storage == ridpointers[(int) RID_INLINE])
19161 nomem_p = 1;
19162 else if (storage == ridpointers[(int) RID_EXTERN])
19163 extern_p = 1;
19164 else if (storage == ridpointers[(int) RID_STATIC])
19165 static_p = 1;
19166 else
19168 error ("storage class %qD applied to template instantiation",
19169 storage);
19170 extern_p = 0;
19174 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
19176 /* DR 259 [temp.spec].
19178 Both an explicit instantiation and a declaration of an explicit
19179 specialization shall not appear in a program unless the explicit
19180 instantiation follows a declaration of the explicit specialization.
19182 For a given set of template parameters, if an explicit
19183 instantiation of a template appears after a declaration of an
19184 explicit specialization for that template, the explicit
19185 instantiation has no effect. */
19186 return;
19188 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
19190 /* [temp.spec]
19192 No program shall explicitly instantiate any template more
19193 than once.
19195 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19196 instantiation was `extern'. If EXTERN_P then the second is.
19197 These cases are OK. */
19198 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
19200 if (!previous_instantiation_extern_p && !extern_p
19201 && (complain & tf_error))
19202 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
19204 /* If we've already instantiated the template, just return now. */
19205 if (!CLASSTYPE_INTERFACE_ONLY (t))
19206 return;
19209 check_explicit_instantiation_namespace (TYPE_NAME (t));
19210 mark_class_instantiated (t, extern_p);
19212 if (nomem_p)
19213 return;
19216 tree tmp;
19218 /* In contrast to implicit instantiation, where only the
19219 declarations, and not the definitions, of members are
19220 instantiated, we have here:
19222 [temp.explicit]
19224 The explicit instantiation of a class template specialization
19225 implies the instantiation of all of its members not
19226 previously explicitly specialized in the translation unit
19227 containing the explicit instantiation.
19229 Of course, we can't instantiate member template classes, since
19230 we don't have any arguments for them. Note that the standard
19231 is unclear on whether the instantiation of the members are
19232 *explicit* instantiations or not. However, the most natural
19233 interpretation is that it should be an explicit instantiation. */
19235 if (! static_p)
19236 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
19237 if (TREE_CODE (tmp) == FUNCTION_DECL
19238 && DECL_TEMPLATE_INSTANTIATION (tmp))
19239 instantiate_class_member (tmp, extern_p);
19241 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19242 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19243 instantiate_class_member (tmp, extern_p);
19245 if (CLASSTYPE_NESTED_UTDS (t))
19246 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19247 bt_instantiate_type_proc, &storage);
19251 /* Given a function DECL, which is a specialization of TMPL, modify
19252 DECL to be a re-instantiation of TMPL with the same template
19253 arguments. TMPL should be the template into which tsubst'ing
19254 should occur for DECL, not the most general template.
19256 One reason for doing this is a scenario like this:
19258 template <class T>
19259 void f(const T&, int i);
19261 void g() { f(3, 7); }
19263 template <class T>
19264 void f(const T& t, const int i) { }
19266 Note that when the template is first instantiated, with
19267 instantiate_template, the resulting DECL will have no name for the
19268 first parameter, and the wrong type for the second. So, when we go
19269 to instantiate the DECL, we regenerate it. */
19271 static void
19272 regenerate_decl_from_template (tree decl, tree tmpl)
19274 /* The arguments used to instantiate DECL, from the most general
19275 template. */
19276 tree args;
19277 tree code_pattern;
19279 args = DECL_TI_ARGS (decl);
19280 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19282 /* Make sure that we can see identifiers, and compute access
19283 correctly. */
19284 push_access_scope (decl);
19286 if (TREE_CODE (decl) == FUNCTION_DECL)
19288 tree decl_parm;
19289 tree pattern_parm;
19290 tree specs;
19291 int args_depth;
19292 int parms_depth;
19294 args_depth = TMPL_ARGS_DEPTH (args);
19295 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19296 if (args_depth > parms_depth)
19297 args = get_innermost_template_args (args, parms_depth);
19299 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19300 args, tf_error, NULL_TREE,
19301 /*defer_ok*/false);
19302 if (specs && specs != error_mark_node)
19303 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19304 specs);
19306 /* Merge parameter declarations. */
19307 decl_parm = skip_artificial_parms_for (decl,
19308 DECL_ARGUMENTS (decl));
19309 pattern_parm
19310 = skip_artificial_parms_for (code_pattern,
19311 DECL_ARGUMENTS (code_pattern));
19312 while (decl_parm && !DECL_PACK_P (pattern_parm))
19314 tree parm_type;
19315 tree attributes;
19317 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19318 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19319 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19320 NULL_TREE);
19321 parm_type = type_decays_to (parm_type);
19322 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19323 TREE_TYPE (decl_parm) = parm_type;
19324 attributes = DECL_ATTRIBUTES (pattern_parm);
19325 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19327 DECL_ATTRIBUTES (decl_parm) = attributes;
19328 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19330 decl_parm = DECL_CHAIN (decl_parm);
19331 pattern_parm = DECL_CHAIN (pattern_parm);
19333 /* Merge any parameters that match with the function parameter
19334 pack. */
19335 if (pattern_parm && DECL_PACK_P (pattern_parm))
19337 int i, len;
19338 tree expanded_types;
19339 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19340 the parameters in this function parameter pack. */
19341 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19342 args, tf_error, NULL_TREE);
19343 len = TREE_VEC_LENGTH (expanded_types);
19344 for (i = 0; i < len; i++)
19346 tree parm_type;
19347 tree attributes;
19349 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19350 /* Rename the parameter to include the index. */
19351 DECL_NAME (decl_parm) =
19352 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19353 parm_type = TREE_VEC_ELT (expanded_types, i);
19354 parm_type = type_decays_to (parm_type);
19355 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19356 TREE_TYPE (decl_parm) = parm_type;
19357 attributes = DECL_ATTRIBUTES (pattern_parm);
19358 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19360 DECL_ATTRIBUTES (decl_parm) = attributes;
19361 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19363 decl_parm = DECL_CHAIN (decl_parm);
19366 /* Merge additional specifiers from the CODE_PATTERN. */
19367 if (DECL_DECLARED_INLINE_P (code_pattern)
19368 && !DECL_DECLARED_INLINE_P (decl))
19369 DECL_DECLARED_INLINE_P (decl) = 1;
19371 else if (VAR_P (decl))
19373 DECL_INITIAL (decl) =
19374 tsubst_expr (DECL_INITIAL (code_pattern), args,
19375 tf_error, DECL_TI_TEMPLATE (decl),
19376 /*integral_constant_expression_p=*/false);
19377 if (VAR_HAD_UNKNOWN_BOUND (decl))
19378 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19379 tf_error, DECL_TI_TEMPLATE (decl));
19381 else
19382 gcc_unreachable ();
19384 pop_access_scope (decl);
19387 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19388 substituted to get DECL. */
19390 tree
19391 template_for_substitution (tree decl)
19393 tree tmpl = DECL_TI_TEMPLATE (decl);
19395 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19396 for the instantiation. This is not always the most general
19397 template. Consider, for example:
19399 template <class T>
19400 struct S { template <class U> void f();
19401 template <> void f<int>(); };
19403 and an instantiation of S<double>::f<int>. We want TD to be the
19404 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19405 while (/* An instantiation cannot have a definition, so we need a
19406 more general template. */
19407 DECL_TEMPLATE_INSTANTIATION (tmpl)
19408 /* We must also deal with friend templates. Given:
19410 template <class T> struct S {
19411 template <class U> friend void f() {};
19414 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19415 so far as the language is concerned, but that's still
19416 where we get the pattern for the instantiation from. On
19417 other hand, if the definition comes outside the class, say:
19419 template <class T> struct S {
19420 template <class U> friend void f();
19422 template <class U> friend void f() {}
19424 we don't need to look any further. That's what the check for
19425 DECL_INITIAL is for. */
19426 || (TREE_CODE (decl) == FUNCTION_DECL
19427 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19428 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19430 /* The present template, TD, should not be a definition. If it
19431 were a definition, we should be using it! Note that we
19432 cannot restructure the loop to just keep going until we find
19433 a template with a definition, since that might go too far if
19434 a specialization was declared, but not defined. */
19435 gcc_assert (!VAR_P (decl)
19436 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19438 /* Fetch the more general template. */
19439 tmpl = DECL_TI_TEMPLATE (tmpl);
19442 return tmpl;
19445 /* Returns true if we need to instantiate this template instance even if we
19446 know we aren't going to emit it.. */
19448 bool
19449 always_instantiate_p (tree decl)
19451 /* We always instantiate inline functions so that we can inline them. An
19452 explicit instantiation declaration prohibits implicit instantiation of
19453 non-inline functions. With high levels of optimization, we would
19454 normally inline non-inline functions -- but we're not allowed to do
19455 that for "extern template" functions. Therefore, we check
19456 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19457 return ((TREE_CODE (decl) == FUNCTION_DECL
19458 && (DECL_DECLARED_INLINE_P (decl)
19459 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19460 /* And we need to instantiate static data members so that
19461 their initializers are available in integral constant
19462 expressions. */
19463 || (VAR_P (decl)
19464 && decl_maybe_constant_var_p (decl)));
19467 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19468 instantiate it now, modifying TREE_TYPE (fn). */
19470 void
19471 maybe_instantiate_noexcept (tree fn)
19473 tree fntype, spec, noex, clone;
19475 /* Don't instantiate a noexcept-specification from template context. */
19476 if (processing_template_decl)
19477 return;
19479 if (DECL_CLONED_FUNCTION_P (fn))
19480 fn = DECL_CLONED_FUNCTION (fn);
19481 fntype = TREE_TYPE (fn);
19482 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19484 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19485 return;
19487 noex = TREE_PURPOSE (spec);
19489 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19491 if (push_tinst_level (fn))
19493 push_access_scope (fn);
19494 push_deferring_access_checks (dk_no_deferred);
19495 input_location = DECL_SOURCE_LOCATION (fn);
19496 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19497 DEFERRED_NOEXCEPT_ARGS (noex),
19498 tf_warning_or_error, fn,
19499 /*function_p=*/false,
19500 /*integral_constant_expression_p=*/true);
19501 pop_deferring_access_checks ();
19502 pop_access_scope (fn);
19503 pop_tinst_level ();
19504 spec = build_noexcept_spec (noex, tf_warning_or_error);
19505 if (spec == error_mark_node)
19506 spec = noexcept_false_spec;
19508 else
19509 spec = noexcept_false_spec;
19511 else
19513 /* This is an implicitly declared function, so NOEX is a list of
19514 other functions to evaluate and merge. */
19515 tree elt;
19516 spec = noexcept_true_spec;
19517 for (elt = noex; elt; elt = OVL_NEXT (elt))
19519 tree fn = OVL_CURRENT (elt);
19520 tree subspec;
19521 maybe_instantiate_noexcept (fn);
19522 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19523 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19527 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19529 FOR_EACH_CLONE (clone, fn)
19531 if (TREE_TYPE (clone) == fntype)
19532 TREE_TYPE (clone) = TREE_TYPE (fn);
19533 else
19534 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19538 /* Produce the definition of D, a _DECL generated from a template. If
19539 DEFER_OK is nonzero, then we don't have to actually do the
19540 instantiation now; we just have to do it sometime. Normally it is
19541 an error if this is an explicit instantiation but D is undefined.
19542 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19543 explicitly instantiated class template. */
19545 tree
19546 instantiate_decl (tree d, int defer_ok,
19547 bool expl_inst_class_mem_p)
19549 tree tmpl = DECL_TI_TEMPLATE (d);
19550 tree gen_args;
19551 tree args;
19552 tree td;
19553 tree code_pattern;
19554 tree spec;
19555 tree gen_tmpl;
19556 bool pattern_defined;
19557 location_t saved_loc = input_location;
19558 int saved_unevaluated_operand = cp_unevaluated_operand;
19559 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19560 bool external_p;
19561 tree fn_context;
19562 bool nested;
19564 /* This function should only be used to instantiate templates for
19565 functions and static member variables. */
19566 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19568 /* Variables are never deferred; if instantiation is required, they
19569 are instantiated right away. That allows for better code in the
19570 case that an expression refers to the value of the variable --
19571 if the variable has a constant value the referring expression can
19572 take advantage of that fact. */
19573 if (VAR_P (d)
19574 || DECL_DECLARED_CONSTEXPR_P (d))
19575 defer_ok = 0;
19577 /* Don't instantiate cloned functions. Instead, instantiate the
19578 functions they cloned. */
19579 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19580 d = DECL_CLONED_FUNCTION (d);
19582 if (DECL_TEMPLATE_INSTANTIATED (d)
19583 || (TREE_CODE (d) == FUNCTION_DECL
19584 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19585 || DECL_TEMPLATE_SPECIALIZATION (d))
19586 /* D has already been instantiated or explicitly specialized, so
19587 there's nothing for us to do here.
19589 It might seem reasonable to check whether or not D is an explicit
19590 instantiation, and, if so, stop here. But when an explicit
19591 instantiation is deferred until the end of the compilation,
19592 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19593 the instantiation. */
19594 return d;
19596 /* Check to see whether we know that this template will be
19597 instantiated in some other file, as with "extern template"
19598 extension. */
19599 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19601 /* In general, we do not instantiate such templates. */
19602 if (external_p && !always_instantiate_p (d))
19603 return d;
19605 gen_tmpl = most_general_template (tmpl);
19606 gen_args = DECL_TI_ARGS (d);
19608 if (tmpl != gen_tmpl)
19609 /* We should already have the extra args. */
19610 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19611 == TMPL_ARGS_DEPTH (gen_args));
19612 /* And what's in the hash table should match D. */
19613 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19614 || spec == NULL_TREE);
19616 /* This needs to happen before any tsubsting. */
19617 if (! push_tinst_level (d))
19618 return d;
19620 timevar_push (TV_TEMPLATE_INST);
19622 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19623 for the instantiation. */
19624 td = template_for_substitution (d);
19625 code_pattern = DECL_TEMPLATE_RESULT (td);
19627 /* We should never be trying to instantiate a member of a class
19628 template or partial specialization. */
19629 gcc_assert (d != code_pattern);
19631 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19632 || DECL_TEMPLATE_SPECIALIZATION (td))
19633 /* In the case of a friend template whose definition is provided
19634 outside the class, we may have too many arguments. Drop the
19635 ones we don't need. The same is true for specializations. */
19636 args = get_innermost_template_args
19637 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19638 else
19639 args = gen_args;
19641 if (TREE_CODE (d) == FUNCTION_DECL)
19642 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19643 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19644 else
19645 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19647 /* We may be in the middle of deferred access check. Disable it now. */
19648 push_deferring_access_checks (dk_no_deferred);
19650 /* Unless an explicit instantiation directive has already determined
19651 the linkage of D, remember that a definition is available for
19652 this entity. */
19653 if (pattern_defined
19654 && !DECL_INTERFACE_KNOWN (d)
19655 && !DECL_NOT_REALLY_EXTERN (d))
19656 mark_definable (d);
19658 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19659 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19660 input_location = DECL_SOURCE_LOCATION (d);
19662 /* If D is a member of an explicitly instantiated class template,
19663 and no definition is available, treat it like an implicit
19664 instantiation. */
19665 if (!pattern_defined && expl_inst_class_mem_p
19666 && DECL_EXPLICIT_INSTANTIATION (d))
19668 /* Leave linkage flags alone on instantiations with anonymous
19669 visibility. */
19670 if (TREE_PUBLIC (d))
19672 DECL_NOT_REALLY_EXTERN (d) = 0;
19673 DECL_INTERFACE_KNOWN (d) = 0;
19675 SET_DECL_IMPLICIT_INSTANTIATION (d);
19678 if (TREE_CODE (d) == FUNCTION_DECL)
19679 maybe_instantiate_noexcept (d);
19681 /* Defer all other templates, unless we have been explicitly
19682 forbidden from doing so. */
19683 if (/* If there is no definition, we cannot instantiate the
19684 template. */
19685 ! pattern_defined
19686 /* If it's OK to postpone instantiation, do so. */
19687 || defer_ok
19688 /* If this is a static data member that will be defined
19689 elsewhere, we don't want to instantiate the entire data
19690 member, but we do want to instantiate the initializer so that
19691 we can substitute that elsewhere. */
19692 || (external_p && VAR_P (d)))
19694 /* The definition of the static data member is now required so
19695 we must substitute the initializer. */
19696 if (VAR_P (d)
19697 && !DECL_INITIAL (d)
19698 && DECL_INITIAL (code_pattern))
19700 tree ns;
19701 tree init;
19702 bool const_init = false;
19704 ns = decl_namespace_context (d);
19705 push_nested_namespace (ns);
19706 push_nested_class (DECL_CONTEXT (d));
19707 init = tsubst_expr (DECL_INITIAL (code_pattern),
19708 args,
19709 tf_warning_or_error, NULL_TREE,
19710 /*integral_constant_expression_p=*/false);
19711 /* Make sure the initializer is still constant, in case of
19712 circular dependency (template/instantiate6.C). */
19713 const_init
19714 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19715 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19716 /*asmspec_tree=*/NULL_TREE,
19717 LOOKUP_ONLYCONVERTING);
19718 pop_nested_class ();
19719 pop_nested_namespace (ns);
19722 /* We restore the source position here because it's used by
19723 add_pending_template. */
19724 input_location = saved_loc;
19726 if (at_eof && !pattern_defined
19727 && DECL_EXPLICIT_INSTANTIATION (d)
19728 && DECL_NOT_REALLY_EXTERN (d))
19729 /* [temp.explicit]
19731 The definition of a non-exported function template, a
19732 non-exported member function template, or a non-exported
19733 member function or static data member of a class template
19734 shall be present in every translation unit in which it is
19735 explicitly instantiated. */
19736 permerror (input_location, "explicit instantiation of %qD "
19737 "but no definition available", d);
19739 /* If we're in unevaluated context, we just wanted to get the
19740 constant value; this isn't an odr use, so don't queue
19741 a full instantiation. */
19742 if (cp_unevaluated_operand != 0)
19743 goto out;
19744 /* ??? Historically, we have instantiated inline functions, even
19745 when marked as "extern template". */
19746 if (!(external_p && VAR_P (d)))
19747 add_pending_template (d);
19749 if (L_IPO_COMP_MODE)
19750 /* Capture module info. */
19751 if (TREE_CODE (d) == VAR_DECL)
19752 varpool_node_for_decl (d);
19753 goto out;
19756 /* Tell the repository that D is available in this translation unit
19757 -- and see if it is supposed to be instantiated here. */
19758 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19760 /* In a PCH file, despite the fact that the repository hasn't
19761 requested instantiation in the PCH it is still possible that
19762 an instantiation will be required in a file that includes the
19763 PCH. */
19764 if (pch_file)
19765 add_pending_template (d);
19766 /* Instantiate inline functions so that the inliner can do its
19767 job, even though we'll not be emitting a copy of this
19768 function. */
19769 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19770 goto out;
19773 fn_context = decl_function_context (d);
19774 nested = (current_function_decl != NULL_TREE);
19775 if (!fn_context)
19776 push_to_top_level ();
19777 else
19779 if (nested)
19780 push_function_context ();
19781 cp_unevaluated_operand = 0;
19782 c_inhibit_evaluation_warnings = 0;
19785 /* Mark D as instantiated so that recursive calls to
19786 instantiate_decl do not try to instantiate it again. */
19787 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19789 /* Regenerate the declaration in case the template has been modified
19790 by a subsequent redeclaration. */
19791 regenerate_decl_from_template (d, td);
19793 /* We already set the file and line above. Reset them now in case
19794 they changed as a result of calling regenerate_decl_from_template. */
19795 input_location = DECL_SOURCE_LOCATION (d);
19797 if (VAR_P (d))
19799 tree init;
19800 bool const_init = false;
19802 /* Clear out DECL_RTL; whatever was there before may not be right
19803 since we've reset the type of the declaration. */
19804 SET_DECL_RTL (d, NULL);
19805 DECL_IN_AGGR_P (d) = 0;
19807 /* The initializer is placed in DECL_INITIAL by
19808 regenerate_decl_from_template so we don't need to
19809 push/pop_access_scope again here. Pull it out so that
19810 cp_finish_decl can process it. */
19811 init = DECL_INITIAL (d);
19812 DECL_INITIAL (d) = NULL_TREE;
19813 DECL_INITIALIZED_P (d) = 0;
19815 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19816 initializer. That function will defer actual emission until
19817 we have a chance to determine linkage. */
19818 DECL_EXTERNAL (d) = 0;
19820 /* Enter the scope of D so that access-checking works correctly. */
19821 push_nested_class (DECL_CONTEXT (d));
19822 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19823 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19824 pop_nested_class ();
19826 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19827 synthesize_method (d);
19828 else if (TREE_CODE (d) == FUNCTION_DECL)
19830 struct pointer_map_t *saved_local_specializations;
19831 tree subst_decl;
19832 tree tmpl_parm;
19833 tree spec_parm;
19834 tree block = NULL_TREE;
19836 /* Save away the current list, in case we are instantiating one
19837 template from within the body of another. */
19838 saved_local_specializations = local_specializations;
19840 /* Set up the list of local specializations. */
19841 local_specializations = pointer_map_create ();
19843 /* Set up context. */
19844 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19845 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19846 block = push_stmt_list ();
19847 else
19848 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
19850 /* Some typedefs referenced from within the template code need to be
19851 access checked at template instantiation time, i.e now. These
19852 types were added to the template at parsing time. Let's get those
19853 and perform the access checks then. */
19854 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
19855 gen_args);
19857 /* Create substitution entries for the parameters. */
19858 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
19859 tmpl_parm = DECL_ARGUMENTS (subst_decl);
19860 spec_parm = DECL_ARGUMENTS (d);
19861 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
19863 register_local_specialization (spec_parm, tmpl_parm);
19864 spec_parm = skip_artificial_parms_for (d, spec_parm);
19865 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
19867 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
19869 if (!DECL_PACK_P (tmpl_parm))
19871 register_local_specialization (spec_parm, tmpl_parm);
19872 spec_parm = DECL_CHAIN (spec_parm);
19874 else
19876 /* Register the (value) argument pack as a specialization of
19877 TMPL_PARM, then move on. */
19878 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
19879 register_local_specialization (argpack, tmpl_parm);
19882 gcc_assert (!spec_parm);
19884 /* Substitute into the body of the function. */
19885 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19886 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
19887 tf_warning_or_error, tmpl);
19888 else
19890 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
19891 tf_warning_or_error, tmpl,
19892 /*integral_constant_expression_p=*/false);
19894 /* Set the current input_location to the end of the function
19895 so that finish_function knows where we are. */
19896 input_location
19897 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
19899 /* Remember if we saw an infinite loop in the template. */
19900 current_function_infinite_loop
19901 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
19904 /* We don't need the local specializations any more. */
19905 pointer_map_destroy (local_specializations);
19906 local_specializations = saved_local_specializations;
19908 /* Finish the function. */
19909 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19910 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19911 DECL_SAVED_TREE (d) = pop_stmt_list (block);
19912 else
19914 d = finish_function (0);
19915 expand_or_defer_fn (d);
19918 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19919 cp_check_omp_declare_reduction (d);
19922 /* We're not deferring instantiation any more. */
19923 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
19925 if (!fn_context)
19926 pop_from_top_level ();
19927 else if (nested)
19928 pop_function_context ();
19930 out:
19931 input_location = saved_loc;
19932 cp_unevaluated_operand = saved_unevaluated_operand;
19933 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19934 pop_deferring_access_checks ();
19935 pop_tinst_level ();
19937 timevar_pop (TV_TEMPLATE_INST);
19939 return d;
19942 /* Run through the list of templates that we wish we could
19943 instantiate, and instantiate any we can. RETRIES is the
19944 number of times we retry pending template instantiation. */
19946 void
19947 instantiate_pending_templates (int retries)
19949 int reconsider;
19950 location_t saved_loc = input_location;
19952 /* Instantiating templates may trigger vtable generation. This in turn
19953 may require further template instantiations. We place a limit here
19954 to avoid infinite loop. */
19955 if (pending_templates && retries >= max_tinst_depth)
19957 tree decl = pending_templates->tinst->decl;
19959 error ("template instantiation depth exceeds maximum of %d"
19960 " instantiating %q+D, possibly from virtual table generation"
19961 " (use -ftemplate-depth= to increase the maximum)",
19962 max_tinst_depth, decl);
19963 if (TREE_CODE (decl) == FUNCTION_DECL)
19964 /* Pretend that we defined it. */
19965 DECL_INITIAL (decl) = error_mark_node;
19966 return;
19971 struct pending_template **t = &pending_templates;
19972 struct pending_template *last = NULL;
19973 reconsider = 0;
19974 while (*t)
19976 tree instantiation = reopen_tinst_level ((*t)->tinst);
19977 bool complete = false;
19979 if (TYPE_P (instantiation))
19981 tree fn;
19983 if (!COMPLETE_TYPE_P (instantiation))
19985 instantiate_class_template (instantiation);
19986 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
19987 for (fn = TYPE_METHODS (instantiation);
19989 fn = TREE_CHAIN (fn))
19990 if (! DECL_ARTIFICIAL (fn))
19991 instantiate_decl (fn,
19992 /*defer_ok=*/0,
19993 /*expl_inst_class_mem_p=*/false);
19994 if (COMPLETE_TYPE_P (instantiation))
19995 reconsider = 1;
19998 complete = COMPLETE_TYPE_P (instantiation);
20000 else
20002 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
20003 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
20005 instantiation
20006 = instantiate_decl (instantiation,
20007 /*defer_ok=*/0,
20008 /*expl_inst_class_mem_p=*/false);
20009 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
20010 reconsider = 1;
20013 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
20014 || DECL_TEMPLATE_INSTANTIATED (instantiation));
20017 if (complete)
20018 /* If INSTANTIATION has been instantiated, then we don't
20019 need to consider it again in the future. */
20020 *t = (*t)->next;
20021 else
20023 last = *t;
20024 t = &(*t)->next;
20026 tinst_depth = 0;
20027 current_tinst_level = NULL;
20029 last_pending_template = last;
20031 while (reconsider);
20033 input_location = saved_loc;
20036 /* Substitute ARGVEC into T, which is a list of initializers for
20037 either base class or a non-static data member. The TREE_PURPOSEs
20038 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20039 instantiate_decl. */
20041 static tree
20042 tsubst_initializer_list (tree t, tree argvec)
20044 tree inits = NULL_TREE;
20046 for (; t; t = TREE_CHAIN (t))
20048 tree decl;
20049 tree init;
20050 tree expanded_bases = NULL_TREE;
20051 tree expanded_arguments = NULL_TREE;
20052 int i, len = 1;
20054 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
20056 tree expr;
20057 tree arg;
20059 /* Expand the base class expansion type into separate base
20060 classes. */
20061 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
20062 tf_warning_or_error,
20063 NULL_TREE);
20064 if (expanded_bases == error_mark_node)
20065 continue;
20067 /* We'll be building separate TREE_LISTs of arguments for
20068 each base. */
20069 len = TREE_VEC_LENGTH (expanded_bases);
20070 expanded_arguments = make_tree_vec (len);
20071 for (i = 0; i < len; i++)
20072 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
20074 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20075 expand each argument in the TREE_VALUE of t. */
20076 expr = make_node (EXPR_PACK_EXPANSION);
20077 PACK_EXPANSION_LOCAL_P (expr) = true;
20078 PACK_EXPANSION_PARAMETER_PACKS (expr) =
20079 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
20081 if (TREE_VALUE (t) == void_type_node)
20082 /* VOID_TYPE_NODE is used to indicate
20083 value-initialization. */
20085 for (i = 0; i < len; i++)
20086 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
20088 else
20090 /* Substitute parameter packs into each argument in the
20091 TREE_LIST. */
20092 in_base_initializer = 1;
20093 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
20095 tree expanded_exprs;
20097 /* Expand the argument. */
20098 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
20099 expanded_exprs
20100 = tsubst_pack_expansion (expr, argvec,
20101 tf_warning_or_error,
20102 NULL_TREE);
20103 if (expanded_exprs == error_mark_node)
20104 continue;
20106 /* Prepend each of the expanded expressions to the
20107 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20108 for (i = 0; i < len; i++)
20110 TREE_VEC_ELT (expanded_arguments, i) =
20111 tree_cons (NULL_TREE,
20112 TREE_VEC_ELT (expanded_exprs, i),
20113 TREE_VEC_ELT (expanded_arguments, i));
20116 in_base_initializer = 0;
20118 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20119 since we built them backwards. */
20120 for (i = 0; i < len; i++)
20122 TREE_VEC_ELT (expanded_arguments, i) =
20123 nreverse (TREE_VEC_ELT (expanded_arguments, i));
20128 for (i = 0; i < len; ++i)
20130 if (expanded_bases)
20132 decl = TREE_VEC_ELT (expanded_bases, i);
20133 decl = expand_member_init (decl);
20134 init = TREE_VEC_ELT (expanded_arguments, i);
20136 else
20138 tree tmp;
20139 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
20140 tf_warning_or_error, NULL_TREE);
20142 decl = expand_member_init (decl);
20143 if (decl && !DECL_P (decl))
20144 in_base_initializer = 1;
20146 init = TREE_VALUE (t);
20147 tmp = init;
20148 if (init != void_type_node)
20149 init = tsubst_expr (init, argvec,
20150 tf_warning_or_error, NULL_TREE,
20151 /*integral_constant_expression_p=*/false);
20152 if (init == NULL_TREE && tmp != NULL_TREE)
20153 /* If we had an initializer but it instantiated to nothing,
20154 value-initialize the object. This will only occur when
20155 the initializer was a pack expansion where the parameter
20156 packs used in that expansion were of length zero. */
20157 init = void_type_node;
20158 in_base_initializer = 0;
20161 if (decl)
20163 init = build_tree_list (decl, init);
20164 TREE_CHAIN (init) = inits;
20165 inits = init;
20169 return inits;
20172 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20174 static void
20175 set_current_access_from_decl (tree decl)
20177 if (TREE_PRIVATE (decl))
20178 current_access_specifier = access_private_node;
20179 else if (TREE_PROTECTED (decl))
20180 current_access_specifier = access_protected_node;
20181 else
20182 current_access_specifier = access_public_node;
20185 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20186 is the instantiation (which should have been created with
20187 start_enum) and ARGS are the template arguments to use. */
20189 static void
20190 tsubst_enum (tree tag, tree newtag, tree args)
20192 tree e;
20194 if (SCOPED_ENUM_P (newtag))
20195 begin_scope (sk_scoped_enum, newtag);
20197 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
20199 tree value;
20200 tree decl;
20202 decl = TREE_VALUE (e);
20203 /* Note that in a template enum, the TREE_VALUE is the
20204 CONST_DECL, not the corresponding INTEGER_CST. */
20205 value = tsubst_expr (DECL_INITIAL (decl),
20206 args, tf_warning_or_error, NULL_TREE,
20207 /*integral_constant_expression_p=*/true);
20209 /* Give this enumeration constant the correct access. */
20210 set_current_access_from_decl (decl);
20212 /* Actually build the enumerator itself. */
20213 build_enumerator
20214 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
20217 if (SCOPED_ENUM_P (newtag))
20218 finish_scope ();
20220 finish_enum_value_list (newtag);
20221 finish_enum (newtag);
20223 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
20224 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
20227 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20228 its type -- but without substituting the innermost set of template
20229 arguments. So, innermost set of template parameters will appear in
20230 the type. */
20232 tree
20233 get_mostly_instantiated_function_type (tree decl)
20235 tree fn_type;
20236 tree tmpl;
20237 tree targs;
20238 tree tparms;
20239 int parm_depth;
20241 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
20242 targs = DECL_TI_ARGS (decl);
20243 tparms = DECL_TEMPLATE_PARMS (tmpl);
20244 parm_depth = TMPL_PARMS_DEPTH (tparms);
20246 /* There should be as many levels of arguments as there are levels
20247 of parameters. */
20248 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
20250 fn_type = TREE_TYPE (tmpl);
20252 if (parm_depth == 1)
20253 /* No substitution is necessary. */
20255 else
20257 int i;
20258 tree partial_args;
20260 /* Replace the innermost level of the TARGS with NULL_TREEs to
20261 let tsubst know not to substitute for those parameters. */
20262 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
20263 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
20264 SET_TMPL_ARGS_LEVEL (partial_args, i,
20265 TMPL_ARGS_LEVEL (targs, i));
20266 SET_TMPL_ARGS_LEVEL (partial_args,
20267 TMPL_ARGS_DEPTH (targs),
20268 make_tree_vec (DECL_NTPARMS (tmpl)));
20270 /* Make sure that we can see identifiers, and compute access
20271 correctly. */
20272 push_access_scope (decl);
20274 ++processing_template_decl;
20275 /* Now, do the (partial) substitution to figure out the
20276 appropriate function type. */
20277 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20278 --processing_template_decl;
20280 /* Substitute into the template parameters to obtain the real
20281 innermost set of parameters. This step is important if the
20282 innermost set of template parameters contains value
20283 parameters whose types depend on outer template parameters. */
20284 TREE_VEC_LENGTH (partial_args)--;
20285 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20287 pop_access_scope (decl);
20290 return fn_type;
20293 /* Return truthvalue if we're processing a template different from
20294 the last one involved in diagnostics. */
20296 problematic_instantiation_changed (void)
20298 return current_tinst_level != last_error_tinst_level;
20301 /* Remember current template involved in diagnostics. */
20302 void
20303 record_last_problematic_instantiation (void)
20305 last_error_tinst_level = current_tinst_level;
20308 struct tinst_level *
20309 current_instantiation (void)
20311 return current_tinst_level;
20314 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20315 type. Return zero for ok, nonzero for disallowed. Issue error and
20316 warning messages under control of COMPLAIN. */
20318 static int
20319 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20321 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20322 return 0;
20323 else if (POINTER_TYPE_P (type))
20324 return 0;
20325 else if (TYPE_PTRMEM_P (type))
20326 return 0;
20327 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20328 return 0;
20329 else if (TREE_CODE (type) == TYPENAME_TYPE)
20330 return 0;
20331 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20332 return 0;
20333 else if (TREE_CODE (type) == NULLPTR_TYPE)
20334 return 0;
20336 if (complain & tf_error)
20338 if (type == error_mark_node)
20339 inform (input_location, "invalid template non-type parameter");
20340 else
20341 error ("%q#T is not a valid type for a template non-type parameter",
20342 type);
20344 return 1;
20347 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20348 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20350 static bool
20351 dependent_type_p_r (tree type)
20353 tree scope;
20355 /* [temp.dep.type]
20357 A type is dependent if it is:
20359 -- a template parameter. Template template parameters are types
20360 for us (since TYPE_P holds true for them) so we handle
20361 them here. */
20362 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20363 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20364 return true;
20365 /* -- a qualified-id with a nested-name-specifier which contains a
20366 class-name that names a dependent type or whose unqualified-id
20367 names a dependent type. */
20368 if (TREE_CODE (type) == TYPENAME_TYPE)
20369 return true;
20370 /* -- a cv-qualified type where the cv-unqualified type is
20371 dependent. */
20372 type = TYPE_MAIN_VARIANT (type);
20373 /* -- a compound type constructed from any dependent type. */
20374 if (TYPE_PTRMEM_P (type))
20375 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20376 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20377 (type)));
20378 else if (TYPE_PTR_P (type)
20379 || TREE_CODE (type) == REFERENCE_TYPE)
20380 return dependent_type_p (TREE_TYPE (type));
20381 else if (TREE_CODE (type) == FUNCTION_TYPE
20382 || TREE_CODE (type) == METHOD_TYPE)
20384 tree arg_type;
20386 if (dependent_type_p (TREE_TYPE (type)))
20387 return true;
20388 for (arg_type = TYPE_ARG_TYPES (type);
20389 arg_type;
20390 arg_type = TREE_CHAIN (arg_type))
20391 if (dependent_type_p (TREE_VALUE (arg_type)))
20392 return true;
20393 return false;
20395 /* -- an array type constructed from any dependent type or whose
20396 size is specified by a constant expression that is
20397 value-dependent.
20399 We checked for type- and value-dependence of the bounds in
20400 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20401 if (TREE_CODE (type) == ARRAY_TYPE)
20403 if (TYPE_DOMAIN (type)
20404 && dependent_type_p (TYPE_DOMAIN (type)))
20405 return true;
20406 return dependent_type_p (TREE_TYPE (type));
20409 /* -- a template-id in which either the template name is a template
20410 parameter ... */
20411 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20412 return true;
20413 /* ... or any of the template arguments is a dependent type or
20414 an expression that is type-dependent or value-dependent. */
20415 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20416 && (any_dependent_template_arguments_p
20417 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20418 return true;
20420 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20421 dependent; if the argument of the `typeof' expression is not
20422 type-dependent, then it should already been have resolved. */
20423 if (TREE_CODE (type) == TYPEOF_TYPE
20424 || TREE_CODE (type) == DECLTYPE_TYPE
20425 || TREE_CODE (type) == UNDERLYING_TYPE)
20426 return true;
20428 /* A template argument pack is dependent if any of its packed
20429 arguments are. */
20430 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20432 tree args = ARGUMENT_PACK_ARGS (type);
20433 int i, len = TREE_VEC_LENGTH (args);
20434 for (i = 0; i < len; ++i)
20435 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20436 return true;
20439 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20440 be template parameters. */
20441 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20442 return true;
20444 /* The standard does not specifically mention types that are local
20445 to template functions or local classes, but they should be
20446 considered dependent too. For example:
20448 template <int I> void f() {
20449 enum E { a = I };
20450 S<sizeof (E)> s;
20453 The size of `E' cannot be known until the value of `I' has been
20454 determined. Therefore, `E' must be considered dependent. */
20455 scope = TYPE_CONTEXT (type);
20456 if (scope && TYPE_P (scope))
20457 return dependent_type_p (scope);
20458 /* Don't use type_dependent_expression_p here, as it can lead
20459 to infinite recursion trying to determine whether a lambda
20460 nested in a lambda is dependent (c++/47687). */
20461 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20462 && DECL_LANG_SPECIFIC (scope)
20463 && DECL_TEMPLATE_INFO (scope)
20464 && (any_dependent_template_arguments_p
20465 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20466 return true;
20468 /* Other types are non-dependent. */
20469 return false;
20472 /* Returns TRUE if TYPE is dependent, in the sense of
20473 [temp.dep.type]. Note that a NULL type is considered dependent. */
20475 bool
20476 dependent_type_p (tree type)
20478 /* If there are no template parameters in scope, then there can't be
20479 any dependent types. */
20480 if (!processing_template_decl)
20482 /* If we are not processing a template, then nobody should be
20483 providing us with a dependent type. */
20484 gcc_assert (type);
20485 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20486 return false;
20489 /* If the type is NULL, we have not computed a type for the entity
20490 in question; in that case, the type is dependent. */
20491 if (!type)
20492 return true;
20494 /* Erroneous types can be considered non-dependent. */
20495 if (type == error_mark_node)
20496 return false;
20498 /* If we have not already computed the appropriate value for TYPE,
20499 do so now. */
20500 if (!TYPE_DEPENDENT_P_VALID (type))
20502 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20503 TYPE_DEPENDENT_P_VALID (type) = 1;
20506 return TYPE_DEPENDENT_P (type);
20509 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20510 lookup. In other words, a dependent type that is not the current
20511 instantiation. */
20513 bool
20514 dependent_scope_p (tree scope)
20516 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20517 && !currently_open_class (scope));
20520 /* T is a SCOPE_REF; return whether we need to consider it
20521 instantiation-dependent so that we can check access at instantiation
20522 time even though we know which member it resolves to. */
20524 static bool
20525 instantiation_dependent_scope_ref_p (tree t)
20527 if (DECL_P (TREE_OPERAND (t, 1))
20528 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20529 && accessible_in_template_p (TREE_OPERAND (t, 0),
20530 TREE_OPERAND (t, 1)))
20531 return false;
20532 else
20533 return true;
20536 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20537 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20538 expression. */
20540 /* Note that this predicate is not appropriate for general expressions;
20541 only constant expressions (that satisfy potential_constant_expression)
20542 can be tested for value dependence. */
20544 bool
20545 value_dependent_expression_p (tree expression)
20547 if (!processing_template_decl)
20548 return false;
20550 /* A name declared with a dependent type. */
20551 if (DECL_P (expression) && type_dependent_expression_p (expression))
20552 return true;
20554 switch (TREE_CODE (expression))
20556 case IDENTIFIER_NODE:
20557 /* A name that has not been looked up -- must be dependent. */
20558 return true;
20560 case TEMPLATE_PARM_INDEX:
20561 /* A non-type template parm. */
20562 return true;
20564 case CONST_DECL:
20565 /* A non-type template parm. */
20566 if (DECL_TEMPLATE_PARM_P (expression))
20567 return true;
20568 return value_dependent_expression_p (DECL_INITIAL (expression));
20570 case VAR_DECL:
20571 /* A constant with literal type and is initialized
20572 with an expression that is value-dependent.
20574 Note that a non-dependent parenthesized initializer will have
20575 already been replaced with its constant value, so if we see
20576 a TREE_LIST it must be dependent. */
20577 if (DECL_INITIAL (expression)
20578 && decl_constant_var_p (expression)
20579 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20580 || value_dependent_expression_p (DECL_INITIAL (expression))))
20581 return true;
20582 return false;
20584 case DYNAMIC_CAST_EXPR:
20585 case STATIC_CAST_EXPR:
20586 case CONST_CAST_EXPR:
20587 case REINTERPRET_CAST_EXPR:
20588 case CAST_EXPR:
20589 /* These expressions are value-dependent if the type to which
20590 the cast occurs is dependent or the expression being casted
20591 is value-dependent. */
20593 tree type = TREE_TYPE (expression);
20595 if (dependent_type_p (type))
20596 return true;
20598 /* A functional cast has a list of operands. */
20599 expression = TREE_OPERAND (expression, 0);
20600 if (!expression)
20602 /* If there are no operands, it must be an expression such
20603 as "int()". This should not happen for aggregate types
20604 because it would form non-constant expressions. */
20605 gcc_assert (cxx_dialect >= cxx11
20606 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20608 return false;
20611 if (TREE_CODE (expression) == TREE_LIST)
20612 return any_value_dependent_elements_p (expression);
20614 return value_dependent_expression_p (expression);
20617 case SIZEOF_EXPR:
20618 if (SIZEOF_EXPR_TYPE_P (expression))
20619 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20620 /* FALLTHRU */
20621 case ALIGNOF_EXPR:
20622 case TYPEID_EXPR:
20623 /* A `sizeof' expression is value-dependent if the operand is
20624 type-dependent or is a pack expansion. */
20625 expression = TREE_OPERAND (expression, 0);
20626 if (PACK_EXPANSION_P (expression))
20627 return true;
20628 else if (TYPE_P (expression))
20629 return dependent_type_p (expression);
20630 return instantiation_dependent_expression_p (expression);
20632 case AT_ENCODE_EXPR:
20633 /* An 'encode' expression is value-dependent if the operand is
20634 type-dependent. */
20635 expression = TREE_OPERAND (expression, 0);
20636 return dependent_type_p (expression);
20638 case NOEXCEPT_EXPR:
20639 expression = TREE_OPERAND (expression, 0);
20640 return instantiation_dependent_expression_p (expression);
20642 case SCOPE_REF:
20643 /* All instantiation-dependent expressions should also be considered
20644 value-dependent. */
20645 return instantiation_dependent_scope_ref_p (expression);
20647 case COMPONENT_REF:
20648 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20649 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20651 case NONTYPE_ARGUMENT_PACK:
20652 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20653 is value-dependent. */
20655 tree values = ARGUMENT_PACK_ARGS (expression);
20656 int i, len = TREE_VEC_LENGTH (values);
20658 for (i = 0; i < len; ++i)
20659 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20660 return true;
20662 return false;
20665 case TRAIT_EXPR:
20667 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20668 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20669 || (type2 ? dependent_type_p (type2) : false));
20672 case MODOP_EXPR:
20673 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20674 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20676 case ARRAY_REF:
20677 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20678 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20680 case ADDR_EXPR:
20682 tree op = TREE_OPERAND (expression, 0);
20683 return (value_dependent_expression_p (op)
20684 || has_value_dependent_address (op));
20687 case CALL_EXPR:
20689 tree fn = get_callee_fndecl (expression);
20690 int i, nargs;
20691 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20692 return true;
20693 nargs = call_expr_nargs (expression);
20694 for (i = 0; i < nargs; ++i)
20696 tree op = CALL_EXPR_ARG (expression, i);
20697 /* In a call to a constexpr member function, look through the
20698 implicit ADDR_EXPR on the object argument so that it doesn't
20699 cause the call to be considered value-dependent. We also
20700 look through it in potential_constant_expression. */
20701 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20702 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20703 && TREE_CODE (op) == ADDR_EXPR)
20704 op = TREE_OPERAND (op, 0);
20705 if (value_dependent_expression_p (op))
20706 return true;
20708 return false;
20711 case TEMPLATE_ID_EXPR:
20712 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20713 type-dependent. */
20714 return type_dependent_expression_p (expression);
20716 case CONSTRUCTOR:
20718 unsigned ix;
20719 tree val;
20720 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20721 if (value_dependent_expression_p (val))
20722 return true;
20723 return false;
20726 case STMT_EXPR:
20727 /* Treat a GNU statement expression as dependent to avoid crashing
20728 under fold_non_dependent_expr; it can't be constant. */
20729 return true;
20731 default:
20732 /* A constant expression is value-dependent if any subexpression is
20733 value-dependent. */
20734 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20736 case tcc_reference:
20737 case tcc_unary:
20738 case tcc_comparison:
20739 case tcc_binary:
20740 case tcc_expression:
20741 case tcc_vl_exp:
20743 int i, len = cp_tree_operand_length (expression);
20745 for (i = 0; i < len; i++)
20747 tree t = TREE_OPERAND (expression, i);
20749 /* In some cases, some of the operands may be missing.l
20750 (For example, in the case of PREDECREMENT_EXPR, the
20751 amount to increment by may be missing.) That doesn't
20752 make the expression dependent. */
20753 if (t && value_dependent_expression_p (t))
20754 return true;
20757 break;
20758 default:
20759 break;
20761 break;
20764 /* The expression is not value-dependent. */
20765 return false;
20768 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20769 [temp.dep.expr]. Note that an expression with no type is
20770 considered dependent. Other parts of the compiler arrange for an
20771 expression with type-dependent subexpressions to have no type, so
20772 this function doesn't have to be fully recursive. */
20774 bool
20775 type_dependent_expression_p (tree expression)
20777 if (!processing_template_decl)
20778 return false;
20780 if (expression == NULL_TREE || expression == error_mark_node)
20781 return false;
20783 /* An unresolved name is always dependent. */
20784 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
20785 return true;
20787 /* Some expression forms are never type-dependent. */
20788 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20789 || TREE_CODE (expression) == SIZEOF_EXPR
20790 || TREE_CODE (expression) == ALIGNOF_EXPR
20791 || TREE_CODE (expression) == AT_ENCODE_EXPR
20792 || TREE_CODE (expression) == NOEXCEPT_EXPR
20793 || TREE_CODE (expression) == TRAIT_EXPR
20794 || TREE_CODE (expression) == TYPEID_EXPR
20795 || TREE_CODE (expression) == DELETE_EXPR
20796 || TREE_CODE (expression) == VEC_DELETE_EXPR
20797 || TREE_CODE (expression) == THROW_EXPR)
20798 return false;
20800 /* The types of these expressions depends only on the type to which
20801 the cast occurs. */
20802 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20803 || TREE_CODE (expression) == STATIC_CAST_EXPR
20804 || TREE_CODE (expression) == CONST_CAST_EXPR
20805 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20806 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20807 || TREE_CODE (expression) == CAST_EXPR)
20808 return dependent_type_p (TREE_TYPE (expression));
20810 /* The types of these expressions depends only on the type created
20811 by the expression. */
20812 if (TREE_CODE (expression) == NEW_EXPR
20813 || TREE_CODE (expression) == VEC_NEW_EXPR)
20815 /* For NEW_EXPR tree nodes created inside a template, either
20816 the object type itself or a TREE_LIST may appear as the
20817 operand 1. */
20818 tree type = TREE_OPERAND (expression, 1);
20819 if (TREE_CODE (type) == TREE_LIST)
20820 /* This is an array type. We need to check array dimensions
20821 as well. */
20822 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20823 || value_dependent_expression_p
20824 (TREE_OPERAND (TREE_VALUE (type), 1));
20825 else
20826 return dependent_type_p (type);
20829 if (TREE_CODE (expression) == SCOPE_REF)
20831 tree scope = TREE_OPERAND (expression, 0);
20832 tree name = TREE_OPERAND (expression, 1);
20834 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20835 contains an identifier associated by name lookup with one or more
20836 declarations declared with a dependent type, or...a
20837 nested-name-specifier or qualified-id that names a member of an
20838 unknown specialization. */
20839 return (type_dependent_expression_p (name)
20840 || dependent_scope_p (scope));
20843 if (TREE_CODE (expression) == FUNCTION_DECL
20844 && DECL_LANG_SPECIFIC (expression)
20845 && DECL_TEMPLATE_INFO (expression)
20846 && (any_dependent_template_arguments_p
20847 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
20848 return true;
20850 if (TREE_CODE (expression) == TEMPLATE_DECL
20851 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
20852 return false;
20854 if (TREE_CODE (expression) == STMT_EXPR)
20855 expression = stmt_expr_value_expr (expression);
20857 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
20859 tree elt;
20860 unsigned i;
20862 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
20864 if (type_dependent_expression_p (elt))
20865 return true;
20867 return false;
20870 /* A static data member of the current instantiation with incomplete
20871 array type is type-dependent, as the definition and specializations
20872 can have different bounds. */
20873 if (VAR_P (expression)
20874 && DECL_CLASS_SCOPE_P (expression)
20875 && dependent_type_p (DECL_CONTEXT (expression))
20876 && VAR_HAD_UNKNOWN_BOUND (expression))
20877 return true;
20879 /* An array of unknown bound depending on a variadic parameter, eg:
20881 template<typename... Args>
20882 void foo (Args... args)
20884 int arr[] = { args... };
20887 template<int... vals>
20888 void bar ()
20890 int arr[] = { vals... };
20893 If the array has no length and has an initializer, it must be that
20894 we couldn't determine its length in cp_complete_array_type because
20895 it is dependent. */
20896 if (VAR_P (expression)
20897 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
20898 && !TYPE_DOMAIN (TREE_TYPE (expression))
20899 && DECL_INITIAL (expression))
20900 return true;
20902 if (TREE_TYPE (expression) == unknown_type_node)
20904 if (TREE_CODE (expression) == ADDR_EXPR)
20905 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
20906 if (TREE_CODE (expression) == COMPONENT_REF
20907 || TREE_CODE (expression) == OFFSET_REF)
20909 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
20910 return true;
20911 expression = TREE_OPERAND (expression, 1);
20912 if (identifier_p (expression))
20913 return false;
20915 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20916 if (TREE_CODE (expression) == SCOPE_REF)
20917 return false;
20919 /* Always dependent, on the number of arguments if nothing else. */
20920 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
20921 return true;
20923 if (BASELINK_P (expression))
20924 expression = BASELINK_FUNCTIONS (expression);
20926 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
20928 if (any_dependent_template_arguments_p
20929 (TREE_OPERAND (expression, 1)))
20930 return true;
20931 expression = TREE_OPERAND (expression, 0);
20933 gcc_assert (TREE_CODE (expression) == OVERLOAD
20934 || TREE_CODE (expression) == FUNCTION_DECL);
20936 while (expression)
20938 if (type_dependent_expression_p (OVL_CURRENT (expression)))
20939 return true;
20940 expression = OVL_NEXT (expression);
20942 return false;
20945 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
20947 return (dependent_type_p (TREE_TYPE (expression)));
20950 /* walk_tree callback function for instantiation_dependent_expression_p,
20951 below. Returns non-zero if a dependent subexpression is found. */
20953 static tree
20954 instantiation_dependent_r (tree *tp, int *walk_subtrees,
20955 void * /*data*/)
20957 if (TYPE_P (*tp))
20959 /* We don't have to worry about decltype currently because decltype
20960 of an instantiation-dependent expr is a dependent type. This
20961 might change depending on the resolution of DR 1172. */
20962 *walk_subtrees = false;
20963 return NULL_TREE;
20965 enum tree_code code = TREE_CODE (*tp);
20966 switch (code)
20968 /* Don't treat an argument list as dependent just because it has no
20969 TREE_TYPE. */
20970 case TREE_LIST:
20971 case TREE_VEC:
20972 return NULL_TREE;
20974 case VAR_DECL:
20975 case CONST_DECL:
20976 /* A constant with a dependent initializer is dependent. */
20977 if (value_dependent_expression_p (*tp))
20978 return *tp;
20979 break;
20981 case TEMPLATE_PARM_INDEX:
20982 return *tp;
20984 /* Handle expressions with type operands. */
20985 case SIZEOF_EXPR:
20986 case ALIGNOF_EXPR:
20987 case TYPEID_EXPR:
20988 case AT_ENCODE_EXPR:
20990 tree op = TREE_OPERAND (*tp, 0);
20991 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
20992 op = TREE_TYPE (op);
20993 if (TYPE_P (op))
20995 if (dependent_type_p (op))
20996 return *tp;
20997 else
20999 *walk_subtrees = false;
21000 return NULL_TREE;
21003 break;
21006 case TRAIT_EXPR:
21007 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
21008 || (TRAIT_EXPR_TYPE2 (*tp)
21009 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
21010 return *tp;
21011 *walk_subtrees = false;
21012 return NULL_TREE;
21014 case COMPONENT_REF:
21015 if (identifier_p (TREE_OPERAND (*tp, 1)))
21016 /* In a template, finish_class_member_access_expr creates a
21017 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
21018 type-dependent, so that we can check access control at
21019 instantiation time (PR 42277). See also Core issue 1273. */
21020 return *tp;
21021 break;
21023 case SCOPE_REF:
21024 if (instantiation_dependent_scope_ref_p (*tp))
21025 return *tp;
21026 else
21027 break;
21029 /* Treat statement-expressions as dependent. */
21030 case BIND_EXPR:
21031 return *tp;
21033 default:
21034 break;
21037 if (type_dependent_expression_p (*tp))
21038 return *tp;
21039 else
21040 return NULL_TREE;
21043 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21044 sense defined by the ABI:
21046 "An expression is instantiation-dependent if it is type-dependent
21047 or value-dependent, or it has a subexpression that is type-dependent
21048 or value-dependent." */
21050 bool
21051 instantiation_dependent_expression_p (tree expression)
21053 tree result;
21055 if (!processing_template_decl)
21056 return false;
21058 if (expression == error_mark_node)
21059 return false;
21061 result = cp_walk_tree_without_duplicates (&expression,
21062 instantiation_dependent_r, NULL);
21063 return result != NULL_TREE;
21066 /* Like type_dependent_expression_p, but it also works while not processing
21067 a template definition, i.e. during substitution or mangling. */
21069 bool
21070 type_dependent_expression_p_push (tree expr)
21072 bool b;
21073 ++processing_template_decl;
21074 b = type_dependent_expression_p (expr);
21075 --processing_template_decl;
21076 return b;
21079 /* Returns TRUE if ARGS contains a type-dependent expression. */
21081 bool
21082 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
21084 unsigned int i;
21085 tree arg;
21087 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
21089 if (type_dependent_expression_p (arg))
21090 return true;
21092 return false;
21095 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21096 expressions) contains any type-dependent expressions. */
21098 bool
21099 any_type_dependent_elements_p (const_tree list)
21101 for (; list; list = TREE_CHAIN (list))
21102 if (type_dependent_expression_p (TREE_VALUE (list)))
21103 return true;
21105 return false;
21108 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21109 expressions) contains any value-dependent expressions. */
21111 bool
21112 any_value_dependent_elements_p (const_tree list)
21114 for (; list; list = TREE_CHAIN (list))
21115 if (value_dependent_expression_p (TREE_VALUE (list)))
21116 return true;
21118 return false;
21121 /* Returns TRUE if the ARG (a template argument) is dependent. */
21123 bool
21124 dependent_template_arg_p (tree arg)
21126 if (!processing_template_decl)
21127 return false;
21129 /* Assume a template argument that was wrongly written by the user
21130 is dependent. This is consistent with what
21131 any_dependent_template_arguments_p [that calls this function]
21132 does. */
21133 if (!arg || arg == error_mark_node)
21134 return true;
21136 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
21137 arg = ARGUMENT_PACK_SELECT_ARG (arg);
21139 if (TREE_CODE (arg) == TEMPLATE_DECL
21140 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
21141 return dependent_template_p (arg);
21142 else if (ARGUMENT_PACK_P (arg))
21144 tree args = ARGUMENT_PACK_ARGS (arg);
21145 int i, len = TREE_VEC_LENGTH (args);
21146 for (i = 0; i < len; ++i)
21148 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
21149 return true;
21152 return false;
21154 else if (TYPE_P (arg))
21155 return dependent_type_p (arg);
21156 else
21157 return (type_dependent_expression_p (arg)
21158 || value_dependent_expression_p (arg));
21161 /* Returns true if ARGS (a collection of template arguments) contains
21162 any types that require structural equality testing. */
21164 bool
21165 any_template_arguments_need_structural_equality_p (tree args)
21167 int i;
21168 int j;
21170 if (!args)
21171 return false;
21172 if (args == error_mark_node)
21173 return true;
21175 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21177 tree level = TMPL_ARGS_LEVEL (args, i + 1);
21178 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21180 tree arg = TREE_VEC_ELT (level, j);
21181 tree packed_args = NULL_TREE;
21182 int k, len = 1;
21184 if (ARGUMENT_PACK_P (arg))
21186 /* Look inside the argument pack. */
21187 packed_args = ARGUMENT_PACK_ARGS (arg);
21188 len = TREE_VEC_LENGTH (packed_args);
21191 for (k = 0; k < len; ++k)
21193 if (packed_args)
21194 arg = TREE_VEC_ELT (packed_args, k);
21196 if (error_operand_p (arg))
21197 return true;
21198 else if (TREE_CODE (arg) == TEMPLATE_DECL)
21199 continue;
21200 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
21201 return true;
21202 else if (!TYPE_P (arg) && TREE_TYPE (arg)
21203 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
21204 return true;
21209 return false;
21212 /* Returns true if ARGS (a collection of template arguments) contains
21213 any dependent arguments. */
21215 bool
21216 any_dependent_template_arguments_p (const_tree args)
21218 int i;
21219 int j;
21221 if (!args)
21222 return false;
21223 if (args == error_mark_node)
21224 return true;
21226 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21228 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
21229 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21230 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
21231 return true;
21234 return false;
21237 /* Returns TRUE if the template TMPL is dependent. */
21239 bool
21240 dependent_template_p (tree tmpl)
21242 if (TREE_CODE (tmpl) == OVERLOAD)
21244 while (tmpl)
21246 if (dependent_template_p (OVL_CURRENT (tmpl)))
21247 return true;
21248 tmpl = OVL_NEXT (tmpl);
21250 return false;
21253 /* Template template parameters are dependent. */
21254 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
21255 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21256 return true;
21257 /* So are names that have not been looked up. */
21258 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21259 return true;
21260 /* So are member templates of dependent classes. */
21261 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21262 return dependent_type_p (DECL_CONTEXT (tmpl));
21263 return false;
21266 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21268 bool
21269 dependent_template_id_p (tree tmpl, tree args)
21271 return (dependent_template_p (tmpl)
21272 || any_dependent_template_arguments_p (args));
21275 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21276 is dependent. */
21278 bool
21279 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21281 int i;
21283 if (!processing_template_decl)
21284 return false;
21286 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21288 tree decl = TREE_VEC_ELT (declv, i);
21289 tree init = TREE_VEC_ELT (initv, i);
21290 tree cond = TREE_VEC_ELT (condv, i);
21291 tree incr = TREE_VEC_ELT (incrv, i);
21293 if (type_dependent_expression_p (decl))
21294 return true;
21296 if (init && type_dependent_expression_p (init))
21297 return true;
21299 if (type_dependent_expression_p (cond))
21300 return true;
21302 if (COMPARISON_CLASS_P (cond)
21303 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21304 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21305 return true;
21307 if (TREE_CODE (incr) == MODOP_EXPR)
21309 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21310 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21311 return true;
21313 else if (type_dependent_expression_p (incr))
21314 return true;
21315 else if (TREE_CODE (incr) == MODIFY_EXPR)
21317 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21318 return true;
21319 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21321 tree t = TREE_OPERAND (incr, 1);
21322 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21323 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21324 return true;
21329 return false;
21332 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21333 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21334 no such TYPE can be found. Note that this function peers inside
21335 uninstantiated templates and therefore should be used only in
21336 extremely limited situations. ONLY_CURRENT_P restricts this
21337 peering to the currently open classes hierarchy (which is required
21338 when comparing types). */
21340 tree
21341 resolve_typename_type (tree type, bool only_current_p)
21343 tree scope;
21344 tree name;
21345 tree decl;
21346 int quals;
21347 tree pushed_scope;
21348 tree result;
21350 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21352 scope = TYPE_CONTEXT (type);
21353 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21354 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21355 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21356 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21357 identifier of the TYPENAME_TYPE anymore.
21358 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21359 TYPENAME_TYPE instead, we avoid messing up with a possible
21360 typedef variant case. */
21361 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21363 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21364 it first before we can figure out what NAME refers to. */
21365 if (TREE_CODE (scope) == TYPENAME_TYPE)
21367 if (TYPENAME_IS_RESOLVING_P (scope))
21368 /* Given a class template A with a dependent base with nested type C,
21369 typedef typename A::C::C C will land us here, as trying to resolve
21370 the initial A::C leads to the local C typedef, which leads back to
21371 A::C::C. So we break the recursion now. */
21372 return type;
21373 else
21374 scope = resolve_typename_type (scope, only_current_p);
21376 /* If we don't know what SCOPE refers to, then we cannot resolve the
21377 TYPENAME_TYPE. */
21378 if (TREE_CODE (scope) == TYPENAME_TYPE)
21379 return type;
21380 /* If the SCOPE is a template type parameter, we have no way of
21381 resolving the name. */
21382 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21383 return type;
21384 /* If the SCOPE is not the current instantiation, there's no reason
21385 to look inside it. */
21386 if (only_current_p && !currently_open_class (scope))
21387 return type;
21388 /* If this is a typedef, we don't want to look inside (c++/11987). */
21389 if (typedef_variant_p (type))
21390 return type;
21391 /* If SCOPE isn't the template itself, it will not have a valid
21392 TYPE_FIELDS list. */
21393 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21394 /* scope is either the template itself or a compatible instantiation
21395 like X<T>, so look up the name in the original template. */
21396 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21397 else
21398 /* scope is a partial instantiation, so we can't do the lookup or we
21399 will lose the template arguments. */
21400 return type;
21401 /* Enter the SCOPE so that name lookup will be resolved as if we
21402 were in the class definition. In particular, SCOPE will no
21403 longer be considered a dependent type. */
21404 pushed_scope = push_scope (scope);
21405 /* Look up the declaration. */
21406 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21407 tf_warning_or_error);
21409 result = NULL_TREE;
21411 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21412 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21413 if (!decl)
21414 /*nop*/;
21415 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21416 && TREE_CODE (decl) == TYPE_DECL)
21418 result = TREE_TYPE (decl);
21419 if (result == error_mark_node)
21420 result = NULL_TREE;
21422 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21423 && DECL_CLASS_TEMPLATE_P (decl))
21425 tree tmpl;
21426 tree args;
21427 /* Obtain the template and the arguments. */
21428 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21429 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21430 /* Instantiate the template. */
21431 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21432 /*entering_scope=*/0,
21433 tf_error | tf_user);
21434 if (result == error_mark_node)
21435 result = NULL_TREE;
21438 /* Leave the SCOPE. */
21439 if (pushed_scope)
21440 pop_scope (pushed_scope);
21442 /* If we failed to resolve it, return the original typename. */
21443 if (!result)
21444 return type;
21446 /* If lookup found a typename type, resolve that too. */
21447 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21449 /* Ill-formed programs can cause infinite recursion here, so we
21450 must catch that. */
21451 TYPENAME_IS_RESOLVING_P (type) = 1;
21452 result = resolve_typename_type (result, only_current_p);
21453 TYPENAME_IS_RESOLVING_P (type) = 0;
21456 /* Qualify the resulting type. */
21457 quals = cp_type_quals (type);
21458 if (quals)
21459 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21461 return result;
21464 /* EXPR is an expression which is not type-dependent. Return a proxy
21465 for EXPR that can be used to compute the types of larger
21466 expressions containing EXPR. */
21468 tree
21469 build_non_dependent_expr (tree expr)
21471 tree inner_expr;
21473 #ifdef ENABLE_CHECKING
21474 /* Try to get a constant value for all non-dependent expressions in
21475 order to expose bugs in *_dependent_expression_p and constexpr. */
21476 if (cxx_dialect >= cxx11)
21477 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21478 #endif
21480 /* Preserve OVERLOADs; the functions must be available to resolve
21481 types. */
21482 inner_expr = expr;
21483 if (TREE_CODE (inner_expr) == STMT_EXPR)
21484 inner_expr = stmt_expr_value_expr (inner_expr);
21485 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21486 inner_expr = TREE_OPERAND (inner_expr, 0);
21487 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21488 inner_expr = TREE_OPERAND (inner_expr, 1);
21489 if (is_overloaded_fn (inner_expr)
21490 || TREE_CODE (inner_expr) == OFFSET_REF)
21491 return expr;
21492 /* There is no need to return a proxy for a variable. */
21493 if (VAR_P (expr))
21494 return expr;
21495 /* Preserve string constants; conversions from string constants to
21496 "char *" are allowed, even though normally a "const char *"
21497 cannot be used to initialize a "char *". */
21498 if (TREE_CODE (expr) == STRING_CST)
21499 return expr;
21500 /* Preserve arithmetic constants, as an optimization -- there is no
21501 reason to create a new node. */
21502 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21503 return expr;
21504 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21505 There is at least one place where we want to know that a
21506 particular expression is a throw-expression: when checking a ?:
21507 expression, there are special rules if the second or third
21508 argument is a throw-expression. */
21509 if (TREE_CODE (expr) == THROW_EXPR)
21510 return expr;
21512 /* Don't wrap an initializer list, we need to be able to look inside. */
21513 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21514 return expr;
21516 /* Don't wrap a dummy object, we need to be able to test for it. */
21517 if (is_dummy_object (expr))
21518 return expr;
21520 if (TREE_CODE (expr) == COND_EXPR)
21521 return build3 (COND_EXPR,
21522 TREE_TYPE (expr),
21523 TREE_OPERAND (expr, 0),
21524 (TREE_OPERAND (expr, 1)
21525 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21526 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21527 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21528 if (TREE_CODE (expr) == COMPOUND_EXPR
21529 && !COMPOUND_EXPR_OVERLOADED (expr))
21530 return build2 (COMPOUND_EXPR,
21531 TREE_TYPE (expr),
21532 TREE_OPERAND (expr, 0),
21533 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21535 /* If the type is unknown, it can't really be non-dependent */
21536 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21538 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21539 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21542 /* ARGS is a vector of expressions as arguments to a function call.
21543 Replace the arguments with equivalent non-dependent expressions.
21544 This modifies ARGS in place. */
21546 void
21547 make_args_non_dependent (vec<tree, va_gc> *args)
21549 unsigned int ix;
21550 tree arg;
21552 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21554 tree newarg = build_non_dependent_expr (arg);
21555 if (newarg != arg)
21556 (*args)[ix] = newarg;
21560 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21561 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21562 parms. */
21564 static tree
21565 make_auto_1 (tree name)
21567 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21568 TYPE_NAME (au) = build_decl (input_location,
21569 TYPE_DECL, name, au);
21570 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21571 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21572 (0, processing_template_decl + 1, processing_template_decl + 1,
21573 TYPE_NAME (au), NULL_TREE);
21574 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21575 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21576 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21578 return au;
21581 tree
21582 make_decltype_auto (void)
21584 return make_auto_1 (get_identifier ("decltype(auto)"));
21587 tree
21588 make_auto (void)
21590 return make_auto_1 (get_identifier ("auto"));
21593 /* Given type ARG, return std::initializer_list<ARG>. */
21595 static tree
21596 listify (tree arg)
21598 tree std_init_list = namespace_binding
21599 (get_identifier ("initializer_list"), std_node);
21600 tree argvec;
21601 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21603 error ("deducing from brace-enclosed initializer list requires "
21604 "#include <initializer_list>");
21605 return error_mark_node;
21607 argvec = make_tree_vec (1);
21608 TREE_VEC_ELT (argvec, 0) = arg;
21609 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21610 NULL_TREE, 0, tf_warning_or_error);
21613 /* Replace auto in TYPE with std::initializer_list<auto>. */
21615 static tree
21616 listify_autos (tree type, tree auto_node)
21618 tree init_auto = listify (auto_node);
21619 tree argvec = make_tree_vec (1);
21620 TREE_VEC_ELT (argvec, 0) = init_auto;
21621 if (processing_template_decl)
21622 argvec = add_to_template_args (current_template_args (), argvec);
21623 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21626 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21627 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21629 tree
21630 do_auto_deduction (tree type, tree init, tree auto_node)
21632 tree targs;
21634 if (init == error_mark_node)
21635 return error_mark_node;
21637 if (type_dependent_expression_p (init))
21638 /* Defining a subset of type-dependent expressions that we can deduce
21639 from ahead of time isn't worth the trouble. */
21640 return type;
21642 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21643 with either a new invented type template parameter U or, if the
21644 initializer is a braced-init-list (8.5.4), with
21645 std::initializer_list<U>. */
21646 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21647 type = listify_autos (type, auto_node);
21649 init = resolve_nondeduced_context (init);
21651 targs = make_tree_vec (1);
21652 if (AUTO_IS_DECLTYPE (auto_node))
21654 bool id = (DECL_P (init) || (TREE_CODE (init) == COMPONENT_REF
21655 && !REF_PARENTHESIZED_P (init)));
21656 TREE_VEC_ELT (targs, 0)
21657 = finish_decltype_type (init, id, tf_warning_or_error);
21658 if (type != auto_node)
21660 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21661 return error_mark_node;
21664 else
21666 tree parms = build_tree_list (NULL_TREE, type);
21667 tree tparms = make_tree_vec (1);
21668 int val;
21670 TREE_VEC_ELT (tparms, 0)
21671 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21672 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21673 DEDUCE_CALL, LOOKUP_NORMAL,
21674 NULL, /*explain_p=*/false);
21675 if (val > 0)
21677 if (processing_template_decl)
21678 /* Try again at instantiation time. */
21679 return type;
21680 if (type && type != error_mark_node)
21681 /* If type is error_mark_node a diagnostic must have been
21682 emitted by now. Also, having a mention to '<type error>'
21683 in the diagnostic is not really useful to the user. */
21685 if (cfun && auto_node == current_function_auto_return_pattern
21686 && LAMBDA_FUNCTION_P (current_function_decl))
21687 error ("unable to deduce lambda return type from %qE", init);
21688 else
21689 error ("unable to deduce %qT from %qE", type, init);
21691 return error_mark_node;
21695 /* If the list of declarators contains more than one declarator, the type
21696 of each declared variable is determined as described above. If the
21697 type deduced for the template parameter U is not the same in each
21698 deduction, the program is ill-formed. */
21699 if (TREE_TYPE (auto_node)
21700 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21702 if (cfun && auto_node == current_function_auto_return_pattern
21703 && LAMBDA_FUNCTION_P (current_function_decl))
21704 error ("inconsistent types %qT and %qT deduced for "
21705 "lambda return type", TREE_TYPE (auto_node),
21706 TREE_VEC_ELT (targs, 0));
21707 else
21708 error ("inconsistent deduction for %qT: %qT and then %qT",
21709 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21710 return error_mark_node;
21712 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21714 if (processing_template_decl)
21715 targs = add_to_template_args (current_template_args (), targs);
21716 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21719 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21720 result. */
21722 tree
21723 splice_late_return_type (tree type, tree late_return_type)
21725 tree argvec;
21727 if (late_return_type == NULL_TREE)
21728 return type;
21729 argvec = make_tree_vec (1);
21730 TREE_VEC_ELT (argvec, 0) = late_return_type;
21731 if (processing_template_parmlist)
21732 /* For a late-specified return type in a template type-parameter, we
21733 need to add a dummy argument level for its parmlist. */
21734 argvec = add_to_template_args
21735 (make_tree_vec (processing_template_parmlist), argvec);
21736 if (current_template_parms)
21737 argvec = add_to_template_args (current_template_args (), argvec);
21738 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21741 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21742 'decltype(auto)'. */
21744 bool
21745 is_auto (const_tree type)
21747 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21748 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21749 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21750 return true;
21751 else
21752 return false;
21755 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21756 a use of `auto'. Returns NULL_TREE otherwise. */
21758 tree
21759 type_uses_auto (tree type)
21761 return find_type_usage (type, is_auto);
21764 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21765 'decltype(auto)' or a concept. */
21767 bool
21768 is_auto_or_concept (const_tree type)
21770 return is_auto (type); // or concept
21773 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21774 a concept identifier) iff TYPE contains a use of a generic type. Returns
21775 NULL_TREE otherwise. */
21777 tree
21778 type_uses_auto_or_concept (tree type)
21780 return find_type_usage (type, is_auto_or_concept);
21784 /* For a given template T, return the vector of typedefs referenced
21785 in T for which access check is needed at T instantiation time.
21786 T is either a FUNCTION_DECL or a RECORD_TYPE.
21787 Those typedefs were added to T by the function
21788 append_type_to_template_for_access_check. */
21790 vec<qualified_typedef_usage_t, va_gc> *
21791 get_types_needing_access_check (tree t)
21793 tree ti;
21794 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21796 if (!t || t == error_mark_node)
21797 return NULL;
21799 if (!(ti = get_template_info (t)))
21800 return NULL;
21802 if (CLASS_TYPE_P (t)
21803 || TREE_CODE (t) == FUNCTION_DECL)
21805 if (!TI_TEMPLATE (ti))
21806 return NULL;
21808 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21811 return result;
21814 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21815 tied to T. That list of typedefs will be access checked at
21816 T instantiation time.
21817 T is either a FUNCTION_DECL or a RECORD_TYPE.
21818 TYPE_DECL is a TYPE_DECL node representing a typedef.
21819 SCOPE is the scope through which TYPE_DECL is accessed.
21820 LOCATION is the location of the usage point of TYPE_DECL.
21822 This function is a subroutine of
21823 append_type_to_template_for_access_check. */
21825 static void
21826 append_type_to_template_for_access_check_1 (tree t,
21827 tree type_decl,
21828 tree scope,
21829 location_t location)
21831 qualified_typedef_usage_t typedef_usage;
21832 tree ti;
21834 if (!t || t == error_mark_node)
21835 return;
21837 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
21838 || CLASS_TYPE_P (t))
21839 && type_decl
21840 && TREE_CODE (type_decl) == TYPE_DECL
21841 && scope);
21843 if (!(ti = get_template_info (t)))
21844 return;
21846 gcc_assert (TI_TEMPLATE (ti));
21848 typedef_usage.typedef_decl = type_decl;
21849 typedef_usage.context = scope;
21850 typedef_usage.locus = location;
21852 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
21855 /* Append TYPE_DECL to the template TEMPL.
21856 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21857 At TEMPL instanciation time, TYPE_DECL will be checked to see
21858 if it can be accessed through SCOPE.
21859 LOCATION is the location of the usage point of TYPE_DECL.
21861 e.g. consider the following code snippet:
21863 class C
21865 typedef int myint;
21868 template<class U> struct S
21870 C::myint mi; // <-- usage point of the typedef C::myint
21873 S<char> s;
21875 At S<char> instantiation time, we need to check the access of C::myint
21876 In other words, we need to check the access of the myint typedef through
21877 the C scope. For that purpose, this function will add the myint typedef
21878 and the scope C through which its being accessed to a list of typedefs
21879 tied to the template S. That list will be walked at template instantiation
21880 time and access check performed on each typedefs it contains.
21881 Note that this particular code snippet should yield an error because
21882 myint is private to C. */
21884 void
21885 append_type_to_template_for_access_check (tree templ,
21886 tree type_decl,
21887 tree scope,
21888 location_t location)
21890 qualified_typedef_usage_t *iter;
21891 unsigned i;
21893 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
21895 /* Make sure we don't append the type to the template twice. */
21896 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
21897 if (iter->typedef_decl == type_decl && scope == iter->context)
21898 return;
21900 append_type_to_template_for_access_check_1 (templ, type_decl,
21901 scope, location);
21904 /* Convert the generic type parameters in PARM that match the types given in the
21905 range [START_IDX, END_IDX) from the current_template_parms into generic type
21906 packs. */
21908 tree
21909 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
21911 tree current = current_template_parms;
21912 int depth = TMPL_PARMS_DEPTH (current);
21913 current = INNERMOST_TEMPLATE_PARMS (current);
21914 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
21916 for (int i = 0; i < start_idx; ++i)
21917 TREE_VEC_ELT (replacement, i)
21918 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21920 for (int i = start_idx; i < end_idx; ++i)
21922 /* Create a distinct parameter pack type from the current parm and add it
21923 to the replacement args to tsubst below into the generic function
21924 parameter. */
21926 tree o = TREE_TYPE (TREE_VALUE
21927 (TREE_VEC_ELT (current, i)));
21928 tree t = copy_type (o);
21929 TEMPLATE_TYPE_PARM_INDEX (t)
21930 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
21931 o, 0, 0, tf_none);
21932 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
21933 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
21934 TYPE_MAIN_VARIANT (t) = t;
21935 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
21936 TYPE_CANONICAL (t) = canonical_type_parameter (t);
21937 TREE_VEC_ELT (replacement, i) = t;
21938 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
21941 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
21942 TREE_VEC_ELT (replacement, i)
21943 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21945 /* If there are more levels then build up the replacement with the outer
21946 template parms. */
21947 if (depth > 1)
21948 replacement = add_to_template_args (template_parms_to_args
21949 (TREE_CHAIN (current_template_parms)),
21950 replacement);
21952 return tsubst (parm, replacement, tf_none, NULL_TREE);
21956 /* Set up the hash tables for template instantiations. */
21958 void
21959 init_template_processing (void)
21961 decl_specializations = htab_create_ggc (37,
21962 hash_specialization,
21963 eq_specializations,
21964 ggc_free);
21965 type_specializations = htab_create_ggc (37,
21966 hash_specialization,
21967 eq_specializations,
21968 ggc_free);
21971 /* Print stats about the template hash tables for -fstats. */
21973 void
21974 print_template_statistics (void)
21976 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
21977 "%f collisions\n", (long) htab_size (decl_specializations),
21978 (long) htab_elements (decl_specializations),
21979 htab_collisions (decl_specializations));
21980 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
21981 "%f collisions\n", (long) htab_size (type_specializations),
21982 (long) htab_elements (type_specializations),
21983 htab_collisions (type_specializations));
21986 #include "gt-cp-pt.h"