Update copyright years.
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44
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 either a non-defining friend
4984 function template declaration or a friend class template
4985 declaration. In the function case, 1 indicates a declaration, 2
4986 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4987 emitted for extraneous default arguments.
4988
4989 Returns TRUE if there were no errors found, FALSE otherwise. */
4990
4991 bool
4992 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4993 bool is_partial, int is_friend_decl)
4994 {
4995 const char *msg;
4996 int last_level_to_check;
4997 tree parm_level;
4998 bool no_errors = true;
4999
5000 /* [temp.param]
5001
5002 A default template-argument shall not be specified in a
5003 function template declaration or a function template definition, nor
5004 in the template-parameter-list of the definition of a member of a
5005 class template. */
5006
5007 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5008 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5009 /* You can't have a function template declaration in a local
5010 scope, nor you can you define a member of a class template in a
5011 local scope. */
5012 return true;
5013
5014 if ((TREE_CODE (decl) == TYPE_DECL
5015 && TREE_TYPE (decl)
5016 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5017 || (TREE_CODE (decl) == FUNCTION_DECL
5018 && LAMBDA_FUNCTION_P (decl)))
5019 /* A lambda doesn't have an explicit declaration; don't complain
5020 about the parms of the enclosing class. */
5021 return true;
5022
5023 if (current_class_type
5024 && !TYPE_BEING_DEFINED (current_class_type)
5025 && DECL_LANG_SPECIFIC (decl)
5026 && DECL_DECLARES_FUNCTION_P (decl)
5027 /* If this is either a friend defined in the scope of the class
5028 or a member function. */
5029 && (DECL_FUNCTION_MEMBER_P (decl)
5030 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5031 : DECL_FRIEND_CONTEXT (decl)
5032 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5033 : false)
5034 /* And, if it was a member function, it really was defined in
5035 the scope of the class. */
5036 && (!DECL_FUNCTION_MEMBER_P (decl)
5037 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5038 /* We already checked these parameters when the template was
5039 declared, so there's no need to do it again now. This function
5040 was defined in class scope, but we're processing its body now
5041 that the class is complete. */
5042 return true;
5043
5044 /* Core issue 226 (C++0x only): the following only applies to class
5045 templates. */
5046 if (is_primary
5047 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5048 {
5049 /* [temp.param]
5050
5051 If a template-parameter has a default template-argument, all
5052 subsequent template-parameters shall have a default
5053 template-argument supplied. */
5054 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5055 {
5056 tree inner_parms = TREE_VALUE (parm_level);
5057 int ntparms = TREE_VEC_LENGTH (inner_parms);
5058 int seen_def_arg_p = 0;
5059 int i;
5060
5061 for (i = 0; i < ntparms; ++i)
5062 {
5063 tree parm = TREE_VEC_ELT (inner_parms, i);
5064
5065 if (parm == error_mark_node)
5066 continue;
5067
5068 if (TREE_PURPOSE (parm))
5069 seen_def_arg_p = 1;
5070 else if (seen_def_arg_p
5071 && !template_parameter_pack_p (TREE_VALUE (parm)))
5072 {
5073 error ("no default argument for %qD", TREE_VALUE (parm));
5074 /* For better subsequent error-recovery, we indicate that
5075 there should have been a default argument. */
5076 TREE_PURPOSE (parm) = error_mark_node;
5077 no_errors = false;
5078 }
5079 else if (!is_partial
5080 && !is_friend_decl
5081 /* Don't complain about an enclosing partial
5082 specialization. */
5083 && parm_level == parms
5084 && TREE_CODE (decl) == TYPE_DECL
5085 && i < ntparms - 1
5086 && template_parameter_pack_p (TREE_VALUE (parm))
5087 /* A fixed parameter pack will be partially
5088 instantiated into a fixed length list. */
5089 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5090 {
5091 /* A primary class template can only have one
5092 parameter pack, at the end of the template
5093 parameter list. */
5094
5095 error ("parameter pack %q+D must be at the end of the"
5096 " template parameter list", TREE_VALUE (parm));
5097
5098 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5099 = error_mark_node;
5100 no_errors = false;
5101 }
5102 }
5103 }
5104 }
5105
5106 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5107 || is_partial
5108 || !is_primary
5109 || is_friend_decl)
5110 /* For an ordinary class template, default template arguments are
5111 allowed at the innermost level, e.g.:
5112 template <class T = int>
5113 struct S {};
5114 but, in a partial specialization, they're not allowed even
5115 there, as we have in [temp.class.spec]:
5116
5117 The template parameter list of a specialization shall not
5118 contain default template argument values.
5119
5120 So, for a partial specialization, or for a function template
5121 (in C++98/C++03), we look at all of them. */
5122 ;
5123 else
5124 /* But, for a primary class template that is not a partial
5125 specialization we look at all template parameters except the
5126 innermost ones. */
5127 parms = TREE_CHAIN (parms);
5128
5129 /* Figure out what error message to issue. */
5130 if (is_friend_decl == 2)
5131 msg = G_("default template arguments may not be used in function template "
5132 "friend re-declaration");
5133 else if (is_friend_decl)
5134 msg = G_("default template arguments may not be used in template "
5135 "friend declarations");
5136 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5137 msg = G_("default template arguments may not be used in function templates "
5138 "without -std=c++11 or -std=gnu++11");
5139 else if (is_partial)
5140 msg = G_("default template arguments may not be used in "
5141 "partial specializations");
5142 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5143 msg = G_("default argument for template parameter for class enclosing %qD");
5144 else
5145 /* Per [temp.param]/9, "A default template-argument shall not be
5146 specified in the template-parameter-lists of the definition of
5147 a member of a class template that appears outside of the member's
5148 class.", thus if we aren't handling a member of a class template
5149 there is no need to examine the parameters. */
5150 return true;
5151
5152 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5153 /* If we're inside a class definition, there's no need to
5154 examine the parameters to the class itself. On the one
5155 hand, they will be checked when the class is defined, and,
5156 on the other, default arguments are valid in things like:
5157 template <class T = double>
5158 struct S { template <class U> void f(U); };
5159 Here the default argument for `S' has no bearing on the
5160 declaration of `f'. */
5161 last_level_to_check = template_class_depth (current_class_type) + 1;
5162 else
5163 /* Check everything. */
5164 last_level_to_check = 0;
5165
5166 for (parm_level = parms;
5167 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5168 parm_level = TREE_CHAIN (parm_level))
5169 {
5170 tree inner_parms = TREE_VALUE (parm_level);
5171 int i;
5172 int ntparms;
5173
5174 ntparms = TREE_VEC_LENGTH (inner_parms);
5175 for (i = 0; i < ntparms; ++i)
5176 {
5177 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5178 continue;
5179
5180 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5181 {
5182 if (msg)
5183 {
5184 no_errors = false;
5185 if (is_friend_decl == 2)
5186 return no_errors;
5187
5188 error (msg, decl);
5189 msg = 0;
5190 }
5191
5192 /* Clear out the default argument so that we are not
5193 confused later. */
5194 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5195 }
5196 }
5197
5198 /* At this point, if we're still interested in issuing messages,
5199 they must apply to classes surrounding the object declared. */
5200 if (msg)
5201 msg = G_("default argument for template parameter for class "
5202 "enclosing %qD");
5203 }
5204
5205 return no_errors;
5206 }
5207
5208 /* Worker for push_template_decl_real, called via
5209 for_each_template_parm. DATA is really an int, indicating the
5210 level of the parameters we are interested in. If T is a template
5211 parameter of that level, return nonzero. */
5212
5213 static int
5214 template_parm_this_level_p (tree t, void* data)
5215 {
5216 int this_level = *(int *)data;
5217 int level;
5218
5219 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5220 level = TEMPLATE_PARM_LEVEL (t);
5221 else
5222 level = TEMPLATE_TYPE_LEVEL (t);
5223 return level == this_level;
5224 }
5225
5226 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5227 DATA is really an int, indicating the innermost outer level of parameters.
5228 If T is a template parameter of that level or further out, return
5229 nonzero. */
5230
5231 static int
5232 template_parm_outer_level (tree t, void *data)
5233 {
5234 int this_level = *(int *)data;
5235 int level;
5236
5237 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5238 level = TEMPLATE_PARM_LEVEL (t);
5239 else
5240 level = TEMPLATE_TYPE_LEVEL (t);
5241 return level <= this_level;
5242 }
5243
5244 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5245 parameters given by current_template_args, or reuses a
5246 previously existing one, if appropriate. Returns the DECL, or an
5247 equivalent one, if it is replaced via a call to duplicate_decls.
5248
5249 If IS_FRIEND is true, DECL is a friend declaration. */
5250
5251 tree
5252 push_template_decl_real (tree decl, bool is_friend)
5253 {
5254 tree tmpl;
5255 tree args;
5256 tree info;
5257 tree ctx;
5258 bool is_primary;
5259 bool is_partial;
5260 int new_template_p = 0;
5261 /* True if the template is a member template, in the sense of
5262 [temp.mem]. */
5263 bool member_template_p = false;
5264
5265 if (decl == error_mark_node || !current_template_parms)
5266 return error_mark_node;
5267
5268 /* See if this is a partial specialization. */
5269 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5270 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5271 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5272 || (VAR_P (decl)
5273 && DECL_LANG_SPECIFIC (decl)
5274 && DECL_TEMPLATE_SPECIALIZATION (decl)
5275 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5276
5277 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5278 is_friend = true;
5279
5280 if (is_friend)
5281 /* For a friend, we want the context of the friend, not
5282 the type of which it is a friend. */
5283 ctx = CP_DECL_CONTEXT (decl);
5284 else if (CP_DECL_CONTEXT (decl)
5285 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5286 /* In the case of a virtual function, we want the class in which
5287 it is defined. */
5288 ctx = CP_DECL_CONTEXT (decl);
5289 else
5290 /* Otherwise, if we're currently defining some class, the DECL
5291 is assumed to be a member of the class. */
5292 ctx = current_scope ();
5293
5294 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5295 ctx = NULL_TREE;
5296
5297 if (!DECL_CONTEXT (decl))
5298 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5299
5300 /* See if this is a primary template. */
5301 if (is_friend && ctx
5302 && uses_template_parms_level (ctx, processing_template_decl))
5303 /* A friend template that specifies a class context, i.e.
5304 template <typename T> friend void A<T>::f();
5305 is not primary. */
5306 is_primary = false;
5307 else if (TREE_CODE (decl) == TYPE_DECL
5308 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5309 is_primary = false;
5310 else
5311 is_primary = template_parm_scope_p ();
5312
5313 if (is_primary)
5314 {
5315 warning (OPT_Wtemplates, "template %qD declared", decl);
5316
5317 if (DECL_CLASS_SCOPE_P (decl))
5318 member_template_p = true;
5319 if (TREE_CODE (decl) == TYPE_DECL
5320 && anon_aggrname_p (DECL_NAME (decl)))
5321 {
5322 error ("template class without a name");
5323 return error_mark_node;
5324 }
5325 else if (TREE_CODE (decl) == FUNCTION_DECL)
5326 {
5327 if (member_template_p)
5328 {
5329 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5330 error ("member template %qD may not have virt-specifiers", decl);
5331 }
5332 if (DECL_DESTRUCTOR_P (decl))
5333 {
5334 /* [temp.mem]
5335
5336 A destructor shall not be a member template. */
5337 error ("destructor %qD declared as member template", decl);
5338 return error_mark_node;
5339 }
5340 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5341 && (!prototype_p (TREE_TYPE (decl))
5342 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5343 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5344 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5345 == void_list_node)))
5346 {
5347 /* [basic.stc.dynamic.allocation]
5348
5349 An allocation function can be a function
5350 template. ... Template allocation functions shall
5351 have two or more parameters. */
5352 error ("invalid template declaration of %qD", decl);
5353 return error_mark_node;
5354 }
5355 }
5356 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5357 && CLASS_TYPE_P (TREE_TYPE (decl)))
5358 {
5359 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5360 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5361 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5362 {
5363 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5364 if (TREE_CODE (t) == TYPE_DECL)
5365 t = TREE_TYPE (t);
5366 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5367 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5368 }
5369 }
5370 else if (TREE_CODE (decl) == TYPE_DECL
5371 && TYPE_DECL_ALIAS_P (decl))
5372 /* alias-declaration */
5373 gcc_assert (!DECL_ARTIFICIAL (decl));
5374 else if (VAR_P (decl))
5375 /* C++14 variable template. */;
5376 else
5377 {
5378 error ("template declaration of %q#D", decl);
5379 return error_mark_node;
5380 }
5381 }
5382
5383 /* Check to see that the rules regarding the use of default
5384 arguments are not being violated. We check args for a friend
5385 functions when we know whether it's a definition, introducing
5386 declaration or re-declaration. */
5387 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5388 check_default_tmpl_args (decl, current_template_parms,
5389 is_primary, is_partial, is_friend);
5390
5391 /* Ensure that there are no parameter packs in the type of this
5392 declaration that have not been expanded. */
5393 if (TREE_CODE (decl) == FUNCTION_DECL)
5394 {
5395 /* Check each of the arguments individually to see if there are
5396 any bare parameter packs. */
5397 tree type = TREE_TYPE (decl);
5398 tree arg = DECL_ARGUMENTS (decl);
5399 tree argtype = TYPE_ARG_TYPES (type);
5400
5401 while (arg && argtype)
5402 {
5403 if (!DECL_PACK_P (arg)
5404 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5405 {
5406 /* This is a PARM_DECL that contains unexpanded parameter
5407 packs. We have already complained about this in the
5408 check_for_bare_parameter_packs call, so just replace
5409 these types with ERROR_MARK_NODE. */
5410 TREE_TYPE (arg) = error_mark_node;
5411 TREE_VALUE (argtype) = error_mark_node;
5412 }
5413
5414 arg = DECL_CHAIN (arg);
5415 argtype = TREE_CHAIN (argtype);
5416 }
5417
5418 /* Check for bare parameter packs in the return type and the
5419 exception specifiers. */
5420 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5421 /* Errors were already issued, set return type to int
5422 as the frontend doesn't expect error_mark_node as
5423 the return type. */
5424 TREE_TYPE (type) = integer_type_node;
5425 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5426 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5427 }
5428 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5429 && TYPE_DECL_ALIAS_P (decl))
5430 ? DECL_ORIGINAL_TYPE (decl)
5431 : TREE_TYPE (decl)))
5432 {
5433 TREE_TYPE (decl) = error_mark_node;
5434 return error_mark_node;
5435 }
5436
5437 if (is_partial)
5438 return process_partial_specialization (decl);
5439
5440 args = current_template_args ();
5441
5442 if (!ctx
5443 || TREE_CODE (ctx) == FUNCTION_DECL
5444 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5445 || (TREE_CODE (decl) == TYPE_DECL
5446 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5447 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5448 {
5449 if (DECL_LANG_SPECIFIC (decl)
5450 && DECL_TEMPLATE_INFO (decl)
5451 && DECL_TI_TEMPLATE (decl))
5452 tmpl = DECL_TI_TEMPLATE (decl);
5453 /* If DECL is a TYPE_DECL for a class-template, then there won't
5454 be DECL_LANG_SPECIFIC. The information equivalent to
5455 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5456 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5457 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5458 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5459 {
5460 /* Since a template declaration already existed for this
5461 class-type, we must be redeclaring it here. Make sure
5462 that the redeclaration is valid. */
5463 redeclare_class_template (TREE_TYPE (decl),
5464 current_template_parms,
5465 current_template_constraints ());
5466 /* We don't need to create a new TEMPLATE_DECL; just use the
5467 one we already had. */
5468 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5469 }
5470 else
5471 {
5472 tmpl = build_template_decl (decl, current_template_parms,
5473 member_template_p);
5474 new_template_p = 1;
5475
5476 if (DECL_LANG_SPECIFIC (decl)
5477 && DECL_TEMPLATE_SPECIALIZATION (decl))
5478 {
5479 /* A specialization of a member template of a template
5480 class. */
5481 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5482 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5483 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5484 }
5485 }
5486 }
5487 else
5488 {
5489 tree a, t, current, parms;
5490 int i;
5491 tree tinfo = get_template_info (decl);
5492
5493 if (!tinfo)
5494 {
5495 error ("template definition of non-template %q#D", decl);
5496 return error_mark_node;
5497 }
5498
5499 tmpl = TI_TEMPLATE (tinfo);
5500
5501 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5502 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5503 && DECL_TEMPLATE_SPECIALIZATION (decl)
5504 && DECL_MEMBER_TEMPLATE_P (tmpl))
5505 {
5506 tree new_tmpl;
5507
5508 /* The declaration is a specialization of a member
5509 template, declared outside the class. Therefore, the
5510 innermost template arguments will be NULL, so we
5511 replace them with the arguments determined by the
5512 earlier call to check_explicit_specialization. */
5513 args = DECL_TI_ARGS (decl);
5514
5515 new_tmpl
5516 = build_template_decl (decl, current_template_parms,
5517 member_template_p);
5518 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5519 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5520 DECL_TI_TEMPLATE (decl) = new_tmpl;
5521 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5522 DECL_TEMPLATE_INFO (new_tmpl)
5523 = build_template_info (tmpl, args);
5524
5525 register_specialization (new_tmpl,
5526 most_general_template (tmpl),
5527 args,
5528 is_friend, 0);
5529 return decl;
5530 }
5531
5532 /* Make sure the template headers we got make sense. */
5533
5534 parms = DECL_TEMPLATE_PARMS (tmpl);
5535 i = TMPL_PARMS_DEPTH (parms);
5536 if (TMPL_ARGS_DEPTH (args) != i)
5537 {
5538 error ("expected %d levels of template parms for %q#D, got %d",
5539 i, decl, TMPL_ARGS_DEPTH (args));
5540 DECL_INTERFACE_KNOWN (decl) = 1;
5541 return error_mark_node;
5542 }
5543 else
5544 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5545 {
5546 a = TMPL_ARGS_LEVEL (args, i);
5547 t = INNERMOST_TEMPLATE_PARMS (parms);
5548
5549 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5550 {
5551 if (current == decl)
5552 error ("got %d template parameters for %q#D",
5553 TREE_VEC_LENGTH (a), decl);
5554 else
5555 error ("got %d template parameters for %q#T",
5556 TREE_VEC_LENGTH (a), current);
5557 error (" but %d required", TREE_VEC_LENGTH (t));
5558 /* Avoid crash in import_export_decl. */
5559 DECL_INTERFACE_KNOWN (decl) = 1;
5560 return error_mark_node;
5561 }
5562
5563 if (current == decl)
5564 current = ctx;
5565 else if (current == NULL_TREE)
5566 /* Can happen in erroneous input. */
5567 break;
5568 else
5569 current = get_containing_scope (current);
5570 }
5571
5572 /* Check that the parms are used in the appropriate qualifying scopes
5573 in the declarator. */
5574 if (!comp_template_args
5575 (TI_ARGS (tinfo),
5576 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5577 {
5578 error ("template arguments to %qD do not match original "
5579 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5580 if (!uses_template_parms (TI_ARGS (tinfo)))
5581 inform (input_location, "use %<template<>%> for"
5582 " an explicit specialization");
5583 /* Avoid crash in import_export_decl. */
5584 DECL_INTERFACE_KNOWN (decl) = 1;
5585 return error_mark_node;
5586 }
5587 }
5588
5589 DECL_TEMPLATE_RESULT (tmpl) = decl;
5590 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5591
5592 /* Push template declarations for global functions and types. Note
5593 that we do not try to push a global template friend declared in a
5594 template class; such a thing may well depend on the template
5595 parameters of the class. */
5596 if (new_template_p && !ctx
5597 && !(is_friend && template_class_depth (current_class_type) > 0))
5598 {
5599 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5600 if (tmpl == error_mark_node)
5601 return error_mark_node;
5602
5603 /* Hide template friend classes that haven't been declared yet. */
5604 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5605 {
5606 DECL_ANTICIPATED (tmpl) = 1;
5607 DECL_FRIEND_P (tmpl) = 1;
5608 }
5609 }
5610
5611 if (is_primary)
5612 {
5613 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5614
5615 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5616
5617 /* Give template template parms a DECL_CONTEXT of the template
5618 for which they are a parameter. */
5619 parms = INNERMOST_TEMPLATE_PARMS (parms);
5620 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5621 {
5622 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5623 if (TREE_CODE (parm) == TEMPLATE_DECL)
5624 DECL_CONTEXT (parm) = tmpl;
5625 }
5626
5627 if (TREE_CODE (decl) == TYPE_DECL
5628 && TYPE_DECL_ALIAS_P (decl)
5629 && complex_alias_template_p (tmpl))
5630 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5631 }
5632
5633 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5634 back to its most general template. If TMPL is a specialization,
5635 ARGS may only have the innermost set of arguments. Add the missing
5636 argument levels if necessary. */
5637 if (DECL_TEMPLATE_INFO (tmpl))
5638 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5639
5640 info = build_template_info (tmpl, args);
5641
5642 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5643 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5644 else
5645 {
5646 if (is_primary)
5647 retrofit_lang_decl (decl);
5648 if (DECL_LANG_SPECIFIC (decl))
5649 DECL_TEMPLATE_INFO (decl) = info;
5650 }
5651
5652 if (flag_implicit_templates
5653 && !is_friend
5654 && TREE_PUBLIC (decl)
5655 && VAR_OR_FUNCTION_DECL_P (decl))
5656 /* Set DECL_COMDAT on template instantiations; if we force
5657 them to be emitted by explicit instantiation or -frepo,
5658 mark_needed will tell cgraph to do the right thing. */
5659 DECL_COMDAT (decl) = true;
5660
5661 return DECL_TEMPLATE_RESULT (tmpl);
5662 }
5663
5664 tree
5665 push_template_decl (tree decl)
5666 {
5667 return push_template_decl_real (decl, false);
5668 }
5669
5670 /* FN is an inheriting constructor that inherits from the constructor
5671 template INHERITED; turn FN into a constructor template with a matching
5672 template header. */
5673
5674 tree
5675 add_inherited_template_parms (tree fn, tree inherited)
5676 {
5677 tree inner_parms
5678 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5679 inner_parms = copy_node (inner_parms);
5680 tree parms
5681 = tree_cons (size_int (processing_template_decl + 1),
5682 inner_parms, current_template_parms);
5683 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5684 tree args = template_parms_to_args (parms);
5685 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5686 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5687 DECL_TEMPLATE_RESULT (tmpl) = fn;
5688 DECL_ARTIFICIAL (tmpl) = true;
5689 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5690 return tmpl;
5691 }
5692
5693 /* Called when a class template TYPE is redeclared with the indicated
5694 template PARMS, e.g.:
5695
5696 template <class T> struct S;
5697 template <class T> struct S {}; */
5698
5699 bool
5700 redeclare_class_template (tree type, tree parms, tree cons)
5701 {
5702 tree tmpl;
5703 tree tmpl_parms;
5704 int i;
5705
5706 if (!TYPE_TEMPLATE_INFO (type))
5707 {
5708 error ("%qT is not a template type", type);
5709 return false;
5710 }
5711
5712 tmpl = TYPE_TI_TEMPLATE (type);
5713 if (!PRIMARY_TEMPLATE_P (tmpl))
5714 /* The type is nested in some template class. Nothing to worry
5715 about here; there are no new template parameters for the nested
5716 type. */
5717 return true;
5718
5719 if (!parms)
5720 {
5721 error ("template specifiers not specified in declaration of %qD",
5722 tmpl);
5723 return false;
5724 }
5725
5726 parms = INNERMOST_TEMPLATE_PARMS (parms);
5727 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5728
5729 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5730 {
5731 error_n (input_location, TREE_VEC_LENGTH (parms),
5732 "redeclared with %d template parameter",
5733 "redeclared with %d template parameters",
5734 TREE_VEC_LENGTH (parms));
5735 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5736 "previous declaration %qD used %d template parameter",
5737 "previous declaration %qD used %d template parameters",
5738 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5739 return false;
5740 }
5741
5742 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5743 {
5744 tree tmpl_parm;
5745 tree parm;
5746 tree tmpl_default;
5747 tree parm_default;
5748
5749 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5750 || TREE_VEC_ELT (parms, i) == error_mark_node)
5751 continue;
5752
5753 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5754 if (error_operand_p (tmpl_parm))
5755 return false;
5756
5757 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5758 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5759 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5760
5761 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5762 TEMPLATE_DECL. */
5763 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5764 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5765 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5766 || (TREE_CODE (tmpl_parm) != PARM_DECL
5767 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5768 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5769 || (TREE_CODE (tmpl_parm) == PARM_DECL
5770 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5771 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5772 {
5773 error ("template parameter %q+#D", tmpl_parm);
5774 error ("redeclared here as %q#D", parm);
5775 return false;
5776 }
5777
5778 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5779 {
5780 /* We have in [temp.param]:
5781
5782 A template-parameter may not be given default arguments
5783 by two different declarations in the same scope. */
5784 error_at (input_location, "redefinition of default argument for %q#D", parm);
5785 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5786 "original definition appeared here");
5787 return false;
5788 }
5789
5790 if (parm_default != NULL_TREE)
5791 /* Update the previous template parameters (which are the ones
5792 that will really count) with the new default value. */
5793 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5794 else if (tmpl_default != NULL_TREE)
5795 /* Update the new parameters, too; they'll be used as the
5796 parameters for any members. */
5797 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5798
5799 /* Give each template template parm in this redeclaration a
5800 DECL_CONTEXT of the template for which they are a parameter. */
5801 if (TREE_CODE (parm) == TEMPLATE_DECL)
5802 {
5803 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5804 DECL_CONTEXT (parm) = tmpl;
5805 }
5806
5807 if (TREE_CODE (parm) == TYPE_DECL)
5808 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5809 }
5810
5811 // Cannot redeclare a class template with a different set of constraints.
5812 if (!equivalent_constraints (get_constraints (tmpl), cons))
5813 {
5814 error_at (input_location, "redeclaration %q#D with different "
5815 "constraints", tmpl);
5816 inform (DECL_SOURCE_LOCATION (tmpl),
5817 "original declaration appeared here");
5818 }
5819
5820 return true;
5821 }
5822
5823 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5824 to be used when the caller has already checked
5825 (processing_template_decl
5826 && !instantiation_dependent_expression_p (expr)
5827 && potential_constant_expression (expr))
5828 and cleared processing_template_decl. */
5829
5830 tree
5831 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5832 {
5833 return tsubst_copy_and_build (expr,
5834 /*args=*/NULL_TREE,
5835 complain,
5836 /*in_decl=*/NULL_TREE,
5837 /*function_p=*/false,
5838 /*integral_constant_expression_p=*/true);
5839 }
5840
5841 /* Simplify EXPR if it is a non-dependent expression. Returns the
5842 (possibly simplified) expression. */
5843
5844 tree
5845 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5846 {
5847 if (expr == NULL_TREE)
5848 return NULL_TREE;
5849
5850 /* If we're in a template, but EXPR isn't value dependent, simplify
5851 it. We're supposed to treat:
5852
5853 template <typename T> void f(T[1 + 1]);
5854 template <typename T> void f(T[2]);
5855
5856 as two declarations of the same function, for example. */
5857 if (processing_template_decl
5858 && is_nondependent_constant_expression (expr))
5859 {
5860 processing_template_decl_sentinel s;
5861 expr = instantiate_non_dependent_expr_internal (expr, complain);
5862 }
5863 return expr;
5864 }
5865
5866 tree
5867 instantiate_non_dependent_expr (tree expr)
5868 {
5869 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5870 }
5871
5872 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5873 an uninstantiated expression. */
5874
5875 tree
5876 instantiate_non_dependent_or_null (tree expr)
5877 {
5878 if (expr == NULL_TREE)
5879 return NULL_TREE;
5880 if (processing_template_decl)
5881 {
5882 if (!is_nondependent_constant_expression (expr))
5883 expr = NULL_TREE;
5884 else
5885 {
5886 processing_template_decl_sentinel s;
5887 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5888 }
5889 }
5890 return expr;
5891 }
5892
5893 /* True iff T is a specialization of a variable template. */
5894
5895 bool
5896 variable_template_specialization_p (tree t)
5897 {
5898 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5899 return false;
5900 tree tmpl = DECL_TI_TEMPLATE (t);
5901 return variable_template_p (tmpl);
5902 }
5903
5904 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5905 template declaration, or a TYPE_DECL for an alias declaration. */
5906
5907 bool
5908 alias_type_or_template_p (tree t)
5909 {
5910 if (t == NULL_TREE)
5911 return false;
5912 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5913 || (TYPE_P (t)
5914 && TYPE_NAME (t)
5915 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5916 || DECL_ALIAS_TEMPLATE_P (t));
5917 }
5918
5919 /* Return TRUE iff T is a specialization of an alias template. */
5920
5921 bool
5922 alias_template_specialization_p (const_tree t)
5923 {
5924 /* It's an alias template specialization if it's an alias and its
5925 TYPE_NAME is a specialization of a primary template. */
5926 if (TYPE_ALIAS_P (t))
5927 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5928 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5929
5930 return false;
5931 }
5932
5933 /* An alias template is complex from a SFINAE perspective if a template-id
5934 using that alias can be ill-formed when the expansion is not, as with
5935 the void_t template. We determine this by checking whether the
5936 expansion for the alias template uses all its template parameters. */
5937
5938 struct uses_all_template_parms_data
5939 {
5940 int level;
5941 bool *seen;
5942 };
5943
5944 static int
5945 uses_all_template_parms_r (tree t, void *data_)
5946 {
5947 struct uses_all_template_parms_data &data
5948 = *(struct uses_all_template_parms_data*)data_;
5949 tree idx = get_template_parm_index (t);
5950
5951 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5952 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5953 return 0;
5954 }
5955
5956 static bool
5957 complex_alias_template_p (const_tree tmpl)
5958 {
5959 struct uses_all_template_parms_data data;
5960 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5961 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5962 data.level = TMPL_PARMS_DEPTH (parms);
5963 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5964 data.seen = XALLOCAVEC (bool, len);
5965 for (int i = 0; i < len; ++i)
5966 data.seen[i] = false;
5967
5968 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5969 for (int i = 0; i < len; ++i)
5970 if (!data.seen[i])
5971 return true;
5972 return false;
5973 }
5974
5975 /* Return TRUE iff T is a specialization of a complex alias template with
5976 dependent template-arguments. */
5977
5978 bool
5979 dependent_alias_template_spec_p (const_tree t)
5980 {
5981 if (!alias_template_specialization_p (t))
5982 return false;
5983
5984 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5985 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5986 return false;
5987
5988 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5989 if (!any_dependent_template_arguments_p (args))
5990 return false;
5991
5992 return true;
5993 }
5994
5995 /* Return the number of innermost template parameters in TMPL. */
5996
5997 static int
5998 num_innermost_template_parms (tree tmpl)
5999 {
6000 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6001 return TREE_VEC_LENGTH (parms);
6002 }
6003
6004 /* Return either TMPL or another template that it is equivalent to under DR
6005 1286: An alias that just changes the name of a template is equivalent to
6006 the other template. */
6007
6008 static tree
6009 get_underlying_template (tree tmpl)
6010 {
6011 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6012 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6013 {
6014 /* Determine if the alias is equivalent to an underlying template. */
6015 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6016 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6017 if (!tinfo)
6018 break;
6019
6020 tree underlying = TI_TEMPLATE (tinfo);
6021 if (!PRIMARY_TEMPLATE_P (underlying)
6022 || (num_innermost_template_parms (tmpl)
6023 != num_innermost_template_parms (underlying)))
6024 break;
6025
6026 tree alias_args = INNERMOST_TEMPLATE_ARGS
6027 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6028 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6029 break;
6030
6031 /* Alias is equivalent. Strip it and repeat. */
6032 tmpl = underlying;
6033 }
6034
6035 return tmpl;
6036 }
6037
6038 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6039 must be a reference-to-function or a pointer-to-function type, as specified
6040 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6041 and check that the resulting function has external linkage. */
6042
6043 static tree
6044 convert_nontype_argument_function (tree type, tree expr,
6045 tsubst_flags_t complain)
6046 {
6047 tree fns = expr;
6048 tree fn, fn_no_ptr;
6049 linkage_kind linkage;
6050
6051 fn = instantiate_type (type, fns, tf_none);
6052 if (fn == error_mark_node)
6053 return error_mark_node;
6054
6055 if (value_dependent_expression_p (fn))
6056 goto accept;
6057
6058 fn_no_ptr = strip_fnptr_conv (fn);
6059 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6060 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6061 if (BASELINK_P (fn_no_ptr))
6062 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6063
6064 /* [temp.arg.nontype]/1
6065
6066 A template-argument for a non-type, non-template template-parameter
6067 shall be one of:
6068 [...]
6069 -- the address of an object or function with external [C++11: or
6070 internal] linkage. */
6071
6072 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6073 {
6074 if (complain & tf_error)
6075 {
6076 error ("%qE is not a valid template argument for type %qT",
6077 expr, type);
6078 if (TYPE_PTR_P (type))
6079 inform (input_location, "it must be the address of a function "
6080 "with external linkage");
6081 else
6082 inform (input_location, "it must be the name of a function with "
6083 "external linkage");
6084 }
6085 return NULL_TREE;
6086 }
6087
6088 linkage = decl_linkage (fn_no_ptr);
6089 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6090 {
6091 if (complain & tf_error)
6092 {
6093 if (cxx_dialect >= cxx11)
6094 error ("%qE is not a valid template argument for type %qT "
6095 "because %qD has no linkage",
6096 expr, type, fn_no_ptr);
6097 else
6098 error ("%qE is not a valid template argument for type %qT "
6099 "because %qD does not have external linkage",
6100 expr, type, fn_no_ptr);
6101 }
6102 return NULL_TREE;
6103 }
6104
6105 accept:
6106 if (TREE_CODE (type) == REFERENCE_TYPE)
6107 fn = build_address (fn);
6108 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6109 fn = build_nop (type, fn);
6110
6111 return fn;
6112 }
6113
6114 /* Subroutine of convert_nontype_argument.
6115 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6116 Emit an error otherwise. */
6117
6118 static bool
6119 check_valid_ptrmem_cst_expr (tree type, tree expr,
6120 tsubst_flags_t complain)
6121 {
6122 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6123 tree orig_expr = expr;
6124 STRIP_NOPS (expr);
6125 if (null_ptr_cst_p (expr))
6126 return true;
6127 if (TREE_CODE (expr) == PTRMEM_CST
6128 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6129 PTRMEM_CST_CLASS (expr)))
6130 return true;
6131 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6132 return true;
6133 if (processing_template_decl
6134 && TREE_CODE (expr) == ADDR_EXPR
6135 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6136 return true;
6137 if (complain & tf_error)
6138 {
6139 error_at (loc, "%qE is not a valid template argument for type %qT",
6140 orig_expr, type);
6141 if (TREE_CODE (expr) != PTRMEM_CST)
6142 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6143 else
6144 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6145 }
6146 return false;
6147 }
6148
6149 /* Returns TRUE iff the address of OP is value-dependent.
6150
6151 14.6.2.4 [temp.dep.temp]:
6152 A non-integral non-type template-argument is dependent if its type is
6153 dependent or it has either of the following forms
6154 qualified-id
6155 & qualified-id
6156 and contains a nested-name-specifier which specifies a class-name that
6157 names a dependent type.
6158
6159 We generalize this to just say that the address of a member of a
6160 dependent class is value-dependent; the above doesn't cover the
6161 address of a static data member named with an unqualified-id. */
6162
6163 static bool
6164 has_value_dependent_address (tree op)
6165 {
6166 /* We could use get_inner_reference here, but there's no need;
6167 this is only relevant for template non-type arguments, which
6168 can only be expressed as &id-expression. */
6169 if (DECL_P (op))
6170 {
6171 tree ctx = CP_DECL_CONTEXT (op);
6172 if (TYPE_P (ctx) && dependent_type_p (ctx))
6173 return true;
6174 }
6175
6176 return false;
6177 }
6178
6179 /* The next set of functions are used for providing helpful explanatory
6180 diagnostics for failed overload resolution. Their messages should be
6181 indented by two spaces for consistency with the messages in
6182 call.c */
6183
6184 static int
6185 unify_success (bool /*explain_p*/)
6186 {
6187 return 0;
6188 }
6189
6190 /* Other failure functions should call this one, to provide a single function
6191 for setting a breakpoint on. */
6192
6193 static int
6194 unify_invalid (bool /*explain_p*/)
6195 {
6196 return 1;
6197 }
6198
6199 static int
6200 unify_parameter_deduction_failure (bool explain_p, tree parm)
6201 {
6202 if (explain_p)
6203 inform (input_location,
6204 " couldn't deduce template parameter %qD", parm);
6205 return unify_invalid (explain_p);
6206 }
6207
6208 static int
6209 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6210 {
6211 if (explain_p)
6212 inform (input_location,
6213 " types %qT and %qT have incompatible cv-qualifiers",
6214 parm, arg);
6215 return unify_invalid (explain_p);
6216 }
6217
6218 static int
6219 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6220 {
6221 if (explain_p)
6222 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6223 return unify_invalid (explain_p);
6224 }
6225
6226 static int
6227 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6228 {
6229 if (explain_p)
6230 inform (input_location,
6231 " template parameter %qD is not a parameter pack, but "
6232 "argument %qD is",
6233 parm, arg);
6234 return unify_invalid (explain_p);
6235 }
6236
6237 static int
6238 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6239 {
6240 if (explain_p)
6241 inform (input_location,
6242 " template argument %qE does not match "
6243 "pointer-to-member constant %qE",
6244 arg, parm);
6245 return unify_invalid (explain_p);
6246 }
6247
6248 static int
6249 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6250 {
6251 if (explain_p)
6252 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6253 return unify_invalid (explain_p);
6254 }
6255
6256 static int
6257 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6258 {
6259 if (explain_p)
6260 inform (input_location,
6261 " inconsistent parameter pack deduction with %qT and %qT",
6262 old_arg, new_arg);
6263 return unify_invalid (explain_p);
6264 }
6265
6266 static int
6267 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6268 {
6269 if (explain_p)
6270 {
6271 if (TYPE_P (parm))
6272 inform (input_location,
6273 " deduced conflicting types for parameter %qT (%qT and %qT)",
6274 parm, first, second);
6275 else
6276 inform (input_location,
6277 " deduced conflicting values for non-type parameter "
6278 "%qE (%qE and %qE)", parm, first, second);
6279 }
6280 return unify_invalid (explain_p);
6281 }
6282
6283 static int
6284 unify_vla_arg (bool explain_p, tree arg)
6285 {
6286 if (explain_p)
6287 inform (input_location,
6288 " variable-sized array type %qT is not "
6289 "a valid template argument",
6290 arg);
6291 return unify_invalid (explain_p);
6292 }
6293
6294 static int
6295 unify_method_type_error (bool explain_p, tree arg)
6296 {
6297 if (explain_p)
6298 inform (input_location,
6299 " member function type %qT is not a valid template argument",
6300 arg);
6301 return unify_invalid (explain_p);
6302 }
6303
6304 static int
6305 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6306 {
6307 if (explain_p)
6308 {
6309 if (least_p)
6310 inform_n (input_location, wanted,
6311 " candidate expects at least %d argument, %d provided",
6312 " candidate expects at least %d arguments, %d provided",
6313 wanted, have);
6314 else
6315 inform_n (input_location, wanted,
6316 " candidate expects %d argument, %d provided",
6317 " candidate expects %d arguments, %d provided",
6318 wanted, have);
6319 }
6320 return unify_invalid (explain_p);
6321 }
6322
6323 static int
6324 unify_too_many_arguments (bool explain_p, int have, int wanted)
6325 {
6326 return unify_arity (explain_p, have, wanted);
6327 }
6328
6329 static int
6330 unify_too_few_arguments (bool explain_p, int have, int wanted,
6331 bool least_p = false)
6332 {
6333 return unify_arity (explain_p, have, wanted, least_p);
6334 }
6335
6336 static int
6337 unify_arg_conversion (bool explain_p, tree to_type,
6338 tree from_type, tree arg)
6339 {
6340 if (explain_p)
6341 inform (EXPR_LOC_OR_LOC (arg, input_location),
6342 " cannot convert %qE (type %qT) to type %qT",
6343 arg, from_type, to_type);
6344 return unify_invalid (explain_p);
6345 }
6346
6347 static int
6348 unify_no_common_base (bool explain_p, enum template_base_result r,
6349 tree parm, tree arg)
6350 {
6351 if (explain_p)
6352 switch (r)
6353 {
6354 case tbr_ambiguous_baseclass:
6355 inform (input_location, " %qT is an ambiguous base class of %qT",
6356 parm, arg);
6357 break;
6358 default:
6359 inform (input_location, " %qT is not derived from %qT", arg, parm);
6360 break;
6361 }
6362 return unify_invalid (explain_p);
6363 }
6364
6365 static int
6366 unify_inconsistent_template_template_parameters (bool explain_p)
6367 {
6368 if (explain_p)
6369 inform (input_location,
6370 " template parameters of a template template argument are "
6371 "inconsistent with other deduced template arguments");
6372 return unify_invalid (explain_p);
6373 }
6374
6375 static int
6376 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6377 {
6378 if (explain_p)
6379 inform (input_location,
6380 " can't deduce a template for %qT from non-template type %qT",
6381 parm, arg);
6382 return unify_invalid (explain_p);
6383 }
6384
6385 static int
6386 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6387 {
6388 if (explain_p)
6389 inform (input_location,
6390 " template argument %qE does not match %qE", arg, parm);
6391 return unify_invalid (explain_p);
6392 }
6393
6394 /* Attempt to convert the non-type template parameter EXPR to the
6395 indicated TYPE. If the conversion is successful, return the
6396 converted value. If the conversion is unsuccessful, return
6397 NULL_TREE if we issued an error message, or error_mark_node if we
6398 did not. We issue error messages for out-and-out bad template
6399 parameters, but not simply because the conversion failed, since we
6400 might be just trying to do argument deduction. Both TYPE and EXPR
6401 must be non-dependent.
6402
6403 The conversion follows the special rules described in
6404 [temp.arg.nontype], and it is much more strict than an implicit
6405 conversion.
6406
6407 This function is called twice for each template argument (see
6408 lookup_template_class for a more accurate description of this
6409 problem). This means that we need to handle expressions which
6410 are not valid in a C++ source, but can be created from the
6411 first call (for instance, casts to perform conversions). These
6412 hacks can go away after we fix the double coercion problem. */
6413
6414 static tree
6415 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6416 {
6417 tree expr_type;
6418 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6419 tree orig_expr = expr;
6420
6421 /* Detect immediately string literals as invalid non-type argument.
6422 This special-case is not needed for correctness (we would easily
6423 catch this later), but only to provide better diagnostic for this
6424 common user mistake. As suggested by DR 100, we do not mention
6425 linkage issues in the diagnostic as this is not the point. */
6426 /* FIXME we're making this OK. */
6427 if (TREE_CODE (expr) == STRING_CST)
6428 {
6429 if (complain & tf_error)
6430 error ("%qE is not a valid template argument for type %qT "
6431 "because string literals can never be used in this context",
6432 expr, type);
6433 return NULL_TREE;
6434 }
6435
6436 /* Add the ADDR_EXPR now for the benefit of
6437 value_dependent_expression_p. */
6438 if (TYPE_PTROBV_P (type)
6439 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6440 {
6441 expr = decay_conversion (expr, complain);
6442 if (expr == error_mark_node)
6443 return error_mark_node;
6444 }
6445
6446 /* If we are in a template, EXPR may be non-dependent, but still
6447 have a syntactic, rather than semantic, form. For example, EXPR
6448 might be a SCOPE_REF, rather than the VAR_DECL to which the
6449 SCOPE_REF refers. Preserving the qualifying scope is necessary
6450 so that access checking can be performed when the template is
6451 instantiated -- but here we need the resolved form so that we can
6452 convert the argument. */
6453 bool non_dep = false;
6454 if (TYPE_REF_OBJ_P (type)
6455 && has_value_dependent_address (expr))
6456 /* If we want the address and it's value-dependent, don't fold. */;
6457 else if (processing_template_decl
6458 && is_nondependent_constant_expression (expr))
6459 non_dep = true;
6460 if (error_operand_p (expr))
6461 return error_mark_node;
6462 expr_type = TREE_TYPE (expr);
6463
6464 /* If the argument is non-dependent, perform any conversions in
6465 non-dependent context as well. */
6466 processing_template_decl_sentinel s (non_dep);
6467 if (non_dep)
6468 expr = instantiate_non_dependent_expr_internal (expr, complain);
6469
6470 if (value_dependent_expression_p (expr))
6471 expr = canonicalize_expr_argument (expr, complain);
6472
6473 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6474 to a non-type argument of "nullptr". */
6475 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6476 expr = fold_simple (convert (type, expr));
6477
6478 /* In C++11, integral or enumeration non-type template arguments can be
6479 arbitrary constant expressions. Pointer and pointer to
6480 member arguments can be general constant expressions that evaluate
6481 to a null value, but otherwise still need to be of a specific form. */
6482 if (cxx_dialect >= cxx11)
6483 {
6484 if (TREE_CODE (expr) == PTRMEM_CST)
6485 /* A PTRMEM_CST is already constant, and a valid template
6486 argument for a parameter of pointer to member type, we just want
6487 to leave it in that form rather than lower it to a
6488 CONSTRUCTOR. */;
6489 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6490 || cxx_dialect >= cxx17)
6491 {
6492 /* C++17: A template-argument for a non-type template-parameter shall
6493 be a converted constant expression (8.20) of the type of the
6494 template-parameter. */
6495 expr = build_converted_constant_expr (type, expr, complain);
6496 if (expr == error_mark_node)
6497 return error_mark_node;
6498 expr = maybe_constant_value (expr);
6499 expr = convert_from_reference (expr);
6500 }
6501 else if (TYPE_PTR_OR_PTRMEM_P (type))
6502 {
6503 tree folded = maybe_constant_value (expr);
6504 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6505 : null_member_pointer_value_p (folded))
6506 expr = folded;
6507 }
6508 }
6509
6510 if (TREE_CODE (type) == REFERENCE_TYPE)
6511 expr = mark_lvalue_use (expr);
6512 else
6513 expr = mark_rvalue_use (expr);
6514
6515 /* HACK: Due to double coercion, we can get a
6516 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6517 which is the tree that we built on the first call (see
6518 below when coercing to reference to object or to reference to
6519 function). We just strip everything and get to the arg.
6520 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6521 for examples. */
6522 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6523 {
6524 tree probe_type, probe = expr;
6525 if (REFERENCE_REF_P (probe))
6526 probe = TREE_OPERAND (probe, 0);
6527 probe_type = TREE_TYPE (probe);
6528 if (TREE_CODE (probe) == NOP_EXPR)
6529 {
6530 /* ??? Maybe we could use convert_from_reference here, but we
6531 would need to relax its constraints because the NOP_EXPR
6532 could actually change the type to something more cv-qualified,
6533 and this is not folded by convert_from_reference. */
6534 tree addr = TREE_OPERAND (probe, 0);
6535 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6536 && TREE_CODE (addr) == ADDR_EXPR
6537 && TYPE_PTR_P (TREE_TYPE (addr))
6538 && (same_type_ignoring_top_level_qualifiers_p
6539 (TREE_TYPE (probe_type),
6540 TREE_TYPE (TREE_TYPE (addr)))))
6541 {
6542 expr = TREE_OPERAND (addr, 0);
6543 expr_type = TREE_TYPE (probe_type);
6544 }
6545 }
6546 }
6547
6548 /* [temp.arg.nontype]/5, bullet 1
6549
6550 For a non-type template-parameter of integral or enumeration type,
6551 integral promotions (_conv.prom_) and integral conversions
6552 (_conv.integral_) are applied. */
6553 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6554 {
6555 if (cxx_dialect < cxx11)
6556 {
6557 tree t = build_converted_constant_expr (type, expr, complain);
6558 t = maybe_constant_value (t);
6559 if (t != error_mark_node)
6560 expr = t;
6561 }
6562
6563 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6564 return error_mark_node;
6565
6566 /* Notice that there are constant expressions like '4 % 0' which
6567 do not fold into integer constants. */
6568 if (TREE_CODE (expr) != INTEGER_CST
6569 && !value_dependent_expression_p (expr))
6570 {
6571 if (complain & tf_error)
6572 {
6573 int errs = errorcount, warns = warningcount + werrorcount;
6574 if (!require_potential_constant_expression (expr))
6575 expr = error_mark_node;
6576 else
6577 expr = cxx_constant_value (expr);
6578 if (errorcount > errs || warningcount + werrorcount > warns)
6579 inform (loc, "in template argument for type %qT ", type);
6580 if (expr == error_mark_node)
6581 return NULL_TREE;
6582 /* else cxx_constant_value complained but gave us
6583 a real constant, so go ahead. */
6584 if (TREE_CODE (expr) != INTEGER_CST)
6585 {
6586 /* Some assemble time constant expressions like
6587 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6588 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6589 as we can emit them into .rodata initializers of
6590 variables, yet they can't fold into an INTEGER_CST at
6591 compile time. Refuse them here. */
6592 gcc_checking_assert (reduced_constant_expression_p (expr));
6593 error_at (loc, "template argument %qE for type %qT not "
6594 "a constant integer", expr, type);
6595 return NULL_TREE;
6596 }
6597 }
6598 else
6599 return NULL_TREE;
6600 }
6601
6602 /* Avoid typedef problems. */
6603 if (TREE_TYPE (expr) != type)
6604 expr = fold_convert (type, expr);
6605 }
6606 /* [temp.arg.nontype]/5, bullet 2
6607
6608 For a non-type template-parameter of type pointer to object,
6609 qualification conversions (_conv.qual_) and the array-to-pointer
6610 conversion (_conv.array_) are applied. */
6611 else if (TYPE_PTROBV_P (type))
6612 {
6613 tree decayed = expr;
6614
6615 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6616 decay_conversion or an explicit cast. If it's a problematic cast,
6617 we'll complain about it below. */
6618 if (TREE_CODE (expr) == NOP_EXPR)
6619 {
6620 tree probe = expr;
6621 STRIP_NOPS (probe);
6622 if (TREE_CODE (probe) == ADDR_EXPR
6623 && TYPE_PTR_P (TREE_TYPE (probe)))
6624 {
6625 expr = probe;
6626 expr_type = TREE_TYPE (expr);
6627 }
6628 }
6629
6630 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6631
6632 A template-argument for a non-type, non-template template-parameter
6633 shall be one of: [...]
6634
6635 -- the name of a non-type template-parameter;
6636 -- the address of an object or function with external linkage, [...]
6637 expressed as "& id-expression" where the & is optional if the name
6638 refers to a function or array, or if the corresponding
6639 template-parameter is a reference.
6640
6641 Here, we do not care about functions, as they are invalid anyway
6642 for a parameter of type pointer-to-object. */
6643
6644 if (value_dependent_expression_p (expr))
6645 /* Non-type template parameters are OK. */
6646 ;
6647 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6648 /* Null pointer values are OK in C++11. */;
6649 else if (TREE_CODE (expr) != ADDR_EXPR)
6650 {
6651 if (VAR_P (expr))
6652 {
6653 if (complain & tf_error)
6654 error ("%qD is not a valid template argument "
6655 "because %qD is a variable, not the address of "
6656 "a variable", orig_expr, expr);
6657 return NULL_TREE;
6658 }
6659 if (POINTER_TYPE_P (expr_type))
6660 {
6661 if (complain & tf_error)
6662 error ("%qE is not a valid template argument for %qT "
6663 "because it is not the address of a variable",
6664 orig_expr, type);
6665 return NULL_TREE;
6666 }
6667 /* Other values, like integer constants, might be valid
6668 non-type arguments of some other type. */
6669 return error_mark_node;
6670 }
6671 else
6672 {
6673 tree decl = TREE_OPERAND (expr, 0);
6674
6675 if (!VAR_P (decl))
6676 {
6677 if (complain & tf_error)
6678 error ("%qE is not a valid template argument of type %qT "
6679 "because %qE is not a variable", orig_expr, type, decl);
6680 return NULL_TREE;
6681 }
6682 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6683 {
6684 if (complain & tf_error)
6685 error ("%qE is not a valid template argument of type %qT "
6686 "because %qD does not have external linkage",
6687 orig_expr, type, decl);
6688 return NULL_TREE;
6689 }
6690 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6691 && decl_linkage (decl) == lk_none)
6692 {
6693 if (complain & tf_error)
6694 error ("%qE is not a valid template argument of type %qT "
6695 "because %qD has no linkage", orig_expr, type, decl);
6696 return NULL_TREE;
6697 }
6698 /* C++17: For a non-type template-parameter of reference or pointer
6699 type, the value of the constant expression shall not refer to (or
6700 for a pointer type, shall not be the address of):
6701 * a subobject (4.5),
6702 * a temporary object (15.2),
6703 * a string literal (5.13.5),
6704 * the result of a typeid expression (8.2.8), or
6705 * a predefined __func__ variable (11.4.1). */
6706 else if (DECL_ARTIFICIAL (decl))
6707 {
6708 if (complain & tf_error)
6709 error ("the address of %qD is not a valid template argument",
6710 decl);
6711 return NULL_TREE;
6712 }
6713 else if (!same_type_ignoring_top_level_qualifiers_p
6714 (strip_array_types (TREE_TYPE (type)),
6715 strip_array_types (TREE_TYPE (decl))))
6716 {
6717 if (complain & tf_error)
6718 error ("the address of the %qT subobject of %qD is not a "
6719 "valid template argument", TREE_TYPE (type), decl);
6720 return NULL_TREE;
6721 }
6722 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6723 {
6724 if (complain & tf_error)
6725 error ("the address of %qD is not a valid template argument "
6726 "because it does not have static storage duration",
6727 decl);
6728 return NULL_TREE;
6729 }
6730 }
6731
6732 expr = decayed;
6733
6734 expr = perform_qualification_conversions (type, expr);
6735 if (expr == error_mark_node)
6736 return error_mark_node;
6737 }
6738 /* [temp.arg.nontype]/5, bullet 3
6739
6740 For a non-type template-parameter of type reference to object, no
6741 conversions apply. The type referred to by the reference may be more
6742 cv-qualified than the (otherwise identical) type of the
6743 template-argument. The template-parameter is bound directly to the
6744 template-argument, which must be an lvalue. */
6745 else if (TYPE_REF_OBJ_P (type))
6746 {
6747 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6748 expr_type))
6749 return error_mark_node;
6750
6751 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6752 {
6753 if (complain & tf_error)
6754 error ("%qE is not a valid template argument for type %qT "
6755 "because of conflicts in cv-qualification", expr, type);
6756 return NULL_TREE;
6757 }
6758
6759 if (!lvalue_p (expr))
6760 {
6761 if (complain & tf_error)
6762 error ("%qE is not a valid template argument for type %qT "
6763 "because it is not an lvalue", expr, type);
6764 return NULL_TREE;
6765 }
6766
6767 /* [temp.arg.nontype]/1
6768
6769 A template-argument for a non-type, non-template template-parameter
6770 shall be one of: [...]
6771
6772 -- the address of an object or function with external linkage. */
6773 if (INDIRECT_REF_P (expr)
6774 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6775 {
6776 expr = TREE_OPERAND (expr, 0);
6777 if (DECL_P (expr))
6778 {
6779 if (complain & tf_error)
6780 error ("%q#D is not a valid template argument for type %qT "
6781 "because a reference variable does not have a constant "
6782 "address", expr, type);
6783 return NULL_TREE;
6784 }
6785 }
6786
6787 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6788 && value_dependent_expression_p (expr))
6789 /* OK, dependent reference. We don't want to ask whether a DECL is
6790 itself value-dependent, since what we want here is its address. */;
6791 else
6792 {
6793 if (!DECL_P (expr))
6794 {
6795 if (complain & tf_error)
6796 error ("%qE is not a valid template argument for type %qT "
6797 "because it is not an object with linkage",
6798 expr, type);
6799 return NULL_TREE;
6800 }
6801
6802 /* DR 1155 allows internal linkage in C++11 and up. */
6803 linkage_kind linkage = decl_linkage (expr);
6804 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6805 {
6806 if (complain & tf_error)
6807 error ("%qE is not a valid template argument for type %qT "
6808 "because object %qD does not have linkage",
6809 expr, type, expr);
6810 return NULL_TREE;
6811 }
6812
6813 expr = build_address (expr);
6814 }
6815
6816 if (!same_type_p (type, TREE_TYPE (expr)))
6817 expr = build_nop (type, expr);
6818 }
6819 /* [temp.arg.nontype]/5, bullet 4
6820
6821 For a non-type template-parameter of type pointer to function, only
6822 the function-to-pointer conversion (_conv.func_) is applied. If the
6823 template-argument represents a set of overloaded functions (or a
6824 pointer to such), the matching function is selected from the set
6825 (_over.over_). */
6826 else if (TYPE_PTRFN_P (type))
6827 {
6828 /* If the argument is a template-id, we might not have enough
6829 context information to decay the pointer. */
6830 if (!type_unknown_p (expr_type))
6831 {
6832 expr = decay_conversion (expr, complain);
6833 if (expr == error_mark_node)
6834 return error_mark_node;
6835 }
6836
6837 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6838 /* Null pointer values are OK in C++11. */
6839 return perform_qualification_conversions (type, expr);
6840
6841 expr = convert_nontype_argument_function (type, expr, complain);
6842 if (!expr || expr == error_mark_node)
6843 return expr;
6844 }
6845 /* [temp.arg.nontype]/5, bullet 5
6846
6847 For a non-type template-parameter of type reference to function, no
6848 conversions apply. If the template-argument represents a set of
6849 overloaded functions, the matching function is selected from the set
6850 (_over.over_). */
6851 else if (TYPE_REFFN_P (type))
6852 {
6853 if (TREE_CODE (expr) == ADDR_EXPR)
6854 {
6855 if (complain & tf_error)
6856 {
6857 error ("%qE is not a valid template argument for type %qT "
6858 "because it is a pointer", expr, type);
6859 inform (input_location, "try using %qE instead",
6860 TREE_OPERAND (expr, 0));
6861 }
6862 return NULL_TREE;
6863 }
6864
6865 expr = convert_nontype_argument_function (type, expr, complain);
6866 if (!expr || expr == error_mark_node)
6867 return expr;
6868 }
6869 /* [temp.arg.nontype]/5, bullet 6
6870
6871 For a non-type template-parameter of type pointer to member function,
6872 no conversions apply. If the template-argument represents a set of
6873 overloaded member functions, the matching member function is selected
6874 from the set (_over.over_). */
6875 else if (TYPE_PTRMEMFUNC_P (type))
6876 {
6877 expr = instantiate_type (type, expr, tf_none);
6878 if (expr == error_mark_node)
6879 return error_mark_node;
6880
6881 /* [temp.arg.nontype] bullet 1 says the pointer to member
6882 expression must be a pointer-to-member constant. */
6883 if (!value_dependent_expression_p (expr)
6884 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6885 return NULL_TREE;
6886
6887 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6888 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6889 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6890 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6891 }
6892 /* [temp.arg.nontype]/5, bullet 7
6893
6894 For a non-type template-parameter of type pointer to data member,
6895 qualification conversions (_conv.qual_) are applied. */
6896 else if (TYPE_PTRDATAMEM_P (type))
6897 {
6898 /* [temp.arg.nontype] bullet 1 says the pointer to member
6899 expression must be a pointer-to-member constant. */
6900 if (!value_dependent_expression_p (expr)
6901 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6902 return NULL_TREE;
6903
6904 expr = perform_qualification_conversions (type, expr);
6905 if (expr == error_mark_node)
6906 return expr;
6907 }
6908 else if (NULLPTR_TYPE_P (type))
6909 {
6910 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6911 {
6912 if (complain & tf_error)
6913 error ("%qE is not a valid template argument for type %qT "
6914 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6915 return NULL_TREE;
6916 }
6917 return expr;
6918 }
6919 /* A template non-type parameter must be one of the above. */
6920 else
6921 gcc_unreachable ();
6922
6923 /* Sanity check: did we actually convert the argument to the
6924 right type? */
6925 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6926 (type, TREE_TYPE (expr)));
6927 return convert_from_reference (expr);
6928 }
6929
6930 /* Subroutine of coerce_template_template_parms, which returns 1 if
6931 PARM_PARM and ARG_PARM match using the rule for the template
6932 parameters of template template parameters. Both PARM and ARG are
6933 template parameters; the rest of the arguments are the same as for
6934 coerce_template_template_parms.
6935 */
6936 static int
6937 coerce_template_template_parm (tree parm,
6938 tree arg,
6939 tsubst_flags_t complain,
6940 tree in_decl,
6941 tree outer_args)
6942 {
6943 if (arg == NULL_TREE || error_operand_p (arg)
6944 || parm == NULL_TREE || error_operand_p (parm))
6945 return 0;
6946
6947 if (TREE_CODE (arg) != TREE_CODE (parm))
6948 return 0;
6949
6950 switch (TREE_CODE (parm))
6951 {
6952 case TEMPLATE_DECL:
6953 /* We encounter instantiations of templates like
6954 template <template <template <class> class> class TT>
6955 class C; */
6956 {
6957 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6958 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6959
6960 if (!coerce_template_template_parms
6961 (parmparm, argparm, complain, in_decl, outer_args))
6962 return 0;
6963 }
6964 /* Fall through. */
6965
6966 case TYPE_DECL:
6967 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6968 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6969 /* Argument is a parameter pack but parameter is not. */
6970 return 0;
6971 break;
6972
6973 case PARM_DECL:
6974 /* The tsubst call is used to handle cases such as
6975
6976 template <int> class C {};
6977 template <class T, template <T> class TT> class D {};
6978 D<int, C> d;
6979
6980 i.e. the parameter list of TT depends on earlier parameters. */
6981 if (!uses_template_parms (TREE_TYPE (arg)))
6982 {
6983 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6984 if (!uses_template_parms (t)
6985 && !same_type_p (t, TREE_TYPE (arg)))
6986 return 0;
6987 }
6988
6989 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6990 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6991 /* Argument is a parameter pack but parameter is not. */
6992 return 0;
6993
6994 break;
6995
6996 default:
6997 gcc_unreachable ();
6998 }
6999
7000 return 1;
7001 }
7002
7003 /* Coerce template argument list ARGLIST for use with template
7004 template-parameter TEMPL. */
7005
7006 static tree
7007 coerce_template_args_for_ttp (tree templ, tree arglist,
7008 tsubst_flags_t complain)
7009 {
7010 /* Consider an example where a template template parameter declared as
7011
7012 template <class T, class U = std::allocator<T> > class TT
7013
7014 The template parameter level of T and U are one level larger than
7015 of TT. To proper process the default argument of U, say when an
7016 instantiation `TT<int>' is seen, we need to build the full
7017 arguments containing {int} as the innermost level. Outer levels,
7018 available when not appearing as default template argument, can be
7019 obtained from the arguments of the enclosing template.
7020
7021 Suppose that TT is later substituted with std::vector. The above
7022 instantiation is `TT<int, std::allocator<T> >' with TT at
7023 level 1, and T at level 2, while the template arguments at level 1
7024 becomes {std::vector} and the inner level 2 is {int}. */
7025
7026 tree outer = DECL_CONTEXT (templ);
7027 if (outer)
7028 {
7029 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7030 /* We want arguments for the partial specialization, not arguments for
7031 the primary template. */
7032 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7033 else
7034 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7035 }
7036 else if (current_template_parms)
7037 {
7038 /* This is an argument of the current template, so we haven't set
7039 DECL_CONTEXT yet. */
7040 tree relevant_template_parms;
7041
7042 /* Parameter levels that are greater than the level of the given
7043 template template parm are irrelevant. */
7044 relevant_template_parms = current_template_parms;
7045 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7046 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7047 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7048
7049 outer = template_parms_to_args (relevant_template_parms);
7050 }
7051
7052 if (outer)
7053 arglist = add_to_template_args (outer, arglist);
7054
7055 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7056 return coerce_template_parms (parmlist, arglist, templ,
7057 complain,
7058 /*require_all_args=*/true,
7059 /*use_default_args=*/true);
7060 }
7061
7062 /* A cache of template template parameters with match-all default
7063 arguments. */
7064 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7065 static void
7066 store_defaulted_ttp (tree v, tree t)
7067 {
7068 if (!defaulted_ttp_cache)
7069 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7070 defaulted_ttp_cache->put (v, t);
7071 }
7072 static tree
7073 lookup_defaulted_ttp (tree v)
7074 {
7075 if (defaulted_ttp_cache)
7076 if (tree *p = defaulted_ttp_cache->get (v))
7077 return *p;
7078 return NULL_TREE;
7079 }
7080
7081 /* T is a bound template template-parameter. Copy its arguments into default
7082 arguments of the template template-parameter's template parameters. */
7083
7084 static tree
7085 add_defaults_to_ttp (tree otmpl)
7086 {
7087 if (tree c = lookup_defaulted_ttp (otmpl))
7088 return c;
7089
7090 tree ntmpl = copy_node (otmpl);
7091
7092 tree ntype = copy_node (TREE_TYPE (otmpl));
7093 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7094 TYPE_MAIN_VARIANT (ntype) = ntype;
7095 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7096 TYPE_NAME (ntype) = ntmpl;
7097 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7098
7099 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7100 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7101 TEMPLATE_PARM_DECL (idx) = ntmpl;
7102 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7103
7104 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7105 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7106 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7107 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7108 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7109 {
7110 tree o = TREE_VEC_ELT (vec, i);
7111 if (!template_parameter_pack_p (TREE_VALUE (o)))
7112 {
7113 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7114 TREE_PURPOSE (n) = any_targ_node;
7115 }
7116 }
7117
7118 store_defaulted_ttp (otmpl, ntmpl);
7119 return ntmpl;
7120 }
7121
7122 /* ARG is a bound potential template template-argument, and PARGS is a list
7123 of arguments for the corresponding template template-parameter. Adjust
7124 PARGS as appropriate for application to ARG's template, and if ARG is a
7125 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7126 arguments to the template template parameter. */
7127
7128 static tree
7129 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7130 {
7131 ++processing_template_decl;
7132 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7133 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7134 {
7135 /* When comparing two template template-parameters in partial ordering,
7136 rewrite the one currently being used as an argument to have default
7137 arguments for all parameters. */
7138 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7139 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7140 if (pargs != error_mark_node)
7141 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7142 TYPE_TI_ARGS (arg));
7143 }
7144 else
7145 {
7146 tree aparms
7147 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7148 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7149 /*require_all*/true,
7150 /*use_default*/true);
7151 }
7152 --processing_template_decl;
7153 return pargs;
7154 }
7155
7156 /* Subroutine of unify for the case when PARM is a
7157 BOUND_TEMPLATE_TEMPLATE_PARM. */
7158
7159 static int
7160 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7161 bool explain_p)
7162 {
7163 tree parmvec = TYPE_TI_ARGS (parm);
7164 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7165
7166 /* The template template parm might be variadic and the argument
7167 not, so flatten both argument lists. */
7168 parmvec = expand_template_argument_pack (parmvec);
7169 argvec = expand_template_argument_pack (argvec);
7170
7171 if (flag_new_ttp)
7172 {
7173 /* In keeping with P0522R0, adjust P's template arguments
7174 to apply to A's template; then flatten it again. */
7175 tree nparmvec = parmvec;
7176 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7177 nparmvec = expand_template_argument_pack (nparmvec);
7178
7179 if (unify (tparms, targs, nparmvec, argvec,
7180 UNIFY_ALLOW_NONE, explain_p))
7181 return 1;
7182
7183 /* If the P0522 adjustment eliminated a pack expansion, deduce
7184 empty packs. */
7185 if (flag_new_ttp
7186 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7187 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7188 DEDUCE_EXACT, /*sub*/true, explain_p))
7189 return 1;
7190 }
7191 else
7192 {
7193 /* Deduce arguments T, i from TT<T> or TT<i>.
7194 We check each element of PARMVEC and ARGVEC individually
7195 rather than the whole TREE_VEC since they can have
7196 different number of elements, which is allowed under N2555. */
7197
7198 int len = TREE_VEC_LENGTH (parmvec);
7199
7200 /* Check if the parameters end in a pack, making them
7201 variadic. */
7202 int parm_variadic_p = 0;
7203 if (len > 0
7204 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7205 parm_variadic_p = 1;
7206
7207 for (int i = 0; i < len - parm_variadic_p; ++i)
7208 /* If the template argument list of P contains a pack
7209 expansion that is not the last template argument, the
7210 entire template argument list is a non-deduced
7211 context. */
7212 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7213 return unify_success (explain_p);
7214
7215 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7216 return unify_too_few_arguments (explain_p,
7217 TREE_VEC_LENGTH (argvec), len);
7218
7219 for (int i = 0; i < len - parm_variadic_p; ++i)
7220 if (unify (tparms, targs,
7221 TREE_VEC_ELT (parmvec, i),
7222 TREE_VEC_ELT (argvec, i),
7223 UNIFY_ALLOW_NONE, explain_p))
7224 return 1;
7225
7226 if (parm_variadic_p
7227 && unify_pack_expansion (tparms, targs,
7228 parmvec, argvec,
7229 DEDUCE_EXACT,
7230 /*subr=*/true, explain_p))
7231 return 1;
7232 }
7233
7234 return 0;
7235 }
7236
7237 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7238 template template parameters. Both PARM_PARMS and ARG_PARMS are
7239 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7240 or PARM_DECL.
7241
7242 Consider the example:
7243 template <class T> class A;
7244 template<template <class U> class TT> class B;
7245
7246 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7247 the parameters to A, and OUTER_ARGS contains A. */
7248
7249 static int
7250 coerce_template_template_parms (tree parm_parms,
7251 tree arg_parms,
7252 tsubst_flags_t complain,
7253 tree in_decl,
7254 tree outer_args)
7255 {
7256 int nparms, nargs, i;
7257 tree parm, arg;
7258 int variadic_p = 0;
7259
7260 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7261 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7262
7263 nparms = TREE_VEC_LENGTH (parm_parms);
7264 nargs = TREE_VEC_LENGTH (arg_parms);
7265
7266 if (flag_new_ttp)
7267 {
7268 /* P0522R0: A template template-parameter P is at least as specialized as
7269 a template template-argument A if, given the following rewrite to two
7270 function templates, the function template corresponding to P is at
7271 least as specialized as the function template corresponding to A
7272 according to the partial ordering rules for function templates
7273 ([temp.func.order]). Given an invented class template X with the
7274 template parameter list of A (including default arguments):
7275
7276 * Each of the two function templates has the same template parameters,
7277 respectively, as P or A.
7278
7279 * Each function template has a single function parameter whose type is
7280 a specialization of X with template arguments corresponding to the
7281 template parameters from the respective function template where, for
7282 each template parameter PP in the template parameter list of the
7283 function template, a corresponding template argument AA is formed. If
7284 PP declares a parameter pack, then AA is the pack expansion
7285 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7286
7287 If the rewrite produces an invalid type, then P is not at least as
7288 specialized as A. */
7289
7290 /* So coerce P's args to apply to A's parms, and then deduce between A's
7291 args and the converted args. If that succeeds, A is at least as
7292 specialized as P, so they match.*/
7293 tree pargs = template_parms_level_to_args (parm_parms);
7294 ++processing_template_decl;
7295 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7296 /*require_all*/true, /*use_default*/true);
7297 --processing_template_decl;
7298 if (pargs != error_mark_node)
7299 {
7300 tree targs = make_tree_vec (nargs);
7301 tree aargs = template_parms_level_to_args (arg_parms);
7302 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7303 /*explain*/false))
7304 return 1;
7305 }
7306 }
7307
7308 /* Determine whether we have a parameter pack at the end of the
7309 template template parameter's template parameter list. */
7310 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7311 {
7312 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7313
7314 if (error_operand_p (parm))
7315 return 0;
7316
7317 switch (TREE_CODE (parm))
7318 {
7319 case TEMPLATE_DECL:
7320 case TYPE_DECL:
7321 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7322 variadic_p = 1;
7323 break;
7324
7325 case PARM_DECL:
7326 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7327 variadic_p = 1;
7328 break;
7329
7330 default:
7331 gcc_unreachable ();
7332 }
7333 }
7334
7335 if (nargs != nparms
7336 && !(variadic_p && nargs >= nparms - 1))
7337 return 0;
7338
7339 /* Check all of the template parameters except the parameter pack at
7340 the end (if any). */
7341 for (i = 0; i < nparms - variadic_p; ++i)
7342 {
7343 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7344 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7345 continue;
7346
7347 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7348 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7349
7350 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7351 outer_args))
7352 return 0;
7353
7354 }
7355
7356 if (variadic_p)
7357 {
7358 /* Check each of the template parameters in the template
7359 argument against the template parameter pack at the end of
7360 the template template parameter. */
7361 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7362 return 0;
7363
7364 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7365
7366 for (; i < nargs; ++i)
7367 {
7368 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7369 continue;
7370
7371 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7372
7373 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7374 outer_args))
7375 return 0;
7376 }
7377 }
7378
7379 return 1;
7380 }
7381
7382 /* Verifies that the deduced template arguments (in TARGS) for the
7383 template template parameters (in TPARMS) represent valid bindings,
7384 by comparing the template parameter list of each template argument
7385 to the template parameter list of its corresponding template
7386 template parameter, in accordance with DR150. This
7387 routine can only be called after all template arguments have been
7388 deduced. It will return TRUE if all of the template template
7389 parameter bindings are okay, FALSE otherwise. */
7390 bool
7391 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7392 {
7393 int i, ntparms = TREE_VEC_LENGTH (tparms);
7394 bool ret = true;
7395
7396 /* We're dealing with template parms in this process. */
7397 ++processing_template_decl;
7398
7399 targs = INNERMOST_TEMPLATE_ARGS (targs);
7400
7401 for (i = 0; i < ntparms; ++i)
7402 {
7403 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7404 tree targ = TREE_VEC_ELT (targs, i);
7405
7406 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7407 {
7408 tree packed_args = NULL_TREE;
7409 int idx, len = 1;
7410
7411 if (ARGUMENT_PACK_P (targ))
7412 {
7413 /* Look inside the argument pack. */
7414 packed_args = ARGUMENT_PACK_ARGS (targ);
7415 len = TREE_VEC_LENGTH (packed_args);
7416 }
7417
7418 for (idx = 0; idx < len; ++idx)
7419 {
7420 tree targ_parms = NULL_TREE;
7421
7422 if (packed_args)
7423 /* Extract the next argument from the argument
7424 pack. */
7425 targ = TREE_VEC_ELT (packed_args, idx);
7426
7427 if (PACK_EXPANSION_P (targ))
7428 /* Look at the pattern of the pack expansion. */
7429 targ = PACK_EXPANSION_PATTERN (targ);
7430
7431 /* Extract the template parameters from the template
7432 argument. */
7433 if (TREE_CODE (targ) == TEMPLATE_DECL)
7434 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7435 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7436 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7437
7438 /* Verify that we can coerce the template template
7439 parameters from the template argument to the template
7440 parameter. This requires an exact match. */
7441 if (targ_parms
7442 && !coerce_template_template_parms
7443 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7444 targ_parms,
7445 tf_none,
7446 tparm,
7447 targs))
7448 {
7449 ret = false;
7450 goto out;
7451 }
7452 }
7453 }
7454 }
7455
7456 out:
7457
7458 --processing_template_decl;
7459 return ret;
7460 }
7461
7462 /* Since type attributes aren't mangled, we need to strip them from
7463 template type arguments. */
7464
7465 static tree
7466 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7467 {
7468 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7469 return arg;
7470 bool removed_attributes = false;
7471 tree canon = strip_typedefs (arg, &removed_attributes);
7472 if (removed_attributes
7473 && (complain & tf_warning))
7474 warning (OPT_Wignored_attributes,
7475 "ignoring attributes on template argument %qT", arg);
7476 return canon;
7477 }
7478
7479 /* And from inside dependent non-type arguments like sizeof(Type). */
7480
7481 static tree
7482 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7483 {
7484 if (!arg || arg == error_mark_node)
7485 return arg;
7486 bool removed_attributes = false;
7487 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7488 if (removed_attributes
7489 && (complain & tf_warning))
7490 warning (OPT_Wignored_attributes,
7491 "ignoring attributes in template argument %qE", arg);
7492 return canon;
7493 }
7494
7495 // A template declaration can be substituted for a constrained
7496 // template template parameter only when the argument is more
7497 // constrained than the parameter.
7498 static bool
7499 is_compatible_template_arg (tree parm, tree arg)
7500 {
7501 tree parm_cons = get_constraints (parm);
7502
7503 /* For now, allow constrained template template arguments
7504 and unconstrained template template parameters. */
7505 if (parm_cons == NULL_TREE)
7506 return true;
7507
7508 tree arg_cons = get_constraints (arg);
7509
7510 // If the template parameter is constrained, we need to rewrite its
7511 // constraints in terms of the ARG's template parameters. This ensures
7512 // that all of the template parameter types will have the same depth.
7513 //
7514 // Note that this is only valid when coerce_template_template_parm is
7515 // true for the innermost template parameters of PARM and ARG. In other
7516 // words, because coercion is successful, this conversion will be valid.
7517 if (parm_cons)
7518 {
7519 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7520 parm_cons = tsubst_constraint_info (parm_cons,
7521 INNERMOST_TEMPLATE_ARGS (args),
7522 tf_none, NULL_TREE);
7523 if (parm_cons == error_mark_node)
7524 return false;
7525 }
7526
7527 return subsumes (parm_cons, arg_cons);
7528 }
7529
7530 // Convert a placeholder argument into a binding to the original
7531 // parameter. The original parameter is saved as the TREE_TYPE of
7532 // ARG.
7533 static inline tree
7534 convert_wildcard_argument (tree parm, tree arg)
7535 {
7536 TREE_TYPE (arg) = parm;
7537 return arg;
7538 }
7539
7540 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7541 because one of them is dependent. But we need to represent the
7542 conversion for the benefit of cp_tree_equal. */
7543
7544 static tree
7545 maybe_convert_nontype_argument (tree type, tree arg)
7546 {
7547 /* Auto parms get no conversion. */
7548 if (type_uses_auto (type))
7549 return arg;
7550 /* We don't need or want to add this conversion now if we're going to use the
7551 argument for deduction. */
7552 if (value_dependent_expression_p (arg))
7553 return arg;
7554
7555 type = cv_unqualified (type);
7556 tree argtype = TREE_TYPE (arg);
7557 if (same_type_p (type, argtype))
7558 return arg;
7559
7560 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7561 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7562 return arg;
7563 }
7564
7565 /* Convert the indicated template ARG as necessary to match the
7566 indicated template PARM. Returns the converted ARG, or
7567 error_mark_node if the conversion was unsuccessful. Error and
7568 warning messages are issued under control of COMPLAIN. This
7569 conversion is for the Ith parameter in the parameter list. ARGS is
7570 the full set of template arguments deduced so far. */
7571
7572 static tree
7573 convert_template_argument (tree parm,
7574 tree arg,
7575 tree args,
7576 tsubst_flags_t complain,
7577 int i,
7578 tree in_decl)
7579 {
7580 tree orig_arg;
7581 tree val;
7582 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7583
7584 if (parm == error_mark_node)
7585 return error_mark_node;
7586
7587 /* Trivially convert placeholders. */
7588 if (TREE_CODE (arg) == WILDCARD_DECL)
7589 return convert_wildcard_argument (parm, arg);
7590
7591 if (arg == any_targ_node)
7592 return arg;
7593
7594 if (TREE_CODE (arg) == TREE_LIST
7595 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7596 {
7597 /* The template argument was the name of some
7598 member function. That's usually
7599 invalid, but static members are OK. In any
7600 case, grab the underlying fields/functions
7601 and issue an error later if required. */
7602 orig_arg = TREE_VALUE (arg);
7603 TREE_TYPE (arg) = unknown_type_node;
7604 }
7605
7606 orig_arg = arg;
7607
7608 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7609 requires_type = (TREE_CODE (parm) == TYPE_DECL
7610 || requires_tmpl_type);
7611
7612 /* When determining whether an argument pack expansion is a template,
7613 look at the pattern. */
7614 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7615 arg = PACK_EXPANSION_PATTERN (arg);
7616
7617 /* Deal with an injected-class-name used as a template template arg. */
7618 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7619 {
7620 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7621 if (TREE_CODE (t) == TEMPLATE_DECL)
7622 {
7623 if (cxx_dialect >= cxx11)
7624 /* OK under DR 1004. */;
7625 else if (complain & tf_warning_or_error)
7626 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7627 " used as template template argument", TYPE_NAME (arg));
7628 else if (flag_pedantic_errors)
7629 t = arg;
7630
7631 arg = t;
7632 }
7633 }
7634
7635 is_tmpl_type =
7636 ((TREE_CODE (arg) == TEMPLATE_DECL
7637 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7638 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7639 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7640 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7641
7642 if (is_tmpl_type
7643 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7644 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7645 arg = TYPE_STUB_DECL (arg);
7646
7647 is_type = TYPE_P (arg) || is_tmpl_type;
7648
7649 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7650 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7651 {
7652 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7653 {
7654 if (complain & tf_error)
7655 error ("invalid use of destructor %qE as a type", orig_arg);
7656 return error_mark_node;
7657 }
7658
7659 permerror (input_location,
7660 "to refer to a type member of a template parameter, "
7661 "use %<typename %E%>", orig_arg);
7662
7663 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7664 TREE_OPERAND (arg, 1),
7665 typename_type,
7666 complain);
7667 arg = orig_arg;
7668 is_type = 1;
7669 }
7670 if (is_type != requires_type)
7671 {
7672 if (in_decl)
7673 {
7674 if (complain & tf_error)
7675 {
7676 error ("type/value mismatch at argument %d in template "
7677 "parameter list for %qD",
7678 i + 1, in_decl);
7679 if (is_type)
7680 inform (input_location,
7681 " expected a constant of type %qT, got %qT",
7682 TREE_TYPE (parm),
7683 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7684 else if (requires_tmpl_type)
7685 inform (input_location,
7686 " expected a class template, got %qE", orig_arg);
7687 else
7688 inform (input_location,
7689 " expected a type, got %qE", orig_arg);
7690 }
7691 }
7692 return error_mark_node;
7693 }
7694 if (is_tmpl_type ^ requires_tmpl_type)
7695 {
7696 if (in_decl && (complain & tf_error))
7697 {
7698 error ("type/value mismatch at argument %d in template "
7699 "parameter list for %qD",
7700 i + 1, in_decl);
7701 if (is_tmpl_type)
7702 inform (input_location,
7703 " expected a type, got %qT", DECL_NAME (arg));
7704 else
7705 inform (input_location,
7706 " expected a class template, got %qT", orig_arg);
7707 }
7708 return error_mark_node;
7709 }
7710
7711 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7712 /* We already did the appropriate conversion when packing args. */
7713 val = orig_arg;
7714 else if (is_type)
7715 {
7716 if (requires_tmpl_type)
7717 {
7718 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7719 /* The number of argument required is not known yet.
7720 Just accept it for now. */
7721 val = orig_arg;
7722 else
7723 {
7724 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7725 tree argparm;
7726
7727 /* Strip alias templates that are equivalent to another
7728 template. */
7729 arg = get_underlying_template (arg);
7730 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7731
7732 if (coerce_template_template_parms (parmparm, argparm,
7733 complain, in_decl,
7734 args))
7735 {
7736 val = arg;
7737
7738 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7739 TEMPLATE_DECL. */
7740 if (val != error_mark_node)
7741 {
7742 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7743 val = TREE_TYPE (val);
7744 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7745 val = make_pack_expansion (val, complain);
7746 }
7747 }
7748 else
7749 {
7750 if (in_decl && (complain & tf_error))
7751 {
7752 error ("type/value mismatch at argument %d in "
7753 "template parameter list for %qD",
7754 i + 1, in_decl);
7755 inform (input_location,
7756 " expected a template of type %qD, got %qT",
7757 parm, orig_arg);
7758 }
7759
7760 val = error_mark_node;
7761 }
7762
7763 // Check that the constraints are compatible before allowing the
7764 // substitution.
7765 if (val != error_mark_node)
7766 if (!is_compatible_template_arg (parm, arg))
7767 {
7768 if (in_decl && (complain & tf_error))
7769 {
7770 error ("constraint mismatch at argument %d in "
7771 "template parameter list for %qD",
7772 i + 1, in_decl);
7773 inform (input_location, " expected %qD but got %qD",
7774 parm, arg);
7775 }
7776 val = error_mark_node;
7777 }
7778 }
7779 }
7780 else
7781 val = orig_arg;
7782 /* We only form one instance of each template specialization.
7783 Therefore, if we use a non-canonical variant (i.e., a
7784 typedef), any future messages referring to the type will use
7785 the typedef, which is confusing if those future uses do not
7786 themselves also use the typedef. */
7787 if (TYPE_P (val))
7788 val = canonicalize_type_argument (val, complain);
7789 }
7790 else
7791 {
7792 tree t = TREE_TYPE (parm);
7793
7794 if (tree a = type_uses_auto (t))
7795 {
7796 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7797 if (t == error_mark_node)
7798 return error_mark_node;
7799 }
7800 else
7801 t = tsubst (t, args, complain, in_decl);
7802
7803 if (invalid_nontype_parm_type_p (t, complain))
7804 return error_mark_node;
7805
7806 if (!type_dependent_expression_p (orig_arg)
7807 && !uses_template_parms (t))
7808 /* We used to call digest_init here. However, digest_init
7809 will report errors, which we don't want when complain
7810 is zero. More importantly, digest_init will try too
7811 hard to convert things: for example, `0' should not be
7812 converted to pointer type at this point according to
7813 the standard. Accepting this is not merely an
7814 extension, since deciding whether or not these
7815 conversions can occur is part of determining which
7816 function template to call, or whether a given explicit
7817 argument specification is valid. */
7818 val = convert_nontype_argument (t, orig_arg, complain);
7819 else
7820 {
7821 val = canonicalize_expr_argument (orig_arg, complain);
7822 val = maybe_convert_nontype_argument (t, val);
7823 }
7824
7825
7826 if (val == NULL_TREE)
7827 val = error_mark_node;
7828 else if (val == error_mark_node && (complain & tf_error))
7829 error ("could not convert template argument %qE from %qT to %qT",
7830 orig_arg, TREE_TYPE (orig_arg), t);
7831
7832 if (INDIRECT_REF_P (val))
7833 {
7834 /* Reject template arguments that are references to built-in
7835 functions with no library fallbacks. */
7836 const_tree inner = TREE_OPERAND (val, 0);
7837 const_tree innertype = TREE_TYPE (inner);
7838 if (innertype
7839 && TREE_CODE (innertype) == REFERENCE_TYPE
7840 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7841 && TREE_OPERAND_LENGTH (inner) > 0
7842 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7843 return error_mark_node;
7844 }
7845
7846 if (TREE_CODE (val) == SCOPE_REF)
7847 {
7848 /* Strip typedefs from the SCOPE_REF. */
7849 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7850 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7851 complain);
7852 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7853 QUALIFIED_NAME_IS_TEMPLATE (val));
7854 }
7855 }
7856
7857 return val;
7858 }
7859
7860 /* Coerces the remaining template arguments in INNER_ARGS (from
7861 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7862 Returns the coerced argument pack. PARM_IDX is the position of this
7863 parameter in the template parameter list. ARGS is the original
7864 template argument list. */
7865 static tree
7866 coerce_template_parameter_pack (tree parms,
7867 int parm_idx,
7868 tree args,
7869 tree inner_args,
7870 int arg_idx,
7871 tree new_args,
7872 int* lost,
7873 tree in_decl,
7874 tsubst_flags_t complain)
7875 {
7876 tree parm = TREE_VEC_ELT (parms, parm_idx);
7877 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7878 tree packed_args;
7879 tree argument_pack;
7880 tree packed_parms = NULL_TREE;
7881
7882 if (arg_idx > nargs)
7883 arg_idx = nargs;
7884
7885 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7886 {
7887 /* When the template parameter is a non-type template parameter pack
7888 or template template parameter pack whose type or template
7889 parameters use parameter packs, we know exactly how many arguments
7890 we are looking for. Build a vector of the instantiated decls for
7891 these template parameters in PACKED_PARMS. */
7892 /* We can't use make_pack_expansion here because it would interpret a
7893 _DECL as a use rather than a declaration. */
7894 tree decl = TREE_VALUE (parm);
7895 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7896 SET_PACK_EXPANSION_PATTERN (exp, decl);
7897 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7898 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7899
7900 TREE_VEC_LENGTH (args)--;
7901 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7902 TREE_VEC_LENGTH (args)++;
7903
7904 if (packed_parms == error_mark_node)
7905 return error_mark_node;
7906
7907 /* If we're doing a partial instantiation of a member template,
7908 verify that all of the types used for the non-type
7909 template parameter pack are, in fact, valid for non-type
7910 template parameters. */
7911 if (arg_idx < nargs
7912 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7913 {
7914 int j, len = TREE_VEC_LENGTH (packed_parms);
7915 for (j = 0; j < len; ++j)
7916 {
7917 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7918 if (invalid_nontype_parm_type_p (t, complain))
7919 return error_mark_node;
7920 }
7921 /* We don't know how many args we have yet, just
7922 use the unconverted ones for now. */
7923 return NULL_TREE;
7924 }
7925
7926 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7927 }
7928 /* Check if we have a placeholder pack, which indicates we're
7929 in the context of a introduction list. In that case we want
7930 to match this pack to the single placeholder. */
7931 else if (arg_idx < nargs
7932 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7933 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7934 {
7935 nargs = arg_idx + 1;
7936 packed_args = make_tree_vec (1);
7937 }
7938 else
7939 packed_args = make_tree_vec (nargs - arg_idx);
7940
7941 /* Convert the remaining arguments, which will be a part of the
7942 parameter pack "parm". */
7943 int first_pack_arg = arg_idx;
7944 for (; arg_idx < nargs; ++arg_idx)
7945 {
7946 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7947 tree actual_parm = TREE_VALUE (parm);
7948 int pack_idx = arg_idx - first_pack_arg;
7949
7950 if (packed_parms)
7951 {
7952 /* Once we've packed as many args as we have types, stop. */
7953 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7954 break;
7955 else if (PACK_EXPANSION_P (arg))
7956 /* We don't know how many args we have yet, just
7957 use the unconverted ones for now. */
7958 return NULL_TREE;
7959 else
7960 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7961 }
7962
7963 if (arg == error_mark_node)
7964 {
7965 if (complain & tf_error)
7966 error ("template argument %d is invalid", arg_idx + 1);
7967 }
7968 else
7969 arg = convert_template_argument (actual_parm,
7970 arg, new_args, complain, parm_idx,
7971 in_decl);
7972 if (arg == error_mark_node)
7973 (*lost)++;
7974 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7975 }
7976
7977 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7978 && TREE_VEC_LENGTH (packed_args) > 0)
7979 {
7980 if (complain & tf_error)
7981 error ("wrong number of template arguments (%d, should be %d)",
7982 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7983 return error_mark_node;
7984 }
7985
7986 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7987 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7988 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7989 else
7990 {
7991 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7992 TREE_CONSTANT (argument_pack) = 1;
7993 }
7994
7995 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7996 if (CHECKING_P)
7997 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7998 TREE_VEC_LENGTH (packed_args));
7999 return argument_pack;
8000 }
8001
8002 /* Returns the number of pack expansions in the template argument vector
8003 ARGS. */
8004
8005 static int
8006 pack_expansion_args_count (tree args)
8007 {
8008 int i;
8009 int count = 0;
8010 if (args)
8011 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8012 {
8013 tree elt = TREE_VEC_ELT (args, i);
8014 if (elt && PACK_EXPANSION_P (elt))
8015 ++count;
8016 }
8017 return count;
8018 }
8019
8020 /* Convert all template arguments to their appropriate types, and
8021 return a vector containing the innermost resulting template
8022 arguments. If any error occurs, return error_mark_node. Error and
8023 warning messages are issued under control of COMPLAIN.
8024
8025 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8026 for arguments not specified in ARGS. Otherwise, if
8027 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8028 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8029 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8030 ARGS. */
8031
8032 static tree
8033 coerce_template_parms (tree parms,
8034 tree args,
8035 tree in_decl,
8036 tsubst_flags_t complain,
8037 bool require_all_args,
8038 bool use_default_args)
8039 {
8040 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8041 tree orig_inner_args;
8042 tree inner_args;
8043 tree new_args;
8044 tree new_inner_args;
8045 int saved_unevaluated_operand;
8046 int saved_inhibit_evaluation_warnings;
8047
8048 /* When used as a boolean value, indicates whether this is a
8049 variadic template parameter list. Since it's an int, we can also
8050 subtract it from nparms to get the number of non-variadic
8051 parameters. */
8052 int variadic_p = 0;
8053 int variadic_args_p = 0;
8054 int post_variadic_parms = 0;
8055
8056 /* Likewise for parameters with default arguments. */
8057 int default_p = 0;
8058
8059 if (args == error_mark_node)
8060 return error_mark_node;
8061
8062 nparms = TREE_VEC_LENGTH (parms);
8063
8064 /* Determine if there are any parameter packs or default arguments. */
8065 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8066 {
8067 tree parm = TREE_VEC_ELT (parms, parm_idx);
8068 if (variadic_p)
8069 ++post_variadic_parms;
8070 if (template_parameter_pack_p (TREE_VALUE (parm)))
8071 ++variadic_p;
8072 if (TREE_PURPOSE (parm))
8073 ++default_p;
8074 }
8075
8076 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8077 /* If there are no parameters that follow a parameter pack, we need to
8078 expand any argument packs so that we can deduce a parameter pack from
8079 some non-packed args followed by an argument pack, as in variadic85.C.
8080 If there are such parameters, we need to leave argument packs intact
8081 so the arguments are assigned properly. This can happen when dealing
8082 with a nested class inside a partial specialization of a class
8083 template, as in variadic92.C, or when deducing a template parameter pack
8084 from a sub-declarator, as in variadic114.C. */
8085 if (!post_variadic_parms)
8086 inner_args = expand_template_argument_pack (inner_args);
8087
8088 /* Count any pack expansion args. */
8089 variadic_args_p = pack_expansion_args_count (inner_args);
8090
8091 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8092 if ((nargs - variadic_args_p > nparms && !variadic_p)
8093 || (nargs < nparms - variadic_p
8094 && require_all_args
8095 && !variadic_args_p
8096 && (!use_default_args
8097 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8098 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8099 {
8100 if (complain & tf_error)
8101 {
8102 if (variadic_p || default_p)
8103 {
8104 nparms -= variadic_p + default_p;
8105 error ("wrong number of template arguments "
8106 "(%d, should be at least %d)", nargs, nparms);
8107 }
8108 else
8109 error ("wrong number of template arguments "
8110 "(%d, should be %d)", nargs, nparms);
8111
8112 if (in_decl)
8113 inform (DECL_SOURCE_LOCATION (in_decl),
8114 "provided for %qD", in_decl);
8115 }
8116
8117 return error_mark_node;
8118 }
8119 /* We can't pass a pack expansion to a non-pack parameter of an alias
8120 template (DR 1430). */
8121 else if (in_decl
8122 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8123 || concept_template_p (in_decl))
8124 && variadic_args_p
8125 && nargs - variadic_args_p < nparms - variadic_p)
8126 {
8127 if (complain & tf_error)
8128 {
8129 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8130 {
8131 tree arg = TREE_VEC_ELT (inner_args, i);
8132 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8133
8134 if (PACK_EXPANSION_P (arg)
8135 && !template_parameter_pack_p (parm))
8136 {
8137 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8138 error_at (location_of (arg),
8139 "pack expansion argument for non-pack parameter "
8140 "%qD of alias template %qD", parm, in_decl);
8141 else
8142 error_at (location_of (arg),
8143 "pack expansion argument for non-pack parameter "
8144 "%qD of concept %qD", parm, in_decl);
8145 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8146 goto found;
8147 }
8148 }
8149 gcc_unreachable ();
8150 found:;
8151 }
8152 return error_mark_node;
8153 }
8154
8155 /* We need to evaluate the template arguments, even though this
8156 template-id may be nested within a "sizeof". */
8157 saved_unevaluated_operand = cp_unevaluated_operand;
8158 cp_unevaluated_operand = 0;
8159 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8160 c_inhibit_evaluation_warnings = 0;
8161 new_inner_args = make_tree_vec (nparms);
8162 new_args = add_outermost_template_args (args, new_inner_args);
8163 int pack_adjust = 0;
8164 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8165 {
8166 tree arg;
8167 tree parm;
8168
8169 /* Get the Ith template parameter. */
8170 parm = TREE_VEC_ELT (parms, parm_idx);
8171
8172 if (parm == error_mark_node)
8173 {
8174 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8175 continue;
8176 }
8177
8178 /* Calculate the next argument. */
8179 if (arg_idx < nargs)
8180 arg = TREE_VEC_ELT (inner_args, arg_idx);
8181 else
8182 arg = NULL_TREE;
8183
8184 if (template_parameter_pack_p (TREE_VALUE (parm))
8185 && !(arg && ARGUMENT_PACK_P (arg)))
8186 {
8187 /* Some arguments will be placed in the
8188 template parameter pack PARM. */
8189 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8190 inner_args, arg_idx,
8191 new_args, &lost,
8192 in_decl, complain);
8193
8194 if (arg == NULL_TREE)
8195 {
8196 /* We don't know how many args we have yet, just use the
8197 unconverted (and still packed) ones for now. */
8198 new_inner_args = orig_inner_args;
8199 arg_idx = nargs;
8200 break;
8201 }
8202
8203 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8204
8205 /* Store this argument. */
8206 if (arg == error_mark_node)
8207 {
8208 lost++;
8209 /* We are done with all of the arguments. */
8210 arg_idx = nargs;
8211 }
8212 else
8213 {
8214 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8215 arg_idx += pack_adjust;
8216 }
8217
8218 continue;
8219 }
8220 else if (arg)
8221 {
8222 if (PACK_EXPANSION_P (arg))
8223 {
8224 /* "If every valid specialization of a variadic template
8225 requires an empty template parameter pack, the template is
8226 ill-formed, no diagnostic required." So check that the
8227 pattern works with this parameter. */
8228 tree pattern = PACK_EXPANSION_PATTERN (arg);
8229 tree conv = convert_template_argument (TREE_VALUE (parm),
8230 pattern, new_args,
8231 complain, parm_idx,
8232 in_decl);
8233 if (conv == error_mark_node)
8234 {
8235 if (complain & tf_error)
8236 inform (input_location, "so any instantiation with a "
8237 "non-empty parameter pack would be ill-formed");
8238 ++lost;
8239 }
8240 else if (TYPE_P (conv) && !TYPE_P (pattern))
8241 /* Recover from missing typename. */
8242 TREE_VEC_ELT (inner_args, arg_idx)
8243 = make_pack_expansion (conv, complain);
8244
8245 /* We don't know how many args we have yet, just
8246 use the unconverted ones for now. */
8247 new_inner_args = inner_args;
8248 arg_idx = nargs;
8249 break;
8250 }
8251 }
8252 else if (require_all_args)
8253 {
8254 /* There must be a default arg in this case. */
8255 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8256 complain, in_decl);
8257 /* The position of the first default template argument,
8258 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8259 Record that. */
8260 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8261 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8262 arg_idx - pack_adjust);
8263 }
8264 else
8265 break;
8266
8267 if (arg == error_mark_node)
8268 {
8269 if (complain & tf_error)
8270 error ("template argument %d is invalid", arg_idx + 1);
8271 }
8272 else if (!arg)
8273 /* This only occurs if there was an error in the template
8274 parameter list itself (which we would already have
8275 reported) that we are trying to recover from, e.g., a class
8276 template with a parameter list such as
8277 template<typename..., typename>. */
8278 ++lost;
8279 else
8280 arg = convert_template_argument (TREE_VALUE (parm),
8281 arg, new_args, complain,
8282 parm_idx, in_decl);
8283
8284 if (arg == error_mark_node)
8285 lost++;
8286 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8287 }
8288 cp_unevaluated_operand = saved_unevaluated_operand;
8289 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8290
8291 if (variadic_p && arg_idx < nargs)
8292 {
8293 if (complain & tf_error)
8294 {
8295 error ("wrong number of template arguments "
8296 "(%d, should be %d)", nargs, arg_idx);
8297 if (in_decl)
8298 error ("provided for %q+D", in_decl);
8299 }
8300 return error_mark_node;
8301 }
8302
8303 if (lost)
8304 return error_mark_node;
8305
8306 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8307 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8308 TREE_VEC_LENGTH (new_inner_args));
8309
8310 return new_inner_args;
8311 }
8312
8313 /* Convert all template arguments to their appropriate types, and
8314 return a vector containing the innermost resulting template
8315 arguments. If any error occurs, return error_mark_node. Error and
8316 warning messages are not issued.
8317
8318 Note that no function argument deduction is performed, and default
8319 arguments are used to fill in unspecified arguments. */
8320 tree
8321 coerce_template_parms (tree parms, tree args, tree in_decl)
8322 {
8323 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8324 }
8325
8326 /* Convert all template arguments to their appropriate type, and
8327 instantiate default arguments as needed. This returns a vector
8328 containing the innermost resulting template arguments, or
8329 error_mark_node if unsuccessful. */
8330 tree
8331 coerce_template_parms (tree parms, tree args, tree in_decl,
8332 tsubst_flags_t complain)
8333 {
8334 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8335 }
8336
8337 /* Like coerce_template_parms. If PARMS represents all template
8338 parameters levels, this function returns a vector of vectors
8339 representing all the resulting argument levels. Note that in this
8340 case, only the innermost arguments are coerced because the
8341 outermost ones are supposed to have been coerced already.
8342
8343 Otherwise, if PARMS represents only (the innermost) vector of
8344 parameters, this function returns a vector containing just the
8345 innermost resulting arguments. */
8346
8347 static tree
8348 coerce_innermost_template_parms (tree parms,
8349 tree args,
8350 tree in_decl,
8351 tsubst_flags_t complain,
8352 bool require_all_args,
8353 bool use_default_args)
8354 {
8355 int parms_depth = TMPL_PARMS_DEPTH (parms);
8356 int args_depth = TMPL_ARGS_DEPTH (args);
8357 tree coerced_args;
8358
8359 if (parms_depth > 1)
8360 {
8361 coerced_args = make_tree_vec (parms_depth);
8362 tree level;
8363 int cur_depth;
8364
8365 for (level = parms, cur_depth = parms_depth;
8366 parms_depth > 0 && level != NULL_TREE;
8367 level = TREE_CHAIN (level), --cur_depth)
8368 {
8369 tree l;
8370 if (cur_depth == args_depth)
8371 l = coerce_template_parms (TREE_VALUE (level),
8372 args, in_decl, complain,
8373 require_all_args,
8374 use_default_args);
8375 else
8376 l = TMPL_ARGS_LEVEL (args, cur_depth);
8377
8378 if (l == error_mark_node)
8379 return error_mark_node;
8380
8381 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8382 }
8383 }
8384 else
8385 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8386 args, in_decl, complain,
8387 require_all_args,
8388 use_default_args);
8389 return coerced_args;
8390 }
8391
8392 /* Returns 1 if template args OT and NT are equivalent. */
8393
8394 int
8395 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8396 {
8397 if (nt == ot)
8398 return 1;
8399 if (nt == NULL_TREE || ot == NULL_TREE)
8400 return false;
8401 if (nt == any_targ_node || ot == any_targ_node)
8402 return true;
8403
8404 if (TREE_CODE (nt) == TREE_VEC)
8405 /* For member templates */
8406 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8407 else if (PACK_EXPANSION_P (ot))
8408 return (PACK_EXPANSION_P (nt)
8409 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8410 PACK_EXPANSION_PATTERN (nt))
8411 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8412 PACK_EXPANSION_EXTRA_ARGS (nt)));
8413 else if (ARGUMENT_PACK_P (ot))
8414 {
8415 int i, len;
8416 tree opack, npack;
8417
8418 if (!ARGUMENT_PACK_P (nt))
8419 return 0;
8420
8421 opack = ARGUMENT_PACK_ARGS (ot);
8422 npack = ARGUMENT_PACK_ARGS (nt);
8423 len = TREE_VEC_LENGTH (opack);
8424 if (TREE_VEC_LENGTH (npack) != len)
8425 return 0;
8426 for (i = 0; i < len; ++i)
8427 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8428 TREE_VEC_ELT (npack, i)))
8429 return 0;
8430 return 1;
8431 }
8432 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8433 gcc_unreachable ();
8434 else if (TYPE_P (nt))
8435 {
8436 if (!TYPE_P (ot))
8437 return false;
8438 /* Don't treat an alias template specialization with dependent
8439 arguments as equivalent to its underlying type when used as a
8440 template argument; we need them to be distinct so that we
8441 substitute into the specialization arguments at instantiation
8442 time. And aliases can't be equivalent without being ==, so
8443 we don't need to look any deeper.
8444
8445 During partial ordering, however, we need to treat them normally so
8446 that we can order uses of the same alias with different
8447 cv-qualification (79960). */
8448 if (!partial_order
8449 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8450 return false;
8451 else
8452 return same_type_p (ot, nt);
8453 }
8454 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8455 return 0;
8456 else
8457 {
8458 /* Try to treat a template non-type argument that has been converted
8459 to the parameter type as equivalent to one that hasn't yet. */
8460 for (enum tree_code code1 = TREE_CODE (ot);
8461 CONVERT_EXPR_CODE_P (code1)
8462 || code1 == NON_LVALUE_EXPR;
8463 code1 = TREE_CODE (ot))
8464 ot = TREE_OPERAND (ot, 0);
8465 for (enum tree_code code2 = TREE_CODE (nt);
8466 CONVERT_EXPR_CODE_P (code2)
8467 || code2 == NON_LVALUE_EXPR;
8468 code2 = TREE_CODE (nt))
8469 nt = TREE_OPERAND (nt, 0);
8470
8471 return cp_tree_equal (ot, nt);
8472 }
8473 }
8474
8475 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8476 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8477 NEWARG_PTR with the offending arguments if they are non-NULL. */
8478
8479 int
8480 comp_template_args (tree oldargs, tree newargs,
8481 tree *oldarg_ptr, tree *newarg_ptr,
8482 bool partial_order)
8483 {
8484 int i;
8485
8486 if (oldargs == newargs)
8487 return 1;
8488
8489 if (!oldargs || !newargs)
8490 return 0;
8491
8492 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8493 return 0;
8494
8495 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8496 {
8497 tree nt = TREE_VEC_ELT (newargs, i);
8498 tree ot = TREE_VEC_ELT (oldargs, i);
8499
8500 if (! template_args_equal (ot, nt, partial_order))
8501 {
8502 if (oldarg_ptr != NULL)
8503 *oldarg_ptr = ot;
8504 if (newarg_ptr != NULL)
8505 *newarg_ptr = nt;
8506 return 0;
8507 }
8508 }
8509 return 1;
8510 }
8511
8512 inline bool
8513 comp_template_args_porder (tree oargs, tree nargs)
8514 {
8515 return comp_template_args (oargs, nargs, NULL, NULL, true);
8516 }
8517
8518 static void
8519 add_pending_template (tree d)
8520 {
8521 tree ti = (TYPE_P (d)
8522 ? CLASSTYPE_TEMPLATE_INFO (d)
8523 : DECL_TEMPLATE_INFO (d));
8524 struct pending_template *pt;
8525 int level;
8526
8527 if (TI_PENDING_TEMPLATE_FLAG (ti))
8528 return;
8529
8530 /* We are called both from instantiate_decl, where we've already had a
8531 tinst_level pushed, and instantiate_template, where we haven't.
8532 Compensate. */
8533 level = !current_tinst_level || current_tinst_level->decl != d;
8534
8535 if (level)
8536 push_tinst_level (d);
8537
8538 pt = ggc_alloc<pending_template> ();
8539 pt->next = NULL;
8540 pt->tinst = current_tinst_level;
8541 if (last_pending_template)
8542 last_pending_template->next = pt;
8543 else
8544 pending_templates = pt;
8545
8546 last_pending_template = pt;
8547
8548 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8549
8550 if (level)
8551 pop_tinst_level ();
8552 }
8553
8554
8555 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8556 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8557 documentation for TEMPLATE_ID_EXPR. */
8558
8559 tree
8560 lookup_template_function (tree fns, tree arglist)
8561 {
8562 tree type;
8563
8564 if (fns == error_mark_node || arglist == error_mark_node)
8565 return error_mark_node;
8566
8567 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8568
8569 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8570 {
8571 error ("%q#D is not a function template", fns);
8572 return error_mark_node;
8573 }
8574
8575 if (BASELINK_P (fns))
8576 {
8577 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8578 unknown_type_node,
8579 BASELINK_FUNCTIONS (fns),
8580 arglist);
8581 return fns;
8582 }
8583
8584 type = TREE_TYPE (fns);
8585 if (TREE_CODE (fns) == OVERLOAD || !type)
8586 type = unknown_type_node;
8587
8588 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8589 }
8590
8591 /* Within the scope of a template class S<T>, the name S gets bound
8592 (in build_self_reference) to a TYPE_DECL for the class, not a
8593 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8594 or one of its enclosing classes, and that type is a template,
8595 return the associated TEMPLATE_DECL. Otherwise, the original
8596 DECL is returned.
8597
8598 Also handle the case when DECL is a TREE_LIST of ambiguous
8599 injected-class-names from different bases. */
8600
8601 tree
8602 maybe_get_template_decl_from_type_decl (tree decl)
8603 {
8604 if (decl == NULL_TREE)
8605 return decl;
8606
8607 /* DR 176: A lookup that finds an injected-class-name (10.2
8608 [class.member.lookup]) can result in an ambiguity in certain cases
8609 (for example, if it is found in more than one base class). If all of
8610 the injected-class-names that are found refer to specializations of
8611 the same class template, and if the name is followed by a
8612 template-argument-list, the reference refers to the class template
8613 itself and not a specialization thereof, and is not ambiguous. */
8614 if (TREE_CODE (decl) == TREE_LIST)
8615 {
8616 tree t, tmpl = NULL_TREE;
8617 for (t = decl; t; t = TREE_CHAIN (t))
8618 {
8619 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8620 if (!tmpl)
8621 tmpl = elt;
8622 else if (tmpl != elt)
8623 break;
8624 }
8625 if (tmpl && t == NULL_TREE)
8626 return tmpl;
8627 else
8628 return decl;
8629 }
8630
8631 return (decl != NULL_TREE
8632 && DECL_SELF_REFERENCE_P (decl)
8633 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8634 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8635 }
8636
8637 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8638 parameters, find the desired type.
8639
8640 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8641
8642 IN_DECL, if non-NULL, is the template declaration we are trying to
8643 instantiate.
8644
8645 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8646 the class we are looking up.
8647
8648 Issue error and warning messages under control of COMPLAIN.
8649
8650 If the template class is really a local class in a template
8651 function, then the FUNCTION_CONTEXT is the function in which it is
8652 being instantiated.
8653
8654 ??? Note that this function is currently called *twice* for each
8655 template-id: the first time from the parser, while creating the
8656 incomplete type (finish_template_type), and the second type during the
8657 real instantiation (instantiate_template_class). This is surely something
8658 that we want to avoid. It also causes some problems with argument
8659 coercion (see convert_nontype_argument for more information on this). */
8660
8661 static tree
8662 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8663 int entering_scope, tsubst_flags_t complain)
8664 {
8665 tree templ = NULL_TREE, parmlist;
8666 tree t;
8667 spec_entry **slot;
8668 spec_entry *entry;
8669 spec_entry elt;
8670 hashval_t hash;
8671
8672 if (identifier_p (d1))
8673 {
8674 tree value = innermost_non_namespace_value (d1);
8675 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8676 templ = value;
8677 else
8678 {
8679 if (context)
8680 push_decl_namespace (context);
8681 templ = lookup_name (d1);
8682 templ = maybe_get_template_decl_from_type_decl (templ);
8683 if (context)
8684 pop_decl_namespace ();
8685 }
8686 if (templ)
8687 context = DECL_CONTEXT (templ);
8688 }
8689 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8690 {
8691 tree type = TREE_TYPE (d1);
8692
8693 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8694 an implicit typename for the second A. Deal with it. */
8695 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8696 type = TREE_TYPE (type);
8697
8698 if (CLASSTYPE_TEMPLATE_INFO (type))
8699 {
8700 templ = CLASSTYPE_TI_TEMPLATE (type);
8701 d1 = DECL_NAME (templ);
8702 }
8703 }
8704 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8705 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8706 {
8707 templ = TYPE_TI_TEMPLATE (d1);
8708 d1 = DECL_NAME (templ);
8709 }
8710 else if (DECL_TYPE_TEMPLATE_P (d1))
8711 {
8712 templ = d1;
8713 d1 = DECL_NAME (templ);
8714 context = DECL_CONTEXT (templ);
8715 }
8716 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8717 {
8718 templ = d1;
8719 d1 = DECL_NAME (templ);
8720 }
8721
8722 /* Issue an error message if we didn't find a template. */
8723 if (! templ)
8724 {
8725 if (complain & tf_error)
8726 error ("%qT is not a template", d1);
8727 return error_mark_node;
8728 }
8729
8730 if (TREE_CODE (templ) != TEMPLATE_DECL
8731 /* Make sure it's a user visible template, if it was named by
8732 the user. */
8733 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8734 && !PRIMARY_TEMPLATE_P (templ)))
8735 {
8736 if (complain & tf_error)
8737 {
8738 error ("non-template type %qT used as a template", d1);
8739 if (in_decl)
8740 error ("for template declaration %q+D", in_decl);
8741 }
8742 return error_mark_node;
8743 }
8744
8745 complain &= ~tf_user;
8746
8747 /* An alias that just changes the name of a template is equivalent to the
8748 other template, so if any of the arguments are pack expansions, strip
8749 the alias to avoid problems with a pack expansion passed to a non-pack
8750 alias template parameter (DR 1430). */
8751 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8752 templ = get_underlying_template (templ);
8753
8754 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8755 {
8756 tree parm;
8757 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8758 if (arglist2 == error_mark_node
8759 || (!uses_template_parms (arglist2)
8760 && check_instantiated_args (templ, arglist2, complain)))
8761 return error_mark_node;
8762
8763 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8764 return parm;
8765 }
8766 else
8767 {
8768 tree template_type = TREE_TYPE (templ);
8769 tree gen_tmpl;
8770 tree type_decl;
8771 tree found = NULL_TREE;
8772 int arg_depth;
8773 int parm_depth;
8774 int is_dependent_type;
8775 int use_partial_inst_tmpl = false;
8776
8777 if (template_type == error_mark_node)
8778 /* An error occurred while building the template TEMPL, and a
8779 diagnostic has most certainly been emitted for that
8780 already. Let's propagate that error. */
8781 return error_mark_node;
8782
8783 gen_tmpl = most_general_template (templ);
8784 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8785 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8786 arg_depth = TMPL_ARGS_DEPTH (arglist);
8787
8788 if (arg_depth == 1 && parm_depth > 1)
8789 {
8790 /* We've been given an incomplete set of template arguments.
8791 For example, given:
8792
8793 template <class T> struct S1 {
8794 template <class U> struct S2 {};
8795 template <class U> struct S2<U*> {};
8796 };
8797
8798 we will be called with an ARGLIST of `U*', but the
8799 TEMPLATE will be `template <class T> template
8800 <class U> struct S1<T>::S2'. We must fill in the missing
8801 arguments. */
8802 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8803 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8804 arg_depth = TMPL_ARGS_DEPTH (arglist);
8805 }
8806
8807 /* Now we should have enough arguments. */
8808 gcc_assert (parm_depth == arg_depth);
8809
8810 /* From here on, we're only interested in the most general
8811 template. */
8812
8813 /* Calculate the BOUND_ARGS. These will be the args that are
8814 actually tsubst'd into the definition to create the
8815 instantiation. */
8816 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8817 complain,
8818 /*require_all_args=*/true,
8819 /*use_default_args=*/true);
8820
8821 if (arglist == error_mark_node)
8822 /* We were unable to bind the arguments. */
8823 return error_mark_node;
8824
8825 /* In the scope of a template class, explicit references to the
8826 template class refer to the type of the template, not any
8827 instantiation of it. For example, in:
8828
8829 template <class T> class C { void f(C<T>); }
8830
8831 the `C<T>' is just the same as `C'. Outside of the
8832 class, however, such a reference is an instantiation. */
8833 if (entering_scope
8834 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8835 || currently_open_class (template_type))
8836 {
8837 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8838
8839 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8840 return template_type;
8841 }
8842
8843 /* If we already have this specialization, return it. */
8844 elt.tmpl = gen_tmpl;
8845 elt.args = arglist;
8846 elt.spec = NULL_TREE;
8847 hash = spec_hasher::hash (&elt);
8848 entry = type_specializations->find_with_hash (&elt, hash);
8849
8850 if (entry)
8851 return entry->spec;
8852
8853 /* If the the template's constraints are not satisfied,
8854 then we cannot form a valid type.
8855
8856 Note that the check is deferred until after the hash
8857 lookup. This prevents redundant checks on previously
8858 instantiated specializations. */
8859 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8860 {
8861 if (complain & tf_error)
8862 {
8863 error ("template constraint failure");
8864 diagnose_constraints (input_location, gen_tmpl, arglist);
8865 }
8866 return error_mark_node;
8867 }
8868
8869 is_dependent_type = uses_template_parms (arglist);
8870
8871 /* If the deduced arguments are invalid, then the binding
8872 failed. */
8873 if (!is_dependent_type
8874 && check_instantiated_args (gen_tmpl,
8875 INNERMOST_TEMPLATE_ARGS (arglist),
8876 complain))
8877 return error_mark_node;
8878
8879 if (!is_dependent_type
8880 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8881 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8882 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8883 {
8884 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8885 DECL_NAME (gen_tmpl),
8886 /*tag_scope=*/ts_global);
8887 return found;
8888 }
8889
8890 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8891 complain, in_decl);
8892 if (context == error_mark_node)
8893 return error_mark_node;
8894
8895 if (!context)
8896 context = global_namespace;
8897
8898 /* Create the type. */
8899 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8900 {
8901 /* The user referred to a specialization of an alias
8902 template represented by GEN_TMPL.
8903
8904 [temp.alias]/2 says:
8905
8906 When a template-id refers to the specialization of an
8907 alias template, it is equivalent to the associated
8908 type obtained by substitution of its
8909 template-arguments for the template-parameters in the
8910 type-id of the alias template. */
8911
8912 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8913 /* Note that the call above (by indirectly calling
8914 register_specialization in tsubst_decl) registers the
8915 TYPE_DECL representing the specialization of the alias
8916 template. So next time someone substitutes ARGLIST for
8917 the template parms into the alias template (GEN_TMPL),
8918 she'll get that TYPE_DECL back. */
8919
8920 if (t == error_mark_node)
8921 return t;
8922 }
8923 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8924 {
8925 if (!is_dependent_type)
8926 {
8927 set_current_access_from_decl (TYPE_NAME (template_type));
8928 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8929 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8930 arglist, complain, in_decl),
8931 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8932 arglist, complain, in_decl),
8933 SCOPED_ENUM_P (template_type), NULL);
8934
8935 if (t == error_mark_node)
8936 return t;
8937 }
8938 else
8939 {
8940 /* We don't want to call start_enum for this type, since
8941 the values for the enumeration constants may involve
8942 template parameters. And, no one should be interested
8943 in the enumeration constants for such a type. */
8944 t = cxx_make_type (ENUMERAL_TYPE);
8945 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8946 }
8947 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8948 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8949 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8950 }
8951 else if (CLASS_TYPE_P (template_type))
8952 {
8953 t = make_class_type (TREE_CODE (template_type));
8954 CLASSTYPE_DECLARED_CLASS (t)
8955 = CLASSTYPE_DECLARED_CLASS (template_type);
8956 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8957
8958 /* A local class. Make sure the decl gets registered properly. */
8959 if (context == current_function_decl)
8960 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8961
8962 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8963 /* This instantiation is another name for the primary
8964 template type. Set the TYPE_CANONICAL field
8965 appropriately. */
8966 TYPE_CANONICAL (t) = template_type;
8967 else if (any_template_arguments_need_structural_equality_p (arglist))
8968 /* Some of the template arguments require structural
8969 equality testing, so this template class requires
8970 structural equality testing. */
8971 SET_TYPE_STRUCTURAL_EQUALITY (t);
8972 }
8973 else
8974 gcc_unreachable ();
8975
8976 /* If we called start_enum or pushtag above, this information
8977 will already be set up. */
8978 if (!TYPE_NAME (t))
8979 {
8980 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8981
8982 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8983 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8984 DECL_SOURCE_LOCATION (type_decl)
8985 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8986 }
8987 else
8988 type_decl = TYPE_NAME (t);
8989
8990 if (CLASS_TYPE_P (template_type))
8991 {
8992 TREE_PRIVATE (type_decl)
8993 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8994 TREE_PROTECTED (type_decl)
8995 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8996 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8997 {
8998 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8999 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9000 }
9001 }
9002
9003 if (OVERLOAD_TYPE_P (t)
9004 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9005 {
9006 static const char *tags[] = {"abi_tag", "may_alias"};
9007
9008 for (unsigned ix = 0; ix != 2; ix++)
9009 {
9010 tree attributes
9011 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9012
9013 if (attributes)
9014 TYPE_ATTRIBUTES (t)
9015 = tree_cons (TREE_PURPOSE (attributes),
9016 TREE_VALUE (attributes),
9017 TYPE_ATTRIBUTES (t));
9018 }
9019 }
9020
9021 /* Let's consider the explicit specialization of a member
9022 of a class template specialization that is implicitly instantiated,
9023 e.g.:
9024 template<class T>
9025 struct S
9026 {
9027 template<class U> struct M {}; //#0
9028 };
9029
9030 template<>
9031 template<>
9032 struct S<int>::M<char> //#1
9033 {
9034 int i;
9035 };
9036 [temp.expl.spec]/4 says this is valid.
9037
9038 In this case, when we write:
9039 S<int>::M<char> m;
9040
9041 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9042 the one of #0.
9043
9044 When we encounter #1, we want to store the partial instantiation
9045 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9046
9047 For all cases other than this "explicit specialization of member of a
9048 class template", we just want to store the most general template into
9049 the CLASSTYPE_TI_TEMPLATE of M.
9050
9051 This case of "explicit specialization of member of a class template"
9052 only happens when:
9053 1/ the enclosing class is an instantiation of, and therefore not
9054 the same as, the context of the most general template, and
9055 2/ we aren't looking at the partial instantiation itself, i.e.
9056 the innermost arguments are not the same as the innermost parms of
9057 the most general template.
9058
9059 So it's only when 1/ and 2/ happens that we want to use the partial
9060 instantiation of the member template in lieu of its most general
9061 template. */
9062
9063 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9064 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9065 /* the enclosing class must be an instantiation... */
9066 && CLASS_TYPE_P (context)
9067 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9068 {
9069 TREE_VEC_LENGTH (arglist)--;
9070 ++processing_template_decl;
9071 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9072 tree partial_inst_args =
9073 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9074 arglist, complain, NULL_TREE);
9075 --processing_template_decl;
9076 TREE_VEC_LENGTH (arglist)++;
9077 if (partial_inst_args == error_mark_node)
9078 return error_mark_node;
9079 use_partial_inst_tmpl =
9080 /*...and we must not be looking at the partial instantiation
9081 itself. */
9082 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9083 partial_inst_args);
9084 }
9085
9086 if (!use_partial_inst_tmpl)
9087 /* This case is easy; there are no member templates involved. */
9088 found = gen_tmpl;
9089 else
9090 {
9091 /* This is a full instantiation of a member template. Find
9092 the partial instantiation of which this is an instance. */
9093
9094 /* Temporarily reduce by one the number of levels in the ARGLIST
9095 so as to avoid comparing the last set of arguments. */
9096 TREE_VEC_LENGTH (arglist)--;
9097 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9098 TREE_VEC_LENGTH (arglist)++;
9099 /* FOUND is either a proper class type, or an alias
9100 template specialization. In the later case, it's a
9101 TYPE_DECL, resulting from the substituting of arguments
9102 for parameters in the TYPE_DECL of the alias template
9103 done earlier. So be careful while getting the template
9104 of FOUND. */
9105 found = (TREE_CODE (found) == TEMPLATE_DECL
9106 ? found
9107 : (TREE_CODE (found) == TYPE_DECL
9108 ? DECL_TI_TEMPLATE (found)
9109 : CLASSTYPE_TI_TEMPLATE (found)));
9110 }
9111
9112 // Build template info for the new specialization.
9113 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9114
9115 elt.spec = t;
9116 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9117 entry = ggc_alloc<spec_entry> ();
9118 *entry = elt;
9119 *slot = entry;
9120
9121 /* Note this use of the partial instantiation so we can check it
9122 later in maybe_process_partial_specialization. */
9123 DECL_TEMPLATE_INSTANTIATIONS (found)
9124 = tree_cons (arglist, t,
9125 DECL_TEMPLATE_INSTANTIATIONS (found));
9126
9127 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9128 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9129 /* Now that the type has been registered on the instantiations
9130 list, we set up the enumerators. Because the enumeration
9131 constants may involve the enumeration type itself, we make
9132 sure to register the type first, and then create the
9133 constants. That way, doing tsubst_expr for the enumeration
9134 constants won't result in recursive calls here; we'll find
9135 the instantiation and exit above. */
9136 tsubst_enum (template_type, t, arglist);
9137
9138 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9139 /* If the type makes use of template parameters, the
9140 code that generates debugging information will crash. */
9141 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9142
9143 /* Possibly limit visibility based on template args. */
9144 TREE_PUBLIC (type_decl) = 1;
9145 determine_visibility (type_decl);
9146
9147 inherit_targ_abi_tags (t);
9148
9149 return t;
9150 }
9151 }
9152
9153 /* Wrapper for lookup_template_class_1. */
9154
9155 tree
9156 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9157 int entering_scope, tsubst_flags_t complain)
9158 {
9159 tree ret;
9160 timevar_push (TV_TEMPLATE_INST);
9161 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9162 entering_scope, complain);
9163 timevar_pop (TV_TEMPLATE_INST);
9164 return ret;
9165 }
9166
9167 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9168
9169 tree
9170 lookup_template_variable (tree templ, tree arglist)
9171 {
9172 /* The type of the expression is NULL_TREE since the template-id could refer
9173 to an explicit or partial specialization. */
9174 tree type = NULL_TREE;
9175 if (flag_concepts && variable_concept_p (templ))
9176 /* Except that concepts are always bool. */
9177 type = boolean_type_node;
9178 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9179 }
9180
9181 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9182
9183 tree
9184 finish_template_variable (tree var, tsubst_flags_t complain)
9185 {
9186 tree templ = TREE_OPERAND (var, 0);
9187 tree arglist = TREE_OPERAND (var, 1);
9188
9189 /* We never want to return a VAR_DECL for a variable concept, since they
9190 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9191 bool concept_p = flag_concepts && variable_concept_p (templ);
9192 if (concept_p && processing_template_decl)
9193 return var;
9194
9195 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9196 arglist = add_outermost_template_args (tmpl_args, arglist);
9197
9198 templ = most_general_template (templ);
9199 tree parms = DECL_TEMPLATE_PARMS (templ);
9200 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9201 /*req_all*/true,
9202 /*use_default*/true);
9203
9204 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9205 {
9206 if (complain & tf_error)
9207 {
9208 error ("use of invalid variable template %qE", var);
9209 diagnose_constraints (location_of (var), templ, arglist);
9210 }
9211 return error_mark_node;
9212 }
9213
9214 /* If a template-id refers to a specialization of a variable
9215 concept, then the expression is true if and only if the
9216 concept's constraints are satisfied by the given template
9217 arguments.
9218
9219 NOTE: This is an extension of Concepts Lite TS that
9220 allows constraints to be used in expressions. */
9221 if (concept_p)
9222 {
9223 tree decl = DECL_TEMPLATE_RESULT (templ);
9224 return evaluate_variable_concept (decl, arglist);
9225 }
9226
9227 return instantiate_template (templ, arglist, complain);
9228 }
9229
9230 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9231 TARGS template args, and instantiate it if it's not dependent. */
9232
9233 tree
9234 lookup_and_finish_template_variable (tree templ, tree targs,
9235 tsubst_flags_t complain)
9236 {
9237 templ = lookup_template_variable (templ, targs);
9238 if (!any_dependent_template_arguments_p (targs))
9239 {
9240 templ = finish_template_variable (templ, complain);
9241 mark_used (templ);
9242 }
9243
9244 return convert_from_reference (templ);
9245 }
9246
9247 \f
9248 struct pair_fn_data
9249 {
9250 tree_fn_t fn;
9251 tree_fn_t any_fn;
9252 void *data;
9253 /* True when we should also visit template parameters that occur in
9254 non-deduced contexts. */
9255 bool include_nondeduced_p;
9256 hash_set<tree> *visited;
9257 };
9258
9259 /* Called from for_each_template_parm via walk_tree. */
9260
9261 static tree
9262 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9263 {
9264 tree t = *tp;
9265 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9266 tree_fn_t fn = pfd->fn;
9267 void *data = pfd->data;
9268 tree result = NULL_TREE;
9269
9270 #define WALK_SUBTREE(NODE) \
9271 do \
9272 { \
9273 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9274 pfd->include_nondeduced_p, \
9275 pfd->any_fn); \
9276 if (result) goto out; \
9277 } \
9278 while (0)
9279
9280 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9281 return t;
9282
9283 if (TYPE_P (t)
9284 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9285 WALK_SUBTREE (TYPE_CONTEXT (t));
9286
9287 switch (TREE_CODE (t))
9288 {
9289 case RECORD_TYPE:
9290 if (TYPE_PTRMEMFUNC_P (t))
9291 break;
9292 /* Fall through. */
9293
9294 case UNION_TYPE:
9295 case ENUMERAL_TYPE:
9296 if (!TYPE_TEMPLATE_INFO (t))
9297 *walk_subtrees = 0;
9298 else
9299 WALK_SUBTREE (TYPE_TI_ARGS (t));
9300 break;
9301
9302 case INTEGER_TYPE:
9303 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9304 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9305 break;
9306
9307 case METHOD_TYPE:
9308 /* Since we're not going to walk subtrees, we have to do this
9309 explicitly here. */
9310 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9311 /* Fall through. */
9312
9313 case FUNCTION_TYPE:
9314 /* Check the return type. */
9315 WALK_SUBTREE (TREE_TYPE (t));
9316
9317 /* Check the parameter types. Since default arguments are not
9318 instantiated until they are needed, the TYPE_ARG_TYPES may
9319 contain expressions that involve template parameters. But,
9320 no-one should be looking at them yet. And, once they're
9321 instantiated, they don't contain template parameters, so
9322 there's no point in looking at them then, either. */
9323 {
9324 tree parm;
9325
9326 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9327 WALK_SUBTREE (TREE_VALUE (parm));
9328
9329 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9330 want walk_tree walking into them itself. */
9331 *walk_subtrees = 0;
9332 }
9333
9334 if (flag_noexcept_type)
9335 {
9336 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9337 if (spec)
9338 WALK_SUBTREE (TREE_PURPOSE (spec));
9339 }
9340 break;
9341
9342 case TYPEOF_TYPE:
9343 case UNDERLYING_TYPE:
9344 if (pfd->include_nondeduced_p
9345 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9346 pfd->visited,
9347 pfd->include_nondeduced_p,
9348 pfd->any_fn))
9349 return error_mark_node;
9350 break;
9351
9352 case FUNCTION_DECL:
9353 case VAR_DECL:
9354 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9355 WALK_SUBTREE (DECL_TI_ARGS (t));
9356 /* Fall through. */
9357
9358 case PARM_DECL:
9359 case CONST_DECL:
9360 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9361 WALK_SUBTREE (DECL_INITIAL (t));
9362 if (DECL_CONTEXT (t)
9363 && pfd->include_nondeduced_p)
9364 WALK_SUBTREE (DECL_CONTEXT (t));
9365 break;
9366
9367 case BOUND_TEMPLATE_TEMPLATE_PARM:
9368 /* Record template parameters such as `T' inside `TT<T>'. */
9369 WALK_SUBTREE (TYPE_TI_ARGS (t));
9370 /* Fall through. */
9371
9372 case TEMPLATE_TEMPLATE_PARM:
9373 case TEMPLATE_TYPE_PARM:
9374 case TEMPLATE_PARM_INDEX:
9375 if (fn && (*fn)(t, data))
9376 return t;
9377 else if (!fn)
9378 return t;
9379 break;
9380
9381 case TEMPLATE_DECL:
9382 /* A template template parameter is encountered. */
9383 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9384 WALK_SUBTREE (TREE_TYPE (t));
9385
9386 /* Already substituted template template parameter */
9387 *walk_subtrees = 0;
9388 break;
9389
9390 case TYPENAME_TYPE:
9391 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9392 partial instantiation. */
9393 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9394 break;
9395
9396 case CONSTRUCTOR:
9397 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9398 && pfd->include_nondeduced_p)
9399 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9400 break;
9401
9402 case INDIRECT_REF:
9403 case COMPONENT_REF:
9404 /* If there's no type, then this thing must be some expression
9405 involving template parameters. */
9406 if (!fn && !TREE_TYPE (t))
9407 return error_mark_node;
9408 break;
9409
9410 case MODOP_EXPR:
9411 case CAST_EXPR:
9412 case IMPLICIT_CONV_EXPR:
9413 case REINTERPRET_CAST_EXPR:
9414 case CONST_CAST_EXPR:
9415 case STATIC_CAST_EXPR:
9416 case DYNAMIC_CAST_EXPR:
9417 case ARROW_EXPR:
9418 case DOTSTAR_EXPR:
9419 case TYPEID_EXPR:
9420 case PSEUDO_DTOR_EXPR:
9421 if (!fn)
9422 return error_mark_node;
9423 break;
9424
9425 default:
9426 break;
9427 }
9428
9429 #undef WALK_SUBTREE
9430
9431 /* We didn't find any template parameters we liked. */
9432 out:
9433 return result;
9434 }
9435
9436 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9437 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9438 call FN with the parameter and the DATA.
9439 If FN returns nonzero, the iteration is terminated, and
9440 for_each_template_parm returns 1. Otherwise, the iteration
9441 continues. If FN never returns a nonzero value, the value
9442 returned by for_each_template_parm is 0. If FN is NULL, it is
9443 considered to be the function which always returns 1.
9444
9445 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9446 parameters that occur in non-deduced contexts. When false, only
9447 visits those template parameters that can be deduced. */
9448
9449 static tree
9450 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9451 hash_set<tree> *visited,
9452 bool include_nondeduced_p,
9453 tree_fn_t any_fn)
9454 {
9455 struct pair_fn_data pfd;
9456 tree result;
9457
9458 /* Set up. */
9459 pfd.fn = fn;
9460 pfd.any_fn = any_fn;
9461 pfd.data = data;
9462 pfd.include_nondeduced_p = include_nondeduced_p;
9463
9464 /* Walk the tree. (Conceptually, we would like to walk without
9465 duplicates, but for_each_template_parm_r recursively calls
9466 for_each_template_parm, so we would need to reorganize a fair
9467 bit to use walk_tree_without_duplicates, so we keep our own
9468 visited list.) */
9469 if (visited)
9470 pfd.visited = visited;
9471 else
9472 pfd.visited = new hash_set<tree>;
9473 result = cp_walk_tree (&t,
9474 for_each_template_parm_r,
9475 &pfd,
9476 pfd.visited);
9477
9478 /* Clean up. */
9479 if (!visited)
9480 {
9481 delete pfd.visited;
9482 pfd.visited = 0;
9483 }
9484
9485 return result;
9486 }
9487
9488 /* Returns true if T depends on any template parameter. */
9489
9490 int
9491 uses_template_parms (tree t)
9492 {
9493 if (t == NULL_TREE)
9494 return false;
9495
9496 bool dependent_p;
9497 int saved_processing_template_decl;
9498
9499 saved_processing_template_decl = processing_template_decl;
9500 if (!saved_processing_template_decl)
9501 processing_template_decl = 1;
9502 if (TYPE_P (t))
9503 dependent_p = dependent_type_p (t);
9504 else if (TREE_CODE (t) == TREE_VEC)
9505 dependent_p = any_dependent_template_arguments_p (t);
9506 else if (TREE_CODE (t) == TREE_LIST)
9507 dependent_p = (uses_template_parms (TREE_VALUE (t))
9508 || uses_template_parms (TREE_CHAIN (t)));
9509 else if (TREE_CODE (t) == TYPE_DECL)
9510 dependent_p = dependent_type_p (TREE_TYPE (t));
9511 else if (DECL_P (t)
9512 || EXPR_P (t)
9513 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9514 || TREE_CODE (t) == OVERLOAD
9515 || BASELINK_P (t)
9516 || identifier_p (t)
9517 || TREE_CODE (t) == TRAIT_EXPR
9518 || TREE_CODE (t) == CONSTRUCTOR
9519 || CONSTANT_CLASS_P (t))
9520 dependent_p = (type_dependent_expression_p (t)
9521 || value_dependent_expression_p (t));
9522 else
9523 {
9524 gcc_assert (t == error_mark_node);
9525 dependent_p = false;
9526 }
9527
9528 processing_template_decl = saved_processing_template_decl;
9529
9530 return dependent_p;
9531 }
9532
9533 /* Returns true iff current_function_decl is an incompletely instantiated
9534 template. Useful instead of processing_template_decl because the latter
9535 is set to 0 during instantiate_non_dependent_expr. */
9536
9537 bool
9538 in_template_function (void)
9539 {
9540 tree fn = current_function_decl;
9541 bool ret;
9542 ++processing_template_decl;
9543 ret = (fn && DECL_LANG_SPECIFIC (fn)
9544 && DECL_TEMPLATE_INFO (fn)
9545 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9546 --processing_template_decl;
9547 return ret;
9548 }
9549
9550 /* Returns true if T depends on any template parameter with level LEVEL. */
9551
9552 bool
9553 uses_template_parms_level (tree t, int level)
9554 {
9555 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9556 /*include_nondeduced_p=*/true);
9557 }
9558
9559 /* Returns true if the signature of DECL depends on any template parameter from
9560 its enclosing class. */
9561
9562 bool
9563 uses_outer_template_parms (tree decl)
9564 {
9565 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9566 if (depth == 0)
9567 return false;
9568 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9569 &depth, NULL, /*include_nondeduced_p=*/true))
9570 return true;
9571 if (PRIMARY_TEMPLATE_P (decl)
9572 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9573 (DECL_TEMPLATE_PARMS (decl)),
9574 template_parm_outer_level,
9575 &depth, NULL, /*include_nondeduced_p=*/true))
9576 return true;
9577 tree ci = get_constraints (decl);
9578 if (ci)
9579 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9580 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9581 &depth, NULL, /*nondeduced*/true))
9582 return true;
9583 return false;
9584 }
9585
9586 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9587 ill-formed translation unit, i.e. a variable or function that isn't
9588 usable in a constant expression. */
9589
9590 static inline bool
9591 neglectable_inst_p (tree d)
9592 {
9593 return (DECL_P (d)
9594 && !undeduced_auto_decl (d)
9595 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9596 : decl_maybe_constant_var_p (d)));
9597 }
9598
9599 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9600 neglectable and instantiated from within an erroneous instantiation. */
9601
9602 static bool
9603 limit_bad_template_recursion (tree decl)
9604 {
9605 struct tinst_level *lev = current_tinst_level;
9606 int errs = errorcount + sorrycount;
9607 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9608 return false;
9609
9610 for (; lev; lev = lev->next)
9611 if (neglectable_inst_p (lev->decl))
9612 break;
9613
9614 return (lev && errs > lev->errors);
9615 }
9616
9617 static int tinst_depth;
9618 extern int max_tinst_depth;
9619 int depth_reached;
9620
9621 static GTY(()) struct tinst_level *last_error_tinst_level;
9622
9623 /* We're starting to instantiate D; record the template instantiation context
9624 for diagnostics and to restore it later. */
9625
9626 bool
9627 push_tinst_level (tree d)
9628 {
9629 return push_tinst_level_loc (d, input_location);
9630 }
9631
9632 /* We're starting to instantiate D; record the template instantiation context
9633 at LOC for diagnostics and to restore it later. */
9634
9635 bool
9636 push_tinst_level_loc (tree d, location_t loc)
9637 {
9638 struct tinst_level *new_level;
9639
9640 if (tinst_depth >= max_tinst_depth)
9641 {
9642 /* Tell error.c not to try to instantiate any templates. */
9643 at_eof = 2;
9644 fatal_error (input_location,
9645 "template instantiation depth exceeds maximum of %d"
9646 " (use -ftemplate-depth= to increase the maximum)",
9647 max_tinst_depth);
9648 return false;
9649 }
9650
9651 /* If the current instantiation caused problems, don't let it instantiate
9652 anything else. Do allow deduction substitution and decls usable in
9653 constant expressions. */
9654 if (limit_bad_template_recursion (d))
9655 return false;
9656
9657 /* When not -quiet, dump template instantiations other than functions, since
9658 announce_function will take care of those. */
9659 if (!quiet_flag
9660 && TREE_CODE (d) != TREE_LIST
9661 && TREE_CODE (d) != FUNCTION_DECL)
9662 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9663
9664 new_level = ggc_alloc<tinst_level> ();
9665 new_level->decl = d;
9666 new_level->locus = loc;
9667 new_level->errors = errorcount+sorrycount;
9668 new_level->in_system_header_p = in_system_header_at (input_location);
9669 new_level->next = current_tinst_level;
9670 current_tinst_level = new_level;
9671
9672 ++tinst_depth;
9673 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9674 depth_reached = tinst_depth;
9675
9676 return true;
9677 }
9678
9679 /* We're done instantiating this template; return to the instantiation
9680 context. */
9681
9682 void
9683 pop_tinst_level (void)
9684 {
9685 /* Restore the filename and line number stashed away when we started
9686 this instantiation. */
9687 input_location = current_tinst_level->locus;
9688 current_tinst_level = current_tinst_level->next;
9689 --tinst_depth;
9690 }
9691
9692 /* We're instantiating a deferred template; restore the template
9693 instantiation context in which the instantiation was requested, which
9694 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9695
9696 static tree
9697 reopen_tinst_level (struct tinst_level *level)
9698 {
9699 struct tinst_level *t;
9700
9701 tinst_depth = 0;
9702 for (t = level; t; t = t->next)
9703 ++tinst_depth;
9704
9705 current_tinst_level = level;
9706 pop_tinst_level ();
9707 if (current_tinst_level)
9708 current_tinst_level->errors = errorcount+sorrycount;
9709 return level->decl;
9710 }
9711
9712 /* Returns the TINST_LEVEL which gives the original instantiation
9713 context. */
9714
9715 struct tinst_level *
9716 outermost_tinst_level (void)
9717 {
9718 struct tinst_level *level = current_tinst_level;
9719 if (level)
9720 while (level->next)
9721 level = level->next;
9722 return level;
9723 }
9724
9725 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9726 vector of template arguments, as for tsubst.
9727
9728 Returns an appropriate tsubst'd friend declaration. */
9729
9730 static tree
9731 tsubst_friend_function (tree decl, tree args)
9732 {
9733 tree new_friend;
9734
9735 if (TREE_CODE (decl) == FUNCTION_DECL
9736 && DECL_TEMPLATE_INSTANTIATION (decl)
9737 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9738 /* This was a friend declared with an explicit template
9739 argument list, e.g.:
9740
9741 friend void f<>(T);
9742
9743 to indicate that f was a template instantiation, not a new
9744 function declaration. Now, we have to figure out what
9745 instantiation of what template. */
9746 {
9747 tree template_id, arglist, fns;
9748 tree new_args;
9749 tree tmpl;
9750 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9751
9752 /* Friend functions are looked up in the containing namespace scope.
9753 We must enter that scope, to avoid finding member functions of the
9754 current class with same name. */
9755 push_nested_namespace (ns);
9756 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9757 tf_warning_or_error, NULL_TREE,
9758 /*integral_constant_expression_p=*/false);
9759 pop_nested_namespace (ns);
9760 arglist = tsubst (DECL_TI_ARGS (decl), args,
9761 tf_warning_or_error, NULL_TREE);
9762 template_id = lookup_template_function (fns, arglist);
9763
9764 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9765 tmpl = determine_specialization (template_id, new_friend,
9766 &new_args,
9767 /*need_member_template=*/0,
9768 TREE_VEC_LENGTH (args),
9769 tsk_none);
9770 return instantiate_template (tmpl, new_args, tf_error);
9771 }
9772
9773 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9774
9775 /* The NEW_FRIEND will look like an instantiation, to the
9776 compiler, but is not an instantiation from the point of view of
9777 the language. For example, we might have had:
9778
9779 template <class T> struct S {
9780 template <class U> friend void f(T, U);
9781 };
9782
9783 Then, in S<int>, template <class U> void f(int, U) is not an
9784 instantiation of anything. */
9785 if (new_friend == error_mark_node)
9786 return error_mark_node;
9787
9788 DECL_USE_TEMPLATE (new_friend) = 0;
9789 if (TREE_CODE (decl) == TEMPLATE_DECL)
9790 {
9791 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9792 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9793 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9794 }
9795
9796 /* The mangled name for the NEW_FRIEND is incorrect. The function
9797 is not a template instantiation and should not be mangled like
9798 one. Therefore, we forget the mangling here; we'll recompute it
9799 later if we need it. */
9800 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9801 {
9802 SET_DECL_RTL (new_friend, NULL);
9803 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9804 }
9805
9806 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9807 {
9808 tree old_decl;
9809 tree new_friend_template_info;
9810 tree new_friend_result_template_info;
9811 tree ns;
9812 int new_friend_is_defn;
9813
9814 /* We must save some information from NEW_FRIEND before calling
9815 duplicate decls since that function will free NEW_FRIEND if
9816 possible. */
9817 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9818 new_friend_is_defn =
9819 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9820 (template_for_substitution (new_friend)))
9821 != NULL_TREE);
9822 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9823 {
9824 /* This declaration is a `primary' template. */
9825 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9826
9827 new_friend_result_template_info
9828 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9829 }
9830 else
9831 new_friend_result_template_info = NULL_TREE;
9832
9833 /* Inside pushdecl_namespace_level, we will push into the
9834 current namespace. However, the friend function should go
9835 into the namespace of the template. */
9836 ns = decl_namespace_context (new_friend);
9837 push_nested_namespace (ns);
9838 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9839 pop_nested_namespace (ns);
9840
9841 if (old_decl == error_mark_node)
9842 return error_mark_node;
9843
9844 if (old_decl != new_friend)
9845 {
9846 /* This new friend declaration matched an existing
9847 declaration. For example, given:
9848
9849 template <class T> void f(T);
9850 template <class U> class C {
9851 template <class T> friend void f(T) {}
9852 };
9853
9854 the friend declaration actually provides the definition
9855 of `f', once C has been instantiated for some type. So,
9856 old_decl will be the out-of-class template declaration,
9857 while new_friend is the in-class definition.
9858
9859 But, if `f' was called before this point, the
9860 instantiation of `f' will have DECL_TI_ARGS corresponding
9861 to `T' but not to `U', references to which might appear
9862 in the definition of `f'. Previously, the most general
9863 template for an instantiation of `f' was the out-of-class
9864 version; now it is the in-class version. Therefore, we
9865 run through all specialization of `f', adding to their
9866 DECL_TI_ARGS appropriately. In particular, they need a
9867 new set of outer arguments, corresponding to the
9868 arguments for this class instantiation.
9869
9870 The same situation can arise with something like this:
9871
9872 friend void f(int);
9873 template <class T> class C {
9874 friend void f(T) {}
9875 };
9876
9877 when `C<int>' is instantiated. Now, `f(int)' is defined
9878 in the class. */
9879
9880 if (!new_friend_is_defn)
9881 /* On the other hand, if the in-class declaration does
9882 *not* provide a definition, then we don't want to alter
9883 existing definitions. We can just leave everything
9884 alone. */
9885 ;
9886 else
9887 {
9888 tree new_template = TI_TEMPLATE (new_friend_template_info);
9889 tree new_args = TI_ARGS (new_friend_template_info);
9890
9891 /* Overwrite whatever template info was there before, if
9892 any, with the new template information pertaining to
9893 the declaration. */
9894 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9895
9896 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9897 {
9898 /* We should have called reregister_specialization in
9899 duplicate_decls. */
9900 gcc_assert (retrieve_specialization (new_template,
9901 new_args, 0)
9902 == old_decl);
9903
9904 /* Instantiate it if the global has already been used. */
9905 if (DECL_ODR_USED (old_decl))
9906 instantiate_decl (old_decl, /*defer_ok=*/true,
9907 /*expl_inst_class_mem_p=*/false);
9908 }
9909 else
9910 {
9911 tree t;
9912
9913 /* Indicate that the old function template is a partial
9914 instantiation. */
9915 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9916 = new_friend_result_template_info;
9917
9918 gcc_assert (new_template
9919 == most_general_template (new_template));
9920 gcc_assert (new_template != old_decl);
9921
9922 /* Reassign any specializations already in the hash table
9923 to the new more general template, and add the
9924 additional template args. */
9925 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9926 t != NULL_TREE;
9927 t = TREE_CHAIN (t))
9928 {
9929 tree spec = TREE_VALUE (t);
9930 spec_entry elt;
9931
9932 elt.tmpl = old_decl;
9933 elt.args = DECL_TI_ARGS (spec);
9934 elt.spec = NULL_TREE;
9935
9936 decl_specializations->remove_elt (&elt);
9937
9938 DECL_TI_ARGS (spec)
9939 = add_outermost_template_args (new_args,
9940 DECL_TI_ARGS (spec));
9941
9942 register_specialization
9943 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9944
9945 }
9946 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9947 }
9948 }
9949
9950 /* The information from NEW_FRIEND has been merged into OLD_DECL
9951 by duplicate_decls. */
9952 new_friend = old_decl;
9953 }
9954 }
9955 else
9956 {
9957 tree context = DECL_CONTEXT (new_friend);
9958 bool dependent_p;
9959
9960 /* In the code
9961 template <class T> class C {
9962 template <class U> friend void C1<U>::f (); // case 1
9963 friend void C2<T>::f (); // case 2
9964 };
9965 we only need to make sure CONTEXT is a complete type for
9966 case 2. To distinguish between the two cases, we note that
9967 CONTEXT of case 1 remains dependent type after tsubst while
9968 this isn't true for case 2. */
9969 ++processing_template_decl;
9970 dependent_p = dependent_type_p (context);
9971 --processing_template_decl;
9972
9973 if (!dependent_p
9974 && !complete_type_or_else (context, NULL_TREE))
9975 return error_mark_node;
9976
9977 if (COMPLETE_TYPE_P (context))
9978 {
9979 tree fn = new_friend;
9980 /* do_friend adds the TEMPLATE_DECL for any member friend
9981 template even if it isn't a member template, i.e.
9982 template <class T> friend A<T>::f();
9983 Look through it in that case. */
9984 if (TREE_CODE (fn) == TEMPLATE_DECL
9985 && !PRIMARY_TEMPLATE_P (fn))
9986 fn = DECL_TEMPLATE_RESULT (fn);
9987 /* Check to see that the declaration is really present, and,
9988 possibly obtain an improved declaration. */
9989 fn = check_classfn (context, fn, NULL_TREE);
9990
9991 if (fn)
9992 new_friend = fn;
9993 }
9994 }
9995
9996 return new_friend;
9997 }
9998
9999 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10000 template arguments, as for tsubst.
10001
10002 Returns an appropriate tsubst'd friend type or error_mark_node on
10003 failure. */
10004
10005 static tree
10006 tsubst_friend_class (tree friend_tmpl, tree args)
10007 {
10008 tree tmpl;
10009
10010 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10011 {
10012 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10013 return TREE_TYPE (tmpl);
10014 }
10015
10016 tree context = CP_DECL_CONTEXT (friend_tmpl);
10017 if (TREE_CODE (context) == NAMESPACE_DECL)
10018 push_nested_namespace (context);
10019 else
10020 push_nested_class (context);
10021
10022 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10023 /*non_class=*/false, /*block_p=*/false,
10024 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10025
10026 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10027 {
10028 /* The friend template has already been declared. Just
10029 check to see that the declarations match, and install any new
10030 default parameters. We must tsubst the default parameters,
10031 of course. We only need the innermost template parameters
10032 because that is all that redeclare_class_template will look
10033 at. */
10034 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10035 > TMPL_ARGS_DEPTH (args))
10036 {
10037 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10038 args, tf_warning_or_error);
10039 location_t saved_input_location = input_location;
10040 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10041 tree cons = get_constraints (tmpl);
10042 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10043 input_location = saved_input_location;
10044 }
10045 }
10046 else
10047 {
10048 /* The friend template has not already been declared. In this
10049 case, the instantiation of the template class will cause the
10050 injection of this template into the namespace scope. */
10051 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10052
10053 if (tmpl != error_mark_node)
10054 {
10055 /* The new TMPL is not an instantiation of anything, so we
10056 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10057 for the new type because that is supposed to be the
10058 corresponding template decl, i.e., TMPL. */
10059 DECL_USE_TEMPLATE (tmpl) = 0;
10060 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10061 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10062 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10063 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10064
10065 /* It is hidden. */
10066 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10067 DECL_ANTICIPATED (tmpl)
10068 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10069
10070 /* Inject this template into the enclosing namspace scope. */
10071 tmpl = pushdecl_namespace_level (tmpl, true);
10072 }
10073 }
10074
10075 if (TREE_CODE (context) == NAMESPACE_DECL)
10076 pop_nested_namespace (context);
10077 else
10078 pop_nested_class ();
10079
10080 return TREE_TYPE (tmpl);
10081 }
10082
10083 /* Returns zero if TYPE cannot be completed later due to circularity.
10084 Otherwise returns one. */
10085
10086 static int
10087 can_complete_type_without_circularity (tree type)
10088 {
10089 if (type == NULL_TREE || type == error_mark_node)
10090 return 0;
10091 else if (COMPLETE_TYPE_P (type))
10092 return 1;
10093 else if (TREE_CODE (type) == ARRAY_TYPE)
10094 return can_complete_type_without_circularity (TREE_TYPE (type));
10095 else if (CLASS_TYPE_P (type)
10096 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10097 return 0;
10098 else
10099 return 1;
10100 }
10101
10102 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10103 tsubst_flags_t, tree);
10104
10105 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10106 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10107
10108 static tree
10109 tsubst_attribute (tree t, tree *decl_p, tree args,
10110 tsubst_flags_t complain, tree in_decl)
10111 {
10112 gcc_assert (ATTR_IS_DEPENDENT (t));
10113
10114 tree val = TREE_VALUE (t);
10115 if (val == NULL_TREE)
10116 /* Nothing to do. */;
10117 else if ((flag_openmp || flag_openmp_simd)
10118 && is_attribute_p ("omp declare simd",
10119 get_attribute_name (t)))
10120 {
10121 tree clauses = TREE_VALUE (val);
10122 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10123 complain, in_decl);
10124 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10125 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10126 tree parms = DECL_ARGUMENTS (*decl_p);
10127 clauses
10128 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10129 if (clauses)
10130 val = build_tree_list (NULL_TREE, clauses);
10131 else
10132 val = NULL_TREE;
10133 }
10134 /* If the first attribute argument is an identifier, don't
10135 pass it through tsubst. Attributes like mode, format,
10136 cleanup and several target specific attributes expect it
10137 unmodified. */
10138 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10139 {
10140 tree chain
10141 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10142 /*integral_constant_expression_p=*/false);
10143 if (chain != TREE_CHAIN (val))
10144 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10145 }
10146 else if (PACK_EXPANSION_P (val))
10147 {
10148 /* An attribute pack expansion. */
10149 tree purp = TREE_PURPOSE (t);
10150 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10151 if (pack == error_mark_node)
10152 return error_mark_node;
10153 int len = TREE_VEC_LENGTH (pack);
10154 tree list = NULL_TREE;
10155 tree *q = &list;
10156 for (int i = 0; i < len; ++i)
10157 {
10158 tree elt = TREE_VEC_ELT (pack, i);
10159 *q = build_tree_list (purp, elt);
10160 q = &TREE_CHAIN (*q);
10161 }
10162 return list;
10163 }
10164 else
10165 val = tsubst_expr (val, args, complain, in_decl,
10166 /*integral_constant_expression_p=*/false);
10167
10168 if (val != TREE_VALUE (t))
10169 return build_tree_list (TREE_PURPOSE (t), val);
10170 return t;
10171 }
10172
10173 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10174 unchanged or a new TREE_LIST chain. */
10175
10176 static tree
10177 tsubst_attributes (tree attributes, tree args,
10178 tsubst_flags_t complain, tree in_decl)
10179 {
10180 tree last_dep = NULL_TREE;
10181
10182 for (tree t = attributes; t; t = TREE_CHAIN (t))
10183 if (ATTR_IS_DEPENDENT (t))
10184 {
10185 last_dep = t;
10186 attributes = copy_list (attributes);
10187 break;
10188 }
10189
10190 if (last_dep)
10191 for (tree *p = &attributes; *p; )
10192 {
10193 tree t = *p;
10194 if (ATTR_IS_DEPENDENT (t))
10195 {
10196 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10197 if (subst != t)
10198 {
10199 *p = subst;
10200 do
10201 p = &TREE_CHAIN (*p);
10202 while (*p);
10203 *p = TREE_CHAIN (t);
10204 continue;
10205 }
10206 }
10207 p = &TREE_CHAIN (*p);
10208 }
10209
10210 return attributes;
10211 }
10212
10213 /* Apply any attributes which had to be deferred until instantiation
10214 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10215 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10216
10217 static void
10218 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10219 tree args, tsubst_flags_t complain, tree in_decl)
10220 {
10221 tree last_dep = NULL_TREE;
10222 tree t;
10223 tree *p;
10224
10225 if (attributes == NULL_TREE)
10226 return;
10227
10228 if (DECL_P (*decl_p))
10229 {
10230 if (TREE_TYPE (*decl_p) == error_mark_node)
10231 return;
10232 p = &DECL_ATTRIBUTES (*decl_p);
10233 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10234 to our attributes parameter. */
10235 gcc_assert (*p == attributes);
10236 }
10237 else
10238 {
10239 p = &TYPE_ATTRIBUTES (*decl_p);
10240 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10241 lookup_template_class_1, and should be preserved. */
10242 gcc_assert (*p != attributes);
10243 while (*p)
10244 p = &TREE_CHAIN (*p);
10245 }
10246
10247 for (t = attributes; t; t = TREE_CHAIN (t))
10248 if (ATTR_IS_DEPENDENT (t))
10249 {
10250 last_dep = t;
10251 attributes = copy_list (attributes);
10252 break;
10253 }
10254
10255 *p = attributes;
10256 if (last_dep)
10257 {
10258 tree late_attrs = NULL_TREE;
10259 tree *q = &late_attrs;
10260
10261 for (; *p; )
10262 {
10263 t = *p;
10264 if (ATTR_IS_DEPENDENT (t))
10265 {
10266 *p = TREE_CHAIN (t);
10267 TREE_CHAIN (t) = NULL_TREE;
10268 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10269 do
10270 q = &TREE_CHAIN (*q);
10271 while (*q);
10272 }
10273 else
10274 p = &TREE_CHAIN (t);
10275 }
10276
10277 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10278 }
10279 }
10280
10281 /* Perform (or defer) access check for typedefs that were referenced
10282 from within the template TMPL code.
10283 This is a subroutine of instantiate_decl and instantiate_class_template.
10284 TMPL is the template to consider and TARGS is the list of arguments of
10285 that template. */
10286
10287 static void
10288 perform_typedefs_access_check (tree tmpl, tree targs)
10289 {
10290 location_t saved_location;
10291 unsigned i;
10292 qualified_typedef_usage_t *iter;
10293
10294 if (!tmpl
10295 || (!CLASS_TYPE_P (tmpl)
10296 && TREE_CODE (tmpl) != FUNCTION_DECL))
10297 return;
10298
10299 saved_location = input_location;
10300 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10301 {
10302 tree type_decl = iter->typedef_decl;
10303 tree type_scope = iter->context;
10304
10305 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10306 continue;
10307
10308 if (uses_template_parms (type_decl))
10309 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10310 if (uses_template_parms (type_scope))
10311 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10312
10313 /* Make access check error messages point to the location
10314 of the use of the typedef. */
10315 input_location = iter->locus;
10316 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10317 type_decl, type_decl,
10318 tf_warning_or_error);
10319 }
10320 input_location = saved_location;
10321 }
10322
10323 static tree
10324 instantiate_class_template_1 (tree type)
10325 {
10326 tree templ, args, pattern, t, member;
10327 tree typedecl;
10328 tree pbinfo;
10329 tree base_list;
10330 unsigned int saved_maximum_field_alignment;
10331 tree fn_context;
10332
10333 if (type == error_mark_node)
10334 return error_mark_node;
10335
10336 if (COMPLETE_OR_OPEN_TYPE_P (type)
10337 || uses_template_parms (type))
10338 return type;
10339
10340 /* Figure out which template is being instantiated. */
10341 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10342 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10343
10344 /* Determine what specialization of the original template to
10345 instantiate. */
10346 t = most_specialized_partial_spec (type, tf_warning_or_error);
10347 if (t == error_mark_node)
10348 {
10349 TYPE_BEING_DEFINED (type) = 1;
10350 return error_mark_node;
10351 }
10352 else if (t)
10353 {
10354 /* This TYPE is actually an instantiation of a partial
10355 specialization. We replace the innermost set of ARGS with
10356 the arguments appropriate for substitution. For example,
10357 given:
10358
10359 template <class T> struct S {};
10360 template <class T> struct S<T*> {};
10361
10362 and supposing that we are instantiating S<int*>, ARGS will
10363 presently be {int*} -- but we need {int}. */
10364 pattern = TREE_TYPE (t);
10365 args = TREE_PURPOSE (t);
10366 }
10367 else
10368 {
10369 pattern = TREE_TYPE (templ);
10370 args = CLASSTYPE_TI_ARGS (type);
10371 }
10372
10373 /* If the template we're instantiating is incomplete, then clearly
10374 there's nothing we can do. */
10375 if (!COMPLETE_TYPE_P (pattern))
10376 return type;
10377
10378 /* If we've recursively instantiated too many templates, stop. */
10379 if (! push_tinst_level (type))
10380 return type;
10381
10382 /* Now we're really doing the instantiation. Mark the type as in
10383 the process of being defined. */
10384 TYPE_BEING_DEFINED (type) = 1;
10385
10386 /* We may be in the middle of deferred access check. Disable
10387 it now. */
10388 push_deferring_access_checks (dk_no_deferred);
10389
10390 int saved_unevaluated_operand = cp_unevaluated_operand;
10391 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10392
10393 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10394 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10395 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10396 fn_context = error_mark_node;
10397 if (!fn_context)
10398 push_to_top_level ();
10399 else
10400 {
10401 cp_unevaluated_operand = 0;
10402 c_inhibit_evaluation_warnings = 0;
10403 }
10404 /* Use #pragma pack from the template context. */
10405 saved_maximum_field_alignment = maximum_field_alignment;
10406 maximum_field_alignment = TYPE_PRECISION (pattern);
10407
10408 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10409
10410 /* Set the input location to the most specialized template definition.
10411 This is needed if tsubsting causes an error. */
10412 typedecl = TYPE_MAIN_DECL (pattern);
10413 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10414 DECL_SOURCE_LOCATION (typedecl);
10415
10416 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10417 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10418 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10419 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10420 if (ANON_AGGR_TYPE_P (pattern))
10421 SET_ANON_AGGR_TYPE_P (type);
10422 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10423 {
10424 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10425 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10426 /* Adjust visibility for template arguments. */
10427 determine_visibility (TYPE_MAIN_DECL (type));
10428 }
10429 if (CLASS_TYPE_P (type))
10430 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10431
10432 pbinfo = TYPE_BINFO (pattern);
10433
10434 /* We should never instantiate a nested class before its enclosing
10435 class; we need to look up the nested class by name before we can
10436 instantiate it, and that lookup should instantiate the enclosing
10437 class. */
10438 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10439 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10440
10441 base_list = NULL_TREE;
10442 if (BINFO_N_BASE_BINFOS (pbinfo))
10443 {
10444 tree pbase_binfo;
10445 tree pushed_scope;
10446 int i;
10447
10448 /* We must enter the scope containing the type, as that is where
10449 the accessibility of types named in dependent bases are
10450 looked up from. */
10451 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10452
10453 /* Substitute into each of the bases to determine the actual
10454 basetypes. */
10455 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10456 {
10457 tree base;
10458 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10459 tree expanded_bases = NULL_TREE;
10460 int idx, len = 1;
10461
10462 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10463 {
10464 expanded_bases =
10465 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10466 args, tf_error, NULL_TREE);
10467 if (expanded_bases == error_mark_node)
10468 continue;
10469
10470 len = TREE_VEC_LENGTH (expanded_bases);
10471 }
10472
10473 for (idx = 0; idx < len; idx++)
10474 {
10475 if (expanded_bases)
10476 /* Extract the already-expanded base class. */
10477 base = TREE_VEC_ELT (expanded_bases, idx);
10478 else
10479 /* Substitute to figure out the base class. */
10480 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10481 NULL_TREE);
10482
10483 if (base == error_mark_node)
10484 continue;
10485
10486 base_list = tree_cons (access, base, base_list);
10487 if (BINFO_VIRTUAL_P (pbase_binfo))
10488 TREE_TYPE (base_list) = integer_type_node;
10489 }
10490 }
10491
10492 /* The list is now in reverse order; correct that. */
10493 base_list = nreverse (base_list);
10494
10495 if (pushed_scope)
10496 pop_scope (pushed_scope);
10497 }
10498 /* Now call xref_basetypes to set up all the base-class
10499 information. */
10500 xref_basetypes (type, base_list);
10501
10502 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10503 (int) ATTR_FLAG_TYPE_IN_PLACE,
10504 args, tf_error, NULL_TREE);
10505 fixup_attribute_variants (type);
10506
10507 /* Now that our base classes are set up, enter the scope of the
10508 class, so that name lookups into base classes, etc. will work
10509 correctly. This is precisely analogous to what we do in
10510 begin_class_definition when defining an ordinary non-template
10511 class, except we also need to push the enclosing classes. */
10512 push_nested_class (type);
10513
10514 /* Now members are processed in the order of declaration. */
10515 for (member = CLASSTYPE_DECL_LIST (pattern);
10516 member; member = TREE_CHAIN (member))
10517 {
10518 tree t = TREE_VALUE (member);
10519
10520 if (TREE_PURPOSE (member))
10521 {
10522 if (TYPE_P (t))
10523 {
10524 /* Build new CLASSTYPE_NESTED_UTDS. */
10525
10526 tree newtag;
10527 bool class_template_p;
10528
10529 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10530 && TYPE_LANG_SPECIFIC (t)
10531 && CLASSTYPE_IS_TEMPLATE (t));
10532 /* If the member is a class template, then -- even after
10533 substitution -- there may be dependent types in the
10534 template argument list for the class. We increment
10535 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10536 that function will assume that no types are dependent
10537 when outside of a template. */
10538 if (class_template_p)
10539 ++processing_template_decl;
10540 newtag = tsubst (t, args, tf_error, NULL_TREE);
10541 if (class_template_p)
10542 --processing_template_decl;
10543 if (newtag == error_mark_node)
10544 continue;
10545
10546 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10547 {
10548 tree name = TYPE_IDENTIFIER (t);
10549
10550 if (class_template_p)
10551 /* Unfortunately, lookup_template_class sets
10552 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10553 instantiation (i.e., for the type of a member
10554 template class nested within a template class.)
10555 This behavior is required for
10556 maybe_process_partial_specialization to work
10557 correctly, but is not accurate in this case;
10558 the TAG is not an instantiation of anything.
10559 (The corresponding TEMPLATE_DECL is an
10560 instantiation, but the TYPE is not.) */
10561 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10562
10563 /* Now, we call pushtag to put this NEWTAG into the scope of
10564 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10565 pushtag calling push_template_decl. We don't have to do
10566 this for enums because it will already have been done in
10567 tsubst_enum. */
10568 if (name)
10569 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10570 pushtag (name, newtag, /*tag_scope=*/ts_current);
10571 }
10572 }
10573 else if (DECL_DECLARES_FUNCTION_P (t))
10574 {
10575 tree r;
10576
10577 if (TREE_CODE (t) == TEMPLATE_DECL)
10578 ++processing_template_decl;
10579 r = tsubst (t, args, tf_error, NULL_TREE);
10580 if (TREE_CODE (t) == TEMPLATE_DECL)
10581 --processing_template_decl;
10582 set_current_access_from_decl (r);
10583 finish_member_declaration (r);
10584 /* Instantiate members marked with attribute used. */
10585 if (r != error_mark_node && DECL_PRESERVE_P (r))
10586 mark_used (r);
10587 if (TREE_CODE (r) == FUNCTION_DECL
10588 && DECL_OMP_DECLARE_REDUCTION_P (r))
10589 cp_check_omp_declare_reduction (r);
10590 }
10591 else if (DECL_CLASS_TEMPLATE_P (t)
10592 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10593 /* A closure type for a lambda in a default argument for a
10594 member template. Ignore it; it will be instantiated with
10595 the default argument. */;
10596 else
10597 {
10598 /* Build new TYPE_FIELDS. */
10599 if (TREE_CODE (t) == STATIC_ASSERT)
10600 {
10601 tree condition;
10602
10603 ++c_inhibit_evaluation_warnings;
10604 condition =
10605 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10606 tf_warning_or_error, NULL_TREE,
10607 /*integral_constant_expression_p=*/true);
10608 --c_inhibit_evaluation_warnings;
10609
10610 finish_static_assert (condition,
10611 STATIC_ASSERT_MESSAGE (t),
10612 STATIC_ASSERT_SOURCE_LOCATION (t),
10613 /*member_p=*/true);
10614 }
10615 else if (TREE_CODE (t) != CONST_DECL)
10616 {
10617 tree r;
10618 tree vec = NULL_TREE;
10619 int len = 1;
10620
10621 /* The file and line for this declaration, to
10622 assist in error message reporting. Since we
10623 called push_tinst_level above, we don't need to
10624 restore these. */
10625 input_location = DECL_SOURCE_LOCATION (t);
10626
10627 if (TREE_CODE (t) == TEMPLATE_DECL)
10628 ++processing_template_decl;
10629 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10630 if (TREE_CODE (t) == TEMPLATE_DECL)
10631 --processing_template_decl;
10632
10633 if (TREE_CODE (r) == TREE_VEC)
10634 {
10635 /* A capture pack became multiple fields. */
10636 vec = r;
10637 len = TREE_VEC_LENGTH (vec);
10638 }
10639
10640 for (int i = 0; i < len; ++i)
10641 {
10642 if (vec)
10643 r = TREE_VEC_ELT (vec, i);
10644 if (VAR_P (r))
10645 {
10646 /* In [temp.inst]:
10647
10648 [t]he initialization (and any associated
10649 side-effects) of a static data member does
10650 not occur unless the static data member is
10651 itself used in a way that requires the
10652 definition of the static data member to
10653 exist.
10654
10655 Therefore, we do not substitute into the
10656 initialized for the static data member here. */
10657 finish_static_data_member_decl
10658 (r,
10659 /*init=*/NULL_TREE,
10660 /*init_const_expr_p=*/false,
10661 /*asmspec_tree=*/NULL_TREE,
10662 /*flags=*/0);
10663 /* Instantiate members marked with attribute used. */
10664 if (r != error_mark_node && DECL_PRESERVE_P (r))
10665 mark_used (r);
10666 }
10667 else if (TREE_CODE (r) == FIELD_DECL)
10668 {
10669 /* Determine whether R has a valid type and can be
10670 completed later. If R is invalid, then its type
10671 is replaced by error_mark_node. */
10672 tree rtype = TREE_TYPE (r);
10673 if (can_complete_type_without_circularity (rtype))
10674 complete_type (rtype);
10675
10676 if (!complete_or_array_type_p (rtype))
10677 {
10678 /* If R's type couldn't be completed and
10679 it isn't a flexible array member (whose
10680 type is incomplete by definition) give
10681 an error. */
10682 cxx_incomplete_type_error (r, rtype);
10683 TREE_TYPE (r) = error_mark_node;
10684 }
10685 }
10686
10687 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10688 such a thing will already have been added to the field
10689 list by tsubst_enum in finish_member_declaration in the
10690 CLASSTYPE_NESTED_UTDS case above. */
10691 if (!(TREE_CODE (r) == TYPE_DECL
10692 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10693 && DECL_ARTIFICIAL (r)))
10694 {
10695 set_current_access_from_decl (r);
10696 finish_member_declaration (r);
10697 }
10698 }
10699 }
10700 }
10701 }
10702 else
10703 {
10704 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10705 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10706 {
10707 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10708
10709 tree friend_type = t;
10710 bool adjust_processing_template_decl = false;
10711
10712 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10713 {
10714 /* template <class T> friend class C; */
10715 friend_type = tsubst_friend_class (friend_type, args);
10716 adjust_processing_template_decl = true;
10717 }
10718 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10719 {
10720 /* template <class T> friend class C::D; */
10721 friend_type = tsubst (friend_type, args,
10722 tf_warning_or_error, NULL_TREE);
10723 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10724 friend_type = TREE_TYPE (friend_type);
10725 adjust_processing_template_decl = true;
10726 }
10727 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10728 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10729 {
10730 /* This could be either
10731
10732 friend class T::C;
10733
10734 when dependent_type_p is false or
10735
10736 template <class U> friend class T::C;
10737
10738 otherwise. */
10739 /* Bump processing_template_decl in case this is something like
10740 template <class T> friend struct A<T>::B. */
10741 ++processing_template_decl;
10742 friend_type = tsubst (friend_type, args,
10743 tf_warning_or_error, NULL_TREE);
10744 if (dependent_type_p (friend_type))
10745 adjust_processing_template_decl = true;
10746 --processing_template_decl;
10747 }
10748 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10749 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10750 && TYPE_HIDDEN_P (friend_type))
10751 {
10752 /* friend class C;
10753
10754 where C hasn't been declared yet. Let's lookup name
10755 from namespace scope directly, bypassing any name that
10756 come from dependent base class. */
10757 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10758
10759 /* The call to xref_tag_from_type does injection for friend
10760 classes. */
10761 push_nested_namespace (ns);
10762 friend_type =
10763 xref_tag_from_type (friend_type, NULL_TREE,
10764 /*tag_scope=*/ts_current);
10765 pop_nested_namespace (ns);
10766 }
10767 else if (uses_template_parms (friend_type))
10768 /* friend class C<T>; */
10769 friend_type = tsubst (friend_type, args,
10770 tf_warning_or_error, NULL_TREE);
10771 /* Otherwise it's
10772
10773 friend class C;
10774
10775 where C is already declared or
10776
10777 friend class C<int>;
10778
10779 We don't have to do anything in these cases. */
10780
10781 if (adjust_processing_template_decl)
10782 /* Trick make_friend_class into realizing that the friend
10783 we're adding is a template, not an ordinary class. It's
10784 important that we use make_friend_class since it will
10785 perform some error-checking and output cross-reference
10786 information. */
10787 ++processing_template_decl;
10788
10789 if (friend_type != error_mark_node)
10790 make_friend_class (type, friend_type, /*complain=*/false);
10791
10792 if (adjust_processing_template_decl)
10793 --processing_template_decl;
10794 }
10795 else
10796 {
10797 /* Build new DECL_FRIENDLIST. */
10798 tree r;
10799
10800 /* The file and line for this declaration, to
10801 assist in error message reporting. Since we
10802 called push_tinst_level above, we don't need to
10803 restore these. */
10804 input_location = DECL_SOURCE_LOCATION (t);
10805
10806 if (TREE_CODE (t) == TEMPLATE_DECL)
10807 {
10808 ++processing_template_decl;
10809 push_deferring_access_checks (dk_no_check);
10810 }
10811
10812 r = tsubst_friend_function (t, args);
10813 add_friend (type, r, /*complain=*/false);
10814 if (TREE_CODE (t) == TEMPLATE_DECL)
10815 {
10816 pop_deferring_access_checks ();
10817 --processing_template_decl;
10818 }
10819 }
10820 }
10821 }
10822
10823 if (fn_context)
10824 {
10825 /* Restore these before substituting into the lambda capture
10826 initializers. */
10827 cp_unevaluated_operand = saved_unevaluated_operand;
10828 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10829 }
10830
10831 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10832 {
10833 tree decl = lambda_function (type);
10834 if (decl)
10835 {
10836 if (cxx_dialect >= cxx17)
10837 CLASSTYPE_LITERAL_P (type) = true;
10838
10839 if (!DECL_TEMPLATE_INFO (decl)
10840 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10841 {
10842 /* Set function_depth to avoid garbage collection. */
10843 ++function_depth;
10844 instantiate_decl (decl, /*defer_ok=*/false, false);
10845 --function_depth;
10846 }
10847
10848 /* We need to instantiate the capture list from the template
10849 after we've instantiated the closure members, but before we
10850 consider adding the conversion op. Also keep any captures
10851 that may have been added during instantiation of the op(). */
10852 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10853 tree tmpl_cap
10854 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10855 args, tf_warning_or_error, NULL_TREE,
10856 false, false);
10857
10858 LAMBDA_EXPR_CAPTURE_LIST (expr)
10859 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10860
10861 maybe_add_lambda_conv_op (type);
10862 }
10863 else
10864 gcc_assert (errorcount);
10865 }
10866
10867 /* Set the file and line number information to whatever is given for
10868 the class itself. This puts error messages involving generated
10869 implicit functions at a predictable point, and the same point
10870 that would be used for non-template classes. */
10871 input_location = DECL_SOURCE_LOCATION (typedecl);
10872
10873 unreverse_member_declarations (type);
10874 finish_struct_1 (type);
10875 TYPE_BEING_DEFINED (type) = 0;
10876
10877 /* We don't instantiate default arguments for member functions. 14.7.1:
10878
10879 The implicit instantiation of a class template specialization causes
10880 the implicit instantiation of the declarations, but not of the
10881 definitions or default arguments, of the class member functions,
10882 member classes, static data members and member templates.... */
10883
10884 /* Some typedefs referenced from within the template code need to be access
10885 checked at template instantiation time, i.e now. These types were
10886 added to the template at parsing time. Let's get those and perform
10887 the access checks then. */
10888 perform_typedefs_access_check (pattern, args);
10889 perform_deferred_access_checks (tf_warning_or_error);
10890 pop_nested_class ();
10891 maximum_field_alignment = saved_maximum_field_alignment;
10892 if (!fn_context)
10893 pop_from_top_level ();
10894 pop_deferring_access_checks ();
10895 pop_tinst_level ();
10896
10897 /* The vtable for a template class can be emitted in any translation
10898 unit in which the class is instantiated. When there is no key
10899 method, however, finish_struct_1 will already have added TYPE to
10900 the keyed_classes. */
10901 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10902 vec_safe_push (keyed_classes, type);
10903
10904 return type;
10905 }
10906
10907 /* Wrapper for instantiate_class_template_1. */
10908
10909 tree
10910 instantiate_class_template (tree type)
10911 {
10912 tree ret;
10913 timevar_push (TV_TEMPLATE_INST);
10914 ret = instantiate_class_template_1 (type);
10915 timevar_pop (TV_TEMPLATE_INST);
10916 return ret;
10917 }
10918
10919 static tree
10920 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10921 {
10922 tree r;
10923
10924 if (!t)
10925 r = t;
10926 else if (TYPE_P (t))
10927 r = tsubst (t, args, complain, in_decl);
10928 else
10929 {
10930 if (!(complain & tf_warning))
10931 ++c_inhibit_evaluation_warnings;
10932 r = tsubst_expr (t, args, complain, in_decl,
10933 /*integral_constant_expression_p=*/true);
10934 if (!(complain & tf_warning))
10935 --c_inhibit_evaluation_warnings;
10936 }
10937 return r;
10938 }
10939
10940 /* Given a function parameter pack TMPL_PARM and some function parameters
10941 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10942 and set *SPEC_P to point at the next point in the list. */
10943
10944 tree
10945 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10946 {
10947 /* Collect all of the extra "packed" parameters into an
10948 argument pack. */
10949 tree parmvec;
10950 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10951 tree spec_parm = *spec_p;
10952 int i, len;
10953
10954 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10955 if (tmpl_parm
10956 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10957 break;
10958
10959 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10960 parmvec = make_tree_vec (len);
10961 spec_parm = *spec_p;
10962 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10963 TREE_VEC_ELT (parmvec, i) = spec_parm;
10964
10965 /* Build the argument packs. */
10966 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10967 *spec_p = spec_parm;
10968
10969 return argpack;
10970 }
10971
10972 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10973 NONTYPE_ARGUMENT_PACK. */
10974
10975 static tree
10976 make_fnparm_pack (tree spec_parm)
10977 {
10978 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10979 }
10980
10981 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10982 pack expansion with no extra args, 2 if it has extra args, or 0
10983 if it is not a pack expansion. */
10984
10985 static int
10986 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10987 {
10988 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10989 if (i >= TREE_VEC_LENGTH (vec))
10990 return 0;
10991 tree elt = TREE_VEC_ELT (vec, i);
10992 if (DECL_P (elt))
10993 /* A decl pack is itself an expansion. */
10994 elt = TREE_TYPE (elt);
10995 if (!PACK_EXPANSION_P (elt))
10996 return 0;
10997 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10998 return 2;
10999 return 1;
11000 }
11001
11002
11003 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11004
11005 static tree
11006 make_argument_pack_select (tree arg_pack, unsigned index)
11007 {
11008 tree aps = make_node (ARGUMENT_PACK_SELECT);
11009
11010 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11011 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11012
11013 return aps;
11014 }
11015
11016 /* This is a subroutine of tsubst_pack_expansion.
11017
11018 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11019 mechanism to store the (non complete list of) arguments of the
11020 substitution and return a non substituted pack expansion, in order
11021 to wait for when we have enough arguments to really perform the
11022 substitution. */
11023
11024 static bool
11025 use_pack_expansion_extra_args_p (tree parm_packs,
11026 int arg_pack_len,
11027 bool has_empty_arg)
11028 {
11029 /* If one pack has an expansion and another pack has a normal
11030 argument or if one pack has an empty argument and an another
11031 one hasn't then tsubst_pack_expansion cannot perform the
11032 substitution and need to fall back on the
11033 PACK_EXPANSION_EXTRA mechanism. */
11034 if (parm_packs == NULL_TREE)
11035 return false;
11036 else if (has_empty_arg)
11037 return true;
11038
11039 bool has_expansion_arg = false;
11040 for (int i = 0 ; i < arg_pack_len; ++i)
11041 {
11042 bool has_non_expansion_arg = false;
11043 for (tree parm_pack = parm_packs;
11044 parm_pack;
11045 parm_pack = TREE_CHAIN (parm_pack))
11046 {
11047 tree arg = TREE_VALUE (parm_pack);
11048
11049 int exp = argument_pack_element_is_expansion_p (arg, i);
11050 if (exp == 2)
11051 /* We can't substitute a pack expansion with extra args into
11052 our pattern. */
11053 return true;
11054 else if (exp)
11055 has_expansion_arg = true;
11056 else
11057 has_non_expansion_arg = true;
11058 }
11059
11060 if (has_expansion_arg && has_non_expansion_arg)
11061 return true;
11062 }
11063 return false;
11064 }
11065
11066 /* [temp.variadic]/6 says that:
11067
11068 The instantiation of a pack expansion [...]
11069 produces a list E1,E2, ..., En, where N is the number of elements
11070 in the pack expansion parameters.
11071
11072 This subroutine of tsubst_pack_expansion produces one of these Ei.
11073
11074 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11075 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11076 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11077 INDEX is the index 'i' of the element Ei to produce. ARGS,
11078 COMPLAIN, and IN_DECL are the same parameters as for the
11079 tsubst_pack_expansion function.
11080
11081 The function returns the resulting Ei upon successful completion,
11082 or error_mark_node.
11083
11084 Note that this function possibly modifies the ARGS parameter, so
11085 it's the responsibility of the caller to restore it. */
11086
11087 static tree
11088 gen_elem_of_pack_expansion_instantiation (tree pattern,
11089 tree parm_packs,
11090 unsigned index,
11091 tree args /* This parm gets
11092 modified. */,
11093 tsubst_flags_t complain,
11094 tree in_decl)
11095 {
11096 tree t;
11097 bool ith_elem_is_expansion = false;
11098
11099 /* For each parameter pack, change the substitution of the parameter
11100 pack to the ith argument in its argument pack, then expand the
11101 pattern. */
11102 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11103 {
11104 tree parm = TREE_PURPOSE (pack);
11105 tree arg_pack = TREE_VALUE (pack);
11106 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11107
11108 ith_elem_is_expansion |=
11109 argument_pack_element_is_expansion_p (arg_pack, index);
11110
11111 /* Select the Ith argument from the pack. */
11112 if (TREE_CODE (parm) == PARM_DECL
11113 || TREE_CODE (parm) == FIELD_DECL)
11114 {
11115 if (index == 0)
11116 {
11117 aps = make_argument_pack_select (arg_pack, index);
11118 if (!mark_used (parm, complain) && !(complain & tf_error))
11119 return error_mark_node;
11120 register_local_specialization (aps, parm);
11121 }
11122 else
11123 aps = retrieve_local_specialization (parm);
11124 }
11125 else
11126 {
11127 int idx, level;
11128 template_parm_level_and_index (parm, &level, &idx);
11129
11130 if (index == 0)
11131 {
11132 aps = make_argument_pack_select (arg_pack, index);
11133 /* Update the corresponding argument. */
11134 TMPL_ARG (args, level, idx) = aps;
11135 }
11136 else
11137 /* Re-use the ARGUMENT_PACK_SELECT. */
11138 aps = TMPL_ARG (args, level, idx);
11139 }
11140 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11141 }
11142
11143 /* Substitute into the PATTERN with the (possibly altered)
11144 arguments. */
11145 if (pattern == in_decl)
11146 /* Expanding a fixed parameter pack from
11147 coerce_template_parameter_pack. */
11148 t = tsubst_decl (pattern, args, complain);
11149 else if (pattern == error_mark_node)
11150 t = error_mark_node;
11151 else if (constraint_p (pattern))
11152 {
11153 if (processing_template_decl)
11154 t = tsubst_constraint (pattern, args, complain, in_decl);
11155 else
11156 t = (constraints_satisfied_p (pattern, args)
11157 ? boolean_true_node : boolean_false_node);
11158 }
11159 else if (!TYPE_P (pattern))
11160 t = tsubst_expr (pattern, args, complain, in_decl,
11161 /*integral_constant_expression_p=*/false);
11162 else
11163 t = tsubst (pattern, args, complain, in_decl);
11164
11165 /* If the Ith argument pack element is a pack expansion, then
11166 the Ith element resulting from the substituting is going to
11167 be a pack expansion as well. */
11168 if (ith_elem_is_expansion)
11169 t = make_pack_expansion (t, complain);
11170
11171 return t;
11172 }
11173
11174 /* When the unexpanded parameter pack in a fold expression expands to an empty
11175 sequence, the value of the expression is as follows; the program is
11176 ill-formed if the operator is not listed in this table.
11177
11178 && true
11179 || false
11180 , void() */
11181
11182 tree
11183 expand_empty_fold (tree t, tsubst_flags_t complain)
11184 {
11185 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11186 if (!FOLD_EXPR_MODIFY_P (t))
11187 switch (code)
11188 {
11189 case TRUTH_ANDIF_EXPR:
11190 return boolean_true_node;
11191 case TRUTH_ORIF_EXPR:
11192 return boolean_false_node;
11193 case COMPOUND_EXPR:
11194 return void_node;
11195 default:
11196 break;
11197 }
11198
11199 if (complain & tf_error)
11200 error_at (location_of (t),
11201 "fold of empty expansion over %O", code);
11202 return error_mark_node;
11203 }
11204
11205 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11206 form an expression that combines the two terms using the
11207 operator of T. */
11208
11209 static tree
11210 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11211 {
11212 tree op = FOLD_EXPR_OP (t);
11213 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11214
11215 // Handle compound assignment operators.
11216 if (FOLD_EXPR_MODIFY_P (t))
11217 return build_x_modify_expr (input_location, left, code, right, complain);
11218
11219 switch (code)
11220 {
11221 case COMPOUND_EXPR:
11222 return build_x_compound_expr (input_location, left, right, complain);
11223 case DOTSTAR_EXPR:
11224 return build_m_component_ref (left, right, complain);
11225 default:
11226 return build_x_binary_op (input_location, code,
11227 left, TREE_CODE (left),
11228 right, TREE_CODE (right),
11229 /*overload=*/NULL,
11230 complain);
11231 }
11232 }
11233
11234 /* Substitute ARGS into the pack of a fold expression T. */
11235
11236 static inline tree
11237 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11238 {
11239 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11240 }
11241
11242 /* Substitute ARGS into the pack of a fold expression T. */
11243
11244 static inline tree
11245 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11246 {
11247 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11248 }
11249
11250 /* Expand a PACK of arguments into a grouped as left fold.
11251 Given a pack containing elements A0, A1, ..., An and an
11252 operator @, this builds the expression:
11253
11254 ((A0 @ A1) @ A2) ... @ An
11255
11256 Note that PACK must not be empty.
11257
11258 The operator is defined by the original fold expression T. */
11259
11260 static tree
11261 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11262 {
11263 tree left = TREE_VEC_ELT (pack, 0);
11264 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11265 {
11266 tree right = TREE_VEC_ELT (pack, i);
11267 left = fold_expression (t, left, right, complain);
11268 }
11269 return left;
11270 }
11271
11272 /* Substitute into a unary left fold expression. */
11273
11274 static tree
11275 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11276 tree in_decl)
11277 {
11278 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11279 if (pack == error_mark_node)
11280 return error_mark_node;
11281 if (PACK_EXPANSION_P (pack))
11282 {
11283 tree r = copy_node (t);
11284 FOLD_EXPR_PACK (r) = pack;
11285 return r;
11286 }
11287 if (TREE_VEC_LENGTH (pack) == 0)
11288 return expand_empty_fold (t, complain);
11289 else
11290 return expand_left_fold (t, pack, complain);
11291 }
11292
11293 /* Substitute into a binary left fold expression.
11294
11295 Do ths by building a single (non-empty) vector of argumnts and
11296 building the expression from those elements. */
11297
11298 static tree
11299 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11300 tree in_decl)
11301 {
11302 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11303 if (pack == error_mark_node)
11304 return error_mark_node;
11305 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11306 if (init == error_mark_node)
11307 return error_mark_node;
11308
11309 if (PACK_EXPANSION_P (pack))
11310 {
11311 tree r = copy_node (t);
11312 FOLD_EXPR_PACK (r) = pack;
11313 FOLD_EXPR_INIT (r) = init;
11314 return r;
11315 }
11316
11317 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11318 TREE_VEC_ELT (vec, 0) = init;
11319 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11320 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11321
11322 return expand_left_fold (t, vec, complain);
11323 }
11324
11325 /* Expand a PACK of arguments into a grouped as right fold.
11326 Given a pack containing elementns A0, A1, ..., and an
11327 operator @, this builds the expression:
11328
11329 A0@ ... (An-2 @ (An-1 @ An))
11330
11331 Note that PACK must not be empty.
11332
11333 The operator is defined by the original fold expression T. */
11334
11335 tree
11336 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11337 {
11338 // Build the expression.
11339 int n = TREE_VEC_LENGTH (pack);
11340 tree right = TREE_VEC_ELT (pack, n - 1);
11341 for (--n; n != 0; --n)
11342 {
11343 tree left = TREE_VEC_ELT (pack, n - 1);
11344 right = fold_expression (t, left, right, complain);
11345 }
11346 return right;
11347 }
11348
11349 /* Substitute into a unary right fold expression. */
11350
11351 static tree
11352 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11353 tree in_decl)
11354 {
11355 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11356 if (pack == error_mark_node)
11357 return error_mark_node;
11358 if (PACK_EXPANSION_P (pack))
11359 {
11360 tree r = copy_node (t);
11361 FOLD_EXPR_PACK (r) = pack;
11362 return r;
11363 }
11364 if (TREE_VEC_LENGTH (pack) == 0)
11365 return expand_empty_fold (t, complain);
11366 else
11367 return expand_right_fold (t, pack, complain);
11368 }
11369
11370 /* Substitute into a binary right fold expression.
11371
11372 Do ths by building a single (non-empty) vector of arguments and
11373 building the expression from those elements. */
11374
11375 static tree
11376 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11377 tree in_decl)
11378 {
11379 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11380 if (pack == error_mark_node)
11381 return error_mark_node;
11382 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11383 if (init == error_mark_node)
11384 return error_mark_node;
11385
11386 if (PACK_EXPANSION_P (pack))
11387 {
11388 tree r = copy_node (t);
11389 FOLD_EXPR_PACK (r) = pack;
11390 FOLD_EXPR_INIT (r) = init;
11391 return r;
11392 }
11393
11394 int n = TREE_VEC_LENGTH (pack);
11395 tree vec = make_tree_vec (n + 1);
11396 for (int i = 0; i < n; ++i)
11397 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11398 TREE_VEC_ELT (vec, n) = init;
11399
11400 return expand_right_fold (t, vec, complain);
11401 }
11402
11403
11404 /* Substitute ARGS into T, which is an pack expansion
11405 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11406 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11407 (if only a partial substitution could be performed) or
11408 ERROR_MARK_NODE if there was an error. */
11409 tree
11410 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11411 tree in_decl)
11412 {
11413 tree pattern;
11414 tree pack, packs = NULL_TREE;
11415 bool unsubstituted_packs = false;
11416 int i, len = -1;
11417 tree result;
11418 hash_map<tree, tree> *saved_local_specializations = NULL;
11419 bool need_local_specializations = false;
11420 int levels;
11421
11422 gcc_assert (PACK_EXPANSION_P (t));
11423 pattern = PACK_EXPANSION_PATTERN (t);
11424
11425 /* Add in any args remembered from an earlier partial instantiation. */
11426 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11427
11428 levels = TMPL_ARGS_DEPTH (args);
11429
11430 /* Determine the argument packs that will instantiate the parameter
11431 packs used in the expansion expression. While we're at it,
11432 compute the number of arguments to be expanded and make sure it
11433 is consistent. */
11434 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11435 pack = TREE_CHAIN (pack))
11436 {
11437 tree parm_pack = TREE_VALUE (pack);
11438 tree arg_pack = NULL_TREE;
11439 tree orig_arg = NULL_TREE;
11440 int level = 0;
11441
11442 if (TREE_CODE (parm_pack) == BASES)
11443 {
11444 gcc_assert (parm_pack == pattern);
11445 if (BASES_DIRECT (parm_pack))
11446 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11447 args, complain, in_decl, false));
11448 else
11449 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11450 args, complain, in_decl, false));
11451 }
11452 else if (builtin_pack_call_p (parm_pack))
11453 {
11454 /* ??? Support use in other patterns. */
11455 gcc_assert (parm_pack == pattern);
11456 return expand_builtin_pack_call (parm_pack, args,
11457 complain, in_decl);
11458 }
11459 else if (TREE_CODE (parm_pack) == PARM_DECL)
11460 {
11461 /* We know we have correct local_specializations if this
11462 expansion is at function scope, or if we're dealing with a
11463 local parameter in a requires expression; for the latter,
11464 tsubst_requires_expr set it up appropriately. */
11465 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11466 arg_pack = retrieve_local_specialization (parm_pack);
11467 else
11468 /* We can't rely on local_specializations for a parameter
11469 name used later in a function declaration (such as in a
11470 late-specified return type). Even if it exists, it might
11471 have the wrong value for a recursive call. */
11472 need_local_specializations = true;
11473
11474 if (!arg_pack)
11475 {
11476 /* This parameter pack was used in an unevaluated context. Just
11477 make a dummy decl, since it's only used for its type. */
11478 arg_pack = tsubst_decl (parm_pack, args, complain);
11479 if (arg_pack && DECL_PACK_P (arg_pack))
11480 /* Partial instantiation of the parm_pack, we can't build
11481 up an argument pack yet. */
11482 arg_pack = NULL_TREE;
11483 else
11484 arg_pack = make_fnparm_pack (arg_pack);
11485 }
11486 }
11487 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11488 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11489 else
11490 {
11491 int idx;
11492 template_parm_level_and_index (parm_pack, &level, &idx);
11493
11494 if (level <= levels)
11495 arg_pack = TMPL_ARG (args, level, idx);
11496 }
11497
11498 orig_arg = arg_pack;
11499 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11500 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11501
11502 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11503 /* This can only happen if we forget to expand an argument
11504 pack somewhere else. Just return an error, silently. */
11505 {
11506 result = make_tree_vec (1);
11507 TREE_VEC_ELT (result, 0) = error_mark_node;
11508 return result;
11509 }
11510
11511 if (arg_pack)
11512 {
11513 int my_len =
11514 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11515
11516 /* Don't bother trying to do a partial substitution with
11517 incomplete packs; we'll try again after deduction. */
11518 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11519 return t;
11520
11521 if (len < 0)
11522 len = my_len;
11523 else if (len != my_len)
11524 {
11525 if (!(complain & tf_error))
11526 /* Fail quietly. */;
11527 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11528 error ("mismatched argument pack lengths while expanding %qT",
11529 pattern);
11530 else
11531 error ("mismatched argument pack lengths while expanding %qE",
11532 pattern);
11533 return error_mark_node;
11534 }
11535
11536 /* Keep track of the parameter packs and their corresponding
11537 argument packs. */
11538 packs = tree_cons (parm_pack, arg_pack, packs);
11539 TREE_TYPE (packs) = orig_arg;
11540 }
11541 else
11542 {
11543 /* We can't substitute for this parameter pack. We use a flag as
11544 well as the missing_level counter because function parameter
11545 packs don't have a level. */
11546 gcc_assert (processing_template_decl);
11547 unsubstituted_packs = true;
11548 }
11549 }
11550
11551 /* If the expansion is just T..., return the matching argument pack, unless
11552 we need to call convert_from_reference on all the elements. This is an
11553 important optimization; see c++/68422. */
11554 if (!unsubstituted_packs
11555 && TREE_PURPOSE (packs) == pattern)
11556 {
11557 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11558 /* Types need no adjustment, nor does sizeof..., and if we still have
11559 some pack expansion args we won't do anything yet. */
11560 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11561 || PACK_EXPANSION_SIZEOF_P (t)
11562 || pack_expansion_args_count (args))
11563 return args;
11564 /* Also optimize expression pack expansions if we can tell that the
11565 elements won't have reference type. */
11566 tree type = TREE_TYPE (pattern);
11567 if (type && TREE_CODE (type) != REFERENCE_TYPE
11568 && !PACK_EXPANSION_P (type)
11569 && !WILDCARD_TYPE_P (type))
11570 return args;
11571 /* Otherwise use the normal path so we get convert_from_reference. */
11572 }
11573
11574 /* We cannot expand this expansion expression, because we don't have
11575 all of the argument packs we need. */
11576 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11577 {
11578 /* We got some full packs, but we can't substitute them in until we
11579 have values for all the packs. So remember these until then. */
11580
11581 t = make_pack_expansion (pattern, complain);
11582 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11583 return t;
11584 }
11585 else if (unsubstituted_packs)
11586 {
11587 /* There were no real arguments, we're just replacing a parameter
11588 pack with another version of itself. Substitute into the
11589 pattern and return a PACK_EXPANSION_*. The caller will need to
11590 deal with that. */
11591 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11592 t = tsubst_expr (pattern, args, complain, in_decl,
11593 /*integral_constant_expression_p=*/false);
11594 else
11595 t = tsubst (pattern, args, complain, in_decl);
11596 t = make_pack_expansion (t, complain);
11597 return t;
11598 }
11599
11600 gcc_assert (len >= 0);
11601
11602 if (need_local_specializations)
11603 {
11604 /* We're in a late-specified return type, so create our own local
11605 specializations map; the current map is either NULL or (in the
11606 case of recursive unification) might have bindings that we don't
11607 want to use or alter. */
11608 saved_local_specializations = local_specializations;
11609 local_specializations = new hash_map<tree, tree>;
11610 }
11611
11612 /* For each argument in each argument pack, substitute into the
11613 pattern. */
11614 result = make_tree_vec (len);
11615 tree elem_args = copy_template_args (args);
11616 for (i = 0; i < len; ++i)
11617 {
11618 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11619 i,
11620 elem_args, complain,
11621 in_decl);
11622 TREE_VEC_ELT (result, i) = t;
11623 if (t == error_mark_node)
11624 {
11625 result = error_mark_node;
11626 break;
11627 }
11628 }
11629
11630 /* Update ARGS to restore the substitution from parameter packs to
11631 their argument packs. */
11632 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11633 {
11634 tree parm = TREE_PURPOSE (pack);
11635
11636 if (TREE_CODE (parm) == PARM_DECL
11637 || TREE_CODE (parm) == FIELD_DECL)
11638 register_local_specialization (TREE_TYPE (pack), parm);
11639 else
11640 {
11641 int idx, level;
11642
11643 if (TREE_VALUE (pack) == NULL_TREE)
11644 continue;
11645
11646 template_parm_level_and_index (parm, &level, &idx);
11647
11648 /* Update the corresponding argument. */
11649 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11650 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11651 TREE_TYPE (pack);
11652 else
11653 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11654 }
11655 }
11656
11657 if (need_local_specializations)
11658 {
11659 delete local_specializations;
11660 local_specializations = saved_local_specializations;
11661 }
11662
11663 /* If the dependent pack arguments were such that we end up with only a
11664 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11665 if (len == 1 && TREE_CODE (result) == TREE_VEC
11666 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11667 return TREE_VEC_ELT (result, 0);
11668
11669 return result;
11670 }
11671
11672 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11673 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11674 parameter packs; all parms generated from a function parameter pack will
11675 have the same DECL_PARM_INDEX. */
11676
11677 tree
11678 get_pattern_parm (tree parm, tree tmpl)
11679 {
11680 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11681 tree patparm;
11682
11683 if (DECL_ARTIFICIAL (parm))
11684 {
11685 for (patparm = DECL_ARGUMENTS (pattern);
11686 patparm; patparm = DECL_CHAIN (patparm))
11687 if (DECL_ARTIFICIAL (patparm)
11688 && DECL_NAME (parm) == DECL_NAME (patparm))
11689 break;
11690 }
11691 else
11692 {
11693 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11694 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11695 gcc_assert (DECL_PARM_INDEX (patparm)
11696 == DECL_PARM_INDEX (parm));
11697 }
11698
11699 return patparm;
11700 }
11701
11702 /* Make an argument pack out of the TREE_VEC VEC. */
11703
11704 static tree
11705 make_argument_pack (tree vec)
11706 {
11707 tree pack;
11708 tree elt = TREE_VEC_ELT (vec, 0);
11709 if (TYPE_P (elt))
11710 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11711 else
11712 {
11713 pack = make_node (NONTYPE_ARGUMENT_PACK);
11714 TREE_CONSTANT (pack) = 1;
11715 }
11716 SET_ARGUMENT_PACK_ARGS (pack, vec);
11717 return pack;
11718 }
11719
11720 /* Return an exact copy of template args T that can be modified
11721 independently. */
11722
11723 static tree
11724 copy_template_args (tree t)
11725 {
11726 if (t == error_mark_node)
11727 return t;
11728
11729 int len = TREE_VEC_LENGTH (t);
11730 tree new_vec = make_tree_vec (len);
11731
11732 for (int i = 0; i < len; ++i)
11733 {
11734 tree elt = TREE_VEC_ELT (t, i);
11735 if (elt && TREE_CODE (elt) == TREE_VEC)
11736 elt = copy_template_args (elt);
11737 TREE_VEC_ELT (new_vec, i) = elt;
11738 }
11739
11740 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11741 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11742
11743 return new_vec;
11744 }
11745
11746 /* Substitute ARGS into the vector or list of template arguments T. */
11747
11748 static tree
11749 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11750 {
11751 tree orig_t = t;
11752 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11753 tree *elts;
11754
11755 if (t == error_mark_node)
11756 return error_mark_node;
11757
11758 len = TREE_VEC_LENGTH (t);
11759 elts = XALLOCAVEC (tree, len);
11760
11761 for (i = 0; i < len; i++)
11762 {
11763 tree orig_arg = TREE_VEC_ELT (t, i);
11764 tree new_arg;
11765
11766 if (TREE_CODE (orig_arg) == TREE_VEC)
11767 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11768 else if (PACK_EXPANSION_P (orig_arg))
11769 {
11770 /* Substitute into an expansion expression. */
11771 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11772
11773 if (TREE_CODE (new_arg) == TREE_VEC)
11774 /* Add to the expanded length adjustment the number of
11775 expanded arguments. We subtract one from this
11776 measurement, because the argument pack expression
11777 itself is already counted as 1 in
11778 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11779 the argument pack is empty. */
11780 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11781 }
11782 else if (ARGUMENT_PACK_P (orig_arg))
11783 {
11784 /* Substitute into each of the arguments. */
11785 new_arg = TYPE_P (orig_arg)
11786 ? cxx_make_type (TREE_CODE (orig_arg))
11787 : make_node (TREE_CODE (orig_arg));
11788
11789 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11790 args, complain, in_decl);
11791 if (pack_args == error_mark_node)
11792 new_arg = error_mark_node;
11793 else
11794 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11795
11796 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11797 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11798 }
11799 else
11800 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11801
11802 if (new_arg == error_mark_node)
11803 return error_mark_node;
11804
11805 elts[i] = new_arg;
11806 if (new_arg != orig_arg)
11807 need_new = 1;
11808 }
11809
11810 if (!need_new)
11811 return t;
11812
11813 /* Make space for the expanded arguments coming from template
11814 argument packs. */
11815 t = make_tree_vec (len + expanded_len_adjust);
11816 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11817 arguments for a member template.
11818 In that case each TREE_VEC in ORIG_T represents a level of template
11819 arguments, and ORIG_T won't carry any non defaulted argument count.
11820 It will rather be the nested TREE_VECs that will carry one.
11821 In other words, ORIG_T carries a non defaulted argument count only
11822 if it doesn't contain any nested TREE_VEC. */
11823 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11824 {
11825 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11826 count += expanded_len_adjust;
11827 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11828 }
11829 for (i = 0, out = 0; i < len; i++)
11830 {
11831 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11832 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11833 && TREE_CODE (elts[i]) == TREE_VEC)
11834 {
11835 int idx;
11836
11837 /* Now expand the template argument pack "in place". */
11838 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11839 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11840 }
11841 else
11842 {
11843 TREE_VEC_ELT (t, out) = elts[i];
11844 out++;
11845 }
11846 }
11847
11848 return t;
11849 }
11850
11851 /* Substitute ARGS into one level PARMS of template parameters. */
11852
11853 static tree
11854 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11855 {
11856 if (parms == error_mark_node)
11857 return error_mark_node;
11858
11859 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11860
11861 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11862 {
11863 tree tuple = TREE_VEC_ELT (parms, i);
11864
11865 if (tuple == error_mark_node)
11866 continue;
11867
11868 TREE_VEC_ELT (new_vec, i) =
11869 tsubst_template_parm (tuple, args, complain);
11870 }
11871
11872 return new_vec;
11873 }
11874
11875 /* Return the result of substituting ARGS into the template parameters
11876 given by PARMS. If there are m levels of ARGS and m + n levels of
11877 PARMS, then the result will contain n levels of PARMS. For
11878 example, if PARMS is `template <class T> template <class U>
11879 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11880 result will be `template <int*, double, class V>'. */
11881
11882 static tree
11883 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11884 {
11885 tree r = NULL_TREE;
11886 tree* new_parms;
11887
11888 /* When substituting into a template, we must set
11889 PROCESSING_TEMPLATE_DECL as the template parameters may be
11890 dependent if they are based on one-another, and the dependency
11891 predicates are short-circuit outside of templates. */
11892 ++processing_template_decl;
11893
11894 for (new_parms = &r;
11895 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11896 new_parms = &(TREE_CHAIN (*new_parms)),
11897 parms = TREE_CHAIN (parms))
11898 {
11899 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11900 args, complain);
11901 *new_parms =
11902 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11903 - TMPL_ARGS_DEPTH (args)),
11904 new_vec, NULL_TREE);
11905 }
11906
11907 --processing_template_decl;
11908
11909 return r;
11910 }
11911
11912 /* Return the result of substituting ARGS into one template parameter
11913 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11914 parameter and which TREE_PURPOSE is the default argument of the
11915 template parameter. */
11916
11917 static tree
11918 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11919 {
11920 tree default_value, parm_decl;
11921
11922 if (args == NULL_TREE
11923 || t == NULL_TREE
11924 || t == error_mark_node)
11925 return t;
11926
11927 gcc_assert (TREE_CODE (t) == TREE_LIST);
11928
11929 default_value = TREE_PURPOSE (t);
11930 parm_decl = TREE_VALUE (t);
11931
11932 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11933 if (TREE_CODE (parm_decl) == PARM_DECL
11934 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11935 parm_decl = error_mark_node;
11936 default_value = tsubst_template_arg (default_value, args,
11937 complain, NULL_TREE);
11938
11939 return build_tree_list (default_value, parm_decl);
11940 }
11941
11942 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11943 type T. If T is not an aggregate or enumeration type, it is
11944 handled as if by tsubst. IN_DECL is as for tsubst. If
11945 ENTERING_SCOPE is nonzero, T is the context for a template which
11946 we are presently tsubst'ing. Return the substituted value. */
11947
11948 static tree
11949 tsubst_aggr_type (tree t,
11950 tree args,
11951 tsubst_flags_t complain,
11952 tree in_decl,
11953 int entering_scope)
11954 {
11955 if (t == NULL_TREE)
11956 return NULL_TREE;
11957
11958 switch (TREE_CODE (t))
11959 {
11960 case RECORD_TYPE:
11961 if (TYPE_PTRMEMFUNC_P (t))
11962 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11963
11964 /* Fall through. */
11965 case ENUMERAL_TYPE:
11966 case UNION_TYPE:
11967 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11968 {
11969 tree argvec;
11970 tree context;
11971 tree r;
11972 int saved_unevaluated_operand;
11973 int saved_inhibit_evaluation_warnings;
11974
11975 /* In "sizeof(X<I>)" we need to evaluate "I". */
11976 saved_unevaluated_operand = cp_unevaluated_operand;
11977 cp_unevaluated_operand = 0;
11978 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11979 c_inhibit_evaluation_warnings = 0;
11980
11981 /* First, determine the context for the type we are looking
11982 up. */
11983 context = TYPE_CONTEXT (t);
11984 if (context && TYPE_P (context))
11985 {
11986 context = tsubst_aggr_type (context, args, complain,
11987 in_decl, /*entering_scope=*/1);
11988 /* If context is a nested class inside a class template,
11989 it may still need to be instantiated (c++/33959). */
11990 context = complete_type (context);
11991 }
11992
11993 /* Then, figure out what arguments are appropriate for the
11994 type we are trying to find. For example, given:
11995
11996 template <class T> struct S;
11997 template <class T, class U> void f(T, U) { S<U> su; }
11998
11999 and supposing that we are instantiating f<int, double>,
12000 then our ARGS will be {int, double}, but, when looking up
12001 S we only want {double}. */
12002 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12003 complain, in_decl);
12004 if (argvec == error_mark_node)
12005 r = error_mark_node;
12006 else
12007 {
12008 r = lookup_template_class (t, argvec, in_decl, context,
12009 entering_scope, complain);
12010 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12011 }
12012
12013 cp_unevaluated_operand = saved_unevaluated_operand;
12014 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12015
12016 return r;
12017 }
12018 else
12019 /* This is not a template type, so there's nothing to do. */
12020 return t;
12021
12022 default:
12023 return tsubst (t, args, complain, in_decl);
12024 }
12025 }
12026
12027 static GTY((cache)) tree_cache_map *defarg_inst;
12028
12029 /* Substitute into the default argument ARG (a default argument for
12030 FN), which has the indicated TYPE. */
12031
12032 tree
12033 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12034 tsubst_flags_t complain)
12035 {
12036 tree saved_class_ptr = NULL_TREE;
12037 tree saved_class_ref = NULL_TREE;
12038 int errs = errorcount + sorrycount;
12039
12040 /* This can happen in invalid code. */
12041 if (TREE_CODE (arg) == DEFAULT_ARG)
12042 return arg;
12043
12044 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12045 parm = chain_index (parmnum, parm);
12046 tree parmtype = TREE_TYPE (parm);
12047 if (DECL_BY_REFERENCE (parm))
12048 parmtype = TREE_TYPE (parmtype);
12049 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12050
12051 tree *slot;
12052 if (defarg_inst && (slot = defarg_inst->get (parm)))
12053 return *slot;
12054
12055 /* This default argument came from a template. Instantiate the
12056 default argument here, not in tsubst. In the case of
12057 something like:
12058
12059 template <class T>
12060 struct S {
12061 static T t();
12062 void f(T = t());
12063 };
12064
12065 we must be careful to do name lookup in the scope of S<T>,
12066 rather than in the current class. */
12067 push_access_scope (fn);
12068 /* The "this" pointer is not valid in a default argument. */
12069 if (cfun)
12070 {
12071 saved_class_ptr = current_class_ptr;
12072 cp_function_chain->x_current_class_ptr = NULL_TREE;
12073 saved_class_ref = current_class_ref;
12074 cp_function_chain->x_current_class_ref = NULL_TREE;
12075 }
12076
12077 start_lambda_scope (parm);
12078
12079 push_deferring_access_checks(dk_no_deferred);
12080 /* The default argument expression may cause implicitly defined
12081 member functions to be synthesized, which will result in garbage
12082 collection. We must treat this situation as if we were within
12083 the body of function so as to avoid collecting live data on the
12084 stack. */
12085 ++function_depth;
12086 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12087 complain, NULL_TREE,
12088 /*integral_constant_expression_p=*/false);
12089 --function_depth;
12090 pop_deferring_access_checks();
12091
12092 finish_lambda_scope ();
12093
12094 /* Restore the "this" pointer. */
12095 if (cfun)
12096 {
12097 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12098 cp_function_chain->x_current_class_ref = saved_class_ref;
12099 }
12100
12101 if (errorcount+sorrycount > errs
12102 && (complain & tf_warning_or_error))
12103 inform (input_location,
12104 " when instantiating default argument for call to %qD", fn);
12105
12106 /* Make sure the default argument is reasonable. */
12107 arg = check_default_argument (type, arg, complain);
12108
12109 pop_access_scope (fn);
12110
12111 if (arg != error_mark_node && !cp_unevaluated_operand)
12112 {
12113 if (!defarg_inst)
12114 defarg_inst = tree_cache_map::create_ggc (37);
12115 defarg_inst->put (parm, arg);
12116 }
12117
12118 return arg;
12119 }
12120
12121 /* Substitute into all the default arguments for FN. */
12122
12123 static void
12124 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12125 {
12126 tree arg;
12127 tree tmpl_args;
12128
12129 tmpl_args = DECL_TI_ARGS (fn);
12130
12131 /* If this function is not yet instantiated, we certainly don't need
12132 its default arguments. */
12133 if (uses_template_parms (tmpl_args))
12134 return;
12135 /* Don't do this again for clones. */
12136 if (DECL_CLONED_FUNCTION_P (fn))
12137 return;
12138
12139 int i = 0;
12140 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12141 arg;
12142 arg = TREE_CHAIN (arg), ++i)
12143 if (TREE_PURPOSE (arg))
12144 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12145 TREE_VALUE (arg),
12146 TREE_PURPOSE (arg),
12147 complain);
12148 }
12149
12150 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12151
12152 static tree
12153 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12154 tree lambda_fntype)
12155 {
12156 tree gen_tmpl, argvec;
12157 hashval_t hash = 0;
12158 tree in_decl = t;
12159
12160 /* Nobody should be tsubst'ing into non-template functions. */
12161 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12162
12163 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12164 {
12165 /* If T is not dependent, just return it. */
12166 if (!uses_template_parms (DECL_TI_ARGS (t)))
12167 return t;
12168
12169 /* Calculate the most general template of which R is a
12170 specialization, and the complete set of arguments used to
12171 specialize R. */
12172 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12173 argvec = tsubst_template_args (DECL_TI_ARGS
12174 (DECL_TEMPLATE_RESULT
12175 (DECL_TI_TEMPLATE (t))),
12176 args, complain, in_decl);
12177 if (argvec == error_mark_node)
12178 return error_mark_node;
12179
12180 /* Check to see if we already have this specialization. */
12181 if (!lambda_fntype)
12182 {
12183 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12184 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12185 return spec;
12186 }
12187
12188 /* We can see more levels of arguments than parameters if
12189 there was a specialization of a member template, like
12190 this:
12191
12192 template <class T> struct S { template <class U> void f(); }
12193 template <> template <class U> void S<int>::f(U);
12194
12195 Here, we'll be substituting into the specialization,
12196 because that's where we can find the code we actually
12197 want to generate, but we'll have enough arguments for
12198 the most general template.
12199
12200 We also deal with the peculiar case:
12201
12202 template <class T> struct S {
12203 template <class U> friend void f();
12204 };
12205 template <class U> void f() {}
12206 template S<int>;
12207 template void f<double>();
12208
12209 Here, the ARGS for the instantiation of will be {int,
12210 double}. But, we only need as many ARGS as there are
12211 levels of template parameters in CODE_PATTERN. We are
12212 careful not to get fooled into reducing the ARGS in
12213 situations like:
12214
12215 template <class T> struct S { template <class U> void f(U); }
12216 template <class T> template <> void S<T>::f(int) {}
12217
12218 which we can spot because the pattern will be a
12219 specialization in this case. */
12220 int args_depth = TMPL_ARGS_DEPTH (args);
12221 int parms_depth =
12222 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12223
12224 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12225 args = get_innermost_template_args (args, parms_depth);
12226 }
12227 else
12228 {
12229 /* This special case arises when we have something like this:
12230
12231 template <class T> struct S {
12232 friend void f<int>(int, double);
12233 };
12234
12235 Here, the DECL_TI_TEMPLATE for the friend declaration
12236 will be an IDENTIFIER_NODE. We are being called from
12237 tsubst_friend_function, and we want only to create a
12238 new decl (R) with appropriate types so that we can call
12239 determine_specialization. */
12240 gen_tmpl = NULL_TREE;
12241 argvec = NULL_TREE;
12242 }
12243
12244 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12245 : NULL_TREE);
12246 tree ctx = closure ? closure : DECL_CONTEXT (t);
12247 bool member = ctx && TYPE_P (ctx);
12248
12249 if (member && !closure)
12250 ctx = tsubst_aggr_type (ctx, args,
12251 complain, t, /*entering_scope=*/1);
12252
12253 tree type = (lambda_fntype ? lambda_fntype
12254 : tsubst (TREE_TYPE (t), args,
12255 complain | tf_fndecl_type, in_decl));
12256 if (type == error_mark_node)
12257 return error_mark_node;
12258
12259 /* If we hit excessive deduction depth, the type is bogus even if
12260 it isn't error_mark_node, so don't build a decl. */
12261 if (excessive_deduction_depth)
12262 return error_mark_node;
12263
12264 /* We do NOT check for matching decls pushed separately at this
12265 point, as they may not represent instantiations of this
12266 template, and in any case are considered separate under the
12267 discrete model. */
12268 tree r = copy_decl (t);
12269 DECL_USE_TEMPLATE (r) = 0;
12270 TREE_TYPE (r) = type;
12271 /* Clear out the mangled name and RTL for the instantiation. */
12272 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12273 SET_DECL_RTL (r, NULL);
12274 /* Leave DECL_INITIAL set on deleted instantiations. */
12275 if (!DECL_DELETED_FN (r))
12276 DECL_INITIAL (r) = NULL_TREE;
12277 DECL_CONTEXT (r) = ctx;
12278
12279 /* OpenMP UDRs have the only argument a reference to the declared
12280 type. We want to diagnose if the declared type is a reference,
12281 which is invalid, but as references to references are usually
12282 quietly merged, diagnose it here. */
12283 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12284 {
12285 tree argtype
12286 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12287 argtype = tsubst (argtype, args, complain, in_decl);
12288 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12289 error_at (DECL_SOURCE_LOCATION (t),
12290 "reference type %qT in "
12291 "%<#pragma omp declare reduction%>", argtype);
12292 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12293 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12294 argtype);
12295 }
12296
12297 if (member && DECL_CONV_FN_P (r))
12298 /* Type-conversion operator. Reconstruct the name, in
12299 case it's the name of one of the template's parameters. */
12300 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12301
12302 tree parms = DECL_ARGUMENTS (t);
12303 if (closure)
12304 parms = DECL_CHAIN (parms);
12305 parms = tsubst (parms, args, complain, t);
12306 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12307 DECL_CONTEXT (parm) = r;
12308 if (closure)
12309 {
12310 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12311 DECL_CHAIN (tparm) = parms;
12312 parms = tparm;
12313 }
12314 DECL_ARGUMENTS (r) = parms;
12315 DECL_RESULT (r) = NULL_TREE;
12316
12317 TREE_STATIC (r) = 0;
12318 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12319 DECL_EXTERNAL (r) = 1;
12320 /* If this is an instantiation of a function with internal
12321 linkage, we already know what object file linkage will be
12322 assigned to the instantiation. */
12323 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12324 DECL_DEFER_OUTPUT (r) = 0;
12325 DECL_CHAIN (r) = NULL_TREE;
12326 DECL_PENDING_INLINE_INFO (r) = 0;
12327 DECL_PENDING_INLINE_P (r) = 0;
12328 DECL_SAVED_TREE (r) = NULL_TREE;
12329 DECL_STRUCT_FUNCTION (r) = NULL;
12330 TREE_USED (r) = 0;
12331 /* We'll re-clone as appropriate in instantiate_template. */
12332 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12333
12334 /* If we aren't complaining now, return on error before we register
12335 the specialization so that we'll complain eventually. */
12336 if ((complain & tf_error) == 0
12337 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12338 && !grok_op_properties (r, /*complain=*/false))
12339 return error_mark_node;
12340
12341 /* When instantiating a constrained member, substitute
12342 into the constraints to create a new constraint. */
12343 if (tree ci = get_constraints (t))
12344 if (member)
12345 {
12346 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12347 set_constraints (r, ci);
12348 }
12349
12350 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12351 this in the special friend case mentioned above where
12352 GEN_TMPL is NULL. */
12353 if (gen_tmpl && !closure)
12354 {
12355 DECL_TEMPLATE_INFO (r)
12356 = build_template_info (gen_tmpl, argvec);
12357 SET_DECL_IMPLICIT_INSTANTIATION (r);
12358
12359 tree new_r
12360 = register_specialization (r, gen_tmpl, argvec, false, hash);
12361 if (new_r != r)
12362 /* We instantiated this while substituting into
12363 the type earlier (template/friend54.C). */
12364 return new_r;
12365
12366 /* We're not supposed to instantiate default arguments
12367 until they are called, for a template. But, for a
12368 declaration like:
12369
12370 template <class T> void f ()
12371 { extern void g(int i = T()); }
12372
12373 we should do the substitution when the template is
12374 instantiated. We handle the member function case in
12375 instantiate_class_template since the default arguments
12376 might refer to other members of the class. */
12377 if (!member
12378 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12379 && !uses_template_parms (argvec))
12380 tsubst_default_arguments (r, complain);
12381 }
12382 else
12383 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12384
12385 /* Copy the list of befriending classes. */
12386 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12387 *friends;
12388 friends = &TREE_CHAIN (*friends))
12389 {
12390 *friends = copy_node (*friends);
12391 TREE_VALUE (*friends)
12392 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12393 }
12394
12395 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12396 {
12397 maybe_retrofit_in_chrg (r);
12398 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12399 return error_mark_node;
12400 /* If this is an instantiation of a member template, clone it.
12401 If it isn't, that'll be handled by
12402 clone_constructors_and_destructors. */
12403 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12404 clone_function_decl (r, /*update_methods=*/false);
12405 }
12406 else if ((complain & tf_error) != 0
12407 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12408 && !grok_op_properties (r, /*complain=*/true))
12409 return error_mark_node;
12410
12411 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12412 SET_DECL_FRIEND_CONTEXT (r,
12413 tsubst (DECL_FRIEND_CONTEXT (t),
12414 args, complain, in_decl));
12415
12416 /* Possibly limit visibility based on template args. */
12417 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12418 if (DECL_VISIBILITY_SPECIFIED (t))
12419 {
12420 DECL_VISIBILITY_SPECIFIED (r) = 0;
12421 DECL_ATTRIBUTES (r)
12422 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12423 }
12424 determine_visibility (r);
12425 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12426 && !processing_template_decl)
12427 defaulted_late_check (r);
12428
12429 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12430 args, complain, in_decl);
12431 return r;
12432 }
12433
12434 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12435
12436 static tree
12437 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12438 tree lambda_fntype)
12439 {
12440 /* We can get here when processing a member function template,
12441 member class template, or template template parameter. */
12442 tree decl = DECL_TEMPLATE_RESULT (t);
12443 tree in_decl = t;
12444 tree spec;
12445 tree tmpl_args;
12446 tree full_args;
12447 tree r;
12448 hashval_t hash = 0;
12449
12450 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12451 {
12452 /* Template template parameter is treated here. */
12453 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12454 if (new_type == error_mark_node)
12455 r = error_mark_node;
12456 /* If we get a real template back, return it. This can happen in
12457 the context of most_specialized_partial_spec. */
12458 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12459 r = new_type;
12460 else
12461 /* The new TEMPLATE_DECL was built in
12462 reduce_template_parm_level. */
12463 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12464 return r;
12465 }
12466
12467 if (!lambda_fntype)
12468 {
12469 /* We might already have an instance of this template.
12470 The ARGS are for the surrounding class type, so the
12471 full args contain the tsubst'd args for the context,
12472 plus the innermost args from the template decl. */
12473 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12474 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12475 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12476 /* Because this is a template, the arguments will still be
12477 dependent, even after substitution. If
12478 PROCESSING_TEMPLATE_DECL is not set, the dependency
12479 predicates will short-circuit. */
12480 ++processing_template_decl;
12481 full_args = tsubst_template_args (tmpl_args, args,
12482 complain, in_decl);
12483 --processing_template_decl;
12484 if (full_args == error_mark_node)
12485 return error_mark_node;
12486
12487 /* If this is a default template template argument,
12488 tsubst might not have changed anything. */
12489 if (full_args == tmpl_args)
12490 return t;
12491
12492 hash = hash_tmpl_and_args (t, full_args);
12493 spec = retrieve_specialization (t, full_args, hash);
12494 if (spec != NULL_TREE)
12495 return spec;
12496 }
12497
12498 /* Make a new template decl. It will be similar to the
12499 original, but will record the current template arguments.
12500 We also create a new function declaration, which is just
12501 like the old one, but points to this new template, rather
12502 than the old one. */
12503 r = copy_decl (t);
12504 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12505 DECL_CHAIN (r) = NULL_TREE;
12506
12507 // Build new template info linking to the original template decl.
12508 if (!lambda_fntype)
12509 {
12510 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12511 SET_DECL_IMPLICIT_INSTANTIATION (r);
12512 }
12513 else
12514 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12515
12516 /* The template parameters for this new template are all the
12517 template parameters for the old template, except the
12518 outermost level of parameters. */
12519 DECL_TEMPLATE_PARMS (r)
12520 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12521 complain);
12522
12523 if (TREE_CODE (decl) == TYPE_DECL
12524 && !TYPE_DECL_ALIAS_P (decl))
12525 {
12526 tree new_type;
12527 ++processing_template_decl;
12528 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12529 --processing_template_decl;
12530 if (new_type == error_mark_node)
12531 return error_mark_node;
12532
12533 TREE_TYPE (r) = new_type;
12534 /* For a partial specialization, we need to keep pointing to
12535 the primary template. */
12536 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12537 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12538 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12539 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12540 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12541 }
12542 else
12543 {
12544 tree new_decl;
12545 ++processing_template_decl;
12546 if (TREE_CODE (decl) == FUNCTION_DECL)
12547 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12548 else
12549 new_decl = tsubst (decl, args, complain, in_decl);
12550 --processing_template_decl;
12551 if (new_decl == error_mark_node)
12552 return error_mark_node;
12553
12554 DECL_TEMPLATE_RESULT (r) = new_decl;
12555 TREE_TYPE (r) = TREE_TYPE (new_decl);
12556 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12557 if (lambda_fntype)
12558 {
12559 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12560 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12561 }
12562 else
12563 {
12564 DECL_TI_TEMPLATE (new_decl) = r;
12565 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12566 }
12567 }
12568
12569 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12570 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12571
12572 if (PRIMARY_TEMPLATE_P (t))
12573 DECL_PRIMARY_TEMPLATE (r) = r;
12574
12575 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12576 && !lambda_fntype)
12577 /* Record this non-type partial instantiation. */
12578 register_specialization (r, t,
12579 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12580 false, hash);
12581
12582 return r;
12583 }
12584
12585 /* True if FN is the op() for a lambda in an uninstantiated template. */
12586
12587 bool
12588 lambda_fn_in_template_p (tree fn)
12589 {
12590 if (!fn || !LAMBDA_FUNCTION_P (fn))
12591 return false;
12592 tree closure = DECL_CONTEXT (fn);
12593 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12594 }
12595
12596 /* True if FN is the op() for a lambda regenerated from a lambda in an
12597 uninstantiated template. */
12598
12599 bool
12600 regenerated_lambda_fn_p (tree fn)
12601 {
12602 return (LAMBDA_FUNCTION_P (fn)
12603 && !DECL_TEMPLATE_INSTANTIATION (fn));
12604 }
12605
12606 /* We're instantiating a variable from template function TCTX. Return the
12607 corresponding current enclosing scope. This gets complicated because lambda
12608 functions in templates are regenerated rather than instantiated, but generic
12609 lambda functions are subsequently instantiated. */
12610
12611 static tree
12612 enclosing_instantiation_of (tree tctx)
12613 {
12614 tree fn = current_function_decl;
12615 int lambda_count = 0;
12616
12617 for (; tctx && lambda_fn_in_template_p (tctx);
12618 tctx = decl_function_context (tctx))
12619 ++lambda_count;
12620 for (; fn; fn = decl_function_context (fn))
12621 {
12622 tree lambda = fn;
12623 int flambda_count = 0;
12624 for (; fn && regenerated_lambda_fn_p (fn);
12625 fn = decl_function_context (fn))
12626 ++flambda_count;
12627 if (DECL_TEMPLATE_INFO (fn)
12628 ? most_general_template (fn) != most_general_template (tctx)
12629 : fn != tctx)
12630 continue;
12631 if (lambda_count)
12632 {
12633 fn = lambda;
12634 while (flambda_count-- > lambda_count)
12635 fn = decl_function_context (fn);
12636 }
12637 return fn;
12638 }
12639 gcc_unreachable ();
12640 }
12641
12642 /* Substitute the ARGS into the T, which is a _DECL. Return the
12643 result of the substitution. Issue error and warning messages under
12644 control of COMPLAIN. */
12645
12646 static tree
12647 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12648 {
12649 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12650 location_t saved_loc;
12651 tree r = NULL_TREE;
12652 tree in_decl = t;
12653 hashval_t hash = 0;
12654
12655 /* Set the filename and linenumber to improve error-reporting. */
12656 saved_loc = input_location;
12657 input_location = DECL_SOURCE_LOCATION (t);
12658
12659 switch (TREE_CODE (t))
12660 {
12661 case TEMPLATE_DECL:
12662 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12663 break;
12664
12665 case FUNCTION_DECL:
12666 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12667 break;
12668
12669 case PARM_DECL:
12670 {
12671 tree type = NULL_TREE;
12672 int i, len = 1;
12673 tree expanded_types = NULL_TREE;
12674 tree prev_r = NULL_TREE;
12675 tree first_r = NULL_TREE;
12676
12677 if (DECL_PACK_P (t))
12678 {
12679 /* If there is a local specialization that isn't a
12680 parameter pack, it means that we're doing a "simple"
12681 substitution from inside tsubst_pack_expansion. Just
12682 return the local specialization (which will be a single
12683 parm). */
12684 tree spec = retrieve_local_specialization (t);
12685 if (spec
12686 && TREE_CODE (spec) == PARM_DECL
12687 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12688 RETURN (spec);
12689
12690 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12691 the parameters in this function parameter pack. */
12692 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12693 complain, in_decl);
12694 if (TREE_CODE (expanded_types) == TREE_VEC)
12695 {
12696 len = TREE_VEC_LENGTH (expanded_types);
12697
12698 /* Zero-length parameter packs are boring. Just substitute
12699 into the chain. */
12700 if (len == 0)
12701 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12702 TREE_CHAIN (t)));
12703 }
12704 else
12705 {
12706 /* All we did was update the type. Make a note of that. */
12707 type = expanded_types;
12708 expanded_types = NULL_TREE;
12709 }
12710 }
12711
12712 /* Loop through all of the parameters we'll build. When T is
12713 a function parameter pack, LEN is the number of expanded
12714 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12715 r = NULL_TREE;
12716 for (i = 0; i < len; ++i)
12717 {
12718 prev_r = r;
12719 r = copy_node (t);
12720 if (DECL_TEMPLATE_PARM_P (t))
12721 SET_DECL_TEMPLATE_PARM_P (r);
12722
12723 if (expanded_types)
12724 /* We're on the Ith parameter of the function parameter
12725 pack. */
12726 {
12727 /* Get the Ith type. */
12728 type = TREE_VEC_ELT (expanded_types, i);
12729
12730 /* Rename the parameter to include the index. */
12731 DECL_NAME (r)
12732 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12733 }
12734 else if (!type)
12735 /* We're dealing with a normal parameter. */
12736 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12737
12738 type = type_decays_to (type);
12739 TREE_TYPE (r) = type;
12740 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12741
12742 if (DECL_INITIAL (r))
12743 {
12744 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12745 DECL_INITIAL (r) = TREE_TYPE (r);
12746 else
12747 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12748 complain, in_decl);
12749 }
12750
12751 DECL_CONTEXT (r) = NULL_TREE;
12752
12753 if (!DECL_TEMPLATE_PARM_P (r))
12754 DECL_ARG_TYPE (r) = type_passed_as (type);
12755
12756 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12757 args, complain, in_decl);
12758
12759 /* Keep track of the first new parameter we
12760 generate. That's what will be returned to the
12761 caller. */
12762 if (!first_r)
12763 first_r = r;
12764
12765 /* Build a proper chain of parameters when substituting
12766 into a function parameter pack. */
12767 if (prev_r)
12768 DECL_CHAIN (prev_r) = r;
12769 }
12770
12771 /* If cp_unevaluated_operand is set, we're just looking for a
12772 single dummy parameter, so don't keep going. */
12773 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12774 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12775 complain, DECL_CHAIN (t));
12776
12777 /* FIRST_R contains the start of the chain we've built. */
12778 r = first_r;
12779 }
12780 break;
12781
12782 case FIELD_DECL:
12783 {
12784 tree type = NULL_TREE;
12785 tree vec = NULL_TREE;
12786 tree expanded_types = NULL_TREE;
12787 int len = 1;
12788
12789 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12790 {
12791 /* This field is a lambda capture pack. Return a TREE_VEC of
12792 the expanded fields to instantiate_class_template_1 and
12793 store them in the specializations hash table as a
12794 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12795 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12796 complain, in_decl);
12797 if (TREE_CODE (expanded_types) == TREE_VEC)
12798 {
12799 len = TREE_VEC_LENGTH (expanded_types);
12800 vec = make_tree_vec (len);
12801 }
12802 else
12803 {
12804 /* All we did was update the type. Make a note of that. */
12805 type = expanded_types;
12806 expanded_types = NULL_TREE;
12807 }
12808 }
12809
12810 for (int i = 0; i < len; ++i)
12811 {
12812 r = copy_decl (t);
12813 if (expanded_types)
12814 {
12815 type = TREE_VEC_ELT (expanded_types, i);
12816 DECL_NAME (r)
12817 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12818 }
12819 else if (!type)
12820 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12821
12822 if (type == error_mark_node)
12823 RETURN (error_mark_node);
12824 TREE_TYPE (r) = type;
12825 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12826
12827 if (DECL_C_BIT_FIELD (r))
12828 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12829 number of bits. */
12830 DECL_BIT_FIELD_REPRESENTATIVE (r)
12831 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
12832 complain, in_decl,
12833 /*integral_constant_expression_p=*/true);
12834 if (DECL_INITIAL (t))
12835 {
12836 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12837 NSDMI in perform_member_init. Still set DECL_INITIAL
12838 so that we know there is one. */
12839 DECL_INITIAL (r) = void_node;
12840 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12841 retrofit_lang_decl (r);
12842 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12843 }
12844 /* We don't have to set DECL_CONTEXT here; it is set by
12845 finish_member_declaration. */
12846 DECL_CHAIN (r) = NULL_TREE;
12847
12848 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12849 args, complain, in_decl);
12850
12851 if (vec)
12852 TREE_VEC_ELT (vec, i) = r;
12853 }
12854
12855 if (vec)
12856 {
12857 r = vec;
12858 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12859 SET_ARGUMENT_PACK_ARGS (pack, vec);
12860 register_specialization (pack, t, args, false, 0);
12861 }
12862 }
12863 break;
12864
12865 case USING_DECL:
12866 /* We reach here only for member using decls. We also need to check
12867 uses_template_parms because DECL_DEPENDENT_P is not set for a
12868 using-declaration that designates a member of the current
12869 instantiation (c++/53549). */
12870 if (DECL_DEPENDENT_P (t)
12871 || uses_template_parms (USING_DECL_SCOPE (t)))
12872 {
12873 tree scope = USING_DECL_SCOPE (t);
12874 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12875 if (PACK_EXPANSION_P (scope))
12876 {
12877 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12878 int len = TREE_VEC_LENGTH (vec);
12879 r = make_tree_vec (len);
12880 for (int i = 0; i < len; ++i)
12881 {
12882 tree escope = TREE_VEC_ELT (vec, i);
12883 tree elt = do_class_using_decl (escope, name);
12884 if (!elt)
12885 {
12886 r = error_mark_node;
12887 break;
12888 }
12889 else
12890 {
12891 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12892 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12893 }
12894 TREE_VEC_ELT (r, i) = elt;
12895 }
12896 }
12897 else
12898 {
12899 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12900 complain, in_decl);
12901 r = do_class_using_decl (inst_scope, name);
12902 if (!r)
12903 r = error_mark_node;
12904 else
12905 {
12906 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12907 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12908 }
12909 }
12910 }
12911 else
12912 {
12913 r = copy_node (t);
12914 DECL_CHAIN (r) = NULL_TREE;
12915 }
12916 break;
12917
12918 case TYPE_DECL:
12919 case VAR_DECL:
12920 {
12921 tree argvec = NULL_TREE;
12922 tree gen_tmpl = NULL_TREE;
12923 tree spec;
12924 tree tmpl = NULL_TREE;
12925 tree ctx;
12926 tree type = NULL_TREE;
12927 bool local_p;
12928
12929 if (TREE_TYPE (t) == error_mark_node)
12930 RETURN (error_mark_node);
12931
12932 if (TREE_CODE (t) == TYPE_DECL
12933 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12934 {
12935 /* If this is the canonical decl, we don't have to
12936 mess with instantiations, and often we can't (for
12937 typename, template type parms and such). Note that
12938 TYPE_NAME is not correct for the above test if
12939 we've copied the type for a typedef. */
12940 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12941 if (type == error_mark_node)
12942 RETURN (error_mark_node);
12943 r = TYPE_NAME (type);
12944 break;
12945 }
12946
12947 /* Check to see if we already have the specialization we
12948 need. */
12949 spec = NULL_TREE;
12950 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12951 {
12952 /* T is a static data member or namespace-scope entity.
12953 We have to substitute into namespace-scope variables
12954 (not just variable templates) because of cases like:
12955
12956 template <class T> void f() { extern T t; }
12957
12958 where the entity referenced is not known until
12959 instantiation time. */
12960 local_p = false;
12961 ctx = DECL_CONTEXT (t);
12962 if (DECL_CLASS_SCOPE_P (t))
12963 {
12964 ctx = tsubst_aggr_type (ctx, args,
12965 complain,
12966 in_decl, /*entering_scope=*/1);
12967 /* If CTX is unchanged, then T is in fact the
12968 specialization we want. That situation occurs when
12969 referencing a static data member within in its own
12970 class. We can use pointer equality, rather than
12971 same_type_p, because DECL_CONTEXT is always
12972 canonical... */
12973 if (ctx == DECL_CONTEXT (t)
12974 /* ... unless T is a member template; in which
12975 case our caller can be willing to create a
12976 specialization of that template represented
12977 by T. */
12978 && !(DECL_TI_TEMPLATE (t)
12979 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12980 spec = t;
12981 }
12982
12983 if (!spec)
12984 {
12985 tmpl = DECL_TI_TEMPLATE (t);
12986 gen_tmpl = most_general_template (tmpl);
12987 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12988 if (argvec != error_mark_node)
12989 argvec = (coerce_innermost_template_parms
12990 (DECL_TEMPLATE_PARMS (gen_tmpl),
12991 argvec, t, complain,
12992 /*all*/true, /*defarg*/true));
12993 if (argvec == error_mark_node)
12994 RETURN (error_mark_node);
12995 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12996 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12997 }
12998 }
12999 else
13000 {
13001 /* A local variable. */
13002 local_p = true;
13003 /* Subsequent calls to pushdecl will fill this in. */
13004 ctx = NULL_TREE;
13005 /* Unless this is a reference to a static variable from an
13006 enclosing function, in which case we need to fill it in now. */
13007 if (TREE_STATIC (t))
13008 {
13009 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13010 if (fn != current_function_decl)
13011 ctx = fn;
13012 }
13013 spec = retrieve_local_specialization (t);
13014 }
13015 /* If we already have the specialization we need, there is
13016 nothing more to do. */
13017 if (spec)
13018 {
13019 r = spec;
13020 break;
13021 }
13022
13023 /* Create a new node for the specialization we need. */
13024 r = copy_decl (t);
13025 if (type == NULL_TREE)
13026 {
13027 if (is_typedef_decl (t))
13028 type = DECL_ORIGINAL_TYPE (t);
13029 else
13030 type = TREE_TYPE (t);
13031 if (VAR_P (t)
13032 && VAR_HAD_UNKNOWN_BOUND (t)
13033 && type != error_mark_node)
13034 type = strip_array_domain (type);
13035 tree sub_args = args;
13036 if (tree auto_node = type_uses_auto (type))
13037 {
13038 /* Mask off any template args past the variable's context so we
13039 don't replace the auto with an unrelated argument. */
13040 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13041 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13042 if (extra > 0)
13043 /* This should never happen with the new lambda instantiation
13044 model, but keep the handling just in case. */
13045 gcc_assert (!CHECKING_P),
13046 sub_args = strip_innermost_template_args (args, extra);
13047 }
13048 type = tsubst (type, sub_args, complain, in_decl);
13049 }
13050 if (VAR_P (r))
13051 {
13052 /* Even if the original location is out of scope, the
13053 newly substituted one is not. */
13054 DECL_DEAD_FOR_LOCAL (r) = 0;
13055 DECL_INITIALIZED_P (r) = 0;
13056 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13057 if (type == error_mark_node)
13058 RETURN (error_mark_node);
13059 if (TREE_CODE (type) == FUNCTION_TYPE)
13060 {
13061 /* It may seem that this case cannot occur, since:
13062
13063 typedef void f();
13064 void g() { f x; }
13065
13066 declares a function, not a variable. However:
13067
13068 typedef void f();
13069 template <typename T> void g() { T t; }
13070 template void g<f>();
13071
13072 is an attempt to declare a variable with function
13073 type. */
13074 error ("variable %qD has function type",
13075 /* R is not yet sufficiently initialized, so we
13076 just use its name. */
13077 DECL_NAME (r));
13078 RETURN (error_mark_node);
13079 }
13080 type = complete_type (type);
13081 /* Wait until cp_finish_decl to set this again, to handle
13082 circular dependency (template/instantiate6.C). */
13083 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13084 type = check_var_type (DECL_NAME (r), type);
13085
13086 if (DECL_HAS_VALUE_EXPR_P (t))
13087 {
13088 tree ve = DECL_VALUE_EXPR (t);
13089 ve = tsubst_expr (ve, args, complain, in_decl,
13090 /*constant_expression_p=*/false);
13091 if (REFERENCE_REF_P (ve))
13092 {
13093 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13094 ve = TREE_OPERAND (ve, 0);
13095 }
13096 SET_DECL_VALUE_EXPR (r, ve);
13097 }
13098 if (CP_DECL_THREAD_LOCAL_P (r)
13099 && !processing_template_decl)
13100 set_decl_tls_model (r, decl_default_tls_model (r));
13101 }
13102 else if (DECL_SELF_REFERENCE_P (t))
13103 SET_DECL_SELF_REFERENCE_P (r);
13104 TREE_TYPE (r) = type;
13105 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13106 DECL_CONTEXT (r) = ctx;
13107 /* Clear out the mangled name and RTL for the instantiation. */
13108 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13109 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13110 SET_DECL_RTL (r, NULL);
13111 /* The initializer must not be expanded until it is required;
13112 see [temp.inst]. */
13113 DECL_INITIAL (r) = NULL_TREE;
13114 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13115 if (VAR_P (r))
13116 {
13117 if (DECL_LANG_SPECIFIC (r))
13118 SET_DECL_DEPENDENT_INIT_P (r, false);
13119
13120 SET_DECL_MODE (r, VOIDmode);
13121
13122 /* Possibly limit visibility based on template args. */
13123 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13124 if (DECL_VISIBILITY_SPECIFIED (t))
13125 {
13126 DECL_VISIBILITY_SPECIFIED (r) = 0;
13127 DECL_ATTRIBUTES (r)
13128 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13129 }
13130 determine_visibility (r);
13131 }
13132
13133 if (!local_p)
13134 {
13135 /* A static data member declaration is always marked
13136 external when it is declared in-class, even if an
13137 initializer is present. We mimic the non-template
13138 processing here. */
13139 DECL_EXTERNAL (r) = 1;
13140 if (DECL_NAMESPACE_SCOPE_P (t))
13141 DECL_NOT_REALLY_EXTERN (r) = 1;
13142
13143 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13144 SET_DECL_IMPLICIT_INSTANTIATION (r);
13145 register_specialization (r, gen_tmpl, argvec, false, hash);
13146 }
13147 else
13148 {
13149 if (DECL_LANG_SPECIFIC (r))
13150 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13151 if (!cp_unevaluated_operand)
13152 register_local_specialization (r, t);
13153 }
13154
13155 DECL_CHAIN (r) = NULL_TREE;
13156
13157 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13158 /*flags=*/0,
13159 args, complain, in_decl);
13160
13161 /* Preserve a typedef that names a type. */
13162 if (is_typedef_decl (r) && type != error_mark_node)
13163 {
13164 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13165 set_underlying_type (r);
13166 if (TYPE_DECL_ALIAS_P (r))
13167 /* An alias template specialization can be dependent
13168 even if its underlying type is not. */
13169 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13170 }
13171
13172 layout_decl (r, 0);
13173 }
13174 break;
13175
13176 default:
13177 gcc_unreachable ();
13178 }
13179 #undef RETURN
13180
13181 out:
13182 /* Restore the file and line information. */
13183 input_location = saved_loc;
13184
13185 return r;
13186 }
13187
13188 /* Substitute into the ARG_TYPES of a function type.
13189 If END is a TREE_CHAIN, leave it and any following types
13190 un-substituted. */
13191
13192 static tree
13193 tsubst_arg_types (tree arg_types,
13194 tree args,
13195 tree end,
13196 tsubst_flags_t complain,
13197 tree in_decl)
13198 {
13199 tree remaining_arg_types;
13200 tree type = NULL_TREE;
13201 int i = 1;
13202 tree expanded_args = NULL_TREE;
13203 tree default_arg;
13204
13205 if (!arg_types || arg_types == void_list_node || arg_types == end)
13206 return arg_types;
13207
13208 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13209 args, end, complain, in_decl);
13210 if (remaining_arg_types == error_mark_node)
13211 return error_mark_node;
13212
13213 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13214 {
13215 /* For a pack expansion, perform substitution on the
13216 entire expression. Later on, we'll handle the arguments
13217 one-by-one. */
13218 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13219 args, complain, in_decl);
13220
13221 if (TREE_CODE (expanded_args) == TREE_VEC)
13222 /* So that we'll spin through the parameters, one by one. */
13223 i = TREE_VEC_LENGTH (expanded_args);
13224 else
13225 {
13226 /* We only partially substituted into the parameter
13227 pack. Our type is TYPE_PACK_EXPANSION. */
13228 type = expanded_args;
13229 expanded_args = NULL_TREE;
13230 }
13231 }
13232
13233 while (i > 0) {
13234 --i;
13235
13236 if (expanded_args)
13237 type = TREE_VEC_ELT (expanded_args, i);
13238 else if (!type)
13239 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13240
13241 if (type == error_mark_node)
13242 return error_mark_node;
13243 if (VOID_TYPE_P (type))
13244 {
13245 if (complain & tf_error)
13246 {
13247 error ("invalid parameter type %qT", type);
13248 if (in_decl)
13249 error ("in declaration %q+D", in_decl);
13250 }
13251 return error_mark_node;
13252 }
13253 /* DR 657. */
13254 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13255 return error_mark_node;
13256
13257 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13258 top-level qualifiers as required. */
13259 type = cv_unqualified (type_decays_to (type));
13260
13261 /* We do not substitute into default arguments here. The standard
13262 mandates that they be instantiated only when needed, which is
13263 done in build_over_call. */
13264 default_arg = TREE_PURPOSE (arg_types);
13265
13266 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13267 since the new op() won't have any associated template arguments for us
13268 to refer to later. */
13269 if (lambda_fn_in_template_p (in_decl))
13270 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13271 false/*fn*/, false/*constexpr*/);
13272
13273 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13274 {
13275 /* We've instantiated a template before its default arguments
13276 have been parsed. This can happen for a nested template
13277 class, and is not an error unless we require the default
13278 argument in a call of this function. */
13279 remaining_arg_types =
13280 tree_cons (default_arg, type, remaining_arg_types);
13281 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13282 }
13283 else
13284 remaining_arg_types =
13285 hash_tree_cons (default_arg, type, remaining_arg_types);
13286 }
13287
13288 return remaining_arg_types;
13289 }
13290
13291 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13292 *not* handle the exception-specification for FNTYPE, because the
13293 initial substitution of explicitly provided template parameters
13294 during argument deduction forbids substitution into the
13295 exception-specification:
13296
13297 [temp.deduct]
13298
13299 All references in the function type of the function template to the
13300 corresponding template parameters are replaced by the specified tem-
13301 plate argument values. If a substitution in a template parameter or
13302 in the function type of the function template results in an invalid
13303 type, type deduction fails. [Note: The equivalent substitution in
13304 exception specifications is done only when the function is instanti-
13305 ated, at which point a program is ill-formed if the substitution
13306 results in an invalid type.] */
13307
13308 static tree
13309 tsubst_function_type (tree t,
13310 tree args,
13311 tsubst_flags_t complain,
13312 tree in_decl)
13313 {
13314 tree return_type;
13315 tree arg_types = NULL_TREE;
13316 tree fntype;
13317
13318 /* The TYPE_CONTEXT is not used for function/method types. */
13319 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13320
13321 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13322 failure. */
13323 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13324
13325 if (late_return_type_p)
13326 {
13327 /* Substitute the argument types. */
13328 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13329 complain, in_decl);
13330 if (arg_types == error_mark_node)
13331 return error_mark_node;
13332
13333 tree save_ccp = current_class_ptr;
13334 tree save_ccr = current_class_ref;
13335 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13336 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13337 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13338 if (do_inject)
13339 {
13340 /* DR 1207: 'this' is in scope in the trailing return type. */
13341 inject_this_parameter (this_type, cp_type_quals (this_type));
13342 }
13343
13344 /* Substitute the return type. */
13345 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13346
13347 if (do_inject)
13348 {
13349 current_class_ptr = save_ccp;
13350 current_class_ref = save_ccr;
13351 }
13352 }
13353 else
13354 /* Substitute the return type. */
13355 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13356
13357 if (return_type == error_mark_node)
13358 return error_mark_node;
13359 /* DR 486 clarifies that creation of a function type with an
13360 invalid return type is a deduction failure. */
13361 if (TREE_CODE (return_type) == ARRAY_TYPE
13362 || TREE_CODE (return_type) == FUNCTION_TYPE)
13363 {
13364 if (complain & tf_error)
13365 {
13366 if (TREE_CODE (return_type) == ARRAY_TYPE)
13367 error ("function returning an array");
13368 else
13369 error ("function returning a function");
13370 }
13371 return error_mark_node;
13372 }
13373 /* And DR 657. */
13374 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13375 return error_mark_node;
13376
13377 if (!late_return_type_p)
13378 {
13379 /* Substitute the argument types. */
13380 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13381 complain, in_decl);
13382 if (arg_types == error_mark_node)
13383 return error_mark_node;
13384 }
13385
13386 /* Construct a new type node and return it. */
13387 if (TREE_CODE (t) == FUNCTION_TYPE)
13388 {
13389 fntype = build_function_type (return_type, arg_types);
13390 fntype = apply_memfn_quals (fntype,
13391 type_memfn_quals (t),
13392 type_memfn_rqual (t));
13393 }
13394 else
13395 {
13396 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13397 /* Don't pick up extra function qualifiers from the basetype. */
13398 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13399 if (! MAYBE_CLASS_TYPE_P (r))
13400 {
13401 /* [temp.deduct]
13402
13403 Type deduction may fail for any of the following
13404 reasons:
13405
13406 -- Attempting to create "pointer to member of T" when T
13407 is not a class type. */
13408 if (complain & tf_error)
13409 error ("creating pointer to member function of non-class type %qT",
13410 r);
13411 return error_mark_node;
13412 }
13413
13414 fntype = build_method_type_directly (r, return_type,
13415 TREE_CHAIN (arg_types));
13416 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13417 }
13418 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13419
13420 if (late_return_type_p)
13421 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13422
13423 return fntype;
13424 }
13425
13426 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13427 ARGS into that specification, and return the substituted
13428 specification. If there is no specification, return NULL_TREE. */
13429
13430 static tree
13431 tsubst_exception_specification (tree fntype,
13432 tree args,
13433 tsubst_flags_t complain,
13434 tree in_decl,
13435 bool defer_ok)
13436 {
13437 tree specs;
13438 tree new_specs;
13439
13440 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13441 new_specs = NULL_TREE;
13442 if (specs && TREE_PURPOSE (specs))
13443 {
13444 /* A noexcept-specifier. */
13445 tree expr = TREE_PURPOSE (specs);
13446 if (TREE_CODE (expr) == INTEGER_CST)
13447 new_specs = expr;
13448 else if (defer_ok)
13449 {
13450 /* Defer instantiation of noexcept-specifiers to avoid
13451 excessive instantiations (c++/49107). */
13452 new_specs = make_node (DEFERRED_NOEXCEPT);
13453 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13454 {
13455 /* We already partially instantiated this member template,
13456 so combine the new args with the old. */
13457 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13458 = DEFERRED_NOEXCEPT_PATTERN (expr);
13459 DEFERRED_NOEXCEPT_ARGS (new_specs)
13460 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13461 }
13462 else
13463 {
13464 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13465 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13466 }
13467 }
13468 else
13469 new_specs = tsubst_copy_and_build
13470 (expr, args, complain, in_decl, /*function_p=*/false,
13471 /*integral_constant_expression_p=*/true);
13472 new_specs = build_noexcept_spec (new_specs, complain);
13473 }
13474 else if (specs)
13475 {
13476 if (! TREE_VALUE (specs))
13477 new_specs = specs;
13478 else
13479 while (specs)
13480 {
13481 tree spec;
13482 int i, len = 1;
13483 tree expanded_specs = NULL_TREE;
13484
13485 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13486 {
13487 /* Expand the pack expansion type. */
13488 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13489 args, complain,
13490 in_decl);
13491
13492 if (expanded_specs == error_mark_node)
13493 return error_mark_node;
13494 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13495 len = TREE_VEC_LENGTH (expanded_specs);
13496 else
13497 {
13498 /* We're substituting into a member template, so
13499 we got a TYPE_PACK_EXPANSION back. Add that
13500 expansion and move on. */
13501 gcc_assert (TREE_CODE (expanded_specs)
13502 == TYPE_PACK_EXPANSION);
13503 new_specs = add_exception_specifier (new_specs,
13504 expanded_specs,
13505 complain);
13506 specs = TREE_CHAIN (specs);
13507 continue;
13508 }
13509 }
13510
13511 for (i = 0; i < len; ++i)
13512 {
13513 if (expanded_specs)
13514 spec = TREE_VEC_ELT (expanded_specs, i);
13515 else
13516 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13517 if (spec == error_mark_node)
13518 return spec;
13519 new_specs = add_exception_specifier (new_specs, spec,
13520 complain);
13521 }
13522
13523 specs = TREE_CHAIN (specs);
13524 }
13525 }
13526 return new_specs;
13527 }
13528
13529 /* Take the tree structure T and replace template parameters used
13530 therein with the argument vector ARGS. IN_DECL is an associated
13531 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13532 Issue error and warning messages under control of COMPLAIN. Note
13533 that we must be relatively non-tolerant of extensions here, in
13534 order to preserve conformance; if we allow substitutions that
13535 should not be allowed, we may allow argument deductions that should
13536 not succeed, and therefore report ambiguous overload situations
13537 where there are none. In theory, we could allow the substitution,
13538 but indicate that it should have failed, and allow our caller to
13539 make sure that the right thing happens, but we don't try to do this
13540 yet.
13541
13542 This function is used for dealing with types, decls and the like;
13543 for expressions, use tsubst_expr or tsubst_copy. */
13544
13545 tree
13546 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13547 {
13548 enum tree_code code;
13549 tree type, r = NULL_TREE;
13550
13551 if (t == NULL_TREE || t == error_mark_node
13552 || t == integer_type_node
13553 || t == void_type_node
13554 || t == char_type_node
13555 || t == unknown_type_node
13556 || TREE_CODE (t) == NAMESPACE_DECL
13557 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13558 return t;
13559
13560 if (DECL_P (t))
13561 return tsubst_decl (t, args, complain);
13562
13563 if (args == NULL_TREE)
13564 return t;
13565
13566 code = TREE_CODE (t);
13567
13568 if (code == IDENTIFIER_NODE)
13569 type = IDENTIFIER_TYPE_VALUE (t);
13570 else
13571 type = TREE_TYPE (t);
13572
13573 gcc_assert (type != unknown_type_node);
13574
13575 /* Reuse typedefs. We need to do this to handle dependent attributes,
13576 such as attribute aligned. */
13577 if (TYPE_P (t)
13578 && typedef_variant_p (t))
13579 {
13580 tree decl = TYPE_NAME (t);
13581
13582 if (alias_template_specialization_p (t))
13583 {
13584 /* DECL represents an alias template and we want to
13585 instantiate it. */
13586 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13587 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13588 r = instantiate_alias_template (tmpl, gen_args, complain);
13589 }
13590 else if (DECL_CLASS_SCOPE_P (decl)
13591 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13592 && uses_template_parms (DECL_CONTEXT (decl)))
13593 {
13594 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13595 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13596 r = retrieve_specialization (tmpl, gen_args, 0);
13597 }
13598 else if (DECL_FUNCTION_SCOPE_P (decl)
13599 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13600 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13601 r = retrieve_local_specialization (decl);
13602 else
13603 /* The typedef is from a non-template context. */
13604 return t;
13605
13606 if (r)
13607 {
13608 r = TREE_TYPE (r);
13609 r = cp_build_qualified_type_real
13610 (r, cp_type_quals (t) | cp_type_quals (r),
13611 complain | tf_ignore_bad_quals);
13612 return r;
13613 }
13614 else
13615 {
13616 /* We don't have an instantiation yet, so drop the typedef. */
13617 int quals = cp_type_quals (t);
13618 t = DECL_ORIGINAL_TYPE (decl);
13619 t = cp_build_qualified_type_real (t, quals,
13620 complain | tf_ignore_bad_quals);
13621 }
13622 }
13623
13624 bool fndecl_type = (complain & tf_fndecl_type);
13625 complain &= ~tf_fndecl_type;
13626
13627 if (type
13628 && code != TYPENAME_TYPE
13629 && code != TEMPLATE_TYPE_PARM
13630 && code != TEMPLATE_PARM_INDEX
13631 && code != IDENTIFIER_NODE
13632 && code != FUNCTION_TYPE
13633 && code != METHOD_TYPE)
13634 type = tsubst (type, args, complain, in_decl);
13635 if (type == error_mark_node)
13636 return error_mark_node;
13637
13638 switch (code)
13639 {
13640 case RECORD_TYPE:
13641 case UNION_TYPE:
13642 case ENUMERAL_TYPE:
13643 return tsubst_aggr_type (t, args, complain, in_decl,
13644 /*entering_scope=*/0);
13645
13646 case ERROR_MARK:
13647 case IDENTIFIER_NODE:
13648 case VOID_TYPE:
13649 case REAL_TYPE:
13650 case COMPLEX_TYPE:
13651 case VECTOR_TYPE:
13652 case BOOLEAN_TYPE:
13653 case NULLPTR_TYPE:
13654 case LANG_TYPE:
13655 return t;
13656
13657 case INTEGER_TYPE:
13658 if (t == integer_type_node)
13659 return t;
13660
13661 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13662 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13663 return t;
13664
13665 {
13666 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13667
13668 max = tsubst_expr (omax, args, complain, in_decl,
13669 /*integral_constant_expression_p=*/false);
13670
13671 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13672 needed. */
13673 if (TREE_CODE (max) == NOP_EXPR
13674 && TREE_SIDE_EFFECTS (omax)
13675 && !TREE_TYPE (max))
13676 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13677
13678 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13679 with TREE_SIDE_EFFECTS that indicates this is not an integral
13680 constant expression. */
13681 if (processing_template_decl
13682 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13683 {
13684 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13685 TREE_SIDE_EFFECTS (max) = 1;
13686 }
13687
13688 return compute_array_index_type (NULL_TREE, max, complain);
13689 }
13690
13691 case TEMPLATE_TYPE_PARM:
13692 case TEMPLATE_TEMPLATE_PARM:
13693 case BOUND_TEMPLATE_TEMPLATE_PARM:
13694 case TEMPLATE_PARM_INDEX:
13695 {
13696 int idx;
13697 int level;
13698 int levels;
13699 tree arg = NULL_TREE;
13700
13701 /* Early in template argument deduction substitution, we don't
13702 want to reduce the level of 'auto', or it will be confused
13703 with a normal template parm in subsequent deduction. */
13704 if (is_auto (t) && (complain & tf_partial))
13705 return t;
13706
13707 r = NULL_TREE;
13708
13709 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13710 template_parm_level_and_index (t, &level, &idx);
13711
13712 levels = TMPL_ARGS_DEPTH (args);
13713 if (level <= levels
13714 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13715 {
13716 arg = TMPL_ARG (args, level, idx);
13717
13718 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13719 {
13720 /* See through ARGUMENT_PACK_SELECT arguments. */
13721 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13722 /* If the selected argument is an expansion E, that most
13723 likely means we were called from
13724 gen_elem_of_pack_expansion_instantiation during the
13725 substituting of pack an argument pack (which Ith
13726 element is a pack expansion, where I is
13727 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13728 In this case, the Ith element resulting from this
13729 substituting is going to be a pack expansion, which
13730 pattern is the pattern of E. Let's return the
13731 pattern of E, and
13732 gen_elem_of_pack_expansion_instantiation will
13733 build the resulting pack expansion from it. */
13734 if (PACK_EXPANSION_P (arg))
13735 {
13736 /* Make sure we aren't throwing away arg info. */
13737 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13738 arg = PACK_EXPANSION_PATTERN (arg);
13739 }
13740 }
13741 }
13742
13743 if (arg == error_mark_node)
13744 return error_mark_node;
13745 else if (arg != NULL_TREE)
13746 {
13747 if (ARGUMENT_PACK_P (arg))
13748 /* If ARG is an argument pack, we don't actually want to
13749 perform a substitution here, because substitutions
13750 for argument packs are only done
13751 element-by-element. We can get to this point when
13752 substituting the type of a non-type template
13753 parameter pack, when that type actually contains
13754 template parameter packs from an outer template, e.g.,
13755
13756 template<typename... Types> struct A {
13757 template<Types... Values> struct B { };
13758 }; */
13759 return t;
13760
13761 if (code == TEMPLATE_TYPE_PARM)
13762 {
13763 int quals;
13764 gcc_assert (TYPE_P (arg));
13765
13766 quals = cp_type_quals (arg) | cp_type_quals (t);
13767
13768 return cp_build_qualified_type_real
13769 (arg, quals, complain | tf_ignore_bad_quals);
13770 }
13771 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13772 {
13773 /* We are processing a type constructed from a
13774 template template parameter. */
13775 tree argvec = tsubst (TYPE_TI_ARGS (t),
13776 args, complain, in_decl);
13777 if (argvec == error_mark_node)
13778 return error_mark_node;
13779
13780 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13781 || TREE_CODE (arg) == TEMPLATE_DECL
13782 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13783
13784 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13785 /* Consider this code:
13786
13787 template <template <class> class Template>
13788 struct Internal {
13789 template <class Arg> using Bind = Template<Arg>;
13790 };
13791
13792 template <template <class> class Template, class Arg>
13793 using Instantiate = Template<Arg>; //#0
13794
13795 template <template <class> class Template,
13796 class Argument>
13797 using Bind =
13798 Instantiate<Internal<Template>::template Bind,
13799 Argument>; //#1
13800
13801 When #1 is parsed, the
13802 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13803 parameter `Template' in #0 matches the
13804 UNBOUND_CLASS_TEMPLATE representing the argument
13805 `Internal<Template>::template Bind'; We then want
13806 to assemble the type `Bind<Argument>' that can't
13807 be fully created right now, because
13808 `Internal<Template>' not being complete, the Bind
13809 template cannot be looked up in that context. So
13810 we need to "store" `Bind<Argument>' for later
13811 when the context of Bind becomes complete. Let's
13812 store that in a TYPENAME_TYPE. */
13813 return make_typename_type (TYPE_CONTEXT (arg),
13814 build_nt (TEMPLATE_ID_EXPR,
13815 TYPE_IDENTIFIER (arg),
13816 argvec),
13817 typename_type,
13818 complain);
13819
13820 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13821 are resolving nested-types in the signature of a
13822 member function templates. Otherwise ARG is a
13823 TEMPLATE_DECL and is the real template to be
13824 instantiated. */
13825 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13826 arg = TYPE_NAME (arg);
13827
13828 r = lookup_template_class (arg,
13829 argvec, in_decl,
13830 DECL_CONTEXT (arg),
13831 /*entering_scope=*/0,
13832 complain);
13833 return cp_build_qualified_type_real
13834 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13835 }
13836 else if (code == TEMPLATE_TEMPLATE_PARM)
13837 return arg;
13838 else
13839 /* TEMPLATE_PARM_INDEX. */
13840 return convert_from_reference (unshare_expr (arg));
13841 }
13842
13843 if (level == 1)
13844 /* This can happen during the attempted tsubst'ing in
13845 unify. This means that we don't yet have any information
13846 about the template parameter in question. */
13847 return t;
13848
13849 /* If we get here, we must have been looking at a parm for a
13850 more deeply nested template. Make a new version of this
13851 template parameter, but with a lower level. */
13852 switch (code)
13853 {
13854 case TEMPLATE_TYPE_PARM:
13855 case TEMPLATE_TEMPLATE_PARM:
13856 case BOUND_TEMPLATE_TEMPLATE_PARM:
13857 if (cp_type_quals (t))
13858 {
13859 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13860 r = cp_build_qualified_type_real
13861 (r, cp_type_quals (t),
13862 complain | (code == TEMPLATE_TYPE_PARM
13863 ? tf_ignore_bad_quals : 0));
13864 }
13865 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13866 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13867 && (r = (TEMPLATE_PARM_DESCENDANTS
13868 (TEMPLATE_TYPE_PARM_INDEX (t))))
13869 && (r = TREE_TYPE (r))
13870 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13871 /* Break infinite recursion when substituting the constraints
13872 of a constrained placeholder. */;
13873 else
13874 {
13875 r = copy_type (t);
13876 TEMPLATE_TYPE_PARM_INDEX (r)
13877 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13878 r, levels, args, complain);
13879 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13880 TYPE_MAIN_VARIANT (r) = r;
13881 TYPE_POINTER_TO (r) = NULL_TREE;
13882 TYPE_REFERENCE_TO (r) = NULL_TREE;
13883
13884 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13885 {
13886 /* Propagate constraints on placeholders. */
13887 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13888 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13889 = tsubst_constraint (constr, args, complain, in_decl);
13890 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13891 {
13892 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13893 pl = tsubst (pl, args, complain, in_decl);
13894 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13895 }
13896 }
13897
13898 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13899 /* We have reduced the level of the template
13900 template parameter, but not the levels of its
13901 template parameters, so canonical_type_parameter
13902 will not be able to find the canonical template
13903 template parameter for this level. Thus, we
13904 require structural equality checking to compare
13905 TEMPLATE_TEMPLATE_PARMs. */
13906 SET_TYPE_STRUCTURAL_EQUALITY (r);
13907 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13908 SET_TYPE_STRUCTURAL_EQUALITY (r);
13909 else
13910 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13911
13912 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13913 {
13914 tree tinfo = TYPE_TEMPLATE_INFO (t);
13915 /* We might need to substitute into the types of non-type
13916 template parameters. */
13917 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13918 complain, in_decl);
13919 if (tmpl == error_mark_node)
13920 return error_mark_node;
13921 tree argvec = tsubst (TI_ARGS (tinfo), args,
13922 complain, in_decl);
13923 if (argvec == error_mark_node)
13924 return error_mark_node;
13925
13926 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13927 = build_template_info (tmpl, argvec);
13928 }
13929 }
13930 break;
13931
13932 case TEMPLATE_PARM_INDEX:
13933 /* OK, now substitute the type of the non-type parameter. We
13934 couldn't do it earlier because it might be an auto parameter,
13935 and we wouldn't need to if we had an argument. */
13936 type = tsubst (type, args, complain, in_decl);
13937 if (type == error_mark_node)
13938 return error_mark_node;
13939 r = reduce_template_parm_level (t, type, levels, args, complain);
13940 break;
13941
13942 default:
13943 gcc_unreachable ();
13944 }
13945
13946 return r;
13947 }
13948
13949 case TREE_LIST:
13950 {
13951 tree purpose, value, chain;
13952
13953 if (t == void_list_node)
13954 return t;
13955
13956 purpose = TREE_PURPOSE (t);
13957 if (purpose)
13958 {
13959 purpose = tsubst (purpose, args, complain, in_decl);
13960 if (purpose == error_mark_node)
13961 return error_mark_node;
13962 }
13963 value = TREE_VALUE (t);
13964 if (value)
13965 {
13966 value = tsubst (value, args, complain, in_decl);
13967 if (value == error_mark_node)
13968 return error_mark_node;
13969 }
13970 chain = TREE_CHAIN (t);
13971 if (chain && chain != void_type_node)
13972 {
13973 chain = tsubst (chain, args, complain, in_decl);
13974 if (chain == error_mark_node)
13975 return error_mark_node;
13976 }
13977 if (purpose == TREE_PURPOSE (t)
13978 && value == TREE_VALUE (t)
13979 && chain == TREE_CHAIN (t))
13980 return t;
13981 return hash_tree_cons (purpose, value, chain);
13982 }
13983
13984 case TREE_BINFO:
13985 /* We should never be tsubsting a binfo. */
13986 gcc_unreachable ();
13987
13988 case TREE_VEC:
13989 /* A vector of template arguments. */
13990 gcc_assert (!type);
13991 return tsubst_template_args (t, args, complain, in_decl);
13992
13993 case POINTER_TYPE:
13994 case REFERENCE_TYPE:
13995 {
13996 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13997 return t;
13998
13999 /* [temp.deduct]
14000
14001 Type deduction may fail for any of the following
14002 reasons:
14003
14004 -- Attempting to create a pointer to reference type.
14005 -- Attempting to create a reference to a reference type or
14006 a reference to void.
14007
14008 Core issue 106 says that creating a reference to a reference
14009 during instantiation is no longer a cause for failure. We
14010 only enforce this check in strict C++98 mode. */
14011 if ((TREE_CODE (type) == REFERENCE_TYPE
14012 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14013 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14014 {
14015 static location_t last_loc;
14016
14017 /* We keep track of the last time we issued this error
14018 message to avoid spewing a ton of messages during a
14019 single bad template instantiation. */
14020 if (complain & tf_error
14021 && last_loc != input_location)
14022 {
14023 if (VOID_TYPE_P (type))
14024 error ("forming reference to void");
14025 else if (code == POINTER_TYPE)
14026 error ("forming pointer to reference type %qT", type);
14027 else
14028 error ("forming reference to reference type %qT", type);
14029 last_loc = input_location;
14030 }
14031
14032 return error_mark_node;
14033 }
14034 else if (TREE_CODE (type) == FUNCTION_TYPE
14035 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14036 || type_memfn_rqual (type) != REF_QUAL_NONE))
14037 {
14038 if (complain & tf_error)
14039 {
14040 if (code == POINTER_TYPE)
14041 error ("forming pointer to qualified function type %qT",
14042 type);
14043 else
14044 error ("forming reference to qualified function type %qT",
14045 type);
14046 }
14047 return error_mark_node;
14048 }
14049 else if (code == POINTER_TYPE)
14050 {
14051 r = build_pointer_type (type);
14052 if (TREE_CODE (type) == METHOD_TYPE)
14053 r = build_ptrmemfunc_type (r);
14054 }
14055 else if (TREE_CODE (type) == REFERENCE_TYPE)
14056 /* In C++0x, during template argument substitution, when there is an
14057 attempt to create a reference to a reference type, reference
14058 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14059
14060 "If a template-argument for a template-parameter T names a type
14061 that is a reference to a type A, an attempt to create the type
14062 'lvalue reference to cv T' creates the type 'lvalue reference to
14063 A,' while an attempt to create the type type rvalue reference to
14064 cv T' creates the type T"
14065 */
14066 r = cp_build_reference_type
14067 (TREE_TYPE (type),
14068 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14069 else
14070 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14071 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14072
14073 if (r != error_mark_node)
14074 /* Will this ever be needed for TYPE_..._TO values? */
14075 layout_type (r);
14076
14077 return r;
14078 }
14079 case OFFSET_TYPE:
14080 {
14081 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14082 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14083 {
14084 /* [temp.deduct]
14085
14086 Type deduction may fail for any of the following
14087 reasons:
14088
14089 -- Attempting to create "pointer to member of T" when T
14090 is not a class type. */
14091 if (complain & tf_error)
14092 error ("creating pointer to member of non-class type %qT", r);
14093 return error_mark_node;
14094 }
14095 if (TREE_CODE (type) == REFERENCE_TYPE)
14096 {
14097 if (complain & tf_error)
14098 error ("creating pointer to member reference type %qT", type);
14099 return error_mark_node;
14100 }
14101 if (VOID_TYPE_P (type))
14102 {
14103 if (complain & tf_error)
14104 error ("creating pointer to member of type void");
14105 return error_mark_node;
14106 }
14107 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14108 if (TREE_CODE (type) == FUNCTION_TYPE)
14109 {
14110 /* The type of the implicit object parameter gets its
14111 cv-qualifiers from the FUNCTION_TYPE. */
14112 tree memptr;
14113 tree method_type
14114 = build_memfn_type (type, r, type_memfn_quals (type),
14115 type_memfn_rqual (type));
14116 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14117 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14118 complain);
14119 }
14120 else
14121 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14122 cp_type_quals (t),
14123 complain);
14124 }
14125 case FUNCTION_TYPE:
14126 case METHOD_TYPE:
14127 {
14128 tree fntype;
14129 tree specs;
14130 fntype = tsubst_function_type (t, args, complain, in_decl);
14131 if (fntype == error_mark_node)
14132 return error_mark_node;
14133
14134 /* Substitute the exception specification. */
14135 specs = tsubst_exception_specification (t, args, complain, in_decl,
14136 /*defer_ok*/fndecl_type);
14137 if (specs == error_mark_node)
14138 return error_mark_node;
14139 if (specs)
14140 fntype = build_exception_variant (fntype, specs);
14141 return fntype;
14142 }
14143 case ARRAY_TYPE:
14144 {
14145 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14146 if (domain == error_mark_node)
14147 return error_mark_node;
14148
14149 /* As an optimization, we avoid regenerating the array type if
14150 it will obviously be the same as T. */
14151 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14152 return t;
14153
14154 /* These checks should match the ones in create_array_type_for_decl.
14155
14156 [temp.deduct]
14157
14158 The deduction may fail for any of the following reasons:
14159
14160 -- Attempting to create an array with an element type that
14161 is void, a function type, or a reference type, or [DR337]
14162 an abstract class type. */
14163 if (VOID_TYPE_P (type)
14164 || TREE_CODE (type) == FUNCTION_TYPE
14165 || (TREE_CODE (type) == ARRAY_TYPE
14166 && TYPE_DOMAIN (type) == NULL_TREE)
14167 || TREE_CODE (type) == REFERENCE_TYPE)
14168 {
14169 if (complain & tf_error)
14170 error ("creating array of %qT", type);
14171 return error_mark_node;
14172 }
14173
14174 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14175 return error_mark_node;
14176
14177 r = build_cplus_array_type (type, domain);
14178
14179 if (TYPE_USER_ALIGN (t))
14180 {
14181 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14182 TYPE_USER_ALIGN (r) = 1;
14183 }
14184
14185 return r;
14186 }
14187
14188 case TYPENAME_TYPE:
14189 {
14190 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14191 in_decl, /*entering_scope=*/1);
14192 if (ctx == error_mark_node)
14193 return error_mark_node;
14194
14195 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14196 complain, in_decl);
14197 if (f == error_mark_node)
14198 return error_mark_node;
14199
14200 if (!MAYBE_CLASS_TYPE_P (ctx))
14201 {
14202 if (complain & tf_error)
14203 error ("%qT is not a class, struct, or union type", ctx);
14204 return error_mark_node;
14205 }
14206 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14207 {
14208 /* Normally, make_typename_type does not require that the CTX
14209 have complete type in order to allow things like:
14210
14211 template <class T> struct S { typename S<T>::X Y; };
14212
14213 But, such constructs have already been resolved by this
14214 point, so here CTX really should have complete type, unless
14215 it's a partial instantiation. */
14216 ctx = complete_type (ctx);
14217 if (!COMPLETE_TYPE_P (ctx))
14218 {
14219 if (complain & tf_error)
14220 cxx_incomplete_type_error (NULL_TREE, ctx);
14221 return error_mark_node;
14222 }
14223 }
14224
14225 f = make_typename_type (ctx, f, typename_type,
14226 complain | tf_keep_type_decl);
14227 if (f == error_mark_node)
14228 return f;
14229 if (TREE_CODE (f) == TYPE_DECL)
14230 {
14231 complain |= tf_ignore_bad_quals;
14232 f = TREE_TYPE (f);
14233 }
14234
14235 if (TREE_CODE (f) != TYPENAME_TYPE)
14236 {
14237 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14238 {
14239 if (complain & tf_error)
14240 error ("%qT resolves to %qT, which is not an enumeration type",
14241 t, f);
14242 else
14243 return error_mark_node;
14244 }
14245 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14246 {
14247 if (complain & tf_error)
14248 error ("%qT resolves to %qT, which is is not a class type",
14249 t, f);
14250 else
14251 return error_mark_node;
14252 }
14253 }
14254
14255 return cp_build_qualified_type_real
14256 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14257 }
14258
14259 case UNBOUND_CLASS_TEMPLATE:
14260 {
14261 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14262 in_decl, /*entering_scope=*/1);
14263 tree name = TYPE_IDENTIFIER (t);
14264 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14265
14266 if (ctx == error_mark_node || name == error_mark_node)
14267 return error_mark_node;
14268
14269 if (parm_list)
14270 parm_list = tsubst_template_parms (parm_list, args, complain);
14271 return make_unbound_class_template (ctx, name, parm_list, complain);
14272 }
14273
14274 case TYPEOF_TYPE:
14275 {
14276 tree type;
14277
14278 ++cp_unevaluated_operand;
14279 ++c_inhibit_evaluation_warnings;
14280
14281 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14282 complain, in_decl,
14283 /*integral_constant_expression_p=*/false);
14284
14285 --cp_unevaluated_operand;
14286 --c_inhibit_evaluation_warnings;
14287
14288 type = finish_typeof (type);
14289 return cp_build_qualified_type_real (type,
14290 cp_type_quals (t)
14291 | cp_type_quals (type),
14292 complain);
14293 }
14294
14295 case DECLTYPE_TYPE:
14296 {
14297 tree type;
14298
14299 ++cp_unevaluated_operand;
14300 ++c_inhibit_evaluation_warnings;
14301
14302 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14303 complain|tf_decltype, in_decl,
14304 /*function_p*/false,
14305 /*integral_constant_expression*/false);
14306
14307 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14308 {
14309 if (type == NULL_TREE)
14310 {
14311 if (complain & tf_error)
14312 error ("empty initializer in lambda init-capture");
14313 type = error_mark_node;
14314 }
14315 else if (TREE_CODE (type) == TREE_LIST)
14316 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14317 }
14318
14319 --cp_unevaluated_operand;
14320 --c_inhibit_evaluation_warnings;
14321
14322 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14323 type = lambda_capture_field_type (type,
14324 DECLTYPE_FOR_INIT_CAPTURE (t),
14325 DECLTYPE_FOR_REF_CAPTURE (t));
14326 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14327 type = lambda_proxy_type (type);
14328 else
14329 {
14330 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14331 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14332 && EXPR_P (type))
14333 /* In a template ~id could be either a complement expression
14334 or an unqualified-id naming a destructor; if instantiating
14335 it produces an expression, it's not an id-expression or
14336 member access. */
14337 id = false;
14338 type = finish_decltype_type (type, id, complain);
14339 }
14340 return cp_build_qualified_type_real (type,
14341 cp_type_quals (t)
14342 | cp_type_quals (type),
14343 complain | tf_ignore_bad_quals);
14344 }
14345
14346 case UNDERLYING_TYPE:
14347 {
14348 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14349 complain, in_decl);
14350 return finish_underlying_type (type);
14351 }
14352
14353 case TYPE_ARGUMENT_PACK:
14354 case NONTYPE_ARGUMENT_PACK:
14355 {
14356 tree r;
14357
14358 if (code == NONTYPE_ARGUMENT_PACK)
14359 r = make_node (code);
14360 else
14361 r = cxx_make_type (code);
14362
14363 tree pack_args = ARGUMENT_PACK_ARGS (t);
14364 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14365 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14366
14367 return r;
14368 }
14369
14370 case VOID_CST:
14371 case INTEGER_CST:
14372 case REAL_CST:
14373 case STRING_CST:
14374 case PLUS_EXPR:
14375 case MINUS_EXPR:
14376 case NEGATE_EXPR:
14377 case NOP_EXPR:
14378 case INDIRECT_REF:
14379 case ADDR_EXPR:
14380 case CALL_EXPR:
14381 case ARRAY_REF:
14382 case SCOPE_REF:
14383 /* We should use one of the expression tsubsts for these codes. */
14384 gcc_unreachable ();
14385
14386 default:
14387 sorry ("use of %qs in template", get_tree_code_name (code));
14388 return error_mark_node;
14389 }
14390 }
14391
14392 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14393 expression on the left-hand side of the "." or "->" operator. We
14394 only do the lookup if we had a dependent BASELINK. Otherwise we
14395 adjust it onto the instantiated heirarchy. */
14396
14397 static tree
14398 tsubst_baselink (tree baselink, tree object_type,
14399 tree args, tsubst_flags_t complain, tree in_decl)
14400 {
14401 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
14402 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14403 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14404
14405 tree optype = BASELINK_OPTYPE (baselink);
14406 optype = tsubst (optype, args, complain, in_decl);
14407
14408 tree template_args = NULL_TREE;
14409 bool template_id_p = false;
14410 tree fns = BASELINK_FUNCTIONS (baselink);
14411 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14412 {
14413 template_id_p = true;
14414 template_args = TREE_OPERAND (fns, 1);
14415 fns = TREE_OPERAND (fns, 0);
14416 if (template_args)
14417 template_args = tsubst_template_args (template_args, args,
14418 complain, in_decl);
14419 }
14420
14421 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
14422 binfo_type = tsubst (binfo_type, args, complain, in_decl);
14423 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
14424
14425 if (dependent_p)
14426 {
14427 tree name = OVL_NAME (fns);
14428 if (IDENTIFIER_CONV_OP_P (name))
14429 name = make_conv_op_name (optype);
14430
14431 if (name == complete_dtor_identifier)
14432 /* Treat as-if non-dependent below. */
14433 dependent_p = false;
14434
14435 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14436 if (!baselink)
14437 {
14438 if ((complain & tf_error)
14439 && constructor_name_p (name, qualifying_scope))
14440 error ("cannot call constructor %<%T::%D%> directly",
14441 qualifying_scope, name);
14442 return error_mark_node;
14443 }
14444
14445 if (BASELINK_P (baselink))
14446 fns = BASELINK_FUNCTIONS (baselink);
14447 }
14448 else
14449 {
14450 gcc_assert (optype == BASELINK_OPTYPE (baselink));
14451 /* We're going to overwrite pieces below, make a duplicate. */
14452 baselink = copy_node (baselink);
14453 }
14454
14455 /* If lookup found a single function, mark it as used at this point.
14456 (If lookup found multiple functions the one selected later by
14457 overload resolution will be marked as used at that point.) */
14458 if (!template_id_p && !really_overloaded_fn (fns)
14459 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14460 return error_mark_node;
14461
14462 if (BASELINK_P (baselink))
14463 {
14464 /* Add back the template arguments, if present. */
14465 if (template_id_p)
14466 BASELINK_FUNCTIONS (baselink)
14467 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
14468
14469 /* Update the conversion operator type. */
14470 BASELINK_OPTYPE (baselink) = optype;
14471 }
14472
14473 if (!object_type)
14474 object_type = current_class_type;
14475
14476 if (qualified_p || !dependent_p)
14477 {
14478 baselink = adjust_result_of_qualified_name_lookup (baselink,
14479 qualifying_scope,
14480 object_type);
14481 if (!qualified_p)
14482 /* We need to call adjust_result_of_qualified_name_lookup in case the
14483 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14484 so that we still get virtual function binding. */
14485 BASELINK_QUALIFIED_P (baselink) = false;
14486 }
14487
14488 return baselink;
14489 }
14490
14491 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14492 true if the qualified-id will be a postfix-expression in-and-of
14493 itself; false if more of the postfix-expression follows the
14494 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14495 of "&". */
14496
14497 static tree
14498 tsubst_qualified_id (tree qualified_id, tree args,
14499 tsubst_flags_t complain, tree in_decl,
14500 bool done, bool address_p)
14501 {
14502 tree expr;
14503 tree scope;
14504 tree name;
14505 bool is_template;
14506 tree template_args;
14507 location_t loc = UNKNOWN_LOCATION;
14508
14509 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14510
14511 /* Figure out what name to look up. */
14512 name = TREE_OPERAND (qualified_id, 1);
14513 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14514 {
14515 is_template = true;
14516 loc = EXPR_LOCATION (name);
14517 template_args = TREE_OPERAND (name, 1);
14518 if (template_args)
14519 template_args = tsubst_template_args (template_args, args,
14520 complain, in_decl);
14521 if (template_args == error_mark_node)
14522 return error_mark_node;
14523 name = TREE_OPERAND (name, 0);
14524 }
14525 else
14526 {
14527 is_template = false;
14528 template_args = NULL_TREE;
14529 }
14530
14531 /* Substitute into the qualifying scope. When there are no ARGS, we
14532 are just trying to simplify a non-dependent expression. In that
14533 case the qualifying scope may be dependent, and, in any case,
14534 substituting will not help. */
14535 scope = TREE_OPERAND (qualified_id, 0);
14536 if (args)
14537 {
14538 scope = tsubst (scope, args, complain, in_decl);
14539 expr = tsubst_copy (name, args, complain, in_decl);
14540 }
14541 else
14542 expr = name;
14543
14544 if (dependent_scope_p (scope))
14545 {
14546 if (is_template)
14547 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14548 tree r = build_qualified_name (NULL_TREE, scope, expr,
14549 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14550 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14551 return r;
14552 }
14553
14554 if (!BASELINK_P (name) && !DECL_P (expr))
14555 {
14556 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14557 {
14558 /* A BIT_NOT_EXPR is used to represent a destructor. */
14559 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14560 {
14561 error ("qualifying type %qT does not match destructor name ~%qT",
14562 scope, TREE_OPERAND (expr, 0));
14563 expr = error_mark_node;
14564 }
14565 else
14566 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14567 /*is_type_p=*/0, false);
14568 }
14569 else
14570 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14571 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14572 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14573 {
14574 if (complain & tf_error)
14575 {
14576 error ("dependent-name %qE is parsed as a non-type, but "
14577 "instantiation yields a type", qualified_id);
14578 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14579 }
14580 return error_mark_node;
14581 }
14582 }
14583
14584 if (DECL_P (expr))
14585 {
14586 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14587 scope);
14588 /* Remember that there was a reference to this entity. */
14589 if (!mark_used (expr, complain) && !(complain & tf_error))
14590 return error_mark_node;
14591 }
14592
14593 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14594 {
14595 if (complain & tf_error)
14596 qualified_name_lookup_error (scope,
14597 TREE_OPERAND (qualified_id, 1),
14598 expr, input_location);
14599 return error_mark_node;
14600 }
14601
14602 if (is_template)
14603 {
14604 if (variable_template_p (expr))
14605 expr = lookup_and_finish_template_variable (expr, template_args,
14606 complain);
14607 else
14608 expr = lookup_template_function (expr, template_args);
14609 }
14610
14611 if (expr == error_mark_node && complain & tf_error)
14612 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14613 expr, input_location);
14614 else if (TYPE_P (scope))
14615 {
14616 expr = (adjust_result_of_qualified_name_lookup
14617 (expr, scope, current_nonlambda_class_type ()));
14618 expr = (finish_qualified_id_expr
14619 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14620 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14621 /*template_arg_p=*/false, complain));
14622 }
14623
14624 /* Expressions do not generally have reference type. */
14625 if (TREE_CODE (expr) != SCOPE_REF
14626 /* However, if we're about to form a pointer-to-member, we just
14627 want the referenced member referenced. */
14628 && TREE_CODE (expr) != OFFSET_REF)
14629 expr = convert_from_reference (expr);
14630
14631 if (REF_PARENTHESIZED_P (qualified_id))
14632 expr = force_paren_expr (expr);
14633
14634 return expr;
14635 }
14636
14637 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14638 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14639 for tsubst. */
14640
14641 static tree
14642 tsubst_init (tree init, tree decl, tree args,
14643 tsubst_flags_t complain, tree in_decl)
14644 {
14645 if (!init)
14646 return NULL_TREE;
14647
14648 init = tsubst_expr (init, args, complain, in_decl, false);
14649
14650 if (!init && TREE_TYPE (decl) != error_mark_node)
14651 {
14652 /* If we had an initializer but it
14653 instantiated to nothing,
14654 value-initialize the object. This will
14655 only occur when the initializer was a
14656 pack expansion where the parameter packs
14657 used in that expansion were of length
14658 zero. */
14659 init = build_value_init (TREE_TYPE (decl),
14660 complain);
14661 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14662 init = get_target_expr_sfinae (init, complain);
14663 if (TREE_CODE (init) == TARGET_EXPR)
14664 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14665 }
14666
14667 return init;
14668 }
14669
14670 /* Like tsubst, but deals with expressions. This function just replaces
14671 template parms; to finish processing the resultant expression, use
14672 tsubst_copy_and_build or tsubst_expr. */
14673
14674 static tree
14675 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14676 {
14677 enum tree_code code;
14678 tree r;
14679
14680 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14681 return t;
14682
14683 code = TREE_CODE (t);
14684
14685 switch (code)
14686 {
14687 case PARM_DECL:
14688 r = retrieve_local_specialization (t);
14689
14690 if (r == NULL_TREE)
14691 {
14692 /* We get here for a use of 'this' in an NSDMI. */
14693 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14694 return current_class_ptr;
14695
14696 /* This can happen for a parameter name used later in a function
14697 declaration (such as in a late-specified return type). Just
14698 make a dummy decl, since it's only used for its type. */
14699 gcc_assert (cp_unevaluated_operand != 0);
14700 r = tsubst_decl (t, args, complain);
14701 /* Give it the template pattern as its context; its true context
14702 hasn't been instantiated yet and this is good enough for
14703 mangling. */
14704 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14705 }
14706
14707 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14708 r = ARGUMENT_PACK_SELECT_ARG (r);
14709 if (!mark_used (r, complain) && !(complain & tf_error))
14710 return error_mark_node;
14711 return r;
14712
14713 case CONST_DECL:
14714 {
14715 tree enum_type;
14716 tree v;
14717
14718 if (DECL_TEMPLATE_PARM_P (t))
14719 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14720 /* There is no need to substitute into namespace-scope
14721 enumerators. */
14722 if (DECL_NAMESPACE_SCOPE_P (t))
14723 return t;
14724 /* If ARGS is NULL, then T is known to be non-dependent. */
14725 if (args == NULL_TREE)
14726 return scalar_constant_value (t);
14727
14728 /* Unfortunately, we cannot just call lookup_name here.
14729 Consider:
14730
14731 template <int I> int f() {
14732 enum E { a = I };
14733 struct S { void g() { E e = a; } };
14734 };
14735
14736 When we instantiate f<7>::S::g(), say, lookup_name is not
14737 clever enough to find f<7>::a. */
14738 enum_type
14739 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14740 /*entering_scope=*/0);
14741
14742 for (v = TYPE_VALUES (enum_type);
14743 v != NULL_TREE;
14744 v = TREE_CHAIN (v))
14745 if (TREE_PURPOSE (v) == DECL_NAME (t))
14746 return TREE_VALUE (v);
14747
14748 /* We didn't find the name. That should never happen; if
14749 name-lookup found it during preliminary parsing, we
14750 should find it again here during instantiation. */
14751 gcc_unreachable ();
14752 }
14753 return t;
14754
14755 case FIELD_DECL:
14756 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14757 {
14758 /* Check for a local specialization set up by
14759 tsubst_pack_expansion. */
14760 if (tree r = retrieve_local_specialization (t))
14761 {
14762 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14763 r = ARGUMENT_PACK_SELECT_ARG (r);
14764 return r;
14765 }
14766
14767 /* When retrieving a capture pack from a generic lambda, remove the
14768 lambda call op's own template argument list from ARGS. Only the
14769 template arguments active for the closure type should be used to
14770 retrieve the pack specialization. */
14771 if (LAMBDA_FUNCTION_P (current_function_decl)
14772 && (template_class_depth (DECL_CONTEXT (t))
14773 != TMPL_ARGS_DEPTH (args)))
14774 args = strip_innermost_template_args (args, 1);
14775
14776 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14777 tsubst_decl put in the hash table. */
14778 return retrieve_specialization (t, args, 0);
14779 }
14780
14781 if (DECL_CONTEXT (t))
14782 {
14783 tree ctx;
14784
14785 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14786 /*entering_scope=*/1);
14787 if (ctx != DECL_CONTEXT (t))
14788 {
14789 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14790 if (!r)
14791 {
14792 if (complain & tf_error)
14793 error ("using invalid field %qD", t);
14794 return error_mark_node;
14795 }
14796 return r;
14797 }
14798 }
14799
14800 return t;
14801
14802 case VAR_DECL:
14803 case FUNCTION_DECL:
14804 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14805 r = tsubst (t, args, complain, in_decl);
14806 else if (local_variable_p (t)
14807 && uses_template_parms (DECL_CONTEXT (t)))
14808 {
14809 r = retrieve_local_specialization (t);
14810 if (r == NULL_TREE)
14811 {
14812 /* First try name lookup to find the instantiation. */
14813 r = lookup_name (DECL_NAME (t));
14814 if (r && !is_capture_proxy (r))
14815 {
14816 /* Make sure that the one we found is the one we want. */
14817 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
14818 if (ctx != DECL_CONTEXT (r))
14819 r = NULL_TREE;
14820 }
14821
14822 if (r)
14823 /* OK */;
14824 else
14825 {
14826 /* This can happen for a variable used in a
14827 late-specified return type of a local lambda, or for a
14828 local static or constant. Building a new VAR_DECL
14829 should be OK in all those cases. */
14830 r = tsubst_decl (t, args, complain);
14831 if (local_specializations)
14832 /* Avoid infinite recursion (79640). */
14833 register_local_specialization (r, t);
14834 if (decl_maybe_constant_var_p (r))
14835 {
14836 /* We can't call cp_finish_decl, so handle the
14837 initializer by hand. */
14838 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14839 complain, in_decl);
14840 if (!processing_template_decl)
14841 init = maybe_constant_init (init);
14842 if (processing_template_decl
14843 ? potential_constant_expression (init)
14844 : reduced_constant_expression_p (init))
14845 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14846 = TREE_CONSTANT (r) = true;
14847 DECL_INITIAL (r) = init;
14848 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14849 TREE_TYPE (r)
14850 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14851 complain, adc_variable_type);
14852 }
14853 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14854 || decl_constant_var_p (r)
14855 || errorcount || sorrycount);
14856 if (!processing_template_decl
14857 && !TREE_STATIC (r))
14858 r = process_outer_var_ref (r, complain);
14859 }
14860 /* Remember this for subsequent uses. */
14861 if (local_specializations)
14862 register_local_specialization (r, t);
14863 }
14864 }
14865 else
14866 r = t;
14867 if (!mark_used (r, complain))
14868 return error_mark_node;
14869 return r;
14870
14871 case NAMESPACE_DECL:
14872 return t;
14873
14874 case OVERLOAD:
14875 /* An OVERLOAD will always be a non-dependent overload set; an
14876 overload set from function scope will just be represented with an
14877 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14878 gcc_assert (!uses_template_parms (t));
14879 /* We must have marked any lookups as persistent. */
14880 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14881 return t;
14882
14883 case BASELINK:
14884 return tsubst_baselink (t, current_nonlambda_class_type (),
14885 args, complain, in_decl);
14886
14887 case TEMPLATE_DECL:
14888 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14889 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14890 args, complain, in_decl);
14891 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14892 return tsubst (t, args, complain, in_decl);
14893 else if (DECL_CLASS_SCOPE_P (t)
14894 && uses_template_parms (DECL_CONTEXT (t)))
14895 {
14896 /* Template template argument like the following example need
14897 special treatment:
14898
14899 template <template <class> class TT> struct C {};
14900 template <class T> struct D {
14901 template <class U> struct E {};
14902 C<E> c; // #1
14903 };
14904 D<int> d; // #2
14905
14906 We are processing the template argument `E' in #1 for
14907 the template instantiation #2. Originally, `E' is a
14908 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14909 have to substitute this with one having context `D<int>'. */
14910
14911 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14912 if (dependent_scope_p (context))
14913 {
14914 /* When rewriting a constructor into a deduction guide, a
14915 non-dependent name can become dependent, so memtmpl<args>
14916 becomes context::template memtmpl<args>. */
14917 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14918 return build_qualified_name (type, context, DECL_NAME (t),
14919 /*template*/true);
14920 }
14921 return lookup_field (context, DECL_NAME(t), 0, false);
14922 }
14923 else
14924 /* Ordinary template template argument. */
14925 return t;
14926
14927 case CAST_EXPR:
14928 case REINTERPRET_CAST_EXPR:
14929 case CONST_CAST_EXPR:
14930 case STATIC_CAST_EXPR:
14931 case DYNAMIC_CAST_EXPR:
14932 case IMPLICIT_CONV_EXPR:
14933 case CONVERT_EXPR:
14934 case NOP_EXPR:
14935 {
14936 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14937 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14938 return build1 (code, type, op0);
14939 }
14940
14941 case SIZEOF_EXPR:
14942 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14943 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14944 {
14945 tree expanded, op = TREE_OPERAND (t, 0);
14946 int len = 0;
14947
14948 if (SIZEOF_EXPR_TYPE_P (t))
14949 op = TREE_TYPE (op);
14950
14951 ++cp_unevaluated_operand;
14952 ++c_inhibit_evaluation_warnings;
14953 /* We only want to compute the number of arguments. */
14954 if (PACK_EXPANSION_P (op))
14955 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14956 else
14957 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14958 args, complain, in_decl);
14959 --cp_unevaluated_operand;
14960 --c_inhibit_evaluation_warnings;
14961
14962 if (TREE_CODE (expanded) == TREE_VEC)
14963 {
14964 len = TREE_VEC_LENGTH (expanded);
14965 /* Set TREE_USED for the benefit of -Wunused. */
14966 for (int i = 0; i < len; i++)
14967 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14968 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14969 }
14970
14971 if (expanded == error_mark_node)
14972 return error_mark_node;
14973 else if (PACK_EXPANSION_P (expanded)
14974 || (TREE_CODE (expanded) == TREE_VEC
14975 && pack_expansion_args_count (expanded)))
14976
14977 {
14978 if (PACK_EXPANSION_P (expanded))
14979 /* OK. */;
14980 else if (TREE_VEC_LENGTH (expanded) == 1)
14981 expanded = TREE_VEC_ELT (expanded, 0);
14982 else
14983 expanded = make_argument_pack (expanded);
14984
14985 if (TYPE_P (expanded))
14986 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14987 complain & tf_error);
14988 else
14989 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14990 complain & tf_error);
14991 }
14992 else
14993 return build_int_cst (size_type_node, len);
14994 }
14995 if (SIZEOF_EXPR_TYPE_P (t))
14996 {
14997 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14998 args, complain, in_decl);
14999 r = build1 (NOP_EXPR, r, error_mark_node);
15000 r = build1 (SIZEOF_EXPR,
15001 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15002 SIZEOF_EXPR_TYPE_P (r) = 1;
15003 return r;
15004 }
15005 /* Fall through */
15006
15007 case INDIRECT_REF:
15008 case NEGATE_EXPR:
15009 case TRUTH_NOT_EXPR:
15010 case BIT_NOT_EXPR:
15011 case ADDR_EXPR:
15012 case UNARY_PLUS_EXPR: /* Unary + */
15013 case ALIGNOF_EXPR:
15014 case AT_ENCODE_EXPR:
15015 case ARROW_EXPR:
15016 case THROW_EXPR:
15017 case TYPEID_EXPR:
15018 case REALPART_EXPR:
15019 case IMAGPART_EXPR:
15020 case PAREN_EXPR:
15021 {
15022 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15023 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15024 return build1 (code, type, op0);
15025 }
15026
15027 case COMPONENT_REF:
15028 {
15029 tree object;
15030 tree name;
15031
15032 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15033 name = TREE_OPERAND (t, 1);
15034 if (TREE_CODE (name) == BIT_NOT_EXPR)
15035 {
15036 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15037 complain, in_decl);
15038 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15039 }
15040 else if (TREE_CODE (name) == SCOPE_REF
15041 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15042 {
15043 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15044 complain, in_decl);
15045 name = TREE_OPERAND (name, 1);
15046 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15047 complain, in_decl);
15048 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15049 name = build_qualified_name (/*type=*/NULL_TREE,
15050 base, name,
15051 /*template_p=*/false);
15052 }
15053 else if (BASELINK_P (name))
15054 name = tsubst_baselink (name,
15055 non_reference (TREE_TYPE (object)),
15056 args, complain,
15057 in_decl);
15058 else
15059 name = tsubst_copy (name, args, complain, in_decl);
15060 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15061 }
15062
15063 case PLUS_EXPR:
15064 case MINUS_EXPR:
15065 case MULT_EXPR:
15066 case TRUNC_DIV_EXPR:
15067 case CEIL_DIV_EXPR:
15068 case FLOOR_DIV_EXPR:
15069 case ROUND_DIV_EXPR:
15070 case EXACT_DIV_EXPR:
15071 case BIT_AND_EXPR:
15072 case BIT_IOR_EXPR:
15073 case BIT_XOR_EXPR:
15074 case TRUNC_MOD_EXPR:
15075 case FLOOR_MOD_EXPR:
15076 case TRUTH_ANDIF_EXPR:
15077 case TRUTH_ORIF_EXPR:
15078 case TRUTH_AND_EXPR:
15079 case TRUTH_OR_EXPR:
15080 case RSHIFT_EXPR:
15081 case LSHIFT_EXPR:
15082 case RROTATE_EXPR:
15083 case LROTATE_EXPR:
15084 case EQ_EXPR:
15085 case NE_EXPR:
15086 case MAX_EXPR:
15087 case MIN_EXPR:
15088 case LE_EXPR:
15089 case GE_EXPR:
15090 case LT_EXPR:
15091 case GT_EXPR:
15092 case COMPOUND_EXPR:
15093 case DOTSTAR_EXPR:
15094 case MEMBER_REF:
15095 case PREDECREMENT_EXPR:
15096 case PREINCREMENT_EXPR:
15097 case POSTDECREMENT_EXPR:
15098 case POSTINCREMENT_EXPR:
15099 {
15100 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15101 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15102 return build_nt (code, op0, op1);
15103 }
15104
15105 case SCOPE_REF:
15106 {
15107 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15108 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15109 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15110 QUALIFIED_NAME_IS_TEMPLATE (t));
15111 }
15112
15113 case ARRAY_REF:
15114 {
15115 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15116 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15117 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15118 }
15119
15120 case CALL_EXPR:
15121 {
15122 int n = VL_EXP_OPERAND_LENGTH (t);
15123 tree result = build_vl_exp (CALL_EXPR, n);
15124 int i;
15125 for (i = 0; i < n; i++)
15126 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15127 complain, in_decl);
15128 return result;
15129 }
15130
15131 case COND_EXPR:
15132 case MODOP_EXPR:
15133 case PSEUDO_DTOR_EXPR:
15134 case VEC_PERM_EXPR:
15135 {
15136 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15137 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15138 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15139 r = build_nt (code, op0, op1, op2);
15140 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15141 return r;
15142 }
15143
15144 case NEW_EXPR:
15145 {
15146 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15147 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15148 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15149 r = build_nt (code, op0, op1, op2);
15150 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15151 return r;
15152 }
15153
15154 case DELETE_EXPR:
15155 {
15156 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15157 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15158 r = build_nt (code, op0, op1);
15159 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15160 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15161 return r;
15162 }
15163
15164 case TEMPLATE_ID_EXPR:
15165 {
15166 /* Substituted template arguments */
15167 tree fn = TREE_OPERAND (t, 0);
15168 tree targs = TREE_OPERAND (t, 1);
15169
15170 fn = tsubst_copy (fn, args, complain, in_decl);
15171 if (targs)
15172 targs = tsubst_template_args (targs, args, complain, in_decl);
15173
15174 return lookup_template_function (fn, targs);
15175 }
15176
15177 case TREE_LIST:
15178 {
15179 tree purpose, value, chain;
15180
15181 if (t == void_list_node)
15182 return t;
15183
15184 purpose = TREE_PURPOSE (t);
15185 if (purpose)
15186 purpose = tsubst_copy (purpose, args, complain, in_decl);
15187 value = TREE_VALUE (t);
15188 if (value)
15189 value = tsubst_copy (value, args, complain, in_decl);
15190 chain = TREE_CHAIN (t);
15191 if (chain && chain != void_type_node)
15192 chain = tsubst_copy (chain, args, complain, in_decl);
15193 if (purpose == TREE_PURPOSE (t)
15194 && value == TREE_VALUE (t)
15195 && chain == TREE_CHAIN (t))
15196 return t;
15197 return tree_cons (purpose, value, chain);
15198 }
15199
15200 case RECORD_TYPE:
15201 case UNION_TYPE:
15202 case ENUMERAL_TYPE:
15203 case INTEGER_TYPE:
15204 case TEMPLATE_TYPE_PARM:
15205 case TEMPLATE_TEMPLATE_PARM:
15206 case BOUND_TEMPLATE_TEMPLATE_PARM:
15207 case TEMPLATE_PARM_INDEX:
15208 case POINTER_TYPE:
15209 case REFERENCE_TYPE:
15210 case OFFSET_TYPE:
15211 case FUNCTION_TYPE:
15212 case METHOD_TYPE:
15213 case ARRAY_TYPE:
15214 case TYPENAME_TYPE:
15215 case UNBOUND_CLASS_TEMPLATE:
15216 case TYPEOF_TYPE:
15217 case DECLTYPE_TYPE:
15218 case TYPE_DECL:
15219 return tsubst (t, args, complain, in_decl);
15220
15221 case USING_DECL:
15222 t = DECL_NAME (t);
15223 /* Fall through. */
15224 case IDENTIFIER_NODE:
15225 if (IDENTIFIER_CONV_OP_P (t))
15226 {
15227 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15228 return make_conv_op_name (new_type);
15229 }
15230 else
15231 return t;
15232
15233 case CONSTRUCTOR:
15234 /* This is handled by tsubst_copy_and_build. */
15235 gcc_unreachable ();
15236
15237 case VA_ARG_EXPR:
15238 {
15239 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15240 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15241 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15242 }
15243
15244 case CLEANUP_POINT_EXPR:
15245 /* We shouldn't have built any of these during initial template
15246 generation. Instead, they should be built during instantiation
15247 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15248 gcc_unreachable ();
15249
15250 case OFFSET_REF:
15251 {
15252 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15253 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15254 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15255 r = build2 (code, type, op0, op1);
15256 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15257 if (!mark_used (TREE_OPERAND (r, 1), complain)
15258 && !(complain & tf_error))
15259 return error_mark_node;
15260 return r;
15261 }
15262
15263 case EXPR_PACK_EXPANSION:
15264 error ("invalid use of pack expansion expression");
15265 return error_mark_node;
15266
15267 case NONTYPE_ARGUMENT_PACK:
15268 error ("use %<...%> to expand argument pack");
15269 return error_mark_node;
15270
15271 case VOID_CST:
15272 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15273 return t;
15274
15275 case INTEGER_CST:
15276 case REAL_CST:
15277 case STRING_CST:
15278 case COMPLEX_CST:
15279 {
15280 /* Instantiate any typedefs in the type. */
15281 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15282 r = fold_convert (type, t);
15283 gcc_assert (TREE_CODE (r) == code);
15284 return r;
15285 }
15286
15287 case PTRMEM_CST:
15288 /* These can sometimes show up in a partial instantiation, but never
15289 involve template parms. */
15290 gcc_assert (!uses_template_parms (t));
15291 return t;
15292
15293 case UNARY_LEFT_FOLD_EXPR:
15294 return tsubst_unary_left_fold (t, args, complain, in_decl);
15295 case UNARY_RIGHT_FOLD_EXPR:
15296 return tsubst_unary_right_fold (t, args, complain, in_decl);
15297 case BINARY_LEFT_FOLD_EXPR:
15298 return tsubst_binary_left_fold (t, args, complain, in_decl);
15299 case BINARY_RIGHT_FOLD_EXPR:
15300 return tsubst_binary_right_fold (t, args, complain, in_decl);
15301 case PREDICT_EXPR:
15302 return t;
15303
15304 case DEBUG_BEGIN_STMT:
15305 /* ??? There's no point in copying it for now, but maybe some
15306 day it will contain more information, such as a pointer back
15307 to the containing function, inlined copy or so. */
15308 return t;
15309
15310 default:
15311 /* We shouldn't get here, but keep going if !flag_checking. */
15312 if (flag_checking)
15313 gcc_unreachable ();
15314 return t;
15315 }
15316 }
15317
15318 /* Helper function for tsubst_omp_clauses, used for instantiation of
15319 OMP_CLAUSE_DECL of clauses. */
15320
15321 static tree
15322 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15323 tree in_decl)
15324 {
15325 if (decl == NULL_TREE)
15326 return NULL_TREE;
15327
15328 /* Handle an OpenMP array section represented as a TREE_LIST (or
15329 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15330 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15331 TREE_LIST. We can handle it exactly the same as an array section
15332 (purpose, value, and a chain), even though the nomenclature
15333 (low_bound, length, etc) is different. */
15334 if (TREE_CODE (decl) == TREE_LIST)
15335 {
15336 tree low_bound
15337 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15338 /*integral_constant_expression_p=*/false);
15339 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15340 /*integral_constant_expression_p=*/false);
15341 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15342 in_decl);
15343 if (TREE_PURPOSE (decl) == low_bound
15344 && TREE_VALUE (decl) == length
15345 && TREE_CHAIN (decl) == chain)
15346 return decl;
15347 tree ret = tree_cons (low_bound, length, chain);
15348 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15349 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15350 return ret;
15351 }
15352 tree ret = tsubst_expr (decl, args, complain, in_decl,
15353 /*integral_constant_expression_p=*/false);
15354 /* Undo convert_from_reference tsubst_expr could have called. */
15355 if (decl
15356 && REFERENCE_REF_P (ret)
15357 && !REFERENCE_REF_P (decl))
15358 ret = TREE_OPERAND (ret, 0);
15359 return ret;
15360 }
15361
15362 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15363
15364 static tree
15365 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15366 tree args, tsubst_flags_t complain, tree in_decl)
15367 {
15368 tree new_clauses = NULL_TREE, nc, oc;
15369 tree linear_no_step = NULL_TREE;
15370
15371 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15372 {
15373 nc = copy_node (oc);
15374 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15375 new_clauses = nc;
15376
15377 switch (OMP_CLAUSE_CODE (nc))
15378 {
15379 case OMP_CLAUSE_LASTPRIVATE:
15380 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15381 {
15382 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15383 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15384 in_decl, /*integral_constant_expression_p=*/false);
15385 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15386 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15387 }
15388 /* FALLTHRU */
15389 case OMP_CLAUSE_PRIVATE:
15390 case OMP_CLAUSE_SHARED:
15391 case OMP_CLAUSE_FIRSTPRIVATE:
15392 case OMP_CLAUSE_COPYIN:
15393 case OMP_CLAUSE_COPYPRIVATE:
15394 case OMP_CLAUSE_UNIFORM:
15395 case OMP_CLAUSE_DEPEND:
15396 case OMP_CLAUSE_FROM:
15397 case OMP_CLAUSE_TO:
15398 case OMP_CLAUSE_MAP:
15399 case OMP_CLAUSE_USE_DEVICE_PTR:
15400 case OMP_CLAUSE_IS_DEVICE_PTR:
15401 OMP_CLAUSE_DECL (nc)
15402 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15403 in_decl);
15404 break;
15405 case OMP_CLAUSE_TILE:
15406 case OMP_CLAUSE_IF:
15407 case OMP_CLAUSE_NUM_THREADS:
15408 case OMP_CLAUSE_SCHEDULE:
15409 case OMP_CLAUSE_COLLAPSE:
15410 case OMP_CLAUSE_FINAL:
15411 case OMP_CLAUSE_DEVICE:
15412 case OMP_CLAUSE_DIST_SCHEDULE:
15413 case OMP_CLAUSE_NUM_TEAMS:
15414 case OMP_CLAUSE_THREAD_LIMIT:
15415 case OMP_CLAUSE_SAFELEN:
15416 case OMP_CLAUSE_SIMDLEN:
15417 case OMP_CLAUSE_NUM_TASKS:
15418 case OMP_CLAUSE_GRAINSIZE:
15419 case OMP_CLAUSE_PRIORITY:
15420 case OMP_CLAUSE_ORDERED:
15421 case OMP_CLAUSE_HINT:
15422 case OMP_CLAUSE_NUM_GANGS:
15423 case OMP_CLAUSE_NUM_WORKERS:
15424 case OMP_CLAUSE_VECTOR_LENGTH:
15425 case OMP_CLAUSE_WORKER:
15426 case OMP_CLAUSE_VECTOR:
15427 case OMP_CLAUSE_ASYNC:
15428 case OMP_CLAUSE_WAIT:
15429 OMP_CLAUSE_OPERAND (nc, 0)
15430 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15431 in_decl, /*integral_constant_expression_p=*/false);
15432 break;
15433 case OMP_CLAUSE_REDUCTION:
15434 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15435 {
15436 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15437 if (TREE_CODE (placeholder) == SCOPE_REF)
15438 {
15439 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15440 complain, in_decl);
15441 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15442 = build_qualified_name (NULL_TREE, scope,
15443 TREE_OPERAND (placeholder, 1),
15444 false);
15445 }
15446 else
15447 gcc_assert (identifier_p (placeholder));
15448 }
15449 OMP_CLAUSE_DECL (nc)
15450 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15451 in_decl);
15452 break;
15453 case OMP_CLAUSE_GANG:
15454 case OMP_CLAUSE_ALIGNED:
15455 OMP_CLAUSE_DECL (nc)
15456 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15457 in_decl);
15458 OMP_CLAUSE_OPERAND (nc, 1)
15459 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15460 in_decl, /*integral_constant_expression_p=*/false);
15461 break;
15462 case OMP_CLAUSE_LINEAR:
15463 OMP_CLAUSE_DECL (nc)
15464 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15465 in_decl);
15466 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15467 {
15468 gcc_assert (!linear_no_step);
15469 linear_no_step = nc;
15470 }
15471 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15472 OMP_CLAUSE_LINEAR_STEP (nc)
15473 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15474 complain, in_decl);
15475 else
15476 OMP_CLAUSE_LINEAR_STEP (nc)
15477 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15478 in_decl,
15479 /*integral_constant_expression_p=*/false);
15480 break;
15481 case OMP_CLAUSE_NOWAIT:
15482 case OMP_CLAUSE_DEFAULT:
15483 case OMP_CLAUSE_UNTIED:
15484 case OMP_CLAUSE_MERGEABLE:
15485 case OMP_CLAUSE_INBRANCH:
15486 case OMP_CLAUSE_NOTINBRANCH:
15487 case OMP_CLAUSE_PROC_BIND:
15488 case OMP_CLAUSE_FOR:
15489 case OMP_CLAUSE_PARALLEL:
15490 case OMP_CLAUSE_SECTIONS:
15491 case OMP_CLAUSE_TASKGROUP:
15492 case OMP_CLAUSE_NOGROUP:
15493 case OMP_CLAUSE_THREADS:
15494 case OMP_CLAUSE_SIMD:
15495 case OMP_CLAUSE_DEFAULTMAP:
15496 case OMP_CLAUSE_INDEPENDENT:
15497 case OMP_CLAUSE_AUTO:
15498 case OMP_CLAUSE_SEQ:
15499 break;
15500 default:
15501 gcc_unreachable ();
15502 }
15503 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15504 switch (OMP_CLAUSE_CODE (nc))
15505 {
15506 case OMP_CLAUSE_SHARED:
15507 case OMP_CLAUSE_PRIVATE:
15508 case OMP_CLAUSE_FIRSTPRIVATE:
15509 case OMP_CLAUSE_LASTPRIVATE:
15510 case OMP_CLAUSE_COPYPRIVATE:
15511 case OMP_CLAUSE_LINEAR:
15512 case OMP_CLAUSE_REDUCTION:
15513 case OMP_CLAUSE_USE_DEVICE_PTR:
15514 case OMP_CLAUSE_IS_DEVICE_PTR:
15515 /* tsubst_expr on SCOPE_REF results in returning
15516 finish_non_static_data_member result. Undo that here. */
15517 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15518 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15519 == IDENTIFIER_NODE))
15520 {
15521 tree t = OMP_CLAUSE_DECL (nc);
15522 tree v = t;
15523 while (v)
15524 switch (TREE_CODE (v))
15525 {
15526 case COMPONENT_REF:
15527 case MEM_REF:
15528 case INDIRECT_REF:
15529 CASE_CONVERT:
15530 case POINTER_PLUS_EXPR:
15531 v = TREE_OPERAND (v, 0);
15532 continue;
15533 case PARM_DECL:
15534 if (DECL_CONTEXT (v) == current_function_decl
15535 && DECL_ARTIFICIAL (v)
15536 && DECL_NAME (v) == this_identifier)
15537 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15538 /* FALLTHRU */
15539 default:
15540 v = NULL_TREE;
15541 break;
15542 }
15543 }
15544 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15545 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15546 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15547 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15548 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15549 {
15550 tree decl = OMP_CLAUSE_DECL (nc);
15551 if (VAR_P (decl))
15552 {
15553 retrofit_lang_decl (decl);
15554 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15555 }
15556 }
15557 break;
15558 default:
15559 break;
15560 }
15561 }
15562
15563 new_clauses = nreverse (new_clauses);
15564 if (ort != C_ORT_OMP_DECLARE_SIMD)
15565 {
15566 new_clauses = finish_omp_clauses (new_clauses, ort);
15567 if (linear_no_step)
15568 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15569 if (nc == linear_no_step)
15570 {
15571 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15572 break;
15573 }
15574 }
15575 return new_clauses;
15576 }
15577
15578 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15579
15580 static tree
15581 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15582 tree in_decl)
15583 {
15584 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15585
15586 tree purpose, value, chain;
15587
15588 if (t == NULL)
15589 return t;
15590
15591 if (TREE_CODE (t) != TREE_LIST)
15592 return tsubst_copy_and_build (t, args, complain, in_decl,
15593 /*function_p=*/false,
15594 /*integral_constant_expression_p=*/false);
15595
15596 if (t == void_list_node)
15597 return t;
15598
15599 purpose = TREE_PURPOSE (t);
15600 if (purpose)
15601 purpose = RECUR (purpose);
15602 value = TREE_VALUE (t);
15603 if (value)
15604 {
15605 if (TREE_CODE (value) != LABEL_DECL)
15606 value = RECUR (value);
15607 else
15608 {
15609 value = lookup_label (DECL_NAME (value));
15610 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15611 TREE_USED (value) = 1;
15612 }
15613 }
15614 chain = TREE_CHAIN (t);
15615 if (chain && chain != void_type_node)
15616 chain = RECUR (chain);
15617 return tree_cons (purpose, value, chain);
15618 #undef RECUR
15619 }
15620
15621 /* Used to temporarily communicate the list of #pragma omp parallel
15622 clauses to #pragma omp for instantiation if they are combined
15623 together. */
15624
15625 static tree *omp_parallel_combined_clauses;
15626
15627 /* Substitute one OMP_FOR iterator. */
15628
15629 static void
15630 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15631 tree initv, tree condv, tree incrv, tree *clauses,
15632 tree args, tsubst_flags_t complain, tree in_decl,
15633 bool integral_constant_expression_p)
15634 {
15635 #define RECUR(NODE) \
15636 tsubst_expr ((NODE), args, complain, in_decl, \
15637 integral_constant_expression_p)
15638 tree decl, init, cond, incr;
15639
15640 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15641 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15642
15643 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15644 {
15645 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15646 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15647 }
15648
15649 decl = TREE_OPERAND (init, 0);
15650 init = TREE_OPERAND (init, 1);
15651 tree decl_expr = NULL_TREE;
15652 if (init && TREE_CODE (init) == DECL_EXPR)
15653 {
15654 /* We need to jump through some hoops to handle declarations in the
15655 init-statement, since we might need to handle auto deduction,
15656 but we need to keep control of initialization. */
15657 decl_expr = init;
15658 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15659 decl = tsubst_decl (decl, args, complain);
15660 }
15661 else
15662 {
15663 if (TREE_CODE (decl) == SCOPE_REF)
15664 {
15665 decl = RECUR (decl);
15666 if (TREE_CODE (decl) == COMPONENT_REF)
15667 {
15668 tree v = decl;
15669 while (v)
15670 switch (TREE_CODE (v))
15671 {
15672 case COMPONENT_REF:
15673 case MEM_REF:
15674 case INDIRECT_REF:
15675 CASE_CONVERT:
15676 case POINTER_PLUS_EXPR:
15677 v = TREE_OPERAND (v, 0);
15678 continue;
15679 case PARM_DECL:
15680 if (DECL_CONTEXT (v) == current_function_decl
15681 && DECL_ARTIFICIAL (v)
15682 && DECL_NAME (v) == this_identifier)
15683 {
15684 decl = TREE_OPERAND (decl, 1);
15685 decl = omp_privatize_field (decl, false);
15686 }
15687 /* FALLTHRU */
15688 default:
15689 v = NULL_TREE;
15690 break;
15691 }
15692 }
15693 }
15694 else
15695 decl = RECUR (decl);
15696 }
15697 init = RECUR (init);
15698
15699 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15700 if (auto_node && init)
15701 TREE_TYPE (decl)
15702 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15703
15704 gcc_assert (!type_dependent_expression_p (decl));
15705
15706 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15707 {
15708 if (decl_expr)
15709 {
15710 /* Declare the variable, but don't let that initialize it. */
15711 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15712 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15713 RECUR (decl_expr);
15714 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15715 }
15716
15717 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15718 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15719 if (TREE_CODE (incr) == MODIFY_EXPR)
15720 {
15721 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15722 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15723 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15724 NOP_EXPR, rhs, complain);
15725 }
15726 else
15727 incr = RECUR (incr);
15728 TREE_VEC_ELT (declv, i) = decl;
15729 TREE_VEC_ELT (initv, i) = init;
15730 TREE_VEC_ELT (condv, i) = cond;
15731 TREE_VEC_ELT (incrv, i) = incr;
15732 return;
15733 }
15734
15735 if (decl_expr)
15736 {
15737 /* Declare and initialize the variable. */
15738 RECUR (decl_expr);
15739 init = NULL_TREE;
15740 }
15741 else if (init)
15742 {
15743 tree *pc;
15744 int j;
15745 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15746 {
15747 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15748 {
15749 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15750 && OMP_CLAUSE_DECL (*pc) == decl)
15751 break;
15752 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15753 && OMP_CLAUSE_DECL (*pc) == decl)
15754 {
15755 if (j)
15756 break;
15757 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15758 tree c = *pc;
15759 *pc = OMP_CLAUSE_CHAIN (c);
15760 OMP_CLAUSE_CHAIN (c) = *clauses;
15761 *clauses = c;
15762 }
15763 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15764 && OMP_CLAUSE_DECL (*pc) == decl)
15765 {
15766 error ("iteration variable %qD should not be firstprivate",
15767 decl);
15768 *pc = OMP_CLAUSE_CHAIN (*pc);
15769 }
15770 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15771 && OMP_CLAUSE_DECL (*pc) == decl)
15772 {
15773 error ("iteration variable %qD should not be reduction",
15774 decl);
15775 *pc = OMP_CLAUSE_CHAIN (*pc);
15776 }
15777 else
15778 pc = &OMP_CLAUSE_CHAIN (*pc);
15779 }
15780 if (*pc)
15781 break;
15782 }
15783 if (*pc == NULL_TREE)
15784 {
15785 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15786 OMP_CLAUSE_DECL (c) = decl;
15787 c = finish_omp_clauses (c, C_ORT_OMP);
15788 if (c)
15789 {
15790 OMP_CLAUSE_CHAIN (c) = *clauses;
15791 *clauses = c;
15792 }
15793 }
15794 }
15795 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15796 if (COMPARISON_CLASS_P (cond))
15797 {
15798 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15799 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15800 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15801 }
15802 else
15803 cond = RECUR (cond);
15804 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15805 switch (TREE_CODE (incr))
15806 {
15807 case PREINCREMENT_EXPR:
15808 case PREDECREMENT_EXPR:
15809 case POSTINCREMENT_EXPR:
15810 case POSTDECREMENT_EXPR:
15811 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15812 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15813 break;
15814 case MODIFY_EXPR:
15815 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15816 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15817 {
15818 tree rhs = TREE_OPERAND (incr, 1);
15819 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15820 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15821 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15822 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15823 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15824 rhs0, rhs1));
15825 }
15826 else
15827 incr = RECUR (incr);
15828 break;
15829 case MODOP_EXPR:
15830 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15831 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15832 {
15833 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15834 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15835 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15836 TREE_TYPE (decl), lhs,
15837 RECUR (TREE_OPERAND (incr, 2))));
15838 }
15839 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15840 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15841 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15842 {
15843 tree rhs = TREE_OPERAND (incr, 2);
15844 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15845 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15846 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15847 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15848 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15849 rhs0, rhs1));
15850 }
15851 else
15852 incr = RECUR (incr);
15853 break;
15854 default:
15855 incr = RECUR (incr);
15856 break;
15857 }
15858
15859 TREE_VEC_ELT (declv, i) = decl;
15860 TREE_VEC_ELT (initv, i) = init;
15861 TREE_VEC_ELT (condv, i) = cond;
15862 TREE_VEC_ELT (incrv, i) = incr;
15863 #undef RECUR
15864 }
15865
15866 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15867 of OMP_TARGET's body. */
15868
15869 static tree
15870 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15871 {
15872 *walk_subtrees = 0;
15873 switch (TREE_CODE (*tp))
15874 {
15875 case OMP_TEAMS:
15876 return *tp;
15877 case BIND_EXPR:
15878 case STATEMENT_LIST:
15879 *walk_subtrees = 1;
15880 break;
15881 default:
15882 break;
15883 }
15884 return NULL_TREE;
15885 }
15886
15887 /* Helper function for tsubst_expr. For decomposition declaration
15888 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15889 also the corresponding decls representing the identifiers
15890 of the decomposition declaration. Return DECL if successful
15891 or error_mark_node otherwise, set *FIRST to the first decl
15892 in the list chained through DECL_CHAIN and *CNT to the number
15893 of such decls. */
15894
15895 static tree
15896 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15897 tsubst_flags_t complain, tree in_decl, tree *first,
15898 unsigned int *cnt)
15899 {
15900 tree decl2, decl3, prev = decl;
15901 *cnt = 0;
15902 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15903 for (decl2 = DECL_CHAIN (pattern_decl);
15904 decl2
15905 && VAR_P (decl2)
15906 && DECL_DECOMPOSITION_P (decl2)
15907 && DECL_NAME (decl2);
15908 decl2 = DECL_CHAIN (decl2))
15909 {
15910 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15911 {
15912 gcc_assert (errorcount);
15913 return error_mark_node;
15914 }
15915 (*cnt)++;
15916 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15917 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15918 tree v = DECL_VALUE_EXPR (decl2);
15919 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15920 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15921 decl3 = tsubst (decl2, args, complain, in_decl);
15922 SET_DECL_VALUE_EXPR (decl2, v);
15923 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15924 if (VAR_P (decl3))
15925 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15926 maybe_push_decl (decl3);
15927 if (error_operand_p (decl3))
15928 decl = error_mark_node;
15929 else if (decl != error_mark_node
15930 && DECL_CHAIN (decl3) != prev)
15931 {
15932 gcc_assert (errorcount);
15933 decl = error_mark_node;
15934 }
15935 else
15936 prev = decl3;
15937 }
15938 *first = prev;
15939 return decl;
15940 }
15941
15942 /* Like tsubst_copy for expressions, etc. but also does semantic
15943 processing. */
15944
15945 tree
15946 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15947 bool integral_constant_expression_p)
15948 {
15949 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15950 #define RECUR(NODE) \
15951 tsubst_expr ((NODE), args, complain, in_decl, \
15952 integral_constant_expression_p)
15953
15954 tree stmt, tmp;
15955 tree r;
15956 location_t loc;
15957
15958 if (t == NULL_TREE || t == error_mark_node)
15959 return t;
15960
15961 loc = input_location;
15962 if (EXPR_HAS_LOCATION (t))
15963 input_location = EXPR_LOCATION (t);
15964 if (STATEMENT_CODE_P (TREE_CODE (t)))
15965 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15966
15967 switch (TREE_CODE (t))
15968 {
15969 case STATEMENT_LIST:
15970 {
15971 tree_stmt_iterator i;
15972 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15973 RECUR (tsi_stmt (i));
15974 break;
15975 }
15976
15977 case CTOR_INITIALIZER:
15978 finish_mem_initializers (tsubst_initializer_list
15979 (TREE_OPERAND (t, 0), args));
15980 break;
15981
15982 case RETURN_EXPR:
15983 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15984 break;
15985
15986 case EXPR_STMT:
15987 tmp = RECUR (EXPR_STMT_EXPR (t));
15988 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15989 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15990 else
15991 finish_expr_stmt (tmp);
15992 break;
15993
15994 case USING_STMT:
15995 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15996 /*attribs=*/NULL_TREE);
15997 break;
15998
15999 case DECL_EXPR:
16000 {
16001 tree decl, pattern_decl;
16002 tree init;
16003
16004 pattern_decl = decl = DECL_EXPR_DECL (t);
16005 if (TREE_CODE (decl) == LABEL_DECL)
16006 finish_label_decl (DECL_NAME (decl));
16007 else if (TREE_CODE (decl) == USING_DECL)
16008 {
16009 tree scope = USING_DECL_SCOPE (decl);
16010 tree name = DECL_NAME (decl);
16011
16012 scope = tsubst (scope, args, complain, in_decl);
16013 decl = lookup_qualified_name (scope, name,
16014 /*is_type_p=*/false,
16015 /*complain=*/false);
16016 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16017 qualified_name_lookup_error (scope, name, decl, input_location);
16018 else
16019 finish_local_using_decl (decl, scope, name);
16020 }
16021 else if (DECL_PACK_P (decl))
16022 {
16023 /* Don't build up decls for a variadic capture proxy, we'll
16024 instantiate the elements directly as needed. */
16025 break;
16026 }
16027 else if (is_capture_proxy (decl)
16028 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16029 {
16030 /* We're in tsubst_lambda_expr, we've already inserted a new
16031 capture proxy, so look it up and register it. */
16032 tree inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16033 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16034 gcc_assert (inst != decl && is_capture_proxy (inst));
16035 register_local_specialization (inst, decl);
16036 break;
16037 }
16038 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16039 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16040 /* Don't copy the old closure; we'll create a new one in
16041 tsubst_lambda_expr. */
16042 break;
16043 else
16044 {
16045 init = DECL_INITIAL (decl);
16046 decl = tsubst (decl, args, complain, in_decl);
16047 if (decl != error_mark_node)
16048 {
16049 /* By marking the declaration as instantiated, we avoid
16050 trying to instantiate it. Since instantiate_decl can't
16051 handle local variables, and since we've already done
16052 all that needs to be done, that's the right thing to
16053 do. */
16054 if (VAR_P (decl))
16055 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16056 if (VAR_P (decl)
16057 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16058 /* Anonymous aggregates are a special case. */
16059 finish_anon_union (decl);
16060 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16061 {
16062 DECL_CONTEXT (decl) = current_function_decl;
16063 if (DECL_NAME (decl) == this_identifier)
16064 {
16065 tree lam = DECL_CONTEXT (current_function_decl);
16066 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16067 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16068 }
16069 insert_capture_proxy (decl);
16070 }
16071 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16072 /* We already did a pushtag. */;
16073 else if (TREE_CODE (decl) == FUNCTION_DECL
16074 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16075 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16076 {
16077 DECL_CONTEXT (decl) = NULL_TREE;
16078 pushdecl (decl);
16079 DECL_CONTEXT (decl) = current_function_decl;
16080 cp_check_omp_declare_reduction (decl);
16081 }
16082 else
16083 {
16084 int const_init = false;
16085 maybe_push_decl (decl);
16086 if (VAR_P (decl)
16087 && DECL_PRETTY_FUNCTION_P (decl))
16088 {
16089 /* For __PRETTY_FUNCTION__ we have to adjust the
16090 initializer. */
16091 const char *const name
16092 = cxx_printable_name (current_function_decl, 2);
16093 init = cp_fname_init (name, &TREE_TYPE (decl));
16094 }
16095 else
16096 init = tsubst_init (init, decl, args, complain, in_decl);
16097
16098 if (VAR_P (decl))
16099 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16100 (pattern_decl));
16101 if (VAR_P (decl)
16102 && DECL_DECOMPOSITION_P (decl)
16103 && TREE_TYPE (pattern_decl) != error_mark_node)
16104 {
16105 unsigned int cnt;
16106 tree first;
16107 tree ndecl
16108 = tsubst_decomp_names (decl, pattern_decl, args,
16109 complain, in_decl, &first, &cnt);
16110 if (ndecl != error_mark_node)
16111 cp_maybe_mangle_decomp (ndecl, first, cnt);
16112 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16113 if (ndecl != error_mark_node)
16114 cp_finish_decomp (ndecl, first, cnt);
16115 }
16116 else
16117 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16118 }
16119 }
16120 }
16121
16122 break;
16123 }
16124
16125 case FOR_STMT:
16126 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16127 RECUR (FOR_INIT_STMT (t));
16128 finish_init_stmt (stmt);
16129 tmp = RECUR (FOR_COND (t));
16130 finish_for_cond (tmp, stmt, false, 0);
16131 tmp = RECUR (FOR_EXPR (t));
16132 finish_for_expr (tmp, stmt);
16133 {
16134 bool prev = note_iteration_stmt_body_start ();
16135 RECUR (FOR_BODY (t));
16136 note_iteration_stmt_body_end (prev);
16137 }
16138 finish_for_stmt (stmt);
16139 break;
16140
16141 case RANGE_FOR_STMT:
16142 {
16143 tree decl, expr;
16144 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16145 decl = RANGE_FOR_DECL (t);
16146 decl = tsubst (decl, args, complain, in_decl);
16147 maybe_push_decl (decl);
16148 expr = RECUR (RANGE_FOR_EXPR (t));
16149 const unsigned short unroll
16150 = RANGE_FOR_UNROLL (t) ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0;
16151 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16152 {
16153 unsigned int cnt;
16154 tree first;
16155 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16156 complain, in_decl, &first, &cnt);
16157 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
16158 RANGE_FOR_IVDEP (t), unroll);
16159 }
16160 else
16161 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
16162 RANGE_FOR_IVDEP (t), unroll);
16163 bool prev = note_iteration_stmt_body_start ();
16164 RECUR (RANGE_FOR_BODY (t));
16165 note_iteration_stmt_body_end (prev);
16166 finish_for_stmt (stmt);
16167 }
16168 break;
16169
16170 case WHILE_STMT:
16171 stmt = begin_while_stmt ();
16172 tmp = RECUR (WHILE_COND (t));
16173 finish_while_stmt_cond (tmp, stmt, false, 0);
16174 {
16175 bool prev = note_iteration_stmt_body_start ();
16176 RECUR (WHILE_BODY (t));
16177 note_iteration_stmt_body_end (prev);
16178 }
16179 finish_while_stmt (stmt);
16180 break;
16181
16182 case DO_STMT:
16183 stmt = begin_do_stmt ();
16184 {
16185 bool prev = note_iteration_stmt_body_start ();
16186 RECUR (DO_BODY (t));
16187 note_iteration_stmt_body_end (prev);
16188 }
16189 finish_do_body (stmt);
16190 tmp = RECUR (DO_COND (t));
16191 finish_do_stmt (tmp, stmt, false, 0);
16192 break;
16193
16194 case IF_STMT:
16195 stmt = begin_if_stmt ();
16196 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16197 tmp = RECUR (IF_COND (t));
16198 tmp = finish_if_stmt_cond (tmp, stmt);
16199 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16200 /* Don't instantiate the THEN_CLAUSE. */;
16201 else
16202 {
16203 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16204 if (inhibit)
16205 ++c_inhibit_evaluation_warnings;
16206 RECUR (THEN_CLAUSE (t));
16207 if (inhibit)
16208 --c_inhibit_evaluation_warnings;
16209 }
16210 finish_then_clause (stmt);
16211
16212 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16213 /* Don't instantiate the ELSE_CLAUSE. */;
16214 else if (ELSE_CLAUSE (t))
16215 {
16216 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16217 begin_else_clause (stmt);
16218 if (inhibit)
16219 ++c_inhibit_evaluation_warnings;
16220 RECUR (ELSE_CLAUSE (t));
16221 if (inhibit)
16222 --c_inhibit_evaluation_warnings;
16223 finish_else_clause (stmt);
16224 }
16225
16226 finish_if_stmt (stmt);
16227 break;
16228
16229 case BIND_EXPR:
16230 if (BIND_EXPR_BODY_BLOCK (t))
16231 stmt = begin_function_body ();
16232 else
16233 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16234 ? BCS_TRY_BLOCK : 0);
16235
16236 RECUR (BIND_EXPR_BODY (t));
16237
16238 if (BIND_EXPR_BODY_BLOCK (t))
16239 finish_function_body (stmt);
16240 else
16241 finish_compound_stmt (stmt);
16242 break;
16243
16244 case BREAK_STMT:
16245 finish_break_stmt ();
16246 break;
16247
16248 case CONTINUE_STMT:
16249 finish_continue_stmt ();
16250 break;
16251
16252 case SWITCH_STMT:
16253 stmt = begin_switch_stmt ();
16254 tmp = RECUR (SWITCH_STMT_COND (t));
16255 finish_switch_cond (tmp, stmt);
16256 RECUR (SWITCH_STMT_BODY (t));
16257 finish_switch_stmt (stmt);
16258 break;
16259
16260 case CASE_LABEL_EXPR:
16261 {
16262 tree low = RECUR (CASE_LOW (t));
16263 tree high = RECUR (CASE_HIGH (t));
16264 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16265 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16266 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16267 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16268 }
16269 break;
16270
16271 case LABEL_EXPR:
16272 {
16273 tree decl = LABEL_EXPR_LABEL (t);
16274 tree label;
16275
16276 label = finish_label_stmt (DECL_NAME (decl));
16277 if (TREE_CODE (label) == LABEL_DECL)
16278 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16279 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16280 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16281 }
16282 break;
16283
16284 case GOTO_EXPR:
16285 tmp = GOTO_DESTINATION (t);
16286 if (TREE_CODE (tmp) != LABEL_DECL)
16287 /* Computed goto's must be tsubst'd into. On the other hand,
16288 non-computed gotos must not be; the identifier in question
16289 will have no binding. */
16290 tmp = RECUR (tmp);
16291 else
16292 tmp = DECL_NAME (tmp);
16293 finish_goto_stmt (tmp);
16294 break;
16295
16296 case ASM_EXPR:
16297 {
16298 tree string = RECUR (ASM_STRING (t));
16299 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16300 complain, in_decl);
16301 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16302 complain, in_decl);
16303 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16304 complain, in_decl);
16305 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16306 complain, in_decl);
16307 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16308 clobbers, labels);
16309 tree asm_expr = tmp;
16310 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16311 asm_expr = TREE_OPERAND (asm_expr, 0);
16312 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16313 }
16314 break;
16315
16316 case TRY_BLOCK:
16317 if (CLEANUP_P (t))
16318 {
16319 stmt = begin_try_block ();
16320 RECUR (TRY_STMTS (t));
16321 finish_cleanup_try_block (stmt);
16322 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16323 }
16324 else
16325 {
16326 tree compound_stmt = NULL_TREE;
16327
16328 if (FN_TRY_BLOCK_P (t))
16329 stmt = begin_function_try_block (&compound_stmt);
16330 else
16331 stmt = begin_try_block ();
16332
16333 RECUR (TRY_STMTS (t));
16334
16335 if (FN_TRY_BLOCK_P (t))
16336 finish_function_try_block (stmt);
16337 else
16338 finish_try_block (stmt);
16339
16340 RECUR (TRY_HANDLERS (t));
16341 if (FN_TRY_BLOCK_P (t))
16342 finish_function_handler_sequence (stmt, compound_stmt);
16343 else
16344 finish_handler_sequence (stmt);
16345 }
16346 break;
16347
16348 case HANDLER:
16349 {
16350 tree decl = HANDLER_PARMS (t);
16351
16352 if (decl)
16353 {
16354 decl = tsubst (decl, args, complain, in_decl);
16355 /* Prevent instantiate_decl from trying to instantiate
16356 this variable. We've already done all that needs to be
16357 done. */
16358 if (decl != error_mark_node)
16359 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16360 }
16361 stmt = begin_handler ();
16362 finish_handler_parms (decl, stmt);
16363 RECUR (HANDLER_BODY (t));
16364 finish_handler (stmt);
16365 }
16366 break;
16367
16368 case TAG_DEFN:
16369 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16370 if (CLASS_TYPE_P (tmp))
16371 {
16372 /* Local classes are not independent templates; they are
16373 instantiated along with their containing function. And this
16374 way we don't have to deal with pushing out of one local class
16375 to instantiate a member of another local class. */
16376 /* Closures are handled by the LAMBDA_EXPR. */
16377 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16378 complete_type (tmp);
16379 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16380 if ((VAR_P (fld)
16381 || (TREE_CODE (fld) == FUNCTION_DECL
16382 && !DECL_ARTIFICIAL (fld)))
16383 && DECL_TEMPLATE_INSTANTIATION (fld))
16384 instantiate_decl (fld, /*defer_ok=*/false,
16385 /*expl_inst_class=*/false);
16386 }
16387 break;
16388
16389 case STATIC_ASSERT:
16390 {
16391 tree condition;
16392
16393 ++c_inhibit_evaluation_warnings;
16394 condition =
16395 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16396 args,
16397 complain, in_decl,
16398 /*integral_constant_expression_p=*/true);
16399 --c_inhibit_evaluation_warnings;
16400
16401 finish_static_assert (condition,
16402 STATIC_ASSERT_MESSAGE (t),
16403 STATIC_ASSERT_SOURCE_LOCATION (t),
16404 /*member_p=*/false);
16405 }
16406 break;
16407
16408 case OACC_KERNELS:
16409 case OACC_PARALLEL:
16410 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16411 in_decl);
16412 stmt = begin_omp_parallel ();
16413 RECUR (OMP_BODY (t));
16414 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16415 break;
16416
16417 case OMP_PARALLEL:
16418 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16419 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16420 complain, in_decl);
16421 if (OMP_PARALLEL_COMBINED (t))
16422 omp_parallel_combined_clauses = &tmp;
16423 stmt = begin_omp_parallel ();
16424 RECUR (OMP_PARALLEL_BODY (t));
16425 gcc_assert (omp_parallel_combined_clauses == NULL);
16426 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16427 = OMP_PARALLEL_COMBINED (t);
16428 pop_omp_privatization_clauses (r);
16429 break;
16430
16431 case OMP_TASK:
16432 r = push_omp_privatization_clauses (false);
16433 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16434 complain, in_decl);
16435 stmt = begin_omp_task ();
16436 RECUR (OMP_TASK_BODY (t));
16437 finish_omp_task (tmp, stmt);
16438 pop_omp_privatization_clauses (r);
16439 break;
16440
16441 case OMP_FOR:
16442 case OMP_SIMD:
16443 case OMP_DISTRIBUTE:
16444 case OMP_TASKLOOP:
16445 case OACC_LOOP:
16446 {
16447 tree clauses, body, pre_body;
16448 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16449 tree orig_declv = NULL_TREE;
16450 tree incrv = NULL_TREE;
16451 enum c_omp_region_type ort = C_ORT_OMP;
16452 int i;
16453
16454 if (TREE_CODE (t) == OACC_LOOP)
16455 ort = C_ORT_ACC;
16456
16457 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16458 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16459 in_decl);
16460 if (OMP_FOR_INIT (t) != NULL_TREE)
16461 {
16462 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16463 if (OMP_FOR_ORIG_DECLS (t))
16464 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16465 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16466 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16467 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16468 }
16469
16470 stmt = begin_omp_structured_block ();
16471
16472 pre_body = push_stmt_list ();
16473 RECUR (OMP_FOR_PRE_BODY (t));
16474 pre_body = pop_stmt_list (pre_body);
16475
16476 if (OMP_FOR_INIT (t) != NULL_TREE)
16477 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16478 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16479 incrv, &clauses, args, complain, in_decl,
16480 integral_constant_expression_p);
16481 omp_parallel_combined_clauses = NULL;
16482
16483 body = push_stmt_list ();
16484 RECUR (OMP_FOR_BODY (t));
16485 body = pop_stmt_list (body);
16486
16487 if (OMP_FOR_INIT (t) != NULL_TREE)
16488 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16489 orig_declv, initv, condv, incrv, body, pre_body,
16490 NULL, clauses);
16491 else
16492 {
16493 t = make_node (TREE_CODE (t));
16494 TREE_TYPE (t) = void_type_node;
16495 OMP_FOR_BODY (t) = body;
16496 OMP_FOR_PRE_BODY (t) = pre_body;
16497 OMP_FOR_CLAUSES (t) = clauses;
16498 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16499 add_stmt (t);
16500 }
16501
16502 add_stmt (finish_omp_structured_block (stmt));
16503 pop_omp_privatization_clauses (r);
16504 }
16505 break;
16506
16507 case OMP_SECTIONS:
16508 omp_parallel_combined_clauses = NULL;
16509 /* FALLTHRU */
16510 case OMP_SINGLE:
16511 case OMP_TEAMS:
16512 case OMP_CRITICAL:
16513 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16514 && OMP_TEAMS_COMBINED (t));
16515 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16516 in_decl);
16517 stmt = push_stmt_list ();
16518 RECUR (OMP_BODY (t));
16519 stmt = pop_stmt_list (stmt);
16520
16521 t = copy_node (t);
16522 OMP_BODY (t) = stmt;
16523 OMP_CLAUSES (t) = tmp;
16524 add_stmt (t);
16525 pop_omp_privatization_clauses (r);
16526 break;
16527
16528 case OACC_DATA:
16529 case OMP_TARGET_DATA:
16530 case OMP_TARGET:
16531 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16532 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16533 in_decl);
16534 keep_next_level (true);
16535 stmt = begin_omp_structured_block ();
16536
16537 RECUR (OMP_BODY (t));
16538 stmt = finish_omp_structured_block (stmt);
16539
16540 t = copy_node (t);
16541 OMP_BODY (t) = stmt;
16542 OMP_CLAUSES (t) = tmp;
16543 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16544 {
16545 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16546 if (teams)
16547 {
16548 /* For combined target teams, ensure the num_teams and
16549 thread_limit clause expressions are evaluated on the host,
16550 before entering the target construct. */
16551 tree c;
16552 for (c = OMP_TEAMS_CLAUSES (teams);
16553 c; c = OMP_CLAUSE_CHAIN (c))
16554 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16555 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16556 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16557 {
16558 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16559 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16560 if (expr == error_mark_node)
16561 continue;
16562 tmp = TARGET_EXPR_SLOT (expr);
16563 add_stmt (expr);
16564 OMP_CLAUSE_OPERAND (c, 0) = expr;
16565 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16566 OMP_CLAUSE_FIRSTPRIVATE);
16567 OMP_CLAUSE_DECL (tc) = tmp;
16568 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16569 OMP_TARGET_CLAUSES (t) = tc;
16570 }
16571 }
16572 }
16573 add_stmt (t);
16574 break;
16575
16576 case OACC_DECLARE:
16577 t = copy_node (t);
16578 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16579 complain, in_decl);
16580 OACC_DECLARE_CLAUSES (t) = tmp;
16581 add_stmt (t);
16582 break;
16583
16584 case OMP_TARGET_UPDATE:
16585 case OMP_TARGET_ENTER_DATA:
16586 case OMP_TARGET_EXIT_DATA:
16587 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16588 complain, in_decl);
16589 t = copy_node (t);
16590 OMP_STANDALONE_CLAUSES (t) = tmp;
16591 add_stmt (t);
16592 break;
16593
16594 case OACC_ENTER_DATA:
16595 case OACC_EXIT_DATA:
16596 case OACC_UPDATE:
16597 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16598 complain, in_decl);
16599 t = copy_node (t);
16600 OMP_STANDALONE_CLAUSES (t) = tmp;
16601 add_stmt (t);
16602 break;
16603
16604 case OMP_ORDERED:
16605 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16606 complain, in_decl);
16607 stmt = push_stmt_list ();
16608 RECUR (OMP_BODY (t));
16609 stmt = pop_stmt_list (stmt);
16610
16611 t = copy_node (t);
16612 OMP_BODY (t) = stmt;
16613 OMP_ORDERED_CLAUSES (t) = tmp;
16614 add_stmt (t);
16615 break;
16616
16617 case OMP_SECTION:
16618 case OMP_MASTER:
16619 case OMP_TASKGROUP:
16620 stmt = push_stmt_list ();
16621 RECUR (OMP_BODY (t));
16622 stmt = pop_stmt_list (stmt);
16623
16624 t = copy_node (t);
16625 OMP_BODY (t) = stmt;
16626 add_stmt (t);
16627 break;
16628
16629 case OMP_ATOMIC:
16630 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16631 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16632 {
16633 tree op1 = TREE_OPERAND (t, 1);
16634 tree rhs1 = NULL_TREE;
16635 tree lhs, rhs;
16636 if (TREE_CODE (op1) == COMPOUND_EXPR)
16637 {
16638 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16639 op1 = TREE_OPERAND (op1, 1);
16640 }
16641 lhs = RECUR (TREE_OPERAND (op1, 0));
16642 rhs = RECUR (TREE_OPERAND (op1, 1));
16643 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16644 NULL_TREE, NULL_TREE, rhs1,
16645 OMP_ATOMIC_SEQ_CST (t));
16646 }
16647 else
16648 {
16649 tree op1 = TREE_OPERAND (t, 1);
16650 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16651 tree rhs1 = NULL_TREE;
16652 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16653 enum tree_code opcode = NOP_EXPR;
16654 if (code == OMP_ATOMIC_READ)
16655 {
16656 v = RECUR (TREE_OPERAND (op1, 0));
16657 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16658 }
16659 else if (code == OMP_ATOMIC_CAPTURE_OLD
16660 || code == OMP_ATOMIC_CAPTURE_NEW)
16661 {
16662 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16663 v = RECUR (TREE_OPERAND (op1, 0));
16664 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16665 if (TREE_CODE (op11) == COMPOUND_EXPR)
16666 {
16667 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16668 op11 = TREE_OPERAND (op11, 1);
16669 }
16670 lhs = RECUR (TREE_OPERAND (op11, 0));
16671 rhs = RECUR (TREE_OPERAND (op11, 1));
16672 opcode = TREE_CODE (op11);
16673 if (opcode == MODIFY_EXPR)
16674 opcode = NOP_EXPR;
16675 }
16676 else
16677 {
16678 code = OMP_ATOMIC;
16679 lhs = RECUR (TREE_OPERAND (op1, 0));
16680 rhs = RECUR (TREE_OPERAND (op1, 1));
16681 }
16682 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16683 OMP_ATOMIC_SEQ_CST (t));
16684 }
16685 break;
16686
16687 case TRANSACTION_EXPR:
16688 {
16689 int flags = 0;
16690 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16691 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16692
16693 if (TRANSACTION_EXPR_IS_STMT (t))
16694 {
16695 tree body = TRANSACTION_EXPR_BODY (t);
16696 tree noex = NULL_TREE;
16697 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16698 {
16699 noex = MUST_NOT_THROW_COND (body);
16700 if (noex == NULL_TREE)
16701 noex = boolean_true_node;
16702 body = TREE_OPERAND (body, 0);
16703 }
16704 stmt = begin_transaction_stmt (input_location, NULL, flags);
16705 RECUR (body);
16706 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16707 }
16708 else
16709 {
16710 stmt = build_transaction_expr (EXPR_LOCATION (t),
16711 RECUR (TRANSACTION_EXPR_BODY (t)),
16712 flags, NULL_TREE);
16713 RETURN (stmt);
16714 }
16715 }
16716 break;
16717
16718 case MUST_NOT_THROW_EXPR:
16719 {
16720 tree op0 = RECUR (TREE_OPERAND (t, 0));
16721 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16722 RETURN (build_must_not_throw_expr (op0, cond));
16723 }
16724
16725 case EXPR_PACK_EXPANSION:
16726 error ("invalid use of pack expansion expression");
16727 RETURN (error_mark_node);
16728
16729 case NONTYPE_ARGUMENT_PACK:
16730 error ("use %<...%> to expand argument pack");
16731 RETURN (error_mark_node);
16732
16733 case COMPOUND_EXPR:
16734 tmp = RECUR (TREE_OPERAND (t, 0));
16735 if (tmp == NULL_TREE)
16736 /* If the first operand was a statement, we're done with it. */
16737 RETURN (RECUR (TREE_OPERAND (t, 1)));
16738 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16739 RECUR (TREE_OPERAND (t, 1)),
16740 complain));
16741
16742 case ANNOTATE_EXPR:
16743 tmp = RECUR (TREE_OPERAND (t, 0));
16744 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16745 TREE_TYPE (tmp), tmp,
16746 RECUR (TREE_OPERAND (t, 1)),
16747 RECUR (TREE_OPERAND (t, 2))));
16748
16749 default:
16750 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16751
16752 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16753 /*function_p=*/false,
16754 integral_constant_expression_p));
16755 }
16756
16757 RETURN (NULL_TREE);
16758 out:
16759 input_location = loc;
16760 return r;
16761 #undef RECUR
16762 #undef RETURN
16763 }
16764
16765 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16766 function. For description of the body see comment above
16767 cp_parser_omp_declare_reduction_exprs. */
16768
16769 static void
16770 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16771 {
16772 if (t == NULL_TREE || t == error_mark_node)
16773 return;
16774
16775 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16776
16777 tree_stmt_iterator tsi;
16778 int i;
16779 tree stmts[7];
16780 memset (stmts, 0, sizeof stmts);
16781 for (i = 0, tsi = tsi_start (t);
16782 i < 7 && !tsi_end_p (tsi);
16783 i++, tsi_next (&tsi))
16784 stmts[i] = tsi_stmt (tsi);
16785 gcc_assert (tsi_end_p (tsi));
16786
16787 if (i >= 3)
16788 {
16789 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16790 && TREE_CODE (stmts[1]) == DECL_EXPR);
16791 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16792 args, complain, in_decl);
16793 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16794 args, complain, in_decl);
16795 DECL_CONTEXT (omp_out) = current_function_decl;
16796 DECL_CONTEXT (omp_in) = current_function_decl;
16797 keep_next_level (true);
16798 tree block = begin_omp_structured_block ();
16799 tsubst_expr (stmts[2], args, complain, in_decl, false);
16800 block = finish_omp_structured_block (block);
16801 block = maybe_cleanup_point_expr_void (block);
16802 add_decl_expr (omp_out);
16803 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16804 TREE_NO_WARNING (omp_out) = 1;
16805 add_decl_expr (omp_in);
16806 finish_expr_stmt (block);
16807 }
16808 if (i >= 6)
16809 {
16810 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16811 && TREE_CODE (stmts[4]) == DECL_EXPR);
16812 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16813 args, complain, in_decl);
16814 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16815 args, complain, in_decl);
16816 DECL_CONTEXT (omp_priv) = current_function_decl;
16817 DECL_CONTEXT (omp_orig) = current_function_decl;
16818 keep_next_level (true);
16819 tree block = begin_omp_structured_block ();
16820 tsubst_expr (stmts[5], args, complain, in_decl, false);
16821 block = finish_omp_structured_block (block);
16822 block = maybe_cleanup_point_expr_void (block);
16823 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16824 add_decl_expr (omp_priv);
16825 add_decl_expr (omp_orig);
16826 finish_expr_stmt (block);
16827 if (i == 7)
16828 add_decl_expr (omp_orig);
16829 }
16830 }
16831
16832 /* T is a postfix-expression that is not being used in a function
16833 call. Return the substituted version of T. */
16834
16835 static tree
16836 tsubst_non_call_postfix_expression (tree t, tree args,
16837 tsubst_flags_t complain,
16838 tree in_decl)
16839 {
16840 if (TREE_CODE (t) == SCOPE_REF)
16841 t = tsubst_qualified_id (t, args, complain, in_decl,
16842 /*done=*/false, /*address_p=*/false);
16843 else
16844 t = tsubst_copy_and_build (t, args, complain, in_decl,
16845 /*function_p=*/false,
16846 /*integral_constant_expression_p=*/false);
16847
16848 return t;
16849 }
16850
16851 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16852 instantiation context. Instantiating a pack expansion containing a lambda
16853 might result in multiple lambdas all based on the same lambda in the
16854 template. */
16855
16856 tree
16857 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16858 {
16859 tree oldfn = lambda_function (t);
16860 in_decl = oldfn;
16861
16862 tree r = build_lambda_expr ();
16863
16864 LAMBDA_EXPR_LOCATION (r)
16865 = LAMBDA_EXPR_LOCATION (t);
16866 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16867 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16868 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16869
16870 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16871 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16872 else
16873 record_lambda_scope (r);
16874
16875 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16876 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16877
16878 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16879 cap = TREE_CHAIN (cap))
16880 {
16881 tree field = TREE_PURPOSE (cap);
16882 if (PACK_EXPANSION_P (field))
16883 field = PACK_EXPANSION_PATTERN (field);
16884 field = tsubst_decl (field, args, complain);
16885
16886 if (field == error_mark_node)
16887 return error_mark_node;
16888
16889 tree init = TREE_VALUE (cap);
16890 if (PACK_EXPANSION_P (init))
16891 init = tsubst_pack_expansion (init, args, complain, in_decl);
16892 else
16893 init = tsubst_copy_and_build (init, args, complain, in_decl,
16894 /*fn*/false, /*constexpr*/false);
16895
16896 if (TREE_CODE (field) == TREE_VEC)
16897 {
16898 int len = TREE_VEC_LENGTH (field);
16899 gcc_assert (TREE_CODE (init) == TREE_VEC
16900 && TREE_VEC_LENGTH (init) == len);
16901 for (int i = 0; i < len; ++i)
16902 LAMBDA_EXPR_CAPTURE_LIST (r)
16903 = tree_cons (TREE_VEC_ELT (field, i),
16904 TREE_VEC_ELT (init, i),
16905 LAMBDA_EXPR_CAPTURE_LIST (r));
16906 }
16907 else
16908 {
16909 LAMBDA_EXPR_CAPTURE_LIST (r)
16910 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
16911
16912 if (id_equal (DECL_NAME (field), "__this"))
16913 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
16914 }
16915 }
16916
16917 tree type = begin_lambda_type (r);
16918
16919 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16920 determine_visibility (TYPE_NAME (type));
16921
16922 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
16923
16924 tree oldtmpl = (generic_lambda_fn_p (oldfn)
16925 ? DECL_TI_TEMPLATE (oldfn)
16926 : NULL_TREE);
16927
16928 tree fntype = static_fn_type (oldfn);
16929 if (oldtmpl)
16930 ++processing_template_decl;
16931 fntype = tsubst (fntype, args, complain, in_decl);
16932 if (oldtmpl)
16933 --processing_template_decl;
16934
16935 if (fntype == error_mark_node)
16936 r = error_mark_node;
16937 else
16938 {
16939 /* Fix the type of 'this'. */
16940 fntype = build_memfn_type (fntype, type,
16941 type_memfn_quals (fntype),
16942 type_memfn_rqual (fntype));
16943 tree fn, tmpl;
16944 if (oldtmpl)
16945 {
16946 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
16947 fn = DECL_TEMPLATE_RESULT (tmpl);
16948 finish_member_declaration (tmpl);
16949 }
16950 else
16951 {
16952 tmpl = NULL_TREE;
16953 fn = tsubst_function_decl (oldfn, args, complain, fntype);
16954 finish_member_declaration (fn);
16955 }
16956
16957 /* Let finish_function set this. */
16958 DECL_DECLARED_CONSTEXPR_P (fn) = false;
16959
16960 bool nested = cfun;
16961 if (nested)
16962 push_function_context ();
16963
16964 local_specialization_stack s (lss_copy);
16965
16966 tree body = start_lambda_function (fn, r);
16967
16968 register_parameter_specializations (oldfn, fn);
16969
16970 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
16971 /*constexpr*/false);
16972
16973 finish_lambda_function (body);
16974
16975 if (nested)
16976 pop_function_context ();
16977
16978 /* The capture list was built up in reverse order; fix that now. */
16979 LAMBDA_EXPR_CAPTURE_LIST (r)
16980 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
16981
16982 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16983
16984 maybe_add_lambda_conv_op (type);
16985 }
16986
16987 finish_struct (type, /*attr*/NULL_TREE);
16988
16989 insert_pending_capture_proxies ();
16990
16991 return r;
16992 }
16993
16994 /* Like tsubst but deals with expressions and performs semantic
16995 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16996
16997 tree
16998 tsubst_copy_and_build (tree t,
16999 tree args,
17000 tsubst_flags_t complain,
17001 tree in_decl,
17002 bool function_p,
17003 bool integral_constant_expression_p)
17004 {
17005 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17006 #define RECUR(NODE) \
17007 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17008 /*function_p=*/false, \
17009 integral_constant_expression_p)
17010
17011 tree retval, op1;
17012 location_t loc;
17013
17014 if (t == NULL_TREE || t == error_mark_node)
17015 return t;
17016
17017 loc = input_location;
17018 if (EXPR_HAS_LOCATION (t))
17019 input_location = EXPR_LOCATION (t);
17020
17021 /* N3276 decltype magic only applies to calls at the top level or on the
17022 right side of a comma. */
17023 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17024 complain &= ~tf_decltype;
17025
17026 switch (TREE_CODE (t))
17027 {
17028 case USING_DECL:
17029 t = DECL_NAME (t);
17030 /* Fall through. */
17031 case IDENTIFIER_NODE:
17032 {
17033 tree decl;
17034 cp_id_kind idk;
17035 bool non_integral_constant_expression_p;
17036 const char *error_msg;
17037
17038 if (IDENTIFIER_CONV_OP_P (t))
17039 {
17040 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17041 t = make_conv_op_name (new_type);
17042 }
17043
17044 /* Look up the name. */
17045 decl = lookup_name (t);
17046
17047 /* By convention, expressions use ERROR_MARK_NODE to indicate
17048 failure, not NULL_TREE. */
17049 if (decl == NULL_TREE)
17050 decl = error_mark_node;
17051
17052 decl = finish_id_expression (t, decl, NULL_TREE,
17053 &idk,
17054 integral_constant_expression_p,
17055 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17056 &non_integral_constant_expression_p,
17057 /*template_p=*/false,
17058 /*done=*/true,
17059 /*address_p=*/false,
17060 /*template_arg_p=*/false,
17061 &error_msg,
17062 input_location);
17063 if (error_msg)
17064 error (error_msg);
17065 if (!function_p && identifier_p (decl))
17066 {
17067 if (complain & tf_error)
17068 unqualified_name_lookup_error (decl);
17069 decl = error_mark_node;
17070 }
17071 RETURN (decl);
17072 }
17073
17074 case TEMPLATE_ID_EXPR:
17075 {
17076 tree object;
17077 tree templ = RECUR (TREE_OPERAND (t, 0));
17078 tree targs = TREE_OPERAND (t, 1);
17079
17080 if (targs)
17081 targs = tsubst_template_args (targs, args, complain, in_decl);
17082 if (targs == error_mark_node)
17083 return error_mark_node;
17084
17085 if (TREE_CODE (templ) == SCOPE_REF)
17086 {
17087 tree name = TREE_OPERAND (templ, 1);
17088 tree tid = lookup_template_function (name, targs);
17089 TREE_OPERAND (templ, 1) = tid;
17090 return templ;
17091 }
17092
17093 if (variable_template_p (templ))
17094 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17095
17096 if (TREE_CODE (templ) == COMPONENT_REF)
17097 {
17098 object = TREE_OPERAND (templ, 0);
17099 templ = TREE_OPERAND (templ, 1);
17100 }
17101 else
17102 object = NULL_TREE;
17103 templ = lookup_template_function (templ, targs);
17104
17105 if (object)
17106 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17107 object, templ, NULL_TREE));
17108 else
17109 RETURN (baselink_for_fns (templ));
17110 }
17111
17112 case INDIRECT_REF:
17113 {
17114 tree r = RECUR (TREE_OPERAND (t, 0));
17115
17116 if (REFERENCE_REF_P (t))
17117 {
17118 /* A type conversion to reference type will be enclosed in
17119 such an indirect ref, but the substitution of the cast
17120 will have also added such an indirect ref. */
17121 r = convert_from_reference (r);
17122 }
17123 else
17124 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17125 complain|decltype_flag);
17126
17127 if (TREE_CODE (r) == INDIRECT_REF)
17128 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17129
17130 RETURN (r);
17131 }
17132
17133 case NOP_EXPR:
17134 {
17135 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17136 tree op0 = RECUR (TREE_OPERAND (t, 0));
17137 RETURN (build_nop (type, op0));
17138 }
17139
17140 case IMPLICIT_CONV_EXPR:
17141 {
17142 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17143 tree expr = RECUR (TREE_OPERAND (t, 0));
17144 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17145 {
17146 retval = copy_node (t);
17147 TREE_TYPE (retval) = type;
17148 TREE_OPERAND (retval, 0) = expr;
17149 RETURN (retval);
17150 }
17151 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17152 /* We'll pass this to convert_nontype_argument again, we don't need
17153 to actually perform any conversion here. */
17154 RETURN (expr);
17155 int flags = LOOKUP_IMPLICIT;
17156 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17157 flags = LOOKUP_NORMAL;
17158 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17159 flags));
17160 }
17161
17162 case CONVERT_EXPR:
17163 {
17164 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17165 tree op0 = RECUR (TREE_OPERAND (t, 0));
17166 RETURN (build1 (CONVERT_EXPR, type, op0));
17167 }
17168
17169 case CAST_EXPR:
17170 case REINTERPRET_CAST_EXPR:
17171 case CONST_CAST_EXPR:
17172 case DYNAMIC_CAST_EXPR:
17173 case STATIC_CAST_EXPR:
17174 {
17175 tree type;
17176 tree op, r = NULL_TREE;
17177
17178 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17179 if (integral_constant_expression_p
17180 && !cast_valid_in_integral_constant_expression_p (type))
17181 {
17182 if (complain & tf_error)
17183 error ("a cast to a type other than an integral or "
17184 "enumeration type cannot appear in a constant-expression");
17185 RETURN (error_mark_node);
17186 }
17187
17188 op = RECUR (TREE_OPERAND (t, 0));
17189
17190 warning_sentinel s(warn_useless_cast);
17191 warning_sentinel s2(warn_ignored_qualifiers);
17192 switch (TREE_CODE (t))
17193 {
17194 case CAST_EXPR:
17195 r = build_functional_cast (type, op, complain);
17196 break;
17197 case REINTERPRET_CAST_EXPR:
17198 r = build_reinterpret_cast (type, op, complain);
17199 break;
17200 case CONST_CAST_EXPR:
17201 r = build_const_cast (type, op, complain);
17202 break;
17203 case DYNAMIC_CAST_EXPR:
17204 r = build_dynamic_cast (type, op, complain);
17205 break;
17206 case STATIC_CAST_EXPR:
17207 r = build_static_cast (type, op, complain);
17208 break;
17209 default:
17210 gcc_unreachable ();
17211 }
17212
17213 RETURN (r);
17214 }
17215
17216 case POSTDECREMENT_EXPR:
17217 case POSTINCREMENT_EXPR:
17218 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17219 args, complain, in_decl);
17220 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17221 complain|decltype_flag));
17222
17223 case PREDECREMENT_EXPR:
17224 case PREINCREMENT_EXPR:
17225 case NEGATE_EXPR:
17226 case BIT_NOT_EXPR:
17227 case ABS_EXPR:
17228 case TRUTH_NOT_EXPR:
17229 case UNARY_PLUS_EXPR: /* Unary + */
17230 case REALPART_EXPR:
17231 case IMAGPART_EXPR:
17232 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17233 RECUR (TREE_OPERAND (t, 0)),
17234 complain|decltype_flag));
17235
17236 case FIX_TRUNC_EXPR:
17237 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17238 false, complain));
17239
17240 case ADDR_EXPR:
17241 op1 = TREE_OPERAND (t, 0);
17242 if (TREE_CODE (op1) == LABEL_DECL)
17243 RETURN (finish_label_address_expr (DECL_NAME (op1),
17244 EXPR_LOCATION (op1)));
17245 if (TREE_CODE (op1) == SCOPE_REF)
17246 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17247 /*done=*/true, /*address_p=*/true);
17248 else
17249 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17250 in_decl);
17251 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17252 complain|decltype_flag));
17253
17254 case PLUS_EXPR:
17255 case MINUS_EXPR:
17256 case MULT_EXPR:
17257 case TRUNC_DIV_EXPR:
17258 case CEIL_DIV_EXPR:
17259 case FLOOR_DIV_EXPR:
17260 case ROUND_DIV_EXPR:
17261 case EXACT_DIV_EXPR:
17262 case BIT_AND_EXPR:
17263 case BIT_IOR_EXPR:
17264 case BIT_XOR_EXPR:
17265 case TRUNC_MOD_EXPR:
17266 case FLOOR_MOD_EXPR:
17267 case TRUTH_ANDIF_EXPR:
17268 case TRUTH_ORIF_EXPR:
17269 case TRUTH_AND_EXPR:
17270 case TRUTH_OR_EXPR:
17271 case RSHIFT_EXPR:
17272 case LSHIFT_EXPR:
17273 case RROTATE_EXPR:
17274 case LROTATE_EXPR:
17275 case EQ_EXPR:
17276 case NE_EXPR:
17277 case MAX_EXPR:
17278 case MIN_EXPR:
17279 case LE_EXPR:
17280 case GE_EXPR:
17281 case LT_EXPR:
17282 case GT_EXPR:
17283 case MEMBER_REF:
17284 case DOTSTAR_EXPR:
17285 {
17286 warning_sentinel s1(warn_type_limits);
17287 warning_sentinel s2(warn_div_by_zero);
17288 warning_sentinel s3(warn_logical_op);
17289 warning_sentinel s4(warn_tautological_compare);
17290 tree op0 = RECUR (TREE_OPERAND (t, 0));
17291 tree op1 = RECUR (TREE_OPERAND (t, 1));
17292 tree r = build_x_binary_op
17293 (input_location, TREE_CODE (t),
17294 op0,
17295 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17296 ? ERROR_MARK
17297 : TREE_CODE (TREE_OPERAND (t, 0))),
17298 op1,
17299 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17300 ? ERROR_MARK
17301 : TREE_CODE (TREE_OPERAND (t, 1))),
17302 /*overload=*/NULL,
17303 complain|decltype_flag);
17304 if (EXPR_P (r) && TREE_NO_WARNING (t))
17305 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17306
17307 RETURN (r);
17308 }
17309
17310 case POINTER_PLUS_EXPR:
17311 {
17312 tree op0 = RECUR (TREE_OPERAND (t, 0));
17313 tree op1 = RECUR (TREE_OPERAND (t, 1));
17314 return fold_build_pointer_plus (op0, op1);
17315 }
17316
17317 case SCOPE_REF:
17318 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17319 /*address_p=*/false));
17320 case ARRAY_REF:
17321 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17322 args, complain, in_decl);
17323 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17324 RECUR (TREE_OPERAND (t, 1)),
17325 complain|decltype_flag));
17326
17327 case SIZEOF_EXPR:
17328 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17329 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17330 RETURN (tsubst_copy (t, args, complain, in_decl));
17331 /* Fall through */
17332
17333 case ALIGNOF_EXPR:
17334 {
17335 tree r;
17336
17337 op1 = TREE_OPERAND (t, 0);
17338 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17339 op1 = TREE_TYPE (op1);
17340 if (!args)
17341 {
17342 /* When there are no ARGS, we are trying to evaluate a
17343 non-dependent expression from the parser. Trying to do
17344 the substitutions may not work. */
17345 if (!TYPE_P (op1))
17346 op1 = TREE_TYPE (op1);
17347 }
17348 else
17349 {
17350 ++cp_unevaluated_operand;
17351 ++c_inhibit_evaluation_warnings;
17352 if (TYPE_P (op1))
17353 op1 = tsubst (op1, args, complain, in_decl);
17354 else
17355 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17356 /*function_p=*/false,
17357 /*integral_constant_expression_p=*/
17358 false);
17359 --cp_unevaluated_operand;
17360 --c_inhibit_evaluation_warnings;
17361 }
17362 if (TYPE_P (op1))
17363 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17364 complain & tf_error);
17365 else
17366 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17367 complain & tf_error);
17368 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17369 {
17370 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17371 {
17372 if (!processing_template_decl && TYPE_P (op1))
17373 {
17374 r = build_min (SIZEOF_EXPR, size_type_node,
17375 build1 (NOP_EXPR, op1, error_mark_node));
17376 SIZEOF_EXPR_TYPE_P (r) = 1;
17377 }
17378 else
17379 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17380 TREE_SIDE_EFFECTS (r) = 0;
17381 TREE_READONLY (r) = 1;
17382 }
17383 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17384 }
17385 RETURN (r);
17386 }
17387
17388 case AT_ENCODE_EXPR:
17389 {
17390 op1 = TREE_OPERAND (t, 0);
17391 ++cp_unevaluated_operand;
17392 ++c_inhibit_evaluation_warnings;
17393 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17394 /*function_p=*/false,
17395 /*integral_constant_expression_p=*/false);
17396 --cp_unevaluated_operand;
17397 --c_inhibit_evaluation_warnings;
17398 RETURN (objc_build_encode_expr (op1));
17399 }
17400
17401 case NOEXCEPT_EXPR:
17402 op1 = TREE_OPERAND (t, 0);
17403 ++cp_unevaluated_operand;
17404 ++c_inhibit_evaluation_warnings;
17405 ++cp_noexcept_operand;
17406 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17407 /*function_p=*/false,
17408 /*integral_constant_expression_p=*/false);
17409 --cp_unevaluated_operand;
17410 --c_inhibit_evaluation_warnings;
17411 --cp_noexcept_operand;
17412 RETURN (finish_noexcept_expr (op1, complain));
17413
17414 case MODOP_EXPR:
17415 {
17416 warning_sentinel s(warn_div_by_zero);
17417 tree lhs = RECUR (TREE_OPERAND (t, 0));
17418 tree rhs = RECUR (TREE_OPERAND (t, 2));
17419 tree r = build_x_modify_expr
17420 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17421 complain|decltype_flag);
17422 /* TREE_NO_WARNING must be set if either the expression was
17423 parenthesized or it uses an operator such as >>= rather
17424 than plain assignment. In the former case, it was already
17425 set and must be copied. In the latter case,
17426 build_x_modify_expr sets it and it must not be reset
17427 here. */
17428 if (TREE_NO_WARNING (t))
17429 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17430
17431 RETURN (r);
17432 }
17433
17434 case ARROW_EXPR:
17435 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17436 args, complain, in_decl);
17437 /* Remember that there was a reference to this entity. */
17438 if (DECL_P (op1)
17439 && !mark_used (op1, complain) && !(complain & tf_error))
17440 RETURN (error_mark_node);
17441 RETURN (build_x_arrow (input_location, op1, complain));
17442
17443 case NEW_EXPR:
17444 {
17445 tree placement = RECUR (TREE_OPERAND (t, 0));
17446 tree init = RECUR (TREE_OPERAND (t, 3));
17447 vec<tree, va_gc> *placement_vec;
17448 vec<tree, va_gc> *init_vec;
17449 tree ret;
17450
17451 if (placement == NULL_TREE)
17452 placement_vec = NULL;
17453 else
17454 {
17455 placement_vec = make_tree_vector ();
17456 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17457 vec_safe_push (placement_vec, TREE_VALUE (placement));
17458 }
17459
17460 /* If there was an initializer in the original tree, but it
17461 instantiated to an empty list, then we should pass a
17462 non-NULL empty vector to tell build_new that it was an
17463 empty initializer() rather than no initializer. This can
17464 only happen when the initializer is a pack expansion whose
17465 parameter packs are of length zero. */
17466 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17467 init_vec = NULL;
17468 else
17469 {
17470 init_vec = make_tree_vector ();
17471 if (init == void_node)
17472 gcc_assert (init_vec != NULL);
17473 else
17474 {
17475 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17476 vec_safe_push (init_vec, TREE_VALUE (init));
17477 }
17478 }
17479
17480 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17481 tree op2 = RECUR (TREE_OPERAND (t, 2));
17482 ret = build_new (&placement_vec, op1, op2, &init_vec,
17483 NEW_EXPR_USE_GLOBAL (t),
17484 complain);
17485
17486 if (placement_vec != NULL)
17487 release_tree_vector (placement_vec);
17488 if (init_vec != NULL)
17489 release_tree_vector (init_vec);
17490
17491 RETURN (ret);
17492 }
17493
17494 case DELETE_EXPR:
17495 {
17496 tree op0 = RECUR (TREE_OPERAND (t, 0));
17497 tree op1 = RECUR (TREE_OPERAND (t, 1));
17498 RETURN (delete_sanity (op0, op1,
17499 DELETE_EXPR_USE_VEC (t),
17500 DELETE_EXPR_USE_GLOBAL (t),
17501 complain));
17502 }
17503
17504 case COMPOUND_EXPR:
17505 {
17506 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17507 complain & ~tf_decltype, in_decl,
17508 /*function_p=*/false,
17509 integral_constant_expression_p);
17510 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17511 op0,
17512 RECUR (TREE_OPERAND (t, 1)),
17513 complain|decltype_flag));
17514 }
17515
17516 case CALL_EXPR:
17517 {
17518 tree function;
17519 vec<tree, va_gc> *call_args;
17520 unsigned int nargs, i;
17521 bool qualified_p;
17522 bool koenig_p;
17523 tree ret;
17524
17525 function = CALL_EXPR_FN (t);
17526 /* Internal function with no arguments. */
17527 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17528 RETURN (t);
17529
17530 /* When we parsed the expression, we determined whether or
17531 not Koenig lookup should be performed. */
17532 koenig_p = KOENIG_LOOKUP_P (t);
17533 if (function == NULL_TREE)
17534 {
17535 koenig_p = false;
17536 qualified_p = false;
17537 }
17538 else if (TREE_CODE (function) == SCOPE_REF)
17539 {
17540 qualified_p = true;
17541 function = tsubst_qualified_id (function, args, complain, in_decl,
17542 /*done=*/false,
17543 /*address_p=*/false);
17544 }
17545 else if (koenig_p && identifier_p (function))
17546 {
17547 /* Do nothing; calling tsubst_copy_and_build on an identifier
17548 would incorrectly perform unqualified lookup again.
17549
17550 Note that we can also have an IDENTIFIER_NODE if the earlier
17551 unqualified lookup found a member function; in that case
17552 koenig_p will be false and we do want to do the lookup
17553 again to find the instantiated member function.
17554
17555 FIXME but doing that causes c++/15272, so we need to stop
17556 using IDENTIFIER_NODE in that situation. */
17557 qualified_p = false;
17558 }
17559 else
17560 {
17561 if (TREE_CODE (function) == COMPONENT_REF)
17562 {
17563 tree op = TREE_OPERAND (function, 1);
17564
17565 qualified_p = (TREE_CODE (op) == SCOPE_REF
17566 || (BASELINK_P (op)
17567 && BASELINK_QUALIFIED_P (op)));
17568 }
17569 else
17570 qualified_p = false;
17571
17572 if (TREE_CODE (function) == ADDR_EXPR
17573 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17574 /* Avoid error about taking the address of a constructor. */
17575 function = TREE_OPERAND (function, 0);
17576
17577 function = tsubst_copy_and_build (function, args, complain,
17578 in_decl,
17579 !qualified_p,
17580 integral_constant_expression_p);
17581
17582 if (BASELINK_P (function))
17583 qualified_p = true;
17584 }
17585
17586 nargs = call_expr_nargs (t);
17587 call_args = make_tree_vector ();
17588 for (i = 0; i < nargs; ++i)
17589 {
17590 tree arg = CALL_EXPR_ARG (t, i);
17591
17592 if (!PACK_EXPANSION_P (arg))
17593 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17594 else
17595 {
17596 /* Expand the pack expansion and push each entry onto
17597 CALL_ARGS. */
17598 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17599 if (TREE_CODE (arg) == TREE_VEC)
17600 {
17601 unsigned int len, j;
17602
17603 len = TREE_VEC_LENGTH (arg);
17604 for (j = 0; j < len; ++j)
17605 {
17606 tree value = TREE_VEC_ELT (arg, j);
17607 if (value != NULL_TREE)
17608 value = convert_from_reference (value);
17609 vec_safe_push (call_args, value);
17610 }
17611 }
17612 else
17613 {
17614 /* A partial substitution. Add one entry. */
17615 vec_safe_push (call_args, arg);
17616 }
17617 }
17618 }
17619
17620 /* We do not perform argument-dependent lookup if normal
17621 lookup finds a non-function, in accordance with the
17622 expected resolution of DR 218. */
17623 if (koenig_p
17624 && ((is_overloaded_fn (function)
17625 /* If lookup found a member function, the Koenig lookup is
17626 not appropriate, even if an unqualified-name was used
17627 to denote the function. */
17628 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17629 || identifier_p (function))
17630 /* Only do this when substitution turns a dependent call
17631 into a non-dependent call. */
17632 && type_dependent_expression_p_push (t)
17633 && !any_type_dependent_arguments_p (call_args))
17634 function = perform_koenig_lookup (function, call_args, tf_none);
17635
17636 if (function != NULL_TREE
17637 && identifier_p (function)
17638 && !any_type_dependent_arguments_p (call_args))
17639 {
17640 if (koenig_p && (complain & tf_warning_or_error))
17641 {
17642 /* For backwards compatibility and good diagnostics, try
17643 the unqualified lookup again if we aren't in SFINAE
17644 context. */
17645 tree unq = (tsubst_copy_and_build
17646 (function, args, complain, in_decl, true,
17647 integral_constant_expression_p));
17648 if (unq == error_mark_node)
17649 {
17650 release_tree_vector (call_args);
17651 RETURN (error_mark_node);
17652 }
17653
17654 if (unq != function)
17655 {
17656 /* In a lambda fn, we have to be careful to not
17657 introduce new this captures. Legacy code can't
17658 be using lambdas anyway, so it's ok to be
17659 stricter. */
17660 bool in_lambda = (current_class_type
17661 && LAMBDA_TYPE_P (current_class_type));
17662 char const *const msg
17663 = G_("%qD was not declared in this scope, "
17664 "and no declarations were found by "
17665 "argument-dependent lookup at the point "
17666 "of instantiation");
17667
17668 bool diag = true;
17669 if (in_lambda)
17670 error_at (EXPR_LOC_OR_LOC (t, input_location),
17671 msg, function);
17672 else
17673 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17674 msg, function);
17675 if (diag)
17676 {
17677 tree fn = unq;
17678
17679 if (INDIRECT_REF_P (fn))
17680 fn = TREE_OPERAND (fn, 0);
17681 if (is_overloaded_fn (fn))
17682 fn = get_first_fn (fn);
17683
17684 if (!DECL_P (fn))
17685 /* Can't say anything more. */;
17686 else if (DECL_CLASS_SCOPE_P (fn))
17687 {
17688 location_t loc = EXPR_LOC_OR_LOC (t,
17689 input_location);
17690 inform (loc,
17691 "declarations in dependent base %qT are "
17692 "not found by unqualified lookup",
17693 DECL_CLASS_CONTEXT (fn));
17694 if (current_class_ptr)
17695 inform (loc,
17696 "use %<this->%D%> instead", function);
17697 else
17698 inform (loc,
17699 "use %<%T::%D%> instead",
17700 current_class_name, function);
17701 }
17702 else
17703 inform (DECL_SOURCE_LOCATION (fn),
17704 "%qD declared here, later in the "
17705 "translation unit", fn);
17706 if (in_lambda)
17707 {
17708 release_tree_vector (call_args);
17709 RETURN (error_mark_node);
17710 }
17711 }
17712
17713 function = unq;
17714 }
17715 }
17716 if (identifier_p (function))
17717 {
17718 if (complain & tf_error)
17719 unqualified_name_lookup_error (function);
17720 release_tree_vector (call_args);
17721 RETURN (error_mark_node);
17722 }
17723 }
17724
17725 /* Remember that there was a reference to this entity. */
17726 if (function != NULL_TREE
17727 && DECL_P (function)
17728 && !mark_used (function, complain) && !(complain & tf_error))
17729 {
17730 release_tree_vector (call_args);
17731 RETURN (error_mark_node);
17732 }
17733
17734 /* Put back tf_decltype for the actual call. */
17735 complain |= decltype_flag;
17736
17737 if (function == NULL_TREE)
17738 switch (CALL_EXPR_IFN (t))
17739 {
17740 case IFN_LAUNDER:
17741 gcc_assert (nargs == 1);
17742 if (vec_safe_length (call_args) != 1)
17743 {
17744 error_at (EXPR_LOC_OR_LOC (t, input_location),
17745 "wrong number of arguments to "
17746 "%<__builtin_launder%>");
17747 ret = error_mark_node;
17748 }
17749 else
17750 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17751 input_location),
17752 (*call_args)[0], complain);
17753 break;
17754
17755 default:
17756 /* Unsupported internal function with arguments. */
17757 gcc_unreachable ();
17758 }
17759 else if (TREE_CODE (function) == OFFSET_REF)
17760 ret = build_offset_ref_call_from_tree (function, &call_args,
17761 complain);
17762 else if (TREE_CODE (function) == COMPONENT_REF)
17763 {
17764 tree instance = TREE_OPERAND (function, 0);
17765 tree fn = TREE_OPERAND (function, 1);
17766
17767 if (processing_template_decl
17768 && (type_dependent_expression_p (instance)
17769 || (!BASELINK_P (fn)
17770 && TREE_CODE (fn) != FIELD_DECL)
17771 || type_dependent_expression_p (fn)
17772 || any_type_dependent_arguments_p (call_args)))
17773 ret = build_min_nt_call_vec (function, call_args);
17774 else if (!BASELINK_P (fn))
17775 ret = finish_call_expr (function, &call_args,
17776 /*disallow_virtual=*/false,
17777 /*koenig_p=*/false,
17778 complain);
17779 else
17780 ret = (build_new_method_call
17781 (instance, fn,
17782 &call_args, NULL_TREE,
17783 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17784 /*fn_p=*/NULL,
17785 complain));
17786 }
17787 else
17788 ret = finish_call_expr (function, &call_args,
17789 /*disallow_virtual=*/qualified_p,
17790 koenig_p,
17791 complain);
17792
17793 release_tree_vector (call_args);
17794
17795 if (ret != error_mark_node)
17796 {
17797 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17798 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17799 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17800 bool thk = CALL_FROM_THUNK_P (t);
17801 if (op || ord || rev || thk)
17802 {
17803 function = extract_call_expr (ret);
17804 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17805 CALL_EXPR_ORDERED_ARGS (function) = ord;
17806 CALL_EXPR_REVERSE_ARGS (function) = rev;
17807 if (thk)
17808 {
17809 CALL_FROM_THUNK_P (function) = true;
17810 /* The thunk location is not interesting. */
17811 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17812 }
17813 }
17814 }
17815
17816 RETURN (ret);
17817 }
17818
17819 case COND_EXPR:
17820 {
17821 tree cond = RECUR (TREE_OPERAND (t, 0));
17822 tree folded_cond = fold_non_dependent_expr (cond);
17823 tree exp1, exp2;
17824
17825 if (TREE_CODE (folded_cond) == INTEGER_CST)
17826 {
17827 if (integer_zerop (folded_cond))
17828 {
17829 ++c_inhibit_evaluation_warnings;
17830 exp1 = RECUR (TREE_OPERAND (t, 1));
17831 --c_inhibit_evaluation_warnings;
17832 exp2 = RECUR (TREE_OPERAND (t, 2));
17833 }
17834 else
17835 {
17836 exp1 = RECUR (TREE_OPERAND (t, 1));
17837 ++c_inhibit_evaluation_warnings;
17838 exp2 = RECUR (TREE_OPERAND (t, 2));
17839 --c_inhibit_evaluation_warnings;
17840 }
17841 cond = folded_cond;
17842 }
17843 else
17844 {
17845 exp1 = RECUR (TREE_OPERAND (t, 1));
17846 exp2 = RECUR (TREE_OPERAND (t, 2));
17847 }
17848
17849 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17850 cond, exp1, exp2, complain));
17851 }
17852
17853 case PSEUDO_DTOR_EXPR:
17854 {
17855 tree op0 = RECUR (TREE_OPERAND (t, 0));
17856 tree op1 = RECUR (TREE_OPERAND (t, 1));
17857 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17858 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17859 input_location));
17860 }
17861
17862 case TREE_LIST:
17863 {
17864 tree purpose, value, chain;
17865
17866 if (t == void_list_node)
17867 RETURN (t);
17868
17869 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17870 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17871 {
17872 /* We have pack expansions, so expand those and
17873 create a new list out of it. */
17874 tree purposevec = NULL_TREE;
17875 tree valuevec = NULL_TREE;
17876 tree chain;
17877 int i, len = -1;
17878
17879 /* Expand the argument expressions. */
17880 if (TREE_PURPOSE (t))
17881 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17882 complain, in_decl);
17883 if (TREE_VALUE (t))
17884 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17885 complain, in_decl);
17886
17887 /* Build the rest of the list. */
17888 chain = TREE_CHAIN (t);
17889 if (chain && chain != void_type_node)
17890 chain = RECUR (chain);
17891
17892 /* Determine the number of arguments. */
17893 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17894 {
17895 len = TREE_VEC_LENGTH (purposevec);
17896 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17897 }
17898 else if (TREE_CODE (valuevec) == TREE_VEC)
17899 len = TREE_VEC_LENGTH (valuevec);
17900 else
17901 {
17902 /* Since we only performed a partial substitution into
17903 the argument pack, we only RETURN (a single list
17904 node. */
17905 if (purposevec == TREE_PURPOSE (t)
17906 && valuevec == TREE_VALUE (t)
17907 && chain == TREE_CHAIN (t))
17908 RETURN (t);
17909
17910 RETURN (tree_cons (purposevec, valuevec, chain));
17911 }
17912
17913 /* Convert the argument vectors into a TREE_LIST */
17914 i = len;
17915 while (i > 0)
17916 {
17917 /* Grab the Ith values. */
17918 i--;
17919 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17920 : NULL_TREE;
17921 value
17922 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17923 : NULL_TREE;
17924
17925 /* Build the list (backwards). */
17926 chain = tree_cons (purpose, value, chain);
17927 }
17928
17929 RETURN (chain);
17930 }
17931
17932 purpose = TREE_PURPOSE (t);
17933 if (purpose)
17934 purpose = RECUR (purpose);
17935 value = TREE_VALUE (t);
17936 if (value)
17937 value = RECUR (value);
17938 chain = TREE_CHAIN (t);
17939 if (chain && chain != void_type_node)
17940 chain = RECUR (chain);
17941 if (purpose == TREE_PURPOSE (t)
17942 && value == TREE_VALUE (t)
17943 && chain == TREE_CHAIN (t))
17944 RETURN (t);
17945 RETURN (tree_cons (purpose, value, chain));
17946 }
17947
17948 case COMPONENT_REF:
17949 {
17950 tree object;
17951 tree object_type;
17952 tree member;
17953 tree r;
17954
17955 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17956 args, complain, in_decl);
17957 /* Remember that there was a reference to this entity. */
17958 if (DECL_P (object)
17959 && !mark_used (object, complain) && !(complain & tf_error))
17960 RETURN (error_mark_node);
17961 object_type = TREE_TYPE (object);
17962
17963 member = TREE_OPERAND (t, 1);
17964 if (BASELINK_P (member))
17965 member = tsubst_baselink (member,
17966 non_reference (TREE_TYPE (object)),
17967 args, complain, in_decl);
17968 else
17969 member = tsubst_copy (member, args, complain, in_decl);
17970 if (member == error_mark_node)
17971 RETURN (error_mark_node);
17972
17973 if (TREE_CODE (member) == FIELD_DECL)
17974 {
17975 r = finish_non_static_data_member (member, object, NULL_TREE);
17976 if (TREE_CODE (r) == COMPONENT_REF)
17977 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17978 RETURN (r);
17979 }
17980 else if (type_dependent_expression_p (object))
17981 /* We can't do much here. */;
17982 else if (!CLASS_TYPE_P (object_type))
17983 {
17984 if (scalarish_type_p (object_type))
17985 {
17986 tree s = NULL_TREE;
17987 tree dtor = member;
17988
17989 if (TREE_CODE (dtor) == SCOPE_REF)
17990 {
17991 s = TREE_OPERAND (dtor, 0);
17992 dtor = TREE_OPERAND (dtor, 1);
17993 }
17994 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17995 {
17996 dtor = TREE_OPERAND (dtor, 0);
17997 if (TYPE_P (dtor))
17998 RETURN (finish_pseudo_destructor_expr
17999 (object, s, dtor, input_location));
18000 }
18001 }
18002 }
18003 else if (TREE_CODE (member) == SCOPE_REF
18004 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18005 {
18006 /* Lookup the template functions now that we know what the
18007 scope is. */
18008 tree scope = TREE_OPERAND (member, 0);
18009 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18010 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18011 member = lookup_qualified_name (scope, tmpl,
18012 /*is_type_p=*/false,
18013 /*complain=*/false);
18014 if (BASELINK_P (member))
18015 {
18016 BASELINK_FUNCTIONS (member)
18017 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18018 args);
18019 member = (adjust_result_of_qualified_name_lookup
18020 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18021 object_type));
18022 }
18023 else
18024 {
18025 qualified_name_lookup_error (scope, tmpl, member,
18026 input_location);
18027 RETURN (error_mark_node);
18028 }
18029 }
18030 else if (TREE_CODE (member) == SCOPE_REF
18031 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18032 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18033 {
18034 if (complain & tf_error)
18035 {
18036 if (TYPE_P (TREE_OPERAND (member, 0)))
18037 error ("%qT is not a class or namespace",
18038 TREE_OPERAND (member, 0));
18039 else
18040 error ("%qD is not a class or namespace",
18041 TREE_OPERAND (member, 0));
18042 }
18043 RETURN (error_mark_node);
18044 }
18045
18046 r = finish_class_member_access_expr (object, member,
18047 /*template_p=*/false,
18048 complain);
18049 if (TREE_CODE (r) == COMPONENT_REF)
18050 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18051 RETURN (r);
18052 }
18053
18054 case THROW_EXPR:
18055 RETURN (build_throw
18056 (RECUR (TREE_OPERAND (t, 0))));
18057
18058 case CONSTRUCTOR:
18059 {
18060 vec<constructor_elt, va_gc> *n;
18061 constructor_elt *ce;
18062 unsigned HOST_WIDE_INT idx;
18063 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18064 bool process_index_p;
18065 int newlen;
18066 bool need_copy_p = false;
18067 tree r;
18068
18069 if (type == error_mark_node)
18070 RETURN (error_mark_node);
18071
18072 /* digest_init will do the wrong thing if we let it. */
18073 if (type && TYPE_PTRMEMFUNC_P (type))
18074 RETURN (t);
18075
18076 /* We do not want to process the index of aggregate
18077 initializers as they are identifier nodes which will be
18078 looked up by digest_init. */
18079 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18080
18081 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18082 newlen = vec_safe_length (n);
18083 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18084 {
18085 if (ce->index && process_index_p
18086 /* An identifier index is looked up in the type
18087 being initialized, not the current scope. */
18088 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18089 ce->index = RECUR (ce->index);
18090
18091 if (PACK_EXPANSION_P (ce->value))
18092 {
18093 /* Substitute into the pack expansion. */
18094 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18095 in_decl);
18096
18097 if (ce->value == error_mark_node
18098 || PACK_EXPANSION_P (ce->value))
18099 ;
18100 else if (TREE_VEC_LENGTH (ce->value) == 1)
18101 /* Just move the argument into place. */
18102 ce->value = TREE_VEC_ELT (ce->value, 0);
18103 else
18104 {
18105 /* Update the length of the final CONSTRUCTOR
18106 arguments vector, and note that we will need to
18107 copy.*/
18108 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18109 need_copy_p = true;
18110 }
18111 }
18112 else
18113 ce->value = RECUR (ce->value);
18114 }
18115
18116 if (need_copy_p)
18117 {
18118 vec<constructor_elt, va_gc> *old_n = n;
18119
18120 vec_alloc (n, newlen);
18121 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18122 {
18123 if (TREE_CODE (ce->value) == TREE_VEC)
18124 {
18125 int i, len = TREE_VEC_LENGTH (ce->value);
18126 for (i = 0; i < len; ++i)
18127 CONSTRUCTOR_APPEND_ELT (n, 0,
18128 TREE_VEC_ELT (ce->value, i));
18129 }
18130 else
18131 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18132 }
18133 }
18134
18135 r = build_constructor (init_list_type_node, n);
18136 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18137
18138 if (TREE_HAS_CONSTRUCTOR (t))
18139 {
18140 fcl_t cl = fcl_functional;
18141 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18142 cl = fcl_c99;
18143 RETURN (finish_compound_literal (type, r, complain, cl));
18144 }
18145
18146 TREE_TYPE (r) = type;
18147 RETURN (r);
18148 }
18149
18150 case TYPEID_EXPR:
18151 {
18152 tree operand_0 = TREE_OPERAND (t, 0);
18153 if (TYPE_P (operand_0))
18154 {
18155 operand_0 = tsubst (operand_0, args, complain, in_decl);
18156 RETURN (get_typeid (operand_0, complain));
18157 }
18158 else
18159 {
18160 operand_0 = RECUR (operand_0);
18161 RETURN (build_typeid (operand_0, complain));
18162 }
18163 }
18164
18165 case VAR_DECL:
18166 if (!args)
18167 RETURN (t);
18168 else if (DECL_PACK_P (t))
18169 {
18170 /* We don't build decls for an instantiation of a
18171 variadic capture proxy, we instantiate the elements
18172 when needed. */
18173 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
18174 return RECUR (DECL_VALUE_EXPR (t));
18175 }
18176 /* Fall through */
18177
18178 case PARM_DECL:
18179 {
18180 tree r = tsubst_copy (t, args, complain, in_decl);
18181 /* ??? We're doing a subset of finish_id_expression here. */
18182 if (VAR_P (r)
18183 && !processing_template_decl
18184 && !cp_unevaluated_operand
18185 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18186 && CP_DECL_THREAD_LOCAL_P (r))
18187 {
18188 if (tree wrap = get_tls_wrapper_fn (r))
18189 /* Replace an evaluated use of the thread_local variable with
18190 a call to its wrapper. */
18191 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18192 }
18193 else if (outer_automatic_var_p (r))
18194 r = process_outer_var_ref (r, complain);
18195
18196 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18197 /* If the original type was a reference, we'll be wrapped in
18198 the appropriate INDIRECT_REF. */
18199 r = convert_from_reference (r);
18200 RETURN (r);
18201 }
18202
18203 case VA_ARG_EXPR:
18204 {
18205 tree op0 = RECUR (TREE_OPERAND (t, 0));
18206 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18207 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18208 }
18209
18210 case OFFSETOF_EXPR:
18211 {
18212 tree object_ptr
18213 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18214 in_decl, /*function_p=*/false,
18215 /*integral_constant_expression_p=*/false);
18216 RETURN (finish_offsetof (object_ptr,
18217 RECUR (TREE_OPERAND (t, 0)),
18218 EXPR_LOCATION (t)));
18219 }
18220
18221 case ADDRESSOF_EXPR:
18222 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18223 RECUR (TREE_OPERAND (t, 0)), complain));
18224
18225 case TRAIT_EXPR:
18226 {
18227 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18228 complain, in_decl);
18229
18230 tree type2 = TRAIT_EXPR_TYPE2 (t);
18231 if (type2 && TREE_CODE (type2) == TREE_LIST)
18232 type2 = RECUR (type2);
18233 else if (type2)
18234 type2 = tsubst (type2, args, complain, in_decl);
18235
18236 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18237 }
18238
18239 case STMT_EXPR:
18240 {
18241 tree old_stmt_expr = cur_stmt_expr;
18242 tree stmt_expr = begin_stmt_expr ();
18243
18244 cur_stmt_expr = stmt_expr;
18245 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18246 integral_constant_expression_p);
18247 stmt_expr = finish_stmt_expr (stmt_expr, false);
18248 cur_stmt_expr = old_stmt_expr;
18249
18250 /* If the resulting list of expression statement is empty,
18251 fold it further into void_node. */
18252 if (empty_expr_stmt_p (stmt_expr))
18253 stmt_expr = void_node;
18254
18255 RETURN (stmt_expr);
18256 }
18257
18258 case LAMBDA_EXPR:
18259 {
18260 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18261
18262 RETURN (build_lambda_object (r));
18263 }
18264
18265 case TARGET_EXPR:
18266 /* We can get here for a constant initializer of non-dependent type.
18267 FIXME stop folding in cp_parser_initializer_clause. */
18268 {
18269 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18270 complain);
18271 RETURN (r);
18272 }
18273
18274 case TRANSACTION_EXPR:
18275 RETURN (tsubst_expr(t, args, complain, in_decl,
18276 integral_constant_expression_p));
18277
18278 case PAREN_EXPR:
18279 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18280
18281 case VEC_PERM_EXPR:
18282 {
18283 tree op0 = RECUR (TREE_OPERAND (t, 0));
18284 tree op1 = RECUR (TREE_OPERAND (t, 1));
18285 tree op2 = RECUR (TREE_OPERAND (t, 2));
18286 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18287 complain));
18288 }
18289
18290 case REQUIRES_EXPR:
18291 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18292
18293 default:
18294 /* Handle Objective-C++ constructs, if appropriate. */
18295 {
18296 tree subst
18297 = objcp_tsubst_copy_and_build (t, args, complain,
18298 in_decl, /*function_p=*/false);
18299 if (subst)
18300 RETURN (subst);
18301 }
18302 RETURN (tsubst_copy (t, args, complain, in_decl));
18303 }
18304
18305 #undef RECUR
18306 #undef RETURN
18307 out:
18308 input_location = loc;
18309 return retval;
18310 }
18311
18312 /* Verify that the instantiated ARGS are valid. For type arguments,
18313 make sure that the type's linkage is ok. For non-type arguments,
18314 make sure they are constants if they are integral or enumerations.
18315 Emit an error under control of COMPLAIN, and return TRUE on error. */
18316
18317 static bool
18318 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18319 {
18320 if (dependent_template_arg_p (t))
18321 return false;
18322 if (ARGUMENT_PACK_P (t))
18323 {
18324 tree vec = ARGUMENT_PACK_ARGS (t);
18325 int len = TREE_VEC_LENGTH (vec);
18326 bool result = false;
18327 int i;
18328
18329 for (i = 0; i < len; ++i)
18330 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18331 result = true;
18332 return result;
18333 }
18334 else if (TYPE_P (t))
18335 {
18336 /* [basic.link]: A name with no linkage (notably, the name
18337 of a class or enumeration declared in a local scope)
18338 shall not be used to declare an entity with linkage.
18339 This implies that names with no linkage cannot be used as
18340 template arguments
18341
18342 DR 757 relaxes this restriction for C++0x. */
18343 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18344 : no_linkage_check (t, /*relaxed_p=*/false));
18345
18346 if (nt)
18347 {
18348 /* DR 488 makes use of a type with no linkage cause
18349 type deduction to fail. */
18350 if (complain & tf_error)
18351 {
18352 if (TYPE_UNNAMED_P (nt))
18353 error ("%qT is/uses unnamed type", t);
18354 else
18355 error ("template argument for %qD uses local type %qT",
18356 tmpl, t);
18357 }
18358 return true;
18359 }
18360 /* In order to avoid all sorts of complications, we do not
18361 allow variably-modified types as template arguments. */
18362 else if (variably_modified_type_p (t, NULL_TREE))
18363 {
18364 if (complain & tf_error)
18365 error ("%qT is a variably modified type", t);
18366 return true;
18367 }
18368 }
18369 /* Class template and alias template arguments should be OK. */
18370 else if (DECL_TYPE_TEMPLATE_P (t))
18371 ;
18372 /* A non-type argument of integral or enumerated type must be a
18373 constant. */
18374 else if (TREE_TYPE (t)
18375 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18376 && !REFERENCE_REF_P (t)
18377 && !TREE_CONSTANT (t))
18378 {
18379 if (complain & tf_error)
18380 error ("integral expression %qE is not constant", t);
18381 return true;
18382 }
18383 return false;
18384 }
18385
18386 static bool
18387 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18388 {
18389 int ix, len = DECL_NTPARMS (tmpl);
18390 bool result = false;
18391
18392 for (ix = 0; ix != len; ix++)
18393 {
18394 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18395 result = true;
18396 }
18397 if (result && (complain & tf_error))
18398 error (" trying to instantiate %qD", tmpl);
18399 return result;
18400 }
18401
18402 /* We're out of SFINAE context now, so generate diagnostics for the access
18403 errors we saw earlier when instantiating D from TMPL and ARGS. */
18404
18405 static void
18406 recheck_decl_substitution (tree d, tree tmpl, tree args)
18407 {
18408 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18409 tree type = TREE_TYPE (pattern);
18410 location_t loc = input_location;
18411
18412 push_access_scope (d);
18413 push_deferring_access_checks (dk_no_deferred);
18414 input_location = DECL_SOURCE_LOCATION (pattern);
18415 tsubst (type, args, tf_warning_or_error, d);
18416 input_location = loc;
18417 pop_deferring_access_checks ();
18418 pop_access_scope (d);
18419 }
18420
18421 /* Instantiate the indicated variable, function, or alias template TMPL with
18422 the template arguments in TARG_PTR. */
18423
18424 static tree
18425 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18426 {
18427 tree targ_ptr = orig_args;
18428 tree fndecl;
18429 tree gen_tmpl;
18430 tree spec;
18431 bool access_ok = true;
18432
18433 if (tmpl == error_mark_node)
18434 return error_mark_node;
18435
18436 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18437
18438 /* If this function is a clone, handle it specially. */
18439 if (DECL_CLONED_FUNCTION_P (tmpl))
18440 {
18441 tree spec;
18442 tree clone;
18443
18444 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18445 DECL_CLONED_FUNCTION. */
18446 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18447 targ_ptr, complain);
18448 if (spec == error_mark_node)
18449 return error_mark_node;
18450
18451 /* Look for the clone. */
18452 FOR_EACH_CLONE (clone, spec)
18453 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18454 return clone;
18455 /* We should always have found the clone by now. */
18456 gcc_unreachable ();
18457 return NULL_TREE;
18458 }
18459
18460 if (targ_ptr == error_mark_node)
18461 return error_mark_node;
18462
18463 /* Check to see if we already have this specialization. */
18464 gen_tmpl = most_general_template (tmpl);
18465 if (TMPL_ARGS_DEPTH (targ_ptr)
18466 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18467 /* targ_ptr only has the innermost template args, so add the outer ones
18468 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18469 the case of a non-dependent call within a template definition). */
18470 targ_ptr = (add_outermost_template_args
18471 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18472 targ_ptr));
18473
18474 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18475 but it doesn't seem to be on the hot path. */
18476 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18477
18478 gcc_assert (tmpl == gen_tmpl
18479 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18480 == spec)
18481 || fndecl == NULL_TREE);
18482
18483 if (spec != NULL_TREE)
18484 {
18485 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18486 {
18487 if (complain & tf_error)
18488 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18489 return error_mark_node;
18490 }
18491 return spec;
18492 }
18493
18494 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18495 complain))
18496 return error_mark_node;
18497
18498 /* We are building a FUNCTION_DECL, during which the access of its
18499 parameters and return types have to be checked. However this
18500 FUNCTION_DECL which is the desired context for access checking
18501 is not built yet. We solve this chicken-and-egg problem by
18502 deferring all checks until we have the FUNCTION_DECL. */
18503 push_deferring_access_checks (dk_deferred);
18504
18505 /* Instantiation of the function happens in the context of the function
18506 template, not the context of the overload resolution we're doing. */
18507 push_to_top_level ();
18508 /* If there are dependent arguments, e.g. because we're doing partial
18509 ordering, make sure processing_template_decl stays set. */
18510 if (uses_template_parms (targ_ptr))
18511 ++processing_template_decl;
18512 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18513 {
18514 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18515 complain, gen_tmpl, true);
18516 push_nested_class (ctx);
18517 }
18518
18519 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18520
18521 fndecl = NULL_TREE;
18522 if (VAR_P (pattern))
18523 {
18524 /* We need to determine if we're using a partial or explicit
18525 specialization now, because the type of the variable could be
18526 different. */
18527 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18528 tree elt = most_specialized_partial_spec (tid, complain);
18529 if (elt == error_mark_node)
18530 pattern = error_mark_node;
18531 else if (elt)
18532 {
18533 tree partial_tmpl = TREE_VALUE (elt);
18534 tree partial_args = TREE_PURPOSE (elt);
18535 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18536 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18537 }
18538 }
18539
18540 /* Substitute template parameters to obtain the specialization. */
18541 if (fndecl == NULL_TREE)
18542 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18543 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18544 pop_nested_class ();
18545 pop_from_top_level ();
18546
18547 if (fndecl == error_mark_node)
18548 {
18549 pop_deferring_access_checks ();
18550 return error_mark_node;
18551 }
18552
18553 /* The DECL_TI_TEMPLATE should always be the immediate parent
18554 template, not the most general template. */
18555 DECL_TI_TEMPLATE (fndecl) = tmpl;
18556 DECL_TI_ARGS (fndecl) = targ_ptr;
18557
18558 /* Now we know the specialization, compute access previously
18559 deferred. Do no access control for inheriting constructors,
18560 as we already checked access for the inherited constructor. */
18561 if (!(flag_new_inheriting_ctors
18562 && DECL_INHERITED_CTOR (fndecl)))
18563 {
18564 push_access_scope (fndecl);
18565 if (!perform_deferred_access_checks (complain))
18566 access_ok = false;
18567 pop_access_scope (fndecl);
18568 }
18569 pop_deferring_access_checks ();
18570
18571 /* If we've just instantiated the main entry point for a function,
18572 instantiate all the alternate entry points as well. We do this
18573 by cloning the instantiation of the main entry point, not by
18574 instantiating the template clones. */
18575 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18576 clone_function_decl (fndecl, /*update_methods=*/false);
18577
18578 if (!access_ok)
18579 {
18580 if (!(complain & tf_error))
18581 {
18582 /* Remember to reinstantiate when we're out of SFINAE so the user
18583 can see the errors. */
18584 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18585 }
18586 return error_mark_node;
18587 }
18588 return fndecl;
18589 }
18590
18591 /* Wrapper for instantiate_template_1. */
18592
18593 tree
18594 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18595 {
18596 tree ret;
18597 timevar_push (TV_TEMPLATE_INST);
18598 ret = instantiate_template_1 (tmpl, orig_args, complain);
18599 timevar_pop (TV_TEMPLATE_INST);
18600 return ret;
18601 }
18602
18603 /* Instantiate the alias template TMPL with ARGS. Also push a template
18604 instantiation level, which instantiate_template doesn't do because
18605 functions and variables have sufficient context established by the
18606 callers. */
18607
18608 static tree
18609 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18610 {
18611 struct pending_template *old_last_pend = last_pending_template;
18612 struct tinst_level *old_error_tinst = last_error_tinst_level;
18613 if (tmpl == error_mark_node || args == error_mark_node)
18614 return error_mark_node;
18615 tree tinst = build_tree_list (tmpl, args);
18616 if (!push_tinst_level (tinst))
18617 {
18618 ggc_free (tinst);
18619 return error_mark_node;
18620 }
18621
18622 args =
18623 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18624 args, tmpl, complain,
18625 /*require_all_args=*/true,
18626 /*use_default_args=*/true);
18627
18628 tree r = instantiate_template (tmpl, args, complain);
18629 pop_tinst_level ();
18630 /* We can't free this if a pending_template entry or last_error_tinst_level
18631 is pointing at it. */
18632 if (last_pending_template == old_last_pend
18633 && last_error_tinst_level == old_error_tinst)
18634 ggc_free (tinst);
18635
18636 return r;
18637 }
18638
18639 /* PARM is a template parameter pack for FN. Returns true iff
18640 PARM is used in a deducible way in the argument list of FN. */
18641
18642 static bool
18643 pack_deducible_p (tree parm, tree fn)
18644 {
18645 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18646 for (; t; t = TREE_CHAIN (t))
18647 {
18648 tree type = TREE_VALUE (t);
18649 tree packs;
18650 if (!PACK_EXPANSION_P (type))
18651 continue;
18652 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18653 packs; packs = TREE_CHAIN (packs))
18654 if (template_args_equal (TREE_VALUE (packs), parm))
18655 {
18656 /* The template parameter pack is used in a function parameter
18657 pack. If this is the end of the parameter list, the
18658 template parameter pack is deducible. */
18659 if (TREE_CHAIN (t) == void_list_node)
18660 return true;
18661 else
18662 /* Otherwise, not. Well, it could be deduced from
18663 a non-pack parameter, but doing so would end up with
18664 a deduction mismatch, so don't bother. */
18665 return false;
18666 }
18667 }
18668 /* The template parameter pack isn't used in any function parameter
18669 packs, but it might be used deeper, e.g. tuple<Args...>. */
18670 return true;
18671 }
18672
18673 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18674 NARGS elements of the arguments that are being used when calling
18675 it. TARGS is a vector into which the deduced template arguments
18676 are placed.
18677
18678 Returns either a FUNCTION_DECL for the matching specialization of FN or
18679 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18680 true, diagnostics will be printed to explain why it failed.
18681
18682 If FN is a conversion operator, or we are trying to produce a specific
18683 specialization, RETURN_TYPE is the return type desired.
18684
18685 The EXPLICIT_TARGS are explicit template arguments provided via a
18686 template-id.
18687
18688 The parameter STRICT is one of:
18689
18690 DEDUCE_CALL:
18691 We are deducing arguments for a function call, as in
18692 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18693 deducing arguments for a call to the result of a conversion
18694 function template, as in [over.call.object].
18695
18696 DEDUCE_CONV:
18697 We are deducing arguments for a conversion function, as in
18698 [temp.deduct.conv].
18699
18700 DEDUCE_EXACT:
18701 We are deducing arguments when doing an explicit instantiation
18702 as in [temp.explicit], when determining an explicit specialization
18703 as in [temp.expl.spec], or when taking the address of a function
18704 template, as in [temp.deduct.funcaddr]. */
18705
18706 tree
18707 fn_type_unification (tree fn,
18708 tree explicit_targs,
18709 tree targs,
18710 const tree *args,
18711 unsigned int nargs,
18712 tree return_type,
18713 unification_kind_t strict,
18714 int flags,
18715 bool explain_p,
18716 bool decltype_p)
18717 {
18718 tree parms;
18719 tree fntype;
18720 tree decl = NULL_TREE;
18721 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18722 bool ok;
18723 static int deduction_depth;
18724 struct pending_template *old_last_pend = last_pending_template;
18725 struct tinst_level *old_error_tinst = last_error_tinst_level;
18726
18727 tree orig_fn = fn;
18728 if (flag_new_inheriting_ctors)
18729 fn = strip_inheriting_ctors (fn);
18730
18731 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18732 tree tinst;
18733 tree r = error_mark_node;
18734
18735 tree full_targs = targs;
18736 if (TMPL_ARGS_DEPTH (targs)
18737 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18738 full_targs = (add_outermost_template_args
18739 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18740 targs));
18741
18742 if (decltype_p)
18743 complain |= tf_decltype;
18744
18745 /* In C++0x, it's possible to have a function template whose type depends
18746 on itself recursively. This is most obvious with decltype, but can also
18747 occur with enumeration scope (c++/48969). So we need to catch infinite
18748 recursion and reject the substitution at deduction time; this function
18749 will return error_mark_node for any repeated substitution.
18750
18751 This also catches excessive recursion such as when f<N> depends on
18752 f<N-1> across all integers, and returns error_mark_node for all the
18753 substitutions back up to the initial one.
18754
18755 This is, of course, not reentrant. */
18756 if (excessive_deduction_depth)
18757 return error_mark_node;
18758 tinst = build_tree_list (fn, NULL_TREE);
18759 ++deduction_depth;
18760
18761 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18762
18763 fntype = TREE_TYPE (fn);
18764 if (explicit_targs)
18765 {
18766 /* [temp.deduct]
18767
18768 The specified template arguments must match the template
18769 parameters in kind (i.e., type, nontype, template), and there
18770 must not be more arguments than there are parameters;
18771 otherwise type deduction fails.
18772
18773 Nontype arguments must match the types of the corresponding
18774 nontype template parameters, or must be convertible to the
18775 types of the corresponding nontype parameters as specified in
18776 _temp.arg.nontype_, otherwise type deduction fails.
18777
18778 All references in the function type of the function template
18779 to the corresponding template parameters are replaced by the
18780 specified template argument values. If a substitution in a
18781 template parameter or in the function type of the function
18782 template results in an invalid type, type deduction fails. */
18783 int i, len = TREE_VEC_LENGTH (tparms);
18784 location_t loc = input_location;
18785 bool incomplete = false;
18786
18787 if (explicit_targs == error_mark_node)
18788 goto fail;
18789
18790 if (TMPL_ARGS_DEPTH (explicit_targs)
18791 < TMPL_ARGS_DEPTH (full_targs))
18792 explicit_targs = add_outermost_template_args (full_targs,
18793 explicit_targs);
18794
18795 /* Adjust any explicit template arguments before entering the
18796 substitution context. */
18797 explicit_targs
18798 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18799 complain,
18800 /*require_all_args=*/false,
18801 /*use_default_args=*/false));
18802 if (explicit_targs == error_mark_node)
18803 goto fail;
18804
18805 /* Substitute the explicit args into the function type. This is
18806 necessary so that, for instance, explicitly declared function
18807 arguments can match null pointed constants. If we were given
18808 an incomplete set of explicit args, we must not do semantic
18809 processing during substitution as we could create partial
18810 instantiations. */
18811 for (i = 0; i < len; i++)
18812 {
18813 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18814 bool parameter_pack = false;
18815 tree targ = TREE_VEC_ELT (explicit_targs, i);
18816
18817 /* Dig out the actual parm. */
18818 if (TREE_CODE (parm) == TYPE_DECL
18819 || TREE_CODE (parm) == TEMPLATE_DECL)
18820 {
18821 parm = TREE_TYPE (parm);
18822 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18823 }
18824 else if (TREE_CODE (parm) == PARM_DECL)
18825 {
18826 parm = DECL_INITIAL (parm);
18827 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18828 }
18829
18830 if (!parameter_pack && targ == NULL_TREE)
18831 /* No explicit argument for this template parameter. */
18832 incomplete = true;
18833
18834 if (parameter_pack && pack_deducible_p (parm, fn))
18835 {
18836 /* Mark the argument pack as "incomplete". We could
18837 still deduce more arguments during unification.
18838 We remove this mark in type_unification_real. */
18839 if (targ)
18840 {
18841 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18842 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18843 = ARGUMENT_PACK_ARGS (targ);
18844 }
18845
18846 /* We have some incomplete argument packs. */
18847 incomplete = true;
18848 }
18849 }
18850
18851 TREE_VALUE (tinst) = explicit_targs;
18852 if (!push_tinst_level (tinst))
18853 {
18854 excessive_deduction_depth = true;
18855 goto fail;
18856 }
18857 processing_template_decl += incomplete;
18858 input_location = DECL_SOURCE_LOCATION (fn);
18859 /* Ignore any access checks; we'll see them again in
18860 instantiate_template and they might have the wrong
18861 access path at this point. */
18862 push_deferring_access_checks (dk_deferred);
18863 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18864 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18865 pop_deferring_access_checks ();
18866 input_location = loc;
18867 processing_template_decl -= incomplete;
18868 pop_tinst_level ();
18869
18870 if (fntype == error_mark_node)
18871 goto fail;
18872
18873 /* Place the explicitly specified arguments in TARGS. */
18874 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18875 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18876 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18877 }
18878
18879 /* Never do unification on the 'this' parameter. */
18880 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18881
18882 if (return_type && strict == DEDUCE_CALL)
18883 {
18884 /* We're deducing for a call to the result of a template conversion
18885 function. The parms we really want are in return_type. */
18886 if (POINTER_TYPE_P (return_type))
18887 return_type = TREE_TYPE (return_type);
18888 parms = TYPE_ARG_TYPES (return_type);
18889 }
18890 else if (return_type)
18891 {
18892 tree *new_args;
18893
18894 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18895 new_args = XALLOCAVEC (tree, nargs + 1);
18896 new_args[0] = return_type;
18897 memcpy (new_args + 1, args, nargs * sizeof (tree));
18898 args = new_args;
18899 ++nargs;
18900 }
18901
18902 /* We allow incomplete unification without an error message here
18903 because the standard doesn't seem to explicitly prohibit it. Our
18904 callers must be ready to deal with unification failures in any
18905 event. */
18906
18907 TREE_VALUE (tinst) = targs;
18908 /* If we aren't explaining yet, push tinst context so we can see where
18909 any errors (e.g. from class instantiations triggered by instantiation
18910 of default template arguments) come from. If we are explaining, this
18911 context is redundant. */
18912 if (!explain_p && !push_tinst_level (tinst))
18913 {
18914 excessive_deduction_depth = true;
18915 goto fail;
18916 }
18917
18918 /* type_unification_real will pass back any access checks from default
18919 template argument substitution. */
18920 vec<deferred_access_check, va_gc> *checks;
18921 checks = NULL;
18922
18923 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18924 full_targs, parms, args, nargs, /*subr=*/0,
18925 strict, flags, &checks, explain_p);
18926 if (!explain_p)
18927 pop_tinst_level ();
18928 if (!ok)
18929 goto fail;
18930
18931 /* Now that we have bindings for all of the template arguments,
18932 ensure that the arguments deduced for the template template
18933 parameters have compatible template parameter lists. We cannot
18934 check this property before we have deduced all template
18935 arguments, because the template parameter types of a template
18936 template parameter might depend on prior template parameters
18937 deduced after the template template parameter. The following
18938 ill-formed example illustrates this issue:
18939
18940 template<typename T, template<T> class C> void f(C<5>, T);
18941
18942 template<int N> struct X {};
18943
18944 void g() {
18945 f(X<5>(), 5l); // error: template argument deduction fails
18946 }
18947
18948 The template parameter list of 'C' depends on the template type
18949 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18950 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18951 time that we deduce 'C'. */
18952 if (!template_template_parm_bindings_ok_p
18953 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18954 {
18955 unify_inconsistent_template_template_parameters (explain_p);
18956 goto fail;
18957 }
18958
18959 /* All is well so far. Now, check:
18960
18961 [temp.deduct]
18962
18963 When all template arguments have been deduced, all uses of
18964 template parameters in nondeduced contexts are replaced with
18965 the corresponding deduced argument values. If the
18966 substitution results in an invalid type, as described above,
18967 type deduction fails. */
18968 TREE_VALUE (tinst) = targs;
18969 if (!push_tinst_level (tinst))
18970 {
18971 excessive_deduction_depth = true;
18972 goto fail;
18973 }
18974
18975 /* Also collect access checks from the instantiation. */
18976 reopen_deferring_access_checks (checks);
18977
18978 decl = instantiate_template (fn, targs, complain);
18979
18980 checks = get_deferred_access_checks ();
18981 pop_deferring_access_checks ();
18982
18983 pop_tinst_level ();
18984
18985 if (decl == error_mark_node)
18986 goto fail;
18987
18988 /* Now perform any access checks encountered during substitution. */
18989 push_access_scope (decl);
18990 ok = perform_access_checks (checks, complain);
18991 pop_access_scope (decl);
18992 if (!ok)
18993 goto fail;
18994
18995 /* If we're looking for an exact match, check that what we got
18996 is indeed an exact match. It might not be if some template
18997 parameters are used in non-deduced contexts. But don't check
18998 for an exact match if we have dependent template arguments;
18999 in that case we're doing partial ordering, and we already know
19000 that we have two candidates that will provide the actual type. */
19001 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19002 {
19003 tree substed = TREE_TYPE (decl);
19004 unsigned int i;
19005
19006 tree sarg
19007 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19008 if (return_type)
19009 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19010 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19011 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19012 {
19013 unify_type_mismatch (explain_p, args[i],
19014 TREE_VALUE (sarg));
19015 goto fail;
19016 }
19017 }
19018
19019 /* After doing deduction with the inherited constructor, actually return an
19020 instantiation of the inheriting constructor. */
19021 if (orig_fn != fn)
19022 decl = instantiate_template (orig_fn, targs, complain);
19023
19024 r = decl;
19025
19026 fail:
19027 --deduction_depth;
19028 if (excessive_deduction_depth)
19029 {
19030 if (deduction_depth == 0)
19031 /* Reset once we're all the way out. */
19032 excessive_deduction_depth = false;
19033 }
19034
19035 /* We can't free this if a pending_template entry or last_error_tinst_level
19036 is pointing at it. */
19037 if (last_pending_template == old_last_pend
19038 && last_error_tinst_level == old_error_tinst)
19039 ggc_free (tinst);
19040
19041 return r;
19042 }
19043
19044 /* Adjust types before performing type deduction, as described in
19045 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19046 sections are symmetric. PARM is the type of a function parameter
19047 or the return type of the conversion function. ARG is the type of
19048 the argument passed to the call, or the type of the value
19049 initialized with the result of the conversion function.
19050 ARG_EXPR is the original argument expression, which may be null. */
19051
19052 static int
19053 maybe_adjust_types_for_deduction (unification_kind_t strict,
19054 tree* parm,
19055 tree* arg,
19056 tree arg_expr)
19057 {
19058 int result = 0;
19059
19060 switch (strict)
19061 {
19062 case DEDUCE_CALL:
19063 break;
19064
19065 case DEDUCE_CONV:
19066 /* Swap PARM and ARG throughout the remainder of this
19067 function; the handling is precisely symmetric since PARM
19068 will initialize ARG rather than vice versa. */
19069 std::swap (parm, arg);
19070 break;
19071
19072 case DEDUCE_EXACT:
19073 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19074 too, but here handle it by stripping the reference from PARM
19075 rather than by adding it to ARG. */
19076 if (TREE_CODE (*parm) == REFERENCE_TYPE
19077 && TYPE_REF_IS_RVALUE (*parm)
19078 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19079 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19080 && TREE_CODE (*arg) == REFERENCE_TYPE
19081 && !TYPE_REF_IS_RVALUE (*arg))
19082 *parm = TREE_TYPE (*parm);
19083 /* Nothing else to do in this case. */
19084 return 0;
19085
19086 default:
19087 gcc_unreachable ();
19088 }
19089
19090 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19091 {
19092 /* [temp.deduct.call]
19093
19094 If P is not a reference type:
19095
19096 --If A is an array type, the pointer type produced by the
19097 array-to-pointer standard conversion (_conv.array_) is
19098 used in place of A for type deduction; otherwise,
19099
19100 --If A is a function type, the pointer type produced by
19101 the function-to-pointer standard conversion
19102 (_conv.func_) is used in place of A for type deduction;
19103 otherwise,
19104
19105 --If A is a cv-qualified type, the top level
19106 cv-qualifiers of A's type are ignored for type
19107 deduction. */
19108 if (TREE_CODE (*arg) == ARRAY_TYPE)
19109 *arg = build_pointer_type (TREE_TYPE (*arg));
19110 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19111 *arg = build_pointer_type (*arg);
19112 else
19113 *arg = TYPE_MAIN_VARIANT (*arg);
19114 }
19115
19116 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19117 reference to a cv-unqualified template parameter that does not represent a
19118 template parameter of a class template (during class template argument
19119 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19120 an lvalue, the type "lvalue reference to A" is used in place of A for type
19121 deduction. */
19122 if (TREE_CODE (*parm) == REFERENCE_TYPE
19123 && TYPE_REF_IS_RVALUE (*parm)
19124 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19125 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19126 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19127 && (arg_expr ? lvalue_p (arg_expr)
19128 /* try_one_overload doesn't provide an arg_expr, but
19129 functions are always lvalues. */
19130 : TREE_CODE (*arg) == FUNCTION_TYPE))
19131 *arg = build_reference_type (*arg);
19132
19133 /* [temp.deduct.call]
19134
19135 If P is a cv-qualified type, the top level cv-qualifiers
19136 of P's type are ignored for type deduction. If P is a
19137 reference type, the type referred to by P is used for
19138 type deduction. */
19139 *parm = TYPE_MAIN_VARIANT (*parm);
19140 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19141 {
19142 *parm = TREE_TYPE (*parm);
19143 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19144 }
19145
19146 /* DR 322. For conversion deduction, remove a reference type on parm
19147 too (which has been swapped into ARG). */
19148 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19149 *arg = TREE_TYPE (*arg);
19150
19151 return result;
19152 }
19153
19154 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19155 template which does contain any deducible template parameters; check if
19156 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19157 unify_one_argument. */
19158
19159 static int
19160 check_non_deducible_conversion (tree parm, tree arg, int strict,
19161 int flags, bool explain_p)
19162 {
19163 tree type;
19164
19165 if (!TYPE_P (arg))
19166 type = TREE_TYPE (arg);
19167 else
19168 type = arg;
19169
19170 if (same_type_p (parm, type))
19171 return unify_success (explain_p);
19172
19173 if (strict == DEDUCE_CONV)
19174 {
19175 if (can_convert_arg (type, parm, NULL_TREE, flags,
19176 explain_p ? tf_warning_or_error : tf_none))
19177 return unify_success (explain_p);
19178 }
19179 else if (strict != DEDUCE_EXACT)
19180 {
19181 if (can_convert_arg (parm, type,
19182 TYPE_P (arg) ? NULL_TREE : arg,
19183 flags, explain_p ? tf_warning_or_error : tf_none))
19184 return unify_success (explain_p);
19185 }
19186
19187 if (strict == DEDUCE_EXACT)
19188 return unify_type_mismatch (explain_p, parm, arg);
19189 else
19190 return unify_arg_conversion (explain_p, parm, type, arg);
19191 }
19192
19193 static bool uses_deducible_template_parms (tree type);
19194
19195 /* Returns true iff the expression EXPR is one from which a template
19196 argument can be deduced. In other words, if it's an undecorated
19197 use of a template non-type parameter. */
19198
19199 static bool
19200 deducible_expression (tree expr)
19201 {
19202 /* Strip implicit conversions. */
19203 while (CONVERT_EXPR_P (expr))
19204 expr = TREE_OPERAND (expr, 0);
19205 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19206 }
19207
19208 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19209 deducible way; that is, if it has a max value of <PARM> - 1. */
19210
19211 static bool
19212 deducible_array_bound (tree domain)
19213 {
19214 if (domain == NULL_TREE)
19215 return false;
19216
19217 tree max = TYPE_MAX_VALUE (domain);
19218 if (TREE_CODE (max) != MINUS_EXPR)
19219 return false;
19220
19221 return deducible_expression (TREE_OPERAND (max, 0));
19222 }
19223
19224 /* Returns true iff the template arguments ARGS use a template parameter
19225 in a deducible way. */
19226
19227 static bool
19228 deducible_template_args (tree args)
19229 {
19230 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19231 {
19232 bool deducible;
19233 tree elt = TREE_VEC_ELT (args, i);
19234 if (ARGUMENT_PACK_P (elt))
19235 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19236 else
19237 {
19238 if (PACK_EXPANSION_P (elt))
19239 elt = PACK_EXPANSION_PATTERN (elt);
19240 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19241 deducible = true;
19242 else if (TYPE_P (elt))
19243 deducible = uses_deducible_template_parms (elt);
19244 else
19245 deducible = deducible_expression (elt);
19246 }
19247 if (deducible)
19248 return true;
19249 }
19250 return false;
19251 }
19252
19253 /* Returns true iff TYPE contains any deducible references to template
19254 parameters, as per 14.8.2.5. */
19255
19256 static bool
19257 uses_deducible_template_parms (tree type)
19258 {
19259 if (PACK_EXPANSION_P (type))
19260 type = PACK_EXPANSION_PATTERN (type);
19261
19262 /* T
19263 cv-list T
19264 TT<T>
19265 TT<i>
19266 TT<> */
19267 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19268 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19269 return true;
19270
19271 /* T*
19272 T&
19273 T&& */
19274 if (POINTER_TYPE_P (type))
19275 return uses_deducible_template_parms (TREE_TYPE (type));
19276
19277 /* T[integer-constant ]
19278 type [i] */
19279 if (TREE_CODE (type) == ARRAY_TYPE)
19280 return (uses_deducible_template_parms (TREE_TYPE (type))
19281 || deducible_array_bound (TYPE_DOMAIN (type)));
19282
19283 /* T type ::*
19284 type T::*
19285 T T::*
19286 T (type ::*)()
19287 type (T::*)()
19288 type (type ::*)(T)
19289 type (T::*)(T)
19290 T (type ::*)(T)
19291 T (T::*)()
19292 T (T::*)(T) */
19293 if (TYPE_PTRMEM_P (type))
19294 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19295 || (uses_deducible_template_parms
19296 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19297
19298 /* template-name <T> (where template-name refers to a class template)
19299 template-name <i> (where template-name refers to a class template) */
19300 if (CLASS_TYPE_P (type)
19301 && CLASSTYPE_TEMPLATE_INFO (type)
19302 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19303 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19304 (CLASSTYPE_TI_ARGS (type)));
19305
19306 /* type (T)
19307 T()
19308 T(T) */
19309 if (TREE_CODE (type) == FUNCTION_TYPE
19310 || TREE_CODE (type) == METHOD_TYPE)
19311 {
19312 if (uses_deducible_template_parms (TREE_TYPE (type)))
19313 return true;
19314 tree parm = TYPE_ARG_TYPES (type);
19315 if (TREE_CODE (type) == METHOD_TYPE)
19316 parm = TREE_CHAIN (parm);
19317 for (; parm; parm = TREE_CHAIN (parm))
19318 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19319 return true;
19320 }
19321
19322 return false;
19323 }
19324
19325 /* Subroutine of type_unification_real and unify_pack_expansion to
19326 handle unification of a single P/A pair. Parameters are as
19327 for those functions. */
19328
19329 static int
19330 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19331 int subr, unification_kind_t strict,
19332 bool explain_p)
19333 {
19334 tree arg_expr = NULL_TREE;
19335 int arg_strict;
19336
19337 if (arg == error_mark_node || parm == error_mark_node)
19338 return unify_invalid (explain_p);
19339 if (arg == unknown_type_node)
19340 /* We can't deduce anything from this, but we might get all the
19341 template args from other function args. */
19342 return unify_success (explain_p);
19343
19344 /* Implicit conversions (Clause 4) will be performed on a function
19345 argument to convert it to the type of the corresponding function
19346 parameter if the parameter type contains no template-parameters that
19347 participate in template argument deduction. */
19348 if (strict != DEDUCE_EXACT
19349 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19350 /* For function parameters with no deducible template parameters,
19351 just return. We'll check non-dependent conversions later. */
19352 return unify_success (explain_p);
19353
19354 switch (strict)
19355 {
19356 case DEDUCE_CALL:
19357 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19358 | UNIFY_ALLOW_MORE_CV_QUAL
19359 | UNIFY_ALLOW_DERIVED);
19360 break;
19361
19362 case DEDUCE_CONV:
19363 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19364 break;
19365
19366 case DEDUCE_EXACT:
19367 arg_strict = UNIFY_ALLOW_NONE;
19368 break;
19369
19370 default:
19371 gcc_unreachable ();
19372 }
19373
19374 /* We only do these transformations if this is the top-level
19375 parameter_type_list in a call or declaration matching; in other
19376 situations (nested function declarators, template argument lists) we
19377 won't be comparing a type to an expression, and we don't do any type
19378 adjustments. */
19379 if (!subr)
19380 {
19381 if (!TYPE_P (arg))
19382 {
19383 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19384 if (type_unknown_p (arg))
19385 {
19386 /* [temp.deduct.type] A template-argument can be
19387 deduced from a pointer to function or pointer
19388 to member function argument if the set of
19389 overloaded functions does not contain function
19390 templates and at most one of a set of
19391 overloaded functions provides a unique
19392 match. */
19393 resolve_overloaded_unification (tparms, targs, parm,
19394 arg, strict,
19395 arg_strict, explain_p);
19396 /* If a unique match was not found, this is a
19397 non-deduced context, so we still succeed. */
19398 return unify_success (explain_p);
19399 }
19400
19401 arg_expr = arg;
19402 arg = unlowered_expr_type (arg);
19403 if (arg == error_mark_node)
19404 return unify_invalid (explain_p);
19405 }
19406
19407 arg_strict |=
19408 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19409 }
19410 else
19411 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19412 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19413 return unify_template_argument_mismatch (explain_p, parm, arg);
19414
19415 /* For deduction from an init-list we need the actual list. */
19416 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19417 arg = arg_expr;
19418 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19419 }
19420
19421 /* for_each_template_parm callback that always returns 0. */
19422
19423 static int
19424 zero_r (tree, void *)
19425 {
19426 return 0;
19427 }
19428
19429 /* for_each_template_parm any_fn callback to handle deduction of a template
19430 type argument from the type of an array bound. */
19431
19432 static int
19433 array_deduction_r (tree t, void *data)
19434 {
19435 tree_pair_p d = (tree_pair_p)data;
19436 tree &tparms = d->purpose;
19437 tree &targs = d->value;
19438
19439 if (TREE_CODE (t) == ARRAY_TYPE)
19440 if (tree dom = TYPE_DOMAIN (t))
19441 if (tree max = TYPE_MAX_VALUE (dom))
19442 {
19443 if (TREE_CODE (max) == MINUS_EXPR)
19444 max = TREE_OPERAND (max, 0);
19445 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19446 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19447 UNIFY_ALLOW_NONE, /*explain*/false);
19448 }
19449
19450 /* Keep walking. */
19451 return 0;
19452 }
19453
19454 /* Try to deduce any not-yet-deduced template type arguments from the type of
19455 an array bound. This is handled separately from unify because 14.8.2.5 says
19456 "The type of a type parameter is only deduced from an array bound if it is
19457 not otherwise deduced." */
19458
19459 static void
19460 try_array_deduction (tree tparms, tree targs, tree parm)
19461 {
19462 tree_pair_s data = { tparms, targs };
19463 hash_set<tree> visited;
19464 for_each_template_parm (parm, zero_r, &data, &visited,
19465 /*nondeduced*/false, array_deduction_r);
19466 }
19467
19468 /* Most parms like fn_type_unification.
19469
19470 If SUBR is 1, we're being called recursively (to unify the
19471 arguments of a function or method parameter of a function
19472 template).
19473
19474 CHECKS is a pointer to a vector of access checks encountered while
19475 substituting default template arguments. */
19476
19477 static int
19478 type_unification_real (tree tparms,
19479 tree full_targs,
19480 tree xparms,
19481 const tree *xargs,
19482 unsigned int xnargs,
19483 int subr,
19484 unification_kind_t strict,
19485 int flags,
19486 vec<deferred_access_check, va_gc> **checks,
19487 bool explain_p)
19488 {
19489 tree parm, arg;
19490 int i;
19491 int ntparms = TREE_VEC_LENGTH (tparms);
19492 int saw_undeduced = 0;
19493 tree parms;
19494 const tree *args;
19495 unsigned int nargs;
19496 unsigned int ia;
19497
19498 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19499 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19500 gcc_assert (ntparms > 0);
19501
19502 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19503
19504 /* Reset the number of non-defaulted template arguments contained
19505 in TARGS. */
19506 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19507
19508 again:
19509 parms = xparms;
19510 args = xargs;
19511 nargs = xnargs;
19512
19513 ia = 0;
19514 while (parms && parms != void_list_node
19515 && ia < nargs)
19516 {
19517 parm = TREE_VALUE (parms);
19518
19519 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19520 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19521 /* For a function parameter pack that occurs at the end of the
19522 parameter-declaration-list, the type A of each remaining
19523 argument of the call is compared with the type P of the
19524 declarator-id of the function parameter pack. */
19525 break;
19526
19527 parms = TREE_CHAIN (parms);
19528
19529 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19530 /* For a function parameter pack that does not occur at the
19531 end of the parameter-declaration-list, the type of the
19532 parameter pack is a non-deduced context. */
19533 continue;
19534
19535 arg = args[ia];
19536 ++ia;
19537
19538 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19539 explain_p))
19540 return 1;
19541 }
19542
19543 if (parms
19544 && parms != void_list_node
19545 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19546 {
19547 /* Unify the remaining arguments with the pack expansion type. */
19548 tree argvec;
19549 tree parmvec = make_tree_vec (1);
19550
19551 /* Allocate a TREE_VEC and copy in all of the arguments */
19552 argvec = make_tree_vec (nargs - ia);
19553 for (i = 0; ia < nargs; ++ia, ++i)
19554 TREE_VEC_ELT (argvec, i) = args[ia];
19555
19556 /* Copy the parameter into parmvec. */
19557 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19558 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19559 /*subr=*/subr, explain_p))
19560 return 1;
19561
19562 /* Advance to the end of the list of parameters. */
19563 parms = TREE_CHAIN (parms);
19564 }
19565
19566 /* Fail if we've reached the end of the parm list, and more args
19567 are present, and the parm list isn't variadic. */
19568 if (ia < nargs && parms == void_list_node)
19569 return unify_too_many_arguments (explain_p, nargs, ia);
19570 /* Fail if parms are left and they don't have default values and
19571 they aren't all deduced as empty packs (c++/57397). This is
19572 consistent with sufficient_parms_p. */
19573 if (parms && parms != void_list_node
19574 && TREE_PURPOSE (parms) == NULL_TREE)
19575 {
19576 unsigned int count = nargs;
19577 tree p = parms;
19578 bool type_pack_p;
19579 do
19580 {
19581 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19582 if (!type_pack_p)
19583 count++;
19584 p = TREE_CHAIN (p);
19585 }
19586 while (p && p != void_list_node);
19587 if (count != nargs)
19588 return unify_too_few_arguments (explain_p, ia, count,
19589 type_pack_p);
19590 }
19591
19592 if (!subr)
19593 {
19594 tsubst_flags_t complain = (explain_p
19595 ? tf_warning_or_error
19596 : tf_none);
19597 bool tried_array_deduction = (cxx_dialect < cxx17);
19598
19599 for (i = 0; i < ntparms; i++)
19600 {
19601 tree targ = TREE_VEC_ELT (targs, i);
19602 tree tparm = TREE_VEC_ELT (tparms, i);
19603
19604 /* Clear the "incomplete" flags on all argument packs now so that
19605 substituting them into later default arguments works. */
19606 if (targ && ARGUMENT_PACK_P (targ))
19607 {
19608 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19609 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19610 }
19611
19612 if (targ || tparm == error_mark_node)
19613 continue;
19614 tparm = TREE_VALUE (tparm);
19615
19616 if (TREE_CODE (tparm) == TYPE_DECL
19617 && !tried_array_deduction)
19618 {
19619 try_array_deduction (tparms, targs, xparms);
19620 tried_array_deduction = true;
19621 if (TREE_VEC_ELT (targs, i))
19622 continue;
19623 }
19624
19625 /* If this is an undeduced nontype parameter that depends on
19626 a type parameter, try another pass; its type may have been
19627 deduced from a later argument than the one from which
19628 this parameter can be deduced. */
19629 if (TREE_CODE (tparm) == PARM_DECL
19630 && uses_template_parms (TREE_TYPE (tparm))
19631 && saw_undeduced < 2)
19632 {
19633 saw_undeduced = 1;
19634 continue;
19635 }
19636
19637 /* Core issue #226 (C++0x) [temp.deduct]:
19638
19639 If a template argument has not been deduced, its
19640 default template argument, if any, is used.
19641
19642 When we are in C++98 mode, TREE_PURPOSE will either
19643 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19644 to explicitly check cxx_dialect here. */
19645 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19646 /* OK, there is a default argument. Wait until after the
19647 conversion check to do substitution. */
19648 continue;
19649
19650 /* If the type parameter is a parameter pack, then it will
19651 be deduced to an empty parameter pack. */
19652 if (template_parameter_pack_p (tparm))
19653 {
19654 tree arg;
19655
19656 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19657 {
19658 arg = make_node (NONTYPE_ARGUMENT_PACK);
19659 TREE_CONSTANT (arg) = 1;
19660 }
19661 else
19662 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19663
19664 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19665
19666 TREE_VEC_ELT (targs, i) = arg;
19667 continue;
19668 }
19669
19670 return unify_parameter_deduction_failure (explain_p, tparm);
19671 }
19672
19673 /* DR 1391: All parameters have args, now check non-dependent parms for
19674 convertibility. */
19675 if (saw_undeduced < 2)
19676 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19677 parms && parms != void_list_node && ia < nargs; )
19678 {
19679 parm = TREE_VALUE (parms);
19680
19681 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19682 && (!TREE_CHAIN (parms)
19683 || TREE_CHAIN (parms) == void_list_node))
19684 /* For a function parameter pack that occurs at the end of the
19685 parameter-declaration-list, the type A of each remaining
19686 argument of the call is compared with the type P of the
19687 declarator-id of the function parameter pack. */
19688 break;
19689
19690 parms = TREE_CHAIN (parms);
19691
19692 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19693 /* For a function parameter pack that does not occur at the
19694 end of the parameter-declaration-list, the type of the
19695 parameter pack is a non-deduced context. */
19696 continue;
19697
19698 arg = args[ia];
19699 ++ia;
19700
19701 if (uses_template_parms (parm))
19702 continue;
19703 if (check_non_deducible_conversion (parm, arg, strict, flags,
19704 explain_p))
19705 return 1;
19706 }
19707
19708 /* Now substitute into the default template arguments. */
19709 for (i = 0; i < ntparms; i++)
19710 {
19711 tree targ = TREE_VEC_ELT (targs, i);
19712 tree tparm = TREE_VEC_ELT (tparms, i);
19713
19714 if (targ || tparm == error_mark_node)
19715 continue;
19716 tree parm = TREE_VALUE (tparm);
19717
19718 if (TREE_CODE (parm) == PARM_DECL
19719 && uses_template_parms (TREE_TYPE (parm))
19720 && saw_undeduced < 2)
19721 continue;
19722
19723 tree arg = TREE_PURPOSE (tparm);
19724 reopen_deferring_access_checks (*checks);
19725 location_t save_loc = input_location;
19726 if (DECL_P (parm))
19727 input_location = DECL_SOURCE_LOCATION (parm);
19728 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19729 if (!uses_template_parms (arg))
19730 arg = convert_template_argument (parm, arg, full_targs, complain,
19731 i, NULL_TREE);
19732 else if (saw_undeduced < 2)
19733 arg = NULL_TREE;
19734 else
19735 arg = error_mark_node;
19736 input_location = save_loc;
19737 *checks = get_deferred_access_checks ();
19738 pop_deferring_access_checks ();
19739 if (arg == error_mark_node)
19740 return 1;
19741 else if (arg)
19742 {
19743 TREE_VEC_ELT (targs, i) = arg;
19744 /* The position of the first default template argument,
19745 is also the number of non-defaulted arguments in TARGS.
19746 Record that. */
19747 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19748 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19749 }
19750 }
19751
19752 if (saw_undeduced++ == 1)
19753 goto again;
19754 }
19755
19756 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19757 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19758
19759 return unify_success (explain_p);
19760 }
19761
19762 /* Subroutine of type_unification_real. Args are like the variables
19763 at the call site. ARG is an overloaded function (or template-id);
19764 we try deducing template args from each of the overloads, and if
19765 only one succeeds, we go with that. Modifies TARGS and returns
19766 true on success. */
19767
19768 static bool
19769 resolve_overloaded_unification (tree tparms,
19770 tree targs,
19771 tree parm,
19772 tree arg,
19773 unification_kind_t strict,
19774 int sub_strict,
19775 bool explain_p)
19776 {
19777 tree tempargs = copy_node (targs);
19778 int good = 0;
19779 tree goodfn = NULL_TREE;
19780 bool addr_p;
19781
19782 if (TREE_CODE (arg) == ADDR_EXPR)
19783 {
19784 arg = TREE_OPERAND (arg, 0);
19785 addr_p = true;
19786 }
19787 else
19788 addr_p = false;
19789
19790 if (TREE_CODE (arg) == COMPONENT_REF)
19791 /* Handle `&x' where `x' is some static or non-static member
19792 function name. */
19793 arg = TREE_OPERAND (arg, 1);
19794
19795 if (TREE_CODE (arg) == OFFSET_REF)
19796 arg = TREE_OPERAND (arg, 1);
19797
19798 /* Strip baselink information. */
19799 if (BASELINK_P (arg))
19800 arg = BASELINK_FUNCTIONS (arg);
19801
19802 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19803 {
19804 /* If we got some explicit template args, we need to plug them into
19805 the affected templates before we try to unify, in case the
19806 explicit args will completely resolve the templates in question. */
19807
19808 int ok = 0;
19809 tree expl_subargs = TREE_OPERAND (arg, 1);
19810 arg = TREE_OPERAND (arg, 0);
19811
19812 for (lkp_iterator iter (arg); iter; ++iter)
19813 {
19814 tree fn = *iter;
19815 tree subargs, elem;
19816
19817 if (TREE_CODE (fn) != TEMPLATE_DECL)
19818 continue;
19819
19820 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19821 expl_subargs, NULL_TREE, tf_none,
19822 /*require_all_args=*/true,
19823 /*use_default_args=*/true);
19824 if (subargs != error_mark_node
19825 && !any_dependent_template_arguments_p (subargs))
19826 {
19827 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19828 if (try_one_overload (tparms, targs, tempargs, parm,
19829 elem, strict, sub_strict, addr_p, explain_p)
19830 && (!goodfn || !same_type_p (goodfn, elem)))
19831 {
19832 goodfn = elem;
19833 ++good;
19834 }
19835 }
19836 else if (subargs)
19837 ++ok;
19838 }
19839 /* If no templates (or more than one) are fully resolved by the
19840 explicit arguments, this template-id is a non-deduced context; it
19841 could still be OK if we deduce all template arguments for the
19842 enclosing call through other arguments. */
19843 if (good != 1)
19844 good = ok;
19845 }
19846 else if (TREE_CODE (arg) != OVERLOAD
19847 && TREE_CODE (arg) != FUNCTION_DECL)
19848 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19849 -- but the deduction does not succeed because the expression is
19850 not just the function on its own. */
19851 return false;
19852 else
19853 for (lkp_iterator iter (arg); iter; ++iter)
19854 {
19855 tree fn = *iter;
19856 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19857 strict, sub_strict, addr_p, explain_p)
19858 && (!goodfn || !decls_match (goodfn, fn)))
19859 {
19860 goodfn = fn;
19861 ++good;
19862 }
19863 }
19864
19865 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19866 to function or pointer to member function argument if the set of
19867 overloaded functions does not contain function templates and at most
19868 one of a set of overloaded functions provides a unique match.
19869
19870 So if we found multiple possibilities, we return success but don't
19871 deduce anything. */
19872
19873 if (good == 1)
19874 {
19875 int i = TREE_VEC_LENGTH (targs);
19876 for (; i--; )
19877 if (TREE_VEC_ELT (tempargs, i))
19878 {
19879 tree old = TREE_VEC_ELT (targs, i);
19880 tree new_ = TREE_VEC_ELT (tempargs, i);
19881 if (new_ && old && ARGUMENT_PACK_P (old)
19882 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19883 /* Don't forget explicit template arguments in a pack. */
19884 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19885 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19886 TREE_VEC_ELT (targs, i) = new_;
19887 }
19888 }
19889 if (good)
19890 return true;
19891
19892 return false;
19893 }
19894
19895 /* Core DR 115: In contexts where deduction is done and fails, or in
19896 contexts where deduction is not done, if a template argument list is
19897 specified and it, along with any default template arguments, identifies
19898 a single function template specialization, then the template-id is an
19899 lvalue for the function template specialization. */
19900
19901 tree
19902 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19903 {
19904 tree expr, offset, baselink;
19905 bool addr;
19906
19907 if (!type_unknown_p (orig_expr))
19908 return orig_expr;
19909
19910 expr = orig_expr;
19911 addr = false;
19912 offset = NULL_TREE;
19913 baselink = NULL_TREE;
19914
19915 if (TREE_CODE (expr) == ADDR_EXPR)
19916 {
19917 expr = TREE_OPERAND (expr, 0);
19918 addr = true;
19919 }
19920 if (TREE_CODE (expr) == OFFSET_REF)
19921 {
19922 offset = expr;
19923 expr = TREE_OPERAND (expr, 1);
19924 }
19925 if (BASELINK_P (expr))
19926 {
19927 baselink = expr;
19928 expr = BASELINK_FUNCTIONS (expr);
19929 }
19930
19931 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19932 {
19933 int good = 0;
19934 tree goodfn = NULL_TREE;
19935
19936 /* If we got some explicit template args, we need to plug them into
19937 the affected templates before we try to unify, in case the
19938 explicit args will completely resolve the templates in question. */
19939
19940 tree expl_subargs = TREE_OPERAND (expr, 1);
19941 tree arg = TREE_OPERAND (expr, 0);
19942 tree badfn = NULL_TREE;
19943 tree badargs = NULL_TREE;
19944
19945 for (lkp_iterator iter (arg); iter; ++iter)
19946 {
19947 tree fn = *iter;
19948 tree subargs, elem;
19949
19950 if (TREE_CODE (fn) != TEMPLATE_DECL)
19951 continue;
19952
19953 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19954 expl_subargs, NULL_TREE, tf_none,
19955 /*require_all_args=*/true,
19956 /*use_default_args=*/true);
19957 if (subargs != error_mark_node
19958 && !any_dependent_template_arguments_p (subargs))
19959 {
19960 elem = instantiate_template (fn, subargs, tf_none);
19961 if (elem == error_mark_node)
19962 {
19963 badfn = fn;
19964 badargs = subargs;
19965 }
19966 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19967 {
19968 goodfn = elem;
19969 ++good;
19970 }
19971 }
19972 }
19973 if (good == 1)
19974 {
19975 mark_used (goodfn);
19976 expr = goodfn;
19977 if (baselink)
19978 expr = build_baselink (BASELINK_BINFO (baselink),
19979 BASELINK_ACCESS_BINFO (baselink),
19980 expr, BASELINK_OPTYPE (baselink));
19981 if (offset)
19982 {
19983 tree base
19984 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19985 expr = build_offset_ref (base, expr, addr, complain);
19986 }
19987 if (addr)
19988 expr = cp_build_addr_expr (expr, complain);
19989 return expr;
19990 }
19991 else if (good == 0 && badargs && (complain & tf_error))
19992 /* There were no good options and at least one bad one, so let the
19993 user know what the problem is. */
19994 instantiate_template (badfn, badargs, complain);
19995 }
19996 return orig_expr;
19997 }
19998
19999 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20000 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20001 different overloads deduce different arguments for a given parm.
20002 ADDR_P is true if the expression for which deduction is being
20003 performed was of the form "& fn" rather than simply "fn".
20004
20005 Returns 1 on success. */
20006
20007 static int
20008 try_one_overload (tree tparms,
20009 tree orig_targs,
20010 tree targs,
20011 tree parm,
20012 tree arg,
20013 unification_kind_t strict,
20014 int sub_strict,
20015 bool addr_p,
20016 bool explain_p)
20017 {
20018 int nargs;
20019 tree tempargs;
20020 int i;
20021
20022 if (arg == error_mark_node)
20023 return 0;
20024
20025 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20026 to function or pointer to member function argument if the set of
20027 overloaded functions does not contain function templates and at most
20028 one of a set of overloaded functions provides a unique match.
20029
20030 So if this is a template, just return success. */
20031
20032 if (uses_template_parms (arg))
20033 return 1;
20034
20035 if (TREE_CODE (arg) == METHOD_TYPE)
20036 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20037 else if (addr_p)
20038 arg = build_pointer_type (arg);
20039
20040 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20041
20042 /* We don't copy orig_targs for this because if we have already deduced
20043 some template args from previous args, unify would complain when we
20044 try to deduce a template parameter for the same argument, even though
20045 there isn't really a conflict. */
20046 nargs = TREE_VEC_LENGTH (targs);
20047 tempargs = make_tree_vec (nargs);
20048
20049 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20050 return 0;
20051
20052 /* First make sure we didn't deduce anything that conflicts with
20053 explicitly specified args. */
20054 for (i = nargs; i--; )
20055 {
20056 tree elt = TREE_VEC_ELT (tempargs, i);
20057 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20058
20059 if (!elt)
20060 /*NOP*/;
20061 else if (uses_template_parms (elt))
20062 /* Since we're unifying against ourselves, we will fill in
20063 template args used in the function parm list with our own
20064 template parms. Discard them. */
20065 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20066 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20067 {
20068 /* Check that the argument at each index of the deduced argument pack
20069 is equivalent to the corresponding explicitly specified argument.
20070 We may have deduced more arguments than were explicitly specified,
20071 and that's OK. */
20072
20073 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20074 that's wrong if we deduce the same argument pack from multiple
20075 function arguments: it's only incomplete the first time. */
20076
20077 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20078 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20079
20080 if (TREE_VEC_LENGTH (deduced_pack)
20081 < TREE_VEC_LENGTH (explicit_pack))
20082 return 0;
20083
20084 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20085 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20086 TREE_VEC_ELT (deduced_pack, j)))
20087 return 0;
20088 }
20089 else if (oldelt && !template_args_equal (oldelt, elt))
20090 return 0;
20091 }
20092
20093 for (i = nargs; i--; )
20094 {
20095 tree elt = TREE_VEC_ELT (tempargs, i);
20096
20097 if (elt)
20098 TREE_VEC_ELT (targs, i) = elt;
20099 }
20100
20101 return 1;
20102 }
20103
20104 /* PARM is a template class (perhaps with unbound template
20105 parameters). ARG is a fully instantiated type. If ARG can be
20106 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20107 TARGS are as for unify. */
20108
20109 static tree
20110 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20111 bool explain_p)
20112 {
20113 tree copy_of_targs;
20114
20115 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20116 return NULL_TREE;
20117 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20118 /* Matches anything. */;
20119 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20120 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20121 return NULL_TREE;
20122
20123 /* We need to make a new template argument vector for the call to
20124 unify. If we used TARGS, we'd clutter it up with the result of
20125 the attempted unification, even if this class didn't work out.
20126 We also don't want to commit ourselves to all the unifications
20127 we've already done, since unification is supposed to be done on
20128 an argument-by-argument basis. In other words, consider the
20129 following pathological case:
20130
20131 template <int I, int J, int K>
20132 struct S {};
20133
20134 template <int I, int J>
20135 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20136
20137 template <int I, int J, int K>
20138 void f(S<I, J, K>, S<I, I, I>);
20139
20140 void g() {
20141 S<0, 0, 0> s0;
20142 S<0, 1, 2> s2;
20143
20144 f(s0, s2);
20145 }
20146
20147 Now, by the time we consider the unification involving `s2', we
20148 already know that we must have `f<0, 0, 0>'. But, even though
20149 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20150 because there are two ways to unify base classes of S<0, 1, 2>
20151 with S<I, I, I>. If we kept the already deduced knowledge, we
20152 would reject the possibility I=1. */
20153 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20154
20155 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20156 {
20157 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20158 return NULL_TREE;
20159 return arg;
20160 }
20161
20162 /* If unification failed, we're done. */
20163 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20164 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20165 return NULL_TREE;
20166
20167 return arg;
20168 }
20169
20170 /* Given a template type PARM and a class type ARG, find the unique
20171 base type in ARG that is an instance of PARM. We do not examine
20172 ARG itself; only its base-classes. If there is not exactly one
20173 appropriate base class, return NULL_TREE. PARM may be the type of
20174 a partial specialization, as well as a plain template type. Used
20175 by unify. */
20176
20177 static enum template_base_result
20178 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20179 bool explain_p, tree *result)
20180 {
20181 tree rval = NULL_TREE;
20182 tree binfo;
20183
20184 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20185
20186 binfo = TYPE_BINFO (complete_type (arg));
20187 if (!binfo)
20188 {
20189 /* The type could not be completed. */
20190 *result = NULL_TREE;
20191 return tbr_incomplete_type;
20192 }
20193
20194 /* Walk in inheritance graph order. The search order is not
20195 important, and this avoids multiple walks of virtual bases. */
20196 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20197 {
20198 tree r = try_class_unification (tparms, targs, parm,
20199 BINFO_TYPE (binfo), explain_p);
20200
20201 if (r)
20202 {
20203 /* If there is more than one satisfactory baseclass, then:
20204
20205 [temp.deduct.call]
20206
20207 If they yield more than one possible deduced A, the type
20208 deduction fails.
20209
20210 applies. */
20211 if (rval && !same_type_p (r, rval))
20212 {
20213 *result = NULL_TREE;
20214 return tbr_ambiguous_baseclass;
20215 }
20216
20217 rval = r;
20218 }
20219 }
20220
20221 *result = rval;
20222 return tbr_success;
20223 }
20224
20225 /* Returns the level of DECL, which declares a template parameter. */
20226
20227 static int
20228 template_decl_level (tree decl)
20229 {
20230 switch (TREE_CODE (decl))
20231 {
20232 case TYPE_DECL:
20233 case TEMPLATE_DECL:
20234 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20235
20236 case PARM_DECL:
20237 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20238
20239 default:
20240 gcc_unreachable ();
20241 }
20242 return 0;
20243 }
20244
20245 /* Decide whether ARG can be unified with PARM, considering only the
20246 cv-qualifiers of each type, given STRICT as documented for unify.
20247 Returns nonzero iff the unification is OK on that basis. */
20248
20249 static int
20250 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20251 {
20252 int arg_quals = cp_type_quals (arg);
20253 int parm_quals = cp_type_quals (parm);
20254
20255 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20256 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20257 {
20258 /* Although a CVR qualifier is ignored when being applied to a
20259 substituted template parameter ([8.3.2]/1 for example), that
20260 does not allow us to unify "const T" with "int&" because both
20261 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20262 It is ok when we're allowing additional CV qualifiers
20263 at the outer level [14.8.2.1]/3,1st bullet. */
20264 if ((TREE_CODE (arg) == REFERENCE_TYPE
20265 || TREE_CODE (arg) == FUNCTION_TYPE
20266 || TREE_CODE (arg) == METHOD_TYPE)
20267 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20268 return 0;
20269
20270 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20271 && (parm_quals & TYPE_QUAL_RESTRICT))
20272 return 0;
20273 }
20274
20275 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20276 && (arg_quals & parm_quals) != parm_quals)
20277 return 0;
20278
20279 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20280 && (parm_quals & arg_quals) != arg_quals)
20281 return 0;
20282
20283 return 1;
20284 }
20285
20286 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20287 void
20288 template_parm_level_and_index (tree parm, int* level, int* index)
20289 {
20290 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20291 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20292 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20293 {
20294 *index = TEMPLATE_TYPE_IDX (parm);
20295 *level = TEMPLATE_TYPE_LEVEL (parm);
20296 }
20297 else
20298 {
20299 *index = TEMPLATE_PARM_IDX (parm);
20300 *level = TEMPLATE_PARM_LEVEL (parm);
20301 }
20302 }
20303
20304 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20305 do { \
20306 if (unify (TP, TA, P, A, S, EP)) \
20307 return 1; \
20308 } while (0)
20309
20310 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20311 expansion at the end of PACKED_PARMS. Returns 0 if the type
20312 deduction succeeds, 1 otherwise. STRICT is the same as in
20313 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20314 function call argument list. We'll need to adjust the arguments to make them
20315 types. SUBR tells us if this is from a recursive call to
20316 type_unification_real, or for comparing two template argument
20317 lists. */
20318
20319 static int
20320 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20321 tree packed_args, unification_kind_t strict,
20322 bool subr, bool explain_p)
20323 {
20324 tree parm
20325 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20326 tree pattern = PACK_EXPANSION_PATTERN (parm);
20327 tree pack, packs = NULL_TREE;
20328 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20329
20330 /* Add in any args remembered from an earlier partial instantiation. */
20331 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20332
20333 packed_args = expand_template_argument_pack (packed_args);
20334
20335 int len = TREE_VEC_LENGTH (packed_args);
20336
20337 /* Determine the parameter packs we will be deducing from the
20338 pattern, and record their current deductions. */
20339 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20340 pack; pack = TREE_CHAIN (pack))
20341 {
20342 tree parm_pack = TREE_VALUE (pack);
20343 int idx, level;
20344
20345 /* Determine the index and level of this parameter pack. */
20346 template_parm_level_and_index (parm_pack, &level, &idx);
20347
20348 /* Keep track of the parameter packs and their corresponding
20349 argument packs. */
20350 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20351 TREE_TYPE (packs) = make_tree_vec (len - start);
20352 }
20353
20354 /* Loop through all of the arguments that have not yet been
20355 unified and unify each with the pattern. */
20356 for (i = start; i < len; i++)
20357 {
20358 tree parm;
20359 bool any_explicit = false;
20360 tree arg = TREE_VEC_ELT (packed_args, i);
20361
20362 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20363 or the element of its argument pack at the current index if
20364 this argument was explicitly specified. */
20365 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20366 {
20367 int idx, level;
20368 tree arg, pargs;
20369 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20370
20371 arg = NULL_TREE;
20372 if (TREE_VALUE (pack)
20373 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20374 && (i - start < TREE_VEC_LENGTH (pargs)))
20375 {
20376 any_explicit = true;
20377 arg = TREE_VEC_ELT (pargs, i - start);
20378 }
20379 TMPL_ARG (targs, level, idx) = arg;
20380 }
20381
20382 /* If we had explicit template arguments, substitute them into the
20383 pattern before deduction. */
20384 if (any_explicit)
20385 {
20386 /* Some arguments might still be unspecified or dependent. */
20387 bool dependent;
20388 ++processing_template_decl;
20389 dependent = any_dependent_template_arguments_p (targs);
20390 if (!dependent)
20391 --processing_template_decl;
20392 parm = tsubst (pattern, targs,
20393 explain_p ? tf_warning_or_error : tf_none,
20394 NULL_TREE);
20395 if (dependent)
20396 --processing_template_decl;
20397 if (parm == error_mark_node)
20398 return 1;
20399 }
20400 else
20401 parm = pattern;
20402
20403 /* Unify the pattern with the current argument. */
20404 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20405 explain_p))
20406 return 1;
20407
20408 /* For each parameter pack, collect the deduced value. */
20409 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20410 {
20411 int idx, level;
20412 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20413
20414 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20415 TMPL_ARG (targs, level, idx);
20416 }
20417 }
20418
20419 /* Verify that the results of unification with the parameter packs
20420 produce results consistent with what we've seen before, and make
20421 the deduced argument packs available. */
20422 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20423 {
20424 tree old_pack = TREE_VALUE (pack);
20425 tree new_args = TREE_TYPE (pack);
20426 int i, len = TREE_VEC_LENGTH (new_args);
20427 int idx, level;
20428 bool nondeduced_p = false;
20429
20430 /* By default keep the original deduced argument pack.
20431 If necessary, more specific code is going to update the
20432 resulting deduced argument later down in this function. */
20433 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20434 TMPL_ARG (targs, level, idx) = old_pack;
20435
20436 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20437 actually deduce anything. */
20438 for (i = 0; i < len && !nondeduced_p; ++i)
20439 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20440 nondeduced_p = true;
20441 if (nondeduced_p)
20442 continue;
20443
20444 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20445 {
20446 /* If we had fewer function args than explicit template args,
20447 just use the explicits. */
20448 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20449 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20450 if (len < explicit_len)
20451 new_args = explicit_args;
20452 }
20453
20454 if (!old_pack)
20455 {
20456 tree result;
20457 /* Build the deduced *_ARGUMENT_PACK. */
20458 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20459 {
20460 result = make_node (NONTYPE_ARGUMENT_PACK);
20461 TREE_CONSTANT (result) = 1;
20462 }
20463 else
20464 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20465
20466 SET_ARGUMENT_PACK_ARGS (result, new_args);
20467
20468 /* Note the deduced argument packs for this parameter
20469 pack. */
20470 TMPL_ARG (targs, level, idx) = result;
20471 }
20472 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20473 && (ARGUMENT_PACK_ARGS (old_pack)
20474 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20475 {
20476 /* We only had the explicitly-provided arguments before, but
20477 now we have a complete set of arguments. */
20478 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20479
20480 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20481 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20482 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20483 }
20484 else
20485 {
20486 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20487 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20488
20489 if (!comp_template_args (old_args, new_args,
20490 &bad_old_arg, &bad_new_arg))
20491 /* Inconsistent unification of this parameter pack. */
20492 return unify_parameter_pack_inconsistent (explain_p,
20493 bad_old_arg,
20494 bad_new_arg);
20495 }
20496 }
20497
20498 return unify_success (explain_p);
20499 }
20500
20501 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20502 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20503 parameters and return value are as for unify. */
20504
20505 static int
20506 unify_array_domain (tree tparms, tree targs,
20507 tree parm_dom, tree arg_dom,
20508 bool explain_p)
20509 {
20510 tree parm_max;
20511 tree arg_max;
20512 bool parm_cst;
20513 bool arg_cst;
20514
20515 /* Our representation of array types uses "N - 1" as the
20516 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20517 not an integer constant. We cannot unify arbitrarily
20518 complex expressions, so we eliminate the MINUS_EXPRs
20519 here. */
20520 parm_max = TYPE_MAX_VALUE (parm_dom);
20521 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20522 if (!parm_cst)
20523 {
20524 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20525 parm_max = TREE_OPERAND (parm_max, 0);
20526 }
20527 arg_max = TYPE_MAX_VALUE (arg_dom);
20528 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20529 if (!arg_cst)
20530 {
20531 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20532 trying to unify the type of a variable with the type
20533 of a template parameter. For example:
20534
20535 template <unsigned int N>
20536 void f (char (&) [N]);
20537 int g();
20538 void h(int i) {
20539 char a[g(i)];
20540 f(a);
20541 }
20542
20543 Here, the type of the ARG will be "int [g(i)]", and
20544 may be a SAVE_EXPR, etc. */
20545 if (TREE_CODE (arg_max) != MINUS_EXPR)
20546 return unify_vla_arg (explain_p, arg_dom);
20547 arg_max = TREE_OPERAND (arg_max, 0);
20548 }
20549
20550 /* If only one of the bounds used a MINUS_EXPR, compensate
20551 by adding one to the other bound. */
20552 if (parm_cst && !arg_cst)
20553 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20554 integer_type_node,
20555 parm_max,
20556 integer_one_node);
20557 else if (arg_cst && !parm_cst)
20558 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20559 integer_type_node,
20560 arg_max,
20561 integer_one_node);
20562
20563 return unify (tparms, targs, parm_max, arg_max,
20564 UNIFY_ALLOW_INTEGER, explain_p);
20565 }
20566
20567 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20568
20569 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20570
20571 static pa_kind_t
20572 pa_kind (tree t)
20573 {
20574 if (PACK_EXPANSION_P (t))
20575 t = PACK_EXPANSION_PATTERN (t);
20576 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20577 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20578 || DECL_TYPE_TEMPLATE_P (t))
20579 return pa_tmpl;
20580 else if (TYPE_P (t))
20581 return pa_type;
20582 else
20583 return pa_expr;
20584 }
20585
20586 /* Deduce the value of template parameters. TPARMS is the (innermost)
20587 set of template parameters to a template. TARGS is the bindings
20588 for those template parameters, as determined thus far; TARGS may
20589 include template arguments for outer levels of template parameters
20590 as well. PARM is a parameter to a template function, or a
20591 subcomponent of that parameter; ARG is the corresponding argument.
20592 This function attempts to match PARM with ARG in a manner
20593 consistent with the existing assignments in TARGS. If more values
20594 are deduced, then TARGS is updated.
20595
20596 Returns 0 if the type deduction succeeds, 1 otherwise. The
20597 parameter STRICT is a bitwise or of the following flags:
20598
20599 UNIFY_ALLOW_NONE:
20600 Require an exact match between PARM and ARG.
20601 UNIFY_ALLOW_MORE_CV_QUAL:
20602 Allow the deduced ARG to be more cv-qualified (by qualification
20603 conversion) than ARG.
20604 UNIFY_ALLOW_LESS_CV_QUAL:
20605 Allow the deduced ARG to be less cv-qualified than ARG.
20606 UNIFY_ALLOW_DERIVED:
20607 Allow the deduced ARG to be a template base class of ARG,
20608 or a pointer to a template base class of the type pointed to by
20609 ARG.
20610 UNIFY_ALLOW_INTEGER:
20611 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20612 case for more information.
20613 UNIFY_ALLOW_OUTER_LEVEL:
20614 This is the outermost level of a deduction. Used to determine validity
20615 of qualification conversions. A valid qualification conversion must
20616 have const qualified pointers leading up to the inner type which
20617 requires additional CV quals, except at the outer level, where const
20618 is not required [conv.qual]. It would be normal to set this flag in
20619 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20620 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20621 This is the outermost level of a deduction, and PARM can be more CV
20622 qualified at this point.
20623 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20624 This is the outermost level of a deduction, and PARM can be less CV
20625 qualified at this point. */
20626
20627 static int
20628 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20629 bool explain_p)
20630 {
20631 int idx;
20632 tree targ;
20633 tree tparm;
20634 int strict_in = strict;
20635 tsubst_flags_t complain = (explain_p
20636 ? tf_warning_or_error
20637 : tf_none);
20638
20639 /* I don't think this will do the right thing with respect to types.
20640 But the only case I've seen it in so far has been array bounds, where
20641 signedness is the only information lost, and I think that will be
20642 okay. */
20643 while (CONVERT_EXPR_P (parm))
20644 parm = TREE_OPERAND (parm, 0);
20645
20646 if (arg == error_mark_node)
20647 return unify_invalid (explain_p);
20648 if (arg == unknown_type_node
20649 || arg == init_list_type_node)
20650 /* We can't deduce anything from this, but we might get all the
20651 template args from other function args. */
20652 return unify_success (explain_p);
20653
20654 if (parm == any_targ_node || arg == any_targ_node)
20655 return unify_success (explain_p);
20656
20657 /* If PARM uses template parameters, then we can't bail out here,
20658 even if ARG == PARM, since we won't record unifications for the
20659 template parameters. We might need them if we're trying to
20660 figure out which of two things is more specialized. */
20661 if (arg == parm && !uses_template_parms (parm))
20662 return unify_success (explain_p);
20663
20664 /* Handle init lists early, so the rest of the function can assume
20665 we're dealing with a type. */
20666 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20667 {
20668 tree elt, elttype;
20669 unsigned i;
20670 tree orig_parm = parm;
20671
20672 /* Replace T with std::initializer_list<T> for deduction. */
20673 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20674 && flag_deduce_init_list)
20675 parm = listify (parm);
20676
20677 if (!is_std_init_list (parm)
20678 && TREE_CODE (parm) != ARRAY_TYPE)
20679 /* We can only deduce from an initializer list argument if the
20680 parameter is std::initializer_list or an array; otherwise this
20681 is a non-deduced context. */
20682 return unify_success (explain_p);
20683
20684 if (TREE_CODE (parm) == ARRAY_TYPE)
20685 elttype = TREE_TYPE (parm);
20686 else
20687 {
20688 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20689 /* Deduction is defined in terms of a single type, so just punt
20690 on the (bizarre) std::initializer_list<T...>. */
20691 if (PACK_EXPANSION_P (elttype))
20692 return unify_success (explain_p);
20693 }
20694
20695 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20696 {
20697 int elt_strict = strict;
20698
20699 if (elt == error_mark_node)
20700 return unify_invalid (explain_p);
20701
20702 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20703 {
20704 tree type = TREE_TYPE (elt);
20705 if (type == error_mark_node)
20706 return unify_invalid (explain_p);
20707 /* It should only be possible to get here for a call. */
20708 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20709 elt_strict |= maybe_adjust_types_for_deduction
20710 (DEDUCE_CALL, &elttype, &type, elt);
20711 elt = type;
20712 }
20713
20714 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20715 explain_p);
20716 }
20717
20718 if (TREE_CODE (parm) == ARRAY_TYPE
20719 && deducible_array_bound (TYPE_DOMAIN (parm)))
20720 {
20721 /* Also deduce from the length of the initializer list. */
20722 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20723 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20724 if (idx == error_mark_node)
20725 return unify_invalid (explain_p);
20726 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20727 idx, explain_p);
20728 }
20729
20730 /* If the std::initializer_list<T> deduction worked, replace the
20731 deduced A with std::initializer_list<A>. */
20732 if (orig_parm != parm)
20733 {
20734 idx = TEMPLATE_TYPE_IDX (orig_parm);
20735 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20736 targ = listify (targ);
20737 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20738 }
20739 return unify_success (explain_p);
20740 }
20741
20742 /* If parm and arg aren't the same kind of thing (template, type, or
20743 expression), fail early. */
20744 if (pa_kind (parm) != pa_kind (arg))
20745 return unify_invalid (explain_p);
20746
20747 /* Immediately reject some pairs that won't unify because of
20748 cv-qualification mismatches. */
20749 if (TREE_CODE (arg) == TREE_CODE (parm)
20750 && TYPE_P (arg)
20751 /* It is the elements of the array which hold the cv quals of an array
20752 type, and the elements might be template type parms. We'll check
20753 when we recurse. */
20754 && TREE_CODE (arg) != ARRAY_TYPE
20755 /* We check the cv-qualifiers when unifying with template type
20756 parameters below. We want to allow ARG `const T' to unify with
20757 PARM `T' for example, when computing which of two templates
20758 is more specialized, for example. */
20759 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20760 && !check_cv_quals_for_unify (strict_in, arg, parm))
20761 return unify_cv_qual_mismatch (explain_p, parm, arg);
20762
20763 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20764 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20765 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20766 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20767 strict &= ~UNIFY_ALLOW_DERIVED;
20768 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20769 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20770
20771 switch (TREE_CODE (parm))
20772 {
20773 case TYPENAME_TYPE:
20774 case SCOPE_REF:
20775 case UNBOUND_CLASS_TEMPLATE:
20776 /* In a type which contains a nested-name-specifier, template
20777 argument values cannot be deduced for template parameters used
20778 within the nested-name-specifier. */
20779 return unify_success (explain_p);
20780
20781 case TEMPLATE_TYPE_PARM:
20782 case TEMPLATE_TEMPLATE_PARM:
20783 case BOUND_TEMPLATE_TEMPLATE_PARM:
20784 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20785 if (error_operand_p (tparm))
20786 return unify_invalid (explain_p);
20787
20788 if (TEMPLATE_TYPE_LEVEL (parm)
20789 != template_decl_level (tparm))
20790 /* The PARM is not one we're trying to unify. Just check
20791 to see if it matches ARG. */
20792 {
20793 if (TREE_CODE (arg) == TREE_CODE (parm)
20794 && (is_auto (parm) ? is_auto (arg)
20795 : same_type_p (parm, arg)))
20796 return unify_success (explain_p);
20797 else
20798 return unify_type_mismatch (explain_p, parm, arg);
20799 }
20800 idx = TEMPLATE_TYPE_IDX (parm);
20801 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20802 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20803 if (error_operand_p (tparm))
20804 return unify_invalid (explain_p);
20805
20806 /* Check for mixed types and values. */
20807 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20808 && TREE_CODE (tparm) != TYPE_DECL)
20809 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20810 && TREE_CODE (tparm) != TEMPLATE_DECL))
20811 gcc_unreachable ();
20812
20813 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20814 {
20815 if ((strict_in & UNIFY_ALLOW_DERIVED)
20816 && CLASS_TYPE_P (arg))
20817 {
20818 /* First try to match ARG directly. */
20819 tree t = try_class_unification (tparms, targs, parm, arg,
20820 explain_p);
20821 if (!t)
20822 {
20823 /* Otherwise, look for a suitable base of ARG, as below. */
20824 enum template_base_result r;
20825 r = get_template_base (tparms, targs, parm, arg,
20826 explain_p, &t);
20827 if (!t)
20828 return unify_no_common_base (explain_p, r, parm, arg);
20829 arg = t;
20830 }
20831 }
20832 /* ARG must be constructed from a template class or a template
20833 template parameter. */
20834 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20835 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20836 return unify_template_deduction_failure (explain_p, parm, arg);
20837
20838 /* Deduce arguments T, i from TT<T> or TT<i>. */
20839 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20840 return 1;
20841
20842 arg = TYPE_TI_TEMPLATE (arg);
20843
20844 /* Fall through to deduce template name. */
20845 }
20846
20847 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20848 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20849 {
20850 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20851
20852 /* Simple cases: Value already set, does match or doesn't. */
20853 if (targ != NULL_TREE && template_args_equal (targ, arg))
20854 return unify_success (explain_p);
20855 else if (targ)
20856 return unify_inconsistency (explain_p, parm, targ, arg);
20857 }
20858 else
20859 {
20860 /* If PARM is `const T' and ARG is only `int', we don't have
20861 a match unless we are allowing additional qualification.
20862 If ARG is `const int' and PARM is just `T' that's OK;
20863 that binds `const int' to `T'. */
20864 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20865 arg, parm))
20866 return unify_cv_qual_mismatch (explain_p, parm, arg);
20867
20868 /* Consider the case where ARG is `const volatile int' and
20869 PARM is `const T'. Then, T should be `volatile int'. */
20870 arg = cp_build_qualified_type_real
20871 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20872 if (arg == error_mark_node)
20873 return unify_invalid (explain_p);
20874
20875 /* Simple cases: Value already set, does match or doesn't. */
20876 if (targ != NULL_TREE && same_type_p (targ, arg))
20877 return unify_success (explain_p);
20878 else if (targ)
20879 return unify_inconsistency (explain_p, parm, targ, arg);
20880
20881 /* Make sure that ARG is not a variable-sized array. (Note
20882 that were talking about variable-sized arrays (like
20883 `int[n]'), rather than arrays of unknown size (like
20884 `int[]').) We'll get very confused by such a type since
20885 the bound of the array is not constant, and therefore
20886 not mangleable. Besides, such types are not allowed in
20887 ISO C++, so we can do as we please here. We do allow
20888 them for 'auto' deduction, since that isn't ABI-exposed. */
20889 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20890 return unify_vla_arg (explain_p, arg);
20891
20892 /* Strip typedefs as in convert_template_argument. */
20893 arg = canonicalize_type_argument (arg, tf_none);
20894 }
20895
20896 /* If ARG is a parameter pack or an expansion, we cannot unify
20897 against it unless PARM is also a parameter pack. */
20898 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20899 && !template_parameter_pack_p (parm))
20900 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20901
20902 /* If the argument deduction results is a METHOD_TYPE,
20903 then there is a problem.
20904 METHOD_TYPE doesn't map to any real C++ type the result of
20905 the deduction can not be of that type. */
20906 if (TREE_CODE (arg) == METHOD_TYPE)
20907 return unify_method_type_error (explain_p, arg);
20908
20909 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20910 return unify_success (explain_p);
20911
20912 case TEMPLATE_PARM_INDEX:
20913 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20914 if (error_operand_p (tparm))
20915 return unify_invalid (explain_p);
20916
20917 if (TEMPLATE_PARM_LEVEL (parm)
20918 != template_decl_level (tparm))
20919 {
20920 /* The PARM is not one we're trying to unify. Just check
20921 to see if it matches ARG. */
20922 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20923 && cp_tree_equal (parm, arg));
20924 if (result)
20925 unify_expression_unequal (explain_p, parm, arg);
20926 return result;
20927 }
20928
20929 idx = TEMPLATE_PARM_IDX (parm);
20930 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20931
20932 if (targ)
20933 {
20934 if ((strict & UNIFY_ALLOW_INTEGER)
20935 && TREE_TYPE (targ) && TREE_TYPE (arg)
20936 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
20937 /* We're deducing from an array bound, the type doesn't matter. */
20938 arg = fold_convert (TREE_TYPE (targ), arg);
20939 int x = !cp_tree_equal (targ, arg);
20940 if (x)
20941 unify_inconsistency (explain_p, parm, targ, arg);
20942 return x;
20943 }
20944
20945 /* [temp.deduct.type] If, in the declaration of a function template
20946 with a non-type template-parameter, the non-type
20947 template-parameter is used in an expression in the function
20948 parameter-list and, if the corresponding template-argument is
20949 deduced, the template-argument type shall match the type of the
20950 template-parameter exactly, except that a template-argument
20951 deduced from an array bound may be of any integral type.
20952 The non-type parameter might use already deduced type parameters. */
20953 ++processing_template_decl;
20954 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20955 --processing_template_decl;
20956 if (tree a = type_uses_auto (tparm))
20957 {
20958 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20959 if (tparm == error_mark_node)
20960 return 1;
20961 }
20962
20963 if (!TREE_TYPE (arg))
20964 /* Template-parameter dependent expression. Just accept it for now.
20965 It will later be processed in convert_template_argument. */
20966 ;
20967 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20968 non_reference (tparm)))
20969 /* OK */;
20970 else if ((strict & UNIFY_ALLOW_INTEGER)
20971 && CP_INTEGRAL_TYPE_P (tparm))
20972 /* Convert the ARG to the type of PARM; the deduced non-type
20973 template argument must exactly match the types of the
20974 corresponding parameter. */
20975 arg = fold (build_nop (tparm, arg));
20976 else if (uses_template_parms (tparm))
20977 {
20978 /* We haven't deduced the type of this parameter yet. */
20979 if (cxx_dialect >= cxx17
20980 /* We deduce from array bounds in try_array_deduction. */
20981 && !(strict & UNIFY_ALLOW_INTEGER))
20982 {
20983 /* Deduce it from the non-type argument. */
20984 tree atype = TREE_TYPE (arg);
20985 RECUR_AND_CHECK_FAILURE (tparms, targs,
20986 tparm, atype,
20987 UNIFY_ALLOW_NONE, explain_p);
20988 }
20989 else
20990 /* Try again later. */
20991 return unify_success (explain_p);
20992 }
20993 else
20994 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20995
20996 /* If ARG is a parameter pack or an expansion, we cannot unify
20997 against it unless PARM is also a parameter pack. */
20998 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20999 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21000 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21001
21002 {
21003 bool removed_attr = false;
21004 arg = strip_typedefs_expr (arg, &removed_attr);
21005 }
21006 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21007 return unify_success (explain_p);
21008
21009 case PTRMEM_CST:
21010 {
21011 /* A pointer-to-member constant can be unified only with
21012 another constant. */
21013 if (TREE_CODE (arg) != PTRMEM_CST)
21014 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21015
21016 /* Just unify the class member. It would be useless (and possibly
21017 wrong, depending on the strict flags) to unify also
21018 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21019 arg refer to the same variable, even if through different
21020 classes. For instance:
21021
21022 struct A { int x; };
21023 struct B : A { };
21024
21025 Unification of &A::x and &B::x must succeed. */
21026 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21027 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21028 }
21029
21030 case POINTER_TYPE:
21031 {
21032 if (!TYPE_PTR_P (arg))
21033 return unify_type_mismatch (explain_p, parm, arg);
21034
21035 /* [temp.deduct.call]
21036
21037 A can be another pointer or pointer to member type that can
21038 be converted to the deduced A via a qualification
21039 conversion (_conv.qual_).
21040
21041 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21042 This will allow for additional cv-qualification of the
21043 pointed-to types if appropriate. */
21044
21045 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21046 /* The derived-to-base conversion only persists through one
21047 level of pointers. */
21048 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21049
21050 return unify (tparms, targs, TREE_TYPE (parm),
21051 TREE_TYPE (arg), strict, explain_p);
21052 }
21053
21054 case REFERENCE_TYPE:
21055 if (TREE_CODE (arg) != REFERENCE_TYPE)
21056 return unify_type_mismatch (explain_p, parm, arg);
21057 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21058 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21059
21060 case ARRAY_TYPE:
21061 if (TREE_CODE (arg) != ARRAY_TYPE)
21062 return unify_type_mismatch (explain_p, parm, arg);
21063 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21064 != (TYPE_DOMAIN (arg) == NULL_TREE))
21065 return unify_type_mismatch (explain_p, parm, arg);
21066 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21067 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21068 if (TYPE_DOMAIN (parm) != NULL_TREE)
21069 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21070 TYPE_DOMAIN (arg), explain_p);
21071 return unify_success (explain_p);
21072
21073 case REAL_TYPE:
21074 case COMPLEX_TYPE:
21075 case VECTOR_TYPE:
21076 case INTEGER_TYPE:
21077 case BOOLEAN_TYPE:
21078 case ENUMERAL_TYPE:
21079 case VOID_TYPE:
21080 case NULLPTR_TYPE:
21081 if (TREE_CODE (arg) != TREE_CODE (parm))
21082 return unify_type_mismatch (explain_p, parm, arg);
21083
21084 /* We have already checked cv-qualification at the top of the
21085 function. */
21086 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21087 return unify_type_mismatch (explain_p, parm, arg);
21088
21089 /* As far as unification is concerned, this wins. Later checks
21090 will invalidate it if necessary. */
21091 return unify_success (explain_p);
21092
21093 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21094 /* Type INTEGER_CST can come from ordinary constant template args. */
21095 case INTEGER_CST:
21096 while (CONVERT_EXPR_P (arg))
21097 arg = TREE_OPERAND (arg, 0);
21098
21099 if (TREE_CODE (arg) != INTEGER_CST)
21100 return unify_template_argument_mismatch (explain_p, parm, arg);
21101 return (tree_int_cst_equal (parm, arg)
21102 ? unify_success (explain_p)
21103 : unify_template_argument_mismatch (explain_p, parm, arg));
21104
21105 case TREE_VEC:
21106 {
21107 int i, len, argslen;
21108 int parm_variadic_p = 0;
21109
21110 if (TREE_CODE (arg) != TREE_VEC)
21111 return unify_template_argument_mismatch (explain_p, parm, arg);
21112
21113 len = TREE_VEC_LENGTH (parm);
21114 argslen = TREE_VEC_LENGTH (arg);
21115
21116 /* Check for pack expansions in the parameters. */
21117 for (i = 0; i < len; ++i)
21118 {
21119 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21120 {
21121 if (i == len - 1)
21122 /* We can unify against something with a trailing
21123 parameter pack. */
21124 parm_variadic_p = 1;
21125 else
21126 /* [temp.deduct.type]/9: If the template argument list of
21127 P contains a pack expansion that is not the last
21128 template argument, the entire template argument list
21129 is a non-deduced context. */
21130 return unify_success (explain_p);
21131 }
21132 }
21133
21134 /* If we don't have enough arguments to satisfy the parameters
21135 (not counting the pack expression at the end), or we have
21136 too many arguments for a parameter list that doesn't end in
21137 a pack expression, we can't unify. */
21138 if (parm_variadic_p
21139 ? argslen < len - parm_variadic_p
21140 : argslen != len)
21141 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21142
21143 /* Unify all of the parameters that precede the (optional)
21144 pack expression. */
21145 for (i = 0; i < len - parm_variadic_p; ++i)
21146 {
21147 RECUR_AND_CHECK_FAILURE (tparms, targs,
21148 TREE_VEC_ELT (parm, i),
21149 TREE_VEC_ELT (arg, i),
21150 UNIFY_ALLOW_NONE, explain_p);
21151 }
21152 if (parm_variadic_p)
21153 return unify_pack_expansion (tparms, targs, parm, arg,
21154 DEDUCE_EXACT,
21155 /*subr=*/true, explain_p);
21156 return unify_success (explain_p);
21157 }
21158
21159 case RECORD_TYPE:
21160 case UNION_TYPE:
21161 if (TREE_CODE (arg) != TREE_CODE (parm))
21162 return unify_type_mismatch (explain_p, parm, arg);
21163
21164 if (TYPE_PTRMEMFUNC_P (parm))
21165 {
21166 if (!TYPE_PTRMEMFUNC_P (arg))
21167 return unify_type_mismatch (explain_p, parm, arg);
21168
21169 return unify (tparms, targs,
21170 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21171 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21172 strict, explain_p);
21173 }
21174 else if (TYPE_PTRMEMFUNC_P (arg))
21175 return unify_type_mismatch (explain_p, parm, arg);
21176
21177 if (CLASSTYPE_TEMPLATE_INFO (parm))
21178 {
21179 tree t = NULL_TREE;
21180
21181 if (strict_in & UNIFY_ALLOW_DERIVED)
21182 {
21183 /* First, we try to unify the PARM and ARG directly. */
21184 t = try_class_unification (tparms, targs,
21185 parm, arg, explain_p);
21186
21187 if (!t)
21188 {
21189 /* Fallback to the special case allowed in
21190 [temp.deduct.call]:
21191
21192 If P is a class, and P has the form
21193 template-id, then A can be a derived class of
21194 the deduced A. Likewise, if P is a pointer to
21195 a class of the form template-id, A can be a
21196 pointer to a derived class pointed to by the
21197 deduced A. */
21198 enum template_base_result r;
21199 r = get_template_base (tparms, targs, parm, arg,
21200 explain_p, &t);
21201
21202 if (!t)
21203 {
21204 /* Don't give the derived diagnostic if we're
21205 already dealing with the same template. */
21206 bool same_template
21207 = (CLASSTYPE_TEMPLATE_INFO (arg)
21208 && (CLASSTYPE_TI_TEMPLATE (parm)
21209 == CLASSTYPE_TI_TEMPLATE (arg)));
21210 return unify_no_common_base (explain_p && !same_template,
21211 r, parm, arg);
21212 }
21213 }
21214 }
21215 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21216 && (CLASSTYPE_TI_TEMPLATE (parm)
21217 == CLASSTYPE_TI_TEMPLATE (arg)))
21218 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21219 Then, we should unify `int' and `U'. */
21220 t = arg;
21221 else
21222 /* There's no chance of unification succeeding. */
21223 return unify_type_mismatch (explain_p, parm, arg);
21224
21225 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21226 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21227 }
21228 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21229 return unify_type_mismatch (explain_p, parm, arg);
21230 return unify_success (explain_p);
21231
21232 case METHOD_TYPE:
21233 case FUNCTION_TYPE:
21234 {
21235 unsigned int nargs;
21236 tree *args;
21237 tree a;
21238 unsigned int i;
21239
21240 if (TREE_CODE (arg) != TREE_CODE (parm))
21241 return unify_type_mismatch (explain_p, parm, arg);
21242
21243 /* CV qualifications for methods can never be deduced, they must
21244 match exactly. We need to check them explicitly here,
21245 because type_unification_real treats them as any other
21246 cv-qualified parameter. */
21247 if (TREE_CODE (parm) == METHOD_TYPE
21248 && (!check_cv_quals_for_unify
21249 (UNIFY_ALLOW_NONE,
21250 class_of_this_parm (arg),
21251 class_of_this_parm (parm))))
21252 return unify_cv_qual_mismatch (explain_p, parm, arg);
21253 if (TREE_CODE (arg) == FUNCTION_TYPE
21254 && type_memfn_quals (parm) != type_memfn_quals (arg))
21255 return unify_cv_qual_mismatch (explain_p, parm, arg);
21256 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21257 return unify_type_mismatch (explain_p, parm, arg);
21258
21259 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21260 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21261
21262 nargs = list_length (TYPE_ARG_TYPES (arg));
21263 args = XALLOCAVEC (tree, nargs);
21264 for (a = TYPE_ARG_TYPES (arg), i = 0;
21265 a != NULL_TREE && a != void_list_node;
21266 a = TREE_CHAIN (a), ++i)
21267 args[i] = TREE_VALUE (a);
21268 nargs = i;
21269
21270 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21271 args, nargs, 1, DEDUCE_EXACT,
21272 LOOKUP_NORMAL, NULL, explain_p))
21273 return 1;
21274
21275 if (flag_noexcept_type)
21276 {
21277 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21278 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21279 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21280 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21281 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21282 && uses_template_parms (TREE_PURPOSE (pspec)))
21283 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21284 TREE_PURPOSE (aspec),
21285 UNIFY_ALLOW_NONE, explain_p);
21286 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21287 return unify_type_mismatch (explain_p, parm, arg);
21288 }
21289
21290 return 0;
21291 }
21292
21293 case OFFSET_TYPE:
21294 /* Unify a pointer to member with a pointer to member function, which
21295 deduces the type of the member as a function type. */
21296 if (TYPE_PTRMEMFUNC_P (arg))
21297 {
21298 /* Check top-level cv qualifiers */
21299 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21300 return unify_cv_qual_mismatch (explain_p, parm, arg);
21301
21302 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21303 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21304 UNIFY_ALLOW_NONE, explain_p);
21305
21306 /* Determine the type of the function we are unifying against. */
21307 tree fntype = static_fn_type (arg);
21308
21309 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21310 }
21311
21312 if (TREE_CODE (arg) != OFFSET_TYPE)
21313 return unify_type_mismatch (explain_p, parm, arg);
21314 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21315 TYPE_OFFSET_BASETYPE (arg),
21316 UNIFY_ALLOW_NONE, explain_p);
21317 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21318 strict, explain_p);
21319
21320 case CONST_DECL:
21321 if (DECL_TEMPLATE_PARM_P (parm))
21322 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21323 if (arg != scalar_constant_value (parm))
21324 return unify_template_argument_mismatch (explain_p, parm, arg);
21325 return unify_success (explain_p);
21326
21327 case FIELD_DECL:
21328 case TEMPLATE_DECL:
21329 /* Matched cases are handled by the ARG == PARM test above. */
21330 return unify_template_argument_mismatch (explain_p, parm, arg);
21331
21332 case VAR_DECL:
21333 /* We might get a variable as a non-type template argument in parm if the
21334 corresponding parameter is type-dependent. Make any necessary
21335 adjustments based on whether arg is a reference. */
21336 if (CONSTANT_CLASS_P (arg))
21337 parm = fold_non_dependent_expr (parm);
21338 else if (REFERENCE_REF_P (arg))
21339 {
21340 tree sub = TREE_OPERAND (arg, 0);
21341 STRIP_NOPS (sub);
21342 if (TREE_CODE (sub) == ADDR_EXPR)
21343 arg = TREE_OPERAND (sub, 0);
21344 }
21345 /* Now use the normal expression code to check whether they match. */
21346 goto expr;
21347
21348 case TYPE_ARGUMENT_PACK:
21349 case NONTYPE_ARGUMENT_PACK:
21350 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21351 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21352
21353 case TYPEOF_TYPE:
21354 case DECLTYPE_TYPE:
21355 case UNDERLYING_TYPE:
21356 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21357 or UNDERLYING_TYPE nodes. */
21358 return unify_success (explain_p);
21359
21360 case ERROR_MARK:
21361 /* Unification fails if we hit an error node. */
21362 return unify_invalid (explain_p);
21363
21364 case INDIRECT_REF:
21365 if (REFERENCE_REF_P (parm))
21366 {
21367 bool pexp = PACK_EXPANSION_P (arg);
21368 if (pexp)
21369 arg = PACK_EXPANSION_PATTERN (arg);
21370 if (REFERENCE_REF_P (arg))
21371 arg = TREE_OPERAND (arg, 0);
21372 if (pexp)
21373 arg = make_pack_expansion (arg, complain);
21374 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21375 strict, explain_p);
21376 }
21377 /* FALLTHRU */
21378
21379 default:
21380 /* An unresolved overload is a nondeduced context. */
21381 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21382 return unify_success (explain_p);
21383 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21384 expr:
21385 /* We must be looking at an expression. This can happen with
21386 something like:
21387
21388 template <int I>
21389 void foo(S<I>, S<I + 2>);
21390
21391 This is a "nondeduced context":
21392
21393 [deduct.type]
21394
21395 The nondeduced contexts are:
21396
21397 --A type that is a template-id in which one or more of
21398 the template-arguments is an expression that references
21399 a template-parameter.
21400
21401 In these cases, we assume deduction succeeded, but don't
21402 actually infer any unifications. */
21403
21404 if (!uses_template_parms (parm)
21405 && !template_args_equal (parm, arg))
21406 return unify_expression_unequal (explain_p, parm, arg);
21407 else
21408 return unify_success (explain_p);
21409 }
21410 }
21411 #undef RECUR_AND_CHECK_FAILURE
21412 \f
21413 /* Note that DECL can be defined in this translation unit, if
21414 required. */
21415
21416 static void
21417 mark_definable (tree decl)
21418 {
21419 tree clone;
21420 DECL_NOT_REALLY_EXTERN (decl) = 1;
21421 FOR_EACH_CLONE (clone, decl)
21422 DECL_NOT_REALLY_EXTERN (clone) = 1;
21423 }
21424
21425 /* Called if RESULT is explicitly instantiated, or is a member of an
21426 explicitly instantiated class. */
21427
21428 void
21429 mark_decl_instantiated (tree result, int extern_p)
21430 {
21431 SET_DECL_EXPLICIT_INSTANTIATION (result);
21432
21433 /* If this entity has already been written out, it's too late to
21434 make any modifications. */
21435 if (TREE_ASM_WRITTEN (result))
21436 return;
21437
21438 /* For anonymous namespace we don't need to do anything. */
21439 if (decl_anon_ns_mem_p (result))
21440 {
21441 gcc_assert (!TREE_PUBLIC (result));
21442 return;
21443 }
21444
21445 if (TREE_CODE (result) != FUNCTION_DECL)
21446 /* The TREE_PUBLIC flag for function declarations will have been
21447 set correctly by tsubst. */
21448 TREE_PUBLIC (result) = 1;
21449
21450 /* This might have been set by an earlier implicit instantiation. */
21451 DECL_COMDAT (result) = 0;
21452
21453 if (extern_p)
21454 DECL_NOT_REALLY_EXTERN (result) = 0;
21455 else
21456 {
21457 mark_definable (result);
21458 mark_needed (result);
21459 /* Always make artificials weak. */
21460 if (DECL_ARTIFICIAL (result) && flag_weak)
21461 comdat_linkage (result);
21462 /* For WIN32 we also want to put explicit instantiations in
21463 linkonce sections. */
21464 else if (TREE_PUBLIC (result))
21465 maybe_make_one_only (result);
21466 }
21467
21468 /* If EXTERN_P, then this function will not be emitted -- unless
21469 followed by an explicit instantiation, at which point its linkage
21470 will be adjusted. If !EXTERN_P, then this function will be
21471 emitted here. In neither circumstance do we want
21472 import_export_decl to adjust the linkage. */
21473 DECL_INTERFACE_KNOWN (result) = 1;
21474 }
21475
21476 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21477 important template arguments. If any are missing, we check whether
21478 they're important by using error_mark_node for substituting into any
21479 args that were used for partial ordering (the ones between ARGS and END)
21480 and seeing if it bubbles up. */
21481
21482 static bool
21483 check_undeduced_parms (tree targs, tree args, tree end)
21484 {
21485 bool found = false;
21486 int i;
21487 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21488 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21489 {
21490 found = true;
21491 TREE_VEC_ELT (targs, i) = error_mark_node;
21492 }
21493 if (found)
21494 {
21495 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21496 if (substed == error_mark_node)
21497 return true;
21498 }
21499 return false;
21500 }
21501
21502 /* Given two function templates PAT1 and PAT2, return:
21503
21504 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21505 -1 if PAT2 is more specialized than PAT1.
21506 0 if neither is more specialized.
21507
21508 LEN indicates the number of parameters we should consider
21509 (defaulted parameters should not be considered).
21510
21511 The 1998 std underspecified function template partial ordering, and
21512 DR214 addresses the issue. We take pairs of arguments, one from
21513 each of the templates, and deduce them against each other. One of
21514 the templates will be more specialized if all the *other*
21515 template's arguments deduce against its arguments and at least one
21516 of its arguments *does* *not* deduce against the other template's
21517 corresponding argument. Deduction is done as for class templates.
21518 The arguments used in deduction have reference and top level cv
21519 qualifiers removed. Iff both arguments were originally reference
21520 types *and* deduction succeeds in both directions, an lvalue reference
21521 wins against an rvalue reference and otherwise the template
21522 with the more cv-qualified argument wins for that pairing (if
21523 neither is more cv-qualified, they both are equal). Unlike regular
21524 deduction, after all the arguments have been deduced in this way,
21525 we do *not* verify the deduced template argument values can be
21526 substituted into non-deduced contexts.
21527
21528 The logic can be a bit confusing here, because we look at deduce1 and
21529 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21530 can find template arguments for pat1 to make arg1 look like arg2, that
21531 means that arg2 is at least as specialized as arg1. */
21532
21533 int
21534 more_specialized_fn (tree pat1, tree pat2, int len)
21535 {
21536 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21537 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21538 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21539 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21540 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21541 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21542 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21543 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21544 tree origs1, origs2;
21545 bool lose1 = false;
21546 bool lose2 = false;
21547
21548 /* Remove the this parameter from non-static member functions. If
21549 one is a non-static member function and the other is not a static
21550 member function, remove the first parameter from that function
21551 also. This situation occurs for operator functions where we
21552 locate both a member function (with this pointer) and non-member
21553 operator (with explicit first operand). */
21554 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21555 {
21556 len--; /* LEN is the number of significant arguments for DECL1 */
21557 args1 = TREE_CHAIN (args1);
21558 if (!DECL_STATIC_FUNCTION_P (decl2))
21559 args2 = TREE_CHAIN (args2);
21560 }
21561 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21562 {
21563 args2 = TREE_CHAIN (args2);
21564 if (!DECL_STATIC_FUNCTION_P (decl1))
21565 {
21566 len--;
21567 args1 = TREE_CHAIN (args1);
21568 }
21569 }
21570
21571 /* If only one is a conversion operator, they are unordered. */
21572 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21573 return 0;
21574
21575 /* Consider the return type for a conversion function */
21576 if (DECL_CONV_FN_P (decl1))
21577 {
21578 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21579 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21580 len++;
21581 }
21582
21583 processing_template_decl++;
21584
21585 origs1 = args1;
21586 origs2 = args2;
21587
21588 while (len--
21589 /* Stop when an ellipsis is seen. */
21590 && args1 != NULL_TREE && args2 != NULL_TREE)
21591 {
21592 tree arg1 = TREE_VALUE (args1);
21593 tree arg2 = TREE_VALUE (args2);
21594 int deduce1, deduce2;
21595 int quals1 = -1;
21596 int quals2 = -1;
21597 int ref1 = 0;
21598 int ref2 = 0;
21599
21600 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21601 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21602 {
21603 /* When both arguments are pack expansions, we need only
21604 unify the patterns themselves. */
21605 arg1 = PACK_EXPANSION_PATTERN (arg1);
21606 arg2 = PACK_EXPANSION_PATTERN (arg2);
21607
21608 /* This is the last comparison we need to do. */
21609 len = 0;
21610 }
21611
21612 /* DR 1847: If a particular P contains no template-parameters that
21613 participate in template argument deduction, that P is not used to
21614 determine the ordering. */
21615 if (!uses_deducible_template_parms (arg1)
21616 && !uses_deducible_template_parms (arg2))
21617 goto next;
21618
21619 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21620 {
21621 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21622 arg1 = TREE_TYPE (arg1);
21623 quals1 = cp_type_quals (arg1);
21624 }
21625
21626 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21627 {
21628 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21629 arg2 = TREE_TYPE (arg2);
21630 quals2 = cp_type_quals (arg2);
21631 }
21632
21633 arg1 = TYPE_MAIN_VARIANT (arg1);
21634 arg2 = TYPE_MAIN_VARIANT (arg2);
21635
21636 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21637 {
21638 int i, len2 = remaining_arguments (args2);
21639 tree parmvec = make_tree_vec (1);
21640 tree argvec = make_tree_vec (len2);
21641 tree ta = args2;
21642
21643 /* Setup the parameter vector, which contains only ARG1. */
21644 TREE_VEC_ELT (parmvec, 0) = arg1;
21645
21646 /* Setup the argument vector, which contains the remaining
21647 arguments. */
21648 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21649 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21650
21651 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21652 argvec, DEDUCE_EXACT,
21653 /*subr=*/true, /*explain_p=*/false)
21654 == 0);
21655
21656 /* We cannot deduce in the other direction, because ARG1 is
21657 a pack expansion but ARG2 is not. */
21658 deduce2 = 0;
21659 }
21660 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21661 {
21662 int i, len1 = remaining_arguments (args1);
21663 tree parmvec = make_tree_vec (1);
21664 tree argvec = make_tree_vec (len1);
21665 tree ta = args1;
21666
21667 /* Setup the parameter vector, which contains only ARG1. */
21668 TREE_VEC_ELT (parmvec, 0) = arg2;
21669
21670 /* Setup the argument vector, which contains the remaining
21671 arguments. */
21672 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21673 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21674
21675 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21676 argvec, DEDUCE_EXACT,
21677 /*subr=*/true, /*explain_p=*/false)
21678 == 0);
21679
21680 /* We cannot deduce in the other direction, because ARG2 is
21681 a pack expansion but ARG1 is not.*/
21682 deduce1 = 0;
21683 }
21684
21685 else
21686 {
21687 /* The normal case, where neither argument is a pack
21688 expansion. */
21689 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21690 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21691 == 0);
21692 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21693 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21694 == 0);
21695 }
21696
21697 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21698 arg2, then arg2 is not as specialized as arg1. */
21699 if (!deduce1)
21700 lose2 = true;
21701 if (!deduce2)
21702 lose1 = true;
21703
21704 /* "If, for a given type, deduction succeeds in both directions
21705 (i.e., the types are identical after the transformations above)
21706 and both P and A were reference types (before being replaced with
21707 the type referred to above):
21708 - if the type from the argument template was an lvalue reference and
21709 the type from the parameter template was not, the argument type is
21710 considered to be more specialized than the other; otherwise,
21711 - if the type from the argument template is more cv-qualified
21712 than the type from the parameter template (as described above),
21713 the argument type is considered to be more specialized than the other;
21714 otherwise,
21715 - neither type is more specialized than the other." */
21716
21717 if (deduce1 && deduce2)
21718 {
21719 if (ref1 && ref2 && ref1 != ref2)
21720 {
21721 if (ref1 > ref2)
21722 lose1 = true;
21723 else
21724 lose2 = true;
21725 }
21726 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21727 {
21728 if ((quals1 & quals2) == quals2)
21729 lose2 = true;
21730 if ((quals1 & quals2) == quals1)
21731 lose1 = true;
21732 }
21733 }
21734
21735 if (lose1 && lose2)
21736 /* We've failed to deduce something in either direction.
21737 These must be unordered. */
21738 break;
21739
21740 next:
21741
21742 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21743 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21744 /* We have already processed all of the arguments in our
21745 handing of the pack expansion type. */
21746 len = 0;
21747
21748 args1 = TREE_CHAIN (args1);
21749 args2 = TREE_CHAIN (args2);
21750 }
21751
21752 /* "In most cases, all template parameters must have values in order for
21753 deduction to succeed, but for partial ordering purposes a template
21754 parameter may remain without a value provided it is not used in the
21755 types being used for partial ordering."
21756
21757 Thus, if we are missing any of the targs1 we need to substitute into
21758 origs1, then pat2 is not as specialized as pat1. This can happen when
21759 there is a nondeduced context. */
21760 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21761 lose2 = true;
21762 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21763 lose1 = true;
21764
21765 processing_template_decl--;
21766
21767 /* If both deductions succeed, the partial ordering selects the more
21768 constrained template. */
21769 if (!lose1 && !lose2)
21770 {
21771 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21772 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21773 lose1 = !subsumes_constraints (c1, c2);
21774 lose2 = !subsumes_constraints (c2, c1);
21775 }
21776
21777 /* All things being equal, if the next argument is a pack expansion
21778 for one function but not for the other, prefer the
21779 non-variadic function. FIXME this is bogus; see c++/41958. */
21780 if (lose1 == lose2
21781 && args1 && TREE_VALUE (args1)
21782 && args2 && TREE_VALUE (args2))
21783 {
21784 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21785 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21786 }
21787
21788 if (lose1 == lose2)
21789 return 0;
21790 else if (!lose1)
21791 return 1;
21792 else
21793 return -1;
21794 }
21795
21796 /* Determine which of two partial specializations of TMPL is more
21797 specialized.
21798
21799 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21800 to the first partial specialization. The TREE_PURPOSE is the
21801 innermost set of template parameters for the partial
21802 specialization. PAT2 is similar, but for the second template.
21803
21804 Return 1 if the first partial specialization is more specialized;
21805 -1 if the second is more specialized; 0 if neither is more
21806 specialized.
21807
21808 See [temp.class.order] for information about determining which of
21809 two templates is more specialized. */
21810
21811 static int
21812 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21813 {
21814 tree targs;
21815 int winner = 0;
21816 bool any_deductions = false;
21817
21818 tree tmpl1 = TREE_VALUE (pat1);
21819 tree tmpl2 = TREE_VALUE (pat2);
21820 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21821 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21822
21823 /* Just like what happens for functions, if we are ordering between
21824 different template specializations, we may encounter dependent
21825 types in the arguments, and we need our dependency check functions
21826 to behave correctly. */
21827 ++processing_template_decl;
21828 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21829 if (targs)
21830 {
21831 --winner;
21832 any_deductions = true;
21833 }
21834
21835 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21836 if (targs)
21837 {
21838 ++winner;
21839 any_deductions = true;
21840 }
21841 --processing_template_decl;
21842
21843 /* If both deductions succeed, the partial ordering selects the more
21844 constrained template. */
21845 if (!winner && any_deductions)
21846 return more_constrained (tmpl1, tmpl2);
21847
21848 /* In the case of a tie where at least one of the templates
21849 has a parameter pack at the end, the template with the most
21850 non-packed parameters wins. */
21851 if (winner == 0
21852 && any_deductions
21853 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21854 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21855 {
21856 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21857 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21858 int len1 = TREE_VEC_LENGTH (args1);
21859 int len2 = TREE_VEC_LENGTH (args2);
21860
21861 /* We don't count the pack expansion at the end. */
21862 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21863 --len1;
21864 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21865 --len2;
21866
21867 if (len1 > len2)
21868 return 1;
21869 else if (len1 < len2)
21870 return -1;
21871 }
21872
21873 return winner;
21874 }
21875
21876 /* Return the template arguments that will produce the function signature
21877 DECL from the function template FN, with the explicit template
21878 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21879 also match. Return NULL_TREE if no satisfactory arguments could be
21880 found. */
21881
21882 static tree
21883 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21884 {
21885 int ntparms = DECL_NTPARMS (fn);
21886 tree targs = make_tree_vec (ntparms);
21887 tree decl_type = TREE_TYPE (decl);
21888 tree decl_arg_types;
21889 tree *args;
21890 unsigned int nargs, ix;
21891 tree arg;
21892
21893 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21894
21895 /* Never do unification on the 'this' parameter. */
21896 decl_arg_types = skip_artificial_parms_for (decl,
21897 TYPE_ARG_TYPES (decl_type));
21898
21899 nargs = list_length (decl_arg_types);
21900 args = XALLOCAVEC (tree, nargs);
21901 for (arg = decl_arg_types, ix = 0;
21902 arg != NULL_TREE && arg != void_list_node;
21903 arg = TREE_CHAIN (arg), ++ix)
21904 args[ix] = TREE_VALUE (arg);
21905
21906 if (fn_type_unification (fn, explicit_args, targs,
21907 args, ix,
21908 (check_rettype || DECL_CONV_FN_P (fn)
21909 ? TREE_TYPE (decl_type) : NULL_TREE),
21910 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21911 /*decltype*/false)
21912 == error_mark_node)
21913 return NULL_TREE;
21914
21915 return targs;
21916 }
21917
21918 /* Return the innermost template arguments that, when applied to a partial
21919 specialization SPEC_TMPL of TMPL, yield the ARGS.
21920
21921 For example, suppose we have:
21922
21923 template <class T, class U> struct S {};
21924 template <class T> struct S<T*, int> {};
21925
21926 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21927 partial specialization and the ARGS will be {double*, int}. The resulting
21928 vector will be {double}, indicating that `T' is bound to `double'. */
21929
21930 static tree
21931 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21932 {
21933 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21934 tree spec_args
21935 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21936 int i, ntparms = TREE_VEC_LENGTH (tparms);
21937 tree deduced_args;
21938 tree innermost_deduced_args;
21939
21940 innermost_deduced_args = make_tree_vec (ntparms);
21941 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21942 {
21943 deduced_args = copy_node (args);
21944 SET_TMPL_ARGS_LEVEL (deduced_args,
21945 TMPL_ARGS_DEPTH (deduced_args),
21946 innermost_deduced_args);
21947 }
21948 else
21949 deduced_args = innermost_deduced_args;
21950
21951 bool tried_array_deduction = (cxx_dialect < cxx17);
21952 again:
21953 if (unify (tparms, deduced_args,
21954 INNERMOST_TEMPLATE_ARGS (spec_args),
21955 INNERMOST_TEMPLATE_ARGS (args),
21956 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21957 return NULL_TREE;
21958
21959 for (i = 0; i < ntparms; ++i)
21960 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21961 {
21962 if (!tried_array_deduction)
21963 {
21964 try_array_deduction (tparms, innermost_deduced_args,
21965 INNERMOST_TEMPLATE_ARGS (spec_args));
21966 tried_array_deduction = true;
21967 if (TREE_VEC_ELT (innermost_deduced_args, i))
21968 goto again;
21969 }
21970 return NULL_TREE;
21971 }
21972
21973 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21974 if (!push_tinst_level (tinst))
21975 {
21976 excessive_deduction_depth = true;
21977 return NULL_TREE;
21978 }
21979
21980 /* Verify that nondeduced template arguments agree with the type
21981 obtained from argument deduction.
21982
21983 For example:
21984
21985 struct A { typedef int X; };
21986 template <class T, class U> struct C {};
21987 template <class T> struct C<T, typename T::X> {};
21988
21989 Then with the instantiation `C<A, int>', we can deduce that
21990 `T' is `A' but unify () does not check whether `typename T::X'
21991 is `int'. */
21992 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21993
21994 if (spec_args != error_mark_node)
21995 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21996 INNERMOST_TEMPLATE_ARGS (spec_args),
21997 tmpl, tf_none, false, false);
21998
21999 pop_tinst_level ();
22000
22001 if (spec_args == error_mark_node
22002 /* We only need to check the innermost arguments; the other
22003 arguments will always agree. */
22004 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22005 INNERMOST_TEMPLATE_ARGS (args)))
22006 return NULL_TREE;
22007
22008 /* Now that we have bindings for all of the template arguments,
22009 ensure that the arguments deduced for the template template
22010 parameters have compatible template parameter lists. See the use
22011 of template_template_parm_bindings_ok_p in fn_type_unification
22012 for more information. */
22013 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22014 return NULL_TREE;
22015
22016 return deduced_args;
22017 }
22018
22019 // Compare two function templates T1 and T2 by deducing bindings
22020 // from one against the other. If both deductions succeed, compare
22021 // constraints to see which is more constrained.
22022 static int
22023 more_specialized_inst (tree t1, tree t2)
22024 {
22025 int fate = 0;
22026 int count = 0;
22027
22028 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22029 {
22030 --fate;
22031 ++count;
22032 }
22033
22034 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22035 {
22036 ++fate;
22037 ++count;
22038 }
22039
22040 // If both deductions succeed, then one may be more constrained.
22041 if (count == 2 && fate == 0)
22042 fate = more_constrained (t1, t2);
22043
22044 return fate;
22045 }
22046
22047 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22048 Return the TREE_LIST node with the most specialized template, if
22049 any. If there is no most specialized template, the error_mark_node
22050 is returned.
22051
22052 Note that this function does not look at, or modify, the
22053 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22054 returned is one of the elements of INSTANTIATIONS, callers may
22055 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22056 and retrieve it from the value returned. */
22057
22058 tree
22059 most_specialized_instantiation (tree templates)
22060 {
22061 tree fn, champ;
22062
22063 ++processing_template_decl;
22064
22065 champ = templates;
22066 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22067 {
22068 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22069 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22070 if (fate == -1)
22071 champ = fn;
22072 else if (!fate)
22073 {
22074 /* Equally specialized, move to next function. If there
22075 is no next function, nothing's most specialized. */
22076 fn = TREE_CHAIN (fn);
22077 champ = fn;
22078 if (!fn)
22079 break;
22080 }
22081 }
22082
22083 if (champ)
22084 /* Now verify that champ is better than everything earlier in the
22085 instantiation list. */
22086 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22087 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22088 {
22089 champ = NULL_TREE;
22090 break;
22091 }
22092 }
22093
22094 processing_template_decl--;
22095
22096 if (!champ)
22097 return error_mark_node;
22098
22099 return champ;
22100 }
22101
22102 /* If DECL is a specialization of some template, return the most
22103 general such template. Otherwise, returns NULL_TREE.
22104
22105 For example, given:
22106
22107 template <class T> struct S { template <class U> void f(U); };
22108
22109 if TMPL is `template <class U> void S<int>::f(U)' this will return
22110 the full template. This function will not trace past partial
22111 specializations, however. For example, given in addition:
22112
22113 template <class T> struct S<T*> { template <class U> void f(U); };
22114
22115 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22116 `template <class T> template <class U> S<T*>::f(U)'. */
22117
22118 tree
22119 most_general_template (tree decl)
22120 {
22121 if (TREE_CODE (decl) != TEMPLATE_DECL)
22122 {
22123 if (tree tinfo = get_template_info (decl))
22124 decl = TI_TEMPLATE (tinfo);
22125 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22126 template friend, or a FIELD_DECL for a capture pack. */
22127 if (TREE_CODE (decl) != TEMPLATE_DECL)
22128 return NULL_TREE;
22129 }
22130
22131 /* Look for more and more general templates. */
22132 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22133 {
22134 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22135 (See cp-tree.h for details.) */
22136 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22137 break;
22138
22139 if (CLASS_TYPE_P (TREE_TYPE (decl))
22140 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22141 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22142 break;
22143
22144 /* Stop if we run into an explicitly specialized class template. */
22145 if (!DECL_NAMESPACE_SCOPE_P (decl)
22146 && DECL_CONTEXT (decl)
22147 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22148 break;
22149
22150 decl = DECL_TI_TEMPLATE (decl);
22151 }
22152
22153 return decl;
22154 }
22155
22156 /* Return the most specialized of the template partial specializations
22157 which can produce TARGET, a specialization of some class or variable
22158 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22159 a TEMPLATE_DECL node corresponding to the partial specialization, while
22160 the TREE_PURPOSE is the set of template arguments that must be
22161 substituted into the template pattern in order to generate TARGET.
22162
22163 If the choice of partial specialization is ambiguous, a diagnostic
22164 is issued, and the error_mark_node is returned. If there are no
22165 partial specializations matching TARGET, then NULL_TREE is
22166 returned, indicating that the primary template should be used. */
22167
22168 static tree
22169 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22170 {
22171 tree list = NULL_TREE;
22172 tree t;
22173 tree champ;
22174 int fate;
22175 bool ambiguous_p;
22176 tree outer_args = NULL_TREE;
22177 tree tmpl, args;
22178
22179 if (TYPE_P (target))
22180 {
22181 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22182 tmpl = TI_TEMPLATE (tinfo);
22183 args = TI_ARGS (tinfo);
22184 }
22185 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22186 {
22187 tmpl = TREE_OPERAND (target, 0);
22188 args = TREE_OPERAND (target, 1);
22189 }
22190 else if (VAR_P (target))
22191 {
22192 tree tinfo = DECL_TEMPLATE_INFO (target);
22193 tmpl = TI_TEMPLATE (tinfo);
22194 args = TI_ARGS (tinfo);
22195 }
22196 else
22197 gcc_unreachable ();
22198
22199 tree main_tmpl = most_general_template (tmpl);
22200
22201 /* For determining which partial specialization to use, only the
22202 innermost args are interesting. */
22203 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22204 {
22205 outer_args = strip_innermost_template_args (args, 1);
22206 args = INNERMOST_TEMPLATE_ARGS (args);
22207 }
22208
22209 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22210 {
22211 tree spec_args;
22212 tree spec_tmpl = TREE_VALUE (t);
22213
22214 if (outer_args)
22215 {
22216 /* Substitute in the template args from the enclosing class. */
22217 ++processing_template_decl;
22218 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22219 --processing_template_decl;
22220 }
22221
22222 if (spec_tmpl == error_mark_node)
22223 return error_mark_node;
22224
22225 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22226 if (spec_args)
22227 {
22228 if (outer_args)
22229 spec_args = add_to_template_args (outer_args, spec_args);
22230
22231 /* Keep the candidate only if the constraints are satisfied,
22232 or if we're not compiling with concepts. */
22233 if (!flag_concepts
22234 || constraints_satisfied_p (spec_tmpl, spec_args))
22235 {
22236 list = tree_cons (spec_args, TREE_VALUE (t), list);
22237 TREE_TYPE (list) = TREE_TYPE (t);
22238 }
22239 }
22240 }
22241
22242 if (! list)
22243 return NULL_TREE;
22244
22245 ambiguous_p = false;
22246 t = list;
22247 champ = t;
22248 t = TREE_CHAIN (t);
22249 for (; t; t = TREE_CHAIN (t))
22250 {
22251 fate = more_specialized_partial_spec (tmpl, champ, t);
22252 if (fate == 1)
22253 ;
22254 else
22255 {
22256 if (fate == 0)
22257 {
22258 t = TREE_CHAIN (t);
22259 if (! t)
22260 {
22261 ambiguous_p = true;
22262 break;
22263 }
22264 }
22265 champ = t;
22266 }
22267 }
22268
22269 if (!ambiguous_p)
22270 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22271 {
22272 fate = more_specialized_partial_spec (tmpl, champ, t);
22273 if (fate != 1)
22274 {
22275 ambiguous_p = true;
22276 break;
22277 }
22278 }
22279
22280 if (ambiguous_p)
22281 {
22282 const char *str;
22283 char *spaces = NULL;
22284 if (!(complain & tf_error))
22285 return error_mark_node;
22286 if (TYPE_P (target))
22287 error ("ambiguous template instantiation for %q#T", target);
22288 else
22289 error ("ambiguous template instantiation for %q#D", target);
22290 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22291 for (t = list; t; t = TREE_CHAIN (t))
22292 {
22293 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22294 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22295 "%s %#qS", spaces ? spaces : str, subst);
22296 spaces = spaces ? spaces : get_spaces (str);
22297 }
22298 free (spaces);
22299 return error_mark_node;
22300 }
22301
22302 return champ;
22303 }
22304
22305 /* Explicitly instantiate DECL. */
22306
22307 void
22308 do_decl_instantiation (tree decl, tree storage)
22309 {
22310 tree result = NULL_TREE;
22311 int extern_p = 0;
22312
22313 if (!decl || decl == error_mark_node)
22314 /* An error occurred, for which grokdeclarator has already issued
22315 an appropriate message. */
22316 return;
22317 else if (! DECL_LANG_SPECIFIC (decl))
22318 {
22319 error ("explicit instantiation of non-template %q#D", decl);
22320 return;
22321 }
22322
22323 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22324 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22325
22326 if (VAR_P (decl) && !var_templ)
22327 {
22328 /* There is an asymmetry here in the way VAR_DECLs and
22329 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22330 the latter, the DECL we get back will be marked as a
22331 template instantiation, and the appropriate
22332 DECL_TEMPLATE_INFO will be set up. This does not happen for
22333 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22334 should handle VAR_DECLs as it currently handles
22335 FUNCTION_DECLs. */
22336 if (!DECL_CLASS_SCOPE_P (decl))
22337 {
22338 error ("%qD is not a static data member of a class template", decl);
22339 return;
22340 }
22341 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22342 if (!result || !VAR_P (result))
22343 {
22344 error ("no matching template for %qD found", decl);
22345 return;
22346 }
22347 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22348 {
22349 error ("type %qT for explicit instantiation %qD does not match "
22350 "declared type %qT", TREE_TYPE (result), decl,
22351 TREE_TYPE (decl));
22352 return;
22353 }
22354 }
22355 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22356 {
22357 error ("explicit instantiation of %q#D", decl);
22358 return;
22359 }
22360 else
22361 result = decl;
22362
22363 /* Check for various error cases. Note that if the explicit
22364 instantiation is valid the RESULT will currently be marked as an
22365 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22366 until we get here. */
22367
22368 if (DECL_TEMPLATE_SPECIALIZATION (result))
22369 {
22370 /* DR 259 [temp.spec].
22371
22372 Both an explicit instantiation and a declaration of an explicit
22373 specialization shall not appear in a program unless the explicit
22374 instantiation follows a declaration of the explicit specialization.
22375
22376 For a given set of template parameters, if an explicit
22377 instantiation of a template appears after a declaration of an
22378 explicit specialization for that template, the explicit
22379 instantiation has no effect. */
22380 return;
22381 }
22382 else if (DECL_EXPLICIT_INSTANTIATION (result))
22383 {
22384 /* [temp.spec]
22385
22386 No program shall explicitly instantiate any template more
22387 than once.
22388
22389 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22390 the first instantiation was `extern' and the second is not,
22391 and EXTERN_P for the opposite case. */
22392 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22393 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22394 /* If an "extern" explicit instantiation follows an ordinary
22395 explicit instantiation, the template is instantiated. */
22396 if (extern_p)
22397 return;
22398 }
22399 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22400 {
22401 error ("no matching template for %qD found", result);
22402 return;
22403 }
22404 else if (!DECL_TEMPLATE_INFO (result))
22405 {
22406 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22407 return;
22408 }
22409
22410 if (storage == NULL_TREE)
22411 ;
22412 else if (storage == ridpointers[(int) RID_EXTERN])
22413 {
22414 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22415 pedwarn (input_location, OPT_Wpedantic,
22416 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22417 "instantiations");
22418 extern_p = 1;
22419 }
22420 else
22421 error ("storage class %qD applied to template instantiation", storage);
22422
22423 check_explicit_instantiation_namespace (result);
22424 mark_decl_instantiated (result, extern_p);
22425 if (! extern_p)
22426 instantiate_decl (result, /*defer_ok=*/true,
22427 /*expl_inst_class_mem_p=*/false);
22428 }
22429
22430 static void
22431 mark_class_instantiated (tree t, int extern_p)
22432 {
22433 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22434 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22435 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22436 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22437 if (! extern_p)
22438 {
22439 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22440 rest_of_type_compilation (t, 1);
22441 }
22442 }
22443
22444 /* Called from do_type_instantiation through binding_table_foreach to
22445 do recursive instantiation for the type bound in ENTRY. */
22446 static void
22447 bt_instantiate_type_proc (binding_entry entry, void *data)
22448 {
22449 tree storage = *(tree *) data;
22450
22451 if (MAYBE_CLASS_TYPE_P (entry->type)
22452 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22453 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22454 }
22455
22456 /* Perform an explicit instantiation of template class T. STORAGE, if
22457 non-null, is the RID for extern, inline or static. COMPLAIN is
22458 nonzero if this is called from the parser, zero if called recursively,
22459 since the standard is unclear (as detailed below). */
22460
22461 void
22462 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22463 {
22464 int extern_p = 0;
22465 int nomem_p = 0;
22466 int static_p = 0;
22467 int previous_instantiation_extern_p = 0;
22468
22469 if (TREE_CODE (t) == TYPE_DECL)
22470 t = TREE_TYPE (t);
22471
22472 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22473 {
22474 tree tmpl =
22475 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22476 if (tmpl)
22477 error ("explicit instantiation of non-class template %qD", tmpl);
22478 else
22479 error ("explicit instantiation of non-template type %qT", t);
22480 return;
22481 }
22482
22483 complete_type (t);
22484
22485 if (!COMPLETE_TYPE_P (t))
22486 {
22487 if (complain & tf_error)
22488 error ("explicit instantiation of %q#T before definition of template",
22489 t);
22490 return;
22491 }
22492
22493 if (storage != NULL_TREE)
22494 {
22495 if (!in_system_header_at (input_location))
22496 {
22497 if (storage == ridpointers[(int) RID_EXTERN])
22498 {
22499 if (cxx_dialect == cxx98)
22500 pedwarn (input_location, OPT_Wpedantic,
22501 "ISO C++ 1998 forbids the use of %<extern%> on "
22502 "explicit instantiations");
22503 }
22504 else
22505 pedwarn (input_location, OPT_Wpedantic,
22506 "ISO C++ forbids the use of %qE"
22507 " on explicit instantiations", storage);
22508 }
22509
22510 if (storage == ridpointers[(int) RID_INLINE])
22511 nomem_p = 1;
22512 else if (storage == ridpointers[(int) RID_EXTERN])
22513 extern_p = 1;
22514 else if (storage == ridpointers[(int) RID_STATIC])
22515 static_p = 1;
22516 else
22517 {
22518 error ("storage class %qD applied to template instantiation",
22519 storage);
22520 extern_p = 0;
22521 }
22522 }
22523
22524 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22525 {
22526 /* DR 259 [temp.spec].
22527
22528 Both an explicit instantiation and a declaration of an explicit
22529 specialization shall not appear in a program unless the explicit
22530 instantiation follows a declaration of the explicit specialization.
22531
22532 For a given set of template parameters, if an explicit
22533 instantiation of a template appears after a declaration of an
22534 explicit specialization for that template, the explicit
22535 instantiation has no effect. */
22536 return;
22537 }
22538 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22539 {
22540 /* [temp.spec]
22541
22542 No program shall explicitly instantiate any template more
22543 than once.
22544
22545 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22546 instantiation was `extern'. If EXTERN_P then the second is.
22547 These cases are OK. */
22548 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22549
22550 if (!previous_instantiation_extern_p && !extern_p
22551 && (complain & tf_error))
22552 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22553
22554 /* If we've already instantiated the template, just return now. */
22555 if (!CLASSTYPE_INTERFACE_ONLY (t))
22556 return;
22557 }
22558
22559 check_explicit_instantiation_namespace (TYPE_NAME (t));
22560 mark_class_instantiated (t, extern_p);
22561
22562 if (nomem_p)
22563 return;
22564
22565 /* In contrast to implicit instantiation, where only the
22566 declarations, and not the definitions, of members are
22567 instantiated, we have here:
22568
22569 [temp.explicit]
22570
22571 The explicit instantiation of a class template specialization
22572 implies the instantiation of all of its members not
22573 previously explicitly specialized in the translation unit
22574 containing the explicit instantiation.
22575
22576 Of course, we can't instantiate member template classes, since we
22577 don't have any arguments for them. Note that the standard is
22578 unclear on whether the instantiation of the members are
22579 *explicit* instantiations or not. However, the most natural
22580 interpretation is that it should be an explicit
22581 instantiation. */
22582 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22583 if ((VAR_P (fld)
22584 || (TREE_CODE (fld) == FUNCTION_DECL
22585 && !static_p
22586 && user_provided_p (fld)))
22587 && DECL_TEMPLATE_INSTANTIATION (fld))
22588 {
22589 mark_decl_instantiated (fld, extern_p);
22590 if (! extern_p)
22591 instantiate_decl (fld, /*defer_ok=*/true,
22592 /*expl_inst_class_mem_p=*/true);
22593 }
22594
22595 if (CLASSTYPE_NESTED_UTDS (t))
22596 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22597 bt_instantiate_type_proc, &storage);
22598 }
22599
22600 /* Given a function DECL, which is a specialization of TMPL, modify
22601 DECL to be a re-instantiation of TMPL with the same template
22602 arguments. TMPL should be the template into which tsubst'ing
22603 should occur for DECL, not the most general template.
22604
22605 One reason for doing this is a scenario like this:
22606
22607 template <class T>
22608 void f(const T&, int i);
22609
22610 void g() { f(3, 7); }
22611
22612 template <class T>
22613 void f(const T& t, const int i) { }
22614
22615 Note that when the template is first instantiated, with
22616 instantiate_template, the resulting DECL will have no name for the
22617 first parameter, and the wrong type for the second. So, when we go
22618 to instantiate the DECL, we regenerate it. */
22619
22620 static void
22621 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22622 {
22623 /* The arguments used to instantiate DECL, from the most general
22624 template. */
22625 tree code_pattern;
22626
22627 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22628
22629 /* Make sure that we can see identifiers, and compute access
22630 correctly. */
22631 push_access_scope (decl);
22632
22633 if (TREE_CODE (decl) == FUNCTION_DECL)
22634 {
22635 tree decl_parm;
22636 tree pattern_parm;
22637 tree specs;
22638 int args_depth;
22639 int parms_depth;
22640
22641 args_depth = TMPL_ARGS_DEPTH (args);
22642 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22643 if (args_depth > parms_depth)
22644 args = get_innermost_template_args (args, parms_depth);
22645
22646 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22647 args, tf_error, NULL_TREE,
22648 /*defer_ok*/false);
22649 if (specs && specs != error_mark_node)
22650 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22651 specs);
22652
22653 /* Merge parameter declarations. */
22654 decl_parm = skip_artificial_parms_for (decl,
22655 DECL_ARGUMENTS (decl));
22656 pattern_parm
22657 = skip_artificial_parms_for (code_pattern,
22658 DECL_ARGUMENTS (code_pattern));
22659 while (decl_parm && !DECL_PACK_P (pattern_parm))
22660 {
22661 tree parm_type;
22662 tree attributes;
22663
22664 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22665 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22666 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22667 NULL_TREE);
22668 parm_type = type_decays_to (parm_type);
22669 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22670 TREE_TYPE (decl_parm) = parm_type;
22671 attributes = DECL_ATTRIBUTES (pattern_parm);
22672 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22673 {
22674 DECL_ATTRIBUTES (decl_parm) = attributes;
22675 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22676 }
22677 decl_parm = DECL_CHAIN (decl_parm);
22678 pattern_parm = DECL_CHAIN (pattern_parm);
22679 }
22680 /* Merge any parameters that match with the function parameter
22681 pack. */
22682 if (pattern_parm && DECL_PACK_P (pattern_parm))
22683 {
22684 int i, len;
22685 tree expanded_types;
22686 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22687 the parameters in this function parameter pack. */
22688 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22689 args, tf_error, NULL_TREE);
22690 len = TREE_VEC_LENGTH (expanded_types);
22691 for (i = 0; i < len; i++)
22692 {
22693 tree parm_type;
22694 tree attributes;
22695
22696 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22697 /* Rename the parameter to include the index. */
22698 DECL_NAME (decl_parm) =
22699 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22700 parm_type = TREE_VEC_ELT (expanded_types, i);
22701 parm_type = type_decays_to (parm_type);
22702 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22703 TREE_TYPE (decl_parm) = parm_type;
22704 attributes = DECL_ATTRIBUTES (pattern_parm);
22705 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22706 {
22707 DECL_ATTRIBUTES (decl_parm) = attributes;
22708 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22709 }
22710 decl_parm = DECL_CHAIN (decl_parm);
22711 }
22712 }
22713 /* Merge additional specifiers from the CODE_PATTERN. */
22714 if (DECL_DECLARED_INLINE_P (code_pattern)
22715 && !DECL_DECLARED_INLINE_P (decl))
22716 DECL_DECLARED_INLINE_P (decl) = 1;
22717 }
22718 else if (VAR_P (decl))
22719 {
22720 start_lambda_scope (decl);
22721 DECL_INITIAL (decl) =
22722 tsubst_expr (DECL_INITIAL (code_pattern), args,
22723 tf_error, DECL_TI_TEMPLATE (decl),
22724 /*integral_constant_expression_p=*/false);
22725 finish_lambda_scope ();
22726 if (VAR_HAD_UNKNOWN_BOUND (decl))
22727 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22728 tf_error, DECL_TI_TEMPLATE (decl));
22729 }
22730 else
22731 gcc_unreachable ();
22732
22733 pop_access_scope (decl);
22734 }
22735
22736 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22737 substituted to get DECL. */
22738
22739 tree
22740 template_for_substitution (tree decl)
22741 {
22742 tree tmpl = DECL_TI_TEMPLATE (decl);
22743
22744 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22745 for the instantiation. This is not always the most general
22746 template. Consider, for example:
22747
22748 template <class T>
22749 struct S { template <class U> void f();
22750 template <> void f<int>(); };
22751
22752 and an instantiation of S<double>::f<int>. We want TD to be the
22753 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22754 while (/* An instantiation cannot have a definition, so we need a
22755 more general template. */
22756 DECL_TEMPLATE_INSTANTIATION (tmpl)
22757 /* We must also deal with friend templates. Given:
22758
22759 template <class T> struct S {
22760 template <class U> friend void f() {};
22761 };
22762
22763 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22764 so far as the language is concerned, but that's still
22765 where we get the pattern for the instantiation from. On
22766 other hand, if the definition comes outside the class, say:
22767
22768 template <class T> struct S {
22769 template <class U> friend void f();
22770 };
22771 template <class U> friend void f() {}
22772
22773 we don't need to look any further. That's what the check for
22774 DECL_INITIAL is for. */
22775 || (TREE_CODE (decl) == FUNCTION_DECL
22776 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22777 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22778 {
22779 /* The present template, TD, should not be a definition. If it
22780 were a definition, we should be using it! Note that we
22781 cannot restructure the loop to just keep going until we find
22782 a template with a definition, since that might go too far if
22783 a specialization was declared, but not defined. */
22784
22785 /* Fetch the more general template. */
22786 tmpl = DECL_TI_TEMPLATE (tmpl);
22787 }
22788
22789 return tmpl;
22790 }
22791
22792 /* Returns true if we need to instantiate this template instance even if we
22793 know we aren't going to emit it. */
22794
22795 bool
22796 always_instantiate_p (tree decl)
22797 {
22798 /* We always instantiate inline functions so that we can inline them. An
22799 explicit instantiation declaration prohibits implicit instantiation of
22800 non-inline functions. With high levels of optimization, we would
22801 normally inline non-inline functions -- but we're not allowed to do
22802 that for "extern template" functions. Therefore, we check
22803 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22804 return ((TREE_CODE (decl) == FUNCTION_DECL
22805 && (DECL_DECLARED_INLINE_P (decl)
22806 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22807 /* And we need to instantiate static data members so that
22808 their initializers are available in integral constant
22809 expressions. */
22810 || (VAR_P (decl)
22811 && decl_maybe_constant_var_p (decl)));
22812 }
22813
22814 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22815 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22816 error, true otherwise. */
22817
22818 bool
22819 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22820 {
22821 tree fntype, spec, noex, clone;
22822
22823 /* Don't instantiate a noexcept-specification from template context. */
22824 if (processing_template_decl)
22825 return true;
22826
22827 if (DECL_CLONED_FUNCTION_P (fn))
22828 fn = DECL_CLONED_FUNCTION (fn);
22829 fntype = TREE_TYPE (fn);
22830 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22831
22832 if (!spec || !TREE_PURPOSE (spec))
22833 return true;
22834
22835 noex = TREE_PURPOSE (spec);
22836
22837 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22838 {
22839 static hash_set<tree>* fns = new hash_set<tree>;
22840 bool added = false;
22841 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22842 spec = get_defaulted_eh_spec (fn, complain);
22843 else if (!(added = !fns->add (fn)))
22844 {
22845 /* If hash_set::add returns true, the element was already there. */
22846 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22847 DECL_SOURCE_LOCATION (fn));
22848 error_at (loc,
22849 "exception specification of %qD depends on itself",
22850 fn);
22851 spec = noexcept_false_spec;
22852 }
22853 else if (push_tinst_level (fn))
22854 {
22855 push_access_scope (fn);
22856 push_deferring_access_checks (dk_no_deferred);
22857 input_location = DECL_SOURCE_LOCATION (fn);
22858 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22859 DEFERRED_NOEXCEPT_ARGS (noex),
22860 tf_warning_or_error, fn,
22861 /*function_p=*/false,
22862 /*integral_constant_expression_p=*/true);
22863 pop_deferring_access_checks ();
22864 pop_access_scope (fn);
22865 pop_tinst_level ();
22866 spec = build_noexcept_spec (noex, tf_warning_or_error);
22867 if (spec == error_mark_node)
22868 spec = noexcept_false_spec;
22869 }
22870 else
22871 spec = noexcept_false_spec;
22872
22873 if (added)
22874 fns->remove (fn);
22875
22876 if (spec == error_mark_node)
22877 return false;
22878
22879 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22880 }
22881
22882 FOR_EACH_CLONE (clone, fn)
22883 {
22884 if (TREE_TYPE (clone) == fntype)
22885 TREE_TYPE (clone) = TREE_TYPE (fn);
22886 else
22887 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22888 }
22889
22890 return true;
22891 }
22892
22893 /* We're starting to process the function INST, an instantiation of PATTERN;
22894 add their parameters to local_specializations. */
22895
22896 static void
22897 register_parameter_specializations (tree pattern, tree inst)
22898 {
22899 tree tmpl_parm = DECL_ARGUMENTS (pattern);
22900 tree spec_parm = DECL_ARGUMENTS (inst);
22901 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
22902 {
22903 register_local_specialization (spec_parm, tmpl_parm);
22904 spec_parm = skip_artificial_parms_for (inst, spec_parm);
22905 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
22906 }
22907 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22908 {
22909 if (!DECL_PACK_P (tmpl_parm))
22910 {
22911 register_local_specialization (spec_parm, tmpl_parm);
22912 spec_parm = DECL_CHAIN (spec_parm);
22913 }
22914 else
22915 {
22916 /* Register the (value) argument pack as a specialization of
22917 TMPL_PARM, then move on. */
22918 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22919 register_local_specialization (argpack, tmpl_parm);
22920 }
22921 }
22922 gcc_assert (!spec_parm);
22923 }
22924
22925 /* Produce the definition of D, a _DECL generated from a template. If
22926 DEFER_OK is true, then we don't have to actually do the
22927 instantiation now; we just have to do it sometime. Normally it is
22928 an error if this is an explicit instantiation but D is undefined.
22929 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22930 instantiated class template. */
22931
22932 tree
22933 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22934 {
22935 tree tmpl = DECL_TI_TEMPLATE (d);
22936 tree gen_args;
22937 tree args;
22938 tree td;
22939 tree code_pattern;
22940 tree spec;
22941 tree gen_tmpl;
22942 bool pattern_defined;
22943 location_t saved_loc = input_location;
22944 int saved_unevaluated_operand = cp_unevaluated_operand;
22945 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22946 bool external_p;
22947 bool deleted_p;
22948
22949 /* This function should only be used to instantiate templates for
22950 functions and static member variables. */
22951 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22952
22953 /* A concept is never instantiated. */
22954 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22955
22956 /* Variables are never deferred; if instantiation is required, they
22957 are instantiated right away. That allows for better code in the
22958 case that an expression refers to the value of the variable --
22959 if the variable has a constant value the referring expression can
22960 take advantage of that fact. */
22961 if (VAR_P (d))
22962 defer_ok = false;
22963
22964 /* Don't instantiate cloned functions. Instead, instantiate the
22965 functions they cloned. */
22966 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22967 d = DECL_CLONED_FUNCTION (d);
22968
22969 if (DECL_TEMPLATE_INSTANTIATED (d)
22970 || (TREE_CODE (d) == FUNCTION_DECL
22971 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22972 || DECL_TEMPLATE_SPECIALIZATION (d))
22973 /* D has already been instantiated or explicitly specialized, so
22974 there's nothing for us to do here.
22975
22976 It might seem reasonable to check whether or not D is an explicit
22977 instantiation, and, if so, stop here. But when an explicit
22978 instantiation is deferred until the end of the compilation,
22979 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22980 the instantiation. */
22981 return d;
22982
22983 /* Check to see whether we know that this template will be
22984 instantiated in some other file, as with "extern template"
22985 extension. */
22986 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22987
22988 /* In general, we do not instantiate such templates. */
22989 if (external_p && !always_instantiate_p (d))
22990 return d;
22991
22992 gen_tmpl = most_general_template (tmpl);
22993 gen_args = DECL_TI_ARGS (d);
22994
22995 if (tmpl != gen_tmpl)
22996 /* We should already have the extra args. */
22997 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22998 == TMPL_ARGS_DEPTH (gen_args));
22999 /* And what's in the hash table should match D. */
23000 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23001 || spec == NULL_TREE);
23002
23003 /* This needs to happen before any tsubsting. */
23004 if (! push_tinst_level (d))
23005 return d;
23006
23007 timevar_push (TV_TEMPLATE_INST);
23008
23009 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23010 for the instantiation. */
23011 td = template_for_substitution (d);
23012 args = gen_args;
23013
23014 if (VAR_P (d))
23015 {
23016 /* Look up an explicit specialization, if any. */
23017 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23018 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23019 if (elt && elt != error_mark_node)
23020 {
23021 td = TREE_VALUE (elt);
23022 args = TREE_PURPOSE (elt);
23023 }
23024 }
23025
23026 code_pattern = DECL_TEMPLATE_RESULT (td);
23027
23028 /* We should never be trying to instantiate a member of a class
23029 template or partial specialization. */
23030 gcc_assert (d != code_pattern);
23031
23032 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23033 || DECL_TEMPLATE_SPECIALIZATION (td))
23034 /* In the case of a friend template whose definition is provided
23035 outside the class, we may have too many arguments. Drop the
23036 ones we don't need. The same is true for specializations. */
23037 args = get_innermost_template_args
23038 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23039
23040 if (TREE_CODE (d) == FUNCTION_DECL)
23041 {
23042 deleted_p = DECL_DELETED_FN (code_pattern);
23043 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23044 && DECL_INITIAL (code_pattern) != error_mark_node)
23045 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23046 || deleted_p);
23047 }
23048 else
23049 {
23050 deleted_p = false;
23051 if (DECL_CLASS_SCOPE_P (code_pattern))
23052 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23053 || DECL_INLINE_VAR_P (code_pattern));
23054 else
23055 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23056 }
23057
23058 /* We may be in the middle of deferred access check. Disable it now. */
23059 push_deferring_access_checks (dk_no_deferred);
23060
23061 /* Unless an explicit instantiation directive has already determined
23062 the linkage of D, remember that a definition is available for
23063 this entity. */
23064 if (pattern_defined
23065 && !DECL_INTERFACE_KNOWN (d)
23066 && !DECL_NOT_REALLY_EXTERN (d))
23067 mark_definable (d);
23068
23069 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23070 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23071 input_location = DECL_SOURCE_LOCATION (d);
23072
23073 /* If D is a member of an explicitly instantiated class template,
23074 and no definition is available, treat it like an implicit
23075 instantiation. */
23076 if (!pattern_defined && expl_inst_class_mem_p
23077 && DECL_EXPLICIT_INSTANTIATION (d))
23078 {
23079 /* Leave linkage flags alone on instantiations with anonymous
23080 visibility. */
23081 if (TREE_PUBLIC (d))
23082 {
23083 DECL_NOT_REALLY_EXTERN (d) = 0;
23084 DECL_INTERFACE_KNOWN (d) = 0;
23085 }
23086 SET_DECL_IMPLICIT_INSTANTIATION (d);
23087 }
23088
23089 /* Defer all other templates, unless we have been explicitly
23090 forbidden from doing so. */
23091 if (/* If there is no definition, we cannot instantiate the
23092 template. */
23093 ! pattern_defined
23094 /* If it's OK to postpone instantiation, do so. */
23095 || defer_ok
23096 /* If this is a static data member that will be defined
23097 elsewhere, we don't want to instantiate the entire data
23098 member, but we do want to instantiate the initializer so that
23099 we can substitute that elsewhere. */
23100 || (external_p && VAR_P (d))
23101 /* Handle here a deleted function too, avoid generating
23102 its body (c++/61080). */
23103 || deleted_p)
23104 {
23105 /* The definition of the static data member is now required so
23106 we must substitute the initializer. */
23107 if (VAR_P (d)
23108 && !DECL_INITIAL (d)
23109 && DECL_INITIAL (code_pattern))
23110 {
23111 tree ns;
23112 tree init;
23113 bool const_init = false;
23114 bool enter_context = DECL_CLASS_SCOPE_P (d);
23115
23116 ns = decl_namespace_context (d);
23117 push_nested_namespace (ns);
23118 if (enter_context)
23119 push_nested_class (DECL_CONTEXT (d));
23120 init = tsubst_expr (DECL_INITIAL (code_pattern),
23121 args,
23122 tf_warning_or_error, NULL_TREE,
23123 /*integral_constant_expression_p=*/false);
23124 /* If instantiating the initializer involved instantiating this
23125 again, don't call cp_finish_decl twice. */
23126 if (!DECL_INITIAL (d))
23127 {
23128 /* Make sure the initializer is still constant, in case of
23129 circular dependency (template/instantiate6.C). */
23130 const_init
23131 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23132 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23133 /*asmspec_tree=*/NULL_TREE,
23134 LOOKUP_ONLYCONVERTING);
23135 }
23136 if (enter_context)
23137 pop_nested_class ();
23138 pop_nested_namespace (ns);
23139 }
23140
23141 /* We restore the source position here because it's used by
23142 add_pending_template. */
23143 input_location = saved_loc;
23144
23145 if (at_eof && !pattern_defined
23146 && DECL_EXPLICIT_INSTANTIATION (d)
23147 && DECL_NOT_REALLY_EXTERN (d))
23148 /* [temp.explicit]
23149
23150 The definition of a non-exported function template, a
23151 non-exported member function template, or a non-exported
23152 member function or static data member of a class template
23153 shall be present in every translation unit in which it is
23154 explicitly instantiated. */
23155 permerror (input_location, "explicit instantiation of %qD "
23156 "but no definition available", d);
23157
23158 /* If we're in unevaluated context, we just wanted to get the
23159 constant value; this isn't an odr use, so don't queue
23160 a full instantiation. */
23161 if (cp_unevaluated_operand != 0)
23162 goto out;
23163 /* ??? Historically, we have instantiated inline functions, even
23164 when marked as "extern template". */
23165 if (!(external_p && VAR_P (d)))
23166 add_pending_template (d);
23167 goto out;
23168 }
23169 /* Tell the repository that D is available in this translation unit
23170 -- and see if it is supposed to be instantiated here. */
23171 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23172 {
23173 /* In a PCH file, despite the fact that the repository hasn't
23174 requested instantiation in the PCH it is still possible that
23175 an instantiation will be required in a file that includes the
23176 PCH. */
23177 if (pch_file)
23178 add_pending_template (d);
23179 /* Instantiate inline functions so that the inliner can do its
23180 job, even though we'll not be emitting a copy of this
23181 function. */
23182 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23183 goto out;
23184 }
23185
23186 bool push_to_top, nested;
23187 tree fn_context;
23188 fn_context = decl_function_context (d);
23189 nested = current_function_decl != NULL_TREE;
23190 push_to_top = !(nested && fn_context == current_function_decl);
23191
23192 vec<tree> omp_privatization_save;
23193 if (nested)
23194 save_omp_privatization_clauses (omp_privatization_save);
23195
23196 if (push_to_top)
23197 push_to_top_level ();
23198 else
23199 {
23200 push_function_context ();
23201 cp_unevaluated_operand = 0;
23202 c_inhibit_evaluation_warnings = 0;
23203 }
23204
23205 /* Mark D as instantiated so that recursive calls to
23206 instantiate_decl do not try to instantiate it again. */
23207 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23208
23209 /* Regenerate the declaration in case the template has been modified
23210 by a subsequent redeclaration. */
23211 regenerate_decl_from_template (d, td, args);
23212
23213 /* We already set the file and line above. Reset them now in case
23214 they changed as a result of calling regenerate_decl_from_template. */
23215 input_location = DECL_SOURCE_LOCATION (d);
23216
23217 if (VAR_P (d))
23218 {
23219 tree init;
23220 bool const_init = false;
23221
23222 /* Clear out DECL_RTL; whatever was there before may not be right
23223 since we've reset the type of the declaration. */
23224 SET_DECL_RTL (d, NULL);
23225 DECL_IN_AGGR_P (d) = 0;
23226
23227 /* The initializer is placed in DECL_INITIAL by
23228 regenerate_decl_from_template so we don't need to
23229 push/pop_access_scope again here. Pull it out so that
23230 cp_finish_decl can process it. */
23231 init = DECL_INITIAL (d);
23232 DECL_INITIAL (d) = NULL_TREE;
23233 DECL_INITIALIZED_P (d) = 0;
23234
23235 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23236 initializer. That function will defer actual emission until
23237 we have a chance to determine linkage. */
23238 DECL_EXTERNAL (d) = 0;
23239
23240 /* Enter the scope of D so that access-checking works correctly. */
23241 bool enter_context = DECL_CLASS_SCOPE_P (d);
23242 if (enter_context)
23243 push_nested_class (DECL_CONTEXT (d));
23244
23245 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23246 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23247
23248 if (enter_context)
23249 pop_nested_class ();
23250
23251 if (variable_template_p (gen_tmpl))
23252 note_variable_template_instantiation (d);
23253 }
23254 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23255 synthesize_method (d);
23256 else if (TREE_CODE (d) == FUNCTION_DECL)
23257 {
23258 /* Set up the list of local specializations. */
23259 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23260 tree block = NULL_TREE;
23261
23262 /* Set up context. */
23263 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23264 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23265 block = push_stmt_list ();
23266 else
23267 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23268
23269 /* Some typedefs referenced from within the template code need to be
23270 access checked at template instantiation time, i.e now. These
23271 types were added to the template at parsing time. Let's get those
23272 and perform the access checks then. */
23273 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23274 args);
23275
23276 /* Create substitution entries for the parameters. */
23277 register_parameter_specializations (code_pattern, d);
23278
23279 /* Substitute into the body of the function. */
23280 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23281 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23282 tf_warning_or_error, tmpl);
23283 else
23284 {
23285 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23286 tf_warning_or_error, tmpl,
23287 /*integral_constant_expression_p=*/false);
23288
23289 /* Set the current input_location to the end of the function
23290 so that finish_function knows where we are. */
23291 input_location
23292 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23293
23294 /* Remember if we saw an infinite loop in the template. */
23295 current_function_infinite_loop
23296 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23297 }
23298
23299 /* Finish the function. */
23300 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23301 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23302 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23303 else
23304 {
23305 d = finish_function (/*inline_p=*/false);
23306 expand_or_defer_fn (d);
23307 }
23308
23309 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23310 cp_check_omp_declare_reduction (d);
23311 }
23312
23313 /* We're not deferring instantiation any more. */
23314 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23315
23316 if (push_to_top)
23317 pop_from_top_level ();
23318 else
23319 pop_function_context ();
23320
23321 if (nested)
23322 restore_omp_privatization_clauses (omp_privatization_save);
23323
23324 out:
23325 pop_deferring_access_checks ();
23326 timevar_pop (TV_TEMPLATE_INST);
23327 pop_tinst_level ();
23328 input_location = saved_loc;
23329 cp_unevaluated_operand = saved_unevaluated_operand;
23330 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23331
23332 return d;
23333 }
23334
23335 /* Run through the list of templates that we wish we could
23336 instantiate, and instantiate any we can. RETRIES is the
23337 number of times we retry pending template instantiation. */
23338
23339 void
23340 instantiate_pending_templates (int retries)
23341 {
23342 int reconsider;
23343 location_t saved_loc = input_location;
23344
23345 /* Instantiating templates may trigger vtable generation. This in turn
23346 may require further template instantiations. We place a limit here
23347 to avoid infinite loop. */
23348 if (pending_templates && retries >= max_tinst_depth)
23349 {
23350 tree decl = pending_templates->tinst->decl;
23351
23352 fatal_error (input_location,
23353 "template instantiation depth exceeds maximum of %d"
23354 " instantiating %q+D, possibly from virtual table generation"
23355 " (use -ftemplate-depth= to increase the maximum)",
23356 max_tinst_depth, decl);
23357 if (TREE_CODE (decl) == FUNCTION_DECL)
23358 /* Pretend that we defined it. */
23359 DECL_INITIAL (decl) = error_mark_node;
23360 return;
23361 }
23362
23363 do
23364 {
23365 struct pending_template **t = &pending_templates;
23366 struct pending_template *last = NULL;
23367 reconsider = 0;
23368 while (*t)
23369 {
23370 tree instantiation = reopen_tinst_level ((*t)->tinst);
23371 bool complete = false;
23372
23373 if (TYPE_P (instantiation))
23374 {
23375 if (!COMPLETE_TYPE_P (instantiation))
23376 {
23377 instantiate_class_template (instantiation);
23378 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23379 for (tree fld = TYPE_FIELDS (instantiation);
23380 fld; fld = TREE_CHAIN (fld))
23381 if ((VAR_P (fld)
23382 || (TREE_CODE (fld) == FUNCTION_DECL
23383 && !DECL_ARTIFICIAL (fld)))
23384 && DECL_TEMPLATE_INSTANTIATION (fld))
23385 instantiate_decl (fld,
23386 /*defer_ok=*/false,
23387 /*expl_inst_class_mem_p=*/false);
23388
23389 if (COMPLETE_TYPE_P (instantiation))
23390 reconsider = 1;
23391 }
23392
23393 complete = COMPLETE_TYPE_P (instantiation);
23394 }
23395 else
23396 {
23397 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23398 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23399 {
23400 instantiation
23401 = instantiate_decl (instantiation,
23402 /*defer_ok=*/false,
23403 /*expl_inst_class_mem_p=*/false);
23404 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23405 reconsider = 1;
23406 }
23407
23408 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23409 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23410 }
23411
23412 if (complete)
23413 /* If INSTANTIATION has been instantiated, then we don't
23414 need to consider it again in the future. */
23415 *t = (*t)->next;
23416 else
23417 {
23418 last = *t;
23419 t = &(*t)->next;
23420 }
23421 tinst_depth = 0;
23422 current_tinst_level = NULL;
23423 }
23424 last_pending_template = last;
23425 }
23426 while (reconsider);
23427
23428 input_location = saved_loc;
23429 }
23430
23431 /* Substitute ARGVEC into T, which is a list of initializers for
23432 either base class or a non-static data member. The TREE_PURPOSEs
23433 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23434 instantiate_decl. */
23435
23436 static tree
23437 tsubst_initializer_list (tree t, tree argvec)
23438 {
23439 tree inits = NULL_TREE;
23440
23441 for (; t; t = TREE_CHAIN (t))
23442 {
23443 tree decl;
23444 tree init;
23445 tree expanded_bases = NULL_TREE;
23446 tree expanded_arguments = NULL_TREE;
23447 int i, len = 1;
23448
23449 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23450 {
23451 tree expr;
23452 tree arg;
23453
23454 /* Expand the base class expansion type into separate base
23455 classes. */
23456 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23457 tf_warning_or_error,
23458 NULL_TREE);
23459 if (expanded_bases == error_mark_node)
23460 continue;
23461
23462 /* We'll be building separate TREE_LISTs of arguments for
23463 each base. */
23464 len = TREE_VEC_LENGTH (expanded_bases);
23465 expanded_arguments = make_tree_vec (len);
23466 for (i = 0; i < len; i++)
23467 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23468
23469 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23470 expand each argument in the TREE_VALUE of t. */
23471 expr = make_node (EXPR_PACK_EXPANSION);
23472 PACK_EXPANSION_LOCAL_P (expr) = true;
23473 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23474 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23475
23476 if (TREE_VALUE (t) == void_type_node)
23477 /* VOID_TYPE_NODE is used to indicate
23478 value-initialization. */
23479 {
23480 for (i = 0; i < len; i++)
23481 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23482 }
23483 else
23484 {
23485 /* Substitute parameter packs into each argument in the
23486 TREE_LIST. */
23487 in_base_initializer = 1;
23488 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23489 {
23490 tree expanded_exprs;
23491
23492 /* Expand the argument. */
23493 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23494 expanded_exprs
23495 = tsubst_pack_expansion (expr, argvec,
23496 tf_warning_or_error,
23497 NULL_TREE);
23498 if (expanded_exprs == error_mark_node)
23499 continue;
23500
23501 /* Prepend each of the expanded expressions to the
23502 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23503 for (i = 0; i < len; i++)
23504 {
23505 TREE_VEC_ELT (expanded_arguments, i) =
23506 tree_cons (NULL_TREE,
23507 TREE_VEC_ELT (expanded_exprs, i),
23508 TREE_VEC_ELT (expanded_arguments, i));
23509 }
23510 }
23511 in_base_initializer = 0;
23512
23513 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23514 since we built them backwards. */
23515 for (i = 0; i < len; i++)
23516 {
23517 TREE_VEC_ELT (expanded_arguments, i) =
23518 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23519 }
23520 }
23521 }
23522
23523 for (i = 0; i < len; ++i)
23524 {
23525 if (expanded_bases)
23526 {
23527 decl = TREE_VEC_ELT (expanded_bases, i);
23528 decl = expand_member_init (decl);
23529 init = TREE_VEC_ELT (expanded_arguments, i);
23530 }
23531 else
23532 {
23533 tree tmp;
23534 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23535 tf_warning_or_error, NULL_TREE);
23536
23537 decl = expand_member_init (decl);
23538 if (decl && !DECL_P (decl))
23539 in_base_initializer = 1;
23540
23541 init = TREE_VALUE (t);
23542 tmp = init;
23543 if (init != void_type_node)
23544 init = tsubst_expr (init, argvec,
23545 tf_warning_or_error, NULL_TREE,
23546 /*integral_constant_expression_p=*/false);
23547 if (init == NULL_TREE && tmp != NULL_TREE)
23548 /* If we had an initializer but it instantiated to nothing,
23549 value-initialize the object. This will only occur when
23550 the initializer was a pack expansion where the parameter
23551 packs used in that expansion were of length zero. */
23552 init = void_type_node;
23553 in_base_initializer = 0;
23554 }
23555
23556 if (decl)
23557 {
23558 init = build_tree_list (decl, init);
23559 TREE_CHAIN (init) = inits;
23560 inits = init;
23561 }
23562 }
23563 }
23564 return inits;
23565 }
23566
23567 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23568
23569 static void
23570 set_current_access_from_decl (tree decl)
23571 {
23572 if (TREE_PRIVATE (decl))
23573 current_access_specifier = access_private_node;
23574 else if (TREE_PROTECTED (decl))
23575 current_access_specifier = access_protected_node;
23576 else
23577 current_access_specifier = access_public_node;
23578 }
23579
23580 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23581 is the instantiation (which should have been created with
23582 start_enum) and ARGS are the template arguments to use. */
23583
23584 static void
23585 tsubst_enum (tree tag, tree newtag, tree args)
23586 {
23587 tree e;
23588
23589 if (SCOPED_ENUM_P (newtag))
23590 begin_scope (sk_scoped_enum, newtag);
23591
23592 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23593 {
23594 tree value;
23595 tree decl;
23596
23597 decl = TREE_VALUE (e);
23598 /* Note that in a template enum, the TREE_VALUE is the
23599 CONST_DECL, not the corresponding INTEGER_CST. */
23600 value = tsubst_expr (DECL_INITIAL (decl),
23601 args, tf_warning_or_error, NULL_TREE,
23602 /*integral_constant_expression_p=*/true);
23603
23604 /* Give this enumeration constant the correct access. */
23605 set_current_access_from_decl (decl);
23606
23607 /* Actually build the enumerator itself. Here we're assuming that
23608 enumerators can't have dependent attributes. */
23609 build_enumerator (DECL_NAME (decl), value, newtag,
23610 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23611 }
23612
23613 if (SCOPED_ENUM_P (newtag))
23614 finish_scope ();
23615
23616 finish_enum_value_list (newtag);
23617 finish_enum (newtag);
23618
23619 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23620 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23621 }
23622
23623 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23624 its type -- but without substituting the innermost set of template
23625 arguments. So, innermost set of template parameters will appear in
23626 the type. */
23627
23628 tree
23629 get_mostly_instantiated_function_type (tree decl)
23630 {
23631 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23632 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23633 }
23634
23635 /* Return truthvalue if we're processing a template different from
23636 the last one involved in diagnostics. */
23637 bool
23638 problematic_instantiation_changed (void)
23639 {
23640 return current_tinst_level != last_error_tinst_level;
23641 }
23642
23643 /* Remember current template involved in diagnostics. */
23644 void
23645 record_last_problematic_instantiation (void)
23646 {
23647 last_error_tinst_level = current_tinst_level;
23648 }
23649
23650 struct tinst_level *
23651 current_instantiation (void)
23652 {
23653 return current_tinst_level;
23654 }
23655
23656 /* Return TRUE if current_function_decl is being instantiated, false
23657 otherwise. */
23658
23659 bool
23660 instantiating_current_function_p (void)
23661 {
23662 return (current_instantiation ()
23663 && current_instantiation ()->decl == current_function_decl);
23664 }
23665
23666 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23667 type. Return false for ok, true for disallowed. Issue error and
23668 inform messages under control of COMPLAIN. */
23669
23670 static bool
23671 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23672 {
23673 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23674 return false;
23675 else if (POINTER_TYPE_P (type))
23676 return false;
23677 else if (TYPE_PTRMEM_P (type))
23678 return false;
23679 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23680 return false;
23681 else if (TREE_CODE (type) == TYPENAME_TYPE)
23682 return false;
23683 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23684 return false;
23685 else if (TREE_CODE (type) == NULLPTR_TYPE)
23686 return false;
23687 /* A bound template template parm could later be instantiated to have a valid
23688 nontype parm type via an alias template. */
23689 else if (cxx_dialect >= cxx11
23690 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23691 return false;
23692
23693 if (complain & tf_error)
23694 {
23695 if (type == error_mark_node)
23696 inform (input_location, "invalid template non-type parameter");
23697 else
23698 error ("%q#T is not a valid type for a template non-type parameter",
23699 type);
23700 }
23701 return true;
23702 }
23703
23704 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23705 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23706
23707 static bool
23708 dependent_type_p_r (tree type)
23709 {
23710 tree scope;
23711
23712 /* [temp.dep.type]
23713
23714 A type is dependent if it is:
23715
23716 -- a template parameter. Template template parameters are types
23717 for us (since TYPE_P holds true for them) so we handle
23718 them here. */
23719 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23720 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23721 return true;
23722 /* -- a qualified-id with a nested-name-specifier which contains a
23723 class-name that names a dependent type or whose unqualified-id
23724 names a dependent type. */
23725 if (TREE_CODE (type) == TYPENAME_TYPE)
23726 return true;
23727
23728 /* An alias template specialization can be dependent even if the
23729 resulting type is not. */
23730 if (dependent_alias_template_spec_p (type))
23731 return true;
23732
23733 /* -- a cv-qualified type where the cv-unqualified type is
23734 dependent.
23735 No code is necessary for this bullet; the code below handles
23736 cv-qualified types, and we don't want to strip aliases with
23737 TYPE_MAIN_VARIANT because of DR 1558. */
23738 /* -- a compound type constructed from any dependent type. */
23739 if (TYPE_PTRMEM_P (type))
23740 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23741 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23742 (type)));
23743 else if (TYPE_PTR_P (type)
23744 || TREE_CODE (type) == REFERENCE_TYPE)
23745 return dependent_type_p (TREE_TYPE (type));
23746 else if (TREE_CODE (type) == FUNCTION_TYPE
23747 || TREE_CODE (type) == METHOD_TYPE)
23748 {
23749 tree arg_type;
23750
23751 if (dependent_type_p (TREE_TYPE (type)))
23752 return true;
23753 for (arg_type = TYPE_ARG_TYPES (type);
23754 arg_type;
23755 arg_type = TREE_CHAIN (arg_type))
23756 if (dependent_type_p (TREE_VALUE (arg_type)))
23757 return true;
23758 if (cxx_dialect >= cxx17)
23759 /* A value-dependent noexcept-specifier makes the type dependent. */
23760 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
23761 if (tree noex = TREE_PURPOSE (spec))
23762 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
23763 affect overload resolution and treating it as dependent breaks
23764 things. */
23765 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
23766 && value_dependent_expression_p (noex))
23767 return true;
23768 return false;
23769 }
23770 /* -- an array type constructed from any dependent type or whose
23771 size is specified by a constant expression that is
23772 value-dependent.
23773
23774 We checked for type- and value-dependence of the bounds in
23775 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23776 if (TREE_CODE (type) == ARRAY_TYPE)
23777 {
23778 if (TYPE_DOMAIN (type)
23779 && dependent_type_p (TYPE_DOMAIN (type)))
23780 return true;
23781 return dependent_type_p (TREE_TYPE (type));
23782 }
23783
23784 /* -- a template-id in which either the template name is a template
23785 parameter ... */
23786 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23787 return true;
23788 /* ... or any of the template arguments is a dependent type or
23789 an expression that is type-dependent or value-dependent. */
23790 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23791 && (any_dependent_template_arguments_p
23792 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23793 return true;
23794
23795 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23796 dependent; if the argument of the `typeof' expression is not
23797 type-dependent, then it should already been have resolved. */
23798 if (TREE_CODE (type) == TYPEOF_TYPE
23799 || TREE_CODE (type) == DECLTYPE_TYPE
23800 || TREE_CODE (type) == UNDERLYING_TYPE)
23801 return true;
23802
23803 /* A template argument pack is dependent if any of its packed
23804 arguments are. */
23805 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23806 {
23807 tree args = ARGUMENT_PACK_ARGS (type);
23808 int i, len = TREE_VEC_LENGTH (args);
23809 for (i = 0; i < len; ++i)
23810 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23811 return true;
23812 }
23813
23814 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23815 be template parameters. */
23816 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23817 return true;
23818
23819 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23820 return true;
23821
23822 /* The standard does not specifically mention types that are local
23823 to template functions or local classes, but they should be
23824 considered dependent too. For example:
23825
23826 template <int I> void f() {
23827 enum E { a = I };
23828 S<sizeof (E)> s;
23829 }
23830
23831 The size of `E' cannot be known until the value of `I' has been
23832 determined. Therefore, `E' must be considered dependent. */
23833 scope = TYPE_CONTEXT (type);
23834 if (scope && TYPE_P (scope))
23835 return dependent_type_p (scope);
23836 /* Don't use type_dependent_expression_p here, as it can lead
23837 to infinite recursion trying to determine whether a lambda
23838 nested in a lambda is dependent (c++/47687). */
23839 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23840 && DECL_LANG_SPECIFIC (scope)
23841 && DECL_TEMPLATE_INFO (scope)
23842 && (any_dependent_template_arguments_p
23843 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23844 return true;
23845
23846 /* Other types are non-dependent. */
23847 return false;
23848 }
23849
23850 /* Returns TRUE if TYPE is dependent, in the sense of
23851 [temp.dep.type]. Note that a NULL type is considered dependent. */
23852
23853 bool
23854 dependent_type_p (tree type)
23855 {
23856 /* If there are no template parameters in scope, then there can't be
23857 any dependent types. */
23858 if (!processing_template_decl)
23859 {
23860 /* If we are not processing a template, then nobody should be
23861 providing us with a dependent type. */
23862 gcc_assert (type);
23863 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23864 return false;
23865 }
23866
23867 /* If the type is NULL, we have not computed a type for the entity
23868 in question; in that case, the type is dependent. */
23869 if (!type)
23870 return true;
23871
23872 /* Erroneous types can be considered non-dependent. */
23873 if (type == error_mark_node)
23874 return false;
23875
23876 /* Getting here with global_type_node means we improperly called this
23877 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23878 gcc_checking_assert (type != global_type_node);
23879
23880 /* If we have not already computed the appropriate value for TYPE,
23881 do so now. */
23882 if (!TYPE_DEPENDENT_P_VALID (type))
23883 {
23884 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23885 TYPE_DEPENDENT_P_VALID (type) = 1;
23886 }
23887
23888 return TYPE_DEPENDENT_P (type);
23889 }
23890
23891 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23892 lookup. In other words, a dependent type that is not the current
23893 instantiation. */
23894
23895 bool
23896 dependent_scope_p (tree scope)
23897 {
23898 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23899 && !currently_open_class (scope));
23900 }
23901
23902 /* T is a SCOPE_REF; return whether we need to consider it
23903 instantiation-dependent so that we can check access at instantiation
23904 time even though we know which member it resolves to. */
23905
23906 static bool
23907 instantiation_dependent_scope_ref_p (tree t)
23908 {
23909 if (DECL_P (TREE_OPERAND (t, 1))
23910 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23911 && accessible_in_template_p (TREE_OPERAND (t, 0),
23912 TREE_OPERAND (t, 1)))
23913 return false;
23914 else
23915 return true;
23916 }
23917
23918 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23919 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23920 expression. */
23921
23922 /* Note that this predicate is not appropriate for general expressions;
23923 only constant expressions (that satisfy potential_constant_expression)
23924 can be tested for value dependence. */
23925
23926 bool
23927 value_dependent_expression_p (tree expression)
23928 {
23929 if (!processing_template_decl || expression == NULL_TREE)
23930 return false;
23931
23932 /* A type-dependent expression is also value-dependent. */
23933 if (type_dependent_expression_p (expression))
23934 return true;
23935
23936 switch (TREE_CODE (expression))
23937 {
23938 case BASELINK:
23939 /* A dependent member function of the current instantiation. */
23940 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23941
23942 case FUNCTION_DECL:
23943 /* A dependent member function of the current instantiation. */
23944 if (DECL_CLASS_SCOPE_P (expression)
23945 && dependent_type_p (DECL_CONTEXT (expression)))
23946 return true;
23947 break;
23948
23949 case IDENTIFIER_NODE:
23950 /* A name that has not been looked up -- must be dependent. */
23951 return true;
23952
23953 case TEMPLATE_PARM_INDEX:
23954 /* A non-type template parm. */
23955 return true;
23956
23957 case CONST_DECL:
23958 /* A non-type template parm. */
23959 if (DECL_TEMPLATE_PARM_P (expression))
23960 return true;
23961 return value_dependent_expression_p (DECL_INITIAL (expression));
23962
23963 case VAR_DECL:
23964 /* A constant with literal type and is initialized
23965 with an expression that is value-dependent. */
23966 if (DECL_DEPENDENT_INIT_P (expression)
23967 /* FIXME cp_finish_decl doesn't fold reference initializers. */
23968 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
23969 return true;
23970 if (DECL_HAS_VALUE_EXPR_P (expression))
23971 {
23972 tree value_expr = DECL_VALUE_EXPR (expression);
23973 if (value_dependent_expression_p (value_expr))
23974 return true;
23975 }
23976 return false;
23977
23978 case DYNAMIC_CAST_EXPR:
23979 case STATIC_CAST_EXPR:
23980 case CONST_CAST_EXPR:
23981 case REINTERPRET_CAST_EXPR:
23982 case CAST_EXPR:
23983 case IMPLICIT_CONV_EXPR:
23984 /* These expressions are value-dependent if the type to which
23985 the cast occurs is dependent or the expression being casted
23986 is value-dependent. */
23987 {
23988 tree type = TREE_TYPE (expression);
23989
23990 if (dependent_type_p (type))
23991 return true;
23992
23993 /* A functional cast has a list of operands. */
23994 expression = TREE_OPERAND (expression, 0);
23995 if (!expression)
23996 {
23997 /* If there are no operands, it must be an expression such
23998 as "int()". This should not happen for aggregate types
23999 because it would form non-constant expressions. */
24000 gcc_assert (cxx_dialect >= cxx11
24001 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24002
24003 return false;
24004 }
24005
24006 if (TREE_CODE (expression) == TREE_LIST)
24007 return any_value_dependent_elements_p (expression);
24008
24009 return value_dependent_expression_p (expression);
24010 }
24011
24012 case SIZEOF_EXPR:
24013 if (SIZEOF_EXPR_TYPE_P (expression))
24014 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24015 /* FALLTHRU */
24016 case ALIGNOF_EXPR:
24017 case TYPEID_EXPR:
24018 /* A `sizeof' expression is value-dependent if the operand is
24019 type-dependent or is a pack expansion. */
24020 expression = TREE_OPERAND (expression, 0);
24021 if (PACK_EXPANSION_P (expression))
24022 return true;
24023 else if (TYPE_P (expression))
24024 return dependent_type_p (expression);
24025 return instantiation_dependent_uneval_expression_p (expression);
24026
24027 case AT_ENCODE_EXPR:
24028 /* An 'encode' expression is value-dependent if the operand is
24029 type-dependent. */
24030 expression = TREE_OPERAND (expression, 0);
24031 return dependent_type_p (expression);
24032
24033 case NOEXCEPT_EXPR:
24034 expression = TREE_OPERAND (expression, 0);
24035 return instantiation_dependent_uneval_expression_p (expression);
24036
24037 case SCOPE_REF:
24038 /* All instantiation-dependent expressions should also be considered
24039 value-dependent. */
24040 return instantiation_dependent_scope_ref_p (expression);
24041
24042 case COMPONENT_REF:
24043 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24044 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24045
24046 case NONTYPE_ARGUMENT_PACK:
24047 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24048 is value-dependent. */
24049 {
24050 tree values = ARGUMENT_PACK_ARGS (expression);
24051 int i, len = TREE_VEC_LENGTH (values);
24052
24053 for (i = 0; i < len; ++i)
24054 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24055 return true;
24056
24057 return false;
24058 }
24059
24060 case TRAIT_EXPR:
24061 {
24062 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24063
24064 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24065 return true;
24066
24067 if (!type2)
24068 return false;
24069
24070 if (TREE_CODE (type2) != TREE_LIST)
24071 return dependent_type_p (type2);
24072
24073 for (; type2; type2 = TREE_CHAIN (type2))
24074 if (dependent_type_p (TREE_VALUE (type2)))
24075 return true;
24076
24077 return false;
24078 }
24079
24080 case MODOP_EXPR:
24081 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24082 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24083
24084 case ARRAY_REF:
24085 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24086 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24087
24088 case ADDR_EXPR:
24089 {
24090 tree op = TREE_OPERAND (expression, 0);
24091 return (value_dependent_expression_p (op)
24092 || has_value_dependent_address (op));
24093 }
24094
24095 case REQUIRES_EXPR:
24096 /* Treat all requires-expressions as value-dependent so
24097 we don't try to fold them. */
24098 return true;
24099
24100 case TYPE_REQ:
24101 return dependent_type_p (TREE_OPERAND (expression, 0));
24102
24103 case CALL_EXPR:
24104 {
24105 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24106 return true;
24107 tree fn = get_callee_fndecl (expression);
24108 int i, nargs;
24109 nargs = call_expr_nargs (expression);
24110 for (i = 0; i < nargs; ++i)
24111 {
24112 tree op = CALL_EXPR_ARG (expression, i);
24113 /* In a call to a constexpr member function, look through the
24114 implicit ADDR_EXPR on the object argument so that it doesn't
24115 cause the call to be considered value-dependent. We also
24116 look through it in potential_constant_expression. */
24117 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24118 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24119 && TREE_CODE (op) == ADDR_EXPR)
24120 op = TREE_OPERAND (op, 0);
24121 if (value_dependent_expression_p (op))
24122 return true;
24123 }
24124 return false;
24125 }
24126
24127 case TEMPLATE_ID_EXPR:
24128 return variable_concept_p (TREE_OPERAND (expression, 0));
24129
24130 case CONSTRUCTOR:
24131 {
24132 unsigned ix;
24133 tree val;
24134 if (dependent_type_p (TREE_TYPE (expression)))
24135 return true;
24136 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24137 if (value_dependent_expression_p (val))
24138 return true;
24139 return false;
24140 }
24141
24142 case STMT_EXPR:
24143 /* Treat a GNU statement expression as dependent to avoid crashing
24144 under instantiate_non_dependent_expr; it can't be constant. */
24145 return true;
24146
24147 default:
24148 /* A constant expression is value-dependent if any subexpression is
24149 value-dependent. */
24150 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24151 {
24152 case tcc_reference:
24153 case tcc_unary:
24154 case tcc_comparison:
24155 case tcc_binary:
24156 case tcc_expression:
24157 case tcc_vl_exp:
24158 {
24159 int i, len = cp_tree_operand_length (expression);
24160
24161 for (i = 0; i < len; i++)
24162 {
24163 tree t = TREE_OPERAND (expression, i);
24164
24165 /* In some cases, some of the operands may be missing.
24166 (For example, in the case of PREDECREMENT_EXPR, the
24167 amount to increment by may be missing.) That doesn't
24168 make the expression dependent. */
24169 if (t && value_dependent_expression_p (t))
24170 return true;
24171 }
24172 }
24173 break;
24174 default:
24175 break;
24176 }
24177 break;
24178 }
24179
24180 /* The expression is not value-dependent. */
24181 return false;
24182 }
24183
24184 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24185 [temp.dep.expr]. Note that an expression with no type is
24186 considered dependent. Other parts of the compiler arrange for an
24187 expression with type-dependent subexpressions to have no type, so
24188 this function doesn't have to be fully recursive. */
24189
24190 bool
24191 type_dependent_expression_p (tree expression)
24192 {
24193 if (!processing_template_decl)
24194 return false;
24195
24196 if (expression == NULL_TREE || expression == error_mark_node)
24197 return false;
24198
24199 /* An unresolved name is always dependent. */
24200 if (identifier_p (expression)
24201 || TREE_CODE (expression) == USING_DECL
24202 || TREE_CODE (expression) == WILDCARD_DECL)
24203 return true;
24204
24205 /* A fold expression is type-dependent. */
24206 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24207 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24208 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24209 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24210 return true;
24211
24212 /* Some expression forms are never type-dependent. */
24213 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24214 || TREE_CODE (expression) == SIZEOF_EXPR
24215 || TREE_CODE (expression) == ALIGNOF_EXPR
24216 || TREE_CODE (expression) == AT_ENCODE_EXPR
24217 || TREE_CODE (expression) == NOEXCEPT_EXPR
24218 || TREE_CODE (expression) == TRAIT_EXPR
24219 || TREE_CODE (expression) == TYPEID_EXPR
24220 || TREE_CODE (expression) == DELETE_EXPR
24221 || TREE_CODE (expression) == VEC_DELETE_EXPR
24222 || TREE_CODE (expression) == THROW_EXPR
24223 || TREE_CODE (expression) == REQUIRES_EXPR)
24224 return false;
24225
24226 /* The types of these expressions depends only on the type to which
24227 the cast occurs. */
24228 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24229 || TREE_CODE (expression) == STATIC_CAST_EXPR
24230 || TREE_CODE (expression) == CONST_CAST_EXPR
24231 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24232 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24233 || TREE_CODE (expression) == CAST_EXPR)
24234 return dependent_type_p (TREE_TYPE (expression));
24235
24236 /* The types of these expressions depends only on the type created
24237 by the expression. */
24238 if (TREE_CODE (expression) == NEW_EXPR
24239 || TREE_CODE (expression) == VEC_NEW_EXPR)
24240 {
24241 /* For NEW_EXPR tree nodes created inside a template, either
24242 the object type itself or a TREE_LIST may appear as the
24243 operand 1. */
24244 tree type = TREE_OPERAND (expression, 1);
24245 if (TREE_CODE (type) == TREE_LIST)
24246 /* This is an array type. We need to check array dimensions
24247 as well. */
24248 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24249 || value_dependent_expression_p
24250 (TREE_OPERAND (TREE_VALUE (type), 1));
24251 else
24252 return dependent_type_p (type);
24253 }
24254
24255 if (TREE_CODE (expression) == SCOPE_REF)
24256 {
24257 tree scope = TREE_OPERAND (expression, 0);
24258 tree name = TREE_OPERAND (expression, 1);
24259
24260 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24261 contains an identifier associated by name lookup with one or more
24262 declarations declared with a dependent type, or...a
24263 nested-name-specifier or qualified-id that names a member of an
24264 unknown specialization. */
24265 return (type_dependent_expression_p (name)
24266 || dependent_scope_p (scope));
24267 }
24268
24269 if (TREE_CODE (expression) == TEMPLATE_DECL
24270 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24271 return uses_outer_template_parms (expression);
24272
24273 if (TREE_CODE (expression) == STMT_EXPR)
24274 expression = stmt_expr_value_expr (expression);
24275
24276 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24277 {
24278 tree elt;
24279 unsigned i;
24280
24281 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24282 {
24283 if (type_dependent_expression_p (elt))
24284 return true;
24285 }
24286 return false;
24287 }
24288
24289 /* A static data member of the current instantiation with incomplete
24290 array type is type-dependent, as the definition and specializations
24291 can have different bounds. */
24292 if (VAR_P (expression)
24293 && DECL_CLASS_SCOPE_P (expression)
24294 && dependent_type_p (DECL_CONTEXT (expression))
24295 && VAR_HAD_UNKNOWN_BOUND (expression))
24296 return true;
24297
24298 /* An array of unknown bound depending on a variadic parameter, eg:
24299
24300 template<typename... Args>
24301 void foo (Args... args)
24302 {
24303 int arr[] = { args... };
24304 }
24305
24306 template<int... vals>
24307 void bar ()
24308 {
24309 int arr[] = { vals... };
24310 }
24311
24312 If the array has no length and has an initializer, it must be that
24313 we couldn't determine its length in cp_complete_array_type because
24314 it is dependent. */
24315 if (VAR_P (expression)
24316 && TREE_TYPE (expression) != NULL_TREE
24317 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24318 && !TYPE_DOMAIN (TREE_TYPE (expression))
24319 && DECL_INITIAL (expression))
24320 return true;
24321
24322 /* A function or variable template-id is type-dependent if it has any
24323 dependent template arguments. */
24324 if (VAR_OR_FUNCTION_DECL_P (expression)
24325 && DECL_LANG_SPECIFIC (expression)
24326 && DECL_TEMPLATE_INFO (expression))
24327 {
24328 /* Consider the innermost template arguments, since those are the ones
24329 that come from the template-id; the template arguments for the
24330 enclosing class do not make it type-dependent unless they are used in
24331 the type of the decl. */
24332 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24333 && (any_dependent_template_arguments_p
24334 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24335 return true;
24336 }
24337
24338 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24339 type-dependent. Checking this is important for functions with auto return
24340 type, which looks like a dependent type. */
24341 if (TREE_CODE (expression) == FUNCTION_DECL
24342 && !(DECL_CLASS_SCOPE_P (expression)
24343 && dependent_type_p (DECL_CONTEXT (expression)))
24344 && !(DECL_FRIEND_P (expression)
24345 && (!DECL_FRIEND_CONTEXT (expression)
24346 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24347 && !DECL_LOCAL_FUNCTION_P (expression))
24348 {
24349 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24350 || undeduced_auto_decl (expression));
24351 return false;
24352 }
24353
24354 /* Always dependent, on the number of arguments if nothing else. */
24355 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24356 return true;
24357
24358 if (TREE_TYPE (expression) == unknown_type_node)
24359 {
24360 if (TREE_CODE (expression) == ADDR_EXPR)
24361 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24362 if (TREE_CODE (expression) == COMPONENT_REF
24363 || TREE_CODE (expression) == OFFSET_REF)
24364 {
24365 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24366 return true;
24367 expression = TREE_OPERAND (expression, 1);
24368 if (identifier_p (expression))
24369 return false;
24370 }
24371 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24372 if (TREE_CODE (expression) == SCOPE_REF)
24373 return false;
24374
24375 if (BASELINK_P (expression))
24376 {
24377 if (BASELINK_OPTYPE (expression)
24378 && dependent_type_p (BASELINK_OPTYPE (expression)))
24379 return true;
24380 expression = BASELINK_FUNCTIONS (expression);
24381 }
24382
24383 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24384 {
24385 if (any_dependent_template_arguments_p
24386 (TREE_OPERAND (expression, 1)))
24387 return true;
24388 expression = TREE_OPERAND (expression, 0);
24389 if (identifier_p (expression))
24390 return true;
24391 }
24392
24393 gcc_assert (TREE_CODE (expression) == OVERLOAD
24394 || TREE_CODE (expression) == FUNCTION_DECL);
24395
24396 for (lkp_iterator iter (expression); iter; ++iter)
24397 if (type_dependent_expression_p (*iter))
24398 return true;
24399
24400 return false;
24401 }
24402
24403 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24404
24405 /* Dependent type attributes might not have made it from the decl to
24406 the type yet. */
24407 if (DECL_P (expression)
24408 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24409 return true;
24410
24411 return (dependent_type_p (TREE_TYPE (expression)));
24412 }
24413
24414 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24415 type-dependent if the expression refers to a member of the current
24416 instantiation and the type of the referenced member is dependent, or the
24417 class member access expression refers to a member of an unknown
24418 specialization.
24419
24420 This function returns true if the OBJECT in such a class member access
24421 expression is of an unknown specialization. */
24422
24423 bool
24424 type_dependent_object_expression_p (tree object)
24425 {
24426 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24427 dependent. */
24428 if (TREE_CODE (object) == IDENTIFIER_NODE)
24429 return true;
24430 tree scope = TREE_TYPE (object);
24431 return (!scope || dependent_scope_p (scope));
24432 }
24433
24434 /* walk_tree callback function for instantiation_dependent_expression_p,
24435 below. Returns non-zero if a dependent subexpression is found. */
24436
24437 static tree
24438 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24439 void * /*data*/)
24440 {
24441 if (TYPE_P (*tp))
24442 {
24443 /* We don't have to worry about decltype currently because decltype
24444 of an instantiation-dependent expr is a dependent type. This
24445 might change depending on the resolution of DR 1172. */
24446 *walk_subtrees = false;
24447 return NULL_TREE;
24448 }
24449 enum tree_code code = TREE_CODE (*tp);
24450 switch (code)
24451 {
24452 /* Don't treat an argument list as dependent just because it has no
24453 TREE_TYPE. */
24454 case TREE_LIST:
24455 case TREE_VEC:
24456 return NULL_TREE;
24457
24458 case TEMPLATE_PARM_INDEX:
24459 return *tp;
24460
24461 /* Handle expressions with type operands. */
24462 case SIZEOF_EXPR:
24463 case ALIGNOF_EXPR:
24464 case TYPEID_EXPR:
24465 case AT_ENCODE_EXPR:
24466 {
24467 tree op = TREE_OPERAND (*tp, 0);
24468 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24469 op = TREE_TYPE (op);
24470 if (TYPE_P (op))
24471 {
24472 if (dependent_type_p (op))
24473 return *tp;
24474 else
24475 {
24476 *walk_subtrees = false;
24477 return NULL_TREE;
24478 }
24479 }
24480 break;
24481 }
24482
24483 case COMPONENT_REF:
24484 if (identifier_p (TREE_OPERAND (*tp, 1)))
24485 /* In a template, finish_class_member_access_expr creates a
24486 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24487 type-dependent, so that we can check access control at
24488 instantiation time (PR 42277). See also Core issue 1273. */
24489 return *tp;
24490 break;
24491
24492 case SCOPE_REF:
24493 if (instantiation_dependent_scope_ref_p (*tp))
24494 return *tp;
24495 else
24496 break;
24497
24498 /* Treat statement-expressions as dependent. */
24499 case BIND_EXPR:
24500 return *tp;
24501
24502 /* Treat requires-expressions as dependent. */
24503 case REQUIRES_EXPR:
24504 return *tp;
24505
24506 case CALL_EXPR:
24507 /* Treat calls to function concepts as dependent. */
24508 if (function_concept_check_p (*tp))
24509 return *tp;
24510 break;
24511
24512 case TEMPLATE_ID_EXPR:
24513 /* And variable concepts. */
24514 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24515 return *tp;
24516 break;
24517
24518 default:
24519 break;
24520 }
24521
24522 if (type_dependent_expression_p (*tp))
24523 return *tp;
24524 else
24525 return NULL_TREE;
24526 }
24527
24528 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24529 sense defined by the ABI:
24530
24531 "An expression is instantiation-dependent if it is type-dependent
24532 or value-dependent, or it has a subexpression that is type-dependent
24533 or value-dependent."
24534
24535 Except don't actually check value-dependence for unevaluated expressions,
24536 because in sizeof(i) we don't care about the value of i. Checking
24537 type-dependence will in turn check value-dependence of array bounds/template
24538 arguments as needed. */
24539
24540 bool
24541 instantiation_dependent_uneval_expression_p (tree expression)
24542 {
24543 tree result;
24544
24545 if (!processing_template_decl)
24546 return false;
24547
24548 if (expression == error_mark_node)
24549 return false;
24550
24551 result = cp_walk_tree_without_duplicates (&expression,
24552 instantiation_dependent_r, NULL);
24553 return result != NULL_TREE;
24554 }
24555
24556 /* As above, but also check value-dependence of the expression as a whole. */
24557
24558 bool
24559 instantiation_dependent_expression_p (tree expression)
24560 {
24561 return (instantiation_dependent_uneval_expression_p (expression)
24562 || value_dependent_expression_p (expression));
24563 }
24564
24565 /* Like type_dependent_expression_p, but it also works while not processing
24566 a template definition, i.e. during substitution or mangling. */
24567
24568 bool
24569 type_dependent_expression_p_push (tree expr)
24570 {
24571 bool b;
24572 ++processing_template_decl;
24573 b = type_dependent_expression_p (expr);
24574 --processing_template_decl;
24575 return b;
24576 }
24577
24578 /* Returns TRUE if ARGS contains a type-dependent expression. */
24579
24580 bool
24581 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24582 {
24583 unsigned int i;
24584 tree arg;
24585
24586 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24587 {
24588 if (type_dependent_expression_p (arg))
24589 return true;
24590 }
24591 return false;
24592 }
24593
24594 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24595 expressions) contains any type-dependent expressions. */
24596
24597 bool
24598 any_type_dependent_elements_p (const_tree list)
24599 {
24600 for (; list; list = TREE_CHAIN (list))
24601 if (type_dependent_expression_p (TREE_VALUE (list)))
24602 return true;
24603
24604 return false;
24605 }
24606
24607 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24608 expressions) contains any value-dependent expressions. */
24609
24610 bool
24611 any_value_dependent_elements_p (const_tree list)
24612 {
24613 for (; list; list = TREE_CHAIN (list))
24614 if (value_dependent_expression_p (TREE_VALUE (list)))
24615 return true;
24616
24617 return false;
24618 }
24619
24620 /* Returns TRUE if the ARG (a template argument) is dependent. */
24621
24622 bool
24623 dependent_template_arg_p (tree arg)
24624 {
24625 if (!processing_template_decl)
24626 return false;
24627
24628 /* Assume a template argument that was wrongly written by the user
24629 is dependent. This is consistent with what
24630 any_dependent_template_arguments_p [that calls this function]
24631 does. */
24632 if (!arg || arg == error_mark_node)
24633 return true;
24634
24635 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24636 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24637
24638 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24639 return true;
24640 if (TREE_CODE (arg) == TEMPLATE_DECL)
24641 {
24642 if (DECL_TEMPLATE_PARM_P (arg))
24643 return true;
24644 /* A member template of a dependent class is not necessarily
24645 type-dependent, but it is a dependent template argument because it
24646 will be a member of an unknown specialization to that template. */
24647 tree scope = CP_DECL_CONTEXT (arg);
24648 return TYPE_P (scope) && dependent_type_p (scope);
24649 }
24650 else if (ARGUMENT_PACK_P (arg))
24651 {
24652 tree args = ARGUMENT_PACK_ARGS (arg);
24653 int i, len = TREE_VEC_LENGTH (args);
24654 for (i = 0; i < len; ++i)
24655 {
24656 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24657 return true;
24658 }
24659
24660 return false;
24661 }
24662 else if (TYPE_P (arg))
24663 return dependent_type_p (arg);
24664 else
24665 return (type_dependent_expression_p (arg)
24666 || value_dependent_expression_p (arg));
24667 }
24668
24669 /* Returns true if ARGS (a collection of template arguments) contains
24670 any types that require structural equality testing. */
24671
24672 bool
24673 any_template_arguments_need_structural_equality_p (tree args)
24674 {
24675 int i;
24676 int j;
24677
24678 if (!args)
24679 return false;
24680 if (args == error_mark_node)
24681 return true;
24682
24683 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24684 {
24685 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24686 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24687 {
24688 tree arg = TREE_VEC_ELT (level, j);
24689 tree packed_args = NULL_TREE;
24690 int k, len = 1;
24691
24692 if (ARGUMENT_PACK_P (arg))
24693 {
24694 /* Look inside the argument pack. */
24695 packed_args = ARGUMENT_PACK_ARGS (arg);
24696 len = TREE_VEC_LENGTH (packed_args);
24697 }
24698
24699 for (k = 0; k < len; ++k)
24700 {
24701 if (packed_args)
24702 arg = TREE_VEC_ELT (packed_args, k);
24703
24704 if (error_operand_p (arg))
24705 return true;
24706 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24707 continue;
24708 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24709 return true;
24710 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24711 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24712 return true;
24713 }
24714 }
24715 }
24716
24717 return false;
24718 }
24719
24720 /* Returns true if ARGS (a collection of template arguments) contains
24721 any dependent arguments. */
24722
24723 bool
24724 any_dependent_template_arguments_p (const_tree args)
24725 {
24726 int i;
24727 int j;
24728
24729 if (!args)
24730 return false;
24731 if (args == error_mark_node)
24732 return true;
24733
24734 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24735 {
24736 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24737 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24738 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24739 return true;
24740 }
24741
24742 return false;
24743 }
24744
24745 /* Returns TRUE if the template TMPL is type-dependent. */
24746
24747 bool
24748 dependent_template_p (tree tmpl)
24749 {
24750 if (TREE_CODE (tmpl) == OVERLOAD)
24751 {
24752 for (lkp_iterator iter (tmpl); iter; ++iter)
24753 if (dependent_template_p (*iter))
24754 return true;
24755 return false;
24756 }
24757
24758 /* Template template parameters are dependent. */
24759 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24760 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24761 return true;
24762 /* So are names that have not been looked up. */
24763 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24764 return true;
24765 return false;
24766 }
24767
24768 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24769
24770 bool
24771 dependent_template_id_p (tree tmpl, tree args)
24772 {
24773 return (dependent_template_p (tmpl)
24774 || any_dependent_template_arguments_p (args));
24775 }
24776
24777 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24778 are dependent. */
24779
24780 bool
24781 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24782 {
24783 int i;
24784
24785 if (!processing_template_decl)
24786 return false;
24787
24788 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24789 {
24790 tree decl = TREE_VEC_ELT (declv, i);
24791 tree init = TREE_VEC_ELT (initv, i);
24792 tree cond = TREE_VEC_ELT (condv, i);
24793 tree incr = TREE_VEC_ELT (incrv, i);
24794
24795 if (type_dependent_expression_p (decl)
24796 || TREE_CODE (decl) == SCOPE_REF)
24797 return true;
24798
24799 if (init && type_dependent_expression_p (init))
24800 return true;
24801
24802 if (type_dependent_expression_p (cond))
24803 return true;
24804
24805 if (COMPARISON_CLASS_P (cond)
24806 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24807 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24808 return true;
24809
24810 if (TREE_CODE (incr) == MODOP_EXPR)
24811 {
24812 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24813 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24814 return true;
24815 }
24816 else if (type_dependent_expression_p (incr))
24817 return true;
24818 else if (TREE_CODE (incr) == MODIFY_EXPR)
24819 {
24820 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24821 return true;
24822 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24823 {
24824 tree t = TREE_OPERAND (incr, 1);
24825 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24826 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24827 return true;
24828 }
24829 }
24830 }
24831
24832 return false;
24833 }
24834
24835 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24836 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24837 no such TYPE can be found. Note that this function peers inside
24838 uninstantiated templates and therefore should be used only in
24839 extremely limited situations. ONLY_CURRENT_P restricts this
24840 peering to the currently open classes hierarchy (which is required
24841 when comparing types). */
24842
24843 tree
24844 resolve_typename_type (tree type, bool only_current_p)
24845 {
24846 tree scope;
24847 tree name;
24848 tree decl;
24849 int quals;
24850 tree pushed_scope;
24851 tree result;
24852
24853 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24854
24855 scope = TYPE_CONTEXT (type);
24856 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24857 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24858 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24859 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24860 identifier of the TYPENAME_TYPE anymore.
24861 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24862 TYPENAME_TYPE instead, we avoid messing up with a possible
24863 typedef variant case. */
24864 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24865
24866 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24867 it first before we can figure out what NAME refers to. */
24868 if (TREE_CODE (scope) == TYPENAME_TYPE)
24869 {
24870 if (TYPENAME_IS_RESOLVING_P (scope))
24871 /* Given a class template A with a dependent base with nested type C,
24872 typedef typename A::C::C C will land us here, as trying to resolve
24873 the initial A::C leads to the local C typedef, which leads back to
24874 A::C::C. So we break the recursion now. */
24875 return type;
24876 else
24877 scope = resolve_typename_type (scope, only_current_p);
24878 }
24879 /* If we don't know what SCOPE refers to, then we cannot resolve the
24880 TYPENAME_TYPE. */
24881 if (!CLASS_TYPE_P (scope))
24882 return type;
24883 /* If this is a typedef, we don't want to look inside (c++/11987). */
24884 if (typedef_variant_p (type))
24885 return type;
24886 /* If SCOPE isn't the template itself, it will not have a valid
24887 TYPE_FIELDS list. */
24888 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24889 /* scope is either the template itself or a compatible instantiation
24890 like X<T>, so look up the name in the original template. */
24891 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24892 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24893 gcc_checking_assert (uses_template_parms (scope));
24894 /* If scope has no fields, it can't be a current instantiation. Check this
24895 before currently_open_class to avoid infinite recursion (71515). */
24896 if (!TYPE_FIELDS (scope))
24897 return type;
24898 /* If the SCOPE is not the current instantiation, there's no reason
24899 to look inside it. */
24900 if (only_current_p && !currently_open_class (scope))
24901 return type;
24902 /* Enter the SCOPE so that name lookup will be resolved as if we
24903 were in the class definition. In particular, SCOPE will no
24904 longer be considered a dependent type. */
24905 pushed_scope = push_scope (scope);
24906 /* Look up the declaration. */
24907 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24908 tf_warning_or_error);
24909
24910 result = NULL_TREE;
24911
24912 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24913 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24914 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24915 if (!decl)
24916 /*nop*/;
24917 else if (identifier_p (fullname)
24918 && TREE_CODE (decl) == TYPE_DECL)
24919 {
24920 result = TREE_TYPE (decl);
24921 if (result == error_mark_node)
24922 result = NULL_TREE;
24923 }
24924 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24925 && DECL_CLASS_TEMPLATE_P (decl))
24926 {
24927 /* Obtain the template and the arguments. */
24928 tree tmpl = TREE_OPERAND (fullname, 0);
24929 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24930 {
24931 /* We get here with a plain identifier because a previous tentative
24932 parse of the nested-name-specifier as part of a ptr-operator saw
24933 ::template X<A>. The use of ::template is necessary in a
24934 ptr-operator, but wrong in a declarator-id.
24935
24936 [temp.names]: In a qualified-id of a declarator-id, the keyword
24937 template shall not appear at the top level. */
24938 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24939 "keyword %<template%> not allowed in declarator-id");
24940 tmpl = decl;
24941 }
24942 tree args = TREE_OPERAND (fullname, 1);
24943 /* Instantiate the template. */
24944 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24945 /*entering_scope=*/true,
24946 tf_error | tf_user);
24947 if (result == error_mark_node)
24948 result = NULL_TREE;
24949 }
24950
24951 /* Leave the SCOPE. */
24952 if (pushed_scope)
24953 pop_scope (pushed_scope);
24954
24955 /* If we failed to resolve it, return the original typename. */
24956 if (!result)
24957 return type;
24958
24959 /* If lookup found a typename type, resolve that too. */
24960 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24961 {
24962 /* Ill-formed programs can cause infinite recursion here, so we
24963 must catch that. */
24964 TYPENAME_IS_RESOLVING_P (result) = 1;
24965 result = resolve_typename_type (result, only_current_p);
24966 TYPENAME_IS_RESOLVING_P (result) = 0;
24967 }
24968
24969 /* Qualify the resulting type. */
24970 quals = cp_type_quals (type);
24971 if (quals)
24972 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24973
24974 return result;
24975 }
24976
24977 /* EXPR is an expression which is not type-dependent. Return a proxy
24978 for EXPR that can be used to compute the types of larger
24979 expressions containing EXPR. */
24980
24981 tree
24982 build_non_dependent_expr (tree expr)
24983 {
24984 tree inner_expr;
24985
24986 /* When checking, try to get a constant value for all non-dependent
24987 expressions in order to expose bugs in *_dependent_expression_p
24988 and constexpr. This can affect code generation, see PR70704, so
24989 only do this for -fchecking=2. */
24990 if (flag_checking > 1
24991 && cxx_dialect >= cxx11
24992 /* Don't do this during nsdmi parsing as it can lead to
24993 unexpected recursive instantiations. */
24994 && !parsing_nsdmi ()
24995 /* Don't do this during concept expansion either and for
24996 the same reason. */
24997 && !expanding_concept ())
24998 fold_non_dependent_expr (expr);
24999
25000 /* Preserve OVERLOADs; the functions must be available to resolve
25001 types. */
25002 inner_expr = expr;
25003 if (TREE_CODE (inner_expr) == STMT_EXPR)
25004 inner_expr = stmt_expr_value_expr (inner_expr);
25005 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25006 inner_expr = TREE_OPERAND (inner_expr, 0);
25007 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25008 inner_expr = TREE_OPERAND (inner_expr, 1);
25009 if (is_overloaded_fn (inner_expr)
25010 || TREE_CODE (inner_expr) == OFFSET_REF)
25011 return expr;
25012 /* There is no need to return a proxy for a variable. */
25013 if (VAR_P (expr))
25014 return expr;
25015 /* Preserve string constants; conversions from string constants to
25016 "char *" are allowed, even though normally a "const char *"
25017 cannot be used to initialize a "char *". */
25018 if (TREE_CODE (expr) == STRING_CST)
25019 return expr;
25020 /* Preserve void and arithmetic constants, as an optimization -- there is no
25021 reason to create a new node. */
25022 if (TREE_CODE (expr) == VOID_CST
25023 || TREE_CODE (expr) == INTEGER_CST
25024 || TREE_CODE (expr) == REAL_CST)
25025 return expr;
25026 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25027 There is at least one place where we want to know that a
25028 particular expression is a throw-expression: when checking a ?:
25029 expression, there are special rules if the second or third
25030 argument is a throw-expression. */
25031 if (TREE_CODE (expr) == THROW_EXPR)
25032 return expr;
25033
25034 /* Don't wrap an initializer list, we need to be able to look inside. */
25035 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25036 return expr;
25037
25038 /* Don't wrap a dummy object, we need to be able to test for it. */
25039 if (is_dummy_object (expr))
25040 return expr;
25041
25042 if (TREE_CODE (expr) == COND_EXPR)
25043 return build3 (COND_EXPR,
25044 TREE_TYPE (expr),
25045 TREE_OPERAND (expr, 0),
25046 (TREE_OPERAND (expr, 1)
25047 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25048 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25049 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25050 if (TREE_CODE (expr) == COMPOUND_EXPR
25051 && !COMPOUND_EXPR_OVERLOADED (expr))
25052 return build2 (COMPOUND_EXPR,
25053 TREE_TYPE (expr),
25054 TREE_OPERAND (expr, 0),
25055 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25056
25057 /* If the type is unknown, it can't really be non-dependent */
25058 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25059
25060 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25061 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25062 }
25063
25064 /* ARGS is a vector of expressions as arguments to a function call.
25065 Replace the arguments with equivalent non-dependent expressions.
25066 This modifies ARGS in place. */
25067
25068 void
25069 make_args_non_dependent (vec<tree, va_gc> *args)
25070 {
25071 unsigned int ix;
25072 tree arg;
25073
25074 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25075 {
25076 tree newarg = build_non_dependent_expr (arg);
25077 if (newarg != arg)
25078 (*args)[ix] = newarg;
25079 }
25080 }
25081
25082 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25083 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25084 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25085
25086 static tree
25087 make_auto_1 (tree name, bool set_canonical)
25088 {
25089 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25090 TYPE_NAME (au) = build_decl (input_location,
25091 TYPE_DECL, name, au);
25092 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25093 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25094 (0, processing_template_decl + 1, processing_template_decl + 1,
25095 TYPE_NAME (au), NULL_TREE);
25096 if (set_canonical)
25097 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25098 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25099 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25100
25101 return au;
25102 }
25103
25104 tree
25105 make_decltype_auto (void)
25106 {
25107 return make_auto_1 (decltype_auto_identifier, true);
25108 }
25109
25110 tree
25111 make_auto (void)
25112 {
25113 return make_auto_1 (auto_identifier, true);
25114 }
25115
25116 /* Return a C++17 deduction placeholder for class template TMPL. */
25117
25118 tree
25119 make_template_placeholder (tree tmpl)
25120 {
25121 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25122 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25123 return t;
25124 }
25125
25126 /* True iff T is a C++17 class template deduction placeholder. */
25127
25128 bool
25129 template_placeholder_p (tree t)
25130 {
25131 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25132 }
25133
25134 /* Make a "constrained auto" type-specifier. This is an
25135 auto type with constraints that must be associated after
25136 deduction. The constraint is formed from the given
25137 CONC and its optional sequence of arguments, which are
25138 non-null if written as partial-concept-id. */
25139
25140 tree
25141 make_constrained_auto (tree con, tree args)
25142 {
25143 tree type = make_auto_1 (auto_identifier, false);
25144
25145 /* Build the constraint. */
25146 tree tmpl = DECL_TI_TEMPLATE (con);
25147 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25148 expr = build_concept_check (expr, type, args);
25149
25150 tree constr = normalize_expression (expr);
25151 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25152
25153 /* Our canonical type depends on the constraint. */
25154 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25155
25156 /* Attach the constraint to the type declaration. */
25157 tree decl = TYPE_NAME (type);
25158 return decl;
25159 }
25160
25161 /* Given type ARG, return std::initializer_list<ARG>. */
25162
25163 static tree
25164 listify (tree arg)
25165 {
25166 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25167
25168 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25169 {
25170 gcc_rich_location richloc (input_location);
25171 maybe_add_include_fixit (&richloc, "<initializer_list>");
25172 error_at (&richloc,
25173 "deducing from brace-enclosed initializer list"
25174 " requires %<#include <initializer_list>%>");
25175
25176 return error_mark_node;
25177 }
25178 tree argvec = make_tree_vec (1);
25179 TREE_VEC_ELT (argvec, 0) = arg;
25180
25181 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25182 NULL_TREE, 0, tf_warning_or_error);
25183 }
25184
25185 /* Replace auto in TYPE with std::initializer_list<auto>. */
25186
25187 static tree
25188 listify_autos (tree type, tree auto_node)
25189 {
25190 tree init_auto = listify (auto_node);
25191 tree argvec = make_tree_vec (1);
25192 TREE_VEC_ELT (argvec, 0) = init_auto;
25193 if (processing_template_decl)
25194 argvec = add_to_template_args (current_template_args (), argvec);
25195 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25196 }
25197
25198 /* Hash traits for hashing possibly constrained 'auto'
25199 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25200
25201 struct auto_hash : default_hash_traits<tree>
25202 {
25203 static inline hashval_t hash (tree);
25204 static inline bool equal (tree, tree);
25205 };
25206
25207 /* Hash the 'auto' T. */
25208
25209 inline hashval_t
25210 auto_hash::hash (tree t)
25211 {
25212 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25213 /* Matching constrained-type-specifiers denote the same template
25214 parameter, so hash the constraint. */
25215 return hash_placeholder_constraint (c);
25216 else
25217 /* But unconstrained autos are all separate, so just hash the pointer. */
25218 return iterative_hash_object (t, 0);
25219 }
25220
25221 /* Compare two 'auto's. */
25222
25223 inline bool
25224 auto_hash::equal (tree t1, tree t2)
25225 {
25226 if (t1 == t2)
25227 return true;
25228
25229 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25230 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25231
25232 /* Two unconstrained autos are distinct. */
25233 if (!c1 || !c2)
25234 return false;
25235
25236 return equivalent_placeholder_constraints (c1, c2);
25237 }
25238
25239 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25240 constrained) auto, add it to the vector. */
25241
25242 static int
25243 extract_autos_r (tree t, void *data)
25244 {
25245 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25246 if (is_auto (t))
25247 {
25248 /* All the autos were built with index 0; fix that up now. */
25249 tree *p = hash.find_slot (t, INSERT);
25250 unsigned idx;
25251 if (*p)
25252 /* If this is a repeated constrained-type-specifier, use the index we
25253 chose before. */
25254 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25255 else
25256 {
25257 /* Otherwise this is new, so use the current count. */
25258 *p = t;
25259 idx = hash.elements () - 1;
25260 }
25261 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25262 }
25263
25264 /* Always keep walking. */
25265 return 0;
25266 }
25267
25268 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25269 says they can appear anywhere in the type. */
25270
25271 static tree
25272 extract_autos (tree type)
25273 {
25274 hash_set<tree> visited;
25275 hash_table<auto_hash> hash (2);
25276
25277 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25278
25279 tree tree_vec = make_tree_vec (hash.elements());
25280 for (hash_table<auto_hash>::iterator iter = hash.begin();
25281 iter != hash.end(); ++iter)
25282 {
25283 tree elt = *iter;
25284 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25285 TREE_VEC_ELT (tree_vec, i)
25286 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25287 }
25288
25289 return tree_vec;
25290 }
25291
25292 /* The stem for deduction guide names. */
25293 const char *const dguide_base = "__dguide_";
25294
25295 /* Return the name for a deduction guide for class template TMPL. */
25296
25297 tree
25298 dguide_name (tree tmpl)
25299 {
25300 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25301 tree tname = TYPE_IDENTIFIER (type);
25302 char *buf = (char *) alloca (1 + strlen (dguide_base)
25303 + IDENTIFIER_LENGTH (tname));
25304 memcpy (buf, dguide_base, strlen (dguide_base));
25305 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25306 IDENTIFIER_LENGTH (tname) + 1);
25307 tree dname = get_identifier (buf);
25308 TREE_TYPE (dname) = type;
25309 return dname;
25310 }
25311
25312 /* True if NAME is the name of a deduction guide. */
25313
25314 bool
25315 dguide_name_p (tree name)
25316 {
25317 return (TREE_TYPE (name)
25318 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25319 strlen (dguide_base)));
25320 }
25321
25322 /* True if FN is a deduction guide. */
25323
25324 bool
25325 deduction_guide_p (const_tree fn)
25326 {
25327 if (DECL_P (fn))
25328 if (tree name = DECL_NAME (fn))
25329 return dguide_name_p (name);
25330 return false;
25331 }
25332
25333 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25334
25335 bool
25336 copy_guide_p (const_tree fn)
25337 {
25338 gcc_assert (deduction_guide_p (fn));
25339 if (!DECL_ARTIFICIAL (fn))
25340 return false;
25341 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25342 return (TREE_CHAIN (parms) == void_list_node
25343 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25344 }
25345
25346 /* True if FN is a guide generated from a constructor template. */
25347
25348 bool
25349 template_guide_p (const_tree fn)
25350 {
25351 gcc_assert (deduction_guide_p (fn));
25352 if (!DECL_ARTIFICIAL (fn))
25353 return false;
25354 tree tmpl = DECL_TI_TEMPLATE (fn);
25355 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25356 return PRIMARY_TEMPLATE_P (org);
25357 return false;
25358 }
25359
25360 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25361 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25362 template parameter types. Note that the handling of template template
25363 parameters relies on current_template_parms being set appropriately for the
25364 new template. */
25365
25366 static tree
25367 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25368 tree tsubst_args, tsubst_flags_t complain)
25369 {
25370 tree oldidx = get_template_parm_index (olddecl);
25371
25372 tree newtype;
25373 if (TREE_CODE (olddecl) == TYPE_DECL
25374 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25375 {
25376 tree oldtype = TREE_TYPE (olddecl);
25377 newtype = cxx_make_type (TREE_CODE (oldtype));
25378 TYPE_MAIN_VARIANT (newtype) = newtype;
25379 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25380 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25381 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25382 }
25383 else
25384 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25385 complain, NULL_TREE);
25386
25387 tree newdecl
25388 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25389 DECL_NAME (olddecl), newtype);
25390 SET_DECL_TEMPLATE_PARM_P (newdecl);
25391
25392 tree newidx;
25393 if (TREE_CODE (olddecl) == TYPE_DECL
25394 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25395 {
25396 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25397 = build_template_parm_index (index, level, level,
25398 newdecl, newtype);
25399 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25400 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25401 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25402 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25403
25404 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25405 {
25406 DECL_TEMPLATE_RESULT (newdecl)
25407 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25408 DECL_NAME (olddecl), newtype);
25409 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25410 // First create a copy (ttargs) of tsubst_args with an
25411 // additional level for the template template parameter's own
25412 // template parameters (ttparms).
25413 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25414 (DECL_TEMPLATE_PARMS (olddecl)));
25415 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25416 tree ttargs = make_tree_vec (depth + 1);
25417 for (int i = 0; i < depth; ++i)
25418 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25419 TREE_VEC_ELT (ttargs, depth)
25420 = template_parms_level_to_args (ttparms);
25421 // Substitute ttargs into ttparms to fix references to
25422 // other template parameters.
25423 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25424 complain);
25425 // Now substitute again with args based on tparms, to reduce
25426 // the level of the ttparms.
25427 ttargs = current_template_args ();
25428 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25429 complain);
25430 // Finally, tack the adjusted parms onto tparms.
25431 ttparms = tree_cons (size_int (depth), ttparms,
25432 current_template_parms);
25433 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25434 }
25435 }
25436 else
25437 {
25438 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25439 tree newconst
25440 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25441 TREE_CODE (oldconst),
25442 DECL_NAME (oldconst), newtype);
25443 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25444 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25445 SET_DECL_TEMPLATE_PARM_P (newconst);
25446 newidx = build_template_parm_index (index, level, level,
25447 newconst, newtype);
25448 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25449 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25450 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25451 }
25452
25453 return newdecl;
25454 }
25455
25456 /* Returns a C++17 class deduction guide template based on the constructor
25457 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25458 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25459
25460 static tree
25461 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25462 {
25463 tree type, tparms, targs, fparms, fargs, ci;
25464 bool memtmpl = false;
25465 bool explicit_p;
25466 location_t loc;
25467 tree fn_tmpl = NULL_TREE;
25468
25469 if (TYPE_P (ctor))
25470 {
25471 type = ctor;
25472 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25473 if (copy_p)
25474 {
25475 type = TREE_TYPE (type);
25476 fparms = tree_cons (NULL_TREE, type, void_list_node);
25477 }
25478 else
25479 fparms = void_list_node;
25480
25481 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25482 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25483 targs = CLASSTYPE_TI_ARGS (type);
25484 ci = NULL_TREE;
25485 fargs = NULL_TREE;
25486 loc = DECL_SOURCE_LOCATION (ctmpl);
25487 explicit_p = false;
25488 }
25489 else
25490 {
25491 ++processing_template_decl;
25492
25493 fn_tmpl
25494 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25495 : DECL_TI_TEMPLATE (ctor));
25496 if (outer_args)
25497 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25498 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25499
25500 type = DECL_CONTEXT (ctor);
25501
25502 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25503 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25504 fully specialized args for the enclosing class. Strip those off, as
25505 the deduction guide won't have those template parameters. */
25506 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25507 TMPL_PARMS_DEPTH (tparms));
25508 /* Discard the 'this' parameter. */
25509 fparms = FUNCTION_ARG_CHAIN (ctor);
25510 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25511 ci = get_constraints (ctor);
25512 loc = DECL_SOURCE_LOCATION (ctor);
25513 explicit_p = DECL_NONCONVERTING_P (ctor);
25514
25515 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25516 {
25517 memtmpl = true;
25518
25519 /* For a member template constructor, we need to flatten the two
25520 template parameter lists into one, and then adjust the function
25521 signature accordingly. This gets...complicated. */
25522 tree save_parms = current_template_parms;
25523
25524 /* For a member template we should have two levels of parms/args, one
25525 for the class and one for the constructor. We stripped
25526 specialized args for further enclosing classes above. */
25527 const int depth = 2;
25528 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25529
25530 /* Template args for translating references to the two-level template
25531 parameters into references to the one-level template parameters we
25532 are creating. */
25533 tree tsubst_args = copy_node (targs);
25534 TMPL_ARGS_LEVEL (tsubst_args, depth)
25535 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25536
25537 /* Template parms for the constructor template. */
25538 tree ftparms = TREE_VALUE (tparms);
25539 unsigned flen = TREE_VEC_LENGTH (ftparms);
25540 /* Template parms for the class template. */
25541 tparms = TREE_CHAIN (tparms);
25542 tree ctparms = TREE_VALUE (tparms);
25543 unsigned clen = TREE_VEC_LENGTH (ctparms);
25544 /* Template parms for the deduction guide start as a copy of the
25545 template parms for the class. We set current_template_parms for
25546 lookup_template_class_1. */
25547 current_template_parms = tparms = copy_node (tparms);
25548 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25549 for (unsigned i = 0; i < clen; ++i)
25550 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25551
25552 /* Now we need to rewrite the constructor parms to append them to the
25553 class parms. */
25554 for (unsigned i = 0; i < flen; ++i)
25555 {
25556 unsigned index = i + clen;
25557 unsigned level = 1;
25558 tree oldelt = TREE_VEC_ELT (ftparms, i);
25559 tree olddecl = TREE_VALUE (oldelt);
25560 tree newdecl = rewrite_template_parm (olddecl, index, level,
25561 tsubst_args, complain);
25562 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25563 tsubst_args, complain, ctor);
25564 tree list = build_tree_list (newdef, newdecl);
25565 TEMPLATE_PARM_CONSTRAINTS (list)
25566 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25567 tsubst_args, complain, ctor);
25568 TREE_VEC_ELT (new_vec, index) = list;
25569 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25570 }
25571
25572 /* Now we have a final set of template parms to substitute into the
25573 function signature. */
25574 targs = template_parms_to_args (tparms);
25575 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25576 complain, ctor);
25577 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25578 if (ci)
25579 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25580
25581 current_template_parms = save_parms;
25582 }
25583 --processing_template_decl;
25584 }
25585
25586 if (!memtmpl)
25587 {
25588 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25589 tparms = copy_node (tparms);
25590 INNERMOST_TEMPLATE_PARMS (tparms)
25591 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25592 }
25593
25594 tree fntype = build_function_type (type, fparms);
25595 tree ded_fn = build_lang_decl_loc (loc,
25596 FUNCTION_DECL,
25597 dguide_name (type), fntype);
25598 DECL_ARGUMENTS (ded_fn) = fargs;
25599 DECL_ARTIFICIAL (ded_fn) = true;
25600 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25601 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25602 DECL_ARTIFICIAL (ded_tmpl) = true;
25603 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25604 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25605 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25606 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25607 if (DECL_P (ctor))
25608 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25609 if (ci)
25610 set_constraints (ded_tmpl, ci);
25611
25612 return ded_tmpl;
25613 }
25614
25615 /* Deduce template arguments for the class template placeholder PTYPE for
25616 template TMPL based on the initializer INIT, and return the resulting
25617 type. */
25618
25619 static tree
25620 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25621 tsubst_flags_t complain)
25622 {
25623 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25624 {
25625 /* We should have handled this in the caller. */
25626 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25627 return ptype;
25628 if (complain & tf_error)
25629 error ("non-class template %qT used without template arguments", tmpl);
25630 return error_mark_node;
25631 }
25632
25633 tree type = TREE_TYPE (tmpl);
25634
25635 bool try_list_ctor = false;
25636
25637 vec<tree,va_gc> *args;
25638 if (init == NULL_TREE
25639 || TREE_CODE (init) == TREE_LIST)
25640 args = make_tree_vector_from_list (init);
25641 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25642 {
25643 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25644 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25645 {
25646 /* As an exception, the first phase in 16.3.1.7 (considering the
25647 initializer list as a single argument) is omitted if the
25648 initializer list consists of a single expression of type cv U,
25649 where U is a specialization of C or a class derived from a
25650 specialization of C. */
25651 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25652 tree etype = TREE_TYPE (elt);
25653
25654 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25655 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25656 int err = unify (tparms, targs, type, etype,
25657 UNIFY_ALLOW_DERIVED, /*explain*/false);
25658 if (err == 0)
25659 try_list_ctor = false;
25660 ggc_free (targs);
25661 }
25662 if (try_list_ctor || is_std_init_list (type))
25663 args = make_tree_vector_single (init);
25664 else
25665 args = make_tree_vector_from_ctor (init);
25666 }
25667 else
25668 args = make_tree_vector_single (init);
25669
25670 tree dname = dguide_name (tmpl);
25671 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25672 /*type*/false, /*complain*/false,
25673 /*hidden*/false);
25674 bool elided = false;
25675 if (cands == error_mark_node)
25676 cands = NULL_TREE;
25677
25678 /* Prune explicit deduction guides in copy-initialization context. */
25679 if (flags & LOOKUP_ONLYCONVERTING)
25680 {
25681 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25682 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25683 elided = true;
25684
25685 if (elided)
25686 {
25687 /* Found a nonconverting guide, prune the candidates. */
25688 tree pruned = NULL_TREE;
25689 for (lkp_iterator iter (cands); iter; ++iter)
25690 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25691 pruned = lookup_add (*iter, pruned);
25692
25693 cands = pruned;
25694 }
25695 }
25696
25697 tree outer_args = NULL_TREE;
25698 if (DECL_CLASS_SCOPE_P (tmpl)
25699 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25700 {
25701 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25702 type = TREE_TYPE (most_general_template (tmpl));
25703 }
25704
25705 bool saw_ctor = false;
25706 // FIXME cache artificial deduction guides
25707 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25708 {
25709 tree guide = build_deduction_guide (*iter, outer_args, complain);
25710 if ((flags & LOOKUP_ONLYCONVERTING)
25711 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25712 elided = true;
25713 else
25714 cands = lookup_add (guide, cands);
25715
25716 saw_ctor = true;
25717 }
25718
25719 tree call = error_mark_node;
25720
25721 /* If this is list-initialization and the class has a list constructor, first
25722 try deducing from the list as a single argument, as [over.match.list]. */
25723 tree list_cands = NULL_TREE;
25724 if (try_list_ctor && cands)
25725 for (lkp_iterator iter (cands); iter; ++iter)
25726 {
25727 tree dg = *iter;
25728 if (is_list_ctor (dg))
25729 list_cands = lookup_add (dg, list_cands);
25730 }
25731 if (list_cands)
25732 {
25733 ++cp_unevaluated_operand;
25734 call = build_new_function_call (list_cands, &args, tf_decltype);
25735 --cp_unevaluated_operand;
25736
25737 if (call == error_mark_node)
25738 {
25739 /* That didn't work, now try treating the list as a sequence of
25740 arguments. */
25741 release_tree_vector (args);
25742 args = make_tree_vector_from_ctor (init);
25743 }
25744 }
25745
25746 /* Maybe generate an implicit deduction guide. */
25747 if (call == error_mark_node && args->length () < 2)
25748 {
25749 tree gtype = NULL_TREE;
25750
25751 if (args->length () == 1)
25752 /* Generate a copy guide. */
25753 gtype = build_reference_type (type);
25754 else if (!saw_ctor)
25755 /* Generate a default guide. */
25756 gtype = type;
25757
25758 if (gtype)
25759 {
25760 tree guide = build_deduction_guide (gtype, outer_args, complain);
25761 cands = lookup_add (guide, cands);
25762 }
25763 }
25764
25765 if (elided && !cands)
25766 {
25767 error ("cannot deduce template arguments for copy-initialization"
25768 " of %qT, as it has no non-explicit deduction guides or "
25769 "user-declared constructors", type);
25770 return error_mark_node;
25771 }
25772 else if (!cands && call == error_mark_node)
25773 {
25774 error ("cannot deduce template arguments of %qT, as it has no viable "
25775 "deduction guides", type);
25776 return error_mark_node;
25777 }
25778
25779 if (call == error_mark_node)
25780 {
25781 ++cp_unevaluated_operand;
25782 call = build_new_function_call (cands, &args, tf_decltype);
25783 --cp_unevaluated_operand;
25784 }
25785
25786 if (call == error_mark_node && (complain & tf_warning_or_error))
25787 {
25788 error ("class template argument deduction failed:");
25789
25790 ++cp_unevaluated_operand;
25791 call = build_new_function_call (cands, &args, complain | tf_decltype);
25792 --cp_unevaluated_operand;
25793
25794 if (elided)
25795 inform (input_location, "explicit deduction guides not considered "
25796 "for copy-initialization");
25797 }
25798
25799 release_tree_vector (args);
25800
25801 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25802 }
25803
25804 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25805 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25806
25807 tree
25808 do_auto_deduction (tree type, tree init, tree auto_node)
25809 {
25810 return do_auto_deduction (type, init, auto_node,
25811 tf_warning_or_error,
25812 adc_unspecified);
25813 }
25814
25815 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25816 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25817 The CONTEXT determines the context in which auto deduction is performed
25818 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25819 OUTER_TARGS are used during template argument deduction
25820 (context == adc_unify) to properly substitute the result, and is ignored
25821 in other contexts.
25822
25823 For partial-concept-ids, extra args may be appended to the list of deduced
25824 template arguments prior to determining constraint satisfaction. */
25825
25826 tree
25827 do_auto_deduction (tree type, tree init, tree auto_node,
25828 tsubst_flags_t complain, auto_deduction_context context,
25829 tree outer_targs, int flags)
25830 {
25831 tree targs;
25832
25833 if (init == error_mark_node)
25834 return error_mark_node;
25835
25836 if (init && type_dependent_expression_p (init)
25837 && context != adc_unify)
25838 /* Defining a subset of type-dependent expressions that we can deduce
25839 from ahead of time isn't worth the trouble. */
25840 return type;
25841
25842 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25843 /* C++17 class template argument deduction. */
25844 return do_class_deduction (type, tmpl, init, flags, complain);
25845
25846 if (TREE_TYPE (init) == NULL_TREE)
25847 /* Nothing we can do with this, even in deduction context. */
25848 return type;
25849
25850 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25851 with either a new invented type template parameter U or, if the
25852 initializer is a braced-init-list (8.5.4), with
25853 std::initializer_list<U>. */
25854 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25855 {
25856 if (!DIRECT_LIST_INIT_P (init))
25857 type = listify_autos (type, auto_node);
25858 else if (CONSTRUCTOR_NELTS (init) == 1)
25859 init = CONSTRUCTOR_ELT (init, 0)->value;
25860 else
25861 {
25862 if (complain & tf_warning_or_error)
25863 {
25864 if (permerror (input_location, "direct-list-initialization of "
25865 "%<auto%> requires exactly one element"))
25866 inform (input_location,
25867 "for deduction to %<std::initializer_list%>, use copy-"
25868 "list-initialization (i.e. add %<=%> before the %<{%>)");
25869 }
25870 type = listify_autos (type, auto_node);
25871 }
25872 }
25873
25874 if (type == error_mark_node)
25875 return error_mark_node;
25876
25877 init = resolve_nondeduced_context (init, complain);
25878
25879 if (context == adc_decomp_type
25880 && auto_node == type
25881 && init != error_mark_node
25882 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25883 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25884 and initializer has array type, deduce cv-qualified array type. */
25885 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25886 complain);
25887 else if (AUTO_IS_DECLTYPE (auto_node))
25888 {
25889 bool id = (DECL_P (init)
25890 || ((TREE_CODE (init) == COMPONENT_REF
25891 || TREE_CODE (init) == SCOPE_REF)
25892 && !REF_PARENTHESIZED_P (init)));
25893 targs = make_tree_vec (1);
25894 TREE_VEC_ELT (targs, 0)
25895 = finish_decltype_type (init, id, tf_warning_or_error);
25896 if (type != auto_node)
25897 {
25898 if (complain & tf_error)
25899 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25900 return error_mark_node;
25901 }
25902 }
25903 else
25904 {
25905 tree parms = build_tree_list (NULL_TREE, type);
25906 tree tparms;
25907
25908 if (flag_concepts)
25909 tparms = extract_autos (type);
25910 else
25911 {
25912 tparms = make_tree_vec (1);
25913 TREE_VEC_ELT (tparms, 0)
25914 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25915 }
25916
25917 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25918 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25919 DEDUCE_CALL, LOOKUP_NORMAL,
25920 NULL, /*explain_p=*/false);
25921 if (val > 0)
25922 {
25923 if (processing_template_decl)
25924 /* Try again at instantiation time. */
25925 return type;
25926 if (type && type != error_mark_node
25927 && (complain & tf_error))
25928 /* If type is error_mark_node a diagnostic must have been
25929 emitted by now. Also, having a mention to '<type error>'
25930 in the diagnostic is not really useful to the user. */
25931 {
25932 if (cfun && auto_node == current_function_auto_return_pattern
25933 && LAMBDA_FUNCTION_P (current_function_decl))
25934 error ("unable to deduce lambda return type from %qE", init);
25935 else
25936 error ("unable to deduce %qT from %qE", type, init);
25937 type_unification_real (tparms, targs, parms, &init, 1, 0,
25938 DEDUCE_CALL, LOOKUP_NORMAL,
25939 NULL, /*explain_p=*/true);
25940 }
25941 return error_mark_node;
25942 }
25943 }
25944
25945 /* Check any placeholder constraints against the deduced type. */
25946 if (flag_concepts && !processing_template_decl)
25947 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25948 {
25949 /* Use the deduced type to check the associated constraints. If we
25950 have a partial-concept-id, rebuild the argument list so that
25951 we check using the extra arguments. */
25952 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25953 tree cargs = CHECK_CONSTR_ARGS (constr);
25954 if (TREE_VEC_LENGTH (cargs) > 1)
25955 {
25956 cargs = copy_node (cargs);
25957 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25958 }
25959 else
25960 cargs = targs;
25961 if (!constraints_satisfied_p (constr, cargs))
25962 {
25963 if (complain & tf_warning_or_error)
25964 {
25965 switch (context)
25966 {
25967 case adc_unspecified:
25968 case adc_unify:
25969 error("placeholder constraints not satisfied");
25970 break;
25971 case adc_variable_type:
25972 case adc_decomp_type:
25973 error ("deduced initializer does not satisfy "
25974 "placeholder constraints");
25975 break;
25976 case adc_return_type:
25977 error ("deduced return type does not satisfy "
25978 "placeholder constraints");
25979 break;
25980 case adc_requirement:
25981 error ("deduced expression type does not satisfy "
25982 "placeholder constraints");
25983 break;
25984 }
25985 diagnose_constraints (input_location, constr, targs);
25986 }
25987 return error_mark_node;
25988 }
25989 }
25990
25991 if (processing_template_decl && context != adc_unify)
25992 outer_targs = current_template_args ();
25993 targs = add_to_template_args (outer_targs, targs);
25994 return tsubst (type, targs, complain, NULL_TREE);
25995 }
25996
25997 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25998 result. */
25999
26000 tree
26001 splice_late_return_type (tree type, tree late_return_type)
26002 {
26003 if (is_auto (type))
26004 {
26005 if (late_return_type)
26006 return late_return_type;
26007
26008 tree idx = get_template_parm_index (type);
26009 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26010 /* In an abbreviated function template we didn't know we were dealing
26011 with a function template when we saw the auto return type, so update
26012 it to have the correct level. */
26013 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26014 }
26015 return type;
26016 }
26017
26018 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26019 'decltype(auto)' or a deduced class template. */
26020
26021 bool
26022 is_auto (const_tree type)
26023 {
26024 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26025 && (TYPE_IDENTIFIER (type) == auto_identifier
26026 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26027 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26028 return true;
26029 else
26030 return false;
26031 }
26032
26033 /* for_each_template_parm callback for type_uses_auto. */
26034
26035 int
26036 is_auto_r (tree tp, void */*data*/)
26037 {
26038 return is_auto (tp);
26039 }
26040
26041 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26042 a use of `auto'. Returns NULL_TREE otherwise. */
26043
26044 tree
26045 type_uses_auto (tree type)
26046 {
26047 if (type == NULL_TREE)
26048 return NULL_TREE;
26049 else if (flag_concepts)
26050 {
26051 /* The Concepts TS allows multiple autos in one type-specifier; just
26052 return the first one we find, do_auto_deduction will collect all of
26053 them. */
26054 if (uses_template_parms (type))
26055 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26056 /*visited*/NULL, /*nondeduced*/true);
26057 else
26058 return NULL_TREE;
26059 }
26060 else
26061 return find_type_usage (type, is_auto);
26062 }
26063
26064 /* For a given template T, return the vector of typedefs referenced
26065 in T for which access check is needed at T instantiation time.
26066 T is either a FUNCTION_DECL or a RECORD_TYPE.
26067 Those typedefs were added to T by the function
26068 append_type_to_template_for_access_check. */
26069
26070 vec<qualified_typedef_usage_t, va_gc> *
26071 get_types_needing_access_check (tree t)
26072 {
26073 tree ti;
26074 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26075
26076 if (!t || t == error_mark_node)
26077 return NULL;
26078
26079 if (!(ti = get_template_info (t)))
26080 return NULL;
26081
26082 if (CLASS_TYPE_P (t)
26083 || TREE_CODE (t) == FUNCTION_DECL)
26084 {
26085 if (!TI_TEMPLATE (ti))
26086 return NULL;
26087
26088 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26089 }
26090
26091 return result;
26092 }
26093
26094 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26095 tied to T. That list of typedefs will be access checked at
26096 T instantiation time.
26097 T is either a FUNCTION_DECL or a RECORD_TYPE.
26098 TYPE_DECL is a TYPE_DECL node representing a typedef.
26099 SCOPE is the scope through which TYPE_DECL is accessed.
26100 LOCATION is the location of the usage point of TYPE_DECL.
26101
26102 This function is a subroutine of
26103 append_type_to_template_for_access_check. */
26104
26105 static void
26106 append_type_to_template_for_access_check_1 (tree t,
26107 tree type_decl,
26108 tree scope,
26109 location_t location)
26110 {
26111 qualified_typedef_usage_t typedef_usage;
26112 tree ti;
26113
26114 if (!t || t == error_mark_node)
26115 return;
26116
26117 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26118 || CLASS_TYPE_P (t))
26119 && type_decl
26120 && TREE_CODE (type_decl) == TYPE_DECL
26121 && scope);
26122
26123 if (!(ti = get_template_info (t)))
26124 return;
26125
26126 gcc_assert (TI_TEMPLATE (ti));
26127
26128 typedef_usage.typedef_decl = type_decl;
26129 typedef_usage.context = scope;
26130 typedef_usage.locus = location;
26131
26132 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26133 }
26134
26135 /* Append TYPE_DECL to the template TEMPL.
26136 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26137 At TEMPL instanciation time, TYPE_DECL will be checked to see
26138 if it can be accessed through SCOPE.
26139 LOCATION is the location of the usage point of TYPE_DECL.
26140
26141 e.g. consider the following code snippet:
26142
26143 class C
26144 {
26145 typedef int myint;
26146 };
26147
26148 template<class U> struct S
26149 {
26150 C::myint mi; // <-- usage point of the typedef C::myint
26151 };
26152
26153 S<char> s;
26154
26155 At S<char> instantiation time, we need to check the access of C::myint
26156 In other words, we need to check the access of the myint typedef through
26157 the C scope. For that purpose, this function will add the myint typedef
26158 and the scope C through which its being accessed to a list of typedefs
26159 tied to the template S. That list will be walked at template instantiation
26160 time and access check performed on each typedefs it contains.
26161 Note that this particular code snippet should yield an error because
26162 myint is private to C. */
26163
26164 void
26165 append_type_to_template_for_access_check (tree templ,
26166 tree type_decl,
26167 tree scope,
26168 location_t location)
26169 {
26170 qualified_typedef_usage_t *iter;
26171 unsigned i;
26172
26173 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26174
26175 /* Make sure we don't append the type to the template twice. */
26176 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26177 if (iter->typedef_decl == type_decl && scope == iter->context)
26178 return;
26179
26180 append_type_to_template_for_access_check_1 (templ, type_decl,
26181 scope, location);
26182 }
26183
26184 /* Convert the generic type parameters in PARM that match the types given in the
26185 range [START_IDX, END_IDX) from the current_template_parms into generic type
26186 packs. */
26187
26188 tree
26189 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26190 {
26191 tree current = current_template_parms;
26192 int depth = TMPL_PARMS_DEPTH (current);
26193 current = INNERMOST_TEMPLATE_PARMS (current);
26194 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26195
26196 for (int i = 0; i < start_idx; ++i)
26197 TREE_VEC_ELT (replacement, i)
26198 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26199
26200 for (int i = start_idx; i < end_idx; ++i)
26201 {
26202 /* Create a distinct parameter pack type from the current parm and add it
26203 to the replacement args to tsubst below into the generic function
26204 parameter. */
26205
26206 tree o = TREE_TYPE (TREE_VALUE
26207 (TREE_VEC_ELT (current, i)));
26208 tree t = copy_type (o);
26209 TEMPLATE_TYPE_PARM_INDEX (t)
26210 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26211 o, 0, 0, tf_none);
26212 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26213 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26214 TYPE_MAIN_VARIANT (t) = t;
26215 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26216 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26217 TREE_VEC_ELT (replacement, i) = t;
26218 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26219 }
26220
26221 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26222 TREE_VEC_ELT (replacement, i)
26223 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26224
26225 /* If there are more levels then build up the replacement with the outer
26226 template parms. */
26227 if (depth > 1)
26228 replacement = add_to_template_args (template_parms_to_args
26229 (TREE_CHAIN (current_template_parms)),
26230 replacement);
26231
26232 return tsubst (parm, replacement, tf_none, NULL_TREE);
26233 }
26234
26235 /* Entries in the decl_constraint hash table. */
26236 struct GTY((for_user)) constr_entry
26237 {
26238 tree decl;
26239 tree ci;
26240 };
26241
26242 /* Hashing function and equality for constraint entries. */
26243 struct constr_hasher : ggc_ptr_hash<constr_entry>
26244 {
26245 static hashval_t hash (constr_entry *e)
26246 {
26247 return (hashval_t)DECL_UID (e->decl);
26248 }
26249
26250 static bool equal (constr_entry *e1, constr_entry *e2)
26251 {
26252 return e1->decl == e2->decl;
26253 }
26254 };
26255
26256 /* A mapping from declarations to constraint information. Note that
26257 both templates and their underlying declarations are mapped to the
26258 same constraint information.
26259
26260 FIXME: This is defined in pt.c because garbage collection
26261 code is not being generated for constraint.cc. */
26262
26263 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26264
26265 /* Returns the template constraints of declaration T. If T is not
26266 constrained, return NULL_TREE. Note that T must be non-null. */
26267
26268 tree
26269 get_constraints (tree t)
26270 {
26271 if (!flag_concepts)
26272 return NULL_TREE;
26273
26274 gcc_assert (DECL_P (t));
26275 if (TREE_CODE (t) == TEMPLATE_DECL)
26276 t = DECL_TEMPLATE_RESULT (t);
26277 constr_entry elt = { t, NULL_TREE };
26278 constr_entry* found = decl_constraints->find (&elt);
26279 if (found)
26280 return found->ci;
26281 else
26282 return NULL_TREE;
26283 }
26284
26285 /* Associate the given constraint information CI with the declaration
26286 T. If T is a template, then the constraints are associated with
26287 its underlying declaration. Don't build associations if CI is
26288 NULL_TREE. */
26289
26290 void
26291 set_constraints (tree t, tree ci)
26292 {
26293 if (!ci)
26294 return;
26295 gcc_assert (t && flag_concepts);
26296 if (TREE_CODE (t) == TEMPLATE_DECL)
26297 t = DECL_TEMPLATE_RESULT (t);
26298 gcc_assert (!get_constraints (t));
26299 constr_entry elt = {t, ci};
26300 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26301 constr_entry* entry = ggc_alloc<constr_entry> ();
26302 *entry = elt;
26303 *slot = entry;
26304 }
26305
26306 /* Remove the associated constraints of the declaration T. */
26307
26308 void
26309 remove_constraints (tree t)
26310 {
26311 gcc_assert (DECL_P (t));
26312 if (TREE_CODE (t) == TEMPLATE_DECL)
26313 t = DECL_TEMPLATE_RESULT (t);
26314
26315 constr_entry elt = {t, NULL_TREE};
26316 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26317 if (slot)
26318 decl_constraints->clear_slot (slot);
26319 }
26320
26321 /* Memoized satisfaction results for declarations. This
26322 maps the pair (constraint_info, arguments) to the result computed
26323 by constraints_satisfied_p. */
26324
26325 struct GTY((for_user)) constraint_sat_entry
26326 {
26327 tree ci;
26328 tree args;
26329 tree result;
26330 };
26331
26332 /* Hashing function and equality for constraint entries. */
26333
26334 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26335 {
26336 static hashval_t hash (constraint_sat_entry *e)
26337 {
26338 hashval_t val = iterative_hash_object(e->ci, 0);
26339 return iterative_hash_template_arg (e->args, val);
26340 }
26341
26342 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26343 {
26344 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26345 }
26346 };
26347
26348 /* Memoized satisfaction results for concept checks. */
26349
26350 struct GTY((for_user)) concept_spec_entry
26351 {
26352 tree tmpl;
26353 tree args;
26354 tree result;
26355 };
26356
26357 /* Hashing function and equality for constraint entries. */
26358
26359 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26360 {
26361 static hashval_t hash (concept_spec_entry *e)
26362 {
26363 return hash_tmpl_and_args (e->tmpl, e->args);
26364 }
26365
26366 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26367 {
26368 ++comparing_specializations;
26369 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26370 --comparing_specializations;
26371 return eq;
26372 }
26373 };
26374
26375 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26376 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26377
26378 /* Search for a memoized satisfaction result. Returns one of the
26379 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26380
26381 tree
26382 lookup_constraint_satisfaction (tree ci, tree args)
26383 {
26384 constraint_sat_entry elt = { ci, args, NULL_TREE };
26385 constraint_sat_entry* found = constraint_memos->find (&elt);
26386 if (found)
26387 return found->result;
26388 else
26389 return NULL_TREE;
26390 }
26391
26392 /* Memoize the result of a satisfication test. Returns the saved result. */
26393
26394 tree
26395 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26396 {
26397 constraint_sat_entry elt = {ci, args, result};
26398 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26399 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26400 *entry = elt;
26401 *slot = entry;
26402 return result;
26403 }
26404
26405 /* Search for a memoized satisfaction result for a concept. */
26406
26407 tree
26408 lookup_concept_satisfaction (tree tmpl, tree args)
26409 {
26410 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26411 concept_spec_entry* found = concept_memos->find (&elt);
26412 if (found)
26413 return found->result;
26414 else
26415 return NULL_TREE;
26416 }
26417
26418 /* Memoize the result of a concept check. Returns the saved result. */
26419
26420 tree
26421 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26422 {
26423 concept_spec_entry elt = {tmpl, args, result};
26424 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26425 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26426 *entry = elt;
26427 *slot = entry;
26428 return result;
26429 }
26430
26431 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26432
26433 /* Returns a prior concept specialization. This returns the substituted
26434 and normalized constraints defined by the concept. */
26435
26436 tree
26437 get_concept_expansion (tree tmpl, tree args)
26438 {
26439 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26440 concept_spec_entry* found = concept_expansions->find (&elt);
26441 if (found)
26442 return found->result;
26443 else
26444 return NULL_TREE;
26445 }
26446
26447 /* Save a concept expansion for later. */
26448
26449 tree
26450 save_concept_expansion (tree tmpl, tree args, tree def)
26451 {
26452 concept_spec_entry elt = {tmpl, args, def};
26453 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26454 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26455 *entry = elt;
26456 *slot = entry;
26457 return def;
26458 }
26459
26460 static hashval_t
26461 hash_subsumption_args (tree t1, tree t2)
26462 {
26463 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26464 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26465 int val = 0;
26466 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26467 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26468 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26469 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26470 return val;
26471 }
26472
26473 /* Compare the constraints of two subsumption entries. The LEFT1 and
26474 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26475 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26476
26477 static bool
26478 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26479 {
26480 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26481 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26482 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26483 CHECK_CONSTR_ARGS (right1)))
26484 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26485 CHECK_CONSTR_ARGS (right2));
26486 return false;
26487 }
26488
26489 /* Key/value pair for learning and memoizing subsumption results. This
26490 associates a pair of check constraints (including arguments) with
26491 a boolean value indicating the result. */
26492
26493 struct GTY((for_user)) subsumption_entry
26494 {
26495 tree t1;
26496 tree t2;
26497 bool result;
26498 };
26499
26500 /* Hashing function and equality for constraint entries. */
26501
26502 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26503 {
26504 static hashval_t hash (subsumption_entry *e)
26505 {
26506 return hash_subsumption_args (e->t1, e->t2);
26507 }
26508
26509 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26510 {
26511 ++comparing_specializations;
26512 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26513 --comparing_specializations;
26514 return eq;
26515 }
26516 };
26517
26518 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26519
26520 /* Search for a previously cached subsumption result. */
26521
26522 bool*
26523 lookup_subsumption_result (tree t1, tree t2)
26524 {
26525 subsumption_entry elt = { t1, t2, false };
26526 subsumption_entry* found = subsumption_table->find (&elt);
26527 if (found)
26528 return &found->result;
26529 else
26530 return 0;
26531 }
26532
26533 /* Save a subsumption result. */
26534
26535 bool
26536 save_subsumption_result (tree t1, tree t2, bool result)
26537 {
26538 subsumption_entry elt = {t1, t2, result};
26539 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26540 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26541 *entry = elt;
26542 *slot = entry;
26543 return result;
26544 }
26545
26546 /* Set up the hash table for constraint association. */
26547
26548 void
26549 init_constraint_processing (void)
26550 {
26551 if (!flag_concepts)
26552 return;
26553
26554 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26555 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26556 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26557 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26558 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26559 }
26560
26561 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26562 0..N-1. */
26563
26564 void
26565 declare_integer_pack (void)
26566 {
26567 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26568 build_function_type_list (integer_type_node,
26569 integer_type_node,
26570 NULL_TREE),
26571 NULL_TREE, ECF_CONST);
26572 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26573 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26574 }
26575
26576 /* Set up the hash tables for template instantiations. */
26577
26578 void
26579 init_template_processing (void)
26580 {
26581 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26582 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26583
26584 if (cxx_dialect >= cxx11)
26585 declare_integer_pack ();
26586 }
26587
26588 /* Print stats about the template hash tables for -fstats. */
26589
26590 void
26591 print_template_statistics (void)
26592 {
26593 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26594 "%f collisions\n", (long) decl_specializations->size (),
26595 (long) decl_specializations->elements (),
26596 decl_specializations->collisions ());
26597 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26598 "%f collisions\n", (long) type_specializations->size (),
26599 (long) type_specializations->elements (),
26600 type_specializations->collisions ());
26601 }
26602
26603 #include "gt-cp-pt.h"