]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
Handle TYPE_HAS_LATE_RETURN_TYPE like ref-qualifier and eh spec.
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22 /* Known bugs or deficiencies include:
23
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". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
45
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
49
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
54 {
55 struct pending_template *next;
56 struct tinst_level *tinst;
57 };
58
59 static GTY(()) struct pending_template *pending_templates;
60 static GTY(()) struct pending_template *last_pending_template;
61
62 int processing_template_parmlist;
63 static int template_header_count;
64
65 static GTY(()) tree saved_trees;
66 static vec<int> inline_parm_levels;
67
68 static GTY(()) struct tinst_level *current_tinst_level;
69
70 static GTY(()) tree saved_access_scope;
71
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr;
76
77 // -------------------------------------------------------------------------- //
78 // Local Specialization Stack
79 //
80 // Implementation of the RAII helper for creating new local
81 // specializations.
82 local_specialization_stack::local_specialization_stack (lss_policy policy)
83 : saved (local_specializations)
84 {
85 if (policy == lss_blank || !saved)
86 local_specializations = new hash_map<tree, tree>;
87 else
88 local_specializations = new hash_map<tree, tree>(*saved);
89 }
90
91 local_specialization_stack::~local_specialization_stack ()
92 {
93 delete local_specializations;
94 local_specializations = saved;
95 }
96
97 /* True if we've recursed into fn_type_unification too many times. */
98 static bool excessive_deduction_depth;
99
100 struct GTY((for_user)) spec_entry
101 {
102 tree tmpl;
103 tree args;
104 tree spec;
105 };
106
107 struct spec_hasher : ggc_ptr_hash<spec_entry>
108 {
109 static hashval_t hash (spec_entry *);
110 static bool equal (spec_entry *, spec_entry *);
111 };
112
113 static GTY (()) hash_table<spec_hasher> *decl_specializations;
114
115 static GTY (()) hash_table<spec_hasher> *type_specializations;
116
117 /* Contains canonical template parameter types. The vector is indexed by
118 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
119 TREE_LIST, whose TREE_VALUEs contain the canonical template
120 parameters of various types and levels. */
121 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
122
123 #define UNIFY_ALLOW_NONE 0
124 #define UNIFY_ALLOW_MORE_CV_QUAL 1
125 #define UNIFY_ALLOW_LESS_CV_QUAL 2
126 #define UNIFY_ALLOW_DERIVED 4
127 #define UNIFY_ALLOW_INTEGER 8
128 #define UNIFY_ALLOW_OUTER_LEVEL 16
129 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
130 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131
132 enum template_base_result {
133 tbr_incomplete_type,
134 tbr_ambiguous_baseclass,
135 tbr_success
136 };
137
138 static void push_access_scope (tree);
139 static void pop_access_scope (tree);
140 static bool resolve_overloaded_unification (tree, tree, tree, tree,
141 unification_kind_t, int,
142 bool);
143 static int try_one_overload (tree, tree, tree, tree, tree,
144 unification_kind_t, int, bool, bool);
145 static int unify (tree, tree, tree, tree, int, bool);
146 static void add_pending_template (tree);
147 static tree reopen_tinst_level (struct tinst_level *);
148 static tree tsubst_initializer_list (tree, tree);
149 static tree get_partial_spec_bindings (tree, tree, tree);
150 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
153 bool, bool);
154 static void tsubst_enum (tree, tree, tree);
155 static tree add_to_template_args (tree, tree);
156 static tree add_outermost_template_args (tree, tree);
157 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
158 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
159 tree);
160 static int type_unification_real (tree, tree, tree, const tree *,
161 unsigned int, int, unification_kind_t, int,
162 vec<deferred_access_check, va_gc> **,
163 bool);
164 static void note_template_header (int);
165 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
166 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
167 static tree convert_template_argument (tree, tree, tree,
168 tsubst_flags_t, int, tree);
169 static tree for_each_template_parm (tree, tree_fn_t, void*,
170 hash_set<tree> *, bool, tree_fn_t = NULL);
171 static tree expand_template_argument_pack (tree);
172 static tree build_template_parm_index (int, int, int, tree, tree);
173 static bool inline_needs_template_parms (tree, bool);
174 static void push_inline_template_parms_recursive (tree, int);
175 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
176 static int mark_template_parm (tree, void *);
177 static int template_parm_this_level_p (tree, void *);
178 static tree tsubst_friend_function (tree, tree);
179 static tree tsubst_friend_class (tree, tree);
180 static int can_complete_type_without_circularity (tree);
181 static tree get_bindings (tree, tree, tree, bool);
182 static int template_decl_level (tree);
183 static int check_cv_quals_for_unify (int, tree, tree);
184 static void template_parm_level_and_index (tree, int*, int*);
185 static int unify_pack_expansion (tree, tree, tree,
186 tree, unification_kind_t, bool, bool);
187 static tree copy_template_args (tree);
188 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
190 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
191 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
192 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
193 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
194 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
195 static bool check_specialization_scope (void);
196 static tree process_partial_specialization (tree);
197 static void set_current_access_from_decl (tree);
198 static enum template_base_result get_template_base (tree, tree, tree, tree,
199 bool , tree *);
200 static tree try_class_unification (tree, tree, tree, tree, bool);
201 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
202 tree, tree);
203 static bool template_template_parm_bindings_ok_p (tree, tree);
204 static void tsubst_default_arguments (tree, tsubst_flags_t);
205 static tree for_each_template_parm_r (tree *, int *, void *);
206 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
207 static void copy_default_args_to_explicit_spec (tree);
208 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
209 static bool dependent_template_arg_p (tree);
210 static bool any_template_arguments_need_structural_equality_p (tree);
211 static bool dependent_type_p_r (tree);
212 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
213 static tree tsubst_decl (tree, tree, tsubst_flags_t);
214 static void perform_typedefs_access_check (tree tmpl, tree targs);
215 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
216 location_t);
217 static tree listify (tree);
218 static tree listify_autos (tree, tree);
219 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
220 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
221 static bool complex_alias_template_p (const_tree tmpl);
222 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
223 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
224 static tree make_argument_pack (tree);
225 static void register_parameter_specializations (tree, tree);
226 static tree enclosing_instantiation_of (tree tctx);
227
228 /* Make the current scope suitable for access checking when we are
229 processing T. T can be FUNCTION_DECL for instantiated function
230 template, VAR_DECL for static member variable, or TYPE_DECL for
231 alias template (needed by instantiate_decl). */
232
233 static void
234 push_access_scope (tree t)
235 {
236 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
237 || TREE_CODE (t) == TYPE_DECL);
238
239 if (DECL_FRIEND_CONTEXT (t))
240 push_nested_class (DECL_FRIEND_CONTEXT (t));
241 else if (DECL_CLASS_SCOPE_P (t))
242 push_nested_class (DECL_CONTEXT (t));
243 else
244 push_to_top_level ();
245
246 if (TREE_CODE (t) == FUNCTION_DECL)
247 {
248 saved_access_scope = tree_cons
249 (NULL_TREE, current_function_decl, saved_access_scope);
250 current_function_decl = t;
251 }
252 }
253
254 /* Restore the scope set up by push_access_scope. T is the node we
255 are processing. */
256
257 static void
258 pop_access_scope (tree t)
259 {
260 if (TREE_CODE (t) == FUNCTION_DECL)
261 {
262 current_function_decl = TREE_VALUE (saved_access_scope);
263 saved_access_scope = TREE_CHAIN (saved_access_scope);
264 }
265
266 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
267 pop_nested_class ();
268 else
269 pop_from_top_level ();
270 }
271
272 /* Do any processing required when DECL (a member template
273 declaration) is finished. Returns the TEMPLATE_DECL corresponding
274 to DECL, unless it is a specialization, in which case the DECL
275 itself is returned. */
276
277 tree
278 finish_member_template_decl (tree decl)
279 {
280 if (decl == error_mark_node)
281 return error_mark_node;
282
283 gcc_assert (DECL_P (decl));
284
285 if (TREE_CODE (decl) == TYPE_DECL)
286 {
287 tree type;
288
289 type = TREE_TYPE (decl);
290 if (type == error_mark_node)
291 return error_mark_node;
292 if (MAYBE_CLASS_TYPE_P (type)
293 && CLASSTYPE_TEMPLATE_INFO (type)
294 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
295 {
296 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
297 check_member_template (tmpl);
298 return tmpl;
299 }
300 return NULL_TREE;
301 }
302 else if (TREE_CODE (decl) == FIELD_DECL)
303 error ("data member %qD cannot be a member template", decl);
304 else if (DECL_TEMPLATE_INFO (decl))
305 {
306 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
307 {
308 check_member_template (DECL_TI_TEMPLATE (decl));
309 return DECL_TI_TEMPLATE (decl);
310 }
311 else
312 return decl;
313 }
314 else
315 error ("invalid member template declaration %qD", decl);
316
317 return error_mark_node;
318 }
319
320 /* Create a template info node. */
321
322 tree
323 build_template_info (tree template_decl, tree template_args)
324 {
325 tree result = make_node (TEMPLATE_INFO);
326 TI_TEMPLATE (result) = template_decl;
327 TI_ARGS (result) = template_args;
328 return result;
329 }
330
331 /* Return the template info node corresponding to T, whatever T is. */
332
333 tree
334 get_template_info (const_tree t)
335 {
336 tree tinfo = NULL_TREE;
337
338 if (!t || t == error_mark_node)
339 return NULL;
340
341 if (TREE_CODE (t) == NAMESPACE_DECL
342 || TREE_CODE (t) == PARM_DECL)
343 return NULL;
344
345 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
346 tinfo = DECL_TEMPLATE_INFO (t);
347
348 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
349 t = TREE_TYPE (t);
350
351 if (OVERLOAD_TYPE_P (t))
352 tinfo = TYPE_TEMPLATE_INFO (t);
353 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
354 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
355
356 return tinfo;
357 }
358
359 /* Returns the template nesting level of the indicated class TYPE.
360
361 For example, in:
362 template <class T>
363 struct A
364 {
365 template <class U>
366 struct B {};
367 };
368
369 A<T>::B<U> has depth two, while A<T> has depth one.
370 Both A<T>::B<int> and A<int>::B<U> have depth one, if
371 they are instantiations, not specializations.
372
373 This function is guaranteed to return 0 if passed NULL_TREE so
374 that, for example, `template_class_depth (current_class_type)' is
375 always safe. */
376
377 int
378 template_class_depth (tree type)
379 {
380 int depth;
381
382 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
383 {
384 tree tinfo = get_template_info (type);
385
386 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
387 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
388 ++depth;
389
390 if (DECL_P (type))
391 type = CP_DECL_CONTEXT (type);
392 else if (LAMBDA_TYPE_P (type))
393 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
394 else
395 type = CP_TYPE_CONTEXT (type);
396 }
397
398 return depth;
399 }
400
401 /* Subroutine of maybe_begin_member_template_processing.
402 Returns true if processing DECL needs us to push template parms. */
403
404 static bool
405 inline_needs_template_parms (tree decl, bool nsdmi)
406 {
407 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
408 return false;
409
410 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
411 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
412 }
413
414 /* Subroutine of maybe_begin_member_template_processing.
415 Push the template parms in PARMS, starting from LEVELS steps into the
416 chain, and ending at the beginning, since template parms are listed
417 innermost first. */
418
419 static void
420 push_inline_template_parms_recursive (tree parmlist, int levels)
421 {
422 tree parms = TREE_VALUE (parmlist);
423 int i;
424
425 if (levels > 1)
426 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
427
428 ++processing_template_decl;
429 current_template_parms
430 = tree_cons (size_int (processing_template_decl),
431 parms, current_template_parms);
432 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
433
434 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
435 NULL);
436 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
437 {
438 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
439
440 if (error_operand_p (parm))
441 continue;
442
443 gcc_assert (DECL_P (parm));
444
445 switch (TREE_CODE (parm))
446 {
447 case TYPE_DECL:
448 case TEMPLATE_DECL:
449 pushdecl (parm);
450 break;
451
452 case PARM_DECL:
453 /* Push the CONST_DECL. */
454 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
455 break;
456
457 default:
458 gcc_unreachable ();
459 }
460 }
461 }
462
463 /* Restore the template parameter context for a member template, a
464 friend template defined in a class definition, or a non-template
465 member of template class. */
466
467 void
468 maybe_begin_member_template_processing (tree decl)
469 {
470 tree parms;
471 int levels = 0;
472 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
473
474 if (nsdmi)
475 {
476 tree ctx = DECL_CONTEXT (decl);
477 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
478 /* Disregard full specializations (c++/60999). */
479 && uses_template_parms (ctx)
480 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
481 }
482
483 if (inline_needs_template_parms (decl, nsdmi))
484 {
485 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
486 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
487
488 if (DECL_TEMPLATE_SPECIALIZATION (decl))
489 {
490 --levels;
491 parms = TREE_CHAIN (parms);
492 }
493
494 push_inline_template_parms_recursive (parms, levels);
495 }
496
497 /* Remember how many levels of template parameters we pushed so that
498 we can pop them later. */
499 inline_parm_levels.safe_push (levels);
500 }
501
502 /* Undo the effects of maybe_begin_member_template_processing. */
503
504 void
505 maybe_end_member_template_processing (void)
506 {
507 int i;
508 int last;
509
510 if (inline_parm_levels.length () == 0)
511 return;
512
513 last = inline_parm_levels.pop ();
514 for (i = 0; i < last; ++i)
515 {
516 --processing_template_decl;
517 current_template_parms = TREE_CHAIN (current_template_parms);
518 poplevel (0, 0, 0);
519 }
520 }
521
522 /* Return a new template argument vector which contains all of ARGS,
523 but has as its innermost set of arguments the EXTRA_ARGS. */
524
525 static tree
526 add_to_template_args (tree args, tree extra_args)
527 {
528 tree new_args;
529 int extra_depth;
530 int i;
531 int j;
532
533 if (args == NULL_TREE || extra_args == error_mark_node)
534 return extra_args;
535
536 extra_depth = TMPL_ARGS_DEPTH (extra_args);
537 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
538
539 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
540 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
541
542 for (j = 1; j <= extra_depth; ++j, ++i)
543 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
544
545 return new_args;
546 }
547
548 /* Like add_to_template_args, but only the outermost ARGS are added to
549 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
550 (EXTRA_ARGS) levels are added. This function is used to combine
551 the template arguments from a partial instantiation with the
552 template arguments used to attain the full instantiation from the
553 partial instantiation. */
554
555 static tree
556 add_outermost_template_args (tree args, tree extra_args)
557 {
558 tree new_args;
559
560 /* If there are more levels of EXTRA_ARGS than there are ARGS,
561 something very fishy is going on. */
562 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
563
564 /* If *all* the new arguments will be the EXTRA_ARGS, just return
565 them. */
566 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
567 return extra_args;
568
569 /* For the moment, we make ARGS look like it contains fewer levels. */
570 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
571
572 new_args = add_to_template_args (args, extra_args);
573
574 /* Now, we restore ARGS to its full dimensions. */
575 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
576
577 return new_args;
578 }
579
580 /* Return the N levels of innermost template arguments from the ARGS. */
581
582 tree
583 get_innermost_template_args (tree args, int n)
584 {
585 tree new_args;
586 int extra_levels;
587 int i;
588
589 gcc_assert (n >= 0);
590
591 /* If N is 1, just return the innermost set of template arguments. */
592 if (n == 1)
593 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
594
595 /* If we're not removing anything, just return the arguments we were
596 given. */
597 extra_levels = TMPL_ARGS_DEPTH (args) - n;
598 gcc_assert (extra_levels >= 0);
599 if (extra_levels == 0)
600 return args;
601
602 /* Make a new set of arguments, not containing the outer arguments. */
603 new_args = make_tree_vec (n);
604 for (i = 1; i <= n; ++i)
605 SET_TMPL_ARGS_LEVEL (new_args, i,
606 TMPL_ARGS_LEVEL (args, i + extra_levels));
607
608 return new_args;
609 }
610
611 /* The inverse of get_innermost_template_args: Return all but the innermost
612 EXTRA_LEVELS levels of template arguments from the ARGS. */
613
614 static tree
615 strip_innermost_template_args (tree args, int extra_levels)
616 {
617 tree new_args;
618 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
619 int i;
620
621 gcc_assert (n >= 0);
622
623 /* If N is 1, just return the outermost set of template arguments. */
624 if (n == 1)
625 return TMPL_ARGS_LEVEL (args, 1);
626
627 /* If we're not removing anything, just return the arguments we were
628 given. */
629 gcc_assert (extra_levels >= 0);
630 if (extra_levels == 0)
631 return args;
632
633 /* Make a new set of arguments, not containing the inner arguments. */
634 new_args = make_tree_vec (n);
635 for (i = 1; i <= n; ++i)
636 SET_TMPL_ARGS_LEVEL (new_args, i,
637 TMPL_ARGS_LEVEL (args, i));
638
639 return new_args;
640 }
641
642 /* We've got a template header coming up; push to a new level for storing
643 the parms. */
644
645 void
646 begin_template_parm_list (void)
647 {
648 /* We use a non-tag-transparent scope here, which causes pushtag to
649 put tags in this scope, rather than in the enclosing class or
650 namespace scope. This is the right thing, since we want
651 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
652 global template class, push_template_decl handles putting the
653 TEMPLATE_DECL into top-level scope. For a nested template class,
654 e.g.:
655
656 template <class T> struct S1 {
657 template <class T> struct S2 {};
658 };
659
660 pushtag contains special code to insert the TEMPLATE_DECL for S2
661 at the right scope. */
662 begin_scope (sk_template_parms, NULL);
663 ++processing_template_decl;
664 ++processing_template_parmlist;
665 note_template_header (0);
666
667 /* Add a dummy parameter level while we process the parameter list. */
668 current_template_parms
669 = tree_cons (size_int (processing_template_decl),
670 make_tree_vec (0),
671 current_template_parms);
672 }
673
674 /* This routine is called when a specialization is declared. If it is
675 invalid to declare a specialization here, an error is reported and
676 false is returned, otherwise this routine will return true. */
677
678 static bool
679 check_specialization_scope (void)
680 {
681 tree scope = current_scope ();
682
683 /* [temp.expl.spec]
684
685 An explicit specialization shall be declared in the namespace of
686 which the template is a member, or, for member templates, in the
687 namespace of which the enclosing class or enclosing class
688 template is a member. An explicit specialization of a member
689 function, member class or static data member of a class template
690 shall be declared in the namespace of which the class template
691 is a member. */
692 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
693 {
694 error ("explicit specialization in non-namespace scope %qD", scope);
695 return false;
696 }
697
698 /* [temp.expl.spec]
699
700 In an explicit specialization declaration for a member of a class
701 template or a member template that appears in namespace scope,
702 the member template and some of its enclosing class templates may
703 remain unspecialized, except that the declaration shall not
704 explicitly specialize a class member template if its enclosing
705 class templates are not explicitly specialized as well. */
706 if (current_template_parms)
707 {
708 error ("enclosing class templates are not explicitly specialized");
709 return false;
710 }
711
712 return true;
713 }
714
715 /* We've just seen template <>. */
716
717 bool
718 begin_specialization (void)
719 {
720 begin_scope (sk_template_spec, NULL);
721 note_template_header (1);
722 return check_specialization_scope ();
723 }
724
725 /* Called at then end of processing a declaration preceded by
726 template<>. */
727
728 void
729 end_specialization (void)
730 {
731 finish_scope ();
732 reset_specialization ();
733 }
734
735 /* Any template <>'s that we have seen thus far are not referring to a
736 function specialization. */
737
738 void
739 reset_specialization (void)
740 {
741 processing_specialization = 0;
742 template_header_count = 0;
743 }
744
745 /* We've just seen a template header. If SPECIALIZATION is nonzero,
746 it was of the form template <>. */
747
748 static void
749 note_template_header (int specialization)
750 {
751 processing_specialization = specialization;
752 template_header_count++;
753 }
754
755 /* We're beginning an explicit instantiation. */
756
757 void
758 begin_explicit_instantiation (void)
759 {
760 gcc_assert (!processing_explicit_instantiation);
761 processing_explicit_instantiation = true;
762 }
763
764
765 void
766 end_explicit_instantiation (void)
767 {
768 gcc_assert (processing_explicit_instantiation);
769 processing_explicit_instantiation = false;
770 }
771
772 /* An explicit specialization or partial specialization of TMPL is being
773 declared. Check that the namespace in which the specialization is
774 occurring is permissible. Returns false iff it is invalid to
775 specialize TMPL in the current namespace. */
776
777 static bool
778 check_specialization_namespace (tree tmpl)
779 {
780 tree tpl_ns = decl_namespace_context (tmpl);
781
782 /* [tmpl.expl.spec]
783
784 An explicit specialization shall be declared in a namespace enclosing the
785 specialized template. An explicit specialization whose declarator-id is
786 not qualified shall be declared in the nearest enclosing namespace of the
787 template, or, if the namespace is inline (7.3.1), any namespace from its
788 enclosing namespace set. */
789 if (current_scope() != DECL_CONTEXT (tmpl)
790 && !at_namespace_scope_p ())
791 {
792 error ("specialization of %qD must appear at namespace scope", tmpl);
793 return false;
794 }
795
796 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
797 /* Same or enclosing namespace. */
798 return true;
799 else
800 {
801 permerror (input_location,
802 "specialization of %qD in different namespace", tmpl);
803 inform (DECL_SOURCE_LOCATION (tmpl),
804 " from definition of %q#D", tmpl);
805 return false;
806 }
807 }
808
809 /* SPEC is an explicit instantiation. Check that it is valid to
810 perform this explicit instantiation in the current namespace. */
811
812 static void
813 check_explicit_instantiation_namespace (tree spec)
814 {
815 tree ns;
816
817 /* DR 275: An explicit instantiation shall appear in an enclosing
818 namespace of its template. */
819 ns = decl_namespace_context (spec);
820 if (!is_nested_namespace (current_namespace, ns))
821 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
822 "(which does not enclose namespace %qD)",
823 spec, current_namespace, ns);
824 }
825
826 // Returns the type of a template specialization only if that
827 // specialization needs to be defined. Otherwise (e.g., if the type has
828 // already been defined), the function returns NULL_TREE.
829 static tree
830 maybe_new_partial_specialization (tree type)
831 {
832 // An implicit instantiation of an incomplete type implies
833 // the definition of a new class template.
834 //
835 // template<typename T>
836 // struct S;
837 //
838 // template<typename T>
839 // struct S<T*>;
840 //
841 // Here, S<T*> is an implicit instantiation of S whose type
842 // is incomplete.
843 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
844 return type;
845
846 // It can also be the case that TYPE is a completed specialization.
847 // Continuing the previous example, suppose we also declare:
848 //
849 // template<typename T>
850 // requires Integral<T>
851 // struct S<T*>;
852 //
853 // Here, S<T*> refers to the specialization S<T*> defined
854 // above. However, we need to differentiate definitions because
855 // we intend to define a new partial specialization. In this case,
856 // we rely on the fact that the constraints are different for
857 // this declaration than that above.
858 //
859 // Note that we also get here for injected class names and
860 // late-parsed template definitions. We must ensure that we
861 // do not create new type declarations for those cases.
862 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
863 {
864 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
865 tree args = CLASSTYPE_TI_ARGS (type);
866
867 // If there are no template parameters, this cannot be a new
868 // partial template specializtion?
869 if (!current_template_parms)
870 return NULL_TREE;
871
872 // The injected-class-name is not a new partial specialization.
873 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
874 return NULL_TREE;
875
876 // If the constraints are not the same as those of the primary
877 // then, we can probably create a new specialization.
878 tree type_constr = current_template_constraints ();
879
880 if (type == TREE_TYPE (tmpl))
881 {
882 tree main_constr = get_constraints (tmpl);
883 if (equivalent_constraints (type_constr, main_constr))
884 return NULL_TREE;
885 }
886
887 // Also, if there's a pre-existing specialization with matching
888 // constraints, then this also isn't new.
889 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
890 while (specs)
891 {
892 tree spec_tmpl = TREE_VALUE (specs);
893 tree spec_args = TREE_PURPOSE (specs);
894 tree spec_constr = get_constraints (spec_tmpl);
895 if (comp_template_args (args, spec_args)
896 && equivalent_constraints (type_constr, spec_constr))
897 return NULL_TREE;
898 specs = TREE_CHAIN (specs);
899 }
900
901 // Create a new type node (and corresponding type decl)
902 // for the newly declared specialization.
903 tree t = make_class_type (TREE_CODE (type));
904 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
905 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
906
907 /* We only need a separate type node for storing the definition of this
908 partial specialization; uses of S<T*> are unconstrained, so all are
909 equivalent. So keep TYPE_CANONICAL the same. */
910 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
911
912 // Build the corresponding type decl.
913 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
914 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
915 DECL_SOURCE_LOCATION (d) = input_location;
916
917 return t;
918 }
919
920 return NULL_TREE;
921 }
922
923 /* The TYPE is being declared. If it is a template type, that means it
924 is a partial specialization. Do appropriate error-checking. */
925
926 tree
927 maybe_process_partial_specialization (tree type)
928 {
929 tree context;
930
931 if (type == error_mark_node)
932 return error_mark_node;
933
934 /* A lambda that appears in specialization context is not itself a
935 specialization. */
936 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
937 return type;
938
939 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
940 {
941 error ("name of class shadows template template parameter %qD",
942 TYPE_NAME (type));
943 return error_mark_node;
944 }
945
946 context = TYPE_CONTEXT (type);
947
948 if (TYPE_ALIAS_P (type))
949 {
950 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
951
952 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
953 error ("specialization of alias template %qD",
954 TI_TEMPLATE (tinfo));
955 else
956 error ("explicit specialization of non-template %qT", type);
957 return error_mark_node;
958 }
959 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
960 {
961 /* This is for ordinary explicit specialization and partial
962 specialization of a template class such as:
963
964 template <> class C<int>;
965
966 or:
967
968 template <class T> class C<T*>;
969
970 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
971
972 if (tree t = maybe_new_partial_specialization (type))
973 {
974 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
975 && !at_namespace_scope_p ())
976 return error_mark_node;
977 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
978 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
979 if (processing_template_decl)
980 {
981 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
982 if (decl == error_mark_node)
983 return error_mark_node;
984 return TREE_TYPE (decl);
985 }
986 }
987 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
988 error ("specialization of %qT after instantiation", type);
989 else if (errorcount && !processing_specialization
990 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
991 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
992 /* Trying to define a specialization either without a template<> header
993 or in an inappropriate place. We've already given an error, so just
994 bail now so we don't actually define the specialization. */
995 return error_mark_node;
996 }
997 else if (CLASS_TYPE_P (type)
998 && !CLASSTYPE_USE_TEMPLATE (type)
999 && CLASSTYPE_TEMPLATE_INFO (type)
1000 && context && CLASS_TYPE_P (context)
1001 && CLASSTYPE_TEMPLATE_INFO (context))
1002 {
1003 /* This is for an explicit specialization of member class
1004 template according to [temp.expl.spec/18]:
1005
1006 template <> template <class U> class C<int>::D;
1007
1008 The context `C<int>' must be an implicit instantiation.
1009 Otherwise this is just a member class template declared
1010 earlier like:
1011
1012 template <> class C<int> { template <class U> class D; };
1013 template <> template <class U> class C<int>::D;
1014
1015 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1016 while in the second case, `C<int>::D' is a primary template
1017 and `C<T>::D' may not exist. */
1018
1019 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1020 && !COMPLETE_TYPE_P (type))
1021 {
1022 tree t;
1023 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1024
1025 if (current_namespace
1026 != decl_namespace_context (tmpl))
1027 {
1028 permerror (input_location,
1029 "specializing %q#T in different namespace", type);
1030 permerror (DECL_SOURCE_LOCATION (tmpl),
1031 " from definition of %q#D", tmpl);
1032 }
1033
1034 /* Check for invalid specialization after instantiation:
1035
1036 template <> template <> class C<int>::D<int>;
1037 template <> template <class U> class C<int>::D; */
1038
1039 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1040 t; t = TREE_CHAIN (t))
1041 {
1042 tree inst = TREE_VALUE (t);
1043 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1044 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1045 {
1046 /* We already have a full specialization of this partial
1047 instantiation, or a full specialization has been
1048 looked up but not instantiated. Reassign it to the
1049 new member specialization template. */
1050 spec_entry elt;
1051 spec_entry *entry;
1052
1053 elt.tmpl = most_general_template (tmpl);
1054 elt.args = CLASSTYPE_TI_ARGS (inst);
1055 elt.spec = inst;
1056
1057 type_specializations->remove_elt (&elt);
1058
1059 elt.tmpl = tmpl;
1060 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1061
1062 spec_entry **slot
1063 = type_specializations->find_slot (&elt, INSERT);
1064 entry = ggc_alloc<spec_entry> ();
1065 *entry = elt;
1066 *slot = entry;
1067 }
1068 else
1069 /* But if we've had an implicit instantiation, that's a
1070 problem ([temp.expl.spec]/6). */
1071 error ("specialization %qT after instantiation %qT",
1072 type, inst);
1073 }
1074
1075 /* Mark TYPE as a specialization. And as a result, we only
1076 have one level of template argument for the innermost
1077 class template. */
1078 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1079 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1080 CLASSTYPE_TI_ARGS (type)
1081 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1082 }
1083 }
1084 else if (processing_specialization)
1085 {
1086 /* Someday C++0x may allow for enum template specialization. */
1087 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1088 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1089 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1090 "of %qD not allowed by ISO C++", type);
1091 else
1092 {
1093 error ("explicit specialization of non-template %qT", type);
1094 return error_mark_node;
1095 }
1096 }
1097
1098 return type;
1099 }
1100
1101 /* Returns nonzero if we can optimize the retrieval of specializations
1102 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1103 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1104
1105 static inline bool
1106 optimize_specialization_lookup_p (tree tmpl)
1107 {
1108 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1109 && DECL_CLASS_SCOPE_P (tmpl)
1110 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1111 parameter. */
1112 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1113 /* The optimized lookup depends on the fact that the
1114 template arguments for the member function template apply
1115 purely to the containing class, which is not true if the
1116 containing class is an explicit or partial
1117 specialization. */
1118 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1119 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1120 && !DECL_CONV_FN_P (tmpl)
1121 /* It is possible to have a template that is not a member
1122 template and is not a member of a template class:
1123
1124 template <typename T>
1125 struct S { friend A::f(); };
1126
1127 Here, the friend function is a template, but the context does
1128 not have template information. The optimized lookup relies
1129 on having ARGS be the template arguments for both the class
1130 and the function template. */
1131 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1132 }
1133
1134 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1135 gone through coerce_template_parms by now. */
1136
1137 static void
1138 verify_unstripped_args_1 (tree inner)
1139 {
1140 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1141 {
1142 tree arg = TREE_VEC_ELT (inner, i);
1143 if (TREE_CODE (arg) == TEMPLATE_DECL)
1144 /* OK */;
1145 else if (TYPE_P (arg))
1146 gcc_assert (strip_typedefs (arg, NULL) == arg);
1147 else if (ARGUMENT_PACK_P (arg))
1148 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1149 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1150 /* Allow typedefs on the type of a non-type argument, since a
1151 parameter can have them. */;
1152 else
1153 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1154 }
1155 }
1156
1157 static void
1158 verify_unstripped_args (tree args)
1159 {
1160 ++processing_template_decl;
1161 if (!any_dependent_template_arguments_p (args))
1162 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1163 --processing_template_decl;
1164 }
1165
1166 /* Retrieve the specialization (in the sense of [temp.spec] - a
1167 specialization is either an instantiation or an explicit
1168 specialization) of TMPL for the given template ARGS. If there is
1169 no such specialization, return NULL_TREE. The ARGS are a vector of
1170 arguments, or a vector of vectors of arguments, in the case of
1171 templates with more than one level of parameters.
1172
1173 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1174 then we search for a partial specialization matching ARGS. This
1175 parameter is ignored if TMPL is not a class template.
1176
1177 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1178 result is a NONTYPE_ARGUMENT_PACK. */
1179
1180 static tree
1181 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1182 {
1183 if (tmpl == NULL_TREE)
1184 return NULL_TREE;
1185
1186 if (args == error_mark_node)
1187 return NULL_TREE;
1188
1189 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1190 || TREE_CODE (tmpl) == FIELD_DECL);
1191
1192 /* There should be as many levels of arguments as there are
1193 levels of parameters. */
1194 gcc_assert (TMPL_ARGS_DEPTH (args)
1195 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1196 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1197 : template_class_depth (DECL_CONTEXT (tmpl))));
1198
1199 if (flag_checking)
1200 verify_unstripped_args (args);
1201
1202 /* Lambda functions in templates aren't instantiated normally, but through
1203 tsubst_lambda_expr. */
1204 if (lambda_fn_in_template_p (tmpl))
1205 return NULL_TREE;
1206
1207 if (optimize_specialization_lookup_p (tmpl))
1208 {
1209 /* The template arguments actually apply to the containing
1210 class. Find the class specialization with those
1211 arguments. */
1212 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1213 tree class_specialization
1214 = retrieve_specialization (class_template, args, 0);
1215 if (!class_specialization)
1216 return NULL_TREE;
1217
1218 /* Find the instance of TMPL. */
1219 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1220 for (ovl_iterator iter (fns); iter; ++iter)
1221 {
1222 tree fn = *iter;
1223 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1224 /* using-declarations can add base methods to the method vec,
1225 and we don't want those here. */
1226 && DECL_CONTEXT (fn) == class_specialization)
1227 return fn;
1228 }
1229 return NULL_TREE;
1230 }
1231 else
1232 {
1233 spec_entry *found;
1234 spec_entry elt;
1235 hash_table<spec_hasher> *specializations;
1236
1237 elt.tmpl = tmpl;
1238 elt.args = args;
1239 elt.spec = NULL_TREE;
1240
1241 if (DECL_CLASS_TEMPLATE_P (tmpl))
1242 specializations = type_specializations;
1243 else
1244 specializations = decl_specializations;
1245
1246 if (hash == 0)
1247 hash = spec_hasher::hash (&elt);
1248 found = specializations->find_with_hash (&elt, hash);
1249 if (found)
1250 return found->spec;
1251 }
1252
1253 return NULL_TREE;
1254 }
1255
1256 /* Like retrieve_specialization, but for local declarations. */
1257
1258 tree
1259 retrieve_local_specialization (tree tmpl)
1260 {
1261 if (local_specializations == NULL)
1262 return NULL_TREE;
1263
1264 tree *slot = local_specializations->get (tmpl);
1265 return slot ? *slot : NULL_TREE;
1266 }
1267
1268 /* Returns nonzero iff DECL is a specialization of TMPL. */
1269
1270 int
1271 is_specialization_of (tree decl, tree tmpl)
1272 {
1273 tree t;
1274
1275 if (TREE_CODE (decl) == FUNCTION_DECL)
1276 {
1277 for (t = decl;
1278 t != NULL_TREE;
1279 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1280 if (t == tmpl)
1281 return 1;
1282 }
1283 else
1284 {
1285 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1286
1287 for (t = TREE_TYPE (decl);
1288 t != NULL_TREE;
1289 t = CLASSTYPE_USE_TEMPLATE (t)
1290 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1291 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1292 return 1;
1293 }
1294
1295 return 0;
1296 }
1297
1298 /* Returns nonzero iff DECL is a specialization of friend declaration
1299 FRIEND_DECL according to [temp.friend]. */
1300
1301 bool
1302 is_specialization_of_friend (tree decl, tree friend_decl)
1303 {
1304 bool need_template = true;
1305 int template_depth;
1306
1307 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1308 || TREE_CODE (decl) == TYPE_DECL);
1309
1310 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1311 of a template class, we want to check if DECL is a specialization
1312 if this. */
1313 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1314 && DECL_TEMPLATE_INFO (friend_decl)
1315 && !DECL_USE_TEMPLATE (friend_decl))
1316 {
1317 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1318 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1319 need_template = false;
1320 }
1321 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1322 && !PRIMARY_TEMPLATE_P (friend_decl))
1323 need_template = false;
1324
1325 /* There is nothing to do if this is not a template friend. */
1326 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1327 return false;
1328
1329 if (is_specialization_of (decl, friend_decl))
1330 return true;
1331
1332 /* [temp.friend/6]
1333 A member of a class template may be declared to be a friend of a
1334 non-template class. In this case, the corresponding member of
1335 every specialization of the class template is a friend of the
1336 class granting friendship.
1337
1338 For example, given a template friend declaration
1339
1340 template <class T> friend void A<T>::f();
1341
1342 the member function below is considered a friend
1343
1344 template <> struct A<int> {
1345 void f();
1346 };
1347
1348 For this type of template friend, TEMPLATE_DEPTH below will be
1349 nonzero. To determine if DECL is a friend of FRIEND, we first
1350 check if the enclosing class is a specialization of another. */
1351
1352 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1353 if (template_depth
1354 && DECL_CLASS_SCOPE_P (decl)
1355 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1356 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1357 {
1358 /* Next, we check the members themselves. In order to handle
1359 a few tricky cases, such as when FRIEND_DECL's are
1360
1361 template <class T> friend void A<T>::g(T t);
1362 template <class T> template <T t> friend void A<T>::h();
1363
1364 and DECL's are
1365
1366 void A<int>::g(int);
1367 template <int> void A<int>::h();
1368
1369 we need to figure out ARGS, the template arguments from
1370 the context of DECL. This is required for template substitution
1371 of `T' in the function parameter of `g' and template parameter
1372 of `h' in the above examples. Here ARGS corresponds to `int'. */
1373
1374 tree context = DECL_CONTEXT (decl);
1375 tree args = NULL_TREE;
1376 int current_depth = 0;
1377
1378 while (current_depth < template_depth)
1379 {
1380 if (CLASSTYPE_TEMPLATE_INFO (context))
1381 {
1382 if (current_depth == 0)
1383 args = TYPE_TI_ARGS (context);
1384 else
1385 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1386 current_depth++;
1387 }
1388 context = TYPE_CONTEXT (context);
1389 }
1390
1391 if (TREE_CODE (decl) == FUNCTION_DECL)
1392 {
1393 bool is_template;
1394 tree friend_type;
1395 tree decl_type;
1396 tree friend_args_type;
1397 tree decl_args_type;
1398
1399 /* Make sure that both DECL and FRIEND_DECL are templates or
1400 non-templates. */
1401 is_template = DECL_TEMPLATE_INFO (decl)
1402 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1403 if (need_template ^ is_template)
1404 return false;
1405 else if (is_template)
1406 {
1407 /* If both are templates, check template parameter list. */
1408 tree friend_parms
1409 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1410 args, tf_none);
1411 if (!comp_template_parms
1412 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1413 friend_parms))
1414 return false;
1415
1416 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1417 }
1418 else
1419 decl_type = TREE_TYPE (decl);
1420
1421 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1422 tf_none, NULL_TREE);
1423 if (friend_type == error_mark_node)
1424 return false;
1425
1426 /* Check if return types match. */
1427 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1428 return false;
1429
1430 /* Check if function parameter types match, ignoring the
1431 `this' parameter. */
1432 friend_args_type = TYPE_ARG_TYPES (friend_type);
1433 decl_args_type = TYPE_ARG_TYPES (decl_type);
1434 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1435 friend_args_type = TREE_CHAIN (friend_args_type);
1436 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1437 decl_args_type = TREE_CHAIN (decl_args_type);
1438
1439 return compparms (decl_args_type, friend_args_type);
1440 }
1441 else
1442 {
1443 /* DECL is a TYPE_DECL */
1444 bool is_template;
1445 tree decl_type = TREE_TYPE (decl);
1446
1447 /* Make sure that both DECL and FRIEND_DECL are templates or
1448 non-templates. */
1449 is_template
1450 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1451 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1452
1453 if (need_template ^ is_template)
1454 return false;
1455 else if (is_template)
1456 {
1457 tree friend_parms;
1458 /* If both are templates, check the name of the two
1459 TEMPLATE_DECL's first because is_friend didn't. */
1460 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1461 != DECL_NAME (friend_decl))
1462 return false;
1463
1464 /* Now check template parameter list. */
1465 friend_parms
1466 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1467 args, tf_none);
1468 return comp_template_parms
1469 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1470 friend_parms);
1471 }
1472 else
1473 return (DECL_NAME (decl)
1474 == DECL_NAME (friend_decl));
1475 }
1476 }
1477 return false;
1478 }
1479
1480 /* Register the specialization SPEC as a specialization of TMPL with
1481 the indicated ARGS. IS_FRIEND indicates whether the specialization
1482 is actually just a friend declaration. ATTRLIST is the list of
1483 attributes that the specialization is declared with or NULL when
1484 it isn't. Returns SPEC, or an equivalent prior declaration, if
1485 available.
1486
1487 We also store instantiations of field packs in the hash table, even
1488 though they are not themselves templates, to make lookup easier. */
1489
1490 static tree
1491 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1492 hashval_t hash)
1493 {
1494 tree fn;
1495 spec_entry **slot = NULL;
1496 spec_entry elt;
1497
1498 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1499 || (TREE_CODE (tmpl) == FIELD_DECL
1500 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1501
1502 if (TREE_CODE (spec) == FUNCTION_DECL
1503 && uses_template_parms (DECL_TI_ARGS (spec)))
1504 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1505 register it; we want the corresponding TEMPLATE_DECL instead.
1506 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1507 the more obvious `uses_template_parms (spec)' to avoid problems
1508 with default function arguments. In particular, given
1509 something like this:
1510
1511 template <class T> void f(T t1, T t = T())
1512
1513 the default argument expression is not substituted for in an
1514 instantiation unless and until it is actually needed. */
1515 return spec;
1516
1517 if (optimize_specialization_lookup_p (tmpl))
1518 /* We don't put these specializations in the hash table, but we might
1519 want to give an error about a mismatch. */
1520 fn = retrieve_specialization (tmpl, args, 0);
1521 else
1522 {
1523 elt.tmpl = tmpl;
1524 elt.args = args;
1525 elt.spec = spec;
1526
1527 if (hash == 0)
1528 hash = spec_hasher::hash (&elt);
1529
1530 slot =
1531 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1532 if (*slot)
1533 fn = ((spec_entry *) *slot)->spec;
1534 else
1535 fn = NULL_TREE;
1536 }
1537
1538 /* We can sometimes try to re-register a specialization that we've
1539 already got. In particular, regenerate_decl_from_template calls
1540 duplicate_decls which will update the specialization list. But,
1541 we'll still get called again here anyhow. It's more convenient
1542 to simply allow this than to try to prevent it. */
1543 if (fn == spec)
1544 return spec;
1545 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1546 {
1547 if (DECL_TEMPLATE_INSTANTIATION (fn))
1548 {
1549 if (DECL_ODR_USED (fn)
1550 || DECL_EXPLICIT_INSTANTIATION (fn))
1551 {
1552 error ("specialization of %qD after instantiation",
1553 fn);
1554 return error_mark_node;
1555 }
1556 else
1557 {
1558 tree clone;
1559 /* This situation should occur only if the first
1560 specialization is an implicit instantiation, the
1561 second is an explicit specialization, and the
1562 implicit instantiation has not yet been used. That
1563 situation can occur if we have implicitly
1564 instantiated a member function and then specialized
1565 it later.
1566
1567 We can also wind up here if a friend declaration that
1568 looked like an instantiation turns out to be a
1569 specialization:
1570
1571 template <class T> void foo(T);
1572 class S { friend void foo<>(int) };
1573 template <> void foo(int);
1574
1575 We transform the existing DECL in place so that any
1576 pointers to it become pointers to the updated
1577 declaration.
1578
1579 If there was a definition for the template, but not
1580 for the specialization, we want this to look as if
1581 there were no definition, and vice versa. */
1582 DECL_INITIAL (fn) = NULL_TREE;
1583 duplicate_decls (spec, fn, is_friend);
1584 /* The call to duplicate_decls will have applied
1585 [temp.expl.spec]:
1586
1587 An explicit specialization of a function template
1588 is inline only if it is explicitly declared to be,
1589 and independently of whether its function template
1590 is.
1591
1592 to the primary function; now copy the inline bits to
1593 the various clones. */
1594 FOR_EACH_CLONE (clone, fn)
1595 {
1596 DECL_DECLARED_INLINE_P (clone)
1597 = DECL_DECLARED_INLINE_P (fn);
1598 DECL_SOURCE_LOCATION (clone)
1599 = DECL_SOURCE_LOCATION (fn);
1600 DECL_DELETED_FN (clone)
1601 = DECL_DELETED_FN (fn);
1602 }
1603 check_specialization_namespace (tmpl);
1604
1605 return fn;
1606 }
1607 }
1608 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1609 {
1610 tree dd = duplicate_decls (spec, fn, is_friend);
1611 if (dd == error_mark_node)
1612 /* We've already complained in duplicate_decls. */
1613 return error_mark_node;
1614
1615 if (dd == NULL_TREE && DECL_INITIAL (spec))
1616 /* Dup decl failed, but this is a new definition. Set the
1617 line number so any errors match this new
1618 definition. */
1619 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1620
1621 return fn;
1622 }
1623 }
1624 else if (fn)
1625 return duplicate_decls (spec, fn, is_friend);
1626
1627 /* A specialization must be declared in the same namespace as the
1628 template it is specializing. */
1629 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1630 && !check_specialization_namespace (tmpl))
1631 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1632
1633 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1634 {
1635 spec_entry *entry = ggc_alloc<spec_entry> ();
1636 gcc_assert (tmpl && args && spec);
1637 *entry = elt;
1638 *slot = entry;
1639 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1640 && PRIMARY_TEMPLATE_P (tmpl)
1641 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1642 || variable_template_p (tmpl))
1643 /* If TMPL is a forward declaration of a template function, keep a list
1644 of all specializations in case we need to reassign them to a friend
1645 template later in tsubst_friend_function.
1646
1647 Also keep a list of all variable template instantiations so that
1648 process_partial_specialization can check whether a later partial
1649 specialization would have used it. */
1650 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1651 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1652 }
1653
1654 return spec;
1655 }
1656
1657 /* Returns true iff two spec_entry nodes are equivalent. */
1658
1659 int comparing_specializations;
1660
1661 bool
1662 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1663 {
1664 int equal;
1665
1666 ++comparing_specializations;
1667 equal = (e1->tmpl == e2->tmpl
1668 && comp_template_args (e1->args, e2->args));
1669 if (equal && flag_concepts
1670 /* tmpl could be a FIELD_DECL for a capture pack. */
1671 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1672 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1673 && uses_template_parms (e1->args))
1674 {
1675 /* Partial specializations of a variable template can be distinguished by
1676 constraints. */
1677 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1678 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1679 equal = equivalent_constraints (c1, c2);
1680 }
1681 --comparing_specializations;
1682
1683 return equal;
1684 }
1685
1686 /* Returns a hash for a template TMPL and template arguments ARGS. */
1687
1688 static hashval_t
1689 hash_tmpl_and_args (tree tmpl, tree args)
1690 {
1691 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1692 return iterative_hash_template_arg (args, val);
1693 }
1694
1695 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1696 ignoring SPEC. */
1697
1698 hashval_t
1699 spec_hasher::hash (spec_entry *e)
1700 {
1701 return hash_tmpl_and_args (e->tmpl, e->args);
1702 }
1703
1704 /* Recursively calculate a hash value for a template argument ARG, for use
1705 in the hash tables of template specializations. */
1706
1707 hashval_t
1708 iterative_hash_template_arg (tree arg, hashval_t val)
1709 {
1710 unsigned HOST_WIDE_INT i;
1711 enum tree_code code;
1712 char tclass;
1713
1714 if (arg == NULL_TREE)
1715 return iterative_hash_object (arg, val);
1716
1717 if (!TYPE_P (arg))
1718 STRIP_NOPS (arg);
1719
1720 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1721 gcc_unreachable ();
1722
1723 code = TREE_CODE (arg);
1724 tclass = TREE_CODE_CLASS (code);
1725
1726 val = iterative_hash_object (code, val);
1727
1728 switch (code)
1729 {
1730 case ERROR_MARK:
1731 return val;
1732
1733 case IDENTIFIER_NODE:
1734 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1735
1736 case TREE_VEC:
1737 {
1738 int i, len = TREE_VEC_LENGTH (arg);
1739 for (i = 0; i < len; ++i)
1740 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1741 return val;
1742 }
1743
1744 case TYPE_PACK_EXPANSION:
1745 case EXPR_PACK_EXPANSION:
1746 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1747 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1748
1749 case TYPE_ARGUMENT_PACK:
1750 case NONTYPE_ARGUMENT_PACK:
1751 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1752
1753 case TREE_LIST:
1754 for (; arg; arg = TREE_CHAIN (arg))
1755 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1756 return val;
1757
1758 case OVERLOAD:
1759 for (lkp_iterator iter (arg); iter; ++iter)
1760 val = iterative_hash_template_arg (*iter, val);
1761 return val;
1762
1763 case CONSTRUCTOR:
1764 {
1765 tree field, value;
1766 iterative_hash_template_arg (TREE_TYPE (arg), val);
1767 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1768 {
1769 val = iterative_hash_template_arg (field, val);
1770 val = iterative_hash_template_arg (value, val);
1771 }
1772 return val;
1773 }
1774
1775 case PARM_DECL:
1776 if (!DECL_ARTIFICIAL (arg))
1777 {
1778 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1779 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1780 }
1781 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1782
1783 case TARGET_EXPR:
1784 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1785
1786 case PTRMEM_CST:
1787 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1788 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1789
1790 case TEMPLATE_PARM_INDEX:
1791 val = iterative_hash_template_arg
1792 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1793 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1794 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1795
1796 case TRAIT_EXPR:
1797 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1798 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1799 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1800
1801 case BASELINK:
1802 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1803 val);
1804 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1805 val);
1806
1807 case MODOP_EXPR:
1808 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1809 code = TREE_CODE (TREE_OPERAND (arg, 1));
1810 val = iterative_hash_object (code, val);
1811 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1812
1813 case LAMBDA_EXPR:
1814 /* A lambda can't appear in a template arg, but don't crash on
1815 erroneous input. */
1816 gcc_assert (seen_error ());
1817 return val;
1818
1819 case CAST_EXPR:
1820 case IMPLICIT_CONV_EXPR:
1821 case STATIC_CAST_EXPR:
1822 case REINTERPRET_CAST_EXPR:
1823 case CONST_CAST_EXPR:
1824 case DYNAMIC_CAST_EXPR:
1825 case NEW_EXPR:
1826 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1827 /* Now hash operands as usual. */
1828 break;
1829
1830 default:
1831 break;
1832 }
1833
1834 switch (tclass)
1835 {
1836 case tcc_type:
1837 if (alias_template_specialization_p (arg))
1838 {
1839 // We want an alias specialization that survived strip_typedefs
1840 // to hash differently from its TYPE_CANONICAL, to avoid hash
1841 // collisions that compare as different in template_args_equal.
1842 // These could be dependent specializations that strip_typedefs
1843 // left alone, or untouched specializations because
1844 // coerce_template_parms returns the unconverted template
1845 // arguments if it sees incomplete argument packs.
1846 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1847 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1848 }
1849 if (TYPE_CANONICAL (arg))
1850 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1851 val);
1852 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1853 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1854 /* Otherwise just compare the types during lookup. */
1855 return val;
1856
1857 case tcc_declaration:
1858 case tcc_constant:
1859 return iterative_hash_expr (arg, val);
1860
1861 default:
1862 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1863 {
1864 unsigned n = cp_tree_operand_length (arg);
1865 for (i = 0; i < n; ++i)
1866 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1867 return val;
1868 }
1869 }
1870 gcc_unreachable ();
1871 return 0;
1872 }
1873
1874 /* Unregister the specialization SPEC as a specialization of TMPL.
1875 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1876 if the SPEC was listed as a specialization of TMPL.
1877
1878 Note that SPEC has been ggc_freed, so we can't look inside it. */
1879
1880 bool
1881 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1882 {
1883 spec_entry *entry;
1884 spec_entry elt;
1885
1886 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1887 elt.args = TI_ARGS (tinfo);
1888 elt.spec = NULL_TREE;
1889
1890 entry = decl_specializations->find (&elt);
1891 if (entry != NULL)
1892 {
1893 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1894 gcc_assert (new_spec != NULL_TREE);
1895 entry->spec = new_spec;
1896 return 1;
1897 }
1898
1899 return 0;
1900 }
1901
1902 /* Like register_specialization, but for local declarations. We are
1903 registering SPEC, an instantiation of TMPL. */
1904
1905 void
1906 register_local_specialization (tree spec, tree tmpl)
1907 {
1908 gcc_assert (tmpl != spec);
1909 local_specializations->put (tmpl, spec);
1910 }
1911
1912 /* TYPE is a class type. Returns true if TYPE is an explicitly
1913 specialized class. */
1914
1915 bool
1916 explicit_class_specialization_p (tree type)
1917 {
1918 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1919 return false;
1920 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1921 }
1922
1923 /* Print the list of functions at FNS, going through all the overloads
1924 for each element of the list. Alternatively, FNS can not be a
1925 TREE_LIST, in which case it will be printed together with all the
1926 overloads.
1927
1928 MORE and *STR should respectively be FALSE and NULL when the function
1929 is called from the outside. They are used internally on recursive
1930 calls. print_candidates manages the two parameters and leaves NULL
1931 in *STR when it ends. */
1932
1933 static void
1934 print_candidates_1 (tree fns, char **str, bool more = false)
1935 {
1936 if (TREE_CODE (fns) == TREE_LIST)
1937 for (; fns; fns = TREE_CHAIN (fns))
1938 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1939 else
1940 for (lkp_iterator iter (fns); iter;)
1941 {
1942 tree cand = *iter;
1943 ++iter;
1944
1945 const char *pfx = *str;
1946 if (!pfx)
1947 {
1948 if (more || iter)
1949 pfx = _("candidates are:");
1950 else
1951 pfx = _("candidate is:");
1952 *str = get_spaces (pfx);
1953 }
1954 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1955 }
1956 }
1957
1958 /* Print the list of candidate FNS in an error message. FNS can also
1959 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1960
1961 void
1962 print_candidates (tree fns)
1963 {
1964 char *str = NULL;
1965 print_candidates_1 (fns, &str);
1966 free (str);
1967 }
1968
1969 /* Get a (possibly) constrained template declaration for the
1970 purpose of ordering candidates. */
1971 static tree
1972 get_template_for_ordering (tree list)
1973 {
1974 gcc_assert (TREE_CODE (list) == TREE_LIST);
1975 tree f = TREE_VALUE (list);
1976 if (tree ti = DECL_TEMPLATE_INFO (f))
1977 return TI_TEMPLATE (ti);
1978 return f;
1979 }
1980
1981 /* Among candidates having the same signature, return the
1982 most constrained or NULL_TREE if there is no best candidate.
1983 If the signatures of candidates vary (e.g., template
1984 specialization vs. member function), then there can be no
1985 most constrained.
1986
1987 Note that we don't compare constraints on the functions
1988 themselves, but rather those of their templates. */
1989 static tree
1990 most_constrained_function (tree candidates)
1991 {
1992 // Try to find the best candidate in a first pass.
1993 tree champ = candidates;
1994 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1995 {
1996 int winner = more_constrained (get_template_for_ordering (champ),
1997 get_template_for_ordering (c));
1998 if (winner == -1)
1999 champ = c; // The candidate is more constrained
2000 else if (winner == 0)
2001 return NULL_TREE; // Neither is more constrained
2002 }
2003
2004 // Verify that the champ is better than previous candidates.
2005 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2006 if (!more_constrained (get_template_for_ordering (champ),
2007 get_template_for_ordering (c)))
2008 return NULL_TREE;
2009 }
2010
2011 return champ;
2012 }
2013
2014
2015 /* Returns the template (one of the functions given by TEMPLATE_ID)
2016 which can be specialized to match the indicated DECL with the
2017 explicit template args given in TEMPLATE_ID. The DECL may be
2018 NULL_TREE if none is available. In that case, the functions in
2019 TEMPLATE_ID are non-members.
2020
2021 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2022 specialization of a member template.
2023
2024 The TEMPLATE_COUNT is the number of references to qualifying
2025 template classes that appeared in the name of the function. See
2026 check_explicit_specialization for a more accurate description.
2027
2028 TSK indicates what kind of template declaration (if any) is being
2029 declared. TSK_TEMPLATE indicates that the declaration given by
2030 DECL, though a FUNCTION_DECL, has template parameters, and is
2031 therefore a template function.
2032
2033 The template args (those explicitly specified and those deduced)
2034 are output in a newly created vector *TARGS_OUT.
2035
2036 If it is impossible to determine the result, an error message is
2037 issued. The error_mark_node is returned to indicate failure. */
2038
2039 static tree
2040 determine_specialization (tree template_id,
2041 tree decl,
2042 tree* targs_out,
2043 int need_member_template,
2044 int template_count,
2045 tmpl_spec_kind tsk)
2046 {
2047 tree fns;
2048 tree targs;
2049 tree explicit_targs;
2050 tree candidates = NULL_TREE;
2051
2052 /* A TREE_LIST of templates of which DECL may be a specialization.
2053 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2054 corresponding TREE_PURPOSE is the set of template arguments that,
2055 when used to instantiate the template, would produce a function
2056 with the signature of DECL. */
2057 tree templates = NULL_TREE;
2058 int header_count;
2059 cp_binding_level *b;
2060
2061 *targs_out = NULL_TREE;
2062
2063 if (template_id == error_mark_node || decl == error_mark_node)
2064 return error_mark_node;
2065
2066 /* We shouldn't be specializing a member template of an
2067 unspecialized class template; we already gave an error in
2068 check_specialization_scope, now avoid crashing. */
2069 if (!VAR_P (decl)
2070 && template_count && DECL_CLASS_SCOPE_P (decl)
2071 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2072 {
2073 gcc_assert (errorcount);
2074 return error_mark_node;
2075 }
2076
2077 fns = TREE_OPERAND (template_id, 0);
2078 explicit_targs = TREE_OPERAND (template_id, 1);
2079
2080 if (fns == error_mark_node)
2081 return error_mark_node;
2082
2083 /* Check for baselinks. */
2084 if (BASELINK_P (fns))
2085 fns = BASELINK_FUNCTIONS (fns);
2086
2087 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2088 {
2089 error ("%qD is not a function template", fns);
2090 return error_mark_node;
2091 }
2092 else if (VAR_P (decl) && !variable_template_p (fns))
2093 {
2094 error ("%qD is not a variable template", fns);
2095 return error_mark_node;
2096 }
2097
2098 /* Count the number of template headers specified for this
2099 specialization. */
2100 header_count = 0;
2101 for (b = current_binding_level;
2102 b->kind == sk_template_parms;
2103 b = b->level_chain)
2104 ++header_count;
2105
2106 tree orig_fns = fns;
2107
2108 if (variable_template_p (fns))
2109 {
2110 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2111 targs = coerce_template_parms (parms, explicit_targs, fns,
2112 tf_warning_or_error,
2113 /*req_all*/true, /*use_defarg*/true);
2114 if (targs != error_mark_node)
2115 templates = tree_cons (targs, fns, templates);
2116 }
2117 else for (lkp_iterator iter (fns); iter; ++iter)
2118 {
2119 tree fn = *iter;
2120
2121 if (TREE_CODE (fn) == TEMPLATE_DECL)
2122 {
2123 tree decl_arg_types;
2124 tree fn_arg_types;
2125 tree insttype;
2126
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:
2132
2133 template <> void A<int>::f(int);
2134 template <> template <> void A<int>::f(int);
2135
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.
2142
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).
2149
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. */
2153 if (header_count && header_count != template_count + 1)
2154 continue;
2155
2156 /* Check that the number of template arguments at the
2157 innermost level for DECL is the same as for FN. */
2158 if (current_binding_level->kind == sk_template_parms
2159 && !current_binding_level->explicit_spec_p
2160 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2161 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2162 (current_template_parms))))
2163 continue;
2164
2165 /* DECL might be a specialization of FN. */
2166 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2167 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2168
2169 /* For a non-static member function, we need to make sure
2170 that the const qualification is the same. Since
2171 get_bindings does not try to merge the "this" parameter,
2172 we must do the comparison explicitly. */
2173 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2174 {
2175 if (!same_type_p (TREE_VALUE (fn_arg_types),
2176 TREE_VALUE (decl_arg_types)))
2177 continue;
2178
2179 /* And the ref-qualification. */
2180 if (type_memfn_rqual (TREE_TYPE (decl))
2181 != type_memfn_rqual (TREE_TYPE (fn)))
2182 continue;
2183 }
2184
2185 /* Skip the "this" parameter and, for constructors of
2186 classes with virtual bases, the VTT parameter. A
2187 full specialization of a constructor will have a VTT
2188 parameter, but a template never will. */
2189 decl_arg_types
2190 = skip_artificial_parms_for (decl, decl_arg_types);
2191 fn_arg_types
2192 = skip_artificial_parms_for (fn, fn_arg_types);
2193
2194 /* Function templates cannot be specializations; there are
2195 no partial specializations of functions. Therefore, if
2196 the type of DECL does not match FN, there is no
2197 match.
2198
2199 Note that it should never be the case that we have both
2200 candidates added here, and for regular member functions
2201 below. */
2202 if (tsk == tsk_template)
2203 {
2204 if (compparms (fn_arg_types, decl_arg_types))
2205 candidates = tree_cons (NULL_TREE, fn, candidates);
2206 continue;
2207 }
2208
2209 /* See whether this function might be a specialization of this
2210 template. Suppress access control because we might be trying
2211 to make this specialization a friend, and we have already done
2212 access control for the declaration of the specialization. */
2213 push_deferring_access_checks (dk_no_check);
2214 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2215 pop_deferring_access_checks ();
2216
2217 if (!targs)
2218 /* We cannot deduce template arguments that when used to
2219 specialize TMPL will produce DECL. */
2220 continue;
2221
2222 if (uses_template_parms (targs))
2223 /* We deduced something involving 'auto', which isn't a valid
2224 template argument. */
2225 continue;
2226
2227 /* Remove, from the set of candidates, all those functions
2228 whose constraints are not satisfied. */
2229 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2230 continue;
2231
2232 // Then, try to form the new function type.
2233 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2234 if (insttype == error_mark_node)
2235 continue;
2236 fn_arg_types
2237 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2238 if (!compparms (fn_arg_types, decl_arg_types))
2239 continue;
2240
2241 /* Save this template, and the arguments deduced. */
2242 templates = tree_cons (targs, fn, templates);
2243 }
2244 else if (need_member_template)
2245 /* FN is an ordinary member function, and we need a
2246 specialization of a member template. */
2247 ;
2248 else if (TREE_CODE (fn) != FUNCTION_DECL)
2249 /* We can get IDENTIFIER_NODEs here in certain erroneous
2250 cases. */
2251 ;
2252 else if (!DECL_FUNCTION_MEMBER_P (fn))
2253 /* This is just an ordinary non-member function. Nothing can
2254 be a specialization of that. */
2255 ;
2256 else if (DECL_ARTIFICIAL (fn))
2257 /* Cannot specialize functions that are created implicitly. */
2258 ;
2259 else
2260 {
2261 tree decl_arg_types;
2262
2263 /* This is an ordinary member function. However, since
2264 we're here, we can assume its enclosing class is a
2265 template class. For example,
2266
2267 template <typename T> struct S { void f(); };
2268 template <> void S<int>::f() {}
2269
2270 Here, S<int>::f is a non-template, but S<int> is a
2271 template class. If FN has the same type as DECL, we
2272 might be in business. */
2273
2274 if (!DECL_TEMPLATE_INFO (fn))
2275 /* Its enclosing class is an explicit specialization
2276 of a template class. This is not a candidate. */
2277 continue;
2278
2279 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2280 TREE_TYPE (TREE_TYPE (fn))))
2281 /* The return types differ. */
2282 continue;
2283
2284 /* Adjust the type of DECL in case FN is a static member. */
2285 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2286 if (DECL_STATIC_FUNCTION_P (fn)
2287 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2288 decl_arg_types = TREE_CHAIN (decl_arg_types);
2289
2290 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2291 decl_arg_types))
2292 continue;
2293
2294 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2295 && (type_memfn_rqual (TREE_TYPE (decl))
2296 != type_memfn_rqual (TREE_TYPE (fn))))
2297 continue;
2298
2299 // If the deduced arguments do not satisfy the constraints,
2300 // this is not a candidate.
2301 if (flag_concepts && !constraints_satisfied_p (fn))
2302 continue;
2303
2304 // Add the candidate.
2305 candidates = tree_cons (NULL_TREE, fn, candidates);
2306 }
2307 }
2308
2309 if (templates && TREE_CHAIN (templates))
2310 {
2311 /* We have:
2312
2313 [temp.expl.spec]
2314
2315 It is possible for a specialization with a given function
2316 signature to be instantiated from more than one function
2317 template. In such cases, explicit specification of the
2318 template arguments must be used to uniquely identify the
2319 function template specialization being specialized.
2320
2321 Note that here, there's no suggestion that we're supposed to
2322 determine which of the candidate templates is most
2323 specialized. However, we, also have:
2324
2325 [temp.func.order]
2326
2327 Partial ordering of overloaded function template
2328 declarations is used in the following contexts to select
2329 the function template to which a function template
2330 specialization refers:
2331
2332 -- when an explicit specialization refers to a function
2333 template.
2334
2335 So, we do use the partial ordering rules, at least for now.
2336 This extension can only serve to make invalid programs valid,
2337 so it's safe. And, there is strong anecdotal evidence that
2338 the committee intended the partial ordering rules to apply;
2339 the EDG front end has that behavior, and John Spicer claims
2340 that the committee simply forgot to delete the wording in
2341 [temp.expl.spec]. */
2342 tree tmpl = most_specialized_instantiation (templates);
2343 if (tmpl != error_mark_node)
2344 {
2345 templates = tmpl;
2346 TREE_CHAIN (templates) = NULL_TREE;
2347 }
2348 }
2349
2350 // Concepts allows multiple declarations of member functions
2351 // with the same signature. Like above, we need to rely on
2352 // on the partial ordering of those candidates to determine which
2353 // is the best.
2354 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2355 {
2356 if (tree cand = most_constrained_function (candidates))
2357 {
2358 candidates = cand;
2359 TREE_CHAIN (cand) = NULL_TREE;
2360 }
2361 }
2362
2363 if (templates == NULL_TREE && candidates == NULL_TREE)
2364 {
2365 error ("template-id %qD for %q+D does not match any template "
2366 "declaration", template_id, decl);
2367 if (header_count && header_count != template_count + 1)
2368 inform (input_location, "saw %d %<template<>%>, need %d for "
2369 "specializing a member function template",
2370 header_count, template_count + 1);
2371 else
2372 print_candidates (orig_fns);
2373 return error_mark_node;
2374 }
2375 else if ((templates && TREE_CHAIN (templates))
2376 || (candidates && TREE_CHAIN (candidates))
2377 || (templates && candidates))
2378 {
2379 error ("ambiguous template specialization %qD for %q+D",
2380 template_id, decl);
2381 candidates = chainon (candidates, templates);
2382 print_candidates (candidates);
2383 return error_mark_node;
2384 }
2385
2386 /* We have one, and exactly one, match. */
2387 if (candidates)
2388 {
2389 tree fn = TREE_VALUE (candidates);
2390 *targs_out = copy_node (DECL_TI_ARGS (fn));
2391
2392 // Propagate the candidate's constraints to the declaration.
2393 set_constraints (decl, get_constraints (fn));
2394
2395 /* DECL is a re-declaration or partial instantiation of a template
2396 function. */
2397 if (TREE_CODE (fn) == TEMPLATE_DECL)
2398 return fn;
2399 /* It was a specialization of an ordinary member function in a
2400 template class. */
2401 return DECL_TI_TEMPLATE (fn);
2402 }
2403
2404 /* It was a specialization of a template. */
2405 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2406 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2407 {
2408 *targs_out = copy_node (targs);
2409 SET_TMPL_ARGS_LEVEL (*targs_out,
2410 TMPL_ARGS_DEPTH (*targs_out),
2411 TREE_PURPOSE (templates));
2412 }
2413 else
2414 *targs_out = TREE_PURPOSE (templates);
2415 return TREE_VALUE (templates);
2416 }
2417
2418 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2419 but with the default argument values filled in from those in the
2420 TMPL_TYPES. */
2421
2422 static tree
2423 copy_default_args_to_explicit_spec_1 (tree spec_types,
2424 tree tmpl_types)
2425 {
2426 tree new_spec_types;
2427
2428 if (!spec_types)
2429 return NULL_TREE;
2430
2431 if (spec_types == void_list_node)
2432 return void_list_node;
2433
2434 /* Substitute into the rest of the list. */
2435 new_spec_types =
2436 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2437 TREE_CHAIN (tmpl_types));
2438
2439 /* Add the default argument for this parameter. */
2440 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2441 TREE_VALUE (spec_types),
2442 new_spec_types);
2443 }
2444
2445 /* DECL is an explicit specialization. Replicate default arguments
2446 from the template it specializes. (That way, code like:
2447
2448 template <class T> void f(T = 3);
2449 template <> void f(double);
2450 void g () { f (); }
2451
2452 works, as required.) An alternative approach would be to look up
2453 the correct default arguments at the call-site, but this approach
2454 is consistent with how implicit instantiations are handled. */
2455
2456 static void
2457 copy_default_args_to_explicit_spec (tree decl)
2458 {
2459 tree tmpl;
2460 tree spec_types;
2461 tree tmpl_types;
2462 tree new_spec_types;
2463 tree old_type;
2464 tree new_type;
2465 tree t;
2466 tree object_type = NULL_TREE;
2467 tree in_charge = NULL_TREE;
2468 tree vtt = NULL_TREE;
2469
2470 /* See if there's anything we need to do. */
2471 tmpl = DECL_TI_TEMPLATE (decl);
2472 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2473 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2474 if (TREE_PURPOSE (t))
2475 break;
2476 if (!t)
2477 return;
2478
2479 old_type = TREE_TYPE (decl);
2480 spec_types = TYPE_ARG_TYPES (old_type);
2481
2482 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2483 {
2484 /* Remove the this pointer, but remember the object's type for
2485 CV quals. */
2486 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2487 spec_types = TREE_CHAIN (spec_types);
2488 tmpl_types = TREE_CHAIN (tmpl_types);
2489
2490 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2491 {
2492 /* DECL may contain more parameters than TMPL due to the extra
2493 in-charge parameter in constructors and destructors. */
2494 in_charge = spec_types;
2495 spec_types = TREE_CHAIN (spec_types);
2496 }
2497 if (DECL_HAS_VTT_PARM_P (decl))
2498 {
2499 vtt = spec_types;
2500 spec_types = TREE_CHAIN (spec_types);
2501 }
2502 }
2503
2504 /* Compute the merged default arguments. */
2505 new_spec_types =
2506 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2507
2508 /* Compute the new FUNCTION_TYPE. */
2509 if (object_type)
2510 {
2511 if (vtt)
2512 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2513 TREE_VALUE (vtt),
2514 new_spec_types);
2515
2516 if (in_charge)
2517 /* Put the in-charge parameter back. */
2518 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2519 TREE_VALUE (in_charge),
2520 new_spec_types);
2521
2522 new_type = build_method_type_directly (object_type,
2523 TREE_TYPE (old_type),
2524 new_spec_types);
2525 }
2526 else
2527 new_type = build_function_type (TREE_TYPE (old_type),
2528 new_spec_types);
2529 new_type = cp_build_type_attribute_variant (new_type,
2530 TYPE_ATTRIBUTES (old_type));
2531 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2532
2533 TREE_TYPE (decl) = new_type;
2534 }
2535
2536 /* Return the number of template headers we expect to see for a definition
2537 or specialization of CTYPE or one of its non-template members. */
2538
2539 int
2540 num_template_headers_for_class (tree ctype)
2541 {
2542 int num_templates = 0;
2543
2544 while (ctype && CLASS_TYPE_P (ctype))
2545 {
2546 /* You're supposed to have one `template <...>' for every
2547 template class, but you don't need one for a full
2548 specialization. For example:
2549
2550 template <class T> struct S{};
2551 template <> struct S<int> { void f(); };
2552 void S<int>::f () {}
2553
2554 is correct; there shouldn't be a `template <>' for the
2555 definition of `S<int>::f'. */
2556 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2557 /* If CTYPE does not have template information of any
2558 kind, then it is not a template, nor is it nested
2559 within a template. */
2560 break;
2561 if (explicit_class_specialization_p (ctype))
2562 break;
2563 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2564 ++num_templates;
2565
2566 ctype = TYPE_CONTEXT (ctype);
2567 }
2568
2569 return num_templates;
2570 }
2571
2572 /* Do a simple sanity check on the template headers that precede the
2573 variable declaration DECL. */
2574
2575 void
2576 check_template_variable (tree decl)
2577 {
2578 tree ctx = CP_DECL_CONTEXT (decl);
2579 int wanted = num_template_headers_for_class (ctx);
2580 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2581 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2582 {
2583 if (cxx_dialect < cxx14)
2584 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2585 "variable templates only available with "
2586 "-std=c++14 or -std=gnu++14");
2587
2588 // Namespace-scope variable templates should have a template header.
2589 ++wanted;
2590 }
2591 if (template_header_count > wanted)
2592 {
2593 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2594 "too many template headers for %qD "
2595 "(should be %d)",
2596 decl, wanted);
2597 if (warned && CLASS_TYPE_P (ctx)
2598 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2599 inform (DECL_SOURCE_LOCATION (decl),
2600 "members of an explicitly specialized class are defined "
2601 "without a template header");
2602 }
2603 }
2604
2605 /* An explicit specialization whose declarator-id or class-head-name is not
2606 qualified shall be declared in the nearest enclosing namespace of the
2607 template, or, if the namespace is inline (7.3.1), any namespace from its
2608 enclosing namespace set.
2609
2610 If the name declared in the explicit instantiation is an unqualified name,
2611 the explicit instantiation shall appear in the namespace where its template
2612 is declared or, if that namespace is inline (7.3.1), any namespace from its
2613 enclosing namespace set. */
2614
2615 void
2616 check_unqualified_spec_or_inst (tree t, location_t loc)
2617 {
2618 tree tmpl = most_general_template (t);
2619 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2620 && !is_nested_namespace (current_namespace,
2621 CP_DECL_CONTEXT (tmpl), true))
2622 {
2623 if (processing_specialization)
2624 permerror (loc, "explicit specialization of %qD outside its "
2625 "namespace must use a nested-name-specifier", tmpl);
2626 else if (processing_explicit_instantiation
2627 && cxx_dialect >= cxx11)
2628 /* This was allowed in C++98, so only pedwarn. */
2629 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2630 "outside its namespace must use a nested-name-"
2631 "specifier", tmpl);
2632 }
2633 }
2634
2635 /* Warn for a template specialization SPEC that is missing some of a set
2636 of function or type attributes that the template TEMPL is declared with.
2637 ATTRLIST is a list of additional attributes that SPEC should be taken
2638 to ultimately be declared with. */
2639
2640 static void
2641 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2642 {
2643 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2644 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2645
2646 if (TREE_CODE (tmpl) != FUNCTION_DECL)
2647 return;
2648
2649 /* Avoid warning if either declaration or its type is deprecated. */
2650 if (TREE_DEPRECATED (tmpl)
2651 || TREE_DEPRECATED (spec))
2652 return;
2653
2654 tree tmpl_type = TREE_TYPE (tmpl);
2655 tree spec_type = TREE_TYPE (spec);
2656
2657 if (TREE_DEPRECATED (tmpl_type)
2658 || TREE_DEPRECATED (spec_type)
2659 || TREE_DEPRECATED (TREE_TYPE (tmpl_type))
2660 || TREE_DEPRECATED (TREE_TYPE (spec_type)))
2661 return;
2662
2663 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpl_type) };
2664 tree spec_attrs[] = { DECL_ATTRIBUTES (spec), TYPE_ATTRIBUTES (spec_type) };
2665
2666 if (!spec_attrs[0])
2667 spec_attrs[0] = attrlist;
2668 else if (!spec_attrs[1])
2669 spec_attrs[1] = attrlist;
2670
2671 /* Avoid warning if the primary has no attributes. */
2672 if (!tmpl_attrs[0] && !tmpl_attrs[1])
2673 return;
2674
2675 /* Avoid warning if either declaration contains an attribute on
2676 the white list below. */
2677 const char* const whitelist[] = {
2678 "error", "warning"
2679 };
2680
2681 for (unsigned i = 0; i != 2; ++i)
2682 for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j)
2683 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
2684 || lookup_attribute (whitelist[j], spec_attrs[i]))
2685 return;
2686
2687 /* Avoid warning if the difference between the primary and
2688 the specialization is not in one of the attributes below. */
2689 const char* const blacklist[] = {
2690 "alloc_align", "alloc_size", "assume_aligned", "format",
2691 "format_arg", "malloc", "nonnull"
2692 };
2693
2694 /* Put together a list of the black listed attributes that the primary
2695 template is declared with that the specialization is not, in case
2696 it's not apparent from the most recent declaration of the primary. */
2697 unsigned nattrs = 0;
2698 pretty_printer str;
2699
2700 for (unsigned i = 0; i != sizeof blacklist / sizeof *blacklist; ++i)
2701 {
2702 for (unsigned j = 0; j != 2; ++j)
2703 {
2704 if (!lookup_attribute (blacklist[i], tmpl_attrs[j]))
2705 continue;
2706
2707 for (unsigned k = 0; k != 1 + !!spec_attrs[1]; ++k)
2708 {
2709 if (lookup_attribute (blacklist[i], spec_attrs[k]))
2710 break;
2711
2712 if (nattrs)
2713 pp_string (&str, ", ");
2714 pp_begin_quote (&str, pp_show_color (global_dc->printer));
2715 pp_string (&str, blacklist[i]);
2716 pp_end_quote (&str, pp_show_color (global_dc->printer));
2717 ++nattrs;
2718 }
2719 }
2720 }
2721
2722 if (!nattrs)
2723 return;
2724
2725 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2726 "explicit specialization %q#D may be missing attributes",
2727 spec))
2728 inform (DECL_SOURCE_LOCATION (tmpl),
2729 nattrs > 1
2730 ? G_("missing primary template attributes %s")
2731 : G_("missing primary template attribute %s"),
2732 pp_formatted_text (&str));
2733 }
2734
2735 /* Check to see if the function just declared, as indicated in
2736 DECLARATOR, and in DECL, is a specialization of a function
2737 template. We may also discover that the declaration is an explicit
2738 instantiation at this point.
2739
2740 Returns DECL, or an equivalent declaration that should be used
2741 instead if all goes well. Issues an error message if something is
2742 amiss. Returns error_mark_node if the error is not easily
2743 recoverable.
2744
2745 FLAGS is a bitmask consisting of the following flags:
2746
2747 2: The function has a definition.
2748 4: The function is a friend.
2749
2750 The TEMPLATE_COUNT is the number of references to qualifying
2751 template classes that appeared in the name of the function. For
2752 example, in
2753
2754 template <class T> struct S { void f(); };
2755 void S<int>::f();
2756
2757 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2758 classes are not counted in the TEMPLATE_COUNT, so that in
2759
2760 template <class T> struct S {};
2761 template <> struct S<int> { void f(); }
2762 template <> void S<int>::f();
2763
2764 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2765 invalid; there should be no template <>.)
2766
2767 If the function is a specialization, it is marked as such via
2768 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2769 is set up correctly, and it is added to the list of specializations
2770 for that template. */
2771
2772 tree
2773 check_explicit_specialization (tree declarator,
2774 tree decl,
2775 int template_count,
2776 int flags,
2777 tree attrlist)
2778 {
2779 int have_def = flags & 2;
2780 int is_friend = flags & 4;
2781 bool is_concept = flags & 8;
2782 int specialization = 0;
2783 int explicit_instantiation = 0;
2784 int member_specialization = 0;
2785 tree ctype = DECL_CLASS_CONTEXT (decl);
2786 tree dname = DECL_NAME (decl);
2787 tmpl_spec_kind tsk;
2788
2789 if (is_friend)
2790 {
2791 if (!processing_specialization)
2792 tsk = tsk_none;
2793 else
2794 tsk = tsk_excessive_parms;
2795 }
2796 else
2797 tsk = current_tmpl_spec_kind (template_count);
2798
2799 switch (tsk)
2800 {
2801 case tsk_none:
2802 if (processing_specialization && !VAR_P (decl))
2803 {
2804 specialization = 1;
2805 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2806 }
2807 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2808 {
2809 if (is_friend)
2810 /* This could be something like:
2811
2812 template <class T> void f(T);
2813 class S { friend void f<>(int); } */
2814 specialization = 1;
2815 else
2816 {
2817 /* This case handles bogus declarations like template <>
2818 template <class T> void f<int>(); */
2819
2820 error ("template-id %qD in declaration of primary template",
2821 declarator);
2822 return decl;
2823 }
2824 }
2825 break;
2826
2827 case tsk_invalid_member_spec:
2828 /* The error has already been reported in
2829 check_specialization_scope. */
2830 return error_mark_node;
2831
2832 case tsk_invalid_expl_inst:
2833 error ("template parameter list used in explicit instantiation");
2834
2835 /* Fall through. */
2836
2837 case tsk_expl_inst:
2838 if (have_def)
2839 error ("definition provided for explicit instantiation");
2840
2841 explicit_instantiation = 1;
2842 break;
2843
2844 case tsk_excessive_parms:
2845 case tsk_insufficient_parms:
2846 if (tsk == tsk_excessive_parms)
2847 error ("too many template parameter lists in declaration of %qD",
2848 decl);
2849 else if (template_header_count)
2850 error("too few template parameter lists in declaration of %qD", decl);
2851 else
2852 error("explicit specialization of %qD must be introduced by "
2853 "%<template <>%>", decl);
2854
2855 /* Fall through. */
2856 case tsk_expl_spec:
2857 if (is_concept)
2858 error ("explicit specialization declared %<concept%>");
2859
2860 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2861 /* In cases like template<> constexpr bool v = true;
2862 We'll give an error in check_template_variable. */
2863 break;
2864
2865 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2866 if (ctype)
2867 member_specialization = 1;
2868 else
2869 specialization = 1;
2870 break;
2871
2872 case tsk_template:
2873 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2874 {
2875 /* This case handles bogus declarations like template <>
2876 template <class T> void f<int>(); */
2877
2878 if (!uses_template_parms (declarator))
2879 error ("template-id %qD in declaration of primary template",
2880 declarator);
2881 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2882 {
2883 /* Partial specialization of variable template. */
2884 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2885 specialization = 1;
2886 goto ok;
2887 }
2888 else if (cxx_dialect < cxx14)
2889 error ("non-type partial specialization %qD "
2890 "is not allowed", declarator);
2891 else
2892 error ("non-class, non-variable partial specialization %qD "
2893 "is not allowed", declarator);
2894 return decl;
2895 ok:;
2896 }
2897
2898 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2899 /* This is a specialization of a member template, without
2900 specialization the containing class. Something like:
2901
2902 template <class T> struct S {
2903 template <class U> void f (U);
2904 };
2905 template <> template <class U> void S<int>::f(U) {}
2906
2907 That's a specialization -- but of the entire template. */
2908 specialization = 1;
2909 break;
2910
2911 default:
2912 gcc_unreachable ();
2913 }
2914
2915 if ((specialization || member_specialization)
2916 /* This doesn't apply to variable templates. */
2917 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2918 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2919 {
2920 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2921 for (; t; t = TREE_CHAIN (t))
2922 if (TREE_PURPOSE (t))
2923 {
2924 permerror (input_location,
2925 "default argument specified in explicit specialization");
2926 break;
2927 }
2928 }
2929
2930 if (specialization || member_specialization || explicit_instantiation)
2931 {
2932 tree tmpl = NULL_TREE;
2933 tree targs = NULL_TREE;
2934 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2935
2936 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2937 if (!was_template_id)
2938 {
2939 tree fns;
2940
2941 gcc_assert (identifier_p (declarator));
2942 if (ctype)
2943 fns = dname;
2944 else
2945 {
2946 /* If there is no class context, the explicit instantiation
2947 must be at namespace scope. */
2948 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2949
2950 /* Find the namespace binding, using the declaration
2951 context. */
2952 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2953 false, true);
2954 if (fns == error_mark_node)
2955 /* If lookup fails, look for a friend declaration so we can
2956 give a better diagnostic. */
2957 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2958 /*type*/false, /*complain*/true,
2959 /*hidden*/true);
2960
2961 if (fns == error_mark_node || !is_overloaded_fn (fns))
2962 {
2963 error ("%qD is not a template function", dname);
2964 fns = error_mark_node;
2965 }
2966 }
2967
2968 declarator = lookup_template_function (fns, NULL_TREE);
2969 }
2970
2971 if (declarator == error_mark_node)
2972 return error_mark_node;
2973
2974 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2975 {
2976 if (!explicit_instantiation)
2977 /* A specialization in class scope. This is invalid,
2978 but the error will already have been flagged by
2979 check_specialization_scope. */
2980 return error_mark_node;
2981 else
2982 {
2983 /* It's not valid to write an explicit instantiation in
2984 class scope, e.g.:
2985
2986 class C { template void f(); }
2987
2988 This case is caught by the parser. However, on
2989 something like:
2990
2991 template class C { void f(); };
2992
2993 (which is invalid) we can get here. The error will be
2994 issued later. */
2995 ;
2996 }
2997
2998 return decl;
2999 }
3000 else if (ctype != NULL_TREE
3001 && (identifier_p (TREE_OPERAND (declarator, 0))))
3002 {
3003 // We'll match variable templates in start_decl.
3004 if (VAR_P (decl))
3005 return decl;
3006
3007 /* Find the list of functions in ctype that have the same
3008 name as the declared function. */
3009 tree name = TREE_OPERAND (declarator, 0);
3010
3011 if (constructor_name_p (name, ctype))
3012 {
3013 if (DECL_CONSTRUCTOR_P (decl)
3014 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3015 : !CLASSTYPE_DESTRUCTOR (ctype))
3016 {
3017 /* From [temp.expl.spec]:
3018
3019 If such an explicit specialization for the member
3020 of a class template names an implicitly-declared
3021 special member function (clause _special_), the
3022 program is ill-formed.
3023
3024 Similar language is found in [temp.explicit]. */
3025 error ("specialization of implicitly-declared special member function");
3026 return error_mark_node;
3027 }
3028
3029 name = DECL_NAME (decl);
3030 }
3031
3032 /* For a type-conversion operator, We might be looking for
3033 `operator int' which will be a specialization of
3034 `operator T'. Grab all the conversion operators, and
3035 then select from them. */
3036 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3037 ? conv_op_identifier : name);
3038
3039 if (fns == NULL_TREE)
3040 {
3041 error ("no member function %qD declared in %qT", name, ctype);
3042 return error_mark_node;
3043 }
3044 else
3045 TREE_OPERAND (declarator, 0) = fns;
3046 }
3047
3048 /* Figure out what exactly is being specialized at this point.
3049 Note that for an explicit instantiation, even one for a
3050 member function, we cannot tell a priori whether the
3051 instantiation is for a member template, or just a member
3052 function of a template class. Even if a member template is
3053 being instantiated, the member template arguments may be
3054 elided if they can be deduced from the rest of the
3055 declaration. */
3056 tmpl = determine_specialization (declarator, decl,
3057 &targs,
3058 member_specialization,
3059 template_count,
3060 tsk);
3061
3062 if (!tmpl || tmpl == error_mark_node)
3063 /* We couldn't figure out what this declaration was
3064 specializing. */
3065 return error_mark_node;
3066 else
3067 {
3068 if (TREE_CODE (decl) == FUNCTION_DECL
3069 && DECL_HIDDEN_FRIEND_P (tmpl))
3070 {
3071 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3072 "friend declaration %qD is not visible to "
3073 "explicit specialization", tmpl))
3074 inform (DECL_SOURCE_LOCATION (tmpl),
3075 "friend declaration here");
3076 }
3077 else if (!ctype && !is_friend
3078 && CP_DECL_CONTEXT (decl) == current_namespace)
3079 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3080
3081 tree gen_tmpl = most_general_template (tmpl);
3082
3083 if (explicit_instantiation)
3084 {
3085 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3086 is done by do_decl_instantiation later. */
3087
3088 int arg_depth = TMPL_ARGS_DEPTH (targs);
3089 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3090
3091 if (arg_depth > parm_depth)
3092 {
3093 /* If TMPL is not the most general template (for
3094 example, if TMPL is a friend template that is
3095 injected into namespace scope), then there will
3096 be too many levels of TARGS. Remove some of them
3097 here. */
3098 int i;
3099 tree new_targs;
3100
3101 new_targs = make_tree_vec (parm_depth);
3102 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3103 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3104 = TREE_VEC_ELT (targs, i);
3105 targs = new_targs;
3106 }
3107
3108 return instantiate_template (tmpl, targs, tf_error);
3109 }
3110
3111 /* If we thought that the DECL was a member function, but it
3112 turns out to be specializing a static member function,
3113 make DECL a static member function as well. */
3114 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3115 && DECL_STATIC_FUNCTION_P (tmpl)
3116 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3117 revert_static_member_fn (decl);
3118
3119 /* If this is a specialization of a member template of a
3120 template class, we want to return the TEMPLATE_DECL, not
3121 the specialization of it. */
3122 if (tsk == tsk_template && !was_template_id)
3123 {
3124 tree result = DECL_TEMPLATE_RESULT (tmpl);
3125 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3126 DECL_INITIAL (result) = NULL_TREE;
3127 if (have_def)
3128 {
3129 tree parm;
3130 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3131 DECL_SOURCE_LOCATION (result)
3132 = DECL_SOURCE_LOCATION (decl);
3133 /* We want to use the argument list specified in the
3134 definition, not in the original declaration. */
3135 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3136 for (parm = DECL_ARGUMENTS (result); parm;
3137 parm = DECL_CHAIN (parm))
3138 DECL_CONTEXT (parm) = result;
3139 }
3140 return register_specialization (tmpl, gen_tmpl, targs,
3141 is_friend, 0);
3142 }
3143
3144 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3145 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3146
3147 if (was_template_id)
3148 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3149
3150 /* Inherit default function arguments from the template
3151 DECL is specializing. */
3152 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3153 copy_default_args_to_explicit_spec (decl);
3154
3155 /* This specialization has the same protection as the
3156 template it specializes. */
3157 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3158 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3159
3160 /* 7.1.1-1 [dcl.stc]
3161
3162 A storage-class-specifier shall not be specified in an
3163 explicit specialization...
3164
3165 The parser rejects these, so unless action is taken here,
3166 explicit function specializations will always appear with
3167 global linkage.
3168
3169 The action recommended by the C++ CWG in response to C++
3170 defect report 605 is to make the storage class and linkage
3171 of the explicit specialization match the templated function:
3172
3173 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3174 */
3175 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3176 {
3177 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3178 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3179
3180 /* A concept cannot be specialized. */
3181 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3182 {
3183 error ("explicit specialization of function concept %qD",
3184 gen_tmpl);
3185 return error_mark_node;
3186 }
3187
3188 /* This specialization has the same linkage and visibility as
3189 the function template it specializes. */
3190 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3191 if (! TREE_PUBLIC (decl))
3192 {
3193 DECL_INTERFACE_KNOWN (decl) = 1;
3194 DECL_NOT_REALLY_EXTERN (decl) = 1;
3195 }
3196 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3197 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3198 {
3199 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3200 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3201 }
3202 }
3203
3204 /* If DECL is a friend declaration, declared using an
3205 unqualified name, the namespace associated with DECL may
3206 have been set incorrectly. For example, in:
3207
3208 template <typename T> void f(T);
3209 namespace N {
3210 struct S { friend void f<int>(int); }
3211 }
3212
3213 we will have set the DECL_CONTEXT for the friend
3214 declaration to N, rather than to the global namespace. */
3215 if (DECL_NAMESPACE_SCOPE_P (decl))
3216 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3217
3218 if (is_friend && !have_def)
3219 /* This is not really a declaration of a specialization.
3220 It's just the name of an instantiation. But, it's not
3221 a request for an instantiation, either. */
3222 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3223 else if (TREE_CODE (decl) == FUNCTION_DECL)
3224 /* A specialization is not necessarily COMDAT. */
3225 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3226 && DECL_DECLARED_INLINE_P (decl));
3227 else if (VAR_P (decl))
3228 DECL_COMDAT (decl) = false;
3229
3230 /* If this is a full specialization, register it so that we can find
3231 it again. Partial specializations will be registered in
3232 process_partial_specialization. */
3233 if (!processing_template_decl)
3234 {
3235 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3236
3237 decl = register_specialization (decl, gen_tmpl, targs,
3238 is_friend, 0);
3239 }
3240
3241
3242 /* A 'structor should already have clones. */
3243 gcc_assert (decl == error_mark_node
3244 || variable_template_p (tmpl)
3245 || !(DECL_CONSTRUCTOR_P (decl)
3246 || DECL_DESTRUCTOR_P (decl))
3247 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3248 }
3249 }
3250
3251 return decl;
3252 }
3253
3254 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3255 parameters. These are represented in the same format used for
3256 DECL_TEMPLATE_PARMS. */
3257
3258 int
3259 comp_template_parms (const_tree parms1, const_tree parms2)
3260 {
3261 const_tree p1;
3262 const_tree p2;
3263
3264 if (parms1 == parms2)
3265 return 1;
3266
3267 for (p1 = parms1, p2 = parms2;
3268 p1 != NULL_TREE && p2 != NULL_TREE;
3269 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3270 {
3271 tree t1 = TREE_VALUE (p1);
3272 tree t2 = TREE_VALUE (p2);
3273 int i;
3274
3275 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3276 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3277
3278 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3279 return 0;
3280
3281 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3282 {
3283 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3284 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3285
3286 /* If either of the template parameters are invalid, assume
3287 they match for the sake of error recovery. */
3288 if (error_operand_p (parm1) || error_operand_p (parm2))
3289 return 1;
3290
3291 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3292 return 0;
3293
3294 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3295 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3296 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3297 continue;
3298 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3299 return 0;
3300 }
3301 }
3302
3303 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3304 /* One set of parameters has more parameters lists than the
3305 other. */
3306 return 0;
3307
3308 return 1;
3309 }
3310
3311 /* Determine whether PARM is a parameter pack. */
3312
3313 bool
3314 template_parameter_pack_p (const_tree parm)
3315 {
3316 /* Determine if we have a non-type template parameter pack. */
3317 if (TREE_CODE (parm) == PARM_DECL)
3318 return (DECL_TEMPLATE_PARM_P (parm)
3319 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3320 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3321 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3322
3323 /* If this is a list of template parameters, we could get a
3324 TYPE_DECL or a TEMPLATE_DECL. */
3325 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3326 parm = TREE_TYPE (parm);
3327
3328 /* Otherwise it must be a type template parameter. */
3329 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3330 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3331 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3332 }
3333
3334 /* Determine if T is a function parameter pack. */
3335
3336 bool
3337 function_parameter_pack_p (const_tree t)
3338 {
3339 if (t && TREE_CODE (t) == PARM_DECL)
3340 return DECL_PACK_P (t);
3341 return false;
3342 }
3343
3344 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3345 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3346
3347 tree
3348 get_function_template_decl (const_tree primary_func_tmpl_inst)
3349 {
3350 if (! primary_func_tmpl_inst
3351 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3352 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3353 return NULL;
3354
3355 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3356 }
3357
3358 /* Return true iff the function parameter PARAM_DECL was expanded
3359 from the function parameter pack PACK. */
3360
3361 bool
3362 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3363 {
3364 if (DECL_ARTIFICIAL (param_decl)
3365 || !function_parameter_pack_p (pack))
3366 return false;
3367
3368 /* The parameter pack and its pack arguments have the same
3369 DECL_PARM_INDEX. */
3370 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3371 }
3372
3373 /* Determine whether ARGS describes a variadic template args list,
3374 i.e., one that is terminated by a template argument pack. */
3375
3376 static bool
3377 template_args_variadic_p (tree args)
3378 {
3379 int nargs;
3380 tree last_parm;
3381
3382 if (args == NULL_TREE)
3383 return false;
3384
3385 args = INNERMOST_TEMPLATE_ARGS (args);
3386 nargs = TREE_VEC_LENGTH (args);
3387
3388 if (nargs == 0)
3389 return false;
3390
3391 last_parm = TREE_VEC_ELT (args, nargs - 1);
3392
3393 return ARGUMENT_PACK_P (last_parm);
3394 }
3395
3396 /* Generate a new name for the parameter pack name NAME (an
3397 IDENTIFIER_NODE) that incorporates its */
3398
3399 static tree
3400 make_ith_pack_parameter_name (tree name, int i)
3401 {
3402 /* Munge the name to include the parameter index. */
3403 #define NUMBUF_LEN 128
3404 char numbuf[NUMBUF_LEN];
3405 char* newname;
3406 int newname_len;
3407
3408 if (name == NULL_TREE)
3409 return name;
3410 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3411 newname_len = IDENTIFIER_LENGTH (name)
3412 + strlen (numbuf) + 2;
3413 newname = (char*)alloca (newname_len);
3414 snprintf (newname, newname_len,
3415 "%s#%i", IDENTIFIER_POINTER (name), i);
3416 return get_identifier (newname);
3417 }
3418
3419 /* Return true if T is a primary function, class or alias template
3420 specialization, not including the template pattern. */
3421
3422 bool
3423 primary_template_specialization_p (const_tree t)
3424 {
3425 if (!t)
3426 return false;
3427
3428 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3429 return (DECL_LANG_SPECIFIC (t)
3430 && DECL_USE_TEMPLATE (t)
3431 && DECL_TEMPLATE_INFO (t)
3432 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3433 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3434 return (CLASSTYPE_TEMPLATE_INFO (t)
3435 && CLASSTYPE_USE_TEMPLATE (t)
3436 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3437 else if (alias_template_specialization_p (t))
3438 return true;
3439 return false;
3440 }
3441
3442 /* Return true if PARM is a template template parameter. */
3443
3444 bool
3445 template_template_parameter_p (const_tree parm)
3446 {
3447 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3448 }
3449
3450 /* Return true iff PARM is a DECL representing a type template
3451 parameter. */
3452
3453 bool
3454 template_type_parameter_p (const_tree parm)
3455 {
3456 return (parm
3457 && (TREE_CODE (parm) == TYPE_DECL
3458 || TREE_CODE (parm) == TEMPLATE_DECL)
3459 && DECL_TEMPLATE_PARM_P (parm));
3460 }
3461
3462 /* Return the template parameters of T if T is a
3463 primary template instantiation, NULL otherwise. */
3464
3465 tree
3466 get_primary_template_innermost_parameters (const_tree t)
3467 {
3468 tree parms = NULL, template_info = NULL;
3469
3470 if ((template_info = get_template_info (t))
3471 && primary_template_specialization_p (t))
3472 parms = INNERMOST_TEMPLATE_PARMS
3473 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3474
3475 return parms;
3476 }
3477
3478 /* Return the template parameters of the LEVELth level from the full list
3479 of template parameters PARMS. */
3480
3481 tree
3482 get_template_parms_at_level (tree parms, int level)
3483 {
3484 tree p;
3485 if (!parms
3486 || TREE_CODE (parms) != TREE_LIST
3487 || level > TMPL_PARMS_DEPTH (parms))
3488 return NULL_TREE;
3489
3490 for (p = parms; p; p = TREE_CHAIN (p))
3491 if (TMPL_PARMS_DEPTH (p) == level)
3492 return p;
3493
3494 return NULL_TREE;
3495 }
3496
3497 /* Returns the template arguments of T if T is a template instantiation,
3498 NULL otherwise. */
3499
3500 tree
3501 get_template_innermost_arguments (const_tree t)
3502 {
3503 tree args = NULL, template_info = NULL;
3504
3505 if ((template_info = get_template_info (t))
3506 && TI_ARGS (template_info))
3507 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3508
3509 return args;
3510 }
3511
3512 /* Return the argument pack elements of T if T is a template argument pack,
3513 NULL otherwise. */
3514
3515 tree
3516 get_template_argument_pack_elems (const_tree t)
3517 {
3518 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3519 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3520 return NULL;
3521
3522 return ARGUMENT_PACK_ARGS (t);
3523 }
3524
3525 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3526 ARGUMENT_PACK_SELECT represents. */
3527
3528 static tree
3529 argument_pack_select_arg (tree t)
3530 {
3531 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3532 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3533
3534 /* If the selected argument is an expansion E, that most likely means we were
3535 called from gen_elem_of_pack_expansion_instantiation during the
3536 substituting of an argument pack (of which the Ith element is a pack
3537 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3538 In this case, the Ith element resulting from this substituting is going to
3539 be a pack expansion, which pattern is the pattern of E. Let's return the
3540 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3541 resulting pack expansion from it. */
3542 if (PACK_EXPANSION_P (arg))
3543 {
3544 /* Make sure we aren't throwing away arg info. */
3545 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3546 arg = PACK_EXPANSION_PATTERN (arg);
3547 }
3548
3549 return arg;
3550 }
3551
3552
3553 /* True iff FN is a function representing a built-in variadic parameter
3554 pack. */
3555
3556 bool
3557 builtin_pack_fn_p (tree fn)
3558 {
3559 if (!fn
3560 || TREE_CODE (fn) != FUNCTION_DECL
3561 || !DECL_IS_BUILTIN (fn))
3562 return false;
3563
3564 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3565 return true;
3566
3567 return false;
3568 }
3569
3570 /* True iff CALL is a call to a function representing a built-in variadic
3571 parameter pack. */
3572
3573 static bool
3574 builtin_pack_call_p (tree call)
3575 {
3576 if (TREE_CODE (call) != CALL_EXPR)
3577 return false;
3578 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3579 }
3580
3581 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3582
3583 static tree
3584 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3585 tree in_decl)
3586 {
3587 tree ohi = CALL_EXPR_ARG (call, 0);
3588 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3589 false/*fn*/, true/*int_cst*/);
3590
3591 if (value_dependent_expression_p (hi))
3592 {
3593 if (hi != ohi)
3594 {
3595 call = copy_node (call);
3596 CALL_EXPR_ARG (call, 0) = hi;
3597 }
3598 tree ex = make_pack_expansion (call, complain);
3599 tree vec = make_tree_vec (1);
3600 TREE_VEC_ELT (vec, 0) = ex;
3601 return vec;
3602 }
3603 else
3604 {
3605 hi = cxx_constant_value (hi);
3606 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3607
3608 /* Calculate the largest value of len that won't make the size of the vec
3609 overflow an int. The compiler will exceed resource limits long before
3610 this, but it seems a decent place to diagnose. */
3611 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3612
3613 if (len < 0 || len > max)
3614 {
3615 if ((complain & tf_error)
3616 && hi != error_mark_node)
3617 error ("argument to __integer_pack must be between 0 and %d", max);
3618 return error_mark_node;
3619 }
3620
3621 tree vec = make_tree_vec (len);
3622
3623 for (int i = 0; i < len; ++i)
3624 TREE_VEC_ELT (vec, i) = size_int (i);
3625
3626 return vec;
3627 }
3628 }
3629
3630 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3631 CALL. */
3632
3633 static tree
3634 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3635 tree in_decl)
3636 {
3637 if (!builtin_pack_call_p (call))
3638 return NULL_TREE;
3639
3640 tree fn = CALL_EXPR_FN (call);
3641
3642 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3643 return expand_integer_pack (call, args, complain, in_decl);
3644
3645 return NULL_TREE;
3646 }
3647
3648 /* Structure used to track the progress of find_parameter_packs_r. */
3649 struct find_parameter_pack_data
3650 {
3651 /* TREE_LIST that will contain all of the parameter packs found by
3652 the traversal. */
3653 tree* parameter_packs;
3654
3655 /* Set of AST nodes that have been visited by the traversal. */
3656 hash_set<tree> *visited;
3657
3658 /* True iff we're making a type pack expansion. */
3659 bool type_pack_expansion_p;
3660 };
3661
3662 /* Identifies all of the argument packs that occur in a template
3663 argument and appends them to the TREE_LIST inside DATA, which is a
3664 find_parameter_pack_data structure. This is a subroutine of
3665 make_pack_expansion and uses_parameter_packs. */
3666 static tree
3667 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3668 {
3669 tree t = *tp;
3670 struct find_parameter_pack_data* ppd =
3671 (struct find_parameter_pack_data*)data;
3672 bool parameter_pack_p = false;
3673
3674 /* Handle type aliases/typedefs. */
3675 if (TYPE_ALIAS_P (t))
3676 {
3677 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3678 cp_walk_tree (&TI_ARGS (tinfo),
3679 &find_parameter_packs_r,
3680 ppd, ppd->visited);
3681 *walk_subtrees = 0;
3682 return NULL_TREE;
3683 }
3684
3685 /* Identify whether this is a parameter pack or not. */
3686 switch (TREE_CODE (t))
3687 {
3688 case TEMPLATE_PARM_INDEX:
3689 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3690 parameter_pack_p = true;
3691 break;
3692
3693 case TEMPLATE_TYPE_PARM:
3694 t = TYPE_MAIN_VARIANT (t);
3695 /* FALLTHRU */
3696 case TEMPLATE_TEMPLATE_PARM:
3697 /* If the placeholder appears in the decl-specifier-seq of a function
3698 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3699 is a pack expansion, the invented template parameter is a template
3700 parameter pack. */
3701 if (ppd->type_pack_expansion_p && is_auto (t))
3702 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3703 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3704 parameter_pack_p = true;
3705 break;
3706
3707 case FIELD_DECL:
3708 case PARM_DECL:
3709 if (DECL_PACK_P (t))
3710 {
3711 /* We don't want to walk into the type of a PARM_DECL,
3712 because we don't want to see the type parameter pack. */
3713 *walk_subtrees = 0;
3714 parameter_pack_p = true;
3715 }
3716 break;
3717
3718 case VAR_DECL:
3719 if (DECL_PACK_P (t))
3720 {
3721 /* We don't want to walk into the type of a variadic capture proxy,
3722 because we don't want to see the type parameter pack. */
3723 *walk_subtrees = 0;
3724 parameter_pack_p = true;
3725 }
3726 else if (variable_template_specialization_p (t))
3727 {
3728 cp_walk_tree (&DECL_TI_ARGS (t),
3729 find_parameter_packs_r,
3730 ppd, ppd->visited);
3731 *walk_subtrees = 0;
3732 }
3733 break;
3734
3735 case CALL_EXPR:
3736 if (builtin_pack_call_p (t))
3737 parameter_pack_p = true;
3738 break;
3739
3740 case BASES:
3741 parameter_pack_p = true;
3742 break;
3743 default:
3744 /* Not a parameter pack. */
3745 break;
3746 }
3747
3748 if (parameter_pack_p)
3749 {
3750 /* Add this parameter pack to the list. */
3751 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3752 }
3753
3754 if (TYPE_P (t))
3755 cp_walk_tree (&TYPE_CONTEXT (t),
3756 &find_parameter_packs_r, ppd, ppd->visited);
3757
3758 /* This switch statement will return immediately if we don't find a
3759 parameter pack. */
3760 switch (TREE_CODE (t))
3761 {
3762 case TEMPLATE_PARM_INDEX:
3763 return NULL_TREE;
3764
3765 case BOUND_TEMPLATE_TEMPLATE_PARM:
3766 /* Check the template itself. */
3767 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3768 &find_parameter_packs_r, ppd, ppd->visited);
3769 /* Check the template arguments. */
3770 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3771 ppd->visited);
3772 *walk_subtrees = 0;
3773 return NULL_TREE;
3774
3775 case TEMPLATE_TYPE_PARM:
3776 case TEMPLATE_TEMPLATE_PARM:
3777 return NULL_TREE;
3778
3779 case PARM_DECL:
3780 return NULL_TREE;
3781
3782 case DECL_EXPR:
3783 /* Ignore the declaration of a capture proxy for a parameter pack. */
3784 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3785 *walk_subtrees = 0;
3786 return NULL_TREE;
3787
3788 case RECORD_TYPE:
3789 if (TYPE_PTRMEMFUNC_P (t))
3790 return NULL_TREE;
3791 /* Fall through. */
3792
3793 case UNION_TYPE:
3794 case ENUMERAL_TYPE:
3795 if (TYPE_TEMPLATE_INFO (t))
3796 cp_walk_tree (&TYPE_TI_ARGS (t),
3797 &find_parameter_packs_r, ppd, ppd->visited);
3798
3799 *walk_subtrees = 0;
3800 return NULL_TREE;
3801
3802 case TEMPLATE_DECL:
3803 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3804 return NULL_TREE;
3805 gcc_fallthrough();
3806
3807 case CONSTRUCTOR:
3808 cp_walk_tree (&TREE_TYPE (t),
3809 &find_parameter_packs_r, ppd, ppd->visited);
3810 return NULL_TREE;
3811
3812 case TYPENAME_TYPE:
3813 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3814 ppd, ppd->visited);
3815 *walk_subtrees = 0;
3816 return NULL_TREE;
3817
3818 case TYPE_PACK_EXPANSION:
3819 case EXPR_PACK_EXPANSION:
3820 *walk_subtrees = 0;
3821 return NULL_TREE;
3822
3823 case INTEGER_TYPE:
3824 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3825 ppd, ppd->visited);
3826 *walk_subtrees = 0;
3827 return NULL_TREE;
3828
3829 case IDENTIFIER_NODE:
3830 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3831 ppd->visited);
3832 *walk_subtrees = 0;
3833 return NULL_TREE;
3834
3835 case LAMBDA_EXPR:
3836 {
3837 /* Look at explicit captures. */
3838 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3839 cap; cap = TREE_CHAIN (cap))
3840 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3841 ppd->visited);
3842 /* Since we defer implicit capture, look in the body as well. */
3843 tree fn = lambda_function (t);
3844 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3845 ppd->visited);
3846 *walk_subtrees = 0;
3847 return NULL_TREE;
3848 }
3849
3850 case DECLTYPE_TYPE:
3851 {
3852 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3853 type_pack_expansion_p to false so that any placeholders
3854 within the expression don't get marked as parameter packs. */
3855 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3856 ppd->type_pack_expansion_p = false;
3857 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3858 ppd, ppd->visited);
3859 ppd->type_pack_expansion_p = type_pack_expansion_p;
3860 *walk_subtrees = 0;
3861 return NULL_TREE;
3862 }
3863
3864 default:
3865 return NULL_TREE;
3866 }
3867
3868 return NULL_TREE;
3869 }
3870
3871 /* Determines if the expression or type T uses any parameter packs. */
3872 bool
3873 uses_parameter_packs (tree t)
3874 {
3875 tree parameter_packs = NULL_TREE;
3876 struct find_parameter_pack_data ppd;
3877 ppd.parameter_packs = &parameter_packs;
3878 ppd.visited = new hash_set<tree>;
3879 ppd.type_pack_expansion_p = false;
3880 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3881 delete ppd.visited;
3882 return parameter_packs != NULL_TREE;
3883 }
3884
3885 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3886 representation a base-class initializer into a parameter pack
3887 expansion. If all goes well, the resulting node will be an
3888 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3889 respectively. */
3890 tree
3891 make_pack_expansion (tree arg, tsubst_flags_t complain)
3892 {
3893 tree result;
3894 tree parameter_packs = NULL_TREE;
3895 bool for_types = false;
3896 struct find_parameter_pack_data ppd;
3897
3898 if (!arg || arg == error_mark_node)
3899 return arg;
3900
3901 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3902 {
3903 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3904 class initializer. In this case, the TREE_PURPOSE will be a
3905 _TYPE node (representing the base class expansion we're
3906 initializing) and the TREE_VALUE will be a TREE_LIST
3907 containing the initialization arguments.
3908
3909 The resulting expansion looks somewhat different from most
3910 expansions. Rather than returning just one _EXPANSION, we
3911 return a TREE_LIST whose TREE_PURPOSE is a
3912 TYPE_PACK_EXPANSION containing the bases that will be
3913 initialized. The TREE_VALUE will be identical to the
3914 original TREE_VALUE, which is a list of arguments that will
3915 be passed to each base. We do not introduce any new pack
3916 expansion nodes into the TREE_VALUE (although it is possible
3917 that some already exist), because the TREE_PURPOSE and
3918 TREE_VALUE all need to be expanded together with the same
3919 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3920 resulting TREE_PURPOSE will mention the parameter packs in
3921 both the bases and the arguments to the bases. */
3922 tree purpose;
3923 tree value;
3924 tree parameter_packs = NULL_TREE;
3925
3926 /* Determine which parameter packs will be used by the base
3927 class expansion. */
3928 ppd.visited = new hash_set<tree>;
3929 ppd.parameter_packs = &parameter_packs;
3930 ppd.type_pack_expansion_p = true;
3931 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3932 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3933 &ppd, ppd.visited);
3934
3935 if (parameter_packs == NULL_TREE)
3936 {
3937 if (complain & tf_error)
3938 error ("base initializer expansion %qT contains no parameter packs",
3939 arg);
3940 delete ppd.visited;
3941 return error_mark_node;
3942 }
3943
3944 if (TREE_VALUE (arg) != void_type_node)
3945 {
3946 /* Collect the sets of parameter packs used in each of the
3947 initialization arguments. */
3948 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3949 {
3950 /* Determine which parameter packs will be expanded in this
3951 argument. */
3952 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3953 &ppd, ppd.visited);
3954 }
3955 }
3956
3957 delete ppd.visited;
3958
3959 /* Create the pack expansion type for the base type. */
3960 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3961 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3962 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3963 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3964
3965 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3966 they will rarely be compared to anything. */
3967 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3968
3969 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3970 }
3971
3972 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3973 for_types = true;
3974
3975 /* Build the PACK_EXPANSION_* node. */
3976 result = for_types
3977 ? cxx_make_type (TYPE_PACK_EXPANSION)
3978 : make_node (EXPR_PACK_EXPANSION);
3979 SET_PACK_EXPANSION_PATTERN (result, arg);
3980 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3981 {
3982 /* Propagate type and const-expression information. */
3983 TREE_TYPE (result) = TREE_TYPE (arg);
3984 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3985 /* Mark this read now, since the expansion might be length 0. */
3986 mark_exp_read (arg);
3987 }
3988 else
3989 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3990 they will rarely be compared to anything. */
3991 SET_TYPE_STRUCTURAL_EQUALITY (result);
3992
3993 /* Determine which parameter packs will be expanded. */
3994 ppd.parameter_packs = &parameter_packs;
3995 ppd.visited = new hash_set<tree>;
3996 ppd.type_pack_expansion_p = TYPE_P (arg);
3997 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3998 delete ppd.visited;
3999
4000 /* Make sure we found some parameter packs. */
4001 if (parameter_packs == NULL_TREE)
4002 {
4003 if (complain & tf_error)
4004 {
4005 if (TYPE_P (arg))
4006 error ("expansion pattern %qT contains no parameter packs", arg);
4007 else
4008 error ("expansion pattern %qE contains no parameter packs", arg);
4009 }
4010 return error_mark_node;
4011 }
4012 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4013
4014 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4015
4016 return result;
4017 }
4018
4019 /* Checks T for any "bare" parameter packs, which have not yet been
4020 expanded, and issues an error if any are found. This operation can
4021 only be done on full expressions or types (e.g., an expression
4022 statement, "if" condition, etc.), because we could have expressions like:
4023
4024 foo(f(g(h(args)))...)
4025
4026 where "args" is a parameter pack. check_for_bare_parameter_packs
4027 should not be called for the subexpressions args, h(args),
4028 g(h(args)), or f(g(h(args))), because we would produce erroneous
4029 error messages.
4030
4031 Returns TRUE and emits an error if there were bare parameter packs,
4032 returns FALSE otherwise. */
4033 bool
4034 check_for_bare_parameter_packs (tree t)
4035 {
4036 tree parameter_packs = NULL_TREE;
4037 struct find_parameter_pack_data ppd;
4038
4039 if (!processing_template_decl || !t || t == error_mark_node)
4040 return false;
4041
4042 /* A lambda might use a parameter pack from the containing context. */
4043 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4044 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4045 return false;
4046
4047 if (TREE_CODE (t) == TYPE_DECL)
4048 t = TREE_TYPE (t);
4049
4050 ppd.parameter_packs = &parameter_packs;
4051 ppd.visited = new hash_set<tree>;
4052 ppd.type_pack_expansion_p = false;
4053 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4054 delete ppd.visited;
4055
4056 if (parameter_packs)
4057 {
4058 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
4059 error_at (loc, "parameter packs not expanded with %<...%>:");
4060 while (parameter_packs)
4061 {
4062 tree pack = TREE_VALUE (parameter_packs);
4063 tree name = NULL_TREE;
4064
4065 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4066 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4067 name = TYPE_NAME (pack);
4068 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4069 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4070 else if (TREE_CODE (pack) == CALL_EXPR)
4071 name = DECL_NAME (CALL_EXPR_FN (pack));
4072 else
4073 name = DECL_NAME (pack);
4074
4075 if (name)
4076 inform (loc, " %qD", name);
4077 else
4078 inform (loc, " <anonymous>");
4079
4080 parameter_packs = TREE_CHAIN (parameter_packs);
4081 }
4082
4083 return true;
4084 }
4085
4086 return false;
4087 }
4088
4089 /* Expand any parameter packs that occur in the template arguments in
4090 ARGS. */
4091 tree
4092 expand_template_argument_pack (tree args)
4093 {
4094 if (args == error_mark_node)
4095 return error_mark_node;
4096
4097 tree result_args = NULL_TREE;
4098 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4099 int num_result_args = -1;
4100 int non_default_args_count = -1;
4101
4102 /* First, determine if we need to expand anything, and the number of
4103 slots we'll need. */
4104 for (in_arg = 0; in_arg < nargs; ++in_arg)
4105 {
4106 tree arg = TREE_VEC_ELT (args, in_arg);
4107 if (arg == NULL_TREE)
4108 return args;
4109 if (ARGUMENT_PACK_P (arg))
4110 {
4111 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4112 if (num_result_args < 0)
4113 num_result_args = in_arg + num_packed;
4114 else
4115 num_result_args += num_packed;
4116 }
4117 else
4118 {
4119 if (num_result_args >= 0)
4120 num_result_args++;
4121 }
4122 }
4123
4124 /* If no expansion is necessary, we're done. */
4125 if (num_result_args < 0)
4126 return args;
4127
4128 /* Expand arguments. */
4129 result_args = make_tree_vec (num_result_args);
4130 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4131 non_default_args_count =
4132 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4133 for (in_arg = 0; in_arg < nargs; ++in_arg)
4134 {
4135 tree arg = TREE_VEC_ELT (args, in_arg);
4136 if (ARGUMENT_PACK_P (arg))
4137 {
4138 tree packed = ARGUMENT_PACK_ARGS (arg);
4139 int i, num_packed = TREE_VEC_LENGTH (packed);
4140 for (i = 0; i < num_packed; ++i, ++out_arg)
4141 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4142 if (non_default_args_count > 0)
4143 non_default_args_count += num_packed - 1;
4144 }
4145 else
4146 {
4147 TREE_VEC_ELT (result_args, out_arg) = arg;
4148 ++out_arg;
4149 }
4150 }
4151 if (non_default_args_count >= 0)
4152 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4153 return result_args;
4154 }
4155
4156 /* Checks if DECL shadows a template parameter.
4157
4158 [temp.local]: A template-parameter shall not be redeclared within its
4159 scope (including nested scopes).
4160
4161 Emits an error and returns TRUE if the DECL shadows a parameter,
4162 returns FALSE otherwise. */
4163
4164 bool
4165 check_template_shadow (tree decl)
4166 {
4167 tree olddecl;
4168
4169 /* If we're not in a template, we can't possibly shadow a template
4170 parameter. */
4171 if (!current_template_parms)
4172 return true;
4173
4174 /* Figure out what we're shadowing. */
4175 decl = OVL_FIRST (decl);
4176 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4177
4178 /* If there's no previous binding for this name, we're not shadowing
4179 anything, let alone a template parameter. */
4180 if (!olddecl)
4181 return true;
4182
4183 /* If we're not shadowing a template parameter, we're done. Note
4184 that OLDDECL might be an OVERLOAD (or perhaps even an
4185 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4186 node. */
4187 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4188 return true;
4189
4190 /* We check for decl != olddecl to avoid bogus errors for using a
4191 name inside a class. We check TPFI to avoid duplicate errors for
4192 inline member templates. */
4193 if (decl == olddecl
4194 || (DECL_TEMPLATE_PARM_P (decl)
4195 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4196 return true;
4197
4198 /* Don't complain about the injected class name, as we've already
4199 complained about the class itself. */
4200 if (DECL_SELF_REFERENCE_P (decl))
4201 return false;
4202
4203 if (DECL_TEMPLATE_PARM_P (decl))
4204 error ("declaration of template parameter %q+D shadows "
4205 "template parameter", decl);
4206 else
4207 error ("declaration of %q+#D shadows template parameter", decl);
4208 inform (DECL_SOURCE_LOCATION (olddecl),
4209 "template parameter %qD declared here", olddecl);
4210 return false;
4211 }
4212
4213 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4214 ORIG_LEVEL, DECL, and TYPE. */
4215
4216 static tree
4217 build_template_parm_index (int index,
4218 int level,
4219 int orig_level,
4220 tree decl,
4221 tree type)
4222 {
4223 tree t = make_node (TEMPLATE_PARM_INDEX);
4224 TEMPLATE_PARM_IDX (t) = index;
4225 TEMPLATE_PARM_LEVEL (t) = level;
4226 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4227 TEMPLATE_PARM_DECL (t) = decl;
4228 TREE_TYPE (t) = type;
4229 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4230 TREE_READONLY (t) = TREE_READONLY (decl);
4231
4232 return t;
4233 }
4234
4235 /* Find the canonical type parameter for the given template type
4236 parameter. Returns the canonical type parameter, which may be TYPE
4237 if no such parameter existed. */
4238
4239 static tree
4240 canonical_type_parameter (tree type)
4241 {
4242 tree list;
4243 int idx = TEMPLATE_TYPE_IDX (type);
4244 if (!canonical_template_parms)
4245 vec_alloc (canonical_template_parms, idx + 1);
4246
4247 if (canonical_template_parms->length () <= (unsigned) idx)
4248 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4249
4250 list = (*canonical_template_parms)[idx];
4251 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4252 list = TREE_CHAIN (list);
4253
4254 if (list)
4255 return TREE_VALUE (list);
4256 else
4257 {
4258 (*canonical_template_parms)[idx]
4259 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4260 return type;
4261 }
4262 }
4263
4264 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4265 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4266 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4267 new one is created. */
4268
4269 static tree
4270 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4271 tsubst_flags_t complain)
4272 {
4273 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4274 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4275 != TEMPLATE_PARM_LEVEL (index) - levels)
4276 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4277 {
4278 tree orig_decl = TEMPLATE_PARM_DECL (index);
4279 tree decl, t;
4280
4281 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4282 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4283 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4284 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4285 DECL_ARTIFICIAL (decl) = 1;
4286 SET_DECL_TEMPLATE_PARM_P (decl);
4287
4288 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4289 TEMPLATE_PARM_LEVEL (index) - levels,
4290 TEMPLATE_PARM_ORIG_LEVEL (index),
4291 decl, type);
4292 TEMPLATE_PARM_DESCENDANTS (index) = t;
4293 TEMPLATE_PARM_PARAMETER_PACK (t)
4294 = TEMPLATE_PARM_PARAMETER_PACK (index);
4295
4296 /* Template template parameters need this. */
4297 if (TREE_CODE (decl) == TEMPLATE_DECL)
4298 {
4299 DECL_TEMPLATE_RESULT (decl)
4300 = build_decl (DECL_SOURCE_LOCATION (decl),
4301 TYPE_DECL, DECL_NAME (decl), type);
4302 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4303 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4304 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4305 }
4306 }
4307
4308 return TEMPLATE_PARM_DESCENDANTS (index);
4309 }
4310
4311 /* Process information from new template parameter PARM and append it
4312 to the LIST being built. This new parameter is a non-type
4313 parameter iff IS_NON_TYPE is true. This new parameter is a
4314 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4315 is in PARM_LOC. */
4316
4317 tree
4318 process_template_parm (tree list, location_t parm_loc, tree parm,
4319 bool is_non_type, bool is_parameter_pack)
4320 {
4321 tree decl = 0;
4322 int idx = 0;
4323
4324 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4325 tree defval = TREE_PURPOSE (parm);
4326 tree constr = TREE_TYPE (parm);
4327
4328 if (list)
4329 {
4330 tree p = tree_last (list);
4331
4332 if (p && TREE_VALUE (p) != error_mark_node)
4333 {
4334 p = TREE_VALUE (p);
4335 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4336 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4337 else
4338 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4339 }
4340
4341 ++idx;
4342 }
4343
4344 if (is_non_type)
4345 {
4346 parm = TREE_VALUE (parm);
4347
4348 SET_DECL_TEMPLATE_PARM_P (parm);
4349
4350 if (TREE_TYPE (parm) != error_mark_node)
4351 {
4352 /* [temp.param]
4353
4354 The top-level cv-qualifiers on the template-parameter are
4355 ignored when determining its type. */
4356 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4357 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4358 TREE_TYPE (parm) = error_mark_node;
4359 else if (uses_parameter_packs (TREE_TYPE (parm))
4360 && !is_parameter_pack
4361 /* If we're in a nested template parameter list, the template
4362 template parameter could be a parameter pack. */
4363 && processing_template_parmlist == 1)
4364 {
4365 /* This template parameter is not a parameter pack, but it
4366 should be. Complain about "bare" parameter packs. */
4367 check_for_bare_parameter_packs (TREE_TYPE (parm));
4368
4369 /* Recover by calling this a parameter pack. */
4370 is_parameter_pack = true;
4371 }
4372 }
4373
4374 /* A template parameter is not modifiable. */
4375 TREE_CONSTANT (parm) = 1;
4376 TREE_READONLY (parm) = 1;
4377 decl = build_decl (parm_loc,
4378 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4379 TREE_CONSTANT (decl) = 1;
4380 TREE_READONLY (decl) = 1;
4381 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4382 = build_template_parm_index (idx, processing_template_decl,
4383 processing_template_decl,
4384 decl, TREE_TYPE (parm));
4385
4386 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4387 = is_parameter_pack;
4388 }
4389 else
4390 {
4391 tree t;
4392 parm = TREE_VALUE (TREE_VALUE (parm));
4393
4394 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4395 {
4396 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4397 /* This is for distinguishing between real templates and template
4398 template parameters */
4399 TREE_TYPE (parm) = t;
4400 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4401 decl = parm;
4402 }
4403 else
4404 {
4405 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4406 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4407 decl = build_decl (parm_loc,
4408 TYPE_DECL, parm, t);
4409 }
4410
4411 TYPE_NAME (t) = decl;
4412 TYPE_STUB_DECL (t) = decl;
4413 parm = decl;
4414 TEMPLATE_TYPE_PARM_INDEX (t)
4415 = build_template_parm_index (idx, processing_template_decl,
4416 processing_template_decl,
4417 decl, TREE_TYPE (parm));
4418 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4419 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4420 }
4421 DECL_ARTIFICIAL (decl) = 1;
4422 SET_DECL_TEMPLATE_PARM_P (decl);
4423
4424 /* Build requirements for the type/template parameter.
4425 This must be done after SET_DECL_TEMPLATE_PARM_P or
4426 process_template_parm could fail. */
4427 tree reqs = finish_shorthand_constraint (parm, constr);
4428
4429 pushdecl (decl);
4430
4431 if (defval && TREE_CODE (defval) == OVERLOAD)
4432 lookup_keep (defval, true);
4433
4434 /* Build the parameter node linking the parameter declaration,
4435 its default argument (if any), and its constraints (if any). */
4436 parm = build_tree_list (defval, parm);
4437 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4438
4439 return chainon (list, parm);
4440 }
4441
4442 /* The end of a template parameter list has been reached. Process the
4443 tree list into a parameter vector, converting each parameter into a more
4444 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4445 as PARM_DECLs. */
4446
4447 tree
4448 end_template_parm_list (tree parms)
4449 {
4450 int nparms;
4451 tree parm, next;
4452 tree saved_parmlist = make_tree_vec (list_length (parms));
4453
4454 /* Pop the dummy parameter level and add the real one. */
4455 current_template_parms = TREE_CHAIN (current_template_parms);
4456
4457 current_template_parms
4458 = tree_cons (size_int (processing_template_decl),
4459 saved_parmlist, current_template_parms);
4460
4461 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4462 {
4463 next = TREE_CHAIN (parm);
4464 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4465 TREE_CHAIN (parm) = NULL_TREE;
4466 }
4467
4468 --processing_template_parmlist;
4469
4470 return saved_parmlist;
4471 }
4472
4473 // Explicitly indicate the end of the template parameter list. We assume
4474 // that the current template parameters have been constructed and/or
4475 // managed explicitly, as when creating new template template parameters
4476 // from a shorthand constraint.
4477 void
4478 end_template_parm_list ()
4479 {
4480 --processing_template_parmlist;
4481 }
4482
4483 /* end_template_decl is called after a template declaration is seen. */
4484
4485 void
4486 end_template_decl (void)
4487 {
4488 reset_specialization ();
4489
4490 if (! processing_template_decl)
4491 return;
4492
4493 /* This matches the pushlevel in begin_template_parm_list. */
4494 finish_scope ();
4495
4496 --processing_template_decl;
4497 current_template_parms = TREE_CHAIN (current_template_parms);
4498 }
4499
4500 /* Takes a TREE_LIST representing a template parameter and convert it
4501 into an argument suitable to be passed to the type substitution
4502 functions. Note that If the TREE_LIST contains an error_mark
4503 node, the returned argument is error_mark_node. */
4504
4505 tree
4506 template_parm_to_arg (tree t)
4507 {
4508
4509 if (t == NULL_TREE
4510 || TREE_CODE (t) != TREE_LIST)
4511 return t;
4512
4513 if (error_operand_p (TREE_VALUE (t)))
4514 return error_mark_node;
4515
4516 t = TREE_VALUE (t);
4517
4518 if (TREE_CODE (t) == TYPE_DECL
4519 || TREE_CODE (t) == TEMPLATE_DECL)
4520 {
4521 t = TREE_TYPE (t);
4522
4523 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4524 {
4525 /* Turn this argument into a TYPE_ARGUMENT_PACK
4526 with a single element, which expands T. */
4527 tree vec = make_tree_vec (1);
4528 if (CHECKING_P)
4529 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4530
4531 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4532
4533 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4534 SET_ARGUMENT_PACK_ARGS (t, vec);
4535 }
4536 }
4537 else
4538 {
4539 t = DECL_INITIAL (t);
4540
4541 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4542 {
4543 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4544 with a single element, which expands T. */
4545 tree vec = make_tree_vec (1);
4546 if (CHECKING_P)
4547 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4548
4549 t = convert_from_reference (t);
4550 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4551
4552 t = make_node (NONTYPE_ARGUMENT_PACK);
4553 SET_ARGUMENT_PACK_ARGS (t, vec);
4554 }
4555 else
4556 t = convert_from_reference (t);
4557 }
4558 return t;
4559 }
4560
4561 /* Given a single level of template parameters (a TREE_VEC), return it
4562 as a set of template arguments. */
4563
4564 static tree
4565 template_parms_level_to_args (tree parms)
4566 {
4567 tree a = copy_node (parms);
4568 TREE_TYPE (a) = NULL_TREE;
4569 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4570 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4571
4572 if (CHECKING_P)
4573 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4574
4575 return a;
4576 }
4577
4578 /* Given a set of template parameters, return them as a set of template
4579 arguments. The template parameters are represented as a TREE_VEC, in
4580 the form documented in cp-tree.h for template arguments. */
4581
4582 static tree
4583 template_parms_to_args (tree parms)
4584 {
4585 tree header;
4586 tree args = NULL_TREE;
4587 int length = TMPL_PARMS_DEPTH (parms);
4588 int l = length;
4589
4590 /* If there is only one level of template parameters, we do not
4591 create a TREE_VEC of TREE_VECs. Instead, we return a single
4592 TREE_VEC containing the arguments. */
4593 if (length > 1)
4594 args = make_tree_vec (length);
4595
4596 for (header = parms; header; header = TREE_CHAIN (header))
4597 {
4598 tree a = template_parms_level_to_args (TREE_VALUE (header));
4599
4600 if (length > 1)
4601 TREE_VEC_ELT (args, --l) = a;
4602 else
4603 args = a;
4604 }
4605
4606 return args;
4607 }
4608
4609 /* Within the declaration of a template, return the currently active
4610 template parameters as an argument TREE_VEC. */
4611
4612 static tree
4613 current_template_args (void)
4614 {
4615 return template_parms_to_args (current_template_parms);
4616 }
4617
4618 /* Update the declared TYPE by doing any lookups which were thought to be
4619 dependent, but are not now that we know the SCOPE of the declarator. */
4620
4621 tree
4622 maybe_update_decl_type (tree orig_type, tree scope)
4623 {
4624 tree type = orig_type;
4625
4626 if (type == NULL_TREE)
4627 return type;
4628
4629 if (TREE_CODE (orig_type) == TYPE_DECL)
4630 type = TREE_TYPE (type);
4631
4632 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4633 && dependent_type_p (type)
4634 /* Don't bother building up the args in this case. */
4635 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4636 {
4637 /* tsubst in the args corresponding to the template parameters,
4638 including auto if present. Most things will be unchanged, but
4639 make_typename_type and tsubst_qualified_id will resolve
4640 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4641 tree args = current_template_args ();
4642 tree auto_node = type_uses_auto (type);
4643 tree pushed;
4644 if (auto_node)
4645 {
4646 tree auto_vec = make_tree_vec (1);
4647 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4648 args = add_to_template_args (args, auto_vec);
4649 }
4650 pushed = push_scope (scope);
4651 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4652 if (pushed)
4653 pop_scope (scope);
4654 }
4655
4656 if (type == error_mark_node)
4657 return orig_type;
4658
4659 if (TREE_CODE (orig_type) == TYPE_DECL)
4660 {
4661 if (same_type_p (type, TREE_TYPE (orig_type)))
4662 type = orig_type;
4663 else
4664 type = TYPE_NAME (type);
4665 }
4666 return type;
4667 }
4668
4669 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4670 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4671 the new template is a member template. */
4672
4673 static tree
4674 build_template_decl (tree decl, tree parms, bool member_template_p)
4675 {
4676 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4677 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4678 DECL_TEMPLATE_PARMS (tmpl) = parms;
4679 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4680 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4681 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4682
4683 return tmpl;
4684 }
4685
4686 struct template_parm_data
4687 {
4688 /* The level of the template parameters we are currently
4689 processing. */
4690 int level;
4691
4692 /* The index of the specialization argument we are currently
4693 processing. */
4694 int current_arg;
4695
4696 /* An array whose size is the number of template parameters. The
4697 elements are nonzero if the parameter has been used in any one
4698 of the arguments processed so far. */
4699 int* parms;
4700
4701 /* An array whose size is the number of template arguments. The
4702 elements are nonzero if the argument makes use of template
4703 parameters of this level. */
4704 int* arg_uses_template_parms;
4705 };
4706
4707 /* Subroutine of push_template_decl used to see if each template
4708 parameter in a partial specialization is used in the explicit
4709 argument list. If T is of the LEVEL given in DATA (which is
4710 treated as a template_parm_data*), then DATA->PARMS is marked
4711 appropriately. */
4712
4713 static int
4714 mark_template_parm (tree t, void* data)
4715 {
4716 int level;
4717 int idx;
4718 struct template_parm_data* tpd = (struct template_parm_data*) data;
4719
4720 template_parm_level_and_index (t, &level, &idx);
4721
4722 if (level == tpd->level)
4723 {
4724 tpd->parms[idx] = 1;
4725 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4726 }
4727
4728 /* In C++17 the type of a non-type argument is a deduced context. */
4729 if (cxx_dialect >= cxx17
4730 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4731 for_each_template_parm (TREE_TYPE (t),
4732 &mark_template_parm,
4733 data,
4734 NULL,
4735 /*include_nondeduced_p=*/false);
4736
4737 /* Return zero so that for_each_template_parm will continue the
4738 traversal of the tree; we want to mark *every* template parm. */
4739 return 0;
4740 }
4741
4742 /* Process the partial specialization DECL. */
4743
4744 static tree
4745 process_partial_specialization (tree decl)
4746 {
4747 tree type = TREE_TYPE (decl);
4748 tree tinfo = get_template_info (decl);
4749 tree maintmpl = TI_TEMPLATE (tinfo);
4750 tree specargs = TI_ARGS (tinfo);
4751 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4752 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4753 tree inner_parms;
4754 tree inst;
4755 int nargs = TREE_VEC_LENGTH (inner_args);
4756 int ntparms;
4757 int i;
4758 bool did_error_intro = false;
4759 struct template_parm_data tpd;
4760 struct template_parm_data tpd2;
4761
4762 gcc_assert (current_template_parms);
4763
4764 /* A concept cannot be specialized. */
4765 if (flag_concepts && variable_concept_p (maintmpl))
4766 {
4767 error ("specialization of variable concept %q#D", maintmpl);
4768 return error_mark_node;
4769 }
4770
4771 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4772 ntparms = TREE_VEC_LENGTH (inner_parms);
4773
4774 /* We check that each of the template parameters given in the
4775 partial specialization is used in the argument list to the
4776 specialization. For example:
4777
4778 template <class T> struct S;
4779 template <class T> struct S<T*>;
4780
4781 The second declaration is OK because `T*' uses the template
4782 parameter T, whereas
4783
4784 template <class T> struct S<int>;
4785
4786 is no good. Even trickier is:
4787
4788 template <class T>
4789 struct S1
4790 {
4791 template <class U>
4792 struct S2;
4793 template <class U>
4794 struct S2<T>;
4795 };
4796
4797 The S2<T> declaration is actually invalid; it is a
4798 full-specialization. Of course,
4799
4800 template <class U>
4801 struct S2<T (*)(U)>;
4802
4803 or some such would have been OK. */
4804 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4805 tpd.parms = XALLOCAVEC (int, ntparms);
4806 memset (tpd.parms, 0, sizeof (int) * ntparms);
4807
4808 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4809 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4810 for (i = 0; i < nargs; ++i)
4811 {
4812 tpd.current_arg = i;
4813 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4814 &mark_template_parm,
4815 &tpd,
4816 NULL,
4817 /*include_nondeduced_p=*/false);
4818 }
4819 for (i = 0; i < ntparms; ++i)
4820 if (tpd.parms[i] == 0)
4821 {
4822 /* One of the template parms was not used in a deduced context in the
4823 specialization. */
4824 if (!did_error_intro)
4825 {
4826 error ("template parameters not deducible in "
4827 "partial specialization:");
4828 did_error_intro = true;
4829 }
4830
4831 inform (input_location, " %qD",
4832 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4833 }
4834
4835 if (did_error_intro)
4836 return error_mark_node;
4837
4838 /* [temp.class.spec]
4839
4840 The argument list of the specialization shall not be identical to
4841 the implicit argument list of the primary template. */
4842 tree main_args
4843 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4844 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4845 && (!flag_concepts
4846 || !strictly_subsumes (current_template_constraints (),
4847 get_constraints (maintmpl))))
4848 {
4849 if (!flag_concepts)
4850 error ("partial specialization %q+D does not specialize "
4851 "any template arguments; to define the primary template, "
4852 "remove the template argument list", decl);
4853 else
4854 error ("partial specialization %q+D does not specialize any "
4855 "template arguments and is not more constrained than "
4856 "the primary template; to define the primary template, "
4857 "remove the template argument list", decl);
4858 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4859 }
4860
4861 /* A partial specialization that replaces multiple parameters of the
4862 primary template with a pack expansion is less specialized for those
4863 parameters. */
4864 if (nargs < DECL_NTPARMS (maintmpl))
4865 {
4866 error ("partial specialization is not more specialized than the "
4867 "primary template because it replaces multiple parameters "
4868 "with a pack expansion");
4869 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4870 /* Avoid crash in process_partial_specialization. */
4871 return decl;
4872 }
4873
4874 /* If we aren't in a dependent class, we can actually try deduction. */
4875 else if (tpd.level == 1
4876 /* FIXME we should be able to handle a partial specialization of a
4877 partial instantiation, but currently we can't (c++/41727). */
4878 && TMPL_ARGS_DEPTH (specargs) == 1
4879 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4880 {
4881 if (permerror (input_location, "partial specialization %qD is not "
4882 "more specialized than", decl))
4883 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4884 maintmpl);
4885 }
4886
4887 /* [temp.class.spec]
4888
4889 A partially specialized non-type argument expression shall not
4890 involve template parameters of the partial specialization except
4891 when the argument expression is a simple identifier.
4892
4893 The type of a template parameter corresponding to a specialized
4894 non-type argument shall not be dependent on a parameter of the
4895 specialization.
4896
4897 Also, we verify that pack expansions only occur at the
4898 end of the argument list. */
4899 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4900 tpd2.parms = 0;
4901 for (i = 0; i < nargs; ++i)
4902 {
4903 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4904 tree arg = TREE_VEC_ELT (inner_args, i);
4905 tree packed_args = NULL_TREE;
4906 int j, len = 1;
4907
4908 if (ARGUMENT_PACK_P (arg))
4909 {
4910 /* Extract the arguments from the argument pack. We'll be
4911 iterating over these in the following loop. */
4912 packed_args = ARGUMENT_PACK_ARGS (arg);
4913 len = TREE_VEC_LENGTH (packed_args);
4914 }
4915
4916 for (j = 0; j < len; j++)
4917 {
4918 if (packed_args)
4919 /* Get the Jth argument in the parameter pack. */
4920 arg = TREE_VEC_ELT (packed_args, j);
4921
4922 if (PACK_EXPANSION_P (arg))
4923 {
4924 /* Pack expansions must come at the end of the
4925 argument list. */
4926 if ((packed_args && j < len - 1)
4927 || (!packed_args && i < nargs - 1))
4928 {
4929 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4930 error ("parameter pack argument %qE must be at the "
4931 "end of the template argument list", arg);
4932 else
4933 error ("parameter pack argument %qT must be at the "
4934 "end of the template argument list", arg);
4935 }
4936 }
4937
4938 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4939 /* We only care about the pattern. */
4940 arg = PACK_EXPANSION_PATTERN (arg);
4941
4942 if (/* These first two lines are the `non-type' bit. */
4943 !TYPE_P (arg)
4944 && TREE_CODE (arg) != TEMPLATE_DECL
4945 /* This next two lines are the `argument expression is not just a
4946 simple identifier' condition and also the `specialized
4947 non-type argument' bit. */
4948 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4949 && !(REFERENCE_REF_P (arg)
4950 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4951 {
4952 if ((!packed_args && tpd.arg_uses_template_parms[i])
4953 || (packed_args && uses_template_parms (arg)))
4954 error ("template argument %qE involves template parameter(s)",
4955 arg);
4956 else
4957 {
4958 /* Look at the corresponding template parameter,
4959 marking which template parameters its type depends
4960 upon. */
4961 tree type = TREE_TYPE (parm);
4962
4963 if (!tpd2.parms)
4964 {
4965 /* We haven't yet initialized TPD2. Do so now. */
4966 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4967 /* The number of parameters here is the number in the
4968 main template, which, as checked in the assertion
4969 above, is NARGS. */
4970 tpd2.parms = XALLOCAVEC (int, nargs);
4971 tpd2.level =
4972 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4973 }
4974
4975 /* Mark the template parameters. But this time, we're
4976 looking for the template parameters of the main
4977 template, not in the specialization. */
4978 tpd2.current_arg = i;
4979 tpd2.arg_uses_template_parms[i] = 0;
4980 memset (tpd2.parms, 0, sizeof (int) * nargs);
4981 for_each_template_parm (type,
4982 &mark_template_parm,
4983 &tpd2,
4984 NULL,
4985 /*include_nondeduced_p=*/false);
4986
4987 if (tpd2.arg_uses_template_parms [i])
4988 {
4989 /* The type depended on some template parameters.
4990 If they are fully specialized in the
4991 specialization, that's OK. */
4992 int j;
4993 int count = 0;
4994 for (j = 0; j < nargs; ++j)
4995 if (tpd2.parms[j] != 0
4996 && tpd.arg_uses_template_parms [j])
4997 ++count;
4998 if (count != 0)
4999 error_n (input_location, count,
5000 "type %qT of template argument %qE depends "
5001 "on a template parameter",
5002 "type %qT of template argument %qE depends "
5003 "on template parameters",
5004 type,
5005 arg);
5006 }
5007 }
5008 }
5009 }
5010 }
5011
5012 /* We should only get here once. */
5013 if (TREE_CODE (decl) == TYPE_DECL)
5014 gcc_assert (!COMPLETE_TYPE_P (type));
5015
5016 // Build the template decl.
5017 tree tmpl = build_template_decl (decl, current_template_parms,
5018 DECL_MEMBER_TEMPLATE_P (maintmpl));
5019 TREE_TYPE (tmpl) = type;
5020 DECL_TEMPLATE_RESULT (tmpl) = decl;
5021 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5022 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5023 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5024
5025 /* Give template template parms a DECL_CONTEXT of the template
5026 for which they are a parameter. */
5027 for (i = 0; i < ntparms; ++i)
5028 {
5029 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5030 if (TREE_CODE (parm) == TEMPLATE_DECL)
5031 DECL_CONTEXT (parm) = tmpl;
5032 }
5033
5034 if (VAR_P (decl))
5035 /* We didn't register this in check_explicit_specialization so we could
5036 wait until the constraints were set. */
5037 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5038 else
5039 associate_classtype_constraints (type);
5040
5041 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5042 = tree_cons (specargs, tmpl,
5043 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5044 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5045
5046 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5047 inst = TREE_CHAIN (inst))
5048 {
5049 tree instance = TREE_VALUE (inst);
5050 if (TYPE_P (instance)
5051 ? (COMPLETE_TYPE_P (instance)
5052 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5053 : DECL_TEMPLATE_INSTANTIATION (instance))
5054 {
5055 tree spec = most_specialized_partial_spec (instance, tf_none);
5056 tree inst_decl = (DECL_P (instance)
5057 ? instance : TYPE_NAME (instance));
5058 if (!spec)
5059 /* OK */;
5060 else if (spec == error_mark_node)
5061 permerror (input_location,
5062 "declaration of %qD ambiguates earlier template "
5063 "instantiation for %qD", decl, inst_decl);
5064 else if (TREE_VALUE (spec) == tmpl)
5065 permerror (input_location,
5066 "partial specialization of %qD after instantiation "
5067 "of %qD", decl, inst_decl);
5068 }
5069 }
5070
5071 return decl;
5072 }
5073
5074 /* PARM is a template parameter of some form; return the corresponding
5075 TEMPLATE_PARM_INDEX. */
5076
5077 static tree
5078 get_template_parm_index (tree parm)
5079 {
5080 if (TREE_CODE (parm) == PARM_DECL
5081 || TREE_CODE (parm) == CONST_DECL)
5082 parm = DECL_INITIAL (parm);
5083 else if (TREE_CODE (parm) == TYPE_DECL
5084 || TREE_CODE (parm) == TEMPLATE_DECL)
5085 parm = TREE_TYPE (parm);
5086 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5087 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5088 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5089 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5090 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5091 return parm;
5092 }
5093
5094 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5095 parameter packs used by the template parameter PARM. */
5096
5097 static void
5098 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5099 {
5100 /* A type parm can't refer to another parm. */
5101 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5102 return;
5103 else if (TREE_CODE (parm) == PARM_DECL)
5104 {
5105 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5106 ppd, ppd->visited);
5107 return;
5108 }
5109
5110 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5111
5112 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5113 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5114 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5115 }
5116
5117 /* PARM is a template parameter pack. Return any parameter packs used in
5118 its type or the type of any of its template parameters. If there are
5119 any such packs, it will be instantiated into a fixed template parameter
5120 list by partial instantiation rather than be fully deduced. */
5121
5122 tree
5123 fixed_parameter_pack_p (tree parm)
5124 {
5125 /* This can only be true in a member template. */
5126 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5127 return NULL_TREE;
5128 /* This can only be true for a parameter pack. */
5129 if (!template_parameter_pack_p (parm))
5130 return NULL_TREE;
5131 /* A type parm can't refer to another parm. */
5132 if (TREE_CODE (parm) == TYPE_DECL)
5133 return NULL_TREE;
5134
5135 tree parameter_packs = NULL_TREE;
5136 struct find_parameter_pack_data ppd;
5137 ppd.parameter_packs = &parameter_packs;
5138 ppd.visited = new hash_set<tree>;
5139 ppd.type_pack_expansion_p = false;
5140
5141 fixed_parameter_pack_p_1 (parm, &ppd);
5142
5143 delete ppd.visited;
5144 return parameter_packs;
5145 }
5146
5147 /* Check that a template declaration's use of default arguments and
5148 parameter packs is not invalid. Here, PARMS are the template
5149 parameters. IS_PRIMARY is true if DECL is the thing declared by
5150 a primary template. IS_PARTIAL is true if DECL is a partial
5151 specialization.
5152
5153 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5154 function template declaration or a friend class template
5155 declaration. In the function case, 1 indicates a declaration, 2
5156 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5157 emitted for extraneous default arguments.
5158
5159 Returns TRUE if there were no errors found, FALSE otherwise. */
5160
5161 bool
5162 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5163 bool is_partial, int is_friend_decl)
5164 {
5165 const char *msg;
5166 int last_level_to_check;
5167 tree parm_level;
5168 bool no_errors = true;
5169
5170 /* [temp.param]
5171
5172 A default template-argument shall not be specified in a
5173 function template declaration or a function template definition, nor
5174 in the template-parameter-list of the definition of a member of a
5175 class template. */
5176
5177 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5178 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5179 /* You can't have a function template declaration in a local
5180 scope, nor you can you define a member of a class template in a
5181 local scope. */
5182 return true;
5183
5184 if ((TREE_CODE (decl) == TYPE_DECL
5185 && TREE_TYPE (decl)
5186 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5187 || (TREE_CODE (decl) == FUNCTION_DECL
5188 && LAMBDA_FUNCTION_P (decl)))
5189 /* A lambda doesn't have an explicit declaration; don't complain
5190 about the parms of the enclosing class. */
5191 return true;
5192
5193 if (current_class_type
5194 && !TYPE_BEING_DEFINED (current_class_type)
5195 && DECL_LANG_SPECIFIC (decl)
5196 && DECL_DECLARES_FUNCTION_P (decl)
5197 /* If this is either a friend defined in the scope of the class
5198 or a member function. */
5199 && (DECL_FUNCTION_MEMBER_P (decl)
5200 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5201 : DECL_FRIEND_CONTEXT (decl)
5202 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5203 : false)
5204 /* And, if it was a member function, it really was defined in
5205 the scope of the class. */
5206 && (!DECL_FUNCTION_MEMBER_P (decl)
5207 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5208 /* We already checked these parameters when the template was
5209 declared, so there's no need to do it again now. This function
5210 was defined in class scope, but we're processing its body now
5211 that the class is complete. */
5212 return true;
5213
5214 /* Core issue 226 (C++0x only): the following only applies to class
5215 templates. */
5216 if (is_primary
5217 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5218 {
5219 /* [temp.param]
5220
5221 If a template-parameter has a default template-argument, all
5222 subsequent template-parameters shall have a default
5223 template-argument supplied. */
5224 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5225 {
5226 tree inner_parms = TREE_VALUE (parm_level);
5227 int ntparms = TREE_VEC_LENGTH (inner_parms);
5228 int seen_def_arg_p = 0;
5229 int i;
5230
5231 for (i = 0; i < ntparms; ++i)
5232 {
5233 tree parm = TREE_VEC_ELT (inner_parms, i);
5234
5235 if (parm == error_mark_node)
5236 continue;
5237
5238 if (TREE_PURPOSE (parm))
5239 seen_def_arg_p = 1;
5240 else if (seen_def_arg_p
5241 && !template_parameter_pack_p (TREE_VALUE (parm)))
5242 {
5243 error ("no default argument for %qD", TREE_VALUE (parm));
5244 /* For better subsequent error-recovery, we indicate that
5245 there should have been a default argument. */
5246 TREE_PURPOSE (parm) = error_mark_node;
5247 no_errors = false;
5248 }
5249 else if (!is_partial
5250 && !is_friend_decl
5251 /* Don't complain about an enclosing partial
5252 specialization. */
5253 && parm_level == parms
5254 && TREE_CODE (decl) == TYPE_DECL
5255 && i < ntparms - 1
5256 && template_parameter_pack_p (TREE_VALUE (parm))
5257 /* A fixed parameter pack will be partially
5258 instantiated into a fixed length list. */
5259 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5260 {
5261 /* A primary class template can only have one
5262 parameter pack, at the end of the template
5263 parameter list. */
5264
5265 error ("parameter pack %q+D must be at the end of the"
5266 " template parameter list", TREE_VALUE (parm));
5267
5268 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5269 = error_mark_node;
5270 no_errors = false;
5271 }
5272 }
5273 }
5274 }
5275
5276 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5277 || is_partial
5278 || !is_primary
5279 || is_friend_decl)
5280 /* For an ordinary class template, default template arguments are
5281 allowed at the innermost level, e.g.:
5282 template <class T = int>
5283 struct S {};
5284 but, in a partial specialization, they're not allowed even
5285 there, as we have in [temp.class.spec]:
5286
5287 The template parameter list of a specialization shall not
5288 contain default template argument values.
5289
5290 So, for a partial specialization, or for a function template
5291 (in C++98/C++03), we look at all of them. */
5292 ;
5293 else
5294 /* But, for a primary class template that is not a partial
5295 specialization we look at all template parameters except the
5296 innermost ones. */
5297 parms = TREE_CHAIN (parms);
5298
5299 /* Figure out what error message to issue. */
5300 if (is_friend_decl == 2)
5301 msg = G_("default template arguments may not be used in function template "
5302 "friend re-declaration");
5303 else if (is_friend_decl)
5304 msg = G_("default template arguments may not be used in template "
5305 "friend declarations");
5306 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5307 msg = G_("default template arguments may not be used in function templates "
5308 "without -std=c++11 or -std=gnu++11");
5309 else if (is_partial)
5310 msg = G_("default template arguments may not be used in "
5311 "partial specializations");
5312 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5313 msg = G_("default argument for template parameter for class enclosing %qD");
5314 else
5315 /* Per [temp.param]/9, "A default template-argument shall not be
5316 specified in the template-parameter-lists of the definition of
5317 a member of a class template that appears outside of the member's
5318 class.", thus if we aren't handling a member of a class template
5319 there is no need to examine the parameters. */
5320 return true;
5321
5322 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5323 /* If we're inside a class definition, there's no need to
5324 examine the parameters to the class itself. On the one
5325 hand, they will be checked when the class is defined, and,
5326 on the other, default arguments are valid in things like:
5327 template <class T = double>
5328 struct S { template <class U> void f(U); };
5329 Here the default argument for `S' has no bearing on the
5330 declaration of `f'. */
5331 last_level_to_check = template_class_depth (current_class_type) + 1;
5332 else
5333 /* Check everything. */
5334 last_level_to_check = 0;
5335
5336 for (parm_level = parms;
5337 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5338 parm_level = TREE_CHAIN (parm_level))
5339 {
5340 tree inner_parms = TREE_VALUE (parm_level);
5341 int i;
5342 int ntparms;
5343
5344 ntparms = TREE_VEC_LENGTH (inner_parms);
5345 for (i = 0; i < ntparms; ++i)
5346 {
5347 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5348 continue;
5349
5350 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5351 {
5352 if (msg)
5353 {
5354 no_errors = false;
5355 if (is_friend_decl == 2)
5356 return no_errors;
5357
5358 error (msg, decl);
5359 msg = 0;
5360 }
5361
5362 /* Clear out the default argument so that we are not
5363 confused later. */
5364 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5365 }
5366 }
5367
5368 /* At this point, if we're still interested in issuing messages,
5369 they must apply to classes surrounding the object declared. */
5370 if (msg)
5371 msg = G_("default argument for template parameter for class "
5372 "enclosing %qD");
5373 }
5374
5375 return no_errors;
5376 }
5377
5378 /* Worker for push_template_decl_real, called via
5379 for_each_template_parm. DATA is really an int, indicating the
5380 level of the parameters we are interested in. If T is a template
5381 parameter of that level, return nonzero. */
5382
5383 static int
5384 template_parm_this_level_p (tree t, void* data)
5385 {
5386 int this_level = *(int *)data;
5387 int level;
5388
5389 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5390 level = TEMPLATE_PARM_LEVEL (t);
5391 else
5392 level = TEMPLATE_TYPE_LEVEL (t);
5393 return level == this_level;
5394 }
5395
5396 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5397 DATA is really an int, indicating the innermost outer level of parameters.
5398 If T is a template parameter of that level or further out, return
5399 nonzero. */
5400
5401 static int
5402 template_parm_outer_level (tree t, void *data)
5403 {
5404 int this_level = *(int *)data;
5405 int level;
5406
5407 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5408 level = TEMPLATE_PARM_LEVEL (t);
5409 else
5410 level = TEMPLATE_TYPE_LEVEL (t);
5411 return level <= this_level;
5412 }
5413
5414 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5415 parameters given by current_template_args, or reuses a
5416 previously existing one, if appropriate. Returns the DECL, or an
5417 equivalent one, if it is replaced via a call to duplicate_decls.
5418
5419 If IS_FRIEND is true, DECL is a friend declaration. */
5420
5421 tree
5422 push_template_decl_real (tree decl, bool is_friend)
5423 {
5424 tree tmpl;
5425 tree args;
5426 tree info;
5427 tree ctx;
5428 bool is_primary;
5429 bool is_partial;
5430 int new_template_p = 0;
5431 /* True if the template is a member template, in the sense of
5432 [temp.mem]. */
5433 bool member_template_p = false;
5434
5435 if (decl == error_mark_node || !current_template_parms)
5436 return error_mark_node;
5437
5438 /* See if this is a partial specialization. */
5439 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5440 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5441 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5442 || (VAR_P (decl)
5443 && DECL_LANG_SPECIFIC (decl)
5444 && DECL_TEMPLATE_SPECIALIZATION (decl)
5445 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5446
5447 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5448 is_friend = true;
5449
5450 if (is_friend)
5451 /* For a friend, we want the context of the friend, not
5452 the type of which it is a friend. */
5453 ctx = CP_DECL_CONTEXT (decl);
5454 else if (CP_DECL_CONTEXT (decl)
5455 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5456 /* In the case of a virtual function, we want the class in which
5457 it is defined. */
5458 ctx = CP_DECL_CONTEXT (decl);
5459 else
5460 /* Otherwise, if we're currently defining some class, the DECL
5461 is assumed to be a member of the class. */
5462 ctx = current_scope ();
5463
5464 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5465 ctx = NULL_TREE;
5466
5467 if (!DECL_CONTEXT (decl))
5468 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5469
5470 /* See if this is a primary template. */
5471 if (is_friend && ctx
5472 && uses_template_parms_level (ctx, processing_template_decl))
5473 /* A friend template that specifies a class context, i.e.
5474 template <typename T> friend void A<T>::f();
5475 is not primary. */
5476 is_primary = false;
5477 else if (TREE_CODE (decl) == TYPE_DECL
5478 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5479 is_primary = false;
5480 else
5481 is_primary = template_parm_scope_p ();
5482
5483 if (is_primary)
5484 {
5485 warning (OPT_Wtemplates, "template %qD declared", decl);
5486
5487 if (DECL_CLASS_SCOPE_P (decl))
5488 member_template_p = true;
5489 if (TREE_CODE (decl) == TYPE_DECL
5490 && anon_aggrname_p (DECL_NAME (decl)))
5491 {
5492 error ("template class without a name");
5493 return error_mark_node;
5494 }
5495 else if (TREE_CODE (decl) == FUNCTION_DECL)
5496 {
5497 if (member_template_p)
5498 {
5499 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5500 error ("member template %qD may not have virt-specifiers", decl);
5501 }
5502 if (DECL_DESTRUCTOR_P (decl))
5503 {
5504 /* [temp.mem]
5505
5506 A destructor shall not be a member template. */
5507 error ("destructor %qD declared as member template", decl);
5508 return error_mark_node;
5509 }
5510 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5511 && (!prototype_p (TREE_TYPE (decl))
5512 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5513 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5514 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5515 == void_list_node)))
5516 {
5517 /* [basic.stc.dynamic.allocation]
5518
5519 An allocation function can be a function
5520 template. ... Template allocation functions shall
5521 have two or more parameters. */
5522 error ("invalid template declaration of %qD", decl);
5523 return error_mark_node;
5524 }
5525 }
5526 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5527 && CLASS_TYPE_P (TREE_TYPE (decl)))
5528 {
5529 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5530 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5531 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5532 {
5533 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5534 if (TREE_CODE (t) == TYPE_DECL)
5535 t = TREE_TYPE (t);
5536 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5537 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5538 }
5539 }
5540 else if (TREE_CODE (decl) == TYPE_DECL
5541 && TYPE_DECL_ALIAS_P (decl))
5542 /* alias-declaration */
5543 gcc_assert (!DECL_ARTIFICIAL (decl));
5544 else if (VAR_P (decl))
5545 /* C++14 variable template. */;
5546 else
5547 {
5548 error ("template declaration of %q#D", decl);
5549 return error_mark_node;
5550 }
5551 }
5552
5553 /* Check to see that the rules regarding the use of default
5554 arguments are not being violated. We check args for a friend
5555 functions when we know whether it's a definition, introducing
5556 declaration or re-declaration. */
5557 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5558 check_default_tmpl_args (decl, current_template_parms,
5559 is_primary, is_partial, is_friend);
5560
5561 /* Ensure that there are no parameter packs in the type of this
5562 declaration that have not been expanded. */
5563 if (TREE_CODE (decl) == FUNCTION_DECL)
5564 {
5565 /* Check each of the arguments individually to see if there are
5566 any bare parameter packs. */
5567 tree type = TREE_TYPE (decl);
5568 tree arg = DECL_ARGUMENTS (decl);
5569 tree argtype = TYPE_ARG_TYPES (type);
5570
5571 while (arg && argtype)
5572 {
5573 if (!DECL_PACK_P (arg)
5574 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5575 {
5576 /* This is a PARM_DECL that contains unexpanded parameter
5577 packs. We have already complained about this in the
5578 check_for_bare_parameter_packs call, so just replace
5579 these types with ERROR_MARK_NODE. */
5580 TREE_TYPE (arg) = error_mark_node;
5581 TREE_VALUE (argtype) = error_mark_node;
5582 }
5583
5584 arg = DECL_CHAIN (arg);
5585 argtype = TREE_CHAIN (argtype);
5586 }
5587
5588 /* Check for bare parameter packs in the return type and the
5589 exception specifiers. */
5590 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5591 /* Errors were already issued, set return type to int
5592 as the frontend doesn't expect error_mark_node as
5593 the return type. */
5594 TREE_TYPE (type) = integer_type_node;
5595 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5596 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5597 }
5598 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5599 && TYPE_DECL_ALIAS_P (decl))
5600 ? DECL_ORIGINAL_TYPE (decl)
5601 : TREE_TYPE (decl)))
5602 {
5603 TREE_TYPE (decl) = error_mark_node;
5604 return error_mark_node;
5605 }
5606
5607 if (is_partial)
5608 return process_partial_specialization (decl);
5609
5610 args = current_template_args ();
5611
5612 if (!ctx
5613 || TREE_CODE (ctx) == FUNCTION_DECL
5614 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5615 || (TREE_CODE (decl) == TYPE_DECL
5616 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5617 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5618 {
5619 if (DECL_LANG_SPECIFIC (decl)
5620 && DECL_TEMPLATE_INFO (decl)
5621 && DECL_TI_TEMPLATE (decl))
5622 tmpl = DECL_TI_TEMPLATE (decl);
5623 /* If DECL is a TYPE_DECL for a class-template, then there won't
5624 be DECL_LANG_SPECIFIC. The information equivalent to
5625 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5626 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5627 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5628 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5629 {
5630 /* Since a template declaration already existed for this
5631 class-type, we must be redeclaring it here. Make sure
5632 that the redeclaration is valid. */
5633 redeclare_class_template (TREE_TYPE (decl),
5634 current_template_parms,
5635 current_template_constraints ());
5636 /* We don't need to create a new TEMPLATE_DECL; just use the
5637 one we already had. */
5638 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5639 }
5640 else
5641 {
5642 tmpl = build_template_decl (decl, current_template_parms,
5643 member_template_p);
5644 new_template_p = 1;
5645
5646 if (DECL_LANG_SPECIFIC (decl)
5647 && DECL_TEMPLATE_SPECIALIZATION (decl))
5648 {
5649 /* A specialization of a member template of a template
5650 class. */
5651 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5652 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5653 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5654 }
5655 }
5656 }
5657 else
5658 {
5659 tree a, t, current, parms;
5660 int i;
5661 tree tinfo = get_template_info (decl);
5662
5663 if (!tinfo)
5664 {
5665 error ("template definition of non-template %q#D", decl);
5666 return error_mark_node;
5667 }
5668
5669 tmpl = TI_TEMPLATE (tinfo);
5670
5671 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5672 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5673 && DECL_TEMPLATE_SPECIALIZATION (decl)
5674 && DECL_MEMBER_TEMPLATE_P (tmpl))
5675 {
5676 tree new_tmpl;
5677
5678 /* The declaration is a specialization of a member
5679 template, declared outside the class. Therefore, the
5680 innermost template arguments will be NULL, so we
5681 replace them with the arguments determined by the
5682 earlier call to check_explicit_specialization. */
5683 args = DECL_TI_ARGS (decl);
5684
5685 new_tmpl
5686 = build_template_decl (decl, current_template_parms,
5687 member_template_p);
5688 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5689 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5690 DECL_TI_TEMPLATE (decl) = new_tmpl;
5691 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5692 DECL_TEMPLATE_INFO (new_tmpl)
5693 = build_template_info (tmpl, args);
5694
5695 register_specialization (new_tmpl,
5696 most_general_template (tmpl),
5697 args,
5698 is_friend, 0);
5699 return decl;
5700 }
5701
5702 /* Make sure the template headers we got make sense. */
5703
5704 parms = DECL_TEMPLATE_PARMS (tmpl);
5705 i = TMPL_PARMS_DEPTH (parms);
5706 if (TMPL_ARGS_DEPTH (args) != i)
5707 {
5708 error ("expected %d levels of template parms for %q#D, got %d",
5709 i, decl, TMPL_ARGS_DEPTH (args));
5710 DECL_INTERFACE_KNOWN (decl) = 1;
5711 return error_mark_node;
5712 }
5713 else
5714 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5715 {
5716 a = TMPL_ARGS_LEVEL (args, i);
5717 t = INNERMOST_TEMPLATE_PARMS (parms);
5718
5719 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5720 {
5721 if (current == decl)
5722 error ("got %d template parameters for %q#D",
5723 TREE_VEC_LENGTH (a), decl);
5724 else
5725 error ("got %d template parameters for %q#T",
5726 TREE_VEC_LENGTH (a), current);
5727 error (" but %d required", TREE_VEC_LENGTH (t));
5728 /* Avoid crash in import_export_decl. */
5729 DECL_INTERFACE_KNOWN (decl) = 1;
5730 return error_mark_node;
5731 }
5732
5733 if (current == decl)
5734 current = ctx;
5735 else if (current == NULL_TREE)
5736 /* Can happen in erroneous input. */
5737 break;
5738 else
5739 current = get_containing_scope (current);
5740 }
5741
5742 /* Check that the parms are used in the appropriate qualifying scopes
5743 in the declarator. */
5744 if (!comp_template_args
5745 (TI_ARGS (tinfo),
5746 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5747 {
5748 error ("template arguments to %qD do not match original "
5749 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5750 if (!uses_template_parms (TI_ARGS (tinfo)))
5751 inform (input_location, "use %<template<>%> for"
5752 " an explicit specialization");
5753 /* Avoid crash in import_export_decl. */
5754 DECL_INTERFACE_KNOWN (decl) = 1;
5755 return error_mark_node;
5756 }
5757 }
5758
5759 DECL_TEMPLATE_RESULT (tmpl) = decl;
5760 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5761
5762 /* Push template declarations for global functions and types. Note
5763 that we do not try to push a global template friend declared in a
5764 template class; such a thing may well depend on the template
5765 parameters of the class. */
5766 if (new_template_p && !ctx
5767 && !(is_friend && template_class_depth (current_class_type) > 0))
5768 {
5769 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5770 if (tmpl == error_mark_node)
5771 return error_mark_node;
5772
5773 /* Hide template friend classes that haven't been declared yet. */
5774 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5775 {
5776 DECL_ANTICIPATED (tmpl) = 1;
5777 DECL_FRIEND_P (tmpl) = 1;
5778 }
5779 }
5780
5781 if (is_primary)
5782 {
5783 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5784
5785 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5786
5787 /* Give template template parms a DECL_CONTEXT of the template
5788 for which they are a parameter. */
5789 parms = INNERMOST_TEMPLATE_PARMS (parms);
5790 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5791 {
5792 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5793 if (TREE_CODE (parm) == TEMPLATE_DECL)
5794 DECL_CONTEXT (parm) = tmpl;
5795 }
5796
5797 if (TREE_CODE (decl) == TYPE_DECL
5798 && TYPE_DECL_ALIAS_P (decl)
5799 && complex_alias_template_p (tmpl))
5800 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5801 }
5802
5803 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5804 back to its most general template. If TMPL is a specialization,
5805 ARGS may only have the innermost set of arguments. Add the missing
5806 argument levels if necessary. */
5807 if (DECL_TEMPLATE_INFO (tmpl))
5808 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5809
5810 info = build_template_info (tmpl, args);
5811
5812 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5813 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5814 else
5815 {
5816 if (is_primary)
5817 retrofit_lang_decl (decl);
5818 if (DECL_LANG_SPECIFIC (decl))
5819 DECL_TEMPLATE_INFO (decl) = info;
5820 }
5821
5822 if (flag_implicit_templates
5823 && !is_friend
5824 && TREE_PUBLIC (decl)
5825 && VAR_OR_FUNCTION_DECL_P (decl))
5826 /* Set DECL_COMDAT on template instantiations; if we force
5827 them to be emitted by explicit instantiation or -frepo,
5828 mark_needed will tell cgraph to do the right thing. */
5829 DECL_COMDAT (decl) = true;
5830
5831 return DECL_TEMPLATE_RESULT (tmpl);
5832 }
5833
5834 tree
5835 push_template_decl (tree decl)
5836 {
5837 return push_template_decl_real (decl, false);
5838 }
5839
5840 /* FN is an inheriting constructor that inherits from the constructor
5841 template INHERITED; turn FN into a constructor template with a matching
5842 template header. */
5843
5844 tree
5845 add_inherited_template_parms (tree fn, tree inherited)
5846 {
5847 tree inner_parms
5848 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5849 inner_parms = copy_node (inner_parms);
5850 tree parms
5851 = tree_cons (size_int (processing_template_decl + 1),
5852 inner_parms, current_template_parms);
5853 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5854 tree args = template_parms_to_args (parms);
5855 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5856 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5857 DECL_TEMPLATE_RESULT (tmpl) = fn;
5858 DECL_ARTIFICIAL (tmpl) = true;
5859 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5860 return tmpl;
5861 }
5862
5863 /* Called when a class template TYPE is redeclared with the indicated
5864 template PARMS, e.g.:
5865
5866 template <class T> struct S;
5867 template <class T> struct S {}; */
5868
5869 bool
5870 redeclare_class_template (tree type, tree parms, tree cons)
5871 {
5872 tree tmpl;
5873 tree tmpl_parms;
5874 int i;
5875
5876 if (!TYPE_TEMPLATE_INFO (type))
5877 {
5878 error ("%qT is not a template type", type);
5879 return false;
5880 }
5881
5882 tmpl = TYPE_TI_TEMPLATE (type);
5883 if (!PRIMARY_TEMPLATE_P (tmpl))
5884 /* The type is nested in some template class. Nothing to worry
5885 about here; there are no new template parameters for the nested
5886 type. */
5887 return true;
5888
5889 if (!parms)
5890 {
5891 error ("template specifiers not specified in declaration of %qD",
5892 tmpl);
5893 return false;
5894 }
5895
5896 parms = INNERMOST_TEMPLATE_PARMS (parms);
5897 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5898
5899 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5900 {
5901 error_n (input_location, TREE_VEC_LENGTH (parms),
5902 "redeclared with %d template parameter",
5903 "redeclared with %d template parameters",
5904 TREE_VEC_LENGTH (parms));
5905 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5906 "previous declaration %qD used %d template parameter",
5907 "previous declaration %qD used %d template parameters",
5908 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5909 return false;
5910 }
5911
5912 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5913 {
5914 tree tmpl_parm;
5915 tree parm;
5916 tree tmpl_default;
5917 tree parm_default;
5918
5919 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5920 || TREE_VEC_ELT (parms, i) == error_mark_node)
5921 continue;
5922
5923 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5924 if (error_operand_p (tmpl_parm))
5925 return false;
5926
5927 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5928 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5929 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5930
5931 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5932 TEMPLATE_DECL. */
5933 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5934 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5935 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5936 || (TREE_CODE (tmpl_parm) != PARM_DECL
5937 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5938 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5939 || (TREE_CODE (tmpl_parm) == PARM_DECL
5940 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5941 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5942 {
5943 error ("template parameter %q+#D", tmpl_parm);
5944 error ("redeclared here as %q#D", parm);
5945 return false;
5946 }
5947
5948 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5949 {
5950 /* We have in [temp.param]:
5951
5952 A template-parameter may not be given default arguments
5953 by two different declarations in the same scope. */
5954 error_at (input_location, "redefinition of default argument for %q#D", parm);
5955 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5956 "original definition appeared here");
5957 return false;
5958 }
5959
5960 if (parm_default != NULL_TREE)
5961 /* Update the previous template parameters (which are the ones
5962 that will really count) with the new default value. */
5963 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5964 else if (tmpl_default != NULL_TREE)
5965 /* Update the new parameters, too; they'll be used as the
5966 parameters for any members. */
5967 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5968
5969 /* Give each template template parm in this redeclaration a
5970 DECL_CONTEXT of the template for which they are a parameter. */
5971 if (TREE_CODE (parm) == TEMPLATE_DECL)
5972 {
5973 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5974 DECL_CONTEXT (parm) = tmpl;
5975 }
5976
5977 if (TREE_CODE (parm) == TYPE_DECL)
5978 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5979 }
5980
5981 // Cannot redeclare a class template with a different set of constraints.
5982 if (!equivalent_constraints (get_constraints (tmpl), cons))
5983 {
5984 error_at (input_location, "redeclaration %q#D with different "
5985 "constraints", tmpl);
5986 inform (DECL_SOURCE_LOCATION (tmpl),
5987 "original declaration appeared here");
5988 }
5989
5990 return true;
5991 }
5992
5993 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5994 to be used when the caller has already checked
5995 (processing_template_decl
5996 && !instantiation_dependent_expression_p (expr)
5997 && potential_constant_expression (expr))
5998 and cleared processing_template_decl. */
5999
6000 tree
6001 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6002 {
6003 return tsubst_copy_and_build (expr,
6004 /*args=*/NULL_TREE,
6005 complain,
6006 /*in_decl=*/NULL_TREE,
6007 /*function_p=*/false,
6008 /*integral_constant_expression_p=*/true);
6009 }
6010
6011 /* Simplify EXPR if it is a non-dependent expression. Returns the
6012 (possibly simplified) expression. */
6013
6014 tree
6015 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6016 {
6017 if (expr == NULL_TREE)
6018 return NULL_TREE;
6019
6020 /* If we're in a template, but EXPR isn't value dependent, simplify
6021 it. We're supposed to treat:
6022
6023 template <typename T> void f(T[1 + 1]);
6024 template <typename T> void f(T[2]);
6025
6026 as two declarations of the same function, for example. */
6027 if (processing_template_decl
6028 && is_nondependent_constant_expression (expr))
6029 {
6030 processing_template_decl_sentinel s;
6031 expr = instantiate_non_dependent_expr_internal (expr, complain);
6032 }
6033 return expr;
6034 }
6035
6036 tree
6037 instantiate_non_dependent_expr (tree expr)
6038 {
6039 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6040 }
6041
6042 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6043 an uninstantiated expression. */
6044
6045 tree
6046 instantiate_non_dependent_or_null (tree expr)
6047 {
6048 if (expr == NULL_TREE)
6049 return NULL_TREE;
6050 if (processing_template_decl)
6051 {
6052 if (!is_nondependent_constant_expression (expr))
6053 expr = NULL_TREE;
6054 else
6055 {
6056 processing_template_decl_sentinel s;
6057 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6058 }
6059 }
6060 return expr;
6061 }
6062
6063 /* True iff T is a specialization of a variable template. */
6064
6065 bool
6066 variable_template_specialization_p (tree t)
6067 {
6068 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6069 return false;
6070 tree tmpl = DECL_TI_TEMPLATE (t);
6071 return variable_template_p (tmpl);
6072 }
6073
6074 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6075 template declaration, or a TYPE_DECL for an alias declaration. */
6076
6077 bool
6078 alias_type_or_template_p (tree t)
6079 {
6080 if (t == NULL_TREE)
6081 return false;
6082 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6083 || (TYPE_P (t)
6084 && TYPE_NAME (t)
6085 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6086 || DECL_ALIAS_TEMPLATE_P (t));
6087 }
6088
6089 /* Return TRUE iff T is a specialization of an alias template. */
6090
6091 bool
6092 alias_template_specialization_p (const_tree t)
6093 {
6094 /* It's an alias template specialization if it's an alias and its
6095 TYPE_NAME is a specialization of a primary template. */
6096 if (TYPE_ALIAS_P (t))
6097 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6098 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6099
6100 return false;
6101 }
6102
6103 /* An alias template is complex from a SFINAE perspective if a template-id
6104 using that alias can be ill-formed when the expansion is not, as with
6105 the void_t template. We determine this by checking whether the
6106 expansion for the alias template uses all its template parameters. */
6107
6108 struct uses_all_template_parms_data
6109 {
6110 int level;
6111 bool *seen;
6112 };
6113
6114 static int
6115 uses_all_template_parms_r (tree t, void *data_)
6116 {
6117 struct uses_all_template_parms_data &data
6118 = *(struct uses_all_template_parms_data*)data_;
6119 tree idx = get_template_parm_index (t);
6120
6121 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6122 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6123 return 0;
6124 }
6125
6126 static bool
6127 complex_alias_template_p (const_tree tmpl)
6128 {
6129 struct uses_all_template_parms_data data;
6130 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6131 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6132 data.level = TMPL_PARMS_DEPTH (parms);
6133 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6134 data.seen = XALLOCAVEC (bool, len);
6135 for (int i = 0; i < len; ++i)
6136 data.seen[i] = false;
6137
6138 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6139 for (int i = 0; i < len; ++i)
6140 if (!data.seen[i])
6141 return true;
6142 return false;
6143 }
6144
6145 /* Return TRUE iff T is a specialization of a complex alias template with
6146 dependent template-arguments. */
6147
6148 bool
6149 dependent_alias_template_spec_p (const_tree t)
6150 {
6151 if (!alias_template_specialization_p (t))
6152 return false;
6153
6154 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6155 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6156 return false;
6157
6158 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6159 if (!any_dependent_template_arguments_p (args))
6160 return false;
6161
6162 return true;
6163 }
6164
6165 /* Return the number of innermost template parameters in TMPL. */
6166
6167 static int
6168 num_innermost_template_parms (tree tmpl)
6169 {
6170 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6171 return TREE_VEC_LENGTH (parms);
6172 }
6173
6174 /* Return either TMPL or another template that it is equivalent to under DR
6175 1286: An alias that just changes the name of a template is equivalent to
6176 the other template. */
6177
6178 static tree
6179 get_underlying_template (tree tmpl)
6180 {
6181 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6182 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6183 {
6184 /* Determine if the alias is equivalent to an underlying template. */
6185 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6186 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6187 if (!tinfo)
6188 break;
6189
6190 tree underlying = TI_TEMPLATE (tinfo);
6191 if (!PRIMARY_TEMPLATE_P (underlying)
6192 || (num_innermost_template_parms (tmpl)
6193 != num_innermost_template_parms (underlying)))
6194 break;
6195
6196 tree alias_args = INNERMOST_TEMPLATE_ARGS
6197 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6198 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6199 break;
6200
6201 /* Alias is equivalent. Strip it and repeat. */
6202 tmpl = underlying;
6203 }
6204
6205 return tmpl;
6206 }
6207
6208 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6209 must be a reference-to-function or a pointer-to-function type, as specified
6210 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6211 and check that the resulting function has external linkage. */
6212
6213 static tree
6214 convert_nontype_argument_function (tree type, tree expr,
6215 tsubst_flags_t complain)
6216 {
6217 tree fns = expr;
6218 tree fn, fn_no_ptr;
6219 linkage_kind linkage;
6220
6221 fn = instantiate_type (type, fns, tf_none);
6222 if (fn == error_mark_node)
6223 return error_mark_node;
6224
6225 if (value_dependent_expression_p (fn))
6226 goto accept;
6227
6228 fn_no_ptr = strip_fnptr_conv (fn);
6229 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6230 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6231 if (BASELINK_P (fn_no_ptr))
6232 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6233
6234 /* [temp.arg.nontype]/1
6235
6236 A template-argument for a non-type, non-template template-parameter
6237 shall be one of:
6238 [...]
6239 -- the address of an object or function with external [C++11: or
6240 internal] linkage. */
6241
6242 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6243 {
6244 if (complain & tf_error)
6245 {
6246 error ("%qE is not a valid template argument for type %qT",
6247 expr, type);
6248 if (TYPE_PTR_P (type))
6249 inform (input_location, "it must be the address of a function "
6250 "with external linkage");
6251 else
6252 inform (input_location, "it must be the name of a function with "
6253 "external linkage");
6254 }
6255 return NULL_TREE;
6256 }
6257
6258 linkage = decl_linkage (fn_no_ptr);
6259 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6260 {
6261 if (complain & tf_error)
6262 {
6263 if (cxx_dialect >= cxx11)
6264 error ("%qE is not a valid template argument for type %qT "
6265 "because %qD has no linkage",
6266 expr, type, fn_no_ptr);
6267 else
6268 error ("%qE is not a valid template argument for type %qT "
6269 "because %qD does not have external linkage",
6270 expr, type, fn_no_ptr);
6271 }
6272 return NULL_TREE;
6273 }
6274
6275 accept:
6276 if (TYPE_REF_P (type))
6277 {
6278 if (REFERENCE_REF_P (fn))
6279 fn = TREE_OPERAND (fn, 0);
6280 else
6281 fn = build_address (fn);
6282 }
6283 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6284 fn = build_nop (type, fn);
6285
6286 return fn;
6287 }
6288
6289 /* Subroutine of convert_nontype_argument.
6290 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6291 Emit an error otherwise. */
6292
6293 static bool
6294 check_valid_ptrmem_cst_expr (tree type, tree expr,
6295 tsubst_flags_t complain)
6296 {
6297 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6298 tree orig_expr = expr;
6299 STRIP_NOPS (expr);
6300 if (null_ptr_cst_p (expr))
6301 return true;
6302 if (TREE_CODE (expr) == PTRMEM_CST
6303 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6304 PTRMEM_CST_CLASS (expr)))
6305 return true;
6306 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6307 return true;
6308 if (processing_template_decl
6309 && TREE_CODE (expr) == ADDR_EXPR
6310 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6311 return true;
6312 if (complain & tf_error)
6313 {
6314 error_at (loc, "%qE is not a valid template argument for type %qT",
6315 orig_expr, type);
6316 if (TREE_CODE (expr) != PTRMEM_CST)
6317 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6318 else
6319 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6320 }
6321 return false;
6322 }
6323
6324 /* Returns TRUE iff the address of OP is value-dependent.
6325
6326 14.6.2.4 [temp.dep.temp]:
6327 A non-integral non-type template-argument is dependent if its type is
6328 dependent or it has either of the following forms
6329 qualified-id
6330 & qualified-id
6331 and contains a nested-name-specifier which specifies a class-name that
6332 names a dependent type.
6333
6334 We generalize this to just say that the address of a member of a
6335 dependent class is value-dependent; the above doesn't cover the
6336 address of a static data member named with an unqualified-id. */
6337
6338 static bool
6339 has_value_dependent_address (tree op)
6340 {
6341 /* We could use get_inner_reference here, but there's no need;
6342 this is only relevant for template non-type arguments, which
6343 can only be expressed as &id-expression. */
6344 if (DECL_P (op))
6345 {
6346 tree ctx = CP_DECL_CONTEXT (op);
6347 if (TYPE_P (ctx) && dependent_type_p (ctx))
6348 return true;
6349 }
6350
6351 return false;
6352 }
6353
6354 /* The next set of functions are used for providing helpful explanatory
6355 diagnostics for failed overload resolution. Their messages should be
6356 indented by two spaces for consistency with the messages in
6357 call.c */
6358
6359 static int
6360 unify_success (bool /*explain_p*/)
6361 {
6362 return 0;
6363 }
6364
6365 /* Other failure functions should call this one, to provide a single function
6366 for setting a breakpoint on. */
6367
6368 static int
6369 unify_invalid (bool /*explain_p*/)
6370 {
6371 return 1;
6372 }
6373
6374 static int
6375 unify_parameter_deduction_failure (bool explain_p, tree parm)
6376 {
6377 if (explain_p)
6378 inform (input_location,
6379 " couldn't deduce template parameter %qD", parm);
6380 return unify_invalid (explain_p);
6381 }
6382
6383 static int
6384 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6385 {
6386 if (explain_p)
6387 inform (input_location,
6388 " types %qT and %qT have incompatible cv-qualifiers",
6389 parm, arg);
6390 return unify_invalid (explain_p);
6391 }
6392
6393 static int
6394 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6395 {
6396 if (explain_p)
6397 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6398 return unify_invalid (explain_p);
6399 }
6400
6401 static int
6402 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6403 {
6404 if (explain_p)
6405 inform (input_location,
6406 " template parameter %qD is not a parameter pack, but "
6407 "argument %qD is",
6408 parm, arg);
6409 return unify_invalid (explain_p);
6410 }
6411
6412 static int
6413 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6414 {
6415 if (explain_p)
6416 inform (input_location,
6417 " template argument %qE does not match "
6418 "pointer-to-member constant %qE",
6419 arg, parm);
6420 return unify_invalid (explain_p);
6421 }
6422
6423 static int
6424 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6425 {
6426 if (explain_p)
6427 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6428 return unify_invalid (explain_p);
6429 }
6430
6431 static int
6432 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6433 {
6434 if (explain_p)
6435 inform (input_location,
6436 " inconsistent parameter pack deduction with %qT and %qT",
6437 old_arg, new_arg);
6438 return unify_invalid (explain_p);
6439 }
6440
6441 static int
6442 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6443 {
6444 if (explain_p)
6445 {
6446 if (TYPE_P (parm))
6447 inform (input_location,
6448 " deduced conflicting types for parameter %qT (%qT and %qT)",
6449 parm, first, second);
6450 else
6451 inform (input_location,
6452 " deduced conflicting values for non-type parameter "
6453 "%qE (%qE and %qE)", parm, first, second);
6454 }
6455 return unify_invalid (explain_p);
6456 }
6457
6458 static int
6459 unify_vla_arg (bool explain_p, tree arg)
6460 {
6461 if (explain_p)
6462 inform (input_location,
6463 " variable-sized array type %qT is not "
6464 "a valid template argument",
6465 arg);
6466 return unify_invalid (explain_p);
6467 }
6468
6469 static int
6470 unify_method_type_error (bool explain_p, tree arg)
6471 {
6472 if (explain_p)
6473 inform (input_location,
6474 " member function type %qT is not a valid template argument",
6475 arg);
6476 return unify_invalid (explain_p);
6477 }
6478
6479 static int
6480 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6481 {
6482 if (explain_p)
6483 {
6484 if (least_p)
6485 inform_n (input_location, wanted,
6486 " candidate expects at least %d argument, %d provided",
6487 " candidate expects at least %d arguments, %d provided",
6488 wanted, have);
6489 else
6490 inform_n (input_location, wanted,
6491 " candidate expects %d argument, %d provided",
6492 " candidate expects %d arguments, %d provided",
6493 wanted, have);
6494 }
6495 return unify_invalid (explain_p);
6496 }
6497
6498 static int
6499 unify_too_many_arguments (bool explain_p, int have, int wanted)
6500 {
6501 return unify_arity (explain_p, have, wanted);
6502 }
6503
6504 static int
6505 unify_too_few_arguments (bool explain_p, int have, int wanted,
6506 bool least_p = false)
6507 {
6508 return unify_arity (explain_p, have, wanted, least_p);
6509 }
6510
6511 static int
6512 unify_arg_conversion (bool explain_p, tree to_type,
6513 tree from_type, tree arg)
6514 {
6515 if (explain_p)
6516 inform (EXPR_LOC_OR_LOC (arg, input_location),
6517 " cannot convert %qE (type %qT) to type %qT",
6518 arg, from_type, to_type);
6519 return unify_invalid (explain_p);
6520 }
6521
6522 static int
6523 unify_no_common_base (bool explain_p, enum template_base_result r,
6524 tree parm, tree arg)
6525 {
6526 if (explain_p)
6527 switch (r)
6528 {
6529 case tbr_ambiguous_baseclass:
6530 inform (input_location, " %qT is an ambiguous base class of %qT",
6531 parm, arg);
6532 break;
6533 default:
6534 inform (input_location, " %qT is not derived from %qT", arg, parm);
6535 break;
6536 }
6537 return unify_invalid (explain_p);
6538 }
6539
6540 static int
6541 unify_inconsistent_template_template_parameters (bool explain_p)
6542 {
6543 if (explain_p)
6544 inform (input_location,
6545 " template parameters of a template template argument are "
6546 "inconsistent with other deduced template arguments");
6547 return unify_invalid (explain_p);
6548 }
6549
6550 static int
6551 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6552 {
6553 if (explain_p)
6554 inform (input_location,
6555 " can't deduce a template for %qT from non-template type %qT",
6556 parm, arg);
6557 return unify_invalid (explain_p);
6558 }
6559
6560 static int
6561 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6562 {
6563 if (explain_p)
6564 inform (input_location,
6565 " template argument %qE does not match %qE", arg, parm);
6566 return unify_invalid (explain_p);
6567 }
6568
6569 /* Attempt to convert the non-type template parameter EXPR to the
6570 indicated TYPE. If the conversion is successful, return the
6571 converted value. If the conversion is unsuccessful, return
6572 NULL_TREE if we issued an error message, or error_mark_node if we
6573 did not. We issue error messages for out-and-out bad template
6574 parameters, but not simply because the conversion failed, since we
6575 might be just trying to do argument deduction. Both TYPE and EXPR
6576 must be non-dependent.
6577
6578 The conversion follows the special rules described in
6579 [temp.arg.nontype], and it is much more strict than an implicit
6580 conversion.
6581
6582 This function is called twice for each template argument (see
6583 lookup_template_class for a more accurate description of this
6584 problem). This means that we need to handle expressions which
6585 are not valid in a C++ source, but can be created from the
6586 first call (for instance, casts to perform conversions). These
6587 hacks can go away after we fix the double coercion problem. */
6588
6589 static tree
6590 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6591 {
6592 tree expr_type;
6593 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6594 tree orig_expr = expr;
6595
6596 /* Detect immediately string literals as invalid non-type argument.
6597 This special-case is not needed for correctness (we would easily
6598 catch this later), but only to provide better diagnostic for this
6599 common user mistake. As suggested by DR 100, we do not mention
6600 linkage issues in the diagnostic as this is not the point. */
6601 /* FIXME we're making this OK. */
6602 if (TREE_CODE (expr) == STRING_CST)
6603 {
6604 if (complain & tf_error)
6605 error ("%qE is not a valid template argument for type %qT "
6606 "because string literals can never be used in this context",
6607 expr, type);
6608 return NULL_TREE;
6609 }
6610
6611 /* Add the ADDR_EXPR now for the benefit of
6612 value_dependent_expression_p. */
6613 if (TYPE_PTROBV_P (type)
6614 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6615 {
6616 expr = decay_conversion (expr, complain);
6617 if (expr == error_mark_node)
6618 return error_mark_node;
6619 }
6620
6621 /* If we are in a template, EXPR may be non-dependent, but still
6622 have a syntactic, rather than semantic, form. For example, EXPR
6623 might be a SCOPE_REF, rather than the VAR_DECL to which the
6624 SCOPE_REF refers. Preserving the qualifying scope is necessary
6625 so that access checking can be performed when the template is
6626 instantiated -- but here we need the resolved form so that we can
6627 convert the argument. */
6628 bool non_dep = false;
6629 if (TYPE_REF_OBJ_P (type)
6630 && has_value_dependent_address (expr))
6631 /* If we want the address and it's value-dependent, don't fold. */;
6632 else if (processing_template_decl
6633 && is_nondependent_constant_expression (expr))
6634 non_dep = true;
6635 if (error_operand_p (expr))
6636 return error_mark_node;
6637 expr_type = TREE_TYPE (expr);
6638
6639 /* If the argument is non-dependent, perform any conversions in
6640 non-dependent context as well. */
6641 processing_template_decl_sentinel s (non_dep);
6642 if (non_dep)
6643 expr = instantiate_non_dependent_expr_internal (expr, complain);
6644
6645 if (value_dependent_expression_p (expr))
6646 expr = canonicalize_expr_argument (expr, complain);
6647
6648 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6649 to a non-type argument of "nullptr". */
6650 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6651 expr = fold_simple (convert (type, expr));
6652
6653 /* In C++11, integral or enumeration non-type template arguments can be
6654 arbitrary constant expressions. Pointer and pointer to
6655 member arguments can be general constant expressions that evaluate
6656 to a null value, but otherwise still need to be of a specific form. */
6657 if (cxx_dialect >= cxx11)
6658 {
6659 if (TREE_CODE (expr) == PTRMEM_CST)
6660 /* A PTRMEM_CST is already constant, and a valid template
6661 argument for a parameter of pointer to member type, we just want
6662 to leave it in that form rather than lower it to a
6663 CONSTRUCTOR. */;
6664 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6665 || cxx_dialect >= cxx17)
6666 {
6667 /* C++17: A template-argument for a non-type template-parameter shall
6668 be a converted constant expression (8.20) of the type of the
6669 template-parameter. */
6670 expr = build_converted_constant_expr (type, expr, complain);
6671 if (expr == error_mark_node)
6672 return error_mark_node;
6673 expr = maybe_constant_value (expr);
6674 expr = convert_from_reference (expr);
6675 }
6676 else if (TYPE_PTR_OR_PTRMEM_P (type))
6677 {
6678 tree folded = maybe_constant_value (expr);
6679 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6680 : null_member_pointer_value_p (folded))
6681 expr = folded;
6682 }
6683 }
6684
6685 if (TYPE_REF_P (type))
6686 expr = mark_lvalue_use (expr);
6687 else
6688 expr = mark_rvalue_use (expr);
6689
6690 /* HACK: Due to double coercion, we can get a
6691 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6692 which is the tree that we built on the first call (see
6693 below when coercing to reference to object or to reference to
6694 function). We just strip everything and get to the arg.
6695 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6696 for examples. */
6697 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6698 {
6699 tree probe_type, probe = expr;
6700 if (REFERENCE_REF_P (probe))
6701 probe = TREE_OPERAND (probe, 0);
6702 probe_type = TREE_TYPE (probe);
6703 if (TREE_CODE (probe) == NOP_EXPR)
6704 {
6705 /* ??? Maybe we could use convert_from_reference here, but we
6706 would need to relax its constraints because the NOP_EXPR
6707 could actually change the type to something more cv-qualified,
6708 and this is not folded by convert_from_reference. */
6709 tree addr = TREE_OPERAND (probe, 0);
6710 if (TYPE_REF_P (probe_type)
6711 && TREE_CODE (addr) == ADDR_EXPR
6712 && TYPE_PTR_P (TREE_TYPE (addr))
6713 && (same_type_ignoring_top_level_qualifiers_p
6714 (TREE_TYPE (probe_type),
6715 TREE_TYPE (TREE_TYPE (addr)))))
6716 {
6717 expr = TREE_OPERAND (addr, 0);
6718 expr_type = TREE_TYPE (probe_type);
6719 }
6720 }
6721 }
6722
6723 /* [temp.arg.nontype]/5, bullet 1
6724
6725 For a non-type template-parameter of integral or enumeration type,
6726 integral promotions (_conv.prom_) and integral conversions
6727 (_conv.integral_) are applied. */
6728 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6729 {
6730 if (cxx_dialect < cxx11)
6731 {
6732 tree t = build_converted_constant_expr (type, expr, complain);
6733 t = maybe_constant_value (t);
6734 if (t != error_mark_node)
6735 expr = t;
6736 }
6737
6738 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6739 return error_mark_node;
6740
6741 /* Notice that there are constant expressions like '4 % 0' which
6742 do not fold into integer constants. */
6743 if (TREE_CODE (expr) != INTEGER_CST
6744 && !value_dependent_expression_p (expr))
6745 {
6746 if (complain & tf_error)
6747 {
6748 int errs = errorcount, warns = warningcount + werrorcount;
6749 if (!require_potential_constant_expression (expr))
6750 expr = error_mark_node;
6751 else
6752 expr = cxx_constant_value (expr);
6753 if (errorcount > errs || warningcount + werrorcount > warns)
6754 inform (loc, "in template argument for type %qT", type);
6755 if (expr == error_mark_node)
6756 return NULL_TREE;
6757 /* else cxx_constant_value complained but gave us
6758 a real constant, so go ahead. */
6759 if (TREE_CODE (expr) != INTEGER_CST)
6760 {
6761 /* Some assemble time constant expressions like
6762 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6763 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6764 as we can emit them into .rodata initializers of
6765 variables, yet they can't fold into an INTEGER_CST at
6766 compile time. Refuse them here. */
6767 gcc_checking_assert (reduced_constant_expression_p (expr));
6768 error_at (loc, "template argument %qE for type %qT not "
6769 "a constant integer", expr, type);
6770 return NULL_TREE;
6771 }
6772 }
6773 else
6774 return NULL_TREE;
6775 }
6776
6777 /* Avoid typedef problems. */
6778 if (TREE_TYPE (expr) != type)
6779 expr = fold_convert (type, expr);
6780 }
6781 /* [temp.arg.nontype]/5, bullet 2
6782
6783 For a non-type template-parameter of type pointer to object,
6784 qualification conversions (_conv.qual_) and the array-to-pointer
6785 conversion (_conv.array_) are applied. */
6786 else if (TYPE_PTROBV_P (type))
6787 {
6788 tree decayed = expr;
6789
6790 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6791 decay_conversion or an explicit cast. If it's a problematic cast,
6792 we'll complain about it below. */
6793 if (TREE_CODE (expr) == NOP_EXPR)
6794 {
6795 tree probe = expr;
6796 STRIP_NOPS (probe);
6797 if (TREE_CODE (probe) == ADDR_EXPR
6798 && TYPE_PTR_P (TREE_TYPE (probe)))
6799 {
6800 expr = probe;
6801 expr_type = TREE_TYPE (expr);
6802 }
6803 }
6804
6805 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6806
6807 A template-argument for a non-type, non-template template-parameter
6808 shall be one of: [...]
6809
6810 -- the name of a non-type template-parameter;
6811 -- the address of an object or function with external linkage, [...]
6812 expressed as "& id-expression" where the & is optional if the name
6813 refers to a function or array, or if the corresponding
6814 template-parameter is a reference.
6815
6816 Here, we do not care about functions, as they are invalid anyway
6817 for a parameter of type pointer-to-object. */
6818
6819 if (value_dependent_expression_p (expr))
6820 /* Non-type template parameters are OK. */
6821 ;
6822 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6823 /* Null pointer values are OK in C++11. */;
6824 else if (TREE_CODE (expr) != ADDR_EXPR)
6825 {
6826 if (VAR_P (expr))
6827 {
6828 if (complain & tf_error)
6829 error ("%qD is not a valid template argument "
6830 "because %qD is a variable, not the address of "
6831 "a variable", orig_expr, expr);
6832 return NULL_TREE;
6833 }
6834 if (POINTER_TYPE_P (expr_type))
6835 {
6836 if (complain & tf_error)
6837 error ("%qE is not a valid template argument for %qT "
6838 "because it is not the address of a variable",
6839 orig_expr, type);
6840 return NULL_TREE;
6841 }
6842 /* Other values, like integer constants, might be valid
6843 non-type arguments of some other type. */
6844 return error_mark_node;
6845 }
6846 else
6847 {
6848 tree decl = TREE_OPERAND (expr, 0);
6849
6850 if (!VAR_P (decl))
6851 {
6852 if (complain & tf_error)
6853 error ("%qE is not a valid template argument of type %qT "
6854 "because %qE is not a variable", orig_expr, type, decl);
6855 return NULL_TREE;
6856 }
6857 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6858 {
6859 if (complain & tf_error)
6860 error ("%qE is not a valid template argument of type %qT "
6861 "because %qD does not have external linkage",
6862 orig_expr, type, decl);
6863 return NULL_TREE;
6864 }
6865 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6866 && decl_linkage (decl) == lk_none)
6867 {
6868 if (complain & tf_error)
6869 error ("%qE is not a valid template argument of type %qT "
6870 "because %qD has no linkage", orig_expr, type, decl);
6871 return NULL_TREE;
6872 }
6873 /* C++17: For a non-type template-parameter of reference or pointer
6874 type, the value of the constant expression shall not refer to (or
6875 for a pointer type, shall not be the address of):
6876 * a subobject (4.5),
6877 * a temporary object (15.2),
6878 * a string literal (5.13.5),
6879 * the result of a typeid expression (8.2.8), or
6880 * a predefined __func__ variable (11.4.1). */
6881 else if (DECL_ARTIFICIAL (decl))
6882 {
6883 if (complain & tf_error)
6884 error ("the address of %qD is not a valid template argument",
6885 decl);
6886 return NULL_TREE;
6887 }
6888 else if (!same_type_ignoring_top_level_qualifiers_p
6889 (strip_array_types (TREE_TYPE (type)),
6890 strip_array_types (TREE_TYPE (decl))))
6891 {
6892 if (complain & tf_error)
6893 error ("the address of the %qT subobject of %qD is not a "
6894 "valid template argument", TREE_TYPE (type), decl);
6895 return NULL_TREE;
6896 }
6897 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6898 {
6899 if (complain & tf_error)
6900 error ("the address of %qD is not a valid template argument "
6901 "because it does not have static storage duration",
6902 decl);
6903 return NULL_TREE;
6904 }
6905 }
6906
6907 expr = decayed;
6908
6909 expr = perform_qualification_conversions (type, expr);
6910 if (expr == error_mark_node)
6911 return error_mark_node;
6912 }
6913 /* [temp.arg.nontype]/5, bullet 3
6914
6915 For a non-type template-parameter of type reference to object, no
6916 conversions apply. The type referred to by the reference may be more
6917 cv-qualified than the (otherwise identical) type of the
6918 template-argument. The template-parameter is bound directly to the
6919 template-argument, which must be an lvalue. */
6920 else if (TYPE_REF_OBJ_P (type))
6921 {
6922 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6923 expr_type))
6924 return error_mark_node;
6925
6926 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6927 {
6928 if (complain & tf_error)
6929 error ("%qE is not a valid template argument for type %qT "
6930 "because of conflicts in cv-qualification", expr, type);
6931 return NULL_TREE;
6932 }
6933
6934 if (!lvalue_p (expr))
6935 {
6936 if (complain & tf_error)
6937 error ("%qE is not a valid template argument for type %qT "
6938 "because it is not an lvalue", expr, type);
6939 return NULL_TREE;
6940 }
6941
6942 /* [temp.arg.nontype]/1
6943
6944 A template-argument for a non-type, non-template template-parameter
6945 shall be one of: [...]
6946
6947 -- the address of an object or function with external linkage. */
6948 if (INDIRECT_REF_P (expr)
6949 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6950 {
6951 expr = TREE_OPERAND (expr, 0);
6952 if (DECL_P (expr))
6953 {
6954 if (complain & tf_error)
6955 error ("%q#D is not a valid template argument for type %qT "
6956 "because a reference variable does not have a constant "
6957 "address", expr, type);
6958 return NULL_TREE;
6959 }
6960 }
6961
6962 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6963 && value_dependent_expression_p (expr))
6964 /* OK, dependent reference. We don't want to ask whether a DECL is
6965 itself value-dependent, since what we want here is its address. */;
6966 else
6967 {
6968 if (!DECL_P (expr))
6969 {
6970 if (complain & tf_error)
6971 error ("%qE is not a valid template argument for type %qT "
6972 "because it is not an object with linkage",
6973 expr, type);
6974 return NULL_TREE;
6975 }
6976
6977 /* DR 1155 allows internal linkage in C++11 and up. */
6978 linkage_kind linkage = decl_linkage (expr);
6979 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6980 {
6981 if (complain & tf_error)
6982 error ("%qE is not a valid template argument for type %qT "
6983 "because object %qD does not have linkage",
6984 expr, type, expr);
6985 return NULL_TREE;
6986 }
6987
6988 expr = build_address (expr);
6989 }
6990
6991 if (!same_type_p (type, TREE_TYPE (expr)))
6992 expr = build_nop (type, expr);
6993 }
6994 /* [temp.arg.nontype]/5, bullet 4
6995
6996 For a non-type template-parameter of type pointer to function, only
6997 the function-to-pointer conversion (_conv.func_) is applied. If the
6998 template-argument represents a set of overloaded functions (or a
6999 pointer to such), the matching function is selected from the set
7000 (_over.over_). */
7001 else if (TYPE_PTRFN_P (type))
7002 {
7003 /* If the argument is a template-id, we might not have enough
7004 context information to decay the pointer. */
7005 if (!type_unknown_p (expr_type))
7006 {
7007 expr = decay_conversion (expr, complain);
7008 if (expr == error_mark_node)
7009 return error_mark_node;
7010 }
7011
7012 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7013 /* Null pointer values are OK in C++11. */
7014 return perform_qualification_conversions (type, expr);
7015
7016 expr = convert_nontype_argument_function (type, expr, complain);
7017 if (!expr || expr == error_mark_node)
7018 return expr;
7019 }
7020 /* [temp.arg.nontype]/5, bullet 5
7021
7022 For a non-type template-parameter of type reference to function, no
7023 conversions apply. If the template-argument represents a set of
7024 overloaded functions, the matching function is selected from the set
7025 (_over.over_). */
7026 else if (TYPE_REFFN_P (type))
7027 {
7028 if (TREE_CODE (expr) == ADDR_EXPR)
7029 {
7030 if (complain & tf_error)
7031 {
7032 error ("%qE is not a valid template argument for type %qT "
7033 "because it is a pointer", expr, type);
7034 inform (input_location, "try using %qE instead",
7035 TREE_OPERAND (expr, 0));
7036 }
7037 return NULL_TREE;
7038 }
7039
7040 expr = convert_nontype_argument_function (type, expr, complain);
7041 if (!expr || expr == error_mark_node)
7042 return expr;
7043 }
7044 /* [temp.arg.nontype]/5, bullet 6
7045
7046 For a non-type template-parameter of type pointer to member function,
7047 no conversions apply. If the template-argument represents a set of
7048 overloaded member functions, the matching member function is selected
7049 from the set (_over.over_). */
7050 else if (TYPE_PTRMEMFUNC_P (type))
7051 {
7052 expr = instantiate_type (type, expr, tf_none);
7053 if (expr == error_mark_node)
7054 return error_mark_node;
7055
7056 /* [temp.arg.nontype] bullet 1 says the pointer to member
7057 expression must be a pointer-to-member constant. */
7058 if (!value_dependent_expression_p (expr)
7059 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7060 return NULL_TREE;
7061
7062 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7063 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7064 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7065 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7066 }
7067 /* [temp.arg.nontype]/5, bullet 7
7068
7069 For a non-type template-parameter of type pointer to data member,
7070 qualification conversions (_conv.qual_) are applied. */
7071 else if (TYPE_PTRDATAMEM_P (type))
7072 {
7073 /* [temp.arg.nontype] bullet 1 says the pointer to member
7074 expression must be a pointer-to-member constant. */
7075 if (!value_dependent_expression_p (expr)
7076 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7077 return NULL_TREE;
7078
7079 expr = perform_qualification_conversions (type, expr);
7080 if (expr == error_mark_node)
7081 return expr;
7082 }
7083 else if (NULLPTR_TYPE_P (type))
7084 {
7085 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7086 {
7087 if (complain & tf_error)
7088 error ("%qE is not a valid template argument for type %qT "
7089 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7090 return NULL_TREE;
7091 }
7092 return expr;
7093 }
7094 /* A template non-type parameter must be one of the above. */
7095 else
7096 gcc_unreachable ();
7097
7098 /* Sanity check: did we actually convert the argument to the
7099 right type? */
7100 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7101 (type, TREE_TYPE (expr)));
7102 return convert_from_reference (expr);
7103 }
7104
7105 /* Subroutine of coerce_template_template_parms, which returns 1 if
7106 PARM_PARM and ARG_PARM match using the rule for the template
7107 parameters of template template parameters. Both PARM and ARG are
7108 template parameters; the rest of the arguments are the same as for
7109 coerce_template_template_parms.
7110 */
7111 static int
7112 coerce_template_template_parm (tree parm,
7113 tree arg,
7114 tsubst_flags_t complain,
7115 tree in_decl,
7116 tree outer_args)
7117 {
7118 if (arg == NULL_TREE || error_operand_p (arg)
7119 || parm == NULL_TREE || error_operand_p (parm))
7120 return 0;
7121
7122 if (TREE_CODE (arg) != TREE_CODE (parm))
7123 return 0;
7124
7125 switch (TREE_CODE (parm))
7126 {
7127 case TEMPLATE_DECL:
7128 /* We encounter instantiations of templates like
7129 template <template <template <class> class> class TT>
7130 class C; */
7131 {
7132 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7133 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7134
7135 if (!coerce_template_template_parms
7136 (parmparm, argparm, complain, in_decl, outer_args))
7137 return 0;
7138 }
7139 /* Fall through. */
7140
7141 case TYPE_DECL:
7142 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7143 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7144 /* Argument is a parameter pack but parameter is not. */
7145 return 0;
7146 break;
7147
7148 case PARM_DECL:
7149 /* The tsubst call is used to handle cases such as
7150
7151 template <int> class C {};
7152 template <class T, template <T> class TT> class D {};
7153 D<int, C> d;
7154
7155 i.e. the parameter list of TT depends on earlier parameters. */
7156 if (!uses_template_parms (TREE_TYPE (arg)))
7157 {
7158 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7159 if (!uses_template_parms (t)
7160 && !same_type_p (t, TREE_TYPE (arg)))
7161 return 0;
7162 }
7163
7164 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7165 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7166 /* Argument is a parameter pack but parameter is not. */
7167 return 0;
7168
7169 break;
7170
7171 default:
7172 gcc_unreachable ();
7173 }
7174
7175 return 1;
7176 }
7177
7178 /* Coerce template argument list ARGLIST for use with template
7179 template-parameter TEMPL. */
7180
7181 static tree
7182 coerce_template_args_for_ttp (tree templ, tree arglist,
7183 tsubst_flags_t complain)
7184 {
7185 /* Consider an example where a template template parameter declared as
7186
7187 template <class T, class U = std::allocator<T> > class TT
7188
7189 The template parameter level of T and U are one level larger than
7190 of TT. To proper process the default argument of U, say when an
7191 instantiation `TT<int>' is seen, we need to build the full
7192 arguments containing {int} as the innermost level. Outer levels,
7193 available when not appearing as default template argument, can be
7194 obtained from the arguments of the enclosing template.
7195
7196 Suppose that TT is later substituted with std::vector. The above
7197 instantiation is `TT<int, std::allocator<T> >' with TT at
7198 level 1, and T at level 2, while the template arguments at level 1
7199 becomes {std::vector} and the inner level 2 is {int}. */
7200
7201 tree outer = DECL_CONTEXT (templ);
7202 if (outer)
7203 {
7204 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7205 /* We want arguments for the partial specialization, not arguments for
7206 the primary template. */
7207 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7208 else
7209 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7210 }
7211 else if (current_template_parms)
7212 {
7213 /* This is an argument of the current template, so we haven't set
7214 DECL_CONTEXT yet. */
7215 tree relevant_template_parms;
7216
7217 /* Parameter levels that are greater than the level of the given
7218 template template parm are irrelevant. */
7219 relevant_template_parms = current_template_parms;
7220 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7221 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7222 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7223
7224 outer = template_parms_to_args (relevant_template_parms);
7225 }
7226
7227 if (outer)
7228 arglist = add_to_template_args (outer, arglist);
7229
7230 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7231 return coerce_template_parms (parmlist, arglist, templ,
7232 complain,
7233 /*require_all_args=*/true,
7234 /*use_default_args=*/true);
7235 }
7236
7237 /* A cache of template template parameters with match-all default
7238 arguments. */
7239 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7240 static void
7241 store_defaulted_ttp (tree v, tree t)
7242 {
7243 if (!defaulted_ttp_cache)
7244 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7245 defaulted_ttp_cache->put (v, t);
7246 }
7247 static tree
7248 lookup_defaulted_ttp (tree v)
7249 {
7250 if (defaulted_ttp_cache)
7251 if (tree *p = defaulted_ttp_cache->get (v))
7252 return *p;
7253 return NULL_TREE;
7254 }
7255
7256 /* T is a bound template template-parameter. Copy its arguments into default
7257 arguments of the template template-parameter's template parameters. */
7258
7259 static tree
7260 add_defaults_to_ttp (tree otmpl)
7261 {
7262 if (tree c = lookup_defaulted_ttp (otmpl))
7263 return c;
7264
7265 tree ntmpl = copy_node (otmpl);
7266
7267 tree ntype = copy_node (TREE_TYPE (otmpl));
7268 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7269 TYPE_MAIN_VARIANT (ntype) = ntype;
7270 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7271 TYPE_NAME (ntype) = ntmpl;
7272 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7273
7274 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7275 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7276 TEMPLATE_PARM_DECL (idx) = ntmpl;
7277 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7278
7279 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7280 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7281 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7282 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7283 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7284 {
7285 tree o = TREE_VEC_ELT (vec, i);
7286 if (!template_parameter_pack_p (TREE_VALUE (o)))
7287 {
7288 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7289 TREE_PURPOSE (n) = any_targ_node;
7290 }
7291 }
7292
7293 store_defaulted_ttp (otmpl, ntmpl);
7294 return ntmpl;
7295 }
7296
7297 /* ARG is a bound potential template template-argument, and PARGS is a list
7298 of arguments for the corresponding template template-parameter. Adjust
7299 PARGS as appropriate for application to ARG's template, and if ARG is a
7300 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7301 arguments to the template template parameter. */
7302
7303 static tree
7304 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7305 {
7306 ++processing_template_decl;
7307 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7308 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7309 {
7310 /* When comparing two template template-parameters in partial ordering,
7311 rewrite the one currently being used as an argument to have default
7312 arguments for all parameters. */
7313 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7314 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7315 if (pargs != error_mark_node)
7316 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7317 TYPE_TI_ARGS (arg));
7318 }
7319 else
7320 {
7321 tree aparms
7322 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7323 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7324 /*require_all*/true,
7325 /*use_default*/true);
7326 }
7327 --processing_template_decl;
7328 return pargs;
7329 }
7330
7331 /* Subroutine of unify for the case when PARM is a
7332 BOUND_TEMPLATE_TEMPLATE_PARM. */
7333
7334 static int
7335 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7336 bool explain_p)
7337 {
7338 tree parmvec = TYPE_TI_ARGS (parm);
7339 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7340
7341 /* The template template parm might be variadic and the argument
7342 not, so flatten both argument lists. */
7343 parmvec = expand_template_argument_pack (parmvec);
7344 argvec = expand_template_argument_pack (argvec);
7345
7346 if (flag_new_ttp)
7347 {
7348 /* In keeping with P0522R0, adjust P's template arguments
7349 to apply to A's template; then flatten it again. */
7350 tree nparmvec = parmvec;
7351 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7352 nparmvec = expand_template_argument_pack (nparmvec);
7353
7354 if (unify (tparms, targs, nparmvec, argvec,
7355 UNIFY_ALLOW_NONE, explain_p))
7356 return 1;
7357
7358 /* If the P0522 adjustment eliminated a pack expansion, deduce
7359 empty packs. */
7360 if (flag_new_ttp
7361 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7362 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7363 DEDUCE_EXACT, /*sub*/true, explain_p))
7364 return 1;
7365 }
7366 else
7367 {
7368 /* Deduce arguments T, i from TT<T> or TT<i>.
7369 We check each element of PARMVEC and ARGVEC individually
7370 rather than the whole TREE_VEC since they can have
7371 different number of elements, which is allowed under N2555. */
7372
7373 int len = TREE_VEC_LENGTH (parmvec);
7374
7375 /* Check if the parameters end in a pack, making them
7376 variadic. */
7377 int parm_variadic_p = 0;
7378 if (len > 0
7379 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7380 parm_variadic_p = 1;
7381
7382 for (int i = 0; i < len - parm_variadic_p; ++i)
7383 /* If the template argument list of P contains a pack
7384 expansion that is not the last template argument, the
7385 entire template argument list is a non-deduced
7386 context. */
7387 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7388 return unify_success (explain_p);
7389
7390 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7391 return unify_too_few_arguments (explain_p,
7392 TREE_VEC_LENGTH (argvec), len);
7393
7394 for (int i = 0; i < len - parm_variadic_p; ++i)
7395 if (unify (tparms, targs,
7396 TREE_VEC_ELT (parmvec, i),
7397 TREE_VEC_ELT (argvec, i),
7398 UNIFY_ALLOW_NONE, explain_p))
7399 return 1;
7400
7401 if (parm_variadic_p
7402 && unify_pack_expansion (tparms, targs,
7403 parmvec, argvec,
7404 DEDUCE_EXACT,
7405 /*subr=*/true, explain_p))
7406 return 1;
7407 }
7408
7409 return 0;
7410 }
7411
7412 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7413 template template parameters. Both PARM_PARMS and ARG_PARMS are
7414 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7415 or PARM_DECL.
7416
7417 Consider the example:
7418 template <class T> class A;
7419 template<template <class U> class TT> class B;
7420
7421 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7422 the parameters to A, and OUTER_ARGS contains A. */
7423
7424 static int
7425 coerce_template_template_parms (tree parm_parms,
7426 tree arg_parms,
7427 tsubst_flags_t complain,
7428 tree in_decl,
7429 tree outer_args)
7430 {
7431 int nparms, nargs, i;
7432 tree parm, arg;
7433 int variadic_p = 0;
7434
7435 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7436 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7437
7438 nparms = TREE_VEC_LENGTH (parm_parms);
7439 nargs = TREE_VEC_LENGTH (arg_parms);
7440
7441 if (flag_new_ttp)
7442 {
7443 /* P0522R0: A template template-parameter P is at least as specialized as
7444 a template template-argument A if, given the following rewrite to two
7445 function templates, the function template corresponding to P is at
7446 least as specialized as the function template corresponding to A
7447 according to the partial ordering rules for function templates
7448 ([temp.func.order]). Given an invented class template X with the
7449 template parameter list of A (including default arguments):
7450
7451 * Each of the two function templates has the same template parameters,
7452 respectively, as P or A.
7453
7454 * Each function template has a single function parameter whose type is
7455 a specialization of X with template arguments corresponding to the
7456 template parameters from the respective function template where, for
7457 each template parameter PP in the template parameter list of the
7458 function template, a corresponding template argument AA is formed. If
7459 PP declares a parameter pack, then AA is the pack expansion
7460 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7461
7462 If the rewrite produces an invalid type, then P is not at least as
7463 specialized as A. */
7464
7465 /* So coerce P's args to apply to A's parms, and then deduce between A's
7466 args and the converted args. If that succeeds, A is at least as
7467 specialized as P, so they match.*/
7468 tree pargs = template_parms_level_to_args (parm_parms);
7469 ++processing_template_decl;
7470 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7471 /*require_all*/true, /*use_default*/true);
7472 --processing_template_decl;
7473 if (pargs != error_mark_node)
7474 {
7475 tree targs = make_tree_vec (nargs);
7476 tree aargs = template_parms_level_to_args (arg_parms);
7477 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7478 /*explain*/false))
7479 return 1;
7480 }
7481 }
7482
7483 /* Determine whether we have a parameter pack at the end of the
7484 template template parameter's template parameter list. */
7485 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7486 {
7487 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7488
7489 if (error_operand_p (parm))
7490 return 0;
7491
7492 switch (TREE_CODE (parm))
7493 {
7494 case TEMPLATE_DECL:
7495 case TYPE_DECL:
7496 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7497 variadic_p = 1;
7498 break;
7499
7500 case PARM_DECL:
7501 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7502 variadic_p = 1;
7503 break;
7504
7505 default:
7506 gcc_unreachable ();
7507 }
7508 }
7509
7510 if (nargs != nparms
7511 && !(variadic_p && nargs >= nparms - 1))
7512 return 0;
7513
7514 /* Check all of the template parameters except the parameter pack at
7515 the end (if any). */
7516 for (i = 0; i < nparms - variadic_p; ++i)
7517 {
7518 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7519 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7520 continue;
7521
7522 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7523 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7524
7525 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7526 outer_args))
7527 return 0;
7528
7529 }
7530
7531 if (variadic_p)
7532 {
7533 /* Check each of the template parameters in the template
7534 argument against the template parameter pack at the end of
7535 the template template parameter. */
7536 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7537 return 0;
7538
7539 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7540
7541 for (; i < nargs; ++i)
7542 {
7543 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7544 continue;
7545
7546 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7547
7548 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7549 outer_args))
7550 return 0;
7551 }
7552 }
7553
7554 return 1;
7555 }
7556
7557 /* Verifies that the deduced template arguments (in TARGS) for the
7558 template template parameters (in TPARMS) represent valid bindings,
7559 by comparing the template parameter list of each template argument
7560 to the template parameter list of its corresponding template
7561 template parameter, in accordance with DR150. This
7562 routine can only be called after all template arguments have been
7563 deduced. It will return TRUE if all of the template template
7564 parameter bindings are okay, FALSE otherwise. */
7565 bool
7566 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7567 {
7568 int i, ntparms = TREE_VEC_LENGTH (tparms);
7569 bool ret = true;
7570
7571 /* We're dealing with template parms in this process. */
7572 ++processing_template_decl;
7573
7574 targs = INNERMOST_TEMPLATE_ARGS (targs);
7575
7576 for (i = 0; i < ntparms; ++i)
7577 {
7578 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7579 tree targ = TREE_VEC_ELT (targs, i);
7580
7581 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7582 {
7583 tree packed_args = NULL_TREE;
7584 int idx, len = 1;
7585
7586 if (ARGUMENT_PACK_P (targ))
7587 {
7588 /* Look inside the argument pack. */
7589 packed_args = ARGUMENT_PACK_ARGS (targ);
7590 len = TREE_VEC_LENGTH (packed_args);
7591 }
7592
7593 for (idx = 0; idx < len; ++idx)
7594 {
7595 tree targ_parms = NULL_TREE;
7596
7597 if (packed_args)
7598 /* Extract the next argument from the argument
7599 pack. */
7600 targ = TREE_VEC_ELT (packed_args, idx);
7601
7602 if (PACK_EXPANSION_P (targ))
7603 /* Look at the pattern of the pack expansion. */
7604 targ = PACK_EXPANSION_PATTERN (targ);
7605
7606 /* Extract the template parameters from the template
7607 argument. */
7608 if (TREE_CODE (targ) == TEMPLATE_DECL)
7609 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7610 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7611 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7612
7613 /* Verify that we can coerce the template template
7614 parameters from the template argument to the template
7615 parameter. This requires an exact match. */
7616 if (targ_parms
7617 && !coerce_template_template_parms
7618 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7619 targ_parms,
7620 tf_none,
7621 tparm,
7622 targs))
7623 {
7624 ret = false;
7625 goto out;
7626 }
7627 }
7628 }
7629 }
7630
7631 out:
7632
7633 --processing_template_decl;
7634 return ret;
7635 }
7636
7637 /* Since type attributes aren't mangled, we need to strip them from
7638 template type arguments. */
7639
7640 static tree
7641 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7642 {
7643 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7644 return arg;
7645 bool removed_attributes = false;
7646 tree canon = strip_typedefs (arg, &removed_attributes);
7647 if (removed_attributes
7648 && (complain & tf_warning))
7649 warning (OPT_Wignored_attributes,
7650 "ignoring attributes on template argument %qT", arg);
7651 return canon;
7652 }
7653
7654 /* And from inside dependent non-type arguments like sizeof(Type). */
7655
7656 static tree
7657 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7658 {
7659 if (!arg || arg == error_mark_node)
7660 return arg;
7661 bool removed_attributes = false;
7662 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7663 if (removed_attributes
7664 && (complain & tf_warning))
7665 warning (OPT_Wignored_attributes,
7666 "ignoring attributes in template argument %qE", arg);
7667 return canon;
7668 }
7669
7670 // A template declaration can be substituted for a constrained
7671 // template template parameter only when the argument is more
7672 // constrained than the parameter.
7673 static bool
7674 is_compatible_template_arg (tree parm, tree arg)
7675 {
7676 tree parm_cons = get_constraints (parm);
7677
7678 /* For now, allow constrained template template arguments
7679 and unconstrained template template parameters. */
7680 if (parm_cons == NULL_TREE)
7681 return true;
7682
7683 tree arg_cons = get_constraints (arg);
7684
7685 // If the template parameter is constrained, we need to rewrite its
7686 // constraints in terms of the ARG's template parameters. This ensures
7687 // that all of the template parameter types will have the same depth.
7688 //
7689 // Note that this is only valid when coerce_template_template_parm is
7690 // true for the innermost template parameters of PARM and ARG. In other
7691 // words, because coercion is successful, this conversion will be valid.
7692 if (parm_cons)
7693 {
7694 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7695 parm_cons = tsubst_constraint_info (parm_cons,
7696 INNERMOST_TEMPLATE_ARGS (args),
7697 tf_none, NULL_TREE);
7698 if (parm_cons == error_mark_node)
7699 return false;
7700 }
7701
7702 return subsumes (parm_cons, arg_cons);
7703 }
7704
7705 // Convert a placeholder argument into a binding to the original
7706 // parameter. The original parameter is saved as the TREE_TYPE of
7707 // ARG.
7708 static inline tree
7709 convert_wildcard_argument (tree parm, tree arg)
7710 {
7711 TREE_TYPE (arg) = parm;
7712 return arg;
7713 }
7714
7715 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7716 because one of them is dependent. But we need to represent the
7717 conversion for the benefit of cp_tree_equal. */
7718
7719 static tree
7720 maybe_convert_nontype_argument (tree type, tree arg)
7721 {
7722 /* Auto parms get no conversion. */
7723 if (type_uses_auto (type))
7724 return arg;
7725 /* We don't need or want to add this conversion now if we're going to use the
7726 argument for deduction. */
7727 if (value_dependent_expression_p (arg))
7728 return arg;
7729
7730 type = cv_unqualified (type);
7731 tree argtype = TREE_TYPE (arg);
7732 if (same_type_p (type, argtype))
7733 return arg;
7734
7735 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7736 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7737 return arg;
7738 }
7739
7740 /* Convert the indicated template ARG as necessary to match the
7741 indicated template PARM. Returns the converted ARG, or
7742 error_mark_node if the conversion was unsuccessful. Error and
7743 warning messages are issued under control of COMPLAIN. This
7744 conversion is for the Ith parameter in the parameter list. ARGS is
7745 the full set of template arguments deduced so far. */
7746
7747 static tree
7748 convert_template_argument (tree parm,
7749 tree arg,
7750 tree args,
7751 tsubst_flags_t complain,
7752 int i,
7753 tree in_decl)
7754 {
7755 tree orig_arg;
7756 tree val;
7757 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7758
7759 if (parm == error_mark_node)
7760 return error_mark_node;
7761
7762 /* Trivially convert placeholders. */
7763 if (TREE_CODE (arg) == WILDCARD_DECL)
7764 return convert_wildcard_argument (parm, arg);
7765
7766 if (arg == any_targ_node)
7767 return arg;
7768
7769 if (TREE_CODE (arg) == TREE_LIST
7770 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7771 {
7772 /* The template argument was the name of some
7773 member function. That's usually
7774 invalid, but static members are OK. In any
7775 case, grab the underlying fields/functions
7776 and issue an error later if required. */
7777 orig_arg = TREE_VALUE (arg);
7778 TREE_TYPE (arg) = unknown_type_node;
7779 }
7780
7781 orig_arg = arg;
7782
7783 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7784 requires_type = (TREE_CODE (parm) == TYPE_DECL
7785 || requires_tmpl_type);
7786
7787 /* When determining whether an argument pack expansion is a template,
7788 look at the pattern. */
7789 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7790 arg = PACK_EXPANSION_PATTERN (arg);
7791
7792 /* Deal with an injected-class-name used as a template template arg. */
7793 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7794 {
7795 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7796 if (TREE_CODE (t) == TEMPLATE_DECL)
7797 {
7798 if (cxx_dialect >= cxx11)
7799 /* OK under DR 1004. */;
7800 else if (complain & tf_warning_or_error)
7801 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7802 " used as template template argument", TYPE_NAME (arg));
7803 else if (flag_pedantic_errors)
7804 t = arg;
7805
7806 arg = t;
7807 }
7808 }
7809
7810 is_tmpl_type =
7811 ((TREE_CODE (arg) == TEMPLATE_DECL
7812 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7813 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7814 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7815 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7816
7817 if (is_tmpl_type
7818 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7819 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7820 arg = TYPE_STUB_DECL (arg);
7821
7822 is_type = TYPE_P (arg) || is_tmpl_type;
7823
7824 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7825 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7826 {
7827 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7828 {
7829 if (complain & tf_error)
7830 error ("invalid use of destructor %qE as a type", orig_arg);
7831 return error_mark_node;
7832 }
7833
7834 permerror (input_location,
7835 "to refer to a type member of a template parameter, "
7836 "use %<typename %E%>", orig_arg);
7837
7838 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7839 TREE_OPERAND (arg, 1),
7840 typename_type,
7841 complain);
7842 arg = orig_arg;
7843 is_type = 1;
7844 }
7845 if (is_type != requires_type)
7846 {
7847 if (in_decl)
7848 {
7849 if (complain & tf_error)
7850 {
7851 error ("type/value mismatch at argument %d in template "
7852 "parameter list for %qD",
7853 i + 1, in_decl);
7854 if (is_type)
7855 inform (input_location,
7856 " expected a constant of type %qT, got %qT",
7857 TREE_TYPE (parm),
7858 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7859 else if (requires_tmpl_type)
7860 inform (input_location,
7861 " expected a class template, got %qE", orig_arg);
7862 else
7863 inform (input_location,
7864 " expected a type, got %qE", orig_arg);
7865 }
7866 }
7867 return error_mark_node;
7868 }
7869 if (is_tmpl_type ^ requires_tmpl_type)
7870 {
7871 if (in_decl && (complain & tf_error))
7872 {
7873 error ("type/value mismatch at argument %d in template "
7874 "parameter list for %qD",
7875 i + 1, in_decl);
7876 if (is_tmpl_type)
7877 inform (input_location,
7878 " expected a type, got %qT", DECL_NAME (arg));
7879 else
7880 inform (input_location,
7881 " expected a class template, got %qT", orig_arg);
7882 }
7883 return error_mark_node;
7884 }
7885
7886 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7887 /* We already did the appropriate conversion when packing args. */
7888 val = orig_arg;
7889 else if (is_type)
7890 {
7891 if (requires_tmpl_type)
7892 {
7893 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7894 /* The number of argument required is not known yet.
7895 Just accept it for now. */
7896 val = orig_arg;
7897 else
7898 {
7899 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7900 tree argparm;
7901
7902 /* Strip alias templates that are equivalent to another
7903 template. */
7904 arg = get_underlying_template (arg);
7905 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7906
7907 if (coerce_template_template_parms (parmparm, argparm,
7908 complain, in_decl,
7909 args))
7910 {
7911 val = arg;
7912
7913 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7914 TEMPLATE_DECL. */
7915 if (val != error_mark_node)
7916 {
7917 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7918 val = TREE_TYPE (val);
7919 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7920 val = make_pack_expansion (val, complain);
7921 }
7922 }
7923 else
7924 {
7925 if (in_decl && (complain & tf_error))
7926 {
7927 error ("type/value mismatch at argument %d in "
7928 "template parameter list for %qD",
7929 i + 1, in_decl);
7930 inform (input_location,
7931 " expected a template of type %qD, got %qT",
7932 parm, orig_arg);
7933 }
7934
7935 val = error_mark_node;
7936 }
7937
7938 // Check that the constraints are compatible before allowing the
7939 // substitution.
7940 if (val != error_mark_node)
7941 if (!is_compatible_template_arg (parm, arg))
7942 {
7943 if (in_decl && (complain & tf_error))
7944 {
7945 error ("constraint mismatch at argument %d in "
7946 "template parameter list for %qD",
7947 i + 1, in_decl);
7948 inform (input_location, " expected %qD but got %qD",
7949 parm, arg);
7950 }
7951 val = error_mark_node;
7952 }
7953 }
7954 }
7955 else
7956 val = orig_arg;
7957 /* We only form one instance of each template specialization.
7958 Therefore, if we use a non-canonical variant (i.e., a
7959 typedef), any future messages referring to the type will use
7960 the typedef, which is confusing if those future uses do not
7961 themselves also use the typedef. */
7962 if (TYPE_P (val))
7963 val = canonicalize_type_argument (val, complain);
7964 }
7965 else
7966 {
7967 tree t = TREE_TYPE (parm);
7968
7969 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
7970 > TMPL_ARGS_DEPTH (args))
7971 /* We don't have enough levels of args to do any substitution. This
7972 can happen in the context of -fnew-ttp-matching. */;
7973 else if (tree a = type_uses_auto (t))
7974 {
7975 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7976 if (t == error_mark_node)
7977 return error_mark_node;
7978 }
7979 else
7980 t = tsubst (t, args, complain, in_decl);
7981
7982 if (invalid_nontype_parm_type_p (t, complain))
7983 return error_mark_node;
7984
7985 if (!type_dependent_expression_p (orig_arg)
7986 && !uses_template_parms (t))
7987 /* We used to call digest_init here. However, digest_init
7988 will report errors, which we don't want when complain
7989 is zero. More importantly, digest_init will try too
7990 hard to convert things: for example, `0' should not be
7991 converted to pointer type at this point according to
7992 the standard. Accepting this is not merely an
7993 extension, since deciding whether or not these
7994 conversions can occur is part of determining which
7995 function template to call, or whether a given explicit
7996 argument specification is valid. */
7997 val = convert_nontype_argument (t, orig_arg, complain);
7998 else
7999 {
8000 val = canonicalize_expr_argument (orig_arg, complain);
8001 val = maybe_convert_nontype_argument (t, val);
8002 }
8003
8004
8005 if (val == NULL_TREE)
8006 val = error_mark_node;
8007 else if (val == error_mark_node && (complain & tf_error))
8008 error ("could not convert template argument %qE from %qT to %qT",
8009 orig_arg, TREE_TYPE (orig_arg), t);
8010
8011 if (INDIRECT_REF_P (val))
8012 {
8013 /* Reject template arguments that are references to built-in
8014 functions with no library fallbacks. */
8015 const_tree inner = TREE_OPERAND (val, 0);
8016 const_tree innertype = TREE_TYPE (inner);
8017 if (innertype
8018 && TYPE_REF_P (innertype)
8019 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8020 && TREE_OPERAND_LENGTH (inner) > 0
8021 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8022 return error_mark_node;
8023 }
8024
8025 if (TREE_CODE (val) == SCOPE_REF)
8026 {
8027 /* Strip typedefs from the SCOPE_REF. */
8028 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8029 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8030 complain);
8031 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8032 QUALIFIED_NAME_IS_TEMPLATE (val));
8033 }
8034 }
8035
8036 return val;
8037 }
8038
8039 /* Coerces the remaining template arguments in INNER_ARGS (from
8040 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8041 Returns the coerced argument pack. PARM_IDX is the position of this
8042 parameter in the template parameter list. ARGS is the original
8043 template argument list. */
8044 static tree
8045 coerce_template_parameter_pack (tree parms,
8046 int parm_idx,
8047 tree args,
8048 tree inner_args,
8049 int arg_idx,
8050 tree new_args,
8051 int* lost,
8052 tree in_decl,
8053 tsubst_flags_t complain)
8054 {
8055 tree parm = TREE_VEC_ELT (parms, parm_idx);
8056 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8057 tree packed_args;
8058 tree argument_pack;
8059 tree packed_parms = NULL_TREE;
8060
8061 if (arg_idx > nargs)
8062 arg_idx = nargs;
8063
8064 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8065 {
8066 /* When the template parameter is a non-type template parameter pack
8067 or template template parameter pack whose type or template
8068 parameters use parameter packs, we know exactly how many arguments
8069 we are looking for. Build a vector of the instantiated decls for
8070 these template parameters in PACKED_PARMS. */
8071 /* We can't use make_pack_expansion here because it would interpret a
8072 _DECL as a use rather than a declaration. */
8073 tree decl = TREE_VALUE (parm);
8074 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8075 SET_PACK_EXPANSION_PATTERN (exp, decl);
8076 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8077 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8078
8079 TREE_VEC_LENGTH (args)--;
8080 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8081 TREE_VEC_LENGTH (args)++;
8082
8083 if (packed_parms == error_mark_node)
8084 return error_mark_node;
8085
8086 /* If we're doing a partial instantiation of a member template,
8087 verify that all of the types used for the non-type
8088 template parameter pack are, in fact, valid for non-type
8089 template parameters. */
8090 if (arg_idx < nargs
8091 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8092 {
8093 int j, len = TREE_VEC_LENGTH (packed_parms);
8094 for (j = 0; j < len; ++j)
8095 {
8096 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8097 if (invalid_nontype_parm_type_p (t, complain))
8098 return error_mark_node;
8099 }
8100 /* We don't know how many args we have yet, just
8101 use the unconverted ones for now. */
8102 return NULL_TREE;
8103 }
8104
8105 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8106 }
8107 /* Check if we have a placeholder pack, which indicates we're
8108 in the context of a introduction list. In that case we want
8109 to match this pack to the single placeholder. */
8110 else if (arg_idx < nargs
8111 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8112 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8113 {
8114 nargs = arg_idx + 1;
8115 packed_args = make_tree_vec (1);
8116 }
8117 else
8118 packed_args = make_tree_vec (nargs - arg_idx);
8119
8120 /* Convert the remaining arguments, which will be a part of the
8121 parameter pack "parm". */
8122 int first_pack_arg = arg_idx;
8123 for (; arg_idx < nargs; ++arg_idx)
8124 {
8125 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8126 tree actual_parm = TREE_VALUE (parm);
8127 int pack_idx = arg_idx - first_pack_arg;
8128
8129 if (packed_parms)
8130 {
8131 /* Once we've packed as many args as we have types, stop. */
8132 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8133 break;
8134 else if (PACK_EXPANSION_P (arg))
8135 /* We don't know how many args we have yet, just
8136 use the unconverted ones for now. */
8137 return NULL_TREE;
8138 else
8139 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8140 }
8141
8142 if (arg == error_mark_node)
8143 {
8144 if (complain & tf_error)
8145 error ("template argument %d is invalid", arg_idx + 1);
8146 }
8147 else
8148 arg = convert_template_argument (actual_parm,
8149 arg, new_args, complain, parm_idx,
8150 in_decl);
8151 if (arg == error_mark_node)
8152 (*lost)++;
8153 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8154 }
8155
8156 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8157 && TREE_VEC_LENGTH (packed_args) > 0)
8158 {
8159 if (complain & tf_error)
8160 error ("wrong number of template arguments (%d, should be %d)",
8161 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8162 return error_mark_node;
8163 }
8164
8165 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8166 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8167 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8168 else
8169 {
8170 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8171 TREE_CONSTANT (argument_pack) = 1;
8172 }
8173
8174 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8175 if (CHECKING_P)
8176 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8177 TREE_VEC_LENGTH (packed_args));
8178 return argument_pack;
8179 }
8180
8181 /* Returns the number of pack expansions in the template argument vector
8182 ARGS. */
8183
8184 static int
8185 pack_expansion_args_count (tree args)
8186 {
8187 int i;
8188 int count = 0;
8189 if (args)
8190 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8191 {
8192 tree elt = TREE_VEC_ELT (args, i);
8193 if (elt && PACK_EXPANSION_P (elt))
8194 ++count;
8195 }
8196 return count;
8197 }
8198
8199 /* Convert all template arguments to their appropriate types, and
8200 return a vector containing the innermost resulting template
8201 arguments. If any error occurs, return error_mark_node. Error and
8202 warning messages are issued under control of COMPLAIN.
8203
8204 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8205 for arguments not specified in ARGS. Otherwise, if
8206 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8207 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8208 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8209 ARGS. */
8210
8211 static tree
8212 coerce_template_parms (tree parms,
8213 tree args,
8214 tree in_decl,
8215 tsubst_flags_t complain,
8216 bool require_all_args,
8217 bool use_default_args)
8218 {
8219 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8220 tree orig_inner_args;
8221 tree inner_args;
8222 tree new_args;
8223 tree new_inner_args;
8224 int saved_unevaluated_operand;
8225 int saved_inhibit_evaluation_warnings;
8226
8227 /* When used as a boolean value, indicates whether this is a
8228 variadic template parameter list. Since it's an int, we can also
8229 subtract it from nparms to get the number of non-variadic
8230 parameters. */
8231 int variadic_p = 0;
8232 int variadic_args_p = 0;
8233 int post_variadic_parms = 0;
8234
8235 /* Adjustment to nparms for fixed parameter packs. */
8236 int fixed_pack_adjust = 0;
8237 int fixed_packs = 0;
8238 int missing = 0;
8239
8240 /* Likewise for parameters with default arguments. */
8241 int default_p = 0;
8242
8243 if (args == error_mark_node)
8244 return error_mark_node;
8245
8246 nparms = TREE_VEC_LENGTH (parms);
8247
8248 /* Determine if there are any parameter packs or default arguments. */
8249 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8250 {
8251 tree parm = TREE_VEC_ELT (parms, parm_idx);
8252 if (variadic_p)
8253 ++post_variadic_parms;
8254 if (template_parameter_pack_p (TREE_VALUE (parm)))
8255 ++variadic_p;
8256 if (TREE_PURPOSE (parm))
8257 ++default_p;
8258 }
8259
8260 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8261 /* If there are no parameters that follow a parameter pack, we need to
8262 expand any argument packs so that we can deduce a parameter pack from
8263 some non-packed args followed by an argument pack, as in variadic85.C.
8264 If there are such parameters, we need to leave argument packs intact
8265 so the arguments are assigned properly. This can happen when dealing
8266 with a nested class inside a partial specialization of a class
8267 template, as in variadic92.C, or when deducing a template parameter pack
8268 from a sub-declarator, as in variadic114.C. */
8269 if (!post_variadic_parms)
8270 inner_args = expand_template_argument_pack (inner_args);
8271
8272 /* Count any pack expansion args. */
8273 variadic_args_p = pack_expansion_args_count (inner_args);
8274
8275 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8276 if ((nargs - variadic_args_p > nparms && !variadic_p)
8277 || (nargs < nparms - variadic_p
8278 && require_all_args
8279 && !variadic_args_p
8280 && (!use_default_args
8281 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8282 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8283 {
8284 bad_nargs:
8285 if (complain & tf_error)
8286 {
8287 if (variadic_p || default_p)
8288 {
8289 nparms -= variadic_p + default_p;
8290 error ("wrong number of template arguments "
8291 "(%d, should be at least %d)", nargs, nparms);
8292 }
8293 else
8294 error ("wrong number of template arguments "
8295 "(%d, should be %d)", nargs, nparms);
8296
8297 if (in_decl)
8298 inform (DECL_SOURCE_LOCATION (in_decl),
8299 "provided for %qD", in_decl);
8300 }
8301
8302 return error_mark_node;
8303 }
8304 /* We can't pass a pack expansion to a non-pack parameter of an alias
8305 template (DR 1430). */
8306 else if (in_decl
8307 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8308 || concept_template_p (in_decl))
8309 && variadic_args_p
8310 && nargs - variadic_args_p < nparms - variadic_p)
8311 {
8312 if (complain & tf_error)
8313 {
8314 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8315 {
8316 tree arg = TREE_VEC_ELT (inner_args, i);
8317 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8318
8319 if (PACK_EXPANSION_P (arg)
8320 && !template_parameter_pack_p (parm))
8321 {
8322 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8323 error_at (location_of (arg),
8324 "pack expansion argument for non-pack parameter "
8325 "%qD of alias template %qD", parm, in_decl);
8326 else
8327 error_at (location_of (arg),
8328 "pack expansion argument for non-pack parameter "
8329 "%qD of concept %qD", parm, in_decl);
8330 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8331 goto found;
8332 }
8333 }
8334 gcc_unreachable ();
8335 found:;
8336 }
8337 return error_mark_node;
8338 }
8339
8340 /* We need to evaluate the template arguments, even though this
8341 template-id may be nested within a "sizeof". */
8342 saved_unevaluated_operand = cp_unevaluated_operand;
8343 cp_unevaluated_operand = 0;
8344 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8345 c_inhibit_evaluation_warnings = 0;
8346 new_inner_args = make_tree_vec (nparms);
8347 new_args = add_outermost_template_args (args, new_inner_args);
8348 int pack_adjust = 0;
8349 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8350 {
8351 tree arg;
8352 tree parm;
8353
8354 /* Get the Ith template parameter. */
8355 parm = TREE_VEC_ELT (parms, parm_idx);
8356
8357 if (parm == error_mark_node)
8358 {
8359 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8360 continue;
8361 }
8362
8363 /* Calculate the next argument. */
8364 if (arg_idx < nargs)
8365 arg = TREE_VEC_ELT (inner_args, arg_idx);
8366 else
8367 arg = NULL_TREE;
8368
8369 if (template_parameter_pack_p (TREE_VALUE (parm))
8370 && !(arg && ARGUMENT_PACK_P (arg)))
8371 {
8372 /* Some arguments will be placed in the
8373 template parameter pack PARM. */
8374 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8375 inner_args, arg_idx,
8376 new_args, &lost,
8377 in_decl, complain);
8378
8379 if (arg == NULL_TREE)
8380 {
8381 /* We don't know how many args we have yet, just use the
8382 unconverted (and still packed) ones for now. */
8383 new_inner_args = orig_inner_args;
8384 arg_idx = nargs;
8385 break;
8386 }
8387
8388 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8389
8390 /* Store this argument. */
8391 if (arg == error_mark_node)
8392 {
8393 lost++;
8394 /* We are done with all of the arguments. */
8395 arg_idx = nargs;
8396 break;
8397 }
8398 else
8399 {
8400 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8401 arg_idx += pack_adjust;
8402 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8403 {
8404 ++fixed_packs;
8405 fixed_pack_adjust += pack_adjust;
8406 }
8407 }
8408
8409 continue;
8410 }
8411 else if (arg)
8412 {
8413 if (PACK_EXPANSION_P (arg))
8414 {
8415 /* "If every valid specialization of a variadic template
8416 requires an empty template parameter pack, the template is
8417 ill-formed, no diagnostic required." So check that the
8418 pattern works with this parameter. */
8419 tree pattern = PACK_EXPANSION_PATTERN (arg);
8420 tree conv = convert_template_argument (TREE_VALUE (parm),
8421 pattern, new_args,
8422 complain, parm_idx,
8423 in_decl);
8424 if (conv == error_mark_node)
8425 {
8426 if (complain & tf_error)
8427 inform (input_location, "so any instantiation with a "
8428 "non-empty parameter pack would be ill-formed");
8429 ++lost;
8430 }
8431 else if (TYPE_P (conv) && !TYPE_P (pattern))
8432 /* Recover from missing typename. */
8433 TREE_VEC_ELT (inner_args, arg_idx)
8434 = make_pack_expansion (conv, complain);
8435
8436 /* We don't know how many args we have yet, just
8437 use the unconverted ones for now. */
8438 new_inner_args = inner_args;
8439 arg_idx = nargs;
8440 break;
8441 }
8442 }
8443 else if (require_all_args)
8444 {
8445 /* There must be a default arg in this case. */
8446 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8447 complain, in_decl);
8448 /* The position of the first default template argument,
8449 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8450 Record that. */
8451 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8452 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8453 arg_idx - pack_adjust);
8454 }
8455 else
8456 break;
8457
8458 if (arg == error_mark_node)
8459 {
8460 if (complain & tf_error)
8461 error ("template argument %d is invalid", arg_idx + 1);
8462 }
8463 else if (!arg)
8464 {
8465 /* This can occur if there was an error in the template
8466 parameter list itself (which we would already have
8467 reported) that we are trying to recover from, e.g., a class
8468 template with a parameter list such as
8469 template<typename..., typename> (cpp0x/variadic150.C). */
8470 ++lost;
8471
8472 /* This can also happen with a fixed parameter pack (71834). */
8473 if (arg_idx >= nargs)
8474 ++missing;
8475 }
8476 else
8477 arg = convert_template_argument (TREE_VALUE (parm),
8478 arg, new_args, complain,
8479 parm_idx, in_decl);
8480
8481 if (arg == error_mark_node)
8482 lost++;
8483 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8484 }
8485 cp_unevaluated_operand = saved_unevaluated_operand;
8486 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8487
8488 if (missing || arg_idx < nargs - variadic_args_p)
8489 {
8490 /* If we had fixed parameter packs, we didn't know how many arguments we
8491 actually needed earlier; now we do. */
8492 nparms += fixed_pack_adjust;
8493 variadic_p -= fixed_packs;
8494 goto bad_nargs;
8495 }
8496
8497 if (arg_idx < nargs)
8498 {
8499 /* We had some pack expansion arguments that will only work if the packs
8500 are empty, but wait until instantiation time to complain.
8501 See variadic-ttp3.C. */
8502 int len = nparms + (nargs - arg_idx);
8503 tree args = make_tree_vec (len);
8504 int i = 0;
8505 for (; i < nparms; ++i)
8506 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8507 for (; i < len; ++i, ++arg_idx)
8508 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8509 arg_idx - pack_adjust);
8510 new_inner_args = args;
8511 }
8512
8513 if (lost)
8514 {
8515 gcc_assert (!(complain & tf_error) || seen_error ());
8516 return error_mark_node;
8517 }
8518
8519 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8520 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8521 TREE_VEC_LENGTH (new_inner_args));
8522
8523 return new_inner_args;
8524 }
8525
8526 /* Convert all template arguments to their appropriate types, and
8527 return a vector containing the innermost resulting template
8528 arguments. If any error occurs, return error_mark_node. Error and
8529 warning messages are not issued.
8530
8531 Note that no function argument deduction is performed, and default
8532 arguments are used to fill in unspecified arguments. */
8533 tree
8534 coerce_template_parms (tree parms, tree args, tree in_decl)
8535 {
8536 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8537 }
8538
8539 /* Convert all template arguments to their appropriate type, and
8540 instantiate default arguments as needed. This returns a vector
8541 containing the innermost resulting template arguments, or
8542 error_mark_node if unsuccessful. */
8543 tree
8544 coerce_template_parms (tree parms, tree args, tree in_decl,
8545 tsubst_flags_t complain)
8546 {
8547 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8548 }
8549
8550 /* Like coerce_template_parms. If PARMS represents all template
8551 parameters levels, this function returns a vector of vectors
8552 representing all the resulting argument levels. Note that in this
8553 case, only the innermost arguments are coerced because the
8554 outermost ones are supposed to have been coerced already.
8555
8556 Otherwise, if PARMS represents only (the innermost) vector of
8557 parameters, this function returns a vector containing just the
8558 innermost resulting arguments. */
8559
8560 static tree
8561 coerce_innermost_template_parms (tree parms,
8562 tree args,
8563 tree in_decl,
8564 tsubst_flags_t complain,
8565 bool require_all_args,
8566 bool use_default_args)
8567 {
8568 int parms_depth = TMPL_PARMS_DEPTH (parms);
8569 int args_depth = TMPL_ARGS_DEPTH (args);
8570 tree coerced_args;
8571
8572 if (parms_depth > 1)
8573 {
8574 coerced_args = make_tree_vec (parms_depth);
8575 tree level;
8576 int cur_depth;
8577
8578 for (level = parms, cur_depth = parms_depth;
8579 parms_depth > 0 && level != NULL_TREE;
8580 level = TREE_CHAIN (level), --cur_depth)
8581 {
8582 tree l;
8583 if (cur_depth == args_depth)
8584 l = coerce_template_parms (TREE_VALUE (level),
8585 args, in_decl, complain,
8586 require_all_args,
8587 use_default_args);
8588 else
8589 l = TMPL_ARGS_LEVEL (args, cur_depth);
8590
8591 if (l == error_mark_node)
8592 return error_mark_node;
8593
8594 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8595 }
8596 }
8597 else
8598 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8599 args, in_decl, complain,
8600 require_all_args,
8601 use_default_args);
8602 return coerced_args;
8603 }
8604
8605 /* Returns 1 if template args OT and NT are equivalent. */
8606
8607 int
8608 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8609 {
8610 if (nt == ot)
8611 return 1;
8612 if (nt == NULL_TREE || ot == NULL_TREE)
8613 return false;
8614 if (nt == any_targ_node || ot == any_targ_node)
8615 return true;
8616
8617 if (TREE_CODE (nt) == TREE_VEC)
8618 /* For member templates */
8619 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8620 else if (PACK_EXPANSION_P (ot))
8621 return (PACK_EXPANSION_P (nt)
8622 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8623 PACK_EXPANSION_PATTERN (nt))
8624 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8625 PACK_EXPANSION_EXTRA_ARGS (nt)));
8626 else if (ARGUMENT_PACK_P (ot))
8627 {
8628 int i, len;
8629 tree opack, npack;
8630
8631 if (!ARGUMENT_PACK_P (nt))
8632 return 0;
8633
8634 opack = ARGUMENT_PACK_ARGS (ot);
8635 npack = ARGUMENT_PACK_ARGS (nt);
8636 len = TREE_VEC_LENGTH (opack);
8637 if (TREE_VEC_LENGTH (npack) != len)
8638 return 0;
8639 for (i = 0; i < len; ++i)
8640 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8641 TREE_VEC_ELT (npack, i)))
8642 return 0;
8643 return 1;
8644 }
8645 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8646 gcc_unreachable ();
8647 else if (TYPE_P (nt))
8648 {
8649 if (!TYPE_P (ot))
8650 return false;
8651 /* Don't treat an alias template specialization with dependent
8652 arguments as equivalent to its underlying type when used as a
8653 template argument; we need them to be distinct so that we
8654 substitute into the specialization arguments at instantiation
8655 time. And aliases can't be equivalent without being ==, so
8656 we don't need to look any deeper.
8657
8658 During partial ordering, however, we need to treat them normally so
8659 that we can order uses of the same alias with different
8660 cv-qualification (79960). */
8661 if (!partial_order
8662 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8663 return false;
8664 else
8665 return same_type_p (ot, nt);
8666 }
8667 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8668 return 0;
8669 else
8670 {
8671 /* Try to treat a template non-type argument that has been converted
8672 to the parameter type as equivalent to one that hasn't yet. */
8673 for (enum tree_code code1 = TREE_CODE (ot);
8674 CONVERT_EXPR_CODE_P (code1)
8675 || code1 == NON_LVALUE_EXPR;
8676 code1 = TREE_CODE (ot))
8677 ot = TREE_OPERAND (ot, 0);
8678 for (enum tree_code code2 = TREE_CODE (nt);
8679 CONVERT_EXPR_CODE_P (code2)
8680 || code2 == NON_LVALUE_EXPR;
8681 code2 = TREE_CODE (nt))
8682 nt = TREE_OPERAND (nt, 0);
8683
8684 return cp_tree_equal (ot, nt);
8685 }
8686 }
8687
8688 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8689 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8690 NEWARG_PTR with the offending arguments if they are non-NULL. */
8691
8692 int
8693 comp_template_args (tree oldargs, tree newargs,
8694 tree *oldarg_ptr, tree *newarg_ptr,
8695 bool partial_order)
8696 {
8697 int i;
8698
8699 if (oldargs == newargs)
8700 return 1;
8701
8702 if (!oldargs || !newargs)
8703 return 0;
8704
8705 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8706 return 0;
8707
8708 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8709 {
8710 tree nt = TREE_VEC_ELT (newargs, i);
8711 tree ot = TREE_VEC_ELT (oldargs, i);
8712
8713 if (! template_args_equal (ot, nt, partial_order))
8714 {
8715 if (oldarg_ptr != NULL)
8716 *oldarg_ptr = ot;
8717 if (newarg_ptr != NULL)
8718 *newarg_ptr = nt;
8719 return 0;
8720 }
8721 }
8722 return 1;
8723 }
8724
8725 inline bool
8726 comp_template_args_porder (tree oargs, tree nargs)
8727 {
8728 return comp_template_args (oargs, nargs, NULL, NULL, true);
8729 }
8730
8731 /* Implement a freelist interface for objects of type T.
8732
8733 Head is a separate object, rather than a regular member, so that we
8734 can define it as a GTY deletable pointer, which is highly
8735 desirable. A data member could be declared that way, but then the
8736 containing object would implicitly get GTY((user)), which would
8737 prevent us from instantiating freelists as global objects.
8738 Although this way we can create freelist global objects, they're
8739 such thin wrappers that instantiating temporaries at every use
8740 loses nothing and saves permanent storage for the freelist object.
8741
8742 Member functions next, anew, poison and reinit have default
8743 implementations that work for most of the types we're interested
8744 in, but if they don't work for some type, they should be explicitly
8745 specialized. See the comments before them for requirements, and
8746 the example specializations for the tree_list_freelist. */
8747 template <typename T>
8748 class freelist
8749 {
8750 /* Return the next object in a chain. We could just do type
8751 punning, but if we access the object with its underlying type, we
8752 avoid strict-aliasing trouble. This needs only work between
8753 poison and reinit. */
8754 static T *&next (T *obj) { return obj->next; }
8755
8756 /* Return a newly allocated, uninitialized or minimally-initialized
8757 object of type T. Any initialization performed by anew should
8758 either remain across the life of the object and the execution of
8759 poison, or be redone by reinit. */
8760 static T *anew () { return ggc_alloc<T> (); }
8761
8762 /* Optionally scribble all over the bits holding the object, so that
8763 they become (mostly?) uninitialized memory. This is called while
8764 preparing to make the object part of the free list. */
8765 static void poison (T *obj) {
8766 T *p ATTRIBUTE_UNUSED = obj;
8767 T **q ATTRIBUTE_UNUSED = &next (obj);
8768
8769 #ifdef ENABLE_GC_CHECKING
8770 /* Poison the data, to indicate the data is garbage. */
8771 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
8772 memset (p, 0xa5, sizeof (*p));
8773 #endif
8774 /* Let valgrind know the object is free. */
8775 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
8776
8777 /* Let valgrind know the next portion of the object is available,
8778 but uninitialized. */
8779 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8780 }
8781
8782 /* Bring an object that underwent at least one lifecycle after anew
8783 and before the most recent free and poison, back to a usable
8784 state, reinitializing whatever is needed for it to be
8785 functionally equivalent to an object just allocated and returned
8786 by anew. This may poison or clear the next field, used by
8787 freelist housekeeping after poison was called. */
8788 static void reinit (T *obj) {
8789 T **q ATTRIBUTE_UNUSED = &next (obj);
8790
8791 #ifdef ENABLE_GC_CHECKING
8792 memset (q, 0xa5, sizeof (*q));
8793 #endif
8794 /* Let valgrind know the entire object is available, but
8795 uninitialized. */
8796 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
8797 }
8798
8799 /* Reference a GTY-deletable pointer that points to the first object
8800 in the free list proper. */
8801 T *&head;
8802 public:
8803 /* Construct a freelist object chaining objects off of HEAD. */
8804 freelist (T *&head) : head(head) {}
8805
8806 /* Add OBJ to the free object list. The former head becomes OBJ's
8807 successor. */
8808 void free (T *obj)
8809 {
8810 poison (obj);
8811 next (obj) = head;
8812 head = obj;
8813 }
8814
8815 /* Take an object from the free list, if one is available, or
8816 allocate a new one. Objects taken from the free list should be
8817 regarded as filled with garbage, except for bits that are
8818 configured to be preserved across free and alloc. */
8819 T *alloc ()
8820 {
8821 if (head)
8822 {
8823 T *obj = head;
8824 head = next (head);
8825 reinit (obj);
8826 return obj;
8827 }
8828 else
8829 return anew ();
8830 }
8831 };
8832
8833 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8834 want to allocate a TREE_LIST using the usual interface, and ensure
8835 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8836 build_tree_list logic in reinit, so this could go out of sync. */
8837 template <>
8838 inline tree &
8839 freelist<tree_node>::next (tree obj)
8840 {
8841 return TREE_CHAIN (obj);
8842 }
8843 template <>
8844 inline tree
8845 freelist<tree_node>::anew ()
8846 {
8847 return build_tree_list (NULL, NULL);
8848 }
8849 template <>
8850 inline void
8851 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
8852 {
8853 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
8854 tree p ATTRIBUTE_UNUSED = obj;
8855 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8856 tree *q ATTRIBUTE_UNUSED = &next (obj);
8857
8858 #ifdef ENABLE_GC_CHECKING
8859 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8860
8861 /* Poison the data, to indicate the data is garbage. */
8862 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
8863 memset (p, 0xa5, size);
8864 #endif
8865 /* Let valgrind know the object is free. */
8866 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
8867 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8868 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8869 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8870
8871 #ifdef ENABLE_GC_CHECKING
8872 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
8873 /* Keep TREE_CHAIN functional. */
8874 TREE_SET_CODE (obj, TREE_LIST);
8875 #else
8876 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8877 #endif
8878 }
8879 template <>
8880 inline void
8881 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
8882 {
8883 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8884
8885 #ifdef ENABLE_GC_CHECKING
8886 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8887 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8888 memset (obj, 0, sizeof (tree_list));
8889 #endif
8890
8891 /* Let valgrind know the entire object is available, but
8892 uninitialized. */
8893 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8894
8895 #ifdef ENABLE_GC_CHECKING
8896 TREE_SET_CODE (obj, TREE_LIST);
8897 #else
8898 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8899 #endif
8900 }
8901
8902 /* Point to the first object in the TREE_LIST freelist. */
8903 static GTY((deletable)) tree tree_list_freelist_head;
8904 /* Return the/an actual TREE_LIST freelist. */
8905 static inline freelist<tree_node>
8906 tree_list_freelist ()
8907 {
8908 return tree_list_freelist_head;
8909 }
8910
8911 /* Point to the first object in the tinst_level freelist. */
8912 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
8913 /* Return the/an actual tinst_level freelist. */
8914 static inline freelist<tinst_level>
8915 tinst_level_freelist ()
8916 {
8917 return tinst_level_freelist_head;
8918 }
8919
8920 /* Point to the first object in the pending_template freelist. */
8921 static GTY((deletable)) pending_template *pending_template_freelist_head;
8922 /* Return the/an actual pending_template freelist. */
8923 static inline freelist<pending_template>
8924 pending_template_freelist ()
8925 {
8926 return pending_template_freelist_head;
8927 }
8928
8929 /* Build the TREE_LIST object out of a split list, store it
8930 permanently, and return it. */
8931 tree
8932 tinst_level::to_list ()
8933 {
8934 gcc_assert (split_list_p ());
8935 tree ret = tree_list_freelist ().alloc ();
8936 TREE_PURPOSE (ret) = tldcl;
8937 TREE_VALUE (ret) = targs;
8938 tldcl = ret;
8939 targs = NULL;
8940 gcc_assert (tree_list_p ());
8941 return ret;
8942 }
8943
8944 const unsigned short tinst_level::refcount_infinity;
8945
8946 /* Increment OBJ's refcount unless it is already infinite. */
8947 static tinst_level *
8948 inc_refcount_use (tinst_level *obj)
8949 {
8950 if (obj && obj->refcount != tinst_level::refcount_infinity)
8951 ++obj->refcount;
8952 return obj;
8953 }
8954
8955 /* Release storage for OBJ and node, if it's a TREE_LIST. */
8956 void
8957 tinst_level::free (tinst_level *obj)
8958 {
8959 if (obj->tree_list_p ())
8960 tree_list_freelist ().free (obj->get_node ());
8961 tinst_level_freelist ().free (obj);
8962 }
8963
8964 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
8965 OBJ's DECL and OBJ, and start over with the tinst_level object that
8966 used to be referenced by OBJ's NEXT. */
8967 static void
8968 dec_refcount_use (tinst_level *obj)
8969 {
8970 while (obj
8971 && obj->refcount != tinst_level::refcount_infinity
8972 && !--obj->refcount)
8973 {
8974 tinst_level *next = obj->next;
8975 tinst_level::free (obj);
8976 obj = next;
8977 }
8978 }
8979
8980 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
8981 and of the former PTR. Omitting the second argument is equivalent
8982 to passing (T*)NULL; this is allowed because passing the
8983 zero-valued integral constant NULL confuses type deduction and/or
8984 overload resolution. */
8985 template <typename T>
8986 static void
8987 set_refcount_ptr (T *& ptr, T *obj = NULL)
8988 {
8989 T *save = ptr;
8990 ptr = inc_refcount_use (obj);
8991 dec_refcount_use (save);
8992 }
8993
8994 static void
8995 add_pending_template (tree d)
8996 {
8997 tree ti = (TYPE_P (d)
8998 ? CLASSTYPE_TEMPLATE_INFO (d)
8999 : DECL_TEMPLATE_INFO (d));
9000 struct pending_template *pt;
9001 int level;
9002
9003 if (TI_PENDING_TEMPLATE_FLAG (ti))
9004 return;
9005
9006 /* We are called both from instantiate_decl, where we've already had a
9007 tinst_level pushed, and instantiate_template, where we haven't.
9008 Compensate. */
9009 gcc_assert (TREE_CODE (d) != TREE_LIST);
9010 level = !current_tinst_level
9011 || current_tinst_level->maybe_get_node () != d;
9012
9013 if (level)
9014 push_tinst_level (d);
9015
9016 pt = pending_template_freelist ().alloc ();
9017 pt->next = NULL;
9018 pt->tinst = NULL;
9019 set_refcount_ptr (pt->tinst, current_tinst_level);
9020 if (last_pending_template)
9021 last_pending_template->next = pt;
9022 else
9023 pending_templates = pt;
9024
9025 last_pending_template = pt;
9026
9027 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9028
9029 if (level)
9030 pop_tinst_level ();
9031 }
9032
9033
9034 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9035 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9036 documentation for TEMPLATE_ID_EXPR. */
9037
9038 tree
9039 lookup_template_function (tree fns, tree arglist)
9040 {
9041 tree type;
9042
9043 if (fns == error_mark_node || arglist == error_mark_node)
9044 return error_mark_node;
9045
9046 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9047
9048 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9049 {
9050 error ("%q#D is not a function template", fns);
9051 return error_mark_node;
9052 }
9053
9054 if (BASELINK_P (fns))
9055 {
9056 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9057 unknown_type_node,
9058 BASELINK_FUNCTIONS (fns),
9059 arglist);
9060 return fns;
9061 }
9062
9063 type = TREE_TYPE (fns);
9064 if (TREE_CODE (fns) == OVERLOAD || !type)
9065 type = unknown_type_node;
9066
9067 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
9068 }
9069
9070 /* Within the scope of a template class S<T>, the name S gets bound
9071 (in build_self_reference) to a TYPE_DECL for the class, not a
9072 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9073 or one of its enclosing classes, and that type is a template,
9074 return the associated TEMPLATE_DECL. Otherwise, the original
9075 DECL is returned.
9076
9077 Also handle the case when DECL is a TREE_LIST of ambiguous
9078 injected-class-names from different bases. */
9079
9080 tree
9081 maybe_get_template_decl_from_type_decl (tree decl)
9082 {
9083 if (decl == NULL_TREE)
9084 return decl;
9085
9086 /* DR 176: A lookup that finds an injected-class-name (10.2
9087 [class.member.lookup]) can result in an ambiguity in certain cases
9088 (for example, if it is found in more than one base class). If all of
9089 the injected-class-names that are found refer to specializations of
9090 the same class template, and if the name is followed by a
9091 template-argument-list, the reference refers to the class template
9092 itself and not a specialization thereof, and is not ambiguous. */
9093 if (TREE_CODE (decl) == TREE_LIST)
9094 {
9095 tree t, tmpl = NULL_TREE;
9096 for (t = decl; t; t = TREE_CHAIN (t))
9097 {
9098 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9099 if (!tmpl)
9100 tmpl = elt;
9101 else if (tmpl != elt)
9102 break;
9103 }
9104 if (tmpl && t == NULL_TREE)
9105 return tmpl;
9106 else
9107 return decl;
9108 }
9109
9110 return (decl != NULL_TREE
9111 && DECL_SELF_REFERENCE_P (decl)
9112 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9113 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9114 }
9115
9116 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9117 parameters, find the desired type.
9118
9119 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9120
9121 IN_DECL, if non-NULL, is the template declaration we are trying to
9122 instantiate.
9123
9124 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9125 the class we are looking up.
9126
9127 Issue error and warning messages under control of COMPLAIN.
9128
9129 If the template class is really a local class in a template
9130 function, then the FUNCTION_CONTEXT is the function in which it is
9131 being instantiated.
9132
9133 ??? Note that this function is currently called *twice* for each
9134 template-id: the first time from the parser, while creating the
9135 incomplete type (finish_template_type), and the second type during the
9136 real instantiation (instantiate_template_class). This is surely something
9137 that we want to avoid. It also causes some problems with argument
9138 coercion (see convert_nontype_argument for more information on this). */
9139
9140 static tree
9141 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9142 int entering_scope, tsubst_flags_t complain)
9143 {
9144 tree templ = NULL_TREE, parmlist;
9145 tree t;
9146 spec_entry **slot;
9147 spec_entry *entry;
9148 spec_entry elt;
9149 hashval_t hash;
9150
9151 if (identifier_p (d1))
9152 {
9153 tree value = innermost_non_namespace_value (d1);
9154 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9155 templ = value;
9156 else
9157 {
9158 if (context)
9159 push_decl_namespace (context);
9160 templ = lookup_name (d1);
9161 templ = maybe_get_template_decl_from_type_decl (templ);
9162 if (context)
9163 pop_decl_namespace ();
9164 }
9165 if (templ)
9166 context = DECL_CONTEXT (templ);
9167 }
9168 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9169 {
9170 tree type = TREE_TYPE (d1);
9171
9172 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9173 an implicit typename for the second A. Deal with it. */
9174 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9175 type = TREE_TYPE (type);
9176
9177 if (CLASSTYPE_TEMPLATE_INFO (type))
9178 {
9179 templ = CLASSTYPE_TI_TEMPLATE (type);
9180 d1 = DECL_NAME (templ);
9181 }
9182 }
9183 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9184 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9185 {
9186 templ = TYPE_TI_TEMPLATE (d1);
9187 d1 = DECL_NAME (templ);
9188 }
9189 else if (DECL_TYPE_TEMPLATE_P (d1))
9190 {
9191 templ = d1;
9192 d1 = DECL_NAME (templ);
9193 context = DECL_CONTEXT (templ);
9194 }
9195 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9196 {
9197 templ = d1;
9198 d1 = DECL_NAME (templ);
9199 }
9200
9201 /* Issue an error message if we didn't find a template. */
9202 if (! templ)
9203 {
9204 if (complain & tf_error)
9205 error ("%qT is not a template", d1);
9206 return error_mark_node;
9207 }
9208
9209 if (TREE_CODE (templ) != TEMPLATE_DECL
9210 /* Make sure it's a user visible template, if it was named by
9211 the user. */
9212 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9213 && !PRIMARY_TEMPLATE_P (templ)))
9214 {
9215 if (complain & tf_error)
9216 {
9217 error ("non-template type %qT used as a template", d1);
9218 if (in_decl)
9219 error ("for template declaration %q+D", in_decl);
9220 }
9221 return error_mark_node;
9222 }
9223
9224 complain &= ~tf_user;
9225
9226 /* An alias that just changes the name of a template is equivalent to the
9227 other template, so if any of the arguments are pack expansions, strip
9228 the alias to avoid problems with a pack expansion passed to a non-pack
9229 alias template parameter (DR 1430). */
9230 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9231 templ = get_underlying_template (templ);
9232
9233 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9234 {
9235 tree parm;
9236 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9237 if (arglist2 == error_mark_node
9238 || (!uses_template_parms (arglist2)
9239 && check_instantiated_args (templ, arglist2, complain)))
9240 return error_mark_node;
9241
9242 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9243 return parm;
9244 }
9245 else
9246 {
9247 tree template_type = TREE_TYPE (templ);
9248 tree gen_tmpl;
9249 tree type_decl;
9250 tree found = NULL_TREE;
9251 int arg_depth;
9252 int parm_depth;
9253 int is_dependent_type;
9254 int use_partial_inst_tmpl = false;
9255
9256 if (template_type == error_mark_node)
9257 /* An error occurred while building the template TEMPL, and a
9258 diagnostic has most certainly been emitted for that
9259 already. Let's propagate that error. */
9260 return error_mark_node;
9261
9262 gen_tmpl = most_general_template (templ);
9263 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9264 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9265 arg_depth = TMPL_ARGS_DEPTH (arglist);
9266
9267 if (arg_depth == 1 && parm_depth > 1)
9268 {
9269 /* We've been given an incomplete set of template arguments.
9270 For example, given:
9271
9272 template <class T> struct S1 {
9273 template <class U> struct S2 {};
9274 template <class U> struct S2<U*> {};
9275 };
9276
9277 we will be called with an ARGLIST of `U*', but the
9278 TEMPLATE will be `template <class T> template
9279 <class U> struct S1<T>::S2'. We must fill in the missing
9280 arguments. */
9281 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9282 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9283 arg_depth = TMPL_ARGS_DEPTH (arglist);
9284 }
9285
9286 /* Now we should have enough arguments. */
9287 gcc_assert (parm_depth == arg_depth);
9288
9289 /* From here on, we're only interested in the most general
9290 template. */
9291
9292 /* Calculate the BOUND_ARGS. These will be the args that are
9293 actually tsubst'd into the definition to create the
9294 instantiation. */
9295 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9296 complain,
9297 /*require_all_args=*/true,
9298 /*use_default_args=*/true);
9299
9300 if (arglist == error_mark_node)
9301 /* We were unable to bind the arguments. */
9302 return error_mark_node;
9303
9304 /* In the scope of a template class, explicit references to the
9305 template class refer to the type of the template, not any
9306 instantiation of it. For example, in:
9307
9308 template <class T> class C { void f(C<T>); }
9309
9310 the `C<T>' is just the same as `C'. Outside of the
9311 class, however, such a reference is an instantiation. */
9312 if (entering_scope
9313 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9314 || currently_open_class (template_type))
9315 {
9316 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9317
9318 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9319 return template_type;
9320 }
9321
9322 /* If we already have this specialization, return it. */
9323 elt.tmpl = gen_tmpl;
9324 elt.args = arglist;
9325 elt.spec = NULL_TREE;
9326 hash = spec_hasher::hash (&elt);
9327 entry = type_specializations->find_with_hash (&elt, hash);
9328
9329 if (entry)
9330 return entry->spec;
9331
9332 /* If the the template's constraints are not satisfied,
9333 then we cannot form a valid type.
9334
9335 Note that the check is deferred until after the hash
9336 lookup. This prevents redundant checks on previously
9337 instantiated specializations. */
9338 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9339 {
9340 if (complain & tf_error)
9341 {
9342 error ("template constraint failure");
9343 diagnose_constraints (input_location, gen_tmpl, arglist);
9344 }
9345 return error_mark_node;
9346 }
9347
9348 is_dependent_type = uses_template_parms (arglist);
9349
9350 /* If the deduced arguments are invalid, then the binding
9351 failed. */
9352 if (!is_dependent_type
9353 && check_instantiated_args (gen_tmpl,
9354 INNERMOST_TEMPLATE_ARGS (arglist),
9355 complain))
9356 return error_mark_node;
9357
9358 if (!is_dependent_type
9359 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9360 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9361 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9362 {
9363 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9364 DECL_NAME (gen_tmpl),
9365 /*tag_scope=*/ts_global);
9366 return found;
9367 }
9368
9369 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
9370 complain, in_decl);
9371 if (context == error_mark_node)
9372 return error_mark_node;
9373
9374 if (!context)
9375 context = global_namespace;
9376
9377 /* Create the type. */
9378 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9379 {
9380 /* The user referred to a specialization of an alias
9381 template represented by GEN_TMPL.
9382
9383 [temp.alias]/2 says:
9384
9385 When a template-id refers to the specialization of an
9386 alias template, it is equivalent to the associated
9387 type obtained by substitution of its
9388 template-arguments for the template-parameters in the
9389 type-id of the alias template. */
9390
9391 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9392 /* Note that the call above (by indirectly calling
9393 register_specialization in tsubst_decl) registers the
9394 TYPE_DECL representing the specialization of the alias
9395 template. So next time someone substitutes ARGLIST for
9396 the template parms into the alias template (GEN_TMPL),
9397 she'll get that TYPE_DECL back. */
9398
9399 if (t == error_mark_node)
9400 return t;
9401 }
9402 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9403 {
9404 if (!is_dependent_type)
9405 {
9406 set_current_access_from_decl (TYPE_NAME (template_type));
9407 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9408 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9409 arglist, complain, in_decl),
9410 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9411 arglist, complain, in_decl),
9412 SCOPED_ENUM_P (template_type), NULL);
9413
9414 if (t == error_mark_node)
9415 return t;
9416 }
9417 else
9418 {
9419 /* We don't want to call start_enum for this type, since
9420 the values for the enumeration constants may involve
9421 template parameters. And, no one should be interested
9422 in the enumeration constants for such a type. */
9423 t = cxx_make_type (ENUMERAL_TYPE);
9424 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9425 }
9426 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9427 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9428 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9429 }
9430 else if (CLASS_TYPE_P (template_type))
9431 {
9432 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9433 instantiated here. */
9434 gcc_assert (!LAMBDA_TYPE_P (template_type));
9435
9436 t = make_class_type (TREE_CODE (template_type));
9437 CLASSTYPE_DECLARED_CLASS (t)
9438 = CLASSTYPE_DECLARED_CLASS (template_type);
9439 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9440
9441 /* A local class. Make sure the decl gets registered properly. */
9442 if (context == current_function_decl)
9443 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9444 == error_mark_node)
9445 return error_mark_node;
9446
9447 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9448 /* This instantiation is another name for the primary
9449 template type. Set the TYPE_CANONICAL field
9450 appropriately. */
9451 TYPE_CANONICAL (t) = template_type;
9452 else if (any_template_arguments_need_structural_equality_p (arglist))
9453 /* Some of the template arguments require structural
9454 equality testing, so this template class requires
9455 structural equality testing. */
9456 SET_TYPE_STRUCTURAL_EQUALITY (t);
9457 }
9458 else
9459 gcc_unreachable ();
9460
9461 /* If we called start_enum or pushtag above, this information
9462 will already be set up. */
9463 if (!TYPE_NAME (t))
9464 {
9465 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9466
9467 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9468 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9469 DECL_SOURCE_LOCATION (type_decl)
9470 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9471 }
9472 else
9473 type_decl = TYPE_NAME (t);
9474
9475 if (CLASS_TYPE_P (template_type))
9476 {
9477 TREE_PRIVATE (type_decl)
9478 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9479 TREE_PROTECTED (type_decl)
9480 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9481 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9482 {
9483 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9484 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9485 }
9486 }
9487
9488 if (OVERLOAD_TYPE_P (t)
9489 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9490 {
9491 static const char *tags[] = {"abi_tag", "may_alias"};
9492
9493 for (unsigned ix = 0; ix != 2; ix++)
9494 {
9495 tree attributes
9496 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9497
9498 if (attributes)
9499 TYPE_ATTRIBUTES (t)
9500 = tree_cons (TREE_PURPOSE (attributes),
9501 TREE_VALUE (attributes),
9502 TYPE_ATTRIBUTES (t));
9503 }
9504 }
9505
9506 /* Let's consider the explicit specialization of a member
9507 of a class template specialization that is implicitly instantiated,
9508 e.g.:
9509 template<class T>
9510 struct S
9511 {
9512 template<class U> struct M {}; //#0
9513 };
9514
9515 template<>
9516 template<>
9517 struct S<int>::M<char> //#1
9518 {
9519 int i;
9520 };
9521 [temp.expl.spec]/4 says this is valid.
9522
9523 In this case, when we write:
9524 S<int>::M<char> m;
9525
9526 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9527 the one of #0.
9528
9529 When we encounter #1, we want to store the partial instantiation
9530 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9531
9532 For all cases other than this "explicit specialization of member of a
9533 class template", we just want to store the most general template into
9534 the CLASSTYPE_TI_TEMPLATE of M.
9535
9536 This case of "explicit specialization of member of a class template"
9537 only happens when:
9538 1/ the enclosing class is an instantiation of, and therefore not
9539 the same as, the context of the most general template, and
9540 2/ we aren't looking at the partial instantiation itself, i.e.
9541 the innermost arguments are not the same as the innermost parms of
9542 the most general template.
9543
9544 So it's only when 1/ and 2/ happens that we want to use the partial
9545 instantiation of the member template in lieu of its most general
9546 template. */
9547
9548 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9549 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9550 /* the enclosing class must be an instantiation... */
9551 && CLASS_TYPE_P (context)
9552 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9553 {
9554 TREE_VEC_LENGTH (arglist)--;
9555 ++processing_template_decl;
9556 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9557 tree partial_inst_args =
9558 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9559 arglist, complain, NULL_TREE);
9560 --processing_template_decl;
9561 TREE_VEC_LENGTH (arglist)++;
9562 if (partial_inst_args == error_mark_node)
9563 return error_mark_node;
9564 use_partial_inst_tmpl =
9565 /*...and we must not be looking at the partial instantiation
9566 itself. */
9567 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9568 partial_inst_args);
9569 }
9570
9571 if (!use_partial_inst_tmpl)
9572 /* This case is easy; there are no member templates involved. */
9573 found = gen_tmpl;
9574 else
9575 {
9576 /* This is a full instantiation of a member template. Find
9577 the partial instantiation of which this is an instance. */
9578
9579 /* Temporarily reduce by one the number of levels in the ARGLIST
9580 so as to avoid comparing the last set of arguments. */
9581 TREE_VEC_LENGTH (arglist)--;
9582 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9583 TREE_VEC_LENGTH (arglist)++;
9584 /* FOUND is either a proper class type, or an alias
9585 template specialization. In the later case, it's a
9586 TYPE_DECL, resulting from the substituting of arguments
9587 for parameters in the TYPE_DECL of the alias template
9588 done earlier. So be careful while getting the template
9589 of FOUND. */
9590 found = (TREE_CODE (found) == TEMPLATE_DECL
9591 ? found
9592 : (TREE_CODE (found) == TYPE_DECL
9593 ? DECL_TI_TEMPLATE (found)
9594 : CLASSTYPE_TI_TEMPLATE (found)));
9595 }
9596
9597 // Build template info for the new specialization.
9598 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9599
9600 elt.spec = t;
9601 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9602 entry = ggc_alloc<spec_entry> ();
9603 *entry = elt;
9604 *slot = entry;
9605
9606 /* Note this use of the partial instantiation so we can check it
9607 later in maybe_process_partial_specialization. */
9608 DECL_TEMPLATE_INSTANTIATIONS (found)
9609 = tree_cons (arglist, t,
9610 DECL_TEMPLATE_INSTANTIATIONS (found));
9611
9612 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9613 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9614 /* Now that the type has been registered on the instantiations
9615 list, we set up the enumerators. Because the enumeration
9616 constants may involve the enumeration type itself, we make
9617 sure to register the type first, and then create the
9618 constants. That way, doing tsubst_expr for the enumeration
9619 constants won't result in recursive calls here; we'll find
9620 the instantiation and exit above. */
9621 tsubst_enum (template_type, t, arglist);
9622
9623 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9624 /* If the type makes use of template parameters, the
9625 code that generates debugging information will crash. */
9626 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9627
9628 /* Possibly limit visibility based on template args. */
9629 TREE_PUBLIC (type_decl) = 1;
9630 determine_visibility (type_decl);
9631
9632 inherit_targ_abi_tags (t);
9633
9634 return t;
9635 }
9636 }
9637
9638 /* Wrapper for lookup_template_class_1. */
9639
9640 tree
9641 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9642 int entering_scope, tsubst_flags_t complain)
9643 {
9644 tree ret;
9645 timevar_push (TV_TEMPLATE_INST);
9646 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9647 entering_scope, complain);
9648 timevar_pop (TV_TEMPLATE_INST);
9649 return ret;
9650 }
9651
9652 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9653
9654 tree
9655 lookup_template_variable (tree templ, tree arglist)
9656 {
9657 /* The type of the expression is NULL_TREE since the template-id could refer
9658 to an explicit or partial specialization. */
9659 tree type = NULL_TREE;
9660 if (flag_concepts && variable_concept_p (templ))
9661 /* Except that concepts are always bool. */
9662 type = boolean_type_node;
9663 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9664 }
9665
9666 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9667
9668 tree
9669 finish_template_variable (tree var, tsubst_flags_t complain)
9670 {
9671 tree templ = TREE_OPERAND (var, 0);
9672 tree arglist = TREE_OPERAND (var, 1);
9673
9674 /* We never want to return a VAR_DECL for a variable concept, since they
9675 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9676 bool concept_p = flag_concepts && variable_concept_p (templ);
9677 if (concept_p && processing_template_decl)
9678 return var;
9679
9680 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9681 arglist = add_outermost_template_args (tmpl_args, arglist);
9682
9683 templ = most_general_template (templ);
9684 tree parms = DECL_TEMPLATE_PARMS (templ);
9685 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9686 /*req_all*/true,
9687 /*use_default*/true);
9688
9689 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9690 {
9691 if (complain & tf_error)
9692 {
9693 error ("use of invalid variable template %qE", var);
9694 diagnose_constraints (location_of (var), templ, arglist);
9695 }
9696 return error_mark_node;
9697 }
9698
9699 /* If a template-id refers to a specialization of a variable
9700 concept, then the expression is true if and only if the
9701 concept's constraints are satisfied by the given template
9702 arguments.
9703
9704 NOTE: This is an extension of Concepts Lite TS that
9705 allows constraints to be used in expressions. */
9706 if (concept_p)
9707 {
9708 tree decl = DECL_TEMPLATE_RESULT (templ);
9709 return evaluate_variable_concept (decl, arglist);
9710 }
9711
9712 return instantiate_template (templ, arglist, complain);
9713 }
9714
9715 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9716 TARGS template args, and instantiate it if it's not dependent. */
9717
9718 tree
9719 lookup_and_finish_template_variable (tree templ, tree targs,
9720 tsubst_flags_t complain)
9721 {
9722 templ = lookup_template_variable (templ, targs);
9723 if (!any_dependent_template_arguments_p (targs))
9724 {
9725 templ = finish_template_variable (templ, complain);
9726 mark_used (templ);
9727 }
9728
9729 return convert_from_reference (templ);
9730 }
9731
9732 \f
9733 struct pair_fn_data
9734 {
9735 tree_fn_t fn;
9736 tree_fn_t any_fn;
9737 void *data;
9738 /* True when we should also visit template parameters that occur in
9739 non-deduced contexts. */
9740 bool include_nondeduced_p;
9741 hash_set<tree> *visited;
9742 };
9743
9744 /* Called from for_each_template_parm via walk_tree. */
9745
9746 static tree
9747 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9748 {
9749 tree t = *tp;
9750 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9751 tree_fn_t fn = pfd->fn;
9752 void *data = pfd->data;
9753 tree result = NULL_TREE;
9754
9755 #define WALK_SUBTREE(NODE) \
9756 do \
9757 { \
9758 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9759 pfd->include_nondeduced_p, \
9760 pfd->any_fn); \
9761 if (result) goto out; \
9762 } \
9763 while (0)
9764
9765 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9766 return t;
9767
9768 if (TYPE_P (t)
9769 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9770 WALK_SUBTREE (TYPE_CONTEXT (t));
9771
9772 switch (TREE_CODE (t))
9773 {
9774 case RECORD_TYPE:
9775 if (TYPE_PTRMEMFUNC_P (t))
9776 break;
9777 /* Fall through. */
9778
9779 case UNION_TYPE:
9780 case ENUMERAL_TYPE:
9781 if (!TYPE_TEMPLATE_INFO (t))
9782 *walk_subtrees = 0;
9783 else
9784 WALK_SUBTREE (TYPE_TI_ARGS (t));
9785 break;
9786
9787 case INTEGER_TYPE:
9788 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9789 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9790 break;
9791
9792 case METHOD_TYPE:
9793 /* Since we're not going to walk subtrees, we have to do this
9794 explicitly here. */
9795 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9796 /* Fall through. */
9797
9798 case FUNCTION_TYPE:
9799 /* Check the return type. */
9800 WALK_SUBTREE (TREE_TYPE (t));
9801
9802 /* Check the parameter types. Since default arguments are not
9803 instantiated until they are needed, the TYPE_ARG_TYPES may
9804 contain expressions that involve template parameters. But,
9805 no-one should be looking at them yet. And, once they're
9806 instantiated, they don't contain template parameters, so
9807 there's no point in looking at them then, either. */
9808 {
9809 tree parm;
9810
9811 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9812 WALK_SUBTREE (TREE_VALUE (parm));
9813
9814 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9815 want walk_tree walking into them itself. */
9816 *walk_subtrees = 0;
9817 }
9818
9819 if (flag_noexcept_type)
9820 {
9821 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9822 if (spec)
9823 WALK_SUBTREE (TREE_PURPOSE (spec));
9824 }
9825 break;
9826
9827 case TYPEOF_TYPE:
9828 case DECLTYPE_TYPE:
9829 case UNDERLYING_TYPE:
9830 if (pfd->include_nondeduced_p
9831 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9832 pfd->visited,
9833 pfd->include_nondeduced_p,
9834 pfd->any_fn))
9835 return error_mark_node;
9836 *walk_subtrees = false;
9837 break;
9838
9839 case FUNCTION_DECL:
9840 case VAR_DECL:
9841 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9842 WALK_SUBTREE (DECL_TI_ARGS (t));
9843 /* Fall through. */
9844
9845 case PARM_DECL:
9846 case CONST_DECL:
9847 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9848 WALK_SUBTREE (DECL_INITIAL (t));
9849 if (DECL_CONTEXT (t)
9850 && pfd->include_nondeduced_p)
9851 WALK_SUBTREE (DECL_CONTEXT (t));
9852 break;
9853
9854 case BOUND_TEMPLATE_TEMPLATE_PARM:
9855 /* Record template parameters such as `T' inside `TT<T>'. */
9856 WALK_SUBTREE (TYPE_TI_ARGS (t));
9857 /* Fall through. */
9858
9859 case TEMPLATE_TEMPLATE_PARM:
9860 case TEMPLATE_TYPE_PARM:
9861 case TEMPLATE_PARM_INDEX:
9862 if (fn && (*fn)(t, data))
9863 return t;
9864 else if (!fn)
9865 return t;
9866 break;
9867
9868 case TEMPLATE_DECL:
9869 /* A template template parameter is encountered. */
9870 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9871 WALK_SUBTREE (TREE_TYPE (t));
9872
9873 /* Already substituted template template parameter */
9874 *walk_subtrees = 0;
9875 break;
9876
9877 case TYPENAME_TYPE:
9878 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9879 partial instantiation. */
9880 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9881 break;
9882
9883 case CONSTRUCTOR:
9884 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9885 && pfd->include_nondeduced_p)
9886 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9887 break;
9888
9889 case INDIRECT_REF:
9890 case COMPONENT_REF:
9891 /* If there's no type, then this thing must be some expression
9892 involving template parameters. */
9893 if (!fn && !TREE_TYPE (t))
9894 return error_mark_node;
9895 break;
9896
9897 case MODOP_EXPR:
9898 case CAST_EXPR:
9899 case IMPLICIT_CONV_EXPR:
9900 case REINTERPRET_CAST_EXPR:
9901 case CONST_CAST_EXPR:
9902 case STATIC_CAST_EXPR:
9903 case DYNAMIC_CAST_EXPR:
9904 case ARROW_EXPR:
9905 case DOTSTAR_EXPR:
9906 case TYPEID_EXPR:
9907 case PSEUDO_DTOR_EXPR:
9908 if (!fn)
9909 return error_mark_node;
9910 break;
9911
9912 default:
9913 break;
9914 }
9915
9916 #undef WALK_SUBTREE
9917
9918 /* We didn't find any template parameters we liked. */
9919 out:
9920 return result;
9921 }
9922
9923 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9924 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9925 call FN with the parameter and the DATA.
9926 If FN returns nonzero, the iteration is terminated, and
9927 for_each_template_parm returns 1. Otherwise, the iteration
9928 continues. If FN never returns a nonzero value, the value
9929 returned by for_each_template_parm is 0. If FN is NULL, it is
9930 considered to be the function which always returns 1.
9931
9932 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9933 parameters that occur in non-deduced contexts. When false, only
9934 visits those template parameters that can be deduced. */
9935
9936 static tree
9937 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9938 hash_set<tree> *visited,
9939 bool include_nondeduced_p,
9940 tree_fn_t any_fn)
9941 {
9942 struct pair_fn_data pfd;
9943 tree result;
9944
9945 /* Set up. */
9946 pfd.fn = fn;
9947 pfd.any_fn = any_fn;
9948 pfd.data = data;
9949 pfd.include_nondeduced_p = include_nondeduced_p;
9950
9951 /* Walk the tree. (Conceptually, we would like to walk without
9952 duplicates, but for_each_template_parm_r recursively calls
9953 for_each_template_parm, so we would need to reorganize a fair
9954 bit to use walk_tree_without_duplicates, so we keep our own
9955 visited list.) */
9956 if (visited)
9957 pfd.visited = visited;
9958 else
9959 pfd.visited = new hash_set<tree>;
9960 result = cp_walk_tree (&t,
9961 for_each_template_parm_r,
9962 &pfd,
9963 pfd.visited);
9964
9965 /* Clean up. */
9966 if (!visited)
9967 {
9968 delete pfd.visited;
9969 pfd.visited = 0;
9970 }
9971
9972 return result;
9973 }
9974
9975 /* Returns true if T depends on any template parameter. */
9976
9977 int
9978 uses_template_parms (tree t)
9979 {
9980 if (t == NULL_TREE)
9981 return false;
9982
9983 bool dependent_p;
9984 int saved_processing_template_decl;
9985
9986 saved_processing_template_decl = processing_template_decl;
9987 if (!saved_processing_template_decl)
9988 processing_template_decl = 1;
9989 if (TYPE_P (t))
9990 dependent_p = dependent_type_p (t);
9991 else if (TREE_CODE (t) == TREE_VEC)
9992 dependent_p = any_dependent_template_arguments_p (t);
9993 else if (TREE_CODE (t) == TREE_LIST)
9994 dependent_p = (uses_template_parms (TREE_VALUE (t))
9995 || uses_template_parms (TREE_CHAIN (t)));
9996 else if (TREE_CODE (t) == TYPE_DECL)
9997 dependent_p = dependent_type_p (TREE_TYPE (t));
9998 else if (DECL_P (t)
9999 || EXPR_P (t)
10000 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10001 || TREE_CODE (t) == OVERLOAD
10002 || BASELINK_P (t)
10003 || identifier_p (t)
10004 || TREE_CODE (t) == TRAIT_EXPR
10005 || TREE_CODE (t) == CONSTRUCTOR
10006 || CONSTANT_CLASS_P (t))
10007 dependent_p = (type_dependent_expression_p (t)
10008 || value_dependent_expression_p (t));
10009 else
10010 {
10011 gcc_assert (t == error_mark_node);
10012 dependent_p = false;
10013 }
10014
10015 processing_template_decl = saved_processing_template_decl;
10016
10017 return dependent_p;
10018 }
10019
10020 /* Returns true iff current_function_decl is an incompletely instantiated
10021 template. Useful instead of processing_template_decl because the latter
10022 is set to 0 during instantiate_non_dependent_expr. */
10023
10024 bool
10025 in_template_function (void)
10026 {
10027 tree fn = current_function_decl;
10028 bool ret;
10029 ++processing_template_decl;
10030 ret = (fn && DECL_LANG_SPECIFIC (fn)
10031 && DECL_TEMPLATE_INFO (fn)
10032 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10033 --processing_template_decl;
10034 return ret;
10035 }
10036
10037 /* Returns true if T depends on any template parameter with level LEVEL. */
10038
10039 bool
10040 uses_template_parms_level (tree t, int level)
10041 {
10042 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10043 /*include_nondeduced_p=*/true);
10044 }
10045
10046 /* Returns true if the signature of DECL depends on any template parameter from
10047 its enclosing class. */
10048
10049 bool
10050 uses_outer_template_parms (tree decl)
10051 {
10052 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10053 if (depth == 0)
10054 return false;
10055 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10056 &depth, NULL, /*include_nondeduced_p=*/true))
10057 return true;
10058 if (PRIMARY_TEMPLATE_P (decl)
10059 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10060 (DECL_TEMPLATE_PARMS (decl)),
10061 template_parm_outer_level,
10062 &depth, NULL, /*include_nondeduced_p=*/true))
10063 return true;
10064 tree ci = get_constraints (decl);
10065 if (ci)
10066 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10067 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10068 &depth, NULL, /*nondeduced*/true))
10069 return true;
10070 return false;
10071 }
10072
10073 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10074 ill-formed translation unit, i.e. a variable or function that isn't
10075 usable in a constant expression. */
10076
10077 static inline bool
10078 neglectable_inst_p (tree d)
10079 {
10080 return (d && DECL_P (d)
10081 && !undeduced_auto_decl (d)
10082 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10083 : decl_maybe_constant_var_p (d)));
10084 }
10085
10086 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10087 neglectable and instantiated from within an erroneous instantiation. */
10088
10089 static bool
10090 limit_bad_template_recursion (tree decl)
10091 {
10092 struct tinst_level *lev = current_tinst_level;
10093 int errs = errorcount + sorrycount;
10094 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10095 return false;
10096
10097 for (; lev; lev = lev->next)
10098 if (neglectable_inst_p (lev->maybe_get_node ()))
10099 break;
10100
10101 return (lev && errs > lev->errors);
10102 }
10103
10104 static int tinst_depth;
10105 extern int max_tinst_depth;
10106 int depth_reached;
10107
10108 static GTY(()) struct tinst_level *last_error_tinst_level;
10109
10110 /* We're starting to instantiate D; record the template instantiation context
10111 at LOC for diagnostics and to restore it later. */
10112
10113 static bool
10114 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10115 {
10116 struct tinst_level *new_level;
10117
10118 if (tinst_depth >= max_tinst_depth)
10119 {
10120 /* Tell error.c not to try to instantiate any templates. */
10121 at_eof = 2;
10122 fatal_error (input_location,
10123 "template instantiation depth exceeds maximum of %d"
10124 " (use -ftemplate-depth= to increase the maximum)",
10125 max_tinst_depth);
10126 return false;
10127 }
10128
10129 /* If the current instantiation caused problems, don't let it instantiate
10130 anything else. Do allow deduction substitution and decls usable in
10131 constant expressions. */
10132 if (!targs && limit_bad_template_recursion (tldcl))
10133 return false;
10134
10135 /* When not -quiet, dump template instantiations other than functions, since
10136 announce_function will take care of those. */
10137 if (!quiet_flag && !targs
10138 && TREE_CODE (tldcl) != TREE_LIST
10139 && TREE_CODE (tldcl) != FUNCTION_DECL)
10140 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10141
10142 new_level = tinst_level_freelist ().alloc ();
10143 new_level->tldcl = tldcl;
10144 new_level->targs = targs;
10145 new_level->locus = loc;
10146 new_level->errors = errorcount + sorrycount;
10147 new_level->next = NULL;
10148 new_level->refcount = 0;
10149 set_refcount_ptr (new_level->next, current_tinst_level);
10150 set_refcount_ptr (current_tinst_level, new_level);
10151
10152 ++tinst_depth;
10153 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10154 depth_reached = tinst_depth;
10155
10156 return true;
10157 }
10158
10159 /* We're starting substitution of TMPL<ARGS>; record the template
10160 substitution context for diagnostics and to restore it later. */
10161
10162 static bool
10163 push_tinst_level (tree tmpl, tree args)
10164 {
10165 return push_tinst_level_loc (tmpl, args, input_location);
10166 }
10167
10168 /* We're starting to instantiate D; record INPUT_LOCATION and the
10169 template instantiation context for diagnostics and to restore it
10170 later. */
10171
10172 bool
10173 push_tinst_level (tree d)
10174 {
10175 return push_tinst_level_loc (d, input_location);
10176 }
10177
10178 /* Likewise, but record LOC as the program location. */
10179
10180 bool
10181 push_tinst_level_loc (tree d, location_t loc)
10182 {
10183 gcc_assert (TREE_CODE (d) != TREE_LIST);
10184 return push_tinst_level_loc (d, NULL, loc);
10185 }
10186
10187 /* We're done instantiating this template; return to the instantiation
10188 context. */
10189
10190 void
10191 pop_tinst_level (void)
10192 {
10193 /* Restore the filename and line number stashed away when we started
10194 this instantiation. */
10195 input_location = current_tinst_level->locus;
10196 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10197 --tinst_depth;
10198 }
10199
10200 /* We're instantiating a deferred template; restore the template
10201 instantiation context in which the instantiation was requested, which
10202 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10203
10204 static tree
10205 reopen_tinst_level (struct tinst_level *level)
10206 {
10207 struct tinst_level *t;
10208
10209 tinst_depth = 0;
10210 for (t = level; t; t = t->next)
10211 ++tinst_depth;
10212
10213 set_refcount_ptr (current_tinst_level, level);
10214 pop_tinst_level ();
10215 if (current_tinst_level)
10216 current_tinst_level->errors = errorcount+sorrycount;
10217 return level->maybe_get_node ();
10218 }
10219
10220 /* Returns the TINST_LEVEL which gives the original instantiation
10221 context. */
10222
10223 struct tinst_level *
10224 outermost_tinst_level (void)
10225 {
10226 struct tinst_level *level = current_tinst_level;
10227 if (level)
10228 while (level->next)
10229 level = level->next;
10230 return level;
10231 }
10232
10233 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10234 vector of template arguments, as for tsubst.
10235
10236 Returns an appropriate tsubst'd friend declaration. */
10237
10238 static tree
10239 tsubst_friend_function (tree decl, tree args)
10240 {
10241 tree new_friend;
10242
10243 if (TREE_CODE (decl) == FUNCTION_DECL
10244 && DECL_TEMPLATE_INSTANTIATION (decl)
10245 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10246 /* This was a friend declared with an explicit template
10247 argument list, e.g.:
10248
10249 friend void f<>(T);
10250
10251 to indicate that f was a template instantiation, not a new
10252 function declaration. Now, we have to figure out what
10253 instantiation of what template. */
10254 {
10255 tree template_id, arglist, fns;
10256 tree new_args;
10257 tree tmpl;
10258 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10259
10260 /* Friend functions are looked up in the containing namespace scope.
10261 We must enter that scope, to avoid finding member functions of the
10262 current class with same name. */
10263 push_nested_namespace (ns);
10264 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10265 tf_warning_or_error, NULL_TREE,
10266 /*integral_constant_expression_p=*/false);
10267 pop_nested_namespace (ns);
10268 arglist = tsubst (DECL_TI_ARGS (decl), args,
10269 tf_warning_or_error, NULL_TREE);
10270 template_id = lookup_template_function (fns, arglist);
10271
10272 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10273 tmpl = determine_specialization (template_id, new_friend,
10274 &new_args,
10275 /*need_member_template=*/0,
10276 TREE_VEC_LENGTH (args),
10277 tsk_none);
10278 return instantiate_template (tmpl, new_args, tf_error);
10279 }
10280
10281 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10282
10283 /* The NEW_FRIEND will look like an instantiation, to the
10284 compiler, but is not an instantiation from the point of view of
10285 the language. For example, we might have had:
10286
10287 template <class T> struct S {
10288 template <class U> friend void f(T, U);
10289 };
10290
10291 Then, in S<int>, template <class U> void f(int, U) is not an
10292 instantiation of anything. */
10293 if (new_friend == error_mark_node)
10294 return error_mark_node;
10295
10296 DECL_USE_TEMPLATE (new_friend) = 0;
10297 if (TREE_CODE (decl) == TEMPLATE_DECL)
10298 {
10299 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10300 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10301 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10302 }
10303
10304 /* The mangled name for the NEW_FRIEND is incorrect. The function
10305 is not a template instantiation and should not be mangled like
10306 one. Therefore, we forget the mangling here; we'll recompute it
10307 later if we need it. */
10308 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10309 {
10310 SET_DECL_RTL (new_friend, NULL);
10311 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10312 }
10313
10314 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10315 {
10316 tree old_decl;
10317 tree new_friend_template_info;
10318 tree new_friend_result_template_info;
10319 tree ns;
10320 int new_friend_is_defn;
10321
10322 /* We must save some information from NEW_FRIEND before calling
10323 duplicate decls since that function will free NEW_FRIEND if
10324 possible. */
10325 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10326 new_friend_is_defn =
10327 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10328 (template_for_substitution (new_friend)))
10329 != NULL_TREE);
10330 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10331 {
10332 /* This declaration is a `primary' template. */
10333 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10334
10335 new_friend_result_template_info
10336 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10337 }
10338 else
10339 new_friend_result_template_info = NULL_TREE;
10340
10341 /* Inside pushdecl_namespace_level, we will push into the
10342 current namespace. However, the friend function should go
10343 into the namespace of the template. */
10344 ns = decl_namespace_context (new_friend);
10345 push_nested_namespace (ns);
10346 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10347 pop_nested_namespace (ns);
10348
10349 if (old_decl == error_mark_node)
10350 return error_mark_node;
10351
10352 if (old_decl != new_friend)
10353 {
10354 /* This new friend declaration matched an existing
10355 declaration. For example, given:
10356
10357 template <class T> void f(T);
10358 template <class U> class C {
10359 template <class T> friend void f(T) {}
10360 };
10361
10362 the friend declaration actually provides the definition
10363 of `f', once C has been instantiated for some type. So,
10364 old_decl will be the out-of-class template declaration,
10365 while new_friend is the in-class definition.
10366
10367 But, if `f' was called before this point, the
10368 instantiation of `f' will have DECL_TI_ARGS corresponding
10369 to `T' but not to `U', references to which might appear
10370 in the definition of `f'. Previously, the most general
10371 template for an instantiation of `f' was the out-of-class
10372 version; now it is the in-class version. Therefore, we
10373 run through all specialization of `f', adding to their
10374 DECL_TI_ARGS appropriately. In particular, they need a
10375 new set of outer arguments, corresponding to the
10376 arguments for this class instantiation.
10377
10378 The same situation can arise with something like this:
10379
10380 friend void f(int);
10381 template <class T> class C {
10382 friend void f(T) {}
10383 };
10384
10385 when `C<int>' is instantiated. Now, `f(int)' is defined
10386 in the class. */
10387
10388 if (!new_friend_is_defn)
10389 /* On the other hand, if the in-class declaration does
10390 *not* provide a definition, then we don't want to alter
10391 existing definitions. We can just leave everything
10392 alone. */
10393 ;
10394 else
10395 {
10396 tree new_template = TI_TEMPLATE (new_friend_template_info);
10397 tree new_args = TI_ARGS (new_friend_template_info);
10398
10399 /* Overwrite whatever template info was there before, if
10400 any, with the new template information pertaining to
10401 the declaration. */
10402 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10403
10404 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10405 {
10406 /* We should have called reregister_specialization in
10407 duplicate_decls. */
10408 gcc_assert (retrieve_specialization (new_template,
10409 new_args, 0)
10410 == old_decl);
10411
10412 /* Instantiate it if the global has already been used. */
10413 if (DECL_ODR_USED (old_decl))
10414 instantiate_decl (old_decl, /*defer_ok=*/true,
10415 /*expl_inst_class_mem_p=*/false);
10416 }
10417 else
10418 {
10419 tree t;
10420
10421 /* Indicate that the old function template is a partial
10422 instantiation. */
10423 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10424 = new_friend_result_template_info;
10425
10426 gcc_assert (new_template
10427 == most_general_template (new_template));
10428 gcc_assert (new_template != old_decl);
10429
10430 /* Reassign any specializations already in the hash table
10431 to the new more general template, and add the
10432 additional template args. */
10433 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10434 t != NULL_TREE;
10435 t = TREE_CHAIN (t))
10436 {
10437 tree spec = TREE_VALUE (t);
10438 spec_entry elt;
10439
10440 elt.tmpl = old_decl;
10441 elt.args = DECL_TI_ARGS (spec);
10442 elt.spec = NULL_TREE;
10443
10444 decl_specializations->remove_elt (&elt);
10445
10446 DECL_TI_ARGS (spec)
10447 = add_outermost_template_args (new_args,
10448 DECL_TI_ARGS (spec));
10449
10450 register_specialization
10451 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10452
10453 }
10454 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10455 }
10456 }
10457
10458 /* The information from NEW_FRIEND has been merged into OLD_DECL
10459 by duplicate_decls. */
10460 new_friend = old_decl;
10461 }
10462 }
10463 else
10464 {
10465 tree context = DECL_CONTEXT (new_friend);
10466 bool dependent_p;
10467
10468 /* In the code
10469 template <class T> class C {
10470 template <class U> friend void C1<U>::f (); // case 1
10471 friend void C2<T>::f (); // case 2
10472 };
10473 we only need to make sure CONTEXT is a complete type for
10474 case 2. To distinguish between the two cases, we note that
10475 CONTEXT of case 1 remains dependent type after tsubst while
10476 this isn't true for case 2. */
10477 ++processing_template_decl;
10478 dependent_p = dependent_type_p (context);
10479 --processing_template_decl;
10480
10481 if (!dependent_p
10482 && !complete_type_or_else (context, NULL_TREE))
10483 return error_mark_node;
10484
10485 if (COMPLETE_TYPE_P (context))
10486 {
10487 tree fn = new_friend;
10488 /* do_friend adds the TEMPLATE_DECL for any member friend
10489 template even if it isn't a member template, i.e.
10490 template <class T> friend A<T>::f();
10491 Look through it in that case. */
10492 if (TREE_CODE (fn) == TEMPLATE_DECL
10493 && !PRIMARY_TEMPLATE_P (fn))
10494 fn = DECL_TEMPLATE_RESULT (fn);
10495 /* Check to see that the declaration is really present, and,
10496 possibly obtain an improved declaration. */
10497 fn = check_classfn (context, fn, NULL_TREE);
10498
10499 if (fn)
10500 new_friend = fn;
10501 }
10502 }
10503
10504 return new_friend;
10505 }
10506
10507 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10508 template arguments, as for tsubst.
10509
10510 Returns an appropriate tsubst'd friend type or error_mark_node on
10511 failure. */
10512
10513 static tree
10514 tsubst_friend_class (tree friend_tmpl, tree args)
10515 {
10516 tree tmpl;
10517
10518 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10519 {
10520 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10521 return TREE_TYPE (tmpl);
10522 }
10523
10524 tree context = CP_DECL_CONTEXT (friend_tmpl);
10525 if (TREE_CODE (context) == NAMESPACE_DECL)
10526 push_nested_namespace (context);
10527 else
10528 push_nested_class (context);
10529
10530 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10531 /*non_class=*/false, /*block_p=*/false,
10532 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10533
10534 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10535 {
10536 /* The friend template has already been declared. Just
10537 check to see that the declarations match, and install any new
10538 default parameters. We must tsubst the default parameters,
10539 of course. We only need the innermost template parameters
10540 because that is all that redeclare_class_template will look
10541 at. */
10542 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10543 > TMPL_ARGS_DEPTH (args))
10544 {
10545 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10546 args, tf_warning_or_error);
10547 location_t saved_input_location = input_location;
10548 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10549 tree cons = get_constraints (tmpl);
10550 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10551 input_location = saved_input_location;
10552 }
10553 }
10554 else
10555 {
10556 /* The friend template has not already been declared. In this
10557 case, the instantiation of the template class will cause the
10558 injection of this template into the namespace scope. */
10559 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10560
10561 if (tmpl != error_mark_node)
10562 {
10563 /* The new TMPL is not an instantiation of anything, so we
10564 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10565 for the new type because that is supposed to be the
10566 corresponding template decl, i.e., TMPL. */
10567 DECL_USE_TEMPLATE (tmpl) = 0;
10568 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10569 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10570 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10571 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10572
10573 /* It is hidden. */
10574 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10575 DECL_ANTICIPATED (tmpl)
10576 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10577
10578 /* Inject this template into the enclosing namspace scope. */
10579 tmpl = pushdecl_namespace_level (tmpl, true);
10580 }
10581 }
10582
10583 if (TREE_CODE (context) == NAMESPACE_DECL)
10584 pop_nested_namespace (context);
10585 else
10586 pop_nested_class ();
10587
10588 return TREE_TYPE (tmpl);
10589 }
10590
10591 /* Returns zero if TYPE cannot be completed later due to circularity.
10592 Otherwise returns one. */
10593
10594 static int
10595 can_complete_type_without_circularity (tree type)
10596 {
10597 if (type == NULL_TREE || type == error_mark_node)
10598 return 0;
10599 else if (COMPLETE_TYPE_P (type))
10600 return 1;
10601 else if (TREE_CODE (type) == ARRAY_TYPE)
10602 return can_complete_type_without_circularity (TREE_TYPE (type));
10603 else if (CLASS_TYPE_P (type)
10604 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10605 return 0;
10606 else
10607 return 1;
10608 }
10609
10610 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10611 tsubst_flags_t, tree);
10612
10613 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10614 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10615
10616 static tree
10617 tsubst_attribute (tree t, tree *decl_p, tree args,
10618 tsubst_flags_t complain, tree in_decl)
10619 {
10620 gcc_assert (ATTR_IS_DEPENDENT (t));
10621
10622 tree val = TREE_VALUE (t);
10623 if (val == NULL_TREE)
10624 /* Nothing to do. */;
10625 else if ((flag_openmp || flag_openmp_simd)
10626 && is_attribute_p ("omp declare simd",
10627 get_attribute_name (t)))
10628 {
10629 tree clauses = TREE_VALUE (val);
10630 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10631 complain, in_decl);
10632 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10633 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10634 tree parms = DECL_ARGUMENTS (*decl_p);
10635 clauses
10636 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10637 if (clauses)
10638 val = build_tree_list (NULL_TREE, clauses);
10639 else
10640 val = NULL_TREE;
10641 }
10642 /* If the first attribute argument is an identifier, don't
10643 pass it through tsubst. Attributes like mode, format,
10644 cleanup and several target specific attributes expect it
10645 unmodified. */
10646 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10647 {
10648 tree chain
10649 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10650 /*integral_constant_expression_p=*/false);
10651 if (chain != TREE_CHAIN (val))
10652 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10653 }
10654 else if (PACK_EXPANSION_P (val))
10655 {
10656 /* An attribute pack expansion. */
10657 tree purp = TREE_PURPOSE (t);
10658 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10659 if (pack == error_mark_node)
10660 return error_mark_node;
10661 int len = TREE_VEC_LENGTH (pack);
10662 tree list = NULL_TREE;
10663 tree *q = &list;
10664 for (int i = 0; i < len; ++i)
10665 {
10666 tree elt = TREE_VEC_ELT (pack, i);
10667 *q = build_tree_list (purp, elt);
10668 q = &TREE_CHAIN (*q);
10669 }
10670 return list;
10671 }
10672 else
10673 val = tsubst_expr (val, args, complain, in_decl,
10674 /*integral_constant_expression_p=*/false);
10675
10676 if (val != TREE_VALUE (t))
10677 return build_tree_list (TREE_PURPOSE (t), val);
10678 return t;
10679 }
10680
10681 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10682 unchanged or a new TREE_LIST chain. */
10683
10684 static tree
10685 tsubst_attributes (tree attributes, tree args,
10686 tsubst_flags_t complain, tree in_decl)
10687 {
10688 tree last_dep = NULL_TREE;
10689
10690 for (tree t = attributes; t; t = TREE_CHAIN (t))
10691 if (ATTR_IS_DEPENDENT (t))
10692 {
10693 last_dep = t;
10694 attributes = copy_list (attributes);
10695 break;
10696 }
10697
10698 if (last_dep)
10699 for (tree *p = &attributes; *p; )
10700 {
10701 tree t = *p;
10702 if (ATTR_IS_DEPENDENT (t))
10703 {
10704 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10705 if (subst != t)
10706 {
10707 *p = subst;
10708 while (*p)
10709 p = &TREE_CHAIN (*p);
10710 *p = TREE_CHAIN (t);
10711 continue;
10712 }
10713 }
10714 p = &TREE_CHAIN (*p);
10715 }
10716
10717 return attributes;
10718 }
10719
10720 /* Apply any attributes which had to be deferred until instantiation
10721 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10722 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10723
10724 static void
10725 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10726 tree args, tsubst_flags_t complain, tree in_decl)
10727 {
10728 tree last_dep = NULL_TREE;
10729 tree t;
10730 tree *p;
10731
10732 if (attributes == NULL_TREE)
10733 return;
10734
10735 if (DECL_P (*decl_p))
10736 {
10737 if (TREE_TYPE (*decl_p) == error_mark_node)
10738 return;
10739 p = &DECL_ATTRIBUTES (*decl_p);
10740 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10741 to our attributes parameter. */
10742 gcc_assert (*p == attributes);
10743 }
10744 else
10745 {
10746 p = &TYPE_ATTRIBUTES (*decl_p);
10747 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10748 lookup_template_class_1, and should be preserved. */
10749 gcc_assert (*p != attributes);
10750 while (*p)
10751 p = &TREE_CHAIN (*p);
10752 }
10753
10754 for (t = attributes; t; t = TREE_CHAIN (t))
10755 if (ATTR_IS_DEPENDENT (t))
10756 {
10757 last_dep = t;
10758 attributes = copy_list (attributes);
10759 break;
10760 }
10761
10762 *p = attributes;
10763 if (last_dep)
10764 {
10765 tree late_attrs = NULL_TREE;
10766 tree *q = &late_attrs;
10767
10768 for (; *p; )
10769 {
10770 t = *p;
10771 if (ATTR_IS_DEPENDENT (t))
10772 {
10773 *p = TREE_CHAIN (t);
10774 TREE_CHAIN (t) = NULL_TREE;
10775 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10776 while (*q)
10777 q = &TREE_CHAIN (*q);
10778 }
10779 else
10780 p = &TREE_CHAIN (t);
10781 }
10782
10783 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10784 }
10785 }
10786
10787 /* Perform (or defer) access check for typedefs that were referenced
10788 from within the template TMPL code.
10789 This is a subroutine of instantiate_decl and instantiate_class_template.
10790 TMPL is the template to consider and TARGS is the list of arguments of
10791 that template. */
10792
10793 static void
10794 perform_typedefs_access_check (tree tmpl, tree targs)
10795 {
10796 location_t saved_location;
10797 unsigned i;
10798 qualified_typedef_usage_t *iter;
10799
10800 if (!tmpl
10801 || (!CLASS_TYPE_P (tmpl)
10802 && TREE_CODE (tmpl) != FUNCTION_DECL))
10803 return;
10804
10805 saved_location = input_location;
10806 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10807 {
10808 tree type_decl = iter->typedef_decl;
10809 tree type_scope = iter->context;
10810
10811 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10812 continue;
10813
10814 if (uses_template_parms (type_decl))
10815 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10816 if (uses_template_parms (type_scope))
10817 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10818
10819 /* Make access check error messages point to the location
10820 of the use of the typedef. */
10821 input_location = iter->locus;
10822 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10823 type_decl, type_decl,
10824 tf_warning_or_error);
10825 }
10826 input_location = saved_location;
10827 }
10828
10829 static tree
10830 instantiate_class_template_1 (tree type)
10831 {
10832 tree templ, args, pattern, t, member;
10833 tree typedecl;
10834 tree pbinfo;
10835 tree base_list;
10836 unsigned int saved_maximum_field_alignment;
10837 tree fn_context;
10838
10839 if (type == error_mark_node)
10840 return error_mark_node;
10841
10842 if (COMPLETE_OR_OPEN_TYPE_P (type)
10843 || uses_template_parms (type))
10844 return type;
10845
10846 /* Figure out which template is being instantiated. */
10847 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10848 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10849
10850 /* Mark the type as in the process of being defined. */
10851 TYPE_BEING_DEFINED (type) = 1;
10852
10853 /* Determine what specialization of the original template to
10854 instantiate. */
10855 t = most_specialized_partial_spec (type, tf_warning_or_error);
10856 if (t == error_mark_node)
10857 return error_mark_node;
10858 else if (t)
10859 {
10860 /* This TYPE is actually an instantiation of a partial
10861 specialization. We replace the innermost set of ARGS with
10862 the arguments appropriate for substitution. For example,
10863 given:
10864
10865 template <class T> struct S {};
10866 template <class T> struct S<T*> {};
10867
10868 and supposing that we are instantiating S<int*>, ARGS will
10869 presently be {int*} -- but we need {int}. */
10870 pattern = TREE_TYPE (t);
10871 args = TREE_PURPOSE (t);
10872 }
10873 else
10874 {
10875 pattern = TREE_TYPE (templ);
10876 args = CLASSTYPE_TI_ARGS (type);
10877 }
10878
10879 /* If the template we're instantiating is incomplete, then clearly
10880 there's nothing we can do. */
10881 if (!COMPLETE_TYPE_P (pattern))
10882 {
10883 /* We can try again later. */
10884 TYPE_BEING_DEFINED (type) = 0;
10885 return type;
10886 }
10887
10888 /* If we've recursively instantiated too many templates, stop. */
10889 if (! push_tinst_level (type))
10890 return type;
10891
10892 /* We may be in the middle of deferred access check. Disable
10893 it now. */
10894 push_deferring_access_checks (dk_no_deferred);
10895
10896 int saved_unevaluated_operand = cp_unevaluated_operand;
10897 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10898
10899 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10900 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10901 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10902 fn_context = error_mark_node;
10903 if (!fn_context)
10904 push_to_top_level ();
10905 else
10906 {
10907 cp_unevaluated_operand = 0;
10908 c_inhibit_evaluation_warnings = 0;
10909 }
10910 /* Use #pragma pack from the template context. */
10911 saved_maximum_field_alignment = maximum_field_alignment;
10912 maximum_field_alignment = TYPE_PRECISION (pattern);
10913
10914 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10915
10916 /* Set the input location to the most specialized template definition.
10917 This is needed if tsubsting causes an error. */
10918 typedecl = TYPE_MAIN_DECL (pattern);
10919 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10920 DECL_SOURCE_LOCATION (typedecl);
10921
10922 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10923 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10924 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10925 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10926 if (ANON_AGGR_TYPE_P (pattern))
10927 SET_ANON_AGGR_TYPE_P (type);
10928 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10929 {
10930 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10931 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10932 /* Adjust visibility for template arguments. */
10933 determine_visibility (TYPE_MAIN_DECL (type));
10934 }
10935 if (CLASS_TYPE_P (type))
10936 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10937
10938 pbinfo = TYPE_BINFO (pattern);
10939
10940 /* We should never instantiate a nested class before its enclosing
10941 class; we need to look up the nested class by name before we can
10942 instantiate it, and that lookup should instantiate the enclosing
10943 class. */
10944 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10945 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10946
10947 base_list = NULL_TREE;
10948 if (BINFO_N_BASE_BINFOS (pbinfo))
10949 {
10950 tree pbase_binfo;
10951 tree pushed_scope;
10952 int i;
10953
10954 /* We must enter the scope containing the type, as that is where
10955 the accessibility of types named in dependent bases are
10956 looked up from. */
10957 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10958
10959 /* Substitute into each of the bases to determine the actual
10960 basetypes. */
10961 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10962 {
10963 tree base;
10964 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10965 tree expanded_bases = NULL_TREE;
10966 int idx, len = 1;
10967
10968 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10969 {
10970 expanded_bases =
10971 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10972 args, tf_error, NULL_TREE);
10973 if (expanded_bases == error_mark_node)
10974 continue;
10975
10976 len = TREE_VEC_LENGTH (expanded_bases);
10977 }
10978
10979 for (idx = 0; idx < len; idx++)
10980 {
10981 if (expanded_bases)
10982 /* Extract the already-expanded base class. */
10983 base = TREE_VEC_ELT (expanded_bases, idx);
10984 else
10985 /* Substitute to figure out the base class. */
10986 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10987 NULL_TREE);
10988
10989 if (base == error_mark_node)
10990 continue;
10991
10992 base_list = tree_cons (access, base, base_list);
10993 if (BINFO_VIRTUAL_P (pbase_binfo))
10994 TREE_TYPE (base_list) = integer_type_node;
10995 }
10996 }
10997
10998 /* The list is now in reverse order; correct that. */
10999 base_list = nreverse (base_list);
11000
11001 if (pushed_scope)
11002 pop_scope (pushed_scope);
11003 }
11004 /* Now call xref_basetypes to set up all the base-class
11005 information. */
11006 xref_basetypes (type, base_list);
11007
11008 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11009 (int) ATTR_FLAG_TYPE_IN_PLACE,
11010 args, tf_error, NULL_TREE);
11011 fixup_attribute_variants (type);
11012
11013 /* Now that our base classes are set up, enter the scope of the
11014 class, so that name lookups into base classes, etc. will work
11015 correctly. This is precisely analogous to what we do in
11016 begin_class_definition when defining an ordinary non-template
11017 class, except we also need to push the enclosing classes. */
11018 push_nested_class (type);
11019
11020 /* Now members are processed in the order of declaration. */
11021 for (member = CLASSTYPE_DECL_LIST (pattern);
11022 member; member = TREE_CHAIN (member))
11023 {
11024 tree t = TREE_VALUE (member);
11025
11026 if (TREE_PURPOSE (member))
11027 {
11028 if (TYPE_P (t))
11029 {
11030 if (LAMBDA_TYPE_P (t))
11031 /* A closure type for a lambda in an NSDMI or default argument.
11032 Ignore it; it will be regenerated when needed. */
11033 continue;
11034
11035 /* Build new CLASSTYPE_NESTED_UTDS. */
11036
11037 tree newtag;
11038 bool class_template_p;
11039
11040 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11041 && TYPE_LANG_SPECIFIC (t)
11042 && CLASSTYPE_IS_TEMPLATE (t));
11043 /* If the member is a class template, then -- even after
11044 substitution -- there may be dependent types in the
11045 template argument list for the class. We increment
11046 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11047 that function will assume that no types are dependent
11048 when outside of a template. */
11049 if (class_template_p)
11050 ++processing_template_decl;
11051 newtag = tsubst (t, args, tf_error, NULL_TREE);
11052 if (class_template_p)
11053 --processing_template_decl;
11054 if (newtag == error_mark_node)
11055 continue;
11056
11057 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11058 {
11059 tree name = TYPE_IDENTIFIER (t);
11060
11061 if (class_template_p)
11062 /* Unfortunately, lookup_template_class sets
11063 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11064 instantiation (i.e., for the type of a member
11065 template class nested within a template class.)
11066 This behavior is required for
11067 maybe_process_partial_specialization to work
11068 correctly, but is not accurate in this case;
11069 the TAG is not an instantiation of anything.
11070 (The corresponding TEMPLATE_DECL is an
11071 instantiation, but the TYPE is not.) */
11072 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11073
11074 /* Now, we call pushtag to put this NEWTAG into the scope of
11075 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11076 pushtag calling push_template_decl. We don't have to do
11077 this for enums because it will already have been done in
11078 tsubst_enum. */
11079 if (name)
11080 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11081 pushtag (name, newtag, /*tag_scope=*/ts_current);
11082 }
11083 }
11084 else if (DECL_DECLARES_FUNCTION_P (t))
11085 {
11086 tree r;
11087
11088 if (TREE_CODE (t) == TEMPLATE_DECL)
11089 ++processing_template_decl;
11090 r = tsubst (t, args, tf_error, NULL_TREE);
11091 if (TREE_CODE (t) == TEMPLATE_DECL)
11092 --processing_template_decl;
11093 set_current_access_from_decl (r);
11094 finish_member_declaration (r);
11095 /* Instantiate members marked with attribute used. */
11096 if (r != error_mark_node && DECL_PRESERVE_P (r))
11097 mark_used (r);
11098 if (TREE_CODE (r) == FUNCTION_DECL
11099 && DECL_OMP_DECLARE_REDUCTION_P (r))
11100 cp_check_omp_declare_reduction (r);
11101 }
11102 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11103 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11104 /* A closure type for a lambda in an NSDMI or default argument.
11105 Ignore it; it will be regenerated when needed. */;
11106 else
11107 {
11108 /* Build new TYPE_FIELDS. */
11109 if (TREE_CODE (t) == STATIC_ASSERT)
11110 {
11111 tree condition;
11112
11113 ++c_inhibit_evaluation_warnings;
11114 condition =
11115 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11116 tf_warning_or_error, NULL_TREE,
11117 /*integral_constant_expression_p=*/true);
11118 --c_inhibit_evaluation_warnings;
11119
11120 finish_static_assert (condition,
11121 STATIC_ASSERT_MESSAGE (t),
11122 STATIC_ASSERT_SOURCE_LOCATION (t),
11123 /*member_p=*/true);
11124 }
11125 else if (TREE_CODE (t) != CONST_DECL)
11126 {
11127 tree r;
11128 tree vec = NULL_TREE;
11129 int len = 1;
11130
11131 /* The file and line for this declaration, to
11132 assist in error message reporting. Since we
11133 called push_tinst_level above, we don't need to
11134 restore these. */
11135 input_location = DECL_SOURCE_LOCATION (t);
11136
11137 if (TREE_CODE (t) == TEMPLATE_DECL)
11138 ++processing_template_decl;
11139 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11140 if (TREE_CODE (t) == TEMPLATE_DECL)
11141 --processing_template_decl;
11142
11143 if (TREE_CODE (r) == TREE_VEC)
11144 {
11145 /* A capture pack became multiple fields. */
11146 vec = r;
11147 len = TREE_VEC_LENGTH (vec);
11148 }
11149
11150 for (int i = 0; i < len; ++i)
11151 {
11152 if (vec)
11153 r = TREE_VEC_ELT (vec, i);
11154 if (VAR_P (r))
11155 {
11156 /* In [temp.inst]:
11157
11158 [t]he initialization (and any associated
11159 side-effects) of a static data member does
11160 not occur unless the static data member is
11161 itself used in a way that requires the
11162 definition of the static data member to
11163 exist.
11164
11165 Therefore, we do not substitute into the
11166 initialized for the static data member here. */
11167 finish_static_data_member_decl
11168 (r,
11169 /*init=*/NULL_TREE,
11170 /*init_const_expr_p=*/false,
11171 /*asmspec_tree=*/NULL_TREE,
11172 /*flags=*/0);
11173 /* Instantiate members marked with attribute used. */
11174 if (r != error_mark_node && DECL_PRESERVE_P (r))
11175 mark_used (r);
11176 }
11177 else if (TREE_CODE (r) == FIELD_DECL)
11178 {
11179 /* Determine whether R has a valid type and can be
11180 completed later. If R is invalid, then its type
11181 is replaced by error_mark_node. */
11182 tree rtype = TREE_TYPE (r);
11183 if (can_complete_type_without_circularity (rtype))
11184 complete_type (rtype);
11185
11186 if (!complete_or_array_type_p (rtype))
11187 {
11188 /* If R's type couldn't be completed and
11189 it isn't a flexible array member (whose
11190 type is incomplete by definition) give
11191 an error. */
11192 cxx_incomplete_type_error (r, rtype);
11193 TREE_TYPE (r) = error_mark_node;
11194 }
11195 else if (TREE_CODE (rtype) == ARRAY_TYPE
11196 && TYPE_DOMAIN (rtype) == NULL_TREE
11197 && (TREE_CODE (type) == UNION_TYPE
11198 || TREE_CODE (type) == QUAL_UNION_TYPE))
11199 {
11200 error ("flexible array member %qD in union", r);
11201 TREE_TYPE (r) = error_mark_node;
11202 }
11203 }
11204
11205 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11206 such a thing will already have been added to the field
11207 list by tsubst_enum in finish_member_declaration in the
11208 CLASSTYPE_NESTED_UTDS case above. */
11209 if (!(TREE_CODE (r) == TYPE_DECL
11210 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11211 && DECL_ARTIFICIAL (r)))
11212 {
11213 set_current_access_from_decl (r);
11214 finish_member_declaration (r);
11215 }
11216 }
11217 }
11218 }
11219 }
11220 else
11221 {
11222 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11223 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11224 {
11225 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11226
11227 tree friend_type = t;
11228 bool adjust_processing_template_decl = false;
11229
11230 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11231 {
11232 /* template <class T> friend class C; */
11233 friend_type = tsubst_friend_class (friend_type, args);
11234 adjust_processing_template_decl = true;
11235 }
11236 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11237 {
11238 /* template <class T> friend class C::D; */
11239 friend_type = tsubst (friend_type, args,
11240 tf_warning_or_error, NULL_TREE);
11241 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11242 friend_type = TREE_TYPE (friend_type);
11243 adjust_processing_template_decl = true;
11244 }
11245 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11246 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11247 {
11248 /* This could be either
11249
11250 friend class T::C;
11251
11252 when dependent_type_p is false or
11253
11254 template <class U> friend class T::C;
11255
11256 otherwise. */
11257 /* Bump processing_template_decl in case this is something like
11258 template <class T> friend struct A<T>::B. */
11259 ++processing_template_decl;
11260 friend_type = tsubst (friend_type, args,
11261 tf_warning_or_error, NULL_TREE);
11262 if (dependent_type_p (friend_type))
11263 adjust_processing_template_decl = true;
11264 --processing_template_decl;
11265 }
11266 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11267 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11268 && TYPE_HIDDEN_P (friend_type))
11269 {
11270 /* friend class C;
11271
11272 where C hasn't been declared yet. Let's lookup name
11273 from namespace scope directly, bypassing any name that
11274 come from dependent base class. */
11275 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11276
11277 /* The call to xref_tag_from_type does injection for friend
11278 classes. */
11279 push_nested_namespace (ns);
11280 friend_type =
11281 xref_tag_from_type (friend_type, NULL_TREE,
11282 /*tag_scope=*/ts_current);
11283 pop_nested_namespace (ns);
11284 }
11285 else if (uses_template_parms (friend_type))
11286 /* friend class C<T>; */
11287 friend_type = tsubst (friend_type, args,
11288 tf_warning_or_error, NULL_TREE);
11289 /* Otherwise it's
11290
11291 friend class C;
11292
11293 where C is already declared or
11294
11295 friend class C<int>;
11296
11297 We don't have to do anything in these cases. */
11298
11299 if (adjust_processing_template_decl)
11300 /* Trick make_friend_class into realizing that the friend
11301 we're adding is a template, not an ordinary class. It's
11302 important that we use make_friend_class since it will
11303 perform some error-checking and output cross-reference
11304 information. */
11305 ++processing_template_decl;
11306
11307 if (friend_type != error_mark_node)
11308 make_friend_class (type, friend_type, /*complain=*/false);
11309
11310 if (adjust_processing_template_decl)
11311 --processing_template_decl;
11312 }
11313 else
11314 {
11315 /* Build new DECL_FRIENDLIST. */
11316 tree r;
11317
11318 /* The file and line for this declaration, to
11319 assist in error message reporting. Since we
11320 called push_tinst_level above, we don't need to
11321 restore these. */
11322 input_location = DECL_SOURCE_LOCATION (t);
11323
11324 if (TREE_CODE (t) == TEMPLATE_DECL)
11325 {
11326 ++processing_template_decl;
11327 push_deferring_access_checks (dk_no_check);
11328 }
11329
11330 r = tsubst_friend_function (t, args);
11331 add_friend (type, r, /*complain=*/false);
11332 if (TREE_CODE (t) == TEMPLATE_DECL)
11333 {
11334 pop_deferring_access_checks ();
11335 --processing_template_decl;
11336 }
11337 }
11338 }
11339 }
11340
11341 if (fn_context)
11342 {
11343 /* Restore these before substituting into the lambda capture
11344 initializers. */
11345 cp_unevaluated_operand = saved_unevaluated_operand;
11346 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11347 }
11348
11349 /* Set the file and line number information to whatever is given for
11350 the class itself. This puts error messages involving generated
11351 implicit functions at a predictable point, and the same point
11352 that would be used for non-template classes. */
11353 input_location = DECL_SOURCE_LOCATION (typedecl);
11354
11355 unreverse_member_declarations (type);
11356 finish_struct_1 (type);
11357 TYPE_BEING_DEFINED (type) = 0;
11358
11359 /* We don't instantiate default arguments for member functions. 14.7.1:
11360
11361 The implicit instantiation of a class template specialization causes
11362 the implicit instantiation of the declarations, but not of the
11363 definitions or default arguments, of the class member functions,
11364 member classes, static data members and member templates.... */
11365
11366 /* Some typedefs referenced from within the template code need to be access
11367 checked at template instantiation time, i.e now. These types were
11368 added to the template at parsing time. Let's get those and perform
11369 the access checks then. */
11370 perform_typedefs_access_check (pattern, args);
11371 perform_deferred_access_checks (tf_warning_or_error);
11372 pop_nested_class ();
11373 maximum_field_alignment = saved_maximum_field_alignment;
11374 if (!fn_context)
11375 pop_from_top_level ();
11376 pop_deferring_access_checks ();
11377 pop_tinst_level ();
11378
11379 /* The vtable for a template class can be emitted in any translation
11380 unit in which the class is instantiated. When there is no key
11381 method, however, finish_struct_1 will already have added TYPE to
11382 the keyed_classes. */
11383 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11384 vec_safe_push (keyed_classes, type);
11385
11386 return type;
11387 }
11388
11389 /* Wrapper for instantiate_class_template_1. */
11390
11391 tree
11392 instantiate_class_template (tree type)
11393 {
11394 tree ret;
11395 timevar_push (TV_TEMPLATE_INST);
11396 ret = instantiate_class_template_1 (type);
11397 timevar_pop (TV_TEMPLATE_INST);
11398 return ret;
11399 }
11400
11401 static tree
11402 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11403 {
11404 tree r;
11405
11406 if (!t)
11407 r = t;
11408 else if (TYPE_P (t))
11409 r = tsubst (t, args, complain, in_decl);
11410 else
11411 {
11412 if (!(complain & tf_warning))
11413 ++c_inhibit_evaluation_warnings;
11414 r = tsubst_expr (t, args, complain, in_decl,
11415 /*integral_constant_expression_p=*/true);
11416 if (!(complain & tf_warning))
11417 --c_inhibit_evaluation_warnings;
11418 }
11419 return r;
11420 }
11421
11422 /* Given a function parameter pack TMPL_PARM and some function parameters
11423 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11424 and set *SPEC_P to point at the next point in the list. */
11425
11426 tree
11427 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11428 {
11429 /* Collect all of the extra "packed" parameters into an
11430 argument pack. */
11431 tree parmvec;
11432 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11433 tree spec_parm = *spec_p;
11434 int i, len;
11435
11436 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11437 if (tmpl_parm
11438 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11439 break;
11440
11441 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11442 parmvec = make_tree_vec (len);
11443 spec_parm = *spec_p;
11444 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11445 {
11446 tree elt = spec_parm;
11447 if (DECL_PACK_P (elt))
11448 elt = make_pack_expansion (elt);
11449 TREE_VEC_ELT (parmvec, i) = elt;
11450 }
11451
11452 /* Build the argument packs. */
11453 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11454 *spec_p = spec_parm;
11455
11456 return argpack;
11457 }
11458
11459 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11460 NONTYPE_ARGUMENT_PACK. */
11461
11462 static tree
11463 make_fnparm_pack (tree spec_parm)
11464 {
11465 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11466 }
11467
11468 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11469 pack expansion with no extra args, 2 if it has extra args, or 0
11470 if it is not a pack expansion. */
11471
11472 static int
11473 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11474 {
11475 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11476 if (i >= TREE_VEC_LENGTH (vec))
11477 return 0;
11478 tree elt = TREE_VEC_ELT (vec, i);
11479 if (DECL_P (elt))
11480 /* A decl pack is itself an expansion. */
11481 elt = TREE_TYPE (elt);
11482 if (!PACK_EXPANSION_P (elt))
11483 return 0;
11484 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11485 return 2;
11486 return 1;
11487 }
11488
11489
11490 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11491
11492 static tree
11493 make_argument_pack_select (tree arg_pack, unsigned index)
11494 {
11495 tree aps = make_node (ARGUMENT_PACK_SELECT);
11496
11497 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11498 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11499
11500 return aps;
11501 }
11502
11503 /* This is a subroutine of tsubst_pack_expansion.
11504
11505 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11506 mechanism to store the (non complete list of) arguments of the
11507 substitution and return a non substituted pack expansion, in order
11508 to wait for when we have enough arguments to really perform the
11509 substitution. */
11510
11511 static bool
11512 use_pack_expansion_extra_args_p (tree parm_packs,
11513 int arg_pack_len,
11514 bool has_empty_arg)
11515 {
11516 /* If one pack has an expansion and another pack has a normal
11517 argument or if one pack has an empty argument and an another
11518 one hasn't then tsubst_pack_expansion cannot perform the
11519 substitution and need to fall back on the
11520 PACK_EXPANSION_EXTRA mechanism. */
11521 if (parm_packs == NULL_TREE)
11522 return false;
11523 else if (has_empty_arg)
11524 return true;
11525
11526 bool has_expansion_arg = false;
11527 for (int i = 0 ; i < arg_pack_len; ++i)
11528 {
11529 bool has_non_expansion_arg = false;
11530 for (tree parm_pack = parm_packs;
11531 parm_pack;
11532 parm_pack = TREE_CHAIN (parm_pack))
11533 {
11534 tree arg = TREE_VALUE (parm_pack);
11535
11536 int exp = argument_pack_element_is_expansion_p (arg, i);
11537 if (exp == 2)
11538 /* We can't substitute a pack expansion with extra args into
11539 our pattern. */
11540 return true;
11541 else if (exp)
11542 has_expansion_arg = true;
11543 else
11544 has_non_expansion_arg = true;
11545 }
11546
11547 if (has_expansion_arg && has_non_expansion_arg)
11548 return true;
11549 }
11550 return false;
11551 }
11552
11553 /* [temp.variadic]/6 says that:
11554
11555 The instantiation of a pack expansion [...]
11556 produces a list E1,E2, ..., En, where N is the number of elements
11557 in the pack expansion parameters.
11558
11559 This subroutine of tsubst_pack_expansion produces one of these Ei.
11560
11561 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11562 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11563 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11564 INDEX is the index 'i' of the element Ei to produce. ARGS,
11565 COMPLAIN, and IN_DECL are the same parameters as for the
11566 tsubst_pack_expansion function.
11567
11568 The function returns the resulting Ei upon successful completion,
11569 or error_mark_node.
11570
11571 Note that this function possibly modifies the ARGS parameter, so
11572 it's the responsibility of the caller to restore it. */
11573
11574 static tree
11575 gen_elem_of_pack_expansion_instantiation (tree pattern,
11576 tree parm_packs,
11577 unsigned index,
11578 tree args /* This parm gets
11579 modified. */,
11580 tsubst_flags_t complain,
11581 tree in_decl)
11582 {
11583 tree t;
11584 bool ith_elem_is_expansion = false;
11585
11586 /* For each parameter pack, change the substitution of the parameter
11587 pack to the ith argument in its argument pack, then expand the
11588 pattern. */
11589 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11590 {
11591 tree parm = TREE_PURPOSE (pack);
11592 tree arg_pack = TREE_VALUE (pack);
11593 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11594
11595 ith_elem_is_expansion |=
11596 argument_pack_element_is_expansion_p (arg_pack, index);
11597
11598 /* Select the Ith argument from the pack. */
11599 if (TREE_CODE (parm) == PARM_DECL
11600 || VAR_P (parm)
11601 || TREE_CODE (parm) == FIELD_DECL)
11602 {
11603 if (index == 0)
11604 {
11605 aps = make_argument_pack_select (arg_pack, index);
11606 if (!mark_used (parm, complain) && !(complain & tf_error))
11607 return error_mark_node;
11608 register_local_specialization (aps, parm);
11609 }
11610 else
11611 aps = retrieve_local_specialization (parm);
11612 }
11613 else
11614 {
11615 int idx, level;
11616 template_parm_level_and_index (parm, &level, &idx);
11617
11618 if (index == 0)
11619 {
11620 aps = make_argument_pack_select (arg_pack, index);
11621 /* Update the corresponding argument. */
11622 TMPL_ARG (args, level, idx) = aps;
11623 }
11624 else
11625 /* Re-use the ARGUMENT_PACK_SELECT. */
11626 aps = TMPL_ARG (args, level, idx);
11627 }
11628 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11629 }
11630
11631 /* Substitute into the PATTERN with the (possibly altered)
11632 arguments. */
11633 if (pattern == in_decl)
11634 /* Expanding a fixed parameter pack from
11635 coerce_template_parameter_pack. */
11636 t = tsubst_decl (pattern, args, complain);
11637 else if (pattern == error_mark_node)
11638 t = error_mark_node;
11639 else if (constraint_p (pattern))
11640 {
11641 if (processing_template_decl)
11642 t = tsubst_constraint (pattern, args, complain, in_decl);
11643 else
11644 t = (constraints_satisfied_p (pattern, args)
11645 ? boolean_true_node : boolean_false_node);
11646 }
11647 else if (!TYPE_P (pattern))
11648 t = tsubst_expr (pattern, args, complain, in_decl,
11649 /*integral_constant_expression_p=*/false);
11650 else
11651 t = tsubst (pattern, args, complain, in_decl);
11652
11653 /* If the Ith argument pack element is a pack expansion, then
11654 the Ith element resulting from the substituting is going to
11655 be a pack expansion as well. */
11656 if (ith_elem_is_expansion)
11657 t = make_pack_expansion (t, complain);
11658
11659 return t;
11660 }
11661
11662 /* When the unexpanded parameter pack in a fold expression expands to an empty
11663 sequence, the value of the expression is as follows; the program is
11664 ill-formed if the operator is not listed in this table.
11665
11666 && true
11667 || false
11668 , void() */
11669
11670 tree
11671 expand_empty_fold (tree t, tsubst_flags_t complain)
11672 {
11673 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11674 if (!FOLD_EXPR_MODIFY_P (t))
11675 switch (code)
11676 {
11677 case TRUTH_ANDIF_EXPR:
11678 return boolean_true_node;
11679 case TRUTH_ORIF_EXPR:
11680 return boolean_false_node;
11681 case COMPOUND_EXPR:
11682 return void_node;
11683 default:
11684 break;
11685 }
11686
11687 if (complain & tf_error)
11688 error_at (location_of (t),
11689 "fold of empty expansion over %O", code);
11690 return error_mark_node;
11691 }
11692
11693 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11694 form an expression that combines the two terms using the
11695 operator of T. */
11696
11697 static tree
11698 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11699 {
11700 tree op = FOLD_EXPR_OP (t);
11701 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11702
11703 // Handle compound assignment operators.
11704 if (FOLD_EXPR_MODIFY_P (t))
11705 return build_x_modify_expr (input_location, left, code, right, complain);
11706
11707 switch (code)
11708 {
11709 case COMPOUND_EXPR:
11710 return build_x_compound_expr (input_location, left, right, complain);
11711 case DOTSTAR_EXPR:
11712 return build_m_component_ref (left, right, complain);
11713 default:
11714 return build_x_binary_op (input_location, code,
11715 left, TREE_CODE (left),
11716 right, TREE_CODE (right),
11717 /*overload=*/NULL,
11718 complain);
11719 }
11720 }
11721
11722 /* Substitute ARGS into the pack of a fold expression T. */
11723
11724 static inline tree
11725 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11726 {
11727 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11728 }
11729
11730 /* Substitute ARGS into the pack of a fold expression T. */
11731
11732 static inline tree
11733 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11734 {
11735 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11736 }
11737
11738 /* Expand a PACK of arguments into a grouped as left fold.
11739 Given a pack containing elements A0, A1, ..., An and an
11740 operator @, this builds the expression:
11741
11742 ((A0 @ A1) @ A2) ... @ An
11743
11744 Note that PACK must not be empty.
11745
11746 The operator is defined by the original fold expression T. */
11747
11748 static tree
11749 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11750 {
11751 tree left = TREE_VEC_ELT (pack, 0);
11752 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11753 {
11754 tree right = TREE_VEC_ELT (pack, i);
11755 left = fold_expression (t, left, right, complain);
11756 }
11757 return left;
11758 }
11759
11760 /* Substitute into a unary left fold expression. */
11761
11762 static tree
11763 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11764 tree in_decl)
11765 {
11766 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11767 if (pack == error_mark_node)
11768 return error_mark_node;
11769 if (PACK_EXPANSION_P (pack))
11770 {
11771 tree r = copy_node (t);
11772 FOLD_EXPR_PACK (r) = pack;
11773 return r;
11774 }
11775 if (TREE_VEC_LENGTH (pack) == 0)
11776 return expand_empty_fold (t, complain);
11777 else
11778 return expand_left_fold (t, pack, complain);
11779 }
11780
11781 /* Substitute into a binary left fold expression.
11782
11783 Do ths by building a single (non-empty) vector of argumnts and
11784 building the expression from those elements. */
11785
11786 static tree
11787 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11788 tree in_decl)
11789 {
11790 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11791 if (pack == error_mark_node)
11792 return error_mark_node;
11793 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11794 if (init == error_mark_node)
11795 return error_mark_node;
11796
11797 if (PACK_EXPANSION_P (pack))
11798 {
11799 tree r = copy_node (t);
11800 FOLD_EXPR_PACK (r) = pack;
11801 FOLD_EXPR_INIT (r) = init;
11802 return r;
11803 }
11804
11805 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11806 TREE_VEC_ELT (vec, 0) = init;
11807 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11808 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11809
11810 return expand_left_fold (t, vec, complain);
11811 }
11812
11813 /* Expand a PACK of arguments into a grouped as right fold.
11814 Given a pack containing elementns A0, A1, ..., and an
11815 operator @, this builds the expression:
11816
11817 A0@ ... (An-2 @ (An-1 @ An))
11818
11819 Note that PACK must not be empty.
11820
11821 The operator is defined by the original fold expression T. */
11822
11823 tree
11824 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11825 {
11826 // Build the expression.
11827 int n = TREE_VEC_LENGTH (pack);
11828 tree right = TREE_VEC_ELT (pack, n - 1);
11829 for (--n; n != 0; --n)
11830 {
11831 tree left = TREE_VEC_ELT (pack, n - 1);
11832 right = fold_expression (t, left, right, complain);
11833 }
11834 return right;
11835 }
11836
11837 /* Substitute into a unary right fold expression. */
11838
11839 static tree
11840 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11841 tree in_decl)
11842 {
11843 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11844 if (pack == error_mark_node)
11845 return error_mark_node;
11846 if (PACK_EXPANSION_P (pack))
11847 {
11848 tree r = copy_node (t);
11849 FOLD_EXPR_PACK (r) = pack;
11850 return r;
11851 }
11852 if (TREE_VEC_LENGTH (pack) == 0)
11853 return expand_empty_fold (t, complain);
11854 else
11855 return expand_right_fold (t, pack, complain);
11856 }
11857
11858 /* Substitute into a binary right fold expression.
11859
11860 Do ths by building a single (non-empty) vector of arguments and
11861 building the expression from those elements. */
11862
11863 static tree
11864 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11865 tree in_decl)
11866 {
11867 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11868 if (pack == error_mark_node)
11869 return error_mark_node;
11870 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11871 if (init == error_mark_node)
11872 return error_mark_node;
11873
11874 if (PACK_EXPANSION_P (pack))
11875 {
11876 tree r = copy_node (t);
11877 FOLD_EXPR_PACK (r) = pack;
11878 FOLD_EXPR_INIT (r) = init;
11879 return r;
11880 }
11881
11882 int n = TREE_VEC_LENGTH (pack);
11883 tree vec = make_tree_vec (n + 1);
11884 for (int i = 0; i < n; ++i)
11885 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11886 TREE_VEC_ELT (vec, n) = init;
11887
11888 return expand_right_fold (t, vec, complain);
11889 }
11890
11891 /* Walk through the pattern of a pack expansion, adding everything in
11892 local_specializations to a list. */
11893
11894 struct el_data
11895 {
11896 hash_set<tree> internal;
11897 tree extra;
11898 tsubst_flags_t complain;
11899
11900 el_data (tsubst_flags_t c)
11901 : extra (NULL_TREE), complain (c) {}
11902 };
11903 static tree
11904 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11905 {
11906 el_data &data = *reinterpret_cast<el_data*>(data_);
11907 tree *extra = &data.extra;
11908 tsubst_flags_t complain = data.complain;
11909
11910 if (TYPE_P (*tp) && typedef_variant_p (*tp))
11911 /* Remember local typedefs (85214). */
11912 tp = &TYPE_NAME (*tp);
11913
11914 if (TREE_CODE (*tp) == DECL_EXPR)
11915 data.internal.add (DECL_EXPR_DECL (*tp));
11916 else if (tree spec = retrieve_local_specialization (*tp))
11917 {
11918 if (data.internal.contains (*tp))
11919 /* Don't mess with variables declared within the pattern. */
11920 return NULL_TREE;
11921 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11922 {
11923 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11924 tree args = ARGUMENT_PACK_ARGS (spec);
11925 if (TREE_VEC_LENGTH (args) == 1)
11926 {
11927 tree elt = TREE_VEC_ELT (args, 0);
11928 if (PACK_EXPANSION_P (elt))
11929 elt = PACK_EXPANSION_PATTERN (elt);
11930 if (DECL_PACK_P (elt))
11931 spec = elt;
11932 }
11933 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11934 {
11935 /* Handle lambda capture here, since we aren't doing any
11936 substitution now, and so tsubst_copy won't call
11937 process_outer_var_ref. */
11938 tree args = ARGUMENT_PACK_ARGS (spec);
11939 int len = TREE_VEC_LENGTH (args);
11940 for (int i = 0; i < len; ++i)
11941 {
11942 tree arg = TREE_VEC_ELT (args, i);
11943 tree carg = arg;
11944 if (outer_automatic_var_p (arg))
11945 carg = process_outer_var_ref (arg, complain);
11946 if (carg != arg)
11947 {
11948 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11949 proxies. */
11950 if (i == 0)
11951 {
11952 spec = copy_node (spec);
11953 args = copy_node (args);
11954 SET_ARGUMENT_PACK_ARGS (spec, args);
11955 register_local_specialization (spec, *tp);
11956 }
11957 TREE_VEC_ELT (args, i) = carg;
11958 }
11959 }
11960 }
11961 }
11962 if (outer_automatic_var_p (spec))
11963 spec = process_outer_var_ref (spec, complain);
11964 *extra = tree_cons (*tp, spec, *extra);
11965 }
11966 return NULL_TREE;
11967 }
11968 static tree
11969 extract_local_specs (tree pattern, tsubst_flags_t complain)
11970 {
11971 el_data data (complain);
11972 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
11973 return data.extra;
11974 }
11975
11976 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
11977 for use in PACK_EXPANSION_EXTRA_ARGS. */
11978
11979 tree
11980 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
11981 {
11982 tree extra = args;
11983 if (local_specializations)
11984 if (tree locals = extract_local_specs (pattern, complain))
11985 extra = tree_cons (NULL_TREE, extra, locals);
11986 return extra;
11987 }
11988
11989 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
11990 normal template args to ARGS. */
11991
11992 tree
11993 add_extra_args (tree extra, tree args)
11994 {
11995 if (extra && TREE_CODE (extra) == TREE_LIST)
11996 {
11997 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
11998 {
11999 /* The partial instantiation involved local declarations collected in
12000 extract_local_specs; map from the general template to our local
12001 context. */
12002 tree gen = TREE_PURPOSE (elt);
12003 tree inst = TREE_VALUE (elt);
12004 if (DECL_P (inst))
12005 if (tree local = retrieve_local_specialization (inst))
12006 inst = local;
12007 /* else inst is already a full instantiation of the pack. */
12008 register_local_specialization (inst, gen);
12009 }
12010 gcc_assert (!TREE_PURPOSE (extra));
12011 extra = TREE_VALUE (extra);
12012 }
12013 return add_to_template_args (extra, args);
12014 }
12015
12016 /* Substitute ARGS into T, which is an pack expansion
12017 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12018 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12019 (if only a partial substitution could be performed) or
12020 ERROR_MARK_NODE if there was an error. */
12021 tree
12022 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12023 tree in_decl)
12024 {
12025 tree pattern;
12026 tree pack, packs = NULL_TREE;
12027 bool unsubstituted_packs = false;
12028 bool unsubstituted_fn_pack = false;
12029 int i, len = -1;
12030 tree result;
12031 hash_map<tree, tree> *saved_local_specializations = NULL;
12032 bool need_local_specializations = false;
12033 int levels;
12034
12035 gcc_assert (PACK_EXPANSION_P (t));
12036 pattern = PACK_EXPANSION_PATTERN (t);
12037
12038 /* Add in any args remembered from an earlier partial instantiation. */
12039 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12040
12041 levels = TMPL_ARGS_DEPTH (args);
12042
12043 /* Determine the argument packs that will instantiate the parameter
12044 packs used in the expansion expression. While we're at it,
12045 compute the number of arguments to be expanded and make sure it
12046 is consistent. */
12047 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12048 pack = TREE_CHAIN (pack))
12049 {
12050 tree parm_pack = TREE_VALUE (pack);
12051 tree arg_pack = NULL_TREE;
12052 tree orig_arg = NULL_TREE;
12053 int level = 0;
12054
12055 if (TREE_CODE (parm_pack) == BASES)
12056 {
12057 gcc_assert (parm_pack == pattern);
12058 if (BASES_DIRECT (parm_pack))
12059 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12060 args, complain,
12061 in_decl, false),
12062 complain);
12063 else
12064 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12065 args, complain, in_decl,
12066 false), complain);
12067 }
12068 else if (builtin_pack_call_p (parm_pack))
12069 {
12070 /* ??? Support use in other patterns. */
12071 gcc_assert (parm_pack == pattern);
12072 return expand_builtin_pack_call (parm_pack, args,
12073 complain, in_decl);
12074 }
12075 else if (TREE_CODE (parm_pack) == PARM_DECL)
12076 {
12077 /* We know we have correct local_specializations if this
12078 expansion is at function scope, or if we're dealing with a
12079 local parameter in a requires expression; for the latter,
12080 tsubst_requires_expr set it up appropriately. */
12081 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12082 arg_pack = retrieve_local_specialization (parm_pack);
12083 else
12084 /* We can't rely on local_specializations for a parameter
12085 name used later in a function declaration (such as in a
12086 late-specified return type). Even if it exists, it might
12087 have the wrong value for a recursive call. */
12088 need_local_specializations = true;
12089
12090 if (!arg_pack)
12091 {
12092 /* This parameter pack was used in an unevaluated context. Just
12093 make a dummy decl, since it's only used for its type. */
12094 ++cp_unevaluated_operand;
12095 arg_pack = tsubst_decl (parm_pack, args, complain);
12096 --cp_unevaluated_operand;
12097 if (arg_pack && DECL_PACK_P (arg_pack))
12098 /* Partial instantiation of the parm_pack, we can't build
12099 up an argument pack yet. */
12100 arg_pack = NULL_TREE;
12101 else
12102 arg_pack = make_fnparm_pack (arg_pack);
12103 }
12104 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12105 /* This argument pack isn't fully instantiated yet. We set this
12106 flag rather than clear arg_pack because we do want to do the
12107 optimization below, and we don't want to substitute directly
12108 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12109 where it isn't expected). */
12110 unsubstituted_fn_pack = true;
12111 }
12112 else if (is_normal_capture_proxy (parm_pack))
12113 {
12114 arg_pack = retrieve_local_specialization (parm_pack);
12115 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12116 unsubstituted_fn_pack = true;
12117 }
12118 else
12119 {
12120 int idx;
12121 template_parm_level_and_index (parm_pack, &level, &idx);
12122
12123 if (level <= levels)
12124 arg_pack = TMPL_ARG (args, level, idx);
12125 }
12126
12127 orig_arg = arg_pack;
12128 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12129 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12130
12131 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12132 /* This can only happen if we forget to expand an argument
12133 pack somewhere else. Just return an error, silently. */
12134 {
12135 result = make_tree_vec (1);
12136 TREE_VEC_ELT (result, 0) = error_mark_node;
12137 return result;
12138 }
12139
12140 if (arg_pack)
12141 {
12142 int my_len =
12143 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12144
12145 /* Don't bother trying to do a partial substitution with
12146 incomplete packs; we'll try again after deduction. */
12147 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12148 return t;
12149
12150 if (len < 0)
12151 len = my_len;
12152 else if (len != my_len
12153 && !unsubstituted_fn_pack)
12154 {
12155 if (!(complain & tf_error))
12156 /* Fail quietly. */;
12157 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12158 error ("mismatched argument pack lengths while expanding %qT",
12159 pattern);
12160 else
12161 error ("mismatched argument pack lengths while expanding %qE",
12162 pattern);
12163 return error_mark_node;
12164 }
12165
12166 /* Keep track of the parameter packs and their corresponding
12167 argument packs. */
12168 packs = tree_cons (parm_pack, arg_pack, packs);
12169 TREE_TYPE (packs) = orig_arg;
12170 }
12171 else
12172 {
12173 /* We can't substitute for this parameter pack. We use a flag as
12174 well as the missing_level counter because function parameter
12175 packs don't have a level. */
12176 gcc_assert (processing_template_decl || is_auto (parm_pack));
12177 unsubstituted_packs = true;
12178 }
12179 }
12180
12181 /* If the expansion is just T..., return the matching argument pack, unless
12182 we need to call convert_from_reference on all the elements. This is an
12183 important optimization; see c++/68422. */
12184 if (!unsubstituted_packs
12185 && TREE_PURPOSE (packs) == pattern)
12186 {
12187 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12188
12189 /* If the argument pack is a single pack expansion, pull it out. */
12190 if (TREE_VEC_LENGTH (args) == 1
12191 && pack_expansion_args_count (args))
12192 return TREE_VEC_ELT (args, 0);
12193
12194 /* Types need no adjustment, nor does sizeof..., and if we still have
12195 some pack expansion args we won't do anything yet. */
12196 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12197 || PACK_EXPANSION_SIZEOF_P (t)
12198 || pack_expansion_args_count (args))
12199 return args;
12200 /* Also optimize expression pack expansions if we can tell that the
12201 elements won't have reference type. */
12202 tree type = TREE_TYPE (pattern);
12203 if (type && !TYPE_REF_P (type)
12204 && !PACK_EXPANSION_P (type)
12205 && !WILDCARD_TYPE_P (type))
12206 return args;
12207 /* Otherwise use the normal path so we get convert_from_reference. */
12208 }
12209
12210 /* We cannot expand this expansion expression, because we don't have
12211 all of the argument packs we need. */
12212 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12213 {
12214 /* We got some full packs, but we can't substitute them in until we
12215 have values for all the packs. So remember these until then. */
12216
12217 t = make_pack_expansion (pattern, complain);
12218 PACK_EXPANSION_EXTRA_ARGS (t)
12219 = build_extra_args (pattern, args, complain);
12220 return t;
12221 }
12222 else if (unsubstituted_packs)
12223 {
12224 /* There were no real arguments, we're just replacing a parameter
12225 pack with another version of itself. Substitute into the
12226 pattern and return a PACK_EXPANSION_*. The caller will need to
12227 deal with that. */
12228 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12229 t = tsubst_expr (pattern, args, complain, in_decl,
12230 /*integral_constant_expression_p=*/false);
12231 else
12232 t = tsubst (pattern, args, complain, in_decl);
12233 t = make_pack_expansion (t, complain);
12234 return t;
12235 }
12236
12237 gcc_assert (len >= 0);
12238
12239 if (need_local_specializations)
12240 {
12241 /* We're in a late-specified return type, so create our own local
12242 specializations map; the current map is either NULL or (in the
12243 case of recursive unification) might have bindings that we don't
12244 want to use or alter. */
12245 saved_local_specializations = local_specializations;
12246 local_specializations = new hash_map<tree, tree>;
12247 }
12248
12249 /* For each argument in each argument pack, substitute into the
12250 pattern. */
12251 result = make_tree_vec (len);
12252 tree elem_args = copy_template_args (args);
12253 for (i = 0; i < len; ++i)
12254 {
12255 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12256 i,
12257 elem_args, complain,
12258 in_decl);
12259 TREE_VEC_ELT (result, i) = t;
12260 if (t == error_mark_node)
12261 {
12262 result = error_mark_node;
12263 break;
12264 }
12265 }
12266
12267 /* Update ARGS to restore the substitution from parameter packs to
12268 their argument packs. */
12269 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12270 {
12271 tree parm = TREE_PURPOSE (pack);
12272
12273 if (TREE_CODE (parm) == PARM_DECL
12274 || VAR_P (parm)
12275 || TREE_CODE (parm) == FIELD_DECL)
12276 register_local_specialization (TREE_TYPE (pack), parm);
12277 else
12278 {
12279 int idx, level;
12280
12281 if (TREE_VALUE (pack) == NULL_TREE)
12282 continue;
12283
12284 template_parm_level_and_index (parm, &level, &idx);
12285
12286 /* Update the corresponding argument. */
12287 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12288 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12289 TREE_TYPE (pack);
12290 else
12291 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12292 }
12293 }
12294
12295 if (need_local_specializations)
12296 {
12297 delete local_specializations;
12298 local_specializations = saved_local_specializations;
12299 }
12300
12301 /* If the dependent pack arguments were such that we end up with only a
12302 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12303 if (len == 1 && TREE_CODE (result) == TREE_VEC
12304 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12305 return TREE_VEC_ELT (result, 0);
12306
12307 return result;
12308 }
12309
12310 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12311 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12312 parameter packs; all parms generated from a function parameter pack will
12313 have the same DECL_PARM_INDEX. */
12314
12315 tree
12316 get_pattern_parm (tree parm, tree tmpl)
12317 {
12318 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12319 tree patparm;
12320
12321 if (DECL_ARTIFICIAL (parm))
12322 {
12323 for (patparm = DECL_ARGUMENTS (pattern);
12324 patparm; patparm = DECL_CHAIN (patparm))
12325 if (DECL_ARTIFICIAL (patparm)
12326 && DECL_NAME (parm) == DECL_NAME (patparm))
12327 break;
12328 }
12329 else
12330 {
12331 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12332 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12333 gcc_assert (DECL_PARM_INDEX (patparm)
12334 == DECL_PARM_INDEX (parm));
12335 }
12336
12337 return patparm;
12338 }
12339
12340 /* Make an argument pack out of the TREE_VEC VEC. */
12341
12342 static tree
12343 make_argument_pack (tree vec)
12344 {
12345 tree pack;
12346 tree elt = TREE_VEC_ELT (vec, 0);
12347 if (TYPE_P (elt))
12348 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12349 else
12350 {
12351 pack = make_node (NONTYPE_ARGUMENT_PACK);
12352 TREE_CONSTANT (pack) = 1;
12353 }
12354 SET_ARGUMENT_PACK_ARGS (pack, vec);
12355 return pack;
12356 }
12357
12358 /* Return an exact copy of template args T that can be modified
12359 independently. */
12360
12361 static tree
12362 copy_template_args (tree t)
12363 {
12364 if (t == error_mark_node)
12365 return t;
12366
12367 int len = TREE_VEC_LENGTH (t);
12368 tree new_vec = make_tree_vec (len);
12369
12370 for (int i = 0; i < len; ++i)
12371 {
12372 tree elt = TREE_VEC_ELT (t, i);
12373 if (elt && TREE_CODE (elt) == TREE_VEC)
12374 elt = copy_template_args (elt);
12375 TREE_VEC_ELT (new_vec, i) = elt;
12376 }
12377
12378 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12379 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12380
12381 return new_vec;
12382 }
12383
12384 /* Substitute ARGS into the vector or list of template arguments T. */
12385
12386 static tree
12387 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12388 {
12389 tree orig_t = t;
12390 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12391 tree *elts;
12392
12393 if (t == error_mark_node)
12394 return error_mark_node;
12395
12396 len = TREE_VEC_LENGTH (t);
12397 elts = XALLOCAVEC (tree, len);
12398
12399 for (i = 0; i < len; i++)
12400 {
12401 tree orig_arg = TREE_VEC_ELT (t, i);
12402 tree new_arg;
12403
12404 if (TREE_CODE (orig_arg) == TREE_VEC)
12405 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12406 else if (PACK_EXPANSION_P (orig_arg))
12407 {
12408 /* Substitute into an expansion expression. */
12409 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12410
12411 if (TREE_CODE (new_arg) == TREE_VEC)
12412 /* Add to the expanded length adjustment the number of
12413 expanded arguments. We subtract one from this
12414 measurement, because the argument pack expression
12415 itself is already counted as 1 in
12416 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12417 the argument pack is empty. */
12418 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12419 }
12420 else if (ARGUMENT_PACK_P (orig_arg))
12421 {
12422 /* Substitute into each of the arguments. */
12423 new_arg = TYPE_P (orig_arg)
12424 ? cxx_make_type (TREE_CODE (orig_arg))
12425 : make_node (TREE_CODE (orig_arg));
12426
12427 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12428 args, complain, in_decl);
12429 if (pack_args == error_mark_node)
12430 new_arg = error_mark_node;
12431 else
12432 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12433
12434 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12435 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12436 }
12437 else
12438 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12439
12440 if (new_arg == error_mark_node)
12441 return error_mark_node;
12442
12443 elts[i] = new_arg;
12444 if (new_arg != orig_arg)
12445 need_new = 1;
12446 }
12447
12448 if (!need_new)
12449 return t;
12450
12451 /* Make space for the expanded arguments coming from template
12452 argument packs. */
12453 t = make_tree_vec (len + expanded_len_adjust);
12454 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12455 arguments for a member template.
12456 In that case each TREE_VEC in ORIG_T represents a level of template
12457 arguments, and ORIG_T won't carry any non defaulted argument count.
12458 It will rather be the nested TREE_VECs that will carry one.
12459 In other words, ORIG_T carries a non defaulted argument count only
12460 if it doesn't contain any nested TREE_VEC. */
12461 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12462 {
12463 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12464 count += expanded_len_adjust;
12465 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12466 }
12467 for (i = 0, out = 0; i < len; i++)
12468 {
12469 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12470 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12471 && TREE_CODE (elts[i]) == TREE_VEC)
12472 {
12473 int idx;
12474
12475 /* Now expand the template argument pack "in place". */
12476 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12477 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12478 }
12479 else
12480 {
12481 TREE_VEC_ELT (t, out) = elts[i];
12482 out++;
12483 }
12484 }
12485
12486 return t;
12487 }
12488
12489 /* Substitute ARGS into one level PARMS of template parameters. */
12490
12491 static tree
12492 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12493 {
12494 if (parms == error_mark_node)
12495 return error_mark_node;
12496
12497 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12498
12499 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12500 {
12501 tree tuple = TREE_VEC_ELT (parms, i);
12502
12503 if (tuple == error_mark_node)
12504 continue;
12505
12506 TREE_VEC_ELT (new_vec, i) =
12507 tsubst_template_parm (tuple, args, complain);
12508 }
12509
12510 return new_vec;
12511 }
12512
12513 /* Return the result of substituting ARGS into the template parameters
12514 given by PARMS. If there are m levels of ARGS and m + n levels of
12515 PARMS, then the result will contain n levels of PARMS. For
12516 example, if PARMS is `template <class T> template <class U>
12517 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12518 result will be `template <int*, double, class V>'. */
12519
12520 static tree
12521 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12522 {
12523 tree r = NULL_TREE;
12524 tree* new_parms;
12525
12526 /* When substituting into a template, we must set
12527 PROCESSING_TEMPLATE_DECL as the template parameters may be
12528 dependent if they are based on one-another, and the dependency
12529 predicates are short-circuit outside of templates. */
12530 ++processing_template_decl;
12531
12532 for (new_parms = &r;
12533 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12534 new_parms = &(TREE_CHAIN (*new_parms)),
12535 parms = TREE_CHAIN (parms))
12536 {
12537 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12538 args, complain);
12539 *new_parms =
12540 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12541 - TMPL_ARGS_DEPTH (args)),
12542 new_vec, NULL_TREE);
12543 }
12544
12545 --processing_template_decl;
12546
12547 return r;
12548 }
12549
12550 /* Return the result of substituting ARGS into one template parameter
12551 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12552 parameter and which TREE_PURPOSE is the default argument of the
12553 template parameter. */
12554
12555 static tree
12556 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12557 {
12558 tree default_value, parm_decl;
12559
12560 if (args == NULL_TREE
12561 || t == NULL_TREE
12562 || t == error_mark_node)
12563 return t;
12564
12565 gcc_assert (TREE_CODE (t) == TREE_LIST);
12566
12567 default_value = TREE_PURPOSE (t);
12568 parm_decl = TREE_VALUE (t);
12569
12570 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12571 if (TREE_CODE (parm_decl) == PARM_DECL
12572 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12573 parm_decl = error_mark_node;
12574 default_value = tsubst_template_arg (default_value, args,
12575 complain, NULL_TREE);
12576
12577 return build_tree_list (default_value, parm_decl);
12578 }
12579
12580 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12581 type T. If T is not an aggregate or enumeration type, it is
12582 handled as if by tsubst. IN_DECL is as for tsubst. If
12583 ENTERING_SCOPE is nonzero, T is the context for a template which
12584 we are presently tsubst'ing. Return the substituted value. */
12585
12586 static tree
12587 tsubst_aggr_type (tree t,
12588 tree args,
12589 tsubst_flags_t complain,
12590 tree in_decl,
12591 int entering_scope)
12592 {
12593 if (t == NULL_TREE)
12594 return NULL_TREE;
12595
12596 switch (TREE_CODE (t))
12597 {
12598 case RECORD_TYPE:
12599 if (TYPE_PTRMEMFUNC_P (t))
12600 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12601
12602 /* Fall through. */
12603 case ENUMERAL_TYPE:
12604 case UNION_TYPE:
12605 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12606 {
12607 tree argvec;
12608 tree context;
12609 tree r;
12610 int saved_unevaluated_operand;
12611 int saved_inhibit_evaluation_warnings;
12612
12613 /* In "sizeof(X<I>)" we need to evaluate "I". */
12614 saved_unevaluated_operand = cp_unevaluated_operand;
12615 cp_unevaluated_operand = 0;
12616 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12617 c_inhibit_evaluation_warnings = 0;
12618
12619 /* First, determine the context for the type we are looking
12620 up. */
12621 context = TYPE_CONTEXT (t);
12622 if (context && TYPE_P (context))
12623 {
12624 context = tsubst_aggr_type (context, args, complain,
12625 in_decl, /*entering_scope=*/1);
12626 /* If context is a nested class inside a class template,
12627 it may still need to be instantiated (c++/33959). */
12628 context = complete_type (context);
12629 }
12630
12631 /* Then, figure out what arguments are appropriate for the
12632 type we are trying to find. For example, given:
12633
12634 template <class T> struct S;
12635 template <class T, class U> void f(T, U) { S<U> su; }
12636
12637 and supposing that we are instantiating f<int, double>,
12638 then our ARGS will be {int, double}, but, when looking up
12639 S we only want {double}. */
12640 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12641 complain, in_decl);
12642 if (argvec == error_mark_node)
12643 r = error_mark_node;
12644 else
12645 {
12646 r = lookup_template_class (t, argvec, in_decl, context,
12647 entering_scope, complain);
12648 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12649 }
12650
12651 cp_unevaluated_operand = saved_unevaluated_operand;
12652 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12653
12654 return r;
12655 }
12656 else
12657 /* This is not a template type, so there's nothing to do. */
12658 return t;
12659
12660 default:
12661 return tsubst (t, args, complain, in_decl);
12662 }
12663 }
12664
12665 static GTY((cache)) tree_cache_map *defarg_inst;
12666
12667 /* Substitute into the default argument ARG (a default argument for
12668 FN), which has the indicated TYPE. */
12669
12670 tree
12671 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12672 tsubst_flags_t complain)
12673 {
12674 tree saved_class_ptr = NULL_TREE;
12675 tree saved_class_ref = NULL_TREE;
12676 int errs = errorcount + sorrycount;
12677
12678 /* This can happen in invalid code. */
12679 if (TREE_CODE (arg) == DEFAULT_ARG)
12680 return arg;
12681
12682 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12683 parm = chain_index (parmnum, parm);
12684 tree parmtype = TREE_TYPE (parm);
12685 if (DECL_BY_REFERENCE (parm))
12686 parmtype = TREE_TYPE (parmtype);
12687 if (parmtype == error_mark_node)
12688 return error_mark_node;
12689
12690 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12691
12692 tree *slot;
12693 if (defarg_inst && (slot = defarg_inst->get (parm)))
12694 return *slot;
12695
12696 /* This default argument came from a template. Instantiate the
12697 default argument here, not in tsubst. In the case of
12698 something like:
12699
12700 template <class T>
12701 struct S {
12702 static T t();
12703 void f(T = t());
12704 };
12705
12706 we must be careful to do name lookup in the scope of S<T>,
12707 rather than in the current class. */
12708 push_access_scope (fn);
12709 /* The "this" pointer is not valid in a default argument. */
12710 if (cfun)
12711 {
12712 saved_class_ptr = current_class_ptr;
12713 cp_function_chain->x_current_class_ptr = NULL_TREE;
12714 saved_class_ref = current_class_ref;
12715 cp_function_chain->x_current_class_ref = NULL_TREE;
12716 }
12717
12718 start_lambda_scope (parm);
12719
12720 push_deferring_access_checks(dk_no_deferred);
12721 /* The default argument expression may cause implicitly defined
12722 member functions to be synthesized, which will result in garbage
12723 collection. We must treat this situation as if we were within
12724 the body of function so as to avoid collecting live data on the
12725 stack. */
12726 ++function_depth;
12727 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12728 complain, NULL_TREE,
12729 /*integral_constant_expression_p=*/false);
12730 --function_depth;
12731 pop_deferring_access_checks();
12732
12733 finish_lambda_scope ();
12734
12735 /* Restore the "this" pointer. */
12736 if (cfun)
12737 {
12738 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12739 cp_function_chain->x_current_class_ref = saved_class_ref;
12740 }
12741
12742 if (errorcount+sorrycount > errs
12743 && (complain & tf_warning_or_error))
12744 inform (input_location,
12745 " when instantiating default argument for call to %qD", fn);
12746
12747 /* Make sure the default argument is reasonable. */
12748 arg = check_default_argument (type, arg, complain);
12749
12750 pop_access_scope (fn);
12751
12752 if (arg != error_mark_node && !cp_unevaluated_operand)
12753 {
12754 if (!defarg_inst)
12755 defarg_inst = tree_cache_map::create_ggc (37);
12756 defarg_inst->put (parm, arg);
12757 }
12758
12759 return arg;
12760 }
12761
12762 /* Substitute into all the default arguments for FN. */
12763
12764 static void
12765 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12766 {
12767 tree arg;
12768 tree tmpl_args;
12769
12770 tmpl_args = DECL_TI_ARGS (fn);
12771
12772 /* If this function is not yet instantiated, we certainly don't need
12773 its default arguments. */
12774 if (uses_template_parms (tmpl_args))
12775 return;
12776 /* Don't do this again for clones. */
12777 if (DECL_CLONED_FUNCTION_P (fn))
12778 return;
12779
12780 int i = 0;
12781 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12782 arg;
12783 arg = TREE_CHAIN (arg), ++i)
12784 if (TREE_PURPOSE (arg))
12785 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12786 TREE_VALUE (arg),
12787 TREE_PURPOSE (arg),
12788 complain);
12789 }
12790
12791 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12792
12793 static tree
12794 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12795 tree lambda_fntype)
12796 {
12797 tree gen_tmpl, argvec;
12798 hashval_t hash = 0;
12799 tree in_decl = t;
12800
12801 /* Nobody should be tsubst'ing into non-template functions. */
12802 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12803
12804 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12805 {
12806 /* If T is not dependent, just return it. */
12807 if (!uses_template_parms (DECL_TI_ARGS (t)))
12808 return t;
12809
12810 /* Calculate the most general template of which R is a
12811 specialization. */
12812 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12813
12814 /* We're substituting a lambda function under tsubst_lambda_expr but not
12815 directly from it; find the matching function we're already inside.
12816 But don't do this if T is a generic lambda with a single level of
12817 template parms, as in that case we're doing a normal instantiation. */
12818 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12819 && (!generic_lambda_fn_p (t)
12820 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12821 return enclosing_instantiation_of (t);
12822
12823 /* Calculate the complete set of arguments used to
12824 specialize R. */
12825 argvec = tsubst_template_args (DECL_TI_ARGS
12826 (DECL_TEMPLATE_RESULT
12827 (DECL_TI_TEMPLATE (t))),
12828 args, complain, in_decl);
12829 if (argvec == error_mark_node)
12830 return error_mark_node;
12831
12832 /* Check to see if we already have this specialization. */
12833 if (!lambda_fntype)
12834 {
12835 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12836 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12837 return spec;
12838 }
12839
12840 /* We can see more levels of arguments than parameters if
12841 there was a specialization of a member template, like
12842 this:
12843
12844 template <class T> struct S { template <class U> void f(); }
12845 template <> template <class U> void S<int>::f(U);
12846
12847 Here, we'll be substituting into the specialization,
12848 because that's where we can find the code we actually
12849 want to generate, but we'll have enough arguments for
12850 the most general template.
12851
12852 We also deal with the peculiar case:
12853
12854 template <class T> struct S {
12855 template <class U> friend void f();
12856 };
12857 template <class U> void f() {}
12858 template S<int>;
12859 template void f<double>();
12860
12861 Here, the ARGS for the instantiation of will be {int,
12862 double}. But, we only need as many ARGS as there are
12863 levels of template parameters in CODE_PATTERN. We are
12864 careful not to get fooled into reducing the ARGS in
12865 situations like:
12866
12867 template <class T> struct S { template <class U> void f(U); }
12868 template <class T> template <> void S<T>::f(int) {}
12869
12870 which we can spot because the pattern will be a
12871 specialization in this case. */
12872 int args_depth = TMPL_ARGS_DEPTH (args);
12873 int parms_depth =
12874 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12875
12876 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12877 args = get_innermost_template_args (args, parms_depth);
12878 }
12879 else
12880 {
12881 /* This special case arises when we have something like this:
12882
12883 template <class T> struct S {
12884 friend void f<int>(int, double);
12885 };
12886
12887 Here, the DECL_TI_TEMPLATE for the friend declaration
12888 will be an IDENTIFIER_NODE. We are being called from
12889 tsubst_friend_function, and we want only to create a
12890 new decl (R) with appropriate types so that we can call
12891 determine_specialization. */
12892 gen_tmpl = NULL_TREE;
12893 argvec = NULL_TREE;
12894 }
12895
12896 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12897 : NULL_TREE);
12898 tree ctx = closure ? closure : DECL_CONTEXT (t);
12899 bool member = ctx && TYPE_P (ctx);
12900
12901 if (member && !closure)
12902 ctx = tsubst_aggr_type (ctx, args,
12903 complain, t, /*entering_scope=*/1);
12904
12905 tree type = (lambda_fntype ? lambda_fntype
12906 : tsubst (TREE_TYPE (t), args,
12907 complain | tf_fndecl_type, in_decl));
12908 if (type == error_mark_node)
12909 return error_mark_node;
12910
12911 /* If we hit excessive deduction depth, the type is bogus even if
12912 it isn't error_mark_node, so don't build a decl. */
12913 if (excessive_deduction_depth)
12914 return error_mark_node;
12915
12916 /* We do NOT check for matching decls pushed separately at this
12917 point, as they may not represent instantiations of this
12918 template, and in any case are considered separate under the
12919 discrete model. */
12920 tree r = copy_decl (t);
12921 DECL_USE_TEMPLATE (r) = 0;
12922 TREE_TYPE (r) = type;
12923 /* Clear out the mangled name and RTL for the instantiation. */
12924 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12925 SET_DECL_RTL (r, NULL);
12926 /* Leave DECL_INITIAL set on deleted instantiations. */
12927 if (!DECL_DELETED_FN (r))
12928 DECL_INITIAL (r) = NULL_TREE;
12929 DECL_CONTEXT (r) = ctx;
12930
12931 /* OpenMP UDRs have the only argument a reference to the declared
12932 type. We want to diagnose if the declared type is a reference,
12933 which is invalid, but as references to references are usually
12934 quietly merged, diagnose it here. */
12935 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12936 {
12937 tree argtype
12938 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12939 argtype = tsubst (argtype, args, complain, in_decl);
12940 if (TYPE_REF_P (argtype))
12941 error_at (DECL_SOURCE_LOCATION (t),
12942 "reference type %qT in "
12943 "%<#pragma omp declare reduction%>", argtype);
12944 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12945 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12946 argtype);
12947 }
12948
12949 if (member && DECL_CONV_FN_P (r))
12950 /* Type-conversion operator. Reconstruct the name, in
12951 case it's the name of one of the template's parameters. */
12952 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12953
12954 tree parms = DECL_ARGUMENTS (t);
12955 if (closure)
12956 parms = DECL_CHAIN (parms);
12957 parms = tsubst (parms, args, complain, t);
12958 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12959 DECL_CONTEXT (parm) = r;
12960 if (closure)
12961 {
12962 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12963 DECL_CHAIN (tparm) = parms;
12964 parms = tparm;
12965 }
12966 DECL_ARGUMENTS (r) = parms;
12967 DECL_RESULT (r) = NULL_TREE;
12968
12969 TREE_STATIC (r) = 0;
12970 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12971 DECL_EXTERNAL (r) = 1;
12972 /* If this is an instantiation of a function with internal
12973 linkage, we already know what object file linkage will be
12974 assigned to the instantiation. */
12975 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12976 DECL_DEFER_OUTPUT (r) = 0;
12977 DECL_CHAIN (r) = NULL_TREE;
12978 DECL_PENDING_INLINE_INFO (r) = 0;
12979 DECL_PENDING_INLINE_P (r) = 0;
12980 DECL_SAVED_TREE (r) = NULL_TREE;
12981 DECL_STRUCT_FUNCTION (r) = NULL;
12982 TREE_USED (r) = 0;
12983 /* We'll re-clone as appropriate in instantiate_template. */
12984 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12985
12986 /* If we aren't complaining now, return on error before we register
12987 the specialization so that we'll complain eventually. */
12988 if ((complain & tf_error) == 0
12989 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12990 && !grok_op_properties (r, /*complain=*/false))
12991 return error_mark_node;
12992
12993 /* When instantiating a constrained member, substitute
12994 into the constraints to create a new constraint. */
12995 if (tree ci = get_constraints (t))
12996 if (member)
12997 {
12998 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12999 set_constraints (r, ci);
13000 }
13001
13002 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13003 this in the special friend case mentioned above where
13004 GEN_TMPL is NULL. */
13005 if (gen_tmpl && !closure)
13006 {
13007 DECL_TEMPLATE_INFO (r)
13008 = build_template_info (gen_tmpl, argvec);
13009 SET_DECL_IMPLICIT_INSTANTIATION (r);
13010
13011 tree new_r
13012 = register_specialization (r, gen_tmpl, argvec, false, hash);
13013 if (new_r != r)
13014 /* We instantiated this while substituting into
13015 the type earlier (template/friend54.C). */
13016 return new_r;
13017
13018 /* We're not supposed to instantiate default arguments
13019 until they are called, for a template. But, for a
13020 declaration like:
13021
13022 template <class T> void f ()
13023 { extern void g(int i = T()); }
13024
13025 we should do the substitution when the template is
13026 instantiated. We handle the member function case in
13027 instantiate_class_template since the default arguments
13028 might refer to other members of the class. */
13029 if (!member
13030 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13031 && !uses_template_parms (argvec))
13032 tsubst_default_arguments (r, complain);
13033 }
13034 else
13035 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13036
13037 /* Copy the list of befriending classes. */
13038 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13039 *friends;
13040 friends = &TREE_CHAIN (*friends))
13041 {
13042 *friends = copy_node (*friends);
13043 TREE_VALUE (*friends)
13044 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13045 }
13046
13047 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13048 {
13049 maybe_retrofit_in_chrg (r);
13050 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13051 return error_mark_node;
13052 /* If this is an instantiation of a member template, clone it.
13053 If it isn't, that'll be handled by
13054 clone_constructors_and_destructors. */
13055 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13056 clone_function_decl (r, /*update_methods=*/false);
13057 }
13058 else if ((complain & tf_error) != 0
13059 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13060 && !grok_op_properties (r, /*complain=*/true))
13061 return error_mark_node;
13062
13063 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13064 SET_DECL_FRIEND_CONTEXT (r,
13065 tsubst (DECL_FRIEND_CONTEXT (t),
13066 args, complain, in_decl));
13067
13068 /* Possibly limit visibility based on template args. */
13069 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13070 if (DECL_VISIBILITY_SPECIFIED (t))
13071 {
13072 DECL_VISIBILITY_SPECIFIED (r) = 0;
13073 DECL_ATTRIBUTES (r)
13074 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13075 }
13076 determine_visibility (r);
13077 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13078 && !processing_template_decl)
13079 defaulted_late_check (r);
13080
13081 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13082 args, complain, in_decl);
13083 return r;
13084 }
13085
13086 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13087
13088 static tree
13089 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13090 tree lambda_fntype)
13091 {
13092 /* We can get here when processing a member function template,
13093 member class template, or template template parameter. */
13094 tree decl = DECL_TEMPLATE_RESULT (t);
13095 tree in_decl = t;
13096 tree spec;
13097 tree tmpl_args;
13098 tree full_args;
13099 tree r;
13100 hashval_t hash = 0;
13101
13102 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13103 {
13104 /* Template template parameter is treated here. */
13105 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13106 if (new_type == error_mark_node)
13107 r = error_mark_node;
13108 /* If we get a real template back, return it. This can happen in
13109 the context of most_specialized_partial_spec. */
13110 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13111 r = new_type;
13112 else
13113 /* The new TEMPLATE_DECL was built in
13114 reduce_template_parm_level. */
13115 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13116 return r;
13117 }
13118
13119 if (!lambda_fntype)
13120 {
13121 /* We might already have an instance of this template.
13122 The ARGS are for the surrounding class type, so the
13123 full args contain the tsubst'd args for the context,
13124 plus the innermost args from the template decl. */
13125 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13126 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13127 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13128 /* Because this is a template, the arguments will still be
13129 dependent, even after substitution. If
13130 PROCESSING_TEMPLATE_DECL is not set, the dependency
13131 predicates will short-circuit. */
13132 ++processing_template_decl;
13133 full_args = tsubst_template_args (tmpl_args, args,
13134 complain, in_decl);
13135 --processing_template_decl;
13136 if (full_args == error_mark_node)
13137 return error_mark_node;
13138
13139 /* If this is a default template template argument,
13140 tsubst might not have changed anything. */
13141 if (full_args == tmpl_args)
13142 return t;
13143
13144 hash = hash_tmpl_and_args (t, full_args);
13145 spec = retrieve_specialization (t, full_args, hash);
13146 if (spec != NULL_TREE)
13147 return spec;
13148 }
13149
13150 /* Make a new template decl. It will be similar to the
13151 original, but will record the current template arguments.
13152 We also create a new function declaration, which is just
13153 like the old one, but points to this new template, rather
13154 than the old one. */
13155 r = copy_decl (t);
13156 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13157 DECL_CHAIN (r) = NULL_TREE;
13158
13159 // Build new template info linking to the original template decl.
13160 if (!lambda_fntype)
13161 {
13162 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13163 SET_DECL_IMPLICIT_INSTANTIATION (r);
13164 }
13165 else
13166 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13167
13168 /* The template parameters for this new template are all the
13169 template parameters for the old template, except the
13170 outermost level of parameters. */
13171 DECL_TEMPLATE_PARMS (r)
13172 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13173 complain);
13174
13175 if (TREE_CODE (decl) == TYPE_DECL
13176 && !TYPE_DECL_ALIAS_P (decl))
13177 {
13178 tree new_type;
13179 ++processing_template_decl;
13180 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13181 --processing_template_decl;
13182 if (new_type == error_mark_node)
13183 return error_mark_node;
13184
13185 TREE_TYPE (r) = new_type;
13186 /* For a partial specialization, we need to keep pointing to
13187 the primary template. */
13188 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13189 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13190 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13191 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13192 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13193 }
13194 else
13195 {
13196 tree new_decl;
13197 ++processing_template_decl;
13198 if (TREE_CODE (decl) == FUNCTION_DECL)
13199 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13200 else
13201 new_decl = tsubst (decl, args, complain, in_decl);
13202 --processing_template_decl;
13203 if (new_decl == error_mark_node)
13204 return error_mark_node;
13205
13206 DECL_TEMPLATE_RESULT (r) = new_decl;
13207 TREE_TYPE (r) = TREE_TYPE (new_decl);
13208 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13209 if (lambda_fntype)
13210 {
13211 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13212 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13213 }
13214 else
13215 {
13216 DECL_TI_TEMPLATE (new_decl) = r;
13217 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13218 }
13219 }
13220
13221 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13222 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13223
13224 if (PRIMARY_TEMPLATE_P (t))
13225 DECL_PRIMARY_TEMPLATE (r) = r;
13226
13227 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13228 && !lambda_fntype)
13229 /* Record this non-type partial instantiation. */
13230 register_specialization (r, t,
13231 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13232 false, hash);
13233
13234 return r;
13235 }
13236
13237 /* True if FN is the op() for a lambda in an uninstantiated template. */
13238
13239 bool
13240 lambda_fn_in_template_p (tree fn)
13241 {
13242 if (!fn || !LAMBDA_FUNCTION_P (fn))
13243 return false;
13244 tree closure = DECL_CONTEXT (fn);
13245 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13246 }
13247
13248 /* We're instantiating a variable from template function TCTX. Return the
13249 corresponding current enclosing scope. This gets complicated because lambda
13250 functions in templates are regenerated rather than instantiated, but generic
13251 lambda functions are subsequently instantiated. */
13252
13253 static tree
13254 enclosing_instantiation_of (tree otctx)
13255 {
13256 tree tctx = otctx;
13257 tree fn = current_function_decl;
13258 int lambda_count = 0;
13259
13260 for (; tctx && lambda_fn_in_template_p (tctx);
13261 tctx = decl_function_context (tctx))
13262 ++lambda_count;
13263 for (; fn; fn = decl_function_context (fn))
13264 {
13265 tree ofn = fn;
13266 int flambda_count = 0;
13267 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
13268 fn = decl_function_context (fn))
13269 ++flambda_count;
13270 if ((fn && DECL_TEMPLATE_INFO (fn))
13271 ? most_general_template (fn) != most_general_template (tctx)
13272 : fn != tctx)
13273 continue;
13274 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13275 || DECL_CONV_FN_P (ofn));
13276 return ofn;
13277 }
13278 gcc_unreachable ();
13279 }
13280
13281 /* Substitute the ARGS into the T, which is a _DECL. Return the
13282 result of the substitution. Issue error and warning messages under
13283 control of COMPLAIN. */
13284
13285 static tree
13286 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13287 {
13288 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13289 location_t saved_loc;
13290 tree r = NULL_TREE;
13291 tree in_decl = t;
13292 hashval_t hash = 0;
13293
13294 /* Set the filename and linenumber to improve error-reporting. */
13295 saved_loc = input_location;
13296 input_location = DECL_SOURCE_LOCATION (t);
13297
13298 switch (TREE_CODE (t))
13299 {
13300 case TEMPLATE_DECL:
13301 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13302 break;
13303
13304 case FUNCTION_DECL:
13305 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13306 break;
13307
13308 case PARM_DECL:
13309 {
13310 tree type = NULL_TREE;
13311 int i, len = 1;
13312 tree expanded_types = NULL_TREE;
13313 tree prev_r = NULL_TREE;
13314 tree first_r = NULL_TREE;
13315
13316 if (DECL_PACK_P (t))
13317 {
13318 /* If there is a local specialization that isn't a
13319 parameter pack, it means that we're doing a "simple"
13320 substitution from inside tsubst_pack_expansion. Just
13321 return the local specialization (which will be a single
13322 parm). */
13323 tree spec = retrieve_local_specialization (t);
13324 if (spec
13325 && TREE_CODE (spec) == PARM_DECL
13326 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13327 RETURN (spec);
13328
13329 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13330 the parameters in this function parameter pack. */
13331 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13332 complain, in_decl);
13333 if (TREE_CODE (expanded_types) == TREE_VEC)
13334 {
13335 len = TREE_VEC_LENGTH (expanded_types);
13336
13337 /* Zero-length parameter packs are boring. Just substitute
13338 into the chain. */
13339 if (len == 0)
13340 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13341 TREE_CHAIN (t)));
13342 }
13343 else
13344 {
13345 /* All we did was update the type. Make a note of that. */
13346 type = expanded_types;
13347 expanded_types = NULL_TREE;
13348 }
13349 }
13350
13351 /* Loop through all of the parameters we'll build. When T is
13352 a function parameter pack, LEN is the number of expanded
13353 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13354 r = NULL_TREE;
13355 for (i = 0; i < len; ++i)
13356 {
13357 prev_r = r;
13358 r = copy_node (t);
13359 if (DECL_TEMPLATE_PARM_P (t))
13360 SET_DECL_TEMPLATE_PARM_P (r);
13361
13362 if (expanded_types)
13363 /* We're on the Ith parameter of the function parameter
13364 pack. */
13365 {
13366 /* Get the Ith type. */
13367 type = TREE_VEC_ELT (expanded_types, i);
13368
13369 /* Rename the parameter to include the index. */
13370 DECL_NAME (r)
13371 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13372 }
13373 else if (!type)
13374 /* We're dealing with a normal parameter. */
13375 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13376
13377 type = type_decays_to (type);
13378 TREE_TYPE (r) = type;
13379 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13380
13381 if (DECL_INITIAL (r))
13382 {
13383 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13384 DECL_INITIAL (r) = TREE_TYPE (r);
13385 else
13386 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13387 complain, in_decl);
13388 }
13389
13390 DECL_CONTEXT (r) = NULL_TREE;
13391
13392 if (!DECL_TEMPLATE_PARM_P (r))
13393 DECL_ARG_TYPE (r) = type_passed_as (type);
13394
13395 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13396 args, complain, in_decl);
13397
13398 /* Keep track of the first new parameter we
13399 generate. That's what will be returned to the
13400 caller. */
13401 if (!first_r)
13402 first_r = r;
13403
13404 /* Build a proper chain of parameters when substituting
13405 into a function parameter pack. */
13406 if (prev_r)
13407 DECL_CHAIN (prev_r) = r;
13408 }
13409
13410 /* If cp_unevaluated_operand is set, we're just looking for a
13411 single dummy parameter, so don't keep going. */
13412 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13413 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13414 complain, DECL_CHAIN (t));
13415
13416 /* FIRST_R contains the start of the chain we've built. */
13417 r = first_r;
13418 }
13419 break;
13420
13421 case FIELD_DECL:
13422 {
13423 tree type = NULL_TREE;
13424 tree vec = NULL_TREE;
13425 tree expanded_types = NULL_TREE;
13426 int len = 1;
13427
13428 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13429 {
13430 /* This field is a lambda capture pack. Return a TREE_VEC of
13431 the expanded fields to instantiate_class_template_1. */
13432 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13433 complain, in_decl);
13434 if (TREE_CODE (expanded_types) == TREE_VEC)
13435 {
13436 len = TREE_VEC_LENGTH (expanded_types);
13437 vec = make_tree_vec (len);
13438 }
13439 else
13440 {
13441 /* All we did was update the type. Make a note of that. */
13442 type = expanded_types;
13443 expanded_types = NULL_TREE;
13444 }
13445 }
13446
13447 for (int i = 0; i < len; ++i)
13448 {
13449 r = copy_decl (t);
13450 if (expanded_types)
13451 {
13452 type = TREE_VEC_ELT (expanded_types, i);
13453 DECL_NAME (r)
13454 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13455 }
13456 else if (!type)
13457 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13458
13459 if (type == error_mark_node)
13460 RETURN (error_mark_node);
13461 TREE_TYPE (r) = type;
13462 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13463
13464 if (DECL_C_BIT_FIELD (r))
13465 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13466 number of bits. */
13467 DECL_BIT_FIELD_REPRESENTATIVE (r)
13468 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13469 complain, in_decl,
13470 /*integral_constant_expression_p=*/true);
13471 if (DECL_INITIAL (t))
13472 {
13473 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13474 NSDMI in perform_member_init. Still set DECL_INITIAL
13475 so that we know there is one. */
13476 DECL_INITIAL (r) = void_node;
13477 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13478 retrofit_lang_decl (r);
13479 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13480 }
13481 /* We don't have to set DECL_CONTEXT here; it is set by
13482 finish_member_declaration. */
13483 DECL_CHAIN (r) = NULL_TREE;
13484
13485 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13486 args, complain, in_decl);
13487
13488 if (vec)
13489 TREE_VEC_ELT (vec, i) = r;
13490 }
13491
13492 if (vec)
13493 r = vec;
13494 }
13495 break;
13496
13497 case USING_DECL:
13498 /* We reach here only for member using decls. We also need to check
13499 uses_template_parms because DECL_DEPENDENT_P is not set for a
13500 using-declaration that designates a member of the current
13501 instantiation (c++/53549). */
13502 if (DECL_DEPENDENT_P (t)
13503 || uses_template_parms (USING_DECL_SCOPE (t)))
13504 {
13505 tree scope = USING_DECL_SCOPE (t);
13506 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13507 if (PACK_EXPANSION_P (scope))
13508 {
13509 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13510 int len = TREE_VEC_LENGTH (vec);
13511 r = make_tree_vec (len);
13512 for (int i = 0; i < len; ++i)
13513 {
13514 tree escope = TREE_VEC_ELT (vec, i);
13515 tree elt = do_class_using_decl (escope, name);
13516 if (!elt)
13517 {
13518 r = error_mark_node;
13519 break;
13520 }
13521 else
13522 {
13523 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13524 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13525 }
13526 TREE_VEC_ELT (r, i) = elt;
13527 }
13528 }
13529 else
13530 {
13531 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13532 complain, in_decl);
13533 r = do_class_using_decl (inst_scope, name);
13534 if (!r)
13535 r = error_mark_node;
13536 else
13537 {
13538 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13539 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13540 }
13541 }
13542 }
13543 else
13544 {
13545 r = copy_node (t);
13546 DECL_CHAIN (r) = NULL_TREE;
13547 }
13548 break;
13549
13550 case TYPE_DECL:
13551 case VAR_DECL:
13552 {
13553 tree argvec = NULL_TREE;
13554 tree gen_tmpl = NULL_TREE;
13555 tree spec;
13556 tree tmpl = NULL_TREE;
13557 tree ctx;
13558 tree type = NULL_TREE;
13559 bool local_p;
13560
13561 if (TREE_TYPE (t) == error_mark_node)
13562 RETURN (error_mark_node);
13563
13564 if (TREE_CODE (t) == TYPE_DECL
13565 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13566 {
13567 /* If this is the canonical decl, we don't have to
13568 mess with instantiations, and often we can't (for
13569 typename, template type parms and such). Note that
13570 TYPE_NAME is not correct for the above test if
13571 we've copied the type for a typedef. */
13572 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13573 if (type == error_mark_node)
13574 RETURN (error_mark_node);
13575 r = TYPE_NAME (type);
13576 break;
13577 }
13578
13579 /* Check to see if we already have the specialization we
13580 need. */
13581 spec = NULL_TREE;
13582 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13583 {
13584 /* T is a static data member or namespace-scope entity.
13585 We have to substitute into namespace-scope variables
13586 (not just variable templates) because of cases like:
13587
13588 template <class T> void f() { extern T t; }
13589
13590 where the entity referenced is not known until
13591 instantiation time. */
13592 local_p = false;
13593 ctx = DECL_CONTEXT (t);
13594 if (DECL_CLASS_SCOPE_P (t))
13595 {
13596 ctx = tsubst_aggr_type (ctx, args,
13597 complain,
13598 in_decl, /*entering_scope=*/1);
13599 /* If CTX is unchanged, then T is in fact the
13600 specialization we want. That situation occurs when
13601 referencing a static data member within in its own
13602 class. We can use pointer equality, rather than
13603 same_type_p, because DECL_CONTEXT is always
13604 canonical... */
13605 if (ctx == DECL_CONTEXT (t)
13606 /* ... unless T is a member template; in which
13607 case our caller can be willing to create a
13608 specialization of that template represented
13609 by T. */
13610 && !(DECL_TI_TEMPLATE (t)
13611 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13612 spec = t;
13613 }
13614
13615 if (!spec)
13616 {
13617 tmpl = DECL_TI_TEMPLATE (t);
13618 gen_tmpl = most_general_template (tmpl);
13619 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13620 if (argvec != error_mark_node)
13621 argvec = (coerce_innermost_template_parms
13622 (DECL_TEMPLATE_PARMS (gen_tmpl),
13623 argvec, t, complain,
13624 /*all*/true, /*defarg*/true));
13625 if (argvec == error_mark_node)
13626 RETURN (error_mark_node);
13627 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13628 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13629 }
13630 }
13631 else
13632 {
13633 /* A local variable. */
13634 local_p = true;
13635 /* Subsequent calls to pushdecl will fill this in. */
13636 ctx = NULL_TREE;
13637 /* Unless this is a reference to a static variable from an
13638 enclosing function, in which case we need to fill it in now. */
13639 if (TREE_STATIC (t))
13640 {
13641 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13642 if (fn != current_function_decl)
13643 ctx = fn;
13644 }
13645 spec = retrieve_local_specialization (t);
13646 }
13647 /* If we already have the specialization we need, there is
13648 nothing more to do. */
13649 if (spec)
13650 {
13651 r = spec;
13652 break;
13653 }
13654
13655 /* Create a new node for the specialization we need. */
13656 r = copy_decl (t);
13657 if (type == NULL_TREE)
13658 {
13659 if (is_typedef_decl (t))
13660 type = DECL_ORIGINAL_TYPE (t);
13661 else
13662 type = TREE_TYPE (t);
13663 if (VAR_P (t)
13664 && VAR_HAD_UNKNOWN_BOUND (t)
13665 && type != error_mark_node)
13666 type = strip_array_domain (type);
13667 tree sub_args = args;
13668 if (tree auto_node = type_uses_auto (type))
13669 {
13670 /* Mask off any template args past the variable's context so we
13671 don't replace the auto with an unrelated argument. */
13672 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13673 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13674 if (extra > 0)
13675 /* This should never happen with the new lambda instantiation
13676 model, but keep the handling just in case. */
13677 gcc_assert (!CHECKING_P),
13678 sub_args = strip_innermost_template_args (args, extra);
13679 }
13680 type = tsubst (type, sub_args, complain, in_decl);
13681 }
13682 if (VAR_P (r))
13683 {
13684 DECL_INITIALIZED_P (r) = 0;
13685 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13686 if (type == error_mark_node)
13687 RETURN (error_mark_node);
13688 if (TREE_CODE (type) == FUNCTION_TYPE)
13689 {
13690 /* It may seem that this case cannot occur, since:
13691
13692 typedef void f();
13693 void g() { f x; }
13694
13695 declares a function, not a variable. However:
13696
13697 typedef void f();
13698 template <typename T> void g() { T t; }
13699 template void g<f>();
13700
13701 is an attempt to declare a variable with function
13702 type. */
13703 error ("variable %qD has function type",
13704 /* R is not yet sufficiently initialized, so we
13705 just use its name. */
13706 DECL_NAME (r));
13707 RETURN (error_mark_node);
13708 }
13709 type = complete_type (type);
13710 /* Wait until cp_finish_decl to set this again, to handle
13711 circular dependency (template/instantiate6.C). */
13712 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13713 type = check_var_type (DECL_NAME (r), type);
13714
13715 if (DECL_HAS_VALUE_EXPR_P (t))
13716 {
13717 tree ve = DECL_VALUE_EXPR (t);
13718 ve = tsubst_expr (ve, args, complain, in_decl,
13719 /*constant_expression_p=*/false);
13720 if (REFERENCE_REF_P (ve))
13721 {
13722 gcc_assert (TYPE_REF_P (type));
13723 ve = TREE_OPERAND (ve, 0);
13724 }
13725 SET_DECL_VALUE_EXPR (r, ve);
13726 }
13727 if (CP_DECL_THREAD_LOCAL_P (r)
13728 && !processing_template_decl)
13729 set_decl_tls_model (r, decl_default_tls_model (r));
13730 }
13731 else if (DECL_SELF_REFERENCE_P (t))
13732 SET_DECL_SELF_REFERENCE_P (r);
13733 TREE_TYPE (r) = type;
13734 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13735 DECL_CONTEXT (r) = ctx;
13736 /* Clear out the mangled name and RTL for the instantiation. */
13737 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13738 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13739 SET_DECL_RTL (r, NULL);
13740 /* The initializer must not be expanded until it is required;
13741 see [temp.inst]. */
13742 DECL_INITIAL (r) = NULL_TREE;
13743 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13744 if (VAR_P (r))
13745 {
13746 if (DECL_LANG_SPECIFIC (r))
13747 SET_DECL_DEPENDENT_INIT_P (r, false);
13748
13749 SET_DECL_MODE (r, VOIDmode);
13750
13751 /* Possibly limit visibility based on template args. */
13752 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13753 if (DECL_VISIBILITY_SPECIFIED (t))
13754 {
13755 DECL_VISIBILITY_SPECIFIED (r) = 0;
13756 DECL_ATTRIBUTES (r)
13757 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13758 }
13759 determine_visibility (r);
13760 }
13761
13762 if (!local_p)
13763 {
13764 /* A static data member declaration is always marked
13765 external when it is declared in-class, even if an
13766 initializer is present. We mimic the non-template
13767 processing here. */
13768 DECL_EXTERNAL (r) = 1;
13769 if (DECL_NAMESPACE_SCOPE_P (t))
13770 DECL_NOT_REALLY_EXTERN (r) = 1;
13771
13772 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13773 SET_DECL_IMPLICIT_INSTANTIATION (r);
13774 register_specialization (r, gen_tmpl, argvec, false, hash);
13775 }
13776 else
13777 {
13778 if (DECL_LANG_SPECIFIC (r))
13779 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13780 if (!cp_unevaluated_operand)
13781 register_local_specialization (r, t);
13782 }
13783
13784 DECL_CHAIN (r) = NULL_TREE;
13785
13786 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13787 /*flags=*/0,
13788 args, complain, in_decl);
13789
13790 /* Preserve a typedef that names a type. */
13791 if (is_typedef_decl (r) && type != error_mark_node)
13792 {
13793 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13794 set_underlying_type (r);
13795 if (TYPE_DECL_ALIAS_P (r))
13796 /* An alias template specialization can be dependent
13797 even if its underlying type is not. */
13798 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13799 }
13800
13801 layout_decl (r, 0);
13802 }
13803 break;
13804
13805 default:
13806 gcc_unreachable ();
13807 }
13808 #undef RETURN
13809
13810 out:
13811 /* Restore the file and line information. */
13812 input_location = saved_loc;
13813
13814 return r;
13815 }
13816
13817 /* Substitute into the ARG_TYPES of a function type.
13818 If END is a TREE_CHAIN, leave it and any following types
13819 un-substituted. */
13820
13821 static tree
13822 tsubst_arg_types (tree arg_types,
13823 tree args,
13824 tree end,
13825 tsubst_flags_t complain,
13826 tree in_decl)
13827 {
13828 tree remaining_arg_types;
13829 tree type = NULL_TREE;
13830 int i = 1;
13831 tree expanded_args = NULL_TREE;
13832 tree default_arg;
13833
13834 if (!arg_types || arg_types == void_list_node || arg_types == end)
13835 return arg_types;
13836
13837 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13838 args, end, complain, in_decl);
13839 if (remaining_arg_types == error_mark_node)
13840 return error_mark_node;
13841
13842 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13843 {
13844 /* For a pack expansion, perform substitution on the
13845 entire expression. Later on, we'll handle the arguments
13846 one-by-one. */
13847 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13848 args, complain, in_decl);
13849
13850 if (TREE_CODE (expanded_args) == TREE_VEC)
13851 /* So that we'll spin through the parameters, one by one. */
13852 i = TREE_VEC_LENGTH (expanded_args);
13853 else
13854 {
13855 /* We only partially substituted into the parameter
13856 pack. Our type is TYPE_PACK_EXPANSION. */
13857 type = expanded_args;
13858 expanded_args = NULL_TREE;
13859 }
13860 }
13861
13862 while (i > 0) {
13863 --i;
13864
13865 if (expanded_args)
13866 type = TREE_VEC_ELT (expanded_args, i);
13867 else if (!type)
13868 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13869
13870 if (type == error_mark_node)
13871 return error_mark_node;
13872 if (VOID_TYPE_P (type))
13873 {
13874 if (complain & tf_error)
13875 {
13876 error ("invalid parameter type %qT", type);
13877 if (in_decl)
13878 error ("in declaration %q+D", in_decl);
13879 }
13880 return error_mark_node;
13881 }
13882 /* DR 657. */
13883 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13884 return error_mark_node;
13885
13886 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13887 top-level qualifiers as required. */
13888 type = cv_unqualified (type_decays_to (type));
13889
13890 /* We do not substitute into default arguments here. The standard
13891 mandates that they be instantiated only when needed, which is
13892 done in build_over_call. */
13893 default_arg = TREE_PURPOSE (arg_types);
13894
13895 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13896 since the new op() won't have any associated template arguments for us
13897 to refer to later. */
13898 if (lambda_fn_in_template_p (in_decl))
13899 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13900 false/*fn*/, false/*constexpr*/);
13901
13902 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13903 {
13904 /* We've instantiated a template before its default arguments
13905 have been parsed. This can happen for a nested template
13906 class, and is not an error unless we require the default
13907 argument in a call of this function. */
13908 remaining_arg_types =
13909 tree_cons (default_arg, type, remaining_arg_types);
13910 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13911 }
13912 else
13913 remaining_arg_types =
13914 hash_tree_cons (default_arg, type, remaining_arg_types);
13915 }
13916
13917 return remaining_arg_types;
13918 }
13919
13920 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13921 *not* handle the exception-specification for FNTYPE, because the
13922 initial substitution of explicitly provided template parameters
13923 during argument deduction forbids substitution into the
13924 exception-specification:
13925
13926 [temp.deduct]
13927
13928 All references in the function type of the function template to the
13929 corresponding template parameters are replaced by the specified tem-
13930 plate argument values. If a substitution in a template parameter or
13931 in the function type of the function template results in an invalid
13932 type, type deduction fails. [Note: The equivalent substitution in
13933 exception specifications is done only when the function is instanti-
13934 ated, at which point a program is ill-formed if the substitution
13935 results in an invalid type.] */
13936
13937 static tree
13938 tsubst_function_type (tree t,
13939 tree args,
13940 tsubst_flags_t complain,
13941 tree in_decl)
13942 {
13943 tree return_type;
13944 tree arg_types = NULL_TREE;
13945 tree fntype;
13946
13947 /* The TYPE_CONTEXT is not used for function/method types. */
13948 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13949
13950 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13951 failure. */
13952 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13953
13954 if (late_return_type_p)
13955 {
13956 /* Substitute the argument types. */
13957 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13958 complain, in_decl);
13959 if (arg_types == error_mark_node)
13960 return error_mark_node;
13961
13962 tree save_ccp = current_class_ptr;
13963 tree save_ccr = current_class_ref;
13964 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13965 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13966 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13967 if (do_inject)
13968 {
13969 /* DR 1207: 'this' is in scope in the trailing return type. */
13970 inject_this_parameter (this_type, cp_type_quals (this_type));
13971 }
13972
13973 /* Substitute the return type. */
13974 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13975
13976 if (do_inject)
13977 {
13978 current_class_ptr = save_ccp;
13979 current_class_ref = save_ccr;
13980 }
13981 }
13982 else
13983 /* Substitute the return type. */
13984 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13985
13986 if (return_type == error_mark_node)
13987 return error_mark_node;
13988 /* DR 486 clarifies that creation of a function type with an
13989 invalid return type is a deduction failure. */
13990 if (TREE_CODE (return_type) == ARRAY_TYPE
13991 || TREE_CODE (return_type) == FUNCTION_TYPE)
13992 {
13993 if (complain & tf_error)
13994 {
13995 if (TREE_CODE (return_type) == ARRAY_TYPE)
13996 error ("function returning an array");
13997 else
13998 error ("function returning a function");
13999 }
14000 return error_mark_node;
14001 }
14002 /* And DR 657. */
14003 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14004 return error_mark_node;
14005
14006 if (!late_return_type_p)
14007 {
14008 /* Substitute the argument types. */
14009 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14010 complain, in_decl);
14011 if (arg_types == error_mark_node)
14012 return error_mark_node;
14013 }
14014
14015 /* Construct a new type node and return it. */
14016 if (TREE_CODE (t) == FUNCTION_TYPE)
14017 {
14018 fntype = build_function_type (return_type, arg_types);
14019 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14020 }
14021 else
14022 {
14023 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14024 /* Don't pick up extra function qualifiers from the basetype. */
14025 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14026 if (! MAYBE_CLASS_TYPE_P (r))
14027 {
14028 /* [temp.deduct]
14029
14030 Type deduction may fail for any of the following
14031 reasons:
14032
14033 -- Attempting to create "pointer to member of T" when T
14034 is not a class type. */
14035 if (complain & tf_error)
14036 error ("creating pointer to member function of non-class type %qT",
14037 r);
14038 return error_mark_node;
14039 }
14040
14041 fntype = build_method_type_directly (r, return_type,
14042 TREE_CHAIN (arg_types));
14043 }
14044 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14045
14046 /* See comment above. */
14047 tree raises = NULL_TREE;
14048 cp_ref_qualifier rqual = type_memfn_rqual (t);
14049 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14050
14051 return fntype;
14052 }
14053
14054 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14055 ARGS into that specification, and return the substituted
14056 specification. If there is no specification, return NULL_TREE. */
14057
14058 static tree
14059 tsubst_exception_specification (tree fntype,
14060 tree args,
14061 tsubst_flags_t complain,
14062 tree in_decl,
14063 bool defer_ok)
14064 {
14065 tree specs;
14066 tree new_specs;
14067
14068 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14069 new_specs = NULL_TREE;
14070 if (specs && TREE_PURPOSE (specs))
14071 {
14072 /* A noexcept-specifier. */
14073 tree expr = TREE_PURPOSE (specs);
14074 if (TREE_CODE (expr) == INTEGER_CST)
14075 new_specs = expr;
14076 else if (defer_ok)
14077 {
14078 /* Defer instantiation of noexcept-specifiers to avoid
14079 excessive instantiations (c++/49107). */
14080 new_specs = make_node (DEFERRED_NOEXCEPT);
14081 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14082 {
14083 /* We already partially instantiated this member template,
14084 so combine the new args with the old. */
14085 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14086 = DEFERRED_NOEXCEPT_PATTERN (expr);
14087 DEFERRED_NOEXCEPT_ARGS (new_specs)
14088 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14089 }
14090 else
14091 {
14092 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14093 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14094 }
14095 }
14096 else
14097 new_specs = tsubst_copy_and_build
14098 (expr, args, complain, in_decl, /*function_p=*/false,
14099 /*integral_constant_expression_p=*/true);
14100 new_specs = build_noexcept_spec (new_specs, complain);
14101 }
14102 else if (specs)
14103 {
14104 if (! TREE_VALUE (specs))
14105 new_specs = specs;
14106 else
14107 while (specs)
14108 {
14109 tree spec;
14110 int i, len = 1;
14111 tree expanded_specs = NULL_TREE;
14112
14113 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14114 {
14115 /* Expand the pack expansion type. */
14116 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14117 args, complain,
14118 in_decl);
14119
14120 if (expanded_specs == error_mark_node)
14121 return error_mark_node;
14122 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14123 len = TREE_VEC_LENGTH (expanded_specs);
14124 else
14125 {
14126 /* We're substituting into a member template, so
14127 we got a TYPE_PACK_EXPANSION back. Add that
14128 expansion and move on. */
14129 gcc_assert (TREE_CODE (expanded_specs)
14130 == TYPE_PACK_EXPANSION);
14131 new_specs = add_exception_specifier (new_specs,
14132 expanded_specs,
14133 complain);
14134 specs = TREE_CHAIN (specs);
14135 continue;
14136 }
14137 }
14138
14139 for (i = 0; i < len; ++i)
14140 {
14141 if (expanded_specs)
14142 spec = TREE_VEC_ELT (expanded_specs, i);
14143 else
14144 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14145 if (spec == error_mark_node)
14146 return spec;
14147 new_specs = add_exception_specifier (new_specs, spec,
14148 complain);
14149 }
14150
14151 specs = TREE_CHAIN (specs);
14152 }
14153 }
14154 return new_specs;
14155 }
14156
14157 /* Take the tree structure T and replace template parameters used
14158 therein with the argument vector ARGS. IN_DECL is an associated
14159 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14160 Issue error and warning messages under control of COMPLAIN. Note
14161 that we must be relatively non-tolerant of extensions here, in
14162 order to preserve conformance; if we allow substitutions that
14163 should not be allowed, we may allow argument deductions that should
14164 not succeed, and therefore report ambiguous overload situations
14165 where there are none. In theory, we could allow the substitution,
14166 but indicate that it should have failed, and allow our caller to
14167 make sure that the right thing happens, but we don't try to do this
14168 yet.
14169
14170 This function is used for dealing with types, decls and the like;
14171 for expressions, use tsubst_expr or tsubst_copy. */
14172
14173 tree
14174 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14175 {
14176 enum tree_code code;
14177 tree type, r = NULL_TREE;
14178
14179 if (t == NULL_TREE || t == error_mark_node
14180 || t == integer_type_node
14181 || t == void_type_node
14182 || t == char_type_node
14183 || t == unknown_type_node
14184 || TREE_CODE (t) == NAMESPACE_DECL
14185 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14186 return t;
14187
14188 if (DECL_P (t))
14189 return tsubst_decl (t, args, complain);
14190
14191 if (args == NULL_TREE)
14192 return t;
14193
14194 code = TREE_CODE (t);
14195
14196 if (code == IDENTIFIER_NODE)
14197 type = IDENTIFIER_TYPE_VALUE (t);
14198 else
14199 type = TREE_TYPE (t);
14200
14201 gcc_assert (type != unknown_type_node);
14202
14203 /* Reuse typedefs. We need to do this to handle dependent attributes,
14204 such as attribute aligned. */
14205 if (TYPE_P (t)
14206 && typedef_variant_p (t))
14207 {
14208 tree decl = TYPE_NAME (t);
14209
14210 if (alias_template_specialization_p (t))
14211 {
14212 /* DECL represents an alias template and we want to
14213 instantiate it. */
14214 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14215 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14216 r = instantiate_alias_template (tmpl, gen_args, complain);
14217 }
14218 else if (DECL_CLASS_SCOPE_P (decl)
14219 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14220 && uses_template_parms (DECL_CONTEXT (decl)))
14221 {
14222 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14223 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14224 r = retrieve_specialization (tmpl, gen_args, 0);
14225 }
14226 else if (DECL_FUNCTION_SCOPE_P (decl)
14227 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14228 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14229 r = retrieve_local_specialization (decl);
14230 else
14231 /* The typedef is from a non-template context. */
14232 return t;
14233
14234 if (r)
14235 {
14236 r = TREE_TYPE (r);
14237 r = cp_build_qualified_type_real
14238 (r, cp_type_quals (t) | cp_type_quals (r),
14239 complain | tf_ignore_bad_quals);
14240 return r;
14241 }
14242 else
14243 {
14244 /* We don't have an instantiation yet, so drop the typedef. */
14245 int quals = cp_type_quals (t);
14246 t = DECL_ORIGINAL_TYPE (decl);
14247 t = cp_build_qualified_type_real (t, quals,
14248 complain | tf_ignore_bad_quals);
14249 }
14250 }
14251
14252 bool fndecl_type = (complain & tf_fndecl_type);
14253 complain &= ~tf_fndecl_type;
14254
14255 if (type
14256 && code != TYPENAME_TYPE
14257 && code != TEMPLATE_TYPE_PARM
14258 && code != TEMPLATE_PARM_INDEX
14259 && code != IDENTIFIER_NODE
14260 && code != FUNCTION_TYPE
14261 && code != METHOD_TYPE)
14262 type = tsubst (type, args, complain, in_decl);
14263 if (type == error_mark_node)
14264 return error_mark_node;
14265
14266 switch (code)
14267 {
14268 case RECORD_TYPE:
14269 case UNION_TYPE:
14270 case ENUMERAL_TYPE:
14271 return tsubst_aggr_type (t, args, complain, in_decl,
14272 /*entering_scope=*/0);
14273
14274 case ERROR_MARK:
14275 case IDENTIFIER_NODE:
14276 case VOID_TYPE:
14277 case REAL_TYPE:
14278 case COMPLEX_TYPE:
14279 case VECTOR_TYPE:
14280 case BOOLEAN_TYPE:
14281 case NULLPTR_TYPE:
14282 case LANG_TYPE:
14283 return t;
14284
14285 case INTEGER_TYPE:
14286 if (t == integer_type_node)
14287 return t;
14288
14289 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14290 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14291 return t;
14292
14293 {
14294 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14295
14296 max = tsubst_expr (omax, args, complain, in_decl,
14297 /*integral_constant_expression_p=*/false);
14298
14299 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14300 needed. */
14301 if (TREE_CODE (max) == NOP_EXPR
14302 && TREE_SIDE_EFFECTS (omax)
14303 && !TREE_TYPE (max))
14304 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14305
14306 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14307 with TREE_SIDE_EFFECTS that indicates this is not an integral
14308 constant expression. */
14309 if (processing_template_decl
14310 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14311 {
14312 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14313 TREE_SIDE_EFFECTS (max) = 1;
14314 }
14315
14316 return compute_array_index_type (NULL_TREE, max, complain);
14317 }
14318
14319 case TEMPLATE_TYPE_PARM:
14320 case TEMPLATE_TEMPLATE_PARM:
14321 case BOUND_TEMPLATE_TEMPLATE_PARM:
14322 case TEMPLATE_PARM_INDEX:
14323 {
14324 int idx;
14325 int level;
14326 int levels;
14327 tree arg = NULL_TREE;
14328
14329 /* Early in template argument deduction substitution, we don't
14330 want to reduce the level of 'auto', or it will be confused
14331 with a normal template parm in subsequent deduction. */
14332 if (is_auto (t) && (complain & tf_partial))
14333 return t;
14334
14335 r = NULL_TREE;
14336
14337 gcc_assert (TREE_VEC_LENGTH (args) > 0);
14338 template_parm_level_and_index (t, &level, &idx);
14339
14340 levels = TMPL_ARGS_DEPTH (args);
14341 if (level <= levels
14342 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14343 {
14344 arg = TMPL_ARG (args, level, idx);
14345
14346 /* See through ARGUMENT_PACK_SELECT arguments. */
14347 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14348 arg = argument_pack_select_arg (arg);
14349 }
14350
14351 if (arg == error_mark_node)
14352 return error_mark_node;
14353 else if (arg != NULL_TREE)
14354 {
14355 if (ARGUMENT_PACK_P (arg))
14356 /* If ARG is an argument pack, we don't actually want to
14357 perform a substitution here, because substitutions
14358 for argument packs are only done
14359 element-by-element. We can get to this point when
14360 substituting the type of a non-type template
14361 parameter pack, when that type actually contains
14362 template parameter packs from an outer template, e.g.,
14363
14364 template<typename... Types> struct A {
14365 template<Types... Values> struct B { };
14366 }; */
14367 return t;
14368
14369 if (code == TEMPLATE_TYPE_PARM)
14370 {
14371 int quals;
14372 gcc_assert (TYPE_P (arg));
14373
14374 quals = cp_type_quals (arg) | cp_type_quals (t);
14375
14376 return cp_build_qualified_type_real
14377 (arg, quals, complain | tf_ignore_bad_quals);
14378 }
14379 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14380 {
14381 /* We are processing a type constructed from a
14382 template template parameter. */
14383 tree argvec = tsubst (TYPE_TI_ARGS (t),
14384 args, complain, in_decl);
14385 if (argvec == error_mark_node)
14386 return error_mark_node;
14387
14388 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14389 || TREE_CODE (arg) == TEMPLATE_DECL
14390 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14391
14392 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14393 /* Consider this code:
14394
14395 template <template <class> class Template>
14396 struct Internal {
14397 template <class Arg> using Bind = Template<Arg>;
14398 };
14399
14400 template <template <class> class Template, class Arg>
14401 using Instantiate = Template<Arg>; //#0
14402
14403 template <template <class> class Template,
14404 class Argument>
14405 using Bind =
14406 Instantiate<Internal<Template>::template Bind,
14407 Argument>; //#1
14408
14409 When #1 is parsed, the
14410 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14411 parameter `Template' in #0 matches the
14412 UNBOUND_CLASS_TEMPLATE representing the argument
14413 `Internal<Template>::template Bind'; We then want
14414 to assemble the type `Bind<Argument>' that can't
14415 be fully created right now, because
14416 `Internal<Template>' not being complete, the Bind
14417 template cannot be looked up in that context. So
14418 we need to "store" `Bind<Argument>' for later
14419 when the context of Bind becomes complete. Let's
14420 store that in a TYPENAME_TYPE. */
14421 return make_typename_type (TYPE_CONTEXT (arg),
14422 build_nt (TEMPLATE_ID_EXPR,
14423 TYPE_IDENTIFIER (arg),
14424 argvec),
14425 typename_type,
14426 complain);
14427
14428 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14429 are resolving nested-types in the signature of a
14430 member function templates. Otherwise ARG is a
14431 TEMPLATE_DECL and is the real template to be
14432 instantiated. */
14433 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14434 arg = TYPE_NAME (arg);
14435
14436 r = lookup_template_class (arg,
14437 argvec, in_decl,
14438 DECL_CONTEXT (arg),
14439 /*entering_scope=*/0,
14440 complain);
14441 return cp_build_qualified_type_real
14442 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14443 }
14444 else if (code == TEMPLATE_TEMPLATE_PARM)
14445 return arg;
14446 else
14447 /* TEMPLATE_PARM_INDEX. */
14448 return convert_from_reference (unshare_expr (arg));
14449 }
14450
14451 if (level == 1)
14452 /* This can happen during the attempted tsubst'ing in
14453 unify. This means that we don't yet have any information
14454 about the template parameter in question. */
14455 return t;
14456
14457 /* If we get here, we must have been looking at a parm for a
14458 more deeply nested template. Make a new version of this
14459 template parameter, but with a lower level. */
14460 switch (code)
14461 {
14462 case TEMPLATE_TYPE_PARM:
14463 case TEMPLATE_TEMPLATE_PARM:
14464 case BOUND_TEMPLATE_TEMPLATE_PARM:
14465 if (cp_type_quals (t))
14466 {
14467 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14468 r = cp_build_qualified_type_real
14469 (r, cp_type_quals (t),
14470 complain | (code == TEMPLATE_TYPE_PARM
14471 ? tf_ignore_bad_quals : 0));
14472 }
14473 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14474 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14475 && (r = (TEMPLATE_PARM_DESCENDANTS
14476 (TEMPLATE_TYPE_PARM_INDEX (t))))
14477 && (r = TREE_TYPE (r))
14478 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14479 /* Break infinite recursion when substituting the constraints
14480 of a constrained placeholder. */;
14481 else
14482 {
14483 r = copy_type (t);
14484 TEMPLATE_TYPE_PARM_INDEX (r)
14485 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14486 r, levels, args, complain);
14487 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14488 TYPE_MAIN_VARIANT (r) = r;
14489 TYPE_POINTER_TO (r) = NULL_TREE;
14490 TYPE_REFERENCE_TO (r) = NULL_TREE;
14491
14492 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14493 {
14494 /* Propagate constraints on placeholders. */
14495 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14496 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14497 = tsubst_constraint (constr, args, complain, in_decl);
14498 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14499 {
14500 pl = tsubst_copy (pl, args, complain, in_decl);
14501 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14502 }
14503 }
14504
14505 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14506 /* We have reduced the level of the template
14507 template parameter, but not the levels of its
14508 template parameters, so canonical_type_parameter
14509 will not be able to find the canonical template
14510 template parameter for this level. Thus, we
14511 require structural equality checking to compare
14512 TEMPLATE_TEMPLATE_PARMs. */
14513 SET_TYPE_STRUCTURAL_EQUALITY (r);
14514 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14515 SET_TYPE_STRUCTURAL_EQUALITY (r);
14516 else
14517 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14518
14519 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14520 {
14521 tree tinfo = TYPE_TEMPLATE_INFO (t);
14522 /* We might need to substitute into the types of non-type
14523 template parameters. */
14524 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14525 complain, in_decl);
14526 if (tmpl == error_mark_node)
14527 return error_mark_node;
14528 tree argvec = tsubst (TI_ARGS (tinfo), args,
14529 complain, in_decl);
14530 if (argvec == error_mark_node)
14531 return error_mark_node;
14532
14533 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14534 = build_template_info (tmpl, argvec);
14535 }
14536 }
14537 break;
14538
14539 case TEMPLATE_PARM_INDEX:
14540 /* OK, now substitute the type of the non-type parameter. We
14541 couldn't do it earlier because it might be an auto parameter,
14542 and we wouldn't need to if we had an argument. */
14543 type = tsubst (type, args, complain, in_decl);
14544 if (type == error_mark_node)
14545 return error_mark_node;
14546 r = reduce_template_parm_level (t, type, levels, args, complain);
14547 break;
14548
14549 default:
14550 gcc_unreachable ();
14551 }
14552
14553 return r;
14554 }
14555
14556 case TREE_LIST:
14557 {
14558 tree purpose, value, chain;
14559
14560 if (t == void_list_node)
14561 return t;
14562
14563 purpose = TREE_PURPOSE (t);
14564 if (purpose)
14565 {
14566 purpose = tsubst (purpose, args, complain, in_decl);
14567 if (purpose == error_mark_node)
14568 return error_mark_node;
14569 }
14570 value = TREE_VALUE (t);
14571 if (value)
14572 {
14573 value = tsubst (value, args, complain, in_decl);
14574 if (value == error_mark_node)
14575 return error_mark_node;
14576 }
14577 chain = TREE_CHAIN (t);
14578 if (chain && chain != void_type_node)
14579 {
14580 chain = tsubst (chain, args, complain, in_decl);
14581 if (chain == error_mark_node)
14582 return error_mark_node;
14583 }
14584 if (purpose == TREE_PURPOSE (t)
14585 && value == TREE_VALUE (t)
14586 && chain == TREE_CHAIN (t))
14587 return t;
14588 return hash_tree_cons (purpose, value, chain);
14589 }
14590
14591 case TREE_BINFO:
14592 /* We should never be tsubsting a binfo. */
14593 gcc_unreachable ();
14594
14595 case TREE_VEC:
14596 /* A vector of template arguments. */
14597 gcc_assert (!type);
14598 return tsubst_template_args (t, args, complain, in_decl);
14599
14600 case POINTER_TYPE:
14601 case REFERENCE_TYPE:
14602 {
14603 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14604 return t;
14605
14606 /* [temp.deduct]
14607
14608 Type deduction may fail for any of the following
14609 reasons:
14610
14611 -- Attempting to create a pointer to reference type.
14612 -- Attempting to create a reference to a reference type or
14613 a reference to void.
14614
14615 Core issue 106 says that creating a reference to a reference
14616 during instantiation is no longer a cause for failure. We
14617 only enforce this check in strict C++98 mode. */
14618 if ((TYPE_REF_P (type)
14619 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14620 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14621 {
14622 static location_t last_loc;
14623
14624 /* We keep track of the last time we issued this error
14625 message to avoid spewing a ton of messages during a
14626 single bad template instantiation. */
14627 if (complain & tf_error
14628 && last_loc != input_location)
14629 {
14630 if (VOID_TYPE_P (type))
14631 error ("forming reference to void");
14632 else if (code == POINTER_TYPE)
14633 error ("forming pointer to reference type %qT", type);
14634 else
14635 error ("forming reference to reference type %qT", type);
14636 last_loc = input_location;
14637 }
14638
14639 return error_mark_node;
14640 }
14641 else if (TREE_CODE (type) == FUNCTION_TYPE
14642 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14643 || type_memfn_rqual (type) != REF_QUAL_NONE))
14644 {
14645 if (complain & tf_error)
14646 {
14647 if (code == POINTER_TYPE)
14648 error ("forming pointer to qualified function type %qT",
14649 type);
14650 else
14651 error ("forming reference to qualified function type %qT",
14652 type);
14653 }
14654 return error_mark_node;
14655 }
14656 else if (code == POINTER_TYPE)
14657 {
14658 r = build_pointer_type (type);
14659 if (TREE_CODE (type) == METHOD_TYPE)
14660 r = build_ptrmemfunc_type (r);
14661 }
14662 else if (TYPE_REF_P (type))
14663 /* In C++0x, during template argument substitution, when there is an
14664 attempt to create a reference to a reference type, reference
14665 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14666
14667 "If a template-argument for a template-parameter T names a type
14668 that is a reference to a type A, an attempt to create the type
14669 'lvalue reference to cv T' creates the type 'lvalue reference to
14670 A,' while an attempt to create the type type rvalue reference to
14671 cv T' creates the type T"
14672 */
14673 r = cp_build_reference_type
14674 (TREE_TYPE (type),
14675 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14676 else
14677 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14678 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14679
14680 if (r != error_mark_node)
14681 /* Will this ever be needed for TYPE_..._TO values? */
14682 layout_type (r);
14683
14684 return r;
14685 }
14686 case OFFSET_TYPE:
14687 {
14688 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14689 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14690 {
14691 /* [temp.deduct]
14692
14693 Type deduction may fail for any of the following
14694 reasons:
14695
14696 -- Attempting to create "pointer to member of T" when T
14697 is not a class type. */
14698 if (complain & tf_error)
14699 error ("creating pointer to member of non-class type %qT", r);
14700 return error_mark_node;
14701 }
14702 if (TYPE_REF_P (type))
14703 {
14704 if (complain & tf_error)
14705 error ("creating pointer to member reference type %qT", type);
14706 return error_mark_node;
14707 }
14708 if (VOID_TYPE_P (type))
14709 {
14710 if (complain & tf_error)
14711 error ("creating pointer to member of type void");
14712 return error_mark_node;
14713 }
14714 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14715 if (TREE_CODE (type) == FUNCTION_TYPE)
14716 {
14717 /* The type of the implicit object parameter gets its
14718 cv-qualifiers from the FUNCTION_TYPE. */
14719 tree memptr;
14720 tree method_type
14721 = build_memfn_type (type, r, type_memfn_quals (type),
14722 type_memfn_rqual (type));
14723 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14724 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14725 complain);
14726 }
14727 else
14728 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14729 cp_type_quals (t),
14730 complain);
14731 }
14732 case FUNCTION_TYPE:
14733 case METHOD_TYPE:
14734 {
14735 tree fntype;
14736 tree specs;
14737 fntype = tsubst_function_type (t, args, complain, in_decl);
14738 if (fntype == error_mark_node)
14739 return error_mark_node;
14740
14741 /* Substitute the exception specification. */
14742 specs = tsubst_exception_specification (t, args, complain, in_decl,
14743 /*defer_ok*/fndecl_type);
14744 if (specs == error_mark_node)
14745 return error_mark_node;
14746 if (specs)
14747 fntype = build_exception_variant (fntype, specs);
14748 return fntype;
14749 }
14750 case ARRAY_TYPE:
14751 {
14752 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14753 if (domain == error_mark_node)
14754 return error_mark_node;
14755
14756 /* As an optimization, we avoid regenerating the array type if
14757 it will obviously be the same as T. */
14758 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14759 return t;
14760
14761 /* These checks should match the ones in create_array_type_for_decl.
14762
14763 [temp.deduct]
14764
14765 The deduction may fail for any of the following reasons:
14766
14767 -- Attempting to create an array with an element type that
14768 is void, a function type, or a reference type, or [DR337]
14769 an abstract class type. */
14770 if (VOID_TYPE_P (type)
14771 || TREE_CODE (type) == FUNCTION_TYPE
14772 || (TREE_CODE (type) == ARRAY_TYPE
14773 && TYPE_DOMAIN (type) == NULL_TREE)
14774 || TYPE_REF_P (type))
14775 {
14776 if (complain & tf_error)
14777 error ("creating array of %qT", type);
14778 return error_mark_node;
14779 }
14780
14781 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14782 return error_mark_node;
14783
14784 r = build_cplus_array_type (type, domain);
14785
14786 if (!valid_array_size_p (input_location, r, in_decl,
14787 (complain & tf_error)))
14788 return error_mark_node;
14789
14790 if (TYPE_USER_ALIGN (t))
14791 {
14792 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14793 TYPE_USER_ALIGN (r) = 1;
14794 }
14795
14796 return r;
14797 }
14798
14799 case TYPENAME_TYPE:
14800 {
14801 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14802 in_decl, /*entering_scope=*/1);
14803 if (ctx == error_mark_node)
14804 return error_mark_node;
14805
14806 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14807 complain, in_decl);
14808 if (f == error_mark_node)
14809 return error_mark_node;
14810
14811 if (!MAYBE_CLASS_TYPE_P (ctx))
14812 {
14813 if (complain & tf_error)
14814 error ("%qT is not a class, struct, or union type", ctx);
14815 return error_mark_node;
14816 }
14817 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14818 {
14819 /* Normally, make_typename_type does not require that the CTX
14820 have complete type in order to allow things like:
14821
14822 template <class T> struct S { typename S<T>::X Y; };
14823
14824 But, such constructs have already been resolved by this
14825 point, so here CTX really should have complete type, unless
14826 it's a partial instantiation. */
14827 ctx = complete_type (ctx);
14828 if (!COMPLETE_TYPE_P (ctx))
14829 {
14830 if (complain & tf_error)
14831 cxx_incomplete_type_error (NULL_TREE, ctx);
14832 return error_mark_node;
14833 }
14834 }
14835
14836 f = make_typename_type (ctx, f, typename_type,
14837 complain | tf_keep_type_decl);
14838 if (f == error_mark_node)
14839 return f;
14840 if (TREE_CODE (f) == TYPE_DECL)
14841 {
14842 complain |= tf_ignore_bad_quals;
14843 f = TREE_TYPE (f);
14844 }
14845
14846 if (TREE_CODE (f) != TYPENAME_TYPE)
14847 {
14848 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14849 {
14850 if (complain & tf_error)
14851 error ("%qT resolves to %qT, which is not an enumeration type",
14852 t, f);
14853 else
14854 return error_mark_node;
14855 }
14856 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14857 {
14858 if (complain & tf_error)
14859 error ("%qT resolves to %qT, which is is not a class type",
14860 t, f);
14861 else
14862 return error_mark_node;
14863 }
14864 }
14865
14866 return cp_build_qualified_type_real
14867 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14868 }
14869
14870 case UNBOUND_CLASS_TEMPLATE:
14871 {
14872 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14873 in_decl, /*entering_scope=*/1);
14874 tree name = TYPE_IDENTIFIER (t);
14875 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14876
14877 if (ctx == error_mark_node || name == error_mark_node)
14878 return error_mark_node;
14879
14880 if (parm_list)
14881 parm_list = tsubst_template_parms (parm_list, args, complain);
14882 return make_unbound_class_template (ctx, name, parm_list, complain);
14883 }
14884
14885 case TYPEOF_TYPE:
14886 {
14887 tree type;
14888
14889 ++cp_unevaluated_operand;
14890 ++c_inhibit_evaluation_warnings;
14891
14892 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14893 complain, in_decl,
14894 /*integral_constant_expression_p=*/false);
14895
14896 --cp_unevaluated_operand;
14897 --c_inhibit_evaluation_warnings;
14898
14899 type = finish_typeof (type);
14900 return cp_build_qualified_type_real (type,
14901 cp_type_quals (t)
14902 | cp_type_quals (type),
14903 complain);
14904 }
14905
14906 case DECLTYPE_TYPE:
14907 {
14908 tree type;
14909
14910 ++cp_unevaluated_operand;
14911 ++c_inhibit_evaluation_warnings;
14912
14913 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14914 complain|tf_decltype, in_decl,
14915 /*function_p*/false,
14916 /*integral_constant_expression*/false);
14917
14918 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14919 {
14920 if (type == NULL_TREE)
14921 {
14922 if (complain & tf_error)
14923 error ("empty initializer in lambda init-capture");
14924 type = error_mark_node;
14925 }
14926 else if (TREE_CODE (type) == TREE_LIST)
14927 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14928 }
14929
14930 --cp_unevaluated_operand;
14931 --c_inhibit_evaluation_warnings;
14932
14933 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14934 type = lambda_capture_field_type (type,
14935 DECLTYPE_FOR_INIT_CAPTURE (t),
14936 DECLTYPE_FOR_REF_CAPTURE (t));
14937 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14938 type = lambda_proxy_type (type);
14939 else
14940 {
14941 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14942 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14943 && EXPR_P (type))
14944 /* In a template ~id could be either a complement expression
14945 or an unqualified-id naming a destructor; if instantiating
14946 it produces an expression, it's not an id-expression or
14947 member access. */
14948 id = false;
14949 type = finish_decltype_type (type, id, complain);
14950 }
14951 return cp_build_qualified_type_real (type,
14952 cp_type_quals (t)
14953 | cp_type_quals (type),
14954 complain | tf_ignore_bad_quals);
14955 }
14956
14957 case UNDERLYING_TYPE:
14958 {
14959 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14960 complain, in_decl);
14961 return finish_underlying_type (type);
14962 }
14963
14964 case TYPE_ARGUMENT_PACK:
14965 case NONTYPE_ARGUMENT_PACK:
14966 {
14967 tree r;
14968
14969 if (code == NONTYPE_ARGUMENT_PACK)
14970 r = make_node (code);
14971 else
14972 r = cxx_make_type (code);
14973
14974 tree pack_args = ARGUMENT_PACK_ARGS (t);
14975 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14976 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14977
14978 return r;
14979 }
14980
14981 case VOID_CST:
14982 case INTEGER_CST:
14983 case REAL_CST:
14984 case STRING_CST:
14985 case PLUS_EXPR:
14986 case MINUS_EXPR:
14987 case NEGATE_EXPR:
14988 case NOP_EXPR:
14989 case INDIRECT_REF:
14990 case ADDR_EXPR:
14991 case CALL_EXPR:
14992 case ARRAY_REF:
14993 case SCOPE_REF:
14994 /* We should use one of the expression tsubsts for these codes. */
14995 gcc_unreachable ();
14996
14997 default:
14998 sorry ("use of %qs in template", get_tree_code_name (code));
14999 return error_mark_node;
15000 }
15001 }
15002
15003 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15004 expression on the left-hand side of the "." or "->" operator. We
15005 only do the lookup if we had a dependent BASELINK. Otherwise we
15006 adjust it onto the instantiated heirarchy. */
15007
15008 static tree
15009 tsubst_baselink (tree baselink, tree object_type,
15010 tree args, tsubst_flags_t complain, tree in_decl)
15011 {
15012 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15013 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15014 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15015
15016 tree optype = BASELINK_OPTYPE (baselink);
15017 optype = tsubst (optype, args, complain, in_decl);
15018
15019 tree template_args = NULL_TREE;
15020 bool template_id_p = false;
15021 tree fns = BASELINK_FUNCTIONS (baselink);
15022 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15023 {
15024 template_id_p = true;
15025 template_args = TREE_OPERAND (fns, 1);
15026 fns = TREE_OPERAND (fns, 0);
15027 if (template_args)
15028 template_args = tsubst_template_args (template_args, args,
15029 complain, in_decl);
15030 }
15031
15032 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15033 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15034 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15035
15036 if (dependent_p)
15037 {
15038 tree name = OVL_NAME (fns);
15039 if (IDENTIFIER_CONV_OP_P (name))
15040 name = make_conv_op_name (optype);
15041
15042 if (name == complete_dtor_identifier)
15043 /* Treat as-if non-dependent below. */
15044 dependent_p = false;
15045
15046 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15047 if (!baselink)
15048 {
15049 if ((complain & tf_error)
15050 && constructor_name_p (name, qualifying_scope))
15051 error ("cannot call constructor %<%T::%D%> directly",
15052 qualifying_scope, name);
15053 return error_mark_node;
15054 }
15055
15056 if (BASELINK_P (baselink))
15057 fns = BASELINK_FUNCTIONS (baselink);
15058 }
15059 else
15060 /* We're going to overwrite pieces below, make a duplicate. */
15061 baselink = copy_node (baselink);
15062
15063 /* If lookup found a single function, mark it as used at this point.
15064 (If lookup found multiple functions the one selected later by
15065 overload resolution will be marked as used at that point.) */
15066 if (!template_id_p && !really_overloaded_fn (fns))
15067 {
15068 tree fn = OVL_FIRST (fns);
15069 bool ok = mark_used (fn, complain);
15070 if (!ok && !(complain & tf_error))
15071 return error_mark_node;
15072 if (ok && BASELINK_P (baselink))
15073 /* We might have instantiated an auto function. */
15074 TREE_TYPE (baselink) = TREE_TYPE (fn);
15075 }
15076
15077 if (BASELINK_P (baselink))
15078 {
15079 /* Add back the template arguments, if present. */
15080 if (template_id_p)
15081 BASELINK_FUNCTIONS (baselink)
15082 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15083
15084 /* Update the conversion operator type. */
15085 BASELINK_OPTYPE (baselink) = optype;
15086 }
15087
15088 if (!object_type)
15089 object_type = current_class_type;
15090
15091 if (qualified_p || !dependent_p)
15092 {
15093 baselink = adjust_result_of_qualified_name_lookup (baselink,
15094 qualifying_scope,
15095 object_type);
15096 if (!qualified_p)
15097 /* We need to call adjust_result_of_qualified_name_lookup in case the
15098 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15099 so that we still get virtual function binding. */
15100 BASELINK_QUALIFIED_P (baselink) = false;
15101 }
15102
15103 return baselink;
15104 }
15105
15106 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15107 true if the qualified-id will be a postfix-expression in-and-of
15108 itself; false if more of the postfix-expression follows the
15109 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15110 of "&". */
15111
15112 static tree
15113 tsubst_qualified_id (tree qualified_id, tree args,
15114 tsubst_flags_t complain, tree in_decl,
15115 bool done, bool address_p)
15116 {
15117 tree expr;
15118 tree scope;
15119 tree name;
15120 bool is_template;
15121 tree template_args;
15122 location_t loc = UNKNOWN_LOCATION;
15123
15124 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15125
15126 /* Figure out what name to look up. */
15127 name = TREE_OPERAND (qualified_id, 1);
15128 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15129 {
15130 is_template = true;
15131 loc = EXPR_LOCATION (name);
15132 template_args = TREE_OPERAND (name, 1);
15133 if (template_args)
15134 template_args = tsubst_template_args (template_args, args,
15135 complain, in_decl);
15136 if (template_args == error_mark_node)
15137 return error_mark_node;
15138 name = TREE_OPERAND (name, 0);
15139 }
15140 else
15141 {
15142 is_template = false;
15143 template_args = NULL_TREE;
15144 }
15145
15146 /* Substitute into the qualifying scope. When there are no ARGS, we
15147 are just trying to simplify a non-dependent expression. In that
15148 case the qualifying scope may be dependent, and, in any case,
15149 substituting will not help. */
15150 scope = TREE_OPERAND (qualified_id, 0);
15151 if (args)
15152 {
15153 scope = tsubst (scope, args, complain, in_decl);
15154 expr = tsubst_copy (name, args, complain, in_decl);
15155 }
15156 else
15157 expr = name;
15158
15159 if (dependent_scope_p (scope))
15160 {
15161 if (is_template)
15162 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15163 tree r = build_qualified_name (NULL_TREE, scope, expr,
15164 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15165 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15166 return r;
15167 }
15168
15169 if (!BASELINK_P (name) && !DECL_P (expr))
15170 {
15171 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15172 {
15173 /* A BIT_NOT_EXPR is used to represent a destructor. */
15174 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15175 {
15176 error ("qualifying type %qT does not match destructor name ~%qT",
15177 scope, TREE_OPERAND (expr, 0));
15178 expr = error_mark_node;
15179 }
15180 else
15181 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15182 /*is_type_p=*/0, false);
15183 }
15184 else
15185 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15186 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15187 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15188 {
15189 if (complain & tf_error)
15190 {
15191 error ("dependent-name %qE is parsed as a non-type, but "
15192 "instantiation yields a type", qualified_id);
15193 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15194 }
15195 return error_mark_node;
15196 }
15197 }
15198
15199 if (DECL_P (expr))
15200 {
15201 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15202 scope);
15203 /* Remember that there was a reference to this entity. */
15204 if (!mark_used (expr, complain) && !(complain & tf_error))
15205 return error_mark_node;
15206 }
15207
15208 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15209 {
15210 if (complain & tf_error)
15211 qualified_name_lookup_error (scope,
15212 TREE_OPERAND (qualified_id, 1),
15213 expr, input_location);
15214 return error_mark_node;
15215 }
15216
15217 if (is_template)
15218 {
15219 /* We may be repeating a check already done during parsing, but
15220 if it was well-formed and passed then, it will pass again
15221 now, and if it didn't, we wouldn't have got here. The case
15222 we want to catch is when we couldn't tell then, and can now,
15223 namely when templ prior to substitution was an
15224 identifier. */
15225 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15226 return error_mark_node;
15227
15228 if (variable_template_p (expr))
15229 expr = lookup_and_finish_template_variable (expr, template_args,
15230 complain);
15231 else
15232 expr = lookup_template_function (expr, template_args);
15233 }
15234
15235 if (expr == error_mark_node && complain & tf_error)
15236 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15237 expr, input_location);
15238 else if (TYPE_P (scope))
15239 {
15240 expr = (adjust_result_of_qualified_name_lookup
15241 (expr, scope, current_nonlambda_class_type ()));
15242 expr = (finish_qualified_id_expr
15243 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15244 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15245 /*template_arg_p=*/false, complain));
15246 }
15247
15248 /* Expressions do not generally have reference type. */
15249 if (TREE_CODE (expr) != SCOPE_REF
15250 /* However, if we're about to form a pointer-to-member, we just
15251 want the referenced member referenced. */
15252 && TREE_CODE (expr) != OFFSET_REF)
15253 expr = convert_from_reference (expr);
15254
15255 if (REF_PARENTHESIZED_P (qualified_id))
15256 expr = force_paren_expr (expr);
15257
15258 return expr;
15259 }
15260
15261 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15262 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15263 for tsubst. */
15264
15265 static tree
15266 tsubst_init (tree init, tree decl, tree args,
15267 tsubst_flags_t complain, tree in_decl)
15268 {
15269 if (!init)
15270 return NULL_TREE;
15271
15272 init = tsubst_expr (init, args, complain, in_decl, false);
15273
15274 if (!init && TREE_TYPE (decl) != error_mark_node)
15275 {
15276 /* If we had an initializer but it
15277 instantiated to nothing,
15278 value-initialize the object. This will
15279 only occur when the initializer was a
15280 pack expansion where the parameter packs
15281 used in that expansion were of length
15282 zero. */
15283 init = build_value_init (TREE_TYPE (decl),
15284 complain);
15285 if (TREE_CODE (init) == AGGR_INIT_EXPR)
15286 init = get_target_expr_sfinae (init, complain);
15287 if (TREE_CODE (init) == TARGET_EXPR)
15288 TARGET_EXPR_DIRECT_INIT_P (init) = true;
15289 }
15290
15291 return init;
15292 }
15293
15294 /* Like tsubst, but deals with expressions. This function just replaces
15295 template parms; to finish processing the resultant expression, use
15296 tsubst_copy_and_build or tsubst_expr. */
15297
15298 static tree
15299 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15300 {
15301 enum tree_code code;
15302 tree r;
15303
15304 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
15305 return t;
15306
15307 code = TREE_CODE (t);
15308
15309 switch (code)
15310 {
15311 case PARM_DECL:
15312 r = retrieve_local_specialization (t);
15313
15314 if (r == NULL_TREE)
15315 {
15316 /* We get here for a use of 'this' in an NSDMI. */
15317 if (DECL_NAME (t) == this_identifier && current_class_ptr)
15318 return current_class_ptr;
15319
15320 /* This can happen for a parameter name used later in a function
15321 declaration (such as in a late-specified return type). Just
15322 make a dummy decl, since it's only used for its type. */
15323 gcc_assert (cp_unevaluated_operand != 0);
15324 r = tsubst_decl (t, args, complain);
15325 /* Give it the template pattern as its context; its true context
15326 hasn't been instantiated yet and this is good enough for
15327 mangling. */
15328 DECL_CONTEXT (r) = DECL_CONTEXT (t);
15329 }
15330
15331 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15332 r = argument_pack_select_arg (r);
15333 if (!mark_used (r, complain) && !(complain & tf_error))
15334 return error_mark_node;
15335 return r;
15336
15337 case CONST_DECL:
15338 {
15339 tree enum_type;
15340 tree v;
15341
15342 if (DECL_TEMPLATE_PARM_P (t))
15343 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
15344 /* There is no need to substitute into namespace-scope
15345 enumerators. */
15346 if (DECL_NAMESPACE_SCOPE_P (t))
15347 return t;
15348 /* If ARGS is NULL, then T is known to be non-dependent. */
15349 if (args == NULL_TREE)
15350 return scalar_constant_value (t);
15351
15352 /* Unfortunately, we cannot just call lookup_name here.
15353 Consider:
15354
15355 template <int I> int f() {
15356 enum E { a = I };
15357 struct S { void g() { E e = a; } };
15358 };
15359
15360 When we instantiate f<7>::S::g(), say, lookup_name is not
15361 clever enough to find f<7>::a. */
15362 enum_type
15363 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15364 /*entering_scope=*/0);
15365
15366 for (v = TYPE_VALUES (enum_type);
15367 v != NULL_TREE;
15368 v = TREE_CHAIN (v))
15369 if (TREE_PURPOSE (v) == DECL_NAME (t))
15370 return TREE_VALUE (v);
15371
15372 /* We didn't find the name. That should never happen; if
15373 name-lookup found it during preliminary parsing, we
15374 should find it again here during instantiation. */
15375 gcc_unreachable ();
15376 }
15377 return t;
15378
15379 case FIELD_DECL:
15380 if (DECL_CONTEXT (t))
15381 {
15382 tree ctx;
15383
15384 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15385 /*entering_scope=*/1);
15386 if (ctx != DECL_CONTEXT (t))
15387 {
15388 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15389 if (!r)
15390 {
15391 if (complain & tf_error)
15392 error ("using invalid field %qD", t);
15393 return error_mark_node;
15394 }
15395 return r;
15396 }
15397 }
15398
15399 return t;
15400
15401 case VAR_DECL:
15402 case FUNCTION_DECL:
15403 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15404 r = tsubst (t, args, complain, in_decl);
15405 else if (local_variable_p (t)
15406 && uses_template_parms (DECL_CONTEXT (t)))
15407 {
15408 r = retrieve_local_specialization (t);
15409 if (r == NULL_TREE)
15410 {
15411 /* First try name lookup to find the instantiation. */
15412 r = lookup_name (DECL_NAME (t));
15413 if (r && !is_capture_proxy (r))
15414 {
15415 /* Make sure that the one we found is the one we want. */
15416 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15417 if (ctx != DECL_CONTEXT (r))
15418 r = NULL_TREE;
15419 }
15420
15421 if (r)
15422 /* OK */;
15423 else
15424 {
15425 /* This can happen for a variable used in a
15426 late-specified return type of a local lambda, or for a
15427 local static or constant. Building a new VAR_DECL
15428 should be OK in all those cases. */
15429 r = tsubst_decl (t, args, complain);
15430 if (local_specializations)
15431 /* Avoid infinite recursion (79640). */
15432 register_local_specialization (r, t);
15433 if (decl_maybe_constant_var_p (r))
15434 {
15435 /* We can't call cp_finish_decl, so handle the
15436 initializer by hand. */
15437 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15438 complain, in_decl);
15439 if (!processing_template_decl)
15440 init = maybe_constant_init (init);
15441 if (processing_template_decl
15442 ? potential_constant_expression (init)
15443 : reduced_constant_expression_p (init))
15444 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15445 = TREE_CONSTANT (r) = true;
15446 DECL_INITIAL (r) = init;
15447 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15448 TREE_TYPE (r)
15449 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15450 complain, adc_variable_type);
15451 }
15452 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15453 || decl_constant_var_p (r)
15454 || errorcount || sorrycount);
15455 if (!processing_template_decl
15456 && !TREE_STATIC (r))
15457 r = process_outer_var_ref (r, complain);
15458 }
15459 /* Remember this for subsequent uses. */
15460 if (local_specializations)
15461 register_local_specialization (r, t);
15462 }
15463 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15464 r = argument_pack_select_arg (r);
15465 }
15466 else
15467 r = t;
15468 if (!mark_used (r, complain))
15469 return error_mark_node;
15470 return r;
15471
15472 case NAMESPACE_DECL:
15473 return t;
15474
15475 case OVERLOAD:
15476 /* An OVERLOAD will always be a non-dependent overload set; an
15477 overload set from function scope will just be represented with an
15478 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15479 gcc_assert (!uses_template_parms (t));
15480 /* We must have marked any lookups as persistent. */
15481 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
15482 return t;
15483
15484 case BASELINK:
15485 return tsubst_baselink (t, current_nonlambda_class_type (),
15486 args, complain, in_decl);
15487
15488 case TEMPLATE_DECL:
15489 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15490 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15491 args, complain, in_decl);
15492 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15493 return tsubst (t, args, complain, in_decl);
15494 else if (DECL_CLASS_SCOPE_P (t)
15495 && uses_template_parms (DECL_CONTEXT (t)))
15496 {
15497 /* Template template argument like the following example need
15498 special treatment:
15499
15500 template <template <class> class TT> struct C {};
15501 template <class T> struct D {
15502 template <class U> struct E {};
15503 C<E> c; // #1
15504 };
15505 D<int> d; // #2
15506
15507 We are processing the template argument `E' in #1 for
15508 the template instantiation #2. Originally, `E' is a
15509 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15510 have to substitute this with one having context `D<int>'. */
15511
15512 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15513 if (dependent_scope_p (context))
15514 {
15515 /* When rewriting a constructor into a deduction guide, a
15516 non-dependent name can become dependent, so memtmpl<args>
15517 becomes context::template memtmpl<args>. */
15518 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15519 return build_qualified_name (type, context, DECL_NAME (t),
15520 /*template*/true);
15521 }
15522 return lookup_field (context, DECL_NAME(t), 0, false);
15523 }
15524 else
15525 /* Ordinary template template argument. */
15526 return t;
15527
15528 case NON_LVALUE_EXPR:
15529 case VIEW_CONVERT_EXPR:
15530 {
15531 /* Handle location wrappers by substituting the wrapped node
15532 first, *then* reusing the resulting type. Doing the type
15533 first ensures that we handle template parameters and
15534 parameter pack expansions. */
15535 gcc_assert (location_wrapper_p (t));
15536 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15537 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15538 }
15539
15540 case CAST_EXPR:
15541 case REINTERPRET_CAST_EXPR:
15542 case CONST_CAST_EXPR:
15543 case STATIC_CAST_EXPR:
15544 case DYNAMIC_CAST_EXPR:
15545 case IMPLICIT_CONV_EXPR:
15546 case CONVERT_EXPR:
15547 case NOP_EXPR:
15548 {
15549 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15550 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15551 return build1 (code, type, op0);
15552 }
15553
15554 case SIZEOF_EXPR:
15555 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15556 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15557 {
15558 tree expanded, op = TREE_OPERAND (t, 0);
15559 int len = 0;
15560
15561 if (SIZEOF_EXPR_TYPE_P (t))
15562 op = TREE_TYPE (op);
15563
15564 ++cp_unevaluated_operand;
15565 ++c_inhibit_evaluation_warnings;
15566 /* We only want to compute the number of arguments. */
15567 if (PACK_EXPANSION_P (op))
15568 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15569 else
15570 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15571 args, complain, in_decl);
15572 --cp_unevaluated_operand;
15573 --c_inhibit_evaluation_warnings;
15574
15575 if (TREE_CODE (expanded) == TREE_VEC)
15576 {
15577 len = TREE_VEC_LENGTH (expanded);
15578 /* Set TREE_USED for the benefit of -Wunused. */
15579 for (int i = 0; i < len; i++)
15580 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15581 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15582 }
15583
15584 if (expanded == error_mark_node)
15585 return error_mark_node;
15586 else if (PACK_EXPANSION_P (expanded)
15587 || (TREE_CODE (expanded) == TREE_VEC
15588 && pack_expansion_args_count (expanded)))
15589
15590 {
15591 if (PACK_EXPANSION_P (expanded))
15592 /* OK. */;
15593 else if (TREE_VEC_LENGTH (expanded) == 1)
15594 expanded = TREE_VEC_ELT (expanded, 0);
15595 else
15596 expanded = make_argument_pack (expanded);
15597
15598 if (TYPE_P (expanded))
15599 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15600 false,
15601 complain & tf_error);
15602 else
15603 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15604 complain & tf_error);
15605 }
15606 else
15607 return build_int_cst (size_type_node, len);
15608 }
15609 if (SIZEOF_EXPR_TYPE_P (t))
15610 {
15611 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15612 args, complain, in_decl);
15613 r = build1 (NOP_EXPR, r, error_mark_node);
15614 r = build1 (SIZEOF_EXPR,
15615 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15616 SIZEOF_EXPR_TYPE_P (r) = 1;
15617 return r;
15618 }
15619 /* Fall through */
15620
15621 case INDIRECT_REF:
15622 case NEGATE_EXPR:
15623 case TRUTH_NOT_EXPR:
15624 case BIT_NOT_EXPR:
15625 case ADDR_EXPR:
15626 case UNARY_PLUS_EXPR: /* Unary + */
15627 case ALIGNOF_EXPR:
15628 case AT_ENCODE_EXPR:
15629 case ARROW_EXPR:
15630 case THROW_EXPR:
15631 case TYPEID_EXPR:
15632 case REALPART_EXPR:
15633 case IMAGPART_EXPR:
15634 case PAREN_EXPR:
15635 {
15636 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15637 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15638 r = build1 (code, type, op0);
15639 if (code == ALIGNOF_EXPR)
15640 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
15641 return r;
15642 }
15643
15644 case COMPONENT_REF:
15645 {
15646 tree object;
15647 tree name;
15648
15649 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15650 name = TREE_OPERAND (t, 1);
15651 if (TREE_CODE (name) == BIT_NOT_EXPR)
15652 {
15653 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15654 complain, in_decl);
15655 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15656 }
15657 else if (TREE_CODE (name) == SCOPE_REF
15658 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15659 {
15660 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15661 complain, in_decl);
15662 name = TREE_OPERAND (name, 1);
15663 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15664 complain, in_decl);
15665 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15666 name = build_qualified_name (/*type=*/NULL_TREE,
15667 base, name,
15668 /*template_p=*/false);
15669 }
15670 else if (BASELINK_P (name))
15671 name = tsubst_baselink (name,
15672 non_reference (TREE_TYPE (object)),
15673 args, complain,
15674 in_decl);
15675 else
15676 name = tsubst_copy (name, args, complain, in_decl);
15677 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15678 }
15679
15680 case PLUS_EXPR:
15681 case MINUS_EXPR:
15682 case MULT_EXPR:
15683 case TRUNC_DIV_EXPR:
15684 case CEIL_DIV_EXPR:
15685 case FLOOR_DIV_EXPR:
15686 case ROUND_DIV_EXPR:
15687 case EXACT_DIV_EXPR:
15688 case BIT_AND_EXPR:
15689 case BIT_IOR_EXPR:
15690 case BIT_XOR_EXPR:
15691 case TRUNC_MOD_EXPR:
15692 case FLOOR_MOD_EXPR:
15693 case TRUTH_ANDIF_EXPR:
15694 case TRUTH_ORIF_EXPR:
15695 case TRUTH_AND_EXPR:
15696 case TRUTH_OR_EXPR:
15697 case RSHIFT_EXPR:
15698 case LSHIFT_EXPR:
15699 case RROTATE_EXPR:
15700 case LROTATE_EXPR:
15701 case EQ_EXPR:
15702 case NE_EXPR:
15703 case MAX_EXPR:
15704 case MIN_EXPR:
15705 case LE_EXPR:
15706 case GE_EXPR:
15707 case LT_EXPR:
15708 case GT_EXPR:
15709 case COMPOUND_EXPR:
15710 case DOTSTAR_EXPR:
15711 case MEMBER_REF:
15712 case PREDECREMENT_EXPR:
15713 case PREINCREMENT_EXPR:
15714 case POSTDECREMENT_EXPR:
15715 case POSTINCREMENT_EXPR:
15716 {
15717 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15718 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15719 return build_nt (code, op0, op1);
15720 }
15721
15722 case SCOPE_REF:
15723 {
15724 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15725 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15726 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15727 QUALIFIED_NAME_IS_TEMPLATE (t));
15728 }
15729
15730 case ARRAY_REF:
15731 {
15732 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15733 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15734 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15735 }
15736
15737 case CALL_EXPR:
15738 {
15739 int n = VL_EXP_OPERAND_LENGTH (t);
15740 tree result = build_vl_exp (CALL_EXPR, n);
15741 int i;
15742 for (i = 0; i < n; i++)
15743 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15744 complain, in_decl);
15745 return result;
15746 }
15747
15748 case COND_EXPR:
15749 case MODOP_EXPR:
15750 case PSEUDO_DTOR_EXPR:
15751 case VEC_PERM_EXPR:
15752 {
15753 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15754 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15755 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15756 r = build_nt (code, op0, op1, op2);
15757 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15758 return r;
15759 }
15760
15761 case NEW_EXPR:
15762 {
15763 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15764 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15765 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15766 r = build_nt (code, op0, op1, op2);
15767 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15768 return r;
15769 }
15770
15771 case DELETE_EXPR:
15772 {
15773 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15774 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15775 r = build_nt (code, op0, op1);
15776 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15777 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15778 return r;
15779 }
15780
15781 case TEMPLATE_ID_EXPR:
15782 {
15783 /* Substituted template arguments */
15784 tree fn = TREE_OPERAND (t, 0);
15785 tree targs = TREE_OPERAND (t, 1);
15786
15787 fn = tsubst_copy (fn, args, complain, in_decl);
15788 if (targs)
15789 targs = tsubst_template_args (targs, args, complain, in_decl);
15790
15791 return lookup_template_function (fn, targs);
15792 }
15793
15794 case TREE_LIST:
15795 {
15796 tree purpose, value, chain;
15797
15798 if (t == void_list_node)
15799 return t;
15800
15801 purpose = TREE_PURPOSE (t);
15802 if (purpose)
15803 purpose = tsubst_copy (purpose, args, complain, in_decl);
15804 value = TREE_VALUE (t);
15805 if (value)
15806 value = tsubst_copy (value, args, complain, in_decl);
15807 chain = TREE_CHAIN (t);
15808 if (chain && chain != void_type_node)
15809 chain = tsubst_copy (chain, args, complain, in_decl);
15810 if (purpose == TREE_PURPOSE (t)
15811 && value == TREE_VALUE (t)
15812 && chain == TREE_CHAIN (t))
15813 return t;
15814 return tree_cons (purpose, value, chain);
15815 }
15816
15817 case RECORD_TYPE:
15818 case UNION_TYPE:
15819 case ENUMERAL_TYPE:
15820 case INTEGER_TYPE:
15821 case TEMPLATE_TYPE_PARM:
15822 case TEMPLATE_TEMPLATE_PARM:
15823 case BOUND_TEMPLATE_TEMPLATE_PARM:
15824 case TEMPLATE_PARM_INDEX:
15825 case POINTER_TYPE:
15826 case REFERENCE_TYPE:
15827 case OFFSET_TYPE:
15828 case FUNCTION_TYPE:
15829 case METHOD_TYPE:
15830 case ARRAY_TYPE:
15831 case TYPENAME_TYPE:
15832 case UNBOUND_CLASS_TEMPLATE:
15833 case TYPEOF_TYPE:
15834 case DECLTYPE_TYPE:
15835 case TYPE_DECL:
15836 return tsubst (t, args, complain, in_decl);
15837
15838 case USING_DECL:
15839 t = DECL_NAME (t);
15840 /* Fall through. */
15841 case IDENTIFIER_NODE:
15842 if (IDENTIFIER_CONV_OP_P (t))
15843 {
15844 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15845 return make_conv_op_name (new_type);
15846 }
15847 else
15848 return t;
15849
15850 case CONSTRUCTOR:
15851 /* This is handled by tsubst_copy_and_build. */
15852 gcc_unreachable ();
15853
15854 case VA_ARG_EXPR:
15855 {
15856 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15857 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15858 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15859 }
15860
15861 case CLEANUP_POINT_EXPR:
15862 /* We shouldn't have built any of these during initial template
15863 generation. Instead, they should be built during instantiation
15864 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15865 gcc_unreachable ();
15866
15867 case OFFSET_REF:
15868 {
15869 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15870 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15871 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15872 r = build2 (code, type, op0, op1);
15873 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15874 if (!mark_used (TREE_OPERAND (r, 1), complain)
15875 && !(complain & tf_error))
15876 return error_mark_node;
15877 return r;
15878 }
15879
15880 case EXPR_PACK_EXPANSION:
15881 error ("invalid use of pack expansion expression");
15882 return error_mark_node;
15883
15884 case NONTYPE_ARGUMENT_PACK:
15885 error ("use %<...%> to expand argument pack");
15886 return error_mark_node;
15887
15888 case VOID_CST:
15889 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15890 return t;
15891
15892 case INTEGER_CST:
15893 case REAL_CST:
15894 case STRING_CST:
15895 case COMPLEX_CST:
15896 {
15897 /* Instantiate any typedefs in the type. */
15898 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15899 r = fold_convert (type, t);
15900 gcc_assert (TREE_CODE (r) == code);
15901 return r;
15902 }
15903
15904 case PTRMEM_CST:
15905 /* These can sometimes show up in a partial instantiation, but never
15906 involve template parms. */
15907 gcc_assert (!uses_template_parms (t));
15908 return t;
15909
15910 case UNARY_LEFT_FOLD_EXPR:
15911 return tsubst_unary_left_fold (t, args, complain, in_decl);
15912 case UNARY_RIGHT_FOLD_EXPR:
15913 return tsubst_unary_right_fold (t, args, complain, in_decl);
15914 case BINARY_LEFT_FOLD_EXPR:
15915 return tsubst_binary_left_fold (t, args, complain, in_decl);
15916 case BINARY_RIGHT_FOLD_EXPR:
15917 return tsubst_binary_right_fold (t, args, complain, in_decl);
15918 case PREDICT_EXPR:
15919 return t;
15920
15921 case DEBUG_BEGIN_STMT:
15922 /* ??? There's no point in copying it for now, but maybe some
15923 day it will contain more information, such as a pointer back
15924 to the containing function, inlined copy or so. */
15925 return t;
15926
15927 default:
15928 /* We shouldn't get here, but keep going if !flag_checking. */
15929 if (flag_checking)
15930 gcc_unreachable ();
15931 return t;
15932 }
15933 }
15934
15935 /* Helper function for tsubst_omp_clauses, used for instantiation of
15936 OMP_CLAUSE_DECL of clauses. */
15937
15938 static tree
15939 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15940 tree in_decl)
15941 {
15942 if (decl == NULL_TREE)
15943 return NULL_TREE;
15944
15945 /* Handle an OpenMP array section represented as a TREE_LIST (or
15946 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15947 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15948 TREE_LIST. We can handle it exactly the same as an array section
15949 (purpose, value, and a chain), even though the nomenclature
15950 (low_bound, length, etc) is different. */
15951 if (TREE_CODE (decl) == TREE_LIST)
15952 {
15953 tree low_bound
15954 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15955 /*integral_constant_expression_p=*/false);
15956 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15957 /*integral_constant_expression_p=*/false);
15958 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15959 in_decl);
15960 if (TREE_PURPOSE (decl) == low_bound
15961 && TREE_VALUE (decl) == length
15962 && TREE_CHAIN (decl) == chain)
15963 return decl;
15964 tree ret = tree_cons (low_bound, length, chain);
15965 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15966 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15967 return ret;
15968 }
15969 tree ret = tsubst_expr (decl, args, complain, in_decl,
15970 /*integral_constant_expression_p=*/false);
15971 /* Undo convert_from_reference tsubst_expr could have called. */
15972 if (decl
15973 && REFERENCE_REF_P (ret)
15974 && !REFERENCE_REF_P (decl))
15975 ret = TREE_OPERAND (ret, 0);
15976 return ret;
15977 }
15978
15979 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15980
15981 static tree
15982 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15983 tree args, tsubst_flags_t complain, tree in_decl)
15984 {
15985 tree new_clauses = NULL_TREE, nc, oc;
15986 tree linear_no_step = NULL_TREE;
15987
15988 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15989 {
15990 nc = copy_node (oc);
15991 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15992 new_clauses = nc;
15993
15994 switch (OMP_CLAUSE_CODE (nc))
15995 {
15996 case OMP_CLAUSE_LASTPRIVATE:
15997 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15998 {
15999 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16000 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16001 in_decl, /*integral_constant_expression_p=*/false);
16002 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16003 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16004 }
16005 /* FALLTHRU */
16006 case OMP_CLAUSE_PRIVATE:
16007 case OMP_CLAUSE_SHARED:
16008 case OMP_CLAUSE_FIRSTPRIVATE:
16009 case OMP_CLAUSE_COPYIN:
16010 case OMP_CLAUSE_COPYPRIVATE:
16011 case OMP_CLAUSE_UNIFORM:
16012 case OMP_CLAUSE_DEPEND:
16013 case OMP_CLAUSE_FROM:
16014 case OMP_CLAUSE_TO:
16015 case OMP_CLAUSE_MAP:
16016 case OMP_CLAUSE_USE_DEVICE_PTR:
16017 case OMP_CLAUSE_IS_DEVICE_PTR:
16018 OMP_CLAUSE_DECL (nc)
16019 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16020 in_decl);
16021 break;
16022 case OMP_CLAUSE_TILE:
16023 case OMP_CLAUSE_IF:
16024 case OMP_CLAUSE_NUM_THREADS:
16025 case OMP_CLAUSE_SCHEDULE:
16026 case OMP_CLAUSE_COLLAPSE:
16027 case OMP_CLAUSE_FINAL:
16028 case OMP_CLAUSE_DEVICE:
16029 case OMP_CLAUSE_DIST_SCHEDULE:
16030 case OMP_CLAUSE_NUM_TEAMS:
16031 case OMP_CLAUSE_THREAD_LIMIT:
16032 case OMP_CLAUSE_SAFELEN:
16033 case OMP_CLAUSE_SIMDLEN:
16034 case OMP_CLAUSE_NUM_TASKS:
16035 case OMP_CLAUSE_GRAINSIZE:
16036 case OMP_CLAUSE_PRIORITY:
16037 case OMP_CLAUSE_ORDERED:
16038 case OMP_CLAUSE_HINT:
16039 case OMP_CLAUSE_NUM_GANGS:
16040 case OMP_CLAUSE_NUM_WORKERS:
16041 case OMP_CLAUSE_VECTOR_LENGTH:
16042 case OMP_CLAUSE_WORKER:
16043 case OMP_CLAUSE_VECTOR:
16044 case OMP_CLAUSE_ASYNC:
16045 case OMP_CLAUSE_WAIT:
16046 OMP_CLAUSE_OPERAND (nc, 0)
16047 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16048 in_decl, /*integral_constant_expression_p=*/false);
16049 break;
16050 case OMP_CLAUSE_REDUCTION:
16051 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16052 {
16053 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16054 if (TREE_CODE (placeholder) == SCOPE_REF)
16055 {
16056 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16057 complain, in_decl);
16058 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16059 = build_qualified_name (NULL_TREE, scope,
16060 TREE_OPERAND (placeholder, 1),
16061 false);
16062 }
16063 else
16064 gcc_assert (identifier_p (placeholder));
16065 }
16066 OMP_CLAUSE_DECL (nc)
16067 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16068 in_decl);
16069 break;
16070 case OMP_CLAUSE_GANG:
16071 case OMP_CLAUSE_ALIGNED:
16072 OMP_CLAUSE_DECL (nc)
16073 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16074 in_decl);
16075 OMP_CLAUSE_OPERAND (nc, 1)
16076 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16077 in_decl, /*integral_constant_expression_p=*/false);
16078 break;
16079 case OMP_CLAUSE_LINEAR:
16080 OMP_CLAUSE_DECL (nc)
16081 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16082 in_decl);
16083 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16084 {
16085 gcc_assert (!linear_no_step);
16086 linear_no_step = nc;
16087 }
16088 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16089 OMP_CLAUSE_LINEAR_STEP (nc)
16090 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16091 complain, in_decl);
16092 else
16093 OMP_CLAUSE_LINEAR_STEP (nc)
16094 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16095 in_decl,
16096 /*integral_constant_expression_p=*/false);
16097 break;
16098 case OMP_CLAUSE_NOWAIT:
16099 case OMP_CLAUSE_DEFAULT:
16100 case OMP_CLAUSE_UNTIED:
16101 case OMP_CLAUSE_MERGEABLE:
16102 case OMP_CLAUSE_INBRANCH:
16103 case OMP_CLAUSE_NOTINBRANCH:
16104 case OMP_CLAUSE_PROC_BIND:
16105 case OMP_CLAUSE_FOR:
16106 case OMP_CLAUSE_PARALLEL:
16107 case OMP_CLAUSE_SECTIONS:
16108 case OMP_CLAUSE_TASKGROUP:
16109 case OMP_CLAUSE_NOGROUP:
16110 case OMP_CLAUSE_THREADS:
16111 case OMP_CLAUSE_SIMD:
16112 case OMP_CLAUSE_DEFAULTMAP:
16113 case OMP_CLAUSE_INDEPENDENT:
16114 case OMP_CLAUSE_AUTO:
16115 case OMP_CLAUSE_SEQ:
16116 break;
16117 default:
16118 gcc_unreachable ();
16119 }
16120 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16121 switch (OMP_CLAUSE_CODE (nc))
16122 {
16123 case OMP_CLAUSE_SHARED:
16124 case OMP_CLAUSE_PRIVATE:
16125 case OMP_CLAUSE_FIRSTPRIVATE:
16126 case OMP_CLAUSE_LASTPRIVATE:
16127 case OMP_CLAUSE_COPYPRIVATE:
16128 case OMP_CLAUSE_LINEAR:
16129 case OMP_CLAUSE_REDUCTION:
16130 case OMP_CLAUSE_USE_DEVICE_PTR:
16131 case OMP_CLAUSE_IS_DEVICE_PTR:
16132 /* tsubst_expr on SCOPE_REF results in returning
16133 finish_non_static_data_member result. Undo that here. */
16134 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16135 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16136 == IDENTIFIER_NODE))
16137 {
16138 tree t = OMP_CLAUSE_DECL (nc);
16139 tree v = t;
16140 while (v)
16141 switch (TREE_CODE (v))
16142 {
16143 case COMPONENT_REF:
16144 case MEM_REF:
16145 case INDIRECT_REF:
16146 CASE_CONVERT:
16147 case POINTER_PLUS_EXPR:
16148 v = TREE_OPERAND (v, 0);
16149 continue;
16150 case PARM_DECL:
16151 if (DECL_CONTEXT (v) == current_function_decl
16152 && DECL_ARTIFICIAL (v)
16153 && DECL_NAME (v) == this_identifier)
16154 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16155 /* FALLTHRU */
16156 default:
16157 v = NULL_TREE;
16158 break;
16159 }
16160 }
16161 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16162 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16163 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16164 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16165 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16166 {
16167 tree decl = OMP_CLAUSE_DECL (nc);
16168 if (VAR_P (decl))
16169 {
16170 retrofit_lang_decl (decl);
16171 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16172 }
16173 }
16174 break;
16175 default:
16176 break;
16177 }
16178 }
16179
16180 new_clauses = nreverse (new_clauses);
16181 if (ort != C_ORT_OMP_DECLARE_SIMD)
16182 {
16183 new_clauses = finish_omp_clauses (new_clauses, ort);
16184 if (linear_no_step)
16185 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16186 if (nc == linear_no_step)
16187 {
16188 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16189 break;
16190 }
16191 }
16192 return new_clauses;
16193 }
16194
16195 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16196
16197 static tree
16198 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
16199 tree in_decl)
16200 {
16201 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16202
16203 tree purpose, value, chain;
16204
16205 if (t == NULL)
16206 return t;
16207
16208 if (TREE_CODE (t) != TREE_LIST)
16209 return tsubst_copy_and_build (t, args, complain, in_decl,
16210 /*function_p=*/false,
16211 /*integral_constant_expression_p=*/false);
16212
16213 if (t == void_list_node)
16214 return t;
16215
16216 purpose = TREE_PURPOSE (t);
16217 if (purpose)
16218 purpose = RECUR (purpose);
16219 value = TREE_VALUE (t);
16220 if (value)
16221 {
16222 if (TREE_CODE (value) != LABEL_DECL)
16223 value = RECUR (value);
16224 else
16225 {
16226 value = lookup_label (DECL_NAME (value));
16227 gcc_assert (TREE_CODE (value) == LABEL_DECL);
16228 TREE_USED (value) = 1;
16229 }
16230 }
16231 chain = TREE_CHAIN (t);
16232 if (chain && chain != void_type_node)
16233 chain = RECUR (chain);
16234 return tree_cons (purpose, value, chain);
16235 #undef RECUR
16236 }
16237
16238 /* Used to temporarily communicate the list of #pragma omp parallel
16239 clauses to #pragma omp for instantiation if they are combined
16240 together. */
16241
16242 static tree *omp_parallel_combined_clauses;
16243
16244 /* Substitute one OMP_FOR iterator. */
16245
16246 static void
16247 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
16248 tree initv, tree condv, tree incrv, tree *clauses,
16249 tree args, tsubst_flags_t complain, tree in_decl,
16250 bool integral_constant_expression_p)
16251 {
16252 #define RECUR(NODE) \
16253 tsubst_expr ((NODE), args, complain, in_decl, \
16254 integral_constant_expression_p)
16255 tree decl, init, cond, incr;
16256
16257 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
16258 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
16259
16260 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
16261 {
16262 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
16263 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
16264 }
16265
16266 decl = TREE_OPERAND (init, 0);
16267 init = TREE_OPERAND (init, 1);
16268 tree decl_expr = NULL_TREE;
16269 if (init && TREE_CODE (init) == DECL_EXPR)
16270 {
16271 /* We need to jump through some hoops to handle declarations in the
16272 init-statement, since we might need to handle auto deduction,
16273 but we need to keep control of initialization. */
16274 decl_expr = init;
16275 init = DECL_INITIAL (DECL_EXPR_DECL (init));
16276 decl = tsubst_decl (decl, args, complain);
16277 }
16278 else
16279 {
16280 if (TREE_CODE (decl) == SCOPE_REF)
16281 {
16282 decl = RECUR (decl);
16283 if (TREE_CODE (decl) == COMPONENT_REF)
16284 {
16285 tree v = decl;
16286 while (v)
16287 switch (TREE_CODE (v))
16288 {
16289 case COMPONENT_REF:
16290 case MEM_REF:
16291 case INDIRECT_REF:
16292 CASE_CONVERT:
16293 case POINTER_PLUS_EXPR:
16294 v = TREE_OPERAND (v, 0);
16295 continue;
16296 case PARM_DECL:
16297 if (DECL_CONTEXT (v) == current_function_decl
16298 && DECL_ARTIFICIAL (v)
16299 && DECL_NAME (v) == this_identifier)
16300 {
16301 decl = TREE_OPERAND (decl, 1);
16302 decl = omp_privatize_field (decl, false);
16303 }
16304 /* FALLTHRU */
16305 default:
16306 v = NULL_TREE;
16307 break;
16308 }
16309 }
16310 }
16311 else
16312 decl = RECUR (decl);
16313 }
16314 init = RECUR (init);
16315
16316 tree auto_node = type_uses_auto (TREE_TYPE (decl));
16317 if (auto_node && init)
16318 TREE_TYPE (decl)
16319 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
16320
16321 gcc_assert (!type_dependent_expression_p (decl));
16322
16323 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16324 {
16325 if (decl_expr)
16326 {
16327 /* Declare the variable, but don't let that initialize it. */
16328 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
16329 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
16330 RECUR (decl_expr);
16331 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
16332 }
16333
16334 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
16335 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16336 if (TREE_CODE (incr) == MODIFY_EXPR)
16337 {
16338 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16339 tree rhs = RECUR (TREE_OPERAND (incr, 1));
16340 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
16341 NOP_EXPR, rhs, complain);
16342 }
16343 else
16344 incr = RECUR (incr);
16345 TREE_VEC_ELT (declv, i) = decl;
16346 TREE_VEC_ELT (initv, i) = init;
16347 TREE_VEC_ELT (condv, i) = cond;
16348 TREE_VEC_ELT (incrv, i) = incr;
16349 return;
16350 }
16351
16352 if (decl_expr)
16353 {
16354 /* Declare and initialize the variable. */
16355 RECUR (decl_expr);
16356 init = NULL_TREE;
16357 }
16358 else if (init)
16359 {
16360 tree *pc;
16361 int j;
16362 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
16363 {
16364 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16365 {
16366 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16367 && OMP_CLAUSE_DECL (*pc) == decl)
16368 break;
16369 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16370 && OMP_CLAUSE_DECL (*pc) == decl)
16371 {
16372 if (j)
16373 break;
16374 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16375 tree c = *pc;
16376 *pc = OMP_CLAUSE_CHAIN (c);
16377 OMP_CLAUSE_CHAIN (c) = *clauses;
16378 *clauses = c;
16379 }
16380 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16381 && OMP_CLAUSE_DECL (*pc) == decl)
16382 {
16383 error ("iteration variable %qD should not be firstprivate",
16384 decl);
16385 *pc = OMP_CLAUSE_CHAIN (*pc);
16386 }
16387 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16388 && OMP_CLAUSE_DECL (*pc) == decl)
16389 {
16390 error ("iteration variable %qD should not be reduction",
16391 decl);
16392 *pc = OMP_CLAUSE_CHAIN (*pc);
16393 }
16394 else
16395 pc = &OMP_CLAUSE_CHAIN (*pc);
16396 }
16397 if (*pc)
16398 break;
16399 }
16400 if (*pc == NULL_TREE)
16401 {
16402 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16403 OMP_CLAUSE_DECL (c) = decl;
16404 c = finish_omp_clauses (c, C_ORT_OMP);
16405 if (c)
16406 {
16407 OMP_CLAUSE_CHAIN (c) = *clauses;
16408 *clauses = c;
16409 }
16410 }
16411 }
16412 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16413 if (COMPARISON_CLASS_P (cond))
16414 {
16415 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16416 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16417 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16418 }
16419 else
16420 cond = RECUR (cond);
16421 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16422 switch (TREE_CODE (incr))
16423 {
16424 case PREINCREMENT_EXPR:
16425 case PREDECREMENT_EXPR:
16426 case POSTINCREMENT_EXPR:
16427 case POSTDECREMENT_EXPR:
16428 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16429 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16430 break;
16431 case MODIFY_EXPR:
16432 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16433 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16434 {
16435 tree rhs = TREE_OPERAND (incr, 1);
16436 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16437 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16438 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16439 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16440 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16441 rhs0, rhs1));
16442 }
16443 else
16444 incr = RECUR (incr);
16445 break;
16446 case MODOP_EXPR:
16447 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16448 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16449 {
16450 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16451 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16452 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16453 TREE_TYPE (decl), lhs,
16454 RECUR (TREE_OPERAND (incr, 2))));
16455 }
16456 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16457 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16458 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16459 {
16460 tree rhs = TREE_OPERAND (incr, 2);
16461 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16462 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16463 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16464 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16465 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16466 rhs0, rhs1));
16467 }
16468 else
16469 incr = RECUR (incr);
16470 break;
16471 default:
16472 incr = RECUR (incr);
16473 break;
16474 }
16475
16476 TREE_VEC_ELT (declv, i) = decl;
16477 TREE_VEC_ELT (initv, i) = init;
16478 TREE_VEC_ELT (condv, i) = cond;
16479 TREE_VEC_ELT (incrv, i) = incr;
16480 #undef RECUR
16481 }
16482
16483 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16484 of OMP_TARGET's body. */
16485
16486 static tree
16487 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16488 {
16489 *walk_subtrees = 0;
16490 switch (TREE_CODE (*tp))
16491 {
16492 case OMP_TEAMS:
16493 return *tp;
16494 case BIND_EXPR:
16495 case STATEMENT_LIST:
16496 *walk_subtrees = 1;
16497 break;
16498 default:
16499 break;
16500 }
16501 return NULL_TREE;
16502 }
16503
16504 /* Helper function for tsubst_expr. For decomposition declaration
16505 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16506 also the corresponding decls representing the identifiers
16507 of the decomposition declaration. Return DECL if successful
16508 or error_mark_node otherwise, set *FIRST to the first decl
16509 in the list chained through DECL_CHAIN and *CNT to the number
16510 of such decls. */
16511
16512 static tree
16513 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16514 tsubst_flags_t complain, tree in_decl, tree *first,
16515 unsigned int *cnt)
16516 {
16517 tree decl2, decl3, prev = decl;
16518 *cnt = 0;
16519 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16520 for (decl2 = DECL_CHAIN (pattern_decl);
16521 decl2
16522 && VAR_P (decl2)
16523 && DECL_DECOMPOSITION_P (decl2)
16524 && DECL_NAME (decl2);
16525 decl2 = DECL_CHAIN (decl2))
16526 {
16527 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16528 {
16529 gcc_assert (errorcount);
16530 return error_mark_node;
16531 }
16532 (*cnt)++;
16533 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16534 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16535 tree v = DECL_VALUE_EXPR (decl2);
16536 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16537 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16538 decl3 = tsubst (decl2, args, complain, in_decl);
16539 SET_DECL_VALUE_EXPR (decl2, v);
16540 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16541 if (VAR_P (decl3))
16542 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16543 else
16544 {
16545 gcc_assert (errorcount);
16546 decl = error_mark_node;
16547 continue;
16548 }
16549 maybe_push_decl (decl3);
16550 if (error_operand_p (decl3))
16551 decl = error_mark_node;
16552 else if (decl != error_mark_node
16553 && DECL_CHAIN (decl3) != prev
16554 && decl != prev)
16555 {
16556 gcc_assert (errorcount);
16557 decl = error_mark_node;
16558 }
16559 else
16560 prev = decl3;
16561 }
16562 *first = prev;
16563 return decl;
16564 }
16565
16566 /* Like tsubst_copy for expressions, etc. but also does semantic
16567 processing. */
16568
16569 tree
16570 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16571 bool integral_constant_expression_p)
16572 {
16573 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16574 #define RECUR(NODE) \
16575 tsubst_expr ((NODE), args, complain, in_decl, \
16576 integral_constant_expression_p)
16577
16578 tree stmt, tmp;
16579 tree r;
16580 location_t loc;
16581
16582 if (t == NULL_TREE || t == error_mark_node)
16583 return t;
16584
16585 loc = input_location;
16586 if (EXPR_HAS_LOCATION (t))
16587 input_location = EXPR_LOCATION (t);
16588 if (STATEMENT_CODE_P (TREE_CODE (t)))
16589 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16590
16591 switch (TREE_CODE (t))
16592 {
16593 case STATEMENT_LIST:
16594 {
16595 tree_stmt_iterator i;
16596 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16597 RECUR (tsi_stmt (i));
16598 break;
16599 }
16600
16601 case CTOR_INITIALIZER:
16602 finish_mem_initializers (tsubst_initializer_list
16603 (TREE_OPERAND (t, 0), args));
16604 break;
16605
16606 case RETURN_EXPR:
16607 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16608 break;
16609
16610 case EXPR_STMT:
16611 tmp = RECUR (EXPR_STMT_EXPR (t));
16612 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16613 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16614 else
16615 finish_expr_stmt (tmp);
16616 break;
16617
16618 case USING_STMT:
16619 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16620 /*attribs=*/NULL_TREE);
16621 break;
16622
16623 case DECL_EXPR:
16624 {
16625 tree decl, pattern_decl;
16626 tree init;
16627
16628 pattern_decl = decl = DECL_EXPR_DECL (t);
16629 if (TREE_CODE (decl) == LABEL_DECL)
16630 finish_label_decl (DECL_NAME (decl));
16631 else if (TREE_CODE (decl) == USING_DECL)
16632 {
16633 tree scope = USING_DECL_SCOPE (decl);
16634 tree name = DECL_NAME (decl);
16635
16636 scope = tsubst (scope, args, complain, in_decl);
16637 decl = lookup_qualified_name (scope, name,
16638 /*is_type_p=*/false,
16639 /*complain=*/false);
16640 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16641 qualified_name_lookup_error (scope, name, decl, input_location);
16642 else
16643 finish_local_using_decl (decl, scope, name);
16644 }
16645 else if (is_capture_proxy (decl)
16646 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16647 {
16648 /* We're in tsubst_lambda_expr, we've already inserted a new
16649 capture proxy, so look it up and register it. */
16650 tree inst;
16651 if (DECL_PACK_P (decl))
16652 {
16653 inst = (retrieve_local_specialization
16654 (DECL_CAPTURED_VARIABLE (decl)));
16655 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16656 }
16657 else
16658 {
16659 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16660 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16661 gcc_assert (inst != decl && is_capture_proxy (inst));
16662 }
16663 register_local_specialization (inst, decl);
16664 break;
16665 }
16666 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16667 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16668 /* Don't copy the old closure; we'll create a new one in
16669 tsubst_lambda_expr. */
16670 break;
16671 else
16672 {
16673 init = DECL_INITIAL (decl);
16674 decl = tsubst (decl, args, complain, in_decl);
16675 if (decl != error_mark_node)
16676 {
16677 /* By marking the declaration as instantiated, we avoid
16678 trying to instantiate it. Since instantiate_decl can't
16679 handle local variables, and since we've already done
16680 all that needs to be done, that's the right thing to
16681 do. */
16682 if (VAR_P (decl))
16683 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16684 if (VAR_P (decl)
16685 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16686 /* Anonymous aggregates are a special case. */
16687 finish_anon_union (decl);
16688 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16689 {
16690 DECL_CONTEXT (decl) = current_function_decl;
16691 if (DECL_NAME (decl) == this_identifier)
16692 {
16693 tree lam = DECL_CONTEXT (current_function_decl);
16694 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16695 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16696 }
16697 insert_capture_proxy (decl);
16698 }
16699 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16700 /* We already did a pushtag. */;
16701 else if (TREE_CODE (decl) == FUNCTION_DECL
16702 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16703 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16704 {
16705 DECL_CONTEXT (decl) = NULL_TREE;
16706 pushdecl (decl);
16707 DECL_CONTEXT (decl) = current_function_decl;
16708 cp_check_omp_declare_reduction (decl);
16709 }
16710 else
16711 {
16712 int const_init = false;
16713 maybe_push_decl (decl);
16714 if (VAR_P (decl)
16715 && DECL_PRETTY_FUNCTION_P (decl))
16716 {
16717 /* For __PRETTY_FUNCTION__ we have to adjust the
16718 initializer. */
16719 const char *const name
16720 = cxx_printable_name (current_function_decl, 2);
16721 init = cp_fname_init (name, &TREE_TYPE (decl));
16722 }
16723 else
16724 init = tsubst_init (init, decl, args, complain, in_decl);
16725
16726 if (VAR_P (decl))
16727 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16728 (pattern_decl));
16729 if (VAR_P (decl)
16730 && DECL_DECOMPOSITION_P (decl)
16731 && TREE_TYPE (pattern_decl) != error_mark_node)
16732 {
16733 unsigned int cnt;
16734 tree first;
16735 tree ndecl
16736 = tsubst_decomp_names (decl, pattern_decl, args,
16737 complain, in_decl, &first, &cnt);
16738 if (ndecl != error_mark_node)
16739 cp_maybe_mangle_decomp (ndecl, first, cnt);
16740 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16741 if (ndecl != error_mark_node)
16742 cp_finish_decomp (ndecl, first, cnt);
16743 }
16744 else
16745 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16746 }
16747 }
16748 }
16749
16750 break;
16751 }
16752
16753 case FOR_STMT:
16754 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16755 RECUR (FOR_INIT_STMT (t));
16756 finish_init_stmt (stmt);
16757 tmp = RECUR (FOR_COND (t));
16758 finish_for_cond (tmp, stmt, false, 0);
16759 tmp = RECUR (FOR_EXPR (t));
16760 finish_for_expr (tmp, stmt);
16761 {
16762 bool prev = note_iteration_stmt_body_start ();
16763 RECUR (FOR_BODY (t));
16764 note_iteration_stmt_body_end (prev);
16765 }
16766 finish_for_stmt (stmt);
16767 break;
16768
16769 case RANGE_FOR_STMT:
16770 {
16771 /* Construct another range_for, if this is not a final
16772 substitution (for inside inside a generic lambda of a
16773 template). Otherwise convert to a regular for. */
16774 tree decl, expr;
16775 stmt = (processing_template_decl
16776 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16777 : begin_for_stmt (NULL_TREE, NULL_TREE));
16778 decl = RANGE_FOR_DECL (t);
16779 decl = tsubst (decl, args, complain, in_decl);
16780 maybe_push_decl (decl);
16781 expr = RECUR (RANGE_FOR_EXPR (t));
16782
16783 tree decomp_first = NULL_TREE;
16784 unsigned decomp_cnt = 0;
16785 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16786 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16787 complain, in_decl,
16788 &decomp_first, &decomp_cnt);
16789
16790 if (processing_template_decl)
16791 {
16792 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16793 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16794 finish_range_for_decl (stmt, decl, expr);
16795 }
16796 else
16797 {
16798 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16799 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16800 stmt = cp_convert_range_for (stmt, decl, expr,
16801 decomp_first, decomp_cnt,
16802 RANGE_FOR_IVDEP (t), unroll);
16803 }
16804
16805 bool prev = note_iteration_stmt_body_start ();
16806 RECUR (RANGE_FOR_BODY (t));
16807 note_iteration_stmt_body_end (prev);
16808 finish_for_stmt (stmt);
16809 }
16810 break;
16811
16812 case WHILE_STMT:
16813 stmt = begin_while_stmt ();
16814 tmp = RECUR (WHILE_COND (t));
16815 finish_while_stmt_cond (tmp, stmt, false, 0);
16816 {
16817 bool prev = note_iteration_stmt_body_start ();
16818 RECUR (WHILE_BODY (t));
16819 note_iteration_stmt_body_end (prev);
16820 }
16821 finish_while_stmt (stmt);
16822 break;
16823
16824 case DO_STMT:
16825 stmt = begin_do_stmt ();
16826 {
16827 bool prev = note_iteration_stmt_body_start ();
16828 RECUR (DO_BODY (t));
16829 note_iteration_stmt_body_end (prev);
16830 }
16831 finish_do_body (stmt);
16832 tmp = RECUR (DO_COND (t));
16833 finish_do_stmt (tmp, stmt, false, 0);
16834 break;
16835
16836 case IF_STMT:
16837 stmt = begin_if_stmt ();
16838 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16839 if (IF_STMT_CONSTEXPR_P (t))
16840 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
16841 tmp = RECUR (IF_COND (t));
16842 tmp = finish_if_stmt_cond (tmp, stmt);
16843 if (IF_STMT_CONSTEXPR_P (t)
16844 && instantiation_dependent_expression_p (tmp))
16845 {
16846 /* We're partially instantiating a generic lambda, but the condition
16847 of the constexpr if is still dependent. Don't substitute into the
16848 branches now, just remember the template arguments. */
16849 do_poplevel (IF_SCOPE (stmt));
16850 IF_COND (stmt) = IF_COND (t);
16851 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
16852 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
16853 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
16854 add_stmt (stmt);
16855 break;
16856 }
16857 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16858 /* Don't instantiate the THEN_CLAUSE. */;
16859 else
16860 {
16861 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16862 if (inhibit)
16863 ++c_inhibit_evaluation_warnings;
16864 RECUR (THEN_CLAUSE (t));
16865 if (inhibit)
16866 --c_inhibit_evaluation_warnings;
16867 }
16868 finish_then_clause (stmt);
16869
16870 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16871 /* Don't instantiate the ELSE_CLAUSE. */;
16872 else if (ELSE_CLAUSE (t))
16873 {
16874 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16875 begin_else_clause (stmt);
16876 if (inhibit)
16877 ++c_inhibit_evaluation_warnings;
16878 RECUR (ELSE_CLAUSE (t));
16879 if (inhibit)
16880 --c_inhibit_evaluation_warnings;
16881 finish_else_clause (stmt);
16882 }
16883
16884 finish_if_stmt (stmt);
16885 break;
16886
16887 case BIND_EXPR:
16888 if (BIND_EXPR_BODY_BLOCK (t))
16889 stmt = begin_function_body ();
16890 else
16891 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16892 ? BCS_TRY_BLOCK : 0);
16893
16894 RECUR (BIND_EXPR_BODY (t));
16895
16896 if (BIND_EXPR_BODY_BLOCK (t))
16897 finish_function_body (stmt);
16898 else
16899 finish_compound_stmt (stmt);
16900 break;
16901
16902 case BREAK_STMT:
16903 finish_break_stmt ();
16904 break;
16905
16906 case CONTINUE_STMT:
16907 finish_continue_stmt ();
16908 break;
16909
16910 case SWITCH_STMT:
16911 stmt = begin_switch_stmt ();
16912 tmp = RECUR (SWITCH_STMT_COND (t));
16913 finish_switch_cond (tmp, stmt);
16914 RECUR (SWITCH_STMT_BODY (t));
16915 finish_switch_stmt (stmt);
16916 break;
16917
16918 case CASE_LABEL_EXPR:
16919 {
16920 tree low = RECUR (CASE_LOW (t));
16921 tree high = RECUR (CASE_HIGH (t));
16922 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16923 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16924 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16925 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16926 }
16927 break;
16928
16929 case LABEL_EXPR:
16930 {
16931 tree decl = LABEL_EXPR_LABEL (t);
16932 tree label;
16933
16934 label = finish_label_stmt (DECL_NAME (decl));
16935 if (TREE_CODE (label) == LABEL_DECL)
16936 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16937 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16938 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16939 }
16940 break;
16941
16942 case GOTO_EXPR:
16943 tmp = GOTO_DESTINATION (t);
16944 if (TREE_CODE (tmp) != LABEL_DECL)
16945 /* Computed goto's must be tsubst'd into. On the other hand,
16946 non-computed gotos must not be; the identifier in question
16947 will have no binding. */
16948 tmp = RECUR (tmp);
16949 else
16950 tmp = DECL_NAME (tmp);
16951 finish_goto_stmt (tmp);
16952 break;
16953
16954 case ASM_EXPR:
16955 {
16956 tree string = RECUR (ASM_STRING (t));
16957 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16958 complain, in_decl);
16959 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16960 complain, in_decl);
16961 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16962 complain, in_decl);
16963 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16964 complain, in_decl);
16965 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16966 clobbers, labels);
16967 tree asm_expr = tmp;
16968 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16969 asm_expr = TREE_OPERAND (asm_expr, 0);
16970 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16971 }
16972 break;
16973
16974 case TRY_BLOCK:
16975 if (CLEANUP_P (t))
16976 {
16977 stmt = begin_try_block ();
16978 RECUR (TRY_STMTS (t));
16979 finish_cleanup_try_block (stmt);
16980 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16981 }
16982 else
16983 {
16984 tree compound_stmt = NULL_TREE;
16985
16986 if (FN_TRY_BLOCK_P (t))
16987 stmt = begin_function_try_block (&compound_stmt);
16988 else
16989 stmt = begin_try_block ();
16990
16991 RECUR (TRY_STMTS (t));
16992
16993 if (FN_TRY_BLOCK_P (t))
16994 finish_function_try_block (stmt);
16995 else
16996 finish_try_block (stmt);
16997
16998 RECUR (TRY_HANDLERS (t));
16999 if (FN_TRY_BLOCK_P (t))
17000 finish_function_handler_sequence (stmt, compound_stmt);
17001 else
17002 finish_handler_sequence (stmt);
17003 }
17004 break;
17005
17006 case HANDLER:
17007 {
17008 tree decl = HANDLER_PARMS (t);
17009
17010 if (decl)
17011 {
17012 decl = tsubst (decl, args, complain, in_decl);
17013 /* Prevent instantiate_decl from trying to instantiate
17014 this variable. We've already done all that needs to be
17015 done. */
17016 if (decl != error_mark_node)
17017 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17018 }
17019 stmt = begin_handler ();
17020 finish_handler_parms (decl, stmt);
17021 RECUR (HANDLER_BODY (t));
17022 finish_handler (stmt);
17023 }
17024 break;
17025
17026 case TAG_DEFN:
17027 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17028 if (CLASS_TYPE_P (tmp))
17029 {
17030 /* Local classes are not independent templates; they are
17031 instantiated along with their containing function. And this
17032 way we don't have to deal with pushing out of one local class
17033 to instantiate a member of another local class. */
17034 /* Closures are handled by the LAMBDA_EXPR. */
17035 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17036 complete_type (tmp);
17037 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17038 if ((VAR_P (fld)
17039 || (TREE_CODE (fld) == FUNCTION_DECL
17040 && !DECL_ARTIFICIAL (fld)))
17041 && DECL_TEMPLATE_INSTANTIATION (fld))
17042 instantiate_decl (fld, /*defer_ok=*/false,
17043 /*expl_inst_class=*/false);
17044 }
17045 break;
17046
17047 case STATIC_ASSERT:
17048 {
17049 tree condition;
17050
17051 ++c_inhibit_evaluation_warnings;
17052 condition =
17053 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17054 args,
17055 complain, in_decl,
17056 /*integral_constant_expression_p=*/true);
17057 --c_inhibit_evaluation_warnings;
17058
17059 finish_static_assert (condition,
17060 STATIC_ASSERT_MESSAGE (t),
17061 STATIC_ASSERT_SOURCE_LOCATION (t),
17062 /*member_p=*/false);
17063 }
17064 break;
17065
17066 case OACC_KERNELS:
17067 case OACC_PARALLEL:
17068 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
17069 in_decl);
17070 stmt = begin_omp_parallel ();
17071 RECUR (OMP_BODY (t));
17072 finish_omp_construct (TREE_CODE (t), stmt, tmp);
17073 break;
17074
17075 case OMP_PARALLEL:
17076 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
17077 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
17078 complain, in_decl);
17079 if (OMP_PARALLEL_COMBINED (t))
17080 omp_parallel_combined_clauses = &tmp;
17081 stmt = begin_omp_parallel ();
17082 RECUR (OMP_PARALLEL_BODY (t));
17083 gcc_assert (omp_parallel_combined_clauses == NULL);
17084 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
17085 = OMP_PARALLEL_COMBINED (t);
17086 pop_omp_privatization_clauses (r);
17087 break;
17088
17089 case OMP_TASK:
17090 r = push_omp_privatization_clauses (false);
17091 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17092 complain, in_decl);
17093 stmt = begin_omp_task ();
17094 RECUR (OMP_TASK_BODY (t));
17095 finish_omp_task (tmp, stmt);
17096 pop_omp_privatization_clauses (r);
17097 break;
17098
17099 case OMP_FOR:
17100 case OMP_SIMD:
17101 case OMP_DISTRIBUTE:
17102 case OMP_TASKLOOP:
17103 case OACC_LOOP:
17104 {
17105 tree clauses, body, pre_body;
17106 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
17107 tree orig_declv = NULL_TREE;
17108 tree incrv = NULL_TREE;
17109 enum c_omp_region_type ort = C_ORT_OMP;
17110 int i;
17111
17112 if (TREE_CODE (t) == OACC_LOOP)
17113 ort = C_ORT_ACC;
17114
17115 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
17116 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
17117 in_decl);
17118 if (OMP_FOR_INIT (t) != NULL_TREE)
17119 {
17120 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17121 if (OMP_FOR_ORIG_DECLS (t))
17122 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17123 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17124 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17125 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17126 }
17127
17128 stmt = begin_omp_structured_block ();
17129
17130 pre_body = push_stmt_list ();
17131 RECUR (OMP_FOR_PRE_BODY (t));
17132 pre_body = pop_stmt_list (pre_body);
17133
17134 if (OMP_FOR_INIT (t) != NULL_TREE)
17135 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17136 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
17137 incrv, &clauses, args, complain, in_decl,
17138 integral_constant_expression_p);
17139 omp_parallel_combined_clauses = NULL;
17140
17141 body = push_stmt_list ();
17142 RECUR (OMP_FOR_BODY (t));
17143 body = pop_stmt_list (body);
17144
17145 if (OMP_FOR_INIT (t) != NULL_TREE)
17146 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
17147 orig_declv, initv, condv, incrv, body, pre_body,
17148 NULL, clauses);
17149 else
17150 {
17151 t = make_node (TREE_CODE (t));
17152 TREE_TYPE (t) = void_type_node;
17153 OMP_FOR_BODY (t) = body;
17154 OMP_FOR_PRE_BODY (t) = pre_body;
17155 OMP_FOR_CLAUSES (t) = clauses;
17156 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
17157 add_stmt (t);
17158 }
17159
17160 add_stmt (finish_omp_structured_block (stmt));
17161 pop_omp_privatization_clauses (r);
17162 }
17163 break;
17164
17165 case OMP_SECTIONS:
17166 omp_parallel_combined_clauses = NULL;
17167 /* FALLTHRU */
17168 case OMP_SINGLE:
17169 case OMP_TEAMS:
17170 case OMP_CRITICAL:
17171 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
17172 && OMP_TEAMS_COMBINED (t));
17173 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
17174 in_decl);
17175 stmt = push_stmt_list ();
17176 RECUR (OMP_BODY (t));
17177 stmt = pop_stmt_list (stmt);
17178
17179 t = copy_node (t);
17180 OMP_BODY (t) = stmt;
17181 OMP_CLAUSES (t) = tmp;
17182 add_stmt (t);
17183 pop_omp_privatization_clauses (r);
17184 break;
17185
17186 case OACC_DATA:
17187 case OMP_TARGET_DATA:
17188 case OMP_TARGET:
17189 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
17190 ? C_ORT_ACC : C_ORT_OMP, args, complain,
17191 in_decl);
17192 keep_next_level (true);
17193 stmt = begin_omp_structured_block ();
17194
17195 RECUR (OMP_BODY (t));
17196 stmt = finish_omp_structured_block (stmt);
17197
17198 t = copy_node (t);
17199 OMP_BODY (t) = stmt;
17200 OMP_CLAUSES (t) = tmp;
17201 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
17202 {
17203 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
17204 if (teams)
17205 {
17206 /* For combined target teams, ensure the num_teams and
17207 thread_limit clause expressions are evaluated on the host,
17208 before entering the target construct. */
17209 tree c;
17210 for (c = OMP_TEAMS_CLAUSES (teams);
17211 c; c = OMP_CLAUSE_CHAIN (c))
17212 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17213 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17214 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17215 {
17216 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17217 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
17218 if (expr == error_mark_node)
17219 continue;
17220 tmp = TARGET_EXPR_SLOT (expr);
17221 add_stmt (expr);
17222 OMP_CLAUSE_OPERAND (c, 0) = expr;
17223 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17224 OMP_CLAUSE_FIRSTPRIVATE);
17225 OMP_CLAUSE_DECL (tc) = tmp;
17226 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
17227 OMP_TARGET_CLAUSES (t) = tc;
17228 }
17229 }
17230 }
17231 add_stmt (t);
17232 break;
17233
17234 case OACC_DECLARE:
17235 t = copy_node (t);
17236 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
17237 complain, in_decl);
17238 OACC_DECLARE_CLAUSES (t) = tmp;
17239 add_stmt (t);
17240 break;
17241
17242 case OMP_TARGET_UPDATE:
17243 case OMP_TARGET_ENTER_DATA:
17244 case OMP_TARGET_EXIT_DATA:
17245 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
17246 complain, in_decl);
17247 t = copy_node (t);
17248 OMP_STANDALONE_CLAUSES (t) = tmp;
17249 add_stmt (t);
17250 break;
17251
17252 case OACC_ENTER_DATA:
17253 case OACC_EXIT_DATA:
17254 case OACC_UPDATE:
17255 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
17256 complain, in_decl);
17257 t = copy_node (t);
17258 OMP_STANDALONE_CLAUSES (t) = tmp;
17259 add_stmt (t);
17260 break;
17261
17262 case OMP_ORDERED:
17263 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
17264 complain, in_decl);
17265 stmt = push_stmt_list ();
17266 RECUR (OMP_BODY (t));
17267 stmt = pop_stmt_list (stmt);
17268
17269 t = copy_node (t);
17270 OMP_BODY (t) = stmt;
17271 OMP_ORDERED_CLAUSES (t) = tmp;
17272 add_stmt (t);
17273 break;
17274
17275 case OMP_SECTION:
17276 case OMP_MASTER:
17277 case OMP_TASKGROUP:
17278 stmt = push_stmt_list ();
17279 RECUR (OMP_BODY (t));
17280 stmt = pop_stmt_list (stmt);
17281
17282 t = copy_node (t);
17283 OMP_BODY (t) = stmt;
17284 add_stmt (t);
17285 break;
17286
17287 case OMP_ATOMIC:
17288 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
17289 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
17290 {
17291 tree op1 = TREE_OPERAND (t, 1);
17292 tree rhs1 = NULL_TREE;
17293 tree lhs, rhs;
17294 if (TREE_CODE (op1) == COMPOUND_EXPR)
17295 {
17296 rhs1 = RECUR (TREE_OPERAND (op1, 0));
17297 op1 = TREE_OPERAND (op1, 1);
17298 }
17299 lhs = RECUR (TREE_OPERAND (op1, 0));
17300 rhs = RECUR (TREE_OPERAND (op1, 1));
17301 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
17302 NULL_TREE, NULL_TREE, rhs1,
17303 OMP_ATOMIC_SEQ_CST (t));
17304 }
17305 else
17306 {
17307 tree op1 = TREE_OPERAND (t, 1);
17308 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
17309 tree rhs1 = NULL_TREE;
17310 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
17311 enum tree_code opcode = NOP_EXPR;
17312 if (code == OMP_ATOMIC_READ)
17313 {
17314 v = RECUR (TREE_OPERAND (op1, 0));
17315 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17316 }
17317 else if (code == OMP_ATOMIC_CAPTURE_OLD
17318 || code == OMP_ATOMIC_CAPTURE_NEW)
17319 {
17320 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
17321 v = RECUR (TREE_OPERAND (op1, 0));
17322 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17323 if (TREE_CODE (op11) == COMPOUND_EXPR)
17324 {
17325 rhs1 = RECUR (TREE_OPERAND (op11, 0));
17326 op11 = TREE_OPERAND (op11, 1);
17327 }
17328 lhs = RECUR (TREE_OPERAND (op11, 0));
17329 rhs = RECUR (TREE_OPERAND (op11, 1));
17330 opcode = TREE_CODE (op11);
17331 if (opcode == MODIFY_EXPR)
17332 opcode = NOP_EXPR;
17333 }
17334 else
17335 {
17336 code = OMP_ATOMIC;
17337 lhs = RECUR (TREE_OPERAND (op1, 0));
17338 rhs = RECUR (TREE_OPERAND (op1, 1));
17339 }
17340 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
17341 OMP_ATOMIC_SEQ_CST (t));
17342 }
17343 break;
17344
17345 case TRANSACTION_EXPR:
17346 {
17347 int flags = 0;
17348 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
17349 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
17350
17351 if (TRANSACTION_EXPR_IS_STMT (t))
17352 {
17353 tree body = TRANSACTION_EXPR_BODY (t);
17354 tree noex = NULL_TREE;
17355 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
17356 {
17357 noex = MUST_NOT_THROW_COND (body);
17358 if (noex == NULL_TREE)
17359 noex = boolean_true_node;
17360 body = TREE_OPERAND (body, 0);
17361 }
17362 stmt = begin_transaction_stmt (input_location, NULL, flags);
17363 RECUR (body);
17364 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
17365 }
17366 else
17367 {
17368 stmt = build_transaction_expr (EXPR_LOCATION (t),
17369 RECUR (TRANSACTION_EXPR_BODY (t)),
17370 flags, NULL_TREE);
17371 RETURN (stmt);
17372 }
17373 }
17374 break;
17375
17376 case MUST_NOT_THROW_EXPR:
17377 {
17378 tree op0 = RECUR (TREE_OPERAND (t, 0));
17379 tree cond = RECUR (MUST_NOT_THROW_COND (t));
17380 RETURN (build_must_not_throw_expr (op0, cond));
17381 }
17382
17383 case EXPR_PACK_EXPANSION:
17384 error ("invalid use of pack expansion expression");
17385 RETURN (error_mark_node);
17386
17387 case NONTYPE_ARGUMENT_PACK:
17388 error ("use %<...%> to expand argument pack");
17389 RETURN (error_mark_node);
17390
17391 case COMPOUND_EXPR:
17392 tmp = RECUR (TREE_OPERAND (t, 0));
17393 if (tmp == NULL_TREE)
17394 /* If the first operand was a statement, we're done with it. */
17395 RETURN (RECUR (TREE_OPERAND (t, 1)));
17396 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17397 RECUR (TREE_OPERAND (t, 1)),
17398 complain));
17399
17400 case ANNOTATE_EXPR:
17401 tmp = RECUR (TREE_OPERAND (t, 0));
17402 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17403 TREE_TYPE (tmp), tmp,
17404 RECUR (TREE_OPERAND (t, 1)),
17405 RECUR (TREE_OPERAND (t, 2))));
17406
17407 default:
17408 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17409
17410 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17411 /*function_p=*/false,
17412 integral_constant_expression_p));
17413 }
17414
17415 RETURN (NULL_TREE);
17416 out:
17417 input_location = loc;
17418 return r;
17419 #undef RECUR
17420 #undef RETURN
17421 }
17422
17423 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17424 function. For description of the body see comment above
17425 cp_parser_omp_declare_reduction_exprs. */
17426
17427 static void
17428 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17429 {
17430 if (t == NULL_TREE || t == error_mark_node)
17431 return;
17432
17433 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17434
17435 tree_stmt_iterator tsi;
17436 int i;
17437 tree stmts[7];
17438 memset (stmts, 0, sizeof stmts);
17439 for (i = 0, tsi = tsi_start (t);
17440 i < 7 && !tsi_end_p (tsi);
17441 i++, tsi_next (&tsi))
17442 stmts[i] = tsi_stmt (tsi);
17443 gcc_assert (tsi_end_p (tsi));
17444
17445 if (i >= 3)
17446 {
17447 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17448 && TREE_CODE (stmts[1]) == DECL_EXPR);
17449 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17450 args, complain, in_decl);
17451 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17452 args, complain, in_decl);
17453 DECL_CONTEXT (omp_out) = current_function_decl;
17454 DECL_CONTEXT (omp_in) = current_function_decl;
17455 keep_next_level (true);
17456 tree block = begin_omp_structured_block ();
17457 tsubst_expr (stmts[2], args, complain, in_decl, false);
17458 block = finish_omp_structured_block (block);
17459 block = maybe_cleanup_point_expr_void (block);
17460 add_decl_expr (omp_out);
17461 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17462 TREE_NO_WARNING (omp_out) = 1;
17463 add_decl_expr (omp_in);
17464 finish_expr_stmt (block);
17465 }
17466 if (i >= 6)
17467 {
17468 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17469 && TREE_CODE (stmts[4]) == DECL_EXPR);
17470 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17471 args, complain, in_decl);
17472 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17473 args, complain, in_decl);
17474 DECL_CONTEXT (omp_priv) = current_function_decl;
17475 DECL_CONTEXT (omp_orig) = current_function_decl;
17476 keep_next_level (true);
17477 tree block = begin_omp_structured_block ();
17478 tsubst_expr (stmts[5], args, complain, in_decl, false);
17479 block = finish_omp_structured_block (block);
17480 block = maybe_cleanup_point_expr_void (block);
17481 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17482 add_decl_expr (omp_priv);
17483 add_decl_expr (omp_orig);
17484 finish_expr_stmt (block);
17485 if (i == 7)
17486 add_decl_expr (omp_orig);
17487 }
17488 }
17489
17490 /* T is a postfix-expression that is not being used in a function
17491 call. Return the substituted version of T. */
17492
17493 static tree
17494 tsubst_non_call_postfix_expression (tree t, tree args,
17495 tsubst_flags_t complain,
17496 tree in_decl)
17497 {
17498 if (TREE_CODE (t) == SCOPE_REF)
17499 t = tsubst_qualified_id (t, args, complain, in_decl,
17500 /*done=*/false, /*address_p=*/false);
17501 else
17502 t = tsubst_copy_and_build (t, args, complain, in_decl,
17503 /*function_p=*/false,
17504 /*integral_constant_expression_p=*/false);
17505
17506 return t;
17507 }
17508
17509 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17510 instantiation context. Instantiating a pack expansion containing a lambda
17511 might result in multiple lambdas all based on the same lambda in the
17512 template. */
17513
17514 tree
17515 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17516 {
17517 tree oldfn = lambda_function (t);
17518 in_decl = oldfn;
17519
17520 tree r = build_lambda_expr ();
17521
17522 LAMBDA_EXPR_LOCATION (r)
17523 = LAMBDA_EXPR_LOCATION (t);
17524 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17525 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17526 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17527
17528 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
17529 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
17530 else
17531 record_lambda_scope (r);
17532
17533 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17534 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17535
17536 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
17537 cap = TREE_CHAIN (cap))
17538 {
17539 tree field = TREE_PURPOSE (cap);
17540 if (PACK_EXPANSION_P (field))
17541 field = PACK_EXPANSION_PATTERN (field);
17542 field = tsubst_decl (field, args, complain);
17543
17544 if (field == error_mark_node)
17545 return error_mark_node;
17546
17547 tree init = TREE_VALUE (cap);
17548 if (PACK_EXPANSION_P (init))
17549 init = tsubst_pack_expansion (init, args, complain, in_decl);
17550 else
17551 init = tsubst_copy_and_build (init, args, complain, in_decl,
17552 /*fn*/false, /*constexpr*/false);
17553
17554 if (TREE_CODE (field) == TREE_VEC)
17555 {
17556 int len = TREE_VEC_LENGTH (field);
17557 gcc_assert (TREE_CODE (init) == TREE_VEC
17558 && TREE_VEC_LENGTH (init) == len);
17559 for (int i = 0; i < len; ++i)
17560 LAMBDA_EXPR_CAPTURE_LIST (r)
17561 = tree_cons (TREE_VEC_ELT (field, i),
17562 TREE_VEC_ELT (init, i),
17563 LAMBDA_EXPR_CAPTURE_LIST (r));
17564 }
17565 else
17566 {
17567 LAMBDA_EXPR_CAPTURE_LIST (r)
17568 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17569
17570 if (id_equal (DECL_NAME (field), "__this"))
17571 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17572 }
17573 }
17574
17575 tree type = begin_lambda_type (r);
17576 if (type == error_mark_node)
17577 return error_mark_node;
17578
17579 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17580 determine_visibility (TYPE_NAME (type));
17581
17582 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17583
17584 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17585 ? DECL_TI_TEMPLATE (oldfn)
17586 : NULL_TREE);
17587
17588 tree fntype = static_fn_type (oldfn);
17589 if (oldtmpl)
17590 ++processing_template_decl;
17591 fntype = tsubst (fntype, args, complain, in_decl);
17592 if (oldtmpl)
17593 --processing_template_decl;
17594
17595 if (fntype == error_mark_node)
17596 r = error_mark_node;
17597 else
17598 {
17599 /* Fix the type of 'this'. */
17600 fntype = build_memfn_type (fntype, type,
17601 type_memfn_quals (fntype),
17602 type_memfn_rqual (fntype));
17603 tree fn, tmpl;
17604 if (oldtmpl)
17605 {
17606 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17607 fn = DECL_TEMPLATE_RESULT (tmpl);
17608 finish_member_declaration (tmpl);
17609 }
17610 else
17611 {
17612 tmpl = NULL_TREE;
17613 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17614 finish_member_declaration (fn);
17615 }
17616
17617 /* Let finish_function set this. */
17618 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17619
17620 bool nested = cfun;
17621 if (nested)
17622 push_function_context ();
17623 else
17624 /* Still increment function_depth so that we don't GC in the
17625 middle of an expression. */
17626 ++function_depth;
17627
17628 local_specialization_stack s (lss_copy);
17629
17630 tree body = start_lambda_function (fn, r);
17631
17632 register_parameter_specializations (oldfn, fn);
17633
17634 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17635 /*constexpr*/false);
17636
17637 finish_lambda_function (body);
17638
17639 if (nested)
17640 pop_function_context ();
17641 else
17642 --function_depth;
17643
17644 /* The capture list was built up in reverse order; fix that now. */
17645 LAMBDA_EXPR_CAPTURE_LIST (r)
17646 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17647
17648 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17649
17650 maybe_add_lambda_conv_op (type);
17651 }
17652
17653 finish_struct (type, /*attr*/NULL_TREE);
17654
17655 insert_pending_capture_proxies ();
17656
17657 return r;
17658 }
17659
17660 /* Like tsubst but deals with expressions and performs semantic
17661 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17662
17663 tree
17664 tsubst_copy_and_build (tree t,
17665 tree args,
17666 tsubst_flags_t complain,
17667 tree in_decl,
17668 bool function_p,
17669 bool integral_constant_expression_p)
17670 {
17671 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17672 #define RECUR(NODE) \
17673 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17674 /*function_p=*/false, \
17675 integral_constant_expression_p)
17676
17677 tree retval, op1;
17678 location_t loc;
17679
17680 if (t == NULL_TREE || t == error_mark_node)
17681 return t;
17682
17683 loc = input_location;
17684 if (EXPR_HAS_LOCATION (t))
17685 input_location = EXPR_LOCATION (t);
17686
17687 /* N3276 decltype magic only applies to calls at the top level or on the
17688 right side of a comma. */
17689 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17690 complain &= ~tf_decltype;
17691
17692 switch (TREE_CODE (t))
17693 {
17694 case USING_DECL:
17695 t = DECL_NAME (t);
17696 /* Fall through. */
17697 case IDENTIFIER_NODE:
17698 {
17699 tree decl;
17700 cp_id_kind idk;
17701 bool non_integral_constant_expression_p;
17702 const char *error_msg;
17703
17704 if (IDENTIFIER_CONV_OP_P (t))
17705 {
17706 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17707 t = make_conv_op_name (new_type);
17708 }
17709
17710 /* Look up the name. */
17711 decl = lookup_name (t);
17712
17713 /* By convention, expressions use ERROR_MARK_NODE to indicate
17714 failure, not NULL_TREE. */
17715 if (decl == NULL_TREE)
17716 decl = error_mark_node;
17717
17718 decl = finish_id_expression (t, decl, NULL_TREE,
17719 &idk,
17720 integral_constant_expression_p,
17721 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17722 &non_integral_constant_expression_p,
17723 /*template_p=*/false,
17724 /*done=*/true,
17725 /*address_p=*/false,
17726 /*template_arg_p=*/false,
17727 &error_msg,
17728 input_location);
17729 if (error_msg)
17730 error (error_msg);
17731 if (!function_p && identifier_p (decl))
17732 {
17733 if (complain & tf_error)
17734 unqualified_name_lookup_error (decl);
17735 decl = error_mark_node;
17736 }
17737 RETURN (decl);
17738 }
17739
17740 case TEMPLATE_ID_EXPR:
17741 {
17742 tree object;
17743 tree templ = RECUR (TREE_OPERAND (t, 0));
17744 tree targs = TREE_OPERAND (t, 1);
17745
17746 if (targs)
17747 targs = tsubst_template_args (targs, args, complain, in_decl);
17748 if (targs == error_mark_node)
17749 RETURN (error_mark_node);
17750
17751 if (TREE_CODE (templ) == SCOPE_REF)
17752 {
17753 tree name = TREE_OPERAND (templ, 1);
17754 tree tid = lookup_template_function (name, targs);
17755 TREE_OPERAND (templ, 1) = tid;
17756 RETURN (templ);
17757 }
17758
17759 if (variable_template_p (templ))
17760 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17761
17762 if (TREE_CODE (templ) == COMPONENT_REF)
17763 {
17764 object = TREE_OPERAND (templ, 0);
17765 templ = TREE_OPERAND (templ, 1);
17766 }
17767 else
17768 object = NULL_TREE;
17769 templ = lookup_template_function (templ, targs);
17770
17771 if (object)
17772 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17773 object, templ, NULL_TREE));
17774 else
17775 RETURN (baselink_for_fns (templ));
17776 }
17777
17778 case INDIRECT_REF:
17779 {
17780 tree r = RECUR (TREE_OPERAND (t, 0));
17781
17782 if (REFERENCE_REF_P (t))
17783 {
17784 /* A type conversion to reference type will be enclosed in
17785 such an indirect ref, but the substitution of the cast
17786 will have also added such an indirect ref. */
17787 r = convert_from_reference (r);
17788 }
17789 else
17790 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17791 complain|decltype_flag);
17792
17793 if (REF_PARENTHESIZED_P (t))
17794 r = force_paren_expr (r);
17795
17796 RETURN (r);
17797 }
17798
17799 case NOP_EXPR:
17800 {
17801 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17802 tree op0 = RECUR (TREE_OPERAND (t, 0));
17803 RETURN (build_nop (type, op0));
17804 }
17805
17806 case IMPLICIT_CONV_EXPR:
17807 {
17808 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17809 tree expr = RECUR (TREE_OPERAND (t, 0));
17810 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17811 {
17812 retval = copy_node (t);
17813 TREE_TYPE (retval) = type;
17814 TREE_OPERAND (retval, 0) = expr;
17815 RETURN (retval);
17816 }
17817 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17818 /* We'll pass this to convert_nontype_argument again, we don't need
17819 to actually perform any conversion here. */
17820 RETURN (expr);
17821 int flags = LOOKUP_IMPLICIT;
17822 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17823 flags = LOOKUP_NORMAL;
17824 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17825 flags));
17826 }
17827
17828 case CONVERT_EXPR:
17829 {
17830 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17831 tree op0 = RECUR (TREE_OPERAND (t, 0));
17832 if (op0 == error_mark_node)
17833 RETURN (error_mark_node);
17834 RETURN (build1 (CONVERT_EXPR, type, op0));
17835 }
17836
17837 case CAST_EXPR:
17838 case REINTERPRET_CAST_EXPR:
17839 case CONST_CAST_EXPR:
17840 case DYNAMIC_CAST_EXPR:
17841 case STATIC_CAST_EXPR:
17842 {
17843 tree type;
17844 tree op, r = NULL_TREE;
17845
17846 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17847 if (integral_constant_expression_p
17848 && !cast_valid_in_integral_constant_expression_p (type))
17849 {
17850 if (complain & tf_error)
17851 error ("a cast to a type other than an integral or "
17852 "enumeration type cannot appear in a constant-expression");
17853 RETURN (error_mark_node);
17854 }
17855
17856 op = RECUR (TREE_OPERAND (t, 0));
17857
17858 warning_sentinel s(warn_useless_cast);
17859 warning_sentinel s2(warn_ignored_qualifiers);
17860 switch (TREE_CODE (t))
17861 {
17862 case CAST_EXPR:
17863 r = build_functional_cast (type, op, complain);
17864 break;
17865 case REINTERPRET_CAST_EXPR:
17866 r = build_reinterpret_cast (type, op, complain);
17867 break;
17868 case CONST_CAST_EXPR:
17869 r = build_const_cast (type, op, complain);
17870 break;
17871 case DYNAMIC_CAST_EXPR:
17872 r = build_dynamic_cast (type, op, complain);
17873 break;
17874 case STATIC_CAST_EXPR:
17875 r = build_static_cast (type, op, complain);
17876 break;
17877 default:
17878 gcc_unreachable ();
17879 }
17880
17881 RETURN (r);
17882 }
17883
17884 case POSTDECREMENT_EXPR:
17885 case POSTINCREMENT_EXPR:
17886 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17887 args, complain, in_decl);
17888 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17889 complain|decltype_flag));
17890
17891 case PREDECREMENT_EXPR:
17892 case PREINCREMENT_EXPR:
17893 case NEGATE_EXPR:
17894 case BIT_NOT_EXPR:
17895 case ABS_EXPR:
17896 case TRUTH_NOT_EXPR:
17897 case UNARY_PLUS_EXPR: /* Unary + */
17898 case REALPART_EXPR:
17899 case IMAGPART_EXPR:
17900 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17901 RECUR (TREE_OPERAND (t, 0)),
17902 complain|decltype_flag));
17903
17904 case FIX_TRUNC_EXPR:
17905 gcc_unreachable ();
17906
17907 case ADDR_EXPR:
17908 op1 = TREE_OPERAND (t, 0);
17909 if (TREE_CODE (op1) == LABEL_DECL)
17910 RETURN (finish_label_address_expr (DECL_NAME (op1),
17911 EXPR_LOCATION (op1)));
17912 if (TREE_CODE (op1) == SCOPE_REF)
17913 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17914 /*done=*/true, /*address_p=*/true);
17915 else
17916 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17917 in_decl);
17918 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17919 complain|decltype_flag));
17920
17921 case PLUS_EXPR:
17922 case MINUS_EXPR:
17923 case MULT_EXPR:
17924 case TRUNC_DIV_EXPR:
17925 case CEIL_DIV_EXPR:
17926 case FLOOR_DIV_EXPR:
17927 case ROUND_DIV_EXPR:
17928 case EXACT_DIV_EXPR:
17929 case BIT_AND_EXPR:
17930 case BIT_IOR_EXPR:
17931 case BIT_XOR_EXPR:
17932 case TRUNC_MOD_EXPR:
17933 case FLOOR_MOD_EXPR:
17934 case TRUTH_ANDIF_EXPR:
17935 case TRUTH_ORIF_EXPR:
17936 case TRUTH_AND_EXPR:
17937 case TRUTH_OR_EXPR:
17938 case RSHIFT_EXPR:
17939 case LSHIFT_EXPR:
17940 case RROTATE_EXPR:
17941 case LROTATE_EXPR:
17942 case EQ_EXPR:
17943 case NE_EXPR:
17944 case MAX_EXPR:
17945 case MIN_EXPR:
17946 case LE_EXPR:
17947 case GE_EXPR:
17948 case LT_EXPR:
17949 case GT_EXPR:
17950 case MEMBER_REF:
17951 case DOTSTAR_EXPR:
17952 {
17953 warning_sentinel s1(warn_type_limits);
17954 warning_sentinel s2(warn_div_by_zero);
17955 warning_sentinel s3(warn_logical_op);
17956 warning_sentinel s4(warn_tautological_compare);
17957 tree op0 = RECUR (TREE_OPERAND (t, 0));
17958 tree op1 = RECUR (TREE_OPERAND (t, 1));
17959 tree r = build_x_binary_op
17960 (input_location, TREE_CODE (t),
17961 op0,
17962 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17963 ? ERROR_MARK
17964 : TREE_CODE (TREE_OPERAND (t, 0))),
17965 op1,
17966 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17967 ? ERROR_MARK
17968 : TREE_CODE (TREE_OPERAND (t, 1))),
17969 /*overload=*/NULL,
17970 complain|decltype_flag);
17971 if (EXPR_P (r) && TREE_NO_WARNING (t))
17972 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17973
17974 RETURN (r);
17975 }
17976
17977 case POINTER_PLUS_EXPR:
17978 {
17979 tree op0 = RECUR (TREE_OPERAND (t, 0));
17980 tree op1 = RECUR (TREE_OPERAND (t, 1));
17981 RETURN (fold_build_pointer_plus (op0, op1));
17982 }
17983
17984 case SCOPE_REF:
17985 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17986 /*address_p=*/false));
17987 case ARRAY_REF:
17988 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17989 args, complain, in_decl);
17990 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17991 RECUR (TREE_OPERAND (t, 1)),
17992 complain|decltype_flag));
17993
17994 case SIZEOF_EXPR:
17995 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17996 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17997 RETURN (tsubst_copy (t, args, complain, in_decl));
17998 /* Fall through */
17999
18000 case ALIGNOF_EXPR:
18001 {
18002 tree r;
18003
18004 op1 = TREE_OPERAND (t, 0);
18005 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
18006 op1 = TREE_TYPE (op1);
18007 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
18008 && ALIGNOF_EXPR_STD_P (t));
18009 if (!args)
18010 {
18011 /* When there are no ARGS, we are trying to evaluate a
18012 non-dependent expression from the parser. Trying to do
18013 the substitutions may not work. */
18014 if (!TYPE_P (op1))
18015 op1 = TREE_TYPE (op1);
18016 }
18017 else
18018 {
18019 ++cp_unevaluated_operand;
18020 ++c_inhibit_evaluation_warnings;
18021 if (TYPE_P (op1))
18022 op1 = tsubst (op1, args, complain, in_decl);
18023 else
18024 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18025 /*function_p=*/false,
18026 /*integral_constant_expression_p=*/
18027 false);
18028 --cp_unevaluated_operand;
18029 --c_inhibit_evaluation_warnings;
18030 }
18031 if (TYPE_P (op1))
18032 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
18033 complain & tf_error);
18034 else
18035 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
18036 complain & tf_error);
18037 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
18038 {
18039 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
18040 {
18041 if (!processing_template_decl && TYPE_P (op1))
18042 {
18043 r = build_min (SIZEOF_EXPR, size_type_node,
18044 build1 (NOP_EXPR, op1, error_mark_node));
18045 SIZEOF_EXPR_TYPE_P (r) = 1;
18046 }
18047 else
18048 r = build_min (SIZEOF_EXPR, size_type_node, op1);
18049 TREE_SIDE_EFFECTS (r) = 0;
18050 TREE_READONLY (r) = 1;
18051 }
18052 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
18053 }
18054 RETURN (r);
18055 }
18056
18057 case AT_ENCODE_EXPR:
18058 {
18059 op1 = TREE_OPERAND (t, 0);
18060 ++cp_unevaluated_operand;
18061 ++c_inhibit_evaluation_warnings;
18062 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18063 /*function_p=*/false,
18064 /*integral_constant_expression_p=*/false);
18065 --cp_unevaluated_operand;
18066 --c_inhibit_evaluation_warnings;
18067 RETURN (objc_build_encode_expr (op1));
18068 }
18069
18070 case NOEXCEPT_EXPR:
18071 op1 = TREE_OPERAND (t, 0);
18072 ++cp_unevaluated_operand;
18073 ++c_inhibit_evaluation_warnings;
18074 ++cp_noexcept_operand;
18075 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18076 /*function_p=*/false,
18077 /*integral_constant_expression_p=*/false);
18078 --cp_unevaluated_operand;
18079 --c_inhibit_evaluation_warnings;
18080 --cp_noexcept_operand;
18081 RETURN (finish_noexcept_expr (op1, complain));
18082
18083 case MODOP_EXPR:
18084 {
18085 warning_sentinel s(warn_div_by_zero);
18086 tree lhs = RECUR (TREE_OPERAND (t, 0));
18087 tree rhs = RECUR (TREE_OPERAND (t, 2));
18088 tree r = build_x_modify_expr
18089 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
18090 complain|decltype_flag);
18091 /* TREE_NO_WARNING must be set if either the expression was
18092 parenthesized or it uses an operator such as >>= rather
18093 than plain assignment. In the former case, it was already
18094 set and must be copied. In the latter case,
18095 build_x_modify_expr sets it and it must not be reset
18096 here. */
18097 if (TREE_NO_WARNING (t))
18098 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18099
18100 RETURN (r);
18101 }
18102
18103 case ARROW_EXPR:
18104 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18105 args, complain, in_decl);
18106 /* Remember that there was a reference to this entity. */
18107 if (DECL_P (op1)
18108 && !mark_used (op1, complain) && !(complain & tf_error))
18109 RETURN (error_mark_node);
18110 RETURN (build_x_arrow (input_location, op1, complain));
18111
18112 case NEW_EXPR:
18113 {
18114 tree placement = RECUR (TREE_OPERAND (t, 0));
18115 tree init = RECUR (TREE_OPERAND (t, 3));
18116 vec<tree, va_gc> *placement_vec;
18117 vec<tree, va_gc> *init_vec;
18118 tree ret;
18119
18120 if (placement == NULL_TREE)
18121 placement_vec = NULL;
18122 else
18123 {
18124 placement_vec = make_tree_vector ();
18125 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
18126 vec_safe_push (placement_vec, TREE_VALUE (placement));
18127 }
18128
18129 /* If there was an initializer in the original tree, but it
18130 instantiated to an empty list, then we should pass a
18131 non-NULL empty vector to tell build_new that it was an
18132 empty initializer() rather than no initializer. This can
18133 only happen when the initializer is a pack expansion whose
18134 parameter packs are of length zero. */
18135 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
18136 init_vec = NULL;
18137 else
18138 {
18139 init_vec = make_tree_vector ();
18140 if (init == void_node)
18141 gcc_assert (init_vec != NULL);
18142 else
18143 {
18144 for (; init != NULL_TREE; init = TREE_CHAIN (init))
18145 vec_safe_push (init_vec, TREE_VALUE (init));
18146 }
18147 }
18148
18149 /* Avoid passing an enclosing decl to valid_array_size_p. */
18150 in_decl = NULL_TREE;
18151
18152 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
18153 tree op2 = RECUR (TREE_OPERAND (t, 2));
18154 ret = build_new (&placement_vec, op1, op2, &init_vec,
18155 NEW_EXPR_USE_GLOBAL (t),
18156 complain);
18157
18158 if (placement_vec != NULL)
18159 release_tree_vector (placement_vec);
18160 if (init_vec != NULL)
18161 release_tree_vector (init_vec);
18162
18163 RETURN (ret);
18164 }
18165
18166 case DELETE_EXPR:
18167 {
18168 tree op0 = RECUR (TREE_OPERAND (t, 0));
18169 tree op1 = RECUR (TREE_OPERAND (t, 1));
18170 RETURN (delete_sanity (op0, op1,
18171 DELETE_EXPR_USE_VEC (t),
18172 DELETE_EXPR_USE_GLOBAL (t),
18173 complain));
18174 }
18175
18176 case COMPOUND_EXPR:
18177 {
18178 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
18179 complain & ~tf_decltype, in_decl,
18180 /*function_p=*/false,
18181 integral_constant_expression_p);
18182 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
18183 op0,
18184 RECUR (TREE_OPERAND (t, 1)),
18185 complain|decltype_flag));
18186 }
18187
18188 case CALL_EXPR:
18189 {
18190 tree function;
18191 vec<tree, va_gc> *call_args;
18192 unsigned int nargs, i;
18193 bool qualified_p;
18194 bool koenig_p;
18195 tree ret;
18196
18197 function = CALL_EXPR_FN (t);
18198 /* Internal function with no arguments. */
18199 if (function == NULL_TREE && call_expr_nargs (t) == 0)
18200 RETURN (t);
18201
18202 /* When we parsed the expression, we determined whether or
18203 not Koenig lookup should be performed. */
18204 koenig_p = KOENIG_LOOKUP_P (t);
18205 if (function == NULL_TREE)
18206 {
18207 koenig_p = false;
18208 qualified_p = false;
18209 }
18210 else if (TREE_CODE (function) == SCOPE_REF)
18211 {
18212 qualified_p = true;
18213 function = tsubst_qualified_id (function, args, complain, in_decl,
18214 /*done=*/false,
18215 /*address_p=*/false);
18216 }
18217 else if (koenig_p && identifier_p (function))
18218 {
18219 /* Do nothing; calling tsubst_copy_and_build on an identifier
18220 would incorrectly perform unqualified lookup again.
18221
18222 Note that we can also have an IDENTIFIER_NODE if the earlier
18223 unqualified lookup found a member function; in that case
18224 koenig_p will be false and we do want to do the lookup
18225 again to find the instantiated member function.
18226
18227 FIXME but doing that causes c++/15272, so we need to stop
18228 using IDENTIFIER_NODE in that situation. */
18229 qualified_p = false;
18230 }
18231 else
18232 {
18233 if (TREE_CODE (function) == COMPONENT_REF)
18234 {
18235 tree op = TREE_OPERAND (function, 1);
18236
18237 qualified_p = (TREE_CODE (op) == SCOPE_REF
18238 || (BASELINK_P (op)
18239 && BASELINK_QUALIFIED_P (op)));
18240 }
18241 else
18242 qualified_p = false;
18243
18244 if (TREE_CODE (function) == ADDR_EXPR
18245 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
18246 /* Avoid error about taking the address of a constructor. */
18247 function = TREE_OPERAND (function, 0);
18248
18249 function = tsubst_copy_and_build (function, args, complain,
18250 in_decl,
18251 !qualified_p,
18252 integral_constant_expression_p);
18253
18254 if (BASELINK_P (function))
18255 qualified_p = true;
18256 }
18257
18258 nargs = call_expr_nargs (t);
18259 call_args = make_tree_vector ();
18260 for (i = 0; i < nargs; ++i)
18261 {
18262 tree arg = CALL_EXPR_ARG (t, i);
18263
18264 if (!PACK_EXPANSION_P (arg))
18265 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
18266 else
18267 {
18268 /* Expand the pack expansion and push each entry onto
18269 CALL_ARGS. */
18270 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
18271 if (TREE_CODE (arg) == TREE_VEC)
18272 {
18273 unsigned int len, j;
18274
18275 len = TREE_VEC_LENGTH (arg);
18276 for (j = 0; j < len; ++j)
18277 {
18278 tree value = TREE_VEC_ELT (arg, j);
18279 if (value != NULL_TREE)
18280 value = convert_from_reference (value);
18281 vec_safe_push (call_args, value);
18282 }
18283 }
18284 else
18285 {
18286 /* A partial substitution. Add one entry. */
18287 vec_safe_push (call_args, arg);
18288 }
18289 }
18290 }
18291
18292 /* We do not perform argument-dependent lookup if normal
18293 lookup finds a non-function, in accordance with the
18294 expected resolution of DR 218. */
18295 if (koenig_p
18296 && ((is_overloaded_fn (function)
18297 /* If lookup found a member function, the Koenig lookup is
18298 not appropriate, even if an unqualified-name was used
18299 to denote the function. */
18300 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
18301 || identifier_p (function))
18302 /* Only do this when substitution turns a dependent call
18303 into a non-dependent call. */
18304 && type_dependent_expression_p_push (t)
18305 && !any_type_dependent_arguments_p (call_args))
18306 function = perform_koenig_lookup (function, call_args, tf_none);
18307
18308 if (function != NULL_TREE
18309 && identifier_p (function)
18310 && !any_type_dependent_arguments_p (call_args))
18311 {
18312 if (koenig_p && (complain & tf_warning_or_error))
18313 {
18314 /* For backwards compatibility and good diagnostics, try
18315 the unqualified lookup again if we aren't in SFINAE
18316 context. */
18317 tree unq = (tsubst_copy_and_build
18318 (function, args, complain, in_decl, true,
18319 integral_constant_expression_p));
18320 if (unq == error_mark_node)
18321 {
18322 release_tree_vector (call_args);
18323 RETURN (error_mark_node);
18324 }
18325
18326 if (unq != function)
18327 {
18328 /* In a lambda fn, we have to be careful to not
18329 introduce new this captures. Legacy code can't
18330 be using lambdas anyway, so it's ok to be
18331 stricter. */
18332 bool in_lambda = (current_class_type
18333 && LAMBDA_TYPE_P (current_class_type));
18334 char const *const msg
18335 = G_("%qD was not declared in this scope, "
18336 "and no declarations were found by "
18337 "argument-dependent lookup at the point "
18338 "of instantiation");
18339
18340 bool diag = true;
18341 if (in_lambda)
18342 error_at (EXPR_LOC_OR_LOC (t, input_location),
18343 msg, function);
18344 else
18345 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
18346 msg, function);
18347 if (diag)
18348 {
18349 tree fn = unq;
18350
18351 if (INDIRECT_REF_P (fn))
18352 fn = TREE_OPERAND (fn, 0);
18353 if (is_overloaded_fn (fn))
18354 fn = get_first_fn (fn);
18355
18356 if (!DECL_P (fn))
18357 /* Can't say anything more. */;
18358 else if (DECL_CLASS_SCOPE_P (fn))
18359 {
18360 location_t loc = EXPR_LOC_OR_LOC (t,
18361 input_location);
18362 inform (loc,
18363 "declarations in dependent base %qT are "
18364 "not found by unqualified lookup",
18365 DECL_CLASS_CONTEXT (fn));
18366 if (current_class_ptr)
18367 inform (loc,
18368 "use %<this->%D%> instead", function);
18369 else
18370 inform (loc,
18371 "use %<%T::%D%> instead",
18372 current_class_name, function);
18373 }
18374 else
18375 inform (DECL_SOURCE_LOCATION (fn),
18376 "%qD declared here, later in the "
18377 "translation unit", fn);
18378 if (in_lambda)
18379 {
18380 release_tree_vector (call_args);
18381 RETURN (error_mark_node);
18382 }
18383 }
18384
18385 function = unq;
18386 }
18387 }
18388 if (identifier_p (function))
18389 {
18390 if (complain & tf_error)
18391 unqualified_name_lookup_error (function);
18392 release_tree_vector (call_args);
18393 RETURN (error_mark_node);
18394 }
18395 }
18396
18397 /* Remember that there was a reference to this entity. */
18398 if (function != NULL_TREE
18399 && DECL_P (function)
18400 && !mark_used (function, complain) && !(complain & tf_error))
18401 {
18402 release_tree_vector (call_args);
18403 RETURN (error_mark_node);
18404 }
18405
18406 /* Put back tf_decltype for the actual call. */
18407 complain |= decltype_flag;
18408
18409 if (function == NULL_TREE)
18410 switch (CALL_EXPR_IFN (t))
18411 {
18412 case IFN_LAUNDER:
18413 gcc_assert (nargs == 1);
18414 if (vec_safe_length (call_args) != 1)
18415 {
18416 error_at (EXPR_LOC_OR_LOC (t, input_location),
18417 "wrong number of arguments to "
18418 "%<__builtin_launder%>");
18419 ret = error_mark_node;
18420 }
18421 else
18422 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
18423 input_location),
18424 (*call_args)[0], complain);
18425 break;
18426
18427 default:
18428 /* Unsupported internal function with arguments. */
18429 gcc_unreachable ();
18430 }
18431 else if (TREE_CODE (function) == OFFSET_REF)
18432 ret = build_offset_ref_call_from_tree (function, &call_args,
18433 complain);
18434 else if (TREE_CODE (function) == COMPONENT_REF)
18435 {
18436 tree instance = TREE_OPERAND (function, 0);
18437 tree fn = TREE_OPERAND (function, 1);
18438
18439 if (processing_template_decl
18440 && (type_dependent_expression_p (instance)
18441 || (!BASELINK_P (fn)
18442 && TREE_CODE (fn) != FIELD_DECL)
18443 || type_dependent_expression_p (fn)
18444 || any_type_dependent_arguments_p (call_args)))
18445 ret = build_min_nt_call_vec (function, call_args);
18446 else if (!BASELINK_P (fn))
18447 ret = finish_call_expr (function, &call_args,
18448 /*disallow_virtual=*/false,
18449 /*koenig_p=*/false,
18450 complain);
18451 else
18452 ret = (build_new_method_call
18453 (instance, fn,
18454 &call_args, NULL_TREE,
18455 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
18456 /*fn_p=*/NULL,
18457 complain));
18458 }
18459 else
18460 ret = finish_call_expr (function, &call_args,
18461 /*disallow_virtual=*/qualified_p,
18462 koenig_p,
18463 complain);
18464
18465 release_tree_vector (call_args);
18466
18467 if (ret != error_mark_node)
18468 {
18469 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
18470 bool ord = CALL_EXPR_ORDERED_ARGS (t);
18471 bool rev = CALL_EXPR_REVERSE_ARGS (t);
18472 bool thk = CALL_FROM_THUNK_P (t);
18473 if (op || ord || rev || thk)
18474 {
18475 function = extract_call_expr (ret);
18476 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
18477 CALL_EXPR_ORDERED_ARGS (function) = ord;
18478 CALL_EXPR_REVERSE_ARGS (function) = rev;
18479 if (thk)
18480 {
18481 if (TREE_CODE (function) == CALL_EXPR)
18482 CALL_FROM_THUNK_P (function) = true;
18483 else
18484 AGGR_INIT_FROM_THUNK_P (function) = true;
18485 /* The thunk location is not interesting. */
18486 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
18487 }
18488 }
18489 }
18490
18491 RETURN (ret);
18492 }
18493
18494 case COND_EXPR:
18495 {
18496 tree cond = RECUR (TREE_OPERAND (t, 0));
18497 tree folded_cond = fold_non_dependent_expr (cond);
18498 tree exp1, exp2;
18499
18500 if (TREE_CODE (folded_cond) == INTEGER_CST)
18501 {
18502 if (integer_zerop (folded_cond))
18503 {
18504 ++c_inhibit_evaluation_warnings;
18505 exp1 = RECUR (TREE_OPERAND (t, 1));
18506 --c_inhibit_evaluation_warnings;
18507 exp2 = RECUR (TREE_OPERAND (t, 2));
18508 }
18509 else
18510 {
18511 exp1 = RECUR (TREE_OPERAND (t, 1));
18512 ++c_inhibit_evaluation_warnings;
18513 exp2 = RECUR (TREE_OPERAND (t, 2));
18514 --c_inhibit_evaluation_warnings;
18515 }
18516 cond = folded_cond;
18517 }
18518 else
18519 {
18520 exp1 = RECUR (TREE_OPERAND (t, 1));
18521 exp2 = RECUR (TREE_OPERAND (t, 2));
18522 }
18523
18524 warning_sentinel s(warn_duplicated_branches);
18525 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
18526 cond, exp1, exp2, complain));
18527 }
18528
18529 case PSEUDO_DTOR_EXPR:
18530 {
18531 tree op0 = RECUR (TREE_OPERAND (t, 0));
18532 tree op1 = RECUR (TREE_OPERAND (t, 1));
18533 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
18534 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
18535 input_location));
18536 }
18537
18538 case TREE_LIST:
18539 {
18540 tree purpose, value, chain;
18541
18542 if (t == void_list_node)
18543 RETURN (t);
18544
18545 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
18546 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
18547 {
18548 /* We have pack expansions, so expand those and
18549 create a new list out of it. */
18550 tree purposevec = NULL_TREE;
18551 tree valuevec = NULL_TREE;
18552 tree chain;
18553 int i, len = -1;
18554
18555 /* Expand the argument expressions. */
18556 if (TREE_PURPOSE (t))
18557 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
18558 complain, in_decl);
18559 if (TREE_VALUE (t))
18560 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
18561 complain, in_decl);
18562
18563 /* Build the rest of the list. */
18564 chain = TREE_CHAIN (t);
18565 if (chain && chain != void_type_node)
18566 chain = RECUR (chain);
18567
18568 /* Determine the number of arguments. */
18569 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
18570 {
18571 len = TREE_VEC_LENGTH (purposevec);
18572 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18573 }
18574 else if (TREE_CODE (valuevec) == TREE_VEC)
18575 len = TREE_VEC_LENGTH (valuevec);
18576 else
18577 {
18578 /* Since we only performed a partial substitution into
18579 the argument pack, we only RETURN (a single list
18580 node. */
18581 if (purposevec == TREE_PURPOSE (t)
18582 && valuevec == TREE_VALUE (t)
18583 && chain == TREE_CHAIN (t))
18584 RETURN (t);
18585
18586 RETURN (tree_cons (purposevec, valuevec, chain));
18587 }
18588
18589 /* Convert the argument vectors into a TREE_LIST */
18590 i = len;
18591 while (i > 0)
18592 {
18593 /* Grab the Ith values. */
18594 i--;
18595 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
18596 : NULL_TREE;
18597 value
18598 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
18599 : NULL_TREE;
18600
18601 /* Build the list (backwards). */
18602 chain = tree_cons (purpose, value, chain);
18603 }
18604
18605 RETURN (chain);
18606 }
18607
18608 purpose = TREE_PURPOSE (t);
18609 if (purpose)
18610 purpose = RECUR (purpose);
18611 value = TREE_VALUE (t);
18612 if (value)
18613 value = RECUR (value);
18614 chain = TREE_CHAIN (t);
18615 if (chain && chain != void_type_node)
18616 chain = RECUR (chain);
18617 if (purpose == TREE_PURPOSE (t)
18618 && value == TREE_VALUE (t)
18619 && chain == TREE_CHAIN (t))
18620 RETURN (t);
18621 RETURN (tree_cons (purpose, value, chain));
18622 }
18623
18624 case COMPONENT_REF:
18625 {
18626 tree object;
18627 tree object_type;
18628 tree member;
18629 tree r;
18630
18631 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18632 args, complain, in_decl);
18633 /* Remember that there was a reference to this entity. */
18634 if (DECL_P (object)
18635 && !mark_used (object, complain) && !(complain & tf_error))
18636 RETURN (error_mark_node);
18637 object_type = TREE_TYPE (object);
18638
18639 member = TREE_OPERAND (t, 1);
18640 if (BASELINK_P (member))
18641 member = tsubst_baselink (member,
18642 non_reference (TREE_TYPE (object)),
18643 args, complain, in_decl);
18644 else
18645 member = tsubst_copy (member, args, complain, in_decl);
18646 if (member == error_mark_node)
18647 RETURN (error_mark_node);
18648
18649 if (TREE_CODE (member) == FIELD_DECL)
18650 {
18651 r = finish_non_static_data_member (member, object, NULL_TREE);
18652 if (TREE_CODE (r) == COMPONENT_REF)
18653 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18654 RETURN (r);
18655 }
18656 else if (type_dependent_expression_p (object))
18657 /* We can't do much here. */;
18658 else if (!CLASS_TYPE_P (object_type))
18659 {
18660 if (scalarish_type_p (object_type))
18661 {
18662 tree s = NULL_TREE;
18663 tree dtor = member;
18664
18665 if (TREE_CODE (dtor) == SCOPE_REF)
18666 {
18667 s = TREE_OPERAND (dtor, 0);
18668 dtor = TREE_OPERAND (dtor, 1);
18669 }
18670 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18671 {
18672 dtor = TREE_OPERAND (dtor, 0);
18673 if (TYPE_P (dtor))
18674 RETURN (finish_pseudo_destructor_expr
18675 (object, s, dtor, input_location));
18676 }
18677 }
18678 }
18679 else if (TREE_CODE (member) == SCOPE_REF
18680 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18681 {
18682 /* Lookup the template functions now that we know what the
18683 scope is. */
18684 tree scope = TREE_OPERAND (member, 0);
18685 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18686 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18687 member = lookup_qualified_name (scope, tmpl,
18688 /*is_type_p=*/false,
18689 /*complain=*/false);
18690 if (BASELINK_P (member))
18691 {
18692 BASELINK_FUNCTIONS (member)
18693 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18694 args);
18695 member = (adjust_result_of_qualified_name_lookup
18696 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18697 object_type));
18698 }
18699 else
18700 {
18701 qualified_name_lookup_error (scope, tmpl, member,
18702 input_location);
18703 RETURN (error_mark_node);
18704 }
18705 }
18706 else if (TREE_CODE (member) == SCOPE_REF
18707 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18708 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18709 {
18710 if (complain & tf_error)
18711 {
18712 if (TYPE_P (TREE_OPERAND (member, 0)))
18713 error ("%qT is not a class or namespace",
18714 TREE_OPERAND (member, 0));
18715 else
18716 error ("%qD is not a class or namespace",
18717 TREE_OPERAND (member, 0));
18718 }
18719 RETURN (error_mark_node);
18720 }
18721
18722 r = finish_class_member_access_expr (object, member,
18723 /*template_p=*/false,
18724 complain);
18725 if (TREE_CODE (r) == COMPONENT_REF)
18726 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18727 RETURN (r);
18728 }
18729
18730 case THROW_EXPR:
18731 RETURN (build_throw
18732 (RECUR (TREE_OPERAND (t, 0))));
18733
18734 case CONSTRUCTOR:
18735 {
18736 vec<constructor_elt, va_gc> *n;
18737 constructor_elt *ce;
18738 unsigned HOST_WIDE_INT idx;
18739 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18740 bool process_index_p;
18741 int newlen;
18742 bool need_copy_p = false;
18743 tree r;
18744
18745 if (type == error_mark_node)
18746 RETURN (error_mark_node);
18747
18748 /* We do not want to process the index of aggregate
18749 initializers as they are identifier nodes which will be
18750 looked up by digest_init. */
18751 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18752
18753 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18754 newlen = vec_safe_length (n);
18755 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18756 {
18757 if (ce->index && process_index_p
18758 /* An identifier index is looked up in the type
18759 being initialized, not the current scope. */
18760 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18761 ce->index = RECUR (ce->index);
18762
18763 if (PACK_EXPANSION_P (ce->value))
18764 {
18765 /* Substitute into the pack expansion. */
18766 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18767 in_decl);
18768
18769 if (ce->value == error_mark_node
18770 || PACK_EXPANSION_P (ce->value))
18771 ;
18772 else if (TREE_VEC_LENGTH (ce->value) == 1)
18773 /* Just move the argument into place. */
18774 ce->value = TREE_VEC_ELT (ce->value, 0);
18775 else
18776 {
18777 /* Update the length of the final CONSTRUCTOR
18778 arguments vector, and note that we will need to
18779 copy.*/
18780 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18781 need_copy_p = true;
18782 }
18783 }
18784 else
18785 ce->value = RECUR (ce->value);
18786 }
18787
18788 if (need_copy_p)
18789 {
18790 vec<constructor_elt, va_gc> *old_n = n;
18791
18792 vec_alloc (n, newlen);
18793 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18794 {
18795 if (TREE_CODE (ce->value) == TREE_VEC)
18796 {
18797 int i, len = TREE_VEC_LENGTH (ce->value);
18798 for (i = 0; i < len; ++i)
18799 CONSTRUCTOR_APPEND_ELT (n, 0,
18800 TREE_VEC_ELT (ce->value, i));
18801 }
18802 else
18803 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18804 }
18805 }
18806
18807 r = build_constructor (init_list_type_node, n);
18808 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18809
18810 if (TREE_HAS_CONSTRUCTOR (t))
18811 {
18812 fcl_t cl = fcl_functional;
18813 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18814 cl = fcl_c99;
18815 RETURN (finish_compound_literal (type, r, complain, cl));
18816 }
18817
18818 TREE_TYPE (r) = type;
18819 RETURN (r);
18820 }
18821
18822 case TYPEID_EXPR:
18823 {
18824 tree operand_0 = TREE_OPERAND (t, 0);
18825 if (TYPE_P (operand_0))
18826 {
18827 operand_0 = tsubst (operand_0, args, complain, in_decl);
18828 RETURN (get_typeid (operand_0, complain));
18829 }
18830 else
18831 {
18832 operand_0 = RECUR (operand_0);
18833 RETURN (build_typeid (operand_0, complain));
18834 }
18835 }
18836
18837 case VAR_DECL:
18838 if (!args)
18839 RETURN (t);
18840 /* Fall through */
18841
18842 case PARM_DECL:
18843 {
18844 tree r = tsubst_copy (t, args, complain, in_decl);
18845 /* ??? We're doing a subset of finish_id_expression here. */
18846 if (VAR_P (r)
18847 && !processing_template_decl
18848 && !cp_unevaluated_operand
18849 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18850 && CP_DECL_THREAD_LOCAL_P (r))
18851 {
18852 if (tree wrap = get_tls_wrapper_fn (r))
18853 /* Replace an evaluated use of the thread_local variable with
18854 a call to its wrapper. */
18855 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18856 }
18857 else if (outer_automatic_var_p (r))
18858 r = process_outer_var_ref (r, complain);
18859
18860 if (!TYPE_REF_P (TREE_TYPE (t)))
18861 /* If the original type was a reference, we'll be wrapped in
18862 the appropriate INDIRECT_REF. */
18863 r = convert_from_reference (r);
18864 RETURN (r);
18865 }
18866
18867 case VA_ARG_EXPR:
18868 {
18869 tree op0 = RECUR (TREE_OPERAND (t, 0));
18870 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18871 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18872 }
18873
18874 case OFFSETOF_EXPR:
18875 {
18876 tree object_ptr
18877 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18878 in_decl, /*function_p=*/false,
18879 /*integral_constant_expression_p=*/false);
18880 RETURN (finish_offsetof (object_ptr,
18881 RECUR (TREE_OPERAND (t, 0)),
18882 EXPR_LOCATION (t)));
18883 }
18884
18885 case ADDRESSOF_EXPR:
18886 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18887 RECUR (TREE_OPERAND (t, 0)), complain));
18888
18889 case TRAIT_EXPR:
18890 {
18891 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18892 complain, in_decl);
18893
18894 tree type2 = TRAIT_EXPR_TYPE2 (t);
18895 if (type2 && TREE_CODE (type2) == TREE_LIST)
18896 type2 = RECUR (type2);
18897 else if (type2)
18898 type2 = tsubst (type2, args, complain, in_decl);
18899
18900 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18901 }
18902
18903 case STMT_EXPR:
18904 {
18905 tree old_stmt_expr = cur_stmt_expr;
18906 tree stmt_expr = begin_stmt_expr ();
18907
18908 cur_stmt_expr = stmt_expr;
18909 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18910 integral_constant_expression_p);
18911 stmt_expr = finish_stmt_expr (stmt_expr, false);
18912 cur_stmt_expr = old_stmt_expr;
18913
18914 /* If the resulting list of expression statement is empty,
18915 fold it further into void_node. */
18916 if (empty_expr_stmt_p (stmt_expr))
18917 stmt_expr = void_node;
18918
18919 RETURN (stmt_expr);
18920 }
18921
18922 case LAMBDA_EXPR:
18923 {
18924 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18925
18926 RETURN (build_lambda_object (r));
18927 }
18928
18929 case TARGET_EXPR:
18930 /* We can get here for a constant initializer of non-dependent type.
18931 FIXME stop folding in cp_parser_initializer_clause. */
18932 {
18933 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18934 complain);
18935 RETURN (r);
18936 }
18937
18938 case TRANSACTION_EXPR:
18939 RETURN (tsubst_expr(t, args, complain, in_decl,
18940 integral_constant_expression_p));
18941
18942 case PAREN_EXPR:
18943 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18944
18945 case VEC_PERM_EXPR:
18946 {
18947 tree op0 = RECUR (TREE_OPERAND (t, 0));
18948 tree op1 = RECUR (TREE_OPERAND (t, 1));
18949 tree op2 = RECUR (TREE_OPERAND (t, 2));
18950 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18951 complain));
18952 }
18953
18954 case REQUIRES_EXPR:
18955 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18956
18957 case NON_LVALUE_EXPR:
18958 case VIEW_CONVERT_EXPR:
18959 /* We should only see these for location wrapper nodes, or within
18960 instantiate_non_dependent_expr (when args is NULL_TREE). */
18961 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
18962 if (location_wrapper_p (t))
18963 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
18964 EXPR_LOCATION (t)));
18965 /* fallthrough. */
18966
18967 default:
18968 /* Handle Objective-C++ constructs, if appropriate. */
18969 {
18970 tree subst
18971 = objcp_tsubst_copy_and_build (t, args, complain,
18972 in_decl, /*function_p=*/false);
18973 if (subst)
18974 RETURN (subst);
18975 }
18976 RETURN (tsubst_copy (t, args, complain, in_decl));
18977 }
18978
18979 #undef RECUR
18980 #undef RETURN
18981 out:
18982 input_location = loc;
18983 return retval;
18984 }
18985
18986 /* Verify that the instantiated ARGS are valid. For type arguments,
18987 make sure that the type's linkage is ok. For non-type arguments,
18988 make sure they are constants if they are integral or enumerations.
18989 Emit an error under control of COMPLAIN, and return TRUE on error. */
18990
18991 static bool
18992 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18993 {
18994 if (dependent_template_arg_p (t))
18995 return false;
18996 if (ARGUMENT_PACK_P (t))
18997 {
18998 tree vec = ARGUMENT_PACK_ARGS (t);
18999 int len = TREE_VEC_LENGTH (vec);
19000 bool result = false;
19001 int i;
19002
19003 for (i = 0; i < len; ++i)
19004 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
19005 result = true;
19006 return result;
19007 }
19008 else if (TYPE_P (t))
19009 {
19010 /* [basic.link]: A name with no linkage (notably, the name
19011 of a class or enumeration declared in a local scope)
19012 shall not be used to declare an entity with linkage.
19013 This implies that names with no linkage cannot be used as
19014 template arguments
19015
19016 DR 757 relaxes this restriction for C++0x. */
19017 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
19018 : no_linkage_check (t, /*relaxed_p=*/false));
19019
19020 if (nt)
19021 {
19022 /* DR 488 makes use of a type with no linkage cause
19023 type deduction to fail. */
19024 if (complain & tf_error)
19025 {
19026 if (TYPE_UNNAMED_P (nt))
19027 error ("%qT is/uses unnamed type", t);
19028 else
19029 error ("template argument for %qD uses local type %qT",
19030 tmpl, t);
19031 }
19032 return true;
19033 }
19034 /* In order to avoid all sorts of complications, we do not
19035 allow variably-modified types as template arguments. */
19036 else if (variably_modified_type_p (t, NULL_TREE))
19037 {
19038 if (complain & tf_error)
19039 error ("%qT is a variably modified type", t);
19040 return true;
19041 }
19042 }
19043 /* Class template and alias template arguments should be OK. */
19044 else if (DECL_TYPE_TEMPLATE_P (t))
19045 ;
19046 /* A non-type argument of integral or enumerated type must be a
19047 constant. */
19048 else if (TREE_TYPE (t)
19049 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
19050 && !REFERENCE_REF_P (t)
19051 && !TREE_CONSTANT (t))
19052 {
19053 if (complain & tf_error)
19054 error ("integral expression %qE is not constant", t);
19055 return true;
19056 }
19057 return false;
19058 }
19059
19060 static bool
19061 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
19062 {
19063 int ix, len = DECL_NTPARMS (tmpl);
19064 bool result = false;
19065
19066 for (ix = 0; ix != len; ix++)
19067 {
19068 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
19069 result = true;
19070 }
19071 if (result && (complain & tf_error))
19072 error (" trying to instantiate %qD", tmpl);
19073 return result;
19074 }
19075
19076 /* We're out of SFINAE context now, so generate diagnostics for the access
19077 errors we saw earlier when instantiating D from TMPL and ARGS. */
19078
19079 static void
19080 recheck_decl_substitution (tree d, tree tmpl, tree args)
19081 {
19082 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
19083 tree type = TREE_TYPE (pattern);
19084 location_t loc = input_location;
19085
19086 push_access_scope (d);
19087 push_deferring_access_checks (dk_no_deferred);
19088 input_location = DECL_SOURCE_LOCATION (pattern);
19089 tsubst (type, args, tf_warning_or_error, d);
19090 input_location = loc;
19091 pop_deferring_access_checks ();
19092 pop_access_scope (d);
19093 }
19094
19095 /* Instantiate the indicated variable, function, or alias template TMPL with
19096 the template arguments in TARG_PTR. */
19097
19098 static tree
19099 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
19100 {
19101 tree targ_ptr = orig_args;
19102 tree fndecl;
19103 tree gen_tmpl;
19104 tree spec;
19105 bool access_ok = true;
19106
19107 if (tmpl == error_mark_node)
19108 return error_mark_node;
19109
19110 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
19111
19112 /* If this function is a clone, handle it specially. */
19113 if (DECL_CLONED_FUNCTION_P (tmpl))
19114 {
19115 tree spec;
19116 tree clone;
19117
19118 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19119 DECL_CLONED_FUNCTION. */
19120 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
19121 targ_ptr, complain);
19122 if (spec == error_mark_node)
19123 return error_mark_node;
19124
19125 /* Look for the clone. */
19126 FOR_EACH_CLONE (clone, spec)
19127 if (DECL_NAME (clone) == DECL_NAME (tmpl))
19128 return clone;
19129 /* We should always have found the clone by now. */
19130 gcc_unreachable ();
19131 return NULL_TREE;
19132 }
19133
19134 if (targ_ptr == error_mark_node)
19135 return error_mark_node;
19136
19137 /* Check to see if we already have this specialization. */
19138 gen_tmpl = most_general_template (tmpl);
19139 if (TMPL_ARGS_DEPTH (targ_ptr)
19140 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
19141 /* targ_ptr only has the innermost template args, so add the outer ones
19142 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19143 the case of a non-dependent call within a template definition). */
19144 targ_ptr = (add_outermost_template_args
19145 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
19146 targ_ptr));
19147
19148 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19149 but it doesn't seem to be on the hot path. */
19150 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
19151
19152 gcc_assert (tmpl == gen_tmpl
19153 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
19154 == spec)
19155 || fndecl == NULL_TREE);
19156
19157 if (spec != NULL_TREE)
19158 {
19159 if (FNDECL_HAS_ACCESS_ERRORS (spec))
19160 {
19161 if (complain & tf_error)
19162 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
19163 return error_mark_node;
19164 }
19165 return spec;
19166 }
19167
19168 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
19169 complain))
19170 return error_mark_node;
19171
19172 /* We are building a FUNCTION_DECL, during which the access of its
19173 parameters and return types have to be checked. However this
19174 FUNCTION_DECL which is the desired context for access checking
19175 is not built yet. We solve this chicken-and-egg problem by
19176 deferring all checks until we have the FUNCTION_DECL. */
19177 push_deferring_access_checks (dk_deferred);
19178
19179 /* Instantiation of the function happens in the context of the function
19180 template, not the context of the overload resolution we're doing. */
19181 push_to_top_level ();
19182 /* If there are dependent arguments, e.g. because we're doing partial
19183 ordering, make sure processing_template_decl stays set. */
19184 if (uses_template_parms (targ_ptr))
19185 ++processing_template_decl;
19186 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19187 {
19188 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
19189 complain, gen_tmpl, true);
19190 push_nested_class (ctx);
19191 }
19192
19193 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
19194
19195 fndecl = NULL_TREE;
19196 if (VAR_P (pattern))
19197 {
19198 /* We need to determine if we're using a partial or explicit
19199 specialization now, because the type of the variable could be
19200 different. */
19201 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
19202 tree elt = most_specialized_partial_spec (tid, complain);
19203 if (elt == error_mark_node)
19204 pattern = error_mark_node;
19205 else if (elt)
19206 {
19207 tree partial_tmpl = TREE_VALUE (elt);
19208 tree partial_args = TREE_PURPOSE (elt);
19209 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
19210 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
19211 }
19212 }
19213
19214 /* Substitute template parameters to obtain the specialization. */
19215 if (fndecl == NULL_TREE)
19216 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
19217 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19218 pop_nested_class ();
19219 pop_from_top_level ();
19220
19221 if (fndecl == error_mark_node)
19222 {
19223 pop_deferring_access_checks ();
19224 return error_mark_node;
19225 }
19226
19227 /* The DECL_TI_TEMPLATE should always be the immediate parent
19228 template, not the most general template. */
19229 DECL_TI_TEMPLATE (fndecl) = tmpl;
19230 DECL_TI_ARGS (fndecl) = targ_ptr;
19231
19232 /* Now we know the specialization, compute access previously
19233 deferred. Do no access control for inheriting constructors,
19234 as we already checked access for the inherited constructor. */
19235 if (!(flag_new_inheriting_ctors
19236 && DECL_INHERITED_CTOR (fndecl)))
19237 {
19238 push_access_scope (fndecl);
19239 if (!perform_deferred_access_checks (complain))
19240 access_ok = false;
19241 pop_access_scope (fndecl);
19242 }
19243 pop_deferring_access_checks ();
19244
19245 /* If we've just instantiated the main entry point for a function,
19246 instantiate all the alternate entry points as well. We do this
19247 by cloning the instantiation of the main entry point, not by
19248 instantiating the template clones. */
19249 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
19250 clone_function_decl (fndecl, /*update_methods=*/false);
19251
19252 if (!access_ok)
19253 {
19254 if (!(complain & tf_error))
19255 {
19256 /* Remember to reinstantiate when we're out of SFINAE so the user
19257 can see the errors. */
19258 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
19259 }
19260 return error_mark_node;
19261 }
19262 return fndecl;
19263 }
19264
19265 /* Wrapper for instantiate_template_1. */
19266
19267 tree
19268 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
19269 {
19270 tree ret;
19271 timevar_push (TV_TEMPLATE_INST);
19272 ret = instantiate_template_1 (tmpl, orig_args, complain);
19273 timevar_pop (TV_TEMPLATE_INST);
19274 return ret;
19275 }
19276
19277 /* Instantiate the alias template TMPL with ARGS. Also push a template
19278 instantiation level, which instantiate_template doesn't do because
19279 functions and variables have sufficient context established by the
19280 callers. */
19281
19282 static tree
19283 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
19284 {
19285 if (tmpl == error_mark_node || args == error_mark_node)
19286 return error_mark_node;
19287 if (!push_tinst_level (tmpl, args))
19288 return error_mark_node;
19289
19290 args =
19291 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
19292 args, tmpl, complain,
19293 /*require_all_args=*/true,
19294 /*use_default_args=*/true);
19295
19296 tree r = instantiate_template (tmpl, args, complain);
19297 pop_tinst_level ();
19298
19299 return r;
19300 }
19301
19302 /* PARM is a template parameter pack for FN. Returns true iff
19303 PARM is used in a deducible way in the argument list of FN. */
19304
19305 static bool
19306 pack_deducible_p (tree parm, tree fn)
19307 {
19308 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
19309 for (; t; t = TREE_CHAIN (t))
19310 {
19311 tree type = TREE_VALUE (t);
19312 tree packs;
19313 if (!PACK_EXPANSION_P (type))
19314 continue;
19315 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
19316 packs; packs = TREE_CHAIN (packs))
19317 if (template_args_equal (TREE_VALUE (packs), parm))
19318 {
19319 /* The template parameter pack is used in a function parameter
19320 pack. If this is the end of the parameter list, the
19321 template parameter pack is deducible. */
19322 if (TREE_CHAIN (t) == void_list_node)
19323 return true;
19324 else
19325 /* Otherwise, not. Well, it could be deduced from
19326 a non-pack parameter, but doing so would end up with
19327 a deduction mismatch, so don't bother. */
19328 return false;
19329 }
19330 }
19331 /* The template parameter pack isn't used in any function parameter
19332 packs, but it might be used deeper, e.g. tuple<Args...>. */
19333 return true;
19334 }
19335
19336 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
19337 NARGS elements of the arguments that are being used when calling
19338 it. TARGS is a vector into which the deduced template arguments
19339 are placed.
19340
19341 Returns either a FUNCTION_DECL for the matching specialization of FN or
19342 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
19343 true, diagnostics will be printed to explain why it failed.
19344
19345 If FN is a conversion operator, or we are trying to produce a specific
19346 specialization, RETURN_TYPE is the return type desired.
19347
19348 The EXPLICIT_TARGS are explicit template arguments provided via a
19349 template-id.
19350
19351 The parameter STRICT is one of:
19352
19353 DEDUCE_CALL:
19354 We are deducing arguments for a function call, as in
19355 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
19356 deducing arguments for a call to the result of a conversion
19357 function template, as in [over.call.object].
19358
19359 DEDUCE_CONV:
19360 We are deducing arguments for a conversion function, as in
19361 [temp.deduct.conv].
19362
19363 DEDUCE_EXACT:
19364 We are deducing arguments when doing an explicit instantiation
19365 as in [temp.explicit], when determining an explicit specialization
19366 as in [temp.expl.spec], or when taking the address of a function
19367 template, as in [temp.deduct.funcaddr]. */
19368
19369 tree
19370 fn_type_unification (tree fn,
19371 tree explicit_targs,
19372 tree targs,
19373 const tree *args,
19374 unsigned int nargs,
19375 tree return_type,
19376 unification_kind_t strict,
19377 int flags,
19378 bool explain_p,
19379 bool decltype_p)
19380 {
19381 tree parms;
19382 tree fntype;
19383 tree decl = NULL_TREE;
19384 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
19385 bool ok;
19386 static int deduction_depth;
19387
19388 tree orig_fn = fn;
19389 if (flag_new_inheriting_ctors)
19390 fn = strip_inheriting_ctors (fn);
19391
19392 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
19393 tree r = error_mark_node;
19394
19395 tree full_targs = targs;
19396 if (TMPL_ARGS_DEPTH (targs)
19397 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
19398 full_targs = (add_outermost_template_args
19399 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
19400 targs));
19401
19402 if (decltype_p)
19403 complain |= tf_decltype;
19404
19405 /* In C++0x, it's possible to have a function template whose type depends
19406 on itself recursively. This is most obvious with decltype, but can also
19407 occur with enumeration scope (c++/48969). So we need to catch infinite
19408 recursion and reject the substitution at deduction time; this function
19409 will return error_mark_node for any repeated substitution.
19410
19411 This also catches excessive recursion such as when f<N> depends on
19412 f<N-1> across all integers, and returns error_mark_node for all the
19413 substitutions back up to the initial one.
19414
19415 This is, of course, not reentrant. */
19416 if (excessive_deduction_depth)
19417 return error_mark_node;
19418 ++deduction_depth;
19419
19420 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
19421
19422 fntype = TREE_TYPE (fn);
19423 if (explicit_targs)
19424 {
19425 /* [temp.deduct]
19426
19427 The specified template arguments must match the template
19428 parameters in kind (i.e., type, nontype, template), and there
19429 must not be more arguments than there are parameters;
19430 otherwise type deduction fails.
19431
19432 Nontype arguments must match the types of the corresponding
19433 nontype template parameters, or must be convertible to the
19434 types of the corresponding nontype parameters as specified in
19435 _temp.arg.nontype_, otherwise type deduction fails.
19436
19437 All references in the function type of the function template
19438 to the corresponding template parameters are replaced by the
19439 specified template argument values. If a substitution in a
19440 template parameter or in the function type of the function
19441 template results in an invalid type, type deduction fails. */
19442 int i, len = TREE_VEC_LENGTH (tparms);
19443 location_t loc = input_location;
19444 bool incomplete = false;
19445
19446 if (explicit_targs == error_mark_node)
19447 goto fail;
19448
19449 if (TMPL_ARGS_DEPTH (explicit_targs)
19450 < TMPL_ARGS_DEPTH (full_targs))
19451 explicit_targs = add_outermost_template_args (full_targs,
19452 explicit_targs);
19453
19454 /* Adjust any explicit template arguments before entering the
19455 substitution context. */
19456 explicit_targs
19457 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
19458 complain,
19459 /*require_all_args=*/false,
19460 /*use_default_args=*/false));
19461 if (explicit_targs == error_mark_node)
19462 goto fail;
19463
19464 /* Substitute the explicit args into the function type. This is
19465 necessary so that, for instance, explicitly declared function
19466 arguments can match null pointed constants. If we were given
19467 an incomplete set of explicit args, we must not do semantic
19468 processing during substitution as we could create partial
19469 instantiations. */
19470 for (i = 0; i < len; i++)
19471 {
19472 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
19473 bool parameter_pack = false;
19474 tree targ = TREE_VEC_ELT (explicit_targs, i);
19475
19476 /* Dig out the actual parm. */
19477 if (TREE_CODE (parm) == TYPE_DECL
19478 || TREE_CODE (parm) == TEMPLATE_DECL)
19479 {
19480 parm = TREE_TYPE (parm);
19481 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
19482 }
19483 else if (TREE_CODE (parm) == PARM_DECL)
19484 {
19485 parm = DECL_INITIAL (parm);
19486 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
19487 }
19488
19489 if (!parameter_pack && targ == NULL_TREE)
19490 /* No explicit argument for this template parameter. */
19491 incomplete = true;
19492
19493 if (parameter_pack && pack_deducible_p (parm, fn))
19494 {
19495 /* Mark the argument pack as "incomplete". We could
19496 still deduce more arguments during unification.
19497 We remove this mark in type_unification_real. */
19498 if (targ)
19499 {
19500 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
19501 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
19502 = ARGUMENT_PACK_ARGS (targ);
19503 }
19504
19505 /* We have some incomplete argument packs. */
19506 incomplete = true;
19507 }
19508 }
19509
19510 if (!push_tinst_level (fn, explicit_targs))
19511 {
19512 excessive_deduction_depth = true;
19513 goto fail;
19514 }
19515 processing_template_decl += incomplete;
19516 input_location = DECL_SOURCE_LOCATION (fn);
19517 /* Ignore any access checks; we'll see them again in
19518 instantiate_template and they might have the wrong
19519 access path at this point. */
19520 push_deferring_access_checks (dk_deferred);
19521 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
19522 complain | tf_partial | tf_fndecl_type, NULL_TREE);
19523 pop_deferring_access_checks ();
19524 input_location = loc;
19525 processing_template_decl -= incomplete;
19526 pop_tinst_level ();
19527
19528 if (fntype == error_mark_node)
19529 goto fail;
19530
19531 /* Place the explicitly specified arguments in TARGS. */
19532 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
19533 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
19534 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
19535 }
19536
19537 /* Never do unification on the 'this' parameter. */
19538 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
19539
19540 if (return_type && strict == DEDUCE_CALL)
19541 {
19542 /* We're deducing for a call to the result of a template conversion
19543 function. The parms we really want are in return_type. */
19544 if (POINTER_TYPE_P (return_type))
19545 return_type = TREE_TYPE (return_type);
19546 parms = TYPE_ARG_TYPES (return_type);
19547 }
19548 else if (return_type)
19549 {
19550 tree *new_args;
19551
19552 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
19553 new_args = XALLOCAVEC (tree, nargs + 1);
19554 new_args[0] = return_type;
19555 memcpy (new_args + 1, args, nargs * sizeof (tree));
19556 args = new_args;
19557 ++nargs;
19558 }
19559
19560 /* We allow incomplete unification without an error message here
19561 because the standard doesn't seem to explicitly prohibit it. Our
19562 callers must be ready to deal with unification failures in any
19563 event. */
19564
19565 /* If we aren't explaining yet, push tinst context so we can see where
19566 any errors (e.g. from class instantiations triggered by instantiation
19567 of default template arguments) come from. If we are explaining, this
19568 context is redundant. */
19569 if (!explain_p && !push_tinst_level (fn, targs))
19570 {
19571 excessive_deduction_depth = true;
19572 goto fail;
19573 }
19574
19575 /* type_unification_real will pass back any access checks from default
19576 template argument substitution. */
19577 vec<deferred_access_check, va_gc> *checks;
19578 checks = NULL;
19579
19580 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19581 full_targs, parms, args, nargs, /*subr=*/0,
19582 strict, flags, &checks, explain_p);
19583 if (!explain_p)
19584 pop_tinst_level ();
19585 if (!ok)
19586 goto fail;
19587
19588 /* Now that we have bindings for all of the template arguments,
19589 ensure that the arguments deduced for the template template
19590 parameters have compatible template parameter lists. We cannot
19591 check this property before we have deduced all template
19592 arguments, because the template parameter types of a template
19593 template parameter might depend on prior template parameters
19594 deduced after the template template parameter. The following
19595 ill-formed example illustrates this issue:
19596
19597 template<typename T, template<T> class C> void f(C<5>, T);
19598
19599 template<int N> struct X {};
19600
19601 void g() {
19602 f(X<5>(), 5l); // error: template argument deduction fails
19603 }
19604
19605 The template parameter list of 'C' depends on the template type
19606 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19607 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19608 time that we deduce 'C'. */
19609 if (!template_template_parm_bindings_ok_p
19610 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19611 {
19612 unify_inconsistent_template_template_parameters (explain_p);
19613 goto fail;
19614 }
19615
19616 /* All is well so far. Now, check:
19617
19618 [temp.deduct]
19619
19620 When all template arguments have been deduced, all uses of
19621 template parameters in nondeduced contexts are replaced with
19622 the corresponding deduced argument values. If the
19623 substitution results in an invalid type, as described above,
19624 type deduction fails. */
19625 if (!push_tinst_level (fn, targs))
19626 {
19627 excessive_deduction_depth = true;
19628 goto fail;
19629 }
19630
19631 /* Also collect access checks from the instantiation. */
19632 reopen_deferring_access_checks (checks);
19633
19634 decl = instantiate_template (fn, targs, complain);
19635
19636 checks = get_deferred_access_checks ();
19637 pop_deferring_access_checks ();
19638
19639 pop_tinst_level ();
19640
19641 if (decl == error_mark_node)
19642 goto fail;
19643
19644 /* Now perform any access checks encountered during substitution. */
19645 push_access_scope (decl);
19646 ok = perform_access_checks (checks, complain);
19647 pop_access_scope (decl);
19648 if (!ok)
19649 goto fail;
19650
19651 /* If we're looking for an exact match, check that what we got
19652 is indeed an exact match. It might not be if some template
19653 parameters are used in non-deduced contexts. But don't check
19654 for an exact match if we have dependent template arguments;
19655 in that case we're doing partial ordering, and we already know
19656 that we have two candidates that will provide the actual type. */
19657 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19658 {
19659 tree substed = TREE_TYPE (decl);
19660 unsigned int i;
19661
19662 tree sarg
19663 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19664 if (return_type)
19665 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19666 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19667 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19668 {
19669 unify_type_mismatch (explain_p, args[i],
19670 TREE_VALUE (sarg));
19671 goto fail;
19672 }
19673 }
19674
19675 /* After doing deduction with the inherited constructor, actually return an
19676 instantiation of the inheriting constructor. */
19677 if (orig_fn != fn)
19678 decl = instantiate_template (orig_fn, targs, complain);
19679
19680 r = decl;
19681
19682 fail:
19683 --deduction_depth;
19684 if (excessive_deduction_depth)
19685 {
19686 if (deduction_depth == 0)
19687 /* Reset once we're all the way out. */
19688 excessive_deduction_depth = false;
19689 }
19690
19691 return r;
19692 }
19693
19694 /* Adjust types before performing type deduction, as described in
19695 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19696 sections are symmetric. PARM is the type of a function parameter
19697 or the return type of the conversion function. ARG is the type of
19698 the argument passed to the call, or the type of the value
19699 initialized with the result of the conversion function.
19700 ARG_EXPR is the original argument expression, which may be null. */
19701
19702 static int
19703 maybe_adjust_types_for_deduction (unification_kind_t strict,
19704 tree* parm,
19705 tree* arg,
19706 tree arg_expr)
19707 {
19708 int result = 0;
19709
19710 switch (strict)
19711 {
19712 case DEDUCE_CALL:
19713 break;
19714
19715 case DEDUCE_CONV:
19716 /* Swap PARM and ARG throughout the remainder of this
19717 function; the handling is precisely symmetric since PARM
19718 will initialize ARG rather than vice versa. */
19719 std::swap (parm, arg);
19720 break;
19721
19722 case DEDUCE_EXACT:
19723 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19724 too, but here handle it by stripping the reference from PARM
19725 rather than by adding it to ARG. */
19726 if (TYPE_REF_P (*parm)
19727 && TYPE_REF_IS_RVALUE (*parm)
19728 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19729 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19730 && TYPE_REF_P (*arg)
19731 && !TYPE_REF_IS_RVALUE (*arg))
19732 *parm = TREE_TYPE (*parm);
19733 /* Nothing else to do in this case. */
19734 return 0;
19735
19736 default:
19737 gcc_unreachable ();
19738 }
19739
19740 if (!TYPE_REF_P (*parm))
19741 {
19742 /* [temp.deduct.call]
19743
19744 If P is not a reference type:
19745
19746 --If A is an array type, the pointer type produced by the
19747 array-to-pointer standard conversion (_conv.array_) is
19748 used in place of A for type deduction; otherwise,
19749
19750 --If A is a function type, the pointer type produced by
19751 the function-to-pointer standard conversion
19752 (_conv.func_) is used in place of A for type deduction;
19753 otherwise,
19754
19755 --If A is a cv-qualified type, the top level
19756 cv-qualifiers of A's type are ignored for type
19757 deduction. */
19758 if (TREE_CODE (*arg) == ARRAY_TYPE)
19759 *arg = build_pointer_type (TREE_TYPE (*arg));
19760 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19761 *arg = build_pointer_type (*arg);
19762 else
19763 *arg = TYPE_MAIN_VARIANT (*arg);
19764 }
19765
19766 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19767 reference to a cv-unqualified template parameter that does not represent a
19768 template parameter of a class template (during class template argument
19769 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19770 an lvalue, the type "lvalue reference to A" is used in place of A for type
19771 deduction. */
19772 if (TYPE_REF_P (*parm)
19773 && TYPE_REF_IS_RVALUE (*parm)
19774 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19775 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19776 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19777 && (arg_expr ? lvalue_p (arg_expr)
19778 /* try_one_overload doesn't provide an arg_expr, but
19779 functions are always lvalues. */
19780 : TREE_CODE (*arg) == FUNCTION_TYPE))
19781 *arg = build_reference_type (*arg);
19782
19783 /* [temp.deduct.call]
19784
19785 If P is a cv-qualified type, the top level cv-qualifiers
19786 of P's type are ignored for type deduction. If P is a
19787 reference type, the type referred to by P is used for
19788 type deduction. */
19789 *parm = TYPE_MAIN_VARIANT (*parm);
19790 if (TYPE_REF_P (*parm))
19791 {
19792 *parm = TREE_TYPE (*parm);
19793 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19794 }
19795
19796 /* DR 322. For conversion deduction, remove a reference type on parm
19797 too (which has been swapped into ARG). */
19798 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
19799 *arg = TREE_TYPE (*arg);
19800
19801 return result;
19802 }
19803
19804 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19805 template which does contain any deducible template parameters; check if
19806 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19807 unify_one_argument. */
19808
19809 static int
19810 check_non_deducible_conversion (tree parm, tree arg, int strict,
19811 int flags, bool explain_p)
19812 {
19813 tree type;
19814
19815 if (!TYPE_P (arg))
19816 type = TREE_TYPE (arg);
19817 else
19818 type = arg;
19819
19820 if (same_type_p (parm, type))
19821 return unify_success (explain_p);
19822
19823 if (strict == DEDUCE_CONV)
19824 {
19825 if (can_convert_arg (type, parm, NULL_TREE, flags,
19826 explain_p ? tf_warning_or_error : tf_none))
19827 return unify_success (explain_p);
19828 }
19829 else if (strict != DEDUCE_EXACT)
19830 {
19831 if (can_convert_arg (parm, type,
19832 TYPE_P (arg) ? NULL_TREE : arg,
19833 flags, explain_p ? tf_warning_or_error : tf_none))
19834 return unify_success (explain_p);
19835 }
19836
19837 if (strict == DEDUCE_EXACT)
19838 return unify_type_mismatch (explain_p, parm, arg);
19839 else
19840 return unify_arg_conversion (explain_p, parm, type, arg);
19841 }
19842
19843 static bool uses_deducible_template_parms (tree type);
19844
19845 /* Returns true iff the expression EXPR is one from which a template
19846 argument can be deduced. In other words, if it's an undecorated
19847 use of a template non-type parameter. */
19848
19849 static bool
19850 deducible_expression (tree expr)
19851 {
19852 /* Strip implicit conversions. */
19853 while (CONVERT_EXPR_P (expr))
19854 expr = TREE_OPERAND (expr, 0);
19855 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19856 }
19857
19858 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19859 deducible way; that is, if it has a max value of <PARM> - 1. */
19860
19861 static bool
19862 deducible_array_bound (tree domain)
19863 {
19864 if (domain == NULL_TREE)
19865 return false;
19866
19867 tree max = TYPE_MAX_VALUE (domain);
19868 if (TREE_CODE (max) != MINUS_EXPR)
19869 return false;
19870
19871 return deducible_expression (TREE_OPERAND (max, 0));
19872 }
19873
19874 /* Returns true iff the template arguments ARGS use a template parameter
19875 in a deducible way. */
19876
19877 static bool
19878 deducible_template_args (tree args)
19879 {
19880 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19881 {
19882 bool deducible;
19883 tree elt = TREE_VEC_ELT (args, i);
19884 if (ARGUMENT_PACK_P (elt))
19885 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19886 else
19887 {
19888 if (PACK_EXPANSION_P (elt))
19889 elt = PACK_EXPANSION_PATTERN (elt);
19890 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19891 deducible = true;
19892 else if (TYPE_P (elt))
19893 deducible = uses_deducible_template_parms (elt);
19894 else
19895 deducible = deducible_expression (elt);
19896 }
19897 if (deducible)
19898 return true;
19899 }
19900 return false;
19901 }
19902
19903 /* Returns true iff TYPE contains any deducible references to template
19904 parameters, as per 14.8.2.5. */
19905
19906 static bool
19907 uses_deducible_template_parms (tree type)
19908 {
19909 if (PACK_EXPANSION_P (type))
19910 type = PACK_EXPANSION_PATTERN (type);
19911
19912 /* T
19913 cv-list T
19914 TT<T>
19915 TT<i>
19916 TT<> */
19917 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19918 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19919 return true;
19920
19921 /* T*
19922 T&
19923 T&& */
19924 if (POINTER_TYPE_P (type))
19925 return uses_deducible_template_parms (TREE_TYPE (type));
19926
19927 /* T[integer-constant ]
19928 type [i] */
19929 if (TREE_CODE (type) == ARRAY_TYPE)
19930 return (uses_deducible_template_parms (TREE_TYPE (type))
19931 || deducible_array_bound (TYPE_DOMAIN (type)));
19932
19933 /* T type ::*
19934 type T::*
19935 T T::*
19936 T (type ::*)()
19937 type (T::*)()
19938 type (type ::*)(T)
19939 type (T::*)(T)
19940 T (type ::*)(T)
19941 T (T::*)()
19942 T (T::*)(T) */
19943 if (TYPE_PTRMEM_P (type))
19944 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19945 || (uses_deducible_template_parms
19946 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19947
19948 /* template-name <T> (where template-name refers to a class template)
19949 template-name <i> (where template-name refers to a class template) */
19950 if (CLASS_TYPE_P (type)
19951 && CLASSTYPE_TEMPLATE_INFO (type)
19952 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19953 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19954 (CLASSTYPE_TI_ARGS (type)));
19955
19956 /* type (T)
19957 T()
19958 T(T) */
19959 if (TREE_CODE (type) == FUNCTION_TYPE
19960 || TREE_CODE (type) == METHOD_TYPE)
19961 {
19962 if (uses_deducible_template_parms (TREE_TYPE (type)))
19963 return true;
19964 tree parm = TYPE_ARG_TYPES (type);
19965 if (TREE_CODE (type) == METHOD_TYPE)
19966 parm = TREE_CHAIN (parm);
19967 for (; parm; parm = TREE_CHAIN (parm))
19968 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19969 return true;
19970 }
19971
19972 return false;
19973 }
19974
19975 /* Subroutine of type_unification_real and unify_pack_expansion to
19976 handle unification of a single P/A pair. Parameters are as
19977 for those functions. */
19978
19979 static int
19980 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19981 int subr, unification_kind_t strict,
19982 bool explain_p)
19983 {
19984 tree arg_expr = NULL_TREE;
19985 int arg_strict;
19986
19987 if (arg == error_mark_node || parm == error_mark_node)
19988 return unify_invalid (explain_p);
19989 if (arg == unknown_type_node)
19990 /* We can't deduce anything from this, but we might get all the
19991 template args from other function args. */
19992 return unify_success (explain_p);
19993
19994 /* Implicit conversions (Clause 4) will be performed on a function
19995 argument to convert it to the type of the corresponding function
19996 parameter if the parameter type contains no template-parameters that
19997 participate in template argument deduction. */
19998 if (strict != DEDUCE_EXACT
19999 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
20000 /* For function parameters with no deducible template parameters,
20001 just return. We'll check non-dependent conversions later. */
20002 return unify_success (explain_p);
20003
20004 switch (strict)
20005 {
20006 case DEDUCE_CALL:
20007 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
20008 | UNIFY_ALLOW_MORE_CV_QUAL
20009 | UNIFY_ALLOW_DERIVED);
20010 break;
20011
20012 case DEDUCE_CONV:
20013 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
20014 break;
20015
20016 case DEDUCE_EXACT:
20017 arg_strict = UNIFY_ALLOW_NONE;
20018 break;
20019
20020 default:
20021 gcc_unreachable ();
20022 }
20023
20024 /* We only do these transformations if this is the top-level
20025 parameter_type_list in a call or declaration matching; in other
20026 situations (nested function declarators, template argument lists) we
20027 won't be comparing a type to an expression, and we don't do any type
20028 adjustments. */
20029 if (!subr)
20030 {
20031 if (!TYPE_P (arg))
20032 {
20033 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
20034 if (type_unknown_p (arg))
20035 {
20036 /* [temp.deduct.type] A template-argument can be
20037 deduced from a pointer to function or pointer
20038 to member function argument if the set of
20039 overloaded functions does not contain function
20040 templates and at most one of a set of
20041 overloaded functions provides a unique
20042 match. */
20043 resolve_overloaded_unification (tparms, targs, parm,
20044 arg, strict,
20045 arg_strict, explain_p);
20046 /* If a unique match was not found, this is a
20047 non-deduced context, so we still succeed. */
20048 return unify_success (explain_p);
20049 }
20050
20051 arg_expr = arg;
20052 arg = unlowered_expr_type (arg);
20053 if (arg == error_mark_node)
20054 return unify_invalid (explain_p);
20055 }
20056
20057 arg_strict |=
20058 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
20059 }
20060 else
20061 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
20062 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
20063 return unify_template_argument_mismatch (explain_p, parm, arg);
20064
20065 /* For deduction from an init-list we need the actual list. */
20066 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
20067 arg = arg_expr;
20068 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
20069 }
20070
20071 /* for_each_template_parm callback that always returns 0. */
20072
20073 static int
20074 zero_r (tree, void *)
20075 {
20076 return 0;
20077 }
20078
20079 /* for_each_template_parm any_fn callback to handle deduction of a template
20080 type argument from the type of an array bound. */
20081
20082 static int
20083 array_deduction_r (tree t, void *data)
20084 {
20085 tree_pair_p d = (tree_pair_p)data;
20086 tree &tparms = d->purpose;
20087 tree &targs = d->value;
20088
20089 if (TREE_CODE (t) == ARRAY_TYPE)
20090 if (tree dom = TYPE_DOMAIN (t))
20091 if (tree max = TYPE_MAX_VALUE (dom))
20092 {
20093 if (TREE_CODE (max) == MINUS_EXPR)
20094 max = TREE_OPERAND (max, 0);
20095 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
20096 unify (tparms, targs, TREE_TYPE (max), size_type_node,
20097 UNIFY_ALLOW_NONE, /*explain*/false);
20098 }
20099
20100 /* Keep walking. */
20101 return 0;
20102 }
20103
20104 /* Try to deduce any not-yet-deduced template type arguments from the type of
20105 an array bound. This is handled separately from unify because 14.8.2.5 says
20106 "The type of a type parameter is only deduced from an array bound if it is
20107 not otherwise deduced." */
20108
20109 static void
20110 try_array_deduction (tree tparms, tree targs, tree parm)
20111 {
20112 tree_pair_s data = { tparms, targs };
20113 hash_set<tree> visited;
20114 for_each_template_parm (parm, zero_r, &data, &visited,
20115 /*nondeduced*/false, array_deduction_r);
20116 }
20117
20118 /* Most parms like fn_type_unification.
20119
20120 If SUBR is 1, we're being called recursively (to unify the
20121 arguments of a function or method parameter of a function
20122 template).
20123
20124 CHECKS is a pointer to a vector of access checks encountered while
20125 substituting default template arguments. */
20126
20127 static int
20128 type_unification_real (tree tparms,
20129 tree full_targs,
20130 tree xparms,
20131 const tree *xargs,
20132 unsigned int xnargs,
20133 int subr,
20134 unification_kind_t strict,
20135 int flags,
20136 vec<deferred_access_check, va_gc> **checks,
20137 bool explain_p)
20138 {
20139 tree parm, arg;
20140 int i;
20141 int ntparms = TREE_VEC_LENGTH (tparms);
20142 int saw_undeduced = 0;
20143 tree parms;
20144 const tree *args;
20145 unsigned int nargs;
20146 unsigned int ia;
20147
20148 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
20149 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
20150 gcc_assert (ntparms > 0);
20151
20152 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
20153
20154 /* Reset the number of non-defaulted template arguments contained
20155 in TARGS. */
20156 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
20157
20158 again:
20159 parms = xparms;
20160 args = xargs;
20161 nargs = xnargs;
20162
20163 ia = 0;
20164 while (parms && parms != void_list_node
20165 && ia < nargs)
20166 {
20167 parm = TREE_VALUE (parms);
20168
20169 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20170 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
20171 /* For a function parameter pack that occurs at the end of the
20172 parameter-declaration-list, the type A of each remaining
20173 argument of the call is compared with the type P of the
20174 declarator-id of the function parameter pack. */
20175 break;
20176
20177 parms = TREE_CHAIN (parms);
20178
20179 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20180 /* For a function parameter pack that does not occur at the
20181 end of the parameter-declaration-list, the type of the
20182 parameter pack is a non-deduced context. */
20183 continue;
20184
20185 arg = args[ia];
20186 ++ia;
20187
20188 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
20189 explain_p))
20190 return 1;
20191 }
20192
20193 if (parms
20194 && parms != void_list_node
20195 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
20196 {
20197 /* Unify the remaining arguments with the pack expansion type. */
20198 tree argvec;
20199 tree parmvec = make_tree_vec (1);
20200
20201 /* Allocate a TREE_VEC and copy in all of the arguments */
20202 argvec = make_tree_vec (nargs - ia);
20203 for (i = 0; ia < nargs; ++ia, ++i)
20204 TREE_VEC_ELT (argvec, i) = args[ia];
20205
20206 /* Copy the parameter into parmvec. */
20207 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
20208 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
20209 /*subr=*/subr, explain_p))
20210 return 1;
20211
20212 /* Advance to the end of the list of parameters. */
20213 parms = TREE_CHAIN (parms);
20214 }
20215
20216 /* Fail if we've reached the end of the parm list, and more args
20217 are present, and the parm list isn't variadic. */
20218 if (ia < nargs && parms == void_list_node)
20219 return unify_too_many_arguments (explain_p, nargs, ia);
20220 /* Fail if parms are left and they don't have default values and
20221 they aren't all deduced as empty packs (c++/57397). This is
20222 consistent with sufficient_parms_p. */
20223 if (parms && parms != void_list_node
20224 && TREE_PURPOSE (parms) == NULL_TREE)
20225 {
20226 unsigned int count = nargs;
20227 tree p = parms;
20228 bool type_pack_p;
20229 do
20230 {
20231 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
20232 if (!type_pack_p)
20233 count++;
20234 p = TREE_CHAIN (p);
20235 }
20236 while (p && p != void_list_node);
20237 if (count != nargs)
20238 return unify_too_few_arguments (explain_p, ia, count,
20239 type_pack_p);
20240 }
20241
20242 if (!subr)
20243 {
20244 tsubst_flags_t complain = (explain_p
20245 ? tf_warning_or_error
20246 : tf_none);
20247 bool tried_array_deduction = (cxx_dialect < cxx17);
20248
20249 for (i = 0; i < ntparms; i++)
20250 {
20251 tree targ = TREE_VEC_ELT (targs, i);
20252 tree tparm = TREE_VEC_ELT (tparms, i);
20253
20254 /* Clear the "incomplete" flags on all argument packs now so that
20255 substituting them into later default arguments works. */
20256 if (targ && ARGUMENT_PACK_P (targ))
20257 {
20258 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
20259 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
20260 }
20261
20262 if (targ || tparm == error_mark_node)
20263 continue;
20264 tparm = TREE_VALUE (tparm);
20265
20266 if (TREE_CODE (tparm) == TYPE_DECL
20267 && !tried_array_deduction)
20268 {
20269 try_array_deduction (tparms, targs, xparms);
20270 tried_array_deduction = true;
20271 if (TREE_VEC_ELT (targs, i))
20272 continue;
20273 }
20274
20275 /* If this is an undeduced nontype parameter that depends on
20276 a type parameter, try another pass; its type may have been
20277 deduced from a later argument than the one from which
20278 this parameter can be deduced. */
20279 if (TREE_CODE (tparm) == PARM_DECL
20280 && uses_template_parms (TREE_TYPE (tparm))
20281 && saw_undeduced < 2)
20282 {
20283 saw_undeduced = 1;
20284 continue;
20285 }
20286
20287 /* Core issue #226 (C++0x) [temp.deduct]:
20288
20289 If a template argument has not been deduced, its
20290 default template argument, if any, is used.
20291
20292 When we are in C++98 mode, TREE_PURPOSE will either
20293 be NULL_TREE or ERROR_MARK_NODE, so we do not need
20294 to explicitly check cxx_dialect here. */
20295 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
20296 /* OK, there is a default argument. Wait until after the
20297 conversion check to do substitution. */
20298 continue;
20299
20300 /* If the type parameter is a parameter pack, then it will
20301 be deduced to an empty parameter pack. */
20302 if (template_parameter_pack_p (tparm))
20303 {
20304 tree arg;
20305
20306 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
20307 {
20308 arg = make_node (NONTYPE_ARGUMENT_PACK);
20309 TREE_CONSTANT (arg) = 1;
20310 }
20311 else
20312 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
20313
20314 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
20315
20316 TREE_VEC_ELT (targs, i) = arg;
20317 continue;
20318 }
20319
20320 return unify_parameter_deduction_failure (explain_p, tparm);
20321 }
20322
20323 /* DR 1391: All parameters have args, now check non-dependent parms for
20324 convertibility. */
20325 if (saw_undeduced < 2)
20326 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
20327 parms && parms != void_list_node && ia < nargs; )
20328 {
20329 parm = TREE_VALUE (parms);
20330
20331 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20332 && (!TREE_CHAIN (parms)
20333 || TREE_CHAIN (parms) == void_list_node))
20334 /* For a function parameter pack that occurs at the end of the
20335 parameter-declaration-list, the type A of each remaining
20336 argument of the call is compared with the type P of the
20337 declarator-id of the function parameter pack. */
20338 break;
20339
20340 parms = TREE_CHAIN (parms);
20341
20342 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20343 /* For a function parameter pack that does not occur at the
20344 end of the parameter-declaration-list, the type of the
20345 parameter pack is a non-deduced context. */
20346 continue;
20347
20348 arg = args[ia];
20349 ++ia;
20350
20351 if (uses_template_parms (parm))
20352 continue;
20353 if (check_non_deducible_conversion (parm, arg, strict, flags,
20354 explain_p))
20355 return 1;
20356 }
20357
20358 /* Now substitute into the default template arguments. */
20359 for (i = 0; i < ntparms; i++)
20360 {
20361 tree targ = TREE_VEC_ELT (targs, i);
20362 tree tparm = TREE_VEC_ELT (tparms, i);
20363
20364 if (targ || tparm == error_mark_node)
20365 continue;
20366 tree parm = TREE_VALUE (tparm);
20367 tree arg = TREE_PURPOSE (tparm);
20368 reopen_deferring_access_checks (*checks);
20369 location_t save_loc = input_location;
20370 if (DECL_P (parm))
20371 input_location = DECL_SOURCE_LOCATION (parm);
20372 if (saw_undeduced == 1)
20373 ++processing_template_decl;
20374
20375 if (saw_undeduced == 1
20376 && TREE_CODE (parm) == PARM_DECL
20377 && uses_template_parms (TREE_TYPE (parm)))
20378 {
20379 /* The type of this non-type parameter depends on undeduced
20380 parameters. Don't try to use its default argument yet,
20381 but do check whether the arguments we already have cause
20382 substitution failure, so that that happens before we try
20383 later default arguments (78489). */
20384 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
20385 NULL_TREE);
20386 if (type == error_mark_node)
20387 arg = error_mark_node;
20388 else
20389 arg = NULL_TREE;
20390 }
20391 else
20392 {
20393 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
20394
20395 if (!uses_template_parms (arg))
20396 arg = convert_template_argument (parm, arg, full_targs,
20397 complain, i, NULL_TREE);
20398 else if (saw_undeduced == 1)
20399 arg = NULL_TREE;
20400 else
20401 arg = error_mark_node;
20402 }
20403
20404 if (saw_undeduced == 1)
20405 --processing_template_decl;
20406 input_location = save_loc;
20407 *checks = get_deferred_access_checks ();
20408 pop_deferring_access_checks ();
20409
20410 if (arg == error_mark_node)
20411 return 1;
20412 else if (arg)
20413 {
20414 TREE_VEC_ELT (targs, i) = arg;
20415 /* The position of the first default template argument,
20416 is also the number of non-defaulted arguments in TARGS.
20417 Record that. */
20418 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20419 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
20420 }
20421 }
20422
20423 if (saw_undeduced++ == 1)
20424 goto again;
20425 }
20426
20427 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20428 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
20429
20430 return unify_success (explain_p);
20431 }
20432
20433 /* Subroutine of type_unification_real. Args are like the variables
20434 at the call site. ARG is an overloaded function (or template-id);
20435 we try deducing template args from each of the overloads, and if
20436 only one succeeds, we go with that. Modifies TARGS and returns
20437 true on success. */
20438
20439 static bool
20440 resolve_overloaded_unification (tree tparms,
20441 tree targs,
20442 tree parm,
20443 tree arg,
20444 unification_kind_t strict,
20445 int sub_strict,
20446 bool explain_p)
20447 {
20448 tree tempargs = copy_node (targs);
20449 int good = 0;
20450 tree goodfn = NULL_TREE;
20451 bool addr_p;
20452
20453 if (TREE_CODE (arg) == ADDR_EXPR)
20454 {
20455 arg = TREE_OPERAND (arg, 0);
20456 addr_p = true;
20457 }
20458 else
20459 addr_p = false;
20460
20461 if (TREE_CODE (arg) == COMPONENT_REF)
20462 /* Handle `&x' where `x' is some static or non-static member
20463 function name. */
20464 arg = TREE_OPERAND (arg, 1);
20465
20466 if (TREE_CODE (arg) == OFFSET_REF)
20467 arg = TREE_OPERAND (arg, 1);
20468
20469 /* Strip baselink information. */
20470 if (BASELINK_P (arg))
20471 arg = BASELINK_FUNCTIONS (arg);
20472
20473 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
20474 {
20475 /* If we got some explicit template args, we need to plug them into
20476 the affected templates before we try to unify, in case the
20477 explicit args will completely resolve the templates in question. */
20478
20479 int ok = 0;
20480 tree expl_subargs = TREE_OPERAND (arg, 1);
20481 arg = TREE_OPERAND (arg, 0);
20482
20483 for (lkp_iterator iter (arg); iter; ++iter)
20484 {
20485 tree fn = *iter;
20486 tree subargs, elem;
20487
20488 if (TREE_CODE (fn) != TEMPLATE_DECL)
20489 continue;
20490
20491 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20492 expl_subargs, NULL_TREE, tf_none,
20493 /*require_all_args=*/true,
20494 /*use_default_args=*/true);
20495 if (subargs != error_mark_node
20496 && !any_dependent_template_arguments_p (subargs))
20497 {
20498 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
20499 if (try_one_overload (tparms, targs, tempargs, parm,
20500 elem, strict, sub_strict, addr_p, explain_p)
20501 && (!goodfn || !same_type_p (goodfn, elem)))
20502 {
20503 goodfn = elem;
20504 ++good;
20505 }
20506 }
20507 else if (subargs)
20508 ++ok;
20509 }
20510 /* If no templates (or more than one) are fully resolved by the
20511 explicit arguments, this template-id is a non-deduced context; it
20512 could still be OK if we deduce all template arguments for the
20513 enclosing call through other arguments. */
20514 if (good != 1)
20515 good = ok;
20516 }
20517 else if (TREE_CODE (arg) != OVERLOAD
20518 && TREE_CODE (arg) != FUNCTION_DECL)
20519 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20520 -- but the deduction does not succeed because the expression is
20521 not just the function on its own. */
20522 return false;
20523 else
20524 for (lkp_iterator iter (arg); iter; ++iter)
20525 {
20526 tree fn = *iter;
20527 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
20528 strict, sub_strict, addr_p, explain_p)
20529 && (!goodfn || !decls_match (goodfn, fn)))
20530 {
20531 goodfn = fn;
20532 ++good;
20533 }
20534 }
20535
20536 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20537 to function or pointer to member function argument if the set of
20538 overloaded functions does not contain function templates and at most
20539 one of a set of overloaded functions provides a unique match.
20540
20541 So if we found multiple possibilities, we return success but don't
20542 deduce anything. */
20543
20544 if (good == 1)
20545 {
20546 int i = TREE_VEC_LENGTH (targs);
20547 for (; i--; )
20548 if (TREE_VEC_ELT (tempargs, i))
20549 {
20550 tree old = TREE_VEC_ELT (targs, i);
20551 tree new_ = TREE_VEC_ELT (tempargs, i);
20552 if (new_ && old && ARGUMENT_PACK_P (old)
20553 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
20554 /* Don't forget explicit template arguments in a pack. */
20555 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
20556 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
20557 TREE_VEC_ELT (targs, i) = new_;
20558 }
20559 }
20560 if (good)
20561 return true;
20562
20563 return false;
20564 }
20565
20566 /* Core DR 115: In contexts where deduction is done and fails, or in
20567 contexts where deduction is not done, if a template argument list is
20568 specified and it, along with any default template arguments, identifies
20569 a single function template specialization, then the template-id is an
20570 lvalue for the function template specialization. */
20571
20572 tree
20573 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
20574 {
20575 tree expr, offset, baselink;
20576 bool addr;
20577
20578 if (!type_unknown_p (orig_expr))
20579 return orig_expr;
20580
20581 expr = orig_expr;
20582 addr = false;
20583 offset = NULL_TREE;
20584 baselink = NULL_TREE;
20585
20586 if (TREE_CODE (expr) == ADDR_EXPR)
20587 {
20588 expr = TREE_OPERAND (expr, 0);
20589 addr = true;
20590 }
20591 if (TREE_CODE (expr) == OFFSET_REF)
20592 {
20593 offset = expr;
20594 expr = TREE_OPERAND (expr, 1);
20595 }
20596 if (BASELINK_P (expr))
20597 {
20598 baselink = expr;
20599 expr = BASELINK_FUNCTIONS (expr);
20600 }
20601
20602 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
20603 {
20604 int good = 0;
20605 tree goodfn = NULL_TREE;
20606
20607 /* If we got some explicit template args, we need to plug them into
20608 the affected templates before we try to unify, in case the
20609 explicit args will completely resolve the templates in question. */
20610
20611 tree expl_subargs = TREE_OPERAND (expr, 1);
20612 tree arg = TREE_OPERAND (expr, 0);
20613 tree badfn = NULL_TREE;
20614 tree badargs = NULL_TREE;
20615
20616 for (lkp_iterator iter (arg); iter; ++iter)
20617 {
20618 tree fn = *iter;
20619 tree subargs, elem;
20620
20621 if (TREE_CODE (fn) != TEMPLATE_DECL)
20622 continue;
20623
20624 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20625 expl_subargs, NULL_TREE, tf_none,
20626 /*require_all_args=*/true,
20627 /*use_default_args=*/true);
20628 if (subargs != error_mark_node
20629 && !any_dependent_template_arguments_p (subargs))
20630 {
20631 elem = instantiate_template (fn, subargs, tf_none);
20632 if (elem == error_mark_node)
20633 {
20634 badfn = fn;
20635 badargs = subargs;
20636 }
20637 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20638 {
20639 goodfn = elem;
20640 ++good;
20641 }
20642 }
20643 }
20644 if (good == 1)
20645 {
20646 mark_used (goodfn);
20647 expr = goodfn;
20648 if (baselink)
20649 expr = build_baselink (BASELINK_BINFO (baselink),
20650 BASELINK_ACCESS_BINFO (baselink),
20651 expr, BASELINK_OPTYPE (baselink));
20652 if (offset)
20653 {
20654 tree base
20655 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20656 expr = build_offset_ref (base, expr, addr, complain);
20657 }
20658 if (addr)
20659 expr = cp_build_addr_expr (expr, complain);
20660 return expr;
20661 }
20662 else if (good == 0 && badargs && (complain & tf_error))
20663 /* There were no good options and at least one bad one, so let the
20664 user know what the problem is. */
20665 instantiate_template (badfn, badargs, complain);
20666 }
20667 return orig_expr;
20668 }
20669
20670 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20671 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20672 different overloads deduce different arguments for a given parm.
20673 ADDR_P is true if the expression for which deduction is being
20674 performed was of the form "& fn" rather than simply "fn".
20675
20676 Returns 1 on success. */
20677
20678 static int
20679 try_one_overload (tree tparms,
20680 tree orig_targs,
20681 tree targs,
20682 tree parm,
20683 tree arg,
20684 unification_kind_t strict,
20685 int sub_strict,
20686 bool addr_p,
20687 bool explain_p)
20688 {
20689 int nargs;
20690 tree tempargs;
20691 int i;
20692
20693 if (arg == error_mark_node)
20694 return 0;
20695
20696 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20697 to function or pointer to member function argument if the set of
20698 overloaded functions does not contain function templates and at most
20699 one of a set of overloaded functions provides a unique match.
20700
20701 So if this is a template, just return success. */
20702
20703 if (uses_template_parms (arg))
20704 return 1;
20705
20706 if (TREE_CODE (arg) == METHOD_TYPE)
20707 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20708 else if (addr_p)
20709 arg = build_pointer_type (arg);
20710
20711 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20712
20713 /* We don't copy orig_targs for this because if we have already deduced
20714 some template args from previous args, unify would complain when we
20715 try to deduce a template parameter for the same argument, even though
20716 there isn't really a conflict. */
20717 nargs = TREE_VEC_LENGTH (targs);
20718 tempargs = make_tree_vec (nargs);
20719
20720 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20721 return 0;
20722
20723 /* First make sure we didn't deduce anything that conflicts with
20724 explicitly specified args. */
20725 for (i = nargs; i--; )
20726 {
20727 tree elt = TREE_VEC_ELT (tempargs, i);
20728 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20729
20730 if (!elt)
20731 /*NOP*/;
20732 else if (uses_template_parms (elt))
20733 /* Since we're unifying against ourselves, we will fill in
20734 template args used in the function parm list with our own
20735 template parms. Discard them. */
20736 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20737 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20738 {
20739 /* Check that the argument at each index of the deduced argument pack
20740 is equivalent to the corresponding explicitly specified argument.
20741 We may have deduced more arguments than were explicitly specified,
20742 and that's OK. */
20743
20744 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20745 that's wrong if we deduce the same argument pack from multiple
20746 function arguments: it's only incomplete the first time. */
20747
20748 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20749 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20750
20751 if (TREE_VEC_LENGTH (deduced_pack)
20752 < TREE_VEC_LENGTH (explicit_pack))
20753 return 0;
20754
20755 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20756 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20757 TREE_VEC_ELT (deduced_pack, j)))
20758 return 0;
20759 }
20760 else if (oldelt && !template_args_equal (oldelt, elt))
20761 return 0;
20762 }
20763
20764 for (i = nargs; i--; )
20765 {
20766 tree elt = TREE_VEC_ELT (tempargs, i);
20767
20768 if (elt)
20769 TREE_VEC_ELT (targs, i) = elt;
20770 }
20771
20772 return 1;
20773 }
20774
20775 /* PARM is a template class (perhaps with unbound template
20776 parameters). ARG is a fully instantiated type. If ARG can be
20777 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20778 TARGS are as for unify. */
20779
20780 static tree
20781 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20782 bool explain_p)
20783 {
20784 tree copy_of_targs;
20785
20786 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20787 return NULL_TREE;
20788 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20789 /* Matches anything. */;
20790 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20791 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20792 return NULL_TREE;
20793
20794 /* We need to make a new template argument vector for the call to
20795 unify. If we used TARGS, we'd clutter it up with the result of
20796 the attempted unification, even if this class didn't work out.
20797 We also don't want to commit ourselves to all the unifications
20798 we've already done, since unification is supposed to be done on
20799 an argument-by-argument basis. In other words, consider the
20800 following pathological case:
20801
20802 template <int I, int J, int K>
20803 struct S {};
20804
20805 template <int I, int J>
20806 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20807
20808 template <int I, int J, int K>
20809 void f(S<I, J, K>, S<I, I, I>);
20810
20811 void g() {
20812 S<0, 0, 0> s0;
20813 S<0, 1, 2> s2;
20814
20815 f(s0, s2);
20816 }
20817
20818 Now, by the time we consider the unification involving `s2', we
20819 already know that we must have `f<0, 0, 0>'. But, even though
20820 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20821 because there are two ways to unify base classes of S<0, 1, 2>
20822 with S<I, I, I>. If we kept the already deduced knowledge, we
20823 would reject the possibility I=1. */
20824 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20825
20826 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20827 {
20828 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20829 return NULL_TREE;
20830 return arg;
20831 }
20832
20833 /* If unification failed, we're done. */
20834 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20835 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20836 return NULL_TREE;
20837
20838 return arg;
20839 }
20840
20841 /* Given a template type PARM and a class type ARG, find the unique
20842 base type in ARG that is an instance of PARM. We do not examine
20843 ARG itself; only its base-classes. If there is not exactly one
20844 appropriate base class, return NULL_TREE. PARM may be the type of
20845 a partial specialization, as well as a plain template type. Used
20846 by unify. */
20847
20848 static enum template_base_result
20849 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20850 bool explain_p, tree *result)
20851 {
20852 tree rval = NULL_TREE;
20853 tree binfo;
20854
20855 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20856
20857 binfo = TYPE_BINFO (complete_type (arg));
20858 if (!binfo)
20859 {
20860 /* The type could not be completed. */
20861 *result = NULL_TREE;
20862 return tbr_incomplete_type;
20863 }
20864
20865 /* Walk in inheritance graph order. The search order is not
20866 important, and this avoids multiple walks of virtual bases. */
20867 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20868 {
20869 tree r = try_class_unification (tparms, targs, parm,
20870 BINFO_TYPE (binfo), explain_p);
20871
20872 if (r)
20873 {
20874 /* If there is more than one satisfactory baseclass, then:
20875
20876 [temp.deduct.call]
20877
20878 If they yield more than one possible deduced A, the type
20879 deduction fails.
20880
20881 applies. */
20882 if (rval && !same_type_p (r, rval))
20883 {
20884 *result = NULL_TREE;
20885 return tbr_ambiguous_baseclass;
20886 }
20887
20888 rval = r;
20889 }
20890 }
20891
20892 *result = rval;
20893 return tbr_success;
20894 }
20895
20896 /* Returns the level of DECL, which declares a template parameter. */
20897
20898 static int
20899 template_decl_level (tree decl)
20900 {
20901 switch (TREE_CODE (decl))
20902 {
20903 case TYPE_DECL:
20904 case TEMPLATE_DECL:
20905 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20906
20907 case PARM_DECL:
20908 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20909
20910 default:
20911 gcc_unreachable ();
20912 }
20913 return 0;
20914 }
20915
20916 /* Decide whether ARG can be unified with PARM, considering only the
20917 cv-qualifiers of each type, given STRICT as documented for unify.
20918 Returns nonzero iff the unification is OK on that basis. */
20919
20920 static int
20921 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20922 {
20923 int arg_quals = cp_type_quals (arg);
20924 int parm_quals = cp_type_quals (parm);
20925
20926 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20927 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20928 {
20929 /* Although a CVR qualifier is ignored when being applied to a
20930 substituted template parameter ([8.3.2]/1 for example), that
20931 does not allow us to unify "const T" with "int&" because both
20932 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20933 It is ok when we're allowing additional CV qualifiers
20934 at the outer level [14.8.2.1]/3,1st bullet. */
20935 if ((TYPE_REF_P (arg)
20936 || TREE_CODE (arg) == FUNCTION_TYPE
20937 || TREE_CODE (arg) == METHOD_TYPE)
20938 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20939 return 0;
20940
20941 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20942 && (parm_quals & TYPE_QUAL_RESTRICT))
20943 return 0;
20944 }
20945
20946 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20947 && (arg_quals & parm_quals) != parm_quals)
20948 return 0;
20949
20950 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20951 && (parm_quals & arg_quals) != arg_quals)
20952 return 0;
20953
20954 return 1;
20955 }
20956
20957 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20958 void
20959 template_parm_level_and_index (tree parm, int* level, int* index)
20960 {
20961 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20962 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20963 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20964 {
20965 *index = TEMPLATE_TYPE_IDX (parm);
20966 *level = TEMPLATE_TYPE_LEVEL (parm);
20967 }
20968 else
20969 {
20970 *index = TEMPLATE_PARM_IDX (parm);
20971 *level = TEMPLATE_PARM_LEVEL (parm);
20972 }
20973 }
20974
20975 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20976 do { \
20977 if (unify (TP, TA, P, A, S, EP)) \
20978 return 1; \
20979 } while (0)
20980
20981 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20982 expansion at the end of PACKED_PARMS. Returns 0 if the type
20983 deduction succeeds, 1 otherwise. STRICT is the same as in
20984 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20985 function call argument list. We'll need to adjust the arguments to make them
20986 types. SUBR tells us if this is from a recursive call to
20987 type_unification_real, or for comparing two template argument
20988 lists. */
20989
20990 static int
20991 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20992 tree packed_args, unification_kind_t strict,
20993 bool subr, bool explain_p)
20994 {
20995 tree parm
20996 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20997 tree pattern = PACK_EXPANSION_PATTERN (parm);
20998 tree pack, packs = NULL_TREE;
20999 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
21000
21001 /* Add in any args remembered from an earlier partial instantiation. */
21002 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
21003 int levels = TMPL_ARGS_DEPTH (targs);
21004
21005 packed_args = expand_template_argument_pack (packed_args);
21006
21007 int len = TREE_VEC_LENGTH (packed_args);
21008
21009 /* Determine the parameter packs we will be deducing from the
21010 pattern, and record their current deductions. */
21011 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
21012 pack; pack = TREE_CHAIN (pack))
21013 {
21014 tree parm_pack = TREE_VALUE (pack);
21015 int idx, level;
21016
21017 /* Only template parameter packs can be deduced, not e.g. function
21018 parameter packs or __bases or __integer_pack. */
21019 if (!TEMPLATE_PARM_P (parm_pack))
21020 continue;
21021
21022 /* Determine the index and level of this parameter pack. */
21023 template_parm_level_and_index (parm_pack, &level, &idx);
21024 if (level < levels)
21025 continue;
21026
21027 /* Keep track of the parameter packs and their corresponding
21028 argument packs. */
21029 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
21030 TREE_TYPE (packs) = make_tree_vec (len - start);
21031 }
21032
21033 /* Loop through all of the arguments that have not yet been
21034 unified and unify each with the pattern. */
21035 for (i = start; i < len; i++)
21036 {
21037 tree parm;
21038 bool any_explicit = false;
21039 tree arg = TREE_VEC_ELT (packed_args, i);
21040
21041 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21042 or the element of its argument pack at the current index if
21043 this argument was explicitly specified. */
21044 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21045 {
21046 int idx, level;
21047 tree arg, pargs;
21048 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21049
21050 arg = NULL_TREE;
21051 if (TREE_VALUE (pack)
21052 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
21053 && (i - start < TREE_VEC_LENGTH (pargs)))
21054 {
21055 any_explicit = true;
21056 arg = TREE_VEC_ELT (pargs, i - start);
21057 }
21058 TMPL_ARG (targs, level, idx) = arg;
21059 }
21060
21061 /* If we had explicit template arguments, substitute them into the
21062 pattern before deduction. */
21063 if (any_explicit)
21064 {
21065 /* Some arguments might still be unspecified or dependent. */
21066 bool dependent;
21067 ++processing_template_decl;
21068 dependent = any_dependent_template_arguments_p (targs);
21069 if (!dependent)
21070 --processing_template_decl;
21071 parm = tsubst (pattern, targs,
21072 explain_p ? tf_warning_or_error : tf_none,
21073 NULL_TREE);
21074 if (dependent)
21075 --processing_template_decl;
21076 if (parm == error_mark_node)
21077 return 1;
21078 }
21079 else
21080 parm = pattern;
21081
21082 /* Unify the pattern with the current argument. */
21083 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
21084 explain_p))
21085 return 1;
21086
21087 /* For each parameter pack, collect the deduced value. */
21088 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21089 {
21090 int idx, level;
21091 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21092
21093 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
21094 TMPL_ARG (targs, level, idx);
21095 }
21096 }
21097
21098 /* Verify that the results of unification with the parameter packs
21099 produce results consistent with what we've seen before, and make
21100 the deduced argument packs available. */
21101 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21102 {
21103 tree old_pack = TREE_VALUE (pack);
21104 tree new_args = TREE_TYPE (pack);
21105 int i, len = TREE_VEC_LENGTH (new_args);
21106 int idx, level;
21107 bool nondeduced_p = false;
21108
21109 /* By default keep the original deduced argument pack.
21110 If necessary, more specific code is going to update the
21111 resulting deduced argument later down in this function. */
21112 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21113 TMPL_ARG (targs, level, idx) = old_pack;
21114
21115 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21116 actually deduce anything. */
21117 for (i = 0; i < len && !nondeduced_p; ++i)
21118 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
21119 nondeduced_p = true;
21120 if (nondeduced_p)
21121 continue;
21122
21123 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
21124 {
21125 /* If we had fewer function args than explicit template args,
21126 just use the explicits. */
21127 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21128 int explicit_len = TREE_VEC_LENGTH (explicit_args);
21129 if (len < explicit_len)
21130 new_args = explicit_args;
21131 }
21132
21133 if (!old_pack)
21134 {
21135 tree result;
21136 /* Build the deduced *_ARGUMENT_PACK. */
21137 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
21138 {
21139 result = make_node (NONTYPE_ARGUMENT_PACK);
21140 TREE_CONSTANT (result) = 1;
21141 }
21142 else
21143 result = cxx_make_type (TYPE_ARGUMENT_PACK);
21144
21145 SET_ARGUMENT_PACK_ARGS (result, new_args);
21146
21147 /* Note the deduced argument packs for this parameter
21148 pack. */
21149 TMPL_ARG (targs, level, idx) = result;
21150 }
21151 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
21152 && (ARGUMENT_PACK_ARGS (old_pack)
21153 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
21154 {
21155 /* We only had the explicitly-provided arguments before, but
21156 now we have a complete set of arguments. */
21157 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21158
21159 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
21160 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
21161 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
21162 }
21163 else
21164 {
21165 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
21166 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
21167
21168 if (!comp_template_args (old_args, new_args,
21169 &bad_old_arg, &bad_new_arg))
21170 /* Inconsistent unification of this parameter pack. */
21171 return unify_parameter_pack_inconsistent (explain_p,
21172 bad_old_arg,
21173 bad_new_arg);
21174 }
21175 }
21176
21177 return unify_success (explain_p);
21178 }
21179
21180 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21181 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21182 parameters and return value are as for unify. */
21183
21184 static int
21185 unify_array_domain (tree tparms, tree targs,
21186 tree parm_dom, tree arg_dom,
21187 bool explain_p)
21188 {
21189 tree parm_max;
21190 tree arg_max;
21191 bool parm_cst;
21192 bool arg_cst;
21193
21194 /* Our representation of array types uses "N - 1" as the
21195 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21196 not an integer constant. We cannot unify arbitrarily
21197 complex expressions, so we eliminate the MINUS_EXPRs
21198 here. */
21199 parm_max = TYPE_MAX_VALUE (parm_dom);
21200 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
21201 if (!parm_cst)
21202 {
21203 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
21204 parm_max = TREE_OPERAND (parm_max, 0);
21205 }
21206 arg_max = TYPE_MAX_VALUE (arg_dom);
21207 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
21208 if (!arg_cst)
21209 {
21210 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21211 trying to unify the type of a variable with the type
21212 of a template parameter. For example:
21213
21214 template <unsigned int N>
21215 void f (char (&) [N]);
21216 int g();
21217 void h(int i) {
21218 char a[g(i)];
21219 f(a);
21220 }
21221
21222 Here, the type of the ARG will be "int [g(i)]", and
21223 may be a SAVE_EXPR, etc. */
21224 if (TREE_CODE (arg_max) != MINUS_EXPR)
21225 return unify_vla_arg (explain_p, arg_dom);
21226 arg_max = TREE_OPERAND (arg_max, 0);
21227 }
21228
21229 /* If only one of the bounds used a MINUS_EXPR, compensate
21230 by adding one to the other bound. */
21231 if (parm_cst && !arg_cst)
21232 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
21233 integer_type_node,
21234 parm_max,
21235 integer_one_node);
21236 else if (arg_cst && !parm_cst)
21237 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
21238 integer_type_node,
21239 arg_max,
21240 integer_one_node);
21241
21242 return unify (tparms, targs, parm_max, arg_max,
21243 UNIFY_ALLOW_INTEGER, explain_p);
21244 }
21245
21246 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21247
21248 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
21249
21250 static pa_kind_t
21251 pa_kind (tree t)
21252 {
21253 if (PACK_EXPANSION_P (t))
21254 t = PACK_EXPANSION_PATTERN (t);
21255 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
21256 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
21257 || DECL_TYPE_TEMPLATE_P (t))
21258 return pa_tmpl;
21259 else if (TYPE_P (t))
21260 return pa_type;
21261 else
21262 return pa_expr;
21263 }
21264
21265 /* Deduce the value of template parameters. TPARMS is the (innermost)
21266 set of template parameters to a template. TARGS is the bindings
21267 for those template parameters, as determined thus far; TARGS may
21268 include template arguments for outer levels of template parameters
21269 as well. PARM is a parameter to a template function, or a
21270 subcomponent of that parameter; ARG is the corresponding argument.
21271 This function attempts to match PARM with ARG in a manner
21272 consistent with the existing assignments in TARGS. If more values
21273 are deduced, then TARGS is updated.
21274
21275 Returns 0 if the type deduction succeeds, 1 otherwise. The
21276 parameter STRICT is a bitwise or of the following flags:
21277
21278 UNIFY_ALLOW_NONE:
21279 Require an exact match between PARM and ARG.
21280 UNIFY_ALLOW_MORE_CV_QUAL:
21281 Allow the deduced ARG to be more cv-qualified (by qualification
21282 conversion) than ARG.
21283 UNIFY_ALLOW_LESS_CV_QUAL:
21284 Allow the deduced ARG to be less cv-qualified than ARG.
21285 UNIFY_ALLOW_DERIVED:
21286 Allow the deduced ARG to be a template base class of ARG,
21287 or a pointer to a template base class of the type pointed to by
21288 ARG.
21289 UNIFY_ALLOW_INTEGER:
21290 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
21291 case for more information.
21292 UNIFY_ALLOW_OUTER_LEVEL:
21293 This is the outermost level of a deduction. Used to determine validity
21294 of qualification conversions. A valid qualification conversion must
21295 have const qualified pointers leading up to the inner type which
21296 requires additional CV quals, except at the outer level, where const
21297 is not required [conv.qual]. It would be normal to set this flag in
21298 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
21299 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
21300 This is the outermost level of a deduction, and PARM can be more CV
21301 qualified at this point.
21302 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
21303 This is the outermost level of a deduction, and PARM can be less CV
21304 qualified at this point. */
21305
21306 static int
21307 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
21308 bool explain_p)
21309 {
21310 int idx;
21311 tree targ;
21312 tree tparm;
21313 int strict_in = strict;
21314 tsubst_flags_t complain = (explain_p
21315 ? tf_warning_or_error
21316 : tf_none);
21317
21318 /* I don't think this will do the right thing with respect to types.
21319 But the only case I've seen it in so far has been array bounds, where
21320 signedness is the only information lost, and I think that will be
21321 okay. */
21322 while (CONVERT_EXPR_P (parm))
21323 parm = TREE_OPERAND (parm, 0);
21324
21325 if (arg == error_mark_node)
21326 return unify_invalid (explain_p);
21327 if (arg == unknown_type_node
21328 || arg == init_list_type_node)
21329 /* We can't deduce anything from this, but we might get all the
21330 template args from other function args. */
21331 return unify_success (explain_p);
21332
21333 if (parm == any_targ_node || arg == any_targ_node)
21334 return unify_success (explain_p);
21335
21336 /* If PARM uses template parameters, then we can't bail out here,
21337 even if ARG == PARM, since we won't record unifications for the
21338 template parameters. We might need them if we're trying to
21339 figure out which of two things is more specialized. */
21340 if (arg == parm && !uses_template_parms (parm))
21341 return unify_success (explain_p);
21342
21343 /* Handle init lists early, so the rest of the function can assume
21344 we're dealing with a type. */
21345 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
21346 {
21347 tree elt, elttype;
21348 unsigned i;
21349 tree orig_parm = parm;
21350
21351 /* Replace T with std::initializer_list<T> for deduction. */
21352 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21353 && flag_deduce_init_list)
21354 parm = listify (parm);
21355
21356 if (!is_std_init_list (parm)
21357 && TREE_CODE (parm) != ARRAY_TYPE)
21358 /* We can only deduce from an initializer list argument if the
21359 parameter is std::initializer_list or an array; otherwise this
21360 is a non-deduced context. */
21361 return unify_success (explain_p);
21362
21363 if (TREE_CODE (parm) == ARRAY_TYPE)
21364 elttype = TREE_TYPE (parm);
21365 else
21366 {
21367 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
21368 /* Deduction is defined in terms of a single type, so just punt
21369 on the (bizarre) std::initializer_list<T...>. */
21370 if (PACK_EXPANSION_P (elttype))
21371 return unify_success (explain_p);
21372 }
21373
21374 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
21375 {
21376 int elt_strict = strict;
21377
21378 if (elt == error_mark_node)
21379 return unify_invalid (explain_p);
21380
21381 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
21382 {
21383 tree type = TREE_TYPE (elt);
21384 if (type == error_mark_node)
21385 return unify_invalid (explain_p);
21386 /* It should only be possible to get here for a call. */
21387 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
21388 elt_strict |= maybe_adjust_types_for_deduction
21389 (DEDUCE_CALL, &elttype, &type, elt);
21390 elt = type;
21391 }
21392
21393 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
21394 explain_p);
21395 }
21396
21397 if (TREE_CODE (parm) == ARRAY_TYPE
21398 && deducible_array_bound (TYPE_DOMAIN (parm)))
21399 {
21400 /* Also deduce from the length of the initializer list. */
21401 tree max = size_int (CONSTRUCTOR_NELTS (arg));
21402 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
21403 if (idx == error_mark_node)
21404 return unify_invalid (explain_p);
21405 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21406 idx, explain_p);
21407 }
21408
21409 /* If the std::initializer_list<T> deduction worked, replace the
21410 deduced A with std::initializer_list<A>. */
21411 if (orig_parm != parm)
21412 {
21413 idx = TEMPLATE_TYPE_IDX (orig_parm);
21414 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21415 targ = listify (targ);
21416 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
21417 }
21418 return unify_success (explain_p);
21419 }
21420
21421 /* If parm and arg aren't the same kind of thing (template, type, or
21422 expression), fail early. */
21423 if (pa_kind (parm) != pa_kind (arg))
21424 return unify_invalid (explain_p);
21425
21426 /* Immediately reject some pairs that won't unify because of
21427 cv-qualification mismatches. */
21428 if (TREE_CODE (arg) == TREE_CODE (parm)
21429 && TYPE_P (arg)
21430 /* It is the elements of the array which hold the cv quals of an array
21431 type, and the elements might be template type parms. We'll check
21432 when we recurse. */
21433 && TREE_CODE (arg) != ARRAY_TYPE
21434 /* We check the cv-qualifiers when unifying with template type
21435 parameters below. We want to allow ARG `const T' to unify with
21436 PARM `T' for example, when computing which of two templates
21437 is more specialized, for example. */
21438 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
21439 && !check_cv_quals_for_unify (strict_in, arg, parm))
21440 return unify_cv_qual_mismatch (explain_p, parm, arg);
21441
21442 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
21443 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
21444 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
21445 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
21446 strict &= ~UNIFY_ALLOW_DERIVED;
21447 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21448 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
21449
21450 switch (TREE_CODE (parm))
21451 {
21452 case TYPENAME_TYPE:
21453 case SCOPE_REF:
21454 case UNBOUND_CLASS_TEMPLATE:
21455 /* In a type which contains a nested-name-specifier, template
21456 argument values cannot be deduced for template parameters used
21457 within the nested-name-specifier. */
21458 return unify_success (explain_p);
21459
21460 case TEMPLATE_TYPE_PARM:
21461 case TEMPLATE_TEMPLATE_PARM:
21462 case BOUND_TEMPLATE_TEMPLATE_PARM:
21463 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21464 if (error_operand_p (tparm))
21465 return unify_invalid (explain_p);
21466
21467 if (TEMPLATE_TYPE_LEVEL (parm)
21468 != template_decl_level (tparm))
21469 /* The PARM is not one we're trying to unify. Just check
21470 to see if it matches ARG. */
21471 {
21472 if (TREE_CODE (arg) == TREE_CODE (parm)
21473 && (is_auto (parm) ? is_auto (arg)
21474 : same_type_p (parm, arg)))
21475 return unify_success (explain_p);
21476 else
21477 return unify_type_mismatch (explain_p, parm, arg);
21478 }
21479 idx = TEMPLATE_TYPE_IDX (parm);
21480 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21481 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
21482 if (error_operand_p (tparm))
21483 return unify_invalid (explain_p);
21484
21485 /* Check for mixed types and values. */
21486 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21487 && TREE_CODE (tparm) != TYPE_DECL)
21488 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21489 && TREE_CODE (tparm) != TEMPLATE_DECL))
21490 gcc_unreachable ();
21491
21492 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21493 {
21494 if ((strict_in & UNIFY_ALLOW_DERIVED)
21495 && CLASS_TYPE_P (arg))
21496 {
21497 /* First try to match ARG directly. */
21498 tree t = try_class_unification (tparms, targs, parm, arg,
21499 explain_p);
21500 if (!t)
21501 {
21502 /* Otherwise, look for a suitable base of ARG, as below. */
21503 enum template_base_result r;
21504 r = get_template_base (tparms, targs, parm, arg,
21505 explain_p, &t);
21506 if (!t)
21507 return unify_no_common_base (explain_p, r, parm, arg);
21508 arg = t;
21509 }
21510 }
21511 /* ARG must be constructed from a template class or a template
21512 template parameter. */
21513 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
21514 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21515 return unify_template_deduction_failure (explain_p, parm, arg);
21516
21517 /* Deduce arguments T, i from TT<T> or TT<i>. */
21518 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
21519 return 1;
21520
21521 arg = TYPE_TI_TEMPLATE (arg);
21522
21523 /* Fall through to deduce template name. */
21524 }
21525
21526 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21527 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21528 {
21529 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21530
21531 /* Simple cases: Value already set, does match or doesn't. */
21532 if (targ != NULL_TREE && template_args_equal (targ, arg))
21533 return unify_success (explain_p);
21534 else if (targ)
21535 return unify_inconsistency (explain_p, parm, targ, arg);
21536 }
21537 else
21538 {
21539 /* If PARM is `const T' and ARG is only `int', we don't have
21540 a match unless we are allowing additional qualification.
21541 If ARG is `const int' and PARM is just `T' that's OK;
21542 that binds `const int' to `T'. */
21543 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
21544 arg, parm))
21545 return unify_cv_qual_mismatch (explain_p, parm, arg);
21546
21547 /* Consider the case where ARG is `const volatile int' and
21548 PARM is `const T'. Then, T should be `volatile int'. */
21549 arg = cp_build_qualified_type_real
21550 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
21551 if (arg == error_mark_node)
21552 return unify_invalid (explain_p);
21553
21554 /* Simple cases: Value already set, does match or doesn't. */
21555 if (targ != NULL_TREE && same_type_p (targ, arg))
21556 return unify_success (explain_p);
21557 else if (targ)
21558 return unify_inconsistency (explain_p, parm, targ, arg);
21559
21560 /* Make sure that ARG is not a variable-sized array. (Note
21561 that were talking about variable-sized arrays (like
21562 `int[n]'), rather than arrays of unknown size (like
21563 `int[]').) We'll get very confused by such a type since
21564 the bound of the array is not constant, and therefore
21565 not mangleable. Besides, such types are not allowed in
21566 ISO C++, so we can do as we please here. We do allow
21567 them for 'auto' deduction, since that isn't ABI-exposed. */
21568 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
21569 return unify_vla_arg (explain_p, arg);
21570
21571 /* Strip typedefs as in convert_template_argument. */
21572 arg = canonicalize_type_argument (arg, tf_none);
21573 }
21574
21575 /* If ARG is a parameter pack or an expansion, we cannot unify
21576 against it unless PARM is also a parameter pack. */
21577 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21578 && !template_parameter_pack_p (parm))
21579 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21580
21581 /* If the argument deduction results is a METHOD_TYPE,
21582 then there is a problem.
21583 METHOD_TYPE doesn't map to any real C++ type the result of
21584 the deduction can not be of that type. */
21585 if (TREE_CODE (arg) == METHOD_TYPE)
21586 return unify_method_type_error (explain_p, arg);
21587
21588 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21589 return unify_success (explain_p);
21590
21591 case TEMPLATE_PARM_INDEX:
21592 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21593 if (error_operand_p (tparm))
21594 return unify_invalid (explain_p);
21595
21596 if (TEMPLATE_PARM_LEVEL (parm)
21597 != template_decl_level (tparm))
21598 {
21599 /* The PARM is not one we're trying to unify. Just check
21600 to see if it matches ARG. */
21601 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
21602 && cp_tree_equal (parm, arg));
21603 if (result)
21604 unify_expression_unequal (explain_p, parm, arg);
21605 return result;
21606 }
21607
21608 idx = TEMPLATE_PARM_IDX (parm);
21609 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21610
21611 if (targ)
21612 {
21613 if ((strict & UNIFY_ALLOW_INTEGER)
21614 && TREE_TYPE (targ) && TREE_TYPE (arg)
21615 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
21616 /* We're deducing from an array bound, the type doesn't matter. */
21617 arg = fold_convert (TREE_TYPE (targ), arg);
21618 int x = !cp_tree_equal (targ, arg);
21619 if (x)
21620 unify_inconsistency (explain_p, parm, targ, arg);
21621 return x;
21622 }
21623
21624 /* [temp.deduct.type] If, in the declaration of a function template
21625 with a non-type template-parameter, the non-type
21626 template-parameter is used in an expression in the function
21627 parameter-list and, if the corresponding template-argument is
21628 deduced, the template-argument type shall match the type of the
21629 template-parameter exactly, except that a template-argument
21630 deduced from an array bound may be of any integral type.
21631 The non-type parameter might use already deduced type parameters. */
21632 tparm = TREE_TYPE (parm);
21633 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
21634 /* We don't have enough levels of args to do any substitution. This
21635 can happen in the context of -fnew-ttp-matching. */;
21636 else
21637 {
21638 ++processing_template_decl;
21639 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
21640 --processing_template_decl;
21641
21642 if (tree a = type_uses_auto (tparm))
21643 {
21644 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21645 if (tparm == error_mark_node)
21646 return 1;
21647 }
21648 }
21649
21650 if (!TREE_TYPE (arg))
21651 /* Template-parameter dependent expression. Just accept it for now.
21652 It will later be processed in convert_template_argument. */
21653 ;
21654 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21655 non_reference (tparm)))
21656 /* OK */;
21657 else if ((strict & UNIFY_ALLOW_INTEGER)
21658 && CP_INTEGRAL_TYPE_P (tparm))
21659 /* Convert the ARG to the type of PARM; the deduced non-type
21660 template argument must exactly match the types of the
21661 corresponding parameter. */
21662 arg = fold (build_nop (tparm, arg));
21663 else if (uses_template_parms (tparm))
21664 {
21665 /* We haven't deduced the type of this parameter yet. */
21666 if (cxx_dialect >= cxx17
21667 /* We deduce from array bounds in try_array_deduction. */
21668 && !(strict & UNIFY_ALLOW_INTEGER))
21669 {
21670 /* Deduce it from the non-type argument. */
21671 tree atype = TREE_TYPE (arg);
21672 RECUR_AND_CHECK_FAILURE (tparms, targs,
21673 tparm, atype,
21674 UNIFY_ALLOW_NONE, explain_p);
21675 }
21676 else
21677 /* Try again later. */
21678 return unify_success (explain_p);
21679 }
21680 else
21681 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21682
21683 /* If ARG is a parameter pack or an expansion, we cannot unify
21684 against it unless PARM is also a parameter pack. */
21685 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21686 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21687 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21688
21689 {
21690 bool removed_attr = false;
21691 arg = strip_typedefs_expr (arg, &removed_attr);
21692 }
21693 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21694 return unify_success (explain_p);
21695
21696 case PTRMEM_CST:
21697 {
21698 /* A pointer-to-member constant can be unified only with
21699 another constant. */
21700 if (TREE_CODE (arg) != PTRMEM_CST)
21701 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21702
21703 /* Just unify the class member. It would be useless (and possibly
21704 wrong, depending on the strict flags) to unify also
21705 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21706 arg refer to the same variable, even if through different
21707 classes. For instance:
21708
21709 struct A { int x; };
21710 struct B : A { };
21711
21712 Unification of &A::x and &B::x must succeed. */
21713 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21714 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21715 }
21716
21717 case POINTER_TYPE:
21718 {
21719 if (!TYPE_PTR_P (arg))
21720 return unify_type_mismatch (explain_p, parm, arg);
21721
21722 /* [temp.deduct.call]
21723
21724 A can be another pointer or pointer to member type that can
21725 be converted to the deduced A via a qualification
21726 conversion (_conv.qual_).
21727
21728 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21729 This will allow for additional cv-qualification of the
21730 pointed-to types if appropriate. */
21731
21732 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21733 /* The derived-to-base conversion only persists through one
21734 level of pointers. */
21735 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21736
21737 return unify (tparms, targs, TREE_TYPE (parm),
21738 TREE_TYPE (arg), strict, explain_p);
21739 }
21740
21741 case REFERENCE_TYPE:
21742 if (!TYPE_REF_P (arg))
21743 return unify_type_mismatch (explain_p, parm, arg);
21744 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21745 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21746
21747 case ARRAY_TYPE:
21748 if (TREE_CODE (arg) != ARRAY_TYPE)
21749 return unify_type_mismatch (explain_p, parm, arg);
21750 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21751 != (TYPE_DOMAIN (arg) == NULL_TREE))
21752 return unify_type_mismatch (explain_p, parm, arg);
21753 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21754 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21755 if (TYPE_DOMAIN (parm) != NULL_TREE)
21756 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21757 TYPE_DOMAIN (arg), explain_p);
21758 return unify_success (explain_p);
21759
21760 case REAL_TYPE:
21761 case COMPLEX_TYPE:
21762 case VECTOR_TYPE:
21763 case INTEGER_TYPE:
21764 case BOOLEAN_TYPE:
21765 case ENUMERAL_TYPE:
21766 case VOID_TYPE:
21767 case NULLPTR_TYPE:
21768 if (TREE_CODE (arg) != TREE_CODE (parm))
21769 return unify_type_mismatch (explain_p, parm, arg);
21770
21771 /* We have already checked cv-qualification at the top of the
21772 function. */
21773 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21774 return unify_type_mismatch (explain_p, parm, arg);
21775
21776 /* As far as unification is concerned, this wins. Later checks
21777 will invalidate it if necessary. */
21778 return unify_success (explain_p);
21779
21780 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21781 /* Type INTEGER_CST can come from ordinary constant template args. */
21782 case INTEGER_CST:
21783 while (CONVERT_EXPR_P (arg))
21784 arg = TREE_OPERAND (arg, 0);
21785
21786 if (TREE_CODE (arg) != INTEGER_CST)
21787 return unify_template_argument_mismatch (explain_p, parm, arg);
21788 return (tree_int_cst_equal (parm, arg)
21789 ? unify_success (explain_p)
21790 : unify_template_argument_mismatch (explain_p, parm, arg));
21791
21792 case TREE_VEC:
21793 {
21794 int i, len, argslen;
21795 int parm_variadic_p = 0;
21796
21797 if (TREE_CODE (arg) != TREE_VEC)
21798 return unify_template_argument_mismatch (explain_p, parm, arg);
21799
21800 len = TREE_VEC_LENGTH (parm);
21801 argslen = TREE_VEC_LENGTH (arg);
21802
21803 /* Check for pack expansions in the parameters. */
21804 for (i = 0; i < len; ++i)
21805 {
21806 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21807 {
21808 if (i == len - 1)
21809 /* We can unify against something with a trailing
21810 parameter pack. */
21811 parm_variadic_p = 1;
21812 else
21813 /* [temp.deduct.type]/9: If the template argument list of
21814 P contains a pack expansion that is not the last
21815 template argument, the entire template argument list
21816 is a non-deduced context. */
21817 return unify_success (explain_p);
21818 }
21819 }
21820
21821 /* If we don't have enough arguments to satisfy the parameters
21822 (not counting the pack expression at the end), or we have
21823 too many arguments for a parameter list that doesn't end in
21824 a pack expression, we can't unify. */
21825 if (parm_variadic_p
21826 ? argslen < len - parm_variadic_p
21827 : argslen != len)
21828 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21829
21830 /* Unify all of the parameters that precede the (optional)
21831 pack expression. */
21832 for (i = 0; i < len - parm_variadic_p; ++i)
21833 {
21834 RECUR_AND_CHECK_FAILURE (tparms, targs,
21835 TREE_VEC_ELT (parm, i),
21836 TREE_VEC_ELT (arg, i),
21837 UNIFY_ALLOW_NONE, explain_p);
21838 }
21839 if (parm_variadic_p)
21840 return unify_pack_expansion (tparms, targs, parm, arg,
21841 DEDUCE_EXACT,
21842 /*subr=*/true, explain_p);
21843 return unify_success (explain_p);
21844 }
21845
21846 case RECORD_TYPE:
21847 case UNION_TYPE:
21848 if (TREE_CODE (arg) != TREE_CODE (parm))
21849 return unify_type_mismatch (explain_p, parm, arg);
21850
21851 if (TYPE_PTRMEMFUNC_P (parm))
21852 {
21853 if (!TYPE_PTRMEMFUNC_P (arg))
21854 return unify_type_mismatch (explain_p, parm, arg);
21855
21856 return unify (tparms, targs,
21857 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21858 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21859 strict, explain_p);
21860 }
21861 else if (TYPE_PTRMEMFUNC_P (arg))
21862 return unify_type_mismatch (explain_p, parm, arg);
21863
21864 if (CLASSTYPE_TEMPLATE_INFO (parm))
21865 {
21866 tree t = NULL_TREE;
21867
21868 if (strict_in & UNIFY_ALLOW_DERIVED)
21869 {
21870 /* First, we try to unify the PARM and ARG directly. */
21871 t = try_class_unification (tparms, targs,
21872 parm, arg, explain_p);
21873
21874 if (!t)
21875 {
21876 /* Fallback to the special case allowed in
21877 [temp.deduct.call]:
21878
21879 If P is a class, and P has the form
21880 template-id, then A can be a derived class of
21881 the deduced A. Likewise, if P is a pointer to
21882 a class of the form template-id, A can be a
21883 pointer to a derived class pointed to by the
21884 deduced A. */
21885 enum template_base_result r;
21886 r = get_template_base (tparms, targs, parm, arg,
21887 explain_p, &t);
21888
21889 if (!t)
21890 {
21891 /* Don't give the derived diagnostic if we're
21892 already dealing with the same template. */
21893 bool same_template
21894 = (CLASSTYPE_TEMPLATE_INFO (arg)
21895 && (CLASSTYPE_TI_TEMPLATE (parm)
21896 == CLASSTYPE_TI_TEMPLATE (arg)));
21897 return unify_no_common_base (explain_p && !same_template,
21898 r, parm, arg);
21899 }
21900 }
21901 }
21902 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21903 && (CLASSTYPE_TI_TEMPLATE (parm)
21904 == CLASSTYPE_TI_TEMPLATE (arg)))
21905 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21906 Then, we should unify `int' and `U'. */
21907 t = arg;
21908 else
21909 /* There's no chance of unification succeeding. */
21910 return unify_type_mismatch (explain_p, parm, arg);
21911
21912 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21913 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21914 }
21915 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21916 return unify_type_mismatch (explain_p, parm, arg);
21917 return unify_success (explain_p);
21918
21919 case METHOD_TYPE:
21920 case FUNCTION_TYPE:
21921 {
21922 unsigned int nargs;
21923 tree *args;
21924 tree a;
21925 unsigned int i;
21926
21927 if (TREE_CODE (arg) != TREE_CODE (parm))
21928 return unify_type_mismatch (explain_p, parm, arg);
21929
21930 /* CV qualifications for methods can never be deduced, they must
21931 match exactly. We need to check them explicitly here,
21932 because type_unification_real treats them as any other
21933 cv-qualified parameter. */
21934 if (TREE_CODE (parm) == METHOD_TYPE
21935 && (!check_cv_quals_for_unify
21936 (UNIFY_ALLOW_NONE,
21937 class_of_this_parm (arg),
21938 class_of_this_parm (parm))))
21939 return unify_cv_qual_mismatch (explain_p, parm, arg);
21940 if (TREE_CODE (arg) == FUNCTION_TYPE
21941 && type_memfn_quals (parm) != type_memfn_quals (arg))
21942 return unify_cv_qual_mismatch (explain_p, parm, arg);
21943 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21944 return unify_type_mismatch (explain_p, parm, arg);
21945
21946 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21947 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21948
21949 nargs = list_length (TYPE_ARG_TYPES (arg));
21950 args = XALLOCAVEC (tree, nargs);
21951 for (a = TYPE_ARG_TYPES (arg), i = 0;
21952 a != NULL_TREE && a != void_list_node;
21953 a = TREE_CHAIN (a), ++i)
21954 args[i] = TREE_VALUE (a);
21955 nargs = i;
21956
21957 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21958 args, nargs, 1, DEDUCE_EXACT,
21959 LOOKUP_NORMAL, NULL, explain_p))
21960 return 1;
21961
21962 if (flag_noexcept_type)
21963 {
21964 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21965 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21966 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21967 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21968 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21969 && uses_template_parms (TREE_PURPOSE (pspec)))
21970 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21971 TREE_PURPOSE (aspec),
21972 UNIFY_ALLOW_NONE, explain_p);
21973 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21974 return unify_type_mismatch (explain_p, parm, arg);
21975 }
21976
21977 return 0;
21978 }
21979
21980 case OFFSET_TYPE:
21981 /* Unify a pointer to member with a pointer to member function, which
21982 deduces the type of the member as a function type. */
21983 if (TYPE_PTRMEMFUNC_P (arg))
21984 {
21985 /* Check top-level cv qualifiers */
21986 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21987 return unify_cv_qual_mismatch (explain_p, parm, arg);
21988
21989 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21990 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21991 UNIFY_ALLOW_NONE, explain_p);
21992
21993 /* Determine the type of the function we are unifying against. */
21994 tree fntype = static_fn_type (arg);
21995
21996 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21997 }
21998
21999 if (TREE_CODE (arg) != OFFSET_TYPE)
22000 return unify_type_mismatch (explain_p, parm, arg);
22001 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22002 TYPE_OFFSET_BASETYPE (arg),
22003 UNIFY_ALLOW_NONE, explain_p);
22004 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22005 strict, explain_p);
22006
22007 case CONST_DECL:
22008 if (DECL_TEMPLATE_PARM_P (parm))
22009 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
22010 if (arg != scalar_constant_value (parm))
22011 return unify_template_argument_mismatch (explain_p, parm, arg);
22012 return unify_success (explain_p);
22013
22014 case FIELD_DECL:
22015 case TEMPLATE_DECL:
22016 /* Matched cases are handled by the ARG == PARM test above. */
22017 return unify_template_argument_mismatch (explain_p, parm, arg);
22018
22019 case VAR_DECL:
22020 /* We might get a variable as a non-type template argument in parm if the
22021 corresponding parameter is type-dependent. Make any necessary
22022 adjustments based on whether arg is a reference. */
22023 if (CONSTANT_CLASS_P (arg))
22024 parm = fold_non_dependent_expr (parm);
22025 else if (REFERENCE_REF_P (arg))
22026 {
22027 tree sub = TREE_OPERAND (arg, 0);
22028 STRIP_NOPS (sub);
22029 if (TREE_CODE (sub) == ADDR_EXPR)
22030 arg = TREE_OPERAND (sub, 0);
22031 }
22032 /* Now use the normal expression code to check whether they match. */
22033 goto expr;
22034
22035 case TYPE_ARGUMENT_PACK:
22036 case NONTYPE_ARGUMENT_PACK:
22037 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
22038 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
22039
22040 case TYPEOF_TYPE:
22041 case DECLTYPE_TYPE:
22042 case UNDERLYING_TYPE:
22043 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22044 or UNDERLYING_TYPE nodes. */
22045 return unify_success (explain_p);
22046
22047 case ERROR_MARK:
22048 /* Unification fails if we hit an error node. */
22049 return unify_invalid (explain_p);
22050
22051 case INDIRECT_REF:
22052 if (REFERENCE_REF_P (parm))
22053 {
22054 bool pexp = PACK_EXPANSION_P (arg);
22055 if (pexp)
22056 arg = PACK_EXPANSION_PATTERN (arg);
22057 if (REFERENCE_REF_P (arg))
22058 arg = TREE_OPERAND (arg, 0);
22059 if (pexp)
22060 arg = make_pack_expansion (arg, complain);
22061 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
22062 strict, explain_p);
22063 }
22064 /* FALLTHRU */
22065
22066 default:
22067 /* An unresolved overload is a nondeduced context. */
22068 if (is_overloaded_fn (parm) || type_unknown_p (parm))
22069 return unify_success (explain_p);
22070 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
22071 expr:
22072 /* We must be looking at an expression. This can happen with
22073 something like:
22074
22075 template <int I>
22076 void foo(S<I>, S<I + 2>);
22077
22078 This is a "nondeduced context":
22079
22080 [deduct.type]
22081
22082 The nondeduced contexts are:
22083
22084 --A type that is a template-id in which one or more of
22085 the template-arguments is an expression that references
22086 a template-parameter.
22087
22088 In these cases, we assume deduction succeeded, but don't
22089 actually infer any unifications. */
22090
22091 if (!uses_template_parms (parm)
22092 && !template_args_equal (parm, arg))
22093 return unify_expression_unequal (explain_p, parm, arg);
22094 else
22095 return unify_success (explain_p);
22096 }
22097 }
22098 #undef RECUR_AND_CHECK_FAILURE
22099 \f
22100 /* Note that DECL can be defined in this translation unit, if
22101 required. */
22102
22103 static void
22104 mark_definable (tree decl)
22105 {
22106 tree clone;
22107 DECL_NOT_REALLY_EXTERN (decl) = 1;
22108 FOR_EACH_CLONE (clone, decl)
22109 DECL_NOT_REALLY_EXTERN (clone) = 1;
22110 }
22111
22112 /* Called if RESULT is explicitly instantiated, or is a member of an
22113 explicitly instantiated class. */
22114
22115 void
22116 mark_decl_instantiated (tree result, int extern_p)
22117 {
22118 SET_DECL_EXPLICIT_INSTANTIATION (result);
22119
22120 /* If this entity has already been written out, it's too late to
22121 make any modifications. */
22122 if (TREE_ASM_WRITTEN (result))
22123 return;
22124
22125 /* For anonymous namespace we don't need to do anything. */
22126 if (decl_anon_ns_mem_p (result))
22127 {
22128 gcc_assert (!TREE_PUBLIC (result));
22129 return;
22130 }
22131
22132 if (TREE_CODE (result) != FUNCTION_DECL)
22133 /* The TREE_PUBLIC flag for function declarations will have been
22134 set correctly by tsubst. */
22135 TREE_PUBLIC (result) = 1;
22136
22137 /* This might have been set by an earlier implicit instantiation. */
22138 DECL_COMDAT (result) = 0;
22139
22140 if (extern_p)
22141 DECL_NOT_REALLY_EXTERN (result) = 0;
22142 else
22143 {
22144 mark_definable (result);
22145 mark_needed (result);
22146 /* Always make artificials weak. */
22147 if (DECL_ARTIFICIAL (result) && flag_weak)
22148 comdat_linkage (result);
22149 /* For WIN32 we also want to put explicit instantiations in
22150 linkonce sections. */
22151 else if (TREE_PUBLIC (result))
22152 maybe_make_one_only (result);
22153 }
22154
22155 /* If EXTERN_P, then this function will not be emitted -- unless
22156 followed by an explicit instantiation, at which point its linkage
22157 will be adjusted. If !EXTERN_P, then this function will be
22158 emitted here. In neither circumstance do we want
22159 import_export_decl to adjust the linkage. */
22160 DECL_INTERFACE_KNOWN (result) = 1;
22161 }
22162
22163 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22164 important template arguments. If any are missing, we check whether
22165 they're important by using error_mark_node for substituting into any
22166 args that were used for partial ordering (the ones between ARGS and END)
22167 and seeing if it bubbles up. */
22168
22169 static bool
22170 check_undeduced_parms (tree targs, tree args, tree end)
22171 {
22172 bool found = false;
22173 int i;
22174 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
22175 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
22176 {
22177 found = true;
22178 TREE_VEC_ELT (targs, i) = error_mark_node;
22179 }
22180 if (found)
22181 {
22182 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
22183 if (substed == error_mark_node)
22184 return true;
22185 }
22186 return false;
22187 }
22188
22189 /* Given two function templates PAT1 and PAT2, return:
22190
22191 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22192 -1 if PAT2 is more specialized than PAT1.
22193 0 if neither is more specialized.
22194
22195 LEN indicates the number of parameters we should consider
22196 (defaulted parameters should not be considered).
22197
22198 The 1998 std underspecified function template partial ordering, and
22199 DR214 addresses the issue. We take pairs of arguments, one from
22200 each of the templates, and deduce them against each other. One of
22201 the templates will be more specialized if all the *other*
22202 template's arguments deduce against its arguments and at least one
22203 of its arguments *does* *not* deduce against the other template's
22204 corresponding argument. Deduction is done as for class templates.
22205 The arguments used in deduction have reference and top level cv
22206 qualifiers removed. Iff both arguments were originally reference
22207 types *and* deduction succeeds in both directions, an lvalue reference
22208 wins against an rvalue reference and otherwise the template
22209 with the more cv-qualified argument wins for that pairing (if
22210 neither is more cv-qualified, they both are equal). Unlike regular
22211 deduction, after all the arguments have been deduced in this way,
22212 we do *not* verify the deduced template argument values can be
22213 substituted into non-deduced contexts.
22214
22215 The logic can be a bit confusing here, because we look at deduce1 and
22216 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22217 can find template arguments for pat1 to make arg1 look like arg2, that
22218 means that arg2 is at least as specialized as arg1. */
22219
22220 int
22221 more_specialized_fn (tree pat1, tree pat2, int len)
22222 {
22223 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
22224 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
22225 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
22226 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
22227 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
22228 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
22229 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
22230 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
22231 tree origs1, origs2;
22232 bool lose1 = false;
22233 bool lose2 = false;
22234
22235 /* Remove the this parameter from non-static member functions. If
22236 one is a non-static member function and the other is not a static
22237 member function, remove the first parameter from that function
22238 also. This situation occurs for operator functions where we
22239 locate both a member function (with this pointer) and non-member
22240 operator (with explicit first operand). */
22241 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
22242 {
22243 len--; /* LEN is the number of significant arguments for DECL1 */
22244 args1 = TREE_CHAIN (args1);
22245 if (!DECL_STATIC_FUNCTION_P (decl2))
22246 args2 = TREE_CHAIN (args2);
22247 }
22248 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
22249 {
22250 args2 = TREE_CHAIN (args2);
22251 if (!DECL_STATIC_FUNCTION_P (decl1))
22252 {
22253 len--;
22254 args1 = TREE_CHAIN (args1);
22255 }
22256 }
22257
22258 /* If only one is a conversion operator, they are unordered. */
22259 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
22260 return 0;
22261
22262 /* Consider the return type for a conversion function */
22263 if (DECL_CONV_FN_P (decl1))
22264 {
22265 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
22266 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
22267 len++;
22268 }
22269
22270 processing_template_decl++;
22271
22272 origs1 = args1;
22273 origs2 = args2;
22274
22275 while (len--
22276 /* Stop when an ellipsis is seen. */
22277 && args1 != NULL_TREE && args2 != NULL_TREE)
22278 {
22279 tree arg1 = TREE_VALUE (args1);
22280 tree arg2 = TREE_VALUE (args2);
22281 int deduce1, deduce2;
22282 int quals1 = -1;
22283 int quals2 = -1;
22284 int ref1 = 0;
22285 int ref2 = 0;
22286
22287 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22288 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22289 {
22290 /* When both arguments are pack expansions, we need only
22291 unify the patterns themselves. */
22292 arg1 = PACK_EXPANSION_PATTERN (arg1);
22293 arg2 = PACK_EXPANSION_PATTERN (arg2);
22294
22295 /* This is the last comparison we need to do. */
22296 len = 0;
22297 }
22298
22299 /* DR 1847: If a particular P contains no template-parameters that
22300 participate in template argument deduction, that P is not used to
22301 determine the ordering. */
22302 if (!uses_deducible_template_parms (arg1)
22303 && !uses_deducible_template_parms (arg2))
22304 goto next;
22305
22306 if (TYPE_REF_P (arg1))
22307 {
22308 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
22309 arg1 = TREE_TYPE (arg1);
22310 quals1 = cp_type_quals (arg1);
22311 }
22312
22313 if (TYPE_REF_P (arg2))
22314 {
22315 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
22316 arg2 = TREE_TYPE (arg2);
22317 quals2 = cp_type_quals (arg2);
22318 }
22319
22320 arg1 = TYPE_MAIN_VARIANT (arg1);
22321 arg2 = TYPE_MAIN_VARIANT (arg2);
22322
22323 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
22324 {
22325 int i, len2 = remaining_arguments (args2);
22326 tree parmvec = make_tree_vec (1);
22327 tree argvec = make_tree_vec (len2);
22328 tree ta = args2;
22329
22330 /* Setup the parameter vector, which contains only ARG1. */
22331 TREE_VEC_ELT (parmvec, 0) = arg1;
22332
22333 /* Setup the argument vector, which contains the remaining
22334 arguments. */
22335 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
22336 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
22337
22338 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
22339 argvec, DEDUCE_EXACT,
22340 /*subr=*/true, /*explain_p=*/false)
22341 == 0);
22342
22343 /* We cannot deduce in the other direction, because ARG1 is
22344 a pack expansion but ARG2 is not. */
22345 deduce2 = 0;
22346 }
22347 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22348 {
22349 int i, len1 = remaining_arguments (args1);
22350 tree parmvec = make_tree_vec (1);
22351 tree argvec = make_tree_vec (len1);
22352 tree ta = args1;
22353
22354 /* Setup the parameter vector, which contains only ARG1. */
22355 TREE_VEC_ELT (parmvec, 0) = arg2;
22356
22357 /* Setup the argument vector, which contains the remaining
22358 arguments. */
22359 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
22360 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
22361
22362 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
22363 argvec, DEDUCE_EXACT,
22364 /*subr=*/true, /*explain_p=*/false)
22365 == 0);
22366
22367 /* We cannot deduce in the other direction, because ARG2 is
22368 a pack expansion but ARG1 is not.*/
22369 deduce1 = 0;
22370 }
22371
22372 else
22373 {
22374 /* The normal case, where neither argument is a pack
22375 expansion. */
22376 deduce1 = (unify (tparms1, targs1, arg1, arg2,
22377 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22378 == 0);
22379 deduce2 = (unify (tparms2, targs2, arg2, arg1,
22380 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22381 == 0);
22382 }
22383
22384 /* If we couldn't deduce arguments for tparms1 to make arg1 match
22385 arg2, then arg2 is not as specialized as arg1. */
22386 if (!deduce1)
22387 lose2 = true;
22388 if (!deduce2)
22389 lose1 = true;
22390
22391 /* "If, for a given type, deduction succeeds in both directions
22392 (i.e., the types are identical after the transformations above)
22393 and both P and A were reference types (before being replaced with
22394 the type referred to above):
22395 - if the type from the argument template was an lvalue reference and
22396 the type from the parameter template was not, the argument type is
22397 considered to be more specialized than the other; otherwise,
22398 - if the type from the argument template is more cv-qualified
22399 than the type from the parameter template (as described above),
22400 the argument type is considered to be more specialized than the other;
22401 otherwise,
22402 - neither type is more specialized than the other." */
22403
22404 if (deduce1 && deduce2)
22405 {
22406 if (ref1 && ref2 && ref1 != ref2)
22407 {
22408 if (ref1 > ref2)
22409 lose1 = true;
22410 else
22411 lose2 = true;
22412 }
22413 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
22414 {
22415 if ((quals1 & quals2) == quals2)
22416 lose2 = true;
22417 if ((quals1 & quals2) == quals1)
22418 lose1 = true;
22419 }
22420 }
22421
22422 if (lose1 && lose2)
22423 /* We've failed to deduce something in either direction.
22424 These must be unordered. */
22425 break;
22426
22427 next:
22428
22429 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22430 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22431 /* We have already processed all of the arguments in our
22432 handing of the pack expansion type. */
22433 len = 0;
22434
22435 args1 = TREE_CHAIN (args1);
22436 args2 = TREE_CHAIN (args2);
22437 }
22438
22439 /* "In most cases, all template parameters must have values in order for
22440 deduction to succeed, but for partial ordering purposes a template
22441 parameter may remain without a value provided it is not used in the
22442 types being used for partial ordering."
22443
22444 Thus, if we are missing any of the targs1 we need to substitute into
22445 origs1, then pat2 is not as specialized as pat1. This can happen when
22446 there is a nondeduced context. */
22447 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
22448 lose2 = true;
22449 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
22450 lose1 = true;
22451
22452 processing_template_decl--;
22453
22454 /* If both deductions succeed, the partial ordering selects the more
22455 constrained template. */
22456 if (!lose1 && !lose2)
22457 {
22458 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
22459 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
22460 lose1 = !subsumes_constraints (c1, c2);
22461 lose2 = !subsumes_constraints (c2, c1);
22462 }
22463
22464 /* All things being equal, if the next argument is a pack expansion
22465 for one function but not for the other, prefer the
22466 non-variadic function. FIXME this is bogus; see c++/41958. */
22467 if (lose1 == lose2
22468 && args1 && TREE_VALUE (args1)
22469 && args2 && TREE_VALUE (args2))
22470 {
22471 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
22472 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
22473 }
22474
22475 if (lose1 == lose2)
22476 return 0;
22477 else if (!lose1)
22478 return 1;
22479 else
22480 return -1;
22481 }
22482
22483 /* Determine which of two partial specializations of TMPL is more
22484 specialized.
22485
22486 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22487 to the first partial specialization. The TREE_PURPOSE is the
22488 innermost set of template parameters for the partial
22489 specialization. PAT2 is similar, but for the second template.
22490
22491 Return 1 if the first partial specialization is more specialized;
22492 -1 if the second is more specialized; 0 if neither is more
22493 specialized.
22494
22495 See [temp.class.order] for information about determining which of
22496 two templates is more specialized. */
22497
22498 static int
22499 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
22500 {
22501 tree targs;
22502 int winner = 0;
22503 bool any_deductions = false;
22504
22505 tree tmpl1 = TREE_VALUE (pat1);
22506 tree tmpl2 = TREE_VALUE (pat2);
22507 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
22508 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
22509
22510 /* Just like what happens for functions, if we are ordering between
22511 different template specializations, we may encounter dependent
22512 types in the arguments, and we need our dependency check functions
22513 to behave correctly. */
22514 ++processing_template_decl;
22515 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
22516 if (targs)
22517 {
22518 --winner;
22519 any_deductions = true;
22520 }
22521
22522 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
22523 if (targs)
22524 {
22525 ++winner;
22526 any_deductions = true;
22527 }
22528 --processing_template_decl;
22529
22530 /* If both deductions succeed, the partial ordering selects the more
22531 constrained template. */
22532 if (!winner && any_deductions)
22533 return more_constrained (tmpl1, tmpl2);
22534
22535 /* In the case of a tie where at least one of the templates
22536 has a parameter pack at the end, the template with the most
22537 non-packed parameters wins. */
22538 if (winner == 0
22539 && any_deductions
22540 && (template_args_variadic_p (TREE_PURPOSE (pat1))
22541 || template_args_variadic_p (TREE_PURPOSE (pat2))))
22542 {
22543 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
22544 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
22545 int len1 = TREE_VEC_LENGTH (args1);
22546 int len2 = TREE_VEC_LENGTH (args2);
22547
22548 /* We don't count the pack expansion at the end. */
22549 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
22550 --len1;
22551 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
22552 --len2;
22553
22554 if (len1 > len2)
22555 return 1;
22556 else if (len1 < len2)
22557 return -1;
22558 }
22559
22560 return winner;
22561 }
22562
22563 /* Return the template arguments that will produce the function signature
22564 DECL from the function template FN, with the explicit template
22565 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22566 also match. Return NULL_TREE if no satisfactory arguments could be
22567 found. */
22568
22569 static tree
22570 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
22571 {
22572 int ntparms = DECL_NTPARMS (fn);
22573 tree targs = make_tree_vec (ntparms);
22574 tree decl_type = TREE_TYPE (decl);
22575 tree decl_arg_types;
22576 tree *args;
22577 unsigned int nargs, ix;
22578 tree arg;
22579
22580 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
22581
22582 /* Never do unification on the 'this' parameter. */
22583 decl_arg_types = skip_artificial_parms_for (decl,
22584 TYPE_ARG_TYPES (decl_type));
22585
22586 nargs = list_length (decl_arg_types);
22587 args = XALLOCAVEC (tree, nargs);
22588 for (arg = decl_arg_types, ix = 0;
22589 arg != NULL_TREE && arg != void_list_node;
22590 arg = TREE_CHAIN (arg), ++ix)
22591 args[ix] = TREE_VALUE (arg);
22592
22593 if (fn_type_unification (fn, explicit_args, targs,
22594 args, ix,
22595 (check_rettype || DECL_CONV_FN_P (fn)
22596 ? TREE_TYPE (decl_type) : NULL_TREE),
22597 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
22598 /*decltype*/false)
22599 == error_mark_node)
22600 return NULL_TREE;
22601
22602 return targs;
22603 }
22604
22605 /* Return the innermost template arguments that, when applied to a partial
22606 specialization SPEC_TMPL of TMPL, yield the ARGS.
22607
22608 For example, suppose we have:
22609
22610 template <class T, class U> struct S {};
22611 template <class T> struct S<T*, int> {};
22612
22613 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22614 partial specialization and the ARGS will be {double*, int}. The resulting
22615 vector will be {double}, indicating that `T' is bound to `double'. */
22616
22617 static tree
22618 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
22619 {
22620 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
22621 tree spec_args
22622 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
22623 int i, ntparms = TREE_VEC_LENGTH (tparms);
22624 tree deduced_args;
22625 tree innermost_deduced_args;
22626
22627 innermost_deduced_args = make_tree_vec (ntparms);
22628 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22629 {
22630 deduced_args = copy_node (args);
22631 SET_TMPL_ARGS_LEVEL (deduced_args,
22632 TMPL_ARGS_DEPTH (deduced_args),
22633 innermost_deduced_args);
22634 }
22635 else
22636 deduced_args = innermost_deduced_args;
22637
22638 bool tried_array_deduction = (cxx_dialect < cxx17);
22639 again:
22640 if (unify (tparms, deduced_args,
22641 INNERMOST_TEMPLATE_ARGS (spec_args),
22642 INNERMOST_TEMPLATE_ARGS (args),
22643 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22644 return NULL_TREE;
22645
22646 for (i = 0; i < ntparms; ++i)
22647 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22648 {
22649 if (!tried_array_deduction)
22650 {
22651 try_array_deduction (tparms, innermost_deduced_args,
22652 INNERMOST_TEMPLATE_ARGS (spec_args));
22653 tried_array_deduction = true;
22654 if (TREE_VEC_ELT (innermost_deduced_args, i))
22655 goto again;
22656 }
22657 return NULL_TREE;
22658 }
22659
22660 if (!push_tinst_level (spec_tmpl, deduced_args))
22661 {
22662 excessive_deduction_depth = true;
22663 return NULL_TREE;
22664 }
22665
22666 /* Verify that nondeduced template arguments agree with the type
22667 obtained from argument deduction.
22668
22669 For example:
22670
22671 struct A { typedef int X; };
22672 template <class T, class U> struct C {};
22673 template <class T> struct C<T, typename T::X> {};
22674
22675 Then with the instantiation `C<A, int>', we can deduce that
22676 `T' is `A' but unify () does not check whether `typename T::X'
22677 is `int'. */
22678 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22679
22680 if (spec_args != error_mark_node)
22681 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22682 INNERMOST_TEMPLATE_ARGS (spec_args),
22683 tmpl, tf_none, false, false);
22684
22685 pop_tinst_level ();
22686
22687 if (spec_args == error_mark_node
22688 /* We only need to check the innermost arguments; the other
22689 arguments will always agree. */
22690 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22691 INNERMOST_TEMPLATE_ARGS (args)))
22692 return NULL_TREE;
22693
22694 /* Now that we have bindings for all of the template arguments,
22695 ensure that the arguments deduced for the template template
22696 parameters have compatible template parameter lists. See the use
22697 of template_template_parm_bindings_ok_p in fn_type_unification
22698 for more information. */
22699 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22700 return NULL_TREE;
22701
22702 return deduced_args;
22703 }
22704
22705 // Compare two function templates T1 and T2 by deducing bindings
22706 // from one against the other. If both deductions succeed, compare
22707 // constraints to see which is more constrained.
22708 static int
22709 more_specialized_inst (tree t1, tree t2)
22710 {
22711 int fate = 0;
22712 int count = 0;
22713
22714 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22715 {
22716 --fate;
22717 ++count;
22718 }
22719
22720 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22721 {
22722 ++fate;
22723 ++count;
22724 }
22725
22726 // If both deductions succeed, then one may be more constrained.
22727 if (count == 2 && fate == 0)
22728 fate = more_constrained (t1, t2);
22729
22730 return fate;
22731 }
22732
22733 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22734 Return the TREE_LIST node with the most specialized template, if
22735 any. If there is no most specialized template, the error_mark_node
22736 is returned.
22737
22738 Note that this function does not look at, or modify, the
22739 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22740 returned is one of the elements of INSTANTIATIONS, callers may
22741 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22742 and retrieve it from the value returned. */
22743
22744 tree
22745 most_specialized_instantiation (tree templates)
22746 {
22747 tree fn, champ;
22748
22749 ++processing_template_decl;
22750
22751 champ = templates;
22752 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22753 {
22754 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22755 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22756 if (fate == -1)
22757 champ = fn;
22758 else if (!fate)
22759 {
22760 /* Equally specialized, move to next function. If there
22761 is no next function, nothing's most specialized. */
22762 fn = TREE_CHAIN (fn);
22763 champ = fn;
22764 if (!fn)
22765 break;
22766 }
22767 }
22768
22769 if (champ)
22770 /* Now verify that champ is better than everything earlier in the
22771 instantiation list. */
22772 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22773 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22774 {
22775 champ = NULL_TREE;
22776 break;
22777 }
22778 }
22779
22780 processing_template_decl--;
22781
22782 if (!champ)
22783 return error_mark_node;
22784
22785 return champ;
22786 }
22787
22788 /* If DECL is a specialization of some template, return the most
22789 general such template. Otherwise, returns NULL_TREE.
22790
22791 For example, given:
22792
22793 template <class T> struct S { template <class U> void f(U); };
22794
22795 if TMPL is `template <class U> void S<int>::f(U)' this will return
22796 the full template. This function will not trace past partial
22797 specializations, however. For example, given in addition:
22798
22799 template <class T> struct S<T*> { template <class U> void f(U); };
22800
22801 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22802 `template <class T> template <class U> S<T*>::f(U)'. */
22803
22804 tree
22805 most_general_template (tree decl)
22806 {
22807 if (TREE_CODE (decl) != TEMPLATE_DECL)
22808 {
22809 if (tree tinfo = get_template_info (decl))
22810 decl = TI_TEMPLATE (tinfo);
22811 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22812 template friend, or a FIELD_DECL for a capture pack. */
22813 if (TREE_CODE (decl) != TEMPLATE_DECL)
22814 return NULL_TREE;
22815 }
22816
22817 /* Look for more and more general templates. */
22818 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22819 {
22820 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22821 (See cp-tree.h for details.) */
22822 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22823 break;
22824
22825 if (CLASS_TYPE_P (TREE_TYPE (decl))
22826 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22827 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22828 break;
22829
22830 /* Stop if we run into an explicitly specialized class template. */
22831 if (!DECL_NAMESPACE_SCOPE_P (decl)
22832 && DECL_CONTEXT (decl)
22833 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22834 break;
22835
22836 decl = DECL_TI_TEMPLATE (decl);
22837 }
22838
22839 return decl;
22840 }
22841
22842 /* Return the most specialized of the template partial specializations
22843 which can produce TARGET, a specialization of some class or variable
22844 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22845 a TEMPLATE_DECL node corresponding to the partial specialization, while
22846 the TREE_PURPOSE is the set of template arguments that must be
22847 substituted into the template pattern in order to generate TARGET.
22848
22849 If the choice of partial specialization is ambiguous, a diagnostic
22850 is issued, and the error_mark_node is returned. If there are no
22851 partial specializations matching TARGET, then NULL_TREE is
22852 returned, indicating that the primary template should be used. */
22853
22854 static tree
22855 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22856 {
22857 tree list = NULL_TREE;
22858 tree t;
22859 tree champ;
22860 int fate;
22861 bool ambiguous_p;
22862 tree outer_args = NULL_TREE;
22863 tree tmpl, args;
22864
22865 if (TYPE_P (target))
22866 {
22867 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22868 tmpl = TI_TEMPLATE (tinfo);
22869 args = TI_ARGS (tinfo);
22870 }
22871 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22872 {
22873 tmpl = TREE_OPERAND (target, 0);
22874 args = TREE_OPERAND (target, 1);
22875 }
22876 else if (VAR_P (target))
22877 {
22878 tree tinfo = DECL_TEMPLATE_INFO (target);
22879 tmpl = TI_TEMPLATE (tinfo);
22880 args = TI_ARGS (tinfo);
22881 }
22882 else
22883 gcc_unreachable ();
22884
22885 tree main_tmpl = most_general_template (tmpl);
22886
22887 /* For determining which partial specialization to use, only the
22888 innermost args are interesting. */
22889 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22890 {
22891 outer_args = strip_innermost_template_args (args, 1);
22892 args = INNERMOST_TEMPLATE_ARGS (args);
22893 }
22894
22895 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22896 {
22897 tree spec_args;
22898 tree spec_tmpl = TREE_VALUE (t);
22899
22900 if (outer_args)
22901 {
22902 /* Substitute in the template args from the enclosing class. */
22903 ++processing_template_decl;
22904 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22905 --processing_template_decl;
22906 }
22907
22908 if (spec_tmpl == error_mark_node)
22909 return error_mark_node;
22910
22911 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22912 if (spec_args)
22913 {
22914 if (outer_args)
22915 spec_args = add_to_template_args (outer_args, spec_args);
22916
22917 /* Keep the candidate only if the constraints are satisfied,
22918 or if we're not compiling with concepts. */
22919 if (!flag_concepts
22920 || constraints_satisfied_p (spec_tmpl, spec_args))
22921 {
22922 list = tree_cons (spec_args, TREE_VALUE (t), list);
22923 TREE_TYPE (list) = TREE_TYPE (t);
22924 }
22925 }
22926 }
22927
22928 if (! list)
22929 return NULL_TREE;
22930
22931 ambiguous_p = false;
22932 t = list;
22933 champ = t;
22934 t = TREE_CHAIN (t);
22935 for (; t; t = TREE_CHAIN (t))
22936 {
22937 fate = more_specialized_partial_spec (tmpl, champ, t);
22938 if (fate == 1)
22939 ;
22940 else
22941 {
22942 if (fate == 0)
22943 {
22944 t = TREE_CHAIN (t);
22945 if (! t)
22946 {
22947 ambiguous_p = true;
22948 break;
22949 }
22950 }
22951 champ = t;
22952 }
22953 }
22954
22955 if (!ambiguous_p)
22956 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22957 {
22958 fate = more_specialized_partial_spec (tmpl, champ, t);
22959 if (fate != 1)
22960 {
22961 ambiguous_p = true;
22962 break;
22963 }
22964 }
22965
22966 if (ambiguous_p)
22967 {
22968 const char *str;
22969 char *spaces = NULL;
22970 if (!(complain & tf_error))
22971 return error_mark_node;
22972 if (TYPE_P (target))
22973 error ("ambiguous template instantiation for %q#T", target);
22974 else
22975 error ("ambiguous template instantiation for %q#D", target);
22976 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22977 for (t = list; t; t = TREE_CHAIN (t))
22978 {
22979 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22980 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22981 "%s %#qS", spaces ? spaces : str, subst);
22982 spaces = spaces ? spaces : get_spaces (str);
22983 }
22984 free (spaces);
22985 return error_mark_node;
22986 }
22987
22988 return champ;
22989 }
22990
22991 /* Explicitly instantiate DECL. */
22992
22993 void
22994 do_decl_instantiation (tree decl, tree storage)
22995 {
22996 tree result = NULL_TREE;
22997 int extern_p = 0;
22998
22999 if (!decl || decl == error_mark_node)
23000 /* An error occurred, for which grokdeclarator has already issued
23001 an appropriate message. */
23002 return;
23003 else if (! DECL_LANG_SPECIFIC (decl))
23004 {
23005 error ("explicit instantiation of non-template %q#D", decl);
23006 return;
23007 }
23008
23009 bool var_templ = (DECL_TEMPLATE_INFO (decl)
23010 && variable_template_p (DECL_TI_TEMPLATE (decl)));
23011
23012 if (VAR_P (decl) && !var_templ)
23013 {
23014 /* There is an asymmetry here in the way VAR_DECLs and
23015 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23016 the latter, the DECL we get back will be marked as a
23017 template instantiation, and the appropriate
23018 DECL_TEMPLATE_INFO will be set up. This does not happen for
23019 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23020 should handle VAR_DECLs as it currently handles
23021 FUNCTION_DECLs. */
23022 if (!DECL_CLASS_SCOPE_P (decl))
23023 {
23024 error ("%qD is not a static data member of a class template", decl);
23025 return;
23026 }
23027 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
23028 if (!result || !VAR_P (result))
23029 {
23030 error ("no matching template for %qD found", decl);
23031 return;
23032 }
23033 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
23034 {
23035 error ("type %qT for explicit instantiation %qD does not match "
23036 "declared type %qT", TREE_TYPE (result), decl,
23037 TREE_TYPE (decl));
23038 return;
23039 }
23040 }
23041 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
23042 {
23043 error ("explicit instantiation of %q#D", decl);
23044 return;
23045 }
23046 else
23047 result = decl;
23048
23049 /* Check for various error cases. Note that if the explicit
23050 instantiation is valid the RESULT will currently be marked as an
23051 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23052 until we get here. */
23053
23054 if (DECL_TEMPLATE_SPECIALIZATION (result))
23055 {
23056 /* DR 259 [temp.spec].
23057
23058 Both an explicit instantiation and a declaration of an explicit
23059 specialization shall not appear in a program unless the explicit
23060 instantiation follows a declaration of the explicit specialization.
23061
23062 For a given set of template parameters, if an explicit
23063 instantiation of a template appears after a declaration of an
23064 explicit specialization for that template, the explicit
23065 instantiation has no effect. */
23066 return;
23067 }
23068 else if (DECL_EXPLICIT_INSTANTIATION (result))
23069 {
23070 /* [temp.spec]
23071
23072 No program shall explicitly instantiate any template more
23073 than once.
23074
23075 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23076 the first instantiation was `extern' and the second is not,
23077 and EXTERN_P for the opposite case. */
23078 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
23079 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
23080 /* If an "extern" explicit instantiation follows an ordinary
23081 explicit instantiation, the template is instantiated. */
23082 if (extern_p)
23083 return;
23084 }
23085 else if (!DECL_IMPLICIT_INSTANTIATION (result))
23086 {
23087 error ("no matching template for %qD found", result);
23088 return;
23089 }
23090 else if (!DECL_TEMPLATE_INFO (result))
23091 {
23092 permerror (input_location, "explicit instantiation of non-template %q#D", result);
23093 return;
23094 }
23095
23096 if (storage == NULL_TREE)
23097 ;
23098 else if (storage == ridpointers[(int) RID_EXTERN])
23099 {
23100 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
23101 pedwarn (input_location, OPT_Wpedantic,
23102 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23103 "instantiations");
23104 extern_p = 1;
23105 }
23106 else
23107 error ("storage class %qD applied to template instantiation", storage);
23108
23109 check_explicit_instantiation_namespace (result);
23110 mark_decl_instantiated (result, extern_p);
23111 if (! extern_p)
23112 instantiate_decl (result, /*defer_ok=*/true,
23113 /*expl_inst_class_mem_p=*/false);
23114 }
23115
23116 static void
23117 mark_class_instantiated (tree t, int extern_p)
23118 {
23119 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
23120 SET_CLASSTYPE_INTERFACE_KNOWN (t);
23121 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
23122 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
23123 if (! extern_p)
23124 {
23125 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
23126 rest_of_type_compilation (t, 1);
23127 }
23128 }
23129
23130 /* Called from do_type_instantiation through binding_table_foreach to
23131 do recursive instantiation for the type bound in ENTRY. */
23132 static void
23133 bt_instantiate_type_proc (binding_entry entry, void *data)
23134 {
23135 tree storage = *(tree *) data;
23136
23137 if (MAYBE_CLASS_TYPE_P (entry->type)
23138 && CLASSTYPE_TEMPLATE_INFO (entry->type)
23139 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
23140 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
23141 }
23142
23143 /* Perform an explicit instantiation of template class T. STORAGE, if
23144 non-null, is the RID for extern, inline or static. COMPLAIN is
23145 nonzero if this is called from the parser, zero if called recursively,
23146 since the standard is unclear (as detailed below). */
23147
23148 void
23149 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
23150 {
23151 int extern_p = 0;
23152 int nomem_p = 0;
23153 int static_p = 0;
23154 int previous_instantiation_extern_p = 0;
23155
23156 if (TREE_CODE (t) == TYPE_DECL)
23157 t = TREE_TYPE (t);
23158
23159 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
23160 {
23161 tree tmpl =
23162 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
23163 if (tmpl)
23164 error ("explicit instantiation of non-class template %qD", tmpl);
23165 else
23166 error ("explicit instantiation of non-template type %qT", t);
23167 return;
23168 }
23169
23170 complete_type (t);
23171
23172 if (!COMPLETE_TYPE_P (t))
23173 {
23174 if (complain & tf_error)
23175 error ("explicit instantiation of %q#T before definition of template",
23176 t);
23177 return;
23178 }
23179
23180 if (storage != NULL_TREE)
23181 {
23182 if (!in_system_header_at (input_location))
23183 {
23184 if (storage == ridpointers[(int) RID_EXTERN])
23185 {
23186 if (cxx_dialect == cxx98)
23187 pedwarn (input_location, OPT_Wpedantic,
23188 "ISO C++ 1998 forbids the use of %<extern%> on "
23189 "explicit instantiations");
23190 }
23191 else
23192 pedwarn (input_location, OPT_Wpedantic,
23193 "ISO C++ forbids the use of %qE"
23194 " on explicit instantiations", storage);
23195 }
23196
23197 if (storage == ridpointers[(int) RID_INLINE])
23198 nomem_p = 1;
23199 else if (storage == ridpointers[(int) RID_EXTERN])
23200 extern_p = 1;
23201 else if (storage == ridpointers[(int) RID_STATIC])
23202 static_p = 1;
23203 else
23204 {
23205 error ("storage class %qD applied to template instantiation",
23206 storage);
23207 extern_p = 0;
23208 }
23209 }
23210
23211 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
23212 {
23213 /* DR 259 [temp.spec].
23214
23215 Both an explicit instantiation and a declaration of an explicit
23216 specialization shall not appear in a program unless the explicit
23217 instantiation follows a declaration of the explicit specialization.
23218
23219 For a given set of template parameters, if an explicit
23220 instantiation of a template appears after a declaration of an
23221 explicit specialization for that template, the explicit
23222 instantiation has no effect. */
23223 return;
23224 }
23225 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
23226 {
23227 /* [temp.spec]
23228
23229 No program shall explicitly instantiate any template more
23230 than once.
23231
23232 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23233 instantiation was `extern'. If EXTERN_P then the second is.
23234 These cases are OK. */
23235 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
23236
23237 if (!previous_instantiation_extern_p && !extern_p
23238 && (complain & tf_error))
23239 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
23240
23241 /* If we've already instantiated the template, just return now. */
23242 if (!CLASSTYPE_INTERFACE_ONLY (t))
23243 return;
23244 }
23245
23246 check_explicit_instantiation_namespace (TYPE_NAME (t));
23247 mark_class_instantiated (t, extern_p);
23248
23249 if (nomem_p)
23250 return;
23251
23252 /* In contrast to implicit instantiation, where only the
23253 declarations, and not the definitions, of members are
23254 instantiated, we have here:
23255
23256 [temp.explicit]
23257
23258 The explicit instantiation of a class template specialization
23259 implies the instantiation of all of its members not
23260 previously explicitly specialized in the translation unit
23261 containing the explicit instantiation.
23262
23263 Of course, we can't instantiate member template classes, since we
23264 don't have any arguments for them. Note that the standard is
23265 unclear on whether the instantiation of the members are
23266 *explicit* instantiations or not. However, the most natural
23267 interpretation is that it should be an explicit
23268 instantiation. */
23269 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
23270 if ((VAR_P (fld)
23271 || (TREE_CODE (fld) == FUNCTION_DECL
23272 && !static_p
23273 && user_provided_p (fld)))
23274 && DECL_TEMPLATE_INSTANTIATION (fld))
23275 {
23276 mark_decl_instantiated (fld, extern_p);
23277 if (! extern_p)
23278 instantiate_decl (fld, /*defer_ok=*/true,
23279 /*expl_inst_class_mem_p=*/true);
23280 }
23281
23282 if (CLASSTYPE_NESTED_UTDS (t))
23283 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
23284 bt_instantiate_type_proc, &storage);
23285 }
23286
23287 /* Given a function DECL, which is a specialization of TMPL, modify
23288 DECL to be a re-instantiation of TMPL with the same template
23289 arguments. TMPL should be the template into which tsubst'ing
23290 should occur for DECL, not the most general template.
23291
23292 One reason for doing this is a scenario like this:
23293
23294 template <class T>
23295 void f(const T&, int i);
23296
23297 void g() { f(3, 7); }
23298
23299 template <class T>
23300 void f(const T& t, const int i) { }
23301
23302 Note that when the template is first instantiated, with
23303 instantiate_template, the resulting DECL will have no name for the
23304 first parameter, and the wrong type for the second. So, when we go
23305 to instantiate the DECL, we regenerate it. */
23306
23307 static void
23308 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
23309 {
23310 /* The arguments used to instantiate DECL, from the most general
23311 template. */
23312 tree code_pattern;
23313
23314 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
23315
23316 /* Make sure that we can see identifiers, and compute access
23317 correctly. */
23318 push_access_scope (decl);
23319
23320 if (TREE_CODE (decl) == FUNCTION_DECL)
23321 {
23322 tree decl_parm;
23323 tree pattern_parm;
23324 tree specs;
23325 int args_depth;
23326 int parms_depth;
23327
23328 args_depth = TMPL_ARGS_DEPTH (args);
23329 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
23330 if (args_depth > parms_depth)
23331 args = get_innermost_template_args (args, parms_depth);
23332
23333 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
23334 args, tf_error, NULL_TREE,
23335 /*defer_ok*/false);
23336 if (specs && specs != error_mark_node)
23337 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
23338 specs);
23339
23340 /* Merge parameter declarations. */
23341 decl_parm = skip_artificial_parms_for (decl,
23342 DECL_ARGUMENTS (decl));
23343 pattern_parm
23344 = skip_artificial_parms_for (code_pattern,
23345 DECL_ARGUMENTS (code_pattern));
23346 while (decl_parm && !DECL_PACK_P (pattern_parm))
23347 {
23348 tree parm_type;
23349 tree attributes;
23350
23351 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23352 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
23353 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
23354 NULL_TREE);
23355 parm_type = type_decays_to (parm_type);
23356 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23357 TREE_TYPE (decl_parm) = parm_type;
23358 attributes = DECL_ATTRIBUTES (pattern_parm);
23359 if (DECL_ATTRIBUTES (decl_parm) != attributes)
23360 {
23361 DECL_ATTRIBUTES (decl_parm) = attributes;
23362 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
23363 }
23364 decl_parm = DECL_CHAIN (decl_parm);
23365 pattern_parm = DECL_CHAIN (pattern_parm);
23366 }
23367 /* Merge any parameters that match with the function parameter
23368 pack. */
23369 if (pattern_parm && DECL_PACK_P (pattern_parm))
23370 {
23371 int i, len;
23372 tree expanded_types;
23373 /* Expand the TYPE_PACK_EXPANSION that provides the types for
23374 the parameters in this function parameter pack. */
23375 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
23376 args, tf_error, NULL_TREE);
23377 len = TREE_VEC_LENGTH (expanded_types);
23378 for (i = 0; i < len; i++)
23379 {
23380 tree parm_type;
23381 tree attributes;
23382
23383 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23384 /* Rename the parameter to include the index. */
23385 DECL_NAME (decl_parm) =
23386 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
23387 parm_type = TREE_VEC_ELT (expanded_types, i);
23388 parm_type = type_decays_to (parm_type);
23389 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23390 TREE_TYPE (decl_parm) = parm_type;
23391 attributes = DECL_ATTRIBUTES (pattern_parm);
23392 if (DECL_ATTRIBUTES (decl_parm) != attributes)
23393 {
23394 DECL_ATTRIBUTES (decl_parm) = attributes;
23395 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
23396 }
23397 decl_parm = DECL_CHAIN (decl_parm);
23398 }
23399 }
23400 /* Merge additional specifiers from the CODE_PATTERN. */
23401 if (DECL_DECLARED_INLINE_P (code_pattern)
23402 && !DECL_DECLARED_INLINE_P (decl))
23403 DECL_DECLARED_INLINE_P (decl) = 1;
23404 }
23405 else if (VAR_P (decl))
23406 {
23407 start_lambda_scope (decl);
23408 DECL_INITIAL (decl) =
23409 tsubst_expr (DECL_INITIAL (code_pattern), args,
23410 tf_error, DECL_TI_TEMPLATE (decl),
23411 /*integral_constant_expression_p=*/false);
23412 finish_lambda_scope ();
23413 if (VAR_HAD_UNKNOWN_BOUND (decl))
23414 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
23415 tf_error, DECL_TI_TEMPLATE (decl));
23416 }
23417 else
23418 gcc_unreachable ();
23419
23420 pop_access_scope (decl);
23421 }
23422
23423 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
23424 substituted to get DECL. */
23425
23426 tree
23427 template_for_substitution (tree decl)
23428 {
23429 tree tmpl = DECL_TI_TEMPLATE (decl);
23430
23431 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
23432 for the instantiation. This is not always the most general
23433 template. Consider, for example:
23434
23435 template <class T>
23436 struct S { template <class U> void f();
23437 template <> void f<int>(); };
23438
23439 and an instantiation of S<double>::f<int>. We want TD to be the
23440 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23441 while (/* An instantiation cannot have a definition, so we need a
23442 more general template. */
23443 DECL_TEMPLATE_INSTANTIATION (tmpl)
23444 /* We must also deal with friend templates. Given:
23445
23446 template <class T> struct S {
23447 template <class U> friend void f() {};
23448 };
23449
23450 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23451 so far as the language is concerned, but that's still
23452 where we get the pattern for the instantiation from. On
23453 other hand, if the definition comes outside the class, say:
23454
23455 template <class T> struct S {
23456 template <class U> friend void f();
23457 };
23458 template <class U> friend void f() {}
23459
23460 we don't need to look any further. That's what the check for
23461 DECL_INITIAL is for. */
23462 || (TREE_CODE (decl) == FUNCTION_DECL
23463 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
23464 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
23465 {
23466 /* The present template, TD, should not be a definition. If it
23467 were a definition, we should be using it! Note that we
23468 cannot restructure the loop to just keep going until we find
23469 a template with a definition, since that might go too far if
23470 a specialization was declared, but not defined. */
23471
23472 /* Fetch the more general template. */
23473 tmpl = DECL_TI_TEMPLATE (tmpl);
23474 }
23475
23476 return tmpl;
23477 }
23478
23479 /* Returns true if we need to instantiate this template instance even if we
23480 know we aren't going to emit it. */
23481
23482 bool
23483 always_instantiate_p (tree decl)
23484 {
23485 /* We always instantiate inline functions so that we can inline them. An
23486 explicit instantiation declaration prohibits implicit instantiation of
23487 non-inline functions. With high levels of optimization, we would
23488 normally inline non-inline functions -- but we're not allowed to do
23489 that for "extern template" functions. Therefore, we check
23490 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23491 return ((TREE_CODE (decl) == FUNCTION_DECL
23492 && (DECL_DECLARED_INLINE_P (decl)
23493 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
23494 /* And we need to instantiate static data members so that
23495 their initializers are available in integral constant
23496 expressions. */
23497 || (VAR_P (decl)
23498 && decl_maybe_constant_var_p (decl)));
23499 }
23500
23501 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23502 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23503 error, true otherwise. */
23504
23505 bool
23506 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
23507 {
23508 tree fntype, spec, noex, clone;
23509
23510 /* Don't instantiate a noexcept-specification from template context. */
23511 if (processing_template_decl
23512 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
23513 return true;
23514
23515 if (DECL_CLONED_FUNCTION_P (fn))
23516 fn = DECL_CLONED_FUNCTION (fn);
23517 fntype = TREE_TYPE (fn);
23518 spec = TYPE_RAISES_EXCEPTIONS (fntype);
23519
23520 if (!spec || !TREE_PURPOSE (spec))
23521 return true;
23522
23523 noex = TREE_PURPOSE (spec);
23524
23525 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
23526 {
23527 static hash_set<tree>* fns = new hash_set<tree>;
23528 bool added = false;
23529 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
23530 spec = get_defaulted_eh_spec (fn, complain);
23531 else if (!(added = !fns->add (fn)))
23532 {
23533 /* If hash_set::add returns true, the element was already there. */
23534 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
23535 DECL_SOURCE_LOCATION (fn));
23536 error_at (loc,
23537 "exception specification of %qD depends on itself",
23538 fn);
23539 spec = noexcept_false_spec;
23540 }
23541 else if (push_tinst_level (fn))
23542 {
23543 push_access_scope (fn);
23544 push_deferring_access_checks (dk_no_deferred);
23545 input_location = DECL_SOURCE_LOCATION (fn);
23546 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
23547 DEFERRED_NOEXCEPT_ARGS (noex),
23548 tf_warning_or_error, fn,
23549 /*function_p=*/false,
23550 /*integral_constant_expression_p=*/true);
23551 spec = build_noexcept_spec (noex, tf_warning_or_error);
23552 pop_deferring_access_checks ();
23553 pop_access_scope (fn);
23554 pop_tinst_level ();
23555 if (spec == error_mark_node)
23556 spec = noexcept_false_spec;
23557 }
23558 else
23559 spec = noexcept_false_spec;
23560
23561 if (added)
23562 fns->remove (fn);
23563
23564 if (spec == error_mark_node)
23565 return false;
23566
23567 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
23568 }
23569
23570 FOR_EACH_CLONE (clone, fn)
23571 {
23572 if (TREE_TYPE (clone) == fntype)
23573 TREE_TYPE (clone) = TREE_TYPE (fn);
23574 else
23575 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
23576 }
23577
23578 return true;
23579 }
23580
23581 /* We're starting to process the function INST, an instantiation of PATTERN;
23582 add their parameters to local_specializations. */
23583
23584 static void
23585 register_parameter_specializations (tree pattern, tree inst)
23586 {
23587 tree tmpl_parm = DECL_ARGUMENTS (pattern);
23588 tree spec_parm = DECL_ARGUMENTS (inst);
23589 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
23590 {
23591 register_local_specialization (spec_parm, tmpl_parm);
23592 spec_parm = skip_artificial_parms_for (inst, spec_parm);
23593 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
23594 }
23595 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23596 {
23597 if (!DECL_PACK_P (tmpl_parm))
23598 {
23599 register_local_specialization (spec_parm, tmpl_parm);
23600 spec_parm = DECL_CHAIN (spec_parm);
23601 }
23602 else
23603 {
23604 /* Register the (value) argument pack as a specialization of
23605 TMPL_PARM, then move on. */
23606 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23607 register_local_specialization (argpack, tmpl_parm);
23608 }
23609 }
23610 gcc_assert (!spec_parm);
23611 }
23612
23613 /* Produce the definition of D, a _DECL generated from a template. If
23614 DEFER_OK is true, then we don't have to actually do the
23615 instantiation now; we just have to do it sometime. Normally it is
23616 an error if this is an explicit instantiation but D is undefined.
23617 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23618 instantiated class template. */
23619
23620 tree
23621 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
23622 {
23623 tree tmpl = DECL_TI_TEMPLATE (d);
23624 tree gen_args;
23625 tree args;
23626 tree td;
23627 tree code_pattern;
23628 tree spec;
23629 tree gen_tmpl;
23630 bool pattern_defined;
23631 location_t saved_loc = input_location;
23632 int saved_unevaluated_operand = cp_unevaluated_operand;
23633 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23634 bool external_p;
23635 bool deleted_p;
23636
23637 /* This function should only be used to instantiate templates for
23638 functions and static member variables. */
23639 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23640
23641 /* A concept is never instantiated. */
23642 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23643
23644 /* Variables are never deferred; if instantiation is required, they
23645 are instantiated right away. That allows for better code in the
23646 case that an expression refers to the value of the variable --
23647 if the variable has a constant value the referring expression can
23648 take advantage of that fact. */
23649 if (VAR_P (d))
23650 defer_ok = false;
23651
23652 /* Don't instantiate cloned functions. Instead, instantiate the
23653 functions they cloned. */
23654 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23655 d = DECL_CLONED_FUNCTION (d);
23656
23657 if (DECL_TEMPLATE_INSTANTIATED (d)
23658 || (TREE_CODE (d) == FUNCTION_DECL
23659 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23660 || DECL_TEMPLATE_SPECIALIZATION (d))
23661 /* D has already been instantiated or explicitly specialized, so
23662 there's nothing for us to do here.
23663
23664 It might seem reasonable to check whether or not D is an explicit
23665 instantiation, and, if so, stop here. But when an explicit
23666 instantiation is deferred until the end of the compilation,
23667 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23668 the instantiation. */
23669 return d;
23670
23671 /* Check to see whether we know that this template will be
23672 instantiated in some other file, as with "extern template"
23673 extension. */
23674 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23675
23676 /* In general, we do not instantiate such templates. */
23677 if (external_p && !always_instantiate_p (d))
23678 return d;
23679
23680 gen_tmpl = most_general_template (tmpl);
23681 gen_args = DECL_TI_ARGS (d);
23682
23683 if (tmpl != gen_tmpl)
23684 /* We should already have the extra args. */
23685 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23686 == TMPL_ARGS_DEPTH (gen_args));
23687 /* And what's in the hash table should match D. */
23688 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23689 || spec == NULL_TREE);
23690
23691 /* This needs to happen before any tsubsting. */
23692 if (! push_tinst_level (d))
23693 return d;
23694
23695 timevar_push (TV_TEMPLATE_INST);
23696
23697 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23698 for the instantiation. */
23699 td = template_for_substitution (d);
23700 args = gen_args;
23701
23702 if (VAR_P (d))
23703 {
23704 /* Look up an explicit specialization, if any. */
23705 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23706 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23707 if (elt && elt != error_mark_node)
23708 {
23709 td = TREE_VALUE (elt);
23710 args = TREE_PURPOSE (elt);
23711 }
23712 }
23713
23714 code_pattern = DECL_TEMPLATE_RESULT (td);
23715
23716 /* We should never be trying to instantiate a member of a class
23717 template or partial specialization. */
23718 gcc_assert (d != code_pattern);
23719
23720 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23721 || DECL_TEMPLATE_SPECIALIZATION (td))
23722 /* In the case of a friend template whose definition is provided
23723 outside the class, we may have too many arguments. Drop the
23724 ones we don't need. The same is true for specializations. */
23725 args = get_innermost_template_args
23726 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23727
23728 if (TREE_CODE (d) == FUNCTION_DECL)
23729 {
23730 deleted_p = DECL_DELETED_FN (code_pattern);
23731 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23732 && DECL_INITIAL (code_pattern) != error_mark_node)
23733 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23734 || deleted_p);
23735 }
23736 else
23737 {
23738 deleted_p = false;
23739 if (DECL_CLASS_SCOPE_P (code_pattern))
23740 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23741 || DECL_INLINE_VAR_P (code_pattern));
23742 else
23743 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23744 }
23745
23746 /* We may be in the middle of deferred access check. Disable it now. */
23747 push_deferring_access_checks (dk_no_deferred);
23748
23749 /* Unless an explicit instantiation directive has already determined
23750 the linkage of D, remember that a definition is available for
23751 this entity. */
23752 if (pattern_defined
23753 && !DECL_INTERFACE_KNOWN (d)
23754 && !DECL_NOT_REALLY_EXTERN (d))
23755 mark_definable (d);
23756
23757 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23758 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23759 input_location = DECL_SOURCE_LOCATION (d);
23760
23761 /* If D is a member of an explicitly instantiated class template,
23762 and no definition is available, treat it like an implicit
23763 instantiation. */
23764 if (!pattern_defined && expl_inst_class_mem_p
23765 && DECL_EXPLICIT_INSTANTIATION (d))
23766 {
23767 /* Leave linkage flags alone on instantiations with anonymous
23768 visibility. */
23769 if (TREE_PUBLIC (d))
23770 {
23771 DECL_NOT_REALLY_EXTERN (d) = 0;
23772 DECL_INTERFACE_KNOWN (d) = 0;
23773 }
23774 SET_DECL_IMPLICIT_INSTANTIATION (d);
23775 }
23776
23777 /* Defer all other templates, unless we have been explicitly
23778 forbidden from doing so. */
23779 if (/* If there is no definition, we cannot instantiate the
23780 template. */
23781 ! pattern_defined
23782 /* If it's OK to postpone instantiation, do so. */
23783 || defer_ok
23784 /* If this is a static data member that will be defined
23785 elsewhere, we don't want to instantiate the entire data
23786 member, but we do want to instantiate the initializer so that
23787 we can substitute that elsewhere. */
23788 || (external_p && VAR_P (d))
23789 /* Handle here a deleted function too, avoid generating
23790 its body (c++/61080). */
23791 || deleted_p)
23792 {
23793 /* The definition of the static data member is now required so
23794 we must substitute the initializer. */
23795 if (VAR_P (d)
23796 && !DECL_INITIAL (d)
23797 && DECL_INITIAL (code_pattern))
23798 {
23799 tree ns;
23800 tree init;
23801 bool const_init = false;
23802 bool enter_context = DECL_CLASS_SCOPE_P (d);
23803
23804 ns = decl_namespace_context (d);
23805 push_nested_namespace (ns);
23806 if (enter_context)
23807 push_nested_class (DECL_CONTEXT (d));
23808 init = tsubst_expr (DECL_INITIAL (code_pattern),
23809 args,
23810 tf_warning_or_error, NULL_TREE,
23811 /*integral_constant_expression_p=*/false);
23812 /* If instantiating the initializer involved instantiating this
23813 again, don't call cp_finish_decl twice. */
23814 if (!DECL_INITIAL (d))
23815 {
23816 /* Make sure the initializer is still constant, in case of
23817 circular dependency (template/instantiate6.C). */
23818 const_init
23819 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23820 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23821 /*asmspec_tree=*/NULL_TREE,
23822 LOOKUP_ONLYCONVERTING);
23823 }
23824 if (enter_context)
23825 pop_nested_class ();
23826 pop_nested_namespace (ns);
23827 }
23828
23829 /* We restore the source position here because it's used by
23830 add_pending_template. */
23831 input_location = saved_loc;
23832
23833 if (at_eof && !pattern_defined
23834 && DECL_EXPLICIT_INSTANTIATION (d)
23835 && DECL_NOT_REALLY_EXTERN (d))
23836 /* [temp.explicit]
23837
23838 The definition of a non-exported function template, a
23839 non-exported member function template, or a non-exported
23840 member function or static data member of a class template
23841 shall be present in every translation unit in which it is
23842 explicitly instantiated. */
23843 permerror (input_location, "explicit instantiation of %qD "
23844 "but no definition available", d);
23845
23846 /* If we're in unevaluated context, we just wanted to get the
23847 constant value; this isn't an odr use, so don't queue
23848 a full instantiation. */
23849 if (cp_unevaluated_operand != 0)
23850 goto out;
23851 /* ??? Historically, we have instantiated inline functions, even
23852 when marked as "extern template". */
23853 if (!(external_p && VAR_P (d)))
23854 add_pending_template (d);
23855 goto out;
23856 }
23857 /* Tell the repository that D is available in this translation unit
23858 -- and see if it is supposed to be instantiated here. */
23859 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23860 {
23861 /* In a PCH file, despite the fact that the repository hasn't
23862 requested instantiation in the PCH it is still possible that
23863 an instantiation will be required in a file that includes the
23864 PCH. */
23865 if (pch_file)
23866 add_pending_template (d);
23867 /* Instantiate inline functions so that the inliner can do its
23868 job, even though we'll not be emitting a copy of this
23869 function. */
23870 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23871 goto out;
23872 }
23873
23874 bool push_to_top, nested;
23875 tree fn_context;
23876 fn_context = decl_function_context (d);
23877 if (LAMBDA_FUNCTION_P (d))
23878 /* tsubst_lambda_expr resolved any references to enclosing functions. */
23879 fn_context = NULL_TREE;
23880 nested = current_function_decl != NULL_TREE;
23881 push_to_top = !(nested && fn_context == current_function_decl);
23882
23883 vec<tree> omp_privatization_save;
23884 if (nested)
23885 save_omp_privatization_clauses (omp_privatization_save);
23886
23887 if (push_to_top)
23888 push_to_top_level ();
23889 else
23890 {
23891 gcc_assert (!processing_template_decl);
23892 push_function_context ();
23893 cp_unevaluated_operand = 0;
23894 c_inhibit_evaluation_warnings = 0;
23895 }
23896
23897 /* Mark D as instantiated so that recursive calls to
23898 instantiate_decl do not try to instantiate it again. */
23899 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23900
23901 /* Regenerate the declaration in case the template has been modified
23902 by a subsequent redeclaration. */
23903 regenerate_decl_from_template (d, td, args);
23904
23905 /* We already set the file and line above. Reset them now in case
23906 they changed as a result of calling regenerate_decl_from_template. */
23907 input_location = DECL_SOURCE_LOCATION (d);
23908
23909 if (VAR_P (d))
23910 {
23911 tree init;
23912 bool const_init = false;
23913
23914 /* Clear out DECL_RTL; whatever was there before may not be right
23915 since we've reset the type of the declaration. */
23916 SET_DECL_RTL (d, NULL);
23917 DECL_IN_AGGR_P (d) = 0;
23918
23919 /* The initializer is placed in DECL_INITIAL by
23920 regenerate_decl_from_template so we don't need to
23921 push/pop_access_scope again here. Pull it out so that
23922 cp_finish_decl can process it. */
23923 init = DECL_INITIAL (d);
23924 DECL_INITIAL (d) = NULL_TREE;
23925 DECL_INITIALIZED_P (d) = 0;
23926
23927 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23928 initializer. That function will defer actual emission until
23929 we have a chance to determine linkage. */
23930 DECL_EXTERNAL (d) = 0;
23931
23932 /* Enter the scope of D so that access-checking works correctly. */
23933 bool enter_context = DECL_CLASS_SCOPE_P (d);
23934 if (enter_context)
23935 push_nested_class (DECL_CONTEXT (d));
23936
23937 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23938 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23939
23940 if (enter_context)
23941 pop_nested_class ();
23942
23943 if (variable_template_p (gen_tmpl))
23944 note_variable_template_instantiation (d);
23945 }
23946 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23947 synthesize_method (d);
23948 else if (TREE_CODE (d) == FUNCTION_DECL)
23949 {
23950 /* Set up the list of local specializations. */
23951 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23952 tree block = NULL_TREE;
23953
23954 /* Set up context. */
23955 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23956 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23957 block = push_stmt_list ();
23958 else
23959 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23960
23961 /* Some typedefs referenced from within the template code need to be
23962 access checked at template instantiation time, i.e now. These
23963 types were added to the template at parsing time. Let's get those
23964 and perform the access checks then. */
23965 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23966 args);
23967
23968 /* Create substitution entries for the parameters. */
23969 register_parameter_specializations (code_pattern, d);
23970
23971 /* Substitute into the body of the function. */
23972 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23973 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23974 tf_warning_or_error, tmpl);
23975 else
23976 {
23977 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23978 tf_warning_or_error, tmpl,
23979 /*integral_constant_expression_p=*/false);
23980
23981 /* Set the current input_location to the end of the function
23982 so that finish_function knows where we are. */
23983 input_location
23984 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23985
23986 /* Remember if we saw an infinite loop in the template. */
23987 current_function_infinite_loop
23988 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23989 }
23990
23991 /* Finish the function. */
23992 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23993 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23994 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23995 else
23996 {
23997 d = finish_function (/*inline_p=*/false);
23998 expand_or_defer_fn (d);
23999 }
24000
24001 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24002 cp_check_omp_declare_reduction (d);
24003 }
24004
24005 /* We're not deferring instantiation any more. */
24006 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
24007
24008 if (push_to_top)
24009 pop_from_top_level ();
24010 else
24011 pop_function_context ();
24012
24013 if (nested)
24014 restore_omp_privatization_clauses (omp_privatization_save);
24015
24016 out:
24017 pop_deferring_access_checks ();
24018 timevar_pop (TV_TEMPLATE_INST);
24019 pop_tinst_level ();
24020 input_location = saved_loc;
24021 cp_unevaluated_operand = saved_unevaluated_operand;
24022 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24023
24024 return d;
24025 }
24026
24027 /* Run through the list of templates that we wish we could
24028 instantiate, and instantiate any we can. RETRIES is the
24029 number of times we retry pending template instantiation. */
24030
24031 void
24032 instantiate_pending_templates (int retries)
24033 {
24034 int reconsider;
24035 location_t saved_loc = input_location;
24036
24037 /* Instantiating templates may trigger vtable generation. This in turn
24038 may require further template instantiations. We place a limit here
24039 to avoid infinite loop. */
24040 if (pending_templates && retries >= max_tinst_depth)
24041 {
24042 tree decl = pending_templates->tinst->maybe_get_node ();
24043
24044 fatal_error (input_location,
24045 "template instantiation depth exceeds maximum of %d"
24046 " instantiating %q+D, possibly from virtual table generation"
24047 " (use -ftemplate-depth= to increase the maximum)",
24048 max_tinst_depth, decl);
24049 if (TREE_CODE (decl) == FUNCTION_DECL)
24050 /* Pretend that we defined it. */
24051 DECL_INITIAL (decl) = error_mark_node;
24052 return;
24053 }
24054
24055 do
24056 {
24057 struct pending_template **t = &pending_templates;
24058 struct pending_template *last = NULL;
24059 reconsider = 0;
24060 while (*t)
24061 {
24062 tree instantiation = reopen_tinst_level ((*t)->tinst);
24063 bool complete = false;
24064
24065 if (TYPE_P (instantiation))
24066 {
24067 if (!COMPLETE_TYPE_P (instantiation))
24068 {
24069 instantiate_class_template (instantiation);
24070 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
24071 for (tree fld = TYPE_FIELDS (instantiation);
24072 fld; fld = TREE_CHAIN (fld))
24073 if ((VAR_P (fld)
24074 || (TREE_CODE (fld) == FUNCTION_DECL
24075 && !DECL_ARTIFICIAL (fld)))
24076 && DECL_TEMPLATE_INSTANTIATION (fld))
24077 instantiate_decl (fld,
24078 /*defer_ok=*/false,
24079 /*expl_inst_class_mem_p=*/false);
24080
24081 if (COMPLETE_TYPE_P (instantiation))
24082 reconsider = 1;
24083 }
24084
24085 complete = COMPLETE_TYPE_P (instantiation);
24086 }
24087 else
24088 {
24089 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
24090 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
24091 {
24092 instantiation
24093 = instantiate_decl (instantiation,
24094 /*defer_ok=*/false,
24095 /*expl_inst_class_mem_p=*/false);
24096 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
24097 reconsider = 1;
24098 }
24099
24100 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
24101 || DECL_TEMPLATE_INSTANTIATED (instantiation));
24102 }
24103
24104 if (complete)
24105 {
24106 /* If INSTANTIATION has been instantiated, then we don't
24107 need to consider it again in the future. */
24108 struct pending_template *drop = *t;
24109 *t = (*t)->next;
24110 set_refcount_ptr (drop->tinst);
24111 pending_template_freelist ().free (drop);
24112 }
24113 else
24114 {
24115 last = *t;
24116 t = &(*t)->next;
24117 }
24118 tinst_depth = 0;
24119 set_refcount_ptr (current_tinst_level);
24120 }
24121 last_pending_template = last;
24122 }
24123 while (reconsider);
24124
24125 input_location = saved_loc;
24126 }
24127
24128 /* Substitute ARGVEC into T, which is a list of initializers for
24129 either base class or a non-static data member. The TREE_PURPOSEs
24130 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24131 instantiate_decl. */
24132
24133 static tree
24134 tsubst_initializer_list (tree t, tree argvec)
24135 {
24136 tree inits = NULL_TREE;
24137 tree target_ctor = error_mark_node;
24138
24139 for (; t; t = TREE_CHAIN (t))
24140 {
24141 tree decl;
24142 tree init;
24143 tree expanded_bases = NULL_TREE;
24144 tree expanded_arguments = NULL_TREE;
24145 int i, len = 1;
24146
24147 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
24148 {
24149 tree expr;
24150 tree arg;
24151
24152 /* Expand the base class expansion type into separate base
24153 classes. */
24154 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
24155 tf_warning_or_error,
24156 NULL_TREE);
24157 if (expanded_bases == error_mark_node)
24158 continue;
24159
24160 /* We'll be building separate TREE_LISTs of arguments for
24161 each base. */
24162 len = TREE_VEC_LENGTH (expanded_bases);
24163 expanded_arguments = make_tree_vec (len);
24164 for (i = 0; i < len; i++)
24165 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
24166
24167 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24168 expand each argument in the TREE_VALUE of t. */
24169 expr = make_node (EXPR_PACK_EXPANSION);
24170 PACK_EXPANSION_LOCAL_P (expr) = true;
24171 PACK_EXPANSION_PARAMETER_PACKS (expr) =
24172 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
24173
24174 if (TREE_VALUE (t) == void_type_node)
24175 /* VOID_TYPE_NODE is used to indicate
24176 value-initialization. */
24177 {
24178 for (i = 0; i < len; i++)
24179 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
24180 }
24181 else
24182 {
24183 /* Substitute parameter packs into each argument in the
24184 TREE_LIST. */
24185 in_base_initializer = 1;
24186 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
24187 {
24188 tree expanded_exprs;
24189
24190 /* Expand the argument. */
24191 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
24192 expanded_exprs
24193 = tsubst_pack_expansion (expr, argvec,
24194 tf_warning_or_error,
24195 NULL_TREE);
24196 if (expanded_exprs == error_mark_node)
24197 continue;
24198
24199 /* Prepend each of the expanded expressions to the
24200 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
24201 for (i = 0; i < len; i++)
24202 {
24203 TREE_VEC_ELT (expanded_arguments, i) =
24204 tree_cons (NULL_TREE,
24205 TREE_VEC_ELT (expanded_exprs, i),
24206 TREE_VEC_ELT (expanded_arguments, i));
24207 }
24208 }
24209 in_base_initializer = 0;
24210
24211 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
24212 since we built them backwards. */
24213 for (i = 0; i < len; i++)
24214 {
24215 TREE_VEC_ELT (expanded_arguments, i) =
24216 nreverse (TREE_VEC_ELT (expanded_arguments, i));
24217 }
24218 }
24219 }
24220
24221 for (i = 0; i < len; ++i)
24222 {
24223 if (expanded_bases)
24224 {
24225 decl = TREE_VEC_ELT (expanded_bases, i);
24226 decl = expand_member_init (decl);
24227 init = TREE_VEC_ELT (expanded_arguments, i);
24228 }
24229 else
24230 {
24231 tree tmp;
24232 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
24233 tf_warning_or_error, NULL_TREE);
24234
24235 decl = expand_member_init (decl);
24236 if (decl && !DECL_P (decl))
24237 in_base_initializer = 1;
24238
24239 init = TREE_VALUE (t);
24240 tmp = init;
24241 if (init != void_type_node)
24242 init = tsubst_expr (init, argvec,
24243 tf_warning_or_error, NULL_TREE,
24244 /*integral_constant_expression_p=*/false);
24245 if (init == NULL_TREE && tmp != NULL_TREE)
24246 /* If we had an initializer but it instantiated to nothing,
24247 value-initialize the object. This will only occur when
24248 the initializer was a pack expansion where the parameter
24249 packs used in that expansion were of length zero. */
24250 init = void_type_node;
24251 in_base_initializer = 0;
24252 }
24253
24254 if (target_ctor != error_mark_node
24255 && init != error_mark_node)
24256 {
24257 error ("mem-initializer for %qD follows constructor delegation",
24258 decl);
24259 return inits;
24260 }
24261 /* Look for a target constructor. */
24262 if (init != error_mark_node
24263 && decl && CLASS_TYPE_P (decl)
24264 && same_type_p (decl, current_class_type))
24265 {
24266 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
24267 if (inits)
24268 {
24269 error ("constructor delegation follows mem-initializer for %qD",
24270 TREE_PURPOSE (inits));
24271 continue;
24272 }
24273 target_ctor = init;
24274 }
24275
24276 if (decl)
24277 {
24278 init = build_tree_list (decl, init);
24279 TREE_CHAIN (init) = inits;
24280 inits = init;
24281 }
24282 }
24283 }
24284 return inits;
24285 }
24286
24287 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
24288
24289 static void
24290 set_current_access_from_decl (tree decl)
24291 {
24292 if (TREE_PRIVATE (decl))
24293 current_access_specifier = access_private_node;
24294 else if (TREE_PROTECTED (decl))
24295 current_access_specifier = access_protected_node;
24296 else
24297 current_access_specifier = access_public_node;
24298 }
24299
24300 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
24301 is the instantiation (which should have been created with
24302 start_enum) and ARGS are the template arguments to use. */
24303
24304 static void
24305 tsubst_enum (tree tag, tree newtag, tree args)
24306 {
24307 tree e;
24308
24309 if (SCOPED_ENUM_P (newtag))
24310 begin_scope (sk_scoped_enum, newtag);
24311
24312 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
24313 {
24314 tree value;
24315 tree decl;
24316
24317 decl = TREE_VALUE (e);
24318 /* Note that in a template enum, the TREE_VALUE is the
24319 CONST_DECL, not the corresponding INTEGER_CST. */
24320 value = tsubst_expr (DECL_INITIAL (decl),
24321 args, tf_warning_or_error, NULL_TREE,
24322 /*integral_constant_expression_p=*/true);
24323
24324 /* Give this enumeration constant the correct access. */
24325 set_current_access_from_decl (decl);
24326
24327 /* Actually build the enumerator itself. Here we're assuming that
24328 enumerators can't have dependent attributes. */
24329 build_enumerator (DECL_NAME (decl), value, newtag,
24330 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
24331 }
24332
24333 if (SCOPED_ENUM_P (newtag))
24334 finish_scope ();
24335
24336 finish_enum_value_list (newtag);
24337 finish_enum (newtag);
24338
24339 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
24340 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
24341 }
24342
24343 /* DECL is a FUNCTION_DECL that is a template specialization. Return
24344 its type -- but without substituting the innermost set of template
24345 arguments. So, innermost set of template parameters will appear in
24346 the type. */
24347
24348 tree
24349 get_mostly_instantiated_function_type (tree decl)
24350 {
24351 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
24352 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
24353 }
24354
24355 /* Return truthvalue if we're processing a template different from
24356 the last one involved in diagnostics. */
24357 bool
24358 problematic_instantiation_changed (void)
24359 {
24360 return current_tinst_level != last_error_tinst_level;
24361 }
24362
24363 /* Remember current template involved in diagnostics. */
24364 void
24365 record_last_problematic_instantiation (void)
24366 {
24367 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
24368 }
24369
24370 struct tinst_level *
24371 current_instantiation (void)
24372 {
24373 return current_tinst_level;
24374 }
24375
24376 /* Return TRUE if current_function_decl is being instantiated, false
24377 otherwise. */
24378
24379 bool
24380 instantiating_current_function_p (void)
24381 {
24382 return (current_instantiation ()
24383 && (current_instantiation ()->maybe_get_node ()
24384 == current_function_decl));
24385 }
24386
24387 /* [temp.param] Check that template non-type parm TYPE is of an allowable
24388 type. Return false for ok, true for disallowed. Issue error and
24389 inform messages under control of COMPLAIN. */
24390
24391 static bool
24392 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
24393 {
24394 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
24395 return false;
24396 else if (TYPE_PTR_P (type))
24397 return false;
24398 else if (TYPE_REF_P (type)
24399 && !TYPE_REF_IS_RVALUE (type))
24400 return false;
24401 else if (TYPE_PTRMEM_P (type))
24402 return false;
24403 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
24404 return false;
24405 else if (TREE_CODE (type) == TYPENAME_TYPE)
24406 return false;
24407 else if (TREE_CODE (type) == DECLTYPE_TYPE)
24408 return false;
24409 else if (TREE_CODE (type) == NULLPTR_TYPE)
24410 return false;
24411 /* A bound template template parm could later be instantiated to have a valid
24412 nontype parm type via an alias template. */
24413 else if (cxx_dialect >= cxx11
24414 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24415 return false;
24416
24417 if (complain & tf_error)
24418 {
24419 if (type == error_mark_node)
24420 inform (input_location, "invalid template non-type parameter");
24421 else
24422 error ("%q#T is not a valid type for a template non-type parameter",
24423 type);
24424 }
24425 return true;
24426 }
24427
24428 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
24429 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
24430
24431 static bool
24432 dependent_type_p_r (tree type)
24433 {
24434 tree scope;
24435
24436 /* [temp.dep.type]
24437
24438 A type is dependent if it is:
24439
24440 -- a template parameter. Template template parameters are types
24441 for us (since TYPE_P holds true for them) so we handle
24442 them here. */
24443 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
24444 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
24445 return true;
24446 /* -- a qualified-id with a nested-name-specifier which contains a
24447 class-name that names a dependent type or whose unqualified-id
24448 names a dependent type. */
24449 if (TREE_CODE (type) == TYPENAME_TYPE)
24450 return true;
24451
24452 /* An alias template specialization can be dependent even if the
24453 resulting type is not. */
24454 if (dependent_alias_template_spec_p (type))
24455 return true;
24456
24457 /* -- a cv-qualified type where the cv-unqualified type is
24458 dependent.
24459 No code is necessary for this bullet; the code below handles
24460 cv-qualified types, and we don't want to strip aliases with
24461 TYPE_MAIN_VARIANT because of DR 1558. */
24462 /* -- a compound type constructed from any dependent type. */
24463 if (TYPE_PTRMEM_P (type))
24464 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
24465 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24466 (type)));
24467 else if (TYPE_PTR_P (type)
24468 || TYPE_REF_P (type))
24469 return dependent_type_p (TREE_TYPE (type));
24470 else if (TREE_CODE (type) == FUNCTION_TYPE
24471 || TREE_CODE (type) == METHOD_TYPE)
24472 {
24473 tree arg_type;
24474
24475 if (dependent_type_p (TREE_TYPE (type)))
24476 return true;
24477 for (arg_type = TYPE_ARG_TYPES (type);
24478 arg_type;
24479 arg_type = TREE_CHAIN (arg_type))
24480 if (dependent_type_p (TREE_VALUE (arg_type)))
24481 return true;
24482 if (cxx_dialect >= cxx17)
24483 /* A value-dependent noexcept-specifier makes the type dependent. */
24484 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
24485 if (tree noex = TREE_PURPOSE (spec))
24486 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24487 affect overload resolution and treating it as dependent breaks
24488 things. */
24489 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
24490 && value_dependent_expression_p (noex))
24491 return true;
24492 return false;
24493 }
24494 /* -- an array type constructed from any dependent type or whose
24495 size is specified by a constant expression that is
24496 value-dependent.
24497
24498 We checked for type- and value-dependence of the bounds in
24499 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24500 if (TREE_CODE (type) == ARRAY_TYPE)
24501 {
24502 if (TYPE_DOMAIN (type)
24503 && dependent_type_p (TYPE_DOMAIN (type)))
24504 return true;
24505 return dependent_type_p (TREE_TYPE (type));
24506 }
24507
24508 /* -- a template-id in which either the template name is a template
24509 parameter ... */
24510 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24511 return true;
24512 /* ... or any of the template arguments is a dependent type or
24513 an expression that is type-dependent or value-dependent. */
24514 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
24515 && (any_dependent_template_arguments_p
24516 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
24517 return true;
24518
24519 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24520 dependent; if the argument of the `typeof' expression is not
24521 type-dependent, then it should already been have resolved. */
24522 if (TREE_CODE (type) == TYPEOF_TYPE
24523 || TREE_CODE (type) == DECLTYPE_TYPE
24524 || TREE_CODE (type) == UNDERLYING_TYPE)
24525 return true;
24526
24527 /* A template argument pack is dependent if any of its packed
24528 arguments are. */
24529 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
24530 {
24531 tree args = ARGUMENT_PACK_ARGS (type);
24532 int i, len = TREE_VEC_LENGTH (args);
24533 for (i = 0; i < len; ++i)
24534 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24535 return true;
24536 }
24537
24538 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24539 be template parameters. */
24540 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
24541 return true;
24542
24543 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
24544 return true;
24545
24546 /* The standard does not specifically mention types that are local
24547 to template functions or local classes, but they should be
24548 considered dependent too. For example:
24549
24550 template <int I> void f() {
24551 enum E { a = I };
24552 S<sizeof (E)> s;
24553 }
24554
24555 The size of `E' cannot be known until the value of `I' has been
24556 determined. Therefore, `E' must be considered dependent. */
24557 scope = TYPE_CONTEXT (type);
24558 if (scope && TYPE_P (scope))
24559 return dependent_type_p (scope);
24560 /* Don't use type_dependent_expression_p here, as it can lead
24561 to infinite recursion trying to determine whether a lambda
24562 nested in a lambda is dependent (c++/47687). */
24563 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
24564 && DECL_LANG_SPECIFIC (scope)
24565 && DECL_TEMPLATE_INFO (scope)
24566 && (any_dependent_template_arguments_p
24567 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
24568 return true;
24569
24570 /* Other types are non-dependent. */
24571 return false;
24572 }
24573
24574 /* Returns TRUE if TYPE is dependent, in the sense of
24575 [temp.dep.type]. Note that a NULL type is considered dependent. */
24576
24577 bool
24578 dependent_type_p (tree type)
24579 {
24580 /* If there are no template parameters in scope, then there can't be
24581 any dependent types. */
24582 if (!processing_template_decl)
24583 {
24584 /* If we are not processing a template, then nobody should be
24585 providing us with a dependent type. */
24586 gcc_assert (type);
24587 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
24588 return false;
24589 }
24590
24591 /* If the type is NULL, we have not computed a type for the entity
24592 in question; in that case, the type is dependent. */
24593 if (!type)
24594 return true;
24595
24596 /* Erroneous types can be considered non-dependent. */
24597 if (type == error_mark_node)
24598 return false;
24599
24600 /* Getting here with global_type_node means we improperly called this
24601 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24602 gcc_checking_assert (type != global_type_node);
24603
24604 /* If we have not already computed the appropriate value for TYPE,
24605 do so now. */
24606 if (!TYPE_DEPENDENT_P_VALID (type))
24607 {
24608 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
24609 TYPE_DEPENDENT_P_VALID (type) = 1;
24610 }
24611
24612 return TYPE_DEPENDENT_P (type);
24613 }
24614
24615 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24616 lookup. In other words, a dependent type that is not the current
24617 instantiation. */
24618
24619 bool
24620 dependent_scope_p (tree scope)
24621 {
24622 return (scope && TYPE_P (scope) && dependent_type_p (scope)
24623 && !currently_open_class (scope));
24624 }
24625
24626 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24627 an unknown base of 'this' (and is therefore instantiation-dependent). */
24628
24629 static bool
24630 unknown_base_ref_p (tree t)
24631 {
24632 if (!current_class_ptr)
24633 return false;
24634
24635 tree mem = TREE_OPERAND (t, 1);
24636 if (shared_member_p (mem))
24637 return false;
24638
24639 tree cur = current_nonlambda_class_type ();
24640 if (!any_dependent_bases_p (cur))
24641 return false;
24642
24643 tree ctx = TREE_OPERAND (t, 0);
24644 if (DERIVED_FROM_P (ctx, cur))
24645 return false;
24646
24647 return true;
24648 }
24649
24650 /* T is a SCOPE_REF; return whether we need to consider it
24651 instantiation-dependent so that we can check access at instantiation
24652 time even though we know which member it resolves to. */
24653
24654 static bool
24655 instantiation_dependent_scope_ref_p (tree t)
24656 {
24657 if (DECL_P (TREE_OPERAND (t, 1))
24658 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
24659 && !unknown_base_ref_p (t)
24660 && accessible_in_template_p (TREE_OPERAND (t, 0),
24661 TREE_OPERAND (t, 1)))
24662 return false;
24663 else
24664 return true;
24665 }
24666
24667 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24668 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24669 expression. */
24670
24671 /* Note that this predicate is not appropriate for general expressions;
24672 only constant expressions (that satisfy potential_constant_expression)
24673 can be tested for value dependence. */
24674
24675 bool
24676 value_dependent_expression_p (tree expression)
24677 {
24678 if (!processing_template_decl || expression == NULL_TREE)
24679 return false;
24680
24681 /* A type-dependent expression is also value-dependent. */
24682 if (type_dependent_expression_p (expression))
24683 return true;
24684
24685 switch (TREE_CODE (expression))
24686 {
24687 case BASELINK:
24688 /* A dependent member function of the current instantiation. */
24689 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24690
24691 case FUNCTION_DECL:
24692 /* A dependent member function of the current instantiation. */
24693 if (DECL_CLASS_SCOPE_P (expression)
24694 && dependent_type_p (DECL_CONTEXT (expression)))
24695 return true;
24696 break;
24697
24698 case IDENTIFIER_NODE:
24699 /* A name that has not been looked up -- must be dependent. */
24700 return true;
24701
24702 case TEMPLATE_PARM_INDEX:
24703 /* A non-type template parm. */
24704 return true;
24705
24706 case CONST_DECL:
24707 /* A non-type template parm. */
24708 if (DECL_TEMPLATE_PARM_P (expression))
24709 return true;
24710 return value_dependent_expression_p (DECL_INITIAL (expression));
24711
24712 case VAR_DECL:
24713 /* A constant with literal type and is initialized
24714 with an expression that is value-dependent. */
24715 if (DECL_DEPENDENT_INIT_P (expression)
24716 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24717 || TYPE_REF_P (TREE_TYPE (expression)))
24718 return true;
24719 if (DECL_HAS_VALUE_EXPR_P (expression))
24720 {
24721 tree value_expr = DECL_VALUE_EXPR (expression);
24722 if (value_dependent_expression_p (value_expr))
24723 return true;
24724 }
24725 return false;
24726
24727 case DYNAMIC_CAST_EXPR:
24728 case STATIC_CAST_EXPR:
24729 case CONST_CAST_EXPR:
24730 case REINTERPRET_CAST_EXPR:
24731 case CAST_EXPR:
24732 case IMPLICIT_CONV_EXPR:
24733 /* These expressions are value-dependent if the type to which
24734 the cast occurs is dependent or the expression being casted
24735 is value-dependent. */
24736 {
24737 tree type = TREE_TYPE (expression);
24738
24739 if (dependent_type_p (type))
24740 return true;
24741
24742 /* A functional cast has a list of operands. */
24743 expression = TREE_OPERAND (expression, 0);
24744 if (!expression)
24745 {
24746 /* If there are no operands, it must be an expression such
24747 as "int()". This should not happen for aggregate types
24748 because it would form non-constant expressions. */
24749 gcc_assert (cxx_dialect >= cxx11
24750 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24751
24752 return false;
24753 }
24754
24755 if (TREE_CODE (expression) == TREE_LIST)
24756 return any_value_dependent_elements_p (expression);
24757
24758 return value_dependent_expression_p (expression);
24759 }
24760
24761 case SIZEOF_EXPR:
24762 if (SIZEOF_EXPR_TYPE_P (expression))
24763 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24764 /* FALLTHRU */
24765 case ALIGNOF_EXPR:
24766 case TYPEID_EXPR:
24767 /* A `sizeof' expression is value-dependent if the operand is
24768 type-dependent or is a pack expansion. */
24769 expression = TREE_OPERAND (expression, 0);
24770 if (PACK_EXPANSION_P (expression))
24771 return true;
24772 else if (TYPE_P (expression))
24773 return dependent_type_p (expression);
24774 return instantiation_dependent_uneval_expression_p (expression);
24775
24776 case AT_ENCODE_EXPR:
24777 /* An 'encode' expression is value-dependent if the operand is
24778 type-dependent. */
24779 expression = TREE_OPERAND (expression, 0);
24780 return dependent_type_p (expression);
24781
24782 case NOEXCEPT_EXPR:
24783 expression = TREE_OPERAND (expression, 0);
24784 return instantiation_dependent_uneval_expression_p (expression);
24785
24786 case SCOPE_REF:
24787 /* All instantiation-dependent expressions should also be considered
24788 value-dependent. */
24789 return instantiation_dependent_scope_ref_p (expression);
24790
24791 case COMPONENT_REF:
24792 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24793 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24794
24795 case NONTYPE_ARGUMENT_PACK:
24796 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24797 is value-dependent. */
24798 {
24799 tree values = ARGUMENT_PACK_ARGS (expression);
24800 int i, len = TREE_VEC_LENGTH (values);
24801
24802 for (i = 0; i < len; ++i)
24803 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24804 return true;
24805
24806 return false;
24807 }
24808
24809 case TRAIT_EXPR:
24810 {
24811 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24812
24813 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24814 return true;
24815
24816 if (!type2)
24817 return false;
24818
24819 if (TREE_CODE (type2) != TREE_LIST)
24820 return dependent_type_p (type2);
24821
24822 for (; type2; type2 = TREE_CHAIN (type2))
24823 if (dependent_type_p (TREE_VALUE (type2)))
24824 return true;
24825
24826 return false;
24827 }
24828
24829 case MODOP_EXPR:
24830 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24831 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24832
24833 case ARRAY_REF:
24834 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24835 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24836
24837 case ADDR_EXPR:
24838 {
24839 tree op = TREE_OPERAND (expression, 0);
24840 return (value_dependent_expression_p (op)
24841 || has_value_dependent_address (op));
24842 }
24843
24844 case REQUIRES_EXPR:
24845 /* Treat all requires-expressions as value-dependent so
24846 we don't try to fold them. */
24847 return true;
24848
24849 case TYPE_REQ:
24850 return dependent_type_p (TREE_OPERAND (expression, 0));
24851
24852 case CALL_EXPR:
24853 {
24854 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24855 return true;
24856 tree fn = get_callee_fndecl (expression);
24857 int i, nargs;
24858 nargs = call_expr_nargs (expression);
24859 for (i = 0; i < nargs; ++i)
24860 {
24861 tree op = CALL_EXPR_ARG (expression, i);
24862 /* In a call to a constexpr member function, look through the
24863 implicit ADDR_EXPR on the object argument so that it doesn't
24864 cause the call to be considered value-dependent. We also
24865 look through it in potential_constant_expression. */
24866 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24867 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24868 && TREE_CODE (op) == ADDR_EXPR)
24869 op = TREE_OPERAND (op, 0);
24870 if (value_dependent_expression_p (op))
24871 return true;
24872 }
24873 return false;
24874 }
24875
24876 case TEMPLATE_ID_EXPR:
24877 return variable_concept_p (TREE_OPERAND (expression, 0));
24878
24879 case CONSTRUCTOR:
24880 {
24881 unsigned ix;
24882 tree val;
24883 if (dependent_type_p (TREE_TYPE (expression)))
24884 return true;
24885 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24886 if (value_dependent_expression_p (val))
24887 return true;
24888 return false;
24889 }
24890
24891 case STMT_EXPR:
24892 /* Treat a GNU statement expression as dependent to avoid crashing
24893 under instantiate_non_dependent_expr; it can't be constant. */
24894 return true;
24895
24896 default:
24897 /* A constant expression is value-dependent if any subexpression is
24898 value-dependent. */
24899 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24900 {
24901 case tcc_reference:
24902 case tcc_unary:
24903 case tcc_comparison:
24904 case tcc_binary:
24905 case tcc_expression:
24906 case tcc_vl_exp:
24907 {
24908 int i, len = cp_tree_operand_length (expression);
24909
24910 for (i = 0; i < len; i++)
24911 {
24912 tree t = TREE_OPERAND (expression, i);
24913
24914 /* In some cases, some of the operands may be missing.
24915 (For example, in the case of PREDECREMENT_EXPR, the
24916 amount to increment by may be missing.) That doesn't
24917 make the expression dependent. */
24918 if (t && value_dependent_expression_p (t))
24919 return true;
24920 }
24921 }
24922 break;
24923 default:
24924 break;
24925 }
24926 break;
24927 }
24928
24929 /* The expression is not value-dependent. */
24930 return false;
24931 }
24932
24933 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24934 [temp.dep.expr]. Note that an expression with no type is
24935 considered dependent. Other parts of the compiler arrange for an
24936 expression with type-dependent subexpressions to have no type, so
24937 this function doesn't have to be fully recursive. */
24938
24939 bool
24940 type_dependent_expression_p (tree expression)
24941 {
24942 if (!processing_template_decl)
24943 return false;
24944
24945 if (expression == NULL_TREE || expression == error_mark_node)
24946 return false;
24947
24948 STRIP_ANY_LOCATION_WRAPPER (expression);
24949
24950 /* An unresolved name is always dependent. */
24951 if (identifier_p (expression)
24952 || TREE_CODE (expression) == USING_DECL
24953 || TREE_CODE (expression) == WILDCARD_DECL)
24954 return true;
24955
24956 /* A fold expression is type-dependent. */
24957 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24958 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24959 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24960 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24961 return true;
24962
24963 /* Some expression forms are never type-dependent. */
24964 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24965 || TREE_CODE (expression) == SIZEOF_EXPR
24966 || TREE_CODE (expression) == ALIGNOF_EXPR
24967 || TREE_CODE (expression) == AT_ENCODE_EXPR
24968 || TREE_CODE (expression) == NOEXCEPT_EXPR
24969 || TREE_CODE (expression) == TRAIT_EXPR
24970 || TREE_CODE (expression) == TYPEID_EXPR
24971 || TREE_CODE (expression) == DELETE_EXPR
24972 || TREE_CODE (expression) == VEC_DELETE_EXPR
24973 || TREE_CODE (expression) == THROW_EXPR
24974 || TREE_CODE (expression) == REQUIRES_EXPR)
24975 return false;
24976
24977 /* The types of these expressions depends only on the type to which
24978 the cast occurs. */
24979 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24980 || TREE_CODE (expression) == STATIC_CAST_EXPR
24981 || TREE_CODE (expression) == CONST_CAST_EXPR
24982 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24983 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24984 || TREE_CODE (expression) == CAST_EXPR)
24985 return dependent_type_p (TREE_TYPE (expression));
24986
24987 /* The types of these expressions depends only on the type created
24988 by the expression. */
24989 if (TREE_CODE (expression) == NEW_EXPR
24990 || TREE_CODE (expression) == VEC_NEW_EXPR)
24991 {
24992 /* For NEW_EXPR tree nodes created inside a template, either
24993 the object type itself or a TREE_LIST may appear as the
24994 operand 1. */
24995 tree type = TREE_OPERAND (expression, 1);
24996 if (TREE_CODE (type) == TREE_LIST)
24997 /* This is an array type. We need to check array dimensions
24998 as well. */
24999 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
25000 || value_dependent_expression_p
25001 (TREE_OPERAND (TREE_VALUE (type), 1));
25002 else
25003 return dependent_type_p (type);
25004 }
25005
25006 if (TREE_CODE (expression) == SCOPE_REF)
25007 {
25008 tree scope = TREE_OPERAND (expression, 0);
25009 tree name = TREE_OPERAND (expression, 1);
25010
25011 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25012 contains an identifier associated by name lookup with one or more
25013 declarations declared with a dependent type, or...a
25014 nested-name-specifier or qualified-id that names a member of an
25015 unknown specialization. */
25016 return (type_dependent_expression_p (name)
25017 || dependent_scope_p (scope));
25018 }
25019
25020 if (TREE_CODE (expression) == TEMPLATE_DECL
25021 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
25022 return uses_outer_template_parms (expression);
25023
25024 if (TREE_CODE (expression) == STMT_EXPR)
25025 expression = stmt_expr_value_expr (expression);
25026
25027 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
25028 {
25029 tree elt;
25030 unsigned i;
25031
25032 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
25033 {
25034 if (type_dependent_expression_p (elt))
25035 return true;
25036 }
25037 return false;
25038 }
25039
25040 /* A static data member of the current instantiation with incomplete
25041 array type is type-dependent, as the definition and specializations
25042 can have different bounds. */
25043 if (VAR_P (expression)
25044 && DECL_CLASS_SCOPE_P (expression)
25045 && dependent_type_p (DECL_CONTEXT (expression))
25046 && VAR_HAD_UNKNOWN_BOUND (expression))
25047 return true;
25048
25049 /* An array of unknown bound depending on a variadic parameter, eg:
25050
25051 template<typename... Args>
25052 void foo (Args... args)
25053 {
25054 int arr[] = { args... };
25055 }
25056
25057 template<int... vals>
25058 void bar ()
25059 {
25060 int arr[] = { vals... };
25061 }
25062
25063 If the array has no length and has an initializer, it must be that
25064 we couldn't determine its length in cp_complete_array_type because
25065 it is dependent. */
25066 if (VAR_P (expression)
25067 && TREE_TYPE (expression) != NULL_TREE
25068 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
25069 && !TYPE_DOMAIN (TREE_TYPE (expression))
25070 && DECL_INITIAL (expression))
25071 return true;
25072
25073 /* A function or variable template-id is type-dependent if it has any
25074 dependent template arguments. */
25075 if (VAR_OR_FUNCTION_DECL_P (expression)
25076 && DECL_LANG_SPECIFIC (expression)
25077 && DECL_TEMPLATE_INFO (expression))
25078 {
25079 /* Consider the innermost template arguments, since those are the ones
25080 that come from the template-id; the template arguments for the
25081 enclosing class do not make it type-dependent unless they are used in
25082 the type of the decl. */
25083 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
25084 && (any_dependent_template_arguments_p
25085 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
25086 return true;
25087 }
25088
25089 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25090 type-dependent. Checking this is important for functions with auto return
25091 type, which looks like a dependent type. */
25092 if (TREE_CODE (expression) == FUNCTION_DECL
25093 && !(DECL_CLASS_SCOPE_P (expression)
25094 && dependent_type_p (DECL_CONTEXT (expression)))
25095 && !(DECL_LANG_SPECIFIC (expression)
25096 && DECL_FRIEND_P (expression)
25097 && (!DECL_FRIEND_CONTEXT (expression)
25098 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
25099 && !DECL_LOCAL_FUNCTION_P (expression))
25100 {
25101 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
25102 || undeduced_auto_decl (expression));
25103 return false;
25104 }
25105
25106 /* Always dependent, on the number of arguments if nothing else. */
25107 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
25108 return true;
25109
25110 if (TREE_TYPE (expression) == unknown_type_node)
25111 {
25112 if (TREE_CODE (expression) == ADDR_EXPR)
25113 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
25114 if (TREE_CODE (expression) == COMPONENT_REF
25115 || TREE_CODE (expression) == OFFSET_REF)
25116 {
25117 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
25118 return true;
25119 expression = TREE_OPERAND (expression, 1);
25120 if (identifier_p (expression))
25121 return false;
25122 }
25123 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25124 if (TREE_CODE (expression) == SCOPE_REF)
25125 return false;
25126
25127 if (BASELINK_P (expression))
25128 {
25129 if (BASELINK_OPTYPE (expression)
25130 && dependent_type_p (BASELINK_OPTYPE (expression)))
25131 return true;
25132 expression = BASELINK_FUNCTIONS (expression);
25133 }
25134
25135 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
25136 {
25137 if (any_dependent_template_arguments_p
25138 (TREE_OPERAND (expression, 1)))
25139 return true;
25140 expression = TREE_OPERAND (expression, 0);
25141 if (identifier_p (expression))
25142 return true;
25143 }
25144
25145 gcc_assert (TREE_CODE (expression) == OVERLOAD
25146 || TREE_CODE (expression) == FUNCTION_DECL);
25147
25148 for (lkp_iterator iter (expression); iter; ++iter)
25149 if (type_dependent_expression_p (*iter))
25150 return true;
25151
25152 return false;
25153 }
25154
25155 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
25156
25157 /* Dependent type attributes might not have made it from the decl to
25158 the type yet. */
25159 if (DECL_P (expression)
25160 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
25161 return true;
25162
25163 return (dependent_type_p (TREE_TYPE (expression)));
25164 }
25165
25166 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
25167 type-dependent if the expression refers to a member of the current
25168 instantiation and the type of the referenced member is dependent, or the
25169 class member access expression refers to a member of an unknown
25170 specialization.
25171
25172 This function returns true if the OBJECT in such a class member access
25173 expression is of an unknown specialization. */
25174
25175 bool
25176 type_dependent_object_expression_p (tree object)
25177 {
25178 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
25179 dependent. */
25180 if (TREE_CODE (object) == IDENTIFIER_NODE)
25181 return true;
25182 tree scope = TREE_TYPE (object);
25183 return (!scope || dependent_scope_p (scope));
25184 }
25185
25186 /* walk_tree callback function for instantiation_dependent_expression_p,
25187 below. Returns non-zero if a dependent subexpression is found. */
25188
25189 static tree
25190 instantiation_dependent_r (tree *tp, int *walk_subtrees,
25191 void * /*data*/)
25192 {
25193 if (TYPE_P (*tp))
25194 {
25195 /* We don't have to worry about decltype currently because decltype
25196 of an instantiation-dependent expr is a dependent type. This
25197 might change depending on the resolution of DR 1172. */
25198 *walk_subtrees = false;
25199 return NULL_TREE;
25200 }
25201 enum tree_code code = TREE_CODE (*tp);
25202 switch (code)
25203 {
25204 /* Don't treat an argument list as dependent just because it has no
25205 TREE_TYPE. */
25206 case TREE_LIST:
25207 case TREE_VEC:
25208 return NULL_TREE;
25209
25210 case TEMPLATE_PARM_INDEX:
25211 return *tp;
25212
25213 /* Handle expressions with type operands. */
25214 case SIZEOF_EXPR:
25215 case ALIGNOF_EXPR:
25216 case TYPEID_EXPR:
25217 case AT_ENCODE_EXPR:
25218 {
25219 tree op = TREE_OPERAND (*tp, 0);
25220 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
25221 op = TREE_TYPE (op);
25222 if (TYPE_P (op))
25223 {
25224 if (dependent_type_p (op))
25225 return *tp;
25226 else
25227 {
25228 *walk_subtrees = false;
25229 return NULL_TREE;
25230 }
25231 }
25232 break;
25233 }
25234
25235 case COMPONENT_REF:
25236 if (identifier_p (TREE_OPERAND (*tp, 1)))
25237 /* In a template, finish_class_member_access_expr creates a
25238 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
25239 type-dependent, so that we can check access control at
25240 instantiation time (PR 42277). See also Core issue 1273. */
25241 return *tp;
25242 break;
25243
25244 case SCOPE_REF:
25245 if (instantiation_dependent_scope_ref_p (*tp))
25246 return *tp;
25247 else
25248 break;
25249
25250 /* Treat statement-expressions as dependent. */
25251 case BIND_EXPR:
25252 return *tp;
25253
25254 /* Treat requires-expressions as dependent. */
25255 case REQUIRES_EXPR:
25256 return *tp;
25257
25258 case CALL_EXPR:
25259 /* Treat calls to function concepts as dependent. */
25260 if (function_concept_check_p (*tp))
25261 return *tp;
25262 break;
25263
25264 case TEMPLATE_ID_EXPR:
25265 /* And variable concepts. */
25266 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
25267 return *tp;
25268 break;
25269
25270 default:
25271 break;
25272 }
25273
25274 if (type_dependent_expression_p (*tp))
25275 return *tp;
25276 else
25277 return NULL_TREE;
25278 }
25279
25280 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
25281 sense defined by the ABI:
25282
25283 "An expression is instantiation-dependent if it is type-dependent
25284 or value-dependent, or it has a subexpression that is type-dependent
25285 or value-dependent."
25286
25287 Except don't actually check value-dependence for unevaluated expressions,
25288 because in sizeof(i) we don't care about the value of i. Checking
25289 type-dependence will in turn check value-dependence of array bounds/template
25290 arguments as needed. */
25291
25292 bool
25293 instantiation_dependent_uneval_expression_p (tree expression)
25294 {
25295 tree result;
25296
25297 if (!processing_template_decl)
25298 return false;
25299
25300 if (expression == error_mark_node)
25301 return false;
25302
25303 result = cp_walk_tree_without_duplicates (&expression,
25304 instantiation_dependent_r, NULL);
25305 return result != NULL_TREE;
25306 }
25307
25308 /* As above, but also check value-dependence of the expression as a whole. */
25309
25310 bool
25311 instantiation_dependent_expression_p (tree expression)
25312 {
25313 return (instantiation_dependent_uneval_expression_p (expression)
25314 || value_dependent_expression_p (expression));
25315 }
25316
25317 /* Like type_dependent_expression_p, but it also works while not processing
25318 a template definition, i.e. during substitution or mangling. */
25319
25320 bool
25321 type_dependent_expression_p_push (tree expr)
25322 {
25323 bool b;
25324 ++processing_template_decl;
25325 b = type_dependent_expression_p (expr);
25326 --processing_template_decl;
25327 return b;
25328 }
25329
25330 /* Returns TRUE if ARGS contains a type-dependent expression. */
25331
25332 bool
25333 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
25334 {
25335 unsigned int i;
25336 tree arg;
25337
25338 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
25339 {
25340 if (type_dependent_expression_p (arg))
25341 return true;
25342 }
25343 return false;
25344 }
25345
25346 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25347 expressions) contains any type-dependent expressions. */
25348
25349 bool
25350 any_type_dependent_elements_p (const_tree list)
25351 {
25352 for (; list; list = TREE_CHAIN (list))
25353 if (type_dependent_expression_p (TREE_VALUE (list)))
25354 return true;
25355
25356 return false;
25357 }
25358
25359 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25360 expressions) contains any value-dependent expressions. */
25361
25362 bool
25363 any_value_dependent_elements_p (const_tree list)
25364 {
25365 for (; list; list = TREE_CHAIN (list))
25366 if (value_dependent_expression_p (TREE_VALUE (list)))
25367 return true;
25368
25369 return false;
25370 }
25371
25372 /* Returns TRUE if the ARG (a template argument) is dependent. */
25373
25374 bool
25375 dependent_template_arg_p (tree arg)
25376 {
25377 if (!processing_template_decl)
25378 return false;
25379
25380 /* Assume a template argument that was wrongly written by the user
25381 is dependent. This is consistent with what
25382 any_dependent_template_arguments_p [that calls this function]
25383 does. */
25384 if (!arg || arg == error_mark_node)
25385 return true;
25386
25387 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
25388 arg = argument_pack_select_arg (arg);
25389
25390 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
25391 return true;
25392 if (TREE_CODE (arg) == TEMPLATE_DECL)
25393 {
25394 if (DECL_TEMPLATE_PARM_P (arg))
25395 return true;
25396 /* A member template of a dependent class is not necessarily
25397 type-dependent, but it is a dependent template argument because it
25398 will be a member of an unknown specialization to that template. */
25399 tree scope = CP_DECL_CONTEXT (arg);
25400 return TYPE_P (scope) && dependent_type_p (scope);
25401 }
25402 else if (ARGUMENT_PACK_P (arg))
25403 {
25404 tree args = ARGUMENT_PACK_ARGS (arg);
25405 int i, len = TREE_VEC_LENGTH (args);
25406 for (i = 0; i < len; ++i)
25407 {
25408 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25409 return true;
25410 }
25411
25412 return false;
25413 }
25414 else if (TYPE_P (arg))
25415 return dependent_type_p (arg);
25416 else
25417 return (type_dependent_expression_p (arg)
25418 || value_dependent_expression_p (arg));
25419 }
25420
25421 /* Returns true if ARGS (a collection of template arguments) contains
25422 any types that require structural equality testing. */
25423
25424 bool
25425 any_template_arguments_need_structural_equality_p (tree args)
25426 {
25427 int i;
25428 int j;
25429
25430 if (!args)
25431 return false;
25432 if (args == error_mark_node)
25433 return true;
25434
25435 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25436 {
25437 tree level = TMPL_ARGS_LEVEL (args, i + 1);
25438 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25439 {
25440 tree arg = TREE_VEC_ELT (level, j);
25441 tree packed_args = NULL_TREE;
25442 int k, len = 1;
25443
25444 if (ARGUMENT_PACK_P (arg))
25445 {
25446 /* Look inside the argument pack. */
25447 packed_args = ARGUMENT_PACK_ARGS (arg);
25448 len = TREE_VEC_LENGTH (packed_args);
25449 }
25450
25451 for (k = 0; k < len; ++k)
25452 {
25453 if (packed_args)
25454 arg = TREE_VEC_ELT (packed_args, k);
25455
25456 if (error_operand_p (arg))
25457 return true;
25458 else if (TREE_CODE (arg) == TEMPLATE_DECL)
25459 continue;
25460 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
25461 return true;
25462 else if (!TYPE_P (arg) && TREE_TYPE (arg)
25463 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
25464 return true;
25465 }
25466 }
25467 }
25468
25469 return false;
25470 }
25471
25472 /* Returns true if ARGS (a collection of template arguments) contains
25473 any dependent arguments. */
25474
25475 bool
25476 any_dependent_template_arguments_p (const_tree args)
25477 {
25478 int i;
25479 int j;
25480
25481 if (!args)
25482 return false;
25483 if (args == error_mark_node)
25484 return true;
25485
25486 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25487 {
25488 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25489 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25490 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
25491 return true;
25492 }
25493
25494 return false;
25495 }
25496
25497 /* Returns true if ARGS contains any errors. */
25498
25499 bool
25500 any_erroneous_template_args_p (const_tree args)
25501 {
25502 int i;
25503 int j;
25504
25505 if (args == error_mark_node)
25506 return true;
25507
25508 if (args && TREE_CODE (args) != TREE_VEC)
25509 {
25510 if (tree ti = get_template_info (args))
25511 args = TI_ARGS (ti);
25512 else
25513 args = NULL_TREE;
25514 }
25515
25516 if (!args)
25517 return false;
25518
25519 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25520 {
25521 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25522 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25523 if (error_operand_p (TREE_VEC_ELT (level, j)))
25524 return true;
25525 }
25526
25527 return false;
25528 }
25529
25530 /* Returns TRUE if the template TMPL is type-dependent. */
25531
25532 bool
25533 dependent_template_p (tree tmpl)
25534 {
25535 if (TREE_CODE (tmpl) == OVERLOAD)
25536 {
25537 for (lkp_iterator iter (tmpl); iter; ++iter)
25538 if (dependent_template_p (*iter))
25539 return true;
25540 return false;
25541 }
25542
25543 /* Template template parameters are dependent. */
25544 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
25545 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
25546 return true;
25547 /* So are names that have not been looked up. */
25548 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
25549 return true;
25550 return false;
25551 }
25552
25553 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25554
25555 bool
25556 dependent_template_id_p (tree tmpl, tree args)
25557 {
25558 return (dependent_template_p (tmpl)
25559 || any_dependent_template_arguments_p (args));
25560 }
25561
25562 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25563 are dependent. */
25564
25565 bool
25566 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
25567 {
25568 int i;
25569
25570 if (!processing_template_decl)
25571 return false;
25572
25573 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
25574 {
25575 tree decl = TREE_VEC_ELT (declv, i);
25576 tree init = TREE_VEC_ELT (initv, i);
25577 tree cond = TREE_VEC_ELT (condv, i);
25578 tree incr = TREE_VEC_ELT (incrv, i);
25579
25580 if (type_dependent_expression_p (decl)
25581 || TREE_CODE (decl) == SCOPE_REF)
25582 return true;
25583
25584 if (init && type_dependent_expression_p (init))
25585 return true;
25586
25587 if (type_dependent_expression_p (cond))
25588 return true;
25589
25590 if (COMPARISON_CLASS_P (cond)
25591 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
25592 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
25593 return true;
25594
25595 if (TREE_CODE (incr) == MODOP_EXPR)
25596 {
25597 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
25598 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
25599 return true;
25600 }
25601 else if (type_dependent_expression_p (incr))
25602 return true;
25603 else if (TREE_CODE (incr) == MODIFY_EXPR)
25604 {
25605 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
25606 return true;
25607 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
25608 {
25609 tree t = TREE_OPERAND (incr, 1);
25610 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
25611 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
25612 return true;
25613 }
25614 }
25615 }
25616
25617 return false;
25618 }
25619
25620 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25621 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25622 no such TYPE can be found. Note that this function peers inside
25623 uninstantiated templates and therefore should be used only in
25624 extremely limited situations. ONLY_CURRENT_P restricts this
25625 peering to the currently open classes hierarchy (which is required
25626 when comparing types). */
25627
25628 tree
25629 resolve_typename_type (tree type, bool only_current_p)
25630 {
25631 tree scope;
25632 tree name;
25633 tree decl;
25634 int quals;
25635 tree pushed_scope;
25636 tree result;
25637
25638 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
25639
25640 scope = TYPE_CONTEXT (type);
25641 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25642 gcc_checking_assert (uses_template_parms (scope));
25643
25644 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25645 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25646 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25647 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25648 identifier of the TYPENAME_TYPE anymore.
25649 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25650 TYPENAME_TYPE instead, we avoid messing up with a possible
25651 typedef variant case. */
25652 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
25653
25654 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25655 it first before we can figure out what NAME refers to. */
25656 if (TREE_CODE (scope) == TYPENAME_TYPE)
25657 {
25658 if (TYPENAME_IS_RESOLVING_P (scope))
25659 /* Given a class template A with a dependent base with nested type C,
25660 typedef typename A::C::C C will land us here, as trying to resolve
25661 the initial A::C leads to the local C typedef, which leads back to
25662 A::C::C. So we break the recursion now. */
25663 return type;
25664 else
25665 scope = resolve_typename_type (scope, only_current_p);
25666 }
25667 /* If we don't know what SCOPE refers to, then we cannot resolve the
25668 TYPENAME_TYPE. */
25669 if (!CLASS_TYPE_P (scope))
25670 return type;
25671 /* If this is a typedef, we don't want to look inside (c++/11987). */
25672 if (typedef_variant_p (type))
25673 return type;
25674 /* If SCOPE isn't the template itself, it will not have a valid
25675 TYPE_FIELDS list. */
25676 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
25677 /* scope is either the template itself or a compatible instantiation
25678 like X<T>, so look up the name in the original template. */
25679 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
25680 /* If scope has no fields, it can't be a current instantiation. Check this
25681 before currently_open_class to avoid infinite recursion (71515). */
25682 if (!TYPE_FIELDS (scope))
25683 return type;
25684 /* If the SCOPE is not the current instantiation, there's no reason
25685 to look inside it. */
25686 if (only_current_p && !currently_open_class (scope))
25687 return type;
25688 /* Enter the SCOPE so that name lookup will be resolved as if we
25689 were in the class definition. In particular, SCOPE will no
25690 longer be considered a dependent type. */
25691 pushed_scope = push_scope (scope);
25692 /* Look up the declaration. */
25693 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
25694 tf_warning_or_error);
25695
25696 result = NULL_TREE;
25697
25698 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25699 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25700 tree fullname = TYPENAME_TYPE_FULLNAME (type);
25701 if (!decl)
25702 /*nop*/;
25703 else if (identifier_p (fullname)
25704 && TREE_CODE (decl) == TYPE_DECL)
25705 {
25706 result = TREE_TYPE (decl);
25707 if (result == error_mark_node)
25708 result = NULL_TREE;
25709 }
25710 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25711 && DECL_CLASS_TEMPLATE_P (decl))
25712 {
25713 /* Obtain the template and the arguments. */
25714 tree tmpl = TREE_OPERAND (fullname, 0);
25715 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25716 {
25717 /* We get here with a plain identifier because a previous tentative
25718 parse of the nested-name-specifier as part of a ptr-operator saw
25719 ::template X<A>. The use of ::template is necessary in a
25720 ptr-operator, but wrong in a declarator-id.
25721
25722 [temp.names]: In a qualified-id of a declarator-id, the keyword
25723 template shall not appear at the top level. */
25724 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
25725 "keyword %<template%> not allowed in declarator-id");
25726 tmpl = decl;
25727 }
25728 tree args = TREE_OPERAND (fullname, 1);
25729 /* Instantiate the template. */
25730 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25731 /*entering_scope=*/true,
25732 tf_error | tf_user);
25733 if (result == error_mark_node)
25734 result = NULL_TREE;
25735 }
25736
25737 /* Leave the SCOPE. */
25738 if (pushed_scope)
25739 pop_scope (pushed_scope);
25740
25741 /* If we failed to resolve it, return the original typename. */
25742 if (!result)
25743 return type;
25744
25745 /* If lookup found a typename type, resolve that too. */
25746 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25747 {
25748 /* Ill-formed programs can cause infinite recursion here, so we
25749 must catch that. */
25750 TYPENAME_IS_RESOLVING_P (result) = 1;
25751 result = resolve_typename_type (result, only_current_p);
25752 TYPENAME_IS_RESOLVING_P (result) = 0;
25753 }
25754
25755 /* Qualify the resulting type. */
25756 quals = cp_type_quals (type);
25757 if (quals)
25758 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25759
25760 return result;
25761 }
25762
25763 /* EXPR is an expression which is not type-dependent. Return a proxy
25764 for EXPR that can be used to compute the types of larger
25765 expressions containing EXPR. */
25766
25767 tree
25768 build_non_dependent_expr (tree expr)
25769 {
25770 tree orig_expr = expr;
25771 tree inner_expr;
25772
25773 /* When checking, try to get a constant value for all non-dependent
25774 expressions in order to expose bugs in *_dependent_expression_p
25775 and constexpr. This can affect code generation, see PR70704, so
25776 only do this for -fchecking=2. */
25777 if (flag_checking > 1
25778 && cxx_dialect >= cxx11
25779 /* Don't do this during nsdmi parsing as it can lead to
25780 unexpected recursive instantiations. */
25781 && !parsing_nsdmi ()
25782 /* Don't do this during concept expansion either and for
25783 the same reason. */
25784 && !expanding_concept ())
25785 fold_non_dependent_expr (expr);
25786
25787 STRIP_ANY_LOCATION_WRAPPER (expr);
25788
25789 /* Preserve OVERLOADs; the functions must be available to resolve
25790 types. */
25791 inner_expr = expr;
25792 if (TREE_CODE (inner_expr) == STMT_EXPR)
25793 inner_expr = stmt_expr_value_expr (inner_expr);
25794 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25795 inner_expr = TREE_OPERAND (inner_expr, 0);
25796 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25797 inner_expr = TREE_OPERAND (inner_expr, 1);
25798 if (is_overloaded_fn (inner_expr)
25799 || TREE_CODE (inner_expr) == OFFSET_REF)
25800 return orig_expr;
25801 /* There is no need to return a proxy for a variable. */
25802 if (VAR_P (expr))
25803 return orig_expr;
25804 /* Preserve string constants; conversions from string constants to
25805 "char *" are allowed, even though normally a "const char *"
25806 cannot be used to initialize a "char *". */
25807 if (TREE_CODE (expr) == STRING_CST)
25808 return orig_expr;
25809 /* Preserve void and arithmetic constants, as an optimization -- there is no
25810 reason to create a new node. */
25811 if (TREE_CODE (expr) == VOID_CST
25812 || TREE_CODE (expr) == INTEGER_CST
25813 || TREE_CODE (expr) == REAL_CST)
25814 return orig_expr;
25815 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25816 There is at least one place where we want to know that a
25817 particular expression is a throw-expression: when checking a ?:
25818 expression, there are special rules if the second or third
25819 argument is a throw-expression. */
25820 if (TREE_CODE (expr) == THROW_EXPR)
25821 return orig_expr;
25822
25823 /* Don't wrap an initializer list, we need to be able to look inside. */
25824 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25825 return orig_expr;
25826
25827 /* Don't wrap a dummy object, we need to be able to test for it. */
25828 if (is_dummy_object (expr))
25829 return orig_expr;
25830
25831 if (TREE_CODE (expr) == COND_EXPR)
25832 return build3 (COND_EXPR,
25833 TREE_TYPE (expr),
25834 TREE_OPERAND (expr, 0),
25835 (TREE_OPERAND (expr, 1)
25836 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25837 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25838 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25839 if (TREE_CODE (expr) == COMPOUND_EXPR
25840 && !COMPOUND_EXPR_OVERLOADED (expr))
25841 return build2 (COMPOUND_EXPR,
25842 TREE_TYPE (expr),
25843 TREE_OPERAND (expr, 0),
25844 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25845
25846 /* If the type is unknown, it can't really be non-dependent */
25847 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25848
25849 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25850 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
25851 TREE_TYPE (expr), expr);
25852 }
25853
25854 /* ARGS is a vector of expressions as arguments to a function call.
25855 Replace the arguments with equivalent non-dependent expressions.
25856 This modifies ARGS in place. */
25857
25858 void
25859 make_args_non_dependent (vec<tree, va_gc> *args)
25860 {
25861 unsigned int ix;
25862 tree arg;
25863
25864 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25865 {
25866 tree newarg = build_non_dependent_expr (arg);
25867 if (newarg != arg)
25868 (*args)[ix] = newarg;
25869 }
25870 }
25871
25872 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25873 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25874 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25875
25876 static tree
25877 make_auto_1 (tree name, bool set_canonical)
25878 {
25879 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25880 TYPE_NAME (au) = build_decl (input_location,
25881 TYPE_DECL, name, au);
25882 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25883 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25884 (0, processing_template_decl + 1, processing_template_decl + 1,
25885 TYPE_NAME (au), NULL_TREE);
25886 if (set_canonical)
25887 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25888 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25889 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25890
25891 return au;
25892 }
25893
25894 tree
25895 make_decltype_auto (void)
25896 {
25897 return make_auto_1 (decltype_auto_identifier, true);
25898 }
25899
25900 tree
25901 make_auto (void)
25902 {
25903 return make_auto_1 (auto_identifier, true);
25904 }
25905
25906 /* Return a C++17 deduction placeholder for class template TMPL. */
25907
25908 tree
25909 make_template_placeholder (tree tmpl)
25910 {
25911 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25912 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25913 return t;
25914 }
25915
25916 /* True iff T is a C++17 class template deduction placeholder. */
25917
25918 bool
25919 template_placeholder_p (tree t)
25920 {
25921 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25922 }
25923
25924 /* Make a "constrained auto" type-specifier. This is an
25925 auto type with constraints that must be associated after
25926 deduction. The constraint is formed from the given
25927 CONC and its optional sequence of arguments, which are
25928 non-null if written as partial-concept-id. */
25929
25930 tree
25931 make_constrained_auto (tree con, tree args)
25932 {
25933 tree type = make_auto_1 (auto_identifier, false);
25934
25935 /* Build the constraint. */
25936 tree tmpl = DECL_TI_TEMPLATE (con);
25937 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25938 expr = build_concept_check (expr, type, args);
25939
25940 tree constr = normalize_expression (expr);
25941 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25942
25943 /* Our canonical type depends on the constraint. */
25944 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25945
25946 /* Attach the constraint to the type declaration. */
25947 tree decl = TYPE_NAME (type);
25948 return decl;
25949 }
25950
25951 /* Given type ARG, return std::initializer_list<ARG>. */
25952
25953 static tree
25954 listify (tree arg)
25955 {
25956 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25957
25958 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25959 {
25960 gcc_rich_location richloc (input_location);
25961 maybe_add_include_fixit (&richloc, "<initializer_list>");
25962 error_at (&richloc,
25963 "deducing from brace-enclosed initializer list"
25964 " requires %<#include <initializer_list>%>");
25965
25966 return error_mark_node;
25967 }
25968 tree argvec = make_tree_vec (1);
25969 TREE_VEC_ELT (argvec, 0) = arg;
25970
25971 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25972 NULL_TREE, 0, tf_warning_or_error);
25973 }
25974
25975 /* Replace auto in TYPE with std::initializer_list<auto>. */
25976
25977 static tree
25978 listify_autos (tree type, tree auto_node)
25979 {
25980 tree init_auto = listify (auto_node);
25981 tree argvec = make_tree_vec (1);
25982 TREE_VEC_ELT (argvec, 0) = init_auto;
25983 if (processing_template_decl)
25984 argvec = add_to_template_args (current_template_args (), argvec);
25985 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25986 }
25987
25988 /* Hash traits for hashing possibly constrained 'auto'
25989 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25990
25991 struct auto_hash : default_hash_traits<tree>
25992 {
25993 static inline hashval_t hash (tree);
25994 static inline bool equal (tree, tree);
25995 };
25996
25997 /* Hash the 'auto' T. */
25998
25999 inline hashval_t
26000 auto_hash::hash (tree t)
26001 {
26002 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
26003 /* Matching constrained-type-specifiers denote the same template
26004 parameter, so hash the constraint. */
26005 return hash_placeholder_constraint (c);
26006 else
26007 /* But unconstrained autos are all separate, so just hash the pointer. */
26008 return iterative_hash_object (t, 0);
26009 }
26010
26011 /* Compare two 'auto's. */
26012
26013 inline bool
26014 auto_hash::equal (tree t1, tree t2)
26015 {
26016 if (t1 == t2)
26017 return true;
26018
26019 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
26020 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
26021
26022 /* Two unconstrained autos are distinct. */
26023 if (!c1 || !c2)
26024 return false;
26025
26026 return equivalent_placeholder_constraints (c1, c2);
26027 }
26028
26029 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26030 constrained) auto, add it to the vector. */
26031
26032 static int
26033 extract_autos_r (tree t, void *data)
26034 {
26035 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
26036 if (is_auto (t))
26037 {
26038 /* All the autos were built with index 0; fix that up now. */
26039 tree *p = hash.find_slot (t, INSERT);
26040 unsigned idx;
26041 if (*p)
26042 /* If this is a repeated constrained-type-specifier, use the index we
26043 chose before. */
26044 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
26045 else
26046 {
26047 /* Otherwise this is new, so use the current count. */
26048 *p = t;
26049 idx = hash.elements () - 1;
26050 }
26051 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
26052 }
26053
26054 /* Always keep walking. */
26055 return 0;
26056 }
26057
26058 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26059 says they can appear anywhere in the type. */
26060
26061 static tree
26062 extract_autos (tree type)
26063 {
26064 hash_set<tree> visited;
26065 hash_table<auto_hash> hash (2);
26066
26067 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
26068
26069 tree tree_vec = make_tree_vec (hash.elements());
26070 for (hash_table<auto_hash>::iterator iter = hash.begin();
26071 iter != hash.end(); ++iter)
26072 {
26073 tree elt = *iter;
26074 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
26075 TREE_VEC_ELT (tree_vec, i)
26076 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
26077 }
26078
26079 return tree_vec;
26080 }
26081
26082 /* The stem for deduction guide names. */
26083 const char *const dguide_base = "__dguide_";
26084
26085 /* Return the name for a deduction guide for class template TMPL. */
26086
26087 tree
26088 dguide_name (tree tmpl)
26089 {
26090 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
26091 tree tname = TYPE_IDENTIFIER (type);
26092 char *buf = (char *) alloca (1 + strlen (dguide_base)
26093 + IDENTIFIER_LENGTH (tname));
26094 memcpy (buf, dguide_base, strlen (dguide_base));
26095 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
26096 IDENTIFIER_LENGTH (tname) + 1);
26097 tree dname = get_identifier (buf);
26098 TREE_TYPE (dname) = type;
26099 return dname;
26100 }
26101
26102 /* True if NAME is the name of a deduction guide. */
26103
26104 bool
26105 dguide_name_p (tree name)
26106 {
26107 return (TREE_CODE (name) == IDENTIFIER_NODE
26108 && TREE_TYPE (name)
26109 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
26110 strlen (dguide_base)));
26111 }
26112
26113 /* True if FN is a deduction guide. */
26114
26115 bool
26116 deduction_guide_p (const_tree fn)
26117 {
26118 if (DECL_P (fn))
26119 if (tree name = DECL_NAME (fn))
26120 return dguide_name_p (name);
26121 return false;
26122 }
26123
26124 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26125
26126 bool
26127 copy_guide_p (const_tree fn)
26128 {
26129 gcc_assert (deduction_guide_p (fn));
26130 if (!DECL_ARTIFICIAL (fn))
26131 return false;
26132 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
26133 return (TREE_CHAIN (parms) == void_list_node
26134 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
26135 }
26136
26137 /* True if FN is a guide generated from a constructor template. */
26138
26139 bool
26140 template_guide_p (const_tree fn)
26141 {
26142 gcc_assert (deduction_guide_p (fn));
26143 if (!DECL_ARTIFICIAL (fn))
26144 return false;
26145 tree tmpl = DECL_TI_TEMPLATE (fn);
26146 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
26147 return PRIMARY_TEMPLATE_P (org);
26148 return false;
26149 }
26150
26151 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
26152 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
26153 template parameter types. Note that the handling of template template
26154 parameters relies on current_template_parms being set appropriately for the
26155 new template. */
26156
26157 static tree
26158 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
26159 tree tsubst_args, tsubst_flags_t complain)
26160 {
26161 if (olddecl == error_mark_node)
26162 return error_mark_node;
26163
26164 tree oldidx = get_template_parm_index (olddecl);
26165
26166 tree newtype;
26167 if (TREE_CODE (olddecl) == TYPE_DECL
26168 || TREE_CODE (olddecl) == TEMPLATE_DECL)
26169 {
26170 tree oldtype = TREE_TYPE (olddecl);
26171 newtype = cxx_make_type (TREE_CODE (oldtype));
26172 TYPE_MAIN_VARIANT (newtype) = newtype;
26173 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
26174 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
26175 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
26176 }
26177 else
26178 {
26179 newtype = TREE_TYPE (olddecl);
26180 if (type_uses_auto (newtype))
26181 {
26182 // Substitute once to fix references to other template parameters.
26183 newtype = tsubst (newtype, tsubst_args,
26184 complain|tf_partial, NULL_TREE);
26185 // Now substitute again to reduce the level of the auto.
26186 newtype = tsubst (newtype, current_template_args (),
26187 complain, NULL_TREE);
26188 }
26189 else
26190 newtype = tsubst (newtype, tsubst_args,
26191 complain, NULL_TREE);
26192 }
26193
26194 tree newdecl
26195 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
26196 DECL_NAME (olddecl), newtype);
26197 SET_DECL_TEMPLATE_PARM_P (newdecl);
26198
26199 tree newidx;
26200 if (TREE_CODE (olddecl) == TYPE_DECL
26201 || TREE_CODE (olddecl) == TEMPLATE_DECL)
26202 {
26203 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
26204 = build_template_parm_index (index, level, level,
26205 newdecl, newtype);
26206 TEMPLATE_PARM_PARAMETER_PACK (newidx)
26207 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
26208 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
26209 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
26210
26211 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
26212 {
26213 DECL_TEMPLATE_RESULT (newdecl)
26214 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
26215 DECL_NAME (olddecl), newtype);
26216 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
26217 // First create a copy (ttargs) of tsubst_args with an
26218 // additional level for the template template parameter's own
26219 // template parameters (ttparms).
26220 tree ttparms = (INNERMOST_TEMPLATE_PARMS
26221 (DECL_TEMPLATE_PARMS (olddecl)));
26222 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
26223 tree ttargs = make_tree_vec (depth + 1);
26224 for (int i = 0; i < depth; ++i)
26225 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
26226 TREE_VEC_ELT (ttargs, depth)
26227 = template_parms_level_to_args (ttparms);
26228 // Substitute ttargs into ttparms to fix references to
26229 // other template parameters.
26230 ttparms = tsubst_template_parms_level (ttparms, ttargs,
26231 complain|tf_partial);
26232 // Now substitute again with args based on tparms, to reduce
26233 // the level of the ttparms.
26234 ttargs = current_template_args ();
26235 ttparms = tsubst_template_parms_level (ttparms, ttargs,
26236 complain);
26237 // Finally, tack the adjusted parms onto tparms.
26238 ttparms = tree_cons (size_int (depth), ttparms,
26239 current_template_parms);
26240 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
26241 }
26242 }
26243 else
26244 {
26245 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
26246 tree newconst
26247 = build_decl (DECL_SOURCE_LOCATION (oldconst),
26248 TREE_CODE (oldconst),
26249 DECL_NAME (oldconst), newtype);
26250 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
26251 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
26252 SET_DECL_TEMPLATE_PARM_P (newconst);
26253 newidx = build_template_parm_index (index, level, level,
26254 newconst, newtype);
26255 TEMPLATE_PARM_PARAMETER_PACK (newidx)
26256 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
26257 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
26258 }
26259
26260 return newdecl;
26261 }
26262
26263 /* Returns a C++17 class deduction guide template based on the constructor
26264 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
26265 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
26266
26267 static tree
26268 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
26269 {
26270 tree type, tparms, targs, fparms, fargs, ci;
26271 bool memtmpl = false;
26272 bool explicit_p;
26273 location_t loc;
26274 tree fn_tmpl = NULL_TREE;
26275
26276 if (TYPE_P (ctor))
26277 {
26278 type = ctor;
26279 bool copy_p = TYPE_REF_P (type);
26280 if (copy_p)
26281 {
26282 type = TREE_TYPE (type);
26283 fparms = tree_cons (NULL_TREE, type, void_list_node);
26284 }
26285 else
26286 fparms = void_list_node;
26287
26288 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
26289 tparms = DECL_TEMPLATE_PARMS (ctmpl);
26290 targs = CLASSTYPE_TI_ARGS (type);
26291 ci = NULL_TREE;
26292 fargs = NULL_TREE;
26293 loc = DECL_SOURCE_LOCATION (ctmpl);
26294 explicit_p = false;
26295 }
26296 else
26297 {
26298 ++processing_template_decl;
26299 bool ok = true;
26300
26301 fn_tmpl
26302 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
26303 : DECL_TI_TEMPLATE (ctor));
26304 if (outer_args)
26305 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
26306 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
26307
26308 type = DECL_CONTEXT (ctor);
26309
26310 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
26311 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
26312 fully specialized args for the enclosing class. Strip those off, as
26313 the deduction guide won't have those template parameters. */
26314 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
26315 TMPL_PARMS_DEPTH (tparms));
26316 /* Discard the 'this' parameter. */
26317 fparms = FUNCTION_ARG_CHAIN (ctor);
26318 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
26319 ci = get_constraints (ctor);
26320 loc = DECL_SOURCE_LOCATION (ctor);
26321 explicit_p = DECL_NONCONVERTING_P (ctor);
26322
26323 if (PRIMARY_TEMPLATE_P (fn_tmpl))
26324 {
26325 memtmpl = true;
26326
26327 /* For a member template constructor, we need to flatten the two
26328 template parameter lists into one, and then adjust the function
26329 signature accordingly. This gets...complicated. */
26330 tree save_parms = current_template_parms;
26331
26332 /* For a member template we should have two levels of parms/args, one
26333 for the class and one for the constructor. We stripped
26334 specialized args for further enclosing classes above. */
26335 const int depth = 2;
26336 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
26337
26338 /* Template args for translating references to the two-level template
26339 parameters into references to the one-level template parameters we
26340 are creating. */
26341 tree tsubst_args = copy_node (targs);
26342 TMPL_ARGS_LEVEL (tsubst_args, depth)
26343 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
26344
26345 /* Template parms for the constructor template. */
26346 tree ftparms = TREE_VALUE (tparms);
26347 unsigned flen = TREE_VEC_LENGTH (ftparms);
26348 /* Template parms for the class template. */
26349 tparms = TREE_CHAIN (tparms);
26350 tree ctparms = TREE_VALUE (tparms);
26351 unsigned clen = TREE_VEC_LENGTH (ctparms);
26352 /* Template parms for the deduction guide start as a copy of the
26353 template parms for the class. We set current_template_parms for
26354 lookup_template_class_1. */
26355 current_template_parms = tparms = copy_node (tparms);
26356 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
26357 for (unsigned i = 0; i < clen; ++i)
26358 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
26359
26360 /* Now we need to rewrite the constructor parms to append them to the
26361 class parms. */
26362 for (unsigned i = 0; i < flen; ++i)
26363 {
26364 unsigned index = i + clen;
26365 unsigned level = 1;
26366 tree oldelt = TREE_VEC_ELT (ftparms, i);
26367 tree olddecl = TREE_VALUE (oldelt);
26368 tree newdecl = rewrite_template_parm (olddecl, index, level,
26369 tsubst_args, complain);
26370 if (newdecl == error_mark_node)
26371 ok = false;
26372 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
26373 tsubst_args, complain, ctor);
26374 tree list = build_tree_list (newdef, newdecl);
26375 TEMPLATE_PARM_CONSTRAINTS (list)
26376 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
26377 tsubst_args, complain, ctor);
26378 TREE_VEC_ELT (new_vec, index) = list;
26379 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
26380 }
26381
26382 /* Now we have a final set of template parms to substitute into the
26383 function signature. */
26384 targs = template_parms_to_args (tparms);
26385 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
26386 complain, ctor);
26387 fargs = tsubst (fargs, tsubst_args, complain, ctor);
26388 if (ci)
26389 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
26390
26391 current_template_parms = save_parms;
26392 }
26393
26394 --processing_template_decl;
26395 if (!ok)
26396 return error_mark_node;
26397 }
26398
26399 if (!memtmpl)
26400 {
26401 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
26402 tparms = copy_node (tparms);
26403 INNERMOST_TEMPLATE_PARMS (tparms)
26404 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
26405 }
26406
26407 tree fntype = build_function_type (type, fparms);
26408 tree ded_fn = build_lang_decl_loc (loc,
26409 FUNCTION_DECL,
26410 dguide_name (type), fntype);
26411 DECL_ARGUMENTS (ded_fn) = fargs;
26412 DECL_ARTIFICIAL (ded_fn) = true;
26413 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
26414 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
26415 DECL_ARTIFICIAL (ded_tmpl) = true;
26416 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
26417 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
26418 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
26419 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
26420 if (DECL_P (ctor))
26421 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
26422 if (ci)
26423 set_constraints (ded_tmpl, ci);
26424
26425 return ded_tmpl;
26426 }
26427
26428 /* Deduce template arguments for the class template placeholder PTYPE for
26429 template TMPL based on the initializer INIT, and return the resulting
26430 type. */
26431
26432 static tree
26433 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
26434 tsubst_flags_t complain)
26435 {
26436 if (!DECL_CLASS_TEMPLATE_P (tmpl))
26437 {
26438 /* We should have handled this in the caller. */
26439 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26440 return ptype;
26441 if (complain & tf_error)
26442 error ("non-class template %qT used without template arguments", tmpl);
26443 return error_mark_node;
26444 }
26445
26446 tree type = TREE_TYPE (tmpl);
26447
26448 bool try_list_ctor = false;
26449
26450 vec<tree,va_gc> *args;
26451 if (init == NULL_TREE
26452 || TREE_CODE (init) == TREE_LIST)
26453 args = make_tree_vector_from_list (init);
26454 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
26455 {
26456 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
26457 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
26458 {
26459 /* As an exception, the first phase in 16.3.1.7 (considering the
26460 initializer list as a single argument) is omitted if the
26461 initializer list consists of a single expression of type cv U,
26462 where U is a specialization of C or a class derived from a
26463 specialization of C. */
26464 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
26465 tree etype = TREE_TYPE (elt);
26466
26467 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
26468 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26469 int err = unify (tparms, targs, type, etype,
26470 UNIFY_ALLOW_DERIVED, /*explain*/false);
26471 if (err == 0)
26472 try_list_ctor = false;
26473 ggc_free (targs);
26474 }
26475 if (try_list_ctor || is_std_init_list (type))
26476 args = make_tree_vector_single (init);
26477 else
26478 args = make_tree_vector_from_ctor (init);
26479 }
26480 else
26481 args = make_tree_vector_single (init);
26482
26483 tree dname = dguide_name (tmpl);
26484 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
26485 /*type*/false, /*complain*/false,
26486 /*hidden*/false);
26487 bool elided = false;
26488 if (cands == error_mark_node)
26489 cands = NULL_TREE;
26490
26491 /* Prune explicit deduction guides in copy-initialization context. */
26492 if (flags & LOOKUP_ONLYCONVERTING)
26493 {
26494 for (lkp_iterator iter (cands); !elided && iter; ++iter)
26495 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26496 elided = true;
26497
26498 if (elided)
26499 {
26500 /* Found a nonconverting guide, prune the candidates. */
26501 tree pruned = NULL_TREE;
26502 for (lkp_iterator iter (cands); iter; ++iter)
26503 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26504 pruned = lookup_add (*iter, pruned);
26505
26506 cands = pruned;
26507 }
26508 }
26509
26510 tree outer_args = NULL_TREE;
26511 if (DECL_CLASS_SCOPE_P (tmpl)
26512 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
26513 {
26514 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
26515 type = TREE_TYPE (most_general_template (tmpl));
26516 }
26517
26518 bool saw_ctor = false;
26519 // FIXME cache artificial deduction guides
26520 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
26521 {
26522 /* Skip inherited constructors. */
26523 if (iter.using_p ())
26524 continue;
26525
26526 tree guide = build_deduction_guide (*iter, outer_args, complain);
26527 if (guide == error_mark_node)
26528 return error_mark_node;
26529 if ((flags & LOOKUP_ONLYCONVERTING)
26530 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
26531 elided = true;
26532 else
26533 cands = lookup_add (guide, cands);
26534
26535 saw_ctor = true;
26536 }
26537
26538 tree call = error_mark_node;
26539
26540 /* If this is list-initialization and the class has a list constructor, first
26541 try deducing from the list as a single argument, as [over.match.list]. */
26542 tree list_cands = NULL_TREE;
26543 if (try_list_ctor && cands)
26544 for (lkp_iterator iter (cands); iter; ++iter)
26545 {
26546 tree dg = *iter;
26547 if (is_list_ctor (dg))
26548 list_cands = lookup_add (dg, list_cands);
26549 }
26550 if (list_cands)
26551 {
26552 ++cp_unevaluated_operand;
26553 call = build_new_function_call (list_cands, &args, tf_decltype);
26554 --cp_unevaluated_operand;
26555
26556 if (call == error_mark_node)
26557 {
26558 /* That didn't work, now try treating the list as a sequence of
26559 arguments. */
26560 release_tree_vector (args);
26561 args = make_tree_vector_from_ctor (init);
26562 }
26563 }
26564
26565 /* Maybe generate an implicit deduction guide. */
26566 if (call == error_mark_node && args->length () < 2)
26567 {
26568 tree gtype = NULL_TREE;
26569
26570 if (args->length () == 1)
26571 /* Generate a copy guide. */
26572 gtype = build_reference_type (type);
26573 else if (!saw_ctor)
26574 /* Generate a default guide. */
26575 gtype = type;
26576
26577 if (gtype)
26578 {
26579 tree guide = build_deduction_guide (gtype, outer_args, complain);
26580 if (guide == error_mark_node)
26581 return error_mark_node;
26582 cands = lookup_add (guide, cands);
26583 }
26584 }
26585
26586 if (elided && !cands)
26587 {
26588 error ("cannot deduce template arguments for copy-initialization"
26589 " of %qT, as it has no non-explicit deduction guides or "
26590 "user-declared constructors", type);
26591 return error_mark_node;
26592 }
26593 else if (!cands && call == error_mark_node)
26594 {
26595 error ("cannot deduce template arguments of %qT, as it has no viable "
26596 "deduction guides", type);
26597 return error_mark_node;
26598 }
26599
26600 if (call == error_mark_node)
26601 {
26602 ++cp_unevaluated_operand;
26603 call = build_new_function_call (cands, &args, tf_decltype);
26604 --cp_unevaluated_operand;
26605 }
26606
26607 if (call == error_mark_node && (complain & tf_warning_or_error))
26608 {
26609 error ("class template argument deduction failed:");
26610
26611 ++cp_unevaluated_operand;
26612 call = build_new_function_call (cands, &args, complain | tf_decltype);
26613 --cp_unevaluated_operand;
26614
26615 if (elided)
26616 inform (input_location, "explicit deduction guides not considered "
26617 "for copy-initialization");
26618 }
26619
26620 release_tree_vector (args);
26621
26622 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
26623 }
26624
26625 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26626 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26627 The CONTEXT determines the context in which auto deduction is performed
26628 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26629 OUTER_TARGS are used during template argument deduction
26630 (context == adc_unify) to properly substitute the result, and is ignored
26631 in other contexts.
26632
26633 For partial-concept-ids, extra args may be appended to the list of deduced
26634 template arguments prior to determining constraint satisfaction. */
26635
26636 tree
26637 do_auto_deduction (tree type, tree init, tree auto_node,
26638 tsubst_flags_t complain, auto_deduction_context context,
26639 tree outer_targs, int flags)
26640 {
26641 tree targs;
26642
26643 if (init == error_mark_node)
26644 return error_mark_node;
26645
26646 if (init && type_dependent_expression_p (init)
26647 && context != adc_unify)
26648 /* Defining a subset of type-dependent expressions that we can deduce
26649 from ahead of time isn't worth the trouble. */
26650 return type;
26651
26652 /* Similarly, we can't deduce from another undeduced decl. */
26653 if (init && undeduced_auto_decl (init))
26654 return type;
26655
26656 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
26657 /* C++17 class template argument deduction. */
26658 return do_class_deduction (type, tmpl, init, flags, complain);
26659
26660 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
26661 /* Nothing we can do with this, even in deduction context. */
26662 return type;
26663
26664 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26665 with either a new invented type template parameter U or, if the
26666 initializer is a braced-init-list (8.5.4), with
26667 std::initializer_list<U>. */
26668 if (BRACE_ENCLOSED_INITIALIZER_P (init))
26669 {
26670 if (!DIRECT_LIST_INIT_P (init))
26671 type = listify_autos (type, auto_node);
26672 else if (CONSTRUCTOR_NELTS (init) == 1)
26673 init = CONSTRUCTOR_ELT (init, 0)->value;
26674 else
26675 {
26676 if (complain & tf_warning_or_error)
26677 {
26678 if (permerror (input_location, "direct-list-initialization of "
26679 "%<auto%> requires exactly one element"))
26680 inform (input_location,
26681 "for deduction to %<std::initializer_list%>, use copy-"
26682 "list-initialization (i.e. add %<=%> before the %<{%>)");
26683 }
26684 type = listify_autos (type, auto_node);
26685 }
26686 }
26687
26688 if (type == error_mark_node)
26689 return error_mark_node;
26690
26691 init = resolve_nondeduced_context (init, complain);
26692
26693 if (context == adc_decomp_type
26694 && auto_node == type
26695 && init != error_mark_node
26696 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
26697 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26698 and initializer has array type, deduce cv-qualified array type. */
26699 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
26700 complain);
26701 else if (AUTO_IS_DECLTYPE (auto_node))
26702 {
26703 bool id = (DECL_P (init)
26704 || ((TREE_CODE (init) == COMPONENT_REF
26705 || TREE_CODE (init) == SCOPE_REF)
26706 && !REF_PARENTHESIZED_P (init)));
26707 targs = make_tree_vec (1);
26708 TREE_VEC_ELT (targs, 0)
26709 = finish_decltype_type (init, id, tf_warning_or_error);
26710 if (type != auto_node)
26711 {
26712 if (complain & tf_error)
26713 error ("%qT as type rather than plain %<decltype(auto)%>", type);
26714 return error_mark_node;
26715 }
26716 }
26717 else
26718 {
26719 tree parms = build_tree_list (NULL_TREE, type);
26720 tree tparms;
26721
26722 if (flag_concepts)
26723 tparms = extract_autos (type);
26724 else
26725 {
26726 tparms = make_tree_vec (1);
26727 TREE_VEC_ELT (tparms, 0)
26728 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
26729 }
26730
26731 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26732 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
26733 DEDUCE_CALL, LOOKUP_NORMAL,
26734 NULL, /*explain_p=*/false);
26735 if (val > 0)
26736 {
26737 if (processing_template_decl)
26738 /* Try again at instantiation time. */
26739 return type;
26740 if (type && type != error_mark_node
26741 && (complain & tf_error))
26742 /* If type is error_mark_node a diagnostic must have been
26743 emitted by now. Also, having a mention to '<type error>'
26744 in the diagnostic is not really useful to the user. */
26745 {
26746 if (cfun && auto_node == current_function_auto_return_pattern
26747 && LAMBDA_FUNCTION_P (current_function_decl))
26748 error ("unable to deduce lambda return type from %qE", init);
26749 else
26750 error ("unable to deduce %qT from %qE", type, init);
26751 type_unification_real (tparms, targs, parms, &init, 1, 0,
26752 DEDUCE_CALL, LOOKUP_NORMAL,
26753 NULL, /*explain_p=*/true);
26754 }
26755 return error_mark_node;
26756 }
26757 }
26758
26759 /* Check any placeholder constraints against the deduced type. */
26760 if (flag_concepts && !processing_template_decl)
26761 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26762 {
26763 /* Use the deduced type to check the associated constraints. If we
26764 have a partial-concept-id, rebuild the argument list so that
26765 we check using the extra arguments. */
26766 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26767 tree cargs = CHECK_CONSTR_ARGS (constr);
26768 if (TREE_VEC_LENGTH (cargs) > 1)
26769 {
26770 cargs = copy_node (cargs);
26771 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26772 }
26773 else
26774 cargs = targs;
26775 if (!constraints_satisfied_p (constr, cargs))
26776 {
26777 if (complain & tf_warning_or_error)
26778 {
26779 switch (context)
26780 {
26781 case adc_unspecified:
26782 case adc_unify:
26783 error("placeholder constraints not satisfied");
26784 break;
26785 case adc_variable_type:
26786 case adc_decomp_type:
26787 error ("deduced initializer does not satisfy "
26788 "placeholder constraints");
26789 break;
26790 case adc_return_type:
26791 error ("deduced return type does not satisfy "
26792 "placeholder constraints");
26793 break;
26794 case adc_requirement:
26795 error ("deduced expression type does not satisfy "
26796 "placeholder constraints");
26797 break;
26798 }
26799 diagnose_constraints (input_location, constr, targs);
26800 }
26801 return error_mark_node;
26802 }
26803 }
26804
26805 if (processing_template_decl && context != adc_unify)
26806 outer_targs = current_template_args ();
26807 targs = add_to_template_args (outer_targs, targs);
26808 return tsubst (type, targs, complain, NULL_TREE);
26809 }
26810
26811 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26812 result. */
26813
26814 tree
26815 splice_late_return_type (tree type, tree late_return_type)
26816 {
26817 if (is_auto (type))
26818 {
26819 if (late_return_type)
26820 return late_return_type;
26821
26822 tree idx = get_template_parm_index (type);
26823 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26824 /* In an abbreviated function template we didn't know we were dealing
26825 with a function template when we saw the auto return type, so update
26826 it to have the correct level. */
26827 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26828 }
26829 return type;
26830 }
26831
26832 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26833 'decltype(auto)' or a deduced class template. */
26834
26835 bool
26836 is_auto (const_tree type)
26837 {
26838 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26839 && (TYPE_IDENTIFIER (type) == auto_identifier
26840 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26841 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26842 return true;
26843 else
26844 return false;
26845 }
26846
26847 /* for_each_template_parm callback for type_uses_auto. */
26848
26849 int
26850 is_auto_r (tree tp, void */*data*/)
26851 {
26852 return is_auto (tp);
26853 }
26854
26855 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26856 a use of `auto'. Returns NULL_TREE otherwise. */
26857
26858 tree
26859 type_uses_auto (tree type)
26860 {
26861 if (type == NULL_TREE)
26862 return NULL_TREE;
26863 else if (flag_concepts)
26864 {
26865 /* The Concepts TS allows multiple autos in one type-specifier; just
26866 return the first one we find, do_auto_deduction will collect all of
26867 them. */
26868 if (uses_template_parms (type))
26869 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26870 /*visited*/NULL, /*nondeduced*/false);
26871 else
26872 return NULL_TREE;
26873 }
26874 else
26875 return find_type_usage (type, is_auto);
26876 }
26877
26878 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
26879 concepts are enabled, auto is acceptable in template arguments, but
26880 only when TEMPL identifies a template class. Return TRUE if any
26881 such errors were reported. */
26882
26883 bool
26884 check_auto_in_tmpl_args (tree tmpl, tree args)
26885 {
26886 /* If there were previous errors, nevermind. */
26887 if (!args || TREE_CODE (args) != TREE_VEC)
26888 return false;
26889
26890 /* If TMPL is an identifier, we're parsing and we can't tell yet
26891 whether TMPL is supposed to be a type, a function or a variable.
26892 We'll only be able to tell during template substitution, so we
26893 expect to be called again then. If concepts are enabled and we
26894 know we have a type, we're ok. */
26895 if (flag_concepts
26896 && (identifier_p (tmpl)
26897 || (DECL_P (tmpl)
26898 && (DECL_TYPE_TEMPLATE_P (tmpl)
26899 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
26900 return false;
26901
26902 /* Quickly search for any occurrences of auto; usually there won't
26903 be any, and then we'll avoid allocating the vector. */
26904 if (!type_uses_auto (args))
26905 return false;
26906
26907 bool errors = false;
26908
26909 tree vec = extract_autos (args);
26910 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
26911 {
26912 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
26913 error_at (DECL_SOURCE_LOCATION (xauto),
26914 "invalid use of %qT in template argument", xauto);
26915 errors = true;
26916 }
26917
26918 return errors;
26919 }
26920
26921 /* For a given template T, return the vector of typedefs referenced
26922 in T for which access check is needed at T instantiation time.
26923 T is either a FUNCTION_DECL or a RECORD_TYPE.
26924 Those typedefs were added to T by the function
26925 append_type_to_template_for_access_check. */
26926
26927 vec<qualified_typedef_usage_t, va_gc> *
26928 get_types_needing_access_check (tree t)
26929 {
26930 tree ti;
26931 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26932
26933 if (!t || t == error_mark_node)
26934 return NULL;
26935
26936 if (!(ti = get_template_info (t)))
26937 return NULL;
26938
26939 if (CLASS_TYPE_P (t)
26940 || TREE_CODE (t) == FUNCTION_DECL)
26941 {
26942 if (!TI_TEMPLATE (ti))
26943 return NULL;
26944
26945 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26946 }
26947
26948 return result;
26949 }
26950
26951 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26952 tied to T. That list of typedefs will be access checked at
26953 T instantiation time.
26954 T is either a FUNCTION_DECL or a RECORD_TYPE.
26955 TYPE_DECL is a TYPE_DECL node representing a typedef.
26956 SCOPE is the scope through which TYPE_DECL is accessed.
26957 LOCATION is the location of the usage point of TYPE_DECL.
26958
26959 This function is a subroutine of
26960 append_type_to_template_for_access_check. */
26961
26962 static void
26963 append_type_to_template_for_access_check_1 (tree t,
26964 tree type_decl,
26965 tree scope,
26966 location_t location)
26967 {
26968 qualified_typedef_usage_t typedef_usage;
26969 tree ti;
26970
26971 if (!t || t == error_mark_node)
26972 return;
26973
26974 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26975 || CLASS_TYPE_P (t))
26976 && type_decl
26977 && TREE_CODE (type_decl) == TYPE_DECL
26978 && scope);
26979
26980 if (!(ti = get_template_info (t)))
26981 return;
26982
26983 gcc_assert (TI_TEMPLATE (ti));
26984
26985 typedef_usage.typedef_decl = type_decl;
26986 typedef_usage.context = scope;
26987 typedef_usage.locus = location;
26988
26989 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26990 }
26991
26992 /* Append TYPE_DECL to the template TEMPL.
26993 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26994 At TEMPL instanciation time, TYPE_DECL will be checked to see
26995 if it can be accessed through SCOPE.
26996 LOCATION is the location of the usage point of TYPE_DECL.
26997
26998 e.g. consider the following code snippet:
26999
27000 class C
27001 {
27002 typedef int myint;
27003 };
27004
27005 template<class U> struct S
27006 {
27007 C::myint mi; // <-- usage point of the typedef C::myint
27008 };
27009
27010 S<char> s;
27011
27012 At S<char> instantiation time, we need to check the access of C::myint
27013 In other words, we need to check the access of the myint typedef through
27014 the C scope. For that purpose, this function will add the myint typedef
27015 and the scope C through which its being accessed to a list of typedefs
27016 tied to the template S. That list will be walked at template instantiation
27017 time and access check performed on each typedefs it contains.
27018 Note that this particular code snippet should yield an error because
27019 myint is private to C. */
27020
27021 void
27022 append_type_to_template_for_access_check (tree templ,
27023 tree type_decl,
27024 tree scope,
27025 location_t location)
27026 {
27027 qualified_typedef_usage_t *iter;
27028 unsigned i;
27029
27030 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
27031
27032 /* Make sure we don't append the type to the template twice. */
27033 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
27034 if (iter->typedef_decl == type_decl && scope == iter->context)
27035 return;
27036
27037 append_type_to_template_for_access_check_1 (templ, type_decl,
27038 scope, location);
27039 }
27040
27041 /* Convert the generic type parameters in PARM that match the types given in the
27042 range [START_IDX, END_IDX) from the current_template_parms into generic type
27043 packs. */
27044
27045 tree
27046 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
27047 {
27048 tree current = current_template_parms;
27049 int depth = TMPL_PARMS_DEPTH (current);
27050 current = INNERMOST_TEMPLATE_PARMS (current);
27051 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
27052
27053 for (int i = 0; i < start_idx; ++i)
27054 TREE_VEC_ELT (replacement, i)
27055 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27056
27057 for (int i = start_idx; i < end_idx; ++i)
27058 {
27059 /* Create a distinct parameter pack type from the current parm and add it
27060 to the replacement args to tsubst below into the generic function
27061 parameter. */
27062
27063 tree o = TREE_TYPE (TREE_VALUE
27064 (TREE_VEC_ELT (current, i)));
27065 tree t = copy_type (o);
27066 TEMPLATE_TYPE_PARM_INDEX (t)
27067 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
27068 o, 0, 0, tf_none);
27069 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
27070 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
27071 TYPE_MAIN_VARIANT (t) = t;
27072 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
27073 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27074 TREE_VEC_ELT (replacement, i) = t;
27075 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
27076 }
27077
27078 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
27079 TREE_VEC_ELT (replacement, i)
27080 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27081
27082 /* If there are more levels then build up the replacement with the outer
27083 template parms. */
27084 if (depth > 1)
27085 replacement = add_to_template_args (template_parms_to_args
27086 (TREE_CHAIN (current_template_parms)),
27087 replacement);
27088
27089 return tsubst (parm, replacement, tf_none, NULL_TREE);
27090 }
27091
27092 /* Entries in the decl_constraint hash table. */
27093 struct GTY((for_user)) constr_entry
27094 {
27095 tree decl;
27096 tree ci;
27097 };
27098
27099 /* Hashing function and equality for constraint entries. */
27100 struct constr_hasher : ggc_ptr_hash<constr_entry>
27101 {
27102 static hashval_t hash (constr_entry *e)
27103 {
27104 return (hashval_t)DECL_UID (e->decl);
27105 }
27106
27107 static bool equal (constr_entry *e1, constr_entry *e2)
27108 {
27109 return e1->decl == e2->decl;
27110 }
27111 };
27112
27113 /* A mapping from declarations to constraint information. Note that
27114 both templates and their underlying declarations are mapped to the
27115 same constraint information.
27116
27117 FIXME: This is defined in pt.c because garbage collection
27118 code is not being generated for constraint.cc. */
27119
27120 static GTY (()) hash_table<constr_hasher> *decl_constraints;
27121
27122 /* Returns the template constraints of declaration T. If T is not
27123 constrained, return NULL_TREE. Note that T must be non-null. */
27124
27125 tree
27126 get_constraints (tree t)
27127 {
27128 if (!flag_concepts)
27129 return NULL_TREE;
27130
27131 gcc_assert (DECL_P (t));
27132 if (TREE_CODE (t) == TEMPLATE_DECL)
27133 t = DECL_TEMPLATE_RESULT (t);
27134 constr_entry elt = { t, NULL_TREE };
27135 constr_entry* found = decl_constraints->find (&elt);
27136 if (found)
27137 return found->ci;
27138 else
27139 return NULL_TREE;
27140 }
27141
27142 /* Associate the given constraint information CI with the declaration
27143 T. If T is a template, then the constraints are associated with
27144 its underlying declaration. Don't build associations if CI is
27145 NULL_TREE. */
27146
27147 void
27148 set_constraints (tree t, tree ci)
27149 {
27150 if (!ci)
27151 return;
27152 gcc_assert (t && flag_concepts);
27153 if (TREE_CODE (t) == TEMPLATE_DECL)
27154 t = DECL_TEMPLATE_RESULT (t);
27155 gcc_assert (!get_constraints (t));
27156 constr_entry elt = {t, ci};
27157 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
27158 constr_entry* entry = ggc_alloc<constr_entry> ();
27159 *entry = elt;
27160 *slot = entry;
27161 }
27162
27163 /* Remove the associated constraints of the declaration T. */
27164
27165 void
27166 remove_constraints (tree t)
27167 {
27168 gcc_assert (DECL_P (t));
27169 if (TREE_CODE (t) == TEMPLATE_DECL)
27170 t = DECL_TEMPLATE_RESULT (t);
27171
27172 constr_entry elt = {t, NULL_TREE};
27173 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
27174 if (slot)
27175 decl_constraints->clear_slot (slot);
27176 }
27177
27178 /* Memoized satisfaction results for declarations. This
27179 maps the pair (constraint_info, arguments) to the result computed
27180 by constraints_satisfied_p. */
27181
27182 struct GTY((for_user)) constraint_sat_entry
27183 {
27184 tree ci;
27185 tree args;
27186 tree result;
27187 };
27188
27189 /* Hashing function and equality for constraint entries. */
27190
27191 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
27192 {
27193 static hashval_t hash (constraint_sat_entry *e)
27194 {
27195 hashval_t val = iterative_hash_object(e->ci, 0);
27196 return iterative_hash_template_arg (e->args, val);
27197 }
27198
27199 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
27200 {
27201 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
27202 }
27203 };
27204
27205 /* Memoized satisfaction results for concept checks. */
27206
27207 struct GTY((for_user)) concept_spec_entry
27208 {
27209 tree tmpl;
27210 tree args;
27211 tree result;
27212 };
27213
27214 /* Hashing function and equality for constraint entries. */
27215
27216 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
27217 {
27218 static hashval_t hash (concept_spec_entry *e)
27219 {
27220 return hash_tmpl_and_args (e->tmpl, e->args);
27221 }
27222
27223 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
27224 {
27225 ++comparing_specializations;
27226 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
27227 --comparing_specializations;
27228 return eq;
27229 }
27230 };
27231
27232 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
27233 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
27234
27235 /* Search for a memoized satisfaction result. Returns one of the
27236 truth value nodes if previously memoized, or NULL_TREE otherwise. */
27237
27238 tree
27239 lookup_constraint_satisfaction (tree ci, tree args)
27240 {
27241 constraint_sat_entry elt = { ci, args, NULL_TREE };
27242 constraint_sat_entry* found = constraint_memos->find (&elt);
27243 if (found)
27244 return found->result;
27245 else
27246 return NULL_TREE;
27247 }
27248
27249 /* Memoize the result of a satisfication test. Returns the saved result. */
27250
27251 tree
27252 memoize_constraint_satisfaction (tree ci, tree args, tree result)
27253 {
27254 constraint_sat_entry elt = {ci, args, result};
27255 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
27256 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
27257 *entry = elt;
27258 *slot = entry;
27259 return result;
27260 }
27261
27262 /* Search for a memoized satisfaction result for a concept. */
27263
27264 tree
27265 lookup_concept_satisfaction (tree tmpl, tree args)
27266 {
27267 concept_spec_entry elt = { tmpl, args, NULL_TREE };
27268 concept_spec_entry* found = concept_memos->find (&elt);
27269 if (found)
27270 return found->result;
27271 else
27272 return NULL_TREE;
27273 }
27274
27275 /* Memoize the result of a concept check. Returns the saved result. */
27276
27277 tree
27278 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
27279 {
27280 concept_spec_entry elt = {tmpl, args, result};
27281 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
27282 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
27283 *entry = elt;
27284 *slot = entry;
27285 return result;
27286 }
27287
27288 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
27289
27290 /* Returns a prior concept specialization. This returns the substituted
27291 and normalized constraints defined by the concept. */
27292
27293 tree
27294 get_concept_expansion (tree tmpl, tree args)
27295 {
27296 concept_spec_entry elt = { tmpl, args, NULL_TREE };
27297 concept_spec_entry* found = concept_expansions->find (&elt);
27298 if (found)
27299 return found->result;
27300 else
27301 return NULL_TREE;
27302 }
27303
27304 /* Save a concept expansion for later. */
27305
27306 tree
27307 save_concept_expansion (tree tmpl, tree args, tree def)
27308 {
27309 concept_spec_entry elt = {tmpl, args, def};
27310 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
27311 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
27312 *entry = elt;
27313 *slot = entry;
27314 return def;
27315 }
27316
27317 static hashval_t
27318 hash_subsumption_args (tree t1, tree t2)
27319 {
27320 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
27321 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
27322 int val = 0;
27323 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
27324 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
27325 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
27326 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
27327 return val;
27328 }
27329
27330 /* Compare the constraints of two subsumption entries. The LEFT1 and
27331 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
27332 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
27333
27334 static bool
27335 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
27336 {
27337 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
27338 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
27339 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
27340 CHECK_CONSTR_ARGS (right1)))
27341 return comp_template_args (CHECK_CONSTR_ARGS (left2),
27342 CHECK_CONSTR_ARGS (right2));
27343 return false;
27344 }
27345
27346 /* Key/value pair for learning and memoizing subsumption results. This
27347 associates a pair of check constraints (including arguments) with
27348 a boolean value indicating the result. */
27349
27350 struct GTY((for_user)) subsumption_entry
27351 {
27352 tree t1;
27353 tree t2;
27354 bool result;
27355 };
27356
27357 /* Hashing function and equality for constraint entries. */
27358
27359 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
27360 {
27361 static hashval_t hash (subsumption_entry *e)
27362 {
27363 return hash_subsumption_args (e->t1, e->t2);
27364 }
27365
27366 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
27367 {
27368 ++comparing_specializations;
27369 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
27370 --comparing_specializations;
27371 return eq;
27372 }
27373 };
27374
27375 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
27376
27377 /* Search for a previously cached subsumption result. */
27378
27379 bool*
27380 lookup_subsumption_result (tree t1, tree t2)
27381 {
27382 subsumption_entry elt = { t1, t2, false };
27383 subsumption_entry* found = subsumption_table->find (&elt);
27384 if (found)
27385 return &found->result;
27386 else
27387 return 0;
27388 }
27389
27390 /* Save a subsumption result. */
27391
27392 bool
27393 save_subsumption_result (tree t1, tree t2, bool result)
27394 {
27395 subsumption_entry elt = {t1, t2, result};
27396 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
27397 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
27398 *entry = elt;
27399 *slot = entry;
27400 return result;
27401 }
27402
27403 /* Set up the hash table for constraint association. */
27404
27405 void
27406 init_constraint_processing (void)
27407 {
27408 if (!flag_concepts)
27409 return;
27410
27411 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
27412 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
27413 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
27414 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
27415 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
27416 }
27417
27418 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
27419 0..N-1. */
27420
27421 void
27422 declare_integer_pack (void)
27423 {
27424 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
27425 build_function_type_list (integer_type_node,
27426 integer_type_node,
27427 NULL_TREE),
27428 NULL_TREE, ECF_CONST);
27429 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
27430 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
27431 }
27432
27433 /* Set up the hash tables for template instantiations. */
27434
27435 void
27436 init_template_processing (void)
27437 {
27438 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
27439 type_specializations = hash_table<spec_hasher>::create_ggc (37);
27440
27441 if (cxx_dialect >= cxx11)
27442 declare_integer_pack ();
27443 }
27444
27445 /* Print stats about the template hash tables for -fstats. */
27446
27447 void
27448 print_template_statistics (void)
27449 {
27450 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
27451 "%f collisions\n", (long) decl_specializations->size (),
27452 (long) decl_specializations->elements (),
27453 decl_specializations->collisions ());
27454 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
27455 "%f collisions\n", (long) type_specializations->size (),
27456 (long) type_specializations->elements (),
27457 type_specializations->collisions ());
27458 }
27459
27460 #if CHECKING_P
27461
27462 namespace selftest {
27463
27464 /* Verify that build_non_dependent_expr () works, for various expressions,
27465 and that location wrappers don't affect the results. */
27466
27467 static void
27468 test_build_non_dependent_expr ()
27469 {
27470 location_t loc = BUILTINS_LOCATION;
27471
27472 /* Verify constants, without and with location wrappers. */
27473 tree int_cst = build_int_cst (integer_type_node, 42);
27474 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
27475
27476 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
27477 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
27478 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
27479
27480 tree string_lit = build_string (4, "foo");
27481 TREE_TYPE (string_lit) = char_array_type_node;
27482 string_lit = fix_string_type (string_lit);
27483 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
27484
27485 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
27486 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
27487 ASSERT_EQ (wrapped_string_lit,
27488 build_non_dependent_expr (wrapped_string_lit));
27489 }
27490
27491 /* Verify that type_dependent_expression_p () works correctly, even
27492 in the presence of location wrapper nodes. */
27493
27494 static void
27495 test_type_dependent_expression_p ()
27496 {
27497 location_t loc = BUILTINS_LOCATION;
27498
27499 tree name = get_identifier ("foo");
27500
27501 /* If no templates are involved, nothing is type-dependent. */
27502 gcc_assert (!processing_template_decl);
27503 ASSERT_FALSE (type_dependent_expression_p (name));
27504
27505 ++processing_template_decl;
27506
27507 /* Within a template, an unresolved name is always type-dependent. */
27508 ASSERT_TRUE (type_dependent_expression_p (name));
27509
27510 /* Ensure it copes with NULL_TREE and errors. */
27511 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
27512 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
27513
27514 /* A USING_DECL in a template should be type-dependent, even if wrapped
27515 with a location wrapper (PR c++/83799). */
27516 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
27517 TREE_TYPE (using_decl) = integer_type_node;
27518 ASSERT_TRUE (type_dependent_expression_p (using_decl));
27519 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
27520 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
27521 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
27522
27523 --processing_template_decl;
27524 }
27525
27526 /* Run all of the selftests within this file. */
27527
27528 void
27529 cp_pt_c_tests ()
27530 {
27531 test_build_non_dependent_expr ();
27532 test_type_dependent_expression_p ();
27533 }
27534
27535 } // namespace selftest
27536
27537 #endif /* #if CHECKING_P */
27538
27539 #include "gt-cp-pt.h"