[C++ PATCH] overloaded operator fns [8/N]
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44
45 /* The type of functions taking a tree, and some additional data, and
46 returning an int. */
47 typedef int (*tree_fn_t) (tree, void*);
48
49 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
50 instantiations have been deferred, either because their definitions
51 were not yet available, or because we were putting off doing the work. */
52 struct GTY ((chain_next ("%h.next"))) pending_template {
53 struct pending_template *next;
54 struct tinst_level *tinst;
55 };
56
57 static GTY(()) struct pending_template *pending_templates;
58 static GTY(()) struct pending_template *last_pending_template;
59
60 int processing_template_parmlist;
61 static int template_header_count;
62
63 static GTY(()) tree saved_trees;
64 static vec<int> inline_parm_levels;
65
66 static GTY(()) struct tinst_level *current_tinst_level;
67
68 static GTY(()) tree saved_access_scope;
69
70 /* Live only within one (recursive) call to tsubst_expr. We use
71 this to pass the statement expression node from the STMT_EXPR
72 to the EXPR_STMT that is its result. */
73 static tree cur_stmt_expr;
74
75 // -------------------------------------------------------------------------- //
76 // Local Specialization Stack
77 //
78 // Implementation of the RAII helper for creating new local
79 // specializations.
80 local_specialization_stack::local_specialization_stack (lss_policy policy)
81 : saved (local_specializations)
82 {
83 if (policy == lss_blank || !saved)
84 local_specializations = new hash_map<tree, tree>;
85 else
86 local_specializations = new hash_map<tree, tree>(*saved);
87 }
88
89 local_specialization_stack::~local_specialization_stack ()
90 {
91 delete local_specializations;
92 local_specializations = saved;
93 }
94
95 /* True if we've recursed into fn_type_unification too many times. */
96 static bool excessive_deduction_depth;
97
98 struct GTY((for_user)) spec_entry
99 {
100 tree tmpl;
101 tree args;
102 tree spec;
103 };
104
105 struct spec_hasher : ggc_ptr_hash<spec_entry>
106 {
107 static hashval_t hash (spec_entry *);
108 static bool equal (spec_entry *, spec_entry *);
109 };
110
111 static GTY (()) hash_table<spec_hasher> *decl_specializations;
112
113 static GTY (()) hash_table<spec_hasher> *type_specializations;
114
115 /* Contains canonical template parameter types. The vector is indexed by
116 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
117 TREE_LIST, whose TREE_VALUEs contain the canonical template
118 parameters of various types and levels. */
119 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
120
121 #define UNIFY_ALLOW_NONE 0
122 #define UNIFY_ALLOW_MORE_CV_QUAL 1
123 #define UNIFY_ALLOW_LESS_CV_QUAL 2
124 #define UNIFY_ALLOW_DERIVED 4
125 #define UNIFY_ALLOW_INTEGER 8
126 #define UNIFY_ALLOW_OUTER_LEVEL 16
127 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
128 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
129
130 enum template_base_result {
131 tbr_incomplete_type,
132 tbr_ambiguous_baseclass,
133 tbr_success
134 };
135
136 static void push_access_scope (tree);
137 static void pop_access_scope (tree);
138 static bool resolve_overloaded_unification (tree, tree, tree, tree,
139 unification_kind_t, int,
140 bool);
141 static int try_one_overload (tree, tree, tree, tree, tree,
142 unification_kind_t, int, bool, bool);
143 static int unify (tree, tree, tree, tree, int, bool);
144 static void add_pending_template (tree);
145 static tree reopen_tinst_level (struct tinst_level *);
146 static tree tsubst_initializer_list (tree, tree);
147 static tree get_partial_spec_bindings (tree, tree, tree);
148 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
149 bool, bool);
150 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static void tsubst_enum (tree, tree, tree);
153 static tree add_to_template_args (tree, tree);
154 static tree add_outermost_template_args (tree, tree);
155 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
156 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
157 tree);
158 static int type_unification_real (tree, tree, tree, const tree *,
159 unsigned int, int, unification_kind_t, int,
160 vec<deferred_access_check, va_gc> **,
161 bool);
162 static void note_template_header (int);
163 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
164 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
165 static tree convert_template_argument (tree, tree, tree,
166 tsubst_flags_t, int, tree);
167 static tree for_each_template_parm (tree, tree_fn_t, void*,
168 hash_set<tree> *, bool, tree_fn_t = NULL);
169 static tree expand_template_argument_pack (tree);
170 static tree build_template_parm_index (int, int, int, tree, tree);
171 static bool inline_needs_template_parms (tree, bool);
172 static void push_inline_template_parms_recursive (tree, int);
173 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
174 static int mark_template_parm (tree, void *);
175 static int template_parm_this_level_p (tree, void *);
176 static tree tsubst_friend_function (tree, tree);
177 static tree tsubst_friend_class (tree, tree);
178 static int can_complete_type_without_circularity (tree);
179 static tree get_bindings (tree, tree, tree, bool);
180 static int template_decl_level (tree);
181 static int check_cv_quals_for_unify (int, tree, tree);
182 static void template_parm_level_and_index (tree, int*, int*);
183 static int unify_pack_expansion (tree, tree, tree,
184 tree, unification_kind_t, bool, bool);
185 static tree copy_template_args (tree);
186 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
187 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
189 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
190 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
191 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
192 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
193 static bool check_specialization_scope (void);
194 static tree process_partial_specialization (tree);
195 static void set_current_access_from_decl (tree);
196 static enum template_base_result get_template_base (tree, tree, tree, tree,
197 bool , tree *);
198 static tree try_class_unification (tree, tree, tree, tree, bool);
199 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
200 tree, tree);
201 static bool template_template_parm_bindings_ok_p (tree, tree);
202 static void tsubst_default_arguments (tree, tsubst_flags_t);
203 static tree for_each_template_parm_r (tree *, int *, void *);
204 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
205 static void copy_default_args_to_explicit_spec (tree);
206 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
207 static bool dependent_template_arg_p (tree);
208 static bool any_template_arguments_need_structural_equality_p (tree);
209 static bool dependent_type_p_r (tree);
210 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
211 static tree tsubst_decl (tree, tree, tsubst_flags_t);
212 static void perform_typedefs_access_check (tree tmpl, tree targs);
213 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
214 location_t);
215 static tree listify (tree);
216 static tree listify_autos (tree, tree);
217 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
218 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
219 static bool complex_alias_template_p (const_tree tmpl);
220 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
221 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
222 static tree make_argument_pack (tree);
223 static void register_parameter_specializations (tree, tree);
224
225 /* Make the current scope suitable for access checking when we are
226 processing T. T can be FUNCTION_DECL for instantiated function
227 template, VAR_DECL for static member variable, or TYPE_DECL for
228 alias template (needed by instantiate_decl). */
229
230 static void
231 push_access_scope (tree t)
232 {
233 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
234 || TREE_CODE (t) == TYPE_DECL);
235
236 if (DECL_FRIEND_CONTEXT (t))
237 push_nested_class (DECL_FRIEND_CONTEXT (t));
238 else if (DECL_CLASS_SCOPE_P (t))
239 push_nested_class (DECL_CONTEXT (t));
240 else
241 push_to_top_level ();
242
243 if (TREE_CODE (t) == FUNCTION_DECL)
244 {
245 saved_access_scope = tree_cons
246 (NULL_TREE, current_function_decl, saved_access_scope);
247 current_function_decl = t;
248 }
249 }
250
251 /* Restore the scope set up by push_access_scope. T is the node we
252 are processing. */
253
254 static void
255 pop_access_scope (tree t)
256 {
257 if (TREE_CODE (t) == FUNCTION_DECL)
258 {
259 current_function_decl = TREE_VALUE (saved_access_scope);
260 saved_access_scope = TREE_CHAIN (saved_access_scope);
261 }
262
263 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
264 pop_nested_class ();
265 else
266 pop_from_top_level ();
267 }
268
269 /* Do any processing required when DECL (a member template
270 declaration) is finished. Returns the TEMPLATE_DECL corresponding
271 to DECL, unless it is a specialization, in which case the DECL
272 itself is returned. */
273
274 tree
275 finish_member_template_decl (tree decl)
276 {
277 if (decl == error_mark_node)
278 return error_mark_node;
279
280 gcc_assert (DECL_P (decl));
281
282 if (TREE_CODE (decl) == TYPE_DECL)
283 {
284 tree type;
285
286 type = TREE_TYPE (decl);
287 if (type == error_mark_node)
288 return error_mark_node;
289 if (MAYBE_CLASS_TYPE_P (type)
290 && CLASSTYPE_TEMPLATE_INFO (type)
291 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
292 {
293 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
294 check_member_template (tmpl);
295 return tmpl;
296 }
297 return NULL_TREE;
298 }
299 else if (TREE_CODE (decl) == FIELD_DECL)
300 error ("data member %qD cannot be a member template", decl);
301 else if (DECL_TEMPLATE_INFO (decl))
302 {
303 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
304 {
305 check_member_template (DECL_TI_TEMPLATE (decl));
306 return DECL_TI_TEMPLATE (decl);
307 }
308 else
309 return decl;
310 }
311 else
312 error ("invalid member template declaration %qD", decl);
313
314 return error_mark_node;
315 }
316
317 /* Create a template info node. */
318
319 tree
320 build_template_info (tree template_decl, tree template_args)
321 {
322 tree result = make_node (TEMPLATE_INFO);
323 TI_TEMPLATE (result) = template_decl;
324 TI_ARGS (result) = template_args;
325 return result;
326 }
327
328 /* Return the template info node corresponding to T, whatever T is. */
329
330 tree
331 get_template_info (const_tree t)
332 {
333 tree tinfo = NULL_TREE;
334
335 if (!t || t == error_mark_node)
336 return NULL;
337
338 if (TREE_CODE (t) == NAMESPACE_DECL
339 || TREE_CODE (t) == PARM_DECL)
340 return NULL;
341
342 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
343 tinfo = DECL_TEMPLATE_INFO (t);
344
345 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
346 t = TREE_TYPE (t);
347
348 if (OVERLOAD_TYPE_P (t))
349 tinfo = TYPE_TEMPLATE_INFO (t);
350 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
351 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
352
353 return tinfo;
354 }
355
356 /* Returns the template nesting level of the indicated class TYPE.
357
358 For example, in:
359 template <class T>
360 struct A
361 {
362 template <class U>
363 struct B {};
364 };
365
366 A<T>::B<U> has depth two, while A<T> has depth one.
367 Both A<T>::B<int> and A<int>::B<U> have depth one, if
368 they are instantiations, not specializations.
369
370 This function is guaranteed to return 0 if passed NULL_TREE so
371 that, for example, `template_class_depth (current_class_type)' is
372 always safe. */
373
374 int
375 template_class_depth (tree type)
376 {
377 int depth;
378
379 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
380 {
381 tree tinfo = get_template_info (type);
382
383 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
384 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
385 ++depth;
386
387 if (DECL_P (type))
388 type = CP_DECL_CONTEXT (type);
389 else if (LAMBDA_TYPE_P (type))
390 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
391 else
392 type = CP_TYPE_CONTEXT (type);
393 }
394
395 return depth;
396 }
397
398 /* Subroutine of maybe_begin_member_template_processing.
399 Returns true if processing DECL needs us to push template parms. */
400
401 static bool
402 inline_needs_template_parms (tree decl, bool nsdmi)
403 {
404 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
405 return false;
406
407 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
408 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
409 }
410
411 /* Subroutine of maybe_begin_member_template_processing.
412 Push the template parms in PARMS, starting from LEVELS steps into the
413 chain, and ending at the beginning, since template parms are listed
414 innermost first. */
415
416 static void
417 push_inline_template_parms_recursive (tree parmlist, int levels)
418 {
419 tree parms = TREE_VALUE (parmlist);
420 int i;
421
422 if (levels > 1)
423 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
424
425 ++processing_template_decl;
426 current_template_parms
427 = tree_cons (size_int (processing_template_decl),
428 parms, current_template_parms);
429 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
430
431 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
432 NULL);
433 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
434 {
435 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
436
437 if (error_operand_p (parm))
438 continue;
439
440 gcc_assert (DECL_P (parm));
441
442 switch (TREE_CODE (parm))
443 {
444 case TYPE_DECL:
445 case TEMPLATE_DECL:
446 pushdecl (parm);
447 break;
448
449 case PARM_DECL:
450 /* Push the CONST_DECL. */
451 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
452 break;
453
454 default:
455 gcc_unreachable ();
456 }
457 }
458 }
459
460 /* Restore the template parameter context for a member template, a
461 friend template defined in a class definition, or a non-template
462 member of template class. */
463
464 void
465 maybe_begin_member_template_processing (tree decl)
466 {
467 tree parms;
468 int levels = 0;
469 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
470
471 if (nsdmi)
472 {
473 tree ctx = DECL_CONTEXT (decl);
474 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
475 /* Disregard full specializations (c++/60999). */
476 && uses_template_parms (ctx)
477 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
478 }
479
480 if (inline_needs_template_parms (decl, nsdmi))
481 {
482 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
483 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
484
485 if (DECL_TEMPLATE_SPECIALIZATION (decl))
486 {
487 --levels;
488 parms = TREE_CHAIN (parms);
489 }
490
491 push_inline_template_parms_recursive (parms, levels);
492 }
493
494 /* Remember how many levels of template parameters we pushed so that
495 we can pop them later. */
496 inline_parm_levels.safe_push (levels);
497 }
498
499 /* Undo the effects of maybe_begin_member_template_processing. */
500
501 void
502 maybe_end_member_template_processing (void)
503 {
504 int i;
505 int last;
506
507 if (inline_parm_levels.length () == 0)
508 return;
509
510 last = inline_parm_levels.pop ();
511 for (i = 0; i < last; ++i)
512 {
513 --processing_template_decl;
514 current_template_parms = TREE_CHAIN (current_template_parms);
515 poplevel (0, 0, 0);
516 }
517 }
518
519 /* Return a new template argument vector which contains all of ARGS,
520 but has as its innermost set of arguments the EXTRA_ARGS. */
521
522 static tree
523 add_to_template_args (tree args, tree extra_args)
524 {
525 tree new_args;
526 int extra_depth;
527 int i;
528 int j;
529
530 if (args == NULL_TREE || extra_args == error_mark_node)
531 return extra_args;
532
533 extra_depth = TMPL_ARGS_DEPTH (extra_args);
534 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
535
536 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
537 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
538
539 for (j = 1; j <= extra_depth; ++j, ++i)
540 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
541
542 return new_args;
543 }
544
545 /* Like add_to_template_args, but only the outermost ARGS are added to
546 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
547 (EXTRA_ARGS) levels are added. This function is used to combine
548 the template arguments from a partial instantiation with the
549 template arguments used to attain the full instantiation from the
550 partial instantiation. */
551
552 static tree
553 add_outermost_template_args (tree args, tree extra_args)
554 {
555 tree new_args;
556
557 /* If there are more levels of EXTRA_ARGS than there are ARGS,
558 something very fishy is going on. */
559 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
560
561 /* If *all* the new arguments will be the EXTRA_ARGS, just return
562 them. */
563 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
564 return extra_args;
565
566 /* For the moment, we make ARGS look like it contains fewer levels. */
567 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
568
569 new_args = add_to_template_args (args, extra_args);
570
571 /* Now, we restore ARGS to its full dimensions. */
572 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
573
574 return new_args;
575 }
576
577 /* Return the N levels of innermost template arguments from the ARGS. */
578
579 tree
580 get_innermost_template_args (tree args, int n)
581 {
582 tree new_args;
583 int extra_levels;
584 int i;
585
586 gcc_assert (n >= 0);
587
588 /* If N is 1, just return the innermost set of template arguments. */
589 if (n == 1)
590 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
591
592 /* If we're not removing anything, just return the arguments we were
593 given. */
594 extra_levels = TMPL_ARGS_DEPTH (args) - n;
595 gcc_assert (extra_levels >= 0);
596 if (extra_levels == 0)
597 return args;
598
599 /* Make a new set of arguments, not containing the outer arguments. */
600 new_args = make_tree_vec (n);
601 for (i = 1; i <= n; ++i)
602 SET_TMPL_ARGS_LEVEL (new_args, i,
603 TMPL_ARGS_LEVEL (args, i + extra_levels));
604
605 return new_args;
606 }
607
608 /* The inverse of get_innermost_template_args: Return all but the innermost
609 EXTRA_LEVELS levels of template arguments from the ARGS. */
610
611 static tree
612 strip_innermost_template_args (tree args, int extra_levels)
613 {
614 tree new_args;
615 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
616 int i;
617
618 gcc_assert (n >= 0);
619
620 /* If N is 1, just return the outermost set of template arguments. */
621 if (n == 1)
622 return TMPL_ARGS_LEVEL (args, 1);
623
624 /* If we're not removing anything, just return the arguments we were
625 given. */
626 gcc_assert (extra_levels >= 0);
627 if (extra_levels == 0)
628 return args;
629
630 /* Make a new set of arguments, not containing the inner arguments. */
631 new_args = make_tree_vec (n);
632 for (i = 1; i <= n; ++i)
633 SET_TMPL_ARGS_LEVEL (new_args, i,
634 TMPL_ARGS_LEVEL (args, i));
635
636 return new_args;
637 }
638
639 /* We've got a template header coming up; push to a new level for storing
640 the parms. */
641
642 void
643 begin_template_parm_list (void)
644 {
645 /* We use a non-tag-transparent scope here, which causes pushtag to
646 put tags in this scope, rather than in the enclosing class or
647 namespace scope. This is the right thing, since we want
648 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
649 global template class, push_template_decl handles putting the
650 TEMPLATE_DECL into top-level scope. For a nested template class,
651 e.g.:
652
653 template <class T> struct S1 {
654 template <class T> struct S2 {};
655 };
656
657 pushtag contains special code to insert the TEMPLATE_DECL for S2
658 at the right scope. */
659 begin_scope (sk_template_parms, NULL);
660 ++processing_template_decl;
661 ++processing_template_parmlist;
662 note_template_header (0);
663
664 /* Add a dummy parameter level while we process the parameter list. */
665 current_template_parms
666 = tree_cons (size_int (processing_template_decl),
667 make_tree_vec (0),
668 current_template_parms);
669 }
670
671 /* This routine is called when a specialization is declared. If it is
672 invalid to declare a specialization here, an error is reported and
673 false is returned, otherwise this routine will return true. */
674
675 static bool
676 check_specialization_scope (void)
677 {
678 tree scope = current_scope ();
679
680 /* [temp.expl.spec]
681
682 An explicit specialization shall be declared in the namespace of
683 which the template is a member, or, for member templates, in the
684 namespace of which the enclosing class or enclosing class
685 template is a member. An explicit specialization of a member
686 function, member class or static data member of a class template
687 shall be declared in the namespace of which the class template
688 is a member. */
689 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
690 {
691 error ("explicit specialization in non-namespace scope %qD", scope);
692 return false;
693 }
694
695 /* [temp.expl.spec]
696
697 In an explicit specialization declaration for a member of a class
698 template or a member template that appears in namespace scope,
699 the member template and some of its enclosing class templates may
700 remain unspecialized, except that the declaration shall not
701 explicitly specialize a class member template if its enclosing
702 class templates are not explicitly specialized as well. */
703 if (current_template_parms)
704 {
705 error ("enclosing class templates are not explicitly specialized");
706 return false;
707 }
708
709 return true;
710 }
711
712 /* We've just seen template <>. */
713
714 bool
715 begin_specialization (void)
716 {
717 begin_scope (sk_template_spec, NULL);
718 note_template_header (1);
719 return check_specialization_scope ();
720 }
721
722 /* Called at then end of processing a declaration preceded by
723 template<>. */
724
725 void
726 end_specialization (void)
727 {
728 finish_scope ();
729 reset_specialization ();
730 }
731
732 /* Any template <>'s that we have seen thus far are not referring to a
733 function specialization. */
734
735 void
736 reset_specialization (void)
737 {
738 processing_specialization = 0;
739 template_header_count = 0;
740 }
741
742 /* We've just seen a template header. If SPECIALIZATION is nonzero,
743 it was of the form template <>. */
744
745 static void
746 note_template_header (int specialization)
747 {
748 processing_specialization = specialization;
749 template_header_count++;
750 }
751
752 /* We're beginning an explicit instantiation. */
753
754 void
755 begin_explicit_instantiation (void)
756 {
757 gcc_assert (!processing_explicit_instantiation);
758 processing_explicit_instantiation = true;
759 }
760
761
762 void
763 end_explicit_instantiation (void)
764 {
765 gcc_assert (processing_explicit_instantiation);
766 processing_explicit_instantiation = false;
767 }
768
769 /* An explicit specialization or partial specialization of TMPL is being
770 declared. Check that the namespace in which the specialization is
771 occurring is permissible. Returns false iff it is invalid to
772 specialize TMPL in the current namespace. */
773
774 static bool
775 check_specialization_namespace (tree tmpl)
776 {
777 tree tpl_ns = decl_namespace_context (tmpl);
778
779 /* [tmpl.expl.spec]
780
781 An explicit specialization shall be declared in a namespace enclosing the
782 specialized template. An explicit specialization whose declarator-id is
783 not qualified shall be declared in the nearest enclosing namespace of the
784 template, or, if the namespace is inline (7.3.1), any namespace from its
785 enclosing namespace set. */
786 if (current_scope() != DECL_CONTEXT (tmpl)
787 && !at_namespace_scope_p ())
788 {
789 error ("specialization of %qD must appear at namespace scope", tmpl);
790 return false;
791 }
792
793 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
794 /* Same or enclosing namespace. */
795 return true;
796 else
797 {
798 permerror (input_location,
799 "specialization of %qD in different namespace", tmpl);
800 inform (DECL_SOURCE_LOCATION (tmpl),
801 " from definition of %q#D", tmpl);
802 return false;
803 }
804 }
805
806 /* SPEC is an explicit instantiation. Check that it is valid to
807 perform this explicit instantiation in the current namespace. */
808
809 static void
810 check_explicit_instantiation_namespace (tree spec)
811 {
812 tree ns;
813
814 /* DR 275: An explicit instantiation shall appear in an enclosing
815 namespace of its template. */
816 ns = decl_namespace_context (spec);
817 if (!is_nested_namespace (current_namespace, ns))
818 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
819 "(which does not enclose namespace %qD)",
820 spec, current_namespace, ns);
821 }
822
823 // Returns the type of a template specialization only if that
824 // specialization needs to be defined. Otherwise (e.g., if the type has
825 // already been defined), the function returns NULL_TREE.
826 static tree
827 maybe_new_partial_specialization (tree type)
828 {
829 // An implicit instantiation of an incomplete type implies
830 // the definition of a new class template.
831 //
832 // template<typename T>
833 // struct S;
834 //
835 // template<typename T>
836 // struct S<T*>;
837 //
838 // Here, S<T*> is an implicit instantiation of S whose type
839 // is incomplete.
840 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
841 return type;
842
843 // It can also be the case that TYPE is a completed specialization.
844 // Continuing the previous example, suppose we also declare:
845 //
846 // template<typename T>
847 // requires Integral<T>
848 // struct S<T*>;
849 //
850 // Here, S<T*> refers to the specialization S<T*> defined
851 // above. However, we need to differentiate definitions because
852 // we intend to define a new partial specialization. In this case,
853 // we rely on the fact that the constraints are different for
854 // this declaration than that above.
855 //
856 // Note that we also get here for injected class names and
857 // late-parsed template definitions. We must ensure that we
858 // do not create new type declarations for those cases.
859 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
860 {
861 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
862 tree args = CLASSTYPE_TI_ARGS (type);
863
864 // If there are no template parameters, this cannot be a new
865 // partial template specializtion?
866 if (!current_template_parms)
867 return NULL_TREE;
868
869 // The injected-class-name is not a new partial specialization.
870 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
871 return NULL_TREE;
872
873 // If the constraints are not the same as those of the primary
874 // then, we can probably create a new specialization.
875 tree type_constr = current_template_constraints ();
876
877 if (type == TREE_TYPE (tmpl))
878 {
879 tree main_constr = get_constraints (tmpl);
880 if (equivalent_constraints (type_constr, main_constr))
881 return NULL_TREE;
882 }
883
884 // Also, if there's a pre-existing specialization with matching
885 // constraints, then this also isn't new.
886 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
887 while (specs)
888 {
889 tree spec_tmpl = TREE_VALUE (specs);
890 tree spec_args = TREE_PURPOSE (specs);
891 tree spec_constr = get_constraints (spec_tmpl);
892 if (comp_template_args (args, spec_args)
893 && equivalent_constraints (type_constr, spec_constr))
894 return NULL_TREE;
895 specs = TREE_CHAIN (specs);
896 }
897
898 // Create a new type node (and corresponding type decl)
899 // for the newly declared specialization.
900 tree t = make_class_type (TREE_CODE (type));
901 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
902 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
903
904 /* We only need a separate type node for storing the definition of this
905 partial specialization; uses of S<T*> are unconstrained, so all are
906 equivalent. So keep TYPE_CANONICAL the same. */
907 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
908
909 // Build the corresponding type decl.
910 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
911 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
912 DECL_SOURCE_LOCATION (d) = input_location;
913
914 return t;
915 }
916
917 return NULL_TREE;
918 }
919
920 /* The TYPE is being declared. If it is a template type, that means it
921 is a partial specialization. Do appropriate error-checking. */
922
923 tree
924 maybe_process_partial_specialization (tree type)
925 {
926 tree context;
927
928 if (type == error_mark_node)
929 return error_mark_node;
930
931 /* A lambda that appears in specialization context is not itself a
932 specialization. */
933 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
934 return type;
935
936 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
937 {
938 error ("name of class shadows template template parameter %qD",
939 TYPE_NAME (type));
940 return error_mark_node;
941 }
942
943 context = TYPE_CONTEXT (type);
944
945 if (TYPE_ALIAS_P (type))
946 {
947 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
948
949 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
950 error ("specialization of alias template %qD",
951 TI_TEMPLATE (tinfo));
952 else
953 error ("explicit specialization of non-template %qT", type);
954 return error_mark_node;
955 }
956 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
957 {
958 /* This is for ordinary explicit specialization and partial
959 specialization of a template class such as:
960
961 template <> class C<int>;
962
963 or:
964
965 template <class T> class C<T*>;
966
967 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
968
969 if (tree t = maybe_new_partial_specialization (type))
970 {
971 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
972 && !at_namespace_scope_p ())
973 return error_mark_node;
974 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
975 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
976 if (processing_template_decl)
977 {
978 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
979 if (decl == error_mark_node)
980 return error_mark_node;
981 return TREE_TYPE (decl);
982 }
983 }
984 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
985 error ("specialization of %qT after instantiation", type);
986 else if (errorcount && !processing_specialization
987 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
988 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
989 /* Trying to define a specialization either without a template<> header
990 or in an inappropriate place. We've already given an error, so just
991 bail now so we don't actually define the specialization. */
992 return error_mark_node;
993 }
994 else if (CLASS_TYPE_P (type)
995 && !CLASSTYPE_USE_TEMPLATE (type)
996 && CLASSTYPE_TEMPLATE_INFO (type)
997 && context && CLASS_TYPE_P (context)
998 && CLASSTYPE_TEMPLATE_INFO (context))
999 {
1000 /* This is for an explicit specialization of member class
1001 template according to [temp.expl.spec/18]:
1002
1003 template <> template <class U> class C<int>::D;
1004
1005 The context `C<int>' must be an implicit instantiation.
1006 Otherwise this is just a member class template declared
1007 earlier like:
1008
1009 template <> class C<int> { template <class U> class D; };
1010 template <> template <class U> class C<int>::D;
1011
1012 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1013 while in the second case, `C<int>::D' is a primary template
1014 and `C<T>::D' may not exist. */
1015
1016 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1017 && !COMPLETE_TYPE_P (type))
1018 {
1019 tree t;
1020 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1021
1022 if (current_namespace
1023 != decl_namespace_context (tmpl))
1024 {
1025 permerror (input_location,
1026 "specializing %q#T in different namespace", type);
1027 permerror (DECL_SOURCE_LOCATION (tmpl),
1028 " from definition of %q#D", tmpl);
1029 }
1030
1031 /* Check for invalid specialization after instantiation:
1032
1033 template <> template <> class C<int>::D<int>;
1034 template <> template <class U> class C<int>::D; */
1035
1036 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1037 t; t = TREE_CHAIN (t))
1038 {
1039 tree inst = TREE_VALUE (t);
1040 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1041 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1042 {
1043 /* We already have a full specialization of this partial
1044 instantiation, or a full specialization has been
1045 looked up but not instantiated. Reassign it to the
1046 new member specialization template. */
1047 spec_entry elt;
1048 spec_entry *entry;
1049
1050 elt.tmpl = most_general_template (tmpl);
1051 elt.args = CLASSTYPE_TI_ARGS (inst);
1052 elt.spec = inst;
1053
1054 type_specializations->remove_elt (&elt);
1055
1056 elt.tmpl = tmpl;
1057 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1058
1059 spec_entry **slot
1060 = type_specializations->find_slot (&elt, INSERT);
1061 entry = ggc_alloc<spec_entry> ();
1062 *entry = elt;
1063 *slot = entry;
1064 }
1065 else
1066 /* But if we've had an implicit instantiation, that's a
1067 problem ([temp.expl.spec]/6). */
1068 error ("specialization %qT after instantiation %qT",
1069 type, inst);
1070 }
1071
1072 /* Mark TYPE as a specialization. And as a result, we only
1073 have one level of template argument for the innermost
1074 class template. */
1075 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1076 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1077 CLASSTYPE_TI_ARGS (type)
1078 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1079 }
1080 }
1081 else if (processing_specialization)
1082 {
1083 /* Someday C++0x may allow for enum template specialization. */
1084 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1085 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1086 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1087 "of %qD not allowed by ISO C++", type);
1088 else
1089 {
1090 error ("explicit specialization of non-template %qT", type);
1091 return error_mark_node;
1092 }
1093 }
1094
1095 return type;
1096 }
1097
1098 /* Returns nonzero if we can optimize the retrieval of specializations
1099 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1100 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1101
1102 static inline bool
1103 optimize_specialization_lookup_p (tree tmpl)
1104 {
1105 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1106 && DECL_CLASS_SCOPE_P (tmpl)
1107 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1108 parameter. */
1109 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1110 /* The optimized lookup depends on the fact that the
1111 template arguments for the member function template apply
1112 purely to the containing class, which is not true if the
1113 containing class is an explicit or partial
1114 specialization. */
1115 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1116 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1117 && !DECL_CONV_FN_P (tmpl)
1118 /* It is possible to have a template that is not a member
1119 template and is not a member of a template class:
1120
1121 template <typename T>
1122 struct S { friend A::f(); };
1123
1124 Here, the friend function is a template, but the context does
1125 not have template information. The optimized lookup relies
1126 on having ARGS be the template arguments for both the class
1127 and the function template. */
1128 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1129 }
1130
1131 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1132 gone through coerce_template_parms by now. */
1133
1134 static void
1135 verify_unstripped_args (tree args)
1136 {
1137 ++processing_template_decl;
1138 if (!any_dependent_template_arguments_p (args))
1139 {
1140 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1141 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1142 {
1143 tree arg = TREE_VEC_ELT (inner, i);
1144 if (TREE_CODE (arg) == TEMPLATE_DECL)
1145 /* OK */;
1146 else if (TYPE_P (arg))
1147 gcc_assert (strip_typedefs (arg, NULL) == arg);
1148 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1149 /* Allow typedefs on the type of a non-type argument, since a
1150 parameter can have them. */;
1151 else
1152 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1153 }
1154 }
1155 --processing_template_decl;
1156 }
1157
1158 /* Retrieve the specialization (in the sense of [temp.spec] - a
1159 specialization is either an instantiation or an explicit
1160 specialization) of TMPL for the given template ARGS. If there is
1161 no such specialization, return NULL_TREE. The ARGS are a vector of
1162 arguments, or a vector of vectors of arguments, in the case of
1163 templates with more than one level of parameters.
1164
1165 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1166 then we search for a partial specialization matching ARGS. This
1167 parameter is ignored if TMPL is not a class template.
1168
1169 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1170 result is a NONTYPE_ARGUMENT_PACK. */
1171
1172 static tree
1173 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1174 {
1175 if (tmpl == NULL_TREE)
1176 return NULL_TREE;
1177
1178 if (args == error_mark_node)
1179 return NULL_TREE;
1180
1181 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1182 || TREE_CODE (tmpl) == FIELD_DECL);
1183
1184 /* There should be as many levels of arguments as there are
1185 levels of parameters. */
1186 gcc_assert (TMPL_ARGS_DEPTH (args)
1187 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1188 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1189 : template_class_depth (DECL_CONTEXT (tmpl))));
1190
1191 if (flag_checking)
1192 verify_unstripped_args (args);
1193
1194 /* Lambda functions in templates aren't instantiated normally, but through
1195 tsubst_lambda_expr. */
1196 if (lambda_fn_in_template_p (tmpl))
1197 return NULL_TREE;
1198
1199 if (optimize_specialization_lookup_p (tmpl))
1200 {
1201 /* The template arguments actually apply to the containing
1202 class. Find the class specialization with those
1203 arguments. */
1204 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1205 tree class_specialization
1206 = retrieve_specialization (class_template, args, 0);
1207 if (!class_specialization)
1208 return NULL_TREE;
1209
1210 /* Find the instance of TMPL. */
1211 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1212 for (ovl_iterator iter (fns); iter; ++iter)
1213 {
1214 tree fn = *iter;
1215 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1216 /* using-declarations can add base methods to the method vec,
1217 and we don't want those here. */
1218 && DECL_CONTEXT (fn) == class_specialization)
1219 return fn;
1220 }
1221 return NULL_TREE;
1222 }
1223 else
1224 {
1225 spec_entry *found;
1226 spec_entry elt;
1227 hash_table<spec_hasher> *specializations;
1228
1229 elt.tmpl = tmpl;
1230 elt.args = args;
1231 elt.spec = NULL_TREE;
1232
1233 if (DECL_CLASS_TEMPLATE_P (tmpl))
1234 specializations = type_specializations;
1235 else
1236 specializations = decl_specializations;
1237
1238 if (hash == 0)
1239 hash = spec_hasher::hash (&elt);
1240 found = specializations->find_with_hash (&elt, hash);
1241 if (found)
1242 return found->spec;
1243 }
1244
1245 return NULL_TREE;
1246 }
1247
1248 /* Like retrieve_specialization, but for local declarations. */
1249
1250 tree
1251 retrieve_local_specialization (tree tmpl)
1252 {
1253 if (local_specializations == NULL)
1254 return NULL_TREE;
1255
1256 tree *slot = local_specializations->get (tmpl);
1257 return slot ? *slot : NULL_TREE;
1258 }
1259
1260 /* Returns nonzero iff DECL is a specialization of TMPL. */
1261
1262 int
1263 is_specialization_of (tree decl, tree tmpl)
1264 {
1265 tree t;
1266
1267 if (TREE_CODE (decl) == FUNCTION_DECL)
1268 {
1269 for (t = decl;
1270 t != NULL_TREE;
1271 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1272 if (t == tmpl)
1273 return 1;
1274 }
1275 else
1276 {
1277 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1278
1279 for (t = TREE_TYPE (decl);
1280 t != NULL_TREE;
1281 t = CLASSTYPE_USE_TEMPLATE (t)
1282 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1283 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1284 return 1;
1285 }
1286
1287 return 0;
1288 }
1289
1290 /* Returns nonzero iff DECL is a specialization of friend declaration
1291 FRIEND_DECL according to [temp.friend]. */
1292
1293 bool
1294 is_specialization_of_friend (tree decl, tree friend_decl)
1295 {
1296 bool need_template = true;
1297 int template_depth;
1298
1299 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1300 || TREE_CODE (decl) == TYPE_DECL);
1301
1302 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1303 of a template class, we want to check if DECL is a specialization
1304 if this. */
1305 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1306 && DECL_TEMPLATE_INFO (friend_decl)
1307 && !DECL_USE_TEMPLATE (friend_decl))
1308 {
1309 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1310 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1311 need_template = false;
1312 }
1313 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1314 && !PRIMARY_TEMPLATE_P (friend_decl))
1315 need_template = false;
1316
1317 /* There is nothing to do if this is not a template friend. */
1318 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1319 return false;
1320
1321 if (is_specialization_of (decl, friend_decl))
1322 return true;
1323
1324 /* [temp.friend/6]
1325 A member of a class template may be declared to be a friend of a
1326 non-template class. In this case, the corresponding member of
1327 every specialization of the class template is a friend of the
1328 class granting friendship.
1329
1330 For example, given a template friend declaration
1331
1332 template <class T> friend void A<T>::f();
1333
1334 the member function below is considered a friend
1335
1336 template <> struct A<int> {
1337 void f();
1338 };
1339
1340 For this type of template friend, TEMPLATE_DEPTH below will be
1341 nonzero. To determine if DECL is a friend of FRIEND, we first
1342 check if the enclosing class is a specialization of another. */
1343
1344 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1345 if (template_depth
1346 && DECL_CLASS_SCOPE_P (decl)
1347 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1348 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1349 {
1350 /* Next, we check the members themselves. In order to handle
1351 a few tricky cases, such as when FRIEND_DECL's are
1352
1353 template <class T> friend void A<T>::g(T t);
1354 template <class T> template <T t> friend void A<T>::h();
1355
1356 and DECL's are
1357
1358 void A<int>::g(int);
1359 template <int> void A<int>::h();
1360
1361 we need to figure out ARGS, the template arguments from
1362 the context of DECL. This is required for template substitution
1363 of `T' in the function parameter of `g' and template parameter
1364 of `h' in the above examples. Here ARGS corresponds to `int'. */
1365
1366 tree context = DECL_CONTEXT (decl);
1367 tree args = NULL_TREE;
1368 int current_depth = 0;
1369
1370 while (current_depth < template_depth)
1371 {
1372 if (CLASSTYPE_TEMPLATE_INFO (context))
1373 {
1374 if (current_depth == 0)
1375 args = TYPE_TI_ARGS (context);
1376 else
1377 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1378 current_depth++;
1379 }
1380 context = TYPE_CONTEXT (context);
1381 }
1382
1383 if (TREE_CODE (decl) == FUNCTION_DECL)
1384 {
1385 bool is_template;
1386 tree friend_type;
1387 tree decl_type;
1388 tree friend_args_type;
1389 tree decl_args_type;
1390
1391 /* Make sure that both DECL and FRIEND_DECL are templates or
1392 non-templates. */
1393 is_template = DECL_TEMPLATE_INFO (decl)
1394 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1395 if (need_template ^ is_template)
1396 return false;
1397 else if (is_template)
1398 {
1399 /* If both are templates, check template parameter list. */
1400 tree friend_parms
1401 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1402 args, tf_none);
1403 if (!comp_template_parms
1404 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1405 friend_parms))
1406 return false;
1407
1408 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1409 }
1410 else
1411 decl_type = TREE_TYPE (decl);
1412
1413 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1414 tf_none, NULL_TREE);
1415 if (friend_type == error_mark_node)
1416 return false;
1417
1418 /* Check if return types match. */
1419 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1420 return false;
1421
1422 /* Check if function parameter types match, ignoring the
1423 `this' parameter. */
1424 friend_args_type = TYPE_ARG_TYPES (friend_type);
1425 decl_args_type = TYPE_ARG_TYPES (decl_type);
1426 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1427 friend_args_type = TREE_CHAIN (friend_args_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1429 decl_args_type = TREE_CHAIN (decl_args_type);
1430
1431 return compparms (decl_args_type, friend_args_type);
1432 }
1433 else
1434 {
1435 /* DECL is a TYPE_DECL */
1436 bool is_template;
1437 tree decl_type = TREE_TYPE (decl);
1438
1439 /* Make sure that both DECL and FRIEND_DECL are templates or
1440 non-templates. */
1441 is_template
1442 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1443 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1444
1445 if (need_template ^ is_template)
1446 return false;
1447 else if (is_template)
1448 {
1449 tree friend_parms;
1450 /* If both are templates, check the name of the two
1451 TEMPLATE_DECL's first because is_friend didn't. */
1452 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1453 != DECL_NAME (friend_decl))
1454 return false;
1455
1456 /* Now check template parameter list. */
1457 friend_parms
1458 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1459 args, tf_none);
1460 return comp_template_parms
1461 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1462 friend_parms);
1463 }
1464 else
1465 return (DECL_NAME (decl)
1466 == DECL_NAME (friend_decl));
1467 }
1468 }
1469 return false;
1470 }
1471
1472 /* Register the specialization SPEC as a specialization of TMPL with
1473 the indicated ARGS. IS_FRIEND indicates whether the specialization
1474 is actually just a friend declaration. Returns SPEC, or an
1475 equivalent prior declaration, if available.
1476
1477 We also store instantiations of field packs in the hash table, even
1478 though they are not themselves templates, to make lookup easier. */
1479
1480 static tree
1481 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1482 hashval_t hash)
1483 {
1484 tree fn;
1485 spec_entry **slot = NULL;
1486 spec_entry elt;
1487
1488 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1489 || (TREE_CODE (tmpl) == FIELD_DECL
1490 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1491
1492 if (TREE_CODE (spec) == FUNCTION_DECL
1493 && uses_template_parms (DECL_TI_ARGS (spec)))
1494 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1495 register it; we want the corresponding TEMPLATE_DECL instead.
1496 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1497 the more obvious `uses_template_parms (spec)' to avoid problems
1498 with default function arguments. In particular, given
1499 something like this:
1500
1501 template <class T> void f(T t1, T t = T())
1502
1503 the default argument expression is not substituted for in an
1504 instantiation unless and until it is actually needed. */
1505 return spec;
1506
1507 if (optimize_specialization_lookup_p (tmpl))
1508 /* We don't put these specializations in the hash table, but we might
1509 want to give an error about a mismatch. */
1510 fn = retrieve_specialization (tmpl, args, 0);
1511 else
1512 {
1513 elt.tmpl = tmpl;
1514 elt.args = args;
1515 elt.spec = spec;
1516
1517 if (hash == 0)
1518 hash = spec_hasher::hash (&elt);
1519
1520 slot =
1521 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1522 if (*slot)
1523 fn = ((spec_entry *) *slot)->spec;
1524 else
1525 fn = NULL_TREE;
1526 }
1527
1528 /* We can sometimes try to re-register a specialization that we've
1529 already got. In particular, regenerate_decl_from_template calls
1530 duplicate_decls which will update the specialization list. But,
1531 we'll still get called again here anyhow. It's more convenient
1532 to simply allow this than to try to prevent it. */
1533 if (fn == spec)
1534 return spec;
1535 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1536 {
1537 if (DECL_TEMPLATE_INSTANTIATION (fn))
1538 {
1539 if (DECL_ODR_USED (fn)
1540 || DECL_EXPLICIT_INSTANTIATION (fn))
1541 {
1542 error ("specialization of %qD after instantiation",
1543 fn);
1544 return error_mark_node;
1545 }
1546 else
1547 {
1548 tree clone;
1549 /* This situation should occur only if the first
1550 specialization is an implicit instantiation, the
1551 second is an explicit specialization, and the
1552 implicit instantiation has not yet been used. That
1553 situation can occur if we have implicitly
1554 instantiated a member function and then specialized
1555 it later.
1556
1557 We can also wind up here if a friend declaration that
1558 looked like an instantiation turns out to be a
1559 specialization:
1560
1561 template <class T> void foo(T);
1562 class S { friend void foo<>(int) };
1563 template <> void foo(int);
1564
1565 We transform the existing DECL in place so that any
1566 pointers to it become pointers to the updated
1567 declaration.
1568
1569 If there was a definition for the template, but not
1570 for the specialization, we want this to look as if
1571 there were no definition, and vice versa. */
1572 DECL_INITIAL (fn) = NULL_TREE;
1573 duplicate_decls (spec, fn, is_friend);
1574 /* The call to duplicate_decls will have applied
1575 [temp.expl.spec]:
1576
1577 An explicit specialization of a function template
1578 is inline only if it is explicitly declared to be,
1579 and independently of whether its function template
1580 is.
1581
1582 to the primary function; now copy the inline bits to
1583 the various clones. */
1584 FOR_EACH_CLONE (clone, fn)
1585 {
1586 DECL_DECLARED_INLINE_P (clone)
1587 = DECL_DECLARED_INLINE_P (fn);
1588 DECL_SOURCE_LOCATION (clone)
1589 = DECL_SOURCE_LOCATION (fn);
1590 DECL_DELETED_FN (clone)
1591 = DECL_DELETED_FN (fn);
1592 }
1593 check_specialization_namespace (tmpl);
1594
1595 return fn;
1596 }
1597 }
1598 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1599 {
1600 tree dd = duplicate_decls (spec, fn, is_friend);
1601 if (dd == error_mark_node)
1602 /* We've already complained in duplicate_decls. */
1603 return error_mark_node;
1604
1605 if (dd == NULL_TREE && DECL_INITIAL (spec))
1606 /* Dup decl failed, but this is a new definition. Set the
1607 line number so any errors match this new
1608 definition. */
1609 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1610
1611 return fn;
1612 }
1613 }
1614 else if (fn)
1615 return duplicate_decls (spec, fn, is_friend);
1616
1617 /* A specialization must be declared in the same namespace as the
1618 template it is specializing. */
1619 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1620 && !check_specialization_namespace (tmpl))
1621 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1622
1623 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1624 {
1625 spec_entry *entry = ggc_alloc<spec_entry> ();
1626 gcc_assert (tmpl && args && spec);
1627 *entry = elt;
1628 *slot = entry;
1629 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1630 && PRIMARY_TEMPLATE_P (tmpl)
1631 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1632 || variable_template_p (tmpl))
1633 /* If TMPL is a forward declaration of a template function, keep a list
1634 of all specializations in case we need to reassign them to a friend
1635 template later in tsubst_friend_function.
1636
1637 Also keep a list of all variable template instantiations so that
1638 process_partial_specialization can check whether a later partial
1639 specialization would have used it. */
1640 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1641 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1642 }
1643
1644 return spec;
1645 }
1646
1647 /* Returns true iff two spec_entry nodes are equivalent. */
1648
1649 int comparing_specializations;
1650
1651 bool
1652 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1653 {
1654 int equal;
1655
1656 ++comparing_specializations;
1657 equal = (e1->tmpl == e2->tmpl
1658 && comp_template_args (e1->args, e2->args));
1659 if (equal && flag_concepts
1660 /* tmpl could be a FIELD_DECL for a capture pack. */
1661 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1662 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1663 && uses_template_parms (e1->args))
1664 {
1665 /* Partial specializations of a variable template can be distinguished by
1666 constraints. */
1667 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1668 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1669 equal = equivalent_constraints (c1, c2);
1670 }
1671 --comparing_specializations;
1672
1673 return equal;
1674 }
1675
1676 /* Returns a hash for a template TMPL and template arguments ARGS. */
1677
1678 static hashval_t
1679 hash_tmpl_and_args (tree tmpl, tree args)
1680 {
1681 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1682 return iterative_hash_template_arg (args, val);
1683 }
1684
1685 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1686 ignoring SPEC. */
1687
1688 hashval_t
1689 spec_hasher::hash (spec_entry *e)
1690 {
1691 return hash_tmpl_and_args (e->tmpl, e->args);
1692 }
1693
1694 /* Recursively calculate a hash value for a template argument ARG, for use
1695 in the hash tables of template specializations. */
1696
1697 hashval_t
1698 iterative_hash_template_arg (tree arg, hashval_t val)
1699 {
1700 unsigned HOST_WIDE_INT i;
1701 enum tree_code code;
1702 char tclass;
1703
1704 if (arg == NULL_TREE)
1705 return iterative_hash_object (arg, val);
1706
1707 if (!TYPE_P (arg))
1708 STRIP_NOPS (arg);
1709
1710 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1711 gcc_unreachable ();
1712
1713 code = TREE_CODE (arg);
1714 tclass = TREE_CODE_CLASS (code);
1715
1716 val = iterative_hash_object (code, val);
1717
1718 switch (code)
1719 {
1720 case ERROR_MARK:
1721 return val;
1722
1723 case IDENTIFIER_NODE:
1724 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1725
1726 case TREE_VEC:
1727 {
1728 int i, len = TREE_VEC_LENGTH (arg);
1729 for (i = 0; i < len; ++i)
1730 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1731 return val;
1732 }
1733
1734 case TYPE_PACK_EXPANSION:
1735 case EXPR_PACK_EXPANSION:
1736 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1737 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1738
1739 case TYPE_ARGUMENT_PACK:
1740 case NONTYPE_ARGUMENT_PACK:
1741 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1742
1743 case TREE_LIST:
1744 for (; arg; arg = TREE_CHAIN (arg))
1745 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1746 return val;
1747
1748 case OVERLOAD:
1749 for (lkp_iterator iter (arg); iter; ++iter)
1750 val = iterative_hash_template_arg (*iter, val);
1751 return val;
1752
1753 case CONSTRUCTOR:
1754 {
1755 tree field, value;
1756 iterative_hash_template_arg (TREE_TYPE (arg), val);
1757 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1758 {
1759 val = iterative_hash_template_arg (field, val);
1760 val = iterative_hash_template_arg (value, val);
1761 }
1762 return val;
1763 }
1764
1765 case PARM_DECL:
1766 if (!DECL_ARTIFICIAL (arg))
1767 {
1768 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1769 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1770 }
1771 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1772
1773 case TARGET_EXPR:
1774 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1775
1776 case PTRMEM_CST:
1777 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1778 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1779
1780 case TEMPLATE_PARM_INDEX:
1781 val = iterative_hash_template_arg
1782 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1783 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1784 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1785
1786 case TRAIT_EXPR:
1787 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1788 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1789 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1790
1791 case BASELINK:
1792 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1793 val);
1794 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1795 val);
1796
1797 case MODOP_EXPR:
1798 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1799 code = TREE_CODE (TREE_OPERAND (arg, 1));
1800 val = iterative_hash_object (code, val);
1801 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1802
1803 case LAMBDA_EXPR:
1804 /* A lambda can't appear in a template arg, but don't crash on
1805 erroneous input. */
1806 gcc_assert (seen_error ());
1807 return val;
1808
1809 case CAST_EXPR:
1810 case IMPLICIT_CONV_EXPR:
1811 case STATIC_CAST_EXPR:
1812 case REINTERPRET_CAST_EXPR:
1813 case CONST_CAST_EXPR:
1814 case DYNAMIC_CAST_EXPR:
1815 case NEW_EXPR:
1816 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1817 /* Now hash operands as usual. */
1818 break;
1819
1820 default:
1821 break;
1822 }
1823
1824 switch (tclass)
1825 {
1826 case tcc_type:
1827 if (alias_template_specialization_p (arg))
1828 {
1829 // We want an alias specialization that survived strip_typedefs
1830 // to hash differently from its TYPE_CANONICAL, to avoid hash
1831 // collisions that compare as different in template_args_equal.
1832 // These could be dependent specializations that strip_typedefs
1833 // left alone, or untouched specializations because
1834 // coerce_template_parms returns the unconverted template
1835 // arguments if it sees incomplete argument packs.
1836 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1837 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1838 }
1839 if (TYPE_CANONICAL (arg))
1840 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1841 val);
1842 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1843 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1844 /* Otherwise just compare the types during lookup. */
1845 return val;
1846
1847 case tcc_declaration:
1848 case tcc_constant:
1849 return iterative_hash_expr (arg, val);
1850
1851 default:
1852 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1853 {
1854 unsigned n = cp_tree_operand_length (arg);
1855 for (i = 0; i < n; ++i)
1856 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1857 return val;
1858 }
1859 }
1860 gcc_unreachable ();
1861 return 0;
1862 }
1863
1864 /* Unregister the specialization SPEC as a specialization of TMPL.
1865 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1866 if the SPEC was listed as a specialization of TMPL.
1867
1868 Note that SPEC has been ggc_freed, so we can't look inside it. */
1869
1870 bool
1871 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1872 {
1873 spec_entry *entry;
1874 spec_entry elt;
1875
1876 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1877 elt.args = TI_ARGS (tinfo);
1878 elt.spec = NULL_TREE;
1879
1880 entry = decl_specializations->find (&elt);
1881 if (entry != NULL)
1882 {
1883 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1884 gcc_assert (new_spec != NULL_TREE);
1885 entry->spec = new_spec;
1886 return 1;
1887 }
1888
1889 return 0;
1890 }
1891
1892 /* Like register_specialization, but for local declarations. We are
1893 registering SPEC, an instantiation of TMPL. */
1894
1895 void
1896 register_local_specialization (tree spec, tree tmpl)
1897 {
1898 gcc_assert (tmpl != spec);
1899 local_specializations->put (tmpl, spec);
1900 }
1901
1902 /* TYPE is a class type. Returns true if TYPE is an explicitly
1903 specialized class. */
1904
1905 bool
1906 explicit_class_specialization_p (tree type)
1907 {
1908 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1909 return false;
1910 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1911 }
1912
1913 /* Print the list of functions at FNS, going through all the overloads
1914 for each element of the list. Alternatively, FNS can not be a
1915 TREE_LIST, in which case it will be printed together with all the
1916 overloads.
1917
1918 MORE and *STR should respectively be FALSE and NULL when the function
1919 is called from the outside. They are used internally on recursive
1920 calls. print_candidates manages the two parameters and leaves NULL
1921 in *STR when it ends. */
1922
1923 static void
1924 print_candidates_1 (tree fns, char **str, bool more = false)
1925 {
1926 if (TREE_CODE (fns) == TREE_LIST)
1927 for (; fns; fns = TREE_CHAIN (fns))
1928 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1929 else
1930 for (lkp_iterator iter (fns); iter;)
1931 {
1932 tree cand = *iter;
1933 ++iter;
1934
1935 const char *pfx = *str;
1936 if (!pfx)
1937 {
1938 if (more || iter)
1939 pfx = _("candidates are:");
1940 else
1941 pfx = _("candidate is:");
1942 *str = get_spaces (pfx);
1943 }
1944 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1945 }
1946 }
1947
1948 /* Print the list of candidate FNS in an error message. FNS can also
1949 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1950
1951 void
1952 print_candidates (tree fns)
1953 {
1954 char *str = NULL;
1955 print_candidates_1 (fns, &str);
1956 free (str);
1957 }
1958
1959 /* Get a (possibly) constrained template declaration for the
1960 purpose of ordering candidates. */
1961 static tree
1962 get_template_for_ordering (tree list)
1963 {
1964 gcc_assert (TREE_CODE (list) == TREE_LIST);
1965 tree f = TREE_VALUE (list);
1966 if (tree ti = DECL_TEMPLATE_INFO (f))
1967 return TI_TEMPLATE (ti);
1968 return f;
1969 }
1970
1971 /* Among candidates having the same signature, return the
1972 most constrained or NULL_TREE if there is no best candidate.
1973 If the signatures of candidates vary (e.g., template
1974 specialization vs. member function), then there can be no
1975 most constrained.
1976
1977 Note that we don't compare constraints on the functions
1978 themselves, but rather those of their templates. */
1979 static tree
1980 most_constrained_function (tree candidates)
1981 {
1982 // Try to find the best candidate in a first pass.
1983 tree champ = candidates;
1984 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1985 {
1986 int winner = more_constrained (get_template_for_ordering (champ),
1987 get_template_for_ordering (c));
1988 if (winner == -1)
1989 champ = c; // The candidate is more constrained
1990 else if (winner == 0)
1991 return NULL_TREE; // Neither is more constrained
1992 }
1993
1994 // Verify that the champ is better than previous candidates.
1995 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
1996 if (!more_constrained (get_template_for_ordering (champ),
1997 get_template_for_ordering (c)))
1998 return NULL_TREE;
1999 }
2000
2001 return champ;
2002 }
2003
2004
2005 /* Returns the template (one of the functions given by TEMPLATE_ID)
2006 which can be specialized to match the indicated DECL with the
2007 explicit template args given in TEMPLATE_ID. The DECL may be
2008 NULL_TREE if none is available. In that case, the functions in
2009 TEMPLATE_ID are non-members.
2010
2011 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2012 specialization of a member template.
2013
2014 The TEMPLATE_COUNT is the number of references to qualifying
2015 template classes that appeared in the name of the function. See
2016 check_explicit_specialization for a more accurate description.
2017
2018 TSK indicates what kind of template declaration (if any) is being
2019 declared. TSK_TEMPLATE indicates that the declaration given by
2020 DECL, though a FUNCTION_DECL, has template parameters, and is
2021 therefore a template function.
2022
2023 The template args (those explicitly specified and those deduced)
2024 are output in a newly created vector *TARGS_OUT.
2025
2026 If it is impossible to determine the result, an error message is
2027 issued. The error_mark_node is returned to indicate failure. */
2028
2029 static tree
2030 determine_specialization (tree template_id,
2031 tree decl,
2032 tree* targs_out,
2033 int need_member_template,
2034 int template_count,
2035 tmpl_spec_kind tsk)
2036 {
2037 tree fns;
2038 tree targs;
2039 tree explicit_targs;
2040 tree candidates = NULL_TREE;
2041
2042 /* A TREE_LIST of templates of which DECL may be a specialization.
2043 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2044 corresponding TREE_PURPOSE is the set of template arguments that,
2045 when used to instantiate the template, would produce a function
2046 with the signature of DECL. */
2047 tree templates = NULL_TREE;
2048 int header_count;
2049 cp_binding_level *b;
2050
2051 *targs_out = NULL_TREE;
2052
2053 if (template_id == error_mark_node || decl == error_mark_node)
2054 return error_mark_node;
2055
2056 /* We shouldn't be specializing a member template of an
2057 unspecialized class template; we already gave an error in
2058 check_specialization_scope, now avoid crashing. */
2059 if (template_count && DECL_CLASS_SCOPE_P (decl)
2060 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2061 {
2062 gcc_assert (errorcount);
2063 return error_mark_node;
2064 }
2065
2066 fns = TREE_OPERAND (template_id, 0);
2067 explicit_targs = TREE_OPERAND (template_id, 1);
2068
2069 if (fns == error_mark_node)
2070 return error_mark_node;
2071
2072 /* Check for baselinks. */
2073 if (BASELINK_P (fns))
2074 fns = BASELINK_FUNCTIONS (fns);
2075
2076 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2077 {
2078 error ("%qD is not a function template", fns);
2079 return error_mark_node;
2080 }
2081 else if (VAR_P (decl) && !variable_template_p (fns))
2082 {
2083 error ("%qD is not a variable template", fns);
2084 return error_mark_node;
2085 }
2086
2087 /* Count the number of template headers specified for this
2088 specialization. */
2089 header_count = 0;
2090 for (b = current_binding_level;
2091 b->kind == sk_template_parms;
2092 b = b->level_chain)
2093 ++header_count;
2094
2095 tree orig_fns = fns;
2096
2097 if (variable_template_p (fns))
2098 {
2099 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2100 targs = coerce_template_parms (parms, explicit_targs, fns,
2101 tf_warning_or_error,
2102 /*req_all*/true, /*use_defarg*/true);
2103 if (targs != error_mark_node)
2104 templates = tree_cons (targs, fns, templates);
2105 }
2106 else for (lkp_iterator iter (fns); iter; ++iter)
2107 {
2108 tree fn = *iter;
2109
2110 if (TREE_CODE (fn) == TEMPLATE_DECL)
2111 {
2112 tree decl_arg_types;
2113 tree fn_arg_types;
2114 tree insttype;
2115
2116 /* In case of explicit specialization, we need to check if
2117 the number of template headers appearing in the specialization
2118 is correct. This is usually done in check_explicit_specialization,
2119 but the check done there cannot be exhaustive when specializing
2120 member functions. Consider the following code:
2121
2122 template <> void A<int>::f(int);
2123 template <> template <> void A<int>::f(int);
2124
2125 Assuming that A<int> is not itself an explicit specialization
2126 already, the first line specializes "f" which is a non-template
2127 member function, whilst the second line specializes "f" which
2128 is a template member function. So both lines are syntactically
2129 correct, and check_explicit_specialization does not reject
2130 them.
2131
2132 Here, we can do better, as we are matching the specialization
2133 against the declarations. We count the number of template
2134 headers, and we check if they match TEMPLATE_COUNT + 1
2135 (TEMPLATE_COUNT is the number of qualifying template classes,
2136 plus there must be another header for the member template
2137 itself).
2138
2139 Notice that if header_count is zero, this is not a
2140 specialization but rather a template instantiation, so there
2141 is no check we can perform here. */
2142 if (header_count && header_count != template_count + 1)
2143 continue;
2144
2145 /* Check that the number of template arguments at the
2146 innermost level for DECL is the same as for FN. */
2147 if (current_binding_level->kind == sk_template_parms
2148 && !current_binding_level->explicit_spec_p
2149 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2150 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2151 (current_template_parms))))
2152 continue;
2153
2154 /* DECL might be a specialization of FN. */
2155 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2156 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2157
2158 /* For a non-static member function, we need to make sure
2159 that the const qualification is the same. Since
2160 get_bindings does not try to merge the "this" parameter,
2161 we must do the comparison explicitly. */
2162 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2163 && !same_type_p (TREE_VALUE (fn_arg_types),
2164 TREE_VALUE (decl_arg_types)))
2165 continue;
2166
2167 /* Skip the "this" parameter and, for constructors of
2168 classes with virtual bases, the VTT parameter. A
2169 full specialization of a constructor will have a VTT
2170 parameter, but a template never will. */
2171 decl_arg_types
2172 = skip_artificial_parms_for (decl, decl_arg_types);
2173 fn_arg_types
2174 = skip_artificial_parms_for (fn, fn_arg_types);
2175
2176 /* Function templates cannot be specializations; there are
2177 no partial specializations of functions. Therefore, if
2178 the type of DECL does not match FN, there is no
2179 match.
2180
2181 Note that it should never be the case that we have both
2182 candidates added here, and for regular member functions
2183 below. */
2184 if (tsk == tsk_template)
2185 {
2186 if (compparms (fn_arg_types, decl_arg_types))
2187 candidates = tree_cons (NULL_TREE, fn, candidates);
2188 continue;
2189 }
2190
2191 /* See whether this function might be a specialization of this
2192 template. Suppress access control because we might be trying
2193 to make this specialization a friend, and we have already done
2194 access control for the declaration of the specialization. */
2195 push_deferring_access_checks (dk_no_check);
2196 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2197 pop_deferring_access_checks ();
2198
2199 if (!targs)
2200 /* We cannot deduce template arguments that when used to
2201 specialize TMPL will produce DECL. */
2202 continue;
2203
2204 /* Remove, from the set of candidates, all those functions
2205 whose constraints are not satisfied. */
2206 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2207 continue;
2208
2209 // Then, try to form the new function type.
2210 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2211 if (insttype == error_mark_node)
2212 continue;
2213 fn_arg_types
2214 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2215 if (!compparms (fn_arg_types, decl_arg_types))
2216 continue;
2217
2218 /* Save this template, and the arguments deduced. */
2219 templates = tree_cons (targs, fn, templates);
2220 }
2221 else if (need_member_template)
2222 /* FN is an ordinary member function, and we need a
2223 specialization of a member template. */
2224 ;
2225 else if (TREE_CODE (fn) != FUNCTION_DECL)
2226 /* We can get IDENTIFIER_NODEs here in certain erroneous
2227 cases. */
2228 ;
2229 else if (!DECL_FUNCTION_MEMBER_P (fn))
2230 /* This is just an ordinary non-member function. Nothing can
2231 be a specialization of that. */
2232 ;
2233 else if (DECL_ARTIFICIAL (fn))
2234 /* Cannot specialize functions that are created implicitly. */
2235 ;
2236 else
2237 {
2238 tree decl_arg_types;
2239
2240 /* This is an ordinary member function. However, since
2241 we're here, we can assume its enclosing class is a
2242 template class. For example,
2243
2244 template <typename T> struct S { void f(); };
2245 template <> void S<int>::f() {}
2246
2247 Here, S<int>::f is a non-template, but S<int> is a
2248 template class. If FN has the same type as DECL, we
2249 might be in business. */
2250
2251 if (!DECL_TEMPLATE_INFO (fn))
2252 /* Its enclosing class is an explicit specialization
2253 of a template class. This is not a candidate. */
2254 continue;
2255
2256 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2257 TREE_TYPE (TREE_TYPE (fn))))
2258 /* The return types differ. */
2259 continue;
2260
2261 /* Adjust the type of DECL in case FN is a static member. */
2262 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2263 if (DECL_STATIC_FUNCTION_P (fn)
2264 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2265 decl_arg_types = TREE_CHAIN (decl_arg_types);
2266
2267 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2268 decl_arg_types))
2269 continue;
2270
2271 // If the deduced arguments do not satisfy the constraints,
2272 // this is not a candidate.
2273 if (flag_concepts && !constraints_satisfied_p (fn))
2274 continue;
2275
2276 // Add the candidate.
2277 candidates = tree_cons (NULL_TREE, fn, candidates);
2278 }
2279 }
2280
2281 if (templates && TREE_CHAIN (templates))
2282 {
2283 /* We have:
2284
2285 [temp.expl.spec]
2286
2287 It is possible for a specialization with a given function
2288 signature to be instantiated from more than one function
2289 template. In such cases, explicit specification of the
2290 template arguments must be used to uniquely identify the
2291 function template specialization being specialized.
2292
2293 Note that here, there's no suggestion that we're supposed to
2294 determine which of the candidate templates is most
2295 specialized. However, we, also have:
2296
2297 [temp.func.order]
2298
2299 Partial ordering of overloaded function template
2300 declarations is used in the following contexts to select
2301 the function template to which a function template
2302 specialization refers:
2303
2304 -- when an explicit specialization refers to a function
2305 template.
2306
2307 So, we do use the partial ordering rules, at least for now.
2308 This extension can only serve to make invalid programs valid,
2309 so it's safe. And, there is strong anecdotal evidence that
2310 the committee intended the partial ordering rules to apply;
2311 the EDG front end has that behavior, and John Spicer claims
2312 that the committee simply forgot to delete the wording in
2313 [temp.expl.spec]. */
2314 tree tmpl = most_specialized_instantiation (templates);
2315 if (tmpl != error_mark_node)
2316 {
2317 templates = tmpl;
2318 TREE_CHAIN (templates) = NULL_TREE;
2319 }
2320 }
2321
2322 // Concepts allows multiple declarations of member functions
2323 // with the same signature. Like above, we need to rely on
2324 // on the partial ordering of those candidates to determine which
2325 // is the best.
2326 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2327 {
2328 if (tree cand = most_constrained_function (candidates))
2329 {
2330 candidates = cand;
2331 TREE_CHAIN (cand) = NULL_TREE;
2332 }
2333 }
2334
2335 if (templates == NULL_TREE && candidates == NULL_TREE)
2336 {
2337 error ("template-id %qD for %q+D does not match any template "
2338 "declaration", template_id, decl);
2339 if (header_count && header_count != template_count + 1)
2340 inform (input_location, "saw %d %<template<>%>, need %d for "
2341 "specializing a member function template",
2342 header_count, template_count + 1);
2343 else
2344 print_candidates (orig_fns);
2345 return error_mark_node;
2346 }
2347 else if ((templates && TREE_CHAIN (templates))
2348 || (candidates && TREE_CHAIN (candidates))
2349 || (templates && candidates))
2350 {
2351 error ("ambiguous template specialization %qD for %q+D",
2352 template_id, decl);
2353 candidates = chainon (candidates, templates);
2354 print_candidates (candidates);
2355 return error_mark_node;
2356 }
2357
2358 /* We have one, and exactly one, match. */
2359 if (candidates)
2360 {
2361 tree fn = TREE_VALUE (candidates);
2362 *targs_out = copy_node (DECL_TI_ARGS (fn));
2363
2364 // Propagate the candidate's constraints to the declaration.
2365 set_constraints (decl, get_constraints (fn));
2366
2367 /* DECL is a re-declaration or partial instantiation of a template
2368 function. */
2369 if (TREE_CODE (fn) == TEMPLATE_DECL)
2370 return fn;
2371 /* It was a specialization of an ordinary member function in a
2372 template class. */
2373 return DECL_TI_TEMPLATE (fn);
2374 }
2375
2376 /* It was a specialization of a template. */
2377 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2378 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2379 {
2380 *targs_out = copy_node (targs);
2381 SET_TMPL_ARGS_LEVEL (*targs_out,
2382 TMPL_ARGS_DEPTH (*targs_out),
2383 TREE_PURPOSE (templates));
2384 }
2385 else
2386 *targs_out = TREE_PURPOSE (templates);
2387 return TREE_VALUE (templates);
2388 }
2389
2390 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2391 but with the default argument values filled in from those in the
2392 TMPL_TYPES. */
2393
2394 static tree
2395 copy_default_args_to_explicit_spec_1 (tree spec_types,
2396 tree tmpl_types)
2397 {
2398 tree new_spec_types;
2399
2400 if (!spec_types)
2401 return NULL_TREE;
2402
2403 if (spec_types == void_list_node)
2404 return void_list_node;
2405
2406 /* Substitute into the rest of the list. */
2407 new_spec_types =
2408 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2409 TREE_CHAIN (tmpl_types));
2410
2411 /* Add the default argument for this parameter. */
2412 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2413 TREE_VALUE (spec_types),
2414 new_spec_types);
2415 }
2416
2417 /* DECL is an explicit specialization. Replicate default arguments
2418 from the template it specializes. (That way, code like:
2419
2420 template <class T> void f(T = 3);
2421 template <> void f(double);
2422 void g () { f (); }
2423
2424 works, as required.) An alternative approach would be to look up
2425 the correct default arguments at the call-site, but this approach
2426 is consistent with how implicit instantiations are handled. */
2427
2428 static void
2429 copy_default_args_to_explicit_spec (tree decl)
2430 {
2431 tree tmpl;
2432 tree spec_types;
2433 tree tmpl_types;
2434 tree new_spec_types;
2435 tree old_type;
2436 tree new_type;
2437 tree t;
2438 tree object_type = NULL_TREE;
2439 tree in_charge = NULL_TREE;
2440 tree vtt = NULL_TREE;
2441
2442 /* See if there's anything we need to do. */
2443 tmpl = DECL_TI_TEMPLATE (decl);
2444 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2445 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2446 if (TREE_PURPOSE (t))
2447 break;
2448 if (!t)
2449 return;
2450
2451 old_type = TREE_TYPE (decl);
2452 spec_types = TYPE_ARG_TYPES (old_type);
2453
2454 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2455 {
2456 /* Remove the this pointer, but remember the object's type for
2457 CV quals. */
2458 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2459 spec_types = TREE_CHAIN (spec_types);
2460 tmpl_types = TREE_CHAIN (tmpl_types);
2461
2462 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2463 {
2464 /* DECL may contain more parameters than TMPL due to the extra
2465 in-charge parameter in constructors and destructors. */
2466 in_charge = spec_types;
2467 spec_types = TREE_CHAIN (spec_types);
2468 }
2469 if (DECL_HAS_VTT_PARM_P (decl))
2470 {
2471 vtt = spec_types;
2472 spec_types = TREE_CHAIN (spec_types);
2473 }
2474 }
2475
2476 /* Compute the merged default arguments. */
2477 new_spec_types =
2478 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2479
2480 /* Compute the new FUNCTION_TYPE. */
2481 if (object_type)
2482 {
2483 if (vtt)
2484 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2485 TREE_VALUE (vtt),
2486 new_spec_types);
2487
2488 if (in_charge)
2489 /* Put the in-charge parameter back. */
2490 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2491 TREE_VALUE (in_charge),
2492 new_spec_types);
2493
2494 new_type = build_method_type_directly (object_type,
2495 TREE_TYPE (old_type),
2496 new_spec_types);
2497 }
2498 else
2499 new_type = build_function_type (TREE_TYPE (old_type),
2500 new_spec_types);
2501 new_type = cp_build_type_attribute_variant (new_type,
2502 TYPE_ATTRIBUTES (old_type));
2503 new_type = build_exception_variant (new_type,
2504 TYPE_RAISES_EXCEPTIONS (old_type));
2505
2506 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2507 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2508
2509 TREE_TYPE (decl) = new_type;
2510 }
2511
2512 /* Return the number of template headers we expect to see for a definition
2513 or specialization of CTYPE or one of its non-template members. */
2514
2515 int
2516 num_template_headers_for_class (tree ctype)
2517 {
2518 int num_templates = 0;
2519
2520 while (ctype && CLASS_TYPE_P (ctype))
2521 {
2522 /* You're supposed to have one `template <...>' for every
2523 template class, but you don't need one for a full
2524 specialization. For example:
2525
2526 template <class T> struct S{};
2527 template <> struct S<int> { void f(); };
2528 void S<int>::f () {}
2529
2530 is correct; there shouldn't be a `template <>' for the
2531 definition of `S<int>::f'. */
2532 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2533 /* If CTYPE does not have template information of any
2534 kind, then it is not a template, nor is it nested
2535 within a template. */
2536 break;
2537 if (explicit_class_specialization_p (ctype))
2538 break;
2539 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2540 ++num_templates;
2541
2542 ctype = TYPE_CONTEXT (ctype);
2543 }
2544
2545 return num_templates;
2546 }
2547
2548 /* Do a simple sanity check on the template headers that precede the
2549 variable declaration DECL. */
2550
2551 void
2552 check_template_variable (tree decl)
2553 {
2554 tree ctx = CP_DECL_CONTEXT (decl);
2555 int wanted = num_template_headers_for_class (ctx);
2556 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2557 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2558 {
2559 if (cxx_dialect < cxx14)
2560 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2561 "variable templates only available with "
2562 "-std=c++14 or -std=gnu++14");
2563
2564 // Namespace-scope variable templates should have a template header.
2565 ++wanted;
2566 }
2567 if (template_header_count > wanted)
2568 {
2569 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2570 "too many template headers for %qD "
2571 "(should be %d)",
2572 decl, wanted);
2573 if (warned && CLASS_TYPE_P (ctx)
2574 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2575 inform (DECL_SOURCE_LOCATION (decl),
2576 "members of an explicitly specialized class are defined "
2577 "without a template header");
2578 }
2579 }
2580
2581 /* An explicit specialization whose declarator-id or class-head-name is not
2582 qualified shall be declared in the nearest enclosing namespace of the
2583 template, or, if the namespace is inline (7.3.1), any namespace from its
2584 enclosing namespace set.
2585
2586 If the name declared in the explicit instantiation is an unqualified name,
2587 the explicit instantiation shall appear in the namespace where its template
2588 is declared or, if that namespace is inline (7.3.1), any namespace from its
2589 enclosing namespace set. */
2590
2591 void
2592 check_unqualified_spec_or_inst (tree t, location_t loc)
2593 {
2594 tree tmpl = most_general_template (t);
2595 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2596 && !is_nested_namespace (current_namespace,
2597 CP_DECL_CONTEXT (tmpl), true))
2598 {
2599 if (processing_specialization)
2600 permerror (loc, "explicit specialization of %qD outside its "
2601 "namespace must use a nested-name-specifier", tmpl);
2602 else if (processing_explicit_instantiation
2603 && cxx_dialect >= cxx11)
2604 /* This was allowed in C++98, so only pedwarn. */
2605 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2606 "outside its namespace must use a nested-name-"
2607 "specifier", tmpl);
2608 }
2609 }
2610
2611 /* Check to see if the function just declared, as indicated in
2612 DECLARATOR, and in DECL, is a specialization of a function
2613 template. We may also discover that the declaration is an explicit
2614 instantiation at this point.
2615
2616 Returns DECL, or an equivalent declaration that should be used
2617 instead if all goes well. Issues an error message if something is
2618 amiss. Returns error_mark_node if the error is not easily
2619 recoverable.
2620
2621 FLAGS is a bitmask consisting of the following flags:
2622
2623 2: The function has a definition.
2624 4: The function is a friend.
2625
2626 The TEMPLATE_COUNT is the number of references to qualifying
2627 template classes that appeared in the name of the function. For
2628 example, in
2629
2630 template <class T> struct S { void f(); };
2631 void S<int>::f();
2632
2633 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2634 classes are not counted in the TEMPLATE_COUNT, so that in
2635
2636 template <class T> struct S {};
2637 template <> struct S<int> { void f(); }
2638 template <> void S<int>::f();
2639
2640 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2641 invalid; there should be no template <>.)
2642
2643 If the function is a specialization, it is marked as such via
2644 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2645 is set up correctly, and it is added to the list of specializations
2646 for that template. */
2647
2648 tree
2649 check_explicit_specialization (tree declarator,
2650 tree decl,
2651 int template_count,
2652 int flags)
2653 {
2654 int have_def = flags & 2;
2655 int is_friend = flags & 4;
2656 bool is_concept = flags & 8;
2657 int specialization = 0;
2658 int explicit_instantiation = 0;
2659 int member_specialization = 0;
2660 tree ctype = DECL_CLASS_CONTEXT (decl);
2661 tree dname = DECL_NAME (decl);
2662 tmpl_spec_kind tsk;
2663
2664 if (is_friend)
2665 {
2666 if (!processing_specialization)
2667 tsk = tsk_none;
2668 else
2669 tsk = tsk_excessive_parms;
2670 }
2671 else
2672 tsk = current_tmpl_spec_kind (template_count);
2673
2674 switch (tsk)
2675 {
2676 case tsk_none:
2677 if (processing_specialization && !VAR_P (decl))
2678 {
2679 specialization = 1;
2680 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2681 }
2682 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2683 {
2684 if (is_friend)
2685 /* This could be something like:
2686
2687 template <class T> void f(T);
2688 class S { friend void f<>(int); } */
2689 specialization = 1;
2690 else
2691 {
2692 /* This case handles bogus declarations like template <>
2693 template <class T> void f<int>(); */
2694
2695 error ("template-id %qD in declaration of primary template",
2696 declarator);
2697 return decl;
2698 }
2699 }
2700 break;
2701
2702 case tsk_invalid_member_spec:
2703 /* The error has already been reported in
2704 check_specialization_scope. */
2705 return error_mark_node;
2706
2707 case tsk_invalid_expl_inst:
2708 error ("template parameter list used in explicit instantiation");
2709
2710 /* Fall through. */
2711
2712 case tsk_expl_inst:
2713 if (have_def)
2714 error ("definition provided for explicit instantiation");
2715
2716 explicit_instantiation = 1;
2717 break;
2718
2719 case tsk_excessive_parms:
2720 case tsk_insufficient_parms:
2721 if (tsk == tsk_excessive_parms)
2722 error ("too many template parameter lists in declaration of %qD",
2723 decl);
2724 else if (template_header_count)
2725 error("too few template parameter lists in declaration of %qD", decl);
2726 else
2727 error("explicit specialization of %qD must be introduced by "
2728 "%<template <>%>", decl);
2729
2730 /* Fall through. */
2731 case tsk_expl_spec:
2732 if (is_concept)
2733 error ("explicit specialization declared %<concept%>");
2734
2735 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2736 /* In cases like template<> constexpr bool v = true;
2737 We'll give an error in check_template_variable. */
2738 break;
2739
2740 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2741 if (ctype)
2742 member_specialization = 1;
2743 else
2744 specialization = 1;
2745 break;
2746
2747 case tsk_template:
2748 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2749 {
2750 /* This case handles bogus declarations like template <>
2751 template <class T> void f<int>(); */
2752
2753 if (!uses_template_parms (declarator))
2754 error ("template-id %qD in declaration of primary template",
2755 declarator);
2756 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2757 {
2758 /* Partial specialization of variable template. */
2759 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2760 specialization = 1;
2761 goto ok;
2762 }
2763 else if (cxx_dialect < cxx14)
2764 error ("non-type partial specialization %qD "
2765 "is not allowed", declarator);
2766 else
2767 error ("non-class, non-variable partial specialization %qD "
2768 "is not allowed", declarator);
2769 return decl;
2770 ok:;
2771 }
2772
2773 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2774 /* This is a specialization of a member template, without
2775 specialization the containing class. Something like:
2776
2777 template <class T> struct S {
2778 template <class U> void f (U);
2779 };
2780 template <> template <class U> void S<int>::f(U) {}
2781
2782 That's a specialization -- but of the entire template. */
2783 specialization = 1;
2784 break;
2785
2786 default:
2787 gcc_unreachable ();
2788 }
2789
2790 if ((specialization || member_specialization)
2791 /* This doesn't apply to variable templates. */
2792 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2793 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2794 {
2795 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2796 for (; t; t = TREE_CHAIN (t))
2797 if (TREE_PURPOSE (t))
2798 {
2799 permerror (input_location,
2800 "default argument specified in explicit specialization");
2801 break;
2802 }
2803 }
2804
2805 if (specialization || member_specialization || explicit_instantiation)
2806 {
2807 tree tmpl = NULL_TREE;
2808 tree targs = NULL_TREE;
2809 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2810
2811 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2812 if (!was_template_id)
2813 {
2814 tree fns;
2815
2816 gcc_assert (identifier_p (declarator));
2817 if (ctype)
2818 fns = dname;
2819 else
2820 {
2821 /* If there is no class context, the explicit instantiation
2822 must be at namespace scope. */
2823 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2824
2825 /* Find the namespace binding, using the declaration
2826 context. */
2827 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2828 false, true);
2829 if (fns == error_mark_node)
2830 /* If lookup fails, look for a friend declaration so we can
2831 give a better diagnostic. */
2832 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2833 /*type*/false, /*complain*/true,
2834 /*hidden*/true);
2835
2836 if (fns == error_mark_node || !is_overloaded_fn (fns))
2837 {
2838 error ("%qD is not a template function", dname);
2839 fns = error_mark_node;
2840 }
2841 }
2842
2843 declarator = lookup_template_function (fns, NULL_TREE);
2844 }
2845
2846 if (declarator == error_mark_node)
2847 return error_mark_node;
2848
2849 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2850 {
2851 if (!explicit_instantiation)
2852 /* A specialization in class scope. This is invalid,
2853 but the error will already have been flagged by
2854 check_specialization_scope. */
2855 return error_mark_node;
2856 else
2857 {
2858 /* It's not valid to write an explicit instantiation in
2859 class scope, e.g.:
2860
2861 class C { template void f(); }
2862
2863 This case is caught by the parser. However, on
2864 something like:
2865
2866 template class C { void f(); };
2867
2868 (which is invalid) we can get here. The error will be
2869 issued later. */
2870 ;
2871 }
2872
2873 return decl;
2874 }
2875 else if (ctype != NULL_TREE
2876 && (identifier_p (TREE_OPERAND (declarator, 0))))
2877 {
2878 // We'll match variable templates in start_decl.
2879 if (VAR_P (decl))
2880 return decl;
2881
2882 /* Find the list of functions in ctype that have the same
2883 name as the declared function. */
2884 tree name = TREE_OPERAND (declarator, 0);
2885
2886 if (constructor_name_p (name, ctype))
2887 {
2888 if (DECL_CONSTRUCTOR_P (decl)
2889 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2890 : !CLASSTYPE_DESTRUCTOR (ctype))
2891 {
2892 /* From [temp.expl.spec]:
2893
2894 If such an explicit specialization for the member
2895 of a class template names an implicitly-declared
2896 special member function (clause _special_), the
2897 program is ill-formed.
2898
2899 Similar language is found in [temp.explicit]. */
2900 error ("specialization of implicitly-declared special member function");
2901 return error_mark_node;
2902 }
2903
2904 name = DECL_NAME (decl);
2905 }
2906
2907 /* For a type-conversion operator, We might be looking for
2908 `operator int' which will be a specialization of
2909 `operator T'. Grab all the conversion operators, and
2910 then select from them. */
2911 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
2912 ? conv_op_identifier : name);
2913
2914 if (fns == NULL_TREE)
2915 {
2916 error ("no member function %qD declared in %qT", name, ctype);
2917 return error_mark_node;
2918 }
2919 else
2920 TREE_OPERAND (declarator, 0) = fns;
2921 }
2922
2923 /* Figure out what exactly is being specialized at this point.
2924 Note that for an explicit instantiation, even one for a
2925 member function, we cannot tell a priori whether the
2926 instantiation is for a member template, or just a member
2927 function of a template class. Even if a member template is
2928 being instantiated, the member template arguments may be
2929 elided if they can be deduced from the rest of the
2930 declaration. */
2931 tmpl = determine_specialization (declarator, decl,
2932 &targs,
2933 member_specialization,
2934 template_count,
2935 tsk);
2936
2937 if (!tmpl || tmpl == error_mark_node)
2938 /* We couldn't figure out what this declaration was
2939 specializing. */
2940 return error_mark_node;
2941 else
2942 {
2943 if (TREE_CODE (decl) == FUNCTION_DECL
2944 && DECL_HIDDEN_FRIEND_P (tmpl))
2945 {
2946 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2947 "friend declaration %qD is not visible to "
2948 "explicit specialization", tmpl))
2949 inform (DECL_SOURCE_LOCATION (tmpl),
2950 "friend declaration here");
2951 }
2952 else if (!ctype && !is_friend
2953 && CP_DECL_CONTEXT (decl) == current_namespace)
2954 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2955
2956 tree gen_tmpl = most_general_template (tmpl);
2957
2958 if (explicit_instantiation)
2959 {
2960 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2961 is done by do_decl_instantiation later. */
2962
2963 int arg_depth = TMPL_ARGS_DEPTH (targs);
2964 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2965
2966 if (arg_depth > parm_depth)
2967 {
2968 /* If TMPL is not the most general template (for
2969 example, if TMPL is a friend template that is
2970 injected into namespace scope), then there will
2971 be too many levels of TARGS. Remove some of them
2972 here. */
2973 int i;
2974 tree new_targs;
2975
2976 new_targs = make_tree_vec (parm_depth);
2977 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2978 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2979 = TREE_VEC_ELT (targs, i);
2980 targs = new_targs;
2981 }
2982
2983 return instantiate_template (tmpl, targs, tf_error);
2984 }
2985
2986 /* If we thought that the DECL was a member function, but it
2987 turns out to be specializing a static member function,
2988 make DECL a static member function as well. */
2989 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2990 && DECL_STATIC_FUNCTION_P (tmpl)
2991 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2992 revert_static_member_fn (decl);
2993
2994 /* If this is a specialization of a member template of a
2995 template class, we want to return the TEMPLATE_DECL, not
2996 the specialization of it. */
2997 if (tsk == tsk_template && !was_template_id)
2998 {
2999 tree result = DECL_TEMPLATE_RESULT (tmpl);
3000 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3001 DECL_INITIAL (result) = NULL_TREE;
3002 if (have_def)
3003 {
3004 tree parm;
3005 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3006 DECL_SOURCE_LOCATION (result)
3007 = DECL_SOURCE_LOCATION (decl);
3008 /* We want to use the argument list specified in the
3009 definition, not in the original declaration. */
3010 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3011 for (parm = DECL_ARGUMENTS (result); parm;
3012 parm = DECL_CHAIN (parm))
3013 DECL_CONTEXT (parm) = result;
3014 }
3015 return register_specialization (tmpl, gen_tmpl, targs,
3016 is_friend, 0);
3017 }
3018
3019 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3020 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3021
3022 if (was_template_id)
3023 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3024
3025 /* Inherit default function arguments from the template
3026 DECL is specializing. */
3027 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3028 copy_default_args_to_explicit_spec (decl);
3029
3030 /* This specialization has the same protection as the
3031 template it specializes. */
3032 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3033 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3034
3035 /* 7.1.1-1 [dcl.stc]
3036
3037 A storage-class-specifier shall not be specified in an
3038 explicit specialization...
3039
3040 The parser rejects these, so unless action is taken here,
3041 explicit function specializations will always appear with
3042 global linkage.
3043
3044 The action recommended by the C++ CWG in response to C++
3045 defect report 605 is to make the storage class and linkage
3046 of the explicit specialization match the templated function:
3047
3048 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3049 */
3050 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3051 {
3052 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3053 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3054
3055 /* A concept cannot be specialized. */
3056 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3057 {
3058 error ("explicit specialization of function concept %qD",
3059 gen_tmpl);
3060 return error_mark_node;
3061 }
3062
3063 /* This specialization has the same linkage and visibility as
3064 the function template it specializes. */
3065 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3066 if (! TREE_PUBLIC (decl))
3067 {
3068 DECL_INTERFACE_KNOWN (decl) = 1;
3069 DECL_NOT_REALLY_EXTERN (decl) = 1;
3070 }
3071 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3072 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3073 {
3074 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3075 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3076 }
3077 }
3078
3079 /* If DECL is a friend declaration, declared using an
3080 unqualified name, the namespace associated with DECL may
3081 have been set incorrectly. For example, in:
3082
3083 template <typename T> void f(T);
3084 namespace N {
3085 struct S { friend void f<int>(int); }
3086 }
3087
3088 we will have set the DECL_CONTEXT for the friend
3089 declaration to N, rather than to the global namespace. */
3090 if (DECL_NAMESPACE_SCOPE_P (decl))
3091 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3092
3093 if (is_friend && !have_def)
3094 /* This is not really a declaration of a specialization.
3095 It's just the name of an instantiation. But, it's not
3096 a request for an instantiation, either. */
3097 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3098 else if (TREE_CODE (decl) == FUNCTION_DECL)
3099 /* A specialization is not necessarily COMDAT. */
3100 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3101 && DECL_DECLARED_INLINE_P (decl));
3102 else if (VAR_P (decl))
3103 DECL_COMDAT (decl) = false;
3104
3105 /* If this is a full specialization, register it so that we can find
3106 it again. Partial specializations will be registered in
3107 process_partial_specialization. */
3108 if (!processing_template_decl)
3109 decl = register_specialization (decl, gen_tmpl, targs,
3110 is_friend, 0);
3111
3112 /* A 'structor should already have clones. */
3113 gcc_assert (decl == error_mark_node
3114 || variable_template_p (tmpl)
3115 || !(DECL_CONSTRUCTOR_P (decl)
3116 || DECL_DESTRUCTOR_P (decl))
3117 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3118 }
3119 }
3120
3121 return decl;
3122 }
3123
3124 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3125 parameters. These are represented in the same format used for
3126 DECL_TEMPLATE_PARMS. */
3127
3128 int
3129 comp_template_parms (const_tree parms1, const_tree parms2)
3130 {
3131 const_tree p1;
3132 const_tree p2;
3133
3134 if (parms1 == parms2)
3135 return 1;
3136
3137 for (p1 = parms1, p2 = parms2;
3138 p1 != NULL_TREE && p2 != NULL_TREE;
3139 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3140 {
3141 tree t1 = TREE_VALUE (p1);
3142 tree t2 = TREE_VALUE (p2);
3143 int i;
3144
3145 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3146 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3147
3148 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3149 return 0;
3150
3151 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3152 {
3153 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3154 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3155
3156 /* If either of the template parameters are invalid, assume
3157 they match for the sake of error recovery. */
3158 if (error_operand_p (parm1) || error_operand_p (parm2))
3159 return 1;
3160
3161 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3162 return 0;
3163
3164 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3165 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3166 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3167 continue;
3168 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3169 return 0;
3170 }
3171 }
3172
3173 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3174 /* One set of parameters has more parameters lists than the
3175 other. */
3176 return 0;
3177
3178 return 1;
3179 }
3180
3181 /* Determine whether PARM is a parameter pack. */
3182
3183 bool
3184 template_parameter_pack_p (const_tree parm)
3185 {
3186 /* Determine if we have a non-type template parameter pack. */
3187 if (TREE_CODE (parm) == PARM_DECL)
3188 return (DECL_TEMPLATE_PARM_P (parm)
3189 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3190 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3191 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3192
3193 /* If this is a list of template parameters, we could get a
3194 TYPE_DECL or a TEMPLATE_DECL. */
3195 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3196 parm = TREE_TYPE (parm);
3197
3198 /* Otherwise it must be a type template parameter. */
3199 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3200 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3201 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3202 }
3203
3204 /* Determine if T is a function parameter pack. */
3205
3206 bool
3207 function_parameter_pack_p (const_tree t)
3208 {
3209 if (t && TREE_CODE (t) == PARM_DECL)
3210 return DECL_PACK_P (t);
3211 return false;
3212 }
3213
3214 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3215 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3216
3217 tree
3218 get_function_template_decl (const_tree primary_func_tmpl_inst)
3219 {
3220 if (! primary_func_tmpl_inst
3221 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3222 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3223 return NULL;
3224
3225 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3226 }
3227
3228 /* Return true iff the function parameter PARAM_DECL was expanded
3229 from the function parameter pack PACK. */
3230
3231 bool
3232 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3233 {
3234 if (DECL_ARTIFICIAL (param_decl)
3235 || !function_parameter_pack_p (pack))
3236 return false;
3237
3238 /* The parameter pack and its pack arguments have the same
3239 DECL_PARM_INDEX. */
3240 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3241 }
3242
3243 /* Determine whether ARGS describes a variadic template args list,
3244 i.e., one that is terminated by a template argument pack. */
3245
3246 static bool
3247 template_args_variadic_p (tree args)
3248 {
3249 int nargs;
3250 tree last_parm;
3251
3252 if (args == NULL_TREE)
3253 return false;
3254
3255 args = INNERMOST_TEMPLATE_ARGS (args);
3256 nargs = TREE_VEC_LENGTH (args);
3257
3258 if (nargs == 0)
3259 return false;
3260
3261 last_parm = TREE_VEC_ELT (args, nargs - 1);
3262
3263 return ARGUMENT_PACK_P (last_parm);
3264 }
3265
3266 /* Generate a new name for the parameter pack name NAME (an
3267 IDENTIFIER_NODE) that incorporates its */
3268
3269 static tree
3270 make_ith_pack_parameter_name (tree name, int i)
3271 {
3272 /* Munge the name to include the parameter index. */
3273 #define NUMBUF_LEN 128
3274 char numbuf[NUMBUF_LEN];
3275 char* newname;
3276 int newname_len;
3277
3278 if (name == NULL_TREE)
3279 return name;
3280 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3281 newname_len = IDENTIFIER_LENGTH (name)
3282 + strlen (numbuf) + 2;
3283 newname = (char*)alloca (newname_len);
3284 snprintf (newname, newname_len,
3285 "%s#%i", IDENTIFIER_POINTER (name), i);
3286 return get_identifier (newname);
3287 }
3288
3289 /* Return true if T is a primary function, class or alias template
3290 instantiation. */
3291
3292 bool
3293 primary_template_instantiation_p (const_tree t)
3294 {
3295 if (!t)
3296 return false;
3297
3298 if (TREE_CODE (t) == FUNCTION_DECL)
3299 return DECL_LANG_SPECIFIC (t)
3300 && DECL_TEMPLATE_INSTANTIATION (t)
3301 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3302 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3303 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3304 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3305 else if (alias_template_specialization_p (t))
3306 return true;
3307 return false;
3308 }
3309
3310 /* Return true if PARM is a template template parameter. */
3311
3312 bool
3313 template_template_parameter_p (const_tree parm)
3314 {
3315 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3316 }
3317
3318 /* Return true iff PARM is a DECL representing a type template
3319 parameter. */
3320
3321 bool
3322 template_type_parameter_p (const_tree parm)
3323 {
3324 return (parm
3325 && (TREE_CODE (parm) == TYPE_DECL
3326 || TREE_CODE (parm) == TEMPLATE_DECL)
3327 && DECL_TEMPLATE_PARM_P (parm));
3328 }
3329
3330 /* Return the template parameters of T if T is a
3331 primary template instantiation, NULL otherwise. */
3332
3333 tree
3334 get_primary_template_innermost_parameters (const_tree t)
3335 {
3336 tree parms = NULL, template_info = NULL;
3337
3338 if ((template_info = get_template_info (t))
3339 && primary_template_instantiation_p (t))
3340 parms = INNERMOST_TEMPLATE_PARMS
3341 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3342
3343 return parms;
3344 }
3345
3346 /* Return the template parameters of the LEVELth level from the full list
3347 of template parameters PARMS. */
3348
3349 tree
3350 get_template_parms_at_level (tree parms, int level)
3351 {
3352 tree p;
3353 if (!parms
3354 || TREE_CODE (parms) != TREE_LIST
3355 || level > TMPL_PARMS_DEPTH (parms))
3356 return NULL_TREE;
3357
3358 for (p = parms; p; p = TREE_CHAIN (p))
3359 if (TMPL_PARMS_DEPTH (p) == level)
3360 return p;
3361
3362 return NULL_TREE;
3363 }
3364
3365 /* Returns the template arguments of T if T is a template instantiation,
3366 NULL otherwise. */
3367
3368 tree
3369 get_template_innermost_arguments (const_tree t)
3370 {
3371 tree args = NULL, template_info = NULL;
3372
3373 if ((template_info = get_template_info (t))
3374 && TI_ARGS (template_info))
3375 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3376
3377 return args;
3378 }
3379
3380 /* Return the argument pack elements of T if T is a template argument pack,
3381 NULL otherwise. */
3382
3383 tree
3384 get_template_argument_pack_elems (const_tree t)
3385 {
3386 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3387 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3388 return NULL;
3389
3390 return ARGUMENT_PACK_ARGS (t);
3391 }
3392
3393 /* True iff FN is a function representing a built-in variadic parameter
3394 pack. */
3395
3396 bool
3397 builtin_pack_fn_p (tree fn)
3398 {
3399 if (!fn
3400 || TREE_CODE (fn) != FUNCTION_DECL
3401 || !DECL_IS_BUILTIN (fn))
3402 return false;
3403
3404 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3405 return true;
3406
3407 return false;
3408 }
3409
3410 /* True iff CALL is a call to a function representing a built-in variadic
3411 parameter pack. */
3412
3413 static bool
3414 builtin_pack_call_p (tree call)
3415 {
3416 if (TREE_CODE (call) != CALL_EXPR)
3417 return false;
3418 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3419 }
3420
3421 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3422
3423 static tree
3424 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3425 tree in_decl)
3426 {
3427 tree ohi = CALL_EXPR_ARG (call, 0);
3428 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3429 false/*fn*/, true/*int_cst*/);
3430
3431 if (value_dependent_expression_p (hi))
3432 {
3433 if (hi != ohi)
3434 {
3435 call = copy_node (call);
3436 CALL_EXPR_ARG (call, 0) = hi;
3437 }
3438 tree ex = make_pack_expansion (call);
3439 tree vec = make_tree_vec (1);
3440 TREE_VEC_ELT (vec, 0) = ex;
3441 return vec;
3442 }
3443 else
3444 {
3445 hi = cxx_constant_value (hi);
3446 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3447
3448 /* Calculate the largest value of len that won't make the size of the vec
3449 overflow an int. The compiler will exceed resource limits long before
3450 this, but it seems a decent place to diagnose. */
3451 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3452
3453 if (len < 0 || len > max)
3454 {
3455 if ((complain & tf_error)
3456 && hi != error_mark_node)
3457 error ("argument to __integer_pack must be between 0 and %d", max);
3458 return error_mark_node;
3459 }
3460
3461 tree vec = make_tree_vec (len);
3462
3463 for (int i = 0; i < len; ++i)
3464 TREE_VEC_ELT (vec, i) = size_int (i);
3465
3466 return vec;
3467 }
3468 }
3469
3470 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3471 CALL. */
3472
3473 static tree
3474 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3475 tree in_decl)
3476 {
3477 if (!builtin_pack_call_p (call))
3478 return NULL_TREE;
3479
3480 tree fn = CALL_EXPR_FN (call);
3481
3482 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3483 return expand_integer_pack (call, args, complain, in_decl);
3484
3485 return NULL_TREE;
3486 }
3487
3488 /* Structure used to track the progress of find_parameter_packs_r. */
3489 struct find_parameter_pack_data
3490 {
3491 /* TREE_LIST that will contain all of the parameter packs found by
3492 the traversal. */
3493 tree* parameter_packs;
3494
3495 /* Set of AST nodes that have been visited by the traversal. */
3496 hash_set<tree> *visited;
3497
3498 /* True iff we're making a type pack expansion. */
3499 bool type_pack_expansion_p;
3500 };
3501
3502 /* Identifies all of the argument packs that occur in a template
3503 argument and appends them to the TREE_LIST inside DATA, which is a
3504 find_parameter_pack_data structure. This is a subroutine of
3505 make_pack_expansion and uses_parameter_packs. */
3506 static tree
3507 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3508 {
3509 tree t = *tp;
3510 struct find_parameter_pack_data* ppd =
3511 (struct find_parameter_pack_data*)data;
3512 bool parameter_pack_p = false;
3513
3514 /* Handle type aliases/typedefs. */
3515 if (TYPE_ALIAS_P (t))
3516 {
3517 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3518 cp_walk_tree (&TI_ARGS (tinfo),
3519 &find_parameter_packs_r,
3520 ppd, ppd->visited);
3521 *walk_subtrees = 0;
3522 return NULL_TREE;
3523 }
3524
3525 /* Identify whether this is a parameter pack or not. */
3526 switch (TREE_CODE (t))
3527 {
3528 case TEMPLATE_PARM_INDEX:
3529 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3530 parameter_pack_p = true;
3531 break;
3532
3533 case TEMPLATE_TYPE_PARM:
3534 t = TYPE_MAIN_VARIANT (t);
3535 /* FALLTHRU */
3536 case TEMPLATE_TEMPLATE_PARM:
3537 /* If the placeholder appears in the decl-specifier-seq of a function
3538 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3539 is a pack expansion, the invented template parameter is a template
3540 parameter pack. */
3541 if (ppd->type_pack_expansion_p && is_auto (t))
3542 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3543 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3544 parameter_pack_p = true;
3545 break;
3546
3547 case FIELD_DECL:
3548 case PARM_DECL:
3549 if (DECL_PACK_P (t))
3550 {
3551 /* We don't want to walk into the type of a PARM_DECL,
3552 because we don't want to see the type parameter pack. */
3553 *walk_subtrees = 0;
3554 parameter_pack_p = true;
3555 }
3556 break;
3557
3558 /* Look through a lambda capture proxy to the field pack. */
3559 case VAR_DECL:
3560 if (DECL_HAS_VALUE_EXPR_P (t))
3561 {
3562 tree v = DECL_VALUE_EXPR (t);
3563 cp_walk_tree (&v,
3564 &find_parameter_packs_r,
3565 ppd, ppd->visited);
3566 *walk_subtrees = 0;
3567 }
3568 else if (variable_template_specialization_p (t))
3569 {
3570 cp_walk_tree (&DECL_TI_ARGS (t),
3571 find_parameter_packs_r,
3572 ppd, ppd->visited);
3573 *walk_subtrees = 0;
3574 }
3575 break;
3576
3577 case CALL_EXPR:
3578 if (builtin_pack_call_p (t))
3579 parameter_pack_p = true;
3580 break;
3581
3582 case BASES:
3583 parameter_pack_p = true;
3584 break;
3585 default:
3586 /* Not a parameter pack. */
3587 break;
3588 }
3589
3590 if (parameter_pack_p)
3591 {
3592 /* Add this parameter pack to the list. */
3593 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3594 }
3595
3596 if (TYPE_P (t))
3597 cp_walk_tree (&TYPE_CONTEXT (t),
3598 &find_parameter_packs_r, ppd, ppd->visited);
3599
3600 /* This switch statement will return immediately if we don't find a
3601 parameter pack. */
3602 switch (TREE_CODE (t))
3603 {
3604 case TEMPLATE_PARM_INDEX:
3605 return NULL_TREE;
3606
3607 case BOUND_TEMPLATE_TEMPLATE_PARM:
3608 /* Check the template itself. */
3609 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3610 &find_parameter_packs_r, ppd, ppd->visited);
3611 /* Check the template arguments. */
3612 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3613 ppd->visited);
3614 *walk_subtrees = 0;
3615 return NULL_TREE;
3616
3617 case TEMPLATE_TYPE_PARM:
3618 case TEMPLATE_TEMPLATE_PARM:
3619 return NULL_TREE;
3620
3621 case PARM_DECL:
3622 return NULL_TREE;
3623
3624 case DECL_EXPR:
3625 /* Ignore the declaration of a capture proxy for a parameter pack. */
3626 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3627 *walk_subtrees = 0;
3628 return NULL_TREE;
3629
3630 case RECORD_TYPE:
3631 if (TYPE_PTRMEMFUNC_P (t))
3632 return NULL_TREE;
3633 /* Fall through. */
3634
3635 case UNION_TYPE:
3636 case ENUMERAL_TYPE:
3637 if (TYPE_TEMPLATE_INFO (t))
3638 cp_walk_tree (&TYPE_TI_ARGS (t),
3639 &find_parameter_packs_r, ppd, ppd->visited);
3640
3641 *walk_subtrees = 0;
3642 return NULL_TREE;
3643
3644 case TEMPLATE_DECL:
3645 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3646 return NULL_TREE;
3647 gcc_fallthrough();
3648
3649 case CONSTRUCTOR:
3650 cp_walk_tree (&TREE_TYPE (t),
3651 &find_parameter_packs_r, ppd, ppd->visited);
3652 return NULL_TREE;
3653
3654 case TYPENAME_TYPE:
3655 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3656 ppd, ppd->visited);
3657 *walk_subtrees = 0;
3658 return NULL_TREE;
3659
3660 case TYPE_PACK_EXPANSION:
3661 case EXPR_PACK_EXPANSION:
3662 *walk_subtrees = 0;
3663 return NULL_TREE;
3664
3665 case INTEGER_TYPE:
3666 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3667 ppd, ppd->visited);
3668 *walk_subtrees = 0;
3669 return NULL_TREE;
3670
3671 case IDENTIFIER_NODE:
3672 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3673 ppd->visited);
3674 *walk_subtrees = 0;
3675 return NULL_TREE;
3676
3677 case LAMBDA_EXPR:
3678 {
3679 tree fn = lambda_function (t);
3680 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3681 ppd->visited);
3682 *walk_subtrees = 0;
3683 return NULL_TREE;
3684 }
3685
3686 case DECLTYPE_TYPE:
3687 {
3688 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3689 type_pack_expansion_p to false so that any placeholders
3690 within the expression don't get marked as parameter packs. */
3691 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3692 ppd->type_pack_expansion_p = false;
3693 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3694 ppd, ppd->visited);
3695 ppd->type_pack_expansion_p = type_pack_expansion_p;
3696 *walk_subtrees = 0;
3697 return NULL_TREE;
3698 }
3699
3700 default:
3701 return NULL_TREE;
3702 }
3703
3704 return NULL_TREE;
3705 }
3706
3707 /* Determines if the expression or type T uses any parameter packs. */
3708 bool
3709 uses_parameter_packs (tree t)
3710 {
3711 tree parameter_packs = NULL_TREE;
3712 struct find_parameter_pack_data ppd;
3713 ppd.parameter_packs = &parameter_packs;
3714 ppd.visited = new hash_set<tree>;
3715 ppd.type_pack_expansion_p = false;
3716 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3717 delete ppd.visited;
3718 return parameter_packs != NULL_TREE;
3719 }
3720
3721 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3722 representation a base-class initializer into a parameter pack
3723 expansion. If all goes well, the resulting node will be an
3724 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3725 respectively. */
3726 tree
3727 make_pack_expansion (tree arg)
3728 {
3729 tree result;
3730 tree parameter_packs = NULL_TREE;
3731 bool for_types = false;
3732 struct find_parameter_pack_data ppd;
3733
3734 if (!arg || arg == error_mark_node)
3735 return arg;
3736
3737 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3738 {
3739 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3740 class initializer. In this case, the TREE_PURPOSE will be a
3741 _TYPE node (representing the base class expansion we're
3742 initializing) and the TREE_VALUE will be a TREE_LIST
3743 containing the initialization arguments.
3744
3745 The resulting expansion looks somewhat different from most
3746 expansions. Rather than returning just one _EXPANSION, we
3747 return a TREE_LIST whose TREE_PURPOSE is a
3748 TYPE_PACK_EXPANSION containing the bases that will be
3749 initialized. The TREE_VALUE will be identical to the
3750 original TREE_VALUE, which is a list of arguments that will
3751 be passed to each base. We do not introduce any new pack
3752 expansion nodes into the TREE_VALUE (although it is possible
3753 that some already exist), because the TREE_PURPOSE and
3754 TREE_VALUE all need to be expanded together with the same
3755 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3756 resulting TREE_PURPOSE will mention the parameter packs in
3757 both the bases and the arguments to the bases. */
3758 tree purpose;
3759 tree value;
3760 tree parameter_packs = NULL_TREE;
3761
3762 /* Determine which parameter packs will be used by the base
3763 class expansion. */
3764 ppd.visited = new hash_set<tree>;
3765 ppd.parameter_packs = &parameter_packs;
3766 ppd.type_pack_expansion_p = true;
3767 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3768 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3769 &ppd, ppd.visited);
3770
3771 if (parameter_packs == NULL_TREE)
3772 {
3773 error ("base initializer expansion %qT contains no parameter packs", arg);
3774 delete ppd.visited;
3775 return error_mark_node;
3776 }
3777
3778 if (TREE_VALUE (arg) != void_type_node)
3779 {
3780 /* Collect the sets of parameter packs used in each of the
3781 initialization arguments. */
3782 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3783 {
3784 /* Determine which parameter packs will be expanded in this
3785 argument. */
3786 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3787 &ppd, ppd.visited);
3788 }
3789 }
3790
3791 delete ppd.visited;
3792
3793 /* Create the pack expansion type for the base type. */
3794 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3795 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3796 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3797 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3798
3799 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3800 they will rarely be compared to anything. */
3801 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3802
3803 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3804 }
3805
3806 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3807 for_types = true;
3808
3809 /* Build the PACK_EXPANSION_* node. */
3810 result = for_types
3811 ? cxx_make_type (TYPE_PACK_EXPANSION)
3812 : make_node (EXPR_PACK_EXPANSION);
3813 SET_PACK_EXPANSION_PATTERN (result, arg);
3814 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3815 {
3816 /* Propagate type and const-expression information. */
3817 TREE_TYPE (result) = TREE_TYPE (arg);
3818 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3819 /* Mark this read now, since the expansion might be length 0. */
3820 mark_exp_read (arg);
3821 }
3822 else
3823 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3824 they will rarely be compared to anything. */
3825 SET_TYPE_STRUCTURAL_EQUALITY (result);
3826
3827 /* Determine which parameter packs will be expanded. */
3828 ppd.parameter_packs = &parameter_packs;
3829 ppd.visited = new hash_set<tree>;
3830 ppd.type_pack_expansion_p = TYPE_P (arg);
3831 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3832 delete ppd.visited;
3833
3834 /* Make sure we found some parameter packs. */
3835 if (parameter_packs == NULL_TREE)
3836 {
3837 if (TYPE_P (arg))
3838 error ("expansion pattern %qT contains no argument packs", arg);
3839 else
3840 error ("expansion pattern %qE contains no argument packs", arg);
3841 return error_mark_node;
3842 }
3843 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3844
3845 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3846
3847 return result;
3848 }
3849
3850 /* Checks T for any "bare" parameter packs, which have not yet been
3851 expanded, and issues an error if any are found. This operation can
3852 only be done on full expressions or types (e.g., an expression
3853 statement, "if" condition, etc.), because we could have expressions like:
3854
3855 foo(f(g(h(args)))...)
3856
3857 where "args" is a parameter pack. check_for_bare_parameter_packs
3858 should not be called for the subexpressions args, h(args),
3859 g(h(args)), or f(g(h(args))), because we would produce erroneous
3860 error messages.
3861
3862 Returns TRUE and emits an error if there were bare parameter packs,
3863 returns FALSE otherwise. */
3864 bool
3865 check_for_bare_parameter_packs (tree t)
3866 {
3867 tree parameter_packs = NULL_TREE;
3868 struct find_parameter_pack_data ppd;
3869
3870 if (!processing_template_decl || !t || t == error_mark_node)
3871 return false;
3872
3873 /* A lambda might use a parameter pack from the containing context. */
3874 if (current_function_decl && LAMBDA_FUNCTION_P (current_function_decl))
3875 return false;
3876
3877 if (TREE_CODE (t) == TYPE_DECL)
3878 t = TREE_TYPE (t);
3879
3880 ppd.parameter_packs = &parameter_packs;
3881 ppd.visited = new hash_set<tree>;
3882 ppd.type_pack_expansion_p = false;
3883 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3884 delete ppd.visited;
3885
3886 if (parameter_packs)
3887 {
3888 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3889 error_at (loc, "parameter packs not expanded with %<...%>:");
3890 while (parameter_packs)
3891 {
3892 tree pack = TREE_VALUE (parameter_packs);
3893 tree name = NULL_TREE;
3894
3895 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3896 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3897 name = TYPE_NAME (pack);
3898 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3899 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3900 else if (TREE_CODE (pack) == CALL_EXPR)
3901 name = DECL_NAME (CALL_EXPR_FN (pack));
3902 else
3903 name = DECL_NAME (pack);
3904
3905 if (name)
3906 inform (loc, " %qD", name);
3907 else
3908 inform (loc, " <anonymous>");
3909
3910 parameter_packs = TREE_CHAIN (parameter_packs);
3911 }
3912
3913 return true;
3914 }
3915
3916 return false;
3917 }
3918
3919 /* Expand any parameter packs that occur in the template arguments in
3920 ARGS. */
3921 tree
3922 expand_template_argument_pack (tree args)
3923 {
3924 if (args == error_mark_node)
3925 return error_mark_node;
3926
3927 tree result_args = NULL_TREE;
3928 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3929 int num_result_args = -1;
3930 int non_default_args_count = -1;
3931
3932 /* First, determine if we need to expand anything, and the number of
3933 slots we'll need. */
3934 for (in_arg = 0; in_arg < nargs; ++in_arg)
3935 {
3936 tree arg = TREE_VEC_ELT (args, in_arg);
3937 if (arg == NULL_TREE)
3938 return args;
3939 if (ARGUMENT_PACK_P (arg))
3940 {
3941 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3942 if (num_result_args < 0)
3943 num_result_args = in_arg + num_packed;
3944 else
3945 num_result_args += num_packed;
3946 }
3947 else
3948 {
3949 if (num_result_args >= 0)
3950 num_result_args++;
3951 }
3952 }
3953
3954 /* If no expansion is necessary, we're done. */
3955 if (num_result_args < 0)
3956 return args;
3957
3958 /* Expand arguments. */
3959 result_args = make_tree_vec (num_result_args);
3960 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3961 non_default_args_count =
3962 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3963 for (in_arg = 0; in_arg < nargs; ++in_arg)
3964 {
3965 tree arg = TREE_VEC_ELT (args, in_arg);
3966 if (ARGUMENT_PACK_P (arg))
3967 {
3968 tree packed = ARGUMENT_PACK_ARGS (arg);
3969 int i, num_packed = TREE_VEC_LENGTH (packed);
3970 for (i = 0; i < num_packed; ++i, ++out_arg)
3971 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3972 if (non_default_args_count > 0)
3973 non_default_args_count += num_packed - 1;
3974 }
3975 else
3976 {
3977 TREE_VEC_ELT (result_args, out_arg) = arg;
3978 ++out_arg;
3979 }
3980 }
3981 if (non_default_args_count >= 0)
3982 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3983 return result_args;
3984 }
3985
3986 /* Checks if DECL shadows a template parameter.
3987
3988 [temp.local]: A template-parameter shall not be redeclared within its
3989 scope (including nested scopes).
3990
3991 Emits an error and returns TRUE if the DECL shadows a parameter,
3992 returns FALSE otherwise. */
3993
3994 bool
3995 check_template_shadow (tree decl)
3996 {
3997 tree olddecl;
3998
3999 /* If we're not in a template, we can't possibly shadow a template
4000 parameter. */
4001 if (!current_template_parms)
4002 return true;
4003
4004 /* Figure out what we're shadowing. */
4005 decl = OVL_FIRST (decl);
4006 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4007
4008 /* If there's no previous binding for this name, we're not shadowing
4009 anything, let alone a template parameter. */
4010 if (!olddecl)
4011 return true;
4012
4013 /* If we're not shadowing a template parameter, we're done. Note
4014 that OLDDECL might be an OVERLOAD (or perhaps even an
4015 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4016 node. */
4017 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4018 return true;
4019
4020 /* We check for decl != olddecl to avoid bogus errors for using a
4021 name inside a class. We check TPFI to avoid duplicate errors for
4022 inline member templates. */
4023 if (decl == olddecl
4024 || (DECL_TEMPLATE_PARM_P (decl)
4025 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4026 return true;
4027
4028 /* Don't complain about the injected class name, as we've already
4029 complained about the class itself. */
4030 if (DECL_SELF_REFERENCE_P (decl))
4031 return false;
4032
4033 if (DECL_TEMPLATE_PARM_P (decl))
4034 error ("declaration of template parameter %q+D shadows "
4035 "template parameter", decl);
4036 else
4037 error ("declaration of %q+#D shadows template parameter", decl);
4038 inform (DECL_SOURCE_LOCATION (olddecl),
4039 "template parameter %qD declared here", olddecl);
4040 return false;
4041 }
4042
4043 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4044 ORIG_LEVEL, DECL, and TYPE. */
4045
4046 static tree
4047 build_template_parm_index (int index,
4048 int level,
4049 int orig_level,
4050 tree decl,
4051 tree type)
4052 {
4053 tree t = make_node (TEMPLATE_PARM_INDEX);
4054 TEMPLATE_PARM_IDX (t) = index;
4055 TEMPLATE_PARM_LEVEL (t) = level;
4056 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4057 TEMPLATE_PARM_DECL (t) = decl;
4058 TREE_TYPE (t) = type;
4059 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4060 TREE_READONLY (t) = TREE_READONLY (decl);
4061
4062 return t;
4063 }
4064
4065 /* Find the canonical type parameter for the given template type
4066 parameter. Returns the canonical type parameter, which may be TYPE
4067 if no such parameter existed. */
4068
4069 static tree
4070 canonical_type_parameter (tree type)
4071 {
4072 tree list;
4073 int idx = TEMPLATE_TYPE_IDX (type);
4074 if (!canonical_template_parms)
4075 vec_alloc (canonical_template_parms, idx + 1);
4076
4077 if (canonical_template_parms->length () <= (unsigned) idx)
4078 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4079
4080 list = (*canonical_template_parms)[idx];
4081 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4082 list = TREE_CHAIN (list);
4083
4084 if (list)
4085 return TREE_VALUE (list);
4086 else
4087 {
4088 (*canonical_template_parms)[idx]
4089 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4090 return type;
4091 }
4092 }
4093
4094 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4095 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4096 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4097 new one is created. */
4098
4099 static tree
4100 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4101 tsubst_flags_t complain)
4102 {
4103 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4104 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4105 != TEMPLATE_PARM_LEVEL (index) - levels)
4106 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4107 {
4108 tree orig_decl = TEMPLATE_PARM_DECL (index);
4109 tree decl, t;
4110
4111 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4112 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4113 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4114 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4115 DECL_ARTIFICIAL (decl) = 1;
4116 SET_DECL_TEMPLATE_PARM_P (decl);
4117
4118 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4119 TEMPLATE_PARM_LEVEL (index) - levels,
4120 TEMPLATE_PARM_ORIG_LEVEL (index),
4121 decl, type);
4122 TEMPLATE_PARM_DESCENDANTS (index) = t;
4123 TEMPLATE_PARM_PARAMETER_PACK (t)
4124 = TEMPLATE_PARM_PARAMETER_PACK (index);
4125
4126 /* Template template parameters need this. */
4127 if (TREE_CODE (decl) == TEMPLATE_DECL)
4128 {
4129 DECL_TEMPLATE_RESULT (decl)
4130 = build_decl (DECL_SOURCE_LOCATION (decl),
4131 TYPE_DECL, DECL_NAME (decl), type);
4132 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4133 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4134 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4135 }
4136 }
4137
4138 return TEMPLATE_PARM_DESCENDANTS (index);
4139 }
4140
4141 /* Process information from new template parameter PARM and append it
4142 to the LIST being built. This new parameter is a non-type
4143 parameter iff IS_NON_TYPE is true. This new parameter is a
4144 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4145 is in PARM_LOC. */
4146
4147 tree
4148 process_template_parm (tree list, location_t parm_loc, tree parm,
4149 bool is_non_type, bool is_parameter_pack)
4150 {
4151 tree decl = 0;
4152 int idx = 0;
4153
4154 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4155 tree defval = TREE_PURPOSE (parm);
4156 tree constr = TREE_TYPE (parm);
4157
4158 if (list)
4159 {
4160 tree p = tree_last (list);
4161
4162 if (p && TREE_VALUE (p) != error_mark_node)
4163 {
4164 p = TREE_VALUE (p);
4165 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4166 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4167 else
4168 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4169 }
4170
4171 ++idx;
4172 }
4173
4174 if (is_non_type)
4175 {
4176 parm = TREE_VALUE (parm);
4177
4178 SET_DECL_TEMPLATE_PARM_P (parm);
4179
4180 if (TREE_TYPE (parm) != error_mark_node)
4181 {
4182 /* [temp.param]
4183
4184 The top-level cv-qualifiers on the template-parameter are
4185 ignored when determining its type. */
4186 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4187 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4188 TREE_TYPE (parm) = error_mark_node;
4189 else if (uses_parameter_packs (TREE_TYPE (parm))
4190 && !is_parameter_pack
4191 /* If we're in a nested template parameter list, the template
4192 template parameter could be a parameter pack. */
4193 && processing_template_parmlist == 1)
4194 {
4195 /* This template parameter is not a parameter pack, but it
4196 should be. Complain about "bare" parameter packs. */
4197 check_for_bare_parameter_packs (TREE_TYPE (parm));
4198
4199 /* Recover by calling this a parameter pack. */
4200 is_parameter_pack = true;
4201 }
4202 }
4203
4204 /* A template parameter is not modifiable. */
4205 TREE_CONSTANT (parm) = 1;
4206 TREE_READONLY (parm) = 1;
4207 decl = build_decl (parm_loc,
4208 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4209 TREE_CONSTANT (decl) = 1;
4210 TREE_READONLY (decl) = 1;
4211 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4212 = build_template_parm_index (idx, processing_template_decl,
4213 processing_template_decl,
4214 decl, TREE_TYPE (parm));
4215
4216 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4217 = is_parameter_pack;
4218 }
4219 else
4220 {
4221 tree t;
4222 parm = TREE_VALUE (TREE_VALUE (parm));
4223
4224 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4225 {
4226 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4227 /* This is for distinguishing between real templates and template
4228 template parameters */
4229 TREE_TYPE (parm) = t;
4230 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4231 decl = parm;
4232 }
4233 else
4234 {
4235 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4236 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4237 decl = build_decl (parm_loc,
4238 TYPE_DECL, parm, t);
4239 }
4240
4241 TYPE_NAME (t) = decl;
4242 TYPE_STUB_DECL (t) = decl;
4243 parm = decl;
4244 TEMPLATE_TYPE_PARM_INDEX (t)
4245 = build_template_parm_index (idx, processing_template_decl,
4246 processing_template_decl,
4247 decl, TREE_TYPE (parm));
4248 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4249 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4250 }
4251 DECL_ARTIFICIAL (decl) = 1;
4252 SET_DECL_TEMPLATE_PARM_P (decl);
4253
4254 /* Build requirements for the type/template parameter.
4255 This must be done after SET_DECL_TEMPLATE_PARM_P or
4256 process_template_parm could fail. */
4257 tree reqs = finish_shorthand_constraint (parm, constr);
4258
4259 pushdecl (decl);
4260
4261 /* Build the parameter node linking the parameter declaration,
4262 its default argument (if any), and its constraints (if any). */
4263 parm = build_tree_list (defval, parm);
4264 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4265
4266 return chainon (list, parm);
4267 }
4268
4269 /* The end of a template parameter list has been reached. Process the
4270 tree list into a parameter vector, converting each parameter into a more
4271 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4272 as PARM_DECLs. */
4273
4274 tree
4275 end_template_parm_list (tree parms)
4276 {
4277 int nparms;
4278 tree parm, next;
4279 tree saved_parmlist = make_tree_vec (list_length (parms));
4280
4281 /* Pop the dummy parameter level and add the real one. */
4282 current_template_parms = TREE_CHAIN (current_template_parms);
4283
4284 current_template_parms
4285 = tree_cons (size_int (processing_template_decl),
4286 saved_parmlist, current_template_parms);
4287
4288 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4289 {
4290 next = TREE_CHAIN (parm);
4291 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4292 TREE_CHAIN (parm) = NULL_TREE;
4293 }
4294
4295 --processing_template_parmlist;
4296
4297 return saved_parmlist;
4298 }
4299
4300 // Explicitly indicate the end of the template parameter list. We assume
4301 // that the current template parameters have been constructed and/or
4302 // managed explicitly, as when creating new template template parameters
4303 // from a shorthand constraint.
4304 void
4305 end_template_parm_list ()
4306 {
4307 --processing_template_parmlist;
4308 }
4309
4310 /* end_template_decl is called after a template declaration is seen. */
4311
4312 void
4313 end_template_decl (void)
4314 {
4315 reset_specialization ();
4316
4317 if (! processing_template_decl)
4318 return;
4319
4320 /* This matches the pushlevel in begin_template_parm_list. */
4321 finish_scope ();
4322
4323 --processing_template_decl;
4324 current_template_parms = TREE_CHAIN (current_template_parms);
4325 }
4326
4327 /* Takes a TREE_LIST representing a template parameter and convert it
4328 into an argument suitable to be passed to the type substitution
4329 functions. Note that If the TREE_LIST contains an error_mark
4330 node, the returned argument is error_mark_node. */
4331
4332 tree
4333 template_parm_to_arg (tree t)
4334 {
4335
4336 if (t == NULL_TREE
4337 || TREE_CODE (t) != TREE_LIST)
4338 return t;
4339
4340 if (error_operand_p (TREE_VALUE (t)))
4341 return error_mark_node;
4342
4343 t = TREE_VALUE (t);
4344
4345 if (TREE_CODE (t) == TYPE_DECL
4346 || TREE_CODE (t) == TEMPLATE_DECL)
4347 {
4348 t = TREE_TYPE (t);
4349
4350 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4351 {
4352 /* Turn this argument into a TYPE_ARGUMENT_PACK
4353 with a single element, which expands T. */
4354 tree vec = make_tree_vec (1);
4355 if (CHECKING_P)
4356 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4357
4358 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4359
4360 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4361 SET_ARGUMENT_PACK_ARGS (t, vec);
4362 }
4363 }
4364 else
4365 {
4366 t = DECL_INITIAL (t);
4367
4368 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4369 {
4370 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4371 with a single element, which expands T. */
4372 tree vec = make_tree_vec (1);
4373 if (CHECKING_P)
4374 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4375
4376 t = convert_from_reference (t);
4377 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4378
4379 t = make_node (NONTYPE_ARGUMENT_PACK);
4380 SET_ARGUMENT_PACK_ARGS (t, vec);
4381 }
4382 else
4383 t = convert_from_reference (t);
4384 }
4385 return t;
4386 }
4387
4388 /* Given a single level of template parameters (a TREE_VEC), return it
4389 as a set of template arguments. */
4390
4391 static tree
4392 template_parms_level_to_args (tree parms)
4393 {
4394 tree a = copy_node (parms);
4395 TREE_TYPE (a) = NULL_TREE;
4396 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4397 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4398
4399 if (CHECKING_P)
4400 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4401
4402 return a;
4403 }
4404
4405 /* Given a set of template parameters, return them as a set of template
4406 arguments. The template parameters are represented as a TREE_VEC, in
4407 the form documented in cp-tree.h for template arguments. */
4408
4409 static tree
4410 template_parms_to_args (tree parms)
4411 {
4412 tree header;
4413 tree args = NULL_TREE;
4414 int length = TMPL_PARMS_DEPTH (parms);
4415 int l = length;
4416
4417 /* If there is only one level of template parameters, we do not
4418 create a TREE_VEC of TREE_VECs. Instead, we return a single
4419 TREE_VEC containing the arguments. */
4420 if (length > 1)
4421 args = make_tree_vec (length);
4422
4423 for (header = parms; header; header = TREE_CHAIN (header))
4424 {
4425 tree a = template_parms_level_to_args (TREE_VALUE (header));
4426
4427 if (length > 1)
4428 TREE_VEC_ELT (args, --l) = a;
4429 else
4430 args = a;
4431 }
4432
4433 return args;
4434 }
4435
4436 /* Within the declaration of a template, return the currently active
4437 template parameters as an argument TREE_VEC. */
4438
4439 static tree
4440 current_template_args (void)
4441 {
4442 return template_parms_to_args (current_template_parms);
4443 }
4444
4445 /* Update the declared TYPE by doing any lookups which were thought to be
4446 dependent, but are not now that we know the SCOPE of the declarator. */
4447
4448 tree
4449 maybe_update_decl_type (tree orig_type, tree scope)
4450 {
4451 tree type = orig_type;
4452
4453 if (type == NULL_TREE)
4454 return type;
4455
4456 if (TREE_CODE (orig_type) == TYPE_DECL)
4457 type = TREE_TYPE (type);
4458
4459 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4460 && dependent_type_p (type)
4461 /* Don't bother building up the args in this case. */
4462 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4463 {
4464 /* tsubst in the args corresponding to the template parameters,
4465 including auto if present. Most things will be unchanged, but
4466 make_typename_type and tsubst_qualified_id will resolve
4467 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4468 tree args = current_template_args ();
4469 tree auto_node = type_uses_auto (type);
4470 tree pushed;
4471 if (auto_node)
4472 {
4473 tree auto_vec = make_tree_vec (1);
4474 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4475 args = add_to_template_args (args, auto_vec);
4476 }
4477 pushed = push_scope (scope);
4478 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4479 if (pushed)
4480 pop_scope (scope);
4481 }
4482
4483 if (type == error_mark_node)
4484 return orig_type;
4485
4486 if (TREE_CODE (orig_type) == TYPE_DECL)
4487 {
4488 if (same_type_p (type, TREE_TYPE (orig_type)))
4489 type = orig_type;
4490 else
4491 type = TYPE_NAME (type);
4492 }
4493 return type;
4494 }
4495
4496 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4497 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4498 the new template is a member template. */
4499
4500 tree
4501 build_template_decl (tree decl, tree parms, bool member_template_p)
4502 {
4503 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4504 DECL_TEMPLATE_PARMS (tmpl) = parms;
4505 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4506 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4507 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4508
4509 return tmpl;
4510 }
4511
4512 struct template_parm_data
4513 {
4514 /* The level of the template parameters we are currently
4515 processing. */
4516 int level;
4517
4518 /* The index of the specialization argument we are currently
4519 processing. */
4520 int current_arg;
4521
4522 /* An array whose size is the number of template parameters. The
4523 elements are nonzero if the parameter has been used in any one
4524 of the arguments processed so far. */
4525 int* parms;
4526
4527 /* An array whose size is the number of template arguments. The
4528 elements are nonzero if the argument makes use of template
4529 parameters of this level. */
4530 int* arg_uses_template_parms;
4531 };
4532
4533 /* Subroutine of push_template_decl used to see if each template
4534 parameter in a partial specialization is used in the explicit
4535 argument list. If T is of the LEVEL given in DATA (which is
4536 treated as a template_parm_data*), then DATA->PARMS is marked
4537 appropriately. */
4538
4539 static int
4540 mark_template_parm (tree t, void* data)
4541 {
4542 int level;
4543 int idx;
4544 struct template_parm_data* tpd = (struct template_parm_data*) data;
4545
4546 template_parm_level_and_index (t, &level, &idx);
4547
4548 if (level == tpd->level)
4549 {
4550 tpd->parms[idx] = 1;
4551 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4552 }
4553
4554 /* In C++17 the type of a non-type argument is a deduced context. */
4555 if (cxx_dialect >= cxx17
4556 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4557 for_each_template_parm (TREE_TYPE (t),
4558 &mark_template_parm,
4559 data,
4560 NULL,
4561 /*include_nondeduced_p=*/false);
4562
4563 /* Return zero so that for_each_template_parm will continue the
4564 traversal of the tree; we want to mark *every* template parm. */
4565 return 0;
4566 }
4567
4568 /* Process the partial specialization DECL. */
4569
4570 static tree
4571 process_partial_specialization (tree decl)
4572 {
4573 tree type = TREE_TYPE (decl);
4574 tree tinfo = get_template_info (decl);
4575 tree maintmpl = TI_TEMPLATE (tinfo);
4576 tree specargs = TI_ARGS (tinfo);
4577 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4578 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4579 tree inner_parms;
4580 tree inst;
4581 int nargs = TREE_VEC_LENGTH (inner_args);
4582 int ntparms;
4583 int i;
4584 bool did_error_intro = false;
4585 struct template_parm_data tpd;
4586 struct template_parm_data tpd2;
4587
4588 gcc_assert (current_template_parms);
4589
4590 /* A concept cannot be specialized. */
4591 if (flag_concepts && variable_concept_p (maintmpl))
4592 {
4593 error ("specialization of variable concept %q#D", maintmpl);
4594 return error_mark_node;
4595 }
4596
4597 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4598 ntparms = TREE_VEC_LENGTH (inner_parms);
4599
4600 /* We check that each of the template parameters given in the
4601 partial specialization is used in the argument list to the
4602 specialization. For example:
4603
4604 template <class T> struct S;
4605 template <class T> struct S<T*>;
4606
4607 The second declaration is OK because `T*' uses the template
4608 parameter T, whereas
4609
4610 template <class T> struct S<int>;
4611
4612 is no good. Even trickier is:
4613
4614 template <class T>
4615 struct S1
4616 {
4617 template <class U>
4618 struct S2;
4619 template <class U>
4620 struct S2<T>;
4621 };
4622
4623 The S2<T> declaration is actually invalid; it is a
4624 full-specialization. Of course,
4625
4626 template <class U>
4627 struct S2<T (*)(U)>;
4628
4629 or some such would have been OK. */
4630 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4631 tpd.parms = XALLOCAVEC (int, ntparms);
4632 memset (tpd.parms, 0, sizeof (int) * ntparms);
4633
4634 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4635 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4636 for (i = 0; i < nargs; ++i)
4637 {
4638 tpd.current_arg = i;
4639 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4640 &mark_template_parm,
4641 &tpd,
4642 NULL,
4643 /*include_nondeduced_p=*/false);
4644 }
4645 for (i = 0; i < ntparms; ++i)
4646 if (tpd.parms[i] == 0)
4647 {
4648 /* One of the template parms was not used in a deduced context in the
4649 specialization. */
4650 if (!did_error_intro)
4651 {
4652 error ("template parameters not deducible in "
4653 "partial specialization:");
4654 did_error_intro = true;
4655 }
4656
4657 inform (input_location, " %qD",
4658 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4659 }
4660
4661 if (did_error_intro)
4662 return error_mark_node;
4663
4664 /* [temp.class.spec]
4665
4666 The argument list of the specialization shall not be identical to
4667 the implicit argument list of the primary template. */
4668 tree main_args
4669 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4670 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4671 && (!flag_concepts
4672 || !strictly_subsumes (current_template_constraints (),
4673 get_constraints (maintmpl))))
4674 {
4675 if (!flag_concepts)
4676 error ("partial specialization %q+D does not specialize "
4677 "any template arguments", decl);
4678 else
4679 error ("partial specialization %q+D does not specialize any "
4680 "template arguments and is not more constrained than", decl);
4681 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4682 }
4683
4684 /* A partial specialization that replaces multiple parameters of the
4685 primary template with a pack expansion is less specialized for those
4686 parameters. */
4687 if (nargs < DECL_NTPARMS (maintmpl))
4688 {
4689 error ("partial specialization is not more specialized than the "
4690 "primary template because it replaces multiple parameters "
4691 "with a pack expansion");
4692 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4693 /* Avoid crash in process_partial_specialization. */
4694 return decl;
4695 }
4696
4697 /* If we aren't in a dependent class, we can actually try deduction. */
4698 else if (tpd.level == 1
4699 /* FIXME we should be able to handle a partial specialization of a
4700 partial instantiation, but currently we can't (c++/41727). */
4701 && TMPL_ARGS_DEPTH (specargs) == 1
4702 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4703 {
4704 if (permerror (input_location, "partial specialization %qD is not "
4705 "more specialized than", decl))
4706 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4707 maintmpl);
4708 }
4709
4710 /* [temp.class.spec]
4711
4712 A partially specialized non-type argument expression shall not
4713 involve template parameters of the partial specialization except
4714 when the argument expression is a simple identifier.
4715
4716 The type of a template parameter corresponding to a specialized
4717 non-type argument shall not be dependent on a parameter of the
4718 specialization.
4719
4720 Also, we verify that pack expansions only occur at the
4721 end of the argument list. */
4722 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4723 tpd2.parms = 0;
4724 for (i = 0; i < nargs; ++i)
4725 {
4726 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4727 tree arg = TREE_VEC_ELT (inner_args, i);
4728 tree packed_args = NULL_TREE;
4729 int j, len = 1;
4730
4731 if (ARGUMENT_PACK_P (arg))
4732 {
4733 /* Extract the arguments from the argument pack. We'll be
4734 iterating over these in the following loop. */
4735 packed_args = ARGUMENT_PACK_ARGS (arg);
4736 len = TREE_VEC_LENGTH (packed_args);
4737 }
4738
4739 for (j = 0; j < len; j++)
4740 {
4741 if (packed_args)
4742 /* Get the Jth argument in the parameter pack. */
4743 arg = TREE_VEC_ELT (packed_args, j);
4744
4745 if (PACK_EXPANSION_P (arg))
4746 {
4747 /* Pack expansions must come at the end of the
4748 argument list. */
4749 if ((packed_args && j < len - 1)
4750 || (!packed_args && i < nargs - 1))
4751 {
4752 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4753 error ("parameter pack argument %qE must be at the "
4754 "end of the template argument list", arg);
4755 else
4756 error ("parameter pack argument %qT must be at the "
4757 "end of the template argument list", arg);
4758 }
4759 }
4760
4761 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4762 /* We only care about the pattern. */
4763 arg = PACK_EXPANSION_PATTERN (arg);
4764
4765 if (/* These first two lines are the `non-type' bit. */
4766 !TYPE_P (arg)
4767 && TREE_CODE (arg) != TEMPLATE_DECL
4768 /* This next two lines are the `argument expression is not just a
4769 simple identifier' condition and also the `specialized
4770 non-type argument' bit. */
4771 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4772 && !(REFERENCE_REF_P (arg)
4773 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4774 {
4775 if ((!packed_args && tpd.arg_uses_template_parms[i])
4776 || (packed_args && uses_template_parms (arg)))
4777 error ("template argument %qE involves template parameter(s)",
4778 arg);
4779 else
4780 {
4781 /* Look at the corresponding template parameter,
4782 marking which template parameters its type depends
4783 upon. */
4784 tree type = TREE_TYPE (parm);
4785
4786 if (!tpd2.parms)
4787 {
4788 /* We haven't yet initialized TPD2. Do so now. */
4789 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4790 /* The number of parameters here is the number in the
4791 main template, which, as checked in the assertion
4792 above, is NARGS. */
4793 tpd2.parms = XALLOCAVEC (int, nargs);
4794 tpd2.level =
4795 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4796 }
4797
4798 /* Mark the template parameters. But this time, we're
4799 looking for the template parameters of the main
4800 template, not in the specialization. */
4801 tpd2.current_arg = i;
4802 tpd2.arg_uses_template_parms[i] = 0;
4803 memset (tpd2.parms, 0, sizeof (int) * nargs);
4804 for_each_template_parm (type,
4805 &mark_template_parm,
4806 &tpd2,
4807 NULL,
4808 /*include_nondeduced_p=*/false);
4809
4810 if (tpd2.arg_uses_template_parms [i])
4811 {
4812 /* The type depended on some template parameters.
4813 If they are fully specialized in the
4814 specialization, that's OK. */
4815 int j;
4816 int count = 0;
4817 for (j = 0; j < nargs; ++j)
4818 if (tpd2.parms[j] != 0
4819 && tpd.arg_uses_template_parms [j])
4820 ++count;
4821 if (count != 0)
4822 error_n (input_location, count,
4823 "type %qT of template argument %qE depends "
4824 "on a template parameter",
4825 "type %qT of template argument %qE depends "
4826 "on template parameters",
4827 type,
4828 arg);
4829 }
4830 }
4831 }
4832 }
4833 }
4834
4835 /* We should only get here once. */
4836 if (TREE_CODE (decl) == TYPE_DECL)
4837 gcc_assert (!COMPLETE_TYPE_P (type));
4838
4839 // Build the template decl.
4840 tree tmpl = build_template_decl (decl, current_template_parms,
4841 DECL_MEMBER_TEMPLATE_P (maintmpl));
4842 TREE_TYPE (tmpl) = type;
4843 DECL_TEMPLATE_RESULT (tmpl) = decl;
4844 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4845 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4846 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4847
4848 /* Give template template parms a DECL_CONTEXT of the template
4849 for which they are a parameter. */
4850 for (i = 0; i < ntparms; ++i)
4851 {
4852 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4853 if (TREE_CODE (parm) == TEMPLATE_DECL)
4854 DECL_CONTEXT (parm) = tmpl;
4855 }
4856
4857 if (VAR_P (decl))
4858 /* We didn't register this in check_explicit_specialization so we could
4859 wait until the constraints were set. */
4860 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4861 else
4862 associate_classtype_constraints (type);
4863
4864 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4865 = tree_cons (specargs, tmpl,
4866 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4867 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4868
4869 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4870 inst = TREE_CHAIN (inst))
4871 {
4872 tree instance = TREE_VALUE (inst);
4873 if (TYPE_P (instance)
4874 ? (COMPLETE_TYPE_P (instance)
4875 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4876 : DECL_TEMPLATE_INSTANTIATION (instance))
4877 {
4878 tree spec = most_specialized_partial_spec (instance, tf_none);
4879 tree inst_decl = (DECL_P (instance)
4880 ? instance : TYPE_NAME (instance));
4881 if (!spec)
4882 /* OK */;
4883 else if (spec == error_mark_node)
4884 permerror (input_location,
4885 "declaration of %qD ambiguates earlier template "
4886 "instantiation for %qD", decl, inst_decl);
4887 else if (TREE_VALUE (spec) == tmpl)
4888 permerror (input_location,
4889 "partial specialization of %qD after instantiation "
4890 "of %qD", decl, inst_decl);
4891 }
4892 }
4893
4894 return decl;
4895 }
4896
4897 /* PARM is a template parameter of some form; return the corresponding
4898 TEMPLATE_PARM_INDEX. */
4899
4900 static tree
4901 get_template_parm_index (tree parm)
4902 {
4903 if (TREE_CODE (parm) == PARM_DECL
4904 || TREE_CODE (parm) == CONST_DECL)
4905 parm = DECL_INITIAL (parm);
4906 else if (TREE_CODE (parm) == TYPE_DECL
4907 || TREE_CODE (parm) == TEMPLATE_DECL)
4908 parm = TREE_TYPE (parm);
4909 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4910 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4911 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4912 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4913 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4914 return parm;
4915 }
4916
4917 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4918 parameter packs used by the template parameter PARM. */
4919
4920 static void
4921 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4922 {
4923 /* A type parm can't refer to another parm. */
4924 if (TREE_CODE (parm) == TYPE_DECL)
4925 return;
4926 else if (TREE_CODE (parm) == PARM_DECL)
4927 {
4928 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4929 ppd, ppd->visited);
4930 return;
4931 }
4932
4933 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4934
4935 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4936 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4937 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4938 }
4939
4940 /* PARM is a template parameter pack. Return any parameter packs used in
4941 its type or the type of any of its template parameters. If there are
4942 any such packs, it will be instantiated into a fixed template parameter
4943 list by partial instantiation rather than be fully deduced. */
4944
4945 tree
4946 fixed_parameter_pack_p (tree parm)
4947 {
4948 /* This can only be true in a member template. */
4949 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4950 return NULL_TREE;
4951 /* This can only be true for a parameter pack. */
4952 if (!template_parameter_pack_p (parm))
4953 return NULL_TREE;
4954 /* A type parm can't refer to another parm. */
4955 if (TREE_CODE (parm) == TYPE_DECL)
4956 return NULL_TREE;
4957
4958 tree parameter_packs = NULL_TREE;
4959 struct find_parameter_pack_data ppd;
4960 ppd.parameter_packs = &parameter_packs;
4961 ppd.visited = new hash_set<tree>;
4962 ppd.type_pack_expansion_p = false;
4963
4964 fixed_parameter_pack_p_1 (parm, &ppd);
4965
4966 delete ppd.visited;
4967 return parameter_packs;
4968 }
4969
4970 /* Check that a template declaration's use of default arguments and
4971 parameter packs is not invalid. Here, PARMS are the template
4972 parameters. IS_PRIMARY is true if DECL is the thing declared by
4973 a primary template. IS_PARTIAL is true if DECL is a partial
4974 specialization.
4975
4976 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4977 declaration (but not a definition); 1 indicates a declaration, 2
4978 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4979 emitted for extraneous default arguments.
4980
4981 Returns TRUE if there were no errors found, FALSE otherwise. */
4982
4983 bool
4984 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4985 bool is_partial, int is_friend_decl)
4986 {
4987 const char *msg;
4988 int last_level_to_check;
4989 tree parm_level;
4990 bool no_errors = true;
4991
4992 /* [temp.param]
4993
4994 A default template-argument shall not be specified in a
4995 function template declaration or a function template definition, nor
4996 in the template-parameter-list of the definition of a member of a
4997 class template. */
4998
4999 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5000 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5001 /* You can't have a function template declaration in a local
5002 scope, nor you can you define a member of a class template in a
5003 local scope. */
5004 return true;
5005
5006 if ((TREE_CODE (decl) == TYPE_DECL
5007 && TREE_TYPE (decl)
5008 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5009 || (TREE_CODE (decl) == FUNCTION_DECL
5010 && LAMBDA_FUNCTION_P (decl)))
5011 /* A lambda doesn't have an explicit declaration; don't complain
5012 about the parms of the enclosing class. */
5013 return true;
5014
5015 if (current_class_type
5016 && !TYPE_BEING_DEFINED (current_class_type)
5017 && DECL_LANG_SPECIFIC (decl)
5018 && DECL_DECLARES_FUNCTION_P (decl)
5019 /* If this is either a friend defined in the scope of the class
5020 or a member function. */
5021 && (DECL_FUNCTION_MEMBER_P (decl)
5022 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5023 : DECL_FRIEND_CONTEXT (decl)
5024 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5025 : false)
5026 /* And, if it was a member function, it really was defined in
5027 the scope of the class. */
5028 && (!DECL_FUNCTION_MEMBER_P (decl)
5029 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5030 /* We already checked these parameters when the template was
5031 declared, so there's no need to do it again now. This function
5032 was defined in class scope, but we're processing its body now
5033 that the class is complete. */
5034 return true;
5035
5036 /* Core issue 226 (C++0x only): the following only applies to class
5037 templates. */
5038 if (is_primary
5039 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5040 {
5041 /* [temp.param]
5042
5043 If a template-parameter has a default template-argument, all
5044 subsequent template-parameters shall have a default
5045 template-argument supplied. */
5046 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5047 {
5048 tree inner_parms = TREE_VALUE (parm_level);
5049 int ntparms = TREE_VEC_LENGTH (inner_parms);
5050 int seen_def_arg_p = 0;
5051 int i;
5052
5053 for (i = 0; i < ntparms; ++i)
5054 {
5055 tree parm = TREE_VEC_ELT (inner_parms, i);
5056
5057 if (parm == error_mark_node)
5058 continue;
5059
5060 if (TREE_PURPOSE (parm))
5061 seen_def_arg_p = 1;
5062 else if (seen_def_arg_p
5063 && !template_parameter_pack_p (TREE_VALUE (parm)))
5064 {
5065 error ("no default argument for %qD", TREE_VALUE (parm));
5066 /* For better subsequent error-recovery, we indicate that
5067 there should have been a default argument. */
5068 TREE_PURPOSE (parm) = error_mark_node;
5069 no_errors = false;
5070 }
5071 else if (!is_partial
5072 && !is_friend_decl
5073 /* Don't complain about an enclosing partial
5074 specialization. */
5075 && parm_level == parms
5076 && TREE_CODE (decl) == TYPE_DECL
5077 && i < ntparms - 1
5078 && template_parameter_pack_p (TREE_VALUE (parm))
5079 /* A fixed parameter pack will be partially
5080 instantiated into a fixed length list. */
5081 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5082 {
5083 /* A primary class template can only have one
5084 parameter pack, at the end of the template
5085 parameter list. */
5086
5087 error ("parameter pack %q+D must be at the end of the"
5088 " template parameter list", TREE_VALUE (parm));
5089
5090 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5091 = error_mark_node;
5092 no_errors = false;
5093 }
5094 }
5095 }
5096 }
5097
5098 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5099 || is_partial
5100 || !is_primary
5101 || is_friend_decl)
5102 /* For an ordinary class template, default template arguments are
5103 allowed at the innermost level, e.g.:
5104 template <class T = int>
5105 struct S {};
5106 but, in a partial specialization, they're not allowed even
5107 there, as we have in [temp.class.spec]:
5108
5109 The template parameter list of a specialization shall not
5110 contain default template argument values.
5111
5112 So, for a partial specialization, or for a function template
5113 (in C++98/C++03), we look at all of them. */
5114 ;
5115 else
5116 /* But, for a primary class template that is not a partial
5117 specialization we look at all template parameters except the
5118 innermost ones. */
5119 parms = TREE_CHAIN (parms);
5120
5121 /* Figure out what error message to issue. */
5122 if (is_friend_decl == 2)
5123 msg = G_("default template arguments may not be used in function template "
5124 "friend re-declaration");
5125 else if (is_friend_decl)
5126 msg = G_("default template arguments may not be used in function template "
5127 "friend declarations");
5128 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5129 msg = G_("default template arguments may not be used in function templates "
5130 "without -std=c++11 or -std=gnu++11");
5131 else if (is_partial)
5132 msg = G_("default template arguments may not be used in "
5133 "partial specializations");
5134 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5135 msg = G_("default argument for template parameter for class enclosing %qD");
5136 else
5137 /* Per [temp.param]/9, "A default template-argument shall not be
5138 specified in the template-parameter-lists of the definition of
5139 a member of a class template that appears outside of the member's
5140 class.", thus if we aren't handling a member of a class template
5141 there is no need to examine the parameters. */
5142 return true;
5143
5144 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5145 /* If we're inside a class definition, there's no need to
5146 examine the parameters to the class itself. On the one
5147 hand, they will be checked when the class is defined, and,
5148 on the other, default arguments are valid in things like:
5149 template <class T = double>
5150 struct S { template <class U> void f(U); };
5151 Here the default argument for `S' has no bearing on the
5152 declaration of `f'. */
5153 last_level_to_check = template_class_depth (current_class_type) + 1;
5154 else
5155 /* Check everything. */
5156 last_level_to_check = 0;
5157
5158 for (parm_level = parms;
5159 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5160 parm_level = TREE_CHAIN (parm_level))
5161 {
5162 tree inner_parms = TREE_VALUE (parm_level);
5163 int i;
5164 int ntparms;
5165
5166 ntparms = TREE_VEC_LENGTH (inner_parms);
5167 for (i = 0; i < ntparms; ++i)
5168 {
5169 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5170 continue;
5171
5172 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5173 {
5174 if (msg)
5175 {
5176 no_errors = false;
5177 if (is_friend_decl == 2)
5178 return no_errors;
5179
5180 error (msg, decl);
5181 msg = 0;
5182 }
5183
5184 /* Clear out the default argument so that we are not
5185 confused later. */
5186 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5187 }
5188 }
5189
5190 /* At this point, if we're still interested in issuing messages,
5191 they must apply to classes surrounding the object declared. */
5192 if (msg)
5193 msg = G_("default argument for template parameter for class "
5194 "enclosing %qD");
5195 }
5196
5197 return no_errors;
5198 }
5199
5200 /* Worker for push_template_decl_real, called via
5201 for_each_template_parm. DATA is really an int, indicating the
5202 level of the parameters we are interested in. If T is a template
5203 parameter of that level, return nonzero. */
5204
5205 static int
5206 template_parm_this_level_p (tree t, void* data)
5207 {
5208 int this_level = *(int *)data;
5209 int level;
5210
5211 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5212 level = TEMPLATE_PARM_LEVEL (t);
5213 else
5214 level = TEMPLATE_TYPE_LEVEL (t);
5215 return level == this_level;
5216 }
5217
5218 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5219 DATA is really an int, indicating the innermost outer level of parameters.
5220 If T is a template parameter of that level or further out, return
5221 nonzero. */
5222
5223 static int
5224 template_parm_outer_level (tree t, void *data)
5225 {
5226 int this_level = *(int *)data;
5227 int level;
5228
5229 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5230 level = TEMPLATE_PARM_LEVEL (t);
5231 else
5232 level = TEMPLATE_TYPE_LEVEL (t);
5233 return level <= this_level;
5234 }
5235
5236 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5237 parameters given by current_template_args, or reuses a
5238 previously existing one, if appropriate. Returns the DECL, or an
5239 equivalent one, if it is replaced via a call to duplicate_decls.
5240
5241 If IS_FRIEND is true, DECL is a friend declaration. */
5242
5243 tree
5244 push_template_decl_real (tree decl, bool is_friend)
5245 {
5246 tree tmpl;
5247 tree args;
5248 tree info;
5249 tree ctx;
5250 bool is_primary;
5251 bool is_partial;
5252 int new_template_p = 0;
5253 /* True if the template is a member template, in the sense of
5254 [temp.mem]. */
5255 bool member_template_p = false;
5256
5257 if (decl == error_mark_node || !current_template_parms)
5258 return error_mark_node;
5259
5260 /* See if this is a partial specialization. */
5261 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5262 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5263 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5264 || (VAR_P (decl)
5265 && DECL_LANG_SPECIFIC (decl)
5266 && DECL_TEMPLATE_SPECIALIZATION (decl)
5267 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5268
5269 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5270 is_friend = true;
5271
5272 if (is_friend)
5273 /* For a friend, we want the context of the friend function, not
5274 the type of which it is a friend. */
5275 ctx = CP_DECL_CONTEXT (decl);
5276 else if (CP_DECL_CONTEXT (decl)
5277 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5278 /* In the case of a virtual function, we want the class in which
5279 it is defined. */
5280 ctx = CP_DECL_CONTEXT (decl);
5281 else
5282 /* Otherwise, if we're currently defining some class, the DECL
5283 is assumed to be a member of the class. */
5284 ctx = current_scope ();
5285
5286 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5287 ctx = NULL_TREE;
5288
5289 if (!DECL_CONTEXT (decl))
5290 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5291
5292 /* See if this is a primary template. */
5293 if (is_friend && ctx
5294 && uses_template_parms_level (ctx, processing_template_decl))
5295 /* A friend template that specifies a class context, i.e.
5296 template <typename T> friend void A<T>::f();
5297 is not primary. */
5298 is_primary = false;
5299 else if (TREE_CODE (decl) == TYPE_DECL
5300 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5301 is_primary = false;
5302 else
5303 is_primary = template_parm_scope_p ();
5304
5305 if (is_primary)
5306 {
5307 warning (OPT_Wtemplates, "template %qD declared", decl);
5308
5309 if (DECL_CLASS_SCOPE_P (decl))
5310 member_template_p = true;
5311 if (TREE_CODE (decl) == TYPE_DECL
5312 && anon_aggrname_p (DECL_NAME (decl)))
5313 {
5314 error ("template class without a name");
5315 return error_mark_node;
5316 }
5317 else if (TREE_CODE (decl) == FUNCTION_DECL)
5318 {
5319 if (member_template_p)
5320 {
5321 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5322 error ("member template %qD may not have virt-specifiers", decl);
5323 }
5324 if (DECL_DESTRUCTOR_P (decl))
5325 {
5326 /* [temp.mem]
5327
5328 A destructor shall not be a member template. */
5329 error ("destructor %qD declared as member template", decl);
5330 return error_mark_node;
5331 }
5332 if (IDENTIFIER_OVL_OP_P (DECL_NAME (decl))
5333 && (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (decl))
5334 & OVL_OP_FLAG_ALLOC)
5335 && (!prototype_p (TREE_TYPE (decl))
5336 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5337 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5338 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5339 == void_list_node)))
5340 {
5341 /* [basic.stc.dynamic.allocation]
5342
5343 An allocation function can be a function
5344 template. ... Template allocation functions shall
5345 have two or more parameters. */
5346 error ("invalid template declaration of %qD", decl);
5347 return error_mark_node;
5348 }
5349 }
5350 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5351 && CLASS_TYPE_P (TREE_TYPE (decl)))
5352 {
5353 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5354 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5355 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5356 {
5357 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5358 if (TREE_CODE (t) == TYPE_DECL)
5359 t = TREE_TYPE (t);
5360 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5361 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5362 }
5363 }
5364 else if (TREE_CODE (decl) == TYPE_DECL
5365 && TYPE_DECL_ALIAS_P (decl))
5366 /* alias-declaration */
5367 gcc_assert (!DECL_ARTIFICIAL (decl));
5368 else if (VAR_P (decl))
5369 /* C++14 variable template. */;
5370 else
5371 {
5372 error ("template declaration of %q#D", decl);
5373 return error_mark_node;
5374 }
5375 }
5376
5377 /* Check to see that the rules regarding the use of default
5378 arguments are not being violated. */
5379 check_default_tmpl_args (decl, current_template_parms,
5380 is_primary, is_partial, /*is_friend_decl=*/0);
5381
5382 /* Ensure that there are no parameter packs in the type of this
5383 declaration that have not been expanded. */
5384 if (TREE_CODE (decl) == FUNCTION_DECL)
5385 {
5386 /* Check each of the arguments individually to see if there are
5387 any bare parameter packs. */
5388 tree type = TREE_TYPE (decl);
5389 tree arg = DECL_ARGUMENTS (decl);
5390 tree argtype = TYPE_ARG_TYPES (type);
5391
5392 while (arg && argtype)
5393 {
5394 if (!DECL_PACK_P (arg)
5395 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5396 {
5397 /* This is a PARM_DECL that contains unexpanded parameter
5398 packs. We have already complained about this in the
5399 check_for_bare_parameter_packs call, so just replace
5400 these types with ERROR_MARK_NODE. */
5401 TREE_TYPE (arg) = error_mark_node;
5402 TREE_VALUE (argtype) = error_mark_node;
5403 }
5404
5405 arg = DECL_CHAIN (arg);
5406 argtype = TREE_CHAIN (argtype);
5407 }
5408
5409 /* Check for bare parameter packs in the return type and the
5410 exception specifiers. */
5411 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5412 /* Errors were already issued, set return type to int
5413 as the frontend doesn't expect error_mark_node as
5414 the return type. */
5415 TREE_TYPE (type) = integer_type_node;
5416 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5417 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5418 }
5419 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5420 && TYPE_DECL_ALIAS_P (decl))
5421 ? DECL_ORIGINAL_TYPE (decl)
5422 : TREE_TYPE (decl)))
5423 {
5424 TREE_TYPE (decl) = error_mark_node;
5425 return error_mark_node;
5426 }
5427
5428 if (is_partial)
5429 return process_partial_specialization (decl);
5430
5431 args = current_template_args ();
5432
5433 if (!ctx
5434 || TREE_CODE (ctx) == FUNCTION_DECL
5435 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5436 || (TREE_CODE (decl) == TYPE_DECL
5437 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5438 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5439 {
5440 if (DECL_LANG_SPECIFIC (decl)
5441 && DECL_TEMPLATE_INFO (decl)
5442 && DECL_TI_TEMPLATE (decl))
5443 tmpl = DECL_TI_TEMPLATE (decl);
5444 /* If DECL is a TYPE_DECL for a class-template, then there won't
5445 be DECL_LANG_SPECIFIC. The information equivalent to
5446 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5447 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5448 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5449 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5450 {
5451 /* Since a template declaration already existed for this
5452 class-type, we must be redeclaring it here. Make sure
5453 that the redeclaration is valid. */
5454 redeclare_class_template (TREE_TYPE (decl),
5455 current_template_parms,
5456 current_template_constraints ());
5457 /* We don't need to create a new TEMPLATE_DECL; just use the
5458 one we already had. */
5459 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5460 }
5461 else
5462 {
5463 tmpl = build_template_decl (decl, current_template_parms,
5464 member_template_p);
5465 new_template_p = 1;
5466
5467 if (DECL_LANG_SPECIFIC (decl)
5468 && DECL_TEMPLATE_SPECIALIZATION (decl))
5469 {
5470 /* A specialization of a member template of a template
5471 class. */
5472 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5473 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5474 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5475 }
5476 }
5477 }
5478 else
5479 {
5480 tree a, t, current, parms;
5481 int i;
5482 tree tinfo = get_template_info (decl);
5483
5484 if (!tinfo)
5485 {
5486 error ("template definition of non-template %q#D", decl);
5487 return error_mark_node;
5488 }
5489
5490 tmpl = TI_TEMPLATE (tinfo);
5491
5492 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5493 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5494 && DECL_TEMPLATE_SPECIALIZATION (decl)
5495 && DECL_MEMBER_TEMPLATE_P (tmpl))
5496 {
5497 tree new_tmpl;
5498
5499 /* The declaration is a specialization of a member
5500 template, declared outside the class. Therefore, the
5501 innermost template arguments will be NULL, so we
5502 replace them with the arguments determined by the
5503 earlier call to check_explicit_specialization. */
5504 args = DECL_TI_ARGS (decl);
5505
5506 new_tmpl
5507 = build_template_decl (decl, current_template_parms,
5508 member_template_p);
5509 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5510 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5511 DECL_TI_TEMPLATE (decl) = new_tmpl;
5512 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5513 DECL_TEMPLATE_INFO (new_tmpl)
5514 = build_template_info (tmpl, args);
5515
5516 register_specialization (new_tmpl,
5517 most_general_template (tmpl),
5518 args,
5519 is_friend, 0);
5520 return decl;
5521 }
5522
5523 /* Make sure the template headers we got make sense. */
5524
5525 parms = DECL_TEMPLATE_PARMS (tmpl);
5526 i = TMPL_PARMS_DEPTH (parms);
5527 if (TMPL_ARGS_DEPTH (args) != i)
5528 {
5529 error ("expected %d levels of template parms for %q#D, got %d",
5530 i, decl, TMPL_ARGS_DEPTH (args));
5531 DECL_INTERFACE_KNOWN (decl) = 1;
5532 return error_mark_node;
5533 }
5534 else
5535 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5536 {
5537 a = TMPL_ARGS_LEVEL (args, i);
5538 t = INNERMOST_TEMPLATE_PARMS (parms);
5539
5540 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5541 {
5542 if (current == decl)
5543 error ("got %d template parameters for %q#D",
5544 TREE_VEC_LENGTH (a), decl);
5545 else
5546 error ("got %d template parameters for %q#T",
5547 TREE_VEC_LENGTH (a), current);
5548 error (" but %d required", TREE_VEC_LENGTH (t));
5549 /* Avoid crash in import_export_decl. */
5550 DECL_INTERFACE_KNOWN (decl) = 1;
5551 return error_mark_node;
5552 }
5553
5554 if (current == decl)
5555 current = ctx;
5556 else if (current == NULL_TREE)
5557 /* Can happen in erroneous input. */
5558 break;
5559 else
5560 current = get_containing_scope (current);
5561 }
5562
5563 /* Check that the parms are used in the appropriate qualifying scopes
5564 in the declarator. */
5565 if (!comp_template_args
5566 (TI_ARGS (tinfo),
5567 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5568 {
5569 error ("template arguments to %qD do not match original "
5570 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5571 if (!uses_template_parms (TI_ARGS (tinfo)))
5572 inform (input_location, "use %<template<>%> for"
5573 " an explicit specialization");
5574 /* Avoid crash in import_export_decl. */
5575 DECL_INTERFACE_KNOWN (decl) = 1;
5576 return error_mark_node;
5577 }
5578 }
5579
5580 DECL_TEMPLATE_RESULT (tmpl) = decl;
5581 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5582
5583 /* Push template declarations for global functions and types. Note
5584 that we do not try to push a global template friend declared in a
5585 template class; such a thing may well depend on the template
5586 parameters of the class. */
5587 if (new_template_p && !ctx
5588 && !(is_friend && template_class_depth (current_class_type) > 0))
5589 {
5590 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5591 if (tmpl == error_mark_node)
5592 return error_mark_node;
5593
5594 /* Hide template friend classes that haven't been declared yet. */
5595 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5596 {
5597 DECL_ANTICIPATED (tmpl) = 1;
5598 DECL_FRIEND_P (tmpl) = 1;
5599 }
5600 }
5601
5602 if (is_primary)
5603 {
5604 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5605
5606 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5607
5608 /* Give template template parms a DECL_CONTEXT of the template
5609 for which they are a parameter. */
5610 parms = INNERMOST_TEMPLATE_PARMS (parms);
5611 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5612 {
5613 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5614 if (TREE_CODE (parm) == TEMPLATE_DECL)
5615 DECL_CONTEXT (parm) = tmpl;
5616 }
5617
5618 if (TREE_CODE (decl) == TYPE_DECL
5619 && TYPE_DECL_ALIAS_P (decl)
5620 && complex_alias_template_p (tmpl))
5621 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5622 }
5623
5624 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5625 back to its most general template. If TMPL is a specialization,
5626 ARGS may only have the innermost set of arguments. Add the missing
5627 argument levels if necessary. */
5628 if (DECL_TEMPLATE_INFO (tmpl))
5629 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5630
5631 info = build_template_info (tmpl, args);
5632
5633 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5634 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5635 else
5636 {
5637 if (is_primary)
5638 retrofit_lang_decl (decl);
5639 if (DECL_LANG_SPECIFIC (decl))
5640 DECL_TEMPLATE_INFO (decl) = info;
5641 }
5642
5643 if (flag_implicit_templates
5644 && !is_friend
5645 && TREE_PUBLIC (decl)
5646 && VAR_OR_FUNCTION_DECL_P (decl))
5647 /* Set DECL_COMDAT on template instantiations; if we force
5648 them to be emitted by explicit instantiation or -frepo,
5649 mark_needed will tell cgraph to do the right thing. */
5650 DECL_COMDAT (decl) = true;
5651
5652 return DECL_TEMPLATE_RESULT (tmpl);
5653 }
5654
5655 tree
5656 push_template_decl (tree decl)
5657 {
5658 return push_template_decl_real (decl, false);
5659 }
5660
5661 /* FN is an inheriting constructor that inherits from the constructor
5662 template INHERITED; turn FN into a constructor template with a matching
5663 template header. */
5664
5665 tree
5666 add_inherited_template_parms (tree fn, tree inherited)
5667 {
5668 tree inner_parms
5669 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5670 inner_parms = copy_node (inner_parms);
5671 tree parms
5672 = tree_cons (size_int (processing_template_decl + 1),
5673 inner_parms, current_template_parms);
5674 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5675 tree args = template_parms_to_args (parms);
5676 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5677 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5678 DECL_TEMPLATE_RESULT (tmpl) = fn;
5679 DECL_ARTIFICIAL (tmpl) = true;
5680 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5681 return tmpl;
5682 }
5683
5684 /* Called when a class template TYPE is redeclared with the indicated
5685 template PARMS, e.g.:
5686
5687 template <class T> struct S;
5688 template <class T> struct S {}; */
5689
5690 bool
5691 redeclare_class_template (tree type, tree parms, tree cons)
5692 {
5693 tree tmpl;
5694 tree tmpl_parms;
5695 int i;
5696
5697 if (!TYPE_TEMPLATE_INFO (type))
5698 {
5699 error ("%qT is not a template type", type);
5700 return false;
5701 }
5702
5703 tmpl = TYPE_TI_TEMPLATE (type);
5704 if (!PRIMARY_TEMPLATE_P (tmpl))
5705 /* The type is nested in some template class. Nothing to worry
5706 about here; there are no new template parameters for the nested
5707 type. */
5708 return true;
5709
5710 if (!parms)
5711 {
5712 error ("template specifiers not specified in declaration of %qD",
5713 tmpl);
5714 return false;
5715 }
5716
5717 parms = INNERMOST_TEMPLATE_PARMS (parms);
5718 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5719
5720 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5721 {
5722 error_n (input_location, TREE_VEC_LENGTH (parms),
5723 "redeclared with %d template parameter",
5724 "redeclared with %d template parameters",
5725 TREE_VEC_LENGTH (parms));
5726 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5727 "previous declaration %qD used %d template parameter",
5728 "previous declaration %qD used %d template parameters",
5729 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5730 return false;
5731 }
5732
5733 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5734 {
5735 tree tmpl_parm;
5736 tree parm;
5737 tree tmpl_default;
5738 tree parm_default;
5739
5740 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5741 || TREE_VEC_ELT (parms, i) == error_mark_node)
5742 continue;
5743
5744 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5745 if (error_operand_p (tmpl_parm))
5746 return false;
5747
5748 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5749 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5750 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5751
5752 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5753 TEMPLATE_DECL. */
5754 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5755 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5756 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5757 || (TREE_CODE (tmpl_parm) != PARM_DECL
5758 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5759 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5760 || (TREE_CODE (tmpl_parm) == PARM_DECL
5761 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5762 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5763 {
5764 error ("template parameter %q+#D", tmpl_parm);
5765 error ("redeclared here as %q#D", parm);
5766 return false;
5767 }
5768
5769 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5770 {
5771 /* We have in [temp.param]:
5772
5773 A template-parameter may not be given default arguments
5774 by two different declarations in the same scope. */
5775 error_at (input_location, "redefinition of default argument for %q#D", parm);
5776 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5777 "original definition appeared here");
5778 return false;
5779 }
5780
5781 if (parm_default != NULL_TREE)
5782 /* Update the previous template parameters (which are the ones
5783 that will really count) with the new default value. */
5784 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5785 else if (tmpl_default != NULL_TREE)
5786 /* Update the new parameters, too; they'll be used as the
5787 parameters for any members. */
5788 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5789
5790 /* Give each template template parm in this redeclaration a
5791 DECL_CONTEXT of the template for which they are a parameter. */
5792 if (TREE_CODE (parm) == TEMPLATE_DECL)
5793 {
5794 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5795 DECL_CONTEXT (parm) = tmpl;
5796 }
5797
5798 if (TREE_CODE (parm) == TYPE_DECL)
5799 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5800 }
5801
5802 // Cannot redeclare a class template with a different set of constraints.
5803 if (!equivalent_constraints (get_constraints (tmpl), cons))
5804 {
5805 error_at (input_location, "redeclaration %q#D with different "
5806 "constraints", tmpl);
5807 inform (DECL_SOURCE_LOCATION (tmpl),
5808 "original declaration appeared here");
5809 }
5810
5811 return true;
5812 }
5813
5814 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5815 to be used when the caller has already checked
5816 (processing_template_decl
5817 && !instantiation_dependent_expression_p (expr)
5818 && potential_constant_expression (expr))
5819 and cleared processing_template_decl. */
5820
5821 tree
5822 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5823 {
5824 return tsubst_copy_and_build (expr,
5825 /*args=*/NULL_TREE,
5826 complain,
5827 /*in_decl=*/NULL_TREE,
5828 /*function_p=*/false,
5829 /*integral_constant_expression_p=*/true);
5830 }
5831
5832 /* Simplify EXPR if it is a non-dependent expression. Returns the
5833 (possibly simplified) expression. */
5834
5835 tree
5836 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5837 {
5838 if (expr == NULL_TREE)
5839 return NULL_TREE;
5840
5841 /* If we're in a template, but EXPR isn't value dependent, simplify
5842 it. We're supposed to treat:
5843
5844 template <typename T> void f(T[1 + 1]);
5845 template <typename T> void f(T[2]);
5846
5847 as two declarations of the same function, for example. */
5848 if (processing_template_decl
5849 && is_nondependent_constant_expression (expr))
5850 {
5851 processing_template_decl_sentinel s;
5852 expr = instantiate_non_dependent_expr_internal (expr, complain);
5853 }
5854 return expr;
5855 }
5856
5857 tree
5858 instantiate_non_dependent_expr (tree expr)
5859 {
5860 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5861 }
5862
5863 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5864 an uninstantiated expression. */
5865
5866 tree
5867 instantiate_non_dependent_or_null (tree expr)
5868 {
5869 if (expr == NULL_TREE)
5870 return NULL_TREE;
5871 if (processing_template_decl)
5872 {
5873 if (!is_nondependent_constant_expression (expr))
5874 expr = NULL_TREE;
5875 else
5876 {
5877 processing_template_decl_sentinel s;
5878 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5879 }
5880 }
5881 return expr;
5882 }
5883
5884 /* True iff T is a specialization of a variable template. */
5885
5886 bool
5887 variable_template_specialization_p (tree t)
5888 {
5889 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5890 return false;
5891 tree tmpl = DECL_TI_TEMPLATE (t);
5892 return variable_template_p (tmpl);
5893 }
5894
5895 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5896 template declaration, or a TYPE_DECL for an alias declaration. */
5897
5898 bool
5899 alias_type_or_template_p (tree t)
5900 {
5901 if (t == NULL_TREE)
5902 return false;
5903 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5904 || (TYPE_P (t)
5905 && TYPE_NAME (t)
5906 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5907 || DECL_ALIAS_TEMPLATE_P (t));
5908 }
5909
5910 /* Return TRUE iff T is a specialization of an alias template. */
5911
5912 bool
5913 alias_template_specialization_p (const_tree t)
5914 {
5915 /* It's an alias template specialization if it's an alias and its
5916 TYPE_NAME is a specialization of a primary template. */
5917 if (TYPE_ALIAS_P (t))
5918 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5919 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5920
5921 return false;
5922 }
5923
5924 /* An alias template is complex from a SFINAE perspective if a template-id
5925 using that alias can be ill-formed when the expansion is not, as with
5926 the void_t template. We determine this by checking whether the
5927 expansion for the alias template uses all its template parameters. */
5928
5929 struct uses_all_template_parms_data
5930 {
5931 int level;
5932 bool *seen;
5933 };
5934
5935 static int
5936 uses_all_template_parms_r (tree t, void *data_)
5937 {
5938 struct uses_all_template_parms_data &data
5939 = *(struct uses_all_template_parms_data*)data_;
5940 tree idx = get_template_parm_index (t);
5941
5942 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5943 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5944 return 0;
5945 }
5946
5947 static bool
5948 complex_alias_template_p (const_tree tmpl)
5949 {
5950 struct uses_all_template_parms_data data;
5951 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5952 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5953 data.level = TMPL_PARMS_DEPTH (parms);
5954 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5955 data.seen = XALLOCAVEC (bool, len);
5956 for (int i = 0; i < len; ++i)
5957 data.seen[i] = false;
5958
5959 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5960 for (int i = 0; i < len; ++i)
5961 if (!data.seen[i])
5962 return true;
5963 return false;
5964 }
5965
5966 /* Return TRUE iff T is a specialization of a complex alias template with
5967 dependent template-arguments. */
5968
5969 bool
5970 dependent_alias_template_spec_p (const_tree t)
5971 {
5972 if (!alias_template_specialization_p (t))
5973 return false;
5974
5975 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5976 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5977 return false;
5978
5979 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5980 if (!any_dependent_template_arguments_p (args))
5981 return false;
5982
5983 return true;
5984 }
5985
5986 /* Return the number of innermost template parameters in TMPL. */
5987
5988 static int
5989 num_innermost_template_parms (tree tmpl)
5990 {
5991 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5992 return TREE_VEC_LENGTH (parms);
5993 }
5994
5995 /* Return either TMPL or another template that it is equivalent to under DR
5996 1286: An alias that just changes the name of a template is equivalent to
5997 the other template. */
5998
5999 static tree
6000 get_underlying_template (tree tmpl)
6001 {
6002 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6003 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6004 {
6005 /* Determine if the alias is equivalent to an underlying template. */
6006 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6007 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6008 if (!tinfo)
6009 break;
6010
6011 tree underlying = TI_TEMPLATE (tinfo);
6012 if (!PRIMARY_TEMPLATE_P (underlying)
6013 || (num_innermost_template_parms (tmpl)
6014 != num_innermost_template_parms (underlying)))
6015 break;
6016
6017 tree alias_args = INNERMOST_TEMPLATE_ARGS
6018 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6019 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6020 break;
6021
6022 /* Alias is equivalent. Strip it and repeat. */
6023 tmpl = underlying;
6024 }
6025
6026 return tmpl;
6027 }
6028
6029 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6030 must be a reference-to-function or a pointer-to-function type, as specified
6031 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6032 and check that the resulting function has external linkage. */
6033
6034 static tree
6035 convert_nontype_argument_function (tree type, tree expr,
6036 tsubst_flags_t complain)
6037 {
6038 tree fns = expr;
6039 tree fn, fn_no_ptr;
6040 linkage_kind linkage;
6041
6042 fn = instantiate_type (type, fns, tf_none);
6043 if (fn == error_mark_node)
6044 return error_mark_node;
6045
6046 if (value_dependent_expression_p (fn))
6047 goto accept;
6048
6049 fn_no_ptr = strip_fnptr_conv (fn);
6050 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6051 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6052 if (BASELINK_P (fn_no_ptr))
6053 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6054
6055 /* [temp.arg.nontype]/1
6056
6057 A template-argument for a non-type, non-template template-parameter
6058 shall be one of:
6059 [...]
6060 -- the address of an object or function with external [C++11: or
6061 internal] linkage. */
6062
6063 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6064 {
6065 if (complain & tf_error)
6066 {
6067 error ("%qE is not a valid template argument for type %qT",
6068 expr, type);
6069 if (TYPE_PTR_P (type))
6070 inform (input_location, "it must be the address of a function "
6071 "with external linkage");
6072 else
6073 inform (input_location, "it must be the name of a function with "
6074 "external linkage");
6075 }
6076 return NULL_TREE;
6077 }
6078
6079 linkage = decl_linkage (fn_no_ptr);
6080 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6081 {
6082 if (complain & tf_error)
6083 {
6084 if (cxx_dialect >= cxx11)
6085 error ("%qE is not a valid template argument for type %qT "
6086 "because %qD has no linkage",
6087 expr, type, fn_no_ptr);
6088 else
6089 error ("%qE is not a valid template argument for type %qT "
6090 "because %qD does not have external linkage",
6091 expr, type, fn_no_ptr);
6092 }
6093 return NULL_TREE;
6094 }
6095
6096 accept:
6097 if (TREE_CODE (type) == REFERENCE_TYPE)
6098 fn = build_address (fn);
6099 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6100 fn = build_nop (type, fn);
6101
6102 return fn;
6103 }
6104
6105 /* Subroutine of convert_nontype_argument.
6106 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6107 Emit an error otherwise. */
6108
6109 static bool
6110 check_valid_ptrmem_cst_expr (tree type, tree expr,
6111 tsubst_flags_t complain)
6112 {
6113 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6114 tree orig_expr = expr;
6115 STRIP_NOPS (expr);
6116 if (null_ptr_cst_p (expr))
6117 return true;
6118 if (TREE_CODE (expr) == PTRMEM_CST
6119 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6120 PTRMEM_CST_CLASS (expr)))
6121 return true;
6122 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6123 return true;
6124 if (processing_template_decl
6125 && TREE_CODE (expr) == ADDR_EXPR
6126 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6127 return true;
6128 if (complain & tf_error)
6129 {
6130 error_at (loc, "%qE is not a valid template argument for type %qT",
6131 orig_expr, type);
6132 if (TREE_CODE (expr) != PTRMEM_CST)
6133 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6134 else
6135 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6136 }
6137 return false;
6138 }
6139
6140 /* Returns TRUE iff the address of OP is value-dependent.
6141
6142 14.6.2.4 [temp.dep.temp]:
6143 A non-integral non-type template-argument is dependent if its type is
6144 dependent or it has either of the following forms
6145 qualified-id
6146 & qualified-id
6147 and contains a nested-name-specifier which specifies a class-name that
6148 names a dependent type.
6149
6150 We generalize this to just say that the address of a member of a
6151 dependent class is value-dependent; the above doesn't cover the
6152 address of a static data member named with an unqualified-id. */
6153
6154 static bool
6155 has_value_dependent_address (tree op)
6156 {
6157 /* We could use get_inner_reference here, but there's no need;
6158 this is only relevant for template non-type arguments, which
6159 can only be expressed as &id-expression. */
6160 if (DECL_P (op))
6161 {
6162 tree ctx = CP_DECL_CONTEXT (op);
6163 if (TYPE_P (ctx) && dependent_type_p (ctx))
6164 return true;
6165 }
6166
6167 return false;
6168 }
6169
6170 /* The next set of functions are used for providing helpful explanatory
6171 diagnostics for failed overload resolution. Their messages should be
6172 indented by two spaces for consistency with the messages in
6173 call.c */
6174
6175 static int
6176 unify_success (bool /*explain_p*/)
6177 {
6178 return 0;
6179 }
6180
6181 /* Other failure functions should call this one, to provide a single function
6182 for setting a breakpoint on. */
6183
6184 static int
6185 unify_invalid (bool /*explain_p*/)
6186 {
6187 return 1;
6188 }
6189
6190 static int
6191 unify_parameter_deduction_failure (bool explain_p, tree parm)
6192 {
6193 if (explain_p)
6194 inform (input_location,
6195 " couldn't deduce template parameter %qD", parm);
6196 return unify_invalid (explain_p);
6197 }
6198
6199 static int
6200 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6201 {
6202 if (explain_p)
6203 inform (input_location,
6204 " types %qT and %qT have incompatible cv-qualifiers",
6205 parm, arg);
6206 return unify_invalid (explain_p);
6207 }
6208
6209 static int
6210 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6211 {
6212 if (explain_p)
6213 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6214 return unify_invalid (explain_p);
6215 }
6216
6217 static int
6218 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6219 {
6220 if (explain_p)
6221 inform (input_location,
6222 " template parameter %qD is not a parameter pack, but "
6223 "argument %qD is",
6224 parm, arg);
6225 return unify_invalid (explain_p);
6226 }
6227
6228 static int
6229 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6230 {
6231 if (explain_p)
6232 inform (input_location,
6233 " template argument %qE does not match "
6234 "pointer-to-member constant %qE",
6235 arg, parm);
6236 return unify_invalid (explain_p);
6237 }
6238
6239 static int
6240 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6241 {
6242 if (explain_p)
6243 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6244 return unify_invalid (explain_p);
6245 }
6246
6247 static int
6248 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6249 {
6250 if (explain_p)
6251 inform (input_location,
6252 " inconsistent parameter pack deduction with %qT and %qT",
6253 old_arg, new_arg);
6254 return unify_invalid (explain_p);
6255 }
6256
6257 static int
6258 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6259 {
6260 if (explain_p)
6261 {
6262 if (TYPE_P (parm))
6263 inform (input_location,
6264 " deduced conflicting types for parameter %qT (%qT and %qT)",
6265 parm, first, second);
6266 else
6267 inform (input_location,
6268 " deduced conflicting values for non-type parameter "
6269 "%qE (%qE and %qE)", parm, first, second);
6270 }
6271 return unify_invalid (explain_p);
6272 }
6273
6274 static int
6275 unify_vla_arg (bool explain_p, tree arg)
6276 {
6277 if (explain_p)
6278 inform (input_location,
6279 " variable-sized array type %qT is not "
6280 "a valid template argument",
6281 arg);
6282 return unify_invalid (explain_p);
6283 }
6284
6285 static int
6286 unify_method_type_error (bool explain_p, tree arg)
6287 {
6288 if (explain_p)
6289 inform (input_location,
6290 " member function type %qT is not a valid template argument",
6291 arg);
6292 return unify_invalid (explain_p);
6293 }
6294
6295 static int
6296 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6297 {
6298 if (explain_p)
6299 {
6300 if (least_p)
6301 inform_n (input_location, wanted,
6302 " candidate expects at least %d argument, %d provided",
6303 " candidate expects at least %d arguments, %d provided",
6304 wanted, have);
6305 else
6306 inform_n (input_location, wanted,
6307 " candidate expects %d argument, %d provided",
6308 " candidate expects %d arguments, %d provided",
6309 wanted, have);
6310 }
6311 return unify_invalid (explain_p);
6312 }
6313
6314 static int
6315 unify_too_many_arguments (bool explain_p, int have, int wanted)
6316 {
6317 return unify_arity (explain_p, have, wanted);
6318 }
6319
6320 static int
6321 unify_too_few_arguments (bool explain_p, int have, int wanted,
6322 bool least_p = false)
6323 {
6324 return unify_arity (explain_p, have, wanted, least_p);
6325 }
6326
6327 static int
6328 unify_arg_conversion (bool explain_p, tree to_type,
6329 tree from_type, tree arg)
6330 {
6331 if (explain_p)
6332 inform (EXPR_LOC_OR_LOC (arg, input_location),
6333 " cannot convert %qE (type %qT) to type %qT",
6334 arg, from_type, to_type);
6335 return unify_invalid (explain_p);
6336 }
6337
6338 static int
6339 unify_no_common_base (bool explain_p, enum template_base_result r,
6340 tree parm, tree arg)
6341 {
6342 if (explain_p)
6343 switch (r)
6344 {
6345 case tbr_ambiguous_baseclass:
6346 inform (input_location, " %qT is an ambiguous base class of %qT",
6347 parm, arg);
6348 break;
6349 default:
6350 inform (input_location, " %qT is not derived from %qT", arg, parm);
6351 break;
6352 }
6353 return unify_invalid (explain_p);
6354 }
6355
6356 static int
6357 unify_inconsistent_template_template_parameters (bool explain_p)
6358 {
6359 if (explain_p)
6360 inform (input_location,
6361 " template parameters of a template template argument are "
6362 "inconsistent with other deduced template arguments");
6363 return unify_invalid (explain_p);
6364 }
6365
6366 static int
6367 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6368 {
6369 if (explain_p)
6370 inform (input_location,
6371 " can't deduce a template for %qT from non-template type %qT",
6372 parm, arg);
6373 return unify_invalid (explain_p);
6374 }
6375
6376 static int
6377 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6378 {
6379 if (explain_p)
6380 inform (input_location,
6381 " template argument %qE does not match %qE", arg, parm);
6382 return unify_invalid (explain_p);
6383 }
6384
6385 /* Attempt to convert the non-type template parameter EXPR to the
6386 indicated TYPE. If the conversion is successful, return the
6387 converted value. If the conversion is unsuccessful, return
6388 NULL_TREE if we issued an error message, or error_mark_node if we
6389 did not. We issue error messages for out-and-out bad template
6390 parameters, but not simply because the conversion failed, since we
6391 might be just trying to do argument deduction. Both TYPE and EXPR
6392 must be non-dependent.
6393
6394 The conversion follows the special rules described in
6395 [temp.arg.nontype], and it is much more strict than an implicit
6396 conversion.
6397
6398 This function is called twice for each template argument (see
6399 lookup_template_class for a more accurate description of this
6400 problem). This means that we need to handle expressions which
6401 are not valid in a C++ source, but can be created from the
6402 first call (for instance, casts to perform conversions). These
6403 hacks can go away after we fix the double coercion problem. */
6404
6405 static tree
6406 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6407 {
6408 tree expr_type;
6409 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6410 tree orig_expr = expr;
6411
6412 /* Detect immediately string literals as invalid non-type argument.
6413 This special-case is not needed for correctness (we would easily
6414 catch this later), but only to provide better diagnostic for this
6415 common user mistake. As suggested by DR 100, we do not mention
6416 linkage issues in the diagnostic as this is not the point. */
6417 /* FIXME we're making this OK. */
6418 if (TREE_CODE (expr) == STRING_CST)
6419 {
6420 if (complain & tf_error)
6421 error ("%qE is not a valid template argument for type %qT "
6422 "because string literals can never be used in this context",
6423 expr, type);
6424 return NULL_TREE;
6425 }
6426
6427 /* Add the ADDR_EXPR now for the benefit of
6428 value_dependent_expression_p. */
6429 if (TYPE_PTROBV_P (type)
6430 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6431 {
6432 expr = decay_conversion (expr, complain);
6433 if (expr == error_mark_node)
6434 return error_mark_node;
6435 }
6436
6437 /* If we are in a template, EXPR may be non-dependent, but still
6438 have a syntactic, rather than semantic, form. For example, EXPR
6439 might be a SCOPE_REF, rather than the VAR_DECL to which the
6440 SCOPE_REF refers. Preserving the qualifying scope is necessary
6441 so that access checking can be performed when the template is
6442 instantiated -- but here we need the resolved form so that we can
6443 convert the argument. */
6444 bool non_dep = false;
6445 if (TYPE_REF_OBJ_P (type)
6446 && has_value_dependent_address (expr))
6447 /* If we want the address and it's value-dependent, don't fold. */;
6448 else if (processing_template_decl
6449 && is_nondependent_constant_expression (expr))
6450 non_dep = true;
6451 if (error_operand_p (expr))
6452 return error_mark_node;
6453 expr_type = TREE_TYPE (expr);
6454
6455 /* If the argument is non-dependent, perform any conversions in
6456 non-dependent context as well. */
6457 processing_template_decl_sentinel s (non_dep);
6458 if (non_dep)
6459 expr = instantiate_non_dependent_expr_internal (expr, complain);
6460
6461 if (value_dependent_expression_p (expr))
6462 expr = canonicalize_expr_argument (expr, complain);
6463
6464 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6465 to a non-type argument of "nullptr". */
6466 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6467 expr = fold_simple (convert (type, expr));
6468
6469 /* In C++11, integral or enumeration non-type template arguments can be
6470 arbitrary constant expressions. Pointer and pointer to
6471 member arguments can be general constant expressions that evaluate
6472 to a null value, but otherwise still need to be of a specific form. */
6473 if (cxx_dialect >= cxx11)
6474 {
6475 if (TREE_CODE (expr) == PTRMEM_CST)
6476 /* A PTRMEM_CST is already constant, and a valid template
6477 argument for a parameter of pointer to member type, we just want
6478 to leave it in that form rather than lower it to a
6479 CONSTRUCTOR. */;
6480 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6481 || cxx_dialect >= cxx17)
6482 {
6483 /* C++17: A template-argument for a non-type template-parameter shall
6484 be a converted constant expression (8.20) of the type of the
6485 template-parameter. */
6486 expr = build_converted_constant_expr (type, expr, complain);
6487 if (expr == error_mark_node)
6488 return error_mark_node;
6489 expr = maybe_constant_value (expr);
6490 expr = convert_from_reference (expr);
6491 }
6492 else if (TYPE_PTR_OR_PTRMEM_P (type))
6493 {
6494 tree folded = maybe_constant_value (expr);
6495 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6496 : null_member_pointer_value_p (folded))
6497 expr = folded;
6498 }
6499 }
6500
6501 if (TREE_CODE (type) == REFERENCE_TYPE)
6502 expr = mark_lvalue_use (expr);
6503 else
6504 expr = mark_rvalue_use (expr);
6505
6506 /* HACK: Due to double coercion, we can get a
6507 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6508 which is the tree that we built on the first call (see
6509 below when coercing to reference to object or to reference to
6510 function). We just strip everything and get to the arg.
6511 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6512 for examples. */
6513 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6514 {
6515 tree probe_type, probe = expr;
6516 if (REFERENCE_REF_P (probe))
6517 probe = TREE_OPERAND (probe, 0);
6518 probe_type = TREE_TYPE (probe);
6519 if (TREE_CODE (probe) == NOP_EXPR)
6520 {
6521 /* ??? Maybe we could use convert_from_reference here, but we
6522 would need to relax its constraints because the NOP_EXPR
6523 could actually change the type to something more cv-qualified,
6524 and this is not folded by convert_from_reference. */
6525 tree addr = TREE_OPERAND (probe, 0);
6526 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6527 && TREE_CODE (addr) == ADDR_EXPR
6528 && TYPE_PTR_P (TREE_TYPE (addr))
6529 && (same_type_ignoring_top_level_qualifiers_p
6530 (TREE_TYPE (probe_type),
6531 TREE_TYPE (TREE_TYPE (addr)))))
6532 {
6533 expr = TREE_OPERAND (addr, 0);
6534 expr_type = TREE_TYPE (probe_type);
6535 }
6536 }
6537 }
6538
6539 /* [temp.arg.nontype]/5, bullet 1
6540
6541 For a non-type template-parameter of integral or enumeration type,
6542 integral promotions (_conv.prom_) and integral conversions
6543 (_conv.integral_) are applied. */
6544 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6545 {
6546 if (cxx_dialect < cxx11)
6547 {
6548 tree t = build_converted_constant_expr (type, expr, complain);
6549 t = maybe_constant_value (t);
6550 if (t != error_mark_node)
6551 expr = t;
6552 }
6553
6554 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6555 return error_mark_node;
6556
6557 /* Notice that there are constant expressions like '4 % 0' which
6558 do not fold into integer constants. */
6559 if (TREE_CODE (expr) != INTEGER_CST
6560 && !value_dependent_expression_p (expr))
6561 {
6562 if (complain & tf_error)
6563 {
6564 int errs = errorcount, warns = warningcount + werrorcount;
6565 if (!require_potential_constant_expression (expr))
6566 expr = error_mark_node;
6567 else
6568 expr = cxx_constant_value (expr);
6569 if (errorcount > errs || warningcount + werrorcount > warns)
6570 inform (loc, "in template argument for type %qT ", type);
6571 if (expr == error_mark_node)
6572 return NULL_TREE;
6573 /* else cxx_constant_value complained but gave us
6574 a real constant, so go ahead. */
6575 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6576 }
6577 else
6578 return NULL_TREE;
6579 }
6580
6581 /* Avoid typedef problems. */
6582 if (TREE_TYPE (expr) != type)
6583 expr = fold_convert (type, expr);
6584 }
6585 /* [temp.arg.nontype]/5, bullet 2
6586
6587 For a non-type template-parameter of type pointer to object,
6588 qualification conversions (_conv.qual_) and the array-to-pointer
6589 conversion (_conv.array_) are applied. */
6590 else if (TYPE_PTROBV_P (type))
6591 {
6592 tree decayed = expr;
6593
6594 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6595 decay_conversion or an explicit cast. If it's a problematic cast,
6596 we'll complain about it below. */
6597 if (TREE_CODE (expr) == NOP_EXPR)
6598 {
6599 tree probe = expr;
6600 STRIP_NOPS (probe);
6601 if (TREE_CODE (probe) == ADDR_EXPR
6602 && TYPE_PTR_P (TREE_TYPE (probe)))
6603 {
6604 expr = probe;
6605 expr_type = TREE_TYPE (expr);
6606 }
6607 }
6608
6609 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6610
6611 A template-argument for a non-type, non-template template-parameter
6612 shall be one of: [...]
6613
6614 -- the name of a non-type template-parameter;
6615 -- the address of an object or function with external linkage, [...]
6616 expressed as "& id-expression" where the & is optional if the name
6617 refers to a function or array, or if the corresponding
6618 template-parameter is a reference.
6619
6620 Here, we do not care about functions, as they are invalid anyway
6621 for a parameter of type pointer-to-object. */
6622
6623 if (value_dependent_expression_p (expr))
6624 /* Non-type template parameters are OK. */
6625 ;
6626 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6627 /* Null pointer values are OK in C++11. */;
6628 else if (TREE_CODE (expr) != ADDR_EXPR)
6629 {
6630 if (VAR_P (expr))
6631 {
6632 if (complain & tf_error)
6633 error ("%qD is not a valid template argument "
6634 "because %qD is a variable, not the address of "
6635 "a variable", orig_expr, expr);
6636 return NULL_TREE;
6637 }
6638 if (POINTER_TYPE_P (expr_type))
6639 {
6640 if (complain & tf_error)
6641 error ("%qE is not a valid template argument for %qT "
6642 "because it is not the address of a variable",
6643 orig_expr, type);
6644 return NULL_TREE;
6645 }
6646 /* Other values, like integer constants, might be valid
6647 non-type arguments of some other type. */
6648 return error_mark_node;
6649 }
6650 else
6651 {
6652 tree decl = TREE_OPERAND (expr, 0);
6653
6654 if (!VAR_P (decl))
6655 {
6656 if (complain & tf_error)
6657 error ("%qE is not a valid template argument of type %qT "
6658 "because %qE is not a variable", orig_expr, type, decl);
6659 return NULL_TREE;
6660 }
6661 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6662 {
6663 if (complain & tf_error)
6664 error ("%qE is not a valid template argument of type %qT "
6665 "because %qD does not have external linkage",
6666 orig_expr, type, decl);
6667 return NULL_TREE;
6668 }
6669 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6670 && decl_linkage (decl) == lk_none)
6671 {
6672 if (complain & tf_error)
6673 error ("%qE is not a valid template argument of type %qT "
6674 "because %qD has no linkage", orig_expr, type, decl);
6675 return NULL_TREE;
6676 }
6677 /* C++17: For a non-type template-parameter of reference or pointer
6678 type, the value of the constant expression shall not refer to (or
6679 for a pointer type, shall not be the address of):
6680 * a subobject (4.5),
6681 * a temporary object (15.2),
6682 * a string literal (5.13.5),
6683 * the result of a typeid expression (8.2.8), or
6684 * a predefined __func__ variable (11.4.1). */
6685 else if (DECL_ARTIFICIAL (decl))
6686 {
6687 if (complain & tf_error)
6688 error ("the address of %qD is not a valid template argument",
6689 decl);
6690 return NULL_TREE;
6691 }
6692 else if (!same_type_ignoring_top_level_qualifiers_p
6693 (strip_array_types (TREE_TYPE (type)),
6694 strip_array_types (TREE_TYPE (decl))))
6695 {
6696 if (complain & tf_error)
6697 error ("the address of the %qT subobject of %qD is not a "
6698 "valid template argument", TREE_TYPE (type), decl);
6699 return NULL_TREE;
6700 }
6701 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6702 {
6703 if (complain & tf_error)
6704 error ("the address of %qD is not a valid template argument "
6705 "because it does not have static storage duration",
6706 decl);
6707 return NULL_TREE;
6708 }
6709 }
6710
6711 expr = decayed;
6712
6713 expr = perform_qualification_conversions (type, expr);
6714 if (expr == error_mark_node)
6715 return error_mark_node;
6716 }
6717 /* [temp.arg.nontype]/5, bullet 3
6718
6719 For a non-type template-parameter of type reference to object, no
6720 conversions apply. The type referred to by the reference may be more
6721 cv-qualified than the (otherwise identical) type of the
6722 template-argument. The template-parameter is bound directly to the
6723 template-argument, which must be an lvalue. */
6724 else if (TYPE_REF_OBJ_P (type))
6725 {
6726 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6727 expr_type))
6728 return error_mark_node;
6729
6730 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6731 {
6732 if (complain & tf_error)
6733 error ("%qE is not a valid template argument for type %qT "
6734 "because of conflicts in cv-qualification", expr, type);
6735 return NULL_TREE;
6736 }
6737
6738 if (!lvalue_p (expr))
6739 {
6740 if (complain & tf_error)
6741 error ("%qE is not a valid template argument for type %qT "
6742 "because it is not an lvalue", expr, type);
6743 return NULL_TREE;
6744 }
6745
6746 /* [temp.arg.nontype]/1
6747
6748 A template-argument for a non-type, non-template template-parameter
6749 shall be one of: [...]
6750
6751 -- the address of an object or function with external linkage. */
6752 if (INDIRECT_REF_P (expr)
6753 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6754 {
6755 expr = TREE_OPERAND (expr, 0);
6756 if (DECL_P (expr))
6757 {
6758 if (complain & tf_error)
6759 error ("%q#D is not a valid template argument for type %qT "
6760 "because a reference variable does not have a constant "
6761 "address", expr, type);
6762 return NULL_TREE;
6763 }
6764 }
6765
6766 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6767 && value_dependent_expression_p (expr))
6768 /* OK, dependent reference. We don't want to ask whether a DECL is
6769 itself value-dependent, since what we want here is its address. */;
6770 else
6771 {
6772 if (!DECL_P (expr))
6773 {
6774 if (complain & tf_error)
6775 error ("%qE is not a valid template argument for type %qT "
6776 "because it is not an object with linkage",
6777 expr, type);
6778 return NULL_TREE;
6779 }
6780
6781 /* DR 1155 allows internal linkage in C++11 and up. */
6782 linkage_kind linkage = decl_linkage (expr);
6783 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6784 {
6785 if (complain & tf_error)
6786 error ("%qE is not a valid template argument for type %qT "
6787 "because object %qD does not have linkage",
6788 expr, type, expr);
6789 return NULL_TREE;
6790 }
6791
6792 expr = build_address (expr);
6793 }
6794
6795 if (!same_type_p (type, TREE_TYPE (expr)))
6796 expr = build_nop (type, expr);
6797 }
6798 /* [temp.arg.nontype]/5, bullet 4
6799
6800 For a non-type template-parameter of type pointer to function, only
6801 the function-to-pointer conversion (_conv.func_) is applied. If the
6802 template-argument represents a set of overloaded functions (or a
6803 pointer to such), the matching function is selected from the set
6804 (_over.over_). */
6805 else if (TYPE_PTRFN_P (type))
6806 {
6807 /* If the argument is a template-id, we might not have enough
6808 context information to decay the pointer. */
6809 if (!type_unknown_p (expr_type))
6810 {
6811 expr = decay_conversion (expr, complain);
6812 if (expr == error_mark_node)
6813 return error_mark_node;
6814 }
6815
6816 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6817 /* Null pointer values are OK in C++11. */
6818 return perform_qualification_conversions (type, expr);
6819
6820 expr = convert_nontype_argument_function (type, expr, complain);
6821 if (!expr || expr == error_mark_node)
6822 return expr;
6823 }
6824 /* [temp.arg.nontype]/5, bullet 5
6825
6826 For a non-type template-parameter of type reference to function, no
6827 conversions apply. If the template-argument represents a set of
6828 overloaded functions, the matching function is selected from the set
6829 (_over.over_). */
6830 else if (TYPE_REFFN_P (type))
6831 {
6832 if (TREE_CODE (expr) == ADDR_EXPR)
6833 {
6834 if (complain & tf_error)
6835 {
6836 error ("%qE is not a valid template argument for type %qT "
6837 "because it is a pointer", expr, type);
6838 inform (input_location, "try using %qE instead",
6839 TREE_OPERAND (expr, 0));
6840 }
6841 return NULL_TREE;
6842 }
6843
6844 expr = convert_nontype_argument_function (type, expr, complain);
6845 if (!expr || expr == error_mark_node)
6846 return expr;
6847 }
6848 /* [temp.arg.nontype]/5, bullet 6
6849
6850 For a non-type template-parameter of type pointer to member function,
6851 no conversions apply. If the template-argument represents a set of
6852 overloaded member functions, the matching member function is selected
6853 from the set (_over.over_). */
6854 else if (TYPE_PTRMEMFUNC_P (type))
6855 {
6856 expr = instantiate_type (type, expr, tf_none);
6857 if (expr == error_mark_node)
6858 return error_mark_node;
6859
6860 /* [temp.arg.nontype] bullet 1 says the pointer to member
6861 expression must be a pointer-to-member constant. */
6862 if (!value_dependent_expression_p (expr)
6863 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6864 return NULL_TREE;
6865
6866 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6867 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6868 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6869 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6870 }
6871 /* [temp.arg.nontype]/5, bullet 7
6872
6873 For a non-type template-parameter of type pointer to data member,
6874 qualification conversions (_conv.qual_) are applied. */
6875 else if (TYPE_PTRDATAMEM_P (type))
6876 {
6877 /* [temp.arg.nontype] bullet 1 says the pointer to member
6878 expression must be a pointer-to-member constant. */
6879 if (!value_dependent_expression_p (expr)
6880 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6881 return NULL_TREE;
6882
6883 expr = perform_qualification_conversions (type, expr);
6884 if (expr == error_mark_node)
6885 return expr;
6886 }
6887 else if (NULLPTR_TYPE_P (type))
6888 {
6889 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6890 {
6891 if (complain & tf_error)
6892 error ("%qE is not a valid template argument for type %qT "
6893 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6894 return NULL_TREE;
6895 }
6896 return expr;
6897 }
6898 /* A template non-type parameter must be one of the above. */
6899 else
6900 gcc_unreachable ();
6901
6902 /* Sanity check: did we actually convert the argument to the
6903 right type? */
6904 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6905 (type, TREE_TYPE (expr)));
6906 return convert_from_reference (expr);
6907 }
6908
6909 /* Subroutine of coerce_template_template_parms, which returns 1 if
6910 PARM_PARM and ARG_PARM match using the rule for the template
6911 parameters of template template parameters. Both PARM and ARG are
6912 template parameters; the rest of the arguments are the same as for
6913 coerce_template_template_parms.
6914 */
6915 static int
6916 coerce_template_template_parm (tree parm,
6917 tree arg,
6918 tsubst_flags_t complain,
6919 tree in_decl,
6920 tree outer_args)
6921 {
6922 if (arg == NULL_TREE || error_operand_p (arg)
6923 || parm == NULL_TREE || error_operand_p (parm))
6924 return 0;
6925
6926 if (TREE_CODE (arg) != TREE_CODE (parm))
6927 return 0;
6928
6929 switch (TREE_CODE (parm))
6930 {
6931 case TEMPLATE_DECL:
6932 /* We encounter instantiations of templates like
6933 template <template <template <class> class> class TT>
6934 class C; */
6935 {
6936 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6937 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6938
6939 if (!coerce_template_template_parms
6940 (parmparm, argparm, complain, in_decl, outer_args))
6941 return 0;
6942 }
6943 /* Fall through. */
6944
6945 case TYPE_DECL:
6946 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6947 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6948 /* Argument is a parameter pack but parameter is not. */
6949 return 0;
6950 break;
6951
6952 case PARM_DECL:
6953 /* The tsubst call is used to handle cases such as
6954
6955 template <int> class C {};
6956 template <class T, template <T> class TT> class D {};
6957 D<int, C> d;
6958
6959 i.e. the parameter list of TT depends on earlier parameters. */
6960 if (!uses_template_parms (TREE_TYPE (arg)))
6961 {
6962 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6963 if (!uses_template_parms (t)
6964 && !same_type_p (t, TREE_TYPE (arg)))
6965 return 0;
6966 }
6967
6968 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6969 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6970 /* Argument is a parameter pack but parameter is not. */
6971 return 0;
6972
6973 break;
6974
6975 default:
6976 gcc_unreachable ();
6977 }
6978
6979 return 1;
6980 }
6981
6982 /* Coerce template argument list ARGLIST for use with template
6983 template-parameter TEMPL. */
6984
6985 static tree
6986 coerce_template_args_for_ttp (tree templ, tree arglist,
6987 tsubst_flags_t complain)
6988 {
6989 /* Consider an example where a template template parameter declared as
6990
6991 template <class T, class U = std::allocator<T> > class TT
6992
6993 The template parameter level of T and U are one level larger than
6994 of TT. To proper process the default argument of U, say when an
6995 instantiation `TT<int>' is seen, we need to build the full
6996 arguments containing {int} as the innermost level. Outer levels,
6997 available when not appearing as default template argument, can be
6998 obtained from the arguments of the enclosing template.
6999
7000 Suppose that TT is later substituted with std::vector. The above
7001 instantiation is `TT<int, std::allocator<T> >' with TT at
7002 level 1, and T at level 2, while the template arguments at level 1
7003 becomes {std::vector} and the inner level 2 is {int}. */
7004
7005 tree outer = DECL_CONTEXT (templ);
7006 if (outer)
7007 {
7008 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7009 /* We want arguments for the partial specialization, not arguments for
7010 the primary template. */
7011 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7012 else
7013 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7014 }
7015 else if (current_template_parms)
7016 {
7017 /* This is an argument of the current template, so we haven't set
7018 DECL_CONTEXT yet. */
7019 tree relevant_template_parms;
7020
7021 /* Parameter levels that are greater than the level of the given
7022 template template parm are irrelevant. */
7023 relevant_template_parms = current_template_parms;
7024 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7025 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7026 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7027
7028 outer = template_parms_to_args (relevant_template_parms);
7029 }
7030
7031 if (outer)
7032 arglist = add_to_template_args (outer, arglist);
7033
7034 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7035 return coerce_template_parms (parmlist, arglist, templ,
7036 complain,
7037 /*require_all_args=*/true,
7038 /*use_default_args=*/true);
7039 }
7040
7041 /* A cache of template template parameters with match-all default
7042 arguments. */
7043 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7044 static void
7045 store_defaulted_ttp (tree v, tree t)
7046 {
7047 if (!defaulted_ttp_cache)
7048 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7049 defaulted_ttp_cache->put (v, t);
7050 }
7051 static tree
7052 lookup_defaulted_ttp (tree v)
7053 {
7054 if (defaulted_ttp_cache)
7055 if (tree *p = defaulted_ttp_cache->get (v))
7056 return *p;
7057 return NULL_TREE;
7058 }
7059
7060 /* T is a bound template template-parameter. Copy its arguments into default
7061 arguments of the template template-parameter's template parameters. */
7062
7063 static tree
7064 add_defaults_to_ttp (tree otmpl)
7065 {
7066 if (tree c = lookup_defaulted_ttp (otmpl))
7067 return c;
7068
7069 tree ntmpl = copy_node (otmpl);
7070
7071 tree ntype = copy_node (TREE_TYPE (otmpl));
7072 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7073 TYPE_MAIN_VARIANT (ntype) = ntype;
7074 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7075 TYPE_NAME (ntype) = ntmpl;
7076 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7077
7078 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7079 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7080 TEMPLATE_PARM_DECL (idx) = ntmpl;
7081 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7082
7083 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7084 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7085 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7086 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7087 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7088 {
7089 tree o = TREE_VEC_ELT (vec, i);
7090 if (!template_parameter_pack_p (TREE_VALUE (o)))
7091 {
7092 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7093 TREE_PURPOSE (n) = any_targ_node;
7094 }
7095 }
7096
7097 store_defaulted_ttp (otmpl, ntmpl);
7098 return ntmpl;
7099 }
7100
7101 /* ARG is a bound potential template template-argument, and PARGS is a list
7102 of arguments for the corresponding template template-parameter. Adjust
7103 PARGS as appropriate for application to ARG's template, and if ARG is a
7104 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7105 arguments to the template template parameter. */
7106
7107 static tree
7108 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7109 {
7110 ++processing_template_decl;
7111 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7112 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7113 {
7114 /* When comparing two template template-parameters in partial ordering,
7115 rewrite the one currently being used as an argument to have default
7116 arguments for all parameters. */
7117 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7118 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7119 if (pargs != error_mark_node)
7120 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7121 TYPE_TI_ARGS (arg));
7122 }
7123 else
7124 {
7125 tree aparms
7126 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7127 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7128 /*require_all*/true,
7129 /*use_default*/true);
7130 }
7131 --processing_template_decl;
7132 return pargs;
7133 }
7134
7135 /* Subroutine of unify for the case when PARM is a
7136 BOUND_TEMPLATE_TEMPLATE_PARM. */
7137
7138 static int
7139 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7140 bool explain_p)
7141 {
7142 tree parmvec = TYPE_TI_ARGS (parm);
7143 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7144
7145 /* The template template parm might be variadic and the argument
7146 not, so flatten both argument lists. */
7147 parmvec = expand_template_argument_pack (parmvec);
7148 argvec = expand_template_argument_pack (argvec);
7149
7150 if (flag_new_ttp)
7151 {
7152 /* In keeping with P0522R0, adjust P's template arguments
7153 to apply to A's template; then flatten it again. */
7154 tree nparmvec = parmvec;
7155 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7156 nparmvec = expand_template_argument_pack (nparmvec);
7157
7158 if (unify (tparms, targs, nparmvec, argvec,
7159 UNIFY_ALLOW_NONE, explain_p))
7160 return 1;
7161
7162 /* If the P0522 adjustment eliminated a pack expansion, deduce
7163 empty packs. */
7164 if (flag_new_ttp
7165 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7166 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7167 DEDUCE_EXACT, /*sub*/true, explain_p))
7168 return 1;
7169 }
7170 else
7171 {
7172 /* Deduce arguments T, i from TT<T> or TT<i>.
7173 We check each element of PARMVEC and ARGVEC individually
7174 rather than the whole TREE_VEC since they can have
7175 different number of elements, which is allowed under N2555. */
7176
7177 int len = TREE_VEC_LENGTH (parmvec);
7178
7179 /* Check if the parameters end in a pack, making them
7180 variadic. */
7181 int parm_variadic_p = 0;
7182 if (len > 0
7183 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7184 parm_variadic_p = 1;
7185
7186 for (int i = 0; i < len - parm_variadic_p; ++i)
7187 /* If the template argument list of P contains a pack
7188 expansion that is not the last template argument, the
7189 entire template argument list is a non-deduced
7190 context. */
7191 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7192 return unify_success (explain_p);
7193
7194 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7195 return unify_too_few_arguments (explain_p,
7196 TREE_VEC_LENGTH (argvec), len);
7197
7198 for (int i = 0; i < len - parm_variadic_p; ++i)
7199 if (unify (tparms, targs,
7200 TREE_VEC_ELT (parmvec, i),
7201 TREE_VEC_ELT (argvec, i),
7202 UNIFY_ALLOW_NONE, explain_p))
7203 return 1;
7204
7205 if (parm_variadic_p
7206 && unify_pack_expansion (tparms, targs,
7207 parmvec, argvec,
7208 DEDUCE_EXACT,
7209 /*subr=*/true, explain_p))
7210 return 1;
7211 }
7212
7213 return 0;
7214 }
7215
7216 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7217 template template parameters. Both PARM_PARMS and ARG_PARMS are
7218 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7219 or PARM_DECL.
7220
7221 Consider the example:
7222 template <class T> class A;
7223 template<template <class U> class TT> class B;
7224
7225 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7226 the parameters to A, and OUTER_ARGS contains A. */
7227
7228 static int
7229 coerce_template_template_parms (tree parm_parms,
7230 tree arg_parms,
7231 tsubst_flags_t complain,
7232 tree in_decl,
7233 tree outer_args)
7234 {
7235 int nparms, nargs, i;
7236 tree parm, arg;
7237 int variadic_p = 0;
7238
7239 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7240 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7241
7242 nparms = TREE_VEC_LENGTH (parm_parms);
7243 nargs = TREE_VEC_LENGTH (arg_parms);
7244
7245 if (flag_new_ttp)
7246 {
7247 /* P0522R0: A template template-parameter P is at least as specialized as
7248 a template template-argument A if, given the following rewrite to two
7249 function templates, the function template corresponding to P is at
7250 least as specialized as the function template corresponding to A
7251 according to the partial ordering rules for function templates
7252 ([temp.func.order]). Given an invented class template X with the
7253 template parameter list of A (including default arguments):
7254
7255 * Each of the two function templates has the same template parameters,
7256 respectively, as P or A.
7257
7258 * Each function template has a single function parameter whose type is
7259 a specialization of X with template arguments corresponding to the
7260 template parameters from the respective function template where, for
7261 each template parameter PP in the template parameter list of the
7262 function template, a corresponding template argument AA is formed. If
7263 PP declares a parameter pack, then AA is the pack expansion
7264 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7265
7266 If the rewrite produces an invalid type, then P is not at least as
7267 specialized as A. */
7268
7269 /* So coerce P's args to apply to A's parms, and then deduce between A's
7270 args and the converted args. If that succeeds, A is at least as
7271 specialized as P, so they match.*/
7272 tree pargs = template_parms_level_to_args (parm_parms);
7273 ++processing_template_decl;
7274 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7275 /*require_all*/true, /*use_default*/true);
7276 --processing_template_decl;
7277 if (pargs != error_mark_node)
7278 {
7279 tree targs = make_tree_vec (nargs);
7280 tree aargs = template_parms_level_to_args (arg_parms);
7281 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7282 /*explain*/false))
7283 return 1;
7284 }
7285 }
7286
7287 /* Determine whether we have a parameter pack at the end of the
7288 template template parameter's template parameter list. */
7289 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7290 {
7291 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7292
7293 if (error_operand_p (parm))
7294 return 0;
7295
7296 switch (TREE_CODE (parm))
7297 {
7298 case TEMPLATE_DECL:
7299 case TYPE_DECL:
7300 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7301 variadic_p = 1;
7302 break;
7303
7304 case PARM_DECL:
7305 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7306 variadic_p = 1;
7307 break;
7308
7309 default:
7310 gcc_unreachable ();
7311 }
7312 }
7313
7314 if (nargs != nparms
7315 && !(variadic_p && nargs >= nparms - 1))
7316 return 0;
7317
7318 /* Check all of the template parameters except the parameter pack at
7319 the end (if any). */
7320 for (i = 0; i < nparms - variadic_p; ++i)
7321 {
7322 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7323 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7324 continue;
7325
7326 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7327 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7328
7329 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7330 outer_args))
7331 return 0;
7332
7333 }
7334
7335 if (variadic_p)
7336 {
7337 /* Check each of the template parameters in the template
7338 argument against the template parameter pack at the end of
7339 the template template parameter. */
7340 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7341 return 0;
7342
7343 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7344
7345 for (; i < nargs; ++i)
7346 {
7347 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7348 continue;
7349
7350 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7351
7352 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7353 outer_args))
7354 return 0;
7355 }
7356 }
7357
7358 return 1;
7359 }
7360
7361 /* Verifies that the deduced template arguments (in TARGS) for the
7362 template template parameters (in TPARMS) represent valid bindings,
7363 by comparing the template parameter list of each template argument
7364 to the template parameter list of its corresponding template
7365 template parameter, in accordance with DR150. This
7366 routine can only be called after all template arguments have been
7367 deduced. It will return TRUE if all of the template template
7368 parameter bindings are okay, FALSE otherwise. */
7369 bool
7370 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7371 {
7372 int i, ntparms = TREE_VEC_LENGTH (tparms);
7373 bool ret = true;
7374
7375 /* We're dealing with template parms in this process. */
7376 ++processing_template_decl;
7377
7378 targs = INNERMOST_TEMPLATE_ARGS (targs);
7379
7380 for (i = 0; i < ntparms; ++i)
7381 {
7382 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7383 tree targ = TREE_VEC_ELT (targs, i);
7384
7385 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7386 {
7387 tree packed_args = NULL_TREE;
7388 int idx, len = 1;
7389
7390 if (ARGUMENT_PACK_P (targ))
7391 {
7392 /* Look inside the argument pack. */
7393 packed_args = ARGUMENT_PACK_ARGS (targ);
7394 len = TREE_VEC_LENGTH (packed_args);
7395 }
7396
7397 for (idx = 0; idx < len; ++idx)
7398 {
7399 tree targ_parms = NULL_TREE;
7400
7401 if (packed_args)
7402 /* Extract the next argument from the argument
7403 pack. */
7404 targ = TREE_VEC_ELT (packed_args, idx);
7405
7406 if (PACK_EXPANSION_P (targ))
7407 /* Look at the pattern of the pack expansion. */
7408 targ = PACK_EXPANSION_PATTERN (targ);
7409
7410 /* Extract the template parameters from the template
7411 argument. */
7412 if (TREE_CODE (targ) == TEMPLATE_DECL)
7413 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7414 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7415 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7416
7417 /* Verify that we can coerce the template template
7418 parameters from the template argument to the template
7419 parameter. This requires an exact match. */
7420 if (targ_parms
7421 && !coerce_template_template_parms
7422 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7423 targ_parms,
7424 tf_none,
7425 tparm,
7426 targs))
7427 {
7428 ret = false;
7429 goto out;
7430 }
7431 }
7432 }
7433 }
7434
7435 out:
7436
7437 --processing_template_decl;
7438 return ret;
7439 }
7440
7441 /* Since type attributes aren't mangled, we need to strip them from
7442 template type arguments. */
7443
7444 static tree
7445 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7446 {
7447 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7448 return arg;
7449 bool removed_attributes = false;
7450 tree canon = strip_typedefs (arg, &removed_attributes);
7451 if (removed_attributes
7452 && (complain & tf_warning))
7453 warning (OPT_Wignored_attributes,
7454 "ignoring attributes on template argument %qT", arg);
7455 return canon;
7456 }
7457
7458 /* And from inside dependent non-type arguments like sizeof(Type). */
7459
7460 static tree
7461 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7462 {
7463 if (!arg || arg == error_mark_node)
7464 return arg;
7465 bool removed_attributes = false;
7466 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7467 if (removed_attributes
7468 && (complain & tf_warning))
7469 warning (OPT_Wignored_attributes,
7470 "ignoring attributes in template argument %qE", arg);
7471 return canon;
7472 }
7473
7474 // A template declaration can be substituted for a constrained
7475 // template template parameter only when the argument is more
7476 // constrained than the parameter.
7477 static bool
7478 is_compatible_template_arg (tree parm, tree arg)
7479 {
7480 tree parm_cons = get_constraints (parm);
7481
7482 /* For now, allow constrained template template arguments
7483 and unconstrained template template parameters. */
7484 if (parm_cons == NULL_TREE)
7485 return true;
7486
7487 tree arg_cons = get_constraints (arg);
7488
7489 // If the template parameter is constrained, we need to rewrite its
7490 // constraints in terms of the ARG's template parameters. This ensures
7491 // that all of the template parameter types will have the same depth.
7492 //
7493 // Note that this is only valid when coerce_template_template_parm is
7494 // true for the innermost template parameters of PARM and ARG. In other
7495 // words, because coercion is successful, this conversion will be valid.
7496 if (parm_cons)
7497 {
7498 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7499 parm_cons = tsubst_constraint_info (parm_cons,
7500 INNERMOST_TEMPLATE_ARGS (args),
7501 tf_none, NULL_TREE);
7502 if (parm_cons == error_mark_node)
7503 return false;
7504 }
7505
7506 return subsumes (parm_cons, arg_cons);
7507 }
7508
7509 // Convert a placeholder argument into a binding to the original
7510 // parameter. The original parameter is saved as the TREE_TYPE of
7511 // ARG.
7512 static inline tree
7513 convert_wildcard_argument (tree parm, tree arg)
7514 {
7515 TREE_TYPE (arg) = parm;
7516 return arg;
7517 }
7518
7519 /* Convert the indicated template ARG as necessary to match the
7520 indicated template PARM. Returns the converted ARG, or
7521 error_mark_node if the conversion was unsuccessful. Error and
7522 warning messages are issued under control of COMPLAIN. This
7523 conversion is for the Ith parameter in the parameter list. ARGS is
7524 the full set of template arguments deduced so far. */
7525
7526 static tree
7527 convert_template_argument (tree parm,
7528 tree arg,
7529 tree args,
7530 tsubst_flags_t complain,
7531 int i,
7532 tree in_decl)
7533 {
7534 tree orig_arg;
7535 tree val;
7536 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7537
7538 if (parm == error_mark_node)
7539 return error_mark_node;
7540
7541 /* Trivially convert placeholders. */
7542 if (TREE_CODE (arg) == WILDCARD_DECL)
7543 return convert_wildcard_argument (parm, arg);
7544
7545 if (arg == any_targ_node)
7546 return arg;
7547
7548 if (TREE_CODE (arg) == TREE_LIST
7549 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7550 {
7551 /* The template argument was the name of some
7552 member function. That's usually
7553 invalid, but static members are OK. In any
7554 case, grab the underlying fields/functions
7555 and issue an error later if required. */
7556 orig_arg = TREE_VALUE (arg);
7557 TREE_TYPE (arg) = unknown_type_node;
7558 }
7559
7560 orig_arg = arg;
7561
7562 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7563 requires_type = (TREE_CODE (parm) == TYPE_DECL
7564 || requires_tmpl_type);
7565
7566 /* When determining whether an argument pack expansion is a template,
7567 look at the pattern. */
7568 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7569 arg = PACK_EXPANSION_PATTERN (arg);
7570
7571 /* Deal with an injected-class-name used as a template template arg. */
7572 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7573 {
7574 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7575 if (TREE_CODE (t) == TEMPLATE_DECL)
7576 {
7577 if (cxx_dialect >= cxx11)
7578 /* OK under DR 1004. */;
7579 else if (complain & tf_warning_or_error)
7580 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7581 " used as template template argument", TYPE_NAME (arg));
7582 else if (flag_pedantic_errors)
7583 t = arg;
7584
7585 arg = t;
7586 }
7587 }
7588
7589 is_tmpl_type =
7590 ((TREE_CODE (arg) == TEMPLATE_DECL
7591 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7592 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7593 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7594 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7595
7596 if (is_tmpl_type
7597 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7598 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7599 arg = TYPE_STUB_DECL (arg);
7600
7601 is_type = TYPE_P (arg) || is_tmpl_type;
7602
7603 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7604 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7605 {
7606 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7607 {
7608 if (complain & tf_error)
7609 error ("invalid use of destructor %qE as a type", orig_arg);
7610 return error_mark_node;
7611 }
7612
7613 permerror (input_location,
7614 "to refer to a type member of a template parameter, "
7615 "use %<typename %E%>", orig_arg);
7616
7617 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7618 TREE_OPERAND (arg, 1),
7619 typename_type,
7620 complain);
7621 arg = orig_arg;
7622 is_type = 1;
7623 }
7624 if (is_type != requires_type)
7625 {
7626 if (in_decl)
7627 {
7628 if (complain & tf_error)
7629 {
7630 error ("type/value mismatch at argument %d in template "
7631 "parameter list for %qD",
7632 i + 1, in_decl);
7633 if (is_type)
7634 inform (input_location,
7635 " expected a constant of type %qT, got %qT",
7636 TREE_TYPE (parm),
7637 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7638 else if (requires_tmpl_type)
7639 inform (input_location,
7640 " expected a class template, got %qE", orig_arg);
7641 else
7642 inform (input_location,
7643 " expected a type, got %qE", orig_arg);
7644 }
7645 }
7646 return error_mark_node;
7647 }
7648 if (is_tmpl_type ^ requires_tmpl_type)
7649 {
7650 if (in_decl && (complain & tf_error))
7651 {
7652 error ("type/value mismatch at argument %d in template "
7653 "parameter list for %qD",
7654 i + 1, in_decl);
7655 if (is_tmpl_type)
7656 inform (input_location,
7657 " expected a type, got %qT", DECL_NAME (arg));
7658 else
7659 inform (input_location,
7660 " expected a class template, got %qT", orig_arg);
7661 }
7662 return error_mark_node;
7663 }
7664
7665 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7666 /* We already did the appropriate conversion when packing args. */
7667 val = orig_arg;
7668 else if (is_type)
7669 {
7670 if (requires_tmpl_type)
7671 {
7672 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7673 /* The number of argument required is not known yet.
7674 Just accept it for now. */
7675 val = orig_arg;
7676 else
7677 {
7678 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7679 tree argparm;
7680
7681 /* Strip alias templates that are equivalent to another
7682 template. */
7683 arg = get_underlying_template (arg);
7684 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7685
7686 if (coerce_template_template_parms (parmparm, argparm,
7687 complain, in_decl,
7688 args))
7689 {
7690 val = arg;
7691
7692 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7693 TEMPLATE_DECL. */
7694 if (val != error_mark_node)
7695 {
7696 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7697 val = TREE_TYPE (val);
7698 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7699 val = make_pack_expansion (val);
7700 }
7701 }
7702 else
7703 {
7704 if (in_decl && (complain & tf_error))
7705 {
7706 error ("type/value mismatch at argument %d in "
7707 "template parameter list for %qD",
7708 i + 1, in_decl);
7709 inform (input_location,
7710 " expected a template of type %qD, got %qT",
7711 parm, orig_arg);
7712 }
7713
7714 val = error_mark_node;
7715 }
7716
7717 // Check that the constraints are compatible before allowing the
7718 // substitution.
7719 if (val != error_mark_node)
7720 if (!is_compatible_template_arg (parm, arg))
7721 {
7722 if (in_decl && (complain & tf_error))
7723 {
7724 error ("constraint mismatch at argument %d in "
7725 "template parameter list for %qD",
7726 i + 1, in_decl);
7727 inform (input_location, " expected %qD but got %qD",
7728 parm, arg);
7729 }
7730 val = error_mark_node;
7731 }
7732 }
7733 }
7734 else
7735 val = orig_arg;
7736 /* We only form one instance of each template specialization.
7737 Therefore, if we use a non-canonical variant (i.e., a
7738 typedef), any future messages referring to the type will use
7739 the typedef, which is confusing if those future uses do not
7740 themselves also use the typedef. */
7741 if (TYPE_P (val))
7742 val = canonicalize_type_argument (val, complain);
7743 }
7744 else
7745 {
7746 tree t = TREE_TYPE (parm);
7747
7748 if (tree a = type_uses_auto (t))
7749 {
7750 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7751 if (t == error_mark_node)
7752 return error_mark_node;
7753 }
7754 else
7755 t = tsubst (t, args, complain, in_decl);
7756
7757 if (invalid_nontype_parm_type_p (t, complain))
7758 return error_mark_node;
7759
7760 if (!type_dependent_expression_p (orig_arg)
7761 && !uses_template_parms (t))
7762 /* We used to call digest_init here. However, digest_init
7763 will report errors, which we don't want when complain
7764 is zero. More importantly, digest_init will try too
7765 hard to convert things: for example, `0' should not be
7766 converted to pointer type at this point according to
7767 the standard. Accepting this is not merely an
7768 extension, since deciding whether or not these
7769 conversions can occur is part of determining which
7770 function template to call, or whether a given explicit
7771 argument specification is valid. */
7772 val = convert_nontype_argument (t, orig_arg, complain);
7773 else
7774 val = canonicalize_expr_argument (orig_arg, complain);
7775
7776 if (val == NULL_TREE)
7777 val = error_mark_node;
7778 else if (val == error_mark_node && (complain & tf_error))
7779 error ("could not convert template argument %qE from %qT to %qT",
7780 orig_arg, TREE_TYPE (orig_arg), t);
7781
7782 if (INDIRECT_REF_P (val))
7783 {
7784 /* Reject template arguments that are references to built-in
7785 functions with no library fallbacks. */
7786 const_tree inner = TREE_OPERAND (val, 0);
7787 const_tree innertype = TREE_TYPE (inner);
7788 if (innertype
7789 && TREE_CODE (innertype) == REFERENCE_TYPE
7790 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7791 && 0 < TREE_OPERAND_LENGTH (inner)
7792 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7793 return error_mark_node;
7794 }
7795
7796 if (TREE_CODE (val) == SCOPE_REF)
7797 {
7798 /* Strip typedefs from the SCOPE_REF. */
7799 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7800 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7801 complain);
7802 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7803 QUALIFIED_NAME_IS_TEMPLATE (val));
7804 }
7805 }
7806
7807 return val;
7808 }
7809
7810 /* Coerces the remaining template arguments in INNER_ARGS (from
7811 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7812 Returns the coerced argument pack. PARM_IDX is the position of this
7813 parameter in the template parameter list. ARGS is the original
7814 template argument list. */
7815 static tree
7816 coerce_template_parameter_pack (tree parms,
7817 int parm_idx,
7818 tree args,
7819 tree inner_args,
7820 int arg_idx,
7821 tree new_args,
7822 int* lost,
7823 tree in_decl,
7824 tsubst_flags_t complain)
7825 {
7826 tree parm = TREE_VEC_ELT (parms, parm_idx);
7827 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7828 tree packed_args;
7829 tree argument_pack;
7830 tree packed_parms = NULL_TREE;
7831
7832 if (arg_idx > nargs)
7833 arg_idx = nargs;
7834
7835 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7836 {
7837 /* When the template parameter is a non-type template parameter pack
7838 or template template parameter pack whose type or template
7839 parameters use parameter packs, we know exactly how many arguments
7840 we are looking for. Build a vector of the instantiated decls for
7841 these template parameters in PACKED_PARMS. */
7842 /* We can't use make_pack_expansion here because it would interpret a
7843 _DECL as a use rather than a declaration. */
7844 tree decl = TREE_VALUE (parm);
7845 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7846 SET_PACK_EXPANSION_PATTERN (exp, decl);
7847 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7848 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7849
7850 TREE_VEC_LENGTH (args)--;
7851 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7852 TREE_VEC_LENGTH (args)++;
7853
7854 if (packed_parms == error_mark_node)
7855 return error_mark_node;
7856
7857 /* If we're doing a partial instantiation of a member template,
7858 verify that all of the types used for the non-type
7859 template parameter pack are, in fact, valid for non-type
7860 template parameters. */
7861 if (arg_idx < nargs
7862 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7863 {
7864 int j, len = TREE_VEC_LENGTH (packed_parms);
7865 for (j = 0; j < len; ++j)
7866 {
7867 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7868 if (invalid_nontype_parm_type_p (t, complain))
7869 return error_mark_node;
7870 }
7871 /* We don't know how many args we have yet, just
7872 use the unconverted ones for now. */
7873 return NULL_TREE;
7874 }
7875
7876 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7877 }
7878 /* Check if we have a placeholder pack, which indicates we're
7879 in the context of a introduction list. In that case we want
7880 to match this pack to the single placeholder. */
7881 else if (arg_idx < nargs
7882 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7883 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7884 {
7885 nargs = arg_idx + 1;
7886 packed_args = make_tree_vec (1);
7887 }
7888 else
7889 packed_args = make_tree_vec (nargs - arg_idx);
7890
7891 /* Convert the remaining arguments, which will be a part of the
7892 parameter pack "parm". */
7893 int first_pack_arg = arg_idx;
7894 for (; arg_idx < nargs; ++arg_idx)
7895 {
7896 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7897 tree actual_parm = TREE_VALUE (parm);
7898 int pack_idx = arg_idx - first_pack_arg;
7899
7900 if (packed_parms)
7901 {
7902 /* Once we've packed as many args as we have types, stop. */
7903 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7904 break;
7905 else if (PACK_EXPANSION_P (arg))
7906 /* We don't know how many args we have yet, just
7907 use the unconverted ones for now. */
7908 return NULL_TREE;
7909 else
7910 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7911 }
7912
7913 if (arg == error_mark_node)
7914 {
7915 if (complain & tf_error)
7916 error ("template argument %d is invalid", arg_idx + 1);
7917 }
7918 else
7919 arg = convert_template_argument (actual_parm,
7920 arg, new_args, complain, parm_idx,
7921 in_decl);
7922 if (arg == error_mark_node)
7923 (*lost)++;
7924 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7925 }
7926
7927 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7928 && TREE_VEC_LENGTH (packed_args) > 0)
7929 {
7930 if (complain & tf_error)
7931 error ("wrong number of template arguments (%d, should be %d)",
7932 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7933 return error_mark_node;
7934 }
7935
7936 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7937 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7938 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7939 else
7940 {
7941 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7942 TREE_CONSTANT (argument_pack) = 1;
7943 }
7944
7945 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7946 if (CHECKING_P)
7947 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7948 TREE_VEC_LENGTH (packed_args));
7949 return argument_pack;
7950 }
7951
7952 /* Returns the number of pack expansions in the template argument vector
7953 ARGS. */
7954
7955 static int
7956 pack_expansion_args_count (tree args)
7957 {
7958 int i;
7959 int count = 0;
7960 if (args)
7961 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7962 {
7963 tree elt = TREE_VEC_ELT (args, i);
7964 if (elt && PACK_EXPANSION_P (elt))
7965 ++count;
7966 }
7967 return count;
7968 }
7969
7970 /* Convert all template arguments to their appropriate types, and
7971 return a vector containing the innermost resulting template
7972 arguments. If any error occurs, return error_mark_node. Error and
7973 warning messages are issued under control of COMPLAIN.
7974
7975 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7976 for arguments not specified in ARGS. Otherwise, if
7977 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7978 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7979 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7980 ARGS. */
7981
7982 static tree
7983 coerce_template_parms (tree parms,
7984 tree args,
7985 tree in_decl,
7986 tsubst_flags_t complain,
7987 bool require_all_args,
7988 bool use_default_args)
7989 {
7990 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7991 tree orig_inner_args;
7992 tree inner_args;
7993 tree new_args;
7994 tree new_inner_args;
7995 int saved_unevaluated_operand;
7996 int saved_inhibit_evaluation_warnings;
7997
7998 /* When used as a boolean value, indicates whether this is a
7999 variadic template parameter list. Since it's an int, we can also
8000 subtract it from nparms to get the number of non-variadic
8001 parameters. */
8002 int variadic_p = 0;
8003 int variadic_args_p = 0;
8004 int post_variadic_parms = 0;
8005
8006 /* Likewise for parameters with default arguments. */
8007 int default_p = 0;
8008
8009 if (args == error_mark_node)
8010 return error_mark_node;
8011
8012 nparms = TREE_VEC_LENGTH (parms);
8013
8014 /* Determine if there are any parameter packs or default arguments. */
8015 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8016 {
8017 tree parm = TREE_VEC_ELT (parms, parm_idx);
8018 if (variadic_p)
8019 ++post_variadic_parms;
8020 if (template_parameter_pack_p (TREE_VALUE (parm)))
8021 ++variadic_p;
8022 if (TREE_PURPOSE (parm))
8023 ++default_p;
8024 }
8025
8026 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8027 /* If there are no parameters that follow a parameter pack, we need to
8028 expand any argument packs so that we can deduce a parameter pack from
8029 some non-packed args followed by an argument pack, as in variadic85.C.
8030 If there are such parameters, we need to leave argument packs intact
8031 so the arguments are assigned properly. This can happen when dealing
8032 with a nested class inside a partial specialization of a class
8033 template, as in variadic92.C, or when deducing a template parameter pack
8034 from a sub-declarator, as in variadic114.C. */
8035 if (!post_variadic_parms)
8036 inner_args = expand_template_argument_pack (inner_args);
8037
8038 /* Count any pack expansion args. */
8039 variadic_args_p = pack_expansion_args_count (inner_args);
8040
8041 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8042 if ((nargs - variadic_args_p > nparms && !variadic_p)
8043 || (nargs < nparms - variadic_p
8044 && require_all_args
8045 && !variadic_args_p
8046 && (!use_default_args
8047 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8048 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8049 {
8050 if (complain & tf_error)
8051 {
8052 if (variadic_p || default_p)
8053 {
8054 nparms -= variadic_p + default_p;
8055 error ("wrong number of template arguments "
8056 "(%d, should be at least %d)", nargs, nparms);
8057 }
8058 else
8059 error ("wrong number of template arguments "
8060 "(%d, should be %d)", nargs, nparms);
8061
8062 if (in_decl)
8063 inform (DECL_SOURCE_LOCATION (in_decl),
8064 "provided for %qD", in_decl);
8065 }
8066
8067 return error_mark_node;
8068 }
8069 /* We can't pass a pack expansion to a non-pack parameter of an alias
8070 template (DR 1430). */
8071 else if (in_decl
8072 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8073 || concept_template_p (in_decl))
8074 && variadic_args_p
8075 && nargs - variadic_args_p < nparms - variadic_p)
8076 {
8077 if (complain & tf_error)
8078 {
8079 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8080 {
8081 tree arg = TREE_VEC_ELT (inner_args, i);
8082 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8083
8084 if (PACK_EXPANSION_P (arg)
8085 && !template_parameter_pack_p (parm))
8086 {
8087 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8088 error_at (location_of (arg),
8089 "pack expansion argument for non-pack parameter "
8090 "%qD of alias template %qD", parm, in_decl);
8091 else
8092 error_at (location_of (arg),
8093 "pack expansion argument for non-pack parameter "
8094 "%qD of concept %qD", parm, in_decl);
8095 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8096 goto found;
8097 }
8098 }
8099 gcc_unreachable ();
8100 found:;
8101 }
8102 return error_mark_node;
8103 }
8104
8105 /* We need to evaluate the template arguments, even though this
8106 template-id may be nested within a "sizeof". */
8107 saved_unevaluated_operand = cp_unevaluated_operand;
8108 cp_unevaluated_operand = 0;
8109 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8110 c_inhibit_evaluation_warnings = 0;
8111 new_inner_args = make_tree_vec (nparms);
8112 new_args = add_outermost_template_args (args, new_inner_args);
8113 int pack_adjust = 0;
8114 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8115 {
8116 tree arg;
8117 tree parm;
8118
8119 /* Get the Ith template parameter. */
8120 parm = TREE_VEC_ELT (parms, parm_idx);
8121
8122 if (parm == error_mark_node)
8123 {
8124 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8125 continue;
8126 }
8127
8128 /* Calculate the next argument. */
8129 if (arg_idx < nargs)
8130 arg = TREE_VEC_ELT (inner_args, arg_idx);
8131 else
8132 arg = NULL_TREE;
8133
8134 if (template_parameter_pack_p (TREE_VALUE (parm))
8135 && !(arg && ARGUMENT_PACK_P (arg)))
8136 {
8137 /* Some arguments will be placed in the
8138 template parameter pack PARM. */
8139 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8140 inner_args, arg_idx,
8141 new_args, &lost,
8142 in_decl, complain);
8143
8144 if (arg == NULL_TREE)
8145 {
8146 /* We don't know how many args we have yet, just use the
8147 unconverted (and still packed) ones for now. */
8148 new_inner_args = orig_inner_args;
8149 arg_idx = nargs;
8150 break;
8151 }
8152
8153 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8154
8155 /* Store this argument. */
8156 if (arg == error_mark_node)
8157 {
8158 lost++;
8159 /* We are done with all of the arguments. */
8160 arg_idx = nargs;
8161 }
8162 else
8163 {
8164 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8165 arg_idx += pack_adjust;
8166 }
8167
8168 continue;
8169 }
8170 else if (arg)
8171 {
8172 if (PACK_EXPANSION_P (arg))
8173 {
8174 /* "If every valid specialization of a variadic template
8175 requires an empty template parameter pack, the template is
8176 ill-formed, no diagnostic required." So check that the
8177 pattern works with this parameter. */
8178 tree pattern = PACK_EXPANSION_PATTERN (arg);
8179 tree conv = convert_template_argument (TREE_VALUE (parm),
8180 pattern, new_args,
8181 complain, parm_idx,
8182 in_decl);
8183 if (conv == error_mark_node)
8184 {
8185 if (complain & tf_error)
8186 inform (input_location, "so any instantiation with a "
8187 "non-empty parameter pack would be ill-formed");
8188 ++lost;
8189 }
8190 else if (TYPE_P (conv) && !TYPE_P (pattern))
8191 /* Recover from missing typename. */
8192 TREE_VEC_ELT (inner_args, arg_idx)
8193 = make_pack_expansion (conv);
8194
8195 /* We don't know how many args we have yet, just
8196 use the unconverted ones for now. */
8197 new_inner_args = inner_args;
8198 arg_idx = nargs;
8199 break;
8200 }
8201 }
8202 else if (require_all_args)
8203 {
8204 /* There must be a default arg in this case. */
8205 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8206 complain, in_decl);
8207 /* The position of the first default template argument,
8208 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8209 Record that. */
8210 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8211 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8212 arg_idx - pack_adjust);
8213 }
8214 else
8215 break;
8216
8217 if (arg == error_mark_node)
8218 {
8219 if (complain & tf_error)
8220 error ("template argument %d is invalid", arg_idx + 1);
8221 }
8222 else if (!arg)
8223 /* This only occurs if there was an error in the template
8224 parameter list itself (which we would already have
8225 reported) that we are trying to recover from, e.g., a class
8226 template with a parameter list such as
8227 template<typename..., typename>. */
8228 ++lost;
8229 else
8230 arg = convert_template_argument (TREE_VALUE (parm),
8231 arg, new_args, complain,
8232 parm_idx, in_decl);
8233
8234 if (arg == error_mark_node)
8235 lost++;
8236 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8237 }
8238 cp_unevaluated_operand = saved_unevaluated_operand;
8239 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8240
8241 if (variadic_p && arg_idx < nargs)
8242 {
8243 if (complain & tf_error)
8244 {
8245 error ("wrong number of template arguments "
8246 "(%d, should be %d)", nargs, arg_idx);
8247 if (in_decl)
8248 error ("provided for %q+D", in_decl);
8249 }
8250 return error_mark_node;
8251 }
8252
8253 if (lost)
8254 return error_mark_node;
8255
8256 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8257 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8258 TREE_VEC_LENGTH (new_inner_args));
8259
8260 return new_inner_args;
8261 }
8262
8263 /* Convert all template arguments to their appropriate types, and
8264 return a vector containing the innermost resulting template
8265 arguments. If any error occurs, return error_mark_node. Error and
8266 warning messages are not issued.
8267
8268 Note that no function argument deduction is performed, and default
8269 arguments are used to fill in unspecified arguments. */
8270 tree
8271 coerce_template_parms (tree parms, tree args, tree in_decl)
8272 {
8273 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8274 }
8275
8276 /* Convert all template arguments to their appropriate type, and
8277 instantiate default arguments as needed. This returns a vector
8278 containing the innermost resulting template arguments, or
8279 error_mark_node if unsuccessful. */
8280 tree
8281 coerce_template_parms (tree parms, tree args, tree in_decl,
8282 tsubst_flags_t complain)
8283 {
8284 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8285 }
8286
8287 /* Like coerce_template_parms. If PARMS represents all template
8288 parameters levels, this function returns a vector of vectors
8289 representing all the resulting argument levels. Note that in this
8290 case, only the innermost arguments are coerced because the
8291 outermost ones are supposed to have been coerced already.
8292
8293 Otherwise, if PARMS represents only (the innermost) vector of
8294 parameters, this function returns a vector containing just the
8295 innermost resulting arguments. */
8296
8297 static tree
8298 coerce_innermost_template_parms (tree parms,
8299 tree args,
8300 tree in_decl,
8301 tsubst_flags_t complain,
8302 bool require_all_args,
8303 bool use_default_args)
8304 {
8305 int parms_depth = TMPL_PARMS_DEPTH (parms);
8306 int args_depth = TMPL_ARGS_DEPTH (args);
8307 tree coerced_args;
8308
8309 if (parms_depth > 1)
8310 {
8311 coerced_args = make_tree_vec (parms_depth);
8312 tree level;
8313 int cur_depth;
8314
8315 for (level = parms, cur_depth = parms_depth;
8316 parms_depth > 0 && level != NULL_TREE;
8317 level = TREE_CHAIN (level), --cur_depth)
8318 {
8319 tree l;
8320 if (cur_depth == args_depth)
8321 l = coerce_template_parms (TREE_VALUE (level),
8322 args, in_decl, complain,
8323 require_all_args,
8324 use_default_args);
8325 else
8326 l = TMPL_ARGS_LEVEL (args, cur_depth);
8327
8328 if (l == error_mark_node)
8329 return error_mark_node;
8330
8331 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8332 }
8333 }
8334 else
8335 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8336 args, in_decl, complain,
8337 require_all_args,
8338 use_default_args);
8339 return coerced_args;
8340 }
8341
8342 /* Returns 1 if template args OT and NT are equivalent. */
8343
8344 int
8345 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8346 {
8347 if (nt == ot)
8348 return 1;
8349 if (nt == NULL_TREE || ot == NULL_TREE)
8350 return false;
8351 if (nt == any_targ_node || ot == any_targ_node)
8352 return true;
8353
8354 if (TREE_CODE (nt) == TREE_VEC)
8355 /* For member templates */
8356 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8357 else if (PACK_EXPANSION_P (ot))
8358 return (PACK_EXPANSION_P (nt)
8359 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8360 PACK_EXPANSION_PATTERN (nt))
8361 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8362 PACK_EXPANSION_EXTRA_ARGS (nt)));
8363 else if (ARGUMENT_PACK_P (ot))
8364 {
8365 int i, len;
8366 tree opack, npack;
8367
8368 if (!ARGUMENT_PACK_P (nt))
8369 return 0;
8370
8371 opack = ARGUMENT_PACK_ARGS (ot);
8372 npack = ARGUMENT_PACK_ARGS (nt);
8373 len = TREE_VEC_LENGTH (opack);
8374 if (TREE_VEC_LENGTH (npack) != len)
8375 return 0;
8376 for (i = 0; i < len; ++i)
8377 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8378 TREE_VEC_ELT (npack, i)))
8379 return 0;
8380 return 1;
8381 }
8382 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8383 gcc_unreachable ();
8384 else if (TYPE_P (nt))
8385 {
8386 if (!TYPE_P (ot))
8387 return false;
8388 /* Don't treat an alias template specialization with dependent
8389 arguments as equivalent to its underlying type when used as a
8390 template argument; we need them to be distinct so that we
8391 substitute into the specialization arguments at instantiation
8392 time. And aliases can't be equivalent without being ==, so
8393 we don't need to look any deeper.
8394
8395 During partial ordering, however, we need to treat them normally so
8396 that we can order uses of the same alias with different
8397 cv-qualification (79960). */
8398 if (!partial_order
8399 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8400 return false;
8401 else
8402 return same_type_p (ot, nt);
8403 }
8404 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8405 return 0;
8406 else
8407 {
8408 /* Try to treat a template non-type argument that has been converted
8409 to the parameter type as equivalent to one that hasn't yet. */
8410 for (enum tree_code code1 = TREE_CODE (ot);
8411 CONVERT_EXPR_CODE_P (code1)
8412 || code1 == NON_LVALUE_EXPR;
8413 code1 = TREE_CODE (ot))
8414 ot = TREE_OPERAND (ot, 0);
8415 for (enum tree_code code2 = TREE_CODE (nt);
8416 CONVERT_EXPR_CODE_P (code2)
8417 || code2 == NON_LVALUE_EXPR;
8418 code2 = TREE_CODE (nt))
8419 nt = TREE_OPERAND (nt, 0);
8420
8421 return cp_tree_equal (ot, nt);
8422 }
8423 }
8424
8425 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8426 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8427 NEWARG_PTR with the offending arguments if they are non-NULL. */
8428
8429 int
8430 comp_template_args (tree oldargs, tree newargs,
8431 tree *oldarg_ptr, tree *newarg_ptr,
8432 bool partial_order)
8433 {
8434 int i;
8435
8436 if (oldargs == newargs)
8437 return 1;
8438
8439 if (!oldargs || !newargs)
8440 return 0;
8441
8442 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8443 return 0;
8444
8445 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8446 {
8447 tree nt = TREE_VEC_ELT (newargs, i);
8448 tree ot = TREE_VEC_ELT (oldargs, i);
8449
8450 if (! template_args_equal (ot, nt, partial_order))
8451 {
8452 if (oldarg_ptr != NULL)
8453 *oldarg_ptr = ot;
8454 if (newarg_ptr != NULL)
8455 *newarg_ptr = nt;
8456 return 0;
8457 }
8458 }
8459 return 1;
8460 }
8461
8462 inline bool
8463 comp_template_args_porder (tree oargs, tree nargs)
8464 {
8465 return comp_template_args (oargs, nargs, NULL, NULL, true);
8466 }
8467
8468 static void
8469 add_pending_template (tree d)
8470 {
8471 tree ti = (TYPE_P (d)
8472 ? CLASSTYPE_TEMPLATE_INFO (d)
8473 : DECL_TEMPLATE_INFO (d));
8474 struct pending_template *pt;
8475 int level;
8476
8477 if (TI_PENDING_TEMPLATE_FLAG (ti))
8478 return;
8479
8480 /* We are called both from instantiate_decl, where we've already had a
8481 tinst_level pushed, and instantiate_template, where we haven't.
8482 Compensate. */
8483 level = !current_tinst_level || current_tinst_level->decl != d;
8484
8485 if (level)
8486 push_tinst_level (d);
8487
8488 pt = ggc_alloc<pending_template> ();
8489 pt->next = NULL;
8490 pt->tinst = current_tinst_level;
8491 if (last_pending_template)
8492 last_pending_template->next = pt;
8493 else
8494 pending_templates = pt;
8495
8496 last_pending_template = pt;
8497
8498 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8499
8500 if (level)
8501 pop_tinst_level ();
8502 }
8503
8504
8505 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8506 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8507 documentation for TEMPLATE_ID_EXPR. */
8508
8509 tree
8510 lookup_template_function (tree fns, tree arglist)
8511 {
8512 tree type;
8513
8514 if (fns == error_mark_node || arglist == error_mark_node)
8515 return error_mark_node;
8516
8517 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8518
8519 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8520 {
8521 error ("%q#D is not a function template", fns);
8522 return error_mark_node;
8523 }
8524
8525 if (BASELINK_P (fns))
8526 {
8527 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8528 unknown_type_node,
8529 BASELINK_FUNCTIONS (fns),
8530 arglist);
8531 return fns;
8532 }
8533
8534 type = TREE_TYPE (fns);
8535 if (TREE_CODE (fns) == OVERLOAD || !type)
8536 type = unknown_type_node;
8537
8538 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8539 }
8540
8541 /* Within the scope of a template class S<T>, the name S gets bound
8542 (in build_self_reference) to a TYPE_DECL for the class, not a
8543 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8544 or one of its enclosing classes, and that type is a template,
8545 return the associated TEMPLATE_DECL. Otherwise, the original
8546 DECL is returned.
8547
8548 Also handle the case when DECL is a TREE_LIST of ambiguous
8549 injected-class-names from different bases. */
8550
8551 tree
8552 maybe_get_template_decl_from_type_decl (tree decl)
8553 {
8554 if (decl == NULL_TREE)
8555 return decl;
8556
8557 /* DR 176: A lookup that finds an injected-class-name (10.2
8558 [class.member.lookup]) can result in an ambiguity in certain cases
8559 (for example, if it is found in more than one base class). If all of
8560 the injected-class-names that are found refer to specializations of
8561 the same class template, and if the name is followed by a
8562 template-argument-list, the reference refers to the class template
8563 itself and not a specialization thereof, and is not ambiguous. */
8564 if (TREE_CODE (decl) == TREE_LIST)
8565 {
8566 tree t, tmpl = NULL_TREE;
8567 for (t = decl; t; t = TREE_CHAIN (t))
8568 {
8569 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8570 if (!tmpl)
8571 tmpl = elt;
8572 else if (tmpl != elt)
8573 break;
8574 }
8575 if (tmpl && t == NULL_TREE)
8576 return tmpl;
8577 else
8578 return decl;
8579 }
8580
8581 return (decl != NULL_TREE
8582 && DECL_SELF_REFERENCE_P (decl)
8583 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8584 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8585 }
8586
8587 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8588 parameters, find the desired type.
8589
8590 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8591
8592 IN_DECL, if non-NULL, is the template declaration we are trying to
8593 instantiate.
8594
8595 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8596 the class we are looking up.
8597
8598 Issue error and warning messages under control of COMPLAIN.
8599
8600 If the template class is really a local class in a template
8601 function, then the FUNCTION_CONTEXT is the function in which it is
8602 being instantiated.
8603
8604 ??? Note that this function is currently called *twice* for each
8605 template-id: the first time from the parser, while creating the
8606 incomplete type (finish_template_type), and the second type during the
8607 real instantiation (instantiate_template_class). This is surely something
8608 that we want to avoid. It also causes some problems with argument
8609 coercion (see convert_nontype_argument for more information on this). */
8610
8611 static tree
8612 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8613 int entering_scope, tsubst_flags_t complain)
8614 {
8615 tree templ = NULL_TREE, parmlist;
8616 tree t;
8617 spec_entry **slot;
8618 spec_entry *entry;
8619 spec_entry elt;
8620 hashval_t hash;
8621
8622 if (identifier_p (d1))
8623 {
8624 tree value = innermost_non_namespace_value (d1);
8625 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8626 templ = value;
8627 else
8628 {
8629 if (context)
8630 push_decl_namespace (context);
8631 templ = lookup_name (d1);
8632 templ = maybe_get_template_decl_from_type_decl (templ);
8633 if (context)
8634 pop_decl_namespace ();
8635 }
8636 if (templ)
8637 context = DECL_CONTEXT (templ);
8638 }
8639 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8640 {
8641 tree type = TREE_TYPE (d1);
8642
8643 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8644 an implicit typename for the second A. Deal with it. */
8645 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8646 type = TREE_TYPE (type);
8647
8648 if (CLASSTYPE_TEMPLATE_INFO (type))
8649 {
8650 templ = CLASSTYPE_TI_TEMPLATE (type);
8651 d1 = DECL_NAME (templ);
8652 }
8653 }
8654 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8655 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8656 {
8657 templ = TYPE_TI_TEMPLATE (d1);
8658 d1 = DECL_NAME (templ);
8659 }
8660 else if (DECL_TYPE_TEMPLATE_P (d1))
8661 {
8662 templ = d1;
8663 d1 = DECL_NAME (templ);
8664 context = DECL_CONTEXT (templ);
8665 }
8666 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8667 {
8668 templ = d1;
8669 d1 = DECL_NAME (templ);
8670 }
8671
8672 /* Issue an error message if we didn't find a template. */
8673 if (! templ)
8674 {
8675 if (complain & tf_error)
8676 error ("%qT is not a template", d1);
8677 return error_mark_node;
8678 }
8679
8680 if (TREE_CODE (templ) != TEMPLATE_DECL
8681 /* Make sure it's a user visible template, if it was named by
8682 the user. */
8683 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8684 && !PRIMARY_TEMPLATE_P (templ)))
8685 {
8686 if (complain & tf_error)
8687 {
8688 error ("non-template type %qT used as a template", d1);
8689 if (in_decl)
8690 error ("for template declaration %q+D", in_decl);
8691 }
8692 return error_mark_node;
8693 }
8694
8695 complain &= ~tf_user;
8696
8697 /* An alias that just changes the name of a template is equivalent to the
8698 other template, so if any of the arguments are pack expansions, strip
8699 the alias to avoid problems with a pack expansion passed to a non-pack
8700 alias template parameter (DR 1430). */
8701 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8702 templ = get_underlying_template (templ);
8703
8704 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8705 {
8706 tree parm;
8707 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8708 if (arglist2 == error_mark_node
8709 || (!uses_template_parms (arglist2)
8710 && check_instantiated_args (templ, arglist2, complain)))
8711 return error_mark_node;
8712
8713 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8714 return parm;
8715 }
8716 else
8717 {
8718 tree template_type = TREE_TYPE (templ);
8719 tree gen_tmpl;
8720 tree type_decl;
8721 tree found = NULL_TREE;
8722 int arg_depth;
8723 int parm_depth;
8724 int is_dependent_type;
8725 int use_partial_inst_tmpl = false;
8726
8727 if (template_type == error_mark_node)
8728 /* An error occurred while building the template TEMPL, and a
8729 diagnostic has most certainly been emitted for that
8730 already. Let's propagate that error. */
8731 return error_mark_node;
8732
8733 gen_tmpl = most_general_template (templ);
8734 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8735 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8736 arg_depth = TMPL_ARGS_DEPTH (arglist);
8737
8738 if (arg_depth == 1 && parm_depth > 1)
8739 {
8740 /* We've been given an incomplete set of template arguments.
8741 For example, given:
8742
8743 template <class T> struct S1 {
8744 template <class U> struct S2 {};
8745 template <class U> struct S2<U*> {};
8746 };
8747
8748 we will be called with an ARGLIST of `U*', but the
8749 TEMPLATE will be `template <class T> template
8750 <class U> struct S1<T>::S2'. We must fill in the missing
8751 arguments. */
8752 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8753 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8754 arg_depth = TMPL_ARGS_DEPTH (arglist);
8755 }
8756
8757 /* Now we should have enough arguments. */
8758 gcc_assert (parm_depth == arg_depth);
8759
8760 /* From here on, we're only interested in the most general
8761 template. */
8762
8763 /* Calculate the BOUND_ARGS. These will be the args that are
8764 actually tsubst'd into the definition to create the
8765 instantiation. */
8766 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8767 complain,
8768 /*require_all_args=*/true,
8769 /*use_default_args=*/true);
8770
8771 if (arglist == error_mark_node)
8772 /* We were unable to bind the arguments. */
8773 return error_mark_node;
8774
8775 /* In the scope of a template class, explicit references to the
8776 template class refer to the type of the template, not any
8777 instantiation of it. For example, in:
8778
8779 template <class T> class C { void f(C<T>); }
8780
8781 the `C<T>' is just the same as `C'. Outside of the
8782 class, however, such a reference is an instantiation. */
8783 if (entering_scope
8784 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8785 || currently_open_class (template_type))
8786 {
8787 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8788
8789 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8790 return template_type;
8791 }
8792
8793 /* If we already have this specialization, return it. */
8794 elt.tmpl = gen_tmpl;
8795 elt.args = arglist;
8796 elt.spec = NULL_TREE;
8797 hash = spec_hasher::hash (&elt);
8798 entry = type_specializations->find_with_hash (&elt, hash);
8799
8800 if (entry)
8801 return entry->spec;
8802
8803 /* If the the template's constraints are not satisfied,
8804 then we cannot form a valid type.
8805
8806 Note that the check is deferred until after the hash
8807 lookup. This prevents redundant checks on previously
8808 instantiated specializations. */
8809 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8810 {
8811 if (complain & tf_error)
8812 {
8813 error ("template constraint failure");
8814 diagnose_constraints (input_location, gen_tmpl, arglist);
8815 }
8816 return error_mark_node;
8817 }
8818
8819 is_dependent_type = uses_template_parms (arglist);
8820
8821 /* If the deduced arguments are invalid, then the binding
8822 failed. */
8823 if (!is_dependent_type
8824 && check_instantiated_args (gen_tmpl,
8825 INNERMOST_TEMPLATE_ARGS (arglist),
8826 complain))
8827 return error_mark_node;
8828
8829 if (!is_dependent_type
8830 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8831 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8832 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8833 {
8834 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8835 DECL_NAME (gen_tmpl),
8836 /*tag_scope=*/ts_global);
8837 return found;
8838 }
8839
8840 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8841 complain, in_decl);
8842 if (context == error_mark_node)
8843 return error_mark_node;
8844
8845 if (!context)
8846 context = global_namespace;
8847
8848 /* Create the type. */
8849 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8850 {
8851 /* The user referred to a specialization of an alias
8852 template represented by GEN_TMPL.
8853
8854 [temp.alias]/2 says:
8855
8856 When a template-id refers to the specialization of an
8857 alias template, it is equivalent to the associated
8858 type obtained by substitution of its
8859 template-arguments for the template-parameters in the
8860 type-id of the alias template. */
8861
8862 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8863 /* Note that the call above (by indirectly calling
8864 register_specialization in tsubst_decl) registers the
8865 TYPE_DECL representing the specialization of the alias
8866 template. So next time someone substitutes ARGLIST for
8867 the template parms into the alias template (GEN_TMPL),
8868 she'll get that TYPE_DECL back. */
8869
8870 if (t == error_mark_node)
8871 return t;
8872 }
8873 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8874 {
8875 if (!is_dependent_type)
8876 {
8877 set_current_access_from_decl (TYPE_NAME (template_type));
8878 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8879 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8880 arglist, complain, in_decl),
8881 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8882 arglist, complain, in_decl),
8883 SCOPED_ENUM_P (template_type), NULL);
8884
8885 if (t == error_mark_node)
8886 return t;
8887 }
8888 else
8889 {
8890 /* We don't want to call start_enum for this type, since
8891 the values for the enumeration constants may involve
8892 template parameters. And, no one should be interested
8893 in the enumeration constants for such a type. */
8894 t = cxx_make_type (ENUMERAL_TYPE);
8895 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8896 }
8897 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8898 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8899 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8900 }
8901 else if (CLASS_TYPE_P (template_type))
8902 {
8903 t = make_class_type (TREE_CODE (template_type));
8904 CLASSTYPE_DECLARED_CLASS (t)
8905 = CLASSTYPE_DECLARED_CLASS (template_type);
8906 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8907
8908 /* A local class. Make sure the decl gets registered properly. */
8909 if (context == current_function_decl)
8910 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8911
8912 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8913 /* This instantiation is another name for the primary
8914 template type. Set the TYPE_CANONICAL field
8915 appropriately. */
8916 TYPE_CANONICAL (t) = template_type;
8917 else if (any_template_arguments_need_structural_equality_p (arglist))
8918 /* Some of the template arguments require structural
8919 equality testing, so this template class requires
8920 structural equality testing. */
8921 SET_TYPE_STRUCTURAL_EQUALITY (t);
8922 }
8923 else
8924 gcc_unreachable ();
8925
8926 /* If we called start_enum or pushtag above, this information
8927 will already be set up. */
8928 if (!TYPE_NAME (t))
8929 {
8930 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8931
8932 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8933 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8934 DECL_SOURCE_LOCATION (type_decl)
8935 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8936 }
8937 else
8938 type_decl = TYPE_NAME (t);
8939
8940 if (CLASS_TYPE_P (template_type))
8941 {
8942 TREE_PRIVATE (type_decl)
8943 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8944 TREE_PROTECTED (type_decl)
8945 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8946 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8947 {
8948 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8949 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8950 }
8951 }
8952
8953 if (OVERLOAD_TYPE_P (t)
8954 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8955 {
8956 static const char *tags[] = {"abi_tag", "may_alias"};
8957
8958 for (unsigned ix = 0; ix != 2; ix++)
8959 {
8960 tree attributes
8961 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8962
8963 if (attributes)
8964 TYPE_ATTRIBUTES (t)
8965 = tree_cons (TREE_PURPOSE (attributes),
8966 TREE_VALUE (attributes),
8967 TYPE_ATTRIBUTES (t));
8968 }
8969 }
8970
8971 /* Let's consider the explicit specialization of a member
8972 of a class template specialization that is implicitly instantiated,
8973 e.g.:
8974 template<class T>
8975 struct S
8976 {
8977 template<class U> struct M {}; //#0
8978 };
8979
8980 template<>
8981 template<>
8982 struct S<int>::M<char> //#1
8983 {
8984 int i;
8985 };
8986 [temp.expl.spec]/4 says this is valid.
8987
8988 In this case, when we write:
8989 S<int>::M<char> m;
8990
8991 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8992 the one of #0.
8993
8994 When we encounter #1, we want to store the partial instantiation
8995 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8996
8997 For all cases other than this "explicit specialization of member of a
8998 class template", we just want to store the most general template into
8999 the CLASSTYPE_TI_TEMPLATE of M.
9000
9001 This case of "explicit specialization of member of a class template"
9002 only happens when:
9003 1/ the enclosing class is an instantiation of, and therefore not
9004 the same as, the context of the most general template, and
9005 2/ we aren't looking at the partial instantiation itself, i.e.
9006 the innermost arguments are not the same as the innermost parms of
9007 the most general template.
9008
9009 So it's only when 1/ and 2/ happens that we want to use the partial
9010 instantiation of the member template in lieu of its most general
9011 template. */
9012
9013 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9014 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9015 /* the enclosing class must be an instantiation... */
9016 && CLASS_TYPE_P (context)
9017 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9018 {
9019 TREE_VEC_LENGTH (arglist)--;
9020 ++processing_template_decl;
9021 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9022 tree partial_inst_args =
9023 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9024 arglist, complain, NULL_TREE);
9025 --processing_template_decl;
9026 TREE_VEC_LENGTH (arglist)++;
9027 if (partial_inst_args == error_mark_node)
9028 return error_mark_node;
9029 use_partial_inst_tmpl =
9030 /*...and we must not be looking at the partial instantiation
9031 itself. */
9032 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9033 partial_inst_args);
9034 }
9035
9036 if (!use_partial_inst_tmpl)
9037 /* This case is easy; there are no member templates involved. */
9038 found = gen_tmpl;
9039 else
9040 {
9041 /* This is a full instantiation of a member template. Find
9042 the partial instantiation of which this is an instance. */
9043
9044 /* Temporarily reduce by one the number of levels in the ARGLIST
9045 so as to avoid comparing the last set of arguments. */
9046 TREE_VEC_LENGTH (arglist)--;
9047 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9048 TREE_VEC_LENGTH (arglist)++;
9049 /* FOUND is either a proper class type, or an alias
9050 template specialization. In the later case, it's a
9051 TYPE_DECL, resulting from the substituting of arguments
9052 for parameters in the TYPE_DECL of the alias template
9053 done earlier. So be careful while getting the template
9054 of FOUND. */
9055 found = (TREE_CODE (found) == TEMPLATE_DECL
9056 ? found
9057 : (TREE_CODE (found) == TYPE_DECL
9058 ? DECL_TI_TEMPLATE (found)
9059 : CLASSTYPE_TI_TEMPLATE (found)));
9060 }
9061
9062 // Build template info for the new specialization.
9063 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9064
9065 elt.spec = t;
9066 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9067 entry = ggc_alloc<spec_entry> ();
9068 *entry = elt;
9069 *slot = entry;
9070
9071 /* Note this use of the partial instantiation so we can check it
9072 later in maybe_process_partial_specialization. */
9073 DECL_TEMPLATE_INSTANTIATIONS (found)
9074 = tree_cons (arglist, t,
9075 DECL_TEMPLATE_INSTANTIATIONS (found));
9076
9077 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9078 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9079 /* Now that the type has been registered on the instantiations
9080 list, we set up the enumerators. Because the enumeration
9081 constants may involve the enumeration type itself, we make
9082 sure to register the type first, and then create the
9083 constants. That way, doing tsubst_expr for the enumeration
9084 constants won't result in recursive calls here; we'll find
9085 the instantiation and exit above. */
9086 tsubst_enum (template_type, t, arglist);
9087
9088 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9089 /* If the type makes use of template parameters, the
9090 code that generates debugging information will crash. */
9091 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9092
9093 /* Possibly limit visibility based on template args. */
9094 TREE_PUBLIC (type_decl) = 1;
9095 determine_visibility (type_decl);
9096
9097 inherit_targ_abi_tags (t);
9098
9099 return t;
9100 }
9101 }
9102
9103 /* Wrapper for lookup_template_class_1. */
9104
9105 tree
9106 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9107 int entering_scope, tsubst_flags_t complain)
9108 {
9109 tree ret;
9110 timevar_push (TV_TEMPLATE_INST);
9111 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9112 entering_scope, complain);
9113 timevar_pop (TV_TEMPLATE_INST);
9114 return ret;
9115 }
9116
9117 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9118
9119 tree
9120 lookup_template_variable (tree templ, tree arglist)
9121 {
9122 /* The type of the expression is NULL_TREE since the template-id could refer
9123 to an explicit or partial specialization. */
9124 tree type = NULL_TREE;
9125 if (flag_concepts && variable_concept_p (templ))
9126 /* Except that concepts are always bool. */
9127 type = boolean_type_node;
9128 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9129 }
9130
9131 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9132
9133 tree
9134 finish_template_variable (tree var, tsubst_flags_t complain)
9135 {
9136 tree templ = TREE_OPERAND (var, 0);
9137 tree arglist = TREE_OPERAND (var, 1);
9138
9139 /* We never want to return a VAR_DECL for a variable concept, since they
9140 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9141 bool concept_p = flag_concepts && variable_concept_p (templ);
9142 if (concept_p && processing_template_decl)
9143 return var;
9144
9145 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9146 arglist = add_outermost_template_args (tmpl_args, arglist);
9147
9148 templ = most_general_template (templ);
9149 tree parms = DECL_TEMPLATE_PARMS (templ);
9150 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9151 /*req_all*/true,
9152 /*use_default*/true);
9153
9154 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9155 {
9156 if (complain & tf_error)
9157 {
9158 error ("use of invalid variable template %qE", var);
9159 diagnose_constraints (location_of (var), templ, arglist);
9160 }
9161 return error_mark_node;
9162 }
9163
9164 /* If a template-id refers to a specialization of a variable
9165 concept, then the expression is true if and only if the
9166 concept's constraints are satisfied by the given template
9167 arguments.
9168
9169 NOTE: This is an extension of Concepts Lite TS that
9170 allows constraints to be used in expressions. */
9171 if (concept_p)
9172 {
9173 tree decl = DECL_TEMPLATE_RESULT (templ);
9174 return evaluate_variable_concept (decl, arglist);
9175 }
9176
9177 return instantiate_template (templ, arglist, complain);
9178 }
9179
9180 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9181 TARGS template args, and instantiate it if it's not dependent. */
9182
9183 tree
9184 lookup_and_finish_template_variable (tree templ, tree targs,
9185 tsubst_flags_t complain)
9186 {
9187 templ = lookup_template_variable (templ, targs);
9188 if (!any_dependent_template_arguments_p (targs))
9189 {
9190 templ = finish_template_variable (templ, complain);
9191 mark_used (templ);
9192 }
9193
9194 return convert_from_reference (templ);
9195 }
9196
9197 \f
9198 struct pair_fn_data
9199 {
9200 tree_fn_t fn;
9201 tree_fn_t any_fn;
9202 void *data;
9203 /* True when we should also visit template parameters that occur in
9204 non-deduced contexts. */
9205 bool include_nondeduced_p;
9206 hash_set<tree> *visited;
9207 };
9208
9209 /* Called from for_each_template_parm via walk_tree. */
9210
9211 static tree
9212 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9213 {
9214 tree t = *tp;
9215 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9216 tree_fn_t fn = pfd->fn;
9217 void *data = pfd->data;
9218 tree result = NULL_TREE;
9219
9220 #define WALK_SUBTREE(NODE) \
9221 do \
9222 { \
9223 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9224 pfd->include_nondeduced_p, \
9225 pfd->any_fn); \
9226 if (result) goto out; \
9227 } \
9228 while (0)
9229
9230 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9231 return t;
9232
9233 if (TYPE_P (t)
9234 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9235 WALK_SUBTREE (TYPE_CONTEXT (t));
9236
9237 switch (TREE_CODE (t))
9238 {
9239 case RECORD_TYPE:
9240 if (TYPE_PTRMEMFUNC_P (t))
9241 break;
9242 /* Fall through. */
9243
9244 case UNION_TYPE:
9245 case ENUMERAL_TYPE:
9246 if (!TYPE_TEMPLATE_INFO (t))
9247 *walk_subtrees = 0;
9248 else
9249 WALK_SUBTREE (TYPE_TI_ARGS (t));
9250 break;
9251
9252 case INTEGER_TYPE:
9253 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9254 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9255 break;
9256
9257 case METHOD_TYPE:
9258 /* Since we're not going to walk subtrees, we have to do this
9259 explicitly here. */
9260 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9261 /* Fall through. */
9262
9263 case FUNCTION_TYPE:
9264 /* Check the return type. */
9265 WALK_SUBTREE (TREE_TYPE (t));
9266
9267 /* Check the parameter types. Since default arguments are not
9268 instantiated until they are needed, the TYPE_ARG_TYPES may
9269 contain expressions that involve template parameters. But,
9270 no-one should be looking at them yet. And, once they're
9271 instantiated, they don't contain template parameters, so
9272 there's no point in looking at them then, either. */
9273 {
9274 tree parm;
9275
9276 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9277 WALK_SUBTREE (TREE_VALUE (parm));
9278
9279 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9280 want walk_tree walking into them itself. */
9281 *walk_subtrees = 0;
9282 }
9283
9284 if (flag_noexcept_type)
9285 {
9286 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9287 if (spec)
9288 WALK_SUBTREE (TREE_PURPOSE (spec));
9289 }
9290 break;
9291
9292 case TYPEOF_TYPE:
9293 case UNDERLYING_TYPE:
9294 if (pfd->include_nondeduced_p
9295 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9296 pfd->visited,
9297 pfd->include_nondeduced_p,
9298 pfd->any_fn))
9299 return error_mark_node;
9300 break;
9301
9302 case FUNCTION_DECL:
9303 case VAR_DECL:
9304 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9305 WALK_SUBTREE (DECL_TI_ARGS (t));
9306 /* Fall through. */
9307
9308 case PARM_DECL:
9309 case CONST_DECL:
9310 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9311 WALK_SUBTREE (DECL_INITIAL (t));
9312 if (DECL_CONTEXT (t)
9313 && pfd->include_nondeduced_p)
9314 WALK_SUBTREE (DECL_CONTEXT (t));
9315 break;
9316
9317 case BOUND_TEMPLATE_TEMPLATE_PARM:
9318 /* Record template parameters such as `T' inside `TT<T>'. */
9319 WALK_SUBTREE (TYPE_TI_ARGS (t));
9320 /* Fall through. */
9321
9322 case TEMPLATE_TEMPLATE_PARM:
9323 case TEMPLATE_TYPE_PARM:
9324 case TEMPLATE_PARM_INDEX:
9325 if (fn && (*fn)(t, data))
9326 return t;
9327 else if (!fn)
9328 return t;
9329 break;
9330
9331 case TEMPLATE_DECL:
9332 /* A template template parameter is encountered. */
9333 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9334 WALK_SUBTREE (TREE_TYPE (t));
9335
9336 /* Already substituted template template parameter */
9337 *walk_subtrees = 0;
9338 break;
9339
9340 case TYPENAME_TYPE:
9341 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9342 partial instantiation. */
9343 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9344 break;
9345
9346 case CONSTRUCTOR:
9347 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9348 && pfd->include_nondeduced_p)
9349 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9350 break;
9351
9352 case INDIRECT_REF:
9353 case COMPONENT_REF:
9354 /* If there's no type, then this thing must be some expression
9355 involving template parameters. */
9356 if (!fn && !TREE_TYPE (t))
9357 return error_mark_node;
9358 break;
9359
9360 case MODOP_EXPR:
9361 case CAST_EXPR:
9362 case IMPLICIT_CONV_EXPR:
9363 case REINTERPRET_CAST_EXPR:
9364 case CONST_CAST_EXPR:
9365 case STATIC_CAST_EXPR:
9366 case DYNAMIC_CAST_EXPR:
9367 case ARROW_EXPR:
9368 case DOTSTAR_EXPR:
9369 case TYPEID_EXPR:
9370 case PSEUDO_DTOR_EXPR:
9371 if (!fn)
9372 return error_mark_node;
9373 break;
9374
9375 default:
9376 break;
9377 }
9378
9379 #undef WALK_SUBTREE
9380
9381 /* We didn't find any template parameters we liked. */
9382 out:
9383 return result;
9384 }
9385
9386 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9387 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9388 call FN with the parameter and the DATA.
9389 If FN returns nonzero, the iteration is terminated, and
9390 for_each_template_parm returns 1. Otherwise, the iteration
9391 continues. If FN never returns a nonzero value, the value
9392 returned by for_each_template_parm is 0. If FN is NULL, it is
9393 considered to be the function which always returns 1.
9394
9395 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9396 parameters that occur in non-deduced contexts. When false, only
9397 visits those template parameters that can be deduced. */
9398
9399 static tree
9400 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9401 hash_set<tree> *visited,
9402 bool include_nondeduced_p,
9403 tree_fn_t any_fn)
9404 {
9405 struct pair_fn_data pfd;
9406 tree result;
9407
9408 /* Set up. */
9409 pfd.fn = fn;
9410 pfd.any_fn = any_fn;
9411 pfd.data = data;
9412 pfd.include_nondeduced_p = include_nondeduced_p;
9413
9414 /* Walk the tree. (Conceptually, we would like to walk without
9415 duplicates, but for_each_template_parm_r recursively calls
9416 for_each_template_parm, so we would need to reorganize a fair
9417 bit to use walk_tree_without_duplicates, so we keep our own
9418 visited list.) */
9419 if (visited)
9420 pfd.visited = visited;
9421 else
9422 pfd.visited = new hash_set<tree>;
9423 result = cp_walk_tree (&t,
9424 for_each_template_parm_r,
9425 &pfd,
9426 pfd.visited);
9427
9428 /* Clean up. */
9429 if (!visited)
9430 {
9431 delete pfd.visited;
9432 pfd.visited = 0;
9433 }
9434
9435 return result;
9436 }
9437
9438 /* Returns true if T depends on any template parameter. */
9439
9440 int
9441 uses_template_parms (tree t)
9442 {
9443 if (t == NULL_TREE)
9444 return false;
9445
9446 bool dependent_p;
9447 int saved_processing_template_decl;
9448
9449 saved_processing_template_decl = processing_template_decl;
9450 if (!saved_processing_template_decl)
9451 processing_template_decl = 1;
9452 if (TYPE_P (t))
9453 dependent_p = dependent_type_p (t);
9454 else if (TREE_CODE (t) == TREE_VEC)
9455 dependent_p = any_dependent_template_arguments_p (t);
9456 else if (TREE_CODE (t) == TREE_LIST)
9457 dependent_p = (uses_template_parms (TREE_VALUE (t))
9458 || uses_template_parms (TREE_CHAIN (t)));
9459 else if (TREE_CODE (t) == TYPE_DECL)
9460 dependent_p = dependent_type_p (TREE_TYPE (t));
9461 else if (DECL_P (t)
9462 || EXPR_P (t)
9463 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9464 || TREE_CODE (t) == OVERLOAD
9465 || BASELINK_P (t)
9466 || identifier_p (t)
9467 || TREE_CODE (t) == TRAIT_EXPR
9468 || TREE_CODE (t) == CONSTRUCTOR
9469 || CONSTANT_CLASS_P (t))
9470 dependent_p = (type_dependent_expression_p (t)
9471 || value_dependent_expression_p (t));
9472 else
9473 {
9474 gcc_assert (t == error_mark_node);
9475 dependent_p = false;
9476 }
9477
9478 processing_template_decl = saved_processing_template_decl;
9479
9480 return dependent_p;
9481 }
9482
9483 /* Returns true iff current_function_decl is an incompletely instantiated
9484 template. Useful instead of processing_template_decl because the latter
9485 is set to 0 during instantiate_non_dependent_expr. */
9486
9487 bool
9488 in_template_function (void)
9489 {
9490 tree fn = current_function_decl;
9491 bool ret;
9492 ++processing_template_decl;
9493 ret = (fn && DECL_LANG_SPECIFIC (fn)
9494 && DECL_TEMPLATE_INFO (fn)
9495 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9496 --processing_template_decl;
9497 return ret;
9498 }
9499
9500 /* Returns true iff we are currently within a template other than a
9501 default-capturing generic lambda, so we don't need to worry about semantic
9502 processing. */
9503
9504 bool
9505 processing_nonlambda_template (void)
9506 {
9507 return processing_template_decl && !need_generic_capture ();
9508 }
9509
9510 /* Returns true if T depends on any template parameter with level LEVEL. */
9511
9512 bool
9513 uses_template_parms_level (tree t, int level)
9514 {
9515 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9516 /*include_nondeduced_p=*/true);
9517 }
9518
9519 /* Returns true if the signature of DECL depends on any template parameter from
9520 its enclosing class. */
9521
9522 bool
9523 uses_outer_template_parms (tree decl)
9524 {
9525 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9526 if (depth == 0)
9527 return false;
9528 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9529 &depth, NULL, /*include_nondeduced_p=*/true))
9530 return true;
9531 if (PRIMARY_TEMPLATE_P (decl)
9532 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9533 (DECL_TEMPLATE_PARMS (decl)),
9534 template_parm_outer_level,
9535 &depth, NULL, /*include_nondeduced_p=*/true))
9536 return true;
9537 tree ci = get_constraints (decl);
9538 if (ci)
9539 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9540 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9541 &depth, NULL, /*nondeduced*/true))
9542 return true;
9543 return false;
9544 }
9545
9546 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9547 ill-formed translation unit, i.e. a variable or function that isn't
9548 usable in a constant expression. */
9549
9550 static inline bool
9551 neglectable_inst_p (tree d)
9552 {
9553 return (DECL_P (d)
9554 && !undeduced_auto_decl (d)
9555 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9556 : decl_maybe_constant_var_p (d)));
9557 }
9558
9559 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9560 neglectable and instantiated from within an erroneous instantiation. */
9561
9562 static bool
9563 limit_bad_template_recursion (tree decl)
9564 {
9565 struct tinst_level *lev = current_tinst_level;
9566 int errs = errorcount + sorrycount;
9567 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9568 return false;
9569
9570 for (; lev; lev = lev->next)
9571 if (neglectable_inst_p (lev->decl))
9572 break;
9573
9574 return (lev && errs > lev->errors);
9575 }
9576
9577 static int tinst_depth;
9578 extern int max_tinst_depth;
9579 int depth_reached;
9580
9581 static GTY(()) struct tinst_level *last_error_tinst_level;
9582
9583 /* We're starting to instantiate D; record the template instantiation context
9584 for diagnostics and to restore it later. */
9585
9586 bool
9587 push_tinst_level (tree d)
9588 {
9589 return push_tinst_level_loc (d, input_location);
9590 }
9591
9592 /* We're starting to instantiate D; record the template instantiation context
9593 at LOC for diagnostics and to restore it later. */
9594
9595 bool
9596 push_tinst_level_loc (tree d, location_t loc)
9597 {
9598 struct tinst_level *new_level;
9599
9600 if (tinst_depth >= max_tinst_depth)
9601 {
9602 /* Tell error.c not to try to instantiate any templates. */
9603 at_eof = 2;
9604 fatal_error (input_location,
9605 "template instantiation depth exceeds maximum of %d"
9606 " (use -ftemplate-depth= to increase the maximum)",
9607 max_tinst_depth);
9608 return false;
9609 }
9610
9611 /* If the current instantiation caused problems, don't let it instantiate
9612 anything else. Do allow deduction substitution and decls usable in
9613 constant expressions. */
9614 if (limit_bad_template_recursion (d))
9615 return false;
9616
9617 /* When not -quiet, dump template instantiations other than functions, since
9618 announce_function will take care of those. */
9619 if (!quiet_flag
9620 && TREE_CODE (d) != TREE_LIST
9621 && TREE_CODE (d) != FUNCTION_DECL)
9622 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9623
9624 new_level = ggc_alloc<tinst_level> ();
9625 new_level->decl = d;
9626 new_level->locus = loc;
9627 new_level->errors = errorcount+sorrycount;
9628 new_level->in_system_header_p = in_system_header_at (input_location);
9629 new_level->next = current_tinst_level;
9630 current_tinst_level = new_level;
9631
9632 ++tinst_depth;
9633 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9634 depth_reached = tinst_depth;
9635
9636 return true;
9637 }
9638
9639 /* We're done instantiating this template; return to the instantiation
9640 context. */
9641
9642 void
9643 pop_tinst_level (void)
9644 {
9645 /* Restore the filename and line number stashed away when we started
9646 this instantiation. */
9647 input_location = current_tinst_level->locus;
9648 current_tinst_level = current_tinst_level->next;
9649 --tinst_depth;
9650 }
9651
9652 /* We're instantiating a deferred template; restore the template
9653 instantiation context in which the instantiation was requested, which
9654 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9655
9656 static tree
9657 reopen_tinst_level (struct tinst_level *level)
9658 {
9659 struct tinst_level *t;
9660
9661 tinst_depth = 0;
9662 for (t = level; t; t = t->next)
9663 ++tinst_depth;
9664
9665 current_tinst_level = level;
9666 pop_tinst_level ();
9667 if (current_tinst_level)
9668 current_tinst_level->errors = errorcount+sorrycount;
9669 return level->decl;
9670 }
9671
9672 /* Returns the TINST_LEVEL which gives the original instantiation
9673 context. */
9674
9675 struct tinst_level *
9676 outermost_tinst_level (void)
9677 {
9678 struct tinst_level *level = current_tinst_level;
9679 if (level)
9680 while (level->next)
9681 level = level->next;
9682 return level;
9683 }
9684
9685 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9686 vector of template arguments, as for tsubst.
9687
9688 Returns an appropriate tsubst'd friend declaration. */
9689
9690 static tree
9691 tsubst_friend_function (tree decl, tree args)
9692 {
9693 tree new_friend;
9694
9695 if (TREE_CODE (decl) == FUNCTION_DECL
9696 && DECL_TEMPLATE_INSTANTIATION (decl)
9697 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9698 /* This was a friend declared with an explicit template
9699 argument list, e.g.:
9700
9701 friend void f<>(T);
9702
9703 to indicate that f was a template instantiation, not a new
9704 function declaration. Now, we have to figure out what
9705 instantiation of what template. */
9706 {
9707 tree template_id, arglist, fns;
9708 tree new_args;
9709 tree tmpl;
9710 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9711
9712 /* Friend functions are looked up in the containing namespace scope.
9713 We must enter that scope, to avoid finding member functions of the
9714 current class with same name. */
9715 push_nested_namespace (ns);
9716 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9717 tf_warning_or_error, NULL_TREE,
9718 /*integral_constant_expression_p=*/false);
9719 pop_nested_namespace (ns);
9720 arglist = tsubst (DECL_TI_ARGS (decl), args,
9721 tf_warning_or_error, NULL_TREE);
9722 template_id = lookup_template_function (fns, arglist);
9723
9724 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9725 tmpl = determine_specialization (template_id, new_friend,
9726 &new_args,
9727 /*need_member_template=*/0,
9728 TREE_VEC_LENGTH (args),
9729 tsk_none);
9730 return instantiate_template (tmpl, new_args, tf_error);
9731 }
9732
9733 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9734
9735 /* The NEW_FRIEND will look like an instantiation, to the
9736 compiler, but is not an instantiation from the point of view of
9737 the language. For example, we might have had:
9738
9739 template <class T> struct S {
9740 template <class U> friend void f(T, U);
9741 };
9742
9743 Then, in S<int>, template <class U> void f(int, U) is not an
9744 instantiation of anything. */
9745 if (new_friend == error_mark_node)
9746 return error_mark_node;
9747
9748 DECL_USE_TEMPLATE (new_friend) = 0;
9749 if (TREE_CODE (decl) == TEMPLATE_DECL)
9750 {
9751 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9752 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9753 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9754 }
9755
9756 /* The mangled name for the NEW_FRIEND is incorrect. The function
9757 is not a template instantiation and should not be mangled like
9758 one. Therefore, we forget the mangling here; we'll recompute it
9759 later if we need it. */
9760 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9761 {
9762 SET_DECL_RTL (new_friend, NULL);
9763 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9764 }
9765
9766 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9767 {
9768 tree old_decl;
9769 tree new_friend_template_info;
9770 tree new_friend_result_template_info;
9771 tree ns;
9772 int new_friend_is_defn;
9773
9774 /* We must save some information from NEW_FRIEND before calling
9775 duplicate decls since that function will free NEW_FRIEND if
9776 possible. */
9777 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9778 new_friend_is_defn =
9779 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9780 (template_for_substitution (new_friend)))
9781 != NULL_TREE);
9782 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9783 {
9784 /* This declaration is a `primary' template. */
9785 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9786
9787 new_friend_result_template_info
9788 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9789 }
9790 else
9791 new_friend_result_template_info = NULL_TREE;
9792
9793 /* Inside pushdecl_namespace_level, we will push into the
9794 current namespace. However, the friend function should go
9795 into the namespace of the template. */
9796 ns = decl_namespace_context (new_friend);
9797 push_nested_namespace (ns);
9798 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9799 pop_nested_namespace (ns);
9800
9801 if (old_decl == error_mark_node)
9802 return error_mark_node;
9803
9804 if (old_decl != new_friend)
9805 {
9806 /* This new friend declaration matched an existing
9807 declaration. For example, given:
9808
9809 template <class T> void f(T);
9810 template <class U> class C {
9811 template <class T> friend void f(T) {}
9812 };
9813
9814 the friend declaration actually provides the definition
9815 of `f', once C has been instantiated for some type. So,
9816 old_decl will be the out-of-class template declaration,
9817 while new_friend is the in-class definition.
9818
9819 But, if `f' was called before this point, the
9820 instantiation of `f' will have DECL_TI_ARGS corresponding
9821 to `T' but not to `U', references to which might appear
9822 in the definition of `f'. Previously, the most general
9823 template for an instantiation of `f' was the out-of-class
9824 version; now it is the in-class version. Therefore, we
9825 run through all specialization of `f', adding to their
9826 DECL_TI_ARGS appropriately. In particular, they need a
9827 new set of outer arguments, corresponding to the
9828 arguments for this class instantiation.
9829
9830 The same situation can arise with something like this:
9831
9832 friend void f(int);
9833 template <class T> class C {
9834 friend void f(T) {}
9835 };
9836
9837 when `C<int>' is instantiated. Now, `f(int)' is defined
9838 in the class. */
9839
9840 if (!new_friend_is_defn)
9841 /* On the other hand, if the in-class declaration does
9842 *not* provide a definition, then we don't want to alter
9843 existing definitions. We can just leave everything
9844 alone. */
9845 ;
9846 else
9847 {
9848 tree new_template = TI_TEMPLATE (new_friend_template_info);
9849 tree new_args = TI_ARGS (new_friend_template_info);
9850
9851 /* Overwrite whatever template info was there before, if
9852 any, with the new template information pertaining to
9853 the declaration. */
9854 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9855
9856 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9857 {
9858 /* We should have called reregister_specialization in
9859 duplicate_decls. */
9860 gcc_assert (retrieve_specialization (new_template,
9861 new_args, 0)
9862 == old_decl);
9863
9864 /* Instantiate it if the global has already been used. */
9865 if (DECL_ODR_USED (old_decl))
9866 instantiate_decl (old_decl, /*defer_ok=*/true,
9867 /*expl_inst_class_mem_p=*/false);
9868 }
9869 else
9870 {
9871 tree t;
9872
9873 /* Indicate that the old function template is a partial
9874 instantiation. */
9875 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9876 = new_friend_result_template_info;
9877
9878 gcc_assert (new_template
9879 == most_general_template (new_template));
9880 gcc_assert (new_template != old_decl);
9881
9882 /* Reassign any specializations already in the hash table
9883 to the new more general template, and add the
9884 additional template args. */
9885 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9886 t != NULL_TREE;
9887 t = TREE_CHAIN (t))
9888 {
9889 tree spec = TREE_VALUE (t);
9890 spec_entry elt;
9891
9892 elt.tmpl = old_decl;
9893 elt.args = DECL_TI_ARGS (spec);
9894 elt.spec = NULL_TREE;
9895
9896 decl_specializations->remove_elt (&elt);
9897
9898 DECL_TI_ARGS (spec)
9899 = add_outermost_template_args (new_args,
9900 DECL_TI_ARGS (spec));
9901
9902 register_specialization
9903 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9904
9905 }
9906 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9907 }
9908 }
9909
9910 /* The information from NEW_FRIEND has been merged into OLD_DECL
9911 by duplicate_decls. */
9912 new_friend = old_decl;
9913 }
9914 }
9915 else
9916 {
9917 tree context = DECL_CONTEXT (new_friend);
9918 bool dependent_p;
9919
9920 /* In the code
9921 template <class T> class C {
9922 template <class U> friend void C1<U>::f (); // case 1
9923 friend void C2<T>::f (); // case 2
9924 };
9925 we only need to make sure CONTEXT is a complete type for
9926 case 2. To distinguish between the two cases, we note that
9927 CONTEXT of case 1 remains dependent type after tsubst while
9928 this isn't true for case 2. */
9929 ++processing_template_decl;
9930 dependent_p = dependent_type_p (context);
9931 --processing_template_decl;
9932
9933 if (!dependent_p
9934 && !complete_type_or_else (context, NULL_TREE))
9935 return error_mark_node;
9936
9937 if (COMPLETE_TYPE_P (context))
9938 {
9939 tree fn = new_friend;
9940 /* do_friend adds the TEMPLATE_DECL for any member friend
9941 template even if it isn't a member template, i.e.
9942 template <class T> friend A<T>::f();
9943 Look through it in that case. */
9944 if (TREE_CODE (fn) == TEMPLATE_DECL
9945 && !PRIMARY_TEMPLATE_P (fn))
9946 fn = DECL_TEMPLATE_RESULT (fn);
9947 /* Check to see that the declaration is really present, and,
9948 possibly obtain an improved declaration. */
9949 fn = check_classfn (context, fn, NULL_TREE);
9950
9951 if (fn)
9952 new_friend = fn;
9953 }
9954 }
9955
9956 return new_friend;
9957 }
9958
9959 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9960 template arguments, as for tsubst.
9961
9962 Returns an appropriate tsubst'd friend type or error_mark_node on
9963 failure. */
9964
9965 static tree
9966 tsubst_friend_class (tree friend_tmpl, tree args)
9967 {
9968 tree friend_type;
9969 tree tmpl;
9970 tree context;
9971
9972 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9973 {
9974 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9975 return TREE_TYPE (t);
9976 }
9977
9978 context = CP_DECL_CONTEXT (friend_tmpl);
9979
9980 if (context != global_namespace)
9981 {
9982 if (TREE_CODE (context) == NAMESPACE_DECL)
9983 push_nested_namespace (context);
9984 else
9985 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9986 }
9987
9988 /* Look for a class template declaration. We look for hidden names
9989 because two friend declarations of the same template are the
9990 same. For example, in:
9991
9992 struct A {
9993 template <typename> friend class F;
9994 };
9995 template <typename> struct B {
9996 template <typename> friend class F;
9997 };
9998
9999 both F templates are the same. */
10000 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
10001 /*block_p=*/true, 0, LOOKUP_HIDDEN);
10002
10003 /* But, if we don't find one, it might be because we're in a
10004 situation like this:
10005
10006 template <class T>
10007 struct S {
10008 template <class U>
10009 friend struct S;
10010 };
10011
10012 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
10013 for `S<int>', not the TEMPLATE_DECL. */
10014 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
10015 {
10016 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
10017 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
10018 }
10019
10020 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10021 {
10022 /* The friend template has already been declared. Just
10023 check to see that the declarations match, and install any new
10024 default parameters. We must tsubst the default parameters,
10025 of course. We only need the innermost template parameters
10026 because that is all that redeclare_class_template will look
10027 at. */
10028 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10029 > TMPL_ARGS_DEPTH (args))
10030 {
10031 tree parms;
10032 location_t saved_input_location;
10033 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10034 args, tf_warning_or_error);
10035
10036 saved_input_location = input_location;
10037 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10038 tree cons = get_constraints (tmpl);
10039 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10040 input_location = saved_input_location;
10041
10042 }
10043
10044 friend_type = TREE_TYPE (tmpl);
10045 }
10046 else
10047 {
10048 /* The friend template has not already been declared. In this
10049 case, the instantiation of the template class will cause the
10050 injection of this template into the global scope. */
10051 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10052 if (tmpl == error_mark_node)
10053 return error_mark_node;
10054
10055 /* The new TMPL is not an instantiation of anything, so we
10056 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10057 the new type because that is supposed to be the corresponding
10058 template decl, i.e., TMPL. */
10059 DECL_USE_TEMPLATE (tmpl) = 0;
10060 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10061 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10062 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10063 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10064
10065 /* Inject this template into the global scope. */
10066 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10067 }
10068
10069 if (context != global_namespace)
10070 {
10071 if (TREE_CODE (context) == NAMESPACE_DECL)
10072 pop_nested_namespace (context);
10073 else
10074 pop_nested_class ();
10075 }
10076
10077 return friend_type;
10078 }
10079
10080 /* Returns zero if TYPE cannot be completed later due to circularity.
10081 Otherwise returns one. */
10082
10083 static int
10084 can_complete_type_without_circularity (tree type)
10085 {
10086 if (type == NULL_TREE || type == error_mark_node)
10087 return 0;
10088 else if (COMPLETE_TYPE_P (type))
10089 return 1;
10090 else if (TREE_CODE (type) == ARRAY_TYPE)
10091 return can_complete_type_without_circularity (TREE_TYPE (type));
10092 else if (CLASS_TYPE_P (type)
10093 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10094 return 0;
10095 else
10096 return 1;
10097 }
10098
10099 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10100 tsubst_flags_t, tree);
10101
10102 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10103 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10104
10105 static tree
10106 tsubst_attribute (tree t, tree *decl_p, tree args,
10107 tsubst_flags_t complain, tree in_decl)
10108 {
10109 gcc_assert (ATTR_IS_DEPENDENT (t));
10110
10111 tree val = TREE_VALUE (t);
10112 if (val == NULL_TREE)
10113 /* Nothing to do. */;
10114 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10115 && is_attribute_p ("omp declare simd",
10116 get_attribute_name (t)))
10117 {
10118 tree clauses = TREE_VALUE (val);
10119 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10120 complain, in_decl);
10121 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10122 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10123 tree parms = DECL_ARGUMENTS (*decl_p);
10124 clauses
10125 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10126 if (clauses)
10127 val = build_tree_list (NULL_TREE, clauses);
10128 else
10129 val = NULL_TREE;
10130 }
10131 /* If the first attribute argument is an identifier, don't
10132 pass it through tsubst. Attributes like mode, format,
10133 cleanup and several target specific attributes expect it
10134 unmodified. */
10135 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10136 {
10137 tree chain
10138 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10139 /*integral_constant_expression_p=*/false);
10140 if (chain != TREE_CHAIN (val))
10141 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10142 }
10143 else if (PACK_EXPANSION_P (val))
10144 {
10145 /* An attribute pack expansion. */
10146 tree purp = TREE_PURPOSE (t);
10147 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10148 if (pack == error_mark_node)
10149 return error_mark_node;
10150 int len = TREE_VEC_LENGTH (pack);
10151 tree list = NULL_TREE;
10152 tree *q = &list;
10153 for (int i = 0; i < len; ++i)
10154 {
10155 tree elt = TREE_VEC_ELT (pack, i);
10156 *q = build_tree_list (purp, elt);
10157 q = &TREE_CHAIN (*q);
10158 }
10159 return list;
10160 }
10161 else
10162 val = tsubst_expr (val, args, complain, in_decl,
10163 /*integral_constant_expression_p=*/false);
10164
10165 if (val != TREE_VALUE (t))
10166 return build_tree_list (TREE_PURPOSE (t), val);
10167 return t;
10168 }
10169
10170 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10171 unchanged or a new TREE_LIST chain. */
10172
10173 static tree
10174 tsubst_attributes (tree attributes, tree args,
10175 tsubst_flags_t complain, tree in_decl)
10176 {
10177 tree last_dep = NULL_TREE;
10178
10179 for (tree t = attributes; t; t = TREE_CHAIN (t))
10180 if (ATTR_IS_DEPENDENT (t))
10181 {
10182 last_dep = t;
10183 attributes = copy_list (attributes);
10184 break;
10185 }
10186
10187 if (last_dep)
10188 for (tree *p = &attributes; *p; )
10189 {
10190 tree t = *p;
10191 if (ATTR_IS_DEPENDENT (t))
10192 {
10193 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10194 if (subst != t)
10195 {
10196 *p = subst;
10197 do
10198 p = &TREE_CHAIN (*p);
10199 while (*p);
10200 *p = TREE_CHAIN (t);
10201 continue;
10202 }
10203 }
10204 p = &TREE_CHAIN (*p);
10205 }
10206
10207 return attributes;
10208 }
10209
10210 /* Apply any attributes which had to be deferred until instantiation
10211 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10212 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10213
10214 static void
10215 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10216 tree args, tsubst_flags_t complain, tree in_decl)
10217 {
10218 tree last_dep = NULL_TREE;
10219 tree t;
10220 tree *p;
10221
10222 if (attributes == NULL_TREE)
10223 return;
10224
10225 if (DECL_P (*decl_p))
10226 {
10227 if (TREE_TYPE (*decl_p) == error_mark_node)
10228 return;
10229 p = &DECL_ATTRIBUTES (*decl_p);
10230 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10231 to our attributes parameter. */
10232 gcc_assert (*p == attributes);
10233 }
10234 else
10235 {
10236 p = &TYPE_ATTRIBUTES (*decl_p);
10237 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10238 lookup_template_class_1, and should be preserved. */
10239 gcc_assert (*p != attributes);
10240 while (*p)
10241 p = &TREE_CHAIN (*p);
10242 }
10243
10244 for (t = attributes; t; t = TREE_CHAIN (t))
10245 if (ATTR_IS_DEPENDENT (t))
10246 {
10247 last_dep = t;
10248 attributes = copy_list (attributes);
10249 break;
10250 }
10251
10252 *p = attributes;
10253 if (last_dep)
10254 {
10255 tree late_attrs = NULL_TREE;
10256 tree *q = &late_attrs;
10257
10258 for (; *p; )
10259 {
10260 t = *p;
10261 if (ATTR_IS_DEPENDENT (t))
10262 {
10263 *p = TREE_CHAIN (t);
10264 TREE_CHAIN (t) = NULL_TREE;
10265 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10266 do
10267 q = &TREE_CHAIN (*q);
10268 while (*q);
10269 }
10270 else
10271 p = &TREE_CHAIN (t);
10272 }
10273
10274 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10275 }
10276 }
10277
10278 /* Perform (or defer) access check for typedefs that were referenced
10279 from within the template TMPL code.
10280 This is a subroutine of instantiate_decl and instantiate_class_template.
10281 TMPL is the template to consider and TARGS is the list of arguments of
10282 that template. */
10283
10284 static void
10285 perform_typedefs_access_check (tree tmpl, tree targs)
10286 {
10287 location_t saved_location;
10288 unsigned i;
10289 qualified_typedef_usage_t *iter;
10290
10291 if (!tmpl
10292 || (!CLASS_TYPE_P (tmpl)
10293 && TREE_CODE (tmpl) != FUNCTION_DECL))
10294 return;
10295
10296 saved_location = input_location;
10297 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10298 {
10299 tree type_decl = iter->typedef_decl;
10300 tree type_scope = iter->context;
10301
10302 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10303 continue;
10304
10305 if (uses_template_parms (type_decl))
10306 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10307 if (uses_template_parms (type_scope))
10308 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10309
10310 /* Make access check error messages point to the location
10311 of the use of the typedef. */
10312 input_location = iter->locus;
10313 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10314 type_decl, type_decl,
10315 tf_warning_or_error);
10316 }
10317 input_location = saved_location;
10318 }
10319
10320 static tree
10321 instantiate_class_template_1 (tree type)
10322 {
10323 tree templ, args, pattern, t, member;
10324 tree typedecl;
10325 tree pbinfo;
10326 tree base_list;
10327 unsigned int saved_maximum_field_alignment;
10328 tree fn_context;
10329
10330 if (type == error_mark_node)
10331 return error_mark_node;
10332
10333 if (COMPLETE_OR_OPEN_TYPE_P (type)
10334 || uses_template_parms (type))
10335 return type;
10336
10337 /* Figure out which template is being instantiated. */
10338 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10339 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10340
10341 /* Determine what specialization of the original template to
10342 instantiate. */
10343 t = most_specialized_partial_spec (type, tf_warning_or_error);
10344 if (t == error_mark_node)
10345 {
10346 TYPE_BEING_DEFINED (type) = 1;
10347 return error_mark_node;
10348 }
10349 else if (t)
10350 {
10351 /* This TYPE is actually an instantiation of a partial
10352 specialization. We replace the innermost set of ARGS with
10353 the arguments appropriate for substitution. For example,
10354 given:
10355
10356 template <class T> struct S {};
10357 template <class T> struct S<T*> {};
10358
10359 and supposing that we are instantiating S<int*>, ARGS will
10360 presently be {int*} -- but we need {int}. */
10361 pattern = TREE_TYPE (t);
10362 args = TREE_PURPOSE (t);
10363 }
10364 else
10365 {
10366 pattern = TREE_TYPE (templ);
10367 args = CLASSTYPE_TI_ARGS (type);
10368 }
10369
10370 /* If the template we're instantiating is incomplete, then clearly
10371 there's nothing we can do. */
10372 if (!COMPLETE_TYPE_P (pattern))
10373 return type;
10374
10375 /* If we've recursively instantiated too many templates, stop. */
10376 if (! push_tinst_level (type))
10377 return type;
10378
10379 /* Now we're really doing the instantiation. Mark the type as in
10380 the process of being defined. */
10381 TYPE_BEING_DEFINED (type) = 1;
10382
10383 /* We may be in the middle of deferred access check. Disable
10384 it now. */
10385 push_deferring_access_checks (dk_no_deferred);
10386
10387 int saved_unevaluated_operand = cp_unevaluated_operand;
10388 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10389
10390 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10391 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10392 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10393 fn_context = error_mark_node;
10394 if (!fn_context)
10395 push_to_top_level ();
10396 else
10397 {
10398 cp_unevaluated_operand = 0;
10399 c_inhibit_evaluation_warnings = 0;
10400 }
10401 /* Use #pragma pack from the template context. */
10402 saved_maximum_field_alignment = maximum_field_alignment;
10403 maximum_field_alignment = TYPE_PRECISION (pattern);
10404
10405 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10406
10407 /* Set the input location to the most specialized template definition.
10408 This is needed if tsubsting causes an error. */
10409 typedecl = TYPE_MAIN_DECL (pattern);
10410 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10411 DECL_SOURCE_LOCATION (typedecl);
10412
10413 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10414 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10415 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10416 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10417 if (ANON_AGGR_TYPE_P (pattern))
10418 SET_ANON_AGGR_TYPE_P (type);
10419 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10420 {
10421 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10422 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10423 /* Adjust visibility for template arguments. */
10424 determine_visibility (TYPE_MAIN_DECL (type));
10425 }
10426 if (CLASS_TYPE_P (type))
10427 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10428
10429 pbinfo = TYPE_BINFO (pattern);
10430
10431 /* We should never instantiate a nested class before its enclosing
10432 class; we need to look up the nested class by name before we can
10433 instantiate it, and that lookup should instantiate the enclosing
10434 class. */
10435 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10436 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10437
10438 base_list = NULL_TREE;
10439 if (BINFO_N_BASE_BINFOS (pbinfo))
10440 {
10441 tree pbase_binfo;
10442 tree pushed_scope;
10443 int i;
10444
10445 /* We must enter the scope containing the type, as that is where
10446 the accessibility of types named in dependent bases are
10447 looked up from. */
10448 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10449
10450 /* Substitute into each of the bases to determine the actual
10451 basetypes. */
10452 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10453 {
10454 tree base;
10455 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10456 tree expanded_bases = NULL_TREE;
10457 int idx, len = 1;
10458
10459 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10460 {
10461 expanded_bases =
10462 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10463 args, tf_error, NULL_TREE);
10464 if (expanded_bases == error_mark_node)
10465 continue;
10466
10467 len = TREE_VEC_LENGTH (expanded_bases);
10468 }
10469
10470 for (idx = 0; idx < len; idx++)
10471 {
10472 if (expanded_bases)
10473 /* Extract the already-expanded base class. */
10474 base = TREE_VEC_ELT (expanded_bases, idx);
10475 else
10476 /* Substitute to figure out the base class. */
10477 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10478 NULL_TREE);
10479
10480 if (base == error_mark_node)
10481 continue;
10482
10483 base_list = tree_cons (access, base, base_list);
10484 if (BINFO_VIRTUAL_P (pbase_binfo))
10485 TREE_TYPE (base_list) = integer_type_node;
10486 }
10487 }
10488
10489 /* The list is now in reverse order; correct that. */
10490 base_list = nreverse (base_list);
10491
10492 if (pushed_scope)
10493 pop_scope (pushed_scope);
10494 }
10495 /* Now call xref_basetypes to set up all the base-class
10496 information. */
10497 xref_basetypes (type, base_list);
10498
10499 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10500 (int) ATTR_FLAG_TYPE_IN_PLACE,
10501 args, tf_error, NULL_TREE);
10502 fixup_attribute_variants (type);
10503
10504 /* Now that our base classes are set up, enter the scope of the
10505 class, so that name lookups into base classes, etc. will work
10506 correctly. This is precisely analogous to what we do in
10507 begin_class_definition when defining an ordinary non-template
10508 class, except we also need to push the enclosing classes. */
10509 push_nested_class (type);
10510
10511 /* Now members are processed in the order of declaration. */
10512 for (member = CLASSTYPE_DECL_LIST (pattern);
10513 member; member = TREE_CHAIN (member))
10514 {
10515 tree t = TREE_VALUE (member);
10516
10517 if (TREE_PURPOSE (member))
10518 {
10519 if (TYPE_P (t))
10520 {
10521 /* Build new CLASSTYPE_NESTED_UTDS. */
10522
10523 tree newtag;
10524 bool class_template_p;
10525
10526 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10527 && TYPE_LANG_SPECIFIC (t)
10528 && CLASSTYPE_IS_TEMPLATE (t));
10529 /* If the member is a class template, then -- even after
10530 substitution -- there may be dependent types in the
10531 template argument list for the class. We increment
10532 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10533 that function will assume that no types are dependent
10534 when outside of a template. */
10535 if (class_template_p)
10536 ++processing_template_decl;
10537 newtag = tsubst (t, args, tf_error, NULL_TREE);
10538 if (class_template_p)
10539 --processing_template_decl;
10540 if (newtag == error_mark_node)
10541 continue;
10542
10543 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10544 {
10545 tree name = TYPE_IDENTIFIER (t);
10546
10547 if (class_template_p)
10548 /* Unfortunately, lookup_template_class sets
10549 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10550 instantiation (i.e., for the type of a member
10551 template class nested within a template class.)
10552 This behavior is required for
10553 maybe_process_partial_specialization to work
10554 correctly, but is not accurate in this case;
10555 the TAG is not an instantiation of anything.
10556 (The corresponding TEMPLATE_DECL is an
10557 instantiation, but the TYPE is not.) */
10558 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10559
10560 /* Now, we call pushtag to put this NEWTAG into the scope of
10561 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10562 pushtag calling push_template_decl. We don't have to do
10563 this for enums because it will already have been done in
10564 tsubst_enum. */
10565 if (name)
10566 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10567 pushtag (name, newtag, /*tag_scope=*/ts_current);
10568 }
10569 }
10570 else if (DECL_DECLARES_FUNCTION_P (t))
10571 {
10572 tree r;
10573
10574 if (TREE_CODE (t) == TEMPLATE_DECL)
10575 ++processing_template_decl;
10576 r = tsubst (t, args, tf_error, NULL_TREE);
10577 if (TREE_CODE (t) == TEMPLATE_DECL)
10578 --processing_template_decl;
10579 set_current_access_from_decl (r);
10580 finish_member_declaration (r);
10581 /* Instantiate members marked with attribute used. */
10582 if (r != error_mark_node && DECL_PRESERVE_P (r))
10583 mark_used (r);
10584 if (TREE_CODE (r) == FUNCTION_DECL
10585 && DECL_OMP_DECLARE_REDUCTION_P (r))
10586 cp_check_omp_declare_reduction (r);
10587 }
10588 else if (DECL_CLASS_TEMPLATE_P (t)
10589 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10590 /* A closure type for a lambda in a default argument for a
10591 member template. Ignore it; it will be instantiated with
10592 the default argument. */;
10593 else
10594 {
10595 /* Build new TYPE_FIELDS. */
10596 if (TREE_CODE (t) == STATIC_ASSERT)
10597 {
10598 tree condition;
10599
10600 ++c_inhibit_evaluation_warnings;
10601 condition =
10602 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10603 tf_warning_or_error, NULL_TREE,
10604 /*integral_constant_expression_p=*/true);
10605 --c_inhibit_evaluation_warnings;
10606
10607 finish_static_assert (condition,
10608 STATIC_ASSERT_MESSAGE (t),
10609 STATIC_ASSERT_SOURCE_LOCATION (t),
10610 /*member_p=*/true);
10611 }
10612 else if (TREE_CODE (t) != CONST_DECL)
10613 {
10614 tree r;
10615 tree vec = NULL_TREE;
10616 int len = 1;
10617
10618 /* The file and line for this declaration, to
10619 assist in error message reporting. Since we
10620 called push_tinst_level above, we don't need to
10621 restore these. */
10622 input_location = DECL_SOURCE_LOCATION (t);
10623
10624 if (TREE_CODE (t) == TEMPLATE_DECL)
10625 ++processing_template_decl;
10626 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10627 if (TREE_CODE (t) == TEMPLATE_DECL)
10628 --processing_template_decl;
10629
10630 if (TREE_CODE (r) == TREE_VEC)
10631 {
10632 /* A capture pack became multiple fields. */
10633 vec = r;
10634 len = TREE_VEC_LENGTH (vec);
10635 }
10636
10637 for (int i = 0; i < len; ++i)
10638 {
10639 if (vec)
10640 r = TREE_VEC_ELT (vec, i);
10641 if (VAR_P (r))
10642 {
10643 /* In [temp.inst]:
10644
10645 [t]he initialization (and any associated
10646 side-effects) of a static data member does
10647 not occur unless the static data member is
10648 itself used in a way that requires the
10649 definition of the static data member to
10650 exist.
10651
10652 Therefore, we do not substitute into the
10653 initialized for the static data member here. */
10654 finish_static_data_member_decl
10655 (r,
10656 /*init=*/NULL_TREE,
10657 /*init_const_expr_p=*/false,
10658 /*asmspec_tree=*/NULL_TREE,
10659 /*flags=*/0);
10660 /* Instantiate members marked with attribute used. */
10661 if (r != error_mark_node && DECL_PRESERVE_P (r))
10662 mark_used (r);
10663 }
10664 else if (TREE_CODE (r) == FIELD_DECL)
10665 {
10666 /* Determine whether R has a valid type and can be
10667 completed later. If R is invalid, then its type
10668 is replaced by error_mark_node. */
10669 tree rtype = TREE_TYPE (r);
10670 if (can_complete_type_without_circularity (rtype))
10671 complete_type (rtype);
10672
10673 if (!complete_or_array_type_p (rtype))
10674 {
10675 /* If R's type couldn't be completed and
10676 it isn't a flexible array member (whose
10677 type is incomplete by definition) give
10678 an error. */
10679 cxx_incomplete_type_error (r, rtype);
10680 TREE_TYPE (r) = error_mark_node;
10681 }
10682 }
10683
10684 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10685 such a thing will already have been added to the field
10686 list by tsubst_enum in finish_member_declaration in the
10687 CLASSTYPE_NESTED_UTDS case above. */
10688 if (!(TREE_CODE (r) == TYPE_DECL
10689 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10690 && DECL_ARTIFICIAL (r)))
10691 {
10692 set_current_access_from_decl (r);
10693 finish_member_declaration (r);
10694 }
10695 }
10696 }
10697 }
10698 }
10699 else
10700 {
10701 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10702 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10703 {
10704 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10705
10706 tree friend_type = t;
10707 bool adjust_processing_template_decl = false;
10708
10709 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10710 {
10711 /* template <class T> friend class C; */
10712 friend_type = tsubst_friend_class (friend_type, args);
10713 adjust_processing_template_decl = true;
10714 }
10715 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10716 {
10717 /* template <class T> friend class C::D; */
10718 friend_type = tsubst (friend_type, args,
10719 tf_warning_or_error, NULL_TREE);
10720 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10721 friend_type = TREE_TYPE (friend_type);
10722 adjust_processing_template_decl = true;
10723 }
10724 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10725 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10726 {
10727 /* This could be either
10728
10729 friend class T::C;
10730
10731 when dependent_type_p is false or
10732
10733 template <class U> friend class T::C;
10734
10735 otherwise. */
10736 /* Bump processing_template_decl in case this is something like
10737 template <class T> friend struct A<T>::B. */
10738 ++processing_template_decl;
10739 friend_type = tsubst (friend_type, args,
10740 tf_warning_or_error, NULL_TREE);
10741 if (dependent_type_p (friend_type))
10742 adjust_processing_template_decl = true;
10743 --processing_template_decl;
10744 }
10745 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10746 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10747 && TYPE_HIDDEN_P (friend_type))
10748 {
10749 /* friend class C;
10750
10751 where C hasn't been declared yet. Let's lookup name
10752 from namespace scope directly, bypassing any name that
10753 come from dependent base class. */
10754 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10755
10756 /* The call to xref_tag_from_type does injection for friend
10757 classes. */
10758 push_nested_namespace (ns);
10759 friend_type =
10760 xref_tag_from_type (friend_type, NULL_TREE,
10761 /*tag_scope=*/ts_current);
10762 pop_nested_namespace (ns);
10763 }
10764 else if (uses_template_parms (friend_type))
10765 /* friend class C<T>; */
10766 friend_type = tsubst (friend_type, args,
10767 tf_warning_or_error, NULL_TREE);
10768 /* Otherwise it's
10769
10770 friend class C;
10771
10772 where C is already declared or
10773
10774 friend class C<int>;
10775
10776 We don't have to do anything in these cases. */
10777
10778 if (adjust_processing_template_decl)
10779 /* Trick make_friend_class into realizing that the friend
10780 we're adding is a template, not an ordinary class. It's
10781 important that we use make_friend_class since it will
10782 perform some error-checking and output cross-reference
10783 information. */
10784 ++processing_template_decl;
10785
10786 if (friend_type != error_mark_node)
10787 make_friend_class (type, friend_type, /*complain=*/false);
10788
10789 if (adjust_processing_template_decl)
10790 --processing_template_decl;
10791 }
10792 else
10793 {
10794 /* Build new DECL_FRIENDLIST. */
10795 tree r;
10796
10797 /* The file and line for this declaration, to
10798 assist in error message reporting. Since we
10799 called push_tinst_level above, we don't need to
10800 restore these. */
10801 input_location = DECL_SOURCE_LOCATION (t);
10802
10803 if (TREE_CODE (t) == TEMPLATE_DECL)
10804 {
10805 ++processing_template_decl;
10806 push_deferring_access_checks (dk_no_check);
10807 }
10808
10809 r = tsubst_friend_function (t, args);
10810 add_friend (type, r, /*complain=*/false);
10811 if (TREE_CODE (t) == TEMPLATE_DECL)
10812 {
10813 pop_deferring_access_checks ();
10814 --processing_template_decl;
10815 }
10816 }
10817 }
10818 }
10819
10820 if (fn_context)
10821 {
10822 /* Restore these before substituting into the lambda capture
10823 initializers. */
10824 cp_unevaluated_operand = saved_unevaluated_operand;
10825 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10826 }
10827
10828 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10829 {
10830 tree decl = lambda_function (type);
10831 if (decl)
10832 {
10833 if (cxx_dialect >= cxx17)
10834 CLASSTYPE_LITERAL_P (type) = true;
10835
10836 if (!DECL_TEMPLATE_INFO (decl)
10837 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10838 {
10839 /* Set function_depth to avoid garbage collection. */
10840 ++function_depth;
10841 instantiate_decl (decl, /*defer_ok=*/false, false);
10842 --function_depth;
10843 }
10844
10845 /* We need to instantiate the capture list from the template
10846 after we've instantiated the closure members, but before we
10847 consider adding the conversion op. Also keep any captures
10848 that may have been added during instantiation of the op(). */
10849 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10850 tree tmpl_cap
10851 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10852 args, tf_warning_or_error, NULL_TREE,
10853 false, false);
10854
10855 LAMBDA_EXPR_CAPTURE_LIST (expr)
10856 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10857
10858 maybe_add_lambda_conv_op (type);
10859 }
10860 else
10861 gcc_assert (errorcount);
10862 }
10863
10864 /* Set the file and line number information to whatever is given for
10865 the class itself. This puts error messages involving generated
10866 implicit functions at a predictable point, and the same point
10867 that would be used for non-template classes. */
10868 input_location = DECL_SOURCE_LOCATION (typedecl);
10869
10870 unreverse_member_declarations (type);
10871 finish_struct_1 (type);
10872 TYPE_BEING_DEFINED (type) = 0;
10873
10874 /* We don't instantiate default arguments for member functions. 14.7.1:
10875
10876 The implicit instantiation of a class template specialization causes
10877 the implicit instantiation of the declarations, but not of the
10878 definitions or default arguments, of the class member functions,
10879 member classes, static data members and member templates.... */
10880
10881 /* Some typedefs referenced from within the template code need to be access
10882 checked at template instantiation time, i.e now. These types were
10883 added to the template at parsing time. Let's get those and perform
10884 the access checks then. */
10885 perform_typedefs_access_check (pattern, args);
10886 perform_deferred_access_checks (tf_warning_or_error);
10887 pop_nested_class ();
10888 maximum_field_alignment = saved_maximum_field_alignment;
10889 if (!fn_context)
10890 pop_from_top_level ();
10891 pop_deferring_access_checks ();
10892 pop_tinst_level ();
10893
10894 /* The vtable for a template class can be emitted in any translation
10895 unit in which the class is instantiated. When there is no key
10896 method, however, finish_struct_1 will already have added TYPE to
10897 the keyed_classes. */
10898 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10899 vec_safe_push (keyed_classes, type);
10900
10901 return type;
10902 }
10903
10904 /* Wrapper for instantiate_class_template_1. */
10905
10906 tree
10907 instantiate_class_template (tree type)
10908 {
10909 tree ret;
10910 timevar_push (TV_TEMPLATE_INST);
10911 ret = instantiate_class_template_1 (type);
10912 timevar_pop (TV_TEMPLATE_INST);
10913 return ret;
10914 }
10915
10916 static tree
10917 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10918 {
10919 tree r;
10920
10921 if (!t)
10922 r = t;
10923 else if (TYPE_P (t))
10924 r = tsubst (t, args, complain, in_decl);
10925 else
10926 {
10927 if (!(complain & tf_warning))
10928 ++c_inhibit_evaluation_warnings;
10929 r = tsubst_expr (t, args, complain, in_decl,
10930 /*integral_constant_expression_p=*/true);
10931 if (!(complain & tf_warning))
10932 --c_inhibit_evaluation_warnings;
10933 }
10934 return r;
10935 }
10936
10937 /* Given a function parameter pack TMPL_PARM and some function parameters
10938 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10939 and set *SPEC_P to point at the next point in the list. */
10940
10941 tree
10942 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10943 {
10944 /* Collect all of the extra "packed" parameters into an
10945 argument pack. */
10946 tree parmvec;
10947 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10948 tree spec_parm = *spec_p;
10949 int i, len;
10950
10951 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10952 if (tmpl_parm
10953 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10954 break;
10955
10956 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10957 parmvec = make_tree_vec (len);
10958 spec_parm = *spec_p;
10959 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10960 TREE_VEC_ELT (parmvec, i) = spec_parm;
10961
10962 /* Build the argument packs. */
10963 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10964 *spec_p = spec_parm;
10965
10966 return argpack;
10967 }
10968
10969 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10970 NONTYPE_ARGUMENT_PACK. */
10971
10972 static tree
10973 make_fnparm_pack (tree spec_parm)
10974 {
10975 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10976 }
10977
10978 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10979 pack expansion with no extra args, 2 if it has extra args, or 0
10980 if it is not a pack expansion. */
10981
10982 static int
10983 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10984 {
10985 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10986 if (i >= TREE_VEC_LENGTH (vec))
10987 return 0;
10988 tree elt = TREE_VEC_ELT (vec, i);
10989 if (DECL_P (elt))
10990 /* A decl pack is itself an expansion. */
10991 elt = TREE_TYPE (elt);
10992 if (!PACK_EXPANSION_P (elt))
10993 return 0;
10994 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10995 return 2;
10996 return 1;
10997 }
10998
10999
11000 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11001
11002 static tree
11003 make_argument_pack_select (tree arg_pack, unsigned index)
11004 {
11005 tree aps = make_node (ARGUMENT_PACK_SELECT);
11006
11007 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11008 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11009
11010 return aps;
11011 }
11012
11013 /* This is a subroutine of tsubst_pack_expansion.
11014
11015 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11016 mechanism to store the (non complete list of) arguments of the
11017 substitution and return a non substituted pack expansion, in order
11018 to wait for when we have enough arguments to really perform the
11019 substitution. */
11020
11021 static bool
11022 use_pack_expansion_extra_args_p (tree parm_packs,
11023 int arg_pack_len,
11024 bool has_empty_arg)
11025 {
11026 /* If one pack has an expansion and another pack has a normal
11027 argument or if one pack has an empty argument and an another
11028 one hasn't then tsubst_pack_expansion cannot perform the
11029 substitution and need to fall back on the
11030 PACK_EXPANSION_EXTRA mechanism. */
11031 if (parm_packs == NULL_TREE)
11032 return false;
11033 else if (has_empty_arg)
11034 return true;
11035
11036 bool has_expansion_arg = false;
11037 for (int i = 0 ; i < arg_pack_len; ++i)
11038 {
11039 bool has_non_expansion_arg = false;
11040 for (tree parm_pack = parm_packs;
11041 parm_pack;
11042 parm_pack = TREE_CHAIN (parm_pack))
11043 {
11044 tree arg = TREE_VALUE (parm_pack);
11045
11046 int exp = argument_pack_element_is_expansion_p (arg, i);
11047 if (exp == 2)
11048 /* We can't substitute a pack expansion with extra args into
11049 our pattern. */
11050 return true;
11051 else if (exp)
11052 has_expansion_arg = true;
11053 else
11054 has_non_expansion_arg = true;
11055 }
11056
11057 if (has_expansion_arg && has_non_expansion_arg)
11058 return true;
11059 }
11060 return false;
11061 }
11062
11063 /* [temp.variadic]/6 says that:
11064
11065 The instantiation of a pack expansion [...]
11066 produces a list E1,E2, ..., En, where N is the number of elements
11067 in the pack expansion parameters.
11068
11069 This subroutine of tsubst_pack_expansion produces one of these Ei.
11070
11071 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11072 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11073 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11074 INDEX is the index 'i' of the element Ei to produce. ARGS,
11075 COMPLAIN, and IN_DECL are the same parameters as for the
11076 tsubst_pack_expansion function.
11077
11078 The function returns the resulting Ei upon successful completion,
11079 or error_mark_node.
11080
11081 Note that this function possibly modifies the ARGS parameter, so
11082 it's the responsibility of the caller to restore it. */
11083
11084 static tree
11085 gen_elem_of_pack_expansion_instantiation (tree pattern,
11086 tree parm_packs,
11087 unsigned index,
11088 tree args /* This parm gets
11089 modified. */,
11090 tsubst_flags_t complain,
11091 tree in_decl)
11092 {
11093 tree t;
11094 bool ith_elem_is_expansion = false;
11095
11096 /* For each parameter pack, change the substitution of the parameter
11097 pack to the ith argument in its argument pack, then expand the
11098 pattern. */
11099 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11100 {
11101 tree parm = TREE_PURPOSE (pack);
11102 tree arg_pack = TREE_VALUE (pack);
11103 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11104
11105 ith_elem_is_expansion |=
11106 argument_pack_element_is_expansion_p (arg_pack, index);
11107
11108 /* Select the Ith argument from the pack. */
11109 if (TREE_CODE (parm) == PARM_DECL
11110 || TREE_CODE (parm) == FIELD_DECL)
11111 {
11112 if (index == 0)
11113 {
11114 aps = make_argument_pack_select (arg_pack, index);
11115 if (!mark_used (parm, complain) && !(complain & tf_error))
11116 return error_mark_node;
11117 register_local_specialization (aps, parm);
11118 }
11119 else
11120 aps = retrieve_local_specialization (parm);
11121 }
11122 else
11123 {
11124 int idx, level;
11125 template_parm_level_and_index (parm, &level, &idx);
11126
11127 if (index == 0)
11128 {
11129 aps = make_argument_pack_select (arg_pack, index);
11130 /* Update the corresponding argument. */
11131 TMPL_ARG (args, level, idx) = aps;
11132 }
11133 else
11134 /* Re-use the ARGUMENT_PACK_SELECT. */
11135 aps = TMPL_ARG (args, level, idx);
11136 }
11137 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11138 }
11139
11140 /* Substitute into the PATTERN with the (possibly altered)
11141 arguments. */
11142 if (pattern == in_decl)
11143 /* Expanding a fixed parameter pack from
11144 coerce_template_parameter_pack. */
11145 t = tsubst_decl (pattern, args, complain);
11146 else if (pattern == error_mark_node)
11147 t = error_mark_node;
11148 else if (constraint_p (pattern))
11149 {
11150 if (processing_template_decl)
11151 t = tsubst_constraint (pattern, args, complain, in_decl);
11152 else
11153 t = (constraints_satisfied_p (pattern, args)
11154 ? boolean_true_node : boolean_false_node);
11155 }
11156 else if (!TYPE_P (pattern))
11157 t = tsubst_expr (pattern, args, complain, in_decl,
11158 /*integral_constant_expression_p=*/false);
11159 else
11160 t = tsubst (pattern, args, complain, in_decl);
11161
11162 /* If the Ith argument pack element is a pack expansion, then
11163 the Ith element resulting from the substituting is going to
11164 be a pack expansion as well. */
11165 if (ith_elem_is_expansion)
11166 t = make_pack_expansion (t);
11167
11168 return t;
11169 }
11170
11171 /* When the unexpanded parameter pack in a fold expression expands to an empty
11172 sequence, the value of the expression is as follows; the program is
11173 ill-formed if the operator is not listed in this table.
11174
11175 && true
11176 || false
11177 , void() */
11178
11179 tree
11180 expand_empty_fold (tree t, tsubst_flags_t complain)
11181 {
11182 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11183 if (!FOLD_EXPR_MODIFY_P (t))
11184 switch (code)
11185 {
11186 case TRUTH_ANDIF_EXPR:
11187 return boolean_true_node;
11188 case TRUTH_ORIF_EXPR:
11189 return boolean_false_node;
11190 case COMPOUND_EXPR:
11191 return void_node;
11192 default:
11193 break;
11194 }
11195
11196 if (complain & tf_error)
11197 error_at (location_of (t),
11198 "fold of empty expansion over %O", code);
11199 return error_mark_node;
11200 }
11201
11202 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11203 form an expression that combines the two terms using the
11204 operator of T. */
11205
11206 static tree
11207 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11208 {
11209 tree op = FOLD_EXPR_OP (t);
11210 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11211
11212 // Handle compound assignment operators.
11213 if (FOLD_EXPR_MODIFY_P (t))
11214 return build_x_modify_expr (input_location, left, code, right, complain);
11215
11216 switch (code)
11217 {
11218 case COMPOUND_EXPR:
11219 return build_x_compound_expr (input_location, left, right, complain);
11220 case DOTSTAR_EXPR:
11221 return build_m_component_ref (left, right, complain);
11222 default:
11223 return build_x_binary_op (input_location, code,
11224 left, TREE_CODE (left),
11225 right, TREE_CODE (right),
11226 /*overload=*/NULL,
11227 complain);
11228 }
11229 }
11230
11231 /* Substitute ARGS into the pack of a fold expression T. */
11232
11233 static inline tree
11234 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11235 {
11236 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11237 }
11238
11239 /* Substitute ARGS into the pack of a fold expression T. */
11240
11241 static inline tree
11242 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11243 {
11244 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11245 }
11246
11247 /* Expand a PACK of arguments into a grouped as left fold.
11248 Given a pack containing elements A0, A1, ..., An and an
11249 operator @, this builds the expression:
11250
11251 ((A0 @ A1) @ A2) ... @ An
11252
11253 Note that PACK must not be empty.
11254
11255 The operator is defined by the original fold expression T. */
11256
11257 static tree
11258 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11259 {
11260 tree left = TREE_VEC_ELT (pack, 0);
11261 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11262 {
11263 tree right = TREE_VEC_ELT (pack, i);
11264 left = fold_expression (t, left, right, complain);
11265 }
11266 return left;
11267 }
11268
11269 /* Substitute into a unary left fold expression. */
11270
11271 static tree
11272 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11273 tree in_decl)
11274 {
11275 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11276 if (pack == error_mark_node)
11277 return error_mark_node;
11278 if (PACK_EXPANSION_P (pack))
11279 {
11280 tree r = copy_node (t);
11281 FOLD_EXPR_PACK (r) = pack;
11282 return r;
11283 }
11284 if (TREE_VEC_LENGTH (pack) == 0)
11285 return expand_empty_fold (t, complain);
11286 else
11287 return expand_left_fold (t, pack, complain);
11288 }
11289
11290 /* Substitute into a binary left fold expression.
11291
11292 Do ths by building a single (non-empty) vector of argumnts and
11293 building the expression from those elements. */
11294
11295 static tree
11296 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11297 tree in_decl)
11298 {
11299 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11300 if (pack == error_mark_node)
11301 return error_mark_node;
11302 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11303 if (init == error_mark_node)
11304 return error_mark_node;
11305
11306 if (PACK_EXPANSION_P (pack))
11307 {
11308 tree r = copy_node (t);
11309 FOLD_EXPR_PACK (r) = pack;
11310 FOLD_EXPR_INIT (r) = init;
11311 return r;
11312 }
11313
11314 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11315 TREE_VEC_ELT (vec, 0) = init;
11316 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11317 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11318
11319 return expand_left_fold (t, vec, complain);
11320 }
11321
11322 /* Expand a PACK of arguments into a grouped as right fold.
11323 Given a pack containing elementns A0, A1, ..., and an
11324 operator @, this builds the expression:
11325
11326 A0@ ... (An-2 @ (An-1 @ An))
11327
11328 Note that PACK must not be empty.
11329
11330 The operator is defined by the original fold expression T. */
11331
11332 tree
11333 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11334 {
11335 // Build the expression.
11336 int n = TREE_VEC_LENGTH (pack);
11337 tree right = TREE_VEC_ELT (pack, n - 1);
11338 for (--n; n != 0; --n)
11339 {
11340 tree left = TREE_VEC_ELT (pack, n - 1);
11341 right = fold_expression (t, left, right, complain);
11342 }
11343 return right;
11344 }
11345
11346 /* Substitute into a unary right fold expression. */
11347
11348 static tree
11349 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11350 tree in_decl)
11351 {
11352 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11353 if (pack == error_mark_node)
11354 return error_mark_node;
11355 if (PACK_EXPANSION_P (pack))
11356 {
11357 tree r = copy_node (t);
11358 FOLD_EXPR_PACK (r) = pack;
11359 return r;
11360 }
11361 if (TREE_VEC_LENGTH (pack) == 0)
11362 return expand_empty_fold (t, complain);
11363 else
11364 return expand_right_fold (t, pack, complain);
11365 }
11366
11367 /* Substitute into a binary right fold expression.
11368
11369 Do ths by building a single (non-empty) vector of arguments and
11370 building the expression from those elements. */
11371
11372 static tree
11373 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11374 tree in_decl)
11375 {
11376 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11377 if (pack == error_mark_node)
11378 return error_mark_node;
11379 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11380 if (init == error_mark_node)
11381 return error_mark_node;
11382
11383 if (PACK_EXPANSION_P (pack))
11384 {
11385 tree r = copy_node (t);
11386 FOLD_EXPR_PACK (r) = pack;
11387 FOLD_EXPR_INIT (r) = init;
11388 return r;
11389 }
11390
11391 int n = TREE_VEC_LENGTH (pack);
11392 tree vec = make_tree_vec (n + 1);
11393 for (int i = 0; i < n; ++i)
11394 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11395 TREE_VEC_ELT (vec, n) = init;
11396
11397 return expand_right_fold (t, vec, complain);
11398 }
11399
11400
11401 /* Substitute ARGS into T, which is an pack expansion
11402 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11403 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11404 (if only a partial substitution could be performed) or
11405 ERROR_MARK_NODE if there was an error. */
11406 tree
11407 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11408 tree in_decl)
11409 {
11410 tree pattern;
11411 tree pack, packs = NULL_TREE;
11412 bool unsubstituted_packs = false;
11413 int i, len = -1;
11414 tree result;
11415 hash_map<tree, tree> *saved_local_specializations = NULL;
11416 bool need_local_specializations = false;
11417 int levels;
11418
11419 gcc_assert (PACK_EXPANSION_P (t));
11420 pattern = PACK_EXPANSION_PATTERN (t);
11421
11422 /* Add in any args remembered from an earlier partial instantiation. */
11423 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11424
11425 levels = TMPL_ARGS_DEPTH (args);
11426
11427 /* Determine the argument packs that will instantiate the parameter
11428 packs used in the expansion expression. While we're at it,
11429 compute the number of arguments to be expanded and make sure it
11430 is consistent. */
11431 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11432 pack = TREE_CHAIN (pack))
11433 {
11434 tree parm_pack = TREE_VALUE (pack);
11435 tree arg_pack = NULL_TREE;
11436 tree orig_arg = NULL_TREE;
11437 int level = 0;
11438
11439 if (TREE_CODE (parm_pack) == BASES)
11440 {
11441 gcc_assert (parm_pack == pattern);
11442 if (BASES_DIRECT (parm_pack))
11443 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11444 args, complain, in_decl, false));
11445 else
11446 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11447 args, complain, in_decl, false));
11448 }
11449 else if (builtin_pack_call_p (parm_pack))
11450 {
11451 /* ??? Support use in other patterns. */
11452 gcc_assert (parm_pack == pattern);
11453 return expand_builtin_pack_call (parm_pack, args,
11454 complain, in_decl);
11455 }
11456 else if (TREE_CODE (parm_pack) == PARM_DECL)
11457 {
11458 /* We know we have correct local_specializations if this
11459 expansion is at function scope, or if we're dealing with a
11460 local parameter in a requires expression; for the latter,
11461 tsubst_requires_expr set it up appropriately. */
11462 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11463 arg_pack = retrieve_local_specialization (parm_pack);
11464 else
11465 /* We can't rely on local_specializations for a parameter
11466 name used later in a function declaration (such as in a
11467 late-specified return type). Even if it exists, it might
11468 have the wrong value for a recursive call. */
11469 need_local_specializations = true;
11470
11471 if (!arg_pack)
11472 {
11473 /* This parameter pack was used in an unevaluated context. Just
11474 make a dummy decl, since it's only used for its type. */
11475 arg_pack = tsubst_decl (parm_pack, args, complain);
11476 if (arg_pack && DECL_PACK_P (arg_pack))
11477 /* Partial instantiation of the parm_pack, we can't build
11478 up an argument pack yet. */
11479 arg_pack = NULL_TREE;
11480 else
11481 arg_pack = make_fnparm_pack (arg_pack);
11482 }
11483 }
11484 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11485 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11486 else
11487 {
11488 int idx;
11489 template_parm_level_and_index (parm_pack, &level, &idx);
11490
11491 if (level <= levels)
11492 arg_pack = TMPL_ARG (args, level, idx);
11493 }
11494
11495 orig_arg = arg_pack;
11496 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11497 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11498
11499 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11500 /* This can only happen if we forget to expand an argument
11501 pack somewhere else. Just return an error, silently. */
11502 {
11503 result = make_tree_vec (1);
11504 TREE_VEC_ELT (result, 0) = error_mark_node;
11505 return result;
11506 }
11507
11508 if (arg_pack)
11509 {
11510 int my_len =
11511 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11512
11513 /* Don't bother trying to do a partial substitution with
11514 incomplete packs; we'll try again after deduction. */
11515 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11516 return t;
11517
11518 if (len < 0)
11519 len = my_len;
11520 else if (len != my_len)
11521 {
11522 if (!(complain & tf_error))
11523 /* Fail quietly. */;
11524 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11525 error ("mismatched argument pack lengths while expanding %qT",
11526 pattern);
11527 else
11528 error ("mismatched argument pack lengths while expanding %qE",
11529 pattern);
11530 return error_mark_node;
11531 }
11532
11533 /* Keep track of the parameter packs and their corresponding
11534 argument packs. */
11535 packs = tree_cons (parm_pack, arg_pack, packs);
11536 TREE_TYPE (packs) = orig_arg;
11537 }
11538 else
11539 {
11540 /* We can't substitute for this parameter pack. We use a flag as
11541 well as the missing_level counter because function parameter
11542 packs don't have a level. */
11543 gcc_assert (processing_template_decl);
11544 unsubstituted_packs = true;
11545 }
11546 }
11547
11548 /* If the expansion is just T..., return the matching argument pack, unless
11549 we need to call convert_from_reference on all the elements. This is an
11550 important optimization; see c++/68422. */
11551 if (!unsubstituted_packs
11552 && TREE_PURPOSE (packs) == pattern)
11553 {
11554 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11555 /* Types need no adjustment, nor does sizeof..., and if we still have
11556 some pack expansion args we won't do anything yet. */
11557 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11558 || PACK_EXPANSION_SIZEOF_P (t)
11559 || pack_expansion_args_count (args))
11560 return args;
11561 /* Also optimize expression pack expansions if we can tell that the
11562 elements won't have reference type. */
11563 tree type = TREE_TYPE (pattern);
11564 if (type && TREE_CODE (type) != REFERENCE_TYPE
11565 && !PACK_EXPANSION_P (type)
11566 && !WILDCARD_TYPE_P (type))
11567 return args;
11568 /* Otherwise use the normal path so we get convert_from_reference. */
11569 }
11570
11571 /* We cannot expand this expansion expression, because we don't have
11572 all of the argument packs we need. */
11573 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11574 {
11575 /* We got some full packs, but we can't substitute them in until we
11576 have values for all the packs. So remember these until then. */
11577
11578 t = make_pack_expansion (pattern);
11579 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11580 return t;
11581 }
11582 else if (unsubstituted_packs)
11583 {
11584 /* There were no real arguments, we're just replacing a parameter
11585 pack with another version of itself. Substitute into the
11586 pattern and return a PACK_EXPANSION_*. The caller will need to
11587 deal with that. */
11588 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11589 t = tsubst_expr (pattern, args, complain, in_decl,
11590 /*integral_constant_expression_p=*/false);
11591 else
11592 t = tsubst (pattern, args, complain, in_decl);
11593 t = make_pack_expansion (t);
11594 return t;
11595 }
11596
11597 gcc_assert (len >= 0);
11598
11599 if (need_local_specializations)
11600 {
11601 /* We're in a late-specified return type, so create our own local
11602 specializations map; the current map is either NULL or (in the
11603 case of recursive unification) might have bindings that we don't
11604 want to use or alter. */
11605 saved_local_specializations = local_specializations;
11606 local_specializations = new hash_map<tree, tree>;
11607 }
11608
11609 /* For each argument in each argument pack, substitute into the
11610 pattern. */
11611 result = make_tree_vec (len);
11612 tree elem_args = copy_template_args (args);
11613 for (i = 0; i < len; ++i)
11614 {
11615 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11616 i,
11617 elem_args, complain,
11618 in_decl);
11619 TREE_VEC_ELT (result, i) = t;
11620 if (t == error_mark_node)
11621 {
11622 result = error_mark_node;
11623 break;
11624 }
11625 }
11626
11627 /* Update ARGS to restore the substitution from parameter packs to
11628 their argument packs. */
11629 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11630 {
11631 tree parm = TREE_PURPOSE (pack);
11632
11633 if (TREE_CODE (parm) == PARM_DECL
11634 || TREE_CODE (parm) == FIELD_DECL)
11635 register_local_specialization (TREE_TYPE (pack), parm);
11636 else
11637 {
11638 int idx, level;
11639
11640 if (TREE_VALUE (pack) == NULL_TREE)
11641 continue;
11642
11643 template_parm_level_and_index (parm, &level, &idx);
11644
11645 /* Update the corresponding argument. */
11646 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11647 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11648 TREE_TYPE (pack);
11649 else
11650 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11651 }
11652 }
11653
11654 if (need_local_specializations)
11655 {
11656 delete local_specializations;
11657 local_specializations = saved_local_specializations;
11658 }
11659
11660 /* If the dependent pack arguments were such that we end up with only a
11661 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11662 if (len == 1 && TREE_CODE (result) == TREE_VEC
11663 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11664 return TREE_VEC_ELT (result, 0);
11665
11666 return result;
11667 }
11668
11669 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11670 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11671 parameter packs; all parms generated from a function parameter pack will
11672 have the same DECL_PARM_INDEX. */
11673
11674 tree
11675 get_pattern_parm (tree parm, tree tmpl)
11676 {
11677 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11678 tree patparm;
11679
11680 if (DECL_ARTIFICIAL (parm))
11681 {
11682 for (patparm = DECL_ARGUMENTS (pattern);
11683 patparm; patparm = DECL_CHAIN (patparm))
11684 if (DECL_ARTIFICIAL (patparm)
11685 && DECL_NAME (parm) == DECL_NAME (patparm))
11686 break;
11687 }
11688 else
11689 {
11690 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11691 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11692 gcc_assert (DECL_PARM_INDEX (patparm)
11693 == DECL_PARM_INDEX (parm));
11694 }
11695
11696 return patparm;
11697 }
11698
11699 /* Make an argument pack out of the TREE_VEC VEC. */
11700
11701 static tree
11702 make_argument_pack (tree vec)
11703 {
11704 tree pack;
11705 tree elt = TREE_VEC_ELT (vec, 0);
11706 if (TYPE_P (elt))
11707 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11708 else
11709 {
11710 pack = make_node (NONTYPE_ARGUMENT_PACK);
11711 TREE_CONSTANT (pack) = 1;
11712 }
11713 SET_ARGUMENT_PACK_ARGS (pack, vec);
11714 return pack;
11715 }
11716
11717 /* Return an exact copy of template args T that can be modified
11718 independently. */
11719
11720 static tree
11721 copy_template_args (tree t)
11722 {
11723 if (t == error_mark_node)
11724 return t;
11725
11726 int len = TREE_VEC_LENGTH (t);
11727 tree new_vec = make_tree_vec (len);
11728
11729 for (int i = 0; i < len; ++i)
11730 {
11731 tree elt = TREE_VEC_ELT (t, i);
11732 if (elt && TREE_CODE (elt) == TREE_VEC)
11733 elt = copy_template_args (elt);
11734 TREE_VEC_ELT (new_vec, i) = elt;
11735 }
11736
11737 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11738 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11739
11740 return new_vec;
11741 }
11742
11743 /* Substitute ARGS into the vector or list of template arguments T. */
11744
11745 static tree
11746 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11747 {
11748 tree orig_t = t;
11749 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11750 tree *elts;
11751
11752 if (t == error_mark_node)
11753 return error_mark_node;
11754
11755 len = TREE_VEC_LENGTH (t);
11756 elts = XALLOCAVEC (tree, len);
11757
11758 for (i = 0; i < len; i++)
11759 {
11760 tree orig_arg = TREE_VEC_ELT (t, i);
11761 tree new_arg;
11762
11763 if (TREE_CODE (orig_arg) == TREE_VEC)
11764 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11765 else if (PACK_EXPANSION_P (orig_arg))
11766 {
11767 /* Substitute into an expansion expression. */
11768 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11769
11770 if (TREE_CODE (new_arg) == TREE_VEC)
11771 /* Add to the expanded length adjustment the number of
11772 expanded arguments. We subtract one from this
11773 measurement, because the argument pack expression
11774 itself is already counted as 1 in
11775 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11776 the argument pack is empty. */
11777 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11778 }
11779 else if (ARGUMENT_PACK_P (orig_arg))
11780 {
11781 /* Substitute into each of the arguments. */
11782 new_arg = TYPE_P (orig_arg)
11783 ? cxx_make_type (TREE_CODE (orig_arg))
11784 : make_node (TREE_CODE (orig_arg));
11785
11786 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11787 args, complain, in_decl);
11788 if (pack_args == error_mark_node)
11789 new_arg = error_mark_node;
11790 else
11791 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11792
11793 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11794 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11795 }
11796 else
11797 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11798
11799 if (new_arg == error_mark_node)
11800 return error_mark_node;
11801
11802 elts[i] = new_arg;
11803 if (new_arg != orig_arg)
11804 need_new = 1;
11805 }
11806
11807 if (!need_new)
11808 return t;
11809
11810 /* Make space for the expanded arguments coming from template
11811 argument packs. */
11812 t = make_tree_vec (len + expanded_len_adjust);
11813 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11814 arguments for a member template.
11815 In that case each TREE_VEC in ORIG_T represents a level of template
11816 arguments, and ORIG_T won't carry any non defaulted argument count.
11817 It will rather be the nested TREE_VECs that will carry one.
11818 In other words, ORIG_T carries a non defaulted argument count only
11819 if it doesn't contain any nested TREE_VEC. */
11820 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11821 {
11822 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11823 count += expanded_len_adjust;
11824 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11825 }
11826 for (i = 0, out = 0; i < len; i++)
11827 {
11828 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11829 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11830 && TREE_CODE (elts[i]) == TREE_VEC)
11831 {
11832 int idx;
11833
11834 /* Now expand the template argument pack "in place". */
11835 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11836 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11837 }
11838 else
11839 {
11840 TREE_VEC_ELT (t, out) = elts[i];
11841 out++;
11842 }
11843 }
11844
11845 return t;
11846 }
11847
11848 /* Substitute ARGS into one level PARMS of template parameters. */
11849
11850 static tree
11851 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11852 {
11853 if (parms == error_mark_node)
11854 return error_mark_node;
11855
11856 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11857
11858 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11859 {
11860 tree tuple = TREE_VEC_ELT (parms, i);
11861
11862 if (tuple == error_mark_node)
11863 continue;
11864
11865 TREE_VEC_ELT (new_vec, i) =
11866 tsubst_template_parm (tuple, args, complain);
11867 }
11868
11869 return new_vec;
11870 }
11871
11872 /* Return the result of substituting ARGS into the template parameters
11873 given by PARMS. If there are m levels of ARGS and m + n levels of
11874 PARMS, then the result will contain n levels of PARMS. For
11875 example, if PARMS is `template <class T> template <class U>
11876 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11877 result will be `template <int*, double, class V>'. */
11878
11879 static tree
11880 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11881 {
11882 tree r = NULL_TREE;
11883 tree* new_parms;
11884
11885 /* When substituting into a template, we must set
11886 PROCESSING_TEMPLATE_DECL as the template parameters may be
11887 dependent if they are based on one-another, and the dependency
11888 predicates are short-circuit outside of templates. */
11889 ++processing_template_decl;
11890
11891 for (new_parms = &r;
11892 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11893 new_parms = &(TREE_CHAIN (*new_parms)),
11894 parms = TREE_CHAIN (parms))
11895 {
11896 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11897 args, complain);
11898 *new_parms =
11899 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11900 - TMPL_ARGS_DEPTH (args)),
11901 new_vec, NULL_TREE);
11902 }
11903
11904 --processing_template_decl;
11905
11906 return r;
11907 }
11908
11909 /* Return the result of substituting ARGS into one template parameter
11910 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11911 parameter and which TREE_PURPOSE is the default argument of the
11912 template parameter. */
11913
11914 static tree
11915 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11916 {
11917 tree default_value, parm_decl;
11918
11919 if (args == NULL_TREE
11920 || t == NULL_TREE
11921 || t == error_mark_node)
11922 return t;
11923
11924 gcc_assert (TREE_CODE (t) == TREE_LIST);
11925
11926 default_value = TREE_PURPOSE (t);
11927 parm_decl = TREE_VALUE (t);
11928
11929 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11930 if (TREE_CODE (parm_decl) == PARM_DECL
11931 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11932 parm_decl = error_mark_node;
11933 default_value = tsubst_template_arg (default_value, args,
11934 complain, NULL_TREE);
11935
11936 return build_tree_list (default_value, parm_decl);
11937 }
11938
11939 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11940 type T. If T is not an aggregate or enumeration type, it is
11941 handled as if by tsubst. IN_DECL is as for tsubst. If
11942 ENTERING_SCOPE is nonzero, T is the context for a template which
11943 we are presently tsubst'ing. Return the substituted value. */
11944
11945 static tree
11946 tsubst_aggr_type (tree t,
11947 tree args,
11948 tsubst_flags_t complain,
11949 tree in_decl,
11950 int entering_scope)
11951 {
11952 if (t == NULL_TREE)
11953 return NULL_TREE;
11954
11955 switch (TREE_CODE (t))
11956 {
11957 case RECORD_TYPE:
11958 if (TYPE_PTRMEMFUNC_P (t))
11959 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11960
11961 /* Fall through. */
11962 case ENUMERAL_TYPE:
11963 case UNION_TYPE:
11964 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11965 {
11966 tree argvec;
11967 tree context;
11968 tree r;
11969 int saved_unevaluated_operand;
11970 int saved_inhibit_evaluation_warnings;
11971
11972 /* In "sizeof(X<I>)" we need to evaluate "I". */
11973 saved_unevaluated_operand = cp_unevaluated_operand;
11974 cp_unevaluated_operand = 0;
11975 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11976 c_inhibit_evaluation_warnings = 0;
11977
11978 /* First, determine the context for the type we are looking
11979 up. */
11980 context = TYPE_CONTEXT (t);
11981 if (context && TYPE_P (context))
11982 {
11983 context = tsubst_aggr_type (context, args, complain,
11984 in_decl, /*entering_scope=*/1);
11985 /* If context is a nested class inside a class template,
11986 it may still need to be instantiated (c++/33959). */
11987 context = complete_type (context);
11988 }
11989
11990 /* Then, figure out what arguments are appropriate for the
11991 type we are trying to find. For example, given:
11992
11993 template <class T> struct S;
11994 template <class T, class U> void f(T, U) { S<U> su; }
11995
11996 and supposing that we are instantiating f<int, double>,
11997 then our ARGS will be {int, double}, but, when looking up
11998 S we only want {double}. */
11999 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12000 complain, in_decl);
12001 if (argvec == error_mark_node)
12002 r = error_mark_node;
12003 else
12004 {
12005 r = lookup_template_class (t, argvec, in_decl, context,
12006 entering_scope, complain);
12007 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12008 }
12009
12010 cp_unevaluated_operand = saved_unevaluated_operand;
12011 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12012
12013 return r;
12014 }
12015 else
12016 /* This is not a template type, so there's nothing to do. */
12017 return t;
12018
12019 default:
12020 return tsubst (t, args, complain, in_decl);
12021 }
12022 }
12023
12024 static GTY(()) hash_map<tree, tree> *defarg_inst;
12025
12026 /* Substitute into the default argument ARG (a default argument for
12027 FN), which has the indicated TYPE. */
12028
12029 tree
12030 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12031 tsubst_flags_t complain)
12032 {
12033 tree saved_class_ptr = NULL_TREE;
12034 tree saved_class_ref = NULL_TREE;
12035 int errs = errorcount + sorrycount;
12036
12037 /* This can happen in invalid code. */
12038 if (TREE_CODE (arg) == DEFAULT_ARG)
12039 return arg;
12040
12041 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12042 parm = chain_index (parmnum, parm);
12043 tree parmtype = TREE_TYPE (parm);
12044 if (DECL_BY_REFERENCE (parm))
12045 parmtype = TREE_TYPE (parmtype);
12046 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12047
12048 tree *slot;
12049 if (defarg_inst && (slot = defarg_inst->get (parm)))
12050 return *slot;
12051
12052 /* This default argument came from a template. Instantiate the
12053 default argument here, not in tsubst. In the case of
12054 something like:
12055
12056 template <class T>
12057 struct S {
12058 static T t();
12059 void f(T = t());
12060 };
12061
12062 we must be careful to do name lookup in the scope of S<T>,
12063 rather than in the current class. */
12064 push_access_scope (fn);
12065 /* The "this" pointer is not valid in a default argument. */
12066 if (cfun)
12067 {
12068 saved_class_ptr = current_class_ptr;
12069 cp_function_chain->x_current_class_ptr = NULL_TREE;
12070 saved_class_ref = current_class_ref;
12071 cp_function_chain->x_current_class_ref = NULL_TREE;
12072 }
12073
12074 start_lambda_scope (parm);
12075
12076 push_deferring_access_checks(dk_no_deferred);
12077 /* The default argument expression may cause implicitly defined
12078 member functions to be synthesized, which will result in garbage
12079 collection. We must treat this situation as if we were within
12080 the body of function so as to avoid collecting live data on the
12081 stack. */
12082 ++function_depth;
12083 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12084 complain, NULL_TREE,
12085 /*integral_constant_expression_p=*/false);
12086 --function_depth;
12087 pop_deferring_access_checks();
12088
12089 finish_lambda_scope ();
12090
12091 /* Restore the "this" pointer. */
12092 if (cfun)
12093 {
12094 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12095 cp_function_chain->x_current_class_ref = saved_class_ref;
12096 }
12097
12098 if (errorcount+sorrycount > errs
12099 && (complain & tf_warning_or_error))
12100 inform (input_location,
12101 " when instantiating default argument for call to %qD", fn);
12102
12103 /* Make sure the default argument is reasonable. */
12104 arg = check_default_argument (type, arg, complain);
12105
12106 pop_access_scope (fn);
12107
12108 if (arg != error_mark_node && !cp_unevaluated_operand)
12109 {
12110 if (!defarg_inst)
12111 defarg_inst = hash_map<tree,tree>::create_ggc (37);
12112 defarg_inst->put (parm, arg);
12113 }
12114
12115 return arg;
12116 }
12117
12118 /* Substitute into all the default arguments for FN. */
12119
12120 static void
12121 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12122 {
12123 tree arg;
12124 tree tmpl_args;
12125
12126 tmpl_args = DECL_TI_ARGS (fn);
12127
12128 /* If this function is not yet instantiated, we certainly don't need
12129 its default arguments. */
12130 if (uses_template_parms (tmpl_args))
12131 return;
12132 /* Don't do this again for clones. */
12133 if (DECL_CLONED_FUNCTION_P (fn))
12134 return;
12135
12136 int i = 0;
12137 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12138 arg;
12139 arg = TREE_CHAIN (arg), ++i)
12140 if (TREE_PURPOSE (arg))
12141 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12142 TREE_VALUE (arg),
12143 TREE_PURPOSE (arg),
12144 complain);
12145 }
12146
12147 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12148
12149 static tree
12150 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12151 tree lambda_fntype)
12152 {
12153 tree gen_tmpl, argvec;
12154 hashval_t hash = 0;
12155 tree in_decl = t;
12156
12157 /* Nobody should be tsubst'ing into non-template functions. */
12158 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12159
12160 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12161 {
12162 /* If T is not dependent, just return it. */
12163 if (!uses_template_parms (DECL_TI_ARGS (t)))
12164 return t;
12165
12166 /* Calculate the most general template of which R is a
12167 specialization, and the complete set of arguments used to
12168 specialize R. */
12169 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12170 argvec = tsubst_template_args (DECL_TI_ARGS
12171 (DECL_TEMPLATE_RESULT
12172 (DECL_TI_TEMPLATE (t))),
12173 args, complain, in_decl);
12174 if (argvec == error_mark_node)
12175 return error_mark_node;
12176
12177 /* Check to see if we already have this specialization. */
12178 if (!lambda_fntype)
12179 {
12180 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12181 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12182 return spec;
12183 }
12184
12185 /* We can see more levels of arguments than parameters if
12186 there was a specialization of a member template, like
12187 this:
12188
12189 template <class T> struct S { template <class U> void f(); }
12190 template <> template <class U> void S<int>::f(U);
12191
12192 Here, we'll be substituting into the specialization,
12193 because that's where we can find the code we actually
12194 want to generate, but we'll have enough arguments for
12195 the most general template.
12196
12197 We also deal with the peculiar case:
12198
12199 template <class T> struct S {
12200 template <class U> friend void f();
12201 };
12202 template <class U> void f() {}
12203 template S<int>;
12204 template void f<double>();
12205
12206 Here, the ARGS for the instantiation of will be {int,
12207 double}. But, we only need as many ARGS as there are
12208 levels of template parameters in CODE_PATTERN. We are
12209 careful not to get fooled into reducing the ARGS in
12210 situations like:
12211
12212 template <class T> struct S { template <class U> void f(U); }
12213 template <class T> template <> void S<T>::f(int) {}
12214
12215 which we can spot because the pattern will be a
12216 specialization in this case. */
12217 int args_depth = TMPL_ARGS_DEPTH (args);
12218 int parms_depth =
12219 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12220
12221 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12222 args = get_innermost_template_args (args, parms_depth);
12223 }
12224 else
12225 {
12226 /* This special case arises when we have something like this:
12227
12228 template <class T> struct S {
12229 friend void f<int>(int, double);
12230 };
12231
12232 Here, the DECL_TI_TEMPLATE for the friend declaration
12233 will be an IDENTIFIER_NODE. We are being called from
12234 tsubst_friend_function, and we want only to create a
12235 new decl (R) with appropriate types so that we can call
12236 determine_specialization. */
12237 gen_tmpl = NULL_TREE;
12238 argvec = NULL_TREE;
12239 }
12240
12241 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12242 : NULL_TREE);
12243 tree ctx = closure ? closure : DECL_CONTEXT (t);
12244 bool member = ctx && TYPE_P (ctx);
12245
12246 if (member && !closure)
12247 ctx = tsubst_aggr_type (ctx, args,
12248 complain, t, /*entering_scope=*/1);
12249
12250 tree type = (lambda_fntype ? lambda_fntype
12251 : tsubst (TREE_TYPE (t), args,
12252 complain | tf_fndecl_type, in_decl));
12253 if (type == error_mark_node)
12254 return error_mark_node;
12255
12256 /* If we hit excessive deduction depth, the type is bogus even if
12257 it isn't error_mark_node, so don't build a decl. */
12258 if (excessive_deduction_depth)
12259 return error_mark_node;
12260
12261 /* We do NOT check for matching decls pushed separately at this
12262 point, as they may not represent instantiations of this
12263 template, and in any case are considered separate under the
12264 discrete model. */
12265 tree r = copy_decl (t);
12266 DECL_USE_TEMPLATE (r) = 0;
12267 TREE_TYPE (r) = type;
12268 /* Clear out the mangled name and RTL for the instantiation. */
12269 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12270 SET_DECL_RTL (r, NULL);
12271 /* Leave DECL_INITIAL set on deleted instantiations. */
12272 if (!DECL_DELETED_FN (r))
12273 DECL_INITIAL (r) = NULL_TREE;
12274 DECL_CONTEXT (r) = ctx;
12275
12276 /* OpenMP UDRs have the only argument a reference to the declared
12277 type. We want to diagnose if the declared type is a reference,
12278 which is invalid, but as references to references are usually
12279 quietly merged, diagnose it here. */
12280 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12281 {
12282 tree argtype
12283 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12284 argtype = tsubst (argtype, args, complain, in_decl);
12285 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12286 error_at (DECL_SOURCE_LOCATION (t),
12287 "reference type %qT in "
12288 "%<#pragma omp declare reduction%>", argtype);
12289 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12290 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12291 argtype);
12292 }
12293
12294 if (member && DECL_CONV_FN_P (r))
12295 /* Type-conversion operator. Reconstruct the name, in
12296 case it's the name of one of the template's parameters. */
12297 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12298
12299 tree parms = DECL_ARGUMENTS (t);
12300 if (closure)
12301 parms = DECL_CHAIN (parms);
12302 parms = tsubst (parms, args, complain, t);
12303 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12304 DECL_CONTEXT (parm) = r;
12305 if (closure)
12306 {
12307 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12308 DECL_CHAIN (tparm) = parms;
12309 parms = tparm;
12310 }
12311 DECL_ARGUMENTS (r) = parms;
12312 DECL_RESULT (r) = NULL_TREE;
12313
12314 TREE_STATIC (r) = 0;
12315 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12316 DECL_EXTERNAL (r) = 1;
12317 /* If this is an instantiation of a function with internal
12318 linkage, we already know what object file linkage will be
12319 assigned to the instantiation. */
12320 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12321 DECL_DEFER_OUTPUT (r) = 0;
12322 DECL_CHAIN (r) = NULL_TREE;
12323 DECL_PENDING_INLINE_INFO (r) = 0;
12324 DECL_PENDING_INLINE_P (r) = 0;
12325 DECL_SAVED_TREE (r) = NULL_TREE;
12326 DECL_STRUCT_FUNCTION (r) = NULL;
12327 TREE_USED (r) = 0;
12328 /* We'll re-clone as appropriate in instantiate_template. */
12329 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12330
12331 /* If we aren't complaining now, return on error before we register
12332 the specialization so that we'll complain eventually. */
12333 if ((complain & tf_error) == 0
12334 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12335 && !grok_op_properties (r, /*complain=*/false))
12336 return error_mark_node;
12337
12338 /* When instantiating a constrained member, substitute
12339 into the constraints to create a new constraint. */
12340 if (tree ci = get_constraints (t))
12341 if (member)
12342 {
12343 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12344 set_constraints (r, ci);
12345 }
12346
12347 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12348 this in the special friend case mentioned above where
12349 GEN_TMPL is NULL. */
12350 if (gen_tmpl && !closure)
12351 {
12352 DECL_TEMPLATE_INFO (r)
12353 = build_template_info (gen_tmpl, argvec);
12354 SET_DECL_IMPLICIT_INSTANTIATION (r);
12355
12356 tree new_r
12357 = register_specialization (r, gen_tmpl, argvec, false, hash);
12358 if (new_r != r)
12359 /* We instantiated this while substituting into
12360 the type earlier (template/friend54.C). */
12361 return new_r;
12362
12363 /* We're not supposed to instantiate default arguments
12364 until they are called, for a template. But, for a
12365 declaration like:
12366
12367 template <class T> void f ()
12368 { extern void g(int i = T()); }
12369
12370 we should do the substitution when the template is
12371 instantiated. We handle the member function case in
12372 instantiate_class_template since the default arguments
12373 might refer to other members of the class. */
12374 if (!member
12375 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12376 && !uses_template_parms (argvec))
12377 tsubst_default_arguments (r, complain);
12378 }
12379 else
12380 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12381
12382 /* Copy the list of befriending classes. */
12383 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12384 *friends;
12385 friends = &TREE_CHAIN (*friends))
12386 {
12387 *friends = copy_node (*friends);
12388 TREE_VALUE (*friends)
12389 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12390 }
12391
12392 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12393 {
12394 maybe_retrofit_in_chrg (r);
12395 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12396 return error_mark_node;
12397 /* If this is an instantiation of a member template, clone it.
12398 If it isn't, that'll be handled by
12399 clone_constructors_and_destructors. */
12400 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12401 clone_function_decl (r, /*update_methods=*/false);
12402 }
12403 else if ((complain & tf_error) != 0
12404 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12405 && !grok_op_properties (r, /*complain=*/true))
12406 return error_mark_node;
12407
12408 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12409 SET_DECL_FRIEND_CONTEXT (r,
12410 tsubst (DECL_FRIEND_CONTEXT (t),
12411 args, complain, in_decl));
12412
12413 /* Possibly limit visibility based on template args. */
12414 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12415 if (DECL_VISIBILITY_SPECIFIED (t))
12416 {
12417 DECL_VISIBILITY_SPECIFIED (r) = 0;
12418 DECL_ATTRIBUTES (r)
12419 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12420 }
12421 determine_visibility (r);
12422 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12423 && !processing_template_decl)
12424 defaulted_late_check (r);
12425
12426 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12427 args, complain, in_decl);
12428 return r;
12429 }
12430
12431 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12432
12433 static tree
12434 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12435 tree lambda_fntype)
12436 {
12437 /* We can get here when processing a member function template,
12438 member class template, or template template parameter. */
12439 tree decl = DECL_TEMPLATE_RESULT (t);
12440 tree in_decl = t;
12441 tree spec;
12442 tree tmpl_args;
12443 tree full_args;
12444 tree r;
12445 hashval_t hash = 0;
12446
12447 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12448 {
12449 /* Template template parameter is treated here. */
12450 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12451 if (new_type == error_mark_node)
12452 r = error_mark_node;
12453 /* If we get a real template back, return it. This can happen in
12454 the context of most_specialized_partial_spec. */
12455 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12456 r = new_type;
12457 else
12458 /* The new TEMPLATE_DECL was built in
12459 reduce_template_parm_level. */
12460 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12461 return r;
12462 }
12463
12464 if (!lambda_fntype)
12465 {
12466 /* We might already have an instance of this template.
12467 The ARGS are for the surrounding class type, so the
12468 full args contain the tsubst'd args for the context,
12469 plus the innermost args from the template decl. */
12470 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12471 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12472 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12473 /* Because this is a template, the arguments will still be
12474 dependent, even after substitution. If
12475 PROCESSING_TEMPLATE_DECL is not set, the dependency
12476 predicates will short-circuit. */
12477 ++processing_template_decl;
12478 full_args = tsubst_template_args (tmpl_args, args,
12479 complain, in_decl);
12480 --processing_template_decl;
12481 if (full_args == error_mark_node)
12482 return error_mark_node;
12483
12484 /* If this is a default template template argument,
12485 tsubst might not have changed anything. */
12486 if (full_args == tmpl_args)
12487 return t;
12488
12489 hash = hash_tmpl_and_args (t, full_args);
12490 spec = retrieve_specialization (t, full_args, hash);
12491 if (spec != NULL_TREE)
12492 return spec;
12493 }
12494
12495 /* Make a new template decl. It will be similar to the
12496 original, but will record the current template arguments.
12497 We also create a new function declaration, which is just
12498 like the old one, but points to this new template, rather
12499 than the old one. */
12500 r = copy_decl (t);
12501 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12502 DECL_CHAIN (r) = NULL_TREE;
12503
12504 // Build new template info linking to the original template decl.
12505 if (!lambda_fntype)
12506 {
12507 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12508 SET_DECL_IMPLICIT_INSTANTIATION (r);
12509 }
12510 else
12511 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12512
12513 /* The template parameters for this new template are all the
12514 template parameters for the old template, except the
12515 outermost level of parameters. */
12516 DECL_TEMPLATE_PARMS (r)
12517 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12518 complain);
12519
12520 if (TREE_CODE (decl) == TYPE_DECL
12521 && !TYPE_DECL_ALIAS_P (decl))
12522 {
12523 tree new_type;
12524 ++processing_template_decl;
12525 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12526 --processing_template_decl;
12527 if (new_type == error_mark_node)
12528 return error_mark_node;
12529
12530 TREE_TYPE (r) = new_type;
12531 /* For a partial specialization, we need to keep pointing to
12532 the primary template. */
12533 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12534 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12535 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12536 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12537 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12538 }
12539 else
12540 {
12541 tree new_decl;
12542 ++processing_template_decl;
12543 if (TREE_CODE (decl) == FUNCTION_DECL)
12544 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12545 else
12546 new_decl = tsubst (decl, args, complain, in_decl);
12547 --processing_template_decl;
12548 if (new_decl == error_mark_node)
12549 return error_mark_node;
12550
12551 DECL_TEMPLATE_RESULT (r) = new_decl;
12552 TREE_TYPE (r) = TREE_TYPE (new_decl);
12553 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12554 if (lambda_fntype)
12555 {
12556 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12557 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12558 }
12559 else
12560 {
12561 DECL_TI_TEMPLATE (new_decl) = r;
12562 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12563 }
12564 }
12565
12566 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12567 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12568
12569 if (PRIMARY_TEMPLATE_P (t))
12570 DECL_PRIMARY_TEMPLATE (r) = r;
12571
12572 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12573 && !lambda_fntype)
12574 /* Record this non-type partial instantiation. */
12575 register_specialization (r, t,
12576 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12577 false, hash);
12578
12579 return r;
12580 }
12581
12582 /* True if FN is the op() for a lambda in an uninstantiated template. */
12583
12584 bool
12585 lambda_fn_in_template_p (tree fn)
12586 {
12587 if (!fn || !LAMBDA_FUNCTION_P (fn))
12588 return false;
12589 tree closure = DECL_CONTEXT (fn);
12590 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12591 }
12592
12593 /* True if FN is the op() for a lambda regenerated from a lambda in an
12594 uninstantiated template. */
12595
12596 bool
12597 regenerated_lambda_fn_p (tree fn)
12598 {
12599 return (LAMBDA_FUNCTION_P (fn)
12600 && !DECL_TEMPLATE_INSTANTIATION (fn));
12601 }
12602
12603 /* We're instantiating a variable from template function TCTX. Return the
12604 corresponding current enclosing scope. This gets complicated because lambda
12605 functions in templates are regenerated rather than instantiated, but generic
12606 lambda functions are subsequently instantiated. */
12607
12608 static tree
12609 enclosing_instantiation_of (tree tctx)
12610 {
12611 tree fn = current_function_decl;
12612 int lambda_count = 0;
12613
12614 for (; tctx && lambda_fn_in_template_p (tctx);
12615 tctx = decl_function_context (tctx))
12616 ++lambda_count;
12617 for (; fn; fn = decl_function_context (fn))
12618 {
12619 tree lambda = fn;
12620 int flambda_count = 0;
12621 for (; fn && regenerated_lambda_fn_p (fn);
12622 fn = decl_function_context (fn))
12623 ++flambda_count;
12624 if (DECL_TEMPLATE_INFO (fn)
12625 ? most_general_template (fn) != most_general_template (tctx)
12626 : fn != tctx)
12627 continue;
12628 if (lambda_count)
12629 {
12630 fn = lambda;
12631 while (flambda_count-- > lambda_count)
12632 fn = decl_function_context (fn);
12633 }
12634 return fn;
12635 }
12636 gcc_unreachable ();
12637 }
12638
12639 /* Substitute the ARGS into the T, which is a _DECL. Return the
12640 result of the substitution. Issue error and warning messages under
12641 control of COMPLAIN. */
12642
12643 static tree
12644 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12645 {
12646 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12647 location_t saved_loc;
12648 tree r = NULL_TREE;
12649 tree in_decl = t;
12650 hashval_t hash = 0;
12651
12652 /* Set the filename and linenumber to improve error-reporting. */
12653 saved_loc = input_location;
12654 input_location = DECL_SOURCE_LOCATION (t);
12655
12656 switch (TREE_CODE (t))
12657 {
12658 case TEMPLATE_DECL:
12659 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12660 break;
12661
12662 case FUNCTION_DECL:
12663 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12664 break;
12665
12666 case PARM_DECL:
12667 {
12668 tree type = NULL_TREE;
12669 int i, len = 1;
12670 tree expanded_types = NULL_TREE;
12671 tree prev_r = NULL_TREE;
12672 tree first_r = NULL_TREE;
12673
12674 if (DECL_PACK_P (t))
12675 {
12676 /* If there is a local specialization that isn't a
12677 parameter pack, it means that we're doing a "simple"
12678 substitution from inside tsubst_pack_expansion. Just
12679 return the local specialization (which will be a single
12680 parm). */
12681 tree spec = retrieve_local_specialization (t);
12682 if (spec
12683 && TREE_CODE (spec) == PARM_DECL
12684 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12685 RETURN (spec);
12686
12687 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12688 the parameters in this function parameter pack. */
12689 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12690 complain, in_decl);
12691 if (TREE_CODE (expanded_types) == TREE_VEC)
12692 {
12693 len = TREE_VEC_LENGTH (expanded_types);
12694
12695 /* Zero-length parameter packs are boring. Just substitute
12696 into the chain. */
12697 if (len == 0)
12698 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12699 TREE_CHAIN (t)));
12700 }
12701 else
12702 {
12703 /* All we did was update the type. Make a note of that. */
12704 type = expanded_types;
12705 expanded_types = NULL_TREE;
12706 }
12707 }
12708
12709 /* Loop through all of the parameters we'll build. When T is
12710 a function parameter pack, LEN is the number of expanded
12711 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12712 r = NULL_TREE;
12713 for (i = 0; i < len; ++i)
12714 {
12715 prev_r = r;
12716 r = copy_node (t);
12717 if (DECL_TEMPLATE_PARM_P (t))
12718 SET_DECL_TEMPLATE_PARM_P (r);
12719
12720 if (expanded_types)
12721 /* We're on the Ith parameter of the function parameter
12722 pack. */
12723 {
12724 /* Get the Ith type. */
12725 type = TREE_VEC_ELT (expanded_types, i);
12726
12727 /* Rename the parameter to include the index. */
12728 DECL_NAME (r)
12729 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12730 }
12731 else if (!type)
12732 /* We're dealing with a normal parameter. */
12733 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12734
12735 type = type_decays_to (type);
12736 TREE_TYPE (r) = type;
12737 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12738
12739 if (DECL_INITIAL (r))
12740 {
12741 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12742 DECL_INITIAL (r) = TREE_TYPE (r);
12743 else
12744 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12745 complain, in_decl);
12746 }
12747
12748 DECL_CONTEXT (r) = NULL_TREE;
12749
12750 if (!DECL_TEMPLATE_PARM_P (r))
12751 DECL_ARG_TYPE (r) = type_passed_as (type);
12752
12753 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12754 args, complain, in_decl);
12755
12756 /* Keep track of the first new parameter we
12757 generate. That's what will be returned to the
12758 caller. */
12759 if (!first_r)
12760 first_r = r;
12761
12762 /* Build a proper chain of parameters when substituting
12763 into a function parameter pack. */
12764 if (prev_r)
12765 DECL_CHAIN (prev_r) = r;
12766 }
12767
12768 /* If cp_unevaluated_operand is set, we're just looking for a
12769 single dummy parameter, so don't keep going. */
12770 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12771 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12772 complain, DECL_CHAIN (t));
12773
12774 /* FIRST_R contains the start of the chain we've built. */
12775 r = first_r;
12776 }
12777 break;
12778
12779 case FIELD_DECL:
12780 {
12781 tree type = NULL_TREE;
12782 tree vec = NULL_TREE;
12783 tree expanded_types = NULL_TREE;
12784 int len = 1;
12785
12786 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12787 {
12788 /* This field is a lambda capture pack. Return a TREE_VEC of
12789 the expanded fields to instantiate_class_template_1 and
12790 store them in the specializations hash table as a
12791 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12792 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12793 complain, in_decl);
12794 if (TREE_CODE (expanded_types) == TREE_VEC)
12795 {
12796 len = TREE_VEC_LENGTH (expanded_types);
12797 vec = make_tree_vec (len);
12798 }
12799 else
12800 {
12801 /* All we did was update the type. Make a note of that. */
12802 type = expanded_types;
12803 expanded_types = NULL_TREE;
12804 }
12805 }
12806
12807 for (int i = 0; i < len; ++i)
12808 {
12809 r = copy_decl (t);
12810 if (expanded_types)
12811 {
12812 type = TREE_VEC_ELT (expanded_types, i);
12813 DECL_NAME (r)
12814 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12815 }
12816 else if (!type)
12817 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12818
12819 if (type == error_mark_node)
12820 RETURN (error_mark_node);
12821 TREE_TYPE (r) = type;
12822 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12823
12824 if (DECL_C_BIT_FIELD (r))
12825 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12826 number of bits. */
12827 DECL_BIT_FIELD_REPRESENTATIVE (r)
12828 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
12829 complain, in_decl,
12830 /*integral_constant_expression_p=*/true);
12831 if (DECL_INITIAL (t))
12832 {
12833 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12834 NSDMI in perform_member_init. Still set DECL_INITIAL
12835 so that we know there is one. */
12836 DECL_INITIAL (r) = void_node;
12837 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12838 retrofit_lang_decl (r);
12839 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12840 }
12841 /* We don't have to set DECL_CONTEXT here; it is set by
12842 finish_member_declaration. */
12843 DECL_CHAIN (r) = NULL_TREE;
12844
12845 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12846 args, complain, in_decl);
12847
12848 if (vec)
12849 TREE_VEC_ELT (vec, i) = r;
12850 }
12851
12852 if (vec)
12853 {
12854 r = vec;
12855 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12856 SET_ARGUMENT_PACK_ARGS (pack, vec);
12857 register_specialization (pack, t, args, false, 0);
12858 }
12859 }
12860 break;
12861
12862 case USING_DECL:
12863 /* We reach here only for member using decls. We also need to check
12864 uses_template_parms because DECL_DEPENDENT_P is not set for a
12865 using-declaration that designates a member of the current
12866 instantiation (c++/53549). */
12867 if (DECL_DEPENDENT_P (t)
12868 || uses_template_parms (USING_DECL_SCOPE (t)))
12869 {
12870 tree scope = USING_DECL_SCOPE (t);
12871 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12872 if (PACK_EXPANSION_P (scope))
12873 {
12874 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12875 int len = TREE_VEC_LENGTH (vec);
12876 r = make_tree_vec (len);
12877 for (int i = 0; i < len; ++i)
12878 {
12879 tree escope = TREE_VEC_ELT (vec, i);
12880 tree elt = do_class_using_decl (escope, name);
12881 if (!elt)
12882 {
12883 r = error_mark_node;
12884 break;
12885 }
12886 else
12887 {
12888 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12889 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12890 }
12891 TREE_VEC_ELT (r, i) = elt;
12892 }
12893 }
12894 else
12895 {
12896 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12897 complain, in_decl);
12898 r = do_class_using_decl (inst_scope, name);
12899 if (!r)
12900 r = error_mark_node;
12901 else
12902 {
12903 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12904 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12905 }
12906 }
12907 }
12908 else
12909 {
12910 r = copy_node (t);
12911 DECL_CHAIN (r) = NULL_TREE;
12912 }
12913 break;
12914
12915 case TYPE_DECL:
12916 case VAR_DECL:
12917 {
12918 tree argvec = NULL_TREE;
12919 tree gen_tmpl = NULL_TREE;
12920 tree spec;
12921 tree tmpl = NULL_TREE;
12922 tree ctx;
12923 tree type = NULL_TREE;
12924 bool local_p;
12925
12926 if (TREE_TYPE (t) == error_mark_node)
12927 RETURN (error_mark_node);
12928
12929 if (TREE_CODE (t) == TYPE_DECL
12930 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12931 {
12932 /* If this is the canonical decl, we don't have to
12933 mess with instantiations, and often we can't (for
12934 typename, template type parms and such). Note that
12935 TYPE_NAME is not correct for the above test if
12936 we've copied the type for a typedef. */
12937 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12938 if (type == error_mark_node)
12939 RETURN (error_mark_node);
12940 r = TYPE_NAME (type);
12941 break;
12942 }
12943
12944 /* Check to see if we already have the specialization we
12945 need. */
12946 spec = NULL_TREE;
12947 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12948 {
12949 /* T is a static data member or namespace-scope entity.
12950 We have to substitute into namespace-scope variables
12951 (not just variable templates) because of cases like:
12952
12953 template <class T> void f() { extern T t; }
12954
12955 where the entity referenced is not known until
12956 instantiation time. */
12957 local_p = false;
12958 ctx = DECL_CONTEXT (t);
12959 if (DECL_CLASS_SCOPE_P (t))
12960 {
12961 ctx = tsubst_aggr_type (ctx, args,
12962 complain,
12963 in_decl, /*entering_scope=*/1);
12964 /* If CTX is unchanged, then T is in fact the
12965 specialization we want. That situation occurs when
12966 referencing a static data member within in its own
12967 class. We can use pointer equality, rather than
12968 same_type_p, because DECL_CONTEXT is always
12969 canonical... */
12970 if (ctx == DECL_CONTEXT (t)
12971 /* ... unless T is a member template; in which
12972 case our caller can be willing to create a
12973 specialization of that template represented
12974 by T. */
12975 && !(DECL_TI_TEMPLATE (t)
12976 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12977 spec = t;
12978 }
12979
12980 if (!spec)
12981 {
12982 tmpl = DECL_TI_TEMPLATE (t);
12983 gen_tmpl = most_general_template (tmpl);
12984 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12985 if (argvec != error_mark_node)
12986 argvec = (coerce_innermost_template_parms
12987 (DECL_TEMPLATE_PARMS (gen_tmpl),
12988 argvec, t, complain,
12989 /*all*/true, /*defarg*/true));
12990 if (argvec == error_mark_node)
12991 RETURN (error_mark_node);
12992 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12993 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12994 }
12995 }
12996 else
12997 {
12998 /* A local variable. */
12999 local_p = true;
13000 /* Subsequent calls to pushdecl will fill this in. */
13001 ctx = NULL_TREE;
13002 /* Unless this is a reference to a static variable from an
13003 enclosing function, in which case we need to fill it in now. */
13004 if (TREE_STATIC (t))
13005 {
13006 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13007 if (fn != current_function_decl)
13008 ctx = fn;
13009 }
13010 spec = retrieve_local_specialization (t);
13011 }
13012 /* If we already have the specialization we need, there is
13013 nothing more to do. */
13014 if (spec)
13015 {
13016 r = spec;
13017 break;
13018 }
13019
13020 /* Create a new node for the specialization we need. */
13021 r = copy_decl (t);
13022 if (type == NULL_TREE)
13023 {
13024 if (is_typedef_decl (t))
13025 type = DECL_ORIGINAL_TYPE (t);
13026 else
13027 type = TREE_TYPE (t);
13028 if (VAR_P (t)
13029 && VAR_HAD_UNKNOWN_BOUND (t)
13030 && type != error_mark_node)
13031 type = strip_array_domain (type);
13032 tree sub_args = args;
13033 if (tree auto_node = type_uses_auto (type))
13034 {
13035 /* Mask off any template args past the variable's context so we
13036 don't replace the auto with an unrelated argument. */
13037 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13038 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13039 if (extra > 0)
13040 /* This should never happen with the new lambda instantiation
13041 model, but keep the handling just in case. */
13042 gcc_assert (!CHECKING_P),
13043 sub_args = strip_innermost_template_args (args, extra);
13044 }
13045 type = tsubst (type, sub_args, complain, in_decl);
13046 }
13047 if (VAR_P (r))
13048 {
13049 /* Even if the original location is out of scope, the
13050 newly substituted one is not. */
13051 DECL_DEAD_FOR_LOCAL (r) = 0;
13052 DECL_INITIALIZED_P (r) = 0;
13053 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13054 if (type == error_mark_node)
13055 RETURN (error_mark_node);
13056 if (TREE_CODE (type) == FUNCTION_TYPE)
13057 {
13058 /* It may seem that this case cannot occur, since:
13059
13060 typedef void f();
13061 void g() { f x; }
13062
13063 declares a function, not a variable. However:
13064
13065 typedef void f();
13066 template <typename T> void g() { T t; }
13067 template void g<f>();
13068
13069 is an attempt to declare a variable with function
13070 type. */
13071 error ("variable %qD has function type",
13072 /* R is not yet sufficiently initialized, so we
13073 just use its name. */
13074 DECL_NAME (r));
13075 RETURN (error_mark_node);
13076 }
13077 type = complete_type (type);
13078 /* Wait until cp_finish_decl to set this again, to handle
13079 circular dependency (template/instantiate6.C). */
13080 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13081 type = check_var_type (DECL_NAME (r), type);
13082
13083 if (DECL_HAS_VALUE_EXPR_P (t))
13084 {
13085 tree ve = DECL_VALUE_EXPR (t);
13086 ve = tsubst_expr (ve, args, complain, in_decl,
13087 /*constant_expression_p=*/false);
13088 if (REFERENCE_REF_P (ve))
13089 {
13090 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13091 ve = TREE_OPERAND (ve, 0);
13092 }
13093 SET_DECL_VALUE_EXPR (r, ve);
13094 }
13095 if (CP_DECL_THREAD_LOCAL_P (r)
13096 && !processing_template_decl)
13097 set_decl_tls_model (r, decl_default_tls_model (r));
13098 }
13099 else if (DECL_SELF_REFERENCE_P (t))
13100 SET_DECL_SELF_REFERENCE_P (r);
13101 TREE_TYPE (r) = type;
13102 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13103 DECL_CONTEXT (r) = ctx;
13104 /* Clear out the mangled name and RTL for the instantiation. */
13105 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13106 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13107 SET_DECL_RTL (r, NULL);
13108 /* The initializer must not be expanded until it is required;
13109 see [temp.inst]. */
13110 DECL_INITIAL (r) = NULL_TREE;
13111 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13112 if (VAR_P (r))
13113 {
13114 SET_DECL_MODE (r, VOIDmode);
13115
13116 /* Possibly limit visibility based on template args. */
13117 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13118 if (DECL_VISIBILITY_SPECIFIED (t))
13119 {
13120 DECL_VISIBILITY_SPECIFIED (r) = 0;
13121 DECL_ATTRIBUTES (r)
13122 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13123 }
13124 determine_visibility (r);
13125 }
13126
13127 if (!local_p)
13128 {
13129 /* A static data member declaration is always marked
13130 external when it is declared in-class, even if an
13131 initializer is present. We mimic the non-template
13132 processing here. */
13133 DECL_EXTERNAL (r) = 1;
13134 if (DECL_NAMESPACE_SCOPE_P (t))
13135 DECL_NOT_REALLY_EXTERN (r) = 1;
13136
13137 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13138 SET_DECL_IMPLICIT_INSTANTIATION (r);
13139 register_specialization (r, gen_tmpl, argvec, false, hash);
13140 }
13141 else
13142 {
13143 if (DECL_LANG_SPECIFIC (r))
13144 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13145 if (!cp_unevaluated_operand)
13146 register_local_specialization (r, t);
13147 }
13148
13149 DECL_CHAIN (r) = NULL_TREE;
13150
13151 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13152 /*flags=*/0,
13153 args, complain, in_decl);
13154
13155 /* Preserve a typedef that names a type. */
13156 if (is_typedef_decl (r) && type != error_mark_node)
13157 {
13158 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13159 set_underlying_type (r);
13160 if (TYPE_DECL_ALIAS_P (r))
13161 /* An alias template specialization can be dependent
13162 even if its underlying type is not. */
13163 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13164 }
13165
13166 layout_decl (r, 0);
13167 }
13168 break;
13169
13170 default:
13171 gcc_unreachable ();
13172 }
13173 #undef RETURN
13174
13175 out:
13176 /* Restore the file and line information. */
13177 input_location = saved_loc;
13178
13179 return r;
13180 }
13181
13182 /* Substitute into the ARG_TYPES of a function type.
13183 If END is a TREE_CHAIN, leave it and any following types
13184 un-substituted. */
13185
13186 static tree
13187 tsubst_arg_types (tree arg_types,
13188 tree args,
13189 tree end,
13190 tsubst_flags_t complain,
13191 tree in_decl)
13192 {
13193 tree remaining_arg_types;
13194 tree type = NULL_TREE;
13195 int i = 1;
13196 tree expanded_args = NULL_TREE;
13197 tree default_arg;
13198
13199 if (!arg_types || arg_types == void_list_node || arg_types == end)
13200 return arg_types;
13201
13202 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13203 args, end, complain, in_decl);
13204 if (remaining_arg_types == error_mark_node)
13205 return error_mark_node;
13206
13207 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13208 {
13209 /* For a pack expansion, perform substitution on the
13210 entire expression. Later on, we'll handle the arguments
13211 one-by-one. */
13212 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13213 args, complain, in_decl);
13214
13215 if (TREE_CODE (expanded_args) == TREE_VEC)
13216 /* So that we'll spin through the parameters, one by one. */
13217 i = TREE_VEC_LENGTH (expanded_args);
13218 else
13219 {
13220 /* We only partially substituted into the parameter
13221 pack. Our type is TYPE_PACK_EXPANSION. */
13222 type = expanded_args;
13223 expanded_args = NULL_TREE;
13224 }
13225 }
13226
13227 while (i > 0) {
13228 --i;
13229
13230 if (expanded_args)
13231 type = TREE_VEC_ELT (expanded_args, i);
13232 else if (!type)
13233 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13234
13235 if (type == error_mark_node)
13236 return error_mark_node;
13237 if (VOID_TYPE_P (type))
13238 {
13239 if (complain & tf_error)
13240 {
13241 error ("invalid parameter type %qT", type);
13242 if (in_decl)
13243 error ("in declaration %q+D", in_decl);
13244 }
13245 return error_mark_node;
13246 }
13247 /* DR 657. */
13248 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13249 return error_mark_node;
13250
13251 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13252 top-level qualifiers as required. */
13253 type = cv_unqualified (type_decays_to (type));
13254
13255 /* We do not substitute into default arguments here. The standard
13256 mandates that they be instantiated only when needed, which is
13257 done in build_over_call. */
13258 default_arg = TREE_PURPOSE (arg_types);
13259
13260 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13261 since the new op() won't have any associated template arguments for us
13262 to refer to later. */
13263 if (lambda_fn_in_template_p (in_decl))
13264 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13265 false/*fn*/, false/*constexpr*/);
13266
13267 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13268 {
13269 /* We've instantiated a template before its default arguments
13270 have been parsed. This can happen for a nested template
13271 class, and is not an error unless we require the default
13272 argument in a call of this function. */
13273 remaining_arg_types =
13274 tree_cons (default_arg, type, remaining_arg_types);
13275 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13276 }
13277 else
13278 remaining_arg_types =
13279 hash_tree_cons (default_arg, type, remaining_arg_types);
13280 }
13281
13282 return remaining_arg_types;
13283 }
13284
13285 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13286 *not* handle the exception-specification for FNTYPE, because the
13287 initial substitution of explicitly provided template parameters
13288 during argument deduction forbids substitution into the
13289 exception-specification:
13290
13291 [temp.deduct]
13292
13293 All references in the function type of the function template to the
13294 corresponding template parameters are replaced by the specified tem-
13295 plate argument values. If a substitution in a template parameter or
13296 in the function type of the function template results in an invalid
13297 type, type deduction fails. [Note: The equivalent substitution in
13298 exception specifications is done only when the function is instanti-
13299 ated, at which point a program is ill-formed if the substitution
13300 results in an invalid type.] */
13301
13302 static tree
13303 tsubst_function_type (tree t,
13304 tree args,
13305 tsubst_flags_t complain,
13306 tree in_decl)
13307 {
13308 tree return_type;
13309 tree arg_types = NULL_TREE;
13310 tree fntype;
13311
13312 /* The TYPE_CONTEXT is not used for function/method types. */
13313 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13314
13315 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13316 failure. */
13317 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13318
13319 if (late_return_type_p)
13320 {
13321 /* Substitute the argument types. */
13322 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13323 complain, in_decl);
13324 if (arg_types == error_mark_node)
13325 return error_mark_node;
13326
13327 tree save_ccp = current_class_ptr;
13328 tree save_ccr = current_class_ref;
13329 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13330 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13331 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13332 if (do_inject)
13333 {
13334 /* DR 1207: 'this' is in scope in the trailing return type. */
13335 inject_this_parameter (this_type, cp_type_quals (this_type));
13336 }
13337
13338 /* Substitute the return type. */
13339 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13340
13341 if (do_inject)
13342 {
13343 current_class_ptr = save_ccp;
13344 current_class_ref = save_ccr;
13345 }
13346 }
13347 else
13348 /* Substitute the return type. */
13349 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13350
13351 if (return_type == error_mark_node)
13352 return error_mark_node;
13353 /* DR 486 clarifies that creation of a function type with an
13354 invalid return type is a deduction failure. */
13355 if (TREE_CODE (return_type) == ARRAY_TYPE
13356 || TREE_CODE (return_type) == FUNCTION_TYPE)
13357 {
13358 if (complain & tf_error)
13359 {
13360 if (TREE_CODE (return_type) == ARRAY_TYPE)
13361 error ("function returning an array");
13362 else
13363 error ("function returning a function");
13364 }
13365 return error_mark_node;
13366 }
13367 /* And DR 657. */
13368 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13369 return error_mark_node;
13370
13371 if (!late_return_type_p)
13372 {
13373 /* Substitute the argument types. */
13374 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13375 complain, in_decl);
13376 if (arg_types == error_mark_node)
13377 return error_mark_node;
13378 }
13379
13380 /* Construct a new type node and return it. */
13381 if (TREE_CODE (t) == FUNCTION_TYPE)
13382 {
13383 fntype = build_function_type (return_type, arg_types);
13384 fntype = apply_memfn_quals (fntype,
13385 type_memfn_quals (t),
13386 type_memfn_rqual (t));
13387 }
13388 else
13389 {
13390 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13391 /* Don't pick up extra function qualifiers from the basetype. */
13392 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13393 if (! MAYBE_CLASS_TYPE_P (r))
13394 {
13395 /* [temp.deduct]
13396
13397 Type deduction may fail for any of the following
13398 reasons:
13399
13400 -- Attempting to create "pointer to member of T" when T
13401 is not a class type. */
13402 if (complain & tf_error)
13403 error ("creating pointer to member function of non-class type %qT",
13404 r);
13405 return error_mark_node;
13406 }
13407
13408 fntype = build_method_type_directly (r, return_type,
13409 TREE_CHAIN (arg_types));
13410 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13411 }
13412 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13413
13414 if (late_return_type_p)
13415 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13416
13417 return fntype;
13418 }
13419
13420 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13421 ARGS into that specification, and return the substituted
13422 specification. If there is no specification, return NULL_TREE. */
13423
13424 static tree
13425 tsubst_exception_specification (tree fntype,
13426 tree args,
13427 tsubst_flags_t complain,
13428 tree in_decl,
13429 bool defer_ok)
13430 {
13431 tree specs;
13432 tree new_specs;
13433
13434 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13435 new_specs = NULL_TREE;
13436 if (specs && TREE_PURPOSE (specs))
13437 {
13438 /* A noexcept-specifier. */
13439 tree expr = TREE_PURPOSE (specs);
13440 if (TREE_CODE (expr) == INTEGER_CST)
13441 new_specs = expr;
13442 else if (defer_ok)
13443 {
13444 /* Defer instantiation of noexcept-specifiers to avoid
13445 excessive instantiations (c++/49107). */
13446 new_specs = make_node (DEFERRED_NOEXCEPT);
13447 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13448 {
13449 /* We already partially instantiated this member template,
13450 so combine the new args with the old. */
13451 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13452 = DEFERRED_NOEXCEPT_PATTERN (expr);
13453 DEFERRED_NOEXCEPT_ARGS (new_specs)
13454 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13455 }
13456 else
13457 {
13458 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13459 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13460 }
13461 }
13462 else
13463 new_specs = tsubst_copy_and_build
13464 (expr, args, complain, in_decl, /*function_p=*/false,
13465 /*integral_constant_expression_p=*/true);
13466 new_specs = build_noexcept_spec (new_specs, complain);
13467 }
13468 else if (specs)
13469 {
13470 if (! TREE_VALUE (specs))
13471 new_specs = specs;
13472 else
13473 while (specs)
13474 {
13475 tree spec;
13476 int i, len = 1;
13477 tree expanded_specs = NULL_TREE;
13478
13479 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13480 {
13481 /* Expand the pack expansion type. */
13482 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13483 args, complain,
13484 in_decl);
13485
13486 if (expanded_specs == error_mark_node)
13487 return error_mark_node;
13488 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13489 len = TREE_VEC_LENGTH (expanded_specs);
13490 else
13491 {
13492 /* We're substituting into a member template, so
13493 we got a TYPE_PACK_EXPANSION back. Add that
13494 expansion and move on. */
13495 gcc_assert (TREE_CODE (expanded_specs)
13496 == TYPE_PACK_EXPANSION);
13497 new_specs = add_exception_specifier (new_specs,
13498 expanded_specs,
13499 complain);
13500 specs = TREE_CHAIN (specs);
13501 continue;
13502 }
13503 }
13504
13505 for (i = 0; i < len; ++i)
13506 {
13507 if (expanded_specs)
13508 spec = TREE_VEC_ELT (expanded_specs, i);
13509 else
13510 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13511 if (spec == error_mark_node)
13512 return spec;
13513 new_specs = add_exception_specifier (new_specs, spec,
13514 complain);
13515 }
13516
13517 specs = TREE_CHAIN (specs);
13518 }
13519 }
13520 return new_specs;
13521 }
13522
13523 /* Take the tree structure T and replace template parameters used
13524 therein with the argument vector ARGS. IN_DECL is an associated
13525 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13526 Issue error and warning messages under control of COMPLAIN. Note
13527 that we must be relatively non-tolerant of extensions here, in
13528 order to preserve conformance; if we allow substitutions that
13529 should not be allowed, we may allow argument deductions that should
13530 not succeed, and therefore report ambiguous overload situations
13531 where there are none. In theory, we could allow the substitution,
13532 but indicate that it should have failed, and allow our caller to
13533 make sure that the right thing happens, but we don't try to do this
13534 yet.
13535
13536 This function is used for dealing with types, decls and the like;
13537 for expressions, use tsubst_expr or tsubst_copy. */
13538
13539 tree
13540 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13541 {
13542 enum tree_code code;
13543 tree type, r = NULL_TREE;
13544
13545 if (t == NULL_TREE || t == error_mark_node
13546 || t == integer_type_node
13547 || t == void_type_node
13548 || t == char_type_node
13549 || t == unknown_type_node
13550 || TREE_CODE (t) == NAMESPACE_DECL
13551 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13552 return t;
13553
13554 if (DECL_P (t))
13555 return tsubst_decl (t, args, complain);
13556
13557 if (args == NULL_TREE)
13558 return t;
13559
13560 code = TREE_CODE (t);
13561
13562 if (code == IDENTIFIER_NODE)
13563 type = IDENTIFIER_TYPE_VALUE (t);
13564 else
13565 type = TREE_TYPE (t);
13566
13567 gcc_assert (type != unknown_type_node);
13568
13569 /* Reuse typedefs. We need to do this to handle dependent attributes,
13570 such as attribute aligned. */
13571 if (TYPE_P (t)
13572 && typedef_variant_p (t))
13573 {
13574 tree decl = TYPE_NAME (t);
13575
13576 if (alias_template_specialization_p (t))
13577 {
13578 /* DECL represents an alias template and we want to
13579 instantiate it. */
13580 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13581 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13582 r = instantiate_alias_template (tmpl, gen_args, complain);
13583 }
13584 else if (DECL_CLASS_SCOPE_P (decl)
13585 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13586 && uses_template_parms (DECL_CONTEXT (decl)))
13587 {
13588 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13589 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13590 r = retrieve_specialization (tmpl, gen_args, 0);
13591 }
13592 else if (DECL_FUNCTION_SCOPE_P (decl)
13593 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13594 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13595 r = retrieve_local_specialization (decl);
13596 else
13597 /* The typedef is from a non-template context. */
13598 return t;
13599
13600 if (r)
13601 {
13602 r = TREE_TYPE (r);
13603 r = cp_build_qualified_type_real
13604 (r, cp_type_quals (t) | cp_type_quals (r),
13605 complain | tf_ignore_bad_quals);
13606 return r;
13607 }
13608 else
13609 {
13610 /* We don't have an instantiation yet, so drop the typedef. */
13611 int quals = cp_type_quals (t);
13612 t = DECL_ORIGINAL_TYPE (decl);
13613 t = cp_build_qualified_type_real (t, quals,
13614 complain | tf_ignore_bad_quals);
13615 }
13616 }
13617
13618 bool fndecl_type = (complain & tf_fndecl_type);
13619 complain &= ~tf_fndecl_type;
13620
13621 if (type
13622 && code != TYPENAME_TYPE
13623 && code != TEMPLATE_TYPE_PARM
13624 && code != TEMPLATE_PARM_INDEX
13625 && code != IDENTIFIER_NODE
13626 && code != FUNCTION_TYPE
13627 && code != METHOD_TYPE)
13628 type = tsubst (type, args, complain, in_decl);
13629 if (type == error_mark_node)
13630 return error_mark_node;
13631
13632 switch (code)
13633 {
13634 case RECORD_TYPE:
13635 case UNION_TYPE:
13636 case ENUMERAL_TYPE:
13637 return tsubst_aggr_type (t, args, complain, in_decl,
13638 /*entering_scope=*/0);
13639
13640 case ERROR_MARK:
13641 case IDENTIFIER_NODE:
13642 case VOID_TYPE:
13643 case REAL_TYPE:
13644 case COMPLEX_TYPE:
13645 case VECTOR_TYPE:
13646 case BOOLEAN_TYPE:
13647 case NULLPTR_TYPE:
13648 case LANG_TYPE:
13649 return t;
13650
13651 case INTEGER_TYPE:
13652 if (t == integer_type_node)
13653 return t;
13654
13655 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13656 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13657 return t;
13658
13659 {
13660 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13661
13662 max = tsubst_expr (omax, args, complain, in_decl,
13663 /*integral_constant_expression_p=*/false);
13664
13665 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13666 needed. */
13667 if (TREE_CODE (max) == NOP_EXPR
13668 && TREE_SIDE_EFFECTS (omax)
13669 && !TREE_TYPE (max))
13670 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13671
13672 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13673 with TREE_SIDE_EFFECTS that indicates this is not an integral
13674 constant expression. */
13675 if (processing_template_decl
13676 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13677 {
13678 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13679 TREE_SIDE_EFFECTS (max) = 1;
13680 }
13681
13682 return compute_array_index_type (NULL_TREE, max, complain);
13683 }
13684
13685 case TEMPLATE_TYPE_PARM:
13686 case TEMPLATE_TEMPLATE_PARM:
13687 case BOUND_TEMPLATE_TEMPLATE_PARM:
13688 case TEMPLATE_PARM_INDEX:
13689 {
13690 int idx;
13691 int level;
13692 int levels;
13693 tree arg = NULL_TREE;
13694
13695 /* Early in template argument deduction substitution, we don't
13696 want to reduce the level of 'auto', or it will be confused
13697 with a normal template parm in subsequent deduction. */
13698 if (is_auto (t) && (complain & tf_partial))
13699 return t;
13700
13701 r = NULL_TREE;
13702
13703 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13704 template_parm_level_and_index (t, &level, &idx);
13705
13706 levels = TMPL_ARGS_DEPTH (args);
13707 if (level <= levels
13708 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13709 {
13710 arg = TMPL_ARG (args, level, idx);
13711
13712 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13713 {
13714 /* See through ARGUMENT_PACK_SELECT arguments. */
13715 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13716 /* If the selected argument is an expansion E, that most
13717 likely means we were called from
13718 gen_elem_of_pack_expansion_instantiation during the
13719 substituting of pack an argument pack (which Ith
13720 element is a pack expansion, where I is
13721 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13722 In this case, the Ith element resulting from this
13723 substituting is going to be a pack expansion, which
13724 pattern is the pattern of E. Let's return the
13725 pattern of E, and
13726 gen_elem_of_pack_expansion_instantiation will
13727 build the resulting pack expansion from it. */
13728 if (PACK_EXPANSION_P (arg))
13729 {
13730 /* Make sure we aren't throwing away arg info. */
13731 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13732 arg = PACK_EXPANSION_PATTERN (arg);
13733 }
13734 }
13735 }
13736
13737 if (arg == error_mark_node)
13738 return error_mark_node;
13739 else if (arg != NULL_TREE)
13740 {
13741 if (ARGUMENT_PACK_P (arg))
13742 /* If ARG is an argument pack, we don't actually want to
13743 perform a substitution here, because substitutions
13744 for argument packs are only done
13745 element-by-element. We can get to this point when
13746 substituting the type of a non-type template
13747 parameter pack, when that type actually contains
13748 template parameter packs from an outer template, e.g.,
13749
13750 template<typename... Types> struct A {
13751 template<Types... Values> struct B { };
13752 }; */
13753 return t;
13754
13755 if (code == TEMPLATE_TYPE_PARM)
13756 {
13757 int quals;
13758 gcc_assert (TYPE_P (arg));
13759
13760 quals = cp_type_quals (arg) | cp_type_quals (t);
13761
13762 return cp_build_qualified_type_real
13763 (arg, quals, complain | tf_ignore_bad_quals);
13764 }
13765 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13766 {
13767 /* We are processing a type constructed from a
13768 template template parameter. */
13769 tree argvec = tsubst (TYPE_TI_ARGS (t),
13770 args, complain, in_decl);
13771 if (argvec == error_mark_node)
13772 return error_mark_node;
13773
13774 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13775 || TREE_CODE (arg) == TEMPLATE_DECL
13776 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13777
13778 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13779 /* Consider this code:
13780
13781 template <template <class> class Template>
13782 struct Internal {
13783 template <class Arg> using Bind = Template<Arg>;
13784 };
13785
13786 template <template <class> class Template, class Arg>
13787 using Instantiate = Template<Arg>; //#0
13788
13789 template <template <class> class Template,
13790 class Argument>
13791 using Bind =
13792 Instantiate<Internal<Template>::template Bind,
13793 Argument>; //#1
13794
13795 When #1 is parsed, the
13796 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13797 parameter `Template' in #0 matches the
13798 UNBOUND_CLASS_TEMPLATE representing the argument
13799 `Internal<Template>::template Bind'; We then want
13800 to assemble the type `Bind<Argument>' that can't
13801 be fully created right now, because
13802 `Internal<Template>' not being complete, the Bind
13803 template cannot be looked up in that context. So
13804 we need to "store" `Bind<Argument>' for later
13805 when the context of Bind becomes complete. Let's
13806 store that in a TYPENAME_TYPE. */
13807 return make_typename_type (TYPE_CONTEXT (arg),
13808 build_nt (TEMPLATE_ID_EXPR,
13809 TYPE_IDENTIFIER (arg),
13810 argvec),
13811 typename_type,
13812 complain);
13813
13814 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13815 are resolving nested-types in the signature of a
13816 member function templates. Otherwise ARG is a
13817 TEMPLATE_DECL and is the real template to be
13818 instantiated. */
13819 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13820 arg = TYPE_NAME (arg);
13821
13822 r = lookup_template_class (arg,
13823 argvec, in_decl,
13824 DECL_CONTEXT (arg),
13825 /*entering_scope=*/0,
13826 complain);
13827 return cp_build_qualified_type_real
13828 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13829 }
13830 else if (code == TEMPLATE_TEMPLATE_PARM)
13831 return arg;
13832 else
13833 /* TEMPLATE_PARM_INDEX. */
13834 return convert_from_reference (unshare_expr (arg));
13835 }
13836
13837 if (level == 1)
13838 /* This can happen during the attempted tsubst'ing in
13839 unify. This means that we don't yet have any information
13840 about the template parameter in question. */
13841 return t;
13842
13843 /* If we get here, we must have been looking at a parm for a
13844 more deeply nested template. Make a new version of this
13845 template parameter, but with a lower level. */
13846 switch (code)
13847 {
13848 case TEMPLATE_TYPE_PARM:
13849 case TEMPLATE_TEMPLATE_PARM:
13850 case BOUND_TEMPLATE_TEMPLATE_PARM:
13851 if (cp_type_quals (t))
13852 {
13853 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13854 r = cp_build_qualified_type_real
13855 (r, cp_type_quals (t),
13856 complain | (code == TEMPLATE_TYPE_PARM
13857 ? tf_ignore_bad_quals : 0));
13858 }
13859 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13860 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13861 && (r = (TEMPLATE_PARM_DESCENDANTS
13862 (TEMPLATE_TYPE_PARM_INDEX (t))))
13863 && (r = TREE_TYPE (r))
13864 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13865 /* Break infinite recursion when substituting the constraints
13866 of a constrained placeholder. */;
13867 else
13868 {
13869 r = copy_type (t);
13870 TEMPLATE_TYPE_PARM_INDEX (r)
13871 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13872 r, levels, args, complain);
13873 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13874 TYPE_MAIN_VARIANT (r) = r;
13875 TYPE_POINTER_TO (r) = NULL_TREE;
13876 TYPE_REFERENCE_TO (r) = NULL_TREE;
13877
13878 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13879 {
13880 /* Propagate constraints on placeholders. */
13881 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13882 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13883 = tsubst_constraint (constr, args, complain, in_decl);
13884 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13885 {
13886 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13887 pl = tsubst (pl, args, complain, in_decl);
13888 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13889 }
13890 }
13891
13892 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13893 /* We have reduced the level of the template
13894 template parameter, but not the levels of its
13895 template parameters, so canonical_type_parameter
13896 will not be able to find the canonical template
13897 template parameter for this level. Thus, we
13898 require structural equality checking to compare
13899 TEMPLATE_TEMPLATE_PARMs. */
13900 SET_TYPE_STRUCTURAL_EQUALITY (r);
13901 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13902 SET_TYPE_STRUCTURAL_EQUALITY (r);
13903 else
13904 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13905
13906 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13907 {
13908 tree tinfo = TYPE_TEMPLATE_INFO (t);
13909 /* We might need to substitute into the types of non-type
13910 template parameters. */
13911 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13912 complain, in_decl);
13913 if (tmpl == error_mark_node)
13914 return error_mark_node;
13915 tree argvec = tsubst (TI_ARGS (tinfo), args,
13916 complain, in_decl);
13917 if (argvec == error_mark_node)
13918 return error_mark_node;
13919
13920 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13921 = build_template_info (tmpl, argvec);
13922 }
13923 }
13924 break;
13925
13926 case TEMPLATE_PARM_INDEX:
13927 /* OK, now substitute the type of the non-type parameter. We
13928 couldn't do it earlier because it might be an auto parameter,
13929 and we wouldn't need to if we had an argument. */
13930 type = tsubst (type, args, complain, in_decl);
13931 if (type == error_mark_node)
13932 return error_mark_node;
13933 r = reduce_template_parm_level (t, type, levels, args, complain);
13934 break;
13935
13936 default:
13937 gcc_unreachable ();
13938 }
13939
13940 return r;
13941 }
13942
13943 case TREE_LIST:
13944 {
13945 tree purpose, value, chain;
13946
13947 if (t == void_list_node)
13948 return t;
13949
13950 purpose = TREE_PURPOSE (t);
13951 if (purpose)
13952 {
13953 purpose = tsubst (purpose, args, complain, in_decl);
13954 if (purpose == error_mark_node)
13955 return error_mark_node;
13956 }
13957 value = TREE_VALUE (t);
13958 if (value)
13959 {
13960 value = tsubst (value, args, complain, in_decl);
13961 if (value == error_mark_node)
13962 return error_mark_node;
13963 }
13964 chain = TREE_CHAIN (t);
13965 if (chain && chain != void_type_node)
13966 {
13967 chain = tsubst (chain, args, complain, in_decl);
13968 if (chain == error_mark_node)
13969 return error_mark_node;
13970 }
13971 if (purpose == TREE_PURPOSE (t)
13972 && value == TREE_VALUE (t)
13973 && chain == TREE_CHAIN (t))
13974 return t;
13975 return hash_tree_cons (purpose, value, chain);
13976 }
13977
13978 case TREE_BINFO:
13979 /* We should never be tsubsting a binfo. */
13980 gcc_unreachable ();
13981
13982 case TREE_VEC:
13983 /* A vector of template arguments. */
13984 gcc_assert (!type);
13985 return tsubst_template_args (t, args, complain, in_decl);
13986
13987 case POINTER_TYPE:
13988 case REFERENCE_TYPE:
13989 {
13990 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13991 return t;
13992
13993 /* [temp.deduct]
13994
13995 Type deduction may fail for any of the following
13996 reasons:
13997
13998 -- Attempting to create a pointer to reference type.
13999 -- Attempting to create a reference to a reference type or
14000 a reference to void.
14001
14002 Core issue 106 says that creating a reference to a reference
14003 during instantiation is no longer a cause for failure. We
14004 only enforce this check in strict C++98 mode. */
14005 if ((TREE_CODE (type) == REFERENCE_TYPE
14006 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14007 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14008 {
14009 static location_t last_loc;
14010
14011 /* We keep track of the last time we issued this error
14012 message to avoid spewing a ton of messages during a
14013 single bad template instantiation. */
14014 if (complain & tf_error
14015 && last_loc != input_location)
14016 {
14017 if (VOID_TYPE_P (type))
14018 error ("forming reference to void");
14019 else if (code == POINTER_TYPE)
14020 error ("forming pointer to reference type %qT", type);
14021 else
14022 error ("forming reference to reference type %qT", type);
14023 last_loc = input_location;
14024 }
14025
14026 return error_mark_node;
14027 }
14028 else if (TREE_CODE (type) == FUNCTION_TYPE
14029 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14030 || type_memfn_rqual (type) != REF_QUAL_NONE))
14031 {
14032 if (complain & tf_error)
14033 {
14034 if (code == POINTER_TYPE)
14035 error ("forming pointer to qualified function type %qT",
14036 type);
14037 else
14038 error ("forming reference to qualified function type %qT",
14039 type);
14040 }
14041 return error_mark_node;
14042 }
14043 else if (code == POINTER_TYPE)
14044 {
14045 r = build_pointer_type (type);
14046 if (TREE_CODE (type) == METHOD_TYPE)
14047 r = build_ptrmemfunc_type (r);
14048 }
14049 else if (TREE_CODE (type) == REFERENCE_TYPE)
14050 /* In C++0x, during template argument substitution, when there is an
14051 attempt to create a reference to a reference type, reference
14052 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14053
14054 "If a template-argument for a template-parameter T names a type
14055 that is a reference to a type A, an attempt to create the type
14056 'lvalue reference to cv T' creates the type 'lvalue reference to
14057 A,' while an attempt to create the type type rvalue reference to
14058 cv T' creates the type T"
14059 */
14060 r = cp_build_reference_type
14061 (TREE_TYPE (type),
14062 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14063 else
14064 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14065 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14066
14067 if (r != error_mark_node)
14068 /* Will this ever be needed for TYPE_..._TO values? */
14069 layout_type (r);
14070
14071 return r;
14072 }
14073 case OFFSET_TYPE:
14074 {
14075 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14076 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14077 {
14078 /* [temp.deduct]
14079
14080 Type deduction may fail for any of the following
14081 reasons:
14082
14083 -- Attempting to create "pointer to member of T" when T
14084 is not a class type. */
14085 if (complain & tf_error)
14086 error ("creating pointer to member of non-class type %qT", r);
14087 return error_mark_node;
14088 }
14089 if (TREE_CODE (type) == REFERENCE_TYPE)
14090 {
14091 if (complain & tf_error)
14092 error ("creating pointer to member reference type %qT", type);
14093 return error_mark_node;
14094 }
14095 if (VOID_TYPE_P (type))
14096 {
14097 if (complain & tf_error)
14098 error ("creating pointer to member of type void");
14099 return error_mark_node;
14100 }
14101 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14102 if (TREE_CODE (type) == FUNCTION_TYPE)
14103 {
14104 /* The type of the implicit object parameter gets its
14105 cv-qualifiers from the FUNCTION_TYPE. */
14106 tree memptr;
14107 tree method_type
14108 = build_memfn_type (type, r, type_memfn_quals (type),
14109 type_memfn_rqual (type));
14110 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14111 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14112 complain);
14113 }
14114 else
14115 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14116 cp_type_quals (t),
14117 complain);
14118 }
14119 case FUNCTION_TYPE:
14120 case METHOD_TYPE:
14121 {
14122 tree fntype;
14123 tree specs;
14124 fntype = tsubst_function_type (t, args, complain, in_decl);
14125 if (fntype == error_mark_node)
14126 return error_mark_node;
14127
14128 /* Substitute the exception specification. */
14129 specs = tsubst_exception_specification (t, args, complain, in_decl,
14130 /*defer_ok*/fndecl_type);
14131 if (specs == error_mark_node)
14132 return error_mark_node;
14133 if (specs)
14134 fntype = build_exception_variant (fntype, specs);
14135 return fntype;
14136 }
14137 case ARRAY_TYPE:
14138 {
14139 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14140 if (domain == error_mark_node)
14141 return error_mark_node;
14142
14143 /* As an optimization, we avoid regenerating the array type if
14144 it will obviously be the same as T. */
14145 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14146 return t;
14147
14148 /* These checks should match the ones in create_array_type_for_decl.
14149
14150 [temp.deduct]
14151
14152 The deduction may fail for any of the following reasons:
14153
14154 -- Attempting to create an array with an element type that
14155 is void, a function type, or a reference type, or [DR337]
14156 an abstract class type. */
14157 if (VOID_TYPE_P (type)
14158 || TREE_CODE (type) == FUNCTION_TYPE
14159 || (TREE_CODE (type) == ARRAY_TYPE
14160 && TYPE_DOMAIN (type) == NULL_TREE)
14161 || TREE_CODE (type) == REFERENCE_TYPE)
14162 {
14163 if (complain & tf_error)
14164 error ("creating array of %qT", type);
14165 return error_mark_node;
14166 }
14167
14168 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14169 return error_mark_node;
14170
14171 r = build_cplus_array_type (type, domain);
14172
14173 if (TYPE_USER_ALIGN (t))
14174 {
14175 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14176 TYPE_USER_ALIGN (r) = 1;
14177 }
14178
14179 return r;
14180 }
14181
14182 case TYPENAME_TYPE:
14183 {
14184 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14185 in_decl, /*entering_scope=*/1);
14186 if (ctx == error_mark_node)
14187 return error_mark_node;
14188
14189 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14190 complain, in_decl);
14191 if (f == error_mark_node)
14192 return error_mark_node;
14193
14194 if (!MAYBE_CLASS_TYPE_P (ctx))
14195 {
14196 if (complain & tf_error)
14197 error ("%qT is not a class, struct, or union type", ctx);
14198 return error_mark_node;
14199 }
14200 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14201 {
14202 /* Normally, make_typename_type does not require that the CTX
14203 have complete type in order to allow things like:
14204
14205 template <class T> struct S { typename S<T>::X Y; };
14206
14207 But, such constructs have already been resolved by this
14208 point, so here CTX really should have complete type, unless
14209 it's a partial instantiation. */
14210 ctx = complete_type (ctx);
14211 if (!COMPLETE_TYPE_P (ctx))
14212 {
14213 if (complain & tf_error)
14214 cxx_incomplete_type_error (NULL_TREE, ctx);
14215 return error_mark_node;
14216 }
14217 }
14218
14219 f = make_typename_type (ctx, f, typename_type,
14220 complain | tf_keep_type_decl);
14221 if (f == error_mark_node)
14222 return f;
14223 if (TREE_CODE (f) == TYPE_DECL)
14224 {
14225 complain |= tf_ignore_bad_quals;
14226 f = TREE_TYPE (f);
14227 }
14228
14229 if (TREE_CODE (f) != TYPENAME_TYPE)
14230 {
14231 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14232 {
14233 if (complain & tf_error)
14234 error ("%qT resolves to %qT, which is not an enumeration type",
14235 t, f);
14236 else
14237 return error_mark_node;
14238 }
14239 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14240 {
14241 if (complain & tf_error)
14242 error ("%qT resolves to %qT, which is is not a class type",
14243 t, f);
14244 else
14245 return error_mark_node;
14246 }
14247 }
14248
14249 return cp_build_qualified_type_real
14250 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14251 }
14252
14253 case UNBOUND_CLASS_TEMPLATE:
14254 {
14255 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14256 in_decl, /*entering_scope=*/1);
14257 tree name = TYPE_IDENTIFIER (t);
14258 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14259
14260 if (ctx == error_mark_node || name == error_mark_node)
14261 return error_mark_node;
14262
14263 if (parm_list)
14264 parm_list = tsubst_template_parms (parm_list, args, complain);
14265 return make_unbound_class_template (ctx, name, parm_list, complain);
14266 }
14267
14268 case TYPEOF_TYPE:
14269 {
14270 tree type;
14271
14272 ++cp_unevaluated_operand;
14273 ++c_inhibit_evaluation_warnings;
14274
14275 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14276 complain, in_decl,
14277 /*integral_constant_expression_p=*/false);
14278
14279 --cp_unevaluated_operand;
14280 --c_inhibit_evaluation_warnings;
14281
14282 type = finish_typeof (type);
14283 return cp_build_qualified_type_real (type,
14284 cp_type_quals (t)
14285 | cp_type_quals (type),
14286 complain);
14287 }
14288
14289 case DECLTYPE_TYPE:
14290 {
14291 tree type;
14292
14293 ++cp_unevaluated_operand;
14294 ++c_inhibit_evaluation_warnings;
14295
14296 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14297 complain|tf_decltype, in_decl,
14298 /*function_p*/false,
14299 /*integral_constant_expression*/false);
14300
14301 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14302 {
14303 if (type == NULL_TREE)
14304 {
14305 if (complain & tf_error)
14306 error ("empty initializer in lambda init-capture");
14307 type = error_mark_node;
14308 }
14309 else if (TREE_CODE (type) == TREE_LIST)
14310 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14311 }
14312
14313 --cp_unevaluated_operand;
14314 --c_inhibit_evaluation_warnings;
14315
14316 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14317 type = lambda_capture_field_type (type,
14318 DECLTYPE_FOR_INIT_CAPTURE (t),
14319 DECLTYPE_FOR_REF_CAPTURE (t));
14320 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14321 type = lambda_proxy_type (type);
14322 else
14323 {
14324 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14325 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14326 && EXPR_P (type))
14327 /* In a template ~id could be either a complement expression
14328 or an unqualified-id naming a destructor; if instantiating
14329 it produces an expression, it's not an id-expression or
14330 member access. */
14331 id = false;
14332 type = finish_decltype_type (type, id, complain);
14333 }
14334 return cp_build_qualified_type_real (type,
14335 cp_type_quals (t)
14336 | cp_type_quals (type),
14337 complain | tf_ignore_bad_quals);
14338 }
14339
14340 case UNDERLYING_TYPE:
14341 {
14342 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14343 complain, in_decl);
14344 return finish_underlying_type (type);
14345 }
14346
14347 case TYPE_ARGUMENT_PACK:
14348 case NONTYPE_ARGUMENT_PACK:
14349 {
14350 tree r;
14351
14352 if (code == NONTYPE_ARGUMENT_PACK)
14353 r = make_node (code);
14354 else
14355 r = cxx_make_type (code);
14356
14357 tree pack_args = ARGUMENT_PACK_ARGS (t);
14358 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14359 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14360
14361 return r;
14362 }
14363
14364 case VOID_CST:
14365 case INTEGER_CST:
14366 case REAL_CST:
14367 case STRING_CST:
14368 case PLUS_EXPR:
14369 case MINUS_EXPR:
14370 case NEGATE_EXPR:
14371 case NOP_EXPR:
14372 case INDIRECT_REF:
14373 case ADDR_EXPR:
14374 case CALL_EXPR:
14375 case ARRAY_REF:
14376 case SCOPE_REF:
14377 /* We should use one of the expression tsubsts for these codes. */
14378 gcc_unreachable ();
14379
14380 default:
14381 sorry ("use of %qs in template", get_tree_code_name (code));
14382 return error_mark_node;
14383 }
14384 }
14385
14386 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14387 expression on the left-hand side of the "." or "->" operator. A
14388 baselink indicates a function from a base class. Both the
14389 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14390 bases of the template class, rather than the instantiated class.
14391 In addition, lookups that were not ambiguous before may be
14392 ambiguous now. Therefore, we perform the lookup again. */
14393
14394 static tree
14395 tsubst_baselink (tree baselink, tree object_type,
14396 tree args, tsubst_flags_t complain, tree in_decl)
14397 {
14398 bool qualified = BASELINK_QUALIFIED_P (baselink);
14399
14400 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14401 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14402
14403 tree optype = BASELINK_OPTYPE (baselink);
14404 optype = tsubst (optype, args, complain, in_decl);
14405
14406 tree template_args = NULL_TREE;
14407 bool template_id_p = false;
14408 tree fns = BASELINK_FUNCTIONS (baselink);
14409 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14410 {
14411 template_id_p = true;
14412 template_args = TREE_OPERAND (fns, 1);
14413 fns = TREE_OPERAND (fns, 0);
14414 if (template_args)
14415 template_args = tsubst_template_args (template_args, args,
14416 complain, in_decl);
14417 }
14418
14419 tree name = OVL_NAME (fns);
14420 if (IDENTIFIER_CONV_OP_P (name))
14421 name = make_conv_op_name (optype);
14422
14423 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14424 if (!baselink)
14425 {
14426 if ((complain & tf_error) && constructor_name_p (name, qualifying_scope))
14427 error ("cannot call constructor %<%T::%D%> directly",
14428 qualifying_scope, name);
14429 return error_mark_node;
14430 }
14431
14432 /* If lookup found a single function, mark it as used at this point.
14433 (If it lookup found multiple functions the one selected later by
14434 overload resolution will be marked as used at that point.) */
14435 if (BASELINK_P (baselink))
14436 fns = BASELINK_FUNCTIONS (baselink);
14437 if (!template_id_p && !really_overloaded_fn (fns)
14438 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14439 return error_mark_node;
14440
14441 if (BASELINK_P (baselink))
14442 {
14443 /* Add back the template arguments, if present. */
14444 if (template_id_p)
14445 BASELINK_FUNCTIONS (baselink)
14446 = build2 (TEMPLATE_ID_EXPR, unknown_type_node,
14447 BASELINK_FUNCTIONS (baselink), template_args);
14448
14449 /* Update the conversion operator type. */
14450 BASELINK_OPTYPE (baselink) = optype;
14451 }
14452
14453 if (!object_type)
14454 object_type = current_class_type;
14455
14456 if (qualified || name == complete_dtor_identifier)
14457 {
14458 baselink = adjust_result_of_qualified_name_lookup (baselink,
14459 qualifying_scope,
14460 object_type);
14461 if (!qualified)
14462 /* We need to call adjust_result_of_qualified_name_lookup in case the
14463 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14464 so that we still get virtual function binding. */
14465 BASELINK_QUALIFIED_P (baselink) = false;
14466 }
14467
14468 return baselink;
14469 }
14470
14471 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14472 true if the qualified-id will be a postfix-expression in-and-of
14473 itself; false if more of the postfix-expression follows the
14474 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14475 of "&". */
14476
14477 static tree
14478 tsubst_qualified_id (tree qualified_id, tree args,
14479 tsubst_flags_t complain, tree in_decl,
14480 bool done, bool address_p)
14481 {
14482 tree expr;
14483 tree scope;
14484 tree name;
14485 bool is_template;
14486 tree template_args;
14487 location_t loc = UNKNOWN_LOCATION;
14488
14489 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14490
14491 /* Figure out what name to look up. */
14492 name = TREE_OPERAND (qualified_id, 1);
14493 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14494 {
14495 is_template = true;
14496 loc = EXPR_LOCATION (name);
14497 template_args = TREE_OPERAND (name, 1);
14498 if (template_args)
14499 template_args = tsubst_template_args (template_args, args,
14500 complain, in_decl);
14501 if (template_args == error_mark_node)
14502 return error_mark_node;
14503 name = TREE_OPERAND (name, 0);
14504 }
14505 else
14506 {
14507 is_template = false;
14508 template_args = NULL_TREE;
14509 }
14510
14511 /* Substitute into the qualifying scope. When there are no ARGS, we
14512 are just trying to simplify a non-dependent expression. In that
14513 case the qualifying scope may be dependent, and, in any case,
14514 substituting will not help. */
14515 scope = TREE_OPERAND (qualified_id, 0);
14516 if (args)
14517 {
14518 scope = tsubst (scope, args, complain, in_decl);
14519 expr = tsubst_copy (name, args, complain, in_decl);
14520 }
14521 else
14522 expr = name;
14523
14524 if (dependent_scope_p (scope))
14525 {
14526 if (is_template)
14527 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14528 tree r = build_qualified_name (NULL_TREE, scope, expr,
14529 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14530 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14531 return r;
14532 }
14533
14534 if (!BASELINK_P (name) && !DECL_P (expr))
14535 {
14536 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14537 {
14538 /* A BIT_NOT_EXPR is used to represent a destructor. */
14539 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14540 {
14541 error ("qualifying type %qT does not match destructor name ~%qT",
14542 scope, TREE_OPERAND (expr, 0));
14543 expr = error_mark_node;
14544 }
14545 else
14546 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14547 /*is_type_p=*/0, false);
14548 }
14549 else
14550 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14551 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14552 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14553 {
14554 if (complain & tf_error)
14555 {
14556 error ("dependent-name %qE is parsed as a non-type, but "
14557 "instantiation yields a type", qualified_id);
14558 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14559 }
14560 return error_mark_node;
14561 }
14562 }
14563
14564 if (DECL_P (expr))
14565 {
14566 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14567 scope);
14568 /* Remember that there was a reference to this entity. */
14569 if (!mark_used (expr, complain) && !(complain & tf_error))
14570 return error_mark_node;
14571 }
14572
14573 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14574 {
14575 if (complain & tf_error)
14576 qualified_name_lookup_error (scope,
14577 TREE_OPERAND (qualified_id, 1),
14578 expr, input_location);
14579 return error_mark_node;
14580 }
14581
14582 if (is_template)
14583 {
14584 if (variable_template_p (expr))
14585 expr = lookup_and_finish_template_variable (expr, template_args,
14586 complain);
14587 else
14588 expr = lookup_template_function (expr, template_args);
14589 }
14590
14591 if (expr == error_mark_node && complain & tf_error)
14592 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14593 expr, input_location);
14594 else if (TYPE_P (scope))
14595 {
14596 expr = (adjust_result_of_qualified_name_lookup
14597 (expr, scope, current_nonlambda_class_type ()));
14598 expr = (finish_qualified_id_expr
14599 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14600 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14601 /*template_arg_p=*/false, complain));
14602 }
14603
14604 /* Expressions do not generally have reference type. */
14605 if (TREE_CODE (expr) != SCOPE_REF
14606 /* However, if we're about to form a pointer-to-member, we just
14607 want the referenced member referenced. */
14608 && TREE_CODE (expr) != OFFSET_REF)
14609 expr = convert_from_reference (expr);
14610
14611 if (REF_PARENTHESIZED_P (qualified_id))
14612 expr = force_paren_expr (expr);
14613
14614 return expr;
14615 }
14616
14617 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14618 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14619 for tsubst. */
14620
14621 static tree
14622 tsubst_init (tree init, tree decl, tree args,
14623 tsubst_flags_t complain, tree in_decl)
14624 {
14625 if (!init)
14626 return NULL_TREE;
14627
14628 init = tsubst_expr (init, args, complain, in_decl, false);
14629
14630 if (!init && TREE_TYPE (decl) != error_mark_node)
14631 {
14632 /* If we had an initializer but it
14633 instantiated to nothing,
14634 value-initialize the object. This will
14635 only occur when the initializer was a
14636 pack expansion where the parameter packs
14637 used in that expansion were of length
14638 zero. */
14639 init = build_value_init (TREE_TYPE (decl),
14640 complain);
14641 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14642 init = get_target_expr_sfinae (init, complain);
14643 if (TREE_CODE (init) == TARGET_EXPR)
14644 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14645 }
14646
14647 return init;
14648 }
14649
14650 /* Like tsubst, but deals with expressions. This function just replaces
14651 template parms; to finish processing the resultant expression, use
14652 tsubst_copy_and_build or tsubst_expr. */
14653
14654 static tree
14655 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14656 {
14657 enum tree_code code;
14658 tree r;
14659
14660 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14661 return t;
14662
14663 code = TREE_CODE (t);
14664
14665 switch (code)
14666 {
14667 case PARM_DECL:
14668 r = retrieve_local_specialization (t);
14669
14670 if (r == NULL_TREE)
14671 {
14672 /* We get here for a use of 'this' in an NSDMI. */
14673 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14674 return current_class_ptr;
14675
14676 /* This can happen for a parameter name used later in a function
14677 declaration (such as in a late-specified return type). Just
14678 make a dummy decl, since it's only used for its type. */
14679 gcc_assert (cp_unevaluated_operand != 0);
14680 r = tsubst_decl (t, args, complain);
14681 /* Give it the template pattern as its context; its true context
14682 hasn't been instantiated yet and this is good enough for
14683 mangling. */
14684 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14685 }
14686
14687 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14688 r = ARGUMENT_PACK_SELECT_ARG (r);
14689 if (!mark_used (r, complain) && !(complain & tf_error))
14690 return error_mark_node;
14691 return r;
14692
14693 case CONST_DECL:
14694 {
14695 tree enum_type;
14696 tree v;
14697
14698 if (DECL_TEMPLATE_PARM_P (t))
14699 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14700 /* There is no need to substitute into namespace-scope
14701 enumerators. */
14702 if (DECL_NAMESPACE_SCOPE_P (t))
14703 return t;
14704 /* If ARGS is NULL, then T is known to be non-dependent. */
14705 if (args == NULL_TREE)
14706 return scalar_constant_value (t);
14707
14708 /* Unfortunately, we cannot just call lookup_name here.
14709 Consider:
14710
14711 template <int I> int f() {
14712 enum E { a = I };
14713 struct S { void g() { E e = a; } };
14714 };
14715
14716 When we instantiate f<7>::S::g(), say, lookup_name is not
14717 clever enough to find f<7>::a. */
14718 enum_type
14719 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14720 /*entering_scope=*/0);
14721
14722 for (v = TYPE_VALUES (enum_type);
14723 v != NULL_TREE;
14724 v = TREE_CHAIN (v))
14725 if (TREE_PURPOSE (v) == DECL_NAME (t))
14726 return TREE_VALUE (v);
14727
14728 /* We didn't find the name. That should never happen; if
14729 name-lookup found it during preliminary parsing, we
14730 should find it again here during instantiation. */
14731 gcc_unreachable ();
14732 }
14733 return t;
14734
14735 case FIELD_DECL:
14736 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14737 {
14738 /* Check for a local specialization set up by
14739 tsubst_pack_expansion. */
14740 if (tree r = retrieve_local_specialization (t))
14741 {
14742 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14743 r = ARGUMENT_PACK_SELECT_ARG (r);
14744 return r;
14745 }
14746
14747 /* When retrieving a capture pack from a generic lambda, remove the
14748 lambda call op's own template argument list from ARGS. Only the
14749 template arguments active for the closure type should be used to
14750 retrieve the pack specialization. */
14751 if (LAMBDA_FUNCTION_P (current_function_decl)
14752 && (template_class_depth (DECL_CONTEXT (t))
14753 != TMPL_ARGS_DEPTH (args)))
14754 args = strip_innermost_template_args (args, 1);
14755
14756 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14757 tsubst_decl put in the hash table. */
14758 return retrieve_specialization (t, args, 0);
14759 }
14760
14761 if (DECL_CONTEXT (t))
14762 {
14763 tree ctx;
14764
14765 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14766 /*entering_scope=*/1);
14767 if (ctx != DECL_CONTEXT (t))
14768 {
14769 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14770 if (!r)
14771 {
14772 if (complain & tf_error)
14773 error ("using invalid field %qD", t);
14774 return error_mark_node;
14775 }
14776 return r;
14777 }
14778 }
14779
14780 return t;
14781
14782 case VAR_DECL:
14783 case FUNCTION_DECL:
14784 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14785 r = tsubst (t, args, complain, in_decl);
14786 else if (local_variable_p (t)
14787 && uses_template_parms (DECL_CONTEXT (t)))
14788 {
14789 r = retrieve_local_specialization (t);
14790 if (r == NULL_TREE)
14791 {
14792 /* First try name lookup to find the instantiation. */
14793 r = lookup_name (DECL_NAME (t));
14794 if (r && !is_capture_proxy (r))
14795 {
14796 /* Make sure that the one we found is the one we want. */
14797 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
14798 if (ctx != DECL_CONTEXT (r))
14799 r = NULL_TREE;
14800 }
14801
14802 if (r)
14803 /* OK */;
14804 else
14805 {
14806 /* This can happen for a variable used in a
14807 late-specified return type of a local lambda, or for a
14808 local static or constant. Building a new VAR_DECL
14809 should be OK in all those cases. */
14810 r = tsubst_decl (t, args, complain);
14811 if (local_specializations)
14812 /* Avoid infinite recursion (79640). */
14813 register_local_specialization (r, t);
14814 if (decl_maybe_constant_var_p (r))
14815 {
14816 /* We can't call cp_finish_decl, so handle the
14817 initializer by hand. */
14818 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14819 complain, in_decl);
14820 if (!processing_template_decl)
14821 init = maybe_constant_init (init);
14822 if (processing_template_decl
14823 ? potential_constant_expression (init)
14824 : reduced_constant_expression_p (init))
14825 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14826 = TREE_CONSTANT (r) = true;
14827 DECL_INITIAL (r) = init;
14828 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14829 TREE_TYPE (r)
14830 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14831 complain, adc_variable_type);
14832 }
14833 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14834 || decl_constant_var_p (r)
14835 || errorcount || sorrycount);
14836 if (!processing_template_decl
14837 && !TREE_STATIC (r))
14838 r = process_outer_var_ref (r, complain);
14839 }
14840 /* Remember this for subsequent uses. */
14841 if (local_specializations)
14842 register_local_specialization (r, t);
14843 }
14844 }
14845 else
14846 r = t;
14847 if (!mark_used (r, complain))
14848 return error_mark_node;
14849 return r;
14850
14851 case NAMESPACE_DECL:
14852 return t;
14853
14854 case OVERLOAD:
14855 /* An OVERLOAD will always be a non-dependent overload set; an
14856 overload set from function scope will just be represented with an
14857 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14858 gcc_assert (!uses_template_parms (t));
14859 /* We must have marked any lookups as persistent. */
14860 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14861 return t;
14862
14863 case BASELINK:
14864 return tsubst_baselink (t, current_nonlambda_class_type (),
14865 args, complain, in_decl);
14866
14867 case TEMPLATE_DECL:
14868 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14869 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14870 args, complain, in_decl);
14871 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14872 return tsubst (t, args, complain, in_decl);
14873 else if (DECL_CLASS_SCOPE_P (t)
14874 && uses_template_parms (DECL_CONTEXT (t)))
14875 {
14876 /* Template template argument like the following example need
14877 special treatment:
14878
14879 template <template <class> class TT> struct C {};
14880 template <class T> struct D {
14881 template <class U> struct E {};
14882 C<E> c; // #1
14883 };
14884 D<int> d; // #2
14885
14886 We are processing the template argument `E' in #1 for
14887 the template instantiation #2. Originally, `E' is a
14888 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14889 have to substitute this with one having context `D<int>'. */
14890
14891 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14892 if (dependent_scope_p (context))
14893 {
14894 /* When rewriting a constructor into a deduction guide, a
14895 non-dependent name can become dependent, so memtmpl<args>
14896 becomes context::template memtmpl<args>. */
14897 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14898 return build_qualified_name (type, context, DECL_NAME (t),
14899 /*template*/true);
14900 }
14901 return lookup_field (context, DECL_NAME(t), 0, false);
14902 }
14903 else
14904 /* Ordinary template template argument. */
14905 return t;
14906
14907 case CAST_EXPR:
14908 case REINTERPRET_CAST_EXPR:
14909 case CONST_CAST_EXPR:
14910 case STATIC_CAST_EXPR:
14911 case DYNAMIC_CAST_EXPR:
14912 case IMPLICIT_CONV_EXPR:
14913 case CONVERT_EXPR:
14914 case NOP_EXPR:
14915 {
14916 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14917 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14918 return build1 (code, type, op0);
14919 }
14920
14921 case SIZEOF_EXPR:
14922 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14923 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14924 {
14925 tree expanded, op = TREE_OPERAND (t, 0);
14926 int len = 0;
14927
14928 if (SIZEOF_EXPR_TYPE_P (t))
14929 op = TREE_TYPE (op);
14930
14931 ++cp_unevaluated_operand;
14932 ++c_inhibit_evaluation_warnings;
14933 /* We only want to compute the number of arguments. */
14934 if (PACK_EXPANSION_P (op))
14935 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14936 else
14937 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14938 args, complain, in_decl);
14939 --cp_unevaluated_operand;
14940 --c_inhibit_evaluation_warnings;
14941
14942 if (TREE_CODE (expanded) == TREE_VEC)
14943 {
14944 len = TREE_VEC_LENGTH (expanded);
14945 /* Set TREE_USED for the benefit of -Wunused. */
14946 for (int i = 0; i < len; i++)
14947 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14948 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14949 }
14950
14951 if (expanded == error_mark_node)
14952 return error_mark_node;
14953 else if (PACK_EXPANSION_P (expanded)
14954 || (TREE_CODE (expanded) == TREE_VEC
14955 && pack_expansion_args_count (expanded)))
14956
14957 {
14958 if (PACK_EXPANSION_P (expanded))
14959 /* OK. */;
14960 else if (TREE_VEC_LENGTH (expanded) == 1)
14961 expanded = TREE_VEC_ELT (expanded, 0);
14962 else
14963 expanded = make_argument_pack (expanded);
14964
14965 if (TYPE_P (expanded))
14966 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14967 complain & tf_error);
14968 else
14969 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14970 complain & tf_error);
14971 }
14972 else
14973 return build_int_cst (size_type_node, len);
14974 }
14975 if (SIZEOF_EXPR_TYPE_P (t))
14976 {
14977 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14978 args, complain, in_decl);
14979 r = build1 (NOP_EXPR, r, error_mark_node);
14980 r = build1 (SIZEOF_EXPR,
14981 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14982 SIZEOF_EXPR_TYPE_P (r) = 1;
14983 return r;
14984 }
14985 /* Fall through */
14986
14987 case INDIRECT_REF:
14988 case NEGATE_EXPR:
14989 case TRUTH_NOT_EXPR:
14990 case BIT_NOT_EXPR:
14991 case ADDR_EXPR:
14992 case UNARY_PLUS_EXPR: /* Unary + */
14993 case ALIGNOF_EXPR:
14994 case AT_ENCODE_EXPR:
14995 case ARROW_EXPR:
14996 case THROW_EXPR:
14997 case TYPEID_EXPR:
14998 case REALPART_EXPR:
14999 case IMAGPART_EXPR:
15000 case PAREN_EXPR:
15001 {
15002 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15003 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15004 return build1 (code, type, op0);
15005 }
15006
15007 case COMPONENT_REF:
15008 {
15009 tree object;
15010 tree name;
15011
15012 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15013 name = TREE_OPERAND (t, 1);
15014 if (TREE_CODE (name) == BIT_NOT_EXPR)
15015 {
15016 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15017 complain, in_decl);
15018 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15019 }
15020 else if (TREE_CODE (name) == SCOPE_REF
15021 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15022 {
15023 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15024 complain, in_decl);
15025 name = TREE_OPERAND (name, 1);
15026 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15027 complain, in_decl);
15028 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15029 name = build_qualified_name (/*type=*/NULL_TREE,
15030 base, name,
15031 /*template_p=*/false);
15032 }
15033 else if (BASELINK_P (name))
15034 name = tsubst_baselink (name,
15035 non_reference (TREE_TYPE (object)),
15036 args, complain,
15037 in_decl);
15038 else
15039 name = tsubst_copy (name, args, complain, in_decl);
15040 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15041 }
15042
15043 case PLUS_EXPR:
15044 case MINUS_EXPR:
15045 case MULT_EXPR:
15046 case TRUNC_DIV_EXPR:
15047 case CEIL_DIV_EXPR:
15048 case FLOOR_DIV_EXPR:
15049 case ROUND_DIV_EXPR:
15050 case EXACT_DIV_EXPR:
15051 case BIT_AND_EXPR:
15052 case BIT_IOR_EXPR:
15053 case BIT_XOR_EXPR:
15054 case TRUNC_MOD_EXPR:
15055 case FLOOR_MOD_EXPR:
15056 case TRUTH_ANDIF_EXPR:
15057 case TRUTH_ORIF_EXPR:
15058 case TRUTH_AND_EXPR:
15059 case TRUTH_OR_EXPR:
15060 case RSHIFT_EXPR:
15061 case LSHIFT_EXPR:
15062 case RROTATE_EXPR:
15063 case LROTATE_EXPR:
15064 case EQ_EXPR:
15065 case NE_EXPR:
15066 case MAX_EXPR:
15067 case MIN_EXPR:
15068 case LE_EXPR:
15069 case GE_EXPR:
15070 case LT_EXPR:
15071 case GT_EXPR:
15072 case COMPOUND_EXPR:
15073 case DOTSTAR_EXPR:
15074 case MEMBER_REF:
15075 case PREDECREMENT_EXPR:
15076 case PREINCREMENT_EXPR:
15077 case POSTDECREMENT_EXPR:
15078 case POSTINCREMENT_EXPR:
15079 {
15080 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15081 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15082 return build_nt (code, op0, op1);
15083 }
15084
15085 case SCOPE_REF:
15086 {
15087 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15088 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15089 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15090 QUALIFIED_NAME_IS_TEMPLATE (t));
15091 }
15092
15093 case ARRAY_REF:
15094 {
15095 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15096 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15097 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15098 }
15099
15100 case CALL_EXPR:
15101 {
15102 int n = VL_EXP_OPERAND_LENGTH (t);
15103 tree result = build_vl_exp (CALL_EXPR, n);
15104 int i;
15105 for (i = 0; i < n; i++)
15106 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15107 complain, in_decl);
15108 return result;
15109 }
15110
15111 case COND_EXPR:
15112 case MODOP_EXPR:
15113 case PSEUDO_DTOR_EXPR:
15114 case VEC_PERM_EXPR:
15115 {
15116 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15117 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15118 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15119 r = build_nt (code, op0, op1, op2);
15120 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15121 return r;
15122 }
15123
15124 case NEW_EXPR:
15125 {
15126 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15127 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15128 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15129 r = build_nt (code, op0, op1, op2);
15130 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15131 return r;
15132 }
15133
15134 case DELETE_EXPR:
15135 {
15136 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15137 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15138 r = build_nt (code, op0, op1);
15139 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15140 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15141 return r;
15142 }
15143
15144 case TEMPLATE_ID_EXPR:
15145 {
15146 /* Substituted template arguments */
15147 tree fn = TREE_OPERAND (t, 0);
15148 tree targs = TREE_OPERAND (t, 1);
15149
15150 fn = tsubst_copy (fn, args, complain, in_decl);
15151 if (targs)
15152 targs = tsubst_template_args (targs, args, complain, in_decl);
15153
15154 return lookup_template_function (fn, targs);
15155 }
15156
15157 case TREE_LIST:
15158 {
15159 tree purpose, value, chain;
15160
15161 if (t == void_list_node)
15162 return t;
15163
15164 purpose = TREE_PURPOSE (t);
15165 if (purpose)
15166 purpose = tsubst_copy (purpose, args, complain, in_decl);
15167 value = TREE_VALUE (t);
15168 if (value)
15169 value = tsubst_copy (value, args, complain, in_decl);
15170 chain = TREE_CHAIN (t);
15171 if (chain && chain != void_type_node)
15172 chain = tsubst_copy (chain, args, complain, in_decl);
15173 if (purpose == TREE_PURPOSE (t)
15174 && value == TREE_VALUE (t)
15175 && chain == TREE_CHAIN (t))
15176 return t;
15177 return tree_cons (purpose, value, chain);
15178 }
15179
15180 case RECORD_TYPE:
15181 case UNION_TYPE:
15182 case ENUMERAL_TYPE:
15183 case INTEGER_TYPE:
15184 case TEMPLATE_TYPE_PARM:
15185 case TEMPLATE_TEMPLATE_PARM:
15186 case BOUND_TEMPLATE_TEMPLATE_PARM:
15187 case TEMPLATE_PARM_INDEX:
15188 case POINTER_TYPE:
15189 case REFERENCE_TYPE:
15190 case OFFSET_TYPE:
15191 case FUNCTION_TYPE:
15192 case METHOD_TYPE:
15193 case ARRAY_TYPE:
15194 case TYPENAME_TYPE:
15195 case UNBOUND_CLASS_TEMPLATE:
15196 case TYPEOF_TYPE:
15197 case DECLTYPE_TYPE:
15198 case TYPE_DECL:
15199 return tsubst (t, args, complain, in_decl);
15200
15201 case USING_DECL:
15202 t = DECL_NAME (t);
15203 /* Fall through. */
15204 case IDENTIFIER_NODE:
15205 if (IDENTIFIER_CONV_OP_P (t))
15206 {
15207 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15208 return make_conv_op_name (new_type);
15209 }
15210 else
15211 return t;
15212
15213 case CONSTRUCTOR:
15214 /* This is handled by tsubst_copy_and_build. */
15215 gcc_unreachable ();
15216
15217 case VA_ARG_EXPR:
15218 {
15219 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15220 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15221 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15222 }
15223
15224 case CLEANUP_POINT_EXPR:
15225 /* We shouldn't have built any of these during initial template
15226 generation. Instead, they should be built during instantiation
15227 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15228 gcc_unreachable ();
15229
15230 case OFFSET_REF:
15231 {
15232 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15233 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15234 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15235 r = build2 (code, type, op0, op1);
15236 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15237 if (!mark_used (TREE_OPERAND (r, 1), complain)
15238 && !(complain & tf_error))
15239 return error_mark_node;
15240 return r;
15241 }
15242
15243 case EXPR_PACK_EXPANSION:
15244 error ("invalid use of pack expansion expression");
15245 return error_mark_node;
15246
15247 case NONTYPE_ARGUMENT_PACK:
15248 error ("use %<...%> to expand argument pack");
15249 return error_mark_node;
15250
15251 case VOID_CST:
15252 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15253 return t;
15254
15255 case INTEGER_CST:
15256 case REAL_CST:
15257 case STRING_CST:
15258 case COMPLEX_CST:
15259 {
15260 /* Instantiate any typedefs in the type. */
15261 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15262 r = fold_convert (type, t);
15263 gcc_assert (TREE_CODE (r) == code);
15264 return r;
15265 }
15266
15267 case PTRMEM_CST:
15268 /* These can sometimes show up in a partial instantiation, but never
15269 involve template parms. */
15270 gcc_assert (!uses_template_parms (t));
15271 return t;
15272
15273 case UNARY_LEFT_FOLD_EXPR:
15274 return tsubst_unary_left_fold (t, args, complain, in_decl);
15275 case UNARY_RIGHT_FOLD_EXPR:
15276 return tsubst_unary_right_fold (t, args, complain, in_decl);
15277 case BINARY_LEFT_FOLD_EXPR:
15278 return tsubst_binary_left_fold (t, args, complain, in_decl);
15279 case BINARY_RIGHT_FOLD_EXPR:
15280 return tsubst_binary_right_fold (t, args, complain, in_decl);
15281 case PREDICT_EXPR:
15282 return t;
15283
15284 default:
15285 /* We shouldn't get here, but keep going if !flag_checking. */
15286 if (flag_checking)
15287 gcc_unreachable ();
15288 return t;
15289 }
15290 }
15291
15292 /* Helper function for tsubst_omp_clauses, used for instantiation of
15293 OMP_CLAUSE_DECL of clauses. */
15294
15295 static tree
15296 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15297 tree in_decl)
15298 {
15299 if (decl == NULL_TREE)
15300 return NULL_TREE;
15301
15302 /* Handle an OpenMP array section represented as a TREE_LIST (or
15303 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15304 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15305 TREE_LIST. We can handle it exactly the same as an array section
15306 (purpose, value, and a chain), even though the nomenclature
15307 (low_bound, length, etc) is different. */
15308 if (TREE_CODE (decl) == TREE_LIST)
15309 {
15310 tree low_bound
15311 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15312 /*integral_constant_expression_p=*/false);
15313 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15314 /*integral_constant_expression_p=*/false);
15315 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15316 in_decl);
15317 if (TREE_PURPOSE (decl) == low_bound
15318 && TREE_VALUE (decl) == length
15319 && TREE_CHAIN (decl) == chain)
15320 return decl;
15321 tree ret = tree_cons (low_bound, length, chain);
15322 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15323 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15324 return ret;
15325 }
15326 tree ret = tsubst_expr (decl, args, complain, in_decl,
15327 /*integral_constant_expression_p=*/false);
15328 /* Undo convert_from_reference tsubst_expr could have called. */
15329 if (decl
15330 && REFERENCE_REF_P (ret)
15331 && !REFERENCE_REF_P (decl))
15332 ret = TREE_OPERAND (ret, 0);
15333 return ret;
15334 }
15335
15336 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15337
15338 static tree
15339 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15340 tree args, tsubst_flags_t complain, tree in_decl)
15341 {
15342 tree new_clauses = NULL_TREE, nc, oc;
15343 tree linear_no_step = NULL_TREE;
15344
15345 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15346 {
15347 nc = copy_node (oc);
15348 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15349 new_clauses = nc;
15350
15351 switch (OMP_CLAUSE_CODE (nc))
15352 {
15353 case OMP_CLAUSE_LASTPRIVATE:
15354 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15355 {
15356 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15357 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15358 in_decl, /*integral_constant_expression_p=*/false);
15359 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15360 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15361 }
15362 /* FALLTHRU */
15363 case OMP_CLAUSE_PRIVATE:
15364 case OMP_CLAUSE_SHARED:
15365 case OMP_CLAUSE_FIRSTPRIVATE:
15366 case OMP_CLAUSE_COPYIN:
15367 case OMP_CLAUSE_COPYPRIVATE:
15368 case OMP_CLAUSE_UNIFORM:
15369 case OMP_CLAUSE_DEPEND:
15370 case OMP_CLAUSE_FROM:
15371 case OMP_CLAUSE_TO:
15372 case OMP_CLAUSE_MAP:
15373 case OMP_CLAUSE_USE_DEVICE_PTR:
15374 case OMP_CLAUSE_IS_DEVICE_PTR:
15375 OMP_CLAUSE_DECL (nc)
15376 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15377 in_decl);
15378 break;
15379 case OMP_CLAUSE_TILE:
15380 case OMP_CLAUSE_IF:
15381 case OMP_CLAUSE_NUM_THREADS:
15382 case OMP_CLAUSE_SCHEDULE:
15383 case OMP_CLAUSE_COLLAPSE:
15384 case OMP_CLAUSE_FINAL:
15385 case OMP_CLAUSE_DEVICE:
15386 case OMP_CLAUSE_DIST_SCHEDULE:
15387 case OMP_CLAUSE_NUM_TEAMS:
15388 case OMP_CLAUSE_THREAD_LIMIT:
15389 case OMP_CLAUSE_SAFELEN:
15390 case OMP_CLAUSE_SIMDLEN:
15391 case OMP_CLAUSE_NUM_TASKS:
15392 case OMP_CLAUSE_GRAINSIZE:
15393 case OMP_CLAUSE_PRIORITY:
15394 case OMP_CLAUSE_ORDERED:
15395 case OMP_CLAUSE_HINT:
15396 case OMP_CLAUSE_NUM_GANGS:
15397 case OMP_CLAUSE_NUM_WORKERS:
15398 case OMP_CLAUSE_VECTOR_LENGTH:
15399 case OMP_CLAUSE_WORKER:
15400 case OMP_CLAUSE_VECTOR:
15401 case OMP_CLAUSE_ASYNC:
15402 case OMP_CLAUSE_WAIT:
15403 OMP_CLAUSE_OPERAND (nc, 0)
15404 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15405 in_decl, /*integral_constant_expression_p=*/false);
15406 break;
15407 case OMP_CLAUSE_REDUCTION:
15408 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15409 {
15410 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15411 if (TREE_CODE (placeholder) == SCOPE_REF)
15412 {
15413 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15414 complain, in_decl);
15415 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15416 = build_qualified_name (NULL_TREE, scope,
15417 TREE_OPERAND (placeholder, 1),
15418 false);
15419 }
15420 else
15421 gcc_assert (identifier_p (placeholder));
15422 }
15423 OMP_CLAUSE_DECL (nc)
15424 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15425 in_decl);
15426 break;
15427 case OMP_CLAUSE_GANG:
15428 case OMP_CLAUSE_ALIGNED:
15429 OMP_CLAUSE_DECL (nc)
15430 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15431 in_decl);
15432 OMP_CLAUSE_OPERAND (nc, 1)
15433 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15434 in_decl, /*integral_constant_expression_p=*/false);
15435 break;
15436 case OMP_CLAUSE_LINEAR:
15437 OMP_CLAUSE_DECL (nc)
15438 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15439 in_decl);
15440 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15441 {
15442 gcc_assert (!linear_no_step);
15443 linear_no_step = nc;
15444 }
15445 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15446 OMP_CLAUSE_LINEAR_STEP (nc)
15447 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15448 complain, in_decl);
15449 else
15450 OMP_CLAUSE_LINEAR_STEP (nc)
15451 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15452 in_decl,
15453 /*integral_constant_expression_p=*/false);
15454 break;
15455 case OMP_CLAUSE_NOWAIT:
15456 case OMP_CLAUSE_DEFAULT:
15457 case OMP_CLAUSE_UNTIED:
15458 case OMP_CLAUSE_MERGEABLE:
15459 case OMP_CLAUSE_INBRANCH:
15460 case OMP_CLAUSE_NOTINBRANCH:
15461 case OMP_CLAUSE_PROC_BIND:
15462 case OMP_CLAUSE_FOR:
15463 case OMP_CLAUSE_PARALLEL:
15464 case OMP_CLAUSE_SECTIONS:
15465 case OMP_CLAUSE_TASKGROUP:
15466 case OMP_CLAUSE_NOGROUP:
15467 case OMP_CLAUSE_THREADS:
15468 case OMP_CLAUSE_SIMD:
15469 case OMP_CLAUSE_DEFAULTMAP:
15470 case OMP_CLAUSE_INDEPENDENT:
15471 case OMP_CLAUSE_AUTO:
15472 case OMP_CLAUSE_SEQ:
15473 break;
15474 default:
15475 gcc_unreachable ();
15476 }
15477 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15478 switch (OMP_CLAUSE_CODE (nc))
15479 {
15480 case OMP_CLAUSE_SHARED:
15481 case OMP_CLAUSE_PRIVATE:
15482 case OMP_CLAUSE_FIRSTPRIVATE:
15483 case OMP_CLAUSE_LASTPRIVATE:
15484 case OMP_CLAUSE_COPYPRIVATE:
15485 case OMP_CLAUSE_LINEAR:
15486 case OMP_CLAUSE_REDUCTION:
15487 case OMP_CLAUSE_USE_DEVICE_PTR:
15488 case OMP_CLAUSE_IS_DEVICE_PTR:
15489 /* tsubst_expr on SCOPE_REF results in returning
15490 finish_non_static_data_member result. Undo that here. */
15491 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15492 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15493 == IDENTIFIER_NODE))
15494 {
15495 tree t = OMP_CLAUSE_DECL (nc);
15496 tree v = t;
15497 while (v)
15498 switch (TREE_CODE (v))
15499 {
15500 case COMPONENT_REF:
15501 case MEM_REF:
15502 case INDIRECT_REF:
15503 CASE_CONVERT:
15504 case POINTER_PLUS_EXPR:
15505 v = TREE_OPERAND (v, 0);
15506 continue;
15507 case PARM_DECL:
15508 if (DECL_CONTEXT (v) == current_function_decl
15509 && DECL_ARTIFICIAL (v)
15510 && DECL_NAME (v) == this_identifier)
15511 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15512 /* FALLTHRU */
15513 default:
15514 v = NULL_TREE;
15515 break;
15516 }
15517 }
15518 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15519 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15520 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15521 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15522 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15523 {
15524 tree decl = OMP_CLAUSE_DECL (nc);
15525 if (VAR_P (decl))
15526 {
15527 retrofit_lang_decl (decl);
15528 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15529 }
15530 }
15531 break;
15532 default:
15533 break;
15534 }
15535 }
15536
15537 new_clauses = nreverse (new_clauses);
15538 if (ort != C_ORT_OMP_DECLARE_SIMD)
15539 {
15540 new_clauses = finish_omp_clauses (new_clauses, ort);
15541 if (linear_no_step)
15542 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15543 if (nc == linear_no_step)
15544 {
15545 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15546 break;
15547 }
15548 }
15549 return new_clauses;
15550 }
15551
15552 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15553
15554 static tree
15555 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15556 tree in_decl)
15557 {
15558 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15559
15560 tree purpose, value, chain;
15561
15562 if (t == NULL)
15563 return t;
15564
15565 if (TREE_CODE (t) != TREE_LIST)
15566 return tsubst_copy_and_build (t, args, complain, in_decl,
15567 /*function_p=*/false,
15568 /*integral_constant_expression_p=*/false);
15569
15570 if (t == void_list_node)
15571 return t;
15572
15573 purpose = TREE_PURPOSE (t);
15574 if (purpose)
15575 purpose = RECUR (purpose);
15576 value = TREE_VALUE (t);
15577 if (value)
15578 {
15579 if (TREE_CODE (value) != LABEL_DECL)
15580 value = RECUR (value);
15581 else
15582 {
15583 value = lookup_label (DECL_NAME (value));
15584 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15585 TREE_USED (value) = 1;
15586 }
15587 }
15588 chain = TREE_CHAIN (t);
15589 if (chain && chain != void_type_node)
15590 chain = RECUR (chain);
15591 return tree_cons (purpose, value, chain);
15592 #undef RECUR
15593 }
15594
15595 /* Used to temporarily communicate the list of #pragma omp parallel
15596 clauses to #pragma omp for instantiation if they are combined
15597 together. */
15598
15599 static tree *omp_parallel_combined_clauses;
15600
15601 /* Substitute one OMP_FOR iterator. */
15602
15603 static void
15604 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15605 tree initv, tree condv, tree incrv, tree *clauses,
15606 tree args, tsubst_flags_t complain, tree in_decl,
15607 bool integral_constant_expression_p)
15608 {
15609 #define RECUR(NODE) \
15610 tsubst_expr ((NODE), args, complain, in_decl, \
15611 integral_constant_expression_p)
15612 tree decl, init, cond, incr;
15613
15614 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15615 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15616
15617 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15618 {
15619 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15620 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15621 }
15622
15623 decl = TREE_OPERAND (init, 0);
15624 init = TREE_OPERAND (init, 1);
15625 tree decl_expr = NULL_TREE;
15626 if (init && TREE_CODE (init) == DECL_EXPR)
15627 {
15628 /* We need to jump through some hoops to handle declarations in the
15629 init-statement, since we might need to handle auto deduction,
15630 but we need to keep control of initialization. */
15631 decl_expr = init;
15632 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15633 decl = tsubst_decl (decl, args, complain);
15634 }
15635 else
15636 {
15637 if (TREE_CODE (decl) == SCOPE_REF)
15638 {
15639 decl = RECUR (decl);
15640 if (TREE_CODE (decl) == COMPONENT_REF)
15641 {
15642 tree v = decl;
15643 while (v)
15644 switch (TREE_CODE (v))
15645 {
15646 case COMPONENT_REF:
15647 case MEM_REF:
15648 case INDIRECT_REF:
15649 CASE_CONVERT:
15650 case POINTER_PLUS_EXPR:
15651 v = TREE_OPERAND (v, 0);
15652 continue;
15653 case PARM_DECL:
15654 if (DECL_CONTEXT (v) == current_function_decl
15655 && DECL_ARTIFICIAL (v)
15656 && DECL_NAME (v) == this_identifier)
15657 {
15658 decl = TREE_OPERAND (decl, 1);
15659 decl = omp_privatize_field (decl, false);
15660 }
15661 /* FALLTHRU */
15662 default:
15663 v = NULL_TREE;
15664 break;
15665 }
15666 }
15667 }
15668 else
15669 decl = RECUR (decl);
15670 }
15671 init = RECUR (init);
15672
15673 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15674 if (auto_node && init)
15675 TREE_TYPE (decl)
15676 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15677
15678 gcc_assert (!type_dependent_expression_p (decl));
15679
15680 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15681 {
15682 if (decl_expr)
15683 {
15684 /* Declare the variable, but don't let that initialize it. */
15685 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15686 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15687 RECUR (decl_expr);
15688 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15689 }
15690
15691 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15692 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15693 if (TREE_CODE (incr) == MODIFY_EXPR)
15694 {
15695 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15696 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15697 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15698 NOP_EXPR, rhs, complain);
15699 }
15700 else
15701 incr = RECUR (incr);
15702 TREE_VEC_ELT (declv, i) = decl;
15703 TREE_VEC_ELT (initv, i) = init;
15704 TREE_VEC_ELT (condv, i) = cond;
15705 TREE_VEC_ELT (incrv, i) = incr;
15706 return;
15707 }
15708
15709 if (decl_expr)
15710 {
15711 /* Declare and initialize the variable. */
15712 RECUR (decl_expr);
15713 init = NULL_TREE;
15714 }
15715 else if (init)
15716 {
15717 tree *pc;
15718 int j;
15719 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15720 {
15721 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15722 {
15723 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15724 && OMP_CLAUSE_DECL (*pc) == decl)
15725 break;
15726 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15727 && OMP_CLAUSE_DECL (*pc) == decl)
15728 {
15729 if (j)
15730 break;
15731 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15732 tree c = *pc;
15733 *pc = OMP_CLAUSE_CHAIN (c);
15734 OMP_CLAUSE_CHAIN (c) = *clauses;
15735 *clauses = c;
15736 }
15737 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15738 && OMP_CLAUSE_DECL (*pc) == decl)
15739 {
15740 error ("iteration variable %qD should not be firstprivate",
15741 decl);
15742 *pc = OMP_CLAUSE_CHAIN (*pc);
15743 }
15744 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15745 && OMP_CLAUSE_DECL (*pc) == decl)
15746 {
15747 error ("iteration variable %qD should not be reduction",
15748 decl);
15749 *pc = OMP_CLAUSE_CHAIN (*pc);
15750 }
15751 else
15752 pc = &OMP_CLAUSE_CHAIN (*pc);
15753 }
15754 if (*pc)
15755 break;
15756 }
15757 if (*pc == NULL_TREE)
15758 {
15759 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15760 OMP_CLAUSE_DECL (c) = decl;
15761 c = finish_omp_clauses (c, C_ORT_OMP);
15762 if (c)
15763 {
15764 OMP_CLAUSE_CHAIN (c) = *clauses;
15765 *clauses = c;
15766 }
15767 }
15768 }
15769 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15770 if (COMPARISON_CLASS_P (cond))
15771 {
15772 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15773 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15774 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15775 }
15776 else
15777 cond = RECUR (cond);
15778 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15779 switch (TREE_CODE (incr))
15780 {
15781 case PREINCREMENT_EXPR:
15782 case PREDECREMENT_EXPR:
15783 case POSTINCREMENT_EXPR:
15784 case POSTDECREMENT_EXPR:
15785 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15786 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15787 break;
15788 case MODIFY_EXPR:
15789 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15790 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15791 {
15792 tree rhs = TREE_OPERAND (incr, 1);
15793 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15794 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15795 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15796 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15797 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15798 rhs0, rhs1));
15799 }
15800 else
15801 incr = RECUR (incr);
15802 break;
15803 case MODOP_EXPR:
15804 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15805 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15806 {
15807 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15808 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15809 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15810 TREE_TYPE (decl), lhs,
15811 RECUR (TREE_OPERAND (incr, 2))));
15812 }
15813 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15814 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15815 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15816 {
15817 tree rhs = TREE_OPERAND (incr, 2);
15818 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15819 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15820 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15821 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15822 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15823 rhs0, rhs1));
15824 }
15825 else
15826 incr = RECUR (incr);
15827 break;
15828 default:
15829 incr = RECUR (incr);
15830 break;
15831 }
15832
15833 TREE_VEC_ELT (declv, i) = decl;
15834 TREE_VEC_ELT (initv, i) = init;
15835 TREE_VEC_ELT (condv, i) = cond;
15836 TREE_VEC_ELT (incrv, i) = incr;
15837 #undef RECUR
15838 }
15839
15840 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15841 of OMP_TARGET's body. */
15842
15843 static tree
15844 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15845 {
15846 *walk_subtrees = 0;
15847 switch (TREE_CODE (*tp))
15848 {
15849 case OMP_TEAMS:
15850 return *tp;
15851 case BIND_EXPR:
15852 case STATEMENT_LIST:
15853 *walk_subtrees = 1;
15854 break;
15855 default:
15856 break;
15857 }
15858 return NULL_TREE;
15859 }
15860
15861 /* Helper function for tsubst_expr. For decomposition declaration
15862 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15863 also the corresponding decls representing the identifiers
15864 of the decomposition declaration. Return DECL if successful
15865 or error_mark_node otherwise, set *FIRST to the first decl
15866 in the list chained through DECL_CHAIN and *CNT to the number
15867 of such decls. */
15868
15869 static tree
15870 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15871 tsubst_flags_t complain, tree in_decl, tree *first,
15872 unsigned int *cnt)
15873 {
15874 tree decl2, decl3, prev = decl;
15875 *cnt = 0;
15876 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15877 for (decl2 = DECL_CHAIN (pattern_decl);
15878 decl2
15879 && VAR_P (decl2)
15880 && DECL_DECOMPOSITION_P (decl2)
15881 && DECL_NAME (decl2);
15882 decl2 = DECL_CHAIN (decl2))
15883 {
15884 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15885 {
15886 gcc_assert (errorcount);
15887 return error_mark_node;
15888 }
15889 (*cnt)++;
15890 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15891 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15892 tree v = DECL_VALUE_EXPR (decl2);
15893 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15894 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15895 decl3 = tsubst (decl2, args, complain, in_decl);
15896 SET_DECL_VALUE_EXPR (decl2, v);
15897 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15898 if (VAR_P (decl3))
15899 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15900 maybe_push_decl (decl3);
15901 if (error_operand_p (decl3))
15902 decl = error_mark_node;
15903 else if (decl != error_mark_node
15904 && DECL_CHAIN (decl3) != prev)
15905 {
15906 gcc_assert (errorcount);
15907 decl = error_mark_node;
15908 }
15909 else
15910 prev = decl3;
15911 }
15912 *first = prev;
15913 return decl;
15914 }
15915
15916 /* Like tsubst_copy for expressions, etc. but also does semantic
15917 processing. */
15918
15919 tree
15920 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15921 bool integral_constant_expression_p)
15922 {
15923 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15924 #define RECUR(NODE) \
15925 tsubst_expr ((NODE), args, complain, in_decl, \
15926 integral_constant_expression_p)
15927
15928 tree stmt, tmp;
15929 tree r;
15930 location_t loc;
15931
15932 if (t == NULL_TREE || t == error_mark_node)
15933 return t;
15934
15935 loc = input_location;
15936 if (EXPR_HAS_LOCATION (t))
15937 input_location = EXPR_LOCATION (t);
15938 if (STATEMENT_CODE_P (TREE_CODE (t)))
15939 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15940
15941 switch (TREE_CODE (t))
15942 {
15943 case STATEMENT_LIST:
15944 {
15945 tree_stmt_iterator i;
15946 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15947 RECUR (tsi_stmt (i));
15948 break;
15949 }
15950
15951 case CTOR_INITIALIZER:
15952 finish_mem_initializers (tsubst_initializer_list
15953 (TREE_OPERAND (t, 0), args));
15954 break;
15955
15956 case RETURN_EXPR:
15957 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15958 break;
15959
15960 case EXPR_STMT:
15961 tmp = RECUR (EXPR_STMT_EXPR (t));
15962 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15963 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15964 else
15965 finish_expr_stmt (tmp);
15966 break;
15967
15968 case USING_STMT:
15969 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15970 /*attribs=*/NULL_TREE);
15971 break;
15972
15973 case DECL_EXPR:
15974 {
15975 tree decl, pattern_decl;
15976 tree init;
15977
15978 pattern_decl = decl = DECL_EXPR_DECL (t);
15979 if (TREE_CODE (decl) == LABEL_DECL)
15980 finish_label_decl (DECL_NAME (decl));
15981 else if (TREE_CODE (decl) == USING_DECL)
15982 {
15983 tree scope = USING_DECL_SCOPE (decl);
15984 tree name = DECL_NAME (decl);
15985
15986 scope = tsubst (scope, args, complain, in_decl);
15987 decl = lookup_qualified_name (scope, name,
15988 /*is_type_p=*/false,
15989 /*complain=*/false);
15990 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15991 qualified_name_lookup_error (scope, name, decl, input_location);
15992 else
15993 finish_local_using_decl (decl, scope, name);
15994 }
15995 else if (DECL_PACK_P (decl))
15996 {
15997 /* Don't build up decls for a variadic capture proxy, we'll
15998 instantiate the elements directly as needed. */
15999 break;
16000 }
16001 else if (is_capture_proxy (decl)
16002 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16003 {
16004 /* We're in tsubst_lambda_expr, we've already inserted a new
16005 capture proxy, so look it up and register it. */
16006 tree inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16007 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16008 gcc_assert (inst != decl && is_capture_proxy (inst));
16009 register_local_specialization (inst, decl);
16010 break;
16011 }
16012 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16013 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16014 /* Don't copy the old closure; we'll create a new one in
16015 tsubst_lambda_expr. */
16016 break;
16017 else
16018 {
16019 init = DECL_INITIAL (decl);
16020 decl = tsubst (decl, args, complain, in_decl);
16021 if (decl != error_mark_node)
16022 {
16023 /* By marking the declaration as instantiated, we avoid
16024 trying to instantiate it. Since instantiate_decl can't
16025 handle local variables, and since we've already done
16026 all that needs to be done, that's the right thing to
16027 do. */
16028 if (VAR_P (decl))
16029 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16030 if (VAR_P (decl)
16031 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16032 /* Anonymous aggregates are a special case. */
16033 finish_anon_union (decl);
16034 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16035 {
16036 DECL_CONTEXT (decl) = current_function_decl;
16037 if (DECL_NAME (decl) == this_identifier)
16038 {
16039 tree lam = DECL_CONTEXT (current_function_decl);
16040 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16041 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16042 }
16043 insert_capture_proxy (decl);
16044 }
16045 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16046 /* We already did a pushtag. */;
16047 else if (TREE_CODE (decl) == FUNCTION_DECL
16048 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16049 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16050 {
16051 DECL_CONTEXT (decl) = NULL_TREE;
16052 pushdecl (decl);
16053 DECL_CONTEXT (decl) = current_function_decl;
16054 cp_check_omp_declare_reduction (decl);
16055 }
16056 else
16057 {
16058 int const_init = false;
16059 maybe_push_decl (decl);
16060 if (VAR_P (decl)
16061 && DECL_PRETTY_FUNCTION_P (decl))
16062 {
16063 /* For __PRETTY_FUNCTION__ we have to adjust the
16064 initializer. */
16065 const char *const name
16066 = cxx_printable_name (current_function_decl, 2);
16067 init = cp_fname_init (name, &TREE_TYPE (decl));
16068 }
16069 else
16070 init = tsubst_init (init, decl, args, complain, in_decl);
16071
16072 if (VAR_P (decl))
16073 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16074 (pattern_decl));
16075 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16076 if (VAR_P (decl)
16077 && DECL_DECOMPOSITION_P (decl)
16078 && TREE_TYPE (pattern_decl) != error_mark_node)
16079 {
16080 unsigned int cnt;
16081 tree first;
16082 decl = tsubst_decomp_names (decl, pattern_decl, args,
16083 complain, in_decl, &first,
16084 &cnt);
16085 if (decl != error_mark_node)
16086 cp_finish_decomp (decl, first, cnt);
16087 }
16088 }
16089 }
16090 }
16091
16092 break;
16093 }
16094
16095 case FOR_STMT:
16096 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16097 RECUR (FOR_INIT_STMT (t));
16098 finish_init_stmt (stmt);
16099 tmp = RECUR (FOR_COND (t));
16100 finish_for_cond (tmp, stmt, false);
16101 tmp = RECUR (FOR_EXPR (t));
16102 finish_for_expr (tmp, stmt);
16103 RECUR (FOR_BODY (t));
16104 finish_for_stmt (stmt);
16105 break;
16106
16107 case RANGE_FOR_STMT:
16108 {
16109 tree decl, expr;
16110 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16111 decl = RANGE_FOR_DECL (t);
16112 decl = tsubst (decl, args, complain, in_decl);
16113 maybe_push_decl (decl);
16114 expr = RECUR (RANGE_FOR_EXPR (t));
16115 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16116 {
16117 unsigned int cnt;
16118 tree first;
16119 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16120 complain, in_decl, &first, &cnt);
16121 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
16122 RANGE_FOR_IVDEP (t));
16123 }
16124 else
16125 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
16126 RANGE_FOR_IVDEP (t));
16127 RECUR (RANGE_FOR_BODY (t));
16128 finish_for_stmt (stmt);
16129 }
16130 break;
16131
16132 case WHILE_STMT:
16133 stmt = begin_while_stmt ();
16134 tmp = RECUR (WHILE_COND (t));
16135 finish_while_stmt_cond (tmp, stmt, false);
16136 RECUR (WHILE_BODY (t));
16137 finish_while_stmt (stmt);
16138 break;
16139
16140 case DO_STMT:
16141 stmt = begin_do_stmt ();
16142 RECUR (DO_BODY (t));
16143 finish_do_body (stmt);
16144 tmp = RECUR (DO_COND (t));
16145 finish_do_stmt (tmp, stmt, false);
16146 break;
16147
16148 case IF_STMT:
16149 stmt = begin_if_stmt ();
16150 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16151 tmp = RECUR (IF_COND (t));
16152 tmp = finish_if_stmt_cond (tmp, stmt);
16153 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16154 /* Don't instantiate the THEN_CLAUSE. */;
16155 else
16156 {
16157 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16158 if (inhibit)
16159 ++c_inhibit_evaluation_warnings;
16160 RECUR (THEN_CLAUSE (t));
16161 if (inhibit)
16162 --c_inhibit_evaluation_warnings;
16163 }
16164 finish_then_clause (stmt);
16165
16166 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16167 /* Don't instantiate the ELSE_CLAUSE. */;
16168 else if (ELSE_CLAUSE (t))
16169 {
16170 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16171 begin_else_clause (stmt);
16172 if (inhibit)
16173 ++c_inhibit_evaluation_warnings;
16174 RECUR (ELSE_CLAUSE (t));
16175 if (inhibit)
16176 --c_inhibit_evaluation_warnings;
16177 finish_else_clause (stmt);
16178 }
16179
16180 finish_if_stmt (stmt);
16181 break;
16182
16183 case BIND_EXPR:
16184 if (BIND_EXPR_BODY_BLOCK (t))
16185 stmt = begin_function_body ();
16186 else
16187 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16188 ? BCS_TRY_BLOCK : 0);
16189
16190 RECUR (BIND_EXPR_BODY (t));
16191
16192 if (BIND_EXPR_BODY_BLOCK (t))
16193 finish_function_body (stmt);
16194 else
16195 finish_compound_stmt (stmt);
16196 break;
16197
16198 case BREAK_STMT:
16199 finish_break_stmt ();
16200 break;
16201
16202 case CONTINUE_STMT:
16203 finish_continue_stmt ();
16204 break;
16205
16206 case SWITCH_STMT:
16207 stmt = begin_switch_stmt ();
16208 tmp = RECUR (SWITCH_STMT_COND (t));
16209 finish_switch_cond (tmp, stmt);
16210 RECUR (SWITCH_STMT_BODY (t));
16211 finish_switch_stmt (stmt);
16212 break;
16213
16214 case CASE_LABEL_EXPR:
16215 {
16216 tree low = RECUR (CASE_LOW (t));
16217 tree high = RECUR (CASE_HIGH (t));
16218 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16219 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16220 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16221 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16222 }
16223 break;
16224
16225 case LABEL_EXPR:
16226 {
16227 tree decl = LABEL_EXPR_LABEL (t);
16228 tree label;
16229
16230 label = finish_label_stmt (DECL_NAME (decl));
16231 if (TREE_CODE (label) == LABEL_DECL)
16232 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16233 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16234 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16235 }
16236 break;
16237
16238 case GOTO_EXPR:
16239 tmp = GOTO_DESTINATION (t);
16240 if (TREE_CODE (tmp) != LABEL_DECL)
16241 /* Computed goto's must be tsubst'd into. On the other hand,
16242 non-computed gotos must not be; the identifier in question
16243 will have no binding. */
16244 tmp = RECUR (tmp);
16245 else
16246 tmp = DECL_NAME (tmp);
16247 finish_goto_stmt (tmp);
16248 break;
16249
16250 case ASM_EXPR:
16251 {
16252 tree string = RECUR (ASM_STRING (t));
16253 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16254 complain, in_decl);
16255 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16256 complain, in_decl);
16257 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16258 complain, in_decl);
16259 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16260 complain, in_decl);
16261 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16262 clobbers, labels);
16263 tree asm_expr = tmp;
16264 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16265 asm_expr = TREE_OPERAND (asm_expr, 0);
16266 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16267 }
16268 break;
16269
16270 case TRY_BLOCK:
16271 if (CLEANUP_P (t))
16272 {
16273 stmt = begin_try_block ();
16274 RECUR (TRY_STMTS (t));
16275 finish_cleanup_try_block (stmt);
16276 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16277 }
16278 else
16279 {
16280 tree compound_stmt = NULL_TREE;
16281
16282 if (FN_TRY_BLOCK_P (t))
16283 stmt = begin_function_try_block (&compound_stmt);
16284 else
16285 stmt = begin_try_block ();
16286
16287 RECUR (TRY_STMTS (t));
16288
16289 if (FN_TRY_BLOCK_P (t))
16290 finish_function_try_block (stmt);
16291 else
16292 finish_try_block (stmt);
16293
16294 RECUR (TRY_HANDLERS (t));
16295 if (FN_TRY_BLOCK_P (t))
16296 finish_function_handler_sequence (stmt, compound_stmt);
16297 else
16298 finish_handler_sequence (stmt);
16299 }
16300 break;
16301
16302 case HANDLER:
16303 {
16304 tree decl = HANDLER_PARMS (t);
16305
16306 if (decl)
16307 {
16308 decl = tsubst (decl, args, complain, in_decl);
16309 /* Prevent instantiate_decl from trying to instantiate
16310 this variable. We've already done all that needs to be
16311 done. */
16312 if (decl != error_mark_node)
16313 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16314 }
16315 stmt = begin_handler ();
16316 finish_handler_parms (decl, stmt);
16317 RECUR (HANDLER_BODY (t));
16318 finish_handler (stmt);
16319 }
16320 break;
16321
16322 case TAG_DEFN:
16323 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16324 if (CLASS_TYPE_P (tmp))
16325 {
16326 /* Local classes are not independent templates; they are
16327 instantiated along with their containing function. And this
16328 way we don't have to deal with pushing out of one local class
16329 to instantiate a member of another local class. */
16330 /* Closures are handled by the LAMBDA_EXPR. */
16331 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16332 complete_type (tmp);
16333 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16334 if ((VAR_P (fld)
16335 || (TREE_CODE (fld) == FUNCTION_DECL
16336 && !DECL_ARTIFICIAL (fld)))
16337 && DECL_TEMPLATE_INSTANTIATION (fld))
16338 instantiate_decl (fld, /*defer_ok=*/false,
16339 /*expl_inst_class=*/false);
16340 }
16341 break;
16342
16343 case STATIC_ASSERT:
16344 {
16345 tree condition;
16346
16347 ++c_inhibit_evaluation_warnings;
16348 condition =
16349 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16350 args,
16351 complain, in_decl,
16352 /*integral_constant_expression_p=*/true);
16353 --c_inhibit_evaluation_warnings;
16354
16355 finish_static_assert (condition,
16356 STATIC_ASSERT_MESSAGE (t),
16357 STATIC_ASSERT_SOURCE_LOCATION (t),
16358 /*member_p=*/false);
16359 }
16360 break;
16361
16362 case OACC_KERNELS:
16363 case OACC_PARALLEL:
16364 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16365 in_decl);
16366 stmt = begin_omp_parallel ();
16367 RECUR (OMP_BODY (t));
16368 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16369 break;
16370
16371 case OMP_PARALLEL:
16372 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16373 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16374 complain, in_decl);
16375 if (OMP_PARALLEL_COMBINED (t))
16376 omp_parallel_combined_clauses = &tmp;
16377 stmt = begin_omp_parallel ();
16378 RECUR (OMP_PARALLEL_BODY (t));
16379 gcc_assert (omp_parallel_combined_clauses == NULL);
16380 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16381 = OMP_PARALLEL_COMBINED (t);
16382 pop_omp_privatization_clauses (r);
16383 break;
16384
16385 case OMP_TASK:
16386 r = push_omp_privatization_clauses (false);
16387 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16388 complain, in_decl);
16389 stmt = begin_omp_task ();
16390 RECUR (OMP_TASK_BODY (t));
16391 finish_omp_task (tmp, stmt);
16392 pop_omp_privatization_clauses (r);
16393 break;
16394
16395 case OMP_FOR:
16396 case OMP_SIMD:
16397 case CILK_SIMD:
16398 case CILK_FOR:
16399 case OMP_DISTRIBUTE:
16400 case OMP_TASKLOOP:
16401 case OACC_LOOP:
16402 {
16403 tree clauses, body, pre_body;
16404 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16405 tree orig_declv = NULL_TREE;
16406 tree incrv = NULL_TREE;
16407 enum c_omp_region_type ort = C_ORT_OMP;
16408 int i;
16409
16410 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16411 ort = C_ORT_CILK;
16412 else if (TREE_CODE (t) == OACC_LOOP)
16413 ort = C_ORT_ACC;
16414
16415 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16416 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16417 in_decl);
16418 if (OMP_FOR_INIT (t) != NULL_TREE)
16419 {
16420 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16421 if (OMP_FOR_ORIG_DECLS (t))
16422 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16423 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16424 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16425 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16426 }
16427
16428 stmt = begin_omp_structured_block ();
16429
16430 pre_body = push_stmt_list ();
16431 RECUR (OMP_FOR_PRE_BODY (t));
16432 pre_body = pop_stmt_list (pre_body);
16433
16434 if (OMP_FOR_INIT (t) != NULL_TREE)
16435 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16436 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16437 incrv, &clauses, args, complain, in_decl,
16438 integral_constant_expression_p);
16439 omp_parallel_combined_clauses = NULL;
16440
16441 body = push_stmt_list ();
16442 RECUR (OMP_FOR_BODY (t));
16443 body = pop_stmt_list (body);
16444
16445 if (OMP_FOR_INIT (t) != NULL_TREE)
16446 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16447 orig_declv, initv, condv, incrv, body, pre_body,
16448 NULL, clauses);
16449 else
16450 {
16451 t = make_node (TREE_CODE (t));
16452 TREE_TYPE (t) = void_type_node;
16453 OMP_FOR_BODY (t) = body;
16454 OMP_FOR_PRE_BODY (t) = pre_body;
16455 OMP_FOR_CLAUSES (t) = clauses;
16456 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16457 add_stmt (t);
16458 }
16459
16460 add_stmt (finish_omp_structured_block (stmt));
16461 pop_omp_privatization_clauses (r);
16462 }
16463 break;
16464
16465 case OMP_SECTIONS:
16466 omp_parallel_combined_clauses = NULL;
16467 /* FALLTHRU */
16468 case OMP_SINGLE:
16469 case OMP_TEAMS:
16470 case OMP_CRITICAL:
16471 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16472 && OMP_TEAMS_COMBINED (t));
16473 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16474 in_decl);
16475 stmt = push_stmt_list ();
16476 RECUR (OMP_BODY (t));
16477 stmt = pop_stmt_list (stmt);
16478
16479 t = copy_node (t);
16480 OMP_BODY (t) = stmt;
16481 OMP_CLAUSES (t) = tmp;
16482 add_stmt (t);
16483 pop_omp_privatization_clauses (r);
16484 break;
16485
16486 case OACC_DATA:
16487 case OMP_TARGET_DATA:
16488 case OMP_TARGET:
16489 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16490 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16491 in_decl);
16492 keep_next_level (true);
16493 stmt = begin_omp_structured_block ();
16494
16495 RECUR (OMP_BODY (t));
16496 stmt = finish_omp_structured_block (stmt);
16497
16498 t = copy_node (t);
16499 OMP_BODY (t) = stmt;
16500 OMP_CLAUSES (t) = tmp;
16501 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16502 {
16503 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16504 if (teams)
16505 {
16506 /* For combined target teams, ensure the num_teams and
16507 thread_limit clause expressions are evaluated on the host,
16508 before entering the target construct. */
16509 tree c;
16510 for (c = OMP_TEAMS_CLAUSES (teams);
16511 c; c = OMP_CLAUSE_CHAIN (c))
16512 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16513 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16514 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16515 {
16516 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16517 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16518 if (expr == error_mark_node)
16519 continue;
16520 tmp = TARGET_EXPR_SLOT (expr);
16521 add_stmt (expr);
16522 OMP_CLAUSE_OPERAND (c, 0) = expr;
16523 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16524 OMP_CLAUSE_FIRSTPRIVATE);
16525 OMP_CLAUSE_DECL (tc) = tmp;
16526 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16527 OMP_TARGET_CLAUSES (t) = tc;
16528 }
16529 }
16530 }
16531 add_stmt (t);
16532 break;
16533
16534 case OACC_DECLARE:
16535 t = copy_node (t);
16536 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16537 complain, in_decl);
16538 OACC_DECLARE_CLAUSES (t) = tmp;
16539 add_stmt (t);
16540 break;
16541
16542 case OMP_TARGET_UPDATE:
16543 case OMP_TARGET_ENTER_DATA:
16544 case OMP_TARGET_EXIT_DATA:
16545 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16546 complain, in_decl);
16547 t = copy_node (t);
16548 OMP_STANDALONE_CLAUSES (t) = tmp;
16549 add_stmt (t);
16550 break;
16551
16552 case OACC_ENTER_DATA:
16553 case OACC_EXIT_DATA:
16554 case OACC_UPDATE:
16555 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16556 complain, in_decl);
16557 t = copy_node (t);
16558 OMP_STANDALONE_CLAUSES (t) = tmp;
16559 add_stmt (t);
16560 break;
16561
16562 case OMP_ORDERED:
16563 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16564 complain, in_decl);
16565 stmt = push_stmt_list ();
16566 RECUR (OMP_BODY (t));
16567 stmt = pop_stmt_list (stmt);
16568
16569 t = copy_node (t);
16570 OMP_BODY (t) = stmt;
16571 OMP_ORDERED_CLAUSES (t) = tmp;
16572 add_stmt (t);
16573 break;
16574
16575 case OMP_SECTION:
16576 case OMP_MASTER:
16577 case OMP_TASKGROUP:
16578 stmt = push_stmt_list ();
16579 RECUR (OMP_BODY (t));
16580 stmt = pop_stmt_list (stmt);
16581
16582 t = copy_node (t);
16583 OMP_BODY (t) = stmt;
16584 add_stmt (t);
16585 break;
16586
16587 case OMP_ATOMIC:
16588 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16589 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16590 {
16591 tree op1 = TREE_OPERAND (t, 1);
16592 tree rhs1 = NULL_TREE;
16593 tree lhs, rhs;
16594 if (TREE_CODE (op1) == COMPOUND_EXPR)
16595 {
16596 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16597 op1 = TREE_OPERAND (op1, 1);
16598 }
16599 lhs = RECUR (TREE_OPERAND (op1, 0));
16600 rhs = RECUR (TREE_OPERAND (op1, 1));
16601 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16602 NULL_TREE, NULL_TREE, rhs1,
16603 OMP_ATOMIC_SEQ_CST (t));
16604 }
16605 else
16606 {
16607 tree op1 = TREE_OPERAND (t, 1);
16608 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16609 tree rhs1 = NULL_TREE;
16610 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16611 enum tree_code opcode = NOP_EXPR;
16612 if (code == OMP_ATOMIC_READ)
16613 {
16614 v = RECUR (TREE_OPERAND (op1, 0));
16615 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16616 }
16617 else if (code == OMP_ATOMIC_CAPTURE_OLD
16618 || code == OMP_ATOMIC_CAPTURE_NEW)
16619 {
16620 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16621 v = RECUR (TREE_OPERAND (op1, 0));
16622 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16623 if (TREE_CODE (op11) == COMPOUND_EXPR)
16624 {
16625 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16626 op11 = TREE_OPERAND (op11, 1);
16627 }
16628 lhs = RECUR (TREE_OPERAND (op11, 0));
16629 rhs = RECUR (TREE_OPERAND (op11, 1));
16630 opcode = TREE_CODE (op11);
16631 if (opcode == MODIFY_EXPR)
16632 opcode = NOP_EXPR;
16633 }
16634 else
16635 {
16636 code = OMP_ATOMIC;
16637 lhs = RECUR (TREE_OPERAND (op1, 0));
16638 rhs = RECUR (TREE_OPERAND (op1, 1));
16639 }
16640 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16641 OMP_ATOMIC_SEQ_CST (t));
16642 }
16643 break;
16644
16645 case TRANSACTION_EXPR:
16646 {
16647 int flags = 0;
16648 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16649 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16650
16651 if (TRANSACTION_EXPR_IS_STMT (t))
16652 {
16653 tree body = TRANSACTION_EXPR_BODY (t);
16654 tree noex = NULL_TREE;
16655 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16656 {
16657 noex = MUST_NOT_THROW_COND (body);
16658 if (noex == NULL_TREE)
16659 noex = boolean_true_node;
16660 body = TREE_OPERAND (body, 0);
16661 }
16662 stmt = begin_transaction_stmt (input_location, NULL, flags);
16663 RECUR (body);
16664 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16665 }
16666 else
16667 {
16668 stmt = build_transaction_expr (EXPR_LOCATION (t),
16669 RECUR (TRANSACTION_EXPR_BODY (t)),
16670 flags, NULL_TREE);
16671 RETURN (stmt);
16672 }
16673 }
16674 break;
16675
16676 case MUST_NOT_THROW_EXPR:
16677 {
16678 tree op0 = RECUR (TREE_OPERAND (t, 0));
16679 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16680 RETURN (build_must_not_throw_expr (op0, cond));
16681 }
16682
16683 case EXPR_PACK_EXPANSION:
16684 error ("invalid use of pack expansion expression");
16685 RETURN (error_mark_node);
16686
16687 case NONTYPE_ARGUMENT_PACK:
16688 error ("use %<...%> to expand argument pack");
16689 RETURN (error_mark_node);
16690
16691 case CILK_SPAWN_STMT:
16692 cfun->calls_cilk_spawn = 1;
16693 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16694
16695 case CILK_SYNC_STMT:
16696 RETURN (build_cilk_sync ());
16697
16698 case COMPOUND_EXPR:
16699 tmp = RECUR (TREE_OPERAND (t, 0));
16700 if (tmp == NULL_TREE)
16701 /* If the first operand was a statement, we're done with it. */
16702 RETURN (RECUR (TREE_OPERAND (t, 1)));
16703 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16704 RECUR (TREE_OPERAND (t, 1)),
16705 complain));
16706
16707 case ANNOTATE_EXPR:
16708 tmp = RECUR (TREE_OPERAND (t, 0));
16709 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16710 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16711
16712 default:
16713 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16714
16715 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16716 /*function_p=*/false,
16717 integral_constant_expression_p));
16718 }
16719
16720 RETURN (NULL_TREE);
16721 out:
16722 input_location = loc;
16723 return r;
16724 #undef RECUR
16725 #undef RETURN
16726 }
16727
16728 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16729 function. For description of the body see comment above
16730 cp_parser_omp_declare_reduction_exprs. */
16731
16732 static void
16733 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16734 {
16735 if (t == NULL_TREE || t == error_mark_node)
16736 return;
16737
16738 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16739
16740 tree_stmt_iterator tsi;
16741 int i;
16742 tree stmts[7];
16743 memset (stmts, 0, sizeof stmts);
16744 for (i = 0, tsi = tsi_start (t);
16745 i < 7 && !tsi_end_p (tsi);
16746 i++, tsi_next (&tsi))
16747 stmts[i] = tsi_stmt (tsi);
16748 gcc_assert (tsi_end_p (tsi));
16749
16750 if (i >= 3)
16751 {
16752 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16753 && TREE_CODE (stmts[1]) == DECL_EXPR);
16754 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16755 args, complain, in_decl);
16756 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16757 args, complain, in_decl);
16758 DECL_CONTEXT (omp_out) = current_function_decl;
16759 DECL_CONTEXT (omp_in) = current_function_decl;
16760 keep_next_level (true);
16761 tree block = begin_omp_structured_block ();
16762 tsubst_expr (stmts[2], args, complain, in_decl, false);
16763 block = finish_omp_structured_block (block);
16764 block = maybe_cleanup_point_expr_void (block);
16765 add_decl_expr (omp_out);
16766 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16767 TREE_NO_WARNING (omp_out) = 1;
16768 add_decl_expr (omp_in);
16769 finish_expr_stmt (block);
16770 }
16771 if (i >= 6)
16772 {
16773 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16774 && TREE_CODE (stmts[4]) == DECL_EXPR);
16775 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16776 args, complain, in_decl);
16777 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16778 args, complain, in_decl);
16779 DECL_CONTEXT (omp_priv) = current_function_decl;
16780 DECL_CONTEXT (omp_orig) = current_function_decl;
16781 keep_next_level (true);
16782 tree block = begin_omp_structured_block ();
16783 tsubst_expr (stmts[5], args, complain, in_decl, false);
16784 block = finish_omp_structured_block (block);
16785 block = maybe_cleanup_point_expr_void (block);
16786 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16787 add_decl_expr (omp_priv);
16788 add_decl_expr (omp_orig);
16789 finish_expr_stmt (block);
16790 if (i == 7)
16791 add_decl_expr (omp_orig);
16792 }
16793 }
16794
16795 /* T is a postfix-expression that is not being used in a function
16796 call. Return the substituted version of T. */
16797
16798 static tree
16799 tsubst_non_call_postfix_expression (tree t, tree args,
16800 tsubst_flags_t complain,
16801 tree in_decl)
16802 {
16803 if (TREE_CODE (t) == SCOPE_REF)
16804 t = tsubst_qualified_id (t, args, complain, in_decl,
16805 /*done=*/false, /*address_p=*/false);
16806 else
16807 t = tsubst_copy_and_build (t, args, complain, in_decl,
16808 /*function_p=*/false,
16809 /*integral_constant_expression_p=*/false);
16810
16811 return t;
16812 }
16813
16814 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16815 instantiation context. Instantiating a pack expansion containing a lambda
16816 might result in multiple lambdas all based on the same lambda in the
16817 template. */
16818
16819 tree
16820 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16821 {
16822 tree oldfn = lambda_function (t);
16823 in_decl = oldfn;
16824
16825 tree r = build_lambda_expr ();
16826
16827 LAMBDA_EXPR_LOCATION (r)
16828 = LAMBDA_EXPR_LOCATION (t);
16829 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16830 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16831 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16832
16833 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16834 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16835 else
16836 record_lambda_scope (r);
16837
16838 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16839 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16840
16841 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16842 cap = TREE_CHAIN (cap))
16843 {
16844 tree field = TREE_PURPOSE (cap);
16845 if (PACK_EXPANSION_P (field))
16846 field = PACK_EXPANSION_PATTERN (field);
16847 field = tsubst_decl (field, args, complain);
16848
16849 if (field == error_mark_node)
16850 return error_mark_node;
16851
16852 tree init = TREE_VALUE (cap);
16853 if (PACK_EXPANSION_P (init))
16854 init = tsubst_pack_expansion (init, args, complain, in_decl);
16855 else
16856 init = tsubst_copy_and_build (init, args, complain, in_decl,
16857 /*fn*/false, /*constexpr*/false);
16858
16859 if (TREE_CODE (field) == TREE_VEC)
16860 {
16861 int len = TREE_VEC_LENGTH (field);
16862 gcc_assert (TREE_CODE (init) == TREE_VEC
16863 && TREE_VEC_LENGTH (init) == len);
16864 for (int i = 0; i < len; ++i)
16865 LAMBDA_EXPR_CAPTURE_LIST (r)
16866 = tree_cons (TREE_VEC_ELT (field, i),
16867 TREE_VEC_ELT (init, i),
16868 LAMBDA_EXPR_CAPTURE_LIST (r));
16869 }
16870 else
16871 {
16872 LAMBDA_EXPR_CAPTURE_LIST (r)
16873 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
16874
16875 if (id_equal (DECL_NAME (field), "__this"))
16876 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
16877 }
16878 }
16879
16880 tree type = begin_lambda_type (r);
16881
16882 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16883 determine_visibility (TYPE_NAME (type));
16884
16885 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
16886
16887 tree oldtmpl = (generic_lambda_fn_p (oldfn)
16888 ? DECL_TI_TEMPLATE (oldfn)
16889 : NULL_TREE);
16890
16891 tree fntype = static_fn_type (oldfn);
16892 if (oldtmpl)
16893 ++processing_template_decl;
16894 fntype = tsubst (fntype, args, complain, in_decl);
16895 if (oldtmpl)
16896 --processing_template_decl;
16897
16898 if (fntype == error_mark_node)
16899 r = error_mark_node;
16900 else
16901 {
16902 /* Fix the type of 'this'. */
16903 fntype = build_memfn_type (fntype, type,
16904 type_memfn_quals (fntype),
16905 type_memfn_rqual (fntype));
16906 tree fn, tmpl;
16907 if (oldtmpl)
16908 {
16909 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
16910 fn = DECL_TEMPLATE_RESULT (tmpl);
16911 finish_member_declaration (tmpl);
16912 }
16913 else
16914 {
16915 tmpl = NULL_TREE;
16916 fn = tsubst_function_decl (oldfn, args, complain, fntype);
16917 finish_member_declaration (fn);
16918 }
16919
16920 /* Let finish_function set this. */
16921 DECL_DECLARED_CONSTEXPR_P (fn) = false;
16922
16923 bool nested = cfun;
16924 if (nested)
16925 push_function_context ();
16926
16927 local_specialization_stack s (lss_copy);
16928
16929 tree body = start_lambda_function (fn, r);
16930
16931 register_parameter_specializations (oldfn, fn);
16932
16933 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
16934 /*constexpr*/false);
16935
16936 finish_lambda_function (body);
16937
16938 if (nested)
16939 pop_function_context ();
16940
16941 /* The capture list was built up in reverse order; fix that now. */
16942 LAMBDA_EXPR_CAPTURE_LIST (r)
16943 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
16944
16945 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16946
16947 maybe_add_lambda_conv_op (type);
16948 }
16949
16950 finish_struct (type, /*attr*/NULL_TREE);
16951
16952 insert_pending_capture_proxies ();
16953
16954 return r;
16955 }
16956
16957 /* Like tsubst but deals with expressions and performs semantic
16958 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16959
16960 tree
16961 tsubst_copy_and_build (tree t,
16962 tree args,
16963 tsubst_flags_t complain,
16964 tree in_decl,
16965 bool function_p,
16966 bool integral_constant_expression_p)
16967 {
16968 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16969 #define RECUR(NODE) \
16970 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16971 /*function_p=*/false, \
16972 integral_constant_expression_p)
16973
16974 tree retval, op1;
16975 location_t loc;
16976
16977 if (t == NULL_TREE || t == error_mark_node)
16978 return t;
16979
16980 loc = input_location;
16981 if (EXPR_HAS_LOCATION (t))
16982 input_location = EXPR_LOCATION (t);
16983
16984 /* N3276 decltype magic only applies to calls at the top level or on the
16985 right side of a comma. */
16986 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16987 complain &= ~tf_decltype;
16988
16989 switch (TREE_CODE (t))
16990 {
16991 case USING_DECL:
16992 t = DECL_NAME (t);
16993 /* Fall through. */
16994 case IDENTIFIER_NODE:
16995 {
16996 tree decl;
16997 cp_id_kind idk;
16998 bool non_integral_constant_expression_p;
16999 const char *error_msg;
17000
17001 if (IDENTIFIER_CONV_OP_P (t))
17002 {
17003 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17004 t = make_conv_op_name (new_type);
17005 }
17006
17007 /* Look up the name. */
17008 decl = lookup_name (t);
17009
17010 /* By convention, expressions use ERROR_MARK_NODE to indicate
17011 failure, not NULL_TREE. */
17012 if (decl == NULL_TREE)
17013 decl = error_mark_node;
17014
17015 decl = finish_id_expression (t, decl, NULL_TREE,
17016 &idk,
17017 integral_constant_expression_p,
17018 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17019 &non_integral_constant_expression_p,
17020 /*template_p=*/false,
17021 /*done=*/true,
17022 /*address_p=*/false,
17023 /*template_arg_p=*/false,
17024 &error_msg,
17025 input_location);
17026 if (error_msg)
17027 error (error_msg);
17028 if (!function_p && identifier_p (decl))
17029 {
17030 if (complain & tf_error)
17031 unqualified_name_lookup_error (decl);
17032 decl = error_mark_node;
17033 }
17034 RETURN (decl);
17035 }
17036
17037 case TEMPLATE_ID_EXPR:
17038 {
17039 tree object;
17040 tree templ = RECUR (TREE_OPERAND (t, 0));
17041 tree targs = TREE_OPERAND (t, 1);
17042
17043 if (targs)
17044 targs = tsubst_template_args (targs, args, complain, in_decl);
17045 if (targs == error_mark_node)
17046 return error_mark_node;
17047
17048 if (TREE_CODE (templ) == SCOPE_REF)
17049 {
17050 tree name = TREE_OPERAND (templ, 1);
17051 tree tid = lookup_template_function (name, targs);
17052 TREE_OPERAND (templ, 1) = tid;
17053 return templ;
17054 }
17055
17056 if (variable_template_p (templ))
17057 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17058
17059 if (TREE_CODE (templ) == COMPONENT_REF)
17060 {
17061 object = TREE_OPERAND (templ, 0);
17062 templ = TREE_OPERAND (templ, 1);
17063 }
17064 else
17065 object = NULL_TREE;
17066 templ = lookup_template_function (templ, targs);
17067
17068 if (object)
17069 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17070 object, templ, NULL_TREE));
17071 else
17072 RETURN (baselink_for_fns (templ));
17073 }
17074
17075 case INDIRECT_REF:
17076 {
17077 tree r = RECUR (TREE_OPERAND (t, 0));
17078
17079 if (REFERENCE_REF_P (t))
17080 {
17081 /* A type conversion to reference type will be enclosed in
17082 such an indirect ref, but the substitution of the cast
17083 will have also added such an indirect ref. */
17084 r = convert_from_reference (r);
17085 }
17086 else
17087 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17088 complain|decltype_flag);
17089
17090 if (TREE_CODE (r) == INDIRECT_REF)
17091 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17092
17093 RETURN (r);
17094 }
17095
17096 case NOP_EXPR:
17097 {
17098 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17099 tree op0 = RECUR (TREE_OPERAND (t, 0));
17100 RETURN (build_nop (type, op0));
17101 }
17102
17103 case IMPLICIT_CONV_EXPR:
17104 {
17105 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17106 tree expr = RECUR (TREE_OPERAND (t, 0));
17107 int flags = LOOKUP_IMPLICIT;
17108 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17109 flags = LOOKUP_NORMAL;
17110 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17111 flags));
17112 }
17113
17114 case CONVERT_EXPR:
17115 {
17116 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17117 tree op0 = RECUR (TREE_OPERAND (t, 0));
17118 RETURN (build1 (CONVERT_EXPR, type, op0));
17119 }
17120
17121 case CAST_EXPR:
17122 case REINTERPRET_CAST_EXPR:
17123 case CONST_CAST_EXPR:
17124 case DYNAMIC_CAST_EXPR:
17125 case STATIC_CAST_EXPR:
17126 {
17127 tree type;
17128 tree op, r = NULL_TREE;
17129
17130 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17131 if (integral_constant_expression_p
17132 && !cast_valid_in_integral_constant_expression_p (type))
17133 {
17134 if (complain & tf_error)
17135 error ("a cast to a type other than an integral or "
17136 "enumeration type cannot appear in a constant-expression");
17137 RETURN (error_mark_node);
17138 }
17139
17140 op = RECUR (TREE_OPERAND (t, 0));
17141
17142 warning_sentinel s(warn_useless_cast);
17143 switch (TREE_CODE (t))
17144 {
17145 case CAST_EXPR:
17146 r = build_functional_cast (type, op, complain);
17147 break;
17148 case REINTERPRET_CAST_EXPR:
17149 r = build_reinterpret_cast (type, op, complain);
17150 break;
17151 case CONST_CAST_EXPR:
17152 r = build_const_cast (type, op, complain);
17153 break;
17154 case DYNAMIC_CAST_EXPR:
17155 r = build_dynamic_cast (type, op, complain);
17156 break;
17157 case STATIC_CAST_EXPR:
17158 r = build_static_cast (type, op, complain);
17159 break;
17160 default:
17161 gcc_unreachable ();
17162 }
17163
17164 RETURN (r);
17165 }
17166
17167 case POSTDECREMENT_EXPR:
17168 case POSTINCREMENT_EXPR:
17169 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17170 args, complain, in_decl);
17171 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17172 complain|decltype_flag));
17173
17174 case PREDECREMENT_EXPR:
17175 case PREINCREMENT_EXPR:
17176 case NEGATE_EXPR:
17177 case BIT_NOT_EXPR:
17178 case ABS_EXPR:
17179 case TRUTH_NOT_EXPR:
17180 case UNARY_PLUS_EXPR: /* Unary + */
17181 case REALPART_EXPR:
17182 case IMAGPART_EXPR:
17183 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17184 RECUR (TREE_OPERAND (t, 0)),
17185 complain|decltype_flag));
17186
17187 case FIX_TRUNC_EXPR:
17188 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17189 false, complain));
17190
17191 case ADDR_EXPR:
17192 op1 = TREE_OPERAND (t, 0);
17193 if (TREE_CODE (op1) == LABEL_DECL)
17194 RETURN (finish_label_address_expr (DECL_NAME (op1),
17195 EXPR_LOCATION (op1)));
17196 if (TREE_CODE (op1) == SCOPE_REF)
17197 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17198 /*done=*/true, /*address_p=*/true);
17199 else
17200 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17201 in_decl);
17202 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17203 complain|decltype_flag));
17204
17205 case PLUS_EXPR:
17206 case MINUS_EXPR:
17207 case MULT_EXPR:
17208 case TRUNC_DIV_EXPR:
17209 case CEIL_DIV_EXPR:
17210 case FLOOR_DIV_EXPR:
17211 case ROUND_DIV_EXPR:
17212 case EXACT_DIV_EXPR:
17213 case BIT_AND_EXPR:
17214 case BIT_IOR_EXPR:
17215 case BIT_XOR_EXPR:
17216 case TRUNC_MOD_EXPR:
17217 case FLOOR_MOD_EXPR:
17218 case TRUTH_ANDIF_EXPR:
17219 case TRUTH_ORIF_EXPR:
17220 case TRUTH_AND_EXPR:
17221 case TRUTH_OR_EXPR:
17222 case RSHIFT_EXPR:
17223 case LSHIFT_EXPR:
17224 case RROTATE_EXPR:
17225 case LROTATE_EXPR:
17226 case EQ_EXPR:
17227 case NE_EXPR:
17228 case MAX_EXPR:
17229 case MIN_EXPR:
17230 case LE_EXPR:
17231 case GE_EXPR:
17232 case LT_EXPR:
17233 case GT_EXPR:
17234 case MEMBER_REF:
17235 case DOTSTAR_EXPR:
17236 {
17237 warning_sentinel s1(warn_type_limits);
17238 warning_sentinel s2(warn_div_by_zero);
17239 warning_sentinel s3(warn_logical_op);
17240 warning_sentinel s4(warn_tautological_compare);
17241 tree op0 = RECUR (TREE_OPERAND (t, 0));
17242 tree op1 = RECUR (TREE_OPERAND (t, 1));
17243 tree r = build_x_binary_op
17244 (input_location, TREE_CODE (t),
17245 op0,
17246 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17247 ? ERROR_MARK
17248 : TREE_CODE (TREE_OPERAND (t, 0))),
17249 op1,
17250 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17251 ? ERROR_MARK
17252 : TREE_CODE (TREE_OPERAND (t, 1))),
17253 /*overload=*/NULL,
17254 complain|decltype_flag);
17255 if (EXPR_P (r) && TREE_NO_WARNING (t))
17256 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17257
17258 RETURN (r);
17259 }
17260
17261 case POINTER_PLUS_EXPR:
17262 {
17263 tree op0 = RECUR (TREE_OPERAND (t, 0));
17264 tree op1 = RECUR (TREE_OPERAND (t, 1));
17265 return fold_build_pointer_plus (op0, op1);
17266 }
17267
17268 case SCOPE_REF:
17269 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17270 /*address_p=*/false));
17271 case ARRAY_REF:
17272 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17273 args, complain, in_decl);
17274 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17275 RECUR (TREE_OPERAND (t, 1)),
17276 complain|decltype_flag));
17277
17278 case ARRAY_NOTATION_REF:
17279 {
17280 tree start_index, length, stride;
17281 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
17282 args, complain, in_decl);
17283 start_index = RECUR (ARRAY_NOTATION_START (t));
17284 length = RECUR (ARRAY_NOTATION_LENGTH (t));
17285 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
17286 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
17287 length, stride, TREE_TYPE (op1)));
17288 }
17289 case SIZEOF_EXPR:
17290 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17291 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17292 RETURN (tsubst_copy (t, args, complain, in_decl));
17293 /* Fall through */
17294
17295 case ALIGNOF_EXPR:
17296 {
17297 tree r;
17298
17299 op1 = TREE_OPERAND (t, 0);
17300 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17301 op1 = TREE_TYPE (op1);
17302 if (!args)
17303 {
17304 /* When there are no ARGS, we are trying to evaluate a
17305 non-dependent expression from the parser. Trying to do
17306 the substitutions may not work. */
17307 if (!TYPE_P (op1))
17308 op1 = TREE_TYPE (op1);
17309 }
17310 else
17311 {
17312 ++cp_unevaluated_operand;
17313 ++c_inhibit_evaluation_warnings;
17314 if (TYPE_P (op1))
17315 op1 = tsubst (op1, args, complain, in_decl);
17316 else
17317 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17318 /*function_p=*/false,
17319 /*integral_constant_expression_p=*/
17320 false);
17321 --cp_unevaluated_operand;
17322 --c_inhibit_evaluation_warnings;
17323 }
17324 if (TYPE_P (op1))
17325 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17326 complain & tf_error);
17327 else
17328 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17329 complain & tf_error);
17330 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17331 {
17332 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17333 {
17334 if (!processing_template_decl && TYPE_P (op1))
17335 {
17336 r = build_min (SIZEOF_EXPR, size_type_node,
17337 build1 (NOP_EXPR, op1, error_mark_node));
17338 SIZEOF_EXPR_TYPE_P (r) = 1;
17339 }
17340 else
17341 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17342 TREE_SIDE_EFFECTS (r) = 0;
17343 TREE_READONLY (r) = 1;
17344 }
17345 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17346 }
17347 RETURN (r);
17348 }
17349
17350 case AT_ENCODE_EXPR:
17351 {
17352 op1 = TREE_OPERAND (t, 0);
17353 ++cp_unevaluated_operand;
17354 ++c_inhibit_evaluation_warnings;
17355 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17356 /*function_p=*/false,
17357 /*integral_constant_expression_p=*/false);
17358 --cp_unevaluated_operand;
17359 --c_inhibit_evaluation_warnings;
17360 RETURN (objc_build_encode_expr (op1));
17361 }
17362
17363 case NOEXCEPT_EXPR:
17364 op1 = TREE_OPERAND (t, 0);
17365 ++cp_unevaluated_operand;
17366 ++c_inhibit_evaluation_warnings;
17367 ++cp_noexcept_operand;
17368 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17369 /*function_p=*/false,
17370 /*integral_constant_expression_p=*/false);
17371 --cp_unevaluated_operand;
17372 --c_inhibit_evaluation_warnings;
17373 --cp_noexcept_operand;
17374 RETURN (finish_noexcept_expr (op1, complain));
17375
17376 case MODOP_EXPR:
17377 {
17378 warning_sentinel s(warn_div_by_zero);
17379 tree lhs = RECUR (TREE_OPERAND (t, 0));
17380 tree rhs = RECUR (TREE_OPERAND (t, 2));
17381 tree r = build_x_modify_expr
17382 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17383 complain|decltype_flag);
17384 /* TREE_NO_WARNING must be set if either the expression was
17385 parenthesized or it uses an operator such as >>= rather
17386 than plain assignment. In the former case, it was already
17387 set and must be copied. In the latter case,
17388 build_x_modify_expr sets it and it must not be reset
17389 here. */
17390 if (TREE_NO_WARNING (t))
17391 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17392
17393 RETURN (r);
17394 }
17395
17396 case ARROW_EXPR:
17397 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17398 args, complain, in_decl);
17399 /* Remember that there was a reference to this entity. */
17400 if (DECL_P (op1)
17401 && !mark_used (op1, complain) && !(complain & tf_error))
17402 RETURN (error_mark_node);
17403 RETURN (build_x_arrow (input_location, op1, complain));
17404
17405 case NEW_EXPR:
17406 {
17407 tree placement = RECUR (TREE_OPERAND (t, 0));
17408 tree init = RECUR (TREE_OPERAND (t, 3));
17409 vec<tree, va_gc> *placement_vec;
17410 vec<tree, va_gc> *init_vec;
17411 tree ret;
17412
17413 if (placement == NULL_TREE)
17414 placement_vec = NULL;
17415 else
17416 {
17417 placement_vec = make_tree_vector ();
17418 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17419 vec_safe_push (placement_vec, TREE_VALUE (placement));
17420 }
17421
17422 /* If there was an initializer in the original tree, but it
17423 instantiated to an empty list, then we should pass a
17424 non-NULL empty vector to tell build_new that it was an
17425 empty initializer() rather than no initializer. This can
17426 only happen when the initializer is a pack expansion whose
17427 parameter packs are of length zero. */
17428 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17429 init_vec = NULL;
17430 else
17431 {
17432 init_vec = make_tree_vector ();
17433 if (init == void_node)
17434 gcc_assert (init_vec != NULL);
17435 else
17436 {
17437 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17438 vec_safe_push (init_vec, TREE_VALUE (init));
17439 }
17440 }
17441
17442 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17443 tree op2 = RECUR (TREE_OPERAND (t, 2));
17444 ret = build_new (&placement_vec, op1, op2, &init_vec,
17445 NEW_EXPR_USE_GLOBAL (t),
17446 complain);
17447
17448 if (placement_vec != NULL)
17449 release_tree_vector (placement_vec);
17450 if (init_vec != NULL)
17451 release_tree_vector (init_vec);
17452
17453 RETURN (ret);
17454 }
17455
17456 case DELETE_EXPR:
17457 {
17458 tree op0 = RECUR (TREE_OPERAND (t, 0));
17459 tree op1 = RECUR (TREE_OPERAND (t, 1));
17460 RETURN (delete_sanity (op0, op1,
17461 DELETE_EXPR_USE_VEC (t),
17462 DELETE_EXPR_USE_GLOBAL (t),
17463 complain));
17464 }
17465
17466 case COMPOUND_EXPR:
17467 {
17468 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17469 complain & ~tf_decltype, in_decl,
17470 /*function_p=*/false,
17471 integral_constant_expression_p);
17472 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17473 op0,
17474 RECUR (TREE_OPERAND (t, 1)),
17475 complain|decltype_flag));
17476 }
17477
17478 case CALL_EXPR:
17479 {
17480 tree function;
17481 vec<tree, va_gc> *call_args;
17482 unsigned int nargs, i;
17483 bool qualified_p;
17484 bool koenig_p;
17485 tree ret;
17486
17487 function = CALL_EXPR_FN (t);
17488 /* Internal function with no arguments. */
17489 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17490 RETURN (t);
17491
17492 /* When we parsed the expression, we determined whether or
17493 not Koenig lookup should be performed. */
17494 koenig_p = KOENIG_LOOKUP_P (t);
17495 if (function == NULL_TREE)
17496 {
17497 koenig_p = false;
17498 qualified_p = false;
17499 }
17500 else if (TREE_CODE (function) == SCOPE_REF)
17501 {
17502 qualified_p = true;
17503 function = tsubst_qualified_id (function, args, complain, in_decl,
17504 /*done=*/false,
17505 /*address_p=*/false);
17506 }
17507 else if (koenig_p && identifier_p (function))
17508 {
17509 /* Do nothing; calling tsubst_copy_and_build on an identifier
17510 would incorrectly perform unqualified lookup again.
17511
17512 Note that we can also have an IDENTIFIER_NODE if the earlier
17513 unqualified lookup found a member function; in that case
17514 koenig_p will be false and we do want to do the lookup
17515 again to find the instantiated member function.
17516
17517 FIXME but doing that causes c++/15272, so we need to stop
17518 using IDENTIFIER_NODE in that situation. */
17519 qualified_p = false;
17520 }
17521 else
17522 {
17523 if (TREE_CODE (function) == COMPONENT_REF)
17524 {
17525 tree op = TREE_OPERAND (function, 1);
17526
17527 qualified_p = (TREE_CODE (op) == SCOPE_REF
17528 || (BASELINK_P (op)
17529 && BASELINK_QUALIFIED_P (op)));
17530 }
17531 else
17532 qualified_p = false;
17533
17534 if (TREE_CODE (function) == ADDR_EXPR
17535 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17536 /* Avoid error about taking the address of a constructor. */
17537 function = TREE_OPERAND (function, 0);
17538
17539 function = tsubst_copy_and_build (function, args, complain,
17540 in_decl,
17541 !qualified_p,
17542 integral_constant_expression_p);
17543
17544 if (BASELINK_P (function))
17545 qualified_p = true;
17546 }
17547
17548 nargs = call_expr_nargs (t);
17549 call_args = make_tree_vector ();
17550 for (i = 0; i < nargs; ++i)
17551 {
17552 tree arg = CALL_EXPR_ARG (t, i);
17553
17554 if (!PACK_EXPANSION_P (arg))
17555 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17556 else
17557 {
17558 /* Expand the pack expansion and push each entry onto
17559 CALL_ARGS. */
17560 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17561 if (TREE_CODE (arg) == TREE_VEC)
17562 {
17563 unsigned int len, j;
17564
17565 len = TREE_VEC_LENGTH (arg);
17566 for (j = 0; j < len; ++j)
17567 {
17568 tree value = TREE_VEC_ELT (arg, j);
17569 if (value != NULL_TREE)
17570 value = convert_from_reference (value);
17571 vec_safe_push (call_args, value);
17572 }
17573 }
17574 else
17575 {
17576 /* A partial substitution. Add one entry. */
17577 vec_safe_push (call_args, arg);
17578 }
17579 }
17580 }
17581
17582 /* We do not perform argument-dependent lookup if normal
17583 lookup finds a non-function, in accordance with the
17584 expected resolution of DR 218. */
17585 if (koenig_p
17586 && ((is_overloaded_fn (function)
17587 /* If lookup found a member function, the Koenig lookup is
17588 not appropriate, even if an unqualified-name was used
17589 to denote the function. */
17590 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17591 || identifier_p (function))
17592 /* Only do this when substitution turns a dependent call
17593 into a non-dependent call. */
17594 && type_dependent_expression_p_push (t)
17595 && !any_type_dependent_arguments_p (call_args))
17596 function = perform_koenig_lookup (function, call_args, tf_none);
17597
17598 if (function != NULL_TREE
17599 && identifier_p (function)
17600 && !any_type_dependent_arguments_p (call_args))
17601 {
17602 if (koenig_p && (complain & tf_warning_or_error))
17603 {
17604 /* For backwards compatibility and good diagnostics, try
17605 the unqualified lookup again if we aren't in SFINAE
17606 context. */
17607 tree unq = (tsubst_copy_and_build
17608 (function, args, complain, in_decl, true,
17609 integral_constant_expression_p));
17610 if (unq == error_mark_node)
17611 {
17612 release_tree_vector (call_args);
17613 RETURN (error_mark_node);
17614 }
17615
17616 if (unq != function)
17617 {
17618 /* In a lambda fn, we have to be careful to not
17619 introduce new this captures. Legacy code can't
17620 be using lambdas anyway, so it's ok to be
17621 stricter. */
17622 bool in_lambda = (current_class_type
17623 && LAMBDA_TYPE_P (current_class_type));
17624 char const *const msg
17625 = G_("%qD was not declared in this scope, "
17626 "and no declarations were found by "
17627 "argument-dependent lookup at the point "
17628 "of instantiation");
17629
17630 bool diag = true;
17631 if (in_lambda)
17632 error_at (EXPR_LOC_OR_LOC (t, input_location),
17633 msg, function);
17634 else
17635 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17636 msg, function);
17637 if (diag)
17638 {
17639 tree fn = unq;
17640
17641 if (INDIRECT_REF_P (fn))
17642 fn = TREE_OPERAND (fn, 0);
17643 if (is_overloaded_fn (fn))
17644 fn = get_first_fn (fn);
17645
17646 if (!DECL_P (fn))
17647 /* Can't say anything more. */;
17648 else if (DECL_CLASS_SCOPE_P (fn))
17649 {
17650 location_t loc = EXPR_LOC_OR_LOC (t,
17651 input_location);
17652 inform (loc,
17653 "declarations in dependent base %qT are "
17654 "not found by unqualified lookup",
17655 DECL_CLASS_CONTEXT (fn));
17656 if (current_class_ptr)
17657 inform (loc,
17658 "use %<this->%D%> instead", function);
17659 else
17660 inform (loc,
17661 "use %<%T::%D%> instead",
17662 current_class_name, function);
17663 }
17664 else
17665 inform (DECL_SOURCE_LOCATION (fn),
17666 "%qD declared here, later in the "
17667 "translation unit", fn);
17668 if (in_lambda)
17669 {
17670 release_tree_vector (call_args);
17671 RETURN (error_mark_node);
17672 }
17673 }
17674
17675 function = unq;
17676 }
17677 }
17678 if (identifier_p (function))
17679 {
17680 if (complain & tf_error)
17681 unqualified_name_lookup_error (function);
17682 release_tree_vector (call_args);
17683 RETURN (error_mark_node);
17684 }
17685 }
17686
17687 /* Remember that there was a reference to this entity. */
17688 if (function != NULL_TREE
17689 && DECL_P (function)
17690 && !mark_used (function, complain) && !(complain & tf_error))
17691 {
17692 release_tree_vector (call_args);
17693 RETURN (error_mark_node);
17694 }
17695
17696 /* Put back tf_decltype for the actual call. */
17697 complain |= decltype_flag;
17698
17699 if (function == NULL_TREE)
17700 switch (CALL_EXPR_IFN (t))
17701 {
17702 case IFN_LAUNDER:
17703 gcc_assert (nargs == 1);
17704 if (vec_safe_length (call_args) != 1)
17705 {
17706 error_at (EXPR_LOC_OR_LOC (t, input_location),
17707 "wrong number of arguments to "
17708 "%<__builtin_launder%>");
17709 ret = error_mark_node;
17710 }
17711 else
17712 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17713 input_location),
17714 (*call_args)[0], complain);
17715 break;
17716
17717 default:
17718 /* Unsupported internal function with arguments. */
17719 gcc_unreachable ();
17720 }
17721 else if (TREE_CODE (function) == OFFSET_REF)
17722 ret = build_offset_ref_call_from_tree (function, &call_args,
17723 complain);
17724 else if (TREE_CODE (function) == COMPONENT_REF)
17725 {
17726 tree instance = TREE_OPERAND (function, 0);
17727 tree fn = TREE_OPERAND (function, 1);
17728
17729 if (processing_template_decl
17730 && (type_dependent_expression_p (instance)
17731 || (!BASELINK_P (fn)
17732 && TREE_CODE (fn) != FIELD_DECL)
17733 || type_dependent_expression_p (fn)
17734 || any_type_dependent_arguments_p (call_args)))
17735 ret = build_min_nt_call_vec (function, call_args);
17736 else if (!BASELINK_P (fn))
17737 ret = finish_call_expr (function, &call_args,
17738 /*disallow_virtual=*/false,
17739 /*koenig_p=*/false,
17740 complain);
17741 else
17742 ret = (build_new_method_call
17743 (instance, fn,
17744 &call_args, NULL_TREE,
17745 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17746 /*fn_p=*/NULL,
17747 complain));
17748 }
17749 else
17750 ret = finish_call_expr (function, &call_args,
17751 /*disallow_virtual=*/qualified_p,
17752 koenig_p,
17753 complain);
17754
17755 release_tree_vector (call_args);
17756
17757 if (ret != error_mark_node)
17758 {
17759 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17760 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17761 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17762 bool thk = CALL_FROM_THUNK_P (t);
17763 if (op || ord || rev || thk)
17764 {
17765 function = extract_call_expr (ret);
17766 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17767 CALL_EXPR_ORDERED_ARGS (function) = ord;
17768 CALL_EXPR_REVERSE_ARGS (function) = rev;
17769 if (thk)
17770 {
17771 CALL_FROM_THUNK_P (function) = true;
17772 /* The thunk location is not interesting. */
17773 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17774 }
17775 }
17776 }
17777
17778 RETURN (ret);
17779 }
17780
17781 case COND_EXPR:
17782 {
17783 tree cond = RECUR (TREE_OPERAND (t, 0));
17784 tree folded_cond = fold_non_dependent_expr (cond);
17785 tree exp1, exp2;
17786
17787 if (TREE_CODE (folded_cond) == INTEGER_CST)
17788 {
17789 if (integer_zerop (folded_cond))
17790 {
17791 ++c_inhibit_evaluation_warnings;
17792 exp1 = RECUR (TREE_OPERAND (t, 1));
17793 --c_inhibit_evaluation_warnings;
17794 exp2 = RECUR (TREE_OPERAND (t, 2));
17795 }
17796 else
17797 {
17798 exp1 = RECUR (TREE_OPERAND (t, 1));
17799 ++c_inhibit_evaluation_warnings;
17800 exp2 = RECUR (TREE_OPERAND (t, 2));
17801 --c_inhibit_evaluation_warnings;
17802 }
17803 cond = folded_cond;
17804 }
17805 else
17806 {
17807 exp1 = RECUR (TREE_OPERAND (t, 1));
17808 exp2 = RECUR (TREE_OPERAND (t, 2));
17809 }
17810
17811 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17812 cond, exp1, exp2, complain));
17813 }
17814
17815 case PSEUDO_DTOR_EXPR:
17816 {
17817 tree op0 = RECUR (TREE_OPERAND (t, 0));
17818 tree op1 = RECUR (TREE_OPERAND (t, 1));
17819 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17820 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17821 input_location));
17822 }
17823
17824 case TREE_LIST:
17825 {
17826 tree purpose, value, chain;
17827
17828 if (t == void_list_node)
17829 RETURN (t);
17830
17831 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17832 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17833 {
17834 /* We have pack expansions, so expand those and
17835 create a new list out of it. */
17836 tree purposevec = NULL_TREE;
17837 tree valuevec = NULL_TREE;
17838 tree chain;
17839 int i, len = -1;
17840
17841 /* Expand the argument expressions. */
17842 if (TREE_PURPOSE (t))
17843 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17844 complain, in_decl);
17845 if (TREE_VALUE (t))
17846 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17847 complain, in_decl);
17848
17849 /* Build the rest of the list. */
17850 chain = TREE_CHAIN (t);
17851 if (chain && chain != void_type_node)
17852 chain = RECUR (chain);
17853
17854 /* Determine the number of arguments. */
17855 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17856 {
17857 len = TREE_VEC_LENGTH (purposevec);
17858 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17859 }
17860 else if (TREE_CODE (valuevec) == TREE_VEC)
17861 len = TREE_VEC_LENGTH (valuevec);
17862 else
17863 {
17864 /* Since we only performed a partial substitution into
17865 the argument pack, we only RETURN (a single list
17866 node. */
17867 if (purposevec == TREE_PURPOSE (t)
17868 && valuevec == TREE_VALUE (t)
17869 && chain == TREE_CHAIN (t))
17870 RETURN (t);
17871
17872 RETURN (tree_cons (purposevec, valuevec, chain));
17873 }
17874
17875 /* Convert the argument vectors into a TREE_LIST */
17876 i = len;
17877 while (i > 0)
17878 {
17879 /* Grab the Ith values. */
17880 i--;
17881 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17882 : NULL_TREE;
17883 value
17884 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17885 : NULL_TREE;
17886
17887 /* Build the list (backwards). */
17888 chain = tree_cons (purpose, value, chain);
17889 }
17890
17891 RETURN (chain);
17892 }
17893
17894 purpose = TREE_PURPOSE (t);
17895 if (purpose)
17896 purpose = RECUR (purpose);
17897 value = TREE_VALUE (t);
17898 if (value)
17899 value = RECUR (value);
17900 chain = TREE_CHAIN (t);
17901 if (chain && chain != void_type_node)
17902 chain = RECUR (chain);
17903 if (purpose == TREE_PURPOSE (t)
17904 && value == TREE_VALUE (t)
17905 && chain == TREE_CHAIN (t))
17906 RETURN (t);
17907 RETURN (tree_cons (purpose, value, chain));
17908 }
17909
17910 case COMPONENT_REF:
17911 {
17912 tree object;
17913 tree object_type;
17914 tree member;
17915 tree r;
17916
17917 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17918 args, complain, in_decl);
17919 /* Remember that there was a reference to this entity. */
17920 if (DECL_P (object)
17921 && !mark_used (object, complain) && !(complain & tf_error))
17922 RETURN (error_mark_node);
17923 object_type = TREE_TYPE (object);
17924
17925 member = TREE_OPERAND (t, 1);
17926 if (BASELINK_P (member))
17927 member = tsubst_baselink (member,
17928 non_reference (TREE_TYPE (object)),
17929 args, complain, in_decl);
17930 else
17931 member = tsubst_copy (member, args, complain, in_decl);
17932 if (member == error_mark_node)
17933 RETURN (error_mark_node);
17934
17935 if (TREE_CODE (member) == FIELD_DECL)
17936 {
17937 r = finish_non_static_data_member (member, object, NULL_TREE);
17938 if (TREE_CODE (r) == COMPONENT_REF)
17939 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17940 RETURN (r);
17941 }
17942 else if (type_dependent_expression_p (object))
17943 /* We can't do much here. */;
17944 else if (!CLASS_TYPE_P (object_type))
17945 {
17946 if (scalarish_type_p (object_type))
17947 {
17948 tree s = NULL_TREE;
17949 tree dtor = member;
17950
17951 if (TREE_CODE (dtor) == SCOPE_REF)
17952 {
17953 s = TREE_OPERAND (dtor, 0);
17954 dtor = TREE_OPERAND (dtor, 1);
17955 }
17956 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17957 {
17958 dtor = TREE_OPERAND (dtor, 0);
17959 if (TYPE_P (dtor))
17960 RETURN (finish_pseudo_destructor_expr
17961 (object, s, dtor, input_location));
17962 }
17963 }
17964 }
17965 else if (TREE_CODE (member) == SCOPE_REF
17966 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17967 {
17968 /* Lookup the template functions now that we know what the
17969 scope is. */
17970 tree scope = TREE_OPERAND (member, 0);
17971 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17972 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17973 member = lookup_qualified_name (scope, tmpl,
17974 /*is_type_p=*/false,
17975 /*complain=*/false);
17976 if (BASELINK_P (member))
17977 {
17978 BASELINK_FUNCTIONS (member)
17979 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17980 args);
17981 member = (adjust_result_of_qualified_name_lookup
17982 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17983 object_type));
17984 }
17985 else
17986 {
17987 qualified_name_lookup_error (scope, tmpl, member,
17988 input_location);
17989 RETURN (error_mark_node);
17990 }
17991 }
17992 else if (TREE_CODE (member) == SCOPE_REF
17993 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17994 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17995 {
17996 if (complain & tf_error)
17997 {
17998 if (TYPE_P (TREE_OPERAND (member, 0)))
17999 error ("%qT is not a class or namespace",
18000 TREE_OPERAND (member, 0));
18001 else
18002 error ("%qD is not a class or namespace",
18003 TREE_OPERAND (member, 0));
18004 }
18005 RETURN (error_mark_node);
18006 }
18007
18008 r = finish_class_member_access_expr (object, member,
18009 /*template_p=*/false,
18010 complain);
18011 if (TREE_CODE (r) == COMPONENT_REF)
18012 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18013 RETURN (r);
18014 }
18015
18016 case THROW_EXPR:
18017 RETURN (build_throw
18018 (RECUR (TREE_OPERAND (t, 0))));
18019
18020 case CONSTRUCTOR:
18021 {
18022 vec<constructor_elt, va_gc> *n;
18023 constructor_elt *ce;
18024 unsigned HOST_WIDE_INT idx;
18025 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18026 bool process_index_p;
18027 int newlen;
18028 bool need_copy_p = false;
18029 tree r;
18030
18031 if (type == error_mark_node)
18032 RETURN (error_mark_node);
18033
18034 /* digest_init will do the wrong thing if we let it. */
18035 if (type && TYPE_PTRMEMFUNC_P (type))
18036 RETURN (t);
18037
18038 /* We do not want to process the index of aggregate
18039 initializers as they are identifier nodes which will be
18040 looked up by digest_init. */
18041 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18042
18043 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18044 newlen = vec_safe_length (n);
18045 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18046 {
18047 if (ce->index && process_index_p
18048 /* An identifier index is looked up in the type
18049 being initialized, not the current scope. */
18050 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18051 ce->index = RECUR (ce->index);
18052
18053 if (PACK_EXPANSION_P (ce->value))
18054 {
18055 /* Substitute into the pack expansion. */
18056 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18057 in_decl);
18058
18059 if (ce->value == error_mark_node
18060 || PACK_EXPANSION_P (ce->value))
18061 ;
18062 else if (TREE_VEC_LENGTH (ce->value) == 1)
18063 /* Just move the argument into place. */
18064 ce->value = TREE_VEC_ELT (ce->value, 0);
18065 else
18066 {
18067 /* Update the length of the final CONSTRUCTOR
18068 arguments vector, and note that we will need to
18069 copy.*/
18070 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18071 need_copy_p = true;
18072 }
18073 }
18074 else
18075 ce->value = RECUR (ce->value);
18076 }
18077
18078 if (need_copy_p)
18079 {
18080 vec<constructor_elt, va_gc> *old_n = n;
18081
18082 vec_alloc (n, newlen);
18083 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18084 {
18085 if (TREE_CODE (ce->value) == TREE_VEC)
18086 {
18087 int i, len = TREE_VEC_LENGTH (ce->value);
18088 for (i = 0; i < len; ++i)
18089 CONSTRUCTOR_APPEND_ELT (n, 0,
18090 TREE_VEC_ELT (ce->value, i));
18091 }
18092 else
18093 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18094 }
18095 }
18096
18097 r = build_constructor (init_list_type_node, n);
18098 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18099
18100 if (TREE_HAS_CONSTRUCTOR (t))
18101 {
18102 fcl_t cl = fcl_functional;
18103 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18104 cl = fcl_c99;
18105 RETURN (finish_compound_literal (type, r, complain, cl));
18106 }
18107
18108 TREE_TYPE (r) = type;
18109 RETURN (r);
18110 }
18111
18112 case TYPEID_EXPR:
18113 {
18114 tree operand_0 = TREE_OPERAND (t, 0);
18115 if (TYPE_P (operand_0))
18116 {
18117 operand_0 = tsubst (operand_0, args, complain, in_decl);
18118 RETURN (get_typeid (operand_0, complain));
18119 }
18120 else
18121 {
18122 operand_0 = RECUR (operand_0);
18123 RETURN (build_typeid (operand_0, complain));
18124 }
18125 }
18126
18127 case VAR_DECL:
18128 if (!args)
18129 RETURN (t);
18130 else if (DECL_PACK_P (t))
18131 {
18132 /* We don't build decls for an instantiation of a
18133 variadic capture proxy, we instantiate the elements
18134 when needed. */
18135 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
18136 return RECUR (DECL_VALUE_EXPR (t));
18137 }
18138 /* Fall through */
18139
18140 case PARM_DECL:
18141 {
18142 tree r = tsubst_copy (t, args, complain, in_decl);
18143 /* ??? We're doing a subset of finish_id_expression here. */
18144 if (VAR_P (r)
18145 && !processing_template_decl
18146 && !cp_unevaluated_operand
18147 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18148 && CP_DECL_THREAD_LOCAL_P (r))
18149 {
18150 if (tree wrap = get_tls_wrapper_fn (r))
18151 /* Replace an evaluated use of the thread_local variable with
18152 a call to its wrapper. */
18153 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18154 }
18155 else if (outer_automatic_var_p (r))
18156 r = process_outer_var_ref (r, complain);
18157
18158 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18159 /* If the original type was a reference, we'll be wrapped in
18160 the appropriate INDIRECT_REF. */
18161 r = convert_from_reference (r);
18162 RETURN (r);
18163 }
18164
18165 case VA_ARG_EXPR:
18166 {
18167 tree op0 = RECUR (TREE_OPERAND (t, 0));
18168 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18169 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18170 }
18171
18172 case OFFSETOF_EXPR:
18173 {
18174 tree object_ptr
18175 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18176 in_decl, /*function_p=*/false,
18177 /*integral_constant_expression_p=*/false);
18178 RETURN (finish_offsetof (object_ptr,
18179 RECUR (TREE_OPERAND (t, 0)),
18180 EXPR_LOCATION (t)));
18181 }
18182
18183 case ADDRESSOF_EXPR:
18184 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18185 RECUR (TREE_OPERAND (t, 0)), complain));
18186
18187 case TRAIT_EXPR:
18188 {
18189 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18190 complain, in_decl);
18191
18192 tree type2 = TRAIT_EXPR_TYPE2 (t);
18193 if (type2 && TREE_CODE (type2) == TREE_LIST)
18194 type2 = RECUR (type2);
18195 else if (type2)
18196 type2 = tsubst (type2, args, complain, in_decl);
18197
18198 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18199 }
18200
18201 case STMT_EXPR:
18202 {
18203 tree old_stmt_expr = cur_stmt_expr;
18204 tree stmt_expr = begin_stmt_expr ();
18205
18206 cur_stmt_expr = stmt_expr;
18207 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18208 integral_constant_expression_p);
18209 stmt_expr = finish_stmt_expr (stmt_expr, false);
18210 cur_stmt_expr = old_stmt_expr;
18211
18212 /* If the resulting list of expression statement is empty,
18213 fold it further into void_node. */
18214 if (empty_expr_stmt_p (stmt_expr))
18215 stmt_expr = void_node;
18216
18217 RETURN (stmt_expr);
18218 }
18219
18220 case LAMBDA_EXPR:
18221 {
18222 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18223
18224 RETURN (build_lambda_object (r));
18225 }
18226
18227 case TARGET_EXPR:
18228 /* We can get here for a constant initializer of non-dependent type.
18229 FIXME stop folding in cp_parser_initializer_clause. */
18230 {
18231 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18232 complain);
18233 RETURN (r);
18234 }
18235
18236 case TRANSACTION_EXPR:
18237 RETURN (tsubst_expr(t, args, complain, in_decl,
18238 integral_constant_expression_p));
18239
18240 case PAREN_EXPR:
18241 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18242
18243 case VEC_PERM_EXPR:
18244 {
18245 tree op0 = RECUR (TREE_OPERAND (t, 0));
18246 tree op1 = RECUR (TREE_OPERAND (t, 1));
18247 tree op2 = RECUR (TREE_OPERAND (t, 2));
18248 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18249 complain));
18250 }
18251
18252 case REQUIRES_EXPR:
18253 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18254
18255 default:
18256 /* Handle Objective-C++ constructs, if appropriate. */
18257 {
18258 tree subst
18259 = objcp_tsubst_copy_and_build (t, args, complain,
18260 in_decl, /*function_p=*/false);
18261 if (subst)
18262 RETURN (subst);
18263 }
18264 RETURN (tsubst_copy (t, args, complain, in_decl));
18265 }
18266
18267 #undef RECUR
18268 #undef RETURN
18269 out:
18270 input_location = loc;
18271 return retval;
18272 }
18273
18274 /* Verify that the instantiated ARGS are valid. For type arguments,
18275 make sure that the type's linkage is ok. For non-type arguments,
18276 make sure they are constants if they are integral or enumerations.
18277 Emit an error under control of COMPLAIN, and return TRUE on error. */
18278
18279 static bool
18280 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18281 {
18282 if (dependent_template_arg_p (t))
18283 return false;
18284 if (ARGUMENT_PACK_P (t))
18285 {
18286 tree vec = ARGUMENT_PACK_ARGS (t);
18287 int len = TREE_VEC_LENGTH (vec);
18288 bool result = false;
18289 int i;
18290
18291 for (i = 0; i < len; ++i)
18292 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18293 result = true;
18294 return result;
18295 }
18296 else if (TYPE_P (t))
18297 {
18298 /* [basic.link]: A name with no linkage (notably, the name
18299 of a class or enumeration declared in a local scope)
18300 shall not be used to declare an entity with linkage.
18301 This implies that names with no linkage cannot be used as
18302 template arguments
18303
18304 DR 757 relaxes this restriction for C++0x. */
18305 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18306 : no_linkage_check (t, /*relaxed_p=*/false));
18307
18308 if (nt)
18309 {
18310 /* DR 488 makes use of a type with no linkage cause
18311 type deduction to fail. */
18312 if (complain & tf_error)
18313 {
18314 if (TYPE_UNNAMED_P (nt))
18315 error ("%qT is/uses unnamed type", t);
18316 else
18317 error ("template argument for %qD uses local type %qT",
18318 tmpl, t);
18319 }
18320 return true;
18321 }
18322 /* In order to avoid all sorts of complications, we do not
18323 allow variably-modified types as template arguments. */
18324 else if (variably_modified_type_p (t, NULL_TREE))
18325 {
18326 if (complain & tf_error)
18327 error ("%qT is a variably modified type", t);
18328 return true;
18329 }
18330 }
18331 /* Class template and alias template arguments should be OK. */
18332 else if (DECL_TYPE_TEMPLATE_P (t))
18333 ;
18334 /* A non-type argument of integral or enumerated type must be a
18335 constant. */
18336 else if (TREE_TYPE (t)
18337 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18338 && !REFERENCE_REF_P (t)
18339 && !TREE_CONSTANT (t))
18340 {
18341 if (complain & tf_error)
18342 error ("integral expression %qE is not constant", t);
18343 return true;
18344 }
18345 return false;
18346 }
18347
18348 static bool
18349 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18350 {
18351 int ix, len = DECL_NTPARMS (tmpl);
18352 bool result = false;
18353
18354 for (ix = 0; ix != len; ix++)
18355 {
18356 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18357 result = true;
18358 }
18359 if (result && (complain & tf_error))
18360 error (" trying to instantiate %qD", tmpl);
18361 return result;
18362 }
18363
18364 /* We're out of SFINAE context now, so generate diagnostics for the access
18365 errors we saw earlier when instantiating D from TMPL and ARGS. */
18366
18367 static void
18368 recheck_decl_substitution (tree d, tree tmpl, tree args)
18369 {
18370 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18371 tree type = TREE_TYPE (pattern);
18372 location_t loc = input_location;
18373
18374 push_access_scope (d);
18375 push_deferring_access_checks (dk_no_deferred);
18376 input_location = DECL_SOURCE_LOCATION (pattern);
18377 tsubst (type, args, tf_warning_or_error, d);
18378 input_location = loc;
18379 pop_deferring_access_checks ();
18380 pop_access_scope (d);
18381 }
18382
18383 /* Instantiate the indicated variable, function, or alias template TMPL with
18384 the template arguments in TARG_PTR. */
18385
18386 static tree
18387 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18388 {
18389 tree targ_ptr = orig_args;
18390 tree fndecl;
18391 tree gen_tmpl;
18392 tree spec;
18393 bool access_ok = true;
18394
18395 if (tmpl == error_mark_node)
18396 return error_mark_node;
18397
18398 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18399
18400 /* If this function is a clone, handle it specially. */
18401 if (DECL_CLONED_FUNCTION_P (tmpl))
18402 {
18403 tree spec;
18404 tree clone;
18405
18406 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18407 DECL_CLONED_FUNCTION. */
18408 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18409 targ_ptr, complain);
18410 if (spec == error_mark_node)
18411 return error_mark_node;
18412
18413 /* Look for the clone. */
18414 FOR_EACH_CLONE (clone, spec)
18415 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18416 return clone;
18417 /* We should always have found the clone by now. */
18418 gcc_unreachable ();
18419 return NULL_TREE;
18420 }
18421
18422 if (targ_ptr == error_mark_node)
18423 return error_mark_node;
18424
18425 /* Check to see if we already have this specialization. */
18426 gen_tmpl = most_general_template (tmpl);
18427 if (TMPL_ARGS_DEPTH (targ_ptr)
18428 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18429 /* targ_ptr only has the innermost template args, so add the outer ones
18430 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18431 the case of a non-dependent call within a template definition). */
18432 targ_ptr = (add_outermost_template_args
18433 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18434 targ_ptr));
18435
18436 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18437 but it doesn't seem to be on the hot path. */
18438 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18439
18440 gcc_assert (tmpl == gen_tmpl
18441 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18442 == spec)
18443 || fndecl == NULL_TREE);
18444
18445 if (spec != NULL_TREE)
18446 {
18447 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18448 {
18449 if (complain & tf_error)
18450 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18451 return error_mark_node;
18452 }
18453 return spec;
18454 }
18455
18456 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18457 complain))
18458 return error_mark_node;
18459
18460 /* We are building a FUNCTION_DECL, during which the access of its
18461 parameters and return types have to be checked. However this
18462 FUNCTION_DECL which is the desired context for access checking
18463 is not built yet. We solve this chicken-and-egg problem by
18464 deferring all checks until we have the FUNCTION_DECL. */
18465 push_deferring_access_checks (dk_deferred);
18466
18467 /* Instantiation of the function happens in the context of the function
18468 template, not the context of the overload resolution we're doing. */
18469 push_to_top_level ();
18470 /* If there are dependent arguments, e.g. because we're doing partial
18471 ordering, make sure processing_template_decl stays set. */
18472 if (uses_template_parms (targ_ptr))
18473 ++processing_template_decl;
18474 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18475 {
18476 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18477 complain, gen_tmpl, true);
18478 push_nested_class (ctx);
18479 }
18480
18481 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18482
18483 fndecl = NULL_TREE;
18484 if (VAR_P (pattern))
18485 {
18486 /* We need to determine if we're using a partial or explicit
18487 specialization now, because the type of the variable could be
18488 different. */
18489 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18490 tree elt = most_specialized_partial_spec (tid, complain);
18491 if (elt == error_mark_node)
18492 pattern = error_mark_node;
18493 else if (elt)
18494 {
18495 tree partial_tmpl = TREE_VALUE (elt);
18496 tree partial_args = TREE_PURPOSE (elt);
18497 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18498 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18499 }
18500 }
18501
18502 /* Substitute template parameters to obtain the specialization. */
18503 if (fndecl == NULL_TREE)
18504 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18505 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18506 pop_nested_class ();
18507 pop_from_top_level ();
18508
18509 if (fndecl == error_mark_node)
18510 {
18511 pop_deferring_access_checks ();
18512 return error_mark_node;
18513 }
18514
18515 /* The DECL_TI_TEMPLATE should always be the immediate parent
18516 template, not the most general template. */
18517 DECL_TI_TEMPLATE (fndecl) = tmpl;
18518 DECL_TI_ARGS (fndecl) = targ_ptr;
18519
18520 /* Now we know the specialization, compute access previously
18521 deferred. Do no access control for inheriting constructors,
18522 as we already checked access for the inherited constructor. */
18523 if (!(flag_new_inheriting_ctors
18524 && DECL_INHERITED_CTOR (fndecl)))
18525 {
18526 push_access_scope (fndecl);
18527 if (!perform_deferred_access_checks (complain))
18528 access_ok = false;
18529 pop_access_scope (fndecl);
18530 }
18531 pop_deferring_access_checks ();
18532
18533 /* If we've just instantiated the main entry point for a function,
18534 instantiate all the alternate entry points as well. We do this
18535 by cloning the instantiation of the main entry point, not by
18536 instantiating the template clones. */
18537 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18538 clone_function_decl (fndecl, /*update_methods=*/false);
18539
18540 if (!access_ok)
18541 {
18542 if (!(complain & tf_error))
18543 {
18544 /* Remember to reinstantiate when we're out of SFINAE so the user
18545 can see the errors. */
18546 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18547 }
18548 return error_mark_node;
18549 }
18550 return fndecl;
18551 }
18552
18553 /* Wrapper for instantiate_template_1. */
18554
18555 tree
18556 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18557 {
18558 tree ret;
18559 timevar_push (TV_TEMPLATE_INST);
18560 ret = instantiate_template_1 (tmpl, orig_args, complain);
18561 timevar_pop (TV_TEMPLATE_INST);
18562 return ret;
18563 }
18564
18565 /* Instantiate the alias template TMPL with ARGS. Also push a template
18566 instantiation level, which instantiate_template doesn't do because
18567 functions and variables have sufficient context established by the
18568 callers. */
18569
18570 static tree
18571 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18572 {
18573 struct pending_template *old_last_pend = last_pending_template;
18574 struct tinst_level *old_error_tinst = last_error_tinst_level;
18575 if (tmpl == error_mark_node || args == error_mark_node)
18576 return error_mark_node;
18577 tree tinst = build_tree_list (tmpl, args);
18578 if (!push_tinst_level (tinst))
18579 {
18580 ggc_free (tinst);
18581 return error_mark_node;
18582 }
18583
18584 args =
18585 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18586 args, tmpl, complain,
18587 /*require_all_args=*/true,
18588 /*use_default_args=*/true);
18589
18590 tree r = instantiate_template (tmpl, args, complain);
18591 pop_tinst_level ();
18592 /* We can't free this if a pending_template entry or last_error_tinst_level
18593 is pointing at it. */
18594 if (last_pending_template == old_last_pend
18595 && last_error_tinst_level == old_error_tinst)
18596 ggc_free (tinst);
18597
18598 return r;
18599 }
18600
18601 /* PARM is a template parameter pack for FN. Returns true iff
18602 PARM is used in a deducible way in the argument list of FN. */
18603
18604 static bool
18605 pack_deducible_p (tree parm, tree fn)
18606 {
18607 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18608 for (; t; t = TREE_CHAIN (t))
18609 {
18610 tree type = TREE_VALUE (t);
18611 tree packs;
18612 if (!PACK_EXPANSION_P (type))
18613 continue;
18614 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18615 packs; packs = TREE_CHAIN (packs))
18616 if (template_args_equal (TREE_VALUE (packs), parm))
18617 {
18618 /* The template parameter pack is used in a function parameter
18619 pack. If this is the end of the parameter list, the
18620 template parameter pack is deducible. */
18621 if (TREE_CHAIN (t) == void_list_node)
18622 return true;
18623 else
18624 /* Otherwise, not. Well, it could be deduced from
18625 a non-pack parameter, but doing so would end up with
18626 a deduction mismatch, so don't bother. */
18627 return false;
18628 }
18629 }
18630 /* The template parameter pack isn't used in any function parameter
18631 packs, but it might be used deeper, e.g. tuple<Args...>. */
18632 return true;
18633 }
18634
18635 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18636 NARGS elements of the arguments that are being used when calling
18637 it. TARGS is a vector into which the deduced template arguments
18638 are placed.
18639
18640 Returns either a FUNCTION_DECL for the matching specialization of FN or
18641 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18642 true, diagnostics will be printed to explain why it failed.
18643
18644 If FN is a conversion operator, or we are trying to produce a specific
18645 specialization, RETURN_TYPE is the return type desired.
18646
18647 The EXPLICIT_TARGS are explicit template arguments provided via a
18648 template-id.
18649
18650 The parameter STRICT is one of:
18651
18652 DEDUCE_CALL:
18653 We are deducing arguments for a function call, as in
18654 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18655 deducing arguments for a call to the result of a conversion
18656 function template, as in [over.call.object].
18657
18658 DEDUCE_CONV:
18659 We are deducing arguments for a conversion function, as in
18660 [temp.deduct.conv].
18661
18662 DEDUCE_EXACT:
18663 We are deducing arguments when doing an explicit instantiation
18664 as in [temp.explicit], when determining an explicit specialization
18665 as in [temp.expl.spec], or when taking the address of a function
18666 template, as in [temp.deduct.funcaddr]. */
18667
18668 tree
18669 fn_type_unification (tree fn,
18670 tree explicit_targs,
18671 tree targs,
18672 const tree *args,
18673 unsigned int nargs,
18674 tree return_type,
18675 unification_kind_t strict,
18676 int flags,
18677 bool explain_p,
18678 bool decltype_p)
18679 {
18680 tree parms;
18681 tree fntype;
18682 tree decl = NULL_TREE;
18683 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18684 bool ok;
18685 static int deduction_depth;
18686 struct pending_template *old_last_pend = last_pending_template;
18687 struct tinst_level *old_error_tinst = last_error_tinst_level;
18688
18689 tree orig_fn = fn;
18690 if (flag_new_inheriting_ctors)
18691 fn = strip_inheriting_ctors (fn);
18692
18693 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18694 tree tinst;
18695 tree r = error_mark_node;
18696
18697 tree full_targs = targs;
18698 if (TMPL_ARGS_DEPTH (targs)
18699 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18700 full_targs = (add_outermost_template_args
18701 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18702 targs));
18703
18704 if (decltype_p)
18705 complain |= tf_decltype;
18706
18707 /* In C++0x, it's possible to have a function template whose type depends
18708 on itself recursively. This is most obvious with decltype, but can also
18709 occur with enumeration scope (c++/48969). So we need to catch infinite
18710 recursion and reject the substitution at deduction time; this function
18711 will return error_mark_node for any repeated substitution.
18712
18713 This also catches excessive recursion such as when f<N> depends on
18714 f<N-1> across all integers, and returns error_mark_node for all the
18715 substitutions back up to the initial one.
18716
18717 This is, of course, not reentrant. */
18718 if (excessive_deduction_depth)
18719 return error_mark_node;
18720 tinst = build_tree_list (fn, NULL_TREE);
18721 ++deduction_depth;
18722
18723 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18724
18725 fntype = TREE_TYPE (fn);
18726 if (explicit_targs)
18727 {
18728 /* [temp.deduct]
18729
18730 The specified template arguments must match the template
18731 parameters in kind (i.e., type, nontype, template), and there
18732 must not be more arguments than there are parameters;
18733 otherwise type deduction fails.
18734
18735 Nontype arguments must match the types of the corresponding
18736 nontype template parameters, or must be convertible to the
18737 types of the corresponding nontype parameters as specified in
18738 _temp.arg.nontype_, otherwise type deduction fails.
18739
18740 All references in the function type of the function template
18741 to the corresponding template parameters are replaced by the
18742 specified template argument values. If a substitution in a
18743 template parameter or in the function type of the function
18744 template results in an invalid type, type deduction fails. */
18745 int i, len = TREE_VEC_LENGTH (tparms);
18746 location_t loc = input_location;
18747 bool incomplete = false;
18748
18749 if (explicit_targs == error_mark_node)
18750 goto fail;
18751
18752 if (TMPL_ARGS_DEPTH (explicit_targs)
18753 < TMPL_ARGS_DEPTH (full_targs))
18754 explicit_targs = add_outermost_template_args (full_targs,
18755 explicit_targs);
18756
18757 /* Adjust any explicit template arguments before entering the
18758 substitution context. */
18759 explicit_targs
18760 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18761 complain,
18762 /*require_all_args=*/false,
18763 /*use_default_args=*/false));
18764 if (explicit_targs == error_mark_node)
18765 goto fail;
18766
18767 /* Substitute the explicit args into the function type. This is
18768 necessary so that, for instance, explicitly declared function
18769 arguments can match null pointed constants. If we were given
18770 an incomplete set of explicit args, we must not do semantic
18771 processing during substitution as we could create partial
18772 instantiations. */
18773 for (i = 0; i < len; i++)
18774 {
18775 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18776 bool parameter_pack = false;
18777 tree targ = TREE_VEC_ELT (explicit_targs, i);
18778
18779 /* Dig out the actual parm. */
18780 if (TREE_CODE (parm) == TYPE_DECL
18781 || TREE_CODE (parm) == TEMPLATE_DECL)
18782 {
18783 parm = TREE_TYPE (parm);
18784 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18785 }
18786 else if (TREE_CODE (parm) == PARM_DECL)
18787 {
18788 parm = DECL_INITIAL (parm);
18789 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18790 }
18791
18792 if (!parameter_pack && targ == NULL_TREE)
18793 /* No explicit argument for this template parameter. */
18794 incomplete = true;
18795
18796 if (parameter_pack && pack_deducible_p (parm, fn))
18797 {
18798 /* Mark the argument pack as "incomplete". We could
18799 still deduce more arguments during unification.
18800 We remove this mark in type_unification_real. */
18801 if (targ)
18802 {
18803 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18804 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18805 = ARGUMENT_PACK_ARGS (targ);
18806 }
18807
18808 /* We have some incomplete argument packs. */
18809 incomplete = true;
18810 }
18811 }
18812
18813 TREE_VALUE (tinst) = explicit_targs;
18814 if (!push_tinst_level (tinst))
18815 {
18816 excessive_deduction_depth = true;
18817 goto fail;
18818 }
18819 processing_template_decl += incomplete;
18820 input_location = DECL_SOURCE_LOCATION (fn);
18821 /* Ignore any access checks; we'll see them again in
18822 instantiate_template and they might have the wrong
18823 access path at this point. */
18824 push_deferring_access_checks (dk_deferred);
18825 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18826 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18827 pop_deferring_access_checks ();
18828 input_location = loc;
18829 processing_template_decl -= incomplete;
18830 pop_tinst_level ();
18831
18832 if (fntype == error_mark_node)
18833 goto fail;
18834
18835 /* Place the explicitly specified arguments in TARGS. */
18836 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18837 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18838 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18839 }
18840
18841 /* Never do unification on the 'this' parameter. */
18842 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18843
18844 if (return_type && strict == DEDUCE_CALL)
18845 {
18846 /* We're deducing for a call to the result of a template conversion
18847 function. The parms we really want are in return_type. */
18848 if (POINTER_TYPE_P (return_type))
18849 return_type = TREE_TYPE (return_type);
18850 parms = TYPE_ARG_TYPES (return_type);
18851 }
18852 else if (return_type)
18853 {
18854 tree *new_args;
18855
18856 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18857 new_args = XALLOCAVEC (tree, nargs + 1);
18858 new_args[0] = return_type;
18859 memcpy (new_args + 1, args, nargs * sizeof (tree));
18860 args = new_args;
18861 ++nargs;
18862 }
18863
18864 /* We allow incomplete unification without an error message here
18865 because the standard doesn't seem to explicitly prohibit it. Our
18866 callers must be ready to deal with unification failures in any
18867 event. */
18868
18869 TREE_VALUE (tinst) = targs;
18870 /* If we aren't explaining yet, push tinst context so we can see where
18871 any errors (e.g. from class instantiations triggered by instantiation
18872 of default template arguments) come from. If we are explaining, this
18873 context is redundant. */
18874 if (!explain_p && !push_tinst_level (tinst))
18875 {
18876 excessive_deduction_depth = true;
18877 goto fail;
18878 }
18879
18880 /* type_unification_real will pass back any access checks from default
18881 template argument substitution. */
18882 vec<deferred_access_check, va_gc> *checks;
18883 checks = NULL;
18884
18885 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18886 full_targs, parms, args, nargs, /*subr=*/0,
18887 strict, flags, &checks, explain_p);
18888 if (!explain_p)
18889 pop_tinst_level ();
18890 if (!ok)
18891 goto fail;
18892
18893 /* Now that we have bindings for all of the template arguments,
18894 ensure that the arguments deduced for the template template
18895 parameters have compatible template parameter lists. We cannot
18896 check this property before we have deduced all template
18897 arguments, because the template parameter types of a template
18898 template parameter might depend on prior template parameters
18899 deduced after the template template parameter. The following
18900 ill-formed example illustrates this issue:
18901
18902 template<typename T, template<T> class C> void f(C<5>, T);
18903
18904 template<int N> struct X {};
18905
18906 void g() {
18907 f(X<5>(), 5l); // error: template argument deduction fails
18908 }
18909
18910 The template parameter list of 'C' depends on the template type
18911 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18912 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18913 time that we deduce 'C'. */
18914 if (!template_template_parm_bindings_ok_p
18915 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18916 {
18917 unify_inconsistent_template_template_parameters (explain_p);
18918 goto fail;
18919 }
18920
18921 /* All is well so far. Now, check:
18922
18923 [temp.deduct]
18924
18925 When all template arguments have been deduced, all uses of
18926 template parameters in nondeduced contexts are replaced with
18927 the corresponding deduced argument values. If the
18928 substitution results in an invalid type, as described above,
18929 type deduction fails. */
18930 TREE_VALUE (tinst) = targs;
18931 if (!push_tinst_level (tinst))
18932 {
18933 excessive_deduction_depth = true;
18934 goto fail;
18935 }
18936
18937 /* Also collect access checks from the instantiation. */
18938 reopen_deferring_access_checks (checks);
18939
18940 decl = instantiate_template (fn, targs, complain);
18941
18942 checks = get_deferred_access_checks ();
18943 pop_deferring_access_checks ();
18944
18945 pop_tinst_level ();
18946
18947 if (decl == error_mark_node)
18948 goto fail;
18949
18950 /* Now perform any access checks encountered during substitution. */
18951 push_access_scope (decl);
18952 ok = perform_access_checks (checks, complain);
18953 pop_access_scope (decl);
18954 if (!ok)
18955 goto fail;
18956
18957 /* If we're looking for an exact match, check that what we got
18958 is indeed an exact match. It might not be if some template
18959 parameters are used in non-deduced contexts. But don't check
18960 for an exact match if we have dependent template arguments;
18961 in that case we're doing partial ordering, and we already know
18962 that we have two candidates that will provide the actual type. */
18963 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18964 {
18965 tree substed = TREE_TYPE (decl);
18966 unsigned int i;
18967
18968 tree sarg
18969 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18970 if (return_type)
18971 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18972 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18973 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18974 {
18975 unify_type_mismatch (explain_p, args[i],
18976 TREE_VALUE (sarg));
18977 goto fail;
18978 }
18979 }
18980
18981 /* After doing deduction with the inherited constructor, actually return an
18982 instantiation of the inheriting constructor. */
18983 if (orig_fn != fn)
18984 decl = instantiate_template (orig_fn, targs, complain);
18985
18986 r = decl;
18987
18988 fail:
18989 --deduction_depth;
18990 if (excessive_deduction_depth)
18991 {
18992 if (deduction_depth == 0)
18993 /* Reset once we're all the way out. */
18994 excessive_deduction_depth = false;
18995 }
18996
18997 /* We can't free this if a pending_template entry or last_error_tinst_level
18998 is pointing at it. */
18999 if (last_pending_template == old_last_pend
19000 && last_error_tinst_level == old_error_tinst)
19001 ggc_free (tinst);
19002
19003 return r;
19004 }
19005
19006 /* Adjust types before performing type deduction, as described in
19007 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19008 sections are symmetric. PARM is the type of a function parameter
19009 or the return type of the conversion function. ARG is the type of
19010 the argument passed to the call, or the type of the value
19011 initialized with the result of the conversion function.
19012 ARG_EXPR is the original argument expression, which may be null. */
19013
19014 static int
19015 maybe_adjust_types_for_deduction (unification_kind_t strict,
19016 tree* parm,
19017 tree* arg,
19018 tree arg_expr)
19019 {
19020 int result = 0;
19021
19022 switch (strict)
19023 {
19024 case DEDUCE_CALL:
19025 break;
19026
19027 case DEDUCE_CONV:
19028 /* Swap PARM and ARG throughout the remainder of this
19029 function; the handling is precisely symmetric since PARM
19030 will initialize ARG rather than vice versa. */
19031 std::swap (parm, arg);
19032 break;
19033
19034 case DEDUCE_EXACT:
19035 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19036 too, but here handle it by stripping the reference from PARM
19037 rather than by adding it to ARG. */
19038 if (TREE_CODE (*parm) == REFERENCE_TYPE
19039 && TYPE_REF_IS_RVALUE (*parm)
19040 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19041 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19042 && TREE_CODE (*arg) == REFERENCE_TYPE
19043 && !TYPE_REF_IS_RVALUE (*arg))
19044 *parm = TREE_TYPE (*parm);
19045 /* Nothing else to do in this case. */
19046 return 0;
19047
19048 default:
19049 gcc_unreachable ();
19050 }
19051
19052 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19053 {
19054 /* [temp.deduct.call]
19055
19056 If P is not a reference type:
19057
19058 --If A is an array type, the pointer type produced by the
19059 array-to-pointer standard conversion (_conv.array_) is
19060 used in place of A for type deduction; otherwise,
19061
19062 --If A is a function type, the pointer type produced by
19063 the function-to-pointer standard conversion
19064 (_conv.func_) is used in place of A for type deduction;
19065 otherwise,
19066
19067 --If A is a cv-qualified type, the top level
19068 cv-qualifiers of A's type are ignored for type
19069 deduction. */
19070 if (TREE_CODE (*arg) == ARRAY_TYPE)
19071 *arg = build_pointer_type (TREE_TYPE (*arg));
19072 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19073 *arg = build_pointer_type (*arg);
19074 else
19075 *arg = TYPE_MAIN_VARIANT (*arg);
19076 }
19077
19078 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19079 reference to a cv-unqualified template parameter that does not represent a
19080 template parameter of a class template (during class template argument
19081 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19082 an lvalue, the type "lvalue reference to A" is used in place of A for type
19083 deduction. */
19084 if (TREE_CODE (*parm) == REFERENCE_TYPE
19085 && TYPE_REF_IS_RVALUE (*parm)
19086 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19087 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19088 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19089 && (arg_expr ? lvalue_p (arg_expr)
19090 /* try_one_overload doesn't provide an arg_expr, but
19091 functions are always lvalues. */
19092 : TREE_CODE (*arg) == FUNCTION_TYPE))
19093 *arg = build_reference_type (*arg);
19094
19095 /* [temp.deduct.call]
19096
19097 If P is a cv-qualified type, the top level cv-qualifiers
19098 of P's type are ignored for type deduction. If P is a
19099 reference type, the type referred to by P is used for
19100 type deduction. */
19101 *parm = TYPE_MAIN_VARIANT (*parm);
19102 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19103 {
19104 *parm = TREE_TYPE (*parm);
19105 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19106 }
19107
19108 /* DR 322. For conversion deduction, remove a reference type on parm
19109 too (which has been swapped into ARG). */
19110 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19111 *arg = TREE_TYPE (*arg);
19112
19113 return result;
19114 }
19115
19116 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19117 template which does contain any deducible template parameters; check if
19118 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19119 unify_one_argument. */
19120
19121 static int
19122 check_non_deducible_conversion (tree parm, tree arg, int strict,
19123 int flags, bool explain_p)
19124 {
19125 tree type;
19126
19127 if (!TYPE_P (arg))
19128 type = TREE_TYPE (arg);
19129 else
19130 type = arg;
19131
19132 if (same_type_p (parm, type))
19133 return unify_success (explain_p);
19134
19135 if (strict == DEDUCE_CONV)
19136 {
19137 if (can_convert_arg (type, parm, NULL_TREE, flags,
19138 explain_p ? tf_warning_or_error : tf_none))
19139 return unify_success (explain_p);
19140 }
19141 else if (strict != DEDUCE_EXACT)
19142 {
19143 if (can_convert_arg (parm, type,
19144 TYPE_P (arg) ? NULL_TREE : arg,
19145 flags, explain_p ? tf_warning_or_error : tf_none))
19146 return unify_success (explain_p);
19147 }
19148
19149 if (strict == DEDUCE_EXACT)
19150 return unify_type_mismatch (explain_p, parm, arg);
19151 else
19152 return unify_arg_conversion (explain_p, parm, type, arg);
19153 }
19154
19155 static bool uses_deducible_template_parms (tree type);
19156
19157 /* Returns true iff the expression EXPR is one from which a template
19158 argument can be deduced. In other words, if it's an undecorated
19159 use of a template non-type parameter. */
19160
19161 static bool
19162 deducible_expression (tree expr)
19163 {
19164 /* Strip implicit conversions. */
19165 while (CONVERT_EXPR_P (expr))
19166 expr = TREE_OPERAND (expr, 0);
19167 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19168 }
19169
19170 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19171 deducible way; that is, if it has a max value of <PARM> - 1. */
19172
19173 static bool
19174 deducible_array_bound (tree domain)
19175 {
19176 if (domain == NULL_TREE)
19177 return false;
19178
19179 tree max = TYPE_MAX_VALUE (domain);
19180 if (TREE_CODE (max) != MINUS_EXPR)
19181 return false;
19182
19183 return deducible_expression (TREE_OPERAND (max, 0));
19184 }
19185
19186 /* Returns true iff the template arguments ARGS use a template parameter
19187 in a deducible way. */
19188
19189 static bool
19190 deducible_template_args (tree args)
19191 {
19192 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19193 {
19194 bool deducible;
19195 tree elt = TREE_VEC_ELT (args, i);
19196 if (ARGUMENT_PACK_P (elt))
19197 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19198 else
19199 {
19200 if (PACK_EXPANSION_P (elt))
19201 elt = PACK_EXPANSION_PATTERN (elt);
19202 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19203 deducible = true;
19204 else if (TYPE_P (elt))
19205 deducible = uses_deducible_template_parms (elt);
19206 else
19207 deducible = deducible_expression (elt);
19208 }
19209 if (deducible)
19210 return true;
19211 }
19212 return false;
19213 }
19214
19215 /* Returns true iff TYPE contains any deducible references to template
19216 parameters, as per 14.8.2.5. */
19217
19218 static bool
19219 uses_deducible_template_parms (tree type)
19220 {
19221 if (PACK_EXPANSION_P (type))
19222 type = PACK_EXPANSION_PATTERN (type);
19223
19224 /* T
19225 cv-list T
19226 TT<T>
19227 TT<i>
19228 TT<> */
19229 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19230 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19231 return true;
19232
19233 /* T*
19234 T&
19235 T&& */
19236 if (POINTER_TYPE_P (type))
19237 return uses_deducible_template_parms (TREE_TYPE (type));
19238
19239 /* T[integer-constant ]
19240 type [i] */
19241 if (TREE_CODE (type) == ARRAY_TYPE)
19242 return (uses_deducible_template_parms (TREE_TYPE (type))
19243 || deducible_array_bound (TYPE_DOMAIN (type)));
19244
19245 /* T type ::*
19246 type T::*
19247 T T::*
19248 T (type ::*)()
19249 type (T::*)()
19250 type (type ::*)(T)
19251 type (T::*)(T)
19252 T (type ::*)(T)
19253 T (T::*)()
19254 T (T::*)(T) */
19255 if (TYPE_PTRMEM_P (type))
19256 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19257 || (uses_deducible_template_parms
19258 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19259
19260 /* template-name <T> (where template-name refers to a class template)
19261 template-name <i> (where template-name refers to a class template) */
19262 if (CLASS_TYPE_P (type)
19263 && CLASSTYPE_TEMPLATE_INFO (type)
19264 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19265 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19266 (CLASSTYPE_TI_ARGS (type)));
19267
19268 /* type (T)
19269 T()
19270 T(T) */
19271 if (TREE_CODE (type) == FUNCTION_TYPE
19272 || TREE_CODE (type) == METHOD_TYPE)
19273 {
19274 if (uses_deducible_template_parms (TREE_TYPE (type)))
19275 return true;
19276 tree parm = TYPE_ARG_TYPES (type);
19277 if (TREE_CODE (type) == METHOD_TYPE)
19278 parm = TREE_CHAIN (parm);
19279 for (; parm; parm = TREE_CHAIN (parm))
19280 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19281 return true;
19282 }
19283
19284 return false;
19285 }
19286
19287 /* Subroutine of type_unification_real and unify_pack_expansion to
19288 handle unification of a single P/A pair. Parameters are as
19289 for those functions. */
19290
19291 static int
19292 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19293 int subr, unification_kind_t strict,
19294 bool explain_p)
19295 {
19296 tree arg_expr = NULL_TREE;
19297 int arg_strict;
19298
19299 if (arg == error_mark_node || parm == error_mark_node)
19300 return unify_invalid (explain_p);
19301 if (arg == unknown_type_node)
19302 /* We can't deduce anything from this, but we might get all the
19303 template args from other function args. */
19304 return unify_success (explain_p);
19305
19306 /* Implicit conversions (Clause 4) will be performed on a function
19307 argument to convert it to the type of the corresponding function
19308 parameter if the parameter type contains no template-parameters that
19309 participate in template argument deduction. */
19310 if (strict != DEDUCE_EXACT
19311 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19312 /* For function parameters with no deducible template parameters,
19313 just return. We'll check non-dependent conversions later. */
19314 return unify_success (explain_p);
19315
19316 switch (strict)
19317 {
19318 case DEDUCE_CALL:
19319 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19320 | UNIFY_ALLOW_MORE_CV_QUAL
19321 | UNIFY_ALLOW_DERIVED);
19322 break;
19323
19324 case DEDUCE_CONV:
19325 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19326 break;
19327
19328 case DEDUCE_EXACT:
19329 arg_strict = UNIFY_ALLOW_NONE;
19330 break;
19331
19332 default:
19333 gcc_unreachable ();
19334 }
19335
19336 /* We only do these transformations if this is the top-level
19337 parameter_type_list in a call or declaration matching; in other
19338 situations (nested function declarators, template argument lists) we
19339 won't be comparing a type to an expression, and we don't do any type
19340 adjustments. */
19341 if (!subr)
19342 {
19343 if (!TYPE_P (arg))
19344 {
19345 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19346 if (type_unknown_p (arg))
19347 {
19348 /* [temp.deduct.type] A template-argument can be
19349 deduced from a pointer to function or pointer
19350 to member function argument if the set of
19351 overloaded functions does not contain function
19352 templates and at most one of a set of
19353 overloaded functions provides a unique
19354 match. */
19355 resolve_overloaded_unification (tparms, targs, parm,
19356 arg, strict,
19357 arg_strict, explain_p);
19358 /* If a unique match was not found, this is a
19359 non-deduced context, so we still succeed. */
19360 return unify_success (explain_p);
19361 }
19362
19363 arg_expr = arg;
19364 arg = unlowered_expr_type (arg);
19365 if (arg == error_mark_node)
19366 return unify_invalid (explain_p);
19367 }
19368
19369 arg_strict |=
19370 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19371 }
19372 else
19373 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19374 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19375 return unify_template_argument_mismatch (explain_p, parm, arg);
19376
19377 /* For deduction from an init-list we need the actual list. */
19378 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19379 arg = arg_expr;
19380 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19381 }
19382
19383 /* for_each_template_parm callback that always returns 0. */
19384
19385 static int
19386 zero_r (tree, void *)
19387 {
19388 return 0;
19389 }
19390
19391 /* for_each_template_parm any_fn callback to handle deduction of a template
19392 type argument from the type of an array bound. */
19393
19394 static int
19395 array_deduction_r (tree t, void *data)
19396 {
19397 tree_pair_p d = (tree_pair_p)data;
19398 tree &tparms = d->purpose;
19399 tree &targs = d->value;
19400
19401 if (TREE_CODE (t) == ARRAY_TYPE)
19402 if (tree dom = TYPE_DOMAIN (t))
19403 if (tree max = TYPE_MAX_VALUE (dom))
19404 {
19405 if (TREE_CODE (max) == MINUS_EXPR)
19406 max = TREE_OPERAND (max, 0);
19407 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19408 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19409 UNIFY_ALLOW_NONE, /*explain*/false);
19410 }
19411
19412 /* Keep walking. */
19413 return 0;
19414 }
19415
19416 /* Try to deduce any not-yet-deduced template type arguments from the type of
19417 an array bound. This is handled separately from unify because 14.8.2.5 says
19418 "The type of a type parameter is only deduced from an array bound if it is
19419 not otherwise deduced." */
19420
19421 static void
19422 try_array_deduction (tree tparms, tree targs, tree parm)
19423 {
19424 tree_pair_s data = { tparms, targs };
19425 hash_set<tree> visited;
19426 for_each_template_parm (parm, zero_r, &data, &visited,
19427 /*nondeduced*/false, array_deduction_r);
19428 }
19429
19430 /* Most parms like fn_type_unification.
19431
19432 If SUBR is 1, we're being called recursively (to unify the
19433 arguments of a function or method parameter of a function
19434 template).
19435
19436 CHECKS is a pointer to a vector of access checks encountered while
19437 substituting default template arguments. */
19438
19439 static int
19440 type_unification_real (tree tparms,
19441 tree full_targs,
19442 tree xparms,
19443 const tree *xargs,
19444 unsigned int xnargs,
19445 int subr,
19446 unification_kind_t strict,
19447 int flags,
19448 vec<deferred_access_check, va_gc> **checks,
19449 bool explain_p)
19450 {
19451 tree parm, arg;
19452 int i;
19453 int ntparms = TREE_VEC_LENGTH (tparms);
19454 int saw_undeduced = 0;
19455 tree parms;
19456 const tree *args;
19457 unsigned int nargs;
19458 unsigned int ia;
19459
19460 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19461 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19462 gcc_assert (ntparms > 0);
19463
19464 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19465
19466 /* Reset the number of non-defaulted template arguments contained
19467 in TARGS. */
19468 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19469
19470 again:
19471 parms = xparms;
19472 args = xargs;
19473 nargs = xnargs;
19474
19475 ia = 0;
19476 while (parms && parms != void_list_node
19477 && ia < nargs)
19478 {
19479 parm = TREE_VALUE (parms);
19480
19481 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19482 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19483 /* For a function parameter pack that occurs at the end of the
19484 parameter-declaration-list, the type A of each remaining
19485 argument of the call is compared with the type P of the
19486 declarator-id of the function parameter pack. */
19487 break;
19488
19489 parms = TREE_CHAIN (parms);
19490
19491 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19492 /* For a function parameter pack that does not occur at the
19493 end of the parameter-declaration-list, the type of the
19494 parameter pack is a non-deduced context. */
19495 continue;
19496
19497 arg = args[ia];
19498 ++ia;
19499
19500 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19501 explain_p))
19502 return 1;
19503 }
19504
19505 if (parms
19506 && parms != void_list_node
19507 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19508 {
19509 /* Unify the remaining arguments with the pack expansion type. */
19510 tree argvec;
19511 tree parmvec = make_tree_vec (1);
19512
19513 /* Allocate a TREE_VEC and copy in all of the arguments */
19514 argvec = make_tree_vec (nargs - ia);
19515 for (i = 0; ia < nargs; ++ia, ++i)
19516 TREE_VEC_ELT (argvec, i) = args[ia];
19517
19518 /* Copy the parameter into parmvec. */
19519 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19520 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19521 /*subr=*/subr, explain_p))
19522 return 1;
19523
19524 /* Advance to the end of the list of parameters. */
19525 parms = TREE_CHAIN (parms);
19526 }
19527
19528 /* Fail if we've reached the end of the parm list, and more args
19529 are present, and the parm list isn't variadic. */
19530 if (ia < nargs && parms == void_list_node)
19531 return unify_too_many_arguments (explain_p, nargs, ia);
19532 /* Fail if parms are left and they don't have default values and
19533 they aren't all deduced as empty packs (c++/57397). This is
19534 consistent with sufficient_parms_p. */
19535 if (parms && parms != void_list_node
19536 && TREE_PURPOSE (parms) == NULL_TREE)
19537 {
19538 unsigned int count = nargs;
19539 tree p = parms;
19540 bool type_pack_p;
19541 do
19542 {
19543 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19544 if (!type_pack_p)
19545 count++;
19546 p = TREE_CHAIN (p);
19547 }
19548 while (p && p != void_list_node);
19549 if (count != nargs)
19550 return unify_too_few_arguments (explain_p, ia, count,
19551 type_pack_p);
19552 }
19553
19554 if (!subr)
19555 {
19556 tsubst_flags_t complain = (explain_p
19557 ? tf_warning_or_error
19558 : tf_none);
19559 bool tried_array_deduction = (cxx_dialect < cxx17);
19560
19561 for (i = 0; i < ntparms; i++)
19562 {
19563 tree targ = TREE_VEC_ELT (targs, i);
19564 tree tparm = TREE_VEC_ELT (tparms, i);
19565
19566 /* Clear the "incomplete" flags on all argument packs now so that
19567 substituting them into later default arguments works. */
19568 if (targ && ARGUMENT_PACK_P (targ))
19569 {
19570 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19571 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19572 }
19573
19574 if (targ || tparm == error_mark_node)
19575 continue;
19576 tparm = TREE_VALUE (tparm);
19577
19578 if (TREE_CODE (tparm) == TYPE_DECL
19579 && !tried_array_deduction)
19580 {
19581 try_array_deduction (tparms, targs, xparms);
19582 tried_array_deduction = true;
19583 if (TREE_VEC_ELT (targs, i))
19584 continue;
19585 }
19586
19587 /* If this is an undeduced nontype parameter that depends on
19588 a type parameter, try another pass; its type may have been
19589 deduced from a later argument than the one from which
19590 this parameter can be deduced. */
19591 if (TREE_CODE (tparm) == PARM_DECL
19592 && uses_template_parms (TREE_TYPE (tparm))
19593 && saw_undeduced < 2)
19594 {
19595 saw_undeduced = 1;
19596 continue;
19597 }
19598
19599 /* Core issue #226 (C++0x) [temp.deduct]:
19600
19601 If a template argument has not been deduced, its
19602 default template argument, if any, is used.
19603
19604 When we are in C++98 mode, TREE_PURPOSE will either
19605 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19606 to explicitly check cxx_dialect here. */
19607 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19608 /* OK, there is a default argument. Wait until after the
19609 conversion check to do substitution. */
19610 continue;
19611
19612 /* If the type parameter is a parameter pack, then it will
19613 be deduced to an empty parameter pack. */
19614 if (template_parameter_pack_p (tparm))
19615 {
19616 tree arg;
19617
19618 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19619 {
19620 arg = make_node (NONTYPE_ARGUMENT_PACK);
19621 TREE_CONSTANT (arg) = 1;
19622 }
19623 else
19624 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19625
19626 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19627
19628 TREE_VEC_ELT (targs, i) = arg;
19629 continue;
19630 }
19631
19632 return unify_parameter_deduction_failure (explain_p, tparm);
19633 }
19634
19635 /* DR 1391: All parameters have args, now check non-dependent parms for
19636 convertibility. */
19637 if (saw_undeduced < 2)
19638 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19639 parms && parms != void_list_node && ia < nargs; )
19640 {
19641 parm = TREE_VALUE (parms);
19642
19643 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19644 && (!TREE_CHAIN (parms)
19645 || TREE_CHAIN (parms) == void_list_node))
19646 /* For a function parameter pack that occurs at the end of the
19647 parameter-declaration-list, the type A of each remaining
19648 argument of the call is compared with the type P of the
19649 declarator-id of the function parameter pack. */
19650 break;
19651
19652 parms = TREE_CHAIN (parms);
19653
19654 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19655 /* For a function parameter pack that does not occur at the
19656 end of the parameter-declaration-list, the type of the
19657 parameter pack is a non-deduced context. */
19658 continue;
19659
19660 arg = args[ia];
19661 ++ia;
19662
19663 if (uses_template_parms (parm))
19664 continue;
19665 if (check_non_deducible_conversion (parm, arg, strict, flags,
19666 explain_p))
19667 return 1;
19668 }
19669
19670 /* Now substitute into the default template arguments. */
19671 for (i = 0; i < ntparms; i++)
19672 {
19673 tree targ = TREE_VEC_ELT (targs, i);
19674 tree tparm = TREE_VEC_ELT (tparms, i);
19675
19676 if (targ || tparm == error_mark_node)
19677 continue;
19678 tree parm = TREE_VALUE (tparm);
19679
19680 if (TREE_CODE (parm) == PARM_DECL
19681 && uses_template_parms (TREE_TYPE (parm))
19682 && saw_undeduced < 2)
19683 continue;
19684
19685 tree arg = TREE_PURPOSE (tparm);
19686 reopen_deferring_access_checks (*checks);
19687 location_t save_loc = input_location;
19688 if (DECL_P (parm))
19689 input_location = DECL_SOURCE_LOCATION (parm);
19690 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19691 if (!uses_template_parms (arg))
19692 arg = convert_template_argument (parm, arg, full_targs, complain,
19693 i, NULL_TREE);
19694 else if (saw_undeduced < 2)
19695 arg = NULL_TREE;
19696 else
19697 arg = error_mark_node;
19698 input_location = save_loc;
19699 *checks = get_deferred_access_checks ();
19700 pop_deferring_access_checks ();
19701 if (arg == error_mark_node)
19702 return 1;
19703 else if (arg)
19704 {
19705 TREE_VEC_ELT (targs, i) = arg;
19706 /* The position of the first default template argument,
19707 is also the number of non-defaulted arguments in TARGS.
19708 Record that. */
19709 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19710 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19711 }
19712 }
19713
19714 if (saw_undeduced++ == 1)
19715 goto again;
19716 }
19717
19718 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19719 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19720
19721 return unify_success (explain_p);
19722 }
19723
19724 /* Subroutine of type_unification_real. Args are like the variables
19725 at the call site. ARG is an overloaded function (or template-id);
19726 we try deducing template args from each of the overloads, and if
19727 only one succeeds, we go with that. Modifies TARGS and returns
19728 true on success. */
19729
19730 static bool
19731 resolve_overloaded_unification (tree tparms,
19732 tree targs,
19733 tree parm,
19734 tree arg,
19735 unification_kind_t strict,
19736 int sub_strict,
19737 bool explain_p)
19738 {
19739 tree tempargs = copy_node (targs);
19740 int good = 0;
19741 tree goodfn = NULL_TREE;
19742 bool addr_p;
19743
19744 if (TREE_CODE (arg) == ADDR_EXPR)
19745 {
19746 arg = TREE_OPERAND (arg, 0);
19747 addr_p = true;
19748 }
19749 else
19750 addr_p = false;
19751
19752 if (TREE_CODE (arg) == COMPONENT_REF)
19753 /* Handle `&x' where `x' is some static or non-static member
19754 function name. */
19755 arg = TREE_OPERAND (arg, 1);
19756
19757 if (TREE_CODE (arg) == OFFSET_REF)
19758 arg = TREE_OPERAND (arg, 1);
19759
19760 /* Strip baselink information. */
19761 if (BASELINK_P (arg))
19762 arg = BASELINK_FUNCTIONS (arg);
19763
19764 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19765 {
19766 /* If we got some explicit template args, we need to plug them into
19767 the affected templates before we try to unify, in case the
19768 explicit args will completely resolve the templates in question. */
19769
19770 int ok = 0;
19771 tree expl_subargs = TREE_OPERAND (arg, 1);
19772 arg = TREE_OPERAND (arg, 0);
19773
19774 for (lkp_iterator iter (arg); iter; ++iter)
19775 {
19776 tree fn = *iter;
19777 tree subargs, elem;
19778
19779 if (TREE_CODE (fn) != TEMPLATE_DECL)
19780 continue;
19781
19782 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19783 expl_subargs, NULL_TREE, tf_none,
19784 /*require_all_args=*/true,
19785 /*use_default_args=*/true);
19786 if (subargs != error_mark_node
19787 && !any_dependent_template_arguments_p (subargs))
19788 {
19789 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19790 if (try_one_overload (tparms, targs, tempargs, parm,
19791 elem, strict, sub_strict, addr_p, explain_p)
19792 && (!goodfn || !same_type_p (goodfn, elem)))
19793 {
19794 goodfn = elem;
19795 ++good;
19796 }
19797 }
19798 else if (subargs)
19799 ++ok;
19800 }
19801 /* If no templates (or more than one) are fully resolved by the
19802 explicit arguments, this template-id is a non-deduced context; it
19803 could still be OK if we deduce all template arguments for the
19804 enclosing call through other arguments. */
19805 if (good != 1)
19806 good = ok;
19807 }
19808 else if (TREE_CODE (arg) != OVERLOAD
19809 && TREE_CODE (arg) != FUNCTION_DECL)
19810 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19811 -- but the deduction does not succeed because the expression is
19812 not just the function on its own. */
19813 return false;
19814 else
19815 for (lkp_iterator iter (arg); iter; ++iter)
19816 {
19817 tree fn = *iter;
19818 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19819 strict, sub_strict, addr_p, explain_p)
19820 && (!goodfn || !decls_match (goodfn, fn)))
19821 {
19822 goodfn = fn;
19823 ++good;
19824 }
19825 }
19826
19827 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19828 to function or pointer to member function argument if the set of
19829 overloaded functions does not contain function templates and at most
19830 one of a set of overloaded functions provides a unique match.
19831
19832 So if we found multiple possibilities, we return success but don't
19833 deduce anything. */
19834
19835 if (good == 1)
19836 {
19837 int i = TREE_VEC_LENGTH (targs);
19838 for (; i--; )
19839 if (TREE_VEC_ELT (tempargs, i))
19840 {
19841 tree old = TREE_VEC_ELT (targs, i);
19842 tree new_ = TREE_VEC_ELT (tempargs, i);
19843 if (new_ && old && ARGUMENT_PACK_P (old)
19844 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19845 /* Don't forget explicit template arguments in a pack. */
19846 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19847 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19848 TREE_VEC_ELT (targs, i) = new_;
19849 }
19850 }
19851 if (good)
19852 return true;
19853
19854 return false;
19855 }
19856
19857 /* Core DR 115: In contexts where deduction is done and fails, or in
19858 contexts where deduction is not done, if a template argument list is
19859 specified and it, along with any default template arguments, identifies
19860 a single function template specialization, then the template-id is an
19861 lvalue for the function template specialization. */
19862
19863 tree
19864 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19865 {
19866 tree expr, offset, baselink;
19867 bool addr;
19868
19869 if (!type_unknown_p (orig_expr))
19870 return orig_expr;
19871
19872 expr = orig_expr;
19873 addr = false;
19874 offset = NULL_TREE;
19875 baselink = NULL_TREE;
19876
19877 if (TREE_CODE (expr) == ADDR_EXPR)
19878 {
19879 expr = TREE_OPERAND (expr, 0);
19880 addr = true;
19881 }
19882 if (TREE_CODE (expr) == OFFSET_REF)
19883 {
19884 offset = expr;
19885 expr = TREE_OPERAND (expr, 1);
19886 }
19887 if (BASELINK_P (expr))
19888 {
19889 baselink = expr;
19890 expr = BASELINK_FUNCTIONS (expr);
19891 }
19892
19893 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19894 {
19895 int good = 0;
19896 tree goodfn = NULL_TREE;
19897
19898 /* If we got some explicit template args, we need to plug them into
19899 the affected templates before we try to unify, in case the
19900 explicit args will completely resolve the templates in question. */
19901
19902 tree expl_subargs = TREE_OPERAND (expr, 1);
19903 tree arg = TREE_OPERAND (expr, 0);
19904 tree badfn = NULL_TREE;
19905 tree badargs = NULL_TREE;
19906
19907 for (lkp_iterator iter (arg); iter; ++iter)
19908 {
19909 tree fn = *iter;
19910 tree subargs, elem;
19911
19912 if (TREE_CODE (fn) != TEMPLATE_DECL)
19913 continue;
19914
19915 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19916 expl_subargs, NULL_TREE, tf_none,
19917 /*require_all_args=*/true,
19918 /*use_default_args=*/true);
19919 if (subargs != error_mark_node
19920 && !any_dependent_template_arguments_p (subargs))
19921 {
19922 elem = instantiate_template (fn, subargs, tf_none);
19923 if (elem == error_mark_node)
19924 {
19925 badfn = fn;
19926 badargs = subargs;
19927 }
19928 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19929 {
19930 goodfn = elem;
19931 ++good;
19932 }
19933 }
19934 }
19935 if (good == 1)
19936 {
19937 mark_used (goodfn);
19938 expr = goodfn;
19939 if (baselink)
19940 expr = build_baselink (BASELINK_BINFO (baselink),
19941 BASELINK_ACCESS_BINFO (baselink),
19942 expr, BASELINK_OPTYPE (baselink));
19943 if (offset)
19944 {
19945 tree base
19946 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19947 expr = build_offset_ref (base, expr, addr, complain);
19948 }
19949 if (addr)
19950 expr = cp_build_addr_expr (expr, complain);
19951 return expr;
19952 }
19953 else if (good == 0 && badargs && (complain & tf_error))
19954 /* There were no good options and at least one bad one, so let the
19955 user know what the problem is. */
19956 instantiate_template (badfn, badargs, complain);
19957 }
19958 return orig_expr;
19959 }
19960
19961 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19962 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19963 different overloads deduce different arguments for a given parm.
19964 ADDR_P is true if the expression for which deduction is being
19965 performed was of the form "& fn" rather than simply "fn".
19966
19967 Returns 1 on success. */
19968
19969 static int
19970 try_one_overload (tree tparms,
19971 tree orig_targs,
19972 tree targs,
19973 tree parm,
19974 tree arg,
19975 unification_kind_t strict,
19976 int sub_strict,
19977 bool addr_p,
19978 bool explain_p)
19979 {
19980 int nargs;
19981 tree tempargs;
19982 int i;
19983
19984 if (arg == error_mark_node)
19985 return 0;
19986
19987 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19988 to function or pointer to member function argument if the set of
19989 overloaded functions does not contain function templates and at most
19990 one of a set of overloaded functions provides a unique match.
19991
19992 So if this is a template, just return success. */
19993
19994 if (uses_template_parms (arg))
19995 return 1;
19996
19997 if (TREE_CODE (arg) == METHOD_TYPE)
19998 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19999 else if (addr_p)
20000 arg = build_pointer_type (arg);
20001
20002 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20003
20004 /* We don't copy orig_targs for this because if we have already deduced
20005 some template args from previous args, unify would complain when we
20006 try to deduce a template parameter for the same argument, even though
20007 there isn't really a conflict. */
20008 nargs = TREE_VEC_LENGTH (targs);
20009 tempargs = make_tree_vec (nargs);
20010
20011 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20012 return 0;
20013
20014 /* First make sure we didn't deduce anything that conflicts with
20015 explicitly specified args. */
20016 for (i = nargs; i--; )
20017 {
20018 tree elt = TREE_VEC_ELT (tempargs, i);
20019 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20020
20021 if (!elt)
20022 /*NOP*/;
20023 else if (uses_template_parms (elt))
20024 /* Since we're unifying against ourselves, we will fill in
20025 template args used in the function parm list with our own
20026 template parms. Discard them. */
20027 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20028 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20029 {
20030 /* Check that the argument at each index of the deduced argument pack
20031 is equivalent to the corresponding explicitly specified argument.
20032 We may have deduced more arguments than were explicitly specified,
20033 and that's OK. */
20034
20035 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20036 that's wrong if we deduce the same argument pack from multiple
20037 function arguments: it's only incomplete the first time. */
20038
20039 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20040 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20041
20042 if (TREE_VEC_LENGTH (deduced_pack)
20043 < TREE_VEC_LENGTH (explicit_pack))
20044 return 0;
20045
20046 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20047 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20048 TREE_VEC_ELT (deduced_pack, j)))
20049 return 0;
20050 }
20051 else if (oldelt && !template_args_equal (oldelt, elt))
20052 return 0;
20053 }
20054
20055 for (i = nargs; i--; )
20056 {
20057 tree elt = TREE_VEC_ELT (tempargs, i);
20058
20059 if (elt)
20060 TREE_VEC_ELT (targs, i) = elt;
20061 }
20062
20063 return 1;
20064 }
20065
20066 /* PARM is a template class (perhaps with unbound template
20067 parameters). ARG is a fully instantiated type. If ARG can be
20068 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20069 TARGS are as for unify. */
20070
20071 static tree
20072 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20073 bool explain_p)
20074 {
20075 tree copy_of_targs;
20076
20077 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20078 return NULL_TREE;
20079 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20080 /* Matches anything. */;
20081 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20082 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20083 return NULL_TREE;
20084
20085 /* We need to make a new template argument vector for the call to
20086 unify. If we used TARGS, we'd clutter it up with the result of
20087 the attempted unification, even if this class didn't work out.
20088 We also don't want to commit ourselves to all the unifications
20089 we've already done, since unification is supposed to be done on
20090 an argument-by-argument basis. In other words, consider the
20091 following pathological case:
20092
20093 template <int I, int J, int K>
20094 struct S {};
20095
20096 template <int I, int J>
20097 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20098
20099 template <int I, int J, int K>
20100 void f(S<I, J, K>, S<I, I, I>);
20101
20102 void g() {
20103 S<0, 0, 0> s0;
20104 S<0, 1, 2> s2;
20105
20106 f(s0, s2);
20107 }
20108
20109 Now, by the time we consider the unification involving `s2', we
20110 already know that we must have `f<0, 0, 0>'. But, even though
20111 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20112 because there are two ways to unify base classes of S<0, 1, 2>
20113 with S<I, I, I>. If we kept the already deduced knowledge, we
20114 would reject the possibility I=1. */
20115 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20116
20117 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20118 {
20119 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20120 return NULL_TREE;
20121 return arg;
20122 }
20123
20124 /* If unification failed, we're done. */
20125 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20126 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20127 return NULL_TREE;
20128
20129 return arg;
20130 }
20131
20132 /* Given a template type PARM and a class type ARG, find the unique
20133 base type in ARG that is an instance of PARM. We do not examine
20134 ARG itself; only its base-classes. If there is not exactly one
20135 appropriate base class, return NULL_TREE. PARM may be the type of
20136 a partial specialization, as well as a plain template type. Used
20137 by unify. */
20138
20139 static enum template_base_result
20140 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20141 bool explain_p, tree *result)
20142 {
20143 tree rval = NULL_TREE;
20144 tree binfo;
20145
20146 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20147
20148 binfo = TYPE_BINFO (complete_type (arg));
20149 if (!binfo)
20150 {
20151 /* The type could not be completed. */
20152 *result = NULL_TREE;
20153 return tbr_incomplete_type;
20154 }
20155
20156 /* Walk in inheritance graph order. The search order is not
20157 important, and this avoids multiple walks of virtual bases. */
20158 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20159 {
20160 tree r = try_class_unification (tparms, targs, parm,
20161 BINFO_TYPE (binfo), explain_p);
20162
20163 if (r)
20164 {
20165 /* If there is more than one satisfactory baseclass, then:
20166
20167 [temp.deduct.call]
20168
20169 If they yield more than one possible deduced A, the type
20170 deduction fails.
20171
20172 applies. */
20173 if (rval && !same_type_p (r, rval))
20174 {
20175 *result = NULL_TREE;
20176 return tbr_ambiguous_baseclass;
20177 }
20178
20179 rval = r;
20180 }
20181 }
20182
20183 *result = rval;
20184 return tbr_success;
20185 }
20186
20187 /* Returns the level of DECL, which declares a template parameter. */
20188
20189 static int
20190 template_decl_level (tree decl)
20191 {
20192 switch (TREE_CODE (decl))
20193 {
20194 case TYPE_DECL:
20195 case TEMPLATE_DECL:
20196 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20197
20198 case PARM_DECL:
20199 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20200
20201 default:
20202 gcc_unreachable ();
20203 }
20204 return 0;
20205 }
20206
20207 /* Decide whether ARG can be unified with PARM, considering only the
20208 cv-qualifiers of each type, given STRICT as documented for unify.
20209 Returns nonzero iff the unification is OK on that basis. */
20210
20211 static int
20212 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20213 {
20214 int arg_quals = cp_type_quals (arg);
20215 int parm_quals = cp_type_quals (parm);
20216
20217 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20218 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20219 {
20220 /* Although a CVR qualifier is ignored when being applied to a
20221 substituted template parameter ([8.3.2]/1 for example), that
20222 does not allow us to unify "const T" with "int&" because both
20223 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20224 It is ok when we're allowing additional CV qualifiers
20225 at the outer level [14.8.2.1]/3,1st bullet. */
20226 if ((TREE_CODE (arg) == REFERENCE_TYPE
20227 || TREE_CODE (arg) == FUNCTION_TYPE
20228 || TREE_CODE (arg) == METHOD_TYPE)
20229 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20230 return 0;
20231
20232 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20233 && (parm_quals & TYPE_QUAL_RESTRICT))
20234 return 0;
20235 }
20236
20237 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20238 && (arg_quals & parm_quals) != parm_quals)
20239 return 0;
20240
20241 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20242 && (parm_quals & arg_quals) != arg_quals)
20243 return 0;
20244
20245 return 1;
20246 }
20247
20248 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20249 void
20250 template_parm_level_and_index (tree parm, int* level, int* index)
20251 {
20252 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20253 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20254 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20255 {
20256 *index = TEMPLATE_TYPE_IDX (parm);
20257 *level = TEMPLATE_TYPE_LEVEL (parm);
20258 }
20259 else
20260 {
20261 *index = TEMPLATE_PARM_IDX (parm);
20262 *level = TEMPLATE_PARM_LEVEL (parm);
20263 }
20264 }
20265
20266 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20267 do { \
20268 if (unify (TP, TA, P, A, S, EP)) \
20269 return 1; \
20270 } while (0)
20271
20272 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20273 expansion at the end of PACKED_PARMS. Returns 0 if the type
20274 deduction succeeds, 1 otherwise. STRICT is the same as in
20275 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20276 function call argument list. We'll need to adjust the arguments to make them
20277 types. SUBR tells us if this is from a recursive call to
20278 type_unification_real, or for comparing two template argument
20279 lists. */
20280
20281 static int
20282 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20283 tree packed_args, unification_kind_t strict,
20284 bool subr, bool explain_p)
20285 {
20286 tree parm
20287 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20288 tree pattern = PACK_EXPANSION_PATTERN (parm);
20289 tree pack, packs = NULL_TREE;
20290 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20291
20292 /* Add in any args remembered from an earlier partial instantiation. */
20293 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20294
20295 packed_args = expand_template_argument_pack (packed_args);
20296
20297 int len = TREE_VEC_LENGTH (packed_args);
20298
20299 /* Determine the parameter packs we will be deducing from the
20300 pattern, and record their current deductions. */
20301 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20302 pack; pack = TREE_CHAIN (pack))
20303 {
20304 tree parm_pack = TREE_VALUE (pack);
20305 int idx, level;
20306
20307 /* Determine the index and level of this parameter pack. */
20308 template_parm_level_and_index (parm_pack, &level, &idx);
20309
20310 /* Keep track of the parameter packs and their corresponding
20311 argument packs. */
20312 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20313 TREE_TYPE (packs) = make_tree_vec (len - start);
20314 }
20315
20316 /* Loop through all of the arguments that have not yet been
20317 unified and unify each with the pattern. */
20318 for (i = start; i < len; i++)
20319 {
20320 tree parm;
20321 bool any_explicit = false;
20322 tree arg = TREE_VEC_ELT (packed_args, i);
20323
20324 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20325 or the element of its argument pack at the current index if
20326 this argument was explicitly specified. */
20327 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20328 {
20329 int idx, level;
20330 tree arg, pargs;
20331 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20332
20333 arg = NULL_TREE;
20334 if (TREE_VALUE (pack)
20335 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20336 && (i - start < TREE_VEC_LENGTH (pargs)))
20337 {
20338 any_explicit = true;
20339 arg = TREE_VEC_ELT (pargs, i - start);
20340 }
20341 TMPL_ARG (targs, level, idx) = arg;
20342 }
20343
20344 /* If we had explicit template arguments, substitute them into the
20345 pattern before deduction. */
20346 if (any_explicit)
20347 {
20348 /* Some arguments might still be unspecified or dependent. */
20349 bool dependent;
20350 ++processing_template_decl;
20351 dependent = any_dependent_template_arguments_p (targs);
20352 if (!dependent)
20353 --processing_template_decl;
20354 parm = tsubst (pattern, targs,
20355 explain_p ? tf_warning_or_error : tf_none,
20356 NULL_TREE);
20357 if (dependent)
20358 --processing_template_decl;
20359 if (parm == error_mark_node)
20360 return 1;
20361 }
20362 else
20363 parm = pattern;
20364
20365 /* Unify the pattern with the current argument. */
20366 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20367 explain_p))
20368 return 1;
20369
20370 /* For each parameter pack, collect the deduced value. */
20371 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20372 {
20373 int idx, level;
20374 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20375
20376 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20377 TMPL_ARG (targs, level, idx);
20378 }
20379 }
20380
20381 /* Verify that the results of unification with the parameter packs
20382 produce results consistent with what we've seen before, and make
20383 the deduced argument packs available. */
20384 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20385 {
20386 tree old_pack = TREE_VALUE (pack);
20387 tree new_args = TREE_TYPE (pack);
20388 int i, len = TREE_VEC_LENGTH (new_args);
20389 int idx, level;
20390 bool nondeduced_p = false;
20391
20392 /* By default keep the original deduced argument pack.
20393 If necessary, more specific code is going to update the
20394 resulting deduced argument later down in this function. */
20395 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20396 TMPL_ARG (targs, level, idx) = old_pack;
20397
20398 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20399 actually deduce anything. */
20400 for (i = 0; i < len && !nondeduced_p; ++i)
20401 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20402 nondeduced_p = true;
20403 if (nondeduced_p)
20404 continue;
20405
20406 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20407 {
20408 /* If we had fewer function args than explicit template args,
20409 just use the explicits. */
20410 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20411 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20412 if (len < explicit_len)
20413 new_args = explicit_args;
20414 }
20415
20416 if (!old_pack)
20417 {
20418 tree result;
20419 /* Build the deduced *_ARGUMENT_PACK. */
20420 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20421 {
20422 result = make_node (NONTYPE_ARGUMENT_PACK);
20423 TREE_CONSTANT (result) = 1;
20424 }
20425 else
20426 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20427
20428 SET_ARGUMENT_PACK_ARGS (result, new_args);
20429
20430 /* Note the deduced argument packs for this parameter
20431 pack. */
20432 TMPL_ARG (targs, level, idx) = result;
20433 }
20434 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20435 && (ARGUMENT_PACK_ARGS (old_pack)
20436 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20437 {
20438 /* We only had the explicitly-provided arguments before, but
20439 now we have a complete set of arguments. */
20440 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20441
20442 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20443 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20444 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20445 }
20446 else
20447 {
20448 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20449 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20450
20451 if (!comp_template_args (old_args, new_args,
20452 &bad_old_arg, &bad_new_arg))
20453 /* Inconsistent unification of this parameter pack. */
20454 return unify_parameter_pack_inconsistent (explain_p,
20455 bad_old_arg,
20456 bad_new_arg);
20457 }
20458 }
20459
20460 return unify_success (explain_p);
20461 }
20462
20463 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20464 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20465 parameters and return value are as for unify. */
20466
20467 static int
20468 unify_array_domain (tree tparms, tree targs,
20469 tree parm_dom, tree arg_dom,
20470 bool explain_p)
20471 {
20472 tree parm_max;
20473 tree arg_max;
20474 bool parm_cst;
20475 bool arg_cst;
20476
20477 /* Our representation of array types uses "N - 1" as the
20478 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20479 not an integer constant. We cannot unify arbitrarily
20480 complex expressions, so we eliminate the MINUS_EXPRs
20481 here. */
20482 parm_max = TYPE_MAX_VALUE (parm_dom);
20483 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20484 if (!parm_cst)
20485 {
20486 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20487 parm_max = TREE_OPERAND (parm_max, 0);
20488 }
20489 arg_max = TYPE_MAX_VALUE (arg_dom);
20490 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20491 if (!arg_cst)
20492 {
20493 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20494 trying to unify the type of a variable with the type
20495 of a template parameter. For example:
20496
20497 template <unsigned int N>
20498 void f (char (&) [N]);
20499 int g();
20500 void h(int i) {
20501 char a[g(i)];
20502 f(a);
20503 }
20504
20505 Here, the type of the ARG will be "int [g(i)]", and
20506 may be a SAVE_EXPR, etc. */
20507 if (TREE_CODE (arg_max) != MINUS_EXPR)
20508 return unify_vla_arg (explain_p, arg_dom);
20509 arg_max = TREE_OPERAND (arg_max, 0);
20510 }
20511
20512 /* If only one of the bounds used a MINUS_EXPR, compensate
20513 by adding one to the other bound. */
20514 if (parm_cst && !arg_cst)
20515 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20516 integer_type_node,
20517 parm_max,
20518 integer_one_node);
20519 else if (arg_cst && !parm_cst)
20520 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20521 integer_type_node,
20522 arg_max,
20523 integer_one_node);
20524
20525 return unify (tparms, targs, parm_max, arg_max,
20526 UNIFY_ALLOW_INTEGER, explain_p);
20527 }
20528
20529 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20530
20531 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20532
20533 static pa_kind_t
20534 pa_kind (tree t)
20535 {
20536 if (PACK_EXPANSION_P (t))
20537 t = PACK_EXPANSION_PATTERN (t);
20538 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20539 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20540 || DECL_TYPE_TEMPLATE_P (t))
20541 return pa_tmpl;
20542 else if (TYPE_P (t))
20543 return pa_type;
20544 else
20545 return pa_expr;
20546 }
20547
20548 /* Deduce the value of template parameters. TPARMS is the (innermost)
20549 set of template parameters to a template. TARGS is the bindings
20550 for those template parameters, as determined thus far; TARGS may
20551 include template arguments for outer levels of template parameters
20552 as well. PARM is a parameter to a template function, or a
20553 subcomponent of that parameter; ARG is the corresponding argument.
20554 This function attempts to match PARM with ARG in a manner
20555 consistent with the existing assignments in TARGS. If more values
20556 are deduced, then TARGS is updated.
20557
20558 Returns 0 if the type deduction succeeds, 1 otherwise. The
20559 parameter STRICT is a bitwise or of the following flags:
20560
20561 UNIFY_ALLOW_NONE:
20562 Require an exact match between PARM and ARG.
20563 UNIFY_ALLOW_MORE_CV_QUAL:
20564 Allow the deduced ARG to be more cv-qualified (by qualification
20565 conversion) than ARG.
20566 UNIFY_ALLOW_LESS_CV_QUAL:
20567 Allow the deduced ARG to be less cv-qualified than ARG.
20568 UNIFY_ALLOW_DERIVED:
20569 Allow the deduced ARG to be a template base class of ARG,
20570 or a pointer to a template base class of the type pointed to by
20571 ARG.
20572 UNIFY_ALLOW_INTEGER:
20573 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20574 case for more information.
20575 UNIFY_ALLOW_OUTER_LEVEL:
20576 This is the outermost level of a deduction. Used to determine validity
20577 of qualification conversions. A valid qualification conversion must
20578 have const qualified pointers leading up to the inner type which
20579 requires additional CV quals, except at the outer level, where const
20580 is not required [conv.qual]. It would be normal to set this flag in
20581 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20582 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20583 This is the outermost level of a deduction, and PARM can be more CV
20584 qualified at this point.
20585 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20586 This is the outermost level of a deduction, and PARM can be less CV
20587 qualified at this point. */
20588
20589 static int
20590 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20591 bool explain_p)
20592 {
20593 int idx;
20594 tree targ;
20595 tree tparm;
20596 int strict_in = strict;
20597 tsubst_flags_t complain = (explain_p
20598 ? tf_warning_or_error
20599 : tf_none);
20600
20601 /* I don't think this will do the right thing with respect to types.
20602 But the only case I've seen it in so far has been array bounds, where
20603 signedness is the only information lost, and I think that will be
20604 okay. */
20605 while (CONVERT_EXPR_P (parm))
20606 parm = TREE_OPERAND (parm, 0);
20607
20608 if (arg == error_mark_node)
20609 return unify_invalid (explain_p);
20610 if (arg == unknown_type_node
20611 || arg == init_list_type_node)
20612 /* We can't deduce anything from this, but we might get all the
20613 template args from other function args. */
20614 return unify_success (explain_p);
20615
20616 if (parm == any_targ_node || arg == any_targ_node)
20617 return unify_success (explain_p);
20618
20619 /* If PARM uses template parameters, then we can't bail out here,
20620 even if ARG == PARM, since we won't record unifications for the
20621 template parameters. We might need them if we're trying to
20622 figure out which of two things is more specialized. */
20623 if (arg == parm && !uses_template_parms (parm))
20624 return unify_success (explain_p);
20625
20626 /* Handle init lists early, so the rest of the function can assume
20627 we're dealing with a type. */
20628 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20629 {
20630 tree elt, elttype;
20631 unsigned i;
20632 tree orig_parm = parm;
20633
20634 /* Replace T with std::initializer_list<T> for deduction. */
20635 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20636 && flag_deduce_init_list)
20637 parm = listify (parm);
20638
20639 if (!is_std_init_list (parm)
20640 && TREE_CODE (parm) != ARRAY_TYPE)
20641 /* We can only deduce from an initializer list argument if the
20642 parameter is std::initializer_list or an array; otherwise this
20643 is a non-deduced context. */
20644 return unify_success (explain_p);
20645
20646 if (TREE_CODE (parm) == ARRAY_TYPE)
20647 elttype = TREE_TYPE (parm);
20648 else
20649 {
20650 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20651 /* Deduction is defined in terms of a single type, so just punt
20652 on the (bizarre) std::initializer_list<T...>. */
20653 if (PACK_EXPANSION_P (elttype))
20654 return unify_success (explain_p);
20655 }
20656
20657 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20658 {
20659 int elt_strict = strict;
20660
20661 if (elt == error_mark_node)
20662 return unify_invalid (explain_p);
20663
20664 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20665 {
20666 tree type = TREE_TYPE (elt);
20667 if (type == error_mark_node)
20668 return unify_invalid (explain_p);
20669 /* It should only be possible to get here for a call. */
20670 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20671 elt_strict |= maybe_adjust_types_for_deduction
20672 (DEDUCE_CALL, &elttype, &type, elt);
20673 elt = type;
20674 }
20675
20676 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20677 explain_p);
20678 }
20679
20680 if (TREE_CODE (parm) == ARRAY_TYPE
20681 && deducible_array_bound (TYPE_DOMAIN (parm)))
20682 {
20683 /* Also deduce from the length of the initializer list. */
20684 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20685 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20686 if (idx == error_mark_node)
20687 return unify_invalid (explain_p);
20688 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20689 idx, explain_p);
20690 }
20691
20692 /* If the std::initializer_list<T> deduction worked, replace the
20693 deduced A with std::initializer_list<A>. */
20694 if (orig_parm != parm)
20695 {
20696 idx = TEMPLATE_TYPE_IDX (orig_parm);
20697 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20698 targ = listify (targ);
20699 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20700 }
20701 return unify_success (explain_p);
20702 }
20703
20704 /* If parm and arg aren't the same kind of thing (template, type, or
20705 expression), fail early. */
20706 if (pa_kind (parm) != pa_kind (arg))
20707 return unify_invalid (explain_p);
20708
20709 /* Immediately reject some pairs that won't unify because of
20710 cv-qualification mismatches. */
20711 if (TREE_CODE (arg) == TREE_CODE (parm)
20712 && TYPE_P (arg)
20713 /* It is the elements of the array which hold the cv quals of an array
20714 type, and the elements might be template type parms. We'll check
20715 when we recurse. */
20716 && TREE_CODE (arg) != ARRAY_TYPE
20717 /* We check the cv-qualifiers when unifying with template type
20718 parameters below. We want to allow ARG `const T' to unify with
20719 PARM `T' for example, when computing which of two templates
20720 is more specialized, for example. */
20721 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20722 && !check_cv_quals_for_unify (strict_in, arg, parm))
20723 return unify_cv_qual_mismatch (explain_p, parm, arg);
20724
20725 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20726 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20727 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20728 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20729 strict &= ~UNIFY_ALLOW_DERIVED;
20730 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20731 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20732
20733 switch (TREE_CODE (parm))
20734 {
20735 case TYPENAME_TYPE:
20736 case SCOPE_REF:
20737 case UNBOUND_CLASS_TEMPLATE:
20738 /* In a type which contains a nested-name-specifier, template
20739 argument values cannot be deduced for template parameters used
20740 within the nested-name-specifier. */
20741 return unify_success (explain_p);
20742
20743 case TEMPLATE_TYPE_PARM:
20744 case TEMPLATE_TEMPLATE_PARM:
20745 case BOUND_TEMPLATE_TEMPLATE_PARM:
20746 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20747 if (error_operand_p (tparm))
20748 return unify_invalid (explain_p);
20749
20750 if (TEMPLATE_TYPE_LEVEL (parm)
20751 != template_decl_level (tparm))
20752 /* The PARM is not one we're trying to unify. Just check
20753 to see if it matches ARG. */
20754 {
20755 if (TREE_CODE (arg) == TREE_CODE (parm)
20756 && (is_auto (parm) ? is_auto (arg)
20757 : same_type_p (parm, arg)))
20758 return unify_success (explain_p);
20759 else
20760 return unify_type_mismatch (explain_p, parm, arg);
20761 }
20762 idx = TEMPLATE_TYPE_IDX (parm);
20763 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20764 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20765 if (error_operand_p (tparm))
20766 return unify_invalid (explain_p);
20767
20768 /* Check for mixed types and values. */
20769 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20770 && TREE_CODE (tparm) != TYPE_DECL)
20771 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20772 && TREE_CODE (tparm) != TEMPLATE_DECL))
20773 gcc_unreachable ();
20774
20775 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20776 {
20777 if ((strict_in & UNIFY_ALLOW_DERIVED)
20778 && CLASS_TYPE_P (arg))
20779 {
20780 /* First try to match ARG directly. */
20781 tree t = try_class_unification (tparms, targs, parm, arg,
20782 explain_p);
20783 if (!t)
20784 {
20785 /* Otherwise, look for a suitable base of ARG, as below. */
20786 enum template_base_result r;
20787 r = get_template_base (tparms, targs, parm, arg,
20788 explain_p, &t);
20789 if (!t)
20790 return unify_no_common_base (explain_p, r, parm, arg);
20791 arg = t;
20792 }
20793 }
20794 /* ARG must be constructed from a template class or a template
20795 template parameter. */
20796 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20797 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20798 return unify_template_deduction_failure (explain_p, parm, arg);
20799
20800 /* Deduce arguments T, i from TT<T> or TT<i>. */
20801 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20802 return 1;
20803
20804 arg = TYPE_TI_TEMPLATE (arg);
20805
20806 /* Fall through to deduce template name. */
20807 }
20808
20809 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20810 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20811 {
20812 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20813
20814 /* Simple cases: Value already set, does match or doesn't. */
20815 if (targ != NULL_TREE && template_args_equal (targ, arg))
20816 return unify_success (explain_p);
20817 else if (targ)
20818 return unify_inconsistency (explain_p, parm, targ, arg);
20819 }
20820 else
20821 {
20822 /* If PARM is `const T' and ARG is only `int', we don't have
20823 a match unless we are allowing additional qualification.
20824 If ARG is `const int' and PARM is just `T' that's OK;
20825 that binds `const int' to `T'. */
20826 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20827 arg, parm))
20828 return unify_cv_qual_mismatch (explain_p, parm, arg);
20829
20830 /* Consider the case where ARG is `const volatile int' and
20831 PARM is `const T'. Then, T should be `volatile int'. */
20832 arg = cp_build_qualified_type_real
20833 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20834 if (arg == error_mark_node)
20835 return unify_invalid (explain_p);
20836
20837 /* Simple cases: Value already set, does match or doesn't. */
20838 if (targ != NULL_TREE && same_type_p (targ, arg))
20839 return unify_success (explain_p);
20840 else if (targ)
20841 return unify_inconsistency (explain_p, parm, targ, arg);
20842
20843 /* Make sure that ARG is not a variable-sized array. (Note
20844 that were talking about variable-sized arrays (like
20845 `int[n]'), rather than arrays of unknown size (like
20846 `int[]').) We'll get very confused by such a type since
20847 the bound of the array is not constant, and therefore
20848 not mangleable. Besides, such types are not allowed in
20849 ISO C++, so we can do as we please here. We do allow
20850 them for 'auto' deduction, since that isn't ABI-exposed. */
20851 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20852 return unify_vla_arg (explain_p, arg);
20853
20854 /* Strip typedefs as in convert_template_argument. */
20855 arg = canonicalize_type_argument (arg, tf_none);
20856 }
20857
20858 /* If ARG is a parameter pack or an expansion, we cannot unify
20859 against it unless PARM is also a parameter pack. */
20860 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20861 && !template_parameter_pack_p (parm))
20862 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20863
20864 /* If the argument deduction results is a METHOD_TYPE,
20865 then there is a problem.
20866 METHOD_TYPE doesn't map to any real C++ type the result of
20867 the deduction can not be of that type. */
20868 if (TREE_CODE (arg) == METHOD_TYPE)
20869 return unify_method_type_error (explain_p, arg);
20870
20871 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20872 return unify_success (explain_p);
20873
20874 case TEMPLATE_PARM_INDEX:
20875 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20876 if (error_operand_p (tparm))
20877 return unify_invalid (explain_p);
20878
20879 if (TEMPLATE_PARM_LEVEL (parm)
20880 != template_decl_level (tparm))
20881 {
20882 /* The PARM is not one we're trying to unify. Just check
20883 to see if it matches ARG. */
20884 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20885 && cp_tree_equal (parm, arg));
20886 if (result)
20887 unify_expression_unequal (explain_p, parm, arg);
20888 return result;
20889 }
20890
20891 idx = TEMPLATE_PARM_IDX (parm);
20892 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20893
20894 if (targ)
20895 {
20896 int x = !cp_tree_equal (targ, arg);
20897 if (x)
20898 unify_inconsistency (explain_p, parm, targ, arg);
20899 return x;
20900 }
20901
20902 /* [temp.deduct.type] If, in the declaration of a function template
20903 with a non-type template-parameter, the non-type
20904 template-parameter is used in an expression in the function
20905 parameter-list and, if the corresponding template-argument is
20906 deduced, the template-argument type shall match the type of the
20907 template-parameter exactly, except that a template-argument
20908 deduced from an array bound may be of any integral type.
20909 The non-type parameter might use already deduced type parameters. */
20910 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20911 if (tree a = type_uses_auto (tparm))
20912 {
20913 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20914 if (tparm == error_mark_node)
20915 return 1;
20916 }
20917
20918 if (!TREE_TYPE (arg))
20919 /* Template-parameter dependent expression. Just accept it for now.
20920 It will later be processed in convert_template_argument. */
20921 ;
20922 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20923 non_reference (tparm)))
20924 /* OK */;
20925 else if ((strict & UNIFY_ALLOW_INTEGER)
20926 && CP_INTEGRAL_TYPE_P (tparm))
20927 /* Convert the ARG to the type of PARM; the deduced non-type
20928 template argument must exactly match the types of the
20929 corresponding parameter. */
20930 arg = fold (build_nop (tparm, arg));
20931 else if (uses_template_parms (tparm))
20932 {
20933 /* We haven't deduced the type of this parameter yet. */
20934 if (cxx_dialect >= cxx17
20935 /* We deduce from array bounds in try_array_deduction. */
20936 && !(strict & UNIFY_ALLOW_INTEGER))
20937 {
20938 /* Deduce it from the non-type argument. */
20939 tree atype = TREE_TYPE (arg);
20940 RECUR_AND_CHECK_FAILURE (tparms, targs,
20941 tparm, atype,
20942 UNIFY_ALLOW_NONE, explain_p);
20943 }
20944 else
20945 /* Try again later. */
20946 return unify_success (explain_p);
20947 }
20948 else
20949 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20950
20951 /* If ARG is a parameter pack or an expansion, we cannot unify
20952 against it unless PARM is also a parameter pack. */
20953 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20954 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20955 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20956
20957 {
20958 bool removed_attr = false;
20959 arg = strip_typedefs_expr (arg, &removed_attr);
20960 }
20961 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20962 return unify_success (explain_p);
20963
20964 case PTRMEM_CST:
20965 {
20966 /* A pointer-to-member constant can be unified only with
20967 another constant. */
20968 if (TREE_CODE (arg) != PTRMEM_CST)
20969 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20970
20971 /* Just unify the class member. It would be useless (and possibly
20972 wrong, depending on the strict flags) to unify also
20973 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20974 arg refer to the same variable, even if through different
20975 classes. For instance:
20976
20977 struct A { int x; };
20978 struct B : A { };
20979
20980 Unification of &A::x and &B::x must succeed. */
20981 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20982 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20983 }
20984
20985 case POINTER_TYPE:
20986 {
20987 if (!TYPE_PTR_P (arg))
20988 return unify_type_mismatch (explain_p, parm, arg);
20989
20990 /* [temp.deduct.call]
20991
20992 A can be another pointer or pointer to member type that can
20993 be converted to the deduced A via a qualification
20994 conversion (_conv.qual_).
20995
20996 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20997 This will allow for additional cv-qualification of the
20998 pointed-to types if appropriate. */
20999
21000 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21001 /* The derived-to-base conversion only persists through one
21002 level of pointers. */
21003 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21004
21005 return unify (tparms, targs, TREE_TYPE (parm),
21006 TREE_TYPE (arg), strict, explain_p);
21007 }
21008
21009 case REFERENCE_TYPE:
21010 if (TREE_CODE (arg) != REFERENCE_TYPE)
21011 return unify_type_mismatch (explain_p, parm, arg);
21012 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21013 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21014
21015 case ARRAY_TYPE:
21016 if (TREE_CODE (arg) != ARRAY_TYPE)
21017 return unify_type_mismatch (explain_p, parm, arg);
21018 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21019 != (TYPE_DOMAIN (arg) == NULL_TREE))
21020 return unify_type_mismatch (explain_p, parm, arg);
21021 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21022 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21023 if (TYPE_DOMAIN (parm) != NULL_TREE)
21024 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21025 TYPE_DOMAIN (arg), explain_p);
21026 return unify_success (explain_p);
21027
21028 case REAL_TYPE:
21029 case COMPLEX_TYPE:
21030 case VECTOR_TYPE:
21031 case INTEGER_TYPE:
21032 case BOOLEAN_TYPE:
21033 case ENUMERAL_TYPE:
21034 case VOID_TYPE:
21035 case NULLPTR_TYPE:
21036 if (TREE_CODE (arg) != TREE_CODE (parm))
21037 return unify_type_mismatch (explain_p, parm, arg);
21038
21039 /* We have already checked cv-qualification at the top of the
21040 function. */
21041 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21042 return unify_type_mismatch (explain_p, parm, arg);
21043
21044 /* As far as unification is concerned, this wins. Later checks
21045 will invalidate it if necessary. */
21046 return unify_success (explain_p);
21047
21048 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21049 /* Type INTEGER_CST can come from ordinary constant template args. */
21050 case INTEGER_CST:
21051 while (CONVERT_EXPR_P (arg))
21052 arg = TREE_OPERAND (arg, 0);
21053
21054 if (TREE_CODE (arg) != INTEGER_CST)
21055 return unify_template_argument_mismatch (explain_p, parm, arg);
21056 return (tree_int_cst_equal (parm, arg)
21057 ? unify_success (explain_p)
21058 : unify_template_argument_mismatch (explain_p, parm, arg));
21059
21060 case TREE_VEC:
21061 {
21062 int i, len, argslen;
21063 int parm_variadic_p = 0;
21064
21065 if (TREE_CODE (arg) != TREE_VEC)
21066 return unify_template_argument_mismatch (explain_p, parm, arg);
21067
21068 len = TREE_VEC_LENGTH (parm);
21069 argslen = TREE_VEC_LENGTH (arg);
21070
21071 /* Check for pack expansions in the parameters. */
21072 for (i = 0; i < len; ++i)
21073 {
21074 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21075 {
21076 if (i == len - 1)
21077 /* We can unify against something with a trailing
21078 parameter pack. */
21079 parm_variadic_p = 1;
21080 else
21081 /* [temp.deduct.type]/9: If the template argument list of
21082 P contains a pack expansion that is not the last
21083 template argument, the entire template argument list
21084 is a non-deduced context. */
21085 return unify_success (explain_p);
21086 }
21087 }
21088
21089 /* If we don't have enough arguments to satisfy the parameters
21090 (not counting the pack expression at the end), or we have
21091 too many arguments for a parameter list that doesn't end in
21092 a pack expression, we can't unify. */
21093 if (parm_variadic_p
21094 ? argslen < len - parm_variadic_p
21095 : argslen != len)
21096 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21097
21098 /* Unify all of the parameters that precede the (optional)
21099 pack expression. */
21100 for (i = 0; i < len - parm_variadic_p; ++i)
21101 {
21102 RECUR_AND_CHECK_FAILURE (tparms, targs,
21103 TREE_VEC_ELT (parm, i),
21104 TREE_VEC_ELT (arg, i),
21105 UNIFY_ALLOW_NONE, explain_p);
21106 }
21107 if (parm_variadic_p)
21108 return unify_pack_expansion (tparms, targs, parm, arg,
21109 DEDUCE_EXACT,
21110 /*subr=*/true, explain_p);
21111 return unify_success (explain_p);
21112 }
21113
21114 case RECORD_TYPE:
21115 case UNION_TYPE:
21116 if (TREE_CODE (arg) != TREE_CODE (parm))
21117 return unify_type_mismatch (explain_p, parm, arg);
21118
21119 if (TYPE_PTRMEMFUNC_P (parm))
21120 {
21121 if (!TYPE_PTRMEMFUNC_P (arg))
21122 return unify_type_mismatch (explain_p, parm, arg);
21123
21124 return unify (tparms, targs,
21125 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21126 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21127 strict, explain_p);
21128 }
21129 else if (TYPE_PTRMEMFUNC_P (arg))
21130 return unify_type_mismatch (explain_p, parm, arg);
21131
21132 if (CLASSTYPE_TEMPLATE_INFO (parm))
21133 {
21134 tree t = NULL_TREE;
21135
21136 if (strict_in & UNIFY_ALLOW_DERIVED)
21137 {
21138 /* First, we try to unify the PARM and ARG directly. */
21139 t = try_class_unification (tparms, targs,
21140 parm, arg, explain_p);
21141
21142 if (!t)
21143 {
21144 /* Fallback to the special case allowed in
21145 [temp.deduct.call]:
21146
21147 If P is a class, and P has the form
21148 template-id, then A can be a derived class of
21149 the deduced A. Likewise, if P is a pointer to
21150 a class of the form template-id, A can be a
21151 pointer to a derived class pointed to by the
21152 deduced A. */
21153 enum template_base_result r;
21154 r = get_template_base (tparms, targs, parm, arg,
21155 explain_p, &t);
21156
21157 if (!t)
21158 {
21159 /* Don't give the derived diagnostic if we're
21160 already dealing with the same template. */
21161 bool same_template
21162 = (CLASSTYPE_TEMPLATE_INFO (arg)
21163 && (CLASSTYPE_TI_TEMPLATE (parm)
21164 == CLASSTYPE_TI_TEMPLATE (arg)));
21165 return unify_no_common_base (explain_p && !same_template,
21166 r, parm, arg);
21167 }
21168 }
21169 }
21170 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21171 && (CLASSTYPE_TI_TEMPLATE (parm)
21172 == CLASSTYPE_TI_TEMPLATE (arg)))
21173 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21174 Then, we should unify `int' and `U'. */
21175 t = arg;
21176 else
21177 /* There's no chance of unification succeeding. */
21178 return unify_type_mismatch (explain_p, parm, arg);
21179
21180 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21181 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21182 }
21183 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21184 return unify_type_mismatch (explain_p, parm, arg);
21185 return unify_success (explain_p);
21186
21187 case METHOD_TYPE:
21188 case FUNCTION_TYPE:
21189 {
21190 unsigned int nargs;
21191 tree *args;
21192 tree a;
21193 unsigned int i;
21194
21195 if (TREE_CODE (arg) != TREE_CODE (parm))
21196 return unify_type_mismatch (explain_p, parm, arg);
21197
21198 /* CV qualifications for methods can never be deduced, they must
21199 match exactly. We need to check them explicitly here,
21200 because type_unification_real treats them as any other
21201 cv-qualified parameter. */
21202 if (TREE_CODE (parm) == METHOD_TYPE
21203 && (!check_cv_quals_for_unify
21204 (UNIFY_ALLOW_NONE,
21205 class_of_this_parm (arg),
21206 class_of_this_parm (parm))))
21207 return unify_cv_qual_mismatch (explain_p, parm, arg);
21208 if (TREE_CODE (arg) == FUNCTION_TYPE
21209 && type_memfn_quals (parm) != type_memfn_quals (arg))
21210 return unify_cv_qual_mismatch (explain_p, parm, arg);
21211 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21212 return unify_type_mismatch (explain_p, parm, arg);
21213
21214 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21215 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21216
21217 nargs = list_length (TYPE_ARG_TYPES (arg));
21218 args = XALLOCAVEC (tree, nargs);
21219 for (a = TYPE_ARG_TYPES (arg), i = 0;
21220 a != NULL_TREE && a != void_list_node;
21221 a = TREE_CHAIN (a), ++i)
21222 args[i] = TREE_VALUE (a);
21223 nargs = i;
21224
21225 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21226 args, nargs, 1, DEDUCE_EXACT,
21227 LOOKUP_NORMAL, NULL, explain_p))
21228 return 1;
21229
21230 if (flag_noexcept_type)
21231 {
21232 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21233 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21234 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21235 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21236 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21237 && uses_template_parms (TREE_PURPOSE (pspec)))
21238 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21239 TREE_PURPOSE (aspec),
21240 UNIFY_ALLOW_NONE, explain_p);
21241 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21242 return unify_type_mismatch (explain_p, parm, arg);
21243 }
21244
21245 return 0;
21246 }
21247
21248 case OFFSET_TYPE:
21249 /* Unify a pointer to member with a pointer to member function, which
21250 deduces the type of the member as a function type. */
21251 if (TYPE_PTRMEMFUNC_P (arg))
21252 {
21253 /* Check top-level cv qualifiers */
21254 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21255 return unify_cv_qual_mismatch (explain_p, parm, arg);
21256
21257 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21258 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21259 UNIFY_ALLOW_NONE, explain_p);
21260
21261 /* Determine the type of the function we are unifying against. */
21262 tree fntype = static_fn_type (arg);
21263
21264 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21265 }
21266
21267 if (TREE_CODE (arg) != OFFSET_TYPE)
21268 return unify_type_mismatch (explain_p, parm, arg);
21269 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21270 TYPE_OFFSET_BASETYPE (arg),
21271 UNIFY_ALLOW_NONE, explain_p);
21272 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21273 strict, explain_p);
21274
21275 case CONST_DECL:
21276 if (DECL_TEMPLATE_PARM_P (parm))
21277 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21278 if (arg != scalar_constant_value (parm))
21279 return unify_template_argument_mismatch (explain_p, parm, arg);
21280 return unify_success (explain_p);
21281
21282 case FIELD_DECL:
21283 case TEMPLATE_DECL:
21284 /* Matched cases are handled by the ARG == PARM test above. */
21285 return unify_template_argument_mismatch (explain_p, parm, arg);
21286
21287 case VAR_DECL:
21288 /* We might get a variable as a non-type template argument in parm if the
21289 corresponding parameter is type-dependent. Make any necessary
21290 adjustments based on whether arg is a reference. */
21291 if (CONSTANT_CLASS_P (arg))
21292 parm = fold_non_dependent_expr (parm);
21293 else if (REFERENCE_REF_P (arg))
21294 {
21295 tree sub = TREE_OPERAND (arg, 0);
21296 STRIP_NOPS (sub);
21297 if (TREE_CODE (sub) == ADDR_EXPR)
21298 arg = TREE_OPERAND (sub, 0);
21299 }
21300 /* Now use the normal expression code to check whether they match. */
21301 goto expr;
21302
21303 case TYPE_ARGUMENT_PACK:
21304 case NONTYPE_ARGUMENT_PACK:
21305 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21306 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21307
21308 case TYPEOF_TYPE:
21309 case DECLTYPE_TYPE:
21310 case UNDERLYING_TYPE:
21311 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21312 or UNDERLYING_TYPE nodes. */
21313 return unify_success (explain_p);
21314
21315 case ERROR_MARK:
21316 /* Unification fails if we hit an error node. */
21317 return unify_invalid (explain_p);
21318
21319 case INDIRECT_REF:
21320 if (REFERENCE_REF_P (parm))
21321 {
21322 bool pexp = PACK_EXPANSION_P (arg);
21323 if (pexp)
21324 arg = PACK_EXPANSION_PATTERN (arg);
21325 if (REFERENCE_REF_P (arg))
21326 arg = TREE_OPERAND (arg, 0);
21327 if (pexp)
21328 arg = make_pack_expansion (arg);
21329 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21330 strict, explain_p);
21331 }
21332 /* FALLTHRU */
21333
21334 default:
21335 /* An unresolved overload is a nondeduced context. */
21336 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21337 return unify_success (explain_p);
21338 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21339 expr:
21340 /* We must be looking at an expression. This can happen with
21341 something like:
21342
21343 template <int I>
21344 void foo(S<I>, S<I + 2>);
21345
21346 This is a "nondeduced context":
21347
21348 [deduct.type]
21349
21350 The nondeduced contexts are:
21351
21352 --A type that is a template-id in which one or more of
21353 the template-arguments is an expression that references
21354 a template-parameter.
21355
21356 In these cases, we assume deduction succeeded, but don't
21357 actually infer any unifications. */
21358
21359 if (!uses_template_parms (parm)
21360 && !template_args_equal (parm, arg))
21361 return unify_expression_unequal (explain_p, parm, arg);
21362 else
21363 return unify_success (explain_p);
21364 }
21365 }
21366 #undef RECUR_AND_CHECK_FAILURE
21367 \f
21368 /* Note that DECL can be defined in this translation unit, if
21369 required. */
21370
21371 static void
21372 mark_definable (tree decl)
21373 {
21374 tree clone;
21375 DECL_NOT_REALLY_EXTERN (decl) = 1;
21376 FOR_EACH_CLONE (clone, decl)
21377 DECL_NOT_REALLY_EXTERN (clone) = 1;
21378 }
21379
21380 /* Called if RESULT is explicitly instantiated, or is a member of an
21381 explicitly instantiated class. */
21382
21383 void
21384 mark_decl_instantiated (tree result, int extern_p)
21385 {
21386 SET_DECL_EXPLICIT_INSTANTIATION (result);
21387
21388 /* If this entity has already been written out, it's too late to
21389 make any modifications. */
21390 if (TREE_ASM_WRITTEN (result))
21391 return;
21392
21393 /* For anonymous namespace we don't need to do anything. */
21394 if (decl_anon_ns_mem_p (result))
21395 {
21396 gcc_assert (!TREE_PUBLIC (result));
21397 return;
21398 }
21399
21400 if (TREE_CODE (result) != FUNCTION_DECL)
21401 /* The TREE_PUBLIC flag for function declarations will have been
21402 set correctly by tsubst. */
21403 TREE_PUBLIC (result) = 1;
21404
21405 /* This might have been set by an earlier implicit instantiation. */
21406 DECL_COMDAT (result) = 0;
21407
21408 if (extern_p)
21409 DECL_NOT_REALLY_EXTERN (result) = 0;
21410 else
21411 {
21412 mark_definable (result);
21413 mark_needed (result);
21414 /* Always make artificials weak. */
21415 if (DECL_ARTIFICIAL (result) && flag_weak)
21416 comdat_linkage (result);
21417 /* For WIN32 we also want to put explicit instantiations in
21418 linkonce sections. */
21419 else if (TREE_PUBLIC (result))
21420 maybe_make_one_only (result);
21421 }
21422
21423 /* If EXTERN_P, then this function will not be emitted -- unless
21424 followed by an explicit instantiation, at which point its linkage
21425 will be adjusted. If !EXTERN_P, then this function will be
21426 emitted here. In neither circumstance do we want
21427 import_export_decl to adjust the linkage. */
21428 DECL_INTERFACE_KNOWN (result) = 1;
21429 }
21430
21431 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21432 important template arguments. If any are missing, we check whether
21433 they're important by using error_mark_node for substituting into any
21434 args that were used for partial ordering (the ones between ARGS and END)
21435 and seeing if it bubbles up. */
21436
21437 static bool
21438 check_undeduced_parms (tree targs, tree args, tree end)
21439 {
21440 bool found = false;
21441 int i;
21442 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21443 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21444 {
21445 found = true;
21446 TREE_VEC_ELT (targs, i) = error_mark_node;
21447 }
21448 if (found)
21449 {
21450 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21451 if (substed == error_mark_node)
21452 return true;
21453 }
21454 return false;
21455 }
21456
21457 /* Given two function templates PAT1 and PAT2, return:
21458
21459 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21460 -1 if PAT2 is more specialized than PAT1.
21461 0 if neither is more specialized.
21462
21463 LEN indicates the number of parameters we should consider
21464 (defaulted parameters should not be considered).
21465
21466 The 1998 std underspecified function template partial ordering, and
21467 DR214 addresses the issue. We take pairs of arguments, one from
21468 each of the templates, and deduce them against each other. One of
21469 the templates will be more specialized if all the *other*
21470 template's arguments deduce against its arguments and at least one
21471 of its arguments *does* *not* deduce against the other template's
21472 corresponding argument. Deduction is done as for class templates.
21473 The arguments used in deduction have reference and top level cv
21474 qualifiers removed. Iff both arguments were originally reference
21475 types *and* deduction succeeds in both directions, an lvalue reference
21476 wins against an rvalue reference and otherwise the template
21477 with the more cv-qualified argument wins for that pairing (if
21478 neither is more cv-qualified, they both are equal). Unlike regular
21479 deduction, after all the arguments have been deduced in this way,
21480 we do *not* verify the deduced template argument values can be
21481 substituted into non-deduced contexts.
21482
21483 The logic can be a bit confusing here, because we look at deduce1 and
21484 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21485 can find template arguments for pat1 to make arg1 look like arg2, that
21486 means that arg2 is at least as specialized as arg1. */
21487
21488 int
21489 more_specialized_fn (tree pat1, tree pat2, int len)
21490 {
21491 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21492 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21493 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21494 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21495 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21496 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21497 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21498 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21499 tree origs1, origs2;
21500 bool lose1 = false;
21501 bool lose2 = false;
21502
21503 /* Remove the this parameter from non-static member functions. If
21504 one is a non-static member function and the other is not a static
21505 member function, remove the first parameter from that function
21506 also. This situation occurs for operator functions where we
21507 locate both a member function (with this pointer) and non-member
21508 operator (with explicit first operand). */
21509 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21510 {
21511 len--; /* LEN is the number of significant arguments for DECL1 */
21512 args1 = TREE_CHAIN (args1);
21513 if (!DECL_STATIC_FUNCTION_P (decl2))
21514 args2 = TREE_CHAIN (args2);
21515 }
21516 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21517 {
21518 args2 = TREE_CHAIN (args2);
21519 if (!DECL_STATIC_FUNCTION_P (decl1))
21520 {
21521 len--;
21522 args1 = TREE_CHAIN (args1);
21523 }
21524 }
21525
21526 /* If only one is a conversion operator, they are unordered. */
21527 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21528 return 0;
21529
21530 /* Consider the return type for a conversion function */
21531 if (DECL_CONV_FN_P (decl1))
21532 {
21533 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21534 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21535 len++;
21536 }
21537
21538 processing_template_decl++;
21539
21540 origs1 = args1;
21541 origs2 = args2;
21542
21543 while (len--
21544 /* Stop when an ellipsis is seen. */
21545 && args1 != NULL_TREE && args2 != NULL_TREE)
21546 {
21547 tree arg1 = TREE_VALUE (args1);
21548 tree arg2 = TREE_VALUE (args2);
21549 int deduce1, deduce2;
21550 int quals1 = -1;
21551 int quals2 = -1;
21552 int ref1 = 0;
21553 int ref2 = 0;
21554
21555 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21556 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21557 {
21558 /* When both arguments are pack expansions, we need only
21559 unify the patterns themselves. */
21560 arg1 = PACK_EXPANSION_PATTERN (arg1);
21561 arg2 = PACK_EXPANSION_PATTERN (arg2);
21562
21563 /* This is the last comparison we need to do. */
21564 len = 0;
21565 }
21566
21567 /* DR 1847: If a particular P contains no template-parameters that
21568 participate in template argument deduction, that P is not used to
21569 determine the ordering. */
21570 if (!uses_deducible_template_parms (arg1)
21571 && !uses_deducible_template_parms (arg2))
21572 goto next;
21573
21574 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21575 {
21576 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21577 arg1 = TREE_TYPE (arg1);
21578 quals1 = cp_type_quals (arg1);
21579 }
21580
21581 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21582 {
21583 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21584 arg2 = TREE_TYPE (arg2);
21585 quals2 = cp_type_quals (arg2);
21586 }
21587
21588 arg1 = TYPE_MAIN_VARIANT (arg1);
21589 arg2 = TYPE_MAIN_VARIANT (arg2);
21590
21591 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21592 {
21593 int i, len2 = remaining_arguments (args2);
21594 tree parmvec = make_tree_vec (1);
21595 tree argvec = make_tree_vec (len2);
21596 tree ta = args2;
21597
21598 /* Setup the parameter vector, which contains only ARG1. */
21599 TREE_VEC_ELT (parmvec, 0) = arg1;
21600
21601 /* Setup the argument vector, which contains the remaining
21602 arguments. */
21603 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21604 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21605
21606 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21607 argvec, DEDUCE_EXACT,
21608 /*subr=*/true, /*explain_p=*/false)
21609 == 0);
21610
21611 /* We cannot deduce in the other direction, because ARG1 is
21612 a pack expansion but ARG2 is not. */
21613 deduce2 = 0;
21614 }
21615 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21616 {
21617 int i, len1 = remaining_arguments (args1);
21618 tree parmvec = make_tree_vec (1);
21619 tree argvec = make_tree_vec (len1);
21620 tree ta = args1;
21621
21622 /* Setup the parameter vector, which contains only ARG1. */
21623 TREE_VEC_ELT (parmvec, 0) = arg2;
21624
21625 /* Setup the argument vector, which contains the remaining
21626 arguments. */
21627 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21628 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21629
21630 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21631 argvec, DEDUCE_EXACT,
21632 /*subr=*/true, /*explain_p=*/false)
21633 == 0);
21634
21635 /* We cannot deduce in the other direction, because ARG2 is
21636 a pack expansion but ARG1 is not.*/
21637 deduce1 = 0;
21638 }
21639
21640 else
21641 {
21642 /* The normal case, where neither argument is a pack
21643 expansion. */
21644 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21645 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21646 == 0);
21647 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21648 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21649 == 0);
21650 }
21651
21652 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21653 arg2, then arg2 is not as specialized as arg1. */
21654 if (!deduce1)
21655 lose2 = true;
21656 if (!deduce2)
21657 lose1 = true;
21658
21659 /* "If, for a given type, deduction succeeds in both directions
21660 (i.e., the types are identical after the transformations above)
21661 and both P and A were reference types (before being replaced with
21662 the type referred to above):
21663 - if the type from the argument template was an lvalue reference and
21664 the type from the parameter template was not, the argument type is
21665 considered to be more specialized than the other; otherwise,
21666 - if the type from the argument template is more cv-qualified
21667 than the type from the parameter template (as described above),
21668 the argument type is considered to be more specialized than the other;
21669 otherwise,
21670 - neither type is more specialized than the other." */
21671
21672 if (deduce1 && deduce2)
21673 {
21674 if (ref1 && ref2 && ref1 != ref2)
21675 {
21676 if (ref1 > ref2)
21677 lose1 = true;
21678 else
21679 lose2 = true;
21680 }
21681 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21682 {
21683 if ((quals1 & quals2) == quals2)
21684 lose2 = true;
21685 if ((quals1 & quals2) == quals1)
21686 lose1 = true;
21687 }
21688 }
21689
21690 if (lose1 && lose2)
21691 /* We've failed to deduce something in either direction.
21692 These must be unordered. */
21693 break;
21694
21695 next:
21696
21697 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21698 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21699 /* We have already processed all of the arguments in our
21700 handing of the pack expansion type. */
21701 len = 0;
21702
21703 args1 = TREE_CHAIN (args1);
21704 args2 = TREE_CHAIN (args2);
21705 }
21706
21707 /* "In most cases, all template parameters must have values in order for
21708 deduction to succeed, but for partial ordering purposes a template
21709 parameter may remain without a value provided it is not used in the
21710 types being used for partial ordering."
21711
21712 Thus, if we are missing any of the targs1 we need to substitute into
21713 origs1, then pat2 is not as specialized as pat1. This can happen when
21714 there is a nondeduced context. */
21715 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21716 lose2 = true;
21717 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21718 lose1 = true;
21719
21720 processing_template_decl--;
21721
21722 /* If both deductions succeed, the partial ordering selects the more
21723 constrained template. */
21724 if (!lose1 && !lose2)
21725 {
21726 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21727 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21728 lose1 = !subsumes_constraints (c1, c2);
21729 lose2 = !subsumes_constraints (c2, c1);
21730 }
21731
21732 /* All things being equal, if the next argument is a pack expansion
21733 for one function but not for the other, prefer the
21734 non-variadic function. FIXME this is bogus; see c++/41958. */
21735 if (lose1 == lose2
21736 && args1 && TREE_VALUE (args1)
21737 && args2 && TREE_VALUE (args2))
21738 {
21739 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21740 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21741 }
21742
21743 if (lose1 == lose2)
21744 return 0;
21745 else if (!lose1)
21746 return 1;
21747 else
21748 return -1;
21749 }
21750
21751 /* Determine which of two partial specializations of TMPL is more
21752 specialized.
21753
21754 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21755 to the first partial specialization. The TREE_PURPOSE is the
21756 innermost set of template parameters for the partial
21757 specialization. PAT2 is similar, but for the second template.
21758
21759 Return 1 if the first partial specialization is more specialized;
21760 -1 if the second is more specialized; 0 if neither is more
21761 specialized.
21762
21763 See [temp.class.order] for information about determining which of
21764 two templates is more specialized. */
21765
21766 static int
21767 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21768 {
21769 tree targs;
21770 int winner = 0;
21771 bool any_deductions = false;
21772
21773 tree tmpl1 = TREE_VALUE (pat1);
21774 tree tmpl2 = TREE_VALUE (pat2);
21775 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21776 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21777
21778 /* Just like what happens for functions, if we are ordering between
21779 different template specializations, we may encounter dependent
21780 types in the arguments, and we need our dependency check functions
21781 to behave correctly. */
21782 ++processing_template_decl;
21783 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21784 if (targs)
21785 {
21786 --winner;
21787 any_deductions = true;
21788 }
21789
21790 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21791 if (targs)
21792 {
21793 ++winner;
21794 any_deductions = true;
21795 }
21796 --processing_template_decl;
21797
21798 /* If both deductions succeed, the partial ordering selects the more
21799 constrained template. */
21800 if (!winner && any_deductions)
21801 return more_constrained (tmpl1, tmpl2);
21802
21803 /* In the case of a tie where at least one of the templates
21804 has a parameter pack at the end, the template with the most
21805 non-packed parameters wins. */
21806 if (winner == 0
21807 && any_deductions
21808 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21809 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21810 {
21811 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21812 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21813 int len1 = TREE_VEC_LENGTH (args1);
21814 int len2 = TREE_VEC_LENGTH (args2);
21815
21816 /* We don't count the pack expansion at the end. */
21817 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21818 --len1;
21819 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21820 --len2;
21821
21822 if (len1 > len2)
21823 return 1;
21824 else if (len1 < len2)
21825 return -1;
21826 }
21827
21828 return winner;
21829 }
21830
21831 /* Return the template arguments that will produce the function signature
21832 DECL from the function template FN, with the explicit template
21833 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21834 also match. Return NULL_TREE if no satisfactory arguments could be
21835 found. */
21836
21837 static tree
21838 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21839 {
21840 int ntparms = DECL_NTPARMS (fn);
21841 tree targs = make_tree_vec (ntparms);
21842 tree decl_type = TREE_TYPE (decl);
21843 tree decl_arg_types;
21844 tree *args;
21845 unsigned int nargs, ix;
21846 tree arg;
21847
21848 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21849
21850 /* Never do unification on the 'this' parameter. */
21851 decl_arg_types = skip_artificial_parms_for (decl,
21852 TYPE_ARG_TYPES (decl_type));
21853
21854 nargs = list_length (decl_arg_types);
21855 args = XALLOCAVEC (tree, nargs);
21856 for (arg = decl_arg_types, ix = 0;
21857 arg != NULL_TREE && arg != void_list_node;
21858 arg = TREE_CHAIN (arg), ++ix)
21859 args[ix] = TREE_VALUE (arg);
21860
21861 if (fn_type_unification (fn, explicit_args, targs,
21862 args, ix,
21863 (check_rettype || DECL_CONV_FN_P (fn)
21864 ? TREE_TYPE (decl_type) : NULL_TREE),
21865 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21866 /*decltype*/false)
21867 == error_mark_node)
21868 return NULL_TREE;
21869
21870 return targs;
21871 }
21872
21873 /* Return the innermost template arguments that, when applied to a partial
21874 specialization SPEC_TMPL of TMPL, yield the ARGS.
21875
21876 For example, suppose we have:
21877
21878 template <class T, class U> struct S {};
21879 template <class T> struct S<T*, int> {};
21880
21881 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21882 partial specialization and the ARGS will be {double*, int}. The resulting
21883 vector will be {double}, indicating that `T' is bound to `double'. */
21884
21885 static tree
21886 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21887 {
21888 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21889 tree spec_args
21890 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21891 int i, ntparms = TREE_VEC_LENGTH (tparms);
21892 tree deduced_args;
21893 tree innermost_deduced_args;
21894
21895 innermost_deduced_args = make_tree_vec (ntparms);
21896 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21897 {
21898 deduced_args = copy_node (args);
21899 SET_TMPL_ARGS_LEVEL (deduced_args,
21900 TMPL_ARGS_DEPTH (deduced_args),
21901 innermost_deduced_args);
21902 }
21903 else
21904 deduced_args = innermost_deduced_args;
21905
21906 bool tried_array_deduction = (cxx_dialect < cxx17);
21907 again:
21908 if (unify (tparms, deduced_args,
21909 INNERMOST_TEMPLATE_ARGS (spec_args),
21910 INNERMOST_TEMPLATE_ARGS (args),
21911 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21912 return NULL_TREE;
21913
21914 for (i = 0; i < ntparms; ++i)
21915 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21916 {
21917 if (!tried_array_deduction)
21918 {
21919 try_array_deduction (tparms, innermost_deduced_args,
21920 INNERMOST_TEMPLATE_ARGS (spec_args));
21921 tried_array_deduction = true;
21922 if (TREE_VEC_ELT (innermost_deduced_args, i))
21923 goto again;
21924 }
21925 return NULL_TREE;
21926 }
21927
21928 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21929 if (!push_tinst_level (tinst))
21930 {
21931 excessive_deduction_depth = true;
21932 return NULL_TREE;
21933 }
21934
21935 /* Verify that nondeduced template arguments agree with the type
21936 obtained from argument deduction.
21937
21938 For example:
21939
21940 struct A { typedef int X; };
21941 template <class T, class U> struct C {};
21942 template <class T> struct C<T, typename T::X> {};
21943
21944 Then with the instantiation `C<A, int>', we can deduce that
21945 `T' is `A' but unify () does not check whether `typename T::X'
21946 is `int'. */
21947 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21948
21949 if (spec_args != error_mark_node)
21950 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21951 INNERMOST_TEMPLATE_ARGS (spec_args),
21952 tmpl, tf_none, false, false);
21953
21954 pop_tinst_level ();
21955
21956 if (spec_args == error_mark_node
21957 /* We only need to check the innermost arguments; the other
21958 arguments will always agree. */
21959 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21960 INNERMOST_TEMPLATE_ARGS (args)))
21961 return NULL_TREE;
21962
21963 /* Now that we have bindings for all of the template arguments,
21964 ensure that the arguments deduced for the template template
21965 parameters have compatible template parameter lists. See the use
21966 of template_template_parm_bindings_ok_p in fn_type_unification
21967 for more information. */
21968 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21969 return NULL_TREE;
21970
21971 return deduced_args;
21972 }
21973
21974 // Compare two function templates T1 and T2 by deducing bindings
21975 // from one against the other. If both deductions succeed, compare
21976 // constraints to see which is more constrained.
21977 static int
21978 more_specialized_inst (tree t1, tree t2)
21979 {
21980 int fate = 0;
21981 int count = 0;
21982
21983 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21984 {
21985 --fate;
21986 ++count;
21987 }
21988
21989 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21990 {
21991 ++fate;
21992 ++count;
21993 }
21994
21995 // If both deductions succeed, then one may be more constrained.
21996 if (count == 2 && fate == 0)
21997 fate = more_constrained (t1, t2);
21998
21999 return fate;
22000 }
22001
22002 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22003 Return the TREE_LIST node with the most specialized template, if
22004 any. If there is no most specialized template, the error_mark_node
22005 is returned.
22006
22007 Note that this function does not look at, or modify, the
22008 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22009 returned is one of the elements of INSTANTIATIONS, callers may
22010 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22011 and retrieve it from the value returned. */
22012
22013 tree
22014 most_specialized_instantiation (tree templates)
22015 {
22016 tree fn, champ;
22017
22018 ++processing_template_decl;
22019
22020 champ = templates;
22021 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22022 {
22023 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22024 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22025 if (fate == -1)
22026 champ = fn;
22027 else if (!fate)
22028 {
22029 /* Equally specialized, move to next function. If there
22030 is no next function, nothing's most specialized. */
22031 fn = TREE_CHAIN (fn);
22032 champ = fn;
22033 if (!fn)
22034 break;
22035 }
22036 }
22037
22038 if (champ)
22039 /* Now verify that champ is better than everything earlier in the
22040 instantiation list. */
22041 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22042 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22043 {
22044 champ = NULL_TREE;
22045 break;
22046 }
22047 }
22048
22049 processing_template_decl--;
22050
22051 if (!champ)
22052 return error_mark_node;
22053
22054 return champ;
22055 }
22056
22057 /* If DECL is a specialization of some template, return the most
22058 general such template. Otherwise, returns NULL_TREE.
22059
22060 For example, given:
22061
22062 template <class T> struct S { template <class U> void f(U); };
22063
22064 if TMPL is `template <class U> void S<int>::f(U)' this will return
22065 the full template. This function will not trace past partial
22066 specializations, however. For example, given in addition:
22067
22068 template <class T> struct S<T*> { template <class U> void f(U); };
22069
22070 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22071 `template <class T> template <class U> S<T*>::f(U)'. */
22072
22073 tree
22074 most_general_template (tree decl)
22075 {
22076 if (TREE_CODE (decl) != TEMPLATE_DECL)
22077 {
22078 if (tree tinfo = get_template_info (decl))
22079 decl = TI_TEMPLATE (tinfo);
22080 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22081 template friend, or a FIELD_DECL for a capture pack. */
22082 if (TREE_CODE (decl) != TEMPLATE_DECL)
22083 return NULL_TREE;
22084 }
22085
22086 /* Look for more and more general templates. */
22087 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22088 {
22089 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22090 (See cp-tree.h for details.) */
22091 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22092 break;
22093
22094 if (CLASS_TYPE_P (TREE_TYPE (decl))
22095 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22096 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22097 break;
22098
22099 /* Stop if we run into an explicitly specialized class template. */
22100 if (!DECL_NAMESPACE_SCOPE_P (decl)
22101 && DECL_CONTEXT (decl)
22102 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22103 break;
22104
22105 decl = DECL_TI_TEMPLATE (decl);
22106 }
22107
22108 return decl;
22109 }
22110
22111 /* Return the most specialized of the template partial specializations
22112 which can produce TARGET, a specialization of some class or variable
22113 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22114 a TEMPLATE_DECL node corresponding to the partial specialization, while
22115 the TREE_PURPOSE is the set of template arguments that must be
22116 substituted into the template pattern in order to generate TARGET.
22117
22118 If the choice of partial specialization is ambiguous, a diagnostic
22119 is issued, and the error_mark_node is returned. If there are no
22120 partial specializations matching TARGET, then NULL_TREE is
22121 returned, indicating that the primary template should be used. */
22122
22123 static tree
22124 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22125 {
22126 tree list = NULL_TREE;
22127 tree t;
22128 tree champ;
22129 int fate;
22130 bool ambiguous_p;
22131 tree outer_args = NULL_TREE;
22132 tree tmpl, args;
22133
22134 if (TYPE_P (target))
22135 {
22136 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22137 tmpl = TI_TEMPLATE (tinfo);
22138 args = TI_ARGS (tinfo);
22139 }
22140 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22141 {
22142 tmpl = TREE_OPERAND (target, 0);
22143 args = TREE_OPERAND (target, 1);
22144 }
22145 else if (VAR_P (target))
22146 {
22147 tree tinfo = DECL_TEMPLATE_INFO (target);
22148 tmpl = TI_TEMPLATE (tinfo);
22149 args = TI_ARGS (tinfo);
22150 }
22151 else
22152 gcc_unreachable ();
22153
22154 tree main_tmpl = most_general_template (tmpl);
22155
22156 /* For determining which partial specialization to use, only the
22157 innermost args are interesting. */
22158 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22159 {
22160 outer_args = strip_innermost_template_args (args, 1);
22161 args = INNERMOST_TEMPLATE_ARGS (args);
22162 }
22163
22164 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22165 {
22166 tree spec_args;
22167 tree spec_tmpl = TREE_VALUE (t);
22168
22169 if (outer_args)
22170 {
22171 /* Substitute in the template args from the enclosing class. */
22172 ++processing_template_decl;
22173 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22174 --processing_template_decl;
22175 }
22176
22177 if (spec_tmpl == error_mark_node)
22178 return error_mark_node;
22179
22180 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22181 if (spec_args)
22182 {
22183 if (outer_args)
22184 spec_args = add_to_template_args (outer_args, spec_args);
22185
22186 /* Keep the candidate only if the constraints are satisfied,
22187 or if we're not compiling with concepts. */
22188 if (!flag_concepts
22189 || constraints_satisfied_p (spec_tmpl, spec_args))
22190 {
22191 list = tree_cons (spec_args, TREE_VALUE (t), list);
22192 TREE_TYPE (list) = TREE_TYPE (t);
22193 }
22194 }
22195 }
22196
22197 if (! list)
22198 return NULL_TREE;
22199
22200 ambiguous_p = false;
22201 t = list;
22202 champ = t;
22203 t = TREE_CHAIN (t);
22204 for (; t; t = TREE_CHAIN (t))
22205 {
22206 fate = more_specialized_partial_spec (tmpl, champ, t);
22207 if (fate == 1)
22208 ;
22209 else
22210 {
22211 if (fate == 0)
22212 {
22213 t = TREE_CHAIN (t);
22214 if (! t)
22215 {
22216 ambiguous_p = true;
22217 break;
22218 }
22219 }
22220 champ = t;
22221 }
22222 }
22223
22224 if (!ambiguous_p)
22225 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22226 {
22227 fate = more_specialized_partial_spec (tmpl, champ, t);
22228 if (fate != 1)
22229 {
22230 ambiguous_p = true;
22231 break;
22232 }
22233 }
22234
22235 if (ambiguous_p)
22236 {
22237 const char *str;
22238 char *spaces = NULL;
22239 if (!(complain & tf_error))
22240 return error_mark_node;
22241 if (TYPE_P (target))
22242 error ("ambiguous template instantiation for %q#T", target);
22243 else
22244 error ("ambiguous template instantiation for %q#D", target);
22245 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22246 for (t = list; t; t = TREE_CHAIN (t))
22247 {
22248 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22249 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22250 "%s %#qS", spaces ? spaces : str, subst);
22251 spaces = spaces ? spaces : get_spaces (str);
22252 }
22253 free (spaces);
22254 return error_mark_node;
22255 }
22256
22257 return champ;
22258 }
22259
22260 /* Explicitly instantiate DECL. */
22261
22262 void
22263 do_decl_instantiation (tree decl, tree storage)
22264 {
22265 tree result = NULL_TREE;
22266 int extern_p = 0;
22267
22268 if (!decl || decl == error_mark_node)
22269 /* An error occurred, for which grokdeclarator has already issued
22270 an appropriate message. */
22271 return;
22272 else if (! DECL_LANG_SPECIFIC (decl))
22273 {
22274 error ("explicit instantiation of non-template %q#D", decl);
22275 return;
22276 }
22277
22278 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22279 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22280
22281 if (VAR_P (decl) && !var_templ)
22282 {
22283 /* There is an asymmetry here in the way VAR_DECLs and
22284 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22285 the latter, the DECL we get back will be marked as a
22286 template instantiation, and the appropriate
22287 DECL_TEMPLATE_INFO will be set up. This does not happen for
22288 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22289 should handle VAR_DECLs as it currently handles
22290 FUNCTION_DECLs. */
22291 if (!DECL_CLASS_SCOPE_P (decl))
22292 {
22293 error ("%qD is not a static data member of a class template", decl);
22294 return;
22295 }
22296 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22297 if (!result || !VAR_P (result))
22298 {
22299 error ("no matching template for %qD found", decl);
22300 return;
22301 }
22302 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22303 {
22304 error ("type %qT for explicit instantiation %qD does not match "
22305 "declared type %qT", TREE_TYPE (result), decl,
22306 TREE_TYPE (decl));
22307 return;
22308 }
22309 }
22310 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22311 {
22312 error ("explicit instantiation of %q#D", decl);
22313 return;
22314 }
22315 else
22316 result = decl;
22317
22318 /* Check for various error cases. Note that if the explicit
22319 instantiation is valid the RESULT will currently be marked as an
22320 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22321 until we get here. */
22322
22323 if (DECL_TEMPLATE_SPECIALIZATION (result))
22324 {
22325 /* DR 259 [temp.spec].
22326
22327 Both an explicit instantiation and a declaration of an explicit
22328 specialization shall not appear in a program unless the explicit
22329 instantiation follows a declaration of the explicit specialization.
22330
22331 For a given set of template parameters, if an explicit
22332 instantiation of a template appears after a declaration of an
22333 explicit specialization for that template, the explicit
22334 instantiation has no effect. */
22335 return;
22336 }
22337 else if (DECL_EXPLICIT_INSTANTIATION (result))
22338 {
22339 /* [temp.spec]
22340
22341 No program shall explicitly instantiate any template more
22342 than once.
22343
22344 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22345 the first instantiation was `extern' and the second is not,
22346 and EXTERN_P for the opposite case. */
22347 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22348 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22349 /* If an "extern" explicit instantiation follows an ordinary
22350 explicit instantiation, the template is instantiated. */
22351 if (extern_p)
22352 return;
22353 }
22354 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22355 {
22356 error ("no matching template for %qD found", result);
22357 return;
22358 }
22359 else if (!DECL_TEMPLATE_INFO (result))
22360 {
22361 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22362 return;
22363 }
22364
22365 if (storage == NULL_TREE)
22366 ;
22367 else if (storage == ridpointers[(int) RID_EXTERN])
22368 {
22369 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22370 pedwarn (input_location, OPT_Wpedantic,
22371 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22372 "instantiations");
22373 extern_p = 1;
22374 }
22375 else
22376 error ("storage class %qD applied to template instantiation", storage);
22377
22378 check_explicit_instantiation_namespace (result);
22379 mark_decl_instantiated (result, extern_p);
22380 if (! extern_p)
22381 instantiate_decl (result, /*defer_ok=*/true,
22382 /*expl_inst_class_mem_p=*/false);
22383 }
22384
22385 static void
22386 mark_class_instantiated (tree t, int extern_p)
22387 {
22388 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22389 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22390 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22391 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22392 if (! extern_p)
22393 {
22394 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22395 rest_of_type_compilation (t, 1);
22396 }
22397 }
22398
22399 /* Called from do_type_instantiation through binding_table_foreach to
22400 do recursive instantiation for the type bound in ENTRY. */
22401 static void
22402 bt_instantiate_type_proc (binding_entry entry, void *data)
22403 {
22404 tree storage = *(tree *) data;
22405
22406 if (MAYBE_CLASS_TYPE_P (entry->type)
22407 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22408 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22409 }
22410
22411 /* Perform an explicit instantiation of template class T. STORAGE, if
22412 non-null, is the RID for extern, inline or static. COMPLAIN is
22413 nonzero if this is called from the parser, zero if called recursively,
22414 since the standard is unclear (as detailed below). */
22415
22416 void
22417 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22418 {
22419 int extern_p = 0;
22420 int nomem_p = 0;
22421 int static_p = 0;
22422 int previous_instantiation_extern_p = 0;
22423
22424 if (TREE_CODE (t) == TYPE_DECL)
22425 t = TREE_TYPE (t);
22426
22427 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22428 {
22429 tree tmpl =
22430 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22431 if (tmpl)
22432 error ("explicit instantiation of non-class template %qD", tmpl);
22433 else
22434 error ("explicit instantiation of non-template type %qT", t);
22435 return;
22436 }
22437
22438 complete_type (t);
22439
22440 if (!COMPLETE_TYPE_P (t))
22441 {
22442 if (complain & tf_error)
22443 error ("explicit instantiation of %q#T before definition of template",
22444 t);
22445 return;
22446 }
22447
22448 if (storage != NULL_TREE)
22449 {
22450 if (!in_system_header_at (input_location))
22451 {
22452 if (storage == ridpointers[(int) RID_EXTERN])
22453 {
22454 if (cxx_dialect == cxx98)
22455 pedwarn (input_location, OPT_Wpedantic,
22456 "ISO C++ 1998 forbids the use of %<extern%> on "
22457 "explicit instantiations");
22458 }
22459 else
22460 pedwarn (input_location, OPT_Wpedantic,
22461 "ISO C++ forbids the use of %qE"
22462 " on explicit instantiations", storage);
22463 }
22464
22465 if (storage == ridpointers[(int) RID_INLINE])
22466 nomem_p = 1;
22467 else if (storage == ridpointers[(int) RID_EXTERN])
22468 extern_p = 1;
22469 else if (storage == ridpointers[(int) RID_STATIC])
22470 static_p = 1;
22471 else
22472 {
22473 error ("storage class %qD applied to template instantiation",
22474 storage);
22475 extern_p = 0;
22476 }
22477 }
22478
22479 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22480 {
22481 /* DR 259 [temp.spec].
22482
22483 Both an explicit instantiation and a declaration of an explicit
22484 specialization shall not appear in a program unless the explicit
22485 instantiation follows a declaration of the explicit specialization.
22486
22487 For a given set of template parameters, if an explicit
22488 instantiation of a template appears after a declaration of an
22489 explicit specialization for that template, the explicit
22490 instantiation has no effect. */
22491 return;
22492 }
22493 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22494 {
22495 /* [temp.spec]
22496
22497 No program shall explicitly instantiate any template more
22498 than once.
22499
22500 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22501 instantiation was `extern'. If EXTERN_P then the second is.
22502 These cases are OK. */
22503 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22504
22505 if (!previous_instantiation_extern_p && !extern_p
22506 && (complain & tf_error))
22507 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22508
22509 /* If we've already instantiated the template, just return now. */
22510 if (!CLASSTYPE_INTERFACE_ONLY (t))
22511 return;
22512 }
22513
22514 check_explicit_instantiation_namespace (TYPE_NAME (t));
22515 mark_class_instantiated (t, extern_p);
22516
22517 if (nomem_p)
22518 return;
22519
22520 /* In contrast to implicit instantiation, where only the
22521 declarations, and not the definitions, of members are
22522 instantiated, we have here:
22523
22524 [temp.explicit]
22525
22526 The explicit instantiation of a class template specialization
22527 implies the instantiation of all of its members not
22528 previously explicitly specialized in the translation unit
22529 containing the explicit instantiation.
22530
22531 Of course, we can't instantiate member template classes, since we
22532 don't have any arguments for them. Note that the standard is
22533 unclear on whether the instantiation of the members are
22534 *explicit* instantiations or not. However, the most natural
22535 interpretation is that it should be an explicit
22536 instantiation. */
22537 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22538 if ((VAR_P (fld)
22539 || (TREE_CODE (fld) == FUNCTION_DECL
22540 && !static_p
22541 && user_provided_p (fld)))
22542 && DECL_TEMPLATE_INSTANTIATION (fld))
22543 {
22544 mark_decl_instantiated (fld, extern_p);
22545 if (! extern_p)
22546 instantiate_decl (fld, /*defer_ok=*/true,
22547 /*expl_inst_class_mem_p=*/true);
22548 }
22549
22550 if (CLASSTYPE_NESTED_UTDS (t))
22551 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22552 bt_instantiate_type_proc, &storage);
22553 }
22554
22555 /* Given a function DECL, which is a specialization of TMPL, modify
22556 DECL to be a re-instantiation of TMPL with the same template
22557 arguments. TMPL should be the template into which tsubst'ing
22558 should occur for DECL, not the most general template.
22559
22560 One reason for doing this is a scenario like this:
22561
22562 template <class T>
22563 void f(const T&, int i);
22564
22565 void g() { f(3, 7); }
22566
22567 template <class T>
22568 void f(const T& t, const int i) { }
22569
22570 Note that when the template is first instantiated, with
22571 instantiate_template, the resulting DECL will have no name for the
22572 first parameter, and the wrong type for the second. So, when we go
22573 to instantiate the DECL, we regenerate it. */
22574
22575 static void
22576 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22577 {
22578 /* The arguments used to instantiate DECL, from the most general
22579 template. */
22580 tree code_pattern;
22581
22582 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22583
22584 /* Make sure that we can see identifiers, and compute access
22585 correctly. */
22586 push_access_scope (decl);
22587
22588 if (TREE_CODE (decl) == FUNCTION_DECL)
22589 {
22590 tree decl_parm;
22591 tree pattern_parm;
22592 tree specs;
22593 int args_depth;
22594 int parms_depth;
22595
22596 args_depth = TMPL_ARGS_DEPTH (args);
22597 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22598 if (args_depth > parms_depth)
22599 args = get_innermost_template_args (args, parms_depth);
22600
22601 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22602 args, tf_error, NULL_TREE,
22603 /*defer_ok*/false);
22604 if (specs && specs != error_mark_node)
22605 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22606 specs);
22607
22608 /* Merge parameter declarations. */
22609 decl_parm = skip_artificial_parms_for (decl,
22610 DECL_ARGUMENTS (decl));
22611 pattern_parm
22612 = skip_artificial_parms_for (code_pattern,
22613 DECL_ARGUMENTS (code_pattern));
22614 while (decl_parm && !DECL_PACK_P (pattern_parm))
22615 {
22616 tree parm_type;
22617 tree attributes;
22618
22619 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22620 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22621 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22622 NULL_TREE);
22623 parm_type = type_decays_to (parm_type);
22624 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22625 TREE_TYPE (decl_parm) = parm_type;
22626 attributes = DECL_ATTRIBUTES (pattern_parm);
22627 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22628 {
22629 DECL_ATTRIBUTES (decl_parm) = attributes;
22630 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22631 }
22632 decl_parm = DECL_CHAIN (decl_parm);
22633 pattern_parm = DECL_CHAIN (pattern_parm);
22634 }
22635 /* Merge any parameters that match with the function parameter
22636 pack. */
22637 if (pattern_parm && DECL_PACK_P (pattern_parm))
22638 {
22639 int i, len;
22640 tree expanded_types;
22641 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22642 the parameters in this function parameter pack. */
22643 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22644 args, tf_error, NULL_TREE);
22645 len = TREE_VEC_LENGTH (expanded_types);
22646 for (i = 0; i < len; i++)
22647 {
22648 tree parm_type;
22649 tree attributes;
22650
22651 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22652 /* Rename the parameter to include the index. */
22653 DECL_NAME (decl_parm) =
22654 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22655 parm_type = TREE_VEC_ELT (expanded_types, i);
22656 parm_type = type_decays_to (parm_type);
22657 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22658 TREE_TYPE (decl_parm) = parm_type;
22659 attributes = DECL_ATTRIBUTES (pattern_parm);
22660 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22661 {
22662 DECL_ATTRIBUTES (decl_parm) = attributes;
22663 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22664 }
22665 decl_parm = DECL_CHAIN (decl_parm);
22666 }
22667 }
22668 /* Merge additional specifiers from the CODE_PATTERN. */
22669 if (DECL_DECLARED_INLINE_P (code_pattern)
22670 && !DECL_DECLARED_INLINE_P (decl))
22671 DECL_DECLARED_INLINE_P (decl) = 1;
22672 }
22673 else if (VAR_P (decl))
22674 {
22675 start_lambda_scope (decl);
22676 DECL_INITIAL (decl) =
22677 tsubst_expr (DECL_INITIAL (code_pattern), args,
22678 tf_error, DECL_TI_TEMPLATE (decl),
22679 /*integral_constant_expression_p=*/false);
22680 finish_lambda_scope ();
22681 if (VAR_HAD_UNKNOWN_BOUND (decl))
22682 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22683 tf_error, DECL_TI_TEMPLATE (decl));
22684 }
22685 else
22686 gcc_unreachable ();
22687
22688 pop_access_scope (decl);
22689 }
22690
22691 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22692 substituted to get DECL. */
22693
22694 tree
22695 template_for_substitution (tree decl)
22696 {
22697 tree tmpl = DECL_TI_TEMPLATE (decl);
22698
22699 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22700 for the instantiation. This is not always the most general
22701 template. Consider, for example:
22702
22703 template <class T>
22704 struct S { template <class U> void f();
22705 template <> void f<int>(); };
22706
22707 and an instantiation of S<double>::f<int>. We want TD to be the
22708 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22709 while (/* An instantiation cannot have a definition, so we need a
22710 more general template. */
22711 DECL_TEMPLATE_INSTANTIATION (tmpl)
22712 /* We must also deal with friend templates. Given:
22713
22714 template <class T> struct S {
22715 template <class U> friend void f() {};
22716 };
22717
22718 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22719 so far as the language is concerned, but that's still
22720 where we get the pattern for the instantiation from. On
22721 other hand, if the definition comes outside the class, say:
22722
22723 template <class T> struct S {
22724 template <class U> friend void f();
22725 };
22726 template <class U> friend void f() {}
22727
22728 we don't need to look any further. That's what the check for
22729 DECL_INITIAL is for. */
22730 || (TREE_CODE (decl) == FUNCTION_DECL
22731 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22732 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22733 {
22734 /* The present template, TD, should not be a definition. If it
22735 were a definition, we should be using it! Note that we
22736 cannot restructure the loop to just keep going until we find
22737 a template with a definition, since that might go too far if
22738 a specialization was declared, but not defined. */
22739
22740 /* Fetch the more general template. */
22741 tmpl = DECL_TI_TEMPLATE (tmpl);
22742 }
22743
22744 return tmpl;
22745 }
22746
22747 /* Returns true if we need to instantiate this template instance even if we
22748 know we aren't going to emit it. */
22749
22750 bool
22751 always_instantiate_p (tree decl)
22752 {
22753 /* We always instantiate inline functions so that we can inline them. An
22754 explicit instantiation declaration prohibits implicit instantiation of
22755 non-inline functions. With high levels of optimization, we would
22756 normally inline non-inline functions -- but we're not allowed to do
22757 that for "extern template" functions. Therefore, we check
22758 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22759 return ((TREE_CODE (decl) == FUNCTION_DECL
22760 && (DECL_DECLARED_INLINE_P (decl)
22761 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22762 /* And we need to instantiate static data members so that
22763 their initializers are available in integral constant
22764 expressions. */
22765 || (VAR_P (decl)
22766 && decl_maybe_constant_var_p (decl)));
22767 }
22768
22769 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22770 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22771 error, true otherwise. */
22772
22773 bool
22774 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22775 {
22776 tree fntype, spec, noex, clone;
22777
22778 /* Don't instantiate a noexcept-specification from template context. */
22779 if (processing_template_decl)
22780 return true;
22781
22782 if (DECL_CLONED_FUNCTION_P (fn))
22783 fn = DECL_CLONED_FUNCTION (fn);
22784 fntype = TREE_TYPE (fn);
22785 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22786
22787 if (!spec || !TREE_PURPOSE (spec))
22788 return true;
22789
22790 noex = TREE_PURPOSE (spec);
22791
22792 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22793 {
22794 static hash_set<tree>* fns = new hash_set<tree>;
22795 bool added = false;
22796 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22797 spec = get_defaulted_eh_spec (fn, complain);
22798 else if (!(added = !fns->add (fn)))
22799 {
22800 /* If hash_set::add returns true, the element was already there. */
22801 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22802 DECL_SOURCE_LOCATION (fn));
22803 error_at (loc,
22804 "exception specification of %qD depends on itself",
22805 fn);
22806 spec = noexcept_false_spec;
22807 }
22808 else if (push_tinst_level (fn))
22809 {
22810 push_access_scope (fn);
22811 push_deferring_access_checks (dk_no_deferred);
22812 input_location = DECL_SOURCE_LOCATION (fn);
22813 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22814 DEFERRED_NOEXCEPT_ARGS (noex),
22815 tf_warning_or_error, fn,
22816 /*function_p=*/false,
22817 /*integral_constant_expression_p=*/true);
22818 pop_deferring_access_checks ();
22819 pop_access_scope (fn);
22820 pop_tinst_level ();
22821 spec = build_noexcept_spec (noex, tf_warning_or_error);
22822 if (spec == error_mark_node)
22823 spec = noexcept_false_spec;
22824 }
22825 else
22826 spec = noexcept_false_spec;
22827
22828 if (added)
22829 fns->remove (fn);
22830
22831 if (spec == error_mark_node)
22832 return false;
22833
22834 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22835 }
22836
22837 FOR_EACH_CLONE (clone, fn)
22838 {
22839 if (TREE_TYPE (clone) == fntype)
22840 TREE_TYPE (clone) = TREE_TYPE (fn);
22841 else
22842 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22843 }
22844
22845 return true;
22846 }
22847
22848 /* We're starting to process the function INST, an instantiation of PATTERN;
22849 add their parameters to local_specializations. */
22850
22851 static void
22852 register_parameter_specializations (tree pattern, tree inst)
22853 {
22854 tree tmpl_parm = DECL_ARGUMENTS (pattern);
22855 tree spec_parm = DECL_ARGUMENTS (inst);
22856 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
22857 {
22858 register_local_specialization (spec_parm, tmpl_parm);
22859 spec_parm = skip_artificial_parms_for (inst, spec_parm);
22860 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
22861 }
22862 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22863 {
22864 if (!DECL_PACK_P (tmpl_parm))
22865 {
22866 register_local_specialization (spec_parm, tmpl_parm);
22867 spec_parm = DECL_CHAIN (spec_parm);
22868 }
22869 else
22870 {
22871 /* Register the (value) argument pack as a specialization of
22872 TMPL_PARM, then move on. */
22873 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22874 register_local_specialization (argpack, tmpl_parm);
22875 }
22876 }
22877 gcc_assert (!spec_parm);
22878 }
22879
22880 /* Produce the definition of D, a _DECL generated from a template. If
22881 DEFER_OK is true, then we don't have to actually do the
22882 instantiation now; we just have to do it sometime. Normally it is
22883 an error if this is an explicit instantiation but D is undefined.
22884 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22885 instantiated class template. */
22886
22887 tree
22888 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22889 {
22890 tree tmpl = DECL_TI_TEMPLATE (d);
22891 tree gen_args;
22892 tree args;
22893 tree td;
22894 tree code_pattern;
22895 tree spec;
22896 tree gen_tmpl;
22897 bool pattern_defined;
22898 location_t saved_loc = input_location;
22899 int saved_unevaluated_operand = cp_unevaluated_operand;
22900 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22901 bool external_p;
22902 bool deleted_p;
22903
22904 /* This function should only be used to instantiate templates for
22905 functions and static member variables. */
22906 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22907
22908 /* A concept is never instantiated. */
22909 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22910
22911 /* Variables are never deferred; if instantiation is required, they
22912 are instantiated right away. That allows for better code in the
22913 case that an expression refers to the value of the variable --
22914 if the variable has a constant value the referring expression can
22915 take advantage of that fact. */
22916 if (VAR_P (d))
22917 defer_ok = false;
22918
22919 /* Don't instantiate cloned functions. Instead, instantiate the
22920 functions they cloned. */
22921 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22922 d = DECL_CLONED_FUNCTION (d);
22923
22924 if (DECL_TEMPLATE_INSTANTIATED (d)
22925 || (TREE_CODE (d) == FUNCTION_DECL
22926 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22927 || DECL_TEMPLATE_SPECIALIZATION (d))
22928 /* D has already been instantiated or explicitly specialized, so
22929 there's nothing for us to do here.
22930
22931 It might seem reasonable to check whether or not D is an explicit
22932 instantiation, and, if so, stop here. But when an explicit
22933 instantiation is deferred until the end of the compilation,
22934 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22935 the instantiation. */
22936 return d;
22937
22938 /* Check to see whether we know that this template will be
22939 instantiated in some other file, as with "extern template"
22940 extension. */
22941 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22942
22943 /* In general, we do not instantiate such templates. */
22944 if (external_p && !always_instantiate_p (d))
22945 return d;
22946
22947 gen_tmpl = most_general_template (tmpl);
22948 gen_args = DECL_TI_ARGS (d);
22949
22950 if (tmpl != gen_tmpl)
22951 /* We should already have the extra args. */
22952 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22953 == TMPL_ARGS_DEPTH (gen_args));
22954 /* And what's in the hash table should match D. */
22955 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22956 || spec == NULL_TREE);
22957
22958 /* This needs to happen before any tsubsting. */
22959 if (! push_tinst_level (d))
22960 return d;
22961
22962 timevar_push (TV_TEMPLATE_INST);
22963
22964 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22965 for the instantiation. */
22966 td = template_for_substitution (d);
22967 args = gen_args;
22968
22969 if (VAR_P (d))
22970 {
22971 /* Look up an explicit specialization, if any. */
22972 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22973 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22974 if (elt && elt != error_mark_node)
22975 {
22976 td = TREE_VALUE (elt);
22977 args = TREE_PURPOSE (elt);
22978 }
22979 }
22980
22981 code_pattern = DECL_TEMPLATE_RESULT (td);
22982
22983 /* We should never be trying to instantiate a member of a class
22984 template or partial specialization. */
22985 gcc_assert (d != code_pattern);
22986
22987 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22988 || DECL_TEMPLATE_SPECIALIZATION (td))
22989 /* In the case of a friend template whose definition is provided
22990 outside the class, we may have too many arguments. Drop the
22991 ones we don't need. The same is true for specializations. */
22992 args = get_innermost_template_args
22993 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22994
22995 if (TREE_CODE (d) == FUNCTION_DECL)
22996 {
22997 deleted_p = DECL_DELETED_FN (code_pattern);
22998 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22999 && DECL_INITIAL (code_pattern) != error_mark_node)
23000 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23001 || deleted_p);
23002 }
23003 else
23004 {
23005 deleted_p = false;
23006 if (DECL_CLASS_SCOPE_P (code_pattern))
23007 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23008 || DECL_INLINE_VAR_P (code_pattern));
23009 else
23010 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23011 }
23012
23013 /* We may be in the middle of deferred access check. Disable it now. */
23014 push_deferring_access_checks (dk_no_deferred);
23015
23016 /* Unless an explicit instantiation directive has already determined
23017 the linkage of D, remember that a definition is available for
23018 this entity. */
23019 if (pattern_defined
23020 && !DECL_INTERFACE_KNOWN (d)
23021 && !DECL_NOT_REALLY_EXTERN (d))
23022 mark_definable (d);
23023
23024 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23025 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23026 input_location = DECL_SOURCE_LOCATION (d);
23027
23028 /* If D is a member of an explicitly instantiated class template,
23029 and no definition is available, treat it like an implicit
23030 instantiation. */
23031 if (!pattern_defined && expl_inst_class_mem_p
23032 && DECL_EXPLICIT_INSTANTIATION (d))
23033 {
23034 /* Leave linkage flags alone on instantiations with anonymous
23035 visibility. */
23036 if (TREE_PUBLIC (d))
23037 {
23038 DECL_NOT_REALLY_EXTERN (d) = 0;
23039 DECL_INTERFACE_KNOWN (d) = 0;
23040 }
23041 SET_DECL_IMPLICIT_INSTANTIATION (d);
23042 }
23043
23044 /* Defer all other templates, unless we have been explicitly
23045 forbidden from doing so. */
23046 if (/* If there is no definition, we cannot instantiate the
23047 template. */
23048 ! pattern_defined
23049 /* If it's OK to postpone instantiation, do so. */
23050 || defer_ok
23051 /* If this is a static data member that will be defined
23052 elsewhere, we don't want to instantiate the entire data
23053 member, but we do want to instantiate the initializer so that
23054 we can substitute that elsewhere. */
23055 || (external_p && VAR_P (d))
23056 /* Handle here a deleted function too, avoid generating
23057 its body (c++/61080). */
23058 || deleted_p)
23059 {
23060 /* The definition of the static data member is now required so
23061 we must substitute the initializer. */
23062 if (VAR_P (d)
23063 && !DECL_INITIAL (d)
23064 && DECL_INITIAL (code_pattern))
23065 {
23066 tree ns;
23067 tree init;
23068 bool const_init = false;
23069 bool enter_context = DECL_CLASS_SCOPE_P (d);
23070
23071 ns = decl_namespace_context (d);
23072 push_nested_namespace (ns);
23073 if (enter_context)
23074 push_nested_class (DECL_CONTEXT (d));
23075 init = tsubst_expr (DECL_INITIAL (code_pattern),
23076 args,
23077 tf_warning_or_error, NULL_TREE,
23078 /*integral_constant_expression_p=*/false);
23079 /* If instantiating the initializer involved instantiating this
23080 again, don't call cp_finish_decl twice. */
23081 if (!DECL_INITIAL (d))
23082 {
23083 /* Make sure the initializer is still constant, in case of
23084 circular dependency (template/instantiate6.C). */
23085 const_init
23086 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23087 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23088 /*asmspec_tree=*/NULL_TREE,
23089 LOOKUP_ONLYCONVERTING);
23090 }
23091 if (enter_context)
23092 pop_nested_class ();
23093 pop_nested_namespace (ns);
23094 }
23095
23096 /* We restore the source position here because it's used by
23097 add_pending_template. */
23098 input_location = saved_loc;
23099
23100 if (at_eof && !pattern_defined
23101 && DECL_EXPLICIT_INSTANTIATION (d)
23102 && DECL_NOT_REALLY_EXTERN (d))
23103 /* [temp.explicit]
23104
23105 The definition of a non-exported function template, a
23106 non-exported member function template, or a non-exported
23107 member function or static data member of a class template
23108 shall be present in every translation unit in which it is
23109 explicitly instantiated. */
23110 permerror (input_location, "explicit instantiation of %qD "
23111 "but no definition available", d);
23112
23113 /* If we're in unevaluated context, we just wanted to get the
23114 constant value; this isn't an odr use, so don't queue
23115 a full instantiation. */
23116 if (cp_unevaluated_operand != 0)
23117 goto out;
23118 /* ??? Historically, we have instantiated inline functions, even
23119 when marked as "extern template". */
23120 if (!(external_p && VAR_P (d)))
23121 add_pending_template (d);
23122 goto out;
23123 }
23124 /* Tell the repository that D is available in this translation unit
23125 -- and see if it is supposed to be instantiated here. */
23126 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23127 {
23128 /* In a PCH file, despite the fact that the repository hasn't
23129 requested instantiation in the PCH it is still possible that
23130 an instantiation will be required in a file that includes the
23131 PCH. */
23132 if (pch_file)
23133 add_pending_template (d);
23134 /* Instantiate inline functions so that the inliner can do its
23135 job, even though we'll not be emitting a copy of this
23136 function. */
23137 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23138 goto out;
23139 }
23140
23141 bool push_to_top, nested;
23142 tree fn_context;
23143 fn_context = decl_function_context (d);
23144 nested = current_function_decl != NULL_TREE;
23145 push_to_top = !(nested && fn_context == current_function_decl);
23146
23147 vec<tree> omp_privatization_save;
23148 if (nested)
23149 save_omp_privatization_clauses (omp_privatization_save);
23150
23151 if (push_to_top)
23152 push_to_top_level ();
23153 else
23154 {
23155 push_function_context ();
23156 cp_unevaluated_operand = 0;
23157 c_inhibit_evaluation_warnings = 0;
23158 }
23159
23160 /* Mark D as instantiated so that recursive calls to
23161 instantiate_decl do not try to instantiate it again. */
23162 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23163
23164 /* Regenerate the declaration in case the template has been modified
23165 by a subsequent redeclaration. */
23166 regenerate_decl_from_template (d, td, args);
23167
23168 /* We already set the file and line above. Reset them now in case
23169 they changed as a result of calling regenerate_decl_from_template. */
23170 input_location = DECL_SOURCE_LOCATION (d);
23171
23172 if (VAR_P (d))
23173 {
23174 tree init;
23175 bool const_init = false;
23176
23177 /* Clear out DECL_RTL; whatever was there before may not be right
23178 since we've reset the type of the declaration. */
23179 SET_DECL_RTL (d, NULL);
23180 DECL_IN_AGGR_P (d) = 0;
23181
23182 /* The initializer is placed in DECL_INITIAL by
23183 regenerate_decl_from_template so we don't need to
23184 push/pop_access_scope again here. Pull it out so that
23185 cp_finish_decl can process it. */
23186 init = DECL_INITIAL (d);
23187 DECL_INITIAL (d) = NULL_TREE;
23188 DECL_INITIALIZED_P (d) = 0;
23189
23190 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23191 initializer. That function will defer actual emission until
23192 we have a chance to determine linkage. */
23193 DECL_EXTERNAL (d) = 0;
23194
23195 /* Enter the scope of D so that access-checking works correctly. */
23196 bool enter_context = DECL_CLASS_SCOPE_P (d);
23197 if (enter_context)
23198 push_nested_class (DECL_CONTEXT (d));
23199
23200 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23201 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23202
23203 if (enter_context)
23204 pop_nested_class ();
23205
23206 if (variable_template_p (gen_tmpl))
23207 note_variable_template_instantiation (d);
23208 }
23209 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23210 synthesize_method (d);
23211 else if (TREE_CODE (d) == FUNCTION_DECL)
23212 {
23213 /* Set up the list of local specializations. */
23214 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23215 tree block = NULL_TREE;
23216
23217 /* Set up context. */
23218 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23219 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23220 block = push_stmt_list ();
23221 else
23222 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23223
23224 /* Some typedefs referenced from within the template code need to be
23225 access checked at template instantiation time, i.e now. These
23226 types were added to the template at parsing time. Let's get those
23227 and perform the access checks then. */
23228 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23229 args);
23230
23231 /* Create substitution entries for the parameters. */
23232 register_parameter_specializations (code_pattern, d);
23233
23234 /* Substitute into the body of the function. */
23235 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23236 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23237 tf_warning_or_error, tmpl);
23238 else
23239 {
23240 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23241 tf_warning_or_error, tmpl,
23242 /*integral_constant_expression_p=*/false);
23243
23244 /* Set the current input_location to the end of the function
23245 so that finish_function knows where we are. */
23246 input_location
23247 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23248
23249 /* Remember if we saw an infinite loop in the template. */
23250 current_function_infinite_loop
23251 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23252 }
23253
23254 /* Finish the function. */
23255 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23256 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23257 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23258 else
23259 {
23260 d = finish_function (/*inline_p=*/false);
23261 expand_or_defer_fn (d);
23262 }
23263
23264 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23265 cp_check_omp_declare_reduction (d);
23266 }
23267
23268 /* We're not deferring instantiation any more. */
23269 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23270
23271 if (push_to_top)
23272 pop_from_top_level ();
23273 else
23274 pop_function_context ();
23275
23276 if (nested)
23277 restore_omp_privatization_clauses (omp_privatization_save);
23278
23279 out:
23280 pop_deferring_access_checks ();
23281 timevar_pop (TV_TEMPLATE_INST);
23282 pop_tinst_level ();
23283 input_location = saved_loc;
23284 cp_unevaluated_operand = saved_unevaluated_operand;
23285 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23286
23287 return d;
23288 }
23289
23290 /* Run through the list of templates that we wish we could
23291 instantiate, and instantiate any we can. RETRIES is the
23292 number of times we retry pending template instantiation. */
23293
23294 void
23295 instantiate_pending_templates (int retries)
23296 {
23297 int reconsider;
23298 location_t saved_loc = input_location;
23299
23300 /* Instantiating templates may trigger vtable generation. This in turn
23301 may require further template instantiations. We place a limit here
23302 to avoid infinite loop. */
23303 if (pending_templates && retries >= max_tinst_depth)
23304 {
23305 tree decl = pending_templates->tinst->decl;
23306
23307 fatal_error (input_location,
23308 "template instantiation depth exceeds maximum of %d"
23309 " instantiating %q+D, possibly from virtual table generation"
23310 " (use -ftemplate-depth= to increase the maximum)",
23311 max_tinst_depth, decl);
23312 if (TREE_CODE (decl) == FUNCTION_DECL)
23313 /* Pretend that we defined it. */
23314 DECL_INITIAL (decl) = error_mark_node;
23315 return;
23316 }
23317
23318 do
23319 {
23320 struct pending_template **t = &pending_templates;
23321 struct pending_template *last = NULL;
23322 reconsider = 0;
23323 while (*t)
23324 {
23325 tree instantiation = reopen_tinst_level ((*t)->tinst);
23326 bool complete = false;
23327
23328 if (TYPE_P (instantiation))
23329 {
23330 if (!COMPLETE_TYPE_P (instantiation))
23331 {
23332 instantiate_class_template (instantiation);
23333 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23334 for (tree fld = TYPE_FIELDS (instantiation);
23335 fld; fld = TREE_CHAIN (fld))
23336 if ((VAR_P (fld)
23337 || (TREE_CODE (fld) == FUNCTION_DECL
23338 && !DECL_ARTIFICIAL (fld)))
23339 && DECL_TEMPLATE_INSTANTIATION (fld))
23340 instantiate_decl (fld,
23341 /*defer_ok=*/false,
23342 /*expl_inst_class_mem_p=*/false);
23343
23344 if (COMPLETE_TYPE_P (instantiation))
23345 reconsider = 1;
23346 }
23347
23348 complete = COMPLETE_TYPE_P (instantiation);
23349 }
23350 else
23351 {
23352 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23353 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23354 {
23355 instantiation
23356 = instantiate_decl (instantiation,
23357 /*defer_ok=*/false,
23358 /*expl_inst_class_mem_p=*/false);
23359 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23360 reconsider = 1;
23361 }
23362
23363 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23364 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23365 }
23366
23367 if (complete)
23368 /* If INSTANTIATION has been instantiated, then we don't
23369 need to consider it again in the future. */
23370 *t = (*t)->next;
23371 else
23372 {
23373 last = *t;
23374 t = &(*t)->next;
23375 }
23376 tinst_depth = 0;
23377 current_tinst_level = NULL;
23378 }
23379 last_pending_template = last;
23380 }
23381 while (reconsider);
23382
23383 input_location = saved_loc;
23384 }
23385
23386 /* Substitute ARGVEC into T, which is a list of initializers for
23387 either base class or a non-static data member. The TREE_PURPOSEs
23388 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23389 instantiate_decl. */
23390
23391 static tree
23392 tsubst_initializer_list (tree t, tree argvec)
23393 {
23394 tree inits = NULL_TREE;
23395
23396 for (; t; t = TREE_CHAIN (t))
23397 {
23398 tree decl;
23399 tree init;
23400 tree expanded_bases = NULL_TREE;
23401 tree expanded_arguments = NULL_TREE;
23402 int i, len = 1;
23403
23404 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23405 {
23406 tree expr;
23407 tree arg;
23408
23409 /* Expand the base class expansion type into separate base
23410 classes. */
23411 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23412 tf_warning_or_error,
23413 NULL_TREE);
23414 if (expanded_bases == error_mark_node)
23415 continue;
23416
23417 /* We'll be building separate TREE_LISTs of arguments for
23418 each base. */
23419 len = TREE_VEC_LENGTH (expanded_bases);
23420 expanded_arguments = make_tree_vec (len);
23421 for (i = 0; i < len; i++)
23422 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23423
23424 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23425 expand each argument in the TREE_VALUE of t. */
23426 expr = make_node (EXPR_PACK_EXPANSION);
23427 PACK_EXPANSION_LOCAL_P (expr) = true;
23428 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23429 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23430
23431 if (TREE_VALUE (t) == void_type_node)
23432 /* VOID_TYPE_NODE is used to indicate
23433 value-initialization. */
23434 {
23435 for (i = 0; i < len; i++)
23436 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23437 }
23438 else
23439 {
23440 /* Substitute parameter packs into each argument in the
23441 TREE_LIST. */
23442 in_base_initializer = 1;
23443 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23444 {
23445 tree expanded_exprs;
23446
23447 /* Expand the argument. */
23448 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23449 expanded_exprs
23450 = tsubst_pack_expansion (expr, argvec,
23451 tf_warning_or_error,
23452 NULL_TREE);
23453 if (expanded_exprs == error_mark_node)
23454 continue;
23455
23456 /* Prepend each of the expanded expressions to the
23457 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23458 for (i = 0; i < len; i++)
23459 {
23460 TREE_VEC_ELT (expanded_arguments, i) =
23461 tree_cons (NULL_TREE,
23462 TREE_VEC_ELT (expanded_exprs, i),
23463 TREE_VEC_ELT (expanded_arguments, i));
23464 }
23465 }
23466 in_base_initializer = 0;
23467
23468 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23469 since we built them backwards. */
23470 for (i = 0; i < len; i++)
23471 {
23472 TREE_VEC_ELT (expanded_arguments, i) =
23473 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23474 }
23475 }
23476 }
23477
23478 for (i = 0; i < len; ++i)
23479 {
23480 if (expanded_bases)
23481 {
23482 decl = TREE_VEC_ELT (expanded_bases, i);
23483 decl = expand_member_init (decl);
23484 init = TREE_VEC_ELT (expanded_arguments, i);
23485 }
23486 else
23487 {
23488 tree tmp;
23489 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23490 tf_warning_or_error, NULL_TREE);
23491
23492 decl = expand_member_init (decl);
23493 if (decl && !DECL_P (decl))
23494 in_base_initializer = 1;
23495
23496 init = TREE_VALUE (t);
23497 tmp = init;
23498 if (init != void_type_node)
23499 init = tsubst_expr (init, argvec,
23500 tf_warning_or_error, NULL_TREE,
23501 /*integral_constant_expression_p=*/false);
23502 if (init == NULL_TREE && tmp != NULL_TREE)
23503 /* If we had an initializer but it instantiated to nothing,
23504 value-initialize the object. This will only occur when
23505 the initializer was a pack expansion where the parameter
23506 packs used in that expansion were of length zero. */
23507 init = void_type_node;
23508 in_base_initializer = 0;
23509 }
23510
23511 if (decl)
23512 {
23513 init = build_tree_list (decl, init);
23514 TREE_CHAIN (init) = inits;
23515 inits = init;
23516 }
23517 }
23518 }
23519 return inits;
23520 }
23521
23522 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23523
23524 static void
23525 set_current_access_from_decl (tree decl)
23526 {
23527 if (TREE_PRIVATE (decl))
23528 current_access_specifier = access_private_node;
23529 else if (TREE_PROTECTED (decl))
23530 current_access_specifier = access_protected_node;
23531 else
23532 current_access_specifier = access_public_node;
23533 }
23534
23535 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23536 is the instantiation (which should have been created with
23537 start_enum) and ARGS are the template arguments to use. */
23538
23539 static void
23540 tsubst_enum (tree tag, tree newtag, tree args)
23541 {
23542 tree e;
23543
23544 if (SCOPED_ENUM_P (newtag))
23545 begin_scope (sk_scoped_enum, newtag);
23546
23547 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23548 {
23549 tree value;
23550 tree decl;
23551
23552 decl = TREE_VALUE (e);
23553 /* Note that in a template enum, the TREE_VALUE is the
23554 CONST_DECL, not the corresponding INTEGER_CST. */
23555 value = tsubst_expr (DECL_INITIAL (decl),
23556 args, tf_warning_or_error, NULL_TREE,
23557 /*integral_constant_expression_p=*/true);
23558
23559 /* Give this enumeration constant the correct access. */
23560 set_current_access_from_decl (decl);
23561
23562 /* Actually build the enumerator itself. Here we're assuming that
23563 enumerators can't have dependent attributes. */
23564 build_enumerator (DECL_NAME (decl), value, newtag,
23565 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23566 }
23567
23568 if (SCOPED_ENUM_P (newtag))
23569 finish_scope ();
23570
23571 finish_enum_value_list (newtag);
23572 finish_enum (newtag);
23573
23574 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23575 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23576 }
23577
23578 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23579 its type -- but without substituting the innermost set of template
23580 arguments. So, innermost set of template parameters will appear in
23581 the type. */
23582
23583 tree
23584 get_mostly_instantiated_function_type (tree decl)
23585 {
23586 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23587 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23588 }
23589
23590 /* Return truthvalue if we're processing a template different from
23591 the last one involved in diagnostics. */
23592 bool
23593 problematic_instantiation_changed (void)
23594 {
23595 return current_tinst_level != last_error_tinst_level;
23596 }
23597
23598 /* Remember current template involved in diagnostics. */
23599 void
23600 record_last_problematic_instantiation (void)
23601 {
23602 last_error_tinst_level = current_tinst_level;
23603 }
23604
23605 struct tinst_level *
23606 current_instantiation (void)
23607 {
23608 return current_tinst_level;
23609 }
23610
23611 /* Return TRUE if current_function_decl is being instantiated, false
23612 otherwise. */
23613
23614 bool
23615 instantiating_current_function_p (void)
23616 {
23617 return (current_instantiation ()
23618 && current_instantiation ()->decl == current_function_decl);
23619 }
23620
23621 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23622 type. Return false for ok, true for disallowed. Issue error and
23623 inform messages under control of COMPLAIN. */
23624
23625 static bool
23626 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23627 {
23628 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23629 return false;
23630 else if (POINTER_TYPE_P (type))
23631 return false;
23632 else if (TYPE_PTRMEM_P (type))
23633 return false;
23634 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23635 return false;
23636 else if (TREE_CODE (type) == TYPENAME_TYPE)
23637 return false;
23638 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23639 return false;
23640 else if (TREE_CODE (type) == NULLPTR_TYPE)
23641 return false;
23642 /* A bound template template parm could later be instantiated to have a valid
23643 nontype parm type via an alias template. */
23644 else if (cxx_dialect >= cxx11
23645 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23646 return false;
23647
23648 if (complain & tf_error)
23649 {
23650 if (type == error_mark_node)
23651 inform (input_location, "invalid template non-type parameter");
23652 else
23653 error ("%q#T is not a valid type for a template non-type parameter",
23654 type);
23655 }
23656 return true;
23657 }
23658
23659 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23660 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23661
23662 static bool
23663 dependent_type_p_r (tree type)
23664 {
23665 tree scope;
23666
23667 /* [temp.dep.type]
23668
23669 A type is dependent if it is:
23670
23671 -- a template parameter. Template template parameters are types
23672 for us (since TYPE_P holds true for them) so we handle
23673 them here. */
23674 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23675 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23676 return true;
23677 /* -- a qualified-id with a nested-name-specifier which contains a
23678 class-name that names a dependent type or whose unqualified-id
23679 names a dependent type. */
23680 if (TREE_CODE (type) == TYPENAME_TYPE)
23681 return true;
23682
23683 /* An alias template specialization can be dependent even if the
23684 resulting type is not. */
23685 if (dependent_alias_template_spec_p (type))
23686 return true;
23687
23688 /* -- a cv-qualified type where the cv-unqualified type is
23689 dependent.
23690 No code is necessary for this bullet; the code below handles
23691 cv-qualified types, and we don't want to strip aliases with
23692 TYPE_MAIN_VARIANT because of DR 1558. */
23693 /* -- a compound type constructed from any dependent type. */
23694 if (TYPE_PTRMEM_P (type))
23695 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23696 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23697 (type)));
23698 else if (TYPE_PTR_P (type)
23699 || TREE_CODE (type) == REFERENCE_TYPE)
23700 return dependent_type_p (TREE_TYPE (type));
23701 else if (TREE_CODE (type) == FUNCTION_TYPE
23702 || TREE_CODE (type) == METHOD_TYPE)
23703 {
23704 tree arg_type;
23705
23706 if (dependent_type_p (TREE_TYPE (type)))
23707 return true;
23708 for (arg_type = TYPE_ARG_TYPES (type);
23709 arg_type;
23710 arg_type = TREE_CHAIN (arg_type))
23711 if (dependent_type_p (TREE_VALUE (arg_type)))
23712 return true;
23713 if (cxx_dialect >= cxx17)
23714 {
23715 /* A value-dependent noexcept-specifier makes the type dependent. */
23716 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23717 if (spec && TREE_PURPOSE (spec)
23718 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23719 return true;
23720 }
23721 return false;
23722 }
23723 /* -- an array type constructed from any dependent type or whose
23724 size is specified by a constant expression that is
23725 value-dependent.
23726
23727 We checked for type- and value-dependence of the bounds in
23728 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23729 if (TREE_CODE (type) == ARRAY_TYPE)
23730 {
23731 if (TYPE_DOMAIN (type)
23732 && dependent_type_p (TYPE_DOMAIN (type)))
23733 return true;
23734 return dependent_type_p (TREE_TYPE (type));
23735 }
23736
23737 /* -- a template-id in which either the template name is a template
23738 parameter ... */
23739 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23740 return true;
23741 /* ... or any of the template arguments is a dependent type or
23742 an expression that is type-dependent or value-dependent. */
23743 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23744 && (any_dependent_template_arguments_p
23745 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23746 return true;
23747
23748 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23749 dependent; if the argument of the `typeof' expression is not
23750 type-dependent, then it should already been have resolved. */
23751 if (TREE_CODE (type) == TYPEOF_TYPE
23752 || TREE_CODE (type) == DECLTYPE_TYPE
23753 || TREE_CODE (type) == UNDERLYING_TYPE)
23754 return true;
23755
23756 /* A template argument pack is dependent if any of its packed
23757 arguments are. */
23758 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23759 {
23760 tree args = ARGUMENT_PACK_ARGS (type);
23761 int i, len = TREE_VEC_LENGTH (args);
23762 for (i = 0; i < len; ++i)
23763 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23764 return true;
23765 }
23766
23767 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23768 be template parameters. */
23769 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23770 return true;
23771
23772 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23773 return true;
23774
23775 /* The standard does not specifically mention types that are local
23776 to template functions or local classes, but they should be
23777 considered dependent too. For example:
23778
23779 template <int I> void f() {
23780 enum E { a = I };
23781 S<sizeof (E)> s;
23782 }
23783
23784 The size of `E' cannot be known until the value of `I' has been
23785 determined. Therefore, `E' must be considered dependent. */
23786 scope = TYPE_CONTEXT (type);
23787 if (scope && TYPE_P (scope))
23788 return dependent_type_p (scope);
23789 /* Don't use type_dependent_expression_p here, as it can lead
23790 to infinite recursion trying to determine whether a lambda
23791 nested in a lambda is dependent (c++/47687). */
23792 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23793 && DECL_LANG_SPECIFIC (scope)
23794 && DECL_TEMPLATE_INFO (scope)
23795 && (any_dependent_template_arguments_p
23796 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23797 return true;
23798
23799 /* Other types are non-dependent. */
23800 return false;
23801 }
23802
23803 /* Returns TRUE if TYPE is dependent, in the sense of
23804 [temp.dep.type]. Note that a NULL type is considered dependent. */
23805
23806 bool
23807 dependent_type_p (tree type)
23808 {
23809 /* If there are no template parameters in scope, then there can't be
23810 any dependent types. */
23811 if (!processing_template_decl)
23812 {
23813 /* If we are not processing a template, then nobody should be
23814 providing us with a dependent type. */
23815 gcc_assert (type);
23816 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23817 return false;
23818 }
23819
23820 /* If the type is NULL, we have not computed a type for the entity
23821 in question; in that case, the type is dependent. */
23822 if (!type)
23823 return true;
23824
23825 /* Erroneous types can be considered non-dependent. */
23826 if (type == error_mark_node)
23827 return false;
23828
23829 /* Getting here with global_type_node means we improperly called this
23830 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23831 gcc_checking_assert (type != global_type_node);
23832
23833 /* If we have not already computed the appropriate value for TYPE,
23834 do so now. */
23835 if (!TYPE_DEPENDENT_P_VALID (type))
23836 {
23837 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23838 TYPE_DEPENDENT_P_VALID (type) = 1;
23839 }
23840
23841 return TYPE_DEPENDENT_P (type);
23842 }
23843
23844 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23845 lookup. In other words, a dependent type that is not the current
23846 instantiation. */
23847
23848 bool
23849 dependent_scope_p (tree scope)
23850 {
23851 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23852 && !currently_open_class (scope));
23853 }
23854
23855 /* T is a SCOPE_REF; return whether we need to consider it
23856 instantiation-dependent so that we can check access at instantiation
23857 time even though we know which member it resolves to. */
23858
23859 static bool
23860 instantiation_dependent_scope_ref_p (tree t)
23861 {
23862 if (DECL_P (TREE_OPERAND (t, 1))
23863 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23864 && accessible_in_template_p (TREE_OPERAND (t, 0),
23865 TREE_OPERAND (t, 1)))
23866 return false;
23867 else
23868 return true;
23869 }
23870
23871 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23872 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23873 expression. */
23874
23875 /* Note that this predicate is not appropriate for general expressions;
23876 only constant expressions (that satisfy potential_constant_expression)
23877 can be tested for value dependence. */
23878
23879 bool
23880 value_dependent_expression_p (tree expression)
23881 {
23882 if (!processing_template_decl || expression == NULL_TREE)
23883 return false;
23884
23885 /* A name declared with a dependent type. */
23886 if (DECL_P (expression) && type_dependent_expression_p (expression))
23887 return true;
23888
23889 switch (TREE_CODE (expression))
23890 {
23891 case BASELINK:
23892 /* A dependent member function of the current instantiation. */
23893 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23894
23895 case FUNCTION_DECL:
23896 /* A dependent member function of the current instantiation. */
23897 if (DECL_CLASS_SCOPE_P (expression)
23898 && dependent_type_p (DECL_CONTEXT (expression)))
23899 return true;
23900 break;
23901
23902 case IDENTIFIER_NODE:
23903 /* A name that has not been looked up -- must be dependent. */
23904 return true;
23905
23906 case TEMPLATE_PARM_INDEX:
23907 /* A non-type template parm. */
23908 return true;
23909
23910 case CONST_DECL:
23911 /* A non-type template parm. */
23912 if (DECL_TEMPLATE_PARM_P (expression))
23913 return true;
23914 return value_dependent_expression_p (DECL_INITIAL (expression));
23915
23916 case VAR_DECL:
23917 /* A constant with literal type and is initialized
23918 with an expression that is value-dependent.
23919
23920 Note that a non-dependent parenthesized initializer will have
23921 already been replaced with its constant value, so if we see
23922 a TREE_LIST it must be dependent. */
23923 if (DECL_INITIAL (expression)
23924 && decl_constant_var_p (expression)
23925 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23926 /* cp_finish_decl doesn't fold reference initializers. */
23927 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23928 || type_dependent_expression_p (DECL_INITIAL (expression))
23929 || value_dependent_expression_p (DECL_INITIAL (expression))))
23930 return true;
23931 if (DECL_HAS_VALUE_EXPR_P (expression))
23932 {
23933 tree value_expr = DECL_VALUE_EXPR (expression);
23934 if (type_dependent_expression_p (value_expr))
23935 return true;
23936 }
23937 return false;
23938
23939 case DYNAMIC_CAST_EXPR:
23940 case STATIC_CAST_EXPR:
23941 case CONST_CAST_EXPR:
23942 case REINTERPRET_CAST_EXPR:
23943 case CAST_EXPR:
23944 /* These expressions are value-dependent if the type to which
23945 the cast occurs is dependent or the expression being casted
23946 is value-dependent. */
23947 {
23948 tree type = TREE_TYPE (expression);
23949
23950 if (dependent_type_p (type))
23951 return true;
23952
23953 /* A functional cast has a list of operands. */
23954 expression = TREE_OPERAND (expression, 0);
23955 if (!expression)
23956 {
23957 /* If there are no operands, it must be an expression such
23958 as "int()". This should not happen for aggregate types
23959 because it would form non-constant expressions. */
23960 gcc_assert (cxx_dialect >= cxx11
23961 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23962
23963 return false;
23964 }
23965
23966 if (TREE_CODE (expression) == TREE_LIST)
23967 return any_value_dependent_elements_p (expression);
23968
23969 return value_dependent_expression_p (expression);
23970 }
23971
23972 case SIZEOF_EXPR:
23973 if (SIZEOF_EXPR_TYPE_P (expression))
23974 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23975 /* FALLTHRU */
23976 case ALIGNOF_EXPR:
23977 case TYPEID_EXPR:
23978 /* A `sizeof' expression is value-dependent if the operand is
23979 type-dependent or is a pack expansion. */
23980 expression = TREE_OPERAND (expression, 0);
23981 if (PACK_EXPANSION_P (expression))
23982 return true;
23983 else if (TYPE_P (expression))
23984 return dependent_type_p (expression);
23985 return instantiation_dependent_uneval_expression_p (expression);
23986
23987 case AT_ENCODE_EXPR:
23988 /* An 'encode' expression is value-dependent if the operand is
23989 type-dependent. */
23990 expression = TREE_OPERAND (expression, 0);
23991 return dependent_type_p (expression);
23992
23993 case NOEXCEPT_EXPR:
23994 expression = TREE_OPERAND (expression, 0);
23995 return instantiation_dependent_uneval_expression_p (expression);
23996
23997 case SCOPE_REF:
23998 /* All instantiation-dependent expressions should also be considered
23999 value-dependent. */
24000 return instantiation_dependent_scope_ref_p (expression);
24001
24002 case COMPONENT_REF:
24003 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24004 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24005
24006 case NONTYPE_ARGUMENT_PACK:
24007 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24008 is value-dependent. */
24009 {
24010 tree values = ARGUMENT_PACK_ARGS (expression);
24011 int i, len = TREE_VEC_LENGTH (values);
24012
24013 for (i = 0; i < len; ++i)
24014 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24015 return true;
24016
24017 return false;
24018 }
24019
24020 case TRAIT_EXPR:
24021 {
24022 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24023
24024 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24025 return true;
24026
24027 if (!type2)
24028 return false;
24029
24030 if (TREE_CODE (type2) != TREE_LIST)
24031 return dependent_type_p (type2);
24032
24033 for (; type2; type2 = TREE_CHAIN (type2))
24034 if (dependent_type_p (TREE_VALUE (type2)))
24035 return true;
24036
24037 return false;
24038 }
24039
24040 case MODOP_EXPR:
24041 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24042 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24043
24044 case ARRAY_REF:
24045 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24046 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24047
24048 case ADDR_EXPR:
24049 {
24050 tree op = TREE_OPERAND (expression, 0);
24051 return (value_dependent_expression_p (op)
24052 || has_value_dependent_address (op));
24053 }
24054
24055 case REQUIRES_EXPR:
24056 /* Treat all requires-expressions as value-dependent so
24057 we don't try to fold them. */
24058 return true;
24059
24060 case TYPE_REQ:
24061 return dependent_type_p (TREE_OPERAND (expression, 0));
24062
24063 case CALL_EXPR:
24064 {
24065 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24066 return true;
24067 tree fn = get_callee_fndecl (expression);
24068 int i, nargs;
24069 nargs = call_expr_nargs (expression);
24070 for (i = 0; i < nargs; ++i)
24071 {
24072 tree op = CALL_EXPR_ARG (expression, i);
24073 /* In a call to a constexpr member function, look through the
24074 implicit ADDR_EXPR on the object argument so that it doesn't
24075 cause the call to be considered value-dependent. We also
24076 look through it in potential_constant_expression. */
24077 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24078 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24079 && TREE_CODE (op) == ADDR_EXPR)
24080 op = TREE_OPERAND (op, 0);
24081 if (value_dependent_expression_p (op))
24082 return true;
24083 }
24084 return false;
24085 }
24086
24087 case TEMPLATE_ID_EXPR:
24088 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
24089 type-dependent. */
24090 return type_dependent_expression_p (expression)
24091 || variable_concept_p (TREE_OPERAND (expression, 0));
24092
24093 case CONSTRUCTOR:
24094 {
24095 unsigned ix;
24096 tree val;
24097 if (dependent_type_p (TREE_TYPE (expression)))
24098 return true;
24099 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24100 if (value_dependent_expression_p (val))
24101 return true;
24102 return false;
24103 }
24104
24105 case STMT_EXPR:
24106 /* Treat a GNU statement expression as dependent to avoid crashing
24107 under instantiate_non_dependent_expr; it can't be constant. */
24108 return true;
24109
24110 default:
24111 /* A constant expression is value-dependent if any subexpression is
24112 value-dependent. */
24113 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24114 {
24115 case tcc_reference:
24116 case tcc_unary:
24117 case tcc_comparison:
24118 case tcc_binary:
24119 case tcc_expression:
24120 case tcc_vl_exp:
24121 {
24122 int i, len = cp_tree_operand_length (expression);
24123
24124 for (i = 0; i < len; i++)
24125 {
24126 tree t = TREE_OPERAND (expression, i);
24127
24128 /* In some cases, some of the operands may be missing.
24129 (For example, in the case of PREDECREMENT_EXPR, the
24130 amount to increment by may be missing.) That doesn't
24131 make the expression dependent. */
24132 if (t && value_dependent_expression_p (t))
24133 return true;
24134 }
24135 }
24136 break;
24137 default:
24138 break;
24139 }
24140 break;
24141 }
24142
24143 /* The expression is not value-dependent. */
24144 return false;
24145 }
24146
24147 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24148 [temp.dep.expr]. Note that an expression with no type is
24149 considered dependent. Other parts of the compiler arrange for an
24150 expression with type-dependent subexpressions to have no type, so
24151 this function doesn't have to be fully recursive. */
24152
24153 bool
24154 type_dependent_expression_p (tree expression)
24155 {
24156 if (!processing_template_decl)
24157 return false;
24158
24159 if (expression == NULL_TREE || expression == error_mark_node)
24160 return false;
24161
24162 /* An unresolved name is always dependent. */
24163 if (identifier_p (expression)
24164 || TREE_CODE (expression) == USING_DECL
24165 || TREE_CODE (expression) == WILDCARD_DECL)
24166 return true;
24167
24168 /* A fold expression is type-dependent. */
24169 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24170 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24171 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24172 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24173 return true;
24174
24175 /* Some expression forms are never type-dependent. */
24176 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24177 || TREE_CODE (expression) == SIZEOF_EXPR
24178 || TREE_CODE (expression) == ALIGNOF_EXPR
24179 || TREE_CODE (expression) == AT_ENCODE_EXPR
24180 || TREE_CODE (expression) == NOEXCEPT_EXPR
24181 || TREE_CODE (expression) == TRAIT_EXPR
24182 || TREE_CODE (expression) == TYPEID_EXPR
24183 || TREE_CODE (expression) == DELETE_EXPR
24184 || TREE_CODE (expression) == VEC_DELETE_EXPR
24185 || TREE_CODE (expression) == THROW_EXPR
24186 || TREE_CODE (expression) == REQUIRES_EXPR)
24187 return false;
24188
24189 /* The types of these expressions depends only on the type to which
24190 the cast occurs. */
24191 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24192 || TREE_CODE (expression) == STATIC_CAST_EXPR
24193 || TREE_CODE (expression) == CONST_CAST_EXPR
24194 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24195 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24196 || TREE_CODE (expression) == CAST_EXPR)
24197 return dependent_type_p (TREE_TYPE (expression));
24198
24199 /* The types of these expressions depends only on the type created
24200 by the expression. */
24201 if (TREE_CODE (expression) == NEW_EXPR
24202 || TREE_CODE (expression) == VEC_NEW_EXPR)
24203 {
24204 /* For NEW_EXPR tree nodes created inside a template, either
24205 the object type itself or a TREE_LIST may appear as the
24206 operand 1. */
24207 tree type = TREE_OPERAND (expression, 1);
24208 if (TREE_CODE (type) == TREE_LIST)
24209 /* This is an array type. We need to check array dimensions
24210 as well. */
24211 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24212 || value_dependent_expression_p
24213 (TREE_OPERAND (TREE_VALUE (type), 1));
24214 else
24215 return dependent_type_p (type);
24216 }
24217
24218 if (TREE_CODE (expression) == SCOPE_REF)
24219 {
24220 tree scope = TREE_OPERAND (expression, 0);
24221 tree name = TREE_OPERAND (expression, 1);
24222
24223 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24224 contains an identifier associated by name lookup with one or more
24225 declarations declared with a dependent type, or...a
24226 nested-name-specifier or qualified-id that names a member of an
24227 unknown specialization. */
24228 return (type_dependent_expression_p (name)
24229 || dependent_scope_p (scope));
24230 }
24231
24232 if (TREE_CODE (expression) == TEMPLATE_DECL
24233 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24234 return uses_outer_template_parms (expression);
24235
24236 if (TREE_CODE (expression) == STMT_EXPR)
24237 expression = stmt_expr_value_expr (expression);
24238
24239 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24240 {
24241 tree elt;
24242 unsigned i;
24243
24244 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24245 {
24246 if (type_dependent_expression_p (elt))
24247 return true;
24248 }
24249 return false;
24250 }
24251
24252 /* A static data member of the current instantiation with incomplete
24253 array type is type-dependent, as the definition and specializations
24254 can have different bounds. */
24255 if (VAR_P (expression)
24256 && DECL_CLASS_SCOPE_P (expression)
24257 && dependent_type_p (DECL_CONTEXT (expression))
24258 && VAR_HAD_UNKNOWN_BOUND (expression))
24259 return true;
24260
24261 /* An array of unknown bound depending on a variadic parameter, eg:
24262
24263 template<typename... Args>
24264 void foo (Args... args)
24265 {
24266 int arr[] = { args... };
24267 }
24268
24269 template<int... vals>
24270 void bar ()
24271 {
24272 int arr[] = { vals... };
24273 }
24274
24275 If the array has no length and has an initializer, it must be that
24276 we couldn't determine its length in cp_complete_array_type because
24277 it is dependent. */
24278 if (VAR_P (expression)
24279 && TREE_TYPE (expression) != NULL_TREE
24280 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24281 && !TYPE_DOMAIN (TREE_TYPE (expression))
24282 && DECL_INITIAL (expression))
24283 return true;
24284
24285 /* A function or variable template-id is type-dependent if it has any
24286 dependent template arguments. */
24287 if (VAR_OR_FUNCTION_DECL_P (expression)
24288 && DECL_LANG_SPECIFIC (expression)
24289 && DECL_TEMPLATE_INFO (expression))
24290 {
24291 /* Consider the innermost template arguments, since those are the ones
24292 that come from the template-id; the template arguments for the
24293 enclosing class do not make it type-dependent unless they are used in
24294 the type of the decl. */
24295 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24296 && (any_dependent_template_arguments_p
24297 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24298 return true;
24299 }
24300
24301 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24302 type-dependent. Checking this is important for functions with auto return
24303 type, which looks like a dependent type. */
24304 if (TREE_CODE (expression) == FUNCTION_DECL
24305 && !(DECL_CLASS_SCOPE_P (expression)
24306 && dependent_type_p (DECL_CONTEXT (expression)))
24307 && !(DECL_FRIEND_P (expression)
24308 && (!DECL_FRIEND_CONTEXT (expression)
24309 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24310 && !DECL_LOCAL_FUNCTION_P (expression))
24311 {
24312 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24313 || undeduced_auto_decl (expression));
24314 return false;
24315 }
24316
24317 /* Always dependent, on the number of arguments if nothing else. */
24318 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24319 return true;
24320
24321 if (TREE_TYPE (expression) == unknown_type_node)
24322 {
24323 if (TREE_CODE (expression) == ADDR_EXPR)
24324 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24325 if (TREE_CODE (expression) == COMPONENT_REF
24326 || TREE_CODE (expression) == OFFSET_REF)
24327 {
24328 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24329 return true;
24330 expression = TREE_OPERAND (expression, 1);
24331 if (identifier_p (expression))
24332 return false;
24333 }
24334 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24335 if (TREE_CODE (expression) == SCOPE_REF)
24336 return false;
24337
24338 if (BASELINK_P (expression))
24339 {
24340 if (BASELINK_OPTYPE (expression)
24341 && dependent_type_p (BASELINK_OPTYPE (expression)))
24342 return true;
24343 expression = BASELINK_FUNCTIONS (expression);
24344 }
24345
24346 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24347 {
24348 if (any_dependent_template_arguments_p
24349 (TREE_OPERAND (expression, 1)))
24350 return true;
24351 expression = TREE_OPERAND (expression, 0);
24352 if (identifier_p (expression))
24353 return true;
24354 }
24355
24356 gcc_assert (TREE_CODE (expression) == OVERLOAD
24357 || TREE_CODE (expression) == FUNCTION_DECL);
24358
24359 for (lkp_iterator iter (expression); iter; ++iter)
24360 if (type_dependent_expression_p (*iter))
24361 return true;
24362
24363 return false;
24364 }
24365
24366 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24367
24368 /* Dependent type attributes might not have made it from the decl to
24369 the type yet. */
24370 if (DECL_P (expression)
24371 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24372 return true;
24373
24374 return (dependent_type_p (TREE_TYPE (expression)));
24375 }
24376
24377 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24378 type-dependent if the expression refers to a member of the current
24379 instantiation and the type of the referenced member is dependent, or the
24380 class member access expression refers to a member of an unknown
24381 specialization.
24382
24383 This function returns true if the OBJECT in such a class member access
24384 expression is of an unknown specialization. */
24385
24386 bool
24387 type_dependent_object_expression_p (tree object)
24388 {
24389 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24390 dependent. */
24391 if (TREE_CODE (object) == IDENTIFIER_NODE)
24392 return true;
24393 tree scope = TREE_TYPE (object);
24394 return (!scope || dependent_scope_p (scope));
24395 }
24396
24397 /* walk_tree callback function for instantiation_dependent_expression_p,
24398 below. Returns non-zero if a dependent subexpression is found. */
24399
24400 static tree
24401 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24402 void * /*data*/)
24403 {
24404 if (TYPE_P (*tp))
24405 {
24406 /* We don't have to worry about decltype currently because decltype
24407 of an instantiation-dependent expr is a dependent type. This
24408 might change depending on the resolution of DR 1172. */
24409 *walk_subtrees = false;
24410 return NULL_TREE;
24411 }
24412 enum tree_code code = TREE_CODE (*tp);
24413 switch (code)
24414 {
24415 /* Don't treat an argument list as dependent just because it has no
24416 TREE_TYPE. */
24417 case TREE_LIST:
24418 case TREE_VEC:
24419 return NULL_TREE;
24420
24421 case TEMPLATE_PARM_INDEX:
24422 return *tp;
24423
24424 /* Handle expressions with type operands. */
24425 case SIZEOF_EXPR:
24426 case ALIGNOF_EXPR:
24427 case TYPEID_EXPR:
24428 case AT_ENCODE_EXPR:
24429 {
24430 tree op = TREE_OPERAND (*tp, 0);
24431 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24432 op = TREE_TYPE (op);
24433 if (TYPE_P (op))
24434 {
24435 if (dependent_type_p (op))
24436 return *tp;
24437 else
24438 {
24439 *walk_subtrees = false;
24440 return NULL_TREE;
24441 }
24442 }
24443 break;
24444 }
24445
24446 case COMPONENT_REF:
24447 if (identifier_p (TREE_OPERAND (*tp, 1)))
24448 /* In a template, finish_class_member_access_expr creates a
24449 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24450 type-dependent, so that we can check access control at
24451 instantiation time (PR 42277). See also Core issue 1273. */
24452 return *tp;
24453 break;
24454
24455 case SCOPE_REF:
24456 if (instantiation_dependent_scope_ref_p (*tp))
24457 return *tp;
24458 else
24459 break;
24460
24461 /* Treat statement-expressions as dependent. */
24462 case BIND_EXPR:
24463 return *tp;
24464
24465 /* Treat requires-expressions as dependent. */
24466 case REQUIRES_EXPR:
24467 return *tp;
24468
24469 case CALL_EXPR:
24470 /* Treat calls to function concepts as dependent. */
24471 if (function_concept_check_p (*tp))
24472 return *tp;
24473 break;
24474
24475 case TEMPLATE_ID_EXPR:
24476 /* And variable concepts. */
24477 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24478 return *tp;
24479 break;
24480
24481 default:
24482 break;
24483 }
24484
24485 if (type_dependent_expression_p (*tp))
24486 return *tp;
24487 else
24488 return NULL_TREE;
24489 }
24490
24491 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24492 sense defined by the ABI:
24493
24494 "An expression is instantiation-dependent if it is type-dependent
24495 or value-dependent, or it has a subexpression that is type-dependent
24496 or value-dependent."
24497
24498 Except don't actually check value-dependence for unevaluated expressions,
24499 because in sizeof(i) we don't care about the value of i. Checking
24500 type-dependence will in turn check value-dependence of array bounds/template
24501 arguments as needed. */
24502
24503 bool
24504 instantiation_dependent_uneval_expression_p (tree expression)
24505 {
24506 tree result;
24507
24508 if (!processing_template_decl)
24509 return false;
24510
24511 if (expression == error_mark_node)
24512 return false;
24513
24514 result = cp_walk_tree_without_duplicates (&expression,
24515 instantiation_dependent_r, NULL);
24516 return result != NULL_TREE;
24517 }
24518
24519 /* As above, but also check value-dependence of the expression as a whole. */
24520
24521 bool
24522 instantiation_dependent_expression_p (tree expression)
24523 {
24524 return (instantiation_dependent_uneval_expression_p (expression)
24525 || value_dependent_expression_p (expression));
24526 }
24527
24528 /* Like type_dependent_expression_p, but it also works while not processing
24529 a template definition, i.e. during substitution or mangling. */
24530
24531 bool
24532 type_dependent_expression_p_push (tree expr)
24533 {
24534 bool b;
24535 ++processing_template_decl;
24536 b = type_dependent_expression_p (expr);
24537 --processing_template_decl;
24538 return b;
24539 }
24540
24541 /* Returns TRUE if ARGS contains a type-dependent expression. */
24542
24543 bool
24544 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24545 {
24546 unsigned int i;
24547 tree arg;
24548
24549 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24550 {
24551 if (type_dependent_expression_p (arg))
24552 return true;
24553 }
24554 return false;
24555 }
24556
24557 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24558 expressions) contains any type-dependent expressions. */
24559
24560 bool
24561 any_type_dependent_elements_p (const_tree list)
24562 {
24563 for (; list; list = TREE_CHAIN (list))
24564 if (type_dependent_expression_p (TREE_VALUE (list)))
24565 return true;
24566
24567 return false;
24568 }
24569
24570 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24571 expressions) contains any value-dependent expressions. */
24572
24573 bool
24574 any_value_dependent_elements_p (const_tree list)
24575 {
24576 for (; list; list = TREE_CHAIN (list))
24577 if (value_dependent_expression_p (TREE_VALUE (list)))
24578 return true;
24579
24580 return false;
24581 }
24582
24583 /* Returns TRUE if the ARG (a template argument) is dependent. */
24584
24585 bool
24586 dependent_template_arg_p (tree arg)
24587 {
24588 if (!processing_template_decl)
24589 return false;
24590
24591 /* Assume a template argument that was wrongly written by the user
24592 is dependent. This is consistent with what
24593 any_dependent_template_arguments_p [that calls this function]
24594 does. */
24595 if (!arg || arg == error_mark_node)
24596 return true;
24597
24598 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24599 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24600
24601 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24602 return true;
24603 if (TREE_CODE (arg) == TEMPLATE_DECL)
24604 {
24605 if (DECL_TEMPLATE_PARM_P (arg))
24606 return true;
24607 /* A member template of a dependent class is not necessarily
24608 type-dependent, but it is a dependent template argument because it
24609 will be a member of an unknown specialization to that template. */
24610 tree scope = CP_DECL_CONTEXT (arg);
24611 return TYPE_P (scope) && dependent_type_p (scope);
24612 }
24613 else if (ARGUMENT_PACK_P (arg))
24614 {
24615 tree args = ARGUMENT_PACK_ARGS (arg);
24616 int i, len = TREE_VEC_LENGTH (args);
24617 for (i = 0; i < len; ++i)
24618 {
24619 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24620 return true;
24621 }
24622
24623 return false;
24624 }
24625 else if (TYPE_P (arg))
24626 return dependent_type_p (arg);
24627 else
24628 return (type_dependent_expression_p (arg)
24629 || value_dependent_expression_p (arg));
24630 }
24631
24632 /* Returns true if ARGS (a collection of template arguments) contains
24633 any types that require structural equality testing. */
24634
24635 bool
24636 any_template_arguments_need_structural_equality_p (tree args)
24637 {
24638 int i;
24639 int j;
24640
24641 if (!args)
24642 return false;
24643 if (args == error_mark_node)
24644 return true;
24645
24646 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24647 {
24648 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24649 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24650 {
24651 tree arg = TREE_VEC_ELT (level, j);
24652 tree packed_args = NULL_TREE;
24653 int k, len = 1;
24654
24655 if (ARGUMENT_PACK_P (arg))
24656 {
24657 /* Look inside the argument pack. */
24658 packed_args = ARGUMENT_PACK_ARGS (arg);
24659 len = TREE_VEC_LENGTH (packed_args);
24660 }
24661
24662 for (k = 0; k < len; ++k)
24663 {
24664 if (packed_args)
24665 arg = TREE_VEC_ELT (packed_args, k);
24666
24667 if (error_operand_p (arg))
24668 return true;
24669 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24670 continue;
24671 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24672 return true;
24673 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24674 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24675 return true;
24676 }
24677 }
24678 }
24679
24680 return false;
24681 }
24682
24683 /* Returns true if ARGS (a collection of template arguments) contains
24684 any dependent arguments. */
24685
24686 bool
24687 any_dependent_template_arguments_p (const_tree args)
24688 {
24689 int i;
24690 int j;
24691
24692 if (!args)
24693 return false;
24694 if (args == error_mark_node)
24695 return true;
24696
24697 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24698 {
24699 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24700 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24701 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24702 return true;
24703 }
24704
24705 return false;
24706 }
24707
24708 /* Returns TRUE if the template TMPL is type-dependent. */
24709
24710 bool
24711 dependent_template_p (tree tmpl)
24712 {
24713 if (TREE_CODE (tmpl) == OVERLOAD)
24714 {
24715 for (lkp_iterator iter (tmpl); iter; ++iter)
24716 if (dependent_template_p (*iter))
24717 return true;
24718 return false;
24719 }
24720
24721 /* Template template parameters are dependent. */
24722 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24723 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24724 return true;
24725 /* So are names that have not been looked up. */
24726 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24727 return true;
24728 return false;
24729 }
24730
24731 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24732
24733 bool
24734 dependent_template_id_p (tree tmpl, tree args)
24735 {
24736 return (dependent_template_p (tmpl)
24737 || any_dependent_template_arguments_p (args));
24738 }
24739
24740 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24741 are dependent. */
24742
24743 bool
24744 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24745 {
24746 int i;
24747
24748 if (!processing_template_decl)
24749 return false;
24750
24751 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24752 {
24753 tree decl = TREE_VEC_ELT (declv, i);
24754 tree init = TREE_VEC_ELT (initv, i);
24755 tree cond = TREE_VEC_ELT (condv, i);
24756 tree incr = TREE_VEC_ELT (incrv, i);
24757
24758 if (type_dependent_expression_p (decl)
24759 || TREE_CODE (decl) == SCOPE_REF)
24760 return true;
24761
24762 if (init && type_dependent_expression_p (init))
24763 return true;
24764
24765 if (type_dependent_expression_p (cond))
24766 return true;
24767
24768 if (COMPARISON_CLASS_P (cond)
24769 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24770 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24771 return true;
24772
24773 if (TREE_CODE (incr) == MODOP_EXPR)
24774 {
24775 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24776 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24777 return true;
24778 }
24779 else if (type_dependent_expression_p (incr))
24780 return true;
24781 else if (TREE_CODE (incr) == MODIFY_EXPR)
24782 {
24783 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24784 return true;
24785 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24786 {
24787 tree t = TREE_OPERAND (incr, 1);
24788 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24789 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24790 return true;
24791 }
24792 }
24793 }
24794
24795 return false;
24796 }
24797
24798 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24799 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24800 no such TYPE can be found. Note that this function peers inside
24801 uninstantiated templates and therefore should be used only in
24802 extremely limited situations. ONLY_CURRENT_P restricts this
24803 peering to the currently open classes hierarchy (which is required
24804 when comparing types). */
24805
24806 tree
24807 resolve_typename_type (tree type, bool only_current_p)
24808 {
24809 tree scope;
24810 tree name;
24811 tree decl;
24812 int quals;
24813 tree pushed_scope;
24814 tree result;
24815
24816 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24817
24818 scope = TYPE_CONTEXT (type);
24819 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24820 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24821 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24822 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24823 identifier of the TYPENAME_TYPE anymore.
24824 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24825 TYPENAME_TYPE instead, we avoid messing up with a possible
24826 typedef variant case. */
24827 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24828
24829 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24830 it first before we can figure out what NAME refers to. */
24831 if (TREE_CODE (scope) == TYPENAME_TYPE)
24832 {
24833 if (TYPENAME_IS_RESOLVING_P (scope))
24834 /* Given a class template A with a dependent base with nested type C,
24835 typedef typename A::C::C C will land us here, as trying to resolve
24836 the initial A::C leads to the local C typedef, which leads back to
24837 A::C::C. So we break the recursion now. */
24838 return type;
24839 else
24840 scope = resolve_typename_type (scope, only_current_p);
24841 }
24842 /* If we don't know what SCOPE refers to, then we cannot resolve the
24843 TYPENAME_TYPE. */
24844 if (!CLASS_TYPE_P (scope))
24845 return type;
24846 /* If this is a typedef, we don't want to look inside (c++/11987). */
24847 if (typedef_variant_p (type))
24848 return type;
24849 /* If SCOPE isn't the template itself, it will not have a valid
24850 TYPE_FIELDS list. */
24851 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24852 /* scope is either the template itself or a compatible instantiation
24853 like X<T>, so look up the name in the original template. */
24854 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24855 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24856 gcc_checking_assert (uses_template_parms (scope));
24857 /* If scope has no fields, it can't be a current instantiation. Check this
24858 before currently_open_class to avoid infinite recursion (71515). */
24859 if (!TYPE_FIELDS (scope))
24860 return type;
24861 /* If the SCOPE is not the current instantiation, there's no reason
24862 to look inside it. */
24863 if (only_current_p && !currently_open_class (scope))
24864 return type;
24865 /* Enter the SCOPE so that name lookup will be resolved as if we
24866 were in the class definition. In particular, SCOPE will no
24867 longer be considered a dependent type. */
24868 pushed_scope = push_scope (scope);
24869 /* Look up the declaration. */
24870 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24871 tf_warning_or_error);
24872
24873 result = NULL_TREE;
24874
24875 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24876 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24877 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24878 if (!decl)
24879 /*nop*/;
24880 else if (identifier_p (fullname)
24881 && TREE_CODE (decl) == TYPE_DECL)
24882 {
24883 result = TREE_TYPE (decl);
24884 if (result == error_mark_node)
24885 result = NULL_TREE;
24886 }
24887 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24888 && DECL_CLASS_TEMPLATE_P (decl))
24889 {
24890 /* Obtain the template and the arguments. */
24891 tree tmpl = TREE_OPERAND (fullname, 0);
24892 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24893 {
24894 /* We get here with a plain identifier because a previous tentative
24895 parse of the nested-name-specifier as part of a ptr-operator saw
24896 ::template X<A>. The use of ::template is necessary in a
24897 ptr-operator, but wrong in a declarator-id.
24898
24899 [temp.names]: In a qualified-id of a declarator-id, the keyword
24900 template shall not appear at the top level. */
24901 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24902 "keyword %<template%> not allowed in declarator-id");
24903 tmpl = decl;
24904 }
24905 tree args = TREE_OPERAND (fullname, 1);
24906 /* Instantiate the template. */
24907 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24908 /*entering_scope=*/true,
24909 tf_error | tf_user);
24910 if (result == error_mark_node)
24911 result = NULL_TREE;
24912 }
24913
24914 /* Leave the SCOPE. */
24915 if (pushed_scope)
24916 pop_scope (pushed_scope);
24917
24918 /* If we failed to resolve it, return the original typename. */
24919 if (!result)
24920 return type;
24921
24922 /* If lookup found a typename type, resolve that too. */
24923 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24924 {
24925 /* Ill-formed programs can cause infinite recursion here, so we
24926 must catch that. */
24927 TYPENAME_IS_RESOLVING_P (result) = 1;
24928 result = resolve_typename_type (result, only_current_p);
24929 TYPENAME_IS_RESOLVING_P (result) = 0;
24930 }
24931
24932 /* Qualify the resulting type. */
24933 quals = cp_type_quals (type);
24934 if (quals)
24935 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24936
24937 return result;
24938 }
24939
24940 /* EXPR is an expression which is not type-dependent. Return a proxy
24941 for EXPR that can be used to compute the types of larger
24942 expressions containing EXPR. */
24943
24944 tree
24945 build_non_dependent_expr (tree expr)
24946 {
24947 tree inner_expr;
24948
24949 /* When checking, try to get a constant value for all non-dependent
24950 expressions in order to expose bugs in *_dependent_expression_p
24951 and constexpr. This can affect code generation, see PR70704, so
24952 only do this for -fchecking=2. */
24953 if (flag_checking > 1
24954 && cxx_dialect >= cxx11
24955 /* Don't do this during nsdmi parsing as it can lead to
24956 unexpected recursive instantiations. */
24957 && !parsing_nsdmi ()
24958 /* Don't do this during concept expansion either and for
24959 the same reason. */
24960 && !expanding_concept ())
24961 fold_non_dependent_expr (expr);
24962
24963 /* Preserve OVERLOADs; the functions must be available to resolve
24964 types. */
24965 inner_expr = expr;
24966 if (TREE_CODE (inner_expr) == STMT_EXPR)
24967 inner_expr = stmt_expr_value_expr (inner_expr);
24968 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24969 inner_expr = TREE_OPERAND (inner_expr, 0);
24970 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24971 inner_expr = TREE_OPERAND (inner_expr, 1);
24972 if (is_overloaded_fn (inner_expr)
24973 || TREE_CODE (inner_expr) == OFFSET_REF)
24974 return expr;
24975 /* There is no need to return a proxy for a variable. */
24976 if (VAR_P (expr))
24977 return expr;
24978 /* Preserve string constants; conversions from string constants to
24979 "char *" are allowed, even though normally a "const char *"
24980 cannot be used to initialize a "char *". */
24981 if (TREE_CODE (expr) == STRING_CST)
24982 return expr;
24983 /* Preserve void and arithmetic constants, as an optimization -- there is no
24984 reason to create a new node. */
24985 if (TREE_CODE (expr) == VOID_CST
24986 || TREE_CODE (expr) == INTEGER_CST
24987 || TREE_CODE (expr) == REAL_CST)
24988 return expr;
24989 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24990 There is at least one place where we want to know that a
24991 particular expression is a throw-expression: when checking a ?:
24992 expression, there are special rules if the second or third
24993 argument is a throw-expression. */
24994 if (TREE_CODE (expr) == THROW_EXPR)
24995 return expr;
24996
24997 /* Don't wrap an initializer list, we need to be able to look inside. */
24998 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24999 return expr;
25000
25001 /* Don't wrap a dummy object, we need to be able to test for it. */
25002 if (is_dummy_object (expr))
25003 return expr;
25004
25005 if (TREE_CODE (expr) == COND_EXPR)
25006 return build3 (COND_EXPR,
25007 TREE_TYPE (expr),
25008 TREE_OPERAND (expr, 0),
25009 (TREE_OPERAND (expr, 1)
25010 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25011 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25012 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25013 if (TREE_CODE (expr) == COMPOUND_EXPR
25014 && !COMPOUND_EXPR_OVERLOADED (expr))
25015 return build2 (COMPOUND_EXPR,
25016 TREE_TYPE (expr),
25017 TREE_OPERAND (expr, 0),
25018 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25019
25020 /* If the type is unknown, it can't really be non-dependent */
25021 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25022
25023 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25024 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25025 }
25026
25027 /* ARGS is a vector of expressions as arguments to a function call.
25028 Replace the arguments with equivalent non-dependent expressions.
25029 This modifies ARGS in place. */
25030
25031 void
25032 make_args_non_dependent (vec<tree, va_gc> *args)
25033 {
25034 unsigned int ix;
25035 tree arg;
25036
25037 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25038 {
25039 tree newarg = build_non_dependent_expr (arg);
25040 if (newarg != arg)
25041 (*args)[ix] = newarg;
25042 }
25043 }
25044
25045 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25046 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25047 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25048
25049 static tree
25050 make_auto_1 (tree name, bool set_canonical)
25051 {
25052 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25053 TYPE_NAME (au) = build_decl (input_location,
25054 TYPE_DECL, name, au);
25055 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25056 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25057 (0, processing_template_decl + 1, processing_template_decl + 1,
25058 TYPE_NAME (au), NULL_TREE);
25059 if (set_canonical)
25060 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25061 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25062 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25063
25064 return au;
25065 }
25066
25067 tree
25068 make_decltype_auto (void)
25069 {
25070 return make_auto_1 (decltype_auto_identifier, true);
25071 }
25072
25073 tree
25074 make_auto (void)
25075 {
25076 return make_auto_1 (auto_identifier, true);
25077 }
25078
25079 /* Return a C++17 deduction placeholder for class template TMPL. */
25080
25081 tree
25082 make_template_placeholder (tree tmpl)
25083 {
25084 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25085 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25086 return t;
25087 }
25088
25089 /* True iff T is a C++17 class template deduction placeholder. */
25090
25091 bool
25092 template_placeholder_p (tree t)
25093 {
25094 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25095 }
25096
25097 /* Make a "constrained auto" type-specifier. This is an
25098 auto type with constraints that must be associated after
25099 deduction. The constraint is formed from the given
25100 CONC and its optional sequence of arguments, which are
25101 non-null if written as partial-concept-id. */
25102
25103 tree
25104 make_constrained_auto (tree con, tree args)
25105 {
25106 tree type = make_auto_1 (auto_identifier, false);
25107
25108 /* Build the constraint. */
25109 tree tmpl = DECL_TI_TEMPLATE (con);
25110 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25111 expr = build_concept_check (expr, type, args);
25112
25113 tree constr = normalize_expression (expr);
25114 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25115
25116 /* Our canonical type depends on the constraint. */
25117 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25118
25119 /* Attach the constraint to the type declaration. */
25120 tree decl = TYPE_NAME (type);
25121 return decl;
25122 }
25123
25124 /* Given type ARG, return std::initializer_list<ARG>. */
25125
25126 static tree
25127 listify (tree arg)
25128 {
25129 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25130
25131 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25132 {
25133 gcc_rich_location richloc (input_location);
25134 maybe_add_include_fixit (&richloc, "<initializer_list>");
25135 error_at (&richloc,
25136 "deducing from brace-enclosed initializer list"
25137 " requires %<#include <initializer_list>%>");
25138
25139 return error_mark_node;
25140 }
25141 tree argvec = make_tree_vec (1);
25142 TREE_VEC_ELT (argvec, 0) = arg;
25143
25144 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25145 NULL_TREE, 0, tf_warning_or_error);
25146 }
25147
25148 /* Replace auto in TYPE with std::initializer_list<auto>. */
25149
25150 static tree
25151 listify_autos (tree type, tree auto_node)
25152 {
25153 tree init_auto = listify (auto_node);
25154 tree argvec = make_tree_vec (1);
25155 TREE_VEC_ELT (argvec, 0) = init_auto;
25156 if (processing_template_decl)
25157 argvec = add_to_template_args (current_template_args (), argvec);
25158 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25159 }
25160
25161 /* Hash traits for hashing possibly constrained 'auto'
25162 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25163
25164 struct auto_hash : default_hash_traits<tree>
25165 {
25166 static inline hashval_t hash (tree);
25167 static inline bool equal (tree, tree);
25168 };
25169
25170 /* Hash the 'auto' T. */
25171
25172 inline hashval_t
25173 auto_hash::hash (tree t)
25174 {
25175 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25176 /* Matching constrained-type-specifiers denote the same template
25177 parameter, so hash the constraint. */
25178 return hash_placeholder_constraint (c);
25179 else
25180 /* But unconstrained autos are all separate, so just hash the pointer. */
25181 return iterative_hash_object (t, 0);
25182 }
25183
25184 /* Compare two 'auto's. */
25185
25186 inline bool
25187 auto_hash::equal (tree t1, tree t2)
25188 {
25189 if (t1 == t2)
25190 return true;
25191
25192 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25193 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25194
25195 /* Two unconstrained autos are distinct. */
25196 if (!c1 || !c2)
25197 return false;
25198
25199 return equivalent_placeholder_constraints (c1, c2);
25200 }
25201
25202 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25203 constrained) auto, add it to the vector. */
25204
25205 static int
25206 extract_autos_r (tree t, void *data)
25207 {
25208 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25209 if (is_auto (t))
25210 {
25211 /* All the autos were built with index 0; fix that up now. */
25212 tree *p = hash.find_slot (t, INSERT);
25213 unsigned idx;
25214 if (*p)
25215 /* If this is a repeated constrained-type-specifier, use the index we
25216 chose before. */
25217 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25218 else
25219 {
25220 /* Otherwise this is new, so use the current count. */
25221 *p = t;
25222 idx = hash.elements () - 1;
25223 }
25224 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25225 }
25226
25227 /* Always keep walking. */
25228 return 0;
25229 }
25230
25231 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25232 says they can appear anywhere in the type. */
25233
25234 static tree
25235 extract_autos (tree type)
25236 {
25237 hash_set<tree> visited;
25238 hash_table<auto_hash> hash (2);
25239
25240 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25241
25242 tree tree_vec = make_tree_vec (hash.elements());
25243 for (hash_table<auto_hash>::iterator iter = hash.begin();
25244 iter != hash.end(); ++iter)
25245 {
25246 tree elt = *iter;
25247 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25248 TREE_VEC_ELT (tree_vec, i)
25249 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25250 }
25251
25252 return tree_vec;
25253 }
25254
25255 /* The stem for deduction guide names. */
25256 const char *const dguide_base = "__dguide_";
25257
25258 /* Return the name for a deduction guide for class template TMPL. */
25259
25260 tree
25261 dguide_name (tree tmpl)
25262 {
25263 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25264 tree tname = TYPE_IDENTIFIER (type);
25265 char *buf = (char *) alloca (1 + strlen (dguide_base)
25266 + IDENTIFIER_LENGTH (tname));
25267 memcpy (buf, dguide_base, strlen (dguide_base));
25268 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25269 IDENTIFIER_LENGTH (tname) + 1);
25270 tree dname = get_identifier (buf);
25271 TREE_TYPE (dname) = type;
25272 return dname;
25273 }
25274
25275 /* True if NAME is the name of a deduction guide. */
25276
25277 bool
25278 dguide_name_p (tree name)
25279 {
25280 return (TREE_TYPE (name)
25281 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25282 strlen (dguide_base)));
25283 }
25284
25285 /* True if FN is a deduction guide. */
25286
25287 bool
25288 deduction_guide_p (const_tree fn)
25289 {
25290 if (DECL_P (fn))
25291 if (tree name = DECL_NAME (fn))
25292 return dguide_name_p (name);
25293 return false;
25294 }
25295
25296 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25297
25298 bool
25299 copy_guide_p (const_tree fn)
25300 {
25301 gcc_assert (deduction_guide_p (fn));
25302 if (!DECL_ARTIFICIAL (fn))
25303 return false;
25304 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25305 return (TREE_CHAIN (parms) == void_list_node
25306 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25307 }
25308
25309 /* True if FN is a guide generated from a constructor template. */
25310
25311 bool
25312 template_guide_p (const_tree fn)
25313 {
25314 gcc_assert (deduction_guide_p (fn));
25315 if (!DECL_ARTIFICIAL (fn))
25316 return false;
25317 tree tmpl = DECL_TI_TEMPLATE (fn);
25318 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25319 return PRIMARY_TEMPLATE_P (org);
25320 return false;
25321 }
25322
25323 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25324 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25325 template parameter types. Note that the handling of template template
25326 parameters relies on current_template_parms being set appropriately for the
25327 new template. */
25328
25329 static tree
25330 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25331 tree tsubst_args, tsubst_flags_t complain)
25332 {
25333 tree oldidx = get_template_parm_index (olddecl);
25334
25335 tree newtype;
25336 if (TREE_CODE (olddecl) == TYPE_DECL
25337 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25338 {
25339 tree oldtype = TREE_TYPE (olddecl);
25340 newtype = cxx_make_type (TREE_CODE (oldtype));
25341 TYPE_MAIN_VARIANT (newtype) = newtype;
25342 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25343 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25344 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25345 }
25346 else
25347 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25348 complain, NULL_TREE);
25349
25350 tree newdecl
25351 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25352 DECL_NAME (olddecl), newtype);
25353 SET_DECL_TEMPLATE_PARM_P (newdecl);
25354
25355 tree newidx;
25356 if (TREE_CODE (olddecl) == TYPE_DECL
25357 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25358 {
25359 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25360 = build_template_parm_index (index, level, level,
25361 newdecl, newtype);
25362 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25363 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25364 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25365 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25366
25367 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25368 {
25369 DECL_TEMPLATE_RESULT (newdecl)
25370 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25371 DECL_NAME (olddecl), newtype);
25372 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25373 // First create a copy (ttargs) of tsubst_args with an
25374 // additional level for the template template parameter's own
25375 // template parameters (ttparms).
25376 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25377 (DECL_TEMPLATE_PARMS (olddecl)));
25378 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25379 tree ttargs = make_tree_vec (depth + 1);
25380 for (int i = 0; i < depth; ++i)
25381 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25382 TREE_VEC_ELT (ttargs, depth)
25383 = template_parms_level_to_args (ttparms);
25384 // Substitute ttargs into ttparms to fix references to
25385 // other template parameters.
25386 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25387 complain);
25388 // Now substitute again with args based on tparms, to reduce
25389 // the level of the ttparms.
25390 ttargs = current_template_args ();
25391 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25392 complain);
25393 // Finally, tack the adjusted parms onto tparms.
25394 ttparms = tree_cons (size_int (depth), ttparms,
25395 current_template_parms);
25396 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25397 }
25398 }
25399 else
25400 {
25401 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25402 tree newconst
25403 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25404 TREE_CODE (oldconst),
25405 DECL_NAME (oldconst), newtype);
25406 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25407 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25408 SET_DECL_TEMPLATE_PARM_P (newconst);
25409 newidx = build_template_parm_index (index, level, level,
25410 newconst, newtype);
25411 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25412 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25413 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25414 }
25415
25416 return newdecl;
25417 }
25418
25419 /* Returns a C++17 class deduction guide template based on the constructor
25420 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25421 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25422
25423 static tree
25424 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25425 {
25426 tree type, tparms, targs, fparms, fargs, ci;
25427 bool memtmpl = false;
25428 bool explicit_p;
25429 location_t loc;
25430 tree fn_tmpl = NULL_TREE;
25431
25432 if (TYPE_P (ctor))
25433 {
25434 type = ctor;
25435 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25436 if (copy_p)
25437 {
25438 type = TREE_TYPE (type);
25439 fparms = tree_cons (NULL_TREE, type, void_list_node);
25440 }
25441 else
25442 fparms = void_list_node;
25443
25444 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25445 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25446 targs = CLASSTYPE_TI_ARGS (type);
25447 ci = NULL_TREE;
25448 fargs = NULL_TREE;
25449 loc = DECL_SOURCE_LOCATION (ctmpl);
25450 explicit_p = false;
25451 }
25452 else
25453 {
25454 ++processing_template_decl;
25455
25456 fn_tmpl
25457 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25458 : DECL_TI_TEMPLATE (ctor));
25459 if (outer_args)
25460 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25461 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25462
25463 type = DECL_CONTEXT (ctor);
25464
25465 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25466 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25467 fully specialized args for the enclosing class. Strip those off, as
25468 the deduction guide won't have those template parameters. */
25469 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25470 TMPL_PARMS_DEPTH (tparms));
25471 /* Discard the 'this' parameter. */
25472 fparms = FUNCTION_ARG_CHAIN (ctor);
25473 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25474 ci = get_constraints (ctor);
25475 loc = DECL_SOURCE_LOCATION (ctor);
25476 explicit_p = DECL_NONCONVERTING_P (ctor);
25477
25478 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25479 {
25480 memtmpl = true;
25481
25482 /* For a member template constructor, we need to flatten the two
25483 template parameter lists into one, and then adjust the function
25484 signature accordingly. This gets...complicated. */
25485 tree save_parms = current_template_parms;
25486
25487 /* For a member template we should have two levels of parms/args, one
25488 for the class and one for the constructor. We stripped
25489 specialized args for further enclosing classes above. */
25490 const int depth = 2;
25491 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25492
25493 /* Template args for translating references to the two-level template
25494 parameters into references to the one-level template parameters we
25495 are creating. */
25496 tree tsubst_args = copy_node (targs);
25497 TMPL_ARGS_LEVEL (tsubst_args, depth)
25498 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25499
25500 /* Template parms for the constructor template. */
25501 tree ftparms = TREE_VALUE (tparms);
25502 unsigned flen = TREE_VEC_LENGTH (ftparms);
25503 /* Template parms for the class template. */
25504 tparms = TREE_CHAIN (tparms);
25505 tree ctparms = TREE_VALUE (tparms);
25506 unsigned clen = TREE_VEC_LENGTH (ctparms);
25507 /* Template parms for the deduction guide start as a copy of the
25508 template parms for the class. We set current_template_parms for
25509 lookup_template_class_1. */
25510 current_template_parms = tparms = copy_node (tparms);
25511 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25512 for (unsigned i = 0; i < clen; ++i)
25513 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25514
25515 /* Now we need to rewrite the constructor parms to append them to the
25516 class parms. */
25517 for (unsigned i = 0; i < flen; ++i)
25518 {
25519 unsigned index = i + clen;
25520 unsigned level = 1;
25521 tree oldelt = TREE_VEC_ELT (ftparms, i);
25522 tree olddecl = TREE_VALUE (oldelt);
25523 tree newdecl = rewrite_template_parm (olddecl, index, level,
25524 tsubst_args, complain);
25525 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25526 tsubst_args, complain, ctor);
25527 tree list = build_tree_list (newdef, newdecl);
25528 TEMPLATE_PARM_CONSTRAINTS (list)
25529 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25530 tsubst_args, complain, ctor);
25531 TREE_VEC_ELT (new_vec, index) = list;
25532 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25533 }
25534
25535 /* Now we have a final set of template parms to substitute into the
25536 function signature. */
25537 targs = template_parms_to_args (tparms);
25538 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25539 complain, ctor);
25540 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25541 if (ci)
25542 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25543
25544 current_template_parms = save_parms;
25545 }
25546 --processing_template_decl;
25547 }
25548
25549 if (!memtmpl)
25550 {
25551 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25552 tparms = copy_node (tparms);
25553 INNERMOST_TEMPLATE_PARMS (tparms)
25554 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25555 }
25556
25557 tree fntype = build_function_type (type, fparms);
25558 tree ded_fn = build_lang_decl_loc (loc,
25559 FUNCTION_DECL,
25560 dguide_name (type), fntype);
25561 DECL_ARGUMENTS (ded_fn) = fargs;
25562 DECL_ARTIFICIAL (ded_fn) = true;
25563 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25564 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25565 DECL_ARTIFICIAL (ded_tmpl) = true;
25566 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25567 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25568 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25569 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25570 if (DECL_P (ctor))
25571 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25572 if (ci)
25573 set_constraints (ded_tmpl, ci);
25574
25575 return ded_tmpl;
25576 }
25577
25578 /* Deduce template arguments for the class template placeholder PTYPE for
25579 template TMPL based on the initializer INIT, and return the resulting
25580 type. */
25581
25582 static tree
25583 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25584 tsubst_flags_t complain)
25585 {
25586 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25587 {
25588 /* We should have handled this in the caller. */
25589 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25590 return ptype;
25591 if (complain & tf_error)
25592 error ("non-class template %qT used without template arguments", tmpl);
25593 return error_mark_node;
25594 }
25595
25596 tree type = TREE_TYPE (tmpl);
25597
25598 bool try_list_ctor = false;
25599
25600 vec<tree,va_gc> *args;
25601 if (init == NULL_TREE
25602 || TREE_CODE (init) == TREE_LIST)
25603 args = make_tree_vector_from_list (init);
25604 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25605 {
25606 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25607 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25608 {
25609 /* As an exception, the first phase in 16.3.1.7 (considering the
25610 initializer list as a single argument) is omitted if the
25611 initializer list consists of a single expression of type cv U,
25612 where U is a specialization of C or a class derived from a
25613 specialization of C. */
25614 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25615 tree etype = TREE_TYPE (elt);
25616
25617 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25618 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25619 int err = unify (tparms, targs, type, etype,
25620 UNIFY_ALLOW_DERIVED, /*explain*/false);
25621 if (err == 0)
25622 try_list_ctor = false;
25623 ggc_free (targs);
25624 }
25625 if (try_list_ctor || is_std_init_list (type))
25626 args = make_tree_vector_single (init);
25627 else
25628 args = make_tree_vector_from_ctor (init);
25629 }
25630 else
25631 args = make_tree_vector_single (init);
25632
25633 tree dname = dguide_name (tmpl);
25634 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25635 /*type*/false, /*complain*/false,
25636 /*hidden*/false);
25637 bool elided = false;
25638 if (cands == error_mark_node)
25639 cands = NULL_TREE;
25640
25641 /* Prune explicit deduction guides in copy-initialization context. */
25642 if (flags & LOOKUP_ONLYCONVERTING)
25643 {
25644 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25645 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25646 elided = true;
25647
25648 if (elided)
25649 {
25650 /* Found a nonconverting guide, prune the candidates. */
25651 tree pruned = NULL_TREE;
25652 for (lkp_iterator iter (cands); iter; ++iter)
25653 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25654 pruned = lookup_add (*iter, pruned);
25655
25656 cands = pruned;
25657 }
25658 }
25659
25660 tree outer_args = NULL_TREE;
25661 if (DECL_CLASS_SCOPE_P (tmpl)
25662 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25663 {
25664 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25665 type = TREE_TYPE (most_general_template (tmpl));
25666 }
25667
25668 bool saw_ctor = false;
25669 // FIXME cache artificial deduction guides
25670 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25671 {
25672 tree guide = build_deduction_guide (*iter, outer_args, complain);
25673 if ((flags & LOOKUP_ONLYCONVERTING)
25674 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25675 elided = true;
25676 else
25677 cands = lookup_add (guide, cands);
25678
25679 saw_ctor = true;
25680 }
25681
25682 tree call = error_mark_node;
25683
25684 /* If this is list-initialization and the class has a list constructor, first
25685 try deducing from the list as a single argument, as [over.match.list]. */
25686 tree list_cands = NULL_TREE;
25687 if (try_list_ctor && cands)
25688 for (lkp_iterator iter (cands); iter; ++iter)
25689 {
25690 tree dg = *iter;
25691 if (is_list_ctor (dg))
25692 list_cands = lookup_add (dg, list_cands);
25693 }
25694 if (list_cands)
25695 {
25696 ++cp_unevaluated_operand;
25697 call = build_new_function_call (list_cands, &args, tf_decltype);
25698 --cp_unevaluated_operand;
25699
25700 if (call == error_mark_node)
25701 {
25702 /* That didn't work, now try treating the list as a sequence of
25703 arguments. */
25704 release_tree_vector (args);
25705 args = make_tree_vector_from_ctor (init);
25706 }
25707 }
25708
25709 /* Maybe generate an implicit deduction guide. */
25710 if (call == error_mark_node && args->length () < 2)
25711 {
25712 tree gtype = NULL_TREE;
25713
25714 if (args->length () == 1)
25715 /* Generate a copy guide. */
25716 gtype = build_reference_type (type);
25717 else if (!saw_ctor)
25718 /* Generate a default guide. */
25719 gtype = type;
25720
25721 if (gtype)
25722 {
25723 tree guide = build_deduction_guide (gtype, outer_args, complain);
25724 cands = lookup_add (guide, cands);
25725 }
25726 }
25727
25728 if (elided && !cands)
25729 {
25730 error ("cannot deduce template arguments for copy-initialization"
25731 " of %qT, as it has no non-explicit deduction guides or "
25732 "user-declared constructors", type);
25733 return error_mark_node;
25734 }
25735 else if (!cands && call == error_mark_node)
25736 {
25737 error ("cannot deduce template arguments of %qT, as it has no viable "
25738 "deduction guides", type);
25739 return error_mark_node;
25740 }
25741
25742 if (call == error_mark_node)
25743 {
25744 ++cp_unevaluated_operand;
25745 call = build_new_function_call (cands, &args, tf_decltype);
25746 --cp_unevaluated_operand;
25747 }
25748
25749 if (call == error_mark_node && (complain & tf_warning_or_error))
25750 {
25751 error ("class template argument deduction failed:");
25752
25753 ++cp_unevaluated_operand;
25754 call = build_new_function_call (cands, &args, complain | tf_decltype);
25755 --cp_unevaluated_operand;
25756
25757 if (elided)
25758 inform (input_location, "explicit deduction guides not considered "
25759 "for copy-initialization");
25760 }
25761
25762 release_tree_vector (args);
25763
25764 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25765 }
25766
25767 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25768 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25769
25770 tree
25771 do_auto_deduction (tree type, tree init, tree auto_node)
25772 {
25773 return do_auto_deduction (type, init, auto_node,
25774 tf_warning_or_error,
25775 adc_unspecified);
25776 }
25777
25778 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25779 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25780 The CONTEXT determines the context in which auto deduction is performed
25781 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25782 OUTER_TARGS are used during template argument deduction
25783 (context == adc_unify) to properly substitute the result, and is ignored
25784 in other contexts.
25785
25786 For partial-concept-ids, extra args may be appended to the list of deduced
25787 template arguments prior to determining constraint satisfaction. */
25788
25789 tree
25790 do_auto_deduction (tree type, tree init, tree auto_node,
25791 tsubst_flags_t complain, auto_deduction_context context,
25792 tree outer_targs, int flags)
25793 {
25794 tree targs;
25795
25796 if (init == error_mark_node)
25797 return error_mark_node;
25798
25799 if (init && type_dependent_expression_p (init)
25800 && context != adc_unify)
25801 /* Defining a subset of type-dependent expressions that we can deduce
25802 from ahead of time isn't worth the trouble. */
25803 return type;
25804
25805 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25806 /* C++17 class template argument deduction. */
25807 return do_class_deduction (type, tmpl, init, flags, complain);
25808
25809 if (TREE_TYPE (init) == NULL_TREE)
25810 /* Nothing we can do with this, even in deduction context. */
25811 return type;
25812
25813 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25814 with either a new invented type template parameter U or, if the
25815 initializer is a braced-init-list (8.5.4), with
25816 std::initializer_list<U>. */
25817 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25818 {
25819 if (!DIRECT_LIST_INIT_P (init))
25820 type = listify_autos (type, auto_node);
25821 else if (CONSTRUCTOR_NELTS (init) == 1)
25822 init = CONSTRUCTOR_ELT (init, 0)->value;
25823 else
25824 {
25825 if (complain & tf_warning_or_error)
25826 {
25827 if (permerror (input_location, "direct-list-initialization of "
25828 "%<auto%> requires exactly one element"))
25829 inform (input_location,
25830 "for deduction to %<std::initializer_list%>, use copy-"
25831 "list-initialization (i.e. add %<=%> before the %<{%>)");
25832 }
25833 type = listify_autos (type, auto_node);
25834 }
25835 }
25836
25837 if (type == error_mark_node)
25838 return error_mark_node;
25839
25840 init = resolve_nondeduced_context (init, complain);
25841
25842 if (context == adc_decomp_type
25843 && auto_node == type
25844 && init != error_mark_node
25845 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25846 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25847 and initializer has array type, deduce cv-qualified array type. */
25848 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25849 complain);
25850 else if (AUTO_IS_DECLTYPE (auto_node))
25851 {
25852 bool id = (DECL_P (init)
25853 || ((TREE_CODE (init) == COMPONENT_REF
25854 || TREE_CODE (init) == SCOPE_REF)
25855 && !REF_PARENTHESIZED_P (init)));
25856 targs = make_tree_vec (1);
25857 TREE_VEC_ELT (targs, 0)
25858 = finish_decltype_type (init, id, tf_warning_or_error);
25859 if (type != auto_node)
25860 {
25861 if (complain & tf_error)
25862 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25863 return error_mark_node;
25864 }
25865 }
25866 else
25867 {
25868 tree parms = build_tree_list (NULL_TREE, type);
25869 tree tparms;
25870
25871 if (flag_concepts)
25872 tparms = extract_autos (type);
25873 else
25874 {
25875 tparms = make_tree_vec (1);
25876 TREE_VEC_ELT (tparms, 0)
25877 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25878 }
25879
25880 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25881 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25882 DEDUCE_CALL, LOOKUP_NORMAL,
25883 NULL, /*explain_p=*/false);
25884 if (val > 0)
25885 {
25886 if (processing_template_decl)
25887 /* Try again at instantiation time. */
25888 return type;
25889 if (type && type != error_mark_node
25890 && (complain & tf_error))
25891 /* If type is error_mark_node a diagnostic must have been
25892 emitted by now. Also, having a mention to '<type error>'
25893 in the diagnostic is not really useful to the user. */
25894 {
25895 if (cfun && auto_node == current_function_auto_return_pattern
25896 && LAMBDA_FUNCTION_P (current_function_decl))
25897 error ("unable to deduce lambda return type from %qE", init);
25898 else
25899 error ("unable to deduce %qT from %qE", type, init);
25900 type_unification_real (tparms, targs, parms, &init, 1, 0,
25901 DEDUCE_CALL, LOOKUP_NORMAL,
25902 NULL, /*explain_p=*/true);
25903 }
25904 return error_mark_node;
25905 }
25906 }
25907
25908 /* Check any placeholder constraints against the deduced type. */
25909 if (flag_concepts && !processing_template_decl)
25910 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25911 {
25912 /* Use the deduced type to check the associated constraints. If we
25913 have a partial-concept-id, rebuild the argument list so that
25914 we check using the extra arguments. */
25915 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25916 tree cargs = CHECK_CONSTR_ARGS (constr);
25917 if (TREE_VEC_LENGTH (cargs) > 1)
25918 {
25919 cargs = copy_node (cargs);
25920 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25921 }
25922 else
25923 cargs = targs;
25924 if (!constraints_satisfied_p (constr, cargs))
25925 {
25926 if (complain & tf_warning_or_error)
25927 {
25928 switch (context)
25929 {
25930 case adc_unspecified:
25931 case adc_unify:
25932 error("placeholder constraints not satisfied");
25933 break;
25934 case adc_variable_type:
25935 case adc_decomp_type:
25936 error ("deduced initializer does not satisfy "
25937 "placeholder constraints");
25938 break;
25939 case adc_return_type:
25940 error ("deduced return type does not satisfy "
25941 "placeholder constraints");
25942 break;
25943 case adc_requirement:
25944 error ("deduced expression type does not satisfy "
25945 "placeholder constraints");
25946 break;
25947 }
25948 diagnose_constraints (input_location, constr, targs);
25949 }
25950 return error_mark_node;
25951 }
25952 }
25953
25954 if (processing_template_decl && context != adc_unify)
25955 outer_targs = current_template_args ();
25956 targs = add_to_template_args (outer_targs, targs);
25957 return tsubst (type, targs, complain, NULL_TREE);
25958 }
25959
25960 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25961 result. */
25962
25963 tree
25964 splice_late_return_type (tree type, tree late_return_type)
25965 {
25966 if (is_auto (type))
25967 {
25968 if (late_return_type)
25969 return late_return_type;
25970
25971 tree idx = get_template_parm_index (type);
25972 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25973 /* In an abbreviated function template we didn't know we were dealing
25974 with a function template when we saw the auto return type, so update
25975 it to have the correct level. */
25976 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25977 }
25978 return type;
25979 }
25980
25981 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25982 'decltype(auto)' or a deduced class template. */
25983
25984 bool
25985 is_auto (const_tree type)
25986 {
25987 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25988 && (TYPE_IDENTIFIER (type) == auto_identifier
25989 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25990 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25991 return true;
25992 else
25993 return false;
25994 }
25995
25996 /* for_each_template_parm callback for type_uses_auto. */
25997
25998 int
25999 is_auto_r (tree tp, void */*data*/)
26000 {
26001 return is_auto (tp);
26002 }
26003
26004 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26005 a use of `auto'. Returns NULL_TREE otherwise. */
26006
26007 tree
26008 type_uses_auto (tree type)
26009 {
26010 if (type == NULL_TREE)
26011 return NULL_TREE;
26012 else if (flag_concepts)
26013 {
26014 /* The Concepts TS allows multiple autos in one type-specifier; just
26015 return the first one we find, do_auto_deduction will collect all of
26016 them. */
26017 if (uses_template_parms (type))
26018 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26019 /*visited*/NULL, /*nondeduced*/true);
26020 else
26021 return NULL_TREE;
26022 }
26023 else
26024 return find_type_usage (type, is_auto);
26025 }
26026
26027 /* For a given template T, return the vector of typedefs referenced
26028 in T for which access check is needed at T instantiation time.
26029 T is either a FUNCTION_DECL or a RECORD_TYPE.
26030 Those typedefs were added to T by the function
26031 append_type_to_template_for_access_check. */
26032
26033 vec<qualified_typedef_usage_t, va_gc> *
26034 get_types_needing_access_check (tree t)
26035 {
26036 tree ti;
26037 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26038
26039 if (!t || t == error_mark_node)
26040 return NULL;
26041
26042 if (!(ti = get_template_info (t)))
26043 return NULL;
26044
26045 if (CLASS_TYPE_P (t)
26046 || TREE_CODE (t) == FUNCTION_DECL)
26047 {
26048 if (!TI_TEMPLATE (ti))
26049 return NULL;
26050
26051 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26052 }
26053
26054 return result;
26055 }
26056
26057 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26058 tied to T. That list of typedefs will be access checked at
26059 T instantiation time.
26060 T is either a FUNCTION_DECL or a RECORD_TYPE.
26061 TYPE_DECL is a TYPE_DECL node representing a typedef.
26062 SCOPE is the scope through which TYPE_DECL is accessed.
26063 LOCATION is the location of the usage point of TYPE_DECL.
26064
26065 This function is a subroutine of
26066 append_type_to_template_for_access_check. */
26067
26068 static void
26069 append_type_to_template_for_access_check_1 (tree t,
26070 tree type_decl,
26071 tree scope,
26072 location_t location)
26073 {
26074 qualified_typedef_usage_t typedef_usage;
26075 tree ti;
26076
26077 if (!t || t == error_mark_node)
26078 return;
26079
26080 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26081 || CLASS_TYPE_P (t))
26082 && type_decl
26083 && TREE_CODE (type_decl) == TYPE_DECL
26084 && scope);
26085
26086 if (!(ti = get_template_info (t)))
26087 return;
26088
26089 gcc_assert (TI_TEMPLATE (ti));
26090
26091 typedef_usage.typedef_decl = type_decl;
26092 typedef_usage.context = scope;
26093 typedef_usage.locus = location;
26094
26095 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26096 }
26097
26098 /* Append TYPE_DECL to the template TEMPL.
26099 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26100 At TEMPL instanciation time, TYPE_DECL will be checked to see
26101 if it can be accessed through SCOPE.
26102 LOCATION is the location of the usage point of TYPE_DECL.
26103
26104 e.g. consider the following code snippet:
26105
26106 class C
26107 {
26108 typedef int myint;
26109 };
26110
26111 template<class U> struct S
26112 {
26113 C::myint mi; // <-- usage point of the typedef C::myint
26114 };
26115
26116 S<char> s;
26117
26118 At S<char> instantiation time, we need to check the access of C::myint
26119 In other words, we need to check the access of the myint typedef through
26120 the C scope. For that purpose, this function will add the myint typedef
26121 and the scope C through which its being accessed to a list of typedefs
26122 tied to the template S. That list will be walked at template instantiation
26123 time and access check performed on each typedefs it contains.
26124 Note that this particular code snippet should yield an error because
26125 myint is private to C. */
26126
26127 void
26128 append_type_to_template_for_access_check (tree templ,
26129 tree type_decl,
26130 tree scope,
26131 location_t location)
26132 {
26133 qualified_typedef_usage_t *iter;
26134 unsigned i;
26135
26136 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26137
26138 /* Make sure we don't append the type to the template twice. */
26139 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26140 if (iter->typedef_decl == type_decl && scope == iter->context)
26141 return;
26142
26143 append_type_to_template_for_access_check_1 (templ, type_decl,
26144 scope, location);
26145 }
26146
26147 /* Convert the generic type parameters in PARM that match the types given in the
26148 range [START_IDX, END_IDX) from the current_template_parms into generic type
26149 packs. */
26150
26151 tree
26152 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26153 {
26154 tree current = current_template_parms;
26155 int depth = TMPL_PARMS_DEPTH (current);
26156 current = INNERMOST_TEMPLATE_PARMS (current);
26157 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26158
26159 for (int i = 0; i < start_idx; ++i)
26160 TREE_VEC_ELT (replacement, i)
26161 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26162
26163 for (int i = start_idx; i < end_idx; ++i)
26164 {
26165 /* Create a distinct parameter pack type from the current parm and add it
26166 to the replacement args to tsubst below into the generic function
26167 parameter. */
26168
26169 tree o = TREE_TYPE (TREE_VALUE
26170 (TREE_VEC_ELT (current, i)));
26171 tree t = copy_type (o);
26172 TEMPLATE_TYPE_PARM_INDEX (t)
26173 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26174 o, 0, 0, tf_none);
26175 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26176 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26177 TYPE_MAIN_VARIANT (t) = t;
26178 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26179 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26180 TREE_VEC_ELT (replacement, i) = t;
26181 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26182 }
26183
26184 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26185 TREE_VEC_ELT (replacement, i)
26186 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26187
26188 /* If there are more levels then build up the replacement with the outer
26189 template parms. */
26190 if (depth > 1)
26191 replacement = add_to_template_args (template_parms_to_args
26192 (TREE_CHAIN (current_template_parms)),
26193 replacement);
26194
26195 return tsubst (parm, replacement, tf_none, NULL_TREE);
26196 }
26197
26198 /* Entries in the decl_constraint hash table. */
26199 struct GTY((for_user)) constr_entry
26200 {
26201 tree decl;
26202 tree ci;
26203 };
26204
26205 /* Hashing function and equality for constraint entries. */
26206 struct constr_hasher : ggc_ptr_hash<constr_entry>
26207 {
26208 static hashval_t hash (constr_entry *e)
26209 {
26210 return (hashval_t)DECL_UID (e->decl);
26211 }
26212
26213 static bool equal (constr_entry *e1, constr_entry *e2)
26214 {
26215 return e1->decl == e2->decl;
26216 }
26217 };
26218
26219 /* A mapping from declarations to constraint information. Note that
26220 both templates and their underlying declarations are mapped to the
26221 same constraint information.
26222
26223 FIXME: This is defined in pt.c because garbage collection
26224 code is not being generated for constraint.cc. */
26225
26226 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26227
26228 /* Returns the template constraints of declaration T. If T is not
26229 constrained, return NULL_TREE. Note that T must be non-null. */
26230
26231 tree
26232 get_constraints (tree t)
26233 {
26234 if (!flag_concepts)
26235 return NULL_TREE;
26236
26237 gcc_assert (DECL_P (t));
26238 if (TREE_CODE (t) == TEMPLATE_DECL)
26239 t = DECL_TEMPLATE_RESULT (t);
26240 constr_entry elt = { t, NULL_TREE };
26241 constr_entry* found = decl_constraints->find (&elt);
26242 if (found)
26243 return found->ci;
26244 else
26245 return NULL_TREE;
26246 }
26247
26248 /* Associate the given constraint information CI with the declaration
26249 T. If T is a template, then the constraints are associated with
26250 its underlying declaration. Don't build associations if CI is
26251 NULL_TREE. */
26252
26253 void
26254 set_constraints (tree t, tree ci)
26255 {
26256 if (!ci)
26257 return;
26258 gcc_assert (t && flag_concepts);
26259 if (TREE_CODE (t) == TEMPLATE_DECL)
26260 t = DECL_TEMPLATE_RESULT (t);
26261 gcc_assert (!get_constraints (t));
26262 constr_entry elt = {t, ci};
26263 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26264 constr_entry* entry = ggc_alloc<constr_entry> ();
26265 *entry = elt;
26266 *slot = entry;
26267 }
26268
26269 /* Remove the associated constraints of the declaration T. */
26270
26271 void
26272 remove_constraints (tree t)
26273 {
26274 gcc_assert (DECL_P (t));
26275 if (TREE_CODE (t) == TEMPLATE_DECL)
26276 t = DECL_TEMPLATE_RESULT (t);
26277
26278 constr_entry elt = {t, NULL_TREE};
26279 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26280 if (slot)
26281 decl_constraints->clear_slot (slot);
26282 }
26283
26284 /* Memoized satisfaction results for declarations. This
26285 maps the pair (constraint_info, arguments) to the result computed
26286 by constraints_satisfied_p. */
26287
26288 struct GTY((for_user)) constraint_sat_entry
26289 {
26290 tree ci;
26291 tree args;
26292 tree result;
26293 };
26294
26295 /* Hashing function and equality for constraint entries. */
26296
26297 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26298 {
26299 static hashval_t hash (constraint_sat_entry *e)
26300 {
26301 hashval_t val = iterative_hash_object(e->ci, 0);
26302 return iterative_hash_template_arg (e->args, val);
26303 }
26304
26305 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26306 {
26307 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26308 }
26309 };
26310
26311 /* Memoized satisfaction results for concept checks. */
26312
26313 struct GTY((for_user)) concept_spec_entry
26314 {
26315 tree tmpl;
26316 tree args;
26317 tree result;
26318 };
26319
26320 /* Hashing function and equality for constraint entries. */
26321
26322 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26323 {
26324 static hashval_t hash (concept_spec_entry *e)
26325 {
26326 return hash_tmpl_and_args (e->tmpl, e->args);
26327 }
26328
26329 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26330 {
26331 ++comparing_specializations;
26332 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26333 --comparing_specializations;
26334 return eq;
26335 }
26336 };
26337
26338 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26339 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26340
26341 /* Search for a memoized satisfaction result. Returns one of the
26342 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26343
26344 tree
26345 lookup_constraint_satisfaction (tree ci, tree args)
26346 {
26347 constraint_sat_entry elt = { ci, args, NULL_TREE };
26348 constraint_sat_entry* found = constraint_memos->find (&elt);
26349 if (found)
26350 return found->result;
26351 else
26352 return NULL_TREE;
26353 }
26354
26355 /* Memoize the result of a satisfication test. Returns the saved result. */
26356
26357 tree
26358 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26359 {
26360 constraint_sat_entry elt = {ci, args, result};
26361 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26362 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26363 *entry = elt;
26364 *slot = entry;
26365 return result;
26366 }
26367
26368 /* Search for a memoized satisfaction result for a concept. */
26369
26370 tree
26371 lookup_concept_satisfaction (tree tmpl, tree args)
26372 {
26373 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26374 concept_spec_entry* found = concept_memos->find (&elt);
26375 if (found)
26376 return found->result;
26377 else
26378 return NULL_TREE;
26379 }
26380
26381 /* Memoize the result of a concept check. Returns the saved result. */
26382
26383 tree
26384 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26385 {
26386 concept_spec_entry elt = {tmpl, args, result};
26387 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26388 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26389 *entry = elt;
26390 *slot = entry;
26391 return result;
26392 }
26393
26394 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26395
26396 /* Returns a prior concept specialization. This returns the substituted
26397 and normalized constraints defined by the concept. */
26398
26399 tree
26400 get_concept_expansion (tree tmpl, tree args)
26401 {
26402 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26403 concept_spec_entry* found = concept_expansions->find (&elt);
26404 if (found)
26405 return found->result;
26406 else
26407 return NULL_TREE;
26408 }
26409
26410 /* Save a concept expansion for later. */
26411
26412 tree
26413 save_concept_expansion (tree tmpl, tree args, tree def)
26414 {
26415 concept_spec_entry elt = {tmpl, args, def};
26416 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26417 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26418 *entry = elt;
26419 *slot = entry;
26420 return def;
26421 }
26422
26423 static hashval_t
26424 hash_subsumption_args (tree t1, tree t2)
26425 {
26426 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26427 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26428 int val = 0;
26429 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26430 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26431 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26432 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26433 return val;
26434 }
26435
26436 /* Compare the constraints of two subsumption entries. The LEFT1 and
26437 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26438 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26439
26440 static bool
26441 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26442 {
26443 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26444 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26445 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26446 CHECK_CONSTR_ARGS (right1)))
26447 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26448 CHECK_CONSTR_ARGS (right2));
26449 return false;
26450 }
26451
26452 /* Key/value pair for learning and memoizing subsumption results. This
26453 associates a pair of check constraints (including arguments) with
26454 a boolean value indicating the result. */
26455
26456 struct GTY((for_user)) subsumption_entry
26457 {
26458 tree t1;
26459 tree t2;
26460 bool result;
26461 };
26462
26463 /* Hashing function and equality for constraint entries. */
26464
26465 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26466 {
26467 static hashval_t hash (subsumption_entry *e)
26468 {
26469 return hash_subsumption_args (e->t1, e->t2);
26470 }
26471
26472 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26473 {
26474 ++comparing_specializations;
26475 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26476 --comparing_specializations;
26477 return eq;
26478 }
26479 };
26480
26481 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26482
26483 /* Search for a previously cached subsumption result. */
26484
26485 bool*
26486 lookup_subsumption_result (tree t1, tree t2)
26487 {
26488 subsumption_entry elt = { t1, t2, false };
26489 subsumption_entry* found = subsumption_table->find (&elt);
26490 if (found)
26491 return &found->result;
26492 else
26493 return 0;
26494 }
26495
26496 /* Save a subsumption result. */
26497
26498 bool
26499 save_subsumption_result (tree t1, tree t2, bool result)
26500 {
26501 subsumption_entry elt = {t1, t2, result};
26502 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26503 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26504 *entry = elt;
26505 *slot = entry;
26506 return result;
26507 }
26508
26509 /* Set up the hash table for constraint association. */
26510
26511 void
26512 init_constraint_processing (void)
26513 {
26514 if (!flag_concepts)
26515 return;
26516
26517 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26518 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26519 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26520 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26521 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26522 }
26523
26524 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26525 0..N-1. */
26526
26527 void
26528 declare_integer_pack (void)
26529 {
26530 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26531 build_function_type_list (integer_type_node,
26532 integer_type_node,
26533 NULL_TREE),
26534 NULL_TREE, ECF_CONST);
26535 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26536 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26537 }
26538
26539 /* Set up the hash tables for template instantiations. */
26540
26541 void
26542 init_template_processing (void)
26543 {
26544 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26545 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26546
26547 if (cxx_dialect >= cxx11)
26548 declare_integer_pack ();
26549 }
26550
26551 /* Print stats about the template hash tables for -fstats. */
26552
26553 void
26554 print_template_statistics (void)
26555 {
26556 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26557 "%f collisions\n", (long) decl_specializations->size (),
26558 (long) decl_specializations->elements (),
26559 decl_specializations->collisions ());
26560 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26561 "%f collisions\n", (long) type_specializations->size (),
26562 (long) type_specializations->elements (),
26563 type_specializations->collisions ());
26564 }
26565
26566 #include "gt-cp-pt.h"