Avoid including <string> directly.
[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 #define INCLUDE_STRING
29 #include "system.h"
30 #include "coretypes.h"
31 #include "cp-tree.h"
32 #include "timevar.h"
33 #include "stringpool.h"
34 #include "varasm.h"
35 #include "attribs.h"
36 #include "stor-layout.h"
37 #include "intl.h"
38 #include "c-family/c-objc.h"
39 #include "cp-objcp-common.h"
40 #include "toplev.h"
41 #include "tree-iterator.h"
42 #include "type-utils.h"
43 #include "gimplify.h"
44 #include "gcc-rich-location.h"
45 #include "selftest.h"
46
47 /* The type of functions taking a tree, and some additional data, and
48 returning an int. */
49 typedef int (*tree_fn_t) (tree, void*);
50
51 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
52 instantiations have been deferred, either because their definitions
53 were not yet available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template {
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 (tree args)
1139 {
1140 ++processing_template_decl;
1141 if (!any_dependent_template_arguments_p (args))
1142 {
1143 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1144 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1145 {
1146 tree arg = TREE_VEC_ELT (inner, i);
1147 if (TREE_CODE (arg) == TEMPLATE_DECL)
1148 /* OK */;
1149 else if (TYPE_P (arg))
1150 gcc_assert (strip_typedefs (arg, NULL) == arg);
1151 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1152 /* Allow typedefs on the type of a non-type argument, since a
1153 parameter can have them. */;
1154 else
1155 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1156 }
1157 }
1158 --processing_template_decl;
1159 }
1160
1161 /* Retrieve the specialization (in the sense of [temp.spec] - a
1162 specialization is either an instantiation or an explicit
1163 specialization) of TMPL for the given template ARGS. If there is
1164 no such specialization, return NULL_TREE. The ARGS are a vector of
1165 arguments, or a vector of vectors of arguments, in the case of
1166 templates with more than one level of parameters.
1167
1168 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1169 then we search for a partial specialization matching ARGS. This
1170 parameter is ignored if TMPL is not a class template.
1171
1172 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1173 result is a NONTYPE_ARGUMENT_PACK. */
1174
1175 static tree
1176 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1177 {
1178 if (tmpl == NULL_TREE)
1179 return NULL_TREE;
1180
1181 if (args == error_mark_node)
1182 return NULL_TREE;
1183
1184 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1185 || TREE_CODE (tmpl) == FIELD_DECL);
1186
1187 /* There should be as many levels of arguments as there are
1188 levels of parameters. */
1189 gcc_assert (TMPL_ARGS_DEPTH (args)
1190 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1191 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1192 : template_class_depth (DECL_CONTEXT (tmpl))));
1193
1194 if (flag_checking)
1195 verify_unstripped_args (args);
1196
1197 /* Lambda functions in templates aren't instantiated normally, but through
1198 tsubst_lambda_expr. */
1199 if (lambda_fn_in_template_p (tmpl))
1200 return NULL_TREE;
1201
1202 if (optimize_specialization_lookup_p (tmpl))
1203 {
1204 /* The template arguments actually apply to the containing
1205 class. Find the class specialization with those
1206 arguments. */
1207 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1208 tree class_specialization
1209 = retrieve_specialization (class_template, args, 0);
1210 if (!class_specialization)
1211 return NULL_TREE;
1212
1213 /* Find the instance of TMPL. */
1214 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1215 for (ovl_iterator iter (fns); iter; ++iter)
1216 {
1217 tree fn = *iter;
1218 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1219 /* using-declarations can add base methods to the method vec,
1220 and we don't want those here. */
1221 && DECL_CONTEXT (fn) == class_specialization)
1222 return fn;
1223 }
1224 return NULL_TREE;
1225 }
1226 else
1227 {
1228 spec_entry *found;
1229 spec_entry elt;
1230 hash_table<spec_hasher> *specializations;
1231
1232 elt.tmpl = tmpl;
1233 elt.args = args;
1234 elt.spec = NULL_TREE;
1235
1236 if (DECL_CLASS_TEMPLATE_P (tmpl))
1237 specializations = type_specializations;
1238 else
1239 specializations = decl_specializations;
1240
1241 if (hash == 0)
1242 hash = spec_hasher::hash (&elt);
1243 found = specializations->find_with_hash (&elt, hash);
1244 if (found)
1245 return found->spec;
1246 }
1247
1248 return NULL_TREE;
1249 }
1250
1251 /* Like retrieve_specialization, but for local declarations. */
1252
1253 tree
1254 retrieve_local_specialization (tree tmpl)
1255 {
1256 if (local_specializations == NULL)
1257 return NULL_TREE;
1258
1259 tree *slot = local_specializations->get (tmpl);
1260 return slot ? *slot : NULL_TREE;
1261 }
1262
1263 /* Returns nonzero iff DECL is a specialization of TMPL. */
1264
1265 int
1266 is_specialization_of (tree decl, tree tmpl)
1267 {
1268 tree t;
1269
1270 if (TREE_CODE (decl) == FUNCTION_DECL)
1271 {
1272 for (t = decl;
1273 t != NULL_TREE;
1274 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1275 if (t == tmpl)
1276 return 1;
1277 }
1278 else
1279 {
1280 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1281
1282 for (t = TREE_TYPE (decl);
1283 t != NULL_TREE;
1284 t = CLASSTYPE_USE_TEMPLATE (t)
1285 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1286 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1287 return 1;
1288 }
1289
1290 return 0;
1291 }
1292
1293 /* Returns nonzero iff DECL is a specialization of friend declaration
1294 FRIEND_DECL according to [temp.friend]. */
1295
1296 bool
1297 is_specialization_of_friend (tree decl, tree friend_decl)
1298 {
1299 bool need_template = true;
1300 int template_depth;
1301
1302 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1303 || TREE_CODE (decl) == TYPE_DECL);
1304
1305 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1306 of a template class, we want to check if DECL is a specialization
1307 if this. */
1308 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1309 && DECL_TEMPLATE_INFO (friend_decl)
1310 && !DECL_USE_TEMPLATE (friend_decl))
1311 {
1312 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1313 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1314 need_template = false;
1315 }
1316 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1317 && !PRIMARY_TEMPLATE_P (friend_decl))
1318 need_template = false;
1319
1320 /* There is nothing to do if this is not a template friend. */
1321 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1322 return false;
1323
1324 if (is_specialization_of (decl, friend_decl))
1325 return true;
1326
1327 /* [temp.friend/6]
1328 A member of a class template may be declared to be a friend of a
1329 non-template class. In this case, the corresponding member of
1330 every specialization of the class template is a friend of the
1331 class granting friendship.
1332
1333 For example, given a template friend declaration
1334
1335 template <class T> friend void A<T>::f();
1336
1337 the member function below is considered a friend
1338
1339 template <> struct A<int> {
1340 void f();
1341 };
1342
1343 For this type of template friend, TEMPLATE_DEPTH below will be
1344 nonzero. To determine if DECL is a friend of FRIEND, we first
1345 check if the enclosing class is a specialization of another. */
1346
1347 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1348 if (template_depth
1349 && DECL_CLASS_SCOPE_P (decl)
1350 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1351 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1352 {
1353 /* Next, we check the members themselves. In order to handle
1354 a few tricky cases, such as when FRIEND_DECL's are
1355
1356 template <class T> friend void A<T>::g(T t);
1357 template <class T> template <T t> friend void A<T>::h();
1358
1359 and DECL's are
1360
1361 void A<int>::g(int);
1362 template <int> void A<int>::h();
1363
1364 we need to figure out ARGS, the template arguments from
1365 the context of DECL. This is required for template substitution
1366 of `T' in the function parameter of `g' and template parameter
1367 of `h' in the above examples. Here ARGS corresponds to `int'. */
1368
1369 tree context = DECL_CONTEXT (decl);
1370 tree args = NULL_TREE;
1371 int current_depth = 0;
1372
1373 while (current_depth < template_depth)
1374 {
1375 if (CLASSTYPE_TEMPLATE_INFO (context))
1376 {
1377 if (current_depth == 0)
1378 args = TYPE_TI_ARGS (context);
1379 else
1380 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1381 current_depth++;
1382 }
1383 context = TYPE_CONTEXT (context);
1384 }
1385
1386 if (TREE_CODE (decl) == FUNCTION_DECL)
1387 {
1388 bool is_template;
1389 tree friend_type;
1390 tree decl_type;
1391 tree friend_args_type;
1392 tree decl_args_type;
1393
1394 /* Make sure that both DECL and FRIEND_DECL are templates or
1395 non-templates. */
1396 is_template = DECL_TEMPLATE_INFO (decl)
1397 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1398 if (need_template ^ is_template)
1399 return false;
1400 else if (is_template)
1401 {
1402 /* If both are templates, check template parameter list. */
1403 tree friend_parms
1404 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1405 args, tf_none);
1406 if (!comp_template_parms
1407 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1408 friend_parms))
1409 return false;
1410
1411 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1412 }
1413 else
1414 decl_type = TREE_TYPE (decl);
1415
1416 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1417 tf_none, NULL_TREE);
1418 if (friend_type == error_mark_node)
1419 return false;
1420
1421 /* Check if return types match. */
1422 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1423 return false;
1424
1425 /* Check if function parameter types match, ignoring the
1426 `this' parameter. */
1427 friend_args_type = TYPE_ARG_TYPES (friend_type);
1428 decl_args_type = TYPE_ARG_TYPES (decl_type);
1429 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1430 friend_args_type = TREE_CHAIN (friend_args_type);
1431 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1432 decl_args_type = TREE_CHAIN (decl_args_type);
1433
1434 return compparms (decl_args_type, friend_args_type);
1435 }
1436 else
1437 {
1438 /* DECL is a TYPE_DECL */
1439 bool is_template;
1440 tree decl_type = TREE_TYPE (decl);
1441
1442 /* Make sure that both DECL and FRIEND_DECL are templates or
1443 non-templates. */
1444 is_template
1445 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1446 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1447
1448 if (need_template ^ is_template)
1449 return false;
1450 else if (is_template)
1451 {
1452 tree friend_parms;
1453 /* If both are templates, check the name of the two
1454 TEMPLATE_DECL's first because is_friend didn't. */
1455 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1456 != DECL_NAME (friend_decl))
1457 return false;
1458
1459 /* Now check template parameter list. */
1460 friend_parms
1461 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1462 args, tf_none);
1463 return comp_template_parms
1464 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1465 friend_parms);
1466 }
1467 else
1468 return (DECL_NAME (decl)
1469 == DECL_NAME (friend_decl));
1470 }
1471 }
1472 return false;
1473 }
1474
1475 /* Register the specialization SPEC as a specialization of TMPL with
1476 the indicated ARGS. IS_FRIEND indicates whether the specialization
1477 is actually just a friend declaration. ATTRLIST is the list of
1478 attributes that the specialization is declared with or NULL when
1479 it isn't. Returns SPEC, or an equivalent prior declaration, if
1480 available.
1481
1482 We also store instantiations of field packs in the hash table, even
1483 though they are not themselves templates, to make lookup easier. */
1484
1485 static tree
1486 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1487 hashval_t hash)
1488 {
1489 tree fn;
1490 spec_entry **slot = NULL;
1491 spec_entry elt;
1492
1493 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1494 || (TREE_CODE (tmpl) == FIELD_DECL
1495 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1496
1497 if (TREE_CODE (spec) == FUNCTION_DECL
1498 && uses_template_parms (DECL_TI_ARGS (spec)))
1499 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1500 register it; we want the corresponding TEMPLATE_DECL instead.
1501 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1502 the more obvious `uses_template_parms (spec)' to avoid problems
1503 with default function arguments. In particular, given
1504 something like this:
1505
1506 template <class T> void f(T t1, T t = T())
1507
1508 the default argument expression is not substituted for in an
1509 instantiation unless and until it is actually needed. */
1510 return spec;
1511
1512 if (optimize_specialization_lookup_p (tmpl))
1513 /* We don't put these specializations in the hash table, but we might
1514 want to give an error about a mismatch. */
1515 fn = retrieve_specialization (tmpl, args, 0);
1516 else
1517 {
1518 elt.tmpl = tmpl;
1519 elt.args = args;
1520 elt.spec = spec;
1521
1522 if (hash == 0)
1523 hash = spec_hasher::hash (&elt);
1524
1525 slot =
1526 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1527 if (*slot)
1528 fn = ((spec_entry *) *slot)->spec;
1529 else
1530 fn = NULL_TREE;
1531 }
1532
1533 /* We can sometimes try to re-register a specialization that we've
1534 already got. In particular, regenerate_decl_from_template calls
1535 duplicate_decls which will update the specialization list. But,
1536 we'll still get called again here anyhow. It's more convenient
1537 to simply allow this than to try to prevent it. */
1538 if (fn == spec)
1539 return spec;
1540 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1541 {
1542 if (DECL_TEMPLATE_INSTANTIATION (fn))
1543 {
1544 if (DECL_ODR_USED (fn)
1545 || DECL_EXPLICIT_INSTANTIATION (fn))
1546 {
1547 error ("specialization of %qD after instantiation",
1548 fn);
1549 return error_mark_node;
1550 }
1551 else
1552 {
1553 tree clone;
1554 /* This situation should occur only if the first
1555 specialization is an implicit instantiation, the
1556 second is an explicit specialization, and the
1557 implicit instantiation has not yet been used. That
1558 situation can occur if we have implicitly
1559 instantiated a member function and then specialized
1560 it later.
1561
1562 We can also wind up here if a friend declaration that
1563 looked like an instantiation turns out to be a
1564 specialization:
1565
1566 template <class T> void foo(T);
1567 class S { friend void foo<>(int) };
1568 template <> void foo(int);
1569
1570 We transform the existing DECL in place so that any
1571 pointers to it become pointers to the updated
1572 declaration.
1573
1574 If there was a definition for the template, but not
1575 for the specialization, we want this to look as if
1576 there were no definition, and vice versa. */
1577 DECL_INITIAL (fn) = NULL_TREE;
1578 duplicate_decls (spec, fn, is_friend);
1579 /* The call to duplicate_decls will have applied
1580 [temp.expl.spec]:
1581
1582 An explicit specialization of a function template
1583 is inline only if it is explicitly declared to be,
1584 and independently of whether its function template
1585 is.
1586
1587 to the primary function; now copy the inline bits to
1588 the various clones. */
1589 FOR_EACH_CLONE (clone, fn)
1590 {
1591 DECL_DECLARED_INLINE_P (clone)
1592 = DECL_DECLARED_INLINE_P (fn);
1593 DECL_SOURCE_LOCATION (clone)
1594 = DECL_SOURCE_LOCATION (fn);
1595 DECL_DELETED_FN (clone)
1596 = DECL_DELETED_FN (fn);
1597 }
1598 check_specialization_namespace (tmpl);
1599
1600 return fn;
1601 }
1602 }
1603 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1604 {
1605 tree dd = duplicate_decls (spec, fn, is_friend);
1606 if (dd == error_mark_node)
1607 /* We've already complained in duplicate_decls. */
1608 return error_mark_node;
1609
1610 if (dd == NULL_TREE && DECL_INITIAL (spec))
1611 /* Dup decl failed, but this is a new definition. Set the
1612 line number so any errors match this new
1613 definition. */
1614 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1615
1616 return fn;
1617 }
1618 }
1619 else if (fn)
1620 return duplicate_decls (spec, fn, is_friend);
1621
1622 /* A specialization must be declared in the same namespace as the
1623 template it is specializing. */
1624 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1625 && !check_specialization_namespace (tmpl))
1626 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1627
1628 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1629 {
1630 spec_entry *entry = ggc_alloc<spec_entry> ();
1631 gcc_assert (tmpl && args && spec);
1632 *entry = elt;
1633 *slot = entry;
1634 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1635 && PRIMARY_TEMPLATE_P (tmpl)
1636 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1637 || variable_template_p (tmpl))
1638 /* If TMPL is a forward declaration of a template function, keep a list
1639 of all specializations in case we need to reassign them to a friend
1640 template later in tsubst_friend_function.
1641
1642 Also keep a list of all variable template instantiations so that
1643 process_partial_specialization can check whether a later partial
1644 specialization would have used it. */
1645 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1646 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1647 }
1648
1649 return spec;
1650 }
1651
1652 /* Returns true iff two spec_entry nodes are equivalent. */
1653
1654 int comparing_specializations;
1655
1656 bool
1657 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1658 {
1659 int equal;
1660
1661 ++comparing_specializations;
1662 equal = (e1->tmpl == e2->tmpl
1663 && comp_template_args (e1->args, e2->args));
1664 if (equal && flag_concepts
1665 /* tmpl could be a FIELD_DECL for a capture pack. */
1666 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1667 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1668 && uses_template_parms (e1->args))
1669 {
1670 /* Partial specializations of a variable template can be distinguished by
1671 constraints. */
1672 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1673 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1674 equal = equivalent_constraints (c1, c2);
1675 }
1676 --comparing_specializations;
1677
1678 return equal;
1679 }
1680
1681 /* Returns a hash for a template TMPL and template arguments ARGS. */
1682
1683 static hashval_t
1684 hash_tmpl_and_args (tree tmpl, tree args)
1685 {
1686 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1687 return iterative_hash_template_arg (args, val);
1688 }
1689
1690 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1691 ignoring SPEC. */
1692
1693 hashval_t
1694 spec_hasher::hash (spec_entry *e)
1695 {
1696 return hash_tmpl_and_args (e->tmpl, e->args);
1697 }
1698
1699 /* Recursively calculate a hash value for a template argument ARG, for use
1700 in the hash tables of template specializations. */
1701
1702 hashval_t
1703 iterative_hash_template_arg (tree arg, hashval_t val)
1704 {
1705 unsigned HOST_WIDE_INT i;
1706 enum tree_code code;
1707 char tclass;
1708
1709 if (arg == NULL_TREE)
1710 return iterative_hash_object (arg, val);
1711
1712 if (!TYPE_P (arg))
1713 STRIP_NOPS (arg);
1714
1715 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1716 gcc_unreachable ();
1717
1718 code = TREE_CODE (arg);
1719 tclass = TREE_CODE_CLASS (code);
1720
1721 val = iterative_hash_object (code, val);
1722
1723 switch (code)
1724 {
1725 case ERROR_MARK:
1726 return val;
1727
1728 case IDENTIFIER_NODE:
1729 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1730
1731 case TREE_VEC:
1732 {
1733 int i, len = TREE_VEC_LENGTH (arg);
1734 for (i = 0; i < len; ++i)
1735 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1736 return val;
1737 }
1738
1739 case TYPE_PACK_EXPANSION:
1740 case EXPR_PACK_EXPANSION:
1741 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1742 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1743
1744 case TYPE_ARGUMENT_PACK:
1745 case NONTYPE_ARGUMENT_PACK:
1746 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1747
1748 case TREE_LIST:
1749 for (; arg; arg = TREE_CHAIN (arg))
1750 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1751 return val;
1752
1753 case OVERLOAD:
1754 for (lkp_iterator iter (arg); iter; ++iter)
1755 val = iterative_hash_template_arg (*iter, val);
1756 return val;
1757
1758 case CONSTRUCTOR:
1759 {
1760 tree field, value;
1761 iterative_hash_template_arg (TREE_TYPE (arg), val);
1762 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1763 {
1764 val = iterative_hash_template_arg (field, val);
1765 val = iterative_hash_template_arg (value, val);
1766 }
1767 return val;
1768 }
1769
1770 case PARM_DECL:
1771 if (!DECL_ARTIFICIAL (arg))
1772 {
1773 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1774 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1775 }
1776 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1777
1778 case TARGET_EXPR:
1779 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1780
1781 case PTRMEM_CST:
1782 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1783 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1784
1785 case TEMPLATE_PARM_INDEX:
1786 val = iterative_hash_template_arg
1787 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1788 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1789 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1790
1791 case TRAIT_EXPR:
1792 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1793 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1794 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1795
1796 case BASELINK:
1797 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1798 val);
1799 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1800 val);
1801
1802 case MODOP_EXPR:
1803 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1804 code = TREE_CODE (TREE_OPERAND (arg, 1));
1805 val = iterative_hash_object (code, val);
1806 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1807
1808 case LAMBDA_EXPR:
1809 /* A lambda can't appear in a template arg, but don't crash on
1810 erroneous input. */
1811 gcc_assert (seen_error ());
1812 return val;
1813
1814 case CAST_EXPR:
1815 case IMPLICIT_CONV_EXPR:
1816 case STATIC_CAST_EXPR:
1817 case REINTERPRET_CAST_EXPR:
1818 case CONST_CAST_EXPR:
1819 case DYNAMIC_CAST_EXPR:
1820 case NEW_EXPR:
1821 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1822 /* Now hash operands as usual. */
1823 break;
1824
1825 default:
1826 break;
1827 }
1828
1829 switch (tclass)
1830 {
1831 case tcc_type:
1832 if (alias_template_specialization_p (arg))
1833 {
1834 // We want an alias specialization that survived strip_typedefs
1835 // to hash differently from its TYPE_CANONICAL, to avoid hash
1836 // collisions that compare as different in template_args_equal.
1837 // These could be dependent specializations that strip_typedefs
1838 // left alone, or untouched specializations because
1839 // coerce_template_parms returns the unconverted template
1840 // arguments if it sees incomplete argument packs.
1841 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1842 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1843 }
1844 if (TYPE_CANONICAL (arg))
1845 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1846 val);
1847 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1848 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1849 /* Otherwise just compare the types during lookup. */
1850 return val;
1851
1852 case tcc_declaration:
1853 case tcc_constant:
1854 return iterative_hash_expr (arg, val);
1855
1856 default:
1857 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1858 {
1859 unsigned n = cp_tree_operand_length (arg);
1860 for (i = 0; i < n; ++i)
1861 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1862 return val;
1863 }
1864 }
1865 gcc_unreachable ();
1866 return 0;
1867 }
1868
1869 /* Unregister the specialization SPEC as a specialization of TMPL.
1870 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1871 if the SPEC was listed as a specialization of TMPL.
1872
1873 Note that SPEC has been ggc_freed, so we can't look inside it. */
1874
1875 bool
1876 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1877 {
1878 spec_entry *entry;
1879 spec_entry elt;
1880
1881 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1882 elt.args = TI_ARGS (tinfo);
1883 elt.spec = NULL_TREE;
1884
1885 entry = decl_specializations->find (&elt);
1886 if (entry != NULL)
1887 {
1888 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1889 gcc_assert (new_spec != NULL_TREE);
1890 entry->spec = new_spec;
1891 return 1;
1892 }
1893
1894 return 0;
1895 }
1896
1897 /* Like register_specialization, but for local declarations. We are
1898 registering SPEC, an instantiation of TMPL. */
1899
1900 void
1901 register_local_specialization (tree spec, tree tmpl)
1902 {
1903 gcc_assert (tmpl != spec);
1904 local_specializations->put (tmpl, spec);
1905 }
1906
1907 /* TYPE is a class type. Returns true if TYPE is an explicitly
1908 specialized class. */
1909
1910 bool
1911 explicit_class_specialization_p (tree type)
1912 {
1913 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1914 return false;
1915 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1916 }
1917
1918 /* Print the list of functions at FNS, going through all the overloads
1919 for each element of the list. Alternatively, FNS can not be a
1920 TREE_LIST, in which case it will be printed together with all the
1921 overloads.
1922
1923 MORE and *STR should respectively be FALSE and NULL when the function
1924 is called from the outside. They are used internally on recursive
1925 calls. print_candidates manages the two parameters and leaves NULL
1926 in *STR when it ends. */
1927
1928 static void
1929 print_candidates_1 (tree fns, char **str, bool more = false)
1930 {
1931 if (TREE_CODE (fns) == TREE_LIST)
1932 for (; fns; fns = TREE_CHAIN (fns))
1933 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1934 else
1935 for (lkp_iterator iter (fns); iter;)
1936 {
1937 tree cand = *iter;
1938 ++iter;
1939
1940 const char *pfx = *str;
1941 if (!pfx)
1942 {
1943 if (more || iter)
1944 pfx = _("candidates are:");
1945 else
1946 pfx = _("candidate is:");
1947 *str = get_spaces (pfx);
1948 }
1949 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1950 }
1951 }
1952
1953 /* Print the list of candidate FNS in an error message. FNS can also
1954 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1955
1956 void
1957 print_candidates (tree fns)
1958 {
1959 char *str = NULL;
1960 print_candidates_1 (fns, &str);
1961 free (str);
1962 }
1963
1964 /* Get a (possibly) constrained template declaration for the
1965 purpose of ordering candidates. */
1966 static tree
1967 get_template_for_ordering (tree list)
1968 {
1969 gcc_assert (TREE_CODE (list) == TREE_LIST);
1970 tree f = TREE_VALUE (list);
1971 if (tree ti = DECL_TEMPLATE_INFO (f))
1972 return TI_TEMPLATE (ti);
1973 return f;
1974 }
1975
1976 /* Among candidates having the same signature, return the
1977 most constrained or NULL_TREE if there is no best candidate.
1978 If the signatures of candidates vary (e.g., template
1979 specialization vs. member function), then there can be no
1980 most constrained.
1981
1982 Note that we don't compare constraints on the functions
1983 themselves, but rather those of their templates. */
1984 static tree
1985 most_constrained_function (tree candidates)
1986 {
1987 // Try to find the best candidate in a first pass.
1988 tree champ = candidates;
1989 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1990 {
1991 int winner = more_constrained (get_template_for_ordering (champ),
1992 get_template_for_ordering (c));
1993 if (winner == -1)
1994 champ = c; // The candidate is more constrained
1995 else if (winner == 0)
1996 return NULL_TREE; // Neither is more constrained
1997 }
1998
1999 // Verify that the champ is better than previous candidates.
2000 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2001 if (!more_constrained (get_template_for_ordering (champ),
2002 get_template_for_ordering (c)))
2003 return NULL_TREE;
2004 }
2005
2006 return champ;
2007 }
2008
2009
2010 /* Returns the template (one of the functions given by TEMPLATE_ID)
2011 which can be specialized to match the indicated DECL with the
2012 explicit template args given in TEMPLATE_ID. The DECL may be
2013 NULL_TREE if none is available. In that case, the functions in
2014 TEMPLATE_ID are non-members.
2015
2016 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2017 specialization of a member template.
2018
2019 The TEMPLATE_COUNT is the number of references to qualifying
2020 template classes that appeared in the name of the function. See
2021 check_explicit_specialization for a more accurate description.
2022
2023 TSK indicates what kind of template declaration (if any) is being
2024 declared. TSK_TEMPLATE indicates that the declaration given by
2025 DECL, though a FUNCTION_DECL, has template parameters, and is
2026 therefore a template function.
2027
2028 The template args (those explicitly specified and those deduced)
2029 are output in a newly created vector *TARGS_OUT.
2030
2031 If it is impossible to determine the result, an error message is
2032 issued. The error_mark_node is returned to indicate failure. */
2033
2034 static tree
2035 determine_specialization (tree template_id,
2036 tree decl,
2037 tree* targs_out,
2038 int need_member_template,
2039 int template_count,
2040 tmpl_spec_kind tsk)
2041 {
2042 tree fns;
2043 tree targs;
2044 tree explicit_targs;
2045 tree candidates = NULL_TREE;
2046
2047 /* A TREE_LIST of templates of which DECL may be a specialization.
2048 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2049 corresponding TREE_PURPOSE is the set of template arguments that,
2050 when used to instantiate the template, would produce a function
2051 with the signature of DECL. */
2052 tree templates = NULL_TREE;
2053 int header_count;
2054 cp_binding_level *b;
2055
2056 *targs_out = NULL_TREE;
2057
2058 if (template_id == error_mark_node || decl == error_mark_node)
2059 return error_mark_node;
2060
2061 /* We shouldn't be specializing a member template of an
2062 unspecialized class template; we already gave an error in
2063 check_specialization_scope, now avoid crashing. */
2064 if (template_count && DECL_CLASS_SCOPE_P (decl)
2065 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2066 {
2067 gcc_assert (errorcount);
2068 return error_mark_node;
2069 }
2070
2071 fns = TREE_OPERAND (template_id, 0);
2072 explicit_targs = TREE_OPERAND (template_id, 1);
2073
2074 if (fns == error_mark_node)
2075 return error_mark_node;
2076
2077 /* Check for baselinks. */
2078 if (BASELINK_P (fns))
2079 fns = BASELINK_FUNCTIONS (fns);
2080
2081 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2082 {
2083 error ("%qD is not a function template", fns);
2084 return error_mark_node;
2085 }
2086 else if (VAR_P (decl) && !variable_template_p (fns))
2087 {
2088 error ("%qD is not a variable template", fns);
2089 return error_mark_node;
2090 }
2091
2092 /* Count the number of template headers specified for this
2093 specialization. */
2094 header_count = 0;
2095 for (b = current_binding_level;
2096 b->kind == sk_template_parms;
2097 b = b->level_chain)
2098 ++header_count;
2099
2100 tree orig_fns = fns;
2101
2102 if (variable_template_p (fns))
2103 {
2104 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2105 targs = coerce_template_parms (parms, explicit_targs, fns,
2106 tf_warning_or_error,
2107 /*req_all*/true, /*use_defarg*/true);
2108 if (targs != error_mark_node)
2109 templates = tree_cons (targs, fns, templates);
2110 }
2111 else for (lkp_iterator iter (fns); iter; ++iter)
2112 {
2113 tree fn = *iter;
2114
2115 if (TREE_CODE (fn) == TEMPLATE_DECL)
2116 {
2117 tree decl_arg_types;
2118 tree fn_arg_types;
2119 tree insttype;
2120
2121 /* In case of explicit specialization, we need to check if
2122 the number of template headers appearing in the specialization
2123 is correct. This is usually done in check_explicit_specialization,
2124 but the check done there cannot be exhaustive when specializing
2125 member functions. Consider the following code:
2126
2127 template <> void A<int>::f(int);
2128 template <> template <> void A<int>::f(int);
2129
2130 Assuming that A<int> is not itself an explicit specialization
2131 already, the first line specializes "f" which is a non-template
2132 member function, whilst the second line specializes "f" which
2133 is a template member function. So both lines are syntactically
2134 correct, and check_explicit_specialization does not reject
2135 them.
2136
2137 Here, we can do better, as we are matching the specialization
2138 against the declarations. We count the number of template
2139 headers, and we check if they match TEMPLATE_COUNT + 1
2140 (TEMPLATE_COUNT is the number of qualifying template classes,
2141 plus there must be another header for the member template
2142 itself).
2143
2144 Notice that if header_count is zero, this is not a
2145 specialization but rather a template instantiation, so there
2146 is no check we can perform here. */
2147 if (header_count && header_count != template_count + 1)
2148 continue;
2149
2150 /* Check that the number of template arguments at the
2151 innermost level for DECL is the same as for FN. */
2152 if (current_binding_level->kind == sk_template_parms
2153 && !current_binding_level->explicit_spec_p
2154 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2155 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2156 (current_template_parms))))
2157 continue;
2158
2159 /* DECL might be a specialization of FN. */
2160 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2161 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2162
2163 /* For a non-static member function, we need to make sure
2164 that the const qualification is the same. Since
2165 get_bindings does not try to merge the "this" parameter,
2166 we must do the comparison explicitly. */
2167 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2168 && !same_type_p (TREE_VALUE (fn_arg_types),
2169 TREE_VALUE (decl_arg_types)))
2170 continue;
2171
2172 /* Skip the "this" parameter and, for constructors of
2173 classes with virtual bases, the VTT parameter. A
2174 full specialization of a constructor will have a VTT
2175 parameter, but a template never will. */
2176 decl_arg_types
2177 = skip_artificial_parms_for (decl, decl_arg_types);
2178 fn_arg_types
2179 = skip_artificial_parms_for (fn, fn_arg_types);
2180
2181 /* Function templates cannot be specializations; there are
2182 no partial specializations of functions. Therefore, if
2183 the type of DECL does not match FN, there is no
2184 match.
2185
2186 Note that it should never be the case that we have both
2187 candidates added here, and for regular member functions
2188 below. */
2189 if (tsk == tsk_template)
2190 {
2191 if (compparms (fn_arg_types, decl_arg_types))
2192 candidates = tree_cons (NULL_TREE, fn, candidates);
2193 continue;
2194 }
2195
2196 /* See whether this function might be a specialization of this
2197 template. Suppress access control because we might be trying
2198 to make this specialization a friend, and we have already done
2199 access control for the declaration of the specialization. */
2200 push_deferring_access_checks (dk_no_check);
2201 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2202 pop_deferring_access_checks ();
2203
2204 if (!targs)
2205 /* We cannot deduce template arguments that when used to
2206 specialize TMPL will produce DECL. */
2207 continue;
2208
2209 if (uses_template_parms (targs))
2210 /* We deduced something involving 'auto', which isn't a valid
2211 template argument. */
2212 continue;
2213
2214 /* Remove, from the set of candidates, all those functions
2215 whose constraints are not satisfied. */
2216 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2217 continue;
2218
2219 // Then, try to form the new function type.
2220 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2221 if (insttype == error_mark_node)
2222 continue;
2223 fn_arg_types
2224 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2225 if (!compparms (fn_arg_types, decl_arg_types))
2226 continue;
2227
2228 /* Save this template, and the arguments deduced. */
2229 templates = tree_cons (targs, fn, templates);
2230 }
2231 else if (need_member_template)
2232 /* FN is an ordinary member function, and we need a
2233 specialization of a member template. */
2234 ;
2235 else if (TREE_CODE (fn) != FUNCTION_DECL)
2236 /* We can get IDENTIFIER_NODEs here in certain erroneous
2237 cases. */
2238 ;
2239 else if (!DECL_FUNCTION_MEMBER_P (fn))
2240 /* This is just an ordinary non-member function. Nothing can
2241 be a specialization of that. */
2242 ;
2243 else if (DECL_ARTIFICIAL (fn))
2244 /* Cannot specialize functions that are created implicitly. */
2245 ;
2246 else
2247 {
2248 tree decl_arg_types;
2249
2250 /* This is an ordinary member function. However, since
2251 we're here, we can assume its enclosing class is a
2252 template class. For example,
2253
2254 template <typename T> struct S { void f(); };
2255 template <> void S<int>::f() {}
2256
2257 Here, S<int>::f is a non-template, but S<int> is a
2258 template class. If FN has the same type as DECL, we
2259 might be in business. */
2260
2261 if (!DECL_TEMPLATE_INFO (fn))
2262 /* Its enclosing class is an explicit specialization
2263 of a template class. This is not a candidate. */
2264 continue;
2265
2266 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2267 TREE_TYPE (TREE_TYPE (fn))))
2268 /* The return types differ. */
2269 continue;
2270
2271 /* Adjust the type of DECL in case FN is a static member. */
2272 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2273 if (DECL_STATIC_FUNCTION_P (fn)
2274 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2275 decl_arg_types = TREE_CHAIN (decl_arg_types);
2276
2277 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2278 decl_arg_types))
2279 continue;
2280
2281 // If the deduced arguments do not satisfy the constraints,
2282 // this is not a candidate.
2283 if (flag_concepts && !constraints_satisfied_p (fn))
2284 continue;
2285
2286 // Add the candidate.
2287 candidates = tree_cons (NULL_TREE, fn, candidates);
2288 }
2289 }
2290
2291 if (templates && TREE_CHAIN (templates))
2292 {
2293 /* We have:
2294
2295 [temp.expl.spec]
2296
2297 It is possible for a specialization with a given function
2298 signature to be instantiated from more than one function
2299 template. In such cases, explicit specification of the
2300 template arguments must be used to uniquely identify the
2301 function template specialization being specialized.
2302
2303 Note that here, there's no suggestion that we're supposed to
2304 determine which of the candidate templates is most
2305 specialized. However, we, also have:
2306
2307 [temp.func.order]
2308
2309 Partial ordering of overloaded function template
2310 declarations is used in the following contexts to select
2311 the function template to which a function template
2312 specialization refers:
2313
2314 -- when an explicit specialization refers to a function
2315 template.
2316
2317 So, we do use the partial ordering rules, at least for now.
2318 This extension can only serve to make invalid programs valid,
2319 so it's safe. And, there is strong anecdotal evidence that
2320 the committee intended the partial ordering rules to apply;
2321 the EDG front end has that behavior, and John Spicer claims
2322 that the committee simply forgot to delete the wording in
2323 [temp.expl.spec]. */
2324 tree tmpl = most_specialized_instantiation (templates);
2325 if (tmpl != error_mark_node)
2326 {
2327 templates = tmpl;
2328 TREE_CHAIN (templates) = NULL_TREE;
2329 }
2330 }
2331
2332 // Concepts allows multiple declarations of member functions
2333 // with the same signature. Like above, we need to rely on
2334 // on the partial ordering of those candidates to determine which
2335 // is the best.
2336 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2337 {
2338 if (tree cand = most_constrained_function (candidates))
2339 {
2340 candidates = cand;
2341 TREE_CHAIN (cand) = NULL_TREE;
2342 }
2343 }
2344
2345 if (templates == NULL_TREE && candidates == NULL_TREE)
2346 {
2347 error ("template-id %qD for %q+D does not match any template "
2348 "declaration", template_id, decl);
2349 if (header_count && header_count != template_count + 1)
2350 inform (input_location, "saw %d %<template<>%>, need %d for "
2351 "specializing a member function template",
2352 header_count, template_count + 1);
2353 else
2354 print_candidates (orig_fns);
2355 return error_mark_node;
2356 }
2357 else if ((templates && TREE_CHAIN (templates))
2358 || (candidates && TREE_CHAIN (candidates))
2359 || (templates && candidates))
2360 {
2361 error ("ambiguous template specialization %qD for %q+D",
2362 template_id, decl);
2363 candidates = chainon (candidates, templates);
2364 print_candidates (candidates);
2365 return error_mark_node;
2366 }
2367
2368 /* We have one, and exactly one, match. */
2369 if (candidates)
2370 {
2371 tree fn = TREE_VALUE (candidates);
2372 *targs_out = copy_node (DECL_TI_ARGS (fn));
2373
2374 // Propagate the candidate's constraints to the declaration.
2375 set_constraints (decl, get_constraints (fn));
2376
2377 /* DECL is a re-declaration or partial instantiation of a template
2378 function. */
2379 if (TREE_CODE (fn) == TEMPLATE_DECL)
2380 return fn;
2381 /* It was a specialization of an ordinary member function in a
2382 template class. */
2383 return DECL_TI_TEMPLATE (fn);
2384 }
2385
2386 /* It was a specialization of a template. */
2387 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2388 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2389 {
2390 *targs_out = copy_node (targs);
2391 SET_TMPL_ARGS_LEVEL (*targs_out,
2392 TMPL_ARGS_DEPTH (*targs_out),
2393 TREE_PURPOSE (templates));
2394 }
2395 else
2396 *targs_out = TREE_PURPOSE (templates);
2397 return TREE_VALUE (templates);
2398 }
2399
2400 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2401 but with the default argument values filled in from those in the
2402 TMPL_TYPES. */
2403
2404 static tree
2405 copy_default_args_to_explicit_spec_1 (tree spec_types,
2406 tree tmpl_types)
2407 {
2408 tree new_spec_types;
2409
2410 if (!spec_types)
2411 return NULL_TREE;
2412
2413 if (spec_types == void_list_node)
2414 return void_list_node;
2415
2416 /* Substitute into the rest of the list. */
2417 new_spec_types =
2418 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2419 TREE_CHAIN (tmpl_types));
2420
2421 /* Add the default argument for this parameter. */
2422 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2423 TREE_VALUE (spec_types),
2424 new_spec_types);
2425 }
2426
2427 /* DECL is an explicit specialization. Replicate default arguments
2428 from the template it specializes. (That way, code like:
2429
2430 template <class T> void f(T = 3);
2431 template <> void f(double);
2432 void g () { f (); }
2433
2434 works, as required.) An alternative approach would be to look up
2435 the correct default arguments at the call-site, but this approach
2436 is consistent with how implicit instantiations are handled. */
2437
2438 static void
2439 copy_default_args_to_explicit_spec (tree decl)
2440 {
2441 tree tmpl;
2442 tree spec_types;
2443 tree tmpl_types;
2444 tree new_spec_types;
2445 tree old_type;
2446 tree new_type;
2447 tree t;
2448 tree object_type = NULL_TREE;
2449 tree in_charge = NULL_TREE;
2450 tree vtt = NULL_TREE;
2451
2452 /* See if there's anything we need to do. */
2453 tmpl = DECL_TI_TEMPLATE (decl);
2454 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2455 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2456 if (TREE_PURPOSE (t))
2457 break;
2458 if (!t)
2459 return;
2460
2461 old_type = TREE_TYPE (decl);
2462 spec_types = TYPE_ARG_TYPES (old_type);
2463
2464 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2465 {
2466 /* Remove the this pointer, but remember the object's type for
2467 CV quals. */
2468 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2469 spec_types = TREE_CHAIN (spec_types);
2470 tmpl_types = TREE_CHAIN (tmpl_types);
2471
2472 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2473 {
2474 /* DECL may contain more parameters than TMPL due to the extra
2475 in-charge parameter in constructors and destructors. */
2476 in_charge = spec_types;
2477 spec_types = TREE_CHAIN (spec_types);
2478 }
2479 if (DECL_HAS_VTT_PARM_P (decl))
2480 {
2481 vtt = spec_types;
2482 spec_types = TREE_CHAIN (spec_types);
2483 }
2484 }
2485
2486 /* Compute the merged default arguments. */
2487 new_spec_types =
2488 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2489
2490 /* Compute the new FUNCTION_TYPE. */
2491 if (object_type)
2492 {
2493 if (vtt)
2494 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2495 TREE_VALUE (vtt),
2496 new_spec_types);
2497
2498 if (in_charge)
2499 /* Put the in-charge parameter back. */
2500 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2501 TREE_VALUE (in_charge),
2502 new_spec_types);
2503
2504 new_type = build_method_type_directly (object_type,
2505 TREE_TYPE (old_type),
2506 new_spec_types);
2507 }
2508 else
2509 new_type = build_function_type (TREE_TYPE (old_type),
2510 new_spec_types);
2511 new_type = cp_build_type_attribute_variant (new_type,
2512 TYPE_ATTRIBUTES (old_type));
2513 new_type = build_exception_variant (new_type,
2514 TYPE_RAISES_EXCEPTIONS (old_type));
2515
2516 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2517 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2518
2519 TREE_TYPE (decl) = new_type;
2520 }
2521
2522 /* Return the number of template headers we expect to see for a definition
2523 or specialization of CTYPE or one of its non-template members. */
2524
2525 int
2526 num_template_headers_for_class (tree ctype)
2527 {
2528 int num_templates = 0;
2529
2530 while (ctype && CLASS_TYPE_P (ctype))
2531 {
2532 /* You're supposed to have one `template <...>' for every
2533 template class, but you don't need one for a full
2534 specialization. For example:
2535
2536 template <class T> struct S{};
2537 template <> struct S<int> { void f(); };
2538 void S<int>::f () {}
2539
2540 is correct; there shouldn't be a `template <>' for the
2541 definition of `S<int>::f'. */
2542 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2543 /* If CTYPE does not have template information of any
2544 kind, then it is not a template, nor is it nested
2545 within a template. */
2546 break;
2547 if (explicit_class_specialization_p (ctype))
2548 break;
2549 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2550 ++num_templates;
2551
2552 ctype = TYPE_CONTEXT (ctype);
2553 }
2554
2555 return num_templates;
2556 }
2557
2558 /* Do a simple sanity check on the template headers that precede the
2559 variable declaration DECL. */
2560
2561 void
2562 check_template_variable (tree decl)
2563 {
2564 tree ctx = CP_DECL_CONTEXT (decl);
2565 int wanted = num_template_headers_for_class (ctx);
2566 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2567 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2568 {
2569 if (cxx_dialect < cxx14)
2570 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2571 "variable templates only available with "
2572 "-std=c++14 or -std=gnu++14");
2573
2574 // Namespace-scope variable templates should have a template header.
2575 ++wanted;
2576 }
2577 if (template_header_count > wanted)
2578 {
2579 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2580 "too many template headers for %qD "
2581 "(should be %d)",
2582 decl, wanted);
2583 if (warned && CLASS_TYPE_P (ctx)
2584 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2585 inform (DECL_SOURCE_LOCATION (decl),
2586 "members of an explicitly specialized class are defined "
2587 "without a template header");
2588 }
2589 }
2590
2591 /* An explicit specialization whose declarator-id or class-head-name is not
2592 qualified shall be declared in the nearest enclosing namespace of the
2593 template, or, if the namespace is inline (7.3.1), any namespace from its
2594 enclosing namespace set.
2595
2596 If the name declared in the explicit instantiation is an unqualified name,
2597 the explicit instantiation shall appear in the namespace where its template
2598 is declared or, if that namespace is inline (7.3.1), any namespace from its
2599 enclosing namespace set. */
2600
2601 void
2602 check_unqualified_spec_or_inst (tree t, location_t loc)
2603 {
2604 tree tmpl = most_general_template (t);
2605 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2606 && !is_nested_namespace (current_namespace,
2607 CP_DECL_CONTEXT (tmpl), true))
2608 {
2609 if (processing_specialization)
2610 permerror (loc, "explicit specialization of %qD outside its "
2611 "namespace must use a nested-name-specifier", tmpl);
2612 else if (processing_explicit_instantiation
2613 && cxx_dialect >= cxx11)
2614 /* This was allowed in C++98, so only pedwarn. */
2615 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2616 "outside its namespace must use a nested-name-"
2617 "specifier", tmpl);
2618 }
2619 }
2620
2621 /* Warn for a template specialization SPEC that is missing some of a set
2622 of function or type attributes that the template TEMPL is declared with.
2623 ATTRLIST is a list of additional attributes that SPEC should be taken
2624 to ultimately be declared with. */
2625
2626 static void
2627 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2628 {
2629 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2630 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2631
2632 if (TREE_CODE (tmpl) != FUNCTION_DECL)
2633 return;
2634
2635 /* Avoid warning if either declaration or its type is deprecated. */
2636 if (TREE_DEPRECATED (tmpl)
2637 || TREE_DEPRECATED (spec))
2638 return;
2639
2640 tree tmpl_type = TREE_TYPE (tmpl);
2641 tree spec_type = TREE_TYPE (spec);
2642
2643 if (TREE_DEPRECATED (tmpl_type)
2644 || TREE_DEPRECATED (spec_type)
2645 || TREE_DEPRECATED (TREE_TYPE (tmpl_type))
2646 || TREE_DEPRECATED (TREE_TYPE (spec_type)))
2647 return;
2648
2649 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpl_type) };
2650 tree spec_attrs[] = { DECL_ATTRIBUTES (spec), TYPE_ATTRIBUTES (spec_type) };
2651
2652 if (!spec_attrs[0])
2653 spec_attrs[0] = attrlist;
2654 else if (!spec_attrs[1])
2655 spec_attrs[1] = attrlist;
2656
2657 /* Avoid warning if the primary has no attributes. */
2658 if (!tmpl_attrs[0] && !tmpl_attrs[1])
2659 return;
2660
2661 /* Avoid warning if either declaration contains an attribute on
2662 the white list below. */
2663 const char* const whitelist[] = {
2664 "error", "warning"
2665 };
2666
2667 for (unsigned i = 0; i != 2; ++i)
2668 for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j)
2669 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
2670 || lookup_attribute (whitelist[j], spec_attrs[i]))
2671 return;
2672
2673 /* Avoid warning if the difference between the primary and
2674 the specialization is not in one of the attributes below. */
2675 const char* const blacklist[] = {
2676 "alloc_align", "alloc_size", "assume_aligned", "format",
2677 "format_arg", "malloc", "nonnull"
2678 };
2679
2680 /* Put together a list of the black listed attributes that the primary
2681 template is declared with that the specialization is not, in case
2682 it's not apparent from the most recent declaration of the primary. */
2683 unsigned nattrs = 0;
2684 std::string str;
2685
2686 for (unsigned i = 0; i != sizeof blacklist / sizeof *blacklist; ++i)
2687 {
2688 for (unsigned j = 0; j != 2; ++j)
2689 {
2690 if (!lookup_attribute (blacklist[i], tmpl_attrs[j]))
2691 continue;
2692
2693 for (unsigned k = 0; k != 1 + !!spec_attrs[1]; ++k)
2694 {
2695 if (lookup_attribute (blacklist[i], spec_attrs[k]))
2696 break;
2697
2698 if (str.size ())
2699 str += ", ";
2700 str += "%<";
2701 str += blacklist[i];
2702 str += "%>";
2703 ++nattrs;
2704 }
2705 }
2706 }
2707
2708 if (!nattrs)
2709 return;
2710
2711 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2712 "explicit specialization %q#D may be missing attributes",
2713 spec))
2714 {
2715 if (nattrs > 1)
2716 str = G_("missing primary template attributes ") + str;
2717 else
2718 str = G_("missing primary template attribute ") + str;
2719
2720 inform (DECL_SOURCE_LOCATION (tmpl), str.c_str ());
2721 }
2722
2723 }
2724
2725 /* Check to see if the function just declared, as indicated in
2726 DECLARATOR, and in DECL, is a specialization of a function
2727 template. We may also discover that the declaration is an explicit
2728 instantiation at this point.
2729
2730 Returns DECL, or an equivalent declaration that should be used
2731 instead if all goes well. Issues an error message if something is
2732 amiss. Returns error_mark_node if the error is not easily
2733 recoverable.
2734
2735 FLAGS is a bitmask consisting of the following flags:
2736
2737 2: The function has a definition.
2738 4: The function is a friend.
2739
2740 The TEMPLATE_COUNT is the number of references to qualifying
2741 template classes that appeared in the name of the function. For
2742 example, in
2743
2744 template <class T> struct S { void f(); };
2745 void S<int>::f();
2746
2747 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2748 classes are not counted in the TEMPLATE_COUNT, so that in
2749
2750 template <class T> struct S {};
2751 template <> struct S<int> { void f(); }
2752 template <> void S<int>::f();
2753
2754 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2755 invalid; there should be no template <>.)
2756
2757 If the function is a specialization, it is marked as such via
2758 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2759 is set up correctly, and it is added to the list of specializations
2760 for that template. */
2761
2762 tree
2763 check_explicit_specialization (tree declarator,
2764 tree decl,
2765 int template_count,
2766 int flags,
2767 tree attrlist)
2768 {
2769 int have_def = flags & 2;
2770 int is_friend = flags & 4;
2771 bool is_concept = flags & 8;
2772 int specialization = 0;
2773 int explicit_instantiation = 0;
2774 int member_specialization = 0;
2775 tree ctype = DECL_CLASS_CONTEXT (decl);
2776 tree dname = DECL_NAME (decl);
2777 tmpl_spec_kind tsk;
2778
2779 if (is_friend)
2780 {
2781 if (!processing_specialization)
2782 tsk = tsk_none;
2783 else
2784 tsk = tsk_excessive_parms;
2785 }
2786 else
2787 tsk = current_tmpl_spec_kind (template_count);
2788
2789 switch (tsk)
2790 {
2791 case tsk_none:
2792 if (processing_specialization && !VAR_P (decl))
2793 {
2794 specialization = 1;
2795 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2796 }
2797 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2798 {
2799 if (is_friend)
2800 /* This could be something like:
2801
2802 template <class T> void f(T);
2803 class S { friend void f<>(int); } */
2804 specialization = 1;
2805 else
2806 {
2807 /* This case handles bogus declarations like template <>
2808 template <class T> void f<int>(); */
2809
2810 error ("template-id %qD in declaration of primary template",
2811 declarator);
2812 return decl;
2813 }
2814 }
2815 break;
2816
2817 case tsk_invalid_member_spec:
2818 /* The error has already been reported in
2819 check_specialization_scope. */
2820 return error_mark_node;
2821
2822 case tsk_invalid_expl_inst:
2823 error ("template parameter list used in explicit instantiation");
2824
2825 /* Fall through. */
2826
2827 case tsk_expl_inst:
2828 if (have_def)
2829 error ("definition provided for explicit instantiation");
2830
2831 explicit_instantiation = 1;
2832 break;
2833
2834 case tsk_excessive_parms:
2835 case tsk_insufficient_parms:
2836 if (tsk == tsk_excessive_parms)
2837 error ("too many template parameter lists in declaration of %qD",
2838 decl);
2839 else if (template_header_count)
2840 error("too few template parameter lists in declaration of %qD", decl);
2841 else
2842 error("explicit specialization of %qD must be introduced by "
2843 "%<template <>%>", decl);
2844
2845 /* Fall through. */
2846 case tsk_expl_spec:
2847 if (is_concept)
2848 error ("explicit specialization declared %<concept%>");
2849
2850 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2851 /* In cases like template<> constexpr bool v = true;
2852 We'll give an error in check_template_variable. */
2853 break;
2854
2855 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2856 if (ctype)
2857 member_specialization = 1;
2858 else
2859 specialization = 1;
2860 break;
2861
2862 case tsk_template:
2863 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2864 {
2865 /* This case handles bogus declarations like template <>
2866 template <class T> void f<int>(); */
2867
2868 if (!uses_template_parms (declarator))
2869 error ("template-id %qD in declaration of primary template",
2870 declarator);
2871 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2872 {
2873 /* Partial specialization of variable template. */
2874 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2875 specialization = 1;
2876 goto ok;
2877 }
2878 else if (cxx_dialect < cxx14)
2879 error ("non-type partial specialization %qD "
2880 "is not allowed", declarator);
2881 else
2882 error ("non-class, non-variable partial specialization %qD "
2883 "is not allowed", declarator);
2884 return decl;
2885 ok:;
2886 }
2887
2888 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2889 /* This is a specialization of a member template, without
2890 specialization the containing class. Something like:
2891
2892 template <class T> struct S {
2893 template <class U> void f (U);
2894 };
2895 template <> template <class U> void S<int>::f(U) {}
2896
2897 That's a specialization -- but of the entire template. */
2898 specialization = 1;
2899 break;
2900
2901 default:
2902 gcc_unreachable ();
2903 }
2904
2905 if ((specialization || member_specialization)
2906 /* This doesn't apply to variable templates. */
2907 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2908 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2909 {
2910 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2911 for (; t; t = TREE_CHAIN (t))
2912 if (TREE_PURPOSE (t))
2913 {
2914 permerror (input_location,
2915 "default argument specified in explicit specialization");
2916 break;
2917 }
2918 }
2919
2920 if (specialization || member_specialization || explicit_instantiation)
2921 {
2922 tree tmpl = NULL_TREE;
2923 tree targs = NULL_TREE;
2924 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2925
2926 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2927 if (!was_template_id)
2928 {
2929 tree fns;
2930
2931 gcc_assert (identifier_p (declarator));
2932 if (ctype)
2933 fns = dname;
2934 else
2935 {
2936 /* If there is no class context, the explicit instantiation
2937 must be at namespace scope. */
2938 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2939
2940 /* Find the namespace binding, using the declaration
2941 context. */
2942 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2943 false, true);
2944 if (fns == error_mark_node)
2945 /* If lookup fails, look for a friend declaration so we can
2946 give a better diagnostic. */
2947 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2948 /*type*/false, /*complain*/true,
2949 /*hidden*/true);
2950
2951 if (fns == error_mark_node || !is_overloaded_fn (fns))
2952 {
2953 error ("%qD is not a template function", dname);
2954 fns = error_mark_node;
2955 }
2956 }
2957
2958 declarator = lookup_template_function (fns, NULL_TREE);
2959 }
2960
2961 if (declarator == error_mark_node)
2962 return error_mark_node;
2963
2964 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2965 {
2966 if (!explicit_instantiation)
2967 /* A specialization in class scope. This is invalid,
2968 but the error will already have been flagged by
2969 check_specialization_scope. */
2970 return error_mark_node;
2971 else
2972 {
2973 /* It's not valid to write an explicit instantiation in
2974 class scope, e.g.:
2975
2976 class C { template void f(); }
2977
2978 This case is caught by the parser. However, on
2979 something like:
2980
2981 template class C { void f(); };
2982
2983 (which is invalid) we can get here. The error will be
2984 issued later. */
2985 ;
2986 }
2987
2988 return decl;
2989 }
2990 else if (ctype != NULL_TREE
2991 && (identifier_p (TREE_OPERAND (declarator, 0))))
2992 {
2993 // We'll match variable templates in start_decl.
2994 if (VAR_P (decl))
2995 return decl;
2996
2997 /* Find the list of functions in ctype that have the same
2998 name as the declared function. */
2999 tree name = TREE_OPERAND (declarator, 0);
3000
3001 if (constructor_name_p (name, ctype))
3002 {
3003 if (DECL_CONSTRUCTOR_P (decl)
3004 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3005 : !CLASSTYPE_DESTRUCTOR (ctype))
3006 {
3007 /* From [temp.expl.spec]:
3008
3009 If such an explicit specialization for the member
3010 of a class template names an implicitly-declared
3011 special member function (clause _special_), the
3012 program is ill-formed.
3013
3014 Similar language is found in [temp.explicit]. */
3015 error ("specialization of implicitly-declared special member function");
3016 return error_mark_node;
3017 }
3018
3019 name = DECL_NAME (decl);
3020 }
3021
3022 /* For a type-conversion operator, We might be looking for
3023 `operator int' which will be a specialization of
3024 `operator T'. Grab all the conversion operators, and
3025 then select from them. */
3026 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3027 ? conv_op_identifier : name);
3028
3029 if (fns == NULL_TREE)
3030 {
3031 error ("no member function %qD declared in %qT", name, ctype);
3032 return error_mark_node;
3033 }
3034 else
3035 TREE_OPERAND (declarator, 0) = fns;
3036 }
3037
3038 /* Figure out what exactly is being specialized at this point.
3039 Note that for an explicit instantiation, even one for a
3040 member function, we cannot tell a priori whether the
3041 instantiation is for a member template, or just a member
3042 function of a template class. Even if a member template is
3043 being instantiated, the member template arguments may be
3044 elided if they can be deduced from the rest of the
3045 declaration. */
3046 tmpl = determine_specialization (declarator, decl,
3047 &targs,
3048 member_specialization,
3049 template_count,
3050 tsk);
3051
3052 if (!tmpl || tmpl == error_mark_node)
3053 /* We couldn't figure out what this declaration was
3054 specializing. */
3055 return error_mark_node;
3056 else
3057 {
3058 if (TREE_CODE (decl) == FUNCTION_DECL
3059 && DECL_HIDDEN_FRIEND_P (tmpl))
3060 {
3061 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3062 "friend declaration %qD is not visible to "
3063 "explicit specialization", tmpl))
3064 inform (DECL_SOURCE_LOCATION (tmpl),
3065 "friend declaration here");
3066 }
3067 else if (!ctype && !is_friend
3068 && CP_DECL_CONTEXT (decl) == current_namespace)
3069 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3070
3071 tree gen_tmpl = most_general_template (tmpl);
3072
3073 if (explicit_instantiation)
3074 {
3075 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3076 is done by do_decl_instantiation later. */
3077
3078 int arg_depth = TMPL_ARGS_DEPTH (targs);
3079 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3080
3081 if (arg_depth > parm_depth)
3082 {
3083 /* If TMPL is not the most general template (for
3084 example, if TMPL is a friend template that is
3085 injected into namespace scope), then there will
3086 be too many levels of TARGS. Remove some of them
3087 here. */
3088 int i;
3089 tree new_targs;
3090
3091 new_targs = make_tree_vec (parm_depth);
3092 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3093 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3094 = TREE_VEC_ELT (targs, i);
3095 targs = new_targs;
3096 }
3097
3098 return instantiate_template (tmpl, targs, tf_error);
3099 }
3100
3101 /* If we thought that the DECL was a member function, but it
3102 turns out to be specializing a static member function,
3103 make DECL a static member function as well. */
3104 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3105 && DECL_STATIC_FUNCTION_P (tmpl)
3106 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3107 revert_static_member_fn (decl);
3108
3109 /* If this is a specialization of a member template of a
3110 template class, we want to return the TEMPLATE_DECL, not
3111 the specialization of it. */
3112 if (tsk == tsk_template && !was_template_id)
3113 {
3114 tree result = DECL_TEMPLATE_RESULT (tmpl);
3115 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3116 DECL_INITIAL (result) = NULL_TREE;
3117 if (have_def)
3118 {
3119 tree parm;
3120 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3121 DECL_SOURCE_LOCATION (result)
3122 = DECL_SOURCE_LOCATION (decl);
3123 /* We want to use the argument list specified in the
3124 definition, not in the original declaration. */
3125 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3126 for (parm = DECL_ARGUMENTS (result); parm;
3127 parm = DECL_CHAIN (parm))
3128 DECL_CONTEXT (parm) = result;
3129 }
3130 return register_specialization (tmpl, gen_tmpl, targs,
3131 is_friend, 0);
3132 }
3133
3134 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3135 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3136
3137 if (was_template_id)
3138 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3139
3140 /* Inherit default function arguments from the template
3141 DECL is specializing. */
3142 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3143 copy_default_args_to_explicit_spec (decl);
3144
3145 /* This specialization has the same protection as the
3146 template it specializes. */
3147 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3148 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3149
3150 /* 7.1.1-1 [dcl.stc]
3151
3152 A storage-class-specifier shall not be specified in an
3153 explicit specialization...
3154
3155 The parser rejects these, so unless action is taken here,
3156 explicit function specializations will always appear with
3157 global linkage.
3158
3159 The action recommended by the C++ CWG in response to C++
3160 defect report 605 is to make the storage class and linkage
3161 of the explicit specialization match the templated function:
3162
3163 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3164 */
3165 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3166 {
3167 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3168 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3169
3170 /* A concept cannot be specialized. */
3171 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3172 {
3173 error ("explicit specialization of function concept %qD",
3174 gen_tmpl);
3175 return error_mark_node;
3176 }
3177
3178 /* This specialization has the same linkage and visibility as
3179 the function template it specializes. */
3180 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3181 if (! TREE_PUBLIC (decl))
3182 {
3183 DECL_INTERFACE_KNOWN (decl) = 1;
3184 DECL_NOT_REALLY_EXTERN (decl) = 1;
3185 }
3186 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3187 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3188 {
3189 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3190 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3191 }
3192 }
3193
3194 /* If DECL is a friend declaration, declared using an
3195 unqualified name, the namespace associated with DECL may
3196 have been set incorrectly. For example, in:
3197
3198 template <typename T> void f(T);
3199 namespace N {
3200 struct S { friend void f<int>(int); }
3201 }
3202
3203 we will have set the DECL_CONTEXT for the friend
3204 declaration to N, rather than to the global namespace. */
3205 if (DECL_NAMESPACE_SCOPE_P (decl))
3206 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3207
3208 if (is_friend && !have_def)
3209 /* This is not really a declaration of a specialization.
3210 It's just the name of an instantiation. But, it's not
3211 a request for an instantiation, either. */
3212 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3213 else if (TREE_CODE (decl) == FUNCTION_DECL)
3214 /* A specialization is not necessarily COMDAT. */
3215 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3216 && DECL_DECLARED_INLINE_P (decl));
3217 else if (VAR_P (decl))
3218 DECL_COMDAT (decl) = false;
3219
3220 /* If this is a full specialization, register it so that we can find
3221 it again. Partial specializations will be registered in
3222 process_partial_specialization. */
3223 if (!processing_template_decl)
3224 {
3225 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3226
3227 decl = register_specialization (decl, gen_tmpl, targs,
3228 is_friend, 0);
3229 }
3230
3231
3232 /* A 'structor should already have clones. */
3233 gcc_assert (decl == error_mark_node
3234 || variable_template_p (tmpl)
3235 || !(DECL_CONSTRUCTOR_P (decl)
3236 || DECL_DESTRUCTOR_P (decl))
3237 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3238 }
3239 }
3240
3241 return decl;
3242 }
3243
3244 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3245 parameters. These are represented in the same format used for
3246 DECL_TEMPLATE_PARMS. */
3247
3248 int
3249 comp_template_parms (const_tree parms1, const_tree parms2)
3250 {
3251 const_tree p1;
3252 const_tree p2;
3253
3254 if (parms1 == parms2)
3255 return 1;
3256
3257 for (p1 = parms1, p2 = parms2;
3258 p1 != NULL_TREE && p2 != NULL_TREE;
3259 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3260 {
3261 tree t1 = TREE_VALUE (p1);
3262 tree t2 = TREE_VALUE (p2);
3263 int i;
3264
3265 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3266 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3267
3268 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3269 return 0;
3270
3271 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3272 {
3273 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3274 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3275
3276 /* If either of the template parameters are invalid, assume
3277 they match for the sake of error recovery. */
3278 if (error_operand_p (parm1) || error_operand_p (parm2))
3279 return 1;
3280
3281 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3282 return 0;
3283
3284 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3285 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3286 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3287 continue;
3288 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3289 return 0;
3290 }
3291 }
3292
3293 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3294 /* One set of parameters has more parameters lists than the
3295 other. */
3296 return 0;
3297
3298 return 1;
3299 }
3300
3301 /* Determine whether PARM is a parameter pack. */
3302
3303 bool
3304 template_parameter_pack_p (const_tree parm)
3305 {
3306 /* Determine if we have a non-type template parameter pack. */
3307 if (TREE_CODE (parm) == PARM_DECL)
3308 return (DECL_TEMPLATE_PARM_P (parm)
3309 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3310 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3311 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3312
3313 /* If this is a list of template parameters, we could get a
3314 TYPE_DECL or a TEMPLATE_DECL. */
3315 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3316 parm = TREE_TYPE (parm);
3317
3318 /* Otherwise it must be a type template parameter. */
3319 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3320 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3321 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3322 }
3323
3324 /* Determine if T is a function parameter pack. */
3325
3326 bool
3327 function_parameter_pack_p (const_tree t)
3328 {
3329 if (t && TREE_CODE (t) == PARM_DECL)
3330 return DECL_PACK_P (t);
3331 return false;
3332 }
3333
3334 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3335 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3336
3337 tree
3338 get_function_template_decl (const_tree primary_func_tmpl_inst)
3339 {
3340 if (! primary_func_tmpl_inst
3341 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3342 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3343 return NULL;
3344
3345 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3346 }
3347
3348 /* Return true iff the function parameter PARAM_DECL was expanded
3349 from the function parameter pack PACK. */
3350
3351 bool
3352 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3353 {
3354 if (DECL_ARTIFICIAL (param_decl)
3355 || !function_parameter_pack_p (pack))
3356 return false;
3357
3358 /* The parameter pack and its pack arguments have the same
3359 DECL_PARM_INDEX. */
3360 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3361 }
3362
3363 /* Determine whether ARGS describes a variadic template args list,
3364 i.e., one that is terminated by a template argument pack. */
3365
3366 static bool
3367 template_args_variadic_p (tree args)
3368 {
3369 int nargs;
3370 tree last_parm;
3371
3372 if (args == NULL_TREE)
3373 return false;
3374
3375 args = INNERMOST_TEMPLATE_ARGS (args);
3376 nargs = TREE_VEC_LENGTH (args);
3377
3378 if (nargs == 0)
3379 return false;
3380
3381 last_parm = TREE_VEC_ELT (args, nargs - 1);
3382
3383 return ARGUMENT_PACK_P (last_parm);
3384 }
3385
3386 /* Generate a new name for the parameter pack name NAME (an
3387 IDENTIFIER_NODE) that incorporates its */
3388
3389 static tree
3390 make_ith_pack_parameter_name (tree name, int i)
3391 {
3392 /* Munge the name to include the parameter index. */
3393 #define NUMBUF_LEN 128
3394 char numbuf[NUMBUF_LEN];
3395 char* newname;
3396 int newname_len;
3397
3398 if (name == NULL_TREE)
3399 return name;
3400 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3401 newname_len = IDENTIFIER_LENGTH (name)
3402 + strlen (numbuf) + 2;
3403 newname = (char*)alloca (newname_len);
3404 snprintf (newname, newname_len,
3405 "%s#%i", IDENTIFIER_POINTER (name), i);
3406 return get_identifier (newname);
3407 }
3408
3409 /* Return true if T is a primary function, class or alias template
3410 specialization, not including the template pattern. */
3411
3412 bool
3413 primary_template_specialization_p (const_tree t)
3414 {
3415 if (!t)
3416 return false;
3417
3418 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3419 return (DECL_LANG_SPECIFIC (t)
3420 && DECL_USE_TEMPLATE (t)
3421 && DECL_TEMPLATE_INFO (t)
3422 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3423 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3424 return (CLASSTYPE_TEMPLATE_INFO (t)
3425 && CLASSTYPE_USE_TEMPLATE (t)
3426 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3427 else if (alias_template_specialization_p (t))
3428 return true;
3429 return false;
3430 }
3431
3432 /* Return true if PARM is a template template parameter. */
3433
3434 bool
3435 template_template_parameter_p (const_tree parm)
3436 {
3437 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3438 }
3439
3440 /* Return true iff PARM is a DECL representing a type template
3441 parameter. */
3442
3443 bool
3444 template_type_parameter_p (const_tree parm)
3445 {
3446 return (parm
3447 && (TREE_CODE (parm) == TYPE_DECL
3448 || TREE_CODE (parm) == TEMPLATE_DECL)
3449 && DECL_TEMPLATE_PARM_P (parm));
3450 }
3451
3452 /* Return the template parameters of T if T is a
3453 primary template instantiation, NULL otherwise. */
3454
3455 tree
3456 get_primary_template_innermost_parameters (const_tree t)
3457 {
3458 tree parms = NULL, template_info = NULL;
3459
3460 if ((template_info = get_template_info (t))
3461 && primary_template_specialization_p (t))
3462 parms = INNERMOST_TEMPLATE_PARMS
3463 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3464
3465 return parms;
3466 }
3467
3468 /* Return the template parameters of the LEVELth level from the full list
3469 of template parameters PARMS. */
3470
3471 tree
3472 get_template_parms_at_level (tree parms, int level)
3473 {
3474 tree p;
3475 if (!parms
3476 || TREE_CODE (parms) != TREE_LIST
3477 || level > TMPL_PARMS_DEPTH (parms))
3478 return NULL_TREE;
3479
3480 for (p = parms; p; p = TREE_CHAIN (p))
3481 if (TMPL_PARMS_DEPTH (p) == level)
3482 return p;
3483
3484 return NULL_TREE;
3485 }
3486
3487 /* Returns the template arguments of T if T is a template instantiation,
3488 NULL otherwise. */
3489
3490 tree
3491 get_template_innermost_arguments (const_tree t)
3492 {
3493 tree args = NULL, template_info = NULL;
3494
3495 if ((template_info = get_template_info (t))
3496 && TI_ARGS (template_info))
3497 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3498
3499 return args;
3500 }
3501
3502 /* Return the argument pack elements of T if T is a template argument pack,
3503 NULL otherwise. */
3504
3505 tree
3506 get_template_argument_pack_elems (const_tree t)
3507 {
3508 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3509 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3510 return NULL;
3511
3512 return ARGUMENT_PACK_ARGS (t);
3513 }
3514
3515 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3516 ARGUMENT_PACK_SELECT represents. */
3517
3518 static tree
3519 argument_pack_select_arg (tree t)
3520 {
3521 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3522 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3523
3524 /* If the selected argument is an expansion E, that most likely means we were
3525 called from gen_elem_of_pack_expansion_instantiation during the
3526 substituting of an argument pack (of which the Ith element is a pack
3527 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3528 In this case, the Ith element resulting from this substituting is going to
3529 be a pack expansion, which pattern is the pattern of E. Let's return the
3530 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3531 resulting pack expansion from it. */
3532 if (PACK_EXPANSION_P (arg))
3533 {
3534 /* Make sure we aren't throwing away arg info. */
3535 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3536 arg = PACK_EXPANSION_PATTERN (arg);
3537 }
3538
3539 return arg;
3540 }
3541
3542
3543 /* True iff FN is a function representing a built-in variadic parameter
3544 pack. */
3545
3546 bool
3547 builtin_pack_fn_p (tree fn)
3548 {
3549 if (!fn
3550 || TREE_CODE (fn) != FUNCTION_DECL
3551 || !DECL_IS_BUILTIN (fn))
3552 return false;
3553
3554 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3555 return true;
3556
3557 return false;
3558 }
3559
3560 /* True iff CALL is a call to a function representing a built-in variadic
3561 parameter pack. */
3562
3563 static bool
3564 builtin_pack_call_p (tree call)
3565 {
3566 if (TREE_CODE (call) != CALL_EXPR)
3567 return false;
3568 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3569 }
3570
3571 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3572
3573 static tree
3574 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3575 tree in_decl)
3576 {
3577 tree ohi = CALL_EXPR_ARG (call, 0);
3578 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3579 false/*fn*/, true/*int_cst*/);
3580
3581 if (value_dependent_expression_p (hi))
3582 {
3583 if (hi != ohi)
3584 {
3585 call = copy_node (call);
3586 CALL_EXPR_ARG (call, 0) = hi;
3587 }
3588 tree ex = make_pack_expansion (call, complain);
3589 tree vec = make_tree_vec (1);
3590 TREE_VEC_ELT (vec, 0) = ex;
3591 return vec;
3592 }
3593 else
3594 {
3595 hi = cxx_constant_value (hi);
3596 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3597
3598 /* Calculate the largest value of len that won't make the size of the vec
3599 overflow an int. The compiler will exceed resource limits long before
3600 this, but it seems a decent place to diagnose. */
3601 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3602
3603 if (len < 0 || len > max)
3604 {
3605 if ((complain & tf_error)
3606 && hi != error_mark_node)
3607 error ("argument to __integer_pack must be between 0 and %d", max);
3608 return error_mark_node;
3609 }
3610
3611 tree vec = make_tree_vec (len);
3612
3613 for (int i = 0; i < len; ++i)
3614 TREE_VEC_ELT (vec, i) = size_int (i);
3615
3616 return vec;
3617 }
3618 }
3619
3620 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3621 CALL. */
3622
3623 static tree
3624 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3625 tree in_decl)
3626 {
3627 if (!builtin_pack_call_p (call))
3628 return NULL_TREE;
3629
3630 tree fn = CALL_EXPR_FN (call);
3631
3632 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3633 return expand_integer_pack (call, args, complain, in_decl);
3634
3635 return NULL_TREE;
3636 }
3637
3638 /* Structure used to track the progress of find_parameter_packs_r. */
3639 struct find_parameter_pack_data
3640 {
3641 /* TREE_LIST that will contain all of the parameter packs found by
3642 the traversal. */
3643 tree* parameter_packs;
3644
3645 /* Set of AST nodes that have been visited by the traversal. */
3646 hash_set<tree> *visited;
3647
3648 /* True iff we're making a type pack expansion. */
3649 bool type_pack_expansion_p;
3650 };
3651
3652 /* Identifies all of the argument packs that occur in a template
3653 argument and appends them to the TREE_LIST inside DATA, which is a
3654 find_parameter_pack_data structure. This is a subroutine of
3655 make_pack_expansion and uses_parameter_packs. */
3656 static tree
3657 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3658 {
3659 tree t = *tp;
3660 struct find_parameter_pack_data* ppd =
3661 (struct find_parameter_pack_data*)data;
3662 bool parameter_pack_p = false;
3663
3664 /* Handle type aliases/typedefs. */
3665 if (TYPE_ALIAS_P (t))
3666 {
3667 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3668 cp_walk_tree (&TI_ARGS (tinfo),
3669 &find_parameter_packs_r,
3670 ppd, ppd->visited);
3671 *walk_subtrees = 0;
3672 return NULL_TREE;
3673 }
3674
3675 /* Identify whether this is a parameter pack or not. */
3676 switch (TREE_CODE (t))
3677 {
3678 case TEMPLATE_PARM_INDEX:
3679 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3680 parameter_pack_p = true;
3681 break;
3682
3683 case TEMPLATE_TYPE_PARM:
3684 t = TYPE_MAIN_VARIANT (t);
3685 /* FALLTHRU */
3686 case TEMPLATE_TEMPLATE_PARM:
3687 /* If the placeholder appears in the decl-specifier-seq of a function
3688 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3689 is a pack expansion, the invented template parameter is a template
3690 parameter pack. */
3691 if (ppd->type_pack_expansion_p && is_auto (t))
3692 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3693 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3694 parameter_pack_p = true;
3695 break;
3696
3697 case FIELD_DECL:
3698 case PARM_DECL:
3699 if (DECL_PACK_P (t))
3700 {
3701 /* We don't want to walk into the type of a PARM_DECL,
3702 because we don't want to see the type parameter pack. */
3703 *walk_subtrees = 0;
3704 parameter_pack_p = true;
3705 }
3706 break;
3707
3708 case VAR_DECL:
3709 if (DECL_PACK_P (t))
3710 {
3711 /* We don't want to walk into the type of a variadic capture proxy,
3712 because we don't want to see the type parameter pack. */
3713 *walk_subtrees = 0;
3714 parameter_pack_p = true;
3715 }
3716 else if (variable_template_specialization_p (t))
3717 {
3718 cp_walk_tree (&DECL_TI_ARGS (t),
3719 find_parameter_packs_r,
3720 ppd, ppd->visited);
3721 *walk_subtrees = 0;
3722 }
3723 break;
3724
3725 case CALL_EXPR:
3726 if (builtin_pack_call_p (t))
3727 parameter_pack_p = true;
3728 break;
3729
3730 case BASES:
3731 parameter_pack_p = true;
3732 break;
3733 default:
3734 /* Not a parameter pack. */
3735 break;
3736 }
3737
3738 if (parameter_pack_p)
3739 {
3740 /* Add this parameter pack to the list. */
3741 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3742 }
3743
3744 if (TYPE_P (t))
3745 cp_walk_tree (&TYPE_CONTEXT (t),
3746 &find_parameter_packs_r, ppd, ppd->visited);
3747
3748 /* This switch statement will return immediately if we don't find a
3749 parameter pack. */
3750 switch (TREE_CODE (t))
3751 {
3752 case TEMPLATE_PARM_INDEX:
3753 return NULL_TREE;
3754
3755 case BOUND_TEMPLATE_TEMPLATE_PARM:
3756 /* Check the template itself. */
3757 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3758 &find_parameter_packs_r, ppd, ppd->visited);
3759 /* Check the template arguments. */
3760 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3761 ppd->visited);
3762 *walk_subtrees = 0;
3763 return NULL_TREE;
3764
3765 case TEMPLATE_TYPE_PARM:
3766 case TEMPLATE_TEMPLATE_PARM:
3767 return NULL_TREE;
3768
3769 case PARM_DECL:
3770 return NULL_TREE;
3771
3772 case DECL_EXPR:
3773 /* Ignore the declaration of a capture proxy for a parameter pack. */
3774 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3775 *walk_subtrees = 0;
3776 return NULL_TREE;
3777
3778 case RECORD_TYPE:
3779 if (TYPE_PTRMEMFUNC_P (t))
3780 return NULL_TREE;
3781 /* Fall through. */
3782
3783 case UNION_TYPE:
3784 case ENUMERAL_TYPE:
3785 if (TYPE_TEMPLATE_INFO (t))
3786 cp_walk_tree (&TYPE_TI_ARGS (t),
3787 &find_parameter_packs_r, ppd, ppd->visited);
3788
3789 *walk_subtrees = 0;
3790 return NULL_TREE;
3791
3792 case TEMPLATE_DECL:
3793 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3794 return NULL_TREE;
3795 gcc_fallthrough();
3796
3797 case CONSTRUCTOR:
3798 cp_walk_tree (&TREE_TYPE (t),
3799 &find_parameter_packs_r, ppd, ppd->visited);
3800 return NULL_TREE;
3801
3802 case TYPENAME_TYPE:
3803 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3804 ppd, ppd->visited);
3805 *walk_subtrees = 0;
3806 return NULL_TREE;
3807
3808 case TYPE_PACK_EXPANSION:
3809 case EXPR_PACK_EXPANSION:
3810 *walk_subtrees = 0;
3811 return NULL_TREE;
3812
3813 case INTEGER_TYPE:
3814 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3815 ppd, ppd->visited);
3816 *walk_subtrees = 0;
3817 return NULL_TREE;
3818
3819 case IDENTIFIER_NODE:
3820 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3821 ppd->visited);
3822 *walk_subtrees = 0;
3823 return NULL_TREE;
3824
3825 case LAMBDA_EXPR:
3826 {
3827 /* Look at explicit captures. */
3828 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3829 cap; cap = TREE_CHAIN (cap))
3830 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3831 ppd->visited);
3832 /* Since we defer implicit capture, look in the body as well. */
3833 tree fn = lambda_function (t);
3834 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3835 ppd->visited);
3836 *walk_subtrees = 0;
3837 return NULL_TREE;
3838 }
3839
3840 case DECLTYPE_TYPE:
3841 {
3842 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3843 type_pack_expansion_p to false so that any placeholders
3844 within the expression don't get marked as parameter packs. */
3845 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3846 ppd->type_pack_expansion_p = false;
3847 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3848 ppd, ppd->visited);
3849 ppd->type_pack_expansion_p = type_pack_expansion_p;
3850 *walk_subtrees = 0;
3851 return NULL_TREE;
3852 }
3853
3854 default:
3855 return NULL_TREE;
3856 }
3857
3858 return NULL_TREE;
3859 }
3860
3861 /* Determines if the expression or type T uses any parameter packs. */
3862 bool
3863 uses_parameter_packs (tree t)
3864 {
3865 tree parameter_packs = NULL_TREE;
3866 struct find_parameter_pack_data ppd;
3867 ppd.parameter_packs = &parameter_packs;
3868 ppd.visited = new hash_set<tree>;
3869 ppd.type_pack_expansion_p = false;
3870 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3871 delete ppd.visited;
3872 return parameter_packs != NULL_TREE;
3873 }
3874
3875 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3876 representation a base-class initializer into a parameter pack
3877 expansion. If all goes well, the resulting node will be an
3878 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3879 respectively. */
3880 tree
3881 make_pack_expansion (tree arg, tsubst_flags_t complain)
3882 {
3883 tree result;
3884 tree parameter_packs = NULL_TREE;
3885 bool for_types = false;
3886 struct find_parameter_pack_data ppd;
3887
3888 if (!arg || arg == error_mark_node)
3889 return arg;
3890
3891 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3892 {
3893 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3894 class initializer. In this case, the TREE_PURPOSE will be a
3895 _TYPE node (representing the base class expansion we're
3896 initializing) and the TREE_VALUE will be a TREE_LIST
3897 containing the initialization arguments.
3898
3899 The resulting expansion looks somewhat different from most
3900 expansions. Rather than returning just one _EXPANSION, we
3901 return a TREE_LIST whose TREE_PURPOSE is a
3902 TYPE_PACK_EXPANSION containing the bases that will be
3903 initialized. The TREE_VALUE will be identical to the
3904 original TREE_VALUE, which is a list of arguments that will
3905 be passed to each base. We do not introduce any new pack
3906 expansion nodes into the TREE_VALUE (although it is possible
3907 that some already exist), because the TREE_PURPOSE and
3908 TREE_VALUE all need to be expanded together with the same
3909 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3910 resulting TREE_PURPOSE will mention the parameter packs in
3911 both the bases and the arguments to the bases. */
3912 tree purpose;
3913 tree value;
3914 tree parameter_packs = NULL_TREE;
3915
3916 /* Determine which parameter packs will be used by the base
3917 class expansion. */
3918 ppd.visited = new hash_set<tree>;
3919 ppd.parameter_packs = &parameter_packs;
3920 ppd.type_pack_expansion_p = true;
3921 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3922 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3923 &ppd, ppd.visited);
3924
3925 if (parameter_packs == NULL_TREE)
3926 {
3927 if (complain & tf_error)
3928 error ("base initializer expansion %qT contains no parameter packs",
3929 arg);
3930 delete ppd.visited;
3931 return error_mark_node;
3932 }
3933
3934 if (TREE_VALUE (arg) != void_type_node)
3935 {
3936 /* Collect the sets of parameter packs used in each of the
3937 initialization arguments. */
3938 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3939 {
3940 /* Determine which parameter packs will be expanded in this
3941 argument. */
3942 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3943 &ppd, ppd.visited);
3944 }
3945 }
3946
3947 delete ppd.visited;
3948
3949 /* Create the pack expansion type for the base type. */
3950 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3951 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3952 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3953 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3954
3955 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3956 they will rarely be compared to anything. */
3957 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3958
3959 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3960 }
3961
3962 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3963 for_types = true;
3964
3965 /* Build the PACK_EXPANSION_* node. */
3966 result = for_types
3967 ? cxx_make_type (TYPE_PACK_EXPANSION)
3968 : make_node (EXPR_PACK_EXPANSION);
3969 SET_PACK_EXPANSION_PATTERN (result, arg);
3970 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3971 {
3972 /* Propagate type and const-expression information. */
3973 TREE_TYPE (result) = TREE_TYPE (arg);
3974 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3975 /* Mark this read now, since the expansion might be length 0. */
3976 mark_exp_read (arg);
3977 }
3978 else
3979 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3980 they will rarely be compared to anything. */
3981 SET_TYPE_STRUCTURAL_EQUALITY (result);
3982
3983 /* Determine which parameter packs will be expanded. */
3984 ppd.parameter_packs = &parameter_packs;
3985 ppd.visited = new hash_set<tree>;
3986 ppd.type_pack_expansion_p = TYPE_P (arg);
3987 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3988 delete ppd.visited;
3989
3990 /* Make sure we found some parameter packs. */
3991 if (parameter_packs == NULL_TREE)
3992 {
3993 if (complain & tf_error)
3994 {
3995 if (TYPE_P (arg))
3996 error ("expansion pattern %qT contains no argument packs", arg);
3997 else
3998 error ("expansion pattern %qE contains no argument packs", arg);
3999 }
4000 return error_mark_node;
4001 }
4002 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4003
4004 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4005
4006 return result;
4007 }
4008
4009 /* Checks T for any "bare" parameter packs, which have not yet been
4010 expanded, and issues an error if any are found. This operation can
4011 only be done on full expressions or types (e.g., an expression
4012 statement, "if" condition, etc.), because we could have expressions like:
4013
4014 foo(f(g(h(args)))...)
4015
4016 where "args" is a parameter pack. check_for_bare_parameter_packs
4017 should not be called for the subexpressions args, h(args),
4018 g(h(args)), or f(g(h(args))), because we would produce erroneous
4019 error messages.
4020
4021 Returns TRUE and emits an error if there were bare parameter packs,
4022 returns FALSE otherwise. */
4023 bool
4024 check_for_bare_parameter_packs (tree t)
4025 {
4026 tree parameter_packs = NULL_TREE;
4027 struct find_parameter_pack_data ppd;
4028
4029 if (!processing_template_decl || !t || t == error_mark_node)
4030 return false;
4031
4032 /* A lambda might use a parameter pack from the containing context. */
4033 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
4034 return false;
4035
4036 if (TREE_CODE (t) == TYPE_DECL)
4037 t = TREE_TYPE (t);
4038
4039 ppd.parameter_packs = &parameter_packs;
4040 ppd.visited = new hash_set<tree>;
4041 ppd.type_pack_expansion_p = false;
4042 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4043 delete ppd.visited;
4044
4045 if (parameter_packs)
4046 {
4047 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
4048 error_at (loc, "parameter packs not expanded with %<...%>:");
4049 while (parameter_packs)
4050 {
4051 tree pack = TREE_VALUE (parameter_packs);
4052 tree name = NULL_TREE;
4053
4054 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4055 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4056 name = TYPE_NAME (pack);
4057 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4058 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4059 else if (TREE_CODE (pack) == CALL_EXPR)
4060 name = DECL_NAME (CALL_EXPR_FN (pack));
4061 else
4062 name = DECL_NAME (pack);
4063
4064 if (name)
4065 inform (loc, " %qD", name);
4066 else
4067 inform (loc, " <anonymous>");
4068
4069 parameter_packs = TREE_CHAIN (parameter_packs);
4070 }
4071
4072 return true;
4073 }
4074
4075 return false;
4076 }
4077
4078 /* Expand any parameter packs that occur in the template arguments in
4079 ARGS. */
4080 tree
4081 expand_template_argument_pack (tree args)
4082 {
4083 if (args == error_mark_node)
4084 return error_mark_node;
4085
4086 tree result_args = NULL_TREE;
4087 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4088 int num_result_args = -1;
4089 int non_default_args_count = -1;
4090
4091 /* First, determine if we need to expand anything, and the number of
4092 slots we'll need. */
4093 for (in_arg = 0; in_arg < nargs; ++in_arg)
4094 {
4095 tree arg = TREE_VEC_ELT (args, in_arg);
4096 if (arg == NULL_TREE)
4097 return args;
4098 if (ARGUMENT_PACK_P (arg))
4099 {
4100 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4101 if (num_result_args < 0)
4102 num_result_args = in_arg + num_packed;
4103 else
4104 num_result_args += num_packed;
4105 }
4106 else
4107 {
4108 if (num_result_args >= 0)
4109 num_result_args++;
4110 }
4111 }
4112
4113 /* If no expansion is necessary, we're done. */
4114 if (num_result_args < 0)
4115 return args;
4116
4117 /* Expand arguments. */
4118 result_args = make_tree_vec (num_result_args);
4119 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4120 non_default_args_count =
4121 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4122 for (in_arg = 0; in_arg < nargs; ++in_arg)
4123 {
4124 tree arg = TREE_VEC_ELT (args, in_arg);
4125 if (ARGUMENT_PACK_P (arg))
4126 {
4127 tree packed = ARGUMENT_PACK_ARGS (arg);
4128 int i, num_packed = TREE_VEC_LENGTH (packed);
4129 for (i = 0; i < num_packed; ++i, ++out_arg)
4130 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4131 if (non_default_args_count > 0)
4132 non_default_args_count += num_packed - 1;
4133 }
4134 else
4135 {
4136 TREE_VEC_ELT (result_args, out_arg) = arg;
4137 ++out_arg;
4138 }
4139 }
4140 if (non_default_args_count >= 0)
4141 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4142 return result_args;
4143 }
4144
4145 /* Checks if DECL shadows a template parameter.
4146
4147 [temp.local]: A template-parameter shall not be redeclared within its
4148 scope (including nested scopes).
4149
4150 Emits an error and returns TRUE if the DECL shadows a parameter,
4151 returns FALSE otherwise. */
4152
4153 bool
4154 check_template_shadow (tree decl)
4155 {
4156 tree olddecl;
4157
4158 /* If we're not in a template, we can't possibly shadow a template
4159 parameter. */
4160 if (!current_template_parms)
4161 return true;
4162
4163 /* Figure out what we're shadowing. */
4164 decl = OVL_FIRST (decl);
4165 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4166
4167 /* If there's no previous binding for this name, we're not shadowing
4168 anything, let alone a template parameter. */
4169 if (!olddecl)
4170 return true;
4171
4172 /* If we're not shadowing a template parameter, we're done. Note
4173 that OLDDECL might be an OVERLOAD (or perhaps even an
4174 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4175 node. */
4176 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4177 return true;
4178
4179 /* We check for decl != olddecl to avoid bogus errors for using a
4180 name inside a class. We check TPFI to avoid duplicate errors for
4181 inline member templates. */
4182 if (decl == olddecl
4183 || (DECL_TEMPLATE_PARM_P (decl)
4184 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4185 return true;
4186
4187 /* Don't complain about the injected class name, as we've already
4188 complained about the class itself. */
4189 if (DECL_SELF_REFERENCE_P (decl))
4190 return false;
4191
4192 if (DECL_TEMPLATE_PARM_P (decl))
4193 error ("declaration of template parameter %q+D shadows "
4194 "template parameter", decl);
4195 else
4196 error ("declaration of %q+#D shadows template parameter", decl);
4197 inform (DECL_SOURCE_LOCATION (olddecl),
4198 "template parameter %qD declared here", olddecl);
4199 return false;
4200 }
4201
4202 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4203 ORIG_LEVEL, DECL, and TYPE. */
4204
4205 static tree
4206 build_template_parm_index (int index,
4207 int level,
4208 int orig_level,
4209 tree decl,
4210 tree type)
4211 {
4212 tree t = make_node (TEMPLATE_PARM_INDEX);
4213 TEMPLATE_PARM_IDX (t) = index;
4214 TEMPLATE_PARM_LEVEL (t) = level;
4215 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4216 TEMPLATE_PARM_DECL (t) = decl;
4217 TREE_TYPE (t) = type;
4218 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4219 TREE_READONLY (t) = TREE_READONLY (decl);
4220
4221 return t;
4222 }
4223
4224 /* Find the canonical type parameter for the given template type
4225 parameter. Returns the canonical type parameter, which may be TYPE
4226 if no such parameter existed. */
4227
4228 static tree
4229 canonical_type_parameter (tree type)
4230 {
4231 tree list;
4232 int idx = TEMPLATE_TYPE_IDX (type);
4233 if (!canonical_template_parms)
4234 vec_alloc (canonical_template_parms, idx + 1);
4235
4236 if (canonical_template_parms->length () <= (unsigned) idx)
4237 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4238
4239 list = (*canonical_template_parms)[idx];
4240 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4241 list = TREE_CHAIN (list);
4242
4243 if (list)
4244 return TREE_VALUE (list);
4245 else
4246 {
4247 (*canonical_template_parms)[idx]
4248 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4249 return type;
4250 }
4251 }
4252
4253 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4254 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4255 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4256 new one is created. */
4257
4258 static tree
4259 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4260 tsubst_flags_t complain)
4261 {
4262 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4263 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4264 != TEMPLATE_PARM_LEVEL (index) - levels)
4265 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4266 {
4267 tree orig_decl = TEMPLATE_PARM_DECL (index);
4268 tree decl, t;
4269
4270 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4271 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4272 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4273 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4274 DECL_ARTIFICIAL (decl) = 1;
4275 SET_DECL_TEMPLATE_PARM_P (decl);
4276
4277 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4278 TEMPLATE_PARM_LEVEL (index) - levels,
4279 TEMPLATE_PARM_ORIG_LEVEL (index),
4280 decl, type);
4281 TEMPLATE_PARM_DESCENDANTS (index) = t;
4282 TEMPLATE_PARM_PARAMETER_PACK (t)
4283 = TEMPLATE_PARM_PARAMETER_PACK (index);
4284
4285 /* Template template parameters need this. */
4286 if (TREE_CODE (decl) == TEMPLATE_DECL)
4287 {
4288 DECL_TEMPLATE_RESULT (decl)
4289 = build_decl (DECL_SOURCE_LOCATION (decl),
4290 TYPE_DECL, DECL_NAME (decl), type);
4291 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4292 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4293 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4294 }
4295 }
4296
4297 return TEMPLATE_PARM_DESCENDANTS (index);
4298 }
4299
4300 /* Process information from new template parameter PARM and append it
4301 to the LIST being built. This new parameter is a non-type
4302 parameter iff IS_NON_TYPE is true. This new parameter is a
4303 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4304 is in PARM_LOC. */
4305
4306 tree
4307 process_template_parm (tree list, location_t parm_loc, tree parm,
4308 bool is_non_type, bool is_parameter_pack)
4309 {
4310 tree decl = 0;
4311 int idx = 0;
4312
4313 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4314 tree defval = TREE_PURPOSE (parm);
4315 tree constr = TREE_TYPE (parm);
4316
4317 if (list)
4318 {
4319 tree p = tree_last (list);
4320
4321 if (p && TREE_VALUE (p) != error_mark_node)
4322 {
4323 p = TREE_VALUE (p);
4324 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4325 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4326 else
4327 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4328 }
4329
4330 ++idx;
4331 }
4332
4333 if (is_non_type)
4334 {
4335 parm = TREE_VALUE (parm);
4336
4337 SET_DECL_TEMPLATE_PARM_P (parm);
4338
4339 if (TREE_TYPE (parm) != error_mark_node)
4340 {
4341 /* [temp.param]
4342
4343 The top-level cv-qualifiers on the template-parameter are
4344 ignored when determining its type. */
4345 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4346 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4347 TREE_TYPE (parm) = error_mark_node;
4348 else if (uses_parameter_packs (TREE_TYPE (parm))
4349 && !is_parameter_pack
4350 /* If we're in a nested template parameter list, the template
4351 template parameter could be a parameter pack. */
4352 && processing_template_parmlist == 1)
4353 {
4354 /* This template parameter is not a parameter pack, but it
4355 should be. Complain about "bare" parameter packs. */
4356 check_for_bare_parameter_packs (TREE_TYPE (parm));
4357
4358 /* Recover by calling this a parameter pack. */
4359 is_parameter_pack = true;
4360 }
4361 }
4362
4363 /* A template parameter is not modifiable. */
4364 TREE_CONSTANT (parm) = 1;
4365 TREE_READONLY (parm) = 1;
4366 decl = build_decl (parm_loc,
4367 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4368 TREE_CONSTANT (decl) = 1;
4369 TREE_READONLY (decl) = 1;
4370 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4371 = build_template_parm_index (idx, processing_template_decl,
4372 processing_template_decl,
4373 decl, TREE_TYPE (parm));
4374
4375 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4376 = is_parameter_pack;
4377 }
4378 else
4379 {
4380 tree t;
4381 parm = TREE_VALUE (TREE_VALUE (parm));
4382
4383 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4384 {
4385 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4386 /* This is for distinguishing between real templates and template
4387 template parameters */
4388 TREE_TYPE (parm) = t;
4389 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4390 decl = parm;
4391 }
4392 else
4393 {
4394 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4395 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4396 decl = build_decl (parm_loc,
4397 TYPE_DECL, parm, t);
4398 }
4399
4400 TYPE_NAME (t) = decl;
4401 TYPE_STUB_DECL (t) = decl;
4402 parm = decl;
4403 TEMPLATE_TYPE_PARM_INDEX (t)
4404 = build_template_parm_index (idx, processing_template_decl,
4405 processing_template_decl,
4406 decl, TREE_TYPE (parm));
4407 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4408 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4409 }
4410 DECL_ARTIFICIAL (decl) = 1;
4411 SET_DECL_TEMPLATE_PARM_P (decl);
4412
4413 /* Build requirements for the type/template parameter.
4414 This must be done after SET_DECL_TEMPLATE_PARM_P or
4415 process_template_parm could fail. */
4416 tree reqs = finish_shorthand_constraint (parm, constr);
4417
4418 pushdecl (decl);
4419
4420 /* Build the parameter node linking the parameter declaration,
4421 its default argument (if any), and its constraints (if any). */
4422 parm = build_tree_list (defval, parm);
4423 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4424
4425 return chainon (list, parm);
4426 }
4427
4428 /* The end of a template parameter list has been reached. Process the
4429 tree list into a parameter vector, converting each parameter into a more
4430 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4431 as PARM_DECLs. */
4432
4433 tree
4434 end_template_parm_list (tree parms)
4435 {
4436 int nparms;
4437 tree parm, next;
4438 tree saved_parmlist = make_tree_vec (list_length (parms));
4439
4440 /* Pop the dummy parameter level and add the real one. */
4441 current_template_parms = TREE_CHAIN (current_template_parms);
4442
4443 current_template_parms
4444 = tree_cons (size_int (processing_template_decl),
4445 saved_parmlist, current_template_parms);
4446
4447 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4448 {
4449 next = TREE_CHAIN (parm);
4450 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4451 TREE_CHAIN (parm) = NULL_TREE;
4452 }
4453
4454 --processing_template_parmlist;
4455
4456 return saved_parmlist;
4457 }
4458
4459 // Explicitly indicate the end of the template parameter list. We assume
4460 // that the current template parameters have been constructed and/or
4461 // managed explicitly, as when creating new template template parameters
4462 // from a shorthand constraint.
4463 void
4464 end_template_parm_list ()
4465 {
4466 --processing_template_parmlist;
4467 }
4468
4469 /* end_template_decl is called after a template declaration is seen. */
4470
4471 void
4472 end_template_decl (void)
4473 {
4474 reset_specialization ();
4475
4476 if (! processing_template_decl)
4477 return;
4478
4479 /* This matches the pushlevel in begin_template_parm_list. */
4480 finish_scope ();
4481
4482 --processing_template_decl;
4483 current_template_parms = TREE_CHAIN (current_template_parms);
4484 }
4485
4486 /* Takes a TREE_LIST representing a template parameter and convert it
4487 into an argument suitable to be passed to the type substitution
4488 functions. Note that If the TREE_LIST contains an error_mark
4489 node, the returned argument is error_mark_node. */
4490
4491 tree
4492 template_parm_to_arg (tree t)
4493 {
4494
4495 if (t == NULL_TREE
4496 || TREE_CODE (t) != TREE_LIST)
4497 return t;
4498
4499 if (error_operand_p (TREE_VALUE (t)))
4500 return error_mark_node;
4501
4502 t = TREE_VALUE (t);
4503
4504 if (TREE_CODE (t) == TYPE_DECL
4505 || TREE_CODE (t) == TEMPLATE_DECL)
4506 {
4507 t = TREE_TYPE (t);
4508
4509 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4510 {
4511 /* Turn this argument into a TYPE_ARGUMENT_PACK
4512 with a single element, which expands T. */
4513 tree vec = make_tree_vec (1);
4514 if (CHECKING_P)
4515 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4516
4517 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4518
4519 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4520 SET_ARGUMENT_PACK_ARGS (t, vec);
4521 }
4522 }
4523 else
4524 {
4525 t = DECL_INITIAL (t);
4526
4527 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4528 {
4529 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4530 with a single element, which expands T. */
4531 tree vec = make_tree_vec (1);
4532 if (CHECKING_P)
4533 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4534
4535 t = convert_from_reference (t);
4536 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4537
4538 t = make_node (NONTYPE_ARGUMENT_PACK);
4539 SET_ARGUMENT_PACK_ARGS (t, vec);
4540 }
4541 else
4542 t = convert_from_reference (t);
4543 }
4544 return t;
4545 }
4546
4547 /* Given a single level of template parameters (a TREE_VEC), return it
4548 as a set of template arguments. */
4549
4550 static tree
4551 template_parms_level_to_args (tree parms)
4552 {
4553 tree a = copy_node (parms);
4554 TREE_TYPE (a) = NULL_TREE;
4555 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4556 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4557
4558 if (CHECKING_P)
4559 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4560
4561 return a;
4562 }
4563
4564 /* Given a set of template parameters, return them as a set of template
4565 arguments. The template parameters are represented as a TREE_VEC, in
4566 the form documented in cp-tree.h for template arguments. */
4567
4568 static tree
4569 template_parms_to_args (tree parms)
4570 {
4571 tree header;
4572 tree args = NULL_TREE;
4573 int length = TMPL_PARMS_DEPTH (parms);
4574 int l = length;
4575
4576 /* If there is only one level of template parameters, we do not
4577 create a TREE_VEC of TREE_VECs. Instead, we return a single
4578 TREE_VEC containing the arguments. */
4579 if (length > 1)
4580 args = make_tree_vec (length);
4581
4582 for (header = parms; header; header = TREE_CHAIN (header))
4583 {
4584 tree a = template_parms_level_to_args (TREE_VALUE (header));
4585
4586 if (length > 1)
4587 TREE_VEC_ELT (args, --l) = a;
4588 else
4589 args = a;
4590 }
4591
4592 return args;
4593 }
4594
4595 /* Within the declaration of a template, return the currently active
4596 template parameters as an argument TREE_VEC. */
4597
4598 static tree
4599 current_template_args (void)
4600 {
4601 return template_parms_to_args (current_template_parms);
4602 }
4603
4604 /* Update the declared TYPE by doing any lookups which were thought to be
4605 dependent, but are not now that we know the SCOPE of the declarator. */
4606
4607 tree
4608 maybe_update_decl_type (tree orig_type, tree scope)
4609 {
4610 tree type = orig_type;
4611
4612 if (type == NULL_TREE)
4613 return type;
4614
4615 if (TREE_CODE (orig_type) == TYPE_DECL)
4616 type = TREE_TYPE (type);
4617
4618 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4619 && dependent_type_p (type)
4620 /* Don't bother building up the args in this case. */
4621 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4622 {
4623 /* tsubst in the args corresponding to the template parameters,
4624 including auto if present. Most things will be unchanged, but
4625 make_typename_type and tsubst_qualified_id will resolve
4626 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4627 tree args = current_template_args ();
4628 tree auto_node = type_uses_auto (type);
4629 tree pushed;
4630 if (auto_node)
4631 {
4632 tree auto_vec = make_tree_vec (1);
4633 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4634 args = add_to_template_args (args, auto_vec);
4635 }
4636 pushed = push_scope (scope);
4637 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4638 if (pushed)
4639 pop_scope (scope);
4640 }
4641
4642 if (type == error_mark_node)
4643 return orig_type;
4644
4645 if (TREE_CODE (orig_type) == TYPE_DECL)
4646 {
4647 if (same_type_p (type, TREE_TYPE (orig_type)))
4648 type = orig_type;
4649 else
4650 type = TYPE_NAME (type);
4651 }
4652 return type;
4653 }
4654
4655 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4656 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4657 the new template is a member template. */
4658
4659 tree
4660 build_template_decl (tree decl, tree parms, bool member_template_p)
4661 {
4662 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4663 DECL_TEMPLATE_PARMS (tmpl) = parms;
4664 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4665 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4666 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4667
4668 return tmpl;
4669 }
4670
4671 struct template_parm_data
4672 {
4673 /* The level of the template parameters we are currently
4674 processing. */
4675 int level;
4676
4677 /* The index of the specialization argument we are currently
4678 processing. */
4679 int current_arg;
4680
4681 /* An array whose size is the number of template parameters. The
4682 elements are nonzero if the parameter has been used in any one
4683 of the arguments processed so far. */
4684 int* parms;
4685
4686 /* An array whose size is the number of template arguments. The
4687 elements are nonzero if the argument makes use of template
4688 parameters of this level. */
4689 int* arg_uses_template_parms;
4690 };
4691
4692 /* Subroutine of push_template_decl used to see if each template
4693 parameter in a partial specialization is used in the explicit
4694 argument list. If T is of the LEVEL given in DATA (which is
4695 treated as a template_parm_data*), then DATA->PARMS is marked
4696 appropriately. */
4697
4698 static int
4699 mark_template_parm (tree t, void* data)
4700 {
4701 int level;
4702 int idx;
4703 struct template_parm_data* tpd = (struct template_parm_data*) data;
4704
4705 template_parm_level_and_index (t, &level, &idx);
4706
4707 if (level == tpd->level)
4708 {
4709 tpd->parms[idx] = 1;
4710 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4711 }
4712
4713 /* In C++17 the type of a non-type argument is a deduced context. */
4714 if (cxx_dialect >= cxx17
4715 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4716 for_each_template_parm (TREE_TYPE (t),
4717 &mark_template_parm,
4718 data,
4719 NULL,
4720 /*include_nondeduced_p=*/false);
4721
4722 /* Return zero so that for_each_template_parm will continue the
4723 traversal of the tree; we want to mark *every* template parm. */
4724 return 0;
4725 }
4726
4727 /* Process the partial specialization DECL. */
4728
4729 static tree
4730 process_partial_specialization (tree decl)
4731 {
4732 tree type = TREE_TYPE (decl);
4733 tree tinfo = get_template_info (decl);
4734 tree maintmpl = TI_TEMPLATE (tinfo);
4735 tree specargs = TI_ARGS (tinfo);
4736 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4737 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4738 tree inner_parms;
4739 tree inst;
4740 int nargs = TREE_VEC_LENGTH (inner_args);
4741 int ntparms;
4742 int i;
4743 bool did_error_intro = false;
4744 struct template_parm_data tpd;
4745 struct template_parm_data tpd2;
4746
4747 gcc_assert (current_template_parms);
4748
4749 /* A concept cannot be specialized. */
4750 if (flag_concepts && variable_concept_p (maintmpl))
4751 {
4752 error ("specialization of variable concept %q#D", maintmpl);
4753 return error_mark_node;
4754 }
4755
4756 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4757 ntparms = TREE_VEC_LENGTH (inner_parms);
4758
4759 /* We check that each of the template parameters given in the
4760 partial specialization is used in the argument list to the
4761 specialization. For example:
4762
4763 template <class T> struct S;
4764 template <class T> struct S<T*>;
4765
4766 The second declaration is OK because `T*' uses the template
4767 parameter T, whereas
4768
4769 template <class T> struct S<int>;
4770
4771 is no good. Even trickier is:
4772
4773 template <class T>
4774 struct S1
4775 {
4776 template <class U>
4777 struct S2;
4778 template <class U>
4779 struct S2<T>;
4780 };
4781
4782 The S2<T> declaration is actually invalid; it is a
4783 full-specialization. Of course,
4784
4785 template <class U>
4786 struct S2<T (*)(U)>;
4787
4788 or some such would have been OK. */
4789 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4790 tpd.parms = XALLOCAVEC (int, ntparms);
4791 memset (tpd.parms, 0, sizeof (int) * ntparms);
4792
4793 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4794 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4795 for (i = 0; i < nargs; ++i)
4796 {
4797 tpd.current_arg = i;
4798 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4799 &mark_template_parm,
4800 &tpd,
4801 NULL,
4802 /*include_nondeduced_p=*/false);
4803 }
4804 for (i = 0; i < ntparms; ++i)
4805 if (tpd.parms[i] == 0)
4806 {
4807 /* One of the template parms was not used in a deduced context in the
4808 specialization. */
4809 if (!did_error_intro)
4810 {
4811 error ("template parameters not deducible in "
4812 "partial specialization:");
4813 did_error_intro = true;
4814 }
4815
4816 inform (input_location, " %qD",
4817 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4818 }
4819
4820 if (did_error_intro)
4821 return error_mark_node;
4822
4823 /* [temp.class.spec]
4824
4825 The argument list of the specialization shall not be identical to
4826 the implicit argument list of the primary template. */
4827 tree main_args
4828 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4829 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4830 && (!flag_concepts
4831 || !strictly_subsumes (current_template_constraints (),
4832 get_constraints (maintmpl))))
4833 {
4834 if (!flag_concepts)
4835 error ("partial specialization %q+D does not specialize "
4836 "any template arguments", decl);
4837 else
4838 error ("partial specialization %q+D does not specialize any "
4839 "template arguments and is not more constrained than", decl);
4840 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4841 }
4842
4843 /* A partial specialization that replaces multiple parameters of the
4844 primary template with a pack expansion is less specialized for those
4845 parameters. */
4846 if (nargs < DECL_NTPARMS (maintmpl))
4847 {
4848 error ("partial specialization is not more specialized than the "
4849 "primary template because it replaces multiple parameters "
4850 "with a pack expansion");
4851 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4852 /* Avoid crash in process_partial_specialization. */
4853 return decl;
4854 }
4855
4856 /* If we aren't in a dependent class, we can actually try deduction. */
4857 else if (tpd.level == 1
4858 /* FIXME we should be able to handle a partial specialization of a
4859 partial instantiation, but currently we can't (c++/41727). */
4860 && TMPL_ARGS_DEPTH (specargs) == 1
4861 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4862 {
4863 if (permerror (input_location, "partial specialization %qD is not "
4864 "more specialized than", decl))
4865 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4866 maintmpl);
4867 }
4868
4869 /* [temp.class.spec]
4870
4871 A partially specialized non-type argument expression shall not
4872 involve template parameters of the partial specialization except
4873 when the argument expression is a simple identifier.
4874
4875 The type of a template parameter corresponding to a specialized
4876 non-type argument shall not be dependent on a parameter of the
4877 specialization.
4878
4879 Also, we verify that pack expansions only occur at the
4880 end of the argument list. */
4881 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4882 tpd2.parms = 0;
4883 for (i = 0; i < nargs; ++i)
4884 {
4885 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4886 tree arg = TREE_VEC_ELT (inner_args, i);
4887 tree packed_args = NULL_TREE;
4888 int j, len = 1;
4889
4890 if (ARGUMENT_PACK_P (arg))
4891 {
4892 /* Extract the arguments from the argument pack. We'll be
4893 iterating over these in the following loop. */
4894 packed_args = ARGUMENT_PACK_ARGS (arg);
4895 len = TREE_VEC_LENGTH (packed_args);
4896 }
4897
4898 for (j = 0; j < len; j++)
4899 {
4900 if (packed_args)
4901 /* Get the Jth argument in the parameter pack. */
4902 arg = TREE_VEC_ELT (packed_args, j);
4903
4904 if (PACK_EXPANSION_P (arg))
4905 {
4906 /* Pack expansions must come at the end of the
4907 argument list. */
4908 if ((packed_args && j < len - 1)
4909 || (!packed_args && i < nargs - 1))
4910 {
4911 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4912 error ("parameter pack argument %qE must be at the "
4913 "end of the template argument list", arg);
4914 else
4915 error ("parameter pack argument %qT must be at the "
4916 "end of the template argument list", arg);
4917 }
4918 }
4919
4920 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4921 /* We only care about the pattern. */
4922 arg = PACK_EXPANSION_PATTERN (arg);
4923
4924 if (/* These first two lines are the `non-type' bit. */
4925 !TYPE_P (arg)
4926 && TREE_CODE (arg) != TEMPLATE_DECL
4927 /* This next two lines are the `argument expression is not just a
4928 simple identifier' condition and also the `specialized
4929 non-type argument' bit. */
4930 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4931 && !(REFERENCE_REF_P (arg)
4932 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4933 {
4934 if ((!packed_args && tpd.arg_uses_template_parms[i])
4935 || (packed_args && uses_template_parms (arg)))
4936 error ("template argument %qE involves template parameter(s)",
4937 arg);
4938 else
4939 {
4940 /* Look at the corresponding template parameter,
4941 marking which template parameters its type depends
4942 upon. */
4943 tree type = TREE_TYPE (parm);
4944
4945 if (!tpd2.parms)
4946 {
4947 /* We haven't yet initialized TPD2. Do so now. */
4948 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4949 /* The number of parameters here is the number in the
4950 main template, which, as checked in the assertion
4951 above, is NARGS. */
4952 tpd2.parms = XALLOCAVEC (int, nargs);
4953 tpd2.level =
4954 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4955 }
4956
4957 /* Mark the template parameters. But this time, we're
4958 looking for the template parameters of the main
4959 template, not in the specialization. */
4960 tpd2.current_arg = i;
4961 tpd2.arg_uses_template_parms[i] = 0;
4962 memset (tpd2.parms, 0, sizeof (int) * nargs);
4963 for_each_template_parm (type,
4964 &mark_template_parm,
4965 &tpd2,
4966 NULL,
4967 /*include_nondeduced_p=*/false);
4968
4969 if (tpd2.arg_uses_template_parms [i])
4970 {
4971 /* The type depended on some template parameters.
4972 If they are fully specialized in the
4973 specialization, that's OK. */
4974 int j;
4975 int count = 0;
4976 for (j = 0; j < nargs; ++j)
4977 if (tpd2.parms[j] != 0
4978 && tpd.arg_uses_template_parms [j])
4979 ++count;
4980 if (count != 0)
4981 error_n (input_location, count,
4982 "type %qT of template argument %qE depends "
4983 "on a template parameter",
4984 "type %qT of template argument %qE depends "
4985 "on template parameters",
4986 type,
4987 arg);
4988 }
4989 }
4990 }
4991 }
4992 }
4993
4994 /* We should only get here once. */
4995 if (TREE_CODE (decl) == TYPE_DECL)
4996 gcc_assert (!COMPLETE_TYPE_P (type));
4997
4998 // Build the template decl.
4999 tree tmpl = build_template_decl (decl, current_template_parms,
5000 DECL_MEMBER_TEMPLATE_P (maintmpl));
5001 TREE_TYPE (tmpl) = type;
5002 DECL_TEMPLATE_RESULT (tmpl) = decl;
5003 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5004 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5005 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5006
5007 /* Give template template parms a DECL_CONTEXT of the template
5008 for which they are a parameter. */
5009 for (i = 0; i < ntparms; ++i)
5010 {
5011 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5012 if (TREE_CODE (parm) == TEMPLATE_DECL)
5013 DECL_CONTEXT (parm) = tmpl;
5014 }
5015
5016 if (VAR_P (decl))
5017 /* We didn't register this in check_explicit_specialization so we could
5018 wait until the constraints were set. */
5019 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5020 else
5021 associate_classtype_constraints (type);
5022
5023 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5024 = tree_cons (specargs, tmpl,
5025 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5026 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5027
5028 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5029 inst = TREE_CHAIN (inst))
5030 {
5031 tree instance = TREE_VALUE (inst);
5032 if (TYPE_P (instance)
5033 ? (COMPLETE_TYPE_P (instance)
5034 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5035 : DECL_TEMPLATE_INSTANTIATION (instance))
5036 {
5037 tree spec = most_specialized_partial_spec (instance, tf_none);
5038 tree inst_decl = (DECL_P (instance)
5039 ? instance : TYPE_NAME (instance));
5040 if (!spec)
5041 /* OK */;
5042 else if (spec == error_mark_node)
5043 permerror (input_location,
5044 "declaration of %qD ambiguates earlier template "
5045 "instantiation for %qD", decl, inst_decl);
5046 else if (TREE_VALUE (spec) == tmpl)
5047 permerror (input_location,
5048 "partial specialization of %qD after instantiation "
5049 "of %qD", decl, inst_decl);
5050 }
5051 }
5052
5053 return decl;
5054 }
5055
5056 /* PARM is a template parameter of some form; return the corresponding
5057 TEMPLATE_PARM_INDEX. */
5058
5059 static tree
5060 get_template_parm_index (tree parm)
5061 {
5062 if (TREE_CODE (parm) == PARM_DECL
5063 || TREE_CODE (parm) == CONST_DECL)
5064 parm = DECL_INITIAL (parm);
5065 else if (TREE_CODE (parm) == TYPE_DECL
5066 || TREE_CODE (parm) == TEMPLATE_DECL)
5067 parm = TREE_TYPE (parm);
5068 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5069 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5070 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5071 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5072 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5073 return parm;
5074 }
5075
5076 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5077 parameter packs used by the template parameter PARM. */
5078
5079 static void
5080 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5081 {
5082 /* A type parm can't refer to another parm. */
5083 if (TREE_CODE (parm) == TYPE_DECL)
5084 return;
5085 else if (TREE_CODE (parm) == PARM_DECL)
5086 {
5087 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5088 ppd, ppd->visited);
5089 return;
5090 }
5091
5092 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5093
5094 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5095 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5096 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5097 }
5098
5099 /* PARM is a template parameter pack. Return any parameter packs used in
5100 its type or the type of any of its template parameters. If there are
5101 any such packs, it will be instantiated into a fixed template parameter
5102 list by partial instantiation rather than be fully deduced. */
5103
5104 tree
5105 fixed_parameter_pack_p (tree parm)
5106 {
5107 /* This can only be true in a member template. */
5108 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5109 return NULL_TREE;
5110 /* This can only be true for a parameter pack. */
5111 if (!template_parameter_pack_p (parm))
5112 return NULL_TREE;
5113 /* A type parm can't refer to another parm. */
5114 if (TREE_CODE (parm) == TYPE_DECL)
5115 return NULL_TREE;
5116
5117 tree parameter_packs = NULL_TREE;
5118 struct find_parameter_pack_data ppd;
5119 ppd.parameter_packs = &parameter_packs;
5120 ppd.visited = new hash_set<tree>;
5121 ppd.type_pack_expansion_p = false;
5122
5123 fixed_parameter_pack_p_1 (parm, &ppd);
5124
5125 delete ppd.visited;
5126 return parameter_packs;
5127 }
5128
5129 /* Check that a template declaration's use of default arguments and
5130 parameter packs is not invalid. Here, PARMS are the template
5131 parameters. IS_PRIMARY is true if DECL is the thing declared by
5132 a primary template. IS_PARTIAL is true if DECL is a partial
5133 specialization.
5134
5135 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5136 function template declaration or a friend class template
5137 declaration. In the function case, 1 indicates a declaration, 2
5138 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5139 emitted for extraneous default arguments.
5140
5141 Returns TRUE if there were no errors found, FALSE otherwise. */
5142
5143 bool
5144 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5145 bool is_partial, int is_friend_decl)
5146 {
5147 const char *msg;
5148 int last_level_to_check;
5149 tree parm_level;
5150 bool no_errors = true;
5151
5152 /* [temp.param]
5153
5154 A default template-argument shall not be specified in a
5155 function template declaration or a function template definition, nor
5156 in the template-parameter-list of the definition of a member of a
5157 class template. */
5158
5159 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5160 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5161 /* You can't have a function template declaration in a local
5162 scope, nor you can you define a member of a class template in a
5163 local scope. */
5164 return true;
5165
5166 if ((TREE_CODE (decl) == TYPE_DECL
5167 && TREE_TYPE (decl)
5168 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5169 || (TREE_CODE (decl) == FUNCTION_DECL
5170 && LAMBDA_FUNCTION_P (decl)))
5171 /* A lambda doesn't have an explicit declaration; don't complain
5172 about the parms of the enclosing class. */
5173 return true;
5174
5175 if (current_class_type
5176 && !TYPE_BEING_DEFINED (current_class_type)
5177 && DECL_LANG_SPECIFIC (decl)
5178 && DECL_DECLARES_FUNCTION_P (decl)
5179 /* If this is either a friend defined in the scope of the class
5180 or a member function. */
5181 && (DECL_FUNCTION_MEMBER_P (decl)
5182 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5183 : DECL_FRIEND_CONTEXT (decl)
5184 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5185 : false)
5186 /* And, if it was a member function, it really was defined in
5187 the scope of the class. */
5188 && (!DECL_FUNCTION_MEMBER_P (decl)
5189 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5190 /* We already checked these parameters when the template was
5191 declared, so there's no need to do it again now. This function
5192 was defined in class scope, but we're processing its body now
5193 that the class is complete. */
5194 return true;
5195
5196 /* Core issue 226 (C++0x only): the following only applies to class
5197 templates. */
5198 if (is_primary
5199 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5200 {
5201 /* [temp.param]
5202
5203 If a template-parameter has a default template-argument, all
5204 subsequent template-parameters shall have a default
5205 template-argument supplied. */
5206 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5207 {
5208 tree inner_parms = TREE_VALUE (parm_level);
5209 int ntparms = TREE_VEC_LENGTH (inner_parms);
5210 int seen_def_arg_p = 0;
5211 int i;
5212
5213 for (i = 0; i < ntparms; ++i)
5214 {
5215 tree parm = TREE_VEC_ELT (inner_parms, i);
5216
5217 if (parm == error_mark_node)
5218 continue;
5219
5220 if (TREE_PURPOSE (parm))
5221 seen_def_arg_p = 1;
5222 else if (seen_def_arg_p
5223 && !template_parameter_pack_p (TREE_VALUE (parm)))
5224 {
5225 error ("no default argument for %qD", TREE_VALUE (parm));
5226 /* For better subsequent error-recovery, we indicate that
5227 there should have been a default argument. */
5228 TREE_PURPOSE (parm) = error_mark_node;
5229 no_errors = false;
5230 }
5231 else if (!is_partial
5232 && !is_friend_decl
5233 /* Don't complain about an enclosing partial
5234 specialization. */
5235 && parm_level == parms
5236 && TREE_CODE (decl) == TYPE_DECL
5237 && i < ntparms - 1
5238 && template_parameter_pack_p (TREE_VALUE (parm))
5239 /* A fixed parameter pack will be partially
5240 instantiated into a fixed length list. */
5241 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5242 {
5243 /* A primary class template can only have one
5244 parameter pack, at the end of the template
5245 parameter list. */
5246
5247 error ("parameter pack %q+D must be at the end of the"
5248 " template parameter list", TREE_VALUE (parm));
5249
5250 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5251 = error_mark_node;
5252 no_errors = false;
5253 }
5254 }
5255 }
5256 }
5257
5258 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5259 || is_partial
5260 || !is_primary
5261 || is_friend_decl)
5262 /* For an ordinary class template, default template arguments are
5263 allowed at the innermost level, e.g.:
5264 template <class T = int>
5265 struct S {};
5266 but, in a partial specialization, they're not allowed even
5267 there, as we have in [temp.class.spec]:
5268
5269 The template parameter list of a specialization shall not
5270 contain default template argument values.
5271
5272 So, for a partial specialization, or for a function template
5273 (in C++98/C++03), we look at all of them. */
5274 ;
5275 else
5276 /* But, for a primary class template that is not a partial
5277 specialization we look at all template parameters except the
5278 innermost ones. */
5279 parms = TREE_CHAIN (parms);
5280
5281 /* Figure out what error message to issue. */
5282 if (is_friend_decl == 2)
5283 msg = G_("default template arguments may not be used in function template "
5284 "friend re-declaration");
5285 else if (is_friend_decl)
5286 msg = G_("default template arguments may not be used in template "
5287 "friend declarations");
5288 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5289 msg = G_("default template arguments may not be used in function templates "
5290 "without -std=c++11 or -std=gnu++11");
5291 else if (is_partial)
5292 msg = G_("default template arguments may not be used in "
5293 "partial specializations");
5294 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5295 msg = G_("default argument for template parameter for class enclosing %qD");
5296 else
5297 /* Per [temp.param]/9, "A default template-argument shall not be
5298 specified in the template-parameter-lists of the definition of
5299 a member of a class template that appears outside of the member's
5300 class.", thus if we aren't handling a member of a class template
5301 there is no need to examine the parameters. */
5302 return true;
5303
5304 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5305 /* If we're inside a class definition, there's no need to
5306 examine the parameters to the class itself. On the one
5307 hand, they will be checked when the class is defined, and,
5308 on the other, default arguments are valid in things like:
5309 template <class T = double>
5310 struct S { template <class U> void f(U); };
5311 Here the default argument for `S' has no bearing on the
5312 declaration of `f'. */
5313 last_level_to_check = template_class_depth (current_class_type) + 1;
5314 else
5315 /* Check everything. */
5316 last_level_to_check = 0;
5317
5318 for (parm_level = parms;
5319 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5320 parm_level = TREE_CHAIN (parm_level))
5321 {
5322 tree inner_parms = TREE_VALUE (parm_level);
5323 int i;
5324 int ntparms;
5325
5326 ntparms = TREE_VEC_LENGTH (inner_parms);
5327 for (i = 0; i < ntparms; ++i)
5328 {
5329 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5330 continue;
5331
5332 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5333 {
5334 if (msg)
5335 {
5336 no_errors = false;
5337 if (is_friend_decl == 2)
5338 return no_errors;
5339
5340 error (msg, decl);
5341 msg = 0;
5342 }
5343
5344 /* Clear out the default argument so that we are not
5345 confused later. */
5346 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5347 }
5348 }
5349
5350 /* At this point, if we're still interested in issuing messages,
5351 they must apply to classes surrounding the object declared. */
5352 if (msg)
5353 msg = G_("default argument for template parameter for class "
5354 "enclosing %qD");
5355 }
5356
5357 return no_errors;
5358 }
5359
5360 /* Worker for push_template_decl_real, called via
5361 for_each_template_parm. DATA is really an int, indicating the
5362 level of the parameters we are interested in. If T is a template
5363 parameter of that level, return nonzero. */
5364
5365 static int
5366 template_parm_this_level_p (tree t, void* data)
5367 {
5368 int this_level = *(int *)data;
5369 int level;
5370
5371 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5372 level = TEMPLATE_PARM_LEVEL (t);
5373 else
5374 level = TEMPLATE_TYPE_LEVEL (t);
5375 return level == this_level;
5376 }
5377
5378 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5379 DATA is really an int, indicating the innermost outer level of parameters.
5380 If T is a template parameter of that level or further out, return
5381 nonzero. */
5382
5383 static int
5384 template_parm_outer_level (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 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5397 parameters given by current_template_args, or reuses a
5398 previously existing one, if appropriate. Returns the DECL, or an
5399 equivalent one, if it is replaced via a call to duplicate_decls.
5400
5401 If IS_FRIEND is true, DECL is a friend declaration. */
5402
5403 tree
5404 push_template_decl_real (tree decl, bool is_friend)
5405 {
5406 tree tmpl;
5407 tree args;
5408 tree info;
5409 tree ctx;
5410 bool is_primary;
5411 bool is_partial;
5412 int new_template_p = 0;
5413 /* True if the template is a member template, in the sense of
5414 [temp.mem]. */
5415 bool member_template_p = false;
5416
5417 if (decl == error_mark_node || !current_template_parms)
5418 return error_mark_node;
5419
5420 /* See if this is a partial specialization. */
5421 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5422 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5423 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5424 || (VAR_P (decl)
5425 && DECL_LANG_SPECIFIC (decl)
5426 && DECL_TEMPLATE_SPECIALIZATION (decl)
5427 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5428
5429 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5430 is_friend = true;
5431
5432 if (is_friend)
5433 /* For a friend, we want the context of the friend, not
5434 the type of which it is a friend. */
5435 ctx = CP_DECL_CONTEXT (decl);
5436 else if (CP_DECL_CONTEXT (decl)
5437 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5438 /* In the case of a virtual function, we want the class in which
5439 it is defined. */
5440 ctx = CP_DECL_CONTEXT (decl);
5441 else
5442 /* Otherwise, if we're currently defining some class, the DECL
5443 is assumed to be a member of the class. */
5444 ctx = current_scope ();
5445
5446 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5447 ctx = NULL_TREE;
5448
5449 if (!DECL_CONTEXT (decl))
5450 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5451
5452 /* See if this is a primary template. */
5453 if (is_friend && ctx
5454 && uses_template_parms_level (ctx, processing_template_decl))
5455 /* A friend template that specifies a class context, i.e.
5456 template <typename T> friend void A<T>::f();
5457 is not primary. */
5458 is_primary = false;
5459 else if (TREE_CODE (decl) == TYPE_DECL
5460 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5461 is_primary = false;
5462 else
5463 is_primary = template_parm_scope_p ();
5464
5465 if (is_primary)
5466 {
5467 warning (OPT_Wtemplates, "template %qD declared", decl);
5468
5469 if (DECL_CLASS_SCOPE_P (decl))
5470 member_template_p = true;
5471 if (TREE_CODE (decl) == TYPE_DECL
5472 && anon_aggrname_p (DECL_NAME (decl)))
5473 {
5474 error ("template class without a name");
5475 return error_mark_node;
5476 }
5477 else if (TREE_CODE (decl) == FUNCTION_DECL)
5478 {
5479 if (member_template_p)
5480 {
5481 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5482 error ("member template %qD may not have virt-specifiers", decl);
5483 }
5484 if (DECL_DESTRUCTOR_P (decl))
5485 {
5486 /* [temp.mem]
5487
5488 A destructor shall not be a member template. */
5489 error ("destructor %qD declared as member template", decl);
5490 return error_mark_node;
5491 }
5492 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5493 && (!prototype_p (TREE_TYPE (decl))
5494 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5495 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5496 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5497 == void_list_node)))
5498 {
5499 /* [basic.stc.dynamic.allocation]
5500
5501 An allocation function can be a function
5502 template. ... Template allocation functions shall
5503 have two or more parameters. */
5504 error ("invalid template declaration of %qD", decl);
5505 return error_mark_node;
5506 }
5507 }
5508 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5509 && CLASS_TYPE_P (TREE_TYPE (decl)))
5510 {
5511 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5512 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5513 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5514 {
5515 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5516 if (TREE_CODE (t) == TYPE_DECL)
5517 t = TREE_TYPE (t);
5518 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5519 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5520 }
5521 }
5522 else if (TREE_CODE (decl) == TYPE_DECL
5523 && TYPE_DECL_ALIAS_P (decl))
5524 /* alias-declaration */
5525 gcc_assert (!DECL_ARTIFICIAL (decl));
5526 else if (VAR_P (decl))
5527 /* C++14 variable template. */;
5528 else
5529 {
5530 error ("template declaration of %q#D", decl);
5531 return error_mark_node;
5532 }
5533 }
5534
5535 /* Check to see that the rules regarding the use of default
5536 arguments are not being violated. We check args for a friend
5537 functions when we know whether it's a definition, introducing
5538 declaration or re-declaration. */
5539 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5540 check_default_tmpl_args (decl, current_template_parms,
5541 is_primary, is_partial, is_friend);
5542
5543 /* Ensure that there are no parameter packs in the type of this
5544 declaration that have not been expanded. */
5545 if (TREE_CODE (decl) == FUNCTION_DECL)
5546 {
5547 /* Check each of the arguments individually to see if there are
5548 any bare parameter packs. */
5549 tree type = TREE_TYPE (decl);
5550 tree arg = DECL_ARGUMENTS (decl);
5551 tree argtype = TYPE_ARG_TYPES (type);
5552
5553 while (arg && argtype)
5554 {
5555 if (!DECL_PACK_P (arg)
5556 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5557 {
5558 /* This is a PARM_DECL that contains unexpanded parameter
5559 packs. We have already complained about this in the
5560 check_for_bare_parameter_packs call, so just replace
5561 these types with ERROR_MARK_NODE. */
5562 TREE_TYPE (arg) = error_mark_node;
5563 TREE_VALUE (argtype) = error_mark_node;
5564 }
5565
5566 arg = DECL_CHAIN (arg);
5567 argtype = TREE_CHAIN (argtype);
5568 }
5569
5570 /* Check for bare parameter packs in the return type and the
5571 exception specifiers. */
5572 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5573 /* Errors were already issued, set return type to int
5574 as the frontend doesn't expect error_mark_node as
5575 the return type. */
5576 TREE_TYPE (type) = integer_type_node;
5577 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5578 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5579 }
5580 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5581 && TYPE_DECL_ALIAS_P (decl))
5582 ? DECL_ORIGINAL_TYPE (decl)
5583 : TREE_TYPE (decl)))
5584 {
5585 TREE_TYPE (decl) = error_mark_node;
5586 return error_mark_node;
5587 }
5588
5589 if (is_partial)
5590 return process_partial_specialization (decl);
5591
5592 args = current_template_args ();
5593
5594 if (!ctx
5595 || TREE_CODE (ctx) == FUNCTION_DECL
5596 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5597 || (TREE_CODE (decl) == TYPE_DECL
5598 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5599 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5600 {
5601 if (DECL_LANG_SPECIFIC (decl)
5602 && DECL_TEMPLATE_INFO (decl)
5603 && DECL_TI_TEMPLATE (decl))
5604 tmpl = DECL_TI_TEMPLATE (decl);
5605 /* If DECL is a TYPE_DECL for a class-template, then there won't
5606 be DECL_LANG_SPECIFIC. The information equivalent to
5607 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5608 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5609 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5610 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5611 {
5612 /* Since a template declaration already existed for this
5613 class-type, we must be redeclaring it here. Make sure
5614 that the redeclaration is valid. */
5615 redeclare_class_template (TREE_TYPE (decl),
5616 current_template_parms,
5617 current_template_constraints ());
5618 /* We don't need to create a new TEMPLATE_DECL; just use the
5619 one we already had. */
5620 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5621 }
5622 else
5623 {
5624 tmpl = build_template_decl (decl, current_template_parms,
5625 member_template_p);
5626 new_template_p = 1;
5627
5628 if (DECL_LANG_SPECIFIC (decl)
5629 && DECL_TEMPLATE_SPECIALIZATION (decl))
5630 {
5631 /* A specialization of a member template of a template
5632 class. */
5633 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5634 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5635 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5636 }
5637 }
5638 }
5639 else
5640 {
5641 tree a, t, current, parms;
5642 int i;
5643 tree tinfo = get_template_info (decl);
5644
5645 if (!tinfo)
5646 {
5647 error ("template definition of non-template %q#D", decl);
5648 return error_mark_node;
5649 }
5650
5651 tmpl = TI_TEMPLATE (tinfo);
5652
5653 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5654 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5655 && DECL_TEMPLATE_SPECIALIZATION (decl)
5656 && DECL_MEMBER_TEMPLATE_P (tmpl))
5657 {
5658 tree new_tmpl;
5659
5660 /* The declaration is a specialization of a member
5661 template, declared outside the class. Therefore, the
5662 innermost template arguments will be NULL, so we
5663 replace them with the arguments determined by the
5664 earlier call to check_explicit_specialization. */
5665 args = DECL_TI_ARGS (decl);
5666
5667 new_tmpl
5668 = build_template_decl (decl, current_template_parms,
5669 member_template_p);
5670 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5671 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5672 DECL_TI_TEMPLATE (decl) = new_tmpl;
5673 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5674 DECL_TEMPLATE_INFO (new_tmpl)
5675 = build_template_info (tmpl, args);
5676
5677 register_specialization (new_tmpl,
5678 most_general_template (tmpl),
5679 args,
5680 is_friend, 0);
5681 return decl;
5682 }
5683
5684 /* Make sure the template headers we got make sense. */
5685
5686 parms = DECL_TEMPLATE_PARMS (tmpl);
5687 i = TMPL_PARMS_DEPTH (parms);
5688 if (TMPL_ARGS_DEPTH (args) != i)
5689 {
5690 error ("expected %d levels of template parms for %q#D, got %d",
5691 i, decl, TMPL_ARGS_DEPTH (args));
5692 DECL_INTERFACE_KNOWN (decl) = 1;
5693 return error_mark_node;
5694 }
5695 else
5696 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5697 {
5698 a = TMPL_ARGS_LEVEL (args, i);
5699 t = INNERMOST_TEMPLATE_PARMS (parms);
5700
5701 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5702 {
5703 if (current == decl)
5704 error ("got %d template parameters for %q#D",
5705 TREE_VEC_LENGTH (a), decl);
5706 else
5707 error ("got %d template parameters for %q#T",
5708 TREE_VEC_LENGTH (a), current);
5709 error (" but %d required", TREE_VEC_LENGTH (t));
5710 /* Avoid crash in import_export_decl. */
5711 DECL_INTERFACE_KNOWN (decl) = 1;
5712 return error_mark_node;
5713 }
5714
5715 if (current == decl)
5716 current = ctx;
5717 else if (current == NULL_TREE)
5718 /* Can happen in erroneous input. */
5719 break;
5720 else
5721 current = get_containing_scope (current);
5722 }
5723
5724 /* Check that the parms are used in the appropriate qualifying scopes
5725 in the declarator. */
5726 if (!comp_template_args
5727 (TI_ARGS (tinfo),
5728 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5729 {
5730 error ("template arguments to %qD do not match original "
5731 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5732 if (!uses_template_parms (TI_ARGS (tinfo)))
5733 inform (input_location, "use %<template<>%> for"
5734 " an explicit specialization");
5735 /* Avoid crash in import_export_decl. */
5736 DECL_INTERFACE_KNOWN (decl) = 1;
5737 return error_mark_node;
5738 }
5739 }
5740
5741 DECL_TEMPLATE_RESULT (tmpl) = decl;
5742 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5743
5744 /* Push template declarations for global functions and types. Note
5745 that we do not try to push a global template friend declared in a
5746 template class; such a thing may well depend on the template
5747 parameters of the class. */
5748 if (new_template_p && !ctx
5749 && !(is_friend && template_class_depth (current_class_type) > 0))
5750 {
5751 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5752 if (tmpl == error_mark_node)
5753 return error_mark_node;
5754
5755 /* Hide template friend classes that haven't been declared yet. */
5756 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5757 {
5758 DECL_ANTICIPATED (tmpl) = 1;
5759 DECL_FRIEND_P (tmpl) = 1;
5760 }
5761 }
5762
5763 if (is_primary)
5764 {
5765 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5766
5767 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5768
5769 /* Give template template parms a DECL_CONTEXT of the template
5770 for which they are a parameter. */
5771 parms = INNERMOST_TEMPLATE_PARMS (parms);
5772 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5773 {
5774 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5775 if (TREE_CODE (parm) == TEMPLATE_DECL)
5776 DECL_CONTEXT (parm) = tmpl;
5777 }
5778
5779 if (TREE_CODE (decl) == TYPE_DECL
5780 && TYPE_DECL_ALIAS_P (decl)
5781 && complex_alias_template_p (tmpl))
5782 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5783 }
5784
5785 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5786 back to its most general template. If TMPL is a specialization,
5787 ARGS may only have the innermost set of arguments. Add the missing
5788 argument levels if necessary. */
5789 if (DECL_TEMPLATE_INFO (tmpl))
5790 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5791
5792 info = build_template_info (tmpl, args);
5793
5794 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5795 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5796 else
5797 {
5798 if (is_primary)
5799 retrofit_lang_decl (decl);
5800 if (DECL_LANG_SPECIFIC (decl))
5801 DECL_TEMPLATE_INFO (decl) = info;
5802 }
5803
5804 if (flag_implicit_templates
5805 && !is_friend
5806 && TREE_PUBLIC (decl)
5807 && VAR_OR_FUNCTION_DECL_P (decl))
5808 /* Set DECL_COMDAT on template instantiations; if we force
5809 them to be emitted by explicit instantiation or -frepo,
5810 mark_needed will tell cgraph to do the right thing. */
5811 DECL_COMDAT (decl) = true;
5812
5813 return DECL_TEMPLATE_RESULT (tmpl);
5814 }
5815
5816 tree
5817 push_template_decl (tree decl)
5818 {
5819 return push_template_decl_real (decl, false);
5820 }
5821
5822 /* FN is an inheriting constructor that inherits from the constructor
5823 template INHERITED; turn FN into a constructor template with a matching
5824 template header. */
5825
5826 tree
5827 add_inherited_template_parms (tree fn, tree inherited)
5828 {
5829 tree inner_parms
5830 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5831 inner_parms = copy_node (inner_parms);
5832 tree parms
5833 = tree_cons (size_int (processing_template_decl + 1),
5834 inner_parms, current_template_parms);
5835 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5836 tree args = template_parms_to_args (parms);
5837 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5838 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5839 DECL_TEMPLATE_RESULT (tmpl) = fn;
5840 DECL_ARTIFICIAL (tmpl) = true;
5841 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5842 return tmpl;
5843 }
5844
5845 /* Called when a class template TYPE is redeclared with the indicated
5846 template PARMS, e.g.:
5847
5848 template <class T> struct S;
5849 template <class T> struct S {}; */
5850
5851 bool
5852 redeclare_class_template (tree type, tree parms, tree cons)
5853 {
5854 tree tmpl;
5855 tree tmpl_parms;
5856 int i;
5857
5858 if (!TYPE_TEMPLATE_INFO (type))
5859 {
5860 error ("%qT is not a template type", type);
5861 return false;
5862 }
5863
5864 tmpl = TYPE_TI_TEMPLATE (type);
5865 if (!PRIMARY_TEMPLATE_P (tmpl))
5866 /* The type is nested in some template class. Nothing to worry
5867 about here; there are no new template parameters for the nested
5868 type. */
5869 return true;
5870
5871 if (!parms)
5872 {
5873 error ("template specifiers not specified in declaration of %qD",
5874 tmpl);
5875 return false;
5876 }
5877
5878 parms = INNERMOST_TEMPLATE_PARMS (parms);
5879 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5880
5881 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5882 {
5883 error_n (input_location, TREE_VEC_LENGTH (parms),
5884 "redeclared with %d template parameter",
5885 "redeclared with %d template parameters",
5886 TREE_VEC_LENGTH (parms));
5887 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5888 "previous declaration %qD used %d template parameter",
5889 "previous declaration %qD used %d template parameters",
5890 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5891 return false;
5892 }
5893
5894 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5895 {
5896 tree tmpl_parm;
5897 tree parm;
5898 tree tmpl_default;
5899 tree parm_default;
5900
5901 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5902 || TREE_VEC_ELT (parms, i) == error_mark_node)
5903 continue;
5904
5905 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5906 if (error_operand_p (tmpl_parm))
5907 return false;
5908
5909 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5910 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5911 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5912
5913 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5914 TEMPLATE_DECL. */
5915 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5916 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5917 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5918 || (TREE_CODE (tmpl_parm) != PARM_DECL
5919 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5920 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5921 || (TREE_CODE (tmpl_parm) == PARM_DECL
5922 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5923 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5924 {
5925 error ("template parameter %q+#D", tmpl_parm);
5926 error ("redeclared here as %q#D", parm);
5927 return false;
5928 }
5929
5930 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5931 {
5932 /* We have in [temp.param]:
5933
5934 A template-parameter may not be given default arguments
5935 by two different declarations in the same scope. */
5936 error_at (input_location, "redefinition of default argument for %q#D", parm);
5937 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5938 "original definition appeared here");
5939 return false;
5940 }
5941
5942 if (parm_default != NULL_TREE)
5943 /* Update the previous template parameters (which are the ones
5944 that will really count) with the new default value. */
5945 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5946 else if (tmpl_default != NULL_TREE)
5947 /* Update the new parameters, too; they'll be used as the
5948 parameters for any members. */
5949 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5950
5951 /* Give each template template parm in this redeclaration a
5952 DECL_CONTEXT of the template for which they are a parameter. */
5953 if (TREE_CODE (parm) == TEMPLATE_DECL)
5954 {
5955 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5956 DECL_CONTEXT (parm) = tmpl;
5957 }
5958
5959 if (TREE_CODE (parm) == TYPE_DECL)
5960 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5961 }
5962
5963 // Cannot redeclare a class template with a different set of constraints.
5964 if (!equivalent_constraints (get_constraints (tmpl), cons))
5965 {
5966 error_at (input_location, "redeclaration %q#D with different "
5967 "constraints", tmpl);
5968 inform (DECL_SOURCE_LOCATION (tmpl),
5969 "original declaration appeared here");
5970 }
5971
5972 return true;
5973 }
5974
5975 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5976 to be used when the caller has already checked
5977 (processing_template_decl
5978 && !instantiation_dependent_expression_p (expr)
5979 && potential_constant_expression (expr))
5980 and cleared processing_template_decl. */
5981
5982 tree
5983 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5984 {
5985 return tsubst_copy_and_build (expr,
5986 /*args=*/NULL_TREE,
5987 complain,
5988 /*in_decl=*/NULL_TREE,
5989 /*function_p=*/false,
5990 /*integral_constant_expression_p=*/true);
5991 }
5992
5993 /* Simplify EXPR if it is a non-dependent expression. Returns the
5994 (possibly simplified) expression. */
5995
5996 tree
5997 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5998 {
5999 if (expr == NULL_TREE)
6000 return NULL_TREE;
6001
6002 /* If we're in a template, but EXPR isn't value dependent, simplify
6003 it. We're supposed to treat:
6004
6005 template <typename T> void f(T[1 + 1]);
6006 template <typename T> void f(T[2]);
6007
6008 as two declarations of the same function, for example. */
6009 if (processing_template_decl
6010 && is_nondependent_constant_expression (expr))
6011 {
6012 processing_template_decl_sentinel s;
6013 expr = instantiate_non_dependent_expr_internal (expr, complain);
6014 }
6015 return expr;
6016 }
6017
6018 tree
6019 instantiate_non_dependent_expr (tree expr)
6020 {
6021 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6022 }
6023
6024 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6025 an uninstantiated expression. */
6026
6027 tree
6028 instantiate_non_dependent_or_null (tree expr)
6029 {
6030 if (expr == NULL_TREE)
6031 return NULL_TREE;
6032 if (processing_template_decl)
6033 {
6034 if (!is_nondependent_constant_expression (expr))
6035 expr = NULL_TREE;
6036 else
6037 {
6038 processing_template_decl_sentinel s;
6039 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6040 }
6041 }
6042 return expr;
6043 }
6044
6045 /* True iff T is a specialization of a variable template. */
6046
6047 bool
6048 variable_template_specialization_p (tree t)
6049 {
6050 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6051 return false;
6052 tree tmpl = DECL_TI_TEMPLATE (t);
6053 return variable_template_p (tmpl);
6054 }
6055
6056 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6057 template declaration, or a TYPE_DECL for an alias declaration. */
6058
6059 bool
6060 alias_type_or_template_p (tree t)
6061 {
6062 if (t == NULL_TREE)
6063 return false;
6064 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6065 || (TYPE_P (t)
6066 && TYPE_NAME (t)
6067 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6068 || DECL_ALIAS_TEMPLATE_P (t));
6069 }
6070
6071 /* Return TRUE iff T is a specialization of an alias template. */
6072
6073 bool
6074 alias_template_specialization_p (const_tree t)
6075 {
6076 /* It's an alias template specialization if it's an alias and its
6077 TYPE_NAME is a specialization of a primary template. */
6078 if (TYPE_ALIAS_P (t))
6079 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6080 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6081
6082 return false;
6083 }
6084
6085 /* An alias template is complex from a SFINAE perspective if a template-id
6086 using that alias can be ill-formed when the expansion is not, as with
6087 the void_t template. We determine this by checking whether the
6088 expansion for the alias template uses all its template parameters. */
6089
6090 struct uses_all_template_parms_data
6091 {
6092 int level;
6093 bool *seen;
6094 };
6095
6096 static int
6097 uses_all_template_parms_r (tree t, void *data_)
6098 {
6099 struct uses_all_template_parms_data &data
6100 = *(struct uses_all_template_parms_data*)data_;
6101 tree idx = get_template_parm_index (t);
6102
6103 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6104 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6105 return 0;
6106 }
6107
6108 static bool
6109 complex_alias_template_p (const_tree tmpl)
6110 {
6111 struct uses_all_template_parms_data data;
6112 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6113 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6114 data.level = TMPL_PARMS_DEPTH (parms);
6115 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6116 data.seen = XALLOCAVEC (bool, len);
6117 for (int i = 0; i < len; ++i)
6118 data.seen[i] = false;
6119
6120 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6121 for (int i = 0; i < len; ++i)
6122 if (!data.seen[i])
6123 return true;
6124 return false;
6125 }
6126
6127 /* Return TRUE iff T is a specialization of a complex alias template with
6128 dependent template-arguments. */
6129
6130 bool
6131 dependent_alias_template_spec_p (const_tree t)
6132 {
6133 if (!alias_template_specialization_p (t))
6134 return false;
6135
6136 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6137 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6138 return false;
6139
6140 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6141 if (!any_dependent_template_arguments_p (args))
6142 return false;
6143
6144 return true;
6145 }
6146
6147 /* Return the number of innermost template parameters in TMPL. */
6148
6149 static int
6150 num_innermost_template_parms (tree tmpl)
6151 {
6152 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6153 return TREE_VEC_LENGTH (parms);
6154 }
6155
6156 /* Return either TMPL or another template that it is equivalent to under DR
6157 1286: An alias that just changes the name of a template is equivalent to
6158 the other template. */
6159
6160 static tree
6161 get_underlying_template (tree tmpl)
6162 {
6163 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6164 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6165 {
6166 /* Determine if the alias is equivalent to an underlying template. */
6167 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6168 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6169 if (!tinfo)
6170 break;
6171
6172 tree underlying = TI_TEMPLATE (tinfo);
6173 if (!PRIMARY_TEMPLATE_P (underlying)
6174 || (num_innermost_template_parms (tmpl)
6175 != num_innermost_template_parms (underlying)))
6176 break;
6177
6178 tree alias_args = INNERMOST_TEMPLATE_ARGS
6179 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6180 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6181 break;
6182
6183 /* Alias is equivalent. Strip it and repeat. */
6184 tmpl = underlying;
6185 }
6186
6187 return tmpl;
6188 }
6189
6190 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6191 must be a reference-to-function or a pointer-to-function type, as specified
6192 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6193 and check that the resulting function has external linkage. */
6194
6195 static tree
6196 convert_nontype_argument_function (tree type, tree expr,
6197 tsubst_flags_t complain)
6198 {
6199 tree fns = expr;
6200 tree fn, fn_no_ptr;
6201 linkage_kind linkage;
6202
6203 fn = instantiate_type (type, fns, tf_none);
6204 if (fn == error_mark_node)
6205 return error_mark_node;
6206
6207 if (value_dependent_expression_p (fn))
6208 goto accept;
6209
6210 fn_no_ptr = strip_fnptr_conv (fn);
6211 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6212 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6213 if (BASELINK_P (fn_no_ptr))
6214 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6215
6216 /* [temp.arg.nontype]/1
6217
6218 A template-argument for a non-type, non-template template-parameter
6219 shall be one of:
6220 [...]
6221 -- the address of an object or function with external [C++11: or
6222 internal] linkage. */
6223
6224 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6225 {
6226 if (complain & tf_error)
6227 {
6228 error ("%qE is not a valid template argument for type %qT",
6229 expr, type);
6230 if (TYPE_PTR_P (type))
6231 inform (input_location, "it must be the address of a function "
6232 "with external linkage");
6233 else
6234 inform (input_location, "it must be the name of a function with "
6235 "external linkage");
6236 }
6237 return NULL_TREE;
6238 }
6239
6240 linkage = decl_linkage (fn_no_ptr);
6241 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6242 {
6243 if (complain & tf_error)
6244 {
6245 if (cxx_dialect >= cxx11)
6246 error ("%qE is not a valid template argument for type %qT "
6247 "because %qD has no linkage",
6248 expr, type, fn_no_ptr);
6249 else
6250 error ("%qE is not a valid template argument for type %qT "
6251 "because %qD does not have external linkage",
6252 expr, type, fn_no_ptr);
6253 }
6254 return NULL_TREE;
6255 }
6256
6257 accept:
6258 if (TREE_CODE (type) == REFERENCE_TYPE)
6259 {
6260 if (REFERENCE_REF_P (fn))
6261 fn = TREE_OPERAND (fn, 0);
6262 else
6263 fn = build_address (fn);
6264 }
6265 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6266 fn = build_nop (type, fn);
6267
6268 return fn;
6269 }
6270
6271 /* Subroutine of convert_nontype_argument.
6272 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6273 Emit an error otherwise. */
6274
6275 static bool
6276 check_valid_ptrmem_cst_expr (tree type, tree expr,
6277 tsubst_flags_t complain)
6278 {
6279 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6280 tree orig_expr = expr;
6281 STRIP_NOPS (expr);
6282 if (null_ptr_cst_p (expr))
6283 return true;
6284 if (TREE_CODE (expr) == PTRMEM_CST
6285 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6286 PTRMEM_CST_CLASS (expr)))
6287 return true;
6288 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6289 return true;
6290 if (processing_template_decl
6291 && TREE_CODE (expr) == ADDR_EXPR
6292 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6293 return true;
6294 if (complain & tf_error)
6295 {
6296 error_at (loc, "%qE is not a valid template argument for type %qT",
6297 orig_expr, type);
6298 if (TREE_CODE (expr) != PTRMEM_CST)
6299 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6300 else
6301 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6302 }
6303 return false;
6304 }
6305
6306 /* Returns TRUE iff the address of OP is value-dependent.
6307
6308 14.6.2.4 [temp.dep.temp]:
6309 A non-integral non-type template-argument is dependent if its type is
6310 dependent or it has either of the following forms
6311 qualified-id
6312 & qualified-id
6313 and contains a nested-name-specifier which specifies a class-name that
6314 names a dependent type.
6315
6316 We generalize this to just say that the address of a member of a
6317 dependent class is value-dependent; the above doesn't cover the
6318 address of a static data member named with an unqualified-id. */
6319
6320 static bool
6321 has_value_dependent_address (tree op)
6322 {
6323 /* We could use get_inner_reference here, but there's no need;
6324 this is only relevant for template non-type arguments, which
6325 can only be expressed as &id-expression. */
6326 if (DECL_P (op))
6327 {
6328 tree ctx = CP_DECL_CONTEXT (op);
6329 if (TYPE_P (ctx) && dependent_type_p (ctx))
6330 return true;
6331 }
6332
6333 return false;
6334 }
6335
6336 /* The next set of functions are used for providing helpful explanatory
6337 diagnostics for failed overload resolution. Their messages should be
6338 indented by two spaces for consistency with the messages in
6339 call.c */
6340
6341 static int
6342 unify_success (bool /*explain_p*/)
6343 {
6344 return 0;
6345 }
6346
6347 /* Other failure functions should call this one, to provide a single function
6348 for setting a breakpoint on. */
6349
6350 static int
6351 unify_invalid (bool /*explain_p*/)
6352 {
6353 return 1;
6354 }
6355
6356 static int
6357 unify_parameter_deduction_failure (bool explain_p, tree parm)
6358 {
6359 if (explain_p)
6360 inform (input_location,
6361 " couldn't deduce template parameter %qD", parm);
6362 return unify_invalid (explain_p);
6363 }
6364
6365 static int
6366 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6367 {
6368 if (explain_p)
6369 inform (input_location,
6370 " types %qT and %qT have incompatible cv-qualifiers",
6371 parm, arg);
6372 return unify_invalid (explain_p);
6373 }
6374
6375 static int
6376 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6377 {
6378 if (explain_p)
6379 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6380 return unify_invalid (explain_p);
6381 }
6382
6383 static int
6384 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6385 {
6386 if (explain_p)
6387 inform (input_location,
6388 " template parameter %qD is not a parameter pack, but "
6389 "argument %qD is",
6390 parm, arg);
6391 return unify_invalid (explain_p);
6392 }
6393
6394 static int
6395 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6396 {
6397 if (explain_p)
6398 inform (input_location,
6399 " template argument %qE does not match "
6400 "pointer-to-member constant %qE",
6401 arg, parm);
6402 return unify_invalid (explain_p);
6403 }
6404
6405 static int
6406 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6407 {
6408 if (explain_p)
6409 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6410 return unify_invalid (explain_p);
6411 }
6412
6413 static int
6414 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6415 {
6416 if (explain_p)
6417 inform (input_location,
6418 " inconsistent parameter pack deduction with %qT and %qT",
6419 old_arg, new_arg);
6420 return unify_invalid (explain_p);
6421 }
6422
6423 static int
6424 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6425 {
6426 if (explain_p)
6427 {
6428 if (TYPE_P (parm))
6429 inform (input_location,
6430 " deduced conflicting types for parameter %qT (%qT and %qT)",
6431 parm, first, second);
6432 else
6433 inform (input_location,
6434 " deduced conflicting values for non-type parameter "
6435 "%qE (%qE and %qE)", parm, first, second);
6436 }
6437 return unify_invalid (explain_p);
6438 }
6439
6440 static int
6441 unify_vla_arg (bool explain_p, tree arg)
6442 {
6443 if (explain_p)
6444 inform (input_location,
6445 " variable-sized array type %qT is not "
6446 "a valid template argument",
6447 arg);
6448 return unify_invalid (explain_p);
6449 }
6450
6451 static int
6452 unify_method_type_error (bool explain_p, tree arg)
6453 {
6454 if (explain_p)
6455 inform (input_location,
6456 " member function type %qT is not a valid template argument",
6457 arg);
6458 return unify_invalid (explain_p);
6459 }
6460
6461 static int
6462 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6463 {
6464 if (explain_p)
6465 {
6466 if (least_p)
6467 inform_n (input_location, wanted,
6468 " candidate expects at least %d argument, %d provided",
6469 " candidate expects at least %d arguments, %d provided",
6470 wanted, have);
6471 else
6472 inform_n (input_location, wanted,
6473 " candidate expects %d argument, %d provided",
6474 " candidate expects %d arguments, %d provided",
6475 wanted, have);
6476 }
6477 return unify_invalid (explain_p);
6478 }
6479
6480 static int
6481 unify_too_many_arguments (bool explain_p, int have, int wanted)
6482 {
6483 return unify_arity (explain_p, have, wanted);
6484 }
6485
6486 static int
6487 unify_too_few_arguments (bool explain_p, int have, int wanted,
6488 bool least_p = false)
6489 {
6490 return unify_arity (explain_p, have, wanted, least_p);
6491 }
6492
6493 static int
6494 unify_arg_conversion (bool explain_p, tree to_type,
6495 tree from_type, tree arg)
6496 {
6497 if (explain_p)
6498 inform (EXPR_LOC_OR_LOC (arg, input_location),
6499 " cannot convert %qE (type %qT) to type %qT",
6500 arg, from_type, to_type);
6501 return unify_invalid (explain_p);
6502 }
6503
6504 static int
6505 unify_no_common_base (bool explain_p, enum template_base_result r,
6506 tree parm, tree arg)
6507 {
6508 if (explain_p)
6509 switch (r)
6510 {
6511 case tbr_ambiguous_baseclass:
6512 inform (input_location, " %qT is an ambiguous base class of %qT",
6513 parm, arg);
6514 break;
6515 default:
6516 inform (input_location, " %qT is not derived from %qT", arg, parm);
6517 break;
6518 }
6519 return unify_invalid (explain_p);
6520 }
6521
6522 static int
6523 unify_inconsistent_template_template_parameters (bool explain_p)
6524 {
6525 if (explain_p)
6526 inform (input_location,
6527 " template parameters of a template template argument are "
6528 "inconsistent with other deduced template arguments");
6529 return unify_invalid (explain_p);
6530 }
6531
6532 static int
6533 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6534 {
6535 if (explain_p)
6536 inform (input_location,
6537 " can't deduce a template for %qT from non-template type %qT",
6538 parm, arg);
6539 return unify_invalid (explain_p);
6540 }
6541
6542 static int
6543 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6544 {
6545 if (explain_p)
6546 inform (input_location,
6547 " template argument %qE does not match %qE", arg, parm);
6548 return unify_invalid (explain_p);
6549 }
6550
6551 /* Attempt to convert the non-type template parameter EXPR to the
6552 indicated TYPE. If the conversion is successful, return the
6553 converted value. If the conversion is unsuccessful, return
6554 NULL_TREE if we issued an error message, or error_mark_node if we
6555 did not. We issue error messages for out-and-out bad template
6556 parameters, but not simply because the conversion failed, since we
6557 might be just trying to do argument deduction. Both TYPE and EXPR
6558 must be non-dependent.
6559
6560 The conversion follows the special rules described in
6561 [temp.arg.nontype], and it is much more strict than an implicit
6562 conversion.
6563
6564 This function is called twice for each template argument (see
6565 lookup_template_class for a more accurate description of this
6566 problem). This means that we need to handle expressions which
6567 are not valid in a C++ source, but can be created from the
6568 first call (for instance, casts to perform conversions). These
6569 hacks can go away after we fix the double coercion problem. */
6570
6571 static tree
6572 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6573 {
6574 tree expr_type;
6575 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6576 tree orig_expr = expr;
6577
6578 /* Detect immediately string literals as invalid non-type argument.
6579 This special-case is not needed for correctness (we would easily
6580 catch this later), but only to provide better diagnostic for this
6581 common user mistake. As suggested by DR 100, we do not mention
6582 linkage issues in the diagnostic as this is not the point. */
6583 /* FIXME we're making this OK. */
6584 if (TREE_CODE (expr) == STRING_CST)
6585 {
6586 if (complain & tf_error)
6587 error ("%qE is not a valid template argument for type %qT "
6588 "because string literals can never be used in this context",
6589 expr, type);
6590 return NULL_TREE;
6591 }
6592
6593 /* Add the ADDR_EXPR now for the benefit of
6594 value_dependent_expression_p. */
6595 if (TYPE_PTROBV_P (type)
6596 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6597 {
6598 expr = decay_conversion (expr, complain);
6599 if (expr == error_mark_node)
6600 return error_mark_node;
6601 }
6602
6603 /* If we are in a template, EXPR may be non-dependent, but still
6604 have a syntactic, rather than semantic, form. For example, EXPR
6605 might be a SCOPE_REF, rather than the VAR_DECL to which the
6606 SCOPE_REF refers. Preserving the qualifying scope is necessary
6607 so that access checking can be performed when the template is
6608 instantiated -- but here we need the resolved form so that we can
6609 convert the argument. */
6610 bool non_dep = false;
6611 if (TYPE_REF_OBJ_P (type)
6612 && has_value_dependent_address (expr))
6613 /* If we want the address and it's value-dependent, don't fold. */;
6614 else if (processing_template_decl
6615 && is_nondependent_constant_expression (expr))
6616 non_dep = true;
6617 if (error_operand_p (expr))
6618 return error_mark_node;
6619 expr_type = TREE_TYPE (expr);
6620
6621 /* If the argument is non-dependent, perform any conversions in
6622 non-dependent context as well. */
6623 processing_template_decl_sentinel s (non_dep);
6624 if (non_dep)
6625 expr = instantiate_non_dependent_expr_internal (expr, complain);
6626
6627 if (value_dependent_expression_p (expr))
6628 expr = canonicalize_expr_argument (expr, complain);
6629
6630 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6631 to a non-type argument of "nullptr". */
6632 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6633 expr = fold_simple (convert (type, expr));
6634
6635 /* In C++11, integral or enumeration non-type template arguments can be
6636 arbitrary constant expressions. Pointer and pointer to
6637 member arguments can be general constant expressions that evaluate
6638 to a null value, but otherwise still need to be of a specific form. */
6639 if (cxx_dialect >= cxx11)
6640 {
6641 if (TREE_CODE (expr) == PTRMEM_CST)
6642 /* A PTRMEM_CST is already constant, and a valid template
6643 argument for a parameter of pointer to member type, we just want
6644 to leave it in that form rather than lower it to a
6645 CONSTRUCTOR. */;
6646 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6647 || cxx_dialect >= cxx17)
6648 {
6649 /* C++17: A template-argument for a non-type template-parameter shall
6650 be a converted constant expression (8.20) of the type of the
6651 template-parameter. */
6652 expr = build_converted_constant_expr (type, expr, complain);
6653 if (expr == error_mark_node)
6654 return error_mark_node;
6655 expr = maybe_constant_value (expr);
6656 expr = convert_from_reference (expr);
6657 }
6658 else if (TYPE_PTR_OR_PTRMEM_P (type))
6659 {
6660 tree folded = maybe_constant_value (expr);
6661 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6662 : null_member_pointer_value_p (folded))
6663 expr = folded;
6664 }
6665 }
6666
6667 if (TREE_CODE (type) == REFERENCE_TYPE)
6668 expr = mark_lvalue_use (expr);
6669 else
6670 expr = mark_rvalue_use (expr);
6671
6672 /* HACK: Due to double coercion, we can get a
6673 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6674 which is the tree that we built on the first call (see
6675 below when coercing to reference to object or to reference to
6676 function). We just strip everything and get to the arg.
6677 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6678 for examples. */
6679 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6680 {
6681 tree probe_type, probe = expr;
6682 if (REFERENCE_REF_P (probe))
6683 probe = TREE_OPERAND (probe, 0);
6684 probe_type = TREE_TYPE (probe);
6685 if (TREE_CODE (probe) == NOP_EXPR)
6686 {
6687 /* ??? Maybe we could use convert_from_reference here, but we
6688 would need to relax its constraints because the NOP_EXPR
6689 could actually change the type to something more cv-qualified,
6690 and this is not folded by convert_from_reference. */
6691 tree addr = TREE_OPERAND (probe, 0);
6692 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6693 && TREE_CODE (addr) == ADDR_EXPR
6694 && TYPE_PTR_P (TREE_TYPE (addr))
6695 && (same_type_ignoring_top_level_qualifiers_p
6696 (TREE_TYPE (probe_type),
6697 TREE_TYPE (TREE_TYPE (addr)))))
6698 {
6699 expr = TREE_OPERAND (addr, 0);
6700 expr_type = TREE_TYPE (probe_type);
6701 }
6702 }
6703 }
6704
6705 /* [temp.arg.nontype]/5, bullet 1
6706
6707 For a non-type template-parameter of integral or enumeration type,
6708 integral promotions (_conv.prom_) and integral conversions
6709 (_conv.integral_) are applied. */
6710 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6711 {
6712 if (cxx_dialect < cxx11)
6713 {
6714 tree t = build_converted_constant_expr (type, expr, complain);
6715 t = maybe_constant_value (t);
6716 if (t != error_mark_node)
6717 expr = t;
6718 }
6719
6720 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6721 return error_mark_node;
6722
6723 /* Notice that there are constant expressions like '4 % 0' which
6724 do not fold into integer constants. */
6725 if (TREE_CODE (expr) != INTEGER_CST
6726 && !value_dependent_expression_p (expr))
6727 {
6728 if (complain & tf_error)
6729 {
6730 int errs = errorcount, warns = warningcount + werrorcount;
6731 if (!require_potential_constant_expression (expr))
6732 expr = error_mark_node;
6733 else
6734 expr = cxx_constant_value (expr);
6735 if (errorcount > errs || warningcount + werrorcount > warns)
6736 inform (loc, "in template argument for type %qT ", type);
6737 if (expr == error_mark_node)
6738 return NULL_TREE;
6739 /* else cxx_constant_value complained but gave us
6740 a real constant, so go ahead. */
6741 if (TREE_CODE (expr) != INTEGER_CST)
6742 {
6743 /* Some assemble time constant expressions like
6744 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6745 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6746 as we can emit them into .rodata initializers of
6747 variables, yet they can't fold into an INTEGER_CST at
6748 compile time. Refuse them here. */
6749 gcc_checking_assert (reduced_constant_expression_p (expr));
6750 error_at (loc, "template argument %qE for type %qT not "
6751 "a constant integer", expr, type);
6752 return NULL_TREE;
6753 }
6754 }
6755 else
6756 return NULL_TREE;
6757 }
6758
6759 /* Avoid typedef problems. */
6760 if (TREE_TYPE (expr) != type)
6761 expr = fold_convert (type, expr);
6762 }
6763 /* [temp.arg.nontype]/5, bullet 2
6764
6765 For a non-type template-parameter of type pointer to object,
6766 qualification conversions (_conv.qual_) and the array-to-pointer
6767 conversion (_conv.array_) are applied. */
6768 else if (TYPE_PTROBV_P (type))
6769 {
6770 tree decayed = expr;
6771
6772 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6773 decay_conversion or an explicit cast. If it's a problematic cast,
6774 we'll complain about it below. */
6775 if (TREE_CODE (expr) == NOP_EXPR)
6776 {
6777 tree probe = expr;
6778 STRIP_NOPS (probe);
6779 if (TREE_CODE (probe) == ADDR_EXPR
6780 && TYPE_PTR_P (TREE_TYPE (probe)))
6781 {
6782 expr = probe;
6783 expr_type = TREE_TYPE (expr);
6784 }
6785 }
6786
6787 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6788
6789 A template-argument for a non-type, non-template template-parameter
6790 shall be one of: [...]
6791
6792 -- the name of a non-type template-parameter;
6793 -- the address of an object or function with external linkage, [...]
6794 expressed as "& id-expression" where the & is optional if the name
6795 refers to a function or array, or if the corresponding
6796 template-parameter is a reference.
6797
6798 Here, we do not care about functions, as they are invalid anyway
6799 for a parameter of type pointer-to-object. */
6800
6801 if (value_dependent_expression_p (expr))
6802 /* Non-type template parameters are OK. */
6803 ;
6804 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6805 /* Null pointer values are OK in C++11. */;
6806 else if (TREE_CODE (expr) != ADDR_EXPR)
6807 {
6808 if (VAR_P (expr))
6809 {
6810 if (complain & tf_error)
6811 error ("%qD is not a valid template argument "
6812 "because %qD is a variable, not the address of "
6813 "a variable", orig_expr, expr);
6814 return NULL_TREE;
6815 }
6816 if (POINTER_TYPE_P (expr_type))
6817 {
6818 if (complain & tf_error)
6819 error ("%qE is not a valid template argument for %qT "
6820 "because it is not the address of a variable",
6821 orig_expr, type);
6822 return NULL_TREE;
6823 }
6824 /* Other values, like integer constants, might be valid
6825 non-type arguments of some other type. */
6826 return error_mark_node;
6827 }
6828 else
6829 {
6830 tree decl = TREE_OPERAND (expr, 0);
6831
6832 if (!VAR_P (decl))
6833 {
6834 if (complain & tf_error)
6835 error ("%qE is not a valid template argument of type %qT "
6836 "because %qE is not a variable", orig_expr, type, decl);
6837 return NULL_TREE;
6838 }
6839 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6840 {
6841 if (complain & tf_error)
6842 error ("%qE is not a valid template argument of type %qT "
6843 "because %qD does not have external linkage",
6844 orig_expr, type, decl);
6845 return NULL_TREE;
6846 }
6847 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6848 && decl_linkage (decl) == lk_none)
6849 {
6850 if (complain & tf_error)
6851 error ("%qE is not a valid template argument of type %qT "
6852 "because %qD has no linkage", orig_expr, type, decl);
6853 return NULL_TREE;
6854 }
6855 /* C++17: For a non-type template-parameter of reference or pointer
6856 type, the value of the constant expression shall not refer to (or
6857 for a pointer type, shall not be the address of):
6858 * a subobject (4.5),
6859 * a temporary object (15.2),
6860 * a string literal (5.13.5),
6861 * the result of a typeid expression (8.2.8), or
6862 * a predefined __func__ variable (11.4.1). */
6863 else if (DECL_ARTIFICIAL (decl))
6864 {
6865 if (complain & tf_error)
6866 error ("the address of %qD is not a valid template argument",
6867 decl);
6868 return NULL_TREE;
6869 }
6870 else if (!same_type_ignoring_top_level_qualifiers_p
6871 (strip_array_types (TREE_TYPE (type)),
6872 strip_array_types (TREE_TYPE (decl))))
6873 {
6874 if (complain & tf_error)
6875 error ("the address of the %qT subobject of %qD is not a "
6876 "valid template argument", TREE_TYPE (type), decl);
6877 return NULL_TREE;
6878 }
6879 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6880 {
6881 if (complain & tf_error)
6882 error ("the address of %qD is not a valid template argument "
6883 "because it does not have static storage duration",
6884 decl);
6885 return NULL_TREE;
6886 }
6887 }
6888
6889 expr = decayed;
6890
6891 expr = perform_qualification_conversions (type, expr);
6892 if (expr == error_mark_node)
6893 return error_mark_node;
6894 }
6895 /* [temp.arg.nontype]/5, bullet 3
6896
6897 For a non-type template-parameter of type reference to object, no
6898 conversions apply. The type referred to by the reference may be more
6899 cv-qualified than the (otherwise identical) type of the
6900 template-argument. The template-parameter is bound directly to the
6901 template-argument, which must be an lvalue. */
6902 else if (TYPE_REF_OBJ_P (type))
6903 {
6904 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6905 expr_type))
6906 return error_mark_node;
6907
6908 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6909 {
6910 if (complain & tf_error)
6911 error ("%qE is not a valid template argument for type %qT "
6912 "because of conflicts in cv-qualification", expr, type);
6913 return NULL_TREE;
6914 }
6915
6916 if (!lvalue_p (expr))
6917 {
6918 if (complain & tf_error)
6919 error ("%qE is not a valid template argument for type %qT "
6920 "because it is not an lvalue", expr, type);
6921 return NULL_TREE;
6922 }
6923
6924 /* [temp.arg.nontype]/1
6925
6926 A template-argument for a non-type, non-template template-parameter
6927 shall be one of: [...]
6928
6929 -- the address of an object or function with external linkage. */
6930 if (INDIRECT_REF_P (expr)
6931 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6932 {
6933 expr = TREE_OPERAND (expr, 0);
6934 if (DECL_P (expr))
6935 {
6936 if (complain & tf_error)
6937 error ("%q#D is not a valid template argument for type %qT "
6938 "because a reference variable does not have a constant "
6939 "address", expr, type);
6940 return NULL_TREE;
6941 }
6942 }
6943
6944 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6945 && value_dependent_expression_p (expr))
6946 /* OK, dependent reference. We don't want to ask whether a DECL is
6947 itself value-dependent, since what we want here is its address. */;
6948 else
6949 {
6950 if (!DECL_P (expr))
6951 {
6952 if (complain & tf_error)
6953 error ("%qE is not a valid template argument for type %qT "
6954 "because it is not an object with linkage",
6955 expr, type);
6956 return NULL_TREE;
6957 }
6958
6959 /* DR 1155 allows internal linkage in C++11 and up. */
6960 linkage_kind linkage = decl_linkage (expr);
6961 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6962 {
6963 if (complain & tf_error)
6964 error ("%qE is not a valid template argument for type %qT "
6965 "because object %qD does not have linkage",
6966 expr, type, expr);
6967 return NULL_TREE;
6968 }
6969
6970 expr = build_address (expr);
6971 }
6972
6973 if (!same_type_p (type, TREE_TYPE (expr)))
6974 expr = build_nop (type, expr);
6975 }
6976 /* [temp.arg.nontype]/5, bullet 4
6977
6978 For a non-type template-parameter of type pointer to function, only
6979 the function-to-pointer conversion (_conv.func_) is applied. If the
6980 template-argument represents a set of overloaded functions (or a
6981 pointer to such), the matching function is selected from the set
6982 (_over.over_). */
6983 else if (TYPE_PTRFN_P (type))
6984 {
6985 /* If the argument is a template-id, we might not have enough
6986 context information to decay the pointer. */
6987 if (!type_unknown_p (expr_type))
6988 {
6989 expr = decay_conversion (expr, complain);
6990 if (expr == error_mark_node)
6991 return error_mark_node;
6992 }
6993
6994 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6995 /* Null pointer values are OK in C++11. */
6996 return perform_qualification_conversions (type, expr);
6997
6998 expr = convert_nontype_argument_function (type, expr, complain);
6999 if (!expr || expr == error_mark_node)
7000 return expr;
7001 }
7002 /* [temp.arg.nontype]/5, bullet 5
7003
7004 For a non-type template-parameter of type reference to function, no
7005 conversions apply. If the template-argument represents a set of
7006 overloaded functions, the matching function is selected from the set
7007 (_over.over_). */
7008 else if (TYPE_REFFN_P (type))
7009 {
7010 if (TREE_CODE (expr) == ADDR_EXPR)
7011 {
7012 if (complain & tf_error)
7013 {
7014 error ("%qE is not a valid template argument for type %qT "
7015 "because it is a pointer", expr, type);
7016 inform (input_location, "try using %qE instead",
7017 TREE_OPERAND (expr, 0));
7018 }
7019 return NULL_TREE;
7020 }
7021
7022 expr = convert_nontype_argument_function (type, expr, complain);
7023 if (!expr || expr == error_mark_node)
7024 return expr;
7025 }
7026 /* [temp.arg.nontype]/5, bullet 6
7027
7028 For a non-type template-parameter of type pointer to member function,
7029 no conversions apply. If the template-argument represents a set of
7030 overloaded member functions, the matching member function is selected
7031 from the set (_over.over_). */
7032 else if (TYPE_PTRMEMFUNC_P (type))
7033 {
7034 expr = instantiate_type (type, expr, tf_none);
7035 if (expr == error_mark_node)
7036 return error_mark_node;
7037
7038 /* [temp.arg.nontype] bullet 1 says the pointer to member
7039 expression must be a pointer-to-member constant. */
7040 if (!value_dependent_expression_p (expr)
7041 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7042 return NULL_TREE;
7043
7044 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7045 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7046 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7047 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7048 }
7049 /* [temp.arg.nontype]/5, bullet 7
7050
7051 For a non-type template-parameter of type pointer to data member,
7052 qualification conversions (_conv.qual_) are applied. */
7053 else if (TYPE_PTRDATAMEM_P (type))
7054 {
7055 /* [temp.arg.nontype] bullet 1 says the pointer to member
7056 expression must be a pointer-to-member constant. */
7057 if (!value_dependent_expression_p (expr)
7058 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7059 return NULL_TREE;
7060
7061 expr = perform_qualification_conversions (type, expr);
7062 if (expr == error_mark_node)
7063 return expr;
7064 }
7065 else if (NULLPTR_TYPE_P (type))
7066 {
7067 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7068 {
7069 if (complain & tf_error)
7070 error ("%qE is not a valid template argument for type %qT "
7071 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7072 return NULL_TREE;
7073 }
7074 return expr;
7075 }
7076 /* A template non-type parameter must be one of the above. */
7077 else
7078 gcc_unreachable ();
7079
7080 /* Sanity check: did we actually convert the argument to the
7081 right type? */
7082 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7083 (type, TREE_TYPE (expr)));
7084 return convert_from_reference (expr);
7085 }
7086
7087 /* Subroutine of coerce_template_template_parms, which returns 1 if
7088 PARM_PARM and ARG_PARM match using the rule for the template
7089 parameters of template template parameters. Both PARM and ARG are
7090 template parameters; the rest of the arguments are the same as for
7091 coerce_template_template_parms.
7092 */
7093 static int
7094 coerce_template_template_parm (tree parm,
7095 tree arg,
7096 tsubst_flags_t complain,
7097 tree in_decl,
7098 tree outer_args)
7099 {
7100 if (arg == NULL_TREE || error_operand_p (arg)
7101 || parm == NULL_TREE || error_operand_p (parm))
7102 return 0;
7103
7104 if (TREE_CODE (arg) != TREE_CODE (parm))
7105 return 0;
7106
7107 switch (TREE_CODE (parm))
7108 {
7109 case TEMPLATE_DECL:
7110 /* We encounter instantiations of templates like
7111 template <template <template <class> class> class TT>
7112 class C; */
7113 {
7114 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7115 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7116
7117 if (!coerce_template_template_parms
7118 (parmparm, argparm, complain, in_decl, outer_args))
7119 return 0;
7120 }
7121 /* Fall through. */
7122
7123 case TYPE_DECL:
7124 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7125 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7126 /* Argument is a parameter pack but parameter is not. */
7127 return 0;
7128 break;
7129
7130 case PARM_DECL:
7131 /* The tsubst call is used to handle cases such as
7132
7133 template <int> class C {};
7134 template <class T, template <T> class TT> class D {};
7135 D<int, C> d;
7136
7137 i.e. the parameter list of TT depends on earlier parameters. */
7138 if (!uses_template_parms (TREE_TYPE (arg)))
7139 {
7140 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7141 if (!uses_template_parms (t)
7142 && !same_type_p (t, TREE_TYPE (arg)))
7143 return 0;
7144 }
7145
7146 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7147 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7148 /* Argument is a parameter pack but parameter is not. */
7149 return 0;
7150
7151 break;
7152
7153 default:
7154 gcc_unreachable ();
7155 }
7156
7157 return 1;
7158 }
7159
7160 /* Coerce template argument list ARGLIST for use with template
7161 template-parameter TEMPL. */
7162
7163 static tree
7164 coerce_template_args_for_ttp (tree templ, tree arglist,
7165 tsubst_flags_t complain)
7166 {
7167 /* Consider an example where a template template parameter declared as
7168
7169 template <class T, class U = std::allocator<T> > class TT
7170
7171 The template parameter level of T and U are one level larger than
7172 of TT. To proper process the default argument of U, say when an
7173 instantiation `TT<int>' is seen, we need to build the full
7174 arguments containing {int} as the innermost level. Outer levels,
7175 available when not appearing as default template argument, can be
7176 obtained from the arguments of the enclosing template.
7177
7178 Suppose that TT is later substituted with std::vector. The above
7179 instantiation is `TT<int, std::allocator<T> >' with TT at
7180 level 1, and T at level 2, while the template arguments at level 1
7181 becomes {std::vector} and the inner level 2 is {int}. */
7182
7183 tree outer = DECL_CONTEXT (templ);
7184 if (outer)
7185 {
7186 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7187 /* We want arguments for the partial specialization, not arguments for
7188 the primary template. */
7189 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7190 else
7191 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7192 }
7193 else if (current_template_parms)
7194 {
7195 /* This is an argument of the current template, so we haven't set
7196 DECL_CONTEXT yet. */
7197 tree relevant_template_parms;
7198
7199 /* Parameter levels that are greater than the level of the given
7200 template template parm are irrelevant. */
7201 relevant_template_parms = current_template_parms;
7202 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7203 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7204 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7205
7206 outer = template_parms_to_args (relevant_template_parms);
7207 }
7208
7209 if (outer)
7210 arglist = add_to_template_args (outer, arglist);
7211
7212 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7213 return coerce_template_parms (parmlist, arglist, templ,
7214 complain,
7215 /*require_all_args=*/true,
7216 /*use_default_args=*/true);
7217 }
7218
7219 /* A cache of template template parameters with match-all default
7220 arguments. */
7221 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7222 static void
7223 store_defaulted_ttp (tree v, tree t)
7224 {
7225 if (!defaulted_ttp_cache)
7226 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7227 defaulted_ttp_cache->put (v, t);
7228 }
7229 static tree
7230 lookup_defaulted_ttp (tree v)
7231 {
7232 if (defaulted_ttp_cache)
7233 if (tree *p = defaulted_ttp_cache->get (v))
7234 return *p;
7235 return NULL_TREE;
7236 }
7237
7238 /* T is a bound template template-parameter. Copy its arguments into default
7239 arguments of the template template-parameter's template parameters. */
7240
7241 static tree
7242 add_defaults_to_ttp (tree otmpl)
7243 {
7244 if (tree c = lookup_defaulted_ttp (otmpl))
7245 return c;
7246
7247 tree ntmpl = copy_node (otmpl);
7248
7249 tree ntype = copy_node (TREE_TYPE (otmpl));
7250 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7251 TYPE_MAIN_VARIANT (ntype) = ntype;
7252 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7253 TYPE_NAME (ntype) = ntmpl;
7254 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7255
7256 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7257 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7258 TEMPLATE_PARM_DECL (idx) = ntmpl;
7259 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7260
7261 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7262 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7263 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7264 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7265 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7266 {
7267 tree o = TREE_VEC_ELT (vec, i);
7268 if (!template_parameter_pack_p (TREE_VALUE (o)))
7269 {
7270 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7271 TREE_PURPOSE (n) = any_targ_node;
7272 }
7273 }
7274
7275 store_defaulted_ttp (otmpl, ntmpl);
7276 return ntmpl;
7277 }
7278
7279 /* ARG is a bound potential template template-argument, and PARGS is a list
7280 of arguments for the corresponding template template-parameter. Adjust
7281 PARGS as appropriate for application to ARG's template, and if ARG is a
7282 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7283 arguments to the template template parameter. */
7284
7285 static tree
7286 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7287 {
7288 ++processing_template_decl;
7289 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7290 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7291 {
7292 /* When comparing two template template-parameters in partial ordering,
7293 rewrite the one currently being used as an argument to have default
7294 arguments for all parameters. */
7295 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7296 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7297 if (pargs != error_mark_node)
7298 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7299 TYPE_TI_ARGS (arg));
7300 }
7301 else
7302 {
7303 tree aparms
7304 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7305 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7306 /*require_all*/true,
7307 /*use_default*/true);
7308 }
7309 --processing_template_decl;
7310 return pargs;
7311 }
7312
7313 /* Subroutine of unify for the case when PARM is a
7314 BOUND_TEMPLATE_TEMPLATE_PARM. */
7315
7316 static int
7317 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7318 bool explain_p)
7319 {
7320 tree parmvec = TYPE_TI_ARGS (parm);
7321 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7322
7323 /* The template template parm might be variadic and the argument
7324 not, so flatten both argument lists. */
7325 parmvec = expand_template_argument_pack (parmvec);
7326 argvec = expand_template_argument_pack (argvec);
7327
7328 if (flag_new_ttp)
7329 {
7330 /* In keeping with P0522R0, adjust P's template arguments
7331 to apply to A's template; then flatten it again. */
7332 tree nparmvec = parmvec;
7333 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7334 nparmvec = expand_template_argument_pack (nparmvec);
7335
7336 if (unify (tparms, targs, nparmvec, argvec,
7337 UNIFY_ALLOW_NONE, explain_p))
7338 return 1;
7339
7340 /* If the P0522 adjustment eliminated a pack expansion, deduce
7341 empty packs. */
7342 if (flag_new_ttp
7343 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7344 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7345 DEDUCE_EXACT, /*sub*/true, explain_p))
7346 return 1;
7347 }
7348 else
7349 {
7350 /* Deduce arguments T, i from TT<T> or TT<i>.
7351 We check each element of PARMVEC and ARGVEC individually
7352 rather than the whole TREE_VEC since they can have
7353 different number of elements, which is allowed under N2555. */
7354
7355 int len = TREE_VEC_LENGTH (parmvec);
7356
7357 /* Check if the parameters end in a pack, making them
7358 variadic. */
7359 int parm_variadic_p = 0;
7360 if (len > 0
7361 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7362 parm_variadic_p = 1;
7363
7364 for (int i = 0; i < len - parm_variadic_p; ++i)
7365 /* If the template argument list of P contains a pack
7366 expansion that is not the last template argument, the
7367 entire template argument list is a non-deduced
7368 context. */
7369 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7370 return unify_success (explain_p);
7371
7372 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7373 return unify_too_few_arguments (explain_p,
7374 TREE_VEC_LENGTH (argvec), len);
7375
7376 for (int i = 0; i < len - parm_variadic_p; ++i)
7377 if (unify (tparms, targs,
7378 TREE_VEC_ELT (parmvec, i),
7379 TREE_VEC_ELT (argvec, i),
7380 UNIFY_ALLOW_NONE, explain_p))
7381 return 1;
7382
7383 if (parm_variadic_p
7384 && unify_pack_expansion (tparms, targs,
7385 parmvec, argvec,
7386 DEDUCE_EXACT,
7387 /*subr=*/true, explain_p))
7388 return 1;
7389 }
7390
7391 return 0;
7392 }
7393
7394 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7395 template template parameters. Both PARM_PARMS and ARG_PARMS are
7396 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7397 or PARM_DECL.
7398
7399 Consider the example:
7400 template <class T> class A;
7401 template<template <class U> class TT> class B;
7402
7403 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7404 the parameters to A, and OUTER_ARGS contains A. */
7405
7406 static int
7407 coerce_template_template_parms (tree parm_parms,
7408 tree arg_parms,
7409 tsubst_flags_t complain,
7410 tree in_decl,
7411 tree outer_args)
7412 {
7413 int nparms, nargs, i;
7414 tree parm, arg;
7415 int variadic_p = 0;
7416
7417 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7418 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7419
7420 nparms = TREE_VEC_LENGTH (parm_parms);
7421 nargs = TREE_VEC_LENGTH (arg_parms);
7422
7423 if (flag_new_ttp)
7424 {
7425 /* P0522R0: A template template-parameter P is at least as specialized as
7426 a template template-argument A if, given the following rewrite to two
7427 function templates, the function template corresponding to P is at
7428 least as specialized as the function template corresponding to A
7429 according to the partial ordering rules for function templates
7430 ([temp.func.order]). Given an invented class template X with the
7431 template parameter list of A (including default arguments):
7432
7433 * Each of the two function templates has the same template parameters,
7434 respectively, as P or A.
7435
7436 * Each function template has a single function parameter whose type is
7437 a specialization of X with template arguments corresponding to the
7438 template parameters from the respective function template where, for
7439 each template parameter PP in the template parameter list of the
7440 function template, a corresponding template argument AA is formed. If
7441 PP declares a parameter pack, then AA is the pack expansion
7442 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7443
7444 If the rewrite produces an invalid type, then P is not at least as
7445 specialized as A. */
7446
7447 /* So coerce P's args to apply to A's parms, and then deduce between A's
7448 args and the converted args. If that succeeds, A is at least as
7449 specialized as P, so they match.*/
7450 tree pargs = template_parms_level_to_args (parm_parms);
7451 ++processing_template_decl;
7452 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7453 /*require_all*/true, /*use_default*/true);
7454 --processing_template_decl;
7455 if (pargs != error_mark_node)
7456 {
7457 tree targs = make_tree_vec (nargs);
7458 tree aargs = template_parms_level_to_args (arg_parms);
7459 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7460 /*explain*/false))
7461 return 1;
7462 }
7463 }
7464
7465 /* Determine whether we have a parameter pack at the end of the
7466 template template parameter's template parameter list. */
7467 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7468 {
7469 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7470
7471 if (error_operand_p (parm))
7472 return 0;
7473
7474 switch (TREE_CODE (parm))
7475 {
7476 case TEMPLATE_DECL:
7477 case TYPE_DECL:
7478 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7479 variadic_p = 1;
7480 break;
7481
7482 case PARM_DECL:
7483 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7484 variadic_p = 1;
7485 break;
7486
7487 default:
7488 gcc_unreachable ();
7489 }
7490 }
7491
7492 if (nargs != nparms
7493 && !(variadic_p && nargs >= nparms - 1))
7494 return 0;
7495
7496 /* Check all of the template parameters except the parameter pack at
7497 the end (if any). */
7498 for (i = 0; i < nparms - variadic_p; ++i)
7499 {
7500 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7501 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7502 continue;
7503
7504 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7505 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7506
7507 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7508 outer_args))
7509 return 0;
7510
7511 }
7512
7513 if (variadic_p)
7514 {
7515 /* Check each of the template parameters in the template
7516 argument against the template parameter pack at the end of
7517 the template template parameter. */
7518 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7519 return 0;
7520
7521 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7522
7523 for (; i < nargs; ++i)
7524 {
7525 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7526 continue;
7527
7528 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7529
7530 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7531 outer_args))
7532 return 0;
7533 }
7534 }
7535
7536 return 1;
7537 }
7538
7539 /* Verifies that the deduced template arguments (in TARGS) for the
7540 template template parameters (in TPARMS) represent valid bindings,
7541 by comparing the template parameter list of each template argument
7542 to the template parameter list of its corresponding template
7543 template parameter, in accordance with DR150. This
7544 routine can only be called after all template arguments have been
7545 deduced. It will return TRUE if all of the template template
7546 parameter bindings are okay, FALSE otherwise. */
7547 bool
7548 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7549 {
7550 int i, ntparms = TREE_VEC_LENGTH (tparms);
7551 bool ret = true;
7552
7553 /* We're dealing with template parms in this process. */
7554 ++processing_template_decl;
7555
7556 targs = INNERMOST_TEMPLATE_ARGS (targs);
7557
7558 for (i = 0; i < ntparms; ++i)
7559 {
7560 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7561 tree targ = TREE_VEC_ELT (targs, i);
7562
7563 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7564 {
7565 tree packed_args = NULL_TREE;
7566 int idx, len = 1;
7567
7568 if (ARGUMENT_PACK_P (targ))
7569 {
7570 /* Look inside the argument pack. */
7571 packed_args = ARGUMENT_PACK_ARGS (targ);
7572 len = TREE_VEC_LENGTH (packed_args);
7573 }
7574
7575 for (idx = 0; idx < len; ++idx)
7576 {
7577 tree targ_parms = NULL_TREE;
7578
7579 if (packed_args)
7580 /* Extract the next argument from the argument
7581 pack. */
7582 targ = TREE_VEC_ELT (packed_args, idx);
7583
7584 if (PACK_EXPANSION_P (targ))
7585 /* Look at the pattern of the pack expansion. */
7586 targ = PACK_EXPANSION_PATTERN (targ);
7587
7588 /* Extract the template parameters from the template
7589 argument. */
7590 if (TREE_CODE (targ) == TEMPLATE_DECL)
7591 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7592 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7593 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7594
7595 /* Verify that we can coerce the template template
7596 parameters from the template argument to the template
7597 parameter. This requires an exact match. */
7598 if (targ_parms
7599 && !coerce_template_template_parms
7600 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7601 targ_parms,
7602 tf_none,
7603 tparm,
7604 targs))
7605 {
7606 ret = false;
7607 goto out;
7608 }
7609 }
7610 }
7611 }
7612
7613 out:
7614
7615 --processing_template_decl;
7616 return ret;
7617 }
7618
7619 /* Since type attributes aren't mangled, we need to strip them from
7620 template type arguments. */
7621
7622 static tree
7623 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7624 {
7625 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7626 return arg;
7627 bool removed_attributes = false;
7628 tree canon = strip_typedefs (arg, &removed_attributes);
7629 if (removed_attributes
7630 && (complain & tf_warning))
7631 warning (OPT_Wignored_attributes,
7632 "ignoring attributes on template argument %qT", arg);
7633 return canon;
7634 }
7635
7636 /* And from inside dependent non-type arguments like sizeof(Type). */
7637
7638 static tree
7639 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7640 {
7641 if (!arg || arg == error_mark_node)
7642 return arg;
7643 bool removed_attributes = false;
7644 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7645 if (removed_attributes
7646 && (complain & tf_warning))
7647 warning (OPT_Wignored_attributes,
7648 "ignoring attributes in template argument %qE", arg);
7649 return canon;
7650 }
7651
7652 // A template declaration can be substituted for a constrained
7653 // template template parameter only when the argument is more
7654 // constrained than the parameter.
7655 static bool
7656 is_compatible_template_arg (tree parm, tree arg)
7657 {
7658 tree parm_cons = get_constraints (parm);
7659
7660 /* For now, allow constrained template template arguments
7661 and unconstrained template template parameters. */
7662 if (parm_cons == NULL_TREE)
7663 return true;
7664
7665 tree arg_cons = get_constraints (arg);
7666
7667 // If the template parameter is constrained, we need to rewrite its
7668 // constraints in terms of the ARG's template parameters. This ensures
7669 // that all of the template parameter types will have the same depth.
7670 //
7671 // Note that this is only valid when coerce_template_template_parm is
7672 // true for the innermost template parameters of PARM and ARG. In other
7673 // words, because coercion is successful, this conversion will be valid.
7674 if (parm_cons)
7675 {
7676 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7677 parm_cons = tsubst_constraint_info (parm_cons,
7678 INNERMOST_TEMPLATE_ARGS (args),
7679 tf_none, NULL_TREE);
7680 if (parm_cons == error_mark_node)
7681 return false;
7682 }
7683
7684 return subsumes (parm_cons, arg_cons);
7685 }
7686
7687 // Convert a placeholder argument into a binding to the original
7688 // parameter. The original parameter is saved as the TREE_TYPE of
7689 // ARG.
7690 static inline tree
7691 convert_wildcard_argument (tree parm, tree arg)
7692 {
7693 TREE_TYPE (arg) = parm;
7694 return arg;
7695 }
7696
7697 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7698 because one of them is dependent. But we need to represent the
7699 conversion for the benefit of cp_tree_equal. */
7700
7701 static tree
7702 maybe_convert_nontype_argument (tree type, tree arg)
7703 {
7704 /* Auto parms get no conversion. */
7705 if (type_uses_auto (type))
7706 return arg;
7707 /* We don't need or want to add this conversion now if we're going to use the
7708 argument for deduction. */
7709 if (value_dependent_expression_p (arg))
7710 return arg;
7711
7712 type = cv_unqualified (type);
7713 tree argtype = TREE_TYPE (arg);
7714 if (same_type_p (type, argtype))
7715 return arg;
7716
7717 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7718 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7719 return arg;
7720 }
7721
7722 /* Convert the indicated template ARG as necessary to match the
7723 indicated template PARM. Returns the converted ARG, or
7724 error_mark_node if the conversion was unsuccessful. Error and
7725 warning messages are issued under control of COMPLAIN. This
7726 conversion is for the Ith parameter in the parameter list. ARGS is
7727 the full set of template arguments deduced so far. */
7728
7729 static tree
7730 convert_template_argument (tree parm,
7731 tree arg,
7732 tree args,
7733 tsubst_flags_t complain,
7734 int i,
7735 tree in_decl)
7736 {
7737 tree orig_arg;
7738 tree val;
7739 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7740
7741 if (parm == error_mark_node)
7742 return error_mark_node;
7743
7744 /* Trivially convert placeholders. */
7745 if (TREE_CODE (arg) == WILDCARD_DECL)
7746 return convert_wildcard_argument (parm, arg);
7747
7748 if (arg == any_targ_node)
7749 return arg;
7750
7751 if (TREE_CODE (arg) == TREE_LIST
7752 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7753 {
7754 /* The template argument was the name of some
7755 member function. That's usually
7756 invalid, but static members are OK. In any
7757 case, grab the underlying fields/functions
7758 and issue an error later if required. */
7759 orig_arg = TREE_VALUE (arg);
7760 TREE_TYPE (arg) = unknown_type_node;
7761 }
7762
7763 orig_arg = arg;
7764
7765 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7766 requires_type = (TREE_CODE (parm) == TYPE_DECL
7767 || requires_tmpl_type);
7768
7769 /* When determining whether an argument pack expansion is a template,
7770 look at the pattern. */
7771 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7772 arg = PACK_EXPANSION_PATTERN (arg);
7773
7774 /* Deal with an injected-class-name used as a template template arg. */
7775 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7776 {
7777 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7778 if (TREE_CODE (t) == TEMPLATE_DECL)
7779 {
7780 if (cxx_dialect >= cxx11)
7781 /* OK under DR 1004. */;
7782 else if (complain & tf_warning_or_error)
7783 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7784 " used as template template argument", TYPE_NAME (arg));
7785 else if (flag_pedantic_errors)
7786 t = arg;
7787
7788 arg = t;
7789 }
7790 }
7791
7792 is_tmpl_type =
7793 ((TREE_CODE (arg) == TEMPLATE_DECL
7794 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7795 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7796 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7797 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7798
7799 if (is_tmpl_type
7800 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7801 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7802 arg = TYPE_STUB_DECL (arg);
7803
7804 is_type = TYPE_P (arg) || is_tmpl_type;
7805
7806 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7807 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7808 {
7809 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7810 {
7811 if (complain & tf_error)
7812 error ("invalid use of destructor %qE as a type", orig_arg);
7813 return error_mark_node;
7814 }
7815
7816 permerror (input_location,
7817 "to refer to a type member of a template parameter, "
7818 "use %<typename %E%>", orig_arg);
7819
7820 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7821 TREE_OPERAND (arg, 1),
7822 typename_type,
7823 complain);
7824 arg = orig_arg;
7825 is_type = 1;
7826 }
7827 if (is_type != requires_type)
7828 {
7829 if (in_decl)
7830 {
7831 if (complain & tf_error)
7832 {
7833 error ("type/value mismatch at argument %d in template "
7834 "parameter list for %qD",
7835 i + 1, in_decl);
7836 if (is_type)
7837 inform (input_location,
7838 " expected a constant of type %qT, got %qT",
7839 TREE_TYPE (parm),
7840 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7841 else if (requires_tmpl_type)
7842 inform (input_location,
7843 " expected a class template, got %qE", orig_arg);
7844 else
7845 inform (input_location,
7846 " expected a type, got %qE", orig_arg);
7847 }
7848 }
7849 return error_mark_node;
7850 }
7851 if (is_tmpl_type ^ requires_tmpl_type)
7852 {
7853 if (in_decl && (complain & tf_error))
7854 {
7855 error ("type/value mismatch at argument %d in template "
7856 "parameter list for %qD",
7857 i + 1, in_decl);
7858 if (is_tmpl_type)
7859 inform (input_location,
7860 " expected a type, got %qT", DECL_NAME (arg));
7861 else
7862 inform (input_location,
7863 " expected a class template, got %qT", orig_arg);
7864 }
7865 return error_mark_node;
7866 }
7867
7868 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7869 /* We already did the appropriate conversion when packing args. */
7870 val = orig_arg;
7871 else if (is_type)
7872 {
7873 if (requires_tmpl_type)
7874 {
7875 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7876 /* The number of argument required is not known yet.
7877 Just accept it for now. */
7878 val = orig_arg;
7879 else
7880 {
7881 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7882 tree argparm;
7883
7884 /* Strip alias templates that are equivalent to another
7885 template. */
7886 arg = get_underlying_template (arg);
7887 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7888
7889 if (coerce_template_template_parms (parmparm, argparm,
7890 complain, in_decl,
7891 args))
7892 {
7893 val = arg;
7894
7895 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7896 TEMPLATE_DECL. */
7897 if (val != error_mark_node)
7898 {
7899 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7900 val = TREE_TYPE (val);
7901 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7902 val = make_pack_expansion (val, complain);
7903 }
7904 }
7905 else
7906 {
7907 if (in_decl && (complain & tf_error))
7908 {
7909 error ("type/value mismatch at argument %d in "
7910 "template parameter list for %qD",
7911 i + 1, in_decl);
7912 inform (input_location,
7913 " expected a template of type %qD, got %qT",
7914 parm, orig_arg);
7915 }
7916
7917 val = error_mark_node;
7918 }
7919
7920 // Check that the constraints are compatible before allowing the
7921 // substitution.
7922 if (val != error_mark_node)
7923 if (!is_compatible_template_arg (parm, arg))
7924 {
7925 if (in_decl && (complain & tf_error))
7926 {
7927 error ("constraint mismatch at argument %d in "
7928 "template parameter list for %qD",
7929 i + 1, in_decl);
7930 inform (input_location, " expected %qD but got %qD",
7931 parm, arg);
7932 }
7933 val = error_mark_node;
7934 }
7935 }
7936 }
7937 else
7938 val = orig_arg;
7939 /* We only form one instance of each template specialization.
7940 Therefore, if we use a non-canonical variant (i.e., a
7941 typedef), any future messages referring to the type will use
7942 the typedef, which is confusing if those future uses do not
7943 themselves also use the typedef. */
7944 if (TYPE_P (val))
7945 val = canonicalize_type_argument (val, complain);
7946 }
7947 else
7948 {
7949 tree t = TREE_TYPE (parm);
7950
7951 if (tree a = type_uses_auto (t))
7952 {
7953 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7954 if (t == error_mark_node)
7955 return error_mark_node;
7956 }
7957 else
7958 t = tsubst (t, args, complain, in_decl);
7959
7960 if (invalid_nontype_parm_type_p (t, complain))
7961 return error_mark_node;
7962
7963 if (!type_dependent_expression_p (orig_arg)
7964 && !uses_template_parms (t))
7965 /* We used to call digest_init here. However, digest_init
7966 will report errors, which we don't want when complain
7967 is zero. More importantly, digest_init will try too
7968 hard to convert things: for example, `0' should not be
7969 converted to pointer type at this point according to
7970 the standard. Accepting this is not merely an
7971 extension, since deciding whether or not these
7972 conversions can occur is part of determining which
7973 function template to call, or whether a given explicit
7974 argument specification is valid. */
7975 val = convert_nontype_argument (t, orig_arg, complain);
7976 else
7977 {
7978 val = canonicalize_expr_argument (orig_arg, complain);
7979 val = maybe_convert_nontype_argument (t, val);
7980 }
7981
7982
7983 if (val == NULL_TREE)
7984 val = error_mark_node;
7985 else if (val == error_mark_node && (complain & tf_error))
7986 error ("could not convert template argument %qE from %qT to %qT",
7987 orig_arg, TREE_TYPE (orig_arg), t);
7988
7989 if (INDIRECT_REF_P (val))
7990 {
7991 /* Reject template arguments that are references to built-in
7992 functions with no library fallbacks. */
7993 const_tree inner = TREE_OPERAND (val, 0);
7994 const_tree innertype = TREE_TYPE (inner);
7995 if (innertype
7996 && TREE_CODE (innertype) == REFERENCE_TYPE
7997 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7998 && TREE_OPERAND_LENGTH (inner) > 0
7999 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8000 return error_mark_node;
8001 }
8002
8003 if (TREE_CODE (val) == SCOPE_REF)
8004 {
8005 /* Strip typedefs from the SCOPE_REF. */
8006 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8007 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8008 complain);
8009 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8010 QUALIFIED_NAME_IS_TEMPLATE (val));
8011 }
8012 }
8013
8014 return val;
8015 }
8016
8017 /* Coerces the remaining template arguments in INNER_ARGS (from
8018 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8019 Returns the coerced argument pack. PARM_IDX is the position of this
8020 parameter in the template parameter list. ARGS is the original
8021 template argument list. */
8022 static tree
8023 coerce_template_parameter_pack (tree parms,
8024 int parm_idx,
8025 tree args,
8026 tree inner_args,
8027 int arg_idx,
8028 tree new_args,
8029 int* lost,
8030 tree in_decl,
8031 tsubst_flags_t complain)
8032 {
8033 tree parm = TREE_VEC_ELT (parms, parm_idx);
8034 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8035 tree packed_args;
8036 tree argument_pack;
8037 tree packed_parms = NULL_TREE;
8038
8039 if (arg_idx > nargs)
8040 arg_idx = nargs;
8041
8042 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8043 {
8044 /* When the template parameter is a non-type template parameter pack
8045 or template template parameter pack whose type or template
8046 parameters use parameter packs, we know exactly how many arguments
8047 we are looking for. Build a vector of the instantiated decls for
8048 these template parameters in PACKED_PARMS. */
8049 /* We can't use make_pack_expansion here because it would interpret a
8050 _DECL as a use rather than a declaration. */
8051 tree decl = TREE_VALUE (parm);
8052 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8053 SET_PACK_EXPANSION_PATTERN (exp, decl);
8054 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8055 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8056
8057 TREE_VEC_LENGTH (args)--;
8058 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8059 TREE_VEC_LENGTH (args)++;
8060
8061 if (packed_parms == error_mark_node)
8062 return error_mark_node;
8063
8064 /* If we're doing a partial instantiation of a member template,
8065 verify that all of the types used for the non-type
8066 template parameter pack are, in fact, valid for non-type
8067 template parameters. */
8068 if (arg_idx < nargs
8069 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8070 {
8071 int j, len = TREE_VEC_LENGTH (packed_parms);
8072 for (j = 0; j < len; ++j)
8073 {
8074 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8075 if (invalid_nontype_parm_type_p (t, complain))
8076 return error_mark_node;
8077 }
8078 /* We don't know how many args we have yet, just
8079 use the unconverted ones for now. */
8080 return NULL_TREE;
8081 }
8082
8083 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8084 }
8085 /* Check if we have a placeholder pack, which indicates we're
8086 in the context of a introduction list. In that case we want
8087 to match this pack to the single placeholder. */
8088 else if (arg_idx < nargs
8089 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8090 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8091 {
8092 nargs = arg_idx + 1;
8093 packed_args = make_tree_vec (1);
8094 }
8095 else
8096 packed_args = make_tree_vec (nargs - arg_idx);
8097
8098 /* Convert the remaining arguments, which will be a part of the
8099 parameter pack "parm". */
8100 int first_pack_arg = arg_idx;
8101 for (; arg_idx < nargs; ++arg_idx)
8102 {
8103 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8104 tree actual_parm = TREE_VALUE (parm);
8105 int pack_idx = arg_idx - first_pack_arg;
8106
8107 if (packed_parms)
8108 {
8109 /* Once we've packed as many args as we have types, stop. */
8110 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8111 break;
8112 else if (PACK_EXPANSION_P (arg))
8113 /* We don't know how many args we have yet, just
8114 use the unconverted ones for now. */
8115 return NULL_TREE;
8116 else
8117 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8118 }
8119
8120 if (arg == error_mark_node)
8121 {
8122 if (complain & tf_error)
8123 error ("template argument %d is invalid", arg_idx + 1);
8124 }
8125 else
8126 arg = convert_template_argument (actual_parm,
8127 arg, new_args, complain, parm_idx,
8128 in_decl);
8129 if (arg == error_mark_node)
8130 (*lost)++;
8131 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8132 }
8133
8134 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8135 && TREE_VEC_LENGTH (packed_args) > 0)
8136 {
8137 if (complain & tf_error)
8138 error ("wrong number of template arguments (%d, should be %d)",
8139 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8140 return error_mark_node;
8141 }
8142
8143 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8144 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8145 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8146 else
8147 {
8148 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8149 TREE_CONSTANT (argument_pack) = 1;
8150 }
8151
8152 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8153 if (CHECKING_P)
8154 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8155 TREE_VEC_LENGTH (packed_args));
8156 return argument_pack;
8157 }
8158
8159 /* Returns the number of pack expansions in the template argument vector
8160 ARGS. */
8161
8162 static int
8163 pack_expansion_args_count (tree args)
8164 {
8165 int i;
8166 int count = 0;
8167 if (args)
8168 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8169 {
8170 tree elt = TREE_VEC_ELT (args, i);
8171 if (elt && PACK_EXPANSION_P (elt))
8172 ++count;
8173 }
8174 return count;
8175 }
8176
8177 /* Convert all template arguments to their appropriate types, and
8178 return a vector containing the innermost resulting template
8179 arguments. If any error occurs, return error_mark_node. Error and
8180 warning messages are issued under control of COMPLAIN.
8181
8182 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8183 for arguments not specified in ARGS. Otherwise, if
8184 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8185 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8186 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8187 ARGS. */
8188
8189 static tree
8190 coerce_template_parms (tree parms,
8191 tree args,
8192 tree in_decl,
8193 tsubst_flags_t complain,
8194 bool require_all_args,
8195 bool use_default_args)
8196 {
8197 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8198 tree orig_inner_args;
8199 tree inner_args;
8200 tree new_args;
8201 tree new_inner_args;
8202 int saved_unevaluated_operand;
8203 int saved_inhibit_evaluation_warnings;
8204
8205 /* When used as a boolean value, indicates whether this is a
8206 variadic template parameter list. Since it's an int, we can also
8207 subtract it from nparms to get the number of non-variadic
8208 parameters. */
8209 int variadic_p = 0;
8210 int variadic_args_p = 0;
8211 int post_variadic_parms = 0;
8212
8213 /* Likewise for parameters with default arguments. */
8214 int default_p = 0;
8215
8216 if (args == error_mark_node)
8217 return error_mark_node;
8218
8219 nparms = TREE_VEC_LENGTH (parms);
8220
8221 /* Determine if there are any parameter packs or default arguments. */
8222 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8223 {
8224 tree parm = TREE_VEC_ELT (parms, parm_idx);
8225 if (variadic_p)
8226 ++post_variadic_parms;
8227 if (template_parameter_pack_p (TREE_VALUE (parm)))
8228 ++variadic_p;
8229 if (TREE_PURPOSE (parm))
8230 ++default_p;
8231 }
8232
8233 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8234 /* If there are no parameters that follow a parameter pack, we need to
8235 expand any argument packs so that we can deduce a parameter pack from
8236 some non-packed args followed by an argument pack, as in variadic85.C.
8237 If there are such parameters, we need to leave argument packs intact
8238 so the arguments are assigned properly. This can happen when dealing
8239 with a nested class inside a partial specialization of a class
8240 template, as in variadic92.C, or when deducing a template parameter pack
8241 from a sub-declarator, as in variadic114.C. */
8242 if (!post_variadic_parms)
8243 inner_args = expand_template_argument_pack (inner_args);
8244
8245 /* Count any pack expansion args. */
8246 variadic_args_p = pack_expansion_args_count (inner_args);
8247
8248 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8249 if ((nargs - variadic_args_p > nparms && !variadic_p)
8250 || (nargs < nparms - variadic_p
8251 && require_all_args
8252 && !variadic_args_p
8253 && (!use_default_args
8254 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8255 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8256 {
8257 if (complain & tf_error)
8258 {
8259 if (variadic_p || default_p)
8260 {
8261 nparms -= variadic_p + default_p;
8262 error ("wrong number of template arguments "
8263 "(%d, should be at least %d)", nargs, nparms);
8264 }
8265 else
8266 error ("wrong number of template arguments "
8267 "(%d, should be %d)", nargs, nparms);
8268
8269 if (in_decl)
8270 inform (DECL_SOURCE_LOCATION (in_decl),
8271 "provided for %qD", in_decl);
8272 }
8273
8274 return error_mark_node;
8275 }
8276 /* We can't pass a pack expansion to a non-pack parameter of an alias
8277 template (DR 1430). */
8278 else if (in_decl
8279 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8280 || concept_template_p (in_decl))
8281 && variadic_args_p
8282 && nargs - variadic_args_p < nparms - variadic_p)
8283 {
8284 if (complain & tf_error)
8285 {
8286 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8287 {
8288 tree arg = TREE_VEC_ELT (inner_args, i);
8289 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8290
8291 if (PACK_EXPANSION_P (arg)
8292 && !template_parameter_pack_p (parm))
8293 {
8294 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8295 error_at (location_of (arg),
8296 "pack expansion argument for non-pack parameter "
8297 "%qD of alias template %qD", parm, in_decl);
8298 else
8299 error_at (location_of (arg),
8300 "pack expansion argument for non-pack parameter "
8301 "%qD of concept %qD", parm, in_decl);
8302 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8303 goto found;
8304 }
8305 }
8306 gcc_unreachable ();
8307 found:;
8308 }
8309 return error_mark_node;
8310 }
8311
8312 /* We need to evaluate the template arguments, even though this
8313 template-id may be nested within a "sizeof". */
8314 saved_unevaluated_operand = cp_unevaluated_operand;
8315 cp_unevaluated_operand = 0;
8316 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8317 c_inhibit_evaluation_warnings = 0;
8318 new_inner_args = make_tree_vec (nparms);
8319 new_args = add_outermost_template_args (args, new_inner_args);
8320 int pack_adjust = 0;
8321 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8322 {
8323 tree arg;
8324 tree parm;
8325
8326 /* Get the Ith template parameter. */
8327 parm = TREE_VEC_ELT (parms, parm_idx);
8328
8329 if (parm == error_mark_node)
8330 {
8331 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8332 continue;
8333 }
8334
8335 /* Calculate the next argument. */
8336 if (arg_idx < nargs)
8337 arg = TREE_VEC_ELT (inner_args, arg_idx);
8338 else
8339 arg = NULL_TREE;
8340
8341 if (template_parameter_pack_p (TREE_VALUE (parm))
8342 && !(arg && ARGUMENT_PACK_P (arg)))
8343 {
8344 /* Some arguments will be placed in the
8345 template parameter pack PARM. */
8346 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8347 inner_args, arg_idx,
8348 new_args, &lost,
8349 in_decl, complain);
8350
8351 if (arg == NULL_TREE)
8352 {
8353 /* We don't know how many args we have yet, just use the
8354 unconverted (and still packed) ones for now. */
8355 new_inner_args = orig_inner_args;
8356 arg_idx = nargs;
8357 break;
8358 }
8359
8360 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8361
8362 /* Store this argument. */
8363 if (arg == error_mark_node)
8364 {
8365 lost++;
8366 /* We are done with all of the arguments. */
8367 arg_idx = nargs;
8368 }
8369 else
8370 {
8371 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8372 arg_idx += pack_adjust;
8373 }
8374
8375 continue;
8376 }
8377 else if (arg)
8378 {
8379 if (PACK_EXPANSION_P (arg))
8380 {
8381 /* "If every valid specialization of a variadic template
8382 requires an empty template parameter pack, the template is
8383 ill-formed, no diagnostic required." So check that the
8384 pattern works with this parameter. */
8385 tree pattern = PACK_EXPANSION_PATTERN (arg);
8386 tree conv = convert_template_argument (TREE_VALUE (parm),
8387 pattern, new_args,
8388 complain, parm_idx,
8389 in_decl);
8390 if (conv == error_mark_node)
8391 {
8392 if (complain & tf_error)
8393 inform (input_location, "so any instantiation with a "
8394 "non-empty parameter pack would be ill-formed");
8395 ++lost;
8396 }
8397 else if (TYPE_P (conv) && !TYPE_P (pattern))
8398 /* Recover from missing typename. */
8399 TREE_VEC_ELT (inner_args, arg_idx)
8400 = make_pack_expansion (conv, complain);
8401
8402 /* We don't know how many args we have yet, just
8403 use the unconverted ones for now. */
8404 new_inner_args = inner_args;
8405 arg_idx = nargs;
8406 break;
8407 }
8408 }
8409 else if (require_all_args)
8410 {
8411 /* There must be a default arg in this case. */
8412 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8413 complain, in_decl);
8414 /* The position of the first default template argument,
8415 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8416 Record that. */
8417 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8418 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8419 arg_idx - pack_adjust);
8420 }
8421 else
8422 break;
8423
8424 if (arg == error_mark_node)
8425 {
8426 if (complain & tf_error)
8427 error ("template argument %d is invalid", arg_idx + 1);
8428 }
8429 else if (!arg)
8430 /* This only occurs if there was an error in the template
8431 parameter list itself (which we would already have
8432 reported) that we are trying to recover from, e.g., a class
8433 template with a parameter list such as
8434 template<typename..., typename>. */
8435 ++lost;
8436 else
8437 arg = convert_template_argument (TREE_VALUE (parm),
8438 arg, new_args, complain,
8439 parm_idx, in_decl);
8440
8441 if (arg == error_mark_node)
8442 lost++;
8443 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8444 }
8445 cp_unevaluated_operand = saved_unevaluated_operand;
8446 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8447
8448 if (variadic_p && arg_idx < nargs)
8449 {
8450 if (complain & tf_error)
8451 {
8452 error ("wrong number of template arguments "
8453 "(%d, should be %d)", nargs, arg_idx);
8454 if (in_decl)
8455 error ("provided for %q+D", in_decl);
8456 }
8457 return error_mark_node;
8458 }
8459
8460 if (lost)
8461 return error_mark_node;
8462
8463 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8464 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8465 TREE_VEC_LENGTH (new_inner_args));
8466
8467 return new_inner_args;
8468 }
8469
8470 /* Convert all template arguments to their appropriate types, and
8471 return a vector containing the innermost resulting template
8472 arguments. If any error occurs, return error_mark_node. Error and
8473 warning messages are not issued.
8474
8475 Note that no function argument deduction is performed, and default
8476 arguments are used to fill in unspecified arguments. */
8477 tree
8478 coerce_template_parms (tree parms, tree args, tree in_decl)
8479 {
8480 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8481 }
8482
8483 /* Convert all template arguments to their appropriate type, and
8484 instantiate default arguments as needed. This returns a vector
8485 containing the innermost resulting template arguments, or
8486 error_mark_node if unsuccessful. */
8487 tree
8488 coerce_template_parms (tree parms, tree args, tree in_decl,
8489 tsubst_flags_t complain)
8490 {
8491 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8492 }
8493
8494 /* Like coerce_template_parms. If PARMS represents all template
8495 parameters levels, this function returns a vector of vectors
8496 representing all the resulting argument levels. Note that in this
8497 case, only the innermost arguments are coerced because the
8498 outermost ones are supposed to have been coerced already.
8499
8500 Otherwise, if PARMS represents only (the innermost) vector of
8501 parameters, this function returns a vector containing just the
8502 innermost resulting arguments. */
8503
8504 static tree
8505 coerce_innermost_template_parms (tree parms,
8506 tree args,
8507 tree in_decl,
8508 tsubst_flags_t complain,
8509 bool require_all_args,
8510 bool use_default_args)
8511 {
8512 int parms_depth = TMPL_PARMS_DEPTH (parms);
8513 int args_depth = TMPL_ARGS_DEPTH (args);
8514 tree coerced_args;
8515
8516 if (parms_depth > 1)
8517 {
8518 coerced_args = make_tree_vec (parms_depth);
8519 tree level;
8520 int cur_depth;
8521
8522 for (level = parms, cur_depth = parms_depth;
8523 parms_depth > 0 && level != NULL_TREE;
8524 level = TREE_CHAIN (level), --cur_depth)
8525 {
8526 tree l;
8527 if (cur_depth == args_depth)
8528 l = coerce_template_parms (TREE_VALUE (level),
8529 args, in_decl, complain,
8530 require_all_args,
8531 use_default_args);
8532 else
8533 l = TMPL_ARGS_LEVEL (args, cur_depth);
8534
8535 if (l == error_mark_node)
8536 return error_mark_node;
8537
8538 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8539 }
8540 }
8541 else
8542 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8543 args, in_decl, complain,
8544 require_all_args,
8545 use_default_args);
8546 return coerced_args;
8547 }
8548
8549 /* Returns 1 if template args OT and NT are equivalent. */
8550
8551 int
8552 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8553 {
8554 if (nt == ot)
8555 return 1;
8556 if (nt == NULL_TREE || ot == NULL_TREE)
8557 return false;
8558 if (nt == any_targ_node || ot == any_targ_node)
8559 return true;
8560
8561 if (TREE_CODE (nt) == TREE_VEC)
8562 /* For member templates */
8563 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8564 else if (PACK_EXPANSION_P (ot))
8565 return (PACK_EXPANSION_P (nt)
8566 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8567 PACK_EXPANSION_PATTERN (nt))
8568 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8569 PACK_EXPANSION_EXTRA_ARGS (nt)));
8570 else if (ARGUMENT_PACK_P (ot))
8571 {
8572 int i, len;
8573 tree opack, npack;
8574
8575 if (!ARGUMENT_PACK_P (nt))
8576 return 0;
8577
8578 opack = ARGUMENT_PACK_ARGS (ot);
8579 npack = ARGUMENT_PACK_ARGS (nt);
8580 len = TREE_VEC_LENGTH (opack);
8581 if (TREE_VEC_LENGTH (npack) != len)
8582 return 0;
8583 for (i = 0; i < len; ++i)
8584 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8585 TREE_VEC_ELT (npack, i)))
8586 return 0;
8587 return 1;
8588 }
8589 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8590 gcc_unreachable ();
8591 else if (TYPE_P (nt))
8592 {
8593 if (!TYPE_P (ot))
8594 return false;
8595 /* Don't treat an alias template specialization with dependent
8596 arguments as equivalent to its underlying type when used as a
8597 template argument; we need them to be distinct so that we
8598 substitute into the specialization arguments at instantiation
8599 time. And aliases can't be equivalent without being ==, so
8600 we don't need to look any deeper.
8601
8602 During partial ordering, however, we need to treat them normally so
8603 that we can order uses of the same alias with different
8604 cv-qualification (79960). */
8605 if (!partial_order
8606 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8607 return false;
8608 else
8609 return same_type_p (ot, nt);
8610 }
8611 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8612 return 0;
8613 else
8614 {
8615 /* Try to treat a template non-type argument that has been converted
8616 to the parameter type as equivalent to one that hasn't yet. */
8617 for (enum tree_code code1 = TREE_CODE (ot);
8618 CONVERT_EXPR_CODE_P (code1)
8619 || code1 == NON_LVALUE_EXPR;
8620 code1 = TREE_CODE (ot))
8621 ot = TREE_OPERAND (ot, 0);
8622 for (enum tree_code code2 = TREE_CODE (nt);
8623 CONVERT_EXPR_CODE_P (code2)
8624 || code2 == NON_LVALUE_EXPR;
8625 code2 = TREE_CODE (nt))
8626 nt = TREE_OPERAND (nt, 0);
8627
8628 return cp_tree_equal (ot, nt);
8629 }
8630 }
8631
8632 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8633 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8634 NEWARG_PTR with the offending arguments if they are non-NULL. */
8635
8636 int
8637 comp_template_args (tree oldargs, tree newargs,
8638 tree *oldarg_ptr, tree *newarg_ptr,
8639 bool partial_order)
8640 {
8641 int i;
8642
8643 if (oldargs == newargs)
8644 return 1;
8645
8646 if (!oldargs || !newargs)
8647 return 0;
8648
8649 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8650 return 0;
8651
8652 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8653 {
8654 tree nt = TREE_VEC_ELT (newargs, i);
8655 tree ot = TREE_VEC_ELT (oldargs, i);
8656
8657 if (! template_args_equal (ot, nt, partial_order))
8658 {
8659 if (oldarg_ptr != NULL)
8660 *oldarg_ptr = ot;
8661 if (newarg_ptr != NULL)
8662 *newarg_ptr = nt;
8663 return 0;
8664 }
8665 }
8666 return 1;
8667 }
8668
8669 inline bool
8670 comp_template_args_porder (tree oargs, tree nargs)
8671 {
8672 return comp_template_args (oargs, nargs, NULL, NULL, true);
8673 }
8674
8675 static void
8676 add_pending_template (tree d)
8677 {
8678 tree ti = (TYPE_P (d)
8679 ? CLASSTYPE_TEMPLATE_INFO (d)
8680 : DECL_TEMPLATE_INFO (d));
8681 struct pending_template *pt;
8682 int level;
8683
8684 if (TI_PENDING_TEMPLATE_FLAG (ti))
8685 return;
8686
8687 /* We are called both from instantiate_decl, where we've already had a
8688 tinst_level pushed, and instantiate_template, where we haven't.
8689 Compensate. */
8690 level = !current_tinst_level || current_tinst_level->decl != d;
8691
8692 if (level)
8693 push_tinst_level (d);
8694
8695 pt = ggc_alloc<pending_template> ();
8696 pt->next = NULL;
8697 pt->tinst = current_tinst_level;
8698 if (last_pending_template)
8699 last_pending_template->next = pt;
8700 else
8701 pending_templates = pt;
8702
8703 last_pending_template = pt;
8704
8705 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8706
8707 if (level)
8708 pop_tinst_level ();
8709 }
8710
8711
8712 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8713 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8714 documentation for TEMPLATE_ID_EXPR. */
8715
8716 tree
8717 lookup_template_function (tree fns, tree arglist)
8718 {
8719 tree type;
8720
8721 if (fns == error_mark_node || arglist == error_mark_node)
8722 return error_mark_node;
8723
8724 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8725
8726 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8727 {
8728 error ("%q#D is not a function template", fns);
8729 return error_mark_node;
8730 }
8731
8732 if (BASELINK_P (fns))
8733 {
8734 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8735 unknown_type_node,
8736 BASELINK_FUNCTIONS (fns),
8737 arglist);
8738 return fns;
8739 }
8740
8741 type = TREE_TYPE (fns);
8742 if (TREE_CODE (fns) == OVERLOAD || !type)
8743 type = unknown_type_node;
8744
8745 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8746 }
8747
8748 /* Within the scope of a template class S<T>, the name S gets bound
8749 (in build_self_reference) to a TYPE_DECL for the class, not a
8750 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8751 or one of its enclosing classes, and that type is a template,
8752 return the associated TEMPLATE_DECL. Otherwise, the original
8753 DECL is returned.
8754
8755 Also handle the case when DECL is a TREE_LIST of ambiguous
8756 injected-class-names from different bases. */
8757
8758 tree
8759 maybe_get_template_decl_from_type_decl (tree decl)
8760 {
8761 if (decl == NULL_TREE)
8762 return decl;
8763
8764 /* DR 176: A lookup that finds an injected-class-name (10.2
8765 [class.member.lookup]) can result in an ambiguity in certain cases
8766 (for example, if it is found in more than one base class). If all of
8767 the injected-class-names that are found refer to specializations of
8768 the same class template, and if the name is followed by a
8769 template-argument-list, the reference refers to the class template
8770 itself and not a specialization thereof, and is not ambiguous. */
8771 if (TREE_CODE (decl) == TREE_LIST)
8772 {
8773 tree t, tmpl = NULL_TREE;
8774 for (t = decl; t; t = TREE_CHAIN (t))
8775 {
8776 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8777 if (!tmpl)
8778 tmpl = elt;
8779 else if (tmpl != elt)
8780 break;
8781 }
8782 if (tmpl && t == NULL_TREE)
8783 return tmpl;
8784 else
8785 return decl;
8786 }
8787
8788 return (decl != NULL_TREE
8789 && DECL_SELF_REFERENCE_P (decl)
8790 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8791 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8792 }
8793
8794 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8795 parameters, find the desired type.
8796
8797 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8798
8799 IN_DECL, if non-NULL, is the template declaration we are trying to
8800 instantiate.
8801
8802 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8803 the class we are looking up.
8804
8805 Issue error and warning messages under control of COMPLAIN.
8806
8807 If the template class is really a local class in a template
8808 function, then the FUNCTION_CONTEXT is the function in which it is
8809 being instantiated.
8810
8811 ??? Note that this function is currently called *twice* for each
8812 template-id: the first time from the parser, while creating the
8813 incomplete type (finish_template_type), and the second type during the
8814 real instantiation (instantiate_template_class). This is surely something
8815 that we want to avoid. It also causes some problems with argument
8816 coercion (see convert_nontype_argument for more information on this). */
8817
8818 static tree
8819 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8820 int entering_scope, tsubst_flags_t complain)
8821 {
8822 tree templ = NULL_TREE, parmlist;
8823 tree t;
8824 spec_entry **slot;
8825 spec_entry *entry;
8826 spec_entry elt;
8827 hashval_t hash;
8828
8829 if (identifier_p (d1))
8830 {
8831 tree value = innermost_non_namespace_value (d1);
8832 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8833 templ = value;
8834 else
8835 {
8836 if (context)
8837 push_decl_namespace (context);
8838 templ = lookup_name (d1);
8839 templ = maybe_get_template_decl_from_type_decl (templ);
8840 if (context)
8841 pop_decl_namespace ();
8842 }
8843 if (templ)
8844 context = DECL_CONTEXT (templ);
8845 }
8846 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8847 {
8848 tree type = TREE_TYPE (d1);
8849
8850 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8851 an implicit typename for the second A. Deal with it. */
8852 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8853 type = TREE_TYPE (type);
8854
8855 if (CLASSTYPE_TEMPLATE_INFO (type))
8856 {
8857 templ = CLASSTYPE_TI_TEMPLATE (type);
8858 d1 = DECL_NAME (templ);
8859 }
8860 }
8861 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8862 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8863 {
8864 templ = TYPE_TI_TEMPLATE (d1);
8865 d1 = DECL_NAME (templ);
8866 }
8867 else if (DECL_TYPE_TEMPLATE_P (d1))
8868 {
8869 templ = d1;
8870 d1 = DECL_NAME (templ);
8871 context = DECL_CONTEXT (templ);
8872 }
8873 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8874 {
8875 templ = d1;
8876 d1 = DECL_NAME (templ);
8877 }
8878
8879 /* Issue an error message if we didn't find a template. */
8880 if (! templ)
8881 {
8882 if (complain & tf_error)
8883 error ("%qT is not a template", d1);
8884 return error_mark_node;
8885 }
8886
8887 if (TREE_CODE (templ) != TEMPLATE_DECL
8888 /* Make sure it's a user visible template, if it was named by
8889 the user. */
8890 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8891 && !PRIMARY_TEMPLATE_P (templ)))
8892 {
8893 if (complain & tf_error)
8894 {
8895 error ("non-template type %qT used as a template", d1);
8896 if (in_decl)
8897 error ("for template declaration %q+D", in_decl);
8898 }
8899 return error_mark_node;
8900 }
8901
8902 complain &= ~tf_user;
8903
8904 /* An alias that just changes the name of a template is equivalent to the
8905 other template, so if any of the arguments are pack expansions, strip
8906 the alias to avoid problems with a pack expansion passed to a non-pack
8907 alias template parameter (DR 1430). */
8908 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8909 templ = get_underlying_template (templ);
8910
8911 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8912 {
8913 tree parm;
8914 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8915 if (arglist2 == error_mark_node
8916 || (!uses_template_parms (arglist2)
8917 && check_instantiated_args (templ, arglist2, complain)))
8918 return error_mark_node;
8919
8920 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8921 return parm;
8922 }
8923 else
8924 {
8925 tree template_type = TREE_TYPE (templ);
8926 tree gen_tmpl;
8927 tree type_decl;
8928 tree found = NULL_TREE;
8929 int arg_depth;
8930 int parm_depth;
8931 int is_dependent_type;
8932 int use_partial_inst_tmpl = false;
8933
8934 if (template_type == error_mark_node)
8935 /* An error occurred while building the template TEMPL, and a
8936 diagnostic has most certainly been emitted for that
8937 already. Let's propagate that error. */
8938 return error_mark_node;
8939
8940 gen_tmpl = most_general_template (templ);
8941 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8942 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8943 arg_depth = TMPL_ARGS_DEPTH (arglist);
8944
8945 if (arg_depth == 1 && parm_depth > 1)
8946 {
8947 /* We've been given an incomplete set of template arguments.
8948 For example, given:
8949
8950 template <class T> struct S1 {
8951 template <class U> struct S2 {};
8952 template <class U> struct S2<U*> {};
8953 };
8954
8955 we will be called with an ARGLIST of `U*', but the
8956 TEMPLATE will be `template <class T> template
8957 <class U> struct S1<T>::S2'. We must fill in the missing
8958 arguments. */
8959 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8960 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8961 arg_depth = TMPL_ARGS_DEPTH (arglist);
8962 }
8963
8964 /* Now we should have enough arguments. */
8965 gcc_assert (parm_depth == arg_depth);
8966
8967 /* From here on, we're only interested in the most general
8968 template. */
8969
8970 /* Calculate the BOUND_ARGS. These will be the args that are
8971 actually tsubst'd into the definition to create the
8972 instantiation. */
8973 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8974 complain,
8975 /*require_all_args=*/true,
8976 /*use_default_args=*/true);
8977
8978 if (arglist == error_mark_node)
8979 /* We were unable to bind the arguments. */
8980 return error_mark_node;
8981
8982 /* In the scope of a template class, explicit references to the
8983 template class refer to the type of the template, not any
8984 instantiation of it. For example, in:
8985
8986 template <class T> class C { void f(C<T>); }
8987
8988 the `C<T>' is just the same as `C'. Outside of the
8989 class, however, such a reference is an instantiation. */
8990 if (entering_scope
8991 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8992 || currently_open_class (template_type))
8993 {
8994 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8995
8996 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8997 return template_type;
8998 }
8999
9000 /* If we already have this specialization, return it. */
9001 elt.tmpl = gen_tmpl;
9002 elt.args = arglist;
9003 elt.spec = NULL_TREE;
9004 hash = spec_hasher::hash (&elt);
9005 entry = type_specializations->find_with_hash (&elt, hash);
9006
9007 if (entry)
9008 return entry->spec;
9009
9010 /* If the the template's constraints are not satisfied,
9011 then we cannot form a valid type.
9012
9013 Note that the check is deferred until after the hash
9014 lookup. This prevents redundant checks on previously
9015 instantiated specializations. */
9016 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9017 {
9018 if (complain & tf_error)
9019 {
9020 error ("template constraint failure");
9021 diagnose_constraints (input_location, gen_tmpl, arglist);
9022 }
9023 return error_mark_node;
9024 }
9025
9026 is_dependent_type = uses_template_parms (arglist);
9027
9028 /* If the deduced arguments are invalid, then the binding
9029 failed. */
9030 if (!is_dependent_type
9031 && check_instantiated_args (gen_tmpl,
9032 INNERMOST_TEMPLATE_ARGS (arglist),
9033 complain))
9034 return error_mark_node;
9035
9036 if (!is_dependent_type
9037 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9038 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9039 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9040 {
9041 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9042 DECL_NAME (gen_tmpl),
9043 /*tag_scope=*/ts_global);
9044 return found;
9045 }
9046
9047 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
9048 complain, in_decl);
9049 if (context == error_mark_node)
9050 return error_mark_node;
9051
9052 if (!context)
9053 context = global_namespace;
9054
9055 /* Create the type. */
9056 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9057 {
9058 /* The user referred to a specialization of an alias
9059 template represented by GEN_TMPL.
9060
9061 [temp.alias]/2 says:
9062
9063 When a template-id refers to the specialization of an
9064 alias template, it is equivalent to the associated
9065 type obtained by substitution of its
9066 template-arguments for the template-parameters in the
9067 type-id of the alias template. */
9068
9069 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9070 /* Note that the call above (by indirectly calling
9071 register_specialization in tsubst_decl) registers the
9072 TYPE_DECL representing the specialization of the alias
9073 template. So next time someone substitutes ARGLIST for
9074 the template parms into the alias template (GEN_TMPL),
9075 she'll get that TYPE_DECL back. */
9076
9077 if (t == error_mark_node)
9078 return t;
9079 }
9080 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9081 {
9082 if (!is_dependent_type)
9083 {
9084 set_current_access_from_decl (TYPE_NAME (template_type));
9085 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9086 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9087 arglist, complain, in_decl),
9088 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9089 arglist, complain, in_decl),
9090 SCOPED_ENUM_P (template_type), NULL);
9091
9092 if (t == error_mark_node)
9093 return t;
9094 }
9095 else
9096 {
9097 /* We don't want to call start_enum for this type, since
9098 the values for the enumeration constants may involve
9099 template parameters. And, no one should be interested
9100 in the enumeration constants for such a type. */
9101 t = cxx_make_type (ENUMERAL_TYPE);
9102 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9103 }
9104 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9105 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9106 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9107 }
9108 else if (CLASS_TYPE_P (template_type))
9109 {
9110 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9111 instantiated here. */
9112 gcc_assert (!LAMBDA_TYPE_P (template_type));
9113
9114 t = make_class_type (TREE_CODE (template_type));
9115 CLASSTYPE_DECLARED_CLASS (t)
9116 = CLASSTYPE_DECLARED_CLASS (template_type);
9117 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9118
9119 /* A local class. Make sure the decl gets registered properly. */
9120 if (context == current_function_decl)
9121 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
9122
9123 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9124 /* This instantiation is another name for the primary
9125 template type. Set the TYPE_CANONICAL field
9126 appropriately. */
9127 TYPE_CANONICAL (t) = template_type;
9128 else if (any_template_arguments_need_structural_equality_p (arglist))
9129 /* Some of the template arguments require structural
9130 equality testing, so this template class requires
9131 structural equality testing. */
9132 SET_TYPE_STRUCTURAL_EQUALITY (t);
9133 }
9134 else
9135 gcc_unreachable ();
9136
9137 /* If we called start_enum or pushtag above, this information
9138 will already be set up. */
9139 if (!TYPE_NAME (t))
9140 {
9141 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9142
9143 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9144 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9145 DECL_SOURCE_LOCATION (type_decl)
9146 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9147 }
9148 else
9149 type_decl = TYPE_NAME (t);
9150
9151 if (CLASS_TYPE_P (template_type))
9152 {
9153 TREE_PRIVATE (type_decl)
9154 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9155 TREE_PROTECTED (type_decl)
9156 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9157 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9158 {
9159 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9160 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9161 }
9162 }
9163
9164 if (OVERLOAD_TYPE_P (t)
9165 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9166 {
9167 static const char *tags[] = {"abi_tag", "may_alias"};
9168
9169 for (unsigned ix = 0; ix != 2; ix++)
9170 {
9171 tree attributes
9172 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9173
9174 if (attributes)
9175 TYPE_ATTRIBUTES (t)
9176 = tree_cons (TREE_PURPOSE (attributes),
9177 TREE_VALUE (attributes),
9178 TYPE_ATTRIBUTES (t));
9179 }
9180 }
9181
9182 /* Let's consider the explicit specialization of a member
9183 of a class template specialization that is implicitly instantiated,
9184 e.g.:
9185 template<class T>
9186 struct S
9187 {
9188 template<class U> struct M {}; //#0
9189 };
9190
9191 template<>
9192 template<>
9193 struct S<int>::M<char> //#1
9194 {
9195 int i;
9196 };
9197 [temp.expl.spec]/4 says this is valid.
9198
9199 In this case, when we write:
9200 S<int>::M<char> m;
9201
9202 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9203 the one of #0.
9204
9205 When we encounter #1, we want to store the partial instantiation
9206 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9207
9208 For all cases other than this "explicit specialization of member of a
9209 class template", we just want to store the most general template into
9210 the CLASSTYPE_TI_TEMPLATE of M.
9211
9212 This case of "explicit specialization of member of a class template"
9213 only happens when:
9214 1/ the enclosing class is an instantiation of, and therefore not
9215 the same as, the context of the most general template, and
9216 2/ we aren't looking at the partial instantiation itself, i.e.
9217 the innermost arguments are not the same as the innermost parms of
9218 the most general template.
9219
9220 So it's only when 1/ and 2/ happens that we want to use the partial
9221 instantiation of the member template in lieu of its most general
9222 template. */
9223
9224 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9225 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9226 /* the enclosing class must be an instantiation... */
9227 && CLASS_TYPE_P (context)
9228 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9229 {
9230 TREE_VEC_LENGTH (arglist)--;
9231 ++processing_template_decl;
9232 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9233 tree partial_inst_args =
9234 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9235 arglist, complain, NULL_TREE);
9236 --processing_template_decl;
9237 TREE_VEC_LENGTH (arglist)++;
9238 if (partial_inst_args == error_mark_node)
9239 return error_mark_node;
9240 use_partial_inst_tmpl =
9241 /*...and we must not be looking at the partial instantiation
9242 itself. */
9243 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9244 partial_inst_args);
9245 }
9246
9247 if (!use_partial_inst_tmpl)
9248 /* This case is easy; there are no member templates involved. */
9249 found = gen_tmpl;
9250 else
9251 {
9252 /* This is a full instantiation of a member template. Find
9253 the partial instantiation of which this is an instance. */
9254
9255 /* Temporarily reduce by one the number of levels in the ARGLIST
9256 so as to avoid comparing the last set of arguments. */
9257 TREE_VEC_LENGTH (arglist)--;
9258 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9259 TREE_VEC_LENGTH (arglist)++;
9260 /* FOUND is either a proper class type, or an alias
9261 template specialization. In the later case, it's a
9262 TYPE_DECL, resulting from the substituting of arguments
9263 for parameters in the TYPE_DECL of the alias template
9264 done earlier. So be careful while getting the template
9265 of FOUND. */
9266 found = (TREE_CODE (found) == TEMPLATE_DECL
9267 ? found
9268 : (TREE_CODE (found) == TYPE_DECL
9269 ? DECL_TI_TEMPLATE (found)
9270 : CLASSTYPE_TI_TEMPLATE (found)));
9271 }
9272
9273 // Build template info for the new specialization.
9274 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9275
9276 elt.spec = t;
9277 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9278 entry = ggc_alloc<spec_entry> ();
9279 *entry = elt;
9280 *slot = entry;
9281
9282 /* Note this use of the partial instantiation so we can check it
9283 later in maybe_process_partial_specialization. */
9284 DECL_TEMPLATE_INSTANTIATIONS (found)
9285 = tree_cons (arglist, t,
9286 DECL_TEMPLATE_INSTANTIATIONS (found));
9287
9288 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9289 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9290 /* Now that the type has been registered on the instantiations
9291 list, we set up the enumerators. Because the enumeration
9292 constants may involve the enumeration type itself, we make
9293 sure to register the type first, and then create the
9294 constants. That way, doing tsubst_expr for the enumeration
9295 constants won't result in recursive calls here; we'll find
9296 the instantiation and exit above. */
9297 tsubst_enum (template_type, t, arglist);
9298
9299 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9300 /* If the type makes use of template parameters, the
9301 code that generates debugging information will crash. */
9302 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9303
9304 /* Possibly limit visibility based on template args. */
9305 TREE_PUBLIC (type_decl) = 1;
9306 determine_visibility (type_decl);
9307
9308 inherit_targ_abi_tags (t);
9309
9310 return t;
9311 }
9312 }
9313
9314 /* Wrapper for lookup_template_class_1. */
9315
9316 tree
9317 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9318 int entering_scope, tsubst_flags_t complain)
9319 {
9320 tree ret;
9321 timevar_push (TV_TEMPLATE_INST);
9322 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9323 entering_scope, complain);
9324 timevar_pop (TV_TEMPLATE_INST);
9325 return ret;
9326 }
9327
9328 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9329
9330 tree
9331 lookup_template_variable (tree templ, tree arglist)
9332 {
9333 /* The type of the expression is NULL_TREE since the template-id could refer
9334 to an explicit or partial specialization. */
9335 tree type = NULL_TREE;
9336 if (flag_concepts && variable_concept_p (templ))
9337 /* Except that concepts are always bool. */
9338 type = boolean_type_node;
9339 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9340 }
9341
9342 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9343
9344 tree
9345 finish_template_variable (tree var, tsubst_flags_t complain)
9346 {
9347 tree templ = TREE_OPERAND (var, 0);
9348 tree arglist = TREE_OPERAND (var, 1);
9349
9350 /* We never want to return a VAR_DECL for a variable concept, since they
9351 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9352 bool concept_p = flag_concepts && variable_concept_p (templ);
9353 if (concept_p && processing_template_decl)
9354 return var;
9355
9356 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9357 arglist = add_outermost_template_args (tmpl_args, arglist);
9358
9359 templ = most_general_template (templ);
9360 tree parms = DECL_TEMPLATE_PARMS (templ);
9361 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9362 /*req_all*/true,
9363 /*use_default*/true);
9364
9365 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9366 {
9367 if (complain & tf_error)
9368 {
9369 error ("use of invalid variable template %qE", var);
9370 diagnose_constraints (location_of (var), templ, arglist);
9371 }
9372 return error_mark_node;
9373 }
9374
9375 /* If a template-id refers to a specialization of a variable
9376 concept, then the expression is true if and only if the
9377 concept's constraints are satisfied by the given template
9378 arguments.
9379
9380 NOTE: This is an extension of Concepts Lite TS that
9381 allows constraints to be used in expressions. */
9382 if (concept_p)
9383 {
9384 tree decl = DECL_TEMPLATE_RESULT (templ);
9385 return evaluate_variable_concept (decl, arglist);
9386 }
9387
9388 return instantiate_template (templ, arglist, complain);
9389 }
9390
9391 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9392 TARGS template args, and instantiate it if it's not dependent. */
9393
9394 tree
9395 lookup_and_finish_template_variable (tree templ, tree targs,
9396 tsubst_flags_t complain)
9397 {
9398 templ = lookup_template_variable (templ, targs);
9399 if (!any_dependent_template_arguments_p (targs))
9400 {
9401 templ = finish_template_variable (templ, complain);
9402 mark_used (templ);
9403 }
9404
9405 return convert_from_reference (templ);
9406 }
9407
9408 \f
9409 struct pair_fn_data
9410 {
9411 tree_fn_t fn;
9412 tree_fn_t any_fn;
9413 void *data;
9414 /* True when we should also visit template parameters that occur in
9415 non-deduced contexts. */
9416 bool include_nondeduced_p;
9417 hash_set<tree> *visited;
9418 };
9419
9420 /* Called from for_each_template_parm via walk_tree. */
9421
9422 static tree
9423 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9424 {
9425 tree t = *tp;
9426 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9427 tree_fn_t fn = pfd->fn;
9428 void *data = pfd->data;
9429 tree result = NULL_TREE;
9430
9431 #define WALK_SUBTREE(NODE) \
9432 do \
9433 { \
9434 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9435 pfd->include_nondeduced_p, \
9436 pfd->any_fn); \
9437 if (result) goto out; \
9438 } \
9439 while (0)
9440
9441 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9442 return t;
9443
9444 if (TYPE_P (t)
9445 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9446 WALK_SUBTREE (TYPE_CONTEXT (t));
9447
9448 switch (TREE_CODE (t))
9449 {
9450 case RECORD_TYPE:
9451 if (TYPE_PTRMEMFUNC_P (t))
9452 break;
9453 /* Fall through. */
9454
9455 case UNION_TYPE:
9456 case ENUMERAL_TYPE:
9457 if (!TYPE_TEMPLATE_INFO (t))
9458 *walk_subtrees = 0;
9459 else
9460 WALK_SUBTREE (TYPE_TI_ARGS (t));
9461 break;
9462
9463 case INTEGER_TYPE:
9464 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9465 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9466 break;
9467
9468 case METHOD_TYPE:
9469 /* Since we're not going to walk subtrees, we have to do this
9470 explicitly here. */
9471 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9472 /* Fall through. */
9473
9474 case FUNCTION_TYPE:
9475 /* Check the return type. */
9476 WALK_SUBTREE (TREE_TYPE (t));
9477
9478 /* Check the parameter types. Since default arguments are not
9479 instantiated until they are needed, the TYPE_ARG_TYPES may
9480 contain expressions that involve template parameters. But,
9481 no-one should be looking at them yet. And, once they're
9482 instantiated, they don't contain template parameters, so
9483 there's no point in looking at them then, either. */
9484 {
9485 tree parm;
9486
9487 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9488 WALK_SUBTREE (TREE_VALUE (parm));
9489
9490 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9491 want walk_tree walking into them itself. */
9492 *walk_subtrees = 0;
9493 }
9494
9495 if (flag_noexcept_type)
9496 {
9497 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9498 if (spec)
9499 WALK_SUBTREE (TREE_PURPOSE (spec));
9500 }
9501 break;
9502
9503 case TYPEOF_TYPE:
9504 case UNDERLYING_TYPE:
9505 if (pfd->include_nondeduced_p
9506 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9507 pfd->visited,
9508 pfd->include_nondeduced_p,
9509 pfd->any_fn))
9510 return error_mark_node;
9511 break;
9512
9513 case FUNCTION_DECL:
9514 case VAR_DECL:
9515 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9516 WALK_SUBTREE (DECL_TI_ARGS (t));
9517 /* Fall through. */
9518
9519 case PARM_DECL:
9520 case CONST_DECL:
9521 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9522 WALK_SUBTREE (DECL_INITIAL (t));
9523 if (DECL_CONTEXT (t)
9524 && pfd->include_nondeduced_p)
9525 WALK_SUBTREE (DECL_CONTEXT (t));
9526 break;
9527
9528 case BOUND_TEMPLATE_TEMPLATE_PARM:
9529 /* Record template parameters such as `T' inside `TT<T>'. */
9530 WALK_SUBTREE (TYPE_TI_ARGS (t));
9531 /* Fall through. */
9532
9533 case TEMPLATE_TEMPLATE_PARM:
9534 case TEMPLATE_TYPE_PARM:
9535 case TEMPLATE_PARM_INDEX:
9536 if (fn && (*fn)(t, data))
9537 return t;
9538 else if (!fn)
9539 return t;
9540 break;
9541
9542 case TEMPLATE_DECL:
9543 /* A template template parameter is encountered. */
9544 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9545 WALK_SUBTREE (TREE_TYPE (t));
9546
9547 /* Already substituted template template parameter */
9548 *walk_subtrees = 0;
9549 break;
9550
9551 case TYPENAME_TYPE:
9552 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9553 partial instantiation. */
9554 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9555 break;
9556
9557 case CONSTRUCTOR:
9558 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9559 && pfd->include_nondeduced_p)
9560 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9561 break;
9562
9563 case INDIRECT_REF:
9564 case COMPONENT_REF:
9565 /* If there's no type, then this thing must be some expression
9566 involving template parameters. */
9567 if (!fn && !TREE_TYPE (t))
9568 return error_mark_node;
9569 break;
9570
9571 case MODOP_EXPR:
9572 case CAST_EXPR:
9573 case IMPLICIT_CONV_EXPR:
9574 case REINTERPRET_CAST_EXPR:
9575 case CONST_CAST_EXPR:
9576 case STATIC_CAST_EXPR:
9577 case DYNAMIC_CAST_EXPR:
9578 case ARROW_EXPR:
9579 case DOTSTAR_EXPR:
9580 case TYPEID_EXPR:
9581 case PSEUDO_DTOR_EXPR:
9582 if (!fn)
9583 return error_mark_node;
9584 break;
9585
9586 default:
9587 break;
9588 }
9589
9590 #undef WALK_SUBTREE
9591
9592 /* We didn't find any template parameters we liked. */
9593 out:
9594 return result;
9595 }
9596
9597 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9598 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9599 call FN with the parameter and the DATA.
9600 If FN returns nonzero, the iteration is terminated, and
9601 for_each_template_parm returns 1. Otherwise, the iteration
9602 continues. If FN never returns a nonzero value, the value
9603 returned by for_each_template_parm is 0. If FN is NULL, it is
9604 considered to be the function which always returns 1.
9605
9606 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9607 parameters that occur in non-deduced contexts. When false, only
9608 visits those template parameters that can be deduced. */
9609
9610 static tree
9611 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9612 hash_set<tree> *visited,
9613 bool include_nondeduced_p,
9614 tree_fn_t any_fn)
9615 {
9616 struct pair_fn_data pfd;
9617 tree result;
9618
9619 /* Set up. */
9620 pfd.fn = fn;
9621 pfd.any_fn = any_fn;
9622 pfd.data = data;
9623 pfd.include_nondeduced_p = include_nondeduced_p;
9624
9625 /* Walk the tree. (Conceptually, we would like to walk without
9626 duplicates, but for_each_template_parm_r recursively calls
9627 for_each_template_parm, so we would need to reorganize a fair
9628 bit to use walk_tree_without_duplicates, so we keep our own
9629 visited list.) */
9630 if (visited)
9631 pfd.visited = visited;
9632 else
9633 pfd.visited = new hash_set<tree>;
9634 result = cp_walk_tree (&t,
9635 for_each_template_parm_r,
9636 &pfd,
9637 pfd.visited);
9638
9639 /* Clean up. */
9640 if (!visited)
9641 {
9642 delete pfd.visited;
9643 pfd.visited = 0;
9644 }
9645
9646 return result;
9647 }
9648
9649 /* Returns true if T depends on any template parameter. */
9650
9651 int
9652 uses_template_parms (tree t)
9653 {
9654 if (t == NULL_TREE)
9655 return false;
9656
9657 bool dependent_p;
9658 int saved_processing_template_decl;
9659
9660 saved_processing_template_decl = processing_template_decl;
9661 if (!saved_processing_template_decl)
9662 processing_template_decl = 1;
9663 if (TYPE_P (t))
9664 dependent_p = dependent_type_p (t);
9665 else if (TREE_CODE (t) == TREE_VEC)
9666 dependent_p = any_dependent_template_arguments_p (t);
9667 else if (TREE_CODE (t) == TREE_LIST)
9668 dependent_p = (uses_template_parms (TREE_VALUE (t))
9669 || uses_template_parms (TREE_CHAIN (t)));
9670 else if (TREE_CODE (t) == TYPE_DECL)
9671 dependent_p = dependent_type_p (TREE_TYPE (t));
9672 else if (DECL_P (t)
9673 || EXPR_P (t)
9674 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9675 || TREE_CODE (t) == OVERLOAD
9676 || BASELINK_P (t)
9677 || identifier_p (t)
9678 || TREE_CODE (t) == TRAIT_EXPR
9679 || TREE_CODE (t) == CONSTRUCTOR
9680 || CONSTANT_CLASS_P (t))
9681 dependent_p = (type_dependent_expression_p (t)
9682 || value_dependent_expression_p (t));
9683 else
9684 {
9685 gcc_assert (t == error_mark_node);
9686 dependent_p = false;
9687 }
9688
9689 processing_template_decl = saved_processing_template_decl;
9690
9691 return dependent_p;
9692 }
9693
9694 /* Returns true iff current_function_decl is an incompletely instantiated
9695 template. Useful instead of processing_template_decl because the latter
9696 is set to 0 during instantiate_non_dependent_expr. */
9697
9698 bool
9699 in_template_function (void)
9700 {
9701 tree fn = current_function_decl;
9702 bool ret;
9703 ++processing_template_decl;
9704 ret = (fn && DECL_LANG_SPECIFIC (fn)
9705 && DECL_TEMPLATE_INFO (fn)
9706 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9707 --processing_template_decl;
9708 return ret;
9709 }
9710
9711 /* Returns true if T depends on any template parameter with level LEVEL. */
9712
9713 bool
9714 uses_template_parms_level (tree t, int level)
9715 {
9716 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9717 /*include_nondeduced_p=*/true);
9718 }
9719
9720 /* Returns true if the signature of DECL depends on any template parameter from
9721 its enclosing class. */
9722
9723 bool
9724 uses_outer_template_parms (tree decl)
9725 {
9726 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9727 if (depth == 0)
9728 return false;
9729 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9730 &depth, NULL, /*include_nondeduced_p=*/true))
9731 return true;
9732 if (PRIMARY_TEMPLATE_P (decl)
9733 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9734 (DECL_TEMPLATE_PARMS (decl)),
9735 template_parm_outer_level,
9736 &depth, NULL, /*include_nondeduced_p=*/true))
9737 return true;
9738 tree ci = get_constraints (decl);
9739 if (ci)
9740 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9741 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9742 &depth, NULL, /*nondeduced*/true))
9743 return true;
9744 return false;
9745 }
9746
9747 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9748 ill-formed translation unit, i.e. a variable or function that isn't
9749 usable in a constant expression. */
9750
9751 static inline bool
9752 neglectable_inst_p (tree d)
9753 {
9754 return (DECL_P (d)
9755 && !undeduced_auto_decl (d)
9756 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9757 : decl_maybe_constant_var_p (d)));
9758 }
9759
9760 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9761 neglectable and instantiated from within an erroneous instantiation. */
9762
9763 static bool
9764 limit_bad_template_recursion (tree decl)
9765 {
9766 struct tinst_level *lev = current_tinst_level;
9767 int errs = errorcount + sorrycount;
9768 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9769 return false;
9770
9771 for (; lev; lev = lev->next)
9772 if (neglectable_inst_p (lev->decl))
9773 break;
9774
9775 return (lev && errs > lev->errors);
9776 }
9777
9778 static int tinst_depth;
9779 extern int max_tinst_depth;
9780 int depth_reached;
9781
9782 static GTY(()) struct tinst_level *last_error_tinst_level;
9783
9784 /* We're starting to instantiate D; record the template instantiation context
9785 for diagnostics and to restore it later. */
9786
9787 bool
9788 push_tinst_level (tree d)
9789 {
9790 return push_tinst_level_loc (d, input_location);
9791 }
9792
9793 /* We're starting to instantiate D; record the template instantiation context
9794 at LOC for diagnostics and to restore it later. */
9795
9796 bool
9797 push_tinst_level_loc (tree d, location_t loc)
9798 {
9799 struct tinst_level *new_level;
9800
9801 if (tinst_depth >= max_tinst_depth)
9802 {
9803 /* Tell error.c not to try to instantiate any templates. */
9804 at_eof = 2;
9805 fatal_error (input_location,
9806 "template instantiation depth exceeds maximum of %d"
9807 " (use -ftemplate-depth= to increase the maximum)",
9808 max_tinst_depth);
9809 return false;
9810 }
9811
9812 /* If the current instantiation caused problems, don't let it instantiate
9813 anything else. Do allow deduction substitution and decls usable in
9814 constant expressions. */
9815 if (limit_bad_template_recursion (d))
9816 return false;
9817
9818 /* When not -quiet, dump template instantiations other than functions, since
9819 announce_function will take care of those. */
9820 if (!quiet_flag
9821 && TREE_CODE (d) != TREE_LIST
9822 && TREE_CODE (d) != FUNCTION_DECL)
9823 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9824
9825 new_level = ggc_alloc<tinst_level> ();
9826 new_level->decl = d;
9827 new_level->locus = loc;
9828 new_level->errors = errorcount+sorrycount;
9829 new_level->in_system_header_p = in_system_header_at (input_location);
9830 new_level->next = current_tinst_level;
9831 current_tinst_level = new_level;
9832
9833 ++tinst_depth;
9834 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9835 depth_reached = tinst_depth;
9836
9837 return true;
9838 }
9839
9840 /* We're done instantiating this template; return to the instantiation
9841 context. */
9842
9843 void
9844 pop_tinst_level (void)
9845 {
9846 /* Restore the filename and line number stashed away when we started
9847 this instantiation. */
9848 input_location = current_tinst_level->locus;
9849 current_tinst_level = current_tinst_level->next;
9850 --tinst_depth;
9851 }
9852
9853 /* We're instantiating a deferred template; restore the template
9854 instantiation context in which the instantiation was requested, which
9855 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9856
9857 static tree
9858 reopen_tinst_level (struct tinst_level *level)
9859 {
9860 struct tinst_level *t;
9861
9862 tinst_depth = 0;
9863 for (t = level; t; t = t->next)
9864 ++tinst_depth;
9865
9866 current_tinst_level = level;
9867 pop_tinst_level ();
9868 if (current_tinst_level)
9869 current_tinst_level->errors = errorcount+sorrycount;
9870 return level->decl;
9871 }
9872
9873 /* Returns the TINST_LEVEL which gives the original instantiation
9874 context. */
9875
9876 struct tinst_level *
9877 outermost_tinst_level (void)
9878 {
9879 struct tinst_level *level = current_tinst_level;
9880 if (level)
9881 while (level->next)
9882 level = level->next;
9883 return level;
9884 }
9885
9886 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9887 vector of template arguments, as for tsubst.
9888
9889 Returns an appropriate tsubst'd friend declaration. */
9890
9891 static tree
9892 tsubst_friend_function (tree decl, tree args)
9893 {
9894 tree new_friend;
9895
9896 if (TREE_CODE (decl) == FUNCTION_DECL
9897 && DECL_TEMPLATE_INSTANTIATION (decl)
9898 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9899 /* This was a friend declared with an explicit template
9900 argument list, e.g.:
9901
9902 friend void f<>(T);
9903
9904 to indicate that f was a template instantiation, not a new
9905 function declaration. Now, we have to figure out what
9906 instantiation of what template. */
9907 {
9908 tree template_id, arglist, fns;
9909 tree new_args;
9910 tree tmpl;
9911 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9912
9913 /* Friend functions are looked up in the containing namespace scope.
9914 We must enter that scope, to avoid finding member functions of the
9915 current class with same name. */
9916 push_nested_namespace (ns);
9917 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9918 tf_warning_or_error, NULL_TREE,
9919 /*integral_constant_expression_p=*/false);
9920 pop_nested_namespace (ns);
9921 arglist = tsubst (DECL_TI_ARGS (decl), args,
9922 tf_warning_or_error, NULL_TREE);
9923 template_id = lookup_template_function (fns, arglist);
9924
9925 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9926 tmpl = determine_specialization (template_id, new_friend,
9927 &new_args,
9928 /*need_member_template=*/0,
9929 TREE_VEC_LENGTH (args),
9930 tsk_none);
9931 return instantiate_template (tmpl, new_args, tf_error);
9932 }
9933
9934 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9935
9936 /* The NEW_FRIEND will look like an instantiation, to the
9937 compiler, but is not an instantiation from the point of view of
9938 the language. For example, we might have had:
9939
9940 template <class T> struct S {
9941 template <class U> friend void f(T, U);
9942 };
9943
9944 Then, in S<int>, template <class U> void f(int, U) is not an
9945 instantiation of anything. */
9946 if (new_friend == error_mark_node)
9947 return error_mark_node;
9948
9949 DECL_USE_TEMPLATE (new_friend) = 0;
9950 if (TREE_CODE (decl) == TEMPLATE_DECL)
9951 {
9952 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9953 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9954 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9955 }
9956
9957 /* The mangled name for the NEW_FRIEND is incorrect. The function
9958 is not a template instantiation and should not be mangled like
9959 one. Therefore, we forget the mangling here; we'll recompute it
9960 later if we need it. */
9961 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9962 {
9963 SET_DECL_RTL (new_friend, NULL);
9964 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9965 }
9966
9967 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9968 {
9969 tree old_decl;
9970 tree new_friend_template_info;
9971 tree new_friend_result_template_info;
9972 tree ns;
9973 int new_friend_is_defn;
9974
9975 /* We must save some information from NEW_FRIEND before calling
9976 duplicate decls since that function will free NEW_FRIEND if
9977 possible. */
9978 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9979 new_friend_is_defn =
9980 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9981 (template_for_substitution (new_friend)))
9982 != NULL_TREE);
9983 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9984 {
9985 /* This declaration is a `primary' template. */
9986 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9987
9988 new_friend_result_template_info
9989 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9990 }
9991 else
9992 new_friend_result_template_info = NULL_TREE;
9993
9994 /* Inside pushdecl_namespace_level, we will push into the
9995 current namespace. However, the friend function should go
9996 into the namespace of the template. */
9997 ns = decl_namespace_context (new_friend);
9998 push_nested_namespace (ns);
9999 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10000 pop_nested_namespace (ns);
10001
10002 if (old_decl == error_mark_node)
10003 return error_mark_node;
10004
10005 if (old_decl != new_friend)
10006 {
10007 /* This new friend declaration matched an existing
10008 declaration. For example, given:
10009
10010 template <class T> void f(T);
10011 template <class U> class C {
10012 template <class T> friend void f(T) {}
10013 };
10014
10015 the friend declaration actually provides the definition
10016 of `f', once C has been instantiated for some type. So,
10017 old_decl will be the out-of-class template declaration,
10018 while new_friend is the in-class definition.
10019
10020 But, if `f' was called before this point, the
10021 instantiation of `f' will have DECL_TI_ARGS corresponding
10022 to `T' but not to `U', references to which might appear
10023 in the definition of `f'. Previously, the most general
10024 template for an instantiation of `f' was the out-of-class
10025 version; now it is the in-class version. Therefore, we
10026 run through all specialization of `f', adding to their
10027 DECL_TI_ARGS appropriately. In particular, they need a
10028 new set of outer arguments, corresponding to the
10029 arguments for this class instantiation.
10030
10031 The same situation can arise with something like this:
10032
10033 friend void f(int);
10034 template <class T> class C {
10035 friend void f(T) {}
10036 };
10037
10038 when `C<int>' is instantiated. Now, `f(int)' is defined
10039 in the class. */
10040
10041 if (!new_friend_is_defn)
10042 /* On the other hand, if the in-class declaration does
10043 *not* provide a definition, then we don't want to alter
10044 existing definitions. We can just leave everything
10045 alone. */
10046 ;
10047 else
10048 {
10049 tree new_template = TI_TEMPLATE (new_friend_template_info);
10050 tree new_args = TI_ARGS (new_friend_template_info);
10051
10052 /* Overwrite whatever template info was there before, if
10053 any, with the new template information pertaining to
10054 the declaration. */
10055 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10056
10057 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10058 {
10059 /* We should have called reregister_specialization in
10060 duplicate_decls. */
10061 gcc_assert (retrieve_specialization (new_template,
10062 new_args, 0)
10063 == old_decl);
10064
10065 /* Instantiate it if the global has already been used. */
10066 if (DECL_ODR_USED (old_decl))
10067 instantiate_decl (old_decl, /*defer_ok=*/true,
10068 /*expl_inst_class_mem_p=*/false);
10069 }
10070 else
10071 {
10072 tree t;
10073
10074 /* Indicate that the old function template is a partial
10075 instantiation. */
10076 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10077 = new_friend_result_template_info;
10078
10079 gcc_assert (new_template
10080 == most_general_template (new_template));
10081 gcc_assert (new_template != old_decl);
10082
10083 /* Reassign any specializations already in the hash table
10084 to the new more general template, and add the
10085 additional template args. */
10086 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10087 t != NULL_TREE;
10088 t = TREE_CHAIN (t))
10089 {
10090 tree spec = TREE_VALUE (t);
10091 spec_entry elt;
10092
10093 elt.tmpl = old_decl;
10094 elt.args = DECL_TI_ARGS (spec);
10095 elt.spec = NULL_TREE;
10096
10097 decl_specializations->remove_elt (&elt);
10098
10099 DECL_TI_ARGS (spec)
10100 = add_outermost_template_args (new_args,
10101 DECL_TI_ARGS (spec));
10102
10103 register_specialization
10104 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10105
10106 }
10107 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10108 }
10109 }
10110
10111 /* The information from NEW_FRIEND has been merged into OLD_DECL
10112 by duplicate_decls. */
10113 new_friend = old_decl;
10114 }
10115 }
10116 else
10117 {
10118 tree context = DECL_CONTEXT (new_friend);
10119 bool dependent_p;
10120
10121 /* In the code
10122 template <class T> class C {
10123 template <class U> friend void C1<U>::f (); // case 1
10124 friend void C2<T>::f (); // case 2
10125 };
10126 we only need to make sure CONTEXT is a complete type for
10127 case 2. To distinguish between the two cases, we note that
10128 CONTEXT of case 1 remains dependent type after tsubst while
10129 this isn't true for case 2. */
10130 ++processing_template_decl;
10131 dependent_p = dependent_type_p (context);
10132 --processing_template_decl;
10133
10134 if (!dependent_p
10135 && !complete_type_or_else (context, NULL_TREE))
10136 return error_mark_node;
10137
10138 if (COMPLETE_TYPE_P (context))
10139 {
10140 tree fn = new_friend;
10141 /* do_friend adds the TEMPLATE_DECL for any member friend
10142 template even if it isn't a member template, i.e.
10143 template <class T> friend A<T>::f();
10144 Look through it in that case. */
10145 if (TREE_CODE (fn) == TEMPLATE_DECL
10146 && !PRIMARY_TEMPLATE_P (fn))
10147 fn = DECL_TEMPLATE_RESULT (fn);
10148 /* Check to see that the declaration is really present, and,
10149 possibly obtain an improved declaration. */
10150 fn = check_classfn (context, fn, NULL_TREE);
10151
10152 if (fn)
10153 new_friend = fn;
10154 }
10155 }
10156
10157 return new_friend;
10158 }
10159
10160 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10161 template arguments, as for tsubst.
10162
10163 Returns an appropriate tsubst'd friend type or error_mark_node on
10164 failure. */
10165
10166 static tree
10167 tsubst_friend_class (tree friend_tmpl, tree args)
10168 {
10169 tree tmpl;
10170
10171 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10172 {
10173 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10174 return TREE_TYPE (tmpl);
10175 }
10176
10177 tree context = CP_DECL_CONTEXT (friend_tmpl);
10178 if (TREE_CODE (context) == NAMESPACE_DECL)
10179 push_nested_namespace (context);
10180 else
10181 push_nested_class (context);
10182
10183 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10184 /*non_class=*/false, /*block_p=*/false,
10185 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10186
10187 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10188 {
10189 /* The friend template has already been declared. Just
10190 check to see that the declarations match, and install any new
10191 default parameters. We must tsubst the default parameters,
10192 of course. We only need the innermost template parameters
10193 because that is all that redeclare_class_template will look
10194 at. */
10195 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10196 > TMPL_ARGS_DEPTH (args))
10197 {
10198 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10199 args, tf_warning_or_error);
10200 location_t saved_input_location = input_location;
10201 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10202 tree cons = get_constraints (tmpl);
10203 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10204 input_location = saved_input_location;
10205 }
10206 }
10207 else
10208 {
10209 /* The friend template has not already been declared. In this
10210 case, the instantiation of the template class will cause the
10211 injection of this template into the namespace scope. */
10212 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10213
10214 if (tmpl != error_mark_node)
10215 {
10216 /* The new TMPL is not an instantiation of anything, so we
10217 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10218 for the new type because that is supposed to be the
10219 corresponding template decl, i.e., TMPL. */
10220 DECL_USE_TEMPLATE (tmpl) = 0;
10221 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10222 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10223 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10224 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10225
10226 /* It is hidden. */
10227 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10228 DECL_ANTICIPATED (tmpl)
10229 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10230
10231 /* Inject this template into the enclosing namspace scope. */
10232 tmpl = pushdecl_namespace_level (tmpl, true);
10233 }
10234 }
10235
10236 if (TREE_CODE (context) == NAMESPACE_DECL)
10237 pop_nested_namespace (context);
10238 else
10239 pop_nested_class ();
10240
10241 return TREE_TYPE (tmpl);
10242 }
10243
10244 /* Returns zero if TYPE cannot be completed later due to circularity.
10245 Otherwise returns one. */
10246
10247 static int
10248 can_complete_type_without_circularity (tree type)
10249 {
10250 if (type == NULL_TREE || type == error_mark_node)
10251 return 0;
10252 else if (COMPLETE_TYPE_P (type))
10253 return 1;
10254 else if (TREE_CODE (type) == ARRAY_TYPE)
10255 return can_complete_type_without_circularity (TREE_TYPE (type));
10256 else if (CLASS_TYPE_P (type)
10257 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10258 return 0;
10259 else
10260 return 1;
10261 }
10262
10263 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10264 tsubst_flags_t, tree);
10265
10266 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10267 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10268
10269 static tree
10270 tsubst_attribute (tree t, tree *decl_p, tree args,
10271 tsubst_flags_t complain, tree in_decl)
10272 {
10273 gcc_assert (ATTR_IS_DEPENDENT (t));
10274
10275 tree val = TREE_VALUE (t);
10276 if (val == NULL_TREE)
10277 /* Nothing to do. */;
10278 else if ((flag_openmp || flag_openmp_simd)
10279 && is_attribute_p ("omp declare simd",
10280 get_attribute_name (t)))
10281 {
10282 tree clauses = TREE_VALUE (val);
10283 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10284 complain, in_decl);
10285 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10286 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10287 tree parms = DECL_ARGUMENTS (*decl_p);
10288 clauses
10289 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10290 if (clauses)
10291 val = build_tree_list (NULL_TREE, clauses);
10292 else
10293 val = NULL_TREE;
10294 }
10295 /* If the first attribute argument is an identifier, don't
10296 pass it through tsubst. Attributes like mode, format,
10297 cleanup and several target specific attributes expect it
10298 unmodified. */
10299 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10300 {
10301 tree chain
10302 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10303 /*integral_constant_expression_p=*/false);
10304 if (chain != TREE_CHAIN (val))
10305 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10306 }
10307 else if (PACK_EXPANSION_P (val))
10308 {
10309 /* An attribute pack expansion. */
10310 tree purp = TREE_PURPOSE (t);
10311 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10312 if (pack == error_mark_node)
10313 return error_mark_node;
10314 int len = TREE_VEC_LENGTH (pack);
10315 tree list = NULL_TREE;
10316 tree *q = &list;
10317 for (int i = 0; i < len; ++i)
10318 {
10319 tree elt = TREE_VEC_ELT (pack, i);
10320 *q = build_tree_list (purp, elt);
10321 q = &TREE_CHAIN (*q);
10322 }
10323 return list;
10324 }
10325 else
10326 val = tsubst_expr (val, args, complain, in_decl,
10327 /*integral_constant_expression_p=*/false);
10328
10329 if (val != TREE_VALUE (t))
10330 return build_tree_list (TREE_PURPOSE (t), val);
10331 return t;
10332 }
10333
10334 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10335 unchanged or a new TREE_LIST chain. */
10336
10337 static tree
10338 tsubst_attributes (tree attributes, tree args,
10339 tsubst_flags_t complain, tree in_decl)
10340 {
10341 tree last_dep = NULL_TREE;
10342
10343 for (tree t = attributes; t; t = TREE_CHAIN (t))
10344 if (ATTR_IS_DEPENDENT (t))
10345 {
10346 last_dep = t;
10347 attributes = copy_list (attributes);
10348 break;
10349 }
10350
10351 if (last_dep)
10352 for (tree *p = &attributes; *p; )
10353 {
10354 tree t = *p;
10355 if (ATTR_IS_DEPENDENT (t))
10356 {
10357 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10358 if (subst != t)
10359 {
10360 *p = subst;
10361 while (*p)
10362 p = &TREE_CHAIN (*p);
10363 *p = TREE_CHAIN (t);
10364 continue;
10365 }
10366 }
10367 p = &TREE_CHAIN (*p);
10368 }
10369
10370 return attributes;
10371 }
10372
10373 /* Apply any attributes which had to be deferred until instantiation
10374 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10375 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10376
10377 static void
10378 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10379 tree args, tsubst_flags_t complain, tree in_decl)
10380 {
10381 tree last_dep = NULL_TREE;
10382 tree t;
10383 tree *p;
10384
10385 if (attributes == NULL_TREE)
10386 return;
10387
10388 if (DECL_P (*decl_p))
10389 {
10390 if (TREE_TYPE (*decl_p) == error_mark_node)
10391 return;
10392 p = &DECL_ATTRIBUTES (*decl_p);
10393 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10394 to our attributes parameter. */
10395 gcc_assert (*p == attributes);
10396 }
10397 else
10398 {
10399 p = &TYPE_ATTRIBUTES (*decl_p);
10400 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10401 lookup_template_class_1, and should be preserved. */
10402 gcc_assert (*p != attributes);
10403 while (*p)
10404 p = &TREE_CHAIN (*p);
10405 }
10406
10407 for (t = attributes; t; t = TREE_CHAIN (t))
10408 if (ATTR_IS_DEPENDENT (t))
10409 {
10410 last_dep = t;
10411 attributes = copy_list (attributes);
10412 break;
10413 }
10414
10415 *p = attributes;
10416 if (last_dep)
10417 {
10418 tree late_attrs = NULL_TREE;
10419 tree *q = &late_attrs;
10420
10421 for (; *p; )
10422 {
10423 t = *p;
10424 if (ATTR_IS_DEPENDENT (t))
10425 {
10426 *p = TREE_CHAIN (t);
10427 TREE_CHAIN (t) = NULL_TREE;
10428 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10429 while (*q)
10430 q = &TREE_CHAIN (*q);
10431 }
10432 else
10433 p = &TREE_CHAIN (t);
10434 }
10435
10436 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10437 }
10438 }
10439
10440 /* Perform (or defer) access check for typedefs that were referenced
10441 from within the template TMPL code.
10442 This is a subroutine of instantiate_decl and instantiate_class_template.
10443 TMPL is the template to consider and TARGS is the list of arguments of
10444 that template. */
10445
10446 static void
10447 perform_typedefs_access_check (tree tmpl, tree targs)
10448 {
10449 location_t saved_location;
10450 unsigned i;
10451 qualified_typedef_usage_t *iter;
10452
10453 if (!tmpl
10454 || (!CLASS_TYPE_P (tmpl)
10455 && TREE_CODE (tmpl) != FUNCTION_DECL))
10456 return;
10457
10458 saved_location = input_location;
10459 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10460 {
10461 tree type_decl = iter->typedef_decl;
10462 tree type_scope = iter->context;
10463
10464 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10465 continue;
10466
10467 if (uses_template_parms (type_decl))
10468 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10469 if (uses_template_parms (type_scope))
10470 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10471
10472 /* Make access check error messages point to the location
10473 of the use of the typedef. */
10474 input_location = iter->locus;
10475 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10476 type_decl, type_decl,
10477 tf_warning_or_error);
10478 }
10479 input_location = saved_location;
10480 }
10481
10482 static tree
10483 instantiate_class_template_1 (tree type)
10484 {
10485 tree templ, args, pattern, t, member;
10486 tree typedecl;
10487 tree pbinfo;
10488 tree base_list;
10489 unsigned int saved_maximum_field_alignment;
10490 tree fn_context;
10491
10492 if (type == error_mark_node)
10493 return error_mark_node;
10494
10495 if (COMPLETE_OR_OPEN_TYPE_P (type)
10496 || uses_template_parms (type))
10497 return type;
10498
10499 /* Figure out which template is being instantiated. */
10500 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10501 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10502
10503 /* Mark the type as in the process of being defined. */
10504 TYPE_BEING_DEFINED (type) = 1;
10505
10506 /* Determine what specialization of the original template to
10507 instantiate. */
10508 t = most_specialized_partial_spec (type, tf_warning_or_error);
10509 if (t == error_mark_node)
10510 return error_mark_node;
10511 else if (t)
10512 {
10513 /* This TYPE is actually an instantiation of a partial
10514 specialization. We replace the innermost set of ARGS with
10515 the arguments appropriate for substitution. For example,
10516 given:
10517
10518 template <class T> struct S {};
10519 template <class T> struct S<T*> {};
10520
10521 and supposing that we are instantiating S<int*>, ARGS will
10522 presently be {int*} -- but we need {int}. */
10523 pattern = TREE_TYPE (t);
10524 args = TREE_PURPOSE (t);
10525 }
10526 else
10527 {
10528 pattern = TREE_TYPE (templ);
10529 args = CLASSTYPE_TI_ARGS (type);
10530 }
10531
10532 /* If the template we're instantiating is incomplete, then clearly
10533 there's nothing we can do. */
10534 if (!COMPLETE_TYPE_P (pattern))
10535 {
10536 /* We can try again later. */
10537 TYPE_BEING_DEFINED (type) = 0;
10538 return type;
10539 }
10540
10541 /* If we've recursively instantiated too many templates, stop. */
10542 if (! push_tinst_level (type))
10543 return type;
10544
10545 /* We may be in the middle of deferred access check. Disable
10546 it now. */
10547 push_deferring_access_checks (dk_no_deferred);
10548
10549 int saved_unevaluated_operand = cp_unevaluated_operand;
10550 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10551
10552 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10553 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10554 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10555 fn_context = error_mark_node;
10556 if (!fn_context)
10557 push_to_top_level ();
10558 else
10559 {
10560 cp_unevaluated_operand = 0;
10561 c_inhibit_evaluation_warnings = 0;
10562 }
10563 /* Use #pragma pack from the template context. */
10564 saved_maximum_field_alignment = maximum_field_alignment;
10565 maximum_field_alignment = TYPE_PRECISION (pattern);
10566
10567 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10568
10569 /* Set the input location to the most specialized template definition.
10570 This is needed if tsubsting causes an error. */
10571 typedecl = TYPE_MAIN_DECL (pattern);
10572 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10573 DECL_SOURCE_LOCATION (typedecl);
10574
10575 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10576 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10577 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10578 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10579 if (ANON_AGGR_TYPE_P (pattern))
10580 SET_ANON_AGGR_TYPE_P (type);
10581 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10582 {
10583 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10584 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10585 /* Adjust visibility for template arguments. */
10586 determine_visibility (TYPE_MAIN_DECL (type));
10587 }
10588 if (CLASS_TYPE_P (type))
10589 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10590
10591 pbinfo = TYPE_BINFO (pattern);
10592
10593 /* We should never instantiate a nested class before its enclosing
10594 class; we need to look up the nested class by name before we can
10595 instantiate it, and that lookup should instantiate the enclosing
10596 class. */
10597 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10598 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10599
10600 base_list = NULL_TREE;
10601 if (BINFO_N_BASE_BINFOS (pbinfo))
10602 {
10603 tree pbase_binfo;
10604 tree pushed_scope;
10605 int i;
10606
10607 /* We must enter the scope containing the type, as that is where
10608 the accessibility of types named in dependent bases are
10609 looked up from. */
10610 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10611
10612 /* Substitute into each of the bases to determine the actual
10613 basetypes. */
10614 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10615 {
10616 tree base;
10617 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10618 tree expanded_bases = NULL_TREE;
10619 int idx, len = 1;
10620
10621 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10622 {
10623 expanded_bases =
10624 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10625 args, tf_error, NULL_TREE);
10626 if (expanded_bases == error_mark_node)
10627 continue;
10628
10629 len = TREE_VEC_LENGTH (expanded_bases);
10630 }
10631
10632 for (idx = 0; idx < len; idx++)
10633 {
10634 if (expanded_bases)
10635 /* Extract the already-expanded base class. */
10636 base = TREE_VEC_ELT (expanded_bases, idx);
10637 else
10638 /* Substitute to figure out the base class. */
10639 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10640 NULL_TREE);
10641
10642 if (base == error_mark_node)
10643 continue;
10644
10645 base_list = tree_cons (access, base, base_list);
10646 if (BINFO_VIRTUAL_P (pbase_binfo))
10647 TREE_TYPE (base_list) = integer_type_node;
10648 }
10649 }
10650
10651 /* The list is now in reverse order; correct that. */
10652 base_list = nreverse (base_list);
10653
10654 if (pushed_scope)
10655 pop_scope (pushed_scope);
10656 }
10657 /* Now call xref_basetypes to set up all the base-class
10658 information. */
10659 xref_basetypes (type, base_list);
10660
10661 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10662 (int) ATTR_FLAG_TYPE_IN_PLACE,
10663 args, tf_error, NULL_TREE);
10664 fixup_attribute_variants (type);
10665
10666 /* Now that our base classes are set up, enter the scope of the
10667 class, so that name lookups into base classes, etc. will work
10668 correctly. This is precisely analogous to what we do in
10669 begin_class_definition when defining an ordinary non-template
10670 class, except we also need to push the enclosing classes. */
10671 push_nested_class (type);
10672
10673 /* Now members are processed in the order of declaration. */
10674 for (member = CLASSTYPE_DECL_LIST (pattern);
10675 member; member = TREE_CHAIN (member))
10676 {
10677 tree t = TREE_VALUE (member);
10678
10679 if (TREE_PURPOSE (member))
10680 {
10681 if (TYPE_P (t))
10682 {
10683 if (LAMBDA_TYPE_P (t))
10684 /* A closure type for a lambda in an NSDMI or default argument.
10685 Ignore it; it will be regenerated when needed. */
10686 continue;
10687
10688 /* Build new CLASSTYPE_NESTED_UTDS. */
10689
10690 tree newtag;
10691 bool class_template_p;
10692
10693 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10694 && TYPE_LANG_SPECIFIC (t)
10695 && CLASSTYPE_IS_TEMPLATE (t));
10696 /* If the member is a class template, then -- even after
10697 substitution -- there may be dependent types in the
10698 template argument list for the class. We increment
10699 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10700 that function will assume that no types are dependent
10701 when outside of a template. */
10702 if (class_template_p)
10703 ++processing_template_decl;
10704 newtag = tsubst (t, args, tf_error, NULL_TREE);
10705 if (class_template_p)
10706 --processing_template_decl;
10707 if (newtag == error_mark_node)
10708 continue;
10709
10710 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10711 {
10712 tree name = TYPE_IDENTIFIER (t);
10713
10714 if (class_template_p)
10715 /* Unfortunately, lookup_template_class sets
10716 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10717 instantiation (i.e., for the type of a member
10718 template class nested within a template class.)
10719 This behavior is required for
10720 maybe_process_partial_specialization to work
10721 correctly, but is not accurate in this case;
10722 the TAG is not an instantiation of anything.
10723 (The corresponding TEMPLATE_DECL is an
10724 instantiation, but the TYPE is not.) */
10725 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10726
10727 /* Now, we call pushtag to put this NEWTAG into the scope of
10728 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10729 pushtag calling push_template_decl. We don't have to do
10730 this for enums because it will already have been done in
10731 tsubst_enum. */
10732 if (name)
10733 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10734 pushtag (name, newtag, /*tag_scope=*/ts_current);
10735 }
10736 }
10737 else if (DECL_DECLARES_FUNCTION_P (t))
10738 {
10739 tree r;
10740
10741 if (TREE_CODE (t) == TEMPLATE_DECL)
10742 ++processing_template_decl;
10743 r = tsubst (t, args, tf_error, NULL_TREE);
10744 if (TREE_CODE (t) == TEMPLATE_DECL)
10745 --processing_template_decl;
10746 set_current_access_from_decl (r);
10747 finish_member_declaration (r);
10748 /* Instantiate members marked with attribute used. */
10749 if (r != error_mark_node && DECL_PRESERVE_P (r))
10750 mark_used (r);
10751 if (TREE_CODE (r) == FUNCTION_DECL
10752 && DECL_OMP_DECLARE_REDUCTION_P (r))
10753 cp_check_omp_declare_reduction (r);
10754 }
10755 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
10756 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10757 /* A closure type for a lambda in an NSDMI or default argument.
10758 Ignore it; it will be regenerated when needed. */;
10759 else
10760 {
10761 /* Build new TYPE_FIELDS. */
10762 if (TREE_CODE (t) == STATIC_ASSERT)
10763 {
10764 tree condition;
10765
10766 ++c_inhibit_evaluation_warnings;
10767 condition =
10768 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10769 tf_warning_or_error, NULL_TREE,
10770 /*integral_constant_expression_p=*/true);
10771 --c_inhibit_evaluation_warnings;
10772
10773 finish_static_assert (condition,
10774 STATIC_ASSERT_MESSAGE (t),
10775 STATIC_ASSERT_SOURCE_LOCATION (t),
10776 /*member_p=*/true);
10777 }
10778 else if (TREE_CODE (t) != CONST_DECL)
10779 {
10780 tree r;
10781 tree vec = NULL_TREE;
10782 int len = 1;
10783
10784 /* The file and line for this declaration, to
10785 assist in error message reporting. Since we
10786 called push_tinst_level above, we don't need to
10787 restore these. */
10788 input_location = DECL_SOURCE_LOCATION (t);
10789
10790 if (TREE_CODE (t) == TEMPLATE_DECL)
10791 ++processing_template_decl;
10792 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10793 if (TREE_CODE (t) == TEMPLATE_DECL)
10794 --processing_template_decl;
10795
10796 if (TREE_CODE (r) == TREE_VEC)
10797 {
10798 /* A capture pack became multiple fields. */
10799 vec = r;
10800 len = TREE_VEC_LENGTH (vec);
10801 }
10802
10803 for (int i = 0; i < len; ++i)
10804 {
10805 if (vec)
10806 r = TREE_VEC_ELT (vec, i);
10807 if (VAR_P (r))
10808 {
10809 /* In [temp.inst]:
10810
10811 [t]he initialization (and any associated
10812 side-effects) of a static data member does
10813 not occur unless the static data member is
10814 itself used in a way that requires the
10815 definition of the static data member to
10816 exist.
10817
10818 Therefore, we do not substitute into the
10819 initialized for the static data member here. */
10820 finish_static_data_member_decl
10821 (r,
10822 /*init=*/NULL_TREE,
10823 /*init_const_expr_p=*/false,
10824 /*asmspec_tree=*/NULL_TREE,
10825 /*flags=*/0);
10826 /* Instantiate members marked with attribute used. */
10827 if (r != error_mark_node && DECL_PRESERVE_P (r))
10828 mark_used (r);
10829 }
10830 else if (TREE_CODE (r) == FIELD_DECL)
10831 {
10832 /* Determine whether R has a valid type and can be
10833 completed later. If R is invalid, then its type
10834 is replaced by error_mark_node. */
10835 tree rtype = TREE_TYPE (r);
10836 if (can_complete_type_without_circularity (rtype))
10837 complete_type (rtype);
10838
10839 if (!complete_or_array_type_p (rtype))
10840 {
10841 /* If R's type couldn't be completed and
10842 it isn't a flexible array member (whose
10843 type is incomplete by definition) give
10844 an error. */
10845 cxx_incomplete_type_error (r, rtype);
10846 TREE_TYPE (r) = error_mark_node;
10847 }
10848 }
10849
10850 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10851 such a thing will already have been added to the field
10852 list by tsubst_enum in finish_member_declaration in the
10853 CLASSTYPE_NESTED_UTDS case above. */
10854 if (!(TREE_CODE (r) == TYPE_DECL
10855 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10856 && DECL_ARTIFICIAL (r)))
10857 {
10858 set_current_access_from_decl (r);
10859 finish_member_declaration (r);
10860 }
10861 }
10862 }
10863 }
10864 }
10865 else
10866 {
10867 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10868 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10869 {
10870 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10871
10872 tree friend_type = t;
10873 bool adjust_processing_template_decl = false;
10874
10875 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10876 {
10877 /* template <class T> friend class C; */
10878 friend_type = tsubst_friend_class (friend_type, args);
10879 adjust_processing_template_decl = true;
10880 }
10881 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10882 {
10883 /* template <class T> friend class C::D; */
10884 friend_type = tsubst (friend_type, args,
10885 tf_warning_or_error, NULL_TREE);
10886 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10887 friend_type = TREE_TYPE (friend_type);
10888 adjust_processing_template_decl = true;
10889 }
10890 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10891 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10892 {
10893 /* This could be either
10894
10895 friend class T::C;
10896
10897 when dependent_type_p is false or
10898
10899 template <class U> friend class T::C;
10900
10901 otherwise. */
10902 /* Bump processing_template_decl in case this is something like
10903 template <class T> friend struct A<T>::B. */
10904 ++processing_template_decl;
10905 friend_type = tsubst (friend_type, args,
10906 tf_warning_or_error, NULL_TREE);
10907 if (dependent_type_p (friend_type))
10908 adjust_processing_template_decl = true;
10909 --processing_template_decl;
10910 }
10911 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10912 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10913 && TYPE_HIDDEN_P (friend_type))
10914 {
10915 /* friend class C;
10916
10917 where C hasn't been declared yet. Let's lookup name
10918 from namespace scope directly, bypassing any name that
10919 come from dependent base class. */
10920 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10921
10922 /* The call to xref_tag_from_type does injection for friend
10923 classes. */
10924 push_nested_namespace (ns);
10925 friend_type =
10926 xref_tag_from_type (friend_type, NULL_TREE,
10927 /*tag_scope=*/ts_current);
10928 pop_nested_namespace (ns);
10929 }
10930 else if (uses_template_parms (friend_type))
10931 /* friend class C<T>; */
10932 friend_type = tsubst (friend_type, args,
10933 tf_warning_or_error, NULL_TREE);
10934 /* Otherwise it's
10935
10936 friend class C;
10937
10938 where C is already declared or
10939
10940 friend class C<int>;
10941
10942 We don't have to do anything in these cases. */
10943
10944 if (adjust_processing_template_decl)
10945 /* Trick make_friend_class into realizing that the friend
10946 we're adding is a template, not an ordinary class. It's
10947 important that we use make_friend_class since it will
10948 perform some error-checking and output cross-reference
10949 information. */
10950 ++processing_template_decl;
10951
10952 if (friend_type != error_mark_node)
10953 make_friend_class (type, friend_type, /*complain=*/false);
10954
10955 if (adjust_processing_template_decl)
10956 --processing_template_decl;
10957 }
10958 else
10959 {
10960 /* Build new DECL_FRIENDLIST. */
10961 tree r;
10962
10963 /* The file and line for this declaration, to
10964 assist in error message reporting. Since we
10965 called push_tinst_level above, we don't need to
10966 restore these. */
10967 input_location = DECL_SOURCE_LOCATION (t);
10968
10969 if (TREE_CODE (t) == TEMPLATE_DECL)
10970 {
10971 ++processing_template_decl;
10972 push_deferring_access_checks (dk_no_check);
10973 }
10974
10975 r = tsubst_friend_function (t, args);
10976 add_friend (type, r, /*complain=*/false);
10977 if (TREE_CODE (t) == TEMPLATE_DECL)
10978 {
10979 pop_deferring_access_checks ();
10980 --processing_template_decl;
10981 }
10982 }
10983 }
10984 }
10985
10986 if (fn_context)
10987 {
10988 /* Restore these before substituting into the lambda capture
10989 initializers. */
10990 cp_unevaluated_operand = saved_unevaluated_operand;
10991 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10992 }
10993
10994 /* Set the file and line number information to whatever is given for
10995 the class itself. This puts error messages involving generated
10996 implicit functions at a predictable point, and the same point
10997 that would be used for non-template classes. */
10998 input_location = DECL_SOURCE_LOCATION (typedecl);
10999
11000 unreverse_member_declarations (type);
11001 finish_struct_1 (type);
11002 TYPE_BEING_DEFINED (type) = 0;
11003
11004 /* We don't instantiate default arguments for member functions. 14.7.1:
11005
11006 The implicit instantiation of a class template specialization causes
11007 the implicit instantiation of the declarations, but not of the
11008 definitions or default arguments, of the class member functions,
11009 member classes, static data members and member templates.... */
11010
11011 /* Some typedefs referenced from within the template code need to be access
11012 checked at template instantiation time, i.e now. These types were
11013 added to the template at parsing time. Let's get those and perform
11014 the access checks then. */
11015 perform_typedefs_access_check (pattern, args);
11016 perform_deferred_access_checks (tf_warning_or_error);
11017 pop_nested_class ();
11018 maximum_field_alignment = saved_maximum_field_alignment;
11019 if (!fn_context)
11020 pop_from_top_level ();
11021 pop_deferring_access_checks ();
11022 pop_tinst_level ();
11023
11024 /* The vtable for a template class can be emitted in any translation
11025 unit in which the class is instantiated. When there is no key
11026 method, however, finish_struct_1 will already have added TYPE to
11027 the keyed_classes. */
11028 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11029 vec_safe_push (keyed_classes, type);
11030
11031 return type;
11032 }
11033
11034 /* Wrapper for instantiate_class_template_1. */
11035
11036 tree
11037 instantiate_class_template (tree type)
11038 {
11039 tree ret;
11040 timevar_push (TV_TEMPLATE_INST);
11041 ret = instantiate_class_template_1 (type);
11042 timevar_pop (TV_TEMPLATE_INST);
11043 return ret;
11044 }
11045
11046 static tree
11047 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11048 {
11049 tree r;
11050
11051 if (!t)
11052 r = t;
11053 else if (TYPE_P (t))
11054 r = tsubst (t, args, complain, in_decl);
11055 else
11056 {
11057 if (!(complain & tf_warning))
11058 ++c_inhibit_evaluation_warnings;
11059 r = tsubst_expr (t, args, complain, in_decl,
11060 /*integral_constant_expression_p=*/true);
11061 if (!(complain & tf_warning))
11062 --c_inhibit_evaluation_warnings;
11063 }
11064 return r;
11065 }
11066
11067 /* Given a function parameter pack TMPL_PARM and some function parameters
11068 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11069 and set *SPEC_P to point at the next point in the list. */
11070
11071 tree
11072 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11073 {
11074 /* Collect all of the extra "packed" parameters into an
11075 argument pack. */
11076 tree parmvec;
11077 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11078 tree spec_parm = *spec_p;
11079 int i, len;
11080
11081 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11082 if (tmpl_parm
11083 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11084 break;
11085
11086 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11087 parmvec = make_tree_vec (len);
11088 spec_parm = *spec_p;
11089 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11090 {
11091 tree elt = spec_parm;
11092 if (DECL_PACK_P (elt))
11093 elt = make_pack_expansion (elt);
11094 TREE_VEC_ELT (parmvec, i) = elt;
11095 }
11096
11097 /* Build the argument packs. */
11098 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11099 *spec_p = spec_parm;
11100
11101 return argpack;
11102 }
11103
11104 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11105 NONTYPE_ARGUMENT_PACK. */
11106
11107 static tree
11108 make_fnparm_pack (tree spec_parm)
11109 {
11110 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11111 }
11112
11113 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11114 pack expansion with no extra args, 2 if it has extra args, or 0
11115 if it is not a pack expansion. */
11116
11117 static int
11118 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11119 {
11120 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11121 if (i >= TREE_VEC_LENGTH (vec))
11122 return 0;
11123 tree elt = TREE_VEC_ELT (vec, i);
11124 if (DECL_P (elt))
11125 /* A decl pack is itself an expansion. */
11126 elt = TREE_TYPE (elt);
11127 if (!PACK_EXPANSION_P (elt))
11128 return 0;
11129 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11130 return 2;
11131 return 1;
11132 }
11133
11134
11135 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11136
11137 static tree
11138 make_argument_pack_select (tree arg_pack, unsigned index)
11139 {
11140 tree aps = make_node (ARGUMENT_PACK_SELECT);
11141
11142 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11143 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11144
11145 return aps;
11146 }
11147
11148 /* This is a subroutine of tsubst_pack_expansion.
11149
11150 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11151 mechanism to store the (non complete list of) arguments of the
11152 substitution and return a non substituted pack expansion, in order
11153 to wait for when we have enough arguments to really perform the
11154 substitution. */
11155
11156 static bool
11157 use_pack_expansion_extra_args_p (tree parm_packs,
11158 int arg_pack_len,
11159 bool has_empty_arg)
11160 {
11161 /* If one pack has an expansion and another pack has a normal
11162 argument or if one pack has an empty argument and an another
11163 one hasn't then tsubst_pack_expansion cannot perform the
11164 substitution and need to fall back on the
11165 PACK_EXPANSION_EXTRA mechanism. */
11166 if (parm_packs == NULL_TREE)
11167 return false;
11168 else if (has_empty_arg)
11169 return true;
11170
11171 bool has_expansion_arg = false;
11172 for (int i = 0 ; i < arg_pack_len; ++i)
11173 {
11174 bool has_non_expansion_arg = false;
11175 for (tree parm_pack = parm_packs;
11176 parm_pack;
11177 parm_pack = TREE_CHAIN (parm_pack))
11178 {
11179 tree arg = TREE_VALUE (parm_pack);
11180
11181 int exp = argument_pack_element_is_expansion_p (arg, i);
11182 if (exp == 2)
11183 /* We can't substitute a pack expansion with extra args into
11184 our pattern. */
11185 return true;
11186 else if (exp)
11187 has_expansion_arg = true;
11188 else
11189 has_non_expansion_arg = true;
11190 }
11191
11192 if (has_expansion_arg && has_non_expansion_arg)
11193 return true;
11194 }
11195 return false;
11196 }
11197
11198 /* [temp.variadic]/6 says that:
11199
11200 The instantiation of a pack expansion [...]
11201 produces a list E1,E2, ..., En, where N is the number of elements
11202 in the pack expansion parameters.
11203
11204 This subroutine of tsubst_pack_expansion produces one of these Ei.
11205
11206 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11207 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11208 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11209 INDEX is the index 'i' of the element Ei to produce. ARGS,
11210 COMPLAIN, and IN_DECL are the same parameters as for the
11211 tsubst_pack_expansion function.
11212
11213 The function returns the resulting Ei upon successful completion,
11214 or error_mark_node.
11215
11216 Note that this function possibly modifies the ARGS parameter, so
11217 it's the responsibility of the caller to restore it. */
11218
11219 static tree
11220 gen_elem_of_pack_expansion_instantiation (tree pattern,
11221 tree parm_packs,
11222 unsigned index,
11223 tree args /* This parm gets
11224 modified. */,
11225 tsubst_flags_t complain,
11226 tree in_decl)
11227 {
11228 tree t;
11229 bool ith_elem_is_expansion = false;
11230
11231 /* For each parameter pack, change the substitution of the parameter
11232 pack to the ith argument in its argument pack, then expand the
11233 pattern. */
11234 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11235 {
11236 tree parm = TREE_PURPOSE (pack);
11237 tree arg_pack = TREE_VALUE (pack);
11238 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11239
11240 ith_elem_is_expansion |=
11241 argument_pack_element_is_expansion_p (arg_pack, index);
11242
11243 /* Select the Ith argument from the pack. */
11244 if (TREE_CODE (parm) == PARM_DECL
11245 || VAR_P (parm)
11246 || TREE_CODE (parm) == FIELD_DECL)
11247 {
11248 if (index == 0)
11249 {
11250 aps = make_argument_pack_select (arg_pack, index);
11251 if (!mark_used (parm, complain) && !(complain & tf_error))
11252 return error_mark_node;
11253 register_local_specialization (aps, parm);
11254 }
11255 else
11256 aps = retrieve_local_specialization (parm);
11257 }
11258 else
11259 {
11260 int idx, level;
11261 template_parm_level_and_index (parm, &level, &idx);
11262
11263 if (index == 0)
11264 {
11265 aps = make_argument_pack_select (arg_pack, index);
11266 /* Update the corresponding argument. */
11267 TMPL_ARG (args, level, idx) = aps;
11268 }
11269 else
11270 /* Re-use the ARGUMENT_PACK_SELECT. */
11271 aps = TMPL_ARG (args, level, idx);
11272 }
11273 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11274 }
11275
11276 /* Substitute into the PATTERN with the (possibly altered)
11277 arguments. */
11278 if (pattern == in_decl)
11279 /* Expanding a fixed parameter pack from
11280 coerce_template_parameter_pack. */
11281 t = tsubst_decl (pattern, args, complain);
11282 else if (pattern == error_mark_node)
11283 t = error_mark_node;
11284 else if (constraint_p (pattern))
11285 {
11286 if (processing_template_decl)
11287 t = tsubst_constraint (pattern, args, complain, in_decl);
11288 else
11289 t = (constraints_satisfied_p (pattern, args)
11290 ? boolean_true_node : boolean_false_node);
11291 }
11292 else if (!TYPE_P (pattern))
11293 t = tsubst_expr (pattern, args, complain, in_decl,
11294 /*integral_constant_expression_p=*/false);
11295 else
11296 t = tsubst (pattern, args, complain, in_decl);
11297
11298 /* If the Ith argument pack element is a pack expansion, then
11299 the Ith element resulting from the substituting is going to
11300 be a pack expansion as well. */
11301 if (ith_elem_is_expansion)
11302 t = make_pack_expansion (t, complain);
11303
11304 return t;
11305 }
11306
11307 /* When the unexpanded parameter pack in a fold expression expands to an empty
11308 sequence, the value of the expression is as follows; the program is
11309 ill-formed if the operator is not listed in this table.
11310
11311 && true
11312 || false
11313 , void() */
11314
11315 tree
11316 expand_empty_fold (tree t, tsubst_flags_t complain)
11317 {
11318 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11319 if (!FOLD_EXPR_MODIFY_P (t))
11320 switch (code)
11321 {
11322 case TRUTH_ANDIF_EXPR:
11323 return boolean_true_node;
11324 case TRUTH_ORIF_EXPR:
11325 return boolean_false_node;
11326 case COMPOUND_EXPR:
11327 return void_node;
11328 default:
11329 break;
11330 }
11331
11332 if (complain & tf_error)
11333 error_at (location_of (t),
11334 "fold of empty expansion over %O", code);
11335 return error_mark_node;
11336 }
11337
11338 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11339 form an expression that combines the two terms using the
11340 operator of T. */
11341
11342 static tree
11343 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11344 {
11345 tree op = FOLD_EXPR_OP (t);
11346 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11347
11348 // Handle compound assignment operators.
11349 if (FOLD_EXPR_MODIFY_P (t))
11350 return build_x_modify_expr (input_location, left, code, right, complain);
11351
11352 switch (code)
11353 {
11354 case COMPOUND_EXPR:
11355 return build_x_compound_expr (input_location, left, right, complain);
11356 case DOTSTAR_EXPR:
11357 return build_m_component_ref (left, right, complain);
11358 default:
11359 return build_x_binary_op (input_location, code,
11360 left, TREE_CODE (left),
11361 right, TREE_CODE (right),
11362 /*overload=*/NULL,
11363 complain);
11364 }
11365 }
11366
11367 /* Substitute ARGS into the pack of a fold expression T. */
11368
11369 static inline tree
11370 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11371 {
11372 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11373 }
11374
11375 /* Substitute ARGS into the pack of a fold expression T. */
11376
11377 static inline tree
11378 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11379 {
11380 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11381 }
11382
11383 /* Expand a PACK of arguments into a grouped as left fold.
11384 Given a pack containing elements A0, A1, ..., An and an
11385 operator @, this builds the expression:
11386
11387 ((A0 @ A1) @ A2) ... @ An
11388
11389 Note that PACK must not be empty.
11390
11391 The operator is defined by the original fold expression T. */
11392
11393 static tree
11394 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11395 {
11396 tree left = TREE_VEC_ELT (pack, 0);
11397 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11398 {
11399 tree right = TREE_VEC_ELT (pack, i);
11400 left = fold_expression (t, left, right, complain);
11401 }
11402 return left;
11403 }
11404
11405 /* Substitute into a unary left fold expression. */
11406
11407 static tree
11408 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11409 tree in_decl)
11410 {
11411 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11412 if (pack == error_mark_node)
11413 return error_mark_node;
11414 if (PACK_EXPANSION_P (pack))
11415 {
11416 tree r = copy_node (t);
11417 FOLD_EXPR_PACK (r) = pack;
11418 return r;
11419 }
11420 if (TREE_VEC_LENGTH (pack) == 0)
11421 return expand_empty_fold (t, complain);
11422 else
11423 return expand_left_fold (t, pack, complain);
11424 }
11425
11426 /* Substitute into a binary left fold expression.
11427
11428 Do ths by building a single (non-empty) vector of argumnts and
11429 building the expression from those elements. */
11430
11431 static tree
11432 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11433 tree in_decl)
11434 {
11435 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11436 if (pack == error_mark_node)
11437 return error_mark_node;
11438 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11439 if (init == error_mark_node)
11440 return error_mark_node;
11441
11442 if (PACK_EXPANSION_P (pack))
11443 {
11444 tree r = copy_node (t);
11445 FOLD_EXPR_PACK (r) = pack;
11446 FOLD_EXPR_INIT (r) = init;
11447 return r;
11448 }
11449
11450 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11451 TREE_VEC_ELT (vec, 0) = init;
11452 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11453 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11454
11455 return expand_left_fold (t, vec, complain);
11456 }
11457
11458 /* Expand a PACK of arguments into a grouped as right fold.
11459 Given a pack containing elementns A0, A1, ..., and an
11460 operator @, this builds the expression:
11461
11462 A0@ ... (An-2 @ (An-1 @ An))
11463
11464 Note that PACK must not be empty.
11465
11466 The operator is defined by the original fold expression T. */
11467
11468 tree
11469 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11470 {
11471 // Build the expression.
11472 int n = TREE_VEC_LENGTH (pack);
11473 tree right = TREE_VEC_ELT (pack, n - 1);
11474 for (--n; n != 0; --n)
11475 {
11476 tree left = TREE_VEC_ELT (pack, n - 1);
11477 right = fold_expression (t, left, right, complain);
11478 }
11479 return right;
11480 }
11481
11482 /* Substitute into a unary right fold expression. */
11483
11484 static tree
11485 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11486 tree in_decl)
11487 {
11488 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11489 if (pack == error_mark_node)
11490 return error_mark_node;
11491 if (PACK_EXPANSION_P (pack))
11492 {
11493 tree r = copy_node (t);
11494 FOLD_EXPR_PACK (r) = pack;
11495 return r;
11496 }
11497 if (TREE_VEC_LENGTH (pack) == 0)
11498 return expand_empty_fold (t, complain);
11499 else
11500 return expand_right_fold (t, pack, complain);
11501 }
11502
11503 /* Substitute into a binary right fold expression.
11504
11505 Do ths by building a single (non-empty) vector of arguments and
11506 building the expression from those elements. */
11507
11508 static tree
11509 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11510 tree in_decl)
11511 {
11512 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11513 if (pack == error_mark_node)
11514 return error_mark_node;
11515 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11516 if (init == error_mark_node)
11517 return error_mark_node;
11518
11519 if (PACK_EXPANSION_P (pack))
11520 {
11521 tree r = copy_node (t);
11522 FOLD_EXPR_PACK (r) = pack;
11523 FOLD_EXPR_INIT (r) = init;
11524 return r;
11525 }
11526
11527 int n = TREE_VEC_LENGTH (pack);
11528 tree vec = make_tree_vec (n + 1);
11529 for (int i = 0; i < n; ++i)
11530 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11531 TREE_VEC_ELT (vec, n) = init;
11532
11533 return expand_right_fold (t, vec, complain);
11534 }
11535
11536 /* Walk through the pattern of a pack expansion, adding everything in
11537 local_specializations to a list. */
11538
11539 struct el_data
11540 {
11541 tree extra;
11542 tsubst_flags_t complain;
11543 };
11544 static tree
11545 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11546 {
11547 el_data &data = *reinterpret_cast<el_data*>(data_);
11548 tree *extra = &data.extra;
11549 tsubst_flags_t complain = data.complain;
11550 if (tree spec = retrieve_local_specialization (*tp))
11551 {
11552 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11553 {
11554 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11555 tree args = ARGUMENT_PACK_ARGS (spec);
11556 if (TREE_VEC_LENGTH (args) == 1)
11557 {
11558 tree elt = TREE_VEC_ELT (args, 0);
11559 if (PACK_EXPANSION_P (elt))
11560 elt = PACK_EXPANSION_PATTERN (elt);
11561 if (DECL_PACK_P (elt))
11562 spec = elt;
11563 }
11564 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11565 {
11566 /* Handle lambda capture here, since we aren't doing any
11567 substitution now, and so tsubst_copy won't call
11568 process_outer_var_ref. */
11569 tree args = ARGUMENT_PACK_ARGS (spec);
11570 int len = TREE_VEC_LENGTH (args);
11571 for (int i = 0; i < len; ++i)
11572 {
11573 tree arg = TREE_VEC_ELT (args, i);
11574 tree carg = arg;
11575 if (outer_automatic_var_p (arg))
11576 carg = process_outer_var_ref (arg, complain);
11577 if (carg != arg)
11578 {
11579 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11580 proxies. */
11581 if (i == 0)
11582 {
11583 spec = copy_node (spec);
11584 args = copy_node (args);
11585 SET_ARGUMENT_PACK_ARGS (spec, args);
11586 register_local_specialization (spec, *tp);
11587 }
11588 TREE_VEC_ELT (args, i) = carg;
11589 }
11590 }
11591 }
11592 }
11593 if (outer_automatic_var_p (spec))
11594 spec = process_outer_var_ref (spec, complain);
11595 *extra = tree_cons (*tp, spec, *extra);
11596 }
11597 return NULL_TREE;
11598 }
11599 static tree
11600 extract_local_specs (tree pattern, tsubst_flags_t complain)
11601 {
11602 el_data data = { NULL_TREE, complain };
11603 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
11604 return data.extra;
11605 }
11606
11607 /* Substitute ARGS into T, which is an pack expansion
11608 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11609 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11610 (if only a partial substitution could be performed) or
11611 ERROR_MARK_NODE if there was an error. */
11612 tree
11613 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11614 tree in_decl)
11615 {
11616 tree pattern;
11617 tree pack, packs = NULL_TREE;
11618 bool unsubstituted_packs = false;
11619 bool unsubstituted_fn_pack = false;
11620 int i, len = -1;
11621 tree result;
11622 hash_map<tree, tree> *saved_local_specializations = NULL;
11623 bool need_local_specializations = false;
11624 int levels;
11625
11626 gcc_assert (PACK_EXPANSION_P (t));
11627 pattern = PACK_EXPANSION_PATTERN (t);
11628
11629 /* Add in any args remembered from an earlier partial instantiation. */
11630 tree extra = PACK_EXPANSION_EXTRA_ARGS (t);
11631 if (extra && TREE_CODE (extra) == TREE_LIST)
11632 {
11633 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
11634 {
11635 /* The partial instantiation involved local declarations collected in
11636 extract_local_specs; map from the general template to our local
11637 context. */
11638 tree gen = TREE_PURPOSE (elt);
11639 tree inst = TREE_VALUE (elt);
11640 if (DECL_P (inst))
11641 if (tree local = retrieve_local_specialization (inst))
11642 inst = local;
11643 /* else inst is already a full instantiation of the pack. */
11644 register_local_specialization (inst, gen);
11645 }
11646 gcc_assert (!TREE_PURPOSE (extra));
11647 extra = TREE_VALUE (extra);
11648 }
11649 args = add_to_template_args (extra, args);
11650
11651 levels = TMPL_ARGS_DEPTH (args);
11652
11653 /* Determine the argument packs that will instantiate the parameter
11654 packs used in the expansion expression. While we're at it,
11655 compute the number of arguments to be expanded and make sure it
11656 is consistent. */
11657 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11658 pack = TREE_CHAIN (pack))
11659 {
11660 tree parm_pack = TREE_VALUE (pack);
11661 tree arg_pack = NULL_TREE;
11662 tree orig_arg = NULL_TREE;
11663 int level = 0;
11664
11665 if (TREE_CODE (parm_pack) == BASES)
11666 {
11667 gcc_assert (parm_pack == pattern);
11668 if (BASES_DIRECT (parm_pack))
11669 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11670 args, complain, in_decl, false));
11671 else
11672 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11673 args, complain, in_decl, false));
11674 }
11675 else if (builtin_pack_call_p (parm_pack))
11676 {
11677 /* ??? Support use in other patterns. */
11678 gcc_assert (parm_pack == pattern);
11679 return expand_builtin_pack_call (parm_pack, args,
11680 complain, in_decl);
11681 }
11682 else if (TREE_CODE (parm_pack) == PARM_DECL)
11683 {
11684 /* We know we have correct local_specializations if this
11685 expansion is at function scope, or if we're dealing with a
11686 local parameter in a requires expression; for the latter,
11687 tsubst_requires_expr set it up appropriately. */
11688 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11689 arg_pack = retrieve_local_specialization (parm_pack);
11690 else
11691 /* We can't rely on local_specializations for a parameter
11692 name used later in a function declaration (such as in a
11693 late-specified return type). Even if it exists, it might
11694 have the wrong value for a recursive call. */
11695 need_local_specializations = true;
11696
11697 if (!arg_pack)
11698 {
11699 /* This parameter pack was used in an unevaluated context. Just
11700 make a dummy decl, since it's only used for its type. */
11701 arg_pack = tsubst_decl (parm_pack, args, complain);
11702 if (arg_pack && DECL_PACK_P (arg_pack))
11703 /* Partial instantiation of the parm_pack, we can't build
11704 up an argument pack yet. */
11705 arg_pack = NULL_TREE;
11706 else
11707 arg_pack = make_fnparm_pack (arg_pack);
11708 }
11709 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
11710 /* This argument pack isn't fully instantiated yet. We set this
11711 flag rather than clear arg_pack because we do want to do the
11712 optimization below, and we don't want to substitute directly
11713 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11714 where it isn't expected). */
11715 unsubstituted_fn_pack = true;
11716 }
11717 else if (is_normal_capture_proxy (parm_pack))
11718 {
11719 arg_pack = retrieve_local_specialization (parm_pack);
11720 if (argument_pack_element_is_expansion_p (arg_pack, 0))
11721 unsubstituted_fn_pack = true;
11722 }
11723 else
11724 {
11725 int idx;
11726 template_parm_level_and_index (parm_pack, &level, &idx);
11727
11728 if (level <= levels)
11729 arg_pack = TMPL_ARG (args, level, idx);
11730 }
11731
11732 orig_arg = arg_pack;
11733 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11734 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11735
11736 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11737 /* This can only happen if we forget to expand an argument
11738 pack somewhere else. Just return an error, silently. */
11739 {
11740 result = make_tree_vec (1);
11741 TREE_VEC_ELT (result, 0) = error_mark_node;
11742 return result;
11743 }
11744
11745 if (arg_pack)
11746 {
11747 int my_len =
11748 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11749
11750 /* Don't bother trying to do a partial substitution with
11751 incomplete packs; we'll try again after deduction. */
11752 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11753 return t;
11754
11755 if (len < 0)
11756 len = my_len;
11757 else if (len != my_len
11758 && !unsubstituted_fn_pack)
11759 {
11760 if (!(complain & tf_error))
11761 /* Fail quietly. */;
11762 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11763 error ("mismatched argument pack lengths while expanding %qT",
11764 pattern);
11765 else
11766 error ("mismatched argument pack lengths while expanding %qE",
11767 pattern);
11768 return error_mark_node;
11769 }
11770
11771 /* Keep track of the parameter packs and their corresponding
11772 argument packs. */
11773 packs = tree_cons (parm_pack, arg_pack, packs);
11774 TREE_TYPE (packs) = orig_arg;
11775 }
11776 else
11777 {
11778 /* We can't substitute for this parameter pack. We use a flag as
11779 well as the missing_level counter because function parameter
11780 packs don't have a level. */
11781 gcc_assert (processing_template_decl);
11782 unsubstituted_packs = true;
11783 }
11784 }
11785
11786 /* If the expansion is just T..., return the matching argument pack, unless
11787 we need to call convert_from_reference on all the elements. This is an
11788 important optimization; see c++/68422. */
11789 if (!unsubstituted_packs
11790 && TREE_PURPOSE (packs) == pattern)
11791 {
11792 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11793
11794 /* If the argument pack is a single pack expansion, pull it out. */
11795 if (TREE_VEC_LENGTH (args) == 1
11796 && pack_expansion_args_count (args))
11797 return TREE_VEC_ELT (args, 0);
11798
11799 /* Types need no adjustment, nor does sizeof..., and if we still have
11800 some pack expansion args we won't do anything yet. */
11801 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11802 || PACK_EXPANSION_SIZEOF_P (t)
11803 || pack_expansion_args_count (args))
11804 return args;
11805 /* Also optimize expression pack expansions if we can tell that the
11806 elements won't have reference type. */
11807 tree type = TREE_TYPE (pattern);
11808 if (type && TREE_CODE (type) != REFERENCE_TYPE
11809 && !PACK_EXPANSION_P (type)
11810 && !WILDCARD_TYPE_P (type))
11811 return args;
11812 /* Otherwise use the normal path so we get convert_from_reference. */
11813 }
11814
11815 /* We cannot expand this expansion expression, because we don't have
11816 all of the argument packs we need. */
11817 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11818 {
11819 /* We got some full packs, but we can't substitute them in until we
11820 have values for all the packs. So remember these until then. */
11821
11822 t = make_pack_expansion (pattern, complain);
11823 tree extra = args;
11824 if (local_specializations)
11825 if (tree locals = extract_local_specs (pattern, complain))
11826 extra = tree_cons (NULL_TREE, extra, locals);
11827 PACK_EXPANSION_EXTRA_ARGS (t) = extra;
11828 return t;
11829 }
11830 else if (unsubstituted_packs)
11831 {
11832 /* There were no real arguments, we're just replacing a parameter
11833 pack with another version of itself. Substitute into the
11834 pattern and return a PACK_EXPANSION_*. The caller will need to
11835 deal with that. */
11836 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11837 t = tsubst_expr (pattern, args, complain, in_decl,
11838 /*integral_constant_expression_p=*/false);
11839 else
11840 t = tsubst (pattern, args, complain, in_decl);
11841 t = make_pack_expansion (t, complain);
11842 return t;
11843 }
11844
11845 gcc_assert (len >= 0);
11846
11847 if (need_local_specializations)
11848 {
11849 /* We're in a late-specified return type, so create our own local
11850 specializations map; the current map is either NULL or (in the
11851 case of recursive unification) might have bindings that we don't
11852 want to use or alter. */
11853 saved_local_specializations = local_specializations;
11854 local_specializations = new hash_map<tree, tree>;
11855 }
11856
11857 /* For each argument in each argument pack, substitute into the
11858 pattern. */
11859 result = make_tree_vec (len);
11860 tree elem_args = copy_template_args (args);
11861 for (i = 0; i < len; ++i)
11862 {
11863 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11864 i,
11865 elem_args, complain,
11866 in_decl);
11867 TREE_VEC_ELT (result, i) = t;
11868 if (t == error_mark_node)
11869 {
11870 result = error_mark_node;
11871 break;
11872 }
11873 }
11874
11875 /* Update ARGS to restore the substitution from parameter packs to
11876 their argument packs. */
11877 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11878 {
11879 tree parm = TREE_PURPOSE (pack);
11880
11881 if (TREE_CODE (parm) == PARM_DECL
11882 || VAR_P (parm)
11883 || TREE_CODE (parm) == FIELD_DECL)
11884 register_local_specialization (TREE_TYPE (pack), parm);
11885 else
11886 {
11887 int idx, level;
11888
11889 if (TREE_VALUE (pack) == NULL_TREE)
11890 continue;
11891
11892 template_parm_level_and_index (parm, &level, &idx);
11893
11894 /* Update the corresponding argument. */
11895 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11896 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11897 TREE_TYPE (pack);
11898 else
11899 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11900 }
11901 }
11902
11903 if (need_local_specializations)
11904 {
11905 delete local_specializations;
11906 local_specializations = saved_local_specializations;
11907 }
11908
11909 /* If the dependent pack arguments were such that we end up with only a
11910 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11911 if (len == 1 && TREE_CODE (result) == TREE_VEC
11912 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11913 return TREE_VEC_ELT (result, 0);
11914
11915 return result;
11916 }
11917
11918 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11919 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11920 parameter packs; all parms generated from a function parameter pack will
11921 have the same DECL_PARM_INDEX. */
11922
11923 tree
11924 get_pattern_parm (tree parm, tree tmpl)
11925 {
11926 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11927 tree patparm;
11928
11929 if (DECL_ARTIFICIAL (parm))
11930 {
11931 for (patparm = DECL_ARGUMENTS (pattern);
11932 patparm; patparm = DECL_CHAIN (patparm))
11933 if (DECL_ARTIFICIAL (patparm)
11934 && DECL_NAME (parm) == DECL_NAME (patparm))
11935 break;
11936 }
11937 else
11938 {
11939 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11940 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11941 gcc_assert (DECL_PARM_INDEX (patparm)
11942 == DECL_PARM_INDEX (parm));
11943 }
11944
11945 return patparm;
11946 }
11947
11948 /* Make an argument pack out of the TREE_VEC VEC. */
11949
11950 static tree
11951 make_argument_pack (tree vec)
11952 {
11953 tree pack;
11954 tree elt = TREE_VEC_ELT (vec, 0);
11955 if (TYPE_P (elt))
11956 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11957 else
11958 {
11959 pack = make_node (NONTYPE_ARGUMENT_PACK);
11960 TREE_CONSTANT (pack) = 1;
11961 }
11962 SET_ARGUMENT_PACK_ARGS (pack, vec);
11963 return pack;
11964 }
11965
11966 /* Return an exact copy of template args T that can be modified
11967 independently. */
11968
11969 static tree
11970 copy_template_args (tree t)
11971 {
11972 if (t == error_mark_node)
11973 return t;
11974
11975 int len = TREE_VEC_LENGTH (t);
11976 tree new_vec = make_tree_vec (len);
11977
11978 for (int i = 0; i < len; ++i)
11979 {
11980 tree elt = TREE_VEC_ELT (t, i);
11981 if (elt && TREE_CODE (elt) == TREE_VEC)
11982 elt = copy_template_args (elt);
11983 TREE_VEC_ELT (new_vec, i) = elt;
11984 }
11985
11986 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11987 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11988
11989 return new_vec;
11990 }
11991
11992 /* Substitute ARGS into the vector or list of template arguments T. */
11993
11994 static tree
11995 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11996 {
11997 tree orig_t = t;
11998 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11999 tree *elts;
12000
12001 if (t == error_mark_node)
12002 return error_mark_node;
12003
12004 len = TREE_VEC_LENGTH (t);
12005 elts = XALLOCAVEC (tree, len);
12006
12007 for (i = 0; i < len; i++)
12008 {
12009 tree orig_arg = TREE_VEC_ELT (t, i);
12010 tree new_arg;
12011
12012 if (TREE_CODE (orig_arg) == TREE_VEC)
12013 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12014 else if (PACK_EXPANSION_P (orig_arg))
12015 {
12016 /* Substitute into an expansion expression. */
12017 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12018
12019 if (TREE_CODE (new_arg) == TREE_VEC)
12020 /* Add to the expanded length adjustment the number of
12021 expanded arguments. We subtract one from this
12022 measurement, because the argument pack expression
12023 itself is already counted as 1 in
12024 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12025 the argument pack is empty. */
12026 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12027 }
12028 else if (ARGUMENT_PACK_P (orig_arg))
12029 {
12030 /* Substitute into each of the arguments. */
12031 new_arg = TYPE_P (orig_arg)
12032 ? cxx_make_type (TREE_CODE (orig_arg))
12033 : make_node (TREE_CODE (orig_arg));
12034
12035 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12036 args, complain, in_decl);
12037 if (pack_args == error_mark_node)
12038 new_arg = error_mark_node;
12039 else
12040 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12041
12042 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12043 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12044 }
12045 else
12046 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12047
12048 if (new_arg == error_mark_node)
12049 return error_mark_node;
12050
12051 elts[i] = new_arg;
12052 if (new_arg != orig_arg)
12053 need_new = 1;
12054 }
12055
12056 if (!need_new)
12057 return t;
12058
12059 /* Make space for the expanded arguments coming from template
12060 argument packs. */
12061 t = make_tree_vec (len + expanded_len_adjust);
12062 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12063 arguments for a member template.
12064 In that case each TREE_VEC in ORIG_T represents a level of template
12065 arguments, and ORIG_T won't carry any non defaulted argument count.
12066 It will rather be the nested TREE_VECs that will carry one.
12067 In other words, ORIG_T carries a non defaulted argument count only
12068 if it doesn't contain any nested TREE_VEC. */
12069 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12070 {
12071 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12072 count += expanded_len_adjust;
12073 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12074 }
12075 for (i = 0, out = 0; i < len; i++)
12076 {
12077 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12078 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12079 && TREE_CODE (elts[i]) == TREE_VEC)
12080 {
12081 int idx;
12082
12083 /* Now expand the template argument pack "in place". */
12084 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12085 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12086 }
12087 else
12088 {
12089 TREE_VEC_ELT (t, out) = elts[i];
12090 out++;
12091 }
12092 }
12093
12094 return t;
12095 }
12096
12097 /* Substitute ARGS into one level PARMS of template parameters. */
12098
12099 static tree
12100 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12101 {
12102 if (parms == error_mark_node)
12103 return error_mark_node;
12104
12105 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12106
12107 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12108 {
12109 tree tuple = TREE_VEC_ELT (parms, i);
12110
12111 if (tuple == error_mark_node)
12112 continue;
12113
12114 TREE_VEC_ELT (new_vec, i) =
12115 tsubst_template_parm (tuple, args, complain);
12116 }
12117
12118 return new_vec;
12119 }
12120
12121 /* Return the result of substituting ARGS into the template parameters
12122 given by PARMS. If there are m levels of ARGS and m + n levels of
12123 PARMS, then the result will contain n levels of PARMS. For
12124 example, if PARMS is `template <class T> template <class U>
12125 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12126 result will be `template <int*, double, class V>'. */
12127
12128 static tree
12129 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12130 {
12131 tree r = NULL_TREE;
12132 tree* new_parms;
12133
12134 /* When substituting into a template, we must set
12135 PROCESSING_TEMPLATE_DECL as the template parameters may be
12136 dependent if they are based on one-another, and the dependency
12137 predicates are short-circuit outside of templates. */
12138 ++processing_template_decl;
12139
12140 for (new_parms = &r;
12141 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12142 new_parms = &(TREE_CHAIN (*new_parms)),
12143 parms = TREE_CHAIN (parms))
12144 {
12145 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12146 args, complain);
12147 *new_parms =
12148 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12149 - TMPL_ARGS_DEPTH (args)),
12150 new_vec, NULL_TREE);
12151 }
12152
12153 --processing_template_decl;
12154
12155 return r;
12156 }
12157
12158 /* Return the result of substituting ARGS into one template parameter
12159 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12160 parameter and which TREE_PURPOSE is the default argument of the
12161 template parameter. */
12162
12163 static tree
12164 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12165 {
12166 tree default_value, parm_decl;
12167
12168 if (args == NULL_TREE
12169 || t == NULL_TREE
12170 || t == error_mark_node)
12171 return t;
12172
12173 gcc_assert (TREE_CODE (t) == TREE_LIST);
12174
12175 default_value = TREE_PURPOSE (t);
12176 parm_decl = TREE_VALUE (t);
12177
12178 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12179 if (TREE_CODE (parm_decl) == PARM_DECL
12180 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12181 parm_decl = error_mark_node;
12182 default_value = tsubst_template_arg (default_value, args,
12183 complain, NULL_TREE);
12184
12185 return build_tree_list (default_value, parm_decl);
12186 }
12187
12188 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12189 type T. If T is not an aggregate or enumeration type, it is
12190 handled as if by tsubst. IN_DECL is as for tsubst. If
12191 ENTERING_SCOPE is nonzero, T is the context for a template which
12192 we are presently tsubst'ing. Return the substituted value. */
12193
12194 static tree
12195 tsubst_aggr_type (tree t,
12196 tree args,
12197 tsubst_flags_t complain,
12198 tree in_decl,
12199 int entering_scope)
12200 {
12201 if (t == NULL_TREE)
12202 return NULL_TREE;
12203
12204 switch (TREE_CODE (t))
12205 {
12206 case RECORD_TYPE:
12207 if (TYPE_PTRMEMFUNC_P (t))
12208 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12209
12210 /* Fall through. */
12211 case ENUMERAL_TYPE:
12212 case UNION_TYPE:
12213 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12214 {
12215 tree argvec;
12216 tree context;
12217 tree r;
12218 int saved_unevaluated_operand;
12219 int saved_inhibit_evaluation_warnings;
12220
12221 /* In "sizeof(X<I>)" we need to evaluate "I". */
12222 saved_unevaluated_operand = cp_unevaluated_operand;
12223 cp_unevaluated_operand = 0;
12224 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12225 c_inhibit_evaluation_warnings = 0;
12226
12227 /* First, determine the context for the type we are looking
12228 up. */
12229 context = TYPE_CONTEXT (t);
12230 if (context && TYPE_P (context))
12231 {
12232 context = tsubst_aggr_type (context, args, complain,
12233 in_decl, /*entering_scope=*/1);
12234 /* If context is a nested class inside a class template,
12235 it may still need to be instantiated (c++/33959). */
12236 context = complete_type (context);
12237 }
12238
12239 /* Then, figure out what arguments are appropriate for the
12240 type we are trying to find. For example, given:
12241
12242 template <class T> struct S;
12243 template <class T, class U> void f(T, U) { S<U> su; }
12244
12245 and supposing that we are instantiating f<int, double>,
12246 then our ARGS will be {int, double}, but, when looking up
12247 S we only want {double}. */
12248 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12249 complain, in_decl);
12250 if (argvec == error_mark_node)
12251 r = error_mark_node;
12252 else
12253 {
12254 r = lookup_template_class (t, argvec, in_decl, context,
12255 entering_scope, complain);
12256 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12257 }
12258
12259 cp_unevaluated_operand = saved_unevaluated_operand;
12260 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12261
12262 return r;
12263 }
12264 else
12265 /* This is not a template type, so there's nothing to do. */
12266 return t;
12267
12268 default:
12269 return tsubst (t, args, complain, in_decl);
12270 }
12271 }
12272
12273 static GTY((cache)) tree_cache_map *defarg_inst;
12274
12275 /* Substitute into the default argument ARG (a default argument for
12276 FN), which has the indicated TYPE. */
12277
12278 tree
12279 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12280 tsubst_flags_t complain)
12281 {
12282 tree saved_class_ptr = NULL_TREE;
12283 tree saved_class_ref = NULL_TREE;
12284 int errs = errorcount + sorrycount;
12285
12286 /* This can happen in invalid code. */
12287 if (TREE_CODE (arg) == DEFAULT_ARG)
12288 return arg;
12289
12290 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12291 parm = chain_index (parmnum, parm);
12292 tree parmtype = TREE_TYPE (parm);
12293 if (DECL_BY_REFERENCE (parm))
12294 parmtype = TREE_TYPE (parmtype);
12295 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12296
12297 tree *slot;
12298 if (defarg_inst && (slot = defarg_inst->get (parm)))
12299 return *slot;
12300
12301 /* This default argument came from a template. Instantiate the
12302 default argument here, not in tsubst. In the case of
12303 something like:
12304
12305 template <class T>
12306 struct S {
12307 static T t();
12308 void f(T = t());
12309 };
12310
12311 we must be careful to do name lookup in the scope of S<T>,
12312 rather than in the current class. */
12313 push_access_scope (fn);
12314 /* The "this" pointer is not valid in a default argument. */
12315 if (cfun)
12316 {
12317 saved_class_ptr = current_class_ptr;
12318 cp_function_chain->x_current_class_ptr = NULL_TREE;
12319 saved_class_ref = current_class_ref;
12320 cp_function_chain->x_current_class_ref = NULL_TREE;
12321 }
12322
12323 start_lambda_scope (parm);
12324
12325 push_deferring_access_checks(dk_no_deferred);
12326 /* The default argument expression may cause implicitly defined
12327 member functions to be synthesized, which will result in garbage
12328 collection. We must treat this situation as if we were within
12329 the body of function so as to avoid collecting live data on the
12330 stack. */
12331 ++function_depth;
12332 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12333 complain, NULL_TREE,
12334 /*integral_constant_expression_p=*/false);
12335 --function_depth;
12336 pop_deferring_access_checks();
12337
12338 finish_lambda_scope ();
12339
12340 /* Restore the "this" pointer. */
12341 if (cfun)
12342 {
12343 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12344 cp_function_chain->x_current_class_ref = saved_class_ref;
12345 }
12346
12347 if (errorcount+sorrycount > errs
12348 && (complain & tf_warning_or_error))
12349 inform (input_location,
12350 " when instantiating default argument for call to %qD", fn);
12351
12352 /* Make sure the default argument is reasonable. */
12353 arg = check_default_argument (type, arg, complain);
12354
12355 pop_access_scope (fn);
12356
12357 if (arg != error_mark_node && !cp_unevaluated_operand)
12358 {
12359 if (!defarg_inst)
12360 defarg_inst = tree_cache_map::create_ggc (37);
12361 defarg_inst->put (parm, arg);
12362 }
12363
12364 return arg;
12365 }
12366
12367 /* Substitute into all the default arguments for FN. */
12368
12369 static void
12370 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12371 {
12372 tree arg;
12373 tree tmpl_args;
12374
12375 tmpl_args = DECL_TI_ARGS (fn);
12376
12377 /* If this function is not yet instantiated, we certainly don't need
12378 its default arguments. */
12379 if (uses_template_parms (tmpl_args))
12380 return;
12381 /* Don't do this again for clones. */
12382 if (DECL_CLONED_FUNCTION_P (fn))
12383 return;
12384
12385 int i = 0;
12386 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12387 arg;
12388 arg = TREE_CHAIN (arg), ++i)
12389 if (TREE_PURPOSE (arg))
12390 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12391 TREE_VALUE (arg),
12392 TREE_PURPOSE (arg),
12393 complain);
12394 }
12395
12396 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12397
12398 static tree
12399 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12400 tree lambda_fntype)
12401 {
12402 tree gen_tmpl, argvec;
12403 hashval_t hash = 0;
12404 tree in_decl = t;
12405
12406 /* Nobody should be tsubst'ing into non-template functions. */
12407 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12408
12409 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12410 {
12411 /* If T is not dependent, just return it. */
12412 if (!uses_template_parms (DECL_TI_ARGS (t)))
12413 return t;
12414
12415 /* Calculate the most general template of which R is a
12416 specialization. */
12417 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12418
12419 /* We're substituting a lambda function under tsubst_lambda_expr but not
12420 directly from it; find the matching function we're already inside.
12421 But don't do this if T is a generic lambda with a single level of
12422 template parms, as in that case we're doing a normal instantiation. */
12423 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12424 && (!generic_lambda_fn_p (t)
12425 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12426 return enclosing_instantiation_of (t);
12427
12428 /* Calculate the complete set of arguments used to
12429 specialize R. */
12430 argvec = tsubst_template_args (DECL_TI_ARGS
12431 (DECL_TEMPLATE_RESULT
12432 (DECL_TI_TEMPLATE (t))),
12433 args, complain, in_decl);
12434 if (argvec == error_mark_node)
12435 return error_mark_node;
12436
12437 /* Check to see if we already have this specialization. */
12438 if (!lambda_fntype)
12439 {
12440 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12441 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12442 return spec;
12443 }
12444
12445 /* We can see more levels of arguments than parameters if
12446 there was a specialization of a member template, like
12447 this:
12448
12449 template <class T> struct S { template <class U> void f(); }
12450 template <> template <class U> void S<int>::f(U);
12451
12452 Here, we'll be substituting into the specialization,
12453 because that's where we can find the code we actually
12454 want to generate, but we'll have enough arguments for
12455 the most general template.
12456
12457 We also deal with the peculiar case:
12458
12459 template <class T> struct S {
12460 template <class U> friend void f();
12461 };
12462 template <class U> void f() {}
12463 template S<int>;
12464 template void f<double>();
12465
12466 Here, the ARGS for the instantiation of will be {int,
12467 double}. But, we only need as many ARGS as there are
12468 levels of template parameters in CODE_PATTERN. We are
12469 careful not to get fooled into reducing the ARGS in
12470 situations like:
12471
12472 template <class T> struct S { template <class U> void f(U); }
12473 template <class T> template <> void S<T>::f(int) {}
12474
12475 which we can spot because the pattern will be a
12476 specialization in this case. */
12477 int args_depth = TMPL_ARGS_DEPTH (args);
12478 int parms_depth =
12479 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12480
12481 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12482 args = get_innermost_template_args (args, parms_depth);
12483 }
12484 else
12485 {
12486 /* This special case arises when we have something like this:
12487
12488 template <class T> struct S {
12489 friend void f<int>(int, double);
12490 };
12491
12492 Here, the DECL_TI_TEMPLATE for the friend declaration
12493 will be an IDENTIFIER_NODE. We are being called from
12494 tsubst_friend_function, and we want only to create a
12495 new decl (R) with appropriate types so that we can call
12496 determine_specialization. */
12497 gen_tmpl = NULL_TREE;
12498 argvec = NULL_TREE;
12499 }
12500
12501 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12502 : NULL_TREE);
12503 tree ctx = closure ? closure : DECL_CONTEXT (t);
12504 bool member = ctx && TYPE_P (ctx);
12505
12506 if (member && !closure)
12507 ctx = tsubst_aggr_type (ctx, args,
12508 complain, t, /*entering_scope=*/1);
12509
12510 tree type = (lambda_fntype ? lambda_fntype
12511 : tsubst (TREE_TYPE (t), args,
12512 complain | tf_fndecl_type, in_decl));
12513 if (type == error_mark_node)
12514 return error_mark_node;
12515
12516 /* If we hit excessive deduction depth, the type is bogus even if
12517 it isn't error_mark_node, so don't build a decl. */
12518 if (excessive_deduction_depth)
12519 return error_mark_node;
12520
12521 /* We do NOT check for matching decls pushed separately at this
12522 point, as they may not represent instantiations of this
12523 template, and in any case are considered separate under the
12524 discrete model. */
12525 tree r = copy_decl (t);
12526 DECL_USE_TEMPLATE (r) = 0;
12527 TREE_TYPE (r) = type;
12528 /* Clear out the mangled name and RTL for the instantiation. */
12529 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12530 SET_DECL_RTL (r, NULL);
12531 /* Leave DECL_INITIAL set on deleted instantiations. */
12532 if (!DECL_DELETED_FN (r))
12533 DECL_INITIAL (r) = NULL_TREE;
12534 DECL_CONTEXT (r) = ctx;
12535
12536 /* OpenMP UDRs have the only argument a reference to the declared
12537 type. We want to diagnose if the declared type is a reference,
12538 which is invalid, but as references to references are usually
12539 quietly merged, diagnose it here. */
12540 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12541 {
12542 tree argtype
12543 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12544 argtype = tsubst (argtype, args, complain, in_decl);
12545 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12546 error_at (DECL_SOURCE_LOCATION (t),
12547 "reference type %qT in "
12548 "%<#pragma omp declare reduction%>", argtype);
12549 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12550 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12551 argtype);
12552 }
12553
12554 if (member && DECL_CONV_FN_P (r))
12555 /* Type-conversion operator. Reconstruct the name, in
12556 case it's the name of one of the template's parameters. */
12557 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12558
12559 tree parms = DECL_ARGUMENTS (t);
12560 if (closure)
12561 parms = DECL_CHAIN (parms);
12562 parms = tsubst (parms, args, complain, t);
12563 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12564 DECL_CONTEXT (parm) = r;
12565 if (closure)
12566 {
12567 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12568 DECL_CHAIN (tparm) = parms;
12569 parms = tparm;
12570 }
12571 DECL_ARGUMENTS (r) = parms;
12572 DECL_RESULT (r) = NULL_TREE;
12573
12574 TREE_STATIC (r) = 0;
12575 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12576 DECL_EXTERNAL (r) = 1;
12577 /* If this is an instantiation of a function with internal
12578 linkage, we already know what object file linkage will be
12579 assigned to the instantiation. */
12580 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12581 DECL_DEFER_OUTPUT (r) = 0;
12582 DECL_CHAIN (r) = NULL_TREE;
12583 DECL_PENDING_INLINE_INFO (r) = 0;
12584 DECL_PENDING_INLINE_P (r) = 0;
12585 DECL_SAVED_TREE (r) = NULL_TREE;
12586 DECL_STRUCT_FUNCTION (r) = NULL;
12587 TREE_USED (r) = 0;
12588 /* We'll re-clone as appropriate in instantiate_template. */
12589 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12590
12591 /* If we aren't complaining now, return on error before we register
12592 the specialization so that we'll complain eventually. */
12593 if ((complain & tf_error) == 0
12594 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12595 && !grok_op_properties (r, /*complain=*/false))
12596 return error_mark_node;
12597
12598 /* When instantiating a constrained member, substitute
12599 into the constraints to create a new constraint. */
12600 if (tree ci = get_constraints (t))
12601 if (member)
12602 {
12603 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12604 set_constraints (r, ci);
12605 }
12606
12607 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12608 this in the special friend case mentioned above where
12609 GEN_TMPL is NULL. */
12610 if (gen_tmpl && !closure)
12611 {
12612 DECL_TEMPLATE_INFO (r)
12613 = build_template_info (gen_tmpl, argvec);
12614 SET_DECL_IMPLICIT_INSTANTIATION (r);
12615
12616 tree new_r
12617 = register_specialization (r, gen_tmpl, argvec, false, hash);
12618 if (new_r != r)
12619 /* We instantiated this while substituting into
12620 the type earlier (template/friend54.C). */
12621 return new_r;
12622
12623 /* We're not supposed to instantiate default arguments
12624 until they are called, for a template. But, for a
12625 declaration like:
12626
12627 template <class T> void f ()
12628 { extern void g(int i = T()); }
12629
12630 we should do the substitution when the template is
12631 instantiated. We handle the member function case in
12632 instantiate_class_template since the default arguments
12633 might refer to other members of the class. */
12634 if (!member
12635 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12636 && !uses_template_parms (argvec))
12637 tsubst_default_arguments (r, complain);
12638 }
12639 else
12640 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12641
12642 /* Copy the list of befriending classes. */
12643 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12644 *friends;
12645 friends = &TREE_CHAIN (*friends))
12646 {
12647 *friends = copy_node (*friends);
12648 TREE_VALUE (*friends)
12649 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12650 }
12651
12652 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12653 {
12654 maybe_retrofit_in_chrg (r);
12655 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12656 return error_mark_node;
12657 /* If this is an instantiation of a member template, clone it.
12658 If it isn't, that'll be handled by
12659 clone_constructors_and_destructors. */
12660 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12661 clone_function_decl (r, /*update_methods=*/false);
12662 }
12663 else if ((complain & tf_error) != 0
12664 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12665 && !grok_op_properties (r, /*complain=*/true))
12666 return error_mark_node;
12667
12668 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12669 SET_DECL_FRIEND_CONTEXT (r,
12670 tsubst (DECL_FRIEND_CONTEXT (t),
12671 args, complain, in_decl));
12672
12673 /* Possibly limit visibility based on template args. */
12674 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12675 if (DECL_VISIBILITY_SPECIFIED (t))
12676 {
12677 DECL_VISIBILITY_SPECIFIED (r) = 0;
12678 DECL_ATTRIBUTES (r)
12679 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12680 }
12681 determine_visibility (r);
12682 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12683 && !processing_template_decl)
12684 defaulted_late_check (r);
12685
12686 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12687 args, complain, in_decl);
12688 return r;
12689 }
12690
12691 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12692
12693 static tree
12694 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12695 tree lambda_fntype)
12696 {
12697 /* We can get here when processing a member function template,
12698 member class template, or template template parameter. */
12699 tree decl = DECL_TEMPLATE_RESULT (t);
12700 tree in_decl = t;
12701 tree spec;
12702 tree tmpl_args;
12703 tree full_args;
12704 tree r;
12705 hashval_t hash = 0;
12706
12707 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12708 {
12709 /* Template template parameter is treated here. */
12710 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12711 if (new_type == error_mark_node)
12712 r = error_mark_node;
12713 /* If we get a real template back, return it. This can happen in
12714 the context of most_specialized_partial_spec. */
12715 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12716 r = new_type;
12717 else
12718 /* The new TEMPLATE_DECL was built in
12719 reduce_template_parm_level. */
12720 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12721 return r;
12722 }
12723
12724 if (!lambda_fntype)
12725 {
12726 /* We might already have an instance of this template.
12727 The ARGS are for the surrounding class type, so the
12728 full args contain the tsubst'd args for the context,
12729 plus the innermost args from the template decl. */
12730 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12731 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12732 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12733 /* Because this is a template, the arguments will still be
12734 dependent, even after substitution. If
12735 PROCESSING_TEMPLATE_DECL is not set, the dependency
12736 predicates will short-circuit. */
12737 ++processing_template_decl;
12738 full_args = tsubst_template_args (tmpl_args, args,
12739 complain, in_decl);
12740 --processing_template_decl;
12741 if (full_args == error_mark_node)
12742 return error_mark_node;
12743
12744 /* If this is a default template template argument,
12745 tsubst might not have changed anything. */
12746 if (full_args == tmpl_args)
12747 return t;
12748
12749 hash = hash_tmpl_and_args (t, full_args);
12750 spec = retrieve_specialization (t, full_args, hash);
12751 if (spec != NULL_TREE)
12752 return spec;
12753 }
12754
12755 /* Make a new template decl. It will be similar to the
12756 original, but will record the current template arguments.
12757 We also create a new function declaration, which is just
12758 like the old one, but points to this new template, rather
12759 than the old one. */
12760 r = copy_decl (t);
12761 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12762 DECL_CHAIN (r) = NULL_TREE;
12763
12764 // Build new template info linking to the original template decl.
12765 if (!lambda_fntype)
12766 {
12767 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12768 SET_DECL_IMPLICIT_INSTANTIATION (r);
12769 }
12770 else
12771 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12772
12773 /* The template parameters for this new template are all the
12774 template parameters for the old template, except the
12775 outermost level of parameters. */
12776 DECL_TEMPLATE_PARMS (r)
12777 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12778 complain);
12779
12780 if (TREE_CODE (decl) == TYPE_DECL
12781 && !TYPE_DECL_ALIAS_P (decl))
12782 {
12783 tree new_type;
12784 ++processing_template_decl;
12785 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12786 --processing_template_decl;
12787 if (new_type == error_mark_node)
12788 return error_mark_node;
12789
12790 TREE_TYPE (r) = new_type;
12791 /* For a partial specialization, we need to keep pointing to
12792 the primary template. */
12793 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12794 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12795 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12796 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12797 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12798 }
12799 else
12800 {
12801 tree new_decl;
12802 ++processing_template_decl;
12803 if (TREE_CODE (decl) == FUNCTION_DECL)
12804 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12805 else
12806 new_decl = tsubst (decl, args, complain, in_decl);
12807 --processing_template_decl;
12808 if (new_decl == error_mark_node)
12809 return error_mark_node;
12810
12811 DECL_TEMPLATE_RESULT (r) = new_decl;
12812 TREE_TYPE (r) = TREE_TYPE (new_decl);
12813 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12814 if (lambda_fntype)
12815 {
12816 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12817 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12818 }
12819 else
12820 {
12821 DECL_TI_TEMPLATE (new_decl) = r;
12822 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12823 }
12824 }
12825
12826 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12827 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12828
12829 if (PRIMARY_TEMPLATE_P (t))
12830 DECL_PRIMARY_TEMPLATE (r) = r;
12831
12832 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12833 && !lambda_fntype)
12834 /* Record this non-type partial instantiation. */
12835 register_specialization (r, t,
12836 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12837 false, hash);
12838
12839 return r;
12840 }
12841
12842 /* True if FN is the op() for a lambda in an uninstantiated template. */
12843
12844 bool
12845 lambda_fn_in_template_p (tree fn)
12846 {
12847 if (!fn || !LAMBDA_FUNCTION_P (fn))
12848 return false;
12849 tree closure = DECL_CONTEXT (fn);
12850 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12851 }
12852
12853 /* We're instantiating a variable from template function TCTX. Return the
12854 corresponding current enclosing scope. This gets complicated because lambda
12855 functions in templates are regenerated rather than instantiated, but generic
12856 lambda functions are subsequently instantiated. */
12857
12858 static tree
12859 enclosing_instantiation_of (tree otctx)
12860 {
12861 tree tctx = otctx;
12862 tree fn = current_function_decl;
12863 int lambda_count = 0;
12864
12865 for (; tctx && lambda_fn_in_template_p (tctx);
12866 tctx = decl_function_context (tctx))
12867 ++lambda_count;
12868 for (; fn; fn = decl_function_context (fn))
12869 {
12870 tree ofn = fn;
12871 int flambda_count = 0;
12872 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
12873 fn = decl_function_context (fn))
12874 ++flambda_count;
12875 if (DECL_TEMPLATE_INFO (fn)
12876 ? most_general_template (fn) != most_general_template (tctx)
12877 : fn != tctx)
12878 continue;
12879 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
12880 || DECL_CONV_FN_P (ofn));
12881 return ofn;
12882 }
12883 gcc_unreachable ();
12884 }
12885
12886 /* Substitute the ARGS into the T, which is a _DECL. Return the
12887 result of the substitution. Issue error and warning messages under
12888 control of COMPLAIN. */
12889
12890 static tree
12891 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12892 {
12893 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12894 location_t saved_loc;
12895 tree r = NULL_TREE;
12896 tree in_decl = t;
12897 hashval_t hash = 0;
12898
12899 /* Set the filename and linenumber to improve error-reporting. */
12900 saved_loc = input_location;
12901 input_location = DECL_SOURCE_LOCATION (t);
12902
12903 switch (TREE_CODE (t))
12904 {
12905 case TEMPLATE_DECL:
12906 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12907 break;
12908
12909 case FUNCTION_DECL:
12910 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12911 break;
12912
12913 case PARM_DECL:
12914 {
12915 tree type = NULL_TREE;
12916 int i, len = 1;
12917 tree expanded_types = NULL_TREE;
12918 tree prev_r = NULL_TREE;
12919 tree first_r = NULL_TREE;
12920
12921 if (DECL_PACK_P (t))
12922 {
12923 /* If there is a local specialization that isn't a
12924 parameter pack, it means that we're doing a "simple"
12925 substitution from inside tsubst_pack_expansion. Just
12926 return the local specialization (which will be a single
12927 parm). */
12928 tree spec = retrieve_local_specialization (t);
12929 if (spec
12930 && TREE_CODE (spec) == PARM_DECL
12931 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12932 RETURN (spec);
12933
12934 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12935 the parameters in this function parameter pack. */
12936 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12937 complain, in_decl);
12938 if (TREE_CODE (expanded_types) == TREE_VEC)
12939 {
12940 len = TREE_VEC_LENGTH (expanded_types);
12941
12942 /* Zero-length parameter packs are boring. Just substitute
12943 into the chain. */
12944 if (len == 0)
12945 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12946 TREE_CHAIN (t)));
12947 }
12948 else
12949 {
12950 /* All we did was update the type. Make a note of that. */
12951 type = expanded_types;
12952 expanded_types = NULL_TREE;
12953 }
12954 }
12955
12956 /* Loop through all of the parameters we'll build. When T is
12957 a function parameter pack, LEN is the number of expanded
12958 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12959 r = NULL_TREE;
12960 for (i = 0; i < len; ++i)
12961 {
12962 prev_r = r;
12963 r = copy_node (t);
12964 if (DECL_TEMPLATE_PARM_P (t))
12965 SET_DECL_TEMPLATE_PARM_P (r);
12966
12967 if (expanded_types)
12968 /* We're on the Ith parameter of the function parameter
12969 pack. */
12970 {
12971 /* Get the Ith type. */
12972 type = TREE_VEC_ELT (expanded_types, i);
12973
12974 /* Rename the parameter to include the index. */
12975 DECL_NAME (r)
12976 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12977 }
12978 else if (!type)
12979 /* We're dealing with a normal parameter. */
12980 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12981
12982 type = type_decays_to (type);
12983 TREE_TYPE (r) = type;
12984 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12985
12986 if (DECL_INITIAL (r))
12987 {
12988 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12989 DECL_INITIAL (r) = TREE_TYPE (r);
12990 else
12991 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12992 complain, in_decl);
12993 }
12994
12995 DECL_CONTEXT (r) = NULL_TREE;
12996
12997 if (!DECL_TEMPLATE_PARM_P (r))
12998 DECL_ARG_TYPE (r) = type_passed_as (type);
12999
13000 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13001 args, complain, in_decl);
13002
13003 /* Keep track of the first new parameter we
13004 generate. That's what will be returned to the
13005 caller. */
13006 if (!first_r)
13007 first_r = r;
13008
13009 /* Build a proper chain of parameters when substituting
13010 into a function parameter pack. */
13011 if (prev_r)
13012 DECL_CHAIN (prev_r) = r;
13013 }
13014
13015 /* If cp_unevaluated_operand is set, we're just looking for a
13016 single dummy parameter, so don't keep going. */
13017 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13018 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13019 complain, DECL_CHAIN (t));
13020
13021 /* FIRST_R contains the start of the chain we've built. */
13022 r = first_r;
13023 }
13024 break;
13025
13026 case FIELD_DECL:
13027 {
13028 tree type = NULL_TREE;
13029 tree vec = NULL_TREE;
13030 tree expanded_types = NULL_TREE;
13031 int len = 1;
13032
13033 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13034 {
13035 /* This field is a lambda capture pack. Return a TREE_VEC of
13036 the expanded fields to instantiate_class_template_1. */
13037 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13038 complain, in_decl);
13039 if (TREE_CODE (expanded_types) == TREE_VEC)
13040 {
13041 len = TREE_VEC_LENGTH (expanded_types);
13042 vec = make_tree_vec (len);
13043 }
13044 else
13045 {
13046 /* All we did was update the type. Make a note of that. */
13047 type = expanded_types;
13048 expanded_types = NULL_TREE;
13049 }
13050 }
13051
13052 for (int i = 0; i < len; ++i)
13053 {
13054 r = copy_decl (t);
13055 if (expanded_types)
13056 {
13057 type = TREE_VEC_ELT (expanded_types, i);
13058 DECL_NAME (r)
13059 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13060 }
13061 else if (!type)
13062 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13063
13064 if (type == error_mark_node)
13065 RETURN (error_mark_node);
13066 TREE_TYPE (r) = type;
13067 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13068
13069 if (DECL_C_BIT_FIELD (r))
13070 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13071 number of bits. */
13072 DECL_BIT_FIELD_REPRESENTATIVE (r)
13073 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13074 complain, in_decl,
13075 /*integral_constant_expression_p=*/true);
13076 if (DECL_INITIAL (t))
13077 {
13078 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13079 NSDMI in perform_member_init. Still set DECL_INITIAL
13080 so that we know there is one. */
13081 DECL_INITIAL (r) = void_node;
13082 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13083 retrofit_lang_decl (r);
13084 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13085 }
13086 /* We don't have to set DECL_CONTEXT here; it is set by
13087 finish_member_declaration. */
13088 DECL_CHAIN (r) = NULL_TREE;
13089
13090 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13091 args, complain, in_decl);
13092
13093 if (vec)
13094 TREE_VEC_ELT (vec, i) = r;
13095 }
13096
13097 if (vec)
13098 r = vec;
13099 }
13100 break;
13101
13102 case USING_DECL:
13103 /* We reach here only for member using decls. We also need to check
13104 uses_template_parms because DECL_DEPENDENT_P is not set for a
13105 using-declaration that designates a member of the current
13106 instantiation (c++/53549). */
13107 if (DECL_DEPENDENT_P (t)
13108 || uses_template_parms (USING_DECL_SCOPE (t)))
13109 {
13110 tree scope = USING_DECL_SCOPE (t);
13111 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13112 if (PACK_EXPANSION_P (scope))
13113 {
13114 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13115 int len = TREE_VEC_LENGTH (vec);
13116 r = make_tree_vec (len);
13117 for (int i = 0; i < len; ++i)
13118 {
13119 tree escope = TREE_VEC_ELT (vec, i);
13120 tree elt = do_class_using_decl (escope, name);
13121 if (!elt)
13122 {
13123 r = error_mark_node;
13124 break;
13125 }
13126 else
13127 {
13128 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13129 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13130 }
13131 TREE_VEC_ELT (r, i) = elt;
13132 }
13133 }
13134 else
13135 {
13136 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13137 complain, in_decl);
13138 r = do_class_using_decl (inst_scope, name);
13139 if (!r)
13140 r = error_mark_node;
13141 else
13142 {
13143 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13144 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13145 }
13146 }
13147 }
13148 else
13149 {
13150 r = copy_node (t);
13151 DECL_CHAIN (r) = NULL_TREE;
13152 }
13153 break;
13154
13155 case TYPE_DECL:
13156 case VAR_DECL:
13157 {
13158 tree argvec = NULL_TREE;
13159 tree gen_tmpl = NULL_TREE;
13160 tree spec;
13161 tree tmpl = NULL_TREE;
13162 tree ctx;
13163 tree type = NULL_TREE;
13164 bool local_p;
13165
13166 if (TREE_TYPE (t) == error_mark_node)
13167 RETURN (error_mark_node);
13168
13169 if (TREE_CODE (t) == TYPE_DECL
13170 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13171 {
13172 /* If this is the canonical decl, we don't have to
13173 mess with instantiations, and often we can't (for
13174 typename, template type parms and such). Note that
13175 TYPE_NAME is not correct for the above test if
13176 we've copied the type for a typedef. */
13177 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13178 if (type == error_mark_node)
13179 RETURN (error_mark_node);
13180 r = TYPE_NAME (type);
13181 break;
13182 }
13183
13184 /* Check to see if we already have the specialization we
13185 need. */
13186 spec = NULL_TREE;
13187 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13188 {
13189 /* T is a static data member or namespace-scope entity.
13190 We have to substitute into namespace-scope variables
13191 (not just variable templates) because of cases like:
13192
13193 template <class T> void f() { extern T t; }
13194
13195 where the entity referenced is not known until
13196 instantiation time. */
13197 local_p = false;
13198 ctx = DECL_CONTEXT (t);
13199 if (DECL_CLASS_SCOPE_P (t))
13200 {
13201 ctx = tsubst_aggr_type (ctx, args,
13202 complain,
13203 in_decl, /*entering_scope=*/1);
13204 /* If CTX is unchanged, then T is in fact the
13205 specialization we want. That situation occurs when
13206 referencing a static data member within in its own
13207 class. We can use pointer equality, rather than
13208 same_type_p, because DECL_CONTEXT is always
13209 canonical... */
13210 if (ctx == DECL_CONTEXT (t)
13211 /* ... unless T is a member template; in which
13212 case our caller can be willing to create a
13213 specialization of that template represented
13214 by T. */
13215 && !(DECL_TI_TEMPLATE (t)
13216 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13217 spec = t;
13218 }
13219
13220 if (!spec)
13221 {
13222 tmpl = DECL_TI_TEMPLATE (t);
13223 gen_tmpl = most_general_template (tmpl);
13224 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13225 if (argvec != error_mark_node)
13226 argvec = (coerce_innermost_template_parms
13227 (DECL_TEMPLATE_PARMS (gen_tmpl),
13228 argvec, t, complain,
13229 /*all*/true, /*defarg*/true));
13230 if (argvec == error_mark_node)
13231 RETURN (error_mark_node);
13232 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13233 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13234 }
13235 }
13236 else
13237 {
13238 /* A local variable. */
13239 local_p = true;
13240 /* Subsequent calls to pushdecl will fill this in. */
13241 ctx = NULL_TREE;
13242 /* Unless this is a reference to a static variable from an
13243 enclosing function, in which case we need to fill it in now. */
13244 if (TREE_STATIC (t))
13245 {
13246 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13247 if (fn != current_function_decl)
13248 ctx = fn;
13249 }
13250 spec = retrieve_local_specialization (t);
13251 }
13252 /* If we already have the specialization we need, there is
13253 nothing more to do. */
13254 if (spec)
13255 {
13256 r = spec;
13257 break;
13258 }
13259
13260 /* Create a new node for the specialization we need. */
13261 r = copy_decl (t);
13262 if (type == NULL_TREE)
13263 {
13264 if (is_typedef_decl (t))
13265 type = DECL_ORIGINAL_TYPE (t);
13266 else
13267 type = TREE_TYPE (t);
13268 if (VAR_P (t)
13269 && VAR_HAD_UNKNOWN_BOUND (t)
13270 && type != error_mark_node)
13271 type = strip_array_domain (type);
13272 tree sub_args = args;
13273 if (tree auto_node = type_uses_auto (type))
13274 {
13275 /* Mask off any template args past the variable's context so we
13276 don't replace the auto with an unrelated argument. */
13277 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13278 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13279 if (extra > 0)
13280 /* This should never happen with the new lambda instantiation
13281 model, but keep the handling just in case. */
13282 gcc_assert (!CHECKING_P),
13283 sub_args = strip_innermost_template_args (args, extra);
13284 }
13285 type = tsubst (type, sub_args, complain, in_decl);
13286 }
13287 if (VAR_P (r))
13288 {
13289 /* Even if the original location is out of scope, the
13290 newly substituted one is not. */
13291 DECL_DEAD_FOR_LOCAL (r) = 0;
13292 DECL_INITIALIZED_P (r) = 0;
13293 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13294 if (type == error_mark_node)
13295 RETURN (error_mark_node);
13296 if (TREE_CODE (type) == FUNCTION_TYPE)
13297 {
13298 /* It may seem that this case cannot occur, since:
13299
13300 typedef void f();
13301 void g() { f x; }
13302
13303 declares a function, not a variable. However:
13304
13305 typedef void f();
13306 template <typename T> void g() { T t; }
13307 template void g<f>();
13308
13309 is an attempt to declare a variable with function
13310 type. */
13311 error ("variable %qD has function type",
13312 /* R is not yet sufficiently initialized, so we
13313 just use its name. */
13314 DECL_NAME (r));
13315 RETURN (error_mark_node);
13316 }
13317 type = complete_type (type);
13318 /* Wait until cp_finish_decl to set this again, to handle
13319 circular dependency (template/instantiate6.C). */
13320 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13321 type = check_var_type (DECL_NAME (r), type);
13322
13323 if (DECL_HAS_VALUE_EXPR_P (t))
13324 {
13325 tree ve = DECL_VALUE_EXPR (t);
13326 ve = tsubst_expr (ve, args, complain, in_decl,
13327 /*constant_expression_p=*/false);
13328 if (REFERENCE_REF_P (ve))
13329 {
13330 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13331 ve = TREE_OPERAND (ve, 0);
13332 }
13333 SET_DECL_VALUE_EXPR (r, ve);
13334 }
13335 if (CP_DECL_THREAD_LOCAL_P (r)
13336 && !processing_template_decl)
13337 set_decl_tls_model (r, decl_default_tls_model (r));
13338 }
13339 else if (DECL_SELF_REFERENCE_P (t))
13340 SET_DECL_SELF_REFERENCE_P (r);
13341 TREE_TYPE (r) = type;
13342 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13343 DECL_CONTEXT (r) = ctx;
13344 /* Clear out the mangled name and RTL for the instantiation. */
13345 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13346 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13347 SET_DECL_RTL (r, NULL);
13348 /* The initializer must not be expanded until it is required;
13349 see [temp.inst]. */
13350 DECL_INITIAL (r) = NULL_TREE;
13351 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13352 if (VAR_P (r))
13353 {
13354 if (DECL_LANG_SPECIFIC (r))
13355 SET_DECL_DEPENDENT_INIT_P (r, false);
13356
13357 SET_DECL_MODE (r, VOIDmode);
13358
13359 /* Possibly limit visibility based on template args. */
13360 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13361 if (DECL_VISIBILITY_SPECIFIED (t))
13362 {
13363 DECL_VISIBILITY_SPECIFIED (r) = 0;
13364 DECL_ATTRIBUTES (r)
13365 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13366 }
13367 determine_visibility (r);
13368 }
13369
13370 if (!local_p)
13371 {
13372 /* A static data member declaration is always marked
13373 external when it is declared in-class, even if an
13374 initializer is present. We mimic the non-template
13375 processing here. */
13376 DECL_EXTERNAL (r) = 1;
13377 if (DECL_NAMESPACE_SCOPE_P (t))
13378 DECL_NOT_REALLY_EXTERN (r) = 1;
13379
13380 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13381 SET_DECL_IMPLICIT_INSTANTIATION (r);
13382 register_specialization (r, gen_tmpl, argvec, false, hash);
13383 }
13384 else
13385 {
13386 if (DECL_LANG_SPECIFIC (r))
13387 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13388 if (!cp_unevaluated_operand)
13389 register_local_specialization (r, t);
13390 }
13391
13392 DECL_CHAIN (r) = NULL_TREE;
13393
13394 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13395 /*flags=*/0,
13396 args, complain, in_decl);
13397
13398 /* Preserve a typedef that names a type. */
13399 if (is_typedef_decl (r) && type != error_mark_node)
13400 {
13401 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13402 set_underlying_type (r);
13403 if (TYPE_DECL_ALIAS_P (r))
13404 /* An alias template specialization can be dependent
13405 even if its underlying type is not. */
13406 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13407 }
13408
13409 layout_decl (r, 0);
13410 }
13411 break;
13412
13413 default:
13414 gcc_unreachable ();
13415 }
13416 #undef RETURN
13417
13418 out:
13419 /* Restore the file and line information. */
13420 input_location = saved_loc;
13421
13422 return r;
13423 }
13424
13425 /* Substitute into the ARG_TYPES of a function type.
13426 If END is a TREE_CHAIN, leave it and any following types
13427 un-substituted. */
13428
13429 static tree
13430 tsubst_arg_types (tree arg_types,
13431 tree args,
13432 tree end,
13433 tsubst_flags_t complain,
13434 tree in_decl)
13435 {
13436 tree remaining_arg_types;
13437 tree type = NULL_TREE;
13438 int i = 1;
13439 tree expanded_args = NULL_TREE;
13440 tree default_arg;
13441
13442 if (!arg_types || arg_types == void_list_node || arg_types == end)
13443 return arg_types;
13444
13445 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13446 args, end, complain, in_decl);
13447 if (remaining_arg_types == error_mark_node)
13448 return error_mark_node;
13449
13450 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13451 {
13452 /* For a pack expansion, perform substitution on the
13453 entire expression. Later on, we'll handle the arguments
13454 one-by-one. */
13455 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13456 args, complain, in_decl);
13457
13458 if (TREE_CODE (expanded_args) == TREE_VEC)
13459 /* So that we'll spin through the parameters, one by one. */
13460 i = TREE_VEC_LENGTH (expanded_args);
13461 else
13462 {
13463 /* We only partially substituted into the parameter
13464 pack. Our type is TYPE_PACK_EXPANSION. */
13465 type = expanded_args;
13466 expanded_args = NULL_TREE;
13467 }
13468 }
13469
13470 while (i > 0) {
13471 --i;
13472
13473 if (expanded_args)
13474 type = TREE_VEC_ELT (expanded_args, i);
13475 else if (!type)
13476 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13477
13478 if (type == error_mark_node)
13479 return error_mark_node;
13480 if (VOID_TYPE_P (type))
13481 {
13482 if (complain & tf_error)
13483 {
13484 error ("invalid parameter type %qT", type);
13485 if (in_decl)
13486 error ("in declaration %q+D", in_decl);
13487 }
13488 return error_mark_node;
13489 }
13490 /* DR 657. */
13491 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13492 return error_mark_node;
13493
13494 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13495 top-level qualifiers as required. */
13496 type = cv_unqualified (type_decays_to (type));
13497
13498 /* We do not substitute into default arguments here. The standard
13499 mandates that they be instantiated only when needed, which is
13500 done in build_over_call. */
13501 default_arg = TREE_PURPOSE (arg_types);
13502
13503 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13504 since the new op() won't have any associated template arguments for us
13505 to refer to later. */
13506 if (lambda_fn_in_template_p (in_decl))
13507 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13508 false/*fn*/, false/*constexpr*/);
13509
13510 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13511 {
13512 /* We've instantiated a template before its default arguments
13513 have been parsed. This can happen for a nested template
13514 class, and is not an error unless we require the default
13515 argument in a call of this function. */
13516 remaining_arg_types =
13517 tree_cons (default_arg, type, remaining_arg_types);
13518 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13519 }
13520 else
13521 remaining_arg_types =
13522 hash_tree_cons (default_arg, type, remaining_arg_types);
13523 }
13524
13525 return remaining_arg_types;
13526 }
13527
13528 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13529 *not* handle the exception-specification for FNTYPE, because the
13530 initial substitution of explicitly provided template parameters
13531 during argument deduction forbids substitution into the
13532 exception-specification:
13533
13534 [temp.deduct]
13535
13536 All references in the function type of the function template to the
13537 corresponding template parameters are replaced by the specified tem-
13538 plate argument values. If a substitution in a template parameter or
13539 in the function type of the function template results in an invalid
13540 type, type deduction fails. [Note: The equivalent substitution in
13541 exception specifications is done only when the function is instanti-
13542 ated, at which point a program is ill-formed if the substitution
13543 results in an invalid type.] */
13544
13545 static tree
13546 tsubst_function_type (tree t,
13547 tree args,
13548 tsubst_flags_t complain,
13549 tree in_decl)
13550 {
13551 tree return_type;
13552 tree arg_types = NULL_TREE;
13553 tree fntype;
13554
13555 /* The TYPE_CONTEXT is not used for function/method types. */
13556 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13557
13558 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13559 failure. */
13560 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13561
13562 if (late_return_type_p)
13563 {
13564 /* Substitute the argument types. */
13565 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13566 complain, in_decl);
13567 if (arg_types == error_mark_node)
13568 return error_mark_node;
13569
13570 tree save_ccp = current_class_ptr;
13571 tree save_ccr = current_class_ref;
13572 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13573 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13574 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13575 if (do_inject)
13576 {
13577 /* DR 1207: 'this' is in scope in the trailing return type. */
13578 inject_this_parameter (this_type, cp_type_quals (this_type));
13579 }
13580
13581 /* Substitute the return type. */
13582 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13583
13584 if (do_inject)
13585 {
13586 current_class_ptr = save_ccp;
13587 current_class_ref = save_ccr;
13588 }
13589 }
13590 else
13591 /* Substitute the return type. */
13592 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13593
13594 if (return_type == error_mark_node)
13595 return error_mark_node;
13596 /* DR 486 clarifies that creation of a function type with an
13597 invalid return type is a deduction failure. */
13598 if (TREE_CODE (return_type) == ARRAY_TYPE
13599 || TREE_CODE (return_type) == FUNCTION_TYPE)
13600 {
13601 if (complain & tf_error)
13602 {
13603 if (TREE_CODE (return_type) == ARRAY_TYPE)
13604 error ("function returning an array");
13605 else
13606 error ("function returning a function");
13607 }
13608 return error_mark_node;
13609 }
13610 /* And DR 657. */
13611 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13612 return error_mark_node;
13613
13614 if (!late_return_type_p)
13615 {
13616 /* Substitute the argument types. */
13617 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13618 complain, in_decl);
13619 if (arg_types == error_mark_node)
13620 return error_mark_node;
13621 }
13622
13623 /* Construct a new type node and return it. */
13624 if (TREE_CODE (t) == FUNCTION_TYPE)
13625 {
13626 fntype = build_function_type (return_type, arg_types);
13627 fntype = apply_memfn_quals (fntype,
13628 type_memfn_quals (t),
13629 type_memfn_rqual (t));
13630 }
13631 else
13632 {
13633 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13634 /* Don't pick up extra function qualifiers from the basetype. */
13635 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13636 if (! MAYBE_CLASS_TYPE_P (r))
13637 {
13638 /* [temp.deduct]
13639
13640 Type deduction may fail for any of the following
13641 reasons:
13642
13643 -- Attempting to create "pointer to member of T" when T
13644 is not a class type. */
13645 if (complain & tf_error)
13646 error ("creating pointer to member function of non-class type %qT",
13647 r);
13648 return error_mark_node;
13649 }
13650
13651 fntype = build_method_type_directly (r, return_type,
13652 TREE_CHAIN (arg_types));
13653 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13654 }
13655 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13656
13657 if (late_return_type_p)
13658 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13659
13660 return fntype;
13661 }
13662
13663 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13664 ARGS into that specification, and return the substituted
13665 specification. If there is no specification, return NULL_TREE. */
13666
13667 static tree
13668 tsubst_exception_specification (tree fntype,
13669 tree args,
13670 tsubst_flags_t complain,
13671 tree in_decl,
13672 bool defer_ok)
13673 {
13674 tree specs;
13675 tree new_specs;
13676
13677 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13678 new_specs = NULL_TREE;
13679 if (specs && TREE_PURPOSE (specs))
13680 {
13681 /* A noexcept-specifier. */
13682 tree expr = TREE_PURPOSE (specs);
13683 if (TREE_CODE (expr) == INTEGER_CST)
13684 new_specs = expr;
13685 else if (defer_ok)
13686 {
13687 /* Defer instantiation of noexcept-specifiers to avoid
13688 excessive instantiations (c++/49107). */
13689 new_specs = make_node (DEFERRED_NOEXCEPT);
13690 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13691 {
13692 /* We already partially instantiated this member template,
13693 so combine the new args with the old. */
13694 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13695 = DEFERRED_NOEXCEPT_PATTERN (expr);
13696 DEFERRED_NOEXCEPT_ARGS (new_specs)
13697 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13698 }
13699 else
13700 {
13701 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13702 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13703 }
13704 }
13705 else
13706 new_specs = tsubst_copy_and_build
13707 (expr, args, complain, in_decl, /*function_p=*/false,
13708 /*integral_constant_expression_p=*/true);
13709 new_specs = build_noexcept_spec (new_specs, complain);
13710 }
13711 else if (specs)
13712 {
13713 if (! TREE_VALUE (specs))
13714 new_specs = specs;
13715 else
13716 while (specs)
13717 {
13718 tree spec;
13719 int i, len = 1;
13720 tree expanded_specs = NULL_TREE;
13721
13722 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13723 {
13724 /* Expand the pack expansion type. */
13725 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13726 args, complain,
13727 in_decl);
13728
13729 if (expanded_specs == error_mark_node)
13730 return error_mark_node;
13731 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13732 len = TREE_VEC_LENGTH (expanded_specs);
13733 else
13734 {
13735 /* We're substituting into a member template, so
13736 we got a TYPE_PACK_EXPANSION back. Add that
13737 expansion and move on. */
13738 gcc_assert (TREE_CODE (expanded_specs)
13739 == TYPE_PACK_EXPANSION);
13740 new_specs = add_exception_specifier (new_specs,
13741 expanded_specs,
13742 complain);
13743 specs = TREE_CHAIN (specs);
13744 continue;
13745 }
13746 }
13747
13748 for (i = 0; i < len; ++i)
13749 {
13750 if (expanded_specs)
13751 spec = TREE_VEC_ELT (expanded_specs, i);
13752 else
13753 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13754 if (spec == error_mark_node)
13755 return spec;
13756 new_specs = add_exception_specifier (new_specs, spec,
13757 complain);
13758 }
13759
13760 specs = TREE_CHAIN (specs);
13761 }
13762 }
13763 return new_specs;
13764 }
13765
13766 /* Take the tree structure T and replace template parameters used
13767 therein with the argument vector ARGS. IN_DECL is an associated
13768 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13769 Issue error and warning messages under control of COMPLAIN. Note
13770 that we must be relatively non-tolerant of extensions here, in
13771 order to preserve conformance; if we allow substitutions that
13772 should not be allowed, we may allow argument deductions that should
13773 not succeed, and therefore report ambiguous overload situations
13774 where there are none. In theory, we could allow the substitution,
13775 but indicate that it should have failed, and allow our caller to
13776 make sure that the right thing happens, but we don't try to do this
13777 yet.
13778
13779 This function is used for dealing with types, decls and the like;
13780 for expressions, use tsubst_expr or tsubst_copy. */
13781
13782 tree
13783 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13784 {
13785 enum tree_code code;
13786 tree type, r = NULL_TREE;
13787
13788 if (t == NULL_TREE || t == error_mark_node
13789 || t == integer_type_node
13790 || t == void_type_node
13791 || t == char_type_node
13792 || t == unknown_type_node
13793 || TREE_CODE (t) == NAMESPACE_DECL
13794 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13795 return t;
13796
13797 if (DECL_P (t))
13798 return tsubst_decl (t, args, complain);
13799
13800 if (args == NULL_TREE)
13801 return t;
13802
13803 code = TREE_CODE (t);
13804
13805 if (code == IDENTIFIER_NODE)
13806 type = IDENTIFIER_TYPE_VALUE (t);
13807 else
13808 type = TREE_TYPE (t);
13809
13810 gcc_assert (type != unknown_type_node);
13811
13812 /* Reuse typedefs. We need to do this to handle dependent attributes,
13813 such as attribute aligned. */
13814 if (TYPE_P (t)
13815 && typedef_variant_p (t))
13816 {
13817 tree decl = TYPE_NAME (t);
13818
13819 if (alias_template_specialization_p (t))
13820 {
13821 /* DECL represents an alias template and we want to
13822 instantiate it. */
13823 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13824 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13825 r = instantiate_alias_template (tmpl, gen_args, complain);
13826 }
13827 else if (DECL_CLASS_SCOPE_P (decl)
13828 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13829 && uses_template_parms (DECL_CONTEXT (decl)))
13830 {
13831 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13832 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13833 r = retrieve_specialization (tmpl, gen_args, 0);
13834 }
13835 else if (DECL_FUNCTION_SCOPE_P (decl)
13836 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13837 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13838 r = retrieve_local_specialization (decl);
13839 else
13840 /* The typedef is from a non-template context. */
13841 return t;
13842
13843 if (r)
13844 {
13845 r = TREE_TYPE (r);
13846 r = cp_build_qualified_type_real
13847 (r, cp_type_quals (t) | cp_type_quals (r),
13848 complain | tf_ignore_bad_quals);
13849 return r;
13850 }
13851 else
13852 {
13853 /* We don't have an instantiation yet, so drop the typedef. */
13854 int quals = cp_type_quals (t);
13855 t = DECL_ORIGINAL_TYPE (decl);
13856 t = cp_build_qualified_type_real (t, quals,
13857 complain | tf_ignore_bad_quals);
13858 }
13859 }
13860
13861 bool fndecl_type = (complain & tf_fndecl_type);
13862 complain &= ~tf_fndecl_type;
13863
13864 if (type
13865 && code != TYPENAME_TYPE
13866 && code != TEMPLATE_TYPE_PARM
13867 && code != TEMPLATE_PARM_INDEX
13868 && code != IDENTIFIER_NODE
13869 && code != FUNCTION_TYPE
13870 && code != METHOD_TYPE)
13871 type = tsubst (type, args, complain, in_decl);
13872 if (type == error_mark_node)
13873 return error_mark_node;
13874
13875 switch (code)
13876 {
13877 case RECORD_TYPE:
13878 case UNION_TYPE:
13879 case ENUMERAL_TYPE:
13880 return tsubst_aggr_type (t, args, complain, in_decl,
13881 /*entering_scope=*/0);
13882
13883 case ERROR_MARK:
13884 case IDENTIFIER_NODE:
13885 case VOID_TYPE:
13886 case REAL_TYPE:
13887 case COMPLEX_TYPE:
13888 case VECTOR_TYPE:
13889 case BOOLEAN_TYPE:
13890 case NULLPTR_TYPE:
13891 case LANG_TYPE:
13892 return t;
13893
13894 case INTEGER_TYPE:
13895 if (t == integer_type_node)
13896 return t;
13897
13898 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13899 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13900 return t;
13901
13902 {
13903 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13904
13905 max = tsubst_expr (omax, args, complain, in_decl,
13906 /*integral_constant_expression_p=*/false);
13907
13908 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13909 needed. */
13910 if (TREE_CODE (max) == NOP_EXPR
13911 && TREE_SIDE_EFFECTS (omax)
13912 && !TREE_TYPE (max))
13913 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13914
13915 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13916 with TREE_SIDE_EFFECTS that indicates this is not an integral
13917 constant expression. */
13918 if (processing_template_decl
13919 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13920 {
13921 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13922 TREE_SIDE_EFFECTS (max) = 1;
13923 }
13924
13925 return compute_array_index_type (NULL_TREE, max, complain);
13926 }
13927
13928 case TEMPLATE_TYPE_PARM:
13929 case TEMPLATE_TEMPLATE_PARM:
13930 case BOUND_TEMPLATE_TEMPLATE_PARM:
13931 case TEMPLATE_PARM_INDEX:
13932 {
13933 int idx;
13934 int level;
13935 int levels;
13936 tree arg = NULL_TREE;
13937
13938 /* Early in template argument deduction substitution, we don't
13939 want to reduce the level of 'auto', or it will be confused
13940 with a normal template parm in subsequent deduction. */
13941 if (is_auto (t) && (complain & tf_partial))
13942 return t;
13943
13944 r = NULL_TREE;
13945
13946 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13947 template_parm_level_and_index (t, &level, &idx);
13948
13949 levels = TMPL_ARGS_DEPTH (args);
13950 if (level <= levels
13951 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13952 {
13953 arg = TMPL_ARG (args, level, idx);
13954
13955 /* See through ARGUMENT_PACK_SELECT arguments. */
13956 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13957 arg = argument_pack_select_arg (arg);
13958 }
13959
13960 if (arg == error_mark_node)
13961 return error_mark_node;
13962 else if (arg != NULL_TREE)
13963 {
13964 if (ARGUMENT_PACK_P (arg))
13965 /* If ARG is an argument pack, we don't actually want to
13966 perform a substitution here, because substitutions
13967 for argument packs are only done
13968 element-by-element. We can get to this point when
13969 substituting the type of a non-type template
13970 parameter pack, when that type actually contains
13971 template parameter packs from an outer template, e.g.,
13972
13973 template<typename... Types> struct A {
13974 template<Types... Values> struct B { };
13975 }; */
13976 return t;
13977
13978 if (code == TEMPLATE_TYPE_PARM)
13979 {
13980 int quals;
13981 gcc_assert (TYPE_P (arg));
13982
13983 quals = cp_type_quals (arg) | cp_type_quals (t);
13984
13985 return cp_build_qualified_type_real
13986 (arg, quals, complain | tf_ignore_bad_quals);
13987 }
13988 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13989 {
13990 /* We are processing a type constructed from a
13991 template template parameter. */
13992 tree argvec = tsubst (TYPE_TI_ARGS (t),
13993 args, complain, in_decl);
13994 if (argvec == error_mark_node)
13995 return error_mark_node;
13996
13997 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13998 || TREE_CODE (arg) == TEMPLATE_DECL
13999 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14000
14001 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14002 /* Consider this code:
14003
14004 template <template <class> class Template>
14005 struct Internal {
14006 template <class Arg> using Bind = Template<Arg>;
14007 };
14008
14009 template <template <class> class Template, class Arg>
14010 using Instantiate = Template<Arg>; //#0
14011
14012 template <template <class> class Template,
14013 class Argument>
14014 using Bind =
14015 Instantiate<Internal<Template>::template Bind,
14016 Argument>; //#1
14017
14018 When #1 is parsed, the
14019 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14020 parameter `Template' in #0 matches the
14021 UNBOUND_CLASS_TEMPLATE representing the argument
14022 `Internal<Template>::template Bind'; We then want
14023 to assemble the type `Bind<Argument>' that can't
14024 be fully created right now, because
14025 `Internal<Template>' not being complete, the Bind
14026 template cannot be looked up in that context. So
14027 we need to "store" `Bind<Argument>' for later
14028 when the context of Bind becomes complete. Let's
14029 store that in a TYPENAME_TYPE. */
14030 return make_typename_type (TYPE_CONTEXT (arg),
14031 build_nt (TEMPLATE_ID_EXPR,
14032 TYPE_IDENTIFIER (arg),
14033 argvec),
14034 typename_type,
14035 complain);
14036
14037 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14038 are resolving nested-types in the signature of a
14039 member function templates. Otherwise ARG is a
14040 TEMPLATE_DECL and is the real template to be
14041 instantiated. */
14042 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14043 arg = TYPE_NAME (arg);
14044
14045 r = lookup_template_class (arg,
14046 argvec, in_decl,
14047 DECL_CONTEXT (arg),
14048 /*entering_scope=*/0,
14049 complain);
14050 return cp_build_qualified_type_real
14051 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14052 }
14053 else if (code == TEMPLATE_TEMPLATE_PARM)
14054 return arg;
14055 else
14056 /* TEMPLATE_PARM_INDEX. */
14057 return convert_from_reference (unshare_expr (arg));
14058 }
14059
14060 if (level == 1)
14061 /* This can happen during the attempted tsubst'ing in
14062 unify. This means that we don't yet have any information
14063 about the template parameter in question. */
14064 return t;
14065
14066 /* If we get here, we must have been looking at a parm for a
14067 more deeply nested template. Make a new version of this
14068 template parameter, but with a lower level. */
14069 switch (code)
14070 {
14071 case TEMPLATE_TYPE_PARM:
14072 case TEMPLATE_TEMPLATE_PARM:
14073 case BOUND_TEMPLATE_TEMPLATE_PARM:
14074 if (cp_type_quals (t))
14075 {
14076 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14077 r = cp_build_qualified_type_real
14078 (r, cp_type_quals (t),
14079 complain | (code == TEMPLATE_TYPE_PARM
14080 ? tf_ignore_bad_quals : 0));
14081 }
14082 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14083 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14084 && (r = (TEMPLATE_PARM_DESCENDANTS
14085 (TEMPLATE_TYPE_PARM_INDEX (t))))
14086 && (r = TREE_TYPE (r))
14087 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14088 /* Break infinite recursion when substituting the constraints
14089 of a constrained placeholder. */;
14090 else
14091 {
14092 r = copy_type (t);
14093 TEMPLATE_TYPE_PARM_INDEX (r)
14094 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14095 r, levels, args, complain);
14096 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14097 TYPE_MAIN_VARIANT (r) = r;
14098 TYPE_POINTER_TO (r) = NULL_TREE;
14099 TYPE_REFERENCE_TO (r) = NULL_TREE;
14100
14101 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14102 {
14103 /* Propagate constraints on placeholders. */
14104 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14105 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14106 = tsubst_constraint (constr, args, complain, in_decl);
14107 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14108 {
14109 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
14110 pl = tsubst (pl, args, complain, in_decl);
14111 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14112 }
14113 }
14114
14115 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14116 /* We have reduced the level of the template
14117 template parameter, but not the levels of its
14118 template parameters, so canonical_type_parameter
14119 will not be able to find the canonical template
14120 template parameter for this level. Thus, we
14121 require structural equality checking to compare
14122 TEMPLATE_TEMPLATE_PARMs. */
14123 SET_TYPE_STRUCTURAL_EQUALITY (r);
14124 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14125 SET_TYPE_STRUCTURAL_EQUALITY (r);
14126 else
14127 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14128
14129 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14130 {
14131 tree tinfo = TYPE_TEMPLATE_INFO (t);
14132 /* We might need to substitute into the types of non-type
14133 template parameters. */
14134 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14135 complain, in_decl);
14136 if (tmpl == error_mark_node)
14137 return error_mark_node;
14138 tree argvec = tsubst (TI_ARGS (tinfo), args,
14139 complain, in_decl);
14140 if (argvec == error_mark_node)
14141 return error_mark_node;
14142
14143 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14144 = build_template_info (tmpl, argvec);
14145 }
14146 }
14147 break;
14148
14149 case TEMPLATE_PARM_INDEX:
14150 /* OK, now substitute the type of the non-type parameter. We
14151 couldn't do it earlier because it might be an auto parameter,
14152 and we wouldn't need to if we had an argument. */
14153 type = tsubst (type, args, complain, in_decl);
14154 if (type == error_mark_node)
14155 return error_mark_node;
14156 r = reduce_template_parm_level (t, type, levels, args, complain);
14157 break;
14158
14159 default:
14160 gcc_unreachable ();
14161 }
14162
14163 return r;
14164 }
14165
14166 case TREE_LIST:
14167 {
14168 tree purpose, value, chain;
14169
14170 if (t == void_list_node)
14171 return t;
14172
14173 purpose = TREE_PURPOSE (t);
14174 if (purpose)
14175 {
14176 purpose = tsubst (purpose, args, complain, in_decl);
14177 if (purpose == error_mark_node)
14178 return error_mark_node;
14179 }
14180 value = TREE_VALUE (t);
14181 if (value)
14182 {
14183 value = tsubst (value, args, complain, in_decl);
14184 if (value == error_mark_node)
14185 return error_mark_node;
14186 }
14187 chain = TREE_CHAIN (t);
14188 if (chain && chain != void_type_node)
14189 {
14190 chain = tsubst (chain, args, complain, in_decl);
14191 if (chain == error_mark_node)
14192 return error_mark_node;
14193 }
14194 if (purpose == TREE_PURPOSE (t)
14195 && value == TREE_VALUE (t)
14196 && chain == TREE_CHAIN (t))
14197 return t;
14198 return hash_tree_cons (purpose, value, chain);
14199 }
14200
14201 case TREE_BINFO:
14202 /* We should never be tsubsting a binfo. */
14203 gcc_unreachable ();
14204
14205 case TREE_VEC:
14206 /* A vector of template arguments. */
14207 gcc_assert (!type);
14208 return tsubst_template_args (t, args, complain, in_decl);
14209
14210 case POINTER_TYPE:
14211 case REFERENCE_TYPE:
14212 {
14213 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14214 return t;
14215
14216 /* [temp.deduct]
14217
14218 Type deduction may fail for any of the following
14219 reasons:
14220
14221 -- Attempting to create a pointer to reference type.
14222 -- Attempting to create a reference to a reference type or
14223 a reference to void.
14224
14225 Core issue 106 says that creating a reference to a reference
14226 during instantiation is no longer a cause for failure. We
14227 only enforce this check in strict C++98 mode. */
14228 if ((TREE_CODE (type) == REFERENCE_TYPE
14229 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14230 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14231 {
14232 static location_t last_loc;
14233
14234 /* We keep track of the last time we issued this error
14235 message to avoid spewing a ton of messages during a
14236 single bad template instantiation. */
14237 if (complain & tf_error
14238 && last_loc != input_location)
14239 {
14240 if (VOID_TYPE_P (type))
14241 error ("forming reference to void");
14242 else if (code == POINTER_TYPE)
14243 error ("forming pointer to reference type %qT", type);
14244 else
14245 error ("forming reference to reference type %qT", type);
14246 last_loc = input_location;
14247 }
14248
14249 return error_mark_node;
14250 }
14251 else if (TREE_CODE (type) == FUNCTION_TYPE
14252 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14253 || type_memfn_rqual (type) != REF_QUAL_NONE))
14254 {
14255 if (complain & tf_error)
14256 {
14257 if (code == POINTER_TYPE)
14258 error ("forming pointer to qualified function type %qT",
14259 type);
14260 else
14261 error ("forming reference to qualified function type %qT",
14262 type);
14263 }
14264 return error_mark_node;
14265 }
14266 else if (code == POINTER_TYPE)
14267 {
14268 r = build_pointer_type (type);
14269 if (TREE_CODE (type) == METHOD_TYPE)
14270 r = build_ptrmemfunc_type (r);
14271 }
14272 else if (TREE_CODE (type) == REFERENCE_TYPE)
14273 /* In C++0x, during template argument substitution, when there is an
14274 attempt to create a reference to a reference type, reference
14275 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14276
14277 "If a template-argument for a template-parameter T names a type
14278 that is a reference to a type A, an attempt to create the type
14279 'lvalue reference to cv T' creates the type 'lvalue reference to
14280 A,' while an attempt to create the type type rvalue reference to
14281 cv T' creates the type T"
14282 */
14283 r = cp_build_reference_type
14284 (TREE_TYPE (type),
14285 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14286 else
14287 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14288 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14289
14290 if (r != error_mark_node)
14291 /* Will this ever be needed for TYPE_..._TO values? */
14292 layout_type (r);
14293
14294 return r;
14295 }
14296 case OFFSET_TYPE:
14297 {
14298 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14299 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14300 {
14301 /* [temp.deduct]
14302
14303 Type deduction may fail for any of the following
14304 reasons:
14305
14306 -- Attempting to create "pointer to member of T" when T
14307 is not a class type. */
14308 if (complain & tf_error)
14309 error ("creating pointer to member of non-class type %qT", r);
14310 return error_mark_node;
14311 }
14312 if (TREE_CODE (type) == REFERENCE_TYPE)
14313 {
14314 if (complain & tf_error)
14315 error ("creating pointer to member reference type %qT", type);
14316 return error_mark_node;
14317 }
14318 if (VOID_TYPE_P (type))
14319 {
14320 if (complain & tf_error)
14321 error ("creating pointer to member of type void");
14322 return error_mark_node;
14323 }
14324 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14325 if (TREE_CODE (type) == FUNCTION_TYPE)
14326 {
14327 /* The type of the implicit object parameter gets its
14328 cv-qualifiers from the FUNCTION_TYPE. */
14329 tree memptr;
14330 tree method_type
14331 = build_memfn_type (type, r, type_memfn_quals (type),
14332 type_memfn_rqual (type));
14333 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14334 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14335 complain);
14336 }
14337 else
14338 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14339 cp_type_quals (t),
14340 complain);
14341 }
14342 case FUNCTION_TYPE:
14343 case METHOD_TYPE:
14344 {
14345 tree fntype;
14346 tree specs;
14347 fntype = tsubst_function_type (t, args, complain, in_decl);
14348 if (fntype == error_mark_node)
14349 return error_mark_node;
14350
14351 /* Substitute the exception specification. */
14352 specs = tsubst_exception_specification (t, args, complain, in_decl,
14353 /*defer_ok*/fndecl_type);
14354 if (specs == error_mark_node)
14355 return error_mark_node;
14356 if (specs)
14357 fntype = build_exception_variant (fntype, specs);
14358 return fntype;
14359 }
14360 case ARRAY_TYPE:
14361 {
14362 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14363 if (domain == error_mark_node)
14364 return error_mark_node;
14365
14366 /* As an optimization, we avoid regenerating the array type if
14367 it will obviously be the same as T. */
14368 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14369 return t;
14370
14371 /* These checks should match the ones in create_array_type_for_decl.
14372
14373 [temp.deduct]
14374
14375 The deduction may fail for any of the following reasons:
14376
14377 -- Attempting to create an array with an element type that
14378 is void, a function type, or a reference type, or [DR337]
14379 an abstract class type. */
14380 if (VOID_TYPE_P (type)
14381 || TREE_CODE (type) == FUNCTION_TYPE
14382 || (TREE_CODE (type) == ARRAY_TYPE
14383 && TYPE_DOMAIN (type) == NULL_TREE)
14384 || TREE_CODE (type) == REFERENCE_TYPE)
14385 {
14386 if (complain & tf_error)
14387 error ("creating array of %qT", type);
14388 return error_mark_node;
14389 }
14390
14391 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14392 return error_mark_node;
14393
14394 r = build_cplus_array_type (type, domain);
14395
14396 if (TYPE_USER_ALIGN (t))
14397 {
14398 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14399 TYPE_USER_ALIGN (r) = 1;
14400 }
14401
14402 return r;
14403 }
14404
14405 case TYPENAME_TYPE:
14406 {
14407 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14408 in_decl, /*entering_scope=*/1);
14409 if (ctx == error_mark_node)
14410 return error_mark_node;
14411
14412 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14413 complain, in_decl);
14414 if (f == error_mark_node)
14415 return error_mark_node;
14416
14417 if (!MAYBE_CLASS_TYPE_P (ctx))
14418 {
14419 if (complain & tf_error)
14420 error ("%qT is not a class, struct, or union type", ctx);
14421 return error_mark_node;
14422 }
14423 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14424 {
14425 /* Normally, make_typename_type does not require that the CTX
14426 have complete type in order to allow things like:
14427
14428 template <class T> struct S { typename S<T>::X Y; };
14429
14430 But, such constructs have already been resolved by this
14431 point, so here CTX really should have complete type, unless
14432 it's a partial instantiation. */
14433 ctx = complete_type (ctx);
14434 if (!COMPLETE_TYPE_P (ctx))
14435 {
14436 if (complain & tf_error)
14437 cxx_incomplete_type_error (NULL_TREE, ctx);
14438 return error_mark_node;
14439 }
14440 }
14441
14442 f = make_typename_type (ctx, f, typename_type,
14443 complain | tf_keep_type_decl);
14444 if (f == error_mark_node)
14445 return f;
14446 if (TREE_CODE (f) == TYPE_DECL)
14447 {
14448 complain |= tf_ignore_bad_quals;
14449 f = TREE_TYPE (f);
14450 }
14451
14452 if (TREE_CODE (f) != TYPENAME_TYPE)
14453 {
14454 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14455 {
14456 if (complain & tf_error)
14457 error ("%qT resolves to %qT, which is not an enumeration type",
14458 t, f);
14459 else
14460 return error_mark_node;
14461 }
14462 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14463 {
14464 if (complain & tf_error)
14465 error ("%qT resolves to %qT, which is is not a class type",
14466 t, f);
14467 else
14468 return error_mark_node;
14469 }
14470 }
14471
14472 return cp_build_qualified_type_real
14473 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14474 }
14475
14476 case UNBOUND_CLASS_TEMPLATE:
14477 {
14478 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14479 in_decl, /*entering_scope=*/1);
14480 tree name = TYPE_IDENTIFIER (t);
14481 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14482
14483 if (ctx == error_mark_node || name == error_mark_node)
14484 return error_mark_node;
14485
14486 if (parm_list)
14487 parm_list = tsubst_template_parms (parm_list, args, complain);
14488 return make_unbound_class_template (ctx, name, parm_list, complain);
14489 }
14490
14491 case TYPEOF_TYPE:
14492 {
14493 tree type;
14494
14495 ++cp_unevaluated_operand;
14496 ++c_inhibit_evaluation_warnings;
14497
14498 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14499 complain, in_decl,
14500 /*integral_constant_expression_p=*/false);
14501
14502 --cp_unevaluated_operand;
14503 --c_inhibit_evaluation_warnings;
14504
14505 type = finish_typeof (type);
14506 return cp_build_qualified_type_real (type,
14507 cp_type_quals (t)
14508 | cp_type_quals (type),
14509 complain);
14510 }
14511
14512 case DECLTYPE_TYPE:
14513 {
14514 tree type;
14515
14516 ++cp_unevaluated_operand;
14517 ++c_inhibit_evaluation_warnings;
14518
14519 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14520 complain|tf_decltype, in_decl,
14521 /*function_p*/false,
14522 /*integral_constant_expression*/false);
14523
14524 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14525 {
14526 if (type == NULL_TREE)
14527 {
14528 if (complain & tf_error)
14529 error ("empty initializer in lambda init-capture");
14530 type = error_mark_node;
14531 }
14532 else if (TREE_CODE (type) == TREE_LIST)
14533 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14534 }
14535
14536 --cp_unevaluated_operand;
14537 --c_inhibit_evaluation_warnings;
14538
14539 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14540 type = lambda_capture_field_type (type,
14541 DECLTYPE_FOR_INIT_CAPTURE (t),
14542 DECLTYPE_FOR_REF_CAPTURE (t));
14543 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14544 type = lambda_proxy_type (type);
14545 else
14546 {
14547 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14548 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14549 && EXPR_P (type))
14550 /* In a template ~id could be either a complement expression
14551 or an unqualified-id naming a destructor; if instantiating
14552 it produces an expression, it's not an id-expression or
14553 member access. */
14554 id = false;
14555 type = finish_decltype_type (type, id, complain);
14556 }
14557 return cp_build_qualified_type_real (type,
14558 cp_type_quals (t)
14559 | cp_type_quals (type),
14560 complain | tf_ignore_bad_quals);
14561 }
14562
14563 case UNDERLYING_TYPE:
14564 {
14565 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14566 complain, in_decl);
14567 return finish_underlying_type (type);
14568 }
14569
14570 case TYPE_ARGUMENT_PACK:
14571 case NONTYPE_ARGUMENT_PACK:
14572 {
14573 tree r;
14574
14575 if (code == NONTYPE_ARGUMENT_PACK)
14576 r = make_node (code);
14577 else
14578 r = cxx_make_type (code);
14579
14580 tree pack_args = ARGUMENT_PACK_ARGS (t);
14581 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14582 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14583
14584 return r;
14585 }
14586
14587 case VOID_CST:
14588 case INTEGER_CST:
14589 case REAL_CST:
14590 case STRING_CST:
14591 case PLUS_EXPR:
14592 case MINUS_EXPR:
14593 case NEGATE_EXPR:
14594 case NOP_EXPR:
14595 case INDIRECT_REF:
14596 case ADDR_EXPR:
14597 case CALL_EXPR:
14598 case ARRAY_REF:
14599 case SCOPE_REF:
14600 /* We should use one of the expression tsubsts for these codes. */
14601 gcc_unreachable ();
14602
14603 default:
14604 sorry ("use of %qs in template", get_tree_code_name (code));
14605 return error_mark_node;
14606 }
14607 }
14608
14609 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14610 expression on the left-hand side of the "." or "->" operator. We
14611 only do the lookup if we had a dependent BASELINK. Otherwise we
14612 adjust it onto the instantiated heirarchy. */
14613
14614 static tree
14615 tsubst_baselink (tree baselink, tree object_type,
14616 tree args, tsubst_flags_t complain, tree in_decl)
14617 {
14618 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
14619 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14620 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14621
14622 tree optype = BASELINK_OPTYPE (baselink);
14623 optype = tsubst (optype, args, complain, in_decl);
14624
14625 tree template_args = NULL_TREE;
14626 bool template_id_p = false;
14627 tree fns = BASELINK_FUNCTIONS (baselink);
14628 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14629 {
14630 template_id_p = true;
14631 template_args = TREE_OPERAND (fns, 1);
14632 fns = TREE_OPERAND (fns, 0);
14633 if (template_args)
14634 template_args = tsubst_template_args (template_args, args,
14635 complain, in_decl);
14636 }
14637
14638 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
14639 binfo_type = tsubst (binfo_type, args, complain, in_decl);
14640 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
14641
14642 if (dependent_p)
14643 {
14644 tree name = OVL_NAME (fns);
14645 if (IDENTIFIER_CONV_OP_P (name))
14646 name = make_conv_op_name (optype);
14647
14648 if (name == complete_dtor_identifier)
14649 /* Treat as-if non-dependent below. */
14650 dependent_p = false;
14651
14652 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14653 if (!baselink)
14654 {
14655 if ((complain & tf_error)
14656 && constructor_name_p (name, qualifying_scope))
14657 error ("cannot call constructor %<%T::%D%> directly",
14658 qualifying_scope, name);
14659 return error_mark_node;
14660 }
14661
14662 if (BASELINK_P (baselink))
14663 fns = BASELINK_FUNCTIONS (baselink);
14664 }
14665 else
14666 /* We're going to overwrite pieces below, make a duplicate. */
14667 baselink = copy_node (baselink);
14668
14669 /* If lookup found a single function, mark it as used at this point.
14670 (If lookup found multiple functions the one selected later by
14671 overload resolution will be marked as used at that point.) */
14672 if (!template_id_p && !really_overloaded_fn (fns)
14673 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14674 return error_mark_node;
14675
14676 if (BASELINK_P (baselink))
14677 {
14678 /* Add back the template arguments, if present. */
14679 if (template_id_p)
14680 BASELINK_FUNCTIONS (baselink)
14681 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
14682
14683 /* Update the conversion operator type. */
14684 BASELINK_OPTYPE (baselink) = optype;
14685 }
14686
14687 if (!object_type)
14688 object_type = current_class_type;
14689
14690 if (qualified_p || !dependent_p)
14691 {
14692 baselink = adjust_result_of_qualified_name_lookup (baselink,
14693 qualifying_scope,
14694 object_type);
14695 if (!qualified_p)
14696 /* We need to call adjust_result_of_qualified_name_lookup in case the
14697 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14698 so that we still get virtual function binding. */
14699 BASELINK_QUALIFIED_P (baselink) = false;
14700 }
14701
14702 return baselink;
14703 }
14704
14705 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14706 true if the qualified-id will be a postfix-expression in-and-of
14707 itself; false if more of the postfix-expression follows the
14708 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14709 of "&". */
14710
14711 static tree
14712 tsubst_qualified_id (tree qualified_id, tree args,
14713 tsubst_flags_t complain, tree in_decl,
14714 bool done, bool address_p)
14715 {
14716 tree expr;
14717 tree scope;
14718 tree name;
14719 bool is_template;
14720 tree template_args;
14721 location_t loc = UNKNOWN_LOCATION;
14722
14723 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14724
14725 /* Figure out what name to look up. */
14726 name = TREE_OPERAND (qualified_id, 1);
14727 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14728 {
14729 is_template = true;
14730 loc = EXPR_LOCATION (name);
14731 template_args = TREE_OPERAND (name, 1);
14732 if (template_args)
14733 template_args = tsubst_template_args (template_args, args,
14734 complain, in_decl);
14735 if (template_args == error_mark_node)
14736 return error_mark_node;
14737 name = TREE_OPERAND (name, 0);
14738 }
14739 else
14740 {
14741 is_template = false;
14742 template_args = NULL_TREE;
14743 }
14744
14745 /* Substitute into the qualifying scope. When there are no ARGS, we
14746 are just trying to simplify a non-dependent expression. In that
14747 case the qualifying scope may be dependent, and, in any case,
14748 substituting will not help. */
14749 scope = TREE_OPERAND (qualified_id, 0);
14750 if (args)
14751 {
14752 scope = tsubst (scope, args, complain, in_decl);
14753 expr = tsubst_copy (name, args, complain, in_decl);
14754 }
14755 else
14756 expr = name;
14757
14758 if (dependent_scope_p (scope))
14759 {
14760 if (is_template)
14761 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14762 tree r = build_qualified_name (NULL_TREE, scope, expr,
14763 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14764 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14765 return r;
14766 }
14767
14768 if (!BASELINK_P (name) && !DECL_P (expr))
14769 {
14770 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14771 {
14772 /* A BIT_NOT_EXPR is used to represent a destructor. */
14773 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14774 {
14775 error ("qualifying type %qT does not match destructor name ~%qT",
14776 scope, TREE_OPERAND (expr, 0));
14777 expr = error_mark_node;
14778 }
14779 else
14780 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14781 /*is_type_p=*/0, false);
14782 }
14783 else
14784 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14785 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14786 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14787 {
14788 if (complain & tf_error)
14789 {
14790 error ("dependent-name %qE is parsed as a non-type, but "
14791 "instantiation yields a type", qualified_id);
14792 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14793 }
14794 return error_mark_node;
14795 }
14796 }
14797
14798 if (DECL_P (expr))
14799 {
14800 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14801 scope);
14802 /* Remember that there was a reference to this entity. */
14803 if (!mark_used (expr, complain) && !(complain & tf_error))
14804 return error_mark_node;
14805 }
14806
14807 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14808 {
14809 if (complain & tf_error)
14810 qualified_name_lookup_error (scope,
14811 TREE_OPERAND (qualified_id, 1),
14812 expr, input_location);
14813 return error_mark_node;
14814 }
14815
14816 if (is_template)
14817 {
14818 if (variable_template_p (expr))
14819 expr = lookup_and_finish_template_variable (expr, template_args,
14820 complain);
14821 else
14822 expr = lookup_template_function (expr, template_args);
14823 }
14824
14825 if (expr == error_mark_node && complain & tf_error)
14826 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14827 expr, input_location);
14828 else if (TYPE_P (scope))
14829 {
14830 expr = (adjust_result_of_qualified_name_lookup
14831 (expr, scope, current_nonlambda_class_type ()));
14832 expr = (finish_qualified_id_expr
14833 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14834 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14835 /*template_arg_p=*/false, complain));
14836 }
14837
14838 /* Expressions do not generally have reference type. */
14839 if (TREE_CODE (expr) != SCOPE_REF
14840 /* However, if we're about to form a pointer-to-member, we just
14841 want the referenced member referenced. */
14842 && TREE_CODE (expr) != OFFSET_REF)
14843 expr = convert_from_reference (expr);
14844
14845 if (REF_PARENTHESIZED_P (qualified_id))
14846 expr = force_paren_expr (expr);
14847
14848 return expr;
14849 }
14850
14851 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14852 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14853 for tsubst. */
14854
14855 static tree
14856 tsubst_init (tree init, tree decl, tree args,
14857 tsubst_flags_t complain, tree in_decl)
14858 {
14859 if (!init)
14860 return NULL_TREE;
14861
14862 init = tsubst_expr (init, args, complain, in_decl, false);
14863
14864 if (!init && TREE_TYPE (decl) != error_mark_node)
14865 {
14866 /* If we had an initializer but it
14867 instantiated to nothing,
14868 value-initialize the object. This will
14869 only occur when the initializer was a
14870 pack expansion where the parameter packs
14871 used in that expansion were of length
14872 zero. */
14873 init = build_value_init (TREE_TYPE (decl),
14874 complain);
14875 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14876 init = get_target_expr_sfinae (init, complain);
14877 if (TREE_CODE (init) == TARGET_EXPR)
14878 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14879 }
14880
14881 return init;
14882 }
14883
14884 /* Like tsubst, but deals with expressions. This function just replaces
14885 template parms; to finish processing the resultant expression, use
14886 tsubst_copy_and_build or tsubst_expr. */
14887
14888 static tree
14889 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14890 {
14891 enum tree_code code;
14892 tree r;
14893
14894 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14895 return t;
14896
14897 code = TREE_CODE (t);
14898
14899 switch (code)
14900 {
14901 case PARM_DECL:
14902 r = retrieve_local_specialization (t);
14903
14904 if (r == NULL_TREE)
14905 {
14906 /* We get here for a use of 'this' in an NSDMI. */
14907 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14908 return current_class_ptr;
14909
14910 /* This can happen for a parameter name used later in a function
14911 declaration (such as in a late-specified return type). Just
14912 make a dummy decl, since it's only used for its type. */
14913 gcc_assert (cp_unevaluated_operand != 0);
14914 r = tsubst_decl (t, args, complain);
14915 /* Give it the template pattern as its context; its true context
14916 hasn't been instantiated yet and this is good enough for
14917 mangling. */
14918 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14919 }
14920
14921 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14922 r = argument_pack_select_arg (r);
14923 if (!mark_used (r, complain) && !(complain & tf_error))
14924 return error_mark_node;
14925 return r;
14926
14927 case CONST_DECL:
14928 {
14929 tree enum_type;
14930 tree v;
14931
14932 if (DECL_TEMPLATE_PARM_P (t))
14933 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14934 /* There is no need to substitute into namespace-scope
14935 enumerators. */
14936 if (DECL_NAMESPACE_SCOPE_P (t))
14937 return t;
14938 /* If ARGS is NULL, then T is known to be non-dependent. */
14939 if (args == NULL_TREE)
14940 return scalar_constant_value (t);
14941
14942 /* Unfortunately, we cannot just call lookup_name here.
14943 Consider:
14944
14945 template <int I> int f() {
14946 enum E { a = I };
14947 struct S { void g() { E e = a; } };
14948 };
14949
14950 When we instantiate f<7>::S::g(), say, lookup_name is not
14951 clever enough to find f<7>::a. */
14952 enum_type
14953 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14954 /*entering_scope=*/0);
14955
14956 for (v = TYPE_VALUES (enum_type);
14957 v != NULL_TREE;
14958 v = TREE_CHAIN (v))
14959 if (TREE_PURPOSE (v) == DECL_NAME (t))
14960 return TREE_VALUE (v);
14961
14962 /* We didn't find the name. That should never happen; if
14963 name-lookup found it during preliminary parsing, we
14964 should find it again here during instantiation. */
14965 gcc_unreachable ();
14966 }
14967 return t;
14968
14969 case FIELD_DECL:
14970 if (DECL_CONTEXT (t))
14971 {
14972 tree ctx;
14973
14974 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14975 /*entering_scope=*/1);
14976 if (ctx != DECL_CONTEXT (t))
14977 {
14978 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14979 if (!r)
14980 {
14981 if (complain & tf_error)
14982 error ("using invalid field %qD", t);
14983 return error_mark_node;
14984 }
14985 return r;
14986 }
14987 }
14988
14989 return t;
14990
14991 case VAR_DECL:
14992 case FUNCTION_DECL:
14993 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14994 r = tsubst (t, args, complain, in_decl);
14995 else if (local_variable_p (t)
14996 && uses_template_parms (DECL_CONTEXT (t)))
14997 {
14998 r = retrieve_local_specialization (t);
14999 if (r == NULL_TREE)
15000 {
15001 /* First try name lookup to find the instantiation. */
15002 r = lookup_name (DECL_NAME (t));
15003 if (r && !is_capture_proxy (r))
15004 {
15005 /* Make sure that the one we found is the one we want. */
15006 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15007 if (ctx != DECL_CONTEXT (r))
15008 r = NULL_TREE;
15009 }
15010
15011 if (r)
15012 /* OK */;
15013 else
15014 {
15015 /* This can happen for a variable used in a
15016 late-specified return type of a local lambda, or for a
15017 local static or constant. Building a new VAR_DECL
15018 should be OK in all those cases. */
15019 r = tsubst_decl (t, args, complain);
15020 if (local_specializations)
15021 /* Avoid infinite recursion (79640). */
15022 register_local_specialization (r, t);
15023 if (decl_maybe_constant_var_p (r))
15024 {
15025 /* We can't call cp_finish_decl, so handle the
15026 initializer by hand. */
15027 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15028 complain, in_decl);
15029 if (!processing_template_decl)
15030 init = maybe_constant_init (init);
15031 if (processing_template_decl
15032 ? potential_constant_expression (init)
15033 : reduced_constant_expression_p (init))
15034 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15035 = TREE_CONSTANT (r) = true;
15036 DECL_INITIAL (r) = init;
15037 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15038 TREE_TYPE (r)
15039 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15040 complain, adc_variable_type);
15041 }
15042 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15043 || decl_constant_var_p (r)
15044 || errorcount || sorrycount);
15045 if (!processing_template_decl
15046 && !TREE_STATIC (r))
15047 r = process_outer_var_ref (r, complain);
15048 }
15049 /* Remember this for subsequent uses. */
15050 if (local_specializations)
15051 register_local_specialization (r, t);
15052 }
15053 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15054 r = argument_pack_select_arg (r);
15055 }
15056 else
15057 r = t;
15058 if (!mark_used (r, complain))
15059 return error_mark_node;
15060 return r;
15061
15062 case NAMESPACE_DECL:
15063 return t;
15064
15065 case OVERLOAD:
15066 /* An OVERLOAD will always be a non-dependent overload set; an
15067 overload set from function scope will just be represented with an
15068 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15069 gcc_assert (!uses_template_parms (t));
15070 /* We must have marked any lookups as persistent. */
15071 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
15072 return t;
15073
15074 case BASELINK:
15075 return tsubst_baselink (t, current_nonlambda_class_type (),
15076 args, complain, in_decl);
15077
15078 case TEMPLATE_DECL:
15079 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15080 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15081 args, complain, in_decl);
15082 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15083 return tsubst (t, args, complain, in_decl);
15084 else if (DECL_CLASS_SCOPE_P (t)
15085 && uses_template_parms (DECL_CONTEXT (t)))
15086 {
15087 /* Template template argument like the following example need
15088 special treatment:
15089
15090 template <template <class> class TT> struct C {};
15091 template <class T> struct D {
15092 template <class U> struct E {};
15093 C<E> c; // #1
15094 };
15095 D<int> d; // #2
15096
15097 We are processing the template argument `E' in #1 for
15098 the template instantiation #2. Originally, `E' is a
15099 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15100 have to substitute this with one having context `D<int>'. */
15101
15102 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15103 if (dependent_scope_p (context))
15104 {
15105 /* When rewriting a constructor into a deduction guide, a
15106 non-dependent name can become dependent, so memtmpl<args>
15107 becomes context::template memtmpl<args>. */
15108 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15109 return build_qualified_name (type, context, DECL_NAME (t),
15110 /*template*/true);
15111 }
15112 return lookup_field (context, DECL_NAME(t), 0, false);
15113 }
15114 else
15115 /* Ordinary template template argument. */
15116 return t;
15117
15118 case NON_LVALUE_EXPR:
15119 case VIEW_CONVERT_EXPR:
15120 {
15121 /* Handle location wrappers by substituting the wrapped node
15122 first, *then* reusing the resulting type. Doing the type
15123 first ensures that we handle template parameters and
15124 parameter pack expansions. */
15125 gcc_assert (location_wrapper_p (t));
15126 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15127 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15128 }
15129
15130 case CAST_EXPR:
15131 case REINTERPRET_CAST_EXPR:
15132 case CONST_CAST_EXPR:
15133 case STATIC_CAST_EXPR:
15134 case DYNAMIC_CAST_EXPR:
15135 case IMPLICIT_CONV_EXPR:
15136 case CONVERT_EXPR:
15137 case NOP_EXPR:
15138 {
15139 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15140 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15141 return build1 (code, type, op0);
15142 }
15143
15144 case SIZEOF_EXPR:
15145 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15146 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15147 {
15148 tree expanded, op = TREE_OPERAND (t, 0);
15149 int len = 0;
15150
15151 if (SIZEOF_EXPR_TYPE_P (t))
15152 op = TREE_TYPE (op);
15153
15154 ++cp_unevaluated_operand;
15155 ++c_inhibit_evaluation_warnings;
15156 /* We only want to compute the number of arguments. */
15157 if (PACK_EXPANSION_P (op))
15158 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15159 else
15160 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15161 args, complain, in_decl);
15162 --cp_unevaluated_operand;
15163 --c_inhibit_evaluation_warnings;
15164
15165 if (TREE_CODE (expanded) == TREE_VEC)
15166 {
15167 len = TREE_VEC_LENGTH (expanded);
15168 /* Set TREE_USED for the benefit of -Wunused. */
15169 for (int i = 0; i < len; i++)
15170 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15171 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15172 }
15173
15174 if (expanded == error_mark_node)
15175 return error_mark_node;
15176 else if (PACK_EXPANSION_P (expanded)
15177 || (TREE_CODE (expanded) == TREE_VEC
15178 && pack_expansion_args_count (expanded)))
15179
15180 {
15181 if (PACK_EXPANSION_P (expanded))
15182 /* OK. */;
15183 else if (TREE_VEC_LENGTH (expanded) == 1)
15184 expanded = TREE_VEC_ELT (expanded, 0);
15185 else
15186 expanded = make_argument_pack (expanded);
15187
15188 if (TYPE_P (expanded))
15189 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15190 complain & tf_error);
15191 else
15192 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15193 complain & tf_error);
15194 }
15195 else
15196 return build_int_cst (size_type_node, len);
15197 }
15198 if (SIZEOF_EXPR_TYPE_P (t))
15199 {
15200 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15201 args, complain, in_decl);
15202 r = build1 (NOP_EXPR, r, error_mark_node);
15203 r = build1 (SIZEOF_EXPR,
15204 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15205 SIZEOF_EXPR_TYPE_P (r) = 1;
15206 return r;
15207 }
15208 /* Fall through */
15209
15210 case INDIRECT_REF:
15211 case NEGATE_EXPR:
15212 case TRUTH_NOT_EXPR:
15213 case BIT_NOT_EXPR:
15214 case ADDR_EXPR:
15215 case UNARY_PLUS_EXPR: /* Unary + */
15216 case ALIGNOF_EXPR:
15217 case AT_ENCODE_EXPR:
15218 case ARROW_EXPR:
15219 case THROW_EXPR:
15220 case TYPEID_EXPR:
15221 case REALPART_EXPR:
15222 case IMAGPART_EXPR:
15223 case PAREN_EXPR:
15224 {
15225 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15226 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15227 return build1 (code, type, op0);
15228 }
15229
15230 case COMPONENT_REF:
15231 {
15232 tree object;
15233 tree name;
15234
15235 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15236 name = TREE_OPERAND (t, 1);
15237 if (TREE_CODE (name) == BIT_NOT_EXPR)
15238 {
15239 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15240 complain, in_decl);
15241 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15242 }
15243 else if (TREE_CODE (name) == SCOPE_REF
15244 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15245 {
15246 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15247 complain, in_decl);
15248 name = TREE_OPERAND (name, 1);
15249 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15250 complain, in_decl);
15251 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15252 name = build_qualified_name (/*type=*/NULL_TREE,
15253 base, name,
15254 /*template_p=*/false);
15255 }
15256 else if (BASELINK_P (name))
15257 name = tsubst_baselink (name,
15258 non_reference (TREE_TYPE (object)),
15259 args, complain,
15260 in_decl);
15261 else
15262 name = tsubst_copy (name, args, complain, in_decl);
15263 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15264 }
15265
15266 case PLUS_EXPR:
15267 case MINUS_EXPR:
15268 case MULT_EXPR:
15269 case TRUNC_DIV_EXPR:
15270 case CEIL_DIV_EXPR:
15271 case FLOOR_DIV_EXPR:
15272 case ROUND_DIV_EXPR:
15273 case EXACT_DIV_EXPR:
15274 case BIT_AND_EXPR:
15275 case BIT_IOR_EXPR:
15276 case BIT_XOR_EXPR:
15277 case TRUNC_MOD_EXPR:
15278 case FLOOR_MOD_EXPR:
15279 case TRUTH_ANDIF_EXPR:
15280 case TRUTH_ORIF_EXPR:
15281 case TRUTH_AND_EXPR:
15282 case TRUTH_OR_EXPR:
15283 case RSHIFT_EXPR:
15284 case LSHIFT_EXPR:
15285 case RROTATE_EXPR:
15286 case LROTATE_EXPR:
15287 case EQ_EXPR:
15288 case NE_EXPR:
15289 case MAX_EXPR:
15290 case MIN_EXPR:
15291 case LE_EXPR:
15292 case GE_EXPR:
15293 case LT_EXPR:
15294 case GT_EXPR:
15295 case COMPOUND_EXPR:
15296 case DOTSTAR_EXPR:
15297 case MEMBER_REF:
15298 case PREDECREMENT_EXPR:
15299 case PREINCREMENT_EXPR:
15300 case POSTDECREMENT_EXPR:
15301 case POSTINCREMENT_EXPR:
15302 {
15303 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15304 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15305 return build_nt (code, op0, op1);
15306 }
15307
15308 case SCOPE_REF:
15309 {
15310 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15311 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15312 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15313 QUALIFIED_NAME_IS_TEMPLATE (t));
15314 }
15315
15316 case ARRAY_REF:
15317 {
15318 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15319 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15320 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15321 }
15322
15323 case CALL_EXPR:
15324 {
15325 int n = VL_EXP_OPERAND_LENGTH (t);
15326 tree result = build_vl_exp (CALL_EXPR, n);
15327 int i;
15328 for (i = 0; i < n; i++)
15329 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15330 complain, in_decl);
15331 return result;
15332 }
15333
15334 case COND_EXPR:
15335 case MODOP_EXPR:
15336 case PSEUDO_DTOR_EXPR:
15337 case VEC_PERM_EXPR:
15338 {
15339 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15340 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15341 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15342 r = build_nt (code, op0, op1, op2);
15343 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15344 return r;
15345 }
15346
15347 case NEW_EXPR:
15348 {
15349 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15350 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15351 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15352 r = build_nt (code, op0, op1, op2);
15353 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15354 return r;
15355 }
15356
15357 case DELETE_EXPR:
15358 {
15359 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15360 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15361 r = build_nt (code, op0, op1);
15362 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15363 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15364 return r;
15365 }
15366
15367 case TEMPLATE_ID_EXPR:
15368 {
15369 /* Substituted template arguments */
15370 tree fn = TREE_OPERAND (t, 0);
15371 tree targs = TREE_OPERAND (t, 1);
15372
15373 fn = tsubst_copy (fn, args, complain, in_decl);
15374 if (targs)
15375 targs = tsubst_template_args (targs, args, complain, in_decl);
15376
15377 return lookup_template_function (fn, targs);
15378 }
15379
15380 case TREE_LIST:
15381 {
15382 tree purpose, value, chain;
15383
15384 if (t == void_list_node)
15385 return t;
15386
15387 purpose = TREE_PURPOSE (t);
15388 if (purpose)
15389 purpose = tsubst_copy (purpose, args, complain, in_decl);
15390 value = TREE_VALUE (t);
15391 if (value)
15392 value = tsubst_copy (value, args, complain, in_decl);
15393 chain = TREE_CHAIN (t);
15394 if (chain && chain != void_type_node)
15395 chain = tsubst_copy (chain, args, complain, in_decl);
15396 if (purpose == TREE_PURPOSE (t)
15397 && value == TREE_VALUE (t)
15398 && chain == TREE_CHAIN (t))
15399 return t;
15400 return tree_cons (purpose, value, chain);
15401 }
15402
15403 case RECORD_TYPE:
15404 case UNION_TYPE:
15405 case ENUMERAL_TYPE:
15406 case INTEGER_TYPE:
15407 case TEMPLATE_TYPE_PARM:
15408 case TEMPLATE_TEMPLATE_PARM:
15409 case BOUND_TEMPLATE_TEMPLATE_PARM:
15410 case TEMPLATE_PARM_INDEX:
15411 case POINTER_TYPE:
15412 case REFERENCE_TYPE:
15413 case OFFSET_TYPE:
15414 case FUNCTION_TYPE:
15415 case METHOD_TYPE:
15416 case ARRAY_TYPE:
15417 case TYPENAME_TYPE:
15418 case UNBOUND_CLASS_TEMPLATE:
15419 case TYPEOF_TYPE:
15420 case DECLTYPE_TYPE:
15421 case TYPE_DECL:
15422 return tsubst (t, args, complain, in_decl);
15423
15424 case USING_DECL:
15425 t = DECL_NAME (t);
15426 /* Fall through. */
15427 case IDENTIFIER_NODE:
15428 if (IDENTIFIER_CONV_OP_P (t))
15429 {
15430 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15431 return make_conv_op_name (new_type);
15432 }
15433 else
15434 return t;
15435
15436 case CONSTRUCTOR:
15437 /* This is handled by tsubst_copy_and_build. */
15438 gcc_unreachable ();
15439
15440 case VA_ARG_EXPR:
15441 {
15442 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15443 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15444 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15445 }
15446
15447 case CLEANUP_POINT_EXPR:
15448 /* We shouldn't have built any of these during initial template
15449 generation. Instead, they should be built during instantiation
15450 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15451 gcc_unreachable ();
15452
15453 case OFFSET_REF:
15454 {
15455 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15456 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15457 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15458 r = build2 (code, type, op0, op1);
15459 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15460 if (!mark_used (TREE_OPERAND (r, 1), complain)
15461 && !(complain & tf_error))
15462 return error_mark_node;
15463 return r;
15464 }
15465
15466 case EXPR_PACK_EXPANSION:
15467 error ("invalid use of pack expansion expression");
15468 return error_mark_node;
15469
15470 case NONTYPE_ARGUMENT_PACK:
15471 error ("use %<...%> to expand argument pack");
15472 return error_mark_node;
15473
15474 case VOID_CST:
15475 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15476 return t;
15477
15478 case INTEGER_CST:
15479 case REAL_CST:
15480 case STRING_CST:
15481 case COMPLEX_CST:
15482 {
15483 /* Instantiate any typedefs in the type. */
15484 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15485 r = fold_convert (type, t);
15486 gcc_assert (TREE_CODE (r) == code);
15487 return r;
15488 }
15489
15490 case PTRMEM_CST:
15491 /* These can sometimes show up in a partial instantiation, but never
15492 involve template parms. */
15493 gcc_assert (!uses_template_parms (t));
15494 return t;
15495
15496 case UNARY_LEFT_FOLD_EXPR:
15497 return tsubst_unary_left_fold (t, args, complain, in_decl);
15498 case UNARY_RIGHT_FOLD_EXPR:
15499 return tsubst_unary_right_fold (t, args, complain, in_decl);
15500 case BINARY_LEFT_FOLD_EXPR:
15501 return tsubst_binary_left_fold (t, args, complain, in_decl);
15502 case BINARY_RIGHT_FOLD_EXPR:
15503 return tsubst_binary_right_fold (t, args, complain, in_decl);
15504 case PREDICT_EXPR:
15505 return t;
15506
15507 case DEBUG_BEGIN_STMT:
15508 /* ??? There's no point in copying it for now, but maybe some
15509 day it will contain more information, such as a pointer back
15510 to the containing function, inlined copy or so. */
15511 return t;
15512
15513 default:
15514 /* We shouldn't get here, but keep going if !flag_checking. */
15515 if (flag_checking)
15516 gcc_unreachable ();
15517 return t;
15518 }
15519 }
15520
15521 /* Helper function for tsubst_omp_clauses, used for instantiation of
15522 OMP_CLAUSE_DECL of clauses. */
15523
15524 static tree
15525 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15526 tree in_decl)
15527 {
15528 if (decl == NULL_TREE)
15529 return NULL_TREE;
15530
15531 /* Handle an OpenMP array section represented as a TREE_LIST (or
15532 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15533 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15534 TREE_LIST. We can handle it exactly the same as an array section
15535 (purpose, value, and a chain), even though the nomenclature
15536 (low_bound, length, etc) is different. */
15537 if (TREE_CODE (decl) == TREE_LIST)
15538 {
15539 tree low_bound
15540 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15541 /*integral_constant_expression_p=*/false);
15542 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15543 /*integral_constant_expression_p=*/false);
15544 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15545 in_decl);
15546 if (TREE_PURPOSE (decl) == low_bound
15547 && TREE_VALUE (decl) == length
15548 && TREE_CHAIN (decl) == chain)
15549 return decl;
15550 tree ret = tree_cons (low_bound, length, chain);
15551 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15552 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15553 return ret;
15554 }
15555 tree ret = tsubst_expr (decl, args, complain, in_decl,
15556 /*integral_constant_expression_p=*/false);
15557 /* Undo convert_from_reference tsubst_expr could have called. */
15558 if (decl
15559 && REFERENCE_REF_P (ret)
15560 && !REFERENCE_REF_P (decl))
15561 ret = TREE_OPERAND (ret, 0);
15562 return ret;
15563 }
15564
15565 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15566
15567 static tree
15568 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15569 tree args, tsubst_flags_t complain, tree in_decl)
15570 {
15571 tree new_clauses = NULL_TREE, nc, oc;
15572 tree linear_no_step = NULL_TREE;
15573
15574 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15575 {
15576 nc = copy_node (oc);
15577 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15578 new_clauses = nc;
15579
15580 switch (OMP_CLAUSE_CODE (nc))
15581 {
15582 case OMP_CLAUSE_LASTPRIVATE:
15583 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15584 {
15585 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15586 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15587 in_decl, /*integral_constant_expression_p=*/false);
15588 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15589 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15590 }
15591 /* FALLTHRU */
15592 case OMP_CLAUSE_PRIVATE:
15593 case OMP_CLAUSE_SHARED:
15594 case OMP_CLAUSE_FIRSTPRIVATE:
15595 case OMP_CLAUSE_COPYIN:
15596 case OMP_CLAUSE_COPYPRIVATE:
15597 case OMP_CLAUSE_UNIFORM:
15598 case OMP_CLAUSE_DEPEND:
15599 case OMP_CLAUSE_FROM:
15600 case OMP_CLAUSE_TO:
15601 case OMP_CLAUSE_MAP:
15602 case OMP_CLAUSE_USE_DEVICE_PTR:
15603 case OMP_CLAUSE_IS_DEVICE_PTR:
15604 OMP_CLAUSE_DECL (nc)
15605 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15606 in_decl);
15607 break;
15608 case OMP_CLAUSE_TILE:
15609 case OMP_CLAUSE_IF:
15610 case OMP_CLAUSE_NUM_THREADS:
15611 case OMP_CLAUSE_SCHEDULE:
15612 case OMP_CLAUSE_COLLAPSE:
15613 case OMP_CLAUSE_FINAL:
15614 case OMP_CLAUSE_DEVICE:
15615 case OMP_CLAUSE_DIST_SCHEDULE:
15616 case OMP_CLAUSE_NUM_TEAMS:
15617 case OMP_CLAUSE_THREAD_LIMIT:
15618 case OMP_CLAUSE_SAFELEN:
15619 case OMP_CLAUSE_SIMDLEN:
15620 case OMP_CLAUSE_NUM_TASKS:
15621 case OMP_CLAUSE_GRAINSIZE:
15622 case OMP_CLAUSE_PRIORITY:
15623 case OMP_CLAUSE_ORDERED:
15624 case OMP_CLAUSE_HINT:
15625 case OMP_CLAUSE_NUM_GANGS:
15626 case OMP_CLAUSE_NUM_WORKERS:
15627 case OMP_CLAUSE_VECTOR_LENGTH:
15628 case OMP_CLAUSE_WORKER:
15629 case OMP_CLAUSE_VECTOR:
15630 case OMP_CLAUSE_ASYNC:
15631 case OMP_CLAUSE_WAIT:
15632 OMP_CLAUSE_OPERAND (nc, 0)
15633 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15634 in_decl, /*integral_constant_expression_p=*/false);
15635 break;
15636 case OMP_CLAUSE_REDUCTION:
15637 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15638 {
15639 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15640 if (TREE_CODE (placeholder) == SCOPE_REF)
15641 {
15642 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15643 complain, in_decl);
15644 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15645 = build_qualified_name (NULL_TREE, scope,
15646 TREE_OPERAND (placeholder, 1),
15647 false);
15648 }
15649 else
15650 gcc_assert (identifier_p (placeholder));
15651 }
15652 OMP_CLAUSE_DECL (nc)
15653 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15654 in_decl);
15655 break;
15656 case OMP_CLAUSE_GANG:
15657 case OMP_CLAUSE_ALIGNED:
15658 OMP_CLAUSE_DECL (nc)
15659 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15660 in_decl);
15661 OMP_CLAUSE_OPERAND (nc, 1)
15662 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15663 in_decl, /*integral_constant_expression_p=*/false);
15664 break;
15665 case OMP_CLAUSE_LINEAR:
15666 OMP_CLAUSE_DECL (nc)
15667 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15668 in_decl);
15669 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15670 {
15671 gcc_assert (!linear_no_step);
15672 linear_no_step = nc;
15673 }
15674 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15675 OMP_CLAUSE_LINEAR_STEP (nc)
15676 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15677 complain, in_decl);
15678 else
15679 OMP_CLAUSE_LINEAR_STEP (nc)
15680 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15681 in_decl,
15682 /*integral_constant_expression_p=*/false);
15683 break;
15684 case OMP_CLAUSE_NOWAIT:
15685 case OMP_CLAUSE_DEFAULT:
15686 case OMP_CLAUSE_UNTIED:
15687 case OMP_CLAUSE_MERGEABLE:
15688 case OMP_CLAUSE_INBRANCH:
15689 case OMP_CLAUSE_NOTINBRANCH:
15690 case OMP_CLAUSE_PROC_BIND:
15691 case OMP_CLAUSE_FOR:
15692 case OMP_CLAUSE_PARALLEL:
15693 case OMP_CLAUSE_SECTIONS:
15694 case OMP_CLAUSE_TASKGROUP:
15695 case OMP_CLAUSE_NOGROUP:
15696 case OMP_CLAUSE_THREADS:
15697 case OMP_CLAUSE_SIMD:
15698 case OMP_CLAUSE_DEFAULTMAP:
15699 case OMP_CLAUSE_INDEPENDENT:
15700 case OMP_CLAUSE_AUTO:
15701 case OMP_CLAUSE_SEQ:
15702 break;
15703 default:
15704 gcc_unreachable ();
15705 }
15706 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15707 switch (OMP_CLAUSE_CODE (nc))
15708 {
15709 case OMP_CLAUSE_SHARED:
15710 case OMP_CLAUSE_PRIVATE:
15711 case OMP_CLAUSE_FIRSTPRIVATE:
15712 case OMP_CLAUSE_LASTPRIVATE:
15713 case OMP_CLAUSE_COPYPRIVATE:
15714 case OMP_CLAUSE_LINEAR:
15715 case OMP_CLAUSE_REDUCTION:
15716 case OMP_CLAUSE_USE_DEVICE_PTR:
15717 case OMP_CLAUSE_IS_DEVICE_PTR:
15718 /* tsubst_expr on SCOPE_REF results in returning
15719 finish_non_static_data_member result. Undo that here. */
15720 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15721 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15722 == IDENTIFIER_NODE))
15723 {
15724 tree t = OMP_CLAUSE_DECL (nc);
15725 tree v = t;
15726 while (v)
15727 switch (TREE_CODE (v))
15728 {
15729 case COMPONENT_REF:
15730 case MEM_REF:
15731 case INDIRECT_REF:
15732 CASE_CONVERT:
15733 case POINTER_PLUS_EXPR:
15734 v = TREE_OPERAND (v, 0);
15735 continue;
15736 case PARM_DECL:
15737 if (DECL_CONTEXT (v) == current_function_decl
15738 && DECL_ARTIFICIAL (v)
15739 && DECL_NAME (v) == this_identifier)
15740 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15741 /* FALLTHRU */
15742 default:
15743 v = NULL_TREE;
15744 break;
15745 }
15746 }
15747 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15748 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15749 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15750 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15751 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15752 {
15753 tree decl = OMP_CLAUSE_DECL (nc);
15754 if (VAR_P (decl))
15755 {
15756 retrofit_lang_decl (decl);
15757 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15758 }
15759 }
15760 break;
15761 default:
15762 break;
15763 }
15764 }
15765
15766 new_clauses = nreverse (new_clauses);
15767 if (ort != C_ORT_OMP_DECLARE_SIMD)
15768 {
15769 new_clauses = finish_omp_clauses (new_clauses, ort);
15770 if (linear_no_step)
15771 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15772 if (nc == linear_no_step)
15773 {
15774 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15775 break;
15776 }
15777 }
15778 return new_clauses;
15779 }
15780
15781 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15782
15783 static tree
15784 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15785 tree in_decl)
15786 {
15787 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15788
15789 tree purpose, value, chain;
15790
15791 if (t == NULL)
15792 return t;
15793
15794 if (TREE_CODE (t) != TREE_LIST)
15795 return tsubst_copy_and_build (t, args, complain, in_decl,
15796 /*function_p=*/false,
15797 /*integral_constant_expression_p=*/false);
15798
15799 if (t == void_list_node)
15800 return t;
15801
15802 purpose = TREE_PURPOSE (t);
15803 if (purpose)
15804 purpose = RECUR (purpose);
15805 value = TREE_VALUE (t);
15806 if (value)
15807 {
15808 if (TREE_CODE (value) != LABEL_DECL)
15809 value = RECUR (value);
15810 else
15811 {
15812 value = lookup_label (DECL_NAME (value));
15813 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15814 TREE_USED (value) = 1;
15815 }
15816 }
15817 chain = TREE_CHAIN (t);
15818 if (chain && chain != void_type_node)
15819 chain = RECUR (chain);
15820 return tree_cons (purpose, value, chain);
15821 #undef RECUR
15822 }
15823
15824 /* Used to temporarily communicate the list of #pragma omp parallel
15825 clauses to #pragma omp for instantiation if they are combined
15826 together. */
15827
15828 static tree *omp_parallel_combined_clauses;
15829
15830 /* Substitute one OMP_FOR iterator. */
15831
15832 static void
15833 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15834 tree initv, tree condv, tree incrv, tree *clauses,
15835 tree args, tsubst_flags_t complain, tree in_decl,
15836 bool integral_constant_expression_p)
15837 {
15838 #define RECUR(NODE) \
15839 tsubst_expr ((NODE), args, complain, in_decl, \
15840 integral_constant_expression_p)
15841 tree decl, init, cond, incr;
15842
15843 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15844 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15845
15846 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15847 {
15848 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15849 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15850 }
15851
15852 decl = TREE_OPERAND (init, 0);
15853 init = TREE_OPERAND (init, 1);
15854 tree decl_expr = NULL_TREE;
15855 if (init && TREE_CODE (init) == DECL_EXPR)
15856 {
15857 /* We need to jump through some hoops to handle declarations in the
15858 init-statement, since we might need to handle auto deduction,
15859 but we need to keep control of initialization. */
15860 decl_expr = init;
15861 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15862 decl = tsubst_decl (decl, args, complain);
15863 }
15864 else
15865 {
15866 if (TREE_CODE (decl) == SCOPE_REF)
15867 {
15868 decl = RECUR (decl);
15869 if (TREE_CODE (decl) == COMPONENT_REF)
15870 {
15871 tree v = decl;
15872 while (v)
15873 switch (TREE_CODE (v))
15874 {
15875 case COMPONENT_REF:
15876 case MEM_REF:
15877 case INDIRECT_REF:
15878 CASE_CONVERT:
15879 case POINTER_PLUS_EXPR:
15880 v = TREE_OPERAND (v, 0);
15881 continue;
15882 case PARM_DECL:
15883 if (DECL_CONTEXT (v) == current_function_decl
15884 && DECL_ARTIFICIAL (v)
15885 && DECL_NAME (v) == this_identifier)
15886 {
15887 decl = TREE_OPERAND (decl, 1);
15888 decl = omp_privatize_field (decl, false);
15889 }
15890 /* FALLTHRU */
15891 default:
15892 v = NULL_TREE;
15893 break;
15894 }
15895 }
15896 }
15897 else
15898 decl = RECUR (decl);
15899 }
15900 init = RECUR (init);
15901
15902 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15903 if (auto_node && init)
15904 TREE_TYPE (decl)
15905 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
15906
15907 gcc_assert (!type_dependent_expression_p (decl));
15908
15909 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15910 {
15911 if (decl_expr)
15912 {
15913 /* Declare the variable, but don't let that initialize it. */
15914 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15915 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15916 RECUR (decl_expr);
15917 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15918 }
15919
15920 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15921 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15922 if (TREE_CODE (incr) == MODIFY_EXPR)
15923 {
15924 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15925 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15926 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15927 NOP_EXPR, rhs, complain);
15928 }
15929 else
15930 incr = RECUR (incr);
15931 TREE_VEC_ELT (declv, i) = decl;
15932 TREE_VEC_ELT (initv, i) = init;
15933 TREE_VEC_ELT (condv, i) = cond;
15934 TREE_VEC_ELT (incrv, i) = incr;
15935 return;
15936 }
15937
15938 if (decl_expr)
15939 {
15940 /* Declare and initialize the variable. */
15941 RECUR (decl_expr);
15942 init = NULL_TREE;
15943 }
15944 else if (init)
15945 {
15946 tree *pc;
15947 int j;
15948 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15949 {
15950 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15951 {
15952 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15953 && OMP_CLAUSE_DECL (*pc) == decl)
15954 break;
15955 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15956 && OMP_CLAUSE_DECL (*pc) == decl)
15957 {
15958 if (j)
15959 break;
15960 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15961 tree c = *pc;
15962 *pc = OMP_CLAUSE_CHAIN (c);
15963 OMP_CLAUSE_CHAIN (c) = *clauses;
15964 *clauses = c;
15965 }
15966 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15967 && OMP_CLAUSE_DECL (*pc) == decl)
15968 {
15969 error ("iteration variable %qD should not be firstprivate",
15970 decl);
15971 *pc = OMP_CLAUSE_CHAIN (*pc);
15972 }
15973 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15974 && OMP_CLAUSE_DECL (*pc) == decl)
15975 {
15976 error ("iteration variable %qD should not be reduction",
15977 decl);
15978 *pc = OMP_CLAUSE_CHAIN (*pc);
15979 }
15980 else
15981 pc = &OMP_CLAUSE_CHAIN (*pc);
15982 }
15983 if (*pc)
15984 break;
15985 }
15986 if (*pc == NULL_TREE)
15987 {
15988 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15989 OMP_CLAUSE_DECL (c) = decl;
15990 c = finish_omp_clauses (c, C_ORT_OMP);
15991 if (c)
15992 {
15993 OMP_CLAUSE_CHAIN (c) = *clauses;
15994 *clauses = c;
15995 }
15996 }
15997 }
15998 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15999 if (COMPARISON_CLASS_P (cond))
16000 {
16001 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16002 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16003 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16004 }
16005 else
16006 cond = RECUR (cond);
16007 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16008 switch (TREE_CODE (incr))
16009 {
16010 case PREINCREMENT_EXPR:
16011 case PREDECREMENT_EXPR:
16012 case POSTINCREMENT_EXPR:
16013 case POSTDECREMENT_EXPR:
16014 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16015 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16016 break;
16017 case MODIFY_EXPR:
16018 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16019 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16020 {
16021 tree rhs = TREE_OPERAND (incr, 1);
16022 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16023 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16024 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16025 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16026 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16027 rhs0, rhs1));
16028 }
16029 else
16030 incr = RECUR (incr);
16031 break;
16032 case MODOP_EXPR:
16033 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16034 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16035 {
16036 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16037 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16038 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16039 TREE_TYPE (decl), lhs,
16040 RECUR (TREE_OPERAND (incr, 2))));
16041 }
16042 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16043 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16044 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16045 {
16046 tree rhs = TREE_OPERAND (incr, 2);
16047 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16048 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16049 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16050 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16051 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16052 rhs0, rhs1));
16053 }
16054 else
16055 incr = RECUR (incr);
16056 break;
16057 default:
16058 incr = RECUR (incr);
16059 break;
16060 }
16061
16062 TREE_VEC_ELT (declv, i) = decl;
16063 TREE_VEC_ELT (initv, i) = init;
16064 TREE_VEC_ELT (condv, i) = cond;
16065 TREE_VEC_ELT (incrv, i) = incr;
16066 #undef RECUR
16067 }
16068
16069 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16070 of OMP_TARGET's body. */
16071
16072 static tree
16073 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16074 {
16075 *walk_subtrees = 0;
16076 switch (TREE_CODE (*tp))
16077 {
16078 case OMP_TEAMS:
16079 return *tp;
16080 case BIND_EXPR:
16081 case STATEMENT_LIST:
16082 *walk_subtrees = 1;
16083 break;
16084 default:
16085 break;
16086 }
16087 return NULL_TREE;
16088 }
16089
16090 /* Helper function for tsubst_expr. For decomposition declaration
16091 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16092 also the corresponding decls representing the identifiers
16093 of the decomposition declaration. Return DECL if successful
16094 or error_mark_node otherwise, set *FIRST to the first decl
16095 in the list chained through DECL_CHAIN and *CNT to the number
16096 of such decls. */
16097
16098 static tree
16099 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16100 tsubst_flags_t complain, tree in_decl, tree *first,
16101 unsigned int *cnt)
16102 {
16103 tree decl2, decl3, prev = decl;
16104 *cnt = 0;
16105 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16106 for (decl2 = DECL_CHAIN (pattern_decl);
16107 decl2
16108 && VAR_P (decl2)
16109 && DECL_DECOMPOSITION_P (decl2)
16110 && DECL_NAME (decl2);
16111 decl2 = DECL_CHAIN (decl2))
16112 {
16113 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16114 {
16115 gcc_assert (errorcount);
16116 return error_mark_node;
16117 }
16118 (*cnt)++;
16119 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16120 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16121 tree v = DECL_VALUE_EXPR (decl2);
16122 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16123 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16124 decl3 = tsubst (decl2, args, complain, in_decl);
16125 SET_DECL_VALUE_EXPR (decl2, v);
16126 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16127 if (VAR_P (decl3))
16128 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16129 maybe_push_decl (decl3);
16130 if (error_operand_p (decl3))
16131 decl = error_mark_node;
16132 else if (decl != error_mark_node
16133 && DECL_CHAIN (decl3) != prev)
16134 {
16135 gcc_assert (errorcount);
16136 decl = error_mark_node;
16137 }
16138 else
16139 prev = decl3;
16140 }
16141 *first = prev;
16142 return decl;
16143 }
16144
16145 /* Like tsubst_copy for expressions, etc. but also does semantic
16146 processing. */
16147
16148 tree
16149 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16150 bool integral_constant_expression_p)
16151 {
16152 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16153 #define RECUR(NODE) \
16154 tsubst_expr ((NODE), args, complain, in_decl, \
16155 integral_constant_expression_p)
16156
16157 tree stmt, tmp;
16158 tree r;
16159 location_t loc;
16160
16161 if (t == NULL_TREE || t == error_mark_node)
16162 return t;
16163
16164 loc = input_location;
16165 if (EXPR_HAS_LOCATION (t))
16166 input_location = EXPR_LOCATION (t);
16167 if (STATEMENT_CODE_P (TREE_CODE (t)))
16168 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16169
16170 switch (TREE_CODE (t))
16171 {
16172 case STATEMENT_LIST:
16173 {
16174 tree_stmt_iterator i;
16175 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16176 RECUR (tsi_stmt (i));
16177 break;
16178 }
16179
16180 case CTOR_INITIALIZER:
16181 finish_mem_initializers (tsubst_initializer_list
16182 (TREE_OPERAND (t, 0), args));
16183 break;
16184
16185 case RETURN_EXPR:
16186 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16187 break;
16188
16189 case EXPR_STMT:
16190 tmp = RECUR (EXPR_STMT_EXPR (t));
16191 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16192 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16193 else
16194 finish_expr_stmt (tmp);
16195 break;
16196
16197 case USING_STMT:
16198 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16199 /*attribs=*/NULL_TREE);
16200 break;
16201
16202 case DECL_EXPR:
16203 {
16204 tree decl, pattern_decl;
16205 tree init;
16206
16207 pattern_decl = decl = DECL_EXPR_DECL (t);
16208 if (TREE_CODE (decl) == LABEL_DECL)
16209 finish_label_decl (DECL_NAME (decl));
16210 else if (TREE_CODE (decl) == USING_DECL)
16211 {
16212 tree scope = USING_DECL_SCOPE (decl);
16213 tree name = DECL_NAME (decl);
16214
16215 scope = tsubst (scope, args, complain, in_decl);
16216 decl = lookup_qualified_name (scope, name,
16217 /*is_type_p=*/false,
16218 /*complain=*/false);
16219 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16220 qualified_name_lookup_error (scope, name, decl, input_location);
16221 else
16222 finish_local_using_decl (decl, scope, name);
16223 }
16224 else if (is_capture_proxy (decl)
16225 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16226 {
16227 /* We're in tsubst_lambda_expr, we've already inserted a new
16228 capture proxy, so look it up and register it. */
16229 tree inst;
16230 if (DECL_PACK_P (decl))
16231 {
16232 inst = (retrieve_local_specialization
16233 (DECL_CAPTURED_VARIABLE (decl)));
16234 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16235 }
16236 else
16237 {
16238 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16239 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16240 gcc_assert (inst != decl && is_capture_proxy (inst));
16241 }
16242 register_local_specialization (inst, decl);
16243 break;
16244 }
16245 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16246 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16247 /* Don't copy the old closure; we'll create a new one in
16248 tsubst_lambda_expr. */
16249 break;
16250 else
16251 {
16252 init = DECL_INITIAL (decl);
16253 decl = tsubst (decl, args, complain, in_decl);
16254 if (decl != error_mark_node)
16255 {
16256 /* By marking the declaration as instantiated, we avoid
16257 trying to instantiate it. Since instantiate_decl can't
16258 handle local variables, and since we've already done
16259 all that needs to be done, that's the right thing to
16260 do. */
16261 if (VAR_P (decl))
16262 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16263 if (VAR_P (decl)
16264 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16265 /* Anonymous aggregates are a special case. */
16266 finish_anon_union (decl);
16267 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16268 {
16269 DECL_CONTEXT (decl) = current_function_decl;
16270 if (DECL_NAME (decl) == this_identifier)
16271 {
16272 tree lam = DECL_CONTEXT (current_function_decl);
16273 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16274 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16275 }
16276 insert_capture_proxy (decl);
16277 }
16278 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16279 /* We already did a pushtag. */;
16280 else if (TREE_CODE (decl) == FUNCTION_DECL
16281 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16282 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16283 {
16284 DECL_CONTEXT (decl) = NULL_TREE;
16285 pushdecl (decl);
16286 DECL_CONTEXT (decl) = current_function_decl;
16287 cp_check_omp_declare_reduction (decl);
16288 }
16289 else
16290 {
16291 int const_init = false;
16292 maybe_push_decl (decl);
16293 if (VAR_P (decl)
16294 && DECL_PRETTY_FUNCTION_P (decl))
16295 {
16296 /* For __PRETTY_FUNCTION__ we have to adjust the
16297 initializer. */
16298 const char *const name
16299 = cxx_printable_name (current_function_decl, 2);
16300 init = cp_fname_init (name, &TREE_TYPE (decl));
16301 }
16302 else
16303 init = tsubst_init (init, decl, args, complain, in_decl);
16304
16305 if (VAR_P (decl))
16306 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16307 (pattern_decl));
16308 if (VAR_P (decl)
16309 && DECL_DECOMPOSITION_P (decl)
16310 && TREE_TYPE (pattern_decl) != error_mark_node)
16311 {
16312 unsigned int cnt;
16313 tree first;
16314 tree ndecl
16315 = tsubst_decomp_names (decl, pattern_decl, args,
16316 complain, in_decl, &first, &cnt);
16317 if (ndecl != error_mark_node)
16318 cp_maybe_mangle_decomp (ndecl, first, cnt);
16319 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16320 if (ndecl != error_mark_node)
16321 cp_finish_decomp (ndecl, first, cnt);
16322 }
16323 else
16324 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16325 }
16326 }
16327 }
16328
16329 break;
16330 }
16331
16332 case FOR_STMT:
16333 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16334 RECUR (FOR_INIT_STMT (t));
16335 finish_init_stmt (stmt);
16336 tmp = RECUR (FOR_COND (t));
16337 finish_for_cond (tmp, stmt, false, 0);
16338 tmp = RECUR (FOR_EXPR (t));
16339 finish_for_expr (tmp, stmt);
16340 {
16341 bool prev = note_iteration_stmt_body_start ();
16342 RECUR (FOR_BODY (t));
16343 note_iteration_stmt_body_end (prev);
16344 }
16345 finish_for_stmt (stmt);
16346 break;
16347
16348 case RANGE_FOR_STMT:
16349 {
16350 /* Construct another range_for, if this is not a final
16351 substitution (for inside inside a generic lambda of a
16352 template). Otherwise convert to a regular for. */
16353 tree decl, expr;
16354 stmt = (processing_template_decl
16355 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16356 : begin_for_stmt (NULL_TREE, NULL_TREE));
16357 decl = RANGE_FOR_DECL (t);
16358 decl = tsubst (decl, args, complain, in_decl);
16359 maybe_push_decl (decl);
16360 expr = RECUR (RANGE_FOR_EXPR (t));
16361
16362 tree decomp_first = NULL_TREE;
16363 unsigned decomp_cnt = 0;
16364 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16365 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16366 complain, in_decl,
16367 &decomp_first, &decomp_cnt);
16368
16369 if (processing_template_decl)
16370 {
16371 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16372 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16373 finish_range_for_decl (stmt, decl, expr);
16374 }
16375 else
16376 {
16377 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16378 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16379 stmt = cp_convert_range_for (stmt, decl, expr,
16380 decomp_first, decomp_cnt,
16381 RANGE_FOR_IVDEP (t), unroll);
16382 }
16383
16384 bool prev = note_iteration_stmt_body_start ();
16385 RECUR (RANGE_FOR_BODY (t));
16386 note_iteration_stmt_body_end (prev);
16387 finish_for_stmt (stmt);
16388 }
16389 break;
16390
16391 case WHILE_STMT:
16392 stmt = begin_while_stmt ();
16393 tmp = RECUR (WHILE_COND (t));
16394 finish_while_stmt_cond (tmp, stmt, false, 0);
16395 {
16396 bool prev = note_iteration_stmt_body_start ();
16397 RECUR (WHILE_BODY (t));
16398 note_iteration_stmt_body_end (prev);
16399 }
16400 finish_while_stmt (stmt);
16401 break;
16402
16403 case DO_STMT:
16404 stmt = begin_do_stmt ();
16405 {
16406 bool prev = note_iteration_stmt_body_start ();
16407 RECUR (DO_BODY (t));
16408 note_iteration_stmt_body_end (prev);
16409 }
16410 finish_do_body (stmt);
16411 tmp = RECUR (DO_COND (t));
16412 finish_do_stmt (tmp, stmt, false, 0);
16413 break;
16414
16415 case IF_STMT:
16416 stmt = begin_if_stmt ();
16417 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16418 tmp = RECUR (IF_COND (t));
16419 tmp = finish_if_stmt_cond (tmp, stmt);
16420 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16421 /* Don't instantiate the THEN_CLAUSE. */;
16422 else
16423 {
16424 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16425 if (inhibit)
16426 ++c_inhibit_evaluation_warnings;
16427 RECUR (THEN_CLAUSE (t));
16428 if (inhibit)
16429 --c_inhibit_evaluation_warnings;
16430 }
16431 finish_then_clause (stmt);
16432
16433 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16434 /* Don't instantiate the ELSE_CLAUSE. */;
16435 else if (ELSE_CLAUSE (t))
16436 {
16437 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16438 begin_else_clause (stmt);
16439 if (inhibit)
16440 ++c_inhibit_evaluation_warnings;
16441 RECUR (ELSE_CLAUSE (t));
16442 if (inhibit)
16443 --c_inhibit_evaluation_warnings;
16444 finish_else_clause (stmt);
16445 }
16446
16447 finish_if_stmt (stmt);
16448 break;
16449
16450 case BIND_EXPR:
16451 if (BIND_EXPR_BODY_BLOCK (t))
16452 stmt = begin_function_body ();
16453 else
16454 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16455 ? BCS_TRY_BLOCK : 0);
16456
16457 RECUR (BIND_EXPR_BODY (t));
16458
16459 if (BIND_EXPR_BODY_BLOCK (t))
16460 finish_function_body (stmt);
16461 else
16462 finish_compound_stmt (stmt);
16463 break;
16464
16465 case BREAK_STMT:
16466 finish_break_stmt ();
16467 break;
16468
16469 case CONTINUE_STMT:
16470 finish_continue_stmt ();
16471 break;
16472
16473 case SWITCH_STMT:
16474 stmt = begin_switch_stmt ();
16475 tmp = RECUR (SWITCH_STMT_COND (t));
16476 finish_switch_cond (tmp, stmt);
16477 RECUR (SWITCH_STMT_BODY (t));
16478 finish_switch_stmt (stmt);
16479 break;
16480
16481 case CASE_LABEL_EXPR:
16482 {
16483 tree low = RECUR (CASE_LOW (t));
16484 tree high = RECUR (CASE_HIGH (t));
16485 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16486 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16487 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16488 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16489 }
16490 break;
16491
16492 case LABEL_EXPR:
16493 {
16494 tree decl = LABEL_EXPR_LABEL (t);
16495 tree label;
16496
16497 label = finish_label_stmt (DECL_NAME (decl));
16498 if (TREE_CODE (label) == LABEL_DECL)
16499 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16500 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16501 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16502 }
16503 break;
16504
16505 case GOTO_EXPR:
16506 tmp = GOTO_DESTINATION (t);
16507 if (TREE_CODE (tmp) != LABEL_DECL)
16508 /* Computed goto's must be tsubst'd into. On the other hand,
16509 non-computed gotos must not be; the identifier in question
16510 will have no binding. */
16511 tmp = RECUR (tmp);
16512 else
16513 tmp = DECL_NAME (tmp);
16514 finish_goto_stmt (tmp);
16515 break;
16516
16517 case ASM_EXPR:
16518 {
16519 tree string = RECUR (ASM_STRING (t));
16520 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16521 complain, in_decl);
16522 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16523 complain, in_decl);
16524 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16525 complain, in_decl);
16526 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16527 complain, in_decl);
16528 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16529 clobbers, labels);
16530 tree asm_expr = tmp;
16531 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16532 asm_expr = TREE_OPERAND (asm_expr, 0);
16533 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16534 }
16535 break;
16536
16537 case TRY_BLOCK:
16538 if (CLEANUP_P (t))
16539 {
16540 stmt = begin_try_block ();
16541 RECUR (TRY_STMTS (t));
16542 finish_cleanup_try_block (stmt);
16543 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16544 }
16545 else
16546 {
16547 tree compound_stmt = NULL_TREE;
16548
16549 if (FN_TRY_BLOCK_P (t))
16550 stmt = begin_function_try_block (&compound_stmt);
16551 else
16552 stmt = begin_try_block ();
16553
16554 RECUR (TRY_STMTS (t));
16555
16556 if (FN_TRY_BLOCK_P (t))
16557 finish_function_try_block (stmt);
16558 else
16559 finish_try_block (stmt);
16560
16561 RECUR (TRY_HANDLERS (t));
16562 if (FN_TRY_BLOCK_P (t))
16563 finish_function_handler_sequence (stmt, compound_stmt);
16564 else
16565 finish_handler_sequence (stmt);
16566 }
16567 break;
16568
16569 case HANDLER:
16570 {
16571 tree decl = HANDLER_PARMS (t);
16572
16573 if (decl)
16574 {
16575 decl = tsubst (decl, args, complain, in_decl);
16576 /* Prevent instantiate_decl from trying to instantiate
16577 this variable. We've already done all that needs to be
16578 done. */
16579 if (decl != error_mark_node)
16580 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16581 }
16582 stmt = begin_handler ();
16583 finish_handler_parms (decl, stmt);
16584 RECUR (HANDLER_BODY (t));
16585 finish_handler (stmt);
16586 }
16587 break;
16588
16589 case TAG_DEFN:
16590 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16591 if (CLASS_TYPE_P (tmp))
16592 {
16593 /* Local classes are not independent templates; they are
16594 instantiated along with their containing function. And this
16595 way we don't have to deal with pushing out of one local class
16596 to instantiate a member of another local class. */
16597 /* Closures are handled by the LAMBDA_EXPR. */
16598 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16599 complete_type (tmp);
16600 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16601 if ((VAR_P (fld)
16602 || (TREE_CODE (fld) == FUNCTION_DECL
16603 && !DECL_ARTIFICIAL (fld)))
16604 && DECL_TEMPLATE_INSTANTIATION (fld))
16605 instantiate_decl (fld, /*defer_ok=*/false,
16606 /*expl_inst_class=*/false);
16607 }
16608 break;
16609
16610 case STATIC_ASSERT:
16611 {
16612 tree condition;
16613
16614 ++c_inhibit_evaluation_warnings;
16615 condition =
16616 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16617 args,
16618 complain, in_decl,
16619 /*integral_constant_expression_p=*/true);
16620 --c_inhibit_evaluation_warnings;
16621
16622 finish_static_assert (condition,
16623 STATIC_ASSERT_MESSAGE (t),
16624 STATIC_ASSERT_SOURCE_LOCATION (t),
16625 /*member_p=*/false);
16626 }
16627 break;
16628
16629 case OACC_KERNELS:
16630 case OACC_PARALLEL:
16631 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16632 in_decl);
16633 stmt = begin_omp_parallel ();
16634 RECUR (OMP_BODY (t));
16635 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16636 break;
16637
16638 case OMP_PARALLEL:
16639 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16640 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16641 complain, in_decl);
16642 if (OMP_PARALLEL_COMBINED (t))
16643 omp_parallel_combined_clauses = &tmp;
16644 stmt = begin_omp_parallel ();
16645 RECUR (OMP_PARALLEL_BODY (t));
16646 gcc_assert (omp_parallel_combined_clauses == NULL);
16647 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16648 = OMP_PARALLEL_COMBINED (t);
16649 pop_omp_privatization_clauses (r);
16650 break;
16651
16652 case OMP_TASK:
16653 r = push_omp_privatization_clauses (false);
16654 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16655 complain, in_decl);
16656 stmt = begin_omp_task ();
16657 RECUR (OMP_TASK_BODY (t));
16658 finish_omp_task (tmp, stmt);
16659 pop_omp_privatization_clauses (r);
16660 break;
16661
16662 case OMP_FOR:
16663 case OMP_SIMD:
16664 case OMP_DISTRIBUTE:
16665 case OMP_TASKLOOP:
16666 case OACC_LOOP:
16667 {
16668 tree clauses, body, pre_body;
16669 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16670 tree orig_declv = NULL_TREE;
16671 tree incrv = NULL_TREE;
16672 enum c_omp_region_type ort = C_ORT_OMP;
16673 int i;
16674
16675 if (TREE_CODE (t) == OACC_LOOP)
16676 ort = C_ORT_ACC;
16677
16678 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16679 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16680 in_decl);
16681 if (OMP_FOR_INIT (t) != NULL_TREE)
16682 {
16683 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16684 if (OMP_FOR_ORIG_DECLS (t))
16685 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16686 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16687 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16688 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16689 }
16690
16691 stmt = begin_omp_structured_block ();
16692
16693 pre_body = push_stmt_list ();
16694 RECUR (OMP_FOR_PRE_BODY (t));
16695 pre_body = pop_stmt_list (pre_body);
16696
16697 if (OMP_FOR_INIT (t) != NULL_TREE)
16698 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16699 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16700 incrv, &clauses, args, complain, in_decl,
16701 integral_constant_expression_p);
16702 omp_parallel_combined_clauses = NULL;
16703
16704 body = push_stmt_list ();
16705 RECUR (OMP_FOR_BODY (t));
16706 body = pop_stmt_list (body);
16707
16708 if (OMP_FOR_INIT (t) != NULL_TREE)
16709 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16710 orig_declv, initv, condv, incrv, body, pre_body,
16711 NULL, clauses);
16712 else
16713 {
16714 t = make_node (TREE_CODE (t));
16715 TREE_TYPE (t) = void_type_node;
16716 OMP_FOR_BODY (t) = body;
16717 OMP_FOR_PRE_BODY (t) = pre_body;
16718 OMP_FOR_CLAUSES (t) = clauses;
16719 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16720 add_stmt (t);
16721 }
16722
16723 add_stmt (finish_omp_structured_block (stmt));
16724 pop_omp_privatization_clauses (r);
16725 }
16726 break;
16727
16728 case OMP_SECTIONS:
16729 omp_parallel_combined_clauses = NULL;
16730 /* FALLTHRU */
16731 case OMP_SINGLE:
16732 case OMP_TEAMS:
16733 case OMP_CRITICAL:
16734 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16735 && OMP_TEAMS_COMBINED (t));
16736 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16737 in_decl);
16738 stmt = push_stmt_list ();
16739 RECUR (OMP_BODY (t));
16740 stmt = pop_stmt_list (stmt);
16741
16742 t = copy_node (t);
16743 OMP_BODY (t) = stmt;
16744 OMP_CLAUSES (t) = tmp;
16745 add_stmt (t);
16746 pop_omp_privatization_clauses (r);
16747 break;
16748
16749 case OACC_DATA:
16750 case OMP_TARGET_DATA:
16751 case OMP_TARGET:
16752 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16753 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16754 in_decl);
16755 keep_next_level (true);
16756 stmt = begin_omp_structured_block ();
16757
16758 RECUR (OMP_BODY (t));
16759 stmt = finish_omp_structured_block (stmt);
16760
16761 t = copy_node (t);
16762 OMP_BODY (t) = stmt;
16763 OMP_CLAUSES (t) = tmp;
16764 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16765 {
16766 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16767 if (teams)
16768 {
16769 /* For combined target teams, ensure the num_teams and
16770 thread_limit clause expressions are evaluated on the host,
16771 before entering the target construct. */
16772 tree c;
16773 for (c = OMP_TEAMS_CLAUSES (teams);
16774 c; c = OMP_CLAUSE_CHAIN (c))
16775 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16776 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16777 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16778 {
16779 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16780 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16781 if (expr == error_mark_node)
16782 continue;
16783 tmp = TARGET_EXPR_SLOT (expr);
16784 add_stmt (expr);
16785 OMP_CLAUSE_OPERAND (c, 0) = expr;
16786 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16787 OMP_CLAUSE_FIRSTPRIVATE);
16788 OMP_CLAUSE_DECL (tc) = tmp;
16789 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16790 OMP_TARGET_CLAUSES (t) = tc;
16791 }
16792 }
16793 }
16794 add_stmt (t);
16795 break;
16796
16797 case OACC_DECLARE:
16798 t = copy_node (t);
16799 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16800 complain, in_decl);
16801 OACC_DECLARE_CLAUSES (t) = tmp;
16802 add_stmt (t);
16803 break;
16804
16805 case OMP_TARGET_UPDATE:
16806 case OMP_TARGET_ENTER_DATA:
16807 case OMP_TARGET_EXIT_DATA:
16808 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16809 complain, in_decl);
16810 t = copy_node (t);
16811 OMP_STANDALONE_CLAUSES (t) = tmp;
16812 add_stmt (t);
16813 break;
16814
16815 case OACC_ENTER_DATA:
16816 case OACC_EXIT_DATA:
16817 case OACC_UPDATE:
16818 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16819 complain, in_decl);
16820 t = copy_node (t);
16821 OMP_STANDALONE_CLAUSES (t) = tmp;
16822 add_stmt (t);
16823 break;
16824
16825 case OMP_ORDERED:
16826 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16827 complain, in_decl);
16828 stmt = push_stmt_list ();
16829 RECUR (OMP_BODY (t));
16830 stmt = pop_stmt_list (stmt);
16831
16832 t = copy_node (t);
16833 OMP_BODY (t) = stmt;
16834 OMP_ORDERED_CLAUSES (t) = tmp;
16835 add_stmt (t);
16836 break;
16837
16838 case OMP_SECTION:
16839 case OMP_MASTER:
16840 case OMP_TASKGROUP:
16841 stmt = push_stmt_list ();
16842 RECUR (OMP_BODY (t));
16843 stmt = pop_stmt_list (stmt);
16844
16845 t = copy_node (t);
16846 OMP_BODY (t) = stmt;
16847 add_stmt (t);
16848 break;
16849
16850 case OMP_ATOMIC:
16851 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16852 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16853 {
16854 tree op1 = TREE_OPERAND (t, 1);
16855 tree rhs1 = NULL_TREE;
16856 tree lhs, rhs;
16857 if (TREE_CODE (op1) == COMPOUND_EXPR)
16858 {
16859 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16860 op1 = TREE_OPERAND (op1, 1);
16861 }
16862 lhs = RECUR (TREE_OPERAND (op1, 0));
16863 rhs = RECUR (TREE_OPERAND (op1, 1));
16864 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16865 NULL_TREE, NULL_TREE, rhs1,
16866 OMP_ATOMIC_SEQ_CST (t));
16867 }
16868 else
16869 {
16870 tree op1 = TREE_OPERAND (t, 1);
16871 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16872 tree rhs1 = NULL_TREE;
16873 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16874 enum tree_code opcode = NOP_EXPR;
16875 if (code == OMP_ATOMIC_READ)
16876 {
16877 v = RECUR (TREE_OPERAND (op1, 0));
16878 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16879 }
16880 else if (code == OMP_ATOMIC_CAPTURE_OLD
16881 || code == OMP_ATOMIC_CAPTURE_NEW)
16882 {
16883 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16884 v = RECUR (TREE_OPERAND (op1, 0));
16885 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16886 if (TREE_CODE (op11) == COMPOUND_EXPR)
16887 {
16888 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16889 op11 = TREE_OPERAND (op11, 1);
16890 }
16891 lhs = RECUR (TREE_OPERAND (op11, 0));
16892 rhs = RECUR (TREE_OPERAND (op11, 1));
16893 opcode = TREE_CODE (op11);
16894 if (opcode == MODIFY_EXPR)
16895 opcode = NOP_EXPR;
16896 }
16897 else
16898 {
16899 code = OMP_ATOMIC;
16900 lhs = RECUR (TREE_OPERAND (op1, 0));
16901 rhs = RECUR (TREE_OPERAND (op1, 1));
16902 }
16903 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16904 OMP_ATOMIC_SEQ_CST (t));
16905 }
16906 break;
16907
16908 case TRANSACTION_EXPR:
16909 {
16910 int flags = 0;
16911 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16912 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16913
16914 if (TRANSACTION_EXPR_IS_STMT (t))
16915 {
16916 tree body = TRANSACTION_EXPR_BODY (t);
16917 tree noex = NULL_TREE;
16918 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16919 {
16920 noex = MUST_NOT_THROW_COND (body);
16921 if (noex == NULL_TREE)
16922 noex = boolean_true_node;
16923 body = TREE_OPERAND (body, 0);
16924 }
16925 stmt = begin_transaction_stmt (input_location, NULL, flags);
16926 RECUR (body);
16927 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16928 }
16929 else
16930 {
16931 stmt = build_transaction_expr (EXPR_LOCATION (t),
16932 RECUR (TRANSACTION_EXPR_BODY (t)),
16933 flags, NULL_TREE);
16934 RETURN (stmt);
16935 }
16936 }
16937 break;
16938
16939 case MUST_NOT_THROW_EXPR:
16940 {
16941 tree op0 = RECUR (TREE_OPERAND (t, 0));
16942 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16943 RETURN (build_must_not_throw_expr (op0, cond));
16944 }
16945
16946 case EXPR_PACK_EXPANSION:
16947 error ("invalid use of pack expansion expression");
16948 RETURN (error_mark_node);
16949
16950 case NONTYPE_ARGUMENT_PACK:
16951 error ("use %<...%> to expand argument pack");
16952 RETURN (error_mark_node);
16953
16954 case COMPOUND_EXPR:
16955 tmp = RECUR (TREE_OPERAND (t, 0));
16956 if (tmp == NULL_TREE)
16957 /* If the first operand was a statement, we're done with it. */
16958 RETURN (RECUR (TREE_OPERAND (t, 1)));
16959 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16960 RECUR (TREE_OPERAND (t, 1)),
16961 complain));
16962
16963 case ANNOTATE_EXPR:
16964 tmp = RECUR (TREE_OPERAND (t, 0));
16965 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16966 TREE_TYPE (tmp), tmp,
16967 RECUR (TREE_OPERAND (t, 1)),
16968 RECUR (TREE_OPERAND (t, 2))));
16969
16970 default:
16971 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16972
16973 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16974 /*function_p=*/false,
16975 integral_constant_expression_p));
16976 }
16977
16978 RETURN (NULL_TREE);
16979 out:
16980 input_location = loc;
16981 return r;
16982 #undef RECUR
16983 #undef RETURN
16984 }
16985
16986 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16987 function. For description of the body see comment above
16988 cp_parser_omp_declare_reduction_exprs. */
16989
16990 static void
16991 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16992 {
16993 if (t == NULL_TREE || t == error_mark_node)
16994 return;
16995
16996 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16997
16998 tree_stmt_iterator tsi;
16999 int i;
17000 tree stmts[7];
17001 memset (stmts, 0, sizeof stmts);
17002 for (i = 0, tsi = tsi_start (t);
17003 i < 7 && !tsi_end_p (tsi);
17004 i++, tsi_next (&tsi))
17005 stmts[i] = tsi_stmt (tsi);
17006 gcc_assert (tsi_end_p (tsi));
17007
17008 if (i >= 3)
17009 {
17010 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17011 && TREE_CODE (stmts[1]) == DECL_EXPR);
17012 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17013 args, complain, in_decl);
17014 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17015 args, complain, in_decl);
17016 DECL_CONTEXT (omp_out) = current_function_decl;
17017 DECL_CONTEXT (omp_in) = current_function_decl;
17018 keep_next_level (true);
17019 tree block = begin_omp_structured_block ();
17020 tsubst_expr (stmts[2], args, complain, in_decl, false);
17021 block = finish_omp_structured_block (block);
17022 block = maybe_cleanup_point_expr_void (block);
17023 add_decl_expr (omp_out);
17024 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17025 TREE_NO_WARNING (omp_out) = 1;
17026 add_decl_expr (omp_in);
17027 finish_expr_stmt (block);
17028 }
17029 if (i >= 6)
17030 {
17031 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17032 && TREE_CODE (stmts[4]) == DECL_EXPR);
17033 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17034 args, complain, in_decl);
17035 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17036 args, complain, in_decl);
17037 DECL_CONTEXT (omp_priv) = current_function_decl;
17038 DECL_CONTEXT (omp_orig) = current_function_decl;
17039 keep_next_level (true);
17040 tree block = begin_omp_structured_block ();
17041 tsubst_expr (stmts[5], args, complain, in_decl, false);
17042 block = finish_omp_structured_block (block);
17043 block = maybe_cleanup_point_expr_void (block);
17044 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17045 add_decl_expr (omp_priv);
17046 add_decl_expr (omp_orig);
17047 finish_expr_stmt (block);
17048 if (i == 7)
17049 add_decl_expr (omp_orig);
17050 }
17051 }
17052
17053 /* T is a postfix-expression that is not being used in a function
17054 call. Return the substituted version of T. */
17055
17056 static tree
17057 tsubst_non_call_postfix_expression (tree t, tree args,
17058 tsubst_flags_t complain,
17059 tree in_decl)
17060 {
17061 if (TREE_CODE (t) == SCOPE_REF)
17062 t = tsubst_qualified_id (t, args, complain, in_decl,
17063 /*done=*/false, /*address_p=*/false);
17064 else
17065 t = tsubst_copy_and_build (t, args, complain, in_decl,
17066 /*function_p=*/false,
17067 /*integral_constant_expression_p=*/false);
17068
17069 return t;
17070 }
17071
17072 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17073 instantiation context. Instantiating a pack expansion containing a lambda
17074 might result in multiple lambdas all based on the same lambda in the
17075 template. */
17076
17077 tree
17078 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17079 {
17080 tree oldfn = lambda_function (t);
17081 in_decl = oldfn;
17082
17083 tree r = build_lambda_expr ();
17084
17085 LAMBDA_EXPR_LOCATION (r)
17086 = LAMBDA_EXPR_LOCATION (t);
17087 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17088 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17089 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17090
17091 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
17092 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
17093 else
17094 record_lambda_scope (r);
17095
17096 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17097 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17098
17099 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
17100 cap = TREE_CHAIN (cap))
17101 {
17102 tree field = TREE_PURPOSE (cap);
17103 if (PACK_EXPANSION_P (field))
17104 field = PACK_EXPANSION_PATTERN (field);
17105 field = tsubst_decl (field, args, complain);
17106
17107 if (field == error_mark_node)
17108 return error_mark_node;
17109
17110 tree init = TREE_VALUE (cap);
17111 if (PACK_EXPANSION_P (init))
17112 init = tsubst_pack_expansion (init, args, complain, in_decl);
17113 else
17114 init = tsubst_copy_and_build (init, args, complain, in_decl,
17115 /*fn*/false, /*constexpr*/false);
17116
17117 if (TREE_CODE (field) == TREE_VEC)
17118 {
17119 int len = TREE_VEC_LENGTH (field);
17120 gcc_assert (TREE_CODE (init) == TREE_VEC
17121 && TREE_VEC_LENGTH (init) == len);
17122 for (int i = 0; i < len; ++i)
17123 LAMBDA_EXPR_CAPTURE_LIST (r)
17124 = tree_cons (TREE_VEC_ELT (field, i),
17125 TREE_VEC_ELT (init, i),
17126 LAMBDA_EXPR_CAPTURE_LIST (r));
17127 }
17128 else
17129 {
17130 LAMBDA_EXPR_CAPTURE_LIST (r)
17131 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17132
17133 if (id_equal (DECL_NAME (field), "__this"))
17134 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17135 }
17136 }
17137
17138 tree type = begin_lambda_type (r);
17139
17140 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17141 determine_visibility (TYPE_NAME (type));
17142
17143 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17144
17145 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17146 ? DECL_TI_TEMPLATE (oldfn)
17147 : NULL_TREE);
17148
17149 tree fntype = static_fn_type (oldfn);
17150 if (oldtmpl)
17151 ++processing_template_decl;
17152 fntype = tsubst (fntype, args, complain, in_decl);
17153 if (oldtmpl)
17154 --processing_template_decl;
17155
17156 if (fntype == error_mark_node)
17157 r = error_mark_node;
17158 else
17159 {
17160 /* Fix the type of 'this'. */
17161 fntype = build_memfn_type (fntype, type,
17162 type_memfn_quals (fntype),
17163 type_memfn_rqual (fntype));
17164 tree fn, tmpl;
17165 if (oldtmpl)
17166 {
17167 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17168 fn = DECL_TEMPLATE_RESULT (tmpl);
17169 finish_member_declaration (tmpl);
17170 }
17171 else
17172 {
17173 tmpl = NULL_TREE;
17174 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17175 finish_member_declaration (fn);
17176 }
17177
17178 /* Let finish_function set this. */
17179 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17180
17181 bool nested = cfun;
17182 if (nested)
17183 push_function_context ();
17184 else
17185 /* Still increment function_depth so that we don't GC in the
17186 middle of an expression. */
17187 ++function_depth;
17188
17189 local_specialization_stack s (lss_copy);
17190
17191 tree body = start_lambda_function (fn, r);
17192
17193 register_parameter_specializations (oldfn, fn);
17194
17195 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17196 /*constexpr*/false);
17197
17198 finish_lambda_function (body);
17199
17200 if (nested)
17201 pop_function_context ();
17202 else
17203 --function_depth;
17204
17205 /* The capture list was built up in reverse order; fix that now. */
17206 LAMBDA_EXPR_CAPTURE_LIST (r)
17207 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17208
17209 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17210
17211 maybe_add_lambda_conv_op (type);
17212 }
17213
17214 finish_struct (type, /*attr*/NULL_TREE);
17215
17216 insert_pending_capture_proxies ();
17217
17218 return r;
17219 }
17220
17221 /* Like tsubst but deals with expressions and performs semantic
17222 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17223
17224 tree
17225 tsubst_copy_and_build (tree t,
17226 tree args,
17227 tsubst_flags_t complain,
17228 tree in_decl,
17229 bool function_p,
17230 bool integral_constant_expression_p)
17231 {
17232 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17233 #define RECUR(NODE) \
17234 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17235 /*function_p=*/false, \
17236 integral_constant_expression_p)
17237
17238 tree retval, op1;
17239 location_t loc;
17240
17241 if (t == NULL_TREE || t == error_mark_node)
17242 return t;
17243
17244 loc = input_location;
17245 if (EXPR_HAS_LOCATION (t))
17246 input_location = EXPR_LOCATION (t);
17247
17248 /* N3276 decltype magic only applies to calls at the top level or on the
17249 right side of a comma. */
17250 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17251 complain &= ~tf_decltype;
17252
17253 switch (TREE_CODE (t))
17254 {
17255 case USING_DECL:
17256 t = DECL_NAME (t);
17257 /* Fall through. */
17258 case IDENTIFIER_NODE:
17259 {
17260 tree decl;
17261 cp_id_kind idk;
17262 bool non_integral_constant_expression_p;
17263 const char *error_msg;
17264
17265 if (IDENTIFIER_CONV_OP_P (t))
17266 {
17267 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17268 t = make_conv_op_name (new_type);
17269 }
17270
17271 /* Look up the name. */
17272 decl = lookup_name (t);
17273
17274 /* By convention, expressions use ERROR_MARK_NODE to indicate
17275 failure, not NULL_TREE. */
17276 if (decl == NULL_TREE)
17277 decl = error_mark_node;
17278
17279 decl = finish_id_expression (t, decl, NULL_TREE,
17280 &idk,
17281 integral_constant_expression_p,
17282 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17283 &non_integral_constant_expression_p,
17284 /*template_p=*/false,
17285 /*done=*/true,
17286 /*address_p=*/false,
17287 /*template_arg_p=*/false,
17288 &error_msg,
17289 input_location);
17290 if (error_msg)
17291 error (error_msg);
17292 if (!function_p && identifier_p (decl))
17293 {
17294 if (complain & tf_error)
17295 unqualified_name_lookup_error (decl);
17296 decl = error_mark_node;
17297 }
17298 RETURN (decl);
17299 }
17300
17301 case TEMPLATE_ID_EXPR:
17302 {
17303 tree object;
17304 tree templ = RECUR (TREE_OPERAND (t, 0));
17305 tree targs = TREE_OPERAND (t, 1);
17306
17307 if (targs)
17308 targs = tsubst_template_args (targs, args, complain, in_decl);
17309 if (targs == error_mark_node)
17310 return error_mark_node;
17311
17312 if (TREE_CODE (templ) == SCOPE_REF)
17313 {
17314 tree name = TREE_OPERAND (templ, 1);
17315 tree tid = lookup_template_function (name, targs);
17316 TREE_OPERAND (templ, 1) = tid;
17317 return templ;
17318 }
17319
17320 if (variable_template_p (templ))
17321 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17322
17323 if (TREE_CODE (templ) == COMPONENT_REF)
17324 {
17325 object = TREE_OPERAND (templ, 0);
17326 templ = TREE_OPERAND (templ, 1);
17327 }
17328 else
17329 object = NULL_TREE;
17330 templ = lookup_template_function (templ, targs);
17331
17332 if (object)
17333 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17334 object, templ, NULL_TREE));
17335 else
17336 RETURN (baselink_for_fns (templ));
17337 }
17338
17339 case INDIRECT_REF:
17340 {
17341 tree r = RECUR (TREE_OPERAND (t, 0));
17342
17343 if (REFERENCE_REF_P (t))
17344 {
17345 /* A type conversion to reference type will be enclosed in
17346 such an indirect ref, but the substitution of the cast
17347 will have also added such an indirect ref. */
17348 r = convert_from_reference (r);
17349 }
17350 else
17351 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17352 complain|decltype_flag);
17353
17354 if (REF_PARENTHESIZED_P (t))
17355 r = force_paren_expr (r);
17356
17357 RETURN (r);
17358 }
17359
17360 case NOP_EXPR:
17361 {
17362 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17363 tree op0 = RECUR (TREE_OPERAND (t, 0));
17364 RETURN (build_nop (type, op0));
17365 }
17366
17367 case IMPLICIT_CONV_EXPR:
17368 {
17369 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17370 tree expr = RECUR (TREE_OPERAND (t, 0));
17371 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17372 {
17373 retval = copy_node (t);
17374 TREE_TYPE (retval) = type;
17375 TREE_OPERAND (retval, 0) = expr;
17376 RETURN (retval);
17377 }
17378 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17379 /* We'll pass this to convert_nontype_argument again, we don't need
17380 to actually perform any conversion here. */
17381 RETURN (expr);
17382 int flags = LOOKUP_IMPLICIT;
17383 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17384 flags = LOOKUP_NORMAL;
17385 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17386 flags));
17387 }
17388
17389 case CONVERT_EXPR:
17390 {
17391 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17392 tree op0 = RECUR (TREE_OPERAND (t, 0));
17393 RETURN (build1 (CONVERT_EXPR, type, op0));
17394 }
17395
17396 case CAST_EXPR:
17397 case REINTERPRET_CAST_EXPR:
17398 case CONST_CAST_EXPR:
17399 case DYNAMIC_CAST_EXPR:
17400 case STATIC_CAST_EXPR:
17401 {
17402 tree type;
17403 tree op, r = NULL_TREE;
17404
17405 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17406 if (integral_constant_expression_p
17407 && !cast_valid_in_integral_constant_expression_p (type))
17408 {
17409 if (complain & tf_error)
17410 error ("a cast to a type other than an integral or "
17411 "enumeration type cannot appear in a constant-expression");
17412 RETURN (error_mark_node);
17413 }
17414
17415 op = RECUR (TREE_OPERAND (t, 0));
17416
17417 warning_sentinel s(warn_useless_cast);
17418 warning_sentinel s2(warn_ignored_qualifiers);
17419 switch (TREE_CODE (t))
17420 {
17421 case CAST_EXPR:
17422 r = build_functional_cast (type, op, complain);
17423 break;
17424 case REINTERPRET_CAST_EXPR:
17425 r = build_reinterpret_cast (type, op, complain);
17426 break;
17427 case CONST_CAST_EXPR:
17428 r = build_const_cast (type, op, complain);
17429 break;
17430 case DYNAMIC_CAST_EXPR:
17431 r = build_dynamic_cast (type, op, complain);
17432 break;
17433 case STATIC_CAST_EXPR:
17434 r = build_static_cast (type, op, complain);
17435 break;
17436 default:
17437 gcc_unreachable ();
17438 }
17439
17440 RETURN (r);
17441 }
17442
17443 case POSTDECREMENT_EXPR:
17444 case POSTINCREMENT_EXPR:
17445 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17446 args, complain, in_decl);
17447 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17448 complain|decltype_flag));
17449
17450 case PREDECREMENT_EXPR:
17451 case PREINCREMENT_EXPR:
17452 case NEGATE_EXPR:
17453 case BIT_NOT_EXPR:
17454 case ABS_EXPR:
17455 case TRUTH_NOT_EXPR:
17456 case UNARY_PLUS_EXPR: /* Unary + */
17457 case REALPART_EXPR:
17458 case IMAGPART_EXPR:
17459 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17460 RECUR (TREE_OPERAND (t, 0)),
17461 complain|decltype_flag));
17462
17463 case FIX_TRUNC_EXPR:
17464 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17465 false, complain));
17466
17467 case ADDR_EXPR:
17468 op1 = TREE_OPERAND (t, 0);
17469 if (TREE_CODE (op1) == LABEL_DECL)
17470 RETURN (finish_label_address_expr (DECL_NAME (op1),
17471 EXPR_LOCATION (op1)));
17472 if (TREE_CODE (op1) == SCOPE_REF)
17473 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17474 /*done=*/true, /*address_p=*/true);
17475 else
17476 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17477 in_decl);
17478 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17479 complain|decltype_flag));
17480
17481 case PLUS_EXPR:
17482 case MINUS_EXPR:
17483 case MULT_EXPR:
17484 case TRUNC_DIV_EXPR:
17485 case CEIL_DIV_EXPR:
17486 case FLOOR_DIV_EXPR:
17487 case ROUND_DIV_EXPR:
17488 case EXACT_DIV_EXPR:
17489 case BIT_AND_EXPR:
17490 case BIT_IOR_EXPR:
17491 case BIT_XOR_EXPR:
17492 case TRUNC_MOD_EXPR:
17493 case FLOOR_MOD_EXPR:
17494 case TRUTH_ANDIF_EXPR:
17495 case TRUTH_ORIF_EXPR:
17496 case TRUTH_AND_EXPR:
17497 case TRUTH_OR_EXPR:
17498 case RSHIFT_EXPR:
17499 case LSHIFT_EXPR:
17500 case RROTATE_EXPR:
17501 case LROTATE_EXPR:
17502 case EQ_EXPR:
17503 case NE_EXPR:
17504 case MAX_EXPR:
17505 case MIN_EXPR:
17506 case LE_EXPR:
17507 case GE_EXPR:
17508 case LT_EXPR:
17509 case GT_EXPR:
17510 case MEMBER_REF:
17511 case DOTSTAR_EXPR:
17512 {
17513 warning_sentinel s1(warn_type_limits);
17514 warning_sentinel s2(warn_div_by_zero);
17515 warning_sentinel s3(warn_logical_op);
17516 warning_sentinel s4(warn_tautological_compare);
17517 tree op0 = RECUR (TREE_OPERAND (t, 0));
17518 tree op1 = RECUR (TREE_OPERAND (t, 1));
17519 tree r = build_x_binary_op
17520 (input_location, TREE_CODE (t),
17521 op0,
17522 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17523 ? ERROR_MARK
17524 : TREE_CODE (TREE_OPERAND (t, 0))),
17525 op1,
17526 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17527 ? ERROR_MARK
17528 : TREE_CODE (TREE_OPERAND (t, 1))),
17529 /*overload=*/NULL,
17530 complain|decltype_flag);
17531 if (EXPR_P (r) && TREE_NO_WARNING (t))
17532 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17533
17534 RETURN (r);
17535 }
17536
17537 case POINTER_PLUS_EXPR:
17538 {
17539 tree op0 = RECUR (TREE_OPERAND (t, 0));
17540 tree op1 = RECUR (TREE_OPERAND (t, 1));
17541 return fold_build_pointer_plus (op0, op1);
17542 }
17543
17544 case SCOPE_REF:
17545 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17546 /*address_p=*/false));
17547 case ARRAY_REF:
17548 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17549 args, complain, in_decl);
17550 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17551 RECUR (TREE_OPERAND (t, 1)),
17552 complain|decltype_flag));
17553
17554 case SIZEOF_EXPR:
17555 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17556 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17557 RETURN (tsubst_copy (t, args, complain, in_decl));
17558 /* Fall through */
17559
17560 case ALIGNOF_EXPR:
17561 {
17562 tree r;
17563
17564 op1 = TREE_OPERAND (t, 0);
17565 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17566 op1 = TREE_TYPE (op1);
17567 if (!args)
17568 {
17569 /* When there are no ARGS, we are trying to evaluate a
17570 non-dependent expression from the parser. Trying to do
17571 the substitutions may not work. */
17572 if (!TYPE_P (op1))
17573 op1 = TREE_TYPE (op1);
17574 }
17575 else
17576 {
17577 ++cp_unevaluated_operand;
17578 ++c_inhibit_evaluation_warnings;
17579 if (TYPE_P (op1))
17580 op1 = tsubst (op1, args, complain, in_decl);
17581 else
17582 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17583 /*function_p=*/false,
17584 /*integral_constant_expression_p=*/
17585 false);
17586 --cp_unevaluated_operand;
17587 --c_inhibit_evaluation_warnings;
17588 }
17589 if (TYPE_P (op1))
17590 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17591 complain & tf_error);
17592 else
17593 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17594 complain & tf_error);
17595 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17596 {
17597 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17598 {
17599 if (!processing_template_decl && TYPE_P (op1))
17600 {
17601 r = build_min (SIZEOF_EXPR, size_type_node,
17602 build1 (NOP_EXPR, op1, error_mark_node));
17603 SIZEOF_EXPR_TYPE_P (r) = 1;
17604 }
17605 else
17606 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17607 TREE_SIDE_EFFECTS (r) = 0;
17608 TREE_READONLY (r) = 1;
17609 }
17610 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17611 }
17612 RETURN (r);
17613 }
17614
17615 case AT_ENCODE_EXPR:
17616 {
17617 op1 = TREE_OPERAND (t, 0);
17618 ++cp_unevaluated_operand;
17619 ++c_inhibit_evaluation_warnings;
17620 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17621 /*function_p=*/false,
17622 /*integral_constant_expression_p=*/false);
17623 --cp_unevaluated_operand;
17624 --c_inhibit_evaluation_warnings;
17625 RETURN (objc_build_encode_expr (op1));
17626 }
17627
17628 case NOEXCEPT_EXPR:
17629 op1 = TREE_OPERAND (t, 0);
17630 ++cp_unevaluated_operand;
17631 ++c_inhibit_evaluation_warnings;
17632 ++cp_noexcept_operand;
17633 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17634 /*function_p=*/false,
17635 /*integral_constant_expression_p=*/false);
17636 --cp_unevaluated_operand;
17637 --c_inhibit_evaluation_warnings;
17638 --cp_noexcept_operand;
17639 RETURN (finish_noexcept_expr (op1, complain));
17640
17641 case MODOP_EXPR:
17642 {
17643 warning_sentinel s(warn_div_by_zero);
17644 tree lhs = RECUR (TREE_OPERAND (t, 0));
17645 tree rhs = RECUR (TREE_OPERAND (t, 2));
17646 tree r = build_x_modify_expr
17647 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17648 complain|decltype_flag);
17649 /* TREE_NO_WARNING must be set if either the expression was
17650 parenthesized or it uses an operator such as >>= rather
17651 than plain assignment. In the former case, it was already
17652 set and must be copied. In the latter case,
17653 build_x_modify_expr sets it and it must not be reset
17654 here. */
17655 if (TREE_NO_WARNING (t))
17656 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17657
17658 RETURN (r);
17659 }
17660
17661 case ARROW_EXPR:
17662 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17663 args, complain, in_decl);
17664 /* Remember that there was a reference to this entity. */
17665 if (DECL_P (op1)
17666 && !mark_used (op1, complain) && !(complain & tf_error))
17667 RETURN (error_mark_node);
17668 RETURN (build_x_arrow (input_location, op1, complain));
17669
17670 case NEW_EXPR:
17671 {
17672 tree placement = RECUR (TREE_OPERAND (t, 0));
17673 tree init = RECUR (TREE_OPERAND (t, 3));
17674 vec<tree, va_gc> *placement_vec;
17675 vec<tree, va_gc> *init_vec;
17676 tree ret;
17677
17678 if (placement == NULL_TREE)
17679 placement_vec = NULL;
17680 else
17681 {
17682 placement_vec = make_tree_vector ();
17683 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17684 vec_safe_push (placement_vec, TREE_VALUE (placement));
17685 }
17686
17687 /* If there was an initializer in the original tree, but it
17688 instantiated to an empty list, then we should pass a
17689 non-NULL empty vector to tell build_new that it was an
17690 empty initializer() rather than no initializer. This can
17691 only happen when the initializer is a pack expansion whose
17692 parameter packs are of length zero. */
17693 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17694 init_vec = NULL;
17695 else
17696 {
17697 init_vec = make_tree_vector ();
17698 if (init == void_node)
17699 gcc_assert (init_vec != NULL);
17700 else
17701 {
17702 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17703 vec_safe_push (init_vec, TREE_VALUE (init));
17704 }
17705 }
17706
17707 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17708 tree op2 = RECUR (TREE_OPERAND (t, 2));
17709 ret = build_new (&placement_vec, op1, op2, &init_vec,
17710 NEW_EXPR_USE_GLOBAL (t),
17711 complain);
17712
17713 if (placement_vec != NULL)
17714 release_tree_vector (placement_vec);
17715 if (init_vec != NULL)
17716 release_tree_vector (init_vec);
17717
17718 RETURN (ret);
17719 }
17720
17721 case DELETE_EXPR:
17722 {
17723 tree op0 = RECUR (TREE_OPERAND (t, 0));
17724 tree op1 = RECUR (TREE_OPERAND (t, 1));
17725 RETURN (delete_sanity (op0, op1,
17726 DELETE_EXPR_USE_VEC (t),
17727 DELETE_EXPR_USE_GLOBAL (t),
17728 complain));
17729 }
17730
17731 case COMPOUND_EXPR:
17732 {
17733 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17734 complain & ~tf_decltype, in_decl,
17735 /*function_p=*/false,
17736 integral_constant_expression_p);
17737 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17738 op0,
17739 RECUR (TREE_OPERAND (t, 1)),
17740 complain|decltype_flag));
17741 }
17742
17743 case CALL_EXPR:
17744 {
17745 tree function;
17746 vec<tree, va_gc> *call_args;
17747 unsigned int nargs, i;
17748 bool qualified_p;
17749 bool koenig_p;
17750 tree ret;
17751
17752 function = CALL_EXPR_FN (t);
17753 /* Internal function with no arguments. */
17754 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17755 RETURN (t);
17756
17757 /* When we parsed the expression, we determined whether or
17758 not Koenig lookup should be performed. */
17759 koenig_p = KOENIG_LOOKUP_P (t);
17760 if (function == NULL_TREE)
17761 {
17762 koenig_p = false;
17763 qualified_p = false;
17764 }
17765 else if (TREE_CODE (function) == SCOPE_REF)
17766 {
17767 qualified_p = true;
17768 function = tsubst_qualified_id (function, args, complain, in_decl,
17769 /*done=*/false,
17770 /*address_p=*/false);
17771 }
17772 else if (koenig_p && identifier_p (function))
17773 {
17774 /* Do nothing; calling tsubst_copy_and_build on an identifier
17775 would incorrectly perform unqualified lookup again.
17776
17777 Note that we can also have an IDENTIFIER_NODE if the earlier
17778 unqualified lookup found a member function; in that case
17779 koenig_p will be false and we do want to do the lookup
17780 again to find the instantiated member function.
17781
17782 FIXME but doing that causes c++/15272, so we need to stop
17783 using IDENTIFIER_NODE in that situation. */
17784 qualified_p = false;
17785 }
17786 else
17787 {
17788 if (TREE_CODE (function) == COMPONENT_REF)
17789 {
17790 tree op = TREE_OPERAND (function, 1);
17791
17792 qualified_p = (TREE_CODE (op) == SCOPE_REF
17793 || (BASELINK_P (op)
17794 && BASELINK_QUALIFIED_P (op)));
17795 }
17796 else
17797 qualified_p = false;
17798
17799 if (TREE_CODE (function) == ADDR_EXPR
17800 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17801 /* Avoid error about taking the address of a constructor. */
17802 function = TREE_OPERAND (function, 0);
17803
17804 function = tsubst_copy_and_build (function, args, complain,
17805 in_decl,
17806 !qualified_p,
17807 integral_constant_expression_p);
17808
17809 if (BASELINK_P (function))
17810 qualified_p = true;
17811 }
17812
17813 nargs = call_expr_nargs (t);
17814 call_args = make_tree_vector ();
17815 for (i = 0; i < nargs; ++i)
17816 {
17817 tree arg = CALL_EXPR_ARG (t, i);
17818
17819 if (!PACK_EXPANSION_P (arg))
17820 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17821 else
17822 {
17823 /* Expand the pack expansion and push each entry onto
17824 CALL_ARGS. */
17825 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17826 if (TREE_CODE (arg) == TREE_VEC)
17827 {
17828 unsigned int len, j;
17829
17830 len = TREE_VEC_LENGTH (arg);
17831 for (j = 0; j < len; ++j)
17832 {
17833 tree value = TREE_VEC_ELT (arg, j);
17834 if (value != NULL_TREE)
17835 value = convert_from_reference (value);
17836 vec_safe_push (call_args, value);
17837 }
17838 }
17839 else
17840 {
17841 /* A partial substitution. Add one entry. */
17842 vec_safe_push (call_args, arg);
17843 }
17844 }
17845 }
17846
17847 /* We do not perform argument-dependent lookup if normal
17848 lookup finds a non-function, in accordance with the
17849 expected resolution of DR 218. */
17850 if (koenig_p
17851 && ((is_overloaded_fn (function)
17852 /* If lookup found a member function, the Koenig lookup is
17853 not appropriate, even if an unqualified-name was used
17854 to denote the function. */
17855 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17856 || identifier_p (function))
17857 /* Only do this when substitution turns a dependent call
17858 into a non-dependent call. */
17859 && type_dependent_expression_p_push (t)
17860 && !any_type_dependent_arguments_p (call_args))
17861 function = perform_koenig_lookup (function, call_args, tf_none);
17862
17863 if (function != NULL_TREE
17864 && identifier_p (function)
17865 && !any_type_dependent_arguments_p (call_args))
17866 {
17867 if (koenig_p && (complain & tf_warning_or_error))
17868 {
17869 /* For backwards compatibility and good diagnostics, try
17870 the unqualified lookup again if we aren't in SFINAE
17871 context. */
17872 tree unq = (tsubst_copy_and_build
17873 (function, args, complain, in_decl, true,
17874 integral_constant_expression_p));
17875 if (unq == error_mark_node)
17876 {
17877 release_tree_vector (call_args);
17878 RETURN (error_mark_node);
17879 }
17880
17881 if (unq != function)
17882 {
17883 /* In a lambda fn, we have to be careful to not
17884 introduce new this captures. Legacy code can't
17885 be using lambdas anyway, so it's ok to be
17886 stricter. */
17887 bool in_lambda = (current_class_type
17888 && LAMBDA_TYPE_P (current_class_type));
17889 char const *const msg
17890 = G_("%qD was not declared in this scope, "
17891 "and no declarations were found by "
17892 "argument-dependent lookup at the point "
17893 "of instantiation");
17894
17895 bool diag = true;
17896 if (in_lambda)
17897 error_at (EXPR_LOC_OR_LOC (t, input_location),
17898 msg, function);
17899 else
17900 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17901 msg, function);
17902 if (diag)
17903 {
17904 tree fn = unq;
17905
17906 if (INDIRECT_REF_P (fn))
17907 fn = TREE_OPERAND (fn, 0);
17908 if (is_overloaded_fn (fn))
17909 fn = get_first_fn (fn);
17910
17911 if (!DECL_P (fn))
17912 /* Can't say anything more. */;
17913 else if (DECL_CLASS_SCOPE_P (fn))
17914 {
17915 location_t loc = EXPR_LOC_OR_LOC (t,
17916 input_location);
17917 inform (loc,
17918 "declarations in dependent base %qT are "
17919 "not found by unqualified lookup",
17920 DECL_CLASS_CONTEXT (fn));
17921 if (current_class_ptr)
17922 inform (loc,
17923 "use %<this->%D%> instead", function);
17924 else
17925 inform (loc,
17926 "use %<%T::%D%> instead",
17927 current_class_name, function);
17928 }
17929 else
17930 inform (DECL_SOURCE_LOCATION (fn),
17931 "%qD declared here, later in the "
17932 "translation unit", fn);
17933 if (in_lambda)
17934 {
17935 release_tree_vector (call_args);
17936 RETURN (error_mark_node);
17937 }
17938 }
17939
17940 function = unq;
17941 }
17942 }
17943 if (identifier_p (function))
17944 {
17945 if (complain & tf_error)
17946 unqualified_name_lookup_error (function);
17947 release_tree_vector (call_args);
17948 RETURN (error_mark_node);
17949 }
17950 }
17951
17952 /* Remember that there was a reference to this entity. */
17953 if (function != NULL_TREE
17954 && DECL_P (function)
17955 && !mark_used (function, complain) && !(complain & tf_error))
17956 {
17957 release_tree_vector (call_args);
17958 RETURN (error_mark_node);
17959 }
17960
17961 /* Put back tf_decltype for the actual call. */
17962 complain |= decltype_flag;
17963
17964 if (function == NULL_TREE)
17965 switch (CALL_EXPR_IFN (t))
17966 {
17967 case IFN_LAUNDER:
17968 gcc_assert (nargs == 1);
17969 if (vec_safe_length (call_args) != 1)
17970 {
17971 error_at (EXPR_LOC_OR_LOC (t, input_location),
17972 "wrong number of arguments to "
17973 "%<__builtin_launder%>");
17974 ret = error_mark_node;
17975 }
17976 else
17977 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17978 input_location),
17979 (*call_args)[0], complain);
17980 break;
17981
17982 default:
17983 /* Unsupported internal function with arguments. */
17984 gcc_unreachable ();
17985 }
17986 else if (TREE_CODE (function) == OFFSET_REF)
17987 ret = build_offset_ref_call_from_tree (function, &call_args,
17988 complain);
17989 else if (TREE_CODE (function) == COMPONENT_REF)
17990 {
17991 tree instance = TREE_OPERAND (function, 0);
17992 tree fn = TREE_OPERAND (function, 1);
17993
17994 if (processing_template_decl
17995 && (type_dependent_expression_p (instance)
17996 || (!BASELINK_P (fn)
17997 && TREE_CODE (fn) != FIELD_DECL)
17998 || type_dependent_expression_p (fn)
17999 || any_type_dependent_arguments_p (call_args)))
18000 ret = build_min_nt_call_vec (function, call_args);
18001 else if (!BASELINK_P (fn))
18002 ret = finish_call_expr (function, &call_args,
18003 /*disallow_virtual=*/false,
18004 /*koenig_p=*/false,
18005 complain);
18006 else
18007 ret = (build_new_method_call
18008 (instance, fn,
18009 &call_args, NULL_TREE,
18010 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
18011 /*fn_p=*/NULL,
18012 complain));
18013 }
18014 else
18015 ret = finish_call_expr (function, &call_args,
18016 /*disallow_virtual=*/qualified_p,
18017 koenig_p,
18018 complain);
18019
18020 release_tree_vector (call_args);
18021
18022 if (ret != error_mark_node)
18023 {
18024 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
18025 bool ord = CALL_EXPR_ORDERED_ARGS (t);
18026 bool rev = CALL_EXPR_REVERSE_ARGS (t);
18027 bool thk = CALL_FROM_THUNK_P (t);
18028 if (op || ord || rev || thk)
18029 {
18030 function = extract_call_expr (ret);
18031 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
18032 CALL_EXPR_ORDERED_ARGS (function) = ord;
18033 CALL_EXPR_REVERSE_ARGS (function) = rev;
18034 if (thk)
18035 {
18036 if (TREE_CODE (function) == CALL_EXPR)
18037 CALL_FROM_THUNK_P (function) = true;
18038 else
18039 AGGR_INIT_FROM_THUNK_P (function) = true;
18040 /* The thunk location is not interesting. */
18041 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
18042 }
18043 }
18044 }
18045
18046 RETURN (ret);
18047 }
18048
18049 case COND_EXPR:
18050 {
18051 tree cond = RECUR (TREE_OPERAND (t, 0));
18052 tree folded_cond = fold_non_dependent_expr (cond);
18053 tree exp1, exp2;
18054
18055 if (TREE_CODE (folded_cond) == INTEGER_CST)
18056 {
18057 if (integer_zerop (folded_cond))
18058 {
18059 ++c_inhibit_evaluation_warnings;
18060 exp1 = RECUR (TREE_OPERAND (t, 1));
18061 --c_inhibit_evaluation_warnings;
18062 exp2 = RECUR (TREE_OPERAND (t, 2));
18063 }
18064 else
18065 {
18066 exp1 = RECUR (TREE_OPERAND (t, 1));
18067 ++c_inhibit_evaluation_warnings;
18068 exp2 = RECUR (TREE_OPERAND (t, 2));
18069 --c_inhibit_evaluation_warnings;
18070 }
18071 cond = folded_cond;
18072 }
18073 else
18074 {
18075 exp1 = RECUR (TREE_OPERAND (t, 1));
18076 exp2 = RECUR (TREE_OPERAND (t, 2));
18077 }
18078
18079 warning_sentinel s(warn_duplicated_branches);
18080 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
18081 cond, exp1, exp2, complain));
18082 }
18083
18084 case PSEUDO_DTOR_EXPR:
18085 {
18086 tree op0 = RECUR (TREE_OPERAND (t, 0));
18087 tree op1 = RECUR (TREE_OPERAND (t, 1));
18088 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
18089 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
18090 input_location));
18091 }
18092
18093 case TREE_LIST:
18094 {
18095 tree purpose, value, chain;
18096
18097 if (t == void_list_node)
18098 RETURN (t);
18099
18100 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
18101 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
18102 {
18103 /* We have pack expansions, so expand those and
18104 create a new list out of it. */
18105 tree purposevec = NULL_TREE;
18106 tree valuevec = NULL_TREE;
18107 tree chain;
18108 int i, len = -1;
18109
18110 /* Expand the argument expressions. */
18111 if (TREE_PURPOSE (t))
18112 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
18113 complain, in_decl);
18114 if (TREE_VALUE (t))
18115 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
18116 complain, in_decl);
18117
18118 /* Build the rest of the list. */
18119 chain = TREE_CHAIN (t);
18120 if (chain && chain != void_type_node)
18121 chain = RECUR (chain);
18122
18123 /* Determine the number of arguments. */
18124 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
18125 {
18126 len = TREE_VEC_LENGTH (purposevec);
18127 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18128 }
18129 else if (TREE_CODE (valuevec) == TREE_VEC)
18130 len = TREE_VEC_LENGTH (valuevec);
18131 else
18132 {
18133 /* Since we only performed a partial substitution into
18134 the argument pack, we only RETURN (a single list
18135 node. */
18136 if (purposevec == TREE_PURPOSE (t)
18137 && valuevec == TREE_VALUE (t)
18138 && chain == TREE_CHAIN (t))
18139 RETURN (t);
18140
18141 RETURN (tree_cons (purposevec, valuevec, chain));
18142 }
18143
18144 /* Convert the argument vectors into a TREE_LIST */
18145 i = len;
18146 while (i > 0)
18147 {
18148 /* Grab the Ith values. */
18149 i--;
18150 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
18151 : NULL_TREE;
18152 value
18153 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
18154 : NULL_TREE;
18155
18156 /* Build the list (backwards). */
18157 chain = tree_cons (purpose, value, chain);
18158 }
18159
18160 RETURN (chain);
18161 }
18162
18163 purpose = TREE_PURPOSE (t);
18164 if (purpose)
18165 purpose = RECUR (purpose);
18166 value = TREE_VALUE (t);
18167 if (value)
18168 value = RECUR (value);
18169 chain = TREE_CHAIN (t);
18170 if (chain && chain != void_type_node)
18171 chain = RECUR (chain);
18172 if (purpose == TREE_PURPOSE (t)
18173 && value == TREE_VALUE (t)
18174 && chain == TREE_CHAIN (t))
18175 RETURN (t);
18176 RETURN (tree_cons (purpose, value, chain));
18177 }
18178
18179 case COMPONENT_REF:
18180 {
18181 tree object;
18182 tree object_type;
18183 tree member;
18184 tree r;
18185
18186 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18187 args, complain, in_decl);
18188 /* Remember that there was a reference to this entity. */
18189 if (DECL_P (object)
18190 && !mark_used (object, complain) && !(complain & tf_error))
18191 RETURN (error_mark_node);
18192 object_type = TREE_TYPE (object);
18193
18194 member = TREE_OPERAND (t, 1);
18195 if (BASELINK_P (member))
18196 member = tsubst_baselink (member,
18197 non_reference (TREE_TYPE (object)),
18198 args, complain, in_decl);
18199 else
18200 member = tsubst_copy (member, args, complain, in_decl);
18201 if (member == error_mark_node)
18202 RETURN (error_mark_node);
18203
18204 if (TREE_CODE (member) == FIELD_DECL)
18205 {
18206 r = finish_non_static_data_member (member, object, NULL_TREE);
18207 if (TREE_CODE (r) == COMPONENT_REF)
18208 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18209 RETURN (r);
18210 }
18211 else if (type_dependent_expression_p (object))
18212 /* We can't do much here. */;
18213 else if (!CLASS_TYPE_P (object_type))
18214 {
18215 if (scalarish_type_p (object_type))
18216 {
18217 tree s = NULL_TREE;
18218 tree dtor = member;
18219
18220 if (TREE_CODE (dtor) == SCOPE_REF)
18221 {
18222 s = TREE_OPERAND (dtor, 0);
18223 dtor = TREE_OPERAND (dtor, 1);
18224 }
18225 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18226 {
18227 dtor = TREE_OPERAND (dtor, 0);
18228 if (TYPE_P (dtor))
18229 RETURN (finish_pseudo_destructor_expr
18230 (object, s, dtor, input_location));
18231 }
18232 }
18233 }
18234 else if (TREE_CODE (member) == SCOPE_REF
18235 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18236 {
18237 /* Lookup the template functions now that we know what the
18238 scope is. */
18239 tree scope = TREE_OPERAND (member, 0);
18240 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18241 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18242 member = lookup_qualified_name (scope, tmpl,
18243 /*is_type_p=*/false,
18244 /*complain=*/false);
18245 if (BASELINK_P (member))
18246 {
18247 BASELINK_FUNCTIONS (member)
18248 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18249 args);
18250 member = (adjust_result_of_qualified_name_lookup
18251 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18252 object_type));
18253 }
18254 else
18255 {
18256 qualified_name_lookup_error (scope, tmpl, member,
18257 input_location);
18258 RETURN (error_mark_node);
18259 }
18260 }
18261 else if (TREE_CODE (member) == SCOPE_REF
18262 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18263 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18264 {
18265 if (complain & tf_error)
18266 {
18267 if (TYPE_P (TREE_OPERAND (member, 0)))
18268 error ("%qT is not a class or namespace",
18269 TREE_OPERAND (member, 0));
18270 else
18271 error ("%qD is not a class or namespace",
18272 TREE_OPERAND (member, 0));
18273 }
18274 RETURN (error_mark_node);
18275 }
18276
18277 r = finish_class_member_access_expr (object, member,
18278 /*template_p=*/false,
18279 complain);
18280 if (TREE_CODE (r) == COMPONENT_REF)
18281 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18282 RETURN (r);
18283 }
18284
18285 case THROW_EXPR:
18286 RETURN (build_throw
18287 (RECUR (TREE_OPERAND (t, 0))));
18288
18289 case CONSTRUCTOR:
18290 {
18291 vec<constructor_elt, va_gc> *n;
18292 constructor_elt *ce;
18293 unsigned HOST_WIDE_INT idx;
18294 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18295 bool process_index_p;
18296 int newlen;
18297 bool need_copy_p = false;
18298 tree r;
18299
18300 if (type == error_mark_node)
18301 RETURN (error_mark_node);
18302
18303 /* We do not want to process the index of aggregate
18304 initializers as they are identifier nodes which will be
18305 looked up by digest_init. */
18306 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18307
18308 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18309 newlen = vec_safe_length (n);
18310 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18311 {
18312 if (ce->index && process_index_p
18313 /* An identifier index is looked up in the type
18314 being initialized, not the current scope. */
18315 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18316 ce->index = RECUR (ce->index);
18317
18318 if (PACK_EXPANSION_P (ce->value))
18319 {
18320 /* Substitute into the pack expansion. */
18321 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18322 in_decl);
18323
18324 if (ce->value == error_mark_node
18325 || PACK_EXPANSION_P (ce->value))
18326 ;
18327 else if (TREE_VEC_LENGTH (ce->value) == 1)
18328 /* Just move the argument into place. */
18329 ce->value = TREE_VEC_ELT (ce->value, 0);
18330 else
18331 {
18332 /* Update the length of the final CONSTRUCTOR
18333 arguments vector, and note that we will need to
18334 copy.*/
18335 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18336 need_copy_p = true;
18337 }
18338 }
18339 else
18340 ce->value = RECUR (ce->value);
18341 }
18342
18343 if (need_copy_p)
18344 {
18345 vec<constructor_elt, va_gc> *old_n = n;
18346
18347 vec_alloc (n, newlen);
18348 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18349 {
18350 if (TREE_CODE (ce->value) == TREE_VEC)
18351 {
18352 int i, len = TREE_VEC_LENGTH (ce->value);
18353 for (i = 0; i < len; ++i)
18354 CONSTRUCTOR_APPEND_ELT (n, 0,
18355 TREE_VEC_ELT (ce->value, i));
18356 }
18357 else
18358 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18359 }
18360 }
18361
18362 r = build_constructor (init_list_type_node, n);
18363 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18364
18365 if (TREE_HAS_CONSTRUCTOR (t))
18366 {
18367 fcl_t cl = fcl_functional;
18368 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18369 cl = fcl_c99;
18370 RETURN (finish_compound_literal (type, r, complain, cl));
18371 }
18372
18373 TREE_TYPE (r) = type;
18374 RETURN (r);
18375 }
18376
18377 case TYPEID_EXPR:
18378 {
18379 tree operand_0 = TREE_OPERAND (t, 0);
18380 if (TYPE_P (operand_0))
18381 {
18382 operand_0 = tsubst (operand_0, args, complain, in_decl);
18383 RETURN (get_typeid (operand_0, complain));
18384 }
18385 else
18386 {
18387 operand_0 = RECUR (operand_0);
18388 RETURN (build_typeid (operand_0, complain));
18389 }
18390 }
18391
18392 case VAR_DECL:
18393 if (!args)
18394 RETURN (t);
18395 /* Fall through */
18396
18397 case PARM_DECL:
18398 {
18399 tree r = tsubst_copy (t, args, complain, in_decl);
18400 /* ??? We're doing a subset of finish_id_expression here. */
18401 if (VAR_P (r)
18402 && !processing_template_decl
18403 && !cp_unevaluated_operand
18404 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18405 && CP_DECL_THREAD_LOCAL_P (r))
18406 {
18407 if (tree wrap = get_tls_wrapper_fn (r))
18408 /* Replace an evaluated use of the thread_local variable with
18409 a call to its wrapper. */
18410 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18411 }
18412 else if (outer_automatic_var_p (r))
18413 r = process_outer_var_ref (r, complain);
18414
18415 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18416 /* If the original type was a reference, we'll be wrapped in
18417 the appropriate INDIRECT_REF. */
18418 r = convert_from_reference (r);
18419 RETURN (r);
18420 }
18421
18422 case VA_ARG_EXPR:
18423 {
18424 tree op0 = RECUR (TREE_OPERAND (t, 0));
18425 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18426 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18427 }
18428
18429 case OFFSETOF_EXPR:
18430 {
18431 tree object_ptr
18432 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18433 in_decl, /*function_p=*/false,
18434 /*integral_constant_expression_p=*/false);
18435 RETURN (finish_offsetof (object_ptr,
18436 RECUR (TREE_OPERAND (t, 0)),
18437 EXPR_LOCATION (t)));
18438 }
18439
18440 case ADDRESSOF_EXPR:
18441 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18442 RECUR (TREE_OPERAND (t, 0)), complain));
18443
18444 case TRAIT_EXPR:
18445 {
18446 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18447 complain, in_decl);
18448
18449 tree type2 = TRAIT_EXPR_TYPE2 (t);
18450 if (type2 && TREE_CODE (type2) == TREE_LIST)
18451 type2 = RECUR (type2);
18452 else if (type2)
18453 type2 = tsubst (type2, args, complain, in_decl);
18454
18455 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18456 }
18457
18458 case STMT_EXPR:
18459 {
18460 tree old_stmt_expr = cur_stmt_expr;
18461 tree stmt_expr = begin_stmt_expr ();
18462
18463 cur_stmt_expr = stmt_expr;
18464 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18465 integral_constant_expression_p);
18466 stmt_expr = finish_stmt_expr (stmt_expr, false);
18467 cur_stmt_expr = old_stmt_expr;
18468
18469 /* If the resulting list of expression statement is empty,
18470 fold it further into void_node. */
18471 if (empty_expr_stmt_p (stmt_expr))
18472 stmt_expr = void_node;
18473
18474 RETURN (stmt_expr);
18475 }
18476
18477 case LAMBDA_EXPR:
18478 {
18479 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18480
18481 RETURN (build_lambda_object (r));
18482 }
18483
18484 case TARGET_EXPR:
18485 /* We can get here for a constant initializer of non-dependent type.
18486 FIXME stop folding in cp_parser_initializer_clause. */
18487 {
18488 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18489 complain);
18490 RETURN (r);
18491 }
18492
18493 case TRANSACTION_EXPR:
18494 RETURN (tsubst_expr(t, args, complain, in_decl,
18495 integral_constant_expression_p));
18496
18497 case PAREN_EXPR:
18498 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18499
18500 case VEC_PERM_EXPR:
18501 {
18502 tree op0 = RECUR (TREE_OPERAND (t, 0));
18503 tree op1 = RECUR (TREE_OPERAND (t, 1));
18504 tree op2 = RECUR (TREE_OPERAND (t, 2));
18505 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18506 complain));
18507 }
18508
18509 case REQUIRES_EXPR:
18510 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18511
18512 case NON_LVALUE_EXPR:
18513 case VIEW_CONVERT_EXPR:
18514 /* We should only see these for location wrapper nodes, or within
18515 instantiate_non_dependent_expr (when args is NULL_TREE). */
18516 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
18517 if (location_wrapper_p (t))
18518 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
18519 EXPR_LOCATION (t)));
18520 /* fallthrough. */
18521
18522 default:
18523 /* Handle Objective-C++ constructs, if appropriate. */
18524 {
18525 tree subst
18526 = objcp_tsubst_copy_and_build (t, args, complain,
18527 in_decl, /*function_p=*/false);
18528 if (subst)
18529 RETURN (subst);
18530 }
18531 RETURN (tsubst_copy (t, args, complain, in_decl));
18532 }
18533
18534 #undef RECUR
18535 #undef RETURN
18536 out:
18537 input_location = loc;
18538 return retval;
18539 }
18540
18541 /* Verify that the instantiated ARGS are valid. For type arguments,
18542 make sure that the type's linkage is ok. For non-type arguments,
18543 make sure they are constants if they are integral or enumerations.
18544 Emit an error under control of COMPLAIN, and return TRUE on error. */
18545
18546 static bool
18547 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18548 {
18549 if (dependent_template_arg_p (t))
18550 return false;
18551 if (ARGUMENT_PACK_P (t))
18552 {
18553 tree vec = ARGUMENT_PACK_ARGS (t);
18554 int len = TREE_VEC_LENGTH (vec);
18555 bool result = false;
18556 int i;
18557
18558 for (i = 0; i < len; ++i)
18559 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18560 result = true;
18561 return result;
18562 }
18563 else if (TYPE_P (t))
18564 {
18565 /* [basic.link]: A name with no linkage (notably, the name
18566 of a class or enumeration declared in a local scope)
18567 shall not be used to declare an entity with linkage.
18568 This implies that names with no linkage cannot be used as
18569 template arguments
18570
18571 DR 757 relaxes this restriction for C++0x. */
18572 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18573 : no_linkage_check (t, /*relaxed_p=*/false));
18574
18575 if (nt)
18576 {
18577 /* DR 488 makes use of a type with no linkage cause
18578 type deduction to fail. */
18579 if (complain & tf_error)
18580 {
18581 if (TYPE_UNNAMED_P (nt))
18582 error ("%qT is/uses unnamed type", t);
18583 else
18584 error ("template argument for %qD uses local type %qT",
18585 tmpl, t);
18586 }
18587 return true;
18588 }
18589 /* In order to avoid all sorts of complications, we do not
18590 allow variably-modified types as template arguments. */
18591 else if (variably_modified_type_p (t, NULL_TREE))
18592 {
18593 if (complain & tf_error)
18594 error ("%qT is a variably modified type", t);
18595 return true;
18596 }
18597 }
18598 /* Class template and alias template arguments should be OK. */
18599 else if (DECL_TYPE_TEMPLATE_P (t))
18600 ;
18601 /* A non-type argument of integral or enumerated type must be a
18602 constant. */
18603 else if (TREE_TYPE (t)
18604 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18605 && !REFERENCE_REF_P (t)
18606 && !TREE_CONSTANT (t))
18607 {
18608 if (complain & tf_error)
18609 error ("integral expression %qE is not constant", t);
18610 return true;
18611 }
18612 return false;
18613 }
18614
18615 static bool
18616 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18617 {
18618 int ix, len = DECL_NTPARMS (tmpl);
18619 bool result = false;
18620
18621 for (ix = 0; ix != len; ix++)
18622 {
18623 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18624 result = true;
18625 }
18626 if (result && (complain & tf_error))
18627 error (" trying to instantiate %qD", tmpl);
18628 return result;
18629 }
18630
18631 /* We're out of SFINAE context now, so generate diagnostics for the access
18632 errors we saw earlier when instantiating D from TMPL and ARGS. */
18633
18634 static void
18635 recheck_decl_substitution (tree d, tree tmpl, tree args)
18636 {
18637 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18638 tree type = TREE_TYPE (pattern);
18639 location_t loc = input_location;
18640
18641 push_access_scope (d);
18642 push_deferring_access_checks (dk_no_deferred);
18643 input_location = DECL_SOURCE_LOCATION (pattern);
18644 tsubst (type, args, tf_warning_or_error, d);
18645 input_location = loc;
18646 pop_deferring_access_checks ();
18647 pop_access_scope (d);
18648 }
18649
18650 /* Instantiate the indicated variable, function, or alias template TMPL with
18651 the template arguments in TARG_PTR. */
18652
18653 static tree
18654 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18655 {
18656 tree targ_ptr = orig_args;
18657 tree fndecl;
18658 tree gen_tmpl;
18659 tree spec;
18660 bool access_ok = true;
18661
18662 if (tmpl == error_mark_node)
18663 return error_mark_node;
18664
18665 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18666
18667 /* If this function is a clone, handle it specially. */
18668 if (DECL_CLONED_FUNCTION_P (tmpl))
18669 {
18670 tree spec;
18671 tree clone;
18672
18673 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18674 DECL_CLONED_FUNCTION. */
18675 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18676 targ_ptr, complain);
18677 if (spec == error_mark_node)
18678 return error_mark_node;
18679
18680 /* Look for the clone. */
18681 FOR_EACH_CLONE (clone, spec)
18682 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18683 return clone;
18684 /* We should always have found the clone by now. */
18685 gcc_unreachable ();
18686 return NULL_TREE;
18687 }
18688
18689 if (targ_ptr == error_mark_node)
18690 return error_mark_node;
18691
18692 /* Check to see if we already have this specialization. */
18693 gen_tmpl = most_general_template (tmpl);
18694 if (TMPL_ARGS_DEPTH (targ_ptr)
18695 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18696 /* targ_ptr only has the innermost template args, so add the outer ones
18697 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18698 the case of a non-dependent call within a template definition). */
18699 targ_ptr = (add_outermost_template_args
18700 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18701 targ_ptr));
18702
18703 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18704 but it doesn't seem to be on the hot path. */
18705 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18706
18707 gcc_assert (tmpl == gen_tmpl
18708 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18709 == spec)
18710 || fndecl == NULL_TREE);
18711
18712 if (spec != NULL_TREE)
18713 {
18714 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18715 {
18716 if (complain & tf_error)
18717 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18718 return error_mark_node;
18719 }
18720 return spec;
18721 }
18722
18723 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18724 complain))
18725 return error_mark_node;
18726
18727 /* We are building a FUNCTION_DECL, during which the access of its
18728 parameters and return types have to be checked. However this
18729 FUNCTION_DECL which is the desired context for access checking
18730 is not built yet. We solve this chicken-and-egg problem by
18731 deferring all checks until we have the FUNCTION_DECL. */
18732 push_deferring_access_checks (dk_deferred);
18733
18734 /* Instantiation of the function happens in the context of the function
18735 template, not the context of the overload resolution we're doing. */
18736 push_to_top_level ();
18737 /* If there are dependent arguments, e.g. because we're doing partial
18738 ordering, make sure processing_template_decl stays set. */
18739 if (uses_template_parms (targ_ptr))
18740 ++processing_template_decl;
18741 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18742 {
18743 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18744 complain, gen_tmpl, true);
18745 push_nested_class (ctx);
18746 }
18747
18748 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18749
18750 fndecl = NULL_TREE;
18751 if (VAR_P (pattern))
18752 {
18753 /* We need to determine if we're using a partial or explicit
18754 specialization now, because the type of the variable could be
18755 different. */
18756 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18757 tree elt = most_specialized_partial_spec (tid, complain);
18758 if (elt == error_mark_node)
18759 pattern = error_mark_node;
18760 else if (elt)
18761 {
18762 tree partial_tmpl = TREE_VALUE (elt);
18763 tree partial_args = TREE_PURPOSE (elt);
18764 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18765 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18766 }
18767 }
18768
18769 /* Substitute template parameters to obtain the specialization. */
18770 if (fndecl == NULL_TREE)
18771 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18772 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18773 pop_nested_class ();
18774 pop_from_top_level ();
18775
18776 if (fndecl == error_mark_node)
18777 {
18778 pop_deferring_access_checks ();
18779 return error_mark_node;
18780 }
18781
18782 /* The DECL_TI_TEMPLATE should always be the immediate parent
18783 template, not the most general template. */
18784 DECL_TI_TEMPLATE (fndecl) = tmpl;
18785 DECL_TI_ARGS (fndecl) = targ_ptr;
18786
18787 /* Now we know the specialization, compute access previously
18788 deferred. Do no access control for inheriting constructors,
18789 as we already checked access for the inherited constructor. */
18790 if (!(flag_new_inheriting_ctors
18791 && DECL_INHERITED_CTOR (fndecl)))
18792 {
18793 push_access_scope (fndecl);
18794 if (!perform_deferred_access_checks (complain))
18795 access_ok = false;
18796 pop_access_scope (fndecl);
18797 }
18798 pop_deferring_access_checks ();
18799
18800 /* If we've just instantiated the main entry point for a function,
18801 instantiate all the alternate entry points as well. We do this
18802 by cloning the instantiation of the main entry point, not by
18803 instantiating the template clones. */
18804 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18805 clone_function_decl (fndecl, /*update_methods=*/false);
18806
18807 if (!access_ok)
18808 {
18809 if (!(complain & tf_error))
18810 {
18811 /* Remember to reinstantiate when we're out of SFINAE so the user
18812 can see the errors. */
18813 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18814 }
18815 return error_mark_node;
18816 }
18817 return fndecl;
18818 }
18819
18820 /* Wrapper for instantiate_template_1. */
18821
18822 tree
18823 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18824 {
18825 tree ret;
18826 timevar_push (TV_TEMPLATE_INST);
18827 ret = instantiate_template_1 (tmpl, orig_args, complain);
18828 timevar_pop (TV_TEMPLATE_INST);
18829 return ret;
18830 }
18831
18832 /* Instantiate the alias template TMPL with ARGS. Also push a template
18833 instantiation level, which instantiate_template doesn't do because
18834 functions and variables have sufficient context established by the
18835 callers. */
18836
18837 static tree
18838 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18839 {
18840 struct pending_template *old_last_pend = last_pending_template;
18841 struct tinst_level *old_error_tinst = last_error_tinst_level;
18842 if (tmpl == error_mark_node || args == error_mark_node)
18843 return error_mark_node;
18844 tree tinst = build_tree_list (tmpl, args);
18845 if (!push_tinst_level (tinst))
18846 {
18847 ggc_free (tinst);
18848 return error_mark_node;
18849 }
18850
18851 args =
18852 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18853 args, tmpl, complain,
18854 /*require_all_args=*/true,
18855 /*use_default_args=*/true);
18856
18857 tree r = instantiate_template (tmpl, args, complain);
18858 pop_tinst_level ();
18859 /* We can't free this if a pending_template entry or last_error_tinst_level
18860 is pointing at it. */
18861 if (last_pending_template == old_last_pend
18862 && last_error_tinst_level == old_error_tinst)
18863 ggc_free (tinst);
18864
18865 return r;
18866 }
18867
18868 /* PARM is a template parameter pack for FN. Returns true iff
18869 PARM is used in a deducible way in the argument list of FN. */
18870
18871 static bool
18872 pack_deducible_p (tree parm, tree fn)
18873 {
18874 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18875 for (; t; t = TREE_CHAIN (t))
18876 {
18877 tree type = TREE_VALUE (t);
18878 tree packs;
18879 if (!PACK_EXPANSION_P (type))
18880 continue;
18881 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18882 packs; packs = TREE_CHAIN (packs))
18883 if (template_args_equal (TREE_VALUE (packs), parm))
18884 {
18885 /* The template parameter pack is used in a function parameter
18886 pack. If this is the end of the parameter list, the
18887 template parameter pack is deducible. */
18888 if (TREE_CHAIN (t) == void_list_node)
18889 return true;
18890 else
18891 /* Otherwise, not. Well, it could be deduced from
18892 a non-pack parameter, but doing so would end up with
18893 a deduction mismatch, so don't bother. */
18894 return false;
18895 }
18896 }
18897 /* The template parameter pack isn't used in any function parameter
18898 packs, but it might be used deeper, e.g. tuple<Args...>. */
18899 return true;
18900 }
18901
18902 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18903 NARGS elements of the arguments that are being used when calling
18904 it. TARGS is a vector into which the deduced template arguments
18905 are placed.
18906
18907 Returns either a FUNCTION_DECL for the matching specialization of FN or
18908 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18909 true, diagnostics will be printed to explain why it failed.
18910
18911 If FN is a conversion operator, or we are trying to produce a specific
18912 specialization, RETURN_TYPE is the return type desired.
18913
18914 The EXPLICIT_TARGS are explicit template arguments provided via a
18915 template-id.
18916
18917 The parameter STRICT is one of:
18918
18919 DEDUCE_CALL:
18920 We are deducing arguments for a function call, as in
18921 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18922 deducing arguments for a call to the result of a conversion
18923 function template, as in [over.call.object].
18924
18925 DEDUCE_CONV:
18926 We are deducing arguments for a conversion function, as in
18927 [temp.deduct.conv].
18928
18929 DEDUCE_EXACT:
18930 We are deducing arguments when doing an explicit instantiation
18931 as in [temp.explicit], when determining an explicit specialization
18932 as in [temp.expl.spec], or when taking the address of a function
18933 template, as in [temp.deduct.funcaddr]. */
18934
18935 tree
18936 fn_type_unification (tree fn,
18937 tree explicit_targs,
18938 tree targs,
18939 const tree *args,
18940 unsigned int nargs,
18941 tree return_type,
18942 unification_kind_t strict,
18943 int flags,
18944 bool explain_p,
18945 bool decltype_p)
18946 {
18947 tree parms;
18948 tree fntype;
18949 tree decl = NULL_TREE;
18950 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18951 bool ok;
18952 static int deduction_depth;
18953 struct pending_template *old_last_pend = last_pending_template;
18954 struct tinst_level *old_error_tinst = last_error_tinst_level;
18955
18956 tree orig_fn = fn;
18957 if (flag_new_inheriting_ctors)
18958 fn = strip_inheriting_ctors (fn);
18959
18960 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18961 tree tinst;
18962 tree r = error_mark_node;
18963
18964 tree full_targs = targs;
18965 if (TMPL_ARGS_DEPTH (targs)
18966 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18967 full_targs = (add_outermost_template_args
18968 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18969 targs));
18970
18971 if (decltype_p)
18972 complain |= tf_decltype;
18973
18974 /* In C++0x, it's possible to have a function template whose type depends
18975 on itself recursively. This is most obvious with decltype, but can also
18976 occur with enumeration scope (c++/48969). So we need to catch infinite
18977 recursion and reject the substitution at deduction time; this function
18978 will return error_mark_node for any repeated substitution.
18979
18980 This also catches excessive recursion such as when f<N> depends on
18981 f<N-1> across all integers, and returns error_mark_node for all the
18982 substitutions back up to the initial one.
18983
18984 This is, of course, not reentrant. */
18985 if (excessive_deduction_depth)
18986 return error_mark_node;
18987 tinst = build_tree_list (fn, NULL_TREE);
18988 ++deduction_depth;
18989
18990 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18991
18992 fntype = TREE_TYPE (fn);
18993 if (explicit_targs)
18994 {
18995 /* [temp.deduct]
18996
18997 The specified template arguments must match the template
18998 parameters in kind (i.e., type, nontype, template), and there
18999 must not be more arguments than there are parameters;
19000 otherwise type deduction fails.
19001
19002 Nontype arguments must match the types of the corresponding
19003 nontype template parameters, or must be convertible to the
19004 types of the corresponding nontype parameters as specified in
19005 _temp.arg.nontype_, otherwise type deduction fails.
19006
19007 All references in the function type of the function template
19008 to the corresponding template parameters are replaced by the
19009 specified template argument values. If a substitution in a
19010 template parameter or in the function type of the function
19011 template results in an invalid type, type deduction fails. */
19012 int i, len = TREE_VEC_LENGTH (tparms);
19013 location_t loc = input_location;
19014 bool incomplete = false;
19015
19016 if (explicit_targs == error_mark_node)
19017 goto fail;
19018
19019 if (TMPL_ARGS_DEPTH (explicit_targs)
19020 < TMPL_ARGS_DEPTH (full_targs))
19021 explicit_targs = add_outermost_template_args (full_targs,
19022 explicit_targs);
19023
19024 /* Adjust any explicit template arguments before entering the
19025 substitution context. */
19026 explicit_targs
19027 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
19028 complain,
19029 /*require_all_args=*/false,
19030 /*use_default_args=*/false));
19031 if (explicit_targs == error_mark_node)
19032 goto fail;
19033
19034 /* Substitute the explicit args into the function type. This is
19035 necessary so that, for instance, explicitly declared function
19036 arguments can match null pointed constants. If we were given
19037 an incomplete set of explicit args, we must not do semantic
19038 processing during substitution as we could create partial
19039 instantiations. */
19040 for (i = 0; i < len; i++)
19041 {
19042 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
19043 bool parameter_pack = false;
19044 tree targ = TREE_VEC_ELT (explicit_targs, i);
19045
19046 /* Dig out the actual parm. */
19047 if (TREE_CODE (parm) == TYPE_DECL
19048 || TREE_CODE (parm) == TEMPLATE_DECL)
19049 {
19050 parm = TREE_TYPE (parm);
19051 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
19052 }
19053 else if (TREE_CODE (parm) == PARM_DECL)
19054 {
19055 parm = DECL_INITIAL (parm);
19056 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
19057 }
19058
19059 if (!parameter_pack && targ == NULL_TREE)
19060 /* No explicit argument for this template parameter. */
19061 incomplete = true;
19062
19063 if (parameter_pack && pack_deducible_p (parm, fn))
19064 {
19065 /* Mark the argument pack as "incomplete". We could
19066 still deduce more arguments during unification.
19067 We remove this mark in type_unification_real. */
19068 if (targ)
19069 {
19070 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
19071 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
19072 = ARGUMENT_PACK_ARGS (targ);
19073 }
19074
19075 /* We have some incomplete argument packs. */
19076 incomplete = true;
19077 }
19078 }
19079
19080 TREE_VALUE (tinst) = explicit_targs;
19081 if (!push_tinst_level (tinst))
19082 {
19083 excessive_deduction_depth = true;
19084 goto fail;
19085 }
19086 processing_template_decl += incomplete;
19087 input_location = DECL_SOURCE_LOCATION (fn);
19088 /* Ignore any access checks; we'll see them again in
19089 instantiate_template and they might have the wrong
19090 access path at this point. */
19091 push_deferring_access_checks (dk_deferred);
19092 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
19093 complain | tf_partial | tf_fndecl_type, NULL_TREE);
19094 pop_deferring_access_checks ();
19095 input_location = loc;
19096 processing_template_decl -= incomplete;
19097 pop_tinst_level ();
19098
19099 if (fntype == error_mark_node)
19100 goto fail;
19101
19102 /* Place the explicitly specified arguments in TARGS. */
19103 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
19104 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
19105 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
19106 }
19107
19108 /* Never do unification on the 'this' parameter. */
19109 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
19110
19111 if (return_type && strict == DEDUCE_CALL)
19112 {
19113 /* We're deducing for a call to the result of a template conversion
19114 function. The parms we really want are in return_type. */
19115 if (POINTER_TYPE_P (return_type))
19116 return_type = TREE_TYPE (return_type);
19117 parms = TYPE_ARG_TYPES (return_type);
19118 }
19119 else if (return_type)
19120 {
19121 tree *new_args;
19122
19123 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
19124 new_args = XALLOCAVEC (tree, nargs + 1);
19125 new_args[0] = return_type;
19126 memcpy (new_args + 1, args, nargs * sizeof (tree));
19127 args = new_args;
19128 ++nargs;
19129 }
19130
19131 /* We allow incomplete unification without an error message here
19132 because the standard doesn't seem to explicitly prohibit it. Our
19133 callers must be ready to deal with unification failures in any
19134 event. */
19135
19136 TREE_VALUE (tinst) = targs;
19137 /* If we aren't explaining yet, push tinst context so we can see where
19138 any errors (e.g. from class instantiations triggered by instantiation
19139 of default template arguments) come from. If we are explaining, this
19140 context is redundant. */
19141 if (!explain_p && !push_tinst_level (tinst))
19142 {
19143 excessive_deduction_depth = true;
19144 goto fail;
19145 }
19146
19147 /* type_unification_real will pass back any access checks from default
19148 template argument substitution. */
19149 vec<deferred_access_check, va_gc> *checks;
19150 checks = NULL;
19151
19152 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19153 full_targs, parms, args, nargs, /*subr=*/0,
19154 strict, flags, &checks, explain_p);
19155 if (!explain_p)
19156 pop_tinst_level ();
19157 if (!ok)
19158 goto fail;
19159
19160 /* Now that we have bindings for all of the template arguments,
19161 ensure that the arguments deduced for the template template
19162 parameters have compatible template parameter lists. We cannot
19163 check this property before we have deduced all template
19164 arguments, because the template parameter types of a template
19165 template parameter might depend on prior template parameters
19166 deduced after the template template parameter. The following
19167 ill-formed example illustrates this issue:
19168
19169 template<typename T, template<T> class C> void f(C<5>, T);
19170
19171 template<int N> struct X {};
19172
19173 void g() {
19174 f(X<5>(), 5l); // error: template argument deduction fails
19175 }
19176
19177 The template parameter list of 'C' depends on the template type
19178 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19179 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19180 time that we deduce 'C'. */
19181 if (!template_template_parm_bindings_ok_p
19182 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19183 {
19184 unify_inconsistent_template_template_parameters (explain_p);
19185 goto fail;
19186 }
19187
19188 /* All is well so far. Now, check:
19189
19190 [temp.deduct]
19191
19192 When all template arguments have been deduced, all uses of
19193 template parameters in nondeduced contexts are replaced with
19194 the corresponding deduced argument values. If the
19195 substitution results in an invalid type, as described above,
19196 type deduction fails. */
19197 TREE_VALUE (tinst) = targs;
19198 if (!push_tinst_level (tinst))
19199 {
19200 excessive_deduction_depth = true;
19201 goto fail;
19202 }
19203
19204 /* Also collect access checks from the instantiation. */
19205 reopen_deferring_access_checks (checks);
19206
19207 decl = instantiate_template (fn, targs, complain);
19208
19209 checks = get_deferred_access_checks ();
19210 pop_deferring_access_checks ();
19211
19212 pop_tinst_level ();
19213
19214 if (decl == error_mark_node)
19215 goto fail;
19216
19217 /* Now perform any access checks encountered during substitution. */
19218 push_access_scope (decl);
19219 ok = perform_access_checks (checks, complain);
19220 pop_access_scope (decl);
19221 if (!ok)
19222 goto fail;
19223
19224 /* If we're looking for an exact match, check that what we got
19225 is indeed an exact match. It might not be if some template
19226 parameters are used in non-deduced contexts. But don't check
19227 for an exact match if we have dependent template arguments;
19228 in that case we're doing partial ordering, and we already know
19229 that we have two candidates that will provide the actual type. */
19230 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19231 {
19232 tree substed = TREE_TYPE (decl);
19233 unsigned int i;
19234
19235 tree sarg
19236 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19237 if (return_type)
19238 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19239 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19240 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19241 {
19242 unify_type_mismatch (explain_p, args[i],
19243 TREE_VALUE (sarg));
19244 goto fail;
19245 }
19246 }
19247
19248 /* After doing deduction with the inherited constructor, actually return an
19249 instantiation of the inheriting constructor. */
19250 if (orig_fn != fn)
19251 decl = instantiate_template (orig_fn, targs, complain);
19252
19253 r = decl;
19254
19255 fail:
19256 --deduction_depth;
19257 if (excessive_deduction_depth)
19258 {
19259 if (deduction_depth == 0)
19260 /* Reset once we're all the way out. */
19261 excessive_deduction_depth = false;
19262 }
19263
19264 /* We can't free this if a pending_template entry or last_error_tinst_level
19265 is pointing at it. */
19266 if (last_pending_template == old_last_pend
19267 && last_error_tinst_level == old_error_tinst)
19268 ggc_free (tinst);
19269
19270 return r;
19271 }
19272
19273 /* Adjust types before performing type deduction, as described in
19274 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19275 sections are symmetric. PARM is the type of a function parameter
19276 or the return type of the conversion function. ARG is the type of
19277 the argument passed to the call, or the type of the value
19278 initialized with the result of the conversion function.
19279 ARG_EXPR is the original argument expression, which may be null. */
19280
19281 static int
19282 maybe_adjust_types_for_deduction (unification_kind_t strict,
19283 tree* parm,
19284 tree* arg,
19285 tree arg_expr)
19286 {
19287 int result = 0;
19288
19289 switch (strict)
19290 {
19291 case DEDUCE_CALL:
19292 break;
19293
19294 case DEDUCE_CONV:
19295 /* Swap PARM and ARG throughout the remainder of this
19296 function; the handling is precisely symmetric since PARM
19297 will initialize ARG rather than vice versa. */
19298 std::swap (parm, arg);
19299 break;
19300
19301 case DEDUCE_EXACT:
19302 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19303 too, but here handle it by stripping the reference from PARM
19304 rather than by adding it to ARG. */
19305 if (TREE_CODE (*parm) == REFERENCE_TYPE
19306 && TYPE_REF_IS_RVALUE (*parm)
19307 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19308 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19309 && TREE_CODE (*arg) == REFERENCE_TYPE
19310 && !TYPE_REF_IS_RVALUE (*arg))
19311 *parm = TREE_TYPE (*parm);
19312 /* Nothing else to do in this case. */
19313 return 0;
19314
19315 default:
19316 gcc_unreachable ();
19317 }
19318
19319 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19320 {
19321 /* [temp.deduct.call]
19322
19323 If P is not a reference type:
19324
19325 --If A is an array type, the pointer type produced by the
19326 array-to-pointer standard conversion (_conv.array_) is
19327 used in place of A for type deduction; otherwise,
19328
19329 --If A is a function type, the pointer type produced by
19330 the function-to-pointer standard conversion
19331 (_conv.func_) is used in place of A for type deduction;
19332 otherwise,
19333
19334 --If A is a cv-qualified type, the top level
19335 cv-qualifiers of A's type are ignored for type
19336 deduction. */
19337 if (TREE_CODE (*arg) == ARRAY_TYPE)
19338 *arg = build_pointer_type (TREE_TYPE (*arg));
19339 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19340 *arg = build_pointer_type (*arg);
19341 else
19342 *arg = TYPE_MAIN_VARIANT (*arg);
19343 }
19344
19345 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19346 reference to a cv-unqualified template parameter that does not represent a
19347 template parameter of a class template (during class template argument
19348 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19349 an lvalue, the type "lvalue reference to A" is used in place of A for type
19350 deduction. */
19351 if (TREE_CODE (*parm) == REFERENCE_TYPE
19352 && TYPE_REF_IS_RVALUE (*parm)
19353 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19354 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19355 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19356 && (arg_expr ? lvalue_p (arg_expr)
19357 /* try_one_overload doesn't provide an arg_expr, but
19358 functions are always lvalues. */
19359 : TREE_CODE (*arg) == FUNCTION_TYPE))
19360 *arg = build_reference_type (*arg);
19361
19362 /* [temp.deduct.call]
19363
19364 If P is a cv-qualified type, the top level cv-qualifiers
19365 of P's type are ignored for type deduction. If P is a
19366 reference type, the type referred to by P is used for
19367 type deduction. */
19368 *parm = TYPE_MAIN_VARIANT (*parm);
19369 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19370 {
19371 *parm = TREE_TYPE (*parm);
19372 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19373 }
19374
19375 /* DR 322. For conversion deduction, remove a reference type on parm
19376 too (which has been swapped into ARG). */
19377 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19378 *arg = TREE_TYPE (*arg);
19379
19380 return result;
19381 }
19382
19383 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19384 template which does contain any deducible template parameters; check if
19385 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19386 unify_one_argument. */
19387
19388 static int
19389 check_non_deducible_conversion (tree parm, tree arg, int strict,
19390 int flags, bool explain_p)
19391 {
19392 tree type;
19393
19394 if (!TYPE_P (arg))
19395 type = TREE_TYPE (arg);
19396 else
19397 type = arg;
19398
19399 if (same_type_p (parm, type))
19400 return unify_success (explain_p);
19401
19402 if (strict == DEDUCE_CONV)
19403 {
19404 if (can_convert_arg (type, parm, NULL_TREE, flags,
19405 explain_p ? tf_warning_or_error : tf_none))
19406 return unify_success (explain_p);
19407 }
19408 else if (strict != DEDUCE_EXACT)
19409 {
19410 if (can_convert_arg (parm, type,
19411 TYPE_P (arg) ? NULL_TREE : arg,
19412 flags, explain_p ? tf_warning_or_error : tf_none))
19413 return unify_success (explain_p);
19414 }
19415
19416 if (strict == DEDUCE_EXACT)
19417 return unify_type_mismatch (explain_p, parm, arg);
19418 else
19419 return unify_arg_conversion (explain_p, parm, type, arg);
19420 }
19421
19422 static bool uses_deducible_template_parms (tree type);
19423
19424 /* Returns true iff the expression EXPR is one from which a template
19425 argument can be deduced. In other words, if it's an undecorated
19426 use of a template non-type parameter. */
19427
19428 static bool
19429 deducible_expression (tree expr)
19430 {
19431 /* Strip implicit conversions. */
19432 while (CONVERT_EXPR_P (expr))
19433 expr = TREE_OPERAND (expr, 0);
19434 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19435 }
19436
19437 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19438 deducible way; that is, if it has a max value of <PARM> - 1. */
19439
19440 static bool
19441 deducible_array_bound (tree domain)
19442 {
19443 if (domain == NULL_TREE)
19444 return false;
19445
19446 tree max = TYPE_MAX_VALUE (domain);
19447 if (TREE_CODE (max) != MINUS_EXPR)
19448 return false;
19449
19450 return deducible_expression (TREE_OPERAND (max, 0));
19451 }
19452
19453 /* Returns true iff the template arguments ARGS use a template parameter
19454 in a deducible way. */
19455
19456 static bool
19457 deducible_template_args (tree args)
19458 {
19459 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19460 {
19461 bool deducible;
19462 tree elt = TREE_VEC_ELT (args, i);
19463 if (ARGUMENT_PACK_P (elt))
19464 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19465 else
19466 {
19467 if (PACK_EXPANSION_P (elt))
19468 elt = PACK_EXPANSION_PATTERN (elt);
19469 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19470 deducible = true;
19471 else if (TYPE_P (elt))
19472 deducible = uses_deducible_template_parms (elt);
19473 else
19474 deducible = deducible_expression (elt);
19475 }
19476 if (deducible)
19477 return true;
19478 }
19479 return false;
19480 }
19481
19482 /* Returns true iff TYPE contains any deducible references to template
19483 parameters, as per 14.8.2.5. */
19484
19485 static bool
19486 uses_deducible_template_parms (tree type)
19487 {
19488 if (PACK_EXPANSION_P (type))
19489 type = PACK_EXPANSION_PATTERN (type);
19490
19491 /* T
19492 cv-list T
19493 TT<T>
19494 TT<i>
19495 TT<> */
19496 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19497 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19498 return true;
19499
19500 /* T*
19501 T&
19502 T&& */
19503 if (POINTER_TYPE_P (type))
19504 return uses_deducible_template_parms (TREE_TYPE (type));
19505
19506 /* T[integer-constant ]
19507 type [i] */
19508 if (TREE_CODE (type) == ARRAY_TYPE)
19509 return (uses_deducible_template_parms (TREE_TYPE (type))
19510 || deducible_array_bound (TYPE_DOMAIN (type)));
19511
19512 /* T type ::*
19513 type T::*
19514 T T::*
19515 T (type ::*)()
19516 type (T::*)()
19517 type (type ::*)(T)
19518 type (T::*)(T)
19519 T (type ::*)(T)
19520 T (T::*)()
19521 T (T::*)(T) */
19522 if (TYPE_PTRMEM_P (type))
19523 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19524 || (uses_deducible_template_parms
19525 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19526
19527 /* template-name <T> (where template-name refers to a class template)
19528 template-name <i> (where template-name refers to a class template) */
19529 if (CLASS_TYPE_P (type)
19530 && CLASSTYPE_TEMPLATE_INFO (type)
19531 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19532 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19533 (CLASSTYPE_TI_ARGS (type)));
19534
19535 /* type (T)
19536 T()
19537 T(T) */
19538 if (TREE_CODE (type) == FUNCTION_TYPE
19539 || TREE_CODE (type) == METHOD_TYPE)
19540 {
19541 if (uses_deducible_template_parms (TREE_TYPE (type)))
19542 return true;
19543 tree parm = TYPE_ARG_TYPES (type);
19544 if (TREE_CODE (type) == METHOD_TYPE)
19545 parm = TREE_CHAIN (parm);
19546 for (; parm; parm = TREE_CHAIN (parm))
19547 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19548 return true;
19549 }
19550
19551 return false;
19552 }
19553
19554 /* Subroutine of type_unification_real and unify_pack_expansion to
19555 handle unification of a single P/A pair. Parameters are as
19556 for those functions. */
19557
19558 static int
19559 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19560 int subr, unification_kind_t strict,
19561 bool explain_p)
19562 {
19563 tree arg_expr = NULL_TREE;
19564 int arg_strict;
19565
19566 if (arg == error_mark_node || parm == error_mark_node)
19567 return unify_invalid (explain_p);
19568 if (arg == unknown_type_node)
19569 /* We can't deduce anything from this, but we might get all the
19570 template args from other function args. */
19571 return unify_success (explain_p);
19572
19573 /* Implicit conversions (Clause 4) will be performed on a function
19574 argument to convert it to the type of the corresponding function
19575 parameter if the parameter type contains no template-parameters that
19576 participate in template argument deduction. */
19577 if (strict != DEDUCE_EXACT
19578 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19579 /* For function parameters with no deducible template parameters,
19580 just return. We'll check non-dependent conversions later. */
19581 return unify_success (explain_p);
19582
19583 switch (strict)
19584 {
19585 case DEDUCE_CALL:
19586 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19587 | UNIFY_ALLOW_MORE_CV_QUAL
19588 | UNIFY_ALLOW_DERIVED);
19589 break;
19590
19591 case DEDUCE_CONV:
19592 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19593 break;
19594
19595 case DEDUCE_EXACT:
19596 arg_strict = UNIFY_ALLOW_NONE;
19597 break;
19598
19599 default:
19600 gcc_unreachable ();
19601 }
19602
19603 /* We only do these transformations if this is the top-level
19604 parameter_type_list in a call or declaration matching; in other
19605 situations (nested function declarators, template argument lists) we
19606 won't be comparing a type to an expression, and we don't do any type
19607 adjustments. */
19608 if (!subr)
19609 {
19610 if (!TYPE_P (arg))
19611 {
19612 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19613 if (type_unknown_p (arg))
19614 {
19615 /* [temp.deduct.type] A template-argument can be
19616 deduced from a pointer to function or pointer
19617 to member function argument if the set of
19618 overloaded functions does not contain function
19619 templates and at most one of a set of
19620 overloaded functions provides a unique
19621 match. */
19622 resolve_overloaded_unification (tparms, targs, parm,
19623 arg, strict,
19624 arg_strict, explain_p);
19625 /* If a unique match was not found, this is a
19626 non-deduced context, so we still succeed. */
19627 return unify_success (explain_p);
19628 }
19629
19630 arg_expr = arg;
19631 arg = unlowered_expr_type (arg);
19632 if (arg == error_mark_node)
19633 return unify_invalid (explain_p);
19634 }
19635
19636 arg_strict |=
19637 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19638 }
19639 else
19640 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19641 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19642 return unify_template_argument_mismatch (explain_p, parm, arg);
19643
19644 /* For deduction from an init-list we need the actual list. */
19645 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19646 arg = arg_expr;
19647 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19648 }
19649
19650 /* for_each_template_parm callback that always returns 0. */
19651
19652 static int
19653 zero_r (tree, void *)
19654 {
19655 return 0;
19656 }
19657
19658 /* for_each_template_parm any_fn callback to handle deduction of a template
19659 type argument from the type of an array bound. */
19660
19661 static int
19662 array_deduction_r (tree t, void *data)
19663 {
19664 tree_pair_p d = (tree_pair_p)data;
19665 tree &tparms = d->purpose;
19666 tree &targs = d->value;
19667
19668 if (TREE_CODE (t) == ARRAY_TYPE)
19669 if (tree dom = TYPE_DOMAIN (t))
19670 if (tree max = TYPE_MAX_VALUE (dom))
19671 {
19672 if (TREE_CODE (max) == MINUS_EXPR)
19673 max = TREE_OPERAND (max, 0);
19674 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19675 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19676 UNIFY_ALLOW_NONE, /*explain*/false);
19677 }
19678
19679 /* Keep walking. */
19680 return 0;
19681 }
19682
19683 /* Try to deduce any not-yet-deduced template type arguments from the type of
19684 an array bound. This is handled separately from unify because 14.8.2.5 says
19685 "The type of a type parameter is only deduced from an array bound if it is
19686 not otherwise deduced." */
19687
19688 static void
19689 try_array_deduction (tree tparms, tree targs, tree parm)
19690 {
19691 tree_pair_s data = { tparms, targs };
19692 hash_set<tree> visited;
19693 for_each_template_parm (parm, zero_r, &data, &visited,
19694 /*nondeduced*/false, array_deduction_r);
19695 }
19696
19697 /* Most parms like fn_type_unification.
19698
19699 If SUBR is 1, we're being called recursively (to unify the
19700 arguments of a function or method parameter of a function
19701 template).
19702
19703 CHECKS is a pointer to a vector of access checks encountered while
19704 substituting default template arguments. */
19705
19706 static int
19707 type_unification_real (tree tparms,
19708 tree full_targs,
19709 tree xparms,
19710 const tree *xargs,
19711 unsigned int xnargs,
19712 int subr,
19713 unification_kind_t strict,
19714 int flags,
19715 vec<deferred_access_check, va_gc> **checks,
19716 bool explain_p)
19717 {
19718 tree parm, arg;
19719 int i;
19720 int ntparms = TREE_VEC_LENGTH (tparms);
19721 int saw_undeduced = 0;
19722 tree parms;
19723 const tree *args;
19724 unsigned int nargs;
19725 unsigned int ia;
19726
19727 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19728 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19729 gcc_assert (ntparms > 0);
19730
19731 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19732
19733 /* Reset the number of non-defaulted template arguments contained
19734 in TARGS. */
19735 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19736
19737 again:
19738 parms = xparms;
19739 args = xargs;
19740 nargs = xnargs;
19741
19742 ia = 0;
19743 while (parms && parms != void_list_node
19744 && ia < nargs)
19745 {
19746 parm = TREE_VALUE (parms);
19747
19748 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19749 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19750 /* For a function parameter pack that occurs at the end of the
19751 parameter-declaration-list, the type A of each remaining
19752 argument of the call is compared with the type P of the
19753 declarator-id of the function parameter pack. */
19754 break;
19755
19756 parms = TREE_CHAIN (parms);
19757
19758 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19759 /* For a function parameter pack that does not occur at the
19760 end of the parameter-declaration-list, the type of the
19761 parameter pack is a non-deduced context. */
19762 continue;
19763
19764 arg = args[ia];
19765 ++ia;
19766
19767 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19768 explain_p))
19769 return 1;
19770 }
19771
19772 if (parms
19773 && parms != void_list_node
19774 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19775 {
19776 /* Unify the remaining arguments with the pack expansion type. */
19777 tree argvec;
19778 tree parmvec = make_tree_vec (1);
19779
19780 /* Allocate a TREE_VEC and copy in all of the arguments */
19781 argvec = make_tree_vec (nargs - ia);
19782 for (i = 0; ia < nargs; ++ia, ++i)
19783 TREE_VEC_ELT (argvec, i) = args[ia];
19784
19785 /* Copy the parameter into parmvec. */
19786 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19787 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19788 /*subr=*/subr, explain_p))
19789 return 1;
19790
19791 /* Advance to the end of the list of parameters. */
19792 parms = TREE_CHAIN (parms);
19793 }
19794
19795 /* Fail if we've reached the end of the parm list, and more args
19796 are present, and the parm list isn't variadic. */
19797 if (ia < nargs && parms == void_list_node)
19798 return unify_too_many_arguments (explain_p, nargs, ia);
19799 /* Fail if parms are left and they don't have default values and
19800 they aren't all deduced as empty packs (c++/57397). This is
19801 consistent with sufficient_parms_p. */
19802 if (parms && parms != void_list_node
19803 && TREE_PURPOSE (parms) == NULL_TREE)
19804 {
19805 unsigned int count = nargs;
19806 tree p = parms;
19807 bool type_pack_p;
19808 do
19809 {
19810 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19811 if (!type_pack_p)
19812 count++;
19813 p = TREE_CHAIN (p);
19814 }
19815 while (p && p != void_list_node);
19816 if (count != nargs)
19817 return unify_too_few_arguments (explain_p, ia, count,
19818 type_pack_p);
19819 }
19820
19821 if (!subr)
19822 {
19823 tsubst_flags_t complain = (explain_p
19824 ? tf_warning_or_error
19825 : tf_none);
19826 bool tried_array_deduction = (cxx_dialect < cxx17);
19827
19828 for (i = 0; i < ntparms; i++)
19829 {
19830 tree targ = TREE_VEC_ELT (targs, i);
19831 tree tparm = TREE_VEC_ELT (tparms, i);
19832
19833 /* Clear the "incomplete" flags on all argument packs now so that
19834 substituting them into later default arguments works. */
19835 if (targ && ARGUMENT_PACK_P (targ))
19836 {
19837 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19838 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19839 }
19840
19841 if (targ || tparm == error_mark_node)
19842 continue;
19843 tparm = TREE_VALUE (tparm);
19844
19845 if (TREE_CODE (tparm) == TYPE_DECL
19846 && !tried_array_deduction)
19847 {
19848 try_array_deduction (tparms, targs, xparms);
19849 tried_array_deduction = true;
19850 if (TREE_VEC_ELT (targs, i))
19851 continue;
19852 }
19853
19854 /* If this is an undeduced nontype parameter that depends on
19855 a type parameter, try another pass; its type may have been
19856 deduced from a later argument than the one from which
19857 this parameter can be deduced. */
19858 if (TREE_CODE (tparm) == PARM_DECL
19859 && uses_template_parms (TREE_TYPE (tparm))
19860 && saw_undeduced < 2)
19861 {
19862 saw_undeduced = 1;
19863 continue;
19864 }
19865
19866 /* Core issue #226 (C++0x) [temp.deduct]:
19867
19868 If a template argument has not been deduced, its
19869 default template argument, if any, is used.
19870
19871 When we are in C++98 mode, TREE_PURPOSE will either
19872 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19873 to explicitly check cxx_dialect here. */
19874 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19875 /* OK, there is a default argument. Wait until after the
19876 conversion check to do substitution. */
19877 continue;
19878
19879 /* If the type parameter is a parameter pack, then it will
19880 be deduced to an empty parameter pack. */
19881 if (template_parameter_pack_p (tparm))
19882 {
19883 tree arg;
19884
19885 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19886 {
19887 arg = make_node (NONTYPE_ARGUMENT_PACK);
19888 TREE_CONSTANT (arg) = 1;
19889 }
19890 else
19891 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19892
19893 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19894
19895 TREE_VEC_ELT (targs, i) = arg;
19896 continue;
19897 }
19898
19899 return unify_parameter_deduction_failure (explain_p, tparm);
19900 }
19901
19902 /* DR 1391: All parameters have args, now check non-dependent parms for
19903 convertibility. */
19904 if (saw_undeduced < 2)
19905 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19906 parms && parms != void_list_node && ia < nargs; )
19907 {
19908 parm = TREE_VALUE (parms);
19909
19910 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19911 && (!TREE_CHAIN (parms)
19912 || TREE_CHAIN (parms) == void_list_node))
19913 /* For a function parameter pack that occurs at the end of the
19914 parameter-declaration-list, the type A of each remaining
19915 argument of the call is compared with the type P of the
19916 declarator-id of the function parameter pack. */
19917 break;
19918
19919 parms = TREE_CHAIN (parms);
19920
19921 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19922 /* For a function parameter pack that does not occur at the
19923 end of the parameter-declaration-list, the type of the
19924 parameter pack is a non-deduced context. */
19925 continue;
19926
19927 arg = args[ia];
19928 ++ia;
19929
19930 if (uses_template_parms (parm))
19931 continue;
19932 if (check_non_deducible_conversion (parm, arg, strict, flags,
19933 explain_p))
19934 return 1;
19935 }
19936
19937 /* Now substitute into the default template arguments. */
19938 for (i = 0; i < ntparms; i++)
19939 {
19940 tree targ = TREE_VEC_ELT (targs, i);
19941 tree tparm = TREE_VEC_ELT (tparms, i);
19942
19943 if (targ || tparm == error_mark_node)
19944 continue;
19945 tree parm = TREE_VALUE (tparm);
19946
19947 tsubst_flags_t fcomplain = complain;
19948 if (saw_undeduced == 1)
19949 {
19950 /* When saw_undeduced == 1, substitution into parm and arg might
19951 fail or not replace all template parameters, and that's
19952 fine. */
19953 fcomplain = tf_none;
19954 if (TREE_CODE (parm) == PARM_DECL
19955 && uses_template_parms (TREE_TYPE (parm)))
19956 continue;
19957 }
19958
19959 tree arg = TREE_PURPOSE (tparm);
19960 reopen_deferring_access_checks (*checks);
19961 location_t save_loc = input_location;
19962 if (DECL_P (parm))
19963 input_location = DECL_SOURCE_LOCATION (parm);
19964 arg = tsubst_template_arg (arg, full_targs, fcomplain, NULL_TREE);
19965 if (arg != error_mark_node && !uses_template_parms (arg))
19966 arg = convert_template_argument (parm, arg, full_targs, complain,
19967 i, NULL_TREE);
19968 else if (saw_undeduced == 1)
19969 arg = NULL_TREE;
19970 else
19971 arg = error_mark_node;
19972 input_location = save_loc;
19973 *checks = get_deferred_access_checks ();
19974 pop_deferring_access_checks ();
19975 if (arg == error_mark_node)
19976 return 1;
19977 else if (arg)
19978 {
19979 TREE_VEC_ELT (targs, i) = arg;
19980 /* The position of the first default template argument,
19981 is also the number of non-defaulted arguments in TARGS.
19982 Record that. */
19983 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19984 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19985 }
19986 }
19987
19988 if (saw_undeduced++ == 1)
19989 goto again;
19990 }
19991
19992 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19993 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19994
19995 return unify_success (explain_p);
19996 }
19997
19998 /* Subroutine of type_unification_real. Args are like the variables
19999 at the call site. ARG is an overloaded function (or template-id);
20000 we try deducing template args from each of the overloads, and if
20001 only one succeeds, we go with that. Modifies TARGS and returns
20002 true on success. */
20003
20004 static bool
20005 resolve_overloaded_unification (tree tparms,
20006 tree targs,
20007 tree parm,
20008 tree arg,
20009 unification_kind_t strict,
20010 int sub_strict,
20011 bool explain_p)
20012 {
20013 tree tempargs = copy_node (targs);
20014 int good = 0;
20015 tree goodfn = NULL_TREE;
20016 bool addr_p;
20017
20018 if (TREE_CODE (arg) == ADDR_EXPR)
20019 {
20020 arg = TREE_OPERAND (arg, 0);
20021 addr_p = true;
20022 }
20023 else
20024 addr_p = false;
20025
20026 if (TREE_CODE (arg) == COMPONENT_REF)
20027 /* Handle `&x' where `x' is some static or non-static member
20028 function name. */
20029 arg = TREE_OPERAND (arg, 1);
20030
20031 if (TREE_CODE (arg) == OFFSET_REF)
20032 arg = TREE_OPERAND (arg, 1);
20033
20034 /* Strip baselink information. */
20035 if (BASELINK_P (arg))
20036 arg = BASELINK_FUNCTIONS (arg);
20037
20038 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
20039 {
20040 /* If we got some explicit template args, we need to plug them into
20041 the affected templates before we try to unify, in case the
20042 explicit args will completely resolve the templates in question. */
20043
20044 int ok = 0;
20045 tree expl_subargs = TREE_OPERAND (arg, 1);
20046 arg = TREE_OPERAND (arg, 0);
20047
20048 for (lkp_iterator iter (arg); iter; ++iter)
20049 {
20050 tree fn = *iter;
20051 tree subargs, elem;
20052
20053 if (TREE_CODE (fn) != TEMPLATE_DECL)
20054 continue;
20055
20056 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20057 expl_subargs, NULL_TREE, tf_none,
20058 /*require_all_args=*/true,
20059 /*use_default_args=*/true);
20060 if (subargs != error_mark_node
20061 && !any_dependent_template_arguments_p (subargs))
20062 {
20063 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
20064 if (try_one_overload (tparms, targs, tempargs, parm,
20065 elem, strict, sub_strict, addr_p, explain_p)
20066 && (!goodfn || !same_type_p (goodfn, elem)))
20067 {
20068 goodfn = elem;
20069 ++good;
20070 }
20071 }
20072 else if (subargs)
20073 ++ok;
20074 }
20075 /* If no templates (or more than one) are fully resolved by the
20076 explicit arguments, this template-id is a non-deduced context; it
20077 could still be OK if we deduce all template arguments for the
20078 enclosing call through other arguments. */
20079 if (good != 1)
20080 good = ok;
20081 }
20082 else if (TREE_CODE (arg) != OVERLOAD
20083 && TREE_CODE (arg) != FUNCTION_DECL)
20084 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20085 -- but the deduction does not succeed because the expression is
20086 not just the function on its own. */
20087 return false;
20088 else
20089 for (lkp_iterator iter (arg); iter; ++iter)
20090 {
20091 tree fn = *iter;
20092 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
20093 strict, sub_strict, addr_p, explain_p)
20094 && (!goodfn || !decls_match (goodfn, fn)))
20095 {
20096 goodfn = fn;
20097 ++good;
20098 }
20099 }
20100
20101 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20102 to function or pointer to member function argument if the set of
20103 overloaded functions does not contain function templates and at most
20104 one of a set of overloaded functions provides a unique match.
20105
20106 So if we found multiple possibilities, we return success but don't
20107 deduce anything. */
20108
20109 if (good == 1)
20110 {
20111 int i = TREE_VEC_LENGTH (targs);
20112 for (; i--; )
20113 if (TREE_VEC_ELT (tempargs, i))
20114 {
20115 tree old = TREE_VEC_ELT (targs, i);
20116 tree new_ = TREE_VEC_ELT (tempargs, i);
20117 if (new_ && old && ARGUMENT_PACK_P (old)
20118 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
20119 /* Don't forget explicit template arguments in a pack. */
20120 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
20121 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
20122 TREE_VEC_ELT (targs, i) = new_;
20123 }
20124 }
20125 if (good)
20126 return true;
20127
20128 return false;
20129 }
20130
20131 /* Core DR 115: In contexts where deduction is done and fails, or in
20132 contexts where deduction is not done, if a template argument list is
20133 specified and it, along with any default template arguments, identifies
20134 a single function template specialization, then the template-id is an
20135 lvalue for the function template specialization. */
20136
20137 tree
20138 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
20139 {
20140 tree expr, offset, baselink;
20141 bool addr;
20142
20143 if (!type_unknown_p (orig_expr))
20144 return orig_expr;
20145
20146 expr = orig_expr;
20147 addr = false;
20148 offset = NULL_TREE;
20149 baselink = NULL_TREE;
20150
20151 if (TREE_CODE (expr) == ADDR_EXPR)
20152 {
20153 expr = TREE_OPERAND (expr, 0);
20154 addr = true;
20155 }
20156 if (TREE_CODE (expr) == OFFSET_REF)
20157 {
20158 offset = expr;
20159 expr = TREE_OPERAND (expr, 1);
20160 }
20161 if (BASELINK_P (expr))
20162 {
20163 baselink = expr;
20164 expr = BASELINK_FUNCTIONS (expr);
20165 }
20166
20167 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
20168 {
20169 int good = 0;
20170 tree goodfn = NULL_TREE;
20171
20172 /* If we got some explicit template args, we need to plug them into
20173 the affected templates before we try to unify, in case the
20174 explicit args will completely resolve the templates in question. */
20175
20176 tree expl_subargs = TREE_OPERAND (expr, 1);
20177 tree arg = TREE_OPERAND (expr, 0);
20178 tree badfn = NULL_TREE;
20179 tree badargs = NULL_TREE;
20180
20181 for (lkp_iterator iter (arg); iter; ++iter)
20182 {
20183 tree fn = *iter;
20184 tree subargs, elem;
20185
20186 if (TREE_CODE (fn) != TEMPLATE_DECL)
20187 continue;
20188
20189 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20190 expl_subargs, NULL_TREE, tf_none,
20191 /*require_all_args=*/true,
20192 /*use_default_args=*/true);
20193 if (subargs != error_mark_node
20194 && !any_dependent_template_arguments_p (subargs))
20195 {
20196 elem = instantiate_template (fn, subargs, tf_none);
20197 if (elem == error_mark_node)
20198 {
20199 badfn = fn;
20200 badargs = subargs;
20201 }
20202 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20203 {
20204 goodfn = elem;
20205 ++good;
20206 }
20207 }
20208 }
20209 if (good == 1)
20210 {
20211 mark_used (goodfn);
20212 expr = goodfn;
20213 if (baselink)
20214 expr = build_baselink (BASELINK_BINFO (baselink),
20215 BASELINK_ACCESS_BINFO (baselink),
20216 expr, BASELINK_OPTYPE (baselink));
20217 if (offset)
20218 {
20219 tree base
20220 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20221 expr = build_offset_ref (base, expr, addr, complain);
20222 }
20223 if (addr)
20224 expr = cp_build_addr_expr (expr, complain);
20225 return expr;
20226 }
20227 else if (good == 0 && badargs && (complain & tf_error))
20228 /* There were no good options and at least one bad one, so let the
20229 user know what the problem is. */
20230 instantiate_template (badfn, badargs, complain);
20231 }
20232 return orig_expr;
20233 }
20234
20235 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20236 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20237 different overloads deduce different arguments for a given parm.
20238 ADDR_P is true if the expression for which deduction is being
20239 performed was of the form "& fn" rather than simply "fn".
20240
20241 Returns 1 on success. */
20242
20243 static int
20244 try_one_overload (tree tparms,
20245 tree orig_targs,
20246 tree targs,
20247 tree parm,
20248 tree arg,
20249 unification_kind_t strict,
20250 int sub_strict,
20251 bool addr_p,
20252 bool explain_p)
20253 {
20254 int nargs;
20255 tree tempargs;
20256 int i;
20257
20258 if (arg == error_mark_node)
20259 return 0;
20260
20261 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20262 to function or pointer to member function argument if the set of
20263 overloaded functions does not contain function templates and at most
20264 one of a set of overloaded functions provides a unique match.
20265
20266 So if this is a template, just return success. */
20267
20268 if (uses_template_parms (arg))
20269 return 1;
20270
20271 if (TREE_CODE (arg) == METHOD_TYPE)
20272 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20273 else if (addr_p)
20274 arg = build_pointer_type (arg);
20275
20276 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20277
20278 /* We don't copy orig_targs for this because if we have already deduced
20279 some template args from previous args, unify would complain when we
20280 try to deduce a template parameter for the same argument, even though
20281 there isn't really a conflict. */
20282 nargs = TREE_VEC_LENGTH (targs);
20283 tempargs = make_tree_vec (nargs);
20284
20285 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20286 return 0;
20287
20288 /* First make sure we didn't deduce anything that conflicts with
20289 explicitly specified args. */
20290 for (i = nargs; i--; )
20291 {
20292 tree elt = TREE_VEC_ELT (tempargs, i);
20293 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20294
20295 if (!elt)
20296 /*NOP*/;
20297 else if (uses_template_parms (elt))
20298 /* Since we're unifying against ourselves, we will fill in
20299 template args used in the function parm list with our own
20300 template parms. Discard them. */
20301 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20302 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20303 {
20304 /* Check that the argument at each index of the deduced argument pack
20305 is equivalent to the corresponding explicitly specified argument.
20306 We may have deduced more arguments than were explicitly specified,
20307 and that's OK. */
20308
20309 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20310 that's wrong if we deduce the same argument pack from multiple
20311 function arguments: it's only incomplete the first time. */
20312
20313 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20314 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20315
20316 if (TREE_VEC_LENGTH (deduced_pack)
20317 < TREE_VEC_LENGTH (explicit_pack))
20318 return 0;
20319
20320 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20321 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20322 TREE_VEC_ELT (deduced_pack, j)))
20323 return 0;
20324 }
20325 else if (oldelt && !template_args_equal (oldelt, elt))
20326 return 0;
20327 }
20328
20329 for (i = nargs; i--; )
20330 {
20331 tree elt = TREE_VEC_ELT (tempargs, i);
20332
20333 if (elt)
20334 TREE_VEC_ELT (targs, i) = elt;
20335 }
20336
20337 return 1;
20338 }
20339
20340 /* PARM is a template class (perhaps with unbound template
20341 parameters). ARG is a fully instantiated type. If ARG can be
20342 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20343 TARGS are as for unify. */
20344
20345 static tree
20346 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20347 bool explain_p)
20348 {
20349 tree copy_of_targs;
20350
20351 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20352 return NULL_TREE;
20353 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20354 /* Matches anything. */;
20355 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20356 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20357 return NULL_TREE;
20358
20359 /* We need to make a new template argument vector for the call to
20360 unify. If we used TARGS, we'd clutter it up with the result of
20361 the attempted unification, even if this class didn't work out.
20362 We also don't want to commit ourselves to all the unifications
20363 we've already done, since unification is supposed to be done on
20364 an argument-by-argument basis. In other words, consider the
20365 following pathological case:
20366
20367 template <int I, int J, int K>
20368 struct S {};
20369
20370 template <int I, int J>
20371 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20372
20373 template <int I, int J, int K>
20374 void f(S<I, J, K>, S<I, I, I>);
20375
20376 void g() {
20377 S<0, 0, 0> s0;
20378 S<0, 1, 2> s2;
20379
20380 f(s0, s2);
20381 }
20382
20383 Now, by the time we consider the unification involving `s2', we
20384 already know that we must have `f<0, 0, 0>'. But, even though
20385 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20386 because there are two ways to unify base classes of S<0, 1, 2>
20387 with S<I, I, I>. If we kept the already deduced knowledge, we
20388 would reject the possibility I=1. */
20389 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20390
20391 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20392 {
20393 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20394 return NULL_TREE;
20395 return arg;
20396 }
20397
20398 /* If unification failed, we're done. */
20399 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20400 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20401 return NULL_TREE;
20402
20403 return arg;
20404 }
20405
20406 /* Given a template type PARM and a class type ARG, find the unique
20407 base type in ARG that is an instance of PARM. We do not examine
20408 ARG itself; only its base-classes. If there is not exactly one
20409 appropriate base class, return NULL_TREE. PARM may be the type of
20410 a partial specialization, as well as a plain template type. Used
20411 by unify. */
20412
20413 static enum template_base_result
20414 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20415 bool explain_p, tree *result)
20416 {
20417 tree rval = NULL_TREE;
20418 tree binfo;
20419
20420 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20421
20422 binfo = TYPE_BINFO (complete_type (arg));
20423 if (!binfo)
20424 {
20425 /* The type could not be completed. */
20426 *result = NULL_TREE;
20427 return tbr_incomplete_type;
20428 }
20429
20430 /* Walk in inheritance graph order. The search order is not
20431 important, and this avoids multiple walks of virtual bases. */
20432 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20433 {
20434 tree r = try_class_unification (tparms, targs, parm,
20435 BINFO_TYPE (binfo), explain_p);
20436
20437 if (r)
20438 {
20439 /* If there is more than one satisfactory baseclass, then:
20440
20441 [temp.deduct.call]
20442
20443 If they yield more than one possible deduced A, the type
20444 deduction fails.
20445
20446 applies. */
20447 if (rval && !same_type_p (r, rval))
20448 {
20449 *result = NULL_TREE;
20450 return tbr_ambiguous_baseclass;
20451 }
20452
20453 rval = r;
20454 }
20455 }
20456
20457 *result = rval;
20458 return tbr_success;
20459 }
20460
20461 /* Returns the level of DECL, which declares a template parameter. */
20462
20463 static int
20464 template_decl_level (tree decl)
20465 {
20466 switch (TREE_CODE (decl))
20467 {
20468 case TYPE_DECL:
20469 case TEMPLATE_DECL:
20470 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20471
20472 case PARM_DECL:
20473 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20474
20475 default:
20476 gcc_unreachable ();
20477 }
20478 return 0;
20479 }
20480
20481 /* Decide whether ARG can be unified with PARM, considering only the
20482 cv-qualifiers of each type, given STRICT as documented for unify.
20483 Returns nonzero iff the unification is OK on that basis. */
20484
20485 static int
20486 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20487 {
20488 int arg_quals = cp_type_quals (arg);
20489 int parm_quals = cp_type_quals (parm);
20490
20491 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20492 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20493 {
20494 /* Although a CVR qualifier is ignored when being applied to a
20495 substituted template parameter ([8.3.2]/1 for example), that
20496 does not allow us to unify "const T" with "int&" because both
20497 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20498 It is ok when we're allowing additional CV qualifiers
20499 at the outer level [14.8.2.1]/3,1st bullet. */
20500 if ((TREE_CODE (arg) == REFERENCE_TYPE
20501 || TREE_CODE (arg) == FUNCTION_TYPE
20502 || TREE_CODE (arg) == METHOD_TYPE)
20503 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20504 return 0;
20505
20506 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20507 && (parm_quals & TYPE_QUAL_RESTRICT))
20508 return 0;
20509 }
20510
20511 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20512 && (arg_quals & parm_quals) != parm_quals)
20513 return 0;
20514
20515 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20516 && (parm_quals & arg_quals) != arg_quals)
20517 return 0;
20518
20519 return 1;
20520 }
20521
20522 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20523 void
20524 template_parm_level_and_index (tree parm, int* level, int* index)
20525 {
20526 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20527 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20528 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20529 {
20530 *index = TEMPLATE_TYPE_IDX (parm);
20531 *level = TEMPLATE_TYPE_LEVEL (parm);
20532 }
20533 else
20534 {
20535 *index = TEMPLATE_PARM_IDX (parm);
20536 *level = TEMPLATE_PARM_LEVEL (parm);
20537 }
20538 }
20539
20540 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20541 do { \
20542 if (unify (TP, TA, P, A, S, EP)) \
20543 return 1; \
20544 } while (0)
20545
20546 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20547 expansion at the end of PACKED_PARMS. Returns 0 if the type
20548 deduction succeeds, 1 otherwise. STRICT is the same as in
20549 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20550 function call argument list. We'll need to adjust the arguments to make them
20551 types. SUBR tells us if this is from a recursive call to
20552 type_unification_real, or for comparing two template argument
20553 lists. */
20554
20555 static int
20556 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20557 tree packed_args, unification_kind_t strict,
20558 bool subr, bool explain_p)
20559 {
20560 tree parm
20561 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20562 tree pattern = PACK_EXPANSION_PATTERN (parm);
20563 tree pack, packs = NULL_TREE;
20564 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20565
20566 /* Add in any args remembered from an earlier partial instantiation. */
20567 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20568 int levels = TMPL_ARGS_DEPTH (targs);
20569
20570 packed_args = expand_template_argument_pack (packed_args);
20571
20572 int len = TREE_VEC_LENGTH (packed_args);
20573
20574 /* Determine the parameter packs we will be deducing from the
20575 pattern, and record their current deductions. */
20576 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20577 pack; pack = TREE_CHAIN (pack))
20578 {
20579 tree parm_pack = TREE_VALUE (pack);
20580 int idx, level;
20581
20582 /* Determine the index and level of this parameter pack. */
20583 template_parm_level_and_index (parm_pack, &level, &idx);
20584 if (level < levels)
20585 continue;
20586
20587 /* Keep track of the parameter packs and their corresponding
20588 argument packs. */
20589 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20590 TREE_TYPE (packs) = make_tree_vec (len - start);
20591 }
20592
20593 /* Loop through all of the arguments that have not yet been
20594 unified and unify each with the pattern. */
20595 for (i = start; i < len; i++)
20596 {
20597 tree parm;
20598 bool any_explicit = false;
20599 tree arg = TREE_VEC_ELT (packed_args, i);
20600
20601 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20602 or the element of its argument pack at the current index if
20603 this argument was explicitly specified. */
20604 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20605 {
20606 int idx, level;
20607 tree arg, pargs;
20608 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20609
20610 arg = NULL_TREE;
20611 if (TREE_VALUE (pack)
20612 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20613 && (i - start < TREE_VEC_LENGTH (pargs)))
20614 {
20615 any_explicit = true;
20616 arg = TREE_VEC_ELT (pargs, i - start);
20617 }
20618 TMPL_ARG (targs, level, idx) = arg;
20619 }
20620
20621 /* If we had explicit template arguments, substitute them into the
20622 pattern before deduction. */
20623 if (any_explicit)
20624 {
20625 /* Some arguments might still be unspecified or dependent. */
20626 bool dependent;
20627 ++processing_template_decl;
20628 dependent = any_dependent_template_arguments_p (targs);
20629 if (!dependent)
20630 --processing_template_decl;
20631 parm = tsubst (pattern, targs,
20632 explain_p ? tf_warning_or_error : tf_none,
20633 NULL_TREE);
20634 if (dependent)
20635 --processing_template_decl;
20636 if (parm == error_mark_node)
20637 return 1;
20638 }
20639 else
20640 parm = pattern;
20641
20642 /* Unify the pattern with the current argument. */
20643 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20644 explain_p))
20645 return 1;
20646
20647 /* For each parameter pack, collect the deduced value. */
20648 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20649 {
20650 int idx, level;
20651 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20652
20653 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20654 TMPL_ARG (targs, level, idx);
20655 }
20656 }
20657
20658 /* Verify that the results of unification with the parameter packs
20659 produce results consistent with what we've seen before, and make
20660 the deduced argument packs available. */
20661 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20662 {
20663 tree old_pack = TREE_VALUE (pack);
20664 tree new_args = TREE_TYPE (pack);
20665 int i, len = TREE_VEC_LENGTH (new_args);
20666 int idx, level;
20667 bool nondeduced_p = false;
20668
20669 /* By default keep the original deduced argument pack.
20670 If necessary, more specific code is going to update the
20671 resulting deduced argument later down in this function. */
20672 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20673 TMPL_ARG (targs, level, idx) = old_pack;
20674
20675 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20676 actually deduce anything. */
20677 for (i = 0; i < len && !nondeduced_p; ++i)
20678 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20679 nondeduced_p = true;
20680 if (nondeduced_p)
20681 continue;
20682
20683 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20684 {
20685 /* If we had fewer function args than explicit template args,
20686 just use the explicits. */
20687 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20688 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20689 if (len < explicit_len)
20690 new_args = explicit_args;
20691 }
20692
20693 if (!old_pack)
20694 {
20695 tree result;
20696 /* Build the deduced *_ARGUMENT_PACK. */
20697 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20698 {
20699 result = make_node (NONTYPE_ARGUMENT_PACK);
20700 TREE_CONSTANT (result) = 1;
20701 }
20702 else
20703 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20704
20705 SET_ARGUMENT_PACK_ARGS (result, new_args);
20706
20707 /* Note the deduced argument packs for this parameter
20708 pack. */
20709 TMPL_ARG (targs, level, idx) = result;
20710 }
20711 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20712 && (ARGUMENT_PACK_ARGS (old_pack)
20713 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20714 {
20715 /* We only had the explicitly-provided arguments before, but
20716 now we have a complete set of arguments. */
20717 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20718
20719 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20720 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20721 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20722 }
20723 else
20724 {
20725 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20726 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20727
20728 if (!comp_template_args (old_args, new_args,
20729 &bad_old_arg, &bad_new_arg))
20730 /* Inconsistent unification of this parameter pack. */
20731 return unify_parameter_pack_inconsistent (explain_p,
20732 bad_old_arg,
20733 bad_new_arg);
20734 }
20735 }
20736
20737 return unify_success (explain_p);
20738 }
20739
20740 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20741 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20742 parameters and return value are as for unify. */
20743
20744 static int
20745 unify_array_domain (tree tparms, tree targs,
20746 tree parm_dom, tree arg_dom,
20747 bool explain_p)
20748 {
20749 tree parm_max;
20750 tree arg_max;
20751 bool parm_cst;
20752 bool arg_cst;
20753
20754 /* Our representation of array types uses "N - 1" as the
20755 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20756 not an integer constant. We cannot unify arbitrarily
20757 complex expressions, so we eliminate the MINUS_EXPRs
20758 here. */
20759 parm_max = TYPE_MAX_VALUE (parm_dom);
20760 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20761 if (!parm_cst)
20762 {
20763 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20764 parm_max = TREE_OPERAND (parm_max, 0);
20765 }
20766 arg_max = TYPE_MAX_VALUE (arg_dom);
20767 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20768 if (!arg_cst)
20769 {
20770 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20771 trying to unify the type of a variable with the type
20772 of a template parameter. For example:
20773
20774 template <unsigned int N>
20775 void f (char (&) [N]);
20776 int g();
20777 void h(int i) {
20778 char a[g(i)];
20779 f(a);
20780 }
20781
20782 Here, the type of the ARG will be "int [g(i)]", and
20783 may be a SAVE_EXPR, etc. */
20784 if (TREE_CODE (arg_max) != MINUS_EXPR)
20785 return unify_vla_arg (explain_p, arg_dom);
20786 arg_max = TREE_OPERAND (arg_max, 0);
20787 }
20788
20789 /* If only one of the bounds used a MINUS_EXPR, compensate
20790 by adding one to the other bound. */
20791 if (parm_cst && !arg_cst)
20792 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20793 integer_type_node,
20794 parm_max,
20795 integer_one_node);
20796 else if (arg_cst && !parm_cst)
20797 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20798 integer_type_node,
20799 arg_max,
20800 integer_one_node);
20801
20802 return unify (tparms, targs, parm_max, arg_max,
20803 UNIFY_ALLOW_INTEGER, explain_p);
20804 }
20805
20806 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20807
20808 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20809
20810 static pa_kind_t
20811 pa_kind (tree t)
20812 {
20813 if (PACK_EXPANSION_P (t))
20814 t = PACK_EXPANSION_PATTERN (t);
20815 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20816 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20817 || DECL_TYPE_TEMPLATE_P (t))
20818 return pa_tmpl;
20819 else if (TYPE_P (t))
20820 return pa_type;
20821 else
20822 return pa_expr;
20823 }
20824
20825 /* Deduce the value of template parameters. TPARMS is the (innermost)
20826 set of template parameters to a template. TARGS is the bindings
20827 for those template parameters, as determined thus far; TARGS may
20828 include template arguments for outer levels of template parameters
20829 as well. PARM is a parameter to a template function, or a
20830 subcomponent of that parameter; ARG is the corresponding argument.
20831 This function attempts to match PARM with ARG in a manner
20832 consistent with the existing assignments in TARGS. If more values
20833 are deduced, then TARGS is updated.
20834
20835 Returns 0 if the type deduction succeeds, 1 otherwise. The
20836 parameter STRICT is a bitwise or of the following flags:
20837
20838 UNIFY_ALLOW_NONE:
20839 Require an exact match between PARM and ARG.
20840 UNIFY_ALLOW_MORE_CV_QUAL:
20841 Allow the deduced ARG to be more cv-qualified (by qualification
20842 conversion) than ARG.
20843 UNIFY_ALLOW_LESS_CV_QUAL:
20844 Allow the deduced ARG to be less cv-qualified than ARG.
20845 UNIFY_ALLOW_DERIVED:
20846 Allow the deduced ARG to be a template base class of ARG,
20847 or a pointer to a template base class of the type pointed to by
20848 ARG.
20849 UNIFY_ALLOW_INTEGER:
20850 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20851 case for more information.
20852 UNIFY_ALLOW_OUTER_LEVEL:
20853 This is the outermost level of a deduction. Used to determine validity
20854 of qualification conversions. A valid qualification conversion must
20855 have const qualified pointers leading up to the inner type which
20856 requires additional CV quals, except at the outer level, where const
20857 is not required [conv.qual]. It would be normal to set this flag in
20858 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20859 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20860 This is the outermost level of a deduction, and PARM can be more CV
20861 qualified at this point.
20862 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20863 This is the outermost level of a deduction, and PARM can be less CV
20864 qualified at this point. */
20865
20866 static int
20867 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20868 bool explain_p)
20869 {
20870 int idx;
20871 tree targ;
20872 tree tparm;
20873 int strict_in = strict;
20874 tsubst_flags_t complain = (explain_p
20875 ? tf_warning_or_error
20876 : tf_none);
20877
20878 /* I don't think this will do the right thing with respect to types.
20879 But the only case I've seen it in so far has been array bounds, where
20880 signedness is the only information lost, and I think that will be
20881 okay. */
20882 while (CONVERT_EXPR_P (parm))
20883 parm = TREE_OPERAND (parm, 0);
20884
20885 if (arg == error_mark_node)
20886 return unify_invalid (explain_p);
20887 if (arg == unknown_type_node
20888 || arg == init_list_type_node)
20889 /* We can't deduce anything from this, but we might get all the
20890 template args from other function args. */
20891 return unify_success (explain_p);
20892
20893 if (parm == any_targ_node || arg == any_targ_node)
20894 return unify_success (explain_p);
20895
20896 /* If PARM uses template parameters, then we can't bail out here,
20897 even if ARG == PARM, since we won't record unifications for the
20898 template parameters. We might need them if we're trying to
20899 figure out which of two things is more specialized. */
20900 if (arg == parm && !uses_template_parms (parm))
20901 return unify_success (explain_p);
20902
20903 /* Handle init lists early, so the rest of the function can assume
20904 we're dealing with a type. */
20905 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20906 {
20907 tree elt, elttype;
20908 unsigned i;
20909 tree orig_parm = parm;
20910
20911 /* Replace T with std::initializer_list<T> for deduction. */
20912 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20913 && flag_deduce_init_list)
20914 parm = listify (parm);
20915
20916 if (!is_std_init_list (parm)
20917 && TREE_CODE (parm) != ARRAY_TYPE)
20918 /* We can only deduce from an initializer list argument if the
20919 parameter is std::initializer_list or an array; otherwise this
20920 is a non-deduced context. */
20921 return unify_success (explain_p);
20922
20923 if (TREE_CODE (parm) == ARRAY_TYPE)
20924 elttype = TREE_TYPE (parm);
20925 else
20926 {
20927 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20928 /* Deduction is defined in terms of a single type, so just punt
20929 on the (bizarre) std::initializer_list<T...>. */
20930 if (PACK_EXPANSION_P (elttype))
20931 return unify_success (explain_p);
20932 }
20933
20934 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20935 {
20936 int elt_strict = strict;
20937
20938 if (elt == error_mark_node)
20939 return unify_invalid (explain_p);
20940
20941 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20942 {
20943 tree type = TREE_TYPE (elt);
20944 if (type == error_mark_node)
20945 return unify_invalid (explain_p);
20946 /* It should only be possible to get here for a call. */
20947 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20948 elt_strict |= maybe_adjust_types_for_deduction
20949 (DEDUCE_CALL, &elttype, &type, elt);
20950 elt = type;
20951 }
20952
20953 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20954 explain_p);
20955 }
20956
20957 if (TREE_CODE (parm) == ARRAY_TYPE
20958 && deducible_array_bound (TYPE_DOMAIN (parm)))
20959 {
20960 /* Also deduce from the length of the initializer list. */
20961 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20962 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20963 if (idx == error_mark_node)
20964 return unify_invalid (explain_p);
20965 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20966 idx, explain_p);
20967 }
20968
20969 /* If the std::initializer_list<T> deduction worked, replace the
20970 deduced A with std::initializer_list<A>. */
20971 if (orig_parm != parm)
20972 {
20973 idx = TEMPLATE_TYPE_IDX (orig_parm);
20974 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20975 targ = listify (targ);
20976 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20977 }
20978 return unify_success (explain_p);
20979 }
20980
20981 /* If parm and arg aren't the same kind of thing (template, type, or
20982 expression), fail early. */
20983 if (pa_kind (parm) != pa_kind (arg))
20984 return unify_invalid (explain_p);
20985
20986 /* Immediately reject some pairs that won't unify because of
20987 cv-qualification mismatches. */
20988 if (TREE_CODE (arg) == TREE_CODE (parm)
20989 && TYPE_P (arg)
20990 /* It is the elements of the array which hold the cv quals of an array
20991 type, and the elements might be template type parms. We'll check
20992 when we recurse. */
20993 && TREE_CODE (arg) != ARRAY_TYPE
20994 /* We check the cv-qualifiers when unifying with template type
20995 parameters below. We want to allow ARG `const T' to unify with
20996 PARM `T' for example, when computing which of two templates
20997 is more specialized, for example. */
20998 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20999 && !check_cv_quals_for_unify (strict_in, arg, parm))
21000 return unify_cv_qual_mismatch (explain_p, parm, arg);
21001
21002 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
21003 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
21004 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
21005 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
21006 strict &= ~UNIFY_ALLOW_DERIVED;
21007 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21008 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
21009
21010 switch (TREE_CODE (parm))
21011 {
21012 case TYPENAME_TYPE:
21013 case SCOPE_REF:
21014 case UNBOUND_CLASS_TEMPLATE:
21015 /* In a type which contains a nested-name-specifier, template
21016 argument values cannot be deduced for template parameters used
21017 within the nested-name-specifier. */
21018 return unify_success (explain_p);
21019
21020 case TEMPLATE_TYPE_PARM:
21021 case TEMPLATE_TEMPLATE_PARM:
21022 case BOUND_TEMPLATE_TEMPLATE_PARM:
21023 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21024 if (error_operand_p (tparm))
21025 return unify_invalid (explain_p);
21026
21027 if (TEMPLATE_TYPE_LEVEL (parm)
21028 != template_decl_level (tparm))
21029 /* The PARM is not one we're trying to unify. Just check
21030 to see if it matches ARG. */
21031 {
21032 if (TREE_CODE (arg) == TREE_CODE (parm)
21033 && (is_auto (parm) ? is_auto (arg)
21034 : same_type_p (parm, arg)))
21035 return unify_success (explain_p);
21036 else
21037 return unify_type_mismatch (explain_p, parm, arg);
21038 }
21039 idx = TEMPLATE_TYPE_IDX (parm);
21040 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21041 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
21042 if (error_operand_p (tparm))
21043 return unify_invalid (explain_p);
21044
21045 /* Check for mixed types and values. */
21046 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21047 && TREE_CODE (tparm) != TYPE_DECL)
21048 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21049 && TREE_CODE (tparm) != TEMPLATE_DECL))
21050 gcc_unreachable ();
21051
21052 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21053 {
21054 if ((strict_in & UNIFY_ALLOW_DERIVED)
21055 && CLASS_TYPE_P (arg))
21056 {
21057 /* First try to match ARG directly. */
21058 tree t = try_class_unification (tparms, targs, parm, arg,
21059 explain_p);
21060 if (!t)
21061 {
21062 /* Otherwise, look for a suitable base of ARG, as below. */
21063 enum template_base_result r;
21064 r = get_template_base (tparms, targs, parm, arg,
21065 explain_p, &t);
21066 if (!t)
21067 return unify_no_common_base (explain_p, r, parm, arg);
21068 arg = t;
21069 }
21070 }
21071 /* ARG must be constructed from a template class or a template
21072 template parameter. */
21073 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
21074 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21075 return unify_template_deduction_failure (explain_p, parm, arg);
21076
21077 /* Deduce arguments T, i from TT<T> or TT<i>. */
21078 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
21079 return 1;
21080
21081 arg = TYPE_TI_TEMPLATE (arg);
21082
21083 /* Fall through to deduce template name. */
21084 }
21085
21086 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21087 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21088 {
21089 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21090
21091 /* Simple cases: Value already set, does match or doesn't. */
21092 if (targ != NULL_TREE && template_args_equal (targ, arg))
21093 return unify_success (explain_p);
21094 else if (targ)
21095 return unify_inconsistency (explain_p, parm, targ, arg);
21096 }
21097 else
21098 {
21099 /* If PARM is `const T' and ARG is only `int', we don't have
21100 a match unless we are allowing additional qualification.
21101 If ARG is `const int' and PARM is just `T' that's OK;
21102 that binds `const int' to `T'. */
21103 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
21104 arg, parm))
21105 return unify_cv_qual_mismatch (explain_p, parm, arg);
21106
21107 /* Consider the case where ARG is `const volatile int' and
21108 PARM is `const T'. Then, T should be `volatile int'. */
21109 arg = cp_build_qualified_type_real
21110 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
21111 if (arg == error_mark_node)
21112 return unify_invalid (explain_p);
21113
21114 /* Simple cases: Value already set, does match or doesn't. */
21115 if (targ != NULL_TREE && same_type_p (targ, arg))
21116 return unify_success (explain_p);
21117 else if (targ)
21118 return unify_inconsistency (explain_p, parm, targ, arg);
21119
21120 /* Make sure that ARG is not a variable-sized array. (Note
21121 that were talking about variable-sized arrays (like
21122 `int[n]'), rather than arrays of unknown size (like
21123 `int[]').) We'll get very confused by such a type since
21124 the bound of the array is not constant, and therefore
21125 not mangleable. Besides, such types are not allowed in
21126 ISO C++, so we can do as we please here. We do allow
21127 them for 'auto' deduction, since that isn't ABI-exposed. */
21128 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
21129 return unify_vla_arg (explain_p, arg);
21130
21131 /* Strip typedefs as in convert_template_argument. */
21132 arg = canonicalize_type_argument (arg, tf_none);
21133 }
21134
21135 /* If ARG is a parameter pack or an expansion, we cannot unify
21136 against it unless PARM is also a parameter pack. */
21137 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21138 && !template_parameter_pack_p (parm))
21139 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21140
21141 /* If the argument deduction results is a METHOD_TYPE,
21142 then there is a problem.
21143 METHOD_TYPE doesn't map to any real C++ type the result of
21144 the deduction can not be of that type. */
21145 if (TREE_CODE (arg) == METHOD_TYPE)
21146 return unify_method_type_error (explain_p, arg);
21147
21148 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21149 return unify_success (explain_p);
21150
21151 case TEMPLATE_PARM_INDEX:
21152 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21153 if (error_operand_p (tparm))
21154 return unify_invalid (explain_p);
21155
21156 if (TEMPLATE_PARM_LEVEL (parm)
21157 != template_decl_level (tparm))
21158 {
21159 /* The PARM is not one we're trying to unify. Just check
21160 to see if it matches ARG. */
21161 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
21162 && cp_tree_equal (parm, arg));
21163 if (result)
21164 unify_expression_unequal (explain_p, parm, arg);
21165 return result;
21166 }
21167
21168 idx = TEMPLATE_PARM_IDX (parm);
21169 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21170
21171 if (targ)
21172 {
21173 if ((strict & UNIFY_ALLOW_INTEGER)
21174 && TREE_TYPE (targ) && TREE_TYPE (arg)
21175 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
21176 /* We're deducing from an array bound, the type doesn't matter. */
21177 arg = fold_convert (TREE_TYPE (targ), arg);
21178 int x = !cp_tree_equal (targ, arg);
21179 if (x)
21180 unify_inconsistency (explain_p, parm, targ, arg);
21181 return x;
21182 }
21183
21184 /* [temp.deduct.type] If, in the declaration of a function template
21185 with a non-type template-parameter, the non-type
21186 template-parameter is used in an expression in the function
21187 parameter-list and, if the corresponding template-argument is
21188 deduced, the template-argument type shall match the type of the
21189 template-parameter exactly, except that a template-argument
21190 deduced from an array bound may be of any integral type.
21191 The non-type parameter might use already deduced type parameters. */
21192 ++processing_template_decl;
21193 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
21194 --processing_template_decl;
21195 if (tree a = type_uses_auto (tparm))
21196 {
21197 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21198 if (tparm == error_mark_node)
21199 return 1;
21200 }
21201
21202 if (!TREE_TYPE (arg))
21203 /* Template-parameter dependent expression. Just accept it for now.
21204 It will later be processed in convert_template_argument. */
21205 ;
21206 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21207 non_reference (tparm)))
21208 /* OK */;
21209 else if ((strict & UNIFY_ALLOW_INTEGER)
21210 && CP_INTEGRAL_TYPE_P (tparm))
21211 /* Convert the ARG to the type of PARM; the deduced non-type
21212 template argument must exactly match the types of the
21213 corresponding parameter. */
21214 arg = fold (build_nop (tparm, arg));
21215 else if (uses_template_parms (tparm))
21216 {
21217 /* We haven't deduced the type of this parameter yet. */
21218 if (cxx_dialect >= cxx17
21219 /* We deduce from array bounds in try_array_deduction. */
21220 && !(strict & UNIFY_ALLOW_INTEGER))
21221 {
21222 /* Deduce it from the non-type argument. */
21223 tree atype = TREE_TYPE (arg);
21224 RECUR_AND_CHECK_FAILURE (tparms, targs,
21225 tparm, atype,
21226 UNIFY_ALLOW_NONE, explain_p);
21227 }
21228 else
21229 /* Try again later. */
21230 return unify_success (explain_p);
21231 }
21232 else
21233 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21234
21235 /* If ARG is a parameter pack or an expansion, we cannot unify
21236 against it unless PARM is also a parameter pack. */
21237 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21238 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21239 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21240
21241 {
21242 bool removed_attr = false;
21243 arg = strip_typedefs_expr (arg, &removed_attr);
21244 }
21245 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21246 return unify_success (explain_p);
21247
21248 case PTRMEM_CST:
21249 {
21250 /* A pointer-to-member constant can be unified only with
21251 another constant. */
21252 if (TREE_CODE (arg) != PTRMEM_CST)
21253 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21254
21255 /* Just unify the class member. It would be useless (and possibly
21256 wrong, depending on the strict flags) to unify also
21257 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21258 arg refer to the same variable, even if through different
21259 classes. For instance:
21260
21261 struct A { int x; };
21262 struct B : A { };
21263
21264 Unification of &A::x and &B::x must succeed. */
21265 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21266 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21267 }
21268
21269 case POINTER_TYPE:
21270 {
21271 if (!TYPE_PTR_P (arg))
21272 return unify_type_mismatch (explain_p, parm, arg);
21273
21274 /* [temp.deduct.call]
21275
21276 A can be another pointer or pointer to member type that can
21277 be converted to the deduced A via a qualification
21278 conversion (_conv.qual_).
21279
21280 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21281 This will allow for additional cv-qualification of the
21282 pointed-to types if appropriate. */
21283
21284 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21285 /* The derived-to-base conversion only persists through one
21286 level of pointers. */
21287 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21288
21289 return unify (tparms, targs, TREE_TYPE (parm),
21290 TREE_TYPE (arg), strict, explain_p);
21291 }
21292
21293 case REFERENCE_TYPE:
21294 if (TREE_CODE (arg) != REFERENCE_TYPE)
21295 return unify_type_mismatch (explain_p, parm, arg);
21296 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21297 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21298
21299 case ARRAY_TYPE:
21300 if (TREE_CODE (arg) != ARRAY_TYPE)
21301 return unify_type_mismatch (explain_p, parm, arg);
21302 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21303 != (TYPE_DOMAIN (arg) == NULL_TREE))
21304 return unify_type_mismatch (explain_p, parm, arg);
21305 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21306 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21307 if (TYPE_DOMAIN (parm) != NULL_TREE)
21308 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21309 TYPE_DOMAIN (arg), explain_p);
21310 return unify_success (explain_p);
21311
21312 case REAL_TYPE:
21313 case COMPLEX_TYPE:
21314 case VECTOR_TYPE:
21315 case INTEGER_TYPE:
21316 case BOOLEAN_TYPE:
21317 case ENUMERAL_TYPE:
21318 case VOID_TYPE:
21319 case NULLPTR_TYPE:
21320 if (TREE_CODE (arg) != TREE_CODE (parm))
21321 return unify_type_mismatch (explain_p, parm, arg);
21322
21323 /* We have already checked cv-qualification at the top of the
21324 function. */
21325 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21326 return unify_type_mismatch (explain_p, parm, arg);
21327
21328 /* As far as unification is concerned, this wins. Later checks
21329 will invalidate it if necessary. */
21330 return unify_success (explain_p);
21331
21332 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21333 /* Type INTEGER_CST can come from ordinary constant template args. */
21334 case INTEGER_CST:
21335 while (CONVERT_EXPR_P (arg))
21336 arg = TREE_OPERAND (arg, 0);
21337
21338 if (TREE_CODE (arg) != INTEGER_CST)
21339 return unify_template_argument_mismatch (explain_p, parm, arg);
21340 return (tree_int_cst_equal (parm, arg)
21341 ? unify_success (explain_p)
21342 : unify_template_argument_mismatch (explain_p, parm, arg));
21343
21344 case TREE_VEC:
21345 {
21346 int i, len, argslen;
21347 int parm_variadic_p = 0;
21348
21349 if (TREE_CODE (arg) != TREE_VEC)
21350 return unify_template_argument_mismatch (explain_p, parm, arg);
21351
21352 len = TREE_VEC_LENGTH (parm);
21353 argslen = TREE_VEC_LENGTH (arg);
21354
21355 /* Check for pack expansions in the parameters. */
21356 for (i = 0; i < len; ++i)
21357 {
21358 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21359 {
21360 if (i == len - 1)
21361 /* We can unify against something with a trailing
21362 parameter pack. */
21363 parm_variadic_p = 1;
21364 else
21365 /* [temp.deduct.type]/9: If the template argument list of
21366 P contains a pack expansion that is not the last
21367 template argument, the entire template argument list
21368 is a non-deduced context. */
21369 return unify_success (explain_p);
21370 }
21371 }
21372
21373 /* If we don't have enough arguments to satisfy the parameters
21374 (not counting the pack expression at the end), or we have
21375 too many arguments for a parameter list that doesn't end in
21376 a pack expression, we can't unify. */
21377 if (parm_variadic_p
21378 ? argslen < len - parm_variadic_p
21379 : argslen != len)
21380 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21381
21382 /* Unify all of the parameters that precede the (optional)
21383 pack expression. */
21384 for (i = 0; i < len - parm_variadic_p; ++i)
21385 {
21386 RECUR_AND_CHECK_FAILURE (tparms, targs,
21387 TREE_VEC_ELT (parm, i),
21388 TREE_VEC_ELT (arg, i),
21389 UNIFY_ALLOW_NONE, explain_p);
21390 }
21391 if (parm_variadic_p)
21392 return unify_pack_expansion (tparms, targs, parm, arg,
21393 DEDUCE_EXACT,
21394 /*subr=*/true, explain_p);
21395 return unify_success (explain_p);
21396 }
21397
21398 case RECORD_TYPE:
21399 case UNION_TYPE:
21400 if (TREE_CODE (arg) != TREE_CODE (parm))
21401 return unify_type_mismatch (explain_p, parm, arg);
21402
21403 if (TYPE_PTRMEMFUNC_P (parm))
21404 {
21405 if (!TYPE_PTRMEMFUNC_P (arg))
21406 return unify_type_mismatch (explain_p, parm, arg);
21407
21408 return unify (tparms, targs,
21409 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21410 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21411 strict, explain_p);
21412 }
21413 else if (TYPE_PTRMEMFUNC_P (arg))
21414 return unify_type_mismatch (explain_p, parm, arg);
21415
21416 if (CLASSTYPE_TEMPLATE_INFO (parm))
21417 {
21418 tree t = NULL_TREE;
21419
21420 if (strict_in & UNIFY_ALLOW_DERIVED)
21421 {
21422 /* First, we try to unify the PARM and ARG directly. */
21423 t = try_class_unification (tparms, targs,
21424 parm, arg, explain_p);
21425
21426 if (!t)
21427 {
21428 /* Fallback to the special case allowed in
21429 [temp.deduct.call]:
21430
21431 If P is a class, and P has the form
21432 template-id, then A can be a derived class of
21433 the deduced A. Likewise, if P is a pointer to
21434 a class of the form template-id, A can be a
21435 pointer to a derived class pointed to by the
21436 deduced A. */
21437 enum template_base_result r;
21438 r = get_template_base (tparms, targs, parm, arg,
21439 explain_p, &t);
21440
21441 if (!t)
21442 {
21443 /* Don't give the derived diagnostic if we're
21444 already dealing with the same template. */
21445 bool same_template
21446 = (CLASSTYPE_TEMPLATE_INFO (arg)
21447 && (CLASSTYPE_TI_TEMPLATE (parm)
21448 == CLASSTYPE_TI_TEMPLATE (arg)));
21449 return unify_no_common_base (explain_p && !same_template,
21450 r, parm, arg);
21451 }
21452 }
21453 }
21454 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21455 && (CLASSTYPE_TI_TEMPLATE (parm)
21456 == CLASSTYPE_TI_TEMPLATE (arg)))
21457 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21458 Then, we should unify `int' and `U'. */
21459 t = arg;
21460 else
21461 /* There's no chance of unification succeeding. */
21462 return unify_type_mismatch (explain_p, parm, arg);
21463
21464 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21465 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21466 }
21467 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21468 return unify_type_mismatch (explain_p, parm, arg);
21469 return unify_success (explain_p);
21470
21471 case METHOD_TYPE:
21472 case FUNCTION_TYPE:
21473 {
21474 unsigned int nargs;
21475 tree *args;
21476 tree a;
21477 unsigned int i;
21478
21479 if (TREE_CODE (arg) != TREE_CODE (parm))
21480 return unify_type_mismatch (explain_p, parm, arg);
21481
21482 /* CV qualifications for methods can never be deduced, they must
21483 match exactly. We need to check them explicitly here,
21484 because type_unification_real treats them as any other
21485 cv-qualified parameter. */
21486 if (TREE_CODE (parm) == METHOD_TYPE
21487 && (!check_cv_quals_for_unify
21488 (UNIFY_ALLOW_NONE,
21489 class_of_this_parm (arg),
21490 class_of_this_parm (parm))))
21491 return unify_cv_qual_mismatch (explain_p, parm, arg);
21492 if (TREE_CODE (arg) == FUNCTION_TYPE
21493 && type_memfn_quals (parm) != type_memfn_quals (arg))
21494 return unify_cv_qual_mismatch (explain_p, parm, arg);
21495 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21496 return unify_type_mismatch (explain_p, parm, arg);
21497
21498 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21499 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21500
21501 nargs = list_length (TYPE_ARG_TYPES (arg));
21502 args = XALLOCAVEC (tree, nargs);
21503 for (a = TYPE_ARG_TYPES (arg), i = 0;
21504 a != NULL_TREE && a != void_list_node;
21505 a = TREE_CHAIN (a), ++i)
21506 args[i] = TREE_VALUE (a);
21507 nargs = i;
21508
21509 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21510 args, nargs, 1, DEDUCE_EXACT,
21511 LOOKUP_NORMAL, NULL, explain_p))
21512 return 1;
21513
21514 if (flag_noexcept_type)
21515 {
21516 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21517 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21518 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21519 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21520 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21521 && uses_template_parms (TREE_PURPOSE (pspec)))
21522 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21523 TREE_PURPOSE (aspec),
21524 UNIFY_ALLOW_NONE, explain_p);
21525 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21526 return unify_type_mismatch (explain_p, parm, arg);
21527 }
21528
21529 return 0;
21530 }
21531
21532 case OFFSET_TYPE:
21533 /* Unify a pointer to member with a pointer to member function, which
21534 deduces the type of the member as a function type. */
21535 if (TYPE_PTRMEMFUNC_P (arg))
21536 {
21537 /* Check top-level cv qualifiers */
21538 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21539 return unify_cv_qual_mismatch (explain_p, parm, arg);
21540
21541 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21542 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21543 UNIFY_ALLOW_NONE, explain_p);
21544
21545 /* Determine the type of the function we are unifying against. */
21546 tree fntype = static_fn_type (arg);
21547
21548 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21549 }
21550
21551 if (TREE_CODE (arg) != OFFSET_TYPE)
21552 return unify_type_mismatch (explain_p, parm, arg);
21553 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21554 TYPE_OFFSET_BASETYPE (arg),
21555 UNIFY_ALLOW_NONE, explain_p);
21556 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21557 strict, explain_p);
21558
21559 case CONST_DECL:
21560 if (DECL_TEMPLATE_PARM_P (parm))
21561 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21562 if (arg != scalar_constant_value (parm))
21563 return unify_template_argument_mismatch (explain_p, parm, arg);
21564 return unify_success (explain_p);
21565
21566 case FIELD_DECL:
21567 case TEMPLATE_DECL:
21568 /* Matched cases are handled by the ARG == PARM test above. */
21569 return unify_template_argument_mismatch (explain_p, parm, arg);
21570
21571 case VAR_DECL:
21572 /* We might get a variable as a non-type template argument in parm if the
21573 corresponding parameter is type-dependent. Make any necessary
21574 adjustments based on whether arg is a reference. */
21575 if (CONSTANT_CLASS_P (arg))
21576 parm = fold_non_dependent_expr (parm);
21577 else if (REFERENCE_REF_P (arg))
21578 {
21579 tree sub = TREE_OPERAND (arg, 0);
21580 STRIP_NOPS (sub);
21581 if (TREE_CODE (sub) == ADDR_EXPR)
21582 arg = TREE_OPERAND (sub, 0);
21583 }
21584 /* Now use the normal expression code to check whether they match. */
21585 goto expr;
21586
21587 case TYPE_ARGUMENT_PACK:
21588 case NONTYPE_ARGUMENT_PACK:
21589 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21590 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21591
21592 case TYPEOF_TYPE:
21593 case DECLTYPE_TYPE:
21594 case UNDERLYING_TYPE:
21595 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21596 or UNDERLYING_TYPE nodes. */
21597 return unify_success (explain_p);
21598
21599 case ERROR_MARK:
21600 /* Unification fails if we hit an error node. */
21601 return unify_invalid (explain_p);
21602
21603 case INDIRECT_REF:
21604 if (REFERENCE_REF_P (parm))
21605 {
21606 bool pexp = PACK_EXPANSION_P (arg);
21607 if (pexp)
21608 arg = PACK_EXPANSION_PATTERN (arg);
21609 if (REFERENCE_REF_P (arg))
21610 arg = TREE_OPERAND (arg, 0);
21611 if (pexp)
21612 arg = make_pack_expansion (arg, complain);
21613 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21614 strict, explain_p);
21615 }
21616 /* FALLTHRU */
21617
21618 default:
21619 /* An unresolved overload is a nondeduced context. */
21620 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21621 return unify_success (explain_p);
21622 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21623 expr:
21624 /* We must be looking at an expression. This can happen with
21625 something like:
21626
21627 template <int I>
21628 void foo(S<I>, S<I + 2>);
21629
21630 This is a "nondeduced context":
21631
21632 [deduct.type]
21633
21634 The nondeduced contexts are:
21635
21636 --A type that is a template-id in which one or more of
21637 the template-arguments is an expression that references
21638 a template-parameter.
21639
21640 In these cases, we assume deduction succeeded, but don't
21641 actually infer any unifications. */
21642
21643 if (!uses_template_parms (parm)
21644 && !template_args_equal (parm, arg))
21645 return unify_expression_unequal (explain_p, parm, arg);
21646 else
21647 return unify_success (explain_p);
21648 }
21649 }
21650 #undef RECUR_AND_CHECK_FAILURE
21651 \f
21652 /* Note that DECL can be defined in this translation unit, if
21653 required. */
21654
21655 static void
21656 mark_definable (tree decl)
21657 {
21658 tree clone;
21659 DECL_NOT_REALLY_EXTERN (decl) = 1;
21660 FOR_EACH_CLONE (clone, decl)
21661 DECL_NOT_REALLY_EXTERN (clone) = 1;
21662 }
21663
21664 /* Called if RESULT is explicitly instantiated, or is a member of an
21665 explicitly instantiated class. */
21666
21667 void
21668 mark_decl_instantiated (tree result, int extern_p)
21669 {
21670 SET_DECL_EXPLICIT_INSTANTIATION (result);
21671
21672 /* If this entity has already been written out, it's too late to
21673 make any modifications. */
21674 if (TREE_ASM_WRITTEN (result))
21675 return;
21676
21677 /* For anonymous namespace we don't need to do anything. */
21678 if (decl_anon_ns_mem_p (result))
21679 {
21680 gcc_assert (!TREE_PUBLIC (result));
21681 return;
21682 }
21683
21684 if (TREE_CODE (result) != FUNCTION_DECL)
21685 /* The TREE_PUBLIC flag for function declarations will have been
21686 set correctly by tsubst. */
21687 TREE_PUBLIC (result) = 1;
21688
21689 /* This might have been set by an earlier implicit instantiation. */
21690 DECL_COMDAT (result) = 0;
21691
21692 if (extern_p)
21693 DECL_NOT_REALLY_EXTERN (result) = 0;
21694 else
21695 {
21696 mark_definable (result);
21697 mark_needed (result);
21698 /* Always make artificials weak. */
21699 if (DECL_ARTIFICIAL (result) && flag_weak)
21700 comdat_linkage (result);
21701 /* For WIN32 we also want to put explicit instantiations in
21702 linkonce sections. */
21703 else if (TREE_PUBLIC (result))
21704 maybe_make_one_only (result);
21705 }
21706
21707 /* If EXTERN_P, then this function will not be emitted -- unless
21708 followed by an explicit instantiation, at which point its linkage
21709 will be adjusted. If !EXTERN_P, then this function will be
21710 emitted here. In neither circumstance do we want
21711 import_export_decl to adjust the linkage. */
21712 DECL_INTERFACE_KNOWN (result) = 1;
21713 }
21714
21715 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21716 important template arguments. If any are missing, we check whether
21717 they're important by using error_mark_node for substituting into any
21718 args that were used for partial ordering (the ones between ARGS and END)
21719 and seeing if it bubbles up. */
21720
21721 static bool
21722 check_undeduced_parms (tree targs, tree args, tree end)
21723 {
21724 bool found = false;
21725 int i;
21726 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21727 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21728 {
21729 found = true;
21730 TREE_VEC_ELT (targs, i) = error_mark_node;
21731 }
21732 if (found)
21733 {
21734 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21735 if (substed == error_mark_node)
21736 return true;
21737 }
21738 return false;
21739 }
21740
21741 /* Given two function templates PAT1 and PAT2, return:
21742
21743 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21744 -1 if PAT2 is more specialized than PAT1.
21745 0 if neither is more specialized.
21746
21747 LEN indicates the number of parameters we should consider
21748 (defaulted parameters should not be considered).
21749
21750 The 1998 std underspecified function template partial ordering, and
21751 DR214 addresses the issue. We take pairs of arguments, one from
21752 each of the templates, and deduce them against each other. One of
21753 the templates will be more specialized if all the *other*
21754 template's arguments deduce against its arguments and at least one
21755 of its arguments *does* *not* deduce against the other template's
21756 corresponding argument. Deduction is done as for class templates.
21757 The arguments used in deduction have reference and top level cv
21758 qualifiers removed. Iff both arguments were originally reference
21759 types *and* deduction succeeds in both directions, an lvalue reference
21760 wins against an rvalue reference and otherwise the template
21761 with the more cv-qualified argument wins for that pairing (if
21762 neither is more cv-qualified, they both are equal). Unlike regular
21763 deduction, after all the arguments have been deduced in this way,
21764 we do *not* verify the deduced template argument values can be
21765 substituted into non-deduced contexts.
21766
21767 The logic can be a bit confusing here, because we look at deduce1 and
21768 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21769 can find template arguments for pat1 to make arg1 look like arg2, that
21770 means that arg2 is at least as specialized as arg1. */
21771
21772 int
21773 more_specialized_fn (tree pat1, tree pat2, int len)
21774 {
21775 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21776 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21777 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21778 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21779 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21780 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21781 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21782 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21783 tree origs1, origs2;
21784 bool lose1 = false;
21785 bool lose2 = false;
21786
21787 /* Remove the this parameter from non-static member functions. If
21788 one is a non-static member function and the other is not a static
21789 member function, remove the first parameter from that function
21790 also. This situation occurs for operator functions where we
21791 locate both a member function (with this pointer) and non-member
21792 operator (with explicit first operand). */
21793 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21794 {
21795 len--; /* LEN is the number of significant arguments for DECL1 */
21796 args1 = TREE_CHAIN (args1);
21797 if (!DECL_STATIC_FUNCTION_P (decl2))
21798 args2 = TREE_CHAIN (args2);
21799 }
21800 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21801 {
21802 args2 = TREE_CHAIN (args2);
21803 if (!DECL_STATIC_FUNCTION_P (decl1))
21804 {
21805 len--;
21806 args1 = TREE_CHAIN (args1);
21807 }
21808 }
21809
21810 /* If only one is a conversion operator, they are unordered. */
21811 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21812 return 0;
21813
21814 /* Consider the return type for a conversion function */
21815 if (DECL_CONV_FN_P (decl1))
21816 {
21817 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21818 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21819 len++;
21820 }
21821
21822 processing_template_decl++;
21823
21824 origs1 = args1;
21825 origs2 = args2;
21826
21827 while (len--
21828 /* Stop when an ellipsis is seen. */
21829 && args1 != NULL_TREE && args2 != NULL_TREE)
21830 {
21831 tree arg1 = TREE_VALUE (args1);
21832 tree arg2 = TREE_VALUE (args2);
21833 int deduce1, deduce2;
21834 int quals1 = -1;
21835 int quals2 = -1;
21836 int ref1 = 0;
21837 int ref2 = 0;
21838
21839 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21840 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21841 {
21842 /* When both arguments are pack expansions, we need only
21843 unify the patterns themselves. */
21844 arg1 = PACK_EXPANSION_PATTERN (arg1);
21845 arg2 = PACK_EXPANSION_PATTERN (arg2);
21846
21847 /* This is the last comparison we need to do. */
21848 len = 0;
21849 }
21850
21851 /* DR 1847: If a particular P contains no template-parameters that
21852 participate in template argument deduction, that P is not used to
21853 determine the ordering. */
21854 if (!uses_deducible_template_parms (arg1)
21855 && !uses_deducible_template_parms (arg2))
21856 goto next;
21857
21858 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21859 {
21860 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21861 arg1 = TREE_TYPE (arg1);
21862 quals1 = cp_type_quals (arg1);
21863 }
21864
21865 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21866 {
21867 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21868 arg2 = TREE_TYPE (arg2);
21869 quals2 = cp_type_quals (arg2);
21870 }
21871
21872 arg1 = TYPE_MAIN_VARIANT (arg1);
21873 arg2 = TYPE_MAIN_VARIANT (arg2);
21874
21875 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21876 {
21877 int i, len2 = remaining_arguments (args2);
21878 tree parmvec = make_tree_vec (1);
21879 tree argvec = make_tree_vec (len2);
21880 tree ta = args2;
21881
21882 /* Setup the parameter vector, which contains only ARG1. */
21883 TREE_VEC_ELT (parmvec, 0) = arg1;
21884
21885 /* Setup the argument vector, which contains the remaining
21886 arguments. */
21887 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21888 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21889
21890 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21891 argvec, DEDUCE_EXACT,
21892 /*subr=*/true, /*explain_p=*/false)
21893 == 0);
21894
21895 /* We cannot deduce in the other direction, because ARG1 is
21896 a pack expansion but ARG2 is not. */
21897 deduce2 = 0;
21898 }
21899 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21900 {
21901 int i, len1 = remaining_arguments (args1);
21902 tree parmvec = make_tree_vec (1);
21903 tree argvec = make_tree_vec (len1);
21904 tree ta = args1;
21905
21906 /* Setup the parameter vector, which contains only ARG1. */
21907 TREE_VEC_ELT (parmvec, 0) = arg2;
21908
21909 /* Setup the argument vector, which contains the remaining
21910 arguments. */
21911 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21912 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21913
21914 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21915 argvec, DEDUCE_EXACT,
21916 /*subr=*/true, /*explain_p=*/false)
21917 == 0);
21918
21919 /* We cannot deduce in the other direction, because ARG2 is
21920 a pack expansion but ARG1 is not.*/
21921 deduce1 = 0;
21922 }
21923
21924 else
21925 {
21926 /* The normal case, where neither argument is a pack
21927 expansion. */
21928 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21929 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21930 == 0);
21931 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21932 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21933 == 0);
21934 }
21935
21936 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21937 arg2, then arg2 is not as specialized as arg1. */
21938 if (!deduce1)
21939 lose2 = true;
21940 if (!deduce2)
21941 lose1 = true;
21942
21943 /* "If, for a given type, deduction succeeds in both directions
21944 (i.e., the types are identical after the transformations above)
21945 and both P and A were reference types (before being replaced with
21946 the type referred to above):
21947 - if the type from the argument template was an lvalue reference and
21948 the type from the parameter template was not, the argument type is
21949 considered to be more specialized than the other; otherwise,
21950 - if the type from the argument template is more cv-qualified
21951 than the type from the parameter template (as described above),
21952 the argument type is considered to be more specialized than the other;
21953 otherwise,
21954 - neither type is more specialized than the other." */
21955
21956 if (deduce1 && deduce2)
21957 {
21958 if (ref1 && ref2 && ref1 != ref2)
21959 {
21960 if (ref1 > ref2)
21961 lose1 = true;
21962 else
21963 lose2 = true;
21964 }
21965 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21966 {
21967 if ((quals1 & quals2) == quals2)
21968 lose2 = true;
21969 if ((quals1 & quals2) == quals1)
21970 lose1 = true;
21971 }
21972 }
21973
21974 if (lose1 && lose2)
21975 /* We've failed to deduce something in either direction.
21976 These must be unordered. */
21977 break;
21978
21979 next:
21980
21981 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21982 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21983 /* We have already processed all of the arguments in our
21984 handing of the pack expansion type. */
21985 len = 0;
21986
21987 args1 = TREE_CHAIN (args1);
21988 args2 = TREE_CHAIN (args2);
21989 }
21990
21991 /* "In most cases, all template parameters must have values in order for
21992 deduction to succeed, but for partial ordering purposes a template
21993 parameter may remain without a value provided it is not used in the
21994 types being used for partial ordering."
21995
21996 Thus, if we are missing any of the targs1 we need to substitute into
21997 origs1, then pat2 is not as specialized as pat1. This can happen when
21998 there is a nondeduced context. */
21999 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
22000 lose2 = true;
22001 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
22002 lose1 = true;
22003
22004 processing_template_decl--;
22005
22006 /* If both deductions succeed, the partial ordering selects the more
22007 constrained template. */
22008 if (!lose1 && !lose2)
22009 {
22010 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
22011 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
22012 lose1 = !subsumes_constraints (c1, c2);
22013 lose2 = !subsumes_constraints (c2, c1);
22014 }
22015
22016 /* All things being equal, if the next argument is a pack expansion
22017 for one function but not for the other, prefer the
22018 non-variadic function. FIXME this is bogus; see c++/41958. */
22019 if (lose1 == lose2
22020 && args1 && TREE_VALUE (args1)
22021 && args2 && TREE_VALUE (args2))
22022 {
22023 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
22024 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
22025 }
22026
22027 if (lose1 == lose2)
22028 return 0;
22029 else if (!lose1)
22030 return 1;
22031 else
22032 return -1;
22033 }
22034
22035 /* Determine which of two partial specializations of TMPL is more
22036 specialized.
22037
22038 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22039 to the first partial specialization. The TREE_PURPOSE is the
22040 innermost set of template parameters for the partial
22041 specialization. PAT2 is similar, but for the second template.
22042
22043 Return 1 if the first partial specialization is more specialized;
22044 -1 if the second is more specialized; 0 if neither is more
22045 specialized.
22046
22047 See [temp.class.order] for information about determining which of
22048 two templates is more specialized. */
22049
22050 static int
22051 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
22052 {
22053 tree targs;
22054 int winner = 0;
22055 bool any_deductions = false;
22056
22057 tree tmpl1 = TREE_VALUE (pat1);
22058 tree tmpl2 = TREE_VALUE (pat2);
22059 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
22060 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
22061
22062 /* Just like what happens for functions, if we are ordering between
22063 different template specializations, we may encounter dependent
22064 types in the arguments, and we need our dependency check functions
22065 to behave correctly. */
22066 ++processing_template_decl;
22067 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
22068 if (targs)
22069 {
22070 --winner;
22071 any_deductions = true;
22072 }
22073
22074 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
22075 if (targs)
22076 {
22077 ++winner;
22078 any_deductions = true;
22079 }
22080 --processing_template_decl;
22081
22082 /* If both deductions succeed, the partial ordering selects the more
22083 constrained template. */
22084 if (!winner && any_deductions)
22085 return more_constrained (tmpl1, tmpl2);
22086
22087 /* In the case of a tie where at least one of the templates
22088 has a parameter pack at the end, the template with the most
22089 non-packed parameters wins. */
22090 if (winner == 0
22091 && any_deductions
22092 && (template_args_variadic_p (TREE_PURPOSE (pat1))
22093 || template_args_variadic_p (TREE_PURPOSE (pat2))))
22094 {
22095 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
22096 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
22097 int len1 = TREE_VEC_LENGTH (args1);
22098 int len2 = TREE_VEC_LENGTH (args2);
22099
22100 /* We don't count the pack expansion at the end. */
22101 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
22102 --len1;
22103 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
22104 --len2;
22105
22106 if (len1 > len2)
22107 return 1;
22108 else if (len1 < len2)
22109 return -1;
22110 }
22111
22112 return winner;
22113 }
22114
22115 /* Return the template arguments that will produce the function signature
22116 DECL from the function template FN, with the explicit template
22117 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22118 also match. Return NULL_TREE if no satisfactory arguments could be
22119 found. */
22120
22121 static tree
22122 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
22123 {
22124 int ntparms = DECL_NTPARMS (fn);
22125 tree targs = make_tree_vec (ntparms);
22126 tree decl_type = TREE_TYPE (decl);
22127 tree decl_arg_types;
22128 tree *args;
22129 unsigned int nargs, ix;
22130 tree arg;
22131
22132 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
22133
22134 /* Never do unification on the 'this' parameter. */
22135 decl_arg_types = skip_artificial_parms_for (decl,
22136 TYPE_ARG_TYPES (decl_type));
22137
22138 nargs = list_length (decl_arg_types);
22139 args = XALLOCAVEC (tree, nargs);
22140 for (arg = decl_arg_types, ix = 0;
22141 arg != NULL_TREE && arg != void_list_node;
22142 arg = TREE_CHAIN (arg), ++ix)
22143 args[ix] = TREE_VALUE (arg);
22144
22145 if (fn_type_unification (fn, explicit_args, targs,
22146 args, ix,
22147 (check_rettype || DECL_CONV_FN_P (fn)
22148 ? TREE_TYPE (decl_type) : NULL_TREE),
22149 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
22150 /*decltype*/false)
22151 == error_mark_node)
22152 return NULL_TREE;
22153
22154 return targs;
22155 }
22156
22157 /* Return the innermost template arguments that, when applied to a partial
22158 specialization SPEC_TMPL of TMPL, yield the ARGS.
22159
22160 For example, suppose we have:
22161
22162 template <class T, class U> struct S {};
22163 template <class T> struct S<T*, int> {};
22164
22165 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22166 partial specialization and the ARGS will be {double*, int}. The resulting
22167 vector will be {double}, indicating that `T' is bound to `double'. */
22168
22169 static tree
22170 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
22171 {
22172 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
22173 tree spec_args
22174 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
22175 int i, ntparms = TREE_VEC_LENGTH (tparms);
22176 tree deduced_args;
22177 tree innermost_deduced_args;
22178
22179 innermost_deduced_args = make_tree_vec (ntparms);
22180 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22181 {
22182 deduced_args = copy_node (args);
22183 SET_TMPL_ARGS_LEVEL (deduced_args,
22184 TMPL_ARGS_DEPTH (deduced_args),
22185 innermost_deduced_args);
22186 }
22187 else
22188 deduced_args = innermost_deduced_args;
22189
22190 bool tried_array_deduction = (cxx_dialect < cxx17);
22191 again:
22192 if (unify (tparms, deduced_args,
22193 INNERMOST_TEMPLATE_ARGS (spec_args),
22194 INNERMOST_TEMPLATE_ARGS (args),
22195 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22196 return NULL_TREE;
22197
22198 for (i = 0; i < ntparms; ++i)
22199 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22200 {
22201 if (!tried_array_deduction)
22202 {
22203 try_array_deduction (tparms, innermost_deduced_args,
22204 INNERMOST_TEMPLATE_ARGS (spec_args));
22205 tried_array_deduction = true;
22206 if (TREE_VEC_ELT (innermost_deduced_args, i))
22207 goto again;
22208 }
22209 return NULL_TREE;
22210 }
22211
22212 tree tinst = build_tree_list (spec_tmpl, deduced_args);
22213 if (!push_tinst_level (tinst))
22214 {
22215 excessive_deduction_depth = true;
22216 return NULL_TREE;
22217 }
22218
22219 /* Verify that nondeduced template arguments agree with the type
22220 obtained from argument deduction.
22221
22222 For example:
22223
22224 struct A { typedef int X; };
22225 template <class T, class U> struct C {};
22226 template <class T> struct C<T, typename T::X> {};
22227
22228 Then with the instantiation `C<A, int>', we can deduce that
22229 `T' is `A' but unify () does not check whether `typename T::X'
22230 is `int'. */
22231 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22232
22233 if (spec_args != error_mark_node)
22234 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22235 INNERMOST_TEMPLATE_ARGS (spec_args),
22236 tmpl, tf_none, false, false);
22237
22238 pop_tinst_level ();
22239
22240 if (spec_args == error_mark_node
22241 /* We only need to check the innermost arguments; the other
22242 arguments will always agree. */
22243 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22244 INNERMOST_TEMPLATE_ARGS (args)))
22245 return NULL_TREE;
22246
22247 /* Now that we have bindings for all of the template arguments,
22248 ensure that the arguments deduced for the template template
22249 parameters have compatible template parameter lists. See the use
22250 of template_template_parm_bindings_ok_p in fn_type_unification
22251 for more information. */
22252 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22253 return NULL_TREE;
22254
22255 return deduced_args;
22256 }
22257
22258 // Compare two function templates T1 and T2 by deducing bindings
22259 // from one against the other. If both deductions succeed, compare
22260 // constraints to see which is more constrained.
22261 static int
22262 more_specialized_inst (tree t1, tree t2)
22263 {
22264 int fate = 0;
22265 int count = 0;
22266
22267 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22268 {
22269 --fate;
22270 ++count;
22271 }
22272
22273 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22274 {
22275 ++fate;
22276 ++count;
22277 }
22278
22279 // If both deductions succeed, then one may be more constrained.
22280 if (count == 2 && fate == 0)
22281 fate = more_constrained (t1, t2);
22282
22283 return fate;
22284 }
22285
22286 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22287 Return the TREE_LIST node with the most specialized template, if
22288 any. If there is no most specialized template, the error_mark_node
22289 is returned.
22290
22291 Note that this function does not look at, or modify, the
22292 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22293 returned is one of the elements of INSTANTIATIONS, callers may
22294 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22295 and retrieve it from the value returned. */
22296
22297 tree
22298 most_specialized_instantiation (tree templates)
22299 {
22300 tree fn, champ;
22301
22302 ++processing_template_decl;
22303
22304 champ = templates;
22305 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22306 {
22307 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22308 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22309 if (fate == -1)
22310 champ = fn;
22311 else if (!fate)
22312 {
22313 /* Equally specialized, move to next function. If there
22314 is no next function, nothing's most specialized. */
22315 fn = TREE_CHAIN (fn);
22316 champ = fn;
22317 if (!fn)
22318 break;
22319 }
22320 }
22321
22322 if (champ)
22323 /* Now verify that champ is better than everything earlier in the
22324 instantiation list. */
22325 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22326 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22327 {
22328 champ = NULL_TREE;
22329 break;
22330 }
22331 }
22332
22333 processing_template_decl--;
22334
22335 if (!champ)
22336 return error_mark_node;
22337
22338 return champ;
22339 }
22340
22341 /* If DECL is a specialization of some template, return the most
22342 general such template. Otherwise, returns NULL_TREE.
22343
22344 For example, given:
22345
22346 template <class T> struct S { template <class U> void f(U); };
22347
22348 if TMPL is `template <class U> void S<int>::f(U)' this will return
22349 the full template. This function will not trace past partial
22350 specializations, however. For example, given in addition:
22351
22352 template <class T> struct S<T*> { template <class U> void f(U); };
22353
22354 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22355 `template <class T> template <class U> S<T*>::f(U)'. */
22356
22357 tree
22358 most_general_template (tree decl)
22359 {
22360 if (TREE_CODE (decl) != TEMPLATE_DECL)
22361 {
22362 if (tree tinfo = get_template_info (decl))
22363 decl = TI_TEMPLATE (tinfo);
22364 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22365 template friend, or a FIELD_DECL for a capture pack. */
22366 if (TREE_CODE (decl) != TEMPLATE_DECL)
22367 return NULL_TREE;
22368 }
22369
22370 /* Look for more and more general templates. */
22371 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22372 {
22373 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22374 (See cp-tree.h for details.) */
22375 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22376 break;
22377
22378 if (CLASS_TYPE_P (TREE_TYPE (decl))
22379 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22380 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22381 break;
22382
22383 /* Stop if we run into an explicitly specialized class template. */
22384 if (!DECL_NAMESPACE_SCOPE_P (decl)
22385 && DECL_CONTEXT (decl)
22386 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22387 break;
22388
22389 decl = DECL_TI_TEMPLATE (decl);
22390 }
22391
22392 return decl;
22393 }
22394
22395 /* Return the most specialized of the template partial specializations
22396 which can produce TARGET, a specialization of some class or variable
22397 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22398 a TEMPLATE_DECL node corresponding to the partial specialization, while
22399 the TREE_PURPOSE is the set of template arguments that must be
22400 substituted into the template pattern in order to generate TARGET.
22401
22402 If the choice of partial specialization is ambiguous, a diagnostic
22403 is issued, and the error_mark_node is returned. If there are no
22404 partial specializations matching TARGET, then NULL_TREE is
22405 returned, indicating that the primary template should be used. */
22406
22407 static tree
22408 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22409 {
22410 tree list = NULL_TREE;
22411 tree t;
22412 tree champ;
22413 int fate;
22414 bool ambiguous_p;
22415 tree outer_args = NULL_TREE;
22416 tree tmpl, args;
22417
22418 if (TYPE_P (target))
22419 {
22420 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22421 tmpl = TI_TEMPLATE (tinfo);
22422 args = TI_ARGS (tinfo);
22423 }
22424 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22425 {
22426 tmpl = TREE_OPERAND (target, 0);
22427 args = TREE_OPERAND (target, 1);
22428 }
22429 else if (VAR_P (target))
22430 {
22431 tree tinfo = DECL_TEMPLATE_INFO (target);
22432 tmpl = TI_TEMPLATE (tinfo);
22433 args = TI_ARGS (tinfo);
22434 }
22435 else
22436 gcc_unreachable ();
22437
22438 tree main_tmpl = most_general_template (tmpl);
22439
22440 /* For determining which partial specialization to use, only the
22441 innermost args are interesting. */
22442 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22443 {
22444 outer_args = strip_innermost_template_args (args, 1);
22445 args = INNERMOST_TEMPLATE_ARGS (args);
22446 }
22447
22448 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22449 {
22450 tree spec_args;
22451 tree spec_tmpl = TREE_VALUE (t);
22452
22453 if (outer_args)
22454 {
22455 /* Substitute in the template args from the enclosing class. */
22456 ++processing_template_decl;
22457 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22458 --processing_template_decl;
22459 }
22460
22461 if (spec_tmpl == error_mark_node)
22462 return error_mark_node;
22463
22464 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22465 if (spec_args)
22466 {
22467 if (outer_args)
22468 spec_args = add_to_template_args (outer_args, spec_args);
22469
22470 /* Keep the candidate only if the constraints are satisfied,
22471 or if we're not compiling with concepts. */
22472 if (!flag_concepts
22473 || constraints_satisfied_p (spec_tmpl, spec_args))
22474 {
22475 list = tree_cons (spec_args, TREE_VALUE (t), list);
22476 TREE_TYPE (list) = TREE_TYPE (t);
22477 }
22478 }
22479 }
22480
22481 if (! list)
22482 return NULL_TREE;
22483
22484 ambiguous_p = false;
22485 t = list;
22486 champ = t;
22487 t = TREE_CHAIN (t);
22488 for (; t; t = TREE_CHAIN (t))
22489 {
22490 fate = more_specialized_partial_spec (tmpl, champ, t);
22491 if (fate == 1)
22492 ;
22493 else
22494 {
22495 if (fate == 0)
22496 {
22497 t = TREE_CHAIN (t);
22498 if (! t)
22499 {
22500 ambiguous_p = true;
22501 break;
22502 }
22503 }
22504 champ = t;
22505 }
22506 }
22507
22508 if (!ambiguous_p)
22509 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22510 {
22511 fate = more_specialized_partial_spec (tmpl, champ, t);
22512 if (fate != 1)
22513 {
22514 ambiguous_p = true;
22515 break;
22516 }
22517 }
22518
22519 if (ambiguous_p)
22520 {
22521 const char *str;
22522 char *spaces = NULL;
22523 if (!(complain & tf_error))
22524 return error_mark_node;
22525 if (TYPE_P (target))
22526 error ("ambiguous template instantiation for %q#T", target);
22527 else
22528 error ("ambiguous template instantiation for %q#D", target);
22529 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22530 for (t = list; t; t = TREE_CHAIN (t))
22531 {
22532 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22533 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22534 "%s %#qS", spaces ? spaces : str, subst);
22535 spaces = spaces ? spaces : get_spaces (str);
22536 }
22537 free (spaces);
22538 return error_mark_node;
22539 }
22540
22541 return champ;
22542 }
22543
22544 /* Explicitly instantiate DECL. */
22545
22546 void
22547 do_decl_instantiation (tree decl, tree storage)
22548 {
22549 tree result = NULL_TREE;
22550 int extern_p = 0;
22551
22552 if (!decl || decl == error_mark_node)
22553 /* An error occurred, for which grokdeclarator has already issued
22554 an appropriate message. */
22555 return;
22556 else if (! DECL_LANG_SPECIFIC (decl))
22557 {
22558 error ("explicit instantiation of non-template %q#D", decl);
22559 return;
22560 }
22561
22562 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22563 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22564
22565 if (VAR_P (decl) && !var_templ)
22566 {
22567 /* There is an asymmetry here in the way VAR_DECLs and
22568 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22569 the latter, the DECL we get back will be marked as a
22570 template instantiation, and the appropriate
22571 DECL_TEMPLATE_INFO will be set up. This does not happen for
22572 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22573 should handle VAR_DECLs as it currently handles
22574 FUNCTION_DECLs. */
22575 if (!DECL_CLASS_SCOPE_P (decl))
22576 {
22577 error ("%qD is not a static data member of a class template", decl);
22578 return;
22579 }
22580 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22581 if (!result || !VAR_P (result))
22582 {
22583 error ("no matching template for %qD found", decl);
22584 return;
22585 }
22586 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22587 {
22588 error ("type %qT for explicit instantiation %qD does not match "
22589 "declared type %qT", TREE_TYPE (result), decl,
22590 TREE_TYPE (decl));
22591 return;
22592 }
22593 }
22594 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22595 {
22596 error ("explicit instantiation of %q#D", decl);
22597 return;
22598 }
22599 else
22600 result = decl;
22601
22602 /* Check for various error cases. Note that if the explicit
22603 instantiation is valid the RESULT will currently be marked as an
22604 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22605 until we get here. */
22606
22607 if (DECL_TEMPLATE_SPECIALIZATION (result))
22608 {
22609 /* DR 259 [temp.spec].
22610
22611 Both an explicit instantiation and a declaration of an explicit
22612 specialization shall not appear in a program unless the explicit
22613 instantiation follows a declaration of the explicit specialization.
22614
22615 For a given set of template parameters, if an explicit
22616 instantiation of a template appears after a declaration of an
22617 explicit specialization for that template, the explicit
22618 instantiation has no effect. */
22619 return;
22620 }
22621 else if (DECL_EXPLICIT_INSTANTIATION (result))
22622 {
22623 /* [temp.spec]
22624
22625 No program shall explicitly instantiate any template more
22626 than once.
22627
22628 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22629 the first instantiation was `extern' and the second is not,
22630 and EXTERN_P for the opposite case. */
22631 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22632 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22633 /* If an "extern" explicit instantiation follows an ordinary
22634 explicit instantiation, the template is instantiated. */
22635 if (extern_p)
22636 return;
22637 }
22638 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22639 {
22640 error ("no matching template for %qD found", result);
22641 return;
22642 }
22643 else if (!DECL_TEMPLATE_INFO (result))
22644 {
22645 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22646 return;
22647 }
22648
22649 if (storage == NULL_TREE)
22650 ;
22651 else if (storage == ridpointers[(int) RID_EXTERN])
22652 {
22653 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22654 pedwarn (input_location, OPT_Wpedantic,
22655 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22656 "instantiations");
22657 extern_p = 1;
22658 }
22659 else
22660 error ("storage class %qD applied to template instantiation", storage);
22661
22662 check_explicit_instantiation_namespace (result);
22663 mark_decl_instantiated (result, extern_p);
22664 if (! extern_p)
22665 instantiate_decl (result, /*defer_ok=*/true,
22666 /*expl_inst_class_mem_p=*/false);
22667 }
22668
22669 static void
22670 mark_class_instantiated (tree t, int extern_p)
22671 {
22672 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22673 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22674 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22675 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22676 if (! extern_p)
22677 {
22678 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22679 rest_of_type_compilation (t, 1);
22680 }
22681 }
22682
22683 /* Called from do_type_instantiation through binding_table_foreach to
22684 do recursive instantiation for the type bound in ENTRY. */
22685 static void
22686 bt_instantiate_type_proc (binding_entry entry, void *data)
22687 {
22688 tree storage = *(tree *) data;
22689
22690 if (MAYBE_CLASS_TYPE_P (entry->type)
22691 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22692 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22693 }
22694
22695 /* Perform an explicit instantiation of template class T. STORAGE, if
22696 non-null, is the RID for extern, inline or static. COMPLAIN is
22697 nonzero if this is called from the parser, zero if called recursively,
22698 since the standard is unclear (as detailed below). */
22699
22700 void
22701 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22702 {
22703 int extern_p = 0;
22704 int nomem_p = 0;
22705 int static_p = 0;
22706 int previous_instantiation_extern_p = 0;
22707
22708 if (TREE_CODE (t) == TYPE_DECL)
22709 t = TREE_TYPE (t);
22710
22711 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22712 {
22713 tree tmpl =
22714 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22715 if (tmpl)
22716 error ("explicit instantiation of non-class template %qD", tmpl);
22717 else
22718 error ("explicit instantiation of non-template type %qT", t);
22719 return;
22720 }
22721
22722 complete_type (t);
22723
22724 if (!COMPLETE_TYPE_P (t))
22725 {
22726 if (complain & tf_error)
22727 error ("explicit instantiation of %q#T before definition of template",
22728 t);
22729 return;
22730 }
22731
22732 if (storage != NULL_TREE)
22733 {
22734 if (!in_system_header_at (input_location))
22735 {
22736 if (storage == ridpointers[(int) RID_EXTERN])
22737 {
22738 if (cxx_dialect == cxx98)
22739 pedwarn (input_location, OPT_Wpedantic,
22740 "ISO C++ 1998 forbids the use of %<extern%> on "
22741 "explicit instantiations");
22742 }
22743 else
22744 pedwarn (input_location, OPT_Wpedantic,
22745 "ISO C++ forbids the use of %qE"
22746 " on explicit instantiations", storage);
22747 }
22748
22749 if (storage == ridpointers[(int) RID_INLINE])
22750 nomem_p = 1;
22751 else if (storage == ridpointers[(int) RID_EXTERN])
22752 extern_p = 1;
22753 else if (storage == ridpointers[(int) RID_STATIC])
22754 static_p = 1;
22755 else
22756 {
22757 error ("storage class %qD applied to template instantiation",
22758 storage);
22759 extern_p = 0;
22760 }
22761 }
22762
22763 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22764 {
22765 /* DR 259 [temp.spec].
22766
22767 Both an explicit instantiation and a declaration of an explicit
22768 specialization shall not appear in a program unless the explicit
22769 instantiation follows a declaration of the explicit specialization.
22770
22771 For a given set of template parameters, if an explicit
22772 instantiation of a template appears after a declaration of an
22773 explicit specialization for that template, the explicit
22774 instantiation has no effect. */
22775 return;
22776 }
22777 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22778 {
22779 /* [temp.spec]
22780
22781 No program shall explicitly instantiate any template more
22782 than once.
22783
22784 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22785 instantiation was `extern'. If EXTERN_P then the second is.
22786 These cases are OK. */
22787 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22788
22789 if (!previous_instantiation_extern_p && !extern_p
22790 && (complain & tf_error))
22791 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22792
22793 /* If we've already instantiated the template, just return now. */
22794 if (!CLASSTYPE_INTERFACE_ONLY (t))
22795 return;
22796 }
22797
22798 check_explicit_instantiation_namespace (TYPE_NAME (t));
22799 mark_class_instantiated (t, extern_p);
22800
22801 if (nomem_p)
22802 return;
22803
22804 /* In contrast to implicit instantiation, where only the
22805 declarations, and not the definitions, of members are
22806 instantiated, we have here:
22807
22808 [temp.explicit]
22809
22810 The explicit instantiation of a class template specialization
22811 implies the instantiation of all of its members not
22812 previously explicitly specialized in the translation unit
22813 containing the explicit instantiation.
22814
22815 Of course, we can't instantiate member template classes, since we
22816 don't have any arguments for them. Note that the standard is
22817 unclear on whether the instantiation of the members are
22818 *explicit* instantiations or not. However, the most natural
22819 interpretation is that it should be an explicit
22820 instantiation. */
22821 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22822 if ((VAR_P (fld)
22823 || (TREE_CODE (fld) == FUNCTION_DECL
22824 && !static_p
22825 && user_provided_p (fld)))
22826 && DECL_TEMPLATE_INSTANTIATION (fld))
22827 {
22828 mark_decl_instantiated (fld, extern_p);
22829 if (! extern_p)
22830 instantiate_decl (fld, /*defer_ok=*/true,
22831 /*expl_inst_class_mem_p=*/true);
22832 }
22833
22834 if (CLASSTYPE_NESTED_UTDS (t))
22835 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22836 bt_instantiate_type_proc, &storage);
22837 }
22838
22839 /* Given a function DECL, which is a specialization of TMPL, modify
22840 DECL to be a re-instantiation of TMPL with the same template
22841 arguments. TMPL should be the template into which tsubst'ing
22842 should occur for DECL, not the most general template.
22843
22844 One reason for doing this is a scenario like this:
22845
22846 template <class T>
22847 void f(const T&, int i);
22848
22849 void g() { f(3, 7); }
22850
22851 template <class T>
22852 void f(const T& t, const int i) { }
22853
22854 Note that when the template is first instantiated, with
22855 instantiate_template, the resulting DECL will have no name for the
22856 first parameter, and the wrong type for the second. So, when we go
22857 to instantiate the DECL, we regenerate it. */
22858
22859 static void
22860 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22861 {
22862 /* The arguments used to instantiate DECL, from the most general
22863 template. */
22864 tree code_pattern;
22865
22866 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22867
22868 /* Make sure that we can see identifiers, and compute access
22869 correctly. */
22870 push_access_scope (decl);
22871
22872 if (TREE_CODE (decl) == FUNCTION_DECL)
22873 {
22874 tree decl_parm;
22875 tree pattern_parm;
22876 tree specs;
22877 int args_depth;
22878 int parms_depth;
22879
22880 args_depth = TMPL_ARGS_DEPTH (args);
22881 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22882 if (args_depth > parms_depth)
22883 args = get_innermost_template_args (args, parms_depth);
22884
22885 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22886 args, tf_error, NULL_TREE,
22887 /*defer_ok*/false);
22888 if (specs && specs != error_mark_node)
22889 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22890 specs);
22891
22892 /* Merge parameter declarations. */
22893 decl_parm = skip_artificial_parms_for (decl,
22894 DECL_ARGUMENTS (decl));
22895 pattern_parm
22896 = skip_artificial_parms_for (code_pattern,
22897 DECL_ARGUMENTS (code_pattern));
22898 while (decl_parm && !DECL_PACK_P (pattern_parm))
22899 {
22900 tree parm_type;
22901 tree attributes;
22902
22903 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22904 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22905 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22906 NULL_TREE);
22907 parm_type = type_decays_to (parm_type);
22908 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22909 TREE_TYPE (decl_parm) = parm_type;
22910 attributes = DECL_ATTRIBUTES (pattern_parm);
22911 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22912 {
22913 DECL_ATTRIBUTES (decl_parm) = attributes;
22914 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22915 }
22916 decl_parm = DECL_CHAIN (decl_parm);
22917 pattern_parm = DECL_CHAIN (pattern_parm);
22918 }
22919 /* Merge any parameters that match with the function parameter
22920 pack. */
22921 if (pattern_parm && DECL_PACK_P (pattern_parm))
22922 {
22923 int i, len;
22924 tree expanded_types;
22925 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22926 the parameters in this function parameter pack. */
22927 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22928 args, tf_error, NULL_TREE);
22929 len = TREE_VEC_LENGTH (expanded_types);
22930 for (i = 0; i < len; i++)
22931 {
22932 tree parm_type;
22933 tree attributes;
22934
22935 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22936 /* Rename the parameter to include the index. */
22937 DECL_NAME (decl_parm) =
22938 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22939 parm_type = TREE_VEC_ELT (expanded_types, i);
22940 parm_type = type_decays_to (parm_type);
22941 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22942 TREE_TYPE (decl_parm) = parm_type;
22943 attributes = DECL_ATTRIBUTES (pattern_parm);
22944 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22945 {
22946 DECL_ATTRIBUTES (decl_parm) = attributes;
22947 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22948 }
22949 decl_parm = DECL_CHAIN (decl_parm);
22950 }
22951 }
22952 /* Merge additional specifiers from the CODE_PATTERN. */
22953 if (DECL_DECLARED_INLINE_P (code_pattern)
22954 && !DECL_DECLARED_INLINE_P (decl))
22955 DECL_DECLARED_INLINE_P (decl) = 1;
22956 }
22957 else if (VAR_P (decl))
22958 {
22959 start_lambda_scope (decl);
22960 DECL_INITIAL (decl) =
22961 tsubst_expr (DECL_INITIAL (code_pattern), args,
22962 tf_error, DECL_TI_TEMPLATE (decl),
22963 /*integral_constant_expression_p=*/false);
22964 finish_lambda_scope ();
22965 if (VAR_HAD_UNKNOWN_BOUND (decl))
22966 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22967 tf_error, DECL_TI_TEMPLATE (decl));
22968 }
22969 else
22970 gcc_unreachable ();
22971
22972 pop_access_scope (decl);
22973 }
22974
22975 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22976 substituted to get DECL. */
22977
22978 tree
22979 template_for_substitution (tree decl)
22980 {
22981 tree tmpl = DECL_TI_TEMPLATE (decl);
22982
22983 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22984 for the instantiation. This is not always the most general
22985 template. Consider, for example:
22986
22987 template <class T>
22988 struct S { template <class U> void f();
22989 template <> void f<int>(); };
22990
22991 and an instantiation of S<double>::f<int>. We want TD to be the
22992 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22993 while (/* An instantiation cannot have a definition, so we need a
22994 more general template. */
22995 DECL_TEMPLATE_INSTANTIATION (tmpl)
22996 /* We must also deal with friend templates. Given:
22997
22998 template <class T> struct S {
22999 template <class U> friend void f() {};
23000 };
23001
23002 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23003 so far as the language is concerned, but that's still
23004 where we get the pattern for the instantiation from. On
23005 other hand, if the definition comes outside the class, say:
23006
23007 template <class T> struct S {
23008 template <class U> friend void f();
23009 };
23010 template <class U> friend void f() {}
23011
23012 we don't need to look any further. That's what the check for
23013 DECL_INITIAL is for. */
23014 || (TREE_CODE (decl) == FUNCTION_DECL
23015 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
23016 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
23017 {
23018 /* The present template, TD, should not be a definition. If it
23019 were a definition, we should be using it! Note that we
23020 cannot restructure the loop to just keep going until we find
23021 a template with a definition, since that might go too far if
23022 a specialization was declared, but not defined. */
23023
23024 /* Fetch the more general template. */
23025 tmpl = DECL_TI_TEMPLATE (tmpl);
23026 }
23027
23028 return tmpl;
23029 }
23030
23031 /* Returns true if we need to instantiate this template instance even if we
23032 know we aren't going to emit it. */
23033
23034 bool
23035 always_instantiate_p (tree decl)
23036 {
23037 /* We always instantiate inline functions so that we can inline them. An
23038 explicit instantiation declaration prohibits implicit instantiation of
23039 non-inline functions. With high levels of optimization, we would
23040 normally inline non-inline functions -- but we're not allowed to do
23041 that for "extern template" functions. Therefore, we check
23042 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23043 return ((TREE_CODE (decl) == FUNCTION_DECL
23044 && (DECL_DECLARED_INLINE_P (decl)
23045 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
23046 /* And we need to instantiate static data members so that
23047 their initializers are available in integral constant
23048 expressions. */
23049 || (VAR_P (decl)
23050 && decl_maybe_constant_var_p (decl)));
23051 }
23052
23053 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23054 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23055 error, true otherwise. */
23056
23057 bool
23058 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
23059 {
23060 tree fntype, spec, noex, clone;
23061
23062 /* Don't instantiate a noexcept-specification from template context. */
23063 if (processing_template_decl)
23064 return true;
23065
23066 if (DECL_CLONED_FUNCTION_P (fn))
23067 fn = DECL_CLONED_FUNCTION (fn);
23068 fntype = TREE_TYPE (fn);
23069 spec = TYPE_RAISES_EXCEPTIONS (fntype);
23070
23071 if (!spec || !TREE_PURPOSE (spec))
23072 return true;
23073
23074 noex = TREE_PURPOSE (spec);
23075
23076 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
23077 {
23078 static hash_set<tree>* fns = new hash_set<tree>;
23079 bool added = false;
23080 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
23081 spec = get_defaulted_eh_spec (fn, complain);
23082 else if (!(added = !fns->add (fn)))
23083 {
23084 /* If hash_set::add returns true, the element was already there. */
23085 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
23086 DECL_SOURCE_LOCATION (fn));
23087 error_at (loc,
23088 "exception specification of %qD depends on itself",
23089 fn);
23090 spec = noexcept_false_spec;
23091 }
23092 else if (push_tinst_level (fn))
23093 {
23094 push_access_scope (fn);
23095 push_deferring_access_checks (dk_no_deferred);
23096 input_location = DECL_SOURCE_LOCATION (fn);
23097 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
23098 DEFERRED_NOEXCEPT_ARGS (noex),
23099 tf_warning_or_error, fn,
23100 /*function_p=*/false,
23101 /*integral_constant_expression_p=*/true);
23102 pop_deferring_access_checks ();
23103 pop_access_scope (fn);
23104 pop_tinst_level ();
23105 spec = build_noexcept_spec (noex, tf_warning_or_error);
23106 if (spec == error_mark_node)
23107 spec = noexcept_false_spec;
23108 }
23109 else
23110 spec = noexcept_false_spec;
23111
23112 if (added)
23113 fns->remove (fn);
23114
23115 if (spec == error_mark_node)
23116 return false;
23117
23118 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
23119 }
23120
23121 FOR_EACH_CLONE (clone, fn)
23122 {
23123 if (TREE_TYPE (clone) == fntype)
23124 TREE_TYPE (clone) = TREE_TYPE (fn);
23125 else
23126 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
23127 }
23128
23129 return true;
23130 }
23131
23132 /* We're starting to process the function INST, an instantiation of PATTERN;
23133 add their parameters to local_specializations. */
23134
23135 static void
23136 register_parameter_specializations (tree pattern, tree inst)
23137 {
23138 tree tmpl_parm = DECL_ARGUMENTS (pattern);
23139 tree spec_parm = DECL_ARGUMENTS (inst);
23140 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
23141 {
23142 register_local_specialization (spec_parm, tmpl_parm);
23143 spec_parm = skip_artificial_parms_for (inst, spec_parm);
23144 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
23145 }
23146 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23147 {
23148 if (!DECL_PACK_P (tmpl_parm))
23149 {
23150 register_local_specialization (spec_parm, tmpl_parm);
23151 spec_parm = DECL_CHAIN (spec_parm);
23152 }
23153 else
23154 {
23155 /* Register the (value) argument pack as a specialization of
23156 TMPL_PARM, then move on. */
23157 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23158 register_local_specialization (argpack, tmpl_parm);
23159 }
23160 }
23161 gcc_assert (!spec_parm);
23162 }
23163
23164 /* Produce the definition of D, a _DECL generated from a template. If
23165 DEFER_OK is true, then we don't have to actually do the
23166 instantiation now; we just have to do it sometime. Normally it is
23167 an error if this is an explicit instantiation but D is undefined.
23168 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23169 instantiated class template. */
23170
23171 tree
23172 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
23173 {
23174 tree tmpl = DECL_TI_TEMPLATE (d);
23175 tree gen_args;
23176 tree args;
23177 tree td;
23178 tree code_pattern;
23179 tree spec;
23180 tree gen_tmpl;
23181 bool pattern_defined;
23182 location_t saved_loc = input_location;
23183 int saved_unevaluated_operand = cp_unevaluated_operand;
23184 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23185 bool external_p;
23186 bool deleted_p;
23187
23188 /* This function should only be used to instantiate templates for
23189 functions and static member variables. */
23190 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23191
23192 /* A concept is never instantiated. */
23193 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23194
23195 /* Variables are never deferred; if instantiation is required, they
23196 are instantiated right away. That allows for better code in the
23197 case that an expression refers to the value of the variable --
23198 if the variable has a constant value the referring expression can
23199 take advantage of that fact. */
23200 if (VAR_P (d))
23201 defer_ok = false;
23202
23203 /* Don't instantiate cloned functions. Instead, instantiate the
23204 functions they cloned. */
23205 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23206 d = DECL_CLONED_FUNCTION (d);
23207
23208 if (DECL_TEMPLATE_INSTANTIATED (d)
23209 || (TREE_CODE (d) == FUNCTION_DECL
23210 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23211 || DECL_TEMPLATE_SPECIALIZATION (d))
23212 /* D has already been instantiated or explicitly specialized, so
23213 there's nothing for us to do here.
23214
23215 It might seem reasonable to check whether or not D is an explicit
23216 instantiation, and, if so, stop here. But when an explicit
23217 instantiation is deferred until the end of the compilation,
23218 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23219 the instantiation. */
23220 return d;
23221
23222 /* Check to see whether we know that this template will be
23223 instantiated in some other file, as with "extern template"
23224 extension. */
23225 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23226
23227 /* In general, we do not instantiate such templates. */
23228 if (external_p && !always_instantiate_p (d))
23229 return d;
23230
23231 gen_tmpl = most_general_template (tmpl);
23232 gen_args = DECL_TI_ARGS (d);
23233
23234 if (tmpl != gen_tmpl)
23235 /* We should already have the extra args. */
23236 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23237 == TMPL_ARGS_DEPTH (gen_args));
23238 /* And what's in the hash table should match D. */
23239 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23240 || spec == NULL_TREE);
23241
23242 /* This needs to happen before any tsubsting. */
23243 if (! push_tinst_level (d))
23244 return d;
23245
23246 timevar_push (TV_TEMPLATE_INST);
23247
23248 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23249 for the instantiation. */
23250 td = template_for_substitution (d);
23251 args = gen_args;
23252
23253 if (VAR_P (d))
23254 {
23255 /* Look up an explicit specialization, if any. */
23256 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23257 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23258 if (elt && elt != error_mark_node)
23259 {
23260 td = TREE_VALUE (elt);
23261 args = TREE_PURPOSE (elt);
23262 }
23263 }
23264
23265 code_pattern = DECL_TEMPLATE_RESULT (td);
23266
23267 /* We should never be trying to instantiate a member of a class
23268 template or partial specialization. */
23269 gcc_assert (d != code_pattern);
23270
23271 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23272 || DECL_TEMPLATE_SPECIALIZATION (td))
23273 /* In the case of a friend template whose definition is provided
23274 outside the class, we may have too many arguments. Drop the
23275 ones we don't need. The same is true for specializations. */
23276 args = get_innermost_template_args
23277 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23278
23279 if (TREE_CODE (d) == FUNCTION_DECL)
23280 {
23281 deleted_p = DECL_DELETED_FN (code_pattern);
23282 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23283 && DECL_INITIAL (code_pattern) != error_mark_node)
23284 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23285 || deleted_p);
23286 }
23287 else
23288 {
23289 deleted_p = false;
23290 if (DECL_CLASS_SCOPE_P (code_pattern))
23291 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23292 || DECL_INLINE_VAR_P (code_pattern));
23293 else
23294 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23295 }
23296
23297 /* We may be in the middle of deferred access check. Disable it now. */
23298 push_deferring_access_checks (dk_no_deferred);
23299
23300 /* Unless an explicit instantiation directive has already determined
23301 the linkage of D, remember that a definition is available for
23302 this entity. */
23303 if (pattern_defined
23304 && !DECL_INTERFACE_KNOWN (d)
23305 && !DECL_NOT_REALLY_EXTERN (d))
23306 mark_definable (d);
23307
23308 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23309 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23310 input_location = DECL_SOURCE_LOCATION (d);
23311
23312 /* If D is a member of an explicitly instantiated class template,
23313 and no definition is available, treat it like an implicit
23314 instantiation. */
23315 if (!pattern_defined && expl_inst_class_mem_p
23316 && DECL_EXPLICIT_INSTANTIATION (d))
23317 {
23318 /* Leave linkage flags alone on instantiations with anonymous
23319 visibility. */
23320 if (TREE_PUBLIC (d))
23321 {
23322 DECL_NOT_REALLY_EXTERN (d) = 0;
23323 DECL_INTERFACE_KNOWN (d) = 0;
23324 }
23325 SET_DECL_IMPLICIT_INSTANTIATION (d);
23326 }
23327
23328 /* Defer all other templates, unless we have been explicitly
23329 forbidden from doing so. */
23330 if (/* If there is no definition, we cannot instantiate the
23331 template. */
23332 ! pattern_defined
23333 /* If it's OK to postpone instantiation, do so. */
23334 || defer_ok
23335 /* If this is a static data member that will be defined
23336 elsewhere, we don't want to instantiate the entire data
23337 member, but we do want to instantiate the initializer so that
23338 we can substitute that elsewhere. */
23339 || (external_p && VAR_P (d))
23340 /* Handle here a deleted function too, avoid generating
23341 its body (c++/61080). */
23342 || deleted_p)
23343 {
23344 /* The definition of the static data member is now required so
23345 we must substitute the initializer. */
23346 if (VAR_P (d)
23347 && !DECL_INITIAL (d)
23348 && DECL_INITIAL (code_pattern))
23349 {
23350 tree ns;
23351 tree init;
23352 bool const_init = false;
23353 bool enter_context = DECL_CLASS_SCOPE_P (d);
23354
23355 ns = decl_namespace_context (d);
23356 push_nested_namespace (ns);
23357 if (enter_context)
23358 push_nested_class (DECL_CONTEXT (d));
23359 init = tsubst_expr (DECL_INITIAL (code_pattern),
23360 args,
23361 tf_warning_or_error, NULL_TREE,
23362 /*integral_constant_expression_p=*/false);
23363 /* If instantiating the initializer involved instantiating this
23364 again, don't call cp_finish_decl twice. */
23365 if (!DECL_INITIAL (d))
23366 {
23367 /* Make sure the initializer is still constant, in case of
23368 circular dependency (template/instantiate6.C). */
23369 const_init
23370 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23371 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23372 /*asmspec_tree=*/NULL_TREE,
23373 LOOKUP_ONLYCONVERTING);
23374 }
23375 if (enter_context)
23376 pop_nested_class ();
23377 pop_nested_namespace (ns);
23378 }
23379
23380 /* We restore the source position here because it's used by
23381 add_pending_template. */
23382 input_location = saved_loc;
23383
23384 if (at_eof && !pattern_defined
23385 && DECL_EXPLICIT_INSTANTIATION (d)
23386 && DECL_NOT_REALLY_EXTERN (d))
23387 /* [temp.explicit]
23388
23389 The definition of a non-exported function template, a
23390 non-exported member function template, or a non-exported
23391 member function or static data member of a class template
23392 shall be present in every translation unit in which it is
23393 explicitly instantiated. */
23394 permerror (input_location, "explicit instantiation of %qD "
23395 "but no definition available", d);
23396
23397 /* If we're in unevaluated context, we just wanted to get the
23398 constant value; this isn't an odr use, so don't queue
23399 a full instantiation. */
23400 if (cp_unevaluated_operand != 0)
23401 goto out;
23402 /* ??? Historically, we have instantiated inline functions, even
23403 when marked as "extern template". */
23404 if (!(external_p && VAR_P (d)))
23405 add_pending_template (d);
23406 goto out;
23407 }
23408 /* Tell the repository that D is available in this translation unit
23409 -- and see if it is supposed to be instantiated here. */
23410 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23411 {
23412 /* In a PCH file, despite the fact that the repository hasn't
23413 requested instantiation in the PCH it is still possible that
23414 an instantiation will be required in a file that includes the
23415 PCH. */
23416 if (pch_file)
23417 add_pending_template (d);
23418 /* Instantiate inline functions so that the inliner can do its
23419 job, even though we'll not be emitting a copy of this
23420 function. */
23421 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23422 goto out;
23423 }
23424
23425 bool push_to_top, nested;
23426 tree fn_context;
23427 fn_context = decl_function_context (d);
23428 nested = current_function_decl != NULL_TREE;
23429 push_to_top = !(nested && fn_context == current_function_decl);
23430
23431 vec<tree> omp_privatization_save;
23432 if (nested)
23433 save_omp_privatization_clauses (omp_privatization_save);
23434
23435 if (push_to_top)
23436 push_to_top_level ();
23437 else
23438 {
23439 push_function_context ();
23440 cp_unevaluated_operand = 0;
23441 c_inhibit_evaluation_warnings = 0;
23442 }
23443
23444 /* Mark D as instantiated so that recursive calls to
23445 instantiate_decl do not try to instantiate it again. */
23446 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23447
23448 /* Regenerate the declaration in case the template has been modified
23449 by a subsequent redeclaration. */
23450 regenerate_decl_from_template (d, td, args);
23451
23452 /* We already set the file and line above. Reset them now in case
23453 they changed as a result of calling regenerate_decl_from_template. */
23454 input_location = DECL_SOURCE_LOCATION (d);
23455
23456 if (VAR_P (d))
23457 {
23458 tree init;
23459 bool const_init = false;
23460
23461 /* Clear out DECL_RTL; whatever was there before may not be right
23462 since we've reset the type of the declaration. */
23463 SET_DECL_RTL (d, NULL);
23464 DECL_IN_AGGR_P (d) = 0;
23465
23466 /* The initializer is placed in DECL_INITIAL by
23467 regenerate_decl_from_template so we don't need to
23468 push/pop_access_scope again here. Pull it out so that
23469 cp_finish_decl can process it. */
23470 init = DECL_INITIAL (d);
23471 DECL_INITIAL (d) = NULL_TREE;
23472 DECL_INITIALIZED_P (d) = 0;
23473
23474 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23475 initializer. That function will defer actual emission until
23476 we have a chance to determine linkage. */
23477 DECL_EXTERNAL (d) = 0;
23478
23479 /* Enter the scope of D so that access-checking works correctly. */
23480 bool enter_context = DECL_CLASS_SCOPE_P (d);
23481 if (enter_context)
23482 push_nested_class (DECL_CONTEXT (d));
23483
23484 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23485 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23486
23487 if (enter_context)
23488 pop_nested_class ();
23489
23490 if (variable_template_p (gen_tmpl))
23491 note_variable_template_instantiation (d);
23492 }
23493 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23494 synthesize_method (d);
23495 else if (TREE_CODE (d) == FUNCTION_DECL)
23496 {
23497 /* Set up the list of local specializations. */
23498 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23499 tree block = NULL_TREE;
23500
23501 /* Set up context. */
23502 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23503 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23504 block = push_stmt_list ();
23505 else
23506 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23507
23508 /* Some typedefs referenced from within the template code need to be
23509 access checked at template instantiation time, i.e now. These
23510 types were added to the template at parsing time. Let's get those
23511 and perform the access checks then. */
23512 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23513 args);
23514
23515 /* Create substitution entries for the parameters. */
23516 register_parameter_specializations (code_pattern, d);
23517
23518 /* Substitute into the body of the function. */
23519 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23520 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23521 tf_warning_or_error, tmpl);
23522 else
23523 {
23524 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23525 tf_warning_or_error, tmpl,
23526 /*integral_constant_expression_p=*/false);
23527
23528 /* Set the current input_location to the end of the function
23529 so that finish_function knows where we are. */
23530 input_location
23531 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23532
23533 /* Remember if we saw an infinite loop in the template. */
23534 current_function_infinite_loop
23535 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23536 }
23537
23538 /* Finish the function. */
23539 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23540 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23541 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23542 else
23543 {
23544 d = finish_function (/*inline_p=*/false);
23545 expand_or_defer_fn (d);
23546 }
23547
23548 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23549 cp_check_omp_declare_reduction (d);
23550 }
23551
23552 /* We're not deferring instantiation any more. */
23553 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23554
23555 if (push_to_top)
23556 pop_from_top_level ();
23557 else
23558 pop_function_context ();
23559
23560 if (nested)
23561 restore_omp_privatization_clauses (omp_privatization_save);
23562
23563 out:
23564 pop_deferring_access_checks ();
23565 timevar_pop (TV_TEMPLATE_INST);
23566 pop_tinst_level ();
23567 input_location = saved_loc;
23568 cp_unevaluated_operand = saved_unevaluated_operand;
23569 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23570
23571 return d;
23572 }
23573
23574 /* Run through the list of templates that we wish we could
23575 instantiate, and instantiate any we can. RETRIES is the
23576 number of times we retry pending template instantiation. */
23577
23578 void
23579 instantiate_pending_templates (int retries)
23580 {
23581 int reconsider;
23582 location_t saved_loc = input_location;
23583
23584 /* Instantiating templates may trigger vtable generation. This in turn
23585 may require further template instantiations. We place a limit here
23586 to avoid infinite loop. */
23587 if (pending_templates && retries >= max_tinst_depth)
23588 {
23589 tree decl = pending_templates->tinst->decl;
23590
23591 fatal_error (input_location,
23592 "template instantiation depth exceeds maximum of %d"
23593 " instantiating %q+D, possibly from virtual table generation"
23594 " (use -ftemplate-depth= to increase the maximum)",
23595 max_tinst_depth, decl);
23596 if (TREE_CODE (decl) == FUNCTION_DECL)
23597 /* Pretend that we defined it. */
23598 DECL_INITIAL (decl) = error_mark_node;
23599 return;
23600 }
23601
23602 do
23603 {
23604 struct pending_template **t = &pending_templates;
23605 struct pending_template *last = NULL;
23606 reconsider = 0;
23607 while (*t)
23608 {
23609 tree instantiation = reopen_tinst_level ((*t)->tinst);
23610 bool complete = false;
23611
23612 if (TYPE_P (instantiation))
23613 {
23614 if (!COMPLETE_TYPE_P (instantiation))
23615 {
23616 instantiate_class_template (instantiation);
23617 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23618 for (tree fld = TYPE_FIELDS (instantiation);
23619 fld; fld = TREE_CHAIN (fld))
23620 if ((VAR_P (fld)
23621 || (TREE_CODE (fld) == FUNCTION_DECL
23622 && !DECL_ARTIFICIAL (fld)))
23623 && DECL_TEMPLATE_INSTANTIATION (fld))
23624 instantiate_decl (fld,
23625 /*defer_ok=*/false,
23626 /*expl_inst_class_mem_p=*/false);
23627
23628 if (COMPLETE_TYPE_P (instantiation))
23629 reconsider = 1;
23630 }
23631
23632 complete = COMPLETE_TYPE_P (instantiation);
23633 }
23634 else
23635 {
23636 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23637 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23638 {
23639 instantiation
23640 = instantiate_decl (instantiation,
23641 /*defer_ok=*/false,
23642 /*expl_inst_class_mem_p=*/false);
23643 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23644 reconsider = 1;
23645 }
23646
23647 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23648 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23649 }
23650
23651 if (complete)
23652 /* If INSTANTIATION has been instantiated, then we don't
23653 need to consider it again in the future. */
23654 *t = (*t)->next;
23655 else
23656 {
23657 last = *t;
23658 t = &(*t)->next;
23659 }
23660 tinst_depth = 0;
23661 current_tinst_level = NULL;
23662 }
23663 last_pending_template = last;
23664 }
23665 while (reconsider);
23666
23667 input_location = saved_loc;
23668 }
23669
23670 /* Substitute ARGVEC into T, which is a list of initializers for
23671 either base class or a non-static data member. The TREE_PURPOSEs
23672 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23673 instantiate_decl. */
23674
23675 static tree
23676 tsubst_initializer_list (tree t, tree argvec)
23677 {
23678 tree inits = NULL_TREE;
23679 tree target_ctor = error_mark_node;
23680
23681 for (; t; t = TREE_CHAIN (t))
23682 {
23683 tree decl;
23684 tree init;
23685 tree expanded_bases = NULL_TREE;
23686 tree expanded_arguments = NULL_TREE;
23687 int i, len = 1;
23688
23689 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23690 {
23691 tree expr;
23692 tree arg;
23693
23694 /* Expand the base class expansion type into separate base
23695 classes. */
23696 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23697 tf_warning_or_error,
23698 NULL_TREE);
23699 if (expanded_bases == error_mark_node)
23700 continue;
23701
23702 /* We'll be building separate TREE_LISTs of arguments for
23703 each base. */
23704 len = TREE_VEC_LENGTH (expanded_bases);
23705 expanded_arguments = make_tree_vec (len);
23706 for (i = 0; i < len; i++)
23707 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23708
23709 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23710 expand each argument in the TREE_VALUE of t. */
23711 expr = make_node (EXPR_PACK_EXPANSION);
23712 PACK_EXPANSION_LOCAL_P (expr) = true;
23713 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23714 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23715
23716 if (TREE_VALUE (t) == void_type_node)
23717 /* VOID_TYPE_NODE is used to indicate
23718 value-initialization. */
23719 {
23720 for (i = 0; i < len; i++)
23721 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23722 }
23723 else
23724 {
23725 /* Substitute parameter packs into each argument in the
23726 TREE_LIST. */
23727 in_base_initializer = 1;
23728 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23729 {
23730 tree expanded_exprs;
23731
23732 /* Expand the argument. */
23733 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23734 expanded_exprs
23735 = tsubst_pack_expansion (expr, argvec,
23736 tf_warning_or_error,
23737 NULL_TREE);
23738 if (expanded_exprs == error_mark_node)
23739 continue;
23740
23741 /* Prepend each of the expanded expressions to the
23742 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23743 for (i = 0; i < len; i++)
23744 {
23745 TREE_VEC_ELT (expanded_arguments, i) =
23746 tree_cons (NULL_TREE,
23747 TREE_VEC_ELT (expanded_exprs, i),
23748 TREE_VEC_ELT (expanded_arguments, i));
23749 }
23750 }
23751 in_base_initializer = 0;
23752
23753 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23754 since we built them backwards. */
23755 for (i = 0; i < len; i++)
23756 {
23757 TREE_VEC_ELT (expanded_arguments, i) =
23758 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23759 }
23760 }
23761 }
23762
23763 for (i = 0; i < len; ++i)
23764 {
23765 if (expanded_bases)
23766 {
23767 decl = TREE_VEC_ELT (expanded_bases, i);
23768 decl = expand_member_init (decl);
23769 init = TREE_VEC_ELT (expanded_arguments, i);
23770 }
23771 else
23772 {
23773 tree tmp;
23774 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23775 tf_warning_or_error, NULL_TREE);
23776
23777 decl = expand_member_init (decl);
23778 if (decl && !DECL_P (decl))
23779 in_base_initializer = 1;
23780
23781 init = TREE_VALUE (t);
23782 tmp = init;
23783 if (init != void_type_node)
23784 init = tsubst_expr (init, argvec,
23785 tf_warning_or_error, NULL_TREE,
23786 /*integral_constant_expression_p=*/false);
23787 if (init == NULL_TREE && tmp != NULL_TREE)
23788 /* If we had an initializer but it instantiated to nothing,
23789 value-initialize the object. This will only occur when
23790 the initializer was a pack expansion where the parameter
23791 packs used in that expansion were of length zero. */
23792 init = void_type_node;
23793 in_base_initializer = 0;
23794 }
23795
23796 if (target_ctor != error_mark_node
23797 && init != error_mark_node)
23798 {
23799 error ("mem-initializer for %qD follows constructor delegation",
23800 decl);
23801 return inits;
23802 }
23803 /* Look for a target constructor. */
23804 if (init != error_mark_node
23805 && decl && CLASS_TYPE_P (decl)
23806 && same_type_p (decl, current_class_type))
23807 {
23808 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
23809 if (inits)
23810 {
23811 error ("constructor delegation follows mem-initializer for %qD",
23812 TREE_PURPOSE (inits));
23813 continue;
23814 }
23815 target_ctor = init;
23816 }
23817
23818 if (decl)
23819 {
23820 init = build_tree_list (decl, init);
23821 TREE_CHAIN (init) = inits;
23822 inits = init;
23823 }
23824 }
23825 }
23826 return inits;
23827 }
23828
23829 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23830
23831 static void
23832 set_current_access_from_decl (tree decl)
23833 {
23834 if (TREE_PRIVATE (decl))
23835 current_access_specifier = access_private_node;
23836 else if (TREE_PROTECTED (decl))
23837 current_access_specifier = access_protected_node;
23838 else
23839 current_access_specifier = access_public_node;
23840 }
23841
23842 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23843 is the instantiation (which should have been created with
23844 start_enum) and ARGS are the template arguments to use. */
23845
23846 static void
23847 tsubst_enum (tree tag, tree newtag, tree args)
23848 {
23849 tree e;
23850
23851 if (SCOPED_ENUM_P (newtag))
23852 begin_scope (sk_scoped_enum, newtag);
23853
23854 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23855 {
23856 tree value;
23857 tree decl;
23858
23859 decl = TREE_VALUE (e);
23860 /* Note that in a template enum, the TREE_VALUE is the
23861 CONST_DECL, not the corresponding INTEGER_CST. */
23862 value = tsubst_expr (DECL_INITIAL (decl),
23863 args, tf_warning_or_error, NULL_TREE,
23864 /*integral_constant_expression_p=*/true);
23865
23866 /* Give this enumeration constant the correct access. */
23867 set_current_access_from_decl (decl);
23868
23869 /* Actually build the enumerator itself. Here we're assuming that
23870 enumerators can't have dependent attributes. */
23871 build_enumerator (DECL_NAME (decl), value, newtag,
23872 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23873 }
23874
23875 if (SCOPED_ENUM_P (newtag))
23876 finish_scope ();
23877
23878 finish_enum_value_list (newtag);
23879 finish_enum (newtag);
23880
23881 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23882 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23883 }
23884
23885 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23886 its type -- but without substituting the innermost set of template
23887 arguments. So, innermost set of template parameters will appear in
23888 the type. */
23889
23890 tree
23891 get_mostly_instantiated_function_type (tree decl)
23892 {
23893 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23894 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23895 }
23896
23897 /* Return truthvalue if we're processing a template different from
23898 the last one involved in diagnostics. */
23899 bool
23900 problematic_instantiation_changed (void)
23901 {
23902 return current_tinst_level != last_error_tinst_level;
23903 }
23904
23905 /* Remember current template involved in diagnostics. */
23906 void
23907 record_last_problematic_instantiation (void)
23908 {
23909 last_error_tinst_level = current_tinst_level;
23910 }
23911
23912 struct tinst_level *
23913 current_instantiation (void)
23914 {
23915 return current_tinst_level;
23916 }
23917
23918 /* Return TRUE if current_function_decl is being instantiated, false
23919 otherwise. */
23920
23921 bool
23922 instantiating_current_function_p (void)
23923 {
23924 return (current_instantiation ()
23925 && current_instantiation ()->decl == current_function_decl);
23926 }
23927
23928 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23929 type. Return false for ok, true for disallowed. Issue error and
23930 inform messages under control of COMPLAIN. */
23931
23932 static bool
23933 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23934 {
23935 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23936 return false;
23937 else if (POINTER_TYPE_P (type))
23938 return false;
23939 else if (TYPE_PTRMEM_P (type))
23940 return false;
23941 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23942 return false;
23943 else if (TREE_CODE (type) == TYPENAME_TYPE)
23944 return false;
23945 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23946 return false;
23947 else if (TREE_CODE (type) == NULLPTR_TYPE)
23948 return false;
23949 /* A bound template template parm could later be instantiated to have a valid
23950 nontype parm type via an alias template. */
23951 else if (cxx_dialect >= cxx11
23952 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23953 return false;
23954
23955 if (complain & tf_error)
23956 {
23957 if (type == error_mark_node)
23958 inform (input_location, "invalid template non-type parameter");
23959 else
23960 error ("%q#T is not a valid type for a template non-type parameter",
23961 type);
23962 }
23963 return true;
23964 }
23965
23966 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23967 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23968
23969 static bool
23970 dependent_type_p_r (tree type)
23971 {
23972 tree scope;
23973
23974 /* [temp.dep.type]
23975
23976 A type is dependent if it is:
23977
23978 -- a template parameter. Template template parameters are types
23979 for us (since TYPE_P holds true for them) so we handle
23980 them here. */
23981 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23982 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23983 return true;
23984 /* -- a qualified-id with a nested-name-specifier which contains a
23985 class-name that names a dependent type or whose unqualified-id
23986 names a dependent type. */
23987 if (TREE_CODE (type) == TYPENAME_TYPE)
23988 return true;
23989
23990 /* An alias template specialization can be dependent even if the
23991 resulting type is not. */
23992 if (dependent_alias_template_spec_p (type))
23993 return true;
23994
23995 /* -- a cv-qualified type where the cv-unqualified type is
23996 dependent.
23997 No code is necessary for this bullet; the code below handles
23998 cv-qualified types, and we don't want to strip aliases with
23999 TYPE_MAIN_VARIANT because of DR 1558. */
24000 /* -- a compound type constructed from any dependent type. */
24001 if (TYPE_PTRMEM_P (type))
24002 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
24003 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24004 (type)));
24005 else if (TYPE_PTR_P (type)
24006 || TREE_CODE (type) == REFERENCE_TYPE)
24007 return dependent_type_p (TREE_TYPE (type));
24008 else if (TREE_CODE (type) == FUNCTION_TYPE
24009 || TREE_CODE (type) == METHOD_TYPE)
24010 {
24011 tree arg_type;
24012
24013 if (dependent_type_p (TREE_TYPE (type)))
24014 return true;
24015 for (arg_type = TYPE_ARG_TYPES (type);
24016 arg_type;
24017 arg_type = TREE_CHAIN (arg_type))
24018 if (dependent_type_p (TREE_VALUE (arg_type)))
24019 return true;
24020 if (cxx_dialect >= cxx17)
24021 /* A value-dependent noexcept-specifier makes the type dependent. */
24022 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
24023 if (tree noex = TREE_PURPOSE (spec))
24024 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24025 affect overload resolution and treating it as dependent breaks
24026 things. */
24027 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
24028 && value_dependent_expression_p (noex))
24029 return true;
24030 return false;
24031 }
24032 /* -- an array type constructed from any dependent type or whose
24033 size is specified by a constant expression that is
24034 value-dependent.
24035
24036 We checked for type- and value-dependence of the bounds in
24037 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24038 if (TREE_CODE (type) == ARRAY_TYPE)
24039 {
24040 if (TYPE_DOMAIN (type)
24041 && dependent_type_p (TYPE_DOMAIN (type)))
24042 return true;
24043 return dependent_type_p (TREE_TYPE (type));
24044 }
24045
24046 /* -- a template-id in which either the template name is a template
24047 parameter ... */
24048 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24049 return true;
24050 /* ... or any of the template arguments is a dependent type or
24051 an expression that is type-dependent or value-dependent. */
24052 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
24053 && (any_dependent_template_arguments_p
24054 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
24055 return true;
24056
24057 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24058 dependent; if the argument of the `typeof' expression is not
24059 type-dependent, then it should already been have resolved. */
24060 if (TREE_CODE (type) == TYPEOF_TYPE
24061 || TREE_CODE (type) == DECLTYPE_TYPE
24062 || TREE_CODE (type) == UNDERLYING_TYPE)
24063 return true;
24064
24065 /* A template argument pack is dependent if any of its packed
24066 arguments are. */
24067 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
24068 {
24069 tree args = ARGUMENT_PACK_ARGS (type);
24070 int i, len = TREE_VEC_LENGTH (args);
24071 for (i = 0; i < len; ++i)
24072 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24073 return true;
24074 }
24075
24076 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24077 be template parameters. */
24078 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
24079 return true;
24080
24081 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
24082 return true;
24083
24084 /* The standard does not specifically mention types that are local
24085 to template functions or local classes, but they should be
24086 considered dependent too. For example:
24087
24088 template <int I> void f() {
24089 enum E { a = I };
24090 S<sizeof (E)> s;
24091 }
24092
24093 The size of `E' cannot be known until the value of `I' has been
24094 determined. Therefore, `E' must be considered dependent. */
24095 scope = TYPE_CONTEXT (type);
24096 if (scope && TYPE_P (scope))
24097 return dependent_type_p (scope);
24098 /* Don't use type_dependent_expression_p here, as it can lead
24099 to infinite recursion trying to determine whether a lambda
24100 nested in a lambda is dependent (c++/47687). */
24101 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
24102 && DECL_LANG_SPECIFIC (scope)
24103 && DECL_TEMPLATE_INFO (scope)
24104 && (any_dependent_template_arguments_p
24105 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
24106 return true;
24107
24108 /* Other types are non-dependent. */
24109 return false;
24110 }
24111
24112 /* Returns TRUE if TYPE is dependent, in the sense of
24113 [temp.dep.type]. Note that a NULL type is considered dependent. */
24114
24115 bool
24116 dependent_type_p (tree type)
24117 {
24118 /* If there are no template parameters in scope, then there can't be
24119 any dependent types. */
24120 if (!processing_template_decl)
24121 {
24122 /* If we are not processing a template, then nobody should be
24123 providing us with a dependent type. */
24124 gcc_assert (type);
24125 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
24126 return false;
24127 }
24128
24129 /* If the type is NULL, we have not computed a type for the entity
24130 in question; in that case, the type is dependent. */
24131 if (!type)
24132 return true;
24133
24134 /* Erroneous types can be considered non-dependent. */
24135 if (type == error_mark_node)
24136 return false;
24137
24138 /* Getting here with global_type_node means we improperly called this
24139 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24140 gcc_checking_assert (type != global_type_node);
24141
24142 /* If we have not already computed the appropriate value for TYPE,
24143 do so now. */
24144 if (!TYPE_DEPENDENT_P_VALID (type))
24145 {
24146 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
24147 TYPE_DEPENDENT_P_VALID (type) = 1;
24148 }
24149
24150 return TYPE_DEPENDENT_P (type);
24151 }
24152
24153 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24154 lookup. In other words, a dependent type that is not the current
24155 instantiation. */
24156
24157 bool
24158 dependent_scope_p (tree scope)
24159 {
24160 return (scope && TYPE_P (scope) && dependent_type_p (scope)
24161 && !currently_open_class (scope));
24162 }
24163
24164 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24165 an unknown base of 'this' (and is therefore instantiation-dependent). */
24166
24167 static bool
24168 unknown_base_ref_p (tree t)
24169 {
24170 if (!current_class_ptr)
24171 return false;
24172
24173 tree mem = TREE_OPERAND (t, 1);
24174 if (shared_member_p (mem))
24175 return false;
24176
24177 tree cur = current_nonlambda_class_type ();
24178 if (!any_dependent_bases_p (cur))
24179 return false;
24180
24181 tree ctx = TREE_OPERAND (t, 0);
24182 if (DERIVED_FROM_P (ctx, cur))
24183 return false;
24184
24185 return true;
24186 }
24187
24188 /* T is a SCOPE_REF; return whether we need to consider it
24189 instantiation-dependent so that we can check access at instantiation
24190 time even though we know which member it resolves to. */
24191
24192 static bool
24193 instantiation_dependent_scope_ref_p (tree t)
24194 {
24195 if (DECL_P (TREE_OPERAND (t, 1))
24196 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
24197 && !unknown_base_ref_p (t)
24198 && accessible_in_template_p (TREE_OPERAND (t, 0),
24199 TREE_OPERAND (t, 1)))
24200 return false;
24201 else
24202 return true;
24203 }
24204
24205 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24206 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24207 expression. */
24208
24209 /* Note that this predicate is not appropriate for general expressions;
24210 only constant expressions (that satisfy potential_constant_expression)
24211 can be tested for value dependence. */
24212
24213 bool
24214 value_dependent_expression_p (tree expression)
24215 {
24216 if (!processing_template_decl || expression == NULL_TREE)
24217 return false;
24218
24219 /* A type-dependent expression is also value-dependent. */
24220 if (type_dependent_expression_p (expression))
24221 return true;
24222
24223 switch (TREE_CODE (expression))
24224 {
24225 case BASELINK:
24226 /* A dependent member function of the current instantiation. */
24227 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24228
24229 case FUNCTION_DECL:
24230 /* A dependent member function of the current instantiation. */
24231 if (DECL_CLASS_SCOPE_P (expression)
24232 && dependent_type_p (DECL_CONTEXT (expression)))
24233 return true;
24234 break;
24235
24236 case IDENTIFIER_NODE:
24237 /* A name that has not been looked up -- must be dependent. */
24238 return true;
24239
24240 case TEMPLATE_PARM_INDEX:
24241 /* A non-type template parm. */
24242 return true;
24243
24244 case CONST_DECL:
24245 /* A non-type template parm. */
24246 if (DECL_TEMPLATE_PARM_P (expression))
24247 return true;
24248 return value_dependent_expression_p (DECL_INITIAL (expression));
24249
24250 case VAR_DECL:
24251 /* A constant with literal type and is initialized
24252 with an expression that is value-dependent. */
24253 if (DECL_DEPENDENT_INIT_P (expression)
24254 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24255 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
24256 return true;
24257 if (DECL_HAS_VALUE_EXPR_P (expression))
24258 {
24259 tree value_expr = DECL_VALUE_EXPR (expression);
24260 if (value_dependent_expression_p (value_expr))
24261 return true;
24262 }
24263 return false;
24264
24265 case DYNAMIC_CAST_EXPR:
24266 case STATIC_CAST_EXPR:
24267 case CONST_CAST_EXPR:
24268 case REINTERPRET_CAST_EXPR:
24269 case CAST_EXPR:
24270 case IMPLICIT_CONV_EXPR:
24271 /* These expressions are value-dependent if the type to which
24272 the cast occurs is dependent or the expression being casted
24273 is value-dependent. */
24274 {
24275 tree type = TREE_TYPE (expression);
24276
24277 if (dependent_type_p (type))
24278 return true;
24279
24280 /* A functional cast has a list of operands. */
24281 expression = TREE_OPERAND (expression, 0);
24282 if (!expression)
24283 {
24284 /* If there are no operands, it must be an expression such
24285 as "int()". This should not happen for aggregate types
24286 because it would form non-constant expressions. */
24287 gcc_assert (cxx_dialect >= cxx11
24288 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24289
24290 return false;
24291 }
24292
24293 if (TREE_CODE (expression) == TREE_LIST)
24294 return any_value_dependent_elements_p (expression);
24295
24296 return value_dependent_expression_p (expression);
24297 }
24298
24299 case SIZEOF_EXPR:
24300 if (SIZEOF_EXPR_TYPE_P (expression))
24301 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24302 /* FALLTHRU */
24303 case ALIGNOF_EXPR:
24304 case TYPEID_EXPR:
24305 /* A `sizeof' expression is value-dependent if the operand is
24306 type-dependent or is a pack expansion. */
24307 expression = TREE_OPERAND (expression, 0);
24308 if (PACK_EXPANSION_P (expression))
24309 return true;
24310 else if (TYPE_P (expression))
24311 return dependent_type_p (expression);
24312 return instantiation_dependent_uneval_expression_p (expression);
24313
24314 case AT_ENCODE_EXPR:
24315 /* An 'encode' expression is value-dependent if the operand is
24316 type-dependent. */
24317 expression = TREE_OPERAND (expression, 0);
24318 return dependent_type_p (expression);
24319
24320 case NOEXCEPT_EXPR:
24321 expression = TREE_OPERAND (expression, 0);
24322 return instantiation_dependent_uneval_expression_p (expression);
24323
24324 case SCOPE_REF:
24325 /* All instantiation-dependent expressions should also be considered
24326 value-dependent. */
24327 return instantiation_dependent_scope_ref_p (expression);
24328
24329 case COMPONENT_REF:
24330 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24331 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24332
24333 case NONTYPE_ARGUMENT_PACK:
24334 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24335 is value-dependent. */
24336 {
24337 tree values = ARGUMENT_PACK_ARGS (expression);
24338 int i, len = TREE_VEC_LENGTH (values);
24339
24340 for (i = 0; i < len; ++i)
24341 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24342 return true;
24343
24344 return false;
24345 }
24346
24347 case TRAIT_EXPR:
24348 {
24349 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24350
24351 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24352 return true;
24353
24354 if (!type2)
24355 return false;
24356
24357 if (TREE_CODE (type2) != TREE_LIST)
24358 return dependent_type_p (type2);
24359
24360 for (; type2; type2 = TREE_CHAIN (type2))
24361 if (dependent_type_p (TREE_VALUE (type2)))
24362 return true;
24363
24364 return false;
24365 }
24366
24367 case MODOP_EXPR:
24368 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24369 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24370
24371 case ARRAY_REF:
24372 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24373 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24374
24375 case ADDR_EXPR:
24376 {
24377 tree op = TREE_OPERAND (expression, 0);
24378 return (value_dependent_expression_p (op)
24379 || has_value_dependent_address (op));
24380 }
24381
24382 case REQUIRES_EXPR:
24383 /* Treat all requires-expressions as value-dependent so
24384 we don't try to fold them. */
24385 return true;
24386
24387 case TYPE_REQ:
24388 return dependent_type_p (TREE_OPERAND (expression, 0));
24389
24390 case CALL_EXPR:
24391 {
24392 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24393 return true;
24394 tree fn = get_callee_fndecl (expression);
24395 int i, nargs;
24396 nargs = call_expr_nargs (expression);
24397 for (i = 0; i < nargs; ++i)
24398 {
24399 tree op = CALL_EXPR_ARG (expression, i);
24400 /* In a call to a constexpr member function, look through the
24401 implicit ADDR_EXPR on the object argument so that it doesn't
24402 cause the call to be considered value-dependent. We also
24403 look through it in potential_constant_expression. */
24404 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24405 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24406 && TREE_CODE (op) == ADDR_EXPR)
24407 op = TREE_OPERAND (op, 0);
24408 if (value_dependent_expression_p (op))
24409 return true;
24410 }
24411 return false;
24412 }
24413
24414 case TEMPLATE_ID_EXPR:
24415 return variable_concept_p (TREE_OPERAND (expression, 0));
24416
24417 case CONSTRUCTOR:
24418 {
24419 unsigned ix;
24420 tree val;
24421 if (dependent_type_p (TREE_TYPE (expression)))
24422 return true;
24423 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24424 if (value_dependent_expression_p (val))
24425 return true;
24426 return false;
24427 }
24428
24429 case STMT_EXPR:
24430 /* Treat a GNU statement expression as dependent to avoid crashing
24431 under instantiate_non_dependent_expr; it can't be constant. */
24432 return true;
24433
24434 default:
24435 /* A constant expression is value-dependent if any subexpression is
24436 value-dependent. */
24437 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24438 {
24439 case tcc_reference:
24440 case tcc_unary:
24441 case tcc_comparison:
24442 case tcc_binary:
24443 case tcc_expression:
24444 case tcc_vl_exp:
24445 {
24446 int i, len = cp_tree_operand_length (expression);
24447
24448 for (i = 0; i < len; i++)
24449 {
24450 tree t = TREE_OPERAND (expression, i);
24451
24452 /* In some cases, some of the operands may be missing.
24453 (For example, in the case of PREDECREMENT_EXPR, the
24454 amount to increment by may be missing.) That doesn't
24455 make the expression dependent. */
24456 if (t && value_dependent_expression_p (t))
24457 return true;
24458 }
24459 }
24460 break;
24461 default:
24462 break;
24463 }
24464 break;
24465 }
24466
24467 /* The expression is not value-dependent. */
24468 return false;
24469 }
24470
24471 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24472 [temp.dep.expr]. Note that an expression with no type is
24473 considered dependent. Other parts of the compiler arrange for an
24474 expression with type-dependent subexpressions to have no type, so
24475 this function doesn't have to be fully recursive. */
24476
24477 bool
24478 type_dependent_expression_p (tree expression)
24479 {
24480 if (!processing_template_decl)
24481 return false;
24482
24483 if (expression == NULL_TREE || expression == error_mark_node)
24484 return false;
24485
24486 STRIP_ANY_LOCATION_WRAPPER (expression);
24487
24488 /* An unresolved name is always dependent. */
24489 if (identifier_p (expression)
24490 || TREE_CODE (expression) == USING_DECL
24491 || TREE_CODE (expression) == WILDCARD_DECL)
24492 return true;
24493
24494 /* A fold expression is type-dependent. */
24495 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24496 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24497 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24498 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24499 return true;
24500
24501 /* Some expression forms are never type-dependent. */
24502 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24503 || TREE_CODE (expression) == SIZEOF_EXPR
24504 || TREE_CODE (expression) == ALIGNOF_EXPR
24505 || TREE_CODE (expression) == AT_ENCODE_EXPR
24506 || TREE_CODE (expression) == NOEXCEPT_EXPR
24507 || TREE_CODE (expression) == TRAIT_EXPR
24508 || TREE_CODE (expression) == TYPEID_EXPR
24509 || TREE_CODE (expression) == DELETE_EXPR
24510 || TREE_CODE (expression) == VEC_DELETE_EXPR
24511 || TREE_CODE (expression) == THROW_EXPR
24512 || TREE_CODE (expression) == REQUIRES_EXPR)
24513 return false;
24514
24515 /* The types of these expressions depends only on the type to which
24516 the cast occurs. */
24517 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24518 || TREE_CODE (expression) == STATIC_CAST_EXPR
24519 || TREE_CODE (expression) == CONST_CAST_EXPR
24520 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24521 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24522 || TREE_CODE (expression) == CAST_EXPR)
24523 return dependent_type_p (TREE_TYPE (expression));
24524
24525 /* The types of these expressions depends only on the type created
24526 by the expression. */
24527 if (TREE_CODE (expression) == NEW_EXPR
24528 || TREE_CODE (expression) == VEC_NEW_EXPR)
24529 {
24530 /* For NEW_EXPR tree nodes created inside a template, either
24531 the object type itself or a TREE_LIST may appear as the
24532 operand 1. */
24533 tree type = TREE_OPERAND (expression, 1);
24534 if (TREE_CODE (type) == TREE_LIST)
24535 /* This is an array type. We need to check array dimensions
24536 as well. */
24537 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24538 || value_dependent_expression_p
24539 (TREE_OPERAND (TREE_VALUE (type), 1));
24540 else
24541 return dependent_type_p (type);
24542 }
24543
24544 if (TREE_CODE (expression) == SCOPE_REF)
24545 {
24546 tree scope = TREE_OPERAND (expression, 0);
24547 tree name = TREE_OPERAND (expression, 1);
24548
24549 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24550 contains an identifier associated by name lookup with one or more
24551 declarations declared with a dependent type, or...a
24552 nested-name-specifier or qualified-id that names a member of an
24553 unknown specialization. */
24554 return (type_dependent_expression_p (name)
24555 || dependent_scope_p (scope));
24556 }
24557
24558 if (TREE_CODE (expression) == TEMPLATE_DECL
24559 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24560 return uses_outer_template_parms (expression);
24561
24562 if (TREE_CODE (expression) == STMT_EXPR)
24563 expression = stmt_expr_value_expr (expression);
24564
24565 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24566 {
24567 tree elt;
24568 unsigned i;
24569
24570 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24571 {
24572 if (type_dependent_expression_p (elt))
24573 return true;
24574 }
24575 return false;
24576 }
24577
24578 /* A static data member of the current instantiation with incomplete
24579 array type is type-dependent, as the definition and specializations
24580 can have different bounds. */
24581 if (VAR_P (expression)
24582 && DECL_CLASS_SCOPE_P (expression)
24583 && dependent_type_p (DECL_CONTEXT (expression))
24584 && VAR_HAD_UNKNOWN_BOUND (expression))
24585 return true;
24586
24587 /* An array of unknown bound depending on a variadic parameter, eg:
24588
24589 template<typename... Args>
24590 void foo (Args... args)
24591 {
24592 int arr[] = { args... };
24593 }
24594
24595 template<int... vals>
24596 void bar ()
24597 {
24598 int arr[] = { vals... };
24599 }
24600
24601 If the array has no length and has an initializer, it must be that
24602 we couldn't determine its length in cp_complete_array_type because
24603 it is dependent. */
24604 if (VAR_P (expression)
24605 && TREE_TYPE (expression) != NULL_TREE
24606 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24607 && !TYPE_DOMAIN (TREE_TYPE (expression))
24608 && DECL_INITIAL (expression))
24609 return true;
24610
24611 /* A function or variable template-id is type-dependent if it has any
24612 dependent template arguments. */
24613 if (VAR_OR_FUNCTION_DECL_P (expression)
24614 && DECL_LANG_SPECIFIC (expression)
24615 && DECL_TEMPLATE_INFO (expression))
24616 {
24617 /* Consider the innermost template arguments, since those are the ones
24618 that come from the template-id; the template arguments for the
24619 enclosing class do not make it type-dependent unless they are used in
24620 the type of the decl. */
24621 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24622 && (any_dependent_template_arguments_p
24623 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24624 return true;
24625 }
24626
24627 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24628 type-dependent. Checking this is important for functions with auto return
24629 type, which looks like a dependent type. */
24630 if (TREE_CODE (expression) == FUNCTION_DECL
24631 && !(DECL_CLASS_SCOPE_P (expression)
24632 && dependent_type_p (DECL_CONTEXT (expression)))
24633 && !(DECL_FRIEND_P (expression)
24634 && (!DECL_FRIEND_CONTEXT (expression)
24635 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24636 && !DECL_LOCAL_FUNCTION_P (expression))
24637 {
24638 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24639 || undeduced_auto_decl (expression));
24640 return false;
24641 }
24642
24643 /* Always dependent, on the number of arguments if nothing else. */
24644 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24645 return true;
24646
24647 if (TREE_TYPE (expression) == unknown_type_node)
24648 {
24649 if (TREE_CODE (expression) == ADDR_EXPR)
24650 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24651 if (TREE_CODE (expression) == COMPONENT_REF
24652 || TREE_CODE (expression) == OFFSET_REF)
24653 {
24654 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24655 return true;
24656 expression = TREE_OPERAND (expression, 1);
24657 if (identifier_p (expression))
24658 return false;
24659 }
24660 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24661 if (TREE_CODE (expression) == SCOPE_REF)
24662 return false;
24663
24664 if (BASELINK_P (expression))
24665 {
24666 if (BASELINK_OPTYPE (expression)
24667 && dependent_type_p (BASELINK_OPTYPE (expression)))
24668 return true;
24669 expression = BASELINK_FUNCTIONS (expression);
24670 }
24671
24672 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24673 {
24674 if (any_dependent_template_arguments_p
24675 (TREE_OPERAND (expression, 1)))
24676 return true;
24677 expression = TREE_OPERAND (expression, 0);
24678 if (identifier_p (expression))
24679 return true;
24680 }
24681
24682 gcc_assert (TREE_CODE (expression) == OVERLOAD
24683 || TREE_CODE (expression) == FUNCTION_DECL);
24684
24685 for (lkp_iterator iter (expression); iter; ++iter)
24686 if (type_dependent_expression_p (*iter))
24687 return true;
24688
24689 return false;
24690 }
24691
24692 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24693
24694 /* Dependent type attributes might not have made it from the decl to
24695 the type yet. */
24696 if (DECL_P (expression)
24697 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24698 return true;
24699
24700 return (dependent_type_p (TREE_TYPE (expression)));
24701 }
24702
24703 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24704 type-dependent if the expression refers to a member of the current
24705 instantiation and the type of the referenced member is dependent, or the
24706 class member access expression refers to a member of an unknown
24707 specialization.
24708
24709 This function returns true if the OBJECT in such a class member access
24710 expression is of an unknown specialization. */
24711
24712 bool
24713 type_dependent_object_expression_p (tree object)
24714 {
24715 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24716 dependent. */
24717 if (TREE_CODE (object) == IDENTIFIER_NODE)
24718 return true;
24719 tree scope = TREE_TYPE (object);
24720 return (!scope || dependent_scope_p (scope));
24721 }
24722
24723 /* walk_tree callback function for instantiation_dependent_expression_p,
24724 below. Returns non-zero if a dependent subexpression is found. */
24725
24726 static tree
24727 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24728 void * /*data*/)
24729 {
24730 if (TYPE_P (*tp))
24731 {
24732 /* We don't have to worry about decltype currently because decltype
24733 of an instantiation-dependent expr is a dependent type. This
24734 might change depending on the resolution of DR 1172. */
24735 *walk_subtrees = false;
24736 return NULL_TREE;
24737 }
24738 enum tree_code code = TREE_CODE (*tp);
24739 switch (code)
24740 {
24741 /* Don't treat an argument list as dependent just because it has no
24742 TREE_TYPE. */
24743 case TREE_LIST:
24744 case TREE_VEC:
24745 return NULL_TREE;
24746
24747 case TEMPLATE_PARM_INDEX:
24748 return *tp;
24749
24750 /* Handle expressions with type operands. */
24751 case SIZEOF_EXPR:
24752 case ALIGNOF_EXPR:
24753 case TYPEID_EXPR:
24754 case AT_ENCODE_EXPR:
24755 {
24756 tree op = TREE_OPERAND (*tp, 0);
24757 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24758 op = TREE_TYPE (op);
24759 if (TYPE_P (op))
24760 {
24761 if (dependent_type_p (op))
24762 return *tp;
24763 else
24764 {
24765 *walk_subtrees = false;
24766 return NULL_TREE;
24767 }
24768 }
24769 break;
24770 }
24771
24772 case COMPONENT_REF:
24773 if (identifier_p (TREE_OPERAND (*tp, 1)))
24774 /* In a template, finish_class_member_access_expr creates a
24775 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24776 type-dependent, so that we can check access control at
24777 instantiation time (PR 42277). See also Core issue 1273. */
24778 return *tp;
24779 break;
24780
24781 case SCOPE_REF:
24782 if (instantiation_dependent_scope_ref_p (*tp))
24783 return *tp;
24784 else
24785 break;
24786
24787 /* Treat statement-expressions as dependent. */
24788 case BIND_EXPR:
24789 return *tp;
24790
24791 /* Treat requires-expressions as dependent. */
24792 case REQUIRES_EXPR:
24793 return *tp;
24794
24795 case CALL_EXPR:
24796 /* Treat calls to function concepts as dependent. */
24797 if (function_concept_check_p (*tp))
24798 return *tp;
24799 break;
24800
24801 case TEMPLATE_ID_EXPR:
24802 /* And variable concepts. */
24803 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24804 return *tp;
24805 break;
24806
24807 default:
24808 break;
24809 }
24810
24811 if (type_dependent_expression_p (*tp))
24812 return *tp;
24813 else
24814 return NULL_TREE;
24815 }
24816
24817 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24818 sense defined by the ABI:
24819
24820 "An expression is instantiation-dependent if it is type-dependent
24821 or value-dependent, or it has a subexpression that is type-dependent
24822 or value-dependent."
24823
24824 Except don't actually check value-dependence for unevaluated expressions,
24825 because in sizeof(i) we don't care about the value of i. Checking
24826 type-dependence will in turn check value-dependence of array bounds/template
24827 arguments as needed. */
24828
24829 bool
24830 instantiation_dependent_uneval_expression_p (tree expression)
24831 {
24832 tree result;
24833
24834 if (!processing_template_decl)
24835 return false;
24836
24837 if (expression == error_mark_node)
24838 return false;
24839
24840 result = cp_walk_tree_without_duplicates (&expression,
24841 instantiation_dependent_r, NULL);
24842 return result != NULL_TREE;
24843 }
24844
24845 /* As above, but also check value-dependence of the expression as a whole. */
24846
24847 bool
24848 instantiation_dependent_expression_p (tree expression)
24849 {
24850 return (instantiation_dependent_uneval_expression_p (expression)
24851 || value_dependent_expression_p (expression));
24852 }
24853
24854 /* Like type_dependent_expression_p, but it also works while not processing
24855 a template definition, i.e. during substitution or mangling. */
24856
24857 bool
24858 type_dependent_expression_p_push (tree expr)
24859 {
24860 bool b;
24861 ++processing_template_decl;
24862 b = type_dependent_expression_p (expr);
24863 --processing_template_decl;
24864 return b;
24865 }
24866
24867 /* Returns TRUE if ARGS contains a type-dependent expression. */
24868
24869 bool
24870 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24871 {
24872 unsigned int i;
24873 tree arg;
24874
24875 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24876 {
24877 if (type_dependent_expression_p (arg))
24878 return true;
24879 }
24880 return false;
24881 }
24882
24883 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24884 expressions) contains any type-dependent expressions. */
24885
24886 bool
24887 any_type_dependent_elements_p (const_tree list)
24888 {
24889 for (; list; list = TREE_CHAIN (list))
24890 if (type_dependent_expression_p (TREE_VALUE (list)))
24891 return true;
24892
24893 return false;
24894 }
24895
24896 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24897 expressions) contains any value-dependent expressions. */
24898
24899 bool
24900 any_value_dependent_elements_p (const_tree list)
24901 {
24902 for (; list; list = TREE_CHAIN (list))
24903 if (value_dependent_expression_p (TREE_VALUE (list)))
24904 return true;
24905
24906 return false;
24907 }
24908
24909 /* Returns TRUE if the ARG (a template argument) is dependent. */
24910
24911 bool
24912 dependent_template_arg_p (tree arg)
24913 {
24914 if (!processing_template_decl)
24915 return false;
24916
24917 /* Assume a template argument that was wrongly written by the user
24918 is dependent. This is consistent with what
24919 any_dependent_template_arguments_p [that calls this function]
24920 does. */
24921 if (!arg || arg == error_mark_node)
24922 return true;
24923
24924 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24925 arg = argument_pack_select_arg (arg);
24926
24927 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24928 return true;
24929 if (TREE_CODE (arg) == TEMPLATE_DECL)
24930 {
24931 if (DECL_TEMPLATE_PARM_P (arg))
24932 return true;
24933 /* A member template of a dependent class is not necessarily
24934 type-dependent, but it is a dependent template argument because it
24935 will be a member of an unknown specialization to that template. */
24936 tree scope = CP_DECL_CONTEXT (arg);
24937 return TYPE_P (scope) && dependent_type_p (scope);
24938 }
24939 else if (ARGUMENT_PACK_P (arg))
24940 {
24941 tree args = ARGUMENT_PACK_ARGS (arg);
24942 int i, len = TREE_VEC_LENGTH (args);
24943 for (i = 0; i < len; ++i)
24944 {
24945 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24946 return true;
24947 }
24948
24949 return false;
24950 }
24951 else if (TYPE_P (arg))
24952 return dependent_type_p (arg);
24953 else
24954 return (type_dependent_expression_p (arg)
24955 || value_dependent_expression_p (arg));
24956 }
24957
24958 /* Returns true if ARGS (a collection of template arguments) contains
24959 any types that require structural equality testing. */
24960
24961 bool
24962 any_template_arguments_need_structural_equality_p (tree args)
24963 {
24964 int i;
24965 int j;
24966
24967 if (!args)
24968 return false;
24969 if (args == error_mark_node)
24970 return true;
24971
24972 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24973 {
24974 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24975 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24976 {
24977 tree arg = TREE_VEC_ELT (level, j);
24978 tree packed_args = NULL_TREE;
24979 int k, len = 1;
24980
24981 if (ARGUMENT_PACK_P (arg))
24982 {
24983 /* Look inside the argument pack. */
24984 packed_args = ARGUMENT_PACK_ARGS (arg);
24985 len = TREE_VEC_LENGTH (packed_args);
24986 }
24987
24988 for (k = 0; k < len; ++k)
24989 {
24990 if (packed_args)
24991 arg = TREE_VEC_ELT (packed_args, k);
24992
24993 if (error_operand_p (arg))
24994 return true;
24995 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24996 continue;
24997 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24998 return true;
24999 else if (!TYPE_P (arg) && TREE_TYPE (arg)
25000 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
25001 return true;
25002 }
25003 }
25004 }
25005
25006 return false;
25007 }
25008
25009 /* Returns true if ARGS (a collection of template arguments) contains
25010 any dependent arguments. */
25011
25012 bool
25013 any_dependent_template_arguments_p (const_tree args)
25014 {
25015 int i;
25016 int j;
25017
25018 if (!args)
25019 return false;
25020 if (args == error_mark_node)
25021 return true;
25022
25023 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25024 {
25025 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25026 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25027 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
25028 return true;
25029 }
25030
25031 return false;
25032 }
25033
25034 /* Returns TRUE if the template TMPL is type-dependent. */
25035
25036 bool
25037 dependent_template_p (tree tmpl)
25038 {
25039 if (TREE_CODE (tmpl) == OVERLOAD)
25040 {
25041 for (lkp_iterator iter (tmpl); iter; ++iter)
25042 if (dependent_template_p (*iter))
25043 return true;
25044 return false;
25045 }
25046
25047 /* Template template parameters are dependent. */
25048 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
25049 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
25050 return true;
25051 /* So are names that have not been looked up. */
25052 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
25053 return true;
25054 return false;
25055 }
25056
25057 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25058
25059 bool
25060 dependent_template_id_p (tree tmpl, tree args)
25061 {
25062 return (dependent_template_p (tmpl)
25063 || any_dependent_template_arguments_p (args));
25064 }
25065
25066 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25067 are dependent. */
25068
25069 bool
25070 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
25071 {
25072 int i;
25073
25074 if (!processing_template_decl)
25075 return false;
25076
25077 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
25078 {
25079 tree decl = TREE_VEC_ELT (declv, i);
25080 tree init = TREE_VEC_ELT (initv, i);
25081 tree cond = TREE_VEC_ELT (condv, i);
25082 tree incr = TREE_VEC_ELT (incrv, i);
25083
25084 if (type_dependent_expression_p (decl)
25085 || TREE_CODE (decl) == SCOPE_REF)
25086 return true;
25087
25088 if (init && type_dependent_expression_p (init))
25089 return true;
25090
25091 if (type_dependent_expression_p (cond))
25092 return true;
25093
25094 if (COMPARISON_CLASS_P (cond)
25095 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
25096 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
25097 return true;
25098
25099 if (TREE_CODE (incr) == MODOP_EXPR)
25100 {
25101 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
25102 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
25103 return true;
25104 }
25105 else if (type_dependent_expression_p (incr))
25106 return true;
25107 else if (TREE_CODE (incr) == MODIFY_EXPR)
25108 {
25109 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
25110 return true;
25111 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
25112 {
25113 tree t = TREE_OPERAND (incr, 1);
25114 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
25115 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
25116 return true;
25117 }
25118 }
25119 }
25120
25121 return false;
25122 }
25123
25124 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25125 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25126 no such TYPE can be found. Note that this function peers inside
25127 uninstantiated templates and therefore should be used only in
25128 extremely limited situations. ONLY_CURRENT_P restricts this
25129 peering to the currently open classes hierarchy (which is required
25130 when comparing types). */
25131
25132 tree
25133 resolve_typename_type (tree type, bool only_current_p)
25134 {
25135 tree scope;
25136 tree name;
25137 tree decl;
25138 int quals;
25139 tree pushed_scope;
25140 tree result;
25141
25142 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
25143
25144 scope = TYPE_CONTEXT (type);
25145 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25146 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25147 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25148 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25149 identifier of the TYPENAME_TYPE anymore.
25150 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25151 TYPENAME_TYPE instead, we avoid messing up with a possible
25152 typedef variant case. */
25153 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
25154
25155 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25156 it first before we can figure out what NAME refers to. */
25157 if (TREE_CODE (scope) == TYPENAME_TYPE)
25158 {
25159 if (TYPENAME_IS_RESOLVING_P (scope))
25160 /* Given a class template A with a dependent base with nested type C,
25161 typedef typename A::C::C C will land us here, as trying to resolve
25162 the initial A::C leads to the local C typedef, which leads back to
25163 A::C::C. So we break the recursion now. */
25164 return type;
25165 else
25166 scope = resolve_typename_type (scope, only_current_p);
25167 }
25168 /* If we don't know what SCOPE refers to, then we cannot resolve the
25169 TYPENAME_TYPE. */
25170 if (!CLASS_TYPE_P (scope))
25171 return type;
25172 /* If this is a typedef, we don't want to look inside (c++/11987). */
25173 if (typedef_variant_p (type))
25174 return type;
25175 /* If SCOPE isn't the template itself, it will not have a valid
25176 TYPE_FIELDS list. */
25177 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
25178 /* scope is either the template itself or a compatible instantiation
25179 like X<T>, so look up the name in the original template. */
25180 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
25181 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25182 gcc_checking_assert (uses_template_parms (scope));
25183 /* If scope has no fields, it can't be a current instantiation. Check this
25184 before currently_open_class to avoid infinite recursion (71515). */
25185 if (!TYPE_FIELDS (scope))
25186 return type;
25187 /* If the SCOPE is not the current instantiation, there's no reason
25188 to look inside it. */
25189 if (only_current_p && !currently_open_class (scope))
25190 return type;
25191 /* Enter the SCOPE so that name lookup will be resolved as if we
25192 were in the class definition. In particular, SCOPE will no
25193 longer be considered a dependent type. */
25194 pushed_scope = push_scope (scope);
25195 /* Look up the declaration. */
25196 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
25197 tf_warning_or_error);
25198
25199 result = NULL_TREE;
25200
25201 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25202 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25203 tree fullname = TYPENAME_TYPE_FULLNAME (type);
25204 if (!decl)
25205 /*nop*/;
25206 else if (identifier_p (fullname)
25207 && TREE_CODE (decl) == TYPE_DECL)
25208 {
25209 result = TREE_TYPE (decl);
25210 if (result == error_mark_node)
25211 result = NULL_TREE;
25212 }
25213 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25214 && DECL_CLASS_TEMPLATE_P (decl))
25215 {
25216 /* Obtain the template and the arguments. */
25217 tree tmpl = TREE_OPERAND (fullname, 0);
25218 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25219 {
25220 /* We get here with a plain identifier because a previous tentative
25221 parse of the nested-name-specifier as part of a ptr-operator saw
25222 ::template X<A>. The use of ::template is necessary in a
25223 ptr-operator, but wrong in a declarator-id.
25224
25225 [temp.names]: In a qualified-id of a declarator-id, the keyword
25226 template shall not appear at the top level. */
25227 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
25228 "keyword %<template%> not allowed in declarator-id");
25229 tmpl = decl;
25230 }
25231 tree args = TREE_OPERAND (fullname, 1);
25232 /* Instantiate the template. */
25233 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25234 /*entering_scope=*/true,
25235 tf_error | tf_user);
25236 if (result == error_mark_node)
25237 result = NULL_TREE;
25238 }
25239
25240 /* Leave the SCOPE. */
25241 if (pushed_scope)
25242 pop_scope (pushed_scope);
25243
25244 /* If we failed to resolve it, return the original typename. */
25245 if (!result)
25246 return type;
25247
25248 /* If lookup found a typename type, resolve that too. */
25249 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25250 {
25251 /* Ill-formed programs can cause infinite recursion here, so we
25252 must catch that. */
25253 TYPENAME_IS_RESOLVING_P (result) = 1;
25254 result = resolve_typename_type (result, only_current_p);
25255 TYPENAME_IS_RESOLVING_P (result) = 0;
25256 }
25257
25258 /* Qualify the resulting type. */
25259 quals = cp_type_quals (type);
25260 if (quals)
25261 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25262
25263 return result;
25264 }
25265
25266 /* EXPR is an expression which is not type-dependent. Return a proxy
25267 for EXPR that can be used to compute the types of larger
25268 expressions containing EXPR. */
25269
25270 tree
25271 build_non_dependent_expr (tree expr)
25272 {
25273 tree orig_expr = expr;
25274 tree inner_expr;
25275
25276 /* When checking, try to get a constant value for all non-dependent
25277 expressions in order to expose bugs in *_dependent_expression_p
25278 and constexpr. This can affect code generation, see PR70704, so
25279 only do this for -fchecking=2. */
25280 if (flag_checking > 1
25281 && cxx_dialect >= cxx11
25282 /* Don't do this during nsdmi parsing as it can lead to
25283 unexpected recursive instantiations. */
25284 && !parsing_nsdmi ()
25285 /* Don't do this during concept expansion either and for
25286 the same reason. */
25287 && !expanding_concept ())
25288 fold_non_dependent_expr (expr);
25289
25290 STRIP_ANY_LOCATION_WRAPPER (expr);
25291
25292 /* Preserve OVERLOADs; the functions must be available to resolve
25293 types. */
25294 inner_expr = expr;
25295 if (TREE_CODE (inner_expr) == STMT_EXPR)
25296 inner_expr = stmt_expr_value_expr (inner_expr);
25297 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25298 inner_expr = TREE_OPERAND (inner_expr, 0);
25299 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25300 inner_expr = TREE_OPERAND (inner_expr, 1);
25301 if (is_overloaded_fn (inner_expr)
25302 || TREE_CODE (inner_expr) == OFFSET_REF)
25303 return orig_expr;
25304 /* There is no need to return a proxy for a variable. */
25305 if (VAR_P (expr))
25306 return orig_expr;
25307 /* Preserve string constants; conversions from string constants to
25308 "char *" are allowed, even though normally a "const char *"
25309 cannot be used to initialize a "char *". */
25310 if (TREE_CODE (expr) == STRING_CST)
25311 return orig_expr;
25312 /* Preserve void and arithmetic constants, as an optimization -- there is no
25313 reason to create a new node. */
25314 if (TREE_CODE (expr) == VOID_CST
25315 || TREE_CODE (expr) == INTEGER_CST
25316 || TREE_CODE (expr) == REAL_CST)
25317 return orig_expr;
25318 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25319 There is at least one place where we want to know that a
25320 particular expression is a throw-expression: when checking a ?:
25321 expression, there are special rules if the second or third
25322 argument is a throw-expression. */
25323 if (TREE_CODE (expr) == THROW_EXPR)
25324 return orig_expr;
25325
25326 /* Don't wrap an initializer list, we need to be able to look inside. */
25327 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25328 return orig_expr;
25329
25330 /* Don't wrap a dummy object, we need to be able to test for it. */
25331 if (is_dummy_object (expr))
25332 return orig_expr;
25333
25334 if (TREE_CODE (expr) == COND_EXPR)
25335 return build3 (COND_EXPR,
25336 TREE_TYPE (expr),
25337 TREE_OPERAND (expr, 0),
25338 (TREE_OPERAND (expr, 1)
25339 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25340 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25341 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25342 if (TREE_CODE (expr) == COMPOUND_EXPR
25343 && !COMPOUND_EXPR_OVERLOADED (expr))
25344 return build2 (COMPOUND_EXPR,
25345 TREE_TYPE (expr),
25346 TREE_OPERAND (expr, 0),
25347 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25348
25349 /* If the type is unknown, it can't really be non-dependent */
25350 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25351
25352 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25353 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25354 }
25355
25356 /* ARGS is a vector of expressions as arguments to a function call.
25357 Replace the arguments with equivalent non-dependent expressions.
25358 This modifies ARGS in place. */
25359
25360 void
25361 make_args_non_dependent (vec<tree, va_gc> *args)
25362 {
25363 unsigned int ix;
25364 tree arg;
25365
25366 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25367 {
25368 tree newarg = build_non_dependent_expr (arg);
25369 if (newarg != arg)
25370 (*args)[ix] = newarg;
25371 }
25372 }
25373
25374 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25375 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25376 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25377
25378 static tree
25379 make_auto_1 (tree name, bool set_canonical)
25380 {
25381 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25382 TYPE_NAME (au) = build_decl (input_location,
25383 TYPE_DECL, name, au);
25384 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25385 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25386 (0, processing_template_decl + 1, processing_template_decl + 1,
25387 TYPE_NAME (au), NULL_TREE);
25388 if (set_canonical)
25389 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25390 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25391 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25392
25393 return au;
25394 }
25395
25396 tree
25397 make_decltype_auto (void)
25398 {
25399 return make_auto_1 (decltype_auto_identifier, true);
25400 }
25401
25402 tree
25403 make_auto (void)
25404 {
25405 return make_auto_1 (auto_identifier, true);
25406 }
25407
25408 /* Return a C++17 deduction placeholder for class template TMPL. */
25409
25410 tree
25411 make_template_placeholder (tree tmpl)
25412 {
25413 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25414 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25415 return t;
25416 }
25417
25418 /* True iff T is a C++17 class template deduction placeholder. */
25419
25420 bool
25421 template_placeholder_p (tree t)
25422 {
25423 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25424 }
25425
25426 /* Make a "constrained auto" type-specifier. This is an
25427 auto type with constraints that must be associated after
25428 deduction. The constraint is formed from the given
25429 CONC and its optional sequence of arguments, which are
25430 non-null if written as partial-concept-id. */
25431
25432 tree
25433 make_constrained_auto (tree con, tree args)
25434 {
25435 tree type = make_auto_1 (auto_identifier, false);
25436
25437 /* Build the constraint. */
25438 tree tmpl = DECL_TI_TEMPLATE (con);
25439 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25440 expr = build_concept_check (expr, type, args);
25441
25442 tree constr = normalize_expression (expr);
25443 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25444
25445 /* Our canonical type depends on the constraint. */
25446 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25447
25448 /* Attach the constraint to the type declaration. */
25449 tree decl = TYPE_NAME (type);
25450 return decl;
25451 }
25452
25453 /* Given type ARG, return std::initializer_list<ARG>. */
25454
25455 static tree
25456 listify (tree arg)
25457 {
25458 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25459
25460 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25461 {
25462 gcc_rich_location richloc (input_location);
25463 maybe_add_include_fixit (&richloc, "<initializer_list>");
25464 error_at (&richloc,
25465 "deducing from brace-enclosed initializer list"
25466 " requires %<#include <initializer_list>%>");
25467
25468 return error_mark_node;
25469 }
25470 tree argvec = make_tree_vec (1);
25471 TREE_VEC_ELT (argvec, 0) = arg;
25472
25473 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25474 NULL_TREE, 0, tf_warning_or_error);
25475 }
25476
25477 /* Replace auto in TYPE with std::initializer_list<auto>. */
25478
25479 static tree
25480 listify_autos (tree type, tree auto_node)
25481 {
25482 tree init_auto = listify (auto_node);
25483 tree argvec = make_tree_vec (1);
25484 TREE_VEC_ELT (argvec, 0) = init_auto;
25485 if (processing_template_decl)
25486 argvec = add_to_template_args (current_template_args (), argvec);
25487 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25488 }
25489
25490 /* Hash traits for hashing possibly constrained 'auto'
25491 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25492
25493 struct auto_hash : default_hash_traits<tree>
25494 {
25495 static inline hashval_t hash (tree);
25496 static inline bool equal (tree, tree);
25497 };
25498
25499 /* Hash the 'auto' T. */
25500
25501 inline hashval_t
25502 auto_hash::hash (tree t)
25503 {
25504 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25505 /* Matching constrained-type-specifiers denote the same template
25506 parameter, so hash the constraint. */
25507 return hash_placeholder_constraint (c);
25508 else
25509 /* But unconstrained autos are all separate, so just hash the pointer. */
25510 return iterative_hash_object (t, 0);
25511 }
25512
25513 /* Compare two 'auto's. */
25514
25515 inline bool
25516 auto_hash::equal (tree t1, tree t2)
25517 {
25518 if (t1 == t2)
25519 return true;
25520
25521 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25522 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25523
25524 /* Two unconstrained autos are distinct. */
25525 if (!c1 || !c2)
25526 return false;
25527
25528 return equivalent_placeholder_constraints (c1, c2);
25529 }
25530
25531 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25532 constrained) auto, add it to the vector. */
25533
25534 static int
25535 extract_autos_r (tree t, void *data)
25536 {
25537 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25538 if (is_auto (t))
25539 {
25540 /* All the autos were built with index 0; fix that up now. */
25541 tree *p = hash.find_slot (t, INSERT);
25542 unsigned idx;
25543 if (*p)
25544 /* If this is a repeated constrained-type-specifier, use the index we
25545 chose before. */
25546 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25547 else
25548 {
25549 /* Otherwise this is new, so use the current count. */
25550 *p = t;
25551 idx = hash.elements () - 1;
25552 }
25553 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25554 }
25555
25556 /* Always keep walking. */
25557 return 0;
25558 }
25559
25560 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25561 says they can appear anywhere in the type. */
25562
25563 static tree
25564 extract_autos (tree type)
25565 {
25566 hash_set<tree> visited;
25567 hash_table<auto_hash> hash (2);
25568
25569 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25570
25571 tree tree_vec = make_tree_vec (hash.elements());
25572 for (hash_table<auto_hash>::iterator iter = hash.begin();
25573 iter != hash.end(); ++iter)
25574 {
25575 tree elt = *iter;
25576 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25577 TREE_VEC_ELT (tree_vec, i)
25578 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25579 }
25580
25581 return tree_vec;
25582 }
25583
25584 /* The stem for deduction guide names. */
25585 const char *const dguide_base = "__dguide_";
25586
25587 /* Return the name for a deduction guide for class template TMPL. */
25588
25589 tree
25590 dguide_name (tree tmpl)
25591 {
25592 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25593 tree tname = TYPE_IDENTIFIER (type);
25594 char *buf = (char *) alloca (1 + strlen (dguide_base)
25595 + IDENTIFIER_LENGTH (tname));
25596 memcpy (buf, dguide_base, strlen (dguide_base));
25597 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25598 IDENTIFIER_LENGTH (tname) + 1);
25599 tree dname = get_identifier (buf);
25600 TREE_TYPE (dname) = type;
25601 return dname;
25602 }
25603
25604 /* True if NAME is the name of a deduction guide. */
25605
25606 bool
25607 dguide_name_p (tree name)
25608 {
25609 return (TREE_CODE (name) == IDENTIFIER_NODE
25610 && TREE_TYPE (name)
25611 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25612 strlen (dguide_base)));
25613 }
25614
25615 /* True if FN is a deduction guide. */
25616
25617 bool
25618 deduction_guide_p (const_tree fn)
25619 {
25620 if (DECL_P (fn))
25621 if (tree name = DECL_NAME (fn))
25622 return dguide_name_p (name);
25623 return false;
25624 }
25625
25626 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25627
25628 bool
25629 copy_guide_p (const_tree fn)
25630 {
25631 gcc_assert (deduction_guide_p (fn));
25632 if (!DECL_ARTIFICIAL (fn))
25633 return false;
25634 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25635 return (TREE_CHAIN (parms) == void_list_node
25636 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25637 }
25638
25639 /* True if FN is a guide generated from a constructor template. */
25640
25641 bool
25642 template_guide_p (const_tree fn)
25643 {
25644 gcc_assert (deduction_guide_p (fn));
25645 if (!DECL_ARTIFICIAL (fn))
25646 return false;
25647 tree tmpl = DECL_TI_TEMPLATE (fn);
25648 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25649 return PRIMARY_TEMPLATE_P (org);
25650 return false;
25651 }
25652
25653 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25654 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25655 template parameter types. Note that the handling of template template
25656 parameters relies on current_template_parms being set appropriately for the
25657 new template. */
25658
25659 static tree
25660 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25661 tree tsubst_args, tsubst_flags_t complain)
25662 {
25663 tree oldidx = get_template_parm_index (olddecl);
25664
25665 tree newtype;
25666 if (TREE_CODE (olddecl) == TYPE_DECL
25667 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25668 {
25669 tree oldtype = TREE_TYPE (olddecl);
25670 newtype = cxx_make_type (TREE_CODE (oldtype));
25671 TYPE_MAIN_VARIANT (newtype) = newtype;
25672 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25673 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25674 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25675 }
25676 else
25677 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25678 complain, NULL_TREE);
25679
25680 tree newdecl
25681 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25682 DECL_NAME (olddecl), newtype);
25683 SET_DECL_TEMPLATE_PARM_P (newdecl);
25684
25685 tree newidx;
25686 if (TREE_CODE (olddecl) == TYPE_DECL
25687 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25688 {
25689 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25690 = build_template_parm_index (index, level, level,
25691 newdecl, newtype);
25692 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25693 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25694 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25695 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25696
25697 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25698 {
25699 DECL_TEMPLATE_RESULT (newdecl)
25700 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25701 DECL_NAME (olddecl), newtype);
25702 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25703 // First create a copy (ttargs) of tsubst_args with an
25704 // additional level for the template template parameter's own
25705 // template parameters (ttparms).
25706 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25707 (DECL_TEMPLATE_PARMS (olddecl)));
25708 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25709 tree ttargs = make_tree_vec (depth + 1);
25710 for (int i = 0; i < depth; ++i)
25711 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25712 TREE_VEC_ELT (ttargs, depth)
25713 = template_parms_level_to_args (ttparms);
25714 // Substitute ttargs into ttparms to fix references to
25715 // other template parameters.
25716 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25717 complain|tf_partial);
25718 // Now substitute again with args based on tparms, to reduce
25719 // the level of the ttparms.
25720 ttargs = current_template_args ();
25721 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25722 complain);
25723 // Finally, tack the adjusted parms onto tparms.
25724 ttparms = tree_cons (size_int (depth), ttparms,
25725 current_template_parms);
25726 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25727 }
25728 }
25729 else
25730 {
25731 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25732 tree newconst
25733 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25734 TREE_CODE (oldconst),
25735 DECL_NAME (oldconst), newtype);
25736 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25737 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25738 SET_DECL_TEMPLATE_PARM_P (newconst);
25739 newidx = build_template_parm_index (index, level, level,
25740 newconst, newtype);
25741 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25742 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25743 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25744 }
25745
25746 return newdecl;
25747 }
25748
25749 /* Returns a C++17 class deduction guide template based on the constructor
25750 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25751 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25752
25753 static tree
25754 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25755 {
25756 tree type, tparms, targs, fparms, fargs, ci;
25757 bool memtmpl = false;
25758 bool explicit_p;
25759 location_t loc;
25760 tree fn_tmpl = NULL_TREE;
25761
25762 if (TYPE_P (ctor))
25763 {
25764 type = ctor;
25765 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25766 if (copy_p)
25767 {
25768 type = TREE_TYPE (type);
25769 fparms = tree_cons (NULL_TREE, type, void_list_node);
25770 }
25771 else
25772 fparms = void_list_node;
25773
25774 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25775 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25776 targs = CLASSTYPE_TI_ARGS (type);
25777 ci = NULL_TREE;
25778 fargs = NULL_TREE;
25779 loc = DECL_SOURCE_LOCATION (ctmpl);
25780 explicit_p = false;
25781 }
25782 else
25783 {
25784 ++processing_template_decl;
25785
25786 fn_tmpl
25787 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25788 : DECL_TI_TEMPLATE (ctor));
25789 if (outer_args)
25790 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25791 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25792
25793 type = DECL_CONTEXT (ctor);
25794
25795 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25796 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25797 fully specialized args for the enclosing class. Strip those off, as
25798 the deduction guide won't have those template parameters. */
25799 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25800 TMPL_PARMS_DEPTH (tparms));
25801 /* Discard the 'this' parameter. */
25802 fparms = FUNCTION_ARG_CHAIN (ctor);
25803 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25804 ci = get_constraints (ctor);
25805 loc = DECL_SOURCE_LOCATION (ctor);
25806 explicit_p = DECL_NONCONVERTING_P (ctor);
25807
25808 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25809 {
25810 memtmpl = true;
25811
25812 /* For a member template constructor, we need to flatten the two
25813 template parameter lists into one, and then adjust the function
25814 signature accordingly. This gets...complicated. */
25815 tree save_parms = current_template_parms;
25816
25817 /* For a member template we should have two levels of parms/args, one
25818 for the class and one for the constructor. We stripped
25819 specialized args for further enclosing classes above. */
25820 const int depth = 2;
25821 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25822
25823 /* Template args for translating references to the two-level template
25824 parameters into references to the one-level template parameters we
25825 are creating. */
25826 tree tsubst_args = copy_node (targs);
25827 TMPL_ARGS_LEVEL (tsubst_args, depth)
25828 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25829
25830 /* Template parms for the constructor template. */
25831 tree ftparms = TREE_VALUE (tparms);
25832 unsigned flen = TREE_VEC_LENGTH (ftparms);
25833 /* Template parms for the class template. */
25834 tparms = TREE_CHAIN (tparms);
25835 tree ctparms = TREE_VALUE (tparms);
25836 unsigned clen = TREE_VEC_LENGTH (ctparms);
25837 /* Template parms for the deduction guide start as a copy of the
25838 template parms for the class. We set current_template_parms for
25839 lookup_template_class_1. */
25840 current_template_parms = tparms = copy_node (tparms);
25841 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25842 for (unsigned i = 0; i < clen; ++i)
25843 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25844
25845 /* Now we need to rewrite the constructor parms to append them to the
25846 class parms. */
25847 for (unsigned i = 0; i < flen; ++i)
25848 {
25849 unsigned index = i + clen;
25850 unsigned level = 1;
25851 tree oldelt = TREE_VEC_ELT (ftparms, i);
25852 tree olddecl = TREE_VALUE (oldelt);
25853 tree newdecl = rewrite_template_parm (olddecl, index, level,
25854 tsubst_args, complain);
25855 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25856 tsubst_args, complain, ctor);
25857 tree list = build_tree_list (newdef, newdecl);
25858 TEMPLATE_PARM_CONSTRAINTS (list)
25859 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25860 tsubst_args, complain, ctor);
25861 TREE_VEC_ELT (new_vec, index) = list;
25862 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25863 }
25864
25865 /* Now we have a final set of template parms to substitute into the
25866 function signature. */
25867 targs = template_parms_to_args (tparms);
25868 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25869 complain, ctor);
25870 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25871 if (ci)
25872 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25873
25874 current_template_parms = save_parms;
25875 }
25876 --processing_template_decl;
25877 }
25878
25879 if (!memtmpl)
25880 {
25881 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25882 tparms = copy_node (tparms);
25883 INNERMOST_TEMPLATE_PARMS (tparms)
25884 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25885 }
25886
25887 tree fntype = build_function_type (type, fparms);
25888 tree ded_fn = build_lang_decl_loc (loc,
25889 FUNCTION_DECL,
25890 dguide_name (type), fntype);
25891 DECL_ARGUMENTS (ded_fn) = fargs;
25892 DECL_ARTIFICIAL (ded_fn) = true;
25893 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25894 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25895 DECL_ARTIFICIAL (ded_tmpl) = true;
25896 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25897 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25898 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25899 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25900 if (DECL_P (ctor))
25901 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25902 if (ci)
25903 set_constraints (ded_tmpl, ci);
25904
25905 return ded_tmpl;
25906 }
25907
25908 /* Deduce template arguments for the class template placeholder PTYPE for
25909 template TMPL based on the initializer INIT, and return the resulting
25910 type. */
25911
25912 static tree
25913 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25914 tsubst_flags_t complain)
25915 {
25916 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25917 {
25918 /* We should have handled this in the caller. */
25919 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25920 return ptype;
25921 if (complain & tf_error)
25922 error ("non-class template %qT used without template arguments", tmpl);
25923 return error_mark_node;
25924 }
25925
25926 tree type = TREE_TYPE (tmpl);
25927
25928 bool try_list_ctor = false;
25929
25930 vec<tree,va_gc> *args;
25931 if (init == NULL_TREE
25932 || TREE_CODE (init) == TREE_LIST)
25933 args = make_tree_vector_from_list (init);
25934 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25935 {
25936 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25937 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25938 {
25939 /* As an exception, the first phase in 16.3.1.7 (considering the
25940 initializer list as a single argument) is omitted if the
25941 initializer list consists of a single expression of type cv U,
25942 where U is a specialization of C or a class derived from a
25943 specialization of C. */
25944 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25945 tree etype = TREE_TYPE (elt);
25946
25947 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25948 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25949 int err = unify (tparms, targs, type, etype,
25950 UNIFY_ALLOW_DERIVED, /*explain*/false);
25951 if (err == 0)
25952 try_list_ctor = false;
25953 ggc_free (targs);
25954 }
25955 if (try_list_ctor || is_std_init_list (type))
25956 args = make_tree_vector_single (init);
25957 else
25958 args = make_tree_vector_from_ctor (init);
25959 }
25960 else
25961 args = make_tree_vector_single (init);
25962
25963 tree dname = dguide_name (tmpl);
25964 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25965 /*type*/false, /*complain*/false,
25966 /*hidden*/false);
25967 bool elided = false;
25968 if (cands == error_mark_node)
25969 cands = NULL_TREE;
25970
25971 /* Prune explicit deduction guides in copy-initialization context. */
25972 if (flags & LOOKUP_ONLYCONVERTING)
25973 {
25974 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25975 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25976 elided = true;
25977
25978 if (elided)
25979 {
25980 /* Found a nonconverting guide, prune the candidates. */
25981 tree pruned = NULL_TREE;
25982 for (lkp_iterator iter (cands); iter; ++iter)
25983 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25984 pruned = lookup_add (*iter, pruned);
25985
25986 cands = pruned;
25987 }
25988 }
25989
25990 tree outer_args = NULL_TREE;
25991 if (DECL_CLASS_SCOPE_P (tmpl)
25992 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25993 {
25994 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25995 type = TREE_TYPE (most_general_template (tmpl));
25996 }
25997
25998 bool saw_ctor = false;
25999 // FIXME cache artificial deduction guides
26000 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
26001 {
26002 tree guide = build_deduction_guide (*iter, outer_args, complain);
26003 if ((flags & LOOKUP_ONLYCONVERTING)
26004 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
26005 elided = true;
26006 else
26007 cands = lookup_add (guide, cands);
26008
26009 saw_ctor = true;
26010 }
26011
26012 tree call = error_mark_node;
26013
26014 /* If this is list-initialization and the class has a list constructor, first
26015 try deducing from the list as a single argument, as [over.match.list]. */
26016 tree list_cands = NULL_TREE;
26017 if (try_list_ctor && cands)
26018 for (lkp_iterator iter (cands); iter; ++iter)
26019 {
26020 tree dg = *iter;
26021 if (is_list_ctor (dg))
26022 list_cands = lookup_add (dg, list_cands);
26023 }
26024 if (list_cands)
26025 {
26026 ++cp_unevaluated_operand;
26027 call = build_new_function_call (list_cands, &args, tf_decltype);
26028 --cp_unevaluated_operand;
26029
26030 if (call == error_mark_node)
26031 {
26032 /* That didn't work, now try treating the list as a sequence of
26033 arguments. */
26034 release_tree_vector (args);
26035 args = make_tree_vector_from_ctor (init);
26036 }
26037 }
26038
26039 /* Maybe generate an implicit deduction guide. */
26040 if (call == error_mark_node && args->length () < 2)
26041 {
26042 tree gtype = NULL_TREE;
26043
26044 if (args->length () == 1)
26045 /* Generate a copy guide. */
26046 gtype = build_reference_type (type);
26047 else if (!saw_ctor)
26048 /* Generate a default guide. */
26049 gtype = type;
26050
26051 if (gtype)
26052 {
26053 tree guide = build_deduction_guide (gtype, outer_args, complain);
26054 cands = lookup_add (guide, cands);
26055 }
26056 }
26057
26058 if (elided && !cands)
26059 {
26060 error ("cannot deduce template arguments for copy-initialization"
26061 " of %qT, as it has no non-explicit deduction guides or "
26062 "user-declared constructors", type);
26063 return error_mark_node;
26064 }
26065 else if (!cands && call == error_mark_node)
26066 {
26067 error ("cannot deduce template arguments of %qT, as it has no viable "
26068 "deduction guides", type);
26069 return error_mark_node;
26070 }
26071
26072 if (call == error_mark_node)
26073 {
26074 ++cp_unevaluated_operand;
26075 call = build_new_function_call (cands, &args, tf_decltype);
26076 --cp_unevaluated_operand;
26077 }
26078
26079 if (call == error_mark_node && (complain & tf_warning_or_error))
26080 {
26081 error ("class template argument deduction failed:");
26082
26083 ++cp_unevaluated_operand;
26084 call = build_new_function_call (cands, &args, complain | tf_decltype);
26085 --cp_unevaluated_operand;
26086
26087 if (elided)
26088 inform (input_location, "explicit deduction guides not considered "
26089 "for copy-initialization");
26090 }
26091
26092 release_tree_vector (args);
26093
26094 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
26095 }
26096
26097 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26098 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26099 The CONTEXT determines the context in which auto deduction is performed
26100 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26101 OUTER_TARGS are used during template argument deduction
26102 (context == adc_unify) to properly substitute the result, and is ignored
26103 in other contexts.
26104
26105 For partial-concept-ids, extra args may be appended to the list of deduced
26106 template arguments prior to determining constraint satisfaction. */
26107
26108 tree
26109 do_auto_deduction (tree type, tree init, tree auto_node,
26110 tsubst_flags_t complain, auto_deduction_context context,
26111 tree outer_targs, int flags)
26112 {
26113 tree targs;
26114
26115 if (init == error_mark_node)
26116 return error_mark_node;
26117
26118 if (init && type_dependent_expression_p (init)
26119 && context != adc_unify)
26120 /* Defining a subset of type-dependent expressions that we can deduce
26121 from ahead of time isn't worth the trouble. */
26122 return type;
26123
26124 /* Similarly, we can't deduce from another undeduced decl. */
26125 if (init && undeduced_auto_decl (init))
26126 return type;
26127
26128 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
26129 /* C++17 class template argument deduction. */
26130 return do_class_deduction (type, tmpl, init, flags, complain);
26131
26132 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
26133 /* Nothing we can do with this, even in deduction context. */
26134 return type;
26135
26136 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26137 with either a new invented type template parameter U or, if the
26138 initializer is a braced-init-list (8.5.4), with
26139 std::initializer_list<U>. */
26140 if (BRACE_ENCLOSED_INITIALIZER_P (init))
26141 {
26142 if (!DIRECT_LIST_INIT_P (init))
26143 type = listify_autos (type, auto_node);
26144 else if (CONSTRUCTOR_NELTS (init) == 1)
26145 init = CONSTRUCTOR_ELT (init, 0)->value;
26146 else
26147 {
26148 if (complain & tf_warning_or_error)
26149 {
26150 if (permerror (input_location, "direct-list-initialization of "
26151 "%<auto%> requires exactly one element"))
26152 inform (input_location,
26153 "for deduction to %<std::initializer_list%>, use copy-"
26154 "list-initialization (i.e. add %<=%> before the %<{%>)");
26155 }
26156 type = listify_autos (type, auto_node);
26157 }
26158 }
26159
26160 if (type == error_mark_node)
26161 return error_mark_node;
26162
26163 init = resolve_nondeduced_context (init, complain);
26164
26165 if (context == adc_decomp_type
26166 && auto_node == type
26167 && init != error_mark_node
26168 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
26169 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26170 and initializer has array type, deduce cv-qualified array type. */
26171 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
26172 complain);
26173 else if (AUTO_IS_DECLTYPE (auto_node))
26174 {
26175 bool id = (DECL_P (init)
26176 || ((TREE_CODE (init) == COMPONENT_REF
26177 || TREE_CODE (init) == SCOPE_REF)
26178 && !REF_PARENTHESIZED_P (init)));
26179 targs = make_tree_vec (1);
26180 TREE_VEC_ELT (targs, 0)
26181 = finish_decltype_type (init, id, tf_warning_or_error);
26182 if (type != auto_node)
26183 {
26184 if (complain & tf_error)
26185 error ("%qT as type rather than plain %<decltype(auto)%>", type);
26186 return error_mark_node;
26187 }
26188 }
26189 else
26190 {
26191 tree parms = build_tree_list (NULL_TREE, type);
26192 tree tparms;
26193
26194 if (flag_concepts)
26195 tparms = extract_autos (type);
26196 else
26197 {
26198 tparms = make_tree_vec (1);
26199 TREE_VEC_ELT (tparms, 0)
26200 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
26201 }
26202
26203 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26204 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
26205 DEDUCE_CALL, LOOKUP_NORMAL,
26206 NULL, /*explain_p=*/false);
26207 if (val > 0)
26208 {
26209 if (processing_template_decl)
26210 /* Try again at instantiation time. */
26211 return type;
26212 if (type && type != error_mark_node
26213 && (complain & tf_error))
26214 /* If type is error_mark_node a diagnostic must have been
26215 emitted by now. Also, having a mention to '<type error>'
26216 in the diagnostic is not really useful to the user. */
26217 {
26218 if (cfun && auto_node == current_function_auto_return_pattern
26219 && LAMBDA_FUNCTION_P (current_function_decl))
26220 error ("unable to deduce lambda return type from %qE", init);
26221 else
26222 error ("unable to deduce %qT from %qE", type, init);
26223 type_unification_real (tparms, targs, parms, &init, 1, 0,
26224 DEDUCE_CALL, LOOKUP_NORMAL,
26225 NULL, /*explain_p=*/true);
26226 }
26227 return error_mark_node;
26228 }
26229 }
26230
26231 /* Check any placeholder constraints against the deduced type. */
26232 if (flag_concepts && !processing_template_decl)
26233 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26234 {
26235 /* Use the deduced type to check the associated constraints. If we
26236 have a partial-concept-id, rebuild the argument list so that
26237 we check using the extra arguments. */
26238 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26239 tree cargs = CHECK_CONSTR_ARGS (constr);
26240 if (TREE_VEC_LENGTH (cargs) > 1)
26241 {
26242 cargs = copy_node (cargs);
26243 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26244 }
26245 else
26246 cargs = targs;
26247 if (!constraints_satisfied_p (constr, cargs))
26248 {
26249 if (complain & tf_warning_or_error)
26250 {
26251 switch (context)
26252 {
26253 case adc_unspecified:
26254 case adc_unify:
26255 error("placeholder constraints not satisfied");
26256 break;
26257 case adc_variable_type:
26258 case adc_decomp_type:
26259 error ("deduced initializer does not satisfy "
26260 "placeholder constraints");
26261 break;
26262 case adc_return_type:
26263 error ("deduced return type does not satisfy "
26264 "placeholder constraints");
26265 break;
26266 case adc_requirement:
26267 error ("deduced expression type does not satisfy "
26268 "placeholder constraints");
26269 break;
26270 }
26271 diagnose_constraints (input_location, constr, targs);
26272 }
26273 return error_mark_node;
26274 }
26275 }
26276
26277 if (processing_template_decl && context != adc_unify)
26278 outer_targs = current_template_args ();
26279 targs = add_to_template_args (outer_targs, targs);
26280 return tsubst (type, targs, complain, NULL_TREE);
26281 }
26282
26283 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26284 result. */
26285
26286 tree
26287 splice_late_return_type (tree type, tree late_return_type)
26288 {
26289 if (is_auto (type))
26290 {
26291 if (late_return_type)
26292 return late_return_type;
26293
26294 tree idx = get_template_parm_index (type);
26295 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26296 /* In an abbreviated function template we didn't know we were dealing
26297 with a function template when we saw the auto return type, so update
26298 it to have the correct level. */
26299 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26300 }
26301 return type;
26302 }
26303
26304 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26305 'decltype(auto)' or a deduced class template. */
26306
26307 bool
26308 is_auto (const_tree type)
26309 {
26310 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26311 && (TYPE_IDENTIFIER (type) == auto_identifier
26312 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26313 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26314 return true;
26315 else
26316 return false;
26317 }
26318
26319 /* for_each_template_parm callback for type_uses_auto. */
26320
26321 int
26322 is_auto_r (tree tp, void */*data*/)
26323 {
26324 return is_auto (tp);
26325 }
26326
26327 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26328 a use of `auto'. Returns NULL_TREE otherwise. */
26329
26330 tree
26331 type_uses_auto (tree type)
26332 {
26333 if (type == NULL_TREE)
26334 return NULL_TREE;
26335 else if (flag_concepts)
26336 {
26337 /* The Concepts TS allows multiple autos in one type-specifier; just
26338 return the first one we find, do_auto_deduction will collect all of
26339 them. */
26340 if (uses_template_parms (type))
26341 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26342 /*visited*/NULL, /*nondeduced*/true);
26343 else
26344 return NULL_TREE;
26345 }
26346 else
26347 return find_type_usage (type, is_auto);
26348 }
26349
26350 /* For a given template T, return the vector of typedefs referenced
26351 in T for which access check is needed at T instantiation time.
26352 T is either a FUNCTION_DECL or a RECORD_TYPE.
26353 Those typedefs were added to T by the function
26354 append_type_to_template_for_access_check. */
26355
26356 vec<qualified_typedef_usage_t, va_gc> *
26357 get_types_needing_access_check (tree t)
26358 {
26359 tree ti;
26360 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26361
26362 if (!t || t == error_mark_node)
26363 return NULL;
26364
26365 if (!(ti = get_template_info (t)))
26366 return NULL;
26367
26368 if (CLASS_TYPE_P (t)
26369 || TREE_CODE (t) == FUNCTION_DECL)
26370 {
26371 if (!TI_TEMPLATE (ti))
26372 return NULL;
26373
26374 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26375 }
26376
26377 return result;
26378 }
26379
26380 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26381 tied to T. That list of typedefs will be access checked at
26382 T instantiation time.
26383 T is either a FUNCTION_DECL or a RECORD_TYPE.
26384 TYPE_DECL is a TYPE_DECL node representing a typedef.
26385 SCOPE is the scope through which TYPE_DECL is accessed.
26386 LOCATION is the location of the usage point of TYPE_DECL.
26387
26388 This function is a subroutine of
26389 append_type_to_template_for_access_check. */
26390
26391 static void
26392 append_type_to_template_for_access_check_1 (tree t,
26393 tree type_decl,
26394 tree scope,
26395 location_t location)
26396 {
26397 qualified_typedef_usage_t typedef_usage;
26398 tree ti;
26399
26400 if (!t || t == error_mark_node)
26401 return;
26402
26403 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26404 || CLASS_TYPE_P (t))
26405 && type_decl
26406 && TREE_CODE (type_decl) == TYPE_DECL
26407 && scope);
26408
26409 if (!(ti = get_template_info (t)))
26410 return;
26411
26412 gcc_assert (TI_TEMPLATE (ti));
26413
26414 typedef_usage.typedef_decl = type_decl;
26415 typedef_usage.context = scope;
26416 typedef_usage.locus = location;
26417
26418 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26419 }
26420
26421 /* Append TYPE_DECL to the template TEMPL.
26422 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26423 At TEMPL instanciation time, TYPE_DECL will be checked to see
26424 if it can be accessed through SCOPE.
26425 LOCATION is the location of the usage point of TYPE_DECL.
26426
26427 e.g. consider the following code snippet:
26428
26429 class C
26430 {
26431 typedef int myint;
26432 };
26433
26434 template<class U> struct S
26435 {
26436 C::myint mi; // <-- usage point of the typedef C::myint
26437 };
26438
26439 S<char> s;
26440
26441 At S<char> instantiation time, we need to check the access of C::myint
26442 In other words, we need to check the access of the myint typedef through
26443 the C scope. For that purpose, this function will add the myint typedef
26444 and the scope C through which its being accessed to a list of typedefs
26445 tied to the template S. That list will be walked at template instantiation
26446 time and access check performed on each typedefs it contains.
26447 Note that this particular code snippet should yield an error because
26448 myint is private to C. */
26449
26450 void
26451 append_type_to_template_for_access_check (tree templ,
26452 tree type_decl,
26453 tree scope,
26454 location_t location)
26455 {
26456 qualified_typedef_usage_t *iter;
26457 unsigned i;
26458
26459 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26460
26461 /* Make sure we don't append the type to the template twice. */
26462 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26463 if (iter->typedef_decl == type_decl && scope == iter->context)
26464 return;
26465
26466 append_type_to_template_for_access_check_1 (templ, type_decl,
26467 scope, location);
26468 }
26469
26470 /* Convert the generic type parameters in PARM that match the types given in the
26471 range [START_IDX, END_IDX) from the current_template_parms into generic type
26472 packs. */
26473
26474 tree
26475 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26476 {
26477 tree current = current_template_parms;
26478 int depth = TMPL_PARMS_DEPTH (current);
26479 current = INNERMOST_TEMPLATE_PARMS (current);
26480 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26481
26482 for (int i = 0; i < start_idx; ++i)
26483 TREE_VEC_ELT (replacement, i)
26484 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26485
26486 for (int i = start_idx; i < end_idx; ++i)
26487 {
26488 /* Create a distinct parameter pack type from the current parm and add it
26489 to the replacement args to tsubst below into the generic function
26490 parameter. */
26491
26492 tree o = TREE_TYPE (TREE_VALUE
26493 (TREE_VEC_ELT (current, i)));
26494 tree t = copy_type (o);
26495 TEMPLATE_TYPE_PARM_INDEX (t)
26496 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26497 o, 0, 0, tf_none);
26498 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26499 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26500 TYPE_MAIN_VARIANT (t) = t;
26501 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26502 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26503 TREE_VEC_ELT (replacement, i) = t;
26504 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26505 }
26506
26507 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26508 TREE_VEC_ELT (replacement, i)
26509 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26510
26511 /* If there are more levels then build up the replacement with the outer
26512 template parms. */
26513 if (depth > 1)
26514 replacement = add_to_template_args (template_parms_to_args
26515 (TREE_CHAIN (current_template_parms)),
26516 replacement);
26517
26518 return tsubst (parm, replacement, tf_none, NULL_TREE);
26519 }
26520
26521 /* Entries in the decl_constraint hash table. */
26522 struct GTY((for_user)) constr_entry
26523 {
26524 tree decl;
26525 tree ci;
26526 };
26527
26528 /* Hashing function and equality for constraint entries. */
26529 struct constr_hasher : ggc_ptr_hash<constr_entry>
26530 {
26531 static hashval_t hash (constr_entry *e)
26532 {
26533 return (hashval_t)DECL_UID (e->decl);
26534 }
26535
26536 static bool equal (constr_entry *e1, constr_entry *e2)
26537 {
26538 return e1->decl == e2->decl;
26539 }
26540 };
26541
26542 /* A mapping from declarations to constraint information. Note that
26543 both templates and their underlying declarations are mapped to the
26544 same constraint information.
26545
26546 FIXME: This is defined in pt.c because garbage collection
26547 code is not being generated for constraint.cc. */
26548
26549 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26550
26551 /* Returns the template constraints of declaration T. If T is not
26552 constrained, return NULL_TREE. Note that T must be non-null. */
26553
26554 tree
26555 get_constraints (tree t)
26556 {
26557 if (!flag_concepts)
26558 return NULL_TREE;
26559
26560 gcc_assert (DECL_P (t));
26561 if (TREE_CODE (t) == TEMPLATE_DECL)
26562 t = DECL_TEMPLATE_RESULT (t);
26563 constr_entry elt = { t, NULL_TREE };
26564 constr_entry* found = decl_constraints->find (&elt);
26565 if (found)
26566 return found->ci;
26567 else
26568 return NULL_TREE;
26569 }
26570
26571 /* Associate the given constraint information CI with the declaration
26572 T. If T is a template, then the constraints are associated with
26573 its underlying declaration. Don't build associations if CI is
26574 NULL_TREE. */
26575
26576 void
26577 set_constraints (tree t, tree ci)
26578 {
26579 if (!ci)
26580 return;
26581 gcc_assert (t && flag_concepts);
26582 if (TREE_CODE (t) == TEMPLATE_DECL)
26583 t = DECL_TEMPLATE_RESULT (t);
26584 gcc_assert (!get_constraints (t));
26585 constr_entry elt = {t, ci};
26586 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26587 constr_entry* entry = ggc_alloc<constr_entry> ();
26588 *entry = elt;
26589 *slot = entry;
26590 }
26591
26592 /* Remove the associated constraints of the declaration T. */
26593
26594 void
26595 remove_constraints (tree t)
26596 {
26597 gcc_assert (DECL_P (t));
26598 if (TREE_CODE (t) == TEMPLATE_DECL)
26599 t = DECL_TEMPLATE_RESULT (t);
26600
26601 constr_entry elt = {t, NULL_TREE};
26602 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26603 if (slot)
26604 decl_constraints->clear_slot (slot);
26605 }
26606
26607 /* Memoized satisfaction results for declarations. This
26608 maps the pair (constraint_info, arguments) to the result computed
26609 by constraints_satisfied_p. */
26610
26611 struct GTY((for_user)) constraint_sat_entry
26612 {
26613 tree ci;
26614 tree args;
26615 tree result;
26616 };
26617
26618 /* Hashing function and equality for constraint entries. */
26619
26620 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26621 {
26622 static hashval_t hash (constraint_sat_entry *e)
26623 {
26624 hashval_t val = iterative_hash_object(e->ci, 0);
26625 return iterative_hash_template_arg (e->args, val);
26626 }
26627
26628 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26629 {
26630 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26631 }
26632 };
26633
26634 /* Memoized satisfaction results for concept checks. */
26635
26636 struct GTY((for_user)) concept_spec_entry
26637 {
26638 tree tmpl;
26639 tree args;
26640 tree result;
26641 };
26642
26643 /* Hashing function and equality for constraint entries. */
26644
26645 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26646 {
26647 static hashval_t hash (concept_spec_entry *e)
26648 {
26649 return hash_tmpl_and_args (e->tmpl, e->args);
26650 }
26651
26652 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26653 {
26654 ++comparing_specializations;
26655 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26656 --comparing_specializations;
26657 return eq;
26658 }
26659 };
26660
26661 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26662 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26663
26664 /* Search for a memoized satisfaction result. Returns one of the
26665 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26666
26667 tree
26668 lookup_constraint_satisfaction (tree ci, tree args)
26669 {
26670 constraint_sat_entry elt = { ci, args, NULL_TREE };
26671 constraint_sat_entry* found = constraint_memos->find (&elt);
26672 if (found)
26673 return found->result;
26674 else
26675 return NULL_TREE;
26676 }
26677
26678 /* Memoize the result of a satisfication test. Returns the saved result. */
26679
26680 tree
26681 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26682 {
26683 constraint_sat_entry elt = {ci, args, result};
26684 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26685 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26686 *entry = elt;
26687 *slot = entry;
26688 return result;
26689 }
26690
26691 /* Search for a memoized satisfaction result for a concept. */
26692
26693 tree
26694 lookup_concept_satisfaction (tree tmpl, tree args)
26695 {
26696 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26697 concept_spec_entry* found = concept_memos->find (&elt);
26698 if (found)
26699 return found->result;
26700 else
26701 return NULL_TREE;
26702 }
26703
26704 /* Memoize the result of a concept check. Returns the saved result. */
26705
26706 tree
26707 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26708 {
26709 concept_spec_entry elt = {tmpl, args, result};
26710 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26711 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26712 *entry = elt;
26713 *slot = entry;
26714 return result;
26715 }
26716
26717 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26718
26719 /* Returns a prior concept specialization. This returns the substituted
26720 and normalized constraints defined by the concept. */
26721
26722 tree
26723 get_concept_expansion (tree tmpl, tree args)
26724 {
26725 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26726 concept_spec_entry* found = concept_expansions->find (&elt);
26727 if (found)
26728 return found->result;
26729 else
26730 return NULL_TREE;
26731 }
26732
26733 /* Save a concept expansion for later. */
26734
26735 tree
26736 save_concept_expansion (tree tmpl, tree args, tree def)
26737 {
26738 concept_spec_entry elt = {tmpl, args, def};
26739 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26740 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26741 *entry = elt;
26742 *slot = entry;
26743 return def;
26744 }
26745
26746 static hashval_t
26747 hash_subsumption_args (tree t1, tree t2)
26748 {
26749 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26750 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26751 int val = 0;
26752 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26753 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26754 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26755 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26756 return val;
26757 }
26758
26759 /* Compare the constraints of two subsumption entries. The LEFT1 and
26760 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26761 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26762
26763 static bool
26764 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26765 {
26766 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26767 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26768 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26769 CHECK_CONSTR_ARGS (right1)))
26770 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26771 CHECK_CONSTR_ARGS (right2));
26772 return false;
26773 }
26774
26775 /* Key/value pair for learning and memoizing subsumption results. This
26776 associates a pair of check constraints (including arguments) with
26777 a boolean value indicating the result. */
26778
26779 struct GTY((for_user)) subsumption_entry
26780 {
26781 tree t1;
26782 tree t2;
26783 bool result;
26784 };
26785
26786 /* Hashing function and equality for constraint entries. */
26787
26788 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26789 {
26790 static hashval_t hash (subsumption_entry *e)
26791 {
26792 return hash_subsumption_args (e->t1, e->t2);
26793 }
26794
26795 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26796 {
26797 ++comparing_specializations;
26798 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26799 --comparing_specializations;
26800 return eq;
26801 }
26802 };
26803
26804 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26805
26806 /* Search for a previously cached subsumption result. */
26807
26808 bool*
26809 lookup_subsumption_result (tree t1, tree t2)
26810 {
26811 subsumption_entry elt = { t1, t2, false };
26812 subsumption_entry* found = subsumption_table->find (&elt);
26813 if (found)
26814 return &found->result;
26815 else
26816 return 0;
26817 }
26818
26819 /* Save a subsumption result. */
26820
26821 bool
26822 save_subsumption_result (tree t1, tree t2, bool result)
26823 {
26824 subsumption_entry elt = {t1, t2, result};
26825 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26826 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26827 *entry = elt;
26828 *slot = entry;
26829 return result;
26830 }
26831
26832 /* Set up the hash table for constraint association. */
26833
26834 void
26835 init_constraint_processing (void)
26836 {
26837 if (!flag_concepts)
26838 return;
26839
26840 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26841 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26842 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26843 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26844 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26845 }
26846
26847 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26848 0..N-1. */
26849
26850 void
26851 declare_integer_pack (void)
26852 {
26853 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26854 build_function_type_list (integer_type_node,
26855 integer_type_node,
26856 NULL_TREE),
26857 NULL_TREE, ECF_CONST);
26858 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26859 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26860 }
26861
26862 /* Set up the hash tables for template instantiations. */
26863
26864 void
26865 init_template_processing (void)
26866 {
26867 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26868 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26869
26870 if (cxx_dialect >= cxx11)
26871 declare_integer_pack ();
26872 }
26873
26874 /* Print stats about the template hash tables for -fstats. */
26875
26876 void
26877 print_template_statistics (void)
26878 {
26879 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26880 "%f collisions\n", (long) decl_specializations->size (),
26881 (long) decl_specializations->elements (),
26882 decl_specializations->collisions ());
26883 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26884 "%f collisions\n", (long) type_specializations->size (),
26885 (long) type_specializations->elements (),
26886 type_specializations->collisions ());
26887 }
26888
26889 #if CHECKING_P
26890
26891 namespace selftest {
26892
26893 /* Verify that build_non_dependent_expr () works, for various expressions,
26894 and that location wrappers don't affect the results. */
26895
26896 static void
26897 test_build_non_dependent_expr ()
26898 {
26899 location_t loc = BUILTINS_LOCATION;
26900
26901 /* Verify constants, without and with location wrappers. */
26902 tree int_cst = build_int_cst (integer_type_node, 42);
26903 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
26904
26905 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
26906 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
26907 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
26908
26909 tree string_lit = build_string (4, "foo");
26910 TREE_TYPE (string_lit) = char_array_type_node;
26911 string_lit = fix_string_type (string_lit);
26912 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
26913
26914 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
26915 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
26916 ASSERT_EQ (wrapped_string_lit,
26917 build_non_dependent_expr (wrapped_string_lit));
26918 }
26919
26920 /* Verify that type_dependent_expression_p () works correctly, even
26921 in the presence of location wrapper nodes. */
26922
26923 static void
26924 test_type_dependent_expression_p ()
26925 {
26926 location_t loc = BUILTINS_LOCATION;
26927
26928 tree name = get_identifier ("foo");
26929
26930 /* If no templates are involved, nothing is type-dependent. */
26931 gcc_assert (!processing_template_decl);
26932 ASSERT_FALSE (type_dependent_expression_p (name));
26933
26934 ++processing_template_decl;
26935
26936 /* Within a template, an unresolved name is always type-dependent. */
26937 ASSERT_TRUE (type_dependent_expression_p (name));
26938
26939 /* Ensure it copes with NULL_TREE and errors. */
26940 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
26941 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
26942
26943 /* A USING_DECL in a template should be type-dependent, even if wrapped
26944 with a location wrapper (PR c++/83799). */
26945 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
26946 TREE_TYPE (using_decl) = integer_type_node;
26947 ASSERT_TRUE (type_dependent_expression_p (using_decl));
26948 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
26949 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
26950 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
26951
26952 --processing_template_decl;
26953 }
26954
26955 /* Run all of the selftests within this file. */
26956
26957 void
26958 cp_pt_c_tests ()
26959 {
26960 test_build_non_dependent_expr ();
26961 test_type_dependent_expression_p ();
26962 }
26963
26964 } // namespace selftest
26965
26966 #endif /* #if CHECKING_P */
26967
26968 #include "gt-cp-pt.h"