Remove Cilk Plus support.
[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_specialization_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 specialization, not including the template pattern. */
3291
3292 bool
3293 primary_template_specialization_p (const_tree t)
3294 {
3295 if (!t)
3296 return false;
3297
3298 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3299 return (DECL_LANG_SPECIFIC (t)
3300 && DECL_USE_TEMPLATE (t)
3301 && DECL_TEMPLATE_INFO (t)
3302 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3303 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3304 return (CLASSTYPE_TEMPLATE_INFO (t)
3305 && CLASSTYPE_USE_TEMPLATE (t)
3306 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3307 else if (alias_template_specialization_p (t))
3308 return true;
3309 return false;
3310 }
3311
3312 /* Return true if PARM is a template template parameter. */
3313
3314 bool
3315 template_template_parameter_p (const_tree parm)
3316 {
3317 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3318 }
3319
3320 /* Return true iff PARM is a DECL representing a type template
3321 parameter. */
3322
3323 bool
3324 template_type_parameter_p (const_tree parm)
3325 {
3326 return (parm
3327 && (TREE_CODE (parm) == TYPE_DECL
3328 || TREE_CODE (parm) == TEMPLATE_DECL)
3329 && DECL_TEMPLATE_PARM_P (parm));
3330 }
3331
3332 /* Return the template parameters of T if T is a
3333 primary template instantiation, NULL otherwise. */
3334
3335 tree
3336 get_primary_template_innermost_parameters (const_tree t)
3337 {
3338 tree parms = NULL, template_info = NULL;
3339
3340 if ((template_info = get_template_info (t))
3341 && primary_template_specialization_p (t))
3342 parms = INNERMOST_TEMPLATE_PARMS
3343 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3344
3345 return parms;
3346 }
3347
3348 /* Return the template parameters of the LEVELth level from the full list
3349 of template parameters PARMS. */
3350
3351 tree
3352 get_template_parms_at_level (tree parms, int level)
3353 {
3354 tree p;
3355 if (!parms
3356 || TREE_CODE (parms) != TREE_LIST
3357 || level > TMPL_PARMS_DEPTH (parms))
3358 return NULL_TREE;
3359
3360 for (p = parms; p; p = TREE_CHAIN (p))
3361 if (TMPL_PARMS_DEPTH (p) == level)
3362 return p;
3363
3364 return NULL_TREE;
3365 }
3366
3367 /* Returns the template arguments of T if T is a template instantiation,
3368 NULL otherwise. */
3369
3370 tree
3371 get_template_innermost_arguments (const_tree t)
3372 {
3373 tree args = NULL, template_info = NULL;
3374
3375 if ((template_info = get_template_info (t))
3376 && TI_ARGS (template_info))
3377 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3378
3379 return args;
3380 }
3381
3382 /* Return the argument pack elements of T if T is a template argument pack,
3383 NULL otherwise. */
3384
3385 tree
3386 get_template_argument_pack_elems (const_tree t)
3387 {
3388 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3389 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3390 return NULL;
3391
3392 return ARGUMENT_PACK_ARGS (t);
3393 }
3394
3395 /* True iff FN is a function representing a built-in variadic parameter
3396 pack. */
3397
3398 bool
3399 builtin_pack_fn_p (tree fn)
3400 {
3401 if (!fn
3402 || TREE_CODE (fn) != FUNCTION_DECL
3403 || !DECL_IS_BUILTIN (fn))
3404 return false;
3405
3406 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3407 return true;
3408
3409 return false;
3410 }
3411
3412 /* True iff CALL is a call to a function representing a built-in variadic
3413 parameter pack. */
3414
3415 static bool
3416 builtin_pack_call_p (tree call)
3417 {
3418 if (TREE_CODE (call) != CALL_EXPR)
3419 return false;
3420 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3421 }
3422
3423 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3424
3425 static tree
3426 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3427 tree in_decl)
3428 {
3429 tree ohi = CALL_EXPR_ARG (call, 0);
3430 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3431 false/*fn*/, true/*int_cst*/);
3432
3433 if (value_dependent_expression_p (hi))
3434 {
3435 if (hi != ohi)
3436 {
3437 call = copy_node (call);
3438 CALL_EXPR_ARG (call, 0) = hi;
3439 }
3440 tree ex = make_pack_expansion (call, complain);
3441 tree vec = make_tree_vec (1);
3442 TREE_VEC_ELT (vec, 0) = ex;
3443 return vec;
3444 }
3445 else
3446 {
3447 hi = cxx_constant_value (hi);
3448 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3449
3450 /* Calculate the largest value of len that won't make the size of the vec
3451 overflow an int. The compiler will exceed resource limits long before
3452 this, but it seems a decent place to diagnose. */
3453 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3454
3455 if (len < 0 || len > max)
3456 {
3457 if ((complain & tf_error)
3458 && hi != error_mark_node)
3459 error ("argument to __integer_pack must be between 0 and %d", max);
3460 return error_mark_node;
3461 }
3462
3463 tree vec = make_tree_vec (len);
3464
3465 for (int i = 0; i < len; ++i)
3466 TREE_VEC_ELT (vec, i) = size_int (i);
3467
3468 return vec;
3469 }
3470 }
3471
3472 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3473 CALL. */
3474
3475 static tree
3476 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3477 tree in_decl)
3478 {
3479 if (!builtin_pack_call_p (call))
3480 return NULL_TREE;
3481
3482 tree fn = CALL_EXPR_FN (call);
3483
3484 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3485 return expand_integer_pack (call, args, complain, in_decl);
3486
3487 return NULL_TREE;
3488 }
3489
3490 /* Structure used to track the progress of find_parameter_packs_r. */
3491 struct find_parameter_pack_data
3492 {
3493 /* TREE_LIST that will contain all of the parameter packs found by
3494 the traversal. */
3495 tree* parameter_packs;
3496
3497 /* Set of AST nodes that have been visited by the traversal. */
3498 hash_set<tree> *visited;
3499
3500 /* True iff we're making a type pack expansion. */
3501 bool type_pack_expansion_p;
3502 };
3503
3504 /* Identifies all of the argument packs that occur in a template
3505 argument and appends them to the TREE_LIST inside DATA, which is a
3506 find_parameter_pack_data structure. This is a subroutine of
3507 make_pack_expansion and uses_parameter_packs. */
3508 static tree
3509 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3510 {
3511 tree t = *tp;
3512 struct find_parameter_pack_data* ppd =
3513 (struct find_parameter_pack_data*)data;
3514 bool parameter_pack_p = false;
3515
3516 /* Handle type aliases/typedefs. */
3517 if (TYPE_ALIAS_P (t))
3518 {
3519 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3520 cp_walk_tree (&TI_ARGS (tinfo),
3521 &find_parameter_packs_r,
3522 ppd, ppd->visited);
3523 *walk_subtrees = 0;
3524 return NULL_TREE;
3525 }
3526
3527 /* Identify whether this is a parameter pack or not. */
3528 switch (TREE_CODE (t))
3529 {
3530 case TEMPLATE_PARM_INDEX:
3531 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3532 parameter_pack_p = true;
3533 break;
3534
3535 case TEMPLATE_TYPE_PARM:
3536 t = TYPE_MAIN_VARIANT (t);
3537 /* FALLTHRU */
3538 case TEMPLATE_TEMPLATE_PARM:
3539 /* If the placeholder appears in the decl-specifier-seq of a function
3540 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3541 is a pack expansion, the invented template parameter is a template
3542 parameter pack. */
3543 if (ppd->type_pack_expansion_p && is_auto (t))
3544 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3545 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3546 parameter_pack_p = true;
3547 break;
3548
3549 case FIELD_DECL:
3550 case PARM_DECL:
3551 if (DECL_PACK_P (t))
3552 {
3553 /* We don't want to walk into the type of a PARM_DECL,
3554 because we don't want to see the type parameter pack. */
3555 *walk_subtrees = 0;
3556 parameter_pack_p = true;
3557 }
3558 break;
3559
3560 /* Look through a lambda capture proxy to the field pack. */
3561 case VAR_DECL:
3562 if (DECL_HAS_VALUE_EXPR_P (t))
3563 {
3564 tree v = DECL_VALUE_EXPR (t);
3565 cp_walk_tree (&v,
3566 &find_parameter_packs_r,
3567 ppd, ppd->visited);
3568 *walk_subtrees = 0;
3569 }
3570 else if (variable_template_specialization_p (t))
3571 {
3572 cp_walk_tree (&DECL_TI_ARGS (t),
3573 find_parameter_packs_r,
3574 ppd, ppd->visited);
3575 *walk_subtrees = 0;
3576 }
3577 break;
3578
3579 case CALL_EXPR:
3580 if (builtin_pack_call_p (t))
3581 parameter_pack_p = true;
3582 break;
3583
3584 case BASES:
3585 parameter_pack_p = true;
3586 break;
3587 default:
3588 /* Not a parameter pack. */
3589 break;
3590 }
3591
3592 if (parameter_pack_p)
3593 {
3594 /* Add this parameter pack to the list. */
3595 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3596 }
3597
3598 if (TYPE_P (t))
3599 cp_walk_tree (&TYPE_CONTEXT (t),
3600 &find_parameter_packs_r, ppd, ppd->visited);
3601
3602 /* This switch statement will return immediately if we don't find a
3603 parameter pack. */
3604 switch (TREE_CODE (t))
3605 {
3606 case TEMPLATE_PARM_INDEX:
3607 return NULL_TREE;
3608
3609 case BOUND_TEMPLATE_TEMPLATE_PARM:
3610 /* Check the template itself. */
3611 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3612 &find_parameter_packs_r, ppd, ppd->visited);
3613 /* Check the template arguments. */
3614 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3615 ppd->visited);
3616 *walk_subtrees = 0;
3617 return NULL_TREE;
3618
3619 case TEMPLATE_TYPE_PARM:
3620 case TEMPLATE_TEMPLATE_PARM:
3621 return NULL_TREE;
3622
3623 case PARM_DECL:
3624 return NULL_TREE;
3625
3626 case DECL_EXPR:
3627 /* Ignore the declaration of a capture proxy for a parameter pack. */
3628 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3629 *walk_subtrees = 0;
3630 return NULL_TREE;
3631
3632 case RECORD_TYPE:
3633 if (TYPE_PTRMEMFUNC_P (t))
3634 return NULL_TREE;
3635 /* Fall through. */
3636
3637 case UNION_TYPE:
3638 case ENUMERAL_TYPE:
3639 if (TYPE_TEMPLATE_INFO (t))
3640 cp_walk_tree (&TYPE_TI_ARGS (t),
3641 &find_parameter_packs_r, ppd, ppd->visited);
3642
3643 *walk_subtrees = 0;
3644 return NULL_TREE;
3645
3646 case TEMPLATE_DECL:
3647 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3648 return NULL_TREE;
3649 gcc_fallthrough();
3650
3651 case CONSTRUCTOR:
3652 cp_walk_tree (&TREE_TYPE (t),
3653 &find_parameter_packs_r, ppd, ppd->visited);
3654 return NULL_TREE;
3655
3656 case TYPENAME_TYPE:
3657 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3658 ppd, ppd->visited);
3659 *walk_subtrees = 0;
3660 return NULL_TREE;
3661
3662 case TYPE_PACK_EXPANSION:
3663 case EXPR_PACK_EXPANSION:
3664 *walk_subtrees = 0;
3665 return NULL_TREE;
3666
3667 case INTEGER_TYPE:
3668 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3669 ppd, ppd->visited);
3670 *walk_subtrees = 0;
3671 return NULL_TREE;
3672
3673 case IDENTIFIER_NODE:
3674 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3675 ppd->visited);
3676 *walk_subtrees = 0;
3677 return NULL_TREE;
3678
3679 case LAMBDA_EXPR:
3680 {
3681 tree fn = lambda_function (t);
3682 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3683 ppd->visited);
3684 *walk_subtrees = 0;
3685 return NULL_TREE;
3686 }
3687
3688 case DECLTYPE_TYPE:
3689 {
3690 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3691 type_pack_expansion_p to false so that any placeholders
3692 within the expression don't get marked as parameter packs. */
3693 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3694 ppd->type_pack_expansion_p = false;
3695 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3696 ppd, ppd->visited);
3697 ppd->type_pack_expansion_p = type_pack_expansion_p;
3698 *walk_subtrees = 0;
3699 return NULL_TREE;
3700 }
3701
3702 default:
3703 return NULL_TREE;
3704 }
3705
3706 return NULL_TREE;
3707 }
3708
3709 /* Determines if the expression or type T uses any parameter packs. */
3710 bool
3711 uses_parameter_packs (tree t)
3712 {
3713 tree parameter_packs = NULL_TREE;
3714 struct find_parameter_pack_data ppd;
3715 ppd.parameter_packs = &parameter_packs;
3716 ppd.visited = new hash_set<tree>;
3717 ppd.type_pack_expansion_p = false;
3718 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3719 delete ppd.visited;
3720 return parameter_packs != NULL_TREE;
3721 }
3722
3723 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3724 representation a base-class initializer into a parameter pack
3725 expansion. If all goes well, the resulting node will be an
3726 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3727 respectively. */
3728 tree
3729 make_pack_expansion (tree arg, tsubst_flags_t complain)
3730 {
3731 tree result;
3732 tree parameter_packs = NULL_TREE;
3733 bool for_types = false;
3734 struct find_parameter_pack_data ppd;
3735
3736 if (!arg || arg == error_mark_node)
3737 return arg;
3738
3739 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3740 {
3741 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3742 class initializer. In this case, the TREE_PURPOSE will be a
3743 _TYPE node (representing the base class expansion we're
3744 initializing) and the TREE_VALUE will be a TREE_LIST
3745 containing the initialization arguments.
3746
3747 The resulting expansion looks somewhat different from most
3748 expansions. Rather than returning just one _EXPANSION, we
3749 return a TREE_LIST whose TREE_PURPOSE is a
3750 TYPE_PACK_EXPANSION containing the bases that will be
3751 initialized. The TREE_VALUE will be identical to the
3752 original TREE_VALUE, which is a list of arguments that will
3753 be passed to each base. We do not introduce any new pack
3754 expansion nodes into the TREE_VALUE (although it is possible
3755 that some already exist), because the TREE_PURPOSE and
3756 TREE_VALUE all need to be expanded together with the same
3757 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3758 resulting TREE_PURPOSE will mention the parameter packs in
3759 both the bases and the arguments to the bases. */
3760 tree purpose;
3761 tree value;
3762 tree parameter_packs = NULL_TREE;
3763
3764 /* Determine which parameter packs will be used by the base
3765 class expansion. */
3766 ppd.visited = new hash_set<tree>;
3767 ppd.parameter_packs = &parameter_packs;
3768 ppd.type_pack_expansion_p = true;
3769 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3770 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3771 &ppd, ppd.visited);
3772
3773 if (parameter_packs == NULL_TREE)
3774 {
3775 if (complain & tf_error)
3776 error ("base initializer expansion %qT contains no parameter packs",
3777 arg);
3778 delete ppd.visited;
3779 return error_mark_node;
3780 }
3781
3782 if (TREE_VALUE (arg) != void_type_node)
3783 {
3784 /* Collect the sets of parameter packs used in each of the
3785 initialization arguments. */
3786 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3787 {
3788 /* Determine which parameter packs will be expanded in this
3789 argument. */
3790 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3791 &ppd, ppd.visited);
3792 }
3793 }
3794
3795 delete ppd.visited;
3796
3797 /* Create the pack expansion type for the base type. */
3798 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3799 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3800 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3801 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3802
3803 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3804 they will rarely be compared to anything. */
3805 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3806
3807 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3808 }
3809
3810 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3811 for_types = true;
3812
3813 /* Build the PACK_EXPANSION_* node. */
3814 result = for_types
3815 ? cxx_make_type (TYPE_PACK_EXPANSION)
3816 : make_node (EXPR_PACK_EXPANSION);
3817 SET_PACK_EXPANSION_PATTERN (result, arg);
3818 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3819 {
3820 /* Propagate type and const-expression information. */
3821 TREE_TYPE (result) = TREE_TYPE (arg);
3822 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3823 /* Mark this read now, since the expansion might be length 0. */
3824 mark_exp_read (arg);
3825 }
3826 else
3827 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3828 they will rarely be compared to anything. */
3829 SET_TYPE_STRUCTURAL_EQUALITY (result);
3830
3831 /* Determine which parameter packs will be expanded. */
3832 ppd.parameter_packs = &parameter_packs;
3833 ppd.visited = new hash_set<tree>;
3834 ppd.type_pack_expansion_p = TYPE_P (arg);
3835 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3836 delete ppd.visited;
3837
3838 /* Make sure we found some parameter packs. */
3839 if (parameter_packs == NULL_TREE)
3840 {
3841 if (complain & tf_error)
3842 {
3843 if (TYPE_P (arg))
3844 error ("expansion pattern %qT contains no argument packs", arg);
3845 else
3846 error ("expansion pattern %qE contains no argument packs", arg);
3847 }
3848 return error_mark_node;
3849 }
3850 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3851
3852 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3853
3854 return result;
3855 }
3856
3857 /* Checks T for any "bare" parameter packs, which have not yet been
3858 expanded, and issues an error if any are found. This operation can
3859 only be done on full expressions or types (e.g., an expression
3860 statement, "if" condition, etc.), because we could have expressions like:
3861
3862 foo(f(g(h(args)))...)
3863
3864 where "args" is a parameter pack. check_for_bare_parameter_packs
3865 should not be called for the subexpressions args, h(args),
3866 g(h(args)), or f(g(h(args))), because we would produce erroneous
3867 error messages.
3868
3869 Returns TRUE and emits an error if there were bare parameter packs,
3870 returns FALSE otherwise. */
3871 bool
3872 check_for_bare_parameter_packs (tree t)
3873 {
3874 tree parameter_packs = NULL_TREE;
3875 struct find_parameter_pack_data ppd;
3876
3877 if (!processing_template_decl || !t || t == error_mark_node)
3878 return false;
3879
3880 /* A lambda might use a parameter pack from the containing context. */
3881 if (current_function_decl && LAMBDA_FUNCTION_P (current_function_decl))
3882 return false;
3883
3884 if (TREE_CODE (t) == TYPE_DECL)
3885 t = TREE_TYPE (t);
3886
3887 ppd.parameter_packs = &parameter_packs;
3888 ppd.visited = new hash_set<tree>;
3889 ppd.type_pack_expansion_p = false;
3890 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3891 delete ppd.visited;
3892
3893 if (parameter_packs)
3894 {
3895 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3896 error_at (loc, "parameter packs not expanded with %<...%>:");
3897 while (parameter_packs)
3898 {
3899 tree pack = TREE_VALUE (parameter_packs);
3900 tree name = NULL_TREE;
3901
3902 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3903 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3904 name = TYPE_NAME (pack);
3905 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3906 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3907 else if (TREE_CODE (pack) == CALL_EXPR)
3908 name = DECL_NAME (CALL_EXPR_FN (pack));
3909 else
3910 name = DECL_NAME (pack);
3911
3912 if (name)
3913 inform (loc, " %qD", name);
3914 else
3915 inform (loc, " <anonymous>");
3916
3917 parameter_packs = TREE_CHAIN (parameter_packs);
3918 }
3919
3920 return true;
3921 }
3922
3923 return false;
3924 }
3925
3926 /* Expand any parameter packs that occur in the template arguments in
3927 ARGS. */
3928 tree
3929 expand_template_argument_pack (tree args)
3930 {
3931 if (args == error_mark_node)
3932 return error_mark_node;
3933
3934 tree result_args = NULL_TREE;
3935 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3936 int num_result_args = -1;
3937 int non_default_args_count = -1;
3938
3939 /* First, determine if we need to expand anything, and the number of
3940 slots we'll need. */
3941 for (in_arg = 0; in_arg < nargs; ++in_arg)
3942 {
3943 tree arg = TREE_VEC_ELT (args, in_arg);
3944 if (arg == NULL_TREE)
3945 return args;
3946 if (ARGUMENT_PACK_P (arg))
3947 {
3948 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3949 if (num_result_args < 0)
3950 num_result_args = in_arg + num_packed;
3951 else
3952 num_result_args += num_packed;
3953 }
3954 else
3955 {
3956 if (num_result_args >= 0)
3957 num_result_args++;
3958 }
3959 }
3960
3961 /* If no expansion is necessary, we're done. */
3962 if (num_result_args < 0)
3963 return args;
3964
3965 /* Expand arguments. */
3966 result_args = make_tree_vec (num_result_args);
3967 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3968 non_default_args_count =
3969 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3970 for (in_arg = 0; in_arg < nargs; ++in_arg)
3971 {
3972 tree arg = TREE_VEC_ELT (args, in_arg);
3973 if (ARGUMENT_PACK_P (arg))
3974 {
3975 tree packed = ARGUMENT_PACK_ARGS (arg);
3976 int i, num_packed = TREE_VEC_LENGTH (packed);
3977 for (i = 0; i < num_packed; ++i, ++out_arg)
3978 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3979 if (non_default_args_count > 0)
3980 non_default_args_count += num_packed - 1;
3981 }
3982 else
3983 {
3984 TREE_VEC_ELT (result_args, out_arg) = arg;
3985 ++out_arg;
3986 }
3987 }
3988 if (non_default_args_count >= 0)
3989 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3990 return result_args;
3991 }
3992
3993 /* Checks if DECL shadows a template parameter.
3994
3995 [temp.local]: A template-parameter shall not be redeclared within its
3996 scope (including nested scopes).
3997
3998 Emits an error and returns TRUE if the DECL shadows a parameter,
3999 returns FALSE otherwise. */
4000
4001 bool
4002 check_template_shadow (tree decl)
4003 {
4004 tree olddecl;
4005
4006 /* If we're not in a template, we can't possibly shadow a template
4007 parameter. */
4008 if (!current_template_parms)
4009 return true;
4010
4011 /* Figure out what we're shadowing. */
4012 decl = OVL_FIRST (decl);
4013 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4014
4015 /* If there's no previous binding for this name, we're not shadowing
4016 anything, let alone a template parameter. */
4017 if (!olddecl)
4018 return true;
4019
4020 /* If we're not shadowing a template parameter, we're done. Note
4021 that OLDDECL might be an OVERLOAD (or perhaps even an
4022 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4023 node. */
4024 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4025 return true;
4026
4027 /* We check for decl != olddecl to avoid bogus errors for using a
4028 name inside a class. We check TPFI to avoid duplicate errors for
4029 inline member templates. */
4030 if (decl == olddecl
4031 || (DECL_TEMPLATE_PARM_P (decl)
4032 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4033 return true;
4034
4035 /* Don't complain about the injected class name, as we've already
4036 complained about the class itself. */
4037 if (DECL_SELF_REFERENCE_P (decl))
4038 return false;
4039
4040 if (DECL_TEMPLATE_PARM_P (decl))
4041 error ("declaration of template parameter %q+D shadows "
4042 "template parameter", decl);
4043 else
4044 error ("declaration of %q+#D shadows template parameter", decl);
4045 inform (DECL_SOURCE_LOCATION (olddecl),
4046 "template parameter %qD declared here", olddecl);
4047 return false;
4048 }
4049
4050 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4051 ORIG_LEVEL, DECL, and TYPE. */
4052
4053 static tree
4054 build_template_parm_index (int index,
4055 int level,
4056 int orig_level,
4057 tree decl,
4058 tree type)
4059 {
4060 tree t = make_node (TEMPLATE_PARM_INDEX);
4061 TEMPLATE_PARM_IDX (t) = index;
4062 TEMPLATE_PARM_LEVEL (t) = level;
4063 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4064 TEMPLATE_PARM_DECL (t) = decl;
4065 TREE_TYPE (t) = type;
4066 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4067 TREE_READONLY (t) = TREE_READONLY (decl);
4068
4069 return t;
4070 }
4071
4072 /* Find the canonical type parameter for the given template type
4073 parameter. Returns the canonical type parameter, which may be TYPE
4074 if no such parameter existed. */
4075
4076 static tree
4077 canonical_type_parameter (tree type)
4078 {
4079 tree list;
4080 int idx = TEMPLATE_TYPE_IDX (type);
4081 if (!canonical_template_parms)
4082 vec_alloc (canonical_template_parms, idx + 1);
4083
4084 if (canonical_template_parms->length () <= (unsigned) idx)
4085 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4086
4087 list = (*canonical_template_parms)[idx];
4088 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4089 list = TREE_CHAIN (list);
4090
4091 if (list)
4092 return TREE_VALUE (list);
4093 else
4094 {
4095 (*canonical_template_parms)[idx]
4096 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4097 return type;
4098 }
4099 }
4100
4101 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4102 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4103 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4104 new one is created. */
4105
4106 static tree
4107 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4108 tsubst_flags_t complain)
4109 {
4110 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4111 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4112 != TEMPLATE_PARM_LEVEL (index) - levels)
4113 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4114 {
4115 tree orig_decl = TEMPLATE_PARM_DECL (index);
4116 tree decl, t;
4117
4118 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4119 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4120 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4121 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4122 DECL_ARTIFICIAL (decl) = 1;
4123 SET_DECL_TEMPLATE_PARM_P (decl);
4124
4125 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4126 TEMPLATE_PARM_LEVEL (index) - levels,
4127 TEMPLATE_PARM_ORIG_LEVEL (index),
4128 decl, type);
4129 TEMPLATE_PARM_DESCENDANTS (index) = t;
4130 TEMPLATE_PARM_PARAMETER_PACK (t)
4131 = TEMPLATE_PARM_PARAMETER_PACK (index);
4132
4133 /* Template template parameters need this. */
4134 if (TREE_CODE (decl) == TEMPLATE_DECL)
4135 {
4136 DECL_TEMPLATE_RESULT (decl)
4137 = build_decl (DECL_SOURCE_LOCATION (decl),
4138 TYPE_DECL, DECL_NAME (decl), type);
4139 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4140 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4141 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4142 }
4143 }
4144
4145 return TEMPLATE_PARM_DESCENDANTS (index);
4146 }
4147
4148 /* Process information from new template parameter PARM and append it
4149 to the LIST being built. This new parameter is a non-type
4150 parameter iff IS_NON_TYPE is true. This new parameter is a
4151 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4152 is in PARM_LOC. */
4153
4154 tree
4155 process_template_parm (tree list, location_t parm_loc, tree parm,
4156 bool is_non_type, bool is_parameter_pack)
4157 {
4158 tree decl = 0;
4159 int idx = 0;
4160
4161 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4162 tree defval = TREE_PURPOSE (parm);
4163 tree constr = TREE_TYPE (parm);
4164
4165 if (list)
4166 {
4167 tree p = tree_last (list);
4168
4169 if (p && TREE_VALUE (p) != error_mark_node)
4170 {
4171 p = TREE_VALUE (p);
4172 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4173 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4174 else
4175 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4176 }
4177
4178 ++idx;
4179 }
4180
4181 if (is_non_type)
4182 {
4183 parm = TREE_VALUE (parm);
4184
4185 SET_DECL_TEMPLATE_PARM_P (parm);
4186
4187 if (TREE_TYPE (parm) != error_mark_node)
4188 {
4189 /* [temp.param]
4190
4191 The top-level cv-qualifiers on the template-parameter are
4192 ignored when determining its type. */
4193 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4194 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4195 TREE_TYPE (parm) = error_mark_node;
4196 else if (uses_parameter_packs (TREE_TYPE (parm))
4197 && !is_parameter_pack
4198 /* If we're in a nested template parameter list, the template
4199 template parameter could be a parameter pack. */
4200 && processing_template_parmlist == 1)
4201 {
4202 /* This template parameter is not a parameter pack, but it
4203 should be. Complain about "bare" parameter packs. */
4204 check_for_bare_parameter_packs (TREE_TYPE (parm));
4205
4206 /* Recover by calling this a parameter pack. */
4207 is_parameter_pack = true;
4208 }
4209 }
4210
4211 /* A template parameter is not modifiable. */
4212 TREE_CONSTANT (parm) = 1;
4213 TREE_READONLY (parm) = 1;
4214 decl = build_decl (parm_loc,
4215 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4216 TREE_CONSTANT (decl) = 1;
4217 TREE_READONLY (decl) = 1;
4218 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4219 = build_template_parm_index (idx, processing_template_decl,
4220 processing_template_decl,
4221 decl, TREE_TYPE (parm));
4222
4223 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4224 = is_parameter_pack;
4225 }
4226 else
4227 {
4228 tree t;
4229 parm = TREE_VALUE (TREE_VALUE (parm));
4230
4231 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4232 {
4233 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4234 /* This is for distinguishing between real templates and template
4235 template parameters */
4236 TREE_TYPE (parm) = t;
4237 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4238 decl = parm;
4239 }
4240 else
4241 {
4242 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4243 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4244 decl = build_decl (parm_loc,
4245 TYPE_DECL, parm, t);
4246 }
4247
4248 TYPE_NAME (t) = decl;
4249 TYPE_STUB_DECL (t) = decl;
4250 parm = decl;
4251 TEMPLATE_TYPE_PARM_INDEX (t)
4252 = build_template_parm_index (idx, processing_template_decl,
4253 processing_template_decl,
4254 decl, TREE_TYPE (parm));
4255 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4256 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4257 }
4258 DECL_ARTIFICIAL (decl) = 1;
4259 SET_DECL_TEMPLATE_PARM_P (decl);
4260
4261 /* Build requirements for the type/template parameter.
4262 This must be done after SET_DECL_TEMPLATE_PARM_P or
4263 process_template_parm could fail. */
4264 tree reqs = finish_shorthand_constraint (parm, constr);
4265
4266 pushdecl (decl);
4267
4268 /* Build the parameter node linking the parameter declaration,
4269 its default argument (if any), and its constraints (if any). */
4270 parm = build_tree_list (defval, parm);
4271 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4272
4273 return chainon (list, parm);
4274 }
4275
4276 /* The end of a template parameter list has been reached. Process the
4277 tree list into a parameter vector, converting each parameter into a more
4278 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4279 as PARM_DECLs. */
4280
4281 tree
4282 end_template_parm_list (tree parms)
4283 {
4284 int nparms;
4285 tree parm, next;
4286 tree saved_parmlist = make_tree_vec (list_length (parms));
4287
4288 /* Pop the dummy parameter level and add the real one. */
4289 current_template_parms = TREE_CHAIN (current_template_parms);
4290
4291 current_template_parms
4292 = tree_cons (size_int (processing_template_decl),
4293 saved_parmlist, current_template_parms);
4294
4295 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4296 {
4297 next = TREE_CHAIN (parm);
4298 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4299 TREE_CHAIN (parm) = NULL_TREE;
4300 }
4301
4302 --processing_template_parmlist;
4303
4304 return saved_parmlist;
4305 }
4306
4307 // Explicitly indicate the end of the template parameter list. We assume
4308 // that the current template parameters have been constructed and/or
4309 // managed explicitly, as when creating new template template parameters
4310 // from a shorthand constraint.
4311 void
4312 end_template_parm_list ()
4313 {
4314 --processing_template_parmlist;
4315 }
4316
4317 /* end_template_decl is called after a template declaration is seen. */
4318
4319 void
4320 end_template_decl (void)
4321 {
4322 reset_specialization ();
4323
4324 if (! processing_template_decl)
4325 return;
4326
4327 /* This matches the pushlevel in begin_template_parm_list. */
4328 finish_scope ();
4329
4330 --processing_template_decl;
4331 current_template_parms = TREE_CHAIN (current_template_parms);
4332 }
4333
4334 /* Takes a TREE_LIST representing a template parameter and convert it
4335 into an argument suitable to be passed to the type substitution
4336 functions. Note that If the TREE_LIST contains an error_mark
4337 node, the returned argument is error_mark_node. */
4338
4339 tree
4340 template_parm_to_arg (tree t)
4341 {
4342
4343 if (t == NULL_TREE
4344 || TREE_CODE (t) != TREE_LIST)
4345 return t;
4346
4347 if (error_operand_p (TREE_VALUE (t)))
4348 return error_mark_node;
4349
4350 t = TREE_VALUE (t);
4351
4352 if (TREE_CODE (t) == TYPE_DECL
4353 || TREE_CODE (t) == TEMPLATE_DECL)
4354 {
4355 t = TREE_TYPE (t);
4356
4357 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4358 {
4359 /* Turn this argument into a TYPE_ARGUMENT_PACK
4360 with a single element, which expands T. */
4361 tree vec = make_tree_vec (1);
4362 if (CHECKING_P)
4363 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4364
4365 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4366
4367 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4368 SET_ARGUMENT_PACK_ARGS (t, vec);
4369 }
4370 }
4371 else
4372 {
4373 t = DECL_INITIAL (t);
4374
4375 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4376 {
4377 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4378 with a single element, which expands T. */
4379 tree vec = make_tree_vec (1);
4380 if (CHECKING_P)
4381 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4382
4383 t = convert_from_reference (t);
4384 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4385
4386 t = make_node (NONTYPE_ARGUMENT_PACK);
4387 SET_ARGUMENT_PACK_ARGS (t, vec);
4388 }
4389 else
4390 t = convert_from_reference (t);
4391 }
4392 return t;
4393 }
4394
4395 /* Given a single level of template parameters (a TREE_VEC), return it
4396 as a set of template arguments. */
4397
4398 static tree
4399 template_parms_level_to_args (tree parms)
4400 {
4401 tree a = copy_node (parms);
4402 TREE_TYPE (a) = NULL_TREE;
4403 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4404 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4405
4406 if (CHECKING_P)
4407 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4408
4409 return a;
4410 }
4411
4412 /* Given a set of template parameters, return them as a set of template
4413 arguments. The template parameters are represented as a TREE_VEC, in
4414 the form documented in cp-tree.h for template arguments. */
4415
4416 static tree
4417 template_parms_to_args (tree parms)
4418 {
4419 tree header;
4420 tree args = NULL_TREE;
4421 int length = TMPL_PARMS_DEPTH (parms);
4422 int l = length;
4423
4424 /* If there is only one level of template parameters, we do not
4425 create a TREE_VEC of TREE_VECs. Instead, we return a single
4426 TREE_VEC containing the arguments. */
4427 if (length > 1)
4428 args = make_tree_vec (length);
4429
4430 for (header = parms; header; header = TREE_CHAIN (header))
4431 {
4432 tree a = template_parms_level_to_args (TREE_VALUE (header));
4433
4434 if (length > 1)
4435 TREE_VEC_ELT (args, --l) = a;
4436 else
4437 args = a;
4438 }
4439
4440 return args;
4441 }
4442
4443 /* Within the declaration of a template, return the currently active
4444 template parameters as an argument TREE_VEC. */
4445
4446 static tree
4447 current_template_args (void)
4448 {
4449 return template_parms_to_args (current_template_parms);
4450 }
4451
4452 /* Update the declared TYPE by doing any lookups which were thought to be
4453 dependent, but are not now that we know the SCOPE of the declarator. */
4454
4455 tree
4456 maybe_update_decl_type (tree orig_type, tree scope)
4457 {
4458 tree type = orig_type;
4459
4460 if (type == NULL_TREE)
4461 return type;
4462
4463 if (TREE_CODE (orig_type) == TYPE_DECL)
4464 type = TREE_TYPE (type);
4465
4466 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4467 && dependent_type_p (type)
4468 /* Don't bother building up the args in this case. */
4469 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4470 {
4471 /* tsubst in the args corresponding to the template parameters,
4472 including auto if present. Most things will be unchanged, but
4473 make_typename_type and tsubst_qualified_id will resolve
4474 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4475 tree args = current_template_args ();
4476 tree auto_node = type_uses_auto (type);
4477 tree pushed;
4478 if (auto_node)
4479 {
4480 tree auto_vec = make_tree_vec (1);
4481 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4482 args = add_to_template_args (args, auto_vec);
4483 }
4484 pushed = push_scope (scope);
4485 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4486 if (pushed)
4487 pop_scope (scope);
4488 }
4489
4490 if (type == error_mark_node)
4491 return orig_type;
4492
4493 if (TREE_CODE (orig_type) == TYPE_DECL)
4494 {
4495 if (same_type_p (type, TREE_TYPE (orig_type)))
4496 type = orig_type;
4497 else
4498 type = TYPE_NAME (type);
4499 }
4500 return type;
4501 }
4502
4503 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4504 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4505 the new template is a member template. */
4506
4507 tree
4508 build_template_decl (tree decl, tree parms, bool member_template_p)
4509 {
4510 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4511 DECL_TEMPLATE_PARMS (tmpl) = parms;
4512 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4513 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4514 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4515
4516 return tmpl;
4517 }
4518
4519 struct template_parm_data
4520 {
4521 /* The level of the template parameters we are currently
4522 processing. */
4523 int level;
4524
4525 /* The index of the specialization argument we are currently
4526 processing. */
4527 int current_arg;
4528
4529 /* An array whose size is the number of template parameters. The
4530 elements are nonzero if the parameter has been used in any one
4531 of the arguments processed so far. */
4532 int* parms;
4533
4534 /* An array whose size is the number of template arguments. The
4535 elements are nonzero if the argument makes use of template
4536 parameters of this level. */
4537 int* arg_uses_template_parms;
4538 };
4539
4540 /* Subroutine of push_template_decl used to see if each template
4541 parameter in a partial specialization is used in the explicit
4542 argument list. If T is of the LEVEL given in DATA (which is
4543 treated as a template_parm_data*), then DATA->PARMS is marked
4544 appropriately. */
4545
4546 static int
4547 mark_template_parm (tree t, void* data)
4548 {
4549 int level;
4550 int idx;
4551 struct template_parm_data* tpd = (struct template_parm_data*) data;
4552
4553 template_parm_level_and_index (t, &level, &idx);
4554
4555 if (level == tpd->level)
4556 {
4557 tpd->parms[idx] = 1;
4558 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4559 }
4560
4561 /* In C++17 the type of a non-type argument is a deduced context. */
4562 if (cxx_dialect >= cxx17
4563 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4564 for_each_template_parm (TREE_TYPE (t),
4565 &mark_template_parm,
4566 data,
4567 NULL,
4568 /*include_nondeduced_p=*/false);
4569
4570 /* Return zero so that for_each_template_parm will continue the
4571 traversal of the tree; we want to mark *every* template parm. */
4572 return 0;
4573 }
4574
4575 /* Process the partial specialization DECL. */
4576
4577 static tree
4578 process_partial_specialization (tree decl)
4579 {
4580 tree type = TREE_TYPE (decl);
4581 tree tinfo = get_template_info (decl);
4582 tree maintmpl = TI_TEMPLATE (tinfo);
4583 tree specargs = TI_ARGS (tinfo);
4584 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4585 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4586 tree inner_parms;
4587 tree inst;
4588 int nargs = TREE_VEC_LENGTH (inner_args);
4589 int ntparms;
4590 int i;
4591 bool did_error_intro = false;
4592 struct template_parm_data tpd;
4593 struct template_parm_data tpd2;
4594
4595 gcc_assert (current_template_parms);
4596
4597 /* A concept cannot be specialized. */
4598 if (flag_concepts && variable_concept_p (maintmpl))
4599 {
4600 error ("specialization of variable concept %q#D", maintmpl);
4601 return error_mark_node;
4602 }
4603
4604 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4605 ntparms = TREE_VEC_LENGTH (inner_parms);
4606
4607 /* We check that each of the template parameters given in the
4608 partial specialization is used in the argument list to the
4609 specialization. For example:
4610
4611 template <class T> struct S;
4612 template <class T> struct S<T*>;
4613
4614 The second declaration is OK because `T*' uses the template
4615 parameter T, whereas
4616
4617 template <class T> struct S<int>;
4618
4619 is no good. Even trickier is:
4620
4621 template <class T>
4622 struct S1
4623 {
4624 template <class U>
4625 struct S2;
4626 template <class U>
4627 struct S2<T>;
4628 };
4629
4630 The S2<T> declaration is actually invalid; it is a
4631 full-specialization. Of course,
4632
4633 template <class U>
4634 struct S2<T (*)(U)>;
4635
4636 or some such would have been OK. */
4637 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4638 tpd.parms = XALLOCAVEC (int, ntparms);
4639 memset (tpd.parms, 0, sizeof (int) * ntparms);
4640
4641 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4642 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4643 for (i = 0; i < nargs; ++i)
4644 {
4645 tpd.current_arg = i;
4646 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4647 &mark_template_parm,
4648 &tpd,
4649 NULL,
4650 /*include_nondeduced_p=*/false);
4651 }
4652 for (i = 0; i < ntparms; ++i)
4653 if (tpd.parms[i] == 0)
4654 {
4655 /* One of the template parms was not used in a deduced context in the
4656 specialization. */
4657 if (!did_error_intro)
4658 {
4659 error ("template parameters not deducible in "
4660 "partial specialization:");
4661 did_error_intro = true;
4662 }
4663
4664 inform (input_location, " %qD",
4665 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4666 }
4667
4668 if (did_error_intro)
4669 return error_mark_node;
4670
4671 /* [temp.class.spec]
4672
4673 The argument list of the specialization shall not be identical to
4674 the implicit argument list of the primary template. */
4675 tree main_args
4676 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4677 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4678 && (!flag_concepts
4679 || !strictly_subsumes (current_template_constraints (),
4680 get_constraints (maintmpl))))
4681 {
4682 if (!flag_concepts)
4683 error ("partial specialization %q+D does not specialize "
4684 "any template arguments", decl);
4685 else
4686 error ("partial specialization %q+D does not specialize any "
4687 "template arguments and is not more constrained than", decl);
4688 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4689 }
4690
4691 /* A partial specialization that replaces multiple parameters of the
4692 primary template with a pack expansion is less specialized for those
4693 parameters. */
4694 if (nargs < DECL_NTPARMS (maintmpl))
4695 {
4696 error ("partial specialization is not more specialized than the "
4697 "primary template because it replaces multiple parameters "
4698 "with a pack expansion");
4699 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4700 /* Avoid crash in process_partial_specialization. */
4701 return decl;
4702 }
4703
4704 /* If we aren't in a dependent class, we can actually try deduction. */
4705 else if (tpd.level == 1
4706 /* FIXME we should be able to handle a partial specialization of a
4707 partial instantiation, but currently we can't (c++/41727). */
4708 && TMPL_ARGS_DEPTH (specargs) == 1
4709 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4710 {
4711 if (permerror (input_location, "partial specialization %qD is not "
4712 "more specialized than", decl))
4713 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4714 maintmpl);
4715 }
4716
4717 /* [temp.class.spec]
4718
4719 A partially specialized non-type argument expression shall not
4720 involve template parameters of the partial specialization except
4721 when the argument expression is a simple identifier.
4722
4723 The type of a template parameter corresponding to a specialized
4724 non-type argument shall not be dependent on a parameter of the
4725 specialization.
4726
4727 Also, we verify that pack expansions only occur at the
4728 end of the argument list. */
4729 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4730 tpd2.parms = 0;
4731 for (i = 0; i < nargs; ++i)
4732 {
4733 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4734 tree arg = TREE_VEC_ELT (inner_args, i);
4735 tree packed_args = NULL_TREE;
4736 int j, len = 1;
4737
4738 if (ARGUMENT_PACK_P (arg))
4739 {
4740 /* Extract the arguments from the argument pack. We'll be
4741 iterating over these in the following loop. */
4742 packed_args = ARGUMENT_PACK_ARGS (arg);
4743 len = TREE_VEC_LENGTH (packed_args);
4744 }
4745
4746 for (j = 0; j < len; j++)
4747 {
4748 if (packed_args)
4749 /* Get the Jth argument in the parameter pack. */
4750 arg = TREE_VEC_ELT (packed_args, j);
4751
4752 if (PACK_EXPANSION_P (arg))
4753 {
4754 /* Pack expansions must come at the end of the
4755 argument list. */
4756 if ((packed_args && j < len - 1)
4757 || (!packed_args && i < nargs - 1))
4758 {
4759 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4760 error ("parameter pack argument %qE must be at the "
4761 "end of the template argument list", arg);
4762 else
4763 error ("parameter pack argument %qT must be at the "
4764 "end of the template argument list", arg);
4765 }
4766 }
4767
4768 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4769 /* We only care about the pattern. */
4770 arg = PACK_EXPANSION_PATTERN (arg);
4771
4772 if (/* These first two lines are the `non-type' bit. */
4773 !TYPE_P (arg)
4774 && TREE_CODE (arg) != TEMPLATE_DECL
4775 /* This next two lines are the `argument expression is not just a
4776 simple identifier' condition and also the `specialized
4777 non-type argument' bit. */
4778 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4779 && !(REFERENCE_REF_P (arg)
4780 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4781 {
4782 if ((!packed_args && tpd.arg_uses_template_parms[i])
4783 || (packed_args && uses_template_parms (arg)))
4784 error ("template argument %qE involves template parameter(s)",
4785 arg);
4786 else
4787 {
4788 /* Look at the corresponding template parameter,
4789 marking which template parameters its type depends
4790 upon. */
4791 tree type = TREE_TYPE (parm);
4792
4793 if (!tpd2.parms)
4794 {
4795 /* We haven't yet initialized TPD2. Do so now. */
4796 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4797 /* The number of parameters here is the number in the
4798 main template, which, as checked in the assertion
4799 above, is NARGS. */
4800 tpd2.parms = XALLOCAVEC (int, nargs);
4801 tpd2.level =
4802 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4803 }
4804
4805 /* Mark the template parameters. But this time, we're
4806 looking for the template parameters of the main
4807 template, not in the specialization. */
4808 tpd2.current_arg = i;
4809 tpd2.arg_uses_template_parms[i] = 0;
4810 memset (tpd2.parms, 0, sizeof (int) * nargs);
4811 for_each_template_parm (type,
4812 &mark_template_parm,
4813 &tpd2,
4814 NULL,
4815 /*include_nondeduced_p=*/false);
4816
4817 if (tpd2.arg_uses_template_parms [i])
4818 {
4819 /* The type depended on some template parameters.
4820 If they are fully specialized in the
4821 specialization, that's OK. */
4822 int j;
4823 int count = 0;
4824 for (j = 0; j < nargs; ++j)
4825 if (tpd2.parms[j] != 0
4826 && tpd.arg_uses_template_parms [j])
4827 ++count;
4828 if (count != 0)
4829 error_n (input_location, count,
4830 "type %qT of template argument %qE depends "
4831 "on a template parameter",
4832 "type %qT of template argument %qE depends "
4833 "on template parameters",
4834 type,
4835 arg);
4836 }
4837 }
4838 }
4839 }
4840 }
4841
4842 /* We should only get here once. */
4843 if (TREE_CODE (decl) == TYPE_DECL)
4844 gcc_assert (!COMPLETE_TYPE_P (type));
4845
4846 // Build the template decl.
4847 tree tmpl = build_template_decl (decl, current_template_parms,
4848 DECL_MEMBER_TEMPLATE_P (maintmpl));
4849 TREE_TYPE (tmpl) = type;
4850 DECL_TEMPLATE_RESULT (tmpl) = decl;
4851 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4852 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4853 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4854
4855 /* Give template template parms a DECL_CONTEXT of the template
4856 for which they are a parameter. */
4857 for (i = 0; i < ntparms; ++i)
4858 {
4859 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4860 if (TREE_CODE (parm) == TEMPLATE_DECL)
4861 DECL_CONTEXT (parm) = tmpl;
4862 }
4863
4864 if (VAR_P (decl))
4865 /* We didn't register this in check_explicit_specialization so we could
4866 wait until the constraints were set. */
4867 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4868 else
4869 associate_classtype_constraints (type);
4870
4871 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4872 = tree_cons (specargs, tmpl,
4873 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4874 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4875
4876 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4877 inst = TREE_CHAIN (inst))
4878 {
4879 tree instance = TREE_VALUE (inst);
4880 if (TYPE_P (instance)
4881 ? (COMPLETE_TYPE_P (instance)
4882 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4883 : DECL_TEMPLATE_INSTANTIATION (instance))
4884 {
4885 tree spec = most_specialized_partial_spec (instance, tf_none);
4886 tree inst_decl = (DECL_P (instance)
4887 ? instance : TYPE_NAME (instance));
4888 if (!spec)
4889 /* OK */;
4890 else if (spec == error_mark_node)
4891 permerror (input_location,
4892 "declaration of %qD ambiguates earlier template "
4893 "instantiation for %qD", decl, inst_decl);
4894 else if (TREE_VALUE (spec) == tmpl)
4895 permerror (input_location,
4896 "partial specialization of %qD after instantiation "
4897 "of %qD", decl, inst_decl);
4898 }
4899 }
4900
4901 return decl;
4902 }
4903
4904 /* PARM is a template parameter of some form; return the corresponding
4905 TEMPLATE_PARM_INDEX. */
4906
4907 static tree
4908 get_template_parm_index (tree parm)
4909 {
4910 if (TREE_CODE (parm) == PARM_DECL
4911 || TREE_CODE (parm) == CONST_DECL)
4912 parm = DECL_INITIAL (parm);
4913 else if (TREE_CODE (parm) == TYPE_DECL
4914 || TREE_CODE (parm) == TEMPLATE_DECL)
4915 parm = TREE_TYPE (parm);
4916 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4917 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4918 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4919 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4920 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4921 return parm;
4922 }
4923
4924 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4925 parameter packs used by the template parameter PARM. */
4926
4927 static void
4928 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4929 {
4930 /* A type parm can't refer to another parm. */
4931 if (TREE_CODE (parm) == TYPE_DECL)
4932 return;
4933 else if (TREE_CODE (parm) == PARM_DECL)
4934 {
4935 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4936 ppd, ppd->visited);
4937 return;
4938 }
4939
4940 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4941
4942 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4943 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4944 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4945 }
4946
4947 /* PARM is a template parameter pack. Return any parameter packs used in
4948 its type or the type of any of its template parameters. If there are
4949 any such packs, it will be instantiated into a fixed template parameter
4950 list by partial instantiation rather than be fully deduced. */
4951
4952 tree
4953 fixed_parameter_pack_p (tree parm)
4954 {
4955 /* This can only be true in a member template. */
4956 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4957 return NULL_TREE;
4958 /* This can only be true for a parameter pack. */
4959 if (!template_parameter_pack_p (parm))
4960 return NULL_TREE;
4961 /* A type parm can't refer to another parm. */
4962 if (TREE_CODE (parm) == TYPE_DECL)
4963 return NULL_TREE;
4964
4965 tree parameter_packs = NULL_TREE;
4966 struct find_parameter_pack_data ppd;
4967 ppd.parameter_packs = &parameter_packs;
4968 ppd.visited = new hash_set<tree>;
4969 ppd.type_pack_expansion_p = false;
4970
4971 fixed_parameter_pack_p_1 (parm, &ppd);
4972
4973 delete ppd.visited;
4974 return parameter_packs;
4975 }
4976
4977 /* Check that a template declaration's use of default arguments and
4978 parameter packs is not invalid. Here, PARMS are the template
4979 parameters. IS_PRIMARY is true if DECL is the thing declared by
4980 a primary template. IS_PARTIAL is true if DECL is a partial
4981 specialization.
4982
4983 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4984 declaration (but not a definition); 1 indicates a declaration, 2
4985 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4986 emitted for extraneous default arguments.
4987
4988 Returns TRUE if there were no errors found, FALSE otherwise. */
4989
4990 bool
4991 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4992 bool is_partial, int is_friend_decl)
4993 {
4994 const char *msg;
4995 int last_level_to_check;
4996 tree parm_level;
4997 bool no_errors = true;
4998
4999 /* [temp.param]
5000
5001 A default template-argument shall not be specified in a
5002 function template declaration or a function template definition, nor
5003 in the template-parameter-list of the definition of a member of a
5004 class template. */
5005
5006 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5007 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5008 /* You can't have a function template declaration in a local
5009 scope, nor you can you define a member of a class template in a
5010 local scope. */
5011 return true;
5012
5013 if ((TREE_CODE (decl) == TYPE_DECL
5014 && TREE_TYPE (decl)
5015 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5016 || (TREE_CODE (decl) == FUNCTION_DECL
5017 && LAMBDA_FUNCTION_P (decl)))
5018 /* A lambda doesn't have an explicit declaration; don't complain
5019 about the parms of the enclosing class. */
5020 return true;
5021
5022 if (current_class_type
5023 && !TYPE_BEING_DEFINED (current_class_type)
5024 && DECL_LANG_SPECIFIC (decl)
5025 && DECL_DECLARES_FUNCTION_P (decl)
5026 /* If this is either a friend defined in the scope of the class
5027 or a member function. */
5028 && (DECL_FUNCTION_MEMBER_P (decl)
5029 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5030 : DECL_FRIEND_CONTEXT (decl)
5031 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5032 : false)
5033 /* And, if it was a member function, it really was defined in
5034 the scope of the class. */
5035 && (!DECL_FUNCTION_MEMBER_P (decl)
5036 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5037 /* We already checked these parameters when the template was
5038 declared, so there's no need to do it again now. This function
5039 was defined in class scope, but we're processing its body now
5040 that the class is complete. */
5041 return true;
5042
5043 /* Core issue 226 (C++0x only): the following only applies to class
5044 templates. */
5045 if (is_primary
5046 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5047 {
5048 /* [temp.param]
5049
5050 If a template-parameter has a default template-argument, all
5051 subsequent template-parameters shall have a default
5052 template-argument supplied. */
5053 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5054 {
5055 tree inner_parms = TREE_VALUE (parm_level);
5056 int ntparms = TREE_VEC_LENGTH (inner_parms);
5057 int seen_def_arg_p = 0;
5058 int i;
5059
5060 for (i = 0; i < ntparms; ++i)
5061 {
5062 tree parm = TREE_VEC_ELT (inner_parms, i);
5063
5064 if (parm == error_mark_node)
5065 continue;
5066
5067 if (TREE_PURPOSE (parm))
5068 seen_def_arg_p = 1;
5069 else if (seen_def_arg_p
5070 && !template_parameter_pack_p (TREE_VALUE (parm)))
5071 {
5072 error ("no default argument for %qD", TREE_VALUE (parm));
5073 /* For better subsequent error-recovery, we indicate that
5074 there should have been a default argument. */
5075 TREE_PURPOSE (parm) = error_mark_node;
5076 no_errors = false;
5077 }
5078 else if (!is_partial
5079 && !is_friend_decl
5080 /* Don't complain about an enclosing partial
5081 specialization. */
5082 && parm_level == parms
5083 && TREE_CODE (decl) == TYPE_DECL
5084 && i < ntparms - 1
5085 && template_parameter_pack_p (TREE_VALUE (parm))
5086 /* A fixed parameter pack will be partially
5087 instantiated into a fixed length list. */
5088 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5089 {
5090 /* A primary class template can only have one
5091 parameter pack, at the end of the template
5092 parameter list. */
5093
5094 error ("parameter pack %q+D must be at the end of the"
5095 " template parameter list", TREE_VALUE (parm));
5096
5097 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5098 = error_mark_node;
5099 no_errors = false;
5100 }
5101 }
5102 }
5103 }
5104
5105 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5106 || is_partial
5107 || !is_primary
5108 || is_friend_decl)
5109 /* For an ordinary class template, default template arguments are
5110 allowed at the innermost level, e.g.:
5111 template <class T = int>
5112 struct S {};
5113 but, in a partial specialization, they're not allowed even
5114 there, as we have in [temp.class.spec]:
5115
5116 The template parameter list of a specialization shall not
5117 contain default template argument values.
5118
5119 So, for a partial specialization, or for a function template
5120 (in C++98/C++03), we look at all of them. */
5121 ;
5122 else
5123 /* But, for a primary class template that is not a partial
5124 specialization we look at all template parameters except the
5125 innermost ones. */
5126 parms = TREE_CHAIN (parms);
5127
5128 /* Figure out what error message to issue. */
5129 if (is_friend_decl == 2)
5130 msg = G_("default template arguments may not be used in function template "
5131 "friend re-declaration");
5132 else if (is_friend_decl)
5133 msg = G_("default template arguments may not be used in function template "
5134 "friend declarations");
5135 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5136 msg = G_("default template arguments may not be used in function templates "
5137 "without -std=c++11 or -std=gnu++11");
5138 else if (is_partial)
5139 msg = G_("default template arguments may not be used in "
5140 "partial specializations");
5141 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5142 msg = G_("default argument for template parameter for class enclosing %qD");
5143 else
5144 /* Per [temp.param]/9, "A default template-argument shall not be
5145 specified in the template-parameter-lists of the definition of
5146 a member of a class template that appears outside of the member's
5147 class.", thus if we aren't handling a member of a class template
5148 there is no need to examine the parameters. */
5149 return true;
5150
5151 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5152 /* If we're inside a class definition, there's no need to
5153 examine the parameters to the class itself. On the one
5154 hand, they will be checked when the class is defined, and,
5155 on the other, default arguments are valid in things like:
5156 template <class T = double>
5157 struct S { template <class U> void f(U); };
5158 Here the default argument for `S' has no bearing on the
5159 declaration of `f'. */
5160 last_level_to_check = template_class_depth (current_class_type) + 1;
5161 else
5162 /* Check everything. */
5163 last_level_to_check = 0;
5164
5165 for (parm_level = parms;
5166 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5167 parm_level = TREE_CHAIN (parm_level))
5168 {
5169 tree inner_parms = TREE_VALUE (parm_level);
5170 int i;
5171 int ntparms;
5172
5173 ntparms = TREE_VEC_LENGTH (inner_parms);
5174 for (i = 0; i < ntparms; ++i)
5175 {
5176 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5177 continue;
5178
5179 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5180 {
5181 if (msg)
5182 {
5183 no_errors = false;
5184 if (is_friend_decl == 2)
5185 return no_errors;
5186
5187 error (msg, decl);
5188 msg = 0;
5189 }
5190
5191 /* Clear out the default argument so that we are not
5192 confused later. */
5193 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5194 }
5195 }
5196
5197 /* At this point, if we're still interested in issuing messages,
5198 they must apply to classes surrounding the object declared. */
5199 if (msg)
5200 msg = G_("default argument for template parameter for class "
5201 "enclosing %qD");
5202 }
5203
5204 return no_errors;
5205 }
5206
5207 /* Worker for push_template_decl_real, called via
5208 for_each_template_parm. DATA is really an int, indicating the
5209 level of the parameters we are interested in. If T is a template
5210 parameter of that level, return nonzero. */
5211
5212 static int
5213 template_parm_this_level_p (tree t, void* data)
5214 {
5215 int this_level = *(int *)data;
5216 int level;
5217
5218 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5219 level = TEMPLATE_PARM_LEVEL (t);
5220 else
5221 level = TEMPLATE_TYPE_LEVEL (t);
5222 return level == this_level;
5223 }
5224
5225 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5226 DATA is really an int, indicating the innermost outer level of parameters.
5227 If T is a template parameter of that level or further out, return
5228 nonzero. */
5229
5230 static int
5231 template_parm_outer_level (tree t, void *data)
5232 {
5233 int this_level = *(int *)data;
5234 int level;
5235
5236 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5237 level = TEMPLATE_PARM_LEVEL (t);
5238 else
5239 level = TEMPLATE_TYPE_LEVEL (t);
5240 return level <= this_level;
5241 }
5242
5243 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5244 parameters given by current_template_args, or reuses a
5245 previously existing one, if appropriate. Returns the DECL, or an
5246 equivalent one, if it is replaced via a call to duplicate_decls.
5247
5248 If IS_FRIEND is true, DECL is a friend declaration. */
5249
5250 tree
5251 push_template_decl_real (tree decl, bool is_friend)
5252 {
5253 tree tmpl;
5254 tree args;
5255 tree info;
5256 tree ctx;
5257 bool is_primary;
5258 bool is_partial;
5259 int new_template_p = 0;
5260 /* True if the template is a member template, in the sense of
5261 [temp.mem]. */
5262 bool member_template_p = false;
5263
5264 if (decl == error_mark_node || !current_template_parms)
5265 return error_mark_node;
5266
5267 /* See if this is a partial specialization. */
5268 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5269 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5270 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5271 || (VAR_P (decl)
5272 && DECL_LANG_SPECIFIC (decl)
5273 && DECL_TEMPLATE_SPECIALIZATION (decl)
5274 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5275
5276 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5277 is_friend = true;
5278
5279 if (is_friend)
5280 /* For a friend, we want the context of the friend function, not
5281 the type of which it is a friend. */
5282 ctx = CP_DECL_CONTEXT (decl);
5283 else if (CP_DECL_CONTEXT (decl)
5284 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5285 /* In the case of a virtual function, we want the class in which
5286 it is defined. */
5287 ctx = CP_DECL_CONTEXT (decl);
5288 else
5289 /* Otherwise, if we're currently defining some class, the DECL
5290 is assumed to be a member of the class. */
5291 ctx = current_scope ();
5292
5293 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5294 ctx = NULL_TREE;
5295
5296 if (!DECL_CONTEXT (decl))
5297 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5298
5299 /* See if this is a primary template. */
5300 if (is_friend && ctx
5301 && uses_template_parms_level (ctx, processing_template_decl))
5302 /* A friend template that specifies a class context, i.e.
5303 template <typename T> friend void A<T>::f();
5304 is not primary. */
5305 is_primary = false;
5306 else if (TREE_CODE (decl) == TYPE_DECL
5307 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5308 is_primary = false;
5309 else
5310 is_primary = template_parm_scope_p ();
5311
5312 if (is_primary)
5313 {
5314 warning (OPT_Wtemplates, "template %qD declared", decl);
5315
5316 if (DECL_CLASS_SCOPE_P (decl))
5317 member_template_p = true;
5318 if (TREE_CODE (decl) == TYPE_DECL
5319 && anon_aggrname_p (DECL_NAME (decl)))
5320 {
5321 error ("template class without a name");
5322 return error_mark_node;
5323 }
5324 else if (TREE_CODE (decl) == FUNCTION_DECL)
5325 {
5326 if (member_template_p)
5327 {
5328 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5329 error ("member template %qD may not have virt-specifiers", decl);
5330 }
5331 if (DECL_DESTRUCTOR_P (decl))
5332 {
5333 /* [temp.mem]
5334
5335 A destructor shall not be a member template. */
5336 error ("destructor %qD declared as member template", decl);
5337 return error_mark_node;
5338 }
5339 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5340 && (!prototype_p (TREE_TYPE (decl))
5341 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5342 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5343 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5344 == void_list_node)))
5345 {
5346 /* [basic.stc.dynamic.allocation]
5347
5348 An allocation function can be a function
5349 template. ... Template allocation functions shall
5350 have two or more parameters. */
5351 error ("invalid template declaration of %qD", decl);
5352 return error_mark_node;
5353 }
5354 }
5355 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5356 && CLASS_TYPE_P (TREE_TYPE (decl)))
5357 {
5358 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5359 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5360 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5361 {
5362 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5363 if (TREE_CODE (t) == TYPE_DECL)
5364 t = TREE_TYPE (t);
5365 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5366 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5367 }
5368 }
5369 else if (TREE_CODE (decl) == TYPE_DECL
5370 && TYPE_DECL_ALIAS_P (decl))
5371 /* alias-declaration */
5372 gcc_assert (!DECL_ARTIFICIAL (decl));
5373 else if (VAR_P (decl))
5374 /* C++14 variable template. */;
5375 else
5376 {
5377 error ("template declaration of %q#D", decl);
5378 return error_mark_node;
5379 }
5380 }
5381
5382 /* Check to see that the rules regarding the use of default
5383 arguments are not being violated. */
5384 check_default_tmpl_args (decl, current_template_parms,
5385 is_primary, is_partial, /*is_friend_decl=*/0);
5386
5387 /* Ensure that there are no parameter packs in the type of this
5388 declaration that have not been expanded. */
5389 if (TREE_CODE (decl) == FUNCTION_DECL)
5390 {
5391 /* Check each of the arguments individually to see if there are
5392 any bare parameter packs. */
5393 tree type = TREE_TYPE (decl);
5394 tree arg = DECL_ARGUMENTS (decl);
5395 tree argtype = TYPE_ARG_TYPES (type);
5396
5397 while (arg && argtype)
5398 {
5399 if (!DECL_PACK_P (arg)
5400 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5401 {
5402 /* This is a PARM_DECL that contains unexpanded parameter
5403 packs. We have already complained about this in the
5404 check_for_bare_parameter_packs call, so just replace
5405 these types with ERROR_MARK_NODE. */
5406 TREE_TYPE (arg) = error_mark_node;
5407 TREE_VALUE (argtype) = error_mark_node;
5408 }
5409
5410 arg = DECL_CHAIN (arg);
5411 argtype = TREE_CHAIN (argtype);
5412 }
5413
5414 /* Check for bare parameter packs in the return type and the
5415 exception specifiers. */
5416 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5417 /* Errors were already issued, set return type to int
5418 as the frontend doesn't expect error_mark_node as
5419 the return type. */
5420 TREE_TYPE (type) = integer_type_node;
5421 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5422 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5423 }
5424 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5425 && TYPE_DECL_ALIAS_P (decl))
5426 ? DECL_ORIGINAL_TYPE (decl)
5427 : TREE_TYPE (decl)))
5428 {
5429 TREE_TYPE (decl) = error_mark_node;
5430 return error_mark_node;
5431 }
5432
5433 if (is_partial)
5434 return process_partial_specialization (decl);
5435
5436 args = current_template_args ();
5437
5438 if (!ctx
5439 || TREE_CODE (ctx) == FUNCTION_DECL
5440 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5441 || (TREE_CODE (decl) == TYPE_DECL
5442 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5443 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5444 {
5445 if (DECL_LANG_SPECIFIC (decl)
5446 && DECL_TEMPLATE_INFO (decl)
5447 && DECL_TI_TEMPLATE (decl))
5448 tmpl = DECL_TI_TEMPLATE (decl);
5449 /* If DECL is a TYPE_DECL for a class-template, then there won't
5450 be DECL_LANG_SPECIFIC. The information equivalent to
5451 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5452 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5453 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5454 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5455 {
5456 /* Since a template declaration already existed for this
5457 class-type, we must be redeclaring it here. Make sure
5458 that the redeclaration is valid. */
5459 redeclare_class_template (TREE_TYPE (decl),
5460 current_template_parms,
5461 current_template_constraints ());
5462 /* We don't need to create a new TEMPLATE_DECL; just use the
5463 one we already had. */
5464 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5465 }
5466 else
5467 {
5468 tmpl = build_template_decl (decl, current_template_parms,
5469 member_template_p);
5470 new_template_p = 1;
5471
5472 if (DECL_LANG_SPECIFIC (decl)
5473 && DECL_TEMPLATE_SPECIALIZATION (decl))
5474 {
5475 /* A specialization of a member template of a template
5476 class. */
5477 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5478 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5479 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5480 }
5481 }
5482 }
5483 else
5484 {
5485 tree a, t, current, parms;
5486 int i;
5487 tree tinfo = get_template_info (decl);
5488
5489 if (!tinfo)
5490 {
5491 error ("template definition of non-template %q#D", decl);
5492 return error_mark_node;
5493 }
5494
5495 tmpl = TI_TEMPLATE (tinfo);
5496
5497 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5498 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5499 && DECL_TEMPLATE_SPECIALIZATION (decl)
5500 && DECL_MEMBER_TEMPLATE_P (tmpl))
5501 {
5502 tree new_tmpl;
5503
5504 /* The declaration is a specialization of a member
5505 template, declared outside the class. Therefore, the
5506 innermost template arguments will be NULL, so we
5507 replace them with the arguments determined by the
5508 earlier call to check_explicit_specialization. */
5509 args = DECL_TI_ARGS (decl);
5510
5511 new_tmpl
5512 = build_template_decl (decl, current_template_parms,
5513 member_template_p);
5514 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5515 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5516 DECL_TI_TEMPLATE (decl) = new_tmpl;
5517 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5518 DECL_TEMPLATE_INFO (new_tmpl)
5519 = build_template_info (tmpl, args);
5520
5521 register_specialization (new_tmpl,
5522 most_general_template (tmpl),
5523 args,
5524 is_friend, 0);
5525 return decl;
5526 }
5527
5528 /* Make sure the template headers we got make sense. */
5529
5530 parms = DECL_TEMPLATE_PARMS (tmpl);
5531 i = TMPL_PARMS_DEPTH (parms);
5532 if (TMPL_ARGS_DEPTH (args) != i)
5533 {
5534 error ("expected %d levels of template parms for %q#D, got %d",
5535 i, decl, TMPL_ARGS_DEPTH (args));
5536 DECL_INTERFACE_KNOWN (decl) = 1;
5537 return error_mark_node;
5538 }
5539 else
5540 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5541 {
5542 a = TMPL_ARGS_LEVEL (args, i);
5543 t = INNERMOST_TEMPLATE_PARMS (parms);
5544
5545 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5546 {
5547 if (current == decl)
5548 error ("got %d template parameters for %q#D",
5549 TREE_VEC_LENGTH (a), decl);
5550 else
5551 error ("got %d template parameters for %q#T",
5552 TREE_VEC_LENGTH (a), current);
5553 error (" but %d required", TREE_VEC_LENGTH (t));
5554 /* Avoid crash in import_export_decl. */
5555 DECL_INTERFACE_KNOWN (decl) = 1;
5556 return error_mark_node;
5557 }
5558
5559 if (current == decl)
5560 current = ctx;
5561 else if (current == NULL_TREE)
5562 /* Can happen in erroneous input. */
5563 break;
5564 else
5565 current = get_containing_scope (current);
5566 }
5567
5568 /* Check that the parms are used in the appropriate qualifying scopes
5569 in the declarator. */
5570 if (!comp_template_args
5571 (TI_ARGS (tinfo),
5572 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5573 {
5574 error ("template arguments to %qD do not match original "
5575 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5576 if (!uses_template_parms (TI_ARGS (tinfo)))
5577 inform (input_location, "use %<template<>%> for"
5578 " an explicit specialization");
5579 /* Avoid crash in import_export_decl. */
5580 DECL_INTERFACE_KNOWN (decl) = 1;
5581 return error_mark_node;
5582 }
5583 }
5584
5585 DECL_TEMPLATE_RESULT (tmpl) = decl;
5586 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5587
5588 /* Push template declarations for global functions and types. Note
5589 that we do not try to push a global template friend declared in a
5590 template class; such a thing may well depend on the template
5591 parameters of the class. */
5592 if (new_template_p && !ctx
5593 && !(is_friend && template_class_depth (current_class_type) > 0))
5594 {
5595 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5596 if (tmpl == error_mark_node)
5597 return error_mark_node;
5598
5599 /* Hide template friend classes that haven't been declared yet. */
5600 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5601 {
5602 DECL_ANTICIPATED (tmpl) = 1;
5603 DECL_FRIEND_P (tmpl) = 1;
5604 }
5605 }
5606
5607 if (is_primary)
5608 {
5609 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5610
5611 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5612
5613 /* Give template template parms a DECL_CONTEXT of the template
5614 for which they are a parameter. */
5615 parms = INNERMOST_TEMPLATE_PARMS (parms);
5616 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5617 {
5618 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5619 if (TREE_CODE (parm) == TEMPLATE_DECL)
5620 DECL_CONTEXT (parm) = tmpl;
5621 }
5622
5623 if (TREE_CODE (decl) == TYPE_DECL
5624 && TYPE_DECL_ALIAS_P (decl)
5625 && complex_alias_template_p (tmpl))
5626 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5627 }
5628
5629 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5630 back to its most general template. If TMPL is a specialization,
5631 ARGS may only have the innermost set of arguments. Add the missing
5632 argument levels if necessary. */
5633 if (DECL_TEMPLATE_INFO (tmpl))
5634 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5635
5636 info = build_template_info (tmpl, args);
5637
5638 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5639 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5640 else
5641 {
5642 if (is_primary)
5643 retrofit_lang_decl (decl);
5644 if (DECL_LANG_SPECIFIC (decl))
5645 DECL_TEMPLATE_INFO (decl) = info;
5646 }
5647
5648 if (flag_implicit_templates
5649 && !is_friend
5650 && TREE_PUBLIC (decl)
5651 && VAR_OR_FUNCTION_DECL_P (decl))
5652 /* Set DECL_COMDAT on template instantiations; if we force
5653 them to be emitted by explicit instantiation or -frepo,
5654 mark_needed will tell cgraph to do the right thing. */
5655 DECL_COMDAT (decl) = true;
5656
5657 return DECL_TEMPLATE_RESULT (tmpl);
5658 }
5659
5660 tree
5661 push_template_decl (tree decl)
5662 {
5663 return push_template_decl_real (decl, false);
5664 }
5665
5666 /* FN is an inheriting constructor that inherits from the constructor
5667 template INHERITED; turn FN into a constructor template with a matching
5668 template header. */
5669
5670 tree
5671 add_inherited_template_parms (tree fn, tree inherited)
5672 {
5673 tree inner_parms
5674 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5675 inner_parms = copy_node (inner_parms);
5676 tree parms
5677 = tree_cons (size_int (processing_template_decl + 1),
5678 inner_parms, current_template_parms);
5679 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5680 tree args = template_parms_to_args (parms);
5681 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5682 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5683 DECL_TEMPLATE_RESULT (tmpl) = fn;
5684 DECL_ARTIFICIAL (tmpl) = true;
5685 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5686 return tmpl;
5687 }
5688
5689 /* Called when a class template TYPE is redeclared with the indicated
5690 template PARMS, e.g.:
5691
5692 template <class T> struct S;
5693 template <class T> struct S {}; */
5694
5695 bool
5696 redeclare_class_template (tree type, tree parms, tree cons)
5697 {
5698 tree tmpl;
5699 tree tmpl_parms;
5700 int i;
5701
5702 if (!TYPE_TEMPLATE_INFO (type))
5703 {
5704 error ("%qT is not a template type", type);
5705 return false;
5706 }
5707
5708 tmpl = TYPE_TI_TEMPLATE (type);
5709 if (!PRIMARY_TEMPLATE_P (tmpl))
5710 /* The type is nested in some template class. Nothing to worry
5711 about here; there are no new template parameters for the nested
5712 type. */
5713 return true;
5714
5715 if (!parms)
5716 {
5717 error ("template specifiers not specified in declaration of %qD",
5718 tmpl);
5719 return false;
5720 }
5721
5722 parms = INNERMOST_TEMPLATE_PARMS (parms);
5723 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5724
5725 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5726 {
5727 error_n (input_location, TREE_VEC_LENGTH (parms),
5728 "redeclared with %d template parameter",
5729 "redeclared with %d template parameters",
5730 TREE_VEC_LENGTH (parms));
5731 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5732 "previous declaration %qD used %d template parameter",
5733 "previous declaration %qD used %d template parameters",
5734 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5735 return false;
5736 }
5737
5738 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5739 {
5740 tree tmpl_parm;
5741 tree parm;
5742 tree tmpl_default;
5743 tree parm_default;
5744
5745 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5746 || TREE_VEC_ELT (parms, i) == error_mark_node)
5747 continue;
5748
5749 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5750 if (error_operand_p (tmpl_parm))
5751 return false;
5752
5753 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5754 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5755 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5756
5757 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5758 TEMPLATE_DECL. */
5759 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5760 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5761 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5762 || (TREE_CODE (tmpl_parm) != PARM_DECL
5763 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5764 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5765 || (TREE_CODE (tmpl_parm) == PARM_DECL
5766 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5767 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5768 {
5769 error ("template parameter %q+#D", tmpl_parm);
5770 error ("redeclared here as %q#D", parm);
5771 return false;
5772 }
5773
5774 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5775 {
5776 /* We have in [temp.param]:
5777
5778 A template-parameter may not be given default arguments
5779 by two different declarations in the same scope. */
5780 error_at (input_location, "redefinition of default argument for %q#D", parm);
5781 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5782 "original definition appeared here");
5783 return false;
5784 }
5785
5786 if (parm_default != NULL_TREE)
5787 /* Update the previous template parameters (which are the ones
5788 that will really count) with the new default value. */
5789 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5790 else if (tmpl_default != NULL_TREE)
5791 /* Update the new parameters, too; they'll be used as the
5792 parameters for any members. */
5793 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5794
5795 /* Give each template template parm in this redeclaration a
5796 DECL_CONTEXT of the template for which they are a parameter. */
5797 if (TREE_CODE (parm) == TEMPLATE_DECL)
5798 {
5799 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5800 DECL_CONTEXT (parm) = tmpl;
5801 }
5802
5803 if (TREE_CODE (parm) == TYPE_DECL)
5804 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5805 }
5806
5807 // Cannot redeclare a class template with a different set of constraints.
5808 if (!equivalent_constraints (get_constraints (tmpl), cons))
5809 {
5810 error_at (input_location, "redeclaration %q#D with different "
5811 "constraints", tmpl);
5812 inform (DECL_SOURCE_LOCATION (tmpl),
5813 "original declaration appeared here");
5814 }
5815
5816 return true;
5817 }
5818
5819 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5820 to be used when the caller has already checked
5821 (processing_template_decl
5822 && !instantiation_dependent_expression_p (expr)
5823 && potential_constant_expression (expr))
5824 and cleared processing_template_decl. */
5825
5826 tree
5827 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5828 {
5829 return tsubst_copy_and_build (expr,
5830 /*args=*/NULL_TREE,
5831 complain,
5832 /*in_decl=*/NULL_TREE,
5833 /*function_p=*/false,
5834 /*integral_constant_expression_p=*/true);
5835 }
5836
5837 /* Simplify EXPR if it is a non-dependent expression. Returns the
5838 (possibly simplified) expression. */
5839
5840 tree
5841 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5842 {
5843 if (expr == NULL_TREE)
5844 return NULL_TREE;
5845
5846 /* If we're in a template, but EXPR isn't value dependent, simplify
5847 it. We're supposed to treat:
5848
5849 template <typename T> void f(T[1 + 1]);
5850 template <typename T> void f(T[2]);
5851
5852 as two declarations of the same function, for example. */
5853 if (processing_template_decl
5854 && is_nondependent_constant_expression (expr))
5855 {
5856 processing_template_decl_sentinel s;
5857 expr = instantiate_non_dependent_expr_internal (expr, complain);
5858 }
5859 return expr;
5860 }
5861
5862 tree
5863 instantiate_non_dependent_expr (tree expr)
5864 {
5865 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5866 }
5867
5868 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5869 an uninstantiated expression. */
5870
5871 tree
5872 instantiate_non_dependent_or_null (tree expr)
5873 {
5874 if (expr == NULL_TREE)
5875 return NULL_TREE;
5876 if (processing_template_decl)
5877 {
5878 if (!is_nondependent_constant_expression (expr))
5879 expr = NULL_TREE;
5880 else
5881 {
5882 processing_template_decl_sentinel s;
5883 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5884 }
5885 }
5886 return expr;
5887 }
5888
5889 /* True iff T is a specialization of a variable template. */
5890
5891 bool
5892 variable_template_specialization_p (tree t)
5893 {
5894 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5895 return false;
5896 tree tmpl = DECL_TI_TEMPLATE (t);
5897 return variable_template_p (tmpl);
5898 }
5899
5900 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5901 template declaration, or a TYPE_DECL for an alias declaration. */
5902
5903 bool
5904 alias_type_or_template_p (tree t)
5905 {
5906 if (t == NULL_TREE)
5907 return false;
5908 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5909 || (TYPE_P (t)
5910 && TYPE_NAME (t)
5911 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5912 || DECL_ALIAS_TEMPLATE_P (t));
5913 }
5914
5915 /* Return TRUE iff T is a specialization of an alias template. */
5916
5917 bool
5918 alias_template_specialization_p (const_tree t)
5919 {
5920 /* It's an alias template specialization if it's an alias and its
5921 TYPE_NAME is a specialization of a primary template. */
5922 if (TYPE_ALIAS_P (t))
5923 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5924 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5925
5926 return false;
5927 }
5928
5929 /* An alias template is complex from a SFINAE perspective if a template-id
5930 using that alias can be ill-formed when the expansion is not, as with
5931 the void_t template. We determine this by checking whether the
5932 expansion for the alias template uses all its template parameters. */
5933
5934 struct uses_all_template_parms_data
5935 {
5936 int level;
5937 bool *seen;
5938 };
5939
5940 static int
5941 uses_all_template_parms_r (tree t, void *data_)
5942 {
5943 struct uses_all_template_parms_data &data
5944 = *(struct uses_all_template_parms_data*)data_;
5945 tree idx = get_template_parm_index (t);
5946
5947 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5948 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5949 return 0;
5950 }
5951
5952 static bool
5953 complex_alias_template_p (const_tree tmpl)
5954 {
5955 struct uses_all_template_parms_data data;
5956 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5957 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5958 data.level = TMPL_PARMS_DEPTH (parms);
5959 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5960 data.seen = XALLOCAVEC (bool, len);
5961 for (int i = 0; i < len; ++i)
5962 data.seen[i] = false;
5963
5964 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5965 for (int i = 0; i < len; ++i)
5966 if (!data.seen[i])
5967 return true;
5968 return false;
5969 }
5970
5971 /* Return TRUE iff T is a specialization of a complex alias template with
5972 dependent template-arguments. */
5973
5974 bool
5975 dependent_alias_template_spec_p (const_tree t)
5976 {
5977 if (!alias_template_specialization_p (t))
5978 return false;
5979
5980 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5981 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5982 return false;
5983
5984 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5985 if (!any_dependent_template_arguments_p (args))
5986 return false;
5987
5988 return true;
5989 }
5990
5991 /* Return the number of innermost template parameters in TMPL. */
5992
5993 static int
5994 num_innermost_template_parms (tree tmpl)
5995 {
5996 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5997 return TREE_VEC_LENGTH (parms);
5998 }
5999
6000 /* Return either TMPL or another template that it is equivalent to under DR
6001 1286: An alias that just changes the name of a template is equivalent to
6002 the other template. */
6003
6004 static tree
6005 get_underlying_template (tree tmpl)
6006 {
6007 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6008 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6009 {
6010 /* Determine if the alias is equivalent to an underlying template. */
6011 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6012 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6013 if (!tinfo)
6014 break;
6015
6016 tree underlying = TI_TEMPLATE (tinfo);
6017 if (!PRIMARY_TEMPLATE_P (underlying)
6018 || (num_innermost_template_parms (tmpl)
6019 != num_innermost_template_parms (underlying)))
6020 break;
6021
6022 tree alias_args = INNERMOST_TEMPLATE_ARGS
6023 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6024 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6025 break;
6026
6027 /* Alias is equivalent. Strip it and repeat. */
6028 tmpl = underlying;
6029 }
6030
6031 return tmpl;
6032 }
6033
6034 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6035 must be a reference-to-function or a pointer-to-function type, as specified
6036 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6037 and check that the resulting function has external linkage. */
6038
6039 static tree
6040 convert_nontype_argument_function (tree type, tree expr,
6041 tsubst_flags_t complain)
6042 {
6043 tree fns = expr;
6044 tree fn, fn_no_ptr;
6045 linkage_kind linkage;
6046
6047 fn = instantiate_type (type, fns, tf_none);
6048 if (fn == error_mark_node)
6049 return error_mark_node;
6050
6051 if (value_dependent_expression_p (fn))
6052 goto accept;
6053
6054 fn_no_ptr = strip_fnptr_conv (fn);
6055 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6056 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6057 if (BASELINK_P (fn_no_ptr))
6058 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6059
6060 /* [temp.arg.nontype]/1
6061
6062 A template-argument for a non-type, non-template template-parameter
6063 shall be one of:
6064 [...]
6065 -- the address of an object or function with external [C++11: or
6066 internal] linkage. */
6067
6068 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6069 {
6070 if (complain & tf_error)
6071 {
6072 error ("%qE is not a valid template argument for type %qT",
6073 expr, type);
6074 if (TYPE_PTR_P (type))
6075 inform (input_location, "it must be the address of a function "
6076 "with external linkage");
6077 else
6078 inform (input_location, "it must be the name of a function with "
6079 "external linkage");
6080 }
6081 return NULL_TREE;
6082 }
6083
6084 linkage = decl_linkage (fn_no_ptr);
6085 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6086 {
6087 if (complain & tf_error)
6088 {
6089 if (cxx_dialect >= cxx11)
6090 error ("%qE is not a valid template argument for type %qT "
6091 "because %qD has no linkage",
6092 expr, type, fn_no_ptr);
6093 else
6094 error ("%qE is not a valid template argument for type %qT "
6095 "because %qD does not have external linkage",
6096 expr, type, fn_no_ptr);
6097 }
6098 return NULL_TREE;
6099 }
6100
6101 accept:
6102 if (TREE_CODE (type) == REFERENCE_TYPE)
6103 fn = build_address (fn);
6104 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6105 fn = build_nop (type, fn);
6106
6107 return fn;
6108 }
6109
6110 /* Subroutine of convert_nontype_argument.
6111 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6112 Emit an error otherwise. */
6113
6114 static bool
6115 check_valid_ptrmem_cst_expr (tree type, tree expr,
6116 tsubst_flags_t complain)
6117 {
6118 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6119 tree orig_expr = expr;
6120 STRIP_NOPS (expr);
6121 if (null_ptr_cst_p (expr))
6122 return true;
6123 if (TREE_CODE (expr) == PTRMEM_CST
6124 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6125 PTRMEM_CST_CLASS (expr)))
6126 return true;
6127 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6128 return true;
6129 if (processing_template_decl
6130 && TREE_CODE (expr) == ADDR_EXPR
6131 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6132 return true;
6133 if (complain & tf_error)
6134 {
6135 error_at (loc, "%qE is not a valid template argument for type %qT",
6136 orig_expr, type);
6137 if (TREE_CODE (expr) != PTRMEM_CST)
6138 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6139 else
6140 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6141 }
6142 return false;
6143 }
6144
6145 /* Returns TRUE iff the address of OP is value-dependent.
6146
6147 14.6.2.4 [temp.dep.temp]:
6148 A non-integral non-type template-argument is dependent if its type is
6149 dependent or it has either of the following forms
6150 qualified-id
6151 & qualified-id
6152 and contains a nested-name-specifier which specifies a class-name that
6153 names a dependent type.
6154
6155 We generalize this to just say that the address of a member of a
6156 dependent class is value-dependent; the above doesn't cover the
6157 address of a static data member named with an unqualified-id. */
6158
6159 static bool
6160 has_value_dependent_address (tree op)
6161 {
6162 /* We could use get_inner_reference here, but there's no need;
6163 this is only relevant for template non-type arguments, which
6164 can only be expressed as &id-expression. */
6165 if (DECL_P (op))
6166 {
6167 tree ctx = CP_DECL_CONTEXT (op);
6168 if (TYPE_P (ctx) && dependent_type_p (ctx))
6169 return true;
6170 }
6171
6172 return false;
6173 }
6174
6175 /* The next set of functions are used for providing helpful explanatory
6176 diagnostics for failed overload resolution. Their messages should be
6177 indented by two spaces for consistency with the messages in
6178 call.c */
6179
6180 static int
6181 unify_success (bool /*explain_p*/)
6182 {
6183 return 0;
6184 }
6185
6186 /* Other failure functions should call this one, to provide a single function
6187 for setting a breakpoint on. */
6188
6189 static int
6190 unify_invalid (bool /*explain_p*/)
6191 {
6192 return 1;
6193 }
6194
6195 static int
6196 unify_parameter_deduction_failure (bool explain_p, tree parm)
6197 {
6198 if (explain_p)
6199 inform (input_location,
6200 " couldn't deduce template parameter %qD", parm);
6201 return unify_invalid (explain_p);
6202 }
6203
6204 static int
6205 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6206 {
6207 if (explain_p)
6208 inform (input_location,
6209 " types %qT and %qT have incompatible cv-qualifiers",
6210 parm, arg);
6211 return unify_invalid (explain_p);
6212 }
6213
6214 static int
6215 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6216 {
6217 if (explain_p)
6218 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6219 return unify_invalid (explain_p);
6220 }
6221
6222 static int
6223 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6224 {
6225 if (explain_p)
6226 inform (input_location,
6227 " template parameter %qD is not a parameter pack, but "
6228 "argument %qD is",
6229 parm, arg);
6230 return unify_invalid (explain_p);
6231 }
6232
6233 static int
6234 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6235 {
6236 if (explain_p)
6237 inform (input_location,
6238 " template argument %qE does not match "
6239 "pointer-to-member constant %qE",
6240 arg, parm);
6241 return unify_invalid (explain_p);
6242 }
6243
6244 static int
6245 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6246 {
6247 if (explain_p)
6248 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6249 return unify_invalid (explain_p);
6250 }
6251
6252 static int
6253 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6254 {
6255 if (explain_p)
6256 inform (input_location,
6257 " inconsistent parameter pack deduction with %qT and %qT",
6258 old_arg, new_arg);
6259 return unify_invalid (explain_p);
6260 }
6261
6262 static int
6263 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6264 {
6265 if (explain_p)
6266 {
6267 if (TYPE_P (parm))
6268 inform (input_location,
6269 " deduced conflicting types for parameter %qT (%qT and %qT)",
6270 parm, first, second);
6271 else
6272 inform (input_location,
6273 " deduced conflicting values for non-type parameter "
6274 "%qE (%qE and %qE)", parm, first, second);
6275 }
6276 return unify_invalid (explain_p);
6277 }
6278
6279 static int
6280 unify_vla_arg (bool explain_p, tree arg)
6281 {
6282 if (explain_p)
6283 inform (input_location,
6284 " variable-sized array type %qT is not "
6285 "a valid template argument",
6286 arg);
6287 return unify_invalid (explain_p);
6288 }
6289
6290 static int
6291 unify_method_type_error (bool explain_p, tree arg)
6292 {
6293 if (explain_p)
6294 inform (input_location,
6295 " member function type %qT is not a valid template argument",
6296 arg);
6297 return unify_invalid (explain_p);
6298 }
6299
6300 static int
6301 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6302 {
6303 if (explain_p)
6304 {
6305 if (least_p)
6306 inform_n (input_location, wanted,
6307 " candidate expects at least %d argument, %d provided",
6308 " candidate expects at least %d arguments, %d provided",
6309 wanted, have);
6310 else
6311 inform_n (input_location, wanted,
6312 " candidate expects %d argument, %d provided",
6313 " candidate expects %d arguments, %d provided",
6314 wanted, have);
6315 }
6316 return unify_invalid (explain_p);
6317 }
6318
6319 static int
6320 unify_too_many_arguments (bool explain_p, int have, int wanted)
6321 {
6322 return unify_arity (explain_p, have, wanted);
6323 }
6324
6325 static int
6326 unify_too_few_arguments (bool explain_p, int have, int wanted,
6327 bool least_p = false)
6328 {
6329 return unify_arity (explain_p, have, wanted, least_p);
6330 }
6331
6332 static int
6333 unify_arg_conversion (bool explain_p, tree to_type,
6334 tree from_type, tree arg)
6335 {
6336 if (explain_p)
6337 inform (EXPR_LOC_OR_LOC (arg, input_location),
6338 " cannot convert %qE (type %qT) to type %qT",
6339 arg, from_type, to_type);
6340 return unify_invalid (explain_p);
6341 }
6342
6343 static int
6344 unify_no_common_base (bool explain_p, enum template_base_result r,
6345 tree parm, tree arg)
6346 {
6347 if (explain_p)
6348 switch (r)
6349 {
6350 case tbr_ambiguous_baseclass:
6351 inform (input_location, " %qT is an ambiguous base class of %qT",
6352 parm, arg);
6353 break;
6354 default:
6355 inform (input_location, " %qT is not derived from %qT", arg, parm);
6356 break;
6357 }
6358 return unify_invalid (explain_p);
6359 }
6360
6361 static int
6362 unify_inconsistent_template_template_parameters (bool explain_p)
6363 {
6364 if (explain_p)
6365 inform (input_location,
6366 " template parameters of a template template argument are "
6367 "inconsistent with other deduced template arguments");
6368 return unify_invalid (explain_p);
6369 }
6370
6371 static int
6372 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6373 {
6374 if (explain_p)
6375 inform (input_location,
6376 " can't deduce a template for %qT from non-template type %qT",
6377 parm, arg);
6378 return unify_invalid (explain_p);
6379 }
6380
6381 static int
6382 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6383 {
6384 if (explain_p)
6385 inform (input_location,
6386 " template argument %qE does not match %qE", arg, parm);
6387 return unify_invalid (explain_p);
6388 }
6389
6390 /* Attempt to convert the non-type template parameter EXPR to the
6391 indicated TYPE. If the conversion is successful, return the
6392 converted value. If the conversion is unsuccessful, return
6393 NULL_TREE if we issued an error message, or error_mark_node if we
6394 did not. We issue error messages for out-and-out bad template
6395 parameters, but not simply because the conversion failed, since we
6396 might be just trying to do argument deduction. Both TYPE and EXPR
6397 must be non-dependent.
6398
6399 The conversion follows the special rules described in
6400 [temp.arg.nontype], and it is much more strict than an implicit
6401 conversion.
6402
6403 This function is called twice for each template argument (see
6404 lookup_template_class for a more accurate description of this
6405 problem). This means that we need to handle expressions which
6406 are not valid in a C++ source, but can be created from the
6407 first call (for instance, casts to perform conversions). These
6408 hacks can go away after we fix the double coercion problem. */
6409
6410 static tree
6411 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6412 {
6413 tree expr_type;
6414 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6415 tree orig_expr = expr;
6416
6417 /* Detect immediately string literals as invalid non-type argument.
6418 This special-case is not needed for correctness (we would easily
6419 catch this later), but only to provide better diagnostic for this
6420 common user mistake. As suggested by DR 100, we do not mention
6421 linkage issues in the diagnostic as this is not the point. */
6422 /* FIXME we're making this OK. */
6423 if (TREE_CODE (expr) == STRING_CST)
6424 {
6425 if (complain & tf_error)
6426 error ("%qE is not a valid template argument for type %qT "
6427 "because string literals can never be used in this context",
6428 expr, type);
6429 return NULL_TREE;
6430 }
6431
6432 /* Add the ADDR_EXPR now for the benefit of
6433 value_dependent_expression_p. */
6434 if (TYPE_PTROBV_P (type)
6435 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6436 {
6437 expr = decay_conversion (expr, complain);
6438 if (expr == error_mark_node)
6439 return error_mark_node;
6440 }
6441
6442 /* If we are in a template, EXPR may be non-dependent, but still
6443 have a syntactic, rather than semantic, form. For example, EXPR
6444 might be a SCOPE_REF, rather than the VAR_DECL to which the
6445 SCOPE_REF refers. Preserving the qualifying scope is necessary
6446 so that access checking can be performed when the template is
6447 instantiated -- but here we need the resolved form so that we can
6448 convert the argument. */
6449 bool non_dep = false;
6450 if (TYPE_REF_OBJ_P (type)
6451 && has_value_dependent_address (expr))
6452 /* If we want the address and it's value-dependent, don't fold. */;
6453 else if (processing_template_decl
6454 && is_nondependent_constant_expression (expr))
6455 non_dep = true;
6456 if (error_operand_p (expr))
6457 return error_mark_node;
6458 expr_type = TREE_TYPE (expr);
6459
6460 /* If the argument is non-dependent, perform any conversions in
6461 non-dependent context as well. */
6462 processing_template_decl_sentinel s (non_dep);
6463 if (non_dep)
6464 expr = instantiate_non_dependent_expr_internal (expr, complain);
6465
6466 if (value_dependent_expression_p (expr))
6467 expr = canonicalize_expr_argument (expr, complain);
6468
6469 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6470 to a non-type argument of "nullptr". */
6471 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6472 expr = fold_simple (convert (type, expr));
6473
6474 /* In C++11, integral or enumeration non-type template arguments can be
6475 arbitrary constant expressions. Pointer and pointer to
6476 member arguments can be general constant expressions that evaluate
6477 to a null value, but otherwise still need to be of a specific form. */
6478 if (cxx_dialect >= cxx11)
6479 {
6480 if (TREE_CODE (expr) == PTRMEM_CST)
6481 /* A PTRMEM_CST is already constant, and a valid template
6482 argument for a parameter of pointer to member type, we just want
6483 to leave it in that form rather than lower it to a
6484 CONSTRUCTOR. */;
6485 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6486 || cxx_dialect >= cxx17)
6487 {
6488 /* C++17: A template-argument for a non-type template-parameter shall
6489 be a converted constant expression (8.20) of the type of the
6490 template-parameter. */
6491 expr = build_converted_constant_expr (type, expr, complain);
6492 if (expr == error_mark_node)
6493 return error_mark_node;
6494 expr = maybe_constant_value (expr);
6495 expr = convert_from_reference (expr);
6496 }
6497 else if (TYPE_PTR_OR_PTRMEM_P (type))
6498 {
6499 tree folded = maybe_constant_value (expr);
6500 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6501 : null_member_pointer_value_p (folded))
6502 expr = folded;
6503 }
6504 }
6505
6506 if (TREE_CODE (type) == REFERENCE_TYPE)
6507 expr = mark_lvalue_use (expr);
6508 else
6509 expr = mark_rvalue_use (expr);
6510
6511 /* HACK: Due to double coercion, we can get a
6512 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6513 which is the tree that we built on the first call (see
6514 below when coercing to reference to object or to reference to
6515 function). We just strip everything and get to the arg.
6516 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6517 for examples. */
6518 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6519 {
6520 tree probe_type, probe = expr;
6521 if (REFERENCE_REF_P (probe))
6522 probe = TREE_OPERAND (probe, 0);
6523 probe_type = TREE_TYPE (probe);
6524 if (TREE_CODE (probe) == NOP_EXPR)
6525 {
6526 /* ??? Maybe we could use convert_from_reference here, but we
6527 would need to relax its constraints because the NOP_EXPR
6528 could actually change the type to something more cv-qualified,
6529 and this is not folded by convert_from_reference. */
6530 tree addr = TREE_OPERAND (probe, 0);
6531 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6532 && TREE_CODE (addr) == ADDR_EXPR
6533 && TYPE_PTR_P (TREE_TYPE (addr))
6534 && (same_type_ignoring_top_level_qualifiers_p
6535 (TREE_TYPE (probe_type),
6536 TREE_TYPE (TREE_TYPE (addr)))))
6537 {
6538 expr = TREE_OPERAND (addr, 0);
6539 expr_type = TREE_TYPE (probe_type);
6540 }
6541 }
6542 }
6543
6544 /* [temp.arg.nontype]/5, bullet 1
6545
6546 For a non-type template-parameter of integral or enumeration type,
6547 integral promotions (_conv.prom_) and integral conversions
6548 (_conv.integral_) are applied. */
6549 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6550 {
6551 if (cxx_dialect < cxx11)
6552 {
6553 tree t = build_converted_constant_expr (type, expr, complain);
6554 t = maybe_constant_value (t);
6555 if (t != error_mark_node)
6556 expr = t;
6557 }
6558
6559 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6560 return error_mark_node;
6561
6562 /* Notice that there are constant expressions like '4 % 0' which
6563 do not fold into integer constants. */
6564 if (TREE_CODE (expr) != INTEGER_CST
6565 && !value_dependent_expression_p (expr))
6566 {
6567 if (complain & tf_error)
6568 {
6569 int errs = errorcount, warns = warningcount + werrorcount;
6570 if (!require_potential_constant_expression (expr))
6571 expr = error_mark_node;
6572 else
6573 expr = cxx_constant_value (expr);
6574 if (errorcount > errs || warningcount + werrorcount > warns)
6575 inform (loc, "in template argument for type %qT ", type);
6576 if (expr == error_mark_node)
6577 return NULL_TREE;
6578 /* else cxx_constant_value complained but gave us
6579 a real constant, so go ahead. */
6580 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6581 }
6582 else
6583 return NULL_TREE;
6584 }
6585
6586 /* Avoid typedef problems. */
6587 if (TREE_TYPE (expr) != type)
6588 expr = fold_convert (type, expr);
6589 }
6590 /* [temp.arg.nontype]/5, bullet 2
6591
6592 For a non-type template-parameter of type pointer to object,
6593 qualification conversions (_conv.qual_) and the array-to-pointer
6594 conversion (_conv.array_) are applied. */
6595 else if (TYPE_PTROBV_P (type))
6596 {
6597 tree decayed = expr;
6598
6599 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6600 decay_conversion or an explicit cast. If it's a problematic cast,
6601 we'll complain about it below. */
6602 if (TREE_CODE (expr) == NOP_EXPR)
6603 {
6604 tree probe = expr;
6605 STRIP_NOPS (probe);
6606 if (TREE_CODE (probe) == ADDR_EXPR
6607 && TYPE_PTR_P (TREE_TYPE (probe)))
6608 {
6609 expr = probe;
6610 expr_type = TREE_TYPE (expr);
6611 }
6612 }
6613
6614 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6615
6616 A template-argument for a non-type, non-template template-parameter
6617 shall be one of: [...]
6618
6619 -- the name of a non-type template-parameter;
6620 -- the address of an object or function with external linkage, [...]
6621 expressed as "& id-expression" where the & is optional if the name
6622 refers to a function or array, or if the corresponding
6623 template-parameter is a reference.
6624
6625 Here, we do not care about functions, as they are invalid anyway
6626 for a parameter of type pointer-to-object. */
6627
6628 if (value_dependent_expression_p (expr))
6629 /* Non-type template parameters are OK. */
6630 ;
6631 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6632 /* Null pointer values are OK in C++11. */;
6633 else if (TREE_CODE (expr) != ADDR_EXPR)
6634 {
6635 if (VAR_P (expr))
6636 {
6637 if (complain & tf_error)
6638 error ("%qD is not a valid template argument "
6639 "because %qD is a variable, not the address of "
6640 "a variable", orig_expr, expr);
6641 return NULL_TREE;
6642 }
6643 if (POINTER_TYPE_P (expr_type))
6644 {
6645 if (complain & tf_error)
6646 error ("%qE is not a valid template argument for %qT "
6647 "because it is not the address of a variable",
6648 orig_expr, type);
6649 return NULL_TREE;
6650 }
6651 /* Other values, like integer constants, might be valid
6652 non-type arguments of some other type. */
6653 return error_mark_node;
6654 }
6655 else
6656 {
6657 tree decl = TREE_OPERAND (expr, 0);
6658
6659 if (!VAR_P (decl))
6660 {
6661 if (complain & tf_error)
6662 error ("%qE is not a valid template argument of type %qT "
6663 "because %qE is not a variable", orig_expr, type, decl);
6664 return NULL_TREE;
6665 }
6666 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6667 {
6668 if (complain & tf_error)
6669 error ("%qE is not a valid template argument of type %qT "
6670 "because %qD does not have external linkage",
6671 orig_expr, type, decl);
6672 return NULL_TREE;
6673 }
6674 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6675 && decl_linkage (decl) == lk_none)
6676 {
6677 if (complain & tf_error)
6678 error ("%qE is not a valid template argument of type %qT "
6679 "because %qD has no linkage", orig_expr, type, decl);
6680 return NULL_TREE;
6681 }
6682 /* C++17: For a non-type template-parameter of reference or pointer
6683 type, the value of the constant expression shall not refer to (or
6684 for a pointer type, shall not be the address of):
6685 * a subobject (4.5),
6686 * a temporary object (15.2),
6687 * a string literal (5.13.5),
6688 * the result of a typeid expression (8.2.8), or
6689 * a predefined __func__ variable (11.4.1). */
6690 else if (DECL_ARTIFICIAL (decl))
6691 {
6692 if (complain & tf_error)
6693 error ("the address of %qD is not a valid template argument",
6694 decl);
6695 return NULL_TREE;
6696 }
6697 else if (!same_type_ignoring_top_level_qualifiers_p
6698 (strip_array_types (TREE_TYPE (type)),
6699 strip_array_types (TREE_TYPE (decl))))
6700 {
6701 if (complain & tf_error)
6702 error ("the address of the %qT subobject of %qD is not a "
6703 "valid template argument", TREE_TYPE (type), decl);
6704 return NULL_TREE;
6705 }
6706 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6707 {
6708 if (complain & tf_error)
6709 error ("the address of %qD is not a valid template argument "
6710 "because it does not have static storage duration",
6711 decl);
6712 return NULL_TREE;
6713 }
6714 }
6715
6716 expr = decayed;
6717
6718 expr = perform_qualification_conversions (type, expr);
6719 if (expr == error_mark_node)
6720 return error_mark_node;
6721 }
6722 /* [temp.arg.nontype]/5, bullet 3
6723
6724 For a non-type template-parameter of type reference to object, no
6725 conversions apply. The type referred to by the reference may be more
6726 cv-qualified than the (otherwise identical) type of the
6727 template-argument. The template-parameter is bound directly to the
6728 template-argument, which must be an lvalue. */
6729 else if (TYPE_REF_OBJ_P (type))
6730 {
6731 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6732 expr_type))
6733 return error_mark_node;
6734
6735 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6736 {
6737 if (complain & tf_error)
6738 error ("%qE is not a valid template argument for type %qT "
6739 "because of conflicts in cv-qualification", expr, type);
6740 return NULL_TREE;
6741 }
6742
6743 if (!lvalue_p (expr))
6744 {
6745 if (complain & tf_error)
6746 error ("%qE is not a valid template argument for type %qT "
6747 "because it is not an lvalue", expr, type);
6748 return NULL_TREE;
6749 }
6750
6751 /* [temp.arg.nontype]/1
6752
6753 A template-argument for a non-type, non-template template-parameter
6754 shall be one of: [...]
6755
6756 -- the address of an object or function with external linkage. */
6757 if (INDIRECT_REF_P (expr)
6758 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6759 {
6760 expr = TREE_OPERAND (expr, 0);
6761 if (DECL_P (expr))
6762 {
6763 if (complain & tf_error)
6764 error ("%q#D is not a valid template argument for type %qT "
6765 "because a reference variable does not have a constant "
6766 "address", expr, type);
6767 return NULL_TREE;
6768 }
6769 }
6770
6771 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6772 && value_dependent_expression_p (expr))
6773 /* OK, dependent reference. We don't want to ask whether a DECL is
6774 itself value-dependent, since what we want here is its address. */;
6775 else
6776 {
6777 if (!DECL_P (expr))
6778 {
6779 if (complain & tf_error)
6780 error ("%qE is not a valid template argument for type %qT "
6781 "because it is not an object with linkage",
6782 expr, type);
6783 return NULL_TREE;
6784 }
6785
6786 /* DR 1155 allows internal linkage in C++11 and up. */
6787 linkage_kind linkage = decl_linkage (expr);
6788 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6789 {
6790 if (complain & tf_error)
6791 error ("%qE is not a valid template argument for type %qT "
6792 "because object %qD does not have linkage",
6793 expr, type, expr);
6794 return NULL_TREE;
6795 }
6796
6797 expr = build_address (expr);
6798 }
6799
6800 if (!same_type_p (type, TREE_TYPE (expr)))
6801 expr = build_nop (type, expr);
6802 }
6803 /* [temp.arg.nontype]/5, bullet 4
6804
6805 For a non-type template-parameter of type pointer to function, only
6806 the function-to-pointer conversion (_conv.func_) is applied. If the
6807 template-argument represents a set of overloaded functions (or a
6808 pointer to such), the matching function is selected from the set
6809 (_over.over_). */
6810 else if (TYPE_PTRFN_P (type))
6811 {
6812 /* If the argument is a template-id, we might not have enough
6813 context information to decay the pointer. */
6814 if (!type_unknown_p (expr_type))
6815 {
6816 expr = decay_conversion (expr, complain);
6817 if (expr == error_mark_node)
6818 return error_mark_node;
6819 }
6820
6821 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6822 /* Null pointer values are OK in C++11. */
6823 return perform_qualification_conversions (type, expr);
6824
6825 expr = convert_nontype_argument_function (type, expr, complain);
6826 if (!expr || expr == error_mark_node)
6827 return expr;
6828 }
6829 /* [temp.arg.nontype]/5, bullet 5
6830
6831 For a non-type template-parameter of type reference to function, no
6832 conversions apply. If the template-argument represents a set of
6833 overloaded functions, the matching function is selected from the set
6834 (_over.over_). */
6835 else if (TYPE_REFFN_P (type))
6836 {
6837 if (TREE_CODE (expr) == ADDR_EXPR)
6838 {
6839 if (complain & tf_error)
6840 {
6841 error ("%qE is not a valid template argument for type %qT "
6842 "because it is a pointer", expr, type);
6843 inform (input_location, "try using %qE instead",
6844 TREE_OPERAND (expr, 0));
6845 }
6846 return NULL_TREE;
6847 }
6848
6849 expr = convert_nontype_argument_function (type, expr, complain);
6850 if (!expr || expr == error_mark_node)
6851 return expr;
6852 }
6853 /* [temp.arg.nontype]/5, bullet 6
6854
6855 For a non-type template-parameter of type pointer to member function,
6856 no conversions apply. If the template-argument represents a set of
6857 overloaded member functions, the matching member function is selected
6858 from the set (_over.over_). */
6859 else if (TYPE_PTRMEMFUNC_P (type))
6860 {
6861 expr = instantiate_type (type, expr, tf_none);
6862 if (expr == error_mark_node)
6863 return error_mark_node;
6864
6865 /* [temp.arg.nontype] bullet 1 says the pointer to member
6866 expression must be a pointer-to-member constant. */
6867 if (!value_dependent_expression_p (expr)
6868 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6869 return NULL_TREE;
6870
6871 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6872 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6873 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6874 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6875 }
6876 /* [temp.arg.nontype]/5, bullet 7
6877
6878 For a non-type template-parameter of type pointer to data member,
6879 qualification conversions (_conv.qual_) are applied. */
6880 else if (TYPE_PTRDATAMEM_P (type))
6881 {
6882 /* [temp.arg.nontype] bullet 1 says the pointer to member
6883 expression must be a pointer-to-member constant. */
6884 if (!value_dependent_expression_p (expr)
6885 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6886 return NULL_TREE;
6887
6888 expr = perform_qualification_conversions (type, expr);
6889 if (expr == error_mark_node)
6890 return expr;
6891 }
6892 else if (NULLPTR_TYPE_P (type))
6893 {
6894 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6895 {
6896 if (complain & tf_error)
6897 error ("%qE is not a valid template argument for type %qT "
6898 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6899 return NULL_TREE;
6900 }
6901 return expr;
6902 }
6903 /* A template non-type parameter must be one of the above. */
6904 else
6905 gcc_unreachable ();
6906
6907 /* Sanity check: did we actually convert the argument to the
6908 right type? */
6909 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6910 (type, TREE_TYPE (expr)));
6911 return convert_from_reference (expr);
6912 }
6913
6914 /* Subroutine of coerce_template_template_parms, which returns 1 if
6915 PARM_PARM and ARG_PARM match using the rule for the template
6916 parameters of template template parameters. Both PARM and ARG are
6917 template parameters; the rest of the arguments are the same as for
6918 coerce_template_template_parms.
6919 */
6920 static int
6921 coerce_template_template_parm (tree parm,
6922 tree arg,
6923 tsubst_flags_t complain,
6924 tree in_decl,
6925 tree outer_args)
6926 {
6927 if (arg == NULL_TREE || error_operand_p (arg)
6928 || parm == NULL_TREE || error_operand_p (parm))
6929 return 0;
6930
6931 if (TREE_CODE (arg) != TREE_CODE (parm))
6932 return 0;
6933
6934 switch (TREE_CODE (parm))
6935 {
6936 case TEMPLATE_DECL:
6937 /* We encounter instantiations of templates like
6938 template <template <template <class> class> class TT>
6939 class C; */
6940 {
6941 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6942 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6943
6944 if (!coerce_template_template_parms
6945 (parmparm, argparm, complain, in_decl, outer_args))
6946 return 0;
6947 }
6948 /* Fall through. */
6949
6950 case TYPE_DECL:
6951 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6952 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6953 /* Argument is a parameter pack but parameter is not. */
6954 return 0;
6955 break;
6956
6957 case PARM_DECL:
6958 /* The tsubst call is used to handle cases such as
6959
6960 template <int> class C {};
6961 template <class T, template <T> class TT> class D {};
6962 D<int, C> d;
6963
6964 i.e. the parameter list of TT depends on earlier parameters. */
6965 if (!uses_template_parms (TREE_TYPE (arg)))
6966 {
6967 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6968 if (!uses_template_parms (t)
6969 && !same_type_p (t, TREE_TYPE (arg)))
6970 return 0;
6971 }
6972
6973 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6974 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6975 /* Argument is a parameter pack but parameter is not. */
6976 return 0;
6977
6978 break;
6979
6980 default:
6981 gcc_unreachable ();
6982 }
6983
6984 return 1;
6985 }
6986
6987 /* Coerce template argument list ARGLIST for use with template
6988 template-parameter TEMPL. */
6989
6990 static tree
6991 coerce_template_args_for_ttp (tree templ, tree arglist,
6992 tsubst_flags_t complain)
6993 {
6994 /* Consider an example where a template template parameter declared as
6995
6996 template <class T, class U = std::allocator<T> > class TT
6997
6998 The template parameter level of T and U are one level larger than
6999 of TT. To proper process the default argument of U, say when an
7000 instantiation `TT<int>' is seen, we need to build the full
7001 arguments containing {int} as the innermost level. Outer levels,
7002 available when not appearing as default template argument, can be
7003 obtained from the arguments of the enclosing template.
7004
7005 Suppose that TT is later substituted with std::vector. The above
7006 instantiation is `TT<int, std::allocator<T> >' with TT at
7007 level 1, and T at level 2, while the template arguments at level 1
7008 becomes {std::vector} and the inner level 2 is {int}. */
7009
7010 tree outer = DECL_CONTEXT (templ);
7011 if (outer)
7012 {
7013 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7014 /* We want arguments for the partial specialization, not arguments for
7015 the primary template. */
7016 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7017 else
7018 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7019 }
7020 else if (current_template_parms)
7021 {
7022 /* This is an argument of the current template, so we haven't set
7023 DECL_CONTEXT yet. */
7024 tree relevant_template_parms;
7025
7026 /* Parameter levels that are greater than the level of the given
7027 template template parm are irrelevant. */
7028 relevant_template_parms = current_template_parms;
7029 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7030 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7031 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7032
7033 outer = template_parms_to_args (relevant_template_parms);
7034 }
7035
7036 if (outer)
7037 arglist = add_to_template_args (outer, arglist);
7038
7039 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7040 return coerce_template_parms (parmlist, arglist, templ,
7041 complain,
7042 /*require_all_args=*/true,
7043 /*use_default_args=*/true);
7044 }
7045
7046 /* A cache of template template parameters with match-all default
7047 arguments. */
7048 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7049 static void
7050 store_defaulted_ttp (tree v, tree t)
7051 {
7052 if (!defaulted_ttp_cache)
7053 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7054 defaulted_ttp_cache->put (v, t);
7055 }
7056 static tree
7057 lookup_defaulted_ttp (tree v)
7058 {
7059 if (defaulted_ttp_cache)
7060 if (tree *p = defaulted_ttp_cache->get (v))
7061 return *p;
7062 return NULL_TREE;
7063 }
7064
7065 /* T is a bound template template-parameter. Copy its arguments into default
7066 arguments of the template template-parameter's template parameters. */
7067
7068 static tree
7069 add_defaults_to_ttp (tree otmpl)
7070 {
7071 if (tree c = lookup_defaulted_ttp (otmpl))
7072 return c;
7073
7074 tree ntmpl = copy_node (otmpl);
7075
7076 tree ntype = copy_node (TREE_TYPE (otmpl));
7077 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7078 TYPE_MAIN_VARIANT (ntype) = ntype;
7079 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7080 TYPE_NAME (ntype) = ntmpl;
7081 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7082
7083 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7084 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7085 TEMPLATE_PARM_DECL (idx) = ntmpl;
7086 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7087
7088 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7089 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7090 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7091 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7092 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7093 {
7094 tree o = TREE_VEC_ELT (vec, i);
7095 if (!template_parameter_pack_p (TREE_VALUE (o)))
7096 {
7097 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7098 TREE_PURPOSE (n) = any_targ_node;
7099 }
7100 }
7101
7102 store_defaulted_ttp (otmpl, ntmpl);
7103 return ntmpl;
7104 }
7105
7106 /* ARG is a bound potential template template-argument, and PARGS is a list
7107 of arguments for the corresponding template template-parameter. Adjust
7108 PARGS as appropriate for application to ARG's template, and if ARG is a
7109 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7110 arguments to the template template parameter. */
7111
7112 static tree
7113 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7114 {
7115 ++processing_template_decl;
7116 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7117 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7118 {
7119 /* When comparing two template template-parameters in partial ordering,
7120 rewrite the one currently being used as an argument to have default
7121 arguments for all parameters. */
7122 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7123 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7124 if (pargs != error_mark_node)
7125 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7126 TYPE_TI_ARGS (arg));
7127 }
7128 else
7129 {
7130 tree aparms
7131 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7132 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7133 /*require_all*/true,
7134 /*use_default*/true);
7135 }
7136 --processing_template_decl;
7137 return pargs;
7138 }
7139
7140 /* Subroutine of unify for the case when PARM is a
7141 BOUND_TEMPLATE_TEMPLATE_PARM. */
7142
7143 static int
7144 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7145 bool explain_p)
7146 {
7147 tree parmvec = TYPE_TI_ARGS (parm);
7148 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7149
7150 /* The template template parm might be variadic and the argument
7151 not, so flatten both argument lists. */
7152 parmvec = expand_template_argument_pack (parmvec);
7153 argvec = expand_template_argument_pack (argvec);
7154
7155 if (flag_new_ttp)
7156 {
7157 /* In keeping with P0522R0, adjust P's template arguments
7158 to apply to A's template; then flatten it again. */
7159 tree nparmvec = parmvec;
7160 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7161 nparmvec = expand_template_argument_pack (nparmvec);
7162
7163 if (unify (tparms, targs, nparmvec, argvec,
7164 UNIFY_ALLOW_NONE, explain_p))
7165 return 1;
7166
7167 /* If the P0522 adjustment eliminated a pack expansion, deduce
7168 empty packs. */
7169 if (flag_new_ttp
7170 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7171 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7172 DEDUCE_EXACT, /*sub*/true, explain_p))
7173 return 1;
7174 }
7175 else
7176 {
7177 /* Deduce arguments T, i from TT<T> or TT<i>.
7178 We check each element of PARMVEC and ARGVEC individually
7179 rather than the whole TREE_VEC since they can have
7180 different number of elements, which is allowed under N2555. */
7181
7182 int len = TREE_VEC_LENGTH (parmvec);
7183
7184 /* Check if the parameters end in a pack, making them
7185 variadic. */
7186 int parm_variadic_p = 0;
7187 if (len > 0
7188 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7189 parm_variadic_p = 1;
7190
7191 for (int i = 0; i < len - parm_variadic_p; ++i)
7192 /* If the template argument list of P contains a pack
7193 expansion that is not the last template argument, the
7194 entire template argument list is a non-deduced
7195 context. */
7196 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7197 return unify_success (explain_p);
7198
7199 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7200 return unify_too_few_arguments (explain_p,
7201 TREE_VEC_LENGTH (argvec), len);
7202
7203 for (int i = 0; i < len - parm_variadic_p; ++i)
7204 if (unify (tparms, targs,
7205 TREE_VEC_ELT (parmvec, i),
7206 TREE_VEC_ELT (argvec, i),
7207 UNIFY_ALLOW_NONE, explain_p))
7208 return 1;
7209
7210 if (parm_variadic_p
7211 && unify_pack_expansion (tparms, targs,
7212 parmvec, argvec,
7213 DEDUCE_EXACT,
7214 /*subr=*/true, explain_p))
7215 return 1;
7216 }
7217
7218 return 0;
7219 }
7220
7221 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7222 template template parameters. Both PARM_PARMS and ARG_PARMS are
7223 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7224 or PARM_DECL.
7225
7226 Consider the example:
7227 template <class T> class A;
7228 template<template <class U> class TT> class B;
7229
7230 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7231 the parameters to A, and OUTER_ARGS contains A. */
7232
7233 static int
7234 coerce_template_template_parms (tree parm_parms,
7235 tree arg_parms,
7236 tsubst_flags_t complain,
7237 tree in_decl,
7238 tree outer_args)
7239 {
7240 int nparms, nargs, i;
7241 tree parm, arg;
7242 int variadic_p = 0;
7243
7244 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7245 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7246
7247 nparms = TREE_VEC_LENGTH (parm_parms);
7248 nargs = TREE_VEC_LENGTH (arg_parms);
7249
7250 if (flag_new_ttp)
7251 {
7252 /* P0522R0: A template template-parameter P is at least as specialized as
7253 a template template-argument A if, given the following rewrite to two
7254 function templates, the function template corresponding to P is at
7255 least as specialized as the function template corresponding to A
7256 according to the partial ordering rules for function templates
7257 ([temp.func.order]). Given an invented class template X with the
7258 template parameter list of A (including default arguments):
7259
7260 * Each of the two function templates has the same template parameters,
7261 respectively, as P or A.
7262
7263 * Each function template has a single function parameter whose type is
7264 a specialization of X with template arguments corresponding to the
7265 template parameters from the respective function template where, for
7266 each template parameter PP in the template parameter list of the
7267 function template, a corresponding template argument AA is formed. If
7268 PP declares a parameter pack, then AA is the pack expansion
7269 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7270
7271 If the rewrite produces an invalid type, then P is not at least as
7272 specialized as A. */
7273
7274 /* So coerce P's args to apply to A's parms, and then deduce between A's
7275 args and the converted args. If that succeeds, A is at least as
7276 specialized as P, so they match.*/
7277 tree pargs = template_parms_level_to_args (parm_parms);
7278 ++processing_template_decl;
7279 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7280 /*require_all*/true, /*use_default*/true);
7281 --processing_template_decl;
7282 if (pargs != error_mark_node)
7283 {
7284 tree targs = make_tree_vec (nargs);
7285 tree aargs = template_parms_level_to_args (arg_parms);
7286 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7287 /*explain*/false))
7288 return 1;
7289 }
7290 }
7291
7292 /* Determine whether we have a parameter pack at the end of the
7293 template template parameter's template parameter list. */
7294 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7295 {
7296 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7297
7298 if (error_operand_p (parm))
7299 return 0;
7300
7301 switch (TREE_CODE (parm))
7302 {
7303 case TEMPLATE_DECL:
7304 case TYPE_DECL:
7305 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7306 variadic_p = 1;
7307 break;
7308
7309 case PARM_DECL:
7310 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7311 variadic_p = 1;
7312 break;
7313
7314 default:
7315 gcc_unreachable ();
7316 }
7317 }
7318
7319 if (nargs != nparms
7320 && !(variadic_p && nargs >= nparms - 1))
7321 return 0;
7322
7323 /* Check all of the template parameters except the parameter pack at
7324 the end (if any). */
7325 for (i = 0; i < nparms - variadic_p; ++i)
7326 {
7327 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7328 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7329 continue;
7330
7331 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7332 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7333
7334 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7335 outer_args))
7336 return 0;
7337
7338 }
7339
7340 if (variadic_p)
7341 {
7342 /* Check each of the template parameters in the template
7343 argument against the template parameter pack at the end of
7344 the template template parameter. */
7345 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7346 return 0;
7347
7348 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7349
7350 for (; i < nargs; ++i)
7351 {
7352 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7353 continue;
7354
7355 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7356
7357 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7358 outer_args))
7359 return 0;
7360 }
7361 }
7362
7363 return 1;
7364 }
7365
7366 /* Verifies that the deduced template arguments (in TARGS) for the
7367 template template parameters (in TPARMS) represent valid bindings,
7368 by comparing the template parameter list of each template argument
7369 to the template parameter list of its corresponding template
7370 template parameter, in accordance with DR150. This
7371 routine can only be called after all template arguments have been
7372 deduced. It will return TRUE if all of the template template
7373 parameter bindings are okay, FALSE otherwise. */
7374 bool
7375 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7376 {
7377 int i, ntparms = TREE_VEC_LENGTH (tparms);
7378 bool ret = true;
7379
7380 /* We're dealing with template parms in this process. */
7381 ++processing_template_decl;
7382
7383 targs = INNERMOST_TEMPLATE_ARGS (targs);
7384
7385 for (i = 0; i < ntparms; ++i)
7386 {
7387 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7388 tree targ = TREE_VEC_ELT (targs, i);
7389
7390 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7391 {
7392 tree packed_args = NULL_TREE;
7393 int idx, len = 1;
7394
7395 if (ARGUMENT_PACK_P (targ))
7396 {
7397 /* Look inside the argument pack. */
7398 packed_args = ARGUMENT_PACK_ARGS (targ);
7399 len = TREE_VEC_LENGTH (packed_args);
7400 }
7401
7402 for (idx = 0; idx < len; ++idx)
7403 {
7404 tree targ_parms = NULL_TREE;
7405
7406 if (packed_args)
7407 /* Extract the next argument from the argument
7408 pack. */
7409 targ = TREE_VEC_ELT (packed_args, idx);
7410
7411 if (PACK_EXPANSION_P (targ))
7412 /* Look at the pattern of the pack expansion. */
7413 targ = PACK_EXPANSION_PATTERN (targ);
7414
7415 /* Extract the template parameters from the template
7416 argument. */
7417 if (TREE_CODE (targ) == TEMPLATE_DECL)
7418 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7419 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7420 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7421
7422 /* Verify that we can coerce the template template
7423 parameters from the template argument to the template
7424 parameter. This requires an exact match. */
7425 if (targ_parms
7426 && !coerce_template_template_parms
7427 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7428 targ_parms,
7429 tf_none,
7430 tparm,
7431 targs))
7432 {
7433 ret = false;
7434 goto out;
7435 }
7436 }
7437 }
7438 }
7439
7440 out:
7441
7442 --processing_template_decl;
7443 return ret;
7444 }
7445
7446 /* Since type attributes aren't mangled, we need to strip them from
7447 template type arguments. */
7448
7449 static tree
7450 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7451 {
7452 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7453 return arg;
7454 bool removed_attributes = false;
7455 tree canon = strip_typedefs (arg, &removed_attributes);
7456 if (removed_attributes
7457 && (complain & tf_warning))
7458 warning (OPT_Wignored_attributes,
7459 "ignoring attributes on template argument %qT", arg);
7460 return canon;
7461 }
7462
7463 /* And from inside dependent non-type arguments like sizeof(Type). */
7464
7465 static tree
7466 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7467 {
7468 if (!arg || arg == error_mark_node)
7469 return arg;
7470 bool removed_attributes = false;
7471 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7472 if (removed_attributes
7473 && (complain & tf_warning))
7474 warning (OPT_Wignored_attributes,
7475 "ignoring attributes in template argument %qE", arg);
7476 return canon;
7477 }
7478
7479 // A template declaration can be substituted for a constrained
7480 // template template parameter only when the argument is more
7481 // constrained than the parameter.
7482 static bool
7483 is_compatible_template_arg (tree parm, tree arg)
7484 {
7485 tree parm_cons = get_constraints (parm);
7486
7487 /* For now, allow constrained template template arguments
7488 and unconstrained template template parameters. */
7489 if (parm_cons == NULL_TREE)
7490 return true;
7491
7492 tree arg_cons = get_constraints (arg);
7493
7494 // If the template parameter is constrained, we need to rewrite its
7495 // constraints in terms of the ARG's template parameters. This ensures
7496 // that all of the template parameter types will have the same depth.
7497 //
7498 // Note that this is only valid when coerce_template_template_parm is
7499 // true for the innermost template parameters of PARM and ARG. In other
7500 // words, because coercion is successful, this conversion will be valid.
7501 if (parm_cons)
7502 {
7503 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7504 parm_cons = tsubst_constraint_info (parm_cons,
7505 INNERMOST_TEMPLATE_ARGS (args),
7506 tf_none, NULL_TREE);
7507 if (parm_cons == error_mark_node)
7508 return false;
7509 }
7510
7511 return subsumes (parm_cons, arg_cons);
7512 }
7513
7514 // Convert a placeholder argument into a binding to the original
7515 // parameter. The original parameter is saved as the TREE_TYPE of
7516 // ARG.
7517 static inline tree
7518 convert_wildcard_argument (tree parm, tree arg)
7519 {
7520 TREE_TYPE (arg) = parm;
7521 return arg;
7522 }
7523
7524 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7525 because one of them is dependent. But we need to represent the
7526 conversion for the benefit of cp_tree_equal. */
7527
7528 static tree
7529 maybe_convert_nontype_argument (tree type, tree arg)
7530 {
7531 /* Auto parms get no conversion. */
7532 if (type_uses_auto (type))
7533 return arg;
7534 /* We don't need or want to add this conversion now if we're going to use the
7535 argument for deduction. */
7536 if (value_dependent_expression_p (arg))
7537 return arg;
7538
7539 type = cv_unqualified (type);
7540 tree argtype = TREE_TYPE (arg);
7541 if (same_type_p (type, argtype))
7542 return arg;
7543
7544 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7545 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7546 return arg;
7547 }
7548
7549 /* Convert the indicated template ARG as necessary to match the
7550 indicated template PARM. Returns the converted ARG, or
7551 error_mark_node if the conversion was unsuccessful. Error and
7552 warning messages are issued under control of COMPLAIN. This
7553 conversion is for the Ith parameter in the parameter list. ARGS is
7554 the full set of template arguments deduced so far. */
7555
7556 static tree
7557 convert_template_argument (tree parm,
7558 tree arg,
7559 tree args,
7560 tsubst_flags_t complain,
7561 int i,
7562 tree in_decl)
7563 {
7564 tree orig_arg;
7565 tree val;
7566 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7567
7568 if (parm == error_mark_node)
7569 return error_mark_node;
7570
7571 /* Trivially convert placeholders. */
7572 if (TREE_CODE (arg) == WILDCARD_DECL)
7573 return convert_wildcard_argument (parm, arg);
7574
7575 if (arg == any_targ_node)
7576 return arg;
7577
7578 if (TREE_CODE (arg) == TREE_LIST
7579 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7580 {
7581 /* The template argument was the name of some
7582 member function. That's usually
7583 invalid, but static members are OK. In any
7584 case, grab the underlying fields/functions
7585 and issue an error later if required. */
7586 orig_arg = TREE_VALUE (arg);
7587 TREE_TYPE (arg) = unknown_type_node;
7588 }
7589
7590 orig_arg = arg;
7591
7592 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7593 requires_type = (TREE_CODE (parm) == TYPE_DECL
7594 || requires_tmpl_type);
7595
7596 /* When determining whether an argument pack expansion is a template,
7597 look at the pattern. */
7598 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7599 arg = PACK_EXPANSION_PATTERN (arg);
7600
7601 /* Deal with an injected-class-name used as a template template arg. */
7602 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7603 {
7604 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7605 if (TREE_CODE (t) == TEMPLATE_DECL)
7606 {
7607 if (cxx_dialect >= cxx11)
7608 /* OK under DR 1004. */;
7609 else if (complain & tf_warning_or_error)
7610 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7611 " used as template template argument", TYPE_NAME (arg));
7612 else if (flag_pedantic_errors)
7613 t = arg;
7614
7615 arg = t;
7616 }
7617 }
7618
7619 is_tmpl_type =
7620 ((TREE_CODE (arg) == TEMPLATE_DECL
7621 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7622 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7623 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7624 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7625
7626 if (is_tmpl_type
7627 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7628 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7629 arg = TYPE_STUB_DECL (arg);
7630
7631 is_type = TYPE_P (arg) || is_tmpl_type;
7632
7633 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7634 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7635 {
7636 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7637 {
7638 if (complain & tf_error)
7639 error ("invalid use of destructor %qE as a type", orig_arg);
7640 return error_mark_node;
7641 }
7642
7643 permerror (input_location,
7644 "to refer to a type member of a template parameter, "
7645 "use %<typename %E%>", orig_arg);
7646
7647 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7648 TREE_OPERAND (arg, 1),
7649 typename_type,
7650 complain);
7651 arg = orig_arg;
7652 is_type = 1;
7653 }
7654 if (is_type != requires_type)
7655 {
7656 if (in_decl)
7657 {
7658 if (complain & tf_error)
7659 {
7660 error ("type/value mismatch at argument %d in template "
7661 "parameter list for %qD",
7662 i + 1, in_decl);
7663 if (is_type)
7664 inform (input_location,
7665 " expected a constant of type %qT, got %qT",
7666 TREE_TYPE (parm),
7667 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7668 else if (requires_tmpl_type)
7669 inform (input_location,
7670 " expected a class template, got %qE", orig_arg);
7671 else
7672 inform (input_location,
7673 " expected a type, got %qE", orig_arg);
7674 }
7675 }
7676 return error_mark_node;
7677 }
7678 if (is_tmpl_type ^ requires_tmpl_type)
7679 {
7680 if (in_decl && (complain & tf_error))
7681 {
7682 error ("type/value mismatch at argument %d in template "
7683 "parameter list for %qD",
7684 i + 1, in_decl);
7685 if (is_tmpl_type)
7686 inform (input_location,
7687 " expected a type, got %qT", DECL_NAME (arg));
7688 else
7689 inform (input_location,
7690 " expected a class template, got %qT", orig_arg);
7691 }
7692 return error_mark_node;
7693 }
7694
7695 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7696 /* We already did the appropriate conversion when packing args. */
7697 val = orig_arg;
7698 else if (is_type)
7699 {
7700 if (requires_tmpl_type)
7701 {
7702 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7703 /* The number of argument required is not known yet.
7704 Just accept it for now. */
7705 val = orig_arg;
7706 else
7707 {
7708 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7709 tree argparm;
7710
7711 /* Strip alias templates that are equivalent to another
7712 template. */
7713 arg = get_underlying_template (arg);
7714 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7715
7716 if (coerce_template_template_parms (parmparm, argparm,
7717 complain, in_decl,
7718 args))
7719 {
7720 val = arg;
7721
7722 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7723 TEMPLATE_DECL. */
7724 if (val != error_mark_node)
7725 {
7726 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7727 val = TREE_TYPE (val);
7728 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7729 val = make_pack_expansion (val, complain);
7730 }
7731 }
7732 else
7733 {
7734 if (in_decl && (complain & tf_error))
7735 {
7736 error ("type/value mismatch at argument %d in "
7737 "template parameter list for %qD",
7738 i + 1, in_decl);
7739 inform (input_location,
7740 " expected a template of type %qD, got %qT",
7741 parm, orig_arg);
7742 }
7743
7744 val = error_mark_node;
7745 }
7746
7747 // Check that the constraints are compatible before allowing the
7748 // substitution.
7749 if (val != error_mark_node)
7750 if (!is_compatible_template_arg (parm, arg))
7751 {
7752 if (in_decl && (complain & tf_error))
7753 {
7754 error ("constraint mismatch at argument %d in "
7755 "template parameter list for %qD",
7756 i + 1, in_decl);
7757 inform (input_location, " expected %qD but got %qD",
7758 parm, arg);
7759 }
7760 val = error_mark_node;
7761 }
7762 }
7763 }
7764 else
7765 val = orig_arg;
7766 /* We only form one instance of each template specialization.
7767 Therefore, if we use a non-canonical variant (i.e., a
7768 typedef), any future messages referring to the type will use
7769 the typedef, which is confusing if those future uses do not
7770 themselves also use the typedef. */
7771 if (TYPE_P (val))
7772 val = canonicalize_type_argument (val, complain);
7773 }
7774 else
7775 {
7776 tree t = TREE_TYPE (parm);
7777
7778 if (tree a = type_uses_auto (t))
7779 {
7780 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7781 if (t == error_mark_node)
7782 return error_mark_node;
7783 }
7784 else
7785 t = tsubst (t, args, complain, in_decl);
7786
7787 if (invalid_nontype_parm_type_p (t, complain))
7788 return error_mark_node;
7789
7790 if (!type_dependent_expression_p (orig_arg)
7791 && !uses_template_parms (t))
7792 /* We used to call digest_init here. However, digest_init
7793 will report errors, which we don't want when complain
7794 is zero. More importantly, digest_init will try too
7795 hard to convert things: for example, `0' should not be
7796 converted to pointer type at this point according to
7797 the standard. Accepting this is not merely an
7798 extension, since deciding whether or not these
7799 conversions can occur is part of determining which
7800 function template to call, or whether a given explicit
7801 argument specification is valid. */
7802 val = convert_nontype_argument (t, orig_arg, complain);
7803 else
7804 {
7805 val = canonicalize_expr_argument (orig_arg, complain);
7806 val = maybe_convert_nontype_argument (t, val);
7807 }
7808
7809
7810 if (val == NULL_TREE)
7811 val = error_mark_node;
7812 else if (val == error_mark_node && (complain & tf_error))
7813 error ("could not convert template argument %qE from %qT to %qT",
7814 orig_arg, TREE_TYPE (orig_arg), t);
7815
7816 if (INDIRECT_REF_P (val))
7817 {
7818 /* Reject template arguments that are references to built-in
7819 functions with no library fallbacks. */
7820 const_tree inner = TREE_OPERAND (val, 0);
7821 const_tree innertype = TREE_TYPE (inner);
7822 if (innertype
7823 && TREE_CODE (innertype) == REFERENCE_TYPE
7824 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7825 && 0 < TREE_OPERAND_LENGTH (inner)
7826 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7827 return error_mark_node;
7828 }
7829
7830 if (TREE_CODE (val) == SCOPE_REF)
7831 {
7832 /* Strip typedefs from the SCOPE_REF. */
7833 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7834 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7835 complain);
7836 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7837 QUALIFIED_NAME_IS_TEMPLATE (val));
7838 }
7839 }
7840
7841 return val;
7842 }
7843
7844 /* Coerces the remaining template arguments in INNER_ARGS (from
7845 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7846 Returns the coerced argument pack. PARM_IDX is the position of this
7847 parameter in the template parameter list. ARGS is the original
7848 template argument list. */
7849 static tree
7850 coerce_template_parameter_pack (tree parms,
7851 int parm_idx,
7852 tree args,
7853 tree inner_args,
7854 int arg_idx,
7855 tree new_args,
7856 int* lost,
7857 tree in_decl,
7858 tsubst_flags_t complain)
7859 {
7860 tree parm = TREE_VEC_ELT (parms, parm_idx);
7861 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7862 tree packed_args;
7863 tree argument_pack;
7864 tree packed_parms = NULL_TREE;
7865
7866 if (arg_idx > nargs)
7867 arg_idx = nargs;
7868
7869 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7870 {
7871 /* When the template parameter is a non-type template parameter pack
7872 or template template parameter pack whose type or template
7873 parameters use parameter packs, we know exactly how many arguments
7874 we are looking for. Build a vector of the instantiated decls for
7875 these template parameters in PACKED_PARMS. */
7876 /* We can't use make_pack_expansion here because it would interpret a
7877 _DECL as a use rather than a declaration. */
7878 tree decl = TREE_VALUE (parm);
7879 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7880 SET_PACK_EXPANSION_PATTERN (exp, decl);
7881 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7882 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7883
7884 TREE_VEC_LENGTH (args)--;
7885 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7886 TREE_VEC_LENGTH (args)++;
7887
7888 if (packed_parms == error_mark_node)
7889 return error_mark_node;
7890
7891 /* If we're doing a partial instantiation of a member template,
7892 verify that all of the types used for the non-type
7893 template parameter pack are, in fact, valid for non-type
7894 template parameters. */
7895 if (arg_idx < nargs
7896 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7897 {
7898 int j, len = TREE_VEC_LENGTH (packed_parms);
7899 for (j = 0; j < len; ++j)
7900 {
7901 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7902 if (invalid_nontype_parm_type_p (t, complain))
7903 return error_mark_node;
7904 }
7905 /* We don't know how many args we have yet, just
7906 use the unconverted ones for now. */
7907 return NULL_TREE;
7908 }
7909
7910 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7911 }
7912 /* Check if we have a placeholder pack, which indicates we're
7913 in the context of a introduction list. In that case we want
7914 to match this pack to the single placeholder. */
7915 else if (arg_idx < nargs
7916 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7917 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7918 {
7919 nargs = arg_idx + 1;
7920 packed_args = make_tree_vec (1);
7921 }
7922 else
7923 packed_args = make_tree_vec (nargs - arg_idx);
7924
7925 /* Convert the remaining arguments, which will be a part of the
7926 parameter pack "parm". */
7927 int first_pack_arg = arg_idx;
7928 for (; arg_idx < nargs; ++arg_idx)
7929 {
7930 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7931 tree actual_parm = TREE_VALUE (parm);
7932 int pack_idx = arg_idx - first_pack_arg;
7933
7934 if (packed_parms)
7935 {
7936 /* Once we've packed as many args as we have types, stop. */
7937 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7938 break;
7939 else if (PACK_EXPANSION_P (arg))
7940 /* We don't know how many args we have yet, just
7941 use the unconverted ones for now. */
7942 return NULL_TREE;
7943 else
7944 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7945 }
7946
7947 if (arg == error_mark_node)
7948 {
7949 if (complain & tf_error)
7950 error ("template argument %d is invalid", arg_idx + 1);
7951 }
7952 else
7953 arg = convert_template_argument (actual_parm,
7954 arg, new_args, complain, parm_idx,
7955 in_decl);
7956 if (arg == error_mark_node)
7957 (*lost)++;
7958 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7959 }
7960
7961 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7962 && TREE_VEC_LENGTH (packed_args) > 0)
7963 {
7964 if (complain & tf_error)
7965 error ("wrong number of template arguments (%d, should be %d)",
7966 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7967 return error_mark_node;
7968 }
7969
7970 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7971 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7972 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7973 else
7974 {
7975 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7976 TREE_CONSTANT (argument_pack) = 1;
7977 }
7978
7979 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7980 if (CHECKING_P)
7981 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7982 TREE_VEC_LENGTH (packed_args));
7983 return argument_pack;
7984 }
7985
7986 /* Returns the number of pack expansions in the template argument vector
7987 ARGS. */
7988
7989 static int
7990 pack_expansion_args_count (tree args)
7991 {
7992 int i;
7993 int count = 0;
7994 if (args)
7995 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7996 {
7997 tree elt = TREE_VEC_ELT (args, i);
7998 if (elt && PACK_EXPANSION_P (elt))
7999 ++count;
8000 }
8001 return count;
8002 }
8003
8004 /* Convert all template arguments to their appropriate types, and
8005 return a vector containing the innermost resulting template
8006 arguments. If any error occurs, return error_mark_node. Error and
8007 warning messages are issued under control of COMPLAIN.
8008
8009 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8010 for arguments not specified in ARGS. Otherwise, if
8011 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8012 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8013 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8014 ARGS. */
8015
8016 static tree
8017 coerce_template_parms (tree parms,
8018 tree args,
8019 tree in_decl,
8020 tsubst_flags_t complain,
8021 bool require_all_args,
8022 bool use_default_args)
8023 {
8024 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8025 tree orig_inner_args;
8026 tree inner_args;
8027 tree new_args;
8028 tree new_inner_args;
8029 int saved_unevaluated_operand;
8030 int saved_inhibit_evaluation_warnings;
8031
8032 /* When used as a boolean value, indicates whether this is a
8033 variadic template parameter list. Since it's an int, we can also
8034 subtract it from nparms to get the number of non-variadic
8035 parameters. */
8036 int variadic_p = 0;
8037 int variadic_args_p = 0;
8038 int post_variadic_parms = 0;
8039
8040 /* Likewise for parameters with default arguments. */
8041 int default_p = 0;
8042
8043 if (args == error_mark_node)
8044 return error_mark_node;
8045
8046 nparms = TREE_VEC_LENGTH (parms);
8047
8048 /* Determine if there are any parameter packs or default arguments. */
8049 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8050 {
8051 tree parm = TREE_VEC_ELT (parms, parm_idx);
8052 if (variadic_p)
8053 ++post_variadic_parms;
8054 if (template_parameter_pack_p (TREE_VALUE (parm)))
8055 ++variadic_p;
8056 if (TREE_PURPOSE (parm))
8057 ++default_p;
8058 }
8059
8060 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8061 /* If there are no parameters that follow a parameter pack, we need to
8062 expand any argument packs so that we can deduce a parameter pack from
8063 some non-packed args followed by an argument pack, as in variadic85.C.
8064 If there are such parameters, we need to leave argument packs intact
8065 so the arguments are assigned properly. This can happen when dealing
8066 with a nested class inside a partial specialization of a class
8067 template, as in variadic92.C, or when deducing a template parameter pack
8068 from a sub-declarator, as in variadic114.C. */
8069 if (!post_variadic_parms)
8070 inner_args = expand_template_argument_pack (inner_args);
8071
8072 /* Count any pack expansion args. */
8073 variadic_args_p = pack_expansion_args_count (inner_args);
8074
8075 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8076 if ((nargs - variadic_args_p > nparms && !variadic_p)
8077 || (nargs < nparms - variadic_p
8078 && require_all_args
8079 && !variadic_args_p
8080 && (!use_default_args
8081 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8082 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8083 {
8084 if (complain & tf_error)
8085 {
8086 if (variadic_p || default_p)
8087 {
8088 nparms -= variadic_p + default_p;
8089 error ("wrong number of template arguments "
8090 "(%d, should be at least %d)", nargs, nparms);
8091 }
8092 else
8093 error ("wrong number of template arguments "
8094 "(%d, should be %d)", nargs, nparms);
8095
8096 if (in_decl)
8097 inform (DECL_SOURCE_LOCATION (in_decl),
8098 "provided for %qD", in_decl);
8099 }
8100
8101 return error_mark_node;
8102 }
8103 /* We can't pass a pack expansion to a non-pack parameter of an alias
8104 template (DR 1430). */
8105 else if (in_decl
8106 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8107 || concept_template_p (in_decl))
8108 && variadic_args_p
8109 && nargs - variadic_args_p < nparms - variadic_p)
8110 {
8111 if (complain & tf_error)
8112 {
8113 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8114 {
8115 tree arg = TREE_VEC_ELT (inner_args, i);
8116 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8117
8118 if (PACK_EXPANSION_P (arg)
8119 && !template_parameter_pack_p (parm))
8120 {
8121 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8122 error_at (location_of (arg),
8123 "pack expansion argument for non-pack parameter "
8124 "%qD of alias template %qD", parm, in_decl);
8125 else
8126 error_at (location_of (arg),
8127 "pack expansion argument for non-pack parameter "
8128 "%qD of concept %qD", parm, in_decl);
8129 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8130 goto found;
8131 }
8132 }
8133 gcc_unreachable ();
8134 found:;
8135 }
8136 return error_mark_node;
8137 }
8138
8139 /* We need to evaluate the template arguments, even though this
8140 template-id may be nested within a "sizeof". */
8141 saved_unevaluated_operand = cp_unevaluated_operand;
8142 cp_unevaluated_operand = 0;
8143 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8144 c_inhibit_evaluation_warnings = 0;
8145 new_inner_args = make_tree_vec (nparms);
8146 new_args = add_outermost_template_args (args, new_inner_args);
8147 int pack_adjust = 0;
8148 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8149 {
8150 tree arg;
8151 tree parm;
8152
8153 /* Get the Ith template parameter. */
8154 parm = TREE_VEC_ELT (parms, parm_idx);
8155
8156 if (parm == error_mark_node)
8157 {
8158 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8159 continue;
8160 }
8161
8162 /* Calculate the next argument. */
8163 if (arg_idx < nargs)
8164 arg = TREE_VEC_ELT (inner_args, arg_idx);
8165 else
8166 arg = NULL_TREE;
8167
8168 if (template_parameter_pack_p (TREE_VALUE (parm))
8169 && !(arg && ARGUMENT_PACK_P (arg)))
8170 {
8171 /* Some arguments will be placed in the
8172 template parameter pack PARM. */
8173 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8174 inner_args, arg_idx,
8175 new_args, &lost,
8176 in_decl, complain);
8177
8178 if (arg == NULL_TREE)
8179 {
8180 /* We don't know how many args we have yet, just use the
8181 unconverted (and still packed) ones for now. */
8182 new_inner_args = orig_inner_args;
8183 arg_idx = nargs;
8184 break;
8185 }
8186
8187 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8188
8189 /* Store this argument. */
8190 if (arg == error_mark_node)
8191 {
8192 lost++;
8193 /* We are done with all of the arguments. */
8194 arg_idx = nargs;
8195 }
8196 else
8197 {
8198 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8199 arg_idx += pack_adjust;
8200 }
8201
8202 continue;
8203 }
8204 else if (arg)
8205 {
8206 if (PACK_EXPANSION_P (arg))
8207 {
8208 /* "If every valid specialization of a variadic template
8209 requires an empty template parameter pack, the template is
8210 ill-formed, no diagnostic required." So check that the
8211 pattern works with this parameter. */
8212 tree pattern = PACK_EXPANSION_PATTERN (arg);
8213 tree conv = convert_template_argument (TREE_VALUE (parm),
8214 pattern, new_args,
8215 complain, parm_idx,
8216 in_decl);
8217 if (conv == error_mark_node)
8218 {
8219 if (complain & tf_error)
8220 inform (input_location, "so any instantiation with a "
8221 "non-empty parameter pack would be ill-formed");
8222 ++lost;
8223 }
8224 else if (TYPE_P (conv) && !TYPE_P (pattern))
8225 /* Recover from missing typename. */
8226 TREE_VEC_ELT (inner_args, arg_idx)
8227 = make_pack_expansion (conv, complain);
8228
8229 /* We don't know how many args we have yet, just
8230 use the unconverted ones for now. */
8231 new_inner_args = inner_args;
8232 arg_idx = nargs;
8233 break;
8234 }
8235 }
8236 else if (require_all_args)
8237 {
8238 /* There must be a default arg in this case. */
8239 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8240 complain, in_decl);
8241 /* The position of the first default template argument,
8242 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8243 Record that. */
8244 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8245 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8246 arg_idx - pack_adjust);
8247 }
8248 else
8249 break;
8250
8251 if (arg == error_mark_node)
8252 {
8253 if (complain & tf_error)
8254 error ("template argument %d is invalid", arg_idx + 1);
8255 }
8256 else if (!arg)
8257 /* This only occurs if there was an error in the template
8258 parameter list itself (which we would already have
8259 reported) that we are trying to recover from, e.g., a class
8260 template with a parameter list such as
8261 template<typename..., typename>. */
8262 ++lost;
8263 else
8264 arg = convert_template_argument (TREE_VALUE (parm),
8265 arg, new_args, complain,
8266 parm_idx, in_decl);
8267
8268 if (arg == error_mark_node)
8269 lost++;
8270 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8271 }
8272 cp_unevaluated_operand = saved_unevaluated_operand;
8273 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8274
8275 if (variadic_p && arg_idx < nargs)
8276 {
8277 if (complain & tf_error)
8278 {
8279 error ("wrong number of template arguments "
8280 "(%d, should be %d)", nargs, arg_idx);
8281 if (in_decl)
8282 error ("provided for %q+D", in_decl);
8283 }
8284 return error_mark_node;
8285 }
8286
8287 if (lost)
8288 return error_mark_node;
8289
8290 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8291 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8292 TREE_VEC_LENGTH (new_inner_args));
8293
8294 return new_inner_args;
8295 }
8296
8297 /* Convert all template arguments to their appropriate types, and
8298 return a vector containing the innermost resulting template
8299 arguments. If any error occurs, return error_mark_node. Error and
8300 warning messages are not issued.
8301
8302 Note that no function argument deduction is performed, and default
8303 arguments are used to fill in unspecified arguments. */
8304 tree
8305 coerce_template_parms (tree parms, tree args, tree in_decl)
8306 {
8307 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8308 }
8309
8310 /* Convert all template arguments to their appropriate type, and
8311 instantiate default arguments as needed. This returns a vector
8312 containing the innermost resulting template arguments, or
8313 error_mark_node if unsuccessful. */
8314 tree
8315 coerce_template_parms (tree parms, tree args, tree in_decl,
8316 tsubst_flags_t complain)
8317 {
8318 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8319 }
8320
8321 /* Like coerce_template_parms. If PARMS represents all template
8322 parameters levels, this function returns a vector of vectors
8323 representing all the resulting argument levels. Note that in this
8324 case, only the innermost arguments are coerced because the
8325 outermost ones are supposed to have been coerced already.
8326
8327 Otherwise, if PARMS represents only (the innermost) vector of
8328 parameters, this function returns a vector containing just the
8329 innermost resulting arguments. */
8330
8331 static tree
8332 coerce_innermost_template_parms (tree parms,
8333 tree args,
8334 tree in_decl,
8335 tsubst_flags_t complain,
8336 bool require_all_args,
8337 bool use_default_args)
8338 {
8339 int parms_depth = TMPL_PARMS_DEPTH (parms);
8340 int args_depth = TMPL_ARGS_DEPTH (args);
8341 tree coerced_args;
8342
8343 if (parms_depth > 1)
8344 {
8345 coerced_args = make_tree_vec (parms_depth);
8346 tree level;
8347 int cur_depth;
8348
8349 for (level = parms, cur_depth = parms_depth;
8350 parms_depth > 0 && level != NULL_TREE;
8351 level = TREE_CHAIN (level), --cur_depth)
8352 {
8353 tree l;
8354 if (cur_depth == args_depth)
8355 l = coerce_template_parms (TREE_VALUE (level),
8356 args, in_decl, complain,
8357 require_all_args,
8358 use_default_args);
8359 else
8360 l = TMPL_ARGS_LEVEL (args, cur_depth);
8361
8362 if (l == error_mark_node)
8363 return error_mark_node;
8364
8365 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8366 }
8367 }
8368 else
8369 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8370 args, in_decl, complain,
8371 require_all_args,
8372 use_default_args);
8373 return coerced_args;
8374 }
8375
8376 /* Returns 1 if template args OT and NT are equivalent. */
8377
8378 int
8379 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8380 {
8381 if (nt == ot)
8382 return 1;
8383 if (nt == NULL_TREE || ot == NULL_TREE)
8384 return false;
8385 if (nt == any_targ_node || ot == any_targ_node)
8386 return true;
8387
8388 if (TREE_CODE (nt) == TREE_VEC)
8389 /* For member templates */
8390 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8391 else if (PACK_EXPANSION_P (ot))
8392 return (PACK_EXPANSION_P (nt)
8393 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8394 PACK_EXPANSION_PATTERN (nt))
8395 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8396 PACK_EXPANSION_EXTRA_ARGS (nt)));
8397 else if (ARGUMENT_PACK_P (ot))
8398 {
8399 int i, len;
8400 tree opack, npack;
8401
8402 if (!ARGUMENT_PACK_P (nt))
8403 return 0;
8404
8405 opack = ARGUMENT_PACK_ARGS (ot);
8406 npack = ARGUMENT_PACK_ARGS (nt);
8407 len = TREE_VEC_LENGTH (opack);
8408 if (TREE_VEC_LENGTH (npack) != len)
8409 return 0;
8410 for (i = 0; i < len; ++i)
8411 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8412 TREE_VEC_ELT (npack, i)))
8413 return 0;
8414 return 1;
8415 }
8416 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8417 gcc_unreachable ();
8418 else if (TYPE_P (nt))
8419 {
8420 if (!TYPE_P (ot))
8421 return false;
8422 /* Don't treat an alias template specialization with dependent
8423 arguments as equivalent to its underlying type when used as a
8424 template argument; we need them to be distinct so that we
8425 substitute into the specialization arguments at instantiation
8426 time. And aliases can't be equivalent without being ==, so
8427 we don't need to look any deeper.
8428
8429 During partial ordering, however, we need to treat them normally so
8430 that we can order uses of the same alias with different
8431 cv-qualification (79960). */
8432 if (!partial_order
8433 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8434 return false;
8435 else
8436 return same_type_p (ot, nt);
8437 }
8438 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8439 return 0;
8440 else
8441 {
8442 /* Try to treat a template non-type argument that has been converted
8443 to the parameter type as equivalent to one that hasn't yet. */
8444 for (enum tree_code code1 = TREE_CODE (ot);
8445 CONVERT_EXPR_CODE_P (code1)
8446 || code1 == NON_LVALUE_EXPR;
8447 code1 = TREE_CODE (ot))
8448 ot = TREE_OPERAND (ot, 0);
8449 for (enum tree_code code2 = TREE_CODE (nt);
8450 CONVERT_EXPR_CODE_P (code2)
8451 || code2 == NON_LVALUE_EXPR;
8452 code2 = TREE_CODE (nt))
8453 nt = TREE_OPERAND (nt, 0);
8454
8455 return cp_tree_equal (ot, nt);
8456 }
8457 }
8458
8459 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8460 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8461 NEWARG_PTR with the offending arguments if they are non-NULL. */
8462
8463 int
8464 comp_template_args (tree oldargs, tree newargs,
8465 tree *oldarg_ptr, tree *newarg_ptr,
8466 bool partial_order)
8467 {
8468 int i;
8469
8470 if (oldargs == newargs)
8471 return 1;
8472
8473 if (!oldargs || !newargs)
8474 return 0;
8475
8476 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8477 return 0;
8478
8479 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8480 {
8481 tree nt = TREE_VEC_ELT (newargs, i);
8482 tree ot = TREE_VEC_ELT (oldargs, i);
8483
8484 if (! template_args_equal (ot, nt, partial_order))
8485 {
8486 if (oldarg_ptr != NULL)
8487 *oldarg_ptr = ot;
8488 if (newarg_ptr != NULL)
8489 *newarg_ptr = nt;
8490 return 0;
8491 }
8492 }
8493 return 1;
8494 }
8495
8496 inline bool
8497 comp_template_args_porder (tree oargs, tree nargs)
8498 {
8499 return comp_template_args (oargs, nargs, NULL, NULL, true);
8500 }
8501
8502 static void
8503 add_pending_template (tree d)
8504 {
8505 tree ti = (TYPE_P (d)
8506 ? CLASSTYPE_TEMPLATE_INFO (d)
8507 : DECL_TEMPLATE_INFO (d));
8508 struct pending_template *pt;
8509 int level;
8510
8511 if (TI_PENDING_TEMPLATE_FLAG (ti))
8512 return;
8513
8514 /* We are called both from instantiate_decl, where we've already had a
8515 tinst_level pushed, and instantiate_template, where we haven't.
8516 Compensate. */
8517 level = !current_tinst_level || current_tinst_level->decl != d;
8518
8519 if (level)
8520 push_tinst_level (d);
8521
8522 pt = ggc_alloc<pending_template> ();
8523 pt->next = NULL;
8524 pt->tinst = current_tinst_level;
8525 if (last_pending_template)
8526 last_pending_template->next = pt;
8527 else
8528 pending_templates = pt;
8529
8530 last_pending_template = pt;
8531
8532 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8533
8534 if (level)
8535 pop_tinst_level ();
8536 }
8537
8538
8539 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8540 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8541 documentation for TEMPLATE_ID_EXPR. */
8542
8543 tree
8544 lookup_template_function (tree fns, tree arglist)
8545 {
8546 tree type;
8547
8548 if (fns == error_mark_node || arglist == error_mark_node)
8549 return error_mark_node;
8550
8551 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8552
8553 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8554 {
8555 error ("%q#D is not a function template", fns);
8556 return error_mark_node;
8557 }
8558
8559 if (BASELINK_P (fns))
8560 {
8561 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8562 unknown_type_node,
8563 BASELINK_FUNCTIONS (fns),
8564 arglist);
8565 return fns;
8566 }
8567
8568 type = TREE_TYPE (fns);
8569 if (TREE_CODE (fns) == OVERLOAD || !type)
8570 type = unknown_type_node;
8571
8572 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8573 }
8574
8575 /* Within the scope of a template class S<T>, the name S gets bound
8576 (in build_self_reference) to a TYPE_DECL for the class, not a
8577 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8578 or one of its enclosing classes, and that type is a template,
8579 return the associated TEMPLATE_DECL. Otherwise, the original
8580 DECL is returned.
8581
8582 Also handle the case when DECL is a TREE_LIST of ambiguous
8583 injected-class-names from different bases. */
8584
8585 tree
8586 maybe_get_template_decl_from_type_decl (tree decl)
8587 {
8588 if (decl == NULL_TREE)
8589 return decl;
8590
8591 /* DR 176: A lookup that finds an injected-class-name (10.2
8592 [class.member.lookup]) can result in an ambiguity in certain cases
8593 (for example, if it is found in more than one base class). If all of
8594 the injected-class-names that are found refer to specializations of
8595 the same class template, and if the name is followed by a
8596 template-argument-list, the reference refers to the class template
8597 itself and not a specialization thereof, and is not ambiguous. */
8598 if (TREE_CODE (decl) == TREE_LIST)
8599 {
8600 tree t, tmpl = NULL_TREE;
8601 for (t = decl; t; t = TREE_CHAIN (t))
8602 {
8603 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8604 if (!tmpl)
8605 tmpl = elt;
8606 else if (tmpl != elt)
8607 break;
8608 }
8609 if (tmpl && t == NULL_TREE)
8610 return tmpl;
8611 else
8612 return decl;
8613 }
8614
8615 return (decl != NULL_TREE
8616 && DECL_SELF_REFERENCE_P (decl)
8617 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8618 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8619 }
8620
8621 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8622 parameters, find the desired type.
8623
8624 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8625
8626 IN_DECL, if non-NULL, is the template declaration we are trying to
8627 instantiate.
8628
8629 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8630 the class we are looking up.
8631
8632 Issue error and warning messages under control of COMPLAIN.
8633
8634 If the template class is really a local class in a template
8635 function, then the FUNCTION_CONTEXT is the function in which it is
8636 being instantiated.
8637
8638 ??? Note that this function is currently called *twice* for each
8639 template-id: the first time from the parser, while creating the
8640 incomplete type (finish_template_type), and the second type during the
8641 real instantiation (instantiate_template_class). This is surely something
8642 that we want to avoid. It also causes some problems with argument
8643 coercion (see convert_nontype_argument for more information on this). */
8644
8645 static tree
8646 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8647 int entering_scope, tsubst_flags_t complain)
8648 {
8649 tree templ = NULL_TREE, parmlist;
8650 tree t;
8651 spec_entry **slot;
8652 spec_entry *entry;
8653 spec_entry elt;
8654 hashval_t hash;
8655
8656 if (identifier_p (d1))
8657 {
8658 tree value = innermost_non_namespace_value (d1);
8659 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8660 templ = value;
8661 else
8662 {
8663 if (context)
8664 push_decl_namespace (context);
8665 templ = lookup_name (d1);
8666 templ = maybe_get_template_decl_from_type_decl (templ);
8667 if (context)
8668 pop_decl_namespace ();
8669 }
8670 if (templ)
8671 context = DECL_CONTEXT (templ);
8672 }
8673 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8674 {
8675 tree type = TREE_TYPE (d1);
8676
8677 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8678 an implicit typename for the second A. Deal with it. */
8679 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8680 type = TREE_TYPE (type);
8681
8682 if (CLASSTYPE_TEMPLATE_INFO (type))
8683 {
8684 templ = CLASSTYPE_TI_TEMPLATE (type);
8685 d1 = DECL_NAME (templ);
8686 }
8687 }
8688 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8689 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8690 {
8691 templ = TYPE_TI_TEMPLATE (d1);
8692 d1 = DECL_NAME (templ);
8693 }
8694 else if (DECL_TYPE_TEMPLATE_P (d1))
8695 {
8696 templ = d1;
8697 d1 = DECL_NAME (templ);
8698 context = DECL_CONTEXT (templ);
8699 }
8700 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8701 {
8702 templ = d1;
8703 d1 = DECL_NAME (templ);
8704 }
8705
8706 /* Issue an error message if we didn't find a template. */
8707 if (! templ)
8708 {
8709 if (complain & tf_error)
8710 error ("%qT is not a template", d1);
8711 return error_mark_node;
8712 }
8713
8714 if (TREE_CODE (templ) != TEMPLATE_DECL
8715 /* Make sure it's a user visible template, if it was named by
8716 the user. */
8717 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8718 && !PRIMARY_TEMPLATE_P (templ)))
8719 {
8720 if (complain & tf_error)
8721 {
8722 error ("non-template type %qT used as a template", d1);
8723 if (in_decl)
8724 error ("for template declaration %q+D", in_decl);
8725 }
8726 return error_mark_node;
8727 }
8728
8729 complain &= ~tf_user;
8730
8731 /* An alias that just changes the name of a template is equivalent to the
8732 other template, so if any of the arguments are pack expansions, strip
8733 the alias to avoid problems with a pack expansion passed to a non-pack
8734 alias template parameter (DR 1430). */
8735 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8736 templ = get_underlying_template (templ);
8737
8738 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8739 {
8740 tree parm;
8741 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8742 if (arglist2 == error_mark_node
8743 || (!uses_template_parms (arglist2)
8744 && check_instantiated_args (templ, arglist2, complain)))
8745 return error_mark_node;
8746
8747 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8748 return parm;
8749 }
8750 else
8751 {
8752 tree template_type = TREE_TYPE (templ);
8753 tree gen_tmpl;
8754 tree type_decl;
8755 tree found = NULL_TREE;
8756 int arg_depth;
8757 int parm_depth;
8758 int is_dependent_type;
8759 int use_partial_inst_tmpl = false;
8760
8761 if (template_type == error_mark_node)
8762 /* An error occurred while building the template TEMPL, and a
8763 diagnostic has most certainly been emitted for that
8764 already. Let's propagate that error. */
8765 return error_mark_node;
8766
8767 gen_tmpl = most_general_template (templ);
8768 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8769 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8770 arg_depth = TMPL_ARGS_DEPTH (arglist);
8771
8772 if (arg_depth == 1 && parm_depth > 1)
8773 {
8774 /* We've been given an incomplete set of template arguments.
8775 For example, given:
8776
8777 template <class T> struct S1 {
8778 template <class U> struct S2 {};
8779 template <class U> struct S2<U*> {};
8780 };
8781
8782 we will be called with an ARGLIST of `U*', but the
8783 TEMPLATE will be `template <class T> template
8784 <class U> struct S1<T>::S2'. We must fill in the missing
8785 arguments. */
8786 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8787 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8788 arg_depth = TMPL_ARGS_DEPTH (arglist);
8789 }
8790
8791 /* Now we should have enough arguments. */
8792 gcc_assert (parm_depth == arg_depth);
8793
8794 /* From here on, we're only interested in the most general
8795 template. */
8796
8797 /* Calculate the BOUND_ARGS. These will be the args that are
8798 actually tsubst'd into the definition to create the
8799 instantiation. */
8800 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8801 complain,
8802 /*require_all_args=*/true,
8803 /*use_default_args=*/true);
8804
8805 if (arglist == error_mark_node)
8806 /* We were unable to bind the arguments. */
8807 return error_mark_node;
8808
8809 /* In the scope of a template class, explicit references to the
8810 template class refer to the type of the template, not any
8811 instantiation of it. For example, in:
8812
8813 template <class T> class C { void f(C<T>); }
8814
8815 the `C<T>' is just the same as `C'. Outside of the
8816 class, however, such a reference is an instantiation. */
8817 if (entering_scope
8818 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8819 || currently_open_class (template_type))
8820 {
8821 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8822
8823 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8824 return template_type;
8825 }
8826
8827 /* If we already have this specialization, return it. */
8828 elt.tmpl = gen_tmpl;
8829 elt.args = arglist;
8830 elt.spec = NULL_TREE;
8831 hash = spec_hasher::hash (&elt);
8832 entry = type_specializations->find_with_hash (&elt, hash);
8833
8834 if (entry)
8835 return entry->spec;
8836
8837 /* If the the template's constraints are not satisfied,
8838 then we cannot form a valid type.
8839
8840 Note that the check is deferred until after the hash
8841 lookup. This prevents redundant checks on previously
8842 instantiated specializations. */
8843 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8844 {
8845 if (complain & tf_error)
8846 {
8847 error ("template constraint failure");
8848 diagnose_constraints (input_location, gen_tmpl, arglist);
8849 }
8850 return error_mark_node;
8851 }
8852
8853 is_dependent_type = uses_template_parms (arglist);
8854
8855 /* If the deduced arguments are invalid, then the binding
8856 failed. */
8857 if (!is_dependent_type
8858 && check_instantiated_args (gen_tmpl,
8859 INNERMOST_TEMPLATE_ARGS (arglist),
8860 complain))
8861 return error_mark_node;
8862
8863 if (!is_dependent_type
8864 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8865 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8866 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8867 {
8868 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8869 DECL_NAME (gen_tmpl),
8870 /*tag_scope=*/ts_global);
8871 return found;
8872 }
8873
8874 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8875 complain, in_decl);
8876 if (context == error_mark_node)
8877 return error_mark_node;
8878
8879 if (!context)
8880 context = global_namespace;
8881
8882 /* Create the type. */
8883 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8884 {
8885 /* The user referred to a specialization of an alias
8886 template represented by GEN_TMPL.
8887
8888 [temp.alias]/2 says:
8889
8890 When a template-id refers to the specialization of an
8891 alias template, it is equivalent to the associated
8892 type obtained by substitution of its
8893 template-arguments for the template-parameters in the
8894 type-id of the alias template. */
8895
8896 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8897 /* Note that the call above (by indirectly calling
8898 register_specialization in tsubst_decl) registers the
8899 TYPE_DECL representing the specialization of the alias
8900 template. So next time someone substitutes ARGLIST for
8901 the template parms into the alias template (GEN_TMPL),
8902 she'll get that TYPE_DECL back. */
8903
8904 if (t == error_mark_node)
8905 return t;
8906 }
8907 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8908 {
8909 if (!is_dependent_type)
8910 {
8911 set_current_access_from_decl (TYPE_NAME (template_type));
8912 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8913 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8914 arglist, complain, in_decl),
8915 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8916 arglist, complain, in_decl),
8917 SCOPED_ENUM_P (template_type), NULL);
8918
8919 if (t == error_mark_node)
8920 return t;
8921 }
8922 else
8923 {
8924 /* We don't want to call start_enum for this type, since
8925 the values for the enumeration constants may involve
8926 template parameters. And, no one should be interested
8927 in the enumeration constants for such a type. */
8928 t = cxx_make_type (ENUMERAL_TYPE);
8929 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8930 }
8931 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8932 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8933 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8934 }
8935 else if (CLASS_TYPE_P (template_type))
8936 {
8937 t = make_class_type (TREE_CODE (template_type));
8938 CLASSTYPE_DECLARED_CLASS (t)
8939 = CLASSTYPE_DECLARED_CLASS (template_type);
8940 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8941
8942 /* A local class. Make sure the decl gets registered properly. */
8943 if (context == current_function_decl)
8944 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8945
8946 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8947 /* This instantiation is another name for the primary
8948 template type. Set the TYPE_CANONICAL field
8949 appropriately. */
8950 TYPE_CANONICAL (t) = template_type;
8951 else if (any_template_arguments_need_structural_equality_p (arglist))
8952 /* Some of the template arguments require structural
8953 equality testing, so this template class requires
8954 structural equality testing. */
8955 SET_TYPE_STRUCTURAL_EQUALITY (t);
8956 }
8957 else
8958 gcc_unreachable ();
8959
8960 /* If we called start_enum or pushtag above, this information
8961 will already be set up. */
8962 if (!TYPE_NAME (t))
8963 {
8964 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8965
8966 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8967 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8968 DECL_SOURCE_LOCATION (type_decl)
8969 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8970 }
8971 else
8972 type_decl = TYPE_NAME (t);
8973
8974 if (CLASS_TYPE_P (template_type))
8975 {
8976 TREE_PRIVATE (type_decl)
8977 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8978 TREE_PROTECTED (type_decl)
8979 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8980 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8981 {
8982 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8983 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8984 }
8985 }
8986
8987 if (OVERLOAD_TYPE_P (t)
8988 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8989 {
8990 static const char *tags[] = {"abi_tag", "may_alias"};
8991
8992 for (unsigned ix = 0; ix != 2; ix++)
8993 {
8994 tree attributes
8995 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8996
8997 if (attributes)
8998 TYPE_ATTRIBUTES (t)
8999 = tree_cons (TREE_PURPOSE (attributes),
9000 TREE_VALUE (attributes),
9001 TYPE_ATTRIBUTES (t));
9002 }
9003 }
9004
9005 /* Let's consider the explicit specialization of a member
9006 of a class template specialization that is implicitly instantiated,
9007 e.g.:
9008 template<class T>
9009 struct S
9010 {
9011 template<class U> struct M {}; //#0
9012 };
9013
9014 template<>
9015 template<>
9016 struct S<int>::M<char> //#1
9017 {
9018 int i;
9019 };
9020 [temp.expl.spec]/4 says this is valid.
9021
9022 In this case, when we write:
9023 S<int>::M<char> m;
9024
9025 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9026 the one of #0.
9027
9028 When we encounter #1, we want to store the partial instantiation
9029 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9030
9031 For all cases other than this "explicit specialization of member of a
9032 class template", we just want to store the most general template into
9033 the CLASSTYPE_TI_TEMPLATE of M.
9034
9035 This case of "explicit specialization of member of a class template"
9036 only happens when:
9037 1/ the enclosing class is an instantiation of, and therefore not
9038 the same as, the context of the most general template, and
9039 2/ we aren't looking at the partial instantiation itself, i.e.
9040 the innermost arguments are not the same as the innermost parms of
9041 the most general template.
9042
9043 So it's only when 1/ and 2/ happens that we want to use the partial
9044 instantiation of the member template in lieu of its most general
9045 template. */
9046
9047 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9048 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9049 /* the enclosing class must be an instantiation... */
9050 && CLASS_TYPE_P (context)
9051 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9052 {
9053 TREE_VEC_LENGTH (arglist)--;
9054 ++processing_template_decl;
9055 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9056 tree partial_inst_args =
9057 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9058 arglist, complain, NULL_TREE);
9059 --processing_template_decl;
9060 TREE_VEC_LENGTH (arglist)++;
9061 if (partial_inst_args == error_mark_node)
9062 return error_mark_node;
9063 use_partial_inst_tmpl =
9064 /*...and we must not be looking at the partial instantiation
9065 itself. */
9066 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9067 partial_inst_args);
9068 }
9069
9070 if (!use_partial_inst_tmpl)
9071 /* This case is easy; there are no member templates involved. */
9072 found = gen_tmpl;
9073 else
9074 {
9075 /* This is a full instantiation of a member template. Find
9076 the partial instantiation of which this is an instance. */
9077
9078 /* Temporarily reduce by one the number of levels in the ARGLIST
9079 so as to avoid comparing the last set of arguments. */
9080 TREE_VEC_LENGTH (arglist)--;
9081 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9082 TREE_VEC_LENGTH (arglist)++;
9083 /* FOUND is either a proper class type, or an alias
9084 template specialization. In the later case, it's a
9085 TYPE_DECL, resulting from the substituting of arguments
9086 for parameters in the TYPE_DECL of the alias template
9087 done earlier. So be careful while getting the template
9088 of FOUND. */
9089 found = (TREE_CODE (found) == TEMPLATE_DECL
9090 ? found
9091 : (TREE_CODE (found) == TYPE_DECL
9092 ? DECL_TI_TEMPLATE (found)
9093 : CLASSTYPE_TI_TEMPLATE (found)));
9094 }
9095
9096 // Build template info for the new specialization.
9097 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9098
9099 elt.spec = t;
9100 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9101 entry = ggc_alloc<spec_entry> ();
9102 *entry = elt;
9103 *slot = entry;
9104
9105 /* Note this use of the partial instantiation so we can check it
9106 later in maybe_process_partial_specialization. */
9107 DECL_TEMPLATE_INSTANTIATIONS (found)
9108 = tree_cons (arglist, t,
9109 DECL_TEMPLATE_INSTANTIATIONS (found));
9110
9111 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9112 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9113 /* Now that the type has been registered on the instantiations
9114 list, we set up the enumerators. Because the enumeration
9115 constants may involve the enumeration type itself, we make
9116 sure to register the type first, and then create the
9117 constants. That way, doing tsubst_expr for the enumeration
9118 constants won't result in recursive calls here; we'll find
9119 the instantiation and exit above. */
9120 tsubst_enum (template_type, t, arglist);
9121
9122 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9123 /* If the type makes use of template parameters, the
9124 code that generates debugging information will crash. */
9125 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9126
9127 /* Possibly limit visibility based on template args. */
9128 TREE_PUBLIC (type_decl) = 1;
9129 determine_visibility (type_decl);
9130
9131 inherit_targ_abi_tags (t);
9132
9133 return t;
9134 }
9135 }
9136
9137 /* Wrapper for lookup_template_class_1. */
9138
9139 tree
9140 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9141 int entering_scope, tsubst_flags_t complain)
9142 {
9143 tree ret;
9144 timevar_push (TV_TEMPLATE_INST);
9145 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9146 entering_scope, complain);
9147 timevar_pop (TV_TEMPLATE_INST);
9148 return ret;
9149 }
9150
9151 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9152
9153 tree
9154 lookup_template_variable (tree templ, tree arglist)
9155 {
9156 /* The type of the expression is NULL_TREE since the template-id could refer
9157 to an explicit or partial specialization. */
9158 tree type = NULL_TREE;
9159 if (flag_concepts && variable_concept_p (templ))
9160 /* Except that concepts are always bool. */
9161 type = boolean_type_node;
9162 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9163 }
9164
9165 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9166
9167 tree
9168 finish_template_variable (tree var, tsubst_flags_t complain)
9169 {
9170 tree templ = TREE_OPERAND (var, 0);
9171 tree arglist = TREE_OPERAND (var, 1);
9172
9173 /* We never want to return a VAR_DECL for a variable concept, since they
9174 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9175 bool concept_p = flag_concepts && variable_concept_p (templ);
9176 if (concept_p && processing_template_decl)
9177 return var;
9178
9179 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9180 arglist = add_outermost_template_args (tmpl_args, arglist);
9181
9182 templ = most_general_template (templ);
9183 tree parms = DECL_TEMPLATE_PARMS (templ);
9184 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9185 /*req_all*/true,
9186 /*use_default*/true);
9187
9188 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9189 {
9190 if (complain & tf_error)
9191 {
9192 error ("use of invalid variable template %qE", var);
9193 diagnose_constraints (location_of (var), templ, arglist);
9194 }
9195 return error_mark_node;
9196 }
9197
9198 /* If a template-id refers to a specialization of a variable
9199 concept, then the expression is true if and only if the
9200 concept's constraints are satisfied by the given template
9201 arguments.
9202
9203 NOTE: This is an extension of Concepts Lite TS that
9204 allows constraints to be used in expressions. */
9205 if (concept_p)
9206 {
9207 tree decl = DECL_TEMPLATE_RESULT (templ);
9208 return evaluate_variable_concept (decl, arglist);
9209 }
9210
9211 return instantiate_template (templ, arglist, complain);
9212 }
9213
9214 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9215 TARGS template args, and instantiate it if it's not dependent. */
9216
9217 tree
9218 lookup_and_finish_template_variable (tree templ, tree targs,
9219 tsubst_flags_t complain)
9220 {
9221 templ = lookup_template_variable (templ, targs);
9222 if (!any_dependent_template_arguments_p (targs))
9223 {
9224 templ = finish_template_variable (templ, complain);
9225 mark_used (templ);
9226 }
9227
9228 return convert_from_reference (templ);
9229 }
9230
9231 \f
9232 struct pair_fn_data
9233 {
9234 tree_fn_t fn;
9235 tree_fn_t any_fn;
9236 void *data;
9237 /* True when we should also visit template parameters that occur in
9238 non-deduced contexts. */
9239 bool include_nondeduced_p;
9240 hash_set<tree> *visited;
9241 };
9242
9243 /* Called from for_each_template_parm via walk_tree. */
9244
9245 static tree
9246 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9247 {
9248 tree t = *tp;
9249 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9250 tree_fn_t fn = pfd->fn;
9251 void *data = pfd->data;
9252 tree result = NULL_TREE;
9253
9254 #define WALK_SUBTREE(NODE) \
9255 do \
9256 { \
9257 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9258 pfd->include_nondeduced_p, \
9259 pfd->any_fn); \
9260 if (result) goto out; \
9261 } \
9262 while (0)
9263
9264 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9265 return t;
9266
9267 if (TYPE_P (t)
9268 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9269 WALK_SUBTREE (TYPE_CONTEXT (t));
9270
9271 switch (TREE_CODE (t))
9272 {
9273 case RECORD_TYPE:
9274 if (TYPE_PTRMEMFUNC_P (t))
9275 break;
9276 /* Fall through. */
9277
9278 case UNION_TYPE:
9279 case ENUMERAL_TYPE:
9280 if (!TYPE_TEMPLATE_INFO (t))
9281 *walk_subtrees = 0;
9282 else
9283 WALK_SUBTREE (TYPE_TI_ARGS (t));
9284 break;
9285
9286 case INTEGER_TYPE:
9287 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9288 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9289 break;
9290
9291 case METHOD_TYPE:
9292 /* Since we're not going to walk subtrees, we have to do this
9293 explicitly here. */
9294 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9295 /* Fall through. */
9296
9297 case FUNCTION_TYPE:
9298 /* Check the return type. */
9299 WALK_SUBTREE (TREE_TYPE (t));
9300
9301 /* Check the parameter types. Since default arguments are not
9302 instantiated until they are needed, the TYPE_ARG_TYPES may
9303 contain expressions that involve template parameters. But,
9304 no-one should be looking at them yet. And, once they're
9305 instantiated, they don't contain template parameters, so
9306 there's no point in looking at them then, either. */
9307 {
9308 tree parm;
9309
9310 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9311 WALK_SUBTREE (TREE_VALUE (parm));
9312
9313 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9314 want walk_tree walking into them itself. */
9315 *walk_subtrees = 0;
9316 }
9317
9318 if (flag_noexcept_type)
9319 {
9320 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9321 if (spec)
9322 WALK_SUBTREE (TREE_PURPOSE (spec));
9323 }
9324 break;
9325
9326 case TYPEOF_TYPE:
9327 case UNDERLYING_TYPE:
9328 if (pfd->include_nondeduced_p
9329 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9330 pfd->visited,
9331 pfd->include_nondeduced_p,
9332 pfd->any_fn))
9333 return error_mark_node;
9334 break;
9335
9336 case FUNCTION_DECL:
9337 case VAR_DECL:
9338 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9339 WALK_SUBTREE (DECL_TI_ARGS (t));
9340 /* Fall through. */
9341
9342 case PARM_DECL:
9343 case CONST_DECL:
9344 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9345 WALK_SUBTREE (DECL_INITIAL (t));
9346 if (DECL_CONTEXT (t)
9347 && pfd->include_nondeduced_p)
9348 WALK_SUBTREE (DECL_CONTEXT (t));
9349 break;
9350
9351 case BOUND_TEMPLATE_TEMPLATE_PARM:
9352 /* Record template parameters such as `T' inside `TT<T>'. */
9353 WALK_SUBTREE (TYPE_TI_ARGS (t));
9354 /* Fall through. */
9355
9356 case TEMPLATE_TEMPLATE_PARM:
9357 case TEMPLATE_TYPE_PARM:
9358 case TEMPLATE_PARM_INDEX:
9359 if (fn && (*fn)(t, data))
9360 return t;
9361 else if (!fn)
9362 return t;
9363 break;
9364
9365 case TEMPLATE_DECL:
9366 /* A template template parameter is encountered. */
9367 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9368 WALK_SUBTREE (TREE_TYPE (t));
9369
9370 /* Already substituted template template parameter */
9371 *walk_subtrees = 0;
9372 break;
9373
9374 case TYPENAME_TYPE:
9375 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9376 partial instantiation. */
9377 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9378 break;
9379
9380 case CONSTRUCTOR:
9381 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9382 && pfd->include_nondeduced_p)
9383 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9384 break;
9385
9386 case INDIRECT_REF:
9387 case COMPONENT_REF:
9388 /* If there's no type, then this thing must be some expression
9389 involving template parameters. */
9390 if (!fn && !TREE_TYPE (t))
9391 return error_mark_node;
9392 break;
9393
9394 case MODOP_EXPR:
9395 case CAST_EXPR:
9396 case IMPLICIT_CONV_EXPR:
9397 case REINTERPRET_CAST_EXPR:
9398 case CONST_CAST_EXPR:
9399 case STATIC_CAST_EXPR:
9400 case DYNAMIC_CAST_EXPR:
9401 case ARROW_EXPR:
9402 case DOTSTAR_EXPR:
9403 case TYPEID_EXPR:
9404 case PSEUDO_DTOR_EXPR:
9405 if (!fn)
9406 return error_mark_node;
9407 break;
9408
9409 default:
9410 break;
9411 }
9412
9413 #undef WALK_SUBTREE
9414
9415 /* We didn't find any template parameters we liked. */
9416 out:
9417 return result;
9418 }
9419
9420 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9421 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9422 call FN with the parameter and the DATA.
9423 If FN returns nonzero, the iteration is terminated, and
9424 for_each_template_parm returns 1. Otherwise, the iteration
9425 continues. If FN never returns a nonzero value, the value
9426 returned by for_each_template_parm is 0. If FN is NULL, it is
9427 considered to be the function which always returns 1.
9428
9429 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9430 parameters that occur in non-deduced contexts. When false, only
9431 visits those template parameters that can be deduced. */
9432
9433 static tree
9434 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9435 hash_set<tree> *visited,
9436 bool include_nondeduced_p,
9437 tree_fn_t any_fn)
9438 {
9439 struct pair_fn_data pfd;
9440 tree result;
9441
9442 /* Set up. */
9443 pfd.fn = fn;
9444 pfd.any_fn = any_fn;
9445 pfd.data = data;
9446 pfd.include_nondeduced_p = include_nondeduced_p;
9447
9448 /* Walk the tree. (Conceptually, we would like to walk without
9449 duplicates, but for_each_template_parm_r recursively calls
9450 for_each_template_parm, so we would need to reorganize a fair
9451 bit to use walk_tree_without_duplicates, so we keep our own
9452 visited list.) */
9453 if (visited)
9454 pfd.visited = visited;
9455 else
9456 pfd.visited = new hash_set<tree>;
9457 result = cp_walk_tree (&t,
9458 for_each_template_parm_r,
9459 &pfd,
9460 pfd.visited);
9461
9462 /* Clean up. */
9463 if (!visited)
9464 {
9465 delete pfd.visited;
9466 pfd.visited = 0;
9467 }
9468
9469 return result;
9470 }
9471
9472 /* Returns true if T depends on any template parameter. */
9473
9474 int
9475 uses_template_parms (tree t)
9476 {
9477 if (t == NULL_TREE)
9478 return false;
9479
9480 bool dependent_p;
9481 int saved_processing_template_decl;
9482
9483 saved_processing_template_decl = processing_template_decl;
9484 if (!saved_processing_template_decl)
9485 processing_template_decl = 1;
9486 if (TYPE_P (t))
9487 dependent_p = dependent_type_p (t);
9488 else if (TREE_CODE (t) == TREE_VEC)
9489 dependent_p = any_dependent_template_arguments_p (t);
9490 else if (TREE_CODE (t) == TREE_LIST)
9491 dependent_p = (uses_template_parms (TREE_VALUE (t))
9492 || uses_template_parms (TREE_CHAIN (t)));
9493 else if (TREE_CODE (t) == TYPE_DECL)
9494 dependent_p = dependent_type_p (TREE_TYPE (t));
9495 else if (DECL_P (t)
9496 || EXPR_P (t)
9497 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9498 || TREE_CODE (t) == OVERLOAD
9499 || BASELINK_P (t)
9500 || identifier_p (t)
9501 || TREE_CODE (t) == TRAIT_EXPR
9502 || TREE_CODE (t) == CONSTRUCTOR
9503 || CONSTANT_CLASS_P (t))
9504 dependent_p = (type_dependent_expression_p (t)
9505 || value_dependent_expression_p (t));
9506 else
9507 {
9508 gcc_assert (t == error_mark_node);
9509 dependent_p = false;
9510 }
9511
9512 processing_template_decl = saved_processing_template_decl;
9513
9514 return dependent_p;
9515 }
9516
9517 /* Returns true iff current_function_decl is an incompletely instantiated
9518 template. Useful instead of processing_template_decl because the latter
9519 is set to 0 during instantiate_non_dependent_expr. */
9520
9521 bool
9522 in_template_function (void)
9523 {
9524 tree fn = current_function_decl;
9525 bool ret;
9526 ++processing_template_decl;
9527 ret = (fn && DECL_LANG_SPECIFIC (fn)
9528 && DECL_TEMPLATE_INFO (fn)
9529 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9530 --processing_template_decl;
9531 return ret;
9532 }
9533
9534 /* Returns true if T depends on any template parameter with level LEVEL. */
9535
9536 bool
9537 uses_template_parms_level (tree t, int level)
9538 {
9539 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9540 /*include_nondeduced_p=*/true);
9541 }
9542
9543 /* Returns true if the signature of DECL depends on any template parameter from
9544 its enclosing class. */
9545
9546 bool
9547 uses_outer_template_parms (tree decl)
9548 {
9549 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9550 if (depth == 0)
9551 return false;
9552 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9553 &depth, NULL, /*include_nondeduced_p=*/true))
9554 return true;
9555 if (PRIMARY_TEMPLATE_P (decl)
9556 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9557 (DECL_TEMPLATE_PARMS (decl)),
9558 template_parm_outer_level,
9559 &depth, NULL, /*include_nondeduced_p=*/true))
9560 return true;
9561 tree ci = get_constraints (decl);
9562 if (ci)
9563 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9564 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9565 &depth, NULL, /*nondeduced*/true))
9566 return true;
9567 return false;
9568 }
9569
9570 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9571 ill-formed translation unit, i.e. a variable or function that isn't
9572 usable in a constant expression. */
9573
9574 static inline bool
9575 neglectable_inst_p (tree d)
9576 {
9577 return (DECL_P (d)
9578 && !undeduced_auto_decl (d)
9579 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9580 : decl_maybe_constant_var_p (d)));
9581 }
9582
9583 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9584 neglectable and instantiated from within an erroneous instantiation. */
9585
9586 static bool
9587 limit_bad_template_recursion (tree decl)
9588 {
9589 struct tinst_level *lev = current_tinst_level;
9590 int errs = errorcount + sorrycount;
9591 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9592 return false;
9593
9594 for (; lev; lev = lev->next)
9595 if (neglectable_inst_p (lev->decl))
9596 break;
9597
9598 return (lev && errs > lev->errors);
9599 }
9600
9601 static int tinst_depth;
9602 extern int max_tinst_depth;
9603 int depth_reached;
9604
9605 static GTY(()) struct tinst_level *last_error_tinst_level;
9606
9607 /* We're starting to instantiate D; record the template instantiation context
9608 for diagnostics and to restore it later. */
9609
9610 bool
9611 push_tinst_level (tree d)
9612 {
9613 return push_tinst_level_loc (d, input_location);
9614 }
9615
9616 /* We're starting to instantiate D; record the template instantiation context
9617 at LOC for diagnostics and to restore it later. */
9618
9619 bool
9620 push_tinst_level_loc (tree d, location_t loc)
9621 {
9622 struct tinst_level *new_level;
9623
9624 if (tinst_depth >= max_tinst_depth)
9625 {
9626 /* Tell error.c not to try to instantiate any templates. */
9627 at_eof = 2;
9628 fatal_error (input_location,
9629 "template instantiation depth exceeds maximum of %d"
9630 " (use -ftemplate-depth= to increase the maximum)",
9631 max_tinst_depth);
9632 return false;
9633 }
9634
9635 /* If the current instantiation caused problems, don't let it instantiate
9636 anything else. Do allow deduction substitution and decls usable in
9637 constant expressions. */
9638 if (limit_bad_template_recursion (d))
9639 return false;
9640
9641 /* When not -quiet, dump template instantiations other than functions, since
9642 announce_function will take care of those. */
9643 if (!quiet_flag
9644 && TREE_CODE (d) != TREE_LIST
9645 && TREE_CODE (d) != FUNCTION_DECL)
9646 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9647
9648 new_level = ggc_alloc<tinst_level> ();
9649 new_level->decl = d;
9650 new_level->locus = loc;
9651 new_level->errors = errorcount+sorrycount;
9652 new_level->in_system_header_p = in_system_header_at (input_location);
9653 new_level->next = current_tinst_level;
9654 current_tinst_level = new_level;
9655
9656 ++tinst_depth;
9657 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9658 depth_reached = tinst_depth;
9659
9660 return true;
9661 }
9662
9663 /* We're done instantiating this template; return to the instantiation
9664 context. */
9665
9666 void
9667 pop_tinst_level (void)
9668 {
9669 /* Restore the filename and line number stashed away when we started
9670 this instantiation. */
9671 input_location = current_tinst_level->locus;
9672 current_tinst_level = current_tinst_level->next;
9673 --tinst_depth;
9674 }
9675
9676 /* We're instantiating a deferred template; restore the template
9677 instantiation context in which the instantiation was requested, which
9678 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9679
9680 static tree
9681 reopen_tinst_level (struct tinst_level *level)
9682 {
9683 struct tinst_level *t;
9684
9685 tinst_depth = 0;
9686 for (t = level; t; t = t->next)
9687 ++tinst_depth;
9688
9689 current_tinst_level = level;
9690 pop_tinst_level ();
9691 if (current_tinst_level)
9692 current_tinst_level->errors = errorcount+sorrycount;
9693 return level->decl;
9694 }
9695
9696 /* Returns the TINST_LEVEL which gives the original instantiation
9697 context. */
9698
9699 struct tinst_level *
9700 outermost_tinst_level (void)
9701 {
9702 struct tinst_level *level = current_tinst_level;
9703 if (level)
9704 while (level->next)
9705 level = level->next;
9706 return level;
9707 }
9708
9709 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9710 vector of template arguments, as for tsubst.
9711
9712 Returns an appropriate tsubst'd friend declaration. */
9713
9714 static tree
9715 tsubst_friend_function (tree decl, tree args)
9716 {
9717 tree new_friend;
9718
9719 if (TREE_CODE (decl) == FUNCTION_DECL
9720 && DECL_TEMPLATE_INSTANTIATION (decl)
9721 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9722 /* This was a friend declared with an explicit template
9723 argument list, e.g.:
9724
9725 friend void f<>(T);
9726
9727 to indicate that f was a template instantiation, not a new
9728 function declaration. Now, we have to figure out what
9729 instantiation of what template. */
9730 {
9731 tree template_id, arglist, fns;
9732 tree new_args;
9733 tree tmpl;
9734 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9735
9736 /* Friend functions are looked up in the containing namespace scope.
9737 We must enter that scope, to avoid finding member functions of the
9738 current class with same name. */
9739 push_nested_namespace (ns);
9740 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9741 tf_warning_or_error, NULL_TREE,
9742 /*integral_constant_expression_p=*/false);
9743 pop_nested_namespace (ns);
9744 arglist = tsubst (DECL_TI_ARGS (decl), args,
9745 tf_warning_or_error, NULL_TREE);
9746 template_id = lookup_template_function (fns, arglist);
9747
9748 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9749 tmpl = determine_specialization (template_id, new_friend,
9750 &new_args,
9751 /*need_member_template=*/0,
9752 TREE_VEC_LENGTH (args),
9753 tsk_none);
9754 return instantiate_template (tmpl, new_args, tf_error);
9755 }
9756
9757 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9758
9759 /* The NEW_FRIEND will look like an instantiation, to the
9760 compiler, but is not an instantiation from the point of view of
9761 the language. For example, we might have had:
9762
9763 template <class T> struct S {
9764 template <class U> friend void f(T, U);
9765 };
9766
9767 Then, in S<int>, template <class U> void f(int, U) is not an
9768 instantiation of anything. */
9769 if (new_friend == error_mark_node)
9770 return error_mark_node;
9771
9772 DECL_USE_TEMPLATE (new_friend) = 0;
9773 if (TREE_CODE (decl) == TEMPLATE_DECL)
9774 {
9775 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9776 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9777 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9778 }
9779
9780 /* The mangled name for the NEW_FRIEND is incorrect. The function
9781 is not a template instantiation and should not be mangled like
9782 one. Therefore, we forget the mangling here; we'll recompute it
9783 later if we need it. */
9784 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9785 {
9786 SET_DECL_RTL (new_friend, NULL);
9787 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9788 }
9789
9790 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9791 {
9792 tree old_decl;
9793 tree new_friend_template_info;
9794 tree new_friend_result_template_info;
9795 tree ns;
9796 int new_friend_is_defn;
9797
9798 /* We must save some information from NEW_FRIEND before calling
9799 duplicate decls since that function will free NEW_FRIEND if
9800 possible. */
9801 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9802 new_friend_is_defn =
9803 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9804 (template_for_substitution (new_friend)))
9805 != NULL_TREE);
9806 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9807 {
9808 /* This declaration is a `primary' template. */
9809 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9810
9811 new_friend_result_template_info
9812 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9813 }
9814 else
9815 new_friend_result_template_info = NULL_TREE;
9816
9817 /* Inside pushdecl_namespace_level, we will push into the
9818 current namespace. However, the friend function should go
9819 into the namespace of the template. */
9820 ns = decl_namespace_context (new_friend);
9821 push_nested_namespace (ns);
9822 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9823 pop_nested_namespace (ns);
9824
9825 if (old_decl == error_mark_node)
9826 return error_mark_node;
9827
9828 if (old_decl != new_friend)
9829 {
9830 /* This new friend declaration matched an existing
9831 declaration. For example, given:
9832
9833 template <class T> void f(T);
9834 template <class U> class C {
9835 template <class T> friend void f(T) {}
9836 };
9837
9838 the friend declaration actually provides the definition
9839 of `f', once C has been instantiated for some type. So,
9840 old_decl will be the out-of-class template declaration,
9841 while new_friend is the in-class definition.
9842
9843 But, if `f' was called before this point, the
9844 instantiation of `f' will have DECL_TI_ARGS corresponding
9845 to `T' but not to `U', references to which might appear
9846 in the definition of `f'. Previously, the most general
9847 template for an instantiation of `f' was the out-of-class
9848 version; now it is the in-class version. Therefore, we
9849 run through all specialization of `f', adding to their
9850 DECL_TI_ARGS appropriately. In particular, they need a
9851 new set of outer arguments, corresponding to the
9852 arguments for this class instantiation.
9853
9854 The same situation can arise with something like this:
9855
9856 friend void f(int);
9857 template <class T> class C {
9858 friend void f(T) {}
9859 };
9860
9861 when `C<int>' is instantiated. Now, `f(int)' is defined
9862 in the class. */
9863
9864 if (!new_friend_is_defn)
9865 /* On the other hand, if the in-class declaration does
9866 *not* provide a definition, then we don't want to alter
9867 existing definitions. We can just leave everything
9868 alone. */
9869 ;
9870 else
9871 {
9872 tree new_template = TI_TEMPLATE (new_friend_template_info);
9873 tree new_args = TI_ARGS (new_friend_template_info);
9874
9875 /* Overwrite whatever template info was there before, if
9876 any, with the new template information pertaining to
9877 the declaration. */
9878 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9879
9880 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9881 {
9882 /* We should have called reregister_specialization in
9883 duplicate_decls. */
9884 gcc_assert (retrieve_specialization (new_template,
9885 new_args, 0)
9886 == old_decl);
9887
9888 /* Instantiate it if the global has already been used. */
9889 if (DECL_ODR_USED (old_decl))
9890 instantiate_decl (old_decl, /*defer_ok=*/true,
9891 /*expl_inst_class_mem_p=*/false);
9892 }
9893 else
9894 {
9895 tree t;
9896
9897 /* Indicate that the old function template is a partial
9898 instantiation. */
9899 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9900 = new_friend_result_template_info;
9901
9902 gcc_assert (new_template
9903 == most_general_template (new_template));
9904 gcc_assert (new_template != old_decl);
9905
9906 /* Reassign any specializations already in the hash table
9907 to the new more general template, and add the
9908 additional template args. */
9909 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9910 t != NULL_TREE;
9911 t = TREE_CHAIN (t))
9912 {
9913 tree spec = TREE_VALUE (t);
9914 spec_entry elt;
9915
9916 elt.tmpl = old_decl;
9917 elt.args = DECL_TI_ARGS (spec);
9918 elt.spec = NULL_TREE;
9919
9920 decl_specializations->remove_elt (&elt);
9921
9922 DECL_TI_ARGS (spec)
9923 = add_outermost_template_args (new_args,
9924 DECL_TI_ARGS (spec));
9925
9926 register_specialization
9927 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9928
9929 }
9930 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9931 }
9932 }
9933
9934 /* The information from NEW_FRIEND has been merged into OLD_DECL
9935 by duplicate_decls. */
9936 new_friend = old_decl;
9937 }
9938 }
9939 else
9940 {
9941 tree context = DECL_CONTEXT (new_friend);
9942 bool dependent_p;
9943
9944 /* In the code
9945 template <class T> class C {
9946 template <class U> friend void C1<U>::f (); // case 1
9947 friend void C2<T>::f (); // case 2
9948 };
9949 we only need to make sure CONTEXT is a complete type for
9950 case 2. To distinguish between the two cases, we note that
9951 CONTEXT of case 1 remains dependent type after tsubst while
9952 this isn't true for case 2. */
9953 ++processing_template_decl;
9954 dependent_p = dependent_type_p (context);
9955 --processing_template_decl;
9956
9957 if (!dependent_p
9958 && !complete_type_or_else (context, NULL_TREE))
9959 return error_mark_node;
9960
9961 if (COMPLETE_TYPE_P (context))
9962 {
9963 tree fn = new_friend;
9964 /* do_friend adds the TEMPLATE_DECL for any member friend
9965 template even if it isn't a member template, i.e.
9966 template <class T> friend A<T>::f();
9967 Look through it in that case. */
9968 if (TREE_CODE (fn) == TEMPLATE_DECL
9969 && !PRIMARY_TEMPLATE_P (fn))
9970 fn = DECL_TEMPLATE_RESULT (fn);
9971 /* Check to see that the declaration is really present, and,
9972 possibly obtain an improved declaration. */
9973 fn = check_classfn (context, fn, NULL_TREE);
9974
9975 if (fn)
9976 new_friend = fn;
9977 }
9978 }
9979
9980 return new_friend;
9981 }
9982
9983 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9984 template arguments, as for tsubst.
9985
9986 Returns an appropriate tsubst'd friend type or error_mark_node on
9987 failure. */
9988
9989 static tree
9990 tsubst_friend_class (tree friend_tmpl, tree args)
9991 {
9992 tree friend_type;
9993 tree tmpl;
9994 tree context;
9995
9996 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9997 {
9998 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9999 return TREE_TYPE (t);
10000 }
10001
10002 context = CP_DECL_CONTEXT (friend_tmpl);
10003
10004 if (context != global_namespace)
10005 {
10006 if (TREE_CODE (context) == NAMESPACE_DECL)
10007 push_nested_namespace (context);
10008 else
10009 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
10010 }
10011
10012 /* Look for a class template declaration. We look for hidden names
10013 because two friend declarations of the same template are the
10014 same. For example, in:
10015
10016 struct A {
10017 template <typename> friend class F;
10018 };
10019 template <typename> struct B {
10020 template <typename> friend class F;
10021 };
10022
10023 both F templates are the same. */
10024 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
10025 /*block_p=*/true, 0, LOOKUP_HIDDEN);
10026
10027 /* But, if we don't find one, it might be because we're in a
10028 situation like this:
10029
10030 template <class T>
10031 struct S {
10032 template <class U>
10033 friend struct S;
10034 };
10035
10036 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
10037 for `S<int>', not the TEMPLATE_DECL. */
10038 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
10039 {
10040 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
10041 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
10042 }
10043
10044 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10045 {
10046 /* The friend template has already been declared. Just
10047 check to see that the declarations match, and install any new
10048 default parameters. We must tsubst the default parameters,
10049 of course. We only need the innermost template parameters
10050 because that is all that redeclare_class_template will look
10051 at. */
10052 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10053 > TMPL_ARGS_DEPTH (args))
10054 {
10055 tree parms;
10056 location_t saved_input_location;
10057 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10058 args, tf_warning_or_error);
10059
10060 saved_input_location = input_location;
10061 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10062 tree cons = get_constraints (tmpl);
10063 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10064 input_location = saved_input_location;
10065
10066 }
10067
10068 friend_type = TREE_TYPE (tmpl);
10069 }
10070 else
10071 {
10072 /* The friend template has not already been declared. In this
10073 case, the instantiation of the template class will cause the
10074 injection of this template into the global scope. */
10075 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10076 if (tmpl == error_mark_node)
10077 return error_mark_node;
10078
10079 /* The new TMPL is not an instantiation of anything, so we
10080 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10081 the new type because that is supposed to be the corresponding
10082 template decl, i.e., TMPL. */
10083 DECL_USE_TEMPLATE (tmpl) = 0;
10084 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10085 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10086 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10087 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10088
10089 /* Inject this template into the global scope. */
10090 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10091 }
10092
10093 if (context != global_namespace)
10094 {
10095 if (TREE_CODE (context) == NAMESPACE_DECL)
10096 pop_nested_namespace (context);
10097 else
10098 pop_nested_class ();
10099 }
10100
10101 return friend_type;
10102 }
10103
10104 /* Returns zero if TYPE cannot be completed later due to circularity.
10105 Otherwise returns one. */
10106
10107 static int
10108 can_complete_type_without_circularity (tree type)
10109 {
10110 if (type == NULL_TREE || type == error_mark_node)
10111 return 0;
10112 else if (COMPLETE_TYPE_P (type))
10113 return 1;
10114 else if (TREE_CODE (type) == ARRAY_TYPE)
10115 return can_complete_type_without_circularity (TREE_TYPE (type));
10116 else if (CLASS_TYPE_P (type)
10117 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10118 return 0;
10119 else
10120 return 1;
10121 }
10122
10123 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10124 tsubst_flags_t, tree);
10125
10126 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10127 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10128
10129 static tree
10130 tsubst_attribute (tree t, tree *decl_p, tree args,
10131 tsubst_flags_t complain, tree in_decl)
10132 {
10133 gcc_assert (ATTR_IS_DEPENDENT (t));
10134
10135 tree val = TREE_VALUE (t);
10136 if (val == NULL_TREE)
10137 /* Nothing to do. */;
10138 else if ((flag_openmp || flag_openmp_simd)
10139 && is_attribute_p ("omp declare simd",
10140 get_attribute_name (t)))
10141 {
10142 tree clauses = TREE_VALUE (val);
10143 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10144 complain, in_decl);
10145 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10146 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10147 tree parms = DECL_ARGUMENTS (*decl_p);
10148 clauses
10149 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10150 if (clauses)
10151 val = build_tree_list (NULL_TREE, clauses);
10152 else
10153 val = NULL_TREE;
10154 }
10155 /* If the first attribute argument is an identifier, don't
10156 pass it through tsubst. Attributes like mode, format,
10157 cleanup and several target specific attributes expect it
10158 unmodified. */
10159 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10160 {
10161 tree chain
10162 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10163 /*integral_constant_expression_p=*/false);
10164 if (chain != TREE_CHAIN (val))
10165 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10166 }
10167 else if (PACK_EXPANSION_P (val))
10168 {
10169 /* An attribute pack expansion. */
10170 tree purp = TREE_PURPOSE (t);
10171 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10172 if (pack == error_mark_node)
10173 return error_mark_node;
10174 int len = TREE_VEC_LENGTH (pack);
10175 tree list = NULL_TREE;
10176 tree *q = &list;
10177 for (int i = 0; i < len; ++i)
10178 {
10179 tree elt = TREE_VEC_ELT (pack, i);
10180 *q = build_tree_list (purp, elt);
10181 q = &TREE_CHAIN (*q);
10182 }
10183 return list;
10184 }
10185 else
10186 val = tsubst_expr (val, args, complain, in_decl,
10187 /*integral_constant_expression_p=*/false);
10188
10189 if (val != TREE_VALUE (t))
10190 return build_tree_list (TREE_PURPOSE (t), val);
10191 return t;
10192 }
10193
10194 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10195 unchanged or a new TREE_LIST chain. */
10196
10197 static tree
10198 tsubst_attributes (tree attributes, tree args,
10199 tsubst_flags_t complain, tree in_decl)
10200 {
10201 tree last_dep = NULL_TREE;
10202
10203 for (tree t = attributes; t; t = TREE_CHAIN (t))
10204 if (ATTR_IS_DEPENDENT (t))
10205 {
10206 last_dep = t;
10207 attributes = copy_list (attributes);
10208 break;
10209 }
10210
10211 if (last_dep)
10212 for (tree *p = &attributes; *p; )
10213 {
10214 tree t = *p;
10215 if (ATTR_IS_DEPENDENT (t))
10216 {
10217 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10218 if (subst != t)
10219 {
10220 *p = subst;
10221 do
10222 p = &TREE_CHAIN (*p);
10223 while (*p);
10224 *p = TREE_CHAIN (t);
10225 continue;
10226 }
10227 }
10228 p = &TREE_CHAIN (*p);
10229 }
10230
10231 return attributes;
10232 }
10233
10234 /* Apply any attributes which had to be deferred until instantiation
10235 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10236 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10237
10238 static void
10239 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10240 tree args, tsubst_flags_t complain, tree in_decl)
10241 {
10242 tree last_dep = NULL_TREE;
10243 tree t;
10244 tree *p;
10245
10246 if (attributes == NULL_TREE)
10247 return;
10248
10249 if (DECL_P (*decl_p))
10250 {
10251 if (TREE_TYPE (*decl_p) == error_mark_node)
10252 return;
10253 p = &DECL_ATTRIBUTES (*decl_p);
10254 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10255 to our attributes parameter. */
10256 gcc_assert (*p == attributes);
10257 }
10258 else
10259 {
10260 p = &TYPE_ATTRIBUTES (*decl_p);
10261 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10262 lookup_template_class_1, and should be preserved. */
10263 gcc_assert (*p != attributes);
10264 while (*p)
10265 p = &TREE_CHAIN (*p);
10266 }
10267
10268 for (t = attributes; t; t = TREE_CHAIN (t))
10269 if (ATTR_IS_DEPENDENT (t))
10270 {
10271 last_dep = t;
10272 attributes = copy_list (attributes);
10273 break;
10274 }
10275
10276 *p = attributes;
10277 if (last_dep)
10278 {
10279 tree late_attrs = NULL_TREE;
10280 tree *q = &late_attrs;
10281
10282 for (; *p; )
10283 {
10284 t = *p;
10285 if (ATTR_IS_DEPENDENT (t))
10286 {
10287 *p = TREE_CHAIN (t);
10288 TREE_CHAIN (t) = NULL_TREE;
10289 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10290 do
10291 q = &TREE_CHAIN (*q);
10292 while (*q);
10293 }
10294 else
10295 p = &TREE_CHAIN (t);
10296 }
10297
10298 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10299 }
10300 }
10301
10302 /* Perform (or defer) access check for typedefs that were referenced
10303 from within the template TMPL code.
10304 This is a subroutine of instantiate_decl and instantiate_class_template.
10305 TMPL is the template to consider and TARGS is the list of arguments of
10306 that template. */
10307
10308 static void
10309 perform_typedefs_access_check (tree tmpl, tree targs)
10310 {
10311 location_t saved_location;
10312 unsigned i;
10313 qualified_typedef_usage_t *iter;
10314
10315 if (!tmpl
10316 || (!CLASS_TYPE_P (tmpl)
10317 && TREE_CODE (tmpl) != FUNCTION_DECL))
10318 return;
10319
10320 saved_location = input_location;
10321 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10322 {
10323 tree type_decl = iter->typedef_decl;
10324 tree type_scope = iter->context;
10325
10326 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10327 continue;
10328
10329 if (uses_template_parms (type_decl))
10330 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10331 if (uses_template_parms (type_scope))
10332 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10333
10334 /* Make access check error messages point to the location
10335 of the use of the typedef. */
10336 input_location = iter->locus;
10337 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10338 type_decl, type_decl,
10339 tf_warning_or_error);
10340 }
10341 input_location = saved_location;
10342 }
10343
10344 static tree
10345 instantiate_class_template_1 (tree type)
10346 {
10347 tree templ, args, pattern, t, member;
10348 tree typedecl;
10349 tree pbinfo;
10350 tree base_list;
10351 unsigned int saved_maximum_field_alignment;
10352 tree fn_context;
10353
10354 if (type == error_mark_node)
10355 return error_mark_node;
10356
10357 if (COMPLETE_OR_OPEN_TYPE_P (type)
10358 || uses_template_parms (type))
10359 return type;
10360
10361 /* Figure out which template is being instantiated. */
10362 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10363 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10364
10365 /* Determine what specialization of the original template to
10366 instantiate. */
10367 t = most_specialized_partial_spec (type, tf_warning_or_error);
10368 if (t == error_mark_node)
10369 {
10370 TYPE_BEING_DEFINED (type) = 1;
10371 return error_mark_node;
10372 }
10373 else if (t)
10374 {
10375 /* This TYPE is actually an instantiation of a partial
10376 specialization. We replace the innermost set of ARGS with
10377 the arguments appropriate for substitution. For example,
10378 given:
10379
10380 template <class T> struct S {};
10381 template <class T> struct S<T*> {};
10382
10383 and supposing that we are instantiating S<int*>, ARGS will
10384 presently be {int*} -- but we need {int}. */
10385 pattern = TREE_TYPE (t);
10386 args = TREE_PURPOSE (t);
10387 }
10388 else
10389 {
10390 pattern = TREE_TYPE (templ);
10391 args = CLASSTYPE_TI_ARGS (type);
10392 }
10393
10394 /* If the template we're instantiating is incomplete, then clearly
10395 there's nothing we can do. */
10396 if (!COMPLETE_TYPE_P (pattern))
10397 return type;
10398
10399 /* If we've recursively instantiated too many templates, stop. */
10400 if (! push_tinst_level (type))
10401 return type;
10402
10403 /* Now we're really doing the instantiation. Mark the type as in
10404 the process of being defined. */
10405 TYPE_BEING_DEFINED (type) = 1;
10406
10407 /* We may be in the middle of deferred access check. Disable
10408 it now. */
10409 push_deferring_access_checks (dk_no_deferred);
10410
10411 int saved_unevaluated_operand = cp_unevaluated_operand;
10412 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10413
10414 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10415 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10416 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10417 fn_context = error_mark_node;
10418 if (!fn_context)
10419 push_to_top_level ();
10420 else
10421 {
10422 cp_unevaluated_operand = 0;
10423 c_inhibit_evaluation_warnings = 0;
10424 }
10425 /* Use #pragma pack from the template context. */
10426 saved_maximum_field_alignment = maximum_field_alignment;
10427 maximum_field_alignment = TYPE_PRECISION (pattern);
10428
10429 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10430
10431 /* Set the input location to the most specialized template definition.
10432 This is needed if tsubsting causes an error. */
10433 typedecl = TYPE_MAIN_DECL (pattern);
10434 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10435 DECL_SOURCE_LOCATION (typedecl);
10436
10437 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10438 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10439 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10440 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10441 if (ANON_AGGR_TYPE_P (pattern))
10442 SET_ANON_AGGR_TYPE_P (type);
10443 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10444 {
10445 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10446 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10447 /* Adjust visibility for template arguments. */
10448 determine_visibility (TYPE_MAIN_DECL (type));
10449 }
10450 if (CLASS_TYPE_P (type))
10451 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10452
10453 pbinfo = TYPE_BINFO (pattern);
10454
10455 /* We should never instantiate a nested class before its enclosing
10456 class; we need to look up the nested class by name before we can
10457 instantiate it, and that lookup should instantiate the enclosing
10458 class. */
10459 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10460 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10461
10462 base_list = NULL_TREE;
10463 if (BINFO_N_BASE_BINFOS (pbinfo))
10464 {
10465 tree pbase_binfo;
10466 tree pushed_scope;
10467 int i;
10468
10469 /* We must enter the scope containing the type, as that is where
10470 the accessibility of types named in dependent bases are
10471 looked up from. */
10472 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10473
10474 /* Substitute into each of the bases to determine the actual
10475 basetypes. */
10476 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10477 {
10478 tree base;
10479 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10480 tree expanded_bases = NULL_TREE;
10481 int idx, len = 1;
10482
10483 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10484 {
10485 expanded_bases =
10486 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10487 args, tf_error, NULL_TREE);
10488 if (expanded_bases == error_mark_node)
10489 continue;
10490
10491 len = TREE_VEC_LENGTH (expanded_bases);
10492 }
10493
10494 for (idx = 0; idx < len; idx++)
10495 {
10496 if (expanded_bases)
10497 /* Extract the already-expanded base class. */
10498 base = TREE_VEC_ELT (expanded_bases, idx);
10499 else
10500 /* Substitute to figure out the base class. */
10501 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10502 NULL_TREE);
10503
10504 if (base == error_mark_node)
10505 continue;
10506
10507 base_list = tree_cons (access, base, base_list);
10508 if (BINFO_VIRTUAL_P (pbase_binfo))
10509 TREE_TYPE (base_list) = integer_type_node;
10510 }
10511 }
10512
10513 /* The list is now in reverse order; correct that. */
10514 base_list = nreverse (base_list);
10515
10516 if (pushed_scope)
10517 pop_scope (pushed_scope);
10518 }
10519 /* Now call xref_basetypes to set up all the base-class
10520 information. */
10521 xref_basetypes (type, base_list);
10522
10523 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10524 (int) ATTR_FLAG_TYPE_IN_PLACE,
10525 args, tf_error, NULL_TREE);
10526 fixup_attribute_variants (type);
10527
10528 /* Now that our base classes are set up, enter the scope of the
10529 class, so that name lookups into base classes, etc. will work
10530 correctly. This is precisely analogous to what we do in
10531 begin_class_definition when defining an ordinary non-template
10532 class, except we also need to push the enclosing classes. */
10533 push_nested_class (type);
10534
10535 /* Now members are processed in the order of declaration. */
10536 for (member = CLASSTYPE_DECL_LIST (pattern);
10537 member; member = TREE_CHAIN (member))
10538 {
10539 tree t = TREE_VALUE (member);
10540
10541 if (TREE_PURPOSE (member))
10542 {
10543 if (TYPE_P (t))
10544 {
10545 /* Build new CLASSTYPE_NESTED_UTDS. */
10546
10547 tree newtag;
10548 bool class_template_p;
10549
10550 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10551 && TYPE_LANG_SPECIFIC (t)
10552 && CLASSTYPE_IS_TEMPLATE (t));
10553 /* If the member is a class template, then -- even after
10554 substitution -- there may be dependent types in the
10555 template argument list for the class. We increment
10556 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10557 that function will assume that no types are dependent
10558 when outside of a template. */
10559 if (class_template_p)
10560 ++processing_template_decl;
10561 newtag = tsubst (t, args, tf_error, NULL_TREE);
10562 if (class_template_p)
10563 --processing_template_decl;
10564 if (newtag == error_mark_node)
10565 continue;
10566
10567 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10568 {
10569 tree name = TYPE_IDENTIFIER (t);
10570
10571 if (class_template_p)
10572 /* Unfortunately, lookup_template_class sets
10573 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10574 instantiation (i.e., for the type of a member
10575 template class nested within a template class.)
10576 This behavior is required for
10577 maybe_process_partial_specialization to work
10578 correctly, but is not accurate in this case;
10579 the TAG is not an instantiation of anything.
10580 (The corresponding TEMPLATE_DECL is an
10581 instantiation, but the TYPE is not.) */
10582 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10583
10584 /* Now, we call pushtag to put this NEWTAG into the scope of
10585 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10586 pushtag calling push_template_decl. We don't have to do
10587 this for enums because it will already have been done in
10588 tsubst_enum. */
10589 if (name)
10590 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10591 pushtag (name, newtag, /*tag_scope=*/ts_current);
10592 }
10593 }
10594 else if (DECL_DECLARES_FUNCTION_P (t))
10595 {
10596 tree r;
10597
10598 if (TREE_CODE (t) == TEMPLATE_DECL)
10599 ++processing_template_decl;
10600 r = tsubst (t, args, tf_error, NULL_TREE);
10601 if (TREE_CODE (t) == TEMPLATE_DECL)
10602 --processing_template_decl;
10603 set_current_access_from_decl (r);
10604 finish_member_declaration (r);
10605 /* Instantiate members marked with attribute used. */
10606 if (r != error_mark_node && DECL_PRESERVE_P (r))
10607 mark_used (r);
10608 if (TREE_CODE (r) == FUNCTION_DECL
10609 && DECL_OMP_DECLARE_REDUCTION_P (r))
10610 cp_check_omp_declare_reduction (r);
10611 }
10612 else if (DECL_CLASS_TEMPLATE_P (t)
10613 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10614 /* A closure type for a lambda in a default argument for a
10615 member template. Ignore it; it will be instantiated with
10616 the default argument. */;
10617 else
10618 {
10619 /* Build new TYPE_FIELDS. */
10620 if (TREE_CODE (t) == STATIC_ASSERT)
10621 {
10622 tree condition;
10623
10624 ++c_inhibit_evaluation_warnings;
10625 condition =
10626 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10627 tf_warning_or_error, NULL_TREE,
10628 /*integral_constant_expression_p=*/true);
10629 --c_inhibit_evaluation_warnings;
10630
10631 finish_static_assert (condition,
10632 STATIC_ASSERT_MESSAGE (t),
10633 STATIC_ASSERT_SOURCE_LOCATION (t),
10634 /*member_p=*/true);
10635 }
10636 else if (TREE_CODE (t) != CONST_DECL)
10637 {
10638 tree r;
10639 tree vec = NULL_TREE;
10640 int len = 1;
10641
10642 /* The file and line for this declaration, to
10643 assist in error message reporting. Since we
10644 called push_tinst_level above, we don't need to
10645 restore these. */
10646 input_location = DECL_SOURCE_LOCATION (t);
10647
10648 if (TREE_CODE (t) == TEMPLATE_DECL)
10649 ++processing_template_decl;
10650 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10651 if (TREE_CODE (t) == TEMPLATE_DECL)
10652 --processing_template_decl;
10653
10654 if (TREE_CODE (r) == TREE_VEC)
10655 {
10656 /* A capture pack became multiple fields. */
10657 vec = r;
10658 len = TREE_VEC_LENGTH (vec);
10659 }
10660
10661 for (int i = 0; i < len; ++i)
10662 {
10663 if (vec)
10664 r = TREE_VEC_ELT (vec, i);
10665 if (VAR_P (r))
10666 {
10667 /* In [temp.inst]:
10668
10669 [t]he initialization (and any associated
10670 side-effects) of a static data member does
10671 not occur unless the static data member is
10672 itself used in a way that requires the
10673 definition of the static data member to
10674 exist.
10675
10676 Therefore, we do not substitute into the
10677 initialized for the static data member here. */
10678 finish_static_data_member_decl
10679 (r,
10680 /*init=*/NULL_TREE,
10681 /*init_const_expr_p=*/false,
10682 /*asmspec_tree=*/NULL_TREE,
10683 /*flags=*/0);
10684 /* Instantiate members marked with attribute used. */
10685 if (r != error_mark_node && DECL_PRESERVE_P (r))
10686 mark_used (r);
10687 }
10688 else if (TREE_CODE (r) == FIELD_DECL)
10689 {
10690 /* Determine whether R has a valid type and can be
10691 completed later. If R is invalid, then its type
10692 is replaced by error_mark_node. */
10693 tree rtype = TREE_TYPE (r);
10694 if (can_complete_type_without_circularity (rtype))
10695 complete_type (rtype);
10696
10697 if (!complete_or_array_type_p (rtype))
10698 {
10699 /* If R's type couldn't be completed and
10700 it isn't a flexible array member (whose
10701 type is incomplete by definition) give
10702 an error. */
10703 cxx_incomplete_type_error (r, rtype);
10704 TREE_TYPE (r) = error_mark_node;
10705 }
10706 }
10707
10708 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10709 such a thing will already have been added to the field
10710 list by tsubst_enum in finish_member_declaration in the
10711 CLASSTYPE_NESTED_UTDS case above. */
10712 if (!(TREE_CODE (r) == TYPE_DECL
10713 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10714 && DECL_ARTIFICIAL (r)))
10715 {
10716 set_current_access_from_decl (r);
10717 finish_member_declaration (r);
10718 }
10719 }
10720 }
10721 }
10722 }
10723 else
10724 {
10725 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10726 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10727 {
10728 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10729
10730 tree friend_type = t;
10731 bool adjust_processing_template_decl = false;
10732
10733 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10734 {
10735 /* template <class T> friend class C; */
10736 friend_type = tsubst_friend_class (friend_type, args);
10737 adjust_processing_template_decl = true;
10738 }
10739 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10740 {
10741 /* template <class T> friend class C::D; */
10742 friend_type = tsubst (friend_type, args,
10743 tf_warning_or_error, NULL_TREE);
10744 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10745 friend_type = TREE_TYPE (friend_type);
10746 adjust_processing_template_decl = true;
10747 }
10748 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10749 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10750 {
10751 /* This could be either
10752
10753 friend class T::C;
10754
10755 when dependent_type_p is false or
10756
10757 template <class U> friend class T::C;
10758
10759 otherwise. */
10760 /* Bump processing_template_decl in case this is something like
10761 template <class T> friend struct A<T>::B. */
10762 ++processing_template_decl;
10763 friend_type = tsubst (friend_type, args,
10764 tf_warning_or_error, NULL_TREE);
10765 if (dependent_type_p (friend_type))
10766 adjust_processing_template_decl = true;
10767 --processing_template_decl;
10768 }
10769 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10770 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10771 && TYPE_HIDDEN_P (friend_type))
10772 {
10773 /* friend class C;
10774
10775 where C hasn't been declared yet. Let's lookup name
10776 from namespace scope directly, bypassing any name that
10777 come from dependent base class. */
10778 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10779
10780 /* The call to xref_tag_from_type does injection for friend
10781 classes. */
10782 push_nested_namespace (ns);
10783 friend_type =
10784 xref_tag_from_type (friend_type, NULL_TREE,
10785 /*tag_scope=*/ts_current);
10786 pop_nested_namespace (ns);
10787 }
10788 else if (uses_template_parms (friend_type))
10789 /* friend class C<T>; */
10790 friend_type = tsubst (friend_type, args,
10791 tf_warning_or_error, NULL_TREE);
10792 /* Otherwise it's
10793
10794 friend class C;
10795
10796 where C is already declared or
10797
10798 friend class C<int>;
10799
10800 We don't have to do anything in these cases. */
10801
10802 if (adjust_processing_template_decl)
10803 /* Trick make_friend_class into realizing that the friend
10804 we're adding is a template, not an ordinary class. It's
10805 important that we use make_friend_class since it will
10806 perform some error-checking and output cross-reference
10807 information. */
10808 ++processing_template_decl;
10809
10810 if (friend_type != error_mark_node)
10811 make_friend_class (type, friend_type, /*complain=*/false);
10812
10813 if (adjust_processing_template_decl)
10814 --processing_template_decl;
10815 }
10816 else
10817 {
10818 /* Build new DECL_FRIENDLIST. */
10819 tree r;
10820
10821 /* The file and line for this declaration, to
10822 assist in error message reporting. Since we
10823 called push_tinst_level above, we don't need to
10824 restore these. */
10825 input_location = DECL_SOURCE_LOCATION (t);
10826
10827 if (TREE_CODE (t) == TEMPLATE_DECL)
10828 {
10829 ++processing_template_decl;
10830 push_deferring_access_checks (dk_no_check);
10831 }
10832
10833 r = tsubst_friend_function (t, args);
10834 add_friend (type, r, /*complain=*/false);
10835 if (TREE_CODE (t) == TEMPLATE_DECL)
10836 {
10837 pop_deferring_access_checks ();
10838 --processing_template_decl;
10839 }
10840 }
10841 }
10842 }
10843
10844 if (fn_context)
10845 {
10846 /* Restore these before substituting into the lambda capture
10847 initializers. */
10848 cp_unevaluated_operand = saved_unevaluated_operand;
10849 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10850 }
10851
10852 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10853 {
10854 tree decl = lambda_function (type);
10855 if (decl)
10856 {
10857 if (cxx_dialect >= cxx17)
10858 CLASSTYPE_LITERAL_P (type) = true;
10859
10860 if (!DECL_TEMPLATE_INFO (decl)
10861 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10862 {
10863 /* Set function_depth to avoid garbage collection. */
10864 ++function_depth;
10865 instantiate_decl (decl, /*defer_ok=*/false, false);
10866 --function_depth;
10867 }
10868
10869 /* We need to instantiate the capture list from the template
10870 after we've instantiated the closure members, but before we
10871 consider adding the conversion op. Also keep any captures
10872 that may have been added during instantiation of the op(). */
10873 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10874 tree tmpl_cap
10875 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10876 args, tf_warning_or_error, NULL_TREE,
10877 false, false);
10878
10879 LAMBDA_EXPR_CAPTURE_LIST (expr)
10880 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10881
10882 maybe_add_lambda_conv_op (type);
10883 }
10884 else
10885 gcc_assert (errorcount);
10886 }
10887
10888 /* Set the file and line number information to whatever is given for
10889 the class itself. This puts error messages involving generated
10890 implicit functions at a predictable point, and the same point
10891 that would be used for non-template classes. */
10892 input_location = DECL_SOURCE_LOCATION (typedecl);
10893
10894 unreverse_member_declarations (type);
10895 finish_struct_1 (type);
10896 TYPE_BEING_DEFINED (type) = 0;
10897
10898 /* We don't instantiate default arguments for member functions. 14.7.1:
10899
10900 The implicit instantiation of a class template specialization causes
10901 the implicit instantiation of the declarations, but not of the
10902 definitions or default arguments, of the class member functions,
10903 member classes, static data members and member templates.... */
10904
10905 /* Some typedefs referenced from within the template code need to be access
10906 checked at template instantiation time, i.e now. These types were
10907 added to the template at parsing time. Let's get those and perform
10908 the access checks then. */
10909 perform_typedefs_access_check (pattern, args);
10910 perform_deferred_access_checks (tf_warning_or_error);
10911 pop_nested_class ();
10912 maximum_field_alignment = saved_maximum_field_alignment;
10913 if (!fn_context)
10914 pop_from_top_level ();
10915 pop_deferring_access_checks ();
10916 pop_tinst_level ();
10917
10918 /* The vtable for a template class can be emitted in any translation
10919 unit in which the class is instantiated. When there is no key
10920 method, however, finish_struct_1 will already have added TYPE to
10921 the keyed_classes. */
10922 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10923 vec_safe_push (keyed_classes, type);
10924
10925 return type;
10926 }
10927
10928 /* Wrapper for instantiate_class_template_1. */
10929
10930 tree
10931 instantiate_class_template (tree type)
10932 {
10933 tree ret;
10934 timevar_push (TV_TEMPLATE_INST);
10935 ret = instantiate_class_template_1 (type);
10936 timevar_pop (TV_TEMPLATE_INST);
10937 return ret;
10938 }
10939
10940 static tree
10941 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10942 {
10943 tree r;
10944
10945 if (!t)
10946 r = t;
10947 else if (TYPE_P (t))
10948 r = tsubst (t, args, complain, in_decl);
10949 else
10950 {
10951 if (!(complain & tf_warning))
10952 ++c_inhibit_evaluation_warnings;
10953 r = tsubst_expr (t, args, complain, in_decl,
10954 /*integral_constant_expression_p=*/true);
10955 if (!(complain & tf_warning))
10956 --c_inhibit_evaluation_warnings;
10957 }
10958 return r;
10959 }
10960
10961 /* Given a function parameter pack TMPL_PARM and some function parameters
10962 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10963 and set *SPEC_P to point at the next point in the list. */
10964
10965 tree
10966 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10967 {
10968 /* Collect all of the extra "packed" parameters into an
10969 argument pack. */
10970 tree parmvec;
10971 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10972 tree spec_parm = *spec_p;
10973 int i, len;
10974
10975 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10976 if (tmpl_parm
10977 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10978 break;
10979
10980 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10981 parmvec = make_tree_vec (len);
10982 spec_parm = *spec_p;
10983 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10984 TREE_VEC_ELT (parmvec, i) = spec_parm;
10985
10986 /* Build the argument packs. */
10987 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10988 *spec_p = spec_parm;
10989
10990 return argpack;
10991 }
10992
10993 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10994 NONTYPE_ARGUMENT_PACK. */
10995
10996 static tree
10997 make_fnparm_pack (tree spec_parm)
10998 {
10999 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11000 }
11001
11002 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11003 pack expansion with no extra args, 2 if it has extra args, or 0
11004 if it is not a pack expansion. */
11005
11006 static int
11007 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11008 {
11009 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11010 if (i >= TREE_VEC_LENGTH (vec))
11011 return 0;
11012 tree elt = TREE_VEC_ELT (vec, i);
11013 if (DECL_P (elt))
11014 /* A decl pack is itself an expansion. */
11015 elt = TREE_TYPE (elt);
11016 if (!PACK_EXPANSION_P (elt))
11017 return 0;
11018 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11019 return 2;
11020 return 1;
11021 }
11022
11023
11024 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11025
11026 static tree
11027 make_argument_pack_select (tree arg_pack, unsigned index)
11028 {
11029 tree aps = make_node (ARGUMENT_PACK_SELECT);
11030
11031 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11032 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11033
11034 return aps;
11035 }
11036
11037 /* This is a subroutine of tsubst_pack_expansion.
11038
11039 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11040 mechanism to store the (non complete list of) arguments of the
11041 substitution and return a non substituted pack expansion, in order
11042 to wait for when we have enough arguments to really perform the
11043 substitution. */
11044
11045 static bool
11046 use_pack_expansion_extra_args_p (tree parm_packs,
11047 int arg_pack_len,
11048 bool has_empty_arg)
11049 {
11050 /* If one pack has an expansion and another pack has a normal
11051 argument or if one pack has an empty argument and an another
11052 one hasn't then tsubst_pack_expansion cannot perform the
11053 substitution and need to fall back on the
11054 PACK_EXPANSION_EXTRA mechanism. */
11055 if (parm_packs == NULL_TREE)
11056 return false;
11057 else if (has_empty_arg)
11058 return true;
11059
11060 bool has_expansion_arg = false;
11061 for (int i = 0 ; i < arg_pack_len; ++i)
11062 {
11063 bool has_non_expansion_arg = false;
11064 for (tree parm_pack = parm_packs;
11065 parm_pack;
11066 parm_pack = TREE_CHAIN (parm_pack))
11067 {
11068 tree arg = TREE_VALUE (parm_pack);
11069
11070 int exp = argument_pack_element_is_expansion_p (arg, i);
11071 if (exp == 2)
11072 /* We can't substitute a pack expansion with extra args into
11073 our pattern. */
11074 return true;
11075 else if (exp)
11076 has_expansion_arg = true;
11077 else
11078 has_non_expansion_arg = true;
11079 }
11080
11081 if (has_expansion_arg && has_non_expansion_arg)
11082 return true;
11083 }
11084 return false;
11085 }
11086
11087 /* [temp.variadic]/6 says that:
11088
11089 The instantiation of a pack expansion [...]
11090 produces a list E1,E2, ..., En, where N is the number of elements
11091 in the pack expansion parameters.
11092
11093 This subroutine of tsubst_pack_expansion produces one of these Ei.
11094
11095 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11096 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11097 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11098 INDEX is the index 'i' of the element Ei to produce. ARGS,
11099 COMPLAIN, and IN_DECL are the same parameters as for the
11100 tsubst_pack_expansion function.
11101
11102 The function returns the resulting Ei upon successful completion,
11103 or error_mark_node.
11104
11105 Note that this function possibly modifies the ARGS parameter, so
11106 it's the responsibility of the caller to restore it. */
11107
11108 static tree
11109 gen_elem_of_pack_expansion_instantiation (tree pattern,
11110 tree parm_packs,
11111 unsigned index,
11112 tree args /* This parm gets
11113 modified. */,
11114 tsubst_flags_t complain,
11115 tree in_decl)
11116 {
11117 tree t;
11118 bool ith_elem_is_expansion = false;
11119
11120 /* For each parameter pack, change the substitution of the parameter
11121 pack to the ith argument in its argument pack, then expand the
11122 pattern. */
11123 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11124 {
11125 tree parm = TREE_PURPOSE (pack);
11126 tree arg_pack = TREE_VALUE (pack);
11127 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11128
11129 ith_elem_is_expansion |=
11130 argument_pack_element_is_expansion_p (arg_pack, index);
11131
11132 /* Select the Ith argument from the pack. */
11133 if (TREE_CODE (parm) == PARM_DECL
11134 || TREE_CODE (parm) == FIELD_DECL)
11135 {
11136 if (index == 0)
11137 {
11138 aps = make_argument_pack_select (arg_pack, index);
11139 if (!mark_used (parm, complain) && !(complain & tf_error))
11140 return error_mark_node;
11141 register_local_specialization (aps, parm);
11142 }
11143 else
11144 aps = retrieve_local_specialization (parm);
11145 }
11146 else
11147 {
11148 int idx, level;
11149 template_parm_level_and_index (parm, &level, &idx);
11150
11151 if (index == 0)
11152 {
11153 aps = make_argument_pack_select (arg_pack, index);
11154 /* Update the corresponding argument. */
11155 TMPL_ARG (args, level, idx) = aps;
11156 }
11157 else
11158 /* Re-use the ARGUMENT_PACK_SELECT. */
11159 aps = TMPL_ARG (args, level, idx);
11160 }
11161 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11162 }
11163
11164 /* Substitute into the PATTERN with the (possibly altered)
11165 arguments. */
11166 if (pattern == in_decl)
11167 /* Expanding a fixed parameter pack from
11168 coerce_template_parameter_pack. */
11169 t = tsubst_decl (pattern, args, complain);
11170 else if (pattern == error_mark_node)
11171 t = error_mark_node;
11172 else if (constraint_p (pattern))
11173 {
11174 if (processing_template_decl)
11175 t = tsubst_constraint (pattern, args, complain, in_decl);
11176 else
11177 t = (constraints_satisfied_p (pattern, args)
11178 ? boolean_true_node : boolean_false_node);
11179 }
11180 else if (!TYPE_P (pattern))
11181 t = tsubst_expr (pattern, args, complain, in_decl,
11182 /*integral_constant_expression_p=*/false);
11183 else
11184 t = tsubst (pattern, args, complain, in_decl);
11185
11186 /* If the Ith argument pack element is a pack expansion, then
11187 the Ith element resulting from the substituting is going to
11188 be a pack expansion as well. */
11189 if (ith_elem_is_expansion)
11190 t = make_pack_expansion (t, complain);
11191
11192 return t;
11193 }
11194
11195 /* When the unexpanded parameter pack in a fold expression expands to an empty
11196 sequence, the value of the expression is as follows; the program is
11197 ill-formed if the operator is not listed in this table.
11198
11199 && true
11200 || false
11201 , void() */
11202
11203 tree
11204 expand_empty_fold (tree t, tsubst_flags_t complain)
11205 {
11206 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11207 if (!FOLD_EXPR_MODIFY_P (t))
11208 switch (code)
11209 {
11210 case TRUTH_ANDIF_EXPR:
11211 return boolean_true_node;
11212 case TRUTH_ORIF_EXPR:
11213 return boolean_false_node;
11214 case COMPOUND_EXPR:
11215 return void_node;
11216 default:
11217 break;
11218 }
11219
11220 if (complain & tf_error)
11221 error_at (location_of (t),
11222 "fold of empty expansion over %O", code);
11223 return error_mark_node;
11224 }
11225
11226 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11227 form an expression that combines the two terms using the
11228 operator of T. */
11229
11230 static tree
11231 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11232 {
11233 tree op = FOLD_EXPR_OP (t);
11234 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11235
11236 // Handle compound assignment operators.
11237 if (FOLD_EXPR_MODIFY_P (t))
11238 return build_x_modify_expr (input_location, left, code, right, complain);
11239
11240 switch (code)
11241 {
11242 case COMPOUND_EXPR:
11243 return build_x_compound_expr (input_location, left, right, complain);
11244 case DOTSTAR_EXPR:
11245 return build_m_component_ref (left, right, complain);
11246 default:
11247 return build_x_binary_op (input_location, code,
11248 left, TREE_CODE (left),
11249 right, TREE_CODE (right),
11250 /*overload=*/NULL,
11251 complain);
11252 }
11253 }
11254
11255 /* Substitute ARGS into the pack of a fold expression T. */
11256
11257 static inline tree
11258 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11259 {
11260 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11261 }
11262
11263 /* Substitute ARGS into the pack of a fold expression T. */
11264
11265 static inline tree
11266 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11267 {
11268 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11269 }
11270
11271 /* Expand a PACK of arguments into a grouped as left fold.
11272 Given a pack containing elements A0, A1, ..., An and an
11273 operator @, this builds the expression:
11274
11275 ((A0 @ A1) @ A2) ... @ An
11276
11277 Note that PACK must not be empty.
11278
11279 The operator is defined by the original fold expression T. */
11280
11281 static tree
11282 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11283 {
11284 tree left = TREE_VEC_ELT (pack, 0);
11285 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11286 {
11287 tree right = TREE_VEC_ELT (pack, i);
11288 left = fold_expression (t, left, right, complain);
11289 }
11290 return left;
11291 }
11292
11293 /* Substitute into a unary left fold expression. */
11294
11295 static tree
11296 tsubst_unary_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 if (PACK_EXPANSION_P (pack))
11303 {
11304 tree r = copy_node (t);
11305 FOLD_EXPR_PACK (r) = pack;
11306 return r;
11307 }
11308 if (TREE_VEC_LENGTH (pack) == 0)
11309 return expand_empty_fold (t, complain);
11310 else
11311 return expand_left_fold (t, pack, complain);
11312 }
11313
11314 /* Substitute into a binary left fold expression.
11315
11316 Do ths by building a single (non-empty) vector of argumnts and
11317 building the expression from those elements. */
11318
11319 static tree
11320 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11321 tree in_decl)
11322 {
11323 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11324 if (pack == error_mark_node)
11325 return error_mark_node;
11326 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11327 if (init == error_mark_node)
11328 return error_mark_node;
11329
11330 if (PACK_EXPANSION_P (pack))
11331 {
11332 tree r = copy_node (t);
11333 FOLD_EXPR_PACK (r) = pack;
11334 FOLD_EXPR_INIT (r) = init;
11335 return r;
11336 }
11337
11338 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11339 TREE_VEC_ELT (vec, 0) = init;
11340 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11341 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11342
11343 return expand_left_fold (t, vec, complain);
11344 }
11345
11346 /* Expand a PACK of arguments into a grouped as right fold.
11347 Given a pack containing elementns A0, A1, ..., and an
11348 operator @, this builds the expression:
11349
11350 A0@ ... (An-2 @ (An-1 @ An))
11351
11352 Note that PACK must not be empty.
11353
11354 The operator is defined by the original fold expression T. */
11355
11356 tree
11357 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11358 {
11359 // Build the expression.
11360 int n = TREE_VEC_LENGTH (pack);
11361 tree right = TREE_VEC_ELT (pack, n - 1);
11362 for (--n; n != 0; --n)
11363 {
11364 tree left = TREE_VEC_ELT (pack, n - 1);
11365 right = fold_expression (t, left, right, complain);
11366 }
11367 return right;
11368 }
11369
11370 /* Substitute into a unary right fold expression. */
11371
11372 static tree
11373 tsubst_unary_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 if (PACK_EXPANSION_P (pack))
11380 {
11381 tree r = copy_node (t);
11382 FOLD_EXPR_PACK (r) = pack;
11383 return r;
11384 }
11385 if (TREE_VEC_LENGTH (pack) == 0)
11386 return expand_empty_fold (t, complain);
11387 else
11388 return expand_right_fold (t, pack, complain);
11389 }
11390
11391 /* Substitute into a binary right fold expression.
11392
11393 Do ths by building a single (non-empty) vector of arguments and
11394 building the expression from those elements. */
11395
11396 static tree
11397 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11398 tree in_decl)
11399 {
11400 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11401 if (pack == error_mark_node)
11402 return error_mark_node;
11403 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11404 if (init == error_mark_node)
11405 return error_mark_node;
11406
11407 if (PACK_EXPANSION_P (pack))
11408 {
11409 tree r = copy_node (t);
11410 FOLD_EXPR_PACK (r) = pack;
11411 FOLD_EXPR_INIT (r) = init;
11412 return r;
11413 }
11414
11415 int n = TREE_VEC_LENGTH (pack);
11416 tree vec = make_tree_vec (n + 1);
11417 for (int i = 0; i < n; ++i)
11418 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11419 TREE_VEC_ELT (vec, n) = init;
11420
11421 return expand_right_fold (t, vec, complain);
11422 }
11423
11424
11425 /* Substitute ARGS into T, which is an pack expansion
11426 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11427 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11428 (if only a partial substitution could be performed) or
11429 ERROR_MARK_NODE if there was an error. */
11430 tree
11431 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11432 tree in_decl)
11433 {
11434 tree pattern;
11435 tree pack, packs = NULL_TREE;
11436 bool unsubstituted_packs = false;
11437 int i, len = -1;
11438 tree result;
11439 hash_map<tree, tree> *saved_local_specializations = NULL;
11440 bool need_local_specializations = false;
11441 int levels;
11442
11443 gcc_assert (PACK_EXPANSION_P (t));
11444 pattern = PACK_EXPANSION_PATTERN (t);
11445
11446 /* Add in any args remembered from an earlier partial instantiation. */
11447 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11448
11449 levels = TMPL_ARGS_DEPTH (args);
11450
11451 /* Determine the argument packs that will instantiate the parameter
11452 packs used in the expansion expression. While we're at it,
11453 compute the number of arguments to be expanded and make sure it
11454 is consistent. */
11455 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11456 pack = TREE_CHAIN (pack))
11457 {
11458 tree parm_pack = TREE_VALUE (pack);
11459 tree arg_pack = NULL_TREE;
11460 tree orig_arg = NULL_TREE;
11461 int level = 0;
11462
11463 if (TREE_CODE (parm_pack) == BASES)
11464 {
11465 gcc_assert (parm_pack == pattern);
11466 if (BASES_DIRECT (parm_pack))
11467 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11468 args, complain, in_decl, false));
11469 else
11470 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11471 args, complain, in_decl, false));
11472 }
11473 else if (builtin_pack_call_p (parm_pack))
11474 {
11475 /* ??? Support use in other patterns. */
11476 gcc_assert (parm_pack == pattern);
11477 return expand_builtin_pack_call (parm_pack, args,
11478 complain, in_decl);
11479 }
11480 else if (TREE_CODE (parm_pack) == PARM_DECL)
11481 {
11482 /* We know we have correct local_specializations if this
11483 expansion is at function scope, or if we're dealing with a
11484 local parameter in a requires expression; for the latter,
11485 tsubst_requires_expr set it up appropriately. */
11486 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11487 arg_pack = retrieve_local_specialization (parm_pack);
11488 else
11489 /* We can't rely on local_specializations for a parameter
11490 name used later in a function declaration (such as in a
11491 late-specified return type). Even if it exists, it might
11492 have the wrong value for a recursive call. */
11493 need_local_specializations = true;
11494
11495 if (!arg_pack)
11496 {
11497 /* This parameter pack was used in an unevaluated context. Just
11498 make a dummy decl, since it's only used for its type. */
11499 arg_pack = tsubst_decl (parm_pack, args, complain);
11500 if (arg_pack && DECL_PACK_P (arg_pack))
11501 /* Partial instantiation of the parm_pack, we can't build
11502 up an argument pack yet. */
11503 arg_pack = NULL_TREE;
11504 else
11505 arg_pack = make_fnparm_pack (arg_pack);
11506 }
11507 }
11508 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11509 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11510 else
11511 {
11512 int idx;
11513 template_parm_level_and_index (parm_pack, &level, &idx);
11514
11515 if (level <= levels)
11516 arg_pack = TMPL_ARG (args, level, idx);
11517 }
11518
11519 orig_arg = arg_pack;
11520 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11521 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11522
11523 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11524 /* This can only happen if we forget to expand an argument
11525 pack somewhere else. Just return an error, silently. */
11526 {
11527 result = make_tree_vec (1);
11528 TREE_VEC_ELT (result, 0) = error_mark_node;
11529 return result;
11530 }
11531
11532 if (arg_pack)
11533 {
11534 int my_len =
11535 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11536
11537 /* Don't bother trying to do a partial substitution with
11538 incomplete packs; we'll try again after deduction. */
11539 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11540 return t;
11541
11542 if (len < 0)
11543 len = my_len;
11544 else if (len != my_len)
11545 {
11546 if (!(complain & tf_error))
11547 /* Fail quietly. */;
11548 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11549 error ("mismatched argument pack lengths while expanding %qT",
11550 pattern);
11551 else
11552 error ("mismatched argument pack lengths while expanding %qE",
11553 pattern);
11554 return error_mark_node;
11555 }
11556
11557 /* Keep track of the parameter packs and their corresponding
11558 argument packs. */
11559 packs = tree_cons (parm_pack, arg_pack, packs);
11560 TREE_TYPE (packs) = orig_arg;
11561 }
11562 else
11563 {
11564 /* We can't substitute for this parameter pack. We use a flag as
11565 well as the missing_level counter because function parameter
11566 packs don't have a level. */
11567 gcc_assert (processing_template_decl);
11568 unsubstituted_packs = true;
11569 }
11570 }
11571
11572 /* If the expansion is just T..., return the matching argument pack, unless
11573 we need to call convert_from_reference on all the elements. This is an
11574 important optimization; see c++/68422. */
11575 if (!unsubstituted_packs
11576 && TREE_PURPOSE (packs) == pattern)
11577 {
11578 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11579 /* Types need no adjustment, nor does sizeof..., and if we still have
11580 some pack expansion args we won't do anything yet. */
11581 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11582 || PACK_EXPANSION_SIZEOF_P (t)
11583 || pack_expansion_args_count (args))
11584 return args;
11585 /* Also optimize expression pack expansions if we can tell that the
11586 elements won't have reference type. */
11587 tree type = TREE_TYPE (pattern);
11588 if (type && TREE_CODE (type) != REFERENCE_TYPE
11589 && !PACK_EXPANSION_P (type)
11590 && !WILDCARD_TYPE_P (type))
11591 return args;
11592 /* Otherwise use the normal path so we get convert_from_reference. */
11593 }
11594
11595 /* We cannot expand this expansion expression, because we don't have
11596 all of the argument packs we need. */
11597 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11598 {
11599 /* We got some full packs, but we can't substitute them in until we
11600 have values for all the packs. So remember these until then. */
11601
11602 t = make_pack_expansion (pattern, complain);
11603 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11604 return t;
11605 }
11606 else if (unsubstituted_packs)
11607 {
11608 /* There were no real arguments, we're just replacing a parameter
11609 pack with another version of itself. Substitute into the
11610 pattern and return a PACK_EXPANSION_*. The caller will need to
11611 deal with that. */
11612 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11613 t = tsubst_expr (pattern, args, complain, in_decl,
11614 /*integral_constant_expression_p=*/false);
11615 else
11616 t = tsubst (pattern, args, complain, in_decl);
11617 t = make_pack_expansion (t, complain);
11618 return t;
11619 }
11620
11621 gcc_assert (len >= 0);
11622
11623 if (need_local_specializations)
11624 {
11625 /* We're in a late-specified return type, so create our own local
11626 specializations map; the current map is either NULL or (in the
11627 case of recursive unification) might have bindings that we don't
11628 want to use or alter. */
11629 saved_local_specializations = local_specializations;
11630 local_specializations = new hash_map<tree, tree>;
11631 }
11632
11633 /* For each argument in each argument pack, substitute into the
11634 pattern. */
11635 result = make_tree_vec (len);
11636 tree elem_args = copy_template_args (args);
11637 for (i = 0; i < len; ++i)
11638 {
11639 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11640 i,
11641 elem_args, complain,
11642 in_decl);
11643 TREE_VEC_ELT (result, i) = t;
11644 if (t == error_mark_node)
11645 {
11646 result = error_mark_node;
11647 break;
11648 }
11649 }
11650
11651 /* Update ARGS to restore the substitution from parameter packs to
11652 their argument packs. */
11653 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11654 {
11655 tree parm = TREE_PURPOSE (pack);
11656
11657 if (TREE_CODE (parm) == PARM_DECL
11658 || TREE_CODE (parm) == FIELD_DECL)
11659 register_local_specialization (TREE_TYPE (pack), parm);
11660 else
11661 {
11662 int idx, level;
11663
11664 if (TREE_VALUE (pack) == NULL_TREE)
11665 continue;
11666
11667 template_parm_level_and_index (parm, &level, &idx);
11668
11669 /* Update the corresponding argument. */
11670 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11671 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11672 TREE_TYPE (pack);
11673 else
11674 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11675 }
11676 }
11677
11678 if (need_local_specializations)
11679 {
11680 delete local_specializations;
11681 local_specializations = saved_local_specializations;
11682 }
11683
11684 /* If the dependent pack arguments were such that we end up with only a
11685 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11686 if (len == 1 && TREE_CODE (result) == TREE_VEC
11687 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11688 return TREE_VEC_ELT (result, 0);
11689
11690 return result;
11691 }
11692
11693 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11694 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11695 parameter packs; all parms generated from a function parameter pack will
11696 have the same DECL_PARM_INDEX. */
11697
11698 tree
11699 get_pattern_parm (tree parm, tree tmpl)
11700 {
11701 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11702 tree patparm;
11703
11704 if (DECL_ARTIFICIAL (parm))
11705 {
11706 for (patparm = DECL_ARGUMENTS (pattern);
11707 patparm; patparm = DECL_CHAIN (patparm))
11708 if (DECL_ARTIFICIAL (patparm)
11709 && DECL_NAME (parm) == DECL_NAME (patparm))
11710 break;
11711 }
11712 else
11713 {
11714 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11715 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11716 gcc_assert (DECL_PARM_INDEX (patparm)
11717 == DECL_PARM_INDEX (parm));
11718 }
11719
11720 return patparm;
11721 }
11722
11723 /* Make an argument pack out of the TREE_VEC VEC. */
11724
11725 static tree
11726 make_argument_pack (tree vec)
11727 {
11728 tree pack;
11729 tree elt = TREE_VEC_ELT (vec, 0);
11730 if (TYPE_P (elt))
11731 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11732 else
11733 {
11734 pack = make_node (NONTYPE_ARGUMENT_PACK);
11735 TREE_CONSTANT (pack) = 1;
11736 }
11737 SET_ARGUMENT_PACK_ARGS (pack, vec);
11738 return pack;
11739 }
11740
11741 /* Return an exact copy of template args T that can be modified
11742 independently. */
11743
11744 static tree
11745 copy_template_args (tree t)
11746 {
11747 if (t == error_mark_node)
11748 return t;
11749
11750 int len = TREE_VEC_LENGTH (t);
11751 tree new_vec = make_tree_vec (len);
11752
11753 for (int i = 0; i < len; ++i)
11754 {
11755 tree elt = TREE_VEC_ELT (t, i);
11756 if (elt && TREE_CODE (elt) == TREE_VEC)
11757 elt = copy_template_args (elt);
11758 TREE_VEC_ELT (new_vec, i) = elt;
11759 }
11760
11761 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11762 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11763
11764 return new_vec;
11765 }
11766
11767 /* Substitute ARGS into the vector or list of template arguments T. */
11768
11769 static tree
11770 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11771 {
11772 tree orig_t = t;
11773 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11774 tree *elts;
11775
11776 if (t == error_mark_node)
11777 return error_mark_node;
11778
11779 len = TREE_VEC_LENGTH (t);
11780 elts = XALLOCAVEC (tree, len);
11781
11782 for (i = 0; i < len; i++)
11783 {
11784 tree orig_arg = TREE_VEC_ELT (t, i);
11785 tree new_arg;
11786
11787 if (TREE_CODE (orig_arg) == TREE_VEC)
11788 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11789 else if (PACK_EXPANSION_P (orig_arg))
11790 {
11791 /* Substitute into an expansion expression. */
11792 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11793
11794 if (TREE_CODE (new_arg) == TREE_VEC)
11795 /* Add to the expanded length adjustment the number of
11796 expanded arguments. We subtract one from this
11797 measurement, because the argument pack expression
11798 itself is already counted as 1 in
11799 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11800 the argument pack is empty. */
11801 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11802 }
11803 else if (ARGUMENT_PACK_P (orig_arg))
11804 {
11805 /* Substitute into each of the arguments. */
11806 new_arg = TYPE_P (orig_arg)
11807 ? cxx_make_type (TREE_CODE (orig_arg))
11808 : make_node (TREE_CODE (orig_arg));
11809
11810 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11811 args, complain, in_decl);
11812 if (pack_args == error_mark_node)
11813 new_arg = error_mark_node;
11814 else
11815 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11816
11817 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11818 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11819 }
11820 else
11821 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11822
11823 if (new_arg == error_mark_node)
11824 return error_mark_node;
11825
11826 elts[i] = new_arg;
11827 if (new_arg != orig_arg)
11828 need_new = 1;
11829 }
11830
11831 if (!need_new)
11832 return t;
11833
11834 /* Make space for the expanded arguments coming from template
11835 argument packs. */
11836 t = make_tree_vec (len + expanded_len_adjust);
11837 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11838 arguments for a member template.
11839 In that case each TREE_VEC in ORIG_T represents a level of template
11840 arguments, and ORIG_T won't carry any non defaulted argument count.
11841 It will rather be the nested TREE_VECs that will carry one.
11842 In other words, ORIG_T carries a non defaulted argument count only
11843 if it doesn't contain any nested TREE_VEC. */
11844 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11845 {
11846 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11847 count += expanded_len_adjust;
11848 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11849 }
11850 for (i = 0, out = 0; i < len; i++)
11851 {
11852 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11853 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11854 && TREE_CODE (elts[i]) == TREE_VEC)
11855 {
11856 int idx;
11857
11858 /* Now expand the template argument pack "in place". */
11859 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11860 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11861 }
11862 else
11863 {
11864 TREE_VEC_ELT (t, out) = elts[i];
11865 out++;
11866 }
11867 }
11868
11869 return t;
11870 }
11871
11872 /* Substitute ARGS into one level PARMS of template parameters. */
11873
11874 static tree
11875 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11876 {
11877 if (parms == error_mark_node)
11878 return error_mark_node;
11879
11880 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11881
11882 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11883 {
11884 tree tuple = TREE_VEC_ELT (parms, i);
11885
11886 if (tuple == error_mark_node)
11887 continue;
11888
11889 TREE_VEC_ELT (new_vec, i) =
11890 tsubst_template_parm (tuple, args, complain);
11891 }
11892
11893 return new_vec;
11894 }
11895
11896 /* Return the result of substituting ARGS into the template parameters
11897 given by PARMS. If there are m levels of ARGS and m + n levels of
11898 PARMS, then the result will contain n levels of PARMS. For
11899 example, if PARMS is `template <class T> template <class U>
11900 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11901 result will be `template <int*, double, class V>'. */
11902
11903 static tree
11904 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11905 {
11906 tree r = NULL_TREE;
11907 tree* new_parms;
11908
11909 /* When substituting into a template, we must set
11910 PROCESSING_TEMPLATE_DECL as the template parameters may be
11911 dependent if they are based on one-another, and the dependency
11912 predicates are short-circuit outside of templates. */
11913 ++processing_template_decl;
11914
11915 for (new_parms = &r;
11916 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11917 new_parms = &(TREE_CHAIN (*new_parms)),
11918 parms = TREE_CHAIN (parms))
11919 {
11920 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11921 args, complain);
11922 *new_parms =
11923 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11924 - TMPL_ARGS_DEPTH (args)),
11925 new_vec, NULL_TREE);
11926 }
11927
11928 --processing_template_decl;
11929
11930 return r;
11931 }
11932
11933 /* Return the result of substituting ARGS into one template parameter
11934 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11935 parameter and which TREE_PURPOSE is the default argument of the
11936 template parameter. */
11937
11938 static tree
11939 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11940 {
11941 tree default_value, parm_decl;
11942
11943 if (args == NULL_TREE
11944 || t == NULL_TREE
11945 || t == error_mark_node)
11946 return t;
11947
11948 gcc_assert (TREE_CODE (t) == TREE_LIST);
11949
11950 default_value = TREE_PURPOSE (t);
11951 parm_decl = TREE_VALUE (t);
11952
11953 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11954 if (TREE_CODE (parm_decl) == PARM_DECL
11955 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11956 parm_decl = error_mark_node;
11957 default_value = tsubst_template_arg (default_value, args,
11958 complain, NULL_TREE);
11959
11960 return build_tree_list (default_value, parm_decl);
11961 }
11962
11963 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11964 type T. If T is not an aggregate or enumeration type, it is
11965 handled as if by tsubst. IN_DECL is as for tsubst. If
11966 ENTERING_SCOPE is nonzero, T is the context for a template which
11967 we are presently tsubst'ing. Return the substituted value. */
11968
11969 static tree
11970 tsubst_aggr_type (tree t,
11971 tree args,
11972 tsubst_flags_t complain,
11973 tree in_decl,
11974 int entering_scope)
11975 {
11976 if (t == NULL_TREE)
11977 return NULL_TREE;
11978
11979 switch (TREE_CODE (t))
11980 {
11981 case RECORD_TYPE:
11982 if (TYPE_PTRMEMFUNC_P (t))
11983 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11984
11985 /* Fall through. */
11986 case ENUMERAL_TYPE:
11987 case UNION_TYPE:
11988 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11989 {
11990 tree argvec;
11991 tree context;
11992 tree r;
11993 int saved_unevaluated_operand;
11994 int saved_inhibit_evaluation_warnings;
11995
11996 /* In "sizeof(X<I>)" we need to evaluate "I". */
11997 saved_unevaluated_operand = cp_unevaluated_operand;
11998 cp_unevaluated_operand = 0;
11999 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12000 c_inhibit_evaluation_warnings = 0;
12001
12002 /* First, determine the context for the type we are looking
12003 up. */
12004 context = TYPE_CONTEXT (t);
12005 if (context && TYPE_P (context))
12006 {
12007 context = tsubst_aggr_type (context, args, complain,
12008 in_decl, /*entering_scope=*/1);
12009 /* If context is a nested class inside a class template,
12010 it may still need to be instantiated (c++/33959). */
12011 context = complete_type (context);
12012 }
12013
12014 /* Then, figure out what arguments are appropriate for the
12015 type we are trying to find. For example, given:
12016
12017 template <class T> struct S;
12018 template <class T, class U> void f(T, U) { S<U> su; }
12019
12020 and supposing that we are instantiating f<int, double>,
12021 then our ARGS will be {int, double}, but, when looking up
12022 S we only want {double}. */
12023 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12024 complain, in_decl);
12025 if (argvec == error_mark_node)
12026 r = error_mark_node;
12027 else
12028 {
12029 r = lookup_template_class (t, argvec, in_decl, context,
12030 entering_scope, complain);
12031 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12032 }
12033
12034 cp_unevaluated_operand = saved_unevaluated_operand;
12035 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12036
12037 return r;
12038 }
12039 else
12040 /* This is not a template type, so there's nothing to do. */
12041 return t;
12042
12043 default:
12044 return tsubst (t, args, complain, in_decl);
12045 }
12046 }
12047
12048 static GTY((cache)) tree_cache_map *defarg_inst;
12049
12050 /* Substitute into the default argument ARG (a default argument for
12051 FN), which has the indicated TYPE. */
12052
12053 tree
12054 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12055 tsubst_flags_t complain)
12056 {
12057 tree saved_class_ptr = NULL_TREE;
12058 tree saved_class_ref = NULL_TREE;
12059 int errs = errorcount + sorrycount;
12060
12061 /* This can happen in invalid code. */
12062 if (TREE_CODE (arg) == DEFAULT_ARG)
12063 return arg;
12064
12065 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12066 parm = chain_index (parmnum, parm);
12067 tree parmtype = TREE_TYPE (parm);
12068 if (DECL_BY_REFERENCE (parm))
12069 parmtype = TREE_TYPE (parmtype);
12070 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12071
12072 tree *slot;
12073 if (defarg_inst && (slot = defarg_inst->get (parm)))
12074 return *slot;
12075
12076 /* This default argument came from a template. Instantiate the
12077 default argument here, not in tsubst. In the case of
12078 something like:
12079
12080 template <class T>
12081 struct S {
12082 static T t();
12083 void f(T = t());
12084 };
12085
12086 we must be careful to do name lookup in the scope of S<T>,
12087 rather than in the current class. */
12088 push_access_scope (fn);
12089 /* The "this" pointer is not valid in a default argument. */
12090 if (cfun)
12091 {
12092 saved_class_ptr = current_class_ptr;
12093 cp_function_chain->x_current_class_ptr = NULL_TREE;
12094 saved_class_ref = current_class_ref;
12095 cp_function_chain->x_current_class_ref = NULL_TREE;
12096 }
12097
12098 start_lambda_scope (parm);
12099
12100 push_deferring_access_checks(dk_no_deferred);
12101 /* The default argument expression may cause implicitly defined
12102 member functions to be synthesized, which will result in garbage
12103 collection. We must treat this situation as if we were within
12104 the body of function so as to avoid collecting live data on the
12105 stack. */
12106 ++function_depth;
12107 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12108 complain, NULL_TREE,
12109 /*integral_constant_expression_p=*/false);
12110 --function_depth;
12111 pop_deferring_access_checks();
12112
12113 finish_lambda_scope ();
12114
12115 /* Restore the "this" pointer. */
12116 if (cfun)
12117 {
12118 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12119 cp_function_chain->x_current_class_ref = saved_class_ref;
12120 }
12121
12122 if (errorcount+sorrycount > errs
12123 && (complain & tf_warning_or_error))
12124 inform (input_location,
12125 " when instantiating default argument for call to %qD", fn);
12126
12127 /* Make sure the default argument is reasonable. */
12128 arg = check_default_argument (type, arg, complain);
12129
12130 pop_access_scope (fn);
12131
12132 if (arg != error_mark_node && !cp_unevaluated_operand)
12133 {
12134 if (!defarg_inst)
12135 defarg_inst = tree_cache_map::create_ggc (37);
12136 defarg_inst->put (parm, arg);
12137 }
12138
12139 return arg;
12140 }
12141
12142 /* Substitute into all the default arguments for FN. */
12143
12144 static void
12145 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12146 {
12147 tree arg;
12148 tree tmpl_args;
12149
12150 tmpl_args = DECL_TI_ARGS (fn);
12151
12152 /* If this function is not yet instantiated, we certainly don't need
12153 its default arguments. */
12154 if (uses_template_parms (tmpl_args))
12155 return;
12156 /* Don't do this again for clones. */
12157 if (DECL_CLONED_FUNCTION_P (fn))
12158 return;
12159
12160 int i = 0;
12161 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12162 arg;
12163 arg = TREE_CHAIN (arg), ++i)
12164 if (TREE_PURPOSE (arg))
12165 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12166 TREE_VALUE (arg),
12167 TREE_PURPOSE (arg),
12168 complain);
12169 }
12170
12171 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12172
12173 static tree
12174 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12175 tree lambda_fntype)
12176 {
12177 tree gen_tmpl, argvec;
12178 hashval_t hash = 0;
12179 tree in_decl = t;
12180
12181 /* Nobody should be tsubst'ing into non-template functions. */
12182 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12183
12184 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12185 {
12186 /* If T is not dependent, just return it. */
12187 if (!uses_template_parms (DECL_TI_ARGS (t)))
12188 return t;
12189
12190 /* Calculate the most general template of which R is a
12191 specialization, and the complete set of arguments used to
12192 specialize R. */
12193 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12194 argvec = tsubst_template_args (DECL_TI_ARGS
12195 (DECL_TEMPLATE_RESULT
12196 (DECL_TI_TEMPLATE (t))),
12197 args, complain, in_decl);
12198 if (argvec == error_mark_node)
12199 return error_mark_node;
12200
12201 /* Check to see if we already have this specialization. */
12202 if (!lambda_fntype)
12203 {
12204 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12205 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12206 return spec;
12207 }
12208
12209 /* We can see more levels of arguments than parameters if
12210 there was a specialization of a member template, like
12211 this:
12212
12213 template <class T> struct S { template <class U> void f(); }
12214 template <> template <class U> void S<int>::f(U);
12215
12216 Here, we'll be substituting into the specialization,
12217 because that's where we can find the code we actually
12218 want to generate, but we'll have enough arguments for
12219 the most general template.
12220
12221 We also deal with the peculiar case:
12222
12223 template <class T> struct S {
12224 template <class U> friend void f();
12225 };
12226 template <class U> void f() {}
12227 template S<int>;
12228 template void f<double>();
12229
12230 Here, the ARGS for the instantiation of will be {int,
12231 double}. But, we only need as many ARGS as there are
12232 levels of template parameters in CODE_PATTERN. We are
12233 careful not to get fooled into reducing the ARGS in
12234 situations like:
12235
12236 template <class T> struct S { template <class U> void f(U); }
12237 template <class T> template <> void S<T>::f(int) {}
12238
12239 which we can spot because the pattern will be a
12240 specialization in this case. */
12241 int args_depth = TMPL_ARGS_DEPTH (args);
12242 int parms_depth =
12243 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12244
12245 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12246 args = get_innermost_template_args (args, parms_depth);
12247 }
12248 else
12249 {
12250 /* This special case arises when we have something like this:
12251
12252 template <class T> struct S {
12253 friend void f<int>(int, double);
12254 };
12255
12256 Here, the DECL_TI_TEMPLATE for the friend declaration
12257 will be an IDENTIFIER_NODE. We are being called from
12258 tsubst_friend_function, and we want only to create a
12259 new decl (R) with appropriate types so that we can call
12260 determine_specialization. */
12261 gen_tmpl = NULL_TREE;
12262 argvec = NULL_TREE;
12263 }
12264
12265 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12266 : NULL_TREE);
12267 tree ctx = closure ? closure : DECL_CONTEXT (t);
12268 bool member = ctx && TYPE_P (ctx);
12269
12270 if (member && !closure)
12271 ctx = tsubst_aggr_type (ctx, args,
12272 complain, t, /*entering_scope=*/1);
12273
12274 tree type = (lambda_fntype ? lambda_fntype
12275 : tsubst (TREE_TYPE (t), args,
12276 complain | tf_fndecl_type, in_decl));
12277 if (type == error_mark_node)
12278 return error_mark_node;
12279
12280 /* If we hit excessive deduction depth, the type is bogus even if
12281 it isn't error_mark_node, so don't build a decl. */
12282 if (excessive_deduction_depth)
12283 return error_mark_node;
12284
12285 /* We do NOT check for matching decls pushed separately at this
12286 point, as they may not represent instantiations of this
12287 template, and in any case are considered separate under the
12288 discrete model. */
12289 tree r = copy_decl (t);
12290 DECL_USE_TEMPLATE (r) = 0;
12291 TREE_TYPE (r) = type;
12292 /* Clear out the mangled name and RTL for the instantiation. */
12293 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12294 SET_DECL_RTL (r, NULL);
12295 /* Leave DECL_INITIAL set on deleted instantiations. */
12296 if (!DECL_DELETED_FN (r))
12297 DECL_INITIAL (r) = NULL_TREE;
12298 DECL_CONTEXT (r) = ctx;
12299
12300 /* OpenMP UDRs have the only argument a reference to the declared
12301 type. We want to diagnose if the declared type is a reference,
12302 which is invalid, but as references to references are usually
12303 quietly merged, diagnose it here. */
12304 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12305 {
12306 tree argtype
12307 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12308 argtype = tsubst (argtype, args, complain, in_decl);
12309 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12310 error_at (DECL_SOURCE_LOCATION (t),
12311 "reference type %qT in "
12312 "%<#pragma omp declare reduction%>", argtype);
12313 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12314 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12315 argtype);
12316 }
12317
12318 if (member && DECL_CONV_FN_P (r))
12319 /* Type-conversion operator. Reconstruct the name, in
12320 case it's the name of one of the template's parameters. */
12321 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12322
12323 tree parms = DECL_ARGUMENTS (t);
12324 if (closure)
12325 parms = DECL_CHAIN (parms);
12326 parms = tsubst (parms, args, complain, t);
12327 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12328 DECL_CONTEXT (parm) = r;
12329 if (closure)
12330 {
12331 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12332 DECL_CHAIN (tparm) = parms;
12333 parms = tparm;
12334 }
12335 DECL_ARGUMENTS (r) = parms;
12336 DECL_RESULT (r) = NULL_TREE;
12337
12338 TREE_STATIC (r) = 0;
12339 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12340 DECL_EXTERNAL (r) = 1;
12341 /* If this is an instantiation of a function with internal
12342 linkage, we already know what object file linkage will be
12343 assigned to the instantiation. */
12344 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12345 DECL_DEFER_OUTPUT (r) = 0;
12346 DECL_CHAIN (r) = NULL_TREE;
12347 DECL_PENDING_INLINE_INFO (r) = 0;
12348 DECL_PENDING_INLINE_P (r) = 0;
12349 DECL_SAVED_TREE (r) = NULL_TREE;
12350 DECL_STRUCT_FUNCTION (r) = NULL;
12351 TREE_USED (r) = 0;
12352 /* We'll re-clone as appropriate in instantiate_template. */
12353 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12354
12355 /* If we aren't complaining now, return on error before we register
12356 the specialization so that we'll complain eventually. */
12357 if ((complain & tf_error) == 0
12358 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12359 && !grok_op_properties (r, /*complain=*/false))
12360 return error_mark_node;
12361
12362 /* When instantiating a constrained member, substitute
12363 into the constraints to create a new constraint. */
12364 if (tree ci = get_constraints (t))
12365 if (member)
12366 {
12367 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12368 set_constraints (r, ci);
12369 }
12370
12371 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12372 this in the special friend case mentioned above where
12373 GEN_TMPL is NULL. */
12374 if (gen_tmpl && !closure)
12375 {
12376 DECL_TEMPLATE_INFO (r)
12377 = build_template_info (gen_tmpl, argvec);
12378 SET_DECL_IMPLICIT_INSTANTIATION (r);
12379
12380 tree new_r
12381 = register_specialization (r, gen_tmpl, argvec, false, hash);
12382 if (new_r != r)
12383 /* We instantiated this while substituting into
12384 the type earlier (template/friend54.C). */
12385 return new_r;
12386
12387 /* We're not supposed to instantiate default arguments
12388 until they are called, for a template. But, for a
12389 declaration like:
12390
12391 template <class T> void f ()
12392 { extern void g(int i = T()); }
12393
12394 we should do the substitution when the template is
12395 instantiated. We handle the member function case in
12396 instantiate_class_template since the default arguments
12397 might refer to other members of the class. */
12398 if (!member
12399 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12400 && !uses_template_parms (argvec))
12401 tsubst_default_arguments (r, complain);
12402 }
12403 else
12404 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12405
12406 /* Copy the list of befriending classes. */
12407 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12408 *friends;
12409 friends = &TREE_CHAIN (*friends))
12410 {
12411 *friends = copy_node (*friends);
12412 TREE_VALUE (*friends)
12413 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12414 }
12415
12416 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12417 {
12418 maybe_retrofit_in_chrg (r);
12419 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12420 return error_mark_node;
12421 /* If this is an instantiation of a member template, clone it.
12422 If it isn't, that'll be handled by
12423 clone_constructors_and_destructors. */
12424 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12425 clone_function_decl (r, /*update_methods=*/false);
12426 }
12427 else if ((complain & tf_error) != 0
12428 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12429 && !grok_op_properties (r, /*complain=*/true))
12430 return error_mark_node;
12431
12432 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12433 SET_DECL_FRIEND_CONTEXT (r,
12434 tsubst (DECL_FRIEND_CONTEXT (t),
12435 args, complain, in_decl));
12436
12437 /* Possibly limit visibility based on template args. */
12438 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12439 if (DECL_VISIBILITY_SPECIFIED (t))
12440 {
12441 DECL_VISIBILITY_SPECIFIED (r) = 0;
12442 DECL_ATTRIBUTES (r)
12443 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12444 }
12445 determine_visibility (r);
12446 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12447 && !processing_template_decl)
12448 defaulted_late_check (r);
12449
12450 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12451 args, complain, in_decl);
12452 return r;
12453 }
12454
12455 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12456
12457 static tree
12458 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12459 tree lambda_fntype)
12460 {
12461 /* We can get here when processing a member function template,
12462 member class template, or template template parameter. */
12463 tree decl = DECL_TEMPLATE_RESULT (t);
12464 tree in_decl = t;
12465 tree spec;
12466 tree tmpl_args;
12467 tree full_args;
12468 tree r;
12469 hashval_t hash = 0;
12470
12471 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12472 {
12473 /* Template template parameter is treated here. */
12474 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12475 if (new_type == error_mark_node)
12476 r = error_mark_node;
12477 /* If we get a real template back, return it. This can happen in
12478 the context of most_specialized_partial_spec. */
12479 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12480 r = new_type;
12481 else
12482 /* The new TEMPLATE_DECL was built in
12483 reduce_template_parm_level. */
12484 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12485 return r;
12486 }
12487
12488 if (!lambda_fntype)
12489 {
12490 /* We might already have an instance of this template.
12491 The ARGS are for the surrounding class type, so the
12492 full args contain the tsubst'd args for the context,
12493 plus the innermost args from the template decl. */
12494 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12495 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12496 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12497 /* Because this is a template, the arguments will still be
12498 dependent, even after substitution. If
12499 PROCESSING_TEMPLATE_DECL is not set, the dependency
12500 predicates will short-circuit. */
12501 ++processing_template_decl;
12502 full_args = tsubst_template_args (tmpl_args, args,
12503 complain, in_decl);
12504 --processing_template_decl;
12505 if (full_args == error_mark_node)
12506 return error_mark_node;
12507
12508 /* If this is a default template template argument,
12509 tsubst might not have changed anything. */
12510 if (full_args == tmpl_args)
12511 return t;
12512
12513 hash = hash_tmpl_and_args (t, full_args);
12514 spec = retrieve_specialization (t, full_args, hash);
12515 if (spec != NULL_TREE)
12516 return spec;
12517 }
12518
12519 /* Make a new template decl. It will be similar to the
12520 original, but will record the current template arguments.
12521 We also create a new function declaration, which is just
12522 like the old one, but points to this new template, rather
12523 than the old one. */
12524 r = copy_decl (t);
12525 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12526 DECL_CHAIN (r) = NULL_TREE;
12527
12528 // Build new template info linking to the original template decl.
12529 if (!lambda_fntype)
12530 {
12531 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12532 SET_DECL_IMPLICIT_INSTANTIATION (r);
12533 }
12534 else
12535 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12536
12537 /* The template parameters for this new template are all the
12538 template parameters for the old template, except the
12539 outermost level of parameters. */
12540 DECL_TEMPLATE_PARMS (r)
12541 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12542 complain);
12543
12544 if (TREE_CODE (decl) == TYPE_DECL
12545 && !TYPE_DECL_ALIAS_P (decl))
12546 {
12547 tree new_type;
12548 ++processing_template_decl;
12549 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12550 --processing_template_decl;
12551 if (new_type == error_mark_node)
12552 return error_mark_node;
12553
12554 TREE_TYPE (r) = new_type;
12555 /* For a partial specialization, we need to keep pointing to
12556 the primary template. */
12557 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12558 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12559 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12560 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12561 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12562 }
12563 else
12564 {
12565 tree new_decl;
12566 ++processing_template_decl;
12567 if (TREE_CODE (decl) == FUNCTION_DECL)
12568 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12569 else
12570 new_decl = tsubst (decl, args, complain, in_decl);
12571 --processing_template_decl;
12572 if (new_decl == error_mark_node)
12573 return error_mark_node;
12574
12575 DECL_TEMPLATE_RESULT (r) = new_decl;
12576 TREE_TYPE (r) = TREE_TYPE (new_decl);
12577 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12578 if (lambda_fntype)
12579 {
12580 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12581 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12582 }
12583 else
12584 {
12585 DECL_TI_TEMPLATE (new_decl) = r;
12586 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12587 }
12588 }
12589
12590 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12591 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12592
12593 if (PRIMARY_TEMPLATE_P (t))
12594 DECL_PRIMARY_TEMPLATE (r) = r;
12595
12596 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12597 && !lambda_fntype)
12598 /* Record this non-type partial instantiation. */
12599 register_specialization (r, t,
12600 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12601 false, hash);
12602
12603 return r;
12604 }
12605
12606 /* True if FN is the op() for a lambda in an uninstantiated template. */
12607
12608 bool
12609 lambda_fn_in_template_p (tree fn)
12610 {
12611 if (!fn || !LAMBDA_FUNCTION_P (fn))
12612 return false;
12613 tree closure = DECL_CONTEXT (fn);
12614 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12615 }
12616
12617 /* True if FN is the op() for a lambda regenerated from a lambda in an
12618 uninstantiated template. */
12619
12620 bool
12621 regenerated_lambda_fn_p (tree fn)
12622 {
12623 return (LAMBDA_FUNCTION_P (fn)
12624 && !DECL_TEMPLATE_INSTANTIATION (fn));
12625 }
12626
12627 /* We're instantiating a variable from template function TCTX. Return the
12628 corresponding current enclosing scope. This gets complicated because lambda
12629 functions in templates are regenerated rather than instantiated, but generic
12630 lambda functions are subsequently instantiated. */
12631
12632 static tree
12633 enclosing_instantiation_of (tree tctx)
12634 {
12635 tree fn = current_function_decl;
12636 int lambda_count = 0;
12637
12638 for (; tctx && lambda_fn_in_template_p (tctx);
12639 tctx = decl_function_context (tctx))
12640 ++lambda_count;
12641 for (; fn; fn = decl_function_context (fn))
12642 {
12643 tree lambda = fn;
12644 int flambda_count = 0;
12645 for (; fn && regenerated_lambda_fn_p (fn);
12646 fn = decl_function_context (fn))
12647 ++flambda_count;
12648 if (DECL_TEMPLATE_INFO (fn)
12649 ? most_general_template (fn) != most_general_template (tctx)
12650 : fn != tctx)
12651 continue;
12652 if (lambda_count)
12653 {
12654 fn = lambda;
12655 while (flambda_count-- > lambda_count)
12656 fn = decl_function_context (fn);
12657 }
12658 return fn;
12659 }
12660 gcc_unreachable ();
12661 }
12662
12663 /* Substitute the ARGS into the T, which is a _DECL. Return the
12664 result of the substitution. Issue error and warning messages under
12665 control of COMPLAIN. */
12666
12667 static tree
12668 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12669 {
12670 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12671 location_t saved_loc;
12672 tree r = NULL_TREE;
12673 tree in_decl = t;
12674 hashval_t hash = 0;
12675
12676 /* Set the filename and linenumber to improve error-reporting. */
12677 saved_loc = input_location;
12678 input_location = DECL_SOURCE_LOCATION (t);
12679
12680 switch (TREE_CODE (t))
12681 {
12682 case TEMPLATE_DECL:
12683 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12684 break;
12685
12686 case FUNCTION_DECL:
12687 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12688 break;
12689
12690 case PARM_DECL:
12691 {
12692 tree type = NULL_TREE;
12693 int i, len = 1;
12694 tree expanded_types = NULL_TREE;
12695 tree prev_r = NULL_TREE;
12696 tree first_r = NULL_TREE;
12697
12698 if (DECL_PACK_P (t))
12699 {
12700 /* If there is a local specialization that isn't a
12701 parameter pack, it means that we're doing a "simple"
12702 substitution from inside tsubst_pack_expansion. Just
12703 return the local specialization (which will be a single
12704 parm). */
12705 tree spec = retrieve_local_specialization (t);
12706 if (spec
12707 && TREE_CODE (spec) == PARM_DECL
12708 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12709 RETURN (spec);
12710
12711 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12712 the parameters in this function parameter pack. */
12713 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12714 complain, in_decl);
12715 if (TREE_CODE (expanded_types) == TREE_VEC)
12716 {
12717 len = TREE_VEC_LENGTH (expanded_types);
12718
12719 /* Zero-length parameter packs are boring. Just substitute
12720 into the chain. */
12721 if (len == 0)
12722 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12723 TREE_CHAIN (t)));
12724 }
12725 else
12726 {
12727 /* All we did was update the type. Make a note of that. */
12728 type = expanded_types;
12729 expanded_types = NULL_TREE;
12730 }
12731 }
12732
12733 /* Loop through all of the parameters we'll build. When T is
12734 a function parameter pack, LEN is the number of expanded
12735 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12736 r = NULL_TREE;
12737 for (i = 0; i < len; ++i)
12738 {
12739 prev_r = r;
12740 r = copy_node (t);
12741 if (DECL_TEMPLATE_PARM_P (t))
12742 SET_DECL_TEMPLATE_PARM_P (r);
12743
12744 if (expanded_types)
12745 /* We're on the Ith parameter of the function parameter
12746 pack. */
12747 {
12748 /* Get the Ith type. */
12749 type = TREE_VEC_ELT (expanded_types, i);
12750
12751 /* Rename the parameter to include the index. */
12752 DECL_NAME (r)
12753 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12754 }
12755 else if (!type)
12756 /* We're dealing with a normal parameter. */
12757 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12758
12759 type = type_decays_to (type);
12760 TREE_TYPE (r) = type;
12761 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12762
12763 if (DECL_INITIAL (r))
12764 {
12765 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12766 DECL_INITIAL (r) = TREE_TYPE (r);
12767 else
12768 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12769 complain, in_decl);
12770 }
12771
12772 DECL_CONTEXT (r) = NULL_TREE;
12773
12774 if (!DECL_TEMPLATE_PARM_P (r))
12775 DECL_ARG_TYPE (r) = type_passed_as (type);
12776
12777 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12778 args, complain, in_decl);
12779
12780 /* Keep track of the first new parameter we
12781 generate. That's what will be returned to the
12782 caller. */
12783 if (!first_r)
12784 first_r = r;
12785
12786 /* Build a proper chain of parameters when substituting
12787 into a function parameter pack. */
12788 if (prev_r)
12789 DECL_CHAIN (prev_r) = r;
12790 }
12791
12792 /* If cp_unevaluated_operand is set, we're just looking for a
12793 single dummy parameter, so don't keep going. */
12794 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12795 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12796 complain, DECL_CHAIN (t));
12797
12798 /* FIRST_R contains the start of the chain we've built. */
12799 r = first_r;
12800 }
12801 break;
12802
12803 case FIELD_DECL:
12804 {
12805 tree type = NULL_TREE;
12806 tree vec = NULL_TREE;
12807 tree expanded_types = NULL_TREE;
12808 int len = 1;
12809
12810 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12811 {
12812 /* This field is a lambda capture pack. Return a TREE_VEC of
12813 the expanded fields to instantiate_class_template_1 and
12814 store them in the specializations hash table as a
12815 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12816 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12817 complain, in_decl);
12818 if (TREE_CODE (expanded_types) == TREE_VEC)
12819 {
12820 len = TREE_VEC_LENGTH (expanded_types);
12821 vec = make_tree_vec (len);
12822 }
12823 else
12824 {
12825 /* All we did was update the type. Make a note of that. */
12826 type = expanded_types;
12827 expanded_types = NULL_TREE;
12828 }
12829 }
12830
12831 for (int i = 0; i < len; ++i)
12832 {
12833 r = copy_decl (t);
12834 if (expanded_types)
12835 {
12836 type = TREE_VEC_ELT (expanded_types, i);
12837 DECL_NAME (r)
12838 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12839 }
12840 else if (!type)
12841 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12842
12843 if (type == error_mark_node)
12844 RETURN (error_mark_node);
12845 TREE_TYPE (r) = type;
12846 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12847
12848 if (DECL_C_BIT_FIELD (r))
12849 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12850 number of bits. */
12851 DECL_BIT_FIELD_REPRESENTATIVE (r)
12852 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
12853 complain, in_decl,
12854 /*integral_constant_expression_p=*/true);
12855 if (DECL_INITIAL (t))
12856 {
12857 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12858 NSDMI in perform_member_init. Still set DECL_INITIAL
12859 so that we know there is one. */
12860 DECL_INITIAL (r) = void_node;
12861 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12862 retrofit_lang_decl (r);
12863 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12864 }
12865 /* We don't have to set DECL_CONTEXT here; it is set by
12866 finish_member_declaration. */
12867 DECL_CHAIN (r) = NULL_TREE;
12868
12869 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12870 args, complain, in_decl);
12871
12872 if (vec)
12873 TREE_VEC_ELT (vec, i) = r;
12874 }
12875
12876 if (vec)
12877 {
12878 r = vec;
12879 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12880 SET_ARGUMENT_PACK_ARGS (pack, vec);
12881 register_specialization (pack, t, args, false, 0);
12882 }
12883 }
12884 break;
12885
12886 case USING_DECL:
12887 /* We reach here only for member using decls. We also need to check
12888 uses_template_parms because DECL_DEPENDENT_P is not set for a
12889 using-declaration that designates a member of the current
12890 instantiation (c++/53549). */
12891 if (DECL_DEPENDENT_P (t)
12892 || uses_template_parms (USING_DECL_SCOPE (t)))
12893 {
12894 tree scope = USING_DECL_SCOPE (t);
12895 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12896 if (PACK_EXPANSION_P (scope))
12897 {
12898 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12899 int len = TREE_VEC_LENGTH (vec);
12900 r = make_tree_vec (len);
12901 for (int i = 0; i < len; ++i)
12902 {
12903 tree escope = TREE_VEC_ELT (vec, i);
12904 tree elt = do_class_using_decl (escope, name);
12905 if (!elt)
12906 {
12907 r = error_mark_node;
12908 break;
12909 }
12910 else
12911 {
12912 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12913 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12914 }
12915 TREE_VEC_ELT (r, i) = elt;
12916 }
12917 }
12918 else
12919 {
12920 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12921 complain, in_decl);
12922 r = do_class_using_decl (inst_scope, name);
12923 if (!r)
12924 r = error_mark_node;
12925 else
12926 {
12927 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12928 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12929 }
12930 }
12931 }
12932 else
12933 {
12934 r = copy_node (t);
12935 DECL_CHAIN (r) = NULL_TREE;
12936 }
12937 break;
12938
12939 case TYPE_DECL:
12940 case VAR_DECL:
12941 {
12942 tree argvec = NULL_TREE;
12943 tree gen_tmpl = NULL_TREE;
12944 tree spec;
12945 tree tmpl = NULL_TREE;
12946 tree ctx;
12947 tree type = NULL_TREE;
12948 bool local_p;
12949
12950 if (TREE_TYPE (t) == error_mark_node)
12951 RETURN (error_mark_node);
12952
12953 if (TREE_CODE (t) == TYPE_DECL
12954 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12955 {
12956 /* If this is the canonical decl, we don't have to
12957 mess with instantiations, and often we can't (for
12958 typename, template type parms and such). Note that
12959 TYPE_NAME is not correct for the above test if
12960 we've copied the type for a typedef. */
12961 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12962 if (type == error_mark_node)
12963 RETURN (error_mark_node);
12964 r = TYPE_NAME (type);
12965 break;
12966 }
12967
12968 /* Check to see if we already have the specialization we
12969 need. */
12970 spec = NULL_TREE;
12971 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12972 {
12973 /* T is a static data member or namespace-scope entity.
12974 We have to substitute into namespace-scope variables
12975 (not just variable templates) because of cases like:
12976
12977 template <class T> void f() { extern T t; }
12978
12979 where the entity referenced is not known until
12980 instantiation time. */
12981 local_p = false;
12982 ctx = DECL_CONTEXT (t);
12983 if (DECL_CLASS_SCOPE_P (t))
12984 {
12985 ctx = tsubst_aggr_type (ctx, args,
12986 complain,
12987 in_decl, /*entering_scope=*/1);
12988 /* If CTX is unchanged, then T is in fact the
12989 specialization we want. That situation occurs when
12990 referencing a static data member within in its own
12991 class. We can use pointer equality, rather than
12992 same_type_p, because DECL_CONTEXT is always
12993 canonical... */
12994 if (ctx == DECL_CONTEXT (t)
12995 /* ... unless T is a member template; in which
12996 case our caller can be willing to create a
12997 specialization of that template represented
12998 by T. */
12999 && !(DECL_TI_TEMPLATE (t)
13000 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13001 spec = t;
13002 }
13003
13004 if (!spec)
13005 {
13006 tmpl = DECL_TI_TEMPLATE (t);
13007 gen_tmpl = most_general_template (tmpl);
13008 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13009 if (argvec != error_mark_node)
13010 argvec = (coerce_innermost_template_parms
13011 (DECL_TEMPLATE_PARMS (gen_tmpl),
13012 argvec, t, complain,
13013 /*all*/true, /*defarg*/true));
13014 if (argvec == error_mark_node)
13015 RETURN (error_mark_node);
13016 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13017 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13018 }
13019 }
13020 else
13021 {
13022 /* A local variable. */
13023 local_p = true;
13024 /* Subsequent calls to pushdecl will fill this in. */
13025 ctx = NULL_TREE;
13026 /* Unless this is a reference to a static variable from an
13027 enclosing function, in which case we need to fill it in now. */
13028 if (TREE_STATIC (t))
13029 {
13030 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13031 if (fn != current_function_decl)
13032 ctx = fn;
13033 }
13034 spec = retrieve_local_specialization (t);
13035 }
13036 /* If we already have the specialization we need, there is
13037 nothing more to do. */
13038 if (spec)
13039 {
13040 r = spec;
13041 break;
13042 }
13043
13044 /* Create a new node for the specialization we need. */
13045 r = copy_decl (t);
13046 if (type == NULL_TREE)
13047 {
13048 if (is_typedef_decl (t))
13049 type = DECL_ORIGINAL_TYPE (t);
13050 else
13051 type = TREE_TYPE (t);
13052 if (VAR_P (t)
13053 && VAR_HAD_UNKNOWN_BOUND (t)
13054 && type != error_mark_node)
13055 type = strip_array_domain (type);
13056 tree sub_args = args;
13057 if (tree auto_node = type_uses_auto (type))
13058 {
13059 /* Mask off any template args past the variable's context so we
13060 don't replace the auto with an unrelated argument. */
13061 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13062 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13063 if (extra > 0)
13064 /* This should never happen with the new lambda instantiation
13065 model, but keep the handling just in case. */
13066 gcc_assert (!CHECKING_P),
13067 sub_args = strip_innermost_template_args (args, extra);
13068 }
13069 type = tsubst (type, sub_args, complain, in_decl);
13070 }
13071 if (VAR_P (r))
13072 {
13073 /* Even if the original location is out of scope, the
13074 newly substituted one is not. */
13075 DECL_DEAD_FOR_LOCAL (r) = 0;
13076 DECL_INITIALIZED_P (r) = 0;
13077 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13078 if (type == error_mark_node)
13079 RETURN (error_mark_node);
13080 if (TREE_CODE (type) == FUNCTION_TYPE)
13081 {
13082 /* It may seem that this case cannot occur, since:
13083
13084 typedef void f();
13085 void g() { f x; }
13086
13087 declares a function, not a variable. However:
13088
13089 typedef void f();
13090 template <typename T> void g() { T t; }
13091 template void g<f>();
13092
13093 is an attempt to declare a variable with function
13094 type. */
13095 error ("variable %qD has function type",
13096 /* R is not yet sufficiently initialized, so we
13097 just use its name. */
13098 DECL_NAME (r));
13099 RETURN (error_mark_node);
13100 }
13101 type = complete_type (type);
13102 /* Wait until cp_finish_decl to set this again, to handle
13103 circular dependency (template/instantiate6.C). */
13104 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13105 type = check_var_type (DECL_NAME (r), type);
13106
13107 if (DECL_HAS_VALUE_EXPR_P (t))
13108 {
13109 tree ve = DECL_VALUE_EXPR (t);
13110 ve = tsubst_expr (ve, args, complain, in_decl,
13111 /*constant_expression_p=*/false);
13112 if (REFERENCE_REF_P (ve))
13113 {
13114 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13115 ve = TREE_OPERAND (ve, 0);
13116 }
13117 SET_DECL_VALUE_EXPR (r, ve);
13118 }
13119 if (CP_DECL_THREAD_LOCAL_P (r)
13120 && !processing_template_decl)
13121 set_decl_tls_model (r, decl_default_tls_model (r));
13122 }
13123 else if (DECL_SELF_REFERENCE_P (t))
13124 SET_DECL_SELF_REFERENCE_P (r);
13125 TREE_TYPE (r) = type;
13126 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13127 DECL_CONTEXT (r) = ctx;
13128 /* Clear out the mangled name and RTL for the instantiation. */
13129 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13130 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13131 SET_DECL_RTL (r, NULL);
13132 /* The initializer must not be expanded until it is required;
13133 see [temp.inst]. */
13134 DECL_INITIAL (r) = NULL_TREE;
13135 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13136 if (VAR_P (r))
13137 {
13138 SET_DECL_MODE (r, VOIDmode);
13139
13140 /* Possibly limit visibility based on template args. */
13141 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13142 if (DECL_VISIBILITY_SPECIFIED (t))
13143 {
13144 DECL_VISIBILITY_SPECIFIED (r) = 0;
13145 DECL_ATTRIBUTES (r)
13146 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13147 }
13148 determine_visibility (r);
13149 }
13150
13151 if (!local_p)
13152 {
13153 /* A static data member declaration is always marked
13154 external when it is declared in-class, even if an
13155 initializer is present. We mimic the non-template
13156 processing here. */
13157 DECL_EXTERNAL (r) = 1;
13158 if (DECL_NAMESPACE_SCOPE_P (t))
13159 DECL_NOT_REALLY_EXTERN (r) = 1;
13160
13161 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13162 SET_DECL_IMPLICIT_INSTANTIATION (r);
13163 register_specialization (r, gen_tmpl, argvec, false, hash);
13164 }
13165 else
13166 {
13167 if (DECL_LANG_SPECIFIC (r))
13168 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13169 if (!cp_unevaluated_operand)
13170 register_local_specialization (r, t);
13171 }
13172
13173 DECL_CHAIN (r) = NULL_TREE;
13174
13175 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13176 /*flags=*/0,
13177 args, complain, in_decl);
13178
13179 /* Preserve a typedef that names a type. */
13180 if (is_typedef_decl (r) && type != error_mark_node)
13181 {
13182 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13183 set_underlying_type (r);
13184 if (TYPE_DECL_ALIAS_P (r))
13185 /* An alias template specialization can be dependent
13186 even if its underlying type is not. */
13187 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13188 }
13189
13190 layout_decl (r, 0);
13191 }
13192 break;
13193
13194 default:
13195 gcc_unreachable ();
13196 }
13197 #undef RETURN
13198
13199 out:
13200 /* Restore the file and line information. */
13201 input_location = saved_loc;
13202
13203 return r;
13204 }
13205
13206 /* Substitute into the ARG_TYPES of a function type.
13207 If END is a TREE_CHAIN, leave it and any following types
13208 un-substituted. */
13209
13210 static tree
13211 tsubst_arg_types (tree arg_types,
13212 tree args,
13213 tree end,
13214 tsubst_flags_t complain,
13215 tree in_decl)
13216 {
13217 tree remaining_arg_types;
13218 tree type = NULL_TREE;
13219 int i = 1;
13220 tree expanded_args = NULL_TREE;
13221 tree default_arg;
13222
13223 if (!arg_types || arg_types == void_list_node || arg_types == end)
13224 return arg_types;
13225
13226 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13227 args, end, complain, in_decl);
13228 if (remaining_arg_types == error_mark_node)
13229 return error_mark_node;
13230
13231 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13232 {
13233 /* For a pack expansion, perform substitution on the
13234 entire expression. Later on, we'll handle the arguments
13235 one-by-one. */
13236 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13237 args, complain, in_decl);
13238
13239 if (TREE_CODE (expanded_args) == TREE_VEC)
13240 /* So that we'll spin through the parameters, one by one. */
13241 i = TREE_VEC_LENGTH (expanded_args);
13242 else
13243 {
13244 /* We only partially substituted into the parameter
13245 pack. Our type is TYPE_PACK_EXPANSION. */
13246 type = expanded_args;
13247 expanded_args = NULL_TREE;
13248 }
13249 }
13250
13251 while (i > 0) {
13252 --i;
13253
13254 if (expanded_args)
13255 type = TREE_VEC_ELT (expanded_args, i);
13256 else if (!type)
13257 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13258
13259 if (type == error_mark_node)
13260 return error_mark_node;
13261 if (VOID_TYPE_P (type))
13262 {
13263 if (complain & tf_error)
13264 {
13265 error ("invalid parameter type %qT", type);
13266 if (in_decl)
13267 error ("in declaration %q+D", in_decl);
13268 }
13269 return error_mark_node;
13270 }
13271 /* DR 657. */
13272 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13273 return error_mark_node;
13274
13275 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13276 top-level qualifiers as required. */
13277 type = cv_unqualified (type_decays_to (type));
13278
13279 /* We do not substitute into default arguments here. The standard
13280 mandates that they be instantiated only when needed, which is
13281 done in build_over_call. */
13282 default_arg = TREE_PURPOSE (arg_types);
13283
13284 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13285 since the new op() won't have any associated template arguments for us
13286 to refer to later. */
13287 if (lambda_fn_in_template_p (in_decl))
13288 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13289 false/*fn*/, false/*constexpr*/);
13290
13291 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13292 {
13293 /* We've instantiated a template before its default arguments
13294 have been parsed. This can happen for a nested template
13295 class, and is not an error unless we require the default
13296 argument in a call of this function. */
13297 remaining_arg_types =
13298 tree_cons (default_arg, type, remaining_arg_types);
13299 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13300 }
13301 else
13302 remaining_arg_types =
13303 hash_tree_cons (default_arg, type, remaining_arg_types);
13304 }
13305
13306 return remaining_arg_types;
13307 }
13308
13309 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13310 *not* handle the exception-specification for FNTYPE, because the
13311 initial substitution of explicitly provided template parameters
13312 during argument deduction forbids substitution into the
13313 exception-specification:
13314
13315 [temp.deduct]
13316
13317 All references in the function type of the function template to the
13318 corresponding template parameters are replaced by the specified tem-
13319 plate argument values. If a substitution in a template parameter or
13320 in the function type of the function template results in an invalid
13321 type, type deduction fails. [Note: The equivalent substitution in
13322 exception specifications is done only when the function is instanti-
13323 ated, at which point a program is ill-formed if the substitution
13324 results in an invalid type.] */
13325
13326 static tree
13327 tsubst_function_type (tree t,
13328 tree args,
13329 tsubst_flags_t complain,
13330 tree in_decl)
13331 {
13332 tree return_type;
13333 tree arg_types = NULL_TREE;
13334 tree fntype;
13335
13336 /* The TYPE_CONTEXT is not used for function/method types. */
13337 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13338
13339 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13340 failure. */
13341 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13342
13343 if (late_return_type_p)
13344 {
13345 /* Substitute the argument types. */
13346 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13347 complain, in_decl);
13348 if (arg_types == error_mark_node)
13349 return error_mark_node;
13350
13351 tree save_ccp = current_class_ptr;
13352 tree save_ccr = current_class_ref;
13353 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13354 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13355 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13356 if (do_inject)
13357 {
13358 /* DR 1207: 'this' is in scope in the trailing return type. */
13359 inject_this_parameter (this_type, cp_type_quals (this_type));
13360 }
13361
13362 /* Substitute the return type. */
13363 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13364
13365 if (do_inject)
13366 {
13367 current_class_ptr = save_ccp;
13368 current_class_ref = save_ccr;
13369 }
13370 }
13371 else
13372 /* Substitute the return type. */
13373 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13374
13375 if (return_type == error_mark_node)
13376 return error_mark_node;
13377 /* DR 486 clarifies that creation of a function type with an
13378 invalid return type is a deduction failure. */
13379 if (TREE_CODE (return_type) == ARRAY_TYPE
13380 || TREE_CODE (return_type) == FUNCTION_TYPE)
13381 {
13382 if (complain & tf_error)
13383 {
13384 if (TREE_CODE (return_type) == ARRAY_TYPE)
13385 error ("function returning an array");
13386 else
13387 error ("function returning a function");
13388 }
13389 return error_mark_node;
13390 }
13391 /* And DR 657. */
13392 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13393 return error_mark_node;
13394
13395 if (!late_return_type_p)
13396 {
13397 /* Substitute the argument types. */
13398 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13399 complain, in_decl);
13400 if (arg_types == error_mark_node)
13401 return error_mark_node;
13402 }
13403
13404 /* Construct a new type node and return it. */
13405 if (TREE_CODE (t) == FUNCTION_TYPE)
13406 {
13407 fntype = build_function_type (return_type, arg_types);
13408 fntype = apply_memfn_quals (fntype,
13409 type_memfn_quals (t),
13410 type_memfn_rqual (t));
13411 }
13412 else
13413 {
13414 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13415 /* Don't pick up extra function qualifiers from the basetype. */
13416 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13417 if (! MAYBE_CLASS_TYPE_P (r))
13418 {
13419 /* [temp.deduct]
13420
13421 Type deduction may fail for any of the following
13422 reasons:
13423
13424 -- Attempting to create "pointer to member of T" when T
13425 is not a class type. */
13426 if (complain & tf_error)
13427 error ("creating pointer to member function of non-class type %qT",
13428 r);
13429 return error_mark_node;
13430 }
13431
13432 fntype = build_method_type_directly (r, return_type,
13433 TREE_CHAIN (arg_types));
13434 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13435 }
13436 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13437
13438 if (late_return_type_p)
13439 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13440
13441 return fntype;
13442 }
13443
13444 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13445 ARGS into that specification, and return the substituted
13446 specification. If there is no specification, return NULL_TREE. */
13447
13448 static tree
13449 tsubst_exception_specification (tree fntype,
13450 tree args,
13451 tsubst_flags_t complain,
13452 tree in_decl,
13453 bool defer_ok)
13454 {
13455 tree specs;
13456 tree new_specs;
13457
13458 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13459 new_specs = NULL_TREE;
13460 if (specs && TREE_PURPOSE (specs))
13461 {
13462 /* A noexcept-specifier. */
13463 tree expr = TREE_PURPOSE (specs);
13464 if (TREE_CODE (expr) == INTEGER_CST)
13465 new_specs = expr;
13466 else if (defer_ok)
13467 {
13468 /* Defer instantiation of noexcept-specifiers to avoid
13469 excessive instantiations (c++/49107). */
13470 new_specs = make_node (DEFERRED_NOEXCEPT);
13471 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13472 {
13473 /* We already partially instantiated this member template,
13474 so combine the new args with the old. */
13475 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13476 = DEFERRED_NOEXCEPT_PATTERN (expr);
13477 DEFERRED_NOEXCEPT_ARGS (new_specs)
13478 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13479 }
13480 else
13481 {
13482 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13483 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13484 }
13485 }
13486 else
13487 new_specs = tsubst_copy_and_build
13488 (expr, args, complain, in_decl, /*function_p=*/false,
13489 /*integral_constant_expression_p=*/true);
13490 new_specs = build_noexcept_spec (new_specs, complain);
13491 }
13492 else if (specs)
13493 {
13494 if (! TREE_VALUE (specs))
13495 new_specs = specs;
13496 else
13497 while (specs)
13498 {
13499 tree spec;
13500 int i, len = 1;
13501 tree expanded_specs = NULL_TREE;
13502
13503 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13504 {
13505 /* Expand the pack expansion type. */
13506 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13507 args, complain,
13508 in_decl);
13509
13510 if (expanded_specs == error_mark_node)
13511 return error_mark_node;
13512 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13513 len = TREE_VEC_LENGTH (expanded_specs);
13514 else
13515 {
13516 /* We're substituting into a member template, so
13517 we got a TYPE_PACK_EXPANSION back. Add that
13518 expansion and move on. */
13519 gcc_assert (TREE_CODE (expanded_specs)
13520 == TYPE_PACK_EXPANSION);
13521 new_specs = add_exception_specifier (new_specs,
13522 expanded_specs,
13523 complain);
13524 specs = TREE_CHAIN (specs);
13525 continue;
13526 }
13527 }
13528
13529 for (i = 0; i < len; ++i)
13530 {
13531 if (expanded_specs)
13532 spec = TREE_VEC_ELT (expanded_specs, i);
13533 else
13534 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13535 if (spec == error_mark_node)
13536 return spec;
13537 new_specs = add_exception_specifier (new_specs, spec,
13538 complain);
13539 }
13540
13541 specs = TREE_CHAIN (specs);
13542 }
13543 }
13544 return new_specs;
13545 }
13546
13547 /* Take the tree structure T and replace template parameters used
13548 therein with the argument vector ARGS. IN_DECL is an associated
13549 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13550 Issue error and warning messages under control of COMPLAIN. Note
13551 that we must be relatively non-tolerant of extensions here, in
13552 order to preserve conformance; if we allow substitutions that
13553 should not be allowed, we may allow argument deductions that should
13554 not succeed, and therefore report ambiguous overload situations
13555 where there are none. In theory, we could allow the substitution,
13556 but indicate that it should have failed, and allow our caller to
13557 make sure that the right thing happens, but we don't try to do this
13558 yet.
13559
13560 This function is used for dealing with types, decls and the like;
13561 for expressions, use tsubst_expr or tsubst_copy. */
13562
13563 tree
13564 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13565 {
13566 enum tree_code code;
13567 tree type, r = NULL_TREE;
13568
13569 if (t == NULL_TREE || t == error_mark_node
13570 || t == integer_type_node
13571 || t == void_type_node
13572 || t == char_type_node
13573 || t == unknown_type_node
13574 || TREE_CODE (t) == NAMESPACE_DECL
13575 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13576 return t;
13577
13578 if (DECL_P (t))
13579 return tsubst_decl (t, args, complain);
13580
13581 if (args == NULL_TREE)
13582 return t;
13583
13584 code = TREE_CODE (t);
13585
13586 if (code == IDENTIFIER_NODE)
13587 type = IDENTIFIER_TYPE_VALUE (t);
13588 else
13589 type = TREE_TYPE (t);
13590
13591 gcc_assert (type != unknown_type_node);
13592
13593 /* Reuse typedefs. We need to do this to handle dependent attributes,
13594 such as attribute aligned. */
13595 if (TYPE_P (t)
13596 && typedef_variant_p (t))
13597 {
13598 tree decl = TYPE_NAME (t);
13599
13600 if (alias_template_specialization_p (t))
13601 {
13602 /* DECL represents an alias template and we want to
13603 instantiate it. */
13604 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13605 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13606 r = instantiate_alias_template (tmpl, gen_args, complain);
13607 }
13608 else if (DECL_CLASS_SCOPE_P (decl)
13609 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13610 && uses_template_parms (DECL_CONTEXT (decl)))
13611 {
13612 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13613 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13614 r = retrieve_specialization (tmpl, gen_args, 0);
13615 }
13616 else if (DECL_FUNCTION_SCOPE_P (decl)
13617 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13618 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13619 r = retrieve_local_specialization (decl);
13620 else
13621 /* The typedef is from a non-template context. */
13622 return t;
13623
13624 if (r)
13625 {
13626 r = TREE_TYPE (r);
13627 r = cp_build_qualified_type_real
13628 (r, cp_type_quals (t) | cp_type_quals (r),
13629 complain | tf_ignore_bad_quals);
13630 return r;
13631 }
13632 else
13633 {
13634 /* We don't have an instantiation yet, so drop the typedef. */
13635 int quals = cp_type_quals (t);
13636 t = DECL_ORIGINAL_TYPE (decl);
13637 t = cp_build_qualified_type_real (t, quals,
13638 complain | tf_ignore_bad_quals);
13639 }
13640 }
13641
13642 bool fndecl_type = (complain & tf_fndecl_type);
13643 complain &= ~tf_fndecl_type;
13644
13645 if (type
13646 && code != TYPENAME_TYPE
13647 && code != TEMPLATE_TYPE_PARM
13648 && code != TEMPLATE_PARM_INDEX
13649 && code != IDENTIFIER_NODE
13650 && code != FUNCTION_TYPE
13651 && code != METHOD_TYPE)
13652 type = tsubst (type, args, complain, in_decl);
13653 if (type == error_mark_node)
13654 return error_mark_node;
13655
13656 switch (code)
13657 {
13658 case RECORD_TYPE:
13659 case UNION_TYPE:
13660 case ENUMERAL_TYPE:
13661 return tsubst_aggr_type (t, args, complain, in_decl,
13662 /*entering_scope=*/0);
13663
13664 case ERROR_MARK:
13665 case IDENTIFIER_NODE:
13666 case VOID_TYPE:
13667 case REAL_TYPE:
13668 case COMPLEX_TYPE:
13669 case VECTOR_TYPE:
13670 case BOOLEAN_TYPE:
13671 case NULLPTR_TYPE:
13672 case LANG_TYPE:
13673 return t;
13674
13675 case INTEGER_TYPE:
13676 if (t == integer_type_node)
13677 return t;
13678
13679 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13680 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13681 return t;
13682
13683 {
13684 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13685
13686 max = tsubst_expr (omax, args, complain, in_decl,
13687 /*integral_constant_expression_p=*/false);
13688
13689 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13690 needed. */
13691 if (TREE_CODE (max) == NOP_EXPR
13692 && TREE_SIDE_EFFECTS (omax)
13693 && !TREE_TYPE (max))
13694 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13695
13696 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13697 with TREE_SIDE_EFFECTS that indicates this is not an integral
13698 constant expression. */
13699 if (processing_template_decl
13700 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13701 {
13702 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13703 TREE_SIDE_EFFECTS (max) = 1;
13704 }
13705
13706 return compute_array_index_type (NULL_TREE, max, complain);
13707 }
13708
13709 case TEMPLATE_TYPE_PARM:
13710 case TEMPLATE_TEMPLATE_PARM:
13711 case BOUND_TEMPLATE_TEMPLATE_PARM:
13712 case TEMPLATE_PARM_INDEX:
13713 {
13714 int idx;
13715 int level;
13716 int levels;
13717 tree arg = NULL_TREE;
13718
13719 /* Early in template argument deduction substitution, we don't
13720 want to reduce the level of 'auto', or it will be confused
13721 with a normal template parm in subsequent deduction. */
13722 if (is_auto (t) && (complain & tf_partial))
13723 return t;
13724
13725 r = NULL_TREE;
13726
13727 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13728 template_parm_level_and_index (t, &level, &idx);
13729
13730 levels = TMPL_ARGS_DEPTH (args);
13731 if (level <= levels
13732 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13733 {
13734 arg = TMPL_ARG (args, level, idx);
13735
13736 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13737 {
13738 /* See through ARGUMENT_PACK_SELECT arguments. */
13739 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13740 /* If the selected argument is an expansion E, that most
13741 likely means we were called from
13742 gen_elem_of_pack_expansion_instantiation during the
13743 substituting of pack an argument pack (which Ith
13744 element is a pack expansion, where I is
13745 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13746 In this case, the Ith element resulting from this
13747 substituting is going to be a pack expansion, which
13748 pattern is the pattern of E. Let's return the
13749 pattern of E, and
13750 gen_elem_of_pack_expansion_instantiation will
13751 build the resulting pack expansion from it. */
13752 if (PACK_EXPANSION_P (arg))
13753 {
13754 /* Make sure we aren't throwing away arg info. */
13755 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13756 arg = PACK_EXPANSION_PATTERN (arg);
13757 }
13758 }
13759 }
13760
13761 if (arg == error_mark_node)
13762 return error_mark_node;
13763 else if (arg != NULL_TREE)
13764 {
13765 if (ARGUMENT_PACK_P (arg))
13766 /* If ARG is an argument pack, we don't actually want to
13767 perform a substitution here, because substitutions
13768 for argument packs are only done
13769 element-by-element. We can get to this point when
13770 substituting the type of a non-type template
13771 parameter pack, when that type actually contains
13772 template parameter packs from an outer template, e.g.,
13773
13774 template<typename... Types> struct A {
13775 template<Types... Values> struct B { };
13776 }; */
13777 return t;
13778
13779 if (code == TEMPLATE_TYPE_PARM)
13780 {
13781 int quals;
13782 gcc_assert (TYPE_P (arg));
13783
13784 quals = cp_type_quals (arg) | cp_type_quals (t);
13785
13786 return cp_build_qualified_type_real
13787 (arg, quals, complain | tf_ignore_bad_quals);
13788 }
13789 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13790 {
13791 /* We are processing a type constructed from a
13792 template template parameter. */
13793 tree argvec = tsubst (TYPE_TI_ARGS (t),
13794 args, complain, in_decl);
13795 if (argvec == error_mark_node)
13796 return error_mark_node;
13797
13798 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13799 || TREE_CODE (arg) == TEMPLATE_DECL
13800 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13801
13802 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13803 /* Consider this code:
13804
13805 template <template <class> class Template>
13806 struct Internal {
13807 template <class Arg> using Bind = Template<Arg>;
13808 };
13809
13810 template <template <class> class Template, class Arg>
13811 using Instantiate = Template<Arg>; //#0
13812
13813 template <template <class> class Template,
13814 class Argument>
13815 using Bind =
13816 Instantiate<Internal<Template>::template Bind,
13817 Argument>; //#1
13818
13819 When #1 is parsed, the
13820 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13821 parameter `Template' in #0 matches the
13822 UNBOUND_CLASS_TEMPLATE representing the argument
13823 `Internal<Template>::template Bind'; We then want
13824 to assemble the type `Bind<Argument>' that can't
13825 be fully created right now, because
13826 `Internal<Template>' not being complete, the Bind
13827 template cannot be looked up in that context. So
13828 we need to "store" `Bind<Argument>' for later
13829 when the context of Bind becomes complete. Let's
13830 store that in a TYPENAME_TYPE. */
13831 return make_typename_type (TYPE_CONTEXT (arg),
13832 build_nt (TEMPLATE_ID_EXPR,
13833 TYPE_IDENTIFIER (arg),
13834 argvec),
13835 typename_type,
13836 complain);
13837
13838 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13839 are resolving nested-types in the signature of a
13840 member function templates. Otherwise ARG is a
13841 TEMPLATE_DECL and is the real template to be
13842 instantiated. */
13843 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13844 arg = TYPE_NAME (arg);
13845
13846 r = lookup_template_class (arg,
13847 argvec, in_decl,
13848 DECL_CONTEXT (arg),
13849 /*entering_scope=*/0,
13850 complain);
13851 return cp_build_qualified_type_real
13852 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13853 }
13854 else if (code == TEMPLATE_TEMPLATE_PARM)
13855 return arg;
13856 else
13857 /* TEMPLATE_PARM_INDEX. */
13858 return convert_from_reference (unshare_expr (arg));
13859 }
13860
13861 if (level == 1)
13862 /* This can happen during the attempted tsubst'ing in
13863 unify. This means that we don't yet have any information
13864 about the template parameter in question. */
13865 return t;
13866
13867 /* If we get here, we must have been looking at a parm for a
13868 more deeply nested template. Make a new version of this
13869 template parameter, but with a lower level. */
13870 switch (code)
13871 {
13872 case TEMPLATE_TYPE_PARM:
13873 case TEMPLATE_TEMPLATE_PARM:
13874 case BOUND_TEMPLATE_TEMPLATE_PARM:
13875 if (cp_type_quals (t))
13876 {
13877 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13878 r = cp_build_qualified_type_real
13879 (r, cp_type_quals (t),
13880 complain | (code == TEMPLATE_TYPE_PARM
13881 ? tf_ignore_bad_quals : 0));
13882 }
13883 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13884 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13885 && (r = (TEMPLATE_PARM_DESCENDANTS
13886 (TEMPLATE_TYPE_PARM_INDEX (t))))
13887 && (r = TREE_TYPE (r))
13888 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13889 /* Break infinite recursion when substituting the constraints
13890 of a constrained placeholder. */;
13891 else
13892 {
13893 r = copy_type (t);
13894 TEMPLATE_TYPE_PARM_INDEX (r)
13895 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13896 r, levels, args, complain);
13897 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13898 TYPE_MAIN_VARIANT (r) = r;
13899 TYPE_POINTER_TO (r) = NULL_TREE;
13900 TYPE_REFERENCE_TO (r) = NULL_TREE;
13901
13902 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13903 {
13904 /* Propagate constraints on placeholders. */
13905 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13906 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13907 = tsubst_constraint (constr, args, complain, in_decl);
13908 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13909 {
13910 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13911 pl = tsubst (pl, args, complain, in_decl);
13912 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13913 }
13914 }
13915
13916 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13917 /* We have reduced the level of the template
13918 template parameter, but not the levels of its
13919 template parameters, so canonical_type_parameter
13920 will not be able to find the canonical template
13921 template parameter for this level. Thus, we
13922 require structural equality checking to compare
13923 TEMPLATE_TEMPLATE_PARMs. */
13924 SET_TYPE_STRUCTURAL_EQUALITY (r);
13925 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13926 SET_TYPE_STRUCTURAL_EQUALITY (r);
13927 else
13928 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13929
13930 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13931 {
13932 tree tinfo = TYPE_TEMPLATE_INFO (t);
13933 /* We might need to substitute into the types of non-type
13934 template parameters. */
13935 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13936 complain, in_decl);
13937 if (tmpl == error_mark_node)
13938 return error_mark_node;
13939 tree argvec = tsubst (TI_ARGS (tinfo), args,
13940 complain, in_decl);
13941 if (argvec == error_mark_node)
13942 return error_mark_node;
13943
13944 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13945 = build_template_info (tmpl, argvec);
13946 }
13947 }
13948 break;
13949
13950 case TEMPLATE_PARM_INDEX:
13951 /* OK, now substitute the type of the non-type parameter. We
13952 couldn't do it earlier because it might be an auto parameter,
13953 and we wouldn't need to if we had an argument. */
13954 type = tsubst (type, args, complain, in_decl);
13955 if (type == error_mark_node)
13956 return error_mark_node;
13957 r = reduce_template_parm_level (t, type, levels, args, complain);
13958 break;
13959
13960 default:
13961 gcc_unreachable ();
13962 }
13963
13964 return r;
13965 }
13966
13967 case TREE_LIST:
13968 {
13969 tree purpose, value, chain;
13970
13971 if (t == void_list_node)
13972 return t;
13973
13974 purpose = TREE_PURPOSE (t);
13975 if (purpose)
13976 {
13977 purpose = tsubst (purpose, args, complain, in_decl);
13978 if (purpose == error_mark_node)
13979 return error_mark_node;
13980 }
13981 value = TREE_VALUE (t);
13982 if (value)
13983 {
13984 value = tsubst (value, args, complain, in_decl);
13985 if (value == error_mark_node)
13986 return error_mark_node;
13987 }
13988 chain = TREE_CHAIN (t);
13989 if (chain && chain != void_type_node)
13990 {
13991 chain = tsubst (chain, args, complain, in_decl);
13992 if (chain == error_mark_node)
13993 return error_mark_node;
13994 }
13995 if (purpose == TREE_PURPOSE (t)
13996 && value == TREE_VALUE (t)
13997 && chain == TREE_CHAIN (t))
13998 return t;
13999 return hash_tree_cons (purpose, value, chain);
14000 }
14001
14002 case TREE_BINFO:
14003 /* We should never be tsubsting a binfo. */
14004 gcc_unreachable ();
14005
14006 case TREE_VEC:
14007 /* A vector of template arguments. */
14008 gcc_assert (!type);
14009 return tsubst_template_args (t, args, complain, in_decl);
14010
14011 case POINTER_TYPE:
14012 case REFERENCE_TYPE:
14013 {
14014 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14015 return t;
14016
14017 /* [temp.deduct]
14018
14019 Type deduction may fail for any of the following
14020 reasons:
14021
14022 -- Attempting to create a pointer to reference type.
14023 -- Attempting to create a reference to a reference type or
14024 a reference to void.
14025
14026 Core issue 106 says that creating a reference to a reference
14027 during instantiation is no longer a cause for failure. We
14028 only enforce this check in strict C++98 mode. */
14029 if ((TREE_CODE (type) == REFERENCE_TYPE
14030 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14031 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14032 {
14033 static location_t last_loc;
14034
14035 /* We keep track of the last time we issued this error
14036 message to avoid spewing a ton of messages during a
14037 single bad template instantiation. */
14038 if (complain & tf_error
14039 && last_loc != input_location)
14040 {
14041 if (VOID_TYPE_P (type))
14042 error ("forming reference to void");
14043 else if (code == POINTER_TYPE)
14044 error ("forming pointer to reference type %qT", type);
14045 else
14046 error ("forming reference to reference type %qT", type);
14047 last_loc = input_location;
14048 }
14049
14050 return error_mark_node;
14051 }
14052 else if (TREE_CODE (type) == FUNCTION_TYPE
14053 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14054 || type_memfn_rqual (type) != REF_QUAL_NONE))
14055 {
14056 if (complain & tf_error)
14057 {
14058 if (code == POINTER_TYPE)
14059 error ("forming pointer to qualified function type %qT",
14060 type);
14061 else
14062 error ("forming reference to qualified function type %qT",
14063 type);
14064 }
14065 return error_mark_node;
14066 }
14067 else if (code == POINTER_TYPE)
14068 {
14069 r = build_pointer_type (type);
14070 if (TREE_CODE (type) == METHOD_TYPE)
14071 r = build_ptrmemfunc_type (r);
14072 }
14073 else if (TREE_CODE (type) == REFERENCE_TYPE)
14074 /* In C++0x, during template argument substitution, when there is an
14075 attempt to create a reference to a reference type, reference
14076 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14077
14078 "If a template-argument for a template-parameter T names a type
14079 that is a reference to a type A, an attempt to create the type
14080 'lvalue reference to cv T' creates the type 'lvalue reference to
14081 A,' while an attempt to create the type type rvalue reference to
14082 cv T' creates the type T"
14083 */
14084 r = cp_build_reference_type
14085 (TREE_TYPE (type),
14086 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14087 else
14088 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14089 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14090
14091 if (r != error_mark_node)
14092 /* Will this ever be needed for TYPE_..._TO values? */
14093 layout_type (r);
14094
14095 return r;
14096 }
14097 case OFFSET_TYPE:
14098 {
14099 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14100 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14101 {
14102 /* [temp.deduct]
14103
14104 Type deduction may fail for any of the following
14105 reasons:
14106
14107 -- Attempting to create "pointer to member of T" when T
14108 is not a class type. */
14109 if (complain & tf_error)
14110 error ("creating pointer to member of non-class type %qT", r);
14111 return error_mark_node;
14112 }
14113 if (TREE_CODE (type) == REFERENCE_TYPE)
14114 {
14115 if (complain & tf_error)
14116 error ("creating pointer to member reference type %qT", type);
14117 return error_mark_node;
14118 }
14119 if (VOID_TYPE_P (type))
14120 {
14121 if (complain & tf_error)
14122 error ("creating pointer to member of type void");
14123 return error_mark_node;
14124 }
14125 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14126 if (TREE_CODE (type) == FUNCTION_TYPE)
14127 {
14128 /* The type of the implicit object parameter gets its
14129 cv-qualifiers from the FUNCTION_TYPE. */
14130 tree memptr;
14131 tree method_type
14132 = build_memfn_type (type, r, type_memfn_quals (type),
14133 type_memfn_rqual (type));
14134 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14135 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14136 complain);
14137 }
14138 else
14139 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14140 cp_type_quals (t),
14141 complain);
14142 }
14143 case FUNCTION_TYPE:
14144 case METHOD_TYPE:
14145 {
14146 tree fntype;
14147 tree specs;
14148 fntype = tsubst_function_type (t, args, complain, in_decl);
14149 if (fntype == error_mark_node)
14150 return error_mark_node;
14151
14152 /* Substitute the exception specification. */
14153 specs = tsubst_exception_specification (t, args, complain, in_decl,
14154 /*defer_ok*/fndecl_type);
14155 if (specs == error_mark_node)
14156 return error_mark_node;
14157 if (specs)
14158 fntype = build_exception_variant (fntype, specs);
14159 return fntype;
14160 }
14161 case ARRAY_TYPE:
14162 {
14163 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14164 if (domain == error_mark_node)
14165 return error_mark_node;
14166
14167 /* As an optimization, we avoid regenerating the array type if
14168 it will obviously be the same as T. */
14169 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14170 return t;
14171
14172 /* These checks should match the ones in create_array_type_for_decl.
14173
14174 [temp.deduct]
14175
14176 The deduction may fail for any of the following reasons:
14177
14178 -- Attempting to create an array with an element type that
14179 is void, a function type, or a reference type, or [DR337]
14180 an abstract class type. */
14181 if (VOID_TYPE_P (type)
14182 || TREE_CODE (type) == FUNCTION_TYPE
14183 || (TREE_CODE (type) == ARRAY_TYPE
14184 && TYPE_DOMAIN (type) == NULL_TREE)
14185 || TREE_CODE (type) == REFERENCE_TYPE)
14186 {
14187 if (complain & tf_error)
14188 error ("creating array of %qT", type);
14189 return error_mark_node;
14190 }
14191
14192 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14193 return error_mark_node;
14194
14195 r = build_cplus_array_type (type, domain);
14196
14197 if (TYPE_USER_ALIGN (t))
14198 {
14199 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14200 TYPE_USER_ALIGN (r) = 1;
14201 }
14202
14203 return r;
14204 }
14205
14206 case TYPENAME_TYPE:
14207 {
14208 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14209 in_decl, /*entering_scope=*/1);
14210 if (ctx == error_mark_node)
14211 return error_mark_node;
14212
14213 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14214 complain, in_decl);
14215 if (f == error_mark_node)
14216 return error_mark_node;
14217
14218 if (!MAYBE_CLASS_TYPE_P (ctx))
14219 {
14220 if (complain & tf_error)
14221 error ("%qT is not a class, struct, or union type", ctx);
14222 return error_mark_node;
14223 }
14224 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14225 {
14226 /* Normally, make_typename_type does not require that the CTX
14227 have complete type in order to allow things like:
14228
14229 template <class T> struct S { typename S<T>::X Y; };
14230
14231 But, such constructs have already been resolved by this
14232 point, so here CTX really should have complete type, unless
14233 it's a partial instantiation. */
14234 ctx = complete_type (ctx);
14235 if (!COMPLETE_TYPE_P (ctx))
14236 {
14237 if (complain & tf_error)
14238 cxx_incomplete_type_error (NULL_TREE, ctx);
14239 return error_mark_node;
14240 }
14241 }
14242
14243 f = make_typename_type (ctx, f, typename_type,
14244 complain | tf_keep_type_decl);
14245 if (f == error_mark_node)
14246 return f;
14247 if (TREE_CODE (f) == TYPE_DECL)
14248 {
14249 complain |= tf_ignore_bad_quals;
14250 f = TREE_TYPE (f);
14251 }
14252
14253 if (TREE_CODE (f) != TYPENAME_TYPE)
14254 {
14255 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14256 {
14257 if (complain & tf_error)
14258 error ("%qT resolves to %qT, which is not an enumeration type",
14259 t, f);
14260 else
14261 return error_mark_node;
14262 }
14263 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14264 {
14265 if (complain & tf_error)
14266 error ("%qT resolves to %qT, which is is not a class type",
14267 t, f);
14268 else
14269 return error_mark_node;
14270 }
14271 }
14272
14273 return cp_build_qualified_type_real
14274 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14275 }
14276
14277 case UNBOUND_CLASS_TEMPLATE:
14278 {
14279 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14280 in_decl, /*entering_scope=*/1);
14281 tree name = TYPE_IDENTIFIER (t);
14282 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14283
14284 if (ctx == error_mark_node || name == error_mark_node)
14285 return error_mark_node;
14286
14287 if (parm_list)
14288 parm_list = tsubst_template_parms (parm_list, args, complain);
14289 return make_unbound_class_template (ctx, name, parm_list, complain);
14290 }
14291
14292 case TYPEOF_TYPE:
14293 {
14294 tree type;
14295
14296 ++cp_unevaluated_operand;
14297 ++c_inhibit_evaluation_warnings;
14298
14299 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14300 complain, in_decl,
14301 /*integral_constant_expression_p=*/false);
14302
14303 --cp_unevaluated_operand;
14304 --c_inhibit_evaluation_warnings;
14305
14306 type = finish_typeof (type);
14307 return cp_build_qualified_type_real (type,
14308 cp_type_quals (t)
14309 | cp_type_quals (type),
14310 complain);
14311 }
14312
14313 case DECLTYPE_TYPE:
14314 {
14315 tree type;
14316
14317 ++cp_unevaluated_operand;
14318 ++c_inhibit_evaluation_warnings;
14319
14320 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14321 complain|tf_decltype, in_decl,
14322 /*function_p*/false,
14323 /*integral_constant_expression*/false);
14324
14325 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14326 {
14327 if (type == NULL_TREE)
14328 {
14329 if (complain & tf_error)
14330 error ("empty initializer in lambda init-capture");
14331 type = error_mark_node;
14332 }
14333 else if (TREE_CODE (type) == TREE_LIST)
14334 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14335 }
14336
14337 --cp_unevaluated_operand;
14338 --c_inhibit_evaluation_warnings;
14339
14340 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14341 type = lambda_capture_field_type (type,
14342 DECLTYPE_FOR_INIT_CAPTURE (t),
14343 DECLTYPE_FOR_REF_CAPTURE (t));
14344 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14345 type = lambda_proxy_type (type);
14346 else
14347 {
14348 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14349 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14350 && EXPR_P (type))
14351 /* In a template ~id could be either a complement expression
14352 or an unqualified-id naming a destructor; if instantiating
14353 it produces an expression, it's not an id-expression or
14354 member access. */
14355 id = false;
14356 type = finish_decltype_type (type, id, complain);
14357 }
14358 return cp_build_qualified_type_real (type,
14359 cp_type_quals (t)
14360 | cp_type_quals (type),
14361 complain | tf_ignore_bad_quals);
14362 }
14363
14364 case UNDERLYING_TYPE:
14365 {
14366 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14367 complain, in_decl);
14368 return finish_underlying_type (type);
14369 }
14370
14371 case TYPE_ARGUMENT_PACK:
14372 case NONTYPE_ARGUMENT_PACK:
14373 {
14374 tree r;
14375
14376 if (code == NONTYPE_ARGUMENT_PACK)
14377 r = make_node (code);
14378 else
14379 r = cxx_make_type (code);
14380
14381 tree pack_args = ARGUMENT_PACK_ARGS (t);
14382 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14383 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14384
14385 return r;
14386 }
14387
14388 case VOID_CST:
14389 case INTEGER_CST:
14390 case REAL_CST:
14391 case STRING_CST:
14392 case PLUS_EXPR:
14393 case MINUS_EXPR:
14394 case NEGATE_EXPR:
14395 case NOP_EXPR:
14396 case INDIRECT_REF:
14397 case ADDR_EXPR:
14398 case CALL_EXPR:
14399 case ARRAY_REF:
14400 case SCOPE_REF:
14401 /* We should use one of the expression tsubsts for these codes. */
14402 gcc_unreachable ();
14403
14404 default:
14405 sorry ("use of %qs in template", get_tree_code_name (code));
14406 return error_mark_node;
14407 }
14408 }
14409
14410 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14411 expression on the left-hand side of the "." or "->" operator. A
14412 baselink indicates a function from a base class. Both the
14413 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14414 bases of the template class, rather than the instantiated class.
14415 In addition, lookups that were not ambiguous before may be
14416 ambiguous now. Therefore, we perform the lookup again. */
14417
14418 static tree
14419 tsubst_baselink (tree baselink, tree object_type,
14420 tree args, tsubst_flags_t complain, tree in_decl)
14421 {
14422 bool qualified = BASELINK_QUALIFIED_P (baselink);
14423
14424 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14425 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14426
14427 tree optype = BASELINK_OPTYPE (baselink);
14428 optype = tsubst (optype, args, complain, in_decl);
14429
14430 tree template_args = NULL_TREE;
14431 bool template_id_p = false;
14432 tree fns = BASELINK_FUNCTIONS (baselink);
14433 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14434 {
14435 template_id_p = true;
14436 template_args = TREE_OPERAND (fns, 1);
14437 fns = TREE_OPERAND (fns, 0);
14438 if (template_args)
14439 template_args = tsubst_template_args (template_args, args,
14440 complain, in_decl);
14441 }
14442
14443 tree name = OVL_NAME (fns);
14444 if (IDENTIFIER_CONV_OP_P (name))
14445 name = make_conv_op_name (optype);
14446
14447 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14448 if (!baselink)
14449 {
14450 if ((complain & tf_error) && constructor_name_p (name, qualifying_scope))
14451 error ("cannot call constructor %<%T::%D%> directly",
14452 qualifying_scope, name);
14453 return error_mark_node;
14454 }
14455
14456 /* If lookup found a single function, mark it as used at this point.
14457 (If it lookup found multiple functions the one selected later by
14458 overload resolution will be marked as used at that point.) */
14459 if (BASELINK_P (baselink))
14460 fns = BASELINK_FUNCTIONS (baselink);
14461 if (!template_id_p && !really_overloaded_fn (fns)
14462 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14463 return error_mark_node;
14464
14465 if (BASELINK_P (baselink))
14466 {
14467 /* Add back the template arguments, if present. */
14468 if (template_id_p)
14469 BASELINK_FUNCTIONS (baselink)
14470 = build2 (TEMPLATE_ID_EXPR, unknown_type_node,
14471 BASELINK_FUNCTIONS (baselink), template_args);
14472
14473 /* Update the conversion operator type. */
14474 BASELINK_OPTYPE (baselink) = optype;
14475 }
14476
14477 if (!object_type)
14478 object_type = current_class_type;
14479
14480 if (qualified || name == complete_dtor_identifier)
14481 {
14482 baselink = adjust_result_of_qualified_name_lookup (baselink,
14483 qualifying_scope,
14484 object_type);
14485 if (!qualified)
14486 /* We need to call adjust_result_of_qualified_name_lookup in case the
14487 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14488 so that we still get virtual function binding. */
14489 BASELINK_QUALIFIED_P (baselink) = false;
14490 }
14491
14492 return baselink;
14493 }
14494
14495 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14496 true if the qualified-id will be a postfix-expression in-and-of
14497 itself; false if more of the postfix-expression follows the
14498 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14499 of "&". */
14500
14501 static tree
14502 tsubst_qualified_id (tree qualified_id, tree args,
14503 tsubst_flags_t complain, tree in_decl,
14504 bool done, bool address_p)
14505 {
14506 tree expr;
14507 tree scope;
14508 tree name;
14509 bool is_template;
14510 tree template_args;
14511 location_t loc = UNKNOWN_LOCATION;
14512
14513 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14514
14515 /* Figure out what name to look up. */
14516 name = TREE_OPERAND (qualified_id, 1);
14517 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14518 {
14519 is_template = true;
14520 loc = EXPR_LOCATION (name);
14521 template_args = TREE_OPERAND (name, 1);
14522 if (template_args)
14523 template_args = tsubst_template_args (template_args, args,
14524 complain, in_decl);
14525 if (template_args == error_mark_node)
14526 return error_mark_node;
14527 name = TREE_OPERAND (name, 0);
14528 }
14529 else
14530 {
14531 is_template = false;
14532 template_args = NULL_TREE;
14533 }
14534
14535 /* Substitute into the qualifying scope. When there are no ARGS, we
14536 are just trying to simplify a non-dependent expression. In that
14537 case the qualifying scope may be dependent, and, in any case,
14538 substituting will not help. */
14539 scope = TREE_OPERAND (qualified_id, 0);
14540 if (args)
14541 {
14542 scope = tsubst (scope, args, complain, in_decl);
14543 expr = tsubst_copy (name, args, complain, in_decl);
14544 }
14545 else
14546 expr = name;
14547
14548 if (dependent_scope_p (scope))
14549 {
14550 if (is_template)
14551 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14552 tree r = build_qualified_name (NULL_TREE, scope, expr,
14553 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14554 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14555 return r;
14556 }
14557
14558 if (!BASELINK_P (name) && !DECL_P (expr))
14559 {
14560 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14561 {
14562 /* A BIT_NOT_EXPR is used to represent a destructor. */
14563 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14564 {
14565 error ("qualifying type %qT does not match destructor name ~%qT",
14566 scope, TREE_OPERAND (expr, 0));
14567 expr = error_mark_node;
14568 }
14569 else
14570 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14571 /*is_type_p=*/0, false);
14572 }
14573 else
14574 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14575 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14576 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14577 {
14578 if (complain & tf_error)
14579 {
14580 error ("dependent-name %qE is parsed as a non-type, but "
14581 "instantiation yields a type", qualified_id);
14582 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14583 }
14584 return error_mark_node;
14585 }
14586 }
14587
14588 if (DECL_P (expr))
14589 {
14590 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14591 scope);
14592 /* Remember that there was a reference to this entity. */
14593 if (!mark_used (expr, complain) && !(complain & tf_error))
14594 return error_mark_node;
14595 }
14596
14597 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14598 {
14599 if (complain & tf_error)
14600 qualified_name_lookup_error (scope,
14601 TREE_OPERAND (qualified_id, 1),
14602 expr, input_location);
14603 return error_mark_node;
14604 }
14605
14606 if (is_template)
14607 {
14608 if (variable_template_p (expr))
14609 expr = lookup_and_finish_template_variable (expr, template_args,
14610 complain);
14611 else
14612 expr = lookup_template_function (expr, template_args);
14613 }
14614
14615 if (expr == error_mark_node && complain & tf_error)
14616 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14617 expr, input_location);
14618 else if (TYPE_P (scope))
14619 {
14620 expr = (adjust_result_of_qualified_name_lookup
14621 (expr, scope, current_nonlambda_class_type ()));
14622 expr = (finish_qualified_id_expr
14623 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14624 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14625 /*template_arg_p=*/false, complain));
14626 }
14627
14628 /* Expressions do not generally have reference type. */
14629 if (TREE_CODE (expr) != SCOPE_REF
14630 /* However, if we're about to form a pointer-to-member, we just
14631 want the referenced member referenced. */
14632 && TREE_CODE (expr) != OFFSET_REF)
14633 expr = convert_from_reference (expr);
14634
14635 if (REF_PARENTHESIZED_P (qualified_id))
14636 expr = force_paren_expr (expr);
14637
14638 return expr;
14639 }
14640
14641 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14642 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14643 for tsubst. */
14644
14645 static tree
14646 tsubst_init (tree init, tree decl, tree args,
14647 tsubst_flags_t complain, tree in_decl)
14648 {
14649 if (!init)
14650 return NULL_TREE;
14651
14652 init = tsubst_expr (init, args, complain, in_decl, false);
14653
14654 if (!init && TREE_TYPE (decl) != error_mark_node)
14655 {
14656 /* If we had an initializer but it
14657 instantiated to nothing,
14658 value-initialize the object. This will
14659 only occur when the initializer was a
14660 pack expansion where the parameter packs
14661 used in that expansion were of length
14662 zero. */
14663 init = build_value_init (TREE_TYPE (decl),
14664 complain);
14665 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14666 init = get_target_expr_sfinae (init, complain);
14667 if (TREE_CODE (init) == TARGET_EXPR)
14668 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14669 }
14670
14671 return init;
14672 }
14673
14674 /* Like tsubst, but deals with expressions. This function just replaces
14675 template parms; to finish processing the resultant expression, use
14676 tsubst_copy_and_build or tsubst_expr. */
14677
14678 static tree
14679 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14680 {
14681 enum tree_code code;
14682 tree r;
14683
14684 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14685 return t;
14686
14687 code = TREE_CODE (t);
14688
14689 switch (code)
14690 {
14691 case PARM_DECL:
14692 r = retrieve_local_specialization (t);
14693
14694 if (r == NULL_TREE)
14695 {
14696 /* We get here for a use of 'this' in an NSDMI. */
14697 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14698 return current_class_ptr;
14699
14700 /* This can happen for a parameter name used later in a function
14701 declaration (such as in a late-specified return type). Just
14702 make a dummy decl, since it's only used for its type. */
14703 gcc_assert (cp_unevaluated_operand != 0);
14704 r = tsubst_decl (t, args, complain);
14705 /* Give it the template pattern as its context; its true context
14706 hasn't been instantiated yet and this is good enough for
14707 mangling. */
14708 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14709 }
14710
14711 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14712 r = ARGUMENT_PACK_SELECT_ARG (r);
14713 if (!mark_used (r, complain) && !(complain & tf_error))
14714 return error_mark_node;
14715 return r;
14716
14717 case CONST_DECL:
14718 {
14719 tree enum_type;
14720 tree v;
14721
14722 if (DECL_TEMPLATE_PARM_P (t))
14723 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14724 /* There is no need to substitute into namespace-scope
14725 enumerators. */
14726 if (DECL_NAMESPACE_SCOPE_P (t))
14727 return t;
14728 /* If ARGS is NULL, then T is known to be non-dependent. */
14729 if (args == NULL_TREE)
14730 return scalar_constant_value (t);
14731
14732 /* Unfortunately, we cannot just call lookup_name here.
14733 Consider:
14734
14735 template <int I> int f() {
14736 enum E { a = I };
14737 struct S { void g() { E e = a; } };
14738 };
14739
14740 When we instantiate f<7>::S::g(), say, lookup_name is not
14741 clever enough to find f<7>::a. */
14742 enum_type
14743 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14744 /*entering_scope=*/0);
14745
14746 for (v = TYPE_VALUES (enum_type);
14747 v != NULL_TREE;
14748 v = TREE_CHAIN (v))
14749 if (TREE_PURPOSE (v) == DECL_NAME (t))
14750 return TREE_VALUE (v);
14751
14752 /* We didn't find the name. That should never happen; if
14753 name-lookup found it during preliminary parsing, we
14754 should find it again here during instantiation. */
14755 gcc_unreachable ();
14756 }
14757 return t;
14758
14759 case FIELD_DECL:
14760 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14761 {
14762 /* Check for a local specialization set up by
14763 tsubst_pack_expansion. */
14764 if (tree r = retrieve_local_specialization (t))
14765 {
14766 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14767 r = ARGUMENT_PACK_SELECT_ARG (r);
14768 return r;
14769 }
14770
14771 /* When retrieving a capture pack from a generic lambda, remove the
14772 lambda call op's own template argument list from ARGS. Only the
14773 template arguments active for the closure type should be used to
14774 retrieve the pack specialization. */
14775 if (LAMBDA_FUNCTION_P (current_function_decl)
14776 && (template_class_depth (DECL_CONTEXT (t))
14777 != TMPL_ARGS_DEPTH (args)))
14778 args = strip_innermost_template_args (args, 1);
14779
14780 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14781 tsubst_decl put in the hash table. */
14782 return retrieve_specialization (t, args, 0);
14783 }
14784
14785 if (DECL_CONTEXT (t))
14786 {
14787 tree ctx;
14788
14789 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14790 /*entering_scope=*/1);
14791 if (ctx != DECL_CONTEXT (t))
14792 {
14793 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14794 if (!r)
14795 {
14796 if (complain & tf_error)
14797 error ("using invalid field %qD", t);
14798 return error_mark_node;
14799 }
14800 return r;
14801 }
14802 }
14803
14804 return t;
14805
14806 case VAR_DECL:
14807 case FUNCTION_DECL:
14808 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14809 r = tsubst (t, args, complain, in_decl);
14810 else if (local_variable_p (t)
14811 && uses_template_parms (DECL_CONTEXT (t)))
14812 {
14813 r = retrieve_local_specialization (t);
14814 if (r == NULL_TREE)
14815 {
14816 /* First try name lookup to find the instantiation. */
14817 r = lookup_name (DECL_NAME (t));
14818 if (r && !is_capture_proxy (r))
14819 {
14820 /* Make sure that the one we found is the one we want. */
14821 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
14822 if (ctx != DECL_CONTEXT (r))
14823 r = NULL_TREE;
14824 }
14825
14826 if (r)
14827 /* OK */;
14828 else
14829 {
14830 /* This can happen for a variable used in a
14831 late-specified return type of a local lambda, or for a
14832 local static or constant. Building a new VAR_DECL
14833 should be OK in all those cases. */
14834 r = tsubst_decl (t, args, complain);
14835 if (local_specializations)
14836 /* Avoid infinite recursion (79640). */
14837 register_local_specialization (r, t);
14838 if (decl_maybe_constant_var_p (r))
14839 {
14840 /* We can't call cp_finish_decl, so handle the
14841 initializer by hand. */
14842 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14843 complain, in_decl);
14844 if (!processing_template_decl)
14845 init = maybe_constant_init (init);
14846 if (processing_template_decl
14847 ? potential_constant_expression (init)
14848 : reduced_constant_expression_p (init))
14849 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14850 = TREE_CONSTANT (r) = true;
14851 DECL_INITIAL (r) = init;
14852 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14853 TREE_TYPE (r)
14854 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14855 complain, adc_variable_type);
14856 }
14857 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14858 || decl_constant_var_p (r)
14859 || errorcount || sorrycount);
14860 if (!processing_template_decl
14861 && !TREE_STATIC (r))
14862 r = process_outer_var_ref (r, complain);
14863 }
14864 /* Remember this for subsequent uses. */
14865 if (local_specializations)
14866 register_local_specialization (r, t);
14867 }
14868 }
14869 else
14870 r = t;
14871 if (!mark_used (r, complain))
14872 return error_mark_node;
14873 return r;
14874
14875 case NAMESPACE_DECL:
14876 return t;
14877
14878 case OVERLOAD:
14879 /* An OVERLOAD will always be a non-dependent overload set; an
14880 overload set from function scope will just be represented with an
14881 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14882 gcc_assert (!uses_template_parms (t));
14883 /* We must have marked any lookups as persistent. */
14884 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14885 return t;
14886
14887 case BASELINK:
14888 return tsubst_baselink (t, current_nonlambda_class_type (),
14889 args, complain, in_decl);
14890
14891 case TEMPLATE_DECL:
14892 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14893 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14894 args, complain, in_decl);
14895 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14896 return tsubst (t, args, complain, in_decl);
14897 else if (DECL_CLASS_SCOPE_P (t)
14898 && uses_template_parms (DECL_CONTEXT (t)))
14899 {
14900 /* Template template argument like the following example need
14901 special treatment:
14902
14903 template <template <class> class TT> struct C {};
14904 template <class T> struct D {
14905 template <class U> struct E {};
14906 C<E> c; // #1
14907 };
14908 D<int> d; // #2
14909
14910 We are processing the template argument `E' in #1 for
14911 the template instantiation #2. Originally, `E' is a
14912 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14913 have to substitute this with one having context `D<int>'. */
14914
14915 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14916 if (dependent_scope_p (context))
14917 {
14918 /* When rewriting a constructor into a deduction guide, a
14919 non-dependent name can become dependent, so memtmpl<args>
14920 becomes context::template memtmpl<args>. */
14921 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14922 return build_qualified_name (type, context, DECL_NAME (t),
14923 /*template*/true);
14924 }
14925 return lookup_field (context, DECL_NAME(t), 0, false);
14926 }
14927 else
14928 /* Ordinary template template argument. */
14929 return t;
14930
14931 case CAST_EXPR:
14932 case REINTERPRET_CAST_EXPR:
14933 case CONST_CAST_EXPR:
14934 case STATIC_CAST_EXPR:
14935 case DYNAMIC_CAST_EXPR:
14936 case IMPLICIT_CONV_EXPR:
14937 case CONVERT_EXPR:
14938 case NOP_EXPR:
14939 {
14940 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14941 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14942 return build1 (code, type, op0);
14943 }
14944
14945 case SIZEOF_EXPR:
14946 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14947 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14948 {
14949 tree expanded, op = TREE_OPERAND (t, 0);
14950 int len = 0;
14951
14952 if (SIZEOF_EXPR_TYPE_P (t))
14953 op = TREE_TYPE (op);
14954
14955 ++cp_unevaluated_operand;
14956 ++c_inhibit_evaluation_warnings;
14957 /* We only want to compute the number of arguments. */
14958 if (PACK_EXPANSION_P (op))
14959 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14960 else
14961 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14962 args, complain, in_decl);
14963 --cp_unevaluated_operand;
14964 --c_inhibit_evaluation_warnings;
14965
14966 if (TREE_CODE (expanded) == TREE_VEC)
14967 {
14968 len = TREE_VEC_LENGTH (expanded);
14969 /* Set TREE_USED for the benefit of -Wunused. */
14970 for (int i = 0; i < len; i++)
14971 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14972 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14973 }
14974
14975 if (expanded == error_mark_node)
14976 return error_mark_node;
14977 else if (PACK_EXPANSION_P (expanded)
14978 || (TREE_CODE (expanded) == TREE_VEC
14979 && pack_expansion_args_count (expanded)))
14980
14981 {
14982 if (PACK_EXPANSION_P (expanded))
14983 /* OK. */;
14984 else if (TREE_VEC_LENGTH (expanded) == 1)
14985 expanded = TREE_VEC_ELT (expanded, 0);
14986 else
14987 expanded = make_argument_pack (expanded);
14988
14989 if (TYPE_P (expanded))
14990 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14991 complain & tf_error);
14992 else
14993 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14994 complain & tf_error);
14995 }
14996 else
14997 return build_int_cst (size_type_node, len);
14998 }
14999 if (SIZEOF_EXPR_TYPE_P (t))
15000 {
15001 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15002 args, complain, in_decl);
15003 r = build1 (NOP_EXPR, r, error_mark_node);
15004 r = build1 (SIZEOF_EXPR,
15005 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15006 SIZEOF_EXPR_TYPE_P (r) = 1;
15007 return r;
15008 }
15009 /* Fall through */
15010
15011 case INDIRECT_REF:
15012 case NEGATE_EXPR:
15013 case TRUTH_NOT_EXPR:
15014 case BIT_NOT_EXPR:
15015 case ADDR_EXPR:
15016 case UNARY_PLUS_EXPR: /* Unary + */
15017 case ALIGNOF_EXPR:
15018 case AT_ENCODE_EXPR:
15019 case ARROW_EXPR:
15020 case THROW_EXPR:
15021 case TYPEID_EXPR:
15022 case REALPART_EXPR:
15023 case IMAGPART_EXPR:
15024 case PAREN_EXPR:
15025 {
15026 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15027 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15028 return build1 (code, type, op0);
15029 }
15030
15031 case COMPONENT_REF:
15032 {
15033 tree object;
15034 tree name;
15035
15036 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15037 name = TREE_OPERAND (t, 1);
15038 if (TREE_CODE (name) == BIT_NOT_EXPR)
15039 {
15040 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15041 complain, in_decl);
15042 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15043 }
15044 else if (TREE_CODE (name) == SCOPE_REF
15045 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15046 {
15047 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15048 complain, in_decl);
15049 name = TREE_OPERAND (name, 1);
15050 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15051 complain, in_decl);
15052 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15053 name = build_qualified_name (/*type=*/NULL_TREE,
15054 base, name,
15055 /*template_p=*/false);
15056 }
15057 else if (BASELINK_P (name))
15058 name = tsubst_baselink (name,
15059 non_reference (TREE_TYPE (object)),
15060 args, complain,
15061 in_decl);
15062 else
15063 name = tsubst_copy (name, args, complain, in_decl);
15064 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15065 }
15066
15067 case PLUS_EXPR:
15068 case MINUS_EXPR:
15069 case MULT_EXPR:
15070 case TRUNC_DIV_EXPR:
15071 case CEIL_DIV_EXPR:
15072 case FLOOR_DIV_EXPR:
15073 case ROUND_DIV_EXPR:
15074 case EXACT_DIV_EXPR:
15075 case BIT_AND_EXPR:
15076 case BIT_IOR_EXPR:
15077 case BIT_XOR_EXPR:
15078 case TRUNC_MOD_EXPR:
15079 case FLOOR_MOD_EXPR:
15080 case TRUTH_ANDIF_EXPR:
15081 case TRUTH_ORIF_EXPR:
15082 case TRUTH_AND_EXPR:
15083 case TRUTH_OR_EXPR:
15084 case RSHIFT_EXPR:
15085 case LSHIFT_EXPR:
15086 case RROTATE_EXPR:
15087 case LROTATE_EXPR:
15088 case EQ_EXPR:
15089 case NE_EXPR:
15090 case MAX_EXPR:
15091 case MIN_EXPR:
15092 case LE_EXPR:
15093 case GE_EXPR:
15094 case LT_EXPR:
15095 case GT_EXPR:
15096 case COMPOUND_EXPR:
15097 case DOTSTAR_EXPR:
15098 case MEMBER_REF:
15099 case PREDECREMENT_EXPR:
15100 case PREINCREMENT_EXPR:
15101 case POSTDECREMENT_EXPR:
15102 case POSTINCREMENT_EXPR:
15103 {
15104 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15105 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15106 return build_nt (code, op0, op1);
15107 }
15108
15109 case SCOPE_REF:
15110 {
15111 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15112 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15113 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15114 QUALIFIED_NAME_IS_TEMPLATE (t));
15115 }
15116
15117 case ARRAY_REF:
15118 {
15119 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15120 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15121 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15122 }
15123
15124 case CALL_EXPR:
15125 {
15126 int n = VL_EXP_OPERAND_LENGTH (t);
15127 tree result = build_vl_exp (CALL_EXPR, n);
15128 int i;
15129 for (i = 0; i < n; i++)
15130 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15131 complain, in_decl);
15132 return result;
15133 }
15134
15135 case COND_EXPR:
15136 case MODOP_EXPR:
15137 case PSEUDO_DTOR_EXPR:
15138 case VEC_PERM_EXPR:
15139 {
15140 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15141 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15142 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15143 r = build_nt (code, op0, op1, op2);
15144 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15145 return r;
15146 }
15147
15148 case NEW_EXPR:
15149 {
15150 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15151 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15152 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15153 r = build_nt (code, op0, op1, op2);
15154 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15155 return r;
15156 }
15157
15158 case DELETE_EXPR:
15159 {
15160 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15161 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15162 r = build_nt (code, op0, op1);
15163 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15164 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15165 return r;
15166 }
15167
15168 case TEMPLATE_ID_EXPR:
15169 {
15170 /* Substituted template arguments */
15171 tree fn = TREE_OPERAND (t, 0);
15172 tree targs = TREE_OPERAND (t, 1);
15173
15174 fn = tsubst_copy (fn, args, complain, in_decl);
15175 if (targs)
15176 targs = tsubst_template_args (targs, args, complain, in_decl);
15177
15178 return lookup_template_function (fn, targs);
15179 }
15180
15181 case TREE_LIST:
15182 {
15183 tree purpose, value, chain;
15184
15185 if (t == void_list_node)
15186 return t;
15187
15188 purpose = TREE_PURPOSE (t);
15189 if (purpose)
15190 purpose = tsubst_copy (purpose, args, complain, in_decl);
15191 value = TREE_VALUE (t);
15192 if (value)
15193 value = tsubst_copy (value, args, complain, in_decl);
15194 chain = TREE_CHAIN (t);
15195 if (chain && chain != void_type_node)
15196 chain = tsubst_copy (chain, args, complain, in_decl);
15197 if (purpose == TREE_PURPOSE (t)
15198 && value == TREE_VALUE (t)
15199 && chain == TREE_CHAIN (t))
15200 return t;
15201 return tree_cons (purpose, value, chain);
15202 }
15203
15204 case RECORD_TYPE:
15205 case UNION_TYPE:
15206 case ENUMERAL_TYPE:
15207 case INTEGER_TYPE:
15208 case TEMPLATE_TYPE_PARM:
15209 case TEMPLATE_TEMPLATE_PARM:
15210 case BOUND_TEMPLATE_TEMPLATE_PARM:
15211 case TEMPLATE_PARM_INDEX:
15212 case POINTER_TYPE:
15213 case REFERENCE_TYPE:
15214 case OFFSET_TYPE:
15215 case FUNCTION_TYPE:
15216 case METHOD_TYPE:
15217 case ARRAY_TYPE:
15218 case TYPENAME_TYPE:
15219 case UNBOUND_CLASS_TEMPLATE:
15220 case TYPEOF_TYPE:
15221 case DECLTYPE_TYPE:
15222 case TYPE_DECL:
15223 return tsubst (t, args, complain, in_decl);
15224
15225 case USING_DECL:
15226 t = DECL_NAME (t);
15227 /* Fall through. */
15228 case IDENTIFIER_NODE:
15229 if (IDENTIFIER_CONV_OP_P (t))
15230 {
15231 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15232 return make_conv_op_name (new_type);
15233 }
15234 else
15235 return t;
15236
15237 case CONSTRUCTOR:
15238 /* This is handled by tsubst_copy_and_build. */
15239 gcc_unreachable ();
15240
15241 case VA_ARG_EXPR:
15242 {
15243 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15244 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15245 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15246 }
15247
15248 case CLEANUP_POINT_EXPR:
15249 /* We shouldn't have built any of these during initial template
15250 generation. Instead, they should be built during instantiation
15251 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15252 gcc_unreachable ();
15253
15254 case OFFSET_REF:
15255 {
15256 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15257 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15258 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15259 r = build2 (code, type, op0, op1);
15260 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15261 if (!mark_used (TREE_OPERAND (r, 1), complain)
15262 && !(complain & tf_error))
15263 return error_mark_node;
15264 return r;
15265 }
15266
15267 case EXPR_PACK_EXPANSION:
15268 error ("invalid use of pack expansion expression");
15269 return error_mark_node;
15270
15271 case NONTYPE_ARGUMENT_PACK:
15272 error ("use %<...%> to expand argument pack");
15273 return error_mark_node;
15274
15275 case VOID_CST:
15276 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15277 return t;
15278
15279 case INTEGER_CST:
15280 case REAL_CST:
15281 case STRING_CST:
15282 case COMPLEX_CST:
15283 {
15284 /* Instantiate any typedefs in the type. */
15285 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15286 r = fold_convert (type, t);
15287 gcc_assert (TREE_CODE (r) == code);
15288 return r;
15289 }
15290
15291 case PTRMEM_CST:
15292 /* These can sometimes show up in a partial instantiation, but never
15293 involve template parms. */
15294 gcc_assert (!uses_template_parms (t));
15295 return t;
15296
15297 case UNARY_LEFT_FOLD_EXPR:
15298 return tsubst_unary_left_fold (t, args, complain, in_decl);
15299 case UNARY_RIGHT_FOLD_EXPR:
15300 return tsubst_unary_right_fold (t, args, complain, in_decl);
15301 case BINARY_LEFT_FOLD_EXPR:
15302 return tsubst_binary_left_fold (t, args, complain, in_decl);
15303 case BINARY_RIGHT_FOLD_EXPR:
15304 return tsubst_binary_right_fold (t, args, complain, in_decl);
15305 case PREDICT_EXPR:
15306 return t;
15307
15308 default:
15309 /* We shouldn't get here, but keep going if !flag_checking. */
15310 if (flag_checking)
15311 gcc_unreachable ();
15312 return t;
15313 }
15314 }
15315
15316 /* Helper function for tsubst_omp_clauses, used for instantiation of
15317 OMP_CLAUSE_DECL of clauses. */
15318
15319 static tree
15320 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15321 tree in_decl)
15322 {
15323 if (decl == NULL_TREE)
15324 return NULL_TREE;
15325
15326 /* Handle an OpenMP array section represented as a TREE_LIST (or
15327 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15328 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15329 TREE_LIST. We can handle it exactly the same as an array section
15330 (purpose, value, and a chain), even though the nomenclature
15331 (low_bound, length, etc) is different. */
15332 if (TREE_CODE (decl) == TREE_LIST)
15333 {
15334 tree low_bound
15335 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15336 /*integral_constant_expression_p=*/false);
15337 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15338 /*integral_constant_expression_p=*/false);
15339 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15340 in_decl);
15341 if (TREE_PURPOSE (decl) == low_bound
15342 && TREE_VALUE (decl) == length
15343 && TREE_CHAIN (decl) == chain)
15344 return decl;
15345 tree ret = tree_cons (low_bound, length, chain);
15346 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15347 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15348 return ret;
15349 }
15350 tree ret = tsubst_expr (decl, args, complain, in_decl,
15351 /*integral_constant_expression_p=*/false);
15352 /* Undo convert_from_reference tsubst_expr could have called. */
15353 if (decl
15354 && REFERENCE_REF_P (ret)
15355 && !REFERENCE_REF_P (decl))
15356 ret = TREE_OPERAND (ret, 0);
15357 return ret;
15358 }
15359
15360 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15361
15362 static tree
15363 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15364 tree args, tsubst_flags_t complain, tree in_decl)
15365 {
15366 tree new_clauses = NULL_TREE, nc, oc;
15367 tree linear_no_step = NULL_TREE;
15368
15369 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15370 {
15371 nc = copy_node (oc);
15372 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15373 new_clauses = nc;
15374
15375 switch (OMP_CLAUSE_CODE (nc))
15376 {
15377 case OMP_CLAUSE_LASTPRIVATE:
15378 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15379 {
15380 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15381 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15382 in_decl, /*integral_constant_expression_p=*/false);
15383 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15384 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15385 }
15386 /* FALLTHRU */
15387 case OMP_CLAUSE_PRIVATE:
15388 case OMP_CLAUSE_SHARED:
15389 case OMP_CLAUSE_FIRSTPRIVATE:
15390 case OMP_CLAUSE_COPYIN:
15391 case OMP_CLAUSE_COPYPRIVATE:
15392 case OMP_CLAUSE_UNIFORM:
15393 case OMP_CLAUSE_DEPEND:
15394 case OMP_CLAUSE_FROM:
15395 case OMP_CLAUSE_TO:
15396 case OMP_CLAUSE_MAP:
15397 case OMP_CLAUSE_USE_DEVICE_PTR:
15398 case OMP_CLAUSE_IS_DEVICE_PTR:
15399 OMP_CLAUSE_DECL (nc)
15400 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15401 in_decl);
15402 break;
15403 case OMP_CLAUSE_TILE:
15404 case OMP_CLAUSE_IF:
15405 case OMP_CLAUSE_NUM_THREADS:
15406 case OMP_CLAUSE_SCHEDULE:
15407 case OMP_CLAUSE_COLLAPSE:
15408 case OMP_CLAUSE_FINAL:
15409 case OMP_CLAUSE_DEVICE:
15410 case OMP_CLAUSE_DIST_SCHEDULE:
15411 case OMP_CLAUSE_NUM_TEAMS:
15412 case OMP_CLAUSE_THREAD_LIMIT:
15413 case OMP_CLAUSE_SAFELEN:
15414 case OMP_CLAUSE_SIMDLEN:
15415 case OMP_CLAUSE_NUM_TASKS:
15416 case OMP_CLAUSE_GRAINSIZE:
15417 case OMP_CLAUSE_PRIORITY:
15418 case OMP_CLAUSE_ORDERED:
15419 case OMP_CLAUSE_HINT:
15420 case OMP_CLAUSE_NUM_GANGS:
15421 case OMP_CLAUSE_NUM_WORKERS:
15422 case OMP_CLAUSE_VECTOR_LENGTH:
15423 case OMP_CLAUSE_WORKER:
15424 case OMP_CLAUSE_VECTOR:
15425 case OMP_CLAUSE_ASYNC:
15426 case OMP_CLAUSE_WAIT:
15427 OMP_CLAUSE_OPERAND (nc, 0)
15428 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15429 in_decl, /*integral_constant_expression_p=*/false);
15430 break;
15431 case OMP_CLAUSE_REDUCTION:
15432 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15433 {
15434 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15435 if (TREE_CODE (placeholder) == SCOPE_REF)
15436 {
15437 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15438 complain, in_decl);
15439 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15440 = build_qualified_name (NULL_TREE, scope,
15441 TREE_OPERAND (placeholder, 1),
15442 false);
15443 }
15444 else
15445 gcc_assert (identifier_p (placeholder));
15446 }
15447 OMP_CLAUSE_DECL (nc)
15448 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15449 in_decl);
15450 break;
15451 case OMP_CLAUSE_GANG:
15452 case OMP_CLAUSE_ALIGNED:
15453 OMP_CLAUSE_DECL (nc)
15454 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15455 in_decl);
15456 OMP_CLAUSE_OPERAND (nc, 1)
15457 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15458 in_decl, /*integral_constant_expression_p=*/false);
15459 break;
15460 case OMP_CLAUSE_LINEAR:
15461 OMP_CLAUSE_DECL (nc)
15462 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15463 in_decl);
15464 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15465 {
15466 gcc_assert (!linear_no_step);
15467 linear_no_step = nc;
15468 }
15469 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15470 OMP_CLAUSE_LINEAR_STEP (nc)
15471 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15472 complain, in_decl);
15473 else
15474 OMP_CLAUSE_LINEAR_STEP (nc)
15475 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15476 in_decl,
15477 /*integral_constant_expression_p=*/false);
15478 break;
15479 case OMP_CLAUSE_NOWAIT:
15480 case OMP_CLAUSE_DEFAULT:
15481 case OMP_CLAUSE_UNTIED:
15482 case OMP_CLAUSE_MERGEABLE:
15483 case OMP_CLAUSE_INBRANCH:
15484 case OMP_CLAUSE_NOTINBRANCH:
15485 case OMP_CLAUSE_PROC_BIND:
15486 case OMP_CLAUSE_FOR:
15487 case OMP_CLAUSE_PARALLEL:
15488 case OMP_CLAUSE_SECTIONS:
15489 case OMP_CLAUSE_TASKGROUP:
15490 case OMP_CLAUSE_NOGROUP:
15491 case OMP_CLAUSE_THREADS:
15492 case OMP_CLAUSE_SIMD:
15493 case OMP_CLAUSE_DEFAULTMAP:
15494 case OMP_CLAUSE_INDEPENDENT:
15495 case OMP_CLAUSE_AUTO:
15496 case OMP_CLAUSE_SEQ:
15497 break;
15498 default:
15499 gcc_unreachable ();
15500 }
15501 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15502 switch (OMP_CLAUSE_CODE (nc))
15503 {
15504 case OMP_CLAUSE_SHARED:
15505 case OMP_CLAUSE_PRIVATE:
15506 case OMP_CLAUSE_FIRSTPRIVATE:
15507 case OMP_CLAUSE_LASTPRIVATE:
15508 case OMP_CLAUSE_COPYPRIVATE:
15509 case OMP_CLAUSE_LINEAR:
15510 case OMP_CLAUSE_REDUCTION:
15511 case OMP_CLAUSE_USE_DEVICE_PTR:
15512 case OMP_CLAUSE_IS_DEVICE_PTR:
15513 /* tsubst_expr on SCOPE_REF results in returning
15514 finish_non_static_data_member result. Undo that here. */
15515 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15516 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15517 == IDENTIFIER_NODE))
15518 {
15519 tree t = OMP_CLAUSE_DECL (nc);
15520 tree v = t;
15521 while (v)
15522 switch (TREE_CODE (v))
15523 {
15524 case COMPONENT_REF:
15525 case MEM_REF:
15526 case INDIRECT_REF:
15527 CASE_CONVERT:
15528 case POINTER_PLUS_EXPR:
15529 v = TREE_OPERAND (v, 0);
15530 continue;
15531 case PARM_DECL:
15532 if (DECL_CONTEXT (v) == current_function_decl
15533 && DECL_ARTIFICIAL (v)
15534 && DECL_NAME (v) == this_identifier)
15535 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15536 /* FALLTHRU */
15537 default:
15538 v = NULL_TREE;
15539 break;
15540 }
15541 }
15542 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15543 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15544 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15545 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15546 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15547 {
15548 tree decl = OMP_CLAUSE_DECL (nc);
15549 if (VAR_P (decl))
15550 {
15551 retrofit_lang_decl (decl);
15552 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15553 }
15554 }
15555 break;
15556 default:
15557 break;
15558 }
15559 }
15560
15561 new_clauses = nreverse (new_clauses);
15562 if (ort != C_ORT_OMP_DECLARE_SIMD)
15563 {
15564 new_clauses = finish_omp_clauses (new_clauses, ort);
15565 if (linear_no_step)
15566 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15567 if (nc == linear_no_step)
15568 {
15569 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15570 break;
15571 }
15572 }
15573 return new_clauses;
15574 }
15575
15576 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15577
15578 static tree
15579 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15580 tree in_decl)
15581 {
15582 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15583
15584 tree purpose, value, chain;
15585
15586 if (t == NULL)
15587 return t;
15588
15589 if (TREE_CODE (t) != TREE_LIST)
15590 return tsubst_copy_and_build (t, args, complain, in_decl,
15591 /*function_p=*/false,
15592 /*integral_constant_expression_p=*/false);
15593
15594 if (t == void_list_node)
15595 return t;
15596
15597 purpose = TREE_PURPOSE (t);
15598 if (purpose)
15599 purpose = RECUR (purpose);
15600 value = TREE_VALUE (t);
15601 if (value)
15602 {
15603 if (TREE_CODE (value) != LABEL_DECL)
15604 value = RECUR (value);
15605 else
15606 {
15607 value = lookup_label (DECL_NAME (value));
15608 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15609 TREE_USED (value) = 1;
15610 }
15611 }
15612 chain = TREE_CHAIN (t);
15613 if (chain && chain != void_type_node)
15614 chain = RECUR (chain);
15615 return tree_cons (purpose, value, chain);
15616 #undef RECUR
15617 }
15618
15619 /* Used to temporarily communicate the list of #pragma omp parallel
15620 clauses to #pragma omp for instantiation if they are combined
15621 together. */
15622
15623 static tree *omp_parallel_combined_clauses;
15624
15625 /* Substitute one OMP_FOR iterator. */
15626
15627 static void
15628 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15629 tree initv, tree condv, tree incrv, tree *clauses,
15630 tree args, tsubst_flags_t complain, tree in_decl,
15631 bool integral_constant_expression_p)
15632 {
15633 #define RECUR(NODE) \
15634 tsubst_expr ((NODE), args, complain, in_decl, \
15635 integral_constant_expression_p)
15636 tree decl, init, cond, incr;
15637
15638 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15639 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15640
15641 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15642 {
15643 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15644 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15645 }
15646
15647 decl = TREE_OPERAND (init, 0);
15648 init = TREE_OPERAND (init, 1);
15649 tree decl_expr = NULL_TREE;
15650 if (init && TREE_CODE (init) == DECL_EXPR)
15651 {
15652 /* We need to jump through some hoops to handle declarations in the
15653 init-statement, since we might need to handle auto deduction,
15654 but we need to keep control of initialization. */
15655 decl_expr = init;
15656 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15657 decl = tsubst_decl (decl, args, complain);
15658 }
15659 else
15660 {
15661 if (TREE_CODE (decl) == SCOPE_REF)
15662 {
15663 decl = RECUR (decl);
15664 if (TREE_CODE (decl) == COMPONENT_REF)
15665 {
15666 tree v = decl;
15667 while (v)
15668 switch (TREE_CODE (v))
15669 {
15670 case COMPONENT_REF:
15671 case MEM_REF:
15672 case INDIRECT_REF:
15673 CASE_CONVERT:
15674 case POINTER_PLUS_EXPR:
15675 v = TREE_OPERAND (v, 0);
15676 continue;
15677 case PARM_DECL:
15678 if (DECL_CONTEXT (v) == current_function_decl
15679 && DECL_ARTIFICIAL (v)
15680 && DECL_NAME (v) == this_identifier)
15681 {
15682 decl = TREE_OPERAND (decl, 1);
15683 decl = omp_privatize_field (decl, false);
15684 }
15685 /* FALLTHRU */
15686 default:
15687 v = NULL_TREE;
15688 break;
15689 }
15690 }
15691 }
15692 else
15693 decl = RECUR (decl);
15694 }
15695 init = RECUR (init);
15696
15697 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15698 if (auto_node && init)
15699 TREE_TYPE (decl)
15700 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15701
15702 gcc_assert (!type_dependent_expression_p (decl));
15703
15704 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15705 {
15706 if (decl_expr)
15707 {
15708 /* Declare the variable, but don't let that initialize it. */
15709 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15710 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15711 RECUR (decl_expr);
15712 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15713 }
15714
15715 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15716 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15717 if (TREE_CODE (incr) == MODIFY_EXPR)
15718 {
15719 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15720 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15721 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15722 NOP_EXPR, rhs, complain);
15723 }
15724 else
15725 incr = RECUR (incr);
15726 TREE_VEC_ELT (declv, i) = decl;
15727 TREE_VEC_ELT (initv, i) = init;
15728 TREE_VEC_ELT (condv, i) = cond;
15729 TREE_VEC_ELT (incrv, i) = incr;
15730 return;
15731 }
15732
15733 if (decl_expr)
15734 {
15735 /* Declare and initialize the variable. */
15736 RECUR (decl_expr);
15737 init = NULL_TREE;
15738 }
15739 else if (init)
15740 {
15741 tree *pc;
15742 int j;
15743 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15744 {
15745 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15746 {
15747 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15748 && OMP_CLAUSE_DECL (*pc) == decl)
15749 break;
15750 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15751 && OMP_CLAUSE_DECL (*pc) == decl)
15752 {
15753 if (j)
15754 break;
15755 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15756 tree c = *pc;
15757 *pc = OMP_CLAUSE_CHAIN (c);
15758 OMP_CLAUSE_CHAIN (c) = *clauses;
15759 *clauses = c;
15760 }
15761 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15762 && OMP_CLAUSE_DECL (*pc) == decl)
15763 {
15764 error ("iteration variable %qD should not be firstprivate",
15765 decl);
15766 *pc = OMP_CLAUSE_CHAIN (*pc);
15767 }
15768 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15769 && OMP_CLAUSE_DECL (*pc) == decl)
15770 {
15771 error ("iteration variable %qD should not be reduction",
15772 decl);
15773 *pc = OMP_CLAUSE_CHAIN (*pc);
15774 }
15775 else
15776 pc = &OMP_CLAUSE_CHAIN (*pc);
15777 }
15778 if (*pc)
15779 break;
15780 }
15781 if (*pc == NULL_TREE)
15782 {
15783 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15784 OMP_CLAUSE_DECL (c) = decl;
15785 c = finish_omp_clauses (c, C_ORT_OMP);
15786 if (c)
15787 {
15788 OMP_CLAUSE_CHAIN (c) = *clauses;
15789 *clauses = c;
15790 }
15791 }
15792 }
15793 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15794 if (COMPARISON_CLASS_P (cond))
15795 {
15796 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15797 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15798 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15799 }
15800 else
15801 cond = RECUR (cond);
15802 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15803 switch (TREE_CODE (incr))
15804 {
15805 case PREINCREMENT_EXPR:
15806 case PREDECREMENT_EXPR:
15807 case POSTINCREMENT_EXPR:
15808 case POSTDECREMENT_EXPR:
15809 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15810 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15811 break;
15812 case MODIFY_EXPR:
15813 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15814 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15815 {
15816 tree rhs = TREE_OPERAND (incr, 1);
15817 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15818 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15819 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15820 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15821 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15822 rhs0, rhs1));
15823 }
15824 else
15825 incr = RECUR (incr);
15826 break;
15827 case MODOP_EXPR:
15828 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15829 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15830 {
15831 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15832 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15833 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15834 TREE_TYPE (decl), lhs,
15835 RECUR (TREE_OPERAND (incr, 2))));
15836 }
15837 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15838 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15839 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15840 {
15841 tree rhs = TREE_OPERAND (incr, 2);
15842 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15843 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15844 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15845 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15846 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15847 rhs0, rhs1));
15848 }
15849 else
15850 incr = RECUR (incr);
15851 break;
15852 default:
15853 incr = RECUR (incr);
15854 break;
15855 }
15856
15857 TREE_VEC_ELT (declv, i) = decl;
15858 TREE_VEC_ELT (initv, i) = init;
15859 TREE_VEC_ELT (condv, i) = cond;
15860 TREE_VEC_ELT (incrv, i) = incr;
15861 #undef RECUR
15862 }
15863
15864 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15865 of OMP_TARGET's body. */
15866
15867 static tree
15868 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15869 {
15870 *walk_subtrees = 0;
15871 switch (TREE_CODE (*tp))
15872 {
15873 case OMP_TEAMS:
15874 return *tp;
15875 case BIND_EXPR:
15876 case STATEMENT_LIST:
15877 *walk_subtrees = 1;
15878 break;
15879 default:
15880 break;
15881 }
15882 return NULL_TREE;
15883 }
15884
15885 /* Helper function for tsubst_expr. For decomposition declaration
15886 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15887 also the corresponding decls representing the identifiers
15888 of the decomposition declaration. Return DECL if successful
15889 or error_mark_node otherwise, set *FIRST to the first decl
15890 in the list chained through DECL_CHAIN and *CNT to the number
15891 of such decls. */
15892
15893 static tree
15894 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15895 tsubst_flags_t complain, tree in_decl, tree *first,
15896 unsigned int *cnt)
15897 {
15898 tree decl2, decl3, prev = decl;
15899 *cnt = 0;
15900 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15901 for (decl2 = DECL_CHAIN (pattern_decl);
15902 decl2
15903 && VAR_P (decl2)
15904 && DECL_DECOMPOSITION_P (decl2)
15905 && DECL_NAME (decl2);
15906 decl2 = DECL_CHAIN (decl2))
15907 {
15908 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15909 {
15910 gcc_assert (errorcount);
15911 return error_mark_node;
15912 }
15913 (*cnt)++;
15914 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15915 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15916 tree v = DECL_VALUE_EXPR (decl2);
15917 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15918 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15919 decl3 = tsubst (decl2, args, complain, in_decl);
15920 SET_DECL_VALUE_EXPR (decl2, v);
15921 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15922 if (VAR_P (decl3))
15923 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15924 maybe_push_decl (decl3);
15925 if (error_operand_p (decl3))
15926 decl = error_mark_node;
15927 else if (decl != error_mark_node
15928 && DECL_CHAIN (decl3) != prev)
15929 {
15930 gcc_assert (errorcount);
15931 decl = error_mark_node;
15932 }
15933 else
15934 prev = decl3;
15935 }
15936 *first = prev;
15937 return decl;
15938 }
15939
15940 /* Like tsubst_copy for expressions, etc. but also does semantic
15941 processing. */
15942
15943 tree
15944 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15945 bool integral_constant_expression_p)
15946 {
15947 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15948 #define RECUR(NODE) \
15949 tsubst_expr ((NODE), args, complain, in_decl, \
15950 integral_constant_expression_p)
15951
15952 tree stmt, tmp;
15953 tree r;
15954 location_t loc;
15955
15956 if (t == NULL_TREE || t == error_mark_node)
15957 return t;
15958
15959 loc = input_location;
15960 if (EXPR_HAS_LOCATION (t))
15961 input_location = EXPR_LOCATION (t);
15962 if (STATEMENT_CODE_P (TREE_CODE (t)))
15963 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15964
15965 switch (TREE_CODE (t))
15966 {
15967 case STATEMENT_LIST:
15968 {
15969 tree_stmt_iterator i;
15970 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15971 RECUR (tsi_stmt (i));
15972 break;
15973 }
15974
15975 case CTOR_INITIALIZER:
15976 finish_mem_initializers (tsubst_initializer_list
15977 (TREE_OPERAND (t, 0), args));
15978 break;
15979
15980 case RETURN_EXPR:
15981 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15982 break;
15983
15984 case EXPR_STMT:
15985 tmp = RECUR (EXPR_STMT_EXPR (t));
15986 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15987 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15988 else
15989 finish_expr_stmt (tmp);
15990 break;
15991
15992 case USING_STMT:
15993 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15994 /*attribs=*/NULL_TREE);
15995 break;
15996
15997 case DECL_EXPR:
15998 {
15999 tree decl, pattern_decl;
16000 tree init;
16001
16002 pattern_decl = decl = DECL_EXPR_DECL (t);
16003 if (TREE_CODE (decl) == LABEL_DECL)
16004 finish_label_decl (DECL_NAME (decl));
16005 else if (TREE_CODE (decl) == USING_DECL)
16006 {
16007 tree scope = USING_DECL_SCOPE (decl);
16008 tree name = DECL_NAME (decl);
16009
16010 scope = tsubst (scope, args, complain, in_decl);
16011 decl = lookup_qualified_name (scope, name,
16012 /*is_type_p=*/false,
16013 /*complain=*/false);
16014 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16015 qualified_name_lookup_error (scope, name, decl, input_location);
16016 else
16017 finish_local_using_decl (decl, scope, name);
16018 }
16019 else if (DECL_PACK_P (decl))
16020 {
16021 /* Don't build up decls for a variadic capture proxy, we'll
16022 instantiate the elements directly as needed. */
16023 break;
16024 }
16025 else if (is_capture_proxy (decl)
16026 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16027 {
16028 /* We're in tsubst_lambda_expr, we've already inserted a new
16029 capture proxy, so look it up and register it. */
16030 tree inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16031 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16032 gcc_assert (inst != decl && is_capture_proxy (inst));
16033 register_local_specialization (inst, decl);
16034 break;
16035 }
16036 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16037 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16038 /* Don't copy the old closure; we'll create a new one in
16039 tsubst_lambda_expr. */
16040 break;
16041 else
16042 {
16043 init = DECL_INITIAL (decl);
16044 decl = tsubst (decl, args, complain, in_decl);
16045 if (decl != error_mark_node)
16046 {
16047 /* By marking the declaration as instantiated, we avoid
16048 trying to instantiate it. Since instantiate_decl can't
16049 handle local variables, and since we've already done
16050 all that needs to be done, that's the right thing to
16051 do. */
16052 if (VAR_P (decl))
16053 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16054 if (VAR_P (decl)
16055 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16056 /* Anonymous aggregates are a special case. */
16057 finish_anon_union (decl);
16058 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16059 {
16060 DECL_CONTEXT (decl) = current_function_decl;
16061 if (DECL_NAME (decl) == this_identifier)
16062 {
16063 tree lam = DECL_CONTEXT (current_function_decl);
16064 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16065 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16066 }
16067 insert_capture_proxy (decl);
16068 }
16069 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16070 /* We already did a pushtag. */;
16071 else if (TREE_CODE (decl) == FUNCTION_DECL
16072 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16073 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16074 {
16075 DECL_CONTEXT (decl) = NULL_TREE;
16076 pushdecl (decl);
16077 DECL_CONTEXT (decl) = current_function_decl;
16078 cp_check_omp_declare_reduction (decl);
16079 }
16080 else
16081 {
16082 int const_init = false;
16083 maybe_push_decl (decl);
16084 if (VAR_P (decl)
16085 && DECL_PRETTY_FUNCTION_P (decl))
16086 {
16087 /* For __PRETTY_FUNCTION__ we have to adjust the
16088 initializer. */
16089 const char *const name
16090 = cxx_printable_name (current_function_decl, 2);
16091 init = cp_fname_init (name, &TREE_TYPE (decl));
16092 }
16093 else
16094 init = tsubst_init (init, decl, args, complain, in_decl);
16095
16096 if (VAR_P (decl))
16097 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16098 (pattern_decl));
16099 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16100 if (VAR_P (decl)
16101 && DECL_DECOMPOSITION_P (decl)
16102 && TREE_TYPE (pattern_decl) != error_mark_node)
16103 {
16104 unsigned int cnt;
16105 tree first;
16106 decl = tsubst_decomp_names (decl, pattern_decl, args,
16107 complain, in_decl, &first,
16108 &cnt);
16109 if (decl != error_mark_node)
16110 cp_finish_decomp (decl, first, cnt);
16111 }
16112 }
16113 }
16114 }
16115
16116 break;
16117 }
16118
16119 case FOR_STMT:
16120 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16121 RECUR (FOR_INIT_STMT (t));
16122 finish_init_stmt (stmt);
16123 tmp = RECUR (FOR_COND (t));
16124 finish_for_cond (tmp, stmt, false);
16125 tmp = RECUR (FOR_EXPR (t));
16126 finish_for_expr (tmp, stmt);
16127 RECUR (FOR_BODY (t));
16128 finish_for_stmt (stmt);
16129 break;
16130
16131 case RANGE_FOR_STMT:
16132 {
16133 tree decl, expr;
16134 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16135 decl = RANGE_FOR_DECL (t);
16136 decl = tsubst (decl, args, complain, in_decl);
16137 maybe_push_decl (decl);
16138 expr = RECUR (RANGE_FOR_EXPR (t));
16139 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16140 {
16141 unsigned int cnt;
16142 tree first;
16143 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16144 complain, in_decl, &first, &cnt);
16145 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
16146 RANGE_FOR_IVDEP (t));
16147 }
16148 else
16149 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
16150 RANGE_FOR_IVDEP (t));
16151 RECUR (RANGE_FOR_BODY (t));
16152 finish_for_stmt (stmt);
16153 }
16154 break;
16155
16156 case WHILE_STMT:
16157 stmt = begin_while_stmt ();
16158 tmp = RECUR (WHILE_COND (t));
16159 finish_while_stmt_cond (tmp, stmt, false);
16160 RECUR (WHILE_BODY (t));
16161 finish_while_stmt (stmt);
16162 break;
16163
16164 case DO_STMT:
16165 stmt = begin_do_stmt ();
16166 RECUR (DO_BODY (t));
16167 finish_do_body (stmt);
16168 tmp = RECUR (DO_COND (t));
16169 finish_do_stmt (tmp, stmt, false);
16170 break;
16171
16172 case IF_STMT:
16173 stmt = begin_if_stmt ();
16174 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16175 tmp = RECUR (IF_COND (t));
16176 tmp = finish_if_stmt_cond (tmp, stmt);
16177 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16178 /* Don't instantiate the THEN_CLAUSE. */;
16179 else
16180 {
16181 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16182 if (inhibit)
16183 ++c_inhibit_evaluation_warnings;
16184 RECUR (THEN_CLAUSE (t));
16185 if (inhibit)
16186 --c_inhibit_evaluation_warnings;
16187 }
16188 finish_then_clause (stmt);
16189
16190 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16191 /* Don't instantiate the ELSE_CLAUSE. */;
16192 else if (ELSE_CLAUSE (t))
16193 {
16194 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16195 begin_else_clause (stmt);
16196 if (inhibit)
16197 ++c_inhibit_evaluation_warnings;
16198 RECUR (ELSE_CLAUSE (t));
16199 if (inhibit)
16200 --c_inhibit_evaluation_warnings;
16201 finish_else_clause (stmt);
16202 }
16203
16204 finish_if_stmt (stmt);
16205 break;
16206
16207 case BIND_EXPR:
16208 if (BIND_EXPR_BODY_BLOCK (t))
16209 stmt = begin_function_body ();
16210 else
16211 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16212 ? BCS_TRY_BLOCK : 0);
16213
16214 RECUR (BIND_EXPR_BODY (t));
16215
16216 if (BIND_EXPR_BODY_BLOCK (t))
16217 finish_function_body (stmt);
16218 else
16219 finish_compound_stmt (stmt);
16220 break;
16221
16222 case BREAK_STMT:
16223 finish_break_stmt ();
16224 break;
16225
16226 case CONTINUE_STMT:
16227 finish_continue_stmt ();
16228 break;
16229
16230 case SWITCH_STMT:
16231 stmt = begin_switch_stmt ();
16232 tmp = RECUR (SWITCH_STMT_COND (t));
16233 finish_switch_cond (tmp, stmt);
16234 RECUR (SWITCH_STMT_BODY (t));
16235 finish_switch_stmt (stmt);
16236 break;
16237
16238 case CASE_LABEL_EXPR:
16239 {
16240 tree low = RECUR (CASE_LOW (t));
16241 tree high = RECUR (CASE_HIGH (t));
16242 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16243 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16244 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16245 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16246 }
16247 break;
16248
16249 case LABEL_EXPR:
16250 {
16251 tree decl = LABEL_EXPR_LABEL (t);
16252 tree label;
16253
16254 label = finish_label_stmt (DECL_NAME (decl));
16255 if (TREE_CODE (label) == LABEL_DECL)
16256 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16257 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16258 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16259 }
16260 break;
16261
16262 case GOTO_EXPR:
16263 tmp = GOTO_DESTINATION (t);
16264 if (TREE_CODE (tmp) != LABEL_DECL)
16265 /* Computed goto's must be tsubst'd into. On the other hand,
16266 non-computed gotos must not be; the identifier in question
16267 will have no binding. */
16268 tmp = RECUR (tmp);
16269 else
16270 tmp = DECL_NAME (tmp);
16271 finish_goto_stmt (tmp);
16272 break;
16273
16274 case ASM_EXPR:
16275 {
16276 tree string = RECUR (ASM_STRING (t));
16277 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16278 complain, in_decl);
16279 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16280 complain, in_decl);
16281 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16282 complain, in_decl);
16283 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16284 complain, in_decl);
16285 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16286 clobbers, labels);
16287 tree asm_expr = tmp;
16288 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16289 asm_expr = TREE_OPERAND (asm_expr, 0);
16290 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16291 }
16292 break;
16293
16294 case TRY_BLOCK:
16295 if (CLEANUP_P (t))
16296 {
16297 stmt = begin_try_block ();
16298 RECUR (TRY_STMTS (t));
16299 finish_cleanup_try_block (stmt);
16300 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16301 }
16302 else
16303 {
16304 tree compound_stmt = NULL_TREE;
16305
16306 if (FN_TRY_BLOCK_P (t))
16307 stmt = begin_function_try_block (&compound_stmt);
16308 else
16309 stmt = begin_try_block ();
16310
16311 RECUR (TRY_STMTS (t));
16312
16313 if (FN_TRY_BLOCK_P (t))
16314 finish_function_try_block (stmt);
16315 else
16316 finish_try_block (stmt);
16317
16318 RECUR (TRY_HANDLERS (t));
16319 if (FN_TRY_BLOCK_P (t))
16320 finish_function_handler_sequence (stmt, compound_stmt);
16321 else
16322 finish_handler_sequence (stmt);
16323 }
16324 break;
16325
16326 case HANDLER:
16327 {
16328 tree decl = HANDLER_PARMS (t);
16329
16330 if (decl)
16331 {
16332 decl = tsubst (decl, args, complain, in_decl);
16333 /* Prevent instantiate_decl from trying to instantiate
16334 this variable. We've already done all that needs to be
16335 done. */
16336 if (decl != error_mark_node)
16337 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16338 }
16339 stmt = begin_handler ();
16340 finish_handler_parms (decl, stmt);
16341 RECUR (HANDLER_BODY (t));
16342 finish_handler (stmt);
16343 }
16344 break;
16345
16346 case TAG_DEFN:
16347 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16348 if (CLASS_TYPE_P (tmp))
16349 {
16350 /* Local classes are not independent templates; they are
16351 instantiated along with their containing function. And this
16352 way we don't have to deal with pushing out of one local class
16353 to instantiate a member of another local class. */
16354 /* Closures are handled by the LAMBDA_EXPR. */
16355 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16356 complete_type (tmp);
16357 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16358 if ((VAR_P (fld)
16359 || (TREE_CODE (fld) == FUNCTION_DECL
16360 && !DECL_ARTIFICIAL (fld)))
16361 && DECL_TEMPLATE_INSTANTIATION (fld))
16362 instantiate_decl (fld, /*defer_ok=*/false,
16363 /*expl_inst_class=*/false);
16364 }
16365 break;
16366
16367 case STATIC_ASSERT:
16368 {
16369 tree condition;
16370
16371 ++c_inhibit_evaluation_warnings;
16372 condition =
16373 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16374 args,
16375 complain, in_decl,
16376 /*integral_constant_expression_p=*/true);
16377 --c_inhibit_evaluation_warnings;
16378
16379 finish_static_assert (condition,
16380 STATIC_ASSERT_MESSAGE (t),
16381 STATIC_ASSERT_SOURCE_LOCATION (t),
16382 /*member_p=*/false);
16383 }
16384 break;
16385
16386 case OACC_KERNELS:
16387 case OACC_PARALLEL:
16388 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16389 in_decl);
16390 stmt = begin_omp_parallel ();
16391 RECUR (OMP_BODY (t));
16392 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16393 break;
16394
16395 case OMP_PARALLEL:
16396 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16397 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16398 complain, in_decl);
16399 if (OMP_PARALLEL_COMBINED (t))
16400 omp_parallel_combined_clauses = &tmp;
16401 stmt = begin_omp_parallel ();
16402 RECUR (OMP_PARALLEL_BODY (t));
16403 gcc_assert (omp_parallel_combined_clauses == NULL);
16404 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16405 = OMP_PARALLEL_COMBINED (t);
16406 pop_omp_privatization_clauses (r);
16407 break;
16408
16409 case OMP_TASK:
16410 r = push_omp_privatization_clauses (false);
16411 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16412 complain, in_decl);
16413 stmt = begin_omp_task ();
16414 RECUR (OMP_TASK_BODY (t));
16415 finish_omp_task (tmp, stmt);
16416 pop_omp_privatization_clauses (r);
16417 break;
16418
16419 case OMP_FOR:
16420 case OMP_SIMD:
16421 case OMP_DISTRIBUTE:
16422 case OMP_TASKLOOP:
16423 case OACC_LOOP:
16424 {
16425 tree clauses, body, pre_body;
16426 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16427 tree orig_declv = NULL_TREE;
16428 tree incrv = NULL_TREE;
16429 enum c_omp_region_type ort = C_ORT_OMP;
16430 int i;
16431
16432 if (TREE_CODE (t) == OACC_LOOP)
16433 ort = C_ORT_ACC;
16434
16435 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16436 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16437 in_decl);
16438 if (OMP_FOR_INIT (t) != NULL_TREE)
16439 {
16440 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16441 if (OMP_FOR_ORIG_DECLS (t))
16442 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16443 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16444 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16445 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16446 }
16447
16448 stmt = begin_omp_structured_block ();
16449
16450 pre_body = push_stmt_list ();
16451 RECUR (OMP_FOR_PRE_BODY (t));
16452 pre_body = pop_stmt_list (pre_body);
16453
16454 if (OMP_FOR_INIT (t) != NULL_TREE)
16455 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16456 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16457 incrv, &clauses, args, complain, in_decl,
16458 integral_constant_expression_p);
16459 omp_parallel_combined_clauses = NULL;
16460
16461 body = push_stmt_list ();
16462 RECUR (OMP_FOR_BODY (t));
16463 body = pop_stmt_list (body);
16464
16465 if (OMP_FOR_INIT (t) != NULL_TREE)
16466 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16467 orig_declv, initv, condv, incrv, body, pre_body,
16468 NULL, clauses);
16469 else
16470 {
16471 t = make_node (TREE_CODE (t));
16472 TREE_TYPE (t) = void_type_node;
16473 OMP_FOR_BODY (t) = body;
16474 OMP_FOR_PRE_BODY (t) = pre_body;
16475 OMP_FOR_CLAUSES (t) = clauses;
16476 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16477 add_stmt (t);
16478 }
16479
16480 add_stmt (finish_omp_structured_block (stmt));
16481 pop_omp_privatization_clauses (r);
16482 }
16483 break;
16484
16485 case OMP_SECTIONS:
16486 omp_parallel_combined_clauses = NULL;
16487 /* FALLTHRU */
16488 case OMP_SINGLE:
16489 case OMP_TEAMS:
16490 case OMP_CRITICAL:
16491 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16492 && OMP_TEAMS_COMBINED (t));
16493 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16494 in_decl);
16495 stmt = push_stmt_list ();
16496 RECUR (OMP_BODY (t));
16497 stmt = pop_stmt_list (stmt);
16498
16499 t = copy_node (t);
16500 OMP_BODY (t) = stmt;
16501 OMP_CLAUSES (t) = tmp;
16502 add_stmt (t);
16503 pop_omp_privatization_clauses (r);
16504 break;
16505
16506 case OACC_DATA:
16507 case OMP_TARGET_DATA:
16508 case OMP_TARGET:
16509 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16510 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16511 in_decl);
16512 keep_next_level (true);
16513 stmt = begin_omp_structured_block ();
16514
16515 RECUR (OMP_BODY (t));
16516 stmt = finish_omp_structured_block (stmt);
16517
16518 t = copy_node (t);
16519 OMP_BODY (t) = stmt;
16520 OMP_CLAUSES (t) = tmp;
16521 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16522 {
16523 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16524 if (teams)
16525 {
16526 /* For combined target teams, ensure the num_teams and
16527 thread_limit clause expressions are evaluated on the host,
16528 before entering the target construct. */
16529 tree c;
16530 for (c = OMP_TEAMS_CLAUSES (teams);
16531 c; c = OMP_CLAUSE_CHAIN (c))
16532 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16533 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16534 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16535 {
16536 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16537 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16538 if (expr == error_mark_node)
16539 continue;
16540 tmp = TARGET_EXPR_SLOT (expr);
16541 add_stmt (expr);
16542 OMP_CLAUSE_OPERAND (c, 0) = expr;
16543 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16544 OMP_CLAUSE_FIRSTPRIVATE);
16545 OMP_CLAUSE_DECL (tc) = tmp;
16546 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16547 OMP_TARGET_CLAUSES (t) = tc;
16548 }
16549 }
16550 }
16551 add_stmt (t);
16552 break;
16553
16554 case OACC_DECLARE:
16555 t = copy_node (t);
16556 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16557 complain, in_decl);
16558 OACC_DECLARE_CLAUSES (t) = tmp;
16559 add_stmt (t);
16560 break;
16561
16562 case OMP_TARGET_UPDATE:
16563 case OMP_TARGET_ENTER_DATA:
16564 case OMP_TARGET_EXIT_DATA:
16565 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16566 complain, in_decl);
16567 t = copy_node (t);
16568 OMP_STANDALONE_CLAUSES (t) = tmp;
16569 add_stmt (t);
16570 break;
16571
16572 case OACC_ENTER_DATA:
16573 case OACC_EXIT_DATA:
16574 case OACC_UPDATE:
16575 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16576 complain, in_decl);
16577 t = copy_node (t);
16578 OMP_STANDALONE_CLAUSES (t) = tmp;
16579 add_stmt (t);
16580 break;
16581
16582 case OMP_ORDERED:
16583 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16584 complain, in_decl);
16585 stmt = push_stmt_list ();
16586 RECUR (OMP_BODY (t));
16587 stmt = pop_stmt_list (stmt);
16588
16589 t = copy_node (t);
16590 OMP_BODY (t) = stmt;
16591 OMP_ORDERED_CLAUSES (t) = tmp;
16592 add_stmt (t);
16593 break;
16594
16595 case OMP_SECTION:
16596 case OMP_MASTER:
16597 case OMP_TASKGROUP:
16598 stmt = push_stmt_list ();
16599 RECUR (OMP_BODY (t));
16600 stmt = pop_stmt_list (stmt);
16601
16602 t = copy_node (t);
16603 OMP_BODY (t) = stmt;
16604 add_stmt (t);
16605 break;
16606
16607 case OMP_ATOMIC:
16608 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16609 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16610 {
16611 tree op1 = TREE_OPERAND (t, 1);
16612 tree rhs1 = NULL_TREE;
16613 tree lhs, rhs;
16614 if (TREE_CODE (op1) == COMPOUND_EXPR)
16615 {
16616 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16617 op1 = TREE_OPERAND (op1, 1);
16618 }
16619 lhs = RECUR (TREE_OPERAND (op1, 0));
16620 rhs = RECUR (TREE_OPERAND (op1, 1));
16621 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16622 NULL_TREE, NULL_TREE, rhs1,
16623 OMP_ATOMIC_SEQ_CST (t));
16624 }
16625 else
16626 {
16627 tree op1 = TREE_OPERAND (t, 1);
16628 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16629 tree rhs1 = NULL_TREE;
16630 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16631 enum tree_code opcode = NOP_EXPR;
16632 if (code == OMP_ATOMIC_READ)
16633 {
16634 v = RECUR (TREE_OPERAND (op1, 0));
16635 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16636 }
16637 else if (code == OMP_ATOMIC_CAPTURE_OLD
16638 || code == OMP_ATOMIC_CAPTURE_NEW)
16639 {
16640 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16641 v = RECUR (TREE_OPERAND (op1, 0));
16642 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16643 if (TREE_CODE (op11) == COMPOUND_EXPR)
16644 {
16645 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16646 op11 = TREE_OPERAND (op11, 1);
16647 }
16648 lhs = RECUR (TREE_OPERAND (op11, 0));
16649 rhs = RECUR (TREE_OPERAND (op11, 1));
16650 opcode = TREE_CODE (op11);
16651 if (opcode == MODIFY_EXPR)
16652 opcode = NOP_EXPR;
16653 }
16654 else
16655 {
16656 code = OMP_ATOMIC;
16657 lhs = RECUR (TREE_OPERAND (op1, 0));
16658 rhs = RECUR (TREE_OPERAND (op1, 1));
16659 }
16660 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16661 OMP_ATOMIC_SEQ_CST (t));
16662 }
16663 break;
16664
16665 case TRANSACTION_EXPR:
16666 {
16667 int flags = 0;
16668 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16669 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16670
16671 if (TRANSACTION_EXPR_IS_STMT (t))
16672 {
16673 tree body = TRANSACTION_EXPR_BODY (t);
16674 tree noex = NULL_TREE;
16675 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16676 {
16677 noex = MUST_NOT_THROW_COND (body);
16678 if (noex == NULL_TREE)
16679 noex = boolean_true_node;
16680 body = TREE_OPERAND (body, 0);
16681 }
16682 stmt = begin_transaction_stmt (input_location, NULL, flags);
16683 RECUR (body);
16684 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16685 }
16686 else
16687 {
16688 stmt = build_transaction_expr (EXPR_LOCATION (t),
16689 RECUR (TRANSACTION_EXPR_BODY (t)),
16690 flags, NULL_TREE);
16691 RETURN (stmt);
16692 }
16693 }
16694 break;
16695
16696 case MUST_NOT_THROW_EXPR:
16697 {
16698 tree op0 = RECUR (TREE_OPERAND (t, 0));
16699 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16700 RETURN (build_must_not_throw_expr (op0, cond));
16701 }
16702
16703 case EXPR_PACK_EXPANSION:
16704 error ("invalid use of pack expansion expression");
16705 RETURN (error_mark_node);
16706
16707 case NONTYPE_ARGUMENT_PACK:
16708 error ("use %<...%> to expand argument pack");
16709 RETURN (error_mark_node);
16710
16711 case COMPOUND_EXPR:
16712 tmp = RECUR (TREE_OPERAND (t, 0));
16713 if (tmp == NULL_TREE)
16714 /* If the first operand was a statement, we're done with it. */
16715 RETURN (RECUR (TREE_OPERAND (t, 1)));
16716 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16717 RECUR (TREE_OPERAND (t, 1)),
16718 complain));
16719
16720 case ANNOTATE_EXPR:
16721 tmp = RECUR (TREE_OPERAND (t, 0));
16722 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16723 TREE_TYPE (tmp), tmp,
16724 RECUR (TREE_OPERAND (t, 1)),
16725 RECUR (TREE_OPERAND (t, 2))));
16726
16727 default:
16728 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16729
16730 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16731 /*function_p=*/false,
16732 integral_constant_expression_p));
16733 }
16734
16735 RETURN (NULL_TREE);
16736 out:
16737 input_location = loc;
16738 return r;
16739 #undef RECUR
16740 #undef RETURN
16741 }
16742
16743 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16744 function. For description of the body see comment above
16745 cp_parser_omp_declare_reduction_exprs. */
16746
16747 static void
16748 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16749 {
16750 if (t == NULL_TREE || t == error_mark_node)
16751 return;
16752
16753 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16754
16755 tree_stmt_iterator tsi;
16756 int i;
16757 tree stmts[7];
16758 memset (stmts, 0, sizeof stmts);
16759 for (i = 0, tsi = tsi_start (t);
16760 i < 7 && !tsi_end_p (tsi);
16761 i++, tsi_next (&tsi))
16762 stmts[i] = tsi_stmt (tsi);
16763 gcc_assert (tsi_end_p (tsi));
16764
16765 if (i >= 3)
16766 {
16767 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16768 && TREE_CODE (stmts[1]) == DECL_EXPR);
16769 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16770 args, complain, in_decl);
16771 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16772 args, complain, in_decl);
16773 DECL_CONTEXT (omp_out) = current_function_decl;
16774 DECL_CONTEXT (omp_in) = current_function_decl;
16775 keep_next_level (true);
16776 tree block = begin_omp_structured_block ();
16777 tsubst_expr (stmts[2], args, complain, in_decl, false);
16778 block = finish_omp_structured_block (block);
16779 block = maybe_cleanup_point_expr_void (block);
16780 add_decl_expr (omp_out);
16781 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16782 TREE_NO_WARNING (omp_out) = 1;
16783 add_decl_expr (omp_in);
16784 finish_expr_stmt (block);
16785 }
16786 if (i >= 6)
16787 {
16788 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16789 && TREE_CODE (stmts[4]) == DECL_EXPR);
16790 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16791 args, complain, in_decl);
16792 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16793 args, complain, in_decl);
16794 DECL_CONTEXT (omp_priv) = current_function_decl;
16795 DECL_CONTEXT (omp_orig) = current_function_decl;
16796 keep_next_level (true);
16797 tree block = begin_omp_structured_block ();
16798 tsubst_expr (stmts[5], args, complain, in_decl, false);
16799 block = finish_omp_structured_block (block);
16800 block = maybe_cleanup_point_expr_void (block);
16801 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16802 add_decl_expr (omp_priv);
16803 add_decl_expr (omp_orig);
16804 finish_expr_stmt (block);
16805 if (i == 7)
16806 add_decl_expr (omp_orig);
16807 }
16808 }
16809
16810 /* T is a postfix-expression that is not being used in a function
16811 call. Return the substituted version of T. */
16812
16813 static tree
16814 tsubst_non_call_postfix_expression (tree t, tree args,
16815 tsubst_flags_t complain,
16816 tree in_decl)
16817 {
16818 if (TREE_CODE (t) == SCOPE_REF)
16819 t = tsubst_qualified_id (t, args, complain, in_decl,
16820 /*done=*/false, /*address_p=*/false);
16821 else
16822 t = tsubst_copy_and_build (t, args, complain, in_decl,
16823 /*function_p=*/false,
16824 /*integral_constant_expression_p=*/false);
16825
16826 return t;
16827 }
16828
16829 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16830 instantiation context. Instantiating a pack expansion containing a lambda
16831 might result in multiple lambdas all based on the same lambda in the
16832 template. */
16833
16834 tree
16835 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16836 {
16837 tree oldfn = lambda_function (t);
16838 in_decl = oldfn;
16839
16840 tree r = build_lambda_expr ();
16841
16842 LAMBDA_EXPR_LOCATION (r)
16843 = LAMBDA_EXPR_LOCATION (t);
16844 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16845 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16846 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16847
16848 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16849 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16850 else
16851 record_lambda_scope (r);
16852
16853 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16854 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16855
16856 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16857 cap = TREE_CHAIN (cap))
16858 {
16859 tree field = TREE_PURPOSE (cap);
16860 if (PACK_EXPANSION_P (field))
16861 field = PACK_EXPANSION_PATTERN (field);
16862 field = tsubst_decl (field, args, complain);
16863
16864 if (field == error_mark_node)
16865 return error_mark_node;
16866
16867 tree init = TREE_VALUE (cap);
16868 if (PACK_EXPANSION_P (init))
16869 init = tsubst_pack_expansion (init, args, complain, in_decl);
16870 else
16871 init = tsubst_copy_and_build (init, args, complain, in_decl,
16872 /*fn*/false, /*constexpr*/false);
16873
16874 if (TREE_CODE (field) == TREE_VEC)
16875 {
16876 int len = TREE_VEC_LENGTH (field);
16877 gcc_assert (TREE_CODE (init) == TREE_VEC
16878 && TREE_VEC_LENGTH (init) == len);
16879 for (int i = 0; i < len; ++i)
16880 LAMBDA_EXPR_CAPTURE_LIST (r)
16881 = tree_cons (TREE_VEC_ELT (field, i),
16882 TREE_VEC_ELT (init, i),
16883 LAMBDA_EXPR_CAPTURE_LIST (r));
16884 }
16885 else
16886 {
16887 LAMBDA_EXPR_CAPTURE_LIST (r)
16888 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
16889
16890 if (id_equal (DECL_NAME (field), "__this"))
16891 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
16892 }
16893 }
16894
16895 tree type = begin_lambda_type (r);
16896
16897 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16898 determine_visibility (TYPE_NAME (type));
16899
16900 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
16901
16902 tree oldtmpl = (generic_lambda_fn_p (oldfn)
16903 ? DECL_TI_TEMPLATE (oldfn)
16904 : NULL_TREE);
16905
16906 tree fntype = static_fn_type (oldfn);
16907 if (oldtmpl)
16908 ++processing_template_decl;
16909 fntype = tsubst (fntype, args, complain, in_decl);
16910 if (oldtmpl)
16911 --processing_template_decl;
16912
16913 if (fntype == error_mark_node)
16914 r = error_mark_node;
16915 else
16916 {
16917 /* Fix the type of 'this'. */
16918 fntype = build_memfn_type (fntype, type,
16919 type_memfn_quals (fntype),
16920 type_memfn_rqual (fntype));
16921 tree fn, tmpl;
16922 if (oldtmpl)
16923 {
16924 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
16925 fn = DECL_TEMPLATE_RESULT (tmpl);
16926 finish_member_declaration (tmpl);
16927 }
16928 else
16929 {
16930 tmpl = NULL_TREE;
16931 fn = tsubst_function_decl (oldfn, args, complain, fntype);
16932 finish_member_declaration (fn);
16933 }
16934
16935 /* Let finish_function set this. */
16936 DECL_DECLARED_CONSTEXPR_P (fn) = false;
16937
16938 bool nested = cfun;
16939 if (nested)
16940 push_function_context ();
16941
16942 local_specialization_stack s (lss_copy);
16943
16944 tree body = start_lambda_function (fn, r);
16945
16946 register_parameter_specializations (oldfn, fn);
16947
16948 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
16949 /*constexpr*/false);
16950
16951 finish_lambda_function (body);
16952
16953 if (nested)
16954 pop_function_context ();
16955
16956 /* The capture list was built up in reverse order; fix that now. */
16957 LAMBDA_EXPR_CAPTURE_LIST (r)
16958 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
16959
16960 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16961
16962 maybe_add_lambda_conv_op (type);
16963 }
16964
16965 finish_struct (type, /*attr*/NULL_TREE);
16966
16967 insert_pending_capture_proxies ();
16968
16969 return r;
16970 }
16971
16972 /* Like tsubst but deals with expressions and performs semantic
16973 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16974
16975 tree
16976 tsubst_copy_and_build (tree t,
16977 tree args,
16978 tsubst_flags_t complain,
16979 tree in_decl,
16980 bool function_p,
16981 bool integral_constant_expression_p)
16982 {
16983 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16984 #define RECUR(NODE) \
16985 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16986 /*function_p=*/false, \
16987 integral_constant_expression_p)
16988
16989 tree retval, op1;
16990 location_t loc;
16991
16992 if (t == NULL_TREE || t == error_mark_node)
16993 return t;
16994
16995 loc = input_location;
16996 if (EXPR_HAS_LOCATION (t))
16997 input_location = EXPR_LOCATION (t);
16998
16999 /* N3276 decltype magic only applies to calls at the top level or on the
17000 right side of a comma. */
17001 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17002 complain &= ~tf_decltype;
17003
17004 switch (TREE_CODE (t))
17005 {
17006 case USING_DECL:
17007 t = DECL_NAME (t);
17008 /* Fall through. */
17009 case IDENTIFIER_NODE:
17010 {
17011 tree decl;
17012 cp_id_kind idk;
17013 bool non_integral_constant_expression_p;
17014 const char *error_msg;
17015
17016 if (IDENTIFIER_CONV_OP_P (t))
17017 {
17018 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17019 t = make_conv_op_name (new_type);
17020 }
17021
17022 /* Look up the name. */
17023 decl = lookup_name (t);
17024
17025 /* By convention, expressions use ERROR_MARK_NODE to indicate
17026 failure, not NULL_TREE. */
17027 if (decl == NULL_TREE)
17028 decl = error_mark_node;
17029
17030 decl = finish_id_expression (t, decl, NULL_TREE,
17031 &idk,
17032 integral_constant_expression_p,
17033 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17034 &non_integral_constant_expression_p,
17035 /*template_p=*/false,
17036 /*done=*/true,
17037 /*address_p=*/false,
17038 /*template_arg_p=*/false,
17039 &error_msg,
17040 input_location);
17041 if (error_msg)
17042 error (error_msg);
17043 if (!function_p && identifier_p (decl))
17044 {
17045 if (complain & tf_error)
17046 unqualified_name_lookup_error (decl);
17047 decl = error_mark_node;
17048 }
17049 RETURN (decl);
17050 }
17051
17052 case TEMPLATE_ID_EXPR:
17053 {
17054 tree object;
17055 tree templ = RECUR (TREE_OPERAND (t, 0));
17056 tree targs = TREE_OPERAND (t, 1);
17057
17058 if (targs)
17059 targs = tsubst_template_args (targs, args, complain, in_decl);
17060 if (targs == error_mark_node)
17061 return error_mark_node;
17062
17063 if (TREE_CODE (templ) == SCOPE_REF)
17064 {
17065 tree name = TREE_OPERAND (templ, 1);
17066 tree tid = lookup_template_function (name, targs);
17067 TREE_OPERAND (templ, 1) = tid;
17068 return templ;
17069 }
17070
17071 if (variable_template_p (templ))
17072 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17073
17074 if (TREE_CODE (templ) == COMPONENT_REF)
17075 {
17076 object = TREE_OPERAND (templ, 0);
17077 templ = TREE_OPERAND (templ, 1);
17078 }
17079 else
17080 object = NULL_TREE;
17081 templ = lookup_template_function (templ, targs);
17082
17083 if (object)
17084 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17085 object, templ, NULL_TREE));
17086 else
17087 RETURN (baselink_for_fns (templ));
17088 }
17089
17090 case INDIRECT_REF:
17091 {
17092 tree r = RECUR (TREE_OPERAND (t, 0));
17093
17094 if (REFERENCE_REF_P (t))
17095 {
17096 /* A type conversion to reference type will be enclosed in
17097 such an indirect ref, but the substitution of the cast
17098 will have also added such an indirect ref. */
17099 r = convert_from_reference (r);
17100 }
17101 else
17102 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17103 complain|decltype_flag);
17104
17105 if (TREE_CODE (r) == INDIRECT_REF)
17106 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17107
17108 RETURN (r);
17109 }
17110
17111 case NOP_EXPR:
17112 {
17113 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17114 tree op0 = RECUR (TREE_OPERAND (t, 0));
17115 RETURN (build_nop (type, op0));
17116 }
17117
17118 case IMPLICIT_CONV_EXPR:
17119 {
17120 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17121 tree expr = RECUR (TREE_OPERAND (t, 0));
17122 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17123 {
17124 retval = copy_node (t);
17125 TREE_TYPE (retval) = type;
17126 TREE_OPERAND (retval, 0) = expr;
17127 RETURN (retval);
17128 }
17129 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17130 /* We'll pass this to convert_nontype_argument again, we don't need
17131 to actually perform any conversion here. */
17132 RETURN (expr);
17133 int flags = LOOKUP_IMPLICIT;
17134 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17135 flags = LOOKUP_NORMAL;
17136 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17137 flags));
17138 }
17139
17140 case CONVERT_EXPR:
17141 {
17142 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17143 tree op0 = RECUR (TREE_OPERAND (t, 0));
17144 RETURN (build1 (CONVERT_EXPR, type, op0));
17145 }
17146
17147 case CAST_EXPR:
17148 case REINTERPRET_CAST_EXPR:
17149 case CONST_CAST_EXPR:
17150 case DYNAMIC_CAST_EXPR:
17151 case STATIC_CAST_EXPR:
17152 {
17153 tree type;
17154 tree op, r = NULL_TREE;
17155
17156 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17157 if (integral_constant_expression_p
17158 && !cast_valid_in_integral_constant_expression_p (type))
17159 {
17160 if (complain & tf_error)
17161 error ("a cast to a type other than an integral or "
17162 "enumeration type cannot appear in a constant-expression");
17163 RETURN (error_mark_node);
17164 }
17165
17166 op = RECUR (TREE_OPERAND (t, 0));
17167
17168 warning_sentinel s(warn_useless_cast);
17169 switch (TREE_CODE (t))
17170 {
17171 case CAST_EXPR:
17172 r = build_functional_cast (type, op, complain);
17173 break;
17174 case REINTERPRET_CAST_EXPR:
17175 r = build_reinterpret_cast (type, op, complain);
17176 break;
17177 case CONST_CAST_EXPR:
17178 r = build_const_cast (type, op, complain);
17179 break;
17180 case DYNAMIC_CAST_EXPR:
17181 r = build_dynamic_cast (type, op, complain);
17182 break;
17183 case STATIC_CAST_EXPR:
17184 r = build_static_cast (type, op, complain);
17185 break;
17186 default:
17187 gcc_unreachable ();
17188 }
17189
17190 RETURN (r);
17191 }
17192
17193 case POSTDECREMENT_EXPR:
17194 case POSTINCREMENT_EXPR:
17195 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17196 args, complain, in_decl);
17197 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17198 complain|decltype_flag));
17199
17200 case PREDECREMENT_EXPR:
17201 case PREINCREMENT_EXPR:
17202 case NEGATE_EXPR:
17203 case BIT_NOT_EXPR:
17204 case ABS_EXPR:
17205 case TRUTH_NOT_EXPR:
17206 case UNARY_PLUS_EXPR: /* Unary + */
17207 case REALPART_EXPR:
17208 case IMAGPART_EXPR:
17209 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17210 RECUR (TREE_OPERAND (t, 0)),
17211 complain|decltype_flag));
17212
17213 case FIX_TRUNC_EXPR:
17214 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17215 false, complain));
17216
17217 case ADDR_EXPR:
17218 op1 = TREE_OPERAND (t, 0);
17219 if (TREE_CODE (op1) == LABEL_DECL)
17220 RETURN (finish_label_address_expr (DECL_NAME (op1),
17221 EXPR_LOCATION (op1)));
17222 if (TREE_CODE (op1) == SCOPE_REF)
17223 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17224 /*done=*/true, /*address_p=*/true);
17225 else
17226 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17227 in_decl);
17228 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17229 complain|decltype_flag));
17230
17231 case PLUS_EXPR:
17232 case MINUS_EXPR:
17233 case MULT_EXPR:
17234 case TRUNC_DIV_EXPR:
17235 case CEIL_DIV_EXPR:
17236 case FLOOR_DIV_EXPR:
17237 case ROUND_DIV_EXPR:
17238 case EXACT_DIV_EXPR:
17239 case BIT_AND_EXPR:
17240 case BIT_IOR_EXPR:
17241 case BIT_XOR_EXPR:
17242 case TRUNC_MOD_EXPR:
17243 case FLOOR_MOD_EXPR:
17244 case TRUTH_ANDIF_EXPR:
17245 case TRUTH_ORIF_EXPR:
17246 case TRUTH_AND_EXPR:
17247 case TRUTH_OR_EXPR:
17248 case RSHIFT_EXPR:
17249 case LSHIFT_EXPR:
17250 case RROTATE_EXPR:
17251 case LROTATE_EXPR:
17252 case EQ_EXPR:
17253 case NE_EXPR:
17254 case MAX_EXPR:
17255 case MIN_EXPR:
17256 case LE_EXPR:
17257 case GE_EXPR:
17258 case LT_EXPR:
17259 case GT_EXPR:
17260 case MEMBER_REF:
17261 case DOTSTAR_EXPR:
17262 {
17263 warning_sentinel s1(warn_type_limits);
17264 warning_sentinel s2(warn_div_by_zero);
17265 warning_sentinel s3(warn_logical_op);
17266 warning_sentinel s4(warn_tautological_compare);
17267 tree op0 = RECUR (TREE_OPERAND (t, 0));
17268 tree op1 = RECUR (TREE_OPERAND (t, 1));
17269 tree r = build_x_binary_op
17270 (input_location, TREE_CODE (t),
17271 op0,
17272 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17273 ? ERROR_MARK
17274 : TREE_CODE (TREE_OPERAND (t, 0))),
17275 op1,
17276 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17277 ? ERROR_MARK
17278 : TREE_CODE (TREE_OPERAND (t, 1))),
17279 /*overload=*/NULL,
17280 complain|decltype_flag);
17281 if (EXPR_P (r) && TREE_NO_WARNING (t))
17282 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17283
17284 RETURN (r);
17285 }
17286
17287 case POINTER_PLUS_EXPR:
17288 {
17289 tree op0 = RECUR (TREE_OPERAND (t, 0));
17290 tree op1 = RECUR (TREE_OPERAND (t, 1));
17291 return fold_build_pointer_plus (op0, op1);
17292 }
17293
17294 case SCOPE_REF:
17295 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17296 /*address_p=*/false));
17297 case ARRAY_REF:
17298 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17299 args, complain, in_decl);
17300 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17301 RECUR (TREE_OPERAND (t, 1)),
17302 complain|decltype_flag));
17303
17304 case SIZEOF_EXPR:
17305 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17306 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17307 RETURN (tsubst_copy (t, args, complain, in_decl));
17308 /* Fall through */
17309
17310 case ALIGNOF_EXPR:
17311 {
17312 tree r;
17313
17314 op1 = TREE_OPERAND (t, 0);
17315 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17316 op1 = TREE_TYPE (op1);
17317 if (!args)
17318 {
17319 /* When there are no ARGS, we are trying to evaluate a
17320 non-dependent expression from the parser. Trying to do
17321 the substitutions may not work. */
17322 if (!TYPE_P (op1))
17323 op1 = TREE_TYPE (op1);
17324 }
17325 else
17326 {
17327 ++cp_unevaluated_operand;
17328 ++c_inhibit_evaluation_warnings;
17329 if (TYPE_P (op1))
17330 op1 = tsubst (op1, args, complain, in_decl);
17331 else
17332 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17333 /*function_p=*/false,
17334 /*integral_constant_expression_p=*/
17335 false);
17336 --cp_unevaluated_operand;
17337 --c_inhibit_evaluation_warnings;
17338 }
17339 if (TYPE_P (op1))
17340 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17341 complain & tf_error);
17342 else
17343 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17344 complain & tf_error);
17345 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17346 {
17347 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17348 {
17349 if (!processing_template_decl && TYPE_P (op1))
17350 {
17351 r = build_min (SIZEOF_EXPR, size_type_node,
17352 build1 (NOP_EXPR, op1, error_mark_node));
17353 SIZEOF_EXPR_TYPE_P (r) = 1;
17354 }
17355 else
17356 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17357 TREE_SIDE_EFFECTS (r) = 0;
17358 TREE_READONLY (r) = 1;
17359 }
17360 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17361 }
17362 RETURN (r);
17363 }
17364
17365 case AT_ENCODE_EXPR:
17366 {
17367 op1 = TREE_OPERAND (t, 0);
17368 ++cp_unevaluated_operand;
17369 ++c_inhibit_evaluation_warnings;
17370 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17371 /*function_p=*/false,
17372 /*integral_constant_expression_p=*/false);
17373 --cp_unevaluated_operand;
17374 --c_inhibit_evaluation_warnings;
17375 RETURN (objc_build_encode_expr (op1));
17376 }
17377
17378 case NOEXCEPT_EXPR:
17379 op1 = TREE_OPERAND (t, 0);
17380 ++cp_unevaluated_operand;
17381 ++c_inhibit_evaluation_warnings;
17382 ++cp_noexcept_operand;
17383 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17384 /*function_p=*/false,
17385 /*integral_constant_expression_p=*/false);
17386 --cp_unevaluated_operand;
17387 --c_inhibit_evaluation_warnings;
17388 --cp_noexcept_operand;
17389 RETURN (finish_noexcept_expr (op1, complain));
17390
17391 case MODOP_EXPR:
17392 {
17393 warning_sentinel s(warn_div_by_zero);
17394 tree lhs = RECUR (TREE_OPERAND (t, 0));
17395 tree rhs = RECUR (TREE_OPERAND (t, 2));
17396 tree r = build_x_modify_expr
17397 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17398 complain|decltype_flag);
17399 /* TREE_NO_WARNING must be set if either the expression was
17400 parenthesized or it uses an operator such as >>= rather
17401 than plain assignment. In the former case, it was already
17402 set and must be copied. In the latter case,
17403 build_x_modify_expr sets it and it must not be reset
17404 here. */
17405 if (TREE_NO_WARNING (t))
17406 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17407
17408 RETURN (r);
17409 }
17410
17411 case ARROW_EXPR:
17412 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17413 args, complain, in_decl);
17414 /* Remember that there was a reference to this entity. */
17415 if (DECL_P (op1)
17416 && !mark_used (op1, complain) && !(complain & tf_error))
17417 RETURN (error_mark_node);
17418 RETURN (build_x_arrow (input_location, op1, complain));
17419
17420 case NEW_EXPR:
17421 {
17422 tree placement = RECUR (TREE_OPERAND (t, 0));
17423 tree init = RECUR (TREE_OPERAND (t, 3));
17424 vec<tree, va_gc> *placement_vec;
17425 vec<tree, va_gc> *init_vec;
17426 tree ret;
17427
17428 if (placement == NULL_TREE)
17429 placement_vec = NULL;
17430 else
17431 {
17432 placement_vec = make_tree_vector ();
17433 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17434 vec_safe_push (placement_vec, TREE_VALUE (placement));
17435 }
17436
17437 /* If there was an initializer in the original tree, but it
17438 instantiated to an empty list, then we should pass a
17439 non-NULL empty vector to tell build_new that it was an
17440 empty initializer() rather than no initializer. This can
17441 only happen when the initializer is a pack expansion whose
17442 parameter packs are of length zero. */
17443 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17444 init_vec = NULL;
17445 else
17446 {
17447 init_vec = make_tree_vector ();
17448 if (init == void_node)
17449 gcc_assert (init_vec != NULL);
17450 else
17451 {
17452 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17453 vec_safe_push (init_vec, TREE_VALUE (init));
17454 }
17455 }
17456
17457 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17458 tree op2 = RECUR (TREE_OPERAND (t, 2));
17459 ret = build_new (&placement_vec, op1, op2, &init_vec,
17460 NEW_EXPR_USE_GLOBAL (t),
17461 complain);
17462
17463 if (placement_vec != NULL)
17464 release_tree_vector (placement_vec);
17465 if (init_vec != NULL)
17466 release_tree_vector (init_vec);
17467
17468 RETURN (ret);
17469 }
17470
17471 case DELETE_EXPR:
17472 {
17473 tree op0 = RECUR (TREE_OPERAND (t, 0));
17474 tree op1 = RECUR (TREE_OPERAND (t, 1));
17475 RETURN (delete_sanity (op0, op1,
17476 DELETE_EXPR_USE_VEC (t),
17477 DELETE_EXPR_USE_GLOBAL (t),
17478 complain));
17479 }
17480
17481 case COMPOUND_EXPR:
17482 {
17483 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17484 complain & ~tf_decltype, in_decl,
17485 /*function_p=*/false,
17486 integral_constant_expression_p);
17487 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17488 op0,
17489 RECUR (TREE_OPERAND (t, 1)),
17490 complain|decltype_flag));
17491 }
17492
17493 case CALL_EXPR:
17494 {
17495 tree function;
17496 vec<tree, va_gc> *call_args;
17497 unsigned int nargs, i;
17498 bool qualified_p;
17499 bool koenig_p;
17500 tree ret;
17501
17502 function = CALL_EXPR_FN (t);
17503 /* Internal function with no arguments. */
17504 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17505 RETURN (t);
17506
17507 /* When we parsed the expression, we determined whether or
17508 not Koenig lookup should be performed. */
17509 koenig_p = KOENIG_LOOKUP_P (t);
17510 if (function == NULL_TREE)
17511 {
17512 koenig_p = false;
17513 qualified_p = false;
17514 }
17515 else if (TREE_CODE (function) == SCOPE_REF)
17516 {
17517 qualified_p = true;
17518 function = tsubst_qualified_id (function, args, complain, in_decl,
17519 /*done=*/false,
17520 /*address_p=*/false);
17521 }
17522 else if (koenig_p && identifier_p (function))
17523 {
17524 /* Do nothing; calling tsubst_copy_and_build on an identifier
17525 would incorrectly perform unqualified lookup again.
17526
17527 Note that we can also have an IDENTIFIER_NODE if the earlier
17528 unqualified lookup found a member function; in that case
17529 koenig_p will be false and we do want to do the lookup
17530 again to find the instantiated member function.
17531
17532 FIXME but doing that causes c++/15272, so we need to stop
17533 using IDENTIFIER_NODE in that situation. */
17534 qualified_p = false;
17535 }
17536 else
17537 {
17538 if (TREE_CODE (function) == COMPONENT_REF)
17539 {
17540 tree op = TREE_OPERAND (function, 1);
17541
17542 qualified_p = (TREE_CODE (op) == SCOPE_REF
17543 || (BASELINK_P (op)
17544 && BASELINK_QUALIFIED_P (op)));
17545 }
17546 else
17547 qualified_p = false;
17548
17549 if (TREE_CODE (function) == ADDR_EXPR
17550 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17551 /* Avoid error about taking the address of a constructor. */
17552 function = TREE_OPERAND (function, 0);
17553
17554 function = tsubst_copy_and_build (function, args, complain,
17555 in_decl,
17556 !qualified_p,
17557 integral_constant_expression_p);
17558
17559 if (BASELINK_P (function))
17560 qualified_p = true;
17561 }
17562
17563 nargs = call_expr_nargs (t);
17564 call_args = make_tree_vector ();
17565 for (i = 0; i < nargs; ++i)
17566 {
17567 tree arg = CALL_EXPR_ARG (t, i);
17568
17569 if (!PACK_EXPANSION_P (arg))
17570 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17571 else
17572 {
17573 /* Expand the pack expansion and push each entry onto
17574 CALL_ARGS. */
17575 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17576 if (TREE_CODE (arg) == TREE_VEC)
17577 {
17578 unsigned int len, j;
17579
17580 len = TREE_VEC_LENGTH (arg);
17581 for (j = 0; j < len; ++j)
17582 {
17583 tree value = TREE_VEC_ELT (arg, j);
17584 if (value != NULL_TREE)
17585 value = convert_from_reference (value);
17586 vec_safe_push (call_args, value);
17587 }
17588 }
17589 else
17590 {
17591 /* A partial substitution. Add one entry. */
17592 vec_safe_push (call_args, arg);
17593 }
17594 }
17595 }
17596
17597 /* We do not perform argument-dependent lookup if normal
17598 lookup finds a non-function, in accordance with the
17599 expected resolution of DR 218. */
17600 if (koenig_p
17601 && ((is_overloaded_fn (function)
17602 /* If lookup found a member function, the Koenig lookup is
17603 not appropriate, even if an unqualified-name was used
17604 to denote the function. */
17605 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17606 || identifier_p (function))
17607 /* Only do this when substitution turns a dependent call
17608 into a non-dependent call. */
17609 && type_dependent_expression_p_push (t)
17610 && !any_type_dependent_arguments_p (call_args))
17611 function = perform_koenig_lookup (function, call_args, tf_none);
17612
17613 if (function != NULL_TREE
17614 && identifier_p (function)
17615 && !any_type_dependent_arguments_p (call_args))
17616 {
17617 if (koenig_p && (complain & tf_warning_or_error))
17618 {
17619 /* For backwards compatibility and good diagnostics, try
17620 the unqualified lookup again if we aren't in SFINAE
17621 context. */
17622 tree unq = (tsubst_copy_and_build
17623 (function, args, complain, in_decl, true,
17624 integral_constant_expression_p));
17625 if (unq == error_mark_node)
17626 {
17627 release_tree_vector (call_args);
17628 RETURN (error_mark_node);
17629 }
17630
17631 if (unq != function)
17632 {
17633 /* In a lambda fn, we have to be careful to not
17634 introduce new this captures. Legacy code can't
17635 be using lambdas anyway, so it's ok to be
17636 stricter. */
17637 bool in_lambda = (current_class_type
17638 && LAMBDA_TYPE_P (current_class_type));
17639 char const *const msg
17640 = G_("%qD was not declared in this scope, "
17641 "and no declarations were found by "
17642 "argument-dependent lookup at the point "
17643 "of instantiation");
17644
17645 bool diag = true;
17646 if (in_lambda)
17647 error_at (EXPR_LOC_OR_LOC (t, input_location),
17648 msg, function);
17649 else
17650 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17651 msg, function);
17652 if (diag)
17653 {
17654 tree fn = unq;
17655
17656 if (INDIRECT_REF_P (fn))
17657 fn = TREE_OPERAND (fn, 0);
17658 if (is_overloaded_fn (fn))
17659 fn = get_first_fn (fn);
17660
17661 if (!DECL_P (fn))
17662 /* Can't say anything more. */;
17663 else if (DECL_CLASS_SCOPE_P (fn))
17664 {
17665 location_t loc = EXPR_LOC_OR_LOC (t,
17666 input_location);
17667 inform (loc,
17668 "declarations in dependent base %qT are "
17669 "not found by unqualified lookup",
17670 DECL_CLASS_CONTEXT (fn));
17671 if (current_class_ptr)
17672 inform (loc,
17673 "use %<this->%D%> instead", function);
17674 else
17675 inform (loc,
17676 "use %<%T::%D%> instead",
17677 current_class_name, function);
17678 }
17679 else
17680 inform (DECL_SOURCE_LOCATION (fn),
17681 "%qD declared here, later in the "
17682 "translation unit", fn);
17683 if (in_lambda)
17684 {
17685 release_tree_vector (call_args);
17686 RETURN (error_mark_node);
17687 }
17688 }
17689
17690 function = unq;
17691 }
17692 }
17693 if (identifier_p (function))
17694 {
17695 if (complain & tf_error)
17696 unqualified_name_lookup_error (function);
17697 release_tree_vector (call_args);
17698 RETURN (error_mark_node);
17699 }
17700 }
17701
17702 /* Remember that there was a reference to this entity. */
17703 if (function != NULL_TREE
17704 && DECL_P (function)
17705 && !mark_used (function, complain) && !(complain & tf_error))
17706 {
17707 release_tree_vector (call_args);
17708 RETURN (error_mark_node);
17709 }
17710
17711 /* Put back tf_decltype for the actual call. */
17712 complain |= decltype_flag;
17713
17714 if (function == NULL_TREE)
17715 switch (CALL_EXPR_IFN (t))
17716 {
17717 case IFN_LAUNDER:
17718 gcc_assert (nargs == 1);
17719 if (vec_safe_length (call_args) != 1)
17720 {
17721 error_at (EXPR_LOC_OR_LOC (t, input_location),
17722 "wrong number of arguments to "
17723 "%<__builtin_launder%>");
17724 ret = error_mark_node;
17725 }
17726 else
17727 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17728 input_location),
17729 (*call_args)[0], complain);
17730 break;
17731
17732 default:
17733 /* Unsupported internal function with arguments. */
17734 gcc_unreachable ();
17735 }
17736 else if (TREE_CODE (function) == OFFSET_REF)
17737 ret = build_offset_ref_call_from_tree (function, &call_args,
17738 complain);
17739 else if (TREE_CODE (function) == COMPONENT_REF)
17740 {
17741 tree instance = TREE_OPERAND (function, 0);
17742 tree fn = TREE_OPERAND (function, 1);
17743
17744 if (processing_template_decl
17745 && (type_dependent_expression_p (instance)
17746 || (!BASELINK_P (fn)
17747 && TREE_CODE (fn) != FIELD_DECL)
17748 || type_dependent_expression_p (fn)
17749 || any_type_dependent_arguments_p (call_args)))
17750 ret = build_min_nt_call_vec (function, call_args);
17751 else if (!BASELINK_P (fn))
17752 ret = finish_call_expr (function, &call_args,
17753 /*disallow_virtual=*/false,
17754 /*koenig_p=*/false,
17755 complain);
17756 else
17757 ret = (build_new_method_call
17758 (instance, fn,
17759 &call_args, NULL_TREE,
17760 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17761 /*fn_p=*/NULL,
17762 complain));
17763 }
17764 else
17765 ret = finish_call_expr (function, &call_args,
17766 /*disallow_virtual=*/qualified_p,
17767 koenig_p,
17768 complain);
17769
17770 release_tree_vector (call_args);
17771
17772 if (ret != error_mark_node)
17773 {
17774 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17775 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17776 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17777 bool thk = CALL_FROM_THUNK_P (t);
17778 if (op || ord || rev || thk)
17779 {
17780 function = extract_call_expr (ret);
17781 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17782 CALL_EXPR_ORDERED_ARGS (function) = ord;
17783 CALL_EXPR_REVERSE_ARGS (function) = rev;
17784 if (thk)
17785 {
17786 CALL_FROM_THUNK_P (function) = true;
17787 /* The thunk location is not interesting. */
17788 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17789 }
17790 }
17791 }
17792
17793 RETURN (ret);
17794 }
17795
17796 case COND_EXPR:
17797 {
17798 tree cond = RECUR (TREE_OPERAND (t, 0));
17799 tree folded_cond = fold_non_dependent_expr (cond);
17800 tree exp1, exp2;
17801
17802 if (TREE_CODE (folded_cond) == INTEGER_CST)
17803 {
17804 if (integer_zerop (folded_cond))
17805 {
17806 ++c_inhibit_evaluation_warnings;
17807 exp1 = RECUR (TREE_OPERAND (t, 1));
17808 --c_inhibit_evaluation_warnings;
17809 exp2 = RECUR (TREE_OPERAND (t, 2));
17810 }
17811 else
17812 {
17813 exp1 = RECUR (TREE_OPERAND (t, 1));
17814 ++c_inhibit_evaluation_warnings;
17815 exp2 = RECUR (TREE_OPERAND (t, 2));
17816 --c_inhibit_evaluation_warnings;
17817 }
17818 cond = folded_cond;
17819 }
17820 else
17821 {
17822 exp1 = RECUR (TREE_OPERAND (t, 1));
17823 exp2 = RECUR (TREE_OPERAND (t, 2));
17824 }
17825
17826 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17827 cond, exp1, exp2, complain));
17828 }
17829
17830 case PSEUDO_DTOR_EXPR:
17831 {
17832 tree op0 = RECUR (TREE_OPERAND (t, 0));
17833 tree op1 = RECUR (TREE_OPERAND (t, 1));
17834 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17835 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17836 input_location));
17837 }
17838
17839 case TREE_LIST:
17840 {
17841 tree purpose, value, chain;
17842
17843 if (t == void_list_node)
17844 RETURN (t);
17845
17846 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17847 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17848 {
17849 /* We have pack expansions, so expand those and
17850 create a new list out of it. */
17851 tree purposevec = NULL_TREE;
17852 tree valuevec = NULL_TREE;
17853 tree chain;
17854 int i, len = -1;
17855
17856 /* Expand the argument expressions. */
17857 if (TREE_PURPOSE (t))
17858 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17859 complain, in_decl);
17860 if (TREE_VALUE (t))
17861 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17862 complain, in_decl);
17863
17864 /* Build the rest of the list. */
17865 chain = TREE_CHAIN (t);
17866 if (chain && chain != void_type_node)
17867 chain = RECUR (chain);
17868
17869 /* Determine the number of arguments. */
17870 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17871 {
17872 len = TREE_VEC_LENGTH (purposevec);
17873 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17874 }
17875 else if (TREE_CODE (valuevec) == TREE_VEC)
17876 len = TREE_VEC_LENGTH (valuevec);
17877 else
17878 {
17879 /* Since we only performed a partial substitution into
17880 the argument pack, we only RETURN (a single list
17881 node. */
17882 if (purposevec == TREE_PURPOSE (t)
17883 && valuevec == TREE_VALUE (t)
17884 && chain == TREE_CHAIN (t))
17885 RETURN (t);
17886
17887 RETURN (tree_cons (purposevec, valuevec, chain));
17888 }
17889
17890 /* Convert the argument vectors into a TREE_LIST */
17891 i = len;
17892 while (i > 0)
17893 {
17894 /* Grab the Ith values. */
17895 i--;
17896 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17897 : NULL_TREE;
17898 value
17899 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17900 : NULL_TREE;
17901
17902 /* Build the list (backwards). */
17903 chain = tree_cons (purpose, value, chain);
17904 }
17905
17906 RETURN (chain);
17907 }
17908
17909 purpose = TREE_PURPOSE (t);
17910 if (purpose)
17911 purpose = RECUR (purpose);
17912 value = TREE_VALUE (t);
17913 if (value)
17914 value = RECUR (value);
17915 chain = TREE_CHAIN (t);
17916 if (chain && chain != void_type_node)
17917 chain = RECUR (chain);
17918 if (purpose == TREE_PURPOSE (t)
17919 && value == TREE_VALUE (t)
17920 && chain == TREE_CHAIN (t))
17921 RETURN (t);
17922 RETURN (tree_cons (purpose, value, chain));
17923 }
17924
17925 case COMPONENT_REF:
17926 {
17927 tree object;
17928 tree object_type;
17929 tree member;
17930 tree r;
17931
17932 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17933 args, complain, in_decl);
17934 /* Remember that there was a reference to this entity. */
17935 if (DECL_P (object)
17936 && !mark_used (object, complain) && !(complain & tf_error))
17937 RETURN (error_mark_node);
17938 object_type = TREE_TYPE (object);
17939
17940 member = TREE_OPERAND (t, 1);
17941 if (BASELINK_P (member))
17942 member = tsubst_baselink (member,
17943 non_reference (TREE_TYPE (object)),
17944 args, complain, in_decl);
17945 else
17946 member = tsubst_copy (member, args, complain, in_decl);
17947 if (member == error_mark_node)
17948 RETURN (error_mark_node);
17949
17950 if (TREE_CODE (member) == FIELD_DECL)
17951 {
17952 r = finish_non_static_data_member (member, object, NULL_TREE);
17953 if (TREE_CODE (r) == COMPONENT_REF)
17954 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17955 RETURN (r);
17956 }
17957 else if (type_dependent_expression_p (object))
17958 /* We can't do much here. */;
17959 else if (!CLASS_TYPE_P (object_type))
17960 {
17961 if (scalarish_type_p (object_type))
17962 {
17963 tree s = NULL_TREE;
17964 tree dtor = member;
17965
17966 if (TREE_CODE (dtor) == SCOPE_REF)
17967 {
17968 s = TREE_OPERAND (dtor, 0);
17969 dtor = TREE_OPERAND (dtor, 1);
17970 }
17971 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17972 {
17973 dtor = TREE_OPERAND (dtor, 0);
17974 if (TYPE_P (dtor))
17975 RETURN (finish_pseudo_destructor_expr
17976 (object, s, dtor, input_location));
17977 }
17978 }
17979 }
17980 else if (TREE_CODE (member) == SCOPE_REF
17981 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17982 {
17983 /* Lookup the template functions now that we know what the
17984 scope is. */
17985 tree scope = TREE_OPERAND (member, 0);
17986 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17987 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17988 member = lookup_qualified_name (scope, tmpl,
17989 /*is_type_p=*/false,
17990 /*complain=*/false);
17991 if (BASELINK_P (member))
17992 {
17993 BASELINK_FUNCTIONS (member)
17994 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17995 args);
17996 member = (adjust_result_of_qualified_name_lookup
17997 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17998 object_type));
17999 }
18000 else
18001 {
18002 qualified_name_lookup_error (scope, tmpl, member,
18003 input_location);
18004 RETURN (error_mark_node);
18005 }
18006 }
18007 else if (TREE_CODE (member) == SCOPE_REF
18008 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18009 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18010 {
18011 if (complain & tf_error)
18012 {
18013 if (TYPE_P (TREE_OPERAND (member, 0)))
18014 error ("%qT is not a class or namespace",
18015 TREE_OPERAND (member, 0));
18016 else
18017 error ("%qD is not a class or namespace",
18018 TREE_OPERAND (member, 0));
18019 }
18020 RETURN (error_mark_node);
18021 }
18022
18023 r = finish_class_member_access_expr (object, member,
18024 /*template_p=*/false,
18025 complain);
18026 if (TREE_CODE (r) == COMPONENT_REF)
18027 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18028 RETURN (r);
18029 }
18030
18031 case THROW_EXPR:
18032 RETURN (build_throw
18033 (RECUR (TREE_OPERAND (t, 0))));
18034
18035 case CONSTRUCTOR:
18036 {
18037 vec<constructor_elt, va_gc> *n;
18038 constructor_elt *ce;
18039 unsigned HOST_WIDE_INT idx;
18040 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18041 bool process_index_p;
18042 int newlen;
18043 bool need_copy_p = false;
18044 tree r;
18045
18046 if (type == error_mark_node)
18047 RETURN (error_mark_node);
18048
18049 /* digest_init will do the wrong thing if we let it. */
18050 if (type && TYPE_PTRMEMFUNC_P (type))
18051 RETURN (t);
18052
18053 /* We do not want to process the index of aggregate
18054 initializers as they are identifier nodes which will be
18055 looked up by digest_init. */
18056 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18057
18058 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18059 newlen = vec_safe_length (n);
18060 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18061 {
18062 if (ce->index && process_index_p
18063 /* An identifier index is looked up in the type
18064 being initialized, not the current scope. */
18065 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18066 ce->index = RECUR (ce->index);
18067
18068 if (PACK_EXPANSION_P (ce->value))
18069 {
18070 /* Substitute into the pack expansion. */
18071 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18072 in_decl);
18073
18074 if (ce->value == error_mark_node
18075 || PACK_EXPANSION_P (ce->value))
18076 ;
18077 else if (TREE_VEC_LENGTH (ce->value) == 1)
18078 /* Just move the argument into place. */
18079 ce->value = TREE_VEC_ELT (ce->value, 0);
18080 else
18081 {
18082 /* Update the length of the final CONSTRUCTOR
18083 arguments vector, and note that we will need to
18084 copy.*/
18085 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18086 need_copy_p = true;
18087 }
18088 }
18089 else
18090 ce->value = RECUR (ce->value);
18091 }
18092
18093 if (need_copy_p)
18094 {
18095 vec<constructor_elt, va_gc> *old_n = n;
18096
18097 vec_alloc (n, newlen);
18098 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18099 {
18100 if (TREE_CODE (ce->value) == TREE_VEC)
18101 {
18102 int i, len = TREE_VEC_LENGTH (ce->value);
18103 for (i = 0; i < len; ++i)
18104 CONSTRUCTOR_APPEND_ELT (n, 0,
18105 TREE_VEC_ELT (ce->value, i));
18106 }
18107 else
18108 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18109 }
18110 }
18111
18112 r = build_constructor (init_list_type_node, n);
18113 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18114
18115 if (TREE_HAS_CONSTRUCTOR (t))
18116 {
18117 fcl_t cl = fcl_functional;
18118 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18119 cl = fcl_c99;
18120 RETURN (finish_compound_literal (type, r, complain, cl));
18121 }
18122
18123 TREE_TYPE (r) = type;
18124 RETURN (r);
18125 }
18126
18127 case TYPEID_EXPR:
18128 {
18129 tree operand_0 = TREE_OPERAND (t, 0);
18130 if (TYPE_P (operand_0))
18131 {
18132 operand_0 = tsubst (operand_0, args, complain, in_decl);
18133 RETURN (get_typeid (operand_0, complain));
18134 }
18135 else
18136 {
18137 operand_0 = RECUR (operand_0);
18138 RETURN (build_typeid (operand_0, complain));
18139 }
18140 }
18141
18142 case VAR_DECL:
18143 if (!args)
18144 RETURN (t);
18145 else if (DECL_PACK_P (t))
18146 {
18147 /* We don't build decls for an instantiation of a
18148 variadic capture proxy, we instantiate the elements
18149 when needed. */
18150 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
18151 return RECUR (DECL_VALUE_EXPR (t));
18152 }
18153 /* Fall through */
18154
18155 case PARM_DECL:
18156 {
18157 tree r = tsubst_copy (t, args, complain, in_decl);
18158 /* ??? We're doing a subset of finish_id_expression here. */
18159 if (VAR_P (r)
18160 && !processing_template_decl
18161 && !cp_unevaluated_operand
18162 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18163 && CP_DECL_THREAD_LOCAL_P (r))
18164 {
18165 if (tree wrap = get_tls_wrapper_fn (r))
18166 /* Replace an evaluated use of the thread_local variable with
18167 a call to its wrapper. */
18168 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18169 }
18170 else if (outer_automatic_var_p (r))
18171 r = process_outer_var_ref (r, complain);
18172
18173 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18174 /* If the original type was a reference, we'll be wrapped in
18175 the appropriate INDIRECT_REF. */
18176 r = convert_from_reference (r);
18177 RETURN (r);
18178 }
18179
18180 case VA_ARG_EXPR:
18181 {
18182 tree op0 = RECUR (TREE_OPERAND (t, 0));
18183 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18184 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18185 }
18186
18187 case OFFSETOF_EXPR:
18188 {
18189 tree object_ptr
18190 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18191 in_decl, /*function_p=*/false,
18192 /*integral_constant_expression_p=*/false);
18193 RETURN (finish_offsetof (object_ptr,
18194 RECUR (TREE_OPERAND (t, 0)),
18195 EXPR_LOCATION (t)));
18196 }
18197
18198 case ADDRESSOF_EXPR:
18199 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18200 RECUR (TREE_OPERAND (t, 0)), complain));
18201
18202 case TRAIT_EXPR:
18203 {
18204 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18205 complain, in_decl);
18206
18207 tree type2 = TRAIT_EXPR_TYPE2 (t);
18208 if (type2 && TREE_CODE (type2) == TREE_LIST)
18209 type2 = RECUR (type2);
18210 else if (type2)
18211 type2 = tsubst (type2, args, complain, in_decl);
18212
18213 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18214 }
18215
18216 case STMT_EXPR:
18217 {
18218 tree old_stmt_expr = cur_stmt_expr;
18219 tree stmt_expr = begin_stmt_expr ();
18220
18221 cur_stmt_expr = stmt_expr;
18222 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18223 integral_constant_expression_p);
18224 stmt_expr = finish_stmt_expr (stmt_expr, false);
18225 cur_stmt_expr = old_stmt_expr;
18226
18227 /* If the resulting list of expression statement is empty,
18228 fold it further into void_node. */
18229 if (empty_expr_stmt_p (stmt_expr))
18230 stmt_expr = void_node;
18231
18232 RETURN (stmt_expr);
18233 }
18234
18235 case LAMBDA_EXPR:
18236 {
18237 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18238
18239 RETURN (build_lambda_object (r));
18240 }
18241
18242 case TARGET_EXPR:
18243 /* We can get here for a constant initializer of non-dependent type.
18244 FIXME stop folding in cp_parser_initializer_clause. */
18245 {
18246 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18247 complain);
18248 RETURN (r);
18249 }
18250
18251 case TRANSACTION_EXPR:
18252 RETURN (tsubst_expr(t, args, complain, in_decl,
18253 integral_constant_expression_p));
18254
18255 case PAREN_EXPR:
18256 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18257
18258 case VEC_PERM_EXPR:
18259 {
18260 tree op0 = RECUR (TREE_OPERAND (t, 0));
18261 tree op1 = RECUR (TREE_OPERAND (t, 1));
18262 tree op2 = RECUR (TREE_OPERAND (t, 2));
18263 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18264 complain));
18265 }
18266
18267 case REQUIRES_EXPR:
18268 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18269
18270 default:
18271 /* Handle Objective-C++ constructs, if appropriate. */
18272 {
18273 tree subst
18274 = objcp_tsubst_copy_and_build (t, args, complain,
18275 in_decl, /*function_p=*/false);
18276 if (subst)
18277 RETURN (subst);
18278 }
18279 RETURN (tsubst_copy (t, args, complain, in_decl));
18280 }
18281
18282 #undef RECUR
18283 #undef RETURN
18284 out:
18285 input_location = loc;
18286 return retval;
18287 }
18288
18289 /* Verify that the instantiated ARGS are valid. For type arguments,
18290 make sure that the type's linkage is ok. For non-type arguments,
18291 make sure they are constants if they are integral or enumerations.
18292 Emit an error under control of COMPLAIN, and return TRUE on error. */
18293
18294 static bool
18295 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18296 {
18297 if (dependent_template_arg_p (t))
18298 return false;
18299 if (ARGUMENT_PACK_P (t))
18300 {
18301 tree vec = ARGUMENT_PACK_ARGS (t);
18302 int len = TREE_VEC_LENGTH (vec);
18303 bool result = false;
18304 int i;
18305
18306 for (i = 0; i < len; ++i)
18307 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18308 result = true;
18309 return result;
18310 }
18311 else if (TYPE_P (t))
18312 {
18313 /* [basic.link]: A name with no linkage (notably, the name
18314 of a class or enumeration declared in a local scope)
18315 shall not be used to declare an entity with linkage.
18316 This implies that names with no linkage cannot be used as
18317 template arguments
18318
18319 DR 757 relaxes this restriction for C++0x. */
18320 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18321 : no_linkage_check (t, /*relaxed_p=*/false));
18322
18323 if (nt)
18324 {
18325 /* DR 488 makes use of a type with no linkage cause
18326 type deduction to fail. */
18327 if (complain & tf_error)
18328 {
18329 if (TYPE_UNNAMED_P (nt))
18330 error ("%qT is/uses unnamed type", t);
18331 else
18332 error ("template argument for %qD uses local type %qT",
18333 tmpl, t);
18334 }
18335 return true;
18336 }
18337 /* In order to avoid all sorts of complications, we do not
18338 allow variably-modified types as template arguments. */
18339 else if (variably_modified_type_p (t, NULL_TREE))
18340 {
18341 if (complain & tf_error)
18342 error ("%qT is a variably modified type", t);
18343 return true;
18344 }
18345 }
18346 /* Class template and alias template arguments should be OK. */
18347 else if (DECL_TYPE_TEMPLATE_P (t))
18348 ;
18349 /* A non-type argument of integral or enumerated type must be a
18350 constant. */
18351 else if (TREE_TYPE (t)
18352 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18353 && !REFERENCE_REF_P (t)
18354 && !TREE_CONSTANT (t))
18355 {
18356 if (complain & tf_error)
18357 error ("integral expression %qE is not constant", t);
18358 return true;
18359 }
18360 return false;
18361 }
18362
18363 static bool
18364 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18365 {
18366 int ix, len = DECL_NTPARMS (tmpl);
18367 bool result = false;
18368
18369 for (ix = 0; ix != len; ix++)
18370 {
18371 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18372 result = true;
18373 }
18374 if (result && (complain & tf_error))
18375 error (" trying to instantiate %qD", tmpl);
18376 return result;
18377 }
18378
18379 /* We're out of SFINAE context now, so generate diagnostics for the access
18380 errors we saw earlier when instantiating D from TMPL and ARGS. */
18381
18382 static void
18383 recheck_decl_substitution (tree d, tree tmpl, tree args)
18384 {
18385 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18386 tree type = TREE_TYPE (pattern);
18387 location_t loc = input_location;
18388
18389 push_access_scope (d);
18390 push_deferring_access_checks (dk_no_deferred);
18391 input_location = DECL_SOURCE_LOCATION (pattern);
18392 tsubst (type, args, tf_warning_or_error, d);
18393 input_location = loc;
18394 pop_deferring_access_checks ();
18395 pop_access_scope (d);
18396 }
18397
18398 /* Instantiate the indicated variable, function, or alias template TMPL with
18399 the template arguments in TARG_PTR. */
18400
18401 static tree
18402 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18403 {
18404 tree targ_ptr = orig_args;
18405 tree fndecl;
18406 tree gen_tmpl;
18407 tree spec;
18408 bool access_ok = true;
18409
18410 if (tmpl == error_mark_node)
18411 return error_mark_node;
18412
18413 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18414
18415 /* If this function is a clone, handle it specially. */
18416 if (DECL_CLONED_FUNCTION_P (tmpl))
18417 {
18418 tree spec;
18419 tree clone;
18420
18421 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18422 DECL_CLONED_FUNCTION. */
18423 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18424 targ_ptr, complain);
18425 if (spec == error_mark_node)
18426 return error_mark_node;
18427
18428 /* Look for the clone. */
18429 FOR_EACH_CLONE (clone, spec)
18430 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18431 return clone;
18432 /* We should always have found the clone by now. */
18433 gcc_unreachable ();
18434 return NULL_TREE;
18435 }
18436
18437 if (targ_ptr == error_mark_node)
18438 return error_mark_node;
18439
18440 /* Check to see if we already have this specialization. */
18441 gen_tmpl = most_general_template (tmpl);
18442 if (TMPL_ARGS_DEPTH (targ_ptr)
18443 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18444 /* targ_ptr only has the innermost template args, so add the outer ones
18445 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18446 the case of a non-dependent call within a template definition). */
18447 targ_ptr = (add_outermost_template_args
18448 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18449 targ_ptr));
18450
18451 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18452 but it doesn't seem to be on the hot path. */
18453 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18454
18455 gcc_assert (tmpl == gen_tmpl
18456 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18457 == spec)
18458 || fndecl == NULL_TREE);
18459
18460 if (spec != NULL_TREE)
18461 {
18462 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18463 {
18464 if (complain & tf_error)
18465 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18466 return error_mark_node;
18467 }
18468 return spec;
18469 }
18470
18471 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18472 complain))
18473 return error_mark_node;
18474
18475 /* We are building a FUNCTION_DECL, during which the access of its
18476 parameters and return types have to be checked. However this
18477 FUNCTION_DECL which is the desired context for access checking
18478 is not built yet. We solve this chicken-and-egg problem by
18479 deferring all checks until we have the FUNCTION_DECL. */
18480 push_deferring_access_checks (dk_deferred);
18481
18482 /* Instantiation of the function happens in the context of the function
18483 template, not the context of the overload resolution we're doing. */
18484 push_to_top_level ();
18485 /* If there are dependent arguments, e.g. because we're doing partial
18486 ordering, make sure processing_template_decl stays set. */
18487 if (uses_template_parms (targ_ptr))
18488 ++processing_template_decl;
18489 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18490 {
18491 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18492 complain, gen_tmpl, true);
18493 push_nested_class (ctx);
18494 }
18495
18496 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18497
18498 fndecl = NULL_TREE;
18499 if (VAR_P (pattern))
18500 {
18501 /* We need to determine if we're using a partial or explicit
18502 specialization now, because the type of the variable could be
18503 different. */
18504 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18505 tree elt = most_specialized_partial_spec (tid, complain);
18506 if (elt == error_mark_node)
18507 pattern = error_mark_node;
18508 else if (elt)
18509 {
18510 tree partial_tmpl = TREE_VALUE (elt);
18511 tree partial_args = TREE_PURPOSE (elt);
18512 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18513 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18514 }
18515 }
18516
18517 /* Substitute template parameters to obtain the specialization. */
18518 if (fndecl == NULL_TREE)
18519 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18520 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18521 pop_nested_class ();
18522 pop_from_top_level ();
18523
18524 if (fndecl == error_mark_node)
18525 {
18526 pop_deferring_access_checks ();
18527 return error_mark_node;
18528 }
18529
18530 /* The DECL_TI_TEMPLATE should always be the immediate parent
18531 template, not the most general template. */
18532 DECL_TI_TEMPLATE (fndecl) = tmpl;
18533 DECL_TI_ARGS (fndecl) = targ_ptr;
18534
18535 /* Now we know the specialization, compute access previously
18536 deferred. Do no access control for inheriting constructors,
18537 as we already checked access for the inherited constructor. */
18538 if (!(flag_new_inheriting_ctors
18539 && DECL_INHERITED_CTOR (fndecl)))
18540 {
18541 push_access_scope (fndecl);
18542 if (!perform_deferred_access_checks (complain))
18543 access_ok = false;
18544 pop_access_scope (fndecl);
18545 }
18546 pop_deferring_access_checks ();
18547
18548 /* If we've just instantiated the main entry point for a function,
18549 instantiate all the alternate entry points as well. We do this
18550 by cloning the instantiation of the main entry point, not by
18551 instantiating the template clones. */
18552 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18553 clone_function_decl (fndecl, /*update_methods=*/false);
18554
18555 if (!access_ok)
18556 {
18557 if (!(complain & tf_error))
18558 {
18559 /* Remember to reinstantiate when we're out of SFINAE so the user
18560 can see the errors. */
18561 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18562 }
18563 return error_mark_node;
18564 }
18565 return fndecl;
18566 }
18567
18568 /* Wrapper for instantiate_template_1. */
18569
18570 tree
18571 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18572 {
18573 tree ret;
18574 timevar_push (TV_TEMPLATE_INST);
18575 ret = instantiate_template_1 (tmpl, orig_args, complain);
18576 timevar_pop (TV_TEMPLATE_INST);
18577 return ret;
18578 }
18579
18580 /* Instantiate the alias template TMPL with ARGS. Also push a template
18581 instantiation level, which instantiate_template doesn't do because
18582 functions and variables have sufficient context established by the
18583 callers. */
18584
18585 static tree
18586 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18587 {
18588 struct pending_template *old_last_pend = last_pending_template;
18589 struct tinst_level *old_error_tinst = last_error_tinst_level;
18590 if (tmpl == error_mark_node || args == error_mark_node)
18591 return error_mark_node;
18592 tree tinst = build_tree_list (tmpl, args);
18593 if (!push_tinst_level (tinst))
18594 {
18595 ggc_free (tinst);
18596 return error_mark_node;
18597 }
18598
18599 args =
18600 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18601 args, tmpl, complain,
18602 /*require_all_args=*/true,
18603 /*use_default_args=*/true);
18604
18605 tree r = instantiate_template (tmpl, args, complain);
18606 pop_tinst_level ();
18607 /* We can't free this if a pending_template entry or last_error_tinst_level
18608 is pointing at it. */
18609 if (last_pending_template == old_last_pend
18610 && last_error_tinst_level == old_error_tinst)
18611 ggc_free (tinst);
18612
18613 return r;
18614 }
18615
18616 /* PARM is a template parameter pack for FN. Returns true iff
18617 PARM is used in a deducible way in the argument list of FN. */
18618
18619 static bool
18620 pack_deducible_p (tree parm, tree fn)
18621 {
18622 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18623 for (; t; t = TREE_CHAIN (t))
18624 {
18625 tree type = TREE_VALUE (t);
18626 tree packs;
18627 if (!PACK_EXPANSION_P (type))
18628 continue;
18629 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18630 packs; packs = TREE_CHAIN (packs))
18631 if (template_args_equal (TREE_VALUE (packs), parm))
18632 {
18633 /* The template parameter pack is used in a function parameter
18634 pack. If this is the end of the parameter list, the
18635 template parameter pack is deducible. */
18636 if (TREE_CHAIN (t) == void_list_node)
18637 return true;
18638 else
18639 /* Otherwise, not. Well, it could be deduced from
18640 a non-pack parameter, but doing so would end up with
18641 a deduction mismatch, so don't bother. */
18642 return false;
18643 }
18644 }
18645 /* The template parameter pack isn't used in any function parameter
18646 packs, but it might be used deeper, e.g. tuple<Args...>. */
18647 return true;
18648 }
18649
18650 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18651 NARGS elements of the arguments that are being used when calling
18652 it. TARGS is a vector into which the deduced template arguments
18653 are placed.
18654
18655 Returns either a FUNCTION_DECL for the matching specialization of FN or
18656 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18657 true, diagnostics will be printed to explain why it failed.
18658
18659 If FN is a conversion operator, or we are trying to produce a specific
18660 specialization, RETURN_TYPE is the return type desired.
18661
18662 The EXPLICIT_TARGS are explicit template arguments provided via a
18663 template-id.
18664
18665 The parameter STRICT is one of:
18666
18667 DEDUCE_CALL:
18668 We are deducing arguments for a function call, as in
18669 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18670 deducing arguments for a call to the result of a conversion
18671 function template, as in [over.call.object].
18672
18673 DEDUCE_CONV:
18674 We are deducing arguments for a conversion function, as in
18675 [temp.deduct.conv].
18676
18677 DEDUCE_EXACT:
18678 We are deducing arguments when doing an explicit instantiation
18679 as in [temp.explicit], when determining an explicit specialization
18680 as in [temp.expl.spec], or when taking the address of a function
18681 template, as in [temp.deduct.funcaddr]. */
18682
18683 tree
18684 fn_type_unification (tree fn,
18685 tree explicit_targs,
18686 tree targs,
18687 const tree *args,
18688 unsigned int nargs,
18689 tree return_type,
18690 unification_kind_t strict,
18691 int flags,
18692 bool explain_p,
18693 bool decltype_p)
18694 {
18695 tree parms;
18696 tree fntype;
18697 tree decl = NULL_TREE;
18698 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18699 bool ok;
18700 static int deduction_depth;
18701 struct pending_template *old_last_pend = last_pending_template;
18702 struct tinst_level *old_error_tinst = last_error_tinst_level;
18703
18704 tree orig_fn = fn;
18705 if (flag_new_inheriting_ctors)
18706 fn = strip_inheriting_ctors (fn);
18707
18708 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18709 tree tinst;
18710 tree r = error_mark_node;
18711
18712 tree full_targs = targs;
18713 if (TMPL_ARGS_DEPTH (targs)
18714 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18715 full_targs = (add_outermost_template_args
18716 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18717 targs));
18718
18719 if (decltype_p)
18720 complain |= tf_decltype;
18721
18722 /* In C++0x, it's possible to have a function template whose type depends
18723 on itself recursively. This is most obvious with decltype, but can also
18724 occur with enumeration scope (c++/48969). So we need to catch infinite
18725 recursion and reject the substitution at deduction time; this function
18726 will return error_mark_node for any repeated substitution.
18727
18728 This also catches excessive recursion such as when f<N> depends on
18729 f<N-1> across all integers, and returns error_mark_node for all the
18730 substitutions back up to the initial one.
18731
18732 This is, of course, not reentrant. */
18733 if (excessive_deduction_depth)
18734 return error_mark_node;
18735 tinst = build_tree_list (fn, NULL_TREE);
18736 ++deduction_depth;
18737
18738 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18739
18740 fntype = TREE_TYPE (fn);
18741 if (explicit_targs)
18742 {
18743 /* [temp.deduct]
18744
18745 The specified template arguments must match the template
18746 parameters in kind (i.e., type, nontype, template), and there
18747 must not be more arguments than there are parameters;
18748 otherwise type deduction fails.
18749
18750 Nontype arguments must match the types of the corresponding
18751 nontype template parameters, or must be convertible to the
18752 types of the corresponding nontype parameters as specified in
18753 _temp.arg.nontype_, otherwise type deduction fails.
18754
18755 All references in the function type of the function template
18756 to the corresponding template parameters are replaced by the
18757 specified template argument values. If a substitution in a
18758 template parameter or in the function type of the function
18759 template results in an invalid type, type deduction fails. */
18760 int i, len = TREE_VEC_LENGTH (tparms);
18761 location_t loc = input_location;
18762 bool incomplete = false;
18763
18764 if (explicit_targs == error_mark_node)
18765 goto fail;
18766
18767 if (TMPL_ARGS_DEPTH (explicit_targs)
18768 < TMPL_ARGS_DEPTH (full_targs))
18769 explicit_targs = add_outermost_template_args (full_targs,
18770 explicit_targs);
18771
18772 /* Adjust any explicit template arguments before entering the
18773 substitution context. */
18774 explicit_targs
18775 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18776 complain,
18777 /*require_all_args=*/false,
18778 /*use_default_args=*/false));
18779 if (explicit_targs == error_mark_node)
18780 goto fail;
18781
18782 /* Substitute the explicit args into the function type. This is
18783 necessary so that, for instance, explicitly declared function
18784 arguments can match null pointed constants. If we were given
18785 an incomplete set of explicit args, we must not do semantic
18786 processing during substitution as we could create partial
18787 instantiations. */
18788 for (i = 0; i < len; i++)
18789 {
18790 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18791 bool parameter_pack = false;
18792 tree targ = TREE_VEC_ELT (explicit_targs, i);
18793
18794 /* Dig out the actual parm. */
18795 if (TREE_CODE (parm) == TYPE_DECL
18796 || TREE_CODE (parm) == TEMPLATE_DECL)
18797 {
18798 parm = TREE_TYPE (parm);
18799 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18800 }
18801 else if (TREE_CODE (parm) == PARM_DECL)
18802 {
18803 parm = DECL_INITIAL (parm);
18804 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18805 }
18806
18807 if (!parameter_pack && targ == NULL_TREE)
18808 /* No explicit argument for this template parameter. */
18809 incomplete = true;
18810
18811 if (parameter_pack && pack_deducible_p (parm, fn))
18812 {
18813 /* Mark the argument pack as "incomplete". We could
18814 still deduce more arguments during unification.
18815 We remove this mark in type_unification_real. */
18816 if (targ)
18817 {
18818 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18819 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18820 = ARGUMENT_PACK_ARGS (targ);
18821 }
18822
18823 /* We have some incomplete argument packs. */
18824 incomplete = true;
18825 }
18826 }
18827
18828 TREE_VALUE (tinst) = explicit_targs;
18829 if (!push_tinst_level (tinst))
18830 {
18831 excessive_deduction_depth = true;
18832 goto fail;
18833 }
18834 processing_template_decl += incomplete;
18835 input_location = DECL_SOURCE_LOCATION (fn);
18836 /* Ignore any access checks; we'll see them again in
18837 instantiate_template and they might have the wrong
18838 access path at this point. */
18839 push_deferring_access_checks (dk_deferred);
18840 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18841 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18842 pop_deferring_access_checks ();
18843 input_location = loc;
18844 processing_template_decl -= incomplete;
18845 pop_tinst_level ();
18846
18847 if (fntype == error_mark_node)
18848 goto fail;
18849
18850 /* Place the explicitly specified arguments in TARGS. */
18851 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18852 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18853 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18854 }
18855
18856 /* Never do unification on the 'this' parameter. */
18857 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18858
18859 if (return_type && strict == DEDUCE_CALL)
18860 {
18861 /* We're deducing for a call to the result of a template conversion
18862 function. The parms we really want are in return_type. */
18863 if (POINTER_TYPE_P (return_type))
18864 return_type = TREE_TYPE (return_type);
18865 parms = TYPE_ARG_TYPES (return_type);
18866 }
18867 else if (return_type)
18868 {
18869 tree *new_args;
18870
18871 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18872 new_args = XALLOCAVEC (tree, nargs + 1);
18873 new_args[0] = return_type;
18874 memcpy (new_args + 1, args, nargs * sizeof (tree));
18875 args = new_args;
18876 ++nargs;
18877 }
18878
18879 /* We allow incomplete unification without an error message here
18880 because the standard doesn't seem to explicitly prohibit it. Our
18881 callers must be ready to deal with unification failures in any
18882 event. */
18883
18884 TREE_VALUE (tinst) = targs;
18885 /* If we aren't explaining yet, push tinst context so we can see where
18886 any errors (e.g. from class instantiations triggered by instantiation
18887 of default template arguments) come from. If we are explaining, this
18888 context is redundant. */
18889 if (!explain_p && !push_tinst_level (tinst))
18890 {
18891 excessive_deduction_depth = true;
18892 goto fail;
18893 }
18894
18895 /* type_unification_real will pass back any access checks from default
18896 template argument substitution. */
18897 vec<deferred_access_check, va_gc> *checks;
18898 checks = NULL;
18899
18900 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18901 full_targs, parms, args, nargs, /*subr=*/0,
18902 strict, flags, &checks, explain_p);
18903 if (!explain_p)
18904 pop_tinst_level ();
18905 if (!ok)
18906 goto fail;
18907
18908 /* Now that we have bindings for all of the template arguments,
18909 ensure that the arguments deduced for the template template
18910 parameters have compatible template parameter lists. We cannot
18911 check this property before we have deduced all template
18912 arguments, because the template parameter types of a template
18913 template parameter might depend on prior template parameters
18914 deduced after the template template parameter. The following
18915 ill-formed example illustrates this issue:
18916
18917 template<typename T, template<T> class C> void f(C<5>, T);
18918
18919 template<int N> struct X {};
18920
18921 void g() {
18922 f(X<5>(), 5l); // error: template argument deduction fails
18923 }
18924
18925 The template parameter list of 'C' depends on the template type
18926 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18927 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18928 time that we deduce 'C'. */
18929 if (!template_template_parm_bindings_ok_p
18930 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18931 {
18932 unify_inconsistent_template_template_parameters (explain_p);
18933 goto fail;
18934 }
18935
18936 /* All is well so far. Now, check:
18937
18938 [temp.deduct]
18939
18940 When all template arguments have been deduced, all uses of
18941 template parameters in nondeduced contexts are replaced with
18942 the corresponding deduced argument values. If the
18943 substitution results in an invalid type, as described above,
18944 type deduction fails. */
18945 TREE_VALUE (tinst) = targs;
18946 if (!push_tinst_level (tinst))
18947 {
18948 excessive_deduction_depth = true;
18949 goto fail;
18950 }
18951
18952 /* Also collect access checks from the instantiation. */
18953 reopen_deferring_access_checks (checks);
18954
18955 decl = instantiate_template (fn, targs, complain);
18956
18957 checks = get_deferred_access_checks ();
18958 pop_deferring_access_checks ();
18959
18960 pop_tinst_level ();
18961
18962 if (decl == error_mark_node)
18963 goto fail;
18964
18965 /* Now perform any access checks encountered during substitution. */
18966 push_access_scope (decl);
18967 ok = perform_access_checks (checks, complain);
18968 pop_access_scope (decl);
18969 if (!ok)
18970 goto fail;
18971
18972 /* If we're looking for an exact match, check that what we got
18973 is indeed an exact match. It might not be if some template
18974 parameters are used in non-deduced contexts. But don't check
18975 for an exact match if we have dependent template arguments;
18976 in that case we're doing partial ordering, and we already know
18977 that we have two candidates that will provide the actual type. */
18978 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18979 {
18980 tree substed = TREE_TYPE (decl);
18981 unsigned int i;
18982
18983 tree sarg
18984 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18985 if (return_type)
18986 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18987 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18988 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18989 {
18990 unify_type_mismatch (explain_p, args[i],
18991 TREE_VALUE (sarg));
18992 goto fail;
18993 }
18994 }
18995
18996 /* After doing deduction with the inherited constructor, actually return an
18997 instantiation of the inheriting constructor. */
18998 if (orig_fn != fn)
18999 decl = instantiate_template (orig_fn, targs, complain);
19000
19001 r = decl;
19002
19003 fail:
19004 --deduction_depth;
19005 if (excessive_deduction_depth)
19006 {
19007 if (deduction_depth == 0)
19008 /* Reset once we're all the way out. */
19009 excessive_deduction_depth = false;
19010 }
19011
19012 /* We can't free this if a pending_template entry or last_error_tinst_level
19013 is pointing at it. */
19014 if (last_pending_template == old_last_pend
19015 && last_error_tinst_level == old_error_tinst)
19016 ggc_free (tinst);
19017
19018 return r;
19019 }
19020
19021 /* Adjust types before performing type deduction, as described in
19022 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19023 sections are symmetric. PARM is the type of a function parameter
19024 or the return type of the conversion function. ARG is the type of
19025 the argument passed to the call, or the type of the value
19026 initialized with the result of the conversion function.
19027 ARG_EXPR is the original argument expression, which may be null. */
19028
19029 static int
19030 maybe_adjust_types_for_deduction (unification_kind_t strict,
19031 tree* parm,
19032 tree* arg,
19033 tree arg_expr)
19034 {
19035 int result = 0;
19036
19037 switch (strict)
19038 {
19039 case DEDUCE_CALL:
19040 break;
19041
19042 case DEDUCE_CONV:
19043 /* Swap PARM and ARG throughout the remainder of this
19044 function; the handling is precisely symmetric since PARM
19045 will initialize ARG rather than vice versa. */
19046 std::swap (parm, arg);
19047 break;
19048
19049 case DEDUCE_EXACT:
19050 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19051 too, but here handle it by stripping the reference from PARM
19052 rather than by adding it to ARG. */
19053 if (TREE_CODE (*parm) == REFERENCE_TYPE
19054 && TYPE_REF_IS_RVALUE (*parm)
19055 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19056 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19057 && TREE_CODE (*arg) == REFERENCE_TYPE
19058 && !TYPE_REF_IS_RVALUE (*arg))
19059 *parm = TREE_TYPE (*parm);
19060 /* Nothing else to do in this case. */
19061 return 0;
19062
19063 default:
19064 gcc_unreachable ();
19065 }
19066
19067 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19068 {
19069 /* [temp.deduct.call]
19070
19071 If P is not a reference type:
19072
19073 --If A is an array type, the pointer type produced by the
19074 array-to-pointer standard conversion (_conv.array_) is
19075 used in place of A for type deduction; otherwise,
19076
19077 --If A is a function type, the pointer type produced by
19078 the function-to-pointer standard conversion
19079 (_conv.func_) is used in place of A for type deduction;
19080 otherwise,
19081
19082 --If A is a cv-qualified type, the top level
19083 cv-qualifiers of A's type are ignored for type
19084 deduction. */
19085 if (TREE_CODE (*arg) == ARRAY_TYPE)
19086 *arg = build_pointer_type (TREE_TYPE (*arg));
19087 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19088 *arg = build_pointer_type (*arg);
19089 else
19090 *arg = TYPE_MAIN_VARIANT (*arg);
19091 }
19092
19093 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19094 reference to a cv-unqualified template parameter that does not represent a
19095 template parameter of a class template (during class template argument
19096 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19097 an lvalue, the type "lvalue reference to A" is used in place of A for type
19098 deduction. */
19099 if (TREE_CODE (*parm) == REFERENCE_TYPE
19100 && TYPE_REF_IS_RVALUE (*parm)
19101 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19102 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19103 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19104 && (arg_expr ? lvalue_p (arg_expr)
19105 /* try_one_overload doesn't provide an arg_expr, but
19106 functions are always lvalues. */
19107 : TREE_CODE (*arg) == FUNCTION_TYPE))
19108 *arg = build_reference_type (*arg);
19109
19110 /* [temp.deduct.call]
19111
19112 If P is a cv-qualified type, the top level cv-qualifiers
19113 of P's type are ignored for type deduction. If P is a
19114 reference type, the type referred to by P is used for
19115 type deduction. */
19116 *parm = TYPE_MAIN_VARIANT (*parm);
19117 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19118 {
19119 *parm = TREE_TYPE (*parm);
19120 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19121 }
19122
19123 /* DR 322. For conversion deduction, remove a reference type on parm
19124 too (which has been swapped into ARG). */
19125 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19126 *arg = TREE_TYPE (*arg);
19127
19128 return result;
19129 }
19130
19131 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19132 template which does contain any deducible template parameters; check if
19133 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19134 unify_one_argument. */
19135
19136 static int
19137 check_non_deducible_conversion (tree parm, tree arg, int strict,
19138 int flags, bool explain_p)
19139 {
19140 tree type;
19141
19142 if (!TYPE_P (arg))
19143 type = TREE_TYPE (arg);
19144 else
19145 type = arg;
19146
19147 if (same_type_p (parm, type))
19148 return unify_success (explain_p);
19149
19150 if (strict == DEDUCE_CONV)
19151 {
19152 if (can_convert_arg (type, parm, NULL_TREE, flags,
19153 explain_p ? tf_warning_or_error : tf_none))
19154 return unify_success (explain_p);
19155 }
19156 else if (strict != DEDUCE_EXACT)
19157 {
19158 if (can_convert_arg (parm, type,
19159 TYPE_P (arg) ? NULL_TREE : arg,
19160 flags, explain_p ? tf_warning_or_error : tf_none))
19161 return unify_success (explain_p);
19162 }
19163
19164 if (strict == DEDUCE_EXACT)
19165 return unify_type_mismatch (explain_p, parm, arg);
19166 else
19167 return unify_arg_conversion (explain_p, parm, type, arg);
19168 }
19169
19170 static bool uses_deducible_template_parms (tree type);
19171
19172 /* Returns true iff the expression EXPR is one from which a template
19173 argument can be deduced. In other words, if it's an undecorated
19174 use of a template non-type parameter. */
19175
19176 static bool
19177 deducible_expression (tree expr)
19178 {
19179 /* Strip implicit conversions. */
19180 while (CONVERT_EXPR_P (expr))
19181 expr = TREE_OPERAND (expr, 0);
19182 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19183 }
19184
19185 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19186 deducible way; that is, if it has a max value of <PARM> - 1. */
19187
19188 static bool
19189 deducible_array_bound (tree domain)
19190 {
19191 if (domain == NULL_TREE)
19192 return false;
19193
19194 tree max = TYPE_MAX_VALUE (domain);
19195 if (TREE_CODE (max) != MINUS_EXPR)
19196 return false;
19197
19198 return deducible_expression (TREE_OPERAND (max, 0));
19199 }
19200
19201 /* Returns true iff the template arguments ARGS use a template parameter
19202 in a deducible way. */
19203
19204 static bool
19205 deducible_template_args (tree args)
19206 {
19207 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19208 {
19209 bool deducible;
19210 tree elt = TREE_VEC_ELT (args, i);
19211 if (ARGUMENT_PACK_P (elt))
19212 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19213 else
19214 {
19215 if (PACK_EXPANSION_P (elt))
19216 elt = PACK_EXPANSION_PATTERN (elt);
19217 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19218 deducible = true;
19219 else if (TYPE_P (elt))
19220 deducible = uses_deducible_template_parms (elt);
19221 else
19222 deducible = deducible_expression (elt);
19223 }
19224 if (deducible)
19225 return true;
19226 }
19227 return false;
19228 }
19229
19230 /* Returns true iff TYPE contains any deducible references to template
19231 parameters, as per 14.8.2.5. */
19232
19233 static bool
19234 uses_deducible_template_parms (tree type)
19235 {
19236 if (PACK_EXPANSION_P (type))
19237 type = PACK_EXPANSION_PATTERN (type);
19238
19239 /* T
19240 cv-list T
19241 TT<T>
19242 TT<i>
19243 TT<> */
19244 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19245 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19246 return true;
19247
19248 /* T*
19249 T&
19250 T&& */
19251 if (POINTER_TYPE_P (type))
19252 return uses_deducible_template_parms (TREE_TYPE (type));
19253
19254 /* T[integer-constant ]
19255 type [i] */
19256 if (TREE_CODE (type) == ARRAY_TYPE)
19257 return (uses_deducible_template_parms (TREE_TYPE (type))
19258 || deducible_array_bound (TYPE_DOMAIN (type)));
19259
19260 /* T type ::*
19261 type T::*
19262 T T::*
19263 T (type ::*)()
19264 type (T::*)()
19265 type (type ::*)(T)
19266 type (T::*)(T)
19267 T (type ::*)(T)
19268 T (T::*)()
19269 T (T::*)(T) */
19270 if (TYPE_PTRMEM_P (type))
19271 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19272 || (uses_deducible_template_parms
19273 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19274
19275 /* template-name <T> (where template-name refers to a class template)
19276 template-name <i> (where template-name refers to a class template) */
19277 if (CLASS_TYPE_P (type)
19278 && CLASSTYPE_TEMPLATE_INFO (type)
19279 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19280 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19281 (CLASSTYPE_TI_ARGS (type)));
19282
19283 /* type (T)
19284 T()
19285 T(T) */
19286 if (TREE_CODE (type) == FUNCTION_TYPE
19287 || TREE_CODE (type) == METHOD_TYPE)
19288 {
19289 if (uses_deducible_template_parms (TREE_TYPE (type)))
19290 return true;
19291 tree parm = TYPE_ARG_TYPES (type);
19292 if (TREE_CODE (type) == METHOD_TYPE)
19293 parm = TREE_CHAIN (parm);
19294 for (; parm; parm = TREE_CHAIN (parm))
19295 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19296 return true;
19297 }
19298
19299 return false;
19300 }
19301
19302 /* Subroutine of type_unification_real and unify_pack_expansion to
19303 handle unification of a single P/A pair. Parameters are as
19304 for those functions. */
19305
19306 static int
19307 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19308 int subr, unification_kind_t strict,
19309 bool explain_p)
19310 {
19311 tree arg_expr = NULL_TREE;
19312 int arg_strict;
19313
19314 if (arg == error_mark_node || parm == error_mark_node)
19315 return unify_invalid (explain_p);
19316 if (arg == unknown_type_node)
19317 /* We can't deduce anything from this, but we might get all the
19318 template args from other function args. */
19319 return unify_success (explain_p);
19320
19321 /* Implicit conversions (Clause 4) will be performed on a function
19322 argument to convert it to the type of the corresponding function
19323 parameter if the parameter type contains no template-parameters that
19324 participate in template argument deduction. */
19325 if (strict != DEDUCE_EXACT
19326 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19327 /* For function parameters with no deducible template parameters,
19328 just return. We'll check non-dependent conversions later. */
19329 return unify_success (explain_p);
19330
19331 switch (strict)
19332 {
19333 case DEDUCE_CALL:
19334 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19335 | UNIFY_ALLOW_MORE_CV_QUAL
19336 | UNIFY_ALLOW_DERIVED);
19337 break;
19338
19339 case DEDUCE_CONV:
19340 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19341 break;
19342
19343 case DEDUCE_EXACT:
19344 arg_strict = UNIFY_ALLOW_NONE;
19345 break;
19346
19347 default:
19348 gcc_unreachable ();
19349 }
19350
19351 /* We only do these transformations if this is the top-level
19352 parameter_type_list in a call or declaration matching; in other
19353 situations (nested function declarators, template argument lists) we
19354 won't be comparing a type to an expression, and we don't do any type
19355 adjustments. */
19356 if (!subr)
19357 {
19358 if (!TYPE_P (arg))
19359 {
19360 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19361 if (type_unknown_p (arg))
19362 {
19363 /* [temp.deduct.type] A template-argument can be
19364 deduced from a pointer to function or pointer
19365 to member function argument if the set of
19366 overloaded functions does not contain function
19367 templates and at most one of a set of
19368 overloaded functions provides a unique
19369 match. */
19370 resolve_overloaded_unification (tparms, targs, parm,
19371 arg, strict,
19372 arg_strict, explain_p);
19373 /* If a unique match was not found, this is a
19374 non-deduced context, so we still succeed. */
19375 return unify_success (explain_p);
19376 }
19377
19378 arg_expr = arg;
19379 arg = unlowered_expr_type (arg);
19380 if (arg == error_mark_node)
19381 return unify_invalid (explain_p);
19382 }
19383
19384 arg_strict |=
19385 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19386 }
19387 else
19388 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19389 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19390 return unify_template_argument_mismatch (explain_p, parm, arg);
19391
19392 /* For deduction from an init-list we need the actual list. */
19393 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19394 arg = arg_expr;
19395 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19396 }
19397
19398 /* for_each_template_parm callback that always returns 0. */
19399
19400 static int
19401 zero_r (tree, void *)
19402 {
19403 return 0;
19404 }
19405
19406 /* for_each_template_parm any_fn callback to handle deduction of a template
19407 type argument from the type of an array bound. */
19408
19409 static int
19410 array_deduction_r (tree t, void *data)
19411 {
19412 tree_pair_p d = (tree_pair_p)data;
19413 tree &tparms = d->purpose;
19414 tree &targs = d->value;
19415
19416 if (TREE_CODE (t) == ARRAY_TYPE)
19417 if (tree dom = TYPE_DOMAIN (t))
19418 if (tree max = TYPE_MAX_VALUE (dom))
19419 {
19420 if (TREE_CODE (max) == MINUS_EXPR)
19421 max = TREE_OPERAND (max, 0);
19422 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19423 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19424 UNIFY_ALLOW_NONE, /*explain*/false);
19425 }
19426
19427 /* Keep walking. */
19428 return 0;
19429 }
19430
19431 /* Try to deduce any not-yet-deduced template type arguments from the type of
19432 an array bound. This is handled separately from unify because 14.8.2.5 says
19433 "The type of a type parameter is only deduced from an array bound if it is
19434 not otherwise deduced." */
19435
19436 static void
19437 try_array_deduction (tree tparms, tree targs, tree parm)
19438 {
19439 tree_pair_s data = { tparms, targs };
19440 hash_set<tree> visited;
19441 for_each_template_parm (parm, zero_r, &data, &visited,
19442 /*nondeduced*/false, array_deduction_r);
19443 }
19444
19445 /* Most parms like fn_type_unification.
19446
19447 If SUBR is 1, we're being called recursively (to unify the
19448 arguments of a function or method parameter of a function
19449 template).
19450
19451 CHECKS is a pointer to a vector of access checks encountered while
19452 substituting default template arguments. */
19453
19454 static int
19455 type_unification_real (tree tparms,
19456 tree full_targs,
19457 tree xparms,
19458 const tree *xargs,
19459 unsigned int xnargs,
19460 int subr,
19461 unification_kind_t strict,
19462 int flags,
19463 vec<deferred_access_check, va_gc> **checks,
19464 bool explain_p)
19465 {
19466 tree parm, arg;
19467 int i;
19468 int ntparms = TREE_VEC_LENGTH (tparms);
19469 int saw_undeduced = 0;
19470 tree parms;
19471 const tree *args;
19472 unsigned int nargs;
19473 unsigned int ia;
19474
19475 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19476 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19477 gcc_assert (ntparms > 0);
19478
19479 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19480
19481 /* Reset the number of non-defaulted template arguments contained
19482 in TARGS. */
19483 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19484
19485 again:
19486 parms = xparms;
19487 args = xargs;
19488 nargs = xnargs;
19489
19490 ia = 0;
19491 while (parms && parms != void_list_node
19492 && ia < nargs)
19493 {
19494 parm = TREE_VALUE (parms);
19495
19496 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19497 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19498 /* For a function parameter pack that occurs at the end of the
19499 parameter-declaration-list, the type A of each remaining
19500 argument of the call is compared with the type P of the
19501 declarator-id of the function parameter pack. */
19502 break;
19503
19504 parms = TREE_CHAIN (parms);
19505
19506 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19507 /* For a function parameter pack that does not occur at the
19508 end of the parameter-declaration-list, the type of the
19509 parameter pack is a non-deduced context. */
19510 continue;
19511
19512 arg = args[ia];
19513 ++ia;
19514
19515 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19516 explain_p))
19517 return 1;
19518 }
19519
19520 if (parms
19521 && parms != void_list_node
19522 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19523 {
19524 /* Unify the remaining arguments with the pack expansion type. */
19525 tree argvec;
19526 tree parmvec = make_tree_vec (1);
19527
19528 /* Allocate a TREE_VEC and copy in all of the arguments */
19529 argvec = make_tree_vec (nargs - ia);
19530 for (i = 0; ia < nargs; ++ia, ++i)
19531 TREE_VEC_ELT (argvec, i) = args[ia];
19532
19533 /* Copy the parameter into parmvec. */
19534 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19535 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19536 /*subr=*/subr, explain_p))
19537 return 1;
19538
19539 /* Advance to the end of the list of parameters. */
19540 parms = TREE_CHAIN (parms);
19541 }
19542
19543 /* Fail if we've reached the end of the parm list, and more args
19544 are present, and the parm list isn't variadic. */
19545 if (ia < nargs && parms == void_list_node)
19546 return unify_too_many_arguments (explain_p, nargs, ia);
19547 /* Fail if parms are left and they don't have default values and
19548 they aren't all deduced as empty packs (c++/57397). This is
19549 consistent with sufficient_parms_p. */
19550 if (parms && parms != void_list_node
19551 && TREE_PURPOSE (parms) == NULL_TREE)
19552 {
19553 unsigned int count = nargs;
19554 tree p = parms;
19555 bool type_pack_p;
19556 do
19557 {
19558 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19559 if (!type_pack_p)
19560 count++;
19561 p = TREE_CHAIN (p);
19562 }
19563 while (p && p != void_list_node);
19564 if (count != nargs)
19565 return unify_too_few_arguments (explain_p, ia, count,
19566 type_pack_p);
19567 }
19568
19569 if (!subr)
19570 {
19571 tsubst_flags_t complain = (explain_p
19572 ? tf_warning_or_error
19573 : tf_none);
19574 bool tried_array_deduction = (cxx_dialect < cxx17);
19575
19576 for (i = 0; i < ntparms; i++)
19577 {
19578 tree targ = TREE_VEC_ELT (targs, i);
19579 tree tparm = TREE_VEC_ELT (tparms, i);
19580
19581 /* Clear the "incomplete" flags on all argument packs now so that
19582 substituting them into later default arguments works. */
19583 if (targ && ARGUMENT_PACK_P (targ))
19584 {
19585 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19586 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19587 }
19588
19589 if (targ || tparm == error_mark_node)
19590 continue;
19591 tparm = TREE_VALUE (tparm);
19592
19593 if (TREE_CODE (tparm) == TYPE_DECL
19594 && !tried_array_deduction)
19595 {
19596 try_array_deduction (tparms, targs, xparms);
19597 tried_array_deduction = true;
19598 if (TREE_VEC_ELT (targs, i))
19599 continue;
19600 }
19601
19602 /* If this is an undeduced nontype parameter that depends on
19603 a type parameter, try another pass; its type may have been
19604 deduced from a later argument than the one from which
19605 this parameter can be deduced. */
19606 if (TREE_CODE (tparm) == PARM_DECL
19607 && uses_template_parms (TREE_TYPE (tparm))
19608 && saw_undeduced < 2)
19609 {
19610 saw_undeduced = 1;
19611 continue;
19612 }
19613
19614 /* Core issue #226 (C++0x) [temp.deduct]:
19615
19616 If a template argument has not been deduced, its
19617 default template argument, if any, is used.
19618
19619 When we are in C++98 mode, TREE_PURPOSE will either
19620 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19621 to explicitly check cxx_dialect here. */
19622 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19623 /* OK, there is a default argument. Wait until after the
19624 conversion check to do substitution. */
19625 continue;
19626
19627 /* If the type parameter is a parameter pack, then it will
19628 be deduced to an empty parameter pack. */
19629 if (template_parameter_pack_p (tparm))
19630 {
19631 tree arg;
19632
19633 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19634 {
19635 arg = make_node (NONTYPE_ARGUMENT_PACK);
19636 TREE_CONSTANT (arg) = 1;
19637 }
19638 else
19639 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19640
19641 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19642
19643 TREE_VEC_ELT (targs, i) = arg;
19644 continue;
19645 }
19646
19647 return unify_parameter_deduction_failure (explain_p, tparm);
19648 }
19649
19650 /* DR 1391: All parameters have args, now check non-dependent parms for
19651 convertibility. */
19652 if (saw_undeduced < 2)
19653 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19654 parms && parms != void_list_node && ia < nargs; )
19655 {
19656 parm = TREE_VALUE (parms);
19657
19658 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19659 && (!TREE_CHAIN (parms)
19660 || TREE_CHAIN (parms) == void_list_node))
19661 /* For a function parameter pack that occurs at the end of the
19662 parameter-declaration-list, the type A of each remaining
19663 argument of the call is compared with the type P of the
19664 declarator-id of the function parameter pack. */
19665 break;
19666
19667 parms = TREE_CHAIN (parms);
19668
19669 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19670 /* For a function parameter pack that does not occur at the
19671 end of the parameter-declaration-list, the type of the
19672 parameter pack is a non-deduced context. */
19673 continue;
19674
19675 arg = args[ia];
19676 ++ia;
19677
19678 if (uses_template_parms (parm))
19679 continue;
19680 if (check_non_deducible_conversion (parm, arg, strict, flags,
19681 explain_p))
19682 return 1;
19683 }
19684
19685 /* Now substitute into the default template arguments. */
19686 for (i = 0; i < ntparms; i++)
19687 {
19688 tree targ = TREE_VEC_ELT (targs, i);
19689 tree tparm = TREE_VEC_ELT (tparms, i);
19690
19691 if (targ || tparm == error_mark_node)
19692 continue;
19693 tree parm = TREE_VALUE (tparm);
19694
19695 if (TREE_CODE (parm) == PARM_DECL
19696 && uses_template_parms (TREE_TYPE (parm))
19697 && saw_undeduced < 2)
19698 continue;
19699
19700 tree arg = TREE_PURPOSE (tparm);
19701 reopen_deferring_access_checks (*checks);
19702 location_t save_loc = input_location;
19703 if (DECL_P (parm))
19704 input_location = DECL_SOURCE_LOCATION (parm);
19705 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19706 if (!uses_template_parms (arg))
19707 arg = convert_template_argument (parm, arg, full_targs, complain,
19708 i, NULL_TREE);
19709 else if (saw_undeduced < 2)
19710 arg = NULL_TREE;
19711 else
19712 arg = error_mark_node;
19713 input_location = save_loc;
19714 *checks = get_deferred_access_checks ();
19715 pop_deferring_access_checks ();
19716 if (arg == error_mark_node)
19717 return 1;
19718 else if (arg)
19719 {
19720 TREE_VEC_ELT (targs, i) = arg;
19721 /* The position of the first default template argument,
19722 is also the number of non-defaulted arguments in TARGS.
19723 Record that. */
19724 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19725 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19726 }
19727 }
19728
19729 if (saw_undeduced++ == 1)
19730 goto again;
19731 }
19732
19733 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19734 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19735
19736 return unify_success (explain_p);
19737 }
19738
19739 /* Subroutine of type_unification_real. Args are like the variables
19740 at the call site. ARG is an overloaded function (or template-id);
19741 we try deducing template args from each of the overloads, and if
19742 only one succeeds, we go with that. Modifies TARGS and returns
19743 true on success. */
19744
19745 static bool
19746 resolve_overloaded_unification (tree tparms,
19747 tree targs,
19748 tree parm,
19749 tree arg,
19750 unification_kind_t strict,
19751 int sub_strict,
19752 bool explain_p)
19753 {
19754 tree tempargs = copy_node (targs);
19755 int good = 0;
19756 tree goodfn = NULL_TREE;
19757 bool addr_p;
19758
19759 if (TREE_CODE (arg) == ADDR_EXPR)
19760 {
19761 arg = TREE_OPERAND (arg, 0);
19762 addr_p = true;
19763 }
19764 else
19765 addr_p = false;
19766
19767 if (TREE_CODE (arg) == COMPONENT_REF)
19768 /* Handle `&x' where `x' is some static or non-static member
19769 function name. */
19770 arg = TREE_OPERAND (arg, 1);
19771
19772 if (TREE_CODE (arg) == OFFSET_REF)
19773 arg = TREE_OPERAND (arg, 1);
19774
19775 /* Strip baselink information. */
19776 if (BASELINK_P (arg))
19777 arg = BASELINK_FUNCTIONS (arg);
19778
19779 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19780 {
19781 /* If we got some explicit template args, we need to plug them into
19782 the affected templates before we try to unify, in case the
19783 explicit args will completely resolve the templates in question. */
19784
19785 int ok = 0;
19786 tree expl_subargs = TREE_OPERAND (arg, 1);
19787 arg = TREE_OPERAND (arg, 0);
19788
19789 for (lkp_iterator iter (arg); iter; ++iter)
19790 {
19791 tree fn = *iter;
19792 tree subargs, elem;
19793
19794 if (TREE_CODE (fn) != TEMPLATE_DECL)
19795 continue;
19796
19797 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19798 expl_subargs, NULL_TREE, tf_none,
19799 /*require_all_args=*/true,
19800 /*use_default_args=*/true);
19801 if (subargs != error_mark_node
19802 && !any_dependent_template_arguments_p (subargs))
19803 {
19804 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19805 if (try_one_overload (tparms, targs, tempargs, parm,
19806 elem, strict, sub_strict, addr_p, explain_p)
19807 && (!goodfn || !same_type_p (goodfn, elem)))
19808 {
19809 goodfn = elem;
19810 ++good;
19811 }
19812 }
19813 else if (subargs)
19814 ++ok;
19815 }
19816 /* If no templates (or more than one) are fully resolved by the
19817 explicit arguments, this template-id is a non-deduced context; it
19818 could still be OK if we deduce all template arguments for the
19819 enclosing call through other arguments. */
19820 if (good != 1)
19821 good = ok;
19822 }
19823 else if (TREE_CODE (arg) != OVERLOAD
19824 && TREE_CODE (arg) != FUNCTION_DECL)
19825 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19826 -- but the deduction does not succeed because the expression is
19827 not just the function on its own. */
19828 return false;
19829 else
19830 for (lkp_iterator iter (arg); iter; ++iter)
19831 {
19832 tree fn = *iter;
19833 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19834 strict, sub_strict, addr_p, explain_p)
19835 && (!goodfn || !decls_match (goodfn, fn)))
19836 {
19837 goodfn = fn;
19838 ++good;
19839 }
19840 }
19841
19842 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19843 to function or pointer to member function argument if the set of
19844 overloaded functions does not contain function templates and at most
19845 one of a set of overloaded functions provides a unique match.
19846
19847 So if we found multiple possibilities, we return success but don't
19848 deduce anything. */
19849
19850 if (good == 1)
19851 {
19852 int i = TREE_VEC_LENGTH (targs);
19853 for (; i--; )
19854 if (TREE_VEC_ELT (tempargs, i))
19855 {
19856 tree old = TREE_VEC_ELT (targs, i);
19857 tree new_ = TREE_VEC_ELT (tempargs, i);
19858 if (new_ && old && ARGUMENT_PACK_P (old)
19859 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19860 /* Don't forget explicit template arguments in a pack. */
19861 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19862 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19863 TREE_VEC_ELT (targs, i) = new_;
19864 }
19865 }
19866 if (good)
19867 return true;
19868
19869 return false;
19870 }
19871
19872 /* Core DR 115: In contexts where deduction is done and fails, or in
19873 contexts where deduction is not done, if a template argument list is
19874 specified and it, along with any default template arguments, identifies
19875 a single function template specialization, then the template-id is an
19876 lvalue for the function template specialization. */
19877
19878 tree
19879 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19880 {
19881 tree expr, offset, baselink;
19882 bool addr;
19883
19884 if (!type_unknown_p (orig_expr))
19885 return orig_expr;
19886
19887 expr = orig_expr;
19888 addr = false;
19889 offset = NULL_TREE;
19890 baselink = NULL_TREE;
19891
19892 if (TREE_CODE (expr) == ADDR_EXPR)
19893 {
19894 expr = TREE_OPERAND (expr, 0);
19895 addr = true;
19896 }
19897 if (TREE_CODE (expr) == OFFSET_REF)
19898 {
19899 offset = expr;
19900 expr = TREE_OPERAND (expr, 1);
19901 }
19902 if (BASELINK_P (expr))
19903 {
19904 baselink = expr;
19905 expr = BASELINK_FUNCTIONS (expr);
19906 }
19907
19908 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19909 {
19910 int good = 0;
19911 tree goodfn = NULL_TREE;
19912
19913 /* If we got some explicit template args, we need to plug them into
19914 the affected templates before we try to unify, in case the
19915 explicit args will completely resolve the templates in question. */
19916
19917 tree expl_subargs = TREE_OPERAND (expr, 1);
19918 tree arg = TREE_OPERAND (expr, 0);
19919 tree badfn = NULL_TREE;
19920 tree badargs = NULL_TREE;
19921
19922 for (lkp_iterator iter (arg); iter; ++iter)
19923 {
19924 tree fn = *iter;
19925 tree subargs, elem;
19926
19927 if (TREE_CODE (fn) != TEMPLATE_DECL)
19928 continue;
19929
19930 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19931 expl_subargs, NULL_TREE, tf_none,
19932 /*require_all_args=*/true,
19933 /*use_default_args=*/true);
19934 if (subargs != error_mark_node
19935 && !any_dependent_template_arguments_p (subargs))
19936 {
19937 elem = instantiate_template (fn, subargs, tf_none);
19938 if (elem == error_mark_node)
19939 {
19940 badfn = fn;
19941 badargs = subargs;
19942 }
19943 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19944 {
19945 goodfn = elem;
19946 ++good;
19947 }
19948 }
19949 }
19950 if (good == 1)
19951 {
19952 mark_used (goodfn);
19953 expr = goodfn;
19954 if (baselink)
19955 expr = build_baselink (BASELINK_BINFO (baselink),
19956 BASELINK_ACCESS_BINFO (baselink),
19957 expr, BASELINK_OPTYPE (baselink));
19958 if (offset)
19959 {
19960 tree base
19961 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19962 expr = build_offset_ref (base, expr, addr, complain);
19963 }
19964 if (addr)
19965 expr = cp_build_addr_expr (expr, complain);
19966 return expr;
19967 }
19968 else if (good == 0 && badargs && (complain & tf_error))
19969 /* There were no good options and at least one bad one, so let the
19970 user know what the problem is. */
19971 instantiate_template (badfn, badargs, complain);
19972 }
19973 return orig_expr;
19974 }
19975
19976 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19977 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19978 different overloads deduce different arguments for a given parm.
19979 ADDR_P is true if the expression for which deduction is being
19980 performed was of the form "& fn" rather than simply "fn".
19981
19982 Returns 1 on success. */
19983
19984 static int
19985 try_one_overload (tree tparms,
19986 tree orig_targs,
19987 tree targs,
19988 tree parm,
19989 tree arg,
19990 unification_kind_t strict,
19991 int sub_strict,
19992 bool addr_p,
19993 bool explain_p)
19994 {
19995 int nargs;
19996 tree tempargs;
19997 int i;
19998
19999 if (arg == error_mark_node)
20000 return 0;
20001
20002 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20003 to function or pointer to member function argument if the set of
20004 overloaded functions does not contain function templates and at most
20005 one of a set of overloaded functions provides a unique match.
20006
20007 So if this is a template, just return success. */
20008
20009 if (uses_template_parms (arg))
20010 return 1;
20011
20012 if (TREE_CODE (arg) == METHOD_TYPE)
20013 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20014 else if (addr_p)
20015 arg = build_pointer_type (arg);
20016
20017 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20018
20019 /* We don't copy orig_targs for this because if we have already deduced
20020 some template args from previous args, unify would complain when we
20021 try to deduce a template parameter for the same argument, even though
20022 there isn't really a conflict. */
20023 nargs = TREE_VEC_LENGTH (targs);
20024 tempargs = make_tree_vec (nargs);
20025
20026 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20027 return 0;
20028
20029 /* First make sure we didn't deduce anything that conflicts with
20030 explicitly specified args. */
20031 for (i = nargs; i--; )
20032 {
20033 tree elt = TREE_VEC_ELT (tempargs, i);
20034 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20035
20036 if (!elt)
20037 /*NOP*/;
20038 else if (uses_template_parms (elt))
20039 /* Since we're unifying against ourselves, we will fill in
20040 template args used in the function parm list with our own
20041 template parms. Discard them. */
20042 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20043 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20044 {
20045 /* Check that the argument at each index of the deduced argument pack
20046 is equivalent to the corresponding explicitly specified argument.
20047 We may have deduced more arguments than were explicitly specified,
20048 and that's OK. */
20049
20050 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20051 that's wrong if we deduce the same argument pack from multiple
20052 function arguments: it's only incomplete the first time. */
20053
20054 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20055 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20056
20057 if (TREE_VEC_LENGTH (deduced_pack)
20058 < TREE_VEC_LENGTH (explicit_pack))
20059 return 0;
20060
20061 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20062 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20063 TREE_VEC_ELT (deduced_pack, j)))
20064 return 0;
20065 }
20066 else if (oldelt && !template_args_equal (oldelt, elt))
20067 return 0;
20068 }
20069
20070 for (i = nargs; i--; )
20071 {
20072 tree elt = TREE_VEC_ELT (tempargs, i);
20073
20074 if (elt)
20075 TREE_VEC_ELT (targs, i) = elt;
20076 }
20077
20078 return 1;
20079 }
20080
20081 /* PARM is a template class (perhaps with unbound template
20082 parameters). ARG is a fully instantiated type. If ARG can be
20083 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20084 TARGS are as for unify. */
20085
20086 static tree
20087 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20088 bool explain_p)
20089 {
20090 tree copy_of_targs;
20091
20092 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20093 return NULL_TREE;
20094 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20095 /* Matches anything. */;
20096 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20097 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20098 return NULL_TREE;
20099
20100 /* We need to make a new template argument vector for the call to
20101 unify. If we used TARGS, we'd clutter it up with the result of
20102 the attempted unification, even if this class didn't work out.
20103 We also don't want to commit ourselves to all the unifications
20104 we've already done, since unification is supposed to be done on
20105 an argument-by-argument basis. In other words, consider the
20106 following pathological case:
20107
20108 template <int I, int J, int K>
20109 struct S {};
20110
20111 template <int I, int J>
20112 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20113
20114 template <int I, int J, int K>
20115 void f(S<I, J, K>, S<I, I, I>);
20116
20117 void g() {
20118 S<0, 0, 0> s0;
20119 S<0, 1, 2> s2;
20120
20121 f(s0, s2);
20122 }
20123
20124 Now, by the time we consider the unification involving `s2', we
20125 already know that we must have `f<0, 0, 0>'. But, even though
20126 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20127 because there are two ways to unify base classes of S<0, 1, 2>
20128 with S<I, I, I>. If we kept the already deduced knowledge, we
20129 would reject the possibility I=1. */
20130 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20131
20132 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20133 {
20134 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20135 return NULL_TREE;
20136 return arg;
20137 }
20138
20139 /* If unification failed, we're done. */
20140 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20141 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20142 return NULL_TREE;
20143
20144 return arg;
20145 }
20146
20147 /* Given a template type PARM and a class type ARG, find the unique
20148 base type in ARG that is an instance of PARM. We do not examine
20149 ARG itself; only its base-classes. If there is not exactly one
20150 appropriate base class, return NULL_TREE. PARM may be the type of
20151 a partial specialization, as well as a plain template type. Used
20152 by unify. */
20153
20154 static enum template_base_result
20155 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20156 bool explain_p, tree *result)
20157 {
20158 tree rval = NULL_TREE;
20159 tree binfo;
20160
20161 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20162
20163 binfo = TYPE_BINFO (complete_type (arg));
20164 if (!binfo)
20165 {
20166 /* The type could not be completed. */
20167 *result = NULL_TREE;
20168 return tbr_incomplete_type;
20169 }
20170
20171 /* Walk in inheritance graph order. The search order is not
20172 important, and this avoids multiple walks of virtual bases. */
20173 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20174 {
20175 tree r = try_class_unification (tparms, targs, parm,
20176 BINFO_TYPE (binfo), explain_p);
20177
20178 if (r)
20179 {
20180 /* If there is more than one satisfactory baseclass, then:
20181
20182 [temp.deduct.call]
20183
20184 If they yield more than one possible deduced A, the type
20185 deduction fails.
20186
20187 applies. */
20188 if (rval && !same_type_p (r, rval))
20189 {
20190 *result = NULL_TREE;
20191 return tbr_ambiguous_baseclass;
20192 }
20193
20194 rval = r;
20195 }
20196 }
20197
20198 *result = rval;
20199 return tbr_success;
20200 }
20201
20202 /* Returns the level of DECL, which declares a template parameter. */
20203
20204 static int
20205 template_decl_level (tree decl)
20206 {
20207 switch (TREE_CODE (decl))
20208 {
20209 case TYPE_DECL:
20210 case TEMPLATE_DECL:
20211 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20212
20213 case PARM_DECL:
20214 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20215
20216 default:
20217 gcc_unreachable ();
20218 }
20219 return 0;
20220 }
20221
20222 /* Decide whether ARG can be unified with PARM, considering only the
20223 cv-qualifiers of each type, given STRICT as documented for unify.
20224 Returns nonzero iff the unification is OK on that basis. */
20225
20226 static int
20227 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20228 {
20229 int arg_quals = cp_type_quals (arg);
20230 int parm_quals = cp_type_quals (parm);
20231
20232 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20233 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20234 {
20235 /* Although a CVR qualifier is ignored when being applied to a
20236 substituted template parameter ([8.3.2]/1 for example), that
20237 does not allow us to unify "const T" with "int&" because both
20238 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20239 It is ok when we're allowing additional CV qualifiers
20240 at the outer level [14.8.2.1]/3,1st bullet. */
20241 if ((TREE_CODE (arg) == REFERENCE_TYPE
20242 || TREE_CODE (arg) == FUNCTION_TYPE
20243 || TREE_CODE (arg) == METHOD_TYPE)
20244 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20245 return 0;
20246
20247 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20248 && (parm_quals & TYPE_QUAL_RESTRICT))
20249 return 0;
20250 }
20251
20252 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20253 && (arg_quals & parm_quals) != parm_quals)
20254 return 0;
20255
20256 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20257 && (parm_quals & arg_quals) != arg_quals)
20258 return 0;
20259
20260 return 1;
20261 }
20262
20263 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20264 void
20265 template_parm_level_and_index (tree parm, int* level, int* index)
20266 {
20267 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20268 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20269 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20270 {
20271 *index = TEMPLATE_TYPE_IDX (parm);
20272 *level = TEMPLATE_TYPE_LEVEL (parm);
20273 }
20274 else
20275 {
20276 *index = TEMPLATE_PARM_IDX (parm);
20277 *level = TEMPLATE_PARM_LEVEL (parm);
20278 }
20279 }
20280
20281 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20282 do { \
20283 if (unify (TP, TA, P, A, S, EP)) \
20284 return 1; \
20285 } while (0)
20286
20287 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20288 expansion at the end of PACKED_PARMS. Returns 0 if the type
20289 deduction succeeds, 1 otherwise. STRICT is the same as in
20290 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20291 function call argument list. We'll need to adjust the arguments to make them
20292 types. SUBR tells us if this is from a recursive call to
20293 type_unification_real, or for comparing two template argument
20294 lists. */
20295
20296 static int
20297 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20298 tree packed_args, unification_kind_t strict,
20299 bool subr, bool explain_p)
20300 {
20301 tree parm
20302 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20303 tree pattern = PACK_EXPANSION_PATTERN (parm);
20304 tree pack, packs = NULL_TREE;
20305 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20306
20307 /* Add in any args remembered from an earlier partial instantiation. */
20308 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20309
20310 packed_args = expand_template_argument_pack (packed_args);
20311
20312 int len = TREE_VEC_LENGTH (packed_args);
20313
20314 /* Determine the parameter packs we will be deducing from the
20315 pattern, and record their current deductions. */
20316 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20317 pack; pack = TREE_CHAIN (pack))
20318 {
20319 tree parm_pack = TREE_VALUE (pack);
20320 int idx, level;
20321
20322 /* Determine the index and level of this parameter pack. */
20323 template_parm_level_and_index (parm_pack, &level, &idx);
20324
20325 /* Keep track of the parameter packs and their corresponding
20326 argument packs. */
20327 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20328 TREE_TYPE (packs) = make_tree_vec (len - start);
20329 }
20330
20331 /* Loop through all of the arguments that have not yet been
20332 unified and unify each with the pattern. */
20333 for (i = start; i < len; i++)
20334 {
20335 tree parm;
20336 bool any_explicit = false;
20337 tree arg = TREE_VEC_ELT (packed_args, i);
20338
20339 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20340 or the element of its argument pack at the current index if
20341 this argument was explicitly specified. */
20342 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20343 {
20344 int idx, level;
20345 tree arg, pargs;
20346 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20347
20348 arg = NULL_TREE;
20349 if (TREE_VALUE (pack)
20350 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20351 && (i - start < TREE_VEC_LENGTH (pargs)))
20352 {
20353 any_explicit = true;
20354 arg = TREE_VEC_ELT (pargs, i - start);
20355 }
20356 TMPL_ARG (targs, level, idx) = arg;
20357 }
20358
20359 /* If we had explicit template arguments, substitute them into the
20360 pattern before deduction. */
20361 if (any_explicit)
20362 {
20363 /* Some arguments might still be unspecified or dependent. */
20364 bool dependent;
20365 ++processing_template_decl;
20366 dependent = any_dependent_template_arguments_p (targs);
20367 if (!dependent)
20368 --processing_template_decl;
20369 parm = tsubst (pattern, targs,
20370 explain_p ? tf_warning_or_error : tf_none,
20371 NULL_TREE);
20372 if (dependent)
20373 --processing_template_decl;
20374 if (parm == error_mark_node)
20375 return 1;
20376 }
20377 else
20378 parm = pattern;
20379
20380 /* Unify the pattern with the current argument. */
20381 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20382 explain_p))
20383 return 1;
20384
20385 /* For each parameter pack, collect the deduced value. */
20386 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20387 {
20388 int idx, level;
20389 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20390
20391 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20392 TMPL_ARG (targs, level, idx);
20393 }
20394 }
20395
20396 /* Verify that the results of unification with the parameter packs
20397 produce results consistent with what we've seen before, and make
20398 the deduced argument packs available. */
20399 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20400 {
20401 tree old_pack = TREE_VALUE (pack);
20402 tree new_args = TREE_TYPE (pack);
20403 int i, len = TREE_VEC_LENGTH (new_args);
20404 int idx, level;
20405 bool nondeduced_p = false;
20406
20407 /* By default keep the original deduced argument pack.
20408 If necessary, more specific code is going to update the
20409 resulting deduced argument later down in this function. */
20410 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20411 TMPL_ARG (targs, level, idx) = old_pack;
20412
20413 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20414 actually deduce anything. */
20415 for (i = 0; i < len && !nondeduced_p; ++i)
20416 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20417 nondeduced_p = true;
20418 if (nondeduced_p)
20419 continue;
20420
20421 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20422 {
20423 /* If we had fewer function args than explicit template args,
20424 just use the explicits. */
20425 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20426 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20427 if (len < explicit_len)
20428 new_args = explicit_args;
20429 }
20430
20431 if (!old_pack)
20432 {
20433 tree result;
20434 /* Build the deduced *_ARGUMENT_PACK. */
20435 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20436 {
20437 result = make_node (NONTYPE_ARGUMENT_PACK);
20438 TREE_CONSTANT (result) = 1;
20439 }
20440 else
20441 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20442
20443 SET_ARGUMENT_PACK_ARGS (result, new_args);
20444
20445 /* Note the deduced argument packs for this parameter
20446 pack. */
20447 TMPL_ARG (targs, level, idx) = result;
20448 }
20449 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20450 && (ARGUMENT_PACK_ARGS (old_pack)
20451 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20452 {
20453 /* We only had the explicitly-provided arguments before, but
20454 now we have a complete set of arguments. */
20455 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20456
20457 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20458 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20459 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20460 }
20461 else
20462 {
20463 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20464 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20465
20466 if (!comp_template_args (old_args, new_args,
20467 &bad_old_arg, &bad_new_arg))
20468 /* Inconsistent unification of this parameter pack. */
20469 return unify_parameter_pack_inconsistent (explain_p,
20470 bad_old_arg,
20471 bad_new_arg);
20472 }
20473 }
20474
20475 return unify_success (explain_p);
20476 }
20477
20478 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20479 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20480 parameters and return value are as for unify. */
20481
20482 static int
20483 unify_array_domain (tree tparms, tree targs,
20484 tree parm_dom, tree arg_dom,
20485 bool explain_p)
20486 {
20487 tree parm_max;
20488 tree arg_max;
20489 bool parm_cst;
20490 bool arg_cst;
20491
20492 /* Our representation of array types uses "N - 1" as the
20493 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20494 not an integer constant. We cannot unify arbitrarily
20495 complex expressions, so we eliminate the MINUS_EXPRs
20496 here. */
20497 parm_max = TYPE_MAX_VALUE (parm_dom);
20498 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20499 if (!parm_cst)
20500 {
20501 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20502 parm_max = TREE_OPERAND (parm_max, 0);
20503 }
20504 arg_max = TYPE_MAX_VALUE (arg_dom);
20505 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20506 if (!arg_cst)
20507 {
20508 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20509 trying to unify the type of a variable with the type
20510 of a template parameter. For example:
20511
20512 template <unsigned int N>
20513 void f (char (&) [N]);
20514 int g();
20515 void h(int i) {
20516 char a[g(i)];
20517 f(a);
20518 }
20519
20520 Here, the type of the ARG will be "int [g(i)]", and
20521 may be a SAVE_EXPR, etc. */
20522 if (TREE_CODE (arg_max) != MINUS_EXPR)
20523 return unify_vla_arg (explain_p, arg_dom);
20524 arg_max = TREE_OPERAND (arg_max, 0);
20525 }
20526
20527 /* If only one of the bounds used a MINUS_EXPR, compensate
20528 by adding one to the other bound. */
20529 if (parm_cst && !arg_cst)
20530 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20531 integer_type_node,
20532 parm_max,
20533 integer_one_node);
20534 else if (arg_cst && !parm_cst)
20535 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20536 integer_type_node,
20537 arg_max,
20538 integer_one_node);
20539
20540 return unify (tparms, targs, parm_max, arg_max,
20541 UNIFY_ALLOW_INTEGER, explain_p);
20542 }
20543
20544 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20545
20546 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20547
20548 static pa_kind_t
20549 pa_kind (tree t)
20550 {
20551 if (PACK_EXPANSION_P (t))
20552 t = PACK_EXPANSION_PATTERN (t);
20553 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20554 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20555 || DECL_TYPE_TEMPLATE_P (t))
20556 return pa_tmpl;
20557 else if (TYPE_P (t))
20558 return pa_type;
20559 else
20560 return pa_expr;
20561 }
20562
20563 /* Deduce the value of template parameters. TPARMS is the (innermost)
20564 set of template parameters to a template. TARGS is the bindings
20565 for those template parameters, as determined thus far; TARGS may
20566 include template arguments for outer levels of template parameters
20567 as well. PARM is a parameter to a template function, or a
20568 subcomponent of that parameter; ARG is the corresponding argument.
20569 This function attempts to match PARM with ARG in a manner
20570 consistent with the existing assignments in TARGS. If more values
20571 are deduced, then TARGS is updated.
20572
20573 Returns 0 if the type deduction succeeds, 1 otherwise. The
20574 parameter STRICT is a bitwise or of the following flags:
20575
20576 UNIFY_ALLOW_NONE:
20577 Require an exact match between PARM and ARG.
20578 UNIFY_ALLOW_MORE_CV_QUAL:
20579 Allow the deduced ARG to be more cv-qualified (by qualification
20580 conversion) than ARG.
20581 UNIFY_ALLOW_LESS_CV_QUAL:
20582 Allow the deduced ARG to be less cv-qualified than ARG.
20583 UNIFY_ALLOW_DERIVED:
20584 Allow the deduced ARG to be a template base class of ARG,
20585 or a pointer to a template base class of the type pointed to by
20586 ARG.
20587 UNIFY_ALLOW_INTEGER:
20588 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20589 case for more information.
20590 UNIFY_ALLOW_OUTER_LEVEL:
20591 This is the outermost level of a deduction. Used to determine validity
20592 of qualification conversions. A valid qualification conversion must
20593 have const qualified pointers leading up to the inner type which
20594 requires additional CV quals, except at the outer level, where const
20595 is not required [conv.qual]. It would be normal to set this flag in
20596 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20597 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20598 This is the outermost level of a deduction, and PARM can be more CV
20599 qualified at this point.
20600 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20601 This is the outermost level of a deduction, and PARM can be less CV
20602 qualified at this point. */
20603
20604 static int
20605 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20606 bool explain_p)
20607 {
20608 int idx;
20609 tree targ;
20610 tree tparm;
20611 int strict_in = strict;
20612 tsubst_flags_t complain = (explain_p
20613 ? tf_warning_or_error
20614 : tf_none);
20615
20616 /* I don't think this will do the right thing with respect to types.
20617 But the only case I've seen it in so far has been array bounds, where
20618 signedness is the only information lost, and I think that will be
20619 okay. */
20620 while (CONVERT_EXPR_P (parm))
20621 parm = TREE_OPERAND (parm, 0);
20622
20623 if (arg == error_mark_node)
20624 return unify_invalid (explain_p);
20625 if (arg == unknown_type_node
20626 || arg == init_list_type_node)
20627 /* We can't deduce anything from this, but we might get all the
20628 template args from other function args. */
20629 return unify_success (explain_p);
20630
20631 if (parm == any_targ_node || arg == any_targ_node)
20632 return unify_success (explain_p);
20633
20634 /* If PARM uses template parameters, then we can't bail out here,
20635 even if ARG == PARM, since we won't record unifications for the
20636 template parameters. We might need them if we're trying to
20637 figure out which of two things is more specialized. */
20638 if (arg == parm && !uses_template_parms (parm))
20639 return unify_success (explain_p);
20640
20641 /* Handle init lists early, so the rest of the function can assume
20642 we're dealing with a type. */
20643 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20644 {
20645 tree elt, elttype;
20646 unsigned i;
20647 tree orig_parm = parm;
20648
20649 /* Replace T with std::initializer_list<T> for deduction. */
20650 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20651 && flag_deduce_init_list)
20652 parm = listify (parm);
20653
20654 if (!is_std_init_list (parm)
20655 && TREE_CODE (parm) != ARRAY_TYPE)
20656 /* We can only deduce from an initializer list argument if the
20657 parameter is std::initializer_list or an array; otherwise this
20658 is a non-deduced context. */
20659 return unify_success (explain_p);
20660
20661 if (TREE_CODE (parm) == ARRAY_TYPE)
20662 elttype = TREE_TYPE (parm);
20663 else
20664 {
20665 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20666 /* Deduction is defined in terms of a single type, so just punt
20667 on the (bizarre) std::initializer_list<T...>. */
20668 if (PACK_EXPANSION_P (elttype))
20669 return unify_success (explain_p);
20670 }
20671
20672 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20673 {
20674 int elt_strict = strict;
20675
20676 if (elt == error_mark_node)
20677 return unify_invalid (explain_p);
20678
20679 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20680 {
20681 tree type = TREE_TYPE (elt);
20682 if (type == error_mark_node)
20683 return unify_invalid (explain_p);
20684 /* It should only be possible to get here for a call. */
20685 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20686 elt_strict |= maybe_adjust_types_for_deduction
20687 (DEDUCE_CALL, &elttype, &type, elt);
20688 elt = type;
20689 }
20690
20691 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20692 explain_p);
20693 }
20694
20695 if (TREE_CODE (parm) == ARRAY_TYPE
20696 && deducible_array_bound (TYPE_DOMAIN (parm)))
20697 {
20698 /* Also deduce from the length of the initializer list. */
20699 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20700 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20701 if (idx == error_mark_node)
20702 return unify_invalid (explain_p);
20703 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20704 idx, explain_p);
20705 }
20706
20707 /* If the std::initializer_list<T> deduction worked, replace the
20708 deduced A with std::initializer_list<A>. */
20709 if (orig_parm != parm)
20710 {
20711 idx = TEMPLATE_TYPE_IDX (orig_parm);
20712 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20713 targ = listify (targ);
20714 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20715 }
20716 return unify_success (explain_p);
20717 }
20718
20719 /* If parm and arg aren't the same kind of thing (template, type, or
20720 expression), fail early. */
20721 if (pa_kind (parm) != pa_kind (arg))
20722 return unify_invalid (explain_p);
20723
20724 /* Immediately reject some pairs that won't unify because of
20725 cv-qualification mismatches. */
20726 if (TREE_CODE (arg) == TREE_CODE (parm)
20727 && TYPE_P (arg)
20728 /* It is the elements of the array which hold the cv quals of an array
20729 type, and the elements might be template type parms. We'll check
20730 when we recurse. */
20731 && TREE_CODE (arg) != ARRAY_TYPE
20732 /* We check the cv-qualifiers when unifying with template type
20733 parameters below. We want to allow ARG `const T' to unify with
20734 PARM `T' for example, when computing which of two templates
20735 is more specialized, for example. */
20736 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20737 && !check_cv_quals_for_unify (strict_in, arg, parm))
20738 return unify_cv_qual_mismatch (explain_p, parm, arg);
20739
20740 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20741 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20742 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20743 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20744 strict &= ~UNIFY_ALLOW_DERIVED;
20745 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20746 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20747
20748 switch (TREE_CODE (parm))
20749 {
20750 case TYPENAME_TYPE:
20751 case SCOPE_REF:
20752 case UNBOUND_CLASS_TEMPLATE:
20753 /* In a type which contains a nested-name-specifier, template
20754 argument values cannot be deduced for template parameters used
20755 within the nested-name-specifier. */
20756 return unify_success (explain_p);
20757
20758 case TEMPLATE_TYPE_PARM:
20759 case TEMPLATE_TEMPLATE_PARM:
20760 case BOUND_TEMPLATE_TEMPLATE_PARM:
20761 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20762 if (error_operand_p (tparm))
20763 return unify_invalid (explain_p);
20764
20765 if (TEMPLATE_TYPE_LEVEL (parm)
20766 != template_decl_level (tparm))
20767 /* The PARM is not one we're trying to unify. Just check
20768 to see if it matches ARG. */
20769 {
20770 if (TREE_CODE (arg) == TREE_CODE (parm)
20771 && (is_auto (parm) ? is_auto (arg)
20772 : same_type_p (parm, arg)))
20773 return unify_success (explain_p);
20774 else
20775 return unify_type_mismatch (explain_p, parm, arg);
20776 }
20777 idx = TEMPLATE_TYPE_IDX (parm);
20778 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20779 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20780 if (error_operand_p (tparm))
20781 return unify_invalid (explain_p);
20782
20783 /* Check for mixed types and values. */
20784 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20785 && TREE_CODE (tparm) != TYPE_DECL)
20786 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20787 && TREE_CODE (tparm) != TEMPLATE_DECL))
20788 gcc_unreachable ();
20789
20790 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20791 {
20792 if ((strict_in & UNIFY_ALLOW_DERIVED)
20793 && CLASS_TYPE_P (arg))
20794 {
20795 /* First try to match ARG directly. */
20796 tree t = try_class_unification (tparms, targs, parm, arg,
20797 explain_p);
20798 if (!t)
20799 {
20800 /* Otherwise, look for a suitable base of ARG, as below. */
20801 enum template_base_result r;
20802 r = get_template_base (tparms, targs, parm, arg,
20803 explain_p, &t);
20804 if (!t)
20805 return unify_no_common_base (explain_p, r, parm, arg);
20806 arg = t;
20807 }
20808 }
20809 /* ARG must be constructed from a template class or a template
20810 template parameter. */
20811 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20812 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20813 return unify_template_deduction_failure (explain_p, parm, arg);
20814
20815 /* Deduce arguments T, i from TT<T> or TT<i>. */
20816 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20817 return 1;
20818
20819 arg = TYPE_TI_TEMPLATE (arg);
20820
20821 /* Fall through to deduce template name. */
20822 }
20823
20824 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20825 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20826 {
20827 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20828
20829 /* Simple cases: Value already set, does match or doesn't. */
20830 if (targ != NULL_TREE && template_args_equal (targ, arg))
20831 return unify_success (explain_p);
20832 else if (targ)
20833 return unify_inconsistency (explain_p, parm, targ, arg);
20834 }
20835 else
20836 {
20837 /* If PARM is `const T' and ARG is only `int', we don't have
20838 a match unless we are allowing additional qualification.
20839 If ARG is `const int' and PARM is just `T' that's OK;
20840 that binds `const int' to `T'. */
20841 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20842 arg, parm))
20843 return unify_cv_qual_mismatch (explain_p, parm, arg);
20844
20845 /* Consider the case where ARG is `const volatile int' and
20846 PARM is `const T'. Then, T should be `volatile int'. */
20847 arg = cp_build_qualified_type_real
20848 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20849 if (arg == error_mark_node)
20850 return unify_invalid (explain_p);
20851
20852 /* Simple cases: Value already set, does match or doesn't. */
20853 if (targ != NULL_TREE && same_type_p (targ, arg))
20854 return unify_success (explain_p);
20855 else if (targ)
20856 return unify_inconsistency (explain_p, parm, targ, arg);
20857
20858 /* Make sure that ARG is not a variable-sized array. (Note
20859 that were talking about variable-sized arrays (like
20860 `int[n]'), rather than arrays of unknown size (like
20861 `int[]').) We'll get very confused by such a type since
20862 the bound of the array is not constant, and therefore
20863 not mangleable. Besides, such types are not allowed in
20864 ISO C++, so we can do as we please here. We do allow
20865 them for 'auto' deduction, since that isn't ABI-exposed. */
20866 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20867 return unify_vla_arg (explain_p, arg);
20868
20869 /* Strip typedefs as in convert_template_argument. */
20870 arg = canonicalize_type_argument (arg, tf_none);
20871 }
20872
20873 /* If ARG is a parameter pack or an expansion, we cannot unify
20874 against it unless PARM is also a parameter pack. */
20875 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20876 && !template_parameter_pack_p (parm))
20877 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20878
20879 /* If the argument deduction results is a METHOD_TYPE,
20880 then there is a problem.
20881 METHOD_TYPE doesn't map to any real C++ type the result of
20882 the deduction can not be of that type. */
20883 if (TREE_CODE (arg) == METHOD_TYPE)
20884 return unify_method_type_error (explain_p, arg);
20885
20886 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20887 return unify_success (explain_p);
20888
20889 case TEMPLATE_PARM_INDEX:
20890 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20891 if (error_operand_p (tparm))
20892 return unify_invalid (explain_p);
20893
20894 if (TEMPLATE_PARM_LEVEL (parm)
20895 != template_decl_level (tparm))
20896 {
20897 /* The PARM is not one we're trying to unify. Just check
20898 to see if it matches ARG. */
20899 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20900 && cp_tree_equal (parm, arg));
20901 if (result)
20902 unify_expression_unequal (explain_p, parm, arg);
20903 return result;
20904 }
20905
20906 idx = TEMPLATE_PARM_IDX (parm);
20907 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20908
20909 if (targ)
20910 {
20911 if ((strict & UNIFY_ALLOW_INTEGER)
20912 && TREE_TYPE (targ) && TREE_TYPE (arg)
20913 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
20914 /* We're deducing from an array bound, the type doesn't matter. */
20915 arg = fold_convert (TREE_TYPE (targ), arg);
20916 int x = !cp_tree_equal (targ, arg);
20917 if (x)
20918 unify_inconsistency (explain_p, parm, targ, arg);
20919 return x;
20920 }
20921
20922 /* [temp.deduct.type] If, in the declaration of a function template
20923 with a non-type template-parameter, the non-type
20924 template-parameter is used in an expression in the function
20925 parameter-list and, if the corresponding template-argument is
20926 deduced, the template-argument type shall match the type of the
20927 template-parameter exactly, except that a template-argument
20928 deduced from an array bound may be of any integral type.
20929 The non-type parameter might use already deduced type parameters. */
20930 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20931 if (tree a = type_uses_auto (tparm))
20932 {
20933 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20934 if (tparm == error_mark_node)
20935 return 1;
20936 }
20937
20938 if (!TREE_TYPE (arg))
20939 /* Template-parameter dependent expression. Just accept it for now.
20940 It will later be processed in convert_template_argument. */
20941 ;
20942 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20943 non_reference (tparm)))
20944 /* OK */;
20945 else if ((strict & UNIFY_ALLOW_INTEGER)
20946 && CP_INTEGRAL_TYPE_P (tparm))
20947 /* Convert the ARG to the type of PARM; the deduced non-type
20948 template argument must exactly match the types of the
20949 corresponding parameter. */
20950 arg = fold (build_nop (tparm, arg));
20951 else if (uses_template_parms (tparm))
20952 {
20953 /* We haven't deduced the type of this parameter yet. */
20954 if (cxx_dialect >= cxx17
20955 /* We deduce from array bounds in try_array_deduction. */
20956 && !(strict & UNIFY_ALLOW_INTEGER))
20957 {
20958 /* Deduce it from the non-type argument. */
20959 tree atype = TREE_TYPE (arg);
20960 RECUR_AND_CHECK_FAILURE (tparms, targs,
20961 tparm, atype,
20962 UNIFY_ALLOW_NONE, explain_p);
20963 }
20964 else
20965 /* Try again later. */
20966 return unify_success (explain_p);
20967 }
20968 else
20969 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20970
20971 /* If ARG is a parameter pack or an expansion, we cannot unify
20972 against it unless PARM is also a parameter pack. */
20973 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20974 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20975 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20976
20977 {
20978 bool removed_attr = false;
20979 arg = strip_typedefs_expr (arg, &removed_attr);
20980 }
20981 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20982 return unify_success (explain_p);
20983
20984 case PTRMEM_CST:
20985 {
20986 /* A pointer-to-member constant can be unified only with
20987 another constant. */
20988 if (TREE_CODE (arg) != PTRMEM_CST)
20989 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20990
20991 /* Just unify the class member. It would be useless (and possibly
20992 wrong, depending on the strict flags) to unify also
20993 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20994 arg refer to the same variable, even if through different
20995 classes. For instance:
20996
20997 struct A { int x; };
20998 struct B : A { };
20999
21000 Unification of &A::x and &B::x must succeed. */
21001 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21002 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21003 }
21004
21005 case POINTER_TYPE:
21006 {
21007 if (!TYPE_PTR_P (arg))
21008 return unify_type_mismatch (explain_p, parm, arg);
21009
21010 /* [temp.deduct.call]
21011
21012 A can be another pointer or pointer to member type that can
21013 be converted to the deduced A via a qualification
21014 conversion (_conv.qual_).
21015
21016 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21017 This will allow for additional cv-qualification of the
21018 pointed-to types if appropriate. */
21019
21020 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21021 /* The derived-to-base conversion only persists through one
21022 level of pointers. */
21023 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21024
21025 return unify (tparms, targs, TREE_TYPE (parm),
21026 TREE_TYPE (arg), strict, explain_p);
21027 }
21028
21029 case REFERENCE_TYPE:
21030 if (TREE_CODE (arg) != REFERENCE_TYPE)
21031 return unify_type_mismatch (explain_p, parm, arg);
21032 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21033 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21034
21035 case ARRAY_TYPE:
21036 if (TREE_CODE (arg) != ARRAY_TYPE)
21037 return unify_type_mismatch (explain_p, parm, arg);
21038 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21039 != (TYPE_DOMAIN (arg) == NULL_TREE))
21040 return unify_type_mismatch (explain_p, parm, arg);
21041 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21042 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21043 if (TYPE_DOMAIN (parm) != NULL_TREE)
21044 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21045 TYPE_DOMAIN (arg), explain_p);
21046 return unify_success (explain_p);
21047
21048 case REAL_TYPE:
21049 case COMPLEX_TYPE:
21050 case VECTOR_TYPE:
21051 case INTEGER_TYPE:
21052 case BOOLEAN_TYPE:
21053 case ENUMERAL_TYPE:
21054 case VOID_TYPE:
21055 case NULLPTR_TYPE:
21056 if (TREE_CODE (arg) != TREE_CODE (parm))
21057 return unify_type_mismatch (explain_p, parm, arg);
21058
21059 /* We have already checked cv-qualification at the top of the
21060 function. */
21061 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21062 return unify_type_mismatch (explain_p, parm, arg);
21063
21064 /* As far as unification is concerned, this wins. Later checks
21065 will invalidate it if necessary. */
21066 return unify_success (explain_p);
21067
21068 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21069 /* Type INTEGER_CST can come from ordinary constant template args. */
21070 case INTEGER_CST:
21071 while (CONVERT_EXPR_P (arg))
21072 arg = TREE_OPERAND (arg, 0);
21073
21074 if (TREE_CODE (arg) != INTEGER_CST)
21075 return unify_template_argument_mismatch (explain_p, parm, arg);
21076 return (tree_int_cst_equal (parm, arg)
21077 ? unify_success (explain_p)
21078 : unify_template_argument_mismatch (explain_p, parm, arg));
21079
21080 case TREE_VEC:
21081 {
21082 int i, len, argslen;
21083 int parm_variadic_p = 0;
21084
21085 if (TREE_CODE (arg) != TREE_VEC)
21086 return unify_template_argument_mismatch (explain_p, parm, arg);
21087
21088 len = TREE_VEC_LENGTH (parm);
21089 argslen = TREE_VEC_LENGTH (arg);
21090
21091 /* Check for pack expansions in the parameters. */
21092 for (i = 0; i < len; ++i)
21093 {
21094 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21095 {
21096 if (i == len - 1)
21097 /* We can unify against something with a trailing
21098 parameter pack. */
21099 parm_variadic_p = 1;
21100 else
21101 /* [temp.deduct.type]/9: If the template argument list of
21102 P contains a pack expansion that is not the last
21103 template argument, the entire template argument list
21104 is a non-deduced context. */
21105 return unify_success (explain_p);
21106 }
21107 }
21108
21109 /* If we don't have enough arguments to satisfy the parameters
21110 (not counting the pack expression at the end), or we have
21111 too many arguments for a parameter list that doesn't end in
21112 a pack expression, we can't unify. */
21113 if (parm_variadic_p
21114 ? argslen < len - parm_variadic_p
21115 : argslen != len)
21116 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21117
21118 /* Unify all of the parameters that precede the (optional)
21119 pack expression. */
21120 for (i = 0; i < len - parm_variadic_p; ++i)
21121 {
21122 RECUR_AND_CHECK_FAILURE (tparms, targs,
21123 TREE_VEC_ELT (parm, i),
21124 TREE_VEC_ELT (arg, i),
21125 UNIFY_ALLOW_NONE, explain_p);
21126 }
21127 if (parm_variadic_p)
21128 return unify_pack_expansion (tparms, targs, parm, arg,
21129 DEDUCE_EXACT,
21130 /*subr=*/true, explain_p);
21131 return unify_success (explain_p);
21132 }
21133
21134 case RECORD_TYPE:
21135 case UNION_TYPE:
21136 if (TREE_CODE (arg) != TREE_CODE (parm))
21137 return unify_type_mismatch (explain_p, parm, arg);
21138
21139 if (TYPE_PTRMEMFUNC_P (parm))
21140 {
21141 if (!TYPE_PTRMEMFUNC_P (arg))
21142 return unify_type_mismatch (explain_p, parm, arg);
21143
21144 return unify (tparms, targs,
21145 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21146 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21147 strict, explain_p);
21148 }
21149 else if (TYPE_PTRMEMFUNC_P (arg))
21150 return unify_type_mismatch (explain_p, parm, arg);
21151
21152 if (CLASSTYPE_TEMPLATE_INFO (parm))
21153 {
21154 tree t = NULL_TREE;
21155
21156 if (strict_in & UNIFY_ALLOW_DERIVED)
21157 {
21158 /* First, we try to unify the PARM and ARG directly. */
21159 t = try_class_unification (tparms, targs,
21160 parm, arg, explain_p);
21161
21162 if (!t)
21163 {
21164 /* Fallback to the special case allowed in
21165 [temp.deduct.call]:
21166
21167 If P is a class, and P has the form
21168 template-id, then A can be a derived class of
21169 the deduced A. Likewise, if P is a pointer to
21170 a class of the form template-id, A can be a
21171 pointer to a derived class pointed to by the
21172 deduced A. */
21173 enum template_base_result r;
21174 r = get_template_base (tparms, targs, parm, arg,
21175 explain_p, &t);
21176
21177 if (!t)
21178 {
21179 /* Don't give the derived diagnostic if we're
21180 already dealing with the same template. */
21181 bool same_template
21182 = (CLASSTYPE_TEMPLATE_INFO (arg)
21183 && (CLASSTYPE_TI_TEMPLATE (parm)
21184 == CLASSTYPE_TI_TEMPLATE (arg)));
21185 return unify_no_common_base (explain_p && !same_template,
21186 r, parm, arg);
21187 }
21188 }
21189 }
21190 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21191 && (CLASSTYPE_TI_TEMPLATE (parm)
21192 == CLASSTYPE_TI_TEMPLATE (arg)))
21193 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21194 Then, we should unify `int' and `U'. */
21195 t = arg;
21196 else
21197 /* There's no chance of unification succeeding. */
21198 return unify_type_mismatch (explain_p, parm, arg);
21199
21200 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21201 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21202 }
21203 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21204 return unify_type_mismatch (explain_p, parm, arg);
21205 return unify_success (explain_p);
21206
21207 case METHOD_TYPE:
21208 case FUNCTION_TYPE:
21209 {
21210 unsigned int nargs;
21211 tree *args;
21212 tree a;
21213 unsigned int i;
21214
21215 if (TREE_CODE (arg) != TREE_CODE (parm))
21216 return unify_type_mismatch (explain_p, parm, arg);
21217
21218 /* CV qualifications for methods can never be deduced, they must
21219 match exactly. We need to check them explicitly here,
21220 because type_unification_real treats them as any other
21221 cv-qualified parameter. */
21222 if (TREE_CODE (parm) == METHOD_TYPE
21223 && (!check_cv_quals_for_unify
21224 (UNIFY_ALLOW_NONE,
21225 class_of_this_parm (arg),
21226 class_of_this_parm (parm))))
21227 return unify_cv_qual_mismatch (explain_p, parm, arg);
21228 if (TREE_CODE (arg) == FUNCTION_TYPE
21229 && type_memfn_quals (parm) != type_memfn_quals (arg))
21230 return unify_cv_qual_mismatch (explain_p, parm, arg);
21231 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21232 return unify_type_mismatch (explain_p, parm, arg);
21233
21234 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21235 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21236
21237 nargs = list_length (TYPE_ARG_TYPES (arg));
21238 args = XALLOCAVEC (tree, nargs);
21239 for (a = TYPE_ARG_TYPES (arg), i = 0;
21240 a != NULL_TREE && a != void_list_node;
21241 a = TREE_CHAIN (a), ++i)
21242 args[i] = TREE_VALUE (a);
21243 nargs = i;
21244
21245 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21246 args, nargs, 1, DEDUCE_EXACT,
21247 LOOKUP_NORMAL, NULL, explain_p))
21248 return 1;
21249
21250 if (flag_noexcept_type)
21251 {
21252 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21253 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21254 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21255 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21256 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21257 && uses_template_parms (TREE_PURPOSE (pspec)))
21258 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21259 TREE_PURPOSE (aspec),
21260 UNIFY_ALLOW_NONE, explain_p);
21261 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21262 return unify_type_mismatch (explain_p, parm, arg);
21263 }
21264
21265 return 0;
21266 }
21267
21268 case OFFSET_TYPE:
21269 /* Unify a pointer to member with a pointer to member function, which
21270 deduces the type of the member as a function type. */
21271 if (TYPE_PTRMEMFUNC_P (arg))
21272 {
21273 /* Check top-level cv qualifiers */
21274 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21275 return unify_cv_qual_mismatch (explain_p, parm, arg);
21276
21277 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21278 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21279 UNIFY_ALLOW_NONE, explain_p);
21280
21281 /* Determine the type of the function we are unifying against. */
21282 tree fntype = static_fn_type (arg);
21283
21284 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21285 }
21286
21287 if (TREE_CODE (arg) != OFFSET_TYPE)
21288 return unify_type_mismatch (explain_p, parm, arg);
21289 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21290 TYPE_OFFSET_BASETYPE (arg),
21291 UNIFY_ALLOW_NONE, explain_p);
21292 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21293 strict, explain_p);
21294
21295 case CONST_DECL:
21296 if (DECL_TEMPLATE_PARM_P (parm))
21297 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21298 if (arg != scalar_constant_value (parm))
21299 return unify_template_argument_mismatch (explain_p, parm, arg);
21300 return unify_success (explain_p);
21301
21302 case FIELD_DECL:
21303 case TEMPLATE_DECL:
21304 /* Matched cases are handled by the ARG == PARM test above. */
21305 return unify_template_argument_mismatch (explain_p, parm, arg);
21306
21307 case VAR_DECL:
21308 /* We might get a variable as a non-type template argument in parm if the
21309 corresponding parameter is type-dependent. Make any necessary
21310 adjustments based on whether arg is a reference. */
21311 if (CONSTANT_CLASS_P (arg))
21312 parm = fold_non_dependent_expr (parm);
21313 else if (REFERENCE_REF_P (arg))
21314 {
21315 tree sub = TREE_OPERAND (arg, 0);
21316 STRIP_NOPS (sub);
21317 if (TREE_CODE (sub) == ADDR_EXPR)
21318 arg = TREE_OPERAND (sub, 0);
21319 }
21320 /* Now use the normal expression code to check whether they match. */
21321 goto expr;
21322
21323 case TYPE_ARGUMENT_PACK:
21324 case NONTYPE_ARGUMENT_PACK:
21325 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21326 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21327
21328 case TYPEOF_TYPE:
21329 case DECLTYPE_TYPE:
21330 case UNDERLYING_TYPE:
21331 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21332 or UNDERLYING_TYPE nodes. */
21333 return unify_success (explain_p);
21334
21335 case ERROR_MARK:
21336 /* Unification fails if we hit an error node. */
21337 return unify_invalid (explain_p);
21338
21339 case INDIRECT_REF:
21340 if (REFERENCE_REF_P (parm))
21341 {
21342 bool pexp = PACK_EXPANSION_P (arg);
21343 if (pexp)
21344 arg = PACK_EXPANSION_PATTERN (arg);
21345 if (REFERENCE_REF_P (arg))
21346 arg = TREE_OPERAND (arg, 0);
21347 if (pexp)
21348 arg = make_pack_expansion (arg, complain);
21349 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21350 strict, explain_p);
21351 }
21352 /* FALLTHRU */
21353
21354 default:
21355 /* An unresolved overload is a nondeduced context. */
21356 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21357 return unify_success (explain_p);
21358 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21359 expr:
21360 /* We must be looking at an expression. This can happen with
21361 something like:
21362
21363 template <int I>
21364 void foo(S<I>, S<I + 2>);
21365
21366 This is a "nondeduced context":
21367
21368 [deduct.type]
21369
21370 The nondeduced contexts are:
21371
21372 --A type that is a template-id in which one or more of
21373 the template-arguments is an expression that references
21374 a template-parameter.
21375
21376 In these cases, we assume deduction succeeded, but don't
21377 actually infer any unifications. */
21378
21379 if (!uses_template_parms (parm)
21380 && !template_args_equal (parm, arg))
21381 return unify_expression_unequal (explain_p, parm, arg);
21382 else
21383 return unify_success (explain_p);
21384 }
21385 }
21386 #undef RECUR_AND_CHECK_FAILURE
21387 \f
21388 /* Note that DECL can be defined in this translation unit, if
21389 required. */
21390
21391 static void
21392 mark_definable (tree decl)
21393 {
21394 tree clone;
21395 DECL_NOT_REALLY_EXTERN (decl) = 1;
21396 FOR_EACH_CLONE (clone, decl)
21397 DECL_NOT_REALLY_EXTERN (clone) = 1;
21398 }
21399
21400 /* Called if RESULT is explicitly instantiated, or is a member of an
21401 explicitly instantiated class. */
21402
21403 void
21404 mark_decl_instantiated (tree result, int extern_p)
21405 {
21406 SET_DECL_EXPLICIT_INSTANTIATION (result);
21407
21408 /* If this entity has already been written out, it's too late to
21409 make any modifications. */
21410 if (TREE_ASM_WRITTEN (result))
21411 return;
21412
21413 /* For anonymous namespace we don't need to do anything. */
21414 if (decl_anon_ns_mem_p (result))
21415 {
21416 gcc_assert (!TREE_PUBLIC (result));
21417 return;
21418 }
21419
21420 if (TREE_CODE (result) != FUNCTION_DECL)
21421 /* The TREE_PUBLIC flag for function declarations will have been
21422 set correctly by tsubst. */
21423 TREE_PUBLIC (result) = 1;
21424
21425 /* This might have been set by an earlier implicit instantiation. */
21426 DECL_COMDAT (result) = 0;
21427
21428 if (extern_p)
21429 DECL_NOT_REALLY_EXTERN (result) = 0;
21430 else
21431 {
21432 mark_definable (result);
21433 mark_needed (result);
21434 /* Always make artificials weak. */
21435 if (DECL_ARTIFICIAL (result) && flag_weak)
21436 comdat_linkage (result);
21437 /* For WIN32 we also want to put explicit instantiations in
21438 linkonce sections. */
21439 else if (TREE_PUBLIC (result))
21440 maybe_make_one_only (result);
21441 }
21442
21443 /* If EXTERN_P, then this function will not be emitted -- unless
21444 followed by an explicit instantiation, at which point its linkage
21445 will be adjusted. If !EXTERN_P, then this function will be
21446 emitted here. In neither circumstance do we want
21447 import_export_decl to adjust the linkage. */
21448 DECL_INTERFACE_KNOWN (result) = 1;
21449 }
21450
21451 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21452 important template arguments. If any are missing, we check whether
21453 they're important by using error_mark_node for substituting into any
21454 args that were used for partial ordering (the ones between ARGS and END)
21455 and seeing if it bubbles up. */
21456
21457 static bool
21458 check_undeduced_parms (tree targs, tree args, tree end)
21459 {
21460 bool found = false;
21461 int i;
21462 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21463 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21464 {
21465 found = true;
21466 TREE_VEC_ELT (targs, i) = error_mark_node;
21467 }
21468 if (found)
21469 {
21470 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21471 if (substed == error_mark_node)
21472 return true;
21473 }
21474 return false;
21475 }
21476
21477 /* Given two function templates PAT1 and PAT2, return:
21478
21479 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21480 -1 if PAT2 is more specialized than PAT1.
21481 0 if neither is more specialized.
21482
21483 LEN indicates the number of parameters we should consider
21484 (defaulted parameters should not be considered).
21485
21486 The 1998 std underspecified function template partial ordering, and
21487 DR214 addresses the issue. We take pairs of arguments, one from
21488 each of the templates, and deduce them against each other. One of
21489 the templates will be more specialized if all the *other*
21490 template's arguments deduce against its arguments and at least one
21491 of its arguments *does* *not* deduce against the other template's
21492 corresponding argument. Deduction is done as for class templates.
21493 The arguments used in deduction have reference and top level cv
21494 qualifiers removed. Iff both arguments were originally reference
21495 types *and* deduction succeeds in both directions, an lvalue reference
21496 wins against an rvalue reference and otherwise the template
21497 with the more cv-qualified argument wins for that pairing (if
21498 neither is more cv-qualified, they both are equal). Unlike regular
21499 deduction, after all the arguments have been deduced in this way,
21500 we do *not* verify the deduced template argument values can be
21501 substituted into non-deduced contexts.
21502
21503 The logic can be a bit confusing here, because we look at deduce1 and
21504 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21505 can find template arguments for pat1 to make arg1 look like arg2, that
21506 means that arg2 is at least as specialized as arg1. */
21507
21508 int
21509 more_specialized_fn (tree pat1, tree pat2, int len)
21510 {
21511 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21512 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21513 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21514 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21515 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21516 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21517 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21518 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21519 tree origs1, origs2;
21520 bool lose1 = false;
21521 bool lose2 = false;
21522
21523 /* Remove the this parameter from non-static member functions. If
21524 one is a non-static member function and the other is not a static
21525 member function, remove the first parameter from that function
21526 also. This situation occurs for operator functions where we
21527 locate both a member function (with this pointer) and non-member
21528 operator (with explicit first operand). */
21529 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21530 {
21531 len--; /* LEN is the number of significant arguments for DECL1 */
21532 args1 = TREE_CHAIN (args1);
21533 if (!DECL_STATIC_FUNCTION_P (decl2))
21534 args2 = TREE_CHAIN (args2);
21535 }
21536 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21537 {
21538 args2 = TREE_CHAIN (args2);
21539 if (!DECL_STATIC_FUNCTION_P (decl1))
21540 {
21541 len--;
21542 args1 = TREE_CHAIN (args1);
21543 }
21544 }
21545
21546 /* If only one is a conversion operator, they are unordered. */
21547 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21548 return 0;
21549
21550 /* Consider the return type for a conversion function */
21551 if (DECL_CONV_FN_P (decl1))
21552 {
21553 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21554 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21555 len++;
21556 }
21557
21558 processing_template_decl++;
21559
21560 origs1 = args1;
21561 origs2 = args2;
21562
21563 while (len--
21564 /* Stop when an ellipsis is seen. */
21565 && args1 != NULL_TREE && args2 != NULL_TREE)
21566 {
21567 tree arg1 = TREE_VALUE (args1);
21568 tree arg2 = TREE_VALUE (args2);
21569 int deduce1, deduce2;
21570 int quals1 = -1;
21571 int quals2 = -1;
21572 int ref1 = 0;
21573 int ref2 = 0;
21574
21575 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21576 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21577 {
21578 /* When both arguments are pack expansions, we need only
21579 unify the patterns themselves. */
21580 arg1 = PACK_EXPANSION_PATTERN (arg1);
21581 arg2 = PACK_EXPANSION_PATTERN (arg2);
21582
21583 /* This is the last comparison we need to do. */
21584 len = 0;
21585 }
21586
21587 /* DR 1847: If a particular P contains no template-parameters that
21588 participate in template argument deduction, that P is not used to
21589 determine the ordering. */
21590 if (!uses_deducible_template_parms (arg1)
21591 && !uses_deducible_template_parms (arg2))
21592 goto next;
21593
21594 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21595 {
21596 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21597 arg1 = TREE_TYPE (arg1);
21598 quals1 = cp_type_quals (arg1);
21599 }
21600
21601 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21602 {
21603 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21604 arg2 = TREE_TYPE (arg2);
21605 quals2 = cp_type_quals (arg2);
21606 }
21607
21608 arg1 = TYPE_MAIN_VARIANT (arg1);
21609 arg2 = TYPE_MAIN_VARIANT (arg2);
21610
21611 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21612 {
21613 int i, len2 = remaining_arguments (args2);
21614 tree parmvec = make_tree_vec (1);
21615 tree argvec = make_tree_vec (len2);
21616 tree ta = args2;
21617
21618 /* Setup the parameter vector, which contains only ARG1. */
21619 TREE_VEC_ELT (parmvec, 0) = arg1;
21620
21621 /* Setup the argument vector, which contains the remaining
21622 arguments. */
21623 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21624 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21625
21626 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21627 argvec, DEDUCE_EXACT,
21628 /*subr=*/true, /*explain_p=*/false)
21629 == 0);
21630
21631 /* We cannot deduce in the other direction, because ARG1 is
21632 a pack expansion but ARG2 is not. */
21633 deduce2 = 0;
21634 }
21635 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21636 {
21637 int i, len1 = remaining_arguments (args1);
21638 tree parmvec = make_tree_vec (1);
21639 tree argvec = make_tree_vec (len1);
21640 tree ta = args1;
21641
21642 /* Setup the parameter vector, which contains only ARG1. */
21643 TREE_VEC_ELT (parmvec, 0) = arg2;
21644
21645 /* Setup the argument vector, which contains the remaining
21646 arguments. */
21647 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21648 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21649
21650 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21651 argvec, DEDUCE_EXACT,
21652 /*subr=*/true, /*explain_p=*/false)
21653 == 0);
21654
21655 /* We cannot deduce in the other direction, because ARG2 is
21656 a pack expansion but ARG1 is not.*/
21657 deduce1 = 0;
21658 }
21659
21660 else
21661 {
21662 /* The normal case, where neither argument is a pack
21663 expansion. */
21664 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21665 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21666 == 0);
21667 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21668 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21669 == 0);
21670 }
21671
21672 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21673 arg2, then arg2 is not as specialized as arg1. */
21674 if (!deduce1)
21675 lose2 = true;
21676 if (!deduce2)
21677 lose1 = true;
21678
21679 /* "If, for a given type, deduction succeeds in both directions
21680 (i.e., the types are identical after the transformations above)
21681 and both P and A were reference types (before being replaced with
21682 the type referred to above):
21683 - if the type from the argument template was an lvalue reference and
21684 the type from the parameter template was not, the argument type is
21685 considered to be more specialized than the other; otherwise,
21686 - if the type from the argument template is more cv-qualified
21687 than the type from the parameter template (as described above),
21688 the argument type is considered to be more specialized than the other;
21689 otherwise,
21690 - neither type is more specialized than the other." */
21691
21692 if (deduce1 && deduce2)
21693 {
21694 if (ref1 && ref2 && ref1 != ref2)
21695 {
21696 if (ref1 > ref2)
21697 lose1 = true;
21698 else
21699 lose2 = true;
21700 }
21701 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21702 {
21703 if ((quals1 & quals2) == quals2)
21704 lose2 = true;
21705 if ((quals1 & quals2) == quals1)
21706 lose1 = true;
21707 }
21708 }
21709
21710 if (lose1 && lose2)
21711 /* We've failed to deduce something in either direction.
21712 These must be unordered. */
21713 break;
21714
21715 next:
21716
21717 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21718 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21719 /* We have already processed all of the arguments in our
21720 handing of the pack expansion type. */
21721 len = 0;
21722
21723 args1 = TREE_CHAIN (args1);
21724 args2 = TREE_CHAIN (args2);
21725 }
21726
21727 /* "In most cases, all template parameters must have values in order for
21728 deduction to succeed, but for partial ordering purposes a template
21729 parameter may remain without a value provided it is not used in the
21730 types being used for partial ordering."
21731
21732 Thus, if we are missing any of the targs1 we need to substitute into
21733 origs1, then pat2 is not as specialized as pat1. This can happen when
21734 there is a nondeduced context. */
21735 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21736 lose2 = true;
21737 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21738 lose1 = true;
21739
21740 processing_template_decl--;
21741
21742 /* If both deductions succeed, the partial ordering selects the more
21743 constrained template. */
21744 if (!lose1 && !lose2)
21745 {
21746 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21747 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21748 lose1 = !subsumes_constraints (c1, c2);
21749 lose2 = !subsumes_constraints (c2, c1);
21750 }
21751
21752 /* All things being equal, if the next argument is a pack expansion
21753 for one function but not for the other, prefer the
21754 non-variadic function. FIXME this is bogus; see c++/41958. */
21755 if (lose1 == lose2
21756 && args1 && TREE_VALUE (args1)
21757 && args2 && TREE_VALUE (args2))
21758 {
21759 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21760 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21761 }
21762
21763 if (lose1 == lose2)
21764 return 0;
21765 else if (!lose1)
21766 return 1;
21767 else
21768 return -1;
21769 }
21770
21771 /* Determine which of two partial specializations of TMPL is more
21772 specialized.
21773
21774 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21775 to the first partial specialization. The TREE_PURPOSE is the
21776 innermost set of template parameters for the partial
21777 specialization. PAT2 is similar, but for the second template.
21778
21779 Return 1 if the first partial specialization is more specialized;
21780 -1 if the second is more specialized; 0 if neither is more
21781 specialized.
21782
21783 See [temp.class.order] for information about determining which of
21784 two templates is more specialized. */
21785
21786 static int
21787 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21788 {
21789 tree targs;
21790 int winner = 0;
21791 bool any_deductions = false;
21792
21793 tree tmpl1 = TREE_VALUE (pat1);
21794 tree tmpl2 = TREE_VALUE (pat2);
21795 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21796 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21797
21798 /* Just like what happens for functions, if we are ordering between
21799 different template specializations, we may encounter dependent
21800 types in the arguments, and we need our dependency check functions
21801 to behave correctly. */
21802 ++processing_template_decl;
21803 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21804 if (targs)
21805 {
21806 --winner;
21807 any_deductions = true;
21808 }
21809
21810 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21811 if (targs)
21812 {
21813 ++winner;
21814 any_deductions = true;
21815 }
21816 --processing_template_decl;
21817
21818 /* If both deductions succeed, the partial ordering selects the more
21819 constrained template. */
21820 if (!winner && any_deductions)
21821 return more_constrained (tmpl1, tmpl2);
21822
21823 /* In the case of a tie where at least one of the templates
21824 has a parameter pack at the end, the template with the most
21825 non-packed parameters wins. */
21826 if (winner == 0
21827 && any_deductions
21828 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21829 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21830 {
21831 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21832 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21833 int len1 = TREE_VEC_LENGTH (args1);
21834 int len2 = TREE_VEC_LENGTH (args2);
21835
21836 /* We don't count the pack expansion at the end. */
21837 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21838 --len1;
21839 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21840 --len2;
21841
21842 if (len1 > len2)
21843 return 1;
21844 else if (len1 < len2)
21845 return -1;
21846 }
21847
21848 return winner;
21849 }
21850
21851 /* Return the template arguments that will produce the function signature
21852 DECL from the function template FN, with the explicit template
21853 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21854 also match. Return NULL_TREE if no satisfactory arguments could be
21855 found. */
21856
21857 static tree
21858 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21859 {
21860 int ntparms = DECL_NTPARMS (fn);
21861 tree targs = make_tree_vec (ntparms);
21862 tree decl_type = TREE_TYPE (decl);
21863 tree decl_arg_types;
21864 tree *args;
21865 unsigned int nargs, ix;
21866 tree arg;
21867
21868 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21869
21870 /* Never do unification on the 'this' parameter. */
21871 decl_arg_types = skip_artificial_parms_for (decl,
21872 TYPE_ARG_TYPES (decl_type));
21873
21874 nargs = list_length (decl_arg_types);
21875 args = XALLOCAVEC (tree, nargs);
21876 for (arg = decl_arg_types, ix = 0;
21877 arg != NULL_TREE && arg != void_list_node;
21878 arg = TREE_CHAIN (arg), ++ix)
21879 args[ix] = TREE_VALUE (arg);
21880
21881 if (fn_type_unification (fn, explicit_args, targs,
21882 args, ix,
21883 (check_rettype || DECL_CONV_FN_P (fn)
21884 ? TREE_TYPE (decl_type) : NULL_TREE),
21885 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21886 /*decltype*/false)
21887 == error_mark_node)
21888 return NULL_TREE;
21889
21890 return targs;
21891 }
21892
21893 /* Return the innermost template arguments that, when applied to a partial
21894 specialization SPEC_TMPL of TMPL, yield the ARGS.
21895
21896 For example, suppose we have:
21897
21898 template <class T, class U> struct S {};
21899 template <class T> struct S<T*, int> {};
21900
21901 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21902 partial specialization and the ARGS will be {double*, int}. The resulting
21903 vector will be {double}, indicating that `T' is bound to `double'. */
21904
21905 static tree
21906 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21907 {
21908 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21909 tree spec_args
21910 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21911 int i, ntparms = TREE_VEC_LENGTH (tparms);
21912 tree deduced_args;
21913 tree innermost_deduced_args;
21914
21915 innermost_deduced_args = make_tree_vec (ntparms);
21916 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21917 {
21918 deduced_args = copy_node (args);
21919 SET_TMPL_ARGS_LEVEL (deduced_args,
21920 TMPL_ARGS_DEPTH (deduced_args),
21921 innermost_deduced_args);
21922 }
21923 else
21924 deduced_args = innermost_deduced_args;
21925
21926 bool tried_array_deduction = (cxx_dialect < cxx17);
21927 again:
21928 if (unify (tparms, deduced_args,
21929 INNERMOST_TEMPLATE_ARGS (spec_args),
21930 INNERMOST_TEMPLATE_ARGS (args),
21931 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21932 return NULL_TREE;
21933
21934 for (i = 0; i < ntparms; ++i)
21935 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21936 {
21937 if (!tried_array_deduction)
21938 {
21939 try_array_deduction (tparms, innermost_deduced_args,
21940 INNERMOST_TEMPLATE_ARGS (spec_args));
21941 tried_array_deduction = true;
21942 if (TREE_VEC_ELT (innermost_deduced_args, i))
21943 goto again;
21944 }
21945 return NULL_TREE;
21946 }
21947
21948 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21949 if (!push_tinst_level (tinst))
21950 {
21951 excessive_deduction_depth = true;
21952 return NULL_TREE;
21953 }
21954
21955 /* Verify that nondeduced template arguments agree with the type
21956 obtained from argument deduction.
21957
21958 For example:
21959
21960 struct A { typedef int X; };
21961 template <class T, class U> struct C {};
21962 template <class T> struct C<T, typename T::X> {};
21963
21964 Then with the instantiation `C<A, int>', we can deduce that
21965 `T' is `A' but unify () does not check whether `typename T::X'
21966 is `int'. */
21967 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21968
21969 if (spec_args != error_mark_node)
21970 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21971 INNERMOST_TEMPLATE_ARGS (spec_args),
21972 tmpl, tf_none, false, false);
21973
21974 pop_tinst_level ();
21975
21976 if (spec_args == error_mark_node
21977 /* We only need to check the innermost arguments; the other
21978 arguments will always agree. */
21979 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21980 INNERMOST_TEMPLATE_ARGS (args)))
21981 return NULL_TREE;
21982
21983 /* Now that we have bindings for all of the template arguments,
21984 ensure that the arguments deduced for the template template
21985 parameters have compatible template parameter lists. See the use
21986 of template_template_parm_bindings_ok_p in fn_type_unification
21987 for more information. */
21988 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21989 return NULL_TREE;
21990
21991 return deduced_args;
21992 }
21993
21994 // Compare two function templates T1 and T2 by deducing bindings
21995 // from one against the other. If both deductions succeed, compare
21996 // constraints to see which is more constrained.
21997 static int
21998 more_specialized_inst (tree t1, tree t2)
21999 {
22000 int fate = 0;
22001 int count = 0;
22002
22003 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22004 {
22005 --fate;
22006 ++count;
22007 }
22008
22009 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22010 {
22011 ++fate;
22012 ++count;
22013 }
22014
22015 // If both deductions succeed, then one may be more constrained.
22016 if (count == 2 && fate == 0)
22017 fate = more_constrained (t1, t2);
22018
22019 return fate;
22020 }
22021
22022 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22023 Return the TREE_LIST node with the most specialized template, if
22024 any. If there is no most specialized template, the error_mark_node
22025 is returned.
22026
22027 Note that this function does not look at, or modify, the
22028 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22029 returned is one of the elements of INSTANTIATIONS, callers may
22030 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22031 and retrieve it from the value returned. */
22032
22033 tree
22034 most_specialized_instantiation (tree templates)
22035 {
22036 tree fn, champ;
22037
22038 ++processing_template_decl;
22039
22040 champ = templates;
22041 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22042 {
22043 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22044 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22045 if (fate == -1)
22046 champ = fn;
22047 else if (!fate)
22048 {
22049 /* Equally specialized, move to next function. If there
22050 is no next function, nothing's most specialized. */
22051 fn = TREE_CHAIN (fn);
22052 champ = fn;
22053 if (!fn)
22054 break;
22055 }
22056 }
22057
22058 if (champ)
22059 /* Now verify that champ is better than everything earlier in the
22060 instantiation list. */
22061 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22062 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22063 {
22064 champ = NULL_TREE;
22065 break;
22066 }
22067 }
22068
22069 processing_template_decl--;
22070
22071 if (!champ)
22072 return error_mark_node;
22073
22074 return champ;
22075 }
22076
22077 /* If DECL is a specialization of some template, return the most
22078 general such template. Otherwise, returns NULL_TREE.
22079
22080 For example, given:
22081
22082 template <class T> struct S { template <class U> void f(U); };
22083
22084 if TMPL is `template <class U> void S<int>::f(U)' this will return
22085 the full template. This function will not trace past partial
22086 specializations, however. For example, given in addition:
22087
22088 template <class T> struct S<T*> { template <class U> void f(U); };
22089
22090 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22091 `template <class T> template <class U> S<T*>::f(U)'. */
22092
22093 tree
22094 most_general_template (tree decl)
22095 {
22096 if (TREE_CODE (decl) != TEMPLATE_DECL)
22097 {
22098 if (tree tinfo = get_template_info (decl))
22099 decl = TI_TEMPLATE (tinfo);
22100 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22101 template friend, or a FIELD_DECL for a capture pack. */
22102 if (TREE_CODE (decl) != TEMPLATE_DECL)
22103 return NULL_TREE;
22104 }
22105
22106 /* Look for more and more general templates. */
22107 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22108 {
22109 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22110 (See cp-tree.h for details.) */
22111 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22112 break;
22113
22114 if (CLASS_TYPE_P (TREE_TYPE (decl))
22115 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22116 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22117 break;
22118
22119 /* Stop if we run into an explicitly specialized class template. */
22120 if (!DECL_NAMESPACE_SCOPE_P (decl)
22121 && DECL_CONTEXT (decl)
22122 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22123 break;
22124
22125 decl = DECL_TI_TEMPLATE (decl);
22126 }
22127
22128 return decl;
22129 }
22130
22131 /* Return the most specialized of the template partial specializations
22132 which can produce TARGET, a specialization of some class or variable
22133 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22134 a TEMPLATE_DECL node corresponding to the partial specialization, while
22135 the TREE_PURPOSE is the set of template arguments that must be
22136 substituted into the template pattern in order to generate TARGET.
22137
22138 If the choice of partial specialization is ambiguous, a diagnostic
22139 is issued, and the error_mark_node is returned. If there are no
22140 partial specializations matching TARGET, then NULL_TREE is
22141 returned, indicating that the primary template should be used. */
22142
22143 static tree
22144 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22145 {
22146 tree list = NULL_TREE;
22147 tree t;
22148 tree champ;
22149 int fate;
22150 bool ambiguous_p;
22151 tree outer_args = NULL_TREE;
22152 tree tmpl, args;
22153
22154 if (TYPE_P (target))
22155 {
22156 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22157 tmpl = TI_TEMPLATE (tinfo);
22158 args = TI_ARGS (tinfo);
22159 }
22160 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22161 {
22162 tmpl = TREE_OPERAND (target, 0);
22163 args = TREE_OPERAND (target, 1);
22164 }
22165 else if (VAR_P (target))
22166 {
22167 tree tinfo = DECL_TEMPLATE_INFO (target);
22168 tmpl = TI_TEMPLATE (tinfo);
22169 args = TI_ARGS (tinfo);
22170 }
22171 else
22172 gcc_unreachable ();
22173
22174 tree main_tmpl = most_general_template (tmpl);
22175
22176 /* For determining which partial specialization to use, only the
22177 innermost args are interesting. */
22178 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22179 {
22180 outer_args = strip_innermost_template_args (args, 1);
22181 args = INNERMOST_TEMPLATE_ARGS (args);
22182 }
22183
22184 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22185 {
22186 tree spec_args;
22187 tree spec_tmpl = TREE_VALUE (t);
22188
22189 if (outer_args)
22190 {
22191 /* Substitute in the template args from the enclosing class. */
22192 ++processing_template_decl;
22193 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22194 --processing_template_decl;
22195 }
22196
22197 if (spec_tmpl == error_mark_node)
22198 return error_mark_node;
22199
22200 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22201 if (spec_args)
22202 {
22203 if (outer_args)
22204 spec_args = add_to_template_args (outer_args, spec_args);
22205
22206 /* Keep the candidate only if the constraints are satisfied,
22207 or if we're not compiling with concepts. */
22208 if (!flag_concepts
22209 || constraints_satisfied_p (spec_tmpl, spec_args))
22210 {
22211 list = tree_cons (spec_args, TREE_VALUE (t), list);
22212 TREE_TYPE (list) = TREE_TYPE (t);
22213 }
22214 }
22215 }
22216
22217 if (! list)
22218 return NULL_TREE;
22219
22220 ambiguous_p = false;
22221 t = list;
22222 champ = t;
22223 t = TREE_CHAIN (t);
22224 for (; t; t = TREE_CHAIN (t))
22225 {
22226 fate = more_specialized_partial_spec (tmpl, champ, t);
22227 if (fate == 1)
22228 ;
22229 else
22230 {
22231 if (fate == 0)
22232 {
22233 t = TREE_CHAIN (t);
22234 if (! t)
22235 {
22236 ambiguous_p = true;
22237 break;
22238 }
22239 }
22240 champ = t;
22241 }
22242 }
22243
22244 if (!ambiguous_p)
22245 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22246 {
22247 fate = more_specialized_partial_spec (tmpl, champ, t);
22248 if (fate != 1)
22249 {
22250 ambiguous_p = true;
22251 break;
22252 }
22253 }
22254
22255 if (ambiguous_p)
22256 {
22257 const char *str;
22258 char *spaces = NULL;
22259 if (!(complain & tf_error))
22260 return error_mark_node;
22261 if (TYPE_P (target))
22262 error ("ambiguous template instantiation for %q#T", target);
22263 else
22264 error ("ambiguous template instantiation for %q#D", target);
22265 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22266 for (t = list; t; t = TREE_CHAIN (t))
22267 {
22268 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22269 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22270 "%s %#qS", spaces ? spaces : str, subst);
22271 spaces = spaces ? spaces : get_spaces (str);
22272 }
22273 free (spaces);
22274 return error_mark_node;
22275 }
22276
22277 return champ;
22278 }
22279
22280 /* Explicitly instantiate DECL. */
22281
22282 void
22283 do_decl_instantiation (tree decl, tree storage)
22284 {
22285 tree result = NULL_TREE;
22286 int extern_p = 0;
22287
22288 if (!decl || decl == error_mark_node)
22289 /* An error occurred, for which grokdeclarator has already issued
22290 an appropriate message. */
22291 return;
22292 else if (! DECL_LANG_SPECIFIC (decl))
22293 {
22294 error ("explicit instantiation of non-template %q#D", decl);
22295 return;
22296 }
22297
22298 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22299 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22300
22301 if (VAR_P (decl) && !var_templ)
22302 {
22303 /* There is an asymmetry here in the way VAR_DECLs and
22304 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22305 the latter, the DECL we get back will be marked as a
22306 template instantiation, and the appropriate
22307 DECL_TEMPLATE_INFO will be set up. This does not happen for
22308 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22309 should handle VAR_DECLs as it currently handles
22310 FUNCTION_DECLs. */
22311 if (!DECL_CLASS_SCOPE_P (decl))
22312 {
22313 error ("%qD is not a static data member of a class template", decl);
22314 return;
22315 }
22316 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22317 if (!result || !VAR_P (result))
22318 {
22319 error ("no matching template for %qD found", decl);
22320 return;
22321 }
22322 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22323 {
22324 error ("type %qT for explicit instantiation %qD does not match "
22325 "declared type %qT", TREE_TYPE (result), decl,
22326 TREE_TYPE (decl));
22327 return;
22328 }
22329 }
22330 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22331 {
22332 error ("explicit instantiation of %q#D", decl);
22333 return;
22334 }
22335 else
22336 result = decl;
22337
22338 /* Check for various error cases. Note that if the explicit
22339 instantiation is valid the RESULT will currently be marked as an
22340 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22341 until we get here. */
22342
22343 if (DECL_TEMPLATE_SPECIALIZATION (result))
22344 {
22345 /* DR 259 [temp.spec].
22346
22347 Both an explicit instantiation and a declaration of an explicit
22348 specialization shall not appear in a program unless the explicit
22349 instantiation follows a declaration of the explicit specialization.
22350
22351 For a given set of template parameters, if an explicit
22352 instantiation of a template appears after a declaration of an
22353 explicit specialization for that template, the explicit
22354 instantiation has no effect. */
22355 return;
22356 }
22357 else if (DECL_EXPLICIT_INSTANTIATION (result))
22358 {
22359 /* [temp.spec]
22360
22361 No program shall explicitly instantiate any template more
22362 than once.
22363
22364 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22365 the first instantiation was `extern' and the second is not,
22366 and EXTERN_P for the opposite case. */
22367 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22368 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22369 /* If an "extern" explicit instantiation follows an ordinary
22370 explicit instantiation, the template is instantiated. */
22371 if (extern_p)
22372 return;
22373 }
22374 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22375 {
22376 error ("no matching template for %qD found", result);
22377 return;
22378 }
22379 else if (!DECL_TEMPLATE_INFO (result))
22380 {
22381 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22382 return;
22383 }
22384
22385 if (storage == NULL_TREE)
22386 ;
22387 else if (storage == ridpointers[(int) RID_EXTERN])
22388 {
22389 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22390 pedwarn (input_location, OPT_Wpedantic,
22391 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22392 "instantiations");
22393 extern_p = 1;
22394 }
22395 else
22396 error ("storage class %qD applied to template instantiation", storage);
22397
22398 check_explicit_instantiation_namespace (result);
22399 mark_decl_instantiated (result, extern_p);
22400 if (! extern_p)
22401 instantiate_decl (result, /*defer_ok=*/true,
22402 /*expl_inst_class_mem_p=*/false);
22403 }
22404
22405 static void
22406 mark_class_instantiated (tree t, int extern_p)
22407 {
22408 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22409 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22410 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22411 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22412 if (! extern_p)
22413 {
22414 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22415 rest_of_type_compilation (t, 1);
22416 }
22417 }
22418
22419 /* Called from do_type_instantiation through binding_table_foreach to
22420 do recursive instantiation for the type bound in ENTRY. */
22421 static void
22422 bt_instantiate_type_proc (binding_entry entry, void *data)
22423 {
22424 tree storage = *(tree *) data;
22425
22426 if (MAYBE_CLASS_TYPE_P (entry->type)
22427 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22428 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22429 }
22430
22431 /* Perform an explicit instantiation of template class T. STORAGE, if
22432 non-null, is the RID for extern, inline or static. COMPLAIN is
22433 nonzero if this is called from the parser, zero if called recursively,
22434 since the standard is unclear (as detailed below). */
22435
22436 void
22437 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22438 {
22439 int extern_p = 0;
22440 int nomem_p = 0;
22441 int static_p = 0;
22442 int previous_instantiation_extern_p = 0;
22443
22444 if (TREE_CODE (t) == TYPE_DECL)
22445 t = TREE_TYPE (t);
22446
22447 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22448 {
22449 tree tmpl =
22450 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22451 if (tmpl)
22452 error ("explicit instantiation of non-class template %qD", tmpl);
22453 else
22454 error ("explicit instantiation of non-template type %qT", t);
22455 return;
22456 }
22457
22458 complete_type (t);
22459
22460 if (!COMPLETE_TYPE_P (t))
22461 {
22462 if (complain & tf_error)
22463 error ("explicit instantiation of %q#T before definition of template",
22464 t);
22465 return;
22466 }
22467
22468 if (storage != NULL_TREE)
22469 {
22470 if (!in_system_header_at (input_location))
22471 {
22472 if (storage == ridpointers[(int) RID_EXTERN])
22473 {
22474 if (cxx_dialect == cxx98)
22475 pedwarn (input_location, OPT_Wpedantic,
22476 "ISO C++ 1998 forbids the use of %<extern%> on "
22477 "explicit instantiations");
22478 }
22479 else
22480 pedwarn (input_location, OPT_Wpedantic,
22481 "ISO C++ forbids the use of %qE"
22482 " on explicit instantiations", storage);
22483 }
22484
22485 if (storage == ridpointers[(int) RID_INLINE])
22486 nomem_p = 1;
22487 else if (storage == ridpointers[(int) RID_EXTERN])
22488 extern_p = 1;
22489 else if (storage == ridpointers[(int) RID_STATIC])
22490 static_p = 1;
22491 else
22492 {
22493 error ("storage class %qD applied to template instantiation",
22494 storage);
22495 extern_p = 0;
22496 }
22497 }
22498
22499 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22500 {
22501 /* DR 259 [temp.spec].
22502
22503 Both an explicit instantiation and a declaration of an explicit
22504 specialization shall not appear in a program unless the explicit
22505 instantiation follows a declaration of the explicit specialization.
22506
22507 For a given set of template parameters, if an explicit
22508 instantiation of a template appears after a declaration of an
22509 explicit specialization for that template, the explicit
22510 instantiation has no effect. */
22511 return;
22512 }
22513 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22514 {
22515 /* [temp.spec]
22516
22517 No program shall explicitly instantiate any template more
22518 than once.
22519
22520 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22521 instantiation was `extern'. If EXTERN_P then the second is.
22522 These cases are OK. */
22523 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22524
22525 if (!previous_instantiation_extern_p && !extern_p
22526 && (complain & tf_error))
22527 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22528
22529 /* If we've already instantiated the template, just return now. */
22530 if (!CLASSTYPE_INTERFACE_ONLY (t))
22531 return;
22532 }
22533
22534 check_explicit_instantiation_namespace (TYPE_NAME (t));
22535 mark_class_instantiated (t, extern_p);
22536
22537 if (nomem_p)
22538 return;
22539
22540 /* In contrast to implicit instantiation, where only the
22541 declarations, and not the definitions, of members are
22542 instantiated, we have here:
22543
22544 [temp.explicit]
22545
22546 The explicit instantiation of a class template specialization
22547 implies the instantiation of all of its members not
22548 previously explicitly specialized in the translation unit
22549 containing the explicit instantiation.
22550
22551 Of course, we can't instantiate member template classes, since we
22552 don't have any arguments for them. Note that the standard is
22553 unclear on whether the instantiation of the members are
22554 *explicit* instantiations or not. However, the most natural
22555 interpretation is that it should be an explicit
22556 instantiation. */
22557 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22558 if ((VAR_P (fld)
22559 || (TREE_CODE (fld) == FUNCTION_DECL
22560 && !static_p
22561 && user_provided_p (fld)))
22562 && DECL_TEMPLATE_INSTANTIATION (fld))
22563 {
22564 mark_decl_instantiated (fld, extern_p);
22565 if (! extern_p)
22566 instantiate_decl (fld, /*defer_ok=*/true,
22567 /*expl_inst_class_mem_p=*/true);
22568 }
22569
22570 if (CLASSTYPE_NESTED_UTDS (t))
22571 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22572 bt_instantiate_type_proc, &storage);
22573 }
22574
22575 /* Given a function DECL, which is a specialization of TMPL, modify
22576 DECL to be a re-instantiation of TMPL with the same template
22577 arguments. TMPL should be the template into which tsubst'ing
22578 should occur for DECL, not the most general template.
22579
22580 One reason for doing this is a scenario like this:
22581
22582 template <class T>
22583 void f(const T&, int i);
22584
22585 void g() { f(3, 7); }
22586
22587 template <class T>
22588 void f(const T& t, const int i) { }
22589
22590 Note that when the template is first instantiated, with
22591 instantiate_template, the resulting DECL will have no name for the
22592 first parameter, and the wrong type for the second. So, when we go
22593 to instantiate the DECL, we regenerate it. */
22594
22595 static void
22596 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22597 {
22598 /* The arguments used to instantiate DECL, from the most general
22599 template. */
22600 tree code_pattern;
22601
22602 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22603
22604 /* Make sure that we can see identifiers, and compute access
22605 correctly. */
22606 push_access_scope (decl);
22607
22608 if (TREE_CODE (decl) == FUNCTION_DECL)
22609 {
22610 tree decl_parm;
22611 tree pattern_parm;
22612 tree specs;
22613 int args_depth;
22614 int parms_depth;
22615
22616 args_depth = TMPL_ARGS_DEPTH (args);
22617 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22618 if (args_depth > parms_depth)
22619 args = get_innermost_template_args (args, parms_depth);
22620
22621 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22622 args, tf_error, NULL_TREE,
22623 /*defer_ok*/false);
22624 if (specs && specs != error_mark_node)
22625 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22626 specs);
22627
22628 /* Merge parameter declarations. */
22629 decl_parm = skip_artificial_parms_for (decl,
22630 DECL_ARGUMENTS (decl));
22631 pattern_parm
22632 = skip_artificial_parms_for (code_pattern,
22633 DECL_ARGUMENTS (code_pattern));
22634 while (decl_parm && !DECL_PACK_P (pattern_parm))
22635 {
22636 tree parm_type;
22637 tree attributes;
22638
22639 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22640 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22641 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22642 NULL_TREE);
22643 parm_type = type_decays_to (parm_type);
22644 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22645 TREE_TYPE (decl_parm) = parm_type;
22646 attributes = DECL_ATTRIBUTES (pattern_parm);
22647 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22648 {
22649 DECL_ATTRIBUTES (decl_parm) = attributes;
22650 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22651 }
22652 decl_parm = DECL_CHAIN (decl_parm);
22653 pattern_parm = DECL_CHAIN (pattern_parm);
22654 }
22655 /* Merge any parameters that match with the function parameter
22656 pack. */
22657 if (pattern_parm && DECL_PACK_P (pattern_parm))
22658 {
22659 int i, len;
22660 tree expanded_types;
22661 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22662 the parameters in this function parameter pack. */
22663 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22664 args, tf_error, NULL_TREE);
22665 len = TREE_VEC_LENGTH (expanded_types);
22666 for (i = 0; i < len; i++)
22667 {
22668 tree parm_type;
22669 tree attributes;
22670
22671 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22672 /* Rename the parameter to include the index. */
22673 DECL_NAME (decl_parm) =
22674 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22675 parm_type = TREE_VEC_ELT (expanded_types, i);
22676 parm_type = type_decays_to (parm_type);
22677 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22678 TREE_TYPE (decl_parm) = parm_type;
22679 attributes = DECL_ATTRIBUTES (pattern_parm);
22680 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22681 {
22682 DECL_ATTRIBUTES (decl_parm) = attributes;
22683 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22684 }
22685 decl_parm = DECL_CHAIN (decl_parm);
22686 }
22687 }
22688 /* Merge additional specifiers from the CODE_PATTERN. */
22689 if (DECL_DECLARED_INLINE_P (code_pattern)
22690 && !DECL_DECLARED_INLINE_P (decl))
22691 DECL_DECLARED_INLINE_P (decl) = 1;
22692 }
22693 else if (VAR_P (decl))
22694 {
22695 start_lambda_scope (decl);
22696 DECL_INITIAL (decl) =
22697 tsubst_expr (DECL_INITIAL (code_pattern), args,
22698 tf_error, DECL_TI_TEMPLATE (decl),
22699 /*integral_constant_expression_p=*/false);
22700 finish_lambda_scope ();
22701 if (VAR_HAD_UNKNOWN_BOUND (decl))
22702 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22703 tf_error, DECL_TI_TEMPLATE (decl));
22704 }
22705 else
22706 gcc_unreachable ();
22707
22708 pop_access_scope (decl);
22709 }
22710
22711 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22712 substituted to get DECL. */
22713
22714 tree
22715 template_for_substitution (tree decl)
22716 {
22717 tree tmpl = DECL_TI_TEMPLATE (decl);
22718
22719 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22720 for the instantiation. This is not always the most general
22721 template. Consider, for example:
22722
22723 template <class T>
22724 struct S { template <class U> void f();
22725 template <> void f<int>(); };
22726
22727 and an instantiation of S<double>::f<int>. We want TD to be the
22728 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22729 while (/* An instantiation cannot have a definition, so we need a
22730 more general template. */
22731 DECL_TEMPLATE_INSTANTIATION (tmpl)
22732 /* We must also deal with friend templates. Given:
22733
22734 template <class T> struct S {
22735 template <class U> friend void f() {};
22736 };
22737
22738 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22739 so far as the language is concerned, but that's still
22740 where we get the pattern for the instantiation from. On
22741 other hand, if the definition comes outside the class, say:
22742
22743 template <class T> struct S {
22744 template <class U> friend void f();
22745 };
22746 template <class U> friend void f() {}
22747
22748 we don't need to look any further. That's what the check for
22749 DECL_INITIAL is for. */
22750 || (TREE_CODE (decl) == FUNCTION_DECL
22751 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22752 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22753 {
22754 /* The present template, TD, should not be a definition. If it
22755 were a definition, we should be using it! Note that we
22756 cannot restructure the loop to just keep going until we find
22757 a template with a definition, since that might go too far if
22758 a specialization was declared, but not defined. */
22759
22760 /* Fetch the more general template. */
22761 tmpl = DECL_TI_TEMPLATE (tmpl);
22762 }
22763
22764 return tmpl;
22765 }
22766
22767 /* Returns true if we need to instantiate this template instance even if we
22768 know we aren't going to emit it. */
22769
22770 bool
22771 always_instantiate_p (tree decl)
22772 {
22773 /* We always instantiate inline functions so that we can inline them. An
22774 explicit instantiation declaration prohibits implicit instantiation of
22775 non-inline functions. With high levels of optimization, we would
22776 normally inline non-inline functions -- but we're not allowed to do
22777 that for "extern template" functions. Therefore, we check
22778 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22779 return ((TREE_CODE (decl) == FUNCTION_DECL
22780 && (DECL_DECLARED_INLINE_P (decl)
22781 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22782 /* And we need to instantiate static data members so that
22783 their initializers are available in integral constant
22784 expressions. */
22785 || (VAR_P (decl)
22786 && decl_maybe_constant_var_p (decl)));
22787 }
22788
22789 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22790 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22791 error, true otherwise. */
22792
22793 bool
22794 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22795 {
22796 tree fntype, spec, noex, clone;
22797
22798 /* Don't instantiate a noexcept-specification from template context. */
22799 if (processing_template_decl)
22800 return true;
22801
22802 if (DECL_CLONED_FUNCTION_P (fn))
22803 fn = DECL_CLONED_FUNCTION (fn);
22804 fntype = TREE_TYPE (fn);
22805 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22806
22807 if (!spec || !TREE_PURPOSE (spec))
22808 return true;
22809
22810 noex = TREE_PURPOSE (spec);
22811
22812 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22813 {
22814 static hash_set<tree>* fns = new hash_set<tree>;
22815 bool added = false;
22816 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22817 spec = get_defaulted_eh_spec (fn, complain);
22818 else if (!(added = !fns->add (fn)))
22819 {
22820 /* If hash_set::add returns true, the element was already there. */
22821 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22822 DECL_SOURCE_LOCATION (fn));
22823 error_at (loc,
22824 "exception specification of %qD depends on itself",
22825 fn);
22826 spec = noexcept_false_spec;
22827 }
22828 else if (push_tinst_level (fn))
22829 {
22830 push_access_scope (fn);
22831 push_deferring_access_checks (dk_no_deferred);
22832 input_location = DECL_SOURCE_LOCATION (fn);
22833 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22834 DEFERRED_NOEXCEPT_ARGS (noex),
22835 tf_warning_or_error, fn,
22836 /*function_p=*/false,
22837 /*integral_constant_expression_p=*/true);
22838 pop_deferring_access_checks ();
22839 pop_access_scope (fn);
22840 pop_tinst_level ();
22841 spec = build_noexcept_spec (noex, tf_warning_or_error);
22842 if (spec == error_mark_node)
22843 spec = noexcept_false_spec;
22844 }
22845 else
22846 spec = noexcept_false_spec;
22847
22848 if (added)
22849 fns->remove (fn);
22850
22851 if (spec == error_mark_node)
22852 return false;
22853
22854 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22855 }
22856
22857 FOR_EACH_CLONE (clone, fn)
22858 {
22859 if (TREE_TYPE (clone) == fntype)
22860 TREE_TYPE (clone) = TREE_TYPE (fn);
22861 else
22862 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22863 }
22864
22865 return true;
22866 }
22867
22868 /* We're starting to process the function INST, an instantiation of PATTERN;
22869 add their parameters to local_specializations. */
22870
22871 static void
22872 register_parameter_specializations (tree pattern, tree inst)
22873 {
22874 tree tmpl_parm = DECL_ARGUMENTS (pattern);
22875 tree spec_parm = DECL_ARGUMENTS (inst);
22876 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
22877 {
22878 register_local_specialization (spec_parm, tmpl_parm);
22879 spec_parm = skip_artificial_parms_for (inst, spec_parm);
22880 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
22881 }
22882 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22883 {
22884 if (!DECL_PACK_P (tmpl_parm))
22885 {
22886 register_local_specialization (spec_parm, tmpl_parm);
22887 spec_parm = DECL_CHAIN (spec_parm);
22888 }
22889 else
22890 {
22891 /* Register the (value) argument pack as a specialization of
22892 TMPL_PARM, then move on. */
22893 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22894 register_local_specialization (argpack, tmpl_parm);
22895 }
22896 }
22897 gcc_assert (!spec_parm);
22898 }
22899
22900 /* Produce the definition of D, a _DECL generated from a template. If
22901 DEFER_OK is true, then we don't have to actually do the
22902 instantiation now; we just have to do it sometime. Normally it is
22903 an error if this is an explicit instantiation but D is undefined.
22904 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22905 instantiated class template. */
22906
22907 tree
22908 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22909 {
22910 tree tmpl = DECL_TI_TEMPLATE (d);
22911 tree gen_args;
22912 tree args;
22913 tree td;
22914 tree code_pattern;
22915 tree spec;
22916 tree gen_tmpl;
22917 bool pattern_defined;
22918 location_t saved_loc = input_location;
22919 int saved_unevaluated_operand = cp_unevaluated_operand;
22920 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22921 bool external_p;
22922 bool deleted_p;
22923
22924 /* This function should only be used to instantiate templates for
22925 functions and static member variables. */
22926 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22927
22928 /* A concept is never instantiated. */
22929 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22930
22931 /* Variables are never deferred; if instantiation is required, they
22932 are instantiated right away. That allows for better code in the
22933 case that an expression refers to the value of the variable --
22934 if the variable has a constant value the referring expression can
22935 take advantage of that fact. */
22936 if (VAR_P (d))
22937 defer_ok = false;
22938
22939 /* Don't instantiate cloned functions. Instead, instantiate the
22940 functions they cloned. */
22941 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22942 d = DECL_CLONED_FUNCTION (d);
22943
22944 if (DECL_TEMPLATE_INSTANTIATED (d)
22945 || (TREE_CODE (d) == FUNCTION_DECL
22946 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22947 || DECL_TEMPLATE_SPECIALIZATION (d))
22948 /* D has already been instantiated or explicitly specialized, so
22949 there's nothing for us to do here.
22950
22951 It might seem reasonable to check whether or not D is an explicit
22952 instantiation, and, if so, stop here. But when an explicit
22953 instantiation is deferred until the end of the compilation,
22954 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22955 the instantiation. */
22956 return d;
22957
22958 /* Check to see whether we know that this template will be
22959 instantiated in some other file, as with "extern template"
22960 extension. */
22961 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22962
22963 /* In general, we do not instantiate such templates. */
22964 if (external_p && !always_instantiate_p (d))
22965 return d;
22966
22967 gen_tmpl = most_general_template (tmpl);
22968 gen_args = DECL_TI_ARGS (d);
22969
22970 if (tmpl != gen_tmpl)
22971 /* We should already have the extra args. */
22972 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22973 == TMPL_ARGS_DEPTH (gen_args));
22974 /* And what's in the hash table should match D. */
22975 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22976 || spec == NULL_TREE);
22977
22978 /* This needs to happen before any tsubsting. */
22979 if (! push_tinst_level (d))
22980 return d;
22981
22982 timevar_push (TV_TEMPLATE_INST);
22983
22984 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22985 for the instantiation. */
22986 td = template_for_substitution (d);
22987 args = gen_args;
22988
22989 if (VAR_P (d))
22990 {
22991 /* Look up an explicit specialization, if any. */
22992 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22993 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22994 if (elt && elt != error_mark_node)
22995 {
22996 td = TREE_VALUE (elt);
22997 args = TREE_PURPOSE (elt);
22998 }
22999 }
23000
23001 code_pattern = DECL_TEMPLATE_RESULT (td);
23002
23003 /* We should never be trying to instantiate a member of a class
23004 template or partial specialization. */
23005 gcc_assert (d != code_pattern);
23006
23007 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23008 || DECL_TEMPLATE_SPECIALIZATION (td))
23009 /* In the case of a friend template whose definition is provided
23010 outside the class, we may have too many arguments. Drop the
23011 ones we don't need. The same is true for specializations. */
23012 args = get_innermost_template_args
23013 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23014
23015 if (TREE_CODE (d) == FUNCTION_DECL)
23016 {
23017 deleted_p = DECL_DELETED_FN (code_pattern);
23018 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23019 && DECL_INITIAL (code_pattern) != error_mark_node)
23020 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23021 || deleted_p);
23022 }
23023 else
23024 {
23025 deleted_p = false;
23026 if (DECL_CLASS_SCOPE_P (code_pattern))
23027 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23028 || DECL_INLINE_VAR_P (code_pattern));
23029 else
23030 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23031 }
23032
23033 /* We may be in the middle of deferred access check. Disable it now. */
23034 push_deferring_access_checks (dk_no_deferred);
23035
23036 /* Unless an explicit instantiation directive has already determined
23037 the linkage of D, remember that a definition is available for
23038 this entity. */
23039 if (pattern_defined
23040 && !DECL_INTERFACE_KNOWN (d)
23041 && !DECL_NOT_REALLY_EXTERN (d))
23042 mark_definable (d);
23043
23044 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23045 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23046 input_location = DECL_SOURCE_LOCATION (d);
23047
23048 /* If D is a member of an explicitly instantiated class template,
23049 and no definition is available, treat it like an implicit
23050 instantiation. */
23051 if (!pattern_defined && expl_inst_class_mem_p
23052 && DECL_EXPLICIT_INSTANTIATION (d))
23053 {
23054 /* Leave linkage flags alone on instantiations with anonymous
23055 visibility. */
23056 if (TREE_PUBLIC (d))
23057 {
23058 DECL_NOT_REALLY_EXTERN (d) = 0;
23059 DECL_INTERFACE_KNOWN (d) = 0;
23060 }
23061 SET_DECL_IMPLICIT_INSTANTIATION (d);
23062 }
23063
23064 /* Defer all other templates, unless we have been explicitly
23065 forbidden from doing so. */
23066 if (/* If there is no definition, we cannot instantiate the
23067 template. */
23068 ! pattern_defined
23069 /* If it's OK to postpone instantiation, do so. */
23070 || defer_ok
23071 /* If this is a static data member that will be defined
23072 elsewhere, we don't want to instantiate the entire data
23073 member, but we do want to instantiate the initializer so that
23074 we can substitute that elsewhere. */
23075 || (external_p && VAR_P (d))
23076 /* Handle here a deleted function too, avoid generating
23077 its body (c++/61080). */
23078 || deleted_p)
23079 {
23080 /* The definition of the static data member is now required so
23081 we must substitute the initializer. */
23082 if (VAR_P (d)
23083 && !DECL_INITIAL (d)
23084 && DECL_INITIAL (code_pattern))
23085 {
23086 tree ns;
23087 tree init;
23088 bool const_init = false;
23089 bool enter_context = DECL_CLASS_SCOPE_P (d);
23090
23091 ns = decl_namespace_context (d);
23092 push_nested_namespace (ns);
23093 if (enter_context)
23094 push_nested_class (DECL_CONTEXT (d));
23095 init = tsubst_expr (DECL_INITIAL (code_pattern),
23096 args,
23097 tf_warning_or_error, NULL_TREE,
23098 /*integral_constant_expression_p=*/false);
23099 /* If instantiating the initializer involved instantiating this
23100 again, don't call cp_finish_decl twice. */
23101 if (!DECL_INITIAL (d))
23102 {
23103 /* Make sure the initializer is still constant, in case of
23104 circular dependency (template/instantiate6.C). */
23105 const_init
23106 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23107 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23108 /*asmspec_tree=*/NULL_TREE,
23109 LOOKUP_ONLYCONVERTING);
23110 }
23111 if (enter_context)
23112 pop_nested_class ();
23113 pop_nested_namespace (ns);
23114 }
23115
23116 /* We restore the source position here because it's used by
23117 add_pending_template. */
23118 input_location = saved_loc;
23119
23120 if (at_eof && !pattern_defined
23121 && DECL_EXPLICIT_INSTANTIATION (d)
23122 && DECL_NOT_REALLY_EXTERN (d))
23123 /* [temp.explicit]
23124
23125 The definition of a non-exported function template, a
23126 non-exported member function template, or a non-exported
23127 member function or static data member of a class template
23128 shall be present in every translation unit in which it is
23129 explicitly instantiated. */
23130 permerror (input_location, "explicit instantiation of %qD "
23131 "but no definition available", d);
23132
23133 /* If we're in unevaluated context, we just wanted to get the
23134 constant value; this isn't an odr use, so don't queue
23135 a full instantiation. */
23136 if (cp_unevaluated_operand != 0)
23137 goto out;
23138 /* ??? Historically, we have instantiated inline functions, even
23139 when marked as "extern template". */
23140 if (!(external_p && VAR_P (d)))
23141 add_pending_template (d);
23142 goto out;
23143 }
23144 /* Tell the repository that D is available in this translation unit
23145 -- and see if it is supposed to be instantiated here. */
23146 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23147 {
23148 /* In a PCH file, despite the fact that the repository hasn't
23149 requested instantiation in the PCH it is still possible that
23150 an instantiation will be required in a file that includes the
23151 PCH. */
23152 if (pch_file)
23153 add_pending_template (d);
23154 /* Instantiate inline functions so that the inliner can do its
23155 job, even though we'll not be emitting a copy of this
23156 function. */
23157 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23158 goto out;
23159 }
23160
23161 bool push_to_top, nested;
23162 tree fn_context;
23163 fn_context = decl_function_context (d);
23164 nested = current_function_decl != NULL_TREE;
23165 push_to_top = !(nested && fn_context == current_function_decl);
23166
23167 vec<tree> omp_privatization_save;
23168 if (nested)
23169 save_omp_privatization_clauses (omp_privatization_save);
23170
23171 if (push_to_top)
23172 push_to_top_level ();
23173 else
23174 {
23175 push_function_context ();
23176 cp_unevaluated_operand = 0;
23177 c_inhibit_evaluation_warnings = 0;
23178 }
23179
23180 /* Mark D as instantiated so that recursive calls to
23181 instantiate_decl do not try to instantiate it again. */
23182 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23183
23184 /* Regenerate the declaration in case the template has been modified
23185 by a subsequent redeclaration. */
23186 regenerate_decl_from_template (d, td, args);
23187
23188 /* We already set the file and line above. Reset them now in case
23189 they changed as a result of calling regenerate_decl_from_template. */
23190 input_location = DECL_SOURCE_LOCATION (d);
23191
23192 if (VAR_P (d))
23193 {
23194 tree init;
23195 bool const_init = false;
23196
23197 /* Clear out DECL_RTL; whatever was there before may not be right
23198 since we've reset the type of the declaration. */
23199 SET_DECL_RTL (d, NULL);
23200 DECL_IN_AGGR_P (d) = 0;
23201
23202 /* The initializer is placed in DECL_INITIAL by
23203 regenerate_decl_from_template so we don't need to
23204 push/pop_access_scope again here. Pull it out so that
23205 cp_finish_decl can process it. */
23206 init = DECL_INITIAL (d);
23207 DECL_INITIAL (d) = NULL_TREE;
23208 DECL_INITIALIZED_P (d) = 0;
23209
23210 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23211 initializer. That function will defer actual emission until
23212 we have a chance to determine linkage. */
23213 DECL_EXTERNAL (d) = 0;
23214
23215 /* Enter the scope of D so that access-checking works correctly. */
23216 bool enter_context = DECL_CLASS_SCOPE_P (d);
23217 if (enter_context)
23218 push_nested_class (DECL_CONTEXT (d));
23219
23220 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23221 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23222
23223 if (enter_context)
23224 pop_nested_class ();
23225
23226 if (variable_template_p (gen_tmpl))
23227 note_variable_template_instantiation (d);
23228 }
23229 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23230 synthesize_method (d);
23231 else if (TREE_CODE (d) == FUNCTION_DECL)
23232 {
23233 /* Set up the list of local specializations. */
23234 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23235 tree block = NULL_TREE;
23236
23237 /* Set up context. */
23238 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23239 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23240 block = push_stmt_list ();
23241 else
23242 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23243
23244 /* Some typedefs referenced from within the template code need to be
23245 access checked at template instantiation time, i.e now. These
23246 types were added to the template at parsing time. Let's get those
23247 and perform the access checks then. */
23248 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23249 args);
23250
23251 /* Create substitution entries for the parameters. */
23252 register_parameter_specializations (code_pattern, d);
23253
23254 /* Substitute into the body of the function. */
23255 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23256 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23257 tf_warning_or_error, tmpl);
23258 else
23259 {
23260 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23261 tf_warning_or_error, tmpl,
23262 /*integral_constant_expression_p=*/false);
23263
23264 /* Set the current input_location to the end of the function
23265 so that finish_function knows where we are. */
23266 input_location
23267 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23268
23269 /* Remember if we saw an infinite loop in the template. */
23270 current_function_infinite_loop
23271 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23272 }
23273
23274 /* Finish the function. */
23275 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23276 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23277 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23278 else
23279 {
23280 d = finish_function (/*inline_p=*/false);
23281 expand_or_defer_fn (d);
23282 }
23283
23284 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23285 cp_check_omp_declare_reduction (d);
23286 }
23287
23288 /* We're not deferring instantiation any more. */
23289 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23290
23291 if (push_to_top)
23292 pop_from_top_level ();
23293 else
23294 pop_function_context ();
23295
23296 if (nested)
23297 restore_omp_privatization_clauses (omp_privatization_save);
23298
23299 out:
23300 pop_deferring_access_checks ();
23301 timevar_pop (TV_TEMPLATE_INST);
23302 pop_tinst_level ();
23303 input_location = saved_loc;
23304 cp_unevaluated_operand = saved_unevaluated_operand;
23305 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23306
23307 return d;
23308 }
23309
23310 /* Run through the list of templates that we wish we could
23311 instantiate, and instantiate any we can. RETRIES is the
23312 number of times we retry pending template instantiation. */
23313
23314 void
23315 instantiate_pending_templates (int retries)
23316 {
23317 int reconsider;
23318 location_t saved_loc = input_location;
23319
23320 /* Instantiating templates may trigger vtable generation. This in turn
23321 may require further template instantiations. We place a limit here
23322 to avoid infinite loop. */
23323 if (pending_templates && retries >= max_tinst_depth)
23324 {
23325 tree decl = pending_templates->tinst->decl;
23326
23327 fatal_error (input_location,
23328 "template instantiation depth exceeds maximum of %d"
23329 " instantiating %q+D, possibly from virtual table generation"
23330 " (use -ftemplate-depth= to increase the maximum)",
23331 max_tinst_depth, decl);
23332 if (TREE_CODE (decl) == FUNCTION_DECL)
23333 /* Pretend that we defined it. */
23334 DECL_INITIAL (decl) = error_mark_node;
23335 return;
23336 }
23337
23338 do
23339 {
23340 struct pending_template **t = &pending_templates;
23341 struct pending_template *last = NULL;
23342 reconsider = 0;
23343 while (*t)
23344 {
23345 tree instantiation = reopen_tinst_level ((*t)->tinst);
23346 bool complete = false;
23347
23348 if (TYPE_P (instantiation))
23349 {
23350 if (!COMPLETE_TYPE_P (instantiation))
23351 {
23352 instantiate_class_template (instantiation);
23353 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23354 for (tree fld = TYPE_FIELDS (instantiation);
23355 fld; fld = TREE_CHAIN (fld))
23356 if ((VAR_P (fld)
23357 || (TREE_CODE (fld) == FUNCTION_DECL
23358 && !DECL_ARTIFICIAL (fld)))
23359 && DECL_TEMPLATE_INSTANTIATION (fld))
23360 instantiate_decl (fld,
23361 /*defer_ok=*/false,
23362 /*expl_inst_class_mem_p=*/false);
23363
23364 if (COMPLETE_TYPE_P (instantiation))
23365 reconsider = 1;
23366 }
23367
23368 complete = COMPLETE_TYPE_P (instantiation);
23369 }
23370 else
23371 {
23372 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23373 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23374 {
23375 instantiation
23376 = instantiate_decl (instantiation,
23377 /*defer_ok=*/false,
23378 /*expl_inst_class_mem_p=*/false);
23379 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23380 reconsider = 1;
23381 }
23382
23383 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23384 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23385 }
23386
23387 if (complete)
23388 /* If INSTANTIATION has been instantiated, then we don't
23389 need to consider it again in the future. */
23390 *t = (*t)->next;
23391 else
23392 {
23393 last = *t;
23394 t = &(*t)->next;
23395 }
23396 tinst_depth = 0;
23397 current_tinst_level = NULL;
23398 }
23399 last_pending_template = last;
23400 }
23401 while (reconsider);
23402
23403 input_location = saved_loc;
23404 }
23405
23406 /* Substitute ARGVEC into T, which is a list of initializers for
23407 either base class or a non-static data member. The TREE_PURPOSEs
23408 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23409 instantiate_decl. */
23410
23411 static tree
23412 tsubst_initializer_list (tree t, tree argvec)
23413 {
23414 tree inits = NULL_TREE;
23415
23416 for (; t; t = TREE_CHAIN (t))
23417 {
23418 tree decl;
23419 tree init;
23420 tree expanded_bases = NULL_TREE;
23421 tree expanded_arguments = NULL_TREE;
23422 int i, len = 1;
23423
23424 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23425 {
23426 tree expr;
23427 tree arg;
23428
23429 /* Expand the base class expansion type into separate base
23430 classes. */
23431 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23432 tf_warning_or_error,
23433 NULL_TREE);
23434 if (expanded_bases == error_mark_node)
23435 continue;
23436
23437 /* We'll be building separate TREE_LISTs of arguments for
23438 each base. */
23439 len = TREE_VEC_LENGTH (expanded_bases);
23440 expanded_arguments = make_tree_vec (len);
23441 for (i = 0; i < len; i++)
23442 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23443
23444 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23445 expand each argument in the TREE_VALUE of t. */
23446 expr = make_node (EXPR_PACK_EXPANSION);
23447 PACK_EXPANSION_LOCAL_P (expr) = true;
23448 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23449 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23450
23451 if (TREE_VALUE (t) == void_type_node)
23452 /* VOID_TYPE_NODE is used to indicate
23453 value-initialization. */
23454 {
23455 for (i = 0; i < len; i++)
23456 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23457 }
23458 else
23459 {
23460 /* Substitute parameter packs into each argument in the
23461 TREE_LIST. */
23462 in_base_initializer = 1;
23463 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23464 {
23465 tree expanded_exprs;
23466
23467 /* Expand the argument. */
23468 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23469 expanded_exprs
23470 = tsubst_pack_expansion (expr, argvec,
23471 tf_warning_or_error,
23472 NULL_TREE);
23473 if (expanded_exprs == error_mark_node)
23474 continue;
23475
23476 /* Prepend each of the expanded expressions to the
23477 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23478 for (i = 0; i < len; i++)
23479 {
23480 TREE_VEC_ELT (expanded_arguments, i) =
23481 tree_cons (NULL_TREE,
23482 TREE_VEC_ELT (expanded_exprs, i),
23483 TREE_VEC_ELT (expanded_arguments, i));
23484 }
23485 }
23486 in_base_initializer = 0;
23487
23488 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23489 since we built them backwards. */
23490 for (i = 0; i < len; i++)
23491 {
23492 TREE_VEC_ELT (expanded_arguments, i) =
23493 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23494 }
23495 }
23496 }
23497
23498 for (i = 0; i < len; ++i)
23499 {
23500 if (expanded_bases)
23501 {
23502 decl = TREE_VEC_ELT (expanded_bases, i);
23503 decl = expand_member_init (decl);
23504 init = TREE_VEC_ELT (expanded_arguments, i);
23505 }
23506 else
23507 {
23508 tree tmp;
23509 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23510 tf_warning_or_error, NULL_TREE);
23511
23512 decl = expand_member_init (decl);
23513 if (decl && !DECL_P (decl))
23514 in_base_initializer = 1;
23515
23516 init = TREE_VALUE (t);
23517 tmp = init;
23518 if (init != void_type_node)
23519 init = tsubst_expr (init, argvec,
23520 tf_warning_or_error, NULL_TREE,
23521 /*integral_constant_expression_p=*/false);
23522 if (init == NULL_TREE && tmp != NULL_TREE)
23523 /* If we had an initializer but it instantiated to nothing,
23524 value-initialize the object. This will only occur when
23525 the initializer was a pack expansion where the parameter
23526 packs used in that expansion were of length zero. */
23527 init = void_type_node;
23528 in_base_initializer = 0;
23529 }
23530
23531 if (decl)
23532 {
23533 init = build_tree_list (decl, init);
23534 TREE_CHAIN (init) = inits;
23535 inits = init;
23536 }
23537 }
23538 }
23539 return inits;
23540 }
23541
23542 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23543
23544 static void
23545 set_current_access_from_decl (tree decl)
23546 {
23547 if (TREE_PRIVATE (decl))
23548 current_access_specifier = access_private_node;
23549 else if (TREE_PROTECTED (decl))
23550 current_access_specifier = access_protected_node;
23551 else
23552 current_access_specifier = access_public_node;
23553 }
23554
23555 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23556 is the instantiation (which should have been created with
23557 start_enum) and ARGS are the template arguments to use. */
23558
23559 static void
23560 tsubst_enum (tree tag, tree newtag, tree args)
23561 {
23562 tree e;
23563
23564 if (SCOPED_ENUM_P (newtag))
23565 begin_scope (sk_scoped_enum, newtag);
23566
23567 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23568 {
23569 tree value;
23570 tree decl;
23571
23572 decl = TREE_VALUE (e);
23573 /* Note that in a template enum, the TREE_VALUE is the
23574 CONST_DECL, not the corresponding INTEGER_CST. */
23575 value = tsubst_expr (DECL_INITIAL (decl),
23576 args, tf_warning_or_error, NULL_TREE,
23577 /*integral_constant_expression_p=*/true);
23578
23579 /* Give this enumeration constant the correct access. */
23580 set_current_access_from_decl (decl);
23581
23582 /* Actually build the enumerator itself. Here we're assuming that
23583 enumerators can't have dependent attributes. */
23584 build_enumerator (DECL_NAME (decl), value, newtag,
23585 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23586 }
23587
23588 if (SCOPED_ENUM_P (newtag))
23589 finish_scope ();
23590
23591 finish_enum_value_list (newtag);
23592 finish_enum (newtag);
23593
23594 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23595 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23596 }
23597
23598 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23599 its type -- but without substituting the innermost set of template
23600 arguments. So, innermost set of template parameters will appear in
23601 the type. */
23602
23603 tree
23604 get_mostly_instantiated_function_type (tree decl)
23605 {
23606 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23607 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23608 }
23609
23610 /* Return truthvalue if we're processing a template different from
23611 the last one involved in diagnostics. */
23612 bool
23613 problematic_instantiation_changed (void)
23614 {
23615 return current_tinst_level != last_error_tinst_level;
23616 }
23617
23618 /* Remember current template involved in diagnostics. */
23619 void
23620 record_last_problematic_instantiation (void)
23621 {
23622 last_error_tinst_level = current_tinst_level;
23623 }
23624
23625 struct tinst_level *
23626 current_instantiation (void)
23627 {
23628 return current_tinst_level;
23629 }
23630
23631 /* Return TRUE if current_function_decl is being instantiated, false
23632 otherwise. */
23633
23634 bool
23635 instantiating_current_function_p (void)
23636 {
23637 return (current_instantiation ()
23638 && current_instantiation ()->decl == current_function_decl);
23639 }
23640
23641 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23642 type. Return false for ok, true for disallowed. Issue error and
23643 inform messages under control of COMPLAIN. */
23644
23645 static bool
23646 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23647 {
23648 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23649 return false;
23650 else if (POINTER_TYPE_P (type))
23651 return false;
23652 else if (TYPE_PTRMEM_P (type))
23653 return false;
23654 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23655 return false;
23656 else if (TREE_CODE (type) == TYPENAME_TYPE)
23657 return false;
23658 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23659 return false;
23660 else if (TREE_CODE (type) == NULLPTR_TYPE)
23661 return false;
23662 /* A bound template template parm could later be instantiated to have a valid
23663 nontype parm type via an alias template. */
23664 else if (cxx_dialect >= cxx11
23665 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23666 return false;
23667
23668 if (complain & tf_error)
23669 {
23670 if (type == error_mark_node)
23671 inform (input_location, "invalid template non-type parameter");
23672 else
23673 error ("%q#T is not a valid type for a template non-type parameter",
23674 type);
23675 }
23676 return true;
23677 }
23678
23679 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23680 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23681
23682 static bool
23683 dependent_type_p_r (tree type)
23684 {
23685 tree scope;
23686
23687 /* [temp.dep.type]
23688
23689 A type is dependent if it is:
23690
23691 -- a template parameter. Template template parameters are types
23692 for us (since TYPE_P holds true for them) so we handle
23693 them here. */
23694 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23695 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23696 return true;
23697 /* -- a qualified-id with a nested-name-specifier which contains a
23698 class-name that names a dependent type or whose unqualified-id
23699 names a dependent type. */
23700 if (TREE_CODE (type) == TYPENAME_TYPE)
23701 return true;
23702
23703 /* An alias template specialization can be dependent even if the
23704 resulting type is not. */
23705 if (dependent_alias_template_spec_p (type))
23706 return true;
23707
23708 /* -- a cv-qualified type where the cv-unqualified type is
23709 dependent.
23710 No code is necessary for this bullet; the code below handles
23711 cv-qualified types, and we don't want to strip aliases with
23712 TYPE_MAIN_VARIANT because of DR 1558. */
23713 /* -- a compound type constructed from any dependent type. */
23714 if (TYPE_PTRMEM_P (type))
23715 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23716 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23717 (type)));
23718 else if (TYPE_PTR_P (type)
23719 || TREE_CODE (type) == REFERENCE_TYPE)
23720 return dependent_type_p (TREE_TYPE (type));
23721 else if (TREE_CODE (type) == FUNCTION_TYPE
23722 || TREE_CODE (type) == METHOD_TYPE)
23723 {
23724 tree arg_type;
23725
23726 if (dependent_type_p (TREE_TYPE (type)))
23727 return true;
23728 for (arg_type = TYPE_ARG_TYPES (type);
23729 arg_type;
23730 arg_type = TREE_CHAIN (arg_type))
23731 if (dependent_type_p (TREE_VALUE (arg_type)))
23732 return true;
23733 if (cxx_dialect >= cxx17)
23734 /* A value-dependent noexcept-specifier makes the type dependent. */
23735 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
23736 if (tree noex = TREE_PURPOSE (spec))
23737 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
23738 affect overload resolution and treating it as dependent breaks
23739 things. */
23740 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
23741 && value_dependent_expression_p (noex))
23742 return true;
23743 return false;
23744 }
23745 /* -- an array type constructed from any dependent type or whose
23746 size is specified by a constant expression that is
23747 value-dependent.
23748
23749 We checked for type- and value-dependence of the bounds in
23750 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23751 if (TREE_CODE (type) == ARRAY_TYPE)
23752 {
23753 if (TYPE_DOMAIN (type)
23754 && dependent_type_p (TYPE_DOMAIN (type)))
23755 return true;
23756 return dependent_type_p (TREE_TYPE (type));
23757 }
23758
23759 /* -- a template-id in which either the template name is a template
23760 parameter ... */
23761 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23762 return true;
23763 /* ... or any of the template arguments is a dependent type or
23764 an expression that is type-dependent or value-dependent. */
23765 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23766 && (any_dependent_template_arguments_p
23767 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23768 return true;
23769
23770 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23771 dependent; if the argument of the `typeof' expression is not
23772 type-dependent, then it should already been have resolved. */
23773 if (TREE_CODE (type) == TYPEOF_TYPE
23774 || TREE_CODE (type) == DECLTYPE_TYPE
23775 || TREE_CODE (type) == UNDERLYING_TYPE)
23776 return true;
23777
23778 /* A template argument pack is dependent if any of its packed
23779 arguments are. */
23780 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23781 {
23782 tree args = ARGUMENT_PACK_ARGS (type);
23783 int i, len = TREE_VEC_LENGTH (args);
23784 for (i = 0; i < len; ++i)
23785 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23786 return true;
23787 }
23788
23789 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23790 be template parameters. */
23791 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23792 return true;
23793
23794 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23795 return true;
23796
23797 /* The standard does not specifically mention types that are local
23798 to template functions or local classes, but they should be
23799 considered dependent too. For example:
23800
23801 template <int I> void f() {
23802 enum E { a = I };
23803 S<sizeof (E)> s;
23804 }
23805
23806 The size of `E' cannot be known until the value of `I' has been
23807 determined. Therefore, `E' must be considered dependent. */
23808 scope = TYPE_CONTEXT (type);
23809 if (scope && TYPE_P (scope))
23810 return dependent_type_p (scope);
23811 /* Don't use type_dependent_expression_p here, as it can lead
23812 to infinite recursion trying to determine whether a lambda
23813 nested in a lambda is dependent (c++/47687). */
23814 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23815 && DECL_LANG_SPECIFIC (scope)
23816 && DECL_TEMPLATE_INFO (scope)
23817 && (any_dependent_template_arguments_p
23818 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23819 return true;
23820
23821 /* Other types are non-dependent. */
23822 return false;
23823 }
23824
23825 /* Returns TRUE if TYPE is dependent, in the sense of
23826 [temp.dep.type]. Note that a NULL type is considered dependent. */
23827
23828 bool
23829 dependent_type_p (tree type)
23830 {
23831 /* If there are no template parameters in scope, then there can't be
23832 any dependent types. */
23833 if (!processing_template_decl)
23834 {
23835 /* If we are not processing a template, then nobody should be
23836 providing us with a dependent type. */
23837 gcc_assert (type);
23838 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23839 return false;
23840 }
23841
23842 /* If the type is NULL, we have not computed a type for the entity
23843 in question; in that case, the type is dependent. */
23844 if (!type)
23845 return true;
23846
23847 /* Erroneous types can be considered non-dependent. */
23848 if (type == error_mark_node)
23849 return false;
23850
23851 /* Getting here with global_type_node means we improperly called this
23852 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23853 gcc_checking_assert (type != global_type_node);
23854
23855 /* If we have not already computed the appropriate value for TYPE,
23856 do so now. */
23857 if (!TYPE_DEPENDENT_P_VALID (type))
23858 {
23859 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23860 TYPE_DEPENDENT_P_VALID (type) = 1;
23861 }
23862
23863 return TYPE_DEPENDENT_P (type);
23864 }
23865
23866 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23867 lookup. In other words, a dependent type that is not the current
23868 instantiation. */
23869
23870 bool
23871 dependent_scope_p (tree scope)
23872 {
23873 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23874 && !currently_open_class (scope));
23875 }
23876
23877 /* T is a SCOPE_REF; return whether we need to consider it
23878 instantiation-dependent so that we can check access at instantiation
23879 time even though we know which member it resolves to. */
23880
23881 static bool
23882 instantiation_dependent_scope_ref_p (tree t)
23883 {
23884 if (DECL_P (TREE_OPERAND (t, 1))
23885 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23886 && accessible_in_template_p (TREE_OPERAND (t, 0),
23887 TREE_OPERAND (t, 1)))
23888 return false;
23889 else
23890 return true;
23891 }
23892
23893 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23894 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23895 expression. */
23896
23897 /* Note that this predicate is not appropriate for general expressions;
23898 only constant expressions (that satisfy potential_constant_expression)
23899 can be tested for value dependence. */
23900
23901 bool
23902 value_dependent_expression_p (tree expression)
23903 {
23904 if (!processing_template_decl || expression == NULL_TREE)
23905 return false;
23906
23907 /* A type-dependent expression is also value-dependent. */
23908 if (type_dependent_expression_p (expression))
23909 return true;
23910
23911 switch (TREE_CODE (expression))
23912 {
23913 case BASELINK:
23914 /* A dependent member function of the current instantiation. */
23915 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23916
23917 case FUNCTION_DECL:
23918 /* A dependent member function of the current instantiation. */
23919 if (DECL_CLASS_SCOPE_P (expression)
23920 && dependent_type_p (DECL_CONTEXT (expression)))
23921 return true;
23922 break;
23923
23924 case IDENTIFIER_NODE:
23925 /* A name that has not been looked up -- must be dependent. */
23926 return true;
23927
23928 case TEMPLATE_PARM_INDEX:
23929 /* A non-type template parm. */
23930 return true;
23931
23932 case CONST_DECL:
23933 /* A non-type template parm. */
23934 if (DECL_TEMPLATE_PARM_P (expression))
23935 return true;
23936 return value_dependent_expression_p (DECL_INITIAL (expression));
23937
23938 case VAR_DECL:
23939 /* A constant with literal type and is initialized
23940 with an expression that is value-dependent.
23941
23942 Note that a non-dependent parenthesized initializer will have
23943 already been replaced with its constant value, so if we see
23944 a TREE_LIST it must be dependent. */
23945 if (DECL_INITIAL (expression)
23946 && decl_constant_var_p (expression)
23947 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23948 /* cp_finish_decl doesn't fold reference initializers. */
23949 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23950 || value_dependent_expression_p (DECL_INITIAL (expression))))
23951 return true;
23952 if (DECL_HAS_VALUE_EXPR_P (expression))
23953 {
23954 tree value_expr = DECL_VALUE_EXPR (expression);
23955 if (value_dependent_expression_p (value_expr))
23956 return true;
23957 }
23958 return false;
23959
23960 case DYNAMIC_CAST_EXPR:
23961 case STATIC_CAST_EXPR:
23962 case CONST_CAST_EXPR:
23963 case REINTERPRET_CAST_EXPR:
23964 case CAST_EXPR:
23965 case IMPLICIT_CONV_EXPR:
23966 /* These expressions are value-dependent if the type to which
23967 the cast occurs is dependent or the expression being casted
23968 is value-dependent. */
23969 {
23970 tree type = TREE_TYPE (expression);
23971
23972 if (dependent_type_p (type))
23973 return true;
23974
23975 /* A functional cast has a list of operands. */
23976 expression = TREE_OPERAND (expression, 0);
23977 if (!expression)
23978 {
23979 /* If there are no operands, it must be an expression such
23980 as "int()". This should not happen for aggregate types
23981 because it would form non-constant expressions. */
23982 gcc_assert (cxx_dialect >= cxx11
23983 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23984
23985 return false;
23986 }
23987
23988 if (TREE_CODE (expression) == TREE_LIST)
23989 return any_value_dependent_elements_p (expression);
23990
23991 return value_dependent_expression_p (expression);
23992 }
23993
23994 case SIZEOF_EXPR:
23995 if (SIZEOF_EXPR_TYPE_P (expression))
23996 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23997 /* FALLTHRU */
23998 case ALIGNOF_EXPR:
23999 case TYPEID_EXPR:
24000 /* A `sizeof' expression is value-dependent if the operand is
24001 type-dependent or is a pack expansion. */
24002 expression = TREE_OPERAND (expression, 0);
24003 if (PACK_EXPANSION_P (expression))
24004 return true;
24005 else if (TYPE_P (expression))
24006 return dependent_type_p (expression);
24007 return instantiation_dependent_uneval_expression_p (expression);
24008
24009 case AT_ENCODE_EXPR:
24010 /* An 'encode' expression is value-dependent if the operand is
24011 type-dependent. */
24012 expression = TREE_OPERAND (expression, 0);
24013 return dependent_type_p (expression);
24014
24015 case NOEXCEPT_EXPR:
24016 expression = TREE_OPERAND (expression, 0);
24017 return instantiation_dependent_uneval_expression_p (expression);
24018
24019 case SCOPE_REF:
24020 /* All instantiation-dependent expressions should also be considered
24021 value-dependent. */
24022 return instantiation_dependent_scope_ref_p (expression);
24023
24024 case COMPONENT_REF:
24025 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24026 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24027
24028 case NONTYPE_ARGUMENT_PACK:
24029 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24030 is value-dependent. */
24031 {
24032 tree values = ARGUMENT_PACK_ARGS (expression);
24033 int i, len = TREE_VEC_LENGTH (values);
24034
24035 for (i = 0; i < len; ++i)
24036 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24037 return true;
24038
24039 return false;
24040 }
24041
24042 case TRAIT_EXPR:
24043 {
24044 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24045
24046 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24047 return true;
24048
24049 if (!type2)
24050 return false;
24051
24052 if (TREE_CODE (type2) != TREE_LIST)
24053 return dependent_type_p (type2);
24054
24055 for (; type2; type2 = TREE_CHAIN (type2))
24056 if (dependent_type_p (TREE_VALUE (type2)))
24057 return true;
24058
24059 return false;
24060 }
24061
24062 case MODOP_EXPR:
24063 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24064 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24065
24066 case ARRAY_REF:
24067 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24068 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24069
24070 case ADDR_EXPR:
24071 {
24072 tree op = TREE_OPERAND (expression, 0);
24073 return (value_dependent_expression_p (op)
24074 || has_value_dependent_address (op));
24075 }
24076
24077 case REQUIRES_EXPR:
24078 /* Treat all requires-expressions as value-dependent so
24079 we don't try to fold them. */
24080 return true;
24081
24082 case TYPE_REQ:
24083 return dependent_type_p (TREE_OPERAND (expression, 0));
24084
24085 case CALL_EXPR:
24086 {
24087 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24088 return true;
24089 tree fn = get_callee_fndecl (expression);
24090 int i, nargs;
24091 nargs = call_expr_nargs (expression);
24092 for (i = 0; i < nargs; ++i)
24093 {
24094 tree op = CALL_EXPR_ARG (expression, i);
24095 /* In a call to a constexpr member function, look through the
24096 implicit ADDR_EXPR on the object argument so that it doesn't
24097 cause the call to be considered value-dependent. We also
24098 look through it in potential_constant_expression. */
24099 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24100 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24101 && TREE_CODE (op) == ADDR_EXPR)
24102 op = TREE_OPERAND (op, 0);
24103 if (value_dependent_expression_p (op))
24104 return true;
24105 }
24106 return false;
24107 }
24108
24109 case TEMPLATE_ID_EXPR:
24110 return variable_concept_p (TREE_OPERAND (expression, 0));
24111
24112 case CONSTRUCTOR:
24113 {
24114 unsigned ix;
24115 tree val;
24116 if (dependent_type_p (TREE_TYPE (expression)))
24117 return true;
24118 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24119 if (value_dependent_expression_p (val))
24120 return true;
24121 return false;
24122 }
24123
24124 case STMT_EXPR:
24125 /* Treat a GNU statement expression as dependent to avoid crashing
24126 under instantiate_non_dependent_expr; it can't be constant. */
24127 return true;
24128
24129 default:
24130 /* A constant expression is value-dependent if any subexpression is
24131 value-dependent. */
24132 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24133 {
24134 case tcc_reference:
24135 case tcc_unary:
24136 case tcc_comparison:
24137 case tcc_binary:
24138 case tcc_expression:
24139 case tcc_vl_exp:
24140 {
24141 int i, len = cp_tree_operand_length (expression);
24142
24143 for (i = 0; i < len; i++)
24144 {
24145 tree t = TREE_OPERAND (expression, i);
24146
24147 /* In some cases, some of the operands may be missing.
24148 (For example, in the case of PREDECREMENT_EXPR, the
24149 amount to increment by may be missing.) That doesn't
24150 make the expression dependent. */
24151 if (t && value_dependent_expression_p (t))
24152 return true;
24153 }
24154 }
24155 break;
24156 default:
24157 break;
24158 }
24159 break;
24160 }
24161
24162 /* The expression is not value-dependent. */
24163 return false;
24164 }
24165
24166 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24167 [temp.dep.expr]. Note that an expression with no type is
24168 considered dependent. Other parts of the compiler arrange for an
24169 expression with type-dependent subexpressions to have no type, so
24170 this function doesn't have to be fully recursive. */
24171
24172 bool
24173 type_dependent_expression_p (tree expression)
24174 {
24175 if (!processing_template_decl)
24176 return false;
24177
24178 if (expression == NULL_TREE || expression == error_mark_node)
24179 return false;
24180
24181 /* An unresolved name is always dependent. */
24182 if (identifier_p (expression)
24183 || TREE_CODE (expression) == USING_DECL
24184 || TREE_CODE (expression) == WILDCARD_DECL)
24185 return true;
24186
24187 /* A fold expression is type-dependent. */
24188 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24189 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24190 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24191 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24192 return true;
24193
24194 /* Some expression forms are never type-dependent. */
24195 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24196 || TREE_CODE (expression) == SIZEOF_EXPR
24197 || TREE_CODE (expression) == ALIGNOF_EXPR
24198 || TREE_CODE (expression) == AT_ENCODE_EXPR
24199 || TREE_CODE (expression) == NOEXCEPT_EXPR
24200 || TREE_CODE (expression) == TRAIT_EXPR
24201 || TREE_CODE (expression) == TYPEID_EXPR
24202 || TREE_CODE (expression) == DELETE_EXPR
24203 || TREE_CODE (expression) == VEC_DELETE_EXPR
24204 || TREE_CODE (expression) == THROW_EXPR
24205 || TREE_CODE (expression) == REQUIRES_EXPR)
24206 return false;
24207
24208 /* The types of these expressions depends only on the type to which
24209 the cast occurs. */
24210 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24211 || TREE_CODE (expression) == STATIC_CAST_EXPR
24212 || TREE_CODE (expression) == CONST_CAST_EXPR
24213 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24214 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24215 || TREE_CODE (expression) == CAST_EXPR)
24216 return dependent_type_p (TREE_TYPE (expression));
24217
24218 /* The types of these expressions depends only on the type created
24219 by the expression. */
24220 if (TREE_CODE (expression) == NEW_EXPR
24221 || TREE_CODE (expression) == VEC_NEW_EXPR)
24222 {
24223 /* For NEW_EXPR tree nodes created inside a template, either
24224 the object type itself or a TREE_LIST may appear as the
24225 operand 1. */
24226 tree type = TREE_OPERAND (expression, 1);
24227 if (TREE_CODE (type) == TREE_LIST)
24228 /* This is an array type. We need to check array dimensions
24229 as well. */
24230 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24231 || value_dependent_expression_p
24232 (TREE_OPERAND (TREE_VALUE (type), 1));
24233 else
24234 return dependent_type_p (type);
24235 }
24236
24237 if (TREE_CODE (expression) == SCOPE_REF)
24238 {
24239 tree scope = TREE_OPERAND (expression, 0);
24240 tree name = TREE_OPERAND (expression, 1);
24241
24242 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24243 contains an identifier associated by name lookup with one or more
24244 declarations declared with a dependent type, or...a
24245 nested-name-specifier or qualified-id that names a member of an
24246 unknown specialization. */
24247 return (type_dependent_expression_p (name)
24248 || dependent_scope_p (scope));
24249 }
24250
24251 if (TREE_CODE (expression) == TEMPLATE_DECL
24252 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24253 return uses_outer_template_parms (expression);
24254
24255 if (TREE_CODE (expression) == STMT_EXPR)
24256 expression = stmt_expr_value_expr (expression);
24257
24258 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24259 {
24260 tree elt;
24261 unsigned i;
24262
24263 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24264 {
24265 if (type_dependent_expression_p (elt))
24266 return true;
24267 }
24268 return false;
24269 }
24270
24271 /* A static data member of the current instantiation with incomplete
24272 array type is type-dependent, as the definition and specializations
24273 can have different bounds. */
24274 if (VAR_P (expression)
24275 && DECL_CLASS_SCOPE_P (expression)
24276 && dependent_type_p (DECL_CONTEXT (expression))
24277 && VAR_HAD_UNKNOWN_BOUND (expression))
24278 return true;
24279
24280 /* An array of unknown bound depending on a variadic parameter, eg:
24281
24282 template<typename... Args>
24283 void foo (Args... args)
24284 {
24285 int arr[] = { args... };
24286 }
24287
24288 template<int... vals>
24289 void bar ()
24290 {
24291 int arr[] = { vals... };
24292 }
24293
24294 If the array has no length and has an initializer, it must be that
24295 we couldn't determine its length in cp_complete_array_type because
24296 it is dependent. */
24297 if (VAR_P (expression)
24298 && TREE_TYPE (expression) != NULL_TREE
24299 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24300 && !TYPE_DOMAIN (TREE_TYPE (expression))
24301 && DECL_INITIAL (expression))
24302 return true;
24303
24304 /* A function or variable template-id is type-dependent if it has any
24305 dependent template arguments. */
24306 if (VAR_OR_FUNCTION_DECL_P (expression)
24307 && DECL_LANG_SPECIFIC (expression)
24308 && DECL_TEMPLATE_INFO (expression))
24309 {
24310 /* Consider the innermost template arguments, since those are the ones
24311 that come from the template-id; the template arguments for the
24312 enclosing class do not make it type-dependent unless they are used in
24313 the type of the decl. */
24314 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24315 && (any_dependent_template_arguments_p
24316 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24317 return true;
24318 }
24319
24320 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24321 type-dependent. Checking this is important for functions with auto return
24322 type, which looks like a dependent type. */
24323 if (TREE_CODE (expression) == FUNCTION_DECL
24324 && !(DECL_CLASS_SCOPE_P (expression)
24325 && dependent_type_p (DECL_CONTEXT (expression)))
24326 && !(DECL_FRIEND_P (expression)
24327 && (!DECL_FRIEND_CONTEXT (expression)
24328 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24329 && !DECL_LOCAL_FUNCTION_P (expression))
24330 {
24331 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24332 || undeduced_auto_decl (expression));
24333 return false;
24334 }
24335
24336 /* Always dependent, on the number of arguments if nothing else. */
24337 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24338 return true;
24339
24340 if (TREE_TYPE (expression) == unknown_type_node)
24341 {
24342 if (TREE_CODE (expression) == ADDR_EXPR)
24343 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24344 if (TREE_CODE (expression) == COMPONENT_REF
24345 || TREE_CODE (expression) == OFFSET_REF)
24346 {
24347 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24348 return true;
24349 expression = TREE_OPERAND (expression, 1);
24350 if (identifier_p (expression))
24351 return false;
24352 }
24353 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24354 if (TREE_CODE (expression) == SCOPE_REF)
24355 return false;
24356
24357 if (BASELINK_P (expression))
24358 {
24359 if (BASELINK_OPTYPE (expression)
24360 && dependent_type_p (BASELINK_OPTYPE (expression)))
24361 return true;
24362 expression = BASELINK_FUNCTIONS (expression);
24363 }
24364
24365 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24366 {
24367 if (any_dependent_template_arguments_p
24368 (TREE_OPERAND (expression, 1)))
24369 return true;
24370 expression = TREE_OPERAND (expression, 0);
24371 if (identifier_p (expression))
24372 return true;
24373 }
24374
24375 gcc_assert (TREE_CODE (expression) == OVERLOAD
24376 || TREE_CODE (expression) == FUNCTION_DECL);
24377
24378 for (lkp_iterator iter (expression); iter; ++iter)
24379 if (type_dependent_expression_p (*iter))
24380 return true;
24381
24382 return false;
24383 }
24384
24385 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24386
24387 /* Dependent type attributes might not have made it from the decl to
24388 the type yet. */
24389 if (DECL_P (expression)
24390 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24391 return true;
24392
24393 return (dependent_type_p (TREE_TYPE (expression)));
24394 }
24395
24396 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24397 type-dependent if the expression refers to a member of the current
24398 instantiation and the type of the referenced member is dependent, or the
24399 class member access expression refers to a member of an unknown
24400 specialization.
24401
24402 This function returns true if the OBJECT in such a class member access
24403 expression is of an unknown specialization. */
24404
24405 bool
24406 type_dependent_object_expression_p (tree object)
24407 {
24408 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24409 dependent. */
24410 if (TREE_CODE (object) == IDENTIFIER_NODE)
24411 return true;
24412 tree scope = TREE_TYPE (object);
24413 return (!scope || dependent_scope_p (scope));
24414 }
24415
24416 /* walk_tree callback function for instantiation_dependent_expression_p,
24417 below. Returns non-zero if a dependent subexpression is found. */
24418
24419 static tree
24420 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24421 void * /*data*/)
24422 {
24423 if (TYPE_P (*tp))
24424 {
24425 /* We don't have to worry about decltype currently because decltype
24426 of an instantiation-dependent expr is a dependent type. This
24427 might change depending on the resolution of DR 1172. */
24428 *walk_subtrees = false;
24429 return NULL_TREE;
24430 }
24431 enum tree_code code = TREE_CODE (*tp);
24432 switch (code)
24433 {
24434 /* Don't treat an argument list as dependent just because it has no
24435 TREE_TYPE. */
24436 case TREE_LIST:
24437 case TREE_VEC:
24438 return NULL_TREE;
24439
24440 case TEMPLATE_PARM_INDEX:
24441 return *tp;
24442
24443 /* Handle expressions with type operands. */
24444 case SIZEOF_EXPR:
24445 case ALIGNOF_EXPR:
24446 case TYPEID_EXPR:
24447 case AT_ENCODE_EXPR:
24448 {
24449 tree op = TREE_OPERAND (*tp, 0);
24450 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24451 op = TREE_TYPE (op);
24452 if (TYPE_P (op))
24453 {
24454 if (dependent_type_p (op))
24455 return *tp;
24456 else
24457 {
24458 *walk_subtrees = false;
24459 return NULL_TREE;
24460 }
24461 }
24462 break;
24463 }
24464
24465 case COMPONENT_REF:
24466 if (identifier_p (TREE_OPERAND (*tp, 1)))
24467 /* In a template, finish_class_member_access_expr creates a
24468 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24469 type-dependent, so that we can check access control at
24470 instantiation time (PR 42277). See also Core issue 1273. */
24471 return *tp;
24472 break;
24473
24474 case SCOPE_REF:
24475 if (instantiation_dependent_scope_ref_p (*tp))
24476 return *tp;
24477 else
24478 break;
24479
24480 /* Treat statement-expressions as dependent. */
24481 case BIND_EXPR:
24482 return *tp;
24483
24484 /* Treat requires-expressions as dependent. */
24485 case REQUIRES_EXPR:
24486 return *tp;
24487
24488 case CALL_EXPR:
24489 /* Treat calls to function concepts as dependent. */
24490 if (function_concept_check_p (*tp))
24491 return *tp;
24492 break;
24493
24494 case TEMPLATE_ID_EXPR:
24495 /* And variable concepts. */
24496 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24497 return *tp;
24498 break;
24499
24500 default:
24501 break;
24502 }
24503
24504 if (type_dependent_expression_p (*tp))
24505 return *tp;
24506 else
24507 return NULL_TREE;
24508 }
24509
24510 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24511 sense defined by the ABI:
24512
24513 "An expression is instantiation-dependent if it is type-dependent
24514 or value-dependent, or it has a subexpression that is type-dependent
24515 or value-dependent."
24516
24517 Except don't actually check value-dependence for unevaluated expressions,
24518 because in sizeof(i) we don't care about the value of i. Checking
24519 type-dependence will in turn check value-dependence of array bounds/template
24520 arguments as needed. */
24521
24522 bool
24523 instantiation_dependent_uneval_expression_p (tree expression)
24524 {
24525 tree result;
24526
24527 if (!processing_template_decl)
24528 return false;
24529
24530 if (expression == error_mark_node)
24531 return false;
24532
24533 result = cp_walk_tree_without_duplicates (&expression,
24534 instantiation_dependent_r, NULL);
24535 return result != NULL_TREE;
24536 }
24537
24538 /* As above, but also check value-dependence of the expression as a whole. */
24539
24540 bool
24541 instantiation_dependent_expression_p (tree expression)
24542 {
24543 return (instantiation_dependent_uneval_expression_p (expression)
24544 || value_dependent_expression_p (expression));
24545 }
24546
24547 /* Like type_dependent_expression_p, but it also works while not processing
24548 a template definition, i.e. during substitution or mangling. */
24549
24550 bool
24551 type_dependent_expression_p_push (tree expr)
24552 {
24553 bool b;
24554 ++processing_template_decl;
24555 b = type_dependent_expression_p (expr);
24556 --processing_template_decl;
24557 return b;
24558 }
24559
24560 /* Returns TRUE if ARGS contains a type-dependent expression. */
24561
24562 bool
24563 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24564 {
24565 unsigned int i;
24566 tree arg;
24567
24568 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24569 {
24570 if (type_dependent_expression_p (arg))
24571 return true;
24572 }
24573 return false;
24574 }
24575
24576 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24577 expressions) contains any type-dependent expressions. */
24578
24579 bool
24580 any_type_dependent_elements_p (const_tree list)
24581 {
24582 for (; list; list = TREE_CHAIN (list))
24583 if (type_dependent_expression_p (TREE_VALUE (list)))
24584 return true;
24585
24586 return false;
24587 }
24588
24589 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24590 expressions) contains any value-dependent expressions. */
24591
24592 bool
24593 any_value_dependent_elements_p (const_tree list)
24594 {
24595 for (; list; list = TREE_CHAIN (list))
24596 if (value_dependent_expression_p (TREE_VALUE (list)))
24597 return true;
24598
24599 return false;
24600 }
24601
24602 /* Returns TRUE if the ARG (a template argument) is dependent. */
24603
24604 bool
24605 dependent_template_arg_p (tree arg)
24606 {
24607 if (!processing_template_decl)
24608 return false;
24609
24610 /* Assume a template argument that was wrongly written by the user
24611 is dependent. This is consistent with what
24612 any_dependent_template_arguments_p [that calls this function]
24613 does. */
24614 if (!arg || arg == error_mark_node)
24615 return true;
24616
24617 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24618 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24619
24620 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24621 return true;
24622 if (TREE_CODE (arg) == TEMPLATE_DECL)
24623 {
24624 if (DECL_TEMPLATE_PARM_P (arg))
24625 return true;
24626 /* A member template of a dependent class is not necessarily
24627 type-dependent, but it is a dependent template argument because it
24628 will be a member of an unknown specialization to that template. */
24629 tree scope = CP_DECL_CONTEXT (arg);
24630 return TYPE_P (scope) && dependent_type_p (scope);
24631 }
24632 else if (ARGUMENT_PACK_P (arg))
24633 {
24634 tree args = ARGUMENT_PACK_ARGS (arg);
24635 int i, len = TREE_VEC_LENGTH (args);
24636 for (i = 0; i < len; ++i)
24637 {
24638 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24639 return true;
24640 }
24641
24642 return false;
24643 }
24644 else if (TYPE_P (arg))
24645 return dependent_type_p (arg);
24646 else
24647 return (type_dependent_expression_p (arg)
24648 || value_dependent_expression_p (arg));
24649 }
24650
24651 /* Returns true if ARGS (a collection of template arguments) contains
24652 any types that require structural equality testing. */
24653
24654 bool
24655 any_template_arguments_need_structural_equality_p (tree args)
24656 {
24657 int i;
24658 int j;
24659
24660 if (!args)
24661 return false;
24662 if (args == error_mark_node)
24663 return true;
24664
24665 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24666 {
24667 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24668 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24669 {
24670 tree arg = TREE_VEC_ELT (level, j);
24671 tree packed_args = NULL_TREE;
24672 int k, len = 1;
24673
24674 if (ARGUMENT_PACK_P (arg))
24675 {
24676 /* Look inside the argument pack. */
24677 packed_args = ARGUMENT_PACK_ARGS (arg);
24678 len = TREE_VEC_LENGTH (packed_args);
24679 }
24680
24681 for (k = 0; k < len; ++k)
24682 {
24683 if (packed_args)
24684 arg = TREE_VEC_ELT (packed_args, k);
24685
24686 if (error_operand_p (arg))
24687 return true;
24688 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24689 continue;
24690 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24691 return true;
24692 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24693 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24694 return true;
24695 }
24696 }
24697 }
24698
24699 return false;
24700 }
24701
24702 /* Returns true if ARGS (a collection of template arguments) contains
24703 any dependent arguments. */
24704
24705 bool
24706 any_dependent_template_arguments_p (const_tree args)
24707 {
24708 int i;
24709 int j;
24710
24711 if (!args)
24712 return false;
24713 if (args == error_mark_node)
24714 return true;
24715
24716 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24717 {
24718 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24719 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24720 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24721 return true;
24722 }
24723
24724 return false;
24725 }
24726
24727 /* Returns TRUE if the template TMPL is type-dependent. */
24728
24729 bool
24730 dependent_template_p (tree tmpl)
24731 {
24732 if (TREE_CODE (tmpl) == OVERLOAD)
24733 {
24734 for (lkp_iterator iter (tmpl); iter; ++iter)
24735 if (dependent_template_p (*iter))
24736 return true;
24737 return false;
24738 }
24739
24740 /* Template template parameters are dependent. */
24741 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24742 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24743 return true;
24744 /* So are names that have not been looked up. */
24745 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24746 return true;
24747 return false;
24748 }
24749
24750 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24751
24752 bool
24753 dependent_template_id_p (tree tmpl, tree args)
24754 {
24755 return (dependent_template_p (tmpl)
24756 || any_dependent_template_arguments_p (args));
24757 }
24758
24759 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24760 are dependent. */
24761
24762 bool
24763 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24764 {
24765 int i;
24766
24767 if (!processing_template_decl)
24768 return false;
24769
24770 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24771 {
24772 tree decl = TREE_VEC_ELT (declv, i);
24773 tree init = TREE_VEC_ELT (initv, i);
24774 tree cond = TREE_VEC_ELT (condv, i);
24775 tree incr = TREE_VEC_ELT (incrv, i);
24776
24777 if (type_dependent_expression_p (decl)
24778 || TREE_CODE (decl) == SCOPE_REF)
24779 return true;
24780
24781 if (init && type_dependent_expression_p (init))
24782 return true;
24783
24784 if (type_dependent_expression_p (cond))
24785 return true;
24786
24787 if (COMPARISON_CLASS_P (cond)
24788 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24789 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24790 return true;
24791
24792 if (TREE_CODE (incr) == MODOP_EXPR)
24793 {
24794 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24795 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24796 return true;
24797 }
24798 else if (type_dependent_expression_p (incr))
24799 return true;
24800 else if (TREE_CODE (incr) == MODIFY_EXPR)
24801 {
24802 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24803 return true;
24804 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24805 {
24806 tree t = TREE_OPERAND (incr, 1);
24807 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24808 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24809 return true;
24810 }
24811 }
24812 }
24813
24814 return false;
24815 }
24816
24817 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24818 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24819 no such TYPE can be found. Note that this function peers inside
24820 uninstantiated templates and therefore should be used only in
24821 extremely limited situations. ONLY_CURRENT_P restricts this
24822 peering to the currently open classes hierarchy (which is required
24823 when comparing types). */
24824
24825 tree
24826 resolve_typename_type (tree type, bool only_current_p)
24827 {
24828 tree scope;
24829 tree name;
24830 tree decl;
24831 int quals;
24832 tree pushed_scope;
24833 tree result;
24834
24835 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24836
24837 scope = TYPE_CONTEXT (type);
24838 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24839 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24840 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24841 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24842 identifier of the TYPENAME_TYPE anymore.
24843 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24844 TYPENAME_TYPE instead, we avoid messing up with a possible
24845 typedef variant case. */
24846 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24847
24848 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24849 it first before we can figure out what NAME refers to. */
24850 if (TREE_CODE (scope) == TYPENAME_TYPE)
24851 {
24852 if (TYPENAME_IS_RESOLVING_P (scope))
24853 /* Given a class template A with a dependent base with nested type C,
24854 typedef typename A::C::C C will land us here, as trying to resolve
24855 the initial A::C leads to the local C typedef, which leads back to
24856 A::C::C. So we break the recursion now. */
24857 return type;
24858 else
24859 scope = resolve_typename_type (scope, only_current_p);
24860 }
24861 /* If we don't know what SCOPE refers to, then we cannot resolve the
24862 TYPENAME_TYPE. */
24863 if (!CLASS_TYPE_P (scope))
24864 return type;
24865 /* If this is a typedef, we don't want to look inside (c++/11987). */
24866 if (typedef_variant_p (type))
24867 return type;
24868 /* If SCOPE isn't the template itself, it will not have a valid
24869 TYPE_FIELDS list. */
24870 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24871 /* scope is either the template itself or a compatible instantiation
24872 like X<T>, so look up the name in the original template. */
24873 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24874 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24875 gcc_checking_assert (uses_template_parms (scope));
24876 /* If scope has no fields, it can't be a current instantiation. Check this
24877 before currently_open_class to avoid infinite recursion (71515). */
24878 if (!TYPE_FIELDS (scope))
24879 return type;
24880 /* If the SCOPE is not the current instantiation, there's no reason
24881 to look inside it. */
24882 if (only_current_p && !currently_open_class (scope))
24883 return type;
24884 /* Enter the SCOPE so that name lookup will be resolved as if we
24885 were in the class definition. In particular, SCOPE will no
24886 longer be considered a dependent type. */
24887 pushed_scope = push_scope (scope);
24888 /* Look up the declaration. */
24889 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24890 tf_warning_or_error);
24891
24892 result = NULL_TREE;
24893
24894 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24895 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24896 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24897 if (!decl)
24898 /*nop*/;
24899 else if (identifier_p (fullname)
24900 && TREE_CODE (decl) == TYPE_DECL)
24901 {
24902 result = TREE_TYPE (decl);
24903 if (result == error_mark_node)
24904 result = NULL_TREE;
24905 }
24906 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24907 && DECL_CLASS_TEMPLATE_P (decl))
24908 {
24909 /* Obtain the template and the arguments. */
24910 tree tmpl = TREE_OPERAND (fullname, 0);
24911 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24912 {
24913 /* We get here with a plain identifier because a previous tentative
24914 parse of the nested-name-specifier as part of a ptr-operator saw
24915 ::template X<A>. The use of ::template is necessary in a
24916 ptr-operator, but wrong in a declarator-id.
24917
24918 [temp.names]: In a qualified-id of a declarator-id, the keyword
24919 template shall not appear at the top level. */
24920 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24921 "keyword %<template%> not allowed in declarator-id");
24922 tmpl = decl;
24923 }
24924 tree args = TREE_OPERAND (fullname, 1);
24925 /* Instantiate the template. */
24926 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24927 /*entering_scope=*/true,
24928 tf_error | tf_user);
24929 if (result == error_mark_node)
24930 result = NULL_TREE;
24931 }
24932
24933 /* Leave the SCOPE. */
24934 if (pushed_scope)
24935 pop_scope (pushed_scope);
24936
24937 /* If we failed to resolve it, return the original typename. */
24938 if (!result)
24939 return type;
24940
24941 /* If lookup found a typename type, resolve that too. */
24942 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24943 {
24944 /* Ill-formed programs can cause infinite recursion here, so we
24945 must catch that. */
24946 TYPENAME_IS_RESOLVING_P (result) = 1;
24947 result = resolve_typename_type (result, only_current_p);
24948 TYPENAME_IS_RESOLVING_P (result) = 0;
24949 }
24950
24951 /* Qualify the resulting type. */
24952 quals = cp_type_quals (type);
24953 if (quals)
24954 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24955
24956 return result;
24957 }
24958
24959 /* EXPR is an expression which is not type-dependent. Return a proxy
24960 for EXPR that can be used to compute the types of larger
24961 expressions containing EXPR. */
24962
24963 tree
24964 build_non_dependent_expr (tree expr)
24965 {
24966 tree inner_expr;
24967
24968 /* When checking, try to get a constant value for all non-dependent
24969 expressions in order to expose bugs in *_dependent_expression_p
24970 and constexpr. This can affect code generation, see PR70704, so
24971 only do this for -fchecking=2. */
24972 if (flag_checking > 1
24973 && cxx_dialect >= cxx11
24974 /* Don't do this during nsdmi parsing as it can lead to
24975 unexpected recursive instantiations. */
24976 && !parsing_nsdmi ()
24977 /* Don't do this during concept expansion either and for
24978 the same reason. */
24979 && !expanding_concept ())
24980 fold_non_dependent_expr (expr);
24981
24982 /* Preserve OVERLOADs; the functions must be available to resolve
24983 types. */
24984 inner_expr = expr;
24985 if (TREE_CODE (inner_expr) == STMT_EXPR)
24986 inner_expr = stmt_expr_value_expr (inner_expr);
24987 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24988 inner_expr = TREE_OPERAND (inner_expr, 0);
24989 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24990 inner_expr = TREE_OPERAND (inner_expr, 1);
24991 if (is_overloaded_fn (inner_expr)
24992 || TREE_CODE (inner_expr) == OFFSET_REF)
24993 return expr;
24994 /* There is no need to return a proxy for a variable. */
24995 if (VAR_P (expr))
24996 return expr;
24997 /* Preserve string constants; conversions from string constants to
24998 "char *" are allowed, even though normally a "const char *"
24999 cannot be used to initialize a "char *". */
25000 if (TREE_CODE (expr) == STRING_CST)
25001 return expr;
25002 /* Preserve void and arithmetic constants, as an optimization -- there is no
25003 reason to create a new node. */
25004 if (TREE_CODE (expr) == VOID_CST
25005 || TREE_CODE (expr) == INTEGER_CST
25006 || TREE_CODE (expr) == REAL_CST)
25007 return expr;
25008 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25009 There is at least one place where we want to know that a
25010 particular expression is a throw-expression: when checking a ?:
25011 expression, there are special rules if the second or third
25012 argument is a throw-expression. */
25013 if (TREE_CODE (expr) == THROW_EXPR)
25014 return expr;
25015
25016 /* Don't wrap an initializer list, we need to be able to look inside. */
25017 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25018 return expr;
25019
25020 /* Don't wrap a dummy object, we need to be able to test for it. */
25021 if (is_dummy_object (expr))
25022 return expr;
25023
25024 if (TREE_CODE (expr) == COND_EXPR)
25025 return build3 (COND_EXPR,
25026 TREE_TYPE (expr),
25027 TREE_OPERAND (expr, 0),
25028 (TREE_OPERAND (expr, 1)
25029 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25030 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25031 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25032 if (TREE_CODE (expr) == COMPOUND_EXPR
25033 && !COMPOUND_EXPR_OVERLOADED (expr))
25034 return build2 (COMPOUND_EXPR,
25035 TREE_TYPE (expr),
25036 TREE_OPERAND (expr, 0),
25037 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25038
25039 /* If the type is unknown, it can't really be non-dependent */
25040 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25041
25042 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25043 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25044 }
25045
25046 /* ARGS is a vector of expressions as arguments to a function call.
25047 Replace the arguments with equivalent non-dependent expressions.
25048 This modifies ARGS in place. */
25049
25050 void
25051 make_args_non_dependent (vec<tree, va_gc> *args)
25052 {
25053 unsigned int ix;
25054 tree arg;
25055
25056 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25057 {
25058 tree newarg = build_non_dependent_expr (arg);
25059 if (newarg != arg)
25060 (*args)[ix] = newarg;
25061 }
25062 }
25063
25064 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25065 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25066 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25067
25068 static tree
25069 make_auto_1 (tree name, bool set_canonical)
25070 {
25071 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25072 TYPE_NAME (au) = build_decl (input_location,
25073 TYPE_DECL, name, au);
25074 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25075 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25076 (0, processing_template_decl + 1, processing_template_decl + 1,
25077 TYPE_NAME (au), NULL_TREE);
25078 if (set_canonical)
25079 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25080 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25081 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25082
25083 return au;
25084 }
25085
25086 tree
25087 make_decltype_auto (void)
25088 {
25089 return make_auto_1 (decltype_auto_identifier, true);
25090 }
25091
25092 tree
25093 make_auto (void)
25094 {
25095 return make_auto_1 (auto_identifier, true);
25096 }
25097
25098 /* Return a C++17 deduction placeholder for class template TMPL. */
25099
25100 tree
25101 make_template_placeholder (tree tmpl)
25102 {
25103 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25104 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25105 return t;
25106 }
25107
25108 /* True iff T is a C++17 class template deduction placeholder. */
25109
25110 bool
25111 template_placeholder_p (tree t)
25112 {
25113 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25114 }
25115
25116 /* Make a "constrained auto" type-specifier. This is an
25117 auto type with constraints that must be associated after
25118 deduction. The constraint is formed from the given
25119 CONC and its optional sequence of arguments, which are
25120 non-null if written as partial-concept-id. */
25121
25122 tree
25123 make_constrained_auto (tree con, tree args)
25124 {
25125 tree type = make_auto_1 (auto_identifier, false);
25126
25127 /* Build the constraint. */
25128 tree tmpl = DECL_TI_TEMPLATE (con);
25129 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25130 expr = build_concept_check (expr, type, args);
25131
25132 tree constr = normalize_expression (expr);
25133 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25134
25135 /* Our canonical type depends on the constraint. */
25136 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25137
25138 /* Attach the constraint to the type declaration. */
25139 tree decl = TYPE_NAME (type);
25140 return decl;
25141 }
25142
25143 /* Given type ARG, return std::initializer_list<ARG>. */
25144
25145 static tree
25146 listify (tree arg)
25147 {
25148 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25149
25150 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25151 {
25152 gcc_rich_location richloc (input_location);
25153 maybe_add_include_fixit (&richloc, "<initializer_list>");
25154 error_at (&richloc,
25155 "deducing from brace-enclosed initializer list"
25156 " requires %<#include <initializer_list>%>");
25157
25158 return error_mark_node;
25159 }
25160 tree argvec = make_tree_vec (1);
25161 TREE_VEC_ELT (argvec, 0) = arg;
25162
25163 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25164 NULL_TREE, 0, tf_warning_or_error);
25165 }
25166
25167 /* Replace auto in TYPE with std::initializer_list<auto>. */
25168
25169 static tree
25170 listify_autos (tree type, tree auto_node)
25171 {
25172 tree init_auto = listify (auto_node);
25173 tree argvec = make_tree_vec (1);
25174 TREE_VEC_ELT (argvec, 0) = init_auto;
25175 if (processing_template_decl)
25176 argvec = add_to_template_args (current_template_args (), argvec);
25177 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25178 }
25179
25180 /* Hash traits for hashing possibly constrained 'auto'
25181 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25182
25183 struct auto_hash : default_hash_traits<tree>
25184 {
25185 static inline hashval_t hash (tree);
25186 static inline bool equal (tree, tree);
25187 };
25188
25189 /* Hash the 'auto' T. */
25190
25191 inline hashval_t
25192 auto_hash::hash (tree t)
25193 {
25194 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25195 /* Matching constrained-type-specifiers denote the same template
25196 parameter, so hash the constraint. */
25197 return hash_placeholder_constraint (c);
25198 else
25199 /* But unconstrained autos are all separate, so just hash the pointer. */
25200 return iterative_hash_object (t, 0);
25201 }
25202
25203 /* Compare two 'auto's. */
25204
25205 inline bool
25206 auto_hash::equal (tree t1, tree t2)
25207 {
25208 if (t1 == t2)
25209 return true;
25210
25211 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25212 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25213
25214 /* Two unconstrained autos are distinct. */
25215 if (!c1 || !c2)
25216 return false;
25217
25218 return equivalent_placeholder_constraints (c1, c2);
25219 }
25220
25221 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25222 constrained) auto, add it to the vector. */
25223
25224 static int
25225 extract_autos_r (tree t, void *data)
25226 {
25227 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25228 if (is_auto (t))
25229 {
25230 /* All the autos were built with index 0; fix that up now. */
25231 tree *p = hash.find_slot (t, INSERT);
25232 unsigned idx;
25233 if (*p)
25234 /* If this is a repeated constrained-type-specifier, use the index we
25235 chose before. */
25236 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25237 else
25238 {
25239 /* Otherwise this is new, so use the current count. */
25240 *p = t;
25241 idx = hash.elements () - 1;
25242 }
25243 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25244 }
25245
25246 /* Always keep walking. */
25247 return 0;
25248 }
25249
25250 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25251 says they can appear anywhere in the type. */
25252
25253 static tree
25254 extract_autos (tree type)
25255 {
25256 hash_set<tree> visited;
25257 hash_table<auto_hash> hash (2);
25258
25259 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25260
25261 tree tree_vec = make_tree_vec (hash.elements());
25262 for (hash_table<auto_hash>::iterator iter = hash.begin();
25263 iter != hash.end(); ++iter)
25264 {
25265 tree elt = *iter;
25266 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25267 TREE_VEC_ELT (tree_vec, i)
25268 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25269 }
25270
25271 return tree_vec;
25272 }
25273
25274 /* The stem for deduction guide names. */
25275 const char *const dguide_base = "__dguide_";
25276
25277 /* Return the name for a deduction guide for class template TMPL. */
25278
25279 tree
25280 dguide_name (tree tmpl)
25281 {
25282 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25283 tree tname = TYPE_IDENTIFIER (type);
25284 char *buf = (char *) alloca (1 + strlen (dguide_base)
25285 + IDENTIFIER_LENGTH (tname));
25286 memcpy (buf, dguide_base, strlen (dguide_base));
25287 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25288 IDENTIFIER_LENGTH (tname) + 1);
25289 tree dname = get_identifier (buf);
25290 TREE_TYPE (dname) = type;
25291 return dname;
25292 }
25293
25294 /* True if NAME is the name of a deduction guide. */
25295
25296 bool
25297 dguide_name_p (tree name)
25298 {
25299 return (TREE_TYPE (name)
25300 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25301 strlen (dguide_base)));
25302 }
25303
25304 /* True if FN is a deduction guide. */
25305
25306 bool
25307 deduction_guide_p (const_tree fn)
25308 {
25309 if (DECL_P (fn))
25310 if (tree name = DECL_NAME (fn))
25311 return dguide_name_p (name);
25312 return false;
25313 }
25314
25315 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25316
25317 bool
25318 copy_guide_p (const_tree fn)
25319 {
25320 gcc_assert (deduction_guide_p (fn));
25321 if (!DECL_ARTIFICIAL (fn))
25322 return false;
25323 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25324 return (TREE_CHAIN (parms) == void_list_node
25325 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25326 }
25327
25328 /* True if FN is a guide generated from a constructor template. */
25329
25330 bool
25331 template_guide_p (const_tree fn)
25332 {
25333 gcc_assert (deduction_guide_p (fn));
25334 if (!DECL_ARTIFICIAL (fn))
25335 return false;
25336 tree tmpl = DECL_TI_TEMPLATE (fn);
25337 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25338 return PRIMARY_TEMPLATE_P (org);
25339 return false;
25340 }
25341
25342 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25343 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25344 template parameter types. Note that the handling of template template
25345 parameters relies on current_template_parms being set appropriately for the
25346 new template. */
25347
25348 static tree
25349 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25350 tree tsubst_args, tsubst_flags_t complain)
25351 {
25352 tree oldidx = get_template_parm_index (olddecl);
25353
25354 tree newtype;
25355 if (TREE_CODE (olddecl) == TYPE_DECL
25356 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25357 {
25358 tree oldtype = TREE_TYPE (olddecl);
25359 newtype = cxx_make_type (TREE_CODE (oldtype));
25360 TYPE_MAIN_VARIANT (newtype) = newtype;
25361 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25362 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25363 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25364 }
25365 else
25366 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25367 complain, NULL_TREE);
25368
25369 tree newdecl
25370 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25371 DECL_NAME (olddecl), newtype);
25372 SET_DECL_TEMPLATE_PARM_P (newdecl);
25373
25374 tree newidx;
25375 if (TREE_CODE (olddecl) == TYPE_DECL
25376 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25377 {
25378 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25379 = build_template_parm_index (index, level, level,
25380 newdecl, newtype);
25381 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25382 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25383 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25384 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25385
25386 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25387 {
25388 DECL_TEMPLATE_RESULT (newdecl)
25389 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25390 DECL_NAME (olddecl), newtype);
25391 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25392 // First create a copy (ttargs) of tsubst_args with an
25393 // additional level for the template template parameter's own
25394 // template parameters (ttparms).
25395 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25396 (DECL_TEMPLATE_PARMS (olddecl)));
25397 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25398 tree ttargs = make_tree_vec (depth + 1);
25399 for (int i = 0; i < depth; ++i)
25400 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25401 TREE_VEC_ELT (ttargs, depth)
25402 = template_parms_level_to_args (ttparms);
25403 // Substitute ttargs into ttparms to fix references to
25404 // other template parameters.
25405 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25406 complain);
25407 // Now substitute again with args based on tparms, to reduce
25408 // the level of the ttparms.
25409 ttargs = current_template_args ();
25410 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25411 complain);
25412 // Finally, tack the adjusted parms onto tparms.
25413 ttparms = tree_cons (size_int (depth), ttparms,
25414 current_template_parms);
25415 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25416 }
25417 }
25418 else
25419 {
25420 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25421 tree newconst
25422 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25423 TREE_CODE (oldconst),
25424 DECL_NAME (oldconst), newtype);
25425 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25426 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25427 SET_DECL_TEMPLATE_PARM_P (newconst);
25428 newidx = build_template_parm_index (index, level, level,
25429 newconst, newtype);
25430 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25431 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25432 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25433 }
25434
25435 return newdecl;
25436 }
25437
25438 /* Returns a C++17 class deduction guide template based on the constructor
25439 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25440 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25441
25442 static tree
25443 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25444 {
25445 tree type, tparms, targs, fparms, fargs, ci;
25446 bool memtmpl = false;
25447 bool explicit_p;
25448 location_t loc;
25449 tree fn_tmpl = NULL_TREE;
25450
25451 if (TYPE_P (ctor))
25452 {
25453 type = ctor;
25454 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25455 if (copy_p)
25456 {
25457 type = TREE_TYPE (type);
25458 fparms = tree_cons (NULL_TREE, type, void_list_node);
25459 }
25460 else
25461 fparms = void_list_node;
25462
25463 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25464 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25465 targs = CLASSTYPE_TI_ARGS (type);
25466 ci = NULL_TREE;
25467 fargs = NULL_TREE;
25468 loc = DECL_SOURCE_LOCATION (ctmpl);
25469 explicit_p = false;
25470 }
25471 else
25472 {
25473 ++processing_template_decl;
25474
25475 fn_tmpl
25476 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25477 : DECL_TI_TEMPLATE (ctor));
25478 if (outer_args)
25479 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25480 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25481
25482 type = DECL_CONTEXT (ctor);
25483
25484 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25485 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25486 fully specialized args for the enclosing class. Strip those off, as
25487 the deduction guide won't have those template parameters. */
25488 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25489 TMPL_PARMS_DEPTH (tparms));
25490 /* Discard the 'this' parameter. */
25491 fparms = FUNCTION_ARG_CHAIN (ctor);
25492 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25493 ci = get_constraints (ctor);
25494 loc = DECL_SOURCE_LOCATION (ctor);
25495 explicit_p = DECL_NONCONVERTING_P (ctor);
25496
25497 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25498 {
25499 memtmpl = true;
25500
25501 /* For a member template constructor, we need to flatten the two
25502 template parameter lists into one, and then adjust the function
25503 signature accordingly. This gets...complicated. */
25504 tree save_parms = current_template_parms;
25505
25506 /* For a member template we should have two levels of parms/args, one
25507 for the class and one for the constructor. We stripped
25508 specialized args for further enclosing classes above. */
25509 const int depth = 2;
25510 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25511
25512 /* Template args for translating references to the two-level template
25513 parameters into references to the one-level template parameters we
25514 are creating. */
25515 tree tsubst_args = copy_node (targs);
25516 TMPL_ARGS_LEVEL (tsubst_args, depth)
25517 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25518
25519 /* Template parms for the constructor template. */
25520 tree ftparms = TREE_VALUE (tparms);
25521 unsigned flen = TREE_VEC_LENGTH (ftparms);
25522 /* Template parms for the class template. */
25523 tparms = TREE_CHAIN (tparms);
25524 tree ctparms = TREE_VALUE (tparms);
25525 unsigned clen = TREE_VEC_LENGTH (ctparms);
25526 /* Template parms for the deduction guide start as a copy of the
25527 template parms for the class. We set current_template_parms for
25528 lookup_template_class_1. */
25529 current_template_parms = tparms = copy_node (tparms);
25530 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25531 for (unsigned i = 0; i < clen; ++i)
25532 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25533
25534 /* Now we need to rewrite the constructor parms to append them to the
25535 class parms. */
25536 for (unsigned i = 0; i < flen; ++i)
25537 {
25538 unsigned index = i + clen;
25539 unsigned level = 1;
25540 tree oldelt = TREE_VEC_ELT (ftparms, i);
25541 tree olddecl = TREE_VALUE (oldelt);
25542 tree newdecl = rewrite_template_parm (olddecl, index, level,
25543 tsubst_args, complain);
25544 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25545 tsubst_args, complain, ctor);
25546 tree list = build_tree_list (newdef, newdecl);
25547 TEMPLATE_PARM_CONSTRAINTS (list)
25548 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25549 tsubst_args, complain, ctor);
25550 TREE_VEC_ELT (new_vec, index) = list;
25551 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25552 }
25553
25554 /* Now we have a final set of template parms to substitute into the
25555 function signature. */
25556 targs = template_parms_to_args (tparms);
25557 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25558 complain, ctor);
25559 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25560 if (ci)
25561 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25562
25563 current_template_parms = save_parms;
25564 }
25565 --processing_template_decl;
25566 }
25567
25568 if (!memtmpl)
25569 {
25570 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25571 tparms = copy_node (tparms);
25572 INNERMOST_TEMPLATE_PARMS (tparms)
25573 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25574 }
25575
25576 tree fntype = build_function_type (type, fparms);
25577 tree ded_fn = build_lang_decl_loc (loc,
25578 FUNCTION_DECL,
25579 dguide_name (type), fntype);
25580 DECL_ARGUMENTS (ded_fn) = fargs;
25581 DECL_ARTIFICIAL (ded_fn) = true;
25582 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25583 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25584 DECL_ARTIFICIAL (ded_tmpl) = true;
25585 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25586 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25587 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25588 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25589 if (DECL_P (ctor))
25590 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25591 if (ci)
25592 set_constraints (ded_tmpl, ci);
25593
25594 return ded_tmpl;
25595 }
25596
25597 /* Deduce template arguments for the class template placeholder PTYPE for
25598 template TMPL based on the initializer INIT, and return the resulting
25599 type. */
25600
25601 static tree
25602 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25603 tsubst_flags_t complain)
25604 {
25605 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25606 {
25607 /* We should have handled this in the caller. */
25608 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25609 return ptype;
25610 if (complain & tf_error)
25611 error ("non-class template %qT used without template arguments", tmpl);
25612 return error_mark_node;
25613 }
25614
25615 tree type = TREE_TYPE (tmpl);
25616
25617 bool try_list_ctor = false;
25618
25619 vec<tree,va_gc> *args;
25620 if (init == NULL_TREE
25621 || TREE_CODE (init) == TREE_LIST)
25622 args = make_tree_vector_from_list (init);
25623 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25624 {
25625 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25626 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25627 {
25628 /* As an exception, the first phase in 16.3.1.7 (considering the
25629 initializer list as a single argument) is omitted if the
25630 initializer list consists of a single expression of type cv U,
25631 where U is a specialization of C or a class derived from a
25632 specialization of C. */
25633 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25634 tree etype = TREE_TYPE (elt);
25635
25636 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25637 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25638 int err = unify (tparms, targs, type, etype,
25639 UNIFY_ALLOW_DERIVED, /*explain*/false);
25640 if (err == 0)
25641 try_list_ctor = false;
25642 ggc_free (targs);
25643 }
25644 if (try_list_ctor || is_std_init_list (type))
25645 args = make_tree_vector_single (init);
25646 else
25647 args = make_tree_vector_from_ctor (init);
25648 }
25649 else
25650 args = make_tree_vector_single (init);
25651
25652 tree dname = dguide_name (tmpl);
25653 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25654 /*type*/false, /*complain*/false,
25655 /*hidden*/false);
25656 bool elided = false;
25657 if (cands == error_mark_node)
25658 cands = NULL_TREE;
25659
25660 /* Prune explicit deduction guides in copy-initialization context. */
25661 if (flags & LOOKUP_ONLYCONVERTING)
25662 {
25663 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25664 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25665 elided = true;
25666
25667 if (elided)
25668 {
25669 /* Found a nonconverting guide, prune the candidates. */
25670 tree pruned = NULL_TREE;
25671 for (lkp_iterator iter (cands); iter; ++iter)
25672 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25673 pruned = lookup_add (*iter, pruned);
25674
25675 cands = pruned;
25676 }
25677 }
25678
25679 tree outer_args = NULL_TREE;
25680 if (DECL_CLASS_SCOPE_P (tmpl)
25681 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25682 {
25683 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25684 type = TREE_TYPE (most_general_template (tmpl));
25685 }
25686
25687 bool saw_ctor = false;
25688 // FIXME cache artificial deduction guides
25689 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25690 {
25691 tree guide = build_deduction_guide (*iter, outer_args, complain);
25692 if ((flags & LOOKUP_ONLYCONVERTING)
25693 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25694 elided = true;
25695 else
25696 cands = lookup_add (guide, cands);
25697
25698 saw_ctor = true;
25699 }
25700
25701 tree call = error_mark_node;
25702
25703 /* If this is list-initialization and the class has a list constructor, first
25704 try deducing from the list as a single argument, as [over.match.list]. */
25705 tree list_cands = NULL_TREE;
25706 if (try_list_ctor && cands)
25707 for (lkp_iterator iter (cands); iter; ++iter)
25708 {
25709 tree dg = *iter;
25710 if (is_list_ctor (dg))
25711 list_cands = lookup_add (dg, list_cands);
25712 }
25713 if (list_cands)
25714 {
25715 ++cp_unevaluated_operand;
25716 call = build_new_function_call (list_cands, &args, tf_decltype);
25717 --cp_unevaluated_operand;
25718
25719 if (call == error_mark_node)
25720 {
25721 /* That didn't work, now try treating the list as a sequence of
25722 arguments. */
25723 release_tree_vector (args);
25724 args = make_tree_vector_from_ctor (init);
25725 }
25726 }
25727
25728 /* Maybe generate an implicit deduction guide. */
25729 if (call == error_mark_node && args->length () < 2)
25730 {
25731 tree gtype = NULL_TREE;
25732
25733 if (args->length () == 1)
25734 /* Generate a copy guide. */
25735 gtype = build_reference_type (type);
25736 else if (!saw_ctor)
25737 /* Generate a default guide. */
25738 gtype = type;
25739
25740 if (gtype)
25741 {
25742 tree guide = build_deduction_guide (gtype, outer_args, complain);
25743 cands = lookup_add (guide, cands);
25744 }
25745 }
25746
25747 if (elided && !cands)
25748 {
25749 error ("cannot deduce template arguments for copy-initialization"
25750 " of %qT, as it has no non-explicit deduction guides or "
25751 "user-declared constructors", type);
25752 return error_mark_node;
25753 }
25754 else if (!cands && call == error_mark_node)
25755 {
25756 error ("cannot deduce template arguments of %qT, as it has no viable "
25757 "deduction guides", type);
25758 return error_mark_node;
25759 }
25760
25761 if (call == error_mark_node)
25762 {
25763 ++cp_unevaluated_operand;
25764 call = build_new_function_call (cands, &args, tf_decltype);
25765 --cp_unevaluated_operand;
25766 }
25767
25768 if (call == error_mark_node && (complain & tf_warning_or_error))
25769 {
25770 error ("class template argument deduction failed:");
25771
25772 ++cp_unevaluated_operand;
25773 call = build_new_function_call (cands, &args, complain | tf_decltype);
25774 --cp_unevaluated_operand;
25775
25776 if (elided)
25777 inform (input_location, "explicit deduction guides not considered "
25778 "for copy-initialization");
25779 }
25780
25781 release_tree_vector (args);
25782
25783 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25784 }
25785
25786 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25787 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25788
25789 tree
25790 do_auto_deduction (tree type, tree init, tree auto_node)
25791 {
25792 return do_auto_deduction (type, init, auto_node,
25793 tf_warning_or_error,
25794 adc_unspecified);
25795 }
25796
25797 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25798 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25799 The CONTEXT determines the context in which auto deduction is performed
25800 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25801 OUTER_TARGS are used during template argument deduction
25802 (context == adc_unify) to properly substitute the result, and is ignored
25803 in other contexts.
25804
25805 For partial-concept-ids, extra args may be appended to the list of deduced
25806 template arguments prior to determining constraint satisfaction. */
25807
25808 tree
25809 do_auto_deduction (tree type, tree init, tree auto_node,
25810 tsubst_flags_t complain, auto_deduction_context context,
25811 tree outer_targs, int flags)
25812 {
25813 tree targs;
25814
25815 if (init == error_mark_node)
25816 return error_mark_node;
25817
25818 if (init && type_dependent_expression_p (init)
25819 && context != adc_unify)
25820 /* Defining a subset of type-dependent expressions that we can deduce
25821 from ahead of time isn't worth the trouble. */
25822 return type;
25823
25824 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25825 /* C++17 class template argument deduction. */
25826 return do_class_deduction (type, tmpl, init, flags, complain);
25827
25828 if (TREE_TYPE (init) == NULL_TREE)
25829 /* Nothing we can do with this, even in deduction context. */
25830 return type;
25831
25832 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25833 with either a new invented type template parameter U or, if the
25834 initializer is a braced-init-list (8.5.4), with
25835 std::initializer_list<U>. */
25836 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25837 {
25838 if (!DIRECT_LIST_INIT_P (init))
25839 type = listify_autos (type, auto_node);
25840 else if (CONSTRUCTOR_NELTS (init) == 1)
25841 init = CONSTRUCTOR_ELT (init, 0)->value;
25842 else
25843 {
25844 if (complain & tf_warning_or_error)
25845 {
25846 if (permerror (input_location, "direct-list-initialization of "
25847 "%<auto%> requires exactly one element"))
25848 inform (input_location,
25849 "for deduction to %<std::initializer_list%>, use copy-"
25850 "list-initialization (i.e. add %<=%> before the %<{%>)");
25851 }
25852 type = listify_autos (type, auto_node);
25853 }
25854 }
25855
25856 if (type == error_mark_node)
25857 return error_mark_node;
25858
25859 init = resolve_nondeduced_context (init, complain);
25860
25861 if (context == adc_decomp_type
25862 && auto_node == type
25863 && init != error_mark_node
25864 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25865 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25866 and initializer has array type, deduce cv-qualified array type. */
25867 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25868 complain);
25869 else if (AUTO_IS_DECLTYPE (auto_node))
25870 {
25871 bool id = (DECL_P (init)
25872 || ((TREE_CODE (init) == COMPONENT_REF
25873 || TREE_CODE (init) == SCOPE_REF)
25874 && !REF_PARENTHESIZED_P (init)));
25875 targs = make_tree_vec (1);
25876 TREE_VEC_ELT (targs, 0)
25877 = finish_decltype_type (init, id, tf_warning_or_error);
25878 if (type != auto_node)
25879 {
25880 if (complain & tf_error)
25881 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25882 return error_mark_node;
25883 }
25884 }
25885 else
25886 {
25887 tree parms = build_tree_list (NULL_TREE, type);
25888 tree tparms;
25889
25890 if (flag_concepts)
25891 tparms = extract_autos (type);
25892 else
25893 {
25894 tparms = make_tree_vec (1);
25895 TREE_VEC_ELT (tparms, 0)
25896 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25897 }
25898
25899 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25900 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25901 DEDUCE_CALL, LOOKUP_NORMAL,
25902 NULL, /*explain_p=*/false);
25903 if (val > 0)
25904 {
25905 if (processing_template_decl)
25906 /* Try again at instantiation time. */
25907 return type;
25908 if (type && type != error_mark_node
25909 && (complain & tf_error))
25910 /* If type is error_mark_node a diagnostic must have been
25911 emitted by now. Also, having a mention to '<type error>'
25912 in the diagnostic is not really useful to the user. */
25913 {
25914 if (cfun && auto_node == current_function_auto_return_pattern
25915 && LAMBDA_FUNCTION_P (current_function_decl))
25916 error ("unable to deduce lambda return type from %qE", init);
25917 else
25918 error ("unable to deduce %qT from %qE", type, init);
25919 type_unification_real (tparms, targs, parms, &init, 1, 0,
25920 DEDUCE_CALL, LOOKUP_NORMAL,
25921 NULL, /*explain_p=*/true);
25922 }
25923 return error_mark_node;
25924 }
25925 }
25926
25927 /* Check any placeholder constraints against the deduced type. */
25928 if (flag_concepts && !processing_template_decl)
25929 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25930 {
25931 /* Use the deduced type to check the associated constraints. If we
25932 have a partial-concept-id, rebuild the argument list so that
25933 we check using the extra arguments. */
25934 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25935 tree cargs = CHECK_CONSTR_ARGS (constr);
25936 if (TREE_VEC_LENGTH (cargs) > 1)
25937 {
25938 cargs = copy_node (cargs);
25939 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25940 }
25941 else
25942 cargs = targs;
25943 if (!constraints_satisfied_p (constr, cargs))
25944 {
25945 if (complain & tf_warning_or_error)
25946 {
25947 switch (context)
25948 {
25949 case adc_unspecified:
25950 case adc_unify:
25951 error("placeholder constraints not satisfied");
25952 break;
25953 case adc_variable_type:
25954 case adc_decomp_type:
25955 error ("deduced initializer does not satisfy "
25956 "placeholder constraints");
25957 break;
25958 case adc_return_type:
25959 error ("deduced return type does not satisfy "
25960 "placeholder constraints");
25961 break;
25962 case adc_requirement:
25963 error ("deduced expression type does not satisfy "
25964 "placeholder constraints");
25965 break;
25966 }
25967 diagnose_constraints (input_location, constr, targs);
25968 }
25969 return error_mark_node;
25970 }
25971 }
25972
25973 if (processing_template_decl && context != adc_unify)
25974 outer_targs = current_template_args ();
25975 targs = add_to_template_args (outer_targs, targs);
25976 return tsubst (type, targs, complain, NULL_TREE);
25977 }
25978
25979 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25980 result. */
25981
25982 tree
25983 splice_late_return_type (tree type, tree late_return_type)
25984 {
25985 if (is_auto (type))
25986 {
25987 if (late_return_type)
25988 return late_return_type;
25989
25990 tree idx = get_template_parm_index (type);
25991 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25992 /* In an abbreviated function template we didn't know we were dealing
25993 with a function template when we saw the auto return type, so update
25994 it to have the correct level. */
25995 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25996 }
25997 return type;
25998 }
25999
26000 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26001 'decltype(auto)' or a deduced class template. */
26002
26003 bool
26004 is_auto (const_tree type)
26005 {
26006 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26007 && (TYPE_IDENTIFIER (type) == auto_identifier
26008 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26009 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26010 return true;
26011 else
26012 return false;
26013 }
26014
26015 /* for_each_template_parm callback for type_uses_auto. */
26016
26017 int
26018 is_auto_r (tree tp, void */*data*/)
26019 {
26020 return is_auto (tp);
26021 }
26022
26023 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26024 a use of `auto'. Returns NULL_TREE otherwise. */
26025
26026 tree
26027 type_uses_auto (tree type)
26028 {
26029 if (type == NULL_TREE)
26030 return NULL_TREE;
26031 else if (flag_concepts)
26032 {
26033 /* The Concepts TS allows multiple autos in one type-specifier; just
26034 return the first one we find, do_auto_deduction will collect all of
26035 them. */
26036 if (uses_template_parms (type))
26037 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26038 /*visited*/NULL, /*nondeduced*/true);
26039 else
26040 return NULL_TREE;
26041 }
26042 else
26043 return find_type_usage (type, is_auto);
26044 }
26045
26046 /* For a given template T, return the vector of typedefs referenced
26047 in T for which access check is needed at T instantiation time.
26048 T is either a FUNCTION_DECL or a RECORD_TYPE.
26049 Those typedefs were added to T by the function
26050 append_type_to_template_for_access_check. */
26051
26052 vec<qualified_typedef_usage_t, va_gc> *
26053 get_types_needing_access_check (tree t)
26054 {
26055 tree ti;
26056 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26057
26058 if (!t || t == error_mark_node)
26059 return NULL;
26060
26061 if (!(ti = get_template_info (t)))
26062 return NULL;
26063
26064 if (CLASS_TYPE_P (t)
26065 || TREE_CODE (t) == FUNCTION_DECL)
26066 {
26067 if (!TI_TEMPLATE (ti))
26068 return NULL;
26069
26070 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26071 }
26072
26073 return result;
26074 }
26075
26076 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26077 tied to T. That list of typedefs will be access checked at
26078 T instantiation time.
26079 T is either a FUNCTION_DECL or a RECORD_TYPE.
26080 TYPE_DECL is a TYPE_DECL node representing a typedef.
26081 SCOPE is the scope through which TYPE_DECL is accessed.
26082 LOCATION is the location of the usage point of TYPE_DECL.
26083
26084 This function is a subroutine of
26085 append_type_to_template_for_access_check. */
26086
26087 static void
26088 append_type_to_template_for_access_check_1 (tree t,
26089 tree type_decl,
26090 tree scope,
26091 location_t location)
26092 {
26093 qualified_typedef_usage_t typedef_usage;
26094 tree ti;
26095
26096 if (!t || t == error_mark_node)
26097 return;
26098
26099 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26100 || CLASS_TYPE_P (t))
26101 && type_decl
26102 && TREE_CODE (type_decl) == TYPE_DECL
26103 && scope);
26104
26105 if (!(ti = get_template_info (t)))
26106 return;
26107
26108 gcc_assert (TI_TEMPLATE (ti));
26109
26110 typedef_usage.typedef_decl = type_decl;
26111 typedef_usage.context = scope;
26112 typedef_usage.locus = location;
26113
26114 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26115 }
26116
26117 /* Append TYPE_DECL to the template TEMPL.
26118 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26119 At TEMPL instanciation time, TYPE_DECL will be checked to see
26120 if it can be accessed through SCOPE.
26121 LOCATION is the location of the usage point of TYPE_DECL.
26122
26123 e.g. consider the following code snippet:
26124
26125 class C
26126 {
26127 typedef int myint;
26128 };
26129
26130 template<class U> struct S
26131 {
26132 C::myint mi; // <-- usage point of the typedef C::myint
26133 };
26134
26135 S<char> s;
26136
26137 At S<char> instantiation time, we need to check the access of C::myint
26138 In other words, we need to check the access of the myint typedef through
26139 the C scope. For that purpose, this function will add the myint typedef
26140 and the scope C through which its being accessed to a list of typedefs
26141 tied to the template S. That list will be walked at template instantiation
26142 time and access check performed on each typedefs it contains.
26143 Note that this particular code snippet should yield an error because
26144 myint is private to C. */
26145
26146 void
26147 append_type_to_template_for_access_check (tree templ,
26148 tree type_decl,
26149 tree scope,
26150 location_t location)
26151 {
26152 qualified_typedef_usage_t *iter;
26153 unsigned i;
26154
26155 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26156
26157 /* Make sure we don't append the type to the template twice. */
26158 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26159 if (iter->typedef_decl == type_decl && scope == iter->context)
26160 return;
26161
26162 append_type_to_template_for_access_check_1 (templ, type_decl,
26163 scope, location);
26164 }
26165
26166 /* Convert the generic type parameters in PARM that match the types given in the
26167 range [START_IDX, END_IDX) from the current_template_parms into generic type
26168 packs. */
26169
26170 tree
26171 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26172 {
26173 tree current = current_template_parms;
26174 int depth = TMPL_PARMS_DEPTH (current);
26175 current = INNERMOST_TEMPLATE_PARMS (current);
26176 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26177
26178 for (int i = 0; i < start_idx; ++i)
26179 TREE_VEC_ELT (replacement, i)
26180 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26181
26182 for (int i = start_idx; i < end_idx; ++i)
26183 {
26184 /* Create a distinct parameter pack type from the current parm and add it
26185 to the replacement args to tsubst below into the generic function
26186 parameter. */
26187
26188 tree o = TREE_TYPE (TREE_VALUE
26189 (TREE_VEC_ELT (current, i)));
26190 tree t = copy_type (o);
26191 TEMPLATE_TYPE_PARM_INDEX (t)
26192 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26193 o, 0, 0, tf_none);
26194 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26195 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26196 TYPE_MAIN_VARIANT (t) = t;
26197 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26198 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26199 TREE_VEC_ELT (replacement, i) = t;
26200 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26201 }
26202
26203 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26204 TREE_VEC_ELT (replacement, i)
26205 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26206
26207 /* If there are more levels then build up the replacement with the outer
26208 template parms. */
26209 if (depth > 1)
26210 replacement = add_to_template_args (template_parms_to_args
26211 (TREE_CHAIN (current_template_parms)),
26212 replacement);
26213
26214 return tsubst (parm, replacement, tf_none, NULL_TREE);
26215 }
26216
26217 /* Entries in the decl_constraint hash table. */
26218 struct GTY((for_user)) constr_entry
26219 {
26220 tree decl;
26221 tree ci;
26222 };
26223
26224 /* Hashing function and equality for constraint entries. */
26225 struct constr_hasher : ggc_ptr_hash<constr_entry>
26226 {
26227 static hashval_t hash (constr_entry *e)
26228 {
26229 return (hashval_t)DECL_UID (e->decl);
26230 }
26231
26232 static bool equal (constr_entry *e1, constr_entry *e2)
26233 {
26234 return e1->decl == e2->decl;
26235 }
26236 };
26237
26238 /* A mapping from declarations to constraint information. Note that
26239 both templates and their underlying declarations are mapped to the
26240 same constraint information.
26241
26242 FIXME: This is defined in pt.c because garbage collection
26243 code is not being generated for constraint.cc. */
26244
26245 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26246
26247 /* Returns the template constraints of declaration T. If T is not
26248 constrained, return NULL_TREE. Note that T must be non-null. */
26249
26250 tree
26251 get_constraints (tree t)
26252 {
26253 if (!flag_concepts)
26254 return NULL_TREE;
26255
26256 gcc_assert (DECL_P (t));
26257 if (TREE_CODE (t) == TEMPLATE_DECL)
26258 t = DECL_TEMPLATE_RESULT (t);
26259 constr_entry elt = { t, NULL_TREE };
26260 constr_entry* found = decl_constraints->find (&elt);
26261 if (found)
26262 return found->ci;
26263 else
26264 return NULL_TREE;
26265 }
26266
26267 /* Associate the given constraint information CI with the declaration
26268 T. If T is a template, then the constraints are associated with
26269 its underlying declaration. Don't build associations if CI is
26270 NULL_TREE. */
26271
26272 void
26273 set_constraints (tree t, tree ci)
26274 {
26275 if (!ci)
26276 return;
26277 gcc_assert (t && flag_concepts);
26278 if (TREE_CODE (t) == TEMPLATE_DECL)
26279 t = DECL_TEMPLATE_RESULT (t);
26280 gcc_assert (!get_constraints (t));
26281 constr_entry elt = {t, ci};
26282 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26283 constr_entry* entry = ggc_alloc<constr_entry> ();
26284 *entry = elt;
26285 *slot = entry;
26286 }
26287
26288 /* Remove the associated constraints of the declaration T. */
26289
26290 void
26291 remove_constraints (tree t)
26292 {
26293 gcc_assert (DECL_P (t));
26294 if (TREE_CODE (t) == TEMPLATE_DECL)
26295 t = DECL_TEMPLATE_RESULT (t);
26296
26297 constr_entry elt = {t, NULL_TREE};
26298 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26299 if (slot)
26300 decl_constraints->clear_slot (slot);
26301 }
26302
26303 /* Memoized satisfaction results for declarations. This
26304 maps the pair (constraint_info, arguments) to the result computed
26305 by constraints_satisfied_p. */
26306
26307 struct GTY((for_user)) constraint_sat_entry
26308 {
26309 tree ci;
26310 tree args;
26311 tree result;
26312 };
26313
26314 /* Hashing function and equality for constraint entries. */
26315
26316 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26317 {
26318 static hashval_t hash (constraint_sat_entry *e)
26319 {
26320 hashval_t val = iterative_hash_object(e->ci, 0);
26321 return iterative_hash_template_arg (e->args, val);
26322 }
26323
26324 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26325 {
26326 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26327 }
26328 };
26329
26330 /* Memoized satisfaction results for concept checks. */
26331
26332 struct GTY((for_user)) concept_spec_entry
26333 {
26334 tree tmpl;
26335 tree args;
26336 tree result;
26337 };
26338
26339 /* Hashing function and equality for constraint entries. */
26340
26341 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26342 {
26343 static hashval_t hash (concept_spec_entry *e)
26344 {
26345 return hash_tmpl_and_args (e->tmpl, e->args);
26346 }
26347
26348 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26349 {
26350 ++comparing_specializations;
26351 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26352 --comparing_specializations;
26353 return eq;
26354 }
26355 };
26356
26357 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26358 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26359
26360 /* Search for a memoized satisfaction result. Returns one of the
26361 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26362
26363 tree
26364 lookup_constraint_satisfaction (tree ci, tree args)
26365 {
26366 constraint_sat_entry elt = { ci, args, NULL_TREE };
26367 constraint_sat_entry* found = constraint_memos->find (&elt);
26368 if (found)
26369 return found->result;
26370 else
26371 return NULL_TREE;
26372 }
26373
26374 /* Memoize the result of a satisfication test. Returns the saved result. */
26375
26376 tree
26377 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26378 {
26379 constraint_sat_entry elt = {ci, args, result};
26380 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26381 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26382 *entry = elt;
26383 *slot = entry;
26384 return result;
26385 }
26386
26387 /* Search for a memoized satisfaction result for a concept. */
26388
26389 tree
26390 lookup_concept_satisfaction (tree tmpl, tree args)
26391 {
26392 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26393 concept_spec_entry* found = concept_memos->find (&elt);
26394 if (found)
26395 return found->result;
26396 else
26397 return NULL_TREE;
26398 }
26399
26400 /* Memoize the result of a concept check. Returns the saved result. */
26401
26402 tree
26403 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26404 {
26405 concept_spec_entry elt = {tmpl, args, result};
26406 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26407 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26408 *entry = elt;
26409 *slot = entry;
26410 return result;
26411 }
26412
26413 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26414
26415 /* Returns a prior concept specialization. This returns the substituted
26416 and normalized constraints defined by the concept. */
26417
26418 tree
26419 get_concept_expansion (tree tmpl, tree args)
26420 {
26421 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26422 concept_spec_entry* found = concept_expansions->find (&elt);
26423 if (found)
26424 return found->result;
26425 else
26426 return NULL_TREE;
26427 }
26428
26429 /* Save a concept expansion for later. */
26430
26431 tree
26432 save_concept_expansion (tree tmpl, tree args, tree def)
26433 {
26434 concept_spec_entry elt = {tmpl, args, def};
26435 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26436 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26437 *entry = elt;
26438 *slot = entry;
26439 return def;
26440 }
26441
26442 static hashval_t
26443 hash_subsumption_args (tree t1, tree t2)
26444 {
26445 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26446 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26447 int val = 0;
26448 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26449 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26450 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26451 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26452 return val;
26453 }
26454
26455 /* Compare the constraints of two subsumption entries. The LEFT1 and
26456 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26457 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26458
26459 static bool
26460 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26461 {
26462 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26463 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26464 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26465 CHECK_CONSTR_ARGS (right1)))
26466 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26467 CHECK_CONSTR_ARGS (right2));
26468 return false;
26469 }
26470
26471 /* Key/value pair for learning and memoizing subsumption results. This
26472 associates a pair of check constraints (including arguments) with
26473 a boolean value indicating the result. */
26474
26475 struct GTY((for_user)) subsumption_entry
26476 {
26477 tree t1;
26478 tree t2;
26479 bool result;
26480 };
26481
26482 /* Hashing function and equality for constraint entries. */
26483
26484 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26485 {
26486 static hashval_t hash (subsumption_entry *e)
26487 {
26488 return hash_subsumption_args (e->t1, e->t2);
26489 }
26490
26491 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26492 {
26493 ++comparing_specializations;
26494 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26495 --comparing_specializations;
26496 return eq;
26497 }
26498 };
26499
26500 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26501
26502 /* Search for a previously cached subsumption result. */
26503
26504 bool*
26505 lookup_subsumption_result (tree t1, tree t2)
26506 {
26507 subsumption_entry elt = { t1, t2, false };
26508 subsumption_entry* found = subsumption_table->find (&elt);
26509 if (found)
26510 return &found->result;
26511 else
26512 return 0;
26513 }
26514
26515 /* Save a subsumption result. */
26516
26517 bool
26518 save_subsumption_result (tree t1, tree t2, bool result)
26519 {
26520 subsumption_entry elt = {t1, t2, result};
26521 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26522 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26523 *entry = elt;
26524 *slot = entry;
26525 return result;
26526 }
26527
26528 /* Set up the hash table for constraint association. */
26529
26530 void
26531 init_constraint_processing (void)
26532 {
26533 if (!flag_concepts)
26534 return;
26535
26536 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26537 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26538 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26539 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26540 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26541 }
26542
26543 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26544 0..N-1. */
26545
26546 void
26547 declare_integer_pack (void)
26548 {
26549 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26550 build_function_type_list (integer_type_node,
26551 integer_type_node,
26552 NULL_TREE),
26553 NULL_TREE, ECF_CONST);
26554 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26555 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26556 }
26557
26558 /* Set up the hash tables for template instantiations. */
26559
26560 void
26561 init_template_processing (void)
26562 {
26563 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26564 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26565
26566 if (cxx_dialect >= cxx11)
26567 declare_integer_pack ();
26568 }
26569
26570 /* Print stats about the template hash tables for -fstats. */
26571
26572 void
26573 print_template_statistics (void)
26574 {
26575 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26576 "%f collisions\n", (long) decl_specializations->size (),
26577 (long) decl_specializations->elements (),
26578 decl_specializations->collisions ());
26579 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26580 "%f collisions\n", (long) type_specializations->size (),
26581 (long) type_specializations->elements (),
26582 type_specializations->collisions ());
26583 }
26584
26585 #include "gt-cp-pt.h"