2015-04-22 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / cp / pt.c
blob5547a6c049939e262ea519ddb543642b252e24de
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2015 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 "hash-set.h"
32 #include "machmode.h"
33 #include "vec.h"
34 #include "double-int.h"
35 #include "input.h"
36 #include "alias.h"
37 #include "symtab.h"
38 #include "wide-int.h"
39 #include "inchash.h"
40 #include "tree.h"
41 #include "print-tree.h"
42 #include "stringpool.h"
43 #include "varasm.h"
44 #include "attribs.h"
45 #include "stor-layout.h"
46 #include "intl.h"
47 #include "flags.h"
48 #include "cp-tree.h"
49 #include "c-family/c-common.h"
50 #include "c-family/c-objc.h"
51 #include "cp-objcp-common.h"
52 #include "tree-inline.h"
53 #include "decl.h"
54 #include "toplev.h"
55 #include "timevar.h"
56 #include "tree-iterator.h"
57 #include "type-utils.h"
58 #include "hash-map.h"
59 #include "gimplify.h"
61 /* The type of functions taking a tree, and some additional data, and
62 returning an int. */
63 typedef int (*tree_fn_t) (tree, void*);
65 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
66 instantiations have been deferred, either because their definitions
67 were not yet available, or because we were putting off doing the work. */
68 struct GTY ((chain_next ("%h.next"))) pending_template {
69 struct pending_template *next;
70 struct tinst_level *tinst;
73 static GTY(()) struct pending_template *pending_templates;
74 static GTY(()) struct pending_template *last_pending_template;
76 int processing_template_parmlist;
77 static int template_header_count;
79 static GTY(()) tree saved_trees;
80 static vec<int> inline_parm_levels;
82 static GTY(()) struct tinst_level *current_tinst_level;
84 static GTY(()) tree saved_access_scope;
86 /* Live only within one (recursive) call to tsubst_expr. We use
87 this to pass the statement expression node from the STMT_EXPR
88 to the EXPR_STMT that is its result. */
89 static tree cur_stmt_expr;
91 // -------------------------------------------------------------------------- //
92 // Local Specialization Stack
94 // Implementation of the RAII helper for creating new local
95 // specializations.
96 local_specialization_stack::local_specialization_stack ()
97 : saved (local_specializations)
99 local_specializations = new hash_map<tree, tree>;
102 local_specialization_stack::~local_specialization_stack ()
104 delete local_specializations;
105 local_specializations = saved;
108 /* True if we've recursed into fn_type_unification too many times. */
109 static bool excessive_deduction_depth;
111 struct GTY((for_user)) spec_entry
113 tree tmpl;
114 tree args;
115 tree spec;
118 struct spec_hasher : ggc_hasher<spec_entry *>
120 static hashval_t hash (spec_entry *);
121 static bool equal (spec_entry *, spec_entry *);
124 static GTY (()) hash_table<spec_hasher> *decl_specializations;
126 static GTY (()) hash_table<spec_hasher> *type_specializations;
128 /* Contains canonical template parameter types. The vector is indexed by
129 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
130 TREE_LIST, whose TREE_VALUEs contain the canonical template
131 parameters of various types and levels. */
132 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
134 #define UNIFY_ALLOW_NONE 0
135 #define UNIFY_ALLOW_MORE_CV_QUAL 1
136 #define UNIFY_ALLOW_LESS_CV_QUAL 2
137 #define UNIFY_ALLOW_DERIVED 4
138 #define UNIFY_ALLOW_INTEGER 8
139 #define UNIFY_ALLOW_OUTER_LEVEL 16
140 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
141 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
143 enum template_base_result {
144 tbr_incomplete_type,
145 tbr_ambiguous_baseclass,
146 tbr_success
149 static void push_access_scope (tree);
150 static void pop_access_scope (tree);
151 static bool resolve_overloaded_unification (tree, tree, tree, tree,
152 unification_kind_t, int,
153 bool);
154 static int try_one_overload (tree, tree, tree, tree, tree,
155 unification_kind_t, int, bool, bool);
156 static int unify (tree, tree, tree, tree, int, bool);
157 static void add_pending_template (tree);
158 static tree reopen_tinst_level (struct tinst_level *);
159 static tree tsubst_initializer_list (tree, tree);
160 static tree get_partial_spec_bindings (tree, tree, tree, tree);
161 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
162 bool, bool);
163 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
164 bool, bool);
165 static void tsubst_enum (tree, tree, tree);
166 static tree add_to_template_args (tree, tree);
167 static tree add_outermost_template_args (tree, tree);
168 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
169 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
170 tree);
171 static int type_unification_real (tree, tree, tree, const tree *,
172 unsigned int, int, unification_kind_t, int,
173 vec<deferred_access_check, va_gc> **,
174 bool);
175 static void note_template_header (int);
176 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
177 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
178 static tree convert_template_argument (tree, tree, tree,
179 tsubst_flags_t, int, tree);
180 static int for_each_template_parm (tree, tree_fn_t, void*,
181 hash_set<tree> *, bool);
182 static tree expand_template_argument_pack (tree);
183 static tree build_template_parm_index (int, int, int, tree, tree);
184 static bool inline_needs_template_parms (tree, bool);
185 static void push_inline_template_parms_recursive (tree, int);
186 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
187 static int mark_template_parm (tree, void *);
188 static int template_parm_this_level_p (tree, void *);
189 static tree tsubst_friend_function (tree, tree);
190 static tree tsubst_friend_class (tree, tree);
191 static int can_complete_type_without_circularity (tree);
192 static tree get_bindings (tree, tree, tree, bool);
193 static int template_decl_level (tree);
194 static int check_cv_quals_for_unify (int, tree, tree);
195 static void template_parm_level_and_index (tree, int*, int*);
196 static int unify_pack_expansion (tree, tree, tree,
197 tree, unification_kind_t, bool, bool);
198 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
199 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
200 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
201 static void regenerate_decl_from_template (tree, tree);
202 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
203 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
204 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
205 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
206 static bool check_specialization_scope (void);
207 static tree process_partial_specialization (tree);
208 static void set_current_access_from_decl (tree);
209 static enum template_base_result get_template_base (tree, tree, tree, tree,
210 bool , tree *);
211 static tree try_class_unification (tree, tree, tree, tree, bool);
212 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
213 tree, tree);
214 static bool template_template_parm_bindings_ok_p (tree, tree);
215 static int template_args_equal (tree, tree);
216 static void tsubst_default_arguments (tree, tsubst_flags_t);
217 static tree for_each_template_parm_r (tree *, int *, void *);
218 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
219 static void copy_default_args_to_explicit_spec (tree);
220 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
221 static bool dependent_template_arg_p (tree);
222 static bool any_template_arguments_need_structural_equality_p (tree);
223 static bool dependent_type_p_r (tree);
224 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
225 static tree tsubst_decl (tree, tree, tsubst_flags_t);
226 static void perform_typedefs_access_check (tree tmpl, tree targs);
227 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
228 location_t);
229 static tree listify (tree);
230 static tree listify_autos (tree, tree);
231 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
232 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
234 /* Make the current scope suitable for access checking when we are
235 processing T. T can be FUNCTION_DECL for instantiated function
236 template, VAR_DECL for static member variable, or TYPE_DECL for
237 alias template (needed by instantiate_decl). */
239 static void
240 push_access_scope (tree t)
242 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
243 || TREE_CODE (t) == TYPE_DECL);
245 if (DECL_FRIEND_CONTEXT (t))
246 push_nested_class (DECL_FRIEND_CONTEXT (t));
247 else if (DECL_CLASS_SCOPE_P (t))
248 push_nested_class (DECL_CONTEXT (t));
249 else
250 push_to_top_level ();
252 if (TREE_CODE (t) == FUNCTION_DECL)
254 saved_access_scope = tree_cons
255 (NULL_TREE, current_function_decl, saved_access_scope);
256 current_function_decl = t;
260 /* Restore the scope set up by push_access_scope. T is the node we
261 are processing. */
263 static void
264 pop_access_scope (tree t)
266 if (TREE_CODE (t) == FUNCTION_DECL)
268 current_function_decl = TREE_VALUE (saved_access_scope);
269 saved_access_scope = TREE_CHAIN (saved_access_scope);
272 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
273 pop_nested_class ();
274 else
275 pop_from_top_level ();
278 /* Do any processing required when DECL (a member template
279 declaration) is finished. Returns the TEMPLATE_DECL corresponding
280 to DECL, unless it is a specialization, in which case the DECL
281 itself is returned. */
283 tree
284 finish_member_template_decl (tree decl)
286 if (decl == error_mark_node)
287 return error_mark_node;
289 gcc_assert (DECL_P (decl));
291 if (TREE_CODE (decl) == TYPE_DECL)
293 tree type;
295 type = TREE_TYPE (decl);
296 if (type == error_mark_node)
297 return error_mark_node;
298 if (MAYBE_CLASS_TYPE_P (type)
299 && CLASSTYPE_TEMPLATE_INFO (type)
300 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
302 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
303 check_member_template (tmpl);
304 return tmpl;
306 return NULL_TREE;
308 else if (TREE_CODE (decl) == FIELD_DECL)
309 error ("data member %qD cannot be a member template", decl);
310 else if (DECL_TEMPLATE_INFO (decl))
312 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
314 check_member_template (DECL_TI_TEMPLATE (decl));
315 return DECL_TI_TEMPLATE (decl);
317 else
318 return decl;
320 else
321 error ("invalid member template declaration %qD", decl);
323 return error_mark_node;
326 /* Create a template info node. */
328 tree
329 build_template_info (tree template_decl, tree template_args)
331 tree result = make_node (TEMPLATE_INFO);
332 TI_TEMPLATE (result) = template_decl;
333 TI_ARGS (result) = template_args;
334 return result;
337 /* Return the template info node corresponding to T, whatever T is. */
339 tree
340 get_template_info (const_tree t)
342 tree tinfo = NULL_TREE;
344 if (!t || t == error_mark_node)
345 return NULL;
347 if (TREE_CODE (t) == NAMESPACE_DECL)
348 return NULL;
350 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
351 tinfo = DECL_TEMPLATE_INFO (t);
353 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
354 t = TREE_TYPE (t);
356 if (OVERLOAD_TYPE_P (t))
357 tinfo = TYPE_TEMPLATE_INFO (t);
358 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
359 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
361 return tinfo;
364 /* Returns the template nesting level of the indicated class TYPE.
366 For example, in:
367 template <class T>
368 struct A
370 template <class U>
371 struct B {};
374 A<T>::B<U> has depth two, while A<T> has depth one.
375 Both A<T>::B<int> and A<int>::B<U> have depth one, if
376 they are instantiations, not specializations.
378 This function is guaranteed to return 0 if passed NULL_TREE so
379 that, for example, `template_class_depth (current_class_type)' is
380 always safe. */
383 template_class_depth (tree type)
385 int depth;
387 for (depth = 0;
388 type && TREE_CODE (type) != NAMESPACE_DECL;
389 type = (TREE_CODE (type) == FUNCTION_DECL)
390 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
392 tree tinfo = get_template_info (type);
394 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
395 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
396 ++depth;
399 return depth;
402 /* Subroutine of maybe_begin_member_template_processing.
403 Returns true if processing DECL needs us to push template parms. */
405 static bool
406 inline_needs_template_parms (tree decl, bool nsdmi)
408 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
409 return false;
411 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
412 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
415 /* Subroutine of maybe_begin_member_template_processing.
416 Push the template parms in PARMS, starting from LEVELS steps into the
417 chain, and ending at the beginning, since template parms are listed
418 innermost first. */
420 static void
421 push_inline_template_parms_recursive (tree parmlist, int levels)
423 tree parms = TREE_VALUE (parmlist);
424 int i;
426 if (levels > 1)
427 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
429 ++processing_template_decl;
430 current_template_parms
431 = tree_cons (size_int (processing_template_decl),
432 parms, current_template_parms);
433 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
435 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
436 NULL);
437 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
439 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
441 if (error_operand_p (parm))
442 continue;
444 gcc_assert (DECL_P (parm));
446 switch (TREE_CODE (parm))
448 case TYPE_DECL:
449 case TEMPLATE_DECL:
450 pushdecl (parm);
451 break;
453 case PARM_DECL:
455 /* Make a CONST_DECL as is done in process_template_parm.
456 It is ugly that we recreate this here; the original
457 version built in process_template_parm is no longer
458 available. */
459 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
460 CONST_DECL, DECL_NAME (parm),
461 TREE_TYPE (parm));
462 DECL_ARTIFICIAL (decl) = 1;
463 TREE_CONSTANT (decl) = 1;
464 TREE_READONLY (decl) = 1;
465 DECL_INITIAL (decl) = DECL_INITIAL (parm);
466 SET_DECL_TEMPLATE_PARM_P (decl);
467 pushdecl (decl);
469 break;
471 default:
472 gcc_unreachable ();
477 /* Restore the template parameter context for a member template, a
478 friend template defined in a class definition, or a non-template
479 member of template class. */
481 void
482 maybe_begin_member_template_processing (tree decl)
484 tree parms;
485 int levels = 0;
486 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
488 if (nsdmi)
490 tree ctx = DECL_CONTEXT (decl);
491 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
492 /* Disregard full specializations (c++/60999). */
493 && uses_template_parms (ctx)
494 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
497 if (inline_needs_template_parms (decl, nsdmi))
499 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
500 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
502 if (DECL_TEMPLATE_SPECIALIZATION (decl))
504 --levels;
505 parms = TREE_CHAIN (parms);
508 push_inline_template_parms_recursive (parms, levels);
511 /* Remember how many levels of template parameters we pushed so that
512 we can pop them later. */
513 inline_parm_levels.safe_push (levels);
516 /* Undo the effects of maybe_begin_member_template_processing. */
518 void
519 maybe_end_member_template_processing (void)
521 int i;
522 int last;
524 if (inline_parm_levels.length () == 0)
525 return;
527 last = inline_parm_levels.pop ();
528 for (i = 0; i < last; ++i)
530 --processing_template_decl;
531 current_template_parms = TREE_CHAIN (current_template_parms);
532 poplevel (0, 0, 0);
536 /* Return a new template argument vector which contains all of ARGS,
537 but has as its innermost set of arguments the EXTRA_ARGS. */
539 static tree
540 add_to_template_args (tree args, tree extra_args)
542 tree new_args;
543 int extra_depth;
544 int i;
545 int j;
547 if (args == NULL_TREE || extra_args == error_mark_node)
548 return extra_args;
550 extra_depth = TMPL_ARGS_DEPTH (extra_args);
551 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
553 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
554 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
556 for (j = 1; j <= extra_depth; ++j, ++i)
557 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
559 return new_args;
562 /* Like add_to_template_args, but only the outermost ARGS are added to
563 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
564 (EXTRA_ARGS) levels are added. This function is used to combine
565 the template arguments from a partial instantiation with the
566 template arguments used to attain the full instantiation from the
567 partial instantiation. */
569 static tree
570 add_outermost_template_args (tree args, tree extra_args)
572 tree new_args;
574 /* If there are more levels of EXTRA_ARGS than there are ARGS,
575 something very fishy is going on. */
576 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
578 /* If *all* the new arguments will be the EXTRA_ARGS, just return
579 them. */
580 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
581 return extra_args;
583 /* For the moment, we make ARGS look like it contains fewer levels. */
584 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
586 new_args = add_to_template_args (args, extra_args);
588 /* Now, we restore ARGS to its full dimensions. */
589 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
591 return new_args;
594 /* Return the N levels of innermost template arguments from the ARGS. */
596 tree
597 get_innermost_template_args (tree args, int n)
599 tree new_args;
600 int extra_levels;
601 int i;
603 gcc_assert (n >= 0);
605 /* If N is 1, just return the innermost set of template arguments. */
606 if (n == 1)
607 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
609 /* If we're not removing anything, just return the arguments we were
610 given. */
611 extra_levels = TMPL_ARGS_DEPTH (args) - n;
612 gcc_assert (extra_levels >= 0);
613 if (extra_levels == 0)
614 return args;
616 /* Make a new set of arguments, not containing the outer arguments. */
617 new_args = make_tree_vec (n);
618 for (i = 1; i <= n; ++i)
619 SET_TMPL_ARGS_LEVEL (new_args, i,
620 TMPL_ARGS_LEVEL (args, i + extra_levels));
622 return new_args;
625 /* The inverse of get_innermost_template_args: Return all but the innermost
626 EXTRA_LEVELS levels of template arguments from the ARGS. */
628 static tree
629 strip_innermost_template_args (tree args, int extra_levels)
631 tree new_args;
632 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
633 int i;
635 gcc_assert (n >= 0);
637 /* If N is 1, just return the outermost set of template arguments. */
638 if (n == 1)
639 return TMPL_ARGS_LEVEL (args, 1);
641 /* If we're not removing anything, just return the arguments we were
642 given. */
643 gcc_assert (extra_levels >= 0);
644 if (extra_levels == 0)
645 return args;
647 /* Make a new set of arguments, not containing the inner arguments. */
648 new_args = make_tree_vec (n);
649 for (i = 1; i <= n; ++i)
650 SET_TMPL_ARGS_LEVEL (new_args, i,
651 TMPL_ARGS_LEVEL (args, i));
653 return new_args;
656 /* We've got a template header coming up; push to a new level for storing
657 the parms. */
659 void
660 begin_template_parm_list (void)
662 /* We use a non-tag-transparent scope here, which causes pushtag to
663 put tags in this scope, rather than in the enclosing class or
664 namespace scope. This is the right thing, since we want
665 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
666 global template class, push_template_decl handles putting the
667 TEMPLATE_DECL into top-level scope. For a nested template class,
668 e.g.:
670 template <class T> struct S1 {
671 template <class T> struct S2 {};
674 pushtag contains special code to call pushdecl_with_scope on the
675 TEMPLATE_DECL for S2. */
676 begin_scope (sk_template_parms, NULL);
677 ++processing_template_decl;
678 ++processing_template_parmlist;
679 note_template_header (0);
682 /* This routine is called when a specialization is declared. If it is
683 invalid to declare a specialization here, an error is reported and
684 false is returned, otherwise this routine will return true. */
686 static bool
687 check_specialization_scope (void)
689 tree scope = current_scope ();
691 /* [temp.expl.spec]
693 An explicit specialization shall be declared in the namespace of
694 which the template is a member, or, for member templates, in the
695 namespace of which the enclosing class or enclosing class
696 template is a member. An explicit specialization of a member
697 function, member class or static data member of a class template
698 shall be declared in the namespace of which the class template
699 is a member. */
700 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
702 error ("explicit specialization in non-namespace scope %qD", scope);
703 return false;
706 /* [temp.expl.spec]
708 In an explicit specialization declaration for a member of a class
709 template or a member template that appears in namespace scope,
710 the member template and some of its enclosing class templates may
711 remain unspecialized, except that the declaration shall not
712 explicitly specialize a class member template if its enclosing
713 class templates are not explicitly specialized as well. */
714 if (current_template_parms)
716 error ("enclosing class templates are not explicitly specialized");
717 return false;
720 return true;
723 /* We've just seen template <>. */
725 bool
726 begin_specialization (void)
728 begin_scope (sk_template_spec, NULL);
729 note_template_header (1);
730 return check_specialization_scope ();
733 /* Called at then end of processing a declaration preceded by
734 template<>. */
736 void
737 end_specialization (void)
739 finish_scope ();
740 reset_specialization ();
743 /* Any template <>'s that we have seen thus far are not referring to a
744 function specialization. */
746 void
747 reset_specialization (void)
749 processing_specialization = 0;
750 template_header_count = 0;
753 /* We've just seen a template header. If SPECIALIZATION is nonzero,
754 it was of the form template <>. */
756 static void
757 note_template_header (int specialization)
759 processing_specialization = specialization;
760 template_header_count++;
763 /* We're beginning an explicit instantiation. */
765 void
766 begin_explicit_instantiation (void)
768 gcc_assert (!processing_explicit_instantiation);
769 processing_explicit_instantiation = true;
773 void
774 end_explicit_instantiation (void)
776 gcc_assert (processing_explicit_instantiation);
777 processing_explicit_instantiation = false;
780 /* An explicit specialization or partial specialization of TMPL is being
781 declared. Check that the namespace in which the specialization is
782 occurring is permissible. Returns false iff it is invalid to
783 specialize TMPL in the current namespace. */
785 static bool
786 check_specialization_namespace (tree tmpl)
788 tree tpl_ns = decl_namespace_context (tmpl);
790 /* [tmpl.expl.spec]
792 An explicit specialization shall be declared in the namespace of
793 which the template is a member, or, for member templates, in the
794 namespace of which the enclosing class or enclosing class
795 template is a member. An explicit specialization of a member
796 function, member class or static data member of a class template
797 shall be declared in the namespace of which the class template is
798 a member. */
799 if (current_scope() != DECL_CONTEXT (tmpl)
800 && !at_namespace_scope_p ())
802 error ("specialization of %qD must appear at namespace scope", tmpl);
803 return false;
805 if (is_associated_namespace (current_namespace, tpl_ns))
806 /* Same or super-using namespace. */
807 return true;
808 else
810 permerror (input_location, "specialization of %qD in different namespace", tmpl);
811 permerror (input_location, " from definition of %q+#D", tmpl);
812 return false;
816 /* SPEC is an explicit instantiation. Check that it is valid to
817 perform this explicit instantiation in the current namespace. */
819 static void
820 check_explicit_instantiation_namespace (tree spec)
822 tree ns;
824 /* DR 275: An explicit instantiation shall appear in an enclosing
825 namespace of its template. */
826 ns = decl_namespace_context (spec);
827 if (!is_ancestor (current_namespace, ns))
828 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
829 "(which does not enclose namespace %qD)",
830 spec, current_namespace, ns);
833 // Returns the TEMPLATE_DECL that defines the partial template specialization
834 // TYPE. If TYPE is not found in the list of partial specializations, then
835 // it must be an explicit specialization.
836 static tree
837 get_specializing_template_decl (tree type)
839 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
840 tree decl = TYPE_MAIN_DECL (type);
841 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
842 while (specs)
844 tree spec = TREE_VALUE (specs);
845 if (DECL_TEMPLATE_RESULT (spec) == decl)
846 return spec;
847 specs = TREE_CHAIN (specs);
849 return NULL_TREE;
852 // Return the template constraints for the template specialization TYPE.
853 static inline tree
854 get_specialization_constraints (tree type)
856 // If TYPE is a partial specialization, return the constraints for
857 // that type. If it is an explicit specialization, return null since
858 // non-templates cannot be constrained.
859 if (tree d = get_specializing_template_decl (type))
860 return get_constraints (d);
861 else
862 return NULL_TREE;
865 // Returns the type of a template specialization only if that
866 // specialization needs to defined. Otherwise (e.g., if the type has
867 // already been defined), the function returns NULL_TREE.
868 static tree
869 maybe_new_partial_specialization (tree type)
871 // An implicit instantiation of an incomplete type implies
872 // the definition of a new class template.
874 // template<typename T>
875 // struct S;
877 // template<typename T>
878 // struct S<T*>;
880 // Here, S<T*> is an implicit instantiation of S whose type
881 // is incomplete.
882 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
883 return type;
885 // It can also be the case that TYPE is a completed specialization.
886 // Continuing the previous example, suppose we also declare:
888 // template<typename T>
889 // requires Integral<T>
890 // struct S<T*>;
892 // Here, S<T*> refers to the specialization S<T*> defined
893 // above. However, we need to differentiate definitions because
894 // we intend to define a new partial specialization. In this case,
895 // we rely on the fact that the constraints are different for
896 // this declaration than that above.
898 // Note that we also get here for injected class names and
899 // late-parsed template definitions. We must ensure that we
900 // do not create new type declarations for those cases.
902 // FIXME: This may allow multiple partial specializations to have
903 // equivalent constraints. We need to check this against all
904 // partial specializations with euqivalent type. For exmaple:
906 // template<typename T> struct S;
907 // template<typename T> struct S<T*>; // #1
908 // template<C T> struct S<T*>; // #2
909 // template<C T> struct S<T*>; // differs from #1 but not #2
910 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
912 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
913 tree args = CLASSTYPE_TI_ARGS (type);
915 // If TYPE is the same as the template's type, this is clearly
916 // not a specialization. This happens when you define a member
917 // template outside of its enclosing class.
918 if (type == DECL_TEMPLATE_RESULT (tmpl))
919 return NULL_TREE;
921 // If there are no template parameters, this cannot be a new
922 // partial template specializtion?
923 if (!current_template_parms)
924 return NULL_TREE;
926 // If the constraints are not the same as those of the primary
927 // then, we can probably create a new specialization.
928 tree tmpl_constr = TEMPLATE_PARM_CONSTRAINTS (current_template_parms);
929 tree type_constr = build_constraints (tmpl_constr, NULL_TREE);
930 tree main_constr = get_constraints (tmpl);
931 if (equivalent_constraints (type_constr, main_constr))
932 return NULL_TREE;
934 // Also, if there's a pre-existing specialization with matching
935 // constraints, then this also isn't new.
936 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
937 while (specs)
939 tree spec_tmpl = TREE_VALUE (specs);
940 tree spec_constr = get_constraints (spec_tmpl);
941 if (equivalent_constraints (type_constr, spec_constr))
942 return NULL_TREE;
943 specs = TREE_CHAIN (specs);
946 // Create a new type node (and corresponding type decl)
947 // for the newly declared specialization.
948 tree t = make_class_type (TREE_CODE (type));
949 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
950 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (type);
951 TYPE_CANONICAL (t) = t;
952 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
954 // Build the corresponding type decl.
955 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
956 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
957 DECL_SOURCE_LOCATION (d) = input_location;
959 return t;
962 return NULL_TREE;
965 /* The TYPE is being declared. If it is a template type, that means it
966 is a partial specialization. Do appropriate error-checking. */
968 tree
969 maybe_process_partial_specialization (tree type)
971 tree context;
972 if (type == error_mark_node)
973 return error_mark_node;
975 /* A lambda that appears in specialization context is not itself a
976 specialization. */
977 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
978 return type;
980 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
982 error ("name of class shadows template template parameter %qD",
983 TYPE_NAME (type));
984 return error_mark_node;
987 context = TYPE_CONTEXT (type);
989 if (TYPE_ALIAS_P (type))
991 if (TYPE_TEMPLATE_INFO (type)
992 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
993 error ("specialization of alias template %qD",
994 TYPE_TI_TEMPLATE (type));
995 else
996 error ("explicit specialization of non-template %qT", type);
997 return error_mark_node;
999 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1001 /* This is for ordinary explicit specialization and partial
1002 specialization of a template class such as:
1004 template <> class C<int>;
1008 template <class T> class C<T*>;
1010 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1012 if (tree t = maybe_new_partial_specialization (type))
1014 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1015 && !at_namespace_scope_p ())
1016 return error_mark_node;
1017 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1018 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1019 if (processing_template_decl)
1021 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1022 if (decl == error_mark_node)
1023 return error_mark_node;
1024 return TREE_TYPE (decl);
1027 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1028 error ("specialization of %qT after instantiation", type);
1029 else if (errorcount && !processing_specialization
1030 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1031 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1032 /* Trying to define a specialization either without a template<> header
1033 or in an inappropriate place. We've already given an error, so just
1034 bail now so we don't actually define the specialization. */
1035 return error_mark_node;
1037 else if (CLASS_TYPE_P (type)
1038 && !CLASSTYPE_USE_TEMPLATE (type)
1039 && CLASSTYPE_TEMPLATE_INFO (type)
1040 && context && CLASS_TYPE_P (context)
1041 && CLASSTYPE_TEMPLATE_INFO (context))
1043 /* This is for an explicit specialization of member class
1044 template according to [temp.expl.spec/18]:
1046 template <> template <class U> class C<int>::D;
1048 The context `C<int>' must be an implicit instantiation.
1049 Otherwise this is just a member class template declared
1050 earlier like:
1052 template <> class C<int> { template <class U> class D; };
1053 template <> template <class U> class C<int>::D;
1055 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1056 while in the second case, `C<int>::D' is a primary template
1057 and `C<T>::D' may not exist. */
1059 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1060 && !COMPLETE_TYPE_P (type))
1062 tree t;
1063 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1065 if (current_namespace
1066 != decl_namespace_context (tmpl))
1068 permerror (input_location, "specializing %q#T in different namespace", type);
1069 permerror (input_location, " from definition of %q+#D", tmpl);
1072 /* Check for invalid specialization after instantiation:
1074 template <> template <> class C<int>::D<int>;
1075 template <> template <class U> class C<int>::D; */
1077 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1078 t; t = TREE_CHAIN (t))
1080 tree inst = TREE_VALUE (t);
1081 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1082 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1084 /* We already have a full specialization of this partial
1085 instantiation, or a full specialization has been
1086 looked up but not instantiated. Reassign it to the
1087 new member specialization template. */
1088 spec_entry elt;
1089 spec_entry *entry;
1091 elt.tmpl = most_general_template (tmpl);
1092 elt.args = CLASSTYPE_TI_ARGS (inst);
1093 elt.spec = inst;
1095 type_specializations->remove_elt (&elt);
1097 elt.tmpl = tmpl;
1098 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1100 spec_entry **slot
1101 = type_specializations->find_slot (&elt, INSERT);
1102 entry = ggc_alloc<spec_entry> ();
1103 *entry = elt;
1104 *slot = entry;
1106 else
1107 /* But if we've had an implicit instantiation, that's a
1108 problem ([temp.expl.spec]/6). */
1109 error ("specialization %qT after instantiation %qT",
1110 type, inst);
1113 /* Mark TYPE as a specialization. And as a result, we only
1114 have one level of template argument for the innermost
1115 class template. */
1116 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1117 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1118 CLASSTYPE_TI_ARGS (type)
1119 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1122 else if (processing_specialization)
1124 /* Someday C++0x may allow for enum template specialization. */
1125 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1126 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1127 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1128 "of %qD not allowed by ISO C++", type);
1129 else
1131 error ("explicit specialization of non-template %qT", type);
1132 return error_mark_node;
1136 return type;
1139 /* Returns nonzero if we can optimize the retrieval of specializations
1140 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1141 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1143 static inline bool
1144 optimize_specialization_lookup_p (tree tmpl)
1146 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1147 && DECL_CLASS_SCOPE_P (tmpl)
1148 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1149 parameter. */
1150 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1151 /* The optimized lookup depends on the fact that the
1152 template arguments for the member function template apply
1153 purely to the containing class, which is not true if the
1154 containing class is an explicit or partial
1155 specialization. */
1156 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1157 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1158 && !DECL_CONV_FN_P (tmpl)
1159 /* It is possible to have a template that is not a member
1160 template and is not a member of a template class:
1162 template <typename T>
1163 struct S { friend A::f(); };
1165 Here, the friend function is a template, but the context does
1166 not have template information. The optimized lookup relies
1167 on having ARGS be the template arguments for both the class
1168 and the function template. */
1169 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1172 /* Retrieve the specialization (in the sense of [temp.spec] - a
1173 specialization is either an instantiation or an explicit
1174 specialization) of TMPL for the given template ARGS. If there is
1175 no such specialization, return NULL_TREE. The ARGS are a vector of
1176 arguments, or a vector of vectors of arguments, in the case of
1177 templates with more than one level of parameters.
1179 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1180 then we search for a partial specialization matching ARGS. This
1181 parameter is ignored if TMPL is not a class template.
1183 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1184 result is a NONTYPE_ARGUMENT_PACK. */
1186 static tree
1187 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1189 if (tmpl == NULL_TREE)
1190 return NULL_TREE;
1192 if (args == error_mark_node)
1193 return NULL_TREE;
1195 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1196 || TREE_CODE (tmpl) == FIELD_DECL);
1198 /* There should be as many levels of arguments as there are
1199 levels of parameters. */
1200 gcc_assert (TMPL_ARGS_DEPTH (args)
1201 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1202 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1203 : template_class_depth (DECL_CONTEXT (tmpl))));
1205 if (optimize_specialization_lookup_p (tmpl))
1207 tree class_template;
1208 tree class_specialization;
1209 vec<tree, va_gc> *methods;
1210 tree fns;
1211 int idx;
1213 /* The template arguments actually apply to the containing
1214 class. Find the class specialization with those
1215 arguments. */
1216 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1217 class_specialization
1218 = retrieve_specialization (class_template, args, 0);
1219 if (!class_specialization)
1220 return NULL_TREE;
1221 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1222 for the specialization. */
1223 idx = class_method_index_for_fn (class_specialization, tmpl);
1224 if (idx == -1)
1225 return NULL_TREE;
1226 /* Iterate through the methods with the indicated name, looking
1227 for the one that has an instance of TMPL. */
1228 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1229 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1231 tree fn = OVL_CURRENT (fns);
1232 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1233 /* using-declarations can add base methods to the method vec,
1234 and we don't want those here. */
1235 && DECL_CONTEXT (fn) == class_specialization)
1236 return fn;
1238 return NULL_TREE;
1240 else
1242 spec_entry *found;
1243 spec_entry elt;
1244 hash_table<spec_hasher> *specializations;
1246 elt.tmpl = tmpl;
1247 elt.args = args;
1248 elt.spec = NULL_TREE;
1250 if (DECL_CLASS_TEMPLATE_P (tmpl))
1251 specializations = type_specializations;
1252 else
1253 specializations = decl_specializations;
1255 if (hash == 0)
1256 hash = spec_hasher::hash (&elt);
1257 found = specializations->find_with_hash (&elt, hash);
1258 if (found)
1259 return found->spec;
1262 return NULL_TREE;
1265 /* Like retrieve_specialization, but for local declarations. */
1267 tree
1268 retrieve_local_specialization (tree tmpl)
1270 if (local_specializations == NULL)
1271 return NULL_TREE;
1273 tree *slot = local_specializations->get (tmpl);
1274 return slot ? *slot : NULL_TREE;
1277 /* Returns nonzero iff DECL is a specialization of TMPL. */
1280 is_specialization_of (tree decl, tree tmpl)
1282 tree t;
1284 if (TREE_CODE (decl) == FUNCTION_DECL)
1286 for (t = decl;
1287 t != NULL_TREE;
1288 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1289 if (t == tmpl)
1290 return 1;
1292 else
1294 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1296 for (t = TREE_TYPE (decl);
1297 t != NULL_TREE;
1298 t = CLASSTYPE_USE_TEMPLATE (t)
1299 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1300 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1301 return 1;
1304 return 0;
1307 /* Returns nonzero iff DECL is a specialization of friend declaration
1308 FRIEND_DECL according to [temp.friend]. */
1310 bool
1311 is_specialization_of_friend (tree decl, tree friend_decl)
1313 bool need_template = true;
1314 int template_depth;
1316 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1317 || TREE_CODE (decl) == TYPE_DECL);
1319 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1320 of a template class, we want to check if DECL is a specialization
1321 if this. */
1322 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1323 && DECL_TEMPLATE_INFO (friend_decl)
1324 && !DECL_USE_TEMPLATE (friend_decl))
1326 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1327 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1328 need_template = false;
1330 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1331 && !PRIMARY_TEMPLATE_P (friend_decl))
1332 need_template = false;
1334 /* There is nothing to do if this is not a template friend. */
1335 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1336 return false;
1338 if (is_specialization_of (decl, friend_decl))
1339 return true;
1341 /* [temp.friend/6]
1342 A member of a class template may be declared to be a friend of a
1343 non-template class. In this case, the corresponding member of
1344 every specialization of the class template is a friend of the
1345 class granting friendship.
1347 For example, given a template friend declaration
1349 template <class T> friend void A<T>::f();
1351 the member function below is considered a friend
1353 template <> struct A<int> {
1354 void f();
1357 For this type of template friend, TEMPLATE_DEPTH below will be
1358 nonzero. To determine if DECL is a friend of FRIEND, we first
1359 check if the enclosing class is a specialization of another. */
1361 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1362 if (template_depth
1363 && DECL_CLASS_SCOPE_P (decl)
1364 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1365 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1367 /* Next, we check the members themselves. In order to handle
1368 a few tricky cases, such as when FRIEND_DECL's are
1370 template <class T> friend void A<T>::g(T t);
1371 template <class T> template <T t> friend void A<T>::h();
1373 and DECL's are
1375 void A<int>::g(int);
1376 template <int> void A<int>::h();
1378 we need to figure out ARGS, the template arguments from
1379 the context of DECL. This is required for template substitution
1380 of `T' in the function parameter of `g' and template parameter
1381 of `h' in the above examples. Here ARGS corresponds to `int'. */
1383 tree context = DECL_CONTEXT (decl);
1384 tree args = NULL_TREE;
1385 int current_depth = 0;
1387 while (current_depth < template_depth)
1389 if (CLASSTYPE_TEMPLATE_INFO (context))
1391 if (current_depth == 0)
1392 args = TYPE_TI_ARGS (context);
1393 else
1394 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1395 current_depth++;
1397 context = TYPE_CONTEXT (context);
1400 if (TREE_CODE (decl) == FUNCTION_DECL)
1402 bool is_template;
1403 tree friend_type;
1404 tree decl_type;
1405 tree friend_args_type;
1406 tree decl_args_type;
1408 /* Make sure that both DECL and FRIEND_DECL are templates or
1409 non-templates. */
1410 is_template = DECL_TEMPLATE_INFO (decl)
1411 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1412 if (need_template ^ is_template)
1413 return false;
1414 else if (is_template)
1416 /* If both are templates, check template parameter list. */
1417 tree friend_parms
1418 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1419 args, tf_none);
1420 if (!comp_template_parms
1421 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1422 friend_parms))
1423 return false;
1425 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1427 else
1428 decl_type = TREE_TYPE (decl);
1430 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1431 tf_none, NULL_TREE);
1432 if (friend_type == error_mark_node)
1433 return false;
1435 /* Check if return types match. */
1436 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1437 return false;
1439 /* Check if function parameter types match, ignoring the
1440 `this' parameter. */
1441 friend_args_type = TYPE_ARG_TYPES (friend_type);
1442 decl_args_type = TYPE_ARG_TYPES (decl_type);
1443 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1444 friend_args_type = TREE_CHAIN (friend_args_type);
1445 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1446 decl_args_type = TREE_CHAIN (decl_args_type);
1448 return compparms (decl_args_type, friend_args_type);
1450 else
1452 /* DECL is a TYPE_DECL */
1453 bool is_template;
1454 tree decl_type = TREE_TYPE (decl);
1456 /* Make sure that both DECL and FRIEND_DECL are templates or
1457 non-templates. */
1458 is_template
1459 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1460 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1462 if (need_template ^ is_template)
1463 return false;
1464 else if (is_template)
1466 tree friend_parms;
1467 /* If both are templates, check the name of the two
1468 TEMPLATE_DECL's first because is_friend didn't. */
1469 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1470 != DECL_NAME (friend_decl))
1471 return false;
1473 /* Now check template parameter list. */
1474 friend_parms
1475 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1476 args, tf_none);
1477 return comp_template_parms
1478 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1479 friend_parms);
1481 else
1482 return (DECL_NAME (decl)
1483 == DECL_NAME (friend_decl));
1486 return false;
1489 /* Register the specialization SPEC as a specialization of TMPL with
1490 the indicated ARGS. IS_FRIEND indicates whether the specialization
1491 is actually just a friend declaration. Returns SPEC, or an
1492 equivalent prior declaration, if available.
1494 We also store instantiations of field packs in the hash table, even
1495 though they are not themselves templates, to make lookup easier. */
1497 static tree
1498 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1499 hashval_t hash)
1501 tree fn;
1502 spec_entry **slot = NULL;
1503 spec_entry elt;
1505 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1506 || (TREE_CODE (tmpl) == FIELD_DECL
1507 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1509 if (TREE_CODE (spec) == FUNCTION_DECL
1510 && uses_template_parms (DECL_TI_ARGS (spec)))
1511 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1512 register it; we want the corresponding TEMPLATE_DECL instead.
1513 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1514 the more obvious `uses_template_parms (spec)' to avoid problems
1515 with default function arguments. In particular, given
1516 something like this:
1518 template <class T> void f(T t1, T t = T())
1520 the default argument expression is not substituted for in an
1521 instantiation unless and until it is actually needed. */
1522 return spec;
1524 if (optimize_specialization_lookup_p (tmpl))
1525 /* We don't put these specializations in the hash table, but we might
1526 want to give an error about a mismatch. */
1527 fn = retrieve_specialization (tmpl, args, 0);
1528 else
1530 elt.tmpl = tmpl;
1531 elt.args = args;
1532 elt.spec = spec;
1534 if (hash == 0)
1535 hash = spec_hasher::hash (&elt);
1537 slot =
1538 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1539 if (*slot)
1540 fn = ((spec_entry *) *slot)->spec;
1541 else
1542 fn = NULL_TREE;
1545 /* We can sometimes try to re-register a specialization that we've
1546 already got. In particular, regenerate_decl_from_template calls
1547 duplicate_decls which will update the specialization list. But,
1548 we'll still get called again here anyhow. It's more convenient
1549 to simply allow this than to try to prevent it. */
1550 if (fn == spec)
1551 return spec;
1552 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1554 if (DECL_TEMPLATE_INSTANTIATION (fn))
1556 if (DECL_ODR_USED (fn)
1557 || DECL_EXPLICIT_INSTANTIATION (fn))
1559 error ("specialization of %qD after instantiation",
1560 fn);
1561 return error_mark_node;
1563 else
1565 tree clone;
1566 /* This situation should occur only if the first
1567 specialization is an implicit instantiation, the
1568 second is an explicit specialization, and the
1569 implicit instantiation has not yet been used. That
1570 situation can occur if we have implicitly
1571 instantiated a member function and then specialized
1572 it later.
1574 We can also wind up here if a friend declaration that
1575 looked like an instantiation turns out to be a
1576 specialization:
1578 template <class T> void foo(T);
1579 class S { friend void foo<>(int) };
1580 template <> void foo(int);
1582 We transform the existing DECL in place so that any
1583 pointers to it become pointers to the updated
1584 declaration.
1586 If there was a definition for the template, but not
1587 for the specialization, we want this to look as if
1588 there were no definition, and vice versa. */
1589 DECL_INITIAL (fn) = NULL_TREE;
1590 duplicate_decls (spec, fn, is_friend);
1592 /* The call to duplicate_decls will have applied
1593 [temp.expl.spec]:
1595 An explicit specialization of a function template
1596 is inline only if it is explicitly declared to be,
1597 and independently of whether its function template
1600 to the primary function; now copy the inline bits to
1601 the various clones. */
1602 FOR_EACH_CLONE (clone, fn)
1604 DECL_DECLARED_INLINE_P (clone)
1605 = DECL_DECLARED_INLINE_P (fn);
1606 DECL_SOURCE_LOCATION (clone)
1607 = DECL_SOURCE_LOCATION (fn);
1608 DECL_DELETED_FN (clone)
1609 = DECL_DELETED_FN (fn);
1611 check_specialization_namespace (tmpl);
1613 return fn;
1616 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1618 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1619 /* Dup decl failed, but this is a new definition. Set the
1620 line number so any errors match this new
1621 definition. */
1622 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1624 return fn;
1627 else if (fn)
1628 return duplicate_decls (spec, fn, is_friend);
1630 /* A specialization must be declared in the same namespace as the
1631 template it is specializing. */
1632 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1633 && !check_specialization_namespace (tmpl))
1634 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1636 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1638 spec_entry *entry = ggc_alloc<spec_entry> ();
1639 gcc_assert (tmpl && args && spec);
1640 *entry = elt;
1641 *slot = entry;
1642 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1643 && PRIMARY_TEMPLATE_P (tmpl)
1644 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1645 || variable_template_p (tmpl))
1646 /* If TMPL is a forward declaration of a template function, keep a list
1647 of all specializations in case we need to reassign them to a friend
1648 template later in tsubst_friend_function.
1650 Also keep a list of all variable template instantiations so that
1651 process_partial_specialization can check whether a later partial
1652 specialization would have used it. */
1653 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1654 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1657 return spec;
1661 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1662 TMPL and ARGS members, ignores SPEC. */
1664 int comparing_specializations;
1666 bool
1667 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1669 int equal;
1671 ++comparing_specializations;
1672 equal = (e1->tmpl == e2->tmpl
1673 && comp_template_args (e1->args, e2->args));
1674 --comparing_specializations;
1676 return equal;
1679 /* Returns a hash for a template TMPL and template arguments ARGS. */
1681 static hashval_t
1682 hash_tmpl_and_args (tree tmpl, tree args)
1684 hashval_t val = DECL_UID (tmpl);
1685 return iterative_hash_template_arg (args, val);
1688 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1689 ignoring SPEC. */
1691 hashval_t
1692 spec_hasher::hash (spec_entry *e)
1694 return hash_tmpl_and_args (e->tmpl, e->args);
1697 /* Recursively calculate a hash value for a template argument ARG, for use
1698 in the hash tables of template specializations. */
1700 hashval_t
1701 iterative_hash_template_arg (tree arg, hashval_t val)
1703 unsigned HOST_WIDE_INT i;
1704 enum tree_code code;
1705 char tclass;
1707 if (arg == NULL_TREE)
1708 return iterative_hash_object (arg, val);
1710 if (!TYPE_P (arg))
1711 STRIP_NOPS (arg);
1713 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1714 /* We can get one of these when re-hashing a previous entry in the middle
1715 of substituting into a pack expansion. Just look through it. */
1716 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1718 code = TREE_CODE (arg);
1719 tclass = TREE_CODE_CLASS (code);
1721 val = iterative_hash_object (code, val);
1723 switch (code)
1725 case ERROR_MARK:
1726 return val;
1728 case IDENTIFIER_NODE:
1729 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1731 case TREE_VEC:
1733 int i, len = TREE_VEC_LENGTH (arg);
1734 for (i = 0; i < len; ++i)
1735 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1736 return val;
1739 case TYPE_PACK_EXPANSION:
1740 case EXPR_PACK_EXPANSION:
1741 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1742 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1744 case TYPE_ARGUMENT_PACK:
1745 case NONTYPE_ARGUMENT_PACK:
1746 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1748 case TREE_LIST:
1749 for (; arg; arg = TREE_CHAIN (arg))
1750 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1751 return val;
1753 case OVERLOAD:
1754 for (; arg; arg = OVL_NEXT (arg))
1755 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1756 return val;
1758 case CONSTRUCTOR:
1760 tree field, value;
1761 iterative_hash_template_arg (TREE_TYPE (arg), val);
1762 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1764 val = iterative_hash_template_arg (field, val);
1765 val = iterative_hash_template_arg (value, val);
1767 return val;
1770 case PARM_DECL:
1771 if (!DECL_ARTIFICIAL (arg))
1773 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1774 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1776 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1778 case TARGET_EXPR:
1779 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1781 case PTRMEM_CST:
1782 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1783 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1785 case TEMPLATE_PARM_INDEX:
1786 val = iterative_hash_template_arg
1787 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1788 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1789 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1791 case TRAIT_EXPR:
1792 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1793 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1794 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1796 case BASELINK:
1797 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1798 val);
1799 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1800 val);
1802 case MODOP_EXPR:
1803 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1804 code = TREE_CODE (TREE_OPERAND (arg, 1));
1805 val = iterative_hash_object (code, val);
1806 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1808 case LAMBDA_EXPR:
1809 /* A lambda can't appear in a template arg, but don't crash on
1810 erroneous input. */
1811 gcc_assert (seen_error ());
1812 return val;
1814 case CAST_EXPR:
1815 case IMPLICIT_CONV_EXPR:
1816 case STATIC_CAST_EXPR:
1817 case REINTERPRET_CAST_EXPR:
1818 case CONST_CAST_EXPR:
1819 case DYNAMIC_CAST_EXPR:
1820 case NEW_EXPR:
1821 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1822 /* Now hash operands as usual. */
1823 break;
1825 default:
1826 break;
1829 switch (tclass)
1831 case tcc_type:
1832 if (alias_template_specialization_p (arg))
1834 // We want an alias specialization that survived strip_typedefs
1835 // to hash differently from its TYPE_CANONICAL, to avoid hash
1836 // collisions that compare as different in template_args_equal.
1837 // These could be dependent specializations that strip_typedefs
1838 // left alone, or untouched specializations because
1839 // coerce_template_parms returns the unconverted template
1840 // arguments if it sees incomplete argument packs.
1841 tree ti = TYPE_TEMPLATE_INFO (arg);
1842 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1844 if (TYPE_CANONICAL (arg))
1845 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1846 val);
1847 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1848 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1849 /* Otherwise just compare the types during lookup. */
1850 return val;
1852 case tcc_declaration:
1853 case tcc_constant:
1854 return iterative_hash_expr (arg, val);
1856 default:
1857 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1859 unsigned n = cp_tree_operand_length (arg);
1860 for (i = 0; i < n; ++i)
1861 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1862 return val;
1865 gcc_unreachable ();
1866 return 0;
1869 /* Unregister the specialization SPEC as a specialization of TMPL.
1870 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1871 if the SPEC was listed as a specialization of TMPL.
1873 Note that SPEC has been ggc_freed, so we can't look inside it. */
1875 bool
1876 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1878 spec_entry *entry;
1879 spec_entry elt;
1881 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1882 elt.args = TI_ARGS (tinfo);
1883 elt.spec = NULL_TREE;
1885 entry = decl_specializations->find (&elt);
1886 if (entry != NULL)
1888 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1889 gcc_assert (new_spec != NULL_TREE);
1890 entry->spec = new_spec;
1891 return 1;
1894 return 0;
1897 /* Like register_specialization, but for local declarations. We are
1898 registering SPEC, an instantiation of TMPL. */
1900 void
1901 register_local_specialization (tree spec, tree tmpl)
1903 local_specializations->put (tmpl, spec);
1906 /* TYPE is a class type. Returns true if TYPE is an explicitly
1907 specialized class. */
1909 bool
1910 explicit_class_specialization_p (tree type)
1912 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1913 return false;
1914 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1917 /* Print the list of functions at FNS, going through all the overloads
1918 for each element of the list. Alternatively, FNS can not be a
1919 TREE_LIST, in which case it will be printed together with all the
1920 overloads.
1922 MORE and *STR should respectively be FALSE and NULL when the function
1923 is called from the outside. They are used internally on recursive
1924 calls. print_candidates manages the two parameters and leaves NULL
1925 in *STR when it ends. */
1927 static void
1928 print_candidates_1 (tree fns, bool more, const char **str)
1930 tree fn, fn2;
1931 char *spaces = NULL;
1933 for (fn = fns; fn; fn = OVL_NEXT (fn))
1934 if (TREE_CODE (fn) == TREE_LIST)
1936 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1937 print_candidates_1 (TREE_VALUE (fn2),
1938 TREE_CHAIN (fn2) || more, str);
1940 else
1942 tree cand = OVL_CURRENT (fn);
1943 if (!*str)
1945 /* Pick the prefix string. */
1946 if (!more && !OVL_NEXT (fns))
1948 inform (DECL_SOURCE_LOCATION (cand),
1949 "candidate is: %#D", cand);
1950 continue;
1953 *str = _("candidates are:");
1954 spaces = get_spaces (*str);
1956 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1957 *str = spaces ? spaces : *str;
1960 if (!more)
1962 free (spaces);
1963 *str = NULL;
1967 /* Print the list of candidate FNS in an error message. FNS can also
1968 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1970 void
1971 print_candidates (tree fns)
1973 const char *str = NULL;
1974 print_candidates_1 (fns, false, &str);
1975 gcc_assert (str == NULL);
1978 /* Get a (possibly) constrained template declaration for the
1979 purpose of ordering candidates. */
1980 static tree
1981 get_template_for_ordering (tree list)
1983 gcc_assert (TREE_CODE (list) == TREE_LIST);
1984 tree f = TREE_VALUE (list);
1985 if (tree ti = DECL_TEMPLATE_INFO (f))
1986 return TI_TEMPLATE (ti);
1987 return f;
1990 /* Among candidates having the same signature, return the
1991 most constrained or NULL_TREE if there is no best candidate.
1992 If the signatures of candidates vary (e.g., template
1993 specialization vs. member function), then there can be no
1994 most constrained.
1996 Note that we don't compare constraints on the functions
1997 themselves, but rather those of their templates. */
1998 static tree
1999 most_constrained_function (tree candidates)
2001 // Try to find the best candidate in a first pass.
2002 tree champ = candidates;
2003 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2005 int winner = more_constrained (get_template_for_ordering (champ),
2006 get_template_for_ordering (c));
2007 if (winner == -1)
2008 champ = c; // The candidate is more constrained
2009 else if (winner == 0)
2010 return NULL_TREE; // Neither is more constrained
2013 // Verify that the champ is better than previous candidates.
2014 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2015 if (!more_constrained (get_template_for_ordering (champ),
2016 get_template_for_ordering (c)))
2017 return NULL_TREE;
2020 return champ;
2024 /* Returns the template (one of the functions given by TEMPLATE_ID)
2025 which can be specialized to match the indicated DECL with the
2026 explicit template args given in TEMPLATE_ID. The DECL may be
2027 NULL_TREE if none is available. In that case, the functions in
2028 TEMPLATE_ID are non-members.
2030 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2031 specialization of a member template.
2033 The TEMPLATE_COUNT is the number of references to qualifying
2034 template classes that appeared in the name of the function. See
2035 check_explicit_specialization for a more accurate description.
2037 TSK indicates what kind of template declaration (if any) is being
2038 declared. TSK_TEMPLATE indicates that the declaration given by
2039 DECL, though a FUNCTION_DECL, has template parameters, and is
2040 therefore a template function.
2042 The template args (those explicitly specified and those deduced)
2043 are output in a newly created vector *TARGS_OUT.
2045 If it is impossible to determine the result, an error message is
2046 issued. The error_mark_node is returned to indicate failure. */
2048 static tree
2049 determine_specialization (tree template_id,
2050 tree decl,
2051 tree* targs_out,
2052 int need_member_template,
2053 int template_count,
2054 tmpl_spec_kind tsk)
2056 tree fns;
2057 tree targs;
2058 tree explicit_targs;
2059 tree candidates = NULL_TREE;
2060 tree rejections = NULL_TREE;
2062 /* A TREE_LIST of templates of which DECL may be a specialization.
2063 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2064 corresponding TREE_PURPOSE is the set of template arguments that,
2065 when used to instantiate the template, would produce a function
2066 with the signature of DECL. */
2067 tree templates = NULL_TREE;
2068 int header_count;
2069 cp_binding_level *b;
2071 *targs_out = NULL_TREE;
2073 if (template_id == error_mark_node || decl == error_mark_node)
2074 return error_mark_node;
2076 /* We shouldn't be specializing a member template of an
2077 unspecialized class template; we already gave an error in
2078 check_specialization_scope, now avoid crashing. */
2079 if (template_count && DECL_CLASS_SCOPE_P (decl)
2080 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2082 gcc_assert (errorcount);
2083 return error_mark_node;
2086 fns = TREE_OPERAND (template_id, 0);
2087 explicit_targs = TREE_OPERAND (template_id, 1);
2089 if (fns == error_mark_node)
2090 return error_mark_node;
2092 /* Check for baselinks. */
2093 if (BASELINK_P (fns))
2094 fns = BASELINK_FUNCTIONS (fns);
2096 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2098 error ("%qD is not a function template", fns);
2099 return error_mark_node;
2101 else if (VAR_P (decl) && !variable_template_p (fns))
2103 error ("%qD is not a variable template", fns);
2104 return error_mark_node;
2107 /* Count the number of template headers specified for this
2108 specialization. */
2109 header_count = 0;
2110 for (b = current_binding_level;
2111 b->kind == sk_template_parms;
2112 b = b->level_chain)
2113 ++header_count;
2115 if (variable_template_p (fns))
2116 templates = tree_cons (explicit_targs, fns, templates);
2117 else for (; fns; fns = OVL_NEXT (fns))
2119 tree fn = OVL_CURRENT (fns);
2121 if (TREE_CODE (fn) == TEMPLATE_DECL)
2123 tree decl_arg_types;
2124 tree fn_arg_types;
2125 tree insttype;
2127 /* In case of explicit specialization, we need to check if
2128 the number of template headers appearing in the specialization
2129 is correct. This is usually done in check_explicit_specialization,
2130 but the check done there cannot be exhaustive when specializing
2131 member functions. Consider the following code:
2133 template <> void A<int>::f(int);
2134 template <> template <> void A<int>::f(int);
2136 Assuming that A<int> is not itself an explicit specialization
2137 already, the first line specializes "f" which is a non-template
2138 member function, whilst the second line specializes "f" which
2139 is a template member function. So both lines are syntactically
2140 correct, and check_explicit_specialization does not reject
2141 them.
2143 Here, we can do better, as we are matching the specialization
2144 against the declarations. We count the number of template
2145 headers, and we check if they match TEMPLATE_COUNT + 1
2146 (TEMPLATE_COUNT is the number of qualifying template classes,
2147 plus there must be another header for the member template
2148 itself).
2150 Notice that if header_count is zero, this is not a
2151 specialization but rather a template instantiation, so there
2152 is no check we can perform here. */
2154 if (header_count && header_count != template_count + 1)
2155 continue;
2157 /* Check that the number of template arguments at the
2158 innermost level for DECL is the same as for FN. */
2159 if (current_binding_level->kind == sk_template_parms
2160 && !current_binding_level->explicit_spec_p
2161 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2162 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2163 (current_template_parms))))
2164 continue;
2166 /* DECL might be a specialization of FN. */
2167 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2168 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2170 /* For a non-static member function, we need to make sure
2171 that the const qualification is the same. Since
2172 get_bindings does not try to merge the "this" parameter,
2173 we must do the comparison explicitly. */
2174 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2175 && !same_type_p (TREE_VALUE (fn_arg_types),
2176 TREE_VALUE (decl_arg_types)))
2177 continue;
2179 /* Skip the "this" parameter and, for constructors of
2180 classes with virtual bases, the VTT parameter. A
2181 full specialization of a constructor will have a VTT
2182 parameter, but a template never will. */
2183 decl_arg_types
2184 = skip_artificial_parms_for (decl, decl_arg_types);
2185 fn_arg_types
2186 = skip_artificial_parms_for (fn, fn_arg_types);
2188 /* Function templates cannot be specializations; there are
2189 no partial specializations of functions. Therefore, if
2190 the type of DECL does not match FN, there is no
2191 match.
2193 Note that it should never be the case that we have both
2194 candidates added here, and for regular member functions
2195 below. */
2196 if (tsk == tsk_template)
2198 if (compparms (fn_arg_types, decl_arg_types))
2199 candidates = tree_cons (NULL_TREE, fn, candidates);
2200 continue;
2203 /* See whether this function might be a specialization of this
2204 template. Suppress access control because we might be trying
2205 to make this specialization a friend, and we have already done
2206 access control for the declaration of the specialization. */
2207 push_deferring_access_checks (dk_no_check);
2208 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2209 pop_deferring_access_checks ();
2211 if (!targs)
2212 /* We cannot deduce template arguments that when used to
2213 specialize TMPL will produce DECL. */
2214 continue;
2216 /* Remove, from the set of candidates, all those functions
2217 whose constraints are not satisfied. */
2218 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2219 continue;
2221 // Then, try to form the new function type.
2222 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
2223 if (insttype == error_mark_node)
2224 continue;
2225 fn_arg_types
2226 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2227 if (!compparms (fn_arg_types, decl_arg_types))
2228 continue;
2230 /* Save this template, and the arguments deduced. */
2231 templates = tree_cons (targs, fn, templates);
2233 else if (need_member_template)
2234 /* FN is an ordinary member function, and we need a
2235 specialization of a member template. */
2237 else if (TREE_CODE (fn) != FUNCTION_DECL)
2238 /* We can get IDENTIFIER_NODEs here in certain erroneous
2239 cases. */
2241 else if (!DECL_FUNCTION_MEMBER_P (fn))
2242 /* This is just an ordinary non-member function. Nothing can
2243 be a specialization of that. */
2245 else if (DECL_ARTIFICIAL (fn))
2246 /* Cannot specialize functions that are created implicitly. */
2248 else
2250 tree decl_arg_types;
2252 /* This is an ordinary member function. However, since
2253 we're here, we can assume its enclosing class is a
2254 template class. For example,
2256 template <typename T> struct S { void f(); };
2257 template <> void S<int>::f() {}
2259 Here, S<int>::f is a non-template, but S<int> is a
2260 template class. If FN has the same type as DECL, we
2261 might be in business. */
2263 if (!DECL_TEMPLATE_INFO (fn))
2264 /* Its enclosing class is an explicit specialization
2265 of a template class. This is not a candidate. */
2266 continue;
2269 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2270 TREE_TYPE (TREE_TYPE (fn))))
2271 /* The return types differ. */
2272 continue;
2274 /* Adjust the type of DECL in case FN is a static member. */
2275 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2276 if (DECL_STATIC_FUNCTION_P (fn)
2277 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2278 decl_arg_types = TREE_CHAIN (decl_arg_types);
2280 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2281 decl_arg_types))
2282 continue;
2284 // If the deduced arguments do not satisfy the constraints,
2285 // this is not a candidate. If it fails, record the
2286 // rejected candidate.
2287 if (flag_concepts && !constraints_satisfied_p (fn))
2289 rejections = tree_cons (NULL_TREE, fn, rejections);
2290 continue;
2293 // Add the candidate.
2294 candidates = tree_cons (NULL_TREE, fn, candidates);
2298 if (templates && TREE_CHAIN (templates))
2300 /* We have:
2302 [temp.expl.spec]
2304 It is possible for a specialization with a given function
2305 signature to be instantiated from more than one function
2306 template. In such cases, explicit specification of the
2307 template arguments must be used to uniquely identify the
2308 function template specialization being specialized.
2310 Note that here, there's no suggestion that we're supposed to
2311 determine which of the candidate templates is most
2312 specialized. However, we, also have:
2314 [temp.func.order]
2316 Partial ordering of overloaded function template
2317 declarations is used in the following contexts to select
2318 the function template to which a function template
2319 specialization refers:
2321 -- when an explicit specialization refers to a function
2322 template.
2324 So, we do use the partial ordering rules, at least for now.
2325 This extension can only serve to make invalid programs valid,
2326 so it's safe. And, there is strong anecdotal evidence that
2327 the committee intended the partial ordering rules to apply;
2328 the EDG front end has that behavior, and John Spicer claims
2329 that the committee simply forgot to delete the wording in
2330 [temp.expl.spec]. */
2331 tree tmpl = most_specialized_instantiation (templates);
2332 if (tmpl != error_mark_node)
2334 templates = tmpl;
2335 TREE_CHAIN (templates) = NULL_TREE;
2339 // Concepts allows multiple declarations of member functions
2340 // with the same signature. Like above, we need to rely on
2341 // on the partial ordering of those candidates to determine which
2342 // is the best.
2343 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2345 if (tree cand = most_constrained_function (candidates))
2347 candidates = cand;
2348 TREE_CHAIN (cand) = NULL_TREE;
2352 if (templates == NULL_TREE && candidates == NULL_TREE)
2354 error ("template-id %qD for %q+D does not match any template "
2355 "declaration", template_id, decl);
2356 if (rejections)
2357 print_candidates (rejections);
2358 else if (header_count && header_count != template_count + 1)
2359 inform (input_location, "saw %d %<template<>%>, need %d for "
2360 "specializing a member function template",
2361 header_count, template_count + 1);
2362 return error_mark_node;
2364 else if ((templates && TREE_CHAIN (templates))
2365 || (candidates && TREE_CHAIN (candidates))
2366 || (templates && candidates))
2368 error ("ambiguous template specialization %qD for %q+D",
2369 template_id, decl);
2370 candidates = chainon (candidates, templates);
2371 print_candidates (candidates);
2372 return error_mark_node;
2375 /* We have one, and exactly one, match. */
2376 if (candidates)
2378 tree fn = TREE_VALUE (candidates);
2379 *targs_out = copy_node (DECL_TI_ARGS (fn));
2381 // Propagate the candidate's constraints to the declaration.
2382 set_constraints (decl, get_constraints (fn));
2384 /* DECL is a re-declaration or partial instantiation of a template
2385 function. */
2386 if (TREE_CODE (fn) == TEMPLATE_DECL)
2387 return fn;
2389 /* It was a specialization of an ordinary member function in a
2390 template class. */
2391 return DECL_TI_TEMPLATE (fn);
2394 /* It was a specialization of a template. */
2395 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2396 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2398 *targs_out = copy_node (targs);
2399 SET_TMPL_ARGS_LEVEL (*targs_out,
2400 TMPL_ARGS_DEPTH (*targs_out),
2401 TREE_PURPOSE (templates));
2403 else
2404 *targs_out = TREE_PURPOSE (templates);
2406 return TREE_VALUE (templates);
2409 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2410 but with the default argument values filled in from those in the
2411 TMPL_TYPES. */
2413 static tree
2414 copy_default_args_to_explicit_spec_1 (tree spec_types,
2415 tree tmpl_types)
2417 tree new_spec_types;
2419 if (!spec_types)
2420 return NULL_TREE;
2422 if (spec_types == void_list_node)
2423 return void_list_node;
2425 /* Substitute into the rest of the list. */
2426 new_spec_types =
2427 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2428 TREE_CHAIN (tmpl_types));
2430 /* Add the default argument for this parameter. */
2431 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2432 TREE_VALUE (spec_types),
2433 new_spec_types);
2436 /* DECL is an explicit specialization. Replicate default arguments
2437 from the template it specializes. (That way, code like:
2439 template <class T> void f(T = 3);
2440 template <> void f(double);
2441 void g () { f (); }
2443 works, as required.) An alternative approach would be to look up
2444 the correct default arguments at the call-site, but this approach
2445 is consistent with how implicit instantiations are handled. */
2447 static void
2448 copy_default_args_to_explicit_spec (tree decl)
2450 tree tmpl;
2451 tree spec_types;
2452 tree tmpl_types;
2453 tree new_spec_types;
2454 tree old_type;
2455 tree new_type;
2456 tree t;
2457 tree object_type = NULL_TREE;
2458 tree in_charge = NULL_TREE;
2459 tree vtt = NULL_TREE;
2461 /* See if there's anything we need to do. */
2462 tmpl = DECL_TI_TEMPLATE (decl);
2463 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2464 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2465 if (TREE_PURPOSE (t))
2466 break;
2467 if (!t)
2468 return;
2470 old_type = TREE_TYPE (decl);
2471 spec_types = TYPE_ARG_TYPES (old_type);
2473 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2475 /* Remove the this pointer, but remember the object's type for
2476 CV quals. */
2477 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2478 spec_types = TREE_CHAIN (spec_types);
2479 tmpl_types = TREE_CHAIN (tmpl_types);
2481 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2483 /* DECL may contain more parameters than TMPL due to the extra
2484 in-charge parameter in constructors and destructors. */
2485 in_charge = spec_types;
2486 spec_types = TREE_CHAIN (spec_types);
2488 if (DECL_HAS_VTT_PARM_P (decl))
2490 vtt = spec_types;
2491 spec_types = TREE_CHAIN (spec_types);
2495 /* Compute the merged default arguments. */
2496 new_spec_types =
2497 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2499 /* Compute the new FUNCTION_TYPE. */
2500 if (object_type)
2502 if (vtt)
2503 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2504 TREE_VALUE (vtt),
2505 new_spec_types);
2507 if (in_charge)
2508 /* Put the in-charge parameter back. */
2509 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2510 TREE_VALUE (in_charge),
2511 new_spec_types);
2513 new_type = build_method_type_directly (object_type,
2514 TREE_TYPE (old_type),
2515 new_spec_types);
2517 else
2518 new_type = build_function_type (TREE_TYPE (old_type),
2519 new_spec_types);
2520 new_type = cp_build_type_attribute_variant (new_type,
2521 TYPE_ATTRIBUTES (old_type));
2522 new_type = build_exception_variant (new_type,
2523 TYPE_RAISES_EXCEPTIONS (old_type));
2525 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2526 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2528 TREE_TYPE (decl) = new_type;
2531 /* Return the number of template headers we expect to see for a definition
2532 or specialization of CTYPE or one of its non-template members. */
2535 num_template_headers_for_class (tree ctype)
2537 int num_templates = 0;
2539 while (ctype && CLASS_TYPE_P (ctype))
2541 /* You're supposed to have one `template <...>' for every
2542 template class, but you don't need one for a full
2543 specialization. For example:
2545 template <class T> struct S{};
2546 template <> struct S<int> { void f(); };
2547 void S<int>::f () {}
2549 is correct; there shouldn't be a `template <>' for the
2550 definition of `S<int>::f'. */
2551 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2552 /* If CTYPE does not have template information of any
2553 kind, then it is not a template, nor is it nested
2554 within a template. */
2555 break;
2556 if (explicit_class_specialization_p (ctype))
2557 break;
2558 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2559 ++num_templates;
2561 ctype = TYPE_CONTEXT (ctype);
2564 return num_templates;
2567 /* Do a simple sanity check on the template headers that precede the
2568 variable declaration DECL. */
2570 void
2571 check_template_variable (tree decl)
2573 tree ctx = CP_DECL_CONTEXT (decl);
2574 int wanted = num_template_headers_for_class (ctx);
2575 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2576 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2578 if (cxx_dialect < cxx14)
2579 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2580 "variable templates only available with "
2581 "-std=c++14 or -std=gnu++14");
2583 // Namespace-scope variable templates should have a template header.
2584 ++wanted;
2586 if (template_header_count > wanted)
2588 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2589 "too many template headers for %D (should be %d)",
2590 decl, wanted);
2591 if (warned && CLASS_TYPE_P (ctx)
2592 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2593 inform (DECL_SOURCE_LOCATION (decl),
2594 "members of an explicitly specialized class are defined "
2595 "without a template header");
2599 /* Check to see if the function just declared, as indicated in
2600 DECLARATOR, and in DECL, is a specialization of a function
2601 template. We may also discover that the declaration is an explicit
2602 instantiation at this point.
2604 Returns DECL, or an equivalent declaration that should be used
2605 instead if all goes well. Issues an error message if something is
2606 amiss. Returns error_mark_node if the error is not easily
2607 recoverable.
2609 FLAGS is a bitmask consisting of the following flags:
2611 2: The function has a definition.
2612 4: The function is a friend.
2614 The TEMPLATE_COUNT is the number of references to qualifying
2615 template classes that appeared in the name of the function. For
2616 example, in
2618 template <class T> struct S { void f(); };
2619 void S<int>::f();
2621 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2622 classes are not counted in the TEMPLATE_COUNT, so that in
2624 template <class T> struct S {};
2625 template <> struct S<int> { void f(); }
2626 template <> void S<int>::f();
2628 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2629 invalid; there should be no template <>.)
2631 If the function is a specialization, it is marked as such via
2632 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2633 is set up correctly, and it is added to the list of specializations
2634 for that template. */
2636 tree
2637 check_explicit_specialization (tree declarator,
2638 tree decl,
2639 int template_count,
2640 int flags)
2642 int have_def = flags & 2;
2643 int is_friend = flags & 4;
2644 bool is_concept = flags & 8;
2645 int specialization = 0;
2646 int explicit_instantiation = 0;
2647 int member_specialization = 0;
2648 tree ctype = DECL_CLASS_CONTEXT (decl);
2649 tree dname = DECL_NAME (decl);
2650 tmpl_spec_kind tsk;
2652 if (is_friend)
2654 if (!processing_specialization)
2655 tsk = tsk_none;
2656 else
2657 tsk = tsk_excessive_parms;
2659 else
2660 tsk = current_tmpl_spec_kind (template_count);
2662 switch (tsk)
2664 case tsk_none:
2665 if (processing_specialization)
2667 specialization = 1;
2668 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2670 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2672 if (is_friend)
2673 /* This could be something like:
2675 template <class T> void f(T);
2676 class S { friend void f<>(int); } */
2677 specialization = 1;
2678 else
2680 /* This case handles bogus declarations like template <>
2681 template <class T> void f<int>(); */
2683 error ("template-id %qD in declaration of primary template",
2684 declarator);
2685 return decl;
2688 break;
2690 case tsk_invalid_member_spec:
2691 /* The error has already been reported in
2692 check_specialization_scope. */
2693 return error_mark_node;
2695 case tsk_invalid_expl_inst:
2696 error ("template parameter list used in explicit instantiation");
2698 /* Fall through. */
2700 case tsk_expl_inst:
2701 if (have_def)
2702 error ("definition provided for explicit instantiation");
2704 explicit_instantiation = 1;
2705 break;
2707 case tsk_excessive_parms:
2708 case tsk_insufficient_parms:
2709 if (tsk == tsk_excessive_parms)
2710 error ("too many template parameter lists in declaration of %qD",
2711 decl);
2712 else if (template_header_count)
2713 error("too few template parameter lists in declaration of %qD", decl);
2714 else
2715 error("explicit specialization of %qD must be introduced by "
2716 "%<template <>%>", decl);
2718 /* Fall through. */
2719 case tsk_expl_spec:
2720 if (is_concept)
2721 error ("explicit specialization declared %<concept%>");
2723 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2724 /* In cases like template<> constexpr bool v = true;
2725 We'll give an error in check_template_variable. */
2726 break;
2728 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2729 if (ctype)
2730 member_specialization = 1;
2731 else
2732 specialization = 1;
2733 break;
2735 case tsk_template:
2736 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2738 /* This case handles bogus declarations like template <>
2739 template <class T> void f<int>(); */
2741 if (!uses_template_parms (declarator))
2742 error ("template-id %qD in declaration of primary template",
2743 declarator);
2744 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2746 /* Partial specialization of variable template. */
2747 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2748 specialization = 1;
2749 goto ok;
2751 else if (cxx_dialect < cxx14)
2752 error ("non-type partial specialization %qD "
2753 "is not allowed", declarator);
2754 else
2755 error ("non-class, non-variable partial specialization %qD "
2756 "is not allowed", declarator);
2757 return decl;
2758 ok:;
2761 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2762 /* This is a specialization of a member template, without
2763 specialization the containing class. Something like:
2765 template <class T> struct S {
2766 template <class U> void f (U);
2768 template <> template <class U> void S<int>::f(U) {}
2770 That's a specialization -- but of the entire template. */
2771 specialization = 1;
2772 break;
2774 default:
2775 gcc_unreachable ();
2778 if ((specialization || member_specialization)
2779 /* This doesn't apply to variable templates. */
2780 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2781 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2783 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2784 for (; t; t = TREE_CHAIN (t))
2785 if (TREE_PURPOSE (t))
2787 permerror (input_location,
2788 "default argument specified in explicit specialization");
2789 break;
2793 if (specialization || member_specialization || explicit_instantiation)
2795 tree tmpl = NULL_TREE;
2796 tree targs = NULL_TREE;
2797 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2799 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2800 if (!was_template_id)
2802 tree fns;
2804 gcc_assert (identifier_p (declarator));
2805 if (ctype)
2806 fns = dname;
2807 else
2809 /* If there is no class context, the explicit instantiation
2810 must be at namespace scope. */
2811 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2813 /* Find the namespace binding, using the declaration
2814 context. */
2815 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2816 false, true);
2817 if (fns == error_mark_node || !is_overloaded_fn (fns))
2819 error ("%qD is not a template function", dname);
2820 fns = error_mark_node;
2822 else
2824 tree fn = OVL_CURRENT (fns);
2825 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2826 CP_DECL_CONTEXT (fn)))
2827 error ("%qD is not declared in %qD",
2828 decl, current_namespace);
2832 declarator = lookup_template_function (fns, NULL_TREE);
2835 if (declarator == error_mark_node)
2836 return error_mark_node;
2838 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2840 if (!explicit_instantiation)
2841 /* A specialization in class scope. This is invalid,
2842 but the error will already have been flagged by
2843 check_specialization_scope. */
2844 return error_mark_node;
2845 else
2847 /* It's not valid to write an explicit instantiation in
2848 class scope, e.g.:
2850 class C { template void f(); }
2852 This case is caught by the parser. However, on
2853 something like:
2855 template class C { void f(); };
2857 (which is invalid) we can get here. The error will be
2858 issued later. */
2862 return decl;
2864 else if (ctype != NULL_TREE
2865 && (identifier_p (TREE_OPERAND (declarator, 0))))
2867 // We'll match variable templates in start_decl.
2868 if (VAR_P (decl))
2869 return decl;
2871 /* Find the list of functions in ctype that have the same
2872 name as the declared function. */
2873 tree name = TREE_OPERAND (declarator, 0);
2874 tree fns = NULL_TREE;
2875 int idx;
2877 if (constructor_name_p (name, ctype))
2879 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2881 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2882 : !CLASSTYPE_DESTRUCTORS (ctype))
2884 /* From [temp.expl.spec]:
2886 If such an explicit specialization for the member
2887 of a class template names an implicitly-declared
2888 special member function (clause _special_), the
2889 program is ill-formed.
2891 Similar language is found in [temp.explicit]. */
2892 error ("specialization of implicitly-declared special member function");
2893 return error_mark_node;
2896 name = is_constructor ? ctor_identifier : dtor_identifier;
2899 if (!DECL_CONV_FN_P (decl))
2901 idx = lookup_fnfields_1 (ctype, name);
2902 if (idx >= 0)
2903 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2905 else
2907 vec<tree, va_gc> *methods;
2908 tree ovl;
2910 /* For a type-conversion operator, we cannot do a
2911 name-based lookup. We might be looking for `operator
2912 int' which will be a specialization of `operator T'.
2913 So, we find *all* the conversion operators, and then
2914 select from them. */
2915 fns = NULL_TREE;
2917 methods = CLASSTYPE_METHOD_VEC (ctype);
2918 if (methods)
2919 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2920 methods->iterate (idx, &ovl);
2921 ++idx)
2923 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2924 /* There are no more conversion functions. */
2925 break;
2927 /* Glue all these conversion functions together
2928 with those we already have. */
2929 for (; ovl; ovl = OVL_NEXT (ovl))
2930 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2934 if (fns == NULL_TREE)
2936 error ("no member function %qD declared in %qT", name, ctype);
2937 return error_mark_node;
2939 else
2940 TREE_OPERAND (declarator, 0) = fns;
2943 /* Figure out what exactly is being specialized at this point.
2944 Note that for an explicit instantiation, even one for a
2945 member function, we cannot tell apriori whether the
2946 instantiation is for a member template, or just a member
2947 function of a template class. Even if a member template is
2948 being instantiated, the member template arguments may be
2949 elided if they can be deduced from the rest of the
2950 declaration. */
2951 tmpl = determine_specialization (declarator, decl,
2952 &targs,
2953 member_specialization,
2954 template_count,
2955 tsk);
2957 if (!tmpl || tmpl == error_mark_node)
2958 /* We couldn't figure out what this declaration was
2959 specializing. */
2960 return error_mark_node;
2961 else
2963 tree gen_tmpl = most_general_template (tmpl);
2965 if (explicit_instantiation)
2967 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2968 is done by do_decl_instantiation later. */
2970 int arg_depth = TMPL_ARGS_DEPTH (targs);
2971 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2973 if (arg_depth > parm_depth)
2975 /* If TMPL is not the most general template (for
2976 example, if TMPL is a friend template that is
2977 injected into namespace scope), then there will
2978 be too many levels of TARGS. Remove some of them
2979 here. */
2980 int i;
2981 tree new_targs;
2983 new_targs = make_tree_vec (parm_depth);
2984 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2985 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2986 = TREE_VEC_ELT (targs, i);
2987 targs = new_targs;
2990 return instantiate_template (tmpl, targs, tf_error);
2993 /* If we thought that the DECL was a member function, but it
2994 turns out to be specializing a static member function,
2995 make DECL a static member function as well. */
2996 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2997 && DECL_STATIC_FUNCTION_P (tmpl)
2998 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2999 revert_static_member_fn (decl);
3001 /* If this is a specialization of a member template of a
3002 template class, we want to return the TEMPLATE_DECL, not
3003 the specialization of it. */
3004 if (tsk == tsk_template && !was_template_id)
3006 tree result = DECL_TEMPLATE_RESULT (tmpl);
3007 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3008 DECL_INITIAL (result) = NULL_TREE;
3009 if (have_def)
3011 tree parm;
3012 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3013 DECL_SOURCE_LOCATION (result)
3014 = DECL_SOURCE_LOCATION (decl);
3015 /* We want to use the argument list specified in the
3016 definition, not in the original declaration. */
3017 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3018 for (parm = DECL_ARGUMENTS (result); parm;
3019 parm = DECL_CHAIN (parm))
3020 DECL_CONTEXT (parm) = result;
3022 return register_specialization (tmpl, gen_tmpl, targs,
3023 is_friend, 0);
3026 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3027 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3029 if (was_template_id)
3030 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3032 /* Inherit default function arguments from the template
3033 DECL is specializing. */
3034 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3035 copy_default_args_to_explicit_spec (decl);
3037 /* This specialization has the same protection as the
3038 template it specializes. */
3039 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3040 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3042 /* 7.1.1-1 [dcl.stc]
3044 A storage-class-specifier shall not be specified in an
3045 explicit specialization...
3047 The parser rejects these, so unless action is taken here,
3048 explicit function specializations will always appear with
3049 global linkage.
3051 The action recommended by the C++ CWG in response to C++
3052 defect report 605 is to make the storage class and linkage
3053 of the explicit specialization match the templated function:
3055 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3057 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3059 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3060 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3062 // A concept cannot be specialized.
3063 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3065 error ("explicit specialization of concept %qD", gen_tmpl);
3066 return error_mark_node;
3069 /* This specialization has the same linkage and visibility as
3070 the function template it specializes. */
3071 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3072 if (! TREE_PUBLIC (decl))
3074 DECL_INTERFACE_KNOWN (decl) = 1;
3075 DECL_NOT_REALLY_EXTERN (decl) = 1;
3077 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3078 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3080 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3081 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3085 /* If DECL is a friend declaration, declared using an
3086 unqualified name, the namespace associated with DECL may
3087 have been set incorrectly. For example, in:
3089 template <typename T> void f(T);
3090 namespace N {
3091 struct S { friend void f<int>(int); }
3094 we will have set the DECL_CONTEXT for the friend
3095 declaration to N, rather than to the global namespace. */
3096 if (DECL_NAMESPACE_SCOPE_P (decl))
3097 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3099 if (is_friend && !have_def)
3100 /* This is not really a declaration of a specialization.
3101 It's just the name of an instantiation. But, it's not
3102 a request for an instantiation, either. */
3103 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3104 else if (TREE_CODE (decl) == FUNCTION_DECL)
3105 /* A specialization is not necessarily COMDAT. */
3106 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3107 && DECL_DECLARED_INLINE_P (decl));
3108 else if (TREE_CODE (decl) == VAR_DECL)
3109 DECL_COMDAT (decl) = false;
3111 /* Register this specialization so that we can find it
3112 again. */
3113 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
3115 /* A 'structor should already have clones. */
3116 gcc_assert (decl == error_mark_node
3117 || variable_template_p (tmpl)
3118 || !(DECL_CONSTRUCTOR_P (decl)
3119 || DECL_DESTRUCTOR_P (decl))
3120 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3124 return decl;
3127 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3128 parameters. These are represented in the same format used for
3129 DECL_TEMPLATE_PARMS. */
3132 comp_template_parms (const_tree parms1, const_tree parms2)
3134 const_tree p1;
3135 const_tree p2;
3137 if (parms1 == parms2)
3138 return 1;
3140 for (p1 = parms1, p2 = parms2;
3141 p1 != NULL_TREE && p2 != NULL_TREE;
3142 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3144 tree t1 = TREE_VALUE (p1);
3145 tree t2 = TREE_VALUE (p2);
3146 int i;
3148 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3149 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3151 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3152 return 0;
3154 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3156 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3157 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3159 /* If either of the template parameters are invalid, assume
3160 they match for the sake of error recovery. */
3161 if (error_operand_p (parm1) || error_operand_p (parm2))
3162 return 1;
3164 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3165 return 0;
3167 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3168 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3169 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3170 continue;
3171 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3172 return 0;
3176 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3177 /* One set of parameters has more parameters lists than the
3178 other. */
3179 return 0;
3181 return 1;
3184 /* Determine whether PARM is a parameter pack. */
3186 bool
3187 template_parameter_pack_p (const_tree parm)
3189 /* Determine if we have a non-type template parameter pack. */
3190 if (TREE_CODE (parm) == PARM_DECL)
3191 return (DECL_TEMPLATE_PARM_P (parm)
3192 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3193 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3194 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3196 /* If this is a list of template parameters, we could get a
3197 TYPE_DECL or a TEMPLATE_DECL. */
3198 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3199 parm = TREE_TYPE (parm);
3201 /* Otherwise it must be a type template parameter. */
3202 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3203 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3204 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3207 /* Determine if T is a function parameter pack. */
3209 bool
3210 function_parameter_pack_p (const_tree t)
3212 if (t && TREE_CODE (t) == PARM_DECL)
3213 return DECL_PACK_P (t);
3214 return false;
3217 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3218 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3220 tree
3221 get_function_template_decl (const_tree primary_func_tmpl_inst)
3223 if (! primary_func_tmpl_inst
3224 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3225 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3226 return NULL;
3228 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3231 /* Return true iff the function parameter PARAM_DECL was expanded
3232 from the function parameter pack PACK. */
3234 bool
3235 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3237 if (DECL_ARTIFICIAL (param_decl)
3238 || !function_parameter_pack_p (pack))
3239 return false;
3241 /* The parameter pack and its pack arguments have the same
3242 DECL_PARM_INDEX. */
3243 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3246 /* Determine whether ARGS describes a variadic template args list,
3247 i.e., one that is terminated by a template argument pack. */
3249 static bool
3250 template_args_variadic_p (tree args)
3252 int nargs;
3253 tree last_parm;
3255 if (args == NULL_TREE)
3256 return false;
3258 args = INNERMOST_TEMPLATE_ARGS (args);
3259 nargs = TREE_VEC_LENGTH (args);
3261 if (nargs == 0)
3262 return false;
3264 last_parm = TREE_VEC_ELT (args, nargs - 1);
3266 return ARGUMENT_PACK_P (last_parm);
3269 /* Generate a new name for the parameter pack name NAME (an
3270 IDENTIFIER_NODE) that incorporates its */
3272 static tree
3273 make_ith_pack_parameter_name (tree name, int i)
3275 /* Munge the name to include the parameter index. */
3276 #define NUMBUF_LEN 128
3277 char numbuf[NUMBUF_LEN];
3278 char* newname;
3279 int newname_len;
3281 if (name == NULL_TREE)
3282 return name;
3283 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3284 newname_len = IDENTIFIER_LENGTH (name)
3285 + strlen (numbuf) + 2;
3286 newname = (char*)alloca (newname_len);
3287 snprintf (newname, newname_len,
3288 "%s#%i", IDENTIFIER_POINTER (name), i);
3289 return get_identifier (newname);
3292 /* Return true if T is a primary function, class or alias template
3293 instantiation. */
3295 bool
3296 primary_template_instantiation_p (const_tree t)
3298 if (!t)
3299 return false;
3301 if (TREE_CODE (t) == FUNCTION_DECL)
3302 return DECL_LANG_SPECIFIC (t)
3303 && DECL_TEMPLATE_INSTANTIATION (t)
3304 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3305 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3306 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3307 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3308 else if (alias_template_specialization_p (t))
3309 return true;
3310 return false;
3313 /* Return true if PARM is a template template parameter. */
3315 bool
3316 template_template_parameter_p (const_tree parm)
3318 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3321 /* Return true iff PARM is a DECL representing a type template
3322 parameter. */
3324 bool
3325 template_type_parameter_p (const_tree parm)
3327 return (parm
3328 && (TREE_CODE (parm) == TYPE_DECL
3329 || TREE_CODE (parm) == TEMPLATE_DECL)
3330 && DECL_TEMPLATE_PARM_P (parm));
3333 /* Return the template parameters of T if T is a
3334 primary template instantiation, NULL otherwise. */
3336 tree
3337 get_primary_template_innermost_parameters (const_tree t)
3339 tree parms = NULL, template_info = NULL;
3341 if ((template_info = get_template_info (t))
3342 && primary_template_instantiation_p (t))
3343 parms = INNERMOST_TEMPLATE_PARMS
3344 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3346 return parms;
3349 /* Return the template parameters of the LEVELth level from the full list
3350 of template parameters PARMS. */
3352 tree
3353 get_template_parms_at_level (tree parms, int level)
3355 tree p;
3356 if (!parms
3357 || TREE_CODE (parms) != TREE_LIST
3358 || level > TMPL_PARMS_DEPTH (parms))
3359 return NULL_TREE;
3361 for (p = parms; p; p = TREE_CHAIN (p))
3362 if (TMPL_PARMS_DEPTH (p) == level)
3363 return p;
3365 return NULL_TREE;
3368 /* Returns the template arguments of T if T is a template instantiation,
3369 NULL otherwise. */
3371 tree
3372 get_template_innermost_arguments (const_tree t)
3374 tree args = NULL, template_info = NULL;
3376 if ((template_info = get_template_info (t))
3377 && TI_ARGS (template_info))
3378 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3380 return args;
3383 /* Return the argument pack elements of T if T is a template argument pack,
3384 NULL otherwise. */
3386 tree
3387 get_template_argument_pack_elems (const_tree t)
3389 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3390 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3391 return NULL;
3393 return ARGUMENT_PACK_ARGS (t);
3396 /* Structure used to track the progress of find_parameter_packs_r. */
3397 struct find_parameter_pack_data
3399 /* TREE_LIST that will contain all of the parameter packs found by
3400 the traversal. */
3401 tree* parameter_packs;
3403 /* Set of AST nodes that have been visited by the traversal. */
3404 hash_set<tree> *visited;
3407 /* Identifies all of the argument packs that occur in a template
3408 argument and appends them to the TREE_LIST inside DATA, which is a
3409 find_parameter_pack_data structure. This is a subroutine of
3410 make_pack_expansion and uses_parameter_packs. */
3411 static tree
3412 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3414 tree t = *tp;
3415 struct find_parameter_pack_data* ppd =
3416 (struct find_parameter_pack_data*)data;
3417 bool parameter_pack_p = false;
3419 /* Handle type aliases/typedefs. */
3420 if (TYPE_ALIAS_P (t))
3422 if (TYPE_TEMPLATE_INFO (t))
3423 cp_walk_tree (&TYPE_TI_ARGS (t),
3424 &find_parameter_packs_r,
3425 ppd, ppd->visited);
3426 *walk_subtrees = 0;
3427 return NULL_TREE;
3430 /* Identify whether this is a parameter pack or not. */
3431 switch (TREE_CODE (t))
3433 case TEMPLATE_PARM_INDEX:
3434 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3435 parameter_pack_p = true;
3436 break;
3438 case TEMPLATE_TYPE_PARM:
3439 t = TYPE_MAIN_VARIANT (t);
3440 case TEMPLATE_TEMPLATE_PARM:
3441 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3442 parameter_pack_p = true;
3443 break;
3445 case FIELD_DECL:
3446 case PARM_DECL:
3447 if (DECL_PACK_P (t))
3449 /* We don't want to walk into the type of a PARM_DECL,
3450 because we don't want to see the type parameter pack. */
3451 *walk_subtrees = 0;
3452 parameter_pack_p = true;
3454 break;
3456 /* Look through a lambda capture proxy to the field pack. */
3457 case VAR_DECL:
3458 if (DECL_HAS_VALUE_EXPR_P (t))
3460 tree v = DECL_VALUE_EXPR (t);
3461 cp_walk_tree (&v,
3462 &find_parameter_packs_r,
3463 ppd, ppd->visited);
3464 *walk_subtrees = 0;
3466 break;
3468 case BASES:
3469 parameter_pack_p = true;
3470 break;
3471 default:
3472 /* Not a parameter pack. */
3473 break;
3476 if (parameter_pack_p)
3478 /* Add this parameter pack to the list. */
3479 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3482 if (TYPE_P (t))
3483 cp_walk_tree (&TYPE_CONTEXT (t),
3484 &find_parameter_packs_r, ppd, ppd->visited);
3486 /* This switch statement will return immediately if we don't find a
3487 parameter pack. */
3488 switch (TREE_CODE (t))
3490 case TEMPLATE_PARM_INDEX:
3491 return NULL_TREE;
3493 case BOUND_TEMPLATE_TEMPLATE_PARM:
3494 /* Check the template itself. */
3495 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3496 &find_parameter_packs_r, ppd, ppd->visited);
3497 /* Check the template arguments. */
3498 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3499 ppd->visited);
3500 *walk_subtrees = 0;
3501 return NULL_TREE;
3503 case TEMPLATE_TYPE_PARM:
3504 case TEMPLATE_TEMPLATE_PARM:
3505 return NULL_TREE;
3507 case PARM_DECL:
3508 return NULL_TREE;
3510 case RECORD_TYPE:
3511 if (TYPE_PTRMEMFUNC_P (t))
3512 return NULL_TREE;
3513 /* Fall through. */
3515 case UNION_TYPE:
3516 case ENUMERAL_TYPE:
3517 if (TYPE_TEMPLATE_INFO (t))
3518 cp_walk_tree (&TYPE_TI_ARGS (t),
3519 &find_parameter_packs_r, ppd, ppd->visited);
3521 *walk_subtrees = 0;
3522 return NULL_TREE;
3524 case CONSTRUCTOR:
3525 case TEMPLATE_DECL:
3526 cp_walk_tree (&TREE_TYPE (t),
3527 &find_parameter_packs_r, ppd, ppd->visited);
3528 return NULL_TREE;
3530 case TYPENAME_TYPE:
3531 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3532 ppd, ppd->visited);
3533 *walk_subtrees = 0;
3534 return NULL_TREE;
3536 case TYPE_PACK_EXPANSION:
3537 case EXPR_PACK_EXPANSION:
3538 *walk_subtrees = 0;
3539 return NULL_TREE;
3541 case INTEGER_TYPE:
3542 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3543 ppd, ppd->visited);
3544 *walk_subtrees = 0;
3545 return NULL_TREE;
3547 case IDENTIFIER_NODE:
3548 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3549 ppd->visited);
3550 *walk_subtrees = 0;
3551 return NULL_TREE;
3553 default:
3554 return NULL_TREE;
3557 return NULL_TREE;
3560 /* Determines if the expression or type T uses any parameter packs. */
3561 bool
3562 uses_parameter_packs (tree t)
3564 tree parameter_packs = NULL_TREE;
3565 struct find_parameter_pack_data ppd;
3566 ppd.parameter_packs = &parameter_packs;
3567 ppd.visited = new hash_set<tree>;
3568 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3569 delete ppd.visited;
3570 return parameter_packs != NULL_TREE;
3573 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3574 representation a base-class initializer into a parameter pack
3575 expansion. If all goes well, the resulting node will be an
3576 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3577 respectively. */
3578 tree
3579 make_pack_expansion (tree arg)
3581 tree result;
3582 tree parameter_packs = NULL_TREE;
3583 bool for_types = false;
3584 struct find_parameter_pack_data ppd;
3586 if (!arg || arg == error_mark_node)
3587 return arg;
3589 if (TREE_CODE (arg) == TREE_LIST)
3591 /* The only time we will see a TREE_LIST here is for a base
3592 class initializer. In this case, the TREE_PURPOSE will be a
3593 _TYPE node (representing the base class expansion we're
3594 initializing) and the TREE_VALUE will be a TREE_LIST
3595 containing the initialization arguments.
3597 The resulting expansion looks somewhat different from most
3598 expansions. Rather than returning just one _EXPANSION, we
3599 return a TREE_LIST whose TREE_PURPOSE is a
3600 TYPE_PACK_EXPANSION containing the bases that will be
3601 initialized. The TREE_VALUE will be identical to the
3602 original TREE_VALUE, which is a list of arguments that will
3603 be passed to each base. We do not introduce any new pack
3604 expansion nodes into the TREE_VALUE (although it is possible
3605 that some already exist), because the TREE_PURPOSE and
3606 TREE_VALUE all need to be expanded together with the same
3607 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3608 resulting TREE_PURPOSE will mention the parameter packs in
3609 both the bases and the arguments to the bases. */
3610 tree purpose;
3611 tree value;
3612 tree parameter_packs = NULL_TREE;
3614 /* Determine which parameter packs will be used by the base
3615 class expansion. */
3616 ppd.visited = new hash_set<tree>;
3617 ppd.parameter_packs = &parameter_packs;
3618 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3619 &ppd, ppd.visited);
3621 if (parameter_packs == NULL_TREE)
3623 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3624 delete ppd.visited;
3625 return error_mark_node;
3628 if (TREE_VALUE (arg) != void_type_node)
3630 /* Collect the sets of parameter packs used in each of the
3631 initialization arguments. */
3632 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3634 /* Determine which parameter packs will be expanded in this
3635 argument. */
3636 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3637 &ppd, ppd.visited);
3641 delete ppd.visited;
3643 /* Create the pack expansion type for the base type. */
3644 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3645 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3646 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3648 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3649 they will rarely be compared to anything. */
3650 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3652 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3655 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3656 for_types = true;
3658 /* Build the PACK_EXPANSION_* node. */
3659 result = for_types
3660 ? cxx_make_type (TYPE_PACK_EXPANSION)
3661 : make_node (EXPR_PACK_EXPANSION);
3662 SET_PACK_EXPANSION_PATTERN (result, arg);
3663 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3665 /* Propagate type and const-expression information. */
3666 TREE_TYPE (result) = TREE_TYPE (arg);
3667 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3669 else
3670 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3671 they will rarely be compared to anything. */
3672 SET_TYPE_STRUCTURAL_EQUALITY (result);
3674 /* Determine which parameter packs will be expanded. */
3675 ppd.parameter_packs = &parameter_packs;
3676 ppd.visited = new hash_set<tree>;
3677 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3678 delete ppd.visited;
3680 /* Make sure we found some parameter packs. */
3681 if (parameter_packs == NULL_TREE)
3683 if (TYPE_P (arg))
3684 error ("expansion pattern %<%T%> contains no argument packs", arg);
3685 else
3686 error ("expansion pattern %<%E%> contains no argument packs", arg);
3687 return error_mark_node;
3689 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3691 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3693 return result;
3696 /* Checks T for any "bare" parameter packs, which have not yet been
3697 expanded, and issues an error if any are found. This operation can
3698 only be done on full expressions or types (e.g., an expression
3699 statement, "if" condition, etc.), because we could have expressions like:
3701 foo(f(g(h(args)))...)
3703 where "args" is a parameter pack. check_for_bare_parameter_packs
3704 should not be called for the subexpressions args, h(args),
3705 g(h(args)), or f(g(h(args))), because we would produce erroneous
3706 error messages.
3708 Returns TRUE and emits an error if there were bare parameter packs,
3709 returns FALSE otherwise. */
3710 bool
3711 check_for_bare_parameter_packs (tree t)
3713 tree parameter_packs = NULL_TREE;
3714 struct find_parameter_pack_data ppd;
3716 if (!processing_template_decl || !t || t == error_mark_node)
3717 return false;
3719 if (TREE_CODE (t) == TYPE_DECL)
3720 t = TREE_TYPE (t);
3722 ppd.parameter_packs = &parameter_packs;
3723 ppd.visited = new hash_set<tree>;
3724 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3725 delete ppd.visited;
3727 if (parameter_packs)
3729 error ("parameter packs not expanded with %<...%>:");
3730 while (parameter_packs)
3732 tree pack = TREE_VALUE (parameter_packs);
3733 tree name = NULL_TREE;
3735 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3736 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3737 name = TYPE_NAME (pack);
3738 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3739 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3740 else
3741 name = DECL_NAME (pack);
3743 if (name)
3744 inform (input_location, " %qD", name);
3745 else
3746 inform (input_location, " <anonymous>");
3748 parameter_packs = TREE_CHAIN (parameter_packs);
3751 return true;
3754 return false;
3757 /* Expand any parameter packs that occur in the template arguments in
3758 ARGS. */
3759 tree
3760 expand_template_argument_pack (tree args)
3762 tree result_args = NULL_TREE;
3763 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3764 int num_result_args = -1;
3765 int non_default_args_count = -1;
3767 /* First, determine if we need to expand anything, and the number of
3768 slots we'll need. */
3769 for (in_arg = 0; in_arg < nargs; ++in_arg)
3771 tree arg = TREE_VEC_ELT (args, in_arg);
3772 if (arg == NULL_TREE)
3773 return args;
3774 if (ARGUMENT_PACK_P (arg))
3776 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3777 if (num_result_args < 0)
3778 num_result_args = in_arg + num_packed;
3779 else
3780 num_result_args += num_packed;
3782 else
3784 if (num_result_args >= 0)
3785 num_result_args++;
3789 /* If no expansion is necessary, we're done. */
3790 if (num_result_args < 0)
3791 return args;
3793 /* Expand arguments. */
3794 result_args = make_tree_vec (num_result_args);
3795 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3796 non_default_args_count =
3797 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3798 for (in_arg = 0; in_arg < nargs; ++in_arg)
3800 tree arg = TREE_VEC_ELT (args, in_arg);
3801 if (ARGUMENT_PACK_P (arg))
3803 tree packed = ARGUMENT_PACK_ARGS (arg);
3804 int i, num_packed = TREE_VEC_LENGTH (packed);
3805 for (i = 0; i < num_packed; ++i, ++out_arg)
3806 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3807 if (non_default_args_count > 0)
3808 non_default_args_count += num_packed - 1;
3810 else
3812 TREE_VEC_ELT (result_args, out_arg) = arg;
3813 ++out_arg;
3816 if (non_default_args_count >= 0)
3817 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3818 return result_args;
3821 /* Checks if DECL shadows a template parameter.
3823 [temp.local]: A template-parameter shall not be redeclared within its
3824 scope (including nested scopes).
3826 Emits an error and returns TRUE if the DECL shadows a parameter,
3827 returns FALSE otherwise. */
3829 bool
3830 check_template_shadow (tree decl)
3832 tree olddecl;
3834 /* If we're not in a template, we can't possibly shadow a template
3835 parameter. */
3836 if (!current_template_parms)
3837 return true;
3839 /* Figure out what we're shadowing. */
3840 if (TREE_CODE (decl) == OVERLOAD)
3841 decl = OVL_CURRENT (decl);
3842 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3844 /* If there's no previous binding for this name, we're not shadowing
3845 anything, let alone a template parameter. */
3846 if (!olddecl)
3847 return true;
3849 /* If we're not shadowing a template parameter, we're done. Note
3850 that OLDDECL might be an OVERLOAD (or perhaps even an
3851 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3852 node. */
3853 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3854 return true;
3856 /* We check for decl != olddecl to avoid bogus errors for using a
3857 name inside a class. We check TPFI to avoid duplicate errors for
3858 inline member templates. */
3859 if (decl == olddecl
3860 || (DECL_TEMPLATE_PARM_P (decl)
3861 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3862 return true;
3864 /* Don't complain about the injected class name, as we've already
3865 complained about the class itself. */
3866 if (DECL_SELF_REFERENCE_P (decl))
3867 return false;
3869 error ("declaration of %q+#D", decl);
3870 error (" shadows template parm %q+#D", olddecl);
3871 return false;
3874 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3875 ORIG_LEVEL, DECL, and TYPE. */
3877 static tree
3878 build_template_parm_index (int index,
3879 int level,
3880 int orig_level,
3881 tree decl,
3882 tree type)
3884 tree t = make_node (TEMPLATE_PARM_INDEX);
3885 TEMPLATE_PARM_IDX (t) = index;
3886 TEMPLATE_PARM_LEVEL (t) = level;
3887 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3888 TEMPLATE_PARM_DECL (t) = decl;
3889 TREE_TYPE (t) = type;
3890 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3891 TREE_READONLY (t) = TREE_READONLY (decl);
3893 return t;
3896 /* Find the canonical type parameter for the given template type
3897 parameter. Returns the canonical type parameter, which may be TYPE
3898 if no such parameter existed. */
3900 static tree
3901 canonical_type_parameter (tree type)
3903 tree list;
3904 int idx = TEMPLATE_TYPE_IDX (type);
3905 if (!canonical_template_parms)
3906 vec_alloc (canonical_template_parms, idx+1);
3908 while (canonical_template_parms->length () <= (unsigned)idx)
3909 vec_safe_push (canonical_template_parms, NULL_TREE);
3911 list = (*canonical_template_parms)[idx];
3912 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3913 list = TREE_CHAIN (list);
3915 if (list)
3916 return TREE_VALUE (list);
3917 else
3919 (*canonical_template_parms)[idx]
3920 = tree_cons (NULL_TREE, type,
3921 (*canonical_template_parms)[idx]);
3922 return type;
3926 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3927 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3928 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3929 new one is created. */
3931 static tree
3932 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3933 tsubst_flags_t complain)
3935 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3936 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3937 != TEMPLATE_PARM_LEVEL (index) - levels)
3938 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3940 tree orig_decl = TEMPLATE_PARM_DECL (index);
3941 tree decl, t;
3943 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3944 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3945 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3946 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3947 DECL_ARTIFICIAL (decl) = 1;
3948 SET_DECL_TEMPLATE_PARM_P (decl);
3950 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3951 TEMPLATE_PARM_LEVEL (index) - levels,
3952 TEMPLATE_PARM_ORIG_LEVEL (index),
3953 decl, type);
3954 TEMPLATE_PARM_DESCENDANTS (index) = t;
3955 TEMPLATE_PARM_PARAMETER_PACK (t)
3956 = TEMPLATE_PARM_PARAMETER_PACK (index);
3958 /* Template template parameters need this. */
3959 if (TREE_CODE (decl) == TEMPLATE_DECL)
3960 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3961 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3962 args, complain);
3965 return TEMPLATE_PARM_DESCENDANTS (index);
3968 /* Process information from new template parameter PARM and append it
3969 to the LIST being built. This new parameter is a non-type
3970 parameter iff IS_NON_TYPE is true. This new parameter is a
3971 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3972 is in PARM_LOC. */
3974 tree
3975 process_template_parm (tree list, location_t parm_loc, tree parm,
3976 bool is_non_type, bool is_parameter_pack)
3978 tree decl = 0;
3979 int idx = 0;
3981 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3982 tree defval = TREE_PURPOSE (parm);
3983 tree constr = TREE_TYPE (parm);
3984 tree reqs = NULL_TREE;
3986 if (list)
3988 tree p = tree_last (list);
3990 if (p && TREE_VALUE (p) != error_mark_node)
3992 p = TREE_VALUE (p);
3993 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3994 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3995 else
3996 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3999 ++idx;
4002 if (is_non_type)
4004 parm = TREE_VALUE (parm);
4006 SET_DECL_TEMPLATE_PARM_P (parm);
4008 if (TREE_TYPE (parm) != error_mark_node)
4010 /* [temp.param]
4012 The top-level cv-qualifiers on the template-parameter are
4013 ignored when determining its type. */
4014 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4015 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4016 TREE_TYPE (parm) = error_mark_node;
4017 else if (uses_parameter_packs (TREE_TYPE (parm))
4018 && !is_parameter_pack
4019 /* If we're in a nested template parameter list, the template
4020 template parameter could be a parameter pack. */
4021 && processing_template_parmlist == 1)
4023 /* This template parameter is not a parameter pack, but it
4024 should be. Complain about "bare" parameter packs. */
4025 check_for_bare_parameter_packs (TREE_TYPE (parm));
4027 /* Recover by calling this a parameter pack. */
4028 is_parameter_pack = true;
4032 /* A template parameter is not modifiable. */
4033 TREE_CONSTANT (parm) = 1;
4034 TREE_READONLY (parm) = 1;
4035 decl = build_decl (parm_loc,
4036 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4037 TREE_CONSTANT (decl) = 1;
4038 TREE_READONLY (decl) = 1;
4039 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4040 = build_template_parm_index (idx, processing_template_decl,
4041 processing_template_decl,
4042 decl, TREE_TYPE (parm));
4044 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4045 = is_parameter_pack;
4047 else
4049 tree t;
4050 parm = TREE_VALUE (TREE_VALUE (parm));
4052 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4054 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4055 /* This is for distinguishing between real templates and template
4056 template parameters */
4057 TREE_TYPE (parm) = t;
4058 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4059 decl = parm;
4061 else
4063 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4064 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4065 decl = build_decl (parm_loc,
4066 TYPE_DECL, parm, t);
4069 TYPE_NAME (t) = decl;
4070 TYPE_STUB_DECL (t) = decl;
4071 parm = decl;
4072 TEMPLATE_TYPE_PARM_INDEX (t)
4073 = build_template_parm_index (idx, processing_template_decl,
4074 processing_template_decl,
4075 decl, TREE_TYPE (parm));
4076 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4077 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4079 DECL_ARTIFICIAL (decl) = 1;
4080 SET_DECL_TEMPLATE_PARM_P (decl);
4082 // Build requirements for the type/template parameter. This must be done
4083 // after SET_DECL_TEMPLATE_PARM_P or process_template_parm could fail.
4084 reqs = finish_shorthand_constraint (parm, constr);
4086 pushdecl (decl);
4088 // Build the parameter node linking the parameter declaration, its
4089 // default argument (if any), and its constraints (if any).
4090 parm = build_tree_list (defval, parm);
4091 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4093 return chainon (list, parm);
4096 /* The end of a template parameter list has been reached. Process the
4097 tree list into a parameter vector, converting each parameter into a more
4098 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4099 as PARM_DECLs. */
4101 tree
4102 end_template_parm_list (tree parms)
4104 int nparms;
4105 tree parm, next;
4106 tree saved_parmlist = make_tree_vec (list_length (parms));
4108 current_template_parms
4109 = tree_cons (size_int (processing_template_decl),
4110 saved_parmlist, current_template_parms);
4112 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4114 next = TREE_CHAIN (parm);
4115 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4116 TREE_CHAIN (parm) = NULL_TREE;
4119 --processing_template_parmlist;
4121 return saved_parmlist;
4124 // Explicitly indicate the end of the template parameter list. We assume
4125 // that the current template parameters have been constructed and/or
4126 // managed explicitly, as when creating new template template parameters
4127 // from a shorthand constraint.
4128 void
4129 end_template_parm_list ()
4131 --processing_template_parmlist;
4134 /* end_template_decl is called after a template declaration is seen. */
4136 void
4137 end_template_decl (void)
4139 reset_specialization ();
4141 if (! processing_template_decl)
4142 return;
4144 /* This matches the pushlevel in begin_template_parm_list. */
4145 finish_scope ();
4147 --processing_template_decl;
4148 current_template_parms = TREE_CHAIN (current_template_parms);
4151 /* Takes a TREE_LIST representing a template parameter and convert it
4152 into an argument suitable to be passed to the type substitution
4153 functions. Note that If the TREE_LIST contains an error_mark
4154 node, the returned argument is error_mark_node. */
4156 tree
4157 template_parm_to_arg (tree t)
4160 if (t == NULL_TREE
4161 || TREE_CODE (t) != TREE_LIST)
4162 return t;
4164 if (error_operand_p (TREE_VALUE (t)))
4165 return error_mark_node;
4167 t = TREE_VALUE (t);
4169 if (TREE_CODE (t) == TYPE_DECL
4170 || TREE_CODE (t) == TEMPLATE_DECL)
4172 t = TREE_TYPE (t);
4174 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4176 /* Turn this argument into a TYPE_ARGUMENT_PACK
4177 with a single element, which expands T. */
4178 tree vec = make_tree_vec (1);
4179 #ifdef ENABLE_CHECKING
4180 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
4181 (vec, TREE_VEC_LENGTH (vec));
4182 #endif
4183 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4185 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4186 SET_ARGUMENT_PACK_ARGS (t, vec);
4189 else
4191 t = DECL_INITIAL (t);
4193 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4195 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4196 with a single element, which expands T. */
4197 tree vec = make_tree_vec (1);
4198 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4199 #ifdef ENABLE_CHECKING
4200 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
4201 (vec, TREE_VEC_LENGTH (vec));
4202 #endif
4203 t = convert_from_reference (t);
4204 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4206 t = make_node (NONTYPE_ARGUMENT_PACK);
4207 SET_ARGUMENT_PACK_ARGS (t, vec);
4208 TREE_TYPE (t) = type;
4210 else
4211 t = convert_from_reference (t);
4213 return t;
4216 /* Given a set of template parameters, return them as a set of template
4217 arguments. The template parameters are represented as a TREE_VEC, in
4218 the form documented in cp-tree.h for template arguments. */
4220 static tree
4221 template_parms_to_args (tree parms)
4223 tree header;
4224 tree args = NULL_TREE;
4225 int length = TMPL_PARMS_DEPTH (parms);
4226 int l = length;
4228 /* If there is only one level of template parameters, we do not
4229 create a TREE_VEC of TREE_VECs. Instead, we return a single
4230 TREE_VEC containing the arguments. */
4231 if (length > 1)
4232 args = make_tree_vec (length);
4234 for (header = parms; header; header = TREE_CHAIN (header))
4236 tree a = copy_node (TREE_VALUE (header));
4237 int i;
4239 TREE_TYPE (a) = NULL_TREE;
4240 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4241 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4243 #ifdef ENABLE_CHECKING
4244 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4245 #endif
4247 if (length > 1)
4248 TREE_VEC_ELT (args, --l) = a;
4249 else
4250 args = a;
4253 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
4254 /* This can happen for template parms of a template template
4255 parameter, e.g:
4257 template<template<class T, class U> class TT> struct S;
4259 Consider the level of the parms of TT; T and U both have
4260 level 2; TT has no template parm of level 1. So in this case
4261 the first element of full_template_args is NULL_TREE. If we
4262 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
4263 of 2. This will make tsubst wrongly consider that T and U
4264 have level 1. Instead, let's create a dummy vector as the
4265 first element of full_template_args so that TMPL_ARGS_DEPTH
4266 returns the correct depth for args. */
4267 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
4268 return args;
4271 /* Within the declaration of a template, return the currently active
4272 template parameters as an argument TREE_VEC. */
4274 static tree
4275 current_template_args (void)
4277 return template_parms_to_args (current_template_parms);
4280 /* Update the declared TYPE by doing any lookups which were thought to be
4281 dependent, but are not now that we know the SCOPE of the declarator. */
4283 tree
4284 maybe_update_decl_type (tree orig_type, tree scope)
4286 tree type = orig_type;
4288 if (type == NULL_TREE)
4289 return type;
4291 if (TREE_CODE (orig_type) == TYPE_DECL)
4292 type = TREE_TYPE (type);
4294 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4295 && dependent_type_p (type)
4296 /* Don't bother building up the args in this case. */
4297 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4299 /* tsubst in the args corresponding to the template parameters,
4300 including auto if present. Most things will be unchanged, but
4301 make_typename_type and tsubst_qualified_id will resolve
4302 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4303 tree args = current_template_args ();
4304 tree auto_node = type_uses_auto (type);
4305 tree pushed;
4306 if (auto_node)
4308 tree auto_vec = make_tree_vec (1);
4309 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4310 args = add_to_template_args (args, auto_vec);
4312 pushed = push_scope (scope);
4313 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4314 if (pushed)
4315 pop_scope (scope);
4318 if (type == error_mark_node)
4319 return orig_type;
4321 if (TREE_CODE (orig_type) == TYPE_DECL)
4323 if (same_type_p (type, TREE_TYPE (orig_type)))
4324 type = orig_type;
4325 else
4326 type = TYPE_NAME (type);
4328 return type;
4331 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4332 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4333 the new template is a member template. */
4335 tree
4336 build_template_decl (tree decl, tree parms, bool member_template_p)
4338 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4339 DECL_TEMPLATE_PARMS (tmpl) = parms;
4340 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4341 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4342 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4344 return tmpl;
4347 struct template_parm_data
4349 /* The level of the template parameters we are currently
4350 processing. */
4351 int level;
4353 /* The index of the specialization argument we are currently
4354 processing. */
4355 int current_arg;
4357 /* An array whose size is the number of template parameters. The
4358 elements are nonzero if the parameter has been used in any one
4359 of the arguments processed so far. */
4360 int* parms;
4362 /* An array whose size is the number of template arguments. The
4363 elements are nonzero if the argument makes use of template
4364 parameters of this level. */
4365 int* arg_uses_template_parms;
4368 /* Subroutine of push_template_decl used to see if each template
4369 parameter in a partial specialization is used in the explicit
4370 argument list. If T is of the LEVEL given in DATA (which is
4371 treated as a template_parm_data*), then DATA->PARMS is marked
4372 appropriately. */
4374 static int
4375 mark_template_parm (tree t, void* data)
4377 int level;
4378 int idx;
4379 struct template_parm_data* tpd = (struct template_parm_data*) data;
4381 template_parm_level_and_index (t, &level, &idx);
4383 if (level == tpd->level)
4385 tpd->parms[idx] = 1;
4386 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4389 /* Return zero so that for_each_template_parm will continue the
4390 traversal of the tree; we want to mark *every* template parm. */
4391 return 0;
4394 /* Process the partial specialization DECL. */
4396 static tree
4397 process_partial_specialization (tree decl)
4399 tree type = TREE_TYPE (decl);
4400 tree tinfo = get_template_info (decl);
4401 tree maintmpl = TI_TEMPLATE (tinfo);
4402 tree specargs = TI_ARGS (tinfo);
4403 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4404 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4405 tree inner_parms;
4406 tree inst;
4407 int nargs = TREE_VEC_LENGTH (inner_args);
4408 int ntparms;
4409 int i;
4410 bool did_error_intro = false;
4411 struct template_parm_data tpd;
4412 struct template_parm_data tpd2;
4414 gcc_assert (current_template_parms);
4416 /* A concept cannot be specialized. */
4417 if (flag_concepts && variable_concept_p (maintmpl))
4419 error ("specialization of variable concept %q#D", maintmpl);
4420 return error_mark_node;
4423 /* When defining a constrained partial specialization, DECL's type
4424 will have been assigned to the canonical type of the primary.
4425 That is:
4427 template<typename T>
4428 struct S; // Has canonical type S<T>
4430 template<C T>
4431 struct S<T>; // Binds to the primary template.
4433 However, the constraints differ, so we should be constructing
4434 a new type instead of simply referring to the old one. We
4435 do this by erasing the canonical type of the partial
4436 specialization and always comparing these types structurally.
4438 TODO: Do we need to compare the current requirements to make
4439 sure this isn't a redeclaration? */
4440 if (TEMPLATE_PARM_CONSTRAINTS (current_template_parms))
4441 SET_TYPE_STRUCTURAL_EQUALITY (type);
4443 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4444 ntparms = TREE_VEC_LENGTH (inner_parms);
4446 /* We check that each of the template parameters given in the
4447 partial specialization is used in the argument list to the
4448 specialization. For example:
4450 template <class T> struct S;
4451 template <class T> struct S<T*>;
4453 The second declaration is OK because `T*' uses the template
4454 parameter T, whereas
4456 template <class T> struct S<int>;
4458 is no good. Even trickier is:
4460 template <class T>
4461 struct S1
4463 template <class U>
4464 struct S2;
4465 template <class U>
4466 struct S2<T>;
4469 The S2<T> declaration is actually invalid; it is a
4470 full-specialization. Of course,
4472 template <class U>
4473 struct S2<T (*)(U)>;
4475 or some such would have been OK. */
4476 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4477 tpd.parms = XALLOCAVEC (int, ntparms);
4478 memset (tpd.parms, 0, sizeof (int) * ntparms);
4480 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4481 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4482 for (i = 0; i < nargs; ++i)
4484 tpd.current_arg = i;
4485 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4486 &mark_template_parm,
4487 &tpd,
4488 NULL,
4489 /*include_nondeduced_p=*/false);
4491 for (i = 0; i < ntparms; ++i)
4492 if (tpd.parms[i] == 0)
4494 /* One of the template parms was not used in a deduced context in the
4495 specialization. */
4496 if (!did_error_intro)
4498 error ("template parameters not deducible in "
4499 "partial specialization:");
4500 did_error_intro = true;
4503 inform (input_location, " %qD",
4504 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4507 if (did_error_intro)
4508 return error_mark_node;
4510 /* Build the associated constraints now so we can compare
4511 and assign them below. */
4512 tree tmpl_constr;
4513 if (flag_concepts)
4515 tree reqs = TEMPLATE_PARM_CONSTRAINTS (current_template_parms);
4516 tmpl_constr = build_constraints (reqs, NULL_TREE);
4518 else
4519 tmpl_constr = NULL_TREE;
4521 /* [temp.class.spec]
4523 The argument list of the specialization shall not be identical to
4524 the implicit argument list of the primary template. */
4525 tree main_args
4526 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4527 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args)))
4529 if (!flag_concepts)
4530 error ("partial specialization %qD does not specialize "
4531 "any template arguments", decl);
4532 else
4534 /* If the arguments compare equal but are not not more
4535 constrained, then this will not be more specialized. */
4536 if (!subsumes_constraints (tmpl_constr, get_constraints (maintmpl)))
4538 error ("partial specialization is not more specialized than the "
4539 "primary template because it is not more constrained");
4540 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4541 return decl;
4546 /* A partial specialization that replaces multiple parameters of the
4547 primary template with a pack expansion is less specialized for those
4548 parameters. */
4549 if (nargs < DECL_NTPARMS (maintmpl))
4551 error ("partial specialization is not more specialized than the "
4552 "primary template because it replaces multiple parameters "
4553 "with a pack expansion");
4554 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4555 return decl;
4558 /* [temp.class.spec]
4560 A partially specialized non-type argument expression shall not
4561 involve template parameters of the partial specialization except
4562 when the argument expression is a simple identifier.
4564 The type of a template parameter corresponding to a specialized
4565 non-type argument shall not be dependent on a parameter of the
4566 specialization.
4568 Also, we verify that pack expansions only occur at the
4569 end of the argument list. */
4570 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4571 tpd2.parms = 0;
4572 for (i = 0; i < nargs; ++i)
4574 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4575 tree arg = TREE_VEC_ELT (inner_args, i);
4576 tree packed_args = NULL_TREE;
4577 int j, len = 1;
4579 if (ARGUMENT_PACK_P (arg))
4581 /* Extract the arguments from the argument pack. We'll be
4582 iterating over these in the following loop. */
4583 packed_args = ARGUMENT_PACK_ARGS (arg);
4584 len = TREE_VEC_LENGTH (packed_args);
4587 for (j = 0; j < len; j++)
4589 if (packed_args)
4590 /* Get the Jth argument in the parameter pack. */
4591 arg = TREE_VEC_ELT (packed_args, j);
4593 if (PACK_EXPANSION_P (arg))
4595 /* Pack expansions must come at the end of the
4596 argument list. */
4597 if ((packed_args && j < len - 1)
4598 || (!packed_args && i < nargs - 1))
4600 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4601 error ("parameter pack argument %qE must be at the "
4602 "end of the template argument list", arg);
4603 else
4604 error ("parameter pack argument %qT must be at the "
4605 "end of the template argument list", arg);
4609 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4610 /* We only care about the pattern. */
4611 arg = PACK_EXPANSION_PATTERN (arg);
4613 if (/* These first two lines are the `non-type' bit. */
4614 !TYPE_P (arg)
4615 && TREE_CODE (arg) != TEMPLATE_DECL
4616 /* This next two lines are the `argument expression is not just a
4617 simple identifier' condition and also the `specialized
4618 non-type argument' bit. */
4619 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4620 && !(REFERENCE_REF_P (arg)
4621 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4623 if ((!packed_args && tpd.arg_uses_template_parms[i])
4624 || (packed_args && uses_template_parms (arg)))
4625 error ("template argument %qE involves template parameter(s)",
4626 arg);
4627 else
4629 /* Look at the corresponding template parameter,
4630 marking which template parameters its type depends
4631 upon. */
4632 tree type = TREE_TYPE (parm);
4634 if (!tpd2.parms)
4636 /* We haven't yet initialized TPD2. Do so now. */
4637 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4638 /* The number of parameters here is the number in the
4639 main template, which, as checked in the assertion
4640 above, is NARGS. */
4641 tpd2.parms = XALLOCAVEC (int, nargs);
4642 tpd2.level =
4643 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4646 /* Mark the template parameters. But this time, we're
4647 looking for the template parameters of the main
4648 template, not in the specialization. */
4649 tpd2.current_arg = i;
4650 tpd2.arg_uses_template_parms[i] = 0;
4651 memset (tpd2.parms, 0, sizeof (int) * nargs);
4652 for_each_template_parm (type,
4653 &mark_template_parm,
4654 &tpd2,
4655 NULL,
4656 /*include_nondeduced_p=*/false);
4658 if (tpd2.arg_uses_template_parms [i])
4660 /* The type depended on some template parameters.
4661 If they are fully specialized in the
4662 specialization, that's OK. */
4663 int j;
4664 int count = 0;
4665 for (j = 0; j < nargs; ++j)
4666 if (tpd2.parms[j] != 0
4667 && tpd.arg_uses_template_parms [j])
4668 ++count;
4669 if (count != 0)
4670 error_n (input_location, count,
4671 "type %qT of template argument %qE depends "
4672 "on a template parameter",
4673 "type %qT of template argument %qE depends "
4674 "on template parameters",
4675 type,
4676 arg);
4683 /* We should only get here once. */
4684 if (TREE_CODE (decl) == TYPE_DECL)
4685 gcc_assert (!COMPLETE_TYPE_P (type));
4687 // Build the template decl.
4688 tree tmpl = build_template_decl (decl,
4689 current_template_parms,
4690 DECL_MEMBER_TEMPLATE_P (maintmpl));
4691 TREE_TYPE (tmpl) = type;
4692 DECL_TEMPLATE_RESULT (tmpl) = decl;
4693 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4694 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4695 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4696 set_constraints (tmpl, tmpl_constr);
4698 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4699 = tree_cons (specargs, tmpl,
4700 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4701 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4703 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4704 inst = TREE_CHAIN (inst))
4706 tree instance = TREE_VALUE (inst);
4707 if (TYPE_P (instance)
4708 ? (COMPLETE_TYPE_P (instance)
4709 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4710 : DECL_TEMPLATE_INSTANTIATION (instance))
4712 tree spec = most_specialized_partial_spec (instance, tf_none);
4713 if (spec && TREE_VALUE (spec) == tmpl)
4715 tree inst_decl = (DECL_P (instance)
4716 ? instance : TYPE_NAME (instance));
4717 permerror (input_location,
4718 "partial specialization of %qD after instantiation "
4719 "of %qD", decl, inst_decl);
4724 return decl;
4727 /* PARM is a template parameter of some form; return the corresponding
4728 TEMPLATE_PARM_INDEX. */
4730 static tree
4731 get_template_parm_index (tree parm)
4733 if (TREE_CODE (parm) == PARM_DECL
4734 || TREE_CODE (parm) == CONST_DECL)
4735 parm = DECL_INITIAL (parm);
4736 else if (TREE_CODE (parm) == TYPE_DECL
4737 || TREE_CODE (parm) == TEMPLATE_DECL)
4738 parm = TREE_TYPE (parm);
4739 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4740 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4741 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4742 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4743 return parm;
4746 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4747 parameter packs used by the template parameter PARM. */
4749 static void
4750 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4752 /* A type parm can't refer to another parm. */
4753 if (TREE_CODE (parm) == TYPE_DECL)
4754 return;
4755 else if (TREE_CODE (parm) == PARM_DECL)
4757 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4758 ppd, ppd->visited);
4759 return;
4762 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4764 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4765 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4766 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4769 /* PARM is a template parameter pack. Return any parameter packs used in
4770 its type or the type of any of its template parameters. If there are
4771 any such packs, it will be instantiated into a fixed template parameter
4772 list by partial instantiation rather than be fully deduced. */
4774 tree
4775 fixed_parameter_pack_p (tree parm)
4777 /* This can only be true in a member template. */
4778 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4779 return NULL_TREE;
4780 /* This can only be true for a parameter pack. */
4781 if (!template_parameter_pack_p (parm))
4782 return NULL_TREE;
4783 /* A type parm can't refer to another parm. */
4784 if (TREE_CODE (parm) == TYPE_DECL)
4785 return NULL_TREE;
4787 tree parameter_packs = NULL_TREE;
4788 struct find_parameter_pack_data ppd;
4789 ppd.parameter_packs = &parameter_packs;
4790 ppd.visited = new hash_set<tree>;
4792 fixed_parameter_pack_p_1 (parm, &ppd);
4794 delete ppd.visited;
4795 return parameter_packs;
4798 /* Check that a template declaration's use of default arguments and
4799 parameter packs is not invalid. Here, PARMS are the template
4800 parameters. IS_PRIMARY is true if DECL is the thing declared by
4801 a primary template. IS_PARTIAL is true if DECL is a partial
4802 specialization.
4804 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4805 declaration (but not a definition); 1 indicates a declaration, 2
4806 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4807 emitted for extraneous default arguments.
4809 Returns TRUE if there were no errors found, FALSE otherwise. */
4811 bool
4812 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4813 bool is_partial, int is_friend_decl)
4815 const char *msg;
4816 int last_level_to_check;
4817 tree parm_level;
4818 bool no_errors = true;
4820 /* [temp.param]
4822 A default template-argument shall not be specified in a
4823 function template declaration or a function template definition, nor
4824 in the template-parameter-list of the definition of a member of a
4825 class template. */
4827 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4828 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4829 /* You can't have a function template declaration in a local
4830 scope, nor you can you define a member of a class template in a
4831 local scope. */
4832 return true;
4834 if ((TREE_CODE (decl) == TYPE_DECL
4835 && TREE_TYPE (decl)
4836 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4837 || (TREE_CODE (decl) == FUNCTION_DECL
4838 && LAMBDA_FUNCTION_P (decl)))
4839 /* A lambda doesn't have an explicit declaration; don't complain
4840 about the parms of the enclosing class. */
4841 return true;
4843 if (current_class_type
4844 && !TYPE_BEING_DEFINED (current_class_type)
4845 && DECL_LANG_SPECIFIC (decl)
4846 && DECL_DECLARES_FUNCTION_P (decl)
4847 /* If this is either a friend defined in the scope of the class
4848 or a member function. */
4849 && (DECL_FUNCTION_MEMBER_P (decl)
4850 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4851 : DECL_FRIEND_CONTEXT (decl)
4852 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4853 : false)
4854 /* And, if it was a member function, it really was defined in
4855 the scope of the class. */
4856 && (!DECL_FUNCTION_MEMBER_P (decl)
4857 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4858 /* We already checked these parameters when the template was
4859 declared, so there's no need to do it again now. This function
4860 was defined in class scope, but we're processing its body now
4861 that the class is complete. */
4862 return true;
4864 /* Core issue 226 (C++0x only): the following only applies to class
4865 templates. */
4866 if (is_primary
4867 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4869 /* [temp.param]
4871 If a template-parameter has a default template-argument, all
4872 subsequent template-parameters shall have a default
4873 template-argument supplied. */
4874 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4876 tree inner_parms = TREE_VALUE (parm_level);
4877 int ntparms = TREE_VEC_LENGTH (inner_parms);
4878 int seen_def_arg_p = 0;
4879 int i;
4881 for (i = 0; i < ntparms; ++i)
4883 tree parm = TREE_VEC_ELT (inner_parms, i);
4885 if (parm == error_mark_node)
4886 continue;
4888 if (TREE_PURPOSE (parm))
4889 seen_def_arg_p = 1;
4890 else if (seen_def_arg_p
4891 && !template_parameter_pack_p (TREE_VALUE (parm)))
4893 error ("no default argument for %qD", TREE_VALUE (parm));
4894 /* For better subsequent error-recovery, we indicate that
4895 there should have been a default argument. */
4896 TREE_PURPOSE (parm) = error_mark_node;
4897 no_errors = false;
4899 else if (!is_partial
4900 && !is_friend_decl
4901 /* Don't complain about an enclosing partial
4902 specialization. */
4903 && parm_level == parms
4904 && TREE_CODE (decl) == TYPE_DECL
4905 && i < ntparms - 1
4906 && template_parameter_pack_p (TREE_VALUE (parm))
4907 /* A fixed parameter pack will be partially
4908 instantiated into a fixed length list. */
4909 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4911 /* A primary class template can only have one
4912 parameter pack, at the end of the template
4913 parameter list. */
4915 error ("parameter pack %q+D must be at the end of the"
4916 " template parameter list", TREE_VALUE (parm));
4918 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4919 = error_mark_node;
4920 no_errors = false;
4926 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4927 || is_partial
4928 || !is_primary
4929 || is_friend_decl)
4930 /* For an ordinary class template, default template arguments are
4931 allowed at the innermost level, e.g.:
4932 template <class T = int>
4933 struct S {};
4934 but, in a partial specialization, they're not allowed even
4935 there, as we have in [temp.class.spec]:
4937 The template parameter list of a specialization shall not
4938 contain default template argument values.
4940 So, for a partial specialization, or for a function template
4941 (in C++98/C++03), we look at all of them. */
4943 else
4944 /* But, for a primary class template that is not a partial
4945 specialization we look at all template parameters except the
4946 innermost ones. */
4947 parms = TREE_CHAIN (parms);
4949 /* Figure out what error message to issue. */
4950 if (is_friend_decl == 2)
4951 msg = G_("default template arguments may not be used in function template "
4952 "friend re-declaration");
4953 else if (is_friend_decl)
4954 msg = G_("default template arguments may not be used in function template "
4955 "friend declarations");
4956 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4957 msg = G_("default template arguments may not be used in function templates "
4958 "without -std=c++11 or -std=gnu++11");
4959 else if (is_partial)
4960 msg = G_("default template arguments may not be used in "
4961 "partial specializations");
4962 else
4963 msg = G_("default argument for template parameter for class enclosing %qD");
4965 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4966 /* If we're inside a class definition, there's no need to
4967 examine the parameters to the class itself. On the one
4968 hand, they will be checked when the class is defined, and,
4969 on the other, default arguments are valid in things like:
4970 template <class T = double>
4971 struct S { template <class U> void f(U); };
4972 Here the default argument for `S' has no bearing on the
4973 declaration of `f'. */
4974 last_level_to_check = template_class_depth (current_class_type) + 1;
4975 else
4976 /* Check everything. */
4977 last_level_to_check = 0;
4979 for (parm_level = parms;
4980 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4981 parm_level = TREE_CHAIN (parm_level))
4983 tree inner_parms = TREE_VALUE (parm_level);
4984 int i;
4985 int ntparms;
4987 ntparms = TREE_VEC_LENGTH (inner_parms);
4988 for (i = 0; i < ntparms; ++i)
4990 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4991 continue;
4993 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4995 if (msg)
4997 no_errors = false;
4998 if (is_friend_decl == 2)
4999 return no_errors;
5001 error (msg, decl);
5002 msg = 0;
5005 /* Clear out the default argument so that we are not
5006 confused later. */
5007 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5011 /* At this point, if we're still interested in issuing messages,
5012 they must apply to classes surrounding the object declared. */
5013 if (msg)
5014 msg = G_("default argument for template parameter for class "
5015 "enclosing %qD");
5018 return no_errors;
5021 /* Worker for push_template_decl_real, called via
5022 for_each_template_parm. DATA is really an int, indicating the
5023 level of the parameters we are interested in. If T is a template
5024 parameter of that level, return nonzero. */
5026 static int
5027 template_parm_this_level_p (tree t, void* data)
5029 int this_level = *(int *)data;
5030 int level;
5032 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5033 level = TEMPLATE_PARM_LEVEL (t);
5034 else
5035 level = TEMPLATE_TYPE_LEVEL (t);
5036 return level == this_level;
5039 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5040 parameters given by current_template_args, or reuses a
5041 previously existing one, if appropriate. Returns the DECL, or an
5042 equivalent one, if it is replaced via a call to duplicate_decls.
5044 If IS_FRIEND is true, DECL is a friend declaration. */
5046 tree
5047 push_template_decl_real (tree decl, bool is_friend)
5049 tree tmpl;
5050 tree args;
5051 tree info;
5052 tree ctx;
5053 bool is_primary;
5054 bool is_partial;
5055 int new_template_p = 0;
5056 /* True if the template is a member template, in the sense of
5057 [temp.mem]. */
5058 bool member_template_p = false;
5060 if (decl == error_mark_node || !current_template_parms)
5061 return error_mark_node;
5063 /* See if this is a partial specialization. */
5064 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5065 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5066 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5067 || (TREE_CODE (decl) == VAR_DECL
5068 && DECL_LANG_SPECIFIC (decl)
5069 && DECL_TEMPLATE_SPECIALIZATION (decl)
5070 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5072 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5073 is_friend = true;
5075 if (is_friend)
5076 /* For a friend, we want the context of the friend function, not
5077 the type of which it is a friend. */
5078 ctx = CP_DECL_CONTEXT (decl);
5079 else if (CP_DECL_CONTEXT (decl)
5080 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5081 /* In the case of a virtual function, we want the class in which
5082 it is defined. */
5083 ctx = CP_DECL_CONTEXT (decl);
5084 else
5085 /* Otherwise, if we're currently defining some class, the DECL
5086 is assumed to be a member of the class. */
5087 ctx = current_scope ();
5089 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5090 ctx = NULL_TREE;
5092 if (!DECL_CONTEXT (decl))
5093 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5095 /* See if this is a primary template. */
5096 if (is_friend && ctx
5097 && uses_template_parms_level (ctx, processing_template_decl))
5098 /* A friend template that specifies a class context, i.e.
5099 template <typename T> friend void A<T>::f();
5100 is not primary. */
5101 is_primary = false;
5102 else if (TREE_CODE (decl) == TYPE_DECL
5103 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5104 is_primary = false;
5105 else
5106 is_primary = template_parm_scope_p ();
5108 if (is_primary)
5110 if (DECL_CLASS_SCOPE_P (decl))
5111 member_template_p = true;
5112 if (TREE_CODE (decl) == TYPE_DECL
5113 && ANON_AGGRNAME_P (DECL_NAME (decl)))
5115 error ("template class without a name");
5116 return error_mark_node;
5118 else if (TREE_CODE (decl) == FUNCTION_DECL)
5120 if (member_template_p)
5122 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5123 error ("member template %qD may not have virt-specifiers", decl);
5125 if (DECL_DESTRUCTOR_P (decl))
5127 /* [temp.mem]
5129 A destructor shall not be a member template. */
5130 error ("destructor %qD declared as member template", decl);
5131 return error_mark_node;
5133 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5134 && (!prototype_p (TREE_TYPE (decl))
5135 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5136 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5137 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5138 == void_list_node)))
5140 /* [basic.stc.dynamic.allocation]
5142 An allocation function can be a function
5143 template. ... Template allocation functions shall
5144 have two or more parameters. */
5145 error ("invalid template declaration of %qD", decl);
5146 return error_mark_node;
5149 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5150 && CLASS_TYPE_P (TREE_TYPE (decl)))
5151 /* OK */;
5152 else if (TREE_CODE (decl) == TYPE_DECL
5153 && TYPE_DECL_ALIAS_P (decl))
5154 /* alias-declaration */
5155 gcc_assert (!DECL_ARTIFICIAL (decl));
5156 else if (VAR_P (decl))
5157 /* C++14 variable template. */;
5158 else
5160 error ("template declaration of %q#D", decl);
5161 return error_mark_node;
5165 /* Check to see that the rules regarding the use of default
5166 arguments are not being violated. */
5167 check_default_tmpl_args (decl, current_template_parms,
5168 is_primary, is_partial, /*is_friend_decl=*/0);
5170 /* Ensure that there are no parameter packs in the type of this
5171 declaration that have not been expanded. */
5172 if (TREE_CODE (decl) == FUNCTION_DECL)
5174 /* Check each of the arguments individually to see if there are
5175 any bare parameter packs. */
5176 tree type = TREE_TYPE (decl);
5177 tree arg = DECL_ARGUMENTS (decl);
5178 tree argtype = TYPE_ARG_TYPES (type);
5180 while (arg && argtype)
5182 if (!DECL_PACK_P (arg)
5183 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5185 /* This is a PARM_DECL that contains unexpanded parameter
5186 packs. We have already complained about this in the
5187 check_for_bare_parameter_packs call, so just replace
5188 these types with ERROR_MARK_NODE. */
5189 TREE_TYPE (arg) = error_mark_node;
5190 TREE_VALUE (argtype) = error_mark_node;
5193 arg = DECL_CHAIN (arg);
5194 argtype = TREE_CHAIN (argtype);
5197 /* Check for bare parameter packs in the return type and the
5198 exception specifiers. */
5199 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5200 /* Errors were already issued, set return type to int
5201 as the frontend doesn't expect error_mark_node as
5202 the return type. */
5203 TREE_TYPE (type) = integer_type_node;
5204 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5205 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5207 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5208 && TYPE_DECL_ALIAS_P (decl))
5209 ? DECL_ORIGINAL_TYPE (decl)
5210 : TREE_TYPE (decl)))
5212 TREE_TYPE (decl) = error_mark_node;
5213 return error_mark_node;
5216 if (is_partial)
5217 return process_partial_specialization (decl);
5219 args = current_template_args ();
5221 if (!ctx
5222 || TREE_CODE (ctx) == FUNCTION_DECL
5223 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5224 || (TREE_CODE (decl) == TYPE_DECL
5225 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5226 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5228 if (DECL_LANG_SPECIFIC (decl)
5229 && DECL_TEMPLATE_INFO (decl)
5230 && DECL_TI_TEMPLATE (decl))
5231 tmpl = DECL_TI_TEMPLATE (decl);
5232 /* If DECL is a TYPE_DECL for a class-template, then there won't
5233 be DECL_LANG_SPECIFIC. The information equivalent to
5234 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5235 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5236 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5237 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5239 /* Since a template declaration already existed for this
5240 class-type, we must be redeclaring it here. Make sure
5241 that the redeclaration is valid. */
5242 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
5243 tree constr = build_constraints (reqs, NULL_TREE);
5244 redeclare_class_template (TREE_TYPE (decl),
5245 current_template_parms,
5246 constr);
5247 /* We don't need to create a new TEMPLATE_DECL; just use the
5248 one we already had. */
5249 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5251 else
5253 tmpl = build_template_decl (decl,
5254 current_template_parms,
5255 member_template_p);
5256 new_template_p = 1;
5258 if (DECL_LANG_SPECIFIC (decl)
5259 && DECL_TEMPLATE_SPECIALIZATION (decl))
5261 /* A specialization of a member template of a template
5262 class. */
5263 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5264 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5265 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5269 else
5271 tree a, t, current, parms;
5272 int i;
5273 tree tinfo = get_template_info (decl);
5275 if (!tinfo)
5277 error ("template definition of non-template %q#D", decl);
5278 return error_mark_node;
5281 tmpl = TI_TEMPLATE (tinfo);
5283 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5284 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5285 && DECL_TEMPLATE_SPECIALIZATION (decl)
5286 && DECL_MEMBER_TEMPLATE_P (tmpl))
5288 tree new_tmpl;
5290 /* The declaration is a specialization of a member
5291 template, declared outside the class. Therefore, the
5292 innermost template arguments will be NULL, so we
5293 replace them with the arguments determined by the
5294 earlier call to check_explicit_specialization. */
5295 args = DECL_TI_ARGS (decl);
5297 new_tmpl = build_template_decl (decl,
5298 current_template_parms,
5299 member_template_p);
5300 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5301 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5302 DECL_TI_TEMPLATE (decl) = new_tmpl;
5303 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5304 DECL_TEMPLATE_INFO (new_tmpl) = build_template_info (tmpl, args);
5306 register_specialization (new_tmpl,
5307 most_general_template (tmpl),
5308 args,
5309 is_friend, 0);
5310 return decl;
5313 /* Make sure the template headers we got make sense. */
5315 parms = DECL_TEMPLATE_PARMS (tmpl);
5316 i = TMPL_PARMS_DEPTH (parms);
5317 if (TMPL_ARGS_DEPTH (args) != i)
5319 error ("expected %d levels of template parms for %q#D, got %d",
5320 i, decl, TMPL_ARGS_DEPTH (args));
5321 DECL_INTERFACE_KNOWN (decl) = 1;
5322 return error_mark_node;
5324 else
5325 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5327 a = TMPL_ARGS_LEVEL (args, i);
5328 t = INNERMOST_TEMPLATE_PARMS (parms);
5330 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5332 if (current == decl)
5333 error ("got %d template parameters for %q#D",
5334 TREE_VEC_LENGTH (a), decl);
5335 else
5336 error ("got %d template parameters for %q#T",
5337 TREE_VEC_LENGTH (a), current);
5338 error (" but %d required", TREE_VEC_LENGTH (t));
5339 /* Avoid crash in import_export_decl. */
5340 DECL_INTERFACE_KNOWN (decl) = 1;
5341 return error_mark_node;
5344 if (current == decl)
5345 current = ctx;
5346 else if (current == NULL_TREE)
5347 /* Can happen in erroneous input. */
5348 break;
5349 else
5350 current = get_containing_scope (current);
5353 /* Check that the parms are used in the appropriate qualifying scopes
5354 in the declarator. */
5355 if (!comp_template_args
5356 (TI_ARGS (tinfo),
5357 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5359 error ("\
5360 template arguments to %qD do not match original template %qD",
5361 decl, DECL_TEMPLATE_RESULT (tmpl));
5362 if (!uses_template_parms (TI_ARGS (tinfo)))
5363 inform (input_location, "use template<> for an explicit specialization");
5364 /* Avoid crash in import_export_decl. */
5365 DECL_INTERFACE_KNOWN (decl) = 1;
5366 return error_mark_node;
5370 DECL_TEMPLATE_RESULT (tmpl) = decl;
5371 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5373 /* Push template declarations for global functions and types. Note
5374 that we do not try to push a global template friend declared in a
5375 template class; such a thing may well depend on the template
5376 parameters of the class. */
5377 if (new_template_p && !ctx
5378 && !(is_friend && template_class_depth (current_class_type) > 0))
5380 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5381 if (tmpl == error_mark_node)
5382 return error_mark_node;
5384 /* Hide template friend classes that haven't been declared yet. */
5385 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5387 DECL_ANTICIPATED (tmpl) = 1;
5388 DECL_FRIEND_P (tmpl) = 1;
5392 if (is_primary)
5394 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5395 int i;
5397 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5398 if (DECL_CONV_FN_P (tmpl))
5400 int depth = TMPL_PARMS_DEPTH (parms);
5402 /* It is a conversion operator. See if the type converted to
5403 depends on innermost template operands. */
5405 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5406 depth))
5407 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5410 /* Give template template parms a DECL_CONTEXT of the template
5411 for which they are a parameter. */
5412 parms = INNERMOST_TEMPLATE_PARMS (parms);
5413 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5415 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5416 if (TREE_CODE (parm) == TEMPLATE_DECL)
5417 DECL_CONTEXT (parm) = tmpl;
5421 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5422 back to its most general template. If TMPL is a specialization,
5423 ARGS may only have the innermost set of arguments. Add the missing
5424 argument levels if necessary. */
5425 if (DECL_TEMPLATE_INFO (tmpl))
5426 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5428 info = build_template_info (tmpl, args);
5430 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5431 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5432 else
5434 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5435 retrofit_lang_decl (decl);
5436 if (DECL_LANG_SPECIFIC (decl))
5437 DECL_TEMPLATE_INFO (decl) = info;
5440 if (flag_implicit_templates
5441 && !is_friend
5442 && TREE_PUBLIC (decl)
5443 && VAR_OR_FUNCTION_DECL_P (decl))
5444 /* Set DECL_COMDAT on template instantiations; if we force
5445 them to be emitted by explicit instantiation or -frepo,
5446 mark_needed will tell cgraph to do the right thing. */
5447 DECL_COMDAT (decl) = true;
5449 return DECL_TEMPLATE_RESULT (tmpl);
5452 tree
5453 push_template_decl (tree decl)
5455 return push_template_decl_real (decl, false);
5458 /* FN is an inheriting constructor that inherits from the constructor
5459 template INHERITED; turn FN into a constructor template with a matching
5460 template header. */
5462 tree
5463 add_inherited_template_parms (tree fn, tree inherited)
5465 tree inner_parms
5466 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5467 inner_parms = copy_node (inner_parms);
5468 tree parms
5469 = tree_cons (size_int (processing_template_decl + 1),
5470 inner_parms, current_template_parms);
5471 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5472 tree args = template_parms_to_args (parms);
5474 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5475 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5476 DECL_TEMPLATE_RESULT (tmpl) = fn;
5477 DECL_ARTIFICIAL (tmpl) = true;
5478 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5480 return tmpl;
5483 /* Called when a class template TYPE is redeclared with the indicated
5484 template PARMS, e.g.:
5486 template <class T> struct S;
5487 template <class T> struct S {}; */
5489 bool
5490 redeclare_class_template (tree type, tree parms, tree cons)
5492 tree tmpl;
5493 tree tmpl_parms;
5494 int i;
5496 if (!TYPE_TEMPLATE_INFO (type))
5498 error ("%qT is not a template type", type);
5499 return false;
5502 tmpl = TYPE_TI_TEMPLATE (type);
5503 if (!PRIMARY_TEMPLATE_P (tmpl))
5504 /* The type is nested in some template class. Nothing to worry
5505 about here; there are no new template parameters for the nested
5506 type. */
5507 return true;
5509 if (!parms)
5511 error ("template specifiers not specified in declaration of %qD",
5512 tmpl);
5513 return false;
5516 parms = INNERMOST_TEMPLATE_PARMS (parms);
5517 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5519 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5521 error_n (input_location, TREE_VEC_LENGTH (parms),
5522 "redeclared with %d template parameter",
5523 "redeclared with %d template parameters",
5524 TREE_VEC_LENGTH (parms));
5525 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5526 "previous declaration %q+D used %d template parameter",
5527 "previous declaration %q+D used %d template parameters",
5528 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5529 return false;
5532 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5534 tree tmpl_parm;
5535 tree parm;
5536 tree tmpl_default;
5537 tree parm_default;
5539 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5540 || TREE_VEC_ELT (parms, i) == error_mark_node)
5541 continue;
5543 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5544 if (error_operand_p (tmpl_parm))
5545 return false;
5547 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5548 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5549 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5551 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5552 TEMPLATE_DECL. */
5553 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5554 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5555 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5556 || (TREE_CODE (tmpl_parm) != PARM_DECL
5557 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5558 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5559 || (TREE_CODE (tmpl_parm) == PARM_DECL
5560 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5561 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5563 error ("template parameter %q+#D", tmpl_parm);
5564 error ("redeclared here as %q#D", parm);
5565 return false;
5568 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5570 /* We have in [temp.param]:
5572 A template-parameter may not be given default arguments
5573 by two different declarations in the same scope. */
5574 error_at (input_location, "redefinition of default argument for %q#D", parm);
5575 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5576 "original definition appeared here");
5577 return false;
5580 if (parm_default != NULL_TREE)
5581 /* Update the previous template parameters (which are the ones
5582 that will really count) with the new default value. */
5583 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5584 else if (tmpl_default != NULL_TREE)
5585 /* Update the new parameters, too; they'll be used as the
5586 parameters for any members. */
5587 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5590 // Cannot redeclare a class template with a different set of constraints.
5591 if (!equivalent_constraints (get_constraints (tmpl), cons))
5593 error_at (input_location, "redeclaration %q#D with different "
5594 "constraints", tmpl);
5595 inform (DECL_SOURCE_LOCATION (tmpl),
5596 "original declaration appeared here");
5599 return true;
5602 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5603 to be used when the caller has already checked
5604 (processing_template_decl
5605 && !instantiation_dependent_expression_p (expr)
5606 && potential_constant_expression (expr))
5607 and cleared processing_template_decl. */
5609 tree
5610 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5612 return tsubst_copy_and_build (expr,
5613 /*args=*/NULL_TREE,
5614 complain,
5615 /*in_decl=*/NULL_TREE,
5616 /*function_p=*/false,
5617 /*integral_constant_expression_p=*/true);
5620 /* Simplify EXPR if it is a non-dependent expression. Returns the
5621 (possibly simplified) expression. */
5623 tree
5624 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5626 if (expr == NULL_TREE)
5627 return NULL_TREE;
5629 /* If we're in a template, but EXPR isn't value dependent, simplify
5630 it. We're supposed to treat:
5632 template <typename T> void f(T[1 + 1]);
5633 template <typename T> void f(T[2]);
5635 as two declarations of the same function, for example. */
5636 if (processing_template_decl
5637 && !instantiation_dependent_expression_p (expr)
5638 && potential_constant_expression (expr))
5640 processing_template_decl_sentinel s;
5641 expr = instantiate_non_dependent_expr_internal (expr, complain);
5643 return expr;
5646 tree
5647 instantiate_non_dependent_expr (tree expr)
5649 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5652 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5653 template declaration, or a TYPE_DECL for an alias declaration. */
5655 bool
5656 alias_type_or_template_p (tree t)
5658 if (t == NULL_TREE)
5659 return false;
5660 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5661 || (TYPE_P (t)
5662 && TYPE_NAME (t)
5663 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5664 || DECL_ALIAS_TEMPLATE_P (t));
5667 /* Return TRUE iff T is a specialization of an alias template. */
5669 bool
5670 alias_template_specialization_p (const_tree t)
5672 /* It's an alias template specialization if it's an alias and its
5673 TYPE_NAME is a specialization of a primary template. */
5674 if (TYPE_ALIAS_P (t))
5676 tree name = TYPE_NAME (t);
5677 if (DECL_LANG_SPECIFIC (name))
5678 if (tree ti = DECL_TEMPLATE_INFO (name))
5680 tree tmpl = TI_TEMPLATE (ti);
5681 return PRIMARY_TEMPLATE_P (tmpl);
5684 return false;
5687 /* Return TRUE iff T is a specialization of an alias template with
5688 dependent template-arguments. */
5690 bool
5691 dependent_alias_template_spec_p (const_tree t)
5693 return (alias_template_specialization_p (t)
5694 && (any_dependent_template_arguments_p
5695 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t)))));
5698 /* Return the number of innermost template parameters in TMPL. */
5700 static int
5701 num_innermost_template_parms (tree tmpl)
5703 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5704 return TREE_VEC_LENGTH (parms);
5707 /* Return either TMPL or another template that it is equivalent to under DR
5708 1286: An alias that just changes the name of a template is equivalent to
5709 the other template. */
5711 static tree
5712 get_underlying_template (tree tmpl)
5714 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5715 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5717 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5718 if (TYPE_TEMPLATE_INFO (result))
5720 tree sub = TYPE_TI_TEMPLATE (result);
5721 if (PRIMARY_TEMPLATE_P (sub)
5722 && (num_innermost_template_parms (tmpl)
5723 == num_innermost_template_parms (sub)))
5725 tree alias_args = INNERMOST_TEMPLATE_ARGS
5726 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5727 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5728 break;
5729 /* The alias type is equivalent to the pattern of the
5730 underlying template, so strip the alias. */
5731 tmpl = sub;
5732 continue;
5735 break;
5737 return tmpl;
5740 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5741 must be a function or a pointer-to-function type, as specified
5742 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5743 and check that the resulting function has external linkage. */
5745 static tree
5746 convert_nontype_argument_function (tree type, tree expr,
5747 tsubst_flags_t complain)
5749 tree fns = expr;
5750 tree fn, fn_no_ptr;
5751 linkage_kind linkage;
5753 fn = instantiate_type (type, fns, tf_none);
5754 if (fn == error_mark_node)
5755 return error_mark_node;
5757 fn_no_ptr = fn;
5758 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5759 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5760 if (BASELINK_P (fn_no_ptr))
5761 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5763 /* [temp.arg.nontype]/1
5765 A template-argument for a non-type, non-template template-parameter
5766 shall be one of:
5767 [...]
5768 -- the address of an object or function with external [C++11: or
5769 internal] linkage. */
5771 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5773 if (complain & tf_error)
5775 error ("%qE is not a valid template argument for type %qT",
5776 expr, type);
5777 if (TYPE_PTR_P (type))
5778 error ("it must be the address of a function with "
5779 "external linkage");
5780 else
5781 error ("it must be the name of a function with "
5782 "external linkage");
5784 return NULL_TREE;
5787 linkage = decl_linkage (fn_no_ptr);
5788 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5790 if (complain & tf_error)
5792 if (cxx_dialect >= cxx11)
5793 error ("%qE is not a valid template argument for type %qT "
5794 "because %qD has no linkage",
5795 expr, type, fn_no_ptr);
5796 else
5797 error ("%qE is not a valid template argument for type %qT "
5798 "because %qD does not have external linkage",
5799 expr, type, fn_no_ptr);
5801 return NULL_TREE;
5804 return fn;
5807 /* Subroutine of convert_nontype_argument.
5808 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5809 Emit an error otherwise. */
5811 static bool
5812 check_valid_ptrmem_cst_expr (tree type, tree expr,
5813 tsubst_flags_t complain)
5815 STRIP_NOPS (expr);
5816 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5817 return true;
5818 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5819 return true;
5820 if (processing_template_decl
5821 && TREE_CODE (expr) == ADDR_EXPR
5822 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
5823 return true;
5824 if (complain & tf_error)
5826 error ("%qE is not a valid template argument for type %qT",
5827 expr, type);
5828 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5830 return false;
5833 /* Returns TRUE iff the address of OP is value-dependent.
5835 14.6.2.4 [temp.dep.temp]:
5836 A non-integral non-type template-argument is dependent if its type is
5837 dependent or it has either of the following forms
5838 qualified-id
5839 & qualified-id
5840 and contains a nested-name-specifier which specifies a class-name that
5841 names a dependent type.
5843 We generalize this to just say that the address of a member of a
5844 dependent class is value-dependent; the above doesn't cover the
5845 address of a static data member named with an unqualified-id. */
5847 static bool
5848 has_value_dependent_address (tree op)
5850 /* We could use get_inner_reference here, but there's no need;
5851 this is only relevant for template non-type arguments, which
5852 can only be expressed as &id-expression. */
5853 if (DECL_P (op))
5855 tree ctx = CP_DECL_CONTEXT (op);
5856 if (TYPE_P (ctx) && dependent_type_p (ctx))
5857 return true;
5860 return false;
5863 /* The next set of functions are used for providing helpful explanatory
5864 diagnostics for failed overload resolution. Their messages should be
5865 indented by two spaces for consistency with the messages in
5866 call.c */
5868 static int
5869 unify_success (bool /*explain_p*/)
5871 return 0;
5874 static int
5875 unify_parameter_deduction_failure (bool explain_p, tree parm)
5877 if (explain_p)
5878 inform (input_location,
5879 " couldn't deduce template parameter %qD", parm);
5880 return 1;
5883 static int
5884 unify_invalid (bool /*explain_p*/)
5886 return 1;
5889 static int
5890 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5892 if (explain_p)
5893 inform (input_location,
5894 " types %qT and %qT have incompatible cv-qualifiers",
5895 parm, arg);
5896 return 1;
5899 static int
5900 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5902 if (explain_p)
5903 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5904 return 1;
5907 static int
5908 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5910 if (explain_p)
5911 inform (input_location,
5912 " template parameter %qD is not a parameter pack, but "
5913 "argument %qD is",
5914 parm, arg);
5915 return 1;
5918 static int
5919 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5921 if (explain_p)
5922 inform (input_location,
5923 " template argument %qE does not match "
5924 "pointer-to-member constant %qE",
5925 arg, parm);
5926 return 1;
5929 static int
5930 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5932 if (explain_p)
5933 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5934 return 1;
5937 static int
5938 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5940 if (explain_p)
5941 inform (input_location,
5942 " inconsistent parameter pack deduction with %qT and %qT",
5943 old_arg, new_arg);
5944 return 1;
5947 static int
5948 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5950 if (explain_p)
5952 if (TYPE_P (parm))
5953 inform (input_location,
5954 " deduced conflicting types for parameter %qT (%qT and %qT)",
5955 parm, first, second);
5956 else
5957 inform (input_location,
5958 " deduced conflicting values for non-type parameter "
5959 "%qE (%qE and %qE)", parm, first, second);
5961 return 1;
5964 static int
5965 unify_vla_arg (bool explain_p, tree arg)
5967 if (explain_p)
5968 inform (input_location,
5969 " variable-sized array type %qT is not "
5970 "a valid template argument",
5971 arg);
5972 return 1;
5975 static int
5976 unify_method_type_error (bool explain_p, tree arg)
5978 if (explain_p)
5979 inform (input_location,
5980 " member function type %qT is not a valid template argument",
5981 arg);
5982 return 1;
5985 static int
5986 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
5988 if (explain_p)
5990 if (least_p)
5991 inform_n (input_location, wanted,
5992 " candidate expects at least %d argument, %d provided",
5993 " candidate expects at least %d arguments, %d provided",
5994 wanted, have);
5995 else
5996 inform_n (input_location, wanted,
5997 " candidate expects %d argument, %d provided",
5998 " candidate expects %d arguments, %d provided",
5999 wanted, have);
6001 return 1;
6004 static int
6005 unify_too_many_arguments (bool explain_p, int have, int wanted)
6007 return unify_arity (explain_p, have, wanted);
6010 static int
6011 unify_too_few_arguments (bool explain_p, int have, int wanted,
6012 bool least_p = false)
6014 return unify_arity (explain_p, have, wanted, least_p);
6017 static int
6018 unify_arg_conversion (bool explain_p, tree to_type,
6019 tree from_type, tree arg)
6021 if (explain_p)
6022 inform (EXPR_LOC_OR_LOC (arg, input_location),
6023 " cannot convert %qE (type %qT) to type %qT",
6024 arg, from_type, to_type);
6025 return 1;
6028 static int
6029 unify_no_common_base (bool explain_p, enum template_base_result r,
6030 tree parm, tree arg)
6032 if (explain_p)
6033 switch (r)
6035 case tbr_ambiguous_baseclass:
6036 inform (input_location, " %qT is an ambiguous base class of %qT",
6037 parm, arg);
6038 break;
6039 default:
6040 inform (input_location, " %qT is not derived from %qT", arg, parm);
6041 break;
6043 return 1;
6046 static int
6047 unify_inconsistent_template_template_parameters (bool explain_p)
6049 if (explain_p)
6050 inform (input_location,
6051 " template parameters of a template template argument are "
6052 "inconsistent with other deduced template arguments");
6053 return 1;
6056 static int
6057 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6059 if (explain_p)
6060 inform (input_location,
6061 " can't deduce a template for %qT from non-template type %qT",
6062 parm, arg);
6063 return 1;
6066 static int
6067 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6069 if (explain_p)
6070 inform (input_location,
6071 " template argument %qE does not match %qD", arg, parm);
6072 return 1;
6075 static int
6076 unify_overload_resolution_failure (bool explain_p, tree arg)
6078 if (explain_p)
6079 inform (input_location,
6080 " could not resolve address from overloaded function %qE",
6081 arg);
6082 return 1;
6085 /* Attempt to convert the non-type template parameter EXPR to the
6086 indicated TYPE. If the conversion is successful, return the
6087 converted value. If the conversion is unsuccessful, return
6088 NULL_TREE if we issued an error message, or error_mark_node if we
6089 did not. We issue error messages for out-and-out bad template
6090 parameters, but not simply because the conversion failed, since we
6091 might be just trying to do argument deduction. Both TYPE and EXPR
6092 must be non-dependent.
6094 The conversion follows the special rules described in
6095 [temp.arg.nontype], and it is much more strict than an implicit
6096 conversion.
6098 This function is called twice for each template argument (see
6099 lookup_template_class for a more accurate description of this
6100 problem). This means that we need to handle expressions which
6101 are not valid in a C++ source, but can be created from the
6102 first call (for instance, casts to perform conversions). These
6103 hacks can go away after we fix the double coercion problem. */
6105 static tree
6106 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6108 tree expr_type;
6110 /* Detect immediately string literals as invalid non-type argument.
6111 This special-case is not needed for correctness (we would easily
6112 catch this later), but only to provide better diagnostic for this
6113 common user mistake. As suggested by DR 100, we do not mention
6114 linkage issues in the diagnostic as this is not the point. */
6115 /* FIXME we're making this OK. */
6116 if (TREE_CODE (expr) == STRING_CST)
6118 if (complain & tf_error)
6119 error ("%qE is not a valid template argument for type %qT "
6120 "because string literals can never be used in this context",
6121 expr, type);
6122 return NULL_TREE;
6125 /* Add the ADDR_EXPR now for the benefit of
6126 value_dependent_expression_p. */
6127 if (TYPE_PTROBV_P (type)
6128 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6130 expr = decay_conversion (expr, complain);
6131 if (expr == error_mark_node)
6132 return error_mark_node;
6135 /* If we are in a template, EXPR may be non-dependent, but still
6136 have a syntactic, rather than semantic, form. For example, EXPR
6137 might be a SCOPE_REF, rather than the VAR_DECL to which the
6138 SCOPE_REF refers. Preserving the qualifying scope is necessary
6139 so that access checking can be performed when the template is
6140 instantiated -- but here we need the resolved form so that we can
6141 convert the argument. */
6142 bool non_dep = false;
6143 if (TYPE_REF_OBJ_P (type)
6144 && has_value_dependent_address (expr))
6145 /* If we want the address and it's value-dependent, don't fold. */;
6146 else if (!type_unknown_p (expr)
6147 && processing_template_decl
6148 && !instantiation_dependent_expression_p (expr)
6149 && potential_constant_expression (expr))
6150 non_dep = true;
6151 if (error_operand_p (expr))
6152 return error_mark_node;
6153 expr_type = TREE_TYPE (expr);
6154 if (TREE_CODE (type) == REFERENCE_TYPE)
6155 expr = mark_lvalue_use (expr);
6156 else
6157 expr = mark_rvalue_use (expr);
6159 /* If the argument is non-dependent, perform any conversions in
6160 non-dependent context as well. */
6161 processing_template_decl_sentinel s (non_dep);
6162 if (non_dep)
6163 expr = instantiate_non_dependent_expr_internal (expr, complain);
6165 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6166 to a non-type argument of "nullptr". */
6167 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6168 expr = convert (type, expr);
6170 /* In C++11, integral or enumeration non-type template arguments can be
6171 arbitrary constant expressions. Pointer and pointer to
6172 member arguments can be general constant expressions that evaluate
6173 to a null value, but otherwise still need to be of a specific form. */
6174 if (cxx_dialect >= cxx11)
6176 if (TREE_CODE (expr) == PTRMEM_CST)
6177 /* A PTRMEM_CST is already constant, and a valid template
6178 argument for a parameter of pointer to member type, we just want
6179 to leave it in that form rather than lower it to a
6180 CONSTRUCTOR. */;
6181 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6182 expr = maybe_constant_value (expr);
6183 else if (TYPE_PTR_OR_PTRMEM_P (type))
6185 tree folded = maybe_constant_value (expr);
6186 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6187 : null_member_pointer_value_p (folded))
6188 expr = folded;
6192 /* HACK: Due to double coercion, we can get a
6193 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6194 which is the tree that we built on the first call (see
6195 below when coercing to reference to object or to reference to
6196 function). We just strip everything and get to the arg.
6197 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6198 for examples. */
6199 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6201 tree probe_type, probe = expr;
6202 if (REFERENCE_REF_P (probe))
6203 probe = TREE_OPERAND (probe, 0);
6204 probe_type = TREE_TYPE (probe);
6205 if (TREE_CODE (probe) == NOP_EXPR)
6207 /* ??? Maybe we could use convert_from_reference here, but we
6208 would need to relax its constraints because the NOP_EXPR
6209 could actually change the type to something more cv-qualified,
6210 and this is not folded by convert_from_reference. */
6211 tree addr = TREE_OPERAND (probe, 0);
6212 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6213 && TREE_CODE (addr) == ADDR_EXPR
6214 && TYPE_PTR_P (TREE_TYPE (addr))
6215 && (same_type_ignoring_top_level_qualifiers_p
6216 (TREE_TYPE (probe_type),
6217 TREE_TYPE (TREE_TYPE (addr)))))
6219 expr = TREE_OPERAND (addr, 0);
6220 expr_type = TREE_TYPE (probe_type);
6225 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6226 parameter is a pointer to object, through decay and
6227 qualification conversion. Let's strip everything. */
6228 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6230 tree probe = expr;
6231 STRIP_NOPS (probe);
6232 if (TREE_CODE (probe) == ADDR_EXPR
6233 && TYPE_PTR_P (TREE_TYPE (probe)))
6235 /* Skip the ADDR_EXPR only if it is part of the decay for
6236 an array. Otherwise, it is part of the original argument
6237 in the source code. */
6238 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6239 probe = TREE_OPERAND (probe, 0);
6240 expr = probe;
6241 expr_type = TREE_TYPE (expr);
6245 /* [temp.arg.nontype]/5, bullet 1
6247 For a non-type template-parameter of integral or enumeration type,
6248 integral promotions (_conv.prom_) and integral conversions
6249 (_conv.integral_) are applied. */
6250 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6252 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6253 t = maybe_constant_value (t);
6254 if (t != error_mark_node)
6255 expr = t;
6257 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6258 return error_mark_node;
6260 /* Notice that there are constant expressions like '4 % 0' which
6261 do not fold into integer constants. */
6262 if (TREE_CODE (expr) != INTEGER_CST)
6264 if (complain & tf_error)
6266 int errs = errorcount, warns = warningcount + werrorcount;
6267 if (processing_template_decl
6268 && !require_potential_constant_expression (expr))
6269 return NULL_TREE;
6270 expr = cxx_constant_value (expr);
6271 if (errorcount > errs || warningcount + werrorcount > warns)
6272 inform (EXPR_LOC_OR_LOC (expr, input_location),
6273 "in template argument for type %qT ", type);
6274 if (expr == error_mark_node)
6275 return NULL_TREE;
6276 /* else cxx_constant_value complained but gave us
6277 a real constant, so go ahead. */
6278 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6280 else
6281 return NULL_TREE;
6284 /* Avoid typedef problems. */
6285 if (TREE_TYPE (expr) != type)
6286 expr = fold_convert (type, expr);
6288 /* [temp.arg.nontype]/5, bullet 2
6290 For a non-type template-parameter of type pointer to object,
6291 qualification conversions (_conv.qual_) and the array-to-pointer
6292 conversion (_conv.array_) are applied. */
6293 else if (TYPE_PTROBV_P (type))
6295 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6297 A template-argument for a non-type, non-template template-parameter
6298 shall be one of: [...]
6300 -- the name of a non-type template-parameter;
6301 -- the address of an object or function with external linkage, [...]
6302 expressed as "& id-expression" where the & is optional if the name
6303 refers to a function or array, or if the corresponding
6304 template-parameter is a reference.
6306 Here, we do not care about functions, as they are invalid anyway
6307 for a parameter of type pointer-to-object. */
6309 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
6310 /* Non-type template parameters are OK. */
6312 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6313 /* Null pointer values are OK in C++11. */;
6314 else if (TREE_CODE (expr) != ADDR_EXPR
6315 && TREE_CODE (expr_type) != ARRAY_TYPE)
6317 if (VAR_P (expr))
6319 if (complain & tf_error)
6320 error ("%qD is not a valid template argument "
6321 "because %qD is a variable, not the address of "
6322 "a variable", expr, expr);
6323 return NULL_TREE;
6325 if (POINTER_TYPE_P (expr_type))
6327 if (complain & tf_error)
6328 error ("%qE is not a valid template argument for %qT "
6329 "because it is not the address of a variable",
6330 expr, type);
6331 return NULL_TREE;
6333 /* Other values, like integer constants, might be valid
6334 non-type arguments of some other type. */
6335 return error_mark_node;
6337 else
6339 tree decl;
6341 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6342 ? TREE_OPERAND (expr, 0) : expr);
6343 if (!VAR_P (decl))
6345 if (complain & tf_error)
6346 error ("%qE is not a valid template argument of type %qT "
6347 "because %qE is not a variable", expr, type, decl);
6348 return NULL_TREE;
6350 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6352 if (complain & tf_error)
6353 error ("%qE is not a valid template argument of type %qT "
6354 "because %qD does not have external linkage",
6355 expr, type, decl);
6356 return NULL_TREE;
6358 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6360 if (complain & tf_error)
6361 error ("%qE is not a valid template argument of type %qT "
6362 "because %qD has no linkage", expr, type, decl);
6363 return NULL_TREE;
6367 expr = decay_conversion (expr, complain);
6368 if (expr == error_mark_node)
6369 return error_mark_node;
6371 expr = perform_qualification_conversions (type, expr);
6372 if (expr == error_mark_node)
6373 return error_mark_node;
6375 /* [temp.arg.nontype]/5, bullet 3
6377 For a non-type template-parameter of type reference to object, no
6378 conversions apply. The type referred to by the reference may be more
6379 cv-qualified than the (otherwise identical) type of the
6380 template-argument. The template-parameter is bound directly to the
6381 template-argument, which must be an lvalue. */
6382 else if (TYPE_REF_OBJ_P (type))
6384 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6385 expr_type))
6386 return error_mark_node;
6388 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6390 if (complain & tf_error)
6391 error ("%qE is not a valid template argument for type %qT "
6392 "because of conflicts in cv-qualification", expr, type);
6393 return NULL_TREE;
6396 if (!real_lvalue_p (expr))
6398 if (complain & tf_error)
6399 error ("%qE is not a valid template argument for type %qT "
6400 "because it is not an lvalue", expr, type);
6401 return NULL_TREE;
6404 /* [temp.arg.nontype]/1
6406 A template-argument for a non-type, non-template template-parameter
6407 shall be one of: [...]
6409 -- the address of an object or function with external linkage. */
6410 if (INDIRECT_REF_P (expr)
6411 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6413 expr = TREE_OPERAND (expr, 0);
6414 if (DECL_P (expr))
6416 if (complain & tf_error)
6417 error ("%q#D is not a valid template argument for type %qT "
6418 "because a reference variable does not have a constant "
6419 "address", expr, type);
6420 return NULL_TREE;
6424 if (!DECL_P (expr))
6426 if (complain & tf_error)
6427 error ("%qE is not a valid template argument for type %qT "
6428 "because it is not an object with external linkage",
6429 expr, type);
6430 return NULL_TREE;
6433 if (!DECL_EXTERNAL_LINKAGE_P (expr))
6435 if (complain & tf_error)
6436 error ("%qE is not a valid template argument for type %qT "
6437 "because object %qD has not external linkage",
6438 expr, type, expr);
6439 return NULL_TREE;
6442 expr = build_nop (type, build_address (expr));
6444 /* [temp.arg.nontype]/5, bullet 4
6446 For a non-type template-parameter of type pointer to function, only
6447 the function-to-pointer conversion (_conv.func_) is applied. If the
6448 template-argument represents a set of overloaded functions (or a
6449 pointer to such), the matching function is selected from the set
6450 (_over.over_). */
6451 else if (TYPE_PTRFN_P (type))
6453 /* If the argument is a template-id, we might not have enough
6454 context information to decay the pointer. */
6455 if (!type_unknown_p (expr_type))
6457 expr = decay_conversion (expr, complain);
6458 if (expr == error_mark_node)
6459 return error_mark_node;
6462 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6463 /* Null pointer values are OK in C++11. */
6464 return perform_qualification_conversions (type, expr);
6466 expr = convert_nontype_argument_function (type, expr, complain);
6467 if (!expr || expr == error_mark_node)
6468 return expr;
6470 /* [temp.arg.nontype]/5, bullet 5
6472 For a non-type template-parameter of type reference to function, no
6473 conversions apply. If the template-argument represents a set of
6474 overloaded functions, the matching function is selected from the set
6475 (_over.over_). */
6476 else if (TYPE_REFFN_P (type))
6478 if (TREE_CODE (expr) == ADDR_EXPR)
6480 if (complain & tf_error)
6482 error ("%qE is not a valid template argument for type %qT "
6483 "because it is a pointer", expr, type);
6484 inform (input_location, "try using %qE instead",
6485 TREE_OPERAND (expr, 0));
6487 return NULL_TREE;
6490 expr = convert_nontype_argument_function (type, expr, complain);
6491 if (!expr || expr == error_mark_node)
6492 return expr;
6494 expr = build_nop (type, build_address (expr));
6496 /* [temp.arg.nontype]/5, bullet 6
6498 For a non-type template-parameter of type pointer to member function,
6499 no conversions apply. If the template-argument represents a set of
6500 overloaded member functions, the matching member function is selected
6501 from the set (_over.over_). */
6502 else if (TYPE_PTRMEMFUNC_P (type))
6504 expr = instantiate_type (type, expr, tf_none);
6505 if (expr == error_mark_node)
6506 return error_mark_node;
6508 /* [temp.arg.nontype] bullet 1 says the pointer to member
6509 expression must be a pointer-to-member constant. */
6510 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6511 return error_mark_node;
6513 /* There is no way to disable standard conversions in
6514 resolve_address_of_overloaded_function (called by
6515 instantiate_type). It is possible that the call succeeded by
6516 converting &B::I to &D::I (where B is a base of D), so we need
6517 to reject this conversion here.
6519 Actually, even if there was a way to disable standard conversions,
6520 it would still be better to reject them here so that we can
6521 provide a superior diagnostic. */
6522 if (!same_type_p (TREE_TYPE (expr), type))
6524 if (complain & tf_error)
6526 error ("%qE is not a valid template argument for type %qT "
6527 "because it is of type %qT", expr, type,
6528 TREE_TYPE (expr));
6529 /* If we are just one standard conversion off, explain. */
6530 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6531 inform (input_location,
6532 "standard conversions are not allowed in this context");
6534 return NULL_TREE;
6537 /* [temp.arg.nontype]/5, bullet 7
6539 For a non-type template-parameter of type pointer to data member,
6540 qualification conversions (_conv.qual_) are applied. */
6541 else if (TYPE_PTRDATAMEM_P (type))
6543 /* [temp.arg.nontype] bullet 1 says the pointer to member
6544 expression must be a pointer-to-member constant. */
6545 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6546 return error_mark_node;
6548 expr = perform_qualification_conversions (type, expr);
6549 if (expr == error_mark_node)
6550 return expr;
6552 else if (NULLPTR_TYPE_P (type))
6554 if (expr != nullptr_node)
6556 if (complain & tf_error)
6557 error ("%qE is not a valid template argument for type %qT "
6558 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6559 return NULL_TREE;
6561 return expr;
6563 /* A template non-type parameter must be one of the above. */
6564 else
6565 gcc_unreachable ();
6567 /* Sanity check: did we actually convert the argument to the
6568 right type? */
6569 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6570 (type, TREE_TYPE (expr)));
6571 return convert_from_reference (expr);
6574 /* Subroutine of coerce_template_template_parms, which returns 1 if
6575 PARM_PARM and ARG_PARM match using the rule for the template
6576 parameters of template template parameters. Both PARM and ARG are
6577 template parameters; the rest of the arguments are the same as for
6578 coerce_template_template_parms.
6580 static int
6581 coerce_template_template_parm (tree parm,
6582 tree arg,
6583 tsubst_flags_t complain,
6584 tree in_decl,
6585 tree outer_args)
6587 if (arg == NULL_TREE || error_operand_p (arg)
6588 || parm == NULL_TREE || error_operand_p (parm))
6589 return 0;
6591 if (TREE_CODE (arg) != TREE_CODE (parm))
6592 return 0;
6594 switch (TREE_CODE (parm))
6596 case TEMPLATE_DECL:
6597 /* We encounter instantiations of templates like
6598 template <template <template <class> class> class TT>
6599 class C; */
6601 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6602 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6604 if (!coerce_template_template_parms
6605 (parmparm, argparm, complain, in_decl, outer_args))
6606 return 0;
6608 /* Fall through. */
6610 case TYPE_DECL:
6611 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6612 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6613 /* Argument is a parameter pack but parameter is not. */
6614 return 0;
6615 break;
6617 case PARM_DECL:
6618 /* The tsubst call is used to handle cases such as
6620 template <int> class C {};
6621 template <class T, template <T> class TT> class D {};
6622 D<int, C> d;
6624 i.e. the parameter list of TT depends on earlier parameters. */
6625 if (!uses_template_parms (TREE_TYPE (arg))
6626 && !same_type_p
6627 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6628 TREE_TYPE (arg)))
6629 return 0;
6631 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6632 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6633 /* Argument is a parameter pack but parameter is not. */
6634 return 0;
6636 break;
6638 default:
6639 gcc_unreachable ();
6642 return 1;
6646 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6647 template template parameters. Both PARM_PARMS and ARG_PARMS are
6648 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6649 or PARM_DECL.
6651 Consider the example:
6652 template <class T> class A;
6653 template<template <class U> class TT> class B;
6655 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6656 the parameters to A, and OUTER_ARGS contains A. */
6658 static int
6659 coerce_template_template_parms (tree parm_parms,
6660 tree arg_parms,
6661 tsubst_flags_t complain,
6662 tree in_decl,
6663 tree outer_args)
6665 int nparms, nargs, i;
6666 tree parm, arg;
6667 int variadic_p = 0;
6669 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6670 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6672 nparms = TREE_VEC_LENGTH (parm_parms);
6673 nargs = TREE_VEC_LENGTH (arg_parms);
6675 /* Determine whether we have a parameter pack at the end of the
6676 template template parameter's template parameter list. */
6677 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6679 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6681 if (error_operand_p (parm))
6682 return 0;
6684 switch (TREE_CODE (parm))
6686 case TEMPLATE_DECL:
6687 case TYPE_DECL:
6688 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6689 variadic_p = 1;
6690 break;
6692 case PARM_DECL:
6693 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6694 variadic_p = 1;
6695 break;
6697 default:
6698 gcc_unreachable ();
6702 if (nargs != nparms
6703 && !(variadic_p && nargs >= nparms - 1))
6704 return 0;
6706 /* Check all of the template parameters except the parameter pack at
6707 the end (if any). */
6708 for (i = 0; i < nparms - variadic_p; ++i)
6710 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6711 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6712 continue;
6714 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6715 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6717 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6718 outer_args))
6719 return 0;
6723 if (variadic_p)
6725 /* Check each of the template parameters in the template
6726 argument against the template parameter pack at the end of
6727 the template template parameter. */
6728 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6729 return 0;
6731 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6733 for (; i < nargs; ++i)
6735 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6736 continue;
6738 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6740 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6741 outer_args))
6742 return 0;
6746 return 1;
6749 /* Verifies that the deduced template arguments (in TARGS) for the
6750 template template parameters (in TPARMS) represent valid bindings,
6751 by comparing the template parameter list of each template argument
6752 to the template parameter list of its corresponding template
6753 template parameter, in accordance with DR150. This
6754 routine can only be called after all template arguments have been
6755 deduced. It will return TRUE if all of the template template
6756 parameter bindings are okay, FALSE otherwise. */
6757 bool
6758 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6760 int i, ntparms = TREE_VEC_LENGTH (tparms);
6761 bool ret = true;
6763 /* We're dealing with template parms in this process. */
6764 ++processing_template_decl;
6766 targs = INNERMOST_TEMPLATE_ARGS (targs);
6768 for (i = 0; i < ntparms; ++i)
6770 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6771 tree targ = TREE_VEC_ELT (targs, i);
6773 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6775 tree packed_args = NULL_TREE;
6776 int idx, len = 1;
6778 if (ARGUMENT_PACK_P (targ))
6780 /* Look inside the argument pack. */
6781 packed_args = ARGUMENT_PACK_ARGS (targ);
6782 len = TREE_VEC_LENGTH (packed_args);
6785 for (idx = 0; idx < len; ++idx)
6787 tree targ_parms = NULL_TREE;
6789 if (packed_args)
6790 /* Extract the next argument from the argument
6791 pack. */
6792 targ = TREE_VEC_ELT (packed_args, idx);
6794 if (PACK_EXPANSION_P (targ))
6795 /* Look at the pattern of the pack expansion. */
6796 targ = PACK_EXPANSION_PATTERN (targ);
6798 /* Extract the template parameters from the template
6799 argument. */
6800 if (TREE_CODE (targ) == TEMPLATE_DECL)
6801 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6802 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6803 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6805 /* Verify that we can coerce the template template
6806 parameters from the template argument to the template
6807 parameter. This requires an exact match. */
6808 if (targ_parms
6809 && !coerce_template_template_parms
6810 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6811 targ_parms,
6812 tf_none,
6813 tparm,
6814 targs))
6816 ret = false;
6817 goto out;
6823 out:
6825 --processing_template_decl;
6826 return ret;
6829 /* Since type attributes aren't mangled, we need to strip them from
6830 template type arguments. */
6832 static tree
6833 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6835 tree mv;
6836 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6837 return arg;
6838 mv = TYPE_MAIN_VARIANT (arg);
6839 arg = strip_typedefs (arg);
6840 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6841 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6843 if (complain & tf_warning)
6844 warning (0, "ignoring attributes on template argument %qT", arg);
6845 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6846 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6848 return arg;
6851 // A template declaration can be substituted for a constrained
6852 // template template parameter only when the argument is more
6853 // constrained than the parameter.
6854 static bool
6855 is_compatible_template_arg (tree parm, tree arg)
6857 // TODO: The argument may not have a decl result. This seems to
6858 // happen in some classes with nested template template
6859 // parameters (e.g., a rebind struct in a class taking a template
6860 // template parameter. If this is the case, just return true and
6861 // allow things to happen as they always did.
6862 if (TREE_CODE (arg) == TEMPLATE_DECL)
6864 if (!DECL_TEMPLATE_RESULT (arg))
6865 return true;
6868 tree parm_cons = get_constraints (parm);
6869 tree arg_cons = get_constraints (arg);
6871 // If the template parameter is constrained, we need to rewrite its
6872 // constraints in terms of the ARG's template parameters. This ensures
6873 // that all of the template parameter types will have the same depth.
6875 // Note that this is only valid when coerce_template_template_parm is
6876 // true for the innermost template parameters of PARM and ARG. In other
6877 // words, because coercion is successful, this conversion will be valid.
6878 if (parm_cons)
6880 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
6881 parm_cons = tsubst_constraint_info (parm_cons, args, tf_none, NULL_TREE);
6882 if (parm_cons == error_mark_node)
6883 return false;
6886 return subsumes (parm_cons, arg_cons);
6889 // Convert a placeholder argument into a binding to the original
6890 // parameter. The original parameter is saved as the TREE_TYPE of
6891 // ARG.
6892 static inline tree
6893 convert_placeholder_argument (tree parm, tree arg)
6895 TREE_TYPE (arg) = parm;
6896 return arg;
6899 /* Convert the indicated template ARG as necessary to match the
6900 indicated template PARM. Returns the converted ARG, or
6901 error_mark_node if the conversion was unsuccessful. Error and
6902 warning messages are issued under control of COMPLAIN. This
6903 conversion is for the Ith parameter in the parameter list. ARGS is
6904 the full set of template arguments deduced so far. */
6906 static tree
6907 convert_template_argument (tree parm,
6908 tree arg,
6909 tree args,
6910 tsubst_flags_t complain,
6911 int i,
6912 tree in_decl)
6914 tree orig_arg;
6915 tree val;
6916 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6918 if (parm == error_mark_node)
6919 return error_mark_node;
6921 /* Trivially convert placeholders. */
6922 if (TREE_CODE (arg) == INTRODUCED_PARM_DECL)
6923 return convert_placeholder_argument (parm, arg);
6925 if (TREE_CODE (arg) == TREE_LIST
6926 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6928 /* The template argument was the name of some
6929 member function. That's usually
6930 invalid, but static members are OK. In any
6931 case, grab the underlying fields/functions
6932 and issue an error later if required. */
6933 orig_arg = TREE_VALUE (arg);
6934 TREE_TYPE (arg) = unknown_type_node;
6937 orig_arg = arg;
6939 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6940 requires_type = (TREE_CODE (parm) == TYPE_DECL
6941 || requires_tmpl_type);
6943 /* When determining whether an argument pack expansion is a template,
6944 look at the pattern. */
6945 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6946 arg = PACK_EXPANSION_PATTERN (arg);
6948 /* Deal with an injected-class-name used as a template template arg. */
6949 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6951 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6952 if (TREE_CODE (t) == TEMPLATE_DECL)
6954 if (cxx_dialect >= cxx11)
6955 /* OK under DR 1004. */;
6956 else if (complain & tf_warning_or_error)
6957 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6958 " used as template template argument", TYPE_NAME (arg));
6959 else if (flag_pedantic_errors)
6960 t = arg;
6962 arg = t;
6966 is_tmpl_type =
6967 ((TREE_CODE (arg) == TEMPLATE_DECL
6968 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6969 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6970 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6971 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6973 if (is_tmpl_type
6974 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6975 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6976 arg = TYPE_STUB_DECL (arg);
6978 is_type = TYPE_P (arg) || is_tmpl_type;
6980 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6981 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6983 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6985 if (complain & tf_error)
6986 error ("invalid use of destructor %qE as a type", orig_arg);
6987 return error_mark_node;
6990 permerror (input_location,
6991 "to refer to a type member of a template parameter, "
6992 "use %<typename %E%>", orig_arg);
6994 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6995 TREE_OPERAND (arg, 1),
6996 typename_type,
6997 complain);
6998 arg = orig_arg;
6999 is_type = 1;
7001 if (is_type != requires_type)
7003 if (in_decl)
7005 if (complain & tf_error)
7007 error ("type/value mismatch at argument %d in template "
7008 "parameter list for %qD",
7009 i + 1, in_decl);
7010 if (is_type)
7011 inform (input_location,
7012 " expected a constant of type %qT, got %qT",
7013 TREE_TYPE (parm),
7014 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7015 else if (requires_tmpl_type)
7016 inform (input_location,
7017 " expected a class template, got %qE", orig_arg);
7018 else
7019 inform (input_location,
7020 " expected a type, got %qE", orig_arg);
7023 return error_mark_node;
7025 if (is_tmpl_type ^ requires_tmpl_type)
7027 if (in_decl && (complain & tf_error))
7029 error ("type/value mismatch at argument %d in template "
7030 "parameter list for %qD",
7031 i + 1, in_decl);
7032 if (is_tmpl_type)
7033 inform (input_location,
7034 " expected a type, got %qT", DECL_NAME (arg));
7035 else
7036 inform (input_location,
7037 " expected a class template, got %qT", orig_arg);
7039 return error_mark_node;
7042 if (is_type)
7044 if (requires_tmpl_type)
7046 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7047 val = orig_arg;
7048 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7049 /* The number of argument required is not known yet.
7050 Just accept it for now. */
7051 val = TREE_TYPE (arg);
7052 else
7054 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7055 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7057 /* Strip alias templates that are equivalent to another
7058 template. */
7059 arg = get_underlying_template (arg);
7060 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7062 if (coerce_template_template_parms (parmparm, argparm,
7063 complain, in_decl,
7064 args))
7066 val = arg;
7068 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7069 TEMPLATE_DECL. */
7070 if (val != error_mark_node)
7072 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7073 val = TREE_TYPE (val);
7074 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7075 val = make_pack_expansion (val);
7078 else
7080 if (in_decl && (complain & tf_error))
7082 error ("type/value mismatch at argument %d in "
7083 "template parameter list for %qD",
7084 i + 1, in_decl);
7085 inform (input_location,
7086 " expected a template of type %qD, got %qT",
7087 parm, orig_arg);
7090 val = error_mark_node;
7093 // Check that the constraints are compatible before allowing the
7094 // substitution.
7095 if (val != error_mark_node)
7096 if (!is_compatible_template_arg (parm, arg))
7098 if (in_decl && (complain & tf_error))
7100 error ("constraint mismatch at argument %d in "
7101 "template parameter list for %qD",
7102 i + 1, in_decl);
7103 inform (input_location, " expected %qD but got %qD",
7104 parm, arg);
7106 val = error_mark_node;
7110 else
7111 val = orig_arg;
7112 /* We only form one instance of each template specialization.
7113 Therefore, if we use a non-canonical variant (i.e., a
7114 typedef), any future messages referring to the type will use
7115 the typedef, which is confusing if those future uses do not
7116 themselves also use the typedef. */
7117 if (TYPE_P (val))
7118 val = canonicalize_type_argument (val, complain);
7120 else
7122 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
7124 if (invalid_nontype_parm_type_p (t, complain))
7125 return error_mark_node;
7127 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7129 if (same_type_p (t, TREE_TYPE (orig_arg)))
7130 val = orig_arg;
7131 else
7133 /* Not sure if this is reachable, but it doesn't hurt
7134 to be robust. */
7135 error ("type mismatch in nontype parameter pack");
7136 val = error_mark_node;
7139 else if (!dependent_template_arg_p (orig_arg)
7140 && !uses_template_parms (t))
7141 /* We used to call digest_init here. However, digest_init
7142 will report errors, which we don't want when complain
7143 is zero. More importantly, digest_init will try too
7144 hard to convert things: for example, `0' should not be
7145 converted to pointer type at this point according to
7146 the standard. Accepting this is not merely an
7147 extension, since deciding whether or not these
7148 conversions can occur is part of determining which
7149 function template to call, or whether a given explicit
7150 argument specification is valid. */
7151 val = convert_nontype_argument (t, orig_arg, complain);
7152 else
7153 val = strip_typedefs_expr (orig_arg);
7155 if (val == NULL_TREE)
7156 val = error_mark_node;
7157 else if (val == error_mark_node && (complain & tf_error))
7158 error ("could not convert template argument %qE to %qT", orig_arg, t);
7160 if (TREE_CODE (val) == SCOPE_REF)
7162 /* Strip typedefs from the SCOPE_REF. */
7163 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7164 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7165 complain);
7166 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7167 QUALIFIED_NAME_IS_TEMPLATE (val));
7171 return val;
7174 /* Coerces the remaining template arguments in INNER_ARGS (from
7175 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7176 Returns the coerced argument pack. PARM_IDX is the position of this
7177 parameter in the template parameter list. ARGS is the original
7178 template argument list. */
7179 static tree
7180 coerce_template_parameter_pack (tree parms,
7181 int parm_idx,
7182 tree args,
7183 tree inner_args,
7184 int arg_idx,
7185 tree new_args,
7186 int* lost,
7187 tree in_decl,
7188 tsubst_flags_t complain)
7190 tree parm = TREE_VEC_ELT (parms, parm_idx);
7191 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7192 tree packed_args;
7193 tree argument_pack;
7194 tree packed_parms = NULL_TREE;
7196 if (arg_idx > nargs)
7197 arg_idx = nargs;
7199 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7201 /* When the template parameter is a non-type template parameter pack
7202 or template template parameter pack whose type or template
7203 parameters use parameter packs, we know exactly how many arguments
7204 we are looking for. Build a vector of the instantiated decls for
7205 these template parameters in PACKED_PARMS. */
7206 /* We can't use make_pack_expansion here because it would interpret a
7207 _DECL as a use rather than a declaration. */
7208 tree decl = TREE_VALUE (parm);
7209 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7210 SET_PACK_EXPANSION_PATTERN (exp, decl);
7211 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7212 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7214 TREE_VEC_LENGTH (args)--;
7215 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7216 TREE_VEC_LENGTH (args)++;
7218 if (packed_parms == error_mark_node)
7219 return error_mark_node;
7221 /* If we're doing a partial instantiation of a member template,
7222 verify that all of the types used for the non-type
7223 template parameter pack are, in fact, valid for non-type
7224 template parameters. */
7225 if (arg_idx < nargs
7226 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7228 int j, len = TREE_VEC_LENGTH (packed_parms);
7229 for (j = 0; j < len; ++j)
7231 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7232 if (invalid_nontype_parm_type_p (t, complain))
7233 return error_mark_node;
7235 /* We don't know how many args we have yet, just
7236 use the unconverted ones for now. */
7237 return NULL_TREE;
7240 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7242 /* Check if we have a placeholder pack, which indicates we're in the context
7243 of a introduction list. In that case we want to simply match this pack to
7244 the single placeholder. */
7245 else if (arg_idx < nargs
7246 && IS_INTRODUCED_PACK (TREE_VEC_ELT (inner_args, arg_idx)))
7248 nargs = arg_idx + 1;
7249 packed_args = make_tree_vec (1);
7251 else
7252 packed_args = make_tree_vec (nargs - arg_idx);
7254 /* Convert the remaining arguments, which will be a part of the
7255 parameter pack "parm". */
7256 for (; arg_idx < nargs; ++arg_idx)
7258 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7259 tree actual_parm = TREE_VALUE (parm);
7260 int pack_idx = arg_idx - parm_idx;
7262 if (packed_parms)
7264 /* Once we've packed as many args as we have types, stop. */
7265 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7266 break;
7267 else if (PACK_EXPANSION_P (arg))
7268 /* We don't know how many args we have yet, just
7269 use the unconverted ones for now. */
7270 return NULL_TREE;
7271 else
7272 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7275 if (arg == error_mark_node)
7277 if (complain & tf_error)
7278 error ("template argument %d is invalid", arg_idx + 1);
7280 else
7281 arg = convert_template_argument (actual_parm,
7282 arg, new_args, complain, parm_idx,
7283 in_decl);
7284 if (arg == error_mark_node)
7285 (*lost)++;
7286 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7289 if (arg_idx - parm_idx < TREE_VEC_LENGTH (packed_args)
7290 && TREE_VEC_LENGTH (packed_args) > 0)
7292 if (complain & tf_error)
7293 error ("wrong number of template arguments (%d, should be %d)",
7294 arg_idx - parm_idx, TREE_VEC_LENGTH (packed_args));
7295 return error_mark_node;
7298 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7299 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7300 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7301 else
7303 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7304 TREE_TYPE (argument_pack)
7305 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7306 TREE_CONSTANT (argument_pack) = 1;
7309 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7310 #ifdef ENABLE_CHECKING
7311 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7312 TREE_VEC_LENGTH (packed_args));
7313 #endif
7314 return argument_pack;
7317 /* Returns the number of pack expansions in the template argument vector
7318 ARGS. */
7320 static int
7321 pack_expansion_args_count (tree args)
7323 int i;
7324 int count = 0;
7325 if (args)
7326 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7328 tree elt = TREE_VEC_ELT (args, i);
7329 if (elt && PACK_EXPANSION_P (elt))
7330 ++count;
7332 return count;
7335 /* Convert all template arguments to their appropriate types, and
7336 return a vector containing the innermost resulting template
7337 arguments. If any error occurs, return error_mark_node. Error and
7338 warning messages are issued under control of COMPLAIN.
7340 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7341 for arguments not specified in ARGS. Otherwise, if
7342 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7343 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7344 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7345 ARGS. */
7347 static tree
7348 coerce_template_parms (tree parms,
7349 tree args,
7350 tree in_decl,
7351 tsubst_flags_t complain,
7352 bool require_all_args,
7353 bool use_default_args)
7355 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7356 tree orig_inner_args;
7357 tree inner_args;
7358 tree new_args;
7359 tree new_inner_args;
7360 int saved_unevaluated_operand;
7361 int saved_inhibit_evaluation_warnings;
7363 /* When used as a boolean value, indicates whether this is a
7364 variadic template parameter list. Since it's an int, we can also
7365 subtract it from nparms to get the number of non-variadic
7366 parameters. */
7367 int variadic_p = 0;
7368 int variadic_args_p = 0;
7369 int post_variadic_parms = 0;
7371 /* Likewise for parameters with default arguments. */
7372 int default_p = 0;
7374 if (args == error_mark_node)
7375 return error_mark_node;
7377 nparms = TREE_VEC_LENGTH (parms);
7379 /* Determine if there are any parameter packs or default arguments. */
7380 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7382 tree parm = TREE_VEC_ELT (parms, parm_idx);
7383 if (variadic_p)
7384 ++post_variadic_parms;
7385 if (template_parameter_pack_p (TREE_VALUE (parm)))
7386 ++variadic_p;
7387 if (TREE_PURPOSE (parm))
7388 ++default_p;
7391 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7392 /* If there are no parameters that follow a parameter pack, we need to
7393 expand any argument packs so that we can deduce a parameter pack from
7394 some non-packed args followed by an argument pack, as in variadic85.C.
7395 If there are such parameters, we need to leave argument packs intact
7396 so the arguments are assigned properly. This can happen when dealing
7397 with a nested class inside a partial specialization of a class
7398 template, as in variadic92.C, or when deducing a template parameter pack
7399 from a sub-declarator, as in variadic114.C. */
7400 if (!post_variadic_parms)
7401 inner_args = expand_template_argument_pack (inner_args);
7403 /* Count any pack expansion args. */
7404 variadic_args_p = pack_expansion_args_count (inner_args);
7406 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7407 if ((nargs > nparms && !variadic_p)
7408 || (nargs < nparms - variadic_p
7409 && require_all_args
7410 && !variadic_args_p
7411 && (!use_default_args
7412 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7413 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7415 if (complain & tf_error)
7417 if (variadic_p || default_p)
7419 nparms -= variadic_p + default_p;
7420 error ("wrong number of template arguments "
7421 "(%d, should be at least %d)", nargs, nparms);
7423 else
7424 error ("wrong number of template arguments "
7425 "(%d, should be %d)", nargs, nparms);
7427 if (in_decl)
7428 inform (input_location, "provided for %q+D", in_decl);
7431 return error_mark_node;
7433 /* We can't pass a pack expansion to a non-pack parameter of an alias
7434 template (DR 1430). */
7435 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
7436 && variadic_args_p
7437 && nargs - variadic_args_p < nparms - variadic_p)
7439 if (complain & tf_error)
7441 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7443 tree arg = TREE_VEC_ELT (inner_args, i);
7444 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7446 if (PACK_EXPANSION_P (arg)
7447 && !template_parameter_pack_p (parm))
7449 error ("pack expansion argument for non-pack parameter "
7450 "%qD of alias template %qD", parm, in_decl);
7451 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7452 goto found;
7455 gcc_unreachable ();
7456 found:;
7458 return error_mark_node;
7461 /* We need to evaluate the template arguments, even though this
7462 template-id may be nested within a "sizeof". */
7463 saved_unevaluated_operand = cp_unevaluated_operand;
7464 cp_unevaluated_operand = 0;
7465 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7466 c_inhibit_evaluation_warnings = 0;
7467 new_inner_args = make_tree_vec (nparms);
7468 new_args = add_outermost_template_args (args, new_inner_args);
7469 int pack_adjust = 0;
7470 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7472 tree arg;
7473 tree parm;
7475 /* Get the Ith template parameter. */
7476 parm = TREE_VEC_ELT (parms, parm_idx);
7478 if (parm == error_mark_node)
7480 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7481 continue;
7484 /* Calculate the next argument. */
7485 if (arg_idx < nargs)
7486 arg = TREE_VEC_ELT (inner_args, arg_idx);
7487 else
7488 arg = NULL_TREE;
7490 if (template_parameter_pack_p (TREE_VALUE (parm))
7491 && !(arg && ARGUMENT_PACK_P (arg)))
7493 /* Some arguments will be placed in the
7494 template parameter pack PARM. */
7495 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7496 inner_args, arg_idx,
7497 new_args, &lost,
7498 in_decl, complain);
7500 if (arg == NULL_TREE)
7502 /* We don't know how many args we have yet, just use the
7503 unconverted (and still packed) ones for now. */
7504 new_inner_args = orig_inner_args;
7505 arg_idx = nargs;
7506 break;
7509 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7511 /* Store this argument. */
7512 if (arg == error_mark_node)
7514 lost++;
7515 /* We are done with all of the arguments. */
7516 arg_idx = nargs;
7518 else
7520 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
7521 arg_idx += pack_adjust;
7524 continue;
7526 else if (arg)
7528 if (PACK_EXPANSION_P (arg))
7530 /* "If every valid specialization of a variadic template
7531 requires an empty template parameter pack, the template is
7532 ill-formed, no diagnostic required." So check that the
7533 pattern works with this parameter. */
7534 tree pattern = PACK_EXPANSION_PATTERN (arg);
7535 tree conv = convert_template_argument (TREE_VALUE (parm),
7536 pattern, new_args,
7537 complain, parm_idx,
7538 in_decl);
7539 if (conv == error_mark_node)
7541 inform (input_location, "so any instantiation with a "
7542 "non-empty parameter pack would be ill-formed");
7543 ++lost;
7545 else if (TYPE_P (conv) && !TYPE_P (pattern))
7546 /* Recover from missing typename. */
7547 TREE_VEC_ELT (inner_args, arg_idx)
7548 = make_pack_expansion (conv);
7550 /* We don't know how many args we have yet, just
7551 use the unconverted ones for now. */
7552 new_inner_args = inner_args;
7553 arg_idx = nargs;
7554 break;
7557 else if (require_all_args)
7559 /* There must be a default arg in this case. */
7560 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7561 complain, in_decl);
7562 /* The position of the first default template argument,
7563 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7564 Record that. */
7565 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7566 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7567 arg_idx - pack_adjust);
7569 else
7570 break;
7572 if (arg == error_mark_node)
7574 if (complain & tf_error)
7575 error ("template argument %d is invalid", arg_idx + 1);
7577 else if (!arg)
7578 /* This only occurs if there was an error in the template
7579 parameter list itself (which we would already have
7580 reported) that we are trying to recover from, e.g., a class
7581 template with a parameter list such as
7582 template<typename..., typename>. */
7583 ++lost;
7584 else
7585 arg = convert_template_argument (TREE_VALUE (parm),
7586 arg, new_args, complain,
7587 parm_idx, in_decl);
7589 if (arg == error_mark_node)
7590 lost++;
7591 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7593 cp_unevaluated_operand = saved_unevaluated_operand;
7594 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7596 if (variadic_p && arg_idx < nargs)
7598 if (complain & tf_error)
7600 error ("wrong number of template arguments "
7601 "(%d, should be %d)", nargs, arg_idx);
7602 if (in_decl)
7603 error ("provided for %q+D", in_decl);
7605 return error_mark_node;
7608 if (lost)
7609 return error_mark_node;
7611 #ifdef ENABLE_CHECKING
7612 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7613 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7614 TREE_VEC_LENGTH (new_inner_args));
7615 #endif
7617 return new_inner_args;
7620 /* Convert all template arguments to their appropriate types, and
7621 return a vector containing the innermost resulting template
7622 arguments. If any error occurs, return error_mark_node. Error and
7623 warning messages are not issued.
7625 Note that no function argument deduction is performed, and default
7626 arguments are used to fill in unspecified arguments. */
7627 tree
7628 coerce_template_parms (tree parms, tree args, tree in_decl)
7630 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
7633 /* Convert all template arguments to their appropriate type, and
7634 instantiate default arguments as needed. This returns a vector
7635 containing the innermost resulting template arguments, or
7636 error_mark_node if unsuccessful. */
7637 tree
7638 coerce_template_parms (tree parms, tree args, tree in_decl,
7639 tsubst_flags_t complain)
7641 return coerce_template_parms (parms, args, in_decl, complain, true, true);
7644 /* Like coerce_template_parms. If PARMS represents all template
7645 parameters levels, this function returns a vector of vectors
7646 representing all the resulting argument levels. Note that in this
7647 case, only the innermost arguments are coerced because the
7648 outermost ones are supposed to have been coerced already.
7650 Otherwise, if PARMS represents only (the innermost) vector of
7651 parameters, this function returns a vector containing just the
7652 innermost resulting arguments. */
7654 static tree
7655 coerce_innermost_template_parms (tree parms,
7656 tree args,
7657 tree in_decl,
7658 tsubst_flags_t complain,
7659 bool require_all_args,
7660 bool use_default_args)
7662 int parms_depth = TMPL_PARMS_DEPTH (parms);
7663 int args_depth = TMPL_ARGS_DEPTH (args);
7664 tree coerced_args;
7666 if (parms_depth > 1)
7668 coerced_args = make_tree_vec (parms_depth);
7669 tree level;
7670 int cur_depth;
7672 for (level = parms, cur_depth = parms_depth;
7673 parms_depth > 0 && level != NULL_TREE;
7674 level = TREE_CHAIN (level), --cur_depth)
7676 tree l;
7677 if (cur_depth == args_depth)
7678 l = coerce_template_parms (TREE_VALUE (level),
7679 args, in_decl, complain,
7680 require_all_args,
7681 use_default_args);
7682 else
7683 l = TMPL_ARGS_LEVEL (args, cur_depth);
7685 if (l == error_mark_node)
7686 return error_mark_node;
7688 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7691 else
7692 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7693 args, in_decl, complain,
7694 require_all_args,
7695 use_default_args);
7696 return coerced_args;
7699 /* Returns 1 if template args OT and NT are equivalent. */
7701 static int
7702 template_args_equal (tree ot, tree nt)
7704 if (nt == ot)
7705 return 1;
7706 if (nt == NULL_TREE || ot == NULL_TREE)
7707 return false;
7709 if (TREE_CODE (nt) == TREE_VEC)
7710 /* For member templates */
7711 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7712 else if (PACK_EXPANSION_P (ot))
7713 return (PACK_EXPANSION_P (nt)
7714 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7715 PACK_EXPANSION_PATTERN (nt))
7716 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7717 PACK_EXPANSION_EXTRA_ARGS (nt)));
7718 else if (ARGUMENT_PACK_P (ot))
7720 int i, len;
7721 tree opack, npack;
7723 if (!ARGUMENT_PACK_P (nt))
7724 return 0;
7726 opack = ARGUMENT_PACK_ARGS (ot);
7727 npack = ARGUMENT_PACK_ARGS (nt);
7728 len = TREE_VEC_LENGTH (opack);
7729 if (TREE_VEC_LENGTH (npack) != len)
7730 return 0;
7731 for (i = 0; i < len; ++i)
7732 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7733 TREE_VEC_ELT (npack, i)))
7734 return 0;
7735 return 1;
7737 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7739 /* We get here probably because we are in the middle of substituting
7740 into the pattern of a pack expansion. In that case the
7741 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7742 interested in. So we want to use the initial pack argument for
7743 the comparison. */
7744 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7745 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7746 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7747 return template_args_equal (ot, nt);
7749 else if (TYPE_P (nt))
7751 if (!TYPE_P (ot))
7752 return false;
7753 /* Don't treat an alias template specialization with dependent
7754 arguments as equivalent to its underlying type when used as a
7755 template argument; we need them to be distinct so that we
7756 substitute into the specialization arguments at instantiation
7757 time. And aliases can't be equivalent without being ==, so
7758 we don't need to look any deeper. */
7759 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
7760 return false;
7761 else
7762 return same_type_p (ot, nt);
7764 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7765 return 0;
7766 else
7768 /* Try to treat a template non-type argument that has been converted
7769 to the parameter type as equivalent to one that hasn't yet. */
7770 for (enum tree_code code1 = TREE_CODE (ot);
7771 CONVERT_EXPR_CODE_P (code1)
7772 || code1 == NON_LVALUE_EXPR;
7773 code1 = TREE_CODE (ot))
7774 ot = TREE_OPERAND (ot, 0);
7775 for (enum tree_code code2 = TREE_CODE (nt);
7776 CONVERT_EXPR_CODE_P (code2)
7777 || code2 == NON_LVALUE_EXPR;
7778 code2 = TREE_CODE (nt))
7779 nt = TREE_OPERAND (nt, 0);
7781 return cp_tree_equal (ot, nt);
7785 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7786 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7787 NEWARG_PTR with the offending arguments if they are non-NULL. */
7789 static int
7790 comp_template_args_with_info (tree oldargs, tree newargs,
7791 tree *oldarg_ptr, tree *newarg_ptr)
7793 int i;
7795 if (oldargs == newargs)
7796 return 1;
7798 if (!oldargs || !newargs)
7799 return 0;
7801 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7802 return 0;
7804 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7806 tree nt = TREE_VEC_ELT (newargs, i);
7807 tree ot = TREE_VEC_ELT (oldargs, i);
7809 if (! template_args_equal (ot, nt))
7811 if (oldarg_ptr != NULL)
7812 *oldarg_ptr = ot;
7813 if (newarg_ptr != NULL)
7814 *newarg_ptr = nt;
7815 return 0;
7818 return 1;
7821 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7822 of template arguments. Returns 0 otherwise. */
7825 comp_template_args (tree oldargs, tree newargs)
7827 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7830 static void
7831 add_pending_template (tree d)
7833 tree ti = (TYPE_P (d)
7834 ? CLASSTYPE_TEMPLATE_INFO (d)
7835 : DECL_TEMPLATE_INFO (d));
7836 struct pending_template *pt;
7837 int level;
7839 if (TI_PENDING_TEMPLATE_FLAG (ti))
7840 return;
7842 /* We are called both from instantiate_decl, where we've already had a
7843 tinst_level pushed, and instantiate_template, where we haven't.
7844 Compensate. */
7845 level = !current_tinst_level || current_tinst_level->decl != d;
7847 if (level)
7848 push_tinst_level (d);
7850 pt = ggc_alloc<pending_template> ();
7851 pt->next = NULL;
7852 pt->tinst = current_tinst_level;
7853 if (last_pending_template)
7854 last_pending_template->next = pt;
7855 else
7856 pending_templates = pt;
7858 last_pending_template = pt;
7860 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7862 if (level)
7863 pop_tinst_level ();
7867 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7868 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7869 documentation for TEMPLATE_ID_EXPR. */
7871 tree
7872 lookup_template_function (tree fns, tree arglist)
7874 tree type;
7876 if (fns == error_mark_node || arglist == error_mark_node)
7877 return error_mark_node;
7879 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7881 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7883 error ("%q#D is not a function template", fns);
7884 return error_mark_node;
7887 if (BASELINK_P (fns))
7889 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7890 unknown_type_node,
7891 BASELINK_FUNCTIONS (fns),
7892 arglist);
7893 return fns;
7896 type = TREE_TYPE (fns);
7897 if (TREE_CODE (fns) == OVERLOAD || !type)
7898 type = unknown_type_node;
7900 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7903 /* Within the scope of a template class S<T>, the name S gets bound
7904 (in build_self_reference) to a TYPE_DECL for the class, not a
7905 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7906 or one of its enclosing classes, and that type is a template,
7907 return the associated TEMPLATE_DECL. Otherwise, the original
7908 DECL is returned.
7910 Also handle the case when DECL is a TREE_LIST of ambiguous
7911 injected-class-names from different bases. */
7913 tree
7914 maybe_get_template_decl_from_type_decl (tree decl)
7916 if (decl == NULL_TREE)
7917 return decl;
7919 /* DR 176: A lookup that finds an injected-class-name (10.2
7920 [class.member.lookup]) can result in an ambiguity in certain cases
7921 (for example, if it is found in more than one base class). If all of
7922 the injected-class-names that are found refer to specializations of
7923 the same class template, and if the name is followed by a
7924 template-argument-list, the reference refers to the class template
7925 itself and not a specialization thereof, and is not ambiguous. */
7926 if (TREE_CODE (decl) == TREE_LIST)
7928 tree t, tmpl = NULL_TREE;
7929 for (t = decl; t; t = TREE_CHAIN (t))
7931 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7932 if (!tmpl)
7933 tmpl = elt;
7934 else if (tmpl != elt)
7935 break;
7937 if (tmpl && t == NULL_TREE)
7938 return tmpl;
7939 else
7940 return decl;
7943 return (decl != NULL_TREE
7944 && DECL_SELF_REFERENCE_P (decl)
7945 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7946 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7949 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7950 parameters, find the desired type.
7952 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7954 IN_DECL, if non-NULL, is the template declaration we are trying to
7955 instantiate.
7957 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7958 the class we are looking up.
7960 Issue error and warning messages under control of COMPLAIN.
7962 If the template class is really a local class in a template
7963 function, then the FUNCTION_CONTEXT is the function in which it is
7964 being instantiated.
7966 ??? Note that this function is currently called *twice* for each
7967 template-id: the first time from the parser, while creating the
7968 incomplete type (finish_template_type), and the second type during the
7969 real instantiation (instantiate_template_class). This is surely something
7970 that we want to avoid. It also causes some problems with argument
7971 coercion (see convert_nontype_argument for more information on this). */
7973 static tree
7974 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7975 int entering_scope, tsubst_flags_t complain)
7977 tree templ = NULL_TREE, parmlist;
7978 tree t;
7979 spec_entry **slot;
7980 spec_entry *entry;
7981 spec_entry elt;
7982 hashval_t hash;
7984 if (identifier_p (d1))
7986 tree value = innermost_non_namespace_value (d1);
7987 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7988 templ = value;
7989 else
7991 if (context)
7992 push_decl_namespace (context);
7993 templ = lookup_name (d1);
7994 templ = maybe_get_template_decl_from_type_decl (templ);
7995 if (context)
7996 pop_decl_namespace ();
7998 if (templ)
7999 context = DECL_CONTEXT (templ);
8001 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8003 tree type = TREE_TYPE (d1);
8005 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8006 an implicit typename for the second A. Deal with it. */
8007 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8008 type = TREE_TYPE (type);
8010 if (CLASSTYPE_TEMPLATE_INFO (type))
8012 templ = CLASSTYPE_TI_TEMPLATE (type);
8013 d1 = DECL_NAME (templ);
8016 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8017 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8019 templ = TYPE_TI_TEMPLATE (d1);
8020 d1 = DECL_NAME (templ);
8022 else if (DECL_TYPE_TEMPLATE_P (d1))
8024 templ = d1;
8025 d1 = DECL_NAME (templ);
8026 context = DECL_CONTEXT (templ);
8028 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8030 templ = d1;
8031 d1 = DECL_NAME (templ);
8034 /* Issue an error message if we didn't find a template. */
8035 if (! templ)
8037 if (complain & tf_error)
8038 error ("%qT is not a template", d1);
8039 return error_mark_node;
8042 if (TREE_CODE (templ) != TEMPLATE_DECL
8043 /* Make sure it's a user visible template, if it was named by
8044 the user. */
8045 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8046 && !PRIMARY_TEMPLATE_P (templ)))
8048 if (complain & tf_error)
8050 error ("non-template type %qT used as a template", d1);
8051 if (in_decl)
8052 error ("for template declaration %q+D", in_decl);
8054 return error_mark_node;
8057 complain &= ~tf_user;
8059 /* An alias that just changes the name of a template is equivalent to the
8060 other template, so if any of the arguments are pack expansions, strip
8061 the alias to avoid problems with a pack expansion passed to a non-pack
8062 alias template parameter (DR 1430). */
8063 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8064 templ = get_underlying_template (templ);
8066 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8068 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
8069 template arguments */
8071 tree parm;
8072 tree arglist2;
8073 tree outer;
8075 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
8077 /* Consider an example where a template template parameter declared as
8079 template <class T, class U = std::allocator<T> > class TT
8081 The template parameter level of T and U are one level larger than
8082 of TT. To proper process the default argument of U, say when an
8083 instantiation `TT<int>' is seen, we need to build the full
8084 arguments containing {int} as the innermost level. Outer levels,
8085 available when not appearing as default template argument, can be
8086 obtained from the arguments of the enclosing template.
8088 Suppose that TT is later substituted with std::vector. The above
8089 instantiation is `TT<int, std::allocator<T> >' with TT at
8090 level 1, and T at level 2, while the template arguments at level 1
8091 becomes {std::vector} and the inner level 2 is {int}. */
8093 outer = DECL_CONTEXT (templ);
8094 if (outer)
8095 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
8096 else if (current_template_parms)
8097 /* This is an argument of the current template, so we haven't set
8098 DECL_CONTEXT yet. */
8099 outer = current_template_args ();
8101 if (outer)
8102 arglist = add_to_template_args (outer, arglist);
8104 arglist2 = coerce_template_parms (parmlist, arglist, templ,
8105 complain,
8106 /*require_all_args=*/true,
8107 /*use_default_args=*/true);
8108 if (arglist2 == error_mark_node
8109 || (!uses_template_parms (arglist2)
8110 && check_instantiated_args (templ, arglist2, complain)))
8111 return error_mark_node;
8113 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8114 return parm;
8116 else
8118 tree template_type = TREE_TYPE (templ);
8119 tree gen_tmpl;
8120 tree type_decl;
8121 tree found = NULL_TREE;
8122 int arg_depth;
8123 int parm_depth;
8124 int is_dependent_type;
8125 int use_partial_inst_tmpl = false;
8127 if (template_type == error_mark_node)
8128 /* An error occurred while building the template TEMPL, and a
8129 diagnostic has most certainly been emitted for that
8130 already. Let's propagate that error. */
8131 return error_mark_node;
8133 gen_tmpl = most_general_template (templ);
8134 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8135 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8136 arg_depth = TMPL_ARGS_DEPTH (arglist);
8138 if (arg_depth == 1 && parm_depth > 1)
8140 /* We've been given an incomplete set of template arguments.
8141 For example, given:
8143 template <class T> struct S1 {
8144 template <class U> struct S2 {};
8145 template <class U> struct S2<U*> {};
8148 we will be called with an ARGLIST of `U*', but the
8149 TEMPLATE will be `template <class T> template
8150 <class U> struct S1<T>::S2'. We must fill in the missing
8151 arguments. */
8152 arglist
8153 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
8154 arglist);
8155 arg_depth = TMPL_ARGS_DEPTH (arglist);
8158 /* Now we should have enough arguments. */
8159 gcc_assert (parm_depth == arg_depth);
8161 /* From here on, we're only interested in the most general
8162 template. */
8164 /* Calculate the BOUND_ARGS. These will be the args that are
8165 actually tsubst'd into the definition to create the
8166 instantiation. */
8167 if (parm_depth > 1)
8169 /* We have multiple levels of arguments to coerce, at once. */
8170 int i;
8171 int saved_depth = TMPL_ARGS_DEPTH (arglist);
8173 tree bound_args = make_tree_vec (parm_depth);
8175 for (i = saved_depth,
8176 t = DECL_TEMPLATE_PARMS (gen_tmpl);
8177 i > 0 && t != NULL_TREE;
8178 --i, t = TREE_CHAIN (t))
8180 tree a;
8181 if (i == saved_depth)
8182 a = coerce_template_parms (TREE_VALUE (t),
8183 arglist, gen_tmpl,
8184 complain,
8185 /*require_all_args=*/true,
8186 /*use_default_args=*/true);
8187 else
8188 /* Outer levels should have already been coerced. */
8189 a = TMPL_ARGS_LEVEL (arglist, i);
8191 /* Don't process further if one of the levels fails. */
8192 if (a == error_mark_node)
8194 /* Restore the ARGLIST to its full size. */
8195 TREE_VEC_LENGTH (arglist) = saved_depth;
8196 return error_mark_node;
8199 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
8201 /* We temporarily reduce the length of the ARGLIST so
8202 that coerce_template_parms will see only the arguments
8203 corresponding to the template parameters it is
8204 examining. */
8205 TREE_VEC_LENGTH (arglist)--;
8208 /* Restore the ARGLIST to its full size. */
8209 TREE_VEC_LENGTH (arglist) = saved_depth;
8211 arglist = bound_args;
8213 else
8214 arglist
8215 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
8216 INNERMOST_TEMPLATE_ARGS (arglist),
8217 gen_tmpl,
8218 complain,
8219 /*require_all_args=*/true,
8220 /*use_default_args=*/true);
8222 if (arglist == error_mark_node)
8223 /* We were unable to bind the arguments. */
8224 return error_mark_node;
8226 /* In the scope of a template class, explicit references to the
8227 template class refer to the type of the template, not any
8228 instantiation of it. For example, in:
8230 template <class T> class C { void f(C<T>); }
8232 the `C<T>' is just the same as `C'. Outside of the
8233 class, however, such a reference is an instantiation. */
8234 if ((entering_scope
8235 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8236 || currently_open_class (template_type))
8237 /* comp_template_args is expensive, check it last. */
8238 && comp_template_args (TYPE_TI_ARGS (template_type),
8239 arglist))
8240 return template_type;
8242 /* If we already have this specialization, return it. */
8243 elt.tmpl = gen_tmpl;
8244 elt.args = arglist;
8245 hash = spec_hasher::hash (&elt);
8246 entry = type_specializations->find_with_hash (&elt, hash);
8248 if (entry)
8249 return entry->spec;
8251 /* If the the template's constraints are not satisfied,
8252 then we cannot form a valid type.
8254 Note that the check is deferred until after the hash
8255 lookup. This prevents redundant checks on previously
8256 instantiated specializations. */
8257 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8259 if (complain & tf_error)
8261 error ("template constraint failure");
8262 diagnose_constraints (input_location, gen_tmpl, arglist);
8264 return error_mark_node;
8267 is_dependent_type = uses_template_parms (arglist);
8269 /* If the deduced arguments are invalid, then the binding
8270 failed. */
8271 if (!is_dependent_type
8272 && check_instantiated_args (gen_tmpl,
8273 INNERMOST_TEMPLATE_ARGS (arglist),
8274 complain))
8275 return error_mark_node;
8277 if (!is_dependent_type
8278 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8279 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8280 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8282 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8283 DECL_NAME (gen_tmpl),
8284 /*tag_scope=*/ts_global);
8285 return found;
8288 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8289 complain, in_decl);
8290 if (context == error_mark_node)
8291 return error_mark_node;
8293 if (!context)
8294 context = global_namespace;
8296 /* Create the type. */
8297 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8299 /* The user referred to a specialization of an alias
8300 template represented by GEN_TMPL.
8302 [temp.alias]/2 says:
8304 When a template-id refers to the specialization of an
8305 alias template, it is equivalent to the associated
8306 type obtained by substitution of its
8307 template-arguments for the template-parameters in the
8308 type-id of the alias template. */
8310 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8311 /* Note that the call above (by indirectly calling
8312 register_specialization in tsubst_decl) registers the
8313 TYPE_DECL representing the specialization of the alias
8314 template. So next time someone substitutes ARGLIST for
8315 the template parms into the alias template (GEN_TMPL),
8316 she'll get that TYPE_DECL back. */
8318 if (t == error_mark_node)
8319 return t;
8321 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8323 if (!is_dependent_type)
8325 set_current_access_from_decl (TYPE_NAME (template_type));
8326 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8327 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8328 arglist, complain, in_decl),
8329 SCOPED_ENUM_P (template_type), NULL);
8331 if (t == error_mark_node)
8332 return t;
8334 else
8336 /* We don't want to call start_enum for this type, since
8337 the values for the enumeration constants may involve
8338 template parameters. And, no one should be interested
8339 in the enumeration constants for such a type. */
8340 t = cxx_make_type (ENUMERAL_TYPE);
8341 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8343 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8344 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8345 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8347 else if (CLASS_TYPE_P (template_type))
8349 t = make_class_type (TREE_CODE (template_type));
8350 CLASSTYPE_DECLARED_CLASS (t)
8351 = CLASSTYPE_DECLARED_CLASS (template_type);
8352 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8353 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
8355 /* A local class. Make sure the decl gets registered properly. */
8356 if (context == current_function_decl)
8357 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8359 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8360 /* This instantiation is another name for the primary
8361 template type. Set the TYPE_CANONICAL field
8362 appropriately. */
8363 TYPE_CANONICAL (t) = template_type;
8364 else if (any_template_arguments_need_structural_equality_p (arglist))
8365 /* Some of the template arguments require structural
8366 equality testing, so this template class requires
8367 structural equality testing. */
8368 SET_TYPE_STRUCTURAL_EQUALITY (t);
8370 else
8371 gcc_unreachable ();
8373 /* If we called start_enum or pushtag above, this information
8374 will already be set up. */
8375 if (!TYPE_NAME (t))
8377 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8379 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8380 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8381 DECL_SOURCE_LOCATION (type_decl)
8382 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8384 else
8385 type_decl = TYPE_NAME (t);
8387 if (CLASS_TYPE_P (template_type))
8389 TREE_PRIVATE (type_decl)
8390 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8391 TREE_PROTECTED (type_decl)
8392 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8393 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8395 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8396 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8400 if (OVERLOAD_TYPE_P (t)
8401 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8403 if (tree attributes
8404 = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (template_type)))
8406 if (!TREE_CHAIN (attributes))
8407 TYPE_ATTRIBUTES (t) = attributes;
8408 else
8409 TYPE_ATTRIBUTES (t)
8410 = build_tree_list (TREE_PURPOSE (attributes),
8411 TREE_VALUE (attributes));
8415 /* Let's consider the explicit specialization of a member
8416 of a class template specialization that is implicitly instantiated,
8417 e.g.:
8418 template<class T>
8419 struct S
8421 template<class U> struct M {}; //#0
8424 template<>
8425 template<>
8426 struct S<int>::M<char> //#1
8428 int i;
8430 [temp.expl.spec]/4 says this is valid.
8432 In this case, when we write:
8433 S<int>::M<char> m;
8435 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8436 the one of #0.
8438 When we encounter #1, we want to store the partial instantiation
8439 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8441 For all cases other than this "explicit specialization of member of a
8442 class template", we just want to store the most general template into
8443 the CLASSTYPE_TI_TEMPLATE of M.
8445 This case of "explicit specialization of member of a class template"
8446 only happens when:
8447 1/ the enclosing class is an instantiation of, and therefore not
8448 the same as, the context of the most general template, and
8449 2/ we aren't looking at the partial instantiation itself, i.e.
8450 the innermost arguments are not the same as the innermost parms of
8451 the most general template.
8453 So it's only when 1/ and 2/ happens that we want to use the partial
8454 instantiation of the member template in lieu of its most general
8455 template. */
8457 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8458 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8459 /* the enclosing class must be an instantiation... */
8460 && CLASS_TYPE_P (context)
8461 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8463 tree partial_inst_args;
8464 TREE_VEC_LENGTH (arglist)--;
8465 ++processing_template_decl;
8466 partial_inst_args =
8467 tsubst (INNERMOST_TEMPLATE_ARGS
8468 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
8469 arglist, complain, NULL_TREE);
8470 --processing_template_decl;
8471 TREE_VEC_LENGTH (arglist)++;
8472 use_partial_inst_tmpl =
8473 /*...and we must not be looking at the partial instantiation
8474 itself. */
8475 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8476 partial_inst_args);
8479 if (!use_partial_inst_tmpl)
8480 /* This case is easy; there are no member templates involved. */
8481 found = gen_tmpl;
8482 else
8484 /* This is a full instantiation of a member template. Find
8485 the partial instantiation of which this is an instance. */
8487 /* Temporarily reduce by one the number of levels in the ARGLIST
8488 so as to avoid comparing the last set of arguments. */
8489 TREE_VEC_LENGTH (arglist)--;
8490 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8491 TREE_VEC_LENGTH (arglist)++;
8492 /* FOUND is either a proper class type, or an alias
8493 template specialization. In the later case, it's a
8494 TYPE_DECL, resulting from the substituting of arguments
8495 for parameters in the TYPE_DECL of the alias template
8496 done earlier. So be careful while getting the template
8497 of FOUND. */
8498 found = TREE_CODE (found) == TYPE_DECL
8499 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
8500 : CLASSTYPE_TI_TEMPLATE (found);
8503 // Build template info for the new specialization.
8504 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8506 elt.spec = t;
8507 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8508 entry = ggc_alloc<spec_entry> ();
8509 *entry = elt;
8510 *slot = entry;
8512 /* Note this use of the partial instantiation so we can check it
8513 later in maybe_process_partial_specialization. */
8514 DECL_TEMPLATE_INSTANTIATIONS (found)
8515 = tree_cons (arglist, t,
8516 DECL_TEMPLATE_INSTANTIATIONS (found));
8518 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8519 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8520 /* Now that the type has been registered on the instantiations
8521 list, we set up the enumerators. Because the enumeration
8522 constants may involve the enumeration type itself, we make
8523 sure to register the type first, and then create the
8524 constants. That way, doing tsubst_expr for the enumeration
8525 constants won't result in recursive calls here; we'll find
8526 the instantiation and exit above. */
8527 tsubst_enum (template_type, t, arglist);
8529 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8530 /* If the type makes use of template parameters, the
8531 code that generates debugging information will crash. */
8532 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8534 /* Possibly limit visibility based on template args. */
8535 TREE_PUBLIC (type_decl) = 1;
8536 determine_visibility (type_decl);
8538 inherit_targ_abi_tags (t);
8540 return t;
8544 /* Wrapper for lookup_template_class_1. */
8546 tree
8547 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8548 int entering_scope, tsubst_flags_t complain)
8550 tree ret;
8551 timevar_push (TV_TEMPLATE_INST);
8552 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8553 entering_scope, complain);
8554 timevar_pop (TV_TEMPLATE_INST);
8555 return ret;
8558 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.
8559 The type of the expression is the unknown_type_node since the
8560 template-id could refer to an explicit or partial specialization. */
8562 tree
8563 lookup_template_variable (tree templ, tree arglist)
8565 tree type = unknown_type_node;
8566 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
8569 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8571 tree
8572 finish_template_variable (tree var)
8574 tree templ = TREE_OPERAND (var, 0);
8576 tree arglist = TREE_OPERAND (var, 1);
8577 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
8578 arglist = add_outermost_template_args (tmpl_args, arglist);
8580 tree parms = DECL_TEMPLATE_PARMS (templ);
8581 tsubst_flags_t complain = tf_warning_or_error;
8582 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
8583 /*req_all*/true,
8584 /*use_default*/true);
8586 /* If a template-id refers to a specialization of a variable
8587 concept, then the expression is true if and only if the
8588 concept's constraints are satisfied by the given template
8589 arguments.
8591 NOTE: This is an extension of Concepts Lite TS that
8592 allows constraints to be used in expressions. */
8593 if (flag_concepts)
8594 if (variable_concept_p (templ) && !uses_template_parms (arglist))
8596 tree decl = DECL_TEMPLATE_RESULT (templ);
8597 return evaluate_variable_concept (decl, arglist);
8600 return instantiate_template (templ, arglist, complain);
8603 struct pair_fn_data
8605 tree_fn_t fn;
8606 void *data;
8607 /* True when we should also visit template parameters that occur in
8608 non-deduced contexts. */
8609 bool include_nondeduced_p;
8610 hash_set<tree> *visited;
8613 /* Called from for_each_template_parm via walk_tree. */
8615 static tree
8616 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8618 tree t = *tp;
8619 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8620 tree_fn_t fn = pfd->fn;
8621 void *data = pfd->data;
8623 if (TYPE_P (t)
8624 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
8625 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
8626 pfd->include_nondeduced_p))
8627 return error_mark_node;
8629 switch (TREE_CODE (t))
8631 case RECORD_TYPE:
8632 if (TYPE_PTRMEMFUNC_P (t))
8633 break;
8634 /* Fall through. */
8636 case UNION_TYPE:
8637 case ENUMERAL_TYPE:
8638 if (!TYPE_TEMPLATE_INFO (t))
8639 *walk_subtrees = 0;
8640 else if (for_each_template_parm (TYPE_TI_ARGS (t),
8641 fn, data, pfd->visited,
8642 pfd->include_nondeduced_p))
8643 return error_mark_node;
8644 break;
8646 case INTEGER_TYPE:
8647 if (for_each_template_parm (TYPE_MIN_VALUE (t),
8648 fn, data, pfd->visited,
8649 pfd->include_nondeduced_p)
8650 || for_each_template_parm (TYPE_MAX_VALUE (t),
8651 fn, data, pfd->visited,
8652 pfd->include_nondeduced_p))
8653 return error_mark_node;
8654 break;
8656 case METHOD_TYPE:
8657 /* Since we're not going to walk subtrees, we have to do this
8658 explicitly here. */
8659 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
8660 pfd->visited, pfd->include_nondeduced_p))
8661 return error_mark_node;
8662 /* Fall through. */
8664 case FUNCTION_TYPE:
8665 /* Check the return type. */
8666 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8667 pfd->include_nondeduced_p))
8668 return error_mark_node;
8670 /* Check the parameter types. Since default arguments are not
8671 instantiated until they are needed, the TYPE_ARG_TYPES may
8672 contain expressions that involve template parameters. But,
8673 no-one should be looking at them yet. And, once they're
8674 instantiated, they don't contain template parameters, so
8675 there's no point in looking at them then, either. */
8677 tree parm;
8679 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8680 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
8681 pfd->visited, pfd->include_nondeduced_p))
8682 return error_mark_node;
8684 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8685 want walk_tree walking into them itself. */
8686 *walk_subtrees = 0;
8688 break;
8690 case TYPEOF_TYPE:
8691 case UNDERLYING_TYPE:
8692 if (pfd->include_nondeduced_p
8693 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
8694 pfd->visited,
8695 pfd->include_nondeduced_p))
8696 return error_mark_node;
8697 break;
8699 case FUNCTION_DECL:
8700 case VAR_DECL:
8701 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
8702 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
8703 pfd->visited, pfd->include_nondeduced_p))
8704 return error_mark_node;
8705 /* Fall through. */
8707 case PARM_DECL:
8708 case CONST_DECL:
8709 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
8710 && for_each_template_parm (DECL_INITIAL (t), fn, data,
8711 pfd->visited, pfd->include_nondeduced_p))
8712 return error_mark_node;
8713 if (DECL_CONTEXT (t)
8714 && pfd->include_nondeduced_p
8715 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
8716 pfd->visited, pfd->include_nondeduced_p))
8717 return error_mark_node;
8718 break;
8720 case BOUND_TEMPLATE_TEMPLATE_PARM:
8721 /* Record template parameters such as `T' inside `TT<T>'. */
8722 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
8723 pfd->include_nondeduced_p))
8724 return error_mark_node;
8725 /* Fall through. */
8727 case TEMPLATE_TEMPLATE_PARM:
8728 case TEMPLATE_TYPE_PARM:
8729 case TEMPLATE_PARM_INDEX:
8730 if (fn && (*fn)(t, data))
8731 return error_mark_node;
8732 else if (!fn)
8733 return error_mark_node;
8734 break;
8736 case TEMPLATE_DECL:
8737 /* A template template parameter is encountered. */
8738 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
8739 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8740 pfd->include_nondeduced_p))
8741 return error_mark_node;
8743 /* Already substituted template template parameter */
8744 *walk_subtrees = 0;
8745 break;
8747 case TYPENAME_TYPE:
8748 if (!fn
8749 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
8750 data, pfd->visited,
8751 pfd->include_nondeduced_p))
8752 return error_mark_node;
8753 break;
8755 case CONSTRUCTOR:
8756 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8757 && pfd->include_nondeduced_p
8758 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8759 (TREE_TYPE (t)), fn, data,
8760 pfd->visited, pfd->include_nondeduced_p))
8761 return error_mark_node;
8762 break;
8764 case INDIRECT_REF:
8765 case COMPONENT_REF:
8766 /* If there's no type, then this thing must be some expression
8767 involving template parameters. */
8768 if (!fn && !TREE_TYPE (t))
8769 return error_mark_node;
8770 break;
8772 case MODOP_EXPR:
8773 case CAST_EXPR:
8774 case IMPLICIT_CONV_EXPR:
8775 case REINTERPRET_CAST_EXPR:
8776 case CONST_CAST_EXPR:
8777 case STATIC_CAST_EXPR:
8778 case DYNAMIC_CAST_EXPR:
8779 case ARROW_EXPR:
8780 case DOTSTAR_EXPR:
8781 case TYPEID_EXPR:
8782 case PSEUDO_DTOR_EXPR:
8783 if (!fn)
8784 return error_mark_node;
8785 break;
8787 default:
8788 break;
8791 /* We didn't find any template parameters we liked. */
8792 return NULL_TREE;
8795 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8796 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8797 call FN with the parameter and the DATA.
8798 If FN returns nonzero, the iteration is terminated, and
8799 for_each_template_parm returns 1. Otherwise, the iteration
8800 continues. If FN never returns a nonzero value, the value
8801 returned by for_each_template_parm is 0. If FN is NULL, it is
8802 considered to be the function which always returns 1.
8804 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8805 parameters that occur in non-deduced contexts. When false, only
8806 visits those template parameters that can be deduced. */
8808 static int
8809 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8810 hash_set<tree> *visited,
8811 bool include_nondeduced_p)
8813 struct pair_fn_data pfd;
8814 int result;
8816 /* Set up. */
8817 pfd.fn = fn;
8818 pfd.data = data;
8819 pfd.include_nondeduced_p = include_nondeduced_p;
8821 /* Walk the tree. (Conceptually, we would like to walk without
8822 duplicates, but for_each_template_parm_r recursively calls
8823 for_each_template_parm, so we would need to reorganize a fair
8824 bit to use walk_tree_without_duplicates, so we keep our own
8825 visited list.) */
8826 if (visited)
8827 pfd.visited = visited;
8828 else
8829 pfd.visited = new hash_set<tree>;
8830 result = cp_walk_tree (&t,
8831 for_each_template_parm_r,
8832 &pfd,
8833 pfd.visited) != NULL_TREE;
8835 /* Clean up. */
8836 if (!visited)
8838 delete pfd.visited;
8839 pfd.visited = 0;
8842 return result;
8845 /* Returns true if T depends on any template parameter. */
8848 uses_template_parms (tree t)
8850 if (t == NULL_TREE)
8851 return false;
8853 bool dependent_p;
8854 int saved_processing_template_decl;
8856 saved_processing_template_decl = processing_template_decl;
8857 if (!saved_processing_template_decl)
8858 processing_template_decl = 1;
8859 if (TYPE_P (t))
8860 dependent_p = dependent_type_p (t);
8861 else if (TREE_CODE (t) == TREE_VEC)
8862 dependent_p = any_dependent_template_arguments_p (t);
8863 else if (TREE_CODE (t) == TREE_LIST)
8864 dependent_p = (uses_template_parms (TREE_VALUE (t))
8865 || uses_template_parms (TREE_CHAIN (t)));
8866 else if (TREE_CODE (t) == TYPE_DECL)
8867 dependent_p = dependent_type_p (TREE_TYPE (t));
8868 else if (DECL_P (t)
8869 || EXPR_P (t)
8870 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8871 || TREE_CODE (t) == OVERLOAD
8872 || BASELINK_P (t)
8873 || identifier_p (t)
8874 || TREE_CODE (t) == TRAIT_EXPR
8875 || TREE_CODE (t) == CONSTRUCTOR
8876 || CONSTANT_CLASS_P (t))
8877 dependent_p = (type_dependent_expression_p (t)
8878 || value_dependent_expression_p (t));
8879 else
8881 gcc_assert (t == error_mark_node);
8882 dependent_p = false;
8885 processing_template_decl = saved_processing_template_decl;
8887 return dependent_p;
8890 /* Returns true iff current_function_decl is an incompletely instantiated
8891 template. Useful instead of processing_template_decl because the latter
8892 is set to 0 during instantiate_non_dependent_expr. */
8894 bool
8895 in_template_function (void)
8897 tree fn = current_function_decl;
8898 bool ret;
8899 ++processing_template_decl;
8900 ret = (fn && DECL_LANG_SPECIFIC (fn)
8901 && DECL_TEMPLATE_INFO (fn)
8902 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8903 --processing_template_decl;
8904 return ret;
8907 /* Returns true if T depends on any template parameter with level LEVEL. */
8910 uses_template_parms_level (tree t, int level)
8912 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8913 /*include_nondeduced_p=*/true);
8916 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8917 ill-formed translation unit, i.e. a variable or function that isn't
8918 usable in a constant expression. */
8920 static inline bool
8921 neglectable_inst_p (tree d)
8923 return (DECL_P (d)
8924 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8925 : decl_maybe_constant_var_p (d)));
8928 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8929 neglectable and instantiated from within an erroneous instantiation. */
8931 static bool
8932 limit_bad_template_recursion (tree decl)
8934 struct tinst_level *lev = current_tinst_level;
8935 int errs = errorcount + sorrycount;
8936 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8937 return false;
8939 for (; lev; lev = lev->next)
8940 if (neglectable_inst_p (lev->decl))
8941 break;
8943 return (lev && errs > lev->errors);
8946 static int tinst_depth;
8947 extern int max_tinst_depth;
8948 int depth_reached;
8950 static GTY(()) struct tinst_level *last_error_tinst_level;
8952 /* We're starting to instantiate D; record the template instantiation context
8953 for diagnostics and to restore it later. */
8955 bool
8956 push_tinst_level (tree d)
8958 return push_tinst_level_loc (d, input_location);
8961 /* We're starting to instantiate D; record the template instantiation context
8962 at LOC for diagnostics and to restore it later. */
8964 bool
8965 push_tinst_level_loc (tree d, location_t loc)
8967 struct tinst_level *new_level;
8969 if (tinst_depth >= max_tinst_depth)
8971 fatal_error (input_location,
8972 "template instantiation depth exceeds maximum of %d"
8973 " (use -ftemplate-depth= to increase the maximum)",
8974 max_tinst_depth);
8975 return false;
8978 /* If the current instantiation caused problems, don't let it instantiate
8979 anything else. Do allow deduction substitution and decls usable in
8980 constant expressions. */
8981 if (limit_bad_template_recursion (d))
8982 return false;
8984 new_level = ggc_alloc<tinst_level> ();
8985 new_level->decl = d;
8986 new_level->locus = loc;
8987 new_level->errors = errorcount+sorrycount;
8988 new_level->in_system_header_p = in_system_header_at (input_location);
8989 new_level->next = current_tinst_level;
8990 current_tinst_level = new_level;
8992 ++tinst_depth;
8993 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8994 depth_reached = tinst_depth;
8996 return true;
8999 /* We're done instantiating this template; return to the instantiation
9000 context. */
9002 void
9003 pop_tinst_level (void)
9005 /* Restore the filename and line number stashed away when we started
9006 this instantiation. */
9007 input_location = current_tinst_level->locus;
9008 current_tinst_level = current_tinst_level->next;
9009 --tinst_depth;
9012 /* We're instantiating a deferred template; restore the template
9013 instantiation context in which the instantiation was requested, which
9014 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9016 static tree
9017 reopen_tinst_level (struct tinst_level *level)
9019 struct tinst_level *t;
9021 tinst_depth = 0;
9022 for (t = level; t; t = t->next)
9023 ++tinst_depth;
9025 current_tinst_level = level;
9026 pop_tinst_level ();
9027 if (current_tinst_level)
9028 current_tinst_level->errors = errorcount+sorrycount;
9029 return level->decl;
9032 /* Returns the TINST_LEVEL which gives the original instantiation
9033 context. */
9035 struct tinst_level *
9036 outermost_tinst_level (void)
9038 struct tinst_level *level = current_tinst_level;
9039 if (level)
9040 while (level->next)
9041 level = level->next;
9042 return level;
9045 /* Returns TRUE if the t is a parameter that will be
9046 expanded into a sequence of arguments, when an argument
9047 pack is substituted into it.
9049 These arise from requires-expressions, where substitution
9050 during normalization produces unexpanded parameters.
9051 For example:
9053 template<typename T, typename... Args>
9054 concept bool C() {
9055 return requires (T t, Args... args) {
9056 t(args...);
9060 tempate<typename X, typename... As>
9061 requires C<X, As...>
9062 struct S { };
9064 When S's constraint is normalized the parameterized expression
9065 is a parameter args whose type is the type pack expansion
9066 As... When we instantiate S and provide a concrete argument
9067 pack, we need to make sure that references to args... are
9068 expanded correctly. */
9070 static inline bool
9071 pending_expansion_p (tree t)
9073 return (TREE_CODE (t) == PARM_DECL && CONSTRAINT_VAR_P (t)
9074 && PACK_EXPANSION_P (TREE_TYPE (t)));
9077 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9078 vector of template arguments, as for tsubst.
9080 Returns an appropriate tsubst'd friend declaration. */
9082 static tree
9083 tsubst_friend_function (tree decl, tree args)
9085 tree new_friend;
9087 if (TREE_CODE (decl) == FUNCTION_DECL
9088 && DECL_TEMPLATE_INSTANTIATION (decl)
9089 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9090 /* This was a friend declared with an explicit template
9091 argument list, e.g.:
9093 friend void f<>(T);
9095 to indicate that f was a template instantiation, not a new
9096 function declaration. Now, we have to figure out what
9097 instantiation of what template. */
9099 tree template_id, arglist, fns;
9100 tree new_args;
9101 tree tmpl;
9102 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9104 /* Friend functions are looked up in the containing namespace scope.
9105 We must enter that scope, to avoid finding member functions of the
9106 current class with same name. */
9107 push_nested_namespace (ns);
9108 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9109 tf_warning_or_error, NULL_TREE,
9110 /*integral_constant_expression_p=*/false);
9111 pop_nested_namespace (ns);
9112 arglist = tsubst (DECL_TI_ARGS (decl), args,
9113 tf_warning_or_error, NULL_TREE);
9114 template_id = lookup_template_function (fns, arglist);
9116 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9117 tmpl = determine_specialization (template_id, new_friend,
9118 &new_args,
9119 /*need_member_template=*/0,
9120 TREE_VEC_LENGTH (args),
9121 tsk_none);
9122 return instantiate_template (tmpl, new_args, tf_error);
9125 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9127 /* The NEW_FRIEND will look like an instantiation, to the
9128 compiler, but is not an instantiation from the point of view of
9129 the language. For example, we might have had:
9131 template <class T> struct S {
9132 template <class U> friend void f(T, U);
9135 Then, in S<int>, template <class U> void f(int, U) is not an
9136 instantiation of anything. */
9137 if (new_friend == error_mark_node)
9138 return error_mark_node;
9140 DECL_USE_TEMPLATE (new_friend) = 0;
9141 if (TREE_CODE (decl) == TEMPLATE_DECL)
9143 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9144 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9145 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9148 /* The mangled name for the NEW_FRIEND is incorrect. The function
9149 is not a template instantiation and should not be mangled like
9150 one. Therefore, we forget the mangling here; we'll recompute it
9151 later if we need it. */
9152 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9154 SET_DECL_RTL (new_friend, NULL);
9155 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9158 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9160 tree old_decl;
9161 tree new_friend_template_info;
9162 tree new_friend_result_template_info;
9163 tree ns;
9164 int new_friend_is_defn;
9166 /* We must save some information from NEW_FRIEND before calling
9167 duplicate decls since that function will free NEW_FRIEND if
9168 possible. */
9169 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9170 new_friend_is_defn =
9171 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9172 (template_for_substitution (new_friend)))
9173 != NULL_TREE);
9174 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9176 /* This declaration is a `primary' template. */
9177 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9179 new_friend_result_template_info
9180 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9182 else
9183 new_friend_result_template_info = NULL_TREE;
9185 /* Make the init_value nonzero so pushdecl knows this is a defn. */
9186 if (new_friend_is_defn)
9187 DECL_INITIAL (new_friend) = error_mark_node;
9189 /* Inside pushdecl_namespace_level, we will push into the
9190 current namespace. However, the friend function should go
9191 into the namespace of the template. */
9192 ns = decl_namespace_context (new_friend);
9193 push_nested_namespace (ns);
9194 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9195 pop_nested_namespace (ns);
9197 if (old_decl == error_mark_node)
9198 return error_mark_node;
9200 if (old_decl != new_friend)
9202 /* This new friend declaration matched an existing
9203 declaration. For example, given:
9205 template <class T> void f(T);
9206 template <class U> class C {
9207 template <class T> friend void f(T) {}
9210 the friend declaration actually provides the definition
9211 of `f', once C has been instantiated for some type. So,
9212 old_decl will be the out-of-class template declaration,
9213 while new_friend is the in-class definition.
9215 But, if `f' was called before this point, the
9216 instantiation of `f' will have DECL_TI_ARGS corresponding
9217 to `T' but not to `U', references to which might appear
9218 in the definition of `f'. Previously, the most general
9219 template for an instantiation of `f' was the out-of-class
9220 version; now it is the in-class version. Therefore, we
9221 run through all specialization of `f', adding to their
9222 DECL_TI_ARGS appropriately. In particular, they need a
9223 new set of outer arguments, corresponding to the
9224 arguments for this class instantiation.
9226 The same situation can arise with something like this:
9228 friend void f(int);
9229 template <class T> class C {
9230 friend void f(T) {}
9233 when `C<int>' is instantiated. Now, `f(int)' is defined
9234 in the class. */
9236 if (!new_friend_is_defn)
9237 /* On the other hand, if the in-class declaration does
9238 *not* provide a definition, then we don't want to alter
9239 existing definitions. We can just leave everything
9240 alone. */
9242 else
9244 tree new_template = TI_TEMPLATE (new_friend_template_info);
9245 tree new_args = TI_ARGS (new_friend_template_info);
9247 /* Overwrite whatever template info was there before, if
9248 any, with the new template information pertaining to
9249 the declaration. */
9250 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9252 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9254 /* We should have called reregister_specialization in
9255 duplicate_decls. */
9256 gcc_assert (retrieve_specialization (new_template,
9257 new_args, 0)
9258 == old_decl);
9260 /* Instantiate it if the global has already been used. */
9261 if (DECL_ODR_USED (old_decl))
9262 instantiate_decl (old_decl, /*defer_ok=*/true,
9263 /*expl_inst_class_mem_p=*/false);
9265 else
9267 tree t;
9269 /* Indicate that the old function template is a partial
9270 instantiation. */
9271 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9272 = new_friend_result_template_info;
9274 gcc_assert (new_template
9275 == most_general_template (new_template));
9276 gcc_assert (new_template != old_decl);
9278 /* Reassign any specializations already in the hash table
9279 to the new more general template, and add the
9280 additional template args. */
9281 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9282 t != NULL_TREE;
9283 t = TREE_CHAIN (t))
9285 tree spec = TREE_VALUE (t);
9286 spec_entry elt;
9288 elt.tmpl = old_decl;
9289 elt.args = DECL_TI_ARGS (spec);
9290 elt.spec = NULL_TREE;
9292 decl_specializations->remove_elt (&elt);
9294 DECL_TI_ARGS (spec)
9295 = add_outermost_template_args (new_args,
9296 DECL_TI_ARGS (spec));
9298 register_specialization
9299 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9302 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9306 /* The information from NEW_FRIEND has been merged into OLD_DECL
9307 by duplicate_decls. */
9308 new_friend = old_decl;
9311 else
9313 tree context = DECL_CONTEXT (new_friend);
9314 bool dependent_p;
9316 /* In the code
9317 template <class T> class C {
9318 template <class U> friend void C1<U>::f (); // case 1
9319 friend void C2<T>::f (); // case 2
9321 we only need to make sure CONTEXT is a complete type for
9322 case 2. To distinguish between the two cases, we note that
9323 CONTEXT of case 1 remains dependent type after tsubst while
9324 this isn't true for case 2. */
9325 ++processing_template_decl;
9326 dependent_p = dependent_type_p (context);
9327 --processing_template_decl;
9329 if (!dependent_p
9330 && !complete_type_or_else (context, NULL_TREE))
9331 return error_mark_node;
9333 if (COMPLETE_TYPE_P (context))
9335 tree fn = new_friend;
9336 /* do_friend adds the TEMPLATE_DECL for any member friend
9337 template even if it isn't a member template, i.e.
9338 template <class T> friend A<T>::f();
9339 Look through it in that case. */
9340 if (TREE_CODE (fn) == TEMPLATE_DECL
9341 && !PRIMARY_TEMPLATE_P (fn))
9342 fn = DECL_TEMPLATE_RESULT (fn);
9343 /* Check to see that the declaration is really present, and,
9344 possibly obtain an improved declaration. */
9345 fn = check_classfn (context, fn, NULL_TREE);
9347 if (fn)
9348 new_friend = fn;
9352 return new_friend;
9355 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9356 template arguments, as for tsubst.
9358 Returns an appropriate tsubst'd friend type or error_mark_node on
9359 failure. */
9361 static tree
9362 tsubst_friend_class (tree friend_tmpl, tree args)
9364 tree friend_type;
9365 tree tmpl;
9366 tree context;
9368 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9370 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9371 return TREE_TYPE (t);
9374 context = CP_DECL_CONTEXT (friend_tmpl);
9376 if (context != global_namespace)
9378 if (TREE_CODE (context) == NAMESPACE_DECL)
9379 push_nested_namespace (context);
9380 else
9381 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9384 /* Look for a class template declaration. We look for hidden names
9385 because two friend declarations of the same template are the
9386 same. For example, in:
9388 struct A {
9389 template <typename> friend class F;
9391 template <typename> struct B {
9392 template <typename> friend class F;
9395 both F templates are the same. */
9396 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9397 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9399 /* But, if we don't find one, it might be because we're in a
9400 situation like this:
9402 template <class T>
9403 struct S {
9404 template <class U>
9405 friend struct S;
9408 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9409 for `S<int>', not the TEMPLATE_DECL. */
9410 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9412 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9413 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9416 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9418 /* The friend template has already been declared. Just
9419 check to see that the declarations match, and install any new
9420 default parameters. We must tsubst the default parameters,
9421 of course. We only need the innermost template parameters
9422 because that is all that redeclare_class_template will look
9423 at. */
9424 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9425 > TMPL_ARGS_DEPTH (args))
9427 tree parms;
9428 location_t saved_input_location;
9429 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9430 args, tf_warning_or_error);
9432 saved_input_location = input_location;
9433 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9434 tree cons = get_constraints (tmpl);
9435 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9436 input_location = saved_input_location;
9440 friend_type = TREE_TYPE (tmpl);
9442 else
9444 /* The friend template has not already been declared. In this
9445 case, the instantiation of the template class will cause the
9446 injection of this template into the global scope. */
9447 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9448 if (tmpl == error_mark_node)
9449 return error_mark_node;
9451 /* The new TMPL is not an instantiation of anything, so we
9452 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9453 the new type because that is supposed to be the corresponding
9454 template decl, i.e., TMPL. */
9455 DECL_USE_TEMPLATE (tmpl) = 0;
9456 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9457 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9458 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9459 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9461 /* Inject this template into the global scope. */
9462 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
9465 if (context != global_namespace)
9467 if (TREE_CODE (context) == NAMESPACE_DECL)
9468 pop_nested_namespace (context);
9469 else
9470 pop_nested_class ();
9473 return friend_type;
9476 /* Returns zero if TYPE cannot be completed later due to circularity.
9477 Otherwise returns one. */
9479 static int
9480 can_complete_type_without_circularity (tree type)
9482 if (type == NULL_TREE || type == error_mark_node)
9483 return 0;
9484 else if (COMPLETE_TYPE_P (type))
9485 return 1;
9486 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
9487 return can_complete_type_without_circularity (TREE_TYPE (type));
9488 else if (CLASS_TYPE_P (type)
9489 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9490 return 0;
9491 else
9492 return 1;
9495 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
9497 /* Apply any attributes which had to be deferred until instantiation
9498 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9499 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9501 static void
9502 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
9503 tree args, tsubst_flags_t complain, tree in_decl)
9505 tree last_dep = NULL_TREE;
9506 tree t;
9507 tree *p;
9509 for (t = attributes; t; t = TREE_CHAIN (t))
9510 if (ATTR_IS_DEPENDENT (t))
9512 last_dep = t;
9513 attributes = copy_list (attributes);
9514 break;
9517 if (DECL_P (*decl_p))
9519 if (TREE_TYPE (*decl_p) == error_mark_node)
9520 return;
9521 p = &DECL_ATTRIBUTES (*decl_p);
9523 else
9524 p = &TYPE_ATTRIBUTES (*decl_p);
9526 if (last_dep)
9528 tree late_attrs = NULL_TREE;
9529 tree *q = &late_attrs;
9531 for (*p = attributes; *p; )
9533 t = *p;
9534 if (ATTR_IS_DEPENDENT (t))
9536 *p = TREE_CHAIN (t);
9537 TREE_CHAIN (t) = NULL_TREE;
9538 if ((flag_openmp || flag_cilkplus)
9539 && is_attribute_p ("omp declare simd",
9540 get_attribute_name (t))
9541 && TREE_VALUE (t))
9543 tree clauses = TREE_VALUE (TREE_VALUE (t));
9544 clauses = tsubst_omp_clauses (clauses, true, args,
9545 complain, in_decl);
9546 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9547 clauses = finish_omp_clauses (clauses);
9548 tree parms = DECL_ARGUMENTS (*decl_p);
9549 clauses
9550 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9551 if (clauses)
9552 TREE_VALUE (TREE_VALUE (t)) = clauses;
9553 else
9554 TREE_VALUE (t) = NULL_TREE;
9556 /* If the first attribute argument is an identifier, don't
9557 pass it through tsubst. Attributes like mode, format,
9558 cleanup and several target specific attributes expect it
9559 unmodified. */
9560 else if (attribute_takes_identifier_p (get_attribute_name (t))
9561 && TREE_VALUE (t))
9563 tree chain
9564 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
9565 in_decl,
9566 /*integral_constant_expression_p=*/false);
9567 if (chain != TREE_CHAIN (TREE_VALUE (t)))
9568 TREE_VALUE (t)
9569 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
9570 chain);
9572 else
9573 TREE_VALUE (t)
9574 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
9575 /*integral_constant_expression_p=*/false);
9576 *q = t;
9577 q = &TREE_CHAIN (t);
9579 else
9580 p = &TREE_CHAIN (t);
9583 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
9587 /* Perform (or defer) access check for typedefs that were referenced
9588 from within the template TMPL code.
9589 This is a subroutine of instantiate_decl and instantiate_class_template.
9590 TMPL is the template to consider and TARGS is the list of arguments of
9591 that template. */
9593 static void
9594 perform_typedefs_access_check (tree tmpl, tree targs)
9596 location_t saved_location;
9597 unsigned i;
9598 qualified_typedef_usage_t *iter;
9600 if (!tmpl
9601 || (!CLASS_TYPE_P (tmpl)
9602 && TREE_CODE (tmpl) != FUNCTION_DECL))
9603 return;
9605 saved_location = input_location;
9606 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
9608 tree type_decl = iter->typedef_decl;
9609 tree type_scope = iter->context;
9611 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
9612 continue;
9614 if (uses_template_parms (type_decl))
9615 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
9616 if (uses_template_parms (type_scope))
9617 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
9619 /* Make access check error messages point to the location
9620 of the use of the typedef. */
9621 input_location = iter->locus;
9622 perform_or_defer_access_check (TYPE_BINFO (type_scope),
9623 type_decl, type_decl,
9624 tf_warning_or_error);
9626 input_location = saved_location;
9629 static tree
9630 instantiate_class_template_1 (tree type)
9632 tree templ, args, pattern, t, member;
9633 tree typedecl;
9634 tree pbinfo;
9635 tree base_list;
9636 unsigned int saved_maximum_field_alignment;
9637 tree fn_context;
9639 if (type == error_mark_node)
9640 return error_mark_node;
9642 if (COMPLETE_OR_OPEN_TYPE_P (type)
9643 || uses_template_parms (type))
9644 return type;
9646 /* Figure out which template is being instantiated. */
9647 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9648 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9650 /* Determine what specialization of the original template to
9651 instantiate. */
9652 t = most_specialized_partial_spec (type, tf_warning_or_error);
9653 if (t == error_mark_node)
9655 TYPE_BEING_DEFINED (type) = 1;
9656 return error_mark_node;
9658 else if (t)
9660 /* This TYPE is actually an instantiation of a partial
9661 specialization. We replace the innermost set of ARGS with
9662 the arguments appropriate for substitution. For example,
9663 given:
9665 template <class T> struct S {};
9666 template <class T> struct S<T*> {};
9668 and supposing that we are instantiating S<int*>, ARGS will
9669 presently be {int*} -- but we need {int}. */
9670 pattern = TREE_TYPE (t);
9671 args = TREE_PURPOSE (t);
9673 else
9675 pattern = TREE_TYPE (templ);
9676 args = CLASSTYPE_TI_ARGS (type);
9679 /* If the template we're instantiating is incomplete, then clearly
9680 there's nothing we can do. */
9681 if (!COMPLETE_TYPE_P (pattern))
9682 return type;
9684 /* If we've recursively instantiated too many templates, stop. */
9685 if (! push_tinst_level (type))
9686 return type;
9688 /* Now we're really doing the instantiation. Mark the type as in
9689 the process of being defined. */
9690 TYPE_BEING_DEFINED (type) = 1;
9692 /* We may be in the middle of deferred access check. Disable
9693 it now. */
9694 push_deferring_access_checks (dk_no_deferred);
9696 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9697 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9698 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
9699 fn_context = error_mark_node;
9700 if (!fn_context)
9701 push_to_top_level ();
9702 /* Use #pragma pack from the template context. */
9703 saved_maximum_field_alignment = maximum_field_alignment;
9704 maximum_field_alignment = TYPE_PRECISION (pattern);
9706 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
9708 /* Set the input location to the most specialized template definition.
9709 This is needed if tsubsting causes an error. */
9710 typedecl = TYPE_MAIN_DECL (pattern);
9711 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
9712 DECL_SOURCE_LOCATION (typedecl);
9714 TYPE_PACKED (type) = TYPE_PACKED (pattern);
9715 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
9716 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
9717 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
9718 if (ANON_AGGR_TYPE_P (pattern))
9719 SET_ANON_AGGR_TYPE_P (type);
9720 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
9722 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
9723 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
9724 /* Adjust visibility for template arguments. */
9725 determine_visibility (TYPE_MAIN_DECL (type));
9727 if (CLASS_TYPE_P (type))
9728 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
9730 pbinfo = TYPE_BINFO (pattern);
9732 /* We should never instantiate a nested class before its enclosing
9733 class; we need to look up the nested class by name before we can
9734 instantiate it, and that lookup should instantiate the enclosing
9735 class. */
9736 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
9737 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
9739 base_list = NULL_TREE;
9740 if (BINFO_N_BASE_BINFOS (pbinfo))
9742 tree pbase_binfo;
9743 tree pushed_scope;
9744 int i;
9746 /* We must enter the scope containing the type, as that is where
9747 the accessibility of types named in dependent bases are
9748 looked up from. */
9749 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
9751 /* Substitute into each of the bases to determine the actual
9752 basetypes. */
9753 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
9755 tree base;
9756 tree access = BINFO_BASE_ACCESS (pbinfo, i);
9757 tree expanded_bases = NULL_TREE;
9758 int idx, len = 1;
9760 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
9762 expanded_bases =
9763 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
9764 args, tf_error, NULL_TREE);
9765 if (expanded_bases == error_mark_node)
9766 continue;
9768 len = TREE_VEC_LENGTH (expanded_bases);
9771 for (idx = 0; idx < len; idx++)
9773 if (expanded_bases)
9774 /* Extract the already-expanded base class. */
9775 base = TREE_VEC_ELT (expanded_bases, idx);
9776 else
9777 /* Substitute to figure out the base class. */
9778 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9779 NULL_TREE);
9781 if (base == error_mark_node)
9782 continue;
9784 base_list = tree_cons (access, base, base_list);
9785 if (BINFO_VIRTUAL_P (pbase_binfo))
9786 TREE_TYPE (base_list) = integer_type_node;
9790 /* The list is now in reverse order; correct that. */
9791 base_list = nreverse (base_list);
9793 if (pushed_scope)
9794 pop_scope (pushed_scope);
9796 /* Now call xref_basetypes to set up all the base-class
9797 information. */
9798 xref_basetypes (type, base_list);
9800 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9801 (int) ATTR_FLAG_TYPE_IN_PLACE,
9802 args, tf_error, NULL_TREE);
9803 fixup_attribute_variants (type);
9805 /* Now that our base classes are set up, enter the scope of the
9806 class, so that name lookups into base classes, etc. will work
9807 correctly. This is precisely analogous to what we do in
9808 begin_class_definition when defining an ordinary non-template
9809 class, except we also need to push the enclosing classes. */
9810 push_nested_class (type);
9812 /* Now members are processed in the order of declaration. */
9813 for (member = CLASSTYPE_DECL_LIST (pattern);
9814 member; member = TREE_CHAIN (member))
9816 tree t = TREE_VALUE (member);
9818 if (TREE_PURPOSE (member))
9820 if (TYPE_P (t))
9822 /* Build new CLASSTYPE_NESTED_UTDS. */
9824 tree newtag;
9825 bool class_template_p;
9827 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9828 && TYPE_LANG_SPECIFIC (t)
9829 && CLASSTYPE_IS_TEMPLATE (t));
9830 /* If the member is a class template, then -- even after
9831 substitution -- there may be dependent types in the
9832 template argument list for the class. We increment
9833 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9834 that function will assume that no types are dependent
9835 when outside of a template. */
9836 if (class_template_p)
9837 ++processing_template_decl;
9838 newtag = tsubst (t, args, tf_error, NULL_TREE);
9839 if (class_template_p)
9840 --processing_template_decl;
9841 if (newtag == error_mark_node)
9842 continue;
9844 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9846 tree name = TYPE_IDENTIFIER (t);
9848 if (class_template_p)
9849 /* Unfortunately, lookup_template_class sets
9850 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9851 instantiation (i.e., for the type of a member
9852 template class nested within a template class.)
9853 This behavior is required for
9854 maybe_process_partial_specialization to work
9855 correctly, but is not accurate in this case;
9856 the TAG is not an instantiation of anything.
9857 (The corresponding TEMPLATE_DECL is an
9858 instantiation, but the TYPE is not.) */
9859 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9861 /* Now, we call pushtag to put this NEWTAG into the scope of
9862 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9863 pushtag calling push_template_decl. We don't have to do
9864 this for enums because it will already have been done in
9865 tsubst_enum. */
9866 if (name)
9867 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9868 pushtag (name, newtag, /*tag_scope=*/ts_current);
9871 else if (DECL_DECLARES_FUNCTION_P (t))
9873 /* Build new TYPE_METHODS. */
9874 tree r;
9876 if (TREE_CODE (t) == TEMPLATE_DECL)
9877 ++processing_template_decl;
9878 r = tsubst (t, args, tf_error, NULL_TREE);
9879 if (TREE_CODE (t) == TEMPLATE_DECL)
9880 --processing_template_decl;
9881 set_current_access_from_decl (r);
9882 finish_member_declaration (r);
9883 /* Instantiate members marked with attribute used. */
9884 if (r != error_mark_node && DECL_PRESERVE_P (r))
9885 mark_used (r);
9886 if (TREE_CODE (r) == FUNCTION_DECL
9887 && DECL_OMP_DECLARE_REDUCTION_P (r))
9888 cp_check_omp_declare_reduction (r);
9890 else if (DECL_CLASS_TEMPLATE_P (t)
9891 && LAMBDA_TYPE_P (TREE_TYPE (t)))
9892 /* A closure type for a lambda in a default argument for a
9893 member template. Ignore it; it will be instantiated with
9894 the default argument. */;
9895 else
9897 /* Build new TYPE_FIELDS. */
9898 if (TREE_CODE (t) == STATIC_ASSERT)
9900 tree condition;
9902 ++c_inhibit_evaluation_warnings;
9903 condition =
9904 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9905 tf_warning_or_error, NULL_TREE,
9906 /*integral_constant_expression_p=*/true);
9907 --c_inhibit_evaluation_warnings;
9909 finish_static_assert (condition,
9910 STATIC_ASSERT_MESSAGE (t),
9911 STATIC_ASSERT_SOURCE_LOCATION (t),
9912 /*member_p=*/true);
9914 else if (TREE_CODE (t) != CONST_DECL)
9916 tree r;
9917 tree vec = NULL_TREE;
9918 int len = 1;
9920 /* The file and line for this declaration, to
9921 assist in error message reporting. Since we
9922 called push_tinst_level above, we don't need to
9923 restore these. */
9924 input_location = DECL_SOURCE_LOCATION (t);
9926 if (TREE_CODE (t) == TEMPLATE_DECL)
9927 ++processing_template_decl;
9928 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9929 if (TREE_CODE (t) == TEMPLATE_DECL)
9930 --processing_template_decl;
9932 if (TREE_CODE (r) == TREE_VEC)
9934 /* A capture pack became multiple fields. */
9935 vec = r;
9936 len = TREE_VEC_LENGTH (vec);
9939 for (int i = 0; i < len; ++i)
9941 if (vec)
9942 r = TREE_VEC_ELT (vec, i);
9943 if (VAR_P (r))
9945 /* In [temp.inst]:
9947 [t]he initialization (and any associated
9948 side-effects) of a static data member does
9949 not occur unless the static data member is
9950 itself used in a way that requires the
9951 definition of the static data member to
9952 exist.
9954 Therefore, we do not substitute into the
9955 initialized for the static data member here. */
9956 finish_static_data_member_decl
9958 /*init=*/NULL_TREE,
9959 /*init_const_expr_p=*/false,
9960 /*asmspec_tree=*/NULL_TREE,
9961 /*flags=*/0);
9962 /* Instantiate members marked with attribute used. */
9963 if (r != error_mark_node && DECL_PRESERVE_P (r))
9964 mark_used (r);
9966 else if (TREE_CODE (r) == FIELD_DECL)
9968 /* Determine whether R has a valid type and can be
9969 completed later. If R is invalid, then its type
9970 is replaced by error_mark_node. */
9971 tree rtype = TREE_TYPE (r);
9972 if (can_complete_type_without_circularity (rtype))
9973 complete_type (rtype);
9975 if (!COMPLETE_TYPE_P (rtype))
9977 cxx_incomplete_type_error (r, rtype);
9978 TREE_TYPE (r) = error_mark_node;
9982 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9983 such a thing will already have been added to the field
9984 list by tsubst_enum in finish_member_declaration in the
9985 CLASSTYPE_NESTED_UTDS case above. */
9986 if (!(TREE_CODE (r) == TYPE_DECL
9987 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9988 && DECL_ARTIFICIAL (r)))
9990 set_current_access_from_decl (r);
9991 finish_member_declaration (r);
9997 else
9999 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10000 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10002 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10004 tree friend_type = t;
10005 bool adjust_processing_template_decl = false;
10007 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10009 /* template <class T> friend class C; */
10010 friend_type = tsubst_friend_class (friend_type, args);
10011 adjust_processing_template_decl = true;
10013 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10015 /* template <class T> friend class C::D; */
10016 friend_type = tsubst (friend_type, args,
10017 tf_warning_or_error, NULL_TREE);
10018 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10019 friend_type = TREE_TYPE (friend_type);
10020 adjust_processing_template_decl = true;
10022 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10023 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10025 /* This could be either
10027 friend class T::C;
10029 when dependent_type_p is false or
10031 template <class U> friend class T::C;
10033 otherwise. */
10034 friend_type = tsubst (friend_type, args,
10035 tf_warning_or_error, NULL_TREE);
10036 /* Bump processing_template_decl for correct
10037 dependent_type_p calculation. */
10038 ++processing_template_decl;
10039 if (dependent_type_p (friend_type))
10040 adjust_processing_template_decl = true;
10041 --processing_template_decl;
10043 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10044 && hidden_name_p (TYPE_NAME (friend_type)))
10046 /* friend class C;
10048 where C hasn't been declared yet. Let's lookup name
10049 from namespace scope directly, bypassing any name that
10050 come from dependent base class. */
10051 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10053 /* The call to xref_tag_from_type does injection for friend
10054 classes. */
10055 push_nested_namespace (ns);
10056 friend_type =
10057 xref_tag_from_type (friend_type, NULL_TREE,
10058 /*tag_scope=*/ts_current);
10059 pop_nested_namespace (ns);
10061 else if (uses_template_parms (friend_type))
10062 /* friend class C<T>; */
10063 friend_type = tsubst (friend_type, args,
10064 tf_warning_or_error, NULL_TREE);
10065 /* Otherwise it's
10067 friend class C;
10069 where C is already declared or
10071 friend class C<int>;
10073 We don't have to do anything in these cases. */
10075 if (adjust_processing_template_decl)
10076 /* Trick make_friend_class into realizing that the friend
10077 we're adding is a template, not an ordinary class. It's
10078 important that we use make_friend_class since it will
10079 perform some error-checking and output cross-reference
10080 information. */
10081 ++processing_template_decl;
10083 if (friend_type != error_mark_node)
10084 make_friend_class (type, friend_type, /*complain=*/false);
10086 if (adjust_processing_template_decl)
10087 --processing_template_decl;
10089 else
10091 /* Build new DECL_FRIENDLIST. */
10092 tree r;
10094 /* The file and line for this declaration, to
10095 assist in error message reporting. Since we
10096 called push_tinst_level above, we don't need to
10097 restore these. */
10098 input_location = DECL_SOURCE_LOCATION (t);
10100 if (TREE_CODE (t) == TEMPLATE_DECL)
10102 ++processing_template_decl;
10103 push_deferring_access_checks (dk_no_check);
10106 r = tsubst_friend_function (t, args);
10107 add_friend (type, r, /*complain=*/false);
10108 if (TREE_CODE (t) == TEMPLATE_DECL)
10110 pop_deferring_access_checks ();
10111 --processing_template_decl;
10117 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10119 tree decl = lambda_function (type);
10120 if (decl)
10122 if (!DECL_TEMPLATE_INFO (decl)
10123 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10124 instantiate_decl (decl, false, false);
10126 /* We need to instantiate the capture list from the template
10127 after we've instantiated the closure members, but before we
10128 consider adding the conversion op. Also keep any captures
10129 that may have been added during instantiation of the op(). */
10130 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10131 tree tmpl_cap
10132 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10133 args, tf_warning_or_error, NULL_TREE,
10134 false, false);
10136 LAMBDA_EXPR_CAPTURE_LIST (expr)
10137 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10139 maybe_add_lambda_conv_op (type);
10141 else
10142 gcc_assert (errorcount);
10145 /* Set the file and line number information to whatever is given for
10146 the class itself. This puts error messages involving generated
10147 implicit functions at a predictable point, and the same point
10148 that would be used for non-template classes. */
10149 input_location = DECL_SOURCE_LOCATION (typedecl);
10151 unreverse_member_declarations (type);
10152 finish_struct_1 (type);
10153 TYPE_BEING_DEFINED (type) = 0;
10155 /* We don't instantiate default arguments for member functions. 14.7.1:
10157 The implicit instantiation of a class template specialization causes
10158 the implicit instantiation of the declarations, but not of the
10159 definitions or default arguments, of the class member functions,
10160 member classes, static data members and member templates.... */
10162 /* Some typedefs referenced from within the template code need to be access
10163 checked at template instantiation time, i.e now. These types were
10164 added to the template at parsing time. Let's get those and perform
10165 the access checks then. */
10166 perform_typedefs_access_check (pattern, args);
10167 perform_deferred_access_checks (tf_warning_or_error);
10168 pop_nested_class ();
10169 maximum_field_alignment = saved_maximum_field_alignment;
10170 if (!fn_context)
10171 pop_from_top_level ();
10172 pop_deferring_access_checks ();
10173 pop_tinst_level ();
10175 /* The vtable for a template class can be emitted in any translation
10176 unit in which the class is instantiated. When there is no key
10177 method, however, finish_struct_1 will already have added TYPE to
10178 the keyed_classes list. */
10179 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10180 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10182 return type;
10185 /* Wrapper for instantiate_class_template_1. */
10187 tree
10188 instantiate_class_template (tree type)
10190 tree ret;
10191 timevar_push (TV_TEMPLATE_INST);
10192 ret = instantiate_class_template_1 (type);
10193 timevar_pop (TV_TEMPLATE_INST);
10194 return ret;
10197 static tree
10198 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10200 tree r;
10202 if (!t)
10203 r = t;
10204 else if (TYPE_P (t))
10205 r = tsubst (t, args, complain, in_decl);
10206 else
10208 if (!(complain & tf_warning))
10209 ++c_inhibit_evaluation_warnings;
10210 r = tsubst_expr (t, args, complain, in_decl,
10211 /*integral_constant_expression_p=*/true);
10212 if (!(complain & tf_warning))
10213 --c_inhibit_evaluation_warnings;
10215 return r;
10218 /* Given a function parameter pack TMPL_PARM and some function parameters
10219 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10220 and set *SPEC_P to point at the next point in the list. */
10222 tree
10223 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10225 /* Collect all of the extra "packed" parameters into an
10226 argument pack. */
10227 tree parmvec;
10228 tree parmtypevec;
10229 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10230 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
10231 tree spec_parm = *spec_p;
10232 int i, len;
10234 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10235 if (tmpl_parm
10236 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10237 break;
10239 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10240 parmvec = make_tree_vec (len);
10241 parmtypevec = make_tree_vec (len);
10242 spec_parm = *spec_p;
10243 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10245 TREE_VEC_ELT (parmvec, i) = spec_parm;
10246 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
10249 /* Build the argument packs. */
10250 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10251 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
10252 TREE_TYPE (argpack) = argtypepack;
10253 *spec_p = spec_parm;
10255 return argpack;
10258 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10259 NONTYPE_ARGUMENT_PACK. */
10261 static tree
10262 make_fnparm_pack (tree spec_parm)
10264 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10267 /* Return true iff the Ith element of the argument pack ARG_PACK is a
10268 pack expansion. */
10270 static bool
10271 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10273 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10274 if (i >= TREE_VEC_LENGTH (vec))
10275 return false;
10276 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
10280 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10282 static tree
10283 make_argument_pack_select (tree arg_pack, unsigned index)
10285 tree aps = make_node (ARGUMENT_PACK_SELECT);
10287 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10288 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10290 return aps;
10293 /* This is a subroutine of tsubst_pack_expansion.
10295 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10296 mechanism to store the (non complete list of) arguments of the
10297 substitution and return a non substituted pack expansion, in order
10298 to wait for when we have enough arguments to really perform the
10299 substitution. */
10301 static bool
10302 use_pack_expansion_extra_args_p (tree parm_packs,
10303 int arg_pack_len,
10304 bool has_empty_arg)
10306 /* If one pack has an expansion and another pack has a normal
10307 argument or if one pack has an empty argument and an another
10308 one hasn't then tsubst_pack_expansion cannot perform the
10309 substitution and need to fall back on the
10310 PACK_EXPANSION_EXTRA mechanism. */
10311 if (parm_packs == NULL_TREE)
10312 return false;
10313 else if (has_empty_arg)
10314 return true;
10316 bool has_expansion_arg = false;
10317 for (int i = 0 ; i < arg_pack_len; ++i)
10319 bool has_non_expansion_arg = false;
10320 for (tree parm_pack = parm_packs;
10321 parm_pack;
10322 parm_pack = TREE_CHAIN (parm_pack))
10324 tree arg = TREE_VALUE (parm_pack);
10326 if (argument_pack_element_is_expansion_p (arg, i))
10327 has_expansion_arg = true;
10328 else
10329 has_non_expansion_arg = true;
10332 if (has_expansion_arg && has_non_expansion_arg)
10333 return true;
10335 return false;
10338 /* [temp.variadic]/6 says that:
10340 The instantiation of a pack expansion [...]
10341 produces a list E1,E2, ..., En, where N is the number of elements
10342 in the pack expansion parameters.
10344 This subroutine of tsubst_pack_expansion produces one of these Ei.
10346 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10347 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10348 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10349 INDEX is the index 'i' of the element Ei to produce. ARGS,
10350 COMPLAIN, and IN_DECL are the same parameters as for the
10351 tsubst_pack_expansion function.
10353 The function returns the resulting Ei upon successful completion,
10354 or error_mark_node.
10356 Note that this function possibly modifies the ARGS parameter, so
10357 it's the responsibility of the caller to restore it. */
10359 static tree
10360 gen_elem_of_pack_expansion_instantiation (tree pattern,
10361 tree parm_packs,
10362 unsigned index,
10363 tree args /* This parm gets
10364 modified. */,
10365 tsubst_flags_t complain,
10366 tree in_decl)
10368 tree t;
10369 bool ith_elem_is_expansion = false;
10371 /* For each parameter pack, change the substitution of the parameter
10372 pack to the ith argument in its argument pack, then expand the
10373 pattern. */
10374 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10376 tree parm = TREE_PURPOSE (pack);
10377 tree arg_pack = TREE_VALUE (pack);
10378 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10380 ith_elem_is_expansion |=
10381 argument_pack_element_is_expansion_p (arg_pack, index);
10383 /* Select the Ith argument from the pack. */
10384 if (TREE_CODE (parm) == PARM_DECL
10385 || TREE_CODE (parm) == FIELD_DECL)
10387 if (index == 0)
10389 aps = make_argument_pack_select (arg_pack, index);
10390 mark_used (parm);
10391 register_local_specialization (aps, parm);
10393 else
10394 aps = retrieve_local_specialization (parm);
10396 else
10398 int idx, level;
10399 template_parm_level_and_index (parm, &level, &idx);
10401 if (index == 0)
10403 aps = make_argument_pack_select (arg_pack, index);
10404 /* Update the corresponding argument. */
10405 TMPL_ARG (args, level, idx) = aps;
10407 else
10408 /* Re-use the ARGUMENT_PACK_SELECT. */
10409 aps = TMPL_ARG (args, level, idx);
10411 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10414 /* Substitute into the PATTERN with the (possibly altered)
10415 arguments. */
10416 if (pattern == in_decl)
10417 /* Expanding a fixed parameter pack from
10418 coerce_template_parameter_pack. */
10419 t = tsubst_decl (pattern, args, complain);
10420 else if (!TYPE_P (pattern))
10421 t = tsubst_expr (pattern, args, complain, in_decl,
10422 /*integral_constant_expression_p=*/false);
10423 else
10424 t = tsubst (pattern, args, complain, in_decl);
10426 /* If the Ith argument pack element is a pack expansion, then
10427 the Ith element resulting from the substituting is going to
10428 be a pack expansion as well. */
10429 if (ith_elem_is_expansion)
10430 t = make_pack_expansion (t);
10432 return t;
10435 /* Substitute args into the pack expansion T, and rewrite the resulting
10436 list as a conjunction of the specified terms. If the result is an empty
10437 expression, return boolean_true_node.
10439 FIXME: This goes away with fold expressions. */
10440 tree
10441 tsubst_pack_conjunction (tree t, tree args, tsubst_flags_t complain,
10442 tree in_decl)
10444 tree terms = tsubst_pack_expansion (t, args, complain, in_decl);
10446 /* If the resulting expression is type- or value-dependent, then
10447 return it after setting the result type to bool (so it can be
10448 expanded as a conjunction). This happens when instantiating
10449 constrained variadic member function templates. Just rebuild
10450 the dependent pack expansion. */
10451 if (instantiation_dependent_expression_p (terms))
10453 terms = TREE_VEC_ELT (terms, 0);
10454 TREE_TYPE (terms) = boolean_type_node;
10455 return terms;
10458 /* Empty expansions are equivalent to 'true'. */
10459 if (TREE_VEC_LENGTH (terms) == 0)
10460 return boolean_true_node;
10462 /* Otherwise, and the terms together. These are transformed
10463 into constraints later. */
10464 tree result = TREE_VEC_ELT (terms, 0);
10465 for (int i = 1; i < TREE_VEC_LENGTH (terms); ++i) {
10466 tree t = TREE_VEC_ELT (terms, i);
10467 result = build_min (TRUTH_ANDIF_EXPR, boolean_type_node, result, t);
10469 return result;
10473 /* Substitute ARGS into T, which is an pack expansion
10474 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
10475 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
10476 (if only a partial substitution could be performed) or
10477 ERROR_MARK_NODE if there was an error. */
10478 tree
10479 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
10480 tree in_decl)
10482 tree pattern;
10483 tree pack, packs = NULL_TREE;
10484 bool unsubstituted_packs = false;
10485 int i, len = -1;
10486 tree result;
10487 hash_map<tree, tree> *saved_local_specializations = NULL;
10488 bool need_local_specializations = false;
10489 int levels;
10491 /* If T is a parameter declaration pending expansion by
10492 ARGS, substitute through and form an argument pack
10493 for the resulting expression. The sequence of
10494 expressions is contained in the argument pack. */
10495 if (pending_expansion_p (t))
10497 tree pack = tsubst_copy_and_build (t, args, complain, in_decl,
10498 /*function_p=*/false,
10499 /*constexpr_p=*/false);
10500 gcc_assert (TREE_CODE (pack) == NONTYPE_ARGUMENT_PACK);
10501 return TREE_OPERAND (pack, 0);
10504 gcc_assert (PACK_EXPANSION_P (t));
10505 pattern = PACK_EXPANSION_PATTERN (t);
10507 /* Add in any args remembered from an earlier partial instantiation. */
10508 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
10510 levels = TMPL_ARGS_DEPTH (args);
10512 /* Determine the argument packs that will instantiate the parameter
10513 packs used in the expansion expression. While we're at it,
10514 compute the number of arguments to be expanded and make sure it
10515 is consistent. */
10516 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
10517 pack = TREE_CHAIN (pack))
10519 tree parm_pack = TREE_VALUE (pack);
10520 tree arg_pack = NULL_TREE;
10521 tree orig_arg = NULL_TREE;
10522 int level = 0;
10524 if (TREE_CODE (parm_pack) == BASES)
10526 if (BASES_DIRECT (parm_pack))
10527 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
10528 args, complain, in_decl, false));
10529 else
10530 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
10531 args, complain, in_decl, false));
10533 if (TREE_CODE (parm_pack) == PARM_DECL)
10535 if (PACK_EXPANSION_LOCAL_P (t))
10536 arg_pack = retrieve_local_specialization (parm_pack);
10537 else
10539 /* We can't rely on local_specializations for a parameter
10540 name used later in a function declaration (such as in a
10541 late-specified return type). Even if it exists, it might
10542 have the wrong value for a recursive call. Just make a
10543 dummy decl, since it's only used for its type. */
10544 arg_pack = tsubst_decl (parm_pack, args, complain);
10545 if (arg_pack && DECL_PACK_P (arg_pack))
10546 /* Partial instantiation of the parm_pack, we can't build
10547 up an argument pack yet. */
10548 arg_pack = NULL_TREE;
10549 else
10550 arg_pack = make_fnparm_pack (arg_pack);
10551 need_local_specializations = true;
10554 else if (TREE_CODE (parm_pack) == FIELD_DECL)
10555 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
10556 else
10558 int idx;
10559 template_parm_level_and_index (parm_pack, &level, &idx);
10561 if (level <= levels)
10562 arg_pack = TMPL_ARG (args, level, idx);
10565 orig_arg = arg_pack;
10566 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
10567 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
10569 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
10570 /* This can only happen if we forget to expand an argument
10571 pack somewhere else. Just return an error, silently. */
10573 result = make_tree_vec (1);
10574 TREE_VEC_ELT (result, 0) = error_mark_node;
10575 return result;
10578 if (arg_pack)
10580 int my_len =
10581 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
10583 /* Don't bother trying to do a partial substitution with
10584 incomplete packs; we'll try again after deduction. */
10585 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
10586 return t;
10588 if (len < 0)
10589 len = my_len;
10590 else if (len != my_len)
10592 if (!(complain & tf_error))
10593 /* Fail quietly. */;
10594 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
10595 error ("mismatched argument pack lengths while expanding "
10596 "%<%T%>",
10597 pattern);
10598 else
10599 error ("mismatched argument pack lengths while expanding "
10600 "%<%E%>",
10601 pattern);
10602 return error_mark_node;
10605 /* Keep track of the parameter packs and their corresponding
10606 argument packs. */
10607 packs = tree_cons (parm_pack, arg_pack, packs);
10608 TREE_TYPE (packs) = orig_arg;
10610 else
10612 /* We can't substitute for this parameter pack. We use a flag as
10613 well as the missing_level counter because function parameter
10614 packs don't have a level. */
10615 unsubstituted_packs = true;
10619 /* If the expansion is just T..., return the matching argument pack. */
10620 if (!unsubstituted_packs
10621 && TREE_PURPOSE (packs) == pattern)
10623 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
10624 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
10625 || pack_expansion_args_count (args))
10626 return args;
10627 /* Otherwise use the normal path so we get convert_from_reference. */
10630 /* We cannot expand this expansion expression, because we don't have
10631 all of the argument packs we need. */
10632 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
10634 /* We got some full packs, but we can't substitute them in until we
10635 have values for all the packs. So remember these until then. */
10637 t = make_pack_expansion (pattern);
10638 PACK_EXPANSION_EXTRA_ARGS (t) = args;
10639 return t;
10641 else if (unsubstituted_packs)
10643 /* There were no real arguments, we're just replacing a parameter
10644 pack with another version of itself. Substitute into the
10645 pattern and return a PACK_EXPANSION_*. The caller will need to
10646 deal with that. */
10647 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
10648 t = tsubst_expr (pattern, args, complain, in_decl,
10649 /*integral_constant_expression_p=*/false);
10650 else
10651 t = tsubst (pattern, args, complain, in_decl);
10652 t = make_pack_expansion (t);
10653 return t;
10656 gcc_assert (len >= 0);
10658 if (need_local_specializations)
10660 /* We're in a late-specified return type, so create our own local
10661 specializations map; the current map is either NULL or (in the
10662 case of recursive unification) might have bindings that we don't
10663 want to use or alter. */
10664 saved_local_specializations = local_specializations;
10665 local_specializations = new hash_map<tree, tree>;
10668 /* For each argument in each argument pack, substitute into the
10669 pattern. */
10670 result = make_tree_vec (len);
10671 for (i = 0; i < len; ++i)
10673 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
10675 args, complain,
10676 in_decl);
10677 TREE_VEC_ELT (result, i) = t;
10678 if (t == error_mark_node)
10680 result = error_mark_node;
10681 break;
10685 /* Update ARGS to restore the substitution from parameter packs to
10686 their argument packs. */
10687 for (pack = packs; pack; pack = TREE_CHAIN (pack))
10689 tree parm = TREE_PURPOSE (pack);
10691 if (TREE_CODE (parm) == PARM_DECL
10692 || TREE_CODE (parm) == FIELD_DECL)
10693 register_local_specialization (TREE_TYPE (pack), parm);
10694 else
10696 int idx, level;
10698 if (TREE_VALUE (pack) == NULL_TREE)
10699 continue;
10701 template_parm_level_and_index (parm, &level, &idx);
10703 /* Update the corresponding argument. */
10704 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
10705 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
10706 TREE_TYPE (pack);
10707 else
10708 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
10712 if (need_local_specializations)
10714 delete local_specializations;
10715 local_specializations = saved_local_specializations;
10718 return result;
10721 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
10722 TMPL. We do this using DECL_PARM_INDEX, which should work even with
10723 parameter packs; all parms generated from a function parameter pack will
10724 have the same DECL_PARM_INDEX. */
10726 tree
10727 get_pattern_parm (tree parm, tree tmpl)
10729 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
10730 tree patparm;
10732 if (DECL_ARTIFICIAL (parm))
10734 for (patparm = DECL_ARGUMENTS (pattern);
10735 patparm; patparm = DECL_CHAIN (patparm))
10736 if (DECL_ARTIFICIAL (patparm)
10737 && DECL_NAME (parm) == DECL_NAME (patparm))
10738 break;
10740 else
10742 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
10743 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
10744 gcc_assert (DECL_PARM_INDEX (patparm)
10745 == DECL_PARM_INDEX (parm));
10748 return patparm;
10751 /* Substitute ARGS into the vector or list of template arguments T. */
10753 static tree
10754 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10756 tree orig_t = t;
10757 int len, need_new = 0, i, expanded_len_adjust = 0, out;
10758 tree *elts;
10760 if (t == error_mark_node)
10761 return error_mark_node;
10763 len = TREE_VEC_LENGTH (t);
10764 elts = XALLOCAVEC (tree, len);
10766 for (i = 0; i < len; i++)
10768 tree orig_arg = TREE_VEC_ELT (t, i);
10769 tree new_arg;
10771 if (TREE_CODE (orig_arg) == TREE_VEC)
10772 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
10773 else if (PACK_EXPANSION_P (orig_arg))
10775 /* Substitute into an expansion expression. */
10776 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
10778 if (TREE_CODE (new_arg) == TREE_VEC)
10779 /* Add to the expanded length adjustment the number of
10780 expanded arguments. We subtract one from this
10781 measurement, because the argument pack expression
10782 itself is already counted as 1 in
10783 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
10784 the argument pack is empty. */
10785 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
10787 else if (ARGUMENT_PACK_P (orig_arg))
10789 /* Substitute into each of the arguments. */
10790 new_arg = TYPE_P (orig_arg)
10791 ? cxx_make_type (TREE_CODE (orig_arg))
10792 : make_node (TREE_CODE (orig_arg));
10794 SET_ARGUMENT_PACK_ARGS (
10795 new_arg,
10796 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
10797 args, complain, in_decl));
10799 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
10800 new_arg = error_mark_node;
10802 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
10803 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
10804 complain, in_decl);
10805 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
10807 if (TREE_TYPE (new_arg) == error_mark_node)
10808 new_arg = error_mark_node;
10811 else
10812 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
10814 if (new_arg == error_mark_node)
10815 return error_mark_node;
10817 elts[i] = new_arg;
10818 if (new_arg != orig_arg)
10819 need_new = 1;
10822 if (!need_new)
10823 return t;
10825 /* Make space for the expanded arguments coming from template
10826 argument packs. */
10827 t = make_tree_vec (len + expanded_len_adjust);
10828 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
10829 arguments for a member template.
10830 In that case each TREE_VEC in ORIG_T represents a level of template
10831 arguments, and ORIG_T won't carry any non defaulted argument count.
10832 It will rather be the nested TREE_VECs that will carry one.
10833 In other words, ORIG_T carries a non defaulted argument count only
10834 if it doesn't contain any nested TREE_VEC. */
10835 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
10837 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
10838 count += expanded_len_adjust;
10839 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
10841 for (i = 0, out = 0; i < len; i++)
10843 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
10844 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
10845 && TREE_CODE (elts[i]) == TREE_VEC)
10847 int idx;
10849 /* Now expand the template argument pack "in place". */
10850 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
10851 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
10853 else
10855 TREE_VEC_ELT (t, out) = elts[i];
10856 out++;
10860 return t;
10863 /* Return the result of substituting ARGS into the template parameters
10864 given by PARMS. If there are m levels of ARGS and m + n levels of
10865 PARMS, then the result will contain n levels of PARMS. For
10866 example, if PARMS is `template <class T> template <class U>
10867 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10868 result will be `template <int*, double, class V>'. */
10870 static tree
10871 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
10873 tree r = NULL_TREE;
10874 tree* new_parms;
10876 /* When substituting into a template, we must set
10877 PROCESSING_TEMPLATE_DECL as the template parameters may be
10878 dependent if they are based on one-another, and the dependency
10879 predicates are short-circuit outside of templates. */
10880 ++processing_template_decl;
10882 for (new_parms = &r;
10883 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
10884 new_parms = &(TREE_CHAIN (*new_parms)),
10885 parms = TREE_CHAIN (parms))
10887 tree new_vec =
10888 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
10889 int i;
10891 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
10893 tree tuple;
10895 if (parms == error_mark_node)
10896 continue;
10898 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
10900 if (tuple == error_mark_node)
10901 continue;
10903 TREE_VEC_ELT (new_vec, i) =
10904 tsubst_template_parm (tuple, args, complain);
10907 *new_parms =
10908 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
10909 - TMPL_ARGS_DEPTH (args)),
10910 new_vec, NULL_TREE);
10913 --processing_template_decl;
10915 return r;
10918 /* Return the result of substituting ARGS into one template parameter
10919 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10920 parameter and which TREE_PURPOSE is the default argument of the
10921 template parameter. */
10923 static tree
10924 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
10926 tree default_value, parm_decl;
10928 if (args == NULL_TREE
10929 || t == NULL_TREE
10930 || t == error_mark_node)
10931 return t;
10933 gcc_assert (TREE_CODE (t) == TREE_LIST);
10935 default_value = TREE_PURPOSE (t);
10936 parm_decl = TREE_VALUE (t);
10938 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
10939 if (TREE_CODE (parm_decl) == PARM_DECL
10940 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
10941 parm_decl = error_mark_node;
10942 default_value = tsubst_template_arg (default_value, args,
10943 complain, NULL_TREE);
10945 return build_tree_list (default_value, parm_decl);
10948 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10949 type T. If T is not an aggregate or enumeration type, it is
10950 handled as if by tsubst. IN_DECL is as for tsubst. If
10951 ENTERING_SCOPE is nonzero, T is the context for a template which
10952 we are presently tsubst'ing. Return the substituted value. */
10954 static tree
10955 tsubst_aggr_type (tree t,
10956 tree args,
10957 tsubst_flags_t complain,
10958 tree in_decl,
10959 int entering_scope)
10961 if (t == NULL_TREE)
10962 return NULL_TREE;
10964 switch (TREE_CODE (t))
10966 case RECORD_TYPE:
10967 if (TYPE_PTRMEMFUNC_P (t))
10968 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
10970 /* Else fall through. */
10971 case ENUMERAL_TYPE:
10972 case UNION_TYPE:
10973 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
10975 tree argvec;
10976 tree context;
10977 tree r;
10978 int saved_unevaluated_operand;
10979 int saved_inhibit_evaluation_warnings;
10981 /* In "sizeof(X<I>)" we need to evaluate "I". */
10982 saved_unevaluated_operand = cp_unevaluated_operand;
10983 cp_unevaluated_operand = 0;
10984 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10985 c_inhibit_evaluation_warnings = 0;
10987 /* First, determine the context for the type we are looking
10988 up. */
10989 context = TYPE_CONTEXT (t);
10990 if (context && TYPE_P (context))
10992 context = tsubst_aggr_type (context, args, complain,
10993 in_decl, /*entering_scope=*/1);
10994 /* If context is a nested class inside a class template,
10995 it may still need to be instantiated (c++/33959). */
10996 context = complete_type (context);
10999 /* Then, figure out what arguments are appropriate for the
11000 type we are trying to find. For example, given:
11002 template <class T> struct S;
11003 template <class T, class U> void f(T, U) { S<U> su; }
11005 and supposing that we are instantiating f<int, double>,
11006 then our ARGS will be {int, double}, but, when looking up
11007 S we only want {double}. */
11008 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11009 complain, in_decl);
11010 if (argvec == error_mark_node)
11011 r = error_mark_node;
11012 else
11014 r = lookup_template_class (t, argvec, in_decl, context,
11015 entering_scope, complain);
11016 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11019 cp_unevaluated_operand = saved_unevaluated_operand;
11020 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11022 return r;
11024 else
11025 /* This is not a template type, so there's nothing to do. */
11026 return t;
11028 default:
11029 return tsubst (t, args, complain, in_decl);
11033 /* Substitute into the default argument ARG (a default argument for
11034 FN), which has the indicated TYPE. */
11036 tree
11037 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11039 tree saved_class_ptr = NULL_TREE;
11040 tree saved_class_ref = NULL_TREE;
11041 int errs = errorcount + sorrycount;
11043 /* This can happen in invalid code. */
11044 if (TREE_CODE (arg) == DEFAULT_ARG)
11045 return arg;
11047 /* This default argument came from a template. Instantiate the
11048 default argument here, not in tsubst. In the case of
11049 something like:
11051 template <class T>
11052 struct S {
11053 static T t();
11054 void f(T = t());
11057 we must be careful to do name lookup in the scope of S<T>,
11058 rather than in the current class. */
11059 push_access_scope (fn);
11060 /* The "this" pointer is not valid in a default argument. */
11061 if (cfun)
11063 saved_class_ptr = current_class_ptr;
11064 cp_function_chain->x_current_class_ptr = NULL_TREE;
11065 saved_class_ref = current_class_ref;
11066 cp_function_chain->x_current_class_ref = NULL_TREE;
11069 push_deferring_access_checks(dk_no_deferred);
11070 /* The default argument expression may cause implicitly defined
11071 member functions to be synthesized, which will result in garbage
11072 collection. We must treat this situation as if we were within
11073 the body of function so as to avoid collecting live data on the
11074 stack. */
11075 ++function_depth;
11076 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
11077 complain, NULL_TREE,
11078 /*integral_constant_expression_p=*/false);
11079 --function_depth;
11080 pop_deferring_access_checks();
11082 /* Restore the "this" pointer. */
11083 if (cfun)
11085 cp_function_chain->x_current_class_ptr = saved_class_ptr;
11086 cp_function_chain->x_current_class_ref = saved_class_ref;
11089 if (errorcount+sorrycount > errs
11090 && (complain & tf_warning_or_error))
11091 inform (input_location,
11092 " when instantiating default argument for call to %D", fn);
11094 /* Make sure the default argument is reasonable. */
11095 arg = check_default_argument (type, arg, complain);
11097 pop_access_scope (fn);
11099 return arg;
11102 /* Substitute into all the default arguments for FN. */
11104 static void
11105 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
11107 tree arg;
11108 tree tmpl_args;
11110 tmpl_args = DECL_TI_ARGS (fn);
11112 /* If this function is not yet instantiated, we certainly don't need
11113 its default arguments. */
11114 if (uses_template_parms (tmpl_args))
11115 return;
11116 /* Don't do this again for clones. */
11117 if (DECL_CLONED_FUNCTION_P (fn))
11118 return;
11120 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
11121 arg;
11122 arg = TREE_CHAIN (arg))
11123 if (TREE_PURPOSE (arg))
11124 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
11125 TREE_VALUE (arg),
11126 TREE_PURPOSE (arg),
11127 complain);
11130 /* Substitute the ARGS into the T, which is a _DECL. Return the
11131 result of the substitution. Issue error and warning messages under
11132 control of COMPLAIN. */
11134 static tree
11135 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
11137 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
11138 location_t saved_loc;
11139 tree r = NULL_TREE;
11140 tree in_decl = t;
11141 hashval_t hash = 0;
11143 /* Set the filename and linenumber to improve error-reporting. */
11144 saved_loc = input_location;
11145 input_location = DECL_SOURCE_LOCATION (t);
11147 switch (TREE_CODE (t))
11149 case TEMPLATE_DECL:
11151 /* We can get here when processing a member function template,
11152 member class template, or template template parameter. */
11153 tree decl = DECL_TEMPLATE_RESULT (t);
11154 tree type = TREE_TYPE (t);
11155 tree spec;
11156 tree tmpl_args;
11157 tree full_args;
11159 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11161 /* Template template parameter is treated here. */
11162 tree new_type = tsubst (type, args, complain, in_decl);
11163 if (new_type == error_mark_node)
11164 RETURN (error_mark_node);
11165 /* If we get a real template back, return it. This can happen in
11166 the context of most_specialized_partial_spec. */
11167 if (TREE_CODE (new_type) == TEMPLATE_DECL)
11168 return new_type;
11170 r = copy_decl (t);
11171 DECL_CHAIN (r) = NULL_TREE;
11172 TREE_TYPE (r) = new_type;
11173 DECL_TEMPLATE_RESULT (r)
11174 = build_decl (DECL_SOURCE_LOCATION (decl),
11175 TYPE_DECL, DECL_NAME (decl), new_type);
11176 DECL_TEMPLATE_PARMS (r)
11177 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
11178 complain);
11179 TYPE_NAME (new_type) = r;
11180 break;
11183 /* We might already have an instance of this template.
11184 The ARGS are for the surrounding class type, so the
11185 full args contain the tsubst'd args for the context,
11186 plus the innermost args from the template decl. */
11187 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
11188 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
11189 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
11190 /* Because this is a template, the arguments will still be
11191 dependent, even after substitution. If
11192 PROCESSING_TEMPLATE_DECL is not set, the dependency
11193 predicates will short-circuit. */
11194 ++processing_template_decl;
11195 full_args = tsubst_template_args (tmpl_args, args,
11196 complain, in_decl);
11197 --processing_template_decl;
11198 if (full_args == error_mark_node)
11199 RETURN (error_mark_node);
11201 /* If this is a default template template argument,
11202 tsubst might not have changed anything. */
11203 if (full_args == tmpl_args)
11204 RETURN (t);
11206 hash = hash_tmpl_and_args (t, full_args);
11207 spec = retrieve_specialization (t, full_args, hash);
11208 if (spec != NULL_TREE)
11210 r = spec;
11211 break;
11214 /* Make a new template decl. It will be similar to the
11215 original, but will record the current template arguments.
11216 We also create a new function declaration, which is just
11217 like the old one, but points to this new template, rather
11218 than the old one. */
11219 r = copy_decl (t);
11220 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
11221 DECL_CHAIN (r) = NULL_TREE;
11223 // Build new template info linking to the original template decl.
11224 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11226 if (TREE_CODE (decl) == TYPE_DECL
11227 && !TYPE_DECL_ALIAS_P (decl))
11229 tree new_type;
11230 ++processing_template_decl;
11231 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11232 --processing_template_decl;
11233 if (new_type == error_mark_node)
11234 RETURN (error_mark_node);
11236 TREE_TYPE (r) = new_type;
11237 /* For a partial specialization, we need to keep pointing to
11238 the primary template. */
11239 if (!DECL_TEMPLATE_SPECIALIZATION (t))
11240 CLASSTYPE_TI_TEMPLATE (new_type) = r;
11241 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
11242 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
11243 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
11245 else
11247 tree new_decl;
11248 ++processing_template_decl;
11249 new_decl = tsubst (decl, args, complain, in_decl);
11250 --processing_template_decl;
11251 if (new_decl == error_mark_node)
11252 RETURN (error_mark_node);
11254 DECL_TEMPLATE_RESULT (r) = new_decl;
11255 DECL_TI_TEMPLATE (new_decl) = r;
11256 TREE_TYPE (r) = TREE_TYPE (new_decl);
11257 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
11258 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
11261 SET_DECL_IMPLICIT_INSTANTIATION (r);
11262 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
11263 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
11265 /* The template parameters for this new template are all the
11266 template parameters for the old template, except the
11267 outermost level of parameters. */
11268 DECL_TEMPLATE_PARMS (r)
11269 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
11270 complain);
11272 if (PRIMARY_TEMPLATE_P (t))
11273 DECL_PRIMARY_TEMPLATE (r) = r;
11275 if (TREE_CODE (decl) != TYPE_DECL && TREE_CODE (decl) != VAR_DECL)
11276 /* Record this non-type partial instantiation. */
11277 register_specialization (r, t,
11278 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
11279 false, hash);
11281 break;
11283 case FUNCTION_DECL:
11285 tree ctx;
11286 tree argvec = NULL_TREE;
11287 tree *friends;
11288 tree gen_tmpl;
11289 tree type;
11290 int member;
11291 int args_depth;
11292 int parms_depth;
11294 /* Nobody should be tsubst'ing into non-template functions. */
11295 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
11297 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
11299 tree spec;
11300 bool dependent_p;
11302 /* If T is not dependent, just return it. We have to
11303 increment PROCESSING_TEMPLATE_DECL because
11304 value_dependent_expression_p assumes that nothing is
11305 dependent when PROCESSING_TEMPLATE_DECL is zero. */
11306 ++processing_template_decl;
11307 dependent_p = value_dependent_expression_p (t);
11308 --processing_template_decl;
11309 if (!dependent_p)
11310 RETURN (t);
11312 /* Calculate the most general template of which R is a
11313 specialization, and the complete set of arguments used to
11314 specialize R. */
11315 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
11316 argvec = tsubst_template_args (DECL_TI_ARGS
11317 (DECL_TEMPLATE_RESULT
11318 (DECL_TI_TEMPLATE (t))),
11319 args, complain, in_decl);
11320 if (argvec == error_mark_node)
11321 RETURN (error_mark_node);
11323 /* Check to see if we already have this specialization. */
11324 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11325 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11327 if (spec)
11329 r = spec;
11330 break;
11333 /* We can see more levels of arguments than parameters if
11334 there was a specialization of a member template, like
11335 this:
11337 template <class T> struct S { template <class U> void f(); }
11338 template <> template <class U> void S<int>::f(U);
11340 Here, we'll be substituting into the specialization,
11341 because that's where we can find the code we actually
11342 want to generate, but we'll have enough arguments for
11343 the most general template.
11345 We also deal with the peculiar case:
11347 template <class T> struct S {
11348 template <class U> friend void f();
11350 template <class U> void f() {}
11351 template S<int>;
11352 template void f<double>();
11354 Here, the ARGS for the instantiation of will be {int,
11355 double}. But, we only need as many ARGS as there are
11356 levels of template parameters in CODE_PATTERN. We are
11357 careful not to get fooled into reducing the ARGS in
11358 situations like:
11360 template <class T> struct S { template <class U> void f(U); }
11361 template <class T> template <> void S<T>::f(int) {}
11363 which we can spot because the pattern will be a
11364 specialization in this case. */
11365 args_depth = TMPL_ARGS_DEPTH (args);
11366 parms_depth =
11367 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
11368 if (args_depth > parms_depth
11369 && !DECL_TEMPLATE_SPECIALIZATION (t))
11370 args = get_innermost_template_args (args, parms_depth);
11372 else
11374 /* This special case arises when we have something like this:
11376 template <class T> struct S {
11377 friend void f<int>(int, double);
11380 Here, the DECL_TI_TEMPLATE for the friend declaration
11381 will be an IDENTIFIER_NODE. We are being called from
11382 tsubst_friend_function, and we want only to create a
11383 new decl (R) with appropriate types so that we can call
11384 determine_specialization. */
11385 gen_tmpl = NULL_TREE;
11388 if (DECL_CLASS_SCOPE_P (t))
11390 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
11391 member = 2;
11392 else
11393 member = 1;
11394 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
11395 complain, t, /*entering_scope=*/1);
11397 else
11399 member = 0;
11400 ctx = DECL_CONTEXT (t);
11402 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11403 if (type == error_mark_node)
11404 RETURN (error_mark_node);
11406 /* If we hit excessive deduction depth, the type is bogus even if
11407 it isn't error_mark_node, so don't build a decl. */
11408 if (excessive_deduction_depth)
11409 RETURN (error_mark_node);
11411 /* We do NOT check for matching decls pushed separately at this
11412 point, as they may not represent instantiations of this
11413 template, and in any case are considered separate under the
11414 discrete model. */
11415 r = copy_decl (t);
11416 DECL_USE_TEMPLATE (r) = 0;
11417 TREE_TYPE (r) = type;
11418 /* Clear out the mangled name and RTL for the instantiation. */
11419 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11420 SET_DECL_RTL (r, NULL);
11421 /* Leave DECL_INITIAL set on deleted instantiations. */
11422 if (!DECL_DELETED_FN (r))
11423 DECL_INITIAL (r) = NULL_TREE;
11424 DECL_CONTEXT (r) = ctx;
11426 /* OpenMP UDRs have the only argument a reference to the declared
11427 type. We want to diagnose if the declared type is a reference,
11428 which is invalid, but as references to references are usually
11429 quietly merged, diagnose it here. */
11430 if (DECL_OMP_DECLARE_REDUCTION_P (t))
11432 tree argtype
11433 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
11434 argtype = tsubst (argtype, args, complain, in_decl);
11435 if (TREE_CODE (argtype) == REFERENCE_TYPE)
11436 error_at (DECL_SOURCE_LOCATION (t),
11437 "reference type %qT in "
11438 "%<#pragma omp declare reduction%>", argtype);
11439 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
11440 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
11441 argtype);
11444 if (member && DECL_CONV_FN_P (r))
11445 /* Type-conversion operator. Reconstruct the name, in
11446 case it's the name of one of the template's parameters. */
11447 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
11449 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
11450 complain, t);
11451 DECL_RESULT (r) = NULL_TREE;
11453 TREE_STATIC (r) = 0;
11454 TREE_PUBLIC (r) = TREE_PUBLIC (t);
11455 DECL_EXTERNAL (r) = 1;
11456 /* If this is an instantiation of a function with internal
11457 linkage, we already know what object file linkage will be
11458 assigned to the instantiation. */
11459 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
11460 DECL_DEFER_OUTPUT (r) = 0;
11461 DECL_CHAIN (r) = NULL_TREE;
11462 DECL_PENDING_INLINE_INFO (r) = 0;
11463 DECL_PENDING_INLINE_P (r) = 0;
11464 DECL_SAVED_TREE (r) = NULL_TREE;
11465 DECL_STRUCT_FUNCTION (r) = NULL;
11466 TREE_USED (r) = 0;
11467 /* We'll re-clone as appropriate in instantiate_template. */
11468 DECL_CLONED_FUNCTION (r) = NULL_TREE;
11470 /* If we aren't complaining now, return on error before we register
11471 the specialization so that we'll complain eventually. */
11472 if ((complain & tf_error) == 0
11473 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
11474 && !grok_op_properties (r, /*complain=*/false))
11475 RETURN (error_mark_node);
11477 /* When instantiating a constrained member, substitute
11478 into the constraints to create a new constraint into
11479 the */
11480 if (tree ci = get_constraints (t)) {
11481 if (member)
11483 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
11484 set_constraints (r, ci);
11488 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
11489 this in the special friend case mentioned above where
11490 GEN_TMPL is NULL. */
11491 if (gen_tmpl)
11493 DECL_TEMPLATE_INFO (r) = build_template_info (gen_tmpl, argvec);
11494 SET_DECL_IMPLICIT_INSTANTIATION (r);
11496 tree new_r
11497 = register_specialization (r, gen_tmpl, argvec, false, hash);
11498 if (new_r != r)
11499 /* We instantiated this while substituting into
11500 the type earlier (template/friend54.C). */
11501 RETURN (new_r);
11503 /* We're not supposed to instantiate default arguments
11504 until they are called, for a template. But, for a
11505 declaration like:
11507 template <class T> void f ()
11508 { extern void g(int i = T()); }
11510 we should do the substitution when the template is
11511 instantiated. We handle the member function case in
11512 instantiate_class_template since the default arguments
11513 might refer to other members of the class. */
11514 if (!member
11515 && !PRIMARY_TEMPLATE_P (gen_tmpl)
11516 && !uses_template_parms (argvec))
11517 tsubst_default_arguments (r, complain);
11519 else
11520 DECL_TEMPLATE_INFO (r) = NULL_TREE;
11522 /* Copy the list of befriending classes. */
11523 for (friends = &DECL_BEFRIENDING_CLASSES (r);
11524 *friends;
11525 friends = &TREE_CHAIN (*friends))
11527 *friends = copy_node (*friends);
11528 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
11529 args, complain,
11530 in_decl);
11533 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
11535 maybe_retrofit_in_chrg (r);
11536 if (DECL_CONSTRUCTOR_P (r))
11537 grok_ctor_properties (ctx, r);
11539 if (DECL_INHERITED_CTOR_BASE (r))
11540 deduce_inheriting_ctor (r);
11542 /* If this is an instantiation of a member template, clone it.
11543 If it isn't, that'll be handled by
11544 clone_constructors_and_destructors. */
11545 if (PRIMARY_TEMPLATE_P (gen_tmpl))
11546 clone_function_decl (r, /*update_method_vec_p=*/0);
11548 else if ((complain & tf_error) != 0
11549 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
11550 && !grok_op_properties (r, /*complain=*/true))
11551 RETURN (error_mark_node);
11553 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
11554 SET_DECL_FRIEND_CONTEXT (r,
11555 tsubst (DECL_FRIEND_CONTEXT (t),
11556 args, complain, in_decl));
11558 /* Possibly limit visibility based on template args. */
11559 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11560 if (DECL_VISIBILITY_SPECIFIED (t))
11562 DECL_VISIBILITY_SPECIFIED (r) = 0;
11563 DECL_ATTRIBUTES (r)
11564 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11566 determine_visibility (r);
11567 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
11568 && !processing_template_decl)
11569 defaulted_late_check (r);
11571 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11572 args, complain, in_decl);
11574 break;
11576 case PARM_DECL:
11578 tree type = NULL_TREE;
11579 int i, len = 1;
11580 tree expanded_types = NULL_TREE;
11581 tree prev_r = NULL_TREE;
11582 tree first_r = NULL_TREE;
11584 if (DECL_PACK_P (t))
11586 /* If there is a local specialization that isn't a
11587 parameter pack, it means that we're doing a "simple"
11588 substitution from inside tsubst_pack_expansion. Just
11589 return the local specialization (which will be a single
11590 parm). */
11591 tree spec = retrieve_local_specialization (t);
11592 if (spec
11593 && TREE_CODE (spec) == PARM_DECL
11594 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
11595 RETURN (spec);
11597 /* Expand the TYPE_PACK_EXPANSION that provides the types for
11598 the parameters in this function parameter pack. */
11599 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11600 complain, in_decl);
11601 if (TREE_CODE (expanded_types) == TREE_VEC)
11603 len = TREE_VEC_LENGTH (expanded_types);
11605 /* Zero-length parameter packs are boring. Just substitute
11606 into the chain. */
11607 if (len == 0)
11608 RETURN (tsubst (TREE_CHAIN (t), args, complain,
11609 TREE_CHAIN (t)));
11611 else
11613 /* All we did was update the type. Make a note of that. */
11614 type = expanded_types;
11615 expanded_types = NULL_TREE;
11619 /* Loop through all of the parameters we'll build. When T is
11620 a function parameter pack, LEN is the number of expanded
11621 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11622 r = NULL_TREE;
11623 for (i = 0; i < len; ++i)
11625 prev_r = r;
11626 r = copy_node (t);
11627 if (DECL_TEMPLATE_PARM_P (t))
11628 SET_DECL_TEMPLATE_PARM_P (r);
11630 if (expanded_types)
11631 /* We're on the Ith parameter of the function parameter
11632 pack. */
11634 /* Get the Ith type. */
11635 type = TREE_VEC_ELT (expanded_types, i);
11637 /* Rename the parameter to include the index. */
11638 DECL_NAME (r)
11639 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11641 else if (!type)
11642 /* We're dealing with a normal parameter. */
11643 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11645 type = type_decays_to (type);
11646 TREE_TYPE (r) = type;
11647 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11649 if (DECL_INITIAL (r))
11651 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
11652 DECL_INITIAL (r) = TREE_TYPE (r);
11653 else
11654 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
11655 complain, in_decl);
11658 DECL_CONTEXT (r) = NULL_TREE;
11660 if (!DECL_TEMPLATE_PARM_P (r))
11661 DECL_ARG_TYPE (r) = type_passed_as (type);
11663 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11664 args, complain, in_decl);
11666 /* Keep track of the first new parameter we
11667 generate. That's what will be returned to the
11668 caller. */
11669 if (!first_r)
11670 first_r = r;
11672 /* Build a proper chain of parameters when substituting
11673 into a function parameter pack. */
11674 if (prev_r)
11675 DECL_CHAIN (prev_r) = r;
11678 /* If cp_unevaluated_operand is set, we're just looking for a
11679 single dummy parameter, so don't keep going. */
11680 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
11681 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
11682 complain, DECL_CHAIN (t));
11684 /* FIRST_R contains the start of the chain we've built. */
11685 r = first_r;
11687 break;
11689 case FIELD_DECL:
11691 tree type = NULL_TREE;
11692 tree vec = NULL_TREE;
11693 tree expanded_types = NULL_TREE;
11694 int len = 1;
11696 if (PACK_EXPANSION_P (TREE_TYPE (t)))
11698 /* This field is a lambda capture pack. Return a TREE_VEC of
11699 the expanded fields to instantiate_class_template_1 and
11700 store them in the specializations hash table as a
11701 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11702 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11703 complain, in_decl);
11704 if (TREE_CODE (expanded_types) == TREE_VEC)
11706 len = TREE_VEC_LENGTH (expanded_types);
11707 vec = make_tree_vec (len);
11709 else
11711 /* All we did was update the type. Make a note of that. */
11712 type = expanded_types;
11713 expanded_types = NULL_TREE;
11717 for (int i = 0; i < len; ++i)
11719 r = copy_decl (t);
11720 if (expanded_types)
11722 type = TREE_VEC_ELT (expanded_types, i);
11723 DECL_NAME (r)
11724 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11726 else if (!type)
11727 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11729 if (type == error_mark_node)
11730 RETURN (error_mark_node);
11731 TREE_TYPE (r) = type;
11732 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11734 if (DECL_C_BIT_FIELD (r))
11735 /* For bit-fields, DECL_INITIAL gives the number of bits. For
11736 non-bit-fields DECL_INITIAL is a non-static data member
11737 initializer, which gets deferred instantiation. */
11738 DECL_INITIAL (r)
11739 = tsubst_expr (DECL_INITIAL (t), args,
11740 complain, in_decl,
11741 /*integral_constant_expression_p=*/true);
11742 else if (DECL_INITIAL (t))
11744 /* Set up DECL_TEMPLATE_INFO so that we can get at the
11745 NSDMI in perform_member_init. Still set DECL_INITIAL
11746 so that we know there is one. */
11747 DECL_INITIAL (r) = void_node;
11748 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
11749 retrofit_lang_decl (r);
11750 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11752 /* We don't have to set DECL_CONTEXT here; it is set by
11753 finish_member_declaration. */
11754 DECL_CHAIN (r) = NULL_TREE;
11756 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11757 args, complain, in_decl);
11759 if (vec)
11760 TREE_VEC_ELT (vec, i) = r;
11763 if (vec)
11765 r = vec;
11766 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
11767 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
11768 SET_ARGUMENT_PACK_ARGS (pack, vec);
11769 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
11770 TREE_TYPE (pack) = tpack;
11771 register_specialization (pack, t, args, false, 0);
11774 break;
11776 case USING_DECL:
11777 /* We reach here only for member using decls. We also need to check
11778 uses_template_parms because DECL_DEPENDENT_P is not set for a
11779 using-declaration that designates a member of the current
11780 instantiation (c++/53549). */
11781 if (DECL_DEPENDENT_P (t)
11782 || uses_template_parms (USING_DECL_SCOPE (t)))
11784 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
11785 complain, in_decl);
11786 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
11787 r = do_class_using_decl (inst_scope, name);
11788 if (!r)
11789 r = error_mark_node;
11790 else
11792 TREE_PROTECTED (r) = TREE_PROTECTED (t);
11793 TREE_PRIVATE (r) = TREE_PRIVATE (t);
11796 else
11798 r = copy_node (t);
11799 DECL_CHAIN (r) = NULL_TREE;
11801 break;
11803 case TYPE_DECL:
11804 case VAR_DECL:
11806 tree argvec = NULL_TREE;
11807 tree gen_tmpl = NULL_TREE;
11808 tree spec;
11809 tree tmpl = NULL_TREE;
11810 tree ctx;
11811 tree type = NULL_TREE;
11812 bool local_p;
11814 if (TREE_TYPE (t) == error_mark_node)
11815 RETURN (error_mark_node);
11817 if (TREE_CODE (t) == TYPE_DECL
11818 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
11820 /* If this is the canonical decl, we don't have to
11821 mess with instantiations, and often we can't (for
11822 typename, template type parms and such). Note that
11823 TYPE_NAME is not correct for the above test if
11824 we've copied the type for a typedef. */
11825 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11826 if (type == error_mark_node)
11827 RETURN (error_mark_node);
11828 r = TYPE_NAME (type);
11829 break;
11832 /* Check to see if we already have the specialization we
11833 need. */
11834 spec = NULL_TREE;
11835 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
11837 /* T is a static data member or namespace-scope entity.
11838 We have to substitute into namespace-scope variables
11839 (even though such entities are never templates) because
11840 of cases like:
11842 template <class T> void f() { extern T t; }
11844 where the entity referenced is not known until
11845 instantiation time. */
11846 local_p = false;
11847 ctx = DECL_CONTEXT (t);
11848 if (DECL_CLASS_SCOPE_P (t))
11850 ctx = tsubst_aggr_type (ctx, args,
11851 complain,
11852 in_decl, /*entering_scope=*/1);
11853 /* If CTX is unchanged, then T is in fact the
11854 specialization we want. That situation occurs when
11855 referencing a static data member within in its own
11856 class. We can use pointer equality, rather than
11857 same_type_p, because DECL_CONTEXT is always
11858 canonical... */
11859 if (ctx == DECL_CONTEXT (t)
11860 /* ... unless T is a member template; in which
11861 case our caller can be willing to create a
11862 specialization of that template represented
11863 by T.
11865 NOTE: The extra check for DECL_TEMPLATE_INFO
11866 is apparenly needed when checking constraints
11867 for associated type names when the requirement
11868 is satisfied by an alias declaration. */
11869 && !(DECL_TEMPLATE_INFO (t)
11870 && DECL_TI_TEMPLATE (t)
11871 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
11872 spec = t;
11875 if (!spec && DECL_LANG_SPECIFIC (t))
11877 tmpl = DECL_TI_TEMPLATE (t);
11878 gen_tmpl = most_general_template (tmpl);
11879 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
11880 if (argvec == error_mark_node)
11881 RETURN (error_mark_node);
11882 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11883 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11886 else
11888 /* A local variable. */
11889 local_p = true;
11890 /* Subsequent calls to pushdecl will fill this in. */
11891 ctx = NULL_TREE;
11892 spec = retrieve_local_specialization (t);
11894 /* If we already have the specialization we need, there is
11895 nothing more to do. */
11896 if (spec)
11898 r = spec;
11899 break;
11902 /* Create a new node for the specialization we need. */
11903 r = copy_decl (t);
11904 if (type == NULL_TREE)
11906 if (is_typedef_decl (t))
11907 type = DECL_ORIGINAL_TYPE (t);
11908 else
11909 type = TREE_TYPE (t);
11910 if (VAR_P (t)
11911 && VAR_HAD_UNKNOWN_BOUND (t)
11912 && type != error_mark_node)
11913 type = strip_array_domain (type);
11914 type = tsubst (type, args, complain, in_decl);
11916 if (VAR_P (r))
11918 /* Even if the original location is out of scope, the
11919 newly substituted one is not. */
11920 DECL_DEAD_FOR_LOCAL (r) = 0;
11921 DECL_INITIALIZED_P (r) = 0;
11922 DECL_TEMPLATE_INSTANTIATED (r) = 0;
11923 if (type == error_mark_node)
11924 RETURN (error_mark_node);
11925 if (TREE_CODE (type) == FUNCTION_TYPE)
11927 /* It may seem that this case cannot occur, since:
11929 typedef void f();
11930 void g() { f x; }
11932 declares a function, not a variable. However:
11934 typedef void f();
11935 template <typename T> void g() { T t; }
11936 template void g<f>();
11938 is an attempt to declare a variable with function
11939 type. */
11940 error ("variable %qD has function type",
11941 /* R is not yet sufficiently initialized, so we
11942 just use its name. */
11943 DECL_NAME (r));
11944 RETURN (error_mark_node);
11946 type = complete_type (type);
11947 /* Wait until cp_finish_decl to set this again, to handle
11948 circular dependency (template/instantiate6.C). */
11949 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
11950 type = check_var_type (DECL_NAME (r), type);
11952 if (DECL_HAS_VALUE_EXPR_P (t))
11954 tree ve = DECL_VALUE_EXPR (t);
11955 ve = tsubst_expr (ve, args, complain, in_decl,
11956 /*constant_expression_p=*/false);
11957 if (REFERENCE_REF_P (ve))
11959 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
11960 ve = TREE_OPERAND (ve, 0);
11962 SET_DECL_VALUE_EXPR (r, ve);
11964 if (TREE_STATIC (r) || DECL_EXTERNAL (r))
11965 set_decl_tls_model (r, decl_tls_model (t));
11967 else if (DECL_SELF_REFERENCE_P (t))
11968 SET_DECL_SELF_REFERENCE_P (r);
11969 TREE_TYPE (r) = type;
11970 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11971 DECL_CONTEXT (r) = ctx;
11972 /* Clear out the mangled name and RTL for the instantiation. */
11973 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11974 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11975 SET_DECL_RTL (r, NULL);
11976 /* The initializer must not be expanded until it is required;
11977 see [temp.inst]. */
11978 DECL_INITIAL (r) = NULL_TREE;
11979 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11980 SET_DECL_RTL (r, NULL);
11981 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
11982 if (VAR_P (r))
11984 /* Possibly limit visibility based on template args. */
11985 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11986 if (DECL_VISIBILITY_SPECIFIED (t))
11988 DECL_VISIBILITY_SPECIFIED (r) = 0;
11989 DECL_ATTRIBUTES (r)
11990 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11992 determine_visibility (r);
11995 if (!local_p && DECL_LANG_SPECIFIC (r))
11997 /* A static data member declaration is always marked
11998 external when it is declared in-class, even if an
11999 initializer is present. We mimic the non-template
12000 processing here. */
12001 DECL_EXTERNAL (r) = 1;
12003 register_specialization (r, gen_tmpl, argvec, false, hash);
12005 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12006 SET_DECL_IMPLICIT_INSTANTIATION (r);
12008 else if (!cp_unevaluated_operand)
12009 register_local_specialization (r, t);
12011 DECL_CHAIN (r) = NULL_TREE;
12013 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12014 /*flags=*/0,
12015 args, complain, in_decl);
12017 /* Preserve a typedef that names a type. */
12018 if (is_typedef_decl (r))
12020 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12021 set_underlying_type (r);
12024 layout_decl (r, 0);
12026 break;
12028 default:
12029 gcc_unreachable ();
12031 #undef RETURN
12033 out:
12034 /* Restore the file and line information. */
12035 input_location = saved_loc;
12037 return r;
12040 /* Substitute into the ARG_TYPES of a function type.
12041 If END is a TREE_CHAIN, leave it and any following types
12042 un-substituted. */
12044 static tree
12045 tsubst_arg_types (tree arg_types,
12046 tree args,
12047 tree end,
12048 tsubst_flags_t complain,
12049 tree in_decl)
12051 tree remaining_arg_types;
12052 tree type = NULL_TREE;
12053 int i = 1;
12054 tree expanded_args = NULL_TREE;
12055 tree default_arg;
12057 if (!arg_types || arg_types == void_list_node || arg_types == end)
12058 return arg_types;
12060 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12061 args, end, complain, in_decl);
12062 if (remaining_arg_types == error_mark_node)
12063 return error_mark_node;
12065 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12067 /* For a pack expansion, perform substitution on the
12068 entire expression. Later on, we'll handle the arguments
12069 one-by-one. */
12070 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12071 args, complain, in_decl);
12073 if (TREE_CODE (expanded_args) == TREE_VEC)
12074 /* So that we'll spin through the parameters, one by one. */
12075 i = TREE_VEC_LENGTH (expanded_args);
12076 else
12078 /* We only partially substituted into the parameter
12079 pack. Our type is TYPE_PACK_EXPANSION. */
12080 type = expanded_args;
12081 expanded_args = NULL_TREE;
12085 while (i > 0) {
12086 --i;
12088 if (expanded_args)
12089 type = TREE_VEC_ELT (expanded_args, i);
12090 else if (!type)
12091 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
12093 if (type == error_mark_node)
12094 return error_mark_node;
12095 if (VOID_TYPE_P (type))
12097 if (complain & tf_error)
12099 error ("invalid parameter type %qT", type);
12100 if (in_decl)
12101 error ("in declaration %q+D", in_decl);
12103 return error_mark_node;
12105 /* DR 657. */
12106 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
12107 return error_mark_node;
12109 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12110 top-level qualifiers as required. */
12111 type = cv_unqualified (type_decays_to (type));
12113 /* We do not substitute into default arguments here. The standard
12114 mandates that they be instantiated only when needed, which is
12115 done in build_over_call. */
12116 default_arg = TREE_PURPOSE (arg_types);
12118 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
12120 /* We've instantiated a template before its default arguments
12121 have been parsed. This can happen for a nested template
12122 class, and is not an error unless we require the default
12123 argument in a call of this function. */
12124 remaining_arg_types =
12125 tree_cons (default_arg, type, remaining_arg_types);
12126 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
12128 else
12129 remaining_arg_types =
12130 hash_tree_cons (default_arg, type, remaining_arg_types);
12133 return remaining_arg_types;
12136 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12137 *not* handle the exception-specification for FNTYPE, because the
12138 initial substitution of explicitly provided template parameters
12139 during argument deduction forbids substitution into the
12140 exception-specification:
12142 [temp.deduct]
12144 All references in the function type of the function template to the
12145 corresponding template parameters are replaced by the specified tem-
12146 plate argument values. If a substitution in a template parameter or
12147 in the function type of the function template results in an invalid
12148 type, type deduction fails. [Note: The equivalent substitution in
12149 exception specifications is done only when the function is instanti-
12150 ated, at which point a program is ill-formed if the substitution
12151 results in an invalid type.] */
12153 static tree
12154 tsubst_function_type (tree t,
12155 tree args,
12156 tsubst_flags_t complain,
12157 tree in_decl)
12159 tree return_type;
12160 tree arg_types = NULL_TREE;
12161 tree fntype;
12163 /* The TYPE_CONTEXT is not used for function/method types. */
12164 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
12166 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
12167 failure. */
12168 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
12170 if (late_return_type_p)
12172 /* Substitute the argument types. */
12173 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12174 complain, in_decl);
12175 if (arg_types == error_mark_node)
12176 return error_mark_node;
12178 tree save_ccp = current_class_ptr;
12179 tree save_ccr = current_class_ref;
12180 tree this_type = (TREE_CODE (t) == METHOD_TYPE
12181 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
12182 bool do_inject = this_type && CLASS_TYPE_P (this_type);
12183 if (do_inject)
12185 /* DR 1207: 'this' is in scope in the trailing return type. */
12186 inject_this_parameter (this_type, cp_type_quals (this_type));
12189 /* Substitute the return type. */
12190 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12192 if (do_inject)
12194 current_class_ptr = save_ccp;
12195 current_class_ref = save_ccr;
12198 else
12199 /* Substitute the return type. */
12200 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12202 if (return_type == error_mark_node)
12203 return error_mark_node;
12204 /* DR 486 clarifies that creation of a function type with an
12205 invalid return type is a deduction failure. */
12206 if (TREE_CODE (return_type) == ARRAY_TYPE
12207 || TREE_CODE (return_type) == FUNCTION_TYPE)
12209 if (complain & tf_error)
12211 if (TREE_CODE (return_type) == ARRAY_TYPE)
12212 error ("function returning an array");
12213 else
12214 error ("function returning a function");
12216 return error_mark_node;
12218 /* And DR 657. */
12219 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
12220 return error_mark_node;
12222 if (!late_return_type_p)
12224 /* Substitute the argument types. */
12225 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12226 complain, in_decl);
12227 if (arg_types == error_mark_node)
12228 return error_mark_node;
12231 /* Construct a new type node and return it. */
12232 if (TREE_CODE (t) == FUNCTION_TYPE)
12234 fntype = build_function_type (return_type, arg_types);
12235 fntype = apply_memfn_quals (fntype,
12236 type_memfn_quals (t),
12237 type_memfn_rqual (t));
12239 else
12241 tree r = TREE_TYPE (TREE_VALUE (arg_types));
12242 /* Don't pick up extra function qualifiers from the basetype. */
12243 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
12244 if (! MAYBE_CLASS_TYPE_P (r))
12246 /* [temp.deduct]
12248 Type deduction may fail for any of the following
12249 reasons:
12251 -- Attempting to create "pointer to member of T" when T
12252 is not a class type. */
12253 if (complain & tf_error)
12254 error ("creating pointer to member function of non-class type %qT",
12256 return error_mark_node;
12259 fntype = build_method_type_directly (r, return_type,
12260 TREE_CHAIN (arg_types));
12261 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
12263 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
12265 if (late_return_type_p)
12266 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
12268 return fntype;
12271 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
12272 ARGS into that specification, and return the substituted
12273 specification. If there is no specification, return NULL_TREE. */
12275 static tree
12276 tsubst_exception_specification (tree fntype,
12277 tree args,
12278 tsubst_flags_t complain,
12279 tree in_decl,
12280 bool defer_ok)
12282 tree specs;
12283 tree new_specs;
12285 specs = TYPE_RAISES_EXCEPTIONS (fntype);
12286 new_specs = NULL_TREE;
12287 if (specs && TREE_PURPOSE (specs))
12289 /* A noexcept-specifier. */
12290 tree expr = TREE_PURPOSE (specs);
12291 if (TREE_CODE (expr) == INTEGER_CST)
12292 new_specs = expr;
12293 else if (defer_ok)
12295 /* Defer instantiation of noexcept-specifiers to avoid
12296 excessive instantiations (c++/49107). */
12297 new_specs = make_node (DEFERRED_NOEXCEPT);
12298 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
12300 /* We already partially instantiated this member template,
12301 so combine the new args with the old. */
12302 DEFERRED_NOEXCEPT_PATTERN (new_specs)
12303 = DEFERRED_NOEXCEPT_PATTERN (expr);
12304 DEFERRED_NOEXCEPT_ARGS (new_specs)
12305 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
12307 else
12309 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
12310 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
12313 else
12314 new_specs = tsubst_copy_and_build
12315 (expr, args, complain, in_decl, /*function_p=*/false,
12316 /*integral_constant_expression_p=*/true);
12317 new_specs = build_noexcept_spec (new_specs, complain);
12319 else if (specs)
12321 if (! TREE_VALUE (specs))
12322 new_specs = specs;
12323 else
12324 while (specs)
12326 tree spec;
12327 int i, len = 1;
12328 tree expanded_specs = NULL_TREE;
12330 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
12332 /* Expand the pack expansion type. */
12333 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
12334 args, complain,
12335 in_decl);
12337 if (expanded_specs == error_mark_node)
12338 return error_mark_node;
12339 else if (TREE_CODE (expanded_specs) == TREE_VEC)
12340 len = TREE_VEC_LENGTH (expanded_specs);
12341 else
12343 /* We're substituting into a member template, so
12344 we got a TYPE_PACK_EXPANSION back. Add that
12345 expansion and move on. */
12346 gcc_assert (TREE_CODE (expanded_specs)
12347 == TYPE_PACK_EXPANSION);
12348 new_specs = add_exception_specifier (new_specs,
12349 expanded_specs,
12350 complain);
12351 specs = TREE_CHAIN (specs);
12352 continue;
12356 for (i = 0; i < len; ++i)
12358 if (expanded_specs)
12359 spec = TREE_VEC_ELT (expanded_specs, i);
12360 else
12361 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
12362 if (spec == error_mark_node)
12363 return spec;
12364 new_specs = add_exception_specifier (new_specs, spec,
12365 complain);
12368 specs = TREE_CHAIN (specs);
12371 return new_specs;
12374 /* Take the tree structure T and replace template parameters used
12375 therein with the argument vector ARGS. IN_DECL is an associated
12376 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
12377 Issue error and warning messages under control of COMPLAIN. Note
12378 that we must be relatively non-tolerant of extensions here, in
12379 order to preserve conformance; if we allow substitutions that
12380 should not be allowed, we may allow argument deductions that should
12381 not succeed, and therefore report ambiguous overload situations
12382 where there are none. In theory, we could allow the substitution,
12383 but indicate that it should have failed, and allow our caller to
12384 make sure that the right thing happens, but we don't try to do this
12385 yet.
12387 This function is used for dealing with types, decls and the like;
12388 for expressions, use tsubst_expr or tsubst_copy. */
12390 tree
12391 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12393 enum tree_code code;
12394 tree type, r = NULL_TREE;
12396 if (t == NULL_TREE || t == error_mark_node
12397 || t == integer_type_node
12398 || t == void_type_node
12399 || t == char_type_node
12400 || t == unknown_type_node
12401 || TREE_CODE (t) == NAMESPACE_DECL
12402 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
12403 return t;
12405 if (DECL_P (t))
12406 return tsubst_decl (t, args, complain);
12408 if (args == NULL_TREE)
12409 return t;
12411 code = TREE_CODE (t);
12413 if (code == IDENTIFIER_NODE)
12414 type = IDENTIFIER_TYPE_VALUE (t);
12415 else
12416 type = TREE_TYPE (t);
12418 gcc_assert (type != unknown_type_node);
12420 /* Reuse typedefs. We need to do this to handle dependent attributes,
12421 such as attribute aligned. */
12422 if (TYPE_P (t)
12423 && typedef_variant_p (t))
12425 tree decl = TYPE_NAME (t);
12427 if (alias_template_specialization_p (t))
12429 /* DECL represents an alias template and we want to
12430 instantiate it. */
12431 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12432 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12433 r = instantiate_alias_template (tmpl, gen_args, complain);
12435 else if (DECL_CLASS_SCOPE_P (decl)
12436 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
12437 && uses_template_parms (DECL_CONTEXT (decl)))
12439 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12440 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12441 r = retrieve_specialization (tmpl, gen_args, 0);
12443 else if (DECL_FUNCTION_SCOPE_P (decl)
12444 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
12445 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
12446 r = retrieve_local_specialization (decl);
12447 else
12448 /* The typedef is from a non-template context. */
12449 return t;
12451 if (r)
12453 r = TREE_TYPE (r);
12454 r = cp_build_qualified_type_real
12455 (r, cp_type_quals (t) | cp_type_quals (r),
12456 complain | tf_ignore_bad_quals);
12457 return r;
12459 else
12461 /* We don't have an instantiation yet, so drop the typedef. */
12462 int quals = cp_type_quals (t);
12463 t = DECL_ORIGINAL_TYPE (decl);
12464 t = cp_build_qualified_type_real (t, quals,
12465 complain | tf_ignore_bad_quals);
12469 if (type
12470 && code != TYPENAME_TYPE
12471 && code != TEMPLATE_TYPE_PARM
12472 && code != IDENTIFIER_NODE
12473 && code != FUNCTION_TYPE
12474 && code != METHOD_TYPE)
12475 type = tsubst (type, args, complain, in_decl);
12476 if (type == error_mark_node)
12477 return error_mark_node;
12479 switch (code)
12481 case RECORD_TYPE:
12482 case UNION_TYPE:
12483 case ENUMERAL_TYPE:
12484 return tsubst_aggr_type (t, args, complain, in_decl,
12485 /*entering_scope=*/0);
12487 case ERROR_MARK:
12488 case IDENTIFIER_NODE:
12489 case VOID_TYPE:
12490 case REAL_TYPE:
12491 case COMPLEX_TYPE:
12492 case VECTOR_TYPE:
12493 case BOOLEAN_TYPE:
12494 case NULLPTR_TYPE:
12495 case LANG_TYPE:
12496 return t;
12498 case INTEGER_TYPE:
12499 if (t == integer_type_node)
12500 return t;
12502 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
12503 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
12504 return t;
12507 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
12509 max = tsubst_expr (omax, args, complain, in_decl,
12510 /*integral_constant_expression_p=*/false);
12512 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
12513 needed. */
12514 if (TREE_CODE (max) == NOP_EXPR
12515 && TREE_SIDE_EFFECTS (omax)
12516 && !TREE_TYPE (max))
12517 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
12519 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
12520 with TREE_SIDE_EFFECTS that indicates this is not an integral
12521 constant expression. */
12522 if (processing_template_decl
12523 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
12525 gcc_assert (TREE_CODE (max) == NOP_EXPR);
12526 TREE_SIDE_EFFECTS (max) = 1;
12529 return compute_array_index_type (NULL_TREE, max, complain);
12532 case TEMPLATE_TYPE_PARM:
12533 case TEMPLATE_TEMPLATE_PARM:
12534 case BOUND_TEMPLATE_TEMPLATE_PARM:
12535 case TEMPLATE_PARM_INDEX:
12537 int idx;
12538 int level;
12539 int levels;
12540 tree arg = NULL_TREE;
12542 r = NULL_TREE;
12544 if (!args)
12545 return t;
12547 gcc_assert (TREE_VEC_LENGTH (args) > 0);
12548 template_parm_level_and_index (t, &level, &idx);
12550 levels = TMPL_ARGS_DEPTH (args);
12551 if (level <= levels)
12553 arg = TMPL_ARG (args, level, idx);
12554 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
12556 /* See through ARGUMENT_PACK_SELECT arguments. */
12557 arg = ARGUMENT_PACK_SELECT_ARG (arg);
12558 /* If the selected argument is an expansion E, that most
12559 likely means we were called from
12560 gen_elem_of_pack_expansion_instantiation during the
12561 substituting of pack an argument pack (which Ith
12562 element is a pack expansion, where I is
12563 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
12564 In this case, the Ith element resulting from this
12565 substituting is going to be a pack expansion, which
12566 pattern is the pattern of E. Let's return the
12567 pattern of E, and
12568 gen_elem_of_pack_expansion_instantiation will
12569 build the resulting pack expansion from it. */
12570 if (PACK_EXPANSION_P (arg))
12572 /* Make sure we aren't throwing away arg info. */
12573 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
12574 arg = PACK_EXPANSION_PATTERN (arg);
12579 if (arg == error_mark_node)
12580 return error_mark_node;
12581 else if (arg != NULL_TREE)
12583 if (ARGUMENT_PACK_P (arg))
12584 /* If ARG is an argument pack, we don't actually want to
12585 perform a substitution here, because substitutions
12586 for argument packs are only done
12587 element-by-element. We can get to this point when
12588 substituting the type of a non-type template
12589 parameter pack, when that type actually contains
12590 template parameter packs from an outer template, e.g.,
12592 template<typename... Types> struct A {
12593 template<Types... Values> struct B { };
12594 }; */
12595 return t;
12597 if (code == TEMPLATE_TYPE_PARM)
12599 int quals;
12600 gcc_assert (TYPE_P (arg));
12602 quals = cp_type_quals (arg) | cp_type_quals (t);
12604 return cp_build_qualified_type_real
12605 (arg, quals, complain | tf_ignore_bad_quals);
12607 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12609 /* We are processing a type constructed from a
12610 template template parameter. */
12611 tree argvec = tsubst (TYPE_TI_ARGS (t),
12612 args, complain, in_decl);
12613 if (argvec == error_mark_node)
12614 return error_mark_node;
12616 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
12617 || TREE_CODE (arg) == TEMPLATE_DECL
12618 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
12620 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
12621 /* Consider this code:
12623 template <template <class> class Template>
12624 struct Internal {
12625 template <class Arg> using Bind = Template<Arg>;
12628 template <template <class> class Template, class Arg>
12629 using Instantiate = Template<Arg>; //#0
12631 template <template <class> class Template,
12632 class Argument>
12633 using Bind =
12634 Instantiate<Internal<Template>::template Bind,
12635 Argument>; //#1
12637 When #1 is parsed, the
12638 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12639 parameter `Template' in #0 matches the
12640 UNBOUND_CLASS_TEMPLATE representing the argument
12641 `Internal<Template>::template Bind'; We then want
12642 to assemble the type `Bind<Argument>' that can't
12643 be fully created right now, because
12644 `Internal<Template>' not being complete, the Bind
12645 template cannot be looked up in that context. So
12646 we need to "store" `Bind<Argument>' for later
12647 when the context of Bind becomes complete. Let's
12648 store that in a TYPENAME_TYPE. */
12649 return make_typename_type (TYPE_CONTEXT (arg),
12650 build_nt (TEMPLATE_ID_EXPR,
12651 TYPE_IDENTIFIER (arg),
12652 argvec),
12653 typename_type,
12654 complain);
12656 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12657 are resolving nested-types in the signature of a
12658 member function templates. Otherwise ARG is a
12659 TEMPLATE_DECL and is the real template to be
12660 instantiated. */
12661 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12662 arg = TYPE_NAME (arg);
12664 r = lookup_template_class (arg,
12665 argvec, in_decl,
12666 DECL_CONTEXT (arg),
12667 /*entering_scope=*/0,
12668 complain);
12669 return cp_build_qualified_type_real
12670 (r, cp_type_quals (t) | cp_type_quals (r), complain);
12672 else
12673 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12674 return convert_from_reference (unshare_expr (arg));
12677 if (level == 1)
12678 /* This can happen during the attempted tsubst'ing in
12679 unify. This means that we don't yet have any information
12680 about the template parameter in question. */
12681 return t;
12683 /* Early in template argument deduction substitution, we don't
12684 want to reduce the level of 'auto', or it will be confused
12685 with a normal template parm in subsequent deduction. */
12686 if (is_auto (t) && (complain & tf_partial))
12687 return t;
12689 /* If we get here, we must have been looking at a parm for a
12690 more deeply nested template. Make a new version of this
12691 template parameter, but with a lower level. */
12692 switch (code)
12694 case TEMPLATE_TYPE_PARM:
12695 case TEMPLATE_TEMPLATE_PARM:
12696 case BOUND_TEMPLATE_TEMPLATE_PARM:
12697 if (cp_type_quals (t))
12699 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
12700 r = cp_build_qualified_type_real
12701 (r, cp_type_quals (t),
12702 complain | (code == TEMPLATE_TYPE_PARM
12703 ? tf_ignore_bad_quals : 0));
12705 else
12707 r = copy_type (t);
12708 TEMPLATE_TYPE_PARM_INDEX (r)
12709 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
12710 r, levels, args, complain);
12711 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
12712 TYPE_MAIN_VARIANT (r) = r;
12713 TYPE_POINTER_TO (r) = NULL_TREE;
12714 TYPE_REFERENCE_TO (r) = NULL_TREE;
12716 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
12717 /* We have reduced the level of the template
12718 template parameter, but not the levels of its
12719 template parameters, so canonical_type_parameter
12720 will not be able to find the canonical template
12721 template parameter for this level. Thus, we
12722 require structural equality checking to compare
12723 TEMPLATE_TEMPLATE_PARMs. */
12724 SET_TYPE_STRUCTURAL_EQUALITY (r);
12725 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
12726 SET_TYPE_STRUCTURAL_EQUALITY (r);
12727 else
12728 TYPE_CANONICAL (r) = canonical_type_parameter (r);
12730 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12732 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
12733 complain, in_decl);
12734 if (argvec == error_mark_node)
12735 return error_mark_node;
12737 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
12738 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
12741 break;
12743 case TEMPLATE_PARM_INDEX:
12744 r = reduce_template_parm_level (t, type, levels, args, complain);
12745 break;
12747 default:
12748 gcc_unreachable ();
12751 return r;
12754 case TREE_LIST:
12756 tree purpose, value, chain;
12758 if (t == void_list_node)
12759 return t;
12761 purpose = TREE_PURPOSE (t);
12762 if (purpose)
12764 purpose = tsubst (purpose, args, complain, in_decl);
12765 if (purpose == error_mark_node)
12766 return error_mark_node;
12768 value = TREE_VALUE (t);
12769 if (value)
12771 value = tsubst (value, args, complain, in_decl);
12772 if (value == error_mark_node)
12773 return error_mark_node;
12775 chain = TREE_CHAIN (t);
12776 if (chain && chain != void_type_node)
12778 chain = tsubst (chain, args, complain, in_decl);
12779 if (chain == error_mark_node)
12780 return error_mark_node;
12782 if (purpose == TREE_PURPOSE (t)
12783 && value == TREE_VALUE (t)
12784 && chain == TREE_CHAIN (t))
12785 return t;
12786 return hash_tree_cons (purpose, value, chain);
12789 case TREE_BINFO:
12790 /* We should never be tsubsting a binfo. */
12791 gcc_unreachable ();
12793 case TREE_VEC:
12794 /* A vector of template arguments. */
12795 gcc_assert (!type);
12796 return tsubst_template_args (t, args, complain, in_decl);
12798 case POINTER_TYPE:
12799 case REFERENCE_TYPE:
12801 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
12802 return t;
12804 /* [temp.deduct]
12806 Type deduction may fail for any of the following
12807 reasons:
12809 -- Attempting to create a pointer to reference type.
12810 -- Attempting to create a reference to a reference type or
12811 a reference to void.
12813 Core issue 106 says that creating a reference to a reference
12814 during instantiation is no longer a cause for failure. We
12815 only enforce this check in strict C++98 mode. */
12816 if ((TREE_CODE (type) == REFERENCE_TYPE
12817 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
12818 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
12820 static location_t last_loc;
12822 /* We keep track of the last time we issued this error
12823 message to avoid spewing a ton of messages during a
12824 single bad template instantiation. */
12825 if (complain & tf_error
12826 && last_loc != input_location)
12828 if (VOID_TYPE_P (type))
12829 error ("forming reference to void");
12830 else if (code == POINTER_TYPE)
12831 error ("forming pointer to reference type %qT", type);
12832 else
12833 error ("forming reference to reference type %qT", type);
12834 last_loc = input_location;
12837 return error_mark_node;
12839 else if (TREE_CODE (type) == FUNCTION_TYPE
12840 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12841 || type_memfn_rqual (type) != REF_QUAL_NONE))
12843 if (complain & tf_error)
12845 if (code == POINTER_TYPE)
12846 error ("forming pointer to qualified function type %qT",
12847 type);
12848 else
12849 error ("forming reference to qualified function type %qT",
12850 type);
12852 return error_mark_node;
12854 else if (code == POINTER_TYPE)
12856 r = build_pointer_type (type);
12857 if (TREE_CODE (type) == METHOD_TYPE)
12858 r = build_ptrmemfunc_type (r);
12860 else if (TREE_CODE (type) == REFERENCE_TYPE)
12861 /* In C++0x, during template argument substitution, when there is an
12862 attempt to create a reference to a reference type, reference
12863 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
12865 "If a template-argument for a template-parameter T names a type
12866 that is a reference to a type A, an attempt to create the type
12867 'lvalue reference to cv T' creates the type 'lvalue reference to
12868 A,' while an attempt to create the type type rvalue reference to
12869 cv T' creates the type T"
12871 r = cp_build_reference_type
12872 (TREE_TYPE (type),
12873 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
12874 else
12875 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
12876 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12878 if (r != error_mark_node)
12879 /* Will this ever be needed for TYPE_..._TO values? */
12880 layout_type (r);
12882 return r;
12884 case OFFSET_TYPE:
12886 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
12887 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
12889 /* [temp.deduct]
12891 Type deduction may fail for any of the following
12892 reasons:
12894 -- Attempting to create "pointer to member of T" when T
12895 is not a class type. */
12896 if (complain & tf_error)
12897 error ("creating pointer to member of non-class type %qT", r);
12898 return error_mark_node;
12900 if (TREE_CODE (type) == REFERENCE_TYPE)
12902 if (complain & tf_error)
12903 error ("creating pointer to member reference type %qT", type);
12904 return error_mark_node;
12906 if (VOID_TYPE_P (type))
12908 if (complain & tf_error)
12909 error ("creating pointer to member of type void");
12910 return error_mark_node;
12912 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12913 if (TREE_CODE (type) == FUNCTION_TYPE)
12915 /* The type of the implicit object parameter gets its
12916 cv-qualifiers from the FUNCTION_TYPE. */
12917 tree memptr;
12918 tree method_type
12919 = build_memfn_type (type, r, type_memfn_quals (type),
12920 type_memfn_rqual (type));
12921 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
12922 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
12923 complain);
12925 else
12926 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
12927 cp_type_quals (t),
12928 complain);
12930 case FUNCTION_TYPE:
12931 case METHOD_TYPE:
12933 tree fntype;
12934 tree specs;
12935 fntype = tsubst_function_type (t, args, complain, in_decl);
12936 if (fntype == error_mark_node)
12937 return error_mark_node;
12939 /* Substitute the exception specification. */
12940 specs = tsubst_exception_specification (t, args, complain,
12941 in_decl, /*defer_ok*/true);
12942 if (specs == error_mark_node)
12943 return error_mark_node;
12944 if (specs)
12945 fntype = build_exception_variant (fntype, specs);
12946 return fntype;
12948 case ARRAY_TYPE:
12950 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
12951 if (domain == error_mark_node)
12952 return error_mark_node;
12954 /* As an optimization, we avoid regenerating the array type if
12955 it will obviously be the same as T. */
12956 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
12957 return t;
12959 /* These checks should match the ones in create_array_type_for_decl.
12961 [temp.deduct]
12963 The deduction may fail for any of the following reasons:
12965 -- Attempting to create an array with an element type that
12966 is void, a function type, or a reference type, or [DR337]
12967 an abstract class type. */
12968 if (VOID_TYPE_P (type)
12969 || TREE_CODE (type) == FUNCTION_TYPE
12970 || (TREE_CODE (type) == ARRAY_TYPE
12971 && TYPE_DOMAIN (type) == NULL_TREE)
12972 || TREE_CODE (type) == REFERENCE_TYPE)
12974 if (complain & tf_error)
12975 error ("creating array of %qT", type);
12976 return error_mark_node;
12979 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
12980 return error_mark_node;
12982 r = build_cplus_array_type (type, domain);
12984 if (TYPE_USER_ALIGN (t))
12986 TYPE_ALIGN (r) = TYPE_ALIGN (t);
12987 TYPE_USER_ALIGN (r) = 1;
12990 return r;
12993 case TYPENAME_TYPE:
12995 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12996 in_decl, /*entering_scope=*/1);
12997 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
12998 complain, in_decl);
13000 if (ctx == error_mark_node || f == error_mark_node)
13001 return error_mark_node;
13003 if (!MAYBE_CLASS_TYPE_P (ctx))
13005 if (complain & tf_error)
13006 error ("%qT is not a class, struct, or union type", ctx);
13007 return error_mark_node;
13009 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13011 /* Normally, make_typename_type does not require that the CTX
13012 have complete type in order to allow things like:
13014 template <class T> struct S { typename S<T>::X Y; };
13016 But, such constructs have already been resolved by this
13017 point, so here CTX really should have complete type, unless
13018 it's a partial instantiation. */
13019 ctx = complete_type (ctx);
13020 if (!COMPLETE_TYPE_P (ctx))
13022 if (complain & tf_error)
13023 cxx_incomplete_type_error (NULL_TREE, ctx);
13024 return error_mark_node;
13028 f = make_typename_type (ctx, f, typename_type,
13029 complain | tf_keep_type_decl);
13030 if (f == error_mark_node)
13031 return f;
13032 if (TREE_CODE (f) == TYPE_DECL)
13034 complain |= tf_ignore_bad_quals;
13035 f = TREE_TYPE (f);
13038 if (TREE_CODE (f) != TYPENAME_TYPE)
13040 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13042 if (complain & tf_error)
13043 error ("%qT resolves to %qT, which is not an enumeration type",
13044 t, f);
13045 else
13046 return error_mark_node;
13048 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
13050 if (complain & tf_error)
13051 error ("%qT resolves to %qT, which is is not a class type",
13052 t, f);
13053 else
13054 return error_mark_node;
13058 return cp_build_qualified_type_real
13059 (f, cp_type_quals (f) | cp_type_quals (t), complain);
13062 case UNBOUND_CLASS_TEMPLATE:
13064 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13065 in_decl, /*entering_scope=*/1);
13066 tree name = TYPE_IDENTIFIER (t);
13067 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
13069 if (ctx == error_mark_node || name == error_mark_node)
13070 return error_mark_node;
13072 if (parm_list)
13073 parm_list = tsubst_template_parms (parm_list, args, complain);
13074 return make_unbound_class_template (ctx, name, parm_list, complain);
13077 case TYPEOF_TYPE:
13079 tree type;
13081 ++cp_unevaluated_operand;
13082 ++c_inhibit_evaluation_warnings;
13084 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
13085 complain, in_decl,
13086 /*integral_constant_expression_p=*/false);
13088 --cp_unevaluated_operand;
13089 --c_inhibit_evaluation_warnings;
13091 type = finish_typeof (type);
13092 return cp_build_qualified_type_real (type,
13093 cp_type_quals (t)
13094 | cp_type_quals (type),
13095 complain);
13098 case DECLTYPE_TYPE:
13100 tree type;
13102 ++cp_unevaluated_operand;
13103 ++c_inhibit_evaluation_warnings;
13105 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
13106 complain|tf_decltype, in_decl,
13107 /*function_p*/false,
13108 /*integral_constant_expression*/false);
13110 --cp_unevaluated_operand;
13111 --c_inhibit_evaluation_warnings;
13113 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
13114 type = lambda_capture_field_type (type,
13115 DECLTYPE_FOR_INIT_CAPTURE (t));
13116 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
13117 type = lambda_proxy_type (type);
13118 else
13120 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
13121 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
13122 && EXPR_P (type))
13123 /* In a template ~id could be either a complement expression
13124 or an unqualified-id naming a destructor; if instantiating
13125 it produces an expression, it's not an id-expression or
13126 member access. */
13127 id = false;
13128 type = finish_decltype_type (type, id, complain);
13130 return cp_build_qualified_type_real (type,
13131 cp_type_quals (t)
13132 | cp_type_quals (type),
13133 complain | tf_ignore_bad_quals);
13136 case UNDERLYING_TYPE:
13138 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
13139 complain, in_decl);
13140 return finish_underlying_type (type);
13143 case TYPE_ARGUMENT_PACK:
13144 case NONTYPE_ARGUMENT_PACK:
13146 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
13147 tree packed_out =
13148 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
13149 args,
13150 complain,
13151 in_decl);
13152 SET_ARGUMENT_PACK_ARGS (r, packed_out);
13154 /* For template nontype argument packs, also substitute into
13155 the type. */
13156 if (code == NONTYPE_ARGUMENT_PACK)
13157 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
13159 return r;
13161 break;
13163 case VOID_CST:
13164 case INTEGER_CST:
13165 case REAL_CST:
13166 case STRING_CST:
13167 case PLUS_EXPR:
13168 case MINUS_EXPR:
13169 case NEGATE_EXPR:
13170 case NOP_EXPR:
13171 case INDIRECT_REF:
13172 case ADDR_EXPR:
13173 case CALL_EXPR:
13174 case ARRAY_REF:
13175 case SCOPE_REF:
13176 /* We should use one of the expression tsubsts for these codes. */
13177 gcc_unreachable ();
13179 default:
13180 sorry ("use of %qs in template", get_tree_code_name (code));
13181 return error_mark_node;
13185 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
13186 type of the expression on the left-hand side of the "." or "->"
13187 operator. */
13189 static tree
13190 tsubst_baselink (tree baselink, tree object_type,
13191 tree args, tsubst_flags_t complain, tree in_decl)
13193 tree name;
13194 tree qualifying_scope;
13195 tree fns;
13196 tree optype;
13197 tree template_args = 0;
13198 bool template_id_p = false;
13199 bool qualified = BASELINK_QUALIFIED_P (baselink);
13201 /* A baselink indicates a function from a base class. Both the
13202 BASELINK_ACCESS_BINFO and the base class referenced may
13203 indicate bases of the template class, rather than the
13204 instantiated class. In addition, lookups that were not
13205 ambiguous before may be ambiguous now. Therefore, we perform
13206 the lookup again. */
13207 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
13208 qualifying_scope = tsubst (qualifying_scope, args,
13209 complain, in_decl);
13210 fns = BASELINK_FUNCTIONS (baselink);
13211 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
13212 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
13214 template_id_p = true;
13215 template_args = TREE_OPERAND (fns, 1);
13216 fns = TREE_OPERAND (fns, 0);
13217 if (template_args)
13218 template_args = tsubst_template_args (template_args, args,
13219 complain, in_decl);
13221 name = DECL_NAME (get_first_fn (fns));
13222 if (IDENTIFIER_TYPENAME_P (name))
13223 name = mangle_conv_op_name_for_type (optype);
13224 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
13225 if (!baselink)
13226 return error_mark_node;
13228 /* If lookup found a single function, mark it as used at this
13229 point. (If it lookup found multiple functions the one selected
13230 later by overload resolution will be marked as used at that
13231 point.) */
13232 if (BASELINK_P (baselink))
13233 fns = BASELINK_FUNCTIONS (baselink);
13234 if (!template_id_p && !really_overloaded_fn (fns))
13235 mark_used (OVL_CURRENT (fns), complain);
13237 /* Add back the template arguments, if present. */
13238 if (BASELINK_P (baselink) && template_id_p)
13239 BASELINK_FUNCTIONS (baselink)
13240 = build_nt (TEMPLATE_ID_EXPR,
13241 BASELINK_FUNCTIONS (baselink),
13242 template_args);
13243 /* Update the conversion operator type. */
13244 BASELINK_OPTYPE (baselink) = optype;
13246 if (!object_type)
13247 object_type = current_class_type;
13249 if (qualified)
13250 baselink = adjust_result_of_qualified_name_lookup (baselink,
13251 qualifying_scope,
13252 object_type);
13253 return baselink;
13256 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
13257 true if the qualified-id will be a postfix-expression in-and-of
13258 itself; false if more of the postfix-expression follows the
13259 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
13260 of "&". */
13262 static tree
13263 tsubst_qualified_id (tree qualified_id, tree args,
13264 tsubst_flags_t complain, tree in_decl,
13265 bool done, bool address_p)
13267 tree expr;
13268 tree scope;
13269 tree name;
13270 bool is_template;
13271 tree template_args;
13272 location_t loc = UNKNOWN_LOCATION;
13274 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
13276 /* Figure out what name to look up. */
13277 name = TREE_OPERAND (qualified_id, 1);
13278 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13280 is_template = true;
13281 loc = EXPR_LOCATION (name);
13282 template_args = TREE_OPERAND (name, 1);
13283 if (template_args)
13284 template_args = tsubst_template_args (template_args, args,
13285 complain, in_decl);
13286 name = TREE_OPERAND (name, 0);
13288 else
13290 is_template = false;
13291 template_args = NULL_TREE;
13294 /* Substitute into the qualifying scope. When there are no ARGS, we
13295 are just trying to simplify a non-dependent expression. In that
13296 case the qualifying scope may be dependent, and, in any case,
13297 substituting will not help. */
13298 scope = TREE_OPERAND (qualified_id, 0);
13299 if (args)
13301 scope = tsubst (scope, args, complain, in_decl);
13302 expr = tsubst_copy (name, args, complain, in_decl);
13304 else
13305 expr = name;
13307 if (dependent_scope_p (scope))
13309 if (is_template)
13310 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
13311 return build_qualified_name (NULL_TREE, scope, expr,
13312 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
13315 if (!BASELINK_P (name) && !DECL_P (expr))
13317 if (TREE_CODE (expr) == BIT_NOT_EXPR)
13319 /* A BIT_NOT_EXPR is used to represent a destructor. */
13320 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
13322 error ("qualifying type %qT does not match destructor name ~%qT",
13323 scope, TREE_OPERAND (expr, 0));
13324 expr = error_mark_node;
13326 else
13327 expr = lookup_qualified_name (scope, complete_dtor_identifier,
13328 /*is_type_p=*/0, false);
13330 else
13331 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
13332 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
13333 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
13335 if (complain & tf_error)
13337 error ("dependent-name %qE is parsed as a non-type, but "
13338 "instantiation yields a type", qualified_id);
13339 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
13341 return error_mark_node;
13345 if (DECL_P (expr))
13347 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
13348 scope);
13349 /* Remember that there was a reference to this entity. */
13350 mark_used (expr);
13353 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
13355 if (complain & tf_error)
13356 qualified_name_lookup_error (scope,
13357 TREE_OPERAND (qualified_id, 1),
13358 expr, input_location);
13359 return error_mark_node;
13362 if (is_template)
13363 expr = lookup_template_function (expr, template_args);
13365 if (expr == error_mark_node && complain & tf_error)
13366 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
13367 expr, input_location);
13368 else if (TYPE_P (scope))
13370 expr = (adjust_result_of_qualified_name_lookup
13371 (expr, scope, current_nonlambda_class_type ()));
13372 expr = (finish_qualified_id_expr
13373 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
13374 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
13375 /*template_arg_p=*/false, complain));
13378 /* Expressions do not generally have reference type. */
13379 if (TREE_CODE (expr) != SCOPE_REF
13380 /* However, if we're about to form a pointer-to-member, we just
13381 want the referenced member referenced. */
13382 && TREE_CODE (expr) != OFFSET_REF)
13383 expr = convert_from_reference (expr);
13385 return expr;
13388 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
13389 initializer, DECL is the substituted VAR_DECL. Other arguments are as
13390 for tsubst. */
13392 static tree
13393 tsubst_init (tree init, tree decl, tree args,
13394 tsubst_flags_t complain, tree in_decl)
13396 if (!init)
13397 return NULL_TREE;
13399 init = tsubst_expr (init, args, complain, in_decl, false);
13401 if (!init)
13403 /* If we had an initializer but it
13404 instantiated to nothing,
13405 value-initialize the object. This will
13406 only occur when the initializer was a
13407 pack expansion where the parameter packs
13408 used in that expansion were of length
13409 zero. */
13410 init = build_value_init (TREE_TYPE (decl),
13411 complain);
13412 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13413 init = get_target_expr_sfinae (init, complain);
13416 return init;
13419 /* Like tsubst, but deals with expressions. This function just replaces
13420 template parms; to finish processing the resultant expression, use
13421 tsubst_copy_and_build or tsubst_expr. */
13423 static tree
13424 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13426 enum tree_code code;
13427 tree r;
13429 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
13430 return t;
13432 code = TREE_CODE (t);
13434 switch (code)
13436 case PARM_DECL:
13437 r = retrieve_local_specialization (t);
13439 if (r == NULL_TREE)
13441 /* We get here for a use of 'this' in an NSDMI. */
13442 if (DECL_NAME (t) == this_identifier
13443 && current_function_decl
13444 && DECL_CONSTRUCTOR_P (current_function_decl))
13445 return current_class_ptr;
13447 /* This can happen for a parameter name used later in a function
13448 declaration (such as in a late-specified return type). Just
13449 make a dummy decl, since it's only used for its type. */
13450 gcc_assert (cp_unevaluated_operand != 0);
13451 r = tsubst_decl (t, args, complain);
13452 /* Give it the template pattern as its context; its true context
13453 hasn't been instantiated yet and this is good enough for
13454 mangling. */
13455 DECL_CONTEXT (r) = DECL_CONTEXT (t);
13458 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
13459 r = ARGUMENT_PACK_SELECT_ARG (r);
13460 mark_used (r);
13461 return r;
13463 case CONST_DECL:
13465 tree enum_type;
13466 tree v;
13468 if (DECL_TEMPLATE_PARM_P (t))
13469 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
13470 /* There is no need to substitute into namespace-scope
13471 enumerators. */
13472 if (DECL_NAMESPACE_SCOPE_P (t))
13473 return t;
13474 /* If ARGS is NULL, then T is known to be non-dependent. */
13475 if (args == NULL_TREE)
13476 return scalar_constant_value (t);
13478 /* Unfortunately, we cannot just call lookup_name here.
13479 Consider:
13481 template <int I> int f() {
13482 enum E { a = I };
13483 struct S { void g() { E e = a; } };
13486 When we instantiate f<7>::S::g(), say, lookup_name is not
13487 clever enough to find f<7>::a. */
13488 enum_type
13489 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
13490 /*entering_scope=*/0);
13492 for (v = TYPE_VALUES (enum_type);
13493 v != NULL_TREE;
13494 v = TREE_CHAIN (v))
13495 if (TREE_PURPOSE (v) == DECL_NAME (t))
13496 return TREE_VALUE (v);
13498 /* We didn't find the name. That should never happen; if
13499 name-lookup found it during preliminary parsing, we
13500 should find it again here during instantiation. */
13501 gcc_unreachable ();
13503 return t;
13505 case FIELD_DECL:
13506 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13508 /* Check for a local specialization set up by
13509 tsubst_pack_expansion. */
13510 if (tree r = retrieve_local_specialization (t))
13512 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
13513 r = ARGUMENT_PACK_SELECT_ARG (r);
13514 return r;
13517 /* When retrieving a capture pack from a generic lambda, remove the
13518 lambda call op's own template argument list from ARGS. Only the
13519 template arguments active for the closure type should be used to
13520 retrieve the pack specialization. */
13521 if (LAMBDA_FUNCTION_P (current_function_decl)
13522 && (template_class_depth (DECL_CONTEXT (t))
13523 != TMPL_ARGS_DEPTH (args)))
13524 args = strip_innermost_template_args (args, 1);
13526 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
13527 tsubst_decl put in the hash table. */
13528 return retrieve_specialization (t, args, 0);
13531 if (DECL_CONTEXT (t))
13533 tree ctx;
13535 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
13536 /*entering_scope=*/1);
13537 if (ctx != DECL_CONTEXT (t))
13539 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
13540 if (!r)
13542 if (complain & tf_error)
13543 error ("using invalid field %qD", t);
13544 return error_mark_node;
13546 return r;
13550 return t;
13552 case VAR_DECL:
13553 case FUNCTION_DECL:
13554 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
13555 r = tsubst (t, args, complain, in_decl);
13556 else if (local_variable_p (t))
13558 r = retrieve_local_specialization (t);
13559 if (r == NULL_TREE)
13561 /* First try name lookup to find the instantiation. */
13562 r = lookup_name (DECL_NAME (t));
13563 if (r)
13565 /* Make sure that the one we found is the one we want. */
13566 tree ctx = tsubst (DECL_CONTEXT (t), args,
13567 complain, in_decl);
13568 if (ctx != DECL_CONTEXT (r))
13569 r = NULL_TREE;
13572 if (r)
13573 /* OK */;
13574 else
13576 /* This can happen for a variable used in a
13577 late-specified return type of a local lambda, or for a
13578 local static or constant. Building a new VAR_DECL
13579 should be OK in all those cases. */
13580 r = tsubst_decl (t, args, complain);
13581 if (decl_maybe_constant_var_p (r))
13583 /* We can't call cp_finish_decl, so handle the
13584 initializer by hand. */
13585 tree init = tsubst_init (DECL_INITIAL (t), r, args,
13586 complain, in_decl);
13587 if (!processing_template_decl)
13588 init = maybe_constant_init (init);
13589 if (processing_template_decl
13590 ? potential_constant_expression (init)
13591 : reduced_constant_expression_p (init))
13592 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
13593 = TREE_CONSTANT (r) = true;
13594 DECL_INITIAL (r) = init;
13596 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
13597 || decl_constant_var_p (r)
13598 || errorcount || sorrycount);
13599 if (!processing_template_decl)
13601 if (TREE_STATIC (r))
13602 rest_of_decl_compilation (r, toplevel_bindings_p (),
13603 at_eof);
13604 else if (decl_constant_var_p (r))
13605 /* A use of a local constant decays to its value.
13606 FIXME update for core DR 696. */
13607 r = scalar_constant_value (r);
13610 /* Remember this for subsequent uses. */
13611 if (local_specializations)
13612 register_local_specialization (r, t);
13615 else
13616 r = t;
13617 mark_used (r);
13618 return r;
13620 case NAMESPACE_DECL:
13621 return t;
13623 case OVERLOAD:
13624 /* An OVERLOAD will always be a non-dependent overload set; an
13625 overload set from function scope will just be represented with an
13626 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13627 gcc_assert (!uses_template_parms (t));
13628 return t;
13630 case BASELINK:
13631 return tsubst_baselink (t, current_nonlambda_class_type (),
13632 args, complain, in_decl);
13634 case TEMPLATE_DECL:
13635 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13636 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
13637 args, complain, in_decl);
13638 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
13639 return tsubst (t, args, complain, in_decl);
13640 else if (DECL_CLASS_SCOPE_P (t)
13641 && uses_template_parms (DECL_CONTEXT (t)))
13643 /* Template template argument like the following example need
13644 special treatment:
13646 template <template <class> class TT> struct C {};
13647 template <class T> struct D {
13648 template <class U> struct E {};
13649 C<E> c; // #1
13651 D<int> d; // #2
13653 We are processing the template argument `E' in #1 for
13654 the template instantiation #2. Originally, `E' is a
13655 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13656 have to substitute this with one having context `D<int>'. */
13658 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
13659 return lookup_field (context, DECL_NAME(t), 0, false);
13661 else
13662 /* Ordinary template template argument. */
13663 return t;
13665 case CAST_EXPR:
13666 case REINTERPRET_CAST_EXPR:
13667 case CONST_CAST_EXPR:
13668 case STATIC_CAST_EXPR:
13669 case DYNAMIC_CAST_EXPR:
13670 case IMPLICIT_CONV_EXPR:
13671 case CONVERT_EXPR:
13672 case NOP_EXPR:
13674 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13675 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13676 return build1 (code, type, op0);
13679 case SIZEOF_EXPR:
13680 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13683 tree expanded, op = TREE_OPERAND (t, 0);
13684 int len = 0;
13686 if (SIZEOF_EXPR_TYPE_P (t))
13687 op = TREE_TYPE (op);
13689 ++cp_unevaluated_operand;
13690 ++c_inhibit_evaluation_warnings;
13691 /* We only want to compute the number of arguments. */
13692 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
13693 --cp_unevaluated_operand;
13694 --c_inhibit_evaluation_warnings;
13696 if (TREE_CODE (expanded) == TREE_VEC)
13697 len = TREE_VEC_LENGTH (expanded);
13699 if (expanded == error_mark_node)
13700 return error_mark_node;
13701 else if (PACK_EXPANSION_P (expanded)
13702 || (TREE_CODE (expanded) == TREE_VEC
13703 && len > 0
13704 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
13706 if (TREE_CODE (expanded) == TREE_VEC)
13707 expanded = TREE_VEC_ELT (expanded, len - 1);
13709 if (TYPE_P (expanded))
13710 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
13711 complain & tf_error);
13712 else
13713 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
13714 complain & tf_error);
13716 else
13717 return build_int_cst (size_type_node, len);
13719 if (SIZEOF_EXPR_TYPE_P (t))
13721 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
13722 args, complain, in_decl);
13723 r = build1 (NOP_EXPR, r, error_mark_node);
13724 r = build1 (SIZEOF_EXPR,
13725 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
13726 SIZEOF_EXPR_TYPE_P (r) = 1;
13727 return r;
13729 /* Fall through */
13731 case INDIRECT_REF:
13732 case NEGATE_EXPR:
13733 case TRUTH_NOT_EXPR:
13734 case BIT_NOT_EXPR:
13735 case ADDR_EXPR:
13736 case UNARY_PLUS_EXPR: /* Unary + */
13737 case ALIGNOF_EXPR:
13738 case AT_ENCODE_EXPR:
13739 case ARROW_EXPR:
13740 case THROW_EXPR:
13741 case TYPEID_EXPR:
13742 case REALPART_EXPR:
13743 case IMAGPART_EXPR:
13744 case PAREN_EXPR:
13746 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13747 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13748 return build1 (code, type, op0);
13751 case COMPONENT_REF:
13753 tree object;
13754 tree name;
13756 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13757 name = TREE_OPERAND (t, 1);
13758 if (TREE_CODE (name) == BIT_NOT_EXPR)
13760 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13761 complain, in_decl);
13762 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
13764 else if (TREE_CODE (name) == SCOPE_REF
13765 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
13767 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
13768 complain, in_decl);
13769 name = TREE_OPERAND (name, 1);
13770 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13771 complain, in_decl);
13772 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
13773 name = build_qualified_name (/*type=*/NULL_TREE,
13774 base, name,
13775 /*template_p=*/false);
13777 else if (BASELINK_P (name))
13778 name = tsubst_baselink (name,
13779 non_reference (TREE_TYPE (object)),
13780 args, complain,
13781 in_decl);
13782 else
13783 name = tsubst_copy (name, args, complain, in_decl);
13784 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
13787 case PLUS_EXPR:
13788 case MINUS_EXPR:
13789 case MULT_EXPR:
13790 case TRUNC_DIV_EXPR:
13791 case CEIL_DIV_EXPR:
13792 case FLOOR_DIV_EXPR:
13793 case ROUND_DIV_EXPR:
13794 case EXACT_DIV_EXPR:
13795 case BIT_AND_EXPR:
13796 case BIT_IOR_EXPR:
13797 case BIT_XOR_EXPR:
13798 case TRUNC_MOD_EXPR:
13799 case FLOOR_MOD_EXPR:
13800 case TRUTH_ANDIF_EXPR:
13801 case TRUTH_ORIF_EXPR:
13802 case TRUTH_AND_EXPR:
13803 case TRUTH_OR_EXPR:
13804 case RSHIFT_EXPR:
13805 case LSHIFT_EXPR:
13806 case RROTATE_EXPR:
13807 case LROTATE_EXPR:
13808 case EQ_EXPR:
13809 case NE_EXPR:
13810 case MAX_EXPR:
13811 case MIN_EXPR:
13812 case LE_EXPR:
13813 case GE_EXPR:
13814 case LT_EXPR:
13815 case GT_EXPR:
13816 case COMPOUND_EXPR:
13817 case DOTSTAR_EXPR:
13818 case MEMBER_REF:
13819 case PREDECREMENT_EXPR:
13820 case PREINCREMENT_EXPR:
13821 case POSTDECREMENT_EXPR:
13822 case POSTINCREMENT_EXPR:
13824 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13825 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13826 return build_nt (code, op0, op1);
13829 case SCOPE_REF:
13831 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13832 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13833 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
13834 QUALIFIED_NAME_IS_TEMPLATE (t));
13837 case ARRAY_REF:
13839 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13840 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13841 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
13844 case CALL_EXPR:
13846 int n = VL_EXP_OPERAND_LENGTH (t);
13847 tree result = build_vl_exp (CALL_EXPR, n);
13848 int i;
13849 for (i = 0; i < n; i++)
13850 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
13851 complain, in_decl);
13852 return result;
13855 case COND_EXPR:
13856 case MODOP_EXPR:
13857 case PSEUDO_DTOR_EXPR:
13858 case VEC_PERM_EXPR:
13860 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13861 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13862 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
13863 r = build_nt (code, op0, op1, op2);
13864 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13865 return r;
13868 case NEW_EXPR:
13870 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13871 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13872 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
13873 r = build_nt (code, op0, op1, op2);
13874 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
13875 return r;
13878 case DELETE_EXPR:
13880 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13881 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13882 r = build_nt (code, op0, op1);
13883 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
13884 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
13885 return r;
13888 case TEMPLATE_ID_EXPR:
13890 /* Substituted template arguments */
13891 tree fn = TREE_OPERAND (t, 0);
13892 tree targs = TREE_OPERAND (t, 1);
13894 fn = tsubst_copy (fn, args, complain, in_decl);
13895 if (targs)
13896 targs = tsubst_template_args (targs, args, complain, in_decl);
13898 return lookup_template_function (fn, targs);
13901 case TREE_LIST:
13903 tree purpose, value, chain;
13905 if (t == void_list_node)
13906 return t;
13908 purpose = TREE_PURPOSE (t);
13909 if (purpose)
13910 purpose = tsubst_copy (purpose, args, complain, in_decl);
13911 value = TREE_VALUE (t);
13912 if (value)
13913 value = tsubst_copy (value, args, complain, in_decl);
13914 chain = TREE_CHAIN (t);
13915 if (chain && chain != void_type_node)
13916 chain = tsubst_copy (chain, args, complain, in_decl);
13917 if (purpose == TREE_PURPOSE (t)
13918 && value == TREE_VALUE (t)
13919 && chain == TREE_CHAIN (t))
13920 return t;
13921 return tree_cons (purpose, value, chain);
13924 case RECORD_TYPE:
13925 case UNION_TYPE:
13926 case ENUMERAL_TYPE:
13927 case INTEGER_TYPE:
13928 case REAL_TYPE:
13929 case BOOLEAN_TYPE:
13930 case TEMPLATE_TYPE_PARM:
13931 case TEMPLATE_TEMPLATE_PARM:
13932 case BOUND_TEMPLATE_TEMPLATE_PARM:
13933 case TEMPLATE_PARM_INDEX:
13934 case POINTER_TYPE:
13935 case REFERENCE_TYPE:
13936 case OFFSET_TYPE:
13937 case FUNCTION_TYPE:
13938 case METHOD_TYPE:
13939 case ARRAY_TYPE:
13940 case TYPENAME_TYPE:
13941 case UNBOUND_CLASS_TEMPLATE:
13942 case TYPEOF_TYPE:
13943 case DECLTYPE_TYPE:
13944 case TYPE_DECL:
13945 return tsubst (t, args, complain, in_decl);
13947 case USING_DECL:
13948 t = DECL_NAME (t);
13949 /* Fall through. */
13950 case IDENTIFIER_NODE:
13951 if (IDENTIFIER_TYPENAME_P (t))
13953 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13954 return mangle_conv_op_name_for_type (new_type);
13956 else
13957 return t;
13959 case CONSTRUCTOR:
13960 /* This is handled by tsubst_copy_and_build. */
13961 gcc_unreachable ();
13963 case VA_ARG_EXPR:
13965 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13966 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13967 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
13970 case CLEANUP_POINT_EXPR:
13971 /* We shouldn't have built any of these during initial template
13972 generation. Instead, they should be built during instantiation
13973 in response to the saved STMT_IS_FULL_EXPR_P setting. */
13974 gcc_unreachable ();
13976 case OFFSET_REF:
13978 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13979 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13980 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13981 r = build2 (code, type, op0, op1);
13982 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
13983 mark_used (TREE_OPERAND (r, 1));
13984 return r;
13987 case EXPR_PACK_EXPANSION:
13988 // Expansions of variadic constraints are viable, expanding
13989 // to a conunction of the expanded terms.
13990 if (TREE_TYPE (t) == boolean_type_node)
13991 return tsubst_pack_conjunction (t, args, complain, in_decl);
13993 error ("invalid use of pack expansion expression");
13994 return error_mark_node;
13996 case NONTYPE_ARGUMENT_PACK:
13997 error ("use %<...%> to expand argument pack");
13998 return error_mark_node;
14000 case VOID_CST:
14001 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
14002 return t;
14004 case INTEGER_CST:
14005 case REAL_CST:
14006 case STRING_CST:
14007 case COMPLEX_CST:
14009 /* Instantiate any typedefs in the type. */
14010 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14011 r = fold_convert (type, t);
14012 gcc_assert (TREE_CODE (r) == code);
14013 return r;
14016 case PTRMEM_CST:
14017 /* These can sometimes show up in a partial instantiation, but never
14018 involve template parms. */
14019 gcc_assert (!uses_template_parms (t));
14020 return t;
14022 default:
14023 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
14024 gcc_checking_assert (false);
14025 return t;
14029 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14031 static tree
14032 tsubst_omp_clauses (tree clauses, bool declare_simd,
14033 tree args, tsubst_flags_t complain, tree in_decl)
14035 tree new_clauses = NULL, nc, oc;
14037 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
14039 nc = copy_node (oc);
14040 OMP_CLAUSE_CHAIN (nc) = new_clauses;
14041 new_clauses = nc;
14043 switch (OMP_CLAUSE_CODE (nc))
14045 case OMP_CLAUSE_LASTPRIVATE:
14046 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
14048 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
14049 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
14050 in_decl, /*integral_constant_expression_p=*/false);
14051 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
14052 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
14054 /* FALLTHRU */
14055 case OMP_CLAUSE_PRIVATE:
14056 case OMP_CLAUSE_SHARED:
14057 case OMP_CLAUSE_FIRSTPRIVATE:
14058 case OMP_CLAUSE_COPYIN:
14059 case OMP_CLAUSE_COPYPRIVATE:
14060 case OMP_CLAUSE_IF:
14061 case OMP_CLAUSE_NUM_THREADS:
14062 case OMP_CLAUSE_SCHEDULE:
14063 case OMP_CLAUSE_COLLAPSE:
14064 case OMP_CLAUSE_FINAL:
14065 case OMP_CLAUSE_DEPEND:
14066 case OMP_CLAUSE_FROM:
14067 case OMP_CLAUSE_TO:
14068 case OMP_CLAUSE_UNIFORM:
14069 case OMP_CLAUSE_MAP:
14070 case OMP_CLAUSE_DEVICE:
14071 case OMP_CLAUSE_DIST_SCHEDULE:
14072 case OMP_CLAUSE_NUM_TEAMS:
14073 case OMP_CLAUSE_THREAD_LIMIT:
14074 case OMP_CLAUSE_SAFELEN:
14075 case OMP_CLAUSE_SIMDLEN:
14076 OMP_CLAUSE_OPERAND (nc, 0)
14077 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
14078 in_decl, /*integral_constant_expression_p=*/false);
14079 break;
14080 case OMP_CLAUSE_REDUCTION:
14081 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
14083 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
14084 if (TREE_CODE (placeholder) == SCOPE_REF)
14086 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
14087 complain, in_decl);
14088 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
14089 = build_qualified_name (NULL_TREE, scope,
14090 TREE_OPERAND (placeholder, 1),
14091 false);
14093 else
14094 gcc_assert (identifier_p (placeholder));
14096 OMP_CLAUSE_OPERAND (nc, 0)
14097 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
14098 in_decl, /*integral_constant_expression_p=*/false);
14099 break;
14100 case OMP_CLAUSE_LINEAR:
14101 case OMP_CLAUSE_ALIGNED:
14102 OMP_CLAUSE_OPERAND (nc, 0)
14103 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
14104 in_decl, /*integral_constant_expression_p=*/false);
14105 OMP_CLAUSE_OPERAND (nc, 1)
14106 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
14107 in_decl, /*integral_constant_expression_p=*/false);
14108 break;
14110 case OMP_CLAUSE_NOWAIT:
14111 case OMP_CLAUSE_ORDERED:
14112 case OMP_CLAUSE_DEFAULT:
14113 case OMP_CLAUSE_UNTIED:
14114 case OMP_CLAUSE_MERGEABLE:
14115 case OMP_CLAUSE_INBRANCH:
14116 case OMP_CLAUSE_NOTINBRANCH:
14117 case OMP_CLAUSE_PROC_BIND:
14118 case OMP_CLAUSE_FOR:
14119 case OMP_CLAUSE_PARALLEL:
14120 case OMP_CLAUSE_SECTIONS:
14121 case OMP_CLAUSE_TASKGROUP:
14122 break;
14123 default:
14124 gcc_unreachable ();
14128 new_clauses = nreverse (new_clauses);
14129 if (!declare_simd)
14130 new_clauses = finish_omp_clauses (new_clauses);
14131 return new_clauses;
14134 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
14136 static tree
14137 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
14138 tree in_decl)
14140 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
14142 tree purpose, value, chain;
14144 if (t == NULL)
14145 return t;
14147 if (TREE_CODE (t) != TREE_LIST)
14148 return tsubst_copy_and_build (t, args, complain, in_decl,
14149 /*function_p=*/false,
14150 /*integral_constant_expression_p=*/false);
14152 if (t == void_list_node)
14153 return t;
14155 purpose = TREE_PURPOSE (t);
14156 if (purpose)
14157 purpose = RECUR (purpose);
14158 value = TREE_VALUE (t);
14159 if (value)
14161 if (TREE_CODE (value) != LABEL_DECL)
14162 value = RECUR (value);
14163 else
14165 value = lookup_label (DECL_NAME (value));
14166 gcc_assert (TREE_CODE (value) == LABEL_DECL);
14167 TREE_USED (value) = 1;
14170 chain = TREE_CHAIN (t);
14171 if (chain && chain != void_type_node)
14172 chain = RECUR (chain);
14173 return tree_cons (purpose, value, chain);
14174 #undef RECUR
14177 /* Substitute one OMP_FOR iterator. */
14179 static void
14180 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
14181 tree condv, tree incrv, tree *clauses,
14182 tree args, tsubst_flags_t complain, tree in_decl,
14183 bool integral_constant_expression_p)
14185 #define RECUR(NODE) \
14186 tsubst_expr ((NODE), args, complain, in_decl, \
14187 integral_constant_expression_p)
14188 tree decl, init, cond, incr;
14190 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
14191 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
14192 decl = TREE_OPERAND (init, 0);
14193 init = TREE_OPERAND (init, 1);
14194 tree decl_expr = NULL_TREE;
14195 if (init && TREE_CODE (init) == DECL_EXPR)
14197 /* We need to jump through some hoops to handle declarations in the
14198 for-init-statement, since we might need to handle auto deduction,
14199 but we need to keep control of initialization. */
14200 decl_expr = init;
14201 init = DECL_INITIAL (DECL_EXPR_DECL (init));
14202 decl = tsubst_decl (decl, args, complain);
14204 else
14205 decl = RECUR (decl);
14206 init = RECUR (init);
14208 tree auto_node = type_uses_auto (TREE_TYPE (decl));
14209 if (auto_node && init)
14210 TREE_TYPE (decl)
14211 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
14213 gcc_assert (!type_dependent_expression_p (decl));
14215 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14217 if (decl_expr)
14219 /* Declare the variable, but don't let that initialize it. */
14220 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
14221 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
14222 RECUR (decl_expr);
14223 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
14226 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
14227 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
14228 if (TREE_CODE (incr) == MODIFY_EXPR)
14230 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14231 tree rhs = RECUR (TREE_OPERAND (incr, 1));
14232 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
14233 NOP_EXPR, rhs, complain);
14235 else
14236 incr = RECUR (incr);
14237 TREE_VEC_ELT (declv, i) = decl;
14238 TREE_VEC_ELT (initv, i) = init;
14239 TREE_VEC_ELT (condv, i) = cond;
14240 TREE_VEC_ELT (incrv, i) = incr;
14241 return;
14244 if (decl_expr)
14246 /* Declare and initialize the variable. */
14247 RECUR (decl_expr);
14248 init = NULL_TREE;
14250 else if (init)
14252 tree c;
14253 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
14255 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14256 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
14257 && OMP_CLAUSE_DECL (c) == decl)
14258 break;
14259 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
14260 && OMP_CLAUSE_DECL (c) == decl)
14261 error ("iteration variable %qD should not be firstprivate", decl);
14262 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14263 && OMP_CLAUSE_DECL (c) == decl)
14264 error ("iteration variable %qD should not be reduction", decl);
14266 if (c == NULL)
14268 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
14269 OMP_CLAUSE_DECL (c) = decl;
14270 c = finish_omp_clauses (c);
14271 if (c)
14273 OMP_CLAUSE_CHAIN (c) = *clauses;
14274 *clauses = c;
14278 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
14279 if (COMPARISON_CLASS_P (cond))
14281 tree op0 = RECUR (TREE_OPERAND (cond, 0));
14282 tree op1 = RECUR (TREE_OPERAND (cond, 1));
14283 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
14285 else
14286 cond = RECUR (cond);
14287 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
14288 switch (TREE_CODE (incr))
14290 case PREINCREMENT_EXPR:
14291 case PREDECREMENT_EXPR:
14292 case POSTINCREMENT_EXPR:
14293 case POSTDECREMENT_EXPR:
14294 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
14295 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
14296 break;
14297 case MODIFY_EXPR:
14298 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
14299 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
14301 tree rhs = TREE_OPERAND (incr, 1);
14302 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14303 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
14304 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
14305 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14306 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
14307 rhs0, rhs1));
14309 else
14310 incr = RECUR (incr);
14311 break;
14312 case MODOP_EXPR:
14313 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
14314 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
14316 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14317 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14318 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
14319 TREE_TYPE (decl), lhs,
14320 RECUR (TREE_OPERAND (incr, 2))));
14322 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
14323 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
14324 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
14326 tree rhs = TREE_OPERAND (incr, 2);
14327 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14328 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
14329 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
14330 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14331 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
14332 rhs0, rhs1));
14334 else
14335 incr = RECUR (incr);
14336 break;
14337 default:
14338 incr = RECUR (incr);
14339 break;
14342 TREE_VEC_ELT (declv, i) = decl;
14343 TREE_VEC_ELT (initv, i) = init;
14344 TREE_VEC_ELT (condv, i) = cond;
14345 TREE_VEC_ELT (incrv, i) = incr;
14346 #undef RECUR
14349 /* Like tsubst_copy for expressions, etc. but also does semantic
14350 processing. */
14352 tree
14353 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
14354 bool integral_constant_expression_p)
14356 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14357 #define RECUR(NODE) \
14358 tsubst_expr ((NODE), args, complain, in_decl, \
14359 integral_constant_expression_p)
14361 tree stmt, tmp;
14362 tree r;
14363 location_t loc;
14365 if (t == NULL_TREE || t == error_mark_node)
14366 return t;
14368 loc = input_location;
14369 if (EXPR_HAS_LOCATION (t))
14370 input_location = EXPR_LOCATION (t);
14371 if (STATEMENT_CODE_P (TREE_CODE (t)))
14372 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
14374 switch (TREE_CODE (t))
14376 case STATEMENT_LIST:
14378 tree_stmt_iterator i;
14379 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
14380 RECUR (tsi_stmt (i));
14381 break;
14384 case CTOR_INITIALIZER:
14385 finish_mem_initializers (tsubst_initializer_list
14386 (TREE_OPERAND (t, 0), args));
14387 break;
14389 case RETURN_EXPR:
14390 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
14391 break;
14393 case EXPR_STMT:
14394 tmp = RECUR (EXPR_STMT_EXPR (t));
14395 if (EXPR_STMT_STMT_EXPR_RESULT (t))
14396 finish_stmt_expr_expr (tmp, cur_stmt_expr);
14397 else
14398 finish_expr_stmt (tmp);
14399 break;
14401 case USING_STMT:
14402 do_using_directive (USING_STMT_NAMESPACE (t));
14403 break;
14405 case DECL_EXPR:
14407 tree decl, pattern_decl;
14408 tree init;
14410 pattern_decl = decl = DECL_EXPR_DECL (t);
14411 if (TREE_CODE (decl) == LABEL_DECL)
14412 finish_label_decl (DECL_NAME (decl));
14413 else if (TREE_CODE (decl) == USING_DECL)
14415 tree scope = USING_DECL_SCOPE (decl);
14416 tree name = DECL_NAME (decl);
14417 tree decl;
14419 scope = tsubst (scope, args, complain, in_decl);
14420 decl = lookup_qualified_name (scope, name,
14421 /*is_type_p=*/false,
14422 /*complain=*/false);
14423 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
14424 qualified_name_lookup_error (scope, name, decl, input_location);
14425 else
14426 do_local_using_decl (decl, scope, name);
14428 else if (DECL_PACK_P (decl))
14430 /* Don't build up decls for a variadic capture proxy, we'll
14431 instantiate the elements directly as needed. */
14432 break;
14434 else
14436 init = DECL_INITIAL (decl);
14437 decl = tsubst (decl, args, complain, in_decl);
14438 if (decl != error_mark_node)
14440 /* By marking the declaration as instantiated, we avoid
14441 trying to instantiate it. Since instantiate_decl can't
14442 handle local variables, and since we've already done
14443 all that needs to be done, that's the right thing to
14444 do. */
14445 if (VAR_P (decl))
14446 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
14447 if (VAR_P (decl)
14448 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
14449 /* Anonymous aggregates are a special case. */
14450 finish_anon_union (decl);
14451 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
14453 DECL_CONTEXT (decl) = current_function_decl;
14454 if (DECL_NAME (decl) == this_identifier)
14456 tree lam = DECL_CONTEXT (current_function_decl);
14457 lam = CLASSTYPE_LAMBDA_EXPR (lam);
14458 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
14460 insert_capture_proxy (decl);
14462 else if (DECL_IMPLICIT_TYPEDEF_P (t))
14463 /* We already did a pushtag. */;
14464 else if (TREE_CODE (decl) == FUNCTION_DECL
14465 && DECL_OMP_DECLARE_REDUCTION_P (decl)
14466 && DECL_FUNCTION_SCOPE_P (pattern_decl))
14468 DECL_CONTEXT (decl) = NULL_TREE;
14469 pushdecl (decl);
14470 DECL_CONTEXT (decl) = current_function_decl;
14471 cp_check_omp_declare_reduction (decl);
14473 else
14475 int const_init = false;
14476 maybe_push_decl (decl);
14477 if (VAR_P (decl)
14478 && DECL_PRETTY_FUNCTION_P (decl))
14480 /* For __PRETTY_FUNCTION__ we have to adjust the
14481 initializer. */
14482 const char *const name
14483 = cxx_printable_name (current_function_decl, 2);
14484 init = cp_fname_init (name, &TREE_TYPE (decl));
14486 else
14487 init = tsubst_init (init, decl, args, complain, in_decl);
14489 if (VAR_P (decl))
14490 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
14491 (pattern_decl));
14492 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
14497 break;
14500 case FOR_STMT:
14501 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
14502 RECUR (FOR_INIT_STMT (t));
14503 finish_for_init_stmt (stmt);
14504 tmp = RECUR (FOR_COND (t));
14505 finish_for_cond (tmp, stmt, false);
14506 tmp = RECUR (FOR_EXPR (t));
14507 finish_for_expr (tmp, stmt);
14508 RECUR (FOR_BODY (t));
14509 finish_for_stmt (stmt);
14510 break;
14512 case RANGE_FOR_STMT:
14514 tree decl, expr;
14515 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
14516 decl = RANGE_FOR_DECL (t);
14517 decl = tsubst (decl, args, complain, in_decl);
14518 maybe_push_decl (decl);
14519 expr = RECUR (RANGE_FOR_EXPR (t));
14520 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
14521 RECUR (RANGE_FOR_BODY (t));
14522 finish_for_stmt (stmt);
14524 break;
14526 case WHILE_STMT:
14527 stmt = begin_while_stmt ();
14528 tmp = RECUR (WHILE_COND (t));
14529 finish_while_stmt_cond (tmp, stmt, false);
14530 RECUR (WHILE_BODY (t));
14531 finish_while_stmt (stmt);
14532 break;
14534 case DO_STMT:
14535 stmt = begin_do_stmt ();
14536 RECUR (DO_BODY (t));
14537 finish_do_body (stmt);
14538 tmp = RECUR (DO_COND (t));
14539 finish_do_stmt (tmp, stmt, false);
14540 break;
14542 case IF_STMT:
14543 stmt = begin_if_stmt ();
14544 tmp = RECUR (IF_COND (t));
14545 finish_if_stmt_cond (tmp, stmt);
14546 RECUR (THEN_CLAUSE (t));
14547 finish_then_clause (stmt);
14549 if (ELSE_CLAUSE (t))
14551 begin_else_clause (stmt);
14552 RECUR (ELSE_CLAUSE (t));
14553 finish_else_clause (stmt);
14556 finish_if_stmt (stmt);
14557 break;
14559 case BIND_EXPR:
14560 if (BIND_EXPR_BODY_BLOCK (t))
14561 stmt = begin_function_body ();
14562 else
14563 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
14564 ? BCS_TRY_BLOCK : 0);
14566 RECUR (BIND_EXPR_BODY (t));
14568 if (BIND_EXPR_BODY_BLOCK (t))
14569 finish_function_body (stmt);
14570 else
14571 finish_compound_stmt (stmt);
14572 break;
14574 case BREAK_STMT:
14575 finish_break_stmt ();
14576 break;
14578 case CONTINUE_STMT:
14579 finish_continue_stmt ();
14580 break;
14582 case SWITCH_STMT:
14583 stmt = begin_switch_stmt ();
14584 tmp = RECUR (SWITCH_STMT_COND (t));
14585 finish_switch_cond (tmp, stmt);
14586 RECUR (SWITCH_STMT_BODY (t));
14587 finish_switch_stmt (stmt);
14588 break;
14590 case CASE_LABEL_EXPR:
14592 tree low = RECUR (CASE_LOW (t));
14593 tree high = RECUR (CASE_HIGH (t));
14594 finish_case_label (EXPR_LOCATION (t), low, high);
14596 break;
14598 case LABEL_EXPR:
14600 tree decl = LABEL_EXPR_LABEL (t);
14601 tree label;
14603 label = finish_label_stmt (DECL_NAME (decl));
14604 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
14605 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
14607 break;
14609 case GOTO_EXPR:
14610 tmp = GOTO_DESTINATION (t);
14611 if (TREE_CODE (tmp) != LABEL_DECL)
14612 /* Computed goto's must be tsubst'd into. On the other hand,
14613 non-computed gotos must not be; the identifier in question
14614 will have no binding. */
14615 tmp = RECUR (tmp);
14616 else
14617 tmp = DECL_NAME (tmp);
14618 finish_goto_stmt (tmp);
14619 break;
14621 case ASM_EXPR:
14623 tree string = RECUR (ASM_STRING (t));
14624 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
14625 complain, in_decl);
14626 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
14627 complain, in_decl);
14628 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
14629 complain, in_decl);
14630 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
14631 complain, in_decl);
14632 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
14633 clobbers, labels);
14634 tree asm_expr = tmp;
14635 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
14636 asm_expr = TREE_OPERAND (asm_expr, 0);
14637 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
14639 break;
14641 case TRY_BLOCK:
14642 if (CLEANUP_P (t))
14644 stmt = begin_try_block ();
14645 RECUR (TRY_STMTS (t));
14646 finish_cleanup_try_block (stmt);
14647 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
14649 else
14651 tree compound_stmt = NULL_TREE;
14653 if (FN_TRY_BLOCK_P (t))
14654 stmt = begin_function_try_block (&compound_stmt);
14655 else
14656 stmt = begin_try_block ();
14658 RECUR (TRY_STMTS (t));
14660 if (FN_TRY_BLOCK_P (t))
14661 finish_function_try_block (stmt);
14662 else
14663 finish_try_block (stmt);
14665 RECUR (TRY_HANDLERS (t));
14666 if (FN_TRY_BLOCK_P (t))
14667 finish_function_handler_sequence (stmt, compound_stmt);
14668 else
14669 finish_handler_sequence (stmt);
14671 break;
14673 case HANDLER:
14675 tree decl = HANDLER_PARMS (t);
14677 if (decl)
14679 decl = tsubst (decl, args, complain, in_decl);
14680 /* Prevent instantiate_decl from trying to instantiate
14681 this variable. We've already done all that needs to be
14682 done. */
14683 if (decl != error_mark_node)
14684 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
14686 stmt = begin_handler ();
14687 finish_handler_parms (decl, stmt);
14688 RECUR (HANDLER_BODY (t));
14689 finish_handler (stmt);
14691 break;
14693 case TAG_DEFN:
14694 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
14695 if (CLASS_TYPE_P (tmp))
14697 /* Local classes are not independent templates; they are
14698 instantiated along with their containing function. And this
14699 way we don't have to deal with pushing out of one local class
14700 to instantiate a member of another local class. */
14701 tree fn;
14702 /* Closures are handled by the LAMBDA_EXPR. */
14703 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
14704 complete_type (tmp);
14705 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
14706 if (!DECL_ARTIFICIAL (fn))
14707 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
14709 break;
14711 case STATIC_ASSERT:
14713 tree condition;
14715 ++c_inhibit_evaluation_warnings;
14716 condition =
14717 tsubst_expr (STATIC_ASSERT_CONDITION (t),
14718 args,
14719 complain, in_decl,
14720 /*integral_constant_expression_p=*/true);
14721 --c_inhibit_evaluation_warnings;
14723 finish_static_assert (condition,
14724 STATIC_ASSERT_MESSAGE (t),
14725 STATIC_ASSERT_SOURCE_LOCATION (t),
14726 /*member_p=*/false);
14728 break;
14730 case OMP_PARALLEL:
14731 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
14732 args, complain, in_decl);
14733 stmt = begin_omp_parallel ();
14734 RECUR (OMP_PARALLEL_BODY (t));
14735 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
14736 = OMP_PARALLEL_COMBINED (t);
14737 break;
14739 case OMP_TASK:
14740 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
14741 args, complain, in_decl);
14742 stmt = begin_omp_task ();
14743 RECUR (OMP_TASK_BODY (t));
14744 finish_omp_task (tmp, stmt);
14745 break;
14747 case OMP_FOR:
14748 case OMP_SIMD:
14749 case CILK_SIMD:
14750 case CILK_FOR:
14751 case OMP_DISTRIBUTE:
14753 tree clauses, body, pre_body;
14754 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
14755 tree incrv = NULL_TREE;
14756 int i;
14758 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
14759 args, complain, in_decl);
14760 if (OMP_FOR_INIT (t) != NULL_TREE)
14762 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14763 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14764 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14765 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14768 stmt = begin_omp_structured_block ();
14770 pre_body = push_stmt_list ();
14771 RECUR (OMP_FOR_PRE_BODY (t));
14772 pre_body = pop_stmt_list (pre_body);
14774 if (OMP_FOR_INIT (t) != NULL_TREE)
14775 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
14776 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
14777 &clauses, args, complain, in_decl,
14778 integral_constant_expression_p);
14780 body = push_stmt_list ();
14781 RECUR (OMP_FOR_BODY (t));
14782 body = pop_stmt_list (body);
14784 if (OMP_FOR_INIT (t) != NULL_TREE)
14785 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
14786 condv, incrv, body, pre_body, clauses);
14787 else
14789 t = make_node (TREE_CODE (t));
14790 TREE_TYPE (t) = void_type_node;
14791 OMP_FOR_BODY (t) = body;
14792 OMP_FOR_PRE_BODY (t) = pre_body;
14793 OMP_FOR_CLAUSES (t) = clauses;
14794 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
14795 add_stmt (t);
14798 add_stmt (finish_omp_structured_block (stmt));
14800 break;
14802 case OMP_SECTIONS:
14803 case OMP_SINGLE:
14804 case OMP_TEAMS:
14805 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
14806 args, complain, in_decl);
14807 stmt = push_stmt_list ();
14808 RECUR (OMP_BODY (t));
14809 stmt = pop_stmt_list (stmt);
14811 t = copy_node (t);
14812 OMP_BODY (t) = stmt;
14813 OMP_CLAUSES (t) = tmp;
14814 add_stmt (t);
14815 break;
14817 case OMP_TARGET_DATA:
14818 case OMP_TARGET:
14819 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
14820 args, complain, in_decl);
14821 keep_next_level (true);
14822 stmt = begin_omp_structured_block ();
14824 RECUR (OMP_BODY (t));
14825 stmt = finish_omp_structured_block (stmt);
14827 t = copy_node (t);
14828 OMP_BODY (t) = stmt;
14829 OMP_CLAUSES (t) = tmp;
14830 add_stmt (t);
14831 break;
14833 case OMP_TARGET_UPDATE:
14834 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
14835 args, complain, in_decl);
14836 t = copy_node (t);
14837 OMP_CLAUSES (t) = tmp;
14838 add_stmt (t);
14839 break;
14841 case OMP_SECTION:
14842 case OMP_CRITICAL:
14843 case OMP_MASTER:
14844 case OMP_TASKGROUP:
14845 case OMP_ORDERED:
14846 stmt = push_stmt_list ();
14847 RECUR (OMP_BODY (t));
14848 stmt = pop_stmt_list (stmt);
14850 t = copy_node (t);
14851 OMP_BODY (t) = stmt;
14852 add_stmt (t);
14853 break;
14855 case OMP_ATOMIC:
14856 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
14857 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
14859 tree op1 = TREE_OPERAND (t, 1);
14860 tree rhs1 = NULL_TREE;
14861 tree lhs, rhs;
14862 if (TREE_CODE (op1) == COMPOUND_EXPR)
14864 rhs1 = RECUR (TREE_OPERAND (op1, 0));
14865 op1 = TREE_OPERAND (op1, 1);
14867 lhs = RECUR (TREE_OPERAND (op1, 0));
14868 rhs = RECUR (TREE_OPERAND (op1, 1));
14869 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
14870 NULL_TREE, NULL_TREE, rhs1,
14871 OMP_ATOMIC_SEQ_CST (t));
14873 else
14875 tree op1 = TREE_OPERAND (t, 1);
14876 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
14877 tree rhs1 = NULL_TREE;
14878 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
14879 enum tree_code opcode = NOP_EXPR;
14880 if (code == OMP_ATOMIC_READ)
14882 v = RECUR (TREE_OPERAND (op1, 0));
14883 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14885 else if (code == OMP_ATOMIC_CAPTURE_OLD
14886 || code == OMP_ATOMIC_CAPTURE_NEW)
14888 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
14889 v = RECUR (TREE_OPERAND (op1, 0));
14890 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14891 if (TREE_CODE (op11) == COMPOUND_EXPR)
14893 rhs1 = RECUR (TREE_OPERAND (op11, 0));
14894 op11 = TREE_OPERAND (op11, 1);
14896 lhs = RECUR (TREE_OPERAND (op11, 0));
14897 rhs = RECUR (TREE_OPERAND (op11, 1));
14898 opcode = TREE_CODE (op11);
14899 if (opcode == MODIFY_EXPR)
14900 opcode = NOP_EXPR;
14902 else
14904 code = OMP_ATOMIC;
14905 lhs = RECUR (TREE_OPERAND (op1, 0));
14906 rhs = RECUR (TREE_OPERAND (op1, 1));
14908 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
14909 OMP_ATOMIC_SEQ_CST (t));
14911 break;
14913 case TRANSACTION_EXPR:
14915 int flags = 0;
14916 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
14917 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
14919 if (TRANSACTION_EXPR_IS_STMT (t))
14921 tree body = TRANSACTION_EXPR_BODY (t);
14922 tree noex = NULL_TREE;
14923 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
14925 noex = MUST_NOT_THROW_COND (body);
14926 if (noex == NULL_TREE)
14927 noex = boolean_true_node;
14928 body = TREE_OPERAND (body, 0);
14930 stmt = begin_transaction_stmt (input_location, NULL, flags);
14931 RECUR (body);
14932 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
14934 else
14936 stmt = build_transaction_expr (EXPR_LOCATION (t),
14937 RECUR (TRANSACTION_EXPR_BODY (t)),
14938 flags, NULL_TREE);
14939 RETURN (stmt);
14942 break;
14944 case MUST_NOT_THROW_EXPR:
14946 tree op0 = RECUR (TREE_OPERAND (t, 0));
14947 tree cond = RECUR (MUST_NOT_THROW_COND (t));
14948 RETURN (build_must_not_throw_expr (op0, cond));
14951 case EXPR_PACK_EXPANSION:
14952 // Expansions of variadic constraints are viable, expanding
14953 // to a conunction of the expanded terms.
14954 if (TREE_TYPE (t) == boolean_type_node)
14955 return tsubst_pack_conjunction (t, args, complain, in_decl);
14957 error ("invalid use of pack expansion expression");
14958 RETURN (error_mark_node);
14960 case NONTYPE_ARGUMENT_PACK:
14961 error ("use %<...%> to expand argument pack");
14962 RETURN (error_mark_node);
14964 case CILK_SPAWN_STMT:
14965 cfun->calls_cilk_spawn = 1;
14966 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
14968 case CILK_SYNC_STMT:
14969 RETURN (build_cilk_sync ());
14971 case COMPOUND_EXPR:
14972 tmp = RECUR (TREE_OPERAND (t, 0));
14973 if (tmp == NULL_TREE)
14974 /* If the first operand was a statement, we're done with it. */
14975 RETURN (RECUR (TREE_OPERAND (t, 1)));
14976 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
14977 RECUR (TREE_OPERAND (t, 1)),
14978 complain));
14980 case ANNOTATE_EXPR:
14981 tmp = RECUR (TREE_OPERAND (t, 0));
14982 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
14983 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
14985 default:
14986 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
14988 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
14989 /*function_p=*/false,
14990 integral_constant_expression_p));
14993 RETURN (NULL_TREE);
14994 out:
14995 input_location = loc;
14996 return r;
14997 #undef RECUR
14998 #undef RETURN
15001 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
15002 function. For description of the body see comment above
15003 cp_parser_omp_declare_reduction_exprs. */
15005 static void
15006 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15008 if (t == NULL_TREE || t == error_mark_node)
15009 return;
15011 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
15013 tree_stmt_iterator tsi;
15014 int i;
15015 tree stmts[7];
15016 memset (stmts, 0, sizeof stmts);
15017 for (i = 0, tsi = tsi_start (t);
15018 i < 7 && !tsi_end_p (tsi);
15019 i++, tsi_next (&tsi))
15020 stmts[i] = tsi_stmt (tsi);
15021 gcc_assert (tsi_end_p (tsi));
15023 if (i >= 3)
15025 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
15026 && TREE_CODE (stmts[1]) == DECL_EXPR);
15027 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
15028 args, complain, in_decl);
15029 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
15030 args, complain, in_decl);
15031 DECL_CONTEXT (omp_out) = current_function_decl;
15032 DECL_CONTEXT (omp_in) = current_function_decl;
15033 keep_next_level (true);
15034 tree block = begin_omp_structured_block ();
15035 tsubst_expr (stmts[2], args, complain, in_decl, false);
15036 block = finish_omp_structured_block (block);
15037 block = maybe_cleanup_point_expr_void (block);
15038 add_decl_expr (omp_out);
15039 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
15040 TREE_NO_WARNING (omp_out) = 1;
15041 add_decl_expr (omp_in);
15042 finish_expr_stmt (block);
15044 if (i >= 6)
15046 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
15047 && TREE_CODE (stmts[4]) == DECL_EXPR);
15048 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
15049 args, complain, in_decl);
15050 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
15051 args, complain, in_decl);
15052 DECL_CONTEXT (omp_priv) = current_function_decl;
15053 DECL_CONTEXT (omp_orig) = current_function_decl;
15054 keep_next_level (true);
15055 tree block = begin_omp_structured_block ();
15056 tsubst_expr (stmts[5], args, complain, in_decl, false);
15057 block = finish_omp_structured_block (block);
15058 block = maybe_cleanup_point_expr_void (block);
15059 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
15060 add_decl_expr (omp_priv);
15061 add_decl_expr (omp_orig);
15062 finish_expr_stmt (block);
15063 if (i == 7)
15064 add_decl_expr (omp_orig);
15068 /* T is a postfix-expression that is not being used in a function
15069 call. Return the substituted version of T. */
15071 static tree
15072 tsubst_non_call_postfix_expression (tree t, tree args,
15073 tsubst_flags_t complain,
15074 tree in_decl)
15076 if (TREE_CODE (t) == SCOPE_REF)
15077 t = tsubst_qualified_id (t, args, complain, in_decl,
15078 /*done=*/false, /*address_p=*/false);
15079 else
15080 t = tsubst_copy_and_build (t, args, complain, in_decl,
15081 /*function_p=*/false,
15082 /*integral_constant_expression_p=*/false);
15084 return t;
15087 /* Like tsubst but deals with expressions and performs semantic
15088 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
15090 tree
15091 tsubst_copy_and_build (tree t,
15092 tree args,
15093 tsubst_flags_t complain,
15094 tree in_decl,
15095 bool function_p,
15096 bool integral_constant_expression_p)
15098 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
15099 #define RECUR(NODE) \
15100 tsubst_copy_and_build (NODE, args, complain, in_decl, \
15101 /*function_p=*/false, \
15102 integral_constant_expression_p)
15104 tree retval, op1;
15105 location_t loc;
15107 if (t == NULL_TREE || t == error_mark_node)
15108 return t;
15110 loc = input_location;
15111 if (EXPR_HAS_LOCATION (t))
15112 input_location = EXPR_LOCATION (t);
15114 /* N3276 decltype magic only applies to calls at the top level or on the
15115 right side of a comma. */
15116 tsubst_flags_t decltype_flag = (complain & tf_decltype);
15117 complain &= ~tf_decltype;
15119 switch (TREE_CODE (t))
15121 case USING_DECL:
15122 t = DECL_NAME (t);
15123 /* Fall through. */
15124 case IDENTIFIER_NODE:
15126 tree decl;
15127 cp_id_kind idk;
15128 bool non_integral_constant_expression_p;
15129 const char *error_msg;
15131 if (IDENTIFIER_TYPENAME_P (t))
15133 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15134 t = mangle_conv_op_name_for_type (new_type);
15137 /* Look up the name. */
15138 decl = lookup_name (t);
15140 /* By convention, expressions use ERROR_MARK_NODE to indicate
15141 failure, not NULL_TREE. */
15142 if (decl == NULL_TREE)
15143 decl = error_mark_node;
15145 decl = finish_id_expression (t, decl, NULL_TREE,
15146 &idk,
15147 integral_constant_expression_p,
15148 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
15149 &non_integral_constant_expression_p,
15150 /*template_p=*/false,
15151 /*done=*/true,
15152 /*address_p=*/false,
15153 /*template_arg_p=*/false,
15154 &error_msg,
15155 input_location);
15156 if (error_msg)
15157 error (error_msg);
15158 if (!function_p && identifier_p (decl))
15160 if (complain & tf_error)
15161 unqualified_name_lookup_error (decl);
15162 decl = error_mark_node;
15164 RETURN (decl);
15167 case TEMPLATE_ID_EXPR:
15169 tree object;
15170 tree templ = RECUR (TREE_OPERAND (t, 0));
15171 tree targs = TREE_OPERAND (t, 1);
15173 if (targs)
15174 targs = tsubst_template_args (targs, args, complain, in_decl);
15176 if (TREE_CODE (templ) == COMPONENT_REF)
15178 object = TREE_OPERAND (templ, 0);
15179 templ = TREE_OPERAND (templ, 1);
15181 else
15182 object = NULL_TREE;
15183 if (variable_template_p (templ))
15185 templ = lookup_template_variable (templ, targs);
15186 RETURN (finish_template_variable (templ));
15188 else
15189 templ = lookup_template_function (templ, targs);
15191 if (object)
15192 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
15193 object, templ, NULL_TREE));
15194 else
15195 RETURN (baselink_for_fns (templ));
15198 case INDIRECT_REF:
15200 tree r = RECUR (TREE_OPERAND (t, 0));
15202 if (REFERENCE_REF_P (t))
15204 /* A type conversion to reference type will be enclosed in
15205 such an indirect ref, but the substitution of the cast
15206 will have also added such an indirect ref. */
15207 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
15208 r = convert_from_reference (r);
15210 else
15211 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
15212 complain|decltype_flag);
15213 RETURN (r);
15216 case NOP_EXPR:
15218 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15219 tree op0 = RECUR (TREE_OPERAND (t, 0));
15220 RETURN (build_nop (type, op0));
15223 case IMPLICIT_CONV_EXPR:
15225 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15226 tree expr = RECUR (TREE_OPERAND (t, 0));
15227 int flags = LOOKUP_IMPLICIT;
15228 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
15229 flags = LOOKUP_NORMAL;
15230 RETURN (perform_implicit_conversion_flags (type, expr, complain,
15231 flags));
15234 case CONVERT_EXPR:
15236 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15237 tree op0 = RECUR (TREE_OPERAND (t, 0));
15238 RETURN (build1 (CONVERT_EXPR, type, op0));
15241 case CAST_EXPR:
15242 case REINTERPRET_CAST_EXPR:
15243 case CONST_CAST_EXPR:
15244 case DYNAMIC_CAST_EXPR:
15245 case STATIC_CAST_EXPR:
15247 tree type;
15248 tree op, r = NULL_TREE;
15250 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15251 if (integral_constant_expression_p
15252 && !cast_valid_in_integral_constant_expression_p (type))
15254 if (complain & tf_error)
15255 error ("a cast to a type other than an integral or "
15256 "enumeration type cannot appear in a constant-expression");
15257 RETURN (error_mark_node);
15260 op = RECUR (TREE_OPERAND (t, 0));
15262 warning_sentinel s(warn_useless_cast);
15263 switch (TREE_CODE (t))
15265 case CAST_EXPR:
15266 r = build_functional_cast (type, op, complain);
15267 break;
15268 case REINTERPRET_CAST_EXPR:
15269 r = build_reinterpret_cast (type, op, complain);
15270 break;
15271 case CONST_CAST_EXPR:
15272 r = build_const_cast (type, op, complain);
15273 break;
15274 case DYNAMIC_CAST_EXPR:
15275 r = build_dynamic_cast (type, op, complain);
15276 break;
15277 case STATIC_CAST_EXPR:
15278 r = build_static_cast (type, op, complain);
15279 break;
15280 default:
15281 gcc_unreachable ();
15284 RETURN (r);
15287 case POSTDECREMENT_EXPR:
15288 case POSTINCREMENT_EXPR:
15289 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15290 args, complain, in_decl);
15291 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
15292 complain|decltype_flag));
15294 case PREDECREMENT_EXPR:
15295 case PREINCREMENT_EXPR:
15296 case NEGATE_EXPR:
15297 case BIT_NOT_EXPR:
15298 case ABS_EXPR:
15299 case TRUTH_NOT_EXPR:
15300 case UNARY_PLUS_EXPR: /* Unary + */
15301 case REALPART_EXPR:
15302 case IMAGPART_EXPR:
15303 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
15304 RECUR (TREE_OPERAND (t, 0)),
15305 complain|decltype_flag));
15307 case FIX_TRUNC_EXPR:
15308 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
15309 0, complain));
15311 case ADDR_EXPR:
15312 op1 = TREE_OPERAND (t, 0);
15313 if (TREE_CODE (op1) == LABEL_DECL)
15314 RETURN (finish_label_address_expr (DECL_NAME (op1),
15315 EXPR_LOCATION (op1)));
15316 if (TREE_CODE (op1) == SCOPE_REF)
15317 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
15318 /*done=*/true, /*address_p=*/true);
15319 else
15320 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
15321 in_decl);
15322 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
15323 complain|decltype_flag));
15325 case PLUS_EXPR:
15326 case MINUS_EXPR:
15327 case MULT_EXPR:
15328 case TRUNC_DIV_EXPR:
15329 case CEIL_DIV_EXPR:
15330 case FLOOR_DIV_EXPR:
15331 case ROUND_DIV_EXPR:
15332 case EXACT_DIV_EXPR:
15333 case BIT_AND_EXPR:
15334 case BIT_IOR_EXPR:
15335 case BIT_XOR_EXPR:
15336 case TRUNC_MOD_EXPR:
15337 case FLOOR_MOD_EXPR:
15338 case TRUTH_ANDIF_EXPR:
15339 case TRUTH_ORIF_EXPR:
15340 case TRUTH_AND_EXPR:
15341 case TRUTH_OR_EXPR:
15342 case RSHIFT_EXPR:
15343 case LSHIFT_EXPR:
15344 case RROTATE_EXPR:
15345 case LROTATE_EXPR:
15346 case EQ_EXPR:
15347 case NE_EXPR:
15348 case MAX_EXPR:
15349 case MIN_EXPR:
15350 case LE_EXPR:
15351 case GE_EXPR:
15352 case LT_EXPR:
15353 case GT_EXPR:
15354 case MEMBER_REF:
15355 case DOTSTAR_EXPR:
15357 warning_sentinel s1(warn_type_limits);
15358 warning_sentinel s2(warn_div_by_zero);
15359 tree op0 = RECUR (TREE_OPERAND (t, 0));
15360 tree op1 = RECUR (TREE_OPERAND (t, 1));
15361 tree r = build_x_binary_op
15362 (input_location, TREE_CODE (t),
15363 op0,
15364 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
15365 ? ERROR_MARK
15366 : TREE_CODE (TREE_OPERAND (t, 0))),
15367 op1,
15368 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
15369 ? ERROR_MARK
15370 : TREE_CODE (TREE_OPERAND (t, 1))),
15371 /*overload=*/NULL,
15372 complain|decltype_flag);
15373 if (EXPR_P (r) && TREE_NO_WARNING (t))
15374 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15376 RETURN (r);
15379 case POINTER_PLUS_EXPR:
15381 tree op0 = RECUR (TREE_OPERAND (t, 0));
15382 tree op1 = RECUR (TREE_OPERAND (t, 1));
15383 return fold_build_pointer_plus (op0, op1);
15386 case SCOPE_REF:
15387 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
15388 /*address_p=*/false));
15389 case ARRAY_REF:
15390 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15391 args, complain, in_decl);
15392 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
15393 RECUR (TREE_OPERAND (t, 1)),
15394 complain|decltype_flag));
15396 case ARRAY_NOTATION_REF:
15398 tree start_index, length, stride;
15399 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
15400 args, complain, in_decl);
15401 start_index = RECUR (ARRAY_NOTATION_START (t));
15402 length = RECUR (ARRAY_NOTATION_LENGTH (t));
15403 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
15404 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
15405 length, stride, TREE_TYPE (op1)));
15407 case SIZEOF_EXPR:
15408 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
15409 RETURN (tsubst_copy (t, args, complain, in_decl));
15410 /* Fall through */
15412 case ALIGNOF_EXPR:
15414 tree r;
15416 op1 = TREE_OPERAND (t, 0);
15417 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
15418 op1 = TREE_TYPE (op1);
15419 if (!args)
15421 /* When there are no ARGS, we are trying to evaluate a
15422 non-dependent expression from the parser. Trying to do
15423 the substitutions may not work. */
15424 if (!TYPE_P (op1))
15425 op1 = TREE_TYPE (op1);
15427 else
15429 ++cp_unevaluated_operand;
15430 ++c_inhibit_evaluation_warnings;
15431 if (TYPE_P (op1))
15432 op1 = tsubst (op1, args, complain, in_decl);
15433 else
15434 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
15435 /*function_p=*/false,
15436 /*integral_constant_expression_p=*/
15437 false);
15438 --cp_unevaluated_operand;
15439 --c_inhibit_evaluation_warnings;
15441 if (TYPE_P (op1))
15442 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
15443 complain & tf_error);
15444 else
15445 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
15446 complain & tf_error);
15447 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
15449 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
15451 if (!processing_template_decl && TYPE_P (op1))
15453 r = build_min (SIZEOF_EXPR, size_type_node,
15454 build1 (NOP_EXPR, op1, error_mark_node));
15455 SIZEOF_EXPR_TYPE_P (r) = 1;
15457 else
15458 r = build_min (SIZEOF_EXPR, size_type_node, op1);
15459 TREE_SIDE_EFFECTS (r) = 0;
15460 TREE_READONLY (r) = 1;
15462 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
15464 RETURN (r);
15467 case AT_ENCODE_EXPR:
15469 op1 = TREE_OPERAND (t, 0);
15470 ++cp_unevaluated_operand;
15471 ++c_inhibit_evaluation_warnings;
15472 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
15473 /*function_p=*/false,
15474 /*integral_constant_expression_p=*/false);
15475 --cp_unevaluated_operand;
15476 --c_inhibit_evaluation_warnings;
15477 RETURN (objc_build_encode_expr (op1));
15480 case NOEXCEPT_EXPR:
15481 op1 = TREE_OPERAND (t, 0);
15482 ++cp_unevaluated_operand;
15483 ++c_inhibit_evaluation_warnings;
15484 ++cp_noexcept_operand;
15485 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
15486 /*function_p=*/false,
15487 /*integral_constant_expression_p=*/false);
15488 --cp_unevaluated_operand;
15489 --c_inhibit_evaluation_warnings;
15490 --cp_noexcept_operand;
15491 RETURN (finish_noexcept_expr (op1, complain));
15493 case MODOP_EXPR:
15495 warning_sentinel s(warn_div_by_zero);
15496 tree lhs = RECUR (TREE_OPERAND (t, 0));
15497 tree rhs = RECUR (TREE_OPERAND (t, 2));
15498 tree r = build_x_modify_expr
15499 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
15500 complain|decltype_flag);
15501 /* TREE_NO_WARNING must be set if either the expression was
15502 parenthesized or it uses an operator such as >>= rather
15503 than plain assignment. In the former case, it was already
15504 set and must be copied. In the latter case,
15505 build_x_modify_expr sets it and it must not be reset
15506 here. */
15507 if (TREE_NO_WARNING (t))
15508 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15510 RETURN (r);
15513 case ARROW_EXPR:
15514 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15515 args, complain, in_decl);
15516 /* Remember that there was a reference to this entity. */
15517 if (DECL_P (op1))
15518 mark_used (op1);
15519 RETURN (build_x_arrow (input_location, op1, complain));
15521 case NEW_EXPR:
15523 tree placement = RECUR (TREE_OPERAND (t, 0));
15524 tree init = RECUR (TREE_OPERAND (t, 3));
15525 vec<tree, va_gc> *placement_vec;
15526 vec<tree, va_gc> *init_vec;
15527 tree ret;
15529 if (placement == NULL_TREE)
15530 placement_vec = NULL;
15531 else
15533 placement_vec = make_tree_vector ();
15534 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
15535 vec_safe_push (placement_vec, TREE_VALUE (placement));
15538 /* If there was an initializer in the original tree, but it
15539 instantiated to an empty list, then we should pass a
15540 non-NULL empty vector to tell build_new that it was an
15541 empty initializer() rather than no initializer. This can
15542 only happen when the initializer is a pack expansion whose
15543 parameter packs are of length zero. */
15544 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
15545 init_vec = NULL;
15546 else
15548 init_vec = make_tree_vector ();
15549 if (init == void_node)
15550 gcc_assert (init_vec != NULL);
15551 else
15553 for (; init != NULL_TREE; init = TREE_CHAIN (init))
15554 vec_safe_push (init_vec, TREE_VALUE (init));
15558 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
15559 tree op2 = RECUR (TREE_OPERAND (t, 2));
15560 ret = build_new (&placement_vec, op1, op2, &init_vec,
15561 NEW_EXPR_USE_GLOBAL (t),
15562 complain);
15564 if (placement_vec != NULL)
15565 release_tree_vector (placement_vec);
15566 if (init_vec != NULL)
15567 release_tree_vector (init_vec);
15569 RETURN (ret);
15572 case DELETE_EXPR:
15574 tree op0 = RECUR (TREE_OPERAND (t, 0));
15575 tree op1 = RECUR (TREE_OPERAND (t, 1));
15576 RETURN (delete_sanity (op0, op1,
15577 DELETE_EXPR_USE_VEC (t),
15578 DELETE_EXPR_USE_GLOBAL (t),
15579 complain));
15582 case COMPOUND_EXPR:
15584 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
15585 complain & ~tf_decltype, in_decl,
15586 /*function_p=*/false,
15587 integral_constant_expression_p);
15588 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
15589 op0,
15590 RECUR (TREE_OPERAND (t, 1)),
15591 complain|decltype_flag));
15594 case CALL_EXPR:
15596 tree function;
15597 vec<tree, va_gc> *call_args;
15598 unsigned int nargs, i;
15599 bool qualified_p;
15600 bool koenig_p;
15601 tree ret;
15603 function = CALL_EXPR_FN (t);
15604 /* When we parsed the expression, we determined whether or
15605 not Koenig lookup should be performed. */
15606 koenig_p = KOENIG_LOOKUP_P (t);
15607 if (TREE_CODE (function) == SCOPE_REF)
15609 qualified_p = true;
15610 function = tsubst_qualified_id (function, args, complain, in_decl,
15611 /*done=*/false,
15612 /*address_p=*/false);
15614 else if (koenig_p && identifier_p (function))
15616 /* Do nothing; calling tsubst_copy_and_build on an identifier
15617 would incorrectly perform unqualified lookup again.
15619 Note that we can also have an IDENTIFIER_NODE if the earlier
15620 unqualified lookup found a member function; in that case
15621 koenig_p will be false and we do want to do the lookup
15622 again to find the instantiated member function.
15624 FIXME but doing that causes c++/15272, so we need to stop
15625 using IDENTIFIER_NODE in that situation. */
15626 qualified_p = false;
15628 else
15630 if (TREE_CODE (function) == COMPONENT_REF)
15632 tree op = TREE_OPERAND (function, 1);
15634 qualified_p = (TREE_CODE (op) == SCOPE_REF
15635 || (BASELINK_P (op)
15636 && BASELINK_QUALIFIED_P (op)));
15638 else
15639 qualified_p = false;
15641 if (TREE_CODE (function) == ADDR_EXPR
15642 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
15643 /* Avoid error about taking the address of a constructor. */
15644 function = TREE_OPERAND (function, 0);
15646 function = tsubst_copy_and_build (function, args, complain,
15647 in_decl,
15648 !qualified_p,
15649 integral_constant_expression_p);
15651 if (BASELINK_P (function))
15652 qualified_p = true;
15655 nargs = call_expr_nargs (t);
15656 call_args = make_tree_vector ();
15657 for (i = 0; i < nargs; ++i)
15659 tree arg = CALL_EXPR_ARG (t, i);
15661 if (!PACK_EXPANSION_P (arg) && !pending_expansion_p (arg))
15662 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
15663 else
15665 /* Expand the pack expansion and push each entry onto
15666 CALL_ARGS. */
15667 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
15668 if (TREE_CODE (arg) == TREE_VEC)
15670 unsigned int len, j;
15672 len = TREE_VEC_LENGTH (arg);
15673 for (j = 0; j < len; ++j)
15675 tree value = TREE_VEC_ELT (arg, j);
15676 if (value != NULL_TREE)
15677 value = convert_from_reference (value);
15678 vec_safe_push (call_args, value);
15681 else
15683 /* A partial substitution. Add one entry. */
15684 vec_safe_push (call_args, arg);
15689 /* We do not perform argument-dependent lookup if normal
15690 lookup finds a non-function, in accordance with the
15691 expected resolution of DR 218. */
15692 if (koenig_p
15693 && ((is_overloaded_fn (function)
15694 /* If lookup found a member function, the Koenig lookup is
15695 not appropriate, even if an unqualified-name was used
15696 to denote the function. */
15697 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
15698 || identifier_p (function))
15699 /* Only do this when substitution turns a dependent call
15700 into a non-dependent call. */
15701 && type_dependent_expression_p_push (t)
15702 && !any_type_dependent_arguments_p (call_args))
15703 function = perform_koenig_lookup (function, call_args, tf_none);
15705 if (identifier_p (function)
15706 && !any_type_dependent_arguments_p (call_args))
15708 if (koenig_p && (complain & tf_warning_or_error))
15710 /* For backwards compatibility and good diagnostics, try
15711 the unqualified lookup again if we aren't in SFINAE
15712 context. */
15713 tree unq = (tsubst_copy_and_build
15714 (function, args, complain, in_decl, true,
15715 integral_constant_expression_p));
15716 if (unq == error_mark_node)
15717 RETURN (error_mark_node);
15719 if (unq != function)
15721 tree fn = unq;
15722 if (INDIRECT_REF_P (fn))
15723 fn = TREE_OPERAND (fn, 0);
15724 if (TREE_CODE (fn) == COMPONENT_REF)
15725 fn = TREE_OPERAND (fn, 1);
15726 if (is_overloaded_fn (fn))
15727 fn = get_first_fn (fn);
15728 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
15729 "%qD was not declared in this scope, "
15730 "and no declarations were found by "
15731 "argument-dependent lookup at the point "
15732 "of instantiation", function))
15734 if (!DECL_P (fn))
15735 /* Can't say anything more. */;
15736 else if (DECL_CLASS_SCOPE_P (fn))
15738 location_t loc = EXPR_LOC_OR_LOC (t,
15739 input_location);
15740 inform (loc,
15741 "declarations in dependent base %qT are "
15742 "not found by unqualified lookup",
15743 DECL_CLASS_CONTEXT (fn));
15744 if (current_class_ptr)
15745 inform (loc,
15746 "use %<this->%D%> instead", function);
15747 else
15748 inform (loc,
15749 "use %<%T::%D%> instead",
15750 current_class_name, function);
15752 else
15753 inform (0, "%q+D declared here, later in the "
15754 "translation unit", fn);
15756 function = unq;
15759 if (identifier_p (function))
15761 if (complain & tf_error)
15762 unqualified_name_lookup_error (function);
15763 release_tree_vector (call_args);
15764 RETURN (error_mark_node);
15768 /* Remember that there was a reference to this entity. */
15769 if (DECL_P (function))
15770 mark_used (function, complain);
15772 /* Put back tf_decltype for the actual call. */
15773 complain |= decltype_flag;
15775 if (TREE_CODE (function) == OFFSET_REF)
15776 ret = build_offset_ref_call_from_tree (function, &call_args,
15777 complain);
15778 else if (TREE_CODE (function) == COMPONENT_REF)
15780 tree instance = TREE_OPERAND (function, 0);
15781 tree fn = TREE_OPERAND (function, 1);
15783 if (processing_template_decl
15784 && (type_dependent_expression_p (instance)
15785 || (!BASELINK_P (fn)
15786 && TREE_CODE (fn) != FIELD_DECL)
15787 || type_dependent_expression_p (fn)
15788 || any_type_dependent_arguments_p (call_args)))
15789 ret = build_nt_call_vec (function, call_args);
15790 else if (!BASELINK_P (fn))
15791 ret = finish_call_expr (function, &call_args,
15792 /*disallow_virtual=*/false,
15793 /*koenig_p=*/false,
15794 complain);
15795 else
15796 ret = (build_new_method_call
15797 (instance, fn,
15798 &call_args, NULL_TREE,
15799 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
15800 /*fn_p=*/NULL,
15801 complain));
15803 else
15804 ret = finish_call_expr (function, &call_args,
15805 /*disallow_virtual=*/qualified_p,
15806 koenig_p,
15807 complain);
15809 release_tree_vector (call_args);
15811 RETURN (ret);
15814 case COND_EXPR:
15816 tree cond = RECUR (TREE_OPERAND (t, 0));
15817 tree folded_cond = fold_non_dependent_expr (cond);
15818 tree exp1, exp2;
15820 if (TREE_CODE (folded_cond) == INTEGER_CST)
15822 if (integer_zerop (folded_cond))
15824 ++c_inhibit_evaluation_warnings;
15825 exp1 = RECUR (TREE_OPERAND (t, 1));
15826 --c_inhibit_evaluation_warnings;
15827 exp2 = RECUR (TREE_OPERAND (t, 2));
15829 else
15831 exp1 = RECUR (TREE_OPERAND (t, 1));
15832 ++c_inhibit_evaluation_warnings;
15833 exp2 = RECUR (TREE_OPERAND (t, 2));
15834 --c_inhibit_evaluation_warnings;
15836 cond = folded_cond;
15838 else
15840 exp1 = RECUR (TREE_OPERAND (t, 1));
15841 exp2 = RECUR (TREE_OPERAND (t, 2));
15844 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
15845 cond, exp1, exp2, complain));
15848 case PSEUDO_DTOR_EXPR:
15850 tree op0 = RECUR (TREE_OPERAND (t, 0));
15851 tree op1 = RECUR (TREE_OPERAND (t, 1));
15852 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
15853 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
15854 input_location));
15857 case TREE_LIST:
15859 tree purpose, value, chain;
15861 if (t == void_list_node)
15862 RETURN (t);
15864 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
15865 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
15867 /* We have pack expansions, so expand those and
15868 create a new list out of it. */
15869 tree purposevec = NULL_TREE;
15870 tree valuevec = NULL_TREE;
15871 tree chain;
15872 int i, len = -1;
15874 /* Expand the argument expressions. */
15875 if (TREE_PURPOSE (t))
15876 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
15877 complain, in_decl);
15878 if (TREE_VALUE (t))
15879 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
15880 complain, in_decl);
15882 /* Build the rest of the list. */
15883 chain = TREE_CHAIN (t);
15884 if (chain && chain != void_type_node)
15885 chain = RECUR (chain);
15887 /* Determine the number of arguments. */
15888 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
15890 len = TREE_VEC_LENGTH (purposevec);
15891 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15893 else if (TREE_CODE (valuevec) == TREE_VEC)
15894 len = TREE_VEC_LENGTH (valuevec);
15895 else
15897 /* Since we only performed a partial substitution into
15898 the argument pack, we only RETURN (a single list
15899 node. */
15900 if (purposevec == TREE_PURPOSE (t)
15901 && valuevec == TREE_VALUE (t)
15902 && chain == TREE_CHAIN (t))
15903 RETURN (t);
15905 RETURN (tree_cons (purposevec, valuevec, chain));
15908 /* Convert the argument vectors into a TREE_LIST */
15909 i = len;
15910 while (i > 0)
15912 /* Grab the Ith values. */
15913 i--;
15914 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
15915 : NULL_TREE;
15916 value
15917 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
15918 : NULL_TREE;
15920 /* Build the list (backwards). */
15921 chain = tree_cons (purpose, value, chain);
15924 RETURN (chain);
15927 purpose = TREE_PURPOSE (t);
15928 if (purpose)
15929 purpose = RECUR (purpose);
15930 value = TREE_VALUE (t);
15931 if (value)
15932 value = RECUR (value);
15933 chain = TREE_CHAIN (t);
15934 if (chain && chain != void_type_node)
15935 chain = RECUR (chain);
15936 if (purpose == TREE_PURPOSE (t)
15937 && value == TREE_VALUE (t)
15938 && chain == TREE_CHAIN (t))
15939 RETURN (t);
15940 RETURN (tree_cons (purpose, value, chain));
15943 case COMPONENT_REF:
15945 tree object;
15946 tree object_type;
15947 tree member;
15948 tree r;
15950 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15951 args, complain, in_decl);
15952 /* Remember that there was a reference to this entity. */
15953 if (DECL_P (object))
15954 mark_used (object);
15955 object_type = TREE_TYPE (object);
15957 member = TREE_OPERAND (t, 1);
15958 if (BASELINK_P (member))
15959 member = tsubst_baselink (member,
15960 non_reference (TREE_TYPE (object)),
15961 args, complain, in_decl);
15962 else
15963 member = tsubst_copy (member, args, complain, in_decl);
15964 if (member == error_mark_node)
15965 RETURN (error_mark_node);
15967 if (type_dependent_expression_p (object))
15968 /* We can't do much here. */;
15969 else if (!CLASS_TYPE_P (object_type))
15971 if (scalarish_type_p (object_type))
15973 tree s = NULL_TREE;
15974 tree dtor = member;
15976 if (TREE_CODE (dtor) == SCOPE_REF)
15978 s = TREE_OPERAND (dtor, 0);
15979 dtor = TREE_OPERAND (dtor, 1);
15981 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
15983 dtor = TREE_OPERAND (dtor, 0);
15984 if (TYPE_P (dtor))
15985 RETURN (finish_pseudo_destructor_expr
15986 (object, s, dtor, input_location));
15990 else if (TREE_CODE (member) == SCOPE_REF
15991 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
15993 /* Lookup the template functions now that we know what the
15994 scope is. */
15995 tree scope = TREE_OPERAND (member, 0);
15996 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
15997 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
15998 member = lookup_qualified_name (scope, tmpl,
15999 /*is_type_p=*/false,
16000 /*complain=*/false);
16001 if (BASELINK_P (member))
16003 BASELINK_FUNCTIONS (member)
16004 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
16005 args);
16006 member = (adjust_result_of_qualified_name_lookup
16007 (member, BINFO_TYPE (BASELINK_BINFO (member)),
16008 object_type));
16010 else
16012 qualified_name_lookup_error (scope, tmpl, member,
16013 input_location);
16014 RETURN (error_mark_node);
16017 else if (TREE_CODE (member) == SCOPE_REF
16018 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
16019 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
16021 if (complain & tf_error)
16023 if (TYPE_P (TREE_OPERAND (member, 0)))
16024 error ("%qT is not a class or namespace",
16025 TREE_OPERAND (member, 0));
16026 else
16027 error ("%qD is not a class or namespace",
16028 TREE_OPERAND (member, 0));
16030 RETURN (error_mark_node);
16032 else if (TREE_CODE (member) == FIELD_DECL)
16034 r = finish_non_static_data_member (member, object, NULL_TREE);
16035 if (TREE_CODE (r) == COMPONENT_REF)
16036 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16037 RETURN (r);
16040 r = finish_class_member_access_expr (object, member,
16041 /*template_p=*/false,
16042 complain);
16043 if (TREE_CODE (r) == COMPONENT_REF)
16044 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16045 RETURN (r);
16048 case THROW_EXPR:
16049 RETURN (build_throw
16050 (RECUR (TREE_OPERAND (t, 0))));
16052 case CONSTRUCTOR:
16054 vec<constructor_elt, va_gc> *n;
16055 constructor_elt *ce;
16056 unsigned HOST_WIDE_INT idx;
16057 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16058 bool process_index_p;
16059 int newlen;
16060 bool need_copy_p = false;
16061 tree r;
16063 if (type == error_mark_node)
16064 RETURN (error_mark_node);
16066 /* digest_init will do the wrong thing if we let it. */
16067 if (type && TYPE_PTRMEMFUNC_P (type))
16068 RETURN (t);
16070 /* We do not want to process the index of aggregate
16071 initializers as they are identifier nodes which will be
16072 looked up by digest_init. */
16073 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
16075 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
16076 newlen = vec_safe_length (n);
16077 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
16079 if (ce->index && process_index_p
16080 /* An identifier index is looked up in the type
16081 being initialized, not the current scope. */
16082 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
16083 ce->index = RECUR (ce->index);
16085 if (PACK_EXPANSION_P (ce->value))
16087 /* Substitute into the pack expansion. */
16088 ce->value = tsubst_pack_expansion (ce->value, args, complain,
16089 in_decl);
16091 if (ce->value == error_mark_node
16092 || PACK_EXPANSION_P (ce->value))
16094 else if (TREE_VEC_LENGTH (ce->value) == 1)
16095 /* Just move the argument into place. */
16096 ce->value = TREE_VEC_ELT (ce->value, 0);
16097 else
16099 /* Update the length of the final CONSTRUCTOR
16100 arguments vector, and note that we will need to
16101 copy.*/
16102 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
16103 need_copy_p = true;
16106 else
16107 ce->value = RECUR (ce->value);
16110 if (need_copy_p)
16112 vec<constructor_elt, va_gc> *old_n = n;
16114 vec_alloc (n, newlen);
16115 FOR_EACH_VEC_ELT (*old_n, idx, ce)
16117 if (TREE_CODE (ce->value) == TREE_VEC)
16119 int i, len = TREE_VEC_LENGTH (ce->value);
16120 for (i = 0; i < len; ++i)
16121 CONSTRUCTOR_APPEND_ELT (n, 0,
16122 TREE_VEC_ELT (ce->value, i));
16124 else
16125 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
16129 r = build_constructor (init_list_type_node, n);
16130 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
16132 if (TREE_HAS_CONSTRUCTOR (t))
16133 RETURN (finish_compound_literal (type, r, complain));
16135 TREE_TYPE (r) = type;
16136 RETURN (r);
16139 case TYPEID_EXPR:
16141 tree operand_0 = TREE_OPERAND (t, 0);
16142 if (TYPE_P (operand_0))
16144 operand_0 = tsubst (operand_0, args, complain, in_decl);
16145 RETURN (get_typeid (operand_0, complain));
16147 else
16149 operand_0 = RECUR (operand_0);
16150 RETURN (build_typeid (operand_0, complain));
16154 case VAR_DECL:
16155 if (!args)
16156 RETURN (t);
16157 else if (DECL_PACK_P (t))
16159 /* We don't build decls for an instantiation of a
16160 variadic capture proxy, we instantiate the elements
16161 when needed. */
16162 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
16163 return RECUR (DECL_VALUE_EXPR (t));
16165 /* Fall through */
16167 case PARM_DECL:
16169 tree r = tsubst_copy (t, args, complain, in_decl);
16170 /* ??? We're doing a subset of finish_id_expression here. */
16171 if (VAR_P (r)
16172 && !processing_template_decl
16173 && !cp_unevaluated_operand
16174 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
16175 && DECL_THREAD_LOCAL_P (r))
16177 if (tree wrap = get_tls_wrapper_fn (r))
16178 /* Replace an evaluated use of the thread_local variable with
16179 a call to its wrapper. */
16180 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
16182 else if (outer_automatic_var_p (r))
16183 r = process_outer_var_ref (r, complain);
16185 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
16186 /* If the original type was a reference, we'll be wrapped in
16187 the appropriate INDIRECT_REF. */
16188 r = convert_from_reference (r);
16190 RETURN (r);
16193 case VA_ARG_EXPR:
16195 tree op0 = RECUR (TREE_OPERAND (t, 0));
16196 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16197 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
16200 case OFFSETOF_EXPR:
16201 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0)),
16202 EXPR_LOCATION (t)));
16204 case TRAIT_EXPR:
16206 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
16207 complain, in_decl);
16209 tree type2 = TRAIT_EXPR_TYPE2 (t);
16210 if (type2 && TREE_CODE (type2) == TREE_LIST)
16211 type2 = RECUR (type2);
16212 else if (type2)
16213 type2 = tsubst (type2, args, complain, in_decl);
16215 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
16218 case STMT_EXPR:
16220 tree old_stmt_expr = cur_stmt_expr;
16221 tree stmt_expr = begin_stmt_expr ();
16223 cur_stmt_expr = stmt_expr;
16224 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
16225 integral_constant_expression_p);
16226 stmt_expr = finish_stmt_expr (stmt_expr, false);
16227 cur_stmt_expr = old_stmt_expr;
16229 /* If the resulting list of expression statement is empty,
16230 fold it further into void_node. */
16231 if (empty_expr_stmt_p (stmt_expr))
16232 stmt_expr = void_node;
16234 RETURN (stmt_expr);
16237 case LAMBDA_EXPR:
16239 tree r = build_lambda_expr ();
16241 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
16242 LAMBDA_EXPR_CLOSURE (r) = type;
16243 CLASSTYPE_LAMBDA_EXPR (type) = r;
16245 LAMBDA_EXPR_LOCATION (r)
16246 = LAMBDA_EXPR_LOCATION (t);
16247 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16248 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16249 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16250 LAMBDA_EXPR_DISCRIMINATOR (r)
16251 = (LAMBDA_EXPR_DISCRIMINATOR (t));
16252 /* For a function scope, we want to use tsubst so that we don't
16253 complain about referring to an auto function before its return
16254 type has been deduced. Otherwise, we want to use tsubst_copy so
16255 that we look up the existing field/parameter/variable rather
16256 than build a new one. */
16257 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
16258 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
16259 scope = tsubst (scope, args, complain, in_decl);
16260 else if (scope && TREE_CODE (scope) == PARM_DECL)
16262 /* Look up the parameter we want directly, as tsubst_copy
16263 doesn't do what we need. */
16264 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
16265 tree parm = FUNCTION_FIRST_USER_PARM (fn);
16266 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
16267 parm = DECL_CHAIN (parm);
16268 scope = parm;
16269 /* FIXME Work around the parm not having DECL_CONTEXT set. */
16270 if (DECL_CONTEXT (scope) == NULL_TREE)
16271 DECL_CONTEXT (scope) = fn;
16273 else
16274 scope = RECUR (scope);
16275 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
16276 LAMBDA_EXPR_RETURN_TYPE (r)
16277 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
16279 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16280 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16282 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16283 determine_visibility (TYPE_NAME (type));
16284 /* Now that we know visibility, instantiate the type so we have a
16285 declaration of the op() for later calls to lambda_function. */
16286 complete_type (type);
16288 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16290 RETURN (build_lambda_object (r));
16293 case TARGET_EXPR:
16294 /* We can get here for a constant initializer of non-dependent type.
16295 FIXME stop folding in cp_parser_initializer_clause. */
16297 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
16298 complain);
16299 RETURN (r);
16302 case TRANSACTION_EXPR:
16303 RETURN (tsubst_expr(t, args, complain, in_decl,
16304 integral_constant_expression_p));
16306 case PAREN_EXPR:
16307 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
16309 case VEC_PERM_EXPR:
16311 tree op0 = RECUR (TREE_OPERAND (t, 0));
16312 tree op1 = RECUR (TREE_OPERAND (t, 1));
16313 tree op2 = RECUR (TREE_OPERAND (t, 2));
16314 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
16315 complain));
16318 case REQUIRES_EXPR:
16319 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
16321 default:
16322 /* Handle Objective-C++ constructs, if appropriate. */
16324 tree subst
16325 = objcp_tsubst_copy_and_build (t, args, complain,
16326 in_decl, /*function_p=*/false);
16327 if (subst)
16328 RETURN (subst);
16330 RETURN (tsubst_copy (t, args, complain, in_decl));
16333 #undef RECUR
16334 #undef RETURN
16335 out:
16336 input_location = loc;
16337 return retval;
16340 /* Verify that the instantiated ARGS are valid. For type arguments,
16341 make sure that the type's linkage is ok. For non-type arguments,
16342 make sure they are constants if they are integral or enumerations.
16343 Emit an error under control of COMPLAIN, and return TRUE on error. */
16345 static bool
16346 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
16348 if (dependent_template_arg_p (t))
16349 return false;
16350 if (ARGUMENT_PACK_P (t))
16352 tree vec = ARGUMENT_PACK_ARGS (t);
16353 int len = TREE_VEC_LENGTH (vec);
16354 bool result = false;
16355 int i;
16357 for (i = 0; i < len; ++i)
16358 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
16359 result = true;
16360 return result;
16362 else if (TYPE_P (t))
16364 /* [basic.link]: A name with no linkage (notably, the name
16365 of a class or enumeration declared in a local scope)
16366 shall not be used to declare an entity with linkage.
16367 This implies that names with no linkage cannot be used as
16368 template arguments
16370 DR 757 relaxes this restriction for C++0x. */
16371 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
16372 : no_linkage_check (t, /*relaxed_p=*/false));
16374 if (nt)
16376 /* DR 488 makes use of a type with no linkage cause
16377 type deduction to fail. */
16378 if (complain & tf_error)
16380 if (TYPE_ANONYMOUS_P (nt))
16381 error ("%qT is/uses anonymous type", t);
16382 else
16383 error ("template argument for %qD uses local type %qT",
16384 tmpl, t);
16386 return true;
16388 /* In order to avoid all sorts of complications, we do not
16389 allow variably-modified types as template arguments. */
16390 else if (variably_modified_type_p (t, NULL_TREE))
16392 if (complain & tf_error)
16393 error ("%qT is a variably modified type", t);
16394 return true;
16397 /* Class template and alias template arguments should be OK. */
16398 else if (DECL_TYPE_TEMPLATE_P (t))
16400 /* A non-type argument of integral or enumerated type must be a
16401 constant. */
16402 else if (TREE_TYPE (t)
16403 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
16404 && !REFERENCE_REF_P (t)
16405 && !TREE_CONSTANT (t))
16407 if (complain & tf_error)
16408 error ("integral expression %qE is not constant", t);
16409 return true;
16411 return false;
16414 static bool
16415 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
16417 int ix, len = DECL_NTPARMS (tmpl);
16418 bool result = false;
16420 for (ix = 0; ix != len; ix++)
16422 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
16423 result = true;
16425 if (result && (complain & tf_error))
16426 error (" trying to instantiate %qD", tmpl);
16427 return result;
16430 /* We're out of SFINAE context now, so generate diagnostics for the access
16431 errors we saw earlier when instantiating D from TMPL and ARGS. */
16433 static void
16434 recheck_decl_substitution (tree d, tree tmpl, tree args)
16436 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
16437 tree type = TREE_TYPE (pattern);
16438 location_t loc = input_location;
16440 push_access_scope (d);
16441 push_deferring_access_checks (dk_no_deferred);
16442 input_location = DECL_SOURCE_LOCATION (pattern);
16443 tsubst (type, args, tf_warning_or_error, d);
16444 input_location = loc;
16445 pop_deferring_access_checks ();
16446 pop_access_scope (d);
16449 /* Instantiate the indicated variable, function, or alias template TMPL with
16450 the template arguments in TARG_PTR. */
16452 static tree
16453 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
16455 tree targ_ptr = orig_args;
16456 tree fndecl;
16457 tree gen_tmpl;
16458 tree spec;
16459 bool access_ok = true;
16461 if (tmpl == error_mark_node)
16462 return error_mark_node;
16464 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
16466 /* If this function is a clone, handle it specially. */
16467 if (DECL_CLONED_FUNCTION_P (tmpl))
16469 tree spec;
16470 tree clone;
16472 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
16473 DECL_CLONED_FUNCTION. */
16474 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
16475 targ_ptr, complain);
16476 if (spec == error_mark_node)
16477 return error_mark_node;
16479 /* Look for the clone. */
16480 FOR_EACH_CLONE (clone, spec)
16481 if (DECL_NAME (clone) == DECL_NAME (tmpl))
16482 return clone;
16483 /* We should always have found the clone by now. */
16484 gcc_unreachable ();
16485 return NULL_TREE;
16488 if (targ_ptr == error_mark_node)
16489 return error_mark_node;
16491 /* Check to see if we already have this specialization. */
16492 gen_tmpl = most_general_template (tmpl);
16493 if (tmpl != gen_tmpl)
16494 /* The TMPL is a partial instantiation. To get a full set of
16495 arguments we must add the arguments used to perform the
16496 partial instantiation. */
16497 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
16498 targ_ptr);
16500 /* It would be nice to avoid hashing here and then again in tsubst_decl,
16501 but it doesn't seem to be on the hot path. */
16502 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
16504 gcc_assert (tmpl == gen_tmpl
16505 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
16506 == spec)
16507 || fndecl == NULL_TREE);
16509 if (spec != NULL_TREE)
16511 if (FNDECL_HAS_ACCESS_ERRORS (spec))
16513 if (complain & tf_error)
16514 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
16515 return error_mark_node;
16517 return spec;
16520 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
16521 complain))
16522 return error_mark_node;
16524 /* We are building a FUNCTION_DECL, during which the access of its
16525 parameters and return types have to be checked. However this
16526 FUNCTION_DECL which is the desired context for access checking
16527 is not built yet. We solve this chicken-and-egg problem by
16528 deferring all checks until we have the FUNCTION_DECL. */
16529 push_deferring_access_checks (dk_deferred);
16531 /* Instantiation of the function happens in the context of the function
16532 template, not the context of the overload resolution we're doing. */
16533 push_to_top_level ();
16534 /* If there are dependent arguments, e.g. because we're doing partial
16535 ordering, make sure processing_template_decl stays set. */
16536 if (uses_template_parms (targ_ptr))
16537 ++processing_template_decl;
16538 if (DECL_CLASS_SCOPE_P (gen_tmpl))
16540 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
16541 complain, gen_tmpl, true);
16542 push_nested_class (ctx);
16545 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
16547 if (VAR_P (pattern))
16549 /* We need to determine if we're using a partial or explicit
16550 specialization now, because the type of the variable could be
16551 different. */
16552 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
16553 tree elt = most_specialized_partial_spec (tid, complain);
16554 if (elt == error_mark_node)
16555 pattern = error_mark_node;
16556 else if (elt)
16558 tmpl = TREE_VALUE (elt);
16559 pattern = DECL_TEMPLATE_RESULT (tmpl);
16560 targ_ptr = TREE_PURPOSE (elt);
16564 /* Substitute template parameters to obtain the specialization. */
16565 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
16566 if (DECL_CLASS_SCOPE_P (gen_tmpl))
16567 pop_nested_class ();
16568 pop_from_top_level ();
16570 if (fndecl == error_mark_node)
16572 pop_deferring_access_checks ();
16573 return error_mark_node;
16576 /* The DECL_TI_TEMPLATE should always be the immediate parent
16577 template, not the most general template. */
16578 DECL_TI_TEMPLATE (fndecl) = tmpl;
16580 /* Now we know the specialization, compute access previously
16581 deferred. */
16582 push_access_scope (fndecl);
16583 if (!perform_deferred_access_checks (complain))
16584 access_ok = false;
16585 pop_access_scope (fndecl);
16586 pop_deferring_access_checks ();
16588 /* If we've just instantiated the main entry point for a function,
16589 instantiate all the alternate entry points as well. We do this
16590 by cloning the instantiation of the main entry point, not by
16591 instantiating the template clones. */
16592 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
16593 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
16595 if (!access_ok)
16597 if (!(complain & tf_error))
16599 /* Remember to reinstantiate when we're out of SFINAE so the user
16600 can see the errors. */
16601 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
16603 return error_mark_node;
16605 return fndecl;
16608 /* Wrapper for instantiate_template_1. */
16610 tree
16611 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
16613 tree ret;
16614 timevar_push (TV_TEMPLATE_INST);
16615 ret = instantiate_template_1 (tmpl, orig_args, complain);
16616 timevar_pop (TV_TEMPLATE_INST);
16617 return ret;
16620 /* Instantiate the alias template TMPL with ARGS. Also push a template
16621 instantiation level, which instantiate_template doesn't do because
16622 functions and variables have sufficient context established by the
16623 callers. */
16625 static tree
16626 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
16628 struct pending_template *old_last_pend = last_pending_template;
16629 struct tinst_level *old_error_tinst = last_error_tinst_level;
16630 if (tmpl == error_mark_node || args == error_mark_node)
16631 return error_mark_node;
16632 tree tinst = build_tree_list (tmpl, args);
16633 if (!push_tinst_level (tinst))
16635 ggc_free (tinst);
16636 return error_mark_node;
16639 args =
16640 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
16641 args, tmpl, complain,
16642 /*require_all_args=*/true,
16643 /*use_default_args=*/true);
16645 tree r = instantiate_template (tmpl, args, complain);
16646 pop_tinst_level ();
16647 /* We can't free this if a pending_template entry or last_error_tinst_level
16648 is pointing at it. */
16649 if (last_pending_template == old_last_pend
16650 && last_error_tinst_level == old_error_tinst) {
16651 ggc_free (tinst);
16654 return r;
16657 /* PARM is a template parameter pack for FN. Returns true iff
16658 PARM is used in a deducible way in the argument list of FN. */
16660 static bool
16661 pack_deducible_p (tree parm, tree fn)
16663 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
16664 for (; t; t = TREE_CHAIN (t))
16666 tree type = TREE_VALUE (t);
16667 tree packs;
16668 if (!PACK_EXPANSION_P (type))
16669 continue;
16670 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
16671 packs; packs = TREE_CHAIN (packs))
16672 if (template_args_equal (TREE_VALUE (packs), parm))
16674 /* The template parameter pack is used in a function parameter
16675 pack. If this is the end of the parameter list, the
16676 template parameter pack is deducible. */
16677 if (TREE_CHAIN (t) == void_list_node)
16678 return true;
16679 else
16680 /* Otherwise, not. Well, it could be deduced from
16681 a non-pack parameter, but doing so would end up with
16682 a deduction mismatch, so don't bother. */
16683 return false;
16686 /* The template parameter pack isn't used in any function parameter
16687 packs, but it might be used deeper, e.g. tuple<Args...>. */
16688 return true;
16691 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
16692 NARGS elements of the arguments that are being used when calling
16693 it. TARGS is a vector into which the deduced template arguments
16694 are placed.
16696 Returns either a FUNCTION_DECL for the matching specialization of FN or
16697 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
16698 true, diagnostics will be printed to explain why it failed.
16700 If FN is a conversion operator, or we are trying to produce a specific
16701 specialization, RETURN_TYPE is the return type desired.
16703 The EXPLICIT_TARGS are explicit template arguments provided via a
16704 template-id.
16706 The parameter STRICT is one of:
16708 DEDUCE_CALL:
16709 We are deducing arguments for a function call, as in
16710 [temp.deduct.call].
16712 DEDUCE_CONV:
16713 We are deducing arguments for a conversion function, as in
16714 [temp.deduct.conv].
16716 DEDUCE_EXACT:
16717 We are deducing arguments when doing an explicit instantiation
16718 as in [temp.explicit], when determining an explicit specialization
16719 as in [temp.expl.spec], or when taking the address of a function
16720 template, as in [temp.deduct.funcaddr]. */
16722 tree
16723 fn_type_unification (tree fn,
16724 tree explicit_targs,
16725 tree targs,
16726 const tree *args,
16727 unsigned int nargs,
16728 tree return_type,
16729 unification_kind_t strict,
16730 int flags,
16731 bool explain_p,
16732 bool decltype_p)
16734 tree parms;
16735 tree fntype;
16736 tree decl = NULL_TREE;
16737 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
16738 bool ok;
16739 static int deduction_depth;
16740 struct pending_template *old_last_pend = last_pending_template;
16741 struct tinst_level *old_error_tinst = last_error_tinst_level;
16742 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
16743 tree tinst;
16744 tree r = error_mark_node;
16746 if (decltype_p)
16747 complain |= tf_decltype;
16749 /* In C++0x, it's possible to have a function template whose type depends
16750 on itself recursively. This is most obvious with decltype, but can also
16751 occur with enumeration scope (c++/48969). So we need to catch infinite
16752 recursion and reject the substitution at deduction time; this function
16753 will return error_mark_node for any repeated substitution.
16755 This also catches excessive recursion such as when f<N> depends on
16756 f<N-1> across all integers, and returns error_mark_node for all the
16757 substitutions back up to the initial one.
16759 This is, of course, not reentrant. */
16760 if (excessive_deduction_depth)
16761 return error_mark_node;
16762 tinst = build_tree_list (fn, NULL_TREE);
16763 ++deduction_depth;
16765 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
16767 fntype = TREE_TYPE (fn);
16768 if (explicit_targs)
16770 /* [temp.deduct]
16772 The specified template arguments must match the template
16773 parameters in kind (i.e., type, nontype, template), and there
16774 must not be more arguments than there are parameters;
16775 otherwise type deduction fails.
16777 Nontype arguments must match the types of the corresponding
16778 nontype template parameters, or must be convertible to the
16779 types of the corresponding nontype parameters as specified in
16780 _temp.arg.nontype_, otherwise type deduction fails.
16782 All references in the function type of the function template
16783 to the corresponding template parameters are replaced by the
16784 specified template argument values. If a substitution in a
16785 template parameter or in the function type of the function
16786 template results in an invalid type, type deduction fails. */
16787 int i, len = TREE_VEC_LENGTH (tparms);
16788 location_t loc = input_location;
16789 bool incomplete = false;
16791 /* Adjust any explicit template arguments before entering the
16792 substitution context. */
16793 explicit_targs
16794 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
16795 complain,
16796 /*require_all_args=*/false,
16797 /*use_default_args=*/false));
16798 if (explicit_targs == error_mark_node)
16799 goto fail;
16801 /* Substitute the explicit args into the function type. This is
16802 necessary so that, for instance, explicitly declared function
16803 arguments can match null pointed constants. If we were given
16804 an incomplete set of explicit args, we must not do semantic
16805 processing during substitution as we could create partial
16806 instantiations. */
16807 for (i = 0; i < len; i++)
16809 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16810 bool parameter_pack = false;
16811 tree targ = TREE_VEC_ELT (explicit_targs, i);
16813 /* Dig out the actual parm. */
16814 if (TREE_CODE (parm) == TYPE_DECL
16815 || TREE_CODE (parm) == TEMPLATE_DECL)
16817 parm = TREE_TYPE (parm);
16818 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
16820 else if (TREE_CODE (parm) == PARM_DECL)
16822 parm = DECL_INITIAL (parm);
16823 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
16826 if (!parameter_pack && targ == NULL_TREE)
16827 /* No explicit argument for this template parameter. */
16828 incomplete = true;
16830 if (parameter_pack && pack_deducible_p (parm, fn))
16832 /* Mark the argument pack as "incomplete". We could
16833 still deduce more arguments during unification.
16834 We remove this mark in type_unification_real. */
16835 if (targ)
16837 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
16838 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
16839 = ARGUMENT_PACK_ARGS (targ);
16842 /* We have some incomplete argument packs. */
16843 incomplete = true;
16847 TREE_VALUE (tinst) = explicit_targs;
16848 if (!push_tinst_level (tinst))
16850 excessive_deduction_depth = true;
16851 goto fail;
16853 processing_template_decl += incomplete;
16854 input_location = DECL_SOURCE_LOCATION (fn);
16855 /* Ignore any access checks; we'll see them again in
16856 instantiate_template and they might have the wrong
16857 access path at this point. */
16858 push_deferring_access_checks (dk_deferred);
16859 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
16860 complain | tf_partial, NULL_TREE);
16861 pop_deferring_access_checks ();
16862 input_location = loc;
16863 processing_template_decl -= incomplete;
16864 pop_tinst_level ();
16866 if (fntype == error_mark_node)
16867 goto fail;
16869 /* Place the explicitly specified arguments in TARGS. */
16870 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
16871 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
16874 /* Never do unification on the 'this' parameter. */
16875 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
16877 if (return_type)
16879 tree *new_args;
16881 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
16882 new_args = XALLOCAVEC (tree, nargs + 1);
16883 new_args[0] = return_type;
16884 memcpy (new_args + 1, args, nargs * sizeof (tree));
16885 args = new_args;
16886 ++nargs;
16889 /* We allow incomplete unification without an error message here
16890 because the standard doesn't seem to explicitly prohibit it. Our
16891 callers must be ready to deal with unification failures in any
16892 event. */
16894 TREE_VALUE (tinst) = targs;
16895 /* If we aren't explaining yet, push tinst context so we can see where
16896 any errors (e.g. from class instantiations triggered by instantiation
16897 of default template arguments) come from. If we are explaining, this
16898 context is redundant. */
16899 if (!explain_p && !push_tinst_level (tinst))
16901 excessive_deduction_depth = true;
16902 goto fail;
16905 /* type_unification_real will pass back any access checks from default
16906 template argument substitution. */
16907 vec<deferred_access_check, va_gc> *checks;
16908 checks = NULL;
16910 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16911 targs, parms, args, nargs, /*subr=*/0,
16912 strict, flags, &checks, explain_p);
16913 if (!explain_p)
16914 pop_tinst_level ();
16915 if (!ok)
16916 goto fail;
16918 /* Now that we have bindings for all of the template arguments,
16919 ensure that the arguments deduced for the template template
16920 parameters have compatible template parameter lists. We cannot
16921 check this property before we have deduced all template
16922 arguments, because the template parameter types of a template
16923 template parameter might depend on prior template parameters
16924 deduced after the template template parameter. The following
16925 ill-formed example illustrates this issue:
16927 template<typename T, template<T> class C> void f(C<5>, T);
16929 template<int N> struct X {};
16931 void g() {
16932 f(X<5>(), 5l); // error: template argument deduction fails
16935 The template parameter list of 'C' depends on the template type
16936 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
16937 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
16938 time that we deduce 'C'. */
16939 if (!template_template_parm_bindings_ok_p
16940 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
16942 unify_inconsistent_template_template_parameters (explain_p);
16943 goto fail;
16946 /* All is well so far. Now, check:
16948 [temp.deduct]
16950 When all template arguments have been deduced, all uses of
16951 template parameters in nondeduced contexts are replaced with
16952 the corresponding deduced argument values. If the
16953 substitution results in an invalid type, as described above,
16954 type deduction fails. */
16955 TREE_VALUE (tinst) = targs;
16956 if (!push_tinst_level (tinst))
16958 excessive_deduction_depth = true;
16959 goto fail;
16962 /* Also collect access checks from the instantiation. */
16963 reopen_deferring_access_checks (checks);
16965 decl = instantiate_template (fn, targs, complain);
16967 checks = get_deferred_access_checks ();
16968 pop_deferring_access_checks ();
16970 pop_tinst_level ();
16972 if (decl == error_mark_node)
16973 goto fail;
16975 /* Now perform any access checks encountered during substitution. */
16976 push_access_scope (decl);
16977 ok = perform_access_checks (checks, complain);
16978 pop_access_scope (decl);
16979 if (!ok)
16980 goto fail;
16982 /* If we're looking for an exact match, check that what we got
16983 is indeed an exact match. It might not be if some template
16984 parameters are used in non-deduced contexts. But don't check
16985 for an exact match if we have dependent template arguments;
16986 in that case we're doing partial ordering, and we already know
16987 that we have two candidates that will provide the actual type. */
16988 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
16990 tree substed = TREE_TYPE (decl);
16991 unsigned int i;
16993 tree sarg
16994 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
16995 if (return_type)
16996 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
16997 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
16998 if (!same_type_p (args[i], TREE_VALUE (sarg)))
17000 unify_type_mismatch (explain_p, args[i],
17001 TREE_VALUE (sarg));
17002 goto fail;
17006 r = decl;
17008 fail:
17009 --deduction_depth;
17010 if (excessive_deduction_depth)
17012 if (deduction_depth == 0)
17013 /* Reset once we're all the way out. */
17014 excessive_deduction_depth = false;
17017 /* We can't free this if a pending_template entry or last_error_tinst_level
17018 is pointing at it. */
17019 if (last_pending_template == old_last_pend
17020 && last_error_tinst_level == old_error_tinst) {
17021 ggc_free (tinst);
17024 return r;
17027 /* Adjust types before performing type deduction, as described in
17028 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
17029 sections are symmetric. PARM is the type of a function parameter
17030 or the return type of the conversion function. ARG is the type of
17031 the argument passed to the call, or the type of the value
17032 initialized with the result of the conversion function.
17033 ARG_EXPR is the original argument expression, which may be null. */
17035 static int
17036 maybe_adjust_types_for_deduction (unification_kind_t strict,
17037 tree* parm,
17038 tree* arg,
17039 tree arg_expr)
17041 int result = 0;
17043 switch (strict)
17045 case DEDUCE_CALL:
17046 break;
17048 case DEDUCE_CONV:
17050 /* Swap PARM and ARG throughout the remainder of this
17051 function; the handling is precisely symmetric since PARM
17052 will initialize ARG rather than vice versa. */
17053 tree* temp = parm;
17054 parm = arg;
17055 arg = temp;
17056 break;
17059 case DEDUCE_EXACT:
17060 /* Core issue #873: Do the DR606 thing (see below) for these cases,
17061 too, but here handle it by stripping the reference from PARM
17062 rather than by adding it to ARG. */
17063 if (TREE_CODE (*parm) == REFERENCE_TYPE
17064 && TYPE_REF_IS_RVALUE (*parm)
17065 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
17066 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
17067 && TREE_CODE (*arg) == REFERENCE_TYPE
17068 && !TYPE_REF_IS_RVALUE (*arg))
17069 *parm = TREE_TYPE (*parm);
17070 /* Nothing else to do in this case. */
17071 return 0;
17073 default:
17074 gcc_unreachable ();
17077 if (TREE_CODE (*parm) != REFERENCE_TYPE)
17079 /* [temp.deduct.call]
17081 If P is not a reference type:
17083 --If A is an array type, the pointer type produced by the
17084 array-to-pointer standard conversion (_conv.array_) is
17085 used in place of A for type deduction; otherwise,
17087 --If A is a function type, the pointer type produced by
17088 the function-to-pointer standard conversion
17089 (_conv.func_) is used in place of A for type deduction;
17090 otherwise,
17092 --If A is a cv-qualified type, the top level
17093 cv-qualifiers of A's type are ignored for type
17094 deduction. */
17095 if (TREE_CODE (*arg) == ARRAY_TYPE)
17096 *arg = build_pointer_type (TREE_TYPE (*arg));
17097 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
17098 *arg = build_pointer_type (*arg);
17099 else
17100 *arg = TYPE_MAIN_VARIANT (*arg);
17103 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
17104 of the form T&&, where T is a template parameter, and the argument
17105 is an lvalue, T is deduced as A& */
17106 if (TREE_CODE (*parm) == REFERENCE_TYPE
17107 && TYPE_REF_IS_RVALUE (*parm)
17108 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
17109 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
17110 && (arg_expr ? real_lvalue_p (arg_expr)
17111 /* try_one_overload doesn't provide an arg_expr, but
17112 functions are always lvalues. */
17113 : TREE_CODE (*arg) == FUNCTION_TYPE))
17114 *arg = build_reference_type (*arg);
17116 /* [temp.deduct.call]
17118 If P is a cv-qualified type, the top level cv-qualifiers
17119 of P's type are ignored for type deduction. If P is a
17120 reference type, the type referred to by P is used for
17121 type deduction. */
17122 *parm = TYPE_MAIN_VARIANT (*parm);
17123 if (TREE_CODE (*parm) == REFERENCE_TYPE)
17125 *parm = TREE_TYPE (*parm);
17126 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17129 /* DR 322. For conversion deduction, remove a reference type on parm
17130 too (which has been swapped into ARG). */
17131 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
17132 *arg = TREE_TYPE (*arg);
17134 return result;
17137 /* Subroutine of unify_one_argument. PARM is a function parameter of a
17138 template which does contain any deducible template parameters; check if
17139 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
17140 unify_one_argument. */
17142 static int
17143 check_non_deducible_conversion (tree parm, tree arg, int strict,
17144 int flags, bool explain_p)
17146 tree type;
17148 if (!TYPE_P (arg))
17149 type = TREE_TYPE (arg);
17150 else
17151 type = arg;
17153 if (same_type_p (parm, type))
17154 return unify_success (explain_p);
17156 if (strict == DEDUCE_CONV)
17158 if (can_convert_arg (type, parm, NULL_TREE, flags,
17159 explain_p ? tf_warning_or_error : tf_none))
17160 return unify_success (explain_p);
17162 else if (strict != DEDUCE_EXACT)
17164 if (can_convert_arg (parm, type,
17165 TYPE_P (arg) ? NULL_TREE : arg,
17166 flags, explain_p ? tf_warning_or_error : tf_none))
17167 return unify_success (explain_p);
17170 if (strict == DEDUCE_EXACT)
17171 return unify_type_mismatch (explain_p, parm, arg);
17172 else
17173 return unify_arg_conversion (explain_p, parm, type, arg);
17176 static bool uses_deducible_template_parms (tree type);
17178 /* Returns true iff the expression EXPR is one from which a template
17179 argument can be deduced. In other words, if it's an undecorated
17180 use of a template non-type parameter. */
17182 static bool
17183 deducible_expression (tree expr)
17185 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
17188 /* Returns true iff the array domain DOMAIN uses a template parameter in a
17189 deducible way; that is, if it has a max value of <PARM> - 1. */
17191 static bool
17192 deducible_array_bound (tree domain)
17194 if (domain == NULL_TREE)
17195 return false;
17197 tree max = TYPE_MAX_VALUE (domain);
17198 if (TREE_CODE (max) != MINUS_EXPR)
17199 return false;
17201 return deducible_expression (TREE_OPERAND (max, 0));
17204 /* Returns true iff the template arguments ARGS use a template parameter
17205 in a deducible way. */
17207 static bool
17208 deducible_template_args (tree args)
17210 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
17212 bool deducible;
17213 tree elt = TREE_VEC_ELT (args, i);
17214 if (ARGUMENT_PACK_P (elt))
17215 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
17216 else
17218 if (PACK_EXPANSION_P (elt))
17219 elt = PACK_EXPANSION_PATTERN (elt);
17220 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
17221 deducible = true;
17222 else if (TYPE_P (elt))
17223 deducible = uses_deducible_template_parms (elt);
17224 else
17225 deducible = deducible_expression (elt);
17227 if (deducible)
17228 return true;
17230 return false;
17233 /* Returns true iff TYPE contains any deducible references to template
17234 parameters, as per 14.8.2.5. */
17236 static bool
17237 uses_deducible_template_parms (tree type)
17239 if (PACK_EXPANSION_P (type))
17240 type = PACK_EXPANSION_PATTERN (type);
17242 /* T
17243 cv-list T
17244 TT<T>
17245 TT<i>
17246 TT<> */
17247 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17248 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17249 return true;
17251 /* T*
17253 T&& */
17254 if (POINTER_TYPE_P (type))
17255 return uses_deducible_template_parms (TREE_TYPE (type));
17257 /* T[integer-constant ]
17258 type [i] */
17259 if (TREE_CODE (type) == ARRAY_TYPE)
17260 return (uses_deducible_template_parms (TREE_TYPE (type))
17261 || deducible_array_bound (TYPE_DOMAIN (type)));
17263 /* T type ::*
17264 type T::*
17265 T T::*
17266 T (type ::*)()
17267 type (T::*)()
17268 type (type ::*)(T)
17269 type (T::*)(T)
17270 T (type ::*)(T)
17271 T (T::*)()
17272 T (T::*)(T) */
17273 if (TYPE_PTRMEM_P (type))
17274 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
17275 || (uses_deducible_template_parms
17276 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
17278 /* template-name <T> (where template-name refers to a class template)
17279 template-name <i> (where template-name refers to a class template) */
17280 if (CLASS_TYPE_P (type)
17281 && CLASSTYPE_TEMPLATE_INFO (type)
17282 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
17283 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
17284 (CLASSTYPE_TI_ARGS (type)));
17286 /* type (T)
17288 T(T) */
17289 if (TREE_CODE (type) == FUNCTION_TYPE
17290 || TREE_CODE (type) == METHOD_TYPE)
17292 if (uses_deducible_template_parms (TREE_TYPE (type)))
17293 return true;
17294 tree parm = TYPE_ARG_TYPES (type);
17295 if (TREE_CODE (type) == METHOD_TYPE)
17296 parm = TREE_CHAIN (parm);
17297 for (; parm; parm = TREE_CHAIN (parm))
17298 if (uses_deducible_template_parms (TREE_VALUE (parm)))
17299 return true;
17302 return false;
17305 /* Subroutine of type_unification_real and unify_pack_expansion to
17306 handle unification of a single P/A pair. Parameters are as
17307 for those functions. */
17309 static int
17310 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
17311 int subr, unification_kind_t strict, int flags,
17312 bool explain_p)
17314 tree arg_expr = NULL_TREE;
17315 int arg_strict;
17317 if (arg == error_mark_node || parm == error_mark_node)
17318 return unify_invalid (explain_p);
17319 if (arg == unknown_type_node)
17320 /* We can't deduce anything from this, but we might get all the
17321 template args from other function args. */
17322 return unify_success (explain_p);
17324 /* Implicit conversions (Clause 4) will be performed on a function
17325 argument to convert it to the type of the corresponding function
17326 parameter if the parameter type contains no template-parameters that
17327 participate in template argument deduction. */
17328 if (TYPE_P (parm) && !uses_template_parms (parm))
17329 /* For function parameters that contain no template-parameters at all,
17330 we have historically checked for convertibility in order to shortcut
17331 consideration of this candidate. */
17332 return check_non_deducible_conversion (parm, arg, strict, flags,
17333 explain_p);
17334 else if (strict == DEDUCE_CALL
17335 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
17336 /* For function parameters with only non-deducible template parameters,
17337 just return. */
17338 return unify_success (explain_p);
17340 switch (strict)
17342 case DEDUCE_CALL:
17343 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
17344 | UNIFY_ALLOW_MORE_CV_QUAL
17345 | UNIFY_ALLOW_DERIVED);
17346 break;
17348 case DEDUCE_CONV:
17349 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
17350 break;
17352 case DEDUCE_EXACT:
17353 arg_strict = UNIFY_ALLOW_NONE;
17354 break;
17356 default:
17357 gcc_unreachable ();
17360 /* We only do these transformations if this is the top-level
17361 parameter_type_list in a call or declaration matching; in other
17362 situations (nested function declarators, template argument lists) we
17363 won't be comparing a type to an expression, and we don't do any type
17364 adjustments. */
17365 if (!subr)
17367 if (!TYPE_P (arg))
17369 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
17370 if (type_unknown_p (arg))
17372 /* [temp.deduct.type] A template-argument can be
17373 deduced from a pointer to function or pointer
17374 to member function argument if the set of
17375 overloaded functions does not contain function
17376 templates and at most one of a set of
17377 overloaded functions provides a unique
17378 match. */
17380 if (resolve_overloaded_unification
17381 (tparms, targs, parm, arg, strict,
17382 arg_strict, explain_p))
17383 return unify_success (explain_p);
17384 return unify_overload_resolution_failure (explain_p, arg);
17387 arg_expr = arg;
17388 arg = unlowered_expr_type (arg);
17389 if (arg == error_mark_node)
17390 return unify_invalid (explain_p);
17393 arg_strict |=
17394 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
17396 else
17397 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
17398 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
17399 return unify_template_argument_mismatch (explain_p, parm, arg);
17401 /* For deduction from an init-list we need the actual list. */
17402 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
17403 arg = arg_expr;
17404 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
17407 /* Most parms like fn_type_unification.
17409 If SUBR is 1, we're being called recursively (to unify the
17410 arguments of a function or method parameter of a function
17411 template).
17413 CHECKS is a pointer to a vector of access checks encountered while
17414 substituting default template arguments. */
17416 static int
17417 type_unification_real (tree tparms,
17418 tree targs,
17419 tree xparms,
17420 const tree *xargs,
17421 unsigned int xnargs,
17422 int subr,
17423 unification_kind_t strict,
17424 int flags,
17425 vec<deferred_access_check, va_gc> **checks,
17426 bool explain_p)
17428 tree parm, arg;
17429 int i;
17430 int ntparms = TREE_VEC_LENGTH (tparms);
17431 int saw_undeduced = 0;
17432 tree parms;
17433 const tree *args;
17434 unsigned int nargs;
17435 unsigned int ia;
17437 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
17438 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
17439 gcc_assert (ntparms > 0);
17441 /* Reset the number of non-defaulted template arguments contained
17442 in TARGS. */
17443 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
17445 again:
17446 parms = xparms;
17447 args = xargs;
17448 nargs = xnargs;
17450 ia = 0;
17451 while (parms && parms != void_list_node
17452 && ia < nargs)
17454 parm = TREE_VALUE (parms);
17456 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
17457 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
17458 /* For a function parameter pack that occurs at the end of the
17459 parameter-declaration-list, the type A of each remaining
17460 argument of the call is compared with the type P of the
17461 declarator-id of the function parameter pack. */
17462 break;
17464 parms = TREE_CHAIN (parms);
17466 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
17467 /* For a function parameter pack that does not occur at the
17468 end of the parameter-declaration-list, the type of the
17469 parameter pack is a non-deduced context. */
17470 continue;
17472 arg = args[ia];
17473 ++ia;
17475 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
17476 flags, explain_p))
17477 return 1;
17480 if (parms
17481 && parms != void_list_node
17482 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
17484 /* Unify the remaining arguments with the pack expansion type. */
17485 tree argvec;
17486 tree parmvec = make_tree_vec (1);
17488 /* Allocate a TREE_VEC and copy in all of the arguments */
17489 argvec = make_tree_vec (nargs - ia);
17490 for (i = 0; ia < nargs; ++ia, ++i)
17491 TREE_VEC_ELT (argvec, i) = args[ia];
17493 /* Copy the parameter into parmvec. */
17494 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
17495 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
17496 /*subr=*/subr, explain_p))
17497 return 1;
17499 /* Advance to the end of the list of parameters. */
17500 parms = TREE_CHAIN (parms);
17503 /* Fail if we've reached the end of the parm list, and more args
17504 are present, and the parm list isn't variadic. */
17505 if (ia < nargs && parms == void_list_node)
17506 return unify_too_many_arguments (explain_p, nargs, ia);
17507 /* Fail if parms are left and they don't have default values and
17508 they aren't all deduced as empty packs (c++/57397). This is
17509 consistent with sufficient_parms_p. */
17510 if (parms && parms != void_list_node
17511 && TREE_PURPOSE (parms) == NULL_TREE)
17513 unsigned int count = nargs;
17514 tree p = parms;
17515 bool type_pack_p;
17518 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
17519 if (!type_pack_p)
17520 count++;
17521 p = TREE_CHAIN (p);
17523 while (p && p != void_list_node);
17524 if (count != nargs)
17525 return unify_too_few_arguments (explain_p, ia, count,
17526 type_pack_p);
17529 if (!subr)
17531 tsubst_flags_t complain = (explain_p
17532 ? tf_warning_or_error
17533 : tf_none);
17535 for (i = 0; i < ntparms; i++)
17537 tree targ = TREE_VEC_ELT (targs, i);
17538 tree tparm = TREE_VEC_ELT (tparms, i);
17540 /* Clear the "incomplete" flags on all argument packs now so that
17541 substituting them into later default arguments works. */
17542 if (targ && ARGUMENT_PACK_P (targ))
17544 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
17545 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
17548 if (targ || tparm == error_mark_node)
17549 continue;
17550 tparm = TREE_VALUE (tparm);
17552 /* If this is an undeduced nontype parameter that depends on
17553 a type parameter, try another pass; its type may have been
17554 deduced from a later argument than the one from which
17555 this parameter can be deduced. */
17556 if (TREE_CODE (tparm) == PARM_DECL
17557 && uses_template_parms (TREE_TYPE (tparm))
17558 && !saw_undeduced++)
17559 goto again;
17561 /* Core issue #226 (C++0x) [temp.deduct]:
17563 If a template argument has not been deduced, its
17564 default template argument, if any, is used.
17566 When we are in C++98 mode, TREE_PURPOSE will either
17567 be NULL_TREE or ERROR_MARK_NODE, so we do not need
17568 to explicitly check cxx_dialect here. */
17569 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
17571 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
17572 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
17573 reopen_deferring_access_checks (*checks);
17574 location_t save_loc = input_location;
17575 if (DECL_P (parm))
17576 input_location = DECL_SOURCE_LOCATION (parm);
17577 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
17578 arg = convert_template_argument (parm, arg, targs, complain,
17579 i, NULL_TREE);
17580 input_location = save_loc;
17581 *checks = get_deferred_access_checks ();
17582 pop_deferring_access_checks ();
17583 if (arg == error_mark_node)
17584 return 1;
17585 else
17587 TREE_VEC_ELT (targs, i) = arg;
17588 /* The position of the first default template argument,
17589 is also the number of non-defaulted arguments in TARGS.
17590 Record that. */
17591 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
17592 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
17593 continue;
17597 /* If the type parameter is a parameter pack, then it will
17598 be deduced to an empty parameter pack. */
17599 if (template_parameter_pack_p (tparm))
17601 tree arg;
17603 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
17605 arg = make_node (NONTYPE_ARGUMENT_PACK);
17606 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
17607 TREE_CONSTANT (arg) = 1;
17609 else
17610 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
17612 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
17614 TREE_VEC_ELT (targs, i) = arg;
17615 continue;
17618 return unify_parameter_deduction_failure (explain_p, tparm);
17621 #ifdef ENABLE_CHECKING
17622 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
17623 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
17624 #endif
17626 return unify_success (explain_p);
17629 /* Subroutine of type_unification_real. Args are like the variables
17630 at the call site. ARG is an overloaded function (or template-id);
17631 we try deducing template args from each of the overloads, and if
17632 only one succeeds, we go with that. Modifies TARGS and returns
17633 true on success. */
17635 static bool
17636 resolve_overloaded_unification (tree tparms,
17637 tree targs,
17638 tree parm,
17639 tree arg,
17640 unification_kind_t strict,
17641 int sub_strict,
17642 bool explain_p)
17644 tree tempargs = copy_node (targs);
17645 int good = 0;
17646 tree goodfn = NULL_TREE;
17647 bool addr_p;
17649 if (TREE_CODE (arg) == ADDR_EXPR)
17651 arg = TREE_OPERAND (arg, 0);
17652 addr_p = true;
17654 else
17655 addr_p = false;
17657 if (TREE_CODE (arg) == COMPONENT_REF)
17658 /* Handle `&x' where `x' is some static or non-static member
17659 function name. */
17660 arg = TREE_OPERAND (arg, 1);
17662 if (TREE_CODE (arg) == OFFSET_REF)
17663 arg = TREE_OPERAND (arg, 1);
17665 /* Strip baselink information. */
17666 if (BASELINK_P (arg))
17667 arg = BASELINK_FUNCTIONS (arg);
17669 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
17671 /* If we got some explicit template args, we need to plug them into
17672 the affected templates before we try to unify, in case the
17673 explicit args will completely resolve the templates in question. */
17675 int ok = 0;
17676 tree expl_subargs = TREE_OPERAND (arg, 1);
17677 arg = TREE_OPERAND (arg, 0);
17679 for (; arg; arg = OVL_NEXT (arg))
17681 tree fn = OVL_CURRENT (arg);
17682 tree subargs, elem;
17684 if (TREE_CODE (fn) != TEMPLATE_DECL)
17685 continue;
17687 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17688 expl_subargs, NULL_TREE, tf_none,
17689 /*require_all_args=*/true,
17690 /*use_default_args=*/true);
17691 if (subargs != error_mark_node
17692 && !any_dependent_template_arguments_p (subargs))
17694 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
17695 if (try_one_overload (tparms, targs, tempargs, parm,
17696 elem, strict, sub_strict, addr_p, explain_p)
17697 && (!goodfn || !same_type_p (goodfn, elem)))
17699 goodfn = elem;
17700 ++good;
17703 else if (subargs)
17704 ++ok;
17706 /* If no templates (or more than one) are fully resolved by the
17707 explicit arguments, this template-id is a non-deduced context; it
17708 could still be OK if we deduce all template arguments for the
17709 enclosing call through other arguments. */
17710 if (good != 1)
17711 good = ok;
17713 else if (TREE_CODE (arg) != OVERLOAD
17714 && TREE_CODE (arg) != FUNCTION_DECL)
17715 /* If ARG is, for example, "(0, &f)" then its type will be unknown
17716 -- but the deduction does not succeed because the expression is
17717 not just the function on its own. */
17718 return false;
17719 else
17720 for (; arg; arg = OVL_NEXT (arg))
17721 if (try_one_overload (tparms, targs, tempargs, parm,
17722 TREE_TYPE (OVL_CURRENT (arg)),
17723 strict, sub_strict, addr_p, explain_p)
17724 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
17726 goodfn = OVL_CURRENT (arg);
17727 ++good;
17730 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17731 to function or pointer to member function argument if the set of
17732 overloaded functions does not contain function templates and at most
17733 one of a set of overloaded functions provides a unique match.
17735 So if we found multiple possibilities, we return success but don't
17736 deduce anything. */
17738 if (good == 1)
17740 int i = TREE_VEC_LENGTH (targs);
17741 for (; i--; )
17742 if (TREE_VEC_ELT (tempargs, i))
17744 tree old = TREE_VEC_ELT (targs, i);
17745 tree new_ = TREE_VEC_ELT (tempargs, i);
17746 if (new_ && old && ARGUMENT_PACK_P (old)
17747 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
17748 /* Don't forget explicit template arguments in a pack. */
17749 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
17750 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
17751 TREE_VEC_ELT (targs, i) = new_;
17754 if (good)
17755 return true;
17757 return false;
17760 /* Core DR 115: In contexts where deduction is done and fails, or in
17761 contexts where deduction is not done, if a template argument list is
17762 specified and it, along with any default template arguments, identifies
17763 a single function template specialization, then the template-id is an
17764 lvalue for the function template specialization. */
17766 tree
17767 resolve_nondeduced_context (tree orig_expr)
17769 tree expr, offset, baselink;
17770 bool addr;
17772 if (!type_unknown_p (orig_expr))
17773 return orig_expr;
17775 expr = orig_expr;
17776 addr = false;
17777 offset = NULL_TREE;
17778 baselink = NULL_TREE;
17780 if (TREE_CODE (expr) == ADDR_EXPR)
17782 expr = TREE_OPERAND (expr, 0);
17783 addr = true;
17785 if (TREE_CODE (expr) == OFFSET_REF)
17787 offset = expr;
17788 expr = TREE_OPERAND (expr, 1);
17790 if (BASELINK_P (expr))
17792 baselink = expr;
17793 expr = BASELINK_FUNCTIONS (expr);
17796 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
17798 int good = 0;
17799 tree goodfn = NULL_TREE;
17801 /* If we got some explicit template args, we need to plug them into
17802 the affected templates before we try to unify, in case the
17803 explicit args will completely resolve the templates in question. */
17805 tree expl_subargs = TREE_OPERAND (expr, 1);
17806 tree arg = TREE_OPERAND (expr, 0);
17807 tree badfn = NULL_TREE;
17808 tree badargs = NULL_TREE;
17810 for (; arg; arg = OVL_NEXT (arg))
17812 tree fn = OVL_CURRENT (arg);
17813 tree subargs, elem;
17815 if (TREE_CODE (fn) != TEMPLATE_DECL)
17816 continue;
17818 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17819 expl_subargs, NULL_TREE, tf_none,
17820 /*require_all_args=*/true,
17821 /*use_default_args=*/true);
17822 if (subargs != error_mark_node
17823 && !any_dependent_template_arguments_p (subargs))
17825 elem = instantiate_template (fn, subargs, tf_none);
17826 if (elem == error_mark_node)
17828 badfn = fn;
17829 badargs = subargs;
17831 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
17833 goodfn = elem;
17834 ++good;
17838 if (good == 1)
17840 mark_used (goodfn);
17841 expr = goodfn;
17842 if (baselink)
17843 expr = build_baselink (BASELINK_BINFO (baselink),
17844 BASELINK_ACCESS_BINFO (baselink),
17845 expr, BASELINK_OPTYPE (baselink));
17846 if (offset)
17848 tree base
17849 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
17850 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
17852 if (addr)
17853 expr = cp_build_addr_expr (expr, tf_warning_or_error);
17854 return expr;
17856 else if (good == 0 && badargs)
17857 /* There were no good options and at least one bad one, so let the
17858 user know what the problem is. */
17859 instantiate_template (badfn, badargs, tf_warning_or_error);
17861 return orig_expr;
17864 /* Subroutine of resolve_overloaded_unification; does deduction for a single
17865 overload. Fills TARGS with any deduced arguments, or error_mark_node if
17866 different overloads deduce different arguments for a given parm.
17867 ADDR_P is true if the expression for which deduction is being
17868 performed was of the form "& fn" rather than simply "fn".
17870 Returns 1 on success. */
17872 static int
17873 try_one_overload (tree tparms,
17874 tree orig_targs,
17875 tree targs,
17876 tree parm,
17877 tree arg,
17878 unification_kind_t strict,
17879 int sub_strict,
17880 bool addr_p,
17881 bool explain_p)
17883 int nargs;
17884 tree tempargs;
17885 int i;
17887 if (arg == error_mark_node)
17888 return 0;
17890 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17891 to function or pointer to member function argument if the set of
17892 overloaded functions does not contain function templates and at most
17893 one of a set of overloaded functions provides a unique match.
17895 So if this is a template, just return success. */
17897 if (uses_template_parms (arg))
17898 return 1;
17900 if (TREE_CODE (arg) == METHOD_TYPE)
17901 arg = build_ptrmemfunc_type (build_pointer_type (arg));
17902 else if (addr_p)
17903 arg = build_pointer_type (arg);
17905 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
17907 /* We don't copy orig_targs for this because if we have already deduced
17908 some template args from previous args, unify would complain when we
17909 try to deduce a template parameter for the same argument, even though
17910 there isn't really a conflict. */
17911 nargs = TREE_VEC_LENGTH (targs);
17912 tempargs = make_tree_vec (nargs);
17914 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
17915 return 0;
17917 /* First make sure we didn't deduce anything that conflicts with
17918 explicitly specified args. */
17919 for (i = nargs; i--; )
17921 tree elt = TREE_VEC_ELT (tempargs, i);
17922 tree oldelt = TREE_VEC_ELT (orig_targs, i);
17924 if (!elt)
17925 /*NOP*/;
17926 else if (uses_template_parms (elt))
17927 /* Since we're unifying against ourselves, we will fill in
17928 template args used in the function parm list with our own
17929 template parms. Discard them. */
17930 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
17931 else if (oldelt && !template_args_equal (oldelt, elt))
17932 return 0;
17935 for (i = nargs; i--; )
17937 tree elt = TREE_VEC_ELT (tempargs, i);
17939 if (elt)
17940 TREE_VEC_ELT (targs, i) = elt;
17943 return 1;
17946 /* PARM is a template class (perhaps with unbound template
17947 parameters). ARG is a fully instantiated type. If ARG can be
17948 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
17949 TARGS are as for unify. */
17951 static tree
17952 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
17953 bool explain_p)
17955 tree copy_of_targs;
17957 if (!CLASSTYPE_TEMPLATE_INFO (arg)
17958 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
17959 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
17960 return NULL_TREE;
17962 /* We need to make a new template argument vector for the call to
17963 unify. If we used TARGS, we'd clutter it up with the result of
17964 the attempted unification, even if this class didn't work out.
17965 We also don't want to commit ourselves to all the unifications
17966 we've already done, since unification is supposed to be done on
17967 an argument-by-argument basis. In other words, consider the
17968 following pathological case:
17970 template <int I, int J, int K>
17971 struct S {};
17973 template <int I, int J>
17974 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
17976 template <int I, int J, int K>
17977 void f(S<I, J, K>, S<I, I, I>);
17979 void g() {
17980 S<0, 0, 0> s0;
17981 S<0, 1, 2> s2;
17983 f(s0, s2);
17986 Now, by the time we consider the unification involving `s2', we
17987 already know that we must have `f<0, 0, 0>'. But, even though
17988 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
17989 because there are two ways to unify base classes of S<0, 1, 2>
17990 with S<I, I, I>. If we kept the already deduced knowledge, we
17991 would reject the possibility I=1. */
17992 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
17994 /* If unification failed, we're done. */
17995 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
17996 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
17997 return NULL_TREE;
17999 return arg;
18002 /* Given a template type PARM and a class type ARG, find the unique
18003 base type in ARG that is an instance of PARM. We do not examine
18004 ARG itself; only its base-classes. If there is not exactly one
18005 appropriate base class, return NULL_TREE. PARM may be the type of
18006 a partial specialization, as well as a plain template type. Used
18007 by unify. */
18009 static enum template_base_result
18010 get_template_base (tree tparms, tree targs, tree parm, tree arg,
18011 bool explain_p, tree *result)
18013 tree rval = NULL_TREE;
18014 tree binfo;
18016 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
18018 binfo = TYPE_BINFO (complete_type (arg));
18019 if (!binfo)
18021 /* The type could not be completed. */
18022 *result = NULL_TREE;
18023 return tbr_incomplete_type;
18026 /* Walk in inheritance graph order. The search order is not
18027 important, and this avoids multiple walks of virtual bases. */
18028 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
18030 tree r = try_class_unification (tparms, targs, parm,
18031 BINFO_TYPE (binfo), explain_p);
18033 if (r)
18035 /* If there is more than one satisfactory baseclass, then:
18037 [temp.deduct.call]
18039 If they yield more than one possible deduced A, the type
18040 deduction fails.
18042 applies. */
18043 if (rval && !same_type_p (r, rval))
18045 *result = NULL_TREE;
18046 return tbr_ambiguous_baseclass;
18049 rval = r;
18053 *result = rval;
18054 return tbr_success;
18057 /* Returns the level of DECL, which declares a template parameter. */
18059 static int
18060 template_decl_level (tree decl)
18062 switch (TREE_CODE (decl))
18064 case TYPE_DECL:
18065 case TEMPLATE_DECL:
18066 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
18068 case PARM_DECL:
18069 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
18071 default:
18072 gcc_unreachable ();
18074 return 0;
18077 /* Decide whether ARG can be unified with PARM, considering only the
18078 cv-qualifiers of each type, given STRICT as documented for unify.
18079 Returns nonzero iff the unification is OK on that basis. */
18081 static int
18082 check_cv_quals_for_unify (int strict, tree arg, tree parm)
18084 int arg_quals = cp_type_quals (arg);
18085 int parm_quals = cp_type_quals (parm);
18087 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18088 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
18090 /* Although a CVR qualifier is ignored when being applied to a
18091 substituted template parameter ([8.3.2]/1 for example), that
18092 does not allow us to unify "const T" with "int&" because both
18093 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
18094 It is ok when we're allowing additional CV qualifiers
18095 at the outer level [14.8.2.1]/3,1st bullet. */
18096 if ((TREE_CODE (arg) == REFERENCE_TYPE
18097 || TREE_CODE (arg) == FUNCTION_TYPE
18098 || TREE_CODE (arg) == METHOD_TYPE)
18099 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
18100 return 0;
18102 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
18103 && (parm_quals & TYPE_QUAL_RESTRICT))
18104 return 0;
18107 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
18108 && (arg_quals & parm_quals) != parm_quals)
18109 return 0;
18111 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
18112 && (parm_quals & arg_quals) != arg_quals)
18113 return 0;
18115 return 1;
18118 /* Determines the LEVEL and INDEX for the template parameter PARM. */
18119 void
18120 template_parm_level_and_index (tree parm, int* level, int* index)
18122 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18123 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18124 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18126 *index = TEMPLATE_TYPE_IDX (parm);
18127 *level = TEMPLATE_TYPE_LEVEL (parm);
18129 else
18131 *index = TEMPLATE_PARM_IDX (parm);
18132 *level = TEMPLATE_PARM_LEVEL (parm);
18136 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
18137 do { \
18138 if (unify (TP, TA, P, A, S, EP)) \
18139 return 1; \
18140 } while (0);
18142 /* Unifies the remaining arguments in PACKED_ARGS with the pack
18143 expansion at the end of PACKED_PARMS. Returns 0 if the type
18144 deduction succeeds, 1 otherwise. STRICT is the same as in
18145 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
18146 call argument list. We'll need to adjust the arguments to make them
18147 types. SUBR tells us if this is from a recursive call to
18148 type_unification_real, or for comparing two template argument
18149 lists. */
18151 static int
18152 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
18153 tree packed_args, unification_kind_t strict,
18154 bool subr, bool explain_p)
18156 tree parm
18157 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
18158 tree pattern = PACK_EXPANSION_PATTERN (parm);
18159 tree pack, packs = NULL_TREE;
18160 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
18162 packed_args = expand_template_argument_pack (packed_args);
18164 int len = TREE_VEC_LENGTH (packed_args);
18166 /* Determine the parameter packs we will be deducing from the
18167 pattern, and record their current deductions. */
18168 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
18169 pack; pack = TREE_CHAIN (pack))
18171 tree parm_pack = TREE_VALUE (pack);
18172 int idx, level;
18174 /* Determine the index and level of this parameter pack. */
18175 template_parm_level_and_index (parm_pack, &level, &idx);
18177 /* Keep track of the parameter packs and their corresponding
18178 argument packs. */
18179 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
18180 TREE_TYPE (packs) = make_tree_vec (len - start);
18183 /* Loop through all of the arguments that have not yet been
18184 unified and unify each with the pattern. */
18185 for (i = start; i < len; i++)
18187 tree parm;
18188 bool any_explicit = false;
18189 tree arg = TREE_VEC_ELT (packed_args, i);
18191 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
18192 or the element of its argument pack at the current index if
18193 this argument was explicitly specified. */
18194 for (pack = packs; pack; pack = TREE_CHAIN (pack))
18196 int idx, level;
18197 tree arg, pargs;
18198 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
18200 arg = NULL_TREE;
18201 if (TREE_VALUE (pack)
18202 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
18203 && (i - start < TREE_VEC_LENGTH (pargs)))
18205 any_explicit = true;
18206 arg = TREE_VEC_ELT (pargs, i - start);
18208 TMPL_ARG (targs, level, idx) = arg;
18211 /* If we had explicit template arguments, substitute them into the
18212 pattern before deduction. */
18213 if (any_explicit)
18215 /* Some arguments might still be unspecified or dependent. */
18216 bool dependent;
18217 ++processing_template_decl;
18218 dependent = any_dependent_template_arguments_p (targs);
18219 if (!dependent)
18220 --processing_template_decl;
18221 parm = tsubst (pattern, targs,
18222 explain_p ? tf_warning_or_error : tf_none,
18223 NULL_TREE);
18224 if (dependent)
18225 --processing_template_decl;
18226 if (parm == error_mark_node)
18227 return 1;
18229 else
18230 parm = pattern;
18232 /* Unify the pattern with the current argument. */
18233 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
18234 LOOKUP_IMPLICIT, explain_p))
18235 return 1;
18237 /* For each parameter pack, collect the deduced value. */
18238 for (pack = packs; pack; pack = TREE_CHAIN (pack))
18240 int idx, level;
18241 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
18243 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
18244 TMPL_ARG (targs, level, idx);
18248 /* Verify that the results of unification with the parameter packs
18249 produce results consistent with what we've seen before, and make
18250 the deduced argument packs available. */
18251 for (pack = packs; pack; pack = TREE_CHAIN (pack))
18253 tree old_pack = TREE_VALUE (pack);
18254 tree new_args = TREE_TYPE (pack);
18255 int i, len = TREE_VEC_LENGTH (new_args);
18256 int idx, level;
18257 bool nondeduced_p = false;
18259 /* By default keep the original deduced argument pack.
18260 If necessary, more specific code is going to update the
18261 resulting deduced argument later down in this function. */
18262 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
18263 TMPL_ARG (targs, level, idx) = old_pack;
18265 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
18266 actually deduce anything. */
18267 for (i = 0; i < len && !nondeduced_p; ++i)
18268 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
18269 nondeduced_p = true;
18270 if (nondeduced_p)
18271 continue;
18273 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
18275 /* If we had fewer function args than explicit template args,
18276 just use the explicits. */
18277 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
18278 int explicit_len = TREE_VEC_LENGTH (explicit_args);
18279 if (len < explicit_len)
18280 new_args = explicit_args;
18283 if (!old_pack)
18285 tree result;
18286 /* Build the deduced *_ARGUMENT_PACK. */
18287 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
18289 result = make_node (NONTYPE_ARGUMENT_PACK);
18290 TREE_TYPE (result) =
18291 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
18292 TREE_CONSTANT (result) = 1;
18294 else
18295 result = cxx_make_type (TYPE_ARGUMENT_PACK);
18297 SET_ARGUMENT_PACK_ARGS (result, new_args);
18299 /* Note the deduced argument packs for this parameter
18300 pack. */
18301 TMPL_ARG (targs, level, idx) = result;
18303 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
18304 && (ARGUMENT_PACK_ARGS (old_pack)
18305 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
18307 /* We only had the explicitly-provided arguments before, but
18308 now we have a complete set of arguments. */
18309 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
18311 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
18312 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
18313 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
18315 else
18317 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
18318 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
18320 if (!comp_template_args_with_info (old_args, new_args,
18321 &bad_old_arg, &bad_new_arg))
18322 /* Inconsistent unification of this parameter pack. */
18323 return unify_parameter_pack_inconsistent (explain_p,
18324 bad_old_arg,
18325 bad_new_arg);
18329 return unify_success (explain_p);
18332 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
18333 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
18334 parameters and return value are as for unify. */
18336 static int
18337 unify_array_domain (tree tparms, tree targs,
18338 tree parm_dom, tree arg_dom,
18339 bool explain_p)
18341 tree parm_max;
18342 tree arg_max;
18343 bool parm_cst;
18344 bool arg_cst;
18346 /* Our representation of array types uses "N - 1" as the
18347 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
18348 not an integer constant. We cannot unify arbitrarily
18349 complex expressions, so we eliminate the MINUS_EXPRs
18350 here. */
18351 parm_max = TYPE_MAX_VALUE (parm_dom);
18352 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
18353 if (!parm_cst)
18355 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
18356 parm_max = TREE_OPERAND (parm_max, 0);
18358 arg_max = TYPE_MAX_VALUE (arg_dom);
18359 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
18360 if (!arg_cst)
18362 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
18363 trying to unify the type of a variable with the type
18364 of a template parameter. For example:
18366 template <unsigned int N>
18367 void f (char (&) [N]);
18368 int g();
18369 void h(int i) {
18370 char a[g(i)];
18371 f(a);
18374 Here, the type of the ARG will be "int [g(i)]", and
18375 may be a SAVE_EXPR, etc. */
18376 if (TREE_CODE (arg_max) != MINUS_EXPR)
18377 return unify_vla_arg (explain_p, arg_dom);
18378 arg_max = TREE_OPERAND (arg_max, 0);
18381 /* If only one of the bounds used a MINUS_EXPR, compensate
18382 by adding one to the other bound. */
18383 if (parm_cst && !arg_cst)
18384 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
18385 integer_type_node,
18386 parm_max,
18387 integer_one_node);
18388 else if (arg_cst && !parm_cst)
18389 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
18390 integer_type_node,
18391 arg_max,
18392 integer_one_node);
18394 return unify (tparms, targs, parm_max, arg_max,
18395 UNIFY_ALLOW_INTEGER, explain_p);
18398 /* Deduce the value of template parameters. TPARMS is the (innermost)
18399 set of template parameters to a template. TARGS is the bindings
18400 for those template parameters, as determined thus far; TARGS may
18401 include template arguments for outer levels of template parameters
18402 as well. PARM is a parameter to a template function, or a
18403 subcomponent of that parameter; ARG is the corresponding argument.
18404 This function attempts to match PARM with ARG in a manner
18405 consistent with the existing assignments in TARGS. If more values
18406 are deduced, then TARGS is updated.
18408 Returns 0 if the type deduction succeeds, 1 otherwise. The
18409 parameter STRICT is a bitwise or of the following flags:
18411 UNIFY_ALLOW_NONE:
18412 Require an exact match between PARM and ARG.
18413 UNIFY_ALLOW_MORE_CV_QUAL:
18414 Allow the deduced ARG to be more cv-qualified (by qualification
18415 conversion) than ARG.
18416 UNIFY_ALLOW_LESS_CV_QUAL:
18417 Allow the deduced ARG to be less cv-qualified than ARG.
18418 UNIFY_ALLOW_DERIVED:
18419 Allow the deduced ARG to be a template base class of ARG,
18420 or a pointer to a template base class of the type pointed to by
18421 ARG.
18422 UNIFY_ALLOW_INTEGER:
18423 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
18424 case for more information.
18425 UNIFY_ALLOW_OUTER_LEVEL:
18426 This is the outermost level of a deduction. Used to determine validity
18427 of qualification conversions. A valid qualification conversion must
18428 have const qualified pointers leading up to the inner type which
18429 requires additional CV quals, except at the outer level, where const
18430 is not required [conv.qual]. It would be normal to set this flag in
18431 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
18432 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
18433 This is the outermost level of a deduction, and PARM can be more CV
18434 qualified at this point.
18435 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
18436 This is the outermost level of a deduction, and PARM can be less CV
18437 qualified at this point. */
18439 static int
18440 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
18441 bool explain_p)
18443 int idx;
18444 tree targ;
18445 tree tparm;
18446 int strict_in = strict;
18448 /* I don't think this will do the right thing with respect to types.
18449 But the only case I've seen it in so far has been array bounds, where
18450 signedness is the only information lost, and I think that will be
18451 okay. */
18452 while (TREE_CODE (parm) == NOP_EXPR)
18453 parm = TREE_OPERAND (parm, 0);
18455 if (arg == error_mark_node)
18456 return unify_invalid (explain_p);
18457 if (arg == unknown_type_node
18458 || arg == init_list_type_node)
18459 /* We can't deduce anything from this, but we might get all the
18460 template args from other function args. */
18461 return unify_success (explain_p);
18463 /* If PARM uses template parameters, then we can't bail out here,
18464 even if ARG == PARM, since we won't record unifications for the
18465 template parameters. We might need them if we're trying to
18466 figure out which of two things is more specialized. */
18467 if (arg == parm && !uses_template_parms (parm))
18468 return unify_success (explain_p);
18470 /* Handle init lists early, so the rest of the function can assume
18471 we're dealing with a type. */
18472 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
18474 tree elt, elttype;
18475 unsigned i;
18476 tree orig_parm = parm;
18478 /* Replace T with std::initializer_list<T> for deduction. */
18479 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18480 && flag_deduce_init_list)
18481 parm = listify (parm);
18483 if (!is_std_init_list (parm)
18484 && TREE_CODE (parm) != ARRAY_TYPE)
18485 /* We can only deduce from an initializer list argument if the
18486 parameter is std::initializer_list or an array; otherwise this
18487 is a non-deduced context. */
18488 return unify_success (explain_p);
18490 if (TREE_CODE (parm) == ARRAY_TYPE)
18491 elttype = TREE_TYPE (parm);
18492 else
18494 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
18495 /* Deduction is defined in terms of a single type, so just punt
18496 on the (bizarre) std::initializer_list<T...>. */
18497 if (PACK_EXPANSION_P (elttype))
18498 return unify_success (explain_p);
18501 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
18503 int elt_strict = strict;
18505 if (elt == error_mark_node)
18506 return unify_invalid (explain_p);
18508 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
18510 tree type = TREE_TYPE (elt);
18511 if (type == error_mark_node)
18512 return unify_invalid (explain_p);
18513 /* It should only be possible to get here for a call. */
18514 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
18515 elt_strict |= maybe_adjust_types_for_deduction
18516 (DEDUCE_CALL, &elttype, &type, elt);
18517 elt = type;
18520 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
18521 explain_p);
18524 if (TREE_CODE (parm) == ARRAY_TYPE
18525 && deducible_array_bound (TYPE_DOMAIN (parm)))
18527 /* Also deduce from the length of the initializer list. */
18528 tree max = size_int (CONSTRUCTOR_NELTS (arg));
18529 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
18530 if (idx == error_mark_node)
18531 return unify_invalid (explain_p);
18532 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
18533 idx, explain_p);
18536 /* If the std::initializer_list<T> deduction worked, replace the
18537 deduced A with std::initializer_list<A>. */
18538 if (orig_parm != parm)
18540 idx = TEMPLATE_TYPE_IDX (orig_parm);
18541 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18542 targ = listify (targ);
18543 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
18545 return unify_success (explain_p);
18548 /* Immediately reject some pairs that won't unify because of
18549 cv-qualification mismatches. */
18550 if (TREE_CODE (arg) == TREE_CODE (parm)
18551 && TYPE_P (arg)
18552 /* It is the elements of the array which hold the cv quals of an array
18553 type, and the elements might be template type parms. We'll check
18554 when we recurse. */
18555 && TREE_CODE (arg) != ARRAY_TYPE
18556 /* We check the cv-qualifiers when unifying with template type
18557 parameters below. We want to allow ARG `const T' to unify with
18558 PARM `T' for example, when computing which of two templates
18559 is more specialized, for example. */
18560 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
18561 && !check_cv_quals_for_unify (strict_in, arg, parm))
18562 return unify_cv_qual_mismatch (explain_p, parm, arg);
18564 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
18565 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
18566 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
18567 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
18568 strict &= ~UNIFY_ALLOW_DERIVED;
18569 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18570 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
18572 switch (TREE_CODE (parm))
18574 case TYPENAME_TYPE:
18575 case SCOPE_REF:
18576 case UNBOUND_CLASS_TEMPLATE:
18577 /* In a type which contains a nested-name-specifier, template
18578 argument values cannot be deduced for template parameters used
18579 within the nested-name-specifier. */
18580 return unify_success (explain_p);
18582 case TEMPLATE_TYPE_PARM:
18583 case TEMPLATE_TEMPLATE_PARM:
18584 case BOUND_TEMPLATE_TEMPLATE_PARM:
18585 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
18586 if (error_operand_p (tparm))
18587 return unify_invalid (explain_p);
18589 if (TEMPLATE_TYPE_LEVEL (parm)
18590 != template_decl_level (tparm))
18591 /* The PARM is not one we're trying to unify. Just check
18592 to see if it matches ARG. */
18594 if (TREE_CODE (arg) == TREE_CODE (parm)
18595 && (is_auto (parm) ? is_auto (arg)
18596 : same_type_p (parm, arg)))
18597 return unify_success (explain_p);
18598 else
18599 return unify_type_mismatch (explain_p, parm, arg);
18601 idx = TEMPLATE_TYPE_IDX (parm);
18602 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18603 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
18604 if (error_operand_p (tparm))
18605 return unify_invalid (explain_p);
18607 /* Check for mixed types and values. */
18608 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18609 && TREE_CODE (tparm) != TYPE_DECL)
18610 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18611 && TREE_CODE (tparm) != TEMPLATE_DECL))
18612 gcc_unreachable ();
18614 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18616 /* ARG must be constructed from a template class or a template
18617 template parameter. */
18618 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
18619 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
18620 return unify_template_deduction_failure (explain_p, parm, arg);
18622 tree parmvec = TYPE_TI_ARGS (parm);
18623 /* An alias template name is never deduced. */
18624 if (TYPE_ALIAS_P (arg))
18625 arg = strip_typedefs (arg);
18626 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
18627 tree full_argvec = add_to_template_args (targs, argvec);
18628 tree parm_parms
18629 = DECL_INNERMOST_TEMPLATE_PARMS
18630 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
18631 int i, len;
18632 int parm_variadic_p = 0;
18634 /* The resolution to DR150 makes clear that default
18635 arguments for an N-argument may not be used to bind T
18636 to a template template parameter with fewer than N
18637 parameters. It is not safe to permit the binding of
18638 default arguments as an extension, as that may change
18639 the meaning of a conforming program. Consider:
18641 struct Dense { static const unsigned int dim = 1; };
18643 template <template <typename> class View,
18644 typename Block>
18645 void operator+(float, View<Block> const&);
18647 template <typename Block,
18648 unsigned int Dim = Block::dim>
18649 struct Lvalue_proxy { operator float() const; };
18651 void
18652 test_1d (void) {
18653 Lvalue_proxy<Dense> p;
18654 float b;
18655 b + p;
18658 Here, if Lvalue_proxy is permitted to bind to View, then
18659 the global operator+ will be used; if they are not, the
18660 Lvalue_proxy will be converted to float. */
18661 if (coerce_template_parms (parm_parms,
18662 full_argvec,
18663 TYPE_TI_TEMPLATE (parm),
18664 (explain_p
18665 ? tf_warning_or_error
18666 : tf_none),
18667 /*require_all_args=*/true,
18668 /*use_default_args=*/false)
18669 == error_mark_node)
18670 return 1;
18672 /* Deduce arguments T, i from TT<T> or TT<i>.
18673 We check each element of PARMVEC and ARGVEC individually
18674 rather than the whole TREE_VEC since they can have
18675 different number of elements. */
18677 parmvec = expand_template_argument_pack (parmvec);
18678 argvec = expand_template_argument_pack (argvec);
18680 len = TREE_VEC_LENGTH (parmvec);
18682 /* Check if the parameters end in a pack, making them
18683 variadic. */
18684 if (len > 0
18685 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
18686 parm_variadic_p = 1;
18688 for (i = 0; i < len - parm_variadic_p; ++i)
18689 /* If the template argument list of P contains a pack
18690 expansion that is not the last template argument, the
18691 entire template argument list is a non-deduced
18692 context. */
18693 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
18694 return unify_success (explain_p);
18696 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
18697 return unify_too_few_arguments (explain_p,
18698 TREE_VEC_LENGTH (argvec), len);
18700 for (i = 0; i < len - parm_variadic_p; ++i)
18702 RECUR_AND_CHECK_FAILURE (tparms, targs,
18703 TREE_VEC_ELT (parmvec, i),
18704 TREE_VEC_ELT (argvec, i),
18705 UNIFY_ALLOW_NONE, explain_p);
18708 if (parm_variadic_p
18709 && unify_pack_expansion (tparms, targs,
18710 parmvec, argvec,
18711 DEDUCE_EXACT,
18712 /*subr=*/true, explain_p))
18713 return 1;
18715 arg = TYPE_TI_TEMPLATE (arg);
18717 /* Fall through to deduce template name. */
18720 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18721 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18723 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
18725 /* Simple cases: Value already set, does match or doesn't. */
18726 if (targ != NULL_TREE && template_args_equal (targ, arg))
18727 return unify_success (explain_p);
18728 else if (targ)
18729 return unify_inconsistency (explain_p, parm, targ, arg);
18731 else
18733 /* If PARM is `const T' and ARG is only `int', we don't have
18734 a match unless we are allowing additional qualification.
18735 If ARG is `const int' and PARM is just `T' that's OK;
18736 that binds `const int' to `T'. */
18737 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
18738 arg, parm))
18739 return unify_cv_qual_mismatch (explain_p, parm, arg);
18741 /* Consider the case where ARG is `const volatile int' and
18742 PARM is `const T'. Then, T should be `volatile int'. */
18743 arg = cp_build_qualified_type_real
18744 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
18745 if (arg == error_mark_node)
18746 return unify_invalid (explain_p);
18748 /* Simple cases: Value already set, does match or doesn't. */
18749 if (targ != NULL_TREE && same_type_p (targ, arg))
18750 return unify_success (explain_p);
18751 else if (targ)
18752 return unify_inconsistency (explain_p, parm, targ, arg);
18754 /* Make sure that ARG is not a variable-sized array. (Note
18755 that were talking about variable-sized arrays (like
18756 `int[n]'), rather than arrays of unknown size (like
18757 `int[]').) We'll get very confused by such a type since
18758 the bound of the array is not constant, and therefore
18759 not mangleable. Besides, such types are not allowed in
18760 ISO C++, so we can do as we please here. We do allow
18761 them for 'auto' deduction, since that isn't ABI-exposed. */
18762 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
18763 return unify_vla_arg (explain_p, arg);
18765 /* Strip typedefs as in convert_template_argument. */
18766 arg = canonicalize_type_argument (arg, tf_none);
18769 /* If ARG is a parameter pack or an expansion, we cannot unify
18770 against it unless PARM is also a parameter pack. */
18771 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
18772 && !template_parameter_pack_p (parm))
18773 return unify_parameter_pack_mismatch (explain_p, parm, arg);
18775 /* If the argument deduction results is a METHOD_TYPE,
18776 then there is a problem.
18777 METHOD_TYPE doesn't map to any real C++ type the result of
18778 the deduction can not be of that type. */
18779 if (TREE_CODE (arg) == METHOD_TYPE)
18780 return unify_method_type_error (explain_p, arg);
18782 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
18783 return unify_success (explain_p);
18785 case TEMPLATE_PARM_INDEX:
18786 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
18787 if (error_operand_p (tparm))
18788 return unify_invalid (explain_p);
18790 if (TEMPLATE_PARM_LEVEL (parm)
18791 != template_decl_level (tparm))
18793 /* The PARM is not one we're trying to unify. Just check
18794 to see if it matches ARG. */
18795 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
18796 && cp_tree_equal (parm, arg));
18797 if (result)
18798 unify_expression_unequal (explain_p, parm, arg);
18799 return result;
18802 idx = TEMPLATE_PARM_IDX (parm);
18803 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18805 if (targ)
18807 int x = !cp_tree_equal (targ, arg);
18808 if (x)
18809 unify_inconsistency (explain_p, parm, targ, arg);
18810 return x;
18813 /* [temp.deduct.type] If, in the declaration of a function template
18814 with a non-type template-parameter, the non-type
18815 template-parameter is used in an expression in the function
18816 parameter-list and, if the corresponding template-argument is
18817 deduced, the template-argument type shall match the type of the
18818 template-parameter exactly, except that a template-argument
18819 deduced from an array bound may be of any integral type.
18820 The non-type parameter might use already deduced type parameters. */
18821 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
18822 if (!TREE_TYPE (arg))
18823 /* Template-parameter dependent expression. Just accept it for now.
18824 It will later be processed in convert_template_argument. */
18826 else if (same_type_p (TREE_TYPE (arg), tparm))
18827 /* OK */;
18828 else if ((strict & UNIFY_ALLOW_INTEGER)
18829 && CP_INTEGRAL_TYPE_P (tparm))
18830 /* Convert the ARG to the type of PARM; the deduced non-type
18831 template argument must exactly match the types of the
18832 corresponding parameter. */
18833 arg = fold (build_nop (tparm, arg));
18834 else if (uses_template_parms (tparm))
18835 /* We haven't deduced the type of this parameter yet. Try again
18836 later. */
18837 return unify_success (explain_p);
18838 else
18839 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
18841 /* If ARG is a parameter pack or an expansion, we cannot unify
18842 against it unless PARM is also a parameter pack. */
18843 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
18844 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
18845 return unify_parameter_pack_mismatch (explain_p, parm, arg);
18847 arg = strip_typedefs_expr (arg);
18848 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
18849 return unify_success (explain_p);
18851 case PTRMEM_CST:
18853 /* A pointer-to-member constant can be unified only with
18854 another constant. */
18855 if (TREE_CODE (arg) != PTRMEM_CST)
18856 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
18858 /* Just unify the class member. It would be useless (and possibly
18859 wrong, depending on the strict flags) to unify also
18860 PTRMEM_CST_CLASS, because we want to be sure that both parm and
18861 arg refer to the same variable, even if through different
18862 classes. For instance:
18864 struct A { int x; };
18865 struct B : A { };
18867 Unification of &A::x and &B::x must succeed. */
18868 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
18869 PTRMEM_CST_MEMBER (arg), strict, explain_p);
18872 case POINTER_TYPE:
18874 if (!TYPE_PTR_P (arg))
18875 return unify_type_mismatch (explain_p, parm, arg);
18877 /* [temp.deduct.call]
18879 A can be another pointer or pointer to member type that can
18880 be converted to the deduced A via a qualification
18881 conversion (_conv.qual_).
18883 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
18884 This will allow for additional cv-qualification of the
18885 pointed-to types if appropriate. */
18887 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
18888 /* The derived-to-base conversion only persists through one
18889 level of pointers. */
18890 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
18892 return unify (tparms, targs, TREE_TYPE (parm),
18893 TREE_TYPE (arg), strict, explain_p);
18896 case REFERENCE_TYPE:
18897 if (TREE_CODE (arg) != REFERENCE_TYPE)
18898 return unify_type_mismatch (explain_p, parm, arg);
18899 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18900 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
18902 case ARRAY_TYPE:
18903 if (TREE_CODE (arg) != ARRAY_TYPE)
18904 return unify_type_mismatch (explain_p, parm, arg);
18905 if ((TYPE_DOMAIN (parm) == NULL_TREE)
18906 != (TYPE_DOMAIN (arg) == NULL_TREE))
18907 return unify_type_mismatch (explain_p, parm, arg);
18908 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18909 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
18910 if (TYPE_DOMAIN (parm) != NULL_TREE)
18911 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
18912 TYPE_DOMAIN (arg), explain_p);
18913 return unify_success (explain_p);
18915 case REAL_TYPE:
18916 case COMPLEX_TYPE:
18917 case VECTOR_TYPE:
18918 case INTEGER_TYPE:
18919 case BOOLEAN_TYPE:
18920 case ENUMERAL_TYPE:
18921 case VOID_TYPE:
18922 case NULLPTR_TYPE:
18923 if (TREE_CODE (arg) != TREE_CODE (parm))
18924 return unify_type_mismatch (explain_p, parm, arg);
18926 /* We have already checked cv-qualification at the top of the
18927 function. */
18928 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
18929 return unify_type_mismatch (explain_p, parm, arg);
18931 /* As far as unification is concerned, this wins. Later checks
18932 will invalidate it if necessary. */
18933 return unify_success (explain_p);
18935 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
18936 /* Type INTEGER_CST can come from ordinary constant template args. */
18937 case INTEGER_CST:
18938 while (TREE_CODE (arg) == NOP_EXPR)
18939 arg = TREE_OPERAND (arg, 0);
18941 if (TREE_CODE (arg) != INTEGER_CST)
18942 return unify_template_argument_mismatch (explain_p, parm, arg);
18943 return (tree_int_cst_equal (parm, arg)
18944 ? unify_success (explain_p)
18945 : unify_template_argument_mismatch (explain_p, parm, arg));
18947 case TREE_VEC:
18949 int i, len, argslen;
18950 int parm_variadic_p = 0;
18952 if (TREE_CODE (arg) != TREE_VEC)
18953 return unify_template_argument_mismatch (explain_p, parm, arg);
18955 len = TREE_VEC_LENGTH (parm);
18956 argslen = TREE_VEC_LENGTH (arg);
18958 /* Check for pack expansions in the parameters. */
18959 for (i = 0; i < len; ++i)
18961 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
18963 if (i == len - 1)
18964 /* We can unify against something with a trailing
18965 parameter pack. */
18966 parm_variadic_p = 1;
18967 else
18968 /* [temp.deduct.type]/9: If the template argument list of
18969 P contains a pack expansion that is not the last
18970 template argument, the entire template argument list
18971 is a non-deduced context. */
18972 return unify_success (explain_p);
18976 /* If we don't have enough arguments to satisfy the parameters
18977 (not counting the pack expression at the end), or we have
18978 too many arguments for a parameter list that doesn't end in
18979 a pack expression, we can't unify. */
18980 if (parm_variadic_p
18981 ? argslen < len - parm_variadic_p
18982 : argslen != len)
18983 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
18985 /* Unify all of the parameters that precede the (optional)
18986 pack expression. */
18987 for (i = 0; i < len - parm_variadic_p; ++i)
18989 RECUR_AND_CHECK_FAILURE (tparms, targs,
18990 TREE_VEC_ELT (parm, i),
18991 TREE_VEC_ELT (arg, i),
18992 UNIFY_ALLOW_NONE, explain_p);
18994 if (parm_variadic_p)
18995 return unify_pack_expansion (tparms, targs, parm, arg,
18996 DEDUCE_EXACT,
18997 /*subr=*/true, explain_p);
18998 return unify_success (explain_p);
19001 case RECORD_TYPE:
19002 case UNION_TYPE:
19003 if (TREE_CODE (arg) != TREE_CODE (parm))
19004 return unify_type_mismatch (explain_p, parm, arg);
19006 if (TYPE_PTRMEMFUNC_P (parm))
19008 if (!TYPE_PTRMEMFUNC_P (arg))
19009 return unify_type_mismatch (explain_p, parm, arg);
19011 return unify (tparms, targs,
19012 TYPE_PTRMEMFUNC_FN_TYPE (parm),
19013 TYPE_PTRMEMFUNC_FN_TYPE (arg),
19014 strict, explain_p);
19016 else if (TYPE_PTRMEMFUNC_P (arg))
19017 return unify_type_mismatch (explain_p, parm, arg);
19019 if (CLASSTYPE_TEMPLATE_INFO (parm))
19021 tree t = NULL_TREE;
19023 if (strict_in & UNIFY_ALLOW_DERIVED)
19025 /* First, we try to unify the PARM and ARG directly. */
19026 t = try_class_unification (tparms, targs,
19027 parm, arg, explain_p);
19029 if (!t)
19031 /* Fallback to the special case allowed in
19032 [temp.deduct.call]:
19034 If P is a class, and P has the form
19035 template-id, then A can be a derived class of
19036 the deduced A. Likewise, if P is a pointer to
19037 a class of the form template-id, A can be a
19038 pointer to a derived class pointed to by the
19039 deduced A. */
19040 enum template_base_result r;
19041 r = get_template_base (tparms, targs, parm, arg,
19042 explain_p, &t);
19044 if (!t)
19045 return unify_no_common_base (explain_p, r, parm, arg);
19048 else if (CLASSTYPE_TEMPLATE_INFO (arg)
19049 && (CLASSTYPE_TI_TEMPLATE (parm)
19050 == CLASSTYPE_TI_TEMPLATE (arg)))
19051 /* Perhaps PARM is something like S<U> and ARG is S<int>.
19052 Then, we should unify `int' and `U'. */
19053 t = arg;
19054 else
19055 /* There's no chance of unification succeeding. */
19056 return unify_type_mismatch (explain_p, parm, arg);
19058 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
19059 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
19061 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
19062 return unify_type_mismatch (explain_p, parm, arg);
19063 return unify_success (explain_p);
19065 case METHOD_TYPE:
19066 case FUNCTION_TYPE:
19068 unsigned int nargs;
19069 tree *args;
19070 tree a;
19071 unsigned int i;
19073 if (TREE_CODE (arg) != TREE_CODE (parm))
19074 return unify_type_mismatch (explain_p, parm, arg);
19076 /* CV qualifications for methods can never be deduced, they must
19077 match exactly. We need to check them explicitly here,
19078 because type_unification_real treats them as any other
19079 cv-qualified parameter. */
19080 if (TREE_CODE (parm) == METHOD_TYPE
19081 && (!check_cv_quals_for_unify
19082 (UNIFY_ALLOW_NONE,
19083 class_of_this_parm (arg),
19084 class_of_this_parm (parm))))
19085 return unify_cv_qual_mismatch (explain_p, parm, arg);
19087 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
19088 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
19090 nargs = list_length (TYPE_ARG_TYPES (arg));
19091 args = XALLOCAVEC (tree, nargs);
19092 for (a = TYPE_ARG_TYPES (arg), i = 0;
19093 a != NULL_TREE && a != void_list_node;
19094 a = TREE_CHAIN (a), ++i)
19095 args[i] = TREE_VALUE (a);
19096 nargs = i;
19098 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
19099 args, nargs, 1, DEDUCE_EXACT,
19100 LOOKUP_NORMAL, NULL, explain_p);
19103 case OFFSET_TYPE:
19104 /* Unify a pointer to member with a pointer to member function, which
19105 deduces the type of the member as a function type. */
19106 if (TYPE_PTRMEMFUNC_P (arg))
19108 /* Check top-level cv qualifiers */
19109 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
19110 return unify_cv_qual_mismatch (explain_p, parm, arg);
19112 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
19113 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
19114 UNIFY_ALLOW_NONE, explain_p);
19116 /* Determine the type of the function we are unifying against. */
19117 tree fntype = static_fn_type (arg);
19119 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
19122 if (TREE_CODE (arg) != OFFSET_TYPE)
19123 return unify_type_mismatch (explain_p, parm, arg);
19124 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
19125 TYPE_OFFSET_BASETYPE (arg),
19126 UNIFY_ALLOW_NONE, explain_p);
19127 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19128 strict, explain_p);
19130 case CONST_DECL:
19131 if (DECL_TEMPLATE_PARM_P (parm))
19132 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
19133 if (arg != scalar_constant_value (parm))
19134 return unify_template_argument_mismatch (explain_p, parm, arg);
19135 return unify_success (explain_p);
19137 case FIELD_DECL:
19138 case TEMPLATE_DECL:
19139 /* Matched cases are handled by the ARG == PARM test above. */
19140 return unify_template_argument_mismatch (explain_p, parm, arg);
19142 case VAR_DECL:
19143 /* A non-type template parameter that is a variable should be a
19144 an integral constant, in which case, it whould have been
19145 folded into its (constant) value. So we should not be getting
19146 a variable here. */
19147 gcc_unreachable ();
19149 case TYPE_ARGUMENT_PACK:
19150 case NONTYPE_ARGUMENT_PACK:
19151 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
19152 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
19154 case TYPEOF_TYPE:
19155 case DECLTYPE_TYPE:
19156 case UNDERLYING_TYPE:
19157 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
19158 or UNDERLYING_TYPE nodes. */
19159 return unify_success (explain_p);
19161 case ERROR_MARK:
19162 /* Unification fails if we hit an error node. */
19163 return unify_invalid (explain_p);
19165 case INDIRECT_REF:
19166 if (REFERENCE_REF_P (parm))
19168 if (REFERENCE_REF_P (arg))
19169 arg = TREE_OPERAND (arg, 0);
19170 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
19171 strict, explain_p);
19173 /* FALLTHRU */
19175 default:
19176 /* An unresolved overload is a nondeduced context. */
19177 if (is_overloaded_fn (parm) || type_unknown_p (parm))
19178 return unify_success (explain_p);
19179 gcc_assert (EXPR_P (parm));
19181 /* We must be looking at an expression. This can happen with
19182 something like:
19184 template <int I>
19185 void foo(S<I>, S<I + 2>);
19187 This is a "nondeduced context":
19189 [deduct.type]
19191 The nondeduced contexts are:
19193 --A type that is a template-id in which one or more of
19194 the template-arguments is an expression that references
19195 a template-parameter.
19197 In these cases, we assume deduction succeeded, but don't
19198 actually infer any unifications. */
19200 if (!uses_template_parms (parm)
19201 && !template_args_equal (parm, arg))
19202 return unify_expression_unequal (explain_p, parm, arg);
19203 else
19204 return unify_success (explain_p);
19207 #undef RECUR_AND_CHECK_FAILURE
19209 /* Note that DECL can be defined in this translation unit, if
19210 required. */
19212 static void
19213 mark_definable (tree decl)
19215 tree clone;
19216 DECL_NOT_REALLY_EXTERN (decl) = 1;
19217 FOR_EACH_CLONE (clone, decl)
19218 DECL_NOT_REALLY_EXTERN (clone) = 1;
19221 /* Called if RESULT is explicitly instantiated, or is a member of an
19222 explicitly instantiated class. */
19224 void
19225 mark_decl_instantiated (tree result, int extern_p)
19227 SET_DECL_EXPLICIT_INSTANTIATION (result);
19229 /* If this entity has already been written out, it's too late to
19230 make any modifications. */
19231 if (TREE_ASM_WRITTEN (result))
19232 return;
19234 /* For anonymous namespace we don't need to do anything. */
19235 if (decl_anon_ns_mem_p (result))
19237 gcc_assert (!TREE_PUBLIC (result));
19238 return;
19241 if (TREE_CODE (result) != FUNCTION_DECL)
19242 /* The TREE_PUBLIC flag for function declarations will have been
19243 set correctly by tsubst. */
19244 TREE_PUBLIC (result) = 1;
19246 /* This might have been set by an earlier implicit instantiation. */
19247 DECL_COMDAT (result) = 0;
19249 if (extern_p)
19250 DECL_NOT_REALLY_EXTERN (result) = 0;
19251 else
19253 mark_definable (result);
19254 mark_needed (result);
19255 /* Always make artificials weak. */
19256 if (DECL_ARTIFICIAL (result) && flag_weak)
19257 comdat_linkage (result);
19258 /* For WIN32 we also want to put explicit instantiations in
19259 linkonce sections. */
19260 else if (TREE_PUBLIC (result))
19261 maybe_make_one_only (result);
19264 /* If EXTERN_P, then this function will not be emitted -- unless
19265 followed by an explicit instantiation, at which point its linkage
19266 will be adjusted. If !EXTERN_P, then this function will be
19267 emitted here. In neither circumstance do we want
19268 import_export_decl to adjust the linkage. */
19269 DECL_INTERFACE_KNOWN (result) = 1;
19272 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
19273 important template arguments. If any are missing, we check whether
19274 they're important by using error_mark_node for substituting into any
19275 args that were used for partial ordering (the ones between ARGS and END)
19276 and seeing if it bubbles up. */
19278 static bool
19279 check_undeduced_parms (tree targs, tree args, tree end)
19281 bool found = false;
19282 int i;
19283 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
19284 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
19286 found = true;
19287 TREE_VEC_ELT (targs, i) = error_mark_node;
19289 if (found)
19291 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
19292 if (substed == error_mark_node)
19293 return true;
19295 return false;
19298 /* Given two function templates PAT1 and PAT2, return:
19300 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
19301 -1 if PAT2 is more specialized than PAT1.
19302 0 if neither is more specialized.
19304 LEN indicates the number of parameters we should consider
19305 (defaulted parameters should not be considered).
19307 The 1998 std underspecified function template partial ordering, and
19308 DR214 addresses the issue. We take pairs of arguments, one from
19309 each of the templates, and deduce them against each other. One of
19310 the templates will be more specialized if all the *other*
19311 template's arguments deduce against its arguments and at least one
19312 of its arguments *does* *not* deduce against the other template's
19313 corresponding argument. Deduction is done as for class templates.
19314 The arguments used in deduction have reference and top level cv
19315 qualifiers removed. Iff both arguments were originally reference
19316 types *and* deduction succeeds in both directions, an lvalue reference
19317 wins against an rvalue reference and otherwise the template
19318 with the more cv-qualified argument wins for that pairing (if
19319 neither is more cv-qualified, they both are equal). Unlike regular
19320 deduction, after all the arguments have been deduced in this way,
19321 we do *not* verify the deduced template argument values can be
19322 substituted into non-deduced contexts.
19324 The logic can be a bit confusing here, because we look at deduce1 and
19325 targs1 to see if pat2 is at least as specialized, and vice versa; if we
19326 can find template arguments for pat1 to make arg1 look like arg2, that
19327 means that arg2 is at least as specialized as arg1. */
19330 more_specialized_fn (tree pat1, tree pat2, int len)
19332 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
19333 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
19334 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
19335 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
19336 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
19337 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
19338 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
19339 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
19340 tree origs1, origs2;
19341 bool lose1 = false;
19342 bool lose2 = false;
19344 /* Remove the this parameter from non-static member functions. If
19345 one is a non-static member function and the other is not a static
19346 member function, remove the first parameter from that function
19347 also. This situation occurs for operator functions where we
19348 locate both a member function (with this pointer) and non-member
19349 operator (with explicit first operand). */
19350 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
19352 len--; /* LEN is the number of significant arguments for DECL1 */
19353 args1 = TREE_CHAIN (args1);
19354 if (!DECL_STATIC_FUNCTION_P (decl2))
19355 args2 = TREE_CHAIN (args2);
19357 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
19359 args2 = TREE_CHAIN (args2);
19360 if (!DECL_STATIC_FUNCTION_P (decl1))
19362 len--;
19363 args1 = TREE_CHAIN (args1);
19367 /* If only one is a conversion operator, they are unordered. */
19368 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
19369 return 0;
19371 /* Consider the return type for a conversion function */
19372 if (DECL_CONV_FN_P (decl1))
19374 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
19375 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
19376 len++;
19379 processing_template_decl++;
19381 origs1 = args1;
19382 origs2 = args2;
19384 while (len--
19385 /* Stop when an ellipsis is seen. */
19386 && args1 != NULL_TREE && args2 != NULL_TREE)
19388 tree arg1 = TREE_VALUE (args1);
19389 tree arg2 = TREE_VALUE (args2);
19390 int deduce1, deduce2;
19391 int quals1 = -1;
19392 int quals2 = -1;
19393 int ref1 = 0;
19394 int ref2 = 0;
19396 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
19397 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
19399 /* When both arguments are pack expansions, we need only
19400 unify the patterns themselves. */
19401 arg1 = PACK_EXPANSION_PATTERN (arg1);
19402 arg2 = PACK_EXPANSION_PATTERN (arg2);
19404 /* This is the last comparison we need to do. */
19405 len = 0;
19408 if (TREE_CODE (arg1) == REFERENCE_TYPE)
19410 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
19411 arg1 = TREE_TYPE (arg1);
19412 quals1 = cp_type_quals (arg1);
19415 if (TREE_CODE (arg2) == REFERENCE_TYPE)
19417 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
19418 arg2 = TREE_TYPE (arg2);
19419 quals2 = cp_type_quals (arg2);
19422 arg1 = TYPE_MAIN_VARIANT (arg1);
19423 arg2 = TYPE_MAIN_VARIANT (arg2);
19425 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
19427 int i, len2 = list_length (args2);
19428 tree parmvec = make_tree_vec (1);
19429 tree argvec = make_tree_vec (len2);
19430 tree ta = args2;
19432 /* Setup the parameter vector, which contains only ARG1. */
19433 TREE_VEC_ELT (parmvec, 0) = arg1;
19435 /* Setup the argument vector, which contains the remaining
19436 arguments. */
19437 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
19438 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
19440 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
19441 argvec, DEDUCE_EXACT,
19442 /*subr=*/true, /*explain_p=*/false)
19443 == 0);
19445 /* We cannot deduce in the other direction, because ARG1 is
19446 a pack expansion but ARG2 is not. */
19447 deduce2 = 0;
19449 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
19451 int i, len1 = list_length (args1);
19452 tree parmvec = make_tree_vec (1);
19453 tree argvec = make_tree_vec (len1);
19454 tree ta = args1;
19456 /* Setup the parameter vector, which contains only ARG1. */
19457 TREE_VEC_ELT (parmvec, 0) = arg2;
19459 /* Setup the argument vector, which contains the remaining
19460 arguments. */
19461 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
19462 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
19464 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
19465 argvec, DEDUCE_EXACT,
19466 /*subr=*/true, /*explain_p=*/false)
19467 == 0);
19469 /* We cannot deduce in the other direction, because ARG2 is
19470 a pack expansion but ARG1 is not.*/
19471 deduce1 = 0;
19474 else
19476 /* The normal case, where neither argument is a pack
19477 expansion. */
19478 deduce1 = (unify (tparms1, targs1, arg1, arg2,
19479 UNIFY_ALLOW_NONE, /*explain_p=*/false)
19480 == 0);
19481 deduce2 = (unify (tparms2, targs2, arg2, arg1,
19482 UNIFY_ALLOW_NONE, /*explain_p=*/false)
19483 == 0);
19486 /* If we couldn't deduce arguments for tparms1 to make arg1 match
19487 arg2, then arg2 is not as specialized as arg1. */
19488 if (!deduce1)
19489 lose2 = true;
19490 if (!deduce2)
19491 lose1 = true;
19493 /* "If, for a given type, deduction succeeds in both directions
19494 (i.e., the types are identical after the transformations above)
19495 and both P and A were reference types (before being replaced with
19496 the type referred to above):
19497 - if the type from the argument template was an lvalue reference and
19498 the type from the parameter template was not, the argument type is
19499 considered to be more specialized than the other; otherwise,
19500 - if the type from the argument template is more cv-qualified
19501 than the type from the parameter template (as described above),
19502 the argument type is considered to be more specialized than the other;
19503 otherwise,
19504 - neither type is more specialized than the other." */
19506 if (deduce1 && deduce2)
19508 if (ref1 && ref2 && ref1 != ref2)
19510 if (ref1 > ref2)
19511 lose1 = true;
19512 else
19513 lose2 = true;
19515 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
19517 if ((quals1 & quals2) == quals2)
19518 lose2 = true;
19519 if ((quals1 & quals2) == quals1)
19520 lose1 = true;
19524 if (lose1 && lose2)
19525 /* We've failed to deduce something in either direction.
19526 These must be unordered. */
19527 break;
19529 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
19530 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
19531 /* We have already processed all of the arguments in our
19532 handing of the pack expansion type. */
19533 len = 0;
19535 args1 = TREE_CHAIN (args1);
19536 args2 = TREE_CHAIN (args2);
19539 /* "In most cases, all template parameters must have values in order for
19540 deduction to succeed, but for partial ordering purposes a template
19541 parameter may remain without a value provided it is not used in the
19542 types being used for partial ordering."
19544 Thus, if we are missing any of the targs1 we need to substitute into
19545 origs1, then pat2 is not as specialized as pat1. This can happen when
19546 there is a nondeduced context. */
19547 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
19548 lose2 = true;
19549 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
19550 lose1 = true;
19552 processing_template_decl--;
19554 /* All things being equal, if the next argument is a pack expansion
19555 for one function but not for the other, prefer the
19556 non-variadic function. FIXME this is bogus; see c++/41958. */
19557 if (lose1 == lose2
19558 && args1 && TREE_VALUE (args1)
19559 && args2 && TREE_VALUE (args2))
19561 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
19562 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
19565 // All things still being equal, determine if one is more constrained.
19566 if (lose1 == lose2)
19568 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
19569 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
19570 lose1 = !subsumes_constraints (c1, c2);
19571 lose2 = !subsumes_constraints (c2, c1);
19574 if (lose1 == lose2)
19575 return 0;
19576 else if (!lose1)
19577 return 1;
19578 else
19579 return -1;
19582 /* Determine which of two partial specializations of TMPL is more
19583 specialized.
19585 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
19586 to the first partial specialization. The TREE_PURPOSE is the
19587 innermost set of template parameters for the partial
19588 specialization. PAT2 is similar, but for the second template.
19590 Return 1 if the first partial specialization is more specialized;
19591 -1 if the second is more specialized; 0 if neither is more
19592 specialized.
19594 See [temp.class.order] for information about determining which of
19595 two templates is more specialized. */
19597 static int
19598 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
19600 tree targs;
19601 int winner = 0;
19602 bool any_deductions = false;
19604 tree tmpl1 = TREE_VALUE (pat1);
19605 tree tmpl2 = TREE_VALUE (pat2);
19606 tree parms1 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl1);
19607 tree parms2 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl2);
19608 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
19609 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
19611 /* Just like what happens for functions, if we are ordering between
19612 different template specializations, we may encounter dependent
19613 types in the arguments, and we need our dependency check functions
19614 to behave correctly. */
19615 ++processing_template_decl;
19616 targs = get_partial_spec_bindings (tmpl, parms1, specargs1, specargs2);
19617 if (targs)
19619 --winner;
19620 any_deductions = true;
19623 targs = get_partial_spec_bindings (tmpl, parms2, specargs2, specargs1);
19624 if (targs)
19626 ++winner;
19627 any_deductions = true;
19629 --processing_template_decl;
19631 /* In the case of a tie where at least one of the templates
19632 has a parameter pack at the end, the template with the most
19633 non-packed parameters wins. */
19634 if (winner == 0
19635 && any_deductions
19636 && (template_args_variadic_p (TREE_PURPOSE (pat1))
19637 || template_args_variadic_p (TREE_PURPOSE (pat2))))
19639 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
19640 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
19641 int len1 = TREE_VEC_LENGTH (args1);
19642 int len2 = TREE_VEC_LENGTH (args2);
19644 /* We don't count the pack expansion at the end. */
19645 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
19646 --len1;
19647 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
19648 --len2;
19650 if (len1 > len2)
19651 return 1;
19652 else if (len1 < len2)
19653 return -1;
19657 // If still tied at this point, the most specialized is also
19658 // the most constrained.
19659 if (!winner)
19660 return more_constrained (tmpl1, tmpl2);
19662 return winner;
19665 /* Return the template arguments that will produce the function signature
19666 DECL from the function template FN, with the explicit template
19667 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
19668 also match. Return NULL_TREE if no satisfactory arguments could be
19669 found. */
19671 static tree
19672 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
19674 int ntparms = DECL_NTPARMS (fn);
19675 tree targs = make_tree_vec (ntparms);
19676 tree decl_type = TREE_TYPE (decl);
19677 tree decl_arg_types;
19678 tree *args;
19679 unsigned int nargs, ix;
19680 tree arg;
19682 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
19684 /* Never do unification on the 'this' parameter. */
19685 decl_arg_types = skip_artificial_parms_for (decl,
19686 TYPE_ARG_TYPES (decl_type));
19688 nargs = list_length (decl_arg_types);
19689 args = XALLOCAVEC (tree, nargs);
19690 for (arg = decl_arg_types, ix = 0;
19691 arg != NULL_TREE && arg != void_list_node;
19692 arg = TREE_CHAIN (arg), ++ix)
19693 args[ix] = TREE_VALUE (arg);
19695 if (fn_type_unification (fn, explicit_args, targs,
19696 args, ix,
19697 (check_rettype || DECL_CONV_FN_P (fn)
19698 ? TREE_TYPE (decl_type) : NULL_TREE),
19699 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
19700 /*decltype*/false)
19701 == error_mark_node)
19702 return NULL_TREE;
19704 return targs;
19707 /* Return the innermost template arguments that, when applied to a partial
19708 specialization of TMPL whose innermost template parameters are
19709 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
19710 ARGS.
19712 For example, suppose we have:
19714 template <class T, class U> struct S {};
19715 template <class T> struct S<T*, int> {};
19717 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
19718 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
19719 int}. The resulting vector will be {double}, indicating that `T'
19720 is bound to `double'. */
19722 static tree
19723 get_partial_spec_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
19725 int i, ntparms = TREE_VEC_LENGTH (tparms);
19726 tree deduced_args;
19727 tree innermost_deduced_args;
19729 innermost_deduced_args = make_tree_vec (ntparms);
19730 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19732 deduced_args = copy_node (args);
19733 SET_TMPL_ARGS_LEVEL (deduced_args,
19734 TMPL_ARGS_DEPTH (deduced_args),
19735 innermost_deduced_args);
19737 else
19738 deduced_args = innermost_deduced_args;
19740 if (unify (tparms, deduced_args,
19741 INNERMOST_TEMPLATE_ARGS (spec_args),
19742 INNERMOST_TEMPLATE_ARGS (args),
19743 UNIFY_ALLOW_NONE, /*explain_p=*/false))
19744 return NULL_TREE;
19746 for (i = 0; i < ntparms; ++i)
19747 if (! TREE_VEC_ELT (innermost_deduced_args, i))
19748 return NULL_TREE;
19750 /* Verify that nondeduced template arguments agree with the type
19751 obtained from argument deduction.
19753 For example:
19755 struct A { typedef int X; };
19756 template <class T, class U> struct C {};
19757 template <class T> struct C<T, typename T::X> {};
19759 Then with the instantiation `C<A, int>', we can deduce that
19760 `T' is `A' but unify () does not check whether `typename T::X'
19761 is `int'. */
19762 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
19763 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19764 spec_args, tmpl,
19765 tf_none, false, false);
19766 if (spec_args == error_mark_node
19767 /* We only need to check the innermost arguments; the other
19768 arguments will always agree. */
19769 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
19770 INNERMOST_TEMPLATE_ARGS (args)))
19771 return NULL_TREE;
19773 /* Now that we have bindings for all of the template arguments,
19774 ensure that the arguments deduced for the template template
19775 parameters have compatible template parameter lists. See the use
19776 of template_template_parm_bindings_ok_p in fn_type_unification
19777 for more information. */
19778 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
19779 return NULL_TREE;
19781 return deduced_args;
19784 // Compare two function templates T1 and T2 by deducing bindings
19785 // from one against the other. If both deductions succeed, compare
19786 // constraints to see which is more constrained.
19787 static int
19788 more_specialized_inst (tree t1, tree t2)
19790 int fate = 0;
19791 int count = 0;
19793 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
19795 --fate;
19796 ++count;
19799 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
19801 ++fate;
19802 ++count;
19805 // If both deductions succeed, then one may be more constrained.
19806 if (count == 2 && fate == 0)
19807 fate = more_constrained (t1, t2);
19809 return fate;
19812 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
19813 Return the TREE_LIST node with the most specialized template, if
19814 any. If there is no most specialized template, the error_mark_node
19815 is returned.
19817 Note that this function does not look at, or modify, the
19818 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
19819 returned is one of the elements of INSTANTIATIONS, callers may
19820 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
19821 and retrieve it from the value returned. */
19823 tree
19824 most_specialized_instantiation (tree templates)
19826 tree fn, champ;
19828 ++processing_template_decl;
19830 champ = templates;
19831 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
19833 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
19834 if (fate == -1)
19835 champ = fn;
19836 else if (!fate)
19838 /* Equally specialized, move to next function. If there
19839 is no next function, nothing's most specialized. */
19840 fn = TREE_CHAIN (fn);
19841 champ = fn;
19842 if (!fn)
19843 break;
19847 if (champ)
19848 /* Now verify that champ is better than everything earlier in the
19849 instantiation list. */
19850 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
19851 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
19853 champ = NULL_TREE;
19854 break;
19858 processing_template_decl--;
19860 if (!champ)
19861 return error_mark_node;
19863 return champ;
19866 /* If DECL is a specialization of some template, return the most
19867 general such template. Otherwise, returns NULL_TREE.
19869 For example, given:
19871 template <class T> struct S { template <class U> void f(U); };
19873 if TMPL is `template <class U> void S<int>::f(U)' this will return
19874 the full template. This function will not trace past partial
19875 specializations, however. For example, given in addition:
19877 template <class T> struct S<T*> { template <class U> void f(U); };
19879 if TMPL is `template <class U> void S<int*>::f(U)' this will return
19880 `template <class T> template <class U> S<T*>::f(U)'. */
19882 tree
19883 most_general_template (tree decl)
19885 if (TREE_CODE (decl) != TEMPLATE_DECL)
19887 if (tree tinfo = get_template_info (decl))
19888 decl = TI_TEMPLATE (tinfo);
19889 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
19890 template friend, or a FIELD_DECL for a capture pack. */
19891 if (TREE_CODE (decl) != TEMPLATE_DECL)
19892 return NULL_TREE;
19895 /* Look for more and more general templates. */
19896 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
19898 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
19899 (See cp-tree.h for details.) */
19900 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
19901 break;
19903 if (CLASS_TYPE_P (TREE_TYPE (decl))
19904 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
19905 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
19906 break;
19908 /* Stop if we run into an explicitly specialized class template. */
19909 if (!DECL_NAMESPACE_SCOPE_P (decl)
19910 && DECL_CONTEXT (decl)
19911 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
19912 break;
19914 decl = DECL_TI_TEMPLATE (decl);
19917 return decl;
19920 /* Return the most specialized of the template partial specializations
19921 which can produce TARGET, a specialization of some class or variable
19922 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
19923 a TEMPLATE_DECL node corresponding to the partial specialization, while
19924 the TREE_PURPOSE is the set of template arguments that must be
19925 substituted into the template pattern in order to generate TARGET.
19927 If the choice of partial specialization is ambiguous, a diagnostic
19928 is issued, and the error_mark_node is returned. If there are no
19929 partial specializations matching TARGET, then NULL_TREE is
19930 returned, indicating that the primary template should be used. */
19932 static tree
19933 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
19935 tree list = NULL_TREE;
19936 tree t;
19937 tree champ;
19938 int fate;
19939 bool ambiguous_p;
19940 tree outer_args = NULL_TREE;
19941 tree tmpl, args;
19943 if (TYPE_P (target))
19945 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
19946 tmpl = TI_TEMPLATE (tinfo);
19947 args = TI_ARGS (tinfo);
19949 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
19951 tmpl = TREE_OPERAND (target, 0);
19952 args = TREE_OPERAND (target, 1);
19954 else if (VAR_P (target))
19956 tree tinfo = DECL_TEMPLATE_INFO (target);
19957 tmpl = TI_TEMPLATE (tinfo);
19958 args = TI_ARGS (tinfo);
19960 else
19961 gcc_unreachable ();
19963 tree main_tmpl = most_general_template (tmpl);
19965 /* For determining which partial specialization to use, only the
19966 innermost args are interesting. */
19967 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19969 outer_args = strip_innermost_template_args (args, 1);
19970 args = INNERMOST_TEMPLATE_ARGS (args);
19973 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
19975 tree partial_spec_args;
19976 tree spec_args;
19977 tree spec_tmpl = TREE_VALUE (t);
19979 partial_spec_args = TREE_PURPOSE (t);
19981 ++processing_template_decl;
19983 if (outer_args)
19985 /* Discard the outer levels of args, and then substitute in the
19986 template args from the enclosing class. */
19987 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
19988 partial_spec_args = tsubst_template_args
19989 (partial_spec_args, outer_args, tf_none, NULL_TREE);
19991 /* And the same for the partial specialization TEMPLATE_DECL. */
19992 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
19995 partial_spec_args =
19996 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19997 partial_spec_args,
19998 tmpl, tf_none,
19999 /*require_all_args=*/true,
20000 /*use_default_args=*/true);
20002 --processing_template_decl;
20004 if (partial_spec_args == error_mark_node)
20005 return error_mark_node;
20006 if (spec_tmpl == error_mark_node)
20007 return error_mark_node;
20009 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
20010 spec_args = get_partial_spec_bindings (tmpl, parms,
20011 partial_spec_args,
20012 args);
20013 if (spec_args)
20015 if (outer_args)
20016 spec_args = add_to_template_args (outer_args, spec_args);
20018 /* Keep the candidate only if the constraints are satisfied,
20019 or if we're not compiling with concepts. */
20020 if (!flag_concepts || constraints_satisfied_p (spec_tmpl, spec_args))
20022 list = tree_cons (spec_args, TREE_VALUE (t), list);
20023 TREE_TYPE (list) = TREE_TYPE (t);
20028 if (! list)
20029 return NULL_TREE;
20031 ambiguous_p = false;
20032 t = list;
20033 champ = t;
20034 t = TREE_CHAIN (t);
20035 for (; t; t = TREE_CHAIN (t))
20037 fate = more_specialized_partial_spec (tmpl, champ, t);
20038 if (fate == 1)
20040 else
20042 if (fate == 0)
20044 t = TREE_CHAIN (t);
20045 if (! t)
20047 ambiguous_p = true;
20048 break;
20051 champ = t;
20055 if (!ambiguous_p)
20056 for (t = list; t && t != champ; t = TREE_CHAIN (t))
20058 fate = more_specialized_partial_spec (tmpl, champ, t);
20059 if (fate != 1)
20061 ambiguous_p = true;
20062 break;
20066 if (ambiguous_p)
20068 const char *str;
20069 char *spaces = NULL;
20070 if (!(complain & tf_error))
20071 return error_mark_node;
20072 if (TYPE_P (target))
20073 error ("ambiguous template instantiation for %q#T", target);
20074 else
20075 error ("ambiguous template instantiation for %q#D", target);
20076 str = ngettext ("candidate is:", "candidates are:", list_length (list));
20077 for (t = list; t; t = TREE_CHAIN (t))
20079 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
20080 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
20081 "%s %#S", spaces ? spaces : str, subst);
20082 spaces = spaces ? spaces : get_spaces (str);
20084 free (spaces);
20085 return error_mark_node;
20088 return champ;
20091 /* Explicitly instantiate DECL. */
20093 void
20094 do_decl_instantiation (tree decl, tree storage)
20096 tree result = NULL_TREE;
20097 int extern_p = 0;
20099 if (!decl || decl == error_mark_node)
20100 /* An error occurred, for which grokdeclarator has already issued
20101 an appropriate message. */
20102 return;
20103 else if (! DECL_LANG_SPECIFIC (decl))
20105 error ("explicit instantiation of non-template %q#D", decl);
20106 return;
20109 bool var_templ = (DECL_TEMPLATE_INFO (decl)
20110 && variable_template_p (DECL_TI_TEMPLATE (decl)));
20112 if (VAR_P (decl) && !var_templ)
20114 /* There is an asymmetry here in the way VAR_DECLs and
20115 FUNCTION_DECLs are handled by grokdeclarator. In the case of
20116 the latter, the DECL we get back will be marked as a
20117 template instantiation, and the appropriate
20118 DECL_TEMPLATE_INFO will be set up. This does not happen for
20119 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
20120 should handle VAR_DECLs as it currently handles
20121 FUNCTION_DECLs. */
20122 if (!DECL_CLASS_SCOPE_P (decl))
20124 error ("%qD is not a static data member of a class template", decl);
20125 return;
20127 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
20128 if (!result || !VAR_P (result))
20130 error ("no matching template for %qD found", decl);
20131 return;
20133 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
20135 error ("type %qT for explicit instantiation %qD does not match "
20136 "declared type %qT", TREE_TYPE (result), decl,
20137 TREE_TYPE (decl));
20138 return;
20141 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
20143 error ("explicit instantiation of %q#D", decl);
20144 return;
20146 else
20147 result = decl;
20149 /* Check for various error cases. Note that if the explicit
20150 instantiation is valid the RESULT will currently be marked as an
20151 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
20152 until we get here. */
20154 if (DECL_TEMPLATE_SPECIALIZATION (result))
20156 /* DR 259 [temp.spec].
20158 Both an explicit instantiation and a declaration of an explicit
20159 specialization shall not appear in a program unless the explicit
20160 instantiation follows a declaration of the explicit specialization.
20162 For a given set of template parameters, if an explicit
20163 instantiation of a template appears after a declaration of an
20164 explicit specialization for that template, the explicit
20165 instantiation has no effect. */
20166 return;
20168 else if (DECL_EXPLICIT_INSTANTIATION (result))
20170 /* [temp.spec]
20172 No program shall explicitly instantiate any template more
20173 than once.
20175 We check DECL_NOT_REALLY_EXTERN so as not to complain when
20176 the first instantiation was `extern' and the second is not,
20177 and EXTERN_P for the opposite case. */
20178 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
20179 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
20180 /* If an "extern" explicit instantiation follows an ordinary
20181 explicit instantiation, the template is instantiated. */
20182 if (extern_p)
20183 return;
20185 else if (!DECL_IMPLICIT_INSTANTIATION (result))
20187 error ("no matching template for %qD found", result);
20188 return;
20190 else if (!DECL_TEMPLATE_INFO (result))
20192 permerror (input_location, "explicit instantiation of non-template %q#D", result);
20193 return;
20196 if (storage == NULL_TREE)
20198 else if (storage == ridpointers[(int) RID_EXTERN])
20200 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
20201 pedwarn (input_location, OPT_Wpedantic,
20202 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
20203 "instantiations");
20204 extern_p = 1;
20206 else
20207 error ("storage class %qD applied to template instantiation", storage);
20209 check_explicit_instantiation_namespace (result);
20210 mark_decl_instantiated (result, extern_p);
20211 if (! extern_p)
20212 instantiate_decl (result, /*defer_ok=*/1,
20213 /*expl_inst_class_mem_p=*/false);
20216 static void
20217 mark_class_instantiated (tree t, int extern_p)
20219 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
20220 SET_CLASSTYPE_INTERFACE_KNOWN (t);
20221 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
20222 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
20223 if (! extern_p)
20225 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
20226 rest_of_type_compilation (t, 1);
20230 /* Called from do_type_instantiation through binding_table_foreach to
20231 do recursive instantiation for the type bound in ENTRY. */
20232 static void
20233 bt_instantiate_type_proc (binding_entry entry, void *data)
20235 tree storage = *(tree *) data;
20237 if (MAYBE_CLASS_TYPE_P (entry->type)
20238 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
20239 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
20242 /* Called from do_type_instantiation to instantiate a member
20243 (a member function or a static member variable) of an
20244 explicitly instantiated class template. */
20245 static void
20246 instantiate_class_member (tree decl, int extern_p)
20248 mark_decl_instantiated (decl, extern_p);
20249 if (! extern_p)
20250 instantiate_decl (decl, /*defer_ok=*/1,
20251 /*expl_inst_class_mem_p=*/true);
20254 /* Perform an explicit instantiation of template class T. STORAGE, if
20255 non-null, is the RID for extern, inline or static. COMPLAIN is
20256 nonzero if this is called from the parser, zero if called recursively,
20257 since the standard is unclear (as detailed below). */
20259 void
20260 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
20262 int extern_p = 0;
20263 int nomem_p = 0;
20264 int static_p = 0;
20265 int previous_instantiation_extern_p = 0;
20267 if (TREE_CODE (t) == TYPE_DECL)
20268 t = TREE_TYPE (t);
20270 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
20272 tree tmpl =
20273 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
20274 if (tmpl)
20275 error ("explicit instantiation of non-class template %qD", tmpl);
20276 else
20277 error ("explicit instantiation of non-template type %qT", t);
20278 return;
20281 complete_type (t);
20283 if (!COMPLETE_TYPE_P (t))
20285 if (complain & tf_error)
20286 error ("explicit instantiation of %q#T before definition of template",
20288 return;
20291 if (storage != NULL_TREE)
20293 if (!in_system_header_at (input_location))
20295 if (storage == ridpointers[(int) RID_EXTERN])
20297 if (cxx_dialect == cxx98)
20298 pedwarn (input_location, OPT_Wpedantic,
20299 "ISO C++ 1998 forbids the use of %<extern%> on "
20300 "explicit instantiations");
20302 else
20303 pedwarn (input_location, OPT_Wpedantic,
20304 "ISO C++ forbids the use of %qE"
20305 " on explicit instantiations", storage);
20308 if (storage == ridpointers[(int) RID_INLINE])
20309 nomem_p = 1;
20310 else if (storage == ridpointers[(int) RID_EXTERN])
20311 extern_p = 1;
20312 else if (storage == ridpointers[(int) RID_STATIC])
20313 static_p = 1;
20314 else
20316 error ("storage class %qD applied to template instantiation",
20317 storage);
20318 extern_p = 0;
20322 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
20324 /* DR 259 [temp.spec].
20326 Both an explicit instantiation and a declaration of an explicit
20327 specialization shall not appear in a program unless the explicit
20328 instantiation follows a declaration of the explicit specialization.
20330 For a given set of template parameters, if an explicit
20331 instantiation of a template appears after a declaration of an
20332 explicit specialization for that template, the explicit
20333 instantiation has no effect. */
20334 return;
20336 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
20338 /* [temp.spec]
20340 No program shall explicitly instantiate any template more
20341 than once.
20343 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
20344 instantiation was `extern'. If EXTERN_P then the second is.
20345 These cases are OK. */
20346 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
20348 if (!previous_instantiation_extern_p && !extern_p
20349 && (complain & tf_error))
20350 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
20352 /* If we've already instantiated the template, just return now. */
20353 if (!CLASSTYPE_INTERFACE_ONLY (t))
20354 return;
20357 check_explicit_instantiation_namespace (TYPE_NAME (t));
20358 mark_class_instantiated (t, extern_p);
20360 if (nomem_p)
20361 return;
20364 tree tmp;
20366 /* In contrast to implicit instantiation, where only the
20367 declarations, and not the definitions, of members are
20368 instantiated, we have here:
20370 [temp.explicit]
20372 The explicit instantiation of a class template specialization
20373 implies the instantiation of all of its members not
20374 previously explicitly specialized in the translation unit
20375 containing the explicit instantiation.
20377 Of course, we can't instantiate member template classes, since
20378 we don't have any arguments for them. Note that the standard
20379 is unclear on whether the instantiation of the members are
20380 *explicit* instantiations or not. However, the most natural
20381 interpretation is that it should be an explicit instantiation. */
20383 if (! static_p)
20384 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
20385 if (TREE_CODE (tmp) == FUNCTION_DECL
20386 && DECL_TEMPLATE_INSTANTIATION (tmp))
20387 instantiate_class_member (tmp, extern_p);
20389 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
20390 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
20391 instantiate_class_member (tmp, extern_p);
20393 if (CLASSTYPE_NESTED_UTDS (t))
20394 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
20395 bt_instantiate_type_proc, &storage);
20399 /* Given a function DECL, which is a specialization of TMPL, modify
20400 DECL to be a re-instantiation of TMPL with the same template
20401 arguments. TMPL should be the template into which tsubst'ing
20402 should occur for DECL, not the most general template.
20404 One reason for doing this is a scenario like this:
20406 template <class T>
20407 void f(const T&, int i);
20409 void g() { f(3, 7); }
20411 template <class T>
20412 void f(const T& t, const int i) { }
20414 Note that when the template is first instantiated, with
20415 instantiate_template, the resulting DECL will have no name for the
20416 first parameter, and the wrong type for the second. So, when we go
20417 to instantiate the DECL, we regenerate it. */
20419 static void
20420 regenerate_decl_from_template (tree decl, tree tmpl)
20422 /* The arguments used to instantiate DECL, from the most general
20423 template. */
20424 tree args;
20425 tree code_pattern;
20427 args = DECL_TI_ARGS (decl);
20428 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
20430 /* Make sure that we can see identifiers, and compute access
20431 correctly. */
20432 push_access_scope (decl);
20434 if (TREE_CODE (decl) == FUNCTION_DECL)
20436 tree decl_parm;
20437 tree pattern_parm;
20438 tree specs;
20439 int args_depth;
20440 int parms_depth;
20442 args_depth = TMPL_ARGS_DEPTH (args);
20443 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
20444 if (args_depth > parms_depth)
20445 args = get_innermost_template_args (args, parms_depth);
20447 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
20448 args, tf_error, NULL_TREE,
20449 /*defer_ok*/false);
20450 if (specs && specs != error_mark_node)
20451 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
20452 specs);
20454 /* Merge parameter declarations. */
20455 decl_parm = skip_artificial_parms_for (decl,
20456 DECL_ARGUMENTS (decl));
20457 pattern_parm
20458 = skip_artificial_parms_for (code_pattern,
20459 DECL_ARGUMENTS (code_pattern));
20460 while (decl_parm && !DECL_PACK_P (pattern_parm))
20462 tree parm_type;
20463 tree attributes;
20465 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
20466 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
20467 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
20468 NULL_TREE);
20469 parm_type = type_decays_to (parm_type);
20470 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
20471 TREE_TYPE (decl_parm) = parm_type;
20472 attributes = DECL_ATTRIBUTES (pattern_parm);
20473 if (DECL_ATTRIBUTES (decl_parm) != attributes)
20475 DECL_ATTRIBUTES (decl_parm) = attributes;
20476 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
20478 decl_parm = DECL_CHAIN (decl_parm);
20479 pattern_parm = DECL_CHAIN (pattern_parm);
20481 /* Merge any parameters that match with the function parameter
20482 pack. */
20483 if (pattern_parm && DECL_PACK_P (pattern_parm))
20485 int i, len;
20486 tree expanded_types;
20487 /* Expand the TYPE_PACK_EXPANSION that provides the types for
20488 the parameters in this function parameter pack. */
20489 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
20490 args, tf_error, NULL_TREE);
20491 len = TREE_VEC_LENGTH (expanded_types);
20492 for (i = 0; i < len; i++)
20494 tree parm_type;
20495 tree attributes;
20497 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
20498 /* Rename the parameter to include the index. */
20499 DECL_NAME (decl_parm) =
20500 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
20501 parm_type = TREE_VEC_ELT (expanded_types, i);
20502 parm_type = type_decays_to (parm_type);
20503 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
20504 TREE_TYPE (decl_parm) = parm_type;
20505 attributes = DECL_ATTRIBUTES (pattern_parm);
20506 if (DECL_ATTRIBUTES (decl_parm) != attributes)
20508 DECL_ATTRIBUTES (decl_parm) = attributes;
20509 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
20511 decl_parm = DECL_CHAIN (decl_parm);
20514 /* Merge additional specifiers from the CODE_PATTERN. */
20515 if (DECL_DECLARED_INLINE_P (code_pattern)
20516 && !DECL_DECLARED_INLINE_P (decl))
20517 DECL_DECLARED_INLINE_P (decl) = 1;
20519 else if (VAR_P (decl))
20521 DECL_INITIAL (decl) =
20522 tsubst_expr (DECL_INITIAL (code_pattern), args,
20523 tf_error, DECL_TI_TEMPLATE (decl),
20524 /*integral_constant_expression_p=*/false);
20525 if (VAR_HAD_UNKNOWN_BOUND (decl))
20526 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
20527 tf_error, DECL_TI_TEMPLATE (decl));
20529 else
20530 gcc_unreachable ();
20532 pop_access_scope (decl);
20535 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
20536 substituted to get DECL. */
20538 tree
20539 template_for_substitution (tree decl)
20541 tree tmpl = DECL_TI_TEMPLATE (decl);
20543 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
20544 for the instantiation. This is not always the most general
20545 template. Consider, for example:
20547 template <class T>
20548 struct S { template <class U> void f();
20549 template <> void f<int>(); };
20551 and an instantiation of S<double>::f<int>. We want TD to be the
20552 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
20553 while (/* An instantiation cannot have a definition, so we need a
20554 more general template. */
20555 DECL_TEMPLATE_INSTANTIATION (tmpl)
20556 /* We must also deal with friend templates. Given:
20558 template <class T> struct S {
20559 template <class U> friend void f() {};
20562 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
20563 so far as the language is concerned, but that's still
20564 where we get the pattern for the instantiation from. On
20565 other hand, if the definition comes outside the class, say:
20567 template <class T> struct S {
20568 template <class U> friend void f();
20570 template <class U> friend void f() {}
20572 we don't need to look any further. That's what the check for
20573 DECL_INITIAL is for. */
20574 || (TREE_CODE (decl) == FUNCTION_DECL
20575 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
20576 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
20578 /* The present template, TD, should not be a definition. If it
20579 were a definition, we should be using it! Note that we
20580 cannot restructure the loop to just keep going until we find
20581 a template with a definition, since that might go too far if
20582 a specialization was declared, but not defined. */
20584 /* Fetch the more general template. */
20585 tmpl = DECL_TI_TEMPLATE (tmpl);
20588 return tmpl;
20591 /* Returns true if we need to instantiate this template instance even if we
20592 know we aren't going to emit it. */
20594 bool
20595 always_instantiate_p (tree decl)
20597 /* We always instantiate inline functions so that we can inline them. An
20598 explicit instantiation declaration prohibits implicit instantiation of
20599 non-inline functions. With high levels of optimization, we would
20600 normally inline non-inline functions -- but we're not allowed to do
20601 that for "extern template" functions. Therefore, we check
20602 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
20603 return ((TREE_CODE (decl) == FUNCTION_DECL
20604 && (DECL_DECLARED_INLINE_P (decl)
20605 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
20606 /* And we need to instantiate static data members so that
20607 their initializers are available in integral constant
20608 expressions. */
20609 || (VAR_P (decl)
20610 && decl_maybe_constant_var_p (decl)));
20613 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
20614 instantiate it now, modifying TREE_TYPE (fn). */
20616 void
20617 maybe_instantiate_noexcept (tree fn)
20619 tree fntype, spec, noex, clone;
20621 /* Don't instantiate a noexcept-specification from template context. */
20622 if (processing_template_decl)
20623 return;
20625 if (DECL_CLONED_FUNCTION_P (fn))
20626 fn = DECL_CLONED_FUNCTION (fn);
20627 fntype = TREE_TYPE (fn);
20628 spec = TYPE_RAISES_EXCEPTIONS (fntype);
20630 if (!spec || !TREE_PURPOSE (spec))
20631 return;
20633 noex = TREE_PURPOSE (spec);
20635 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
20637 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
20638 spec = get_defaulted_eh_spec (fn);
20639 else if (push_tinst_level (fn))
20641 push_access_scope (fn);
20642 push_deferring_access_checks (dk_no_deferred);
20643 input_location = DECL_SOURCE_LOCATION (fn);
20644 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
20645 DEFERRED_NOEXCEPT_ARGS (noex),
20646 tf_warning_or_error, fn,
20647 /*function_p=*/false,
20648 /*integral_constant_expression_p=*/true);
20649 pop_deferring_access_checks ();
20650 pop_access_scope (fn);
20651 pop_tinst_level ();
20652 spec = build_noexcept_spec (noex, tf_warning_or_error);
20653 if (spec == error_mark_node)
20654 spec = noexcept_false_spec;
20656 else
20657 spec = noexcept_false_spec;
20659 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
20662 FOR_EACH_CLONE (clone, fn)
20664 if (TREE_TYPE (clone) == fntype)
20665 TREE_TYPE (clone) = TREE_TYPE (fn);
20666 else
20667 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
20671 /* Produce the definition of D, a _DECL generated from a template. If
20672 DEFER_OK is nonzero, then we don't have to actually do the
20673 instantiation now; we just have to do it sometime. Normally it is
20674 an error if this is an explicit instantiation but D is undefined.
20675 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
20676 explicitly instantiated class template. */
20678 tree
20679 instantiate_decl (tree d, int defer_ok,
20680 bool expl_inst_class_mem_p)
20682 tree tmpl = DECL_TI_TEMPLATE (d);
20683 tree gen_args;
20684 tree args;
20685 tree td;
20686 tree code_pattern;
20687 tree spec;
20688 tree gen_tmpl;
20689 bool pattern_defined;
20690 location_t saved_loc = input_location;
20691 int saved_unevaluated_operand = cp_unevaluated_operand;
20692 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20693 bool external_p;
20694 bool deleted_p;
20695 tree fn_context;
20696 bool nested;
20698 /* This function should only be used to instantiate templates for
20699 functions and static member variables. */
20700 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
20702 /* A concept is never instantiated. */
20703 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
20705 /* Variables are never deferred; if instantiation is required, they
20706 are instantiated right away. That allows for better code in the
20707 case that an expression refers to the value of the variable --
20708 if the variable has a constant value the referring expression can
20709 take advantage of that fact. */
20710 if (VAR_P (d)
20711 || DECL_DECLARED_CONSTEXPR_P (d))
20712 defer_ok = 0;
20714 /* Don't instantiate cloned functions. Instead, instantiate the
20715 functions they cloned. */
20716 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
20717 d = DECL_CLONED_FUNCTION (d);
20719 if (DECL_TEMPLATE_INSTANTIATED (d)
20720 || (TREE_CODE (d) == FUNCTION_DECL
20721 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
20722 || DECL_TEMPLATE_SPECIALIZATION (d))
20723 /* D has already been instantiated or explicitly specialized, so
20724 there's nothing for us to do here.
20726 It might seem reasonable to check whether or not D is an explicit
20727 instantiation, and, if so, stop here. But when an explicit
20728 instantiation is deferred until the end of the compilation,
20729 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
20730 the instantiation. */
20731 return d;
20733 /* Check to see whether we know that this template will be
20734 instantiated in some other file, as with "extern template"
20735 extension. */
20736 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
20738 /* In general, we do not instantiate such templates. */
20739 if (external_p && !always_instantiate_p (d))
20740 return d;
20742 gen_tmpl = most_general_template (tmpl);
20743 gen_args = DECL_TI_ARGS (d);
20745 if (tmpl != gen_tmpl)
20746 /* We should already have the extra args. */
20747 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
20748 == TMPL_ARGS_DEPTH (gen_args));
20749 /* And what's in the hash table should match D. */
20750 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
20751 || spec == NULL_TREE);
20753 /* This needs to happen before any tsubsting. */
20754 if (! push_tinst_level (d))
20755 return d;
20757 timevar_push (TV_TEMPLATE_INST);
20759 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
20760 for the instantiation. */
20761 td = template_for_substitution (d);
20762 code_pattern = DECL_TEMPLATE_RESULT (td);
20764 /* We should never be trying to instantiate a member of a class
20765 template or partial specialization. */
20766 gcc_assert (d != code_pattern);
20768 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
20769 || DECL_TEMPLATE_SPECIALIZATION (td))
20770 /* In the case of a friend template whose definition is provided
20771 outside the class, we may have too many arguments. Drop the
20772 ones we don't need. The same is true for specializations. */
20773 args = get_innermost_template_args
20774 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
20775 else
20776 args = gen_args;
20778 if (TREE_CODE (d) == FUNCTION_DECL)
20780 deleted_p = DECL_DELETED_FN (code_pattern);
20781 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
20782 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
20783 || deleted_p);
20785 else
20787 deleted_p = false;
20788 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
20791 /* We may be in the middle of deferred access check. Disable it now. */
20792 push_deferring_access_checks (dk_no_deferred);
20794 /* Unless an explicit instantiation directive has already determined
20795 the linkage of D, remember that a definition is available for
20796 this entity. */
20797 if (pattern_defined
20798 && !DECL_INTERFACE_KNOWN (d)
20799 && !DECL_NOT_REALLY_EXTERN (d))
20800 mark_definable (d);
20802 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
20803 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
20804 input_location = DECL_SOURCE_LOCATION (d);
20806 /* If D is a member of an explicitly instantiated class template,
20807 and no definition is available, treat it like an implicit
20808 instantiation. */
20809 if (!pattern_defined && expl_inst_class_mem_p
20810 && DECL_EXPLICIT_INSTANTIATION (d))
20812 /* Leave linkage flags alone on instantiations with anonymous
20813 visibility. */
20814 if (TREE_PUBLIC (d))
20816 DECL_NOT_REALLY_EXTERN (d) = 0;
20817 DECL_INTERFACE_KNOWN (d) = 0;
20819 SET_DECL_IMPLICIT_INSTANTIATION (d);
20822 /* Defer all other templates, unless we have been explicitly
20823 forbidden from doing so. */
20824 if (/* If there is no definition, we cannot instantiate the
20825 template. */
20826 ! pattern_defined
20827 /* If it's OK to postpone instantiation, do so. */
20828 || defer_ok
20829 /* If this is a static data member that will be defined
20830 elsewhere, we don't want to instantiate the entire data
20831 member, but we do want to instantiate the initializer so that
20832 we can substitute that elsewhere. */
20833 || (external_p && VAR_P (d))
20834 /* Handle here a deleted function too, avoid generating
20835 its body (c++/61080). */
20836 || deleted_p)
20838 /* The definition of the static data member is now required so
20839 we must substitute the initializer. */
20840 if (VAR_P (d)
20841 && !DECL_INITIAL (d)
20842 && DECL_INITIAL (code_pattern))
20844 tree ns;
20845 tree init;
20846 bool const_init = false;
20847 bool enter_context = DECL_CLASS_SCOPE_P (d);
20849 ns = decl_namespace_context (d);
20850 push_nested_namespace (ns);
20851 if (enter_context)
20852 push_nested_class (DECL_CONTEXT (d));
20853 init = tsubst_expr (DECL_INITIAL (code_pattern),
20854 args,
20855 tf_warning_or_error, NULL_TREE,
20856 /*integral_constant_expression_p=*/false);
20857 /* If instantiating the initializer involved instantiating this
20858 again, don't call cp_finish_decl twice. */
20859 if (!DECL_INITIAL (d))
20861 /* Make sure the initializer is still constant, in case of
20862 circular dependency (template/instantiate6.C). */
20863 const_init
20864 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
20865 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
20866 /*asmspec_tree=*/NULL_TREE,
20867 LOOKUP_ONLYCONVERTING);
20869 if (enter_context)
20870 pop_nested_class ();
20871 pop_nested_namespace (ns);
20874 /* We restore the source position here because it's used by
20875 add_pending_template. */
20876 input_location = saved_loc;
20878 if (at_eof && !pattern_defined
20879 && DECL_EXPLICIT_INSTANTIATION (d)
20880 && DECL_NOT_REALLY_EXTERN (d))
20881 /* [temp.explicit]
20883 The definition of a non-exported function template, a
20884 non-exported member function template, or a non-exported
20885 member function or static data member of a class template
20886 shall be present in every translation unit in which it is
20887 explicitly instantiated. */
20888 permerror (input_location, "explicit instantiation of %qD "
20889 "but no definition available", d);
20891 /* If we're in unevaluated context, we just wanted to get the
20892 constant value; this isn't an odr use, so don't queue
20893 a full instantiation. */
20894 if (cp_unevaluated_operand != 0)
20895 goto out;
20896 /* ??? Historically, we have instantiated inline functions, even
20897 when marked as "extern template". */
20898 if (!(external_p && VAR_P (d)))
20899 add_pending_template (d);
20900 goto out;
20902 /* Tell the repository that D is available in this translation unit
20903 -- and see if it is supposed to be instantiated here. */
20904 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
20906 /* In a PCH file, despite the fact that the repository hasn't
20907 requested instantiation in the PCH it is still possible that
20908 an instantiation will be required in a file that includes the
20909 PCH. */
20910 if (pch_file)
20911 add_pending_template (d);
20912 /* Instantiate inline functions so that the inliner can do its
20913 job, even though we'll not be emitting a copy of this
20914 function. */
20915 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
20916 goto out;
20919 fn_context = decl_function_context (d);
20920 nested = (current_function_decl != NULL_TREE);
20921 if (!fn_context)
20922 push_to_top_level ();
20923 else
20925 if (nested)
20926 push_function_context ();
20927 cp_unevaluated_operand = 0;
20928 c_inhibit_evaluation_warnings = 0;
20931 /* Mark D as instantiated so that recursive calls to
20932 instantiate_decl do not try to instantiate it again. */
20933 DECL_TEMPLATE_INSTANTIATED (d) = 1;
20935 /* Regenerate the declaration in case the template has been modified
20936 by a subsequent redeclaration. */
20937 regenerate_decl_from_template (d, td);
20939 /* We already set the file and line above. Reset them now in case
20940 they changed as a result of calling regenerate_decl_from_template. */
20941 input_location = DECL_SOURCE_LOCATION (d);
20943 if (VAR_P (d))
20945 tree init;
20946 bool const_init = false;
20948 /* Clear out DECL_RTL; whatever was there before may not be right
20949 since we've reset the type of the declaration. */
20950 SET_DECL_RTL (d, NULL);
20951 DECL_IN_AGGR_P (d) = 0;
20953 /* The initializer is placed in DECL_INITIAL by
20954 regenerate_decl_from_template so we don't need to
20955 push/pop_access_scope again here. Pull it out so that
20956 cp_finish_decl can process it. */
20957 init = DECL_INITIAL (d);
20958 DECL_INITIAL (d) = NULL_TREE;
20959 DECL_INITIALIZED_P (d) = 0;
20961 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
20962 initializer. That function will defer actual emission until
20963 we have a chance to determine linkage. */
20964 DECL_EXTERNAL (d) = 0;
20966 /* Enter the scope of D so that access-checking works correctly. */
20967 bool enter_context = DECL_CLASS_SCOPE_P (d);
20968 if (enter_context)
20969 push_nested_class (DECL_CONTEXT (d));
20971 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
20972 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
20974 if (enter_context)
20975 pop_nested_class ();
20977 if (variable_template_p (td))
20978 note_variable_template_instantiation (d);
20980 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
20981 synthesize_method (d);
20982 else if (TREE_CODE (d) == FUNCTION_DECL)
20984 hash_map<tree, tree> *saved_local_specializations;
20985 tree subst_decl;
20986 tree tmpl_parm;
20987 tree spec_parm;
20988 tree block = NULL_TREE;
20990 /* Save away the current list, in case we are instantiating one
20991 template from within the body of another. */
20992 saved_local_specializations = local_specializations;
20994 /* Set up the list of local specializations. */
20995 local_specializations = new hash_map<tree, tree>;
20997 /* Set up context. */
20998 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20999 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
21000 block = push_stmt_list ();
21001 else
21002 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
21004 /* Some typedefs referenced from within the template code need to be
21005 access checked at template instantiation time, i.e now. These
21006 types were added to the template at parsing time. Let's get those
21007 and perform the access checks then. */
21008 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
21009 gen_args);
21011 /* Create substitution entries for the parameters. */
21012 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
21013 tmpl_parm = DECL_ARGUMENTS (subst_decl);
21014 spec_parm = DECL_ARGUMENTS (d);
21015 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
21017 register_local_specialization (spec_parm, tmpl_parm);
21018 spec_parm = skip_artificial_parms_for (d, spec_parm);
21019 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
21021 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
21023 if (!DECL_PACK_P (tmpl_parm))
21025 register_local_specialization (spec_parm, tmpl_parm);
21026 spec_parm = DECL_CHAIN (spec_parm);
21028 else
21030 /* Register the (value) argument pack as a specialization of
21031 TMPL_PARM, then move on. */
21032 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
21033 register_local_specialization (argpack, tmpl_parm);
21036 gcc_assert (!spec_parm);
21038 /* Substitute into the body of the function. */
21039 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
21040 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
21041 tf_warning_or_error, tmpl);
21042 else
21044 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
21045 tf_warning_or_error, tmpl,
21046 /*integral_constant_expression_p=*/false);
21048 /* Set the current input_location to the end of the function
21049 so that finish_function knows where we are. */
21050 input_location
21051 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
21053 /* Remember if we saw an infinite loop in the template. */
21054 current_function_infinite_loop
21055 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
21058 /* We don't need the local specializations any more. */
21059 delete local_specializations;
21060 local_specializations = saved_local_specializations;
21062 /* Finish the function. */
21063 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
21064 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
21065 DECL_SAVED_TREE (d) = pop_stmt_list (block);
21066 else
21068 d = finish_function (0);
21069 expand_or_defer_fn (d);
21072 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
21073 cp_check_omp_declare_reduction (d);
21076 /* We're not deferring instantiation any more. */
21077 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
21079 if (!fn_context)
21080 pop_from_top_level ();
21081 else if (nested)
21082 pop_function_context ();
21084 out:
21085 input_location = saved_loc;
21086 cp_unevaluated_operand = saved_unevaluated_operand;
21087 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21088 pop_deferring_access_checks ();
21089 pop_tinst_level ();
21091 timevar_pop (TV_TEMPLATE_INST);
21093 return d;
21096 /* Run through the list of templates that we wish we could
21097 instantiate, and instantiate any we can. RETRIES is the
21098 number of times we retry pending template instantiation. */
21100 void
21101 instantiate_pending_templates (int retries)
21103 int reconsider;
21104 location_t saved_loc = input_location;
21106 /* Instantiating templates may trigger vtable generation. This in turn
21107 may require further template instantiations. We place a limit here
21108 to avoid infinite loop. */
21109 if (pending_templates && retries >= max_tinst_depth)
21111 tree decl = pending_templates->tinst->decl;
21113 fatal_error (input_location,
21114 "template instantiation depth exceeds maximum of %d"
21115 " instantiating %q+D, possibly from virtual table generation"
21116 " (use -ftemplate-depth= to increase the maximum)",
21117 max_tinst_depth, decl);
21118 if (TREE_CODE (decl) == FUNCTION_DECL)
21119 /* Pretend that we defined it. */
21120 DECL_INITIAL (decl) = error_mark_node;
21121 return;
21126 struct pending_template **t = &pending_templates;
21127 struct pending_template *last = NULL;
21128 reconsider = 0;
21129 while (*t)
21131 tree instantiation = reopen_tinst_level ((*t)->tinst);
21132 bool complete = false;
21134 if (TYPE_P (instantiation))
21136 tree fn;
21138 if (!COMPLETE_TYPE_P (instantiation))
21140 instantiate_class_template (instantiation);
21141 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
21142 for (fn = TYPE_METHODS (instantiation);
21144 fn = TREE_CHAIN (fn))
21145 if (! DECL_ARTIFICIAL (fn))
21146 instantiate_decl (fn,
21147 /*defer_ok=*/0,
21148 /*expl_inst_class_mem_p=*/false);
21149 if (COMPLETE_TYPE_P (instantiation))
21150 reconsider = 1;
21153 complete = COMPLETE_TYPE_P (instantiation);
21155 else
21157 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
21158 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
21160 instantiation
21161 = instantiate_decl (instantiation,
21162 /*defer_ok=*/0,
21163 /*expl_inst_class_mem_p=*/false);
21164 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
21165 reconsider = 1;
21168 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
21169 || DECL_TEMPLATE_INSTANTIATED (instantiation));
21172 if (complete)
21173 /* If INSTANTIATION has been instantiated, then we don't
21174 need to consider it again in the future. */
21175 *t = (*t)->next;
21176 else
21178 last = *t;
21179 t = &(*t)->next;
21181 tinst_depth = 0;
21182 current_tinst_level = NULL;
21184 last_pending_template = last;
21186 while (reconsider);
21188 input_location = saved_loc;
21191 /* Substitute ARGVEC into T, which is a list of initializers for
21192 either base class or a non-static data member. The TREE_PURPOSEs
21193 are DECLs, and the TREE_VALUEs are the initializer values. Used by
21194 instantiate_decl. */
21196 static tree
21197 tsubst_initializer_list (tree t, tree argvec)
21199 tree inits = NULL_TREE;
21201 for (; t; t = TREE_CHAIN (t))
21203 tree decl;
21204 tree init;
21205 tree expanded_bases = NULL_TREE;
21206 tree expanded_arguments = NULL_TREE;
21207 int i, len = 1;
21209 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
21211 tree expr;
21212 tree arg;
21214 /* Expand the base class expansion type into separate base
21215 classes. */
21216 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
21217 tf_warning_or_error,
21218 NULL_TREE);
21219 if (expanded_bases == error_mark_node)
21220 continue;
21222 /* We'll be building separate TREE_LISTs of arguments for
21223 each base. */
21224 len = TREE_VEC_LENGTH (expanded_bases);
21225 expanded_arguments = make_tree_vec (len);
21226 for (i = 0; i < len; i++)
21227 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
21229 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
21230 expand each argument in the TREE_VALUE of t. */
21231 expr = make_node (EXPR_PACK_EXPANSION);
21232 PACK_EXPANSION_LOCAL_P (expr) = true;
21233 PACK_EXPANSION_PARAMETER_PACKS (expr) =
21234 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
21236 if (TREE_VALUE (t) == void_type_node)
21237 /* VOID_TYPE_NODE is used to indicate
21238 value-initialization. */
21240 for (i = 0; i < len; i++)
21241 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
21243 else
21245 /* Substitute parameter packs into each argument in the
21246 TREE_LIST. */
21247 in_base_initializer = 1;
21248 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
21250 tree expanded_exprs;
21252 /* Expand the argument. */
21253 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
21254 expanded_exprs
21255 = tsubst_pack_expansion (expr, argvec,
21256 tf_warning_or_error,
21257 NULL_TREE);
21258 if (expanded_exprs == error_mark_node)
21259 continue;
21261 /* Prepend each of the expanded expressions to the
21262 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
21263 for (i = 0; i < len; i++)
21265 TREE_VEC_ELT (expanded_arguments, i) =
21266 tree_cons (NULL_TREE,
21267 TREE_VEC_ELT (expanded_exprs, i),
21268 TREE_VEC_ELT (expanded_arguments, i));
21271 in_base_initializer = 0;
21273 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
21274 since we built them backwards. */
21275 for (i = 0; i < len; i++)
21277 TREE_VEC_ELT (expanded_arguments, i) =
21278 nreverse (TREE_VEC_ELT (expanded_arguments, i));
21283 for (i = 0; i < len; ++i)
21285 if (expanded_bases)
21287 decl = TREE_VEC_ELT (expanded_bases, i);
21288 decl = expand_member_init (decl);
21289 init = TREE_VEC_ELT (expanded_arguments, i);
21291 else
21293 tree tmp;
21294 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
21295 tf_warning_or_error, NULL_TREE);
21297 decl = expand_member_init (decl);
21298 if (decl && !DECL_P (decl))
21299 in_base_initializer = 1;
21301 init = TREE_VALUE (t);
21302 tmp = init;
21303 if (init != void_type_node)
21304 init = tsubst_expr (init, argvec,
21305 tf_warning_or_error, NULL_TREE,
21306 /*integral_constant_expression_p=*/false);
21307 if (init == NULL_TREE && tmp != NULL_TREE)
21308 /* If we had an initializer but it instantiated to nothing,
21309 value-initialize the object. This will only occur when
21310 the initializer was a pack expansion where the parameter
21311 packs used in that expansion were of length zero. */
21312 init = void_type_node;
21313 in_base_initializer = 0;
21316 if (decl)
21318 init = build_tree_list (decl, init);
21319 TREE_CHAIN (init) = inits;
21320 inits = init;
21324 return inits;
21327 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
21329 static void
21330 set_current_access_from_decl (tree decl)
21332 if (TREE_PRIVATE (decl))
21333 current_access_specifier = access_private_node;
21334 else if (TREE_PROTECTED (decl))
21335 current_access_specifier = access_protected_node;
21336 else
21337 current_access_specifier = access_public_node;
21340 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
21341 is the instantiation (which should have been created with
21342 start_enum) and ARGS are the template arguments to use. */
21344 static void
21345 tsubst_enum (tree tag, tree newtag, tree args)
21347 tree e;
21349 if (SCOPED_ENUM_P (newtag))
21350 begin_scope (sk_scoped_enum, newtag);
21352 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
21354 tree value;
21355 tree decl;
21357 decl = TREE_VALUE (e);
21358 /* Note that in a template enum, the TREE_VALUE is the
21359 CONST_DECL, not the corresponding INTEGER_CST. */
21360 value = tsubst_expr (DECL_INITIAL (decl),
21361 args, tf_warning_or_error, NULL_TREE,
21362 /*integral_constant_expression_p=*/true);
21364 /* Give this enumeration constant the correct access. */
21365 set_current_access_from_decl (decl);
21367 /* Actually build the enumerator itself. */
21368 build_enumerator
21369 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
21372 if (SCOPED_ENUM_P (newtag))
21373 finish_scope ();
21375 finish_enum_value_list (newtag);
21376 finish_enum (newtag);
21378 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
21379 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
21382 /* DECL is a FUNCTION_DECL that is a template specialization. Return
21383 its type -- but without substituting the innermost set of template
21384 arguments. So, innermost set of template parameters will appear in
21385 the type. */
21387 tree
21388 get_mostly_instantiated_function_type (tree decl)
21390 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
21391 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
21394 /* Return truthvalue if we're processing a template different from
21395 the last one involved in diagnostics. */
21396 bool
21397 problematic_instantiation_changed (void)
21399 return current_tinst_level != last_error_tinst_level;
21402 /* Remember current template involved in diagnostics. */
21403 void
21404 record_last_problematic_instantiation (void)
21406 last_error_tinst_level = current_tinst_level;
21409 struct tinst_level *
21410 current_instantiation (void)
21412 return current_tinst_level;
21415 /* Return TRUE if current_function_decl is being instantiated, false
21416 otherwise. */
21418 bool
21419 instantiating_current_function_p (void)
21421 return (current_instantiation ()
21422 && current_instantiation ()->decl == current_function_decl);
21425 /* [temp.param] Check that template non-type parm TYPE is of an allowable
21426 type. Return zero for ok, nonzero for disallowed. Issue error and
21427 warning messages under control of COMPLAIN. */
21429 static int
21430 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
21432 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
21433 return 0;
21434 else if (POINTER_TYPE_P (type))
21435 return 0;
21436 else if (TYPE_PTRMEM_P (type))
21437 return 0;
21438 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
21439 return 0;
21440 else if (TREE_CODE (type) == TYPENAME_TYPE)
21441 return 0;
21442 else if (TREE_CODE (type) == DECLTYPE_TYPE)
21443 return 0;
21444 else if (TREE_CODE (type) == NULLPTR_TYPE)
21445 return 0;
21447 if (complain & tf_error)
21449 if (type == error_mark_node)
21450 inform (input_location, "invalid template non-type parameter");
21451 else
21452 error ("%q#T is not a valid type for a template non-type parameter",
21453 type);
21455 return 1;
21458 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
21459 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
21461 static bool
21462 dependent_type_p_r (tree type)
21464 tree scope;
21466 /* [temp.dep.type]
21468 A type is dependent if it is:
21470 -- a template parameter. Template template parameters are types
21471 for us (since TYPE_P holds true for them) so we handle
21472 them here. */
21473 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21474 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
21475 return true;
21476 /* -- a qualified-id with a nested-name-specifier which contains a
21477 class-name that names a dependent type or whose unqualified-id
21478 names a dependent type. */
21479 if (TREE_CODE (type) == TYPENAME_TYPE)
21480 return true;
21481 /* -- a cv-qualified type where the cv-unqualified type is
21482 dependent.
21483 No code is necessary for this bullet; the code below handles
21484 cv-qualified types, and we don't want to strip aliases with
21485 TYPE_MAIN_VARIANT because of DR 1558. */
21486 /* -- a compound type constructed from any dependent type. */
21487 if (TYPE_PTRMEM_P (type))
21488 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
21489 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
21490 (type)));
21491 else if (TYPE_PTR_P (type)
21492 || TREE_CODE (type) == REFERENCE_TYPE)
21493 return dependent_type_p (TREE_TYPE (type));
21494 else if (TREE_CODE (type) == FUNCTION_TYPE
21495 || TREE_CODE (type) == METHOD_TYPE)
21497 tree arg_type;
21499 if (dependent_type_p (TREE_TYPE (type)))
21500 return true;
21501 for (arg_type = TYPE_ARG_TYPES (type);
21502 arg_type;
21503 arg_type = TREE_CHAIN (arg_type))
21504 if (dependent_type_p (TREE_VALUE (arg_type)))
21505 return true;
21506 return false;
21508 /* -- an array type constructed from any dependent type or whose
21509 size is specified by a constant expression that is
21510 value-dependent.
21512 We checked for type- and value-dependence of the bounds in
21513 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
21514 if (TREE_CODE (type) == ARRAY_TYPE)
21516 if (TYPE_DOMAIN (type)
21517 && dependent_type_p (TYPE_DOMAIN (type)))
21518 return true;
21519 return dependent_type_p (TREE_TYPE (type));
21522 /* -- a template-id in which either the template name is a template
21523 parameter ... */
21524 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21525 return true;
21526 /* ... or any of the template arguments is a dependent type or
21527 an expression that is type-dependent or value-dependent. */
21528 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
21529 && (any_dependent_template_arguments_p
21530 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
21531 return true;
21532 /* For an alias template specialization, check the arguments both to the
21533 class template and the alias template. */
21534 else if (alias_template_specialization_p (type)
21535 && (any_dependent_template_arguments_p
21536 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (type)))))
21537 return true;
21539 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
21540 dependent; if the argument of the `typeof' expression is not
21541 type-dependent, then it should already been have resolved. */
21542 if (TREE_CODE (type) == TYPEOF_TYPE
21543 || TREE_CODE (type) == DECLTYPE_TYPE
21544 || TREE_CODE (type) == UNDERLYING_TYPE)
21545 return true;
21547 /* A template argument pack is dependent if any of its packed
21548 arguments are. */
21549 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
21551 tree args = ARGUMENT_PACK_ARGS (type);
21552 int i, len = TREE_VEC_LENGTH (args);
21553 for (i = 0; i < len; ++i)
21554 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
21555 return true;
21558 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
21559 be template parameters. */
21560 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
21561 return true;
21563 /* The standard does not specifically mention types that are local
21564 to template functions or local classes, but they should be
21565 considered dependent too. For example:
21567 template <int I> void f() {
21568 enum E { a = I };
21569 S<sizeof (E)> s;
21572 The size of `E' cannot be known until the value of `I' has been
21573 determined. Therefore, `E' must be considered dependent. */
21574 scope = TYPE_CONTEXT (type);
21575 if (scope && TYPE_P (scope))
21576 return dependent_type_p (scope);
21577 /* Don't use type_dependent_expression_p here, as it can lead
21578 to infinite recursion trying to determine whether a lambda
21579 nested in a lambda is dependent (c++/47687). */
21580 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
21581 && DECL_LANG_SPECIFIC (scope)
21582 && DECL_TEMPLATE_INFO (scope)
21583 && (any_dependent_template_arguments_p
21584 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
21585 return true;
21587 /* Other types are non-dependent. */
21588 return false;
21591 /* Returns TRUE if TYPE is dependent, in the sense of
21592 [temp.dep.type]. Note that a NULL type is considered dependent. */
21594 bool
21595 dependent_type_p (tree type)
21597 /* If there are no template parameters in scope, then there can't be
21598 any dependent types. */
21599 if (!processing_template_decl)
21601 /* If we are not processing a template, then nobody should be
21602 providing us with a dependent type. */
21603 gcc_assert (type);
21604 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
21605 return false;
21608 /* If the type is NULL, we have not computed a type for the entity
21609 in question; in that case, the type is dependent. */
21610 if (!type)
21611 return true;
21613 /* Erroneous types can be considered non-dependent. */
21614 if (type == error_mark_node)
21615 return false;
21617 /* If we have not already computed the appropriate value for TYPE,
21618 do so now. */
21619 if (!TYPE_DEPENDENT_P_VALID (type))
21621 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
21622 TYPE_DEPENDENT_P_VALID (type) = 1;
21625 return TYPE_DEPENDENT_P (type);
21628 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
21629 lookup. In other words, a dependent type that is not the current
21630 instantiation. */
21632 bool
21633 dependent_scope_p (tree scope)
21635 return (scope && TYPE_P (scope) && dependent_type_p (scope)
21636 && !currently_open_class (scope));
21639 /* T is a SCOPE_REF; return whether we need to consider it
21640 instantiation-dependent so that we can check access at instantiation
21641 time even though we know which member it resolves to. */
21643 static bool
21644 instantiation_dependent_scope_ref_p (tree t)
21646 if (DECL_P (TREE_OPERAND (t, 1))
21647 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
21648 && accessible_in_template_p (TREE_OPERAND (t, 0),
21649 TREE_OPERAND (t, 1)))
21650 return false;
21651 else
21652 return true;
21655 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
21656 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
21657 expression. */
21659 /* Note that this predicate is not appropriate for general expressions;
21660 only constant expressions (that satisfy potential_constant_expression)
21661 can be tested for value dependence. */
21663 bool
21664 value_dependent_expression_p (tree expression)
21666 if (!processing_template_decl)
21667 return false;
21669 /* A name declared with a dependent type. */
21670 if (DECL_P (expression) && type_dependent_expression_p (expression))
21671 return true;
21673 switch (TREE_CODE (expression))
21675 case IDENTIFIER_NODE:
21676 /* A name that has not been looked up -- must be dependent. */
21677 return true;
21679 case TEMPLATE_PARM_INDEX:
21680 /* A non-type template parm. */
21681 return true;
21683 case CONST_DECL:
21684 /* A non-type template parm. */
21685 if (DECL_TEMPLATE_PARM_P (expression))
21686 return true;
21687 return value_dependent_expression_p (DECL_INITIAL (expression));
21689 case VAR_DECL:
21690 /* A constant with literal type and is initialized
21691 with an expression that is value-dependent.
21693 Note that a non-dependent parenthesized initializer will have
21694 already been replaced with its constant value, so if we see
21695 a TREE_LIST it must be dependent. */
21696 if (DECL_INITIAL (expression)
21697 && decl_constant_var_p (expression)
21698 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
21699 /* cp_finish_decl doesn't fold reference initializers. */
21700 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
21701 || value_dependent_expression_p (DECL_INITIAL (expression))))
21702 return true;
21703 return false;
21705 case DYNAMIC_CAST_EXPR:
21706 case STATIC_CAST_EXPR:
21707 case CONST_CAST_EXPR:
21708 case REINTERPRET_CAST_EXPR:
21709 case CAST_EXPR:
21710 /* These expressions are value-dependent if the type to which
21711 the cast occurs is dependent or the expression being casted
21712 is value-dependent. */
21714 tree type = TREE_TYPE (expression);
21716 if (dependent_type_p (type))
21717 return true;
21719 /* A functional cast has a list of operands. */
21720 expression = TREE_OPERAND (expression, 0);
21721 if (!expression)
21723 /* If there are no operands, it must be an expression such
21724 as "int()". This should not happen for aggregate types
21725 because it would form non-constant expressions. */
21726 gcc_assert (cxx_dialect >= cxx11
21727 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
21729 return false;
21732 if (TREE_CODE (expression) == TREE_LIST)
21733 return any_value_dependent_elements_p (expression);
21735 return value_dependent_expression_p (expression);
21738 case SIZEOF_EXPR:
21739 if (SIZEOF_EXPR_TYPE_P (expression))
21740 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
21741 /* FALLTHRU */
21742 case ALIGNOF_EXPR:
21743 case TYPEID_EXPR:
21744 /* A `sizeof' expression is value-dependent if the operand is
21745 type-dependent or is a pack expansion. */
21746 expression = TREE_OPERAND (expression, 0);
21747 if (PACK_EXPANSION_P (expression))
21748 return true;
21749 else if (TYPE_P (expression))
21750 return dependent_type_p (expression);
21751 return instantiation_dependent_expression_p (expression);
21753 case AT_ENCODE_EXPR:
21754 /* An 'encode' expression is value-dependent if the operand is
21755 type-dependent. */
21756 expression = TREE_OPERAND (expression, 0);
21757 return dependent_type_p (expression);
21759 case NOEXCEPT_EXPR:
21760 expression = TREE_OPERAND (expression, 0);
21761 return instantiation_dependent_expression_p (expression);
21763 case SCOPE_REF:
21764 /* All instantiation-dependent expressions should also be considered
21765 value-dependent. */
21766 return instantiation_dependent_scope_ref_p (expression);
21768 case COMPONENT_REF:
21769 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
21770 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
21772 case NONTYPE_ARGUMENT_PACK:
21773 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
21774 is value-dependent. */
21776 tree values = ARGUMENT_PACK_ARGS (expression);
21777 int i, len = TREE_VEC_LENGTH (values);
21779 for (i = 0; i < len; ++i)
21780 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
21781 return true;
21783 return false;
21786 case TRAIT_EXPR:
21788 tree type2 = TRAIT_EXPR_TYPE2 (expression);
21789 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
21790 || (type2 ? dependent_type_p (type2) : false));
21793 case MODOP_EXPR:
21794 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
21795 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
21797 case ARRAY_REF:
21798 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
21799 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
21801 case ADDR_EXPR:
21803 tree op = TREE_OPERAND (expression, 0);
21804 return (value_dependent_expression_p (op)
21805 || has_value_dependent_address (op));
21808 case REQUIRES_EXPR:
21809 /* Treat all requires-expressions as value-dependent so
21810 we don't try to fold them. */
21811 return true;
21813 case TYPE_REQ:
21814 case VALIDEXPR_EXPR:
21815 case VALIDTYPE_EXPR:
21816 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
21818 case CALL_EXPR:
21820 tree fn = get_callee_fndecl (expression);
21821 int i, nargs;
21822 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
21823 return true;
21824 nargs = call_expr_nargs (expression);
21825 for (i = 0; i < nargs; ++i)
21827 tree op = CALL_EXPR_ARG (expression, i);
21828 /* In a call to a constexpr member function, look through the
21829 implicit ADDR_EXPR on the object argument so that it doesn't
21830 cause the call to be considered value-dependent. We also
21831 look through it in potential_constant_expression. */
21832 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
21833 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21834 && TREE_CODE (op) == ADDR_EXPR)
21835 op = TREE_OPERAND (op, 0);
21836 if (value_dependent_expression_p (op))
21837 return true;
21839 return false;
21842 case TEMPLATE_ID_EXPR:
21843 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
21844 type-dependent. */
21845 return type_dependent_expression_p (expression);
21847 case CONSTRUCTOR:
21849 unsigned ix;
21850 tree val;
21851 if (dependent_type_p (TREE_TYPE (expression)))
21852 return true;
21853 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
21854 if (value_dependent_expression_p (val))
21855 return true;
21856 return false;
21859 case STMT_EXPR:
21860 /* Treat a GNU statement expression as dependent to avoid crashing
21861 under instantiate_non_dependent_expr; it can't be constant. */
21862 return true;
21864 default:
21865 /* A constant expression is value-dependent if any subexpression is
21866 value-dependent. */
21867 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
21869 case tcc_reference:
21870 case tcc_unary:
21871 case tcc_comparison:
21872 case tcc_binary:
21873 case tcc_expression:
21874 case tcc_vl_exp:
21876 int i, len = cp_tree_operand_length (expression);
21878 for (i = 0; i < len; i++)
21880 tree t = TREE_OPERAND (expression, i);
21882 /* In some cases, some of the operands may be missing.l
21883 (For example, in the case of PREDECREMENT_EXPR, the
21884 amount to increment by may be missing.) That doesn't
21885 make the expression dependent. */
21886 if (t && value_dependent_expression_p (t))
21887 return true;
21890 break;
21891 default:
21892 break;
21894 break;
21897 /* The expression is not value-dependent. */
21898 return false;
21901 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
21902 [temp.dep.expr]. Note that an expression with no type is
21903 considered dependent. Other parts of the compiler arrange for an
21904 expression with type-dependent subexpressions to have no type, so
21905 this function doesn't have to be fully recursive. */
21907 bool
21908 type_dependent_expression_p (tree expression)
21910 if (!processing_template_decl)
21911 return false;
21913 if (expression == NULL_TREE || expression == error_mark_node)
21914 return false;
21916 /* An unresolved name is always dependent. */
21917 if (identifier_p (expression)
21918 || TREE_CODE (expression) == USING_DECL
21919 || TREE_CODE (expression) == INTRODUCED_PARM_DECL)
21920 return true;
21922 /* Some expression forms are never type-dependent. */
21923 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
21924 || TREE_CODE (expression) == SIZEOF_EXPR
21925 || TREE_CODE (expression) == ALIGNOF_EXPR
21926 || TREE_CODE (expression) == AT_ENCODE_EXPR
21927 || TREE_CODE (expression) == NOEXCEPT_EXPR
21928 || TREE_CODE (expression) == TRAIT_EXPR
21929 || TREE_CODE (expression) == TYPEID_EXPR
21930 || TREE_CODE (expression) == DELETE_EXPR
21931 || TREE_CODE (expression) == VEC_DELETE_EXPR
21932 || TREE_CODE (expression) == THROW_EXPR
21933 || TREE_CODE (expression) == REQUIRES_EXPR)
21934 return false;
21936 /* The types of these expressions depends only on the type to which
21937 the cast occurs. */
21938 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
21939 || TREE_CODE (expression) == STATIC_CAST_EXPR
21940 || TREE_CODE (expression) == CONST_CAST_EXPR
21941 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
21942 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
21943 || TREE_CODE (expression) == CAST_EXPR)
21944 return dependent_type_p (TREE_TYPE (expression));
21946 /* The types of these expressions depends only on the type created
21947 by the expression. */
21948 if (TREE_CODE (expression) == NEW_EXPR
21949 || TREE_CODE (expression) == VEC_NEW_EXPR)
21951 /* For NEW_EXPR tree nodes created inside a template, either
21952 the object type itself or a TREE_LIST may appear as the
21953 operand 1. */
21954 tree type = TREE_OPERAND (expression, 1);
21955 if (TREE_CODE (type) == TREE_LIST)
21956 /* This is an array type. We need to check array dimensions
21957 as well. */
21958 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
21959 || value_dependent_expression_p
21960 (TREE_OPERAND (TREE_VALUE (type), 1));
21961 else
21962 return dependent_type_p (type);
21965 if (TREE_CODE (expression) == SCOPE_REF)
21967 tree scope = TREE_OPERAND (expression, 0);
21968 tree name = TREE_OPERAND (expression, 1);
21970 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
21971 contains an identifier associated by name lookup with one or more
21972 declarations declared with a dependent type, or...a
21973 nested-name-specifier or qualified-id that names a member of an
21974 unknown specialization. */
21975 return (type_dependent_expression_p (name)
21976 || dependent_scope_p (scope));
21979 if (TREE_CODE (expression) == FUNCTION_DECL
21980 && DECL_LANG_SPECIFIC (expression)
21981 && DECL_TEMPLATE_INFO (expression)
21982 && (any_dependent_template_arguments_p
21983 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
21984 return true;
21986 if (TREE_CODE (expression) == TEMPLATE_DECL
21987 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
21988 return false;
21990 if (TREE_CODE (expression) == STMT_EXPR)
21991 expression = stmt_expr_value_expr (expression);
21993 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
21995 tree elt;
21996 unsigned i;
21998 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
22000 if (type_dependent_expression_p (elt))
22001 return true;
22003 return false;
22006 /* A static data member of the current instantiation with incomplete
22007 array type is type-dependent, as the definition and specializations
22008 can have different bounds. */
22009 if (VAR_P (expression)
22010 && DECL_CLASS_SCOPE_P (expression)
22011 && dependent_type_p (DECL_CONTEXT (expression))
22012 && VAR_HAD_UNKNOWN_BOUND (expression))
22013 return true;
22015 /* An array of unknown bound depending on a variadic parameter, eg:
22017 template<typename... Args>
22018 void foo (Args... args)
22020 int arr[] = { args... };
22023 template<int... vals>
22024 void bar ()
22026 int arr[] = { vals... };
22029 If the array has no length and has an initializer, it must be that
22030 we couldn't determine its length in cp_complete_array_type because
22031 it is dependent. */
22032 if (VAR_P (expression)
22033 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
22034 && !TYPE_DOMAIN (TREE_TYPE (expression))
22035 && DECL_INITIAL (expression))
22036 return true;
22038 /* A variable template specialization is type-dependent if it has any
22039 dependent template arguments. */
22040 if (VAR_P (expression)
22041 && DECL_LANG_SPECIFIC (expression)
22042 && DECL_TEMPLATE_INFO (expression)
22043 && variable_template_p (DECL_TI_TEMPLATE (expression)))
22044 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression));
22046 if (TREE_TYPE (expression) == unknown_type_node)
22048 if (TREE_CODE (expression) == ADDR_EXPR)
22049 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
22050 if (TREE_CODE (expression) == COMPONENT_REF
22051 || TREE_CODE (expression) == OFFSET_REF)
22053 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
22054 return true;
22055 expression = TREE_OPERAND (expression, 1);
22056 if (identifier_p (expression))
22057 return false;
22059 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
22060 if (TREE_CODE (expression) == SCOPE_REF)
22061 return false;
22063 /* Always dependent, on the number of arguments if nothing else. */
22064 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
22065 return true;
22067 if (BASELINK_P (expression))
22069 if (BASELINK_OPTYPE (expression)
22070 && dependent_type_p (BASELINK_OPTYPE (expression)))
22071 return true;
22072 expression = BASELINK_FUNCTIONS (expression);
22075 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
22077 if (any_dependent_template_arguments_p
22078 (TREE_OPERAND (expression, 1)))
22079 return true;
22080 expression = TREE_OPERAND (expression, 0);
22082 gcc_assert (TREE_CODE (expression) == OVERLOAD
22083 || TREE_CODE (expression) == FUNCTION_DECL);
22085 while (expression)
22087 if (type_dependent_expression_p (OVL_CURRENT (expression)))
22088 return true;
22089 expression = OVL_NEXT (expression);
22091 return false;
22094 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
22096 return (dependent_type_p (TREE_TYPE (expression)));
22099 /* walk_tree callback function for instantiation_dependent_expression_p,
22100 below. Returns non-zero if a dependent subexpression is found. */
22102 static tree
22103 instantiation_dependent_r (tree *tp, int *walk_subtrees,
22104 void * /*data*/)
22106 if (TYPE_P (*tp))
22108 /* We don't have to worry about decltype currently because decltype
22109 of an instantiation-dependent expr is a dependent type. This
22110 might change depending on the resolution of DR 1172. */
22111 *walk_subtrees = false;
22112 return NULL_TREE;
22114 enum tree_code code = TREE_CODE (*tp);
22115 switch (code)
22117 /* Don't treat an argument list as dependent just because it has no
22118 TREE_TYPE. */
22119 case TREE_LIST:
22120 case TREE_VEC:
22121 return NULL_TREE;
22123 case VAR_DECL:
22124 case CONST_DECL:
22125 /* A constant with a dependent initializer is dependent. */
22126 if (value_dependent_expression_p (*tp))
22127 return *tp;
22128 break;
22130 case TEMPLATE_PARM_INDEX:
22131 return *tp;
22133 /* Handle expressions with type operands. */
22134 case SIZEOF_EXPR:
22135 case ALIGNOF_EXPR:
22136 case TYPEID_EXPR:
22137 case AT_ENCODE_EXPR:
22139 tree op = TREE_OPERAND (*tp, 0);
22140 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
22141 op = TREE_TYPE (op);
22142 if (TYPE_P (op))
22144 if (dependent_type_p (op))
22145 return *tp;
22146 else
22148 *walk_subtrees = false;
22149 return NULL_TREE;
22152 break;
22155 case TRAIT_EXPR:
22157 bool l = dependent_type_p (TRAIT_EXPR_TYPE1 (*tp));
22158 bool r = is_binary_trait (TRAIT_EXPR_KIND (*tp))
22159 ? dependent_type_p (TRAIT_EXPR_TYPE1 (*tp)) : false;
22160 if (l || r)
22161 return *tp;
22162 *walk_subtrees = false;
22163 return NULL_TREE;
22166 case COMPONENT_REF:
22167 if (identifier_p (TREE_OPERAND (*tp, 1)))
22168 /* In a template, finish_class_member_access_expr creates a
22169 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
22170 type-dependent, so that we can check access control at
22171 instantiation time (PR 42277). See also Core issue 1273. */
22172 return *tp;
22173 break;
22175 case SCOPE_REF:
22176 if (instantiation_dependent_scope_ref_p (*tp))
22177 return *tp;
22178 else
22179 break;
22181 /* Treat statement-expressions as dependent. */
22182 case BIND_EXPR:
22183 return *tp;
22185 case REQUIRES_EXPR:
22186 /* Treat requires-expressions as dependent. */
22187 return *tp;
22189 default:
22190 break;
22193 if (type_dependent_expression_p (*tp))
22194 return *tp;
22195 else
22196 return NULL_TREE;
22199 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
22200 sense defined by the ABI:
22202 "An expression is instantiation-dependent if it is type-dependent
22203 or value-dependent, or it has a subexpression that is type-dependent
22204 or value-dependent." */
22206 bool
22207 instantiation_dependent_expression_p (tree expression)
22209 tree result;
22211 if (!processing_template_decl)
22212 return false;
22214 if (expression == error_mark_node)
22215 return false;
22217 result = cp_walk_tree_without_duplicates (&expression,
22218 instantiation_dependent_r, NULL);
22219 return result != NULL_TREE;
22222 /* Like type_dependent_expression_p, but it also works while not processing
22223 a template definition, i.e. during substitution or mangling. */
22225 bool
22226 type_dependent_expression_p_push (tree expr)
22228 bool b;
22229 ++processing_template_decl;
22230 b = type_dependent_expression_p (expr);
22231 --processing_template_decl;
22232 return b;
22235 /* Returns TRUE if ARGS contains a type-dependent expression. */
22237 bool
22238 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
22240 unsigned int i;
22241 tree arg;
22243 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
22245 if (type_dependent_expression_p (arg))
22246 return true;
22248 return false;
22251 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
22252 expressions) contains any type-dependent expressions. */
22254 bool
22255 any_type_dependent_elements_p (const_tree list)
22257 for (; list; list = TREE_CHAIN (list))
22258 if (type_dependent_expression_p (TREE_VALUE (list)))
22259 return true;
22261 return false;
22264 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
22265 expressions) contains any value-dependent expressions. */
22267 bool
22268 any_value_dependent_elements_p (const_tree list)
22270 for (; list; list = TREE_CHAIN (list))
22271 if (value_dependent_expression_p (TREE_VALUE (list)))
22272 return true;
22274 return false;
22277 /* Returns TRUE if the ARG (a template argument) is dependent. */
22279 bool
22280 dependent_template_arg_p (tree arg)
22282 if (!processing_template_decl)
22283 return false;
22285 /* Assume a template argument that was wrongly written by the user
22286 is dependent. This is consistent with what
22287 any_dependent_template_arguments_p [that calls this function]
22288 does. */
22289 if (!arg || arg == error_mark_node)
22290 return true;
22292 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
22293 arg = ARGUMENT_PACK_SELECT_ARG (arg);
22295 if (TREE_CODE (arg) == TEMPLATE_DECL
22296 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
22297 return dependent_template_p (arg);
22298 else if (ARGUMENT_PACK_P (arg))
22300 tree args = ARGUMENT_PACK_ARGS (arg);
22301 int i, len = TREE_VEC_LENGTH (args);
22302 for (i = 0; i < len; ++i)
22304 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
22305 return true;
22308 return false;
22310 else if (TYPE_P (arg))
22311 return dependent_type_p (arg);
22312 else
22313 return (type_dependent_expression_p (arg)
22314 || value_dependent_expression_p (arg));
22317 /* Returns true if ARGS (a collection of template arguments) contains
22318 any types that require structural equality testing. */
22320 bool
22321 any_template_arguments_need_structural_equality_p (tree args)
22323 int i;
22324 int j;
22326 if (!args)
22327 return false;
22328 if (args == error_mark_node)
22329 return true;
22331 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
22333 tree level = TMPL_ARGS_LEVEL (args, i + 1);
22334 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
22336 tree arg = TREE_VEC_ELT (level, j);
22337 tree packed_args = NULL_TREE;
22338 int k, len = 1;
22340 if (ARGUMENT_PACK_P (arg))
22342 /* Look inside the argument pack. */
22343 packed_args = ARGUMENT_PACK_ARGS (arg);
22344 len = TREE_VEC_LENGTH (packed_args);
22347 for (k = 0; k < len; ++k)
22349 if (packed_args)
22350 arg = TREE_VEC_ELT (packed_args, k);
22352 if (error_operand_p (arg))
22353 return true;
22354 else if (TREE_CODE (arg) == TEMPLATE_DECL)
22355 continue;
22356 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
22357 return true;
22358 else if (!TYPE_P (arg) && TREE_TYPE (arg)
22359 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
22360 return true;
22365 return false;
22368 /* Returns true if ARGS (a collection of template arguments) contains
22369 any dependent arguments. */
22371 bool
22372 any_dependent_template_arguments_p (const_tree args)
22374 int i;
22375 int j;
22377 if (!args)
22378 return false;
22379 if (args == error_mark_node)
22380 return true;
22382 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
22384 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
22385 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
22386 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
22387 return true;
22390 return false;
22393 /* Returns TRUE if the template TMPL is dependent. */
22395 bool
22396 dependent_template_p (tree tmpl)
22398 if (TREE_CODE (tmpl) == OVERLOAD)
22400 while (tmpl)
22402 if (dependent_template_p (OVL_CURRENT (tmpl)))
22403 return true;
22404 tmpl = OVL_NEXT (tmpl);
22406 return false;
22409 /* Template template parameters are dependent. */
22410 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
22411 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
22412 return true;
22413 /* So are names that have not been looked up. */
22414 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
22415 return true;
22416 /* So are member templates of dependent classes. */
22417 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
22418 return dependent_type_p (DECL_CONTEXT (tmpl));
22419 return false;
22422 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
22424 bool
22425 dependent_template_id_p (tree tmpl, tree args)
22427 return (dependent_template_p (tmpl)
22428 || any_dependent_template_arguments_p (args));
22431 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
22432 is dependent. */
22434 bool
22435 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
22437 int i;
22439 if (!processing_template_decl)
22440 return false;
22442 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
22444 tree decl = TREE_VEC_ELT (declv, i);
22445 tree init = TREE_VEC_ELT (initv, i);
22446 tree cond = TREE_VEC_ELT (condv, i);
22447 tree incr = TREE_VEC_ELT (incrv, i);
22449 if (type_dependent_expression_p (decl))
22450 return true;
22452 if (init && type_dependent_expression_p (init))
22453 return true;
22455 if (type_dependent_expression_p (cond))
22456 return true;
22458 if (COMPARISON_CLASS_P (cond)
22459 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
22460 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
22461 return true;
22463 if (TREE_CODE (incr) == MODOP_EXPR)
22465 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
22466 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
22467 return true;
22469 else if (type_dependent_expression_p (incr))
22470 return true;
22471 else if (TREE_CODE (incr) == MODIFY_EXPR)
22473 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
22474 return true;
22475 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
22477 tree t = TREE_OPERAND (incr, 1);
22478 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
22479 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
22480 return true;
22485 return false;
22488 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
22489 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
22490 no such TYPE can be found. Note that this function peers inside
22491 uninstantiated templates and therefore should be used only in
22492 extremely limited situations. ONLY_CURRENT_P restricts this
22493 peering to the currently open classes hierarchy (which is required
22494 when comparing types). */
22496 tree
22497 resolve_typename_type (tree type, bool only_current_p)
22499 tree scope;
22500 tree name;
22501 tree decl;
22502 int quals;
22503 tree pushed_scope;
22504 tree result;
22506 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
22508 scope = TYPE_CONTEXT (type);
22509 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
22510 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
22511 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
22512 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
22513 identifier of the TYPENAME_TYPE anymore.
22514 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
22515 TYPENAME_TYPE instead, we avoid messing up with a possible
22516 typedef variant case. */
22517 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
22519 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
22520 it first before we can figure out what NAME refers to. */
22521 if (TREE_CODE (scope) == TYPENAME_TYPE)
22523 if (TYPENAME_IS_RESOLVING_P (scope))
22524 /* Given a class template A with a dependent base with nested type C,
22525 typedef typename A::C::C C will land us here, as trying to resolve
22526 the initial A::C leads to the local C typedef, which leads back to
22527 A::C::C. So we break the recursion now. */
22528 return type;
22529 else
22530 scope = resolve_typename_type (scope, only_current_p);
22532 /* If we don't know what SCOPE refers to, then we cannot resolve the
22533 TYPENAME_TYPE. */
22534 if (TREE_CODE (scope) == TYPENAME_TYPE)
22535 return type;
22536 /* If the SCOPE is a template type parameter, we have no way of
22537 resolving the name. */
22538 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
22539 return type;
22540 /* If the SCOPE is not the current instantiation, there's no reason
22541 to look inside it. */
22542 if (only_current_p && !currently_open_class (scope))
22543 return type;
22544 /* If this is a typedef, we don't want to look inside (c++/11987). */
22545 if (typedef_variant_p (type))
22546 return type;
22547 /* If SCOPE isn't the template itself, it will not have a valid
22548 TYPE_FIELDS list. */
22549 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
22550 /* scope is either the template itself or a compatible instantiation
22551 like X<T>, so look up the name in the original template. */
22552 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
22553 else
22554 /* scope is a partial instantiation, so we can't do the lookup or we
22555 will lose the template arguments. */
22556 return type;
22557 /* Enter the SCOPE so that name lookup will be resolved as if we
22558 were in the class definition. In particular, SCOPE will no
22559 longer be considered a dependent type. */
22560 pushed_scope = push_scope (scope);
22561 /* Look up the declaration. */
22562 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
22563 tf_warning_or_error);
22565 result = NULL_TREE;
22567 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
22568 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
22569 if (!decl)
22570 /*nop*/;
22571 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
22572 && TREE_CODE (decl) == TYPE_DECL)
22574 result = TREE_TYPE (decl);
22575 if (result == error_mark_node)
22576 result = NULL_TREE;
22578 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
22579 && DECL_CLASS_TEMPLATE_P (decl))
22581 tree tmpl;
22582 tree args;
22583 /* Obtain the template and the arguments. */
22584 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
22585 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
22586 /* Instantiate the template. */
22587 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
22588 /*entering_scope=*/0,
22589 tf_error | tf_user);
22590 if (result == error_mark_node)
22591 result = NULL_TREE;
22594 /* Leave the SCOPE. */
22595 if (pushed_scope)
22596 pop_scope (pushed_scope);
22598 /* If we failed to resolve it, return the original typename. */
22599 if (!result)
22600 return type;
22602 /* If lookup found a typename type, resolve that too. */
22603 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
22605 /* Ill-formed programs can cause infinite recursion here, so we
22606 must catch that. */
22607 TYPENAME_IS_RESOLVING_P (type) = 1;
22608 result = resolve_typename_type (result, only_current_p);
22609 TYPENAME_IS_RESOLVING_P (type) = 0;
22612 /* Qualify the resulting type. */
22613 quals = cp_type_quals (type);
22614 if (quals)
22615 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
22617 return result;
22620 /* EXPR is an expression which is not type-dependent. Return a proxy
22621 for EXPR that can be used to compute the types of larger
22622 expressions containing EXPR. */
22624 tree
22625 build_non_dependent_expr (tree expr)
22627 tree inner_expr;
22629 #ifdef ENABLE_CHECKING
22630 /* Try to get a constant value for all non-dependent expressions in
22631 order to expose bugs in *_dependent_expression_p and constexpr. */
22632 if (cxx_dialect >= cxx11)
22633 fold_non_dependent_expr (expr);
22634 #endif
22636 /* Preserve OVERLOADs; the functions must be available to resolve
22637 types. */
22638 inner_expr = expr;
22639 if (TREE_CODE (inner_expr) == STMT_EXPR)
22640 inner_expr = stmt_expr_value_expr (inner_expr);
22641 if (TREE_CODE (inner_expr) == ADDR_EXPR)
22642 inner_expr = TREE_OPERAND (inner_expr, 0);
22643 if (TREE_CODE (inner_expr) == COMPONENT_REF)
22644 inner_expr = TREE_OPERAND (inner_expr, 1);
22645 if (is_overloaded_fn (inner_expr)
22646 || TREE_CODE (inner_expr) == OFFSET_REF)
22647 return expr;
22648 /* There is no need to return a proxy for a variable. */
22649 if (VAR_P (expr))
22650 return expr;
22651 /* Preserve string constants; conversions from string constants to
22652 "char *" are allowed, even though normally a "const char *"
22653 cannot be used to initialize a "char *". */
22654 if (TREE_CODE (expr) == STRING_CST)
22655 return expr;
22656 /* Preserve void and arithmetic constants, as an optimization -- there is no
22657 reason to create a new node. */
22658 if (TREE_CODE (expr) == VOID_CST
22659 || TREE_CODE (expr) == INTEGER_CST
22660 || TREE_CODE (expr) == REAL_CST)
22661 return expr;
22662 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
22663 There is at least one place where we want to know that a
22664 particular expression is a throw-expression: when checking a ?:
22665 expression, there are special rules if the second or third
22666 argument is a throw-expression. */
22667 if (TREE_CODE (expr) == THROW_EXPR)
22668 return expr;
22670 /* Don't wrap an initializer list, we need to be able to look inside. */
22671 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
22672 return expr;
22674 /* Don't wrap a dummy object, we need to be able to test for it. */
22675 if (is_dummy_object (expr))
22676 return expr;
22678 if (TREE_CODE (expr) == COND_EXPR)
22679 return build3 (COND_EXPR,
22680 TREE_TYPE (expr),
22681 TREE_OPERAND (expr, 0),
22682 (TREE_OPERAND (expr, 1)
22683 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
22684 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
22685 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
22686 if (TREE_CODE (expr) == COMPOUND_EXPR
22687 && !COMPOUND_EXPR_OVERLOADED (expr))
22688 return build2 (COMPOUND_EXPR,
22689 TREE_TYPE (expr),
22690 TREE_OPERAND (expr, 0),
22691 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
22693 /* If the type is unknown, it can't really be non-dependent */
22694 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
22696 /* Otherwise, build a NON_DEPENDENT_EXPR. */
22697 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
22700 /* ARGS is a vector of expressions as arguments to a function call.
22701 Replace the arguments with equivalent non-dependent expressions.
22702 This modifies ARGS in place. */
22704 void
22705 make_args_non_dependent (vec<tree, va_gc> *args)
22707 unsigned int ix;
22708 tree arg;
22710 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
22712 tree newarg = build_non_dependent_expr (arg);
22713 if (newarg != arg)
22714 (*args)[ix] = newarg;
22718 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
22719 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
22720 parms. */
22722 static tree
22723 make_auto_1 (tree name)
22725 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
22726 TYPE_NAME (au) = build_decl (input_location,
22727 TYPE_DECL, name, au);
22728 TYPE_STUB_DECL (au) = TYPE_NAME (au);
22729 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
22730 (0, processing_template_decl + 1, processing_template_decl + 1,
22731 TYPE_NAME (au), NULL_TREE);
22732 TYPE_CANONICAL (au) = canonical_type_parameter (au);
22733 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
22734 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
22736 return au;
22739 tree
22740 make_decltype_auto (void)
22742 return make_auto_1 (get_identifier ("decltype(auto)"));
22745 tree
22746 make_auto (void)
22748 return make_auto_1 (get_identifier ("auto"));
22751 /* Given type ARG, return std::initializer_list<ARG>. */
22753 static tree
22754 listify (tree arg)
22756 tree std_init_list = namespace_binding
22757 (get_identifier ("initializer_list"), std_node);
22758 tree argvec;
22759 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
22761 error ("deducing from brace-enclosed initializer list requires "
22762 "#include <initializer_list>");
22763 return error_mark_node;
22765 argvec = make_tree_vec (1);
22766 TREE_VEC_ELT (argvec, 0) = arg;
22767 return lookup_template_class (std_init_list, argvec, NULL_TREE,
22768 NULL_TREE, 0, tf_warning_or_error);
22771 /* Replace auto in TYPE with std::initializer_list<auto>. */
22773 static tree
22774 listify_autos (tree type, tree auto_node)
22776 tree init_auto = listify (auto_node);
22777 tree argvec = make_tree_vec (1);
22778 TREE_VEC_ELT (argvec, 0) = init_auto;
22779 if (processing_template_decl)
22780 argvec = add_to_template_args (current_template_args (), argvec);
22781 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
22784 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
22785 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
22787 tree
22788 do_auto_deduction (tree type, tree init, tree auto_node)
22790 tree targs;
22792 if (init == error_mark_node)
22793 return error_mark_node;
22795 if (type_dependent_expression_p (init))
22796 /* Defining a subset of type-dependent expressions that we can deduce
22797 from ahead of time isn't worth the trouble. */
22798 return type;
22800 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
22801 with either a new invented type template parameter U or, if the
22802 initializer is a braced-init-list (8.5.4), with
22803 std::initializer_list<U>. */
22804 if (BRACE_ENCLOSED_INITIALIZER_P (init))
22806 if (!DIRECT_LIST_INIT_P (init))
22807 type = listify_autos (type, auto_node);
22808 else if (CONSTRUCTOR_NELTS (init) == 1)
22809 init = CONSTRUCTOR_ELT (init, 0)->value;
22810 else
22812 if (permerror (input_location, "direct-list-initialization of "
22813 "%<auto%> requires exactly one element"))
22814 inform (input_location,
22815 "for deduction to %<std::initializer_list%>, use copy-"
22816 "list-initialization (i.e. add %<=%> before the %<{%>)");
22817 type = listify_autos (type, auto_node);
22821 init = resolve_nondeduced_context (init);
22823 targs = make_tree_vec (1);
22824 if (AUTO_IS_DECLTYPE (auto_node))
22826 bool id = (DECL_P (init) || (TREE_CODE (init) == COMPONENT_REF
22827 && !REF_PARENTHESIZED_P (init)));
22828 TREE_VEC_ELT (targs, 0)
22829 = finish_decltype_type (init, id, tf_warning_or_error);
22830 if (type != auto_node)
22832 error ("%qT as type rather than plain %<decltype(auto)%>", type);
22833 return error_mark_node;
22836 else
22838 tree parms = build_tree_list (NULL_TREE, type);
22839 tree tparms = make_tree_vec (1);
22840 int val;
22842 TREE_VEC_ELT (tparms, 0)
22843 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
22844 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
22845 DEDUCE_CALL, LOOKUP_NORMAL,
22846 NULL, /*explain_p=*/false);
22847 if (val > 0)
22849 if (processing_template_decl)
22850 /* Try again at instantiation time. */
22851 return type;
22852 if (type && type != error_mark_node)
22853 /* If type is error_mark_node a diagnostic must have been
22854 emitted by now. Also, having a mention to '<type error>'
22855 in the diagnostic is not really useful to the user. */
22857 if (cfun && auto_node == current_function_auto_return_pattern
22858 && LAMBDA_FUNCTION_P (current_function_decl))
22859 error ("unable to deduce lambda return type from %qE", init);
22860 else
22861 error ("unable to deduce %qT from %qE", type, init);
22863 return error_mark_node;
22867 /* If the list of declarators contains more than one declarator, the type
22868 of each declared variable is determined as described above. If the
22869 type deduced for the template parameter U is not the same in each
22870 deduction, the program is ill-formed. */
22871 if (TREE_TYPE (auto_node)
22872 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
22874 if (cfun && auto_node == current_function_auto_return_pattern
22875 && LAMBDA_FUNCTION_P (current_function_decl))
22876 error ("inconsistent types %qT and %qT deduced for "
22877 "lambda return type", TREE_TYPE (auto_node),
22878 TREE_VEC_ELT (targs, 0));
22879 else
22880 error ("inconsistent deduction for %qT: %qT and then %qT",
22881 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
22882 return error_mark_node;
22884 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
22886 if (processing_template_decl)
22887 targs = add_to_template_args (current_template_args (), targs);
22888 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
22891 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
22892 result. */
22894 tree
22895 splice_late_return_type (tree type, tree late_return_type)
22897 tree argvec;
22899 if (late_return_type == NULL_TREE)
22900 return type;
22901 argvec = make_tree_vec (1);
22902 TREE_VEC_ELT (argvec, 0) = late_return_type;
22903 if (processing_template_parmlist)
22904 /* For a late-specified return type in a template type-parameter, we
22905 need to add a dummy argument level for its parmlist. */
22906 argvec = add_to_template_args
22907 (make_tree_vec (processing_template_parmlist), argvec);
22908 if (current_template_parms)
22909 argvec = add_to_template_args (current_template_args (), argvec);
22910 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
22913 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
22914 'decltype(auto)'. */
22916 bool
22917 is_auto (const_tree type)
22919 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22920 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
22921 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
22922 return true;
22923 else
22924 return false;
22927 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
22928 a use of `auto'. Returns NULL_TREE otherwise. */
22930 tree
22931 type_uses_auto (tree type)
22933 return find_type_usage (type, is_auto);
22936 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
22937 'decltype(auto)' or a concept. */
22939 bool
22940 is_auto_or_concept (const_tree type)
22942 return is_auto (type); // or concept
22945 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
22946 a concept identifier) iff TYPE contains a use of a generic type. Returns
22947 NULL_TREE otherwise. */
22949 tree
22950 type_uses_auto_or_concept (tree type)
22952 return find_type_usage (type, is_auto_or_concept);
22956 /* For a given template T, return the vector of typedefs referenced
22957 in T for which access check is needed at T instantiation time.
22958 T is either a FUNCTION_DECL or a RECORD_TYPE.
22959 Those typedefs were added to T by the function
22960 append_type_to_template_for_access_check. */
22962 vec<qualified_typedef_usage_t, va_gc> *
22963 get_types_needing_access_check (tree t)
22965 tree ti;
22966 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
22968 if (!t || t == error_mark_node)
22969 return NULL;
22971 if (!(ti = get_template_info (t)))
22972 return NULL;
22974 if (CLASS_TYPE_P (t)
22975 || TREE_CODE (t) == FUNCTION_DECL)
22977 if (!TI_TEMPLATE (ti))
22978 return NULL;
22980 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
22983 return result;
22986 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
22987 tied to T. That list of typedefs will be access checked at
22988 T instantiation time.
22989 T is either a FUNCTION_DECL or a RECORD_TYPE.
22990 TYPE_DECL is a TYPE_DECL node representing a typedef.
22991 SCOPE is the scope through which TYPE_DECL is accessed.
22992 LOCATION is the location of the usage point of TYPE_DECL.
22994 This function is a subroutine of
22995 append_type_to_template_for_access_check. */
22997 static void
22998 append_type_to_template_for_access_check_1 (tree t,
22999 tree type_decl,
23000 tree scope,
23001 location_t location)
23003 qualified_typedef_usage_t typedef_usage;
23004 tree ti;
23006 if (!t || t == error_mark_node)
23007 return;
23009 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
23010 || CLASS_TYPE_P (t))
23011 && type_decl
23012 && TREE_CODE (type_decl) == TYPE_DECL
23013 && scope);
23015 if (!(ti = get_template_info (t)))
23016 return;
23018 gcc_assert (TI_TEMPLATE (ti));
23020 typedef_usage.typedef_decl = type_decl;
23021 typedef_usage.context = scope;
23022 typedef_usage.locus = location;
23024 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
23027 /* Append TYPE_DECL to the template TEMPL.
23028 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
23029 At TEMPL instanciation time, TYPE_DECL will be checked to see
23030 if it can be accessed through SCOPE.
23031 LOCATION is the location of the usage point of TYPE_DECL.
23033 e.g. consider the following code snippet:
23035 class C
23037 typedef int myint;
23040 template<class U> struct S
23042 C::myint mi; // <-- usage point of the typedef C::myint
23045 S<char> s;
23047 At S<char> instantiation time, we need to check the access of C::myint
23048 In other words, we need to check the access of the myint typedef through
23049 the C scope. For that purpose, this function will add the myint typedef
23050 and the scope C through which its being accessed to a list of typedefs
23051 tied to the template S. That list will be walked at template instantiation
23052 time and access check performed on each typedefs it contains.
23053 Note that this particular code snippet should yield an error because
23054 myint is private to C. */
23056 void
23057 append_type_to_template_for_access_check (tree templ,
23058 tree type_decl,
23059 tree scope,
23060 location_t location)
23062 qualified_typedef_usage_t *iter;
23063 unsigned i;
23065 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
23067 /* Make sure we don't append the type to the template twice. */
23068 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
23069 if (iter->typedef_decl == type_decl && scope == iter->context)
23070 return;
23072 append_type_to_template_for_access_check_1 (templ, type_decl,
23073 scope, location);
23076 /* Convert the generic type parameters in PARM that match the types given in the
23077 range [START_IDX, END_IDX) from the current_template_parms into generic type
23078 packs. */
23080 tree
23081 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
23083 tree current = current_template_parms;
23084 int depth = TMPL_PARMS_DEPTH (current);
23085 current = INNERMOST_TEMPLATE_PARMS (current);
23086 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
23088 for (int i = 0; i < start_idx; ++i)
23089 TREE_VEC_ELT (replacement, i)
23090 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
23092 for (int i = start_idx; i < end_idx; ++i)
23094 /* Create a distinct parameter pack type from the current parm and add it
23095 to the replacement args to tsubst below into the generic function
23096 parameter. */
23098 tree o = TREE_TYPE (TREE_VALUE
23099 (TREE_VEC_ELT (current, i)));
23100 tree t = copy_type (o);
23101 TEMPLATE_TYPE_PARM_INDEX (t)
23102 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
23103 o, 0, 0, tf_none);
23104 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
23105 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
23106 TYPE_MAIN_VARIANT (t) = t;
23107 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
23108 TYPE_CANONICAL (t) = canonical_type_parameter (t);
23109 TREE_VEC_ELT (replacement, i) = t;
23110 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
23113 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
23114 TREE_VEC_ELT (replacement, i)
23115 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
23117 /* If there are more levels then build up the replacement with the outer
23118 template parms. */
23119 if (depth > 1)
23120 replacement = add_to_template_args (template_parms_to_args
23121 (TREE_CHAIN (current_template_parms)),
23122 replacement);
23124 return tsubst (parm, replacement, tf_none, NULL_TREE);
23127 /* Entries in the decl_constraint hash table. */
23128 struct GTY((for_user)) constr_entry
23130 tree decl;
23131 tree ci;
23134 /* Hashing function and equality fo constraint entries.. */
23135 struct constr_hasher : ggc_hasher<constr_entry *>
23137 static hashval_t hash (constr_entry *e)
23139 return (hashval_t)DECL_UID (e->decl);
23142 static bool equal (constr_entry *e1, constr_entry *e2)
23144 return e1->decl == e2->decl;
23149 /* A mapping from declarations to constraint information. Note that
23150 both templates and their underlying declarations are mapped to the
23151 same constraint information.
23153 FIXME: This is defined in pt.c because it's garbage collection
23154 code is not being generated for constraint.cc. */
23155 static GTY (()) hash_table<constr_hasher> *decl_constraints;
23157 /* Returns true iff cinfo contains a valid set of constraints.
23158 This is the case when the associated requirements have been
23159 successfully decomposed into lists of atomic constraints.
23160 That is, when the saved assumptions are not error_mark_node. */
23161 bool
23162 valid_constraints_p (tree cinfo)
23164 gcc_assert (cinfo);
23165 return CI_ASSUMPTIONS (cinfo) != error_mark_node;
23168 /* Returns the template constraints of declaration T. If T is not
23169 constrained, return NULL_TREE. Note that T must be non-null. */
23170 tree
23171 get_constraints (tree t)
23173 gcc_assert (DECL_P (t));
23174 if (TREE_CODE (t) == TEMPLATE_DECL)
23175 t = DECL_TEMPLATE_RESULT (t);
23176 constr_entry elt = { t, NULL_TREE };
23177 constr_entry* found = decl_constraints->find (&elt);
23178 if (found)
23179 return found->ci;
23180 else
23181 return NULL_TREE;
23184 /* Associate the given constraint information CI with the declaration
23185 T. If T is a template, then the constraints are associated with
23186 its underlying declaration. Don't build associations if CI is
23187 NULL_TREE. */
23188 void
23189 set_constraints (tree t, tree ci)
23191 if (!ci)
23192 return;
23193 gcc_assert (t);
23194 if (TREE_CODE (t) == TEMPLATE_DECL)
23195 t = DECL_TEMPLATE_RESULT (t);
23196 gcc_assert (!get_constraints (t));
23197 constr_entry elt = {t, ci};
23198 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
23199 constr_entry* entry = ggc_alloc<constr_entry> ();
23200 *entry = elt;
23201 *slot = entry;
23204 /* Remove the associated constraints of the declaration T.
23205 If there are no constraints associated with T, then
23206 return NULL_TREE. */
23207 void
23208 remove_constraints (tree t)
23210 gcc_assert (DECL_P (t));
23211 if (TREE_CODE (t) == TEMPLATE_DECL)
23212 t = DECL_TEMPLATE_RESULT (t);
23214 constr_entry elt = {t, NULL_TREE};
23215 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
23216 if (slot)
23217 decl_constraints->clear_slot(slot);
23220 /* Set up the hash table for constraint association. */
23222 void
23223 init_constraint_processing (void)
23225 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
23228 /* Set up the hash tables for template instantiations. */
23230 void
23231 init_template_processing (void)
23233 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
23234 type_specializations = hash_table<spec_hasher>::create_ggc (37);
23237 /* Print stats about the template hash tables for -fstats. */
23239 void
23240 print_template_statistics (void)
23242 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
23243 "%f collisions\n", (long) decl_specializations->size (),
23244 (long) decl_specializations->elements (),
23245 decl_specializations->collisions ());
23246 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
23247 "%f collisions\n", (long) type_specializations->size (),
23248 (long) type_specializations->elements (),
23249 type_specializations->collisions ());
23252 #include "gt-cp-pt.h"