]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
4226d4d6b5cf0e5d8c9fcc1fd07022fec4503513
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
45
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
49
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
54 {
55 struct pending_template *next;
56 struct tinst_level *tinst;
57 };
58
59 static GTY(()) struct pending_template *pending_templates;
60 static GTY(()) struct pending_template *last_pending_template;
61
62 int processing_template_parmlist;
63 static int template_header_count;
64
65 static GTY(()) tree saved_trees;
66 static vec<int> inline_parm_levels;
67
68 static GTY(()) struct tinst_level *current_tinst_level;
69
70 static GTY(()) tree saved_access_scope;
71
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr;
76
77 // -------------------------------------------------------------------------- //
78 // Local Specialization Stack
79 //
80 // Implementation of the RAII helper for creating new local
81 // specializations.
82 local_specialization_stack::local_specialization_stack (lss_policy policy)
83 : saved (local_specializations)
84 {
85 if (policy == lss_blank || !saved)
86 local_specializations = new hash_map<tree, tree>;
87 else
88 local_specializations = new hash_map<tree, tree>(*saved);
89 }
90
91 local_specialization_stack::~local_specialization_stack ()
92 {
93 delete local_specializations;
94 local_specializations = saved;
95 }
96
97 /* True if we've recursed into fn_type_unification too many times. */
98 static bool excessive_deduction_depth;
99
100 struct GTY((for_user)) spec_entry
101 {
102 tree tmpl;
103 tree args;
104 tree spec;
105 };
106
107 struct spec_hasher : ggc_ptr_hash<spec_entry>
108 {
109 static hashval_t hash (spec_entry *);
110 static bool equal (spec_entry *, spec_entry *);
111 };
112
113 static GTY (()) hash_table<spec_hasher> *decl_specializations;
114
115 static GTY (()) hash_table<spec_hasher> *type_specializations;
116
117 /* Contains canonical template parameter types. The vector is indexed by
118 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
119 TREE_LIST, whose TREE_VALUEs contain the canonical template
120 parameters of various types and levels. */
121 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
122
123 #define UNIFY_ALLOW_NONE 0
124 #define UNIFY_ALLOW_MORE_CV_QUAL 1
125 #define UNIFY_ALLOW_LESS_CV_QUAL 2
126 #define UNIFY_ALLOW_DERIVED 4
127 #define UNIFY_ALLOW_INTEGER 8
128 #define UNIFY_ALLOW_OUTER_LEVEL 16
129 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
130 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131
132 enum template_base_result {
133 tbr_incomplete_type,
134 tbr_ambiguous_baseclass,
135 tbr_success
136 };
137
138 static void push_access_scope (tree);
139 static void pop_access_scope (tree);
140 static bool resolve_overloaded_unification (tree, tree, tree, tree,
141 unification_kind_t, int,
142 bool);
143 static int try_one_overload (tree, tree, tree, tree, tree,
144 unification_kind_t, int, bool, bool);
145 static int unify (tree, tree, tree, tree, int, bool);
146 static void add_pending_template (tree);
147 static tree reopen_tinst_level (struct tinst_level *);
148 static tree tsubst_initializer_list (tree, tree);
149 static tree get_partial_spec_bindings (tree, tree, tree);
150 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
153 bool, bool);
154 static void tsubst_enum (tree, tree, tree);
155 static tree add_to_template_args (tree, tree);
156 static tree add_outermost_template_args (tree, tree);
157 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
158 static int check_non_deducible_conversion (tree, tree, int, int,
159 struct conversion **, bool);
160 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
161 tree);
162 static int type_unification_real (tree, tree, tree, const tree *,
163 unsigned int, int, unification_kind_t,
164 vec<deferred_access_check, va_gc> **,
165 bool);
166 static void note_template_header (int);
167 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
168 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
169 static tree convert_template_argument (tree, tree, tree,
170 tsubst_flags_t, int, tree);
171 static tree for_each_template_parm (tree, tree_fn_t, void*,
172 hash_set<tree> *, bool, tree_fn_t = NULL);
173 static tree expand_template_argument_pack (tree);
174 static tree build_template_parm_index (int, int, int, tree, tree);
175 static bool inline_needs_template_parms (tree, bool);
176 static void push_inline_template_parms_recursive (tree, int);
177 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
178 static int mark_template_parm (tree, void *);
179 static int template_parm_this_level_p (tree, void *);
180 static tree tsubst_friend_function (tree, tree);
181 static tree tsubst_friend_class (tree, tree);
182 static int can_complete_type_without_circularity (tree);
183 static tree get_bindings (tree, tree, tree, bool);
184 static int template_decl_level (tree);
185 static int check_cv_quals_for_unify (int, tree, tree);
186 static void template_parm_level_and_index (tree, int*, int*);
187 static int unify_pack_expansion (tree, tree, tree,
188 tree, unification_kind_t, bool, bool);
189 static tree copy_template_args (tree);
190 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
191 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
192 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
193 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
194 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
195 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
196 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
197 static bool check_specialization_scope (void);
198 static tree process_partial_specialization (tree);
199 static void set_current_access_from_decl (tree);
200 static enum template_base_result get_template_base (tree, tree, tree, tree,
201 bool , tree *);
202 static tree try_class_unification (tree, tree, tree, tree, bool);
203 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
204 tree, tree);
205 static bool template_template_parm_bindings_ok_p (tree, tree);
206 static void tsubst_default_arguments (tree, tsubst_flags_t);
207 static tree for_each_template_parm_r (tree *, int *, void *);
208 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
209 static void copy_default_args_to_explicit_spec (tree);
210 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
211 static bool dependent_template_arg_p (tree);
212 static bool any_template_arguments_need_structural_equality_p (tree);
213 static bool dependent_type_p_r (tree);
214 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
215 static tree tsubst_decl (tree, tree, tsubst_flags_t);
216 static void perform_typedefs_access_check (tree tmpl, tree targs);
217 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
218 location_t);
219 static tree listify (tree);
220 static tree listify_autos (tree, tree);
221 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
222 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
223 static bool complex_alias_template_p (const_tree tmpl);
224 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
225 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
226 static tree make_argument_pack (tree);
227 static void register_parameter_specializations (tree, tree);
228 static tree enclosing_instantiation_of (tree tctx);
229
230 /* Make the current scope suitable for access checking when we are
231 processing T. T can be FUNCTION_DECL for instantiated function
232 template, VAR_DECL for static member variable, or TYPE_DECL for
233 alias template (needed by instantiate_decl). */
234
235 static void
236 push_access_scope (tree t)
237 {
238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
239 || TREE_CODE (t) == TYPE_DECL);
240
241 if (DECL_FRIEND_CONTEXT (t))
242 push_nested_class (DECL_FRIEND_CONTEXT (t));
243 else if (DECL_CLASS_SCOPE_P (t))
244 push_nested_class (DECL_CONTEXT (t));
245 else
246 push_to_top_level ();
247
248 if (TREE_CODE (t) == FUNCTION_DECL)
249 {
250 saved_access_scope = tree_cons
251 (NULL_TREE, current_function_decl, saved_access_scope);
252 current_function_decl = t;
253 }
254 }
255
256 /* Restore the scope set up by push_access_scope. T is the node we
257 are processing. */
258
259 static void
260 pop_access_scope (tree t)
261 {
262 if (TREE_CODE (t) == FUNCTION_DECL)
263 {
264 current_function_decl = TREE_VALUE (saved_access_scope);
265 saved_access_scope = TREE_CHAIN (saved_access_scope);
266 }
267
268 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
269 pop_nested_class ();
270 else
271 pop_from_top_level ();
272 }
273
274 /* Do any processing required when DECL (a member template
275 declaration) is finished. Returns the TEMPLATE_DECL corresponding
276 to DECL, unless it is a specialization, in which case the DECL
277 itself is returned. */
278
279 tree
280 finish_member_template_decl (tree decl)
281 {
282 if (decl == error_mark_node)
283 return error_mark_node;
284
285 gcc_assert (DECL_P (decl));
286
287 if (TREE_CODE (decl) == TYPE_DECL)
288 {
289 tree type;
290
291 type = TREE_TYPE (decl);
292 if (type == error_mark_node)
293 return error_mark_node;
294 if (MAYBE_CLASS_TYPE_P (type)
295 && CLASSTYPE_TEMPLATE_INFO (type)
296 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
297 {
298 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
299 check_member_template (tmpl);
300 return tmpl;
301 }
302 return NULL_TREE;
303 }
304 else if (TREE_CODE (decl) == FIELD_DECL)
305 error ("data member %qD cannot be a member template", decl);
306 else if (DECL_TEMPLATE_INFO (decl))
307 {
308 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
309 {
310 check_member_template (DECL_TI_TEMPLATE (decl));
311 return DECL_TI_TEMPLATE (decl);
312 }
313 else
314 return decl;
315 }
316 else
317 error ("invalid member template declaration %qD", decl);
318
319 return error_mark_node;
320 }
321
322 /* Create a template info node. */
323
324 tree
325 build_template_info (tree template_decl, tree template_args)
326 {
327 tree result = make_node (TEMPLATE_INFO);
328 TI_TEMPLATE (result) = template_decl;
329 TI_ARGS (result) = template_args;
330 return result;
331 }
332
333 /* Return the template info node corresponding to T, whatever T is. */
334
335 tree
336 get_template_info (const_tree t)
337 {
338 tree tinfo = NULL_TREE;
339
340 if (!t || t == error_mark_node)
341 return NULL;
342
343 if (TREE_CODE (t) == NAMESPACE_DECL
344 || TREE_CODE (t) == PARM_DECL)
345 return NULL;
346
347 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
348 tinfo = DECL_TEMPLATE_INFO (t);
349
350 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
351 t = TREE_TYPE (t);
352
353 if (OVERLOAD_TYPE_P (t))
354 tinfo = TYPE_TEMPLATE_INFO (t);
355 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
356 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
357
358 return tinfo;
359 }
360
361 /* Returns the template nesting level of the indicated class TYPE.
362
363 For example, in:
364 template <class T>
365 struct A
366 {
367 template <class U>
368 struct B {};
369 };
370
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
374
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
377 always safe. */
378
379 int
380 template_class_depth (tree type)
381 {
382 int depth;
383
384 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
385 {
386 tree tinfo = get_template_info (type);
387
388 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
390 ++depth;
391
392 if (DECL_P (type))
393 type = CP_DECL_CONTEXT (type);
394 else if (LAMBDA_TYPE_P (type))
395 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
396 else
397 type = CP_TYPE_CONTEXT (type);
398 }
399
400 return depth;
401 }
402
403 /* Subroutine of maybe_begin_member_template_processing.
404 Returns true if processing DECL needs us to push template parms. */
405
406 static bool
407 inline_needs_template_parms (tree decl, bool nsdmi)
408 {
409 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
410 return false;
411
412 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
413 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
414 }
415
416 /* Subroutine of maybe_begin_member_template_processing.
417 Push the template parms in PARMS, starting from LEVELS steps into the
418 chain, and ending at the beginning, since template parms are listed
419 innermost first. */
420
421 static void
422 push_inline_template_parms_recursive (tree parmlist, int levels)
423 {
424 tree parms = TREE_VALUE (parmlist);
425 int i;
426
427 if (levels > 1)
428 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
429
430 ++processing_template_decl;
431 current_template_parms
432 = tree_cons (size_int (processing_template_decl),
433 parms, current_template_parms);
434 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
435
436 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
437 NULL);
438 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
439 {
440 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
441
442 if (error_operand_p (parm))
443 continue;
444
445 gcc_assert (DECL_P (parm));
446
447 switch (TREE_CODE (parm))
448 {
449 case TYPE_DECL:
450 case TEMPLATE_DECL:
451 pushdecl (parm);
452 break;
453
454 case PARM_DECL:
455 /* Push the CONST_DECL. */
456 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
457 break;
458
459 default:
460 gcc_unreachable ();
461 }
462 }
463 }
464
465 /* Restore the template parameter context for a member template, a
466 friend template defined in a class definition, or a non-template
467 member of template class. */
468
469 void
470 maybe_begin_member_template_processing (tree decl)
471 {
472 tree parms;
473 int levels = 0;
474 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
475
476 if (nsdmi)
477 {
478 tree ctx = DECL_CONTEXT (decl);
479 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
480 /* Disregard full specializations (c++/60999). */
481 && uses_template_parms (ctx)
482 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
483 }
484
485 if (inline_needs_template_parms (decl, nsdmi))
486 {
487 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
488 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
489
490 if (DECL_TEMPLATE_SPECIALIZATION (decl))
491 {
492 --levels;
493 parms = TREE_CHAIN (parms);
494 }
495
496 push_inline_template_parms_recursive (parms, levels);
497 }
498
499 /* Remember how many levels of template parameters we pushed so that
500 we can pop them later. */
501 inline_parm_levels.safe_push (levels);
502 }
503
504 /* Undo the effects of maybe_begin_member_template_processing. */
505
506 void
507 maybe_end_member_template_processing (void)
508 {
509 int i;
510 int last;
511
512 if (inline_parm_levels.length () == 0)
513 return;
514
515 last = inline_parm_levels.pop ();
516 for (i = 0; i < last; ++i)
517 {
518 --processing_template_decl;
519 current_template_parms = TREE_CHAIN (current_template_parms);
520 poplevel (0, 0, 0);
521 }
522 }
523
524 /* Return a new template argument vector which contains all of ARGS,
525 but has as its innermost set of arguments the EXTRA_ARGS. */
526
527 static tree
528 add_to_template_args (tree args, tree extra_args)
529 {
530 tree new_args;
531 int extra_depth;
532 int i;
533 int j;
534
535 if (args == NULL_TREE || extra_args == error_mark_node)
536 return extra_args;
537
538 extra_depth = TMPL_ARGS_DEPTH (extra_args);
539 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
540
541 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
542 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
543
544 for (j = 1; j <= extra_depth; ++j, ++i)
545 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
546
547 return new_args;
548 }
549
550 /* Like add_to_template_args, but only the outermost ARGS are added to
551 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
552 (EXTRA_ARGS) levels are added. This function is used to combine
553 the template arguments from a partial instantiation with the
554 template arguments used to attain the full instantiation from the
555 partial instantiation. */
556
557 static tree
558 add_outermost_template_args (tree args, tree extra_args)
559 {
560 tree new_args;
561
562 /* If there are more levels of EXTRA_ARGS than there are ARGS,
563 something very fishy is going on. */
564 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
565
566 /* If *all* the new arguments will be the EXTRA_ARGS, just return
567 them. */
568 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
569 return extra_args;
570
571 /* For the moment, we make ARGS look like it contains fewer levels. */
572 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
573
574 new_args = add_to_template_args (args, extra_args);
575
576 /* Now, we restore ARGS to its full dimensions. */
577 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
578
579 return new_args;
580 }
581
582 /* Return the N levels of innermost template arguments from the ARGS. */
583
584 tree
585 get_innermost_template_args (tree args, int n)
586 {
587 tree new_args;
588 int extra_levels;
589 int i;
590
591 gcc_assert (n >= 0);
592
593 /* If N is 1, just return the innermost set of template arguments. */
594 if (n == 1)
595 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
596
597 /* If we're not removing anything, just return the arguments we were
598 given. */
599 extra_levels = TMPL_ARGS_DEPTH (args) - n;
600 gcc_assert (extra_levels >= 0);
601 if (extra_levels == 0)
602 return args;
603
604 /* Make a new set of arguments, not containing the outer arguments. */
605 new_args = make_tree_vec (n);
606 for (i = 1; i <= n; ++i)
607 SET_TMPL_ARGS_LEVEL (new_args, i,
608 TMPL_ARGS_LEVEL (args, i + extra_levels));
609
610 return new_args;
611 }
612
613 /* The inverse of get_innermost_template_args: Return all but the innermost
614 EXTRA_LEVELS levels of template arguments from the ARGS. */
615
616 static tree
617 strip_innermost_template_args (tree args, int extra_levels)
618 {
619 tree new_args;
620 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
621 int i;
622
623 gcc_assert (n >= 0);
624
625 /* If N is 1, just return the outermost set of template arguments. */
626 if (n == 1)
627 return TMPL_ARGS_LEVEL (args, 1);
628
629 /* If we're not removing anything, just return the arguments we were
630 given. */
631 gcc_assert (extra_levels >= 0);
632 if (extra_levels == 0)
633 return args;
634
635 /* Make a new set of arguments, not containing the inner arguments. */
636 new_args = make_tree_vec (n);
637 for (i = 1; i <= n; ++i)
638 SET_TMPL_ARGS_LEVEL (new_args, i,
639 TMPL_ARGS_LEVEL (args, i));
640
641 return new_args;
642 }
643
644 /* We've got a template header coming up; push to a new level for storing
645 the parms. */
646
647 void
648 begin_template_parm_list (void)
649 {
650 /* We use a non-tag-transparent scope here, which causes pushtag to
651 put tags in this scope, rather than in the enclosing class or
652 namespace scope. This is the right thing, since we want
653 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
654 global template class, push_template_decl handles putting the
655 TEMPLATE_DECL into top-level scope. For a nested template class,
656 e.g.:
657
658 template <class T> struct S1 {
659 template <class T> struct S2 {};
660 };
661
662 pushtag contains special code to insert the TEMPLATE_DECL for S2
663 at the right scope. */
664 begin_scope (sk_template_parms, NULL);
665 ++processing_template_decl;
666 ++processing_template_parmlist;
667 note_template_header (0);
668
669 /* Add a dummy parameter level while we process the parameter list. */
670 current_template_parms
671 = tree_cons (size_int (processing_template_decl),
672 make_tree_vec (0),
673 current_template_parms);
674 }
675
676 /* This routine is called when a specialization is declared. If it is
677 invalid to declare a specialization here, an error is reported and
678 false is returned, otherwise this routine will return true. */
679
680 static bool
681 check_specialization_scope (void)
682 {
683 tree scope = current_scope ();
684
685 /* [temp.expl.spec]
686
687 An explicit specialization shall be declared in the namespace of
688 which the template is a member, or, for member templates, in the
689 namespace of which the enclosing class or enclosing class
690 template is a member. An explicit specialization of a member
691 function, member class or static data member of a class template
692 shall be declared in the namespace of which the class template
693 is a member. */
694 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
695 {
696 error ("explicit specialization in non-namespace scope %qD", scope);
697 return false;
698 }
699
700 /* [temp.expl.spec]
701
702 In an explicit specialization declaration for a member of a class
703 template or a member template that appears in namespace scope,
704 the member template and some of its enclosing class templates may
705 remain unspecialized, except that the declaration shall not
706 explicitly specialize a class member template if its enclosing
707 class templates are not explicitly specialized as well. */
708 if (current_template_parms)
709 {
710 error ("enclosing class templates are not explicitly specialized");
711 return false;
712 }
713
714 return true;
715 }
716
717 /* We've just seen template <>. */
718
719 bool
720 begin_specialization (void)
721 {
722 begin_scope (sk_template_spec, NULL);
723 note_template_header (1);
724 return check_specialization_scope ();
725 }
726
727 /* Called at then end of processing a declaration preceded by
728 template<>. */
729
730 void
731 end_specialization (void)
732 {
733 finish_scope ();
734 reset_specialization ();
735 }
736
737 /* Any template <>'s that we have seen thus far are not referring to a
738 function specialization. */
739
740 void
741 reset_specialization (void)
742 {
743 processing_specialization = 0;
744 template_header_count = 0;
745 }
746
747 /* We've just seen a template header. If SPECIALIZATION is nonzero,
748 it was of the form template <>. */
749
750 static void
751 note_template_header (int specialization)
752 {
753 processing_specialization = specialization;
754 template_header_count++;
755 }
756
757 /* We're beginning an explicit instantiation. */
758
759 void
760 begin_explicit_instantiation (void)
761 {
762 gcc_assert (!processing_explicit_instantiation);
763 processing_explicit_instantiation = true;
764 }
765
766
767 void
768 end_explicit_instantiation (void)
769 {
770 gcc_assert (processing_explicit_instantiation);
771 processing_explicit_instantiation = false;
772 }
773
774 /* An explicit specialization or partial specialization of TMPL is being
775 declared. Check that the namespace in which the specialization is
776 occurring is permissible. Returns false iff it is invalid to
777 specialize TMPL in the current namespace. */
778
779 static bool
780 check_specialization_namespace (tree tmpl)
781 {
782 tree tpl_ns = decl_namespace_context (tmpl);
783
784 /* [tmpl.expl.spec]
785
786 An explicit specialization shall be declared in a namespace enclosing the
787 specialized template. An explicit specialization whose declarator-id is
788 not qualified shall be declared in the nearest enclosing namespace of the
789 template, or, if the namespace is inline (7.3.1), any namespace from its
790 enclosing namespace set. */
791 if (current_scope() != DECL_CONTEXT (tmpl)
792 && !at_namespace_scope_p ())
793 {
794 error ("specialization of %qD must appear at namespace scope", tmpl);
795 return false;
796 }
797
798 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
799 /* Same or enclosing namespace. */
800 return true;
801 else
802 {
803 auto_diagnostic_group d;
804 if (permerror (input_location,
805 "specialization of %qD in different namespace", tmpl))
806 inform (DECL_SOURCE_LOCATION (tmpl),
807 " from definition of %q#D", tmpl);
808 return false;
809 }
810 }
811
812 /* SPEC is an explicit instantiation. Check that it is valid to
813 perform this explicit instantiation in the current namespace. */
814
815 static void
816 check_explicit_instantiation_namespace (tree spec)
817 {
818 tree ns;
819
820 /* DR 275: An explicit instantiation shall appear in an enclosing
821 namespace of its template. */
822 ns = decl_namespace_context (spec);
823 if (!is_nested_namespace (current_namespace, ns))
824 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
825 "(which does not enclose namespace %qD)",
826 spec, current_namespace, ns);
827 }
828
829 // Returns the type of a template specialization only if that
830 // specialization needs to be defined. Otherwise (e.g., if the type has
831 // already been defined), the function returns NULL_TREE.
832 static tree
833 maybe_new_partial_specialization (tree type)
834 {
835 // An implicit instantiation of an incomplete type implies
836 // the definition of a new class template.
837 //
838 // template<typename T>
839 // struct S;
840 //
841 // template<typename T>
842 // struct S<T*>;
843 //
844 // Here, S<T*> is an implicit instantiation of S whose type
845 // is incomplete.
846 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
847 return type;
848
849 // It can also be the case that TYPE is a completed specialization.
850 // Continuing the previous example, suppose we also declare:
851 //
852 // template<typename T>
853 // requires Integral<T>
854 // struct S<T*>;
855 //
856 // Here, S<T*> refers to the specialization S<T*> defined
857 // above. However, we need to differentiate definitions because
858 // we intend to define a new partial specialization. In this case,
859 // we rely on the fact that the constraints are different for
860 // this declaration than that above.
861 //
862 // Note that we also get here for injected class names and
863 // late-parsed template definitions. We must ensure that we
864 // do not create new type declarations for those cases.
865 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
866 {
867 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
868 tree args = CLASSTYPE_TI_ARGS (type);
869
870 // If there are no template parameters, this cannot be a new
871 // partial template specializtion?
872 if (!current_template_parms)
873 return NULL_TREE;
874
875 // The injected-class-name is not a new partial specialization.
876 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
877 return NULL_TREE;
878
879 // If the constraints are not the same as those of the primary
880 // then, we can probably create a new specialization.
881 tree type_constr = current_template_constraints ();
882
883 if (type == TREE_TYPE (tmpl))
884 {
885 tree main_constr = get_constraints (tmpl);
886 if (equivalent_constraints (type_constr, main_constr))
887 return NULL_TREE;
888 }
889
890 // Also, if there's a pre-existing specialization with matching
891 // constraints, then this also isn't new.
892 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
893 while (specs)
894 {
895 tree spec_tmpl = TREE_VALUE (specs);
896 tree spec_args = TREE_PURPOSE (specs);
897 tree spec_constr = get_constraints (spec_tmpl);
898 if (comp_template_args (args, spec_args)
899 && equivalent_constraints (type_constr, spec_constr))
900 return NULL_TREE;
901 specs = TREE_CHAIN (specs);
902 }
903
904 // Create a new type node (and corresponding type decl)
905 // for the newly declared specialization.
906 tree t = make_class_type (TREE_CODE (type));
907 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
908 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
909
910 /* We only need a separate type node for storing the definition of this
911 partial specialization; uses of S<T*> are unconstrained, so all are
912 equivalent. So keep TYPE_CANONICAL the same. */
913 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
914
915 // Build the corresponding type decl.
916 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
917 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
918 DECL_SOURCE_LOCATION (d) = input_location;
919
920 return t;
921 }
922
923 return NULL_TREE;
924 }
925
926 /* The TYPE is being declared. If it is a template type, that means it
927 is a partial specialization. Do appropriate error-checking. */
928
929 tree
930 maybe_process_partial_specialization (tree type)
931 {
932 tree context;
933
934 if (type == error_mark_node)
935 return error_mark_node;
936
937 /* A lambda that appears in specialization context is not itself a
938 specialization. */
939 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
940 return type;
941
942 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
943 {
944 error ("name of class shadows template template parameter %qD",
945 TYPE_NAME (type));
946 return error_mark_node;
947 }
948
949 context = TYPE_CONTEXT (type);
950
951 if (TYPE_ALIAS_P (type))
952 {
953 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
954
955 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
956 error ("specialization of alias template %qD",
957 TI_TEMPLATE (tinfo));
958 else
959 error ("explicit specialization of non-template %qT", type);
960 return error_mark_node;
961 }
962 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
963 {
964 /* This is for ordinary explicit specialization and partial
965 specialization of a template class such as:
966
967 template <> class C<int>;
968
969 or:
970
971 template <class T> class C<T*>;
972
973 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
974
975 if (tree t = maybe_new_partial_specialization (type))
976 {
977 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
978 && !at_namespace_scope_p ())
979 return error_mark_node;
980 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
981 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
982 if (processing_template_decl)
983 {
984 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
985 if (decl == error_mark_node)
986 return error_mark_node;
987 return TREE_TYPE (decl);
988 }
989 }
990 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
991 error ("specialization of %qT after instantiation", type);
992 else if (errorcount && !processing_specialization
993 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
994 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
995 /* Trying to define a specialization either without a template<> header
996 or in an inappropriate place. We've already given an error, so just
997 bail now so we don't actually define the specialization. */
998 return error_mark_node;
999 }
1000 else if (CLASS_TYPE_P (type)
1001 && !CLASSTYPE_USE_TEMPLATE (type)
1002 && CLASSTYPE_TEMPLATE_INFO (type)
1003 && context && CLASS_TYPE_P (context)
1004 && CLASSTYPE_TEMPLATE_INFO (context))
1005 {
1006 /* This is for an explicit specialization of member class
1007 template according to [temp.expl.spec/18]:
1008
1009 template <> template <class U> class C<int>::D;
1010
1011 The context `C<int>' must be an implicit instantiation.
1012 Otherwise this is just a member class template declared
1013 earlier like:
1014
1015 template <> class C<int> { template <class U> class D; };
1016 template <> template <class U> class C<int>::D;
1017
1018 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1019 while in the second case, `C<int>::D' is a primary template
1020 and `C<T>::D' may not exist. */
1021
1022 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1023 && !COMPLETE_TYPE_P (type))
1024 {
1025 tree t;
1026 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1027
1028 if (current_namespace
1029 != decl_namespace_context (tmpl))
1030 {
1031 permerror (input_location,
1032 "specializing %q#T in different namespace", type);
1033 permerror (DECL_SOURCE_LOCATION (tmpl),
1034 " from definition of %q#D", tmpl);
1035 }
1036
1037 /* Check for invalid specialization after instantiation:
1038
1039 template <> template <> class C<int>::D<int>;
1040 template <> template <class U> class C<int>::D; */
1041
1042 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1043 t; t = TREE_CHAIN (t))
1044 {
1045 tree inst = TREE_VALUE (t);
1046 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1047 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1048 {
1049 /* We already have a full specialization of this partial
1050 instantiation, or a full specialization has been
1051 looked up but not instantiated. Reassign it to the
1052 new member specialization template. */
1053 spec_entry elt;
1054 spec_entry *entry;
1055
1056 elt.tmpl = most_general_template (tmpl);
1057 elt.args = CLASSTYPE_TI_ARGS (inst);
1058 elt.spec = inst;
1059
1060 type_specializations->remove_elt (&elt);
1061
1062 elt.tmpl = tmpl;
1063 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1064
1065 spec_entry **slot
1066 = type_specializations->find_slot (&elt, INSERT);
1067 entry = ggc_alloc<spec_entry> ();
1068 *entry = elt;
1069 *slot = entry;
1070 }
1071 else
1072 /* But if we've had an implicit instantiation, that's a
1073 problem ([temp.expl.spec]/6). */
1074 error ("specialization %qT after instantiation %qT",
1075 type, inst);
1076 }
1077
1078 /* Mark TYPE as a specialization. And as a result, we only
1079 have one level of template argument for the innermost
1080 class template. */
1081 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1082 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1083 CLASSTYPE_TI_ARGS (type)
1084 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1085 }
1086 }
1087 else if (processing_specialization)
1088 {
1089 /* Someday C++0x may allow for enum template specialization. */
1090 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1091 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1092 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1093 "of %qD not allowed by ISO C++", type);
1094 else
1095 {
1096 error ("explicit specialization of non-template %qT", type);
1097 return error_mark_node;
1098 }
1099 }
1100
1101 return type;
1102 }
1103
1104 /* Returns nonzero if we can optimize the retrieval of specializations
1105 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1106 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1107
1108 static inline bool
1109 optimize_specialization_lookup_p (tree tmpl)
1110 {
1111 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1112 && DECL_CLASS_SCOPE_P (tmpl)
1113 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1114 parameter. */
1115 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1116 /* The optimized lookup depends on the fact that the
1117 template arguments for the member function template apply
1118 purely to the containing class, which is not true if the
1119 containing class is an explicit or partial
1120 specialization. */
1121 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1122 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1123 && !DECL_CONV_FN_P (tmpl)
1124 /* It is possible to have a template that is not a member
1125 template and is not a member of a template class:
1126
1127 template <typename T>
1128 struct S { friend A::f(); };
1129
1130 Here, the friend function is a template, but the context does
1131 not have template information. The optimized lookup relies
1132 on having ARGS be the template arguments for both the class
1133 and the function template. */
1134 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1135 }
1136
1137 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1138 gone through coerce_template_parms by now. */
1139
1140 static void
1141 verify_unstripped_args_1 (tree inner)
1142 {
1143 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1144 {
1145 tree arg = TREE_VEC_ELT (inner, i);
1146 if (TREE_CODE (arg) == TEMPLATE_DECL)
1147 /* OK */;
1148 else if (TYPE_P (arg))
1149 gcc_assert (strip_typedefs (arg, NULL) == arg);
1150 else if (ARGUMENT_PACK_P (arg))
1151 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1152 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1153 /* Allow typedefs on the type of a non-type argument, since a
1154 parameter can have them. */;
1155 else
1156 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1157 }
1158 }
1159
1160 static void
1161 verify_unstripped_args (tree args)
1162 {
1163 ++processing_template_decl;
1164 if (!any_dependent_template_arguments_p (args))
1165 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1166 --processing_template_decl;
1167 }
1168
1169 /* Retrieve the specialization (in the sense of [temp.spec] - a
1170 specialization is either an instantiation or an explicit
1171 specialization) of TMPL for the given template ARGS. If there is
1172 no such specialization, return NULL_TREE. The ARGS are a vector of
1173 arguments, or a vector of vectors of arguments, in the case of
1174 templates with more than one level of parameters.
1175
1176 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1177 then we search for a partial specialization matching ARGS. This
1178 parameter is ignored if TMPL is not a class template.
1179
1180 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1181 result is a NONTYPE_ARGUMENT_PACK. */
1182
1183 static tree
1184 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1185 {
1186 if (tmpl == NULL_TREE)
1187 return NULL_TREE;
1188
1189 if (args == error_mark_node)
1190 return NULL_TREE;
1191
1192 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1193 || TREE_CODE (tmpl) == FIELD_DECL);
1194
1195 /* There should be as many levels of arguments as there are
1196 levels of parameters. */
1197 gcc_assert (TMPL_ARGS_DEPTH (args)
1198 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1199 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1200 : template_class_depth (DECL_CONTEXT (tmpl))));
1201
1202 if (flag_checking)
1203 verify_unstripped_args (args);
1204
1205 /* Lambda functions in templates aren't instantiated normally, but through
1206 tsubst_lambda_expr. */
1207 if (lambda_fn_in_template_p (tmpl))
1208 return NULL_TREE;
1209
1210 if (optimize_specialization_lookup_p (tmpl))
1211 {
1212 /* The template arguments actually apply to the containing
1213 class. Find the class specialization with those
1214 arguments. */
1215 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1216 tree class_specialization
1217 = retrieve_specialization (class_template, args, 0);
1218 if (!class_specialization)
1219 return NULL_TREE;
1220
1221 /* Find the instance of TMPL. */
1222 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1223 for (ovl_iterator iter (fns); iter; ++iter)
1224 {
1225 tree fn = *iter;
1226 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1227 /* using-declarations can add base methods to the method vec,
1228 and we don't want those here. */
1229 && DECL_CONTEXT (fn) == class_specialization)
1230 return fn;
1231 }
1232 return NULL_TREE;
1233 }
1234 else
1235 {
1236 spec_entry *found;
1237 spec_entry elt;
1238 hash_table<spec_hasher> *specializations;
1239
1240 elt.tmpl = tmpl;
1241 elt.args = args;
1242 elt.spec = NULL_TREE;
1243
1244 if (DECL_CLASS_TEMPLATE_P (tmpl))
1245 specializations = type_specializations;
1246 else
1247 specializations = decl_specializations;
1248
1249 if (hash == 0)
1250 hash = spec_hasher::hash (&elt);
1251 found = specializations->find_with_hash (&elt, hash);
1252 if (found)
1253 return found->spec;
1254 }
1255
1256 return NULL_TREE;
1257 }
1258
1259 /* Like retrieve_specialization, but for local declarations. */
1260
1261 tree
1262 retrieve_local_specialization (tree tmpl)
1263 {
1264 if (local_specializations == NULL)
1265 return NULL_TREE;
1266
1267 tree *slot = local_specializations->get (tmpl);
1268 return slot ? *slot : NULL_TREE;
1269 }
1270
1271 /* Returns nonzero iff DECL is a specialization of TMPL. */
1272
1273 int
1274 is_specialization_of (tree decl, tree tmpl)
1275 {
1276 tree t;
1277
1278 if (TREE_CODE (decl) == FUNCTION_DECL)
1279 {
1280 for (t = decl;
1281 t != NULL_TREE;
1282 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1283 if (t == tmpl)
1284 return 1;
1285 }
1286 else
1287 {
1288 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1289
1290 for (t = TREE_TYPE (decl);
1291 t != NULL_TREE;
1292 t = CLASSTYPE_USE_TEMPLATE (t)
1293 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1294 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1295 return 1;
1296 }
1297
1298 return 0;
1299 }
1300
1301 /* Returns nonzero iff DECL is a specialization of friend declaration
1302 FRIEND_DECL according to [temp.friend]. */
1303
1304 bool
1305 is_specialization_of_friend (tree decl, tree friend_decl)
1306 {
1307 bool need_template = true;
1308 int template_depth;
1309
1310 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1311 || TREE_CODE (decl) == TYPE_DECL);
1312
1313 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1314 of a template class, we want to check if DECL is a specialization
1315 if this. */
1316 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1317 && DECL_TEMPLATE_INFO (friend_decl)
1318 && !DECL_USE_TEMPLATE (friend_decl))
1319 {
1320 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1321 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1322 need_template = false;
1323 }
1324 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1325 && !PRIMARY_TEMPLATE_P (friend_decl))
1326 need_template = false;
1327
1328 /* There is nothing to do if this is not a template friend. */
1329 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1330 return false;
1331
1332 if (is_specialization_of (decl, friend_decl))
1333 return true;
1334
1335 /* [temp.friend/6]
1336 A member of a class template may be declared to be a friend of a
1337 non-template class. In this case, the corresponding member of
1338 every specialization of the class template is a friend of the
1339 class granting friendship.
1340
1341 For example, given a template friend declaration
1342
1343 template <class T> friend void A<T>::f();
1344
1345 the member function below is considered a friend
1346
1347 template <> struct A<int> {
1348 void f();
1349 };
1350
1351 For this type of template friend, TEMPLATE_DEPTH below will be
1352 nonzero. To determine if DECL is a friend of FRIEND, we first
1353 check if the enclosing class is a specialization of another. */
1354
1355 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1356 if (template_depth
1357 && DECL_CLASS_SCOPE_P (decl)
1358 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1359 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1360 {
1361 /* Next, we check the members themselves. In order to handle
1362 a few tricky cases, such as when FRIEND_DECL's are
1363
1364 template <class T> friend void A<T>::g(T t);
1365 template <class T> template <T t> friend void A<T>::h();
1366
1367 and DECL's are
1368
1369 void A<int>::g(int);
1370 template <int> void A<int>::h();
1371
1372 we need to figure out ARGS, the template arguments from
1373 the context of DECL. This is required for template substitution
1374 of `T' in the function parameter of `g' and template parameter
1375 of `h' in the above examples. Here ARGS corresponds to `int'. */
1376
1377 tree context = DECL_CONTEXT (decl);
1378 tree args = NULL_TREE;
1379 int current_depth = 0;
1380
1381 while (current_depth < template_depth)
1382 {
1383 if (CLASSTYPE_TEMPLATE_INFO (context))
1384 {
1385 if (current_depth == 0)
1386 args = TYPE_TI_ARGS (context);
1387 else
1388 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1389 current_depth++;
1390 }
1391 context = TYPE_CONTEXT (context);
1392 }
1393
1394 if (TREE_CODE (decl) == FUNCTION_DECL)
1395 {
1396 bool is_template;
1397 tree friend_type;
1398 tree decl_type;
1399 tree friend_args_type;
1400 tree decl_args_type;
1401
1402 /* Make sure that both DECL and FRIEND_DECL are templates or
1403 non-templates. */
1404 is_template = DECL_TEMPLATE_INFO (decl)
1405 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1406 if (need_template ^ is_template)
1407 return false;
1408 else if (is_template)
1409 {
1410 /* If both are templates, check template parameter list. */
1411 tree friend_parms
1412 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1413 args, tf_none);
1414 if (!comp_template_parms
1415 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1416 friend_parms))
1417 return false;
1418
1419 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1420 }
1421 else
1422 decl_type = TREE_TYPE (decl);
1423
1424 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1425 tf_none, NULL_TREE);
1426 if (friend_type == error_mark_node)
1427 return false;
1428
1429 /* Check if return types match. */
1430 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1431 return false;
1432
1433 /* Check if function parameter types match, ignoring the
1434 `this' parameter. */
1435 friend_args_type = TYPE_ARG_TYPES (friend_type);
1436 decl_args_type = TYPE_ARG_TYPES (decl_type);
1437 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1438 friend_args_type = TREE_CHAIN (friend_args_type);
1439 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1440 decl_args_type = TREE_CHAIN (decl_args_type);
1441
1442 return compparms (decl_args_type, friend_args_type);
1443 }
1444 else
1445 {
1446 /* DECL is a TYPE_DECL */
1447 bool is_template;
1448 tree decl_type = TREE_TYPE (decl);
1449
1450 /* Make sure that both DECL and FRIEND_DECL are templates or
1451 non-templates. */
1452 is_template
1453 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1454 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1455
1456 if (need_template ^ is_template)
1457 return false;
1458 else if (is_template)
1459 {
1460 tree friend_parms;
1461 /* If both are templates, check the name of the two
1462 TEMPLATE_DECL's first because is_friend didn't. */
1463 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1464 != DECL_NAME (friend_decl))
1465 return false;
1466
1467 /* Now check template parameter list. */
1468 friend_parms
1469 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1470 args, tf_none);
1471 return comp_template_parms
1472 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1473 friend_parms);
1474 }
1475 else
1476 return (DECL_NAME (decl)
1477 == DECL_NAME (friend_decl));
1478 }
1479 }
1480 return false;
1481 }
1482
1483 /* Register the specialization SPEC as a specialization of TMPL with
1484 the indicated ARGS. IS_FRIEND indicates whether the specialization
1485 is actually just a friend declaration. ATTRLIST is the list of
1486 attributes that the specialization is declared with or NULL when
1487 it isn't. Returns SPEC, or an equivalent prior declaration, if
1488 available.
1489
1490 We also store instantiations of field packs in the hash table, even
1491 though they are not themselves templates, to make lookup easier. */
1492
1493 static tree
1494 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1495 hashval_t hash)
1496 {
1497 tree fn;
1498 spec_entry **slot = NULL;
1499 spec_entry elt;
1500
1501 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1502 || (TREE_CODE (tmpl) == FIELD_DECL
1503 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1504
1505 if (TREE_CODE (spec) == FUNCTION_DECL
1506 && uses_template_parms (DECL_TI_ARGS (spec)))
1507 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1508 register it; we want the corresponding TEMPLATE_DECL instead.
1509 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1510 the more obvious `uses_template_parms (spec)' to avoid problems
1511 with default function arguments. In particular, given
1512 something like this:
1513
1514 template <class T> void f(T t1, T t = T())
1515
1516 the default argument expression is not substituted for in an
1517 instantiation unless and until it is actually needed. */
1518 return spec;
1519
1520 if (optimize_specialization_lookup_p (tmpl))
1521 /* We don't put these specializations in the hash table, but we might
1522 want to give an error about a mismatch. */
1523 fn = retrieve_specialization (tmpl, args, 0);
1524 else
1525 {
1526 elt.tmpl = tmpl;
1527 elt.args = args;
1528 elt.spec = spec;
1529
1530 if (hash == 0)
1531 hash = spec_hasher::hash (&elt);
1532
1533 slot =
1534 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1535 if (*slot)
1536 fn = ((spec_entry *) *slot)->spec;
1537 else
1538 fn = NULL_TREE;
1539 }
1540
1541 /* We can sometimes try to re-register a specialization that we've
1542 already got. In particular, regenerate_decl_from_template calls
1543 duplicate_decls which will update the specialization list. But,
1544 we'll still get called again here anyhow. It's more convenient
1545 to simply allow this than to try to prevent it. */
1546 if (fn == spec)
1547 return spec;
1548 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1549 {
1550 if (DECL_TEMPLATE_INSTANTIATION (fn))
1551 {
1552 if (DECL_ODR_USED (fn)
1553 || DECL_EXPLICIT_INSTANTIATION (fn))
1554 {
1555 error ("specialization of %qD after instantiation",
1556 fn);
1557 return error_mark_node;
1558 }
1559 else
1560 {
1561 tree clone;
1562 /* This situation should occur only if the first
1563 specialization is an implicit instantiation, the
1564 second is an explicit specialization, and the
1565 implicit instantiation has not yet been used. That
1566 situation can occur if we have implicitly
1567 instantiated a member function and then specialized
1568 it later.
1569
1570 We can also wind up here if a friend declaration that
1571 looked like an instantiation turns out to be a
1572 specialization:
1573
1574 template <class T> void foo(T);
1575 class S { friend void foo<>(int) };
1576 template <> void foo(int);
1577
1578 We transform the existing DECL in place so that any
1579 pointers to it become pointers to the updated
1580 declaration.
1581
1582 If there was a definition for the template, but not
1583 for the specialization, we want this to look as if
1584 there were no definition, and vice versa. */
1585 DECL_INITIAL (fn) = NULL_TREE;
1586 duplicate_decls (spec, fn, is_friend);
1587 /* The call to duplicate_decls will have applied
1588 [temp.expl.spec]:
1589
1590 An explicit specialization of a function template
1591 is inline only if it is explicitly declared to be,
1592 and independently of whether its function template
1593 is.
1594
1595 to the primary function; now copy the inline bits to
1596 the various clones. */
1597 FOR_EACH_CLONE (clone, fn)
1598 {
1599 DECL_DECLARED_INLINE_P (clone)
1600 = DECL_DECLARED_INLINE_P (fn);
1601 DECL_SOURCE_LOCATION (clone)
1602 = DECL_SOURCE_LOCATION (fn);
1603 DECL_DELETED_FN (clone)
1604 = DECL_DELETED_FN (fn);
1605 }
1606 check_specialization_namespace (tmpl);
1607
1608 return fn;
1609 }
1610 }
1611 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1612 {
1613 tree dd = duplicate_decls (spec, fn, is_friend);
1614 if (dd == error_mark_node)
1615 /* We've already complained in duplicate_decls. */
1616 return error_mark_node;
1617
1618 if (dd == NULL_TREE && DECL_INITIAL (spec))
1619 /* Dup decl failed, but this is a new definition. Set the
1620 line number so any errors match this new
1621 definition. */
1622 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1623
1624 return fn;
1625 }
1626 }
1627 else if (fn)
1628 return duplicate_decls (spec, fn, is_friend);
1629
1630 /* A specialization must be declared in the same namespace as the
1631 template it is specializing. */
1632 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1633 && !check_specialization_namespace (tmpl))
1634 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1635
1636 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1637 {
1638 spec_entry *entry = ggc_alloc<spec_entry> ();
1639 gcc_assert (tmpl && args && spec);
1640 *entry = elt;
1641 *slot = entry;
1642 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1643 && PRIMARY_TEMPLATE_P (tmpl)
1644 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1645 || variable_template_p (tmpl))
1646 /* If TMPL is a forward declaration of a template function, keep a list
1647 of all specializations in case we need to reassign them to a friend
1648 template later in tsubst_friend_function.
1649
1650 Also keep a list of all variable template instantiations so that
1651 process_partial_specialization can check whether a later partial
1652 specialization would have used it. */
1653 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1654 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1655 }
1656
1657 return spec;
1658 }
1659
1660 /* Returns true iff two spec_entry nodes are equivalent. */
1661
1662 int comparing_specializations;
1663
1664 bool
1665 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1666 {
1667 int equal;
1668
1669 ++comparing_specializations;
1670 equal = (e1->tmpl == e2->tmpl
1671 && comp_template_args (e1->args, e2->args));
1672 if (equal && flag_concepts
1673 /* tmpl could be a FIELD_DECL for a capture pack. */
1674 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1675 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1676 && uses_template_parms (e1->args))
1677 {
1678 /* Partial specializations of a variable template can be distinguished by
1679 constraints. */
1680 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1681 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1682 equal = equivalent_constraints (c1, c2);
1683 }
1684 --comparing_specializations;
1685
1686 return equal;
1687 }
1688
1689 /* Returns a hash for a template TMPL and template arguments ARGS. */
1690
1691 static hashval_t
1692 hash_tmpl_and_args (tree tmpl, tree args)
1693 {
1694 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1695 return iterative_hash_template_arg (args, val);
1696 }
1697
1698 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1699 ignoring SPEC. */
1700
1701 hashval_t
1702 spec_hasher::hash (spec_entry *e)
1703 {
1704 return hash_tmpl_and_args (e->tmpl, e->args);
1705 }
1706
1707 /* Recursively calculate a hash value for a template argument ARG, for use
1708 in the hash tables of template specializations. */
1709
1710 hashval_t
1711 iterative_hash_template_arg (tree arg, hashval_t val)
1712 {
1713 unsigned HOST_WIDE_INT i;
1714 enum tree_code code;
1715 char tclass;
1716
1717 if (arg == NULL_TREE)
1718 return iterative_hash_object (arg, val);
1719
1720 if (!TYPE_P (arg))
1721 STRIP_NOPS (arg);
1722
1723 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1724 gcc_unreachable ();
1725
1726 code = TREE_CODE (arg);
1727 tclass = TREE_CODE_CLASS (code);
1728
1729 val = iterative_hash_object (code, val);
1730
1731 switch (code)
1732 {
1733 case ERROR_MARK:
1734 return val;
1735
1736 case IDENTIFIER_NODE:
1737 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1738
1739 case TREE_VEC:
1740 {
1741 int i, len = TREE_VEC_LENGTH (arg);
1742 for (i = 0; i < len; ++i)
1743 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1744 return val;
1745 }
1746
1747 case TYPE_PACK_EXPANSION:
1748 case EXPR_PACK_EXPANSION:
1749 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1750 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1751
1752 case TYPE_ARGUMENT_PACK:
1753 case NONTYPE_ARGUMENT_PACK:
1754 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1755
1756 case TREE_LIST:
1757 for (; arg; arg = TREE_CHAIN (arg))
1758 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1759 return val;
1760
1761 case OVERLOAD:
1762 for (lkp_iterator iter (arg); iter; ++iter)
1763 val = iterative_hash_template_arg (*iter, val);
1764 return val;
1765
1766 case CONSTRUCTOR:
1767 {
1768 tree field, value;
1769 iterative_hash_template_arg (TREE_TYPE (arg), val);
1770 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1771 {
1772 val = iterative_hash_template_arg (field, val);
1773 val = iterative_hash_template_arg (value, val);
1774 }
1775 return val;
1776 }
1777
1778 case PARM_DECL:
1779 if (!DECL_ARTIFICIAL (arg))
1780 {
1781 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1782 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1783 }
1784 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1785
1786 case TARGET_EXPR:
1787 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1788
1789 case PTRMEM_CST:
1790 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1791 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1792
1793 case TEMPLATE_PARM_INDEX:
1794 val = iterative_hash_template_arg
1795 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1796 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1797 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1798
1799 case TRAIT_EXPR:
1800 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1801 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1802 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1803
1804 case BASELINK:
1805 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1806 val);
1807 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1808 val);
1809
1810 case MODOP_EXPR:
1811 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1812 code = TREE_CODE (TREE_OPERAND (arg, 1));
1813 val = iterative_hash_object (code, val);
1814 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1815
1816 case LAMBDA_EXPR:
1817 /* A lambda can't appear in a template arg, but don't crash on
1818 erroneous input. */
1819 gcc_assert (seen_error ());
1820 return val;
1821
1822 case CAST_EXPR:
1823 case IMPLICIT_CONV_EXPR:
1824 case STATIC_CAST_EXPR:
1825 case REINTERPRET_CAST_EXPR:
1826 case CONST_CAST_EXPR:
1827 case DYNAMIC_CAST_EXPR:
1828 case NEW_EXPR:
1829 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1830 /* Now hash operands as usual. */
1831 break;
1832
1833 default:
1834 break;
1835 }
1836
1837 switch (tclass)
1838 {
1839 case tcc_type:
1840 if (alias_template_specialization_p (arg))
1841 {
1842 // We want an alias specialization that survived strip_typedefs
1843 // to hash differently from its TYPE_CANONICAL, to avoid hash
1844 // collisions that compare as different in template_args_equal.
1845 // These could be dependent specializations that strip_typedefs
1846 // left alone, or untouched specializations because
1847 // coerce_template_parms returns the unconverted template
1848 // arguments if it sees incomplete argument packs.
1849 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1850 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1851 }
1852 if (TYPE_CANONICAL (arg))
1853 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1854 val);
1855 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1856 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1857 /* Otherwise just compare the types during lookup. */
1858 return val;
1859
1860 case tcc_declaration:
1861 case tcc_constant:
1862 return iterative_hash_expr (arg, val);
1863
1864 default:
1865 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1866 {
1867 unsigned n = cp_tree_operand_length (arg);
1868 for (i = 0; i < n; ++i)
1869 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1870 return val;
1871 }
1872 }
1873 gcc_unreachable ();
1874 return 0;
1875 }
1876
1877 /* Unregister the specialization SPEC as a specialization of TMPL.
1878 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1879 if the SPEC was listed as a specialization of TMPL.
1880
1881 Note that SPEC has been ggc_freed, so we can't look inside it. */
1882
1883 bool
1884 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1885 {
1886 spec_entry *entry;
1887 spec_entry elt;
1888
1889 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1890 elt.args = TI_ARGS (tinfo);
1891 elt.spec = NULL_TREE;
1892
1893 entry = decl_specializations->find (&elt);
1894 if (entry != NULL)
1895 {
1896 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1897 gcc_assert (new_spec != NULL_TREE);
1898 entry->spec = new_spec;
1899 return 1;
1900 }
1901
1902 return 0;
1903 }
1904
1905 /* Like register_specialization, but for local declarations. We are
1906 registering SPEC, an instantiation of TMPL. */
1907
1908 void
1909 register_local_specialization (tree spec, tree tmpl)
1910 {
1911 gcc_assert (tmpl != spec);
1912 local_specializations->put (tmpl, spec);
1913 }
1914
1915 /* TYPE is a class type. Returns true if TYPE is an explicitly
1916 specialized class. */
1917
1918 bool
1919 explicit_class_specialization_p (tree type)
1920 {
1921 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1922 return false;
1923 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1924 }
1925
1926 /* Print the list of functions at FNS, going through all the overloads
1927 for each element of the list. Alternatively, FNS can not be a
1928 TREE_LIST, in which case it will be printed together with all the
1929 overloads.
1930
1931 MORE and *STR should respectively be FALSE and NULL when the function
1932 is called from the outside. They are used internally on recursive
1933 calls. print_candidates manages the two parameters and leaves NULL
1934 in *STR when it ends. */
1935
1936 static void
1937 print_candidates_1 (tree fns, char **str, bool more = false)
1938 {
1939 if (TREE_CODE (fns) == TREE_LIST)
1940 for (; fns; fns = TREE_CHAIN (fns))
1941 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1942 else
1943 for (lkp_iterator iter (fns); iter;)
1944 {
1945 tree cand = *iter;
1946 ++iter;
1947
1948 const char *pfx = *str;
1949 if (!pfx)
1950 {
1951 if (more || iter)
1952 pfx = _("candidates are:");
1953 else
1954 pfx = _("candidate is:");
1955 *str = get_spaces (pfx);
1956 }
1957 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1958 }
1959 }
1960
1961 /* Print the list of candidate FNS in an error message. FNS can also
1962 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1963
1964 void
1965 print_candidates (tree fns)
1966 {
1967 char *str = NULL;
1968 print_candidates_1 (fns, &str);
1969 free (str);
1970 }
1971
1972 /* Get a (possibly) constrained template declaration for the
1973 purpose of ordering candidates. */
1974 static tree
1975 get_template_for_ordering (tree list)
1976 {
1977 gcc_assert (TREE_CODE (list) == TREE_LIST);
1978 tree f = TREE_VALUE (list);
1979 if (tree ti = DECL_TEMPLATE_INFO (f))
1980 return TI_TEMPLATE (ti);
1981 return f;
1982 }
1983
1984 /* Among candidates having the same signature, return the
1985 most constrained or NULL_TREE if there is no best candidate.
1986 If the signatures of candidates vary (e.g., template
1987 specialization vs. member function), then there can be no
1988 most constrained.
1989
1990 Note that we don't compare constraints on the functions
1991 themselves, but rather those of their templates. */
1992 static tree
1993 most_constrained_function (tree candidates)
1994 {
1995 // Try to find the best candidate in a first pass.
1996 tree champ = candidates;
1997 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1998 {
1999 int winner = more_constrained (get_template_for_ordering (champ),
2000 get_template_for_ordering (c));
2001 if (winner == -1)
2002 champ = c; // The candidate is more constrained
2003 else if (winner == 0)
2004 return NULL_TREE; // Neither is more constrained
2005 }
2006
2007 // Verify that the champ is better than previous candidates.
2008 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2009 if (!more_constrained (get_template_for_ordering (champ),
2010 get_template_for_ordering (c)))
2011 return NULL_TREE;
2012 }
2013
2014 return champ;
2015 }
2016
2017
2018 /* Returns the template (one of the functions given by TEMPLATE_ID)
2019 which can be specialized to match the indicated DECL with the
2020 explicit template args given in TEMPLATE_ID. The DECL may be
2021 NULL_TREE if none is available. In that case, the functions in
2022 TEMPLATE_ID are non-members.
2023
2024 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2025 specialization of a member template.
2026
2027 The TEMPLATE_COUNT is the number of references to qualifying
2028 template classes that appeared in the name of the function. See
2029 check_explicit_specialization for a more accurate description.
2030
2031 TSK indicates what kind of template declaration (if any) is being
2032 declared. TSK_TEMPLATE indicates that the declaration given by
2033 DECL, though a FUNCTION_DECL, has template parameters, and is
2034 therefore a template function.
2035
2036 The template args (those explicitly specified and those deduced)
2037 are output in a newly created vector *TARGS_OUT.
2038
2039 If it is impossible to determine the result, an error message is
2040 issued. The error_mark_node is returned to indicate failure. */
2041
2042 static tree
2043 determine_specialization (tree template_id,
2044 tree decl,
2045 tree* targs_out,
2046 int need_member_template,
2047 int template_count,
2048 tmpl_spec_kind tsk)
2049 {
2050 tree fns;
2051 tree targs;
2052 tree explicit_targs;
2053 tree candidates = NULL_TREE;
2054
2055 /* A TREE_LIST of templates of which DECL may be a specialization.
2056 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2057 corresponding TREE_PURPOSE is the set of template arguments that,
2058 when used to instantiate the template, would produce a function
2059 with the signature of DECL. */
2060 tree templates = NULL_TREE;
2061 int header_count;
2062 cp_binding_level *b;
2063
2064 *targs_out = NULL_TREE;
2065
2066 if (template_id == error_mark_node || decl == error_mark_node)
2067 return error_mark_node;
2068
2069 /* We shouldn't be specializing a member template of an
2070 unspecialized class template; we already gave an error in
2071 check_specialization_scope, now avoid crashing. */
2072 if (!VAR_P (decl)
2073 && template_count && DECL_CLASS_SCOPE_P (decl)
2074 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2075 {
2076 gcc_assert (errorcount);
2077 return error_mark_node;
2078 }
2079
2080 fns = TREE_OPERAND (template_id, 0);
2081 explicit_targs = TREE_OPERAND (template_id, 1);
2082
2083 if (fns == error_mark_node)
2084 return error_mark_node;
2085
2086 /* Check for baselinks. */
2087 if (BASELINK_P (fns))
2088 fns = BASELINK_FUNCTIONS (fns);
2089
2090 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2091 {
2092 error ("%qD is not a function template", fns);
2093 return error_mark_node;
2094 }
2095 else if (VAR_P (decl) && !variable_template_p (fns))
2096 {
2097 error ("%qD is not a variable template", fns);
2098 return error_mark_node;
2099 }
2100
2101 /* Count the number of template headers specified for this
2102 specialization. */
2103 header_count = 0;
2104 for (b = current_binding_level;
2105 b->kind == sk_template_parms;
2106 b = b->level_chain)
2107 ++header_count;
2108
2109 tree orig_fns = fns;
2110
2111 if (variable_template_p (fns))
2112 {
2113 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2114 targs = coerce_template_parms (parms, explicit_targs, fns,
2115 tf_warning_or_error,
2116 /*req_all*/true, /*use_defarg*/true);
2117 if (targs != error_mark_node)
2118 templates = tree_cons (targs, fns, templates);
2119 }
2120 else for (lkp_iterator iter (fns); iter; ++iter)
2121 {
2122 tree fn = *iter;
2123
2124 if (TREE_CODE (fn) == TEMPLATE_DECL)
2125 {
2126 tree decl_arg_types;
2127 tree fn_arg_types;
2128 tree insttype;
2129
2130 /* In case of explicit specialization, we need to check if
2131 the number of template headers appearing in the specialization
2132 is correct. This is usually done in check_explicit_specialization,
2133 but the check done there cannot be exhaustive when specializing
2134 member functions. Consider the following code:
2135
2136 template <> void A<int>::f(int);
2137 template <> template <> void A<int>::f(int);
2138
2139 Assuming that A<int> is not itself an explicit specialization
2140 already, the first line specializes "f" which is a non-template
2141 member function, whilst the second line specializes "f" which
2142 is a template member function. So both lines are syntactically
2143 correct, and check_explicit_specialization does not reject
2144 them.
2145
2146 Here, we can do better, as we are matching the specialization
2147 against the declarations. We count the number of template
2148 headers, and we check if they match TEMPLATE_COUNT + 1
2149 (TEMPLATE_COUNT is the number of qualifying template classes,
2150 plus there must be another header for the member template
2151 itself).
2152
2153 Notice that if header_count is zero, this is not a
2154 specialization but rather a template instantiation, so there
2155 is no check we can perform here. */
2156 if (header_count && header_count != template_count + 1)
2157 continue;
2158
2159 /* Check that the number of template arguments at the
2160 innermost level for DECL is the same as for FN. */
2161 if (current_binding_level->kind == sk_template_parms
2162 && !current_binding_level->explicit_spec_p
2163 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2164 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2165 (current_template_parms))))
2166 continue;
2167
2168 /* DECL might be a specialization of FN. */
2169 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2170 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2171
2172 /* For a non-static member function, we need to make sure
2173 that the const qualification is the same. Since
2174 get_bindings does not try to merge the "this" parameter,
2175 we must do the comparison explicitly. */
2176 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2177 {
2178 if (!same_type_p (TREE_VALUE (fn_arg_types),
2179 TREE_VALUE (decl_arg_types)))
2180 continue;
2181
2182 /* And the ref-qualification. */
2183 if (type_memfn_rqual (TREE_TYPE (decl))
2184 != type_memfn_rqual (TREE_TYPE (fn)))
2185 continue;
2186 }
2187
2188 /* Skip the "this" parameter and, for constructors of
2189 classes with virtual bases, the VTT parameter. A
2190 full specialization of a constructor will have a VTT
2191 parameter, but a template never will. */
2192 decl_arg_types
2193 = skip_artificial_parms_for (decl, decl_arg_types);
2194 fn_arg_types
2195 = skip_artificial_parms_for (fn, fn_arg_types);
2196
2197 /* Function templates cannot be specializations; there are
2198 no partial specializations of functions. Therefore, if
2199 the type of DECL does not match FN, there is no
2200 match.
2201
2202 Note that it should never be the case that we have both
2203 candidates added here, and for regular member functions
2204 below. */
2205 if (tsk == tsk_template)
2206 {
2207 if (compparms (fn_arg_types, decl_arg_types))
2208 candidates = tree_cons (NULL_TREE, fn, candidates);
2209 continue;
2210 }
2211
2212 /* See whether this function might be a specialization of this
2213 template. Suppress access control because we might be trying
2214 to make this specialization a friend, and we have already done
2215 access control for the declaration of the specialization. */
2216 push_deferring_access_checks (dk_no_check);
2217 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2218 pop_deferring_access_checks ();
2219
2220 if (!targs)
2221 /* We cannot deduce template arguments that when used to
2222 specialize TMPL will produce DECL. */
2223 continue;
2224
2225 if (uses_template_parms (targs))
2226 /* We deduced something involving 'auto', which isn't a valid
2227 template argument. */
2228 continue;
2229
2230 /* Remove, from the set of candidates, all those functions
2231 whose constraints are not satisfied. */
2232 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2233 continue;
2234
2235 // Then, try to form the new function type.
2236 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2237 if (insttype == error_mark_node)
2238 continue;
2239 fn_arg_types
2240 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2241 if (!compparms (fn_arg_types, decl_arg_types))
2242 continue;
2243
2244 /* Save this template, and the arguments deduced. */
2245 templates = tree_cons (targs, fn, templates);
2246 }
2247 else if (need_member_template)
2248 /* FN is an ordinary member function, and we need a
2249 specialization of a member template. */
2250 ;
2251 else if (TREE_CODE (fn) != FUNCTION_DECL)
2252 /* We can get IDENTIFIER_NODEs here in certain erroneous
2253 cases. */
2254 ;
2255 else if (!DECL_FUNCTION_MEMBER_P (fn))
2256 /* This is just an ordinary non-member function. Nothing can
2257 be a specialization of that. */
2258 ;
2259 else if (DECL_ARTIFICIAL (fn))
2260 /* Cannot specialize functions that are created implicitly. */
2261 ;
2262 else
2263 {
2264 tree decl_arg_types;
2265
2266 /* This is an ordinary member function. However, since
2267 we're here, we can assume its enclosing class is a
2268 template class. For example,
2269
2270 template <typename T> struct S { void f(); };
2271 template <> void S<int>::f() {}
2272
2273 Here, S<int>::f is a non-template, but S<int> is a
2274 template class. If FN has the same type as DECL, we
2275 might be in business. */
2276
2277 if (!DECL_TEMPLATE_INFO (fn))
2278 /* Its enclosing class is an explicit specialization
2279 of a template class. This is not a candidate. */
2280 continue;
2281
2282 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2283 TREE_TYPE (TREE_TYPE (fn))))
2284 /* The return types differ. */
2285 continue;
2286
2287 /* Adjust the type of DECL in case FN is a static member. */
2288 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2289 if (DECL_STATIC_FUNCTION_P (fn)
2290 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2291 decl_arg_types = TREE_CHAIN (decl_arg_types);
2292
2293 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2294 decl_arg_types))
2295 continue;
2296
2297 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2298 && (type_memfn_rqual (TREE_TYPE (decl))
2299 != type_memfn_rqual (TREE_TYPE (fn))))
2300 continue;
2301
2302 // If the deduced arguments do not satisfy the constraints,
2303 // this is not a candidate.
2304 if (flag_concepts && !constraints_satisfied_p (fn))
2305 continue;
2306
2307 // Add the candidate.
2308 candidates = tree_cons (NULL_TREE, fn, candidates);
2309 }
2310 }
2311
2312 if (templates && TREE_CHAIN (templates))
2313 {
2314 /* We have:
2315
2316 [temp.expl.spec]
2317
2318 It is possible for a specialization with a given function
2319 signature to be instantiated from more than one function
2320 template. In such cases, explicit specification of the
2321 template arguments must be used to uniquely identify the
2322 function template specialization being specialized.
2323
2324 Note that here, there's no suggestion that we're supposed to
2325 determine which of the candidate templates is most
2326 specialized. However, we, also have:
2327
2328 [temp.func.order]
2329
2330 Partial ordering of overloaded function template
2331 declarations is used in the following contexts to select
2332 the function template to which a function template
2333 specialization refers:
2334
2335 -- when an explicit specialization refers to a function
2336 template.
2337
2338 So, we do use the partial ordering rules, at least for now.
2339 This extension can only serve to make invalid programs valid,
2340 so it's safe. And, there is strong anecdotal evidence that
2341 the committee intended the partial ordering rules to apply;
2342 the EDG front end has that behavior, and John Spicer claims
2343 that the committee simply forgot to delete the wording in
2344 [temp.expl.spec]. */
2345 tree tmpl = most_specialized_instantiation (templates);
2346 if (tmpl != error_mark_node)
2347 {
2348 templates = tmpl;
2349 TREE_CHAIN (templates) = NULL_TREE;
2350 }
2351 }
2352
2353 // Concepts allows multiple declarations of member functions
2354 // with the same signature. Like above, we need to rely on
2355 // on the partial ordering of those candidates to determine which
2356 // is the best.
2357 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2358 {
2359 if (tree cand = most_constrained_function (candidates))
2360 {
2361 candidates = cand;
2362 TREE_CHAIN (cand) = NULL_TREE;
2363 }
2364 }
2365
2366 if (templates == NULL_TREE && candidates == NULL_TREE)
2367 {
2368 error ("template-id %qD for %q+D does not match any template "
2369 "declaration", template_id, decl);
2370 if (header_count && header_count != template_count + 1)
2371 inform (input_location, "saw %d %<template<>%>, need %d for "
2372 "specializing a member function template",
2373 header_count, template_count + 1);
2374 else
2375 print_candidates (orig_fns);
2376 return error_mark_node;
2377 }
2378 else if ((templates && TREE_CHAIN (templates))
2379 || (candidates && TREE_CHAIN (candidates))
2380 || (templates && candidates))
2381 {
2382 error ("ambiguous template specialization %qD for %q+D",
2383 template_id, decl);
2384 candidates = chainon (candidates, templates);
2385 print_candidates (candidates);
2386 return error_mark_node;
2387 }
2388
2389 /* We have one, and exactly one, match. */
2390 if (candidates)
2391 {
2392 tree fn = TREE_VALUE (candidates);
2393 *targs_out = copy_node (DECL_TI_ARGS (fn));
2394
2395 // Propagate the candidate's constraints to the declaration.
2396 set_constraints (decl, get_constraints (fn));
2397
2398 /* DECL is a re-declaration or partial instantiation of a template
2399 function. */
2400 if (TREE_CODE (fn) == TEMPLATE_DECL)
2401 return fn;
2402 /* It was a specialization of an ordinary member function in a
2403 template class. */
2404 return DECL_TI_TEMPLATE (fn);
2405 }
2406
2407 /* It was a specialization of a template. */
2408 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2409 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2410 {
2411 *targs_out = copy_node (targs);
2412 SET_TMPL_ARGS_LEVEL (*targs_out,
2413 TMPL_ARGS_DEPTH (*targs_out),
2414 TREE_PURPOSE (templates));
2415 }
2416 else
2417 *targs_out = TREE_PURPOSE (templates);
2418 return TREE_VALUE (templates);
2419 }
2420
2421 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2422 but with the default argument values filled in from those in the
2423 TMPL_TYPES. */
2424
2425 static tree
2426 copy_default_args_to_explicit_spec_1 (tree spec_types,
2427 tree tmpl_types)
2428 {
2429 tree new_spec_types;
2430
2431 if (!spec_types)
2432 return NULL_TREE;
2433
2434 if (spec_types == void_list_node)
2435 return void_list_node;
2436
2437 /* Substitute into the rest of the list. */
2438 new_spec_types =
2439 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2440 TREE_CHAIN (tmpl_types));
2441
2442 /* Add the default argument for this parameter. */
2443 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2444 TREE_VALUE (spec_types),
2445 new_spec_types);
2446 }
2447
2448 /* DECL is an explicit specialization. Replicate default arguments
2449 from the template it specializes. (That way, code like:
2450
2451 template <class T> void f(T = 3);
2452 template <> void f(double);
2453 void g () { f (); }
2454
2455 works, as required.) An alternative approach would be to look up
2456 the correct default arguments at the call-site, but this approach
2457 is consistent with how implicit instantiations are handled. */
2458
2459 static void
2460 copy_default_args_to_explicit_spec (tree decl)
2461 {
2462 tree tmpl;
2463 tree spec_types;
2464 tree tmpl_types;
2465 tree new_spec_types;
2466 tree old_type;
2467 tree new_type;
2468 tree t;
2469 tree object_type = NULL_TREE;
2470 tree in_charge = NULL_TREE;
2471 tree vtt = NULL_TREE;
2472
2473 /* See if there's anything we need to do. */
2474 tmpl = DECL_TI_TEMPLATE (decl);
2475 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2476 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2477 if (TREE_PURPOSE (t))
2478 break;
2479 if (!t)
2480 return;
2481
2482 old_type = TREE_TYPE (decl);
2483 spec_types = TYPE_ARG_TYPES (old_type);
2484
2485 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2486 {
2487 /* Remove the this pointer, but remember the object's type for
2488 CV quals. */
2489 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2490 spec_types = TREE_CHAIN (spec_types);
2491 tmpl_types = TREE_CHAIN (tmpl_types);
2492
2493 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2494 {
2495 /* DECL may contain more parameters than TMPL due to the extra
2496 in-charge parameter in constructors and destructors. */
2497 in_charge = spec_types;
2498 spec_types = TREE_CHAIN (spec_types);
2499 }
2500 if (DECL_HAS_VTT_PARM_P (decl))
2501 {
2502 vtt = spec_types;
2503 spec_types = TREE_CHAIN (spec_types);
2504 }
2505 }
2506
2507 /* Compute the merged default arguments. */
2508 new_spec_types =
2509 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2510
2511 /* Compute the new FUNCTION_TYPE. */
2512 if (object_type)
2513 {
2514 if (vtt)
2515 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2516 TREE_VALUE (vtt),
2517 new_spec_types);
2518
2519 if (in_charge)
2520 /* Put the in-charge parameter back. */
2521 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2522 TREE_VALUE (in_charge),
2523 new_spec_types);
2524
2525 new_type = build_method_type_directly (object_type,
2526 TREE_TYPE (old_type),
2527 new_spec_types);
2528 }
2529 else
2530 new_type = build_function_type (TREE_TYPE (old_type),
2531 new_spec_types);
2532 new_type = cp_build_type_attribute_variant (new_type,
2533 TYPE_ATTRIBUTES (old_type));
2534 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2535
2536 TREE_TYPE (decl) = new_type;
2537 }
2538
2539 /* Return the number of template headers we expect to see for a definition
2540 or specialization of CTYPE or one of its non-template members. */
2541
2542 int
2543 num_template_headers_for_class (tree ctype)
2544 {
2545 int num_templates = 0;
2546
2547 while (ctype && CLASS_TYPE_P (ctype))
2548 {
2549 /* You're supposed to have one `template <...>' for every
2550 template class, but you don't need one for a full
2551 specialization. For example:
2552
2553 template <class T> struct S{};
2554 template <> struct S<int> { void f(); };
2555 void S<int>::f () {}
2556
2557 is correct; there shouldn't be a `template <>' for the
2558 definition of `S<int>::f'. */
2559 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2560 /* If CTYPE does not have template information of any
2561 kind, then it is not a template, nor is it nested
2562 within a template. */
2563 break;
2564 if (explicit_class_specialization_p (ctype))
2565 break;
2566 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2567 ++num_templates;
2568
2569 ctype = TYPE_CONTEXT (ctype);
2570 }
2571
2572 return num_templates;
2573 }
2574
2575 /* Do a simple sanity check on the template headers that precede the
2576 variable declaration DECL. */
2577
2578 void
2579 check_template_variable (tree decl)
2580 {
2581 tree ctx = CP_DECL_CONTEXT (decl);
2582 int wanted = num_template_headers_for_class (ctx);
2583 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2584 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2585 {
2586 if (cxx_dialect < cxx14)
2587 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2588 "variable templates only available with "
2589 "-std=c++14 or -std=gnu++14");
2590
2591 // Namespace-scope variable templates should have a template header.
2592 ++wanted;
2593 }
2594 if (template_header_count > wanted)
2595 {
2596 auto_diagnostic_group d;
2597 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2598 "too many template headers for %qD "
2599 "(should be %d)",
2600 decl, wanted);
2601 if (warned && CLASS_TYPE_P (ctx)
2602 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2603 inform (DECL_SOURCE_LOCATION (decl),
2604 "members of an explicitly specialized class are defined "
2605 "without a template header");
2606 }
2607 }
2608
2609 /* An explicit specialization whose declarator-id or class-head-name is not
2610 qualified shall be declared in the nearest enclosing namespace of the
2611 template, or, if the namespace is inline (7.3.1), any namespace from its
2612 enclosing namespace set.
2613
2614 If the name declared in the explicit instantiation is an unqualified name,
2615 the explicit instantiation shall appear in the namespace where its template
2616 is declared or, if that namespace is inline (7.3.1), any namespace from its
2617 enclosing namespace set. */
2618
2619 void
2620 check_unqualified_spec_or_inst (tree t, location_t loc)
2621 {
2622 tree tmpl = most_general_template (t);
2623 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2624 && !is_nested_namespace (current_namespace,
2625 CP_DECL_CONTEXT (tmpl), true))
2626 {
2627 if (processing_specialization)
2628 permerror (loc, "explicit specialization of %qD outside its "
2629 "namespace must use a nested-name-specifier", tmpl);
2630 else if (processing_explicit_instantiation
2631 && cxx_dialect >= cxx11)
2632 /* This was allowed in C++98, so only pedwarn. */
2633 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2634 "outside its namespace must use a nested-name-"
2635 "specifier", tmpl);
2636 }
2637 }
2638
2639 /* Warn for a template specialization SPEC that is missing some of a set
2640 of function or type attributes that the template TEMPL is declared with.
2641 ATTRLIST is a list of additional attributes that SPEC should be taken
2642 to ultimately be declared with. */
2643
2644 static void
2645 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2646 {
2647 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2648 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2649
2650 if (TREE_CODE (tmpl) != FUNCTION_DECL)
2651 return;
2652
2653 /* Avoid warning if either declaration or its type is deprecated. */
2654 if (TREE_DEPRECATED (tmpl)
2655 || TREE_DEPRECATED (spec))
2656 return;
2657
2658 tree tmpl_type = TREE_TYPE (tmpl);
2659 tree spec_type = TREE_TYPE (spec);
2660
2661 if (TREE_DEPRECATED (tmpl_type)
2662 || TREE_DEPRECATED (spec_type)
2663 || TREE_DEPRECATED (TREE_TYPE (tmpl_type))
2664 || TREE_DEPRECATED (TREE_TYPE (spec_type)))
2665 return;
2666
2667 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpl_type) };
2668 tree spec_attrs[] = { DECL_ATTRIBUTES (spec), TYPE_ATTRIBUTES (spec_type) };
2669
2670 if (!spec_attrs[0])
2671 spec_attrs[0] = attrlist;
2672 else if (!spec_attrs[1])
2673 spec_attrs[1] = attrlist;
2674
2675 /* Avoid warning if the primary has no attributes. */
2676 if (!tmpl_attrs[0] && !tmpl_attrs[1])
2677 return;
2678
2679 /* Avoid warning if either declaration contains an attribute on
2680 the white list below. */
2681 const char* const whitelist[] = {
2682 "error", "warning"
2683 };
2684
2685 for (unsigned i = 0; i != 2; ++i)
2686 for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j)
2687 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
2688 || lookup_attribute (whitelist[j], spec_attrs[i]))
2689 return;
2690
2691 /* Avoid warning if the difference between the primary and
2692 the specialization is not in one of the attributes below. */
2693 const char* const blacklist[] = {
2694 "alloc_align", "alloc_size", "assume_aligned", "format",
2695 "format_arg", "malloc", "nonnull"
2696 };
2697
2698 /* Put together a list of the black listed attributes that the primary
2699 template is declared with that the specialization is not, in case
2700 it's not apparent from the most recent declaration of the primary. */
2701 unsigned nattrs = 0;
2702 pretty_printer str;
2703
2704 for (unsigned i = 0; i != sizeof blacklist / sizeof *blacklist; ++i)
2705 {
2706 for (unsigned j = 0; j != 2; ++j)
2707 {
2708 if (!lookup_attribute (blacklist[i], tmpl_attrs[j]))
2709 continue;
2710
2711 for (unsigned k = 0; k != 1 + !!spec_attrs[1]; ++k)
2712 {
2713 if (lookup_attribute (blacklist[i], spec_attrs[k]))
2714 break;
2715
2716 if (nattrs)
2717 pp_string (&str, ", ");
2718 pp_begin_quote (&str, pp_show_color (global_dc->printer));
2719 pp_string (&str, blacklist[i]);
2720 pp_end_quote (&str, pp_show_color (global_dc->printer));
2721 ++nattrs;
2722 }
2723 }
2724 }
2725
2726 if (!nattrs)
2727 return;
2728
2729 auto_diagnostic_group d;
2730 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2731 "explicit specialization %q#D may be missing attributes",
2732 spec))
2733 inform (DECL_SOURCE_LOCATION (tmpl),
2734 nattrs > 1
2735 ? G_("missing primary template attributes %s")
2736 : G_("missing primary template attribute %s"),
2737 pp_formatted_text (&str));
2738 }
2739
2740 /* Check to see if the function just declared, as indicated in
2741 DECLARATOR, and in DECL, is a specialization of a function
2742 template. We may also discover that the declaration is an explicit
2743 instantiation at this point.
2744
2745 Returns DECL, or an equivalent declaration that should be used
2746 instead if all goes well. Issues an error message if something is
2747 amiss. Returns error_mark_node if the error is not easily
2748 recoverable.
2749
2750 FLAGS is a bitmask consisting of the following flags:
2751
2752 2: The function has a definition.
2753 4: The function is a friend.
2754
2755 The TEMPLATE_COUNT is the number of references to qualifying
2756 template classes that appeared in the name of the function. For
2757 example, in
2758
2759 template <class T> struct S { void f(); };
2760 void S<int>::f();
2761
2762 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2763 classes are not counted in the TEMPLATE_COUNT, so that in
2764
2765 template <class T> struct S {};
2766 template <> struct S<int> { void f(); }
2767 template <> void S<int>::f();
2768
2769 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2770 invalid; there should be no template <>.)
2771
2772 If the function is a specialization, it is marked as such via
2773 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2774 is set up correctly, and it is added to the list of specializations
2775 for that template. */
2776
2777 tree
2778 check_explicit_specialization (tree declarator,
2779 tree decl,
2780 int template_count,
2781 int flags,
2782 tree attrlist)
2783 {
2784 int have_def = flags & 2;
2785 int is_friend = flags & 4;
2786 bool is_concept = flags & 8;
2787 int specialization = 0;
2788 int explicit_instantiation = 0;
2789 int member_specialization = 0;
2790 tree ctype = DECL_CLASS_CONTEXT (decl);
2791 tree dname = DECL_NAME (decl);
2792 tmpl_spec_kind tsk;
2793
2794 if (is_friend)
2795 {
2796 if (!processing_specialization)
2797 tsk = tsk_none;
2798 else
2799 tsk = tsk_excessive_parms;
2800 }
2801 else
2802 tsk = current_tmpl_spec_kind (template_count);
2803
2804 switch (tsk)
2805 {
2806 case tsk_none:
2807 if (processing_specialization && !VAR_P (decl))
2808 {
2809 specialization = 1;
2810 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2811 }
2812 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2813 {
2814 if (is_friend)
2815 /* This could be something like:
2816
2817 template <class T> void f(T);
2818 class S { friend void f<>(int); } */
2819 specialization = 1;
2820 else
2821 {
2822 /* This case handles bogus declarations like template <>
2823 template <class T> void f<int>(); */
2824
2825 error ("template-id %qD in declaration of primary template",
2826 declarator);
2827 return decl;
2828 }
2829 }
2830 break;
2831
2832 case tsk_invalid_member_spec:
2833 /* The error has already been reported in
2834 check_specialization_scope. */
2835 return error_mark_node;
2836
2837 case tsk_invalid_expl_inst:
2838 error ("template parameter list used in explicit instantiation");
2839
2840 /* Fall through. */
2841
2842 case tsk_expl_inst:
2843 if (have_def)
2844 error ("definition provided for explicit instantiation");
2845
2846 explicit_instantiation = 1;
2847 break;
2848
2849 case tsk_excessive_parms:
2850 case tsk_insufficient_parms:
2851 if (tsk == tsk_excessive_parms)
2852 error ("too many template parameter lists in declaration of %qD",
2853 decl);
2854 else if (template_header_count)
2855 error("too few template parameter lists in declaration of %qD", decl);
2856 else
2857 error("explicit specialization of %qD must be introduced by "
2858 "%<template <>%>", decl);
2859
2860 /* Fall through. */
2861 case tsk_expl_spec:
2862 if (is_concept)
2863 error ("explicit specialization declared %<concept%>");
2864
2865 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2866 /* In cases like template<> constexpr bool v = true;
2867 We'll give an error in check_template_variable. */
2868 break;
2869
2870 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2871 if (ctype)
2872 member_specialization = 1;
2873 else
2874 specialization = 1;
2875 break;
2876
2877 case tsk_template:
2878 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2879 {
2880 /* This case handles bogus declarations like template <>
2881 template <class T> void f<int>(); */
2882
2883 if (!uses_template_parms (declarator))
2884 error ("template-id %qD in declaration of primary template",
2885 declarator);
2886 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2887 {
2888 /* Partial specialization of variable template. */
2889 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2890 specialization = 1;
2891 goto ok;
2892 }
2893 else if (cxx_dialect < cxx14)
2894 error ("non-type partial specialization %qD "
2895 "is not allowed", declarator);
2896 else
2897 error ("non-class, non-variable partial specialization %qD "
2898 "is not allowed", declarator);
2899 return decl;
2900 ok:;
2901 }
2902
2903 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2904 /* This is a specialization of a member template, without
2905 specialization the containing class. Something like:
2906
2907 template <class T> struct S {
2908 template <class U> void f (U);
2909 };
2910 template <> template <class U> void S<int>::f(U) {}
2911
2912 That's a specialization -- but of the entire template. */
2913 specialization = 1;
2914 break;
2915
2916 default:
2917 gcc_unreachable ();
2918 }
2919
2920 if ((specialization || member_specialization)
2921 /* This doesn't apply to variable templates. */
2922 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2923 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2924 {
2925 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2926 for (; t; t = TREE_CHAIN (t))
2927 if (TREE_PURPOSE (t))
2928 {
2929 permerror (input_location,
2930 "default argument specified in explicit specialization");
2931 break;
2932 }
2933 }
2934
2935 if (specialization || member_specialization || explicit_instantiation)
2936 {
2937 tree tmpl = NULL_TREE;
2938 tree targs = NULL_TREE;
2939 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2940
2941 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2942 if (!was_template_id)
2943 {
2944 tree fns;
2945
2946 gcc_assert (identifier_p (declarator));
2947 if (ctype)
2948 fns = dname;
2949 else
2950 {
2951 /* If there is no class context, the explicit instantiation
2952 must be at namespace scope. */
2953 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2954
2955 /* Find the namespace binding, using the declaration
2956 context. */
2957 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2958 false, true);
2959 if (fns == error_mark_node)
2960 /* If lookup fails, look for a friend declaration so we can
2961 give a better diagnostic. */
2962 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2963 /*type*/false, /*complain*/true,
2964 /*hidden*/true);
2965
2966 if (fns == error_mark_node || !is_overloaded_fn (fns))
2967 {
2968 error ("%qD is not a template function", dname);
2969 fns = error_mark_node;
2970 }
2971 }
2972
2973 declarator = lookup_template_function (fns, NULL_TREE);
2974 }
2975
2976 if (declarator == error_mark_node)
2977 return error_mark_node;
2978
2979 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2980 {
2981 if (!explicit_instantiation)
2982 /* A specialization in class scope. This is invalid,
2983 but the error will already have been flagged by
2984 check_specialization_scope. */
2985 return error_mark_node;
2986 else
2987 {
2988 /* It's not valid to write an explicit instantiation in
2989 class scope, e.g.:
2990
2991 class C { template void f(); }
2992
2993 This case is caught by the parser. However, on
2994 something like:
2995
2996 template class C { void f(); };
2997
2998 (which is invalid) we can get here. The error will be
2999 issued later. */
3000 ;
3001 }
3002
3003 return decl;
3004 }
3005 else if (ctype != NULL_TREE
3006 && (identifier_p (TREE_OPERAND (declarator, 0))))
3007 {
3008 // We'll match variable templates in start_decl.
3009 if (VAR_P (decl))
3010 return decl;
3011
3012 /* Find the list of functions in ctype that have the same
3013 name as the declared function. */
3014 tree name = TREE_OPERAND (declarator, 0);
3015
3016 if (constructor_name_p (name, ctype))
3017 {
3018 if (DECL_CONSTRUCTOR_P (decl)
3019 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3020 : !CLASSTYPE_DESTRUCTOR (ctype))
3021 {
3022 /* From [temp.expl.spec]:
3023
3024 If such an explicit specialization for the member
3025 of a class template names an implicitly-declared
3026 special member function (clause _special_), the
3027 program is ill-formed.
3028
3029 Similar language is found in [temp.explicit]. */
3030 error ("specialization of implicitly-declared special member function");
3031 return error_mark_node;
3032 }
3033
3034 name = DECL_NAME (decl);
3035 }
3036
3037 /* For a type-conversion operator, We might be looking for
3038 `operator int' which will be a specialization of
3039 `operator T'. Grab all the conversion operators, and
3040 then select from them. */
3041 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3042 ? conv_op_identifier : name);
3043
3044 if (fns == NULL_TREE)
3045 {
3046 error ("no member function %qD declared in %qT", name, ctype);
3047 return error_mark_node;
3048 }
3049 else
3050 TREE_OPERAND (declarator, 0) = fns;
3051 }
3052
3053 /* Figure out what exactly is being specialized at this point.
3054 Note that for an explicit instantiation, even one for a
3055 member function, we cannot tell a priori whether the
3056 instantiation is for a member template, or just a member
3057 function of a template class. Even if a member template is
3058 being instantiated, the member template arguments may be
3059 elided if they can be deduced from the rest of the
3060 declaration. */
3061 tmpl = determine_specialization (declarator, decl,
3062 &targs,
3063 member_specialization,
3064 template_count,
3065 tsk);
3066
3067 if (!tmpl || tmpl == error_mark_node)
3068 /* We couldn't figure out what this declaration was
3069 specializing. */
3070 return error_mark_node;
3071 else
3072 {
3073 if (TREE_CODE (decl) == FUNCTION_DECL
3074 && DECL_HIDDEN_FRIEND_P (tmpl))
3075 {
3076 auto_diagnostic_group d;
3077 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3078 "friend declaration %qD is not visible to "
3079 "explicit specialization", tmpl))
3080 inform (DECL_SOURCE_LOCATION (tmpl),
3081 "friend declaration here");
3082 }
3083 else if (!ctype && !is_friend
3084 && CP_DECL_CONTEXT (decl) == current_namespace)
3085 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3086
3087 tree gen_tmpl = most_general_template (tmpl);
3088
3089 if (explicit_instantiation)
3090 {
3091 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3092 is done by do_decl_instantiation later. */
3093
3094 int arg_depth = TMPL_ARGS_DEPTH (targs);
3095 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3096
3097 if (arg_depth > parm_depth)
3098 {
3099 /* If TMPL is not the most general template (for
3100 example, if TMPL is a friend template that is
3101 injected into namespace scope), then there will
3102 be too many levels of TARGS. Remove some of them
3103 here. */
3104 int i;
3105 tree new_targs;
3106
3107 new_targs = make_tree_vec (parm_depth);
3108 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3109 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3110 = TREE_VEC_ELT (targs, i);
3111 targs = new_targs;
3112 }
3113
3114 return instantiate_template (tmpl, targs, tf_error);
3115 }
3116
3117 /* If we thought that the DECL was a member function, but it
3118 turns out to be specializing a static member function,
3119 make DECL a static member function as well. */
3120 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3121 && DECL_STATIC_FUNCTION_P (tmpl)
3122 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3123 revert_static_member_fn (decl);
3124
3125 /* If this is a specialization of a member template of a
3126 template class, we want to return the TEMPLATE_DECL, not
3127 the specialization of it. */
3128 if (tsk == tsk_template && !was_template_id)
3129 {
3130 tree result = DECL_TEMPLATE_RESULT (tmpl);
3131 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3132 DECL_INITIAL (result) = NULL_TREE;
3133 if (have_def)
3134 {
3135 tree parm;
3136 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3137 DECL_SOURCE_LOCATION (result)
3138 = DECL_SOURCE_LOCATION (decl);
3139 /* We want to use the argument list specified in the
3140 definition, not in the original declaration. */
3141 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3142 for (parm = DECL_ARGUMENTS (result); parm;
3143 parm = DECL_CHAIN (parm))
3144 DECL_CONTEXT (parm) = result;
3145 }
3146 return register_specialization (tmpl, gen_tmpl, targs,
3147 is_friend, 0);
3148 }
3149
3150 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3151 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3152
3153 if (was_template_id)
3154 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3155
3156 /* Inherit default function arguments from the template
3157 DECL is specializing. */
3158 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3159 copy_default_args_to_explicit_spec (decl);
3160
3161 /* This specialization has the same protection as the
3162 template it specializes. */
3163 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3164 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3165
3166 /* 7.1.1-1 [dcl.stc]
3167
3168 A storage-class-specifier shall not be specified in an
3169 explicit specialization...
3170
3171 The parser rejects these, so unless action is taken here,
3172 explicit function specializations will always appear with
3173 global linkage.
3174
3175 The action recommended by the C++ CWG in response to C++
3176 defect report 605 is to make the storage class and linkage
3177 of the explicit specialization match the templated function:
3178
3179 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3180 */
3181 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3182 {
3183 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3184 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3185
3186 /* A concept cannot be specialized. */
3187 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3188 {
3189 error ("explicit specialization of function concept %qD",
3190 gen_tmpl);
3191 return error_mark_node;
3192 }
3193
3194 /* This specialization has the same linkage and visibility as
3195 the function template it specializes. */
3196 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3197 if (! TREE_PUBLIC (decl))
3198 {
3199 DECL_INTERFACE_KNOWN (decl) = 1;
3200 DECL_NOT_REALLY_EXTERN (decl) = 1;
3201 }
3202 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3203 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3204 {
3205 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3206 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3207 }
3208 }
3209
3210 /* If DECL is a friend declaration, declared using an
3211 unqualified name, the namespace associated with DECL may
3212 have been set incorrectly. For example, in:
3213
3214 template <typename T> void f(T);
3215 namespace N {
3216 struct S { friend void f<int>(int); }
3217 }
3218
3219 we will have set the DECL_CONTEXT for the friend
3220 declaration to N, rather than to the global namespace. */
3221 if (DECL_NAMESPACE_SCOPE_P (decl))
3222 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3223
3224 if (is_friend && !have_def)
3225 /* This is not really a declaration of a specialization.
3226 It's just the name of an instantiation. But, it's not
3227 a request for an instantiation, either. */
3228 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3229 else if (TREE_CODE (decl) == FUNCTION_DECL)
3230 /* A specialization is not necessarily COMDAT. */
3231 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3232 && DECL_DECLARED_INLINE_P (decl));
3233 else if (VAR_P (decl))
3234 DECL_COMDAT (decl) = false;
3235
3236 /* If this is a full specialization, register it so that we can find
3237 it again. Partial specializations will be registered in
3238 process_partial_specialization. */
3239 if (!processing_template_decl)
3240 {
3241 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3242
3243 decl = register_specialization (decl, gen_tmpl, targs,
3244 is_friend, 0);
3245 }
3246
3247
3248 /* A 'structor should already have clones. */
3249 gcc_assert (decl == error_mark_node
3250 || variable_template_p (tmpl)
3251 || !(DECL_CONSTRUCTOR_P (decl)
3252 || DECL_DESTRUCTOR_P (decl))
3253 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3254 }
3255 }
3256
3257 return decl;
3258 }
3259
3260 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3261 parameters. These are represented in the same format used for
3262 DECL_TEMPLATE_PARMS. */
3263
3264 int
3265 comp_template_parms (const_tree parms1, const_tree parms2)
3266 {
3267 const_tree p1;
3268 const_tree p2;
3269
3270 if (parms1 == parms2)
3271 return 1;
3272
3273 for (p1 = parms1, p2 = parms2;
3274 p1 != NULL_TREE && p2 != NULL_TREE;
3275 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3276 {
3277 tree t1 = TREE_VALUE (p1);
3278 tree t2 = TREE_VALUE (p2);
3279 int i;
3280
3281 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3282 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3283
3284 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3285 return 0;
3286
3287 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3288 {
3289 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3290 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3291
3292 /* If either of the template parameters are invalid, assume
3293 they match for the sake of error recovery. */
3294 if (error_operand_p (parm1) || error_operand_p (parm2))
3295 return 1;
3296
3297 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3298 return 0;
3299
3300 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3301 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3302 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3303 continue;
3304 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3305 return 0;
3306 }
3307 }
3308
3309 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3310 /* One set of parameters has more parameters lists than the
3311 other. */
3312 return 0;
3313
3314 return 1;
3315 }
3316
3317 /* Determine whether PARM is a parameter pack. */
3318
3319 bool
3320 template_parameter_pack_p (const_tree parm)
3321 {
3322 /* Determine if we have a non-type template parameter pack. */
3323 if (TREE_CODE (parm) == PARM_DECL)
3324 return (DECL_TEMPLATE_PARM_P (parm)
3325 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3326 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3327 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3328
3329 /* If this is a list of template parameters, we could get a
3330 TYPE_DECL or a TEMPLATE_DECL. */
3331 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3332 parm = TREE_TYPE (parm);
3333
3334 /* Otherwise it must be a type template parameter. */
3335 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3336 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3337 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3338 }
3339
3340 /* Determine if T is a function parameter pack. */
3341
3342 bool
3343 function_parameter_pack_p (const_tree t)
3344 {
3345 if (t && TREE_CODE (t) == PARM_DECL)
3346 return DECL_PACK_P (t);
3347 return false;
3348 }
3349
3350 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3351 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3352
3353 tree
3354 get_function_template_decl (const_tree primary_func_tmpl_inst)
3355 {
3356 if (! primary_func_tmpl_inst
3357 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3358 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3359 return NULL;
3360
3361 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3362 }
3363
3364 /* Return true iff the function parameter PARAM_DECL was expanded
3365 from the function parameter pack PACK. */
3366
3367 bool
3368 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3369 {
3370 if (DECL_ARTIFICIAL (param_decl)
3371 || !function_parameter_pack_p (pack))
3372 return false;
3373
3374 /* The parameter pack and its pack arguments have the same
3375 DECL_PARM_INDEX. */
3376 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3377 }
3378
3379 /* Determine whether ARGS describes a variadic template args list,
3380 i.e., one that is terminated by a template argument pack. */
3381
3382 static bool
3383 template_args_variadic_p (tree args)
3384 {
3385 int nargs;
3386 tree last_parm;
3387
3388 if (args == NULL_TREE)
3389 return false;
3390
3391 args = INNERMOST_TEMPLATE_ARGS (args);
3392 nargs = TREE_VEC_LENGTH (args);
3393
3394 if (nargs == 0)
3395 return false;
3396
3397 last_parm = TREE_VEC_ELT (args, nargs - 1);
3398
3399 return ARGUMENT_PACK_P (last_parm);
3400 }
3401
3402 /* Generate a new name for the parameter pack name NAME (an
3403 IDENTIFIER_NODE) that incorporates its */
3404
3405 static tree
3406 make_ith_pack_parameter_name (tree name, int i)
3407 {
3408 /* Munge the name to include the parameter index. */
3409 #define NUMBUF_LEN 128
3410 char numbuf[NUMBUF_LEN];
3411 char* newname;
3412 int newname_len;
3413
3414 if (name == NULL_TREE)
3415 return name;
3416 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3417 newname_len = IDENTIFIER_LENGTH (name)
3418 + strlen (numbuf) + 2;
3419 newname = (char*)alloca (newname_len);
3420 snprintf (newname, newname_len,
3421 "%s#%i", IDENTIFIER_POINTER (name), i);
3422 return get_identifier (newname);
3423 }
3424
3425 /* Return true if T is a primary function, class or alias template
3426 specialization, not including the template pattern. */
3427
3428 bool
3429 primary_template_specialization_p (const_tree t)
3430 {
3431 if (!t)
3432 return false;
3433
3434 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3435 return (DECL_LANG_SPECIFIC (t)
3436 && DECL_USE_TEMPLATE (t)
3437 && DECL_TEMPLATE_INFO (t)
3438 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3439 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3440 return (CLASSTYPE_TEMPLATE_INFO (t)
3441 && CLASSTYPE_USE_TEMPLATE (t)
3442 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3443 else if (alias_template_specialization_p (t))
3444 return true;
3445 return false;
3446 }
3447
3448 /* Return true if PARM is a template template parameter. */
3449
3450 bool
3451 template_template_parameter_p (const_tree parm)
3452 {
3453 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3454 }
3455
3456 /* Return true iff PARM is a DECL representing a type template
3457 parameter. */
3458
3459 bool
3460 template_type_parameter_p (const_tree parm)
3461 {
3462 return (parm
3463 && (TREE_CODE (parm) == TYPE_DECL
3464 || TREE_CODE (parm) == TEMPLATE_DECL)
3465 && DECL_TEMPLATE_PARM_P (parm));
3466 }
3467
3468 /* Return the template parameters of T if T is a
3469 primary template instantiation, NULL otherwise. */
3470
3471 tree
3472 get_primary_template_innermost_parameters (const_tree t)
3473 {
3474 tree parms = NULL, template_info = NULL;
3475
3476 if ((template_info = get_template_info (t))
3477 && primary_template_specialization_p (t))
3478 parms = INNERMOST_TEMPLATE_PARMS
3479 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3480
3481 return parms;
3482 }
3483
3484 /* Return the template parameters of the LEVELth level from the full list
3485 of template parameters PARMS. */
3486
3487 tree
3488 get_template_parms_at_level (tree parms, int level)
3489 {
3490 tree p;
3491 if (!parms
3492 || TREE_CODE (parms) != TREE_LIST
3493 || level > TMPL_PARMS_DEPTH (parms))
3494 return NULL_TREE;
3495
3496 for (p = parms; p; p = TREE_CHAIN (p))
3497 if (TMPL_PARMS_DEPTH (p) == level)
3498 return p;
3499
3500 return NULL_TREE;
3501 }
3502
3503 /* Returns the template arguments of T if T is a template instantiation,
3504 NULL otherwise. */
3505
3506 tree
3507 get_template_innermost_arguments (const_tree t)
3508 {
3509 tree args = NULL, template_info = NULL;
3510
3511 if ((template_info = get_template_info (t))
3512 && TI_ARGS (template_info))
3513 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3514
3515 return args;
3516 }
3517
3518 /* Return the argument pack elements of T if T is a template argument pack,
3519 NULL otherwise. */
3520
3521 tree
3522 get_template_argument_pack_elems (const_tree t)
3523 {
3524 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3525 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3526 return NULL;
3527
3528 return ARGUMENT_PACK_ARGS (t);
3529 }
3530
3531 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3532 ARGUMENT_PACK_SELECT represents. */
3533
3534 static tree
3535 argument_pack_select_arg (tree t)
3536 {
3537 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3538 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3539
3540 /* If the selected argument is an expansion E, that most likely means we were
3541 called from gen_elem_of_pack_expansion_instantiation during the
3542 substituting of an argument pack (of which the Ith element is a pack
3543 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3544 In this case, the Ith element resulting from this substituting is going to
3545 be a pack expansion, which pattern is the pattern of E. Let's return the
3546 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3547 resulting pack expansion from it. */
3548 if (PACK_EXPANSION_P (arg))
3549 {
3550 /* Make sure we aren't throwing away arg info. */
3551 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3552 arg = PACK_EXPANSION_PATTERN (arg);
3553 }
3554
3555 return arg;
3556 }
3557
3558
3559 /* True iff FN is a function representing a built-in variadic parameter
3560 pack. */
3561
3562 bool
3563 builtin_pack_fn_p (tree fn)
3564 {
3565 if (!fn
3566 || TREE_CODE (fn) != FUNCTION_DECL
3567 || !DECL_IS_BUILTIN (fn))
3568 return false;
3569
3570 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3571 return true;
3572
3573 return false;
3574 }
3575
3576 /* True iff CALL is a call to a function representing a built-in variadic
3577 parameter pack. */
3578
3579 static bool
3580 builtin_pack_call_p (tree call)
3581 {
3582 if (TREE_CODE (call) != CALL_EXPR)
3583 return false;
3584 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3585 }
3586
3587 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3588
3589 static tree
3590 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3591 tree in_decl)
3592 {
3593 tree ohi = CALL_EXPR_ARG (call, 0);
3594 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3595 false/*fn*/, true/*int_cst*/);
3596
3597 if (value_dependent_expression_p (hi))
3598 {
3599 if (hi != ohi)
3600 {
3601 call = copy_node (call);
3602 CALL_EXPR_ARG (call, 0) = hi;
3603 }
3604 tree ex = make_pack_expansion (call, complain);
3605 tree vec = make_tree_vec (1);
3606 TREE_VEC_ELT (vec, 0) = ex;
3607 return vec;
3608 }
3609 else
3610 {
3611 hi = cxx_constant_value (hi);
3612 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3613
3614 /* Calculate the largest value of len that won't make the size of the vec
3615 overflow an int. The compiler will exceed resource limits long before
3616 this, but it seems a decent place to diagnose. */
3617 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3618
3619 if (len < 0 || len > max)
3620 {
3621 if ((complain & tf_error)
3622 && hi != error_mark_node)
3623 error ("argument to __integer_pack must be between 0 and %d", max);
3624 return error_mark_node;
3625 }
3626
3627 tree vec = make_tree_vec (len);
3628
3629 for (int i = 0; i < len; ++i)
3630 TREE_VEC_ELT (vec, i) = size_int (i);
3631
3632 return vec;
3633 }
3634 }
3635
3636 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3637 CALL. */
3638
3639 static tree
3640 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3641 tree in_decl)
3642 {
3643 if (!builtin_pack_call_p (call))
3644 return NULL_TREE;
3645
3646 tree fn = CALL_EXPR_FN (call);
3647
3648 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3649 return expand_integer_pack (call, args, complain, in_decl);
3650
3651 return NULL_TREE;
3652 }
3653
3654 /* Structure used to track the progress of find_parameter_packs_r. */
3655 struct find_parameter_pack_data
3656 {
3657 /* TREE_LIST that will contain all of the parameter packs found by
3658 the traversal. */
3659 tree* parameter_packs;
3660
3661 /* Set of AST nodes that have been visited by the traversal. */
3662 hash_set<tree> *visited;
3663
3664 /* True iff we're making a type pack expansion. */
3665 bool type_pack_expansion_p;
3666 };
3667
3668 /* Identifies all of the argument packs that occur in a template
3669 argument and appends them to the TREE_LIST inside DATA, which is a
3670 find_parameter_pack_data structure. This is a subroutine of
3671 make_pack_expansion and uses_parameter_packs. */
3672 static tree
3673 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3674 {
3675 tree t = *tp;
3676 struct find_parameter_pack_data* ppd =
3677 (struct find_parameter_pack_data*)data;
3678 bool parameter_pack_p = false;
3679
3680 /* Handle type aliases/typedefs. */
3681 if (TYPE_ALIAS_P (t))
3682 {
3683 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3684 cp_walk_tree (&TI_ARGS (tinfo),
3685 &find_parameter_packs_r,
3686 ppd, ppd->visited);
3687 *walk_subtrees = 0;
3688 return NULL_TREE;
3689 }
3690
3691 /* Identify whether this is a parameter pack or not. */
3692 switch (TREE_CODE (t))
3693 {
3694 case TEMPLATE_PARM_INDEX:
3695 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3696 parameter_pack_p = true;
3697 break;
3698
3699 case TEMPLATE_TYPE_PARM:
3700 t = TYPE_MAIN_VARIANT (t);
3701 /* FALLTHRU */
3702 case TEMPLATE_TEMPLATE_PARM:
3703 /* If the placeholder appears in the decl-specifier-seq of a function
3704 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3705 is a pack expansion, the invented template parameter is a template
3706 parameter pack. */
3707 if (ppd->type_pack_expansion_p && is_auto (t))
3708 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3709 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3710 parameter_pack_p = true;
3711 break;
3712
3713 case FIELD_DECL:
3714 case PARM_DECL:
3715 if (DECL_PACK_P (t))
3716 {
3717 /* We don't want to walk into the type of a PARM_DECL,
3718 because we don't want to see the type parameter pack. */
3719 *walk_subtrees = 0;
3720 parameter_pack_p = true;
3721 }
3722 break;
3723
3724 case VAR_DECL:
3725 if (DECL_PACK_P (t))
3726 {
3727 /* We don't want to walk into the type of a variadic capture proxy,
3728 because we don't want to see the type parameter pack. */
3729 *walk_subtrees = 0;
3730 parameter_pack_p = true;
3731 }
3732 else if (variable_template_specialization_p (t))
3733 {
3734 cp_walk_tree (&DECL_TI_ARGS (t),
3735 find_parameter_packs_r,
3736 ppd, ppd->visited);
3737 *walk_subtrees = 0;
3738 }
3739 break;
3740
3741 case CALL_EXPR:
3742 if (builtin_pack_call_p (t))
3743 parameter_pack_p = true;
3744 break;
3745
3746 case BASES:
3747 parameter_pack_p = true;
3748 break;
3749 default:
3750 /* Not a parameter pack. */
3751 break;
3752 }
3753
3754 if (parameter_pack_p)
3755 {
3756 /* Add this parameter pack to the list. */
3757 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3758 }
3759
3760 if (TYPE_P (t))
3761 cp_walk_tree (&TYPE_CONTEXT (t),
3762 &find_parameter_packs_r, ppd, ppd->visited);
3763
3764 /* This switch statement will return immediately if we don't find a
3765 parameter pack. */
3766 switch (TREE_CODE (t))
3767 {
3768 case TEMPLATE_PARM_INDEX:
3769 return NULL_TREE;
3770
3771 case BOUND_TEMPLATE_TEMPLATE_PARM:
3772 /* Check the template itself. */
3773 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3774 &find_parameter_packs_r, ppd, ppd->visited);
3775 /* Check the template arguments. */
3776 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3777 ppd->visited);
3778 *walk_subtrees = 0;
3779 return NULL_TREE;
3780
3781 case TEMPLATE_TYPE_PARM:
3782 case TEMPLATE_TEMPLATE_PARM:
3783 return NULL_TREE;
3784
3785 case PARM_DECL:
3786 return NULL_TREE;
3787
3788 case DECL_EXPR:
3789 /* Ignore the declaration of a capture proxy for a parameter pack. */
3790 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3791 *walk_subtrees = 0;
3792 return NULL_TREE;
3793
3794 case RECORD_TYPE:
3795 if (TYPE_PTRMEMFUNC_P (t))
3796 return NULL_TREE;
3797 /* Fall through. */
3798
3799 case UNION_TYPE:
3800 case ENUMERAL_TYPE:
3801 if (TYPE_TEMPLATE_INFO (t))
3802 cp_walk_tree (&TYPE_TI_ARGS (t),
3803 &find_parameter_packs_r, ppd, ppd->visited);
3804
3805 *walk_subtrees = 0;
3806 return NULL_TREE;
3807
3808 case TEMPLATE_DECL:
3809 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3810 return NULL_TREE;
3811 gcc_fallthrough();
3812
3813 case CONSTRUCTOR:
3814 cp_walk_tree (&TREE_TYPE (t),
3815 &find_parameter_packs_r, ppd, ppd->visited);
3816 return NULL_TREE;
3817
3818 case TYPENAME_TYPE:
3819 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3820 ppd, ppd->visited);
3821 *walk_subtrees = 0;
3822 return NULL_TREE;
3823
3824 case TYPE_PACK_EXPANSION:
3825 case EXPR_PACK_EXPANSION:
3826 *walk_subtrees = 0;
3827 return NULL_TREE;
3828
3829 case INTEGER_TYPE:
3830 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3831 ppd, ppd->visited);
3832 *walk_subtrees = 0;
3833 return NULL_TREE;
3834
3835 case IDENTIFIER_NODE:
3836 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3837 ppd->visited);
3838 *walk_subtrees = 0;
3839 return NULL_TREE;
3840
3841 case LAMBDA_EXPR:
3842 {
3843 /* Look at explicit captures. */
3844 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3845 cap; cap = TREE_CHAIN (cap))
3846 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3847 ppd->visited);
3848 /* Since we defer implicit capture, look in the parms and body. */
3849 tree fn = lambda_function (t);
3850 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3851 ppd->visited);
3852 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3853 ppd->visited);
3854 *walk_subtrees = 0;
3855 return NULL_TREE;
3856 }
3857
3858 case DECLTYPE_TYPE:
3859 {
3860 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3861 type_pack_expansion_p to false so that any placeholders
3862 within the expression don't get marked as parameter packs. */
3863 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3864 ppd->type_pack_expansion_p = false;
3865 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3866 ppd, ppd->visited);
3867 ppd->type_pack_expansion_p = type_pack_expansion_p;
3868 *walk_subtrees = 0;
3869 return NULL_TREE;
3870 }
3871
3872 case IF_STMT:
3873 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
3874 ppd, ppd->visited);
3875 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
3876 ppd, ppd->visited);
3877 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
3878 ppd, ppd->visited);
3879 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3880 *walk_subtrees = 0;
3881 return NULL_TREE;
3882
3883 default:
3884 return NULL_TREE;
3885 }
3886
3887 return NULL_TREE;
3888 }
3889
3890 /* Determines if the expression or type T uses any parameter packs. */
3891 bool
3892 uses_parameter_packs (tree t)
3893 {
3894 tree parameter_packs = NULL_TREE;
3895 struct find_parameter_pack_data ppd;
3896 ppd.parameter_packs = &parameter_packs;
3897 ppd.visited = new hash_set<tree>;
3898 ppd.type_pack_expansion_p = false;
3899 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3900 delete ppd.visited;
3901 return parameter_packs != NULL_TREE;
3902 }
3903
3904 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3905 representation a base-class initializer into a parameter pack
3906 expansion. If all goes well, the resulting node will be an
3907 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3908 respectively. */
3909 tree
3910 make_pack_expansion (tree arg, tsubst_flags_t complain)
3911 {
3912 tree result;
3913 tree parameter_packs = NULL_TREE;
3914 bool for_types = false;
3915 struct find_parameter_pack_data ppd;
3916
3917 if (!arg || arg == error_mark_node)
3918 return arg;
3919
3920 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3921 {
3922 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3923 class initializer. In this case, the TREE_PURPOSE will be a
3924 _TYPE node (representing the base class expansion we're
3925 initializing) and the TREE_VALUE will be a TREE_LIST
3926 containing the initialization arguments.
3927
3928 The resulting expansion looks somewhat different from most
3929 expansions. Rather than returning just one _EXPANSION, we
3930 return a TREE_LIST whose TREE_PURPOSE is a
3931 TYPE_PACK_EXPANSION containing the bases that will be
3932 initialized. The TREE_VALUE will be identical to the
3933 original TREE_VALUE, which is a list of arguments that will
3934 be passed to each base. We do not introduce any new pack
3935 expansion nodes into the TREE_VALUE (although it is possible
3936 that some already exist), because the TREE_PURPOSE and
3937 TREE_VALUE all need to be expanded together with the same
3938 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3939 resulting TREE_PURPOSE will mention the parameter packs in
3940 both the bases and the arguments to the bases. */
3941 tree purpose;
3942 tree value;
3943 tree parameter_packs = NULL_TREE;
3944
3945 /* Determine which parameter packs will be used by the base
3946 class expansion. */
3947 ppd.visited = new hash_set<tree>;
3948 ppd.parameter_packs = &parameter_packs;
3949 ppd.type_pack_expansion_p = true;
3950 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3951 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3952 &ppd, ppd.visited);
3953
3954 if (parameter_packs == NULL_TREE)
3955 {
3956 if (complain & tf_error)
3957 error ("base initializer expansion %qT contains no parameter packs",
3958 arg);
3959 delete ppd.visited;
3960 return error_mark_node;
3961 }
3962
3963 if (TREE_VALUE (arg) != void_type_node)
3964 {
3965 /* Collect the sets of parameter packs used in each of the
3966 initialization arguments. */
3967 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3968 {
3969 /* Determine which parameter packs will be expanded in this
3970 argument. */
3971 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3972 &ppd, ppd.visited);
3973 }
3974 }
3975
3976 delete ppd.visited;
3977
3978 /* Create the pack expansion type for the base type. */
3979 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3980 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3981 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3982 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3983
3984 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3985 they will rarely be compared to anything. */
3986 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3987
3988 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3989 }
3990
3991 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3992 for_types = true;
3993
3994 /* Build the PACK_EXPANSION_* node. */
3995 result = for_types
3996 ? cxx_make_type (TYPE_PACK_EXPANSION)
3997 : make_node (EXPR_PACK_EXPANSION);
3998 SET_PACK_EXPANSION_PATTERN (result, arg);
3999 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4000 {
4001 /* Propagate type and const-expression information. */
4002 TREE_TYPE (result) = TREE_TYPE (arg);
4003 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4004 /* Mark this read now, since the expansion might be length 0. */
4005 mark_exp_read (arg);
4006 }
4007 else
4008 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4009 they will rarely be compared to anything. */
4010 SET_TYPE_STRUCTURAL_EQUALITY (result);
4011
4012 /* Determine which parameter packs will be expanded. */
4013 ppd.parameter_packs = &parameter_packs;
4014 ppd.visited = new hash_set<tree>;
4015 ppd.type_pack_expansion_p = TYPE_P (arg);
4016 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4017 delete ppd.visited;
4018
4019 /* Make sure we found some parameter packs. */
4020 if (parameter_packs == NULL_TREE)
4021 {
4022 if (complain & tf_error)
4023 {
4024 if (TYPE_P (arg))
4025 error ("expansion pattern %qT contains no parameter packs", arg);
4026 else
4027 error ("expansion pattern %qE contains no parameter packs", arg);
4028 }
4029 return error_mark_node;
4030 }
4031 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4032
4033 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4034
4035 return result;
4036 }
4037
4038 /* Checks T for any "bare" parameter packs, which have not yet been
4039 expanded, and issues an error if any are found. This operation can
4040 only be done on full expressions or types (e.g., an expression
4041 statement, "if" condition, etc.), because we could have expressions like:
4042
4043 foo(f(g(h(args)))...)
4044
4045 where "args" is a parameter pack. check_for_bare_parameter_packs
4046 should not be called for the subexpressions args, h(args),
4047 g(h(args)), or f(g(h(args))), because we would produce erroneous
4048 error messages.
4049
4050 Returns TRUE and emits an error if there were bare parameter packs,
4051 returns FALSE otherwise. */
4052 bool
4053 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4054 {
4055 tree parameter_packs = NULL_TREE;
4056 struct find_parameter_pack_data ppd;
4057
4058 if (!processing_template_decl || !t || t == error_mark_node)
4059 return false;
4060
4061 /* A lambda might use a parameter pack from the containing context. */
4062 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4063 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4064 return false;
4065
4066 if (TREE_CODE (t) == TYPE_DECL)
4067 t = TREE_TYPE (t);
4068
4069 ppd.parameter_packs = &parameter_packs;
4070 ppd.visited = new hash_set<tree>;
4071 ppd.type_pack_expansion_p = false;
4072 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4073 delete ppd.visited;
4074
4075 if (parameter_packs)
4076 {
4077 if (loc == UNKNOWN_LOCATION)
4078 loc = cp_expr_loc_or_loc (t, input_location);
4079 error_at (loc, "parameter packs not expanded with %<...%>:");
4080 while (parameter_packs)
4081 {
4082 tree pack = TREE_VALUE (parameter_packs);
4083 tree name = NULL_TREE;
4084
4085 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4086 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4087 name = TYPE_NAME (pack);
4088 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4089 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4090 else if (TREE_CODE (pack) == CALL_EXPR)
4091 name = DECL_NAME (CALL_EXPR_FN (pack));
4092 else
4093 name = DECL_NAME (pack);
4094
4095 if (name)
4096 inform (loc, " %qD", name);
4097 else
4098 inform (loc, " <anonymous>");
4099
4100 parameter_packs = TREE_CHAIN (parameter_packs);
4101 }
4102
4103 return true;
4104 }
4105
4106 return false;
4107 }
4108
4109 /* Expand any parameter packs that occur in the template arguments in
4110 ARGS. */
4111 tree
4112 expand_template_argument_pack (tree args)
4113 {
4114 if (args == error_mark_node)
4115 return error_mark_node;
4116
4117 tree result_args = NULL_TREE;
4118 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4119 int num_result_args = -1;
4120 int non_default_args_count = -1;
4121
4122 /* First, determine if we need to expand anything, and the number of
4123 slots we'll need. */
4124 for (in_arg = 0; in_arg < nargs; ++in_arg)
4125 {
4126 tree arg = TREE_VEC_ELT (args, in_arg);
4127 if (arg == NULL_TREE)
4128 return args;
4129 if (ARGUMENT_PACK_P (arg))
4130 {
4131 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4132 if (num_result_args < 0)
4133 num_result_args = in_arg + num_packed;
4134 else
4135 num_result_args += num_packed;
4136 }
4137 else
4138 {
4139 if (num_result_args >= 0)
4140 num_result_args++;
4141 }
4142 }
4143
4144 /* If no expansion is necessary, we're done. */
4145 if (num_result_args < 0)
4146 return args;
4147
4148 /* Expand arguments. */
4149 result_args = make_tree_vec (num_result_args);
4150 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4151 non_default_args_count =
4152 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4153 for (in_arg = 0; in_arg < nargs; ++in_arg)
4154 {
4155 tree arg = TREE_VEC_ELT (args, in_arg);
4156 if (ARGUMENT_PACK_P (arg))
4157 {
4158 tree packed = ARGUMENT_PACK_ARGS (arg);
4159 int i, num_packed = TREE_VEC_LENGTH (packed);
4160 for (i = 0; i < num_packed; ++i, ++out_arg)
4161 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4162 if (non_default_args_count > 0)
4163 non_default_args_count += num_packed - 1;
4164 }
4165 else
4166 {
4167 TREE_VEC_ELT (result_args, out_arg) = arg;
4168 ++out_arg;
4169 }
4170 }
4171 if (non_default_args_count >= 0)
4172 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4173 return result_args;
4174 }
4175
4176 /* Checks if DECL shadows a template parameter.
4177
4178 [temp.local]: A template-parameter shall not be redeclared within its
4179 scope (including nested scopes).
4180
4181 Emits an error and returns TRUE if the DECL shadows a parameter,
4182 returns FALSE otherwise. */
4183
4184 bool
4185 check_template_shadow (tree decl)
4186 {
4187 tree olddecl;
4188
4189 /* If we're not in a template, we can't possibly shadow a template
4190 parameter. */
4191 if (!current_template_parms)
4192 return true;
4193
4194 /* Figure out what we're shadowing. */
4195 decl = OVL_FIRST (decl);
4196 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4197
4198 /* If there's no previous binding for this name, we're not shadowing
4199 anything, let alone a template parameter. */
4200 if (!olddecl)
4201 return true;
4202
4203 /* If we're not shadowing a template parameter, we're done. Note
4204 that OLDDECL might be an OVERLOAD (or perhaps even an
4205 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4206 node. */
4207 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4208 return true;
4209
4210 /* We check for decl != olddecl to avoid bogus errors for using a
4211 name inside a class. We check TPFI to avoid duplicate errors for
4212 inline member templates. */
4213 if (decl == olddecl
4214 || (DECL_TEMPLATE_PARM_P (decl)
4215 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4216 return true;
4217
4218 /* Don't complain about the injected class name, as we've already
4219 complained about the class itself. */
4220 if (DECL_SELF_REFERENCE_P (decl))
4221 return false;
4222
4223 if (DECL_TEMPLATE_PARM_P (decl))
4224 error ("declaration of template parameter %q+D shadows "
4225 "template parameter", decl);
4226 else
4227 error ("declaration of %q+#D shadows template parameter", decl);
4228 inform (DECL_SOURCE_LOCATION (olddecl),
4229 "template parameter %qD declared here", olddecl);
4230 return false;
4231 }
4232
4233 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4234 ORIG_LEVEL, DECL, and TYPE. */
4235
4236 static tree
4237 build_template_parm_index (int index,
4238 int level,
4239 int orig_level,
4240 tree decl,
4241 tree type)
4242 {
4243 tree t = make_node (TEMPLATE_PARM_INDEX);
4244 TEMPLATE_PARM_IDX (t) = index;
4245 TEMPLATE_PARM_LEVEL (t) = level;
4246 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4247 TEMPLATE_PARM_DECL (t) = decl;
4248 TREE_TYPE (t) = type;
4249 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4250 TREE_READONLY (t) = TREE_READONLY (decl);
4251
4252 return t;
4253 }
4254
4255 /* Find the canonical type parameter for the given template type
4256 parameter. Returns the canonical type parameter, which may be TYPE
4257 if no such parameter existed. */
4258
4259 static tree
4260 canonical_type_parameter (tree type)
4261 {
4262 tree list;
4263 int idx = TEMPLATE_TYPE_IDX (type);
4264 if (!canonical_template_parms)
4265 vec_alloc (canonical_template_parms, idx + 1);
4266
4267 if (canonical_template_parms->length () <= (unsigned) idx)
4268 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4269
4270 list = (*canonical_template_parms)[idx];
4271 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4272 list = TREE_CHAIN (list);
4273
4274 if (list)
4275 return TREE_VALUE (list);
4276 else
4277 {
4278 (*canonical_template_parms)[idx]
4279 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4280 return type;
4281 }
4282 }
4283
4284 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4285 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4286 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4287 new one is created. */
4288
4289 static tree
4290 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4291 tsubst_flags_t complain)
4292 {
4293 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4294 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4295 != TEMPLATE_PARM_LEVEL (index) - levels)
4296 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4297 {
4298 tree orig_decl = TEMPLATE_PARM_DECL (index);
4299 tree decl, t;
4300
4301 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4302 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4303 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4304 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4305 DECL_ARTIFICIAL (decl) = 1;
4306 SET_DECL_TEMPLATE_PARM_P (decl);
4307
4308 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4309 TEMPLATE_PARM_LEVEL (index) - levels,
4310 TEMPLATE_PARM_ORIG_LEVEL (index),
4311 decl, type);
4312 TEMPLATE_PARM_DESCENDANTS (index) = t;
4313 TEMPLATE_PARM_PARAMETER_PACK (t)
4314 = TEMPLATE_PARM_PARAMETER_PACK (index);
4315
4316 /* Template template parameters need this. */
4317 if (TREE_CODE (decl) == TEMPLATE_DECL)
4318 {
4319 DECL_TEMPLATE_RESULT (decl)
4320 = build_decl (DECL_SOURCE_LOCATION (decl),
4321 TYPE_DECL, DECL_NAME (decl), type);
4322 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4323 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4324 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4325 }
4326 }
4327
4328 return TEMPLATE_PARM_DESCENDANTS (index);
4329 }
4330
4331 /* Process information from new template parameter PARM and append it
4332 to the LIST being built. This new parameter is a non-type
4333 parameter iff IS_NON_TYPE is true. This new parameter is a
4334 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4335 is in PARM_LOC. */
4336
4337 tree
4338 process_template_parm (tree list, location_t parm_loc, tree parm,
4339 bool is_non_type, bool is_parameter_pack)
4340 {
4341 tree decl = 0;
4342 int idx = 0;
4343
4344 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4345 tree defval = TREE_PURPOSE (parm);
4346 tree constr = TREE_TYPE (parm);
4347
4348 if (list)
4349 {
4350 tree p = tree_last (list);
4351
4352 if (p && TREE_VALUE (p) != error_mark_node)
4353 {
4354 p = TREE_VALUE (p);
4355 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4356 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4357 else
4358 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4359 }
4360
4361 ++idx;
4362 }
4363
4364 if (is_non_type)
4365 {
4366 parm = TREE_VALUE (parm);
4367
4368 SET_DECL_TEMPLATE_PARM_P (parm);
4369
4370 if (TREE_TYPE (parm) != error_mark_node)
4371 {
4372 /* [temp.param]
4373
4374 The top-level cv-qualifiers on the template-parameter are
4375 ignored when determining its type. */
4376 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4377 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4378 TREE_TYPE (parm) = error_mark_node;
4379 else if (uses_parameter_packs (TREE_TYPE (parm))
4380 && !is_parameter_pack
4381 /* If we're in a nested template parameter list, the template
4382 template parameter could be a parameter pack. */
4383 && processing_template_parmlist == 1)
4384 {
4385 /* This template parameter is not a parameter pack, but it
4386 should be. Complain about "bare" parameter packs. */
4387 check_for_bare_parameter_packs (TREE_TYPE (parm));
4388
4389 /* Recover by calling this a parameter pack. */
4390 is_parameter_pack = true;
4391 }
4392 }
4393
4394 /* A template parameter is not modifiable. */
4395 TREE_CONSTANT (parm) = 1;
4396 TREE_READONLY (parm) = 1;
4397 decl = build_decl (parm_loc,
4398 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4399 TREE_CONSTANT (decl) = 1;
4400 TREE_READONLY (decl) = 1;
4401 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4402 = build_template_parm_index (idx, processing_template_decl,
4403 processing_template_decl,
4404 decl, TREE_TYPE (parm));
4405
4406 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4407 = is_parameter_pack;
4408 }
4409 else
4410 {
4411 tree t;
4412 parm = TREE_VALUE (TREE_VALUE (parm));
4413
4414 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4415 {
4416 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4417 /* This is for distinguishing between real templates and template
4418 template parameters */
4419 TREE_TYPE (parm) = t;
4420 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4421 decl = parm;
4422 }
4423 else
4424 {
4425 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4426 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4427 decl = build_decl (parm_loc,
4428 TYPE_DECL, parm, t);
4429 }
4430
4431 TYPE_NAME (t) = decl;
4432 TYPE_STUB_DECL (t) = decl;
4433 parm = decl;
4434 TEMPLATE_TYPE_PARM_INDEX (t)
4435 = build_template_parm_index (idx, processing_template_decl,
4436 processing_template_decl,
4437 decl, TREE_TYPE (parm));
4438 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4439 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4440 }
4441 DECL_ARTIFICIAL (decl) = 1;
4442 SET_DECL_TEMPLATE_PARM_P (decl);
4443
4444 /* Build requirements for the type/template parameter.
4445 This must be done after SET_DECL_TEMPLATE_PARM_P or
4446 process_template_parm could fail. */
4447 tree reqs = finish_shorthand_constraint (parm, constr);
4448
4449 pushdecl (decl);
4450
4451 /* Build the parameter node linking the parameter declaration,
4452 its default argument (if any), and its constraints (if any). */
4453 parm = build_tree_list (defval, parm);
4454 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4455
4456 return chainon (list, parm);
4457 }
4458
4459 /* The end of a template parameter list has been reached. Process the
4460 tree list into a parameter vector, converting each parameter into a more
4461 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4462 as PARM_DECLs. */
4463
4464 tree
4465 end_template_parm_list (tree parms)
4466 {
4467 int nparms;
4468 tree parm, next;
4469 tree saved_parmlist = make_tree_vec (list_length (parms));
4470
4471 /* Pop the dummy parameter level and add the real one. */
4472 current_template_parms = TREE_CHAIN (current_template_parms);
4473
4474 current_template_parms
4475 = tree_cons (size_int (processing_template_decl),
4476 saved_parmlist, current_template_parms);
4477
4478 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4479 {
4480 next = TREE_CHAIN (parm);
4481 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4482 TREE_CHAIN (parm) = NULL_TREE;
4483 }
4484
4485 --processing_template_parmlist;
4486
4487 return saved_parmlist;
4488 }
4489
4490 // Explicitly indicate the end of the template parameter list. We assume
4491 // that the current template parameters have been constructed and/or
4492 // managed explicitly, as when creating new template template parameters
4493 // from a shorthand constraint.
4494 void
4495 end_template_parm_list ()
4496 {
4497 --processing_template_parmlist;
4498 }
4499
4500 /* end_template_decl is called after a template declaration is seen. */
4501
4502 void
4503 end_template_decl (void)
4504 {
4505 reset_specialization ();
4506
4507 if (! processing_template_decl)
4508 return;
4509
4510 /* This matches the pushlevel in begin_template_parm_list. */
4511 finish_scope ();
4512
4513 --processing_template_decl;
4514 current_template_parms = TREE_CHAIN (current_template_parms);
4515 }
4516
4517 /* Takes a TREE_LIST representing a template parameter and convert it
4518 into an argument suitable to be passed to the type substitution
4519 functions. Note that If the TREE_LIST contains an error_mark
4520 node, the returned argument is error_mark_node. */
4521
4522 tree
4523 template_parm_to_arg (tree t)
4524 {
4525
4526 if (t == NULL_TREE
4527 || TREE_CODE (t) != TREE_LIST)
4528 return t;
4529
4530 if (error_operand_p (TREE_VALUE (t)))
4531 return error_mark_node;
4532
4533 t = TREE_VALUE (t);
4534
4535 if (TREE_CODE (t) == TYPE_DECL
4536 || TREE_CODE (t) == TEMPLATE_DECL)
4537 {
4538 t = TREE_TYPE (t);
4539
4540 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4541 {
4542 /* Turn this argument into a TYPE_ARGUMENT_PACK
4543 with a single element, which expands T. */
4544 tree vec = make_tree_vec (1);
4545 if (CHECKING_P)
4546 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4547
4548 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4549
4550 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4551 SET_ARGUMENT_PACK_ARGS (t, vec);
4552 }
4553 }
4554 else
4555 {
4556 t = DECL_INITIAL (t);
4557
4558 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4559 {
4560 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4561 with a single element, which expands T. */
4562 tree vec = make_tree_vec (1);
4563 if (CHECKING_P)
4564 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4565
4566 t = convert_from_reference (t);
4567 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4568
4569 t = make_node (NONTYPE_ARGUMENT_PACK);
4570 SET_ARGUMENT_PACK_ARGS (t, vec);
4571 }
4572 else
4573 t = convert_from_reference (t);
4574 }
4575 return t;
4576 }
4577
4578 /* Given a single level of template parameters (a TREE_VEC), return it
4579 as a set of template arguments. */
4580
4581 static tree
4582 template_parms_level_to_args (tree parms)
4583 {
4584 tree a = copy_node (parms);
4585 TREE_TYPE (a) = NULL_TREE;
4586 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4587 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4588
4589 if (CHECKING_P)
4590 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4591
4592 return a;
4593 }
4594
4595 /* Given a set of template parameters, return them as a set of template
4596 arguments. The template parameters are represented as a TREE_VEC, in
4597 the form documented in cp-tree.h for template arguments. */
4598
4599 static tree
4600 template_parms_to_args (tree parms)
4601 {
4602 tree header;
4603 tree args = NULL_TREE;
4604 int length = TMPL_PARMS_DEPTH (parms);
4605 int l = length;
4606
4607 /* If there is only one level of template parameters, we do not
4608 create a TREE_VEC of TREE_VECs. Instead, we return a single
4609 TREE_VEC containing the arguments. */
4610 if (length > 1)
4611 args = make_tree_vec (length);
4612
4613 for (header = parms; header; header = TREE_CHAIN (header))
4614 {
4615 tree a = template_parms_level_to_args (TREE_VALUE (header));
4616
4617 if (length > 1)
4618 TREE_VEC_ELT (args, --l) = a;
4619 else
4620 args = a;
4621 }
4622
4623 return args;
4624 }
4625
4626 /* Within the declaration of a template, return the currently active
4627 template parameters as an argument TREE_VEC. */
4628
4629 static tree
4630 current_template_args (void)
4631 {
4632 return template_parms_to_args (current_template_parms);
4633 }
4634
4635 /* Update the declared TYPE by doing any lookups which were thought to be
4636 dependent, but are not now that we know the SCOPE of the declarator. */
4637
4638 tree
4639 maybe_update_decl_type (tree orig_type, tree scope)
4640 {
4641 tree type = orig_type;
4642
4643 if (type == NULL_TREE)
4644 return type;
4645
4646 if (TREE_CODE (orig_type) == TYPE_DECL)
4647 type = TREE_TYPE (type);
4648
4649 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4650 && dependent_type_p (type)
4651 /* Don't bother building up the args in this case. */
4652 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4653 {
4654 /* tsubst in the args corresponding to the template parameters,
4655 including auto if present. Most things will be unchanged, but
4656 make_typename_type and tsubst_qualified_id will resolve
4657 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4658 tree args = current_template_args ();
4659 tree auto_node = type_uses_auto (type);
4660 tree pushed;
4661 if (auto_node)
4662 {
4663 tree auto_vec = make_tree_vec (1);
4664 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4665 args = add_to_template_args (args, auto_vec);
4666 }
4667 pushed = push_scope (scope);
4668 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4669 if (pushed)
4670 pop_scope (scope);
4671 }
4672
4673 if (type == error_mark_node)
4674 return orig_type;
4675
4676 if (TREE_CODE (orig_type) == TYPE_DECL)
4677 {
4678 if (same_type_p (type, TREE_TYPE (orig_type)))
4679 type = orig_type;
4680 else
4681 type = TYPE_NAME (type);
4682 }
4683 return type;
4684 }
4685
4686 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4687 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4688 the new template is a member template. */
4689
4690 static tree
4691 build_template_decl (tree decl, tree parms, bool member_template_p)
4692 {
4693 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4694 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4695 DECL_TEMPLATE_PARMS (tmpl) = parms;
4696 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4697 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4698 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4699
4700 return tmpl;
4701 }
4702
4703 struct template_parm_data
4704 {
4705 /* The level of the template parameters we are currently
4706 processing. */
4707 int level;
4708
4709 /* The index of the specialization argument we are currently
4710 processing. */
4711 int current_arg;
4712
4713 /* An array whose size is the number of template parameters. The
4714 elements are nonzero if the parameter has been used in any one
4715 of the arguments processed so far. */
4716 int* parms;
4717
4718 /* An array whose size is the number of template arguments. The
4719 elements are nonzero if the argument makes use of template
4720 parameters of this level. */
4721 int* arg_uses_template_parms;
4722 };
4723
4724 /* Subroutine of push_template_decl used to see if each template
4725 parameter in a partial specialization is used in the explicit
4726 argument list. If T is of the LEVEL given in DATA (which is
4727 treated as a template_parm_data*), then DATA->PARMS is marked
4728 appropriately. */
4729
4730 static int
4731 mark_template_parm (tree t, void* data)
4732 {
4733 int level;
4734 int idx;
4735 struct template_parm_data* tpd = (struct template_parm_data*) data;
4736
4737 template_parm_level_and_index (t, &level, &idx);
4738
4739 if (level == tpd->level)
4740 {
4741 tpd->parms[idx] = 1;
4742 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4743 }
4744
4745 /* In C++17 the type of a non-type argument is a deduced context. */
4746 if (cxx_dialect >= cxx17
4747 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4748 for_each_template_parm (TREE_TYPE (t),
4749 &mark_template_parm,
4750 data,
4751 NULL,
4752 /*include_nondeduced_p=*/false);
4753
4754 /* Return zero so that for_each_template_parm will continue the
4755 traversal of the tree; we want to mark *every* template parm. */
4756 return 0;
4757 }
4758
4759 /* Process the partial specialization DECL. */
4760
4761 static tree
4762 process_partial_specialization (tree decl)
4763 {
4764 tree type = TREE_TYPE (decl);
4765 tree tinfo = get_template_info (decl);
4766 tree maintmpl = TI_TEMPLATE (tinfo);
4767 tree specargs = TI_ARGS (tinfo);
4768 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4769 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4770 tree inner_parms;
4771 tree inst;
4772 int nargs = TREE_VEC_LENGTH (inner_args);
4773 int ntparms;
4774 int i;
4775 bool did_error_intro = false;
4776 struct template_parm_data tpd;
4777 struct template_parm_data tpd2;
4778
4779 gcc_assert (current_template_parms);
4780
4781 /* A concept cannot be specialized. */
4782 if (flag_concepts && variable_concept_p (maintmpl))
4783 {
4784 error ("specialization of variable concept %q#D", maintmpl);
4785 return error_mark_node;
4786 }
4787
4788 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4789 ntparms = TREE_VEC_LENGTH (inner_parms);
4790
4791 /* We check that each of the template parameters given in the
4792 partial specialization is used in the argument list to the
4793 specialization. For example:
4794
4795 template <class T> struct S;
4796 template <class T> struct S<T*>;
4797
4798 The second declaration is OK because `T*' uses the template
4799 parameter T, whereas
4800
4801 template <class T> struct S<int>;
4802
4803 is no good. Even trickier is:
4804
4805 template <class T>
4806 struct S1
4807 {
4808 template <class U>
4809 struct S2;
4810 template <class U>
4811 struct S2<T>;
4812 };
4813
4814 The S2<T> declaration is actually invalid; it is a
4815 full-specialization. Of course,
4816
4817 template <class U>
4818 struct S2<T (*)(U)>;
4819
4820 or some such would have been OK. */
4821 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4822 tpd.parms = XALLOCAVEC (int, ntparms);
4823 memset (tpd.parms, 0, sizeof (int) * ntparms);
4824
4825 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4826 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4827 for (i = 0; i < nargs; ++i)
4828 {
4829 tpd.current_arg = i;
4830 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4831 &mark_template_parm,
4832 &tpd,
4833 NULL,
4834 /*include_nondeduced_p=*/false);
4835 }
4836 for (i = 0; i < ntparms; ++i)
4837 if (tpd.parms[i] == 0)
4838 {
4839 /* One of the template parms was not used in a deduced context in the
4840 specialization. */
4841 if (!did_error_intro)
4842 {
4843 error ("template parameters not deducible in "
4844 "partial specialization:");
4845 did_error_intro = true;
4846 }
4847
4848 inform (input_location, " %qD",
4849 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4850 }
4851
4852 if (did_error_intro)
4853 return error_mark_node;
4854
4855 /* [temp.class.spec]
4856
4857 The argument list of the specialization shall not be identical to
4858 the implicit argument list of the primary template. */
4859 tree main_args
4860 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4861 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4862 && (!flag_concepts
4863 || !strictly_subsumes (current_template_constraints (),
4864 get_constraints (maintmpl))))
4865 {
4866 if (!flag_concepts)
4867 error ("partial specialization %q+D does not specialize "
4868 "any template arguments; to define the primary template, "
4869 "remove the template argument list", decl);
4870 else
4871 error ("partial specialization %q+D does not specialize any "
4872 "template arguments and is not more constrained than "
4873 "the primary template; to define the primary template, "
4874 "remove the template argument list", decl);
4875 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4876 }
4877
4878 /* A partial specialization that replaces multiple parameters of the
4879 primary template with a pack expansion is less specialized for those
4880 parameters. */
4881 if (nargs < DECL_NTPARMS (maintmpl))
4882 {
4883 error ("partial specialization is not more specialized than the "
4884 "primary template because it replaces multiple parameters "
4885 "with a pack expansion");
4886 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4887 /* Avoid crash in process_partial_specialization. */
4888 return decl;
4889 }
4890
4891 /* If we aren't in a dependent class, we can actually try deduction. */
4892 else if (tpd.level == 1
4893 /* FIXME we should be able to handle a partial specialization of a
4894 partial instantiation, but currently we can't (c++/41727). */
4895 && TMPL_ARGS_DEPTH (specargs) == 1
4896 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4897 {
4898 auto_diagnostic_group d;
4899 if (permerror (input_location, "partial specialization %qD is not "
4900 "more specialized than", decl))
4901 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4902 maintmpl);
4903 }
4904
4905 /* [temp.class.spec]
4906
4907 A partially specialized non-type argument expression shall not
4908 involve template parameters of the partial specialization except
4909 when the argument expression is a simple identifier.
4910
4911 The type of a template parameter corresponding to a specialized
4912 non-type argument shall not be dependent on a parameter of the
4913 specialization.
4914
4915 Also, we verify that pack expansions only occur at the
4916 end of the argument list. */
4917 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4918 tpd2.parms = 0;
4919 for (i = 0; i < nargs; ++i)
4920 {
4921 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4922 tree arg = TREE_VEC_ELT (inner_args, i);
4923 tree packed_args = NULL_TREE;
4924 int j, len = 1;
4925
4926 if (ARGUMENT_PACK_P (arg))
4927 {
4928 /* Extract the arguments from the argument pack. We'll be
4929 iterating over these in the following loop. */
4930 packed_args = ARGUMENT_PACK_ARGS (arg);
4931 len = TREE_VEC_LENGTH (packed_args);
4932 }
4933
4934 for (j = 0; j < len; j++)
4935 {
4936 if (packed_args)
4937 /* Get the Jth argument in the parameter pack. */
4938 arg = TREE_VEC_ELT (packed_args, j);
4939
4940 if (PACK_EXPANSION_P (arg))
4941 {
4942 /* Pack expansions must come at the end of the
4943 argument list. */
4944 if ((packed_args && j < len - 1)
4945 || (!packed_args && i < nargs - 1))
4946 {
4947 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4948 error ("parameter pack argument %qE must be at the "
4949 "end of the template argument list", arg);
4950 else
4951 error ("parameter pack argument %qT must be at the "
4952 "end of the template argument list", arg);
4953 }
4954 }
4955
4956 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4957 /* We only care about the pattern. */
4958 arg = PACK_EXPANSION_PATTERN (arg);
4959
4960 if (/* These first two lines are the `non-type' bit. */
4961 !TYPE_P (arg)
4962 && TREE_CODE (arg) != TEMPLATE_DECL
4963 /* This next two lines are the `argument expression is not just a
4964 simple identifier' condition and also the `specialized
4965 non-type argument' bit. */
4966 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4967 && !(REFERENCE_REF_P (arg)
4968 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4969 {
4970 if ((!packed_args && tpd.arg_uses_template_parms[i])
4971 || (packed_args && uses_template_parms (arg)))
4972 error ("template argument %qE involves template parameter(s)",
4973 arg);
4974 else
4975 {
4976 /* Look at the corresponding template parameter,
4977 marking which template parameters its type depends
4978 upon. */
4979 tree type = TREE_TYPE (parm);
4980
4981 if (!tpd2.parms)
4982 {
4983 /* We haven't yet initialized TPD2. Do so now. */
4984 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4985 /* The number of parameters here is the number in the
4986 main template, which, as checked in the assertion
4987 above, is NARGS. */
4988 tpd2.parms = XALLOCAVEC (int, nargs);
4989 tpd2.level =
4990 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4991 }
4992
4993 /* Mark the template parameters. But this time, we're
4994 looking for the template parameters of the main
4995 template, not in the specialization. */
4996 tpd2.current_arg = i;
4997 tpd2.arg_uses_template_parms[i] = 0;
4998 memset (tpd2.parms, 0, sizeof (int) * nargs);
4999 for_each_template_parm (type,
5000 &mark_template_parm,
5001 &tpd2,
5002 NULL,
5003 /*include_nondeduced_p=*/false);
5004
5005 if (tpd2.arg_uses_template_parms [i])
5006 {
5007 /* The type depended on some template parameters.
5008 If they are fully specialized in the
5009 specialization, that's OK. */
5010 int j;
5011 int count = 0;
5012 for (j = 0; j < nargs; ++j)
5013 if (tpd2.parms[j] != 0
5014 && tpd.arg_uses_template_parms [j])
5015 ++count;
5016 if (count != 0)
5017 error_n (input_location, count,
5018 "type %qT of template argument %qE depends "
5019 "on a template parameter",
5020 "type %qT of template argument %qE depends "
5021 "on template parameters",
5022 type,
5023 arg);
5024 }
5025 }
5026 }
5027 }
5028 }
5029
5030 /* We should only get here once. */
5031 if (TREE_CODE (decl) == TYPE_DECL)
5032 gcc_assert (!COMPLETE_TYPE_P (type));
5033
5034 // Build the template decl.
5035 tree tmpl = build_template_decl (decl, current_template_parms,
5036 DECL_MEMBER_TEMPLATE_P (maintmpl));
5037 TREE_TYPE (tmpl) = type;
5038 DECL_TEMPLATE_RESULT (tmpl) = decl;
5039 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5040 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5041 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5042
5043 /* Give template template parms a DECL_CONTEXT of the template
5044 for which they are a parameter. */
5045 for (i = 0; i < ntparms; ++i)
5046 {
5047 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5048 if (TREE_CODE (parm) == TEMPLATE_DECL)
5049 DECL_CONTEXT (parm) = tmpl;
5050 }
5051
5052 if (VAR_P (decl))
5053 /* We didn't register this in check_explicit_specialization so we could
5054 wait until the constraints were set. */
5055 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5056 else
5057 associate_classtype_constraints (type);
5058
5059 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5060 = tree_cons (specargs, tmpl,
5061 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5062 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5063
5064 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5065 inst = TREE_CHAIN (inst))
5066 {
5067 tree instance = TREE_VALUE (inst);
5068 if (TYPE_P (instance)
5069 ? (COMPLETE_TYPE_P (instance)
5070 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5071 : DECL_TEMPLATE_INSTANTIATION (instance))
5072 {
5073 tree spec = most_specialized_partial_spec (instance, tf_none);
5074 tree inst_decl = (DECL_P (instance)
5075 ? instance : TYPE_NAME (instance));
5076 if (!spec)
5077 /* OK */;
5078 else if (spec == error_mark_node)
5079 permerror (input_location,
5080 "declaration of %qD ambiguates earlier template "
5081 "instantiation for %qD", decl, inst_decl);
5082 else if (TREE_VALUE (spec) == tmpl)
5083 permerror (input_location,
5084 "partial specialization of %qD after instantiation "
5085 "of %qD", decl, inst_decl);
5086 }
5087 }
5088
5089 return decl;
5090 }
5091
5092 /* PARM is a template parameter of some form; return the corresponding
5093 TEMPLATE_PARM_INDEX. */
5094
5095 static tree
5096 get_template_parm_index (tree parm)
5097 {
5098 if (TREE_CODE (parm) == PARM_DECL
5099 || TREE_CODE (parm) == CONST_DECL)
5100 parm = DECL_INITIAL (parm);
5101 else if (TREE_CODE (parm) == TYPE_DECL
5102 || TREE_CODE (parm) == TEMPLATE_DECL)
5103 parm = TREE_TYPE (parm);
5104 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5105 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5106 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5107 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5108 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5109 return parm;
5110 }
5111
5112 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5113 parameter packs used by the template parameter PARM. */
5114
5115 static void
5116 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5117 {
5118 /* A type parm can't refer to another parm. */
5119 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5120 return;
5121 else if (TREE_CODE (parm) == PARM_DECL)
5122 {
5123 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5124 ppd, ppd->visited);
5125 return;
5126 }
5127
5128 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5129
5130 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5131 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5132 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5133 }
5134
5135 /* PARM is a template parameter pack. Return any parameter packs used in
5136 its type or the type of any of its template parameters. If there are
5137 any such packs, it will be instantiated into a fixed template parameter
5138 list by partial instantiation rather than be fully deduced. */
5139
5140 tree
5141 fixed_parameter_pack_p (tree parm)
5142 {
5143 /* This can only be true in a member template. */
5144 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5145 return NULL_TREE;
5146 /* This can only be true for a parameter pack. */
5147 if (!template_parameter_pack_p (parm))
5148 return NULL_TREE;
5149 /* A type parm can't refer to another parm. */
5150 if (TREE_CODE (parm) == TYPE_DECL)
5151 return NULL_TREE;
5152
5153 tree parameter_packs = NULL_TREE;
5154 struct find_parameter_pack_data ppd;
5155 ppd.parameter_packs = &parameter_packs;
5156 ppd.visited = new hash_set<tree>;
5157 ppd.type_pack_expansion_p = false;
5158
5159 fixed_parameter_pack_p_1 (parm, &ppd);
5160
5161 delete ppd.visited;
5162 return parameter_packs;
5163 }
5164
5165 /* Check that a template declaration's use of default arguments and
5166 parameter packs is not invalid. Here, PARMS are the template
5167 parameters. IS_PRIMARY is true if DECL is the thing declared by
5168 a primary template. IS_PARTIAL is true if DECL is a partial
5169 specialization.
5170
5171 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5172 function template declaration or a friend class template
5173 declaration. In the function case, 1 indicates a declaration, 2
5174 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5175 emitted for extraneous default arguments.
5176
5177 Returns TRUE if there were no errors found, FALSE otherwise. */
5178
5179 bool
5180 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5181 bool is_partial, int is_friend_decl)
5182 {
5183 const char *msg;
5184 int last_level_to_check;
5185 tree parm_level;
5186 bool no_errors = true;
5187
5188 /* [temp.param]
5189
5190 A default template-argument shall not be specified in a
5191 function template declaration or a function template definition, nor
5192 in the template-parameter-list of the definition of a member of a
5193 class template. */
5194
5195 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5196 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5197 /* You can't have a function template declaration in a local
5198 scope, nor you can you define a member of a class template in a
5199 local scope. */
5200 return true;
5201
5202 if ((TREE_CODE (decl) == TYPE_DECL
5203 && TREE_TYPE (decl)
5204 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5205 || (TREE_CODE (decl) == FUNCTION_DECL
5206 && LAMBDA_FUNCTION_P (decl)))
5207 /* A lambda doesn't have an explicit declaration; don't complain
5208 about the parms of the enclosing class. */
5209 return true;
5210
5211 if (current_class_type
5212 && !TYPE_BEING_DEFINED (current_class_type)
5213 && DECL_LANG_SPECIFIC (decl)
5214 && DECL_DECLARES_FUNCTION_P (decl)
5215 /* If this is either a friend defined in the scope of the class
5216 or a member function. */
5217 && (DECL_FUNCTION_MEMBER_P (decl)
5218 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5219 : DECL_FRIEND_CONTEXT (decl)
5220 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5221 : false)
5222 /* And, if it was a member function, it really was defined in
5223 the scope of the class. */
5224 && (!DECL_FUNCTION_MEMBER_P (decl)
5225 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5226 /* We already checked these parameters when the template was
5227 declared, so there's no need to do it again now. This function
5228 was defined in class scope, but we're processing its body now
5229 that the class is complete. */
5230 return true;
5231
5232 /* Core issue 226 (C++0x only): the following only applies to class
5233 templates. */
5234 if (is_primary
5235 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5236 {
5237 /* [temp.param]
5238
5239 If a template-parameter has a default template-argument, all
5240 subsequent template-parameters shall have a default
5241 template-argument supplied. */
5242 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5243 {
5244 tree inner_parms = TREE_VALUE (parm_level);
5245 int ntparms = TREE_VEC_LENGTH (inner_parms);
5246 int seen_def_arg_p = 0;
5247 int i;
5248
5249 for (i = 0; i < ntparms; ++i)
5250 {
5251 tree parm = TREE_VEC_ELT (inner_parms, i);
5252
5253 if (parm == error_mark_node)
5254 continue;
5255
5256 if (TREE_PURPOSE (parm))
5257 seen_def_arg_p = 1;
5258 else if (seen_def_arg_p
5259 && !template_parameter_pack_p (TREE_VALUE (parm)))
5260 {
5261 error ("no default argument for %qD", TREE_VALUE (parm));
5262 /* For better subsequent error-recovery, we indicate that
5263 there should have been a default argument. */
5264 TREE_PURPOSE (parm) = error_mark_node;
5265 no_errors = false;
5266 }
5267 else if (!is_partial
5268 && !is_friend_decl
5269 /* Don't complain about an enclosing partial
5270 specialization. */
5271 && parm_level == parms
5272 && TREE_CODE (decl) == TYPE_DECL
5273 && i < ntparms - 1
5274 && template_parameter_pack_p (TREE_VALUE (parm))
5275 /* A fixed parameter pack will be partially
5276 instantiated into a fixed length list. */
5277 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5278 {
5279 /* A primary class template can only have one
5280 parameter pack, at the end of the template
5281 parameter list. */
5282
5283 error ("parameter pack %q+D must be at the end of the"
5284 " template parameter list", TREE_VALUE (parm));
5285
5286 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5287 = error_mark_node;
5288 no_errors = false;
5289 }
5290 }
5291 }
5292 }
5293
5294 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5295 || is_partial
5296 || !is_primary
5297 || is_friend_decl)
5298 /* For an ordinary class template, default template arguments are
5299 allowed at the innermost level, e.g.:
5300 template <class T = int>
5301 struct S {};
5302 but, in a partial specialization, they're not allowed even
5303 there, as we have in [temp.class.spec]:
5304
5305 The template parameter list of a specialization shall not
5306 contain default template argument values.
5307
5308 So, for a partial specialization, or for a function template
5309 (in C++98/C++03), we look at all of them. */
5310 ;
5311 else
5312 /* But, for a primary class template that is not a partial
5313 specialization we look at all template parameters except the
5314 innermost ones. */
5315 parms = TREE_CHAIN (parms);
5316
5317 /* Figure out what error message to issue. */
5318 if (is_friend_decl == 2)
5319 msg = G_("default template arguments may not be used in function template "
5320 "friend re-declaration");
5321 else if (is_friend_decl)
5322 msg = G_("default template arguments may not be used in template "
5323 "friend declarations");
5324 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5325 msg = G_("default template arguments may not be used in function templates "
5326 "without -std=c++11 or -std=gnu++11");
5327 else if (is_partial)
5328 msg = G_("default template arguments may not be used in "
5329 "partial specializations");
5330 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5331 msg = G_("default argument for template parameter for class enclosing %qD");
5332 else
5333 /* Per [temp.param]/9, "A default template-argument shall not be
5334 specified in the template-parameter-lists of the definition of
5335 a member of a class template that appears outside of the member's
5336 class.", thus if we aren't handling a member of a class template
5337 there is no need to examine the parameters. */
5338 return true;
5339
5340 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5341 /* If we're inside a class definition, there's no need to
5342 examine the parameters to the class itself. On the one
5343 hand, they will be checked when the class is defined, and,
5344 on the other, default arguments are valid in things like:
5345 template <class T = double>
5346 struct S { template <class U> void f(U); };
5347 Here the default argument for `S' has no bearing on the
5348 declaration of `f'. */
5349 last_level_to_check = template_class_depth (current_class_type) + 1;
5350 else
5351 /* Check everything. */
5352 last_level_to_check = 0;
5353
5354 for (parm_level = parms;
5355 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5356 parm_level = TREE_CHAIN (parm_level))
5357 {
5358 tree inner_parms = TREE_VALUE (parm_level);
5359 int i;
5360 int ntparms;
5361
5362 ntparms = TREE_VEC_LENGTH (inner_parms);
5363 for (i = 0; i < ntparms; ++i)
5364 {
5365 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5366 continue;
5367
5368 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5369 {
5370 if (msg)
5371 {
5372 no_errors = false;
5373 if (is_friend_decl == 2)
5374 return no_errors;
5375
5376 error (msg, decl);
5377 msg = 0;
5378 }
5379
5380 /* Clear out the default argument so that we are not
5381 confused later. */
5382 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5383 }
5384 }
5385
5386 /* At this point, if we're still interested in issuing messages,
5387 they must apply to classes surrounding the object declared. */
5388 if (msg)
5389 msg = G_("default argument for template parameter for class "
5390 "enclosing %qD");
5391 }
5392
5393 return no_errors;
5394 }
5395
5396 /* Worker for push_template_decl_real, called via
5397 for_each_template_parm. DATA is really an int, indicating the
5398 level of the parameters we are interested in. If T is a template
5399 parameter of that level, return nonzero. */
5400
5401 static int
5402 template_parm_this_level_p (tree t, void* data)
5403 {
5404 int this_level = *(int *)data;
5405 int level;
5406
5407 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5408 level = TEMPLATE_PARM_LEVEL (t);
5409 else
5410 level = TEMPLATE_TYPE_LEVEL (t);
5411 return level == this_level;
5412 }
5413
5414 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5415 DATA is really an int, indicating the innermost outer level of parameters.
5416 If T is a template parameter of that level or further out, return
5417 nonzero. */
5418
5419 static int
5420 template_parm_outer_level (tree t, void *data)
5421 {
5422 int this_level = *(int *)data;
5423 int level;
5424
5425 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5426 level = TEMPLATE_PARM_LEVEL (t);
5427 else
5428 level = TEMPLATE_TYPE_LEVEL (t);
5429 return level <= this_level;
5430 }
5431
5432 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5433 parameters given by current_template_args, or reuses a
5434 previously existing one, if appropriate. Returns the DECL, or an
5435 equivalent one, if it is replaced via a call to duplicate_decls.
5436
5437 If IS_FRIEND is true, DECL is a friend declaration. */
5438
5439 tree
5440 push_template_decl_real (tree decl, bool is_friend)
5441 {
5442 tree tmpl;
5443 tree args;
5444 tree info;
5445 tree ctx;
5446 bool is_primary;
5447 bool is_partial;
5448 int new_template_p = 0;
5449 /* True if the template is a member template, in the sense of
5450 [temp.mem]. */
5451 bool member_template_p = false;
5452
5453 if (decl == error_mark_node || !current_template_parms)
5454 return error_mark_node;
5455
5456 /* See if this is a partial specialization. */
5457 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5458 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5459 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5460 || (VAR_P (decl)
5461 && DECL_LANG_SPECIFIC (decl)
5462 && DECL_TEMPLATE_SPECIALIZATION (decl)
5463 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5464
5465 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5466 is_friend = true;
5467
5468 if (is_friend)
5469 /* For a friend, we want the context of the friend, not
5470 the type of which it is a friend. */
5471 ctx = CP_DECL_CONTEXT (decl);
5472 else if (CP_DECL_CONTEXT (decl)
5473 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5474 /* In the case of a virtual function, we want the class in which
5475 it is defined. */
5476 ctx = CP_DECL_CONTEXT (decl);
5477 else
5478 /* Otherwise, if we're currently defining some class, the DECL
5479 is assumed to be a member of the class. */
5480 ctx = current_scope ();
5481
5482 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5483 ctx = NULL_TREE;
5484
5485 if (!DECL_CONTEXT (decl))
5486 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5487
5488 /* See if this is a primary template. */
5489 if (is_friend && ctx
5490 && uses_template_parms_level (ctx, processing_template_decl))
5491 /* A friend template that specifies a class context, i.e.
5492 template <typename T> friend void A<T>::f();
5493 is not primary. */
5494 is_primary = false;
5495 else if (TREE_CODE (decl) == TYPE_DECL
5496 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5497 is_primary = false;
5498 else
5499 is_primary = template_parm_scope_p ();
5500
5501 if (is_primary)
5502 {
5503 warning (OPT_Wtemplates, "template %qD declared", decl);
5504
5505 if (DECL_CLASS_SCOPE_P (decl))
5506 member_template_p = true;
5507 if (TREE_CODE (decl) == TYPE_DECL
5508 && anon_aggrname_p (DECL_NAME (decl)))
5509 {
5510 error ("template class without a name");
5511 return error_mark_node;
5512 }
5513 else if (TREE_CODE (decl) == FUNCTION_DECL)
5514 {
5515 if (member_template_p)
5516 {
5517 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5518 error ("member template %qD may not have virt-specifiers", decl);
5519 }
5520 if (DECL_DESTRUCTOR_P (decl))
5521 {
5522 /* [temp.mem]
5523
5524 A destructor shall not be a member template. */
5525 error ("destructor %qD declared as member template", decl);
5526 return error_mark_node;
5527 }
5528 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5529 && (!prototype_p (TREE_TYPE (decl))
5530 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5531 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5532 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5533 == void_list_node)))
5534 {
5535 /* [basic.stc.dynamic.allocation]
5536
5537 An allocation function can be a function
5538 template. ... Template allocation functions shall
5539 have two or more parameters. */
5540 error ("invalid template declaration of %qD", decl);
5541 return error_mark_node;
5542 }
5543 }
5544 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5545 && CLASS_TYPE_P (TREE_TYPE (decl)))
5546 {
5547 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5548 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5549 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5550 {
5551 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5552 if (TREE_CODE (t) == TYPE_DECL)
5553 t = TREE_TYPE (t);
5554 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5555 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5556 }
5557 }
5558 else if (TREE_CODE (decl) == TYPE_DECL
5559 && TYPE_DECL_ALIAS_P (decl))
5560 /* alias-declaration */
5561 gcc_assert (!DECL_ARTIFICIAL (decl));
5562 else if (VAR_P (decl))
5563 /* C++14 variable template. */;
5564 else
5565 {
5566 error ("template declaration of %q#D", decl);
5567 return error_mark_node;
5568 }
5569 }
5570
5571 /* Check to see that the rules regarding the use of default
5572 arguments are not being violated. We check args for a friend
5573 functions when we know whether it's a definition, introducing
5574 declaration or re-declaration. */
5575 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5576 check_default_tmpl_args (decl, current_template_parms,
5577 is_primary, is_partial, is_friend);
5578
5579 /* Ensure that there are no parameter packs in the type of this
5580 declaration that have not been expanded. */
5581 if (TREE_CODE (decl) == FUNCTION_DECL)
5582 {
5583 /* Check each of the arguments individually to see if there are
5584 any bare parameter packs. */
5585 tree type = TREE_TYPE (decl);
5586 tree arg = DECL_ARGUMENTS (decl);
5587 tree argtype = TYPE_ARG_TYPES (type);
5588
5589 while (arg && argtype)
5590 {
5591 if (!DECL_PACK_P (arg)
5592 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5593 {
5594 /* This is a PARM_DECL that contains unexpanded parameter
5595 packs. We have already complained about this in the
5596 check_for_bare_parameter_packs call, so just replace
5597 these types with ERROR_MARK_NODE. */
5598 TREE_TYPE (arg) = error_mark_node;
5599 TREE_VALUE (argtype) = error_mark_node;
5600 }
5601
5602 arg = DECL_CHAIN (arg);
5603 argtype = TREE_CHAIN (argtype);
5604 }
5605
5606 /* Check for bare parameter packs in the return type and the
5607 exception specifiers. */
5608 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5609 /* Errors were already issued, set return type to int
5610 as the frontend doesn't expect error_mark_node as
5611 the return type. */
5612 TREE_TYPE (type) = integer_type_node;
5613 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5614 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5615 }
5616 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5617 && TYPE_DECL_ALIAS_P (decl))
5618 ? DECL_ORIGINAL_TYPE (decl)
5619 : TREE_TYPE (decl)))
5620 {
5621 TREE_TYPE (decl) = error_mark_node;
5622 return error_mark_node;
5623 }
5624
5625 if (is_partial)
5626 return process_partial_specialization (decl);
5627
5628 args = current_template_args ();
5629
5630 if (!ctx
5631 || TREE_CODE (ctx) == FUNCTION_DECL
5632 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5633 || (TREE_CODE (decl) == TYPE_DECL
5634 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5635 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5636 {
5637 if (DECL_LANG_SPECIFIC (decl)
5638 && DECL_TEMPLATE_INFO (decl)
5639 && DECL_TI_TEMPLATE (decl))
5640 tmpl = DECL_TI_TEMPLATE (decl);
5641 /* If DECL is a TYPE_DECL for a class-template, then there won't
5642 be DECL_LANG_SPECIFIC. The information equivalent to
5643 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5644 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5645 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5646 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5647 {
5648 /* Since a template declaration already existed for this
5649 class-type, we must be redeclaring it here. Make sure
5650 that the redeclaration is valid. */
5651 redeclare_class_template (TREE_TYPE (decl),
5652 current_template_parms,
5653 current_template_constraints ());
5654 /* We don't need to create a new TEMPLATE_DECL; just use the
5655 one we already had. */
5656 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5657 }
5658 else
5659 {
5660 tmpl = build_template_decl (decl, current_template_parms,
5661 member_template_p);
5662 new_template_p = 1;
5663
5664 if (DECL_LANG_SPECIFIC (decl)
5665 && DECL_TEMPLATE_SPECIALIZATION (decl))
5666 {
5667 /* A specialization of a member template of a template
5668 class. */
5669 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5670 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5671 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5672 }
5673 }
5674 }
5675 else
5676 {
5677 tree a, t, current, parms;
5678 int i;
5679 tree tinfo = get_template_info (decl);
5680
5681 if (!tinfo)
5682 {
5683 error ("template definition of non-template %q#D", decl);
5684 return error_mark_node;
5685 }
5686
5687 tmpl = TI_TEMPLATE (tinfo);
5688
5689 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5690 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5691 && DECL_TEMPLATE_SPECIALIZATION (decl)
5692 && DECL_MEMBER_TEMPLATE_P (tmpl))
5693 {
5694 tree new_tmpl;
5695
5696 /* The declaration is a specialization of a member
5697 template, declared outside the class. Therefore, the
5698 innermost template arguments will be NULL, so we
5699 replace them with the arguments determined by the
5700 earlier call to check_explicit_specialization. */
5701 args = DECL_TI_ARGS (decl);
5702
5703 new_tmpl
5704 = build_template_decl (decl, current_template_parms,
5705 member_template_p);
5706 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5707 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5708 DECL_TI_TEMPLATE (decl) = new_tmpl;
5709 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5710 DECL_TEMPLATE_INFO (new_tmpl)
5711 = build_template_info (tmpl, args);
5712
5713 register_specialization (new_tmpl,
5714 most_general_template (tmpl),
5715 args,
5716 is_friend, 0);
5717 return decl;
5718 }
5719
5720 /* Make sure the template headers we got make sense. */
5721
5722 parms = DECL_TEMPLATE_PARMS (tmpl);
5723 i = TMPL_PARMS_DEPTH (parms);
5724 if (TMPL_ARGS_DEPTH (args) != i)
5725 {
5726 error ("expected %d levels of template parms for %q#D, got %d",
5727 i, decl, TMPL_ARGS_DEPTH (args));
5728 DECL_INTERFACE_KNOWN (decl) = 1;
5729 return error_mark_node;
5730 }
5731 else
5732 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5733 {
5734 a = TMPL_ARGS_LEVEL (args, i);
5735 t = INNERMOST_TEMPLATE_PARMS (parms);
5736
5737 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5738 {
5739 if (current == decl)
5740 error ("got %d template parameters for %q#D",
5741 TREE_VEC_LENGTH (a), decl);
5742 else
5743 error ("got %d template parameters for %q#T",
5744 TREE_VEC_LENGTH (a), current);
5745 error (" but %d required", TREE_VEC_LENGTH (t));
5746 /* Avoid crash in import_export_decl. */
5747 DECL_INTERFACE_KNOWN (decl) = 1;
5748 return error_mark_node;
5749 }
5750
5751 if (current == decl)
5752 current = ctx;
5753 else if (current == NULL_TREE)
5754 /* Can happen in erroneous input. */
5755 break;
5756 else
5757 current = get_containing_scope (current);
5758 }
5759
5760 /* Check that the parms are used in the appropriate qualifying scopes
5761 in the declarator. */
5762 if (!comp_template_args
5763 (TI_ARGS (tinfo),
5764 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5765 {
5766 error ("template arguments to %qD do not match original "
5767 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5768 if (!uses_template_parms (TI_ARGS (tinfo)))
5769 inform (input_location, "use %<template<>%> for"
5770 " an explicit specialization");
5771 /* Avoid crash in import_export_decl. */
5772 DECL_INTERFACE_KNOWN (decl) = 1;
5773 return error_mark_node;
5774 }
5775 }
5776
5777 DECL_TEMPLATE_RESULT (tmpl) = decl;
5778 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5779
5780 /* Push template declarations for global functions and types. Note
5781 that we do not try to push a global template friend declared in a
5782 template class; such a thing may well depend on the template
5783 parameters of the class. */
5784 if (new_template_p && !ctx
5785 && !(is_friend && template_class_depth (current_class_type) > 0))
5786 {
5787 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5788 if (tmpl == error_mark_node)
5789 return error_mark_node;
5790
5791 /* Hide template friend classes that haven't been declared yet. */
5792 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5793 {
5794 DECL_ANTICIPATED (tmpl) = 1;
5795 DECL_FRIEND_P (tmpl) = 1;
5796 }
5797 }
5798
5799 if (is_primary)
5800 {
5801 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5802
5803 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5804
5805 /* Give template template parms a DECL_CONTEXT of the template
5806 for which they are a parameter. */
5807 parms = INNERMOST_TEMPLATE_PARMS (parms);
5808 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5809 {
5810 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5811 if (TREE_CODE (parm) == TEMPLATE_DECL)
5812 DECL_CONTEXT (parm) = tmpl;
5813 }
5814
5815 if (TREE_CODE (decl) == TYPE_DECL
5816 && TYPE_DECL_ALIAS_P (decl)
5817 && complex_alias_template_p (tmpl))
5818 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5819 }
5820
5821 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5822 back to its most general template. If TMPL is a specialization,
5823 ARGS may only have the innermost set of arguments. Add the missing
5824 argument levels if necessary. */
5825 if (DECL_TEMPLATE_INFO (tmpl))
5826 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5827
5828 info = build_template_info (tmpl, args);
5829
5830 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5831 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5832 else
5833 {
5834 if (is_primary)
5835 retrofit_lang_decl (decl);
5836 if (DECL_LANG_SPECIFIC (decl))
5837 DECL_TEMPLATE_INFO (decl) = info;
5838 }
5839
5840 if (flag_implicit_templates
5841 && !is_friend
5842 && TREE_PUBLIC (decl)
5843 && VAR_OR_FUNCTION_DECL_P (decl))
5844 /* Set DECL_COMDAT on template instantiations; if we force
5845 them to be emitted by explicit instantiation or -frepo,
5846 mark_needed will tell cgraph to do the right thing. */
5847 DECL_COMDAT (decl) = true;
5848
5849 return DECL_TEMPLATE_RESULT (tmpl);
5850 }
5851
5852 tree
5853 push_template_decl (tree decl)
5854 {
5855 return push_template_decl_real (decl, false);
5856 }
5857
5858 /* FN is an inheriting constructor that inherits from the constructor
5859 template INHERITED; turn FN into a constructor template with a matching
5860 template header. */
5861
5862 tree
5863 add_inherited_template_parms (tree fn, tree inherited)
5864 {
5865 tree inner_parms
5866 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5867 inner_parms = copy_node (inner_parms);
5868 tree parms
5869 = tree_cons (size_int (processing_template_decl + 1),
5870 inner_parms, current_template_parms);
5871 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5872 tree args = template_parms_to_args (parms);
5873 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5874 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5875 DECL_TEMPLATE_RESULT (tmpl) = fn;
5876 DECL_ARTIFICIAL (tmpl) = true;
5877 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5878 return tmpl;
5879 }
5880
5881 /* Called when a class template TYPE is redeclared with the indicated
5882 template PARMS, e.g.:
5883
5884 template <class T> struct S;
5885 template <class T> struct S {}; */
5886
5887 bool
5888 redeclare_class_template (tree type, tree parms, tree cons)
5889 {
5890 tree tmpl;
5891 tree tmpl_parms;
5892 int i;
5893
5894 if (!TYPE_TEMPLATE_INFO (type))
5895 {
5896 error ("%qT is not a template type", type);
5897 return false;
5898 }
5899
5900 tmpl = TYPE_TI_TEMPLATE (type);
5901 if (!PRIMARY_TEMPLATE_P (tmpl))
5902 /* The type is nested in some template class. Nothing to worry
5903 about here; there are no new template parameters for the nested
5904 type. */
5905 return true;
5906
5907 if (!parms)
5908 {
5909 error ("template specifiers not specified in declaration of %qD",
5910 tmpl);
5911 return false;
5912 }
5913
5914 parms = INNERMOST_TEMPLATE_PARMS (parms);
5915 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5916
5917 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5918 {
5919 error_n (input_location, TREE_VEC_LENGTH (parms),
5920 "redeclared with %d template parameter",
5921 "redeclared with %d template parameters",
5922 TREE_VEC_LENGTH (parms));
5923 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5924 "previous declaration %qD used %d template parameter",
5925 "previous declaration %qD used %d template parameters",
5926 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5927 return false;
5928 }
5929
5930 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5931 {
5932 tree tmpl_parm;
5933 tree parm;
5934 tree tmpl_default;
5935 tree parm_default;
5936
5937 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5938 || TREE_VEC_ELT (parms, i) == error_mark_node)
5939 continue;
5940
5941 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5942 if (error_operand_p (tmpl_parm))
5943 return false;
5944
5945 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5946 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5947 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5948
5949 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5950 TEMPLATE_DECL. */
5951 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5952 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5953 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5954 || (TREE_CODE (tmpl_parm) != PARM_DECL
5955 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5956 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5957 || (TREE_CODE (tmpl_parm) == PARM_DECL
5958 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5959 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5960 {
5961 error ("template parameter %q+#D", tmpl_parm);
5962 error ("redeclared here as %q#D", parm);
5963 return false;
5964 }
5965
5966 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5967 {
5968 /* We have in [temp.param]:
5969
5970 A template-parameter may not be given default arguments
5971 by two different declarations in the same scope. */
5972 error_at (input_location, "redefinition of default argument for %q#D", parm);
5973 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5974 "original definition appeared here");
5975 return false;
5976 }
5977
5978 if (parm_default != NULL_TREE)
5979 /* Update the previous template parameters (which are the ones
5980 that will really count) with the new default value. */
5981 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5982 else if (tmpl_default != NULL_TREE)
5983 /* Update the new parameters, too; they'll be used as the
5984 parameters for any members. */
5985 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5986
5987 /* Give each template template parm in this redeclaration a
5988 DECL_CONTEXT of the template for which they are a parameter. */
5989 if (TREE_CODE (parm) == TEMPLATE_DECL)
5990 {
5991 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5992 DECL_CONTEXT (parm) = tmpl;
5993 }
5994
5995 if (TREE_CODE (parm) == TYPE_DECL)
5996 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5997 }
5998
5999 // Cannot redeclare a class template with a different set of constraints.
6000 if (!equivalent_constraints (get_constraints (tmpl), cons))
6001 {
6002 error_at (input_location, "redeclaration %q#D with different "
6003 "constraints", tmpl);
6004 inform (DECL_SOURCE_LOCATION (tmpl),
6005 "original declaration appeared here");
6006 }
6007
6008 return true;
6009 }
6010
6011 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6012 to be used when the caller has already checked
6013 (processing_template_decl
6014 && !instantiation_dependent_expression_p (expr)
6015 && potential_constant_expression (expr))
6016 and cleared processing_template_decl. */
6017
6018 tree
6019 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6020 {
6021 return tsubst_copy_and_build (expr,
6022 /*args=*/NULL_TREE,
6023 complain,
6024 /*in_decl=*/NULL_TREE,
6025 /*function_p=*/false,
6026 /*integral_constant_expression_p=*/true);
6027 }
6028
6029 /* Simplify EXPR if it is a non-dependent expression. Returns the
6030 (possibly simplified) expression. */
6031
6032 tree
6033 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6034 {
6035 if (expr == NULL_TREE)
6036 return NULL_TREE;
6037
6038 /* If we're in a template, but EXPR isn't value dependent, simplify
6039 it. We're supposed to treat:
6040
6041 template <typename T> void f(T[1 + 1]);
6042 template <typename T> void f(T[2]);
6043
6044 as two declarations of the same function, for example. */
6045 if (processing_template_decl
6046 && is_nondependent_constant_expression (expr))
6047 {
6048 processing_template_decl_sentinel s;
6049 expr = instantiate_non_dependent_expr_internal (expr, complain);
6050 }
6051 return expr;
6052 }
6053
6054 tree
6055 instantiate_non_dependent_expr (tree expr)
6056 {
6057 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6058 }
6059
6060 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6061 an uninstantiated expression. */
6062
6063 tree
6064 instantiate_non_dependent_or_null (tree expr)
6065 {
6066 if (expr == NULL_TREE)
6067 return NULL_TREE;
6068 if (processing_template_decl)
6069 {
6070 if (!is_nondependent_constant_expression (expr))
6071 expr = NULL_TREE;
6072 else
6073 {
6074 processing_template_decl_sentinel s;
6075 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6076 }
6077 }
6078 return expr;
6079 }
6080
6081 /* True iff T is a specialization of a variable template. */
6082
6083 bool
6084 variable_template_specialization_p (tree t)
6085 {
6086 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6087 return false;
6088 tree tmpl = DECL_TI_TEMPLATE (t);
6089 return variable_template_p (tmpl);
6090 }
6091
6092 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6093 template declaration, or a TYPE_DECL for an alias declaration. */
6094
6095 bool
6096 alias_type_or_template_p (tree t)
6097 {
6098 if (t == NULL_TREE)
6099 return false;
6100 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6101 || (TYPE_P (t)
6102 && TYPE_NAME (t)
6103 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6104 || DECL_ALIAS_TEMPLATE_P (t));
6105 }
6106
6107 /* Return TRUE iff T is a specialization of an alias template. */
6108
6109 bool
6110 alias_template_specialization_p (const_tree t)
6111 {
6112 /* It's an alias template specialization if it's an alias and its
6113 TYPE_NAME is a specialization of a primary template. */
6114 if (TYPE_ALIAS_P (t))
6115 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6116 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6117
6118 return false;
6119 }
6120
6121 /* An alias template is complex from a SFINAE perspective if a template-id
6122 using that alias can be ill-formed when the expansion is not, as with
6123 the void_t template. We determine this by checking whether the
6124 expansion for the alias template uses all its template parameters. */
6125
6126 struct uses_all_template_parms_data
6127 {
6128 int level;
6129 bool *seen;
6130 };
6131
6132 static int
6133 uses_all_template_parms_r (tree t, void *data_)
6134 {
6135 struct uses_all_template_parms_data &data
6136 = *(struct uses_all_template_parms_data*)data_;
6137 tree idx = get_template_parm_index (t);
6138
6139 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6140 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6141 return 0;
6142 }
6143
6144 static bool
6145 complex_alias_template_p (const_tree tmpl)
6146 {
6147 struct uses_all_template_parms_data data;
6148 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6149 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6150 data.level = TMPL_PARMS_DEPTH (parms);
6151 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6152 data.seen = XALLOCAVEC (bool, len);
6153 for (int i = 0; i < len; ++i)
6154 data.seen[i] = false;
6155
6156 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6157 for (int i = 0; i < len; ++i)
6158 if (!data.seen[i])
6159 return true;
6160 return false;
6161 }
6162
6163 /* Return TRUE iff T is a specialization of a complex alias template with
6164 dependent template-arguments. */
6165
6166 bool
6167 dependent_alias_template_spec_p (const_tree t)
6168 {
6169 if (!alias_template_specialization_p (t))
6170 return false;
6171
6172 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6173 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6174 return false;
6175
6176 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6177 if (!any_dependent_template_arguments_p (args))
6178 return false;
6179
6180 return true;
6181 }
6182
6183 /* Return the number of innermost template parameters in TMPL. */
6184
6185 static int
6186 num_innermost_template_parms (tree tmpl)
6187 {
6188 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6189 return TREE_VEC_LENGTH (parms);
6190 }
6191
6192 /* Return either TMPL or another template that it is equivalent to under DR
6193 1286: An alias that just changes the name of a template is equivalent to
6194 the other template. */
6195
6196 static tree
6197 get_underlying_template (tree tmpl)
6198 {
6199 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6200 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6201 {
6202 /* Determine if the alias is equivalent to an underlying template. */
6203 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6204 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6205 if (!tinfo)
6206 break;
6207
6208 tree underlying = TI_TEMPLATE (tinfo);
6209 if (!PRIMARY_TEMPLATE_P (underlying)
6210 || (num_innermost_template_parms (tmpl)
6211 != num_innermost_template_parms (underlying)))
6212 break;
6213
6214 tree alias_args = INNERMOST_TEMPLATE_ARGS
6215 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6216 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6217 break;
6218
6219 /* Alias is equivalent. Strip it and repeat. */
6220 tmpl = underlying;
6221 }
6222
6223 return tmpl;
6224 }
6225
6226 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6227 must be a reference-to-function or a pointer-to-function type, as specified
6228 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6229 and check that the resulting function has external linkage. */
6230
6231 static tree
6232 convert_nontype_argument_function (tree type, tree expr,
6233 tsubst_flags_t complain)
6234 {
6235 tree fns = expr;
6236 tree fn, fn_no_ptr;
6237 linkage_kind linkage;
6238
6239 fn = instantiate_type (type, fns, tf_none);
6240 if (fn == error_mark_node)
6241 return error_mark_node;
6242
6243 if (value_dependent_expression_p (fn))
6244 goto accept;
6245
6246 fn_no_ptr = strip_fnptr_conv (fn);
6247 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6248 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6249 if (BASELINK_P (fn_no_ptr))
6250 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6251
6252 /* [temp.arg.nontype]/1
6253
6254 A template-argument for a non-type, non-template template-parameter
6255 shall be one of:
6256 [...]
6257 -- the address of an object or function with external [C++11: or
6258 internal] linkage. */
6259
6260 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6261 {
6262 if (complain & tf_error)
6263 {
6264 error ("%qE is not a valid template argument for type %qT",
6265 expr, type);
6266 if (TYPE_PTR_P (type))
6267 inform (input_location, "it must be the address of a function "
6268 "with external linkage");
6269 else
6270 inform (input_location, "it must be the name of a function with "
6271 "external linkage");
6272 }
6273 return NULL_TREE;
6274 }
6275
6276 linkage = decl_linkage (fn_no_ptr);
6277 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6278 {
6279 if (complain & tf_error)
6280 {
6281 if (cxx_dialect >= cxx11)
6282 error ("%qE is not a valid template argument for type %qT "
6283 "because %qD has no linkage",
6284 expr, type, fn_no_ptr);
6285 else
6286 error ("%qE is not a valid template argument for type %qT "
6287 "because %qD does not have external linkage",
6288 expr, type, fn_no_ptr);
6289 }
6290 return NULL_TREE;
6291 }
6292
6293 accept:
6294 if (TYPE_REF_P (type))
6295 {
6296 if (REFERENCE_REF_P (fn))
6297 fn = TREE_OPERAND (fn, 0);
6298 else
6299 fn = build_address (fn);
6300 }
6301 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6302 fn = build_nop (type, fn);
6303
6304 return fn;
6305 }
6306
6307 /* Subroutine of convert_nontype_argument.
6308 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6309 Emit an error otherwise. */
6310
6311 static bool
6312 check_valid_ptrmem_cst_expr (tree type, tree expr,
6313 tsubst_flags_t complain)
6314 {
6315 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6316 tree orig_expr = expr;
6317 STRIP_NOPS (expr);
6318 if (null_ptr_cst_p (expr))
6319 return true;
6320 if (TREE_CODE (expr) == PTRMEM_CST
6321 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6322 PTRMEM_CST_CLASS (expr)))
6323 return true;
6324 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6325 return true;
6326 if (processing_template_decl
6327 && TREE_CODE (expr) == ADDR_EXPR
6328 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6329 return true;
6330 if (complain & tf_error)
6331 {
6332 error_at (loc, "%qE is not a valid template argument for type %qT",
6333 orig_expr, type);
6334 if (TREE_CODE (expr) != PTRMEM_CST)
6335 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6336 else
6337 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6338 }
6339 return false;
6340 }
6341
6342 /* Returns TRUE iff the address of OP is value-dependent.
6343
6344 14.6.2.4 [temp.dep.temp]:
6345 A non-integral non-type template-argument is dependent if its type is
6346 dependent or it has either of the following forms
6347 qualified-id
6348 & qualified-id
6349 and contains a nested-name-specifier which specifies a class-name that
6350 names a dependent type.
6351
6352 We generalize this to just say that the address of a member of a
6353 dependent class is value-dependent; the above doesn't cover the
6354 address of a static data member named with an unqualified-id. */
6355
6356 static bool
6357 has_value_dependent_address (tree op)
6358 {
6359 /* We could use get_inner_reference here, but there's no need;
6360 this is only relevant for template non-type arguments, which
6361 can only be expressed as &id-expression. */
6362 if (DECL_P (op))
6363 {
6364 tree ctx = CP_DECL_CONTEXT (op);
6365 if (TYPE_P (ctx) && dependent_type_p (ctx))
6366 return true;
6367 }
6368
6369 return false;
6370 }
6371
6372 /* The next set of functions are used for providing helpful explanatory
6373 diagnostics for failed overload resolution. Their messages should be
6374 indented by two spaces for consistency with the messages in
6375 call.c */
6376
6377 static int
6378 unify_success (bool /*explain_p*/)
6379 {
6380 return 0;
6381 }
6382
6383 /* Other failure functions should call this one, to provide a single function
6384 for setting a breakpoint on. */
6385
6386 static int
6387 unify_invalid (bool /*explain_p*/)
6388 {
6389 return 1;
6390 }
6391
6392 static int
6393 unify_parameter_deduction_failure (bool explain_p, tree parm)
6394 {
6395 if (explain_p)
6396 inform (input_location,
6397 " couldn't deduce template parameter %qD", parm);
6398 return unify_invalid (explain_p);
6399 }
6400
6401 static int
6402 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6403 {
6404 if (explain_p)
6405 inform (input_location,
6406 " types %qT and %qT have incompatible cv-qualifiers",
6407 parm, arg);
6408 return unify_invalid (explain_p);
6409 }
6410
6411 static int
6412 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6413 {
6414 if (explain_p)
6415 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6416 return unify_invalid (explain_p);
6417 }
6418
6419 static int
6420 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6421 {
6422 if (explain_p)
6423 inform (input_location,
6424 " template parameter %qD is not a parameter pack, but "
6425 "argument %qD is",
6426 parm, arg);
6427 return unify_invalid (explain_p);
6428 }
6429
6430 static int
6431 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6432 {
6433 if (explain_p)
6434 inform (input_location,
6435 " template argument %qE does not match "
6436 "pointer-to-member constant %qE",
6437 arg, parm);
6438 return unify_invalid (explain_p);
6439 }
6440
6441 static int
6442 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6443 {
6444 if (explain_p)
6445 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6446 return unify_invalid (explain_p);
6447 }
6448
6449 static int
6450 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6451 {
6452 if (explain_p)
6453 inform (input_location,
6454 " inconsistent parameter pack deduction with %qT and %qT",
6455 old_arg, new_arg);
6456 return unify_invalid (explain_p);
6457 }
6458
6459 static int
6460 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6461 {
6462 if (explain_p)
6463 {
6464 if (TYPE_P (parm))
6465 inform (input_location,
6466 " deduced conflicting types for parameter %qT (%qT and %qT)",
6467 parm, first, second);
6468 else
6469 inform (input_location,
6470 " deduced conflicting values for non-type parameter "
6471 "%qE (%qE and %qE)", parm, first, second);
6472 }
6473 return unify_invalid (explain_p);
6474 }
6475
6476 static int
6477 unify_vla_arg (bool explain_p, tree arg)
6478 {
6479 if (explain_p)
6480 inform (input_location,
6481 " variable-sized array type %qT is not "
6482 "a valid template argument",
6483 arg);
6484 return unify_invalid (explain_p);
6485 }
6486
6487 static int
6488 unify_method_type_error (bool explain_p, tree arg)
6489 {
6490 if (explain_p)
6491 inform (input_location,
6492 " member function type %qT is not a valid template argument",
6493 arg);
6494 return unify_invalid (explain_p);
6495 }
6496
6497 static int
6498 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6499 {
6500 if (explain_p)
6501 {
6502 if (least_p)
6503 inform_n (input_location, wanted,
6504 " candidate expects at least %d argument, %d provided",
6505 " candidate expects at least %d arguments, %d provided",
6506 wanted, have);
6507 else
6508 inform_n (input_location, wanted,
6509 " candidate expects %d argument, %d provided",
6510 " candidate expects %d arguments, %d provided",
6511 wanted, have);
6512 }
6513 return unify_invalid (explain_p);
6514 }
6515
6516 static int
6517 unify_too_many_arguments (bool explain_p, int have, int wanted)
6518 {
6519 return unify_arity (explain_p, have, wanted);
6520 }
6521
6522 static int
6523 unify_too_few_arguments (bool explain_p, int have, int wanted,
6524 bool least_p = false)
6525 {
6526 return unify_arity (explain_p, have, wanted, least_p);
6527 }
6528
6529 static int
6530 unify_arg_conversion (bool explain_p, tree to_type,
6531 tree from_type, tree arg)
6532 {
6533 if (explain_p)
6534 inform (cp_expr_loc_or_loc (arg, input_location),
6535 " cannot convert %qE (type %qT) to type %qT",
6536 arg, from_type, to_type);
6537 return unify_invalid (explain_p);
6538 }
6539
6540 static int
6541 unify_no_common_base (bool explain_p, enum template_base_result r,
6542 tree parm, tree arg)
6543 {
6544 if (explain_p)
6545 switch (r)
6546 {
6547 case tbr_ambiguous_baseclass:
6548 inform (input_location, " %qT is an ambiguous base class of %qT",
6549 parm, arg);
6550 break;
6551 default:
6552 inform (input_location, " %qT is not derived from %qT", arg, parm);
6553 break;
6554 }
6555 return unify_invalid (explain_p);
6556 }
6557
6558 static int
6559 unify_inconsistent_template_template_parameters (bool explain_p)
6560 {
6561 if (explain_p)
6562 inform (input_location,
6563 " template parameters of a template template argument are "
6564 "inconsistent with other deduced template arguments");
6565 return unify_invalid (explain_p);
6566 }
6567
6568 static int
6569 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6570 {
6571 if (explain_p)
6572 inform (input_location,
6573 " can't deduce a template for %qT from non-template type %qT",
6574 parm, arg);
6575 return unify_invalid (explain_p);
6576 }
6577
6578 static int
6579 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6580 {
6581 if (explain_p)
6582 inform (input_location,
6583 " template argument %qE does not match %qE", arg, parm);
6584 return unify_invalid (explain_p);
6585 }
6586
6587 /* Attempt to convert the non-type template parameter EXPR to the
6588 indicated TYPE. If the conversion is successful, return the
6589 converted value. If the conversion is unsuccessful, return
6590 NULL_TREE if we issued an error message, or error_mark_node if we
6591 did not. We issue error messages for out-and-out bad template
6592 parameters, but not simply because the conversion failed, since we
6593 might be just trying to do argument deduction. Both TYPE and EXPR
6594 must be non-dependent.
6595
6596 The conversion follows the special rules described in
6597 [temp.arg.nontype], and it is much more strict than an implicit
6598 conversion.
6599
6600 This function is called twice for each template argument (see
6601 lookup_template_class for a more accurate description of this
6602 problem). This means that we need to handle expressions which
6603 are not valid in a C++ source, but can be created from the
6604 first call (for instance, casts to perform conversions). These
6605 hacks can go away after we fix the double coercion problem. */
6606
6607 static tree
6608 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6609 {
6610 tree expr_type;
6611 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6612 tree orig_expr = expr;
6613
6614 /* Detect immediately string literals as invalid non-type argument.
6615 This special-case is not needed for correctness (we would easily
6616 catch this later), but only to provide better diagnostic for this
6617 common user mistake. As suggested by DR 100, we do not mention
6618 linkage issues in the diagnostic as this is not the point. */
6619 /* FIXME we're making this OK. */
6620 if (TREE_CODE (expr) == STRING_CST)
6621 {
6622 if (complain & tf_error)
6623 error ("%qE is not a valid template argument for type %qT "
6624 "because string literals can never be used in this context",
6625 expr, type);
6626 return NULL_TREE;
6627 }
6628
6629 /* Add the ADDR_EXPR now for the benefit of
6630 value_dependent_expression_p. */
6631 if (TYPE_PTROBV_P (type)
6632 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6633 {
6634 expr = decay_conversion (expr, complain);
6635 if (expr == error_mark_node)
6636 return error_mark_node;
6637 }
6638
6639 /* If we are in a template, EXPR may be non-dependent, but still
6640 have a syntactic, rather than semantic, form. For example, EXPR
6641 might be a SCOPE_REF, rather than the VAR_DECL to which the
6642 SCOPE_REF refers. Preserving the qualifying scope is necessary
6643 so that access checking can be performed when the template is
6644 instantiated -- but here we need the resolved form so that we can
6645 convert the argument. */
6646 bool non_dep = false;
6647 if (TYPE_REF_OBJ_P (type)
6648 && has_value_dependent_address (expr))
6649 /* If we want the address and it's value-dependent, don't fold. */;
6650 else if (processing_template_decl
6651 && is_nondependent_constant_expression (expr))
6652 non_dep = true;
6653 if (error_operand_p (expr))
6654 return error_mark_node;
6655 expr_type = TREE_TYPE (expr);
6656
6657 /* If the argument is non-dependent, perform any conversions in
6658 non-dependent context as well. */
6659 processing_template_decl_sentinel s (non_dep);
6660 if (non_dep)
6661 expr = instantiate_non_dependent_expr_internal (expr, complain);
6662
6663 if (value_dependent_expression_p (expr))
6664 expr = canonicalize_expr_argument (expr, complain);
6665
6666 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6667 to a non-type argument of "nullptr". */
6668 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6669 expr = fold_simple (convert (type, expr));
6670
6671 /* In C++11, integral or enumeration non-type template arguments can be
6672 arbitrary constant expressions. Pointer and pointer to
6673 member arguments can be general constant expressions that evaluate
6674 to a null value, but otherwise still need to be of a specific form. */
6675 if (cxx_dialect >= cxx11)
6676 {
6677 if (TREE_CODE (expr) == PTRMEM_CST)
6678 /* A PTRMEM_CST is already constant, and a valid template
6679 argument for a parameter of pointer to member type, we just want
6680 to leave it in that form rather than lower it to a
6681 CONSTRUCTOR. */;
6682 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6683 || cxx_dialect >= cxx17)
6684 {
6685 /* C++17: A template-argument for a non-type template-parameter shall
6686 be a converted constant expression (8.20) of the type of the
6687 template-parameter. */
6688 expr = build_converted_constant_expr (type, expr, complain);
6689 if (expr == error_mark_node)
6690 /* Make sure we return NULL_TREE only if we have really issued
6691 an error, as described above. */
6692 return (complain & tf_error) ? NULL_TREE : error_mark_node;
6693 expr = maybe_constant_value (expr);
6694 expr = convert_from_reference (expr);
6695 }
6696 else if (TYPE_PTR_OR_PTRMEM_P (type))
6697 {
6698 tree folded = maybe_constant_value (expr);
6699 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6700 : null_member_pointer_value_p (folded))
6701 expr = folded;
6702 }
6703 }
6704
6705 if (TYPE_REF_P (type))
6706 expr = mark_lvalue_use (expr);
6707 else
6708 expr = mark_rvalue_use (expr);
6709
6710 /* HACK: Due to double coercion, we can get a
6711 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6712 which is the tree that we built on the first call (see
6713 below when coercing to reference to object or to reference to
6714 function). We just strip everything and get to the arg.
6715 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6716 for examples. */
6717 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6718 {
6719 tree probe_type, probe = expr;
6720 if (REFERENCE_REF_P (probe))
6721 probe = TREE_OPERAND (probe, 0);
6722 probe_type = TREE_TYPE (probe);
6723 if (TREE_CODE (probe) == NOP_EXPR)
6724 {
6725 /* ??? Maybe we could use convert_from_reference here, but we
6726 would need to relax its constraints because the NOP_EXPR
6727 could actually change the type to something more cv-qualified,
6728 and this is not folded by convert_from_reference. */
6729 tree addr = TREE_OPERAND (probe, 0);
6730 if (TYPE_REF_P (probe_type)
6731 && TREE_CODE (addr) == ADDR_EXPR
6732 && TYPE_PTR_P (TREE_TYPE (addr))
6733 && (same_type_ignoring_top_level_qualifiers_p
6734 (TREE_TYPE (probe_type),
6735 TREE_TYPE (TREE_TYPE (addr)))))
6736 {
6737 expr = TREE_OPERAND (addr, 0);
6738 expr_type = TREE_TYPE (probe_type);
6739 }
6740 }
6741 }
6742
6743 /* [temp.arg.nontype]/5, bullet 1
6744
6745 For a non-type template-parameter of integral or enumeration type,
6746 integral promotions (_conv.prom_) and integral conversions
6747 (_conv.integral_) are applied. */
6748 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6749 {
6750 if (cxx_dialect < cxx11)
6751 {
6752 tree t = build_converted_constant_expr (type, expr, complain);
6753 t = maybe_constant_value (t);
6754 if (t != error_mark_node)
6755 expr = t;
6756 }
6757
6758 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6759 return error_mark_node;
6760
6761 /* Notice that there are constant expressions like '4 % 0' which
6762 do not fold into integer constants. */
6763 if (TREE_CODE (expr) != INTEGER_CST
6764 && !value_dependent_expression_p (expr))
6765 {
6766 if (complain & tf_error)
6767 {
6768 int errs = errorcount, warns = warningcount + werrorcount;
6769 if (!require_potential_constant_expression (expr))
6770 expr = error_mark_node;
6771 else
6772 expr = cxx_constant_value (expr);
6773 if (errorcount > errs || warningcount + werrorcount > warns)
6774 inform (loc, "in template argument for type %qT", type);
6775 if (expr == error_mark_node)
6776 return NULL_TREE;
6777 /* else cxx_constant_value complained but gave us
6778 a real constant, so go ahead. */
6779 if (TREE_CODE (expr) != INTEGER_CST)
6780 {
6781 /* Some assemble time constant expressions like
6782 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6783 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6784 as we can emit them into .rodata initializers of
6785 variables, yet they can't fold into an INTEGER_CST at
6786 compile time. Refuse them here. */
6787 gcc_checking_assert (reduced_constant_expression_p (expr));
6788 error_at (loc, "template argument %qE for type %qT not "
6789 "a constant integer", expr, type);
6790 return NULL_TREE;
6791 }
6792 }
6793 else
6794 return NULL_TREE;
6795 }
6796
6797 /* Avoid typedef problems. */
6798 if (TREE_TYPE (expr) != type)
6799 expr = fold_convert (type, expr);
6800 }
6801 /* [temp.arg.nontype]/5, bullet 2
6802
6803 For a non-type template-parameter of type pointer to object,
6804 qualification conversions (_conv.qual_) and the array-to-pointer
6805 conversion (_conv.array_) are applied. */
6806 else if (TYPE_PTROBV_P (type))
6807 {
6808 tree decayed = expr;
6809
6810 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6811 decay_conversion or an explicit cast. If it's a problematic cast,
6812 we'll complain about it below. */
6813 if (TREE_CODE (expr) == NOP_EXPR)
6814 {
6815 tree probe = expr;
6816 STRIP_NOPS (probe);
6817 if (TREE_CODE (probe) == ADDR_EXPR
6818 && TYPE_PTR_P (TREE_TYPE (probe)))
6819 {
6820 expr = probe;
6821 expr_type = TREE_TYPE (expr);
6822 }
6823 }
6824
6825 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6826
6827 A template-argument for a non-type, non-template template-parameter
6828 shall be one of: [...]
6829
6830 -- the name of a non-type template-parameter;
6831 -- the address of an object or function with external linkage, [...]
6832 expressed as "& id-expression" where the & is optional if the name
6833 refers to a function or array, or if the corresponding
6834 template-parameter is a reference.
6835
6836 Here, we do not care about functions, as they are invalid anyway
6837 for a parameter of type pointer-to-object. */
6838
6839 if (value_dependent_expression_p (expr))
6840 /* Non-type template parameters are OK. */
6841 ;
6842 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6843 /* Null pointer values are OK in C++11. */;
6844 else if (TREE_CODE (expr) != ADDR_EXPR)
6845 {
6846 if (VAR_P (expr))
6847 {
6848 if (complain & tf_error)
6849 error ("%qD is not a valid template argument "
6850 "because %qD is a variable, not the address of "
6851 "a variable", orig_expr, expr);
6852 return NULL_TREE;
6853 }
6854 if (INDIRECT_TYPE_P (expr_type))
6855 {
6856 if (complain & tf_error)
6857 error ("%qE is not a valid template argument for %qT "
6858 "because it is not the address of a variable",
6859 orig_expr, type);
6860 return NULL_TREE;
6861 }
6862 /* Other values, like integer constants, might be valid
6863 non-type arguments of some other type. */
6864 return error_mark_node;
6865 }
6866 else
6867 {
6868 tree decl = TREE_OPERAND (expr, 0);
6869
6870 if (!VAR_P (decl))
6871 {
6872 if (complain & tf_error)
6873 error ("%qE is not a valid template argument of type %qT "
6874 "because %qE is not a variable", orig_expr, type, decl);
6875 return NULL_TREE;
6876 }
6877 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6878 {
6879 if (complain & tf_error)
6880 error ("%qE is not a valid template argument of type %qT "
6881 "because %qD does not have external linkage",
6882 orig_expr, type, decl);
6883 return NULL_TREE;
6884 }
6885 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6886 && decl_linkage (decl) == lk_none)
6887 {
6888 if (complain & tf_error)
6889 error ("%qE is not a valid template argument of type %qT "
6890 "because %qD has no linkage", orig_expr, type, decl);
6891 return NULL_TREE;
6892 }
6893 /* C++17: For a non-type template-parameter of reference or pointer
6894 type, the value of the constant expression shall not refer to (or
6895 for a pointer type, shall not be the address of):
6896 * a subobject (4.5),
6897 * a temporary object (15.2),
6898 * a string literal (5.13.5),
6899 * the result of a typeid expression (8.2.8), or
6900 * a predefined __func__ variable (11.4.1). */
6901 else if (DECL_ARTIFICIAL (decl))
6902 {
6903 if (complain & tf_error)
6904 error ("the address of %qD is not a valid template argument",
6905 decl);
6906 return NULL_TREE;
6907 }
6908 else if (!same_type_ignoring_top_level_qualifiers_p
6909 (strip_array_types (TREE_TYPE (type)),
6910 strip_array_types (TREE_TYPE (decl))))
6911 {
6912 if (complain & tf_error)
6913 error ("the address of the %qT subobject of %qD is not a "
6914 "valid template argument", TREE_TYPE (type), decl);
6915 return NULL_TREE;
6916 }
6917 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6918 {
6919 if (complain & tf_error)
6920 error ("the address of %qD is not a valid template argument "
6921 "because it does not have static storage duration",
6922 decl);
6923 return NULL_TREE;
6924 }
6925 }
6926
6927 expr = decayed;
6928
6929 expr = perform_qualification_conversions (type, expr);
6930 if (expr == error_mark_node)
6931 return error_mark_node;
6932 }
6933 /* [temp.arg.nontype]/5, bullet 3
6934
6935 For a non-type template-parameter of type reference to object, no
6936 conversions apply. The type referred to by the reference may be more
6937 cv-qualified than the (otherwise identical) type of the
6938 template-argument. The template-parameter is bound directly to the
6939 template-argument, which must be an lvalue. */
6940 else if (TYPE_REF_OBJ_P (type))
6941 {
6942 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6943 expr_type))
6944 return error_mark_node;
6945
6946 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6947 {
6948 if (complain & tf_error)
6949 error ("%qE is not a valid template argument for type %qT "
6950 "because of conflicts in cv-qualification", expr, type);
6951 return NULL_TREE;
6952 }
6953
6954 if (!lvalue_p (expr))
6955 {
6956 if (complain & tf_error)
6957 error ("%qE is not a valid template argument for type %qT "
6958 "because it is not an lvalue", expr, type);
6959 return NULL_TREE;
6960 }
6961
6962 /* [temp.arg.nontype]/1
6963
6964 A template-argument for a non-type, non-template template-parameter
6965 shall be one of: [...]
6966
6967 -- the address of an object or function with external linkage. */
6968 if (INDIRECT_REF_P (expr)
6969 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6970 {
6971 expr = TREE_OPERAND (expr, 0);
6972 if (DECL_P (expr))
6973 {
6974 if (complain & tf_error)
6975 error ("%q#D is not a valid template argument for type %qT "
6976 "because a reference variable does not have a constant "
6977 "address", expr, type);
6978 return NULL_TREE;
6979 }
6980 }
6981
6982 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6983 && value_dependent_expression_p (expr))
6984 /* OK, dependent reference. We don't want to ask whether a DECL is
6985 itself value-dependent, since what we want here is its address. */;
6986 else
6987 {
6988 if (!DECL_P (expr))
6989 {
6990 if (complain & tf_error)
6991 error ("%qE is not a valid template argument for type %qT "
6992 "because it is not an object with linkage",
6993 expr, type);
6994 return NULL_TREE;
6995 }
6996
6997 /* DR 1155 allows internal linkage in C++11 and up. */
6998 linkage_kind linkage = decl_linkage (expr);
6999 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
7000 {
7001 if (complain & tf_error)
7002 error ("%qE is not a valid template argument for type %qT "
7003 "because object %qD does not have linkage",
7004 expr, type, expr);
7005 return NULL_TREE;
7006 }
7007
7008 expr = build_address (expr);
7009 }
7010
7011 if (!same_type_p (type, TREE_TYPE (expr)))
7012 expr = build_nop (type, expr);
7013 }
7014 /* [temp.arg.nontype]/5, bullet 4
7015
7016 For a non-type template-parameter of type pointer to function, only
7017 the function-to-pointer conversion (_conv.func_) is applied. If the
7018 template-argument represents a set of overloaded functions (or a
7019 pointer to such), the matching function is selected from the set
7020 (_over.over_). */
7021 else if (TYPE_PTRFN_P (type))
7022 {
7023 /* If the argument is a template-id, we might not have enough
7024 context information to decay the pointer. */
7025 if (!type_unknown_p (expr_type))
7026 {
7027 expr = decay_conversion (expr, complain);
7028 if (expr == error_mark_node)
7029 return error_mark_node;
7030 }
7031
7032 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7033 /* Null pointer values are OK in C++11. */
7034 return perform_qualification_conversions (type, expr);
7035
7036 expr = convert_nontype_argument_function (type, expr, complain);
7037 if (!expr || expr == error_mark_node)
7038 return expr;
7039 }
7040 /* [temp.arg.nontype]/5, bullet 5
7041
7042 For a non-type template-parameter of type reference to function, no
7043 conversions apply. If the template-argument represents a set of
7044 overloaded functions, the matching function is selected from the set
7045 (_over.over_). */
7046 else if (TYPE_REFFN_P (type))
7047 {
7048 if (TREE_CODE (expr) == ADDR_EXPR)
7049 {
7050 if (complain & tf_error)
7051 {
7052 error ("%qE is not a valid template argument for type %qT "
7053 "because it is a pointer", expr, type);
7054 inform (input_location, "try using %qE instead",
7055 TREE_OPERAND (expr, 0));
7056 }
7057 return NULL_TREE;
7058 }
7059
7060 expr = convert_nontype_argument_function (type, expr, complain);
7061 if (!expr || expr == error_mark_node)
7062 return expr;
7063 }
7064 /* [temp.arg.nontype]/5, bullet 6
7065
7066 For a non-type template-parameter of type pointer to member function,
7067 no conversions apply. If the template-argument represents a set of
7068 overloaded member functions, the matching member function is selected
7069 from the set (_over.over_). */
7070 else if (TYPE_PTRMEMFUNC_P (type))
7071 {
7072 expr = instantiate_type (type, expr, tf_none);
7073 if (expr == error_mark_node)
7074 return error_mark_node;
7075
7076 /* [temp.arg.nontype] bullet 1 says the pointer to member
7077 expression must be a pointer-to-member constant. */
7078 if (!value_dependent_expression_p (expr)
7079 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7080 return NULL_TREE;
7081
7082 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7083 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7084 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7085 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7086 }
7087 /* [temp.arg.nontype]/5, bullet 7
7088
7089 For a non-type template-parameter of type pointer to data member,
7090 qualification conversions (_conv.qual_) are applied. */
7091 else if (TYPE_PTRDATAMEM_P (type))
7092 {
7093 /* [temp.arg.nontype] bullet 1 says the pointer to member
7094 expression must be a pointer-to-member constant. */
7095 if (!value_dependent_expression_p (expr)
7096 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7097 return NULL_TREE;
7098
7099 expr = perform_qualification_conversions (type, expr);
7100 if (expr == error_mark_node)
7101 return expr;
7102 }
7103 else if (NULLPTR_TYPE_P (type))
7104 {
7105 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7106 {
7107 if (complain & tf_error)
7108 error ("%qE is not a valid template argument for type %qT "
7109 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7110 return NULL_TREE;
7111 }
7112 return expr;
7113 }
7114 /* A template non-type parameter must be one of the above. */
7115 else
7116 gcc_unreachable ();
7117
7118 /* Sanity check: did we actually convert the argument to the
7119 right type? */
7120 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7121 (type, TREE_TYPE (expr)));
7122 return convert_from_reference (expr);
7123 }
7124
7125 /* Subroutine of coerce_template_template_parms, which returns 1 if
7126 PARM_PARM and ARG_PARM match using the rule for the template
7127 parameters of template template parameters. Both PARM and ARG are
7128 template parameters; the rest of the arguments are the same as for
7129 coerce_template_template_parms.
7130 */
7131 static int
7132 coerce_template_template_parm (tree parm,
7133 tree arg,
7134 tsubst_flags_t complain,
7135 tree in_decl,
7136 tree outer_args)
7137 {
7138 if (arg == NULL_TREE || error_operand_p (arg)
7139 || parm == NULL_TREE || error_operand_p (parm))
7140 return 0;
7141
7142 if (TREE_CODE (arg) != TREE_CODE (parm))
7143 return 0;
7144
7145 switch (TREE_CODE (parm))
7146 {
7147 case TEMPLATE_DECL:
7148 /* We encounter instantiations of templates like
7149 template <template <template <class> class> class TT>
7150 class C; */
7151 {
7152 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7153 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7154
7155 if (!coerce_template_template_parms
7156 (parmparm, argparm, complain, in_decl, outer_args))
7157 return 0;
7158 }
7159 /* Fall through. */
7160
7161 case TYPE_DECL:
7162 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7163 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7164 /* Argument is a parameter pack but parameter is not. */
7165 return 0;
7166 break;
7167
7168 case PARM_DECL:
7169 /* The tsubst call is used to handle cases such as
7170
7171 template <int> class C {};
7172 template <class T, template <T> class TT> class D {};
7173 D<int, C> d;
7174
7175 i.e. the parameter list of TT depends on earlier parameters. */
7176 if (!uses_template_parms (TREE_TYPE (arg)))
7177 {
7178 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7179 if (!uses_template_parms (t)
7180 && !same_type_p (t, TREE_TYPE (arg)))
7181 return 0;
7182 }
7183
7184 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7185 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7186 /* Argument is a parameter pack but parameter is not. */
7187 return 0;
7188
7189 break;
7190
7191 default:
7192 gcc_unreachable ();
7193 }
7194
7195 return 1;
7196 }
7197
7198 /* Coerce template argument list ARGLIST for use with template
7199 template-parameter TEMPL. */
7200
7201 static tree
7202 coerce_template_args_for_ttp (tree templ, tree arglist,
7203 tsubst_flags_t complain)
7204 {
7205 /* Consider an example where a template template parameter declared as
7206
7207 template <class T, class U = std::allocator<T> > class TT
7208
7209 The template parameter level of T and U are one level larger than
7210 of TT. To proper process the default argument of U, say when an
7211 instantiation `TT<int>' is seen, we need to build the full
7212 arguments containing {int} as the innermost level. Outer levels,
7213 available when not appearing as default template argument, can be
7214 obtained from the arguments of the enclosing template.
7215
7216 Suppose that TT is later substituted with std::vector. The above
7217 instantiation is `TT<int, std::allocator<T> >' with TT at
7218 level 1, and T at level 2, while the template arguments at level 1
7219 becomes {std::vector} and the inner level 2 is {int}. */
7220
7221 tree outer = DECL_CONTEXT (templ);
7222 if (outer)
7223 {
7224 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7225 /* We want arguments for the partial specialization, not arguments for
7226 the primary template. */
7227 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7228 else
7229 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7230 }
7231 else if (current_template_parms)
7232 {
7233 /* This is an argument of the current template, so we haven't set
7234 DECL_CONTEXT yet. */
7235 tree relevant_template_parms;
7236
7237 /* Parameter levels that are greater than the level of the given
7238 template template parm are irrelevant. */
7239 relevant_template_parms = current_template_parms;
7240 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7241 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7242 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7243
7244 outer = template_parms_to_args (relevant_template_parms);
7245 }
7246
7247 if (outer)
7248 arglist = add_to_template_args (outer, arglist);
7249
7250 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7251 return coerce_template_parms (parmlist, arglist, templ,
7252 complain,
7253 /*require_all_args=*/true,
7254 /*use_default_args=*/true);
7255 }
7256
7257 /* A cache of template template parameters with match-all default
7258 arguments. */
7259 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7260 static void
7261 store_defaulted_ttp (tree v, tree t)
7262 {
7263 if (!defaulted_ttp_cache)
7264 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7265 defaulted_ttp_cache->put (v, t);
7266 }
7267 static tree
7268 lookup_defaulted_ttp (tree v)
7269 {
7270 if (defaulted_ttp_cache)
7271 if (tree *p = defaulted_ttp_cache->get (v))
7272 return *p;
7273 return NULL_TREE;
7274 }
7275
7276 /* T is a bound template template-parameter. Copy its arguments into default
7277 arguments of the template template-parameter's template parameters. */
7278
7279 static tree
7280 add_defaults_to_ttp (tree otmpl)
7281 {
7282 if (tree c = lookup_defaulted_ttp (otmpl))
7283 return c;
7284
7285 tree ntmpl = copy_node (otmpl);
7286
7287 tree ntype = copy_node (TREE_TYPE (otmpl));
7288 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7289 TYPE_MAIN_VARIANT (ntype) = ntype;
7290 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7291 TYPE_NAME (ntype) = ntmpl;
7292 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7293
7294 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7295 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7296 TEMPLATE_PARM_DECL (idx) = ntmpl;
7297 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7298
7299 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7300 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7301 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7302 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7303 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7304 {
7305 tree o = TREE_VEC_ELT (vec, i);
7306 if (!template_parameter_pack_p (TREE_VALUE (o)))
7307 {
7308 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7309 TREE_PURPOSE (n) = any_targ_node;
7310 }
7311 }
7312
7313 store_defaulted_ttp (otmpl, ntmpl);
7314 return ntmpl;
7315 }
7316
7317 /* ARG is a bound potential template template-argument, and PARGS is a list
7318 of arguments for the corresponding template template-parameter. Adjust
7319 PARGS as appropriate for application to ARG's template, and if ARG is a
7320 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7321 arguments to the template template parameter. */
7322
7323 static tree
7324 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7325 {
7326 ++processing_template_decl;
7327 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7328 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7329 {
7330 /* When comparing two template template-parameters in partial ordering,
7331 rewrite the one currently being used as an argument to have default
7332 arguments for all parameters. */
7333 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7334 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7335 if (pargs != error_mark_node)
7336 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7337 TYPE_TI_ARGS (arg));
7338 }
7339 else
7340 {
7341 tree aparms
7342 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7343 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7344 /*require_all*/true,
7345 /*use_default*/true);
7346 }
7347 --processing_template_decl;
7348 return pargs;
7349 }
7350
7351 /* Subroutine of unify for the case when PARM is a
7352 BOUND_TEMPLATE_TEMPLATE_PARM. */
7353
7354 static int
7355 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7356 bool explain_p)
7357 {
7358 tree parmvec = TYPE_TI_ARGS (parm);
7359 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7360
7361 /* The template template parm might be variadic and the argument
7362 not, so flatten both argument lists. */
7363 parmvec = expand_template_argument_pack (parmvec);
7364 argvec = expand_template_argument_pack (argvec);
7365
7366 if (flag_new_ttp)
7367 {
7368 /* In keeping with P0522R0, adjust P's template arguments
7369 to apply to A's template; then flatten it again. */
7370 tree nparmvec = parmvec;
7371 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7372 nparmvec = expand_template_argument_pack (nparmvec);
7373
7374 if (unify (tparms, targs, nparmvec, argvec,
7375 UNIFY_ALLOW_NONE, explain_p))
7376 return 1;
7377
7378 /* If the P0522 adjustment eliminated a pack expansion, deduce
7379 empty packs. */
7380 if (flag_new_ttp
7381 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7382 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7383 DEDUCE_EXACT, /*sub*/true, explain_p))
7384 return 1;
7385 }
7386 else
7387 {
7388 /* Deduce arguments T, i from TT<T> or TT<i>.
7389 We check each element of PARMVEC and ARGVEC individually
7390 rather than the whole TREE_VEC since they can have
7391 different number of elements, which is allowed under N2555. */
7392
7393 int len = TREE_VEC_LENGTH (parmvec);
7394
7395 /* Check if the parameters end in a pack, making them
7396 variadic. */
7397 int parm_variadic_p = 0;
7398 if (len > 0
7399 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7400 parm_variadic_p = 1;
7401
7402 for (int i = 0; i < len - parm_variadic_p; ++i)
7403 /* If the template argument list of P contains a pack
7404 expansion that is not the last template argument, the
7405 entire template argument list is a non-deduced
7406 context. */
7407 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7408 return unify_success (explain_p);
7409
7410 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7411 return unify_too_few_arguments (explain_p,
7412 TREE_VEC_LENGTH (argvec), len);
7413
7414 for (int i = 0; i < len - parm_variadic_p; ++i)
7415 if (unify (tparms, targs,
7416 TREE_VEC_ELT (parmvec, i),
7417 TREE_VEC_ELT (argvec, i),
7418 UNIFY_ALLOW_NONE, explain_p))
7419 return 1;
7420
7421 if (parm_variadic_p
7422 && unify_pack_expansion (tparms, targs,
7423 parmvec, argvec,
7424 DEDUCE_EXACT,
7425 /*subr=*/true, explain_p))
7426 return 1;
7427 }
7428
7429 return 0;
7430 }
7431
7432 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7433 template template parameters. Both PARM_PARMS and ARG_PARMS are
7434 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7435 or PARM_DECL.
7436
7437 Consider the example:
7438 template <class T> class A;
7439 template<template <class U> class TT> class B;
7440
7441 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7442 the parameters to A, and OUTER_ARGS contains A. */
7443
7444 static int
7445 coerce_template_template_parms (tree parm_parms,
7446 tree arg_parms,
7447 tsubst_flags_t complain,
7448 tree in_decl,
7449 tree outer_args)
7450 {
7451 int nparms, nargs, i;
7452 tree parm, arg;
7453 int variadic_p = 0;
7454
7455 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7456 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7457
7458 nparms = TREE_VEC_LENGTH (parm_parms);
7459 nargs = TREE_VEC_LENGTH (arg_parms);
7460
7461 if (flag_new_ttp)
7462 {
7463 /* P0522R0: A template template-parameter P is at least as specialized as
7464 a template template-argument A if, given the following rewrite to two
7465 function templates, the function template corresponding to P is at
7466 least as specialized as the function template corresponding to A
7467 according to the partial ordering rules for function templates
7468 ([temp.func.order]). Given an invented class template X with the
7469 template parameter list of A (including default arguments):
7470
7471 * Each of the two function templates has the same template parameters,
7472 respectively, as P or A.
7473
7474 * Each function template has a single function parameter whose type is
7475 a specialization of X with template arguments corresponding to the
7476 template parameters from the respective function template where, for
7477 each template parameter PP in the template parameter list of the
7478 function template, a corresponding template argument AA is formed. If
7479 PP declares a parameter pack, then AA is the pack expansion
7480 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7481
7482 If the rewrite produces an invalid type, then P is not at least as
7483 specialized as A. */
7484
7485 /* So coerce P's args to apply to A's parms, and then deduce between A's
7486 args and the converted args. If that succeeds, A is at least as
7487 specialized as P, so they match.*/
7488 tree pargs = template_parms_level_to_args (parm_parms);
7489 ++processing_template_decl;
7490 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7491 /*require_all*/true, /*use_default*/true);
7492 --processing_template_decl;
7493 if (pargs != error_mark_node)
7494 {
7495 tree targs = make_tree_vec (nargs);
7496 tree aargs = template_parms_level_to_args (arg_parms);
7497 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7498 /*explain*/false))
7499 return 1;
7500 }
7501 }
7502
7503 /* Determine whether we have a parameter pack at the end of the
7504 template template parameter's template parameter list. */
7505 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7506 {
7507 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7508
7509 if (error_operand_p (parm))
7510 return 0;
7511
7512 switch (TREE_CODE (parm))
7513 {
7514 case TEMPLATE_DECL:
7515 case TYPE_DECL:
7516 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7517 variadic_p = 1;
7518 break;
7519
7520 case PARM_DECL:
7521 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7522 variadic_p = 1;
7523 break;
7524
7525 default:
7526 gcc_unreachable ();
7527 }
7528 }
7529
7530 if (nargs != nparms
7531 && !(variadic_p && nargs >= nparms - 1))
7532 return 0;
7533
7534 /* Check all of the template parameters except the parameter pack at
7535 the end (if any). */
7536 for (i = 0; i < nparms - variadic_p; ++i)
7537 {
7538 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7539 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7540 continue;
7541
7542 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7543 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7544
7545 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7546 outer_args))
7547 return 0;
7548
7549 }
7550
7551 if (variadic_p)
7552 {
7553 /* Check each of the template parameters in the template
7554 argument against the template parameter pack at the end of
7555 the template template parameter. */
7556 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7557 return 0;
7558
7559 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7560
7561 for (; i < nargs; ++i)
7562 {
7563 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7564 continue;
7565
7566 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7567
7568 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7569 outer_args))
7570 return 0;
7571 }
7572 }
7573
7574 return 1;
7575 }
7576
7577 /* Verifies that the deduced template arguments (in TARGS) for the
7578 template template parameters (in TPARMS) represent valid bindings,
7579 by comparing the template parameter list of each template argument
7580 to the template parameter list of its corresponding template
7581 template parameter, in accordance with DR150. This
7582 routine can only be called after all template arguments have been
7583 deduced. It will return TRUE if all of the template template
7584 parameter bindings are okay, FALSE otherwise. */
7585 bool
7586 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7587 {
7588 int i, ntparms = TREE_VEC_LENGTH (tparms);
7589 bool ret = true;
7590
7591 /* We're dealing with template parms in this process. */
7592 ++processing_template_decl;
7593
7594 targs = INNERMOST_TEMPLATE_ARGS (targs);
7595
7596 for (i = 0; i < ntparms; ++i)
7597 {
7598 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7599 tree targ = TREE_VEC_ELT (targs, i);
7600
7601 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7602 {
7603 tree packed_args = NULL_TREE;
7604 int idx, len = 1;
7605
7606 if (ARGUMENT_PACK_P (targ))
7607 {
7608 /* Look inside the argument pack. */
7609 packed_args = ARGUMENT_PACK_ARGS (targ);
7610 len = TREE_VEC_LENGTH (packed_args);
7611 }
7612
7613 for (idx = 0; idx < len; ++idx)
7614 {
7615 tree targ_parms = NULL_TREE;
7616
7617 if (packed_args)
7618 /* Extract the next argument from the argument
7619 pack. */
7620 targ = TREE_VEC_ELT (packed_args, idx);
7621
7622 if (PACK_EXPANSION_P (targ))
7623 /* Look at the pattern of the pack expansion. */
7624 targ = PACK_EXPANSION_PATTERN (targ);
7625
7626 /* Extract the template parameters from the template
7627 argument. */
7628 if (TREE_CODE (targ) == TEMPLATE_DECL)
7629 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7630 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7631 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7632
7633 /* Verify that we can coerce the template template
7634 parameters from the template argument to the template
7635 parameter. This requires an exact match. */
7636 if (targ_parms
7637 && !coerce_template_template_parms
7638 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7639 targ_parms,
7640 tf_none,
7641 tparm,
7642 targs))
7643 {
7644 ret = false;
7645 goto out;
7646 }
7647 }
7648 }
7649 }
7650
7651 out:
7652
7653 --processing_template_decl;
7654 return ret;
7655 }
7656
7657 /* Since type attributes aren't mangled, we need to strip them from
7658 template type arguments. */
7659
7660 static tree
7661 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7662 {
7663 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7664 return arg;
7665 bool removed_attributes = false;
7666 tree canon = strip_typedefs (arg, &removed_attributes);
7667 if (removed_attributes
7668 && (complain & tf_warning))
7669 warning (OPT_Wignored_attributes,
7670 "ignoring attributes on template argument %qT", arg);
7671 return canon;
7672 }
7673
7674 /* And from inside dependent non-type arguments like sizeof(Type). */
7675
7676 static tree
7677 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7678 {
7679 if (!arg || arg == error_mark_node)
7680 return arg;
7681 bool removed_attributes = false;
7682 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7683 if (removed_attributes
7684 && (complain & tf_warning))
7685 warning (OPT_Wignored_attributes,
7686 "ignoring attributes in template argument %qE", arg);
7687 return canon;
7688 }
7689
7690 // A template declaration can be substituted for a constrained
7691 // template template parameter only when the argument is more
7692 // constrained than the parameter.
7693 static bool
7694 is_compatible_template_arg (tree parm, tree arg)
7695 {
7696 tree parm_cons = get_constraints (parm);
7697
7698 /* For now, allow constrained template template arguments
7699 and unconstrained template template parameters. */
7700 if (parm_cons == NULL_TREE)
7701 return true;
7702
7703 tree arg_cons = get_constraints (arg);
7704
7705 // If the template parameter is constrained, we need to rewrite its
7706 // constraints in terms of the ARG's template parameters. This ensures
7707 // that all of the template parameter types will have the same depth.
7708 //
7709 // Note that this is only valid when coerce_template_template_parm is
7710 // true for the innermost template parameters of PARM and ARG. In other
7711 // words, because coercion is successful, this conversion will be valid.
7712 if (parm_cons)
7713 {
7714 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7715 parm_cons = tsubst_constraint_info (parm_cons,
7716 INNERMOST_TEMPLATE_ARGS (args),
7717 tf_none, NULL_TREE);
7718 if (parm_cons == error_mark_node)
7719 return false;
7720 }
7721
7722 return subsumes (parm_cons, arg_cons);
7723 }
7724
7725 // Convert a placeholder argument into a binding to the original
7726 // parameter. The original parameter is saved as the TREE_TYPE of
7727 // ARG.
7728 static inline tree
7729 convert_wildcard_argument (tree parm, tree arg)
7730 {
7731 TREE_TYPE (arg) = parm;
7732 return arg;
7733 }
7734
7735 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7736 because one of them is dependent. But we need to represent the
7737 conversion for the benefit of cp_tree_equal. */
7738
7739 static tree
7740 maybe_convert_nontype_argument (tree type, tree arg)
7741 {
7742 /* Auto parms get no conversion. */
7743 if (type_uses_auto (type))
7744 return arg;
7745 /* We don't need or want to add this conversion now if we're going to use the
7746 argument for deduction. */
7747 if (value_dependent_expression_p (arg))
7748 return arg;
7749
7750 type = cv_unqualified (type);
7751 tree argtype = TREE_TYPE (arg);
7752 if (same_type_p (type, argtype))
7753 return arg;
7754
7755 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7756 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7757 return arg;
7758 }
7759
7760 /* Convert the indicated template ARG as necessary to match the
7761 indicated template PARM. Returns the converted ARG, or
7762 error_mark_node if the conversion was unsuccessful. Error and
7763 warning messages are issued under control of COMPLAIN. This
7764 conversion is for the Ith parameter in the parameter list. ARGS is
7765 the full set of template arguments deduced so far. */
7766
7767 static tree
7768 convert_template_argument (tree parm,
7769 tree arg,
7770 tree args,
7771 tsubst_flags_t complain,
7772 int i,
7773 tree in_decl)
7774 {
7775 tree orig_arg;
7776 tree val;
7777 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7778
7779 if (parm == error_mark_node || error_operand_p (arg))
7780 return error_mark_node;
7781
7782 /* Trivially convert placeholders. */
7783 if (TREE_CODE (arg) == WILDCARD_DECL)
7784 return convert_wildcard_argument (parm, arg);
7785
7786 if (arg == any_targ_node)
7787 return arg;
7788
7789 if (TREE_CODE (arg) == TREE_LIST
7790 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7791 {
7792 /* The template argument was the name of some
7793 member function. That's usually
7794 invalid, but static members are OK. In any
7795 case, grab the underlying fields/functions
7796 and issue an error later if required. */
7797 orig_arg = TREE_VALUE (arg);
7798 TREE_TYPE (arg) = unknown_type_node;
7799 }
7800
7801 orig_arg = arg;
7802
7803 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7804 requires_type = (TREE_CODE (parm) == TYPE_DECL
7805 || requires_tmpl_type);
7806
7807 /* When determining whether an argument pack expansion is a template,
7808 look at the pattern. */
7809 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7810 arg = PACK_EXPANSION_PATTERN (arg);
7811
7812 /* Deal with an injected-class-name used as a template template arg. */
7813 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7814 {
7815 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7816 if (TREE_CODE (t) == TEMPLATE_DECL)
7817 {
7818 if (cxx_dialect >= cxx11)
7819 /* OK under DR 1004. */;
7820 else if (complain & tf_warning_or_error)
7821 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7822 " used as template template argument", TYPE_NAME (arg));
7823 else if (flag_pedantic_errors)
7824 t = arg;
7825
7826 arg = t;
7827 }
7828 }
7829
7830 is_tmpl_type =
7831 ((TREE_CODE (arg) == TEMPLATE_DECL
7832 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7833 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7834 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7835 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7836
7837 if (is_tmpl_type
7838 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7839 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7840 arg = TYPE_STUB_DECL (arg);
7841
7842 is_type = TYPE_P (arg) || is_tmpl_type;
7843
7844 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7845 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7846 {
7847 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7848 {
7849 if (complain & tf_error)
7850 error ("invalid use of destructor %qE as a type", orig_arg);
7851 return error_mark_node;
7852 }
7853
7854 permerror (input_location,
7855 "to refer to a type member of a template parameter, "
7856 "use %<typename %E%>", orig_arg);
7857
7858 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7859 TREE_OPERAND (arg, 1),
7860 typename_type,
7861 complain);
7862 arg = orig_arg;
7863 is_type = 1;
7864 }
7865 if (is_type != requires_type)
7866 {
7867 if (in_decl)
7868 {
7869 if (complain & tf_error)
7870 {
7871 error ("type/value mismatch at argument %d in template "
7872 "parameter list for %qD",
7873 i + 1, in_decl);
7874 if (is_type)
7875 inform (input_location,
7876 " expected a constant of type %qT, got %qT",
7877 TREE_TYPE (parm),
7878 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7879 else if (requires_tmpl_type)
7880 inform (input_location,
7881 " expected a class template, got %qE", orig_arg);
7882 else
7883 inform (input_location,
7884 " expected a type, got %qE", orig_arg);
7885 }
7886 }
7887 return error_mark_node;
7888 }
7889 if (is_tmpl_type ^ requires_tmpl_type)
7890 {
7891 if (in_decl && (complain & tf_error))
7892 {
7893 error ("type/value mismatch at argument %d in template "
7894 "parameter list for %qD",
7895 i + 1, in_decl);
7896 if (is_tmpl_type)
7897 inform (input_location,
7898 " expected a type, got %qT", DECL_NAME (arg));
7899 else
7900 inform (input_location,
7901 " expected a class template, got %qT", orig_arg);
7902 }
7903 return error_mark_node;
7904 }
7905
7906 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7907 /* We already did the appropriate conversion when packing args. */
7908 val = orig_arg;
7909 else if (is_type)
7910 {
7911 if (requires_tmpl_type)
7912 {
7913 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7914 /* The number of argument required is not known yet.
7915 Just accept it for now. */
7916 val = orig_arg;
7917 else
7918 {
7919 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7920 tree argparm;
7921
7922 /* Strip alias templates that are equivalent to another
7923 template. */
7924 arg = get_underlying_template (arg);
7925 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7926
7927 if (coerce_template_template_parms (parmparm, argparm,
7928 complain, in_decl,
7929 args))
7930 {
7931 val = arg;
7932
7933 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7934 TEMPLATE_DECL. */
7935 if (val != error_mark_node)
7936 {
7937 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7938 val = TREE_TYPE (val);
7939 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7940 val = make_pack_expansion (val, complain);
7941 }
7942 }
7943 else
7944 {
7945 if (in_decl && (complain & tf_error))
7946 {
7947 error ("type/value mismatch at argument %d in "
7948 "template parameter list for %qD",
7949 i + 1, in_decl);
7950 inform (input_location,
7951 " expected a template of type %qD, got %qT",
7952 parm, orig_arg);
7953 }
7954
7955 val = error_mark_node;
7956 }
7957
7958 // Check that the constraints are compatible before allowing the
7959 // substitution.
7960 if (val != error_mark_node)
7961 if (!is_compatible_template_arg (parm, arg))
7962 {
7963 if (in_decl && (complain & tf_error))
7964 {
7965 error ("constraint mismatch at argument %d in "
7966 "template parameter list for %qD",
7967 i + 1, in_decl);
7968 inform (input_location, " expected %qD but got %qD",
7969 parm, arg);
7970 }
7971 val = error_mark_node;
7972 }
7973 }
7974 }
7975 else
7976 val = orig_arg;
7977 /* We only form one instance of each template specialization.
7978 Therefore, if we use a non-canonical variant (i.e., a
7979 typedef), any future messages referring to the type will use
7980 the typedef, which is confusing if those future uses do not
7981 themselves also use the typedef. */
7982 if (TYPE_P (val))
7983 val = canonicalize_type_argument (val, complain);
7984 }
7985 else
7986 {
7987 tree t = TREE_TYPE (parm);
7988
7989 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
7990 > TMPL_ARGS_DEPTH (args))
7991 /* We don't have enough levels of args to do any substitution. This
7992 can happen in the context of -fnew-ttp-matching. */;
7993 else if (tree a = type_uses_auto (t))
7994 {
7995 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7996 if (t == error_mark_node)
7997 return error_mark_node;
7998 }
7999 else
8000 t = tsubst (t, args, complain, in_decl);
8001
8002 if (invalid_nontype_parm_type_p (t, complain))
8003 return error_mark_node;
8004
8005 if (!type_dependent_expression_p (orig_arg)
8006 && !uses_template_parms (t))
8007 /* We used to call digest_init here. However, digest_init
8008 will report errors, which we don't want when complain
8009 is zero. More importantly, digest_init will try too
8010 hard to convert things: for example, `0' should not be
8011 converted to pointer type at this point according to
8012 the standard. Accepting this is not merely an
8013 extension, since deciding whether or not these
8014 conversions can occur is part of determining which
8015 function template to call, or whether a given explicit
8016 argument specification is valid. */
8017 val = convert_nontype_argument (t, orig_arg, complain);
8018 else
8019 {
8020 val = canonicalize_expr_argument (orig_arg, complain);
8021 val = maybe_convert_nontype_argument (t, val);
8022 }
8023
8024
8025 if (val == NULL_TREE)
8026 val = error_mark_node;
8027 else if (val == error_mark_node && (complain & tf_error))
8028 error ("could not convert template argument %qE from %qT to %qT",
8029 orig_arg, TREE_TYPE (orig_arg), t);
8030
8031 if (INDIRECT_REF_P (val))
8032 {
8033 /* Reject template arguments that are references to built-in
8034 functions with no library fallbacks. */
8035 const_tree inner = TREE_OPERAND (val, 0);
8036 const_tree innertype = TREE_TYPE (inner);
8037 if (innertype
8038 && TYPE_REF_P (innertype)
8039 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8040 && TREE_OPERAND_LENGTH (inner) > 0
8041 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8042 return error_mark_node;
8043 }
8044
8045 if (TREE_CODE (val) == SCOPE_REF)
8046 {
8047 /* Strip typedefs from the SCOPE_REF. */
8048 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8049 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8050 complain);
8051 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8052 QUALIFIED_NAME_IS_TEMPLATE (val));
8053 }
8054 }
8055
8056 return val;
8057 }
8058
8059 /* Coerces the remaining template arguments in INNER_ARGS (from
8060 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8061 Returns the coerced argument pack. PARM_IDX is the position of this
8062 parameter in the template parameter list. ARGS is the original
8063 template argument list. */
8064 static tree
8065 coerce_template_parameter_pack (tree parms,
8066 int parm_idx,
8067 tree args,
8068 tree inner_args,
8069 int arg_idx,
8070 tree new_args,
8071 int* lost,
8072 tree in_decl,
8073 tsubst_flags_t complain)
8074 {
8075 tree parm = TREE_VEC_ELT (parms, parm_idx);
8076 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8077 tree packed_args;
8078 tree argument_pack;
8079 tree packed_parms = NULL_TREE;
8080
8081 if (arg_idx > nargs)
8082 arg_idx = nargs;
8083
8084 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8085 {
8086 /* When the template parameter is a non-type template parameter pack
8087 or template template parameter pack whose type or template
8088 parameters use parameter packs, we know exactly how many arguments
8089 we are looking for. Build a vector of the instantiated decls for
8090 these template parameters in PACKED_PARMS. */
8091 /* We can't use make_pack_expansion here because it would interpret a
8092 _DECL as a use rather than a declaration. */
8093 tree decl = TREE_VALUE (parm);
8094 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8095 SET_PACK_EXPANSION_PATTERN (exp, decl);
8096 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8097 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8098
8099 TREE_VEC_LENGTH (args)--;
8100 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8101 TREE_VEC_LENGTH (args)++;
8102
8103 if (packed_parms == error_mark_node)
8104 return error_mark_node;
8105
8106 /* If we're doing a partial instantiation of a member template,
8107 verify that all of the types used for the non-type
8108 template parameter pack are, in fact, valid for non-type
8109 template parameters. */
8110 if (arg_idx < nargs
8111 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8112 {
8113 int j, len = TREE_VEC_LENGTH (packed_parms);
8114 for (j = 0; j < len; ++j)
8115 {
8116 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8117 if (invalid_nontype_parm_type_p (t, complain))
8118 return error_mark_node;
8119 }
8120 /* We don't know how many args we have yet, just
8121 use the unconverted ones for now. */
8122 return NULL_TREE;
8123 }
8124
8125 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8126 }
8127 /* Check if we have a placeholder pack, which indicates we're
8128 in the context of a introduction list. In that case we want
8129 to match this pack to the single placeholder. */
8130 else if (arg_idx < nargs
8131 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8132 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8133 {
8134 nargs = arg_idx + 1;
8135 packed_args = make_tree_vec (1);
8136 }
8137 else
8138 packed_args = make_tree_vec (nargs - arg_idx);
8139
8140 /* Convert the remaining arguments, which will be a part of the
8141 parameter pack "parm". */
8142 int first_pack_arg = arg_idx;
8143 for (; arg_idx < nargs; ++arg_idx)
8144 {
8145 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8146 tree actual_parm = TREE_VALUE (parm);
8147 int pack_idx = arg_idx - first_pack_arg;
8148
8149 if (packed_parms)
8150 {
8151 /* Once we've packed as many args as we have types, stop. */
8152 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8153 break;
8154 else if (PACK_EXPANSION_P (arg))
8155 /* We don't know how many args we have yet, just
8156 use the unconverted ones for now. */
8157 return NULL_TREE;
8158 else
8159 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8160 }
8161
8162 if (arg == error_mark_node)
8163 {
8164 if (complain & tf_error)
8165 error ("template argument %d is invalid", arg_idx + 1);
8166 }
8167 else
8168 arg = convert_template_argument (actual_parm,
8169 arg, new_args, complain, parm_idx,
8170 in_decl);
8171 if (arg == error_mark_node)
8172 (*lost)++;
8173 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8174 }
8175
8176 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8177 && TREE_VEC_LENGTH (packed_args) > 0)
8178 {
8179 if (complain & tf_error)
8180 error ("wrong number of template arguments (%d, should be %d)",
8181 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8182 return error_mark_node;
8183 }
8184
8185 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8186 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8187 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8188 else
8189 {
8190 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8191 TREE_CONSTANT (argument_pack) = 1;
8192 }
8193
8194 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8195 if (CHECKING_P)
8196 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8197 TREE_VEC_LENGTH (packed_args));
8198 return argument_pack;
8199 }
8200
8201 /* Returns the number of pack expansions in the template argument vector
8202 ARGS. */
8203
8204 static int
8205 pack_expansion_args_count (tree args)
8206 {
8207 int i;
8208 int count = 0;
8209 if (args)
8210 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8211 {
8212 tree elt = TREE_VEC_ELT (args, i);
8213 if (elt && PACK_EXPANSION_P (elt))
8214 ++count;
8215 }
8216 return count;
8217 }
8218
8219 /* Convert all template arguments to their appropriate types, and
8220 return a vector containing the innermost resulting template
8221 arguments. If any error occurs, return error_mark_node. Error and
8222 warning messages are issued under control of COMPLAIN.
8223
8224 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8225 for arguments not specified in ARGS. Otherwise, if
8226 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8227 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8228 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8229 ARGS. */
8230
8231 static tree
8232 coerce_template_parms (tree parms,
8233 tree args,
8234 tree in_decl,
8235 tsubst_flags_t complain,
8236 bool require_all_args,
8237 bool use_default_args)
8238 {
8239 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8240 tree orig_inner_args;
8241 tree inner_args;
8242 tree new_args;
8243 tree new_inner_args;
8244 int saved_unevaluated_operand;
8245 int saved_inhibit_evaluation_warnings;
8246
8247 /* When used as a boolean value, indicates whether this is a
8248 variadic template parameter list. Since it's an int, we can also
8249 subtract it from nparms to get the number of non-variadic
8250 parameters. */
8251 int variadic_p = 0;
8252 int variadic_args_p = 0;
8253 int post_variadic_parms = 0;
8254
8255 /* Adjustment to nparms for fixed parameter packs. */
8256 int fixed_pack_adjust = 0;
8257 int fixed_packs = 0;
8258 int missing = 0;
8259
8260 /* Likewise for parameters with default arguments. */
8261 int default_p = 0;
8262
8263 if (args == error_mark_node)
8264 return error_mark_node;
8265
8266 nparms = TREE_VEC_LENGTH (parms);
8267
8268 /* Determine if there are any parameter packs or default arguments. */
8269 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8270 {
8271 tree parm = TREE_VEC_ELT (parms, parm_idx);
8272 if (variadic_p)
8273 ++post_variadic_parms;
8274 if (template_parameter_pack_p (TREE_VALUE (parm)))
8275 ++variadic_p;
8276 if (TREE_PURPOSE (parm))
8277 ++default_p;
8278 }
8279
8280 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8281 /* If there are no parameters that follow a parameter pack, we need to
8282 expand any argument packs so that we can deduce a parameter pack from
8283 some non-packed args followed by an argument pack, as in variadic85.C.
8284 If there are such parameters, we need to leave argument packs intact
8285 so the arguments are assigned properly. This can happen when dealing
8286 with a nested class inside a partial specialization of a class
8287 template, as in variadic92.C, or when deducing a template parameter pack
8288 from a sub-declarator, as in variadic114.C. */
8289 if (!post_variadic_parms)
8290 inner_args = expand_template_argument_pack (inner_args);
8291
8292 /* Count any pack expansion args. */
8293 variadic_args_p = pack_expansion_args_count (inner_args);
8294
8295 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8296 if ((nargs - variadic_args_p > nparms && !variadic_p)
8297 || (nargs < nparms - variadic_p
8298 && require_all_args
8299 && !variadic_args_p
8300 && (!use_default_args
8301 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8302 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8303 {
8304 bad_nargs:
8305 if (complain & tf_error)
8306 {
8307 if (variadic_p || default_p)
8308 {
8309 nparms -= variadic_p + default_p;
8310 error ("wrong number of template arguments "
8311 "(%d, should be at least %d)", nargs, nparms);
8312 }
8313 else
8314 error ("wrong number of template arguments "
8315 "(%d, should be %d)", nargs, nparms);
8316
8317 if (in_decl)
8318 inform (DECL_SOURCE_LOCATION (in_decl),
8319 "provided for %qD", in_decl);
8320 }
8321
8322 return error_mark_node;
8323 }
8324 /* We can't pass a pack expansion to a non-pack parameter of an alias
8325 template (DR 1430). */
8326 else if (in_decl
8327 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8328 || concept_template_p (in_decl))
8329 && variadic_args_p
8330 && nargs - variadic_args_p < nparms - variadic_p)
8331 {
8332 if (complain & tf_error)
8333 {
8334 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8335 {
8336 tree arg = TREE_VEC_ELT (inner_args, i);
8337 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8338
8339 if (PACK_EXPANSION_P (arg)
8340 && !template_parameter_pack_p (parm))
8341 {
8342 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8343 error_at (location_of (arg),
8344 "pack expansion argument for non-pack parameter "
8345 "%qD of alias template %qD", parm, in_decl);
8346 else
8347 error_at (location_of (arg),
8348 "pack expansion argument for non-pack parameter "
8349 "%qD of concept %qD", parm, in_decl);
8350 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8351 goto found;
8352 }
8353 }
8354 gcc_unreachable ();
8355 found:;
8356 }
8357 return error_mark_node;
8358 }
8359
8360 /* We need to evaluate the template arguments, even though this
8361 template-id may be nested within a "sizeof". */
8362 saved_unevaluated_operand = cp_unevaluated_operand;
8363 cp_unevaluated_operand = 0;
8364 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8365 c_inhibit_evaluation_warnings = 0;
8366 new_inner_args = make_tree_vec (nparms);
8367 new_args = add_outermost_template_args (args, new_inner_args);
8368 int pack_adjust = 0;
8369 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8370 {
8371 tree arg;
8372 tree parm;
8373
8374 /* Get the Ith template parameter. */
8375 parm = TREE_VEC_ELT (parms, parm_idx);
8376
8377 if (parm == error_mark_node)
8378 {
8379 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8380 continue;
8381 }
8382
8383 /* Calculate the next argument. */
8384 if (arg_idx < nargs)
8385 arg = TREE_VEC_ELT (inner_args, arg_idx);
8386 else
8387 arg = NULL_TREE;
8388
8389 if (template_parameter_pack_p (TREE_VALUE (parm))
8390 && !(arg && ARGUMENT_PACK_P (arg)))
8391 {
8392 /* Some arguments will be placed in the
8393 template parameter pack PARM. */
8394 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8395 inner_args, arg_idx,
8396 new_args, &lost,
8397 in_decl, complain);
8398
8399 if (arg == NULL_TREE)
8400 {
8401 /* We don't know how many args we have yet, just use the
8402 unconverted (and still packed) ones for now. */
8403 new_inner_args = orig_inner_args;
8404 arg_idx = nargs;
8405 break;
8406 }
8407
8408 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8409
8410 /* Store this argument. */
8411 if (arg == error_mark_node)
8412 {
8413 lost++;
8414 /* We are done with all of the arguments. */
8415 arg_idx = nargs;
8416 break;
8417 }
8418 else
8419 {
8420 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8421 arg_idx += pack_adjust;
8422 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8423 {
8424 ++fixed_packs;
8425 fixed_pack_adjust += pack_adjust;
8426 }
8427 }
8428
8429 continue;
8430 }
8431 else if (arg)
8432 {
8433 if (PACK_EXPANSION_P (arg))
8434 {
8435 /* "If every valid specialization of a variadic template
8436 requires an empty template parameter pack, the template is
8437 ill-formed, no diagnostic required." So check that the
8438 pattern works with this parameter. */
8439 tree pattern = PACK_EXPANSION_PATTERN (arg);
8440 tree conv = convert_template_argument (TREE_VALUE (parm),
8441 pattern, new_args,
8442 complain, parm_idx,
8443 in_decl);
8444 if (conv == error_mark_node)
8445 {
8446 if (complain & tf_error)
8447 inform (input_location, "so any instantiation with a "
8448 "non-empty parameter pack would be ill-formed");
8449 ++lost;
8450 }
8451 else if (TYPE_P (conv) && !TYPE_P (pattern))
8452 /* Recover from missing typename. */
8453 TREE_VEC_ELT (inner_args, arg_idx)
8454 = make_pack_expansion (conv, complain);
8455
8456 /* We don't know how many args we have yet, just
8457 use the unconverted ones for now. */
8458 new_inner_args = inner_args;
8459 arg_idx = nargs;
8460 break;
8461 }
8462 }
8463 else if (require_all_args)
8464 {
8465 /* There must be a default arg in this case. */
8466 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8467 complain, in_decl);
8468 /* The position of the first default template argument,
8469 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8470 Record that. */
8471 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8472 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8473 arg_idx - pack_adjust);
8474 }
8475 else
8476 break;
8477
8478 if (arg == error_mark_node)
8479 {
8480 if (complain & tf_error)
8481 error ("template argument %d is invalid", arg_idx + 1);
8482 }
8483 else if (!arg)
8484 {
8485 /* This can occur if there was an error in the template
8486 parameter list itself (which we would already have
8487 reported) that we are trying to recover from, e.g., a class
8488 template with a parameter list such as
8489 template<typename..., typename> (cpp0x/variadic150.C). */
8490 ++lost;
8491
8492 /* This can also happen with a fixed parameter pack (71834). */
8493 if (arg_idx >= nargs)
8494 ++missing;
8495 }
8496 else
8497 arg = convert_template_argument (TREE_VALUE (parm),
8498 arg, new_args, complain,
8499 parm_idx, in_decl);
8500
8501 if (arg == error_mark_node)
8502 lost++;
8503 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8504 }
8505 cp_unevaluated_operand = saved_unevaluated_operand;
8506 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8507
8508 if (missing || arg_idx < nargs - variadic_args_p)
8509 {
8510 /* If we had fixed parameter packs, we didn't know how many arguments we
8511 actually needed earlier; now we do. */
8512 nparms += fixed_pack_adjust;
8513 variadic_p -= fixed_packs;
8514 goto bad_nargs;
8515 }
8516
8517 if (arg_idx < nargs)
8518 {
8519 /* We had some pack expansion arguments that will only work if the packs
8520 are empty, but wait until instantiation time to complain.
8521 See variadic-ttp3.C. */
8522 int len = nparms + (nargs - arg_idx);
8523 tree args = make_tree_vec (len);
8524 int i = 0;
8525 for (; i < nparms; ++i)
8526 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8527 for (; i < len; ++i, ++arg_idx)
8528 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8529 arg_idx - pack_adjust);
8530 new_inner_args = args;
8531 }
8532
8533 if (lost)
8534 {
8535 gcc_assert (!(complain & tf_error) || seen_error ());
8536 return error_mark_node;
8537 }
8538
8539 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8540 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8541 TREE_VEC_LENGTH (new_inner_args));
8542
8543 return new_inner_args;
8544 }
8545
8546 /* Convert all template arguments to their appropriate types, and
8547 return a vector containing the innermost resulting template
8548 arguments. If any error occurs, return error_mark_node. Error and
8549 warning messages are not issued.
8550
8551 Note that no function argument deduction is performed, and default
8552 arguments are used to fill in unspecified arguments. */
8553 tree
8554 coerce_template_parms (tree parms, tree args, tree in_decl)
8555 {
8556 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8557 }
8558
8559 /* Convert all template arguments to their appropriate type, and
8560 instantiate default arguments as needed. This returns a vector
8561 containing the innermost resulting template arguments, or
8562 error_mark_node if unsuccessful. */
8563 tree
8564 coerce_template_parms (tree parms, tree args, tree in_decl,
8565 tsubst_flags_t complain)
8566 {
8567 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8568 }
8569
8570 /* Like coerce_template_parms. If PARMS represents all template
8571 parameters levels, this function returns a vector of vectors
8572 representing all the resulting argument levels. Note that in this
8573 case, only the innermost arguments are coerced because the
8574 outermost ones are supposed to have been coerced already.
8575
8576 Otherwise, if PARMS represents only (the innermost) vector of
8577 parameters, this function returns a vector containing just the
8578 innermost resulting arguments. */
8579
8580 static tree
8581 coerce_innermost_template_parms (tree parms,
8582 tree args,
8583 tree in_decl,
8584 tsubst_flags_t complain,
8585 bool require_all_args,
8586 bool use_default_args)
8587 {
8588 int parms_depth = TMPL_PARMS_DEPTH (parms);
8589 int args_depth = TMPL_ARGS_DEPTH (args);
8590 tree coerced_args;
8591
8592 if (parms_depth > 1)
8593 {
8594 coerced_args = make_tree_vec (parms_depth);
8595 tree level;
8596 int cur_depth;
8597
8598 for (level = parms, cur_depth = parms_depth;
8599 parms_depth > 0 && level != NULL_TREE;
8600 level = TREE_CHAIN (level), --cur_depth)
8601 {
8602 tree l;
8603 if (cur_depth == args_depth)
8604 l = coerce_template_parms (TREE_VALUE (level),
8605 args, in_decl, complain,
8606 require_all_args,
8607 use_default_args);
8608 else
8609 l = TMPL_ARGS_LEVEL (args, cur_depth);
8610
8611 if (l == error_mark_node)
8612 return error_mark_node;
8613
8614 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8615 }
8616 }
8617 else
8618 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8619 args, in_decl, complain,
8620 require_all_args,
8621 use_default_args);
8622 return coerced_args;
8623 }
8624
8625 /* Returns 1 if template args OT and NT are equivalent. */
8626
8627 int
8628 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8629 {
8630 if (nt == ot)
8631 return 1;
8632 if (nt == NULL_TREE || ot == NULL_TREE)
8633 return false;
8634 if (nt == any_targ_node || ot == any_targ_node)
8635 return true;
8636
8637 if (TREE_CODE (nt) == TREE_VEC)
8638 /* For member templates */
8639 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8640 else if (PACK_EXPANSION_P (ot))
8641 return (PACK_EXPANSION_P (nt)
8642 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8643 PACK_EXPANSION_PATTERN (nt))
8644 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8645 PACK_EXPANSION_EXTRA_ARGS (nt)));
8646 else if (ARGUMENT_PACK_P (ot))
8647 {
8648 int i, len;
8649 tree opack, npack;
8650
8651 if (!ARGUMENT_PACK_P (nt))
8652 return 0;
8653
8654 opack = ARGUMENT_PACK_ARGS (ot);
8655 npack = ARGUMENT_PACK_ARGS (nt);
8656 len = TREE_VEC_LENGTH (opack);
8657 if (TREE_VEC_LENGTH (npack) != len)
8658 return 0;
8659 for (i = 0; i < len; ++i)
8660 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8661 TREE_VEC_ELT (npack, i)))
8662 return 0;
8663 return 1;
8664 }
8665 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8666 gcc_unreachable ();
8667 else if (TYPE_P (nt))
8668 {
8669 if (!TYPE_P (ot))
8670 return false;
8671 /* Don't treat an alias template specialization with dependent
8672 arguments as equivalent to its underlying type when used as a
8673 template argument; we need them to be distinct so that we
8674 substitute into the specialization arguments at instantiation
8675 time. And aliases can't be equivalent without being ==, so
8676 we don't need to look any deeper.
8677
8678 During partial ordering, however, we need to treat them normally so
8679 that we can order uses of the same alias with different
8680 cv-qualification (79960). */
8681 if (!partial_order
8682 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8683 return false;
8684 else
8685 return same_type_p (ot, nt);
8686 }
8687 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8688 return 0;
8689 else
8690 {
8691 /* Try to treat a template non-type argument that has been converted
8692 to the parameter type as equivalent to one that hasn't yet. */
8693 for (enum tree_code code1 = TREE_CODE (ot);
8694 CONVERT_EXPR_CODE_P (code1)
8695 || code1 == NON_LVALUE_EXPR;
8696 code1 = TREE_CODE (ot))
8697 ot = TREE_OPERAND (ot, 0);
8698 for (enum tree_code code2 = TREE_CODE (nt);
8699 CONVERT_EXPR_CODE_P (code2)
8700 || code2 == NON_LVALUE_EXPR;
8701 code2 = TREE_CODE (nt))
8702 nt = TREE_OPERAND (nt, 0);
8703
8704 return cp_tree_equal (ot, nt);
8705 }
8706 }
8707
8708 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8709 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8710 NEWARG_PTR with the offending arguments if they are non-NULL. */
8711
8712 int
8713 comp_template_args (tree oldargs, tree newargs,
8714 tree *oldarg_ptr, tree *newarg_ptr,
8715 bool partial_order)
8716 {
8717 int i;
8718
8719 if (oldargs == newargs)
8720 return 1;
8721
8722 if (!oldargs || !newargs)
8723 return 0;
8724
8725 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8726 return 0;
8727
8728 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8729 {
8730 tree nt = TREE_VEC_ELT (newargs, i);
8731 tree ot = TREE_VEC_ELT (oldargs, i);
8732
8733 if (! template_args_equal (ot, nt, partial_order))
8734 {
8735 if (oldarg_ptr != NULL)
8736 *oldarg_ptr = ot;
8737 if (newarg_ptr != NULL)
8738 *newarg_ptr = nt;
8739 return 0;
8740 }
8741 }
8742 return 1;
8743 }
8744
8745 inline bool
8746 comp_template_args_porder (tree oargs, tree nargs)
8747 {
8748 return comp_template_args (oargs, nargs, NULL, NULL, true);
8749 }
8750
8751 /* Implement a freelist interface for objects of type T.
8752
8753 Head is a separate object, rather than a regular member, so that we
8754 can define it as a GTY deletable pointer, which is highly
8755 desirable. A data member could be declared that way, but then the
8756 containing object would implicitly get GTY((user)), which would
8757 prevent us from instantiating freelists as global objects.
8758 Although this way we can create freelist global objects, they're
8759 such thin wrappers that instantiating temporaries at every use
8760 loses nothing and saves permanent storage for the freelist object.
8761
8762 Member functions next, anew, poison and reinit have default
8763 implementations that work for most of the types we're interested
8764 in, but if they don't work for some type, they should be explicitly
8765 specialized. See the comments before them for requirements, and
8766 the example specializations for the tree_list_freelist. */
8767 template <typename T>
8768 class freelist
8769 {
8770 /* Return the next object in a chain. We could just do type
8771 punning, but if we access the object with its underlying type, we
8772 avoid strict-aliasing trouble. This needs only work between
8773 poison and reinit. */
8774 static T *&next (T *obj) { return obj->next; }
8775
8776 /* Return a newly allocated, uninitialized or minimally-initialized
8777 object of type T. Any initialization performed by anew should
8778 either remain across the life of the object and the execution of
8779 poison, or be redone by reinit. */
8780 static T *anew () { return ggc_alloc<T> (); }
8781
8782 /* Optionally scribble all over the bits holding the object, so that
8783 they become (mostly?) uninitialized memory. This is called while
8784 preparing to make the object part of the free list. */
8785 static void poison (T *obj) {
8786 T *p ATTRIBUTE_UNUSED = obj;
8787 T **q ATTRIBUTE_UNUSED = &next (obj);
8788
8789 #ifdef ENABLE_GC_CHECKING
8790 /* Poison the data, to indicate the data is garbage. */
8791 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
8792 memset (p, 0xa5, sizeof (*p));
8793 #endif
8794 /* Let valgrind know the object is free. */
8795 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
8796
8797 /* Let valgrind know the next portion of the object is available,
8798 but uninitialized. */
8799 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8800 }
8801
8802 /* Bring an object that underwent at least one lifecycle after anew
8803 and before the most recent free and poison, back to a usable
8804 state, reinitializing whatever is needed for it to be
8805 functionally equivalent to an object just allocated and returned
8806 by anew. This may poison or clear the next field, used by
8807 freelist housekeeping after poison was called. */
8808 static void reinit (T *obj) {
8809 T **q ATTRIBUTE_UNUSED = &next (obj);
8810
8811 #ifdef ENABLE_GC_CHECKING
8812 memset (q, 0xa5, sizeof (*q));
8813 #endif
8814 /* Let valgrind know the entire object is available, but
8815 uninitialized. */
8816 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
8817 }
8818
8819 /* Reference a GTY-deletable pointer that points to the first object
8820 in the free list proper. */
8821 T *&head;
8822 public:
8823 /* Construct a freelist object chaining objects off of HEAD. */
8824 freelist (T *&head) : head(head) {}
8825
8826 /* Add OBJ to the free object list. The former head becomes OBJ's
8827 successor. */
8828 void free (T *obj)
8829 {
8830 poison (obj);
8831 next (obj) = head;
8832 head = obj;
8833 }
8834
8835 /* Take an object from the free list, if one is available, or
8836 allocate a new one. Objects taken from the free list should be
8837 regarded as filled with garbage, except for bits that are
8838 configured to be preserved across free and alloc. */
8839 T *alloc ()
8840 {
8841 if (head)
8842 {
8843 T *obj = head;
8844 head = next (head);
8845 reinit (obj);
8846 return obj;
8847 }
8848 else
8849 return anew ();
8850 }
8851 };
8852
8853 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8854 want to allocate a TREE_LIST using the usual interface, and ensure
8855 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8856 build_tree_list logic in reinit, so this could go out of sync. */
8857 template <>
8858 inline tree &
8859 freelist<tree_node>::next (tree obj)
8860 {
8861 return TREE_CHAIN (obj);
8862 }
8863 template <>
8864 inline tree
8865 freelist<tree_node>::anew ()
8866 {
8867 return build_tree_list (NULL, NULL);
8868 }
8869 template <>
8870 inline void
8871 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
8872 {
8873 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
8874 tree p ATTRIBUTE_UNUSED = obj;
8875 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8876 tree *q ATTRIBUTE_UNUSED = &next (obj);
8877
8878 #ifdef ENABLE_GC_CHECKING
8879 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8880
8881 /* Poison the data, to indicate the data is garbage. */
8882 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
8883 memset (p, 0xa5, size);
8884 #endif
8885 /* Let valgrind know the object is free. */
8886 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
8887 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8888 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8889 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8890
8891 #ifdef ENABLE_GC_CHECKING
8892 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
8893 /* Keep TREE_CHAIN functional. */
8894 TREE_SET_CODE (obj, TREE_LIST);
8895 #else
8896 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8897 #endif
8898 }
8899 template <>
8900 inline void
8901 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
8902 {
8903 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8904
8905 #ifdef ENABLE_GC_CHECKING
8906 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8907 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8908 memset (obj, 0, sizeof (tree_list));
8909 #endif
8910
8911 /* Let valgrind know the entire object is available, but
8912 uninitialized. */
8913 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8914
8915 #ifdef ENABLE_GC_CHECKING
8916 TREE_SET_CODE (obj, TREE_LIST);
8917 #else
8918 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8919 #endif
8920 }
8921
8922 /* Point to the first object in the TREE_LIST freelist. */
8923 static GTY((deletable)) tree tree_list_freelist_head;
8924 /* Return the/an actual TREE_LIST freelist. */
8925 static inline freelist<tree_node>
8926 tree_list_freelist ()
8927 {
8928 return tree_list_freelist_head;
8929 }
8930
8931 /* Point to the first object in the tinst_level freelist. */
8932 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
8933 /* Return the/an actual tinst_level freelist. */
8934 static inline freelist<tinst_level>
8935 tinst_level_freelist ()
8936 {
8937 return tinst_level_freelist_head;
8938 }
8939
8940 /* Point to the first object in the pending_template freelist. */
8941 static GTY((deletable)) pending_template *pending_template_freelist_head;
8942 /* Return the/an actual pending_template freelist. */
8943 static inline freelist<pending_template>
8944 pending_template_freelist ()
8945 {
8946 return pending_template_freelist_head;
8947 }
8948
8949 /* Build the TREE_LIST object out of a split list, store it
8950 permanently, and return it. */
8951 tree
8952 tinst_level::to_list ()
8953 {
8954 gcc_assert (split_list_p ());
8955 tree ret = tree_list_freelist ().alloc ();
8956 TREE_PURPOSE (ret) = tldcl;
8957 TREE_VALUE (ret) = targs;
8958 tldcl = ret;
8959 targs = NULL;
8960 gcc_assert (tree_list_p ());
8961 return ret;
8962 }
8963
8964 const unsigned short tinst_level::refcount_infinity;
8965
8966 /* Increment OBJ's refcount unless it is already infinite. */
8967 static tinst_level *
8968 inc_refcount_use (tinst_level *obj)
8969 {
8970 if (obj && obj->refcount != tinst_level::refcount_infinity)
8971 ++obj->refcount;
8972 return obj;
8973 }
8974
8975 /* Release storage for OBJ and node, if it's a TREE_LIST. */
8976 void
8977 tinst_level::free (tinst_level *obj)
8978 {
8979 if (obj->tree_list_p ())
8980 tree_list_freelist ().free (obj->get_node ());
8981 tinst_level_freelist ().free (obj);
8982 }
8983
8984 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
8985 OBJ's DECL and OBJ, and start over with the tinst_level object that
8986 used to be referenced by OBJ's NEXT. */
8987 static void
8988 dec_refcount_use (tinst_level *obj)
8989 {
8990 while (obj
8991 && obj->refcount != tinst_level::refcount_infinity
8992 && !--obj->refcount)
8993 {
8994 tinst_level *next = obj->next;
8995 tinst_level::free (obj);
8996 obj = next;
8997 }
8998 }
8999
9000 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9001 and of the former PTR. Omitting the second argument is equivalent
9002 to passing (T*)NULL; this is allowed because passing the
9003 zero-valued integral constant NULL confuses type deduction and/or
9004 overload resolution. */
9005 template <typename T>
9006 static void
9007 set_refcount_ptr (T *& ptr, T *obj = NULL)
9008 {
9009 T *save = ptr;
9010 ptr = inc_refcount_use (obj);
9011 dec_refcount_use (save);
9012 }
9013
9014 static void
9015 add_pending_template (tree d)
9016 {
9017 tree ti = (TYPE_P (d)
9018 ? CLASSTYPE_TEMPLATE_INFO (d)
9019 : DECL_TEMPLATE_INFO (d));
9020 struct pending_template *pt;
9021 int level;
9022
9023 if (TI_PENDING_TEMPLATE_FLAG (ti))
9024 return;
9025
9026 /* We are called both from instantiate_decl, where we've already had a
9027 tinst_level pushed, and instantiate_template, where we haven't.
9028 Compensate. */
9029 gcc_assert (TREE_CODE (d) != TREE_LIST);
9030 level = !current_tinst_level
9031 || current_tinst_level->maybe_get_node () != d;
9032
9033 if (level)
9034 push_tinst_level (d);
9035
9036 pt = pending_template_freelist ().alloc ();
9037 pt->next = NULL;
9038 pt->tinst = NULL;
9039 set_refcount_ptr (pt->tinst, current_tinst_level);
9040 if (last_pending_template)
9041 last_pending_template->next = pt;
9042 else
9043 pending_templates = pt;
9044
9045 last_pending_template = pt;
9046
9047 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9048
9049 if (level)
9050 pop_tinst_level ();
9051 }
9052
9053
9054 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9055 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9056 documentation for TEMPLATE_ID_EXPR. */
9057
9058 tree
9059 lookup_template_function (tree fns, tree arglist)
9060 {
9061 tree type;
9062
9063 if (fns == error_mark_node || arglist == error_mark_node)
9064 return error_mark_node;
9065
9066 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9067
9068 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9069 {
9070 error ("%q#D is not a function template", fns);
9071 return error_mark_node;
9072 }
9073
9074 if (BASELINK_P (fns))
9075 {
9076 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9077 unknown_type_node,
9078 BASELINK_FUNCTIONS (fns),
9079 arglist);
9080 return fns;
9081 }
9082
9083 type = TREE_TYPE (fns);
9084 if (TREE_CODE (fns) == OVERLOAD || !type)
9085 type = unknown_type_node;
9086
9087 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
9088 }
9089
9090 /* Within the scope of a template class S<T>, the name S gets bound
9091 (in build_self_reference) to a TYPE_DECL for the class, not a
9092 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9093 or one of its enclosing classes, and that type is a template,
9094 return the associated TEMPLATE_DECL. Otherwise, the original
9095 DECL is returned.
9096
9097 Also handle the case when DECL is a TREE_LIST of ambiguous
9098 injected-class-names from different bases. */
9099
9100 tree
9101 maybe_get_template_decl_from_type_decl (tree decl)
9102 {
9103 if (decl == NULL_TREE)
9104 return decl;
9105
9106 /* DR 176: A lookup that finds an injected-class-name (10.2
9107 [class.member.lookup]) can result in an ambiguity in certain cases
9108 (for example, if it is found in more than one base class). If all of
9109 the injected-class-names that are found refer to specializations of
9110 the same class template, and if the name is followed by a
9111 template-argument-list, the reference refers to the class template
9112 itself and not a specialization thereof, and is not ambiguous. */
9113 if (TREE_CODE (decl) == TREE_LIST)
9114 {
9115 tree t, tmpl = NULL_TREE;
9116 for (t = decl; t; t = TREE_CHAIN (t))
9117 {
9118 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9119 if (!tmpl)
9120 tmpl = elt;
9121 else if (tmpl != elt)
9122 break;
9123 }
9124 if (tmpl && t == NULL_TREE)
9125 return tmpl;
9126 else
9127 return decl;
9128 }
9129
9130 return (decl != NULL_TREE
9131 && DECL_SELF_REFERENCE_P (decl)
9132 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9133 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9134 }
9135
9136 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9137 parameters, find the desired type.
9138
9139 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9140
9141 IN_DECL, if non-NULL, is the template declaration we are trying to
9142 instantiate.
9143
9144 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9145 the class we are looking up.
9146
9147 Issue error and warning messages under control of COMPLAIN.
9148
9149 If the template class is really a local class in a template
9150 function, then the FUNCTION_CONTEXT is the function in which it is
9151 being instantiated.
9152
9153 ??? Note that this function is currently called *twice* for each
9154 template-id: the first time from the parser, while creating the
9155 incomplete type (finish_template_type), and the second type during the
9156 real instantiation (instantiate_template_class). This is surely something
9157 that we want to avoid. It also causes some problems with argument
9158 coercion (see convert_nontype_argument for more information on this). */
9159
9160 static tree
9161 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9162 int entering_scope, tsubst_flags_t complain)
9163 {
9164 tree templ = NULL_TREE, parmlist;
9165 tree t;
9166 spec_entry **slot;
9167 spec_entry *entry;
9168 spec_entry elt;
9169 hashval_t hash;
9170
9171 if (identifier_p (d1))
9172 {
9173 tree value = innermost_non_namespace_value (d1);
9174 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9175 templ = value;
9176 else
9177 {
9178 if (context)
9179 push_decl_namespace (context);
9180 templ = lookup_name (d1);
9181 templ = maybe_get_template_decl_from_type_decl (templ);
9182 if (context)
9183 pop_decl_namespace ();
9184 }
9185 if (templ)
9186 context = DECL_CONTEXT (templ);
9187 }
9188 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9189 {
9190 tree type = TREE_TYPE (d1);
9191
9192 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9193 an implicit typename for the second A. Deal with it. */
9194 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9195 type = TREE_TYPE (type);
9196
9197 if (CLASSTYPE_TEMPLATE_INFO (type))
9198 {
9199 templ = CLASSTYPE_TI_TEMPLATE (type);
9200 d1 = DECL_NAME (templ);
9201 }
9202 }
9203 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9204 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9205 {
9206 templ = TYPE_TI_TEMPLATE (d1);
9207 d1 = DECL_NAME (templ);
9208 }
9209 else if (DECL_TYPE_TEMPLATE_P (d1))
9210 {
9211 templ = d1;
9212 d1 = DECL_NAME (templ);
9213 context = DECL_CONTEXT (templ);
9214 }
9215 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9216 {
9217 templ = d1;
9218 d1 = DECL_NAME (templ);
9219 }
9220
9221 /* Issue an error message if we didn't find a template. */
9222 if (! templ)
9223 {
9224 if (complain & tf_error)
9225 error ("%qT is not a template", d1);
9226 return error_mark_node;
9227 }
9228
9229 if (TREE_CODE (templ) != TEMPLATE_DECL
9230 /* Make sure it's a user visible template, if it was named by
9231 the user. */
9232 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9233 && !PRIMARY_TEMPLATE_P (templ)))
9234 {
9235 if (complain & tf_error)
9236 {
9237 error ("non-template type %qT used as a template", d1);
9238 if (in_decl)
9239 error ("for template declaration %q+D", in_decl);
9240 }
9241 return error_mark_node;
9242 }
9243
9244 complain &= ~tf_user;
9245
9246 /* An alias that just changes the name of a template is equivalent to the
9247 other template, so if any of the arguments are pack expansions, strip
9248 the alias to avoid problems with a pack expansion passed to a non-pack
9249 alias template parameter (DR 1430). */
9250 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9251 templ = get_underlying_template (templ);
9252
9253 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9254 {
9255 tree parm;
9256 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9257 if (arglist2 == error_mark_node
9258 || (!uses_template_parms (arglist2)
9259 && check_instantiated_args (templ, arglist2, complain)))
9260 return error_mark_node;
9261
9262 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9263 return parm;
9264 }
9265 else
9266 {
9267 tree template_type = TREE_TYPE (templ);
9268 tree gen_tmpl;
9269 tree type_decl;
9270 tree found = NULL_TREE;
9271 int arg_depth;
9272 int parm_depth;
9273 int is_dependent_type;
9274 int use_partial_inst_tmpl = false;
9275
9276 if (template_type == error_mark_node)
9277 /* An error occurred while building the template TEMPL, and a
9278 diagnostic has most certainly been emitted for that
9279 already. Let's propagate that error. */
9280 return error_mark_node;
9281
9282 gen_tmpl = most_general_template (templ);
9283 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9284 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9285 arg_depth = TMPL_ARGS_DEPTH (arglist);
9286
9287 if (arg_depth == 1 && parm_depth > 1)
9288 {
9289 /* We've been given an incomplete set of template arguments.
9290 For example, given:
9291
9292 template <class T> struct S1 {
9293 template <class U> struct S2 {};
9294 template <class U> struct S2<U*> {};
9295 };
9296
9297 we will be called with an ARGLIST of `U*', but the
9298 TEMPLATE will be `template <class T> template
9299 <class U> struct S1<T>::S2'. We must fill in the missing
9300 arguments. */
9301 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9302 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9303 arg_depth = TMPL_ARGS_DEPTH (arglist);
9304 }
9305
9306 /* Now we should have enough arguments. */
9307 gcc_assert (parm_depth == arg_depth);
9308
9309 /* From here on, we're only interested in the most general
9310 template. */
9311
9312 /* Calculate the BOUND_ARGS. These will be the args that are
9313 actually tsubst'd into the definition to create the
9314 instantiation. */
9315 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9316 complain,
9317 /*require_all_args=*/true,
9318 /*use_default_args=*/true);
9319
9320 if (arglist == error_mark_node)
9321 /* We were unable to bind the arguments. */
9322 return error_mark_node;
9323
9324 /* In the scope of a template class, explicit references to the
9325 template class refer to the type of the template, not any
9326 instantiation of it. For example, in:
9327
9328 template <class T> class C { void f(C<T>); }
9329
9330 the `C<T>' is just the same as `C'. Outside of the
9331 class, however, such a reference is an instantiation. */
9332 if (entering_scope
9333 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9334 || currently_open_class (template_type))
9335 {
9336 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9337
9338 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9339 return template_type;
9340 }
9341
9342 /* If we already have this specialization, return it. */
9343 elt.tmpl = gen_tmpl;
9344 elt.args = arglist;
9345 elt.spec = NULL_TREE;
9346 hash = spec_hasher::hash (&elt);
9347 entry = type_specializations->find_with_hash (&elt, hash);
9348
9349 if (entry)
9350 return entry->spec;
9351
9352 /* If the the template's constraints are not satisfied,
9353 then we cannot form a valid type.
9354
9355 Note that the check is deferred until after the hash
9356 lookup. This prevents redundant checks on previously
9357 instantiated specializations. */
9358 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9359 {
9360 if (complain & tf_error)
9361 {
9362 auto_diagnostic_group d;
9363 error ("template constraint failure");
9364 diagnose_constraints (input_location, gen_tmpl, arglist);
9365 }
9366 return error_mark_node;
9367 }
9368
9369 is_dependent_type = uses_template_parms (arglist);
9370
9371 /* If the deduced arguments are invalid, then the binding
9372 failed. */
9373 if (!is_dependent_type
9374 && check_instantiated_args (gen_tmpl,
9375 INNERMOST_TEMPLATE_ARGS (arglist),
9376 complain))
9377 return error_mark_node;
9378
9379 if (!is_dependent_type
9380 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9381 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9382 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9383 {
9384 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9385 DECL_NAME (gen_tmpl),
9386 /*tag_scope=*/ts_global);
9387 return found;
9388 }
9389
9390 context = DECL_CONTEXT (gen_tmpl);
9391 if (context && TYPE_P (context))
9392 {
9393 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9394 context = complete_type (context);
9395 }
9396 else
9397 context = tsubst (context, arglist, complain, in_decl);
9398
9399 if (context == error_mark_node)
9400 return error_mark_node;
9401
9402 if (!context)
9403 context = global_namespace;
9404
9405 /* Create the type. */
9406 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9407 {
9408 /* The user referred to a specialization of an alias
9409 template represented by GEN_TMPL.
9410
9411 [temp.alias]/2 says:
9412
9413 When a template-id refers to the specialization of an
9414 alias template, it is equivalent to the associated
9415 type obtained by substitution of its
9416 template-arguments for the template-parameters in the
9417 type-id of the alias template. */
9418
9419 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9420 /* Note that the call above (by indirectly calling
9421 register_specialization in tsubst_decl) registers the
9422 TYPE_DECL representing the specialization of the alias
9423 template. So next time someone substitutes ARGLIST for
9424 the template parms into the alias template (GEN_TMPL),
9425 she'll get that TYPE_DECL back. */
9426
9427 if (t == error_mark_node)
9428 return t;
9429 }
9430 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9431 {
9432 if (!is_dependent_type)
9433 {
9434 set_current_access_from_decl (TYPE_NAME (template_type));
9435 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9436 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9437 arglist, complain, in_decl),
9438 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9439 arglist, complain, in_decl),
9440 SCOPED_ENUM_P (template_type), NULL);
9441
9442 if (t == error_mark_node)
9443 return t;
9444 }
9445 else
9446 {
9447 /* We don't want to call start_enum for this type, since
9448 the values for the enumeration constants may involve
9449 template parameters. And, no one should be interested
9450 in the enumeration constants for such a type. */
9451 t = cxx_make_type (ENUMERAL_TYPE);
9452 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9453 }
9454 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9455 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9456 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9457 }
9458 else if (CLASS_TYPE_P (template_type))
9459 {
9460 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9461 instantiated here. */
9462 gcc_assert (!LAMBDA_TYPE_P (template_type));
9463
9464 t = make_class_type (TREE_CODE (template_type));
9465 CLASSTYPE_DECLARED_CLASS (t)
9466 = CLASSTYPE_DECLARED_CLASS (template_type);
9467 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9468
9469 /* A local class. Make sure the decl gets registered properly. */
9470 if (context == current_function_decl)
9471 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9472 == error_mark_node)
9473 return error_mark_node;
9474
9475 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9476 /* This instantiation is another name for the primary
9477 template type. Set the TYPE_CANONICAL field
9478 appropriately. */
9479 TYPE_CANONICAL (t) = template_type;
9480 else if (any_template_arguments_need_structural_equality_p (arglist))
9481 /* Some of the template arguments require structural
9482 equality testing, so this template class requires
9483 structural equality testing. */
9484 SET_TYPE_STRUCTURAL_EQUALITY (t);
9485 }
9486 else
9487 gcc_unreachable ();
9488
9489 /* If we called start_enum or pushtag above, this information
9490 will already be set up. */
9491 if (!TYPE_NAME (t))
9492 {
9493 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9494
9495 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9496 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9497 DECL_SOURCE_LOCATION (type_decl)
9498 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9499 }
9500 else
9501 type_decl = TYPE_NAME (t);
9502
9503 if (CLASS_TYPE_P (template_type))
9504 {
9505 TREE_PRIVATE (type_decl)
9506 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9507 TREE_PROTECTED (type_decl)
9508 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9509 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9510 {
9511 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9512 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9513 }
9514 }
9515
9516 if (OVERLOAD_TYPE_P (t)
9517 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9518 {
9519 static const char *tags[] = {"abi_tag", "may_alias"};
9520
9521 for (unsigned ix = 0; ix != 2; ix++)
9522 {
9523 tree attributes
9524 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9525
9526 if (attributes)
9527 TYPE_ATTRIBUTES (t)
9528 = tree_cons (TREE_PURPOSE (attributes),
9529 TREE_VALUE (attributes),
9530 TYPE_ATTRIBUTES (t));
9531 }
9532 }
9533
9534 /* Let's consider the explicit specialization of a member
9535 of a class template specialization that is implicitly instantiated,
9536 e.g.:
9537 template<class T>
9538 struct S
9539 {
9540 template<class U> struct M {}; //#0
9541 };
9542
9543 template<>
9544 template<>
9545 struct S<int>::M<char> //#1
9546 {
9547 int i;
9548 };
9549 [temp.expl.spec]/4 says this is valid.
9550
9551 In this case, when we write:
9552 S<int>::M<char> m;
9553
9554 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9555 the one of #0.
9556
9557 When we encounter #1, we want to store the partial instantiation
9558 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9559
9560 For all cases other than this "explicit specialization of member of a
9561 class template", we just want to store the most general template into
9562 the CLASSTYPE_TI_TEMPLATE of M.
9563
9564 This case of "explicit specialization of member of a class template"
9565 only happens when:
9566 1/ the enclosing class is an instantiation of, and therefore not
9567 the same as, the context of the most general template, and
9568 2/ we aren't looking at the partial instantiation itself, i.e.
9569 the innermost arguments are not the same as the innermost parms of
9570 the most general template.
9571
9572 So it's only when 1/ and 2/ happens that we want to use the partial
9573 instantiation of the member template in lieu of its most general
9574 template. */
9575
9576 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9577 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9578 /* the enclosing class must be an instantiation... */
9579 && CLASS_TYPE_P (context)
9580 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9581 {
9582 TREE_VEC_LENGTH (arglist)--;
9583 ++processing_template_decl;
9584 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9585 tree partial_inst_args =
9586 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9587 arglist, complain, NULL_TREE);
9588 --processing_template_decl;
9589 TREE_VEC_LENGTH (arglist)++;
9590 if (partial_inst_args == error_mark_node)
9591 return error_mark_node;
9592 use_partial_inst_tmpl =
9593 /*...and we must not be looking at the partial instantiation
9594 itself. */
9595 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9596 partial_inst_args);
9597 }
9598
9599 if (!use_partial_inst_tmpl)
9600 /* This case is easy; there are no member templates involved. */
9601 found = gen_tmpl;
9602 else
9603 {
9604 /* This is a full instantiation of a member template. Find
9605 the partial instantiation of which this is an instance. */
9606
9607 /* Temporarily reduce by one the number of levels in the ARGLIST
9608 so as to avoid comparing the last set of arguments. */
9609 TREE_VEC_LENGTH (arglist)--;
9610 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9611 TREE_VEC_LENGTH (arglist)++;
9612 /* FOUND is either a proper class type, or an alias
9613 template specialization. In the later case, it's a
9614 TYPE_DECL, resulting from the substituting of arguments
9615 for parameters in the TYPE_DECL of the alias template
9616 done earlier. So be careful while getting the template
9617 of FOUND. */
9618 found = (TREE_CODE (found) == TEMPLATE_DECL
9619 ? found
9620 : (TREE_CODE (found) == TYPE_DECL
9621 ? DECL_TI_TEMPLATE (found)
9622 : CLASSTYPE_TI_TEMPLATE (found)));
9623 }
9624
9625 // Build template info for the new specialization.
9626 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9627
9628 elt.spec = t;
9629 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9630 entry = ggc_alloc<spec_entry> ();
9631 *entry = elt;
9632 *slot = entry;
9633
9634 /* Note this use of the partial instantiation so we can check it
9635 later in maybe_process_partial_specialization. */
9636 DECL_TEMPLATE_INSTANTIATIONS (found)
9637 = tree_cons (arglist, t,
9638 DECL_TEMPLATE_INSTANTIATIONS (found));
9639
9640 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9641 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9642 /* Now that the type has been registered on the instantiations
9643 list, we set up the enumerators. Because the enumeration
9644 constants may involve the enumeration type itself, we make
9645 sure to register the type first, and then create the
9646 constants. That way, doing tsubst_expr for the enumeration
9647 constants won't result in recursive calls here; we'll find
9648 the instantiation and exit above. */
9649 tsubst_enum (template_type, t, arglist);
9650
9651 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9652 /* If the type makes use of template parameters, the
9653 code that generates debugging information will crash. */
9654 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9655
9656 /* Possibly limit visibility based on template args. */
9657 TREE_PUBLIC (type_decl) = 1;
9658 determine_visibility (type_decl);
9659
9660 inherit_targ_abi_tags (t);
9661
9662 return t;
9663 }
9664 }
9665
9666 /* Wrapper for lookup_template_class_1. */
9667
9668 tree
9669 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9670 int entering_scope, tsubst_flags_t complain)
9671 {
9672 tree ret;
9673 timevar_push (TV_TEMPLATE_INST);
9674 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9675 entering_scope, complain);
9676 timevar_pop (TV_TEMPLATE_INST);
9677 return ret;
9678 }
9679
9680 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9681
9682 tree
9683 lookup_template_variable (tree templ, tree arglist)
9684 {
9685 /* The type of the expression is NULL_TREE since the template-id could refer
9686 to an explicit or partial specialization. */
9687 tree type = NULL_TREE;
9688 if (flag_concepts && variable_concept_p (templ))
9689 /* Except that concepts are always bool. */
9690 type = boolean_type_node;
9691 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9692 }
9693
9694 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9695
9696 tree
9697 finish_template_variable (tree var, tsubst_flags_t complain)
9698 {
9699 tree templ = TREE_OPERAND (var, 0);
9700 tree arglist = TREE_OPERAND (var, 1);
9701
9702 /* We never want to return a VAR_DECL for a variable concept, since they
9703 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9704 bool concept_p = flag_concepts && variable_concept_p (templ);
9705 if (concept_p && processing_template_decl)
9706 return var;
9707
9708 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9709 arglist = add_outermost_template_args (tmpl_args, arglist);
9710
9711 templ = most_general_template (templ);
9712 tree parms = DECL_TEMPLATE_PARMS (templ);
9713 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9714 /*req_all*/true,
9715 /*use_default*/true);
9716
9717 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9718 {
9719 if (complain & tf_error)
9720 {
9721 auto_diagnostic_group d;
9722 error ("use of invalid variable template %qE", var);
9723 diagnose_constraints (location_of (var), templ, arglist);
9724 }
9725 return error_mark_node;
9726 }
9727
9728 /* If a template-id refers to a specialization of a variable
9729 concept, then the expression is true if and only if the
9730 concept's constraints are satisfied by the given template
9731 arguments.
9732
9733 NOTE: This is an extension of Concepts Lite TS that
9734 allows constraints to be used in expressions. */
9735 if (concept_p)
9736 {
9737 tree decl = DECL_TEMPLATE_RESULT (templ);
9738 return evaluate_variable_concept (decl, arglist);
9739 }
9740
9741 return instantiate_template (templ, arglist, complain);
9742 }
9743
9744 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9745 TARGS template args, and instantiate it if it's not dependent. */
9746
9747 tree
9748 lookup_and_finish_template_variable (tree templ, tree targs,
9749 tsubst_flags_t complain)
9750 {
9751 templ = lookup_template_variable (templ, targs);
9752 if (!any_dependent_template_arguments_p (targs))
9753 {
9754 templ = finish_template_variable (templ, complain);
9755 mark_used (templ);
9756 }
9757
9758 return convert_from_reference (templ);
9759 }
9760
9761 \f
9762 struct pair_fn_data
9763 {
9764 tree_fn_t fn;
9765 tree_fn_t any_fn;
9766 void *data;
9767 /* True when we should also visit template parameters that occur in
9768 non-deduced contexts. */
9769 bool include_nondeduced_p;
9770 hash_set<tree> *visited;
9771 };
9772
9773 /* Called from for_each_template_parm via walk_tree. */
9774
9775 static tree
9776 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9777 {
9778 tree t = *tp;
9779 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9780 tree_fn_t fn = pfd->fn;
9781 void *data = pfd->data;
9782 tree result = NULL_TREE;
9783
9784 #define WALK_SUBTREE(NODE) \
9785 do \
9786 { \
9787 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9788 pfd->include_nondeduced_p, \
9789 pfd->any_fn); \
9790 if (result) goto out; \
9791 } \
9792 while (0)
9793
9794 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9795 return t;
9796
9797 if (TYPE_P (t)
9798 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9799 WALK_SUBTREE (TYPE_CONTEXT (t));
9800
9801 switch (TREE_CODE (t))
9802 {
9803 case RECORD_TYPE:
9804 if (TYPE_PTRMEMFUNC_P (t))
9805 break;
9806 /* Fall through. */
9807
9808 case UNION_TYPE:
9809 case ENUMERAL_TYPE:
9810 if (!TYPE_TEMPLATE_INFO (t))
9811 *walk_subtrees = 0;
9812 else
9813 WALK_SUBTREE (TYPE_TI_ARGS (t));
9814 break;
9815
9816 case INTEGER_TYPE:
9817 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9818 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9819 break;
9820
9821 case METHOD_TYPE:
9822 /* Since we're not going to walk subtrees, we have to do this
9823 explicitly here. */
9824 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9825 /* Fall through. */
9826
9827 case FUNCTION_TYPE:
9828 /* Check the return type. */
9829 WALK_SUBTREE (TREE_TYPE (t));
9830
9831 /* Check the parameter types. Since default arguments are not
9832 instantiated until they are needed, the TYPE_ARG_TYPES may
9833 contain expressions that involve template parameters. But,
9834 no-one should be looking at them yet. And, once they're
9835 instantiated, they don't contain template parameters, so
9836 there's no point in looking at them then, either. */
9837 {
9838 tree parm;
9839
9840 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9841 WALK_SUBTREE (TREE_VALUE (parm));
9842
9843 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9844 want walk_tree walking into them itself. */
9845 *walk_subtrees = 0;
9846 }
9847
9848 if (flag_noexcept_type)
9849 {
9850 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9851 if (spec)
9852 WALK_SUBTREE (TREE_PURPOSE (spec));
9853 }
9854 break;
9855
9856 case TYPEOF_TYPE:
9857 case DECLTYPE_TYPE:
9858 case UNDERLYING_TYPE:
9859 if (pfd->include_nondeduced_p
9860 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9861 pfd->visited,
9862 pfd->include_nondeduced_p,
9863 pfd->any_fn))
9864 return error_mark_node;
9865 *walk_subtrees = false;
9866 break;
9867
9868 case FUNCTION_DECL:
9869 case VAR_DECL:
9870 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9871 WALK_SUBTREE (DECL_TI_ARGS (t));
9872 /* Fall through. */
9873
9874 case PARM_DECL:
9875 case CONST_DECL:
9876 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9877 WALK_SUBTREE (DECL_INITIAL (t));
9878 if (DECL_CONTEXT (t)
9879 && pfd->include_nondeduced_p)
9880 WALK_SUBTREE (DECL_CONTEXT (t));
9881 break;
9882
9883 case BOUND_TEMPLATE_TEMPLATE_PARM:
9884 /* Record template parameters such as `T' inside `TT<T>'. */
9885 WALK_SUBTREE (TYPE_TI_ARGS (t));
9886 /* Fall through. */
9887
9888 case TEMPLATE_TEMPLATE_PARM:
9889 case TEMPLATE_TYPE_PARM:
9890 case TEMPLATE_PARM_INDEX:
9891 if (fn && (*fn)(t, data))
9892 return t;
9893 else if (!fn)
9894 return t;
9895 break;
9896
9897 case TEMPLATE_DECL:
9898 /* A template template parameter is encountered. */
9899 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9900 WALK_SUBTREE (TREE_TYPE (t));
9901
9902 /* Already substituted template template parameter */
9903 *walk_subtrees = 0;
9904 break;
9905
9906 case TYPENAME_TYPE:
9907 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9908 partial instantiation. */
9909 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9910 break;
9911
9912 case CONSTRUCTOR:
9913 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9914 && pfd->include_nondeduced_p)
9915 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9916 break;
9917
9918 case INDIRECT_REF:
9919 case COMPONENT_REF:
9920 /* If there's no type, then this thing must be some expression
9921 involving template parameters. */
9922 if (!fn && !TREE_TYPE (t))
9923 return error_mark_node;
9924 break;
9925
9926 case MODOP_EXPR:
9927 case CAST_EXPR:
9928 case IMPLICIT_CONV_EXPR:
9929 case REINTERPRET_CAST_EXPR:
9930 case CONST_CAST_EXPR:
9931 case STATIC_CAST_EXPR:
9932 case DYNAMIC_CAST_EXPR:
9933 case ARROW_EXPR:
9934 case DOTSTAR_EXPR:
9935 case TYPEID_EXPR:
9936 case PSEUDO_DTOR_EXPR:
9937 if (!fn)
9938 return error_mark_node;
9939 break;
9940
9941 default:
9942 break;
9943 }
9944
9945 #undef WALK_SUBTREE
9946
9947 /* We didn't find any template parameters we liked. */
9948 out:
9949 return result;
9950 }
9951
9952 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9953 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9954 call FN with the parameter and the DATA.
9955 If FN returns nonzero, the iteration is terminated, and
9956 for_each_template_parm returns 1. Otherwise, the iteration
9957 continues. If FN never returns a nonzero value, the value
9958 returned by for_each_template_parm is 0. If FN is NULL, it is
9959 considered to be the function which always returns 1.
9960
9961 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9962 parameters that occur in non-deduced contexts. When false, only
9963 visits those template parameters that can be deduced. */
9964
9965 static tree
9966 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9967 hash_set<tree> *visited,
9968 bool include_nondeduced_p,
9969 tree_fn_t any_fn)
9970 {
9971 struct pair_fn_data pfd;
9972 tree result;
9973
9974 /* Set up. */
9975 pfd.fn = fn;
9976 pfd.any_fn = any_fn;
9977 pfd.data = data;
9978 pfd.include_nondeduced_p = include_nondeduced_p;
9979
9980 /* Walk the tree. (Conceptually, we would like to walk without
9981 duplicates, but for_each_template_parm_r recursively calls
9982 for_each_template_parm, so we would need to reorganize a fair
9983 bit to use walk_tree_without_duplicates, so we keep our own
9984 visited list.) */
9985 if (visited)
9986 pfd.visited = visited;
9987 else
9988 pfd.visited = new hash_set<tree>;
9989 result = cp_walk_tree (&t,
9990 for_each_template_parm_r,
9991 &pfd,
9992 pfd.visited);
9993
9994 /* Clean up. */
9995 if (!visited)
9996 {
9997 delete pfd.visited;
9998 pfd.visited = 0;
9999 }
10000
10001 return result;
10002 }
10003
10004 /* Returns true if T depends on any template parameter. */
10005
10006 int
10007 uses_template_parms (tree t)
10008 {
10009 if (t == NULL_TREE)
10010 return false;
10011
10012 bool dependent_p;
10013 int saved_processing_template_decl;
10014
10015 saved_processing_template_decl = processing_template_decl;
10016 if (!saved_processing_template_decl)
10017 processing_template_decl = 1;
10018 if (TYPE_P (t))
10019 dependent_p = dependent_type_p (t);
10020 else if (TREE_CODE (t) == TREE_VEC)
10021 dependent_p = any_dependent_template_arguments_p (t);
10022 else if (TREE_CODE (t) == TREE_LIST)
10023 dependent_p = (uses_template_parms (TREE_VALUE (t))
10024 || uses_template_parms (TREE_CHAIN (t)));
10025 else if (TREE_CODE (t) == TYPE_DECL)
10026 dependent_p = dependent_type_p (TREE_TYPE (t));
10027 else if (DECL_P (t)
10028 || EXPR_P (t)
10029 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10030 || TREE_CODE (t) == OVERLOAD
10031 || BASELINK_P (t)
10032 || identifier_p (t)
10033 || TREE_CODE (t) == TRAIT_EXPR
10034 || TREE_CODE (t) == CONSTRUCTOR
10035 || CONSTANT_CLASS_P (t))
10036 dependent_p = (type_dependent_expression_p (t)
10037 || value_dependent_expression_p (t));
10038 else
10039 {
10040 gcc_assert (t == error_mark_node);
10041 dependent_p = false;
10042 }
10043
10044 processing_template_decl = saved_processing_template_decl;
10045
10046 return dependent_p;
10047 }
10048
10049 /* Returns true iff current_function_decl is an incompletely instantiated
10050 template. Useful instead of processing_template_decl because the latter
10051 is set to 0 during instantiate_non_dependent_expr. */
10052
10053 bool
10054 in_template_function (void)
10055 {
10056 tree fn = current_function_decl;
10057 bool ret;
10058 ++processing_template_decl;
10059 ret = (fn && DECL_LANG_SPECIFIC (fn)
10060 && DECL_TEMPLATE_INFO (fn)
10061 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10062 --processing_template_decl;
10063 return ret;
10064 }
10065
10066 /* Returns true if T depends on any template parameter with level LEVEL. */
10067
10068 bool
10069 uses_template_parms_level (tree t, int level)
10070 {
10071 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10072 /*include_nondeduced_p=*/true);
10073 }
10074
10075 /* Returns true if the signature of DECL depends on any template parameter from
10076 its enclosing class. */
10077
10078 bool
10079 uses_outer_template_parms (tree decl)
10080 {
10081 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10082 if (depth == 0)
10083 return false;
10084 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10085 &depth, NULL, /*include_nondeduced_p=*/true))
10086 return true;
10087 if (PRIMARY_TEMPLATE_P (decl)
10088 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10089 (DECL_TEMPLATE_PARMS (decl)),
10090 template_parm_outer_level,
10091 &depth, NULL, /*include_nondeduced_p=*/true))
10092 return true;
10093 tree ci = get_constraints (decl);
10094 if (ci)
10095 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10096 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10097 &depth, NULL, /*nondeduced*/true))
10098 return true;
10099 return false;
10100 }
10101
10102 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10103 ill-formed translation unit, i.e. a variable or function that isn't
10104 usable in a constant expression. */
10105
10106 static inline bool
10107 neglectable_inst_p (tree d)
10108 {
10109 return (d && DECL_P (d)
10110 && !undeduced_auto_decl (d)
10111 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10112 : decl_maybe_constant_var_p (d)));
10113 }
10114
10115 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10116 neglectable and instantiated from within an erroneous instantiation. */
10117
10118 static bool
10119 limit_bad_template_recursion (tree decl)
10120 {
10121 struct tinst_level *lev = current_tinst_level;
10122 int errs = errorcount + sorrycount;
10123 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10124 return false;
10125
10126 for (; lev; lev = lev->next)
10127 if (neglectable_inst_p (lev->maybe_get_node ()))
10128 break;
10129
10130 return (lev && errs > lev->errors);
10131 }
10132
10133 static int tinst_depth;
10134 extern int max_tinst_depth;
10135 int depth_reached;
10136
10137 static GTY(()) struct tinst_level *last_error_tinst_level;
10138
10139 /* We're starting to instantiate D; record the template instantiation context
10140 at LOC for diagnostics and to restore it later. */
10141
10142 static bool
10143 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10144 {
10145 struct tinst_level *new_level;
10146
10147 if (tinst_depth >= max_tinst_depth)
10148 {
10149 /* Tell error.c not to try to instantiate any templates. */
10150 at_eof = 2;
10151 fatal_error (input_location,
10152 "template instantiation depth exceeds maximum of %d"
10153 " (use -ftemplate-depth= to increase the maximum)",
10154 max_tinst_depth);
10155 return false;
10156 }
10157
10158 /* If the current instantiation caused problems, don't let it instantiate
10159 anything else. Do allow deduction substitution and decls usable in
10160 constant expressions. */
10161 if (!targs && limit_bad_template_recursion (tldcl))
10162 return false;
10163
10164 /* When not -quiet, dump template instantiations other than functions, since
10165 announce_function will take care of those. */
10166 if (!quiet_flag && !targs
10167 && TREE_CODE (tldcl) != TREE_LIST
10168 && TREE_CODE (tldcl) != FUNCTION_DECL)
10169 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10170
10171 new_level = tinst_level_freelist ().alloc ();
10172 new_level->tldcl = tldcl;
10173 new_level->targs = targs;
10174 new_level->locus = loc;
10175 new_level->errors = errorcount + sorrycount;
10176 new_level->next = NULL;
10177 new_level->refcount = 0;
10178 set_refcount_ptr (new_level->next, current_tinst_level);
10179 set_refcount_ptr (current_tinst_level, new_level);
10180
10181 ++tinst_depth;
10182 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10183 depth_reached = tinst_depth;
10184
10185 return true;
10186 }
10187
10188 /* We're starting substitution of TMPL<ARGS>; record the template
10189 substitution context for diagnostics and to restore it later. */
10190
10191 static bool
10192 push_tinst_level (tree tmpl, tree args)
10193 {
10194 return push_tinst_level_loc (tmpl, args, input_location);
10195 }
10196
10197 /* We're starting to instantiate D; record INPUT_LOCATION and the
10198 template instantiation context for diagnostics and to restore it
10199 later. */
10200
10201 bool
10202 push_tinst_level (tree d)
10203 {
10204 return push_tinst_level_loc (d, input_location);
10205 }
10206
10207 /* Likewise, but record LOC as the program location. */
10208
10209 bool
10210 push_tinst_level_loc (tree d, location_t loc)
10211 {
10212 gcc_assert (TREE_CODE (d) != TREE_LIST);
10213 return push_tinst_level_loc (d, NULL, loc);
10214 }
10215
10216 /* We're done instantiating this template; return to the instantiation
10217 context. */
10218
10219 void
10220 pop_tinst_level (void)
10221 {
10222 /* Restore the filename and line number stashed away when we started
10223 this instantiation. */
10224 input_location = current_tinst_level->locus;
10225 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10226 --tinst_depth;
10227 }
10228
10229 /* We're instantiating a deferred template; restore the template
10230 instantiation context in which the instantiation was requested, which
10231 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10232
10233 static tree
10234 reopen_tinst_level (struct tinst_level *level)
10235 {
10236 struct tinst_level *t;
10237
10238 tinst_depth = 0;
10239 for (t = level; t; t = t->next)
10240 ++tinst_depth;
10241
10242 set_refcount_ptr (current_tinst_level, level);
10243 pop_tinst_level ();
10244 if (current_tinst_level)
10245 current_tinst_level->errors = errorcount+sorrycount;
10246 return level->maybe_get_node ();
10247 }
10248
10249 /* Returns the TINST_LEVEL which gives the original instantiation
10250 context. */
10251
10252 struct tinst_level *
10253 outermost_tinst_level (void)
10254 {
10255 struct tinst_level *level = current_tinst_level;
10256 if (level)
10257 while (level->next)
10258 level = level->next;
10259 return level;
10260 }
10261
10262 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10263 vector of template arguments, as for tsubst.
10264
10265 Returns an appropriate tsubst'd friend declaration. */
10266
10267 static tree
10268 tsubst_friend_function (tree decl, tree args)
10269 {
10270 tree new_friend;
10271
10272 if (TREE_CODE (decl) == FUNCTION_DECL
10273 && DECL_TEMPLATE_INSTANTIATION (decl)
10274 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10275 /* This was a friend declared with an explicit template
10276 argument list, e.g.:
10277
10278 friend void f<>(T);
10279
10280 to indicate that f was a template instantiation, not a new
10281 function declaration. Now, we have to figure out what
10282 instantiation of what template. */
10283 {
10284 tree template_id, arglist, fns;
10285 tree new_args;
10286 tree tmpl;
10287 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10288
10289 /* Friend functions are looked up in the containing namespace scope.
10290 We must enter that scope, to avoid finding member functions of the
10291 current class with same name. */
10292 push_nested_namespace (ns);
10293 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10294 tf_warning_or_error, NULL_TREE,
10295 /*integral_constant_expression_p=*/false);
10296 pop_nested_namespace (ns);
10297 arglist = tsubst (DECL_TI_ARGS (decl), args,
10298 tf_warning_or_error, NULL_TREE);
10299 template_id = lookup_template_function (fns, arglist);
10300
10301 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10302 tmpl = determine_specialization (template_id, new_friend,
10303 &new_args,
10304 /*need_member_template=*/0,
10305 TREE_VEC_LENGTH (args),
10306 tsk_none);
10307 return instantiate_template (tmpl, new_args, tf_error);
10308 }
10309
10310 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10311
10312 /* The NEW_FRIEND will look like an instantiation, to the
10313 compiler, but is not an instantiation from the point of view of
10314 the language. For example, we might have had:
10315
10316 template <class T> struct S {
10317 template <class U> friend void f(T, U);
10318 };
10319
10320 Then, in S<int>, template <class U> void f(int, U) is not an
10321 instantiation of anything. */
10322 if (new_friend == error_mark_node)
10323 return error_mark_node;
10324
10325 DECL_USE_TEMPLATE (new_friend) = 0;
10326 if (TREE_CODE (decl) == TEMPLATE_DECL)
10327 {
10328 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10329 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10330 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10331 }
10332
10333 /* The mangled name for the NEW_FRIEND is incorrect. The function
10334 is not a template instantiation and should not be mangled like
10335 one. Therefore, we forget the mangling here; we'll recompute it
10336 later if we need it. */
10337 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10338 {
10339 SET_DECL_RTL (new_friend, NULL);
10340 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10341 }
10342
10343 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10344 {
10345 tree old_decl;
10346 tree new_friend_template_info;
10347 tree new_friend_result_template_info;
10348 tree ns;
10349 int new_friend_is_defn;
10350
10351 /* We must save some information from NEW_FRIEND before calling
10352 duplicate decls since that function will free NEW_FRIEND if
10353 possible. */
10354 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10355 new_friend_is_defn =
10356 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10357 (template_for_substitution (new_friend)))
10358 != NULL_TREE);
10359 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10360 {
10361 /* This declaration is a `primary' template. */
10362 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10363
10364 new_friend_result_template_info
10365 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10366 }
10367 else
10368 new_friend_result_template_info = NULL_TREE;
10369
10370 /* Inside pushdecl_namespace_level, we will push into the
10371 current namespace. However, the friend function should go
10372 into the namespace of the template. */
10373 ns = decl_namespace_context (new_friend);
10374 push_nested_namespace (ns);
10375 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10376 pop_nested_namespace (ns);
10377
10378 if (old_decl == error_mark_node)
10379 return error_mark_node;
10380
10381 if (old_decl != new_friend)
10382 {
10383 /* This new friend declaration matched an existing
10384 declaration. For example, given:
10385
10386 template <class T> void f(T);
10387 template <class U> class C {
10388 template <class T> friend void f(T) {}
10389 };
10390
10391 the friend declaration actually provides the definition
10392 of `f', once C has been instantiated for some type. So,
10393 old_decl will be the out-of-class template declaration,
10394 while new_friend is the in-class definition.
10395
10396 But, if `f' was called before this point, the
10397 instantiation of `f' will have DECL_TI_ARGS corresponding
10398 to `T' but not to `U', references to which might appear
10399 in the definition of `f'. Previously, the most general
10400 template for an instantiation of `f' was the out-of-class
10401 version; now it is the in-class version. Therefore, we
10402 run through all specialization of `f', adding to their
10403 DECL_TI_ARGS appropriately. In particular, they need a
10404 new set of outer arguments, corresponding to the
10405 arguments for this class instantiation.
10406
10407 The same situation can arise with something like this:
10408
10409 friend void f(int);
10410 template <class T> class C {
10411 friend void f(T) {}
10412 };
10413
10414 when `C<int>' is instantiated. Now, `f(int)' is defined
10415 in the class. */
10416
10417 if (!new_friend_is_defn)
10418 /* On the other hand, if the in-class declaration does
10419 *not* provide a definition, then we don't want to alter
10420 existing definitions. We can just leave everything
10421 alone. */
10422 ;
10423 else
10424 {
10425 tree new_template = TI_TEMPLATE (new_friend_template_info);
10426 tree new_args = TI_ARGS (new_friend_template_info);
10427
10428 /* Overwrite whatever template info was there before, if
10429 any, with the new template information pertaining to
10430 the declaration. */
10431 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10432
10433 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10434 {
10435 /* We should have called reregister_specialization in
10436 duplicate_decls. */
10437 gcc_assert (retrieve_specialization (new_template,
10438 new_args, 0)
10439 == old_decl);
10440
10441 /* Instantiate it if the global has already been used. */
10442 if (DECL_ODR_USED (old_decl))
10443 instantiate_decl (old_decl, /*defer_ok=*/true,
10444 /*expl_inst_class_mem_p=*/false);
10445 }
10446 else
10447 {
10448 tree t;
10449
10450 /* Indicate that the old function template is a partial
10451 instantiation. */
10452 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10453 = new_friend_result_template_info;
10454
10455 gcc_assert (new_template
10456 == most_general_template (new_template));
10457 gcc_assert (new_template != old_decl);
10458
10459 /* Reassign any specializations already in the hash table
10460 to the new more general template, and add the
10461 additional template args. */
10462 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10463 t != NULL_TREE;
10464 t = TREE_CHAIN (t))
10465 {
10466 tree spec = TREE_VALUE (t);
10467 spec_entry elt;
10468
10469 elt.tmpl = old_decl;
10470 elt.args = DECL_TI_ARGS (spec);
10471 elt.spec = NULL_TREE;
10472
10473 decl_specializations->remove_elt (&elt);
10474
10475 DECL_TI_ARGS (spec)
10476 = add_outermost_template_args (new_args,
10477 DECL_TI_ARGS (spec));
10478
10479 register_specialization
10480 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10481
10482 }
10483 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10484 }
10485 }
10486
10487 /* The information from NEW_FRIEND has been merged into OLD_DECL
10488 by duplicate_decls. */
10489 new_friend = old_decl;
10490 }
10491 }
10492 else
10493 {
10494 tree context = DECL_CONTEXT (new_friend);
10495 bool dependent_p;
10496
10497 /* In the code
10498 template <class T> class C {
10499 template <class U> friend void C1<U>::f (); // case 1
10500 friend void C2<T>::f (); // case 2
10501 };
10502 we only need to make sure CONTEXT is a complete type for
10503 case 2. To distinguish between the two cases, we note that
10504 CONTEXT of case 1 remains dependent type after tsubst while
10505 this isn't true for case 2. */
10506 ++processing_template_decl;
10507 dependent_p = dependent_type_p (context);
10508 --processing_template_decl;
10509
10510 if (!dependent_p
10511 && !complete_type_or_else (context, NULL_TREE))
10512 return error_mark_node;
10513
10514 if (COMPLETE_TYPE_P (context))
10515 {
10516 tree fn = new_friend;
10517 /* do_friend adds the TEMPLATE_DECL for any member friend
10518 template even if it isn't a member template, i.e.
10519 template <class T> friend A<T>::f();
10520 Look through it in that case. */
10521 if (TREE_CODE (fn) == TEMPLATE_DECL
10522 && !PRIMARY_TEMPLATE_P (fn))
10523 fn = DECL_TEMPLATE_RESULT (fn);
10524 /* Check to see that the declaration is really present, and,
10525 possibly obtain an improved declaration. */
10526 fn = check_classfn (context, fn, NULL_TREE);
10527
10528 if (fn)
10529 new_friend = fn;
10530 }
10531 }
10532
10533 return new_friend;
10534 }
10535
10536 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10537 template arguments, as for tsubst.
10538
10539 Returns an appropriate tsubst'd friend type or error_mark_node on
10540 failure. */
10541
10542 static tree
10543 tsubst_friend_class (tree friend_tmpl, tree args)
10544 {
10545 tree tmpl;
10546
10547 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10548 {
10549 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10550 return TREE_TYPE (tmpl);
10551 }
10552
10553 tree context = CP_DECL_CONTEXT (friend_tmpl);
10554 if (TREE_CODE (context) == NAMESPACE_DECL)
10555 push_nested_namespace (context);
10556 else
10557 push_nested_class (context);
10558
10559 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10560 /*non_class=*/false, /*block_p=*/false,
10561 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10562
10563 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10564 {
10565 /* The friend template has already been declared. Just
10566 check to see that the declarations match, and install any new
10567 default parameters. We must tsubst the default parameters,
10568 of course. We only need the innermost template parameters
10569 because that is all that redeclare_class_template will look
10570 at. */
10571 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10572 > TMPL_ARGS_DEPTH (args))
10573 {
10574 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10575 args, tf_warning_or_error);
10576 location_t saved_input_location = input_location;
10577 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10578 tree cons = get_constraints (tmpl);
10579 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10580 input_location = saved_input_location;
10581 }
10582 }
10583 else
10584 {
10585 /* The friend template has not already been declared. In this
10586 case, the instantiation of the template class will cause the
10587 injection of this template into the namespace scope. */
10588 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10589
10590 if (tmpl != error_mark_node)
10591 {
10592 /* The new TMPL is not an instantiation of anything, so we
10593 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10594 for the new type because that is supposed to be the
10595 corresponding template decl, i.e., TMPL. */
10596 DECL_USE_TEMPLATE (tmpl) = 0;
10597 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10598 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10599 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10600 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10601
10602 /* It is hidden. */
10603 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10604 DECL_ANTICIPATED (tmpl)
10605 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10606
10607 /* Inject this template into the enclosing namspace scope. */
10608 tmpl = pushdecl_namespace_level (tmpl, true);
10609 }
10610 }
10611
10612 if (TREE_CODE (context) == NAMESPACE_DECL)
10613 pop_nested_namespace (context);
10614 else
10615 pop_nested_class ();
10616
10617 return TREE_TYPE (tmpl);
10618 }
10619
10620 /* Returns zero if TYPE cannot be completed later due to circularity.
10621 Otherwise returns one. */
10622
10623 static int
10624 can_complete_type_without_circularity (tree type)
10625 {
10626 if (type == NULL_TREE || type == error_mark_node)
10627 return 0;
10628 else if (COMPLETE_TYPE_P (type))
10629 return 1;
10630 else if (TREE_CODE (type) == ARRAY_TYPE)
10631 return can_complete_type_without_circularity (TREE_TYPE (type));
10632 else if (CLASS_TYPE_P (type)
10633 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10634 return 0;
10635 else
10636 return 1;
10637 }
10638
10639 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10640 tsubst_flags_t, tree);
10641
10642 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10643 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10644
10645 static tree
10646 tsubst_attribute (tree t, tree *decl_p, tree args,
10647 tsubst_flags_t complain, tree in_decl)
10648 {
10649 gcc_assert (ATTR_IS_DEPENDENT (t));
10650
10651 tree val = TREE_VALUE (t);
10652 if (val == NULL_TREE)
10653 /* Nothing to do. */;
10654 else if ((flag_openmp || flag_openmp_simd)
10655 && is_attribute_p ("omp declare simd",
10656 get_attribute_name (t)))
10657 {
10658 tree clauses = TREE_VALUE (val);
10659 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10660 complain, in_decl);
10661 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10662 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10663 tree parms = DECL_ARGUMENTS (*decl_p);
10664 clauses
10665 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10666 if (clauses)
10667 val = build_tree_list (NULL_TREE, clauses);
10668 else
10669 val = NULL_TREE;
10670 }
10671 /* If the first attribute argument is an identifier, don't
10672 pass it through tsubst. Attributes like mode, format,
10673 cleanup and several target specific attributes expect it
10674 unmodified. */
10675 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10676 {
10677 tree chain
10678 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10679 /*integral_constant_expression_p=*/false);
10680 if (chain != TREE_CHAIN (val))
10681 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10682 }
10683 else if (PACK_EXPANSION_P (val))
10684 {
10685 /* An attribute pack expansion. */
10686 tree purp = TREE_PURPOSE (t);
10687 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10688 if (pack == error_mark_node)
10689 return error_mark_node;
10690 int len = TREE_VEC_LENGTH (pack);
10691 tree list = NULL_TREE;
10692 tree *q = &list;
10693 for (int i = 0; i < len; ++i)
10694 {
10695 tree elt = TREE_VEC_ELT (pack, i);
10696 *q = build_tree_list (purp, elt);
10697 q = &TREE_CHAIN (*q);
10698 }
10699 return list;
10700 }
10701 else
10702 val = tsubst_expr (val, args, complain, in_decl,
10703 /*integral_constant_expression_p=*/false);
10704
10705 if (val != TREE_VALUE (t))
10706 return build_tree_list (TREE_PURPOSE (t), val);
10707 return t;
10708 }
10709
10710 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10711 unchanged or a new TREE_LIST chain. */
10712
10713 static tree
10714 tsubst_attributes (tree attributes, tree args,
10715 tsubst_flags_t complain, tree in_decl)
10716 {
10717 tree last_dep = NULL_TREE;
10718
10719 for (tree t = attributes; t; t = TREE_CHAIN (t))
10720 if (ATTR_IS_DEPENDENT (t))
10721 {
10722 last_dep = t;
10723 attributes = copy_list (attributes);
10724 break;
10725 }
10726
10727 if (last_dep)
10728 for (tree *p = &attributes; *p; )
10729 {
10730 tree t = *p;
10731 if (ATTR_IS_DEPENDENT (t))
10732 {
10733 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10734 if (subst != t)
10735 {
10736 *p = subst;
10737 while (*p)
10738 p = &TREE_CHAIN (*p);
10739 *p = TREE_CHAIN (t);
10740 continue;
10741 }
10742 }
10743 p = &TREE_CHAIN (*p);
10744 }
10745
10746 return attributes;
10747 }
10748
10749 /* Apply any attributes which had to be deferred until instantiation
10750 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10751 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10752
10753 static void
10754 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10755 tree args, tsubst_flags_t complain, tree in_decl)
10756 {
10757 tree last_dep = NULL_TREE;
10758 tree t;
10759 tree *p;
10760
10761 if (attributes == NULL_TREE)
10762 return;
10763
10764 if (DECL_P (*decl_p))
10765 {
10766 if (TREE_TYPE (*decl_p) == error_mark_node)
10767 return;
10768 p = &DECL_ATTRIBUTES (*decl_p);
10769 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10770 to our attributes parameter. */
10771 gcc_assert (*p == attributes);
10772 }
10773 else
10774 {
10775 p = &TYPE_ATTRIBUTES (*decl_p);
10776 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10777 lookup_template_class_1, and should be preserved. */
10778 gcc_assert (*p != attributes);
10779 while (*p)
10780 p = &TREE_CHAIN (*p);
10781 }
10782
10783 for (t = attributes; t; t = TREE_CHAIN (t))
10784 if (ATTR_IS_DEPENDENT (t))
10785 {
10786 last_dep = t;
10787 attributes = copy_list (attributes);
10788 break;
10789 }
10790
10791 *p = attributes;
10792 if (last_dep)
10793 {
10794 tree late_attrs = NULL_TREE;
10795 tree *q = &late_attrs;
10796
10797 for (; *p; )
10798 {
10799 t = *p;
10800 if (ATTR_IS_DEPENDENT (t))
10801 {
10802 *p = TREE_CHAIN (t);
10803 TREE_CHAIN (t) = NULL_TREE;
10804 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10805 while (*q)
10806 q = &TREE_CHAIN (*q);
10807 }
10808 else
10809 p = &TREE_CHAIN (t);
10810 }
10811
10812 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10813 }
10814 }
10815
10816 /* Perform (or defer) access check for typedefs that were referenced
10817 from within the template TMPL code.
10818 This is a subroutine of instantiate_decl and instantiate_class_template.
10819 TMPL is the template to consider and TARGS is the list of arguments of
10820 that template. */
10821
10822 static void
10823 perform_typedefs_access_check (tree tmpl, tree targs)
10824 {
10825 location_t saved_location;
10826 unsigned i;
10827 qualified_typedef_usage_t *iter;
10828
10829 if (!tmpl
10830 || (!CLASS_TYPE_P (tmpl)
10831 && TREE_CODE (tmpl) != FUNCTION_DECL))
10832 return;
10833
10834 saved_location = input_location;
10835 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10836 {
10837 tree type_decl = iter->typedef_decl;
10838 tree type_scope = iter->context;
10839
10840 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10841 continue;
10842
10843 if (uses_template_parms (type_decl))
10844 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10845 if (uses_template_parms (type_scope))
10846 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10847
10848 /* Make access check error messages point to the location
10849 of the use of the typedef. */
10850 input_location = iter->locus;
10851 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10852 type_decl, type_decl,
10853 tf_warning_or_error);
10854 }
10855 input_location = saved_location;
10856 }
10857
10858 static tree
10859 instantiate_class_template_1 (tree type)
10860 {
10861 tree templ, args, pattern, t, member;
10862 tree typedecl;
10863 tree pbinfo;
10864 tree base_list;
10865 unsigned int saved_maximum_field_alignment;
10866 tree fn_context;
10867
10868 if (type == error_mark_node)
10869 return error_mark_node;
10870
10871 if (COMPLETE_OR_OPEN_TYPE_P (type)
10872 || uses_template_parms (type))
10873 return type;
10874
10875 /* Figure out which template is being instantiated. */
10876 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10877 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10878
10879 /* Mark the type as in the process of being defined. */
10880 TYPE_BEING_DEFINED (type) = 1;
10881
10882 /* We may be in the middle of deferred access check. Disable
10883 it now. */
10884 deferring_access_check_sentinel acs (dk_no_deferred);
10885
10886 /* Determine what specialization of the original template to
10887 instantiate. */
10888 t = most_specialized_partial_spec (type, tf_warning_or_error);
10889 if (t == error_mark_node)
10890 return error_mark_node;
10891 else if (t)
10892 {
10893 /* This TYPE is actually an instantiation of a partial
10894 specialization. We replace the innermost set of ARGS with
10895 the arguments appropriate for substitution. For example,
10896 given:
10897
10898 template <class T> struct S {};
10899 template <class T> struct S<T*> {};
10900
10901 and supposing that we are instantiating S<int*>, ARGS will
10902 presently be {int*} -- but we need {int}. */
10903 pattern = TREE_TYPE (t);
10904 args = TREE_PURPOSE (t);
10905 }
10906 else
10907 {
10908 pattern = TREE_TYPE (templ);
10909 args = CLASSTYPE_TI_ARGS (type);
10910 }
10911
10912 /* If the template we're instantiating is incomplete, then clearly
10913 there's nothing we can do. */
10914 if (!COMPLETE_TYPE_P (pattern))
10915 {
10916 /* We can try again later. */
10917 TYPE_BEING_DEFINED (type) = 0;
10918 return type;
10919 }
10920
10921 /* If we've recursively instantiated too many templates, stop. */
10922 if (! push_tinst_level (type))
10923 return type;
10924
10925 int saved_unevaluated_operand = cp_unevaluated_operand;
10926 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10927
10928 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10929 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10930 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10931 fn_context = error_mark_node;
10932 if (!fn_context)
10933 push_to_top_level ();
10934 else
10935 {
10936 cp_unevaluated_operand = 0;
10937 c_inhibit_evaluation_warnings = 0;
10938 }
10939 /* Use #pragma pack from the template context. */
10940 saved_maximum_field_alignment = maximum_field_alignment;
10941 maximum_field_alignment = TYPE_PRECISION (pattern);
10942
10943 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10944
10945 /* Set the input location to the most specialized template definition.
10946 This is needed if tsubsting causes an error. */
10947 typedecl = TYPE_MAIN_DECL (pattern);
10948 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10949 DECL_SOURCE_LOCATION (typedecl);
10950
10951 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10952 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10953 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10954 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10955 if (ANON_AGGR_TYPE_P (pattern))
10956 SET_ANON_AGGR_TYPE_P (type);
10957 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10958 {
10959 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10960 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10961 /* Adjust visibility for template arguments. */
10962 determine_visibility (TYPE_MAIN_DECL (type));
10963 }
10964 if (CLASS_TYPE_P (type))
10965 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10966
10967 pbinfo = TYPE_BINFO (pattern);
10968
10969 /* We should never instantiate a nested class before its enclosing
10970 class; we need to look up the nested class by name before we can
10971 instantiate it, and that lookup should instantiate the enclosing
10972 class. */
10973 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10974 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10975
10976 base_list = NULL_TREE;
10977 if (BINFO_N_BASE_BINFOS (pbinfo))
10978 {
10979 tree pbase_binfo;
10980 tree pushed_scope;
10981 int i;
10982
10983 /* We must enter the scope containing the type, as that is where
10984 the accessibility of types named in dependent bases are
10985 looked up from. */
10986 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10987
10988 /* Substitute into each of the bases to determine the actual
10989 basetypes. */
10990 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10991 {
10992 tree base;
10993 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10994 tree expanded_bases = NULL_TREE;
10995 int idx, len = 1;
10996
10997 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10998 {
10999 expanded_bases =
11000 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11001 args, tf_error, NULL_TREE);
11002 if (expanded_bases == error_mark_node)
11003 continue;
11004
11005 len = TREE_VEC_LENGTH (expanded_bases);
11006 }
11007
11008 for (idx = 0; idx < len; idx++)
11009 {
11010 if (expanded_bases)
11011 /* Extract the already-expanded base class. */
11012 base = TREE_VEC_ELT (expanded_bases, idx);
11013 else
11014 /* Substitute to figure out the base class. */
11015 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11016 NULL_TREE);
11017
11018 if (base == error_mark_node)
11019 continue;
11020
11021 base_list = tree_cons (access, base, base_list);
11022 if (BINFO_VIRTUAL_P (pbase_binfo))
11023 TREE_TYPE (base_list) = integer_type_node;
11024 }
11025 }
11026
11027 /* The list is now in reverse order; correct that. */
11028 base_list = nreverse (base_list);
11029
11030 if (pushed_scope)
11031 pop_scope (pushed_scope);
11032 }
11033 /* Now call xref_basetypes to set up all the base-class
11034 information. */
11035 xref_basetypes (type, base_list);
11036
11037 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11038 (int) ATTR_FLAG_TYPE_IN_PLACE,
11039 args, tf_error, NULL_TREE);
11040 fixup_attribute_variants (type);
11041
11042 /* Now that our base classes are set up, enter the scope of the
11043 class, so that name lookups into base classes, etc. will work
11044 correctly. This is precisely analogous to what we do in
11045 begin_class_definition when defining an ordinary non-template
11046 class, except we also need to push the enclosing classes. */
11047 push_nested_class (type);
11048
11049 /* Now members are processed in the order of declaration. */
11050 for (member = CLASSTYPE_DECL_LIST (pattern);
11051 member; member = TREE_CHAIN (member))
11052 {
11053 tree t = TREE_VALUE (member);
11054
11055 if (TREE_PURPOSE (member))
11056 {
11057 if (TYPE_P (t))
11058 {
11059 if (LAMBDA_TYPE_P (t))
11060 /* A closure type for a lambda in an NSDMI or default argument.
11061 Ignore it; it will be regenerated when needed. */
11062 continue;
11063
11064 /* Build new CLASSTYPE_NESTED_UTDS. */
11065
11066 tree newtag;
11067 bool class_template_p;
11068
11069 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11070 && TYPE_LANG_SPECIFIC (t)
11071 && CLASSTYPE_IS_TEMPLATE (t));
11072 /* If the member is a class template, then -- even after
11073 substitution -- there may be dependent types in the
11074 template argument list for the class. We increment
11075 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11076 that function will assume that no types are dependent
11077 when outside of a template. */
11078 if (class_template_p)
11079 ++processing_template_decl;
11080 newtag = tsubst (t, args, tf_error, NULL_TREE);
11081 if (class_template_p)
11082 --processing_template_decl;
11083 if (newtag == error_mark_node)
11084 continue;
11085
11086 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11087 {
11088 tree name = TYPE_IDENTIFIER (t);
11089
11090 if (class_template_p)
11091 /* Unfortunately, lookup_template_class sets
11092 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11093 instantiation (i.e., for the type of a member
11094 template class nested within a template class.)
11095 This behavior is required for
11096 maybe_process_partial_specialization to work
11097 correctly, but is not accurate in this case;
11098 the TAG is not an instantiation of anything.
11099 (The corresponding TEMPLATE_DECL is an
11100 instantiation, but the TYPE is not.) */
11101 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11102
11103 /* Now, we call pushtag to put this NEWTAG into the scope of
11104 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11105 pushtag calling push_template_decl. We don't have to do
11106 this for enums because it will already have been done in
11107 tsubst_enum. */
11108 if (name)
11109 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11110 pushtag (name, newtag, /*tag_scope=*/ts_current);
11111 }
11112 }
11113 else if (DECL_DECLARES_FUNCTION_P (t))
11114 {
11115 tree r;
11116
11117 if (TREE_CODE (t) == TEMPLATE_DECL)
11118 ++processing_template_decl;
11119 r = tsubst (t, args, tf_error, NULL_TREE);
11120 if (TREE_CODE (t) == TEMPLATE_DECL)
11121 --processing_template_decl;
11122 set_current_access_from_decl (r);
11123 finish_member_declaration (r);
11124 /* Instantiate members marked with attribute used. */
11125 if (r != error_mark_node && DECL_PRESERVE_P (r))
11126 mark_used (r);
11127 if (TREE_CODE (r) == FUNCTION_DECL
11128 && DECL_OMP_DECLARE_REDUCTION_P (r))
11129 cp_check_omp_declare_reduction (r);
11130 }
11131 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11132 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11133 /* A closure type for a lambda in an NSDMI or default argument.
11134 Ignore it; it will be regenerated when needed. */;
11135 else
11136 {
11137 /* Build new TYPE_FIELDS. */
11138 if (TREE_CODE (t) == STATIC_ASSERT)
11139 {
11140 tree condition;
11141
11142 ++c_inhibit_evaluation_warnings;
11143 condition =
11144 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11145 tf_warning_or_error, NULL_TREE,
11146 /*integral_constant_expression_p=*/true);
11147 --c_inhibit_evaluation_warnings;
11148
11149 finish_static_assert (condition,
11150 STATIC_ASSERT_MESSAGE (t),
11151 STATIC_ASSERT_SOURCE_LOCATION (t),
11152 /*member_p=*/true);
11153 }
11154 else if (TREE_CODE (t) != CONST_DECL)
11155 {
11156 tree r;
11157 tree vec = NULL_TREE;
11158 int len = 1;
11159
11160 /* The file and line for this declaration, to
11161 assist in error message reporting. Since we
11162 called push_tinst_level above, we don't need to
11163 restore these. */
11164 input_location = DECL_SOURCE_LOCATION (t);
11165
11166 if (TREE_CODE (t) == TEMPLATE_DECL)
11167 ++processing_template_decl;
11168 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11169 if (TREE_CODE (t) == TEMPLATE_DECL)
11170 --processing_template_decl;
11171
11172 if (TREE_CODE (r) == TREE_VEC)
11173 {
11174 /* A capture pack became multiple fields. */
11175 vec = r;
11176 len = TREE_VEC_LENGTH (vec);
11177 }
11178
11179 for (int i = 0; i < len; ++i)
11180 {
11181 if (vec)
11182 r = TREE_VEC_ELT (vec, i);
11183 if (VAR_P (r))
11184 {
11185 /* In [temp.inst]:
11186
11187 [t]he initialization (and any associated
11188 side-effects) of a static data member does
11189 not occur unless the static data member is
11190 itself used in a way that requires the
11191 definition of the static data member to
11192 exist.
11193
11194 Therefore, we do not substitute into the
11195 initialized for the static data member here. */
11196 finish_static_data_member_decl
11197 (r,
11198 /*init=*/NULL_TREE,
11199 /*init_const_expr_p=*/false,
11200 /*asmspec_tree=*/NULL_TREE,
11201 /*flags=*/0);
11202 /* Instantiate members marked with attribute used. */
11203 if (r != error_mark_node && DECL_PRESERVE_P (r))
11204 mark_used (r);
11205 }
11206 else if (TREE_CODE (r) == FIELD_DECL)
11207 {
11208 /* Determine whether R has a valid type and can be
11209 completed later. If R is invalid, then its type
11210 is replaced by error_mark_node. */
11211 tree rtype = TREE_TYPE (r);
11212 if (can_complete_type_without_circularity (rtype))
11213 complete_type (rtype);
11214
11215 if (!complete_or_array_type_p (rtype))
11216 {
11217 /* If R's type couldn't be completed and
11218 it isn't a flexible array member (whose
11219 type is incomplete by definition) give
11220 an error. */
11221 cxx_incomplete_type_error (r, rtype);
11222 TREE_TYPE (r) = error_mark_node;
11223 }
11224 else if (TREE_CODE (rtype) == ARRAY_TYPE
11225 && TYPE_DOMAIN (rtype) == NULL_TREE
11226 && (TREE_CODE (type) == UNION_TYPE
11227 || TREE_CODE (type) == QUAL_UNION_TYPE))
11228 {
11229 error ("flexible array member %qD in union", r);
11230 TREE_TYPE (r) = error_mark_node;
11231 }
11232 }
11233
11234 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11235 such a thing will already have been added to the field
11236 list by tsubst_enum in finish_member_declaration in the
11237 CLASSTYPE_NESTED_UTDS case above. */
11238 if (!(TREE_CODE (r) == TYPE_DECL
11239 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11240 && DECL_ARTIFICIAL (r)))
11241 {
11242 set_current_access_from_decl (r);
11243 finish_member_declaration (r);
11244 }
11245 }
11246 }
11247 }
11248 }
11249 else
11250 {
11251 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11252 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11253 {
11254 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11255
11256 tree friend_type = t;
11257 bool adjust_processing_template_decl = false;
11258
11259 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11260 {
11261 /* template <class T> friend class C; */
11262 friend_type = tsubst_friend_class (friend_type, args);
11263 adjust_processing_template_decl = true;
11264 }
11265 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11266 {
11267 /* template <class T> friend class C::D; */
11268 friend_type = tsubst (friend_type, args,
11269 tf_warning_or_error, NULL_TREE);
11270 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11271 friend_type = TREE_TYPE (friend_type);
11272 adjust_processing_template_decl = true;
11273 }
11274 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11275 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11276 {
11277 /* This could be either
11278
11279 friend class T::C;
11280
11281 when dependent_type_p is false or
11282
11283 template <class U> friend class T::C;
11284
11285 otherwise. */
11286 /* Bump processing_template_decl in case this is something like
11287 template <class T> friend struct A<T>::B. */
11288 ++processing_template_decl;
11289 friend_type = tsubst (friend_type, args,
11290 tf_warning_or_error, NULL_TREE);
11291 if (dependent_type_p (friend_type))
11292 adjust_processing_template_decl = true;
11293 --processing_template_decl;
11294 }
11295 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11296 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11297 && TYPE_HIDDEN_P (friend_type))
11298 {
11299 /* friend class C;
11300
11301 where C hasn't been declared yet. Let's lookup name
11302 from namespace scope directly, bypassing any name that
11303 come from dependent base class. */
11304 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11305
11306 /* The call to xref_tag_from_type does injection for friend
11307 classes. */
11308 push_nested_namespace (ns);
11309 friend_type =
11310 xref_tag_from_type (friend_type, NULL_TREE,
11311 /*tag_scope=*/ts_current);
11312 pop_nested_namespace (ns);
11313 }
11314 else if (uses_template_parms (friend_type))
11315 /* friend class C<T>; */
11316 friend_type = tsubst (friend_type, args,
11317 tf_warning_or_error, NULL_TREE);
11318 /* Otherwise it's
11319
11320 friend class C;
11321
11322 where C is already declared or
11323
11324 friend class C<int>;
11325
11326 We don't have to do anything in these cases. */
11327
11328 if (adjust_processing_template_decl)
11329 /* Trick make_friend_class into realizing that the friend
11330 we're adding is a template, not an ordinary class. It's
11331 important that we use make_friend_class since it will
11332 perform some error-checking and output cross-reference
11333 information. */
11334 ++processing_template_decl;
11335
11336 if (friend_type != error_mark_node)
11337 make_friend_class (type, friend_type, /*complain=*/false);
11338
11339 if (adjust_processing_template_decl)
11340 --processing_template_decl;
11341 }
11342 else
11343 {
11344 /* Build new DECL_FRIENDLIST. */
11345 tree r;
11346
11347 /* The file and line for this declaration, to
11348 assist in error message reporting. Since we
11349 called push_tinst_level above, we don't need to
11350 restore these. */
11351 input_location = DECL_SOURCE_LOCATION (t);
11352
11353 if (TREE_CODE (t) == TEMPLATE_DECL)
11354 {
11355 ++processing_template_decl;
11356 push_deferring_access_checks (dk_no_check);
11357 }
11358
11359 r = tsubst_friend_function (t, args);
11360 add_friend (type, r, /*complain=*/false);
11361 if (TREE_CODE (t) == TEMPLATE_DECL)
11362 {
11363 pop_deferring_access_checks ();
11364 --processing_template_decl;
11365 }
11366 }
11367 }
11368 }
11369
11370 if (fn_context)
11371 {
11372 /* Restore these before substituting into the lambda capture
11373 initializers. */
11374 cp_unevaluated_operand = saved_unevaluated_operand;
11375 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11376 }
11377
11378 /* Set the file and line number information to whatever is given for
11379 the class itself. This puts error messages involving generated
11380 implicit functions at a predictable point, and the same point
11381 that would be used for non-template classes. */
11382 input_location = DECL_SOURCE_LOCATION (typedecl);
11383
11384 unreverse_member_declarations (type);
11385 finish_struct_1 (type);
11386 TYPE_BEING_DEFINED (type) = 0;
11387
11388 /* We don't instantiate default arguments for member functions. 14.7.1:
11389
11390 The implicit instantiation of a class template specialization causes
11391 the implicit instantiation of the declarations, but not of the
11392 definitions or default arguments, of the class member functions,
11393 member classes, static data members and member templates.... */
11394
11395 /* Some typedefs referenced from within the template code need to be access
11396 checked at template instantiation time, i.e now. These types were
11397 added to the template at parsing time. Let's get those and perform
11398 the access checks then. */
11399 perform_typedefs_access_check (pattern, args);
11400 perform_deferred_access_checks (tf_warning_or_error);
11401 pop_nested_class ();
11402 maximum_field_alignment = saved_maximum_field_alignment;
11403 if (!fn_context)
11404 pop_from_top_level ();
11405 pop_tinst_level ();
11406
11407 /* The vtable for a template class can be emitted in any translation
11408 unit in which the class is instantiated. When there is no key
11409 method, however, finish_struct_1 will already have added TYPE to
11410 the keyed_classes. */
11411 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11412 vec_safe_push (keyed_classes, type);
11413
11414 return type;
11415 }
11416
11417 /* Wrapper for instantiate_class_template_1. */
11418
11419 tree
11420 instantiate_class_template (tree type)
11421 {
11422 tree ret;
11423 timevar_push (TV_TEMPLATE_INST);
11424 ret = instantiate_class_template_1 (type);
11425 timevar_pop (TV_TEMPLATE_INST);
11426 return ret;
11427 }
11428
11429 static tree
11430 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11431 {
11432 tree r;
11433
11434 if (!t)
11435 r = t;
11436 else if (TYPE_P (t))
11437 r = tsubst (t, args, complain, in_decl);
11438 else
11439 {
11440 if (!(complain & tf_warning))
11441 ++c_inhibit_evaluation_warnings;
11442 r = tsubst_expr (t, args, complain, in_decl,
11443 /*integral_constant_expression_p=*/true);
11444 if (!(complain & tf_warning))
11445 --c_inhibit_evaluation_warnings;
11446 }
11447 return r;
11448 }
11449
11450 /* Given a function parameter pack TMPL_PARM and some function parameters
11451 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11452 and set *SPEC_P to point at the next point in the list. */
11453
11454 tree
11455 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11456 {
11457 /* Collect all of the extra "packed" parameters into an
11458 argument pack. */
11459 tree parmvec;
11460 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11461 tree spec_parm = *spec_p;
11462 int i, len;
11463
11464 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11465 if (tmpl_parm
11466 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11467 break;
11468
11469 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11470 parmvec = make_tree_vec (len);
11471 spec_parm = *spec_p;
11472 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11473 {
11474 tree elt = spec_parm;
11475 if (DECL_PACK_P (elt))
11476 elt = make_pack_expansion (elt);
11477 TREE_VEC_ELT (parmvec, i) = elt;
11478 }
11479
11480 /* Build the argument packs. */
11481 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11482 *spec_p = spec_parm;
11483
11484 return argpack;
11485 }
11486
11487 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11488 NONTYPE_ARGUMENT_PACK. */
11489
11490 static tree
11491 make_fnparm_pack (tree spec_parm)
11492 {
11493 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11494 }
11495
11496 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11497 pack expansion with no extra args, 2 if it has extra args, or 0
11498 if it is not a pack expansion. */
11499
11500 static int
11501 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11502 {
11503 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11504 if (i >= TREE_VEC_LENGTH (vec))
11505 return 0;
11506 tree elt = TREE_VEC_ELT (vec, i);
11507 if (DECL_P (elt))
11508 /* A decl pack is itself an expansion. */
11509 elt = TREE_TYPE (elt);
11510 if (!PACK_EXPANSION_P (elt))
11511 return 0;
11512 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11513 return 2;
11514 return 1;
11515 }
11516
11517
11518 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11519
11520 static tree
11521 make_argument_pack_select (tree arg_pack, unsigned index)
11522 {
11523 tree aps = make_node (ARGUMENT_PACK_SELECT);
11524
11525 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11526 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11527
11528 return aps;
11529 }
11530
11531 /* This is a subroutine of tsubst_pack_expansion.
11532
11533 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11534 mechanism to store the (non complete list of) arguments of the
11535 substitution and return a non substituted pack expansion, in order
11536 to wait for when we have enough arguments to really perform the
11537 substitution. */
11538
11539 static bool
11540 use_pack_expansion_extra_args_p (tree parm_packs,
11541 int arg_pack_len,
11542 bool has_empty_arg)
11543 {
11544 /* If one pack has an expansion and another pack has a normal
11545 argument or if one pack has an empty argument and an another
11546 one hasn't then tsubst_pack_expansion cannot perform the
11547 substitution and need to fall back on the
11548 PACK_EXPANSION_EXTRA mechanism. */
11549 if (parm_packs == NULL_TREE)
11550 return false;
11551 else if (has_empty_arg)
11552 return true;
11553
11554 bool has_expansion_arg = false;
11555 for (int i = 0 ; i < arg_pack_len; ++i)
11556 {
11557 bool has_non_expansion_arg = false;
11558 for (tree parm_pack = parm_packs;
11559 parm_pack;
11560 parm_pack = TREE_CHAIN (parm_pack))
11561 {
11562 tree arg = TREE_VALUE (parm_pack);
11563
11564 int exp = argument_pack_element_is_expansion_p (arg, i);
11565 if (exp == 2)
11566 /* We can't substitute a pack expansion with extra args into
11567 our pattern. */
11568 return true;
11569 else if (exp)
11570 has_expansion_arg = true;
11571 else
11572 has_non_expansion_arg = true;
11573 }
11574
11575 if (has_expansion_arg && has_non_expansion_arg)
11576 return true;
11577 }
11578 return false;
11579 }
11580
11581 /* [temp.variadic]/6 says that:
11582
11583 The instantiation of a pack expansion [...]
11584 produces a list E1,E2, ..., En, where N is the number of elements
11585 in the pack expansion parameters.
11586
11587 This subroutine of tsubst_pack_expansion produces one of these Ei.
11588
11589 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11590 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11591 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11592 INDEX is the index 'i' of the element Ei to produce. ARGS,
11593 COMPLAIN, and IN_DECL are the same parameters as for the
11594 tsubst_pack_expansion function.
11595
11596 The function returns the resulting Ei upon successful completion,
11597 or error_mark_node.
11598
11599 Note that this function possibly modifies the ARGS parameter, so
11600 it's the responsibility of the caller to restore it. */
11601
11602 static tree
11603 gen_elem_of_pack_expansion_instantiation (tree pattern,
11604 tree parm_packs,
11605 unsigned index,
11606 tree args /* This parm gets
11607 modified. */,
11608 tsubst_flags_t complain,
11609 tree in_decl)
11610 {
11611 tree t;
11612 bool ith_elem_is_expansion = false;
11613
11614 /* For each parameter pack, change the substitution of the parameter
11615 pack to the ith argument in its argument pack, then expand the
11616 pattern. */
11617 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11618 {
11619 tree parm = TREE_PURPOSE (pack);
11620 tree arg_pack = TREE_VALUE (pack);
11621 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11622
11623 ith_elem_is_expansion |=
11624 argument_pack_element_is_expansion_p (arg_pack, index);
11625
11626 /* Select the Ith argument from the pack. */
11627 if (TREE_CODE (parm) == PARM_DECL
11628 || VAR_P (parm)
11629 || TREE_CODE (parm) == FIELD_DECL)
11630 {
11631 if (index == 0)
11632 {
11633 aps = make_argument_pack_select (arg_pack, index);
11634 if (!mark_used (parm, complain) && !(complain & tf_error))
11635 return error_mark_node;
11636 register_local_specialization (aps, parm);
11637 }
11638 else
11639 aps = retrieve_local_specialization (parm);
11640 }
11641 else
11642 {
11643 int idx, level;
11644 template_parm_level_and_index (parm, &level, &idx);
11645
11646 if (index == 0)
11647 {
11648 aps = make_argument_pack_select (arg_pack, index);
11649 /* Update the corresponding argument. */
11650 TMPL_ARG (args, level, idx) = aps;
11651 }
11652 else
11653 /* Re-use the ARGUMENT_PACK_SELECT. */
11654 aps = TMPL_ARG (args, level, idx);
11655 }
11656 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11657 }
11658
11659 /* Substitute into the PATTERN with the (possibly altered)
11660 arguments. */
11661 if (pattern == in_decl)
11662 /* Expanding a fixed parameter pack from
11663 coerce_template_parameter_pack. */
11664 t = tsubst_decl (pattern, args, complain);
11665 else if (pattern == error_mark_node)
11666 t = error_mark_node;
11667 else if (constraint_p (pattern))
11668 {
11669 if (processing_template_decl)
11670 t = tsubst_constraint (pattern, args, complain, in_decl);
11671 else
11672 t = (constraints_satisfied_p (pattern, args)
11673 ? boolean_true_node : boolean_false_node);
11674 }
11675 else if (!TYPE_P (pattern))
11676 t = tsubst_expr (pattern, args, complain, in_decl,
11677 /*integral_constant_expression_p=*/false);
11678 else
11679 t = tsubst (pattern, args, complain, in_decl);
11680
11681 /* If the Ith argument pack element is a pack expansion, then
11682 the Ith element resulting from the substituting is going to
11683 be a pack expansion as well. */
11684 if (ith_elem_is_expansion)
11685 t = make_pack_expansion (t, complain);
11686
11687 return t;
11688 }
11689
11690 /* When the unexpanded parameter pack in a fold expression expands to an empty
11691 sequence, the value of the expression is as follows; the program is
11692 ill-formed if the operator is not listed in this table.
11693
11694 && true
11695 || false
11696 , void() */
11697
11698 tree
11699 expand_empty_fold (tree t, tsubst_flags_t complain)
11700 {
11701 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11702 if (!FOLD_EXPR_MODIFY_P (t))
11703 switch (code)
11704 {
11705 case TRUTH_ANDIF_EXPR:
11706 return boolean_true_node;
11707 case TRUTH_ORIF_EXPR:
11708 return boolean_false_node;
11709 case COMPOUND_EXPR:
11710 return void_node;
11711 default:
11712 break;
11713 }
11714
11715 if (complain & tf_error)
11716 error_at (location_of (t),
11717 "fold of empty expansion over %O", code);
11718 return error_mark_node;
11719 }
11720
11721 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11722 form an expression that combines the two terms using the
11723 operator of T. */
11724
11725 static tree
11726 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11727 {
11728 tree op = FOLD_EXPR_OP (t);
11729 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11730
11731 // Handle compound assignment operators.
11732 if (FOLD_EXPR_MODIFY_P (t))
11733 return build_x_modify_expr (input_location, left, code, right, complain);
11734
11735 switch (code)
11736 {
11737 case COMPOUND_EXPR:
11738 return build_x_compound_expr (input_location, left, right, complain);
11739 case DOTSTAR_EXPR:
11740 return build_m_component_ref (left, right, complain);
11741 default:
11742 return build_x_binary_op (input_location, code,
11743 left, TREE_CODE (left),
11744 right, TREE_CODE (right),
11745 /*overload=*/NULL,
11746 complain);
11747 }
11748 }
11749
11750 /* Substitute ARGS into the pack of a fold expression T. */
11751
11752 static inline tree
11753 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11754 {
11755 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11756 }
11757
11758 /* Substitute ARGS into the pack of a fold expression T. */
11759
11760 static inline tree
11761 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11762 {
11763 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11764 }
11765
11766 /* Expand a PACK of arguments into a grouped as left fold.
11767 Given a pack containing elements A0, A1, ..., An and an
11768 operator @, this builds the expression:
11769
11770 ((A0 @ A1) @ A2) ... @ An
11771
11772 Note that PACK must not be empty.
11773
11774 The operator is defined by the original fold expression T. */
11775
11776 static tree
11777 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11778 {
11779 tree left = TREE_VEC_ELT (pack, 0);
11780 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11781 {
11782 tree right = TREE_VEC_ELT (pack, i);
11783 left = fold_expression (t, left, right, complain);
11784 }
11785 return left;
11786 }
11787
11788 /* Substitute into a unary left fold expression. */
11789
11790 static tree
11791 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11792 tree in_decl)
11793 {
11794 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11795 if (pack == error_mark_node)
11796 return error_mark_node;
11797 if (PACK_EXPANSION_P (pack))
11798 {
11799 tree r = copy_node (t);
11800 FOLD_EXPR_PACK (r) = pack;
11801 return r;
11802 }
11803 if (TREE_VEC_LENGTH (pack) == 0)
11804 return expand_empty_fold (t, complain);
11805 else
11806 return expand_left_fold (t, pack, complain);
11807 }
11808
11809 /* Substitute into a binary left fold expression.
11810
11811 Do ths by building a single (non-empty) vector of argumnts and
11812 building the expression from those elements. */
11813
11814 static tree
11815 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11816 tree in_decl)
11817 {
11818 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11819 if (pack == error_mark_node)
11820 return error_mark_node;
11821 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11822 if (init == error_mark_node)
11823 return error_mark_node;
11824
11825 if (PACK_EXPANSION_P (pack))
11826 {
11827 tree r = copy_node (t);
11828 FOLD_EXPR_PACK (r) = pack;
11829 FOLD_EXPR_INIT (r) = init;
11830 return r;
11831 }
11832
11833 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11834 TREE_VEC_ELT (vec, 0) = init;
11835 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11836 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11837
11838 return expand_left_fold (t, vec, complain);
11839 }
11840
11841 /* Expand a PACK of arguments into a grouped as right fold.
11842 Given a pack containing elementns A0, A1, ..., and an
11843 operator @, this builds the expression:
11844
11845 A0@ ... (An-2 @ (An-1 @ An))
11846
11847 Note that PACK must not be empty.
11848
11849 The operator is defined by the original fold expression T. */
11850
11851 tree
11852 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11853 {
11854 // Build the expression.
11855 int n = TREE_VEC_LENGTH (pack);
11856 tree right = TREE_VEC_ELT (pack, n - 1);
11857 for (--n; n != 0; --n)
11858 {
11859 tree left = TREE_VEC_ELT (pack, n - 1);
11860 right = fold_expression (t, left, right, complain);
11861 }
11862 return right;
11863 }
11864
11865 /* Substitute into a unary right fold expression. */
11866
11867 static tree
11868 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11869 tree in_decl)
11870 {
11871 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11872 if (pack == error_mark_node)
11873 return error_mark_node;
11874 if (PACK_EXPANSION_P (pack))
11875 {
11876 tree r = copy_node (t);
11877 FOLD_EXPR_PACK (r) = pack;
11878 return r;
11879 }
11880 if (TREE_VEC_LENGTH (pack) == 0)
11881 return expand_empty_fold (t, complain);
11882 else
11883 return expand_right_fold (t, pack, complain);
11884 }
11885
11886 /* Substitute into a binary right fold expression.
11887
11888 Do ths by building a single (non-empty) vector of arguments and
11889 building the expression from those elements. */
11890
11891 static tree
11892 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11893 tree in_decl)
11894 {
11895 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11896 if (pack == error_mark_node)
11897 return error_mark_node;
11898 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11899 if (init == error_mark_node)
11900 return error_mark_node;
11901
11902 if (PACK_EXPANSION_P (pack))
11903 {
11904 tree r = copy_node (t);
11905 FOLD_EXPR_PACK (r) = pack;
11906 FOLD_EXPR_INIT (r) = init;
11907 return r;
11908 }
11909
11910 int n = TREE_VEC_LENGTH (pack);
11911 tree vec = make_tree_vec (n + 1);
11912 for (int i = 0; i < n; ++i)
11913 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11914 TREE_VEC_ELT (vec, n) = init;
11915
11916 return expand_right_fold (t, vec, complain);
11917 }
11918
11919 /* Walk through the pattern of a pack expansion, adding everything in
11920 local_specializations to a list. */
11921
11922 struct el_data
11923 {
11924 hash_set<tree> internal;
11925 tree extra;
11926 tsubst_flags_t complain;
11927
11928 el_data (tsubst_flags_t c)
11929 : extra (NULL_TREE), complain (c) {}
11930 };
11931 static tree
11932 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11933 {
11934 el_data &data = *reinterpret_cast<el_data*>(data_);
11935 tree *extra = &data.extra;
11936 tsubst_flags_t complain = data.complain;
11937
11938 if (TYPE_P (*tp) && typedef_variant_p (*tp))
11939 /* Remember local typedefs (85214). */
11940 tp = &TYPE_NAME (*tp);
11941
11942 if (TREE_CODE (*tp) == DECL_EXPR)
11943 data.internal.add (DECL_EXPR_DECL (*tp));
11944 else if (tree spec = retrieve_local_specialization (*tp))
11945 {
11946 if (data.internal.contains (*tp))
11947 /* Don't mess with variables declared within the pattern. */
11948 return NULL_TREE;
11949 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11950 {
11951 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11952 tree args = ARGUMENT_PACK_ARGS (spec);
11953 if (TREE_VEC_LENGTH (args) == 1)
11954 {
11955 tree elt = TREE_VEC_ELT (args, 0);
11956 if (PACK_EXPANSION_P (elt))
11957 elt = PACK_EXPANSION_PATTERN (elt);
11958 if (DECL_PACK_P (elt))
11959 spec = elt;
11960 }
11961 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11962 {
11963 /* Handle lambda capture here, since we aren't doing any
11964 substitution now, and so tsubst_copy won't call
11965 process_outer_var_ref. */
11966 tree args = ARGUMENT_PACK_ARGS (spec);
11967 int len = TREE_VEC_LENGTH (args);
11968 for (int i = 0; i < len; ++i)
11969 {
11970 tree arg = TREE_VEC_ELT (args, i);
11971 tree carg = arg;
11972 if (outer_automatic_var_p (arg))
11973 carg = process_outer_var_ref (arg, complain);
11974 if (carg != arg)
11975 {
11976 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11977 proxies. */
11978 if (i == 0)
11979 {
11980 spec = copy_node (spec);
11981 args = copy_node (args);
11982 SET_ARGUMENT_PACK_ARGS (spec, args);
11983 register_local_specialization (spec, *tp);
11984 }
11985 TREE_VEC_ELT (args, i) = carg;
11986 }
11987 }
11988 }
11989 }
11990 if (outer_automatic_var_p (spec))
11991 spec = process_outer_var_ref (spec, complain);
11992 *extra = tree_cons (*tp, spec, *extra);
11993 }
11994 return NULL_TREE;
11995 }
11996 static tree
11997 extract_local_specs (tree pattern, tsubst_flags_t complain)
11998 {
11999 el_data data (complain);
12000 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12001 return data.extra;
12002 }
12003
12004 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12005 for use in PACK_EXPANSION_EXTRA_ARGS. */
12006
12007 tree
12008 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12009 {
12010 tree extra = args;
12011 if (local_specializations)
12012 if (tree locals = extract_local_specs (pattern, complain))
12013 extra = tree_cons (NULL_TREE, extra, locals);
12014 return extra;
12015 }
12016
12017 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12018 normal template args to ARGS. */
12019
12020 tree
12021 add_extra_args (tree extra, tree args)
12022 {
12023 if (extra && TREE_CODE (extra) == TREE_LIST)
12024 {
12025 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12026 {
12027 /* The partial instantiation involved local declarations collected in
12028 extract_local_specs; map from the general template to our local
12029 context. */
12030 tree gen = TREE_PURPOSE (elt);
12031 tree inst = TREE_VALUE (elt);
12032 if (DECL_P (inst))
12033 if (tree local = retrieve_local_specialization (inst))
12034 inst = local;
12035 /* else inst is already a full instantiation of the pack. */
12036 register_local_specialization (inst, gen);
12037 }
12038 gcc_assert (!TREE_PURPOSE (extra));
12039 extra = TREE_VALUE (extra);
12040 }
12041 return add_to_template_args (extra, args);
12042 }
12043
12044 /* Substitute ARGS into T, which is an pack expansion
12045 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12046 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12047 (if only a partial substitution could be performed) or
12048 ERROR_MARK_NODE if there was an error. */
12049 tree
12050 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12051 tree in_decl)
12052 {
12053 tree pattern;
12054 tree pack, packs = NULL_TREE;
12055 bool unsubstituted_packs = false;
12056 bool unsubstituted_fn_pack = false;
12057 int i, len = -1;
12058 tree result;
12059 hash_map<tree, tree> *saved_local_specializations = NULL;
12060 bool need_local_specializations = false;
12061 int levels;
12062
12063 gcc_assert (PACK_EXPANSION_P (t));
12064 pattern = PACK_EXPANSION_PATTERN (t);
12065
12066 /* Add in any args remembered from an earlier partial instantiation. */
12067 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12068
12069 levels = TMPL_ARGS_DEPTH (args);
12070
12071 /* Determine the argument packs that will instantiate the parameter
12072 packs used in the expansion expression. While we're at it,
12073 compute the number of arguments to be expanded and make sure it
12074 is consistent. */
12075 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12076 pack = TREE_CHAIN (pack))
12077 {
12078 tree parm_pack = TREE_VALUE (pack);
12079 tree arg_pack = NULL_TREE;
12080 tree orig_arg = NULL_TREE;
12081 int level = 0;
12082
12083 if (TREE_CODE (parm_pack) == BASES)
12084 {
12085 gcc_assert (parm_pack == pattern);
12086 if (BASES_DIRECT (parm_pack))
12087 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12088 args, complain,
12089 in_decl, false),
12090 complain);
12091 else
12092 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12093 args, complain, in_decl,
12094 false), complain);
12095 }
12096 else if (builtin_pack_call_p (parm_pack))
12097 {
12098 if (parm_pack != pattern)
12099 {
12100 if (complain & tf_error)
12101 sorry ("%qE is not the entire pattern of the pack expansion",
12102 parm_pack);
12103 return error_mark_node;
12104 }
12105 return expand_builtin_pack_call (parm_pack, args,
12106 complain, in_decl);
12107 }
12108 else if (TREE_CODE (parm_pack) == PARM_DECL)
12109 {
12110 /* We know we have correct local_specializations if this
12111 expansion is at function scope, or if we're dealing with a
12112 local parameter in a requires expression; for the latter,
12113 tsubst_requires_expr set it up appropriately. */
12114 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12115 arg_pack = retrieve_local_specialization (parm_pack);
12116 else
12117 /* We can't rely on local_specializations for a parameter
12118 name used later in a function declaration (such as in a
12119 late-specified return type). Even if it exists, it might
12120 have the wrong value for a recursive call. */
12121 need_local_specializations = true;
12122
12123 if (!arg_pack)
12124 {
12125 /* This parameter pack was used in an unevaluated context. Just
12126 make a dummy decl, since it's only used for its type. */
12127 ++cp_unevaluated_operand;
12128 arg_pack = tsubst_decl (parm_pack, args, complain);
12129 --cp_unevaluated_operand;
12130 if (arg_pack && DECL_PACK_P (arg_pack))
12131 /* Partial instantiation of the parm_pack, we can't build
12132 up an argument pack yet. */
12133 arg_pack = NULL_TREE;
12134 else
12135 arg_pack = make_fnparm_pack (arg_pack);
12136 }
12137 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12138 /* This argument pack isn't fully instantiated yet. We set this
12139 flag rather than clear arg_pack because we do want to do the
12140 optimization below, and we don't want to substitute directly
12141 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12142 where it isn't expected). */
12143 unsubstituted_fn_pack = true;
12144 }
12145 else if (is_normal_capture_proxy (parm_pack))
12146 {
12147 arg_pack = retrieve_local_specialization (parm_pack);
12148 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12149 unsubstituted_fn_pack = true;
12150 }
12151 else
12152 {
12153 int idx;
12154 template_parm_level_and_index (parm_pack, &level, &idx);
12155
12156 if (level <= levels)
12157 arg_pack = TMPL_ARG (args, level, idx);
12158 }
12159
12160 orig_arg = arg_pack;
12161 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12162 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12163
12164 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12165 /* This can only happen if we forget to expand an argument
12166 pack somewhere else. Just return an error, silently. */
12167 {
12168 result = make_tree_vec (1);
12169 TREE_VEC_ELT (result, 0) = error_mark_node;
12170 return result;
12171 }
12172
12173 if (arg_pack)
12174 {
12175 int my_len =
12176 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12177
12178 /* Don't bother trying to do a partial substitution with
12179 incomplete packs; we'll try again after deduction. */
12180 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12181 return t;
12182
12183 if (len < 0)
12184 len = my_len;
12185 else if (len != my_len
12186 && !unsubstituted_fn_pack)
12187 {
12188 if (!(complain & tf_error))
12189 /* Fail quietly. */;
12190 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12191 error ("mismatched argument pack lengths while expanding %qT",
12192 pattern);
12193 else
12194 error ("mismatched argument pack lengths while expanding %qE",
12195 pattern);
12196 return error_mark_node;
12197 }
12198
12199 /* Keep track of the parameter packs and their corresponding
12200 argument packs. */
12201 packs = tree_cons (parm_pack, arg_pack, packs);
12202 TREE_TYPE (packs) = orig_arg;
12203 }
12204 else
12205 {
12206 /* We can't substitute for this parameter pack. We use a flag as
12207 well as the missing_level counter because function parameter
12208 packs don't have a level. */
12209 gcc_assert (processing_template_decl || is_auto (parm_pack));
12210 unsubstituted_packs = true;
12211 }
12212 }
12213
12214 /* If the expansion is just T..., return the matching argument pack, unless
12215 we need to call convert_from_reference on all the elements. This is an
12216 important optimization; see c++/68422. */
12217 if (!unsubstituted_packs
12218 && TREE_PURPOSE (packs) == pattern)
12219 {
12220 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12221
12222 /* If the argument pack is a single pack expansion, pull it out. */
12223 if (TREE_VEC_LENGTH (args) == 1
12224 && pack_expansion_args_count (args))
12225 return TREE_VEC_ELT (args, 0);
12226
12227 /* Types need no adjustment, nor does sizeof..., and if we still have
12228 some pack expansion args we won't do anything yet. */
12229 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12230 || PACK_EXPANSION_SIZEOF_P (t)
12231 || pack_expansion_args_count (args))
12232 return args;
12233 /* Also optimize expression pack expansions if we can tell that the
12234 elements won't have reference type. */
12235 tree type = TREE_TYPE (pattern);
12236 if (type && !TYPE_REF_P (type)
12237 && !PACK_EXPANSION_P (type)
12238 && !WILDCARD_TYPE_P (type))
12239 return args;
12240 /* Otherwise use the normal path so we get convert_from_reference. */
12241 }
12242
12243 /* We cannot expand this expansion expression, because we don't have
12244 all of the argument packs we need. */
12245 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12246 {
12247 /* We got some full packs, but we can't substitute them in until we
12248 have values for all the packs. So remember these until then. */
12249
12250 t = make_pack_expansion (pattern, complain);
12251 PACK_EXPANSION_EXTRA_ARGS (t)
12252 = build_extra_args (pattern, args, complain);
12253 return t;
12254 }
12255 else if (unsubstituted_packs)
12256 {
12257 /* There were no real arguments, we're just replacing a parameter
12258 pack with another version of itself. Substitute into the
12259 pattern and return a PACK_EXPANSION_*. The caller will need to
12260 deal with that. */
12261 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12262 t = tsubst_expr (pattern, args, complain, in_decl,
12263 /*integral_constant_expression_p=*/false);
12264 else
12265 t = tsubst (pattern, args, complain, in_decl);
12266 t = make_pack_expansion (t, complain);
12267 return t;
12268 }
12269
12270 gcc_assert (len >= 0);
12271
12272 if (need_local_specializations)
12273 {
12274 /* We're in a late-specified return type, so create our own local
12275 specializations map; the current map is either NULL or (in the
12276 case of recursive unification) might have bindings that we don't
12277 want to use or alter. */
12278 saved_local_specializations = local_specializations;
12279 local_specializations = new hash_map<tree, tree>;
12280 }
12281
12282 /* For each argument in each argument pack, substitute into the
12283 pattern. */
12284 result = make_tree_vec (len);
12285 tree elem_args = copy_template_args (args);
12286 for (i = 0; i < len; ++i)
12287 {
12288 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12289 i,
12290 elem_args, complain,
12291 in_decl);
12292 TREE_VEC_ELT (result, i) = t;
12293 if (t == error_mark_node)
12294 {
12295 result = error_mark_node;
12296 break;
12297 }
12298 }
12299
12300 /* Update ARGS to restore the substitution from parameter packs to
12301 their argument packs. */
12302 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12303 {
12304 tree parm = TREE_PURPOSE (pack);
12305
12306 if (TREE_CODE (parm) == PARM_DECL
12307 || VAR_P (parm)
12308 || TREE_CODE (parm) == FIELD_DECL)
12309 register_local_specialization (TREE_TYPE (pack), parm);
12310 else
12311 {
12312 int idx, level;
12313
12314 if (TREE_VALUE (pack) == NULL_TREE)
12315 continue;
12316
12317 template_parm_level_and_index (parm, &level, &idx);
12318
12319 /* Update the corresponding argument. */
12320 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12321 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12322 TREE_TYPE (pack);
12323 else
12324 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12325 }
12326 }
12327
12328 if (need_local_specializations)
12329 {
12330 delete local_specializations;
12331 local_specializations = saved_local_specializations;
12332 }
12333
12334 /* If the dependent pack arguments were such that we end up with only a
12335 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12336 if (len == 1 && TREE_CODE (result) == TREE_VEC
12337 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12338 return TREE_VEC_ELT (result, 0);
12339
12340 return result;
12341 }
12342
12343 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12344 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12345 parameter packs; all parms generated from a function parameter pack will
12346 have the same DECL_PARM_INDEX. */
12347
12348 tree
12349 get_pattern_parm (tree parm, tree tmpl)
12350 {
12351 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12352 tree patparm;
12353
12354 if (DECL_ARTIFICIAL (parm))
12355 {
12356 for (patparm = DECL_ARGUMENTS (pattern);
12357 patparm; patparm = DECL_CHAIN (patparm))
12358 if (DECL_ARTIFICIAL (patparm)
12359 && DECL_NAME (parm) == DECL_NAME (patparm))
12360 break;
12361 }
12362 else
12363 {
12364 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12365 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12366 gcc_assert (DECL_PARM_INDEX (patparm)
12367 == DECL_PARM_INDEX (parm));
12368 }
12369
12370 return patparm;
12371 }
12372
12373 /* Make an argument pack out of the TREE_VEC VEC. */
12374
12375 static tree
12376 make_argument_pack (tree vec)
12377 {
12378 tree pack;
12379 tree elt = TREE_VEC_ELT (vec, 0);
12380 if (TYPE_P (elt))
12381 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12382 else
12383 {
12384 pack = make_node (NONTYPE_ARGUMENT_PACK);
12385 TREE_CONSTANT (pack) = 1;
12386 }
12387 SET_ARGUMENT_PACK_ARGS (pack, vec);
12388 return pack;
12389 }
12390
12391 /* Return an exact copy of template args T that can be modified
12392 independently. */
12393
12394 static tree
12395 copy_template_args (tree t)
12396 {
12397 if (t == error_mark_node)
12398 return t;
12399
12400 int len = TREE_VEC_LENGTH (t);
12401 tree new_vec = make_tree_vec (len);
12402
12403 for (int i = 0; i < len; ++i)
12404 {
12405 tree elt = TREE_VEC_ELT (t, i);
12406 if (elt && TREE_CODE (elt) == TREE_VEC)
12407 elt = copy_template_args (elt);
12408 TREE_VEC_ELT (new_vec, i) = elt;
12409 }
12410
12411 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12412 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12413
12414 return new_vec;
12415 }
12416
12417 /* Substitute ARGS into the vector or list of template arguments T. */
12418
12419 static tree
12420 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12421 {
12422 tree orig_t = t;
12423 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12424 tree *elts;
12425
12426 if (t == error_mark_node)
12427 return error_mark_node;
12428
12429 len = TREE_VEC_LENGTH (t);
12430 elts = XALLOCAVEC (tree, len);
12431
12432 for (i = 0; i < len; i++)
12433 {
12434 tree orig_arg = TREE_VEC_ELT (t, i);
12435 tree new_arg;
12436
12437 if (TREE_CODE (orig_arg) == TREE_VEC)
12438 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12439 else if (PACK_EXPANSION_P (orig_arg))
12440 {
12441 /* Substitute into an expansion expression. */
12442 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12443
12444 if (TREE_CODE (new_arg) == TREE_VEC)
12445 /* Add to the expanded length adjustment the number of
12446 expanded arguments. We subtract one from this
12447 measurement, because the argument pack expression
12448 itself is already counted as 1 in
12449 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12450 the argument pack is empty. */
12451 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12452 }
12453 else if (ARGUMENT_PACK_P (orig_arg))
12454 {
12455 /* Substitute into each of the arguments. */
12456 new_arg = TYPE_P (orig_arg)
12457 ? cxx_make_type (TREE_CODE (orig_arg))
12458 : make_node (TREE_CODE (orig_arg));
12459
12460 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12461 args, complain, in_decl);
12462 if (pack_args == error_mark_node)
12463 new_arg = error_mark_node;
12464 else
12465 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12466
12467 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12468 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12469 }
12470 else
12471 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12472
12473 if (new_arg == error_mark_node)
12474 return error_mark_node;
12475
12476 elts[i] = new_arg;
12477 if (new_arg != orig_arg)
12478 need_new = 1;
12479 }
12480
12481 if (!need_new)
12482 return t;
12483
12484 /* Make space for the expanded arguments coming from template
12485 argument packs. */
12486 t = make_tree_vec (len + expanded_len_adjust);
12487 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12488 arguments for a member template.
12489 In that case each TREE_VEC in ORIG_T represents a level of template
12490 arguments, and ORIG_T won't carry any non defaulted argument count.
12491 It will rather be the nested TREE_VECs that will carry one.
12492 In other words, ORIG_T carries a non defaulted argument count only
12493 if it doesn't contain any nested TREE_VEC. */
12494 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12495 {
12496 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12497 count += expanded_len_adjust;
12498 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12499 }
12500 for (i = 0, out = 0; i < len; i++)
12501 {
12502 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12503 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12504 && TREE_CODE (elts[i]) == TREE_VEC)
12505 {
12506 int idx;
12507
12508 /* Now expand the template argument pack "in place". */
12509 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12510 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12511 }
12512 else
12513 {
12514 TREE_VEC_ELT (t, out) = elts[i];
12515 out++;
12516 }
12517 }
12518
12519 return t;
12520 }
12521
12522 /* Substitute ARGS into one level PARMS of template parameters. */
12523
12524 static tree
12525 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12526 {
12527 if (parms == error_mark_node)
12528 return error_mark_node;
12529
12530 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12531
12532 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12533 {
12534 tree tuple = TREE_VEC_ELT (parms, i);
12535
12536 if (tuple == error_mark_node)
12537 continue;
12538
12539 TREE_VEC_ELT (new_vec, i) =
12540 tsubst_template_parm (tuple, args, complain);
12541 }
12542
12543 return new_vec;
12544 }
12545
12546 /* Return the result of substituting ARGS into the template parameters
12547 given by PARMS. If there are m levels of ARGS and m + n levels of
12548 PARMS, then the result will contain n levels of PARMS. For
12549 example, if PARMS is `template <class T> template <class U>
12550 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12551 result will be `template <int*, double, class V>'. */
12552
12553 static tree
12554 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12555 {
12556 tree r = NULL_TREE;
12557 tree* new_parms;
12558
12559 /* When substituting into a template, we must set
12560 PROCESSING_TEMPLATE_DECL as the template parameters may be
12561 dependent if they are based on one-another, and the dependency
12562 predicates are short-circuit outside of templates. */
12563 ++processing_template_decl;
12564
12565 for (new_parms = &r;
12566 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12567 new_parms = &(TREE_CHAIN (*new_parms)),
12568 parms = TREE_CHAIN (parms))
12569 {
12570 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12571 args, complain);
12572 *new_parms =
12573 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12574 - TMPL_ARGS_DEPTH (args)),
12575 new_vec, NULL_TREE);
12576 }
12577
12578 --processing_template_decl;
12579
12580 return r;
12581 }
12582
12583 /* Return the result of substituting ARGS into one template parameter
12584 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12585 parameter and which TREE_PURPOSE is the default argument of the
12586 template parameter. */
12587
12588 static tree
12589 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12590 {
12591 tree default_value, parm_decl;
12592
12593 if (args == NULL_TREE
12594 || t == NULL_TREE
12595 || t == error_mark_node)
12596 return t;
12597
12598 gcc_assert (TREE_CODE (t) == TREE_LIST);
12599
12600 default_value = TREE_PURPOSE (t);
12601 parm_decl = TREE_VALUE (t);
12602
12603 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12604 if (TREE_CODE (parm_decl) == PARM_DECL
12605 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12606 parm_decl = error_mark_node;
12607 default_value = tsubst_template_arg (default_value, args,
12608 complain, NULL_TREE);
12609
12610 return build_tree_list (default_value, parm_decl);
12611 }
12612
12613 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12614 type T. If T is not an aggregate or enumeration type, it is
12615 handled as if by tsubst. IN_DECL is as for tsubst. If
12616 ENTERING_SCOPE is nonzero, T is the context for a template which
12617 we are presently tsubst'ing. Return the substituted value. */
12618
12619 static tree
12620 tsubst_aggr_type (tree t,
12621 tree args,
12622 tsubst_flags_t complain,
12623 tree in_decl,
12624 int entering_scope)
12625 {
12626 if (t == NULL_TREE)
12627 return NULL_TREE;
12628
12629 switch (TREE_CODE (t))
12630 {
12631 case RECORD_TYPE:
12632 if (TYPE_PTRMEMFUNC_P (t))
12633 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12634
12635 /* Fall through. */
12636 case ENUMERAL_TYPE:
12637 case UNION_TYPE:
12638 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12639 {
12640 tree argvec;
12641 tree context;
12642 tree r;
12643 int saved_unevaluated_operand;
12644 int saved_inhibit_evaluation_warnings;
12645
12646 /* In "sizeof(X<I>)" we need to evaluate "I". */
12647 saved_unevaluated_operand = cp_unevaluated_operand;
12648 cp_unevaluated_operand = 0;
12649 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12650 c_inhibit_evaluation_warnings = 0;
12651
12652 /* First, determine the context for the type we are looking
12653 up. */
12654 context = TYPE_CONTEXT (t);
12655 if (context && TYPE_P (context))
12656 {
12657 context = tsubst_aggr_type (context, args, complain,
12658 in_decl, /*entering_scope=*/1);
12659 /* If context is a nested class inside a class template,
12660 it may still need to be instantiated (c++/33959). */
12661 context = complete_type (context);
12662 }
12663
12664 /* Then, figure out what arguments are appropriate for the
12665 type we are trying to find. For example, given:
12666
12667 template <class T> struct S;
12668 template <class T, class U> void f(T, U) { S<U> su; }
12669
12670 and supposing that we are instantiating f<int, double>,
12671 then our ARGS will be {int, double}, but, when looking up
12672 S we only want {double}. */
12673 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12674 complain, in_decl);
12675 if (argvec == error_mark_node)
12676 r = error_mark_node;
12677 else
12678 {
12679 r = lookup_template_class (t, argvec, in_decl, context,
12680 entering_scope, complain);
12681 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12682 }
12683
12684 cp_unevaluated_operand = saved_unevaluated_operand;
12685 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12686
12687 return r;
12688 }
12689 else
12690 /* This is not a template type, so there's nothing to do. */
12691 return t;
12692
12693 default:
12694 return tsubst (t, args, complain, in_decl);
12695 }
12696 }
12697
12698 static GTY((cache)) tree_cache_map *defarg_inst;
12699
12700 /* Substitute into the default argument ARG (a default argument for
12701 FN), which has the indicated TYPE. */
12702
12703 tree
12704 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12705 tsubst_flags_t complain)
12706 {
12707 int errs = errorcount + sorrycount;
12708
12709 /* This can happen in invalid code. */
12710 if (TREE_CODE (arg) == DEFAULT_ARG)
12711 return arg;
12712
12713 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12714 parm = chain_index (parmnum, parm);
12715 tree parmtype = TREE_TYPE (parm);
12716 if (DECL_BY_REFERENCE (parm))
12717 parmtype = TREE_TYPE (parmtype);
12718 if (parmtype == error_mark_node)
12719 return error_mark_node;
12720
12721 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12722
12723 tree *slot;
12724 if (defarg_inst && (slot = defarg_inst->get (parm)))
12725 return *slot;
12726
12727 /* This default argument came from a template. Instantiate the
12728 default argument here, not in tsubst. In the case of
12729 something like:
12730
12731 template <class T>
12732 struct S {
12733 static T t();
12734 void f(T = t());
12735 };
12736
12737 we must be careful to do name lookup in the scope of S<T>,
12738 rather than in the current class. */
12739 push_to_top_level ();
12740 push_access_scope (fn);
12741 start_lambda_scope (parm);
12742
12743 /* The default argument expression may cause implicitly defined
12744 member functions to be synthesized, which will result in garbage
12745 collection. We must treat this situation as if we were within
12746 the body of function so as to avoid collecting live data on the
12747 stack. */
12748 ++function_depth;
12749 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12750 complain, NULL_TREE,
12751 /*integral_constant_expression_p=*/false);
12752 --function_depth;
12753
12754 finish_lambda_scope ();
12755
12756 if (errorcount+sorrycount > errs
12757 && (complain & tf_warning_or_error))
12758 inform (input_location,
12759 " when instantiating default argument for call to %qD", fn);
12760
12761 /* Make sure the default argument is reasonable. */
12762 arg = check_default_argument (type, arg, complain);
12763
12764 pop_access_scope (fn);
12765 pop_from_top_level ();
12766
12767 if (arg != error_mark_node && !cp_unevaluated_operand)
12768 {
12769 if (!defarg_inst)
12770 defarg_inst = tree_cache_map::create_ggc (37);
12771 defarg_inst->put (parm, arg);
12772 }
12773
12774 return arg;
12775 }
12776
12777 /* Substitute into all the default arguments for FN. */
12778
12779 static void
12780 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12781 {
12782 tree arg;
12783 tree tmpl_args;
12784
12785 tmpl_args = DECL_TI_ARGS (fn);
12786
12787 /* If this function is not yet instantiated, we certainly don't need
12788 its default arguments. */
12789 if (uses_template_parms (tmpl_args))
12790 return;
12791 /* Don't do this again for clones. */
12792 if (DECL_CLONED_FUNCTION_P (fn))
12793 return;
12794
12795 int i = 0;
12796 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12797 arg;
12798 arg = TREE_CHAIN (arg), ++i)
12799 if (TREE_PURPOSE (arg))
12800 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12801 TREE_VALUE (arg),
12802 TREE_PURPOSE (arg),
12803 complain);
12804 }
12805
12806 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12807 static GTY((cache)) tree_cache_map *explicit_specifier_map;
12808
12809 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12810
12811 void
12812 store_explicit_specifier (tree v, tree t)
12813 {
12814 if (!explicit_specifier_map)
12815 explicit_specifier_map = tree_cache_map::create_ggc (37);
12816 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
12817 explicit_specifier_map->put (v, t);
12818 }
12819
12820 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12821
12822 static tree
12823 lookup_explicit_specifier (tree v)
12824 {
12825 return *explicit_specifier_map->get (v);
12826 }
12827
12828 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12829
12830 static tree
12831 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12832 tree lambda_fntype)
12833 {
12834 tree gen_tmpl, argvec;
12835 hashval_t hash = 0;
12836 tree in_decl = t;
12837
12838 /* Nobody should be tsubst'ing into non-template functions. */
12839 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12840
12841 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12842 {
12843 /* If T is not dependent, just return it. */
12844 if (!uses_template_parms (DECL_TI_ARGS (t)))
12845 return t;
12846
12847 /* Calculate the most general template of which R is a
12848 specialization. */
12849 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12850
12851 /* We're substituting a lambda function under tsubst_lambda_expr but not
12852 directly from it; find the matching function we're already inside.
12853 But don't do this if T is a generic lambda with a single level of
12854 template parms, as in that case we're doing a normal instantiation. */
12855 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12856 && (!generic_lambda_fn_p (t)
12857 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12858 return enclosing_instantiation_of (t);
12859
12860 /* Calculate the complete set of arguments used to
12861 specialize R. */
12862 argvec = tsubst_template_args (DECL_TI_ARGS
12863 (DECL_TEMPLATE_RESULT
12864 (DECL_TI_TEMPLATE (t))),
12865 args, complain, in_decl);
12866 if (argvec == error_mark_node)
12867 return error_mark_node;
12868
12869 /* Check to see if we already have this specialization. */
12870 if (!lambda_fntype)
12871 {
12872 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12873 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12874 return spec;
12875 }
12876
12877 /* We can see more levels of arguments than parameters if
12878 there was a specialization of a member template, like
12879 this:
12880
12881 template <class T> struct S { template <class U> void f(); }
12882 template <> template <class U> void S<int>::f(U);
12883
12884 Here, we'll be substituting into the specialization,
12885 because that's where we can find the code we actually
12886 want to generate, but we'll have enough arguments for
12887 the most general template.
12888
12889 We also deal with the peculiar case:
12890
12891 template <class T> struct S {
12892 template <class U> friend void f();
12893 };
12894 template <class U> void f() {}
12895 template S<int>;
12896 template void f<double>();
12897
12898 Here, the ARGS for the instantiation of will be {int,
12899 double}. But, we only need as many ARGS as there are
12900 levels of template parameters in CODE_PATTERN. We are
12901 careful not to get fooled into reducing the ARGS in
12902 situations like:
12903
12904 template <class T> struct S { template <class U> void f(U); }
12905 template <class T> template <> void S<T>::f(int) {}
12906
12907 which we can spot because the pattern will be a
12908 specialization in this case. */
12909 int args_depth = TMPL_ARGS_DEPTH (args);
12910 int parms_depth =
12911 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12912
12913 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12914 args = get_innermost_template_args (args, parms_depth);
12915 }
12916 else
12917 {
12918 /* This special case arises when we have something like this:
12919
12920 template <class T> struct S {
12921 friend void f<int>(int, double);
12922 };
12923
12924 Here, the DECL_TI_TEMPLATE for the friend declaration
12925 will be an IDENTIFIER_NODE. We are being called from
12926 tsubst_friend_function, and we want only to create a
12927 new decl (R) with appropriate types so that we can call
12928 determine_specialization. */
12929 gen_tmpl = NULL_TREE;
12930 argvec = NULL_TREE;
12931 }
12932
12933 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12934 : NULL_TREE);
12935 tree ctx = closure ? closure : DECL_CONTEXT (t);
12936 bool member = ctx && TYPE_P (ctx);
12937
12938 if (member && !closure)
12939 ctx = tsubst_aggr_type (ctx, args,
12940 complain, t, /*entering_scope=*/1);
12941
12942 tree type = (lambda_fntype ? lambda_fntype
12943 : tsubst (TREE_TYPE (t), args,
12944 complain | tf_fndecl_type, in_decl));
12945 if (type == error_mark_node)
12946 return error_mark_node;
12947
12948 /* If we hit excessive deduction depth, the type is bogus even if
12949 it isn't error_mark_node, so don't build a decl. */
12950 if (excessive_deduction_depth)
12951 return error_mark_node;
12952
12953 /* We do NOT check for matching decls pushed separately at this
12954 point, as they may not represent instantiations of this
12955 template, and in any case are considered separate under the
12956 discrete model. */
12957 tree r = copy_decl (t);
12958 DECL_USE_TEMPLATE (r) = 0;
12959 TREE_TYPE (r) = type;
12960 /* Clear out the mangled name and RTL for the instantiation. */
12961 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12962 SET_DECL_RTL (r, NULL);
12963 /* Leave DECL_INITIAL set on deleted instantiations. */
12964 if (!DECL_DELETED_FN (r))
12965 DECL_INITIAL (r) = NULL_TREE;
12966 DECL_CONTEXT (r) = ctx;
12967
12968 /* Handle explicit(dependent-expr). */
12969 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
12970 {
12971 tree spec = lookup_explicit_specifier (t);
12972 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
12973 /*function_p=*/false,
12974 /*i_c_e_p=*/true);
12975 spec = build_explicit_specifier (spec, complain);
12976 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
12977 }
12978
12979 /* OpenMP UDRs have the only argument a reference to the declared
12980 type. We want to diagnose if the declared type is a reference,
12981 which is invalid, but as references to references are usually
12982 quietly merged, diagnose it here. */
12983 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12984 {
12985 tree argtype
12986 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12987 argtype = tsubst (argtype, args, complain, in_decl);
12988 if (TYPE_REF_P (argtype))
12989 error_at (DECL_SOURCE_LOCATION (t),
12990 "reference type %qT in "
12991 "%<#pragma omp declare reduction%>", argtype);
12992 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12993 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12994 argtype);
12995 }
12996
12997 if (member && DECL_CONV_FN_P (r))
12998 /* Type-conversion operator. Reconstruct the name, in
12999 case it's the name of one of the template's parameters. */
13000 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13001
13002 tree parms = DECL_ARGUMENTS (t);
13003 if (closure)
13004 parms = DECL_CHAIN (parms);
13005 parms = tsubst (parms, args, complain, t);
13006 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13007 DECL_CONTEXT (parm) = r;
13008 if (closure)
13009 {
13010 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13011 DECL_CHAIN (tparm) = parms;
13012 parms = tparm;
13013 }
13014 DECL_ARGUMENTS (r) = parms;
13015 DECL_RESULT (r) = NULL_TREE;
13016
13017 TREE_STATIC (r) = 0;
13018 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13019 DECL_EXTERNAL (r) = 1;
13020 /* If this is an instantiation of a function with internal
13021 linkage, we already know what object file linkage will be
13022 assigned to the instantiation. */
13023 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13024 DECL_DEFER_OUTPUT (r) = 0;
13025 DECL_CHAIN (r) = NULL_TREE;
13026 DECL_PENDING_INLINE_INFO (r) = 0;
13027 DECL_PENDING_INLINE_P (r) = 0;
13028 DECL_SAVED_TREE (r) = NULL_TREE;
13029 DECL_STRUCT_FUNCTION (r) = NULL;
13030 TREE_USED (r) = 0;
13031 /* We'll re-clone as appropriate in instantiate_template. */
13032 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13033
13034 /* If we aren't complaining now, return on error before we register
13035 the specialization so that we'll complain eventually. */
13036 if ((complain & tf_error) == 0
13037 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13038 && !grok_op_properties (r, /*complain=*/false))
13039 return error_mark_node;
13040
13041 /* When instantiating a constrained member, substitute
13042 into the constraints to create a new constraint. */
13043 if (tree ci = get_constraints (t))
13044 if (member)
13045 {
13046 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
13047 set_constraints (r, ci);
13048 }
13049
13050 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13051 this in the special friend case mentioned above where
13052 GEN_TMPL is NULL. */
13053 if (gen_tmpl && !closure)
13054 {
13055 DECL_TEMPLATE_INFO (r)
13056 = build_template_info (gen_tmpl, argvec);
13057 SET_DECL_IMPLICIT_INSTANTIATION (r);
13058
13059 tree new_r
13060 = register_specialization (r, gen_tmpl, argvec, false, hash);
13061 if (new_r != r)
13062 /* We instantiated this while substituting into
13063 the type earlier (template/friend54.C). */
13064 return new_r;
13065
13066 /* We're not supposed to instantiate default arguments
13067 until they are called, for a template. But, for a
13068 declaration like:
13069
13070 template <class T> void f ()
13071 { extern void g(int i = T()); }
13072
13073 we should do the substitution when the template is
13074 instantiated. We handle the member function case in
13075 instantiate_class_template since the default arguments
13076 might refer to other members of the class. */
13077 if (!member
13078 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13079 && !uses_template_parms (argvec))
13080 tsubst_default_arguments (r, complain);
13081 }
13082 else
13083 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13084
13085 /* Copy the list of befriending classes. */
13086 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13087 *friends;
13088 friends = &TREE_CHAIN (*friends))
13089 {
13090 *friends = copy_node (*friends);
13091 TREE_VALUE (*friends)
13092 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13093 }
13094
13095 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13096 {
13097 maybe_retrofit_in_chrg (r);
13098 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13099 return error_mark_node;
13100 /* If this is an instantiation of a member template, clone it.
13101 If it isn't, that'll be handled by
13102 clone_constructors_and_destructors. */
13103 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13104 clone_function_decl (r, /*update_methods=*/false);
13105 }
13106 else if ((complain & tf_error) != 0
13107 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13108 && !grok_op_properties (r, /*complain=*/true))
13109 return error_mark_node;
13110
13111 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13112 SET_DECL_FRIEND_CONTEXT (r,
13113 tsubst (DECL_FRIEND_CONTEXT (t),
13114 args, complain, in_decl));
13115
13116 /* Possibly limit visibility based on template args. */
13117 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13118 if (DECL_VISIBILITY_SPECIFIED (t))
13119 {
13120 DECL_VISIBILITY_SPECIFIED (r) = 0;
13121 DECL_ATTRIBUTES (r)
13122 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13123 }
13124 determine_visibility (r);
13125 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13126 && !processing_template_decl)
13127 defaulted_late_check (r);
13128
13129 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13130 args, complain, in_decl);
13131 return r;
13132 }
13133
13134 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13135
13136 static tree
13137 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13138 tree lambda_fntype)
13139 {
13140 /* We can get here when processing a member function template,
13141 member class template, or template template parameter. */
13142 tree decl = DECL_TEMPLATE_RESULT (t);
13143 tree in_decl = t;
13144 tree spec;
13145 tree tmpl_args;
13146 tree full_args;
13147 tree r;
13148 hashval_t hash = 0;
13149
13150 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13151 {
13152 /* Template template parameter is treated here. */
13153 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13154 if (new_type == error_mark_node)
13155 r = error_mark_node;
13156 /* If we get a real template back, return it. This can happen in
13157 the context of most_specialized_partial_spec. */
13158 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13159 r = new_type;
13160 else
13161 /* The new TEMPLATE_DECL was built in
13162 reduce_template_parm_level. */
13163 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13164 return r;
13165 }
13166
13167 if (!lambda_fntype)
13168 {
13169 /* We might already have an instance of this template.
13170 The ARGS are for the surrounding class type, so the
13171 full args contain the tsubst'd args for the context,
13172 plus the innermost args from the template decl. */
13173 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13174 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13175 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13176 /* Because this is a template, the arguments will still be
13177 dependent, even after substitution. If
13178 PROCESSING_TEMPLATE_DECL is not set, the dependency
13179 predicates will short-circuit. */
13180 ++processing_template_decl;
13181 full_args = tsubst_template_args (tmpl_args, args,
13182 complain, in_decl);
13183 --processing_template_decl;
13184 if (full_args == error_mark_node)
13185 return error_mark_node;
13186
13187 /* If this is a default template template argument,
13188 tsubst might not have changed anything. */
13189 if (full_args == tmpl_args)
13190 return t;
13191
13192 hash = hash_tmpl_and_args (t, full_args);
13193 spec = retrieve_specialization (t, full_args, hash);
13194 if (spec != NULL_TREE)
13195 return spec;
13196 }
13197
13198 /* Make a new template decl. It will be similar to the
13199 original, but will record the current template arguments.
13200 We also create a new function declaration, which is just
13201 like the old one, but points to this new template, rather
13202 than the old one. */
13203 r = copy_decl (t);
13204 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13205 DECL_CHAIN (r) = NULL_TREE;
13206
13207 // Build new template info linking to the original template decl.
13208 if (!lambda_fntype)
13209 {
13210 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13211 SET_DECL_IMPLICIT_INSTANTIATION (r);
13212 }
13213 else
13214 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13215
13216 /* The template parameters for this new template are all the
13217 template parameters for the old template, except the
13218 outermost level of parameters. */
13219 DECL_TEMPLATE_PARMS (r)
13220 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13221 complain);
13222
13223 if (TREE_CODE (decl) == TYPE_DECL
13224 && !TYPE_DECL_ALIAS_P (decl))
13225 {
13226 tree new_type;
13227 ++processing_template_decl;
13228 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13229 --processing_template_decl;
13230 if (new_type == error_mark_node)
13231 return error_mark_node;
13232
13233 TREE_TYPE (r) = new_type;
13234 /* For a partial specialization, we need to keep pointing to
13235 the primary template. */
13236 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13237 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13238 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13239 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13240 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13241 }
13242 else
13243 {
13244 tree new_decl;
13245 ++processing_template_decl;
13246 if (TREE_CODE (decl) == FUNCTION_DECL)
13247 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13248 else
13249 new_decl = tsubst (decl, args, complain, in_decl);
13250 --processing_template_decl;
13251 if (new_decl == error_mark_node)
13252 return error_mark_node;
13253
13254 DECL_TEMPLATE_RESULT (r) = new_decl;
13255 TREE_TYPE (r) = TREE_TYPE (new_decl);
13256 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13257 if (lambda_fntype)
13258 {
13259 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13260 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13261 }
13262 else
13263 {
13264 DECL_TI_TEMPLATE (new_decl) = r;
13265 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13266 }
13267 }
13268
13269 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13270 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13271
13272 if (PRIMARY_TEMPLATE_P (t))
13273 DECL_PRIMARY_TEMPLATE (r) = r;
13274
13275 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13276 && !lambda_fntype)
13277 /* Record this non-type partial instantiation. */
13278 register_specialization (r, t,
13279 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13280 false, hash);
13281
13282 return r;
13283 }
13284
13285 /* True if FN is the op() for a lambda in an uninstantiated template. */
13286
13287 bool
13288 lambda_fn_in_template_p (tree fn)
13289 {
13290 if (!fn || !LAMBDA_FUNCTION_P (fn))
13291 return false;
13292 tree closure = DECL_CONTEXT (fn);
13293 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13294 }
13295
13296 /* We're instantiating a variable from template function TCTX. Return the
13297 corresponding current enclosing scope. This gets complicated because lambda
13298 functions in templates are regenerated rather than instantiated, but generic
13299 lambda functions are subsequently instantiated. */
13300
13301 static tree
13302 enclosing_instantiation_of (tree otctx)
13303 {
13304 tree tctx = otctx;
13305 tree fn = current_function_decl;
13306 int lambda_count = 0;
13307
13308 for (; tctx && lambda_fn_in_template_p (tctx);
13309 tctx = decl_function_context (tctx))
13310 ++lambda_count;
13311 for (; fn; fn = decl_function_context (fn))
13312 {
13313 tree ofn = fn;
13314 int flambda_count = 0;
13315 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
13316 fn = decl_function_context (fn))
13317 ++flambda_count;
13318 if ((fn && DECL_TEMPLATE_INFO (fn))
13319 ? most_general_template (fn) != most_general_template (tctx)
13320 : fn != tctx)
13321 continue;
13322 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13323 || DECL_CONV_FN_P (ofn));
13324 return ofn;
13325 }
13326 gcc_unreachable ();
13327 }
13328
13329 /* Substitute the ARGS into the T, which is a _DECL. Return the
13330 result of the substitution. Issue error and warning messages under
13331 control of COMPLAIN. */
13332
13333 static tree
13334 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13335 {
13336 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13337 location_t saved_loc;
13338 tree r = NULL_TREE;
13339 tree in_decl = t;
13340 hashval_t hash = 0;
13341
13342 /* Set the filename and linenumber to improve error-reporting. */
13343 saved_loc = input_location;
13344 input_location = DECL_SOURCE_LOCATION (t);
13345
13346 switch (TREE_CODE (t))
13347 {
13348 case TEMPLATE_DECL:
13349 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13350 break;
13351
13352 case FUNCTION_DECL:
13353 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13354 break;
13355
13356 case PARM_DECL:
13357 {
13358 tree type = NULL_TREE;
13359 int i, len = 1;
13360 tree expanded_types = NULL_TREE;
13361 tree prev_r = NULL_TREE;
13362 tree first_r = NULL_TREE;
13363
13364 if (DECL_PACK_P (t))
13365 {
13366 /* If there is a local specialization that isn't a
13367 parameter pack, it means that we're doing a "simple"
13368 substitution from inside tsubst_pack_expansion. Just
13369 return the local specialization (which will be a single
13370 parm). */
13371 tree spec = retrieve_local_specialization (t);
13372 if (spec
13373 && TREE_CODE (spec) == PARM_DECL
13374 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13375 RETURN (spec);
13376
13377 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13378 the parameters in this function parameter pack. */
13379 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13380 complain, in_decl);
13381 if (TREE_CODE (expanded_types) == TREE_VEC)
13382 {
13383 len = TREE_VEC_LENGTH (expanded_types);
13384
13385 /* Zero-length parameter packs are boring. Just substitute
13386 into the chain. */
13387 if (len == 0)
13388 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13389 TREE_CHAIN (t)));
13390 }
13391 else
13392 {
13393 /* All we did was update the type. Make a note of that. */
13394 type = expanded_types;
13395 expanded_types = NULL_TREE;
13396 }
13397 }
13398
13399 /* Loop through all of the parameters we'll build. When T is
13400 a function parameter pack, LEN is the number of expanded
13401 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13402 r = NULL_TREE;
13403 for (i = 0; i < len; ++i)
13404 {
13405 prev_r = r;
13406 r = copy_node (t);
13407 if (DECL_TEMPLATE_PARM_P (t))
13408 SET_DECL_TEMPLATE_PARM_P (r);
13409
13410 if (expanded_types)
13411 /* We're on the Ith parameter of the function parameter
13412 pack. */
13413 {
13414 /* Get the Ith type. */
13415 type = TREE_VEC_ELT (expanded_types, i);
13416
13417 /* Rename the parameter to include the index. */
13418 DECL_NAME (r)
13419 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13420 }
13421 else if (!type)
13422 /* We're dealing with a normal parameter. */
13423 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13424
13425 type = type_decays_to (type);
13426 TREE_TYPE (r) = type;
13427 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13428
13429 if (DECL_INITIAL (r))
13430 {
13431 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13432 DECL_INITIAL (r) = TREE_TYPE (r);
13433 else
13434 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13435 complain, in_decl);
13436 }
13437
13438 DECL_CONTEXT (r) = NULL_TREE;
13439
13440 if (!DECL_TEMPLATE_PARM_P (r))
13441 DECL_ARG_TYPE (r) = type_passed_as (type);
13442
13443 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13444 args, complain, in_decl);
13445
13446 /* Keep track of the first new parameter we
13447 generate. That's what will be returned to the
13448 caller. */
13449 if (!first_r)
13450 first_r = r;
13451
13452 /* Build a proper chain of parameters when substituting
13453 into a function parameter pack. */
13454 if (prev_r)
13455 DECL_CHAIN (prev_r) = r;
13456 }
13457
13458 /* If cp_unevaluated_operand is set, we're just looking for a
13459 single dummy parameter, so don't keep going. */
13460 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13461 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13462 complain, DECL_CHAIN (t));
13463
13464 /* FIRST_R contains the start of the chain we've built. */
13465 r = first_r;
13466 }
13467 break;
13468
13469 case FIELD_DECL:
13470 {
13471 tree type = NULL_TREE;
13472 tree vec = NULL_TREE;
13473 tree expanded_types = NULL_TREE;
13474 int len = 1;
13475
13476 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13477 {
13478 /* This field is a lambda capture pack. Return a TREE_VEC of
13479 the expanded fields to instantiate_class_template_1. */
13480 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13481 complain, in_decl);
13482 if (TREE_CODE (expanded_types) == TREE_VEC)
13483 {
13484 len = TREE_VEC_LENGTH (expanded_types);
13485 vec = make_tree_vec (len);
13486 }
13487 else
13488 {
13489 /* All we did was update the type. Make a note of that. */
13490 type = expanded_types;
13491 expanded_types = NULL_TREE;
13492 }
13493 }
13494
13495 for (int i = 0; i < len; ++i)
13496 {
13497 r = copy_decl (t);
13498 if (expanded_types)
13499 {
13500 type = TREE_VEC_ELT (expanded_types, i);
13501 DECL_NAME (r)
13502 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13503 }
13504 else if (!type)
13505 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13506
13507 if (type == error_mark_node)
13508 RETURN (error_mark_node);
13509 TREE_TYPE (r) = type;
13510 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13511
13512 if (DECL_C_BIT_FIELD (r))
13513 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13514 number of bits. */
13515 DECL_BIT_FIELD_REPRESENTATIVE (r)
13516 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13517 complain, in_decl,
13518 /*integral_constant_expression_p=*/true);
13519 if (DECL_INITIAL (t))
13520 {
13521 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13522 NSDMI in perform_member_init. Still set DECL_INITIAL
13523 so that we know there is one. */
13524 DECL_INITIAL (r) = void_node;
13525 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13526 retrofit_lang_decl (r);
13527 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13528 }
13529 /* We don't have to set DECL_CONTEXT here; it is set by
13530 finish_member_declaration. */
13531 DECL_CHAIN (r) = NULL_TREE;
13532
13533 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13534 args, complain, in_decl);
13535
13536 if (vec)
13537 TREE_VEC_ELT (vec, i) = r;
13538 }
13539
13540 if (vec)
13541 r = vec;
13542 }
13543 break;
13544
13545 case USING_DECL:
13546 /* We reach here only for member using decls. We also need to check
13547 uses_template_parms because DECL_DEPENDENT_P is not set for a
13548 using-declaration that designates a member of the current
13549 instantiation (c++/53549). */
13550 if (DECL_DEPENDENT_P (t)
13551 || uses_template_parms (USING_DECL_SCOPE (t)))
13552 {
13553 tree scope = USING_DECL_SCOPE (t);
13554 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13555 if (PACK_EXPANSION_P (scope))
13556 {
13557 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13558 int len = TREE_VEC_LENGTH (vec);
13559 r = make_tree_vec (len);
13560 for (int i = 0; i < len; ++i)
13561 {
13562 tree escope = TREE_VEC_ELT (vec, i);
13563 tree elt = do_class_using_decl (escope, name);
13564 if (!elt)
13565 {
13566 r = error_mark_node;
13567 break;
13568 }
13569 else
13570 {
13571 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13572 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13573 }
13574 TREE_VEC_ELT (r, i) = elt;
13575 }
13576 }
13577 else
13578 {
13579 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13580 complain, in_decl);
13581 r = do_class_using_decl (inst_scope, name);
13582 if (!r)
13583 r = error_mark_node;
13584 else
13585 {
13586 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13587 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13588 }
13589 }
13590 }
13591 else
13592 {
13593 r = copy_node (t);
13594 DECL_CHAIN (r) = NULL_TREE;
13595 }
13596 break;
13597
13598 case TYPE_DECL:
13599 case VAR_DECL:
13600 {
13601 tree argvec = NULL_TREE;
13602 tree gen_tmpl = NULL_TREE;
13603 tree spec;
13604 tree tmpl = NULL_TREE;
13605 tree ctx;
13606 tree type = NULL_TREE;
13607 bool local_p;
13608
13609 if (TREE_TYPE (t) == error_mark_node)
13610 RETURN (error_mark_node);
13611
13612 if (TREE_CODE (t) == TYPE_DECL
13613 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13614 {
13615 /* If this is the canonical decl, we don't have to
13616 mess with instantiations, and often we can't (for
13617 typename, template type parms and such). Note that
13618 TYPE_NAME is not correct for the above test if
13619 we've copied the type for a typedef. */
13620 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13621 if (type == error_mark_node)
13622 RETURN (error_mark_node);
13623 r = TYPE_NAME (type);
13624 break;
13625 }
13626
13627 /* Check to see if we already have the specialization we
13628 need. */
13629 spec = NULL_TREE;
13630 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13631 {
13632 /* T is a static data member or namespace-scope entity.
13633 We have to substitute into namespace-scope variables
13634 (not just variable templates) because of cases like:
13635
13636 template <class T> void f() { extern T t; }
13637
13638 where the entity referenced is not known until
13639 instantiation time. */
13640 local_p = false;
13641 ctx = DECL_CONTEXT (t);
13642 if (DECL_CLASS_SCOPE_P (t))
13643 {
13644 ctx = tsubst_aggr_type (ctx, args,
13645 complain,
13646 in_decl, /*entering_scope=*/1);
13647 /* If CTX is unchanged, then T is in fact the
13648 specialization we want. That situation occurs when
13649 referencing a static data member within in its own
13650 class. We can use pointer equality, rather than
13651 same_type_p, because DECL_CONTEXT is always
13652 canonical... */
13653 if (ctx == DECL_CONTEXT (t)
13654 /* ... unless T is a member template; in which
13655 case our caller can be willing to create a
13656 specialization of that template represented
13657 by T. */
13658 && !(DECL_TI_TEMPLATE (t)
13659 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13660 spec = t;
13661 }
13662
13663 if (!spec)
13664 {
13665 tmpl = DECL_TI_TEMPLATE (t);
13666 gen_tmpl = most_general_template (tmpl);
13667 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13668 if (argvec != error_mark_node)
13669 argvec = (coerce_innermost_template_parms
13670 (DECL_TEMPLATE_PARMS (gen_tmpl),
13671 argvec, t, complain,
13672 /*all*/true, /*defarg*/true));
13673 if (argvec == error_mark_node)
13674 RETURN (error_mark_node);
13675 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13676 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13677 }
13678 }
13679 else
13680 {
13681 /* A local variable. */
13682 local_p = true;
13683 /* Subsequent calls to pushdecl will fill this in. */
13684 ctx = NULL_TREE;
13685 /* Unless this is a reference to a static variable from an
13686 enclosing function, in which case we need to fill it in now. */
13687 if (TREE_STATIC (t))
13688 {
13689 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13690 if (fn != current_function_decl)
13691 ctx = fn;
13692 }
13693 spec = retrieve_local_specialization (t);
13694 }
13695 /* If we already have the specialization we need, there is
13696 nothing more to do. */
13697 if (spec)
13698 {
13699 r = spec;
13700 break;
13701 }
13702
13703 /* Create a new node for the specialization we need. */
13704 if (type == NULL_TREE)
13705 {
13706 if (is_typedef_decl (t))
13707 type = DECL_ORIGINAL_TYPE (t);
13708 else
13709 type = TREE_TYPE (t);
13710 if (VAR_P (t)
13711 && VAR_HAD_UNKNOWN_BOUND (t)
13712 && type != error_mark_node)
13713 type = strip_array_domain (type);
13714 tree sub_args = args;
13715 if (tree auto_node = type_uses_auto (type))
13716 {
13717 /* Mask off any template args past the variable's context so we
13718 don't replace the auto with an unrelated argument. */
13719 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13720 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13721 if (extra > 0)
13722 /* This should never happen with the new lambda instantiation
13723 model, but keep the handling just in case. */
13724 gcc_assert (!CHECKING_P),
13725 sub_args = strip_innermost_template_args (args, extra);
13726 }
13727 type = tsubst (type, sub_args, complain, in_decl);
13728 /* Substituting the type might have recursively instantiated this
13729 same alias (c++/86171). */
13730 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
13731 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
13732 {
13733 r = spec;
13734 break;
13735 }
13736 }
13737 r = copy_decl (t);
13738 if (VAR_P (r))
13739 {
13740 DECL_INITIALIZED_P (r) = 0;
13741 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13742 if (type == error_mark_node)
13743 RETURN (error_mark_node);
13744 if (TREE_CODE (type) == FUNCTION_TYPE)
13745 {
13746 /* It may seem that this case cannot occur, since:
13747
13748 typedef void f();
13749 void g() { f x; }
13750
13751 declares a function, not a variable. However:
13752
13753 typedef void f();
13754 template <typename T> void g() { T t; }
13755 template void g<f>();
13756
13757 is an attempt to declare a variable with function
13758 type. */
13759 error ("variable %qD has function type",
13760 /* R is not yet sufficiently initialized, so we
13761 just use its name. */
13762 DECL_NAME (r));
13763 RETURN (error_mark_node);
13764 }
13765 type = complete_type (type);
13766 /* Wait until cp_finish_decl to set this again, to handle
13767 circular dependency (template/instantiate6.C). */
13768 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13769 type = check_var_type (DECL_NAME (r), type);
13770
13771 if (DECL_HAS_VALUE_EXPR_P (t))
13772 {
13773 tree ve = DECL_VALUE_EXPR (t);
13774 ve = tsubst_expr (ve, args, complain, in_decl,
13775 /*constant_expression_p=*/false);
13776 if (REFERENCE_REF_P (ve))
13777 {
13778 gcc_assert (TYPE_REF_P (type));
13779 ve = TREE_OPERAND (ve, 0);
13780 }
13781 SET_DECL_VALUE_EXPR (r, ve);
13782 }
13783 if (CP_DECL_THREAD_LOCAL_P (r)
13784 && !processing_template_decl)
13785 set_decl_tls_model (r, decl_default_tls_model (r));
13786 }
13787 else if (DECL_SELF_REFERENCE_P (t))
13788 SET_DECL_SELF_REFERENCE_P (r);
13789 TREE_TYPE (r) = type;
13790 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13791 DECL_CONTEXT (r) = ctx;
13792 /* Clear out the mangled name and RTL for the instantiation. */
13793 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13794 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13795 SET_DECL_RTL (r, NULL);
13796 /* The initializer must not be expanded until it is required;
13797 see [temp.inst]. */
13798 DECL_INITIAL (r) = NULL_TREE;
13799 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13800 if (VAR_P (r))
13801 {
13802 if (DECL_LANG_SPECIFIC (r))
13803 SET_DECL_DEPENDENT_INIT_P (r, false);
13804
13805 SET_DECL_MODE (r, VOIDmode);
13806
13807 /* Possibly limit visibility based on template args. */
13808 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13809 if (DECL_VISIBILITY_SPECIFIED (t))
13810 {
13811 DECL_VISIBILITY_SPECIFIED (r) = 0;
13812 DECL_ATTRIBUTES (r)
13813 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13814 }
13815 determine_visibility (r);
13816 }
13817
13818 if (!local_p)
13819 {
13820 /* A static data member declaration is always marked
13821 external when it is declared in-class, even if an
13822 initializer is present. We mimic the non-template
13823 processing here. */
13824 DECL_EXTERNAL (r) = 1;
13825 if (DECL_NAMESPACE_SCOPE_P (t))
13826 DECL_NOT_REALLY_EXTERN (r) = 1;
13827
13828 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13829 SET_DECL_IMPLICIT_INSTANTIATION (r);
13830 register_specialization (r, gen_tmpl, argvec, false, hash);
13831 }
13832 else
13833 {
13834 if (DECL_LANG_SPECIFIC (r))
13835 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13836 if (!cp_unevaluated_operand)
13837 register_local_specialization (r, t);
13838 }
13839
13840 DECL_CHAIN (r) = NULL_TREE;
13841
13842 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13843 /*flags=*/0,
13844 args, complain, in_decl);
13845
13846 /* Preserve a typedef that names a type. */
13847 if (is_typedef_decl (r) && type != error_mark_node)
13848 {
13849 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13850 set_underlying_type (r);
13851 if (TYPE_DECL_ALIAS_P (r))
13852 /* An alias template specialization can be dependent
13853 even if its underlying type is not. */
13854 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13855 }
13856
13857 layout_decl (r, 0);
13858 }
13859 break;
13860
13861 default:
13862 gcc_unreachable ();
13863 }
13864 #undef RETURN
13865
13866 out:
13867 /* Restore the file and line information. */
13868 input_location = saved_loc;
13869
13870 return r;
13871 }
13872
13873 /* Substitute into the ARG_TYPES of a function type.
13874 If END is a TREE_CHAIN, leave it and any following types
13875 un-substituted. */
13876
13877 static tree
13878 tsubst_arg_types (tree arg_types,
13879 tree args,
13880 tree end,
13881 tsubst_flags_t complain,
13882 tree in_decl)
13883 {
13884 tree remaining_arg_types;
13885 tree type = NULL_TREE;
13886 int i = 1;
13887 tree expanded_args = NULL_TREE;
13888 tree default_arg;
13889
13890 if (!arg_types || arg_types == void_list_node || arg_types == end)
13891 return arg_types;
13892
13893 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13894 args, end, complain, in_decl);
13895 if (remaining_arg_types == error_mark_node)
13896 return error_mark_node;
13897
13898 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13899 {
13900 /* For a pack expansion, perform substitution on the
13901 entire expression. Later on, we'll handle the arguments
13902 one-by-one. */
13903 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13904 args, complain, in_decl);
13905
13906 if (TREE_CODE (expanded_args) == TREE_VEC)
13907 /* So that we'll spin through the parameters, one by one. */
13908 i = TREE_VEC_LENGTH (expanded_args);
13909 else
13910 {
13911 /* We only partially substituted into the parameter
13912 pack. Our type is TYPE_PACK_EXPANSION. */
13913 type = expanded_args;
13914 expanded_args = NULL_TREE;
13915 }
13916 }
13917
13918 while (i > 0) {
13919 --i;
13920
13921 if (expanded_args)
13922 type = TREE_VEC_ELT (expanded_args, i);
13923 else if (!type)
13924 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13925
13926 if (type == error_mark_node)
13927 return error_mark_node;
13928 if (VOID_TYPE_P (type))
13929 {
13930 if (complain & tf_error)
13931 {
13932 error ("invalid parameter type %qT", type);
13933 if (in_decl)
13934 error ("in declaration %q+D", in_decl);
13935 }
13936 return error_mark_node;
13937 }
13938 /* DR 657. */
13939 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13940 return error_mark_node;
13941
13942 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13943 top-level qualifiers as required. */
13944 type = cv_unqualified (type_decays_to (type));
13945
13946 /* We do not substitute into default arguments here. The standard
13947 mandates that they be instantiated only when needed, which is
13948 done in build_over_call. */
13949 default_arg = TREE_PURPOSE (arg_types);
13950
13951 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13952 since the new op() won't have any associated template arguments for us
13953 to refer to later. */
13954 if (lambda_fn_in_template_p (in_decl))
13955 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13956 false/*fn*/, false/*constexpr*/);
13957
13958 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13959 {
13960 /* We've instantiated a template before its default arguments
13961 have been parsed. This can happen for a nested template
13962 class, and is not an error unless we require the default
13963 argument in a call of this function. */
13964 remaining_arg_types =
13965 tree_cons (default_arg, type, remaining_arg_types);
13966 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13967 }
13968 else
13969 remaining_arg_types =
13970 hash_tree_cons (default_arg, type, remaining_arg_types);
13971 }
13972
13973 return remaining_arg_types;
13974 }
13975
13976 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13977 *not* handle the exception-specification for FNTYPE, because the
13978 initial substitution of explicitly provided template parameters
13979 during argument deduction forbids substitution into the
13980 exception-specification:
13981
13982 [temp.deduct]
13983
13984 All references in the function type of the function template to the
13985 corresponding template parameters are replaced by the specified tem-
13986 plate argument values. If a substitution in a template parameter or
13987 in the function type of the function template results in an invalid
13988 type, type deduction fails. [Note: The equivalent substitution in
13989 exception specifications is done only when the function is instanti-
13990 ated, at which point a program is ill-formed if the substitution
13991 results in an invalid type.] */
13992
13993 static tree
13994 tsubst_function_type (tree t,
13995 tree args,
13996 tsubst_flags_t complain,
13997 tree in_decl)
13998 {
13999 tree return_type;
14000 tree arg_types = NULL_TREE;
14001 tree fntype;
14002
14003 /* The TYPE_CONTEXT is not used for function/method types. */
14004 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14005
14006 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14007 failure. */
14008 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14009
14010 if (late_return_type_p)
14011 {
14012 /* Substitute the argument types. */
14013 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14014 complain, in_decl);
14015 if (arg_types == error_mark_node)
14016 return error_mark_node;
14017
14018 tree save_ccp = current_class_ptr;
14019 tree save_ccr = current_class_ref;
14020 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14021 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14022 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14023 if (do_inject)
14024 {
14025 /* DR 1207: 'this' is in scope in the trailing return type. */
14026 inject_this_parameter (this_type, cp_type_quals (this_type));
14027 }
14028
14029 /* Substitute the return type. */
14030 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14031
14032 if (do_inject)
14033 {
14034 current_class_ptr = save_ccp;
14035 current_class_ref = save_ccr;
14036 }
14037 }
14038 else
14039 /* Substitute the return type. */
14040 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14041
14042 if (return_type == error_mark_node)
14043 return error_mark_node;
14044 /* DR 486 clarifies that creation of a function type with an
14045 invalid return type is a deduction failure. */
14046 if (TREE_CODE (return_type) == ARRAY_TYPE
14047 || TREE_CODE (return_type) == FUNCTION_TYPE)
14048 {
14049 if (complain & tf_error)
14050 {
14051 if (TREE_CODE (return_type) == ARRAY_TYPE)
14052 error ("function returning an array");
14053 else
14054 error ("function returning a function");
14055 }
14056 return error_mark_node;
14057 }
14058 /* And DR 657. */
14059 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14060 return error_mark_node;
14061
14062 if (!late_return_type_p)
14063 {
14064 /* Substitute the argument types. */
14065 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14066 complain, in_decl);
14067 if (arg_types == error_mark_node)
14068 return error_mark_node;
14069 }
14070
14071 /* Construct a new type node and return it. */
14072 if (TREE_CODE (t) == FUNCTION_TYPE)
14073 {
14074 fntype = build_function_type (return_type, arg_types);
14075 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14076 }
14077 else
14078 {
14079 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14080 /* Don't pick up extra function qualifiers from the basetype. */
14081 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14082 if (! 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 function of non-class type %qT",
14093 r);
14094 return error_mark_node;
14095 }
14096
14097 fntype = build_method_type_directly (r, return_type,
14098 TREE_CHAIN (arg_types));
14099 }
14100 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14101
14102 /* See comment above. */
14103 tree raises = NULL_TREE;
14104 cp_ref_qualifier rqual = type_memfn_rqual (t);
14105 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14106
14107 return fntype;
14108 }
14109
14110 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14111 ARGS into that specification, and return the substituted
14112 specification. If there is no specification, return NULL_TREE. */
14113
14114 static tree
14115 tsubst_exception_specification (tree fntype,
14116 tree args,
14117 tsubst_flags_t complain,
14118 tree in_decl,
14119 bool defer_ok)
14120 {
14121 tree specs;
14122 tree new_specs;
14123
14124 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14125 new_specs = NULL_TREE;
14126 if (specs && TREE_PURPOSE (specs))
14127 {
14128 /* A noexcept-specifier. */
14129 tree expr = TREE_PURPOSE (specs);
14130 if (TREE_CODE (expr) == INTEGER_CST)
14131 new_specs = expr;
14132 else if (defer_ok)
14133 {
14134 /* Defer instantiation of noexcept-specifiers to avoid
14135 excessive instantiations (c++/49107). */
14136 new_specs = make_node (DEFERRED_NOEXCEPT);
14137 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14138 {
14139 /* We already partially instantiated this member template,
14140 so combine the new args with the old. */
14141 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14142 = DEFERRED_NOEXCEPT_PATTERN (expr);
14143 DEFERRED_NOEXCEPT_ARGS (new_specs)
14144 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14145 }
14146 else
14147 {
14148 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14149 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14150 }
14151 }
14152 else
14153 new_specs = tsubst_copy_and_build
14154 (expr, args, complain, in_decl, /*function_p=*/false,
14155 /*integral_constant_expression_p=*/true);
14156 new_specs = build_noexcept_spec (new_specs, complain);
14157 }
14158 else if (specs)
14159 {
14160 if (! TREE_VALUE (specs))
14161 new_specs = specs;
14162 else
14163 while (specs)
14164 {
14165 tree spec;
14166 int i, len = 1;
14167 tree expanded_specs = NULL_TREE;
14168
14169 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14170 {
14171 /* Expand the pack expansion type. */
14172 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14173 args, complain,
14174 in_decl);
14175
14176 if (expanded_specs == error_mark_node)
14177 return error_mark_node;
14178 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14179 len = TREE_VEC_LENGTH (expanded_specs);
14180 else
14181 {
14182 /* We're substituting into a member template, so
14183 we got a TYPE_PACK_EXPANSION back. Add that
14184 expansion and move on. */
14185 gcc_assert (TREE_CODE (expanded_specs)
14186 == TYPE_PACK_EXPANSION);
14187 new_specs = add_exception_specifier (new_specs,
14188 expanded_specs,
14189 complain);
14190 specs = TREE_CHAIN (specs);
14191 continue;
14192 }
14193 }
14194
14195 for (i = 0; i < len; ++i)
14196 {
14197 if (expanded_specs)
14198 spec = TREE_VEC_ELT (expanded_specs, i);
14199 else
14200 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14201 if (spec == error_mark_node)
14202 return spec;
14203 new_specs = add_exception_specifier (new_specs, spec,
14204 complain);
14205 }
14206
14207 specs = TREE_CHAIN (specs);
14208 }
14209 }
14210 return new_specs;
14211 }
14212
14213 /* Take the tree structure T and replace template parameters used
14214 therein with the argument vector ARGS. IN_DECL is an associated
14215 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14216 Issue error and warning messages under control of COMPLAIN. Note
14217 that we must be relatively non-tolerant of extensions here, in
14218 order to preserve conformance; if we allow substitutions that
14219 should not be allowed, we may allow argument deductions that should
14220 not succeed, and therefore report ambiguous overload situations
14221 where there are none. In theory, we could allow the substitution,
14222 but indicate that it should have failed, and allow our caller to
14223 make sure that the right thing happens, but we don't try to do this
14224 yet.
14225
14226 This function is used for dealing with types, decls and the like;
14227 for expressions, use tsubst_expr or tsubst_copy. */
14228
14229 tree
14230 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14231 {
14232 enum tree_code code;
14233 tree type, r = NULL_TREE;
14234
14235 if (t == NULL_TREE || t == error_mark_node
14236 || t == integer_type_node
14237 || t == void_type_node
14238 || t == char_type_node
14239 || t == unknown_type_node
14240 || TREE_CODE (t) == NAMESPACE_DECL
14241 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14242 return t;
14243
14244 if (DECL_P (t))
14245 return tsubst_decl (t, args, complain);
14246
14247 if (args == NULL_TREE)
14248 return t;
14249
14250 code = TREE_CODE (t);
14251
14252 if (code == IDENTIFIER_NODE)
14253 type = IDENTIFIER_TYPE_VALUE (t);
14254 else
14255 type = TREE_TYPE (t);
14256
14257 gcc_assert (type != unknown_type_node);
14258
14259 /* Reuse typedefs. We need to do this to handle dependent attributes,
14260 such as attribute aligned. */
14261 if (TYPE_P (t)
14262 && typedef_variant_p (t))
14263 {
14264 tree decl = TYPE_NAME (t);
14265
14266 if (alias_template_specialization_p (t))
14267 {
14268 /* DECL represents an alias template and we want to
14269 instantiate it. */
14270 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14271 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14272 r = instantiate_alias_template (tmpl, gen_args, complain);
14273 }
14274 else if (DECL_CLASS_SCOPE_P (decl)
14275 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14276 && uses_template_parms (DECL_CONTEXT (decl)))
14277 {
14278 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14279 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14280 r = retrieve_specialization (tmpl, gen_args, 0);
14281 }
14282 else if (DECL_FUNCTION_SCOPE_P (decl)
14283 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14284 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14285 r = retrieve_local_specialization (decl);
14286 else
14287 /* The typedef is from a non-template context. */
14288 return t;
14289
14290 if (r)
14291 {
14292 r = TREE_TYPE (r);
14293 r = cp_build_qualified_type_real
14294 (r, cp_type_quals (t) | cp_type_quals (r),
14295 complain | tf_ignore_bad_quals);
14296 return r;
14297 }
14298 else
14299 {
14300 /* We don't have an instantiation yet, so drop the typedef. */
14301 int quals = cp_type_quals (t);
14302 t = DECL_ORIGINAL_TYPE (decl);
14303 t = cp_build_qualified_type_real (t, quals,
14304 complain | tf_ignore_bad_quals);
14305 }
14306 }
14307
14308 bool fndecl_type = (complain & tf_fndecl_type);
14309 complain &= ~tf_fndecl_type;
14310
14311 if (type
14312 && code != TYPENAME_TYPE
14313 && code != TEMPLATE_TYPE_PARM
14314 && code != TEMPLATE_PARM_INDEX
14315 && code != IDENTIFIER_NODE
14316 && code != FUNCTION_TYPE
14317 && code != METHOD_TYPE)
14318 type = tsubst (type, args, complain, in_decl);
14319 if (type == error_mark_node)
14320 return error_mark_node;
14321
14322 switch (code)
14323 {
14324 case RECORD_TYPE:
14325 case UNION_TYPE:
14326 case ENUMERAL_TYPE:
14327 return tsubst_aggr_type (t, args, complain, in_decl,
14328 /*entering_scope=*/0);
14329
14330 case ERROR_MARK:
14331 case IDENTIFIER_NODE:
14332 case VOID_TYPE:
14333 case REAL_TYPE:
14334 case COMPLEX_TYPE:
14335 case VECTOR_TYPE:
14336 case BOOLEAN_TYPE:
14337 case NULLPTR_TYPE:
14338 case LANG_TYPE:
14339 return t;
14340
14341 case INTEGER_TYPE:
14342 if (t == integer_type_node)
14343 return t;
14344
14345 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14346 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14347 return t;
14348
14349 {
14350 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14351
14352 max = tsubst_expr (omax, args, complain, in_decl,
14353 /*integral_constant_expression_p=*/false);
14354
14355 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14356 needed. */
14357 if (TREE_CODE (max) == NOP_EXPR
14358 && TREE_SIDE_EFFECTS (omax)
14359 && !TREE_TYPE (max))
14360 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14361
14362 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14363 with TREE_SIDE_EFFECTS that indicates this is not an integral
14364 constant expression. */
14365 if (processing_template_decl
14366 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14367 {
14368 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14369 TREE_SIDE_EFFECTS (max) = 1;
14370 }
14371
14372 return compute_array_index_type (NULL_TREE, max, complain);
14373 }
14374
14375 case TEMPLATE_TYPE_PARM:
14376 case TEMPLATE_TEMPLATE_PARM:
14377 case BOUND_TEMPLATE_TEMPLATE_PARM:
14378 case TEMPLATE_PARM_INDEX:
14379 {
14380 int idx;
14381 int level;
14382 int levels;
14383 tree arg = NULL_TREE;
14384
14385 /* Early in template argument deduction substitution, we don't
14386 want to reduce the level of 'auto', or it will be confused
14387 with a normal template parm in subsequent deduction. */
14388 if (is_auto (t) && (complain & tf_partial))
14389 return t;
14390
14391 r = NULL_TREE;
14392
14393 gcc_assert (TREE_VEC_LENGTH (args) > 0);
14394 template_parm_level_and_index (t, &level, &idx);
14395
14396 levels = TMPL_ARGS_DEPTH (args);
14397 if (level <= levels
14398 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14399 {
14400 arg = TMPL_ARG (args, level, idx);
14401
14402 /* See through ARGUMENT_PACK_SELECT arguments. */
14403 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14404 arg = argument_pack_select_arg (arg);
14405 }
14406
14407 if (arg == error_mark_node)
14408 return error_mark_node;
14409 else if (arg != NULL_TREE)
14410 {
14411 if (ARGUMENT_PACK_P (arg))
14412 /* If ARG is an argument pack, we don't actually want to
14413 perform a substitution here, because substitutions
14414 for argument packs are only done
14415 element-by-element. We can get to this point when
14416 substituting the type of a non-type template
14417 parameter pack, when that type actually contains
14418 template parameter packs from an outer template, e.g.,
14419
14420 template<typename... Types> struct A {
14421 template<Types... Values> struct B { };
14422 }; */
14423 return t;
14424
14425 if (code == TEMPLATE_TYPE_PARM)
14426 {
14427 int quals;
14428 gcc_assert (TYPE_P (arg));
14429
14430 quals = cp_type_quals (arg) | cp_type_quals (t);
14431
14432 return cp_build_qualified_type_real
14433 (arg, quals, complain | tf_ignore_bad_quals);
14434 }
14435 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14436 {
14437 /* We are processing a type constructed from a
14438 template template parameter. */
14439 tree argvec = tsubst (TYPE_TI_ARGS (t),
14440 args, complain, in_decl);
14441 if (argvec == error_mark_node)
14442 return error_mark_node;
14443
14444 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14445 || TREE_CODE (arg) == TEMPLATE_DECL
14446 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14447
14448 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14449 /* Consider this code:
14450
14451 template <template <class> class Template>
14452 struct Internal {
14453 template <class Arg> using Bind = Template<Arg>;
14454 };
14455
14456 template <template <class> class Template, class Arg>
14457 using Instantiate = Template<Arg>; //#0
14458
14459 template <template <class> class Template,
14460 class Argument>
14461 using Bind =
14462 Instantiate<Internal<Template>::template Bind,
14463 Argument>; //#1
14464
14465 When #1 is parsed, the
14466 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14467 parameter `Template' in #0 matches the
14468 UNBOUND_CLASS_TEMPLATE representing the argument
14469 `Internal<Template>::template Bind'; We then want
14470 to assemble the type `Bind<Argument>' that can't
14471 be fully created right now, because
14472 `Internal<Template>' not being complete, the Bind
14473 template cannot be looked up in that context. So
14474 we need to "store" `Bind<Argument>' for later
14475 when the context of Bind becomes complete. Let's
14476 store that in a TYPENAME_TYPE. */
14477 return make_typename_type (TYPE_CONTEXT (arg),
14478 build_nt (TEMPLATE_ID_EXPR,
14479 TYPE_IDENTIFIER (arg),
14480 argvec),
14481 typename_type,
14482 complain);
14483
14484 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14485 are resolving nested-types in the signature of a
14486 member function templates. Otherwise ARG is a
14487 TEMPLATE_DECL and is the real template to be
14488 instantiated. */
14489 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14490 arg = TYPE_NAME (arg);
14491
14492 r = lookup_template_class (arg,
14493 argvec, in_decl,
14494 DECL_CONTEXT (arg),
14495 /*entering_scope=*/0,
14496 complain);
14497 return cp_build_qualified_type_real
14498 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14499 }
14500 else if (code == TEMPLATE_TEMPLATE_PARM)
14501 return arg;
14502 else
14503 /* TEMPLATE_PARM_INDEX. */
14504 return convert_from_reference (unshare_expr (arg));
14505 }
14506
14507 if (level == 1)
14508 /* This can happen during the attempted tsubst'ing in
14509 unify. This means that we don't yet have any information
14510 about the template parameter in question. */
14511 return t;
14512
14513 /* If we get here, we must have been looking at a parm for a
14514 more deeply nested template. Make a new version of this
14515 template parameter, but with a lower level. */
14516 switch (code)
14517 {
14518 case TEMPLATE_TYPE_PARM:
14519 case TEMPLATE_TEMPLATE_PARM:
14520 case BOUND_TEMPLATE_TEMPLATE_PARM:
14521 if (cp_type_quals (t))
14522 {
14523 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14524 r = cp_build_qualified_type_real
14525 (r, cp_type_quals (t),
14526 complain | (code == TEMPLATE_TYPE_PARM
14527 ? tf_ignore_bad_quals : 0));
14528 }
14529 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14530 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14531 && (r = (TEMPLATE_PARM_DESCENDANTS
14532 (TEMPLATE_TYPE_PARM_INDEX (t))))
14533 && (r = TREE_TYPE (r))
14534 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14535 /* Break infinite recursion when substituting the constraints
14536 of a constrained placeholder. */;
14537 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14538 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
14539 && !CLASS_PLACEHOLDER_TEMPLATE (t)
14540 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
14541 r = TEMPLATE_PARM_DESCENDANTS (arg))
14542 && (TEMPLATE_PARM_LEVEL (r)
14543 == TEMPLATE_PARM_LEVEL (arg) - levels))
14544 /* Cache the simple case of lowering a type parameter. */
14545 r = TREE_TYPE (r);
14546 else
14547 {
14548 r = copy_type (t);
14549 TEMPLATE_TYPE_PARM_INDEX (r)
14550 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14551 r, levels, args, complain);
14552 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14553 TYPE_MAIN_VARIANT (r) = r;
14554 TYPE_POINTER_TO (r) = NULL_TREE;
14555 TYPE_REFERENCE_TO (r) = NULL_TREE;
14556
14557 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14558 {
14559 /* Propagate constraints on placeholders. */
14560 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14561 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14562 = tsubst_constraint (constr, args, complain, in_decl);
14563 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14564 {
14565 pl = tsubst_copy (pl, args, complain, in_decl);
14566 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14567 }
14568 }
14569
14570 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14571 /* We have reduced the level of the template
14572 template parameter, but not the levels of its
14573 template parameters, so canonical_type_parameter
14574 will not be able to find the canonical template
14575 template parameter for this level. Thus, we
14576 require structural equality checking to compare
14577 TEMPLATE_TEMPLATE_PARMs. */
14578 SET_TYPE_STRUCTURAL_EQUALITY (r);
14579 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14580 SET_TYPE_STRUCTURAL_EQUALITY (r);
14581 else
14582 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14583
14584 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14585 {
14586 tree tinfo = TYPE_TEMPLATE_INFO (t);
14587 /* We might need to substitute into the types of non-type
14588 template parameters. */
14589 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14590 complain, in_decl);
14591 if (tmpl == error_mark_node)
14592 return error_mark_node;
14593 tree argvec = tsubst (TI_ARGS (tinfo), args,
14594 complain, in_decl);
14595 if (argvec == error_mark_node)
14596 return error_mark_node;
14597
14598 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14599 = build_template_info (tmpl, argvec);
14600 }
14601 }
14602 break;
14603
14604 case TEMPLATE_PARM_INDEX:
14605 /* OK, now substitute the type of the non-type parameter. We
14606 couldn't do it earlier because it might be an auto parameter,
14607 and we wouldn't need to if we had an argument. */
14608 type = tsubst (type, args, complain, in_decl);
14609 if (type == error_mark_node)
14610 return error_mark_node;
14611 r = reduce_template_parm_level (t, type, levels, args, complain);
14612 break;
14613
14614 default:
14615 gcc_unreachable ();
14616 }
14617
14618 return r;
14619 }
14620
14621 case TREE_LIST:
14622 {
14623 tree purpose, value, chain;
14624
14625 if (t == void_list_node)
14626 return t;
14627
14628 purpose = TREE_PURPOSE (t);
14629 if (purpose)
14630 {
14631 purpose = tsubst (purpose, args, complain, in_decl);
14632 if (purpose == error_mark_node)
14633 return error_mark_node;
14634 }
14635 value = TREE_VALUE (t);
14636 if (value)
14637 {
14638 value = tsubst (value, args, complain, in_decl);
14639 if (value == error_mark_node)
14640 return error_mark_node;
14641 }
14642 chain = TREE_CHAIN (t);
14643 if (chain && chain != void_type_node)
14644 {
14645 chain = tsubst (chain, args, complain, in_decl);
14646 if (chain == error_mark_node)
14647 return error_mark_node;
14648 }
14649 if (purpose == TREE_PURPOSE (t)
14650 && value == TREE_VALUE (t)
14651 && chain == TREE_CHAIN (t))
14652 return t;
14653 return hash_tree_cons (purpose, value, chain);
14654 }
14655
14656 case TREE_BINFO:
14657 /* We should never be tsubsting a binfo. */
14658 gcc_unreachable ();
14659
14660 case TREE_VEC:
14661 /* A vector of template arguments. */
14662 gcc_assert (!type);
14663 return tsubst_template_args (t, args, complain, in_decl);
14664
14665 case POINTER_TYPE:
14666 case REFERENCE_TYPE:
14667 {
14668 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14669 return t;
14670
14671 /* [temp.deduct]
14672
14673 Type deduction may fail for any of the following
14674 reasons:
14675
14676 -- Attempting to create a pointer to reference type.
14677 -- Attempting to create a reference to a reference type or
14678 a reference to void.
14679
14680 Core issue 106 says that creating a reference to a reference
14681 during instantiation is no longer a cause for failure. We
14682 only enforce this check in strict C++98 mode. */
14683 if ((TYPE_REF_P (type)
14684 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14685 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14686 {
14687 static location_t last_loc;
14688
14689 /* We keep track of the last time we issued this error
14690 message to avoid spewing a ton of messages during a
14691 single bad template instantiation. */
14692 if (complain & tf_error
14693 && last_loc != input_location)
14694 {
14695 if (VOID_TYPE_P (type))
14696 error ("forming reference to void");
14697 else if (code == POINTER_TYPE)
14698 error ("forming pointer to reference type %qT", type);
14699 else
14700 error ("forming reference to reference type %qT", type);
14701 last_loc = input_location;
14702 }
14703
14704 return error_mark_node;
14705 }
14706 else if (TREE_CODE (type) == FUNCTION_TYPE
14707 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14708 || type_memfn_rqual (type) != REF_QUAL_NONE))
14709 {
14710 if (complain & tf_error)
14711 {
14712 if (code == POINTER_TYPE)
14713 error ("forming pointer to qualified function type %qT",
14714 type);
14715 else
14716 error ("forming reference to qualified function type %qT",
14717 type);
14718 }
14719 return error_mark_node;
14720 }
14721 else if (code == POINTER_TYPE)
14722 {
14723 r = build_pointer_type (type);
14724 if (TREE_CODE (type) == METHOD_TYPE)
14725 r = build_ptrmemfunc_type (r);
14726 }
14727 else if (TYPE_REF_P (type))
14728 /* In C++0x, during template argument substitution, when there is an
14729 attempt to create a reference to a reference type, reference
14730 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14731
14732 "If a template-argument for a template-parameter T names a type
14733 that is a reference to a type A, an attempt to create the type
14734 'lvalue reference to cv T' creates the type 'lvalue reference to
14735 A,' while an attempt to create the type type rvalue reference to
14736 cv T' creates the type T"
14737 */
14738 r = cp_build_reference_type
14739 (TREE_TYPE (type),
14740 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14741 else
14742 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14743 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14744
14745 if (r != error_mark_node)
14746 /* Will this ever be needed for TYPE_..._TO values? */
14747 layout_type (r);
14748
14749 return r;
14750 }
14751 case OFFSET_TYPE:
14752 {
14753 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14754 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14755 {
14756 /* [temp.deduct]
14757
14758 Type deduction may fail for any of the following
14759 reasons:
14760
14761 -- Attempting to create "pointer to member of T" when T
14762 is not a class type. */
14763 if (complain & tf_error)
14764 error ("creating pointer to member of non-class type %qT", r);
14765 return error_mark_node;
14766 }
14767 if (TYPE_REF_P (type))
14768 {
14769 if (complain & tf_error)
14770 error ("creating pointer to member reference type %qT", type);
14771 return error_mark_node;
14772 }
14773 if (VOID_TYPE_P (type))
14774 {
14775 if (complain & tf_error)
14776 error ("creating pointer to member of type void");
14777 return error_mark_node;
14778 }
14779 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14780 if (TREE_CODE (type) == FUNCTION_TYPE)
14781 {
14782 /* The type of the implicit object parameter gets its
14783 cv-qualifiers from the FUNCTION_TYPE. */
14784 tree memptr;
14785 tree method_type
14786 = build_memfn_type (type, r, type_memfn_quals (type),
14787 type_memfn_rqual (type));
14788 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14789 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14790 complain);
14791 }
14792 else
14793 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14794 cp_type_quals (t),
14795 complain);
14796 }
14797 case FUNCTION_TYPE:
14798 case METHOD_TYPE:
14799 {
14800 tree fntype;
14801 tree specs;
14802 fntype = tsubst_function_type (t, args, complain, in_decl);
14803 if (fntype == error_mark_node)
14804 return error_mark_node;
14805
14806 /* Substitute the exception specification. */
14807 specs = tsubst_exception_specification (t, args, complain, in_decl,
14808 /*defer_ok*/fndecl_type);
14809 if (specs == error_mark_node)
14810 return error_mark_node;
14811 if (specs)
14812 fntype = build_exception_variant (fntype, specs);
14813 return fntype;
14814 }
14815 case ARRAY_TYPE:
14816 {
14817 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14818 if (domain == error_mark_node)
14819 return error_mark_node;
14820
14821 /* As an optimization, we avoid regenerating the array type if
14822 it will obviously be the same as T. */
14823 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14824 return t;
14825
14826 /* These checks should match the ones in create_array_type_for_decl.
14827
14828 [temp.deduct]
14829
14830 The deduction may fail for any of the following reasons:
14831
14832 -- Attempting to create an array with an element type that
14833 is void, a function type, or a reference type, or [DR337]
14834 an abstract class type. */
14835 if (VOID_TYPE_P (type)
14836 || TREE_CODE (type) == FUNCTION_TYPE
14837 || (TREE_CODE (type) == ARRAY_TYPE
14838 && TYPE_DOMAIN (type) == NULL_TREE)
14839 || TYPE_REF_P (type))
14840 {
14841 if (complain & tf_error)
14842 error ("creating array of %qT", type);
14843 return error_mark_node;
14844 }
14845
14846 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14847 return error_mark_node;
14848
14849 r = build_cplus_array_type (type, domain);
14850
14851 if (!valid_array_size_p (input_location, r, in_decl,
14852 (complain & tf_error)))
14853 return error_mark_node;
14854
14855 if (TYPE_USER_ALIGN (t))
14856 {
14857 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14858 TYPE_USER_ALIGN (r) = 1;
14859 }
14860
14861 return r;
14862 }
14863
14864 case TYPENAME_TYPE:
14865 {
14866 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14867 in_decl, /*entering_scope=*/1);
14868 if (ctx == error_mark_node)
14869 return error_mark_node;
14870
14871 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14872 complain, in_decl);
14873 if (f == error_mark_node)
14874 return error_mark_node;
14875
14876 if (!MAYBE_CLASS_TYPE_P (ctx))
14877 {
14878 if (complain & tf_error)
14879 error ("%qT is not a class, struct, or union type", ctx);
14880 return error_mark_node;
14881 }
14882 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14883 {
14884 /* Normally, make_typename_type does not require that the CTX
14885 have complete type in order to allow things like:
14886
14887 template <class T> struct S { typename S<T>::X Y; };
14888
14889 But, such constructs have already been resolved by this
14890 point, so here CTX really should have complete type, unless
14891 it's a partial instantiation. */
14892 ctx = complete_type (ctx);
14893 if (!COMPLETE_TYPE_P (ctx))
14894 {
14895 if (complain & tf_error)
14896 cxx_incomplete_type_error (NULL_TREE, ctx);
14897 return error_mark_node;
14898 }
14899 }
14900
14901 f = make_typename_type (ctx, f, typename_type,
14902 complain | tf_keep_type_decl);
14903 if (f == error_mark_node)
14904 return f;
14905 if (TREE_CODE (f) == TYPE_DECL)
14906 {
14907 complain |= tf_ignore_bad_quals;
14908 f = TREE_TYPE (f);
14909 }
14910
14911 if (TREE_CODE (f) != TYPENAME_TYPE)
14912 {
14913 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14914 {
14915 if (complain & tf_error)
14916 error ("%qT resolves to %qT, which is not an enumeration type",
14917 t, f);
14918 else
14919 return error_mark_node;
14920 }
14921 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14922 {
14923 if (complain & tf_error)
14924 error ("%qT resolves to %qT, which is is not a class type",
14925 t, f);
14926 else
14927 return error_mark_node;
14928 }
14929 }
14930
14931 return cp_build_qualified_type_real
14932 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14933 }
14934
14935 case UNBOUND_CLASS_TEMPLATE:
14936 {
14937 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14938 in_decl, /*entering_scope=*/1);
14939 tree name = TYPE_IDENTIFIER (t);
14940 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14941
14942 if (ctx == error_mark_node || name == error_mark_node)
14943 return error_mark_node;
14944
14945 if (parm_list)
14946 parm_list = tsubst_template_parms (parm_list, args, complain);
14947 return make_unbound_class_template (ctx, name, parm_list, complain);
14948 }
14949
14950 case TYPEOF_TYPE:
14951 {
14952 tree type;
14953
14954 ++cp_unevaluated_operand;
14955 ++c_inhibit_evaluation_warnings;
14956
14957 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14958 complain, in_decl,
14959 /*integral_constant_expression_p=*/false);
14960
14961 --cp_unevaluated_operand;
14962 --c_inhibit_evaluation_warnings;
14963
14964 type = finish_typeof (type);
14965 return cp_build_qualified_type_real (type,
14966 cp_type_quals (t)
14967 | cp_type_quals (type),
14968 complain);
14969 }
14970
14971 case DECLTYPE_TYPE:
14972 {
14973 tree type;
14974
14975 ++cp_unevaluated_operand;
14976 ++c_inhibit_evaluation_warnings;
14977
14978 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14979 complain|tf_decltype, in_decl,
14980 /*function_p*/false,
14981 /*integral_constant_expression*/false);
14982
14983 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14984 {
14985 if (type == NULL_TREE)
14986 {
14987 if (complain & tf_error)
14988 error ("empty initializer in lambda init-capture");
14989 type = error_mark_node;
14990 }
14991 else if (TREE_CODE (type) == TREE_LIST)
14992 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14993 }
14994
14995 --cp_unevaluated_operand;
14996 --c_inhibit_evaluation_warnings;
14997
14998 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14999 type = lambda_capture_field_type (type,
15000 DECLTYPE_FOR_INIT_CAPTURE (t),
15001 DECLTYPE_FOR_REF_CAPTURE (t));
15002 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15003 type = lambda_proxy_type (type);
15004 else
15005 {
15006 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15007 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15008 && EXPR_P (type))
15009 /* In a template ~id could be either a complement expression
15010 or an unqualified-id naming a destructor; if instantiating
15011 it produces an expression, it's not an id-expression or
15012 member access. */
15013 id = false;
15014 type = finish_decltype_type (type, id, complain);
15015 }
15016 return cp_build_qualified_type_real (type,
15017 cp_type_quals (t)
15018 | cp_type_quals (type),
15019 complain | tf_ignore_bad_quals);
15020 }
15021
15022 case UNDERLYING_TYPE:
15023 {
15024 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15025 complain, in_decl);
15026 return finish_underlying_type (type);
15027 }
15028
15029 case TYPE_ARGUMENT_PACK:
15030 case NONTYPE_ARGUMENT_PACK:
15031 {
15032 tree r;
15033
15034 if (code == NONTYPE_ARGUMENT_PACK)
15035 r = make_node (code);
15036 else
15037 r = cxx_make_type (code);
15038
15039 tree pack_args = ARGUMENT_PACK_ARGS (t);
15040 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15041 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15042
15043 return r;
15044 }
15045
15046 case VOID_CST:
15047 case INTEGER_CST:
15048 case REAL_CST:
15049 case STRING_CST:
15050 case PLUS_EXPR:
15051 case MINUS_EXPR:
15052 case NEGATE_EXPR:
15053 case NOP_EXPR:
15054 case INDIRECT_REF:
15055 case ADDR_EXPR:
15056 case CALL_EXPR:
15057 case ARRAY_REF:
15058 case SCOPE_REF:
15059 /* We should use one of the expression tsubsts for these codes. */
15060 gcc_unreachable ();
15061
15062 default:
15063 sorry ("use of %qs in template", get_tree_code_name (code));
15064 return error_mark_node;
15065 }
15066 }
15067
15068 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15069 expression on the left-hand side of the "." or "->" operator. We
15070 only do the lookup if we had a dependent BASELINK. Otherwise we
15071 adjust it onto the instantiated heirarchy. */
15072
15073 static tree
15074 tsubst_baselink (tree baselink, tree object_type,
15075 tree args, tsubst_flags_t complain, tree in_decl)
15076 {
15077 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15078 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15079 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15080
15081 tree optype = BASELINK_OPTYPE (baselink);
15082 optype = tsubst (optype, args, complain, in_decl);
15083
15084 tree template_args = NULL_TREE;
15085 bool template_id_p = false;
15086 tree fns = BASELINK_FUNCTIONS (baselink);
15087 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15088 {
15089 template_id_p = true;
15090 template_args = TREE_OPERAND (fns, 1);
15091 fns = TREE_OPERAND (fns, 0);
15092 if (template_args)
15093 template_args = tsubst_template_args (template_args, args,
15094 complain, in_decl);
15095 }
15096
15097 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15098 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15099 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15100
15101 if (dependent_p)
15102 {
15103 tree name = OVL_NAME (fns);
15104 if (IDENTIFIER_CONV_OP_P (name))
15105 name = make_conv_op_name (optype);
15106
15107 if (name == complete_dtor_identifier)
15108 /* Treat as-if non-dependent below. */
15109 dependent_p = false;
15110
15111 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15112 if (!baselink)
15113 {
15114 if ((complain & tf_error)
15115 && constructor_name_p (name, qualifying_scope))
15116 error ("cannot call constructor %<%T::%D%> directly",
15117 qualifying_scope, name);
15118 return error_mark_node;
15119 }
15120
15121 if (BASELINK_P (baselink))
15122 fns = BASELINK_FUNCTIONS (baselink);
15123 }
15124 else
15125 /* We're going to overwrite pieces below, make a duplicate. */
15126 baselink = copy_node (baselink);
15127
15128 /* If lookup found a single function, mark it as used at this point.
15129 (If lookup found multiple functions the one selected later by
15130 overload resolution will be marked as used at that point.) */
15131 if (!template_id_p && !really_overloaded_fn (fns))
15132 {
15133 tree fn = OVL_FIRST (fns);
15134 bool ok = mark_used (fn, complain);
15135 if (!ok && !(complain & tf_error))
15136 return error_mark_node;
15137 if (ok && BASELINK_P (baselink))
15138 /* We might have instantiated an auto function. */
15139 TREE_TYPE (baselink) = TREE_TYPE (fn);
15140 }
15141
15142 if (BASELINK_P (baselink))
15143 {
15144 /* Add back the template arguments, if present. */
15145 if (template_id_p)
15146 BASELINK_FUNCTIONS (baselink)
15147 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15148
15149 /* Update the conversion operator type. */
15150 BASELINK_OPTYPE (baselink) = optype;
15151 }
15152
15153 if (!object_type)
15154 object_type = current_class_type;
15155
15156 if (qualified_p || !dependent_p)
15157 {
15158 baselink = adjust_result_of_qualified_name_lookup (baselink,
15159 qualifying_scope,
15160 object_type);
15161 if (!qualified_p)
15162 /* We need to call adjust_result_of_qualified_name_lookup in case the
15163 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15164 so that we still get virtual function binding. */
15165 BASELINK_QUALIFIED_P (baselink) = false;
15166 }
15167
15168 return baselink;
15169 }
15170
15171 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15172 true if the qualified-id will be a postfix-expression in-and-of
15173 itself; false if more of the postfix-expression follows the
15174 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15175 of "&". */
15176
15177 static tree
15178 tsubst_qualified_id (tree qualified_id, tree args,
15179 tsubst_flags_t complain, tree in_decl,
15180 bool done, bool address_p)
15181 {
15182 tree expr;
15183 tree scope;
15184 tree name;
15185 bool is_template;
15186 tree template_args;
15187 location_t loc = UNKNOWN_LOCATION;
15188
15189 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15190
15191 /* Figure out what name to look up. */
15192 name = TREE_OPERAND (qualified_id, 1);
15193 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15194 {
15195 is_template = true;
15196 loc = EXPR_LOCATION (name);
15197 template_args = TREE_OPERAND (name, 1);
15198 if (template_args)
15199 template_args = tsubst_template_args (template_args, args,
15200 complain, in_decl);
15201 if (template_args == error_mark_node)
15202 return error_mark_node;
15203 name = TREE_OPERAND (name, 0);
15204 }
15205 else
15206 {
15207 is_template = false;
15208 template_args = NULL_TREE;
15209 }
15210
15211 /* Substitute into the qualifying scope. When there are no ARGS, we
15212 are just trying to simplify a non-dependent expression. In that
15213 case the qualifying scope may be dependent, and, in any case,
15214 substituting will not help. */
15215 scope = TREE_OPERAND (qualified_id, 0);
15216 if (args)
15217 {
15218 scope = tsubst (scope, args, complain, in_decl);
15219 expr = tsubst_copy (name, args, complain, in_decl);
15220 }
15221 else
15222 expr = name;
15223
15224 if (dependent_scope_p (scope))
15225 {
15226 if (is_template)
15227 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15228 tree r = build_qualified_name (NULL_TREE, scope, expr,
15229 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15230 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15231 return r;
15232 }
15233
15234 if (!BASELINK_P (name) && !DECL_P (expr))
15235 {
15236 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15237 {
15238 /* A BIT_NOT_EXPR is used to represent a destructor. */
15239 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15240 {
15241 error ("qualifying type %qT does not match destructor name ~%qT",
15242 scope, TREE_OPERAND (expr, 0));
15243 expr = error_mark_node;
15244 }
15245 else
15246 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15247 /*is_type_p=*/0, false);
15248 }
15249 else
15250 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15251 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15252 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15253 {
15254 if (complain & tf_error)
15255 {
15256 error ("dependent-name %qE is parsed as a non-type, but "
15257 "instantiation yields a type", qualified_id);
15258 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15259 }
15260 return error_mark_node;
15261 }
15262 }
15263
15264 if (DECL_P (expr))
15265 {
15266 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15267 scope);
15268 /* Remember that there was a reference to this entity. */
15269 if (!mark_used (expr, complain) && !(complain & tf_error))
15270 return error_mark_node;
15271 }
15272
15273 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15274 {
15275 if (complain & tf_error)
15276 qualified_name_lookup_error (scope,
15277 TREE_OPERAND (qualified_id, 1),
15278 expr, input_location);
15279 return error_mark_node;
15280 }
15281
15282 if (is_template)
15283 {
15284 /* We may be repeating a check already done during parsing, but
15285 if it was well-formed and passed then, it will pass again
15286 now, and if it didn't, we wouldn't have got here. The case
15287 we want to catch is when we couldn't tell then, and can now,
15288 namely when templ prior to substitution was an
15289 identifier. */
15290 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15291 return error_mark_node;
15292
15293 if (variable_template_p (expr))
15294 expr = lookup_and_finish_template_variable (expr, template_args,
15295 complain);
15296 else
15297 expr = lookup_template_function (expr, template_args);
15298 }
15299
15300 if (expr == error_mark_node && complain & tf_error)
15301 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15302 expr, input_location);
15303 else if (TYPE_P (scope))
15304 {
15305 expr = (adjust_result_of_qualified_name_lookup
15306 (expr, scope, current_nonlambda_class_type ()));
15307 expr = (finish_qualified_id_expr
15308 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15309 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15310 /*template_arg_p=*/false, complain));
15311 }
15312
15313 /* Expressions do not generally have reference type. */
15314 if (TREE_CODE (expr) != SCOPE_REF
15315 /* However, if we're about to form a pointer-to-member, we just
15316 want the referenced member referenced. */
15317 && TREE_CODE (expr) != OFFSET_REF)
15318 expr = convert_from_reference (expr);
15319
15320 if (REF_PARENTHESIZED_P (qualified_id))
15321 expr = force_paren_expr (expr);
15322
15323 return expr;
15324 }
15325
15326 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15327 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15328 for tsubst. */
15329
15330 static tree
15331 tsubst_init (tree init, tree decl, tree args,
15332 tsubst_flags_t complain, tree in_decl)
15333 {
15334 if (!init)
15335 return NULL_TREE;
15336
15337 init = tsubst_expr (init, args, complain, in_decl, false);
15338
15339 if (!init && TREE_TYPE (decl) != error_mark_node)
15340 {
15341 /* If we had an initializer but it
15342 instantiated to nothing,
15343 value-initialize the object. This will
15344 only occur when the initializer was a
15345 pack expansion where the parameter packs
15346 used in that expansion were of length
15347 zero. */
15348 init = build_value_init (TREE_TYPE (decl),
15349 complain);
15350 if (TREE_CODE (init) == AGGR_INIT_EXPR)
15351 init = get_target_expr_sfinae (init, complain);
15352 if (TREE_CODE (init) == TARGET_EXPR)
15353 TARGET_EXPR_DIRECT_INIT_P (init) = true;
15354 }
15355
15356 return init;
15357 }
15358
15359 /* Like tsubst, but deals with expressions. This function just replaces
15360 template parms; to finish processing the resultant expression, use
15361 tsubst_copy_and_build or tsubst_expr. */
15362
15363 static tree
15364 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15365 {
15366 enum tree_code code;
15367 tree r;
15368
15369 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
15370 return t;
15371
15372 code = TREE_CODE (t);
15373
15374 switch (code)
15375 {
15376 case PARM_DECL:
15377 r = retrieve_local_specialization (t);
15378
15379 if (r == NULL_TREE)
15380 {
15381 /* We get here for a use of 'this' in an NSDMI. */
15382 if (DECL_NAME (t) == this_identifier && current_class_ptr)
15383 return current_class_ptr;
15384
15385 /* This can happen for a parameter name used later in a function
15386 declaration (such as in a late-specified return type). Just
15387 make a dummy decl, since it's only used for its type. */
15388 gcc_assert (cp_unevaluated_operand != 0);
15389 r = tsubst_decl (t, args, complain);
15390 /* Give it the template pattern as its context; its true context
15391 hasn't been instantiated yet and this is good enough for
15392 mangling. */
15393 DECL_CONTEXT (r) = DECL_CONTEXT (t);
15394 }
15395
15396 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15397 r = argument_pack_select_arg (r);
15398 if (!mark_used (r, complain) && !(complain & tf_error))
15399 return error_mark_node;
15400 return r;
15401
15402 case CONST_DECL:
15403 {
15404 tree enum_type;
15405 tree v;
15406
15407 if (DECL_TEMPLATE_PARM_P (t))
15408 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
15409 /* There is no need to substitute into namespace-scope
15410 enumerators. */
15411 if (DECL_NAMESPACE_SCOPE_P (t))
15412 return t;
15413 /* If ARGS is NULL, then T is known to be non-dependent. */
15414 if (args == NULL_TREE)
15415 return scalar_constant_value (t);
15416
15417 /* Unfortunately, we cannot just call lookup_name here.
15418 Consider:
15419
15420 template <int I> int f() {
15421 enum E { a = I };
15422 struct S { void g() { E e = a; } };
15423 };
15424
15425 When we instantiate f<7>::S::g(), say, lookup_name is not
15426 clever enough to find f<7>::a. */
15427 enum_type
15428 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15429 /*entering_scope=*/0);
15430
15431 for (v = TYPE_VALUES (enum_type);
15432 v != NULL_TREE;
15433 v = TREE_CHAIN (v))
15434 if (TREE_PURPOSE (v) == DECL_NAME (t))
15435 return TREE_VALUE (v);
15436
15437 /* We didn't find the name. That should never happen; if
15438 name-lookup found it during preliminary parsing, we
15439 should find it again here during instantiation. */
15440 gcc_unreachable ();
15441 }
15442 return t;
15443
15444 case FIELD_DECL:
15445 if (DECL_CONTEXT (t))
15446 {
15447 tree ctx;
15448
15449 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15450 /*entering_scope=*/1);
15451 if (ctx != DECL_CONTEXT (t))
15452 {
15453 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15454 if (!r)
15455 {
15456 if (complain & tf_error)
15457 error ("using invalid field %qD", t);
15458 return error_mark_node;
15459 }
15460 return r;
15461 }
15462 }
15463
15464 return t;
15465
15466 case VAR_DECL:
15467 case FUNCTION_DECL:
15468 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15469 r = tsubst (t, args, complain, in_decl);
15470 else if (local_variable_p (t)
15471 && uses_template_parms (DECL_CONTEXT (t)))
15472 {
15473 r = retrieve_local_specialization (t);
15474 if (r == NULL_TREE)
15475 {
15476 /* First try name lookup to find the instantiation. */
15477 r = lookup_name (DECL_NAME (t));
15478 if (r && !is_capture_proxy (r))
15479 {
15480 /* Make sure that the one we found is the one we want. */
15481 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15482 if (ctx != DECL_CONTEXT (r))
15483 r = NULL_TREE;
15484 }
15485
15486 if (r)
15487 /* OK */;
15488 else
15489 {
15490 /* This can happen for a variable used in a
15491 late-specified return type of a local lambda, or for a
15492 local static or constant. Building a new VAR_DECL
15493 should be OK in all those cases. */
15494 r = tsubst_decl (t, args, complain);
15495 if (local_specializations)
15496 /* Avoid infinite recursion (79640). */
15497 register_local_specialization (r, t);
15498 if (decl_maybe_constant_var_p (r))
15499 {
15500 /* We can't call cp_finish_decl, so handle the
15501 initializer by hand. */
15502 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15503 complain, in_decl);
15504 if (!processing_template_decl)
15505 init = maybe_constant_init (init);
15506 if (processing_template_decl
15507 ? potential_constant_expression (init)
15508 : reduced_constant_expression_p (init))
15509 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15510 = TREE_CONSTANT (r) = true;
15511 DECL_INITIAL (r) = init;
15512 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15513 TREE_TYPE (r)
15514 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15515 complain, adc_variable_type);
15516 }
15517 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15518 || decl_constant_var_p (r)
15519 || errorcount || sorrycount);
15520 if (!processing_template_decl
15521 && !TREE_STATIC (r))
15522 r = process_outer_var_ref (r, complain);
15523 }
15524 /* Remember this for subsequent uses. */
15525 if (local_specializations)
15526 register_local_specialization (r, t);
15527 }
15528 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15529 r = argument_pack_select_arg (r);
15530 }
15531 else
15532 r = t;
15533 if (!mark_used (r, complain))
15534 return error_mark_node;
15535 return r;
15536
15537 case NAMESPACE_DECL:
15538 return t;
15539
15540 case OVERLOAD:
15541 /* We must have marked any lookups as persistent. */
15542 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
15543 return t;
15544
15545 case BASELINK:
15546 return tsubst_baselink (t, current_nonlambda_class_type (),
15547 args, complain, in_decl);
15548
15549 case TEMPLATE_DECL:
15550 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15551 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15552 args, complain, in_decl);
15553 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15554 return tsubst (t, args, complain, in_decl);
15555 else if (DECL_CLASS_SCOPE_P (t)
15556 && uses_template_parms (DECL_CONTEXT (t)))
15557 {
15558 /* Template template argument like the following example need
15559 special treatment:
15560
15561 template <template <class> class TT> struct C {};
15562 template <class T> struct D {
15563 template <class U> struct E {};
15564 C<E> c; // #1
15565 };
15566 D<int> d; // #2
15567
15568 We are processing the template argument `E' in #1 for
15569 the template instantiation #2. Originally, `E' is a
15570 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15571 have to substitute this with one having context `D<int>'. */
15572
15573 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15574 if (dependent_scope_p (context))
15575 {
15576 /* When rewriting a constructor into a deduction guide, a
15577 non-dependent name can become dependent, so memtmpl<args>
15578 becomes context::template memtmpl<args>. */
15579 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15580 return build_qualified_name (type, context, DECL_NAME (t),
15581 /*template*/true);
15582 }
15583 return lookup_field (context, DECL_NAME(t), 0, false);
15584 }
15585 else
15586 /* Ordinary template template argument. */
15587 return t;
15588
15589 case NON_LVALUE_EXPR:
15590 case VIEW_CONVERT_EXPR:
15591 {
15592 /* Handle location wrappers by substituting the wrapped node
15593 first, *then* reusing the resulting type. Doing the type
15594 first ensures that we handle template parameters and
15595 parameter pack expansions. */
15596 gcc_assert (location_wrapper_p (t));
15597 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15598 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15599 }
15600
15601 case CAST_EXPR:
15602 case REINTERPRET_CAST_EXPR:
15603 case CONST_CAST_EXPR:
15604 case STATIC_CAST_EXPR:
15605 case DYNAMIC_CAST_EXPR:
15606 case IMPLICIT_CONV_EXPR:
15607 case CONVERT_EXPR:
15608 case NOP_EXPR:
15609 {
15610 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15611 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15612 return build1 (code, type, op0);
15613 }
15614
15615 case SIZEOF_EXPR:
15616 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15617 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15618 {
15619 tree expanded, op = TREE_OPERAND (t, 0);
15620 int len = 0;
15621
15622 if (SIZEOF_EXPR_TYPE_P (t))
15623 op = TREE_TYPE (op);
15624
15625 ++cp_unevaluated_operand;
15626 ++c_inhibit_evaluation_warnings;
15627 /* We only want to compute the number of arguments. */
15628 if (PACK_EXPANSION_P (op))
15629 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15630 else
15631 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15632 args, complain, in_decl);
15633 --cp_unevaluated_operand;
15634 --c_inhibit_evaluation_warnings;
15635
15636 if (TREE_CODE (expanded) == TREE_VEC)
15637 {
15638 len = TREE_VEC_LENGTH (expanded);
15639 /* Set TREE_USED for the benefit of -Wunused. */
15640 for (int i = 0; i < len; i++)
15641 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15642 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15643 }
15644
15645 if (expanded == error_mark_node)
15646 return error_mark_node;
15647 else if (PACK_EXPANSION_P (expanded)
15648 || (TREE_CODE (expanded) == TREE_VEC
15649 && pack_expansion_args_count (expanded)))
15650
15651 {
15652 if (PACK_EXPANSION_P (expanded))
15653 /* OK. */;
15654 else if (TREE_VEC_LENGTH (expanded) == 1)
15655 expanded = TREE_VEC_ELT (expanded, 0);
15656 else
15657 expanded = make_argument_pack (expanded);
15658
15659 if (TYPE_P (expanded))
15660 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15661 false,
15662 complain & tf_error);
15663 else
15664 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15665 complain & tf_error);
15666 }
15667 else
15668 return build_int_cst (size_type_node, len);
15669 }
15670 if (SIZEOF_EXPR_TYPE_P (t))
15671 {
15672 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15673 args, complain, in_decl);
15674 r = build1 (NOP_EXPR, r, error_mark_node);
15675 r = build1 (SIZEOF_EXPR,
15676 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15677 SIZEOF_EXPR_TYPE_P (r) = 1;
15678 return r;
15679 }
15680 /* Fall through */
15681
15682 case INDIRECT_REF:
15683 case NEGATE_EXPR:
15684 case TRUTH_NOT_EXPR:
15685 case BIT_NOT_EXPR:
15686 case ADDR_EXPR:
15687 case UNARY_PLUS_EXPR: /* Unary + */
15688 case ALIGNOF_EXPR:
15689 case AT_ENCODE_EXPR:
15690 case ARROW_EXPR:
15691 case THROW_EXPR:
15692 case TYPEID_EXPR:
15693 case REALPART_EXPR:
15694 case IMAGPART_EXPR:
15695 case PAREN_EXPR:
15696 {
15697 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15698 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15699 r = build1 (code, type, op0);
15700 if (code == ALIGNOF_EXPR)
15701 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
15702 return r;
15703 }
15704
15705 case COMPONENT_REF:
15706 {
15707 tree object;
15708 tree name;
15709
15710 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15711 name = TREE_OPERAND (t, 1);
15712 if (TREE_CODE (name) == BIT_NOT_EXPR)
15713 {
15714 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15715 complain, in_decl);
15716 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15717 }
15718 else if (TREE_CODE (name) == SCOPE_REF
15719 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15720 {
15721 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15722 complain, in_decl);
15723 name = TREE_OPERAND (name, 1);
15724 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15725 complain, in_decl);
15726 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15727 name = build_qualified_name (/*type=*/NULL_TREE,
15728 base, name,
15729 /*template_p=*/false);
15730 }
15731 else if (BASELINK_P (name))
15732 name = tsubst_baselink (name,
15733 non_reference (TREE_TYPE (object)),
15734 args, complain,
15735 in_decl);
15736 else
15737 name = tsubst_copy (name, args, complain, in_decl);
15738 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15739 }
15740
15741 case PLUS_EXPR:
15742 case MINUS_EXPR:
15743 case MULT_EXPR:
15744 case TRUNC_DIV_EXPR:
15745 case CEIL_DIV_EXPR:
15746 case FLOOR_DIV_EXPR:
15747 case ROUND_DIV_EXPR:
15748 case EXACT_DIV_EXPR:
15749 case BIT_AND_EXPR:
15750 case BIT_IOR_EXPR:
15751 case BIT_XOR_EXPR:
15752 case TRUNC_MOD_EXPR:
15753 case FLOOR_MOD_EXPR:
15754 case TRUTH_ANDIF_EXPR:
15755 case TRUTH_ORIF_EXPR:
15756 case TRUTH_AND_EXPR:
15757 case TRUTH_OR_EXPR:
15758 case RSHIFT_EXPR:
15759 case LSHIFT_EXPR:
15760 case RROTATE_EXPR:
15761 case LROTATE_EXPR:
15762 case EQ_EXPR:
15763 case NE_EXPR:
15764 case MAX_EXPR:
15765 case MIN_EXPR:
15766 case LE_EXPR:
15767 case GE_EXPR:
15768 case LT_EXPR:
15769 case GT_EXPR:
15770 case COMPOUND_EXPR:
15771 case DOTSTAR_EXPR:
15772 case MEMBER_REF:
15773 case PREDECREMENT_EXPR:
15774 case PREINCREMENT_EXPR:
15775 case POSTDECREMENT_EXPR:
15776 case POSTINCREMENT_EXPR:
15777 {
15778 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15779 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15780 return build_nt (code, op0, op1);
15781 }
15782
15783 case SCOPE_REF:
15784 {
15785 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15786 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15787 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15788 QUALIFIED_NAME_IS_TEMPLATE (t));
15789 }
15790
15791 case ARRAY_REF:
15792 {
15793 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15794 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15795 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15796 }
15797
15798 case CALL_EXPR:
15799 {
15800 int n = VL_EXP_OPERAND_LENGTH (t);
15801 tree result = build_vl_exp (CALL_EXPR, n);
15802 int i;
15803 for (i = 0; i < n; i++)
15804 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15805 complain, in_decl);
15806 return result;
15807 }
15808
15809 case COND_EXPR:
15810 case MODOP_EXPR:
15811 case PSEUDO_DTOR_EXPR:
15812 case VEC_PERM_EXPR:
15813 {
15814 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15815 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15816 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15817 r = build_nt (code, op0, op1, op2);
15818 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15819 return r;
15820 }
15821
15822 case NEW_EXPR:
15823 {
15824 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15825 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15826 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15827 r = build_nt (code, op0, op1, op2);
15828 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15829 return r;
15830 }
15831
15832 case DELETE_EXPR:
15833 {
15834 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15835 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15836 r = build_nt (code, op0, op1);
15837 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15838 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15839 return r;
15840 }
15841
15842 case TEMPLATE_ID_EXPR:
15843 {
15844 /* Substituted template arguments */
15845 tree fn = TREE_OPERAND (t, 0);
15846 tree targs = TREE_OPERAND (t, 1);
15847
15848 fn = tsubst_copy (fn, args, complain, in_decl);
15849 if (targs)
15850 targs = tsubst_template_args (targs, args, complain, in_decl);
15851
15852 return lookup_template_function (fn, targs);
15853 }
15854
15855 case TREE_LIST:
15856 {
15857 tree purpose, value, chain;
15858
15859 if (t == void_list_node)
15860 return t;
15861
15862 purpose = TREE_PURPOSE (t);
15863 if (purpose)
15864 purpose = tsubst_copy (purpose, args, complain, in_decl);
15865 value = TREE_VALUE (t);
15866 if (value)
15867 value = tsubst_copy (value, args, complain, in_decl);
15868 chain = TREE_CHAIN (t);
15869 if (chain && chain != void_type_node)
15870 chain = tsubst_copy (chain, args, complain, in_decl);
15871 if (purpose == TREE_PURPOSE (t)
15872 && value == TREE_VALUE (t)
15873 && chain == TREE_CHAIN (t))
15874 return t;
15875 return tree_cons (purpose, value, chain);
15876 }
15877
15878 case RECORD_TYPE:
15879 case UNION_TYPE:
15880 case ENUMERAL_TYPE:
15881 case INTEGER_TYPE:
15882 case TEMPLATE_TYPE_PARM:
15883 case TEMPLATE_TEMPLATE_PARM:
15884 case BOUND_TEMPLATE_TEMPLATE_PARM:
15885 case TEMPLATE_PARM_INDEX:
15886 case POINTER_TYPE:
15887 case REFERENCE_TYPE:
15888 case OFFSET_TYPE:
15889 case FUNCTION_TYPE:
15890 case METHOD_TYPE:
15891 case ARRAY_TYPE:
15892 case TYPENAME_TYPE:
15893 case UNBOUND_CLASS_TEMPLATE:
15894 case TYPEOF_TYPE:
15895 case DECLTYPE_TYPE:
15896 case TYPE_DECL:
15897 return tsubst (t, args, complain, in_decl);
15898
15899 case USING_DECL:
15900 t = DECL_NAME (t);
15901 /* Fall through. */
15902 case IDENTIFIER_NODE:
15903 if (IDENTIFIER_CONV_OP_P (t))
15904 {
15905 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15906 return make_conv_op_name (new_type);
15907 }
15908 else
15909 return t;
15910
15911 case CONSTRUCTOR:
15912 /* This is handled by tsubst_copy_and_build. */
15913 gcc_unreachable ();
15914
15915 case VA_ARG_EXPR:
15916 {
15917 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15918 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15919 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15920 }
15921
15922 case CLEANUP_POINT_EXPR:
15923 /* We shouldn't have built any of these during initial template
15924 generation. Instead, they should be built during instantiation
15925 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15926 gcc_unreachable ();
15927
15928 case OFFSET_REF:
15929 {
15930 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15931 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15932 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15933 r = build2 (code, type, op0, op1);
15934 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15935 if (!mark_used (TREE_OPERAND (r, 1), complain)
15936 && !(complain & tf_error))
15937 return error_mark_node;
15938 return r;
15939 }
15940
15941 case EXPR_PACK_EXPANSION:
15942 error ("invalid use of pack expansion expression");
15943 return error_mark_node;
15944
15945 case NONTYPE_ARGUMENT_PACK:
15946 error ("use %<...%> to expand argument pack");
15947 return error_mark_node;
15948
15949 case VOID_CST:
15950 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15951 return t;
15952
15953 case INTEGER_CST:
15954 case REAL_CST:
15955 case STRING_CST:
15956 case COMPLEX_CST:
15957 {
15958 /* Instantiate any typedefs in the type. */
15959 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15960 r = fold_convert (type, t);
15961 gcc_assert (TREE_CODE (r) == code);
15962 return r;
15963 }
15964
15965 case PTRMEM_CST:
15966 /* These can sometimes show up in a partial instantiation, but never
15967 involve template parms. */
15968 gcc_assert (!uses_template_parms (t));
15969 return t;
15970
15971 case UNARY_LEFT_FOLD_EXPR:
15972 return tsubst_unary_left_fold (t, args, complain, in_decl);
15973 case UNARY_RIGHT_FOLD_EXPR:
15974 return tsubst_unary_right_fold (t, args, complain, in_decl);
15975 case BINARY_LEFT_FOLD_EXPR:
15976 return tsubst_binary_left_fold (t, args, complain, in_decl);
15977 case BINARY_RIGHT_FOLD_EXPR:
15978 return tsubst_binary_right_fold (t, args, complain, in_decl);
15979 case PREDICT_EXPR:
15980 return t;
15981
15982 case DEBUG_BEGIN_STMT:
15983 /* ??? There's no point in copying it for now, but maybe some
15984 day it will contain more information, such as a pointer back
15985 to the containing function, inlined copy or so. */
15986 return t;
15987
15988 default:
15989 /* We shouldn't get here, but keep going if !flag_checking. */
15990 if (flag_checking)
15991 gcc_unreachable ();
15992 return t;
15993 }
15994 }
15995
15996 /* Helper function for tsubst_omp_clauses, used for instantiation of
15997 OMP_CLAUSE_DECL of clauses. */
15998
15999 static tree
16000 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16001 tree in_decl)
16002 {
16003 if (decl == NULL_TREE)
16004 return NULL_TREE;
16005
16006 /* Handle an OpenMP array section represented as a TREE_LIST (or
16007 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16008 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16009 TREE_LIST. We can handle it exactly the same as an array section
16010 (purpose, value, and a chain), even though the nomenclature
16011 (low_bound, length, etc) is different. */
16012 if (TREE_CODE (decl) == TREE_LIST)
16013 {
16014 tree low_bound
16015 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16016 /*integral_constant_expression_p=*/false);
16017 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16018 /*integral_constant_expression_p=*/false);
16019 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16020 in_decl);
16021 if (TREE_PURPOSE (decl) == low_bound
16022 && TREE_VALUE (decl) == length
16023 && TREE_CHAIN (decl) == chain)
16024 return decl;
16025 tree ret = tree_cons (low_bound, length, chain);
16026 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16027 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16028 return ret;
16029 }
16030 tree ret = tsubst_expr (decl, args, complain, in_decl,
16031 /*integral_constant_expression_p=*/false);
16032 /* Undo convert_from_reference tsubst_expr could have called. */
16033 if (decl
16034 && REFERENCE_REF_P (ret)
16035 && !REFERENCE_REF_P (decl))
16036 ret = TREE_OPERAND (ret, 0);
16037 return ret;
16038 }
16039
16040 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16041
16042 static tree
16043 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16044 tree args, tsubst_flags_t complain, tree in_decl)
16045 {
16046 tree new_clauses = NULL_TREE, nc, oc;
16047 tree linear_no_step = NULL_TREE;
16048
16049 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16050 {
16051 nc = copy_node (oc);
16052 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16053 new_clauses = nc;
16054
16055 switch (OMP_CLAUSE_CODE (nc))
16056 {
16057 case OMP_CLAUSE_LASTPRIVATE:
16058 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16059 {
16060 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16061 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16062 in_decl, /*integral_constant_expression_p=*/false);
16063 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16064 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16065 }
16066 /* FALLTHRU */
16067 case OMP_CLAUSE_PRIVATE:
16068 case OMP_CLAUSE_SHARED:
16069 case OMP_CLAUSE_FIRSTPRIVATE:
16070 case OMP_CLAUSE_COPYIN:
16071 case OMP_CLAUSE_COPYPRIVATE:
16072 case OMP_CLAUSE_UNIFORM:
16073 case OMP_CLAUSE_DEPEND:
16074 case OMP_CLAUSE_FROM:
16075 case OMP_CLAUSE_TO:
16076 case OMP_CLAUSE_MAP:
16077 case OMP_CLAUSE_USE_DEVICE_PTR:
16078 case OMP_CLAUSE_IS_DEVICE_PTR:
16079 OMP_CLAUSE_DECL (nc)
16080 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16081 in_decl);
16082 break;
16083 case OMP_CLAUSE_TILE:
16084 case OMP_CLAUSE_IF:
16085 case OMP_CLAUSE_NUM_THREADS:
16086 case OMP_CLAUSE_SCHEDULE:
16087 case OMP_CLAUSE_COLLAPSE:
16088 case OMP_CLAUSE_FINAL:
16089 case OMP_CLAUSE_DEVICE:
16090 case OMP_CLAUSE_DIST_SCHEDULE:
16091 case OMP_CLAUSE_NUM_TEAMS:
16092 case OMP_CLAUSE_THREAD_LIMIT:
16093 case OMP_CLAUSE_SAFELEN:
16094 case OMP_CLAUSE_SIMDLEN:
16095 case OMP_CLAUSE_NUM_TASKS:
16096 case OMP_CLAUSE_GRAINSIZE:
16097 case OMP_CLAUSE_PRIORITY:
16098 case OMP_CLAUSE_ORDERED:
16099 case OMP_CLAUSE_HINT:
16100 case OMP_CLAUSE_NUM_GANGS:
16101 case OMP_CLAUSE_NUM_WORKERS:
16102 case OMP_CLAUSE_VECTOR_LENGTH:
16103 case OMP_CLAUSE_WORKER:
16104 case OMP_CLAUSE_VECTOR:
16105 case OMP_CLAUSE_ASYNC:
16106 case OMP_CLAUSE_WAIT:
16107 OMP_CLAUSE_OPERAND (nc, 0)
16108 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16109 in_decl, /*integral_constant_expression_p=*/false);
16110 break;
16111 case OMP_CLAUSE_REDUCTION:
16112 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16113 {
16114 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16115 if (TREE_CODE (placeholder) == SCOPE_REF)
16116 {
16117 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16118 complain, in_decl);
16119 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16120 = build_qualified_name (NULL_TREE, scope,
16121 TREE_OPERAND (placeholder, 1),
16122 false);
16123 }
16124 else
16125 gcc_assert (identifier_p (placeholder));
16126 }
16127 OMP_CLAUSE_DECL (nc)
16128 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16129 in_decl);
16130 break;
16131 case OMP_CLAUSE_GANG:
16132 case OMP_CLAUSE_ALIGNED:
16133 OMP_CLAUSE_DECL (nc)
16134 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16135 in_decl);
16136 OMP_CLAUSE_OPERAND (nc, 1)
16137 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16138 in_decl, /*integral_constant_expression_p=*/false);
16139 break;
16140 case OMP_CLAUSE_LINEAR:
16141 OMP_CLAUSE_DECL (nc)
16142 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16143 in_decl);
16144 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16145 {
16146 gcc_assert (!linear_no_step);
16147 linear_no_step = nc;
16148 }
16149 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16150 OMP_CLAUSE_LINEAR_STEP (nc)
16151 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16152 complain, in_decl);
16153 else
16154 OMP_CLAUSE_LINEAR_STEP (nc)
16155 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16156 in_decl,
16157 /*integral_constant_expression_p=*/false);
16158 break;
16159 case OMP_CLAUSE_NOWAIT:
16160 case OMP_CLAUSE_DEFAULT:
16161 case OMP_CLAUSE_UNTIED:
16162 case OMP_CLAUSE_MERGEABLE:
16163 case OMP_CLAUSE_INBRANCH:
16164 case OMP_CLAUSE_NOTINBRANCH:
16165 case OMP_CLAUSE_PROC_BIND:
16166 case OMP_CLAUSE_FOR:
16167 case OMP_CLAUSE_PARALLEL:
16168 case OMP_CLAUSE_SECTIONS:
16169 case OMP_CLAUSE_TASKGROUP:
16170 case OMP_CLAUSE_NOGROUP:
16171 case OMP_CLAUSE_THREADS:
16172 case OMP_CLAUSE_SIMD:
16173 case OMP_CLAUSE_DEFAULTMAP:
16174 case OMP_CLAUSE_INDEPENDENT:
16175 case OMP_CLAUSE_AUTO:
16176 case OMP_CLAUSE_SEQ:
16177 case OMP_CLAUSE_IF_PRESENT:
16178 case OMP_CLAUSE_FINALIZE:
16179 break;
16180 default:
16181 gcc_unreachable ();
16182 }
16183 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16184 switch (OMP_CLAUSE_CODE (nc))
16185 {
16186 case OMP_CLAUSE_SHARED:
16187 case OMP_CLAUSE_PRIVATE:
16188 case OMP_CLAUSE_FIRSTPRIVATE:
16189 case OMP_CLAUSE_LASTPRIVATE:
16190 case OMP_CLAUSE_COPYPRIVATE:
16191 case OMP_CLAUSE_LINEAR:
16192 case OMP_CLAUSE_REDUCTION:
16193 case OMP_CLAUSE_USE_DEVICE_PTR:
16194 case OMP_CLAUSE_IS_DEVICE_PTR:
16195 /* tsubst_expr on SCOPE_REF results in returning
16196 finish_non_static_data_member result. Undo that here. */
16197 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16198 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16199 == IDENTIFIER_NODE))
16200 {
16201 tree t = OMP_CLAUSE_DECL (nc);
16202 tree v = t;
16203 while (v)
16204 switch (TREE_CODE (v))
16205 {
16206 case COMPONENT_REF:
16207 case MEM_REF:
16208 case INDIRECT_REF:
16209 CASE_CONVERT:
16210 case POINTER_PLUS_EXPR:
16211 v = TREE_OPERAND (v, 0);
16212 continue;
16213 case PARM_DECL:
16214 if (DECL_CONTEXT (v) == current_function_decl
16215 && DECL_ARTIFICIAL (v)
16216 && DECL_NAME (v) == this_identifier)
16217 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16218 /* FALLTHRU */
16219 default:
16220 v = NULL_TREE;
16221 break;
16222 }
16223 }
16224 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16225 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16226 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16227 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16228 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16229 {
16230 tree decl = OMP_CLAUSE_DECL (nc);
16231 if (VAR_P (decl))
16232 {
16233 retrofit_lang_decl (decl);
16234 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16235 }
16236 }
16237 break;
16238 default:
16239 break;
16240 }
16241 }
16242
16243 new_clauses = nreverse (new_clauses);
16244 if (ort != C_ORT_OMP_DECLARE_SIMD)
16245 {
16246 new_clauses = finish_omp_clauses (new_clauses, ort);
16247 if (linear_no_step)
16248 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16249 if (nc == linear_no_step)
16250 {
16251 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16252 break;
16253 }
16254 }
16255 return new_clauses;
16256 }
16257
16258 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16259
16260 static tree
16261 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
16262 tree in_decl)
16263 {
16264 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16265
16266 tree purpose, value, chain;
16267
16268 if (t == NULL)
16269 return t;
16270
16271 if (TREE_CODE (t) != TREE_LIST)
16272 return tsubst_copy_and_build (t, args, complain, in_decl,
16273 /*function_p=*/false,
16274 /*integral_constant_expression_p=*/false);
16275
16276 if (t == void_list_node)
16277 return t;
16278
16279 purpose = TREE_PURPOSE (t);
16280 if (purpose)
16281 purpose = RECUR (purpose);
16282 value = TREE_VALUE (t);
16283 if (value)
16284 {
16285 if (TREE_CODE (value) != LABEL_DECL)
16286 value = RECUR (value);
16287 else
16288 {
16289 value = lookup_label (DECL_NAME (value));
16290 gcc_assert (TREE_CODE (value) == LABEL_DECL);
16291 TREE_USED (value) = 1;
16292 }
16293 }
16294 chain = TREE_CHAIN (t);
16295 if (chain && chain != void_type_node)
16296 chain = RECUR (chain);
16297 return tree_cons (purpose, value, chain);
16298 #undef RECUR
16299 }
16300
16301 /* Used to temporarily communicate the list of #pragma omp parallel
16302 clauses to #pragma omp for instantiation if they are combined
16303 together. */
16304
16305 static tree *omp_parallel_combined_clauses;
16306
16307 /* Substitute one OMP_FOR iterator. */
16308
16309 static void
16310 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
16311 tree initv, tree condv, tree incrv, tree *clauses,
16312 tree args, tsubst_flags_t complain, tree in_decl,
16313 bool integral_constant_expression_p)
16314 {
16315 #define RECUR(NODE) \
16316 tsubst_expr ((NODE), args, complain, in_decl, \
16317 integral_constant_expression_p)
16318 tree decl, init, cond, incr;
16319
16320 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
16321 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
16322
16323 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
16324 {
16325 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
16326 if (TREE_CODE (o) == TREE_LIST)
16327 TREE_VEC_ELT (orig_declv, i)
16328 = tree_cons (RECUR (TREE_PURPOSE (o)),
16329 RECUR (TREE_VALUE (o)), NULL_TREE);
16330 else
16331 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
16332 }
16333
16334 decl = TREE_OPERAND (init, 0);
16335 init = TREE_OPERAND (init, 1);
16336 tree decl_expr = NULL_TREE;
16337 if (init && TREE_CODE (init) == DECL_EXPR)
16338 {
16339 /* We need to jump through some hoops to handle declarations in the
16340 init-statement, since we might need to handle auto deduction,
16341 but we need to keep control of initialization. */
16342 decl_expr = init;
16343 init = DECL_INITIAL (DECL_EXPR_DECL (init));
16344 decl = tsubst_decl (decl, args, complain);
16345 }
16346 else
16347 {
16348 if (TREE_CODE (decl) == SCOPE_REF)
16349 {
16350 decl = RECUR (decl);
16351 if (TREE_CODE (decl) == COMPONENT_REF)
16352 {
16353 tree v = decl;
16354 while (v)
16355 switch (TREE_CODE (v))
16356 {
16357 case COMPONENT_REF:
16358 case MEM_REF:
16359 case INDIRECT_REF:
16360 CASE_CONVERT:
16361 case POINTER_PLUS_EXPR:
16362 v = TREE_OPERAND (v, 0);
16363 continue;
16364 case PARM_DECL:
16365 if (DECL_CONTEXT (v) == current_function_decl
16366 && DECL_ARTIFICIAL (v)
16367 && DECL_NAME (v) == this_identifier)
16368 {
16369 decl = TREE_OPERAND (decl, 1);
16370 decl = omp_privatize_field (decl, false);
16371 }
16372 /* FALLTHRU */
16373 default:
16374 v = NULL_TREE;
16375 break;
16376 }
16377 }
16378 }
16379 else
16380 decl = RECUR (decl);
16381 }
16382 init = RECUR (init);
16383
16384 tree auto_node = type_uses_auto (TREE_TYPE (decl));
16385 if (auto_node && init)
16386 TREE_TYPE (decl)
16387 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
16388
16389 gcc_assert (!type_dependent_expression_p (decl));
16390
16391 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16392 {
16393 if (decl_expr)
16394 {
16395 /* Declare the variable, but don't let that initialize it. */
16396 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
16397 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
16398 RECUR (decl_expr);
16399 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
16400 }
16401
16402 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
16403 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16404 if (TREE_CODE (incr) == MODIFY_EXPR)
16405 {
16406 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16407 tree rhs = RECUR (TREE_OPERAND (incr, 1));
16408 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
16409 NOP_EXPR, rhs, complain);
16410 }
16411 else
16412 incr = RECUR (incr);
16413 TREE_VEC_ELT (declv, i) = decl;
16414 TREE_VEC_ELT (initv, i) = init;
16415 TREE_VEC_ELT (condv, i) = cond;
16416 TREE_VEC_ELT (incrv, i) = incr;
16417 return;
16418 }
16419
16420 if (decl_expr)
16421 {
16422 /* Declare and initialize the variable. */
16423 RECUR (decl_expr);
16424 init = NULL_TREE;
16425 }
16426 else if (init)
16427 {
16428 tree *pc;
16429 int j;
16430 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
16431 {
16432 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16433 {
16434 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16435 && OMP_CLAUSE_DECL (*pc) == decl)
16436 break;
16437 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16438 && OMP_CLAUSE_DECL (*pc) == decl)
16439 {
16440 if (j)
16441 break;
16442 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16443 tree c = *pc;
16444 *pc = OMP_CLAUSE_CHAIN (c);
16445 OMP_CLAUSE_CHAIN (c) = *clauses;
16446 *clauses = c;
16447 }
16448 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16449 && OMP_CLAUSE_DECL (*pc) == decl)
16450 {
16451 error ("iteration variable %qD should not be firstprivate",
16452 decl);
16453 *pc = OMP_CLAUSE_CHAIN (*pc);
16454 }
16455 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16456 && OMP_CLAUSE_DECL (*pc) == decl)
16457 {
16458 error ("iteration variable %qD should not be reduction",
16459 decl);
16460 *pc = OMP_CLAUSE_CHAIN (*pc);
16461 }
16462 else
16463 pc = &OMP_CLAUSE_CHAIN (*pc);
16464 }
16465 if (*pc)
16466 break;
16467 }
16468 if (*pc == NULL_TREE)
16469 {
16470 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16471 OMP_CLAUSE_DECL (c) = decl;
16472 c = finish_omp_clauses (c, C_ORT_OMP);
16473 if (c)
16474 {
16475 OMP_CLAUSE_CHAIN (c) = *clauses;
16476 *clauses = c;
16477 }
16478 }
16479 }
16480 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16481 if (COMPARISON_CLASS_P (cond))
16482 {
16483 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16484 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16485 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16486 }
16487 else
16488 cond = RECUR (cond);
16489 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16490 switch (TREE_CODE (incr))
16491 {
16492 case PREINCREMENT_EXPR:
16493 case PREDECREMENT_EXPR:
16494 case POSTINCREMENT_EXPR:
16495 case POSTDECREMENT_EXPR:
16496 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16497 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16498 break;
16499 case MODIFY_EXPR:
16500 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16501 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16502 {
16503 tree rhs = TREE_OPERAND (incr, 1);
16504 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16505 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16506 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16507 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16508 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16509 rhs0, rhs1));
16510 }
16511 else
16512 incr = RECUR (incr);
16513 break;
16514 case MODOP_EXPR:
16515 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16516 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16517 {
16518 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16519 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16520 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16521 TREE_TYPE (decl), lhs,
16522 RECUR (TREE_OPERAND (incr, 2))));
16523 }
16524 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16525 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16526 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16527 {
16528 tree rhs = TREE_OPERAND (incr, 2);
16529 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16530 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16531 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16532 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16533 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16534 rhs0, rhs1));
16535 }
16536 else
16537 incr = RECUR (incr);
16538 break;
16539 default:
16540 incr = RECUR (incr);
16541 break;
16542 }
16543
16544 TREE_VEC_ELT (declv, i) = decl;
16545 TREE_VEC_ELT (initv, i) = init;
16546 TREE_VEC_ELT (condv, i) = cond;
16547 TREE_VEC_ELT (incrv, i) = incr;
16548 #undef RECUR
16549 }
16550
16551 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16552 of OMP_TARGET's body. */
16553
16554 static tree
16555 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16556 {
16557 *walk_subtrees = 0;
16558 switch (TREE_CODE (*tp))
16559 {
16560 case OMP_TEAMS:
16561 return *tp;
16562 case BIND_EXPR:
16563 case STATEMENT_LIST:
16564 *walk_subtrees = 1;
16565 break;
16566 default:
16567 break;
16568 }
16569 return NULL_TREE;
16570 }
16571
16572 /* Helper function for tsubst_expr. For decomposition declaration
16573 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16574 also the corresponding decls representing the identifiers
16575 of the decomposition declaration. Return DECL if successful
16576 or error_mark_node otherwise, set *FIRST to the first decl
16577 in the list chained through DECL_CHAIN and *CNT to the number
16578 of such decls. */
16579
16580 static tree
16581 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16582 tsubst_flags_t complain, tree in_decl, tree *first,
16583 unsigned int *cnt)
16584 {
16585 tree decl2, decl3, prev = decl;
16586 *cnt = 0;
16587 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16588 for (decl2 = DECL_CHAIN (pattern_decl);
16589 decl2
16590 && VAR_P (decl2)
16591 && DECL_DECOMPOSITION_P (decl2)
16592 && DECL_NAME (decl2);
16593 decl2 = DECL_CHAIN (decl2))
16594 {
16595 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16596 {
16597 gcc_assert (errorcount);
16598 return error_mark_node;
16599 }
16600 (*cnt)++;
16601 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16602 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16603 tree v = DECL_VALUE_EXPR (decl2);
16604 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16605 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16606 decl3 = tsubst (decl2, args, complain, in_decl);
16607 SET_DECL_VALUE_EXPR (decl2, v);
16608 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16609 if (VAR_P (decl3))
16610 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16611 else
16612 {
16613 gcc_assert (errorcount);
16614 decl = error_mark_node;
16615 continue;
16616 }
16617 maybe_push_decl (decl3);
16618 if (error_operand_p (decl3))
16619 decl = error_mark_node;
16620 else if (decl != error_mark_node
16621 && DECL_CHAIN (decl3) != prev
16622 && decl != prev)
16623 {
16624 gcc_assert (errorcount);
16625 decl = error_mark_node;
16626 }
16627 else
16628 prev = decl3;
16629 }
16630 *first = prev;
16631 return decl;
16632 }
16633
16634 /* Like tsubst_copy for expressions, etc. but also does semantic
16635 processing. */
16636
16637 tree
16638 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16639 bool integral_constant_expression_p)
16640 {
16641 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16642 #define RECUR(NODE) \
16643 tsubst_expr ((NODE), args, complain, in_decl, \
16644 integral_constant_expression_p)
16645
16646 tree stmt, tmp;
16647 tree r;
16648 location_t loc;
16649
16650 if (t == NULL_TREE || t == error_mark_node)
16651 return t;
16652
16653 loc = input_location;
16654 if (location_t eloc = cp_expr_location (t))
16655 input_location = eloc;
16656 if (STATEMENT_CODE_P (TREE_CODE (t)))
16657 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16658
16659 switch (TREE_CODE (t))
16660 {
16661 case STATEMENT_LIST:
16662 {
16663 tree_stmt_iterator i;
16664 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16665 RECUR (tsi_stmt (i));
16666 break;
16667 }
16668
16669 case CTOR_INITIALIZER:
16670 finish_mem_initializers (tsubst_initializer_list
16671 (TREE_OPERAND (t, 0), args));
16672 break;
16673
16674 case RETURN_EXPR:
16675 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16676 break;
16677
16678 case EXPR_STMT:
16679 tmp = RECUR (EXPR_STMT_EXPR (t));
16680 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16681 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16682 else
16683 finish_expr_stmt (tmp);
16684 break;
16685
16686 case USING_STMT:
16687 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16688 /*attribs=*/NULL_TREE);
16689 break;
16690
16691 case DECL_EXPR:
16692 {
16693 tree decl, pattern_decl;
16694 tree init;
16695
16696 pattern_decl = decl = DECL_EXPR_DECL (t);
16697 if (TREE_CODE (decl) == LABEL_DECL)
16698 finish_label_decl (DECL_NAME (decl));
16699 else if (TREE_CODE (decl) == USING_DECL)
16700 {
16701 tree scope = USING_DECL_SCOPE (decl);
16702 tree name = DECL_NAME (decl);
16703
16704 scope = tsubst (scope, args, complain, in_decl);
16705 decl = lookup_qualified_name (scope, name,
16706 /*is_type_p=*/false,
16707 /*complain=*/false);
16708 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16709 qualified_name_lookup_error (scope, name, decl, input_location);
16710 else
16711 finish_local_using_decl (decl, scope, name);
16712 }
16713 else if (is_capture_proxy (decl)
16714 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16715 {
16716 /* We're in tsubst_lambda_expr, we've already inserted a new
16717 capture proxy, so look it up and register it. */
16718 tree inst;
16719 if (DECL_PACK_P (decl))
16720 {
16721 inst = (retrieve_local_specialization
16722 (DECL_CAPTURED_VARIABLE (decl)));
16723 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16724 }
16725 else
16726 {
16727 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16728 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16729 gcc_assert (inst != decl && is_capture_proxy (inst));
16730 }
16731 register_local_specialization (inst, decl);
16732 break;
16733 }
16734 else if (DECL_PRETTY_FUNCTION_P (decl))
16735 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
16736 DECL_NAME (decl),
16737 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
16738 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16739 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16740 /* Don't copy the old closure; we'll create a new one in
16741 tsubst_lambda_expr. */
16742 break;
16743 else
16744 {
16745 init = DECL_INITIAL (decl);
16746 decl = tsubst (decl, args, complain, in_decl);
16747 if (decl != error_mark_node)
16748 {
16749 /* By marking the declaration as instantiated, we avoid
16750 trying to instantiate it. Since instantiate_decl can't
16751 handle local variables, and since we've already done
16752 all that needs to be done, that's the right thing to
16753 do. */
16754 if (VAR_P (decl))
16755 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16756 if (VAR_P (decl) && !DECL_NAME (decl)
16757 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16758 /* Anonymous aggregates are a special case. */
16759 finish_anon_union (decl);
16760 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16761 {
16762 DECL_CONTEXT (decl) = current_function_decl;
16763 if (DECL_NAME (decl) == this_identifier)
16764 {
16765 tree lam = DECL_CONTEXT (current_function_decl);
16766 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16767 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16768 }
16769 insert_capture_proxy (decl);
16770 }
16771 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16772 /* We already did a pushtag. */;
16773 else if (TREE_CODE (decl) == FUNCTION_DECL
16774 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16775 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16776 {
16777 DECL_CONTEXT (decl) = NULL_TREE;
16778 pushdecl (decl);
16779 DECL_CONTEXT (decl) = current_function_decl;
16780 cp_check_omp_declare_reduction (decl);
16781 }
16782 else
16783 {
16784 int const_init = false;
16785 unsigned int cnt = 0;
16786 tree first = NULL_TREE, ndecl = error_mark_node;
16787 maybe_push_decl (decl);
16788
16789 if (VAR_P (decl)
16790 && DECL_DECOMPOSITION_P (decl)
16791 && TREE_TYPE (pattern_decl) != error_mark_node)
16792 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
16793 complain, in_decl, &first,
16794 &cnt);
16795
16796 init = tsubst_init (init, decl, args, complain, in_decl);
16797
16798 if (VAR_P (decl))
16799 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16800 (pattern_decl));
16801
16802 if (ndecl != error_mark_node)
16803 cp_maybe_mangle_decomp (ndecl, first, cnt);
16804
16805 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16806
16807 if (ndecl != error_mark_node)
16808 cp_finish_decomp (ndecl, first, cnt);
16809 }
16810 }
16811 }
16812
16813 break;
16814 }
16815
16816 case FOR_STMT:
16817 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16818 RECUR (FOR_INIT_STMT (t));
16819 finish_init_stmt (stmt);
16820 tmp = RECUR (FOR_COND (t));
16821 finish_for_cond (tmp, stmt, false, 0);
16822 tmp = RECUR (FOR_EXPR (t));
16823 finish_for_expr (tmp, stmt);
16824 {
16825 bool prev = note_iteration_stmt_body_start ();
16826 RECUR (FOR_BODY (t));
16827 note_iteration_stmt_body_end (prev);
16828 }
16829 finish_for_stmt (stmt);
16830 break;
16831
16832 case RANGE_FOR_STMT:
16833 {
16834 /* Construct another range_for, if this is not a final
16835 substitution (for inside inside a generic lambda of a
16836 template). Otherwise convert to a regular for. */
16837 tree decl, expr;
16838 stmt = (processing_template_decl
16839 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16840 : begin_for_stmt (NULL_TREE, NULL_TREE));
16841 RECUR (RANGE_FOR_INIT_STMT (t));
16842 decl = RANGE_FOR_DECL (t);
16843 decl = tsubst (decl, args, complain, in_decl);
16844 maybe_push_decl (decl);
16845 expr = RECUR (RANGE_FOR_EXPR (t));
16846
16847 tree decomp_first = NULL_TREE;
16848 unsigned decomp_cnt = 0;
16849 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16850 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16851 complain, in_decl,
16852 &decomp_first, &decomp_cnt);
16853
16854 if (processing_template_decl)
16855 {
16856 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16857 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16858 finish_range_for_decl (stmt, decl, expr);
16859 if (decomp_first && decl != error_mark_node)
16860 cp_finish_decomp (decl, decomp_first, decomp_cnt);
16861 }
16862 else
16863 {
16864 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16865 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16866 stmt = cp_convert_range_for (stmt, decl, expr,
16867 decomp_first, decomp_cnt,
16868 RANGE_FOR_IVDEP (t), unroll);
16869 }
16870
16871 bool prev = note_iteration_stmt_body_start ();
16872 RECUR (RANGE_FOR_BODY (t));
16873 note_iteration_stmt_body_end (prev);
16874 finish_for_stmt (stmt);
16875 }
16876 break;
16877
16878 case WHILE_STMT:
16879 stmt = begin_while_stmt ();
16880 tmp = RECUR (WHILE_COND (t));
16881 finish_while_stmt_cond (tmp, stmt, false, 0);
16882 {
16883 bool prev = note_iteration_stmt_body_start ();
16884 RECUR (WHILE_BODY (t));
16885 note_iteration_stmt_body_end (prev);
16886 }
16887 finish_while_stmt (stmt);
16888 break;
16889
16890 case DO_STMT:
16891 stmt = begin_do_stmt ();
16892 {
16893 bool prev = note_iteration_stmt_body_start ();
16894 RECUR (DO_BODY (t));
16895 note_iteration_stmt_body_end (prev);
16896 }
16897 finish_do_body (stmt);
16898 tmp = RECUR (DO_COND (t));
16899 finish_do_stmt (tmp, stmt, false, 0);
16900 break;
16901
16902 case IF_STMT:
16903 stmt = begin_if_stmt ();
16904 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16905 if (IF_STMT_CONSTEXPR_P (t))
16906 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
16907 tmp = RECUR (IF_COND (t));
16908 tmp = finish_if_stmt_cond (tmp, stmt);
16909 if (IF_STMT_CONSTEXPR_P (t)
16910 && instantiation_dependent_expression_p (tmp))
16911 {
16912 /* We're partially instantiating a generic lambda, but the condition
16913 of the constexpr if is still dependent. Don't substitute into the
16914 branches now, just remember the template arguments. */
16915 do_poplevel (IF_SCOPE (stmt));
16916 IF_COND (stmt) = IF_COND (t);
16917 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
16918 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
16919 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
16920 add_stmt (stmt);
16921 break;
16922 }
16923 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16924 /* Don't instantiate the THEN_CLAUSE. */;
16925 else
16926 {
16927 tree folded = fold_non_dependent_expr (tmp, complain);
16928 bool inhibit = integer_zerop (folded);
16929 if (inhibit)
16930 ++c_inhibit_evaluation_warnings;
16931 RECUR (THEN_CLAUSE (t));
16932 if (inhibit)
16933 --c_inhibit_evaluation_warnings;
16934 }
16935 finish_then_clause (stmt);
16936
16937 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16938 /* Don't instantiate the ELSE_CLAUSE. */;
16939 else if (ELSE_CLAUSE (t))
16940 {
16941 tree folded = fold_non_dependent_expr (tmp, complain);
16942 bool inhibit = integer_nonzerop (folded);
16943 begin_else_clause (stmt);
16944 if (inhibit)
16945 ++c_inhibit_evaluation_warnings;
16946 RECUR (ELSE_CLAUSE (t));
16947 if (inhibit)
16948 --c_inhibit_evaluation_warnings;
16949 finish_else_clause (stmt);
16950 }
16951
16952 finish_if_stmt (stmt);
16953 break;
16954
16955 case BIND_EXPR:
16956 if (BIND_EXPR_BODY_BLOCK (t))
16957 stmt = begin_function_body ();
16958 else
16959 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16960 ? BCS_TRY_BLOCK : 0);
16961
16962 RECUR (BIND_EXPR_BODY (t));
16963
16964 if (BIND_EXPR_BODY_BLOCK (t))
16965 finish_function_body (stmt);
16966 else
16967 finish_compound_stmt (stmt);
16968 break;
16969
16970 case BREAK_STMT:
16971 finish_break_stmt ();
16972 break;
16973
16974 case CONTINUE_STMT:
16975 finish_continue_stmt ();
16976 break;
16977
16978 case SWITCH_STMT:
16979 stmt = begin_switch_stmt ();
16980 tmp = RECUR (SWITCH_STMT_COND (t));
16981 finish_switch_cond (tmp, stmt);
16982 RECUR (SWITCH_STMT_BODY (t));
16983 finish_switch_stmt (stmt);
16984 break;
16985
16986 case CASE_LABEL_EXPR:
16987 {
16988 tree low = RECUR (CASE_LOW (t));
16989 tree high = RECUR (CASE_HIGH (t));
16990 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16991 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16992 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16993 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16994 }
16995 break;
16996
16997 case LABEL_EXPR:
16998 {
16999 tree decl = LABEL_EXPR_LABEL (t);
17000 tree label;
17001
17002 label = finish_label_stmt (DECL_NAME (decl));
17003 if (TREE_CODE (label) == LABEL_DECL)
17004 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17005 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17006 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17007 }
17008 break;
17009
17010 case GOTO_EXPR:
17011 tmp = GOTO_DESTINATION (t);
17012 if (TREE_CODE (tmp) != LABEL_DECL)
17013 /* Computed goto's must be tsubst'd into. On the other hand,
17014 non-computed gotos must not be; the identifier in question
17015 will have no binding. */
17016 tmp = RECUR (tmp);
17017 else
17018 tmp = DECL_NAME (tmp);
17019 finish_goto_stmt (tmp);
17020 break;
17021
17022 case ASM_EXPR:
17023 {
17024 tree string = RECUR (ASM_STRING (t));
17025 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17026 complain, in_decl);
17027 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17028 complain, in_decl);
17029 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17030 complain, in_decl);
17031 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17032 complain, in_decl);
17033 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
17034 clobbers, labels);
17035 tree asm_expr = tmp;
17036 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17037 asm_expr = TREE_OPERAND (asm_expr, 0);
17038 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17039 }
17040 break;
17041
17042 case TRY_BLOCK:
17043 if (CLEANUP_P (t))
17044 {
17045 stmt = begin_try_block ();
17046 RECUR (TRY_STMTS (t));
17047 finish_cleanup_try_block (stmt);
17048 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17049 }
17050 else
17051 {
17052 tree compound_stmt = NULL_TREE;
17053
17054 if (FN_TRY_BLOCK_P (t))
17055 stmt = begin_function_try_block (&compound_stmt);
17056 else
17057 stmt = begin_try_block ();
17058
17059 RECUR (TRY_STMTS (t));
17060
17061 if (FN_TRY_BLOCK_P (t))
17062 finish_function_try_block (stmt);
17063 else
17064 finish_try_block (stmt);
17065
17066 RECUR (TRY_HANDLERS (t));
17067 if (FN_TRY_BLOCK_P (t))
17068 finish_function_handler_sequence (stmt, compound_stmt);
17069 else
17070 finish_handler_sequence (stmt);
17071 }
17072 break;
17073
17074 case HANDLER:
17075 {
17076 tree decl = HANDLER_PARMS (t);
17077
17078 if (decl)
17079 {
17080 decl = tsubst (decl, args, complain, in_decl);
17081 /* Prevent instantiate_decl from trying to instantiate
17082 this variable. We've already done all that needs to be
17083 done. */
17084 if (decl != error_mark_node)
17085 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17086 }
17087 stmt = begin_handler ();
17088 finish_handler_parms (decl, stmt);
17089 RECUR (HANDLER_BODY (t));
17090 finish_handler (stmt);
17091 }
17092 break;
17093
17094 case TAG_DEFN:
17095 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17096 if (CLASS_TYPE_P (tmp))
17097 {
17098 /* Local classes are not independent templates; they are
17099 instantiated along with their containing function. And this
17100 way we don't have to deal with pushing out of one local class
17101 to instantiate a member of another local class. */
17102 /* Closures are handled by the LAMBDA_EXPR. */
17103 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17104 complete_type (tmp);
17105 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17106 if ((VAR_P (fld)
17107 || (TREE_CODE (fld) == FUNCTION_DECL
17108 && !DECL_ARTIFICIAL (fld)))
17109 && DECL_TEMPLATE_INSTANTIATION (fld))
17110 instantiate_decl (fld, /*defer_ok=*/false,
17111 /*expl_inst_class=*/false);
17112 }
17113 break;
17114
17115 case STATIC_ASSERT:
17116 {
17117 tree condition;
17118
17119 ++c_inhibit_evaluation_warnings;
17120 condition =
17121 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17122 args,
17123 complain, in_decl,
17124 /*integral_constant_expression_p=*/true);
17125 --c_inhibit_evaluation_warnings;
17126
17127 finish_static_assert (condition,
17128 STATIC_ASSERT_MESSAGE (t),
17129 STATIC_ASSERT_SOURCE_LOCATION (t),
17130 /*member_p=*/false);
17131 }
17132 break;
17133
17134 case OACC_KERNELS:
17135 case OACC_PARALLEL:
17136 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
17137 in_decl);
17138 stmt = begin_omp_parallel ();
17139 RECUR (OMP_BODY (t));
17140 finish_omp_construct (TREE_CODE (t), stmt, tmp);
17141 break;
17142
17143 case OMP_PARALLEL:
17144 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
17145 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
17146 complain, in_decl);
17147 if (OMP_PARALLEL_COMBINED (t))
17148 omp_parallel_combined_clauses = &tmp;
17149 stmt = begin_omp_parallel ();
17150 RECUR (OMP_PARALLEL_BODY (t));
17151 gcc_assert (omp_parallel_combined_clauses == NULL);
17152 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
17153 = OMP_PARALLEL_COMBINED (t);
17154 pop_omp_privatization_clauses (r);
17155 break;
17156
17157 case OMP_TASK:
17158 r = push_omp_privatization_clauses (false);
17159 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17160 complain, in_decl);
17161 stmt = begin_omp_task ();
17162 RECUR (OMP_TASK_BODY (t));
17163 finish_omp_task (tmp, stmt);
17164 pop_omp_privatization_clauses (r);
17165 break;
17166
17167 case OMP_FOR:
17168 case OMP_SIMD:
17169 case OMP_DISTRIBUTE:
17170 case OMP_TASKLOOP:
17171 case OACC_LOOP:
17172 {
17173 tree clauses, body, pre_body;
17174 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
17175 tree orig_declv = NULL_TREE;
17176 tree incrv = NULL_TREE;
17177 enum c_omp_region_type ort = C_ORT_OMP;
17178 int i;
17179
17180 if (TREE_CODE (t) == OACC_LOOP)
17181 ort = C_ORT_ACC;
17182
17183 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
17184 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
17185 in_decl);
17186 if (OMP_FOR_INIT (t) != NULL_TREE)
17187 {
17188 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17189 if (OMP_FOR_ORIG_DECLS (t))
17190 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17191 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17192 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17193 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17194 }
17195
17196 stmt = begin_omp_structured_block ();
17197
17198 pre_body = push_stmt_list ();
17199 RECUR (OMP_FOR_PRE_BODY (t));
17200 pre_body = pop_stmt_list (pre_body);
17201
17202 if (OMP_FOR_INIT (t) != NULL_TREE)
17203 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17204 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
17205 incrv, &clauses, args, complain, in_decl,
17206 integral_constant_expression_p);
17207 omp_parallel_combined_clauses = NULL;
17208
17209 body = push_stmt_list ();
17210 RECUR (OMP_FOR_BODY (t));
17211 body = pop_stmt_list (body);
17212
17213 if (OMP_FOR_INIT (t) != NULL_TREE)
17214 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
17215 orig_declv, initv, condv, incrv, body, pre_body,
17216 NULL, clauses);
17217 else
17218 {
17219 t = make_node (TREE_CODE (t));
17220 TREE_TYPE (t) = void_type_node;
17221 OMP_FOR_BODY (t) = body;
17222 OMP_FOR_PRE_BODY (t) = pre_body;
17223 OMP_FOR_CLAUSES (t) = clauses;
17224 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
17225 add_stmt (t);
17226 }
17227
17228 add_stmt (finish_omp_structured_block (stmt));
17229 pop_omp_privatization_clauses (r);
17230 }
17231 break;
17232
17233 case OMP_SECTIONS:
17234 omp_parallel_combined_clauses = NULL;
17235 /* FALLTHRU */
17236 case OMP_SINGLE:
17237 case OMP_TEAMS:
17238 case OMP_CRITICAL:
17239 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
17240 && OMP_TEAMS_COMBINED (t));
17241 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
17242 in_decl);
17243 stmt = push_stmt_list ();
17244 RECUR (OMP_BODY (t));
17245 stmt = pop_stmt_list (stmt);
17246
17247 t = copy_node (t);
17248 OMP_BODY (t) = stmt;
17249 OMP_CLAUSES (t) = tmp;
17250 add_stmt (t);
17251 pop_omp_privatization_clauses (r);
17252 break;
17253
17254 case OACC_DATA:
17255 case OMP_TARGET_DATA:
17256 case OMP_TARGET:
17257 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
17258 ? C_ORT_ACC : C_ORT_OMP, args, complain,
17259 in_decl);
17260 keep_next_level (true);
17261 stmt = begin_omp_structured_block ();
17262
17263 RECUR (OMP_BODY (t));
17264 stmt = finish_omp_structured_block (stmt);
17265
17266 t = copy_node (t);
17267 OMP_BODY (t) = stmt;
17268 OMP_CLAUSES (t) = tmp;
17269 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
17270 {
17271 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
17272 if (teams)
17273 {
17274 /* For combined target teams, ensure the num_teams and
17275 thread_limit clause expressions are evaluated on the host,
17276 before entering the target construct. */
17277 tree c;
17278 for (c = OMP_TEAMS_CLAUSES (teams);
17279 c; c = OMP_CLAUSE_CHAIN (c))
17280 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17281 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17282 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17283 {
17284 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17285 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
17286 if (expr == error_mark_node)
17287 continue;
17288 tmp = TARGET_EXPR_SLOT (expr);
17289 add_stmt (expr);
17290 OMP_CLAUSE_OPERAND (c, 0) = expr;
17291 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17292 OMP_CLAUSE_FIRSTPRIVATE);
17293 OMP_CLAUSE_DECL (tc) = tmp;
17294 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
17295 OMP_TARGET_CLAUSES (t) = tc;
17296 }
17297 }
17298 }
17299 add_stmt (t);
17300 break;
17301
17302 case OACC_DECLARE:
17303 t = copy_node (t);
17304 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
17305 complain, in_decl);
17306 OACC_DECLARE_CLAUSES (t) = tmp;
17307 add_stmt (t);
17308 break;
17309
17310 case OMP_TARGET_UPDATE:
17311 case OMP_TARGET_ENTER_DATA:
17312 case OMP_TARGET_EXIT_DATA:
17313 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
17314 complain, in_decl);
17315 t = copy_node (t);
17316 OMP_STANDALONE_CLAUSES (t) = tmp;
17317 add_stmt (t);
17318 break;
17319
17320 case OACC_ENTER_DATA:
17321 case OACC_EXIT_DATA:
17322 case OACC_UPDATE:
17323 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
17324 complain, in_decl);
17325 t = copy_node (t);
17326 OMP_STANDALONE_CLAUSES (t) = tmp;
17327 add_stmt (t);
17328 break;
17329
17330 case OMP_ORDERED:
17331 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
17332 complain, in_decl);
17333 stmt = push_stmt_list ();
17334 RECUR (OMP_BODY (t));
17335 stmt = pop_stmt_list (stmt);
17336
17337 t = copy_node (t);
17338 OMP_BODY (t) = stmt;
17339 OMP_ORDERED_CLAUSES (t) = tmp;
17340 add_stmt (t);
17341 break;
17342
17343 case OMP_SECTION:
17344 case OMP_MASTER:
17345 case OMP_TASKGROUP:
17346 stmt = push_stmt_list ();
17347 RECUR (OMP_BODY (t));
17348 stmt = pop_stmt_list (stmt);
17349
17350 t = copy_node (t);
17351 OMP_BODY (t) = stmt;
17352 add_stmt (t);
17353 break;
17354
17355 case OMP_ATOMIC:
17356 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
17357 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
17358 {
17359 tree op1 = TREE_OPERAND (t, 1);
17360 tree rhs1 = NULL_TREE;
17361 tree lhs, rhs;
17362 if (TREE_CODE (op1) == COMPOUND_EXPR)
17363 {
17364 rhs1 = RECUR (TREE_OPERAND (op1, 0));
17365 op1 = TREE_OPERAND (op1, 1);
17366 }
17367 lhs = RECUR (TREE_OPERAND (op1, 0));
17368 rhs = RECUR (TREE_OPERAND (op1, 1));
17369 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
17370 NULL_TREE, NULL_TREE, rhs1,
17371 OMP_ATOMIC_SEQ_CST (t));
17372 }
17373 else
17374 {
17375 tree op1 = TREE_OPERAND (t, 1);
17376 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
17377 tree rhs1 = NULL_TREE;
17378 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
17379 enum tree_code opcode = NOP_EXPR;
17380 if (code == OMP_ATOMIC_READ)
17381 {
17382 v = RECUR (TREE_OPERAND (op1, 0));
17383 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17384 }
17385 else if (code == OMP_ATOMIC_CAPTURE_OLD
17386 || code == OMP_ATOMIC_CAPTURE_NEW)
17387 {
17388 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
17389 v = RECUR (TREE_OPERAND (op1, 0));
17390 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17391 if (TREE_CODE (op11) == COMPOUND_EXPR)
17392 {
17393 rhs1 = RECUR (TREE_OPERAND (op11, 0));
17394 op11 = TREE_OPERAND (op11, 1);
17395 }
17396 lhs = RECUR (TREE_OPERAND (op11, 0));
17397 rhs = RECUR (TREE_OPERAND (op11, 1));
17398 opcode = TREE_CODE (op11);
17399 if (opcode == MODIFY_EXPR)
17400 opcode = NOP_EXPR;
17401 }
17402 else
17403 {
17404 code = OMP_ATOMIC;
17405 lhs = RECUR (TREE_OPERAND (op1, 0));
17406 rhs = RECUR (TREE_OPERAND (op1, 1));
17407 }
17408 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
17409 OMP_ATOMIC_SEQ_CST (t));
17410 }
17411 break;
17412
17413 case TRANSACTION_EXPR:
17414 {
17415 int flags = 0;
17416 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
17417 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
17418
17419 if (TRANSACTION_EXPR_IS_STMT (t))
17420 {
17421 tree body = TRANSACTION_EXPR_BODY (t);
17422 tree noex = NULL_TREE;
17423 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
17424 {
17425 noex = MUST_NOT_THROW_COND (body);
17426 if (noex == NULL_TREE)
17427 noex = boolean_true_node;
17428 body = TREE_OPERAND (body, 0);
17429 }
17430 stmt = begin_transaction_stmt (input_location, NULL, flags);
17431 RECUR (body);
17432 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
17433 }
17434 else
17435 {
17436 stmt = build_transaction_expr (EXPR_LOCATION (t),
17437 RECUR (TRANSACTION_EXPR_BODY (t)),
17438 flags, NULL_TREE);
17439 RETURN (stmt);
17440 }
17441 }
17442 break;
17443
17444 case MUST_NOT_THROW_EXPR:
17445 {
17446 tree op0 = RECUR (TREE_OPERAND (t, 0));
17447 tree cond = RECUR (MUST_NOT_THROW_COND (t));
17448 RETURN (build_must_not_throw_expr (op0, cond));
17449 }
17450
17451 case EXPR_PACK_EXPANSION:
17452 error ("invalid use of pack expansion expression");
17453 RETURN (error_mark_node);
17454
17455 case NONTYPE_ARGUMENT_PACK:
17456 error ("use %<...%> to expand argument pack");
17457 RETURN (error_mark_node);
17458
17459 case COMPOUND_EXPR:
17460 tmp = RECUR (TREE_OPERAND (t, 0));
17461 if (tmp == NULL_TREE)
17462 /* If the first operand was a statement, we're done with it. */
17463 RETURN (RECUR (TREE_OPERAND (t, 1)));
17464 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17465 RECUR (TREE_OPERAND (t, 1)),
17466 complain));
17467
17468 case ANNOTATE_EXPR:
17469 tmp = RECUR (TREE_OPERAND (t, 0));
17470 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17471 TREE_TYPE (tmp), tmp,
17472 RECUR (TREE_OPERAND (t, 1)),
17473 RECUR (TREE_OPERAND (t, 2))));
17474
17475 default:
17476 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17477
17478 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17479 /*function_p=*/false,
17480 integral_constant_expression_p));
17481 }
17482
17483 RETURN (NULL_TREE);
17484 out:
17485 input_location = loc;
17486 return r;
17487 #undef RECUR
17488 #undef RETURN
17489 }
17490
17491 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17492 function. For description of the body see comment above
17493 cp_parser_omp_declare_reduction_exprs. */
17494
17495 static void
17496 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17497 {
17498 if (t == NULL_TREE || t == error_mark_node)
17499 return;
17500
17501 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17502
17503 tree_stmt_iterator tsi;
17504 int i;
17505 tree stmts[7];
17506 memset (stmts, 0, sizeof stmts);
17507 for (i = 0, tsi = tsi_start (t);
17508 i < 7 && !tsi_end_p (tsi);
17509 i++, tsi_next (&tsi))
17510 stmts[i] = tsi_stmt (tsi);
17511 gcc_assert (tsi_end_p (tsi));
17512
17513 if (i >= 3)
17514 {
17515 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17516 && TREE_CODE (stmts[1]) == DECL_EXPR);
17517 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17518 args, complain, in_decl);
17519 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17520 args, complain, in_decl);
17521 DECL_CONTEXT (omp_out) = current_function_decl;
17522 DECL_CONTEXT (omp_in) = current_function_decl;
17523 keep_next_level (true);
17524 tree block = begin_omp_structured_block ();
17525 tsubst_expr (stmts[2], args, complain, in_decl, false);
17526 block = finish_omp_structured_block (block);
17527 block = maybe_cleanup_point_expr_void (block);
17528 add_decl_expr (omp_out);
17529 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17530 TREE_NO_WARNING (omp_out) = 1;
17531 add_decl_expr (omp_in);
17532 finish_expr_stmt (block);
17533 }
17534 if (i >= 6)
17535 {
17536 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17537 && TREE_CODE (stmts[4]) == DECL_EXPR);
17538 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17539 args, complain, in_decl);
17540 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17541 args, complain, in_decl);
17542 DECL_CONTEXT (omp_priv) = current_function_decl;
17543 DECL_CONTEXT (omp_orig) = current_function_decl;
17544 keep_next_level (true);
17545 tree block = begin_omp_structured_block ();
17546 tsubst_expr (stmts[5], args, complain, in_decl, false);
17547 block = finish_omp_structured_block (block);
17548 block = maybe_cleanup_point_expr_void (block);
17549 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17550 add_decl_expr (omp_priv);
17551 add_decl_expr (omp_orig);
17552 finish_expr_stmt (block);
17553 if (i == 7)
17554 add_decl_expr (omp_orig);
17555 }
17556 }
17557
17558 /* T is a postfix-expression that is not being used in a function
17559 call. Return the substituted version of T. */
17560
17561 static tree
17562 tsubst_non_call_postfix_expression (tree t, tree args,
17563 tsubst_flags_t complain,
17564 tree in_decl)
17565 {
17566 if (TREE_CODE (t) == SCOPE_REF)
17567 t = tsubst_qualified_id (t, args, complain, in_decl,
17568 /*done=*/false, /*address_p=*/false);
17569 else
17570 t = tsubst_copy_and_build (t, args, complain, in_decl,
17571 /*function_p=*/false,
17572 /*integral_constant_expression_p=*/false);
17573
17574 return t;
17575 }
17576
17577 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17578 instantiation context. Instantiating a pack expansion containing a lambda
17579 might result in multiple lambdas all based on the same lambda in the
17580 template. */
17581
17582 tree
17583 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17584 {
17585 tree oldfn = lambda_function (t);
17586 in_decl = oldfn;
17587
17588 tree r = build_lambda_expr ();
17589
17590 LAMBDA_EXPR_LOCATION (r)
17591 = LAMBDA_EXPR_LOCATION (t);
17592 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17593 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17594 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17595
17596 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
17597 /* A lambda in a default argument outside a class gets no
17598 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
17599 tsubst_default_argument calls start_lambda_scope, so we need to
17600 specifically ignore it here, and use the global scope. */
17601 record_null_lambda_scope (r);
17602 else
17603 record_lambda_scope (r);
17604
17605 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17606 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17607
17608 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
17609 cap = TREE_CHAIN (cap))
17610 {
17611 tree field = TREE_PURPOSE (cap);
17612 if (PACK_EXPANSION_P (field))
17613 field = PACK_EXPANSION_PATTERN (field);
17614 field = tsubst_decl (field, args, complain);
17615
17616 if (field == error_mark_node)
17617 return error_mark_node;
17618
17619 tree init = TREE_VALUE (cap);
17620 if (PACK_EXPANSION_P (init))
17621 init = tsubst_pack_expansion (init, args, complain, in_decl);
17622 else
17623 init = tsubst_copy_and_build (init, args, complain, in_decl,
17624 /*fn*/false, /*constexpr*/false);
17625
17626 if (TREE_CODE (field) == TREE_VEC)
17627 {
17628 int len = TREE_VEC_LENGTH (field);
17629 gcc_assert (TREE_CODE (init) == TREE_VEC
17630 && TREE_VEC_LENGTH (init) == len);
17631 for (int i = 0; i < len; ++i)
17632 LAMBDA_EXPR_CAPTURE_LIST (r)
17633 = tree_cons (TREE_VEC_ELT (field, i),
17634 TREE_VEC_ELT (init, i),
17635 LAMBDA_EXPR_CAPTURE_LIST (r));
17636 }
17637 else
17638 {
17639 LAMBDA_EXPR_CAPTURE_LIST (r)
17640 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17641
17642 if (id_equal (DECL_NAME (field), "__this"))
17643 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17644 }
17645 }
17646
17647 tree type = begin_lambda_type (r);
17648 if (type == error_mark_node)
17649 return error_mark_node;
17650
17651 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17652 determine_visibility (TYPE_NAME (type));
17653
17654 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17655
17656 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17657 ? DECL_TI_TEMPLATE (oldfn)
17658 : NULL_TREE);
17659
17660 tree fntype = static_fn_type (oldfn);
17661 if (oldtmpl)
17662 ++processing_template_decl;
17663 fntype = tsubst (fntype, args, complain, in_decl);
17664 if (oldtmpl)
17665 --processing_template_decl;
17666
17667 if (fntype == error_mark_node)
17668 r = error_mark_node;
17669 else
17670 {
17671 /* Fix the type of 'this'. */
17672 fntype = build_memfn_type (fntype, type,
17673 type_memfn_quals (fntype),
17674 type_memfn_rqual (fntype));
17675 tree fn, tmpl;
17676 if (oldtmpl)
17677 {
17678 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17679 fn = DECL_TEMPLATE_RESULT (tmpl);
17680 finish_member_declaration (tmpl);
17681 }
17682 else
17683 {
17684 tmpl = NULL_TREE;
17685 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17686 finish_member_declaration (fn);
17687 }
17688
17689 /* Let finish_function set this. */
17690 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17691
17692 bool nested = cfun;
17693 if (nested)
17694 push_function_context ();
17695 else
17696 /* Still increment function_depth so that we don't GC in the
17697 middle of an expression. */
17698 ++function_depth;
17699
17700 local_specialization_stack s (lss_copy);
17701
17702 tree body = start_lambda_function (fn, r);
17703
17704 register_parameter_specializations (oldfn, fn);
17705
17706 if (oldtmpl)
17707 {
17708 /* We might not partially instantiate some parts of the function, so
17709 copy these flags from the original template. */
17710 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
17711 current_function_returns_value = ol->returns_value;
17712 current_function_returns_null = ol->returns_null;
17713 current_function_returns_abnormally = ol->returns_abnormally;
17714 current_function_infinite_loop = ol->infinite_loop;
17715 }
17716
17717 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17718 /*constexpr*/false);
17719
17720 finish_lambda_function (body);
17721
17722 if (nested)
17723 pop_function_context ();
17724 else
17725 --function_depth;
17726
17727 /* The capture list was built up in reverse order; fix that now. */
17728 LAMBDA_EXPR_CAPTURE_LIST (r)
17729 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17730
17731 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17732
17733 maybe_add_lambda_conv_op (type);
17734 }
17735
17736 finish_struct (type, /*attr*/NULL_TREE);
17737
17738 insert_pending_capture_proxies ();
17739
17740 return r;
17741 }
17742
17743 /* Like tsubst but deals with expressions and performs semantic
17744 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17745
17746 tree
17747 tsubst_copy_and_build (tree t,
17748 tree args,
17749 tsubst_flags_t complain,
17750 tree in_decl,
17751 bool function_p,
17752 bool integral_constant_expression_p)
17753 {
17754 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17755 #define RECUR(NODE) \
17756 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17757 /*function_p=*/false, \
17758 integral_constant_expression_p)
17759
17760 tree retval, op1;
17761 location_t loc;
17762
17763 if (t == NULL_TREE || t == error_mark_node)
17764 return t;
17765
17766 loc = input_location;
17767 if (location_t eloc = cp_expr_location (t))
17768 input_location = eloc;
17769
17770 /* N3276 decltype magic only applies to calls at the top level or on the
17771 right side of a comma. */
17772 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17773 complain &= ~tf_decltype;
17774
17775 switch (TREE_CODE (t))
17776 {
17777 case USING_DECL:
17778 t = DECL_NAME (t);
17779 /* Fall through. */
17780 case IDENTIFIER_NODE:
17781 {
17782 tree decl;
17783 cp_id_kind idk;
17784 bool non_integral_constant_expression_p;
17785 const char *error_msg;
17786
17787 if (IDENTIFIER_CONV_OP_P (t))
17788 {
17789 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17790 t = make_conv_op_name (new_type);
17791 }
17792
17793 /* Look up the name. */
17794 decl = lookup_name (t);
17795
17796 /* By convention, expressions use ERROR_MARK_NODE to indicate
17797 failure, not NULL_TREE. */
17798 if (decl == NULL_TREE)
17799 decl = error_mark_node;
17800
17801 decl = finish_id_expression (t, decl, NULL_TREE,
17802 &idk,
17803 integral_constant_expression_p,
17804 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17805 &non_integral_constant_expression_p,
17806 /*template_p=*/false,
17807 /*done=*/true,
17808 /*address_p=*/false,
17809 /*template_arg_p=*/false,
17810 &error_msg,
17811 input_location);
17812 if (error_msg)
17813 error (error_msg);
17814 if (!function_p && identifier_p (decl))
17815 {
17816 if (complain & tf_error)
17817 unqualified_name_lookup_error (decl);
17818 decl = error_mark_node;
17819 }
17820 RETURN (decl);
17821 }
17822
17823 case TEMPLATE_ID_EXPR:
17824 {
17825 tree object;
17826 tree templ = RECUR (TREE_OPERAND (t, 0));
17827 tree targs = TREE_OPERAND (t, 1);
17828
17829 if (targs)
17830 targs = tsubst_template_args (targs, args, complain, in_decl);
17831 if (targs == error_mark_node)
17832 RETURN (error_mark_node);
17833
17834 if (TREE_CODE (templ) == SCOPE_REF)
17835 {
17836 tree name = TREE_OPERAND (templ, 1);
17837 tree tid = lookup_template_function (name, targs);
17838 TREE_OPERAND (templ, 1) = tid;
17839 RETURN (templ);
17840 }
17841
17842 if (variable_template_p (templ))
17843 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17844
17845 if (TREE_CODE (templ) == COMPONENT_REF)
17846 {
17847 object = TREE_OPERAND (templ, 0);
17848 templ = TREE_OPERAND (templ, 1);
17849 }
17850 else
17851 object = NULL_TREE;
17852 templ = lookup_template_function (templ, targs);
17853
17854 if (object)
17855 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17856 object, templ, NULL_TREE));
17857 else
17858 RETURN (baselink_for_fns (templ));
17859 }
17860
17861 case INDIRECT_REF:
17862 {
17863 tree r = RECUR (TREE_OPERAND (t, 0));
17864
17865 if (REFERENCE_REF_P (t))
17866 {
17867 /* A type conversion to reference type will be enclosed in
17868 such an indirect ref, but the substitution of the cast
17869 will have also added such an indirect ref. */
17870 r = convert_from_reference (r);
17871 }
17872 else
17873 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17874 complain|decltype_flag);
17875
17876 if (REF_PARENTHESIZED_P (t))
17877 r = force_paren_expr (r);
17878
17879 RETURN (r);
17880 }
17881
17882 case NOP_EXPR:
17883 {
17884 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17885 tree op0 = RECUR (TREE_OPERAND (t, 0));
17886 RETURN (build_nop (type, op0));
17887 }
17888
17889 case IMPLICIT_CONV_EXPR:
17890 {
17891 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17892 tree expr = RECUR (TREE_OPERAND (t, 0));
17893 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17894 {
17895 retval = copy_node (t);
17896 TREE_TYPE (retval) = type;
17897 TREE_OPERAND (retval, 0) = expr;
17898 RETURN (retval);
17899 }
17900 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17901 /* We'll pass this to convert_nontype_argument again, we don't need
17902 to actually perform any conversion here. */
17903 RETURN (expr);
17904 int flags = LOOKUP_IMPLICIT;
17905 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17906 flags = LOOKUP_NORMAL;
17907 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17908 flags));
17909 }
17910
17911 case CONVERT_EXPR:
17912 {
17913 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17914 tree op0 = RECUR (TREE_OPERAND (t, 0));
17915 if (op0 == error_mark_node)
17916 RETURN (error_mark_node);
17917 RETURN (build1 (CONVERT_EXPR, type, op0));
17918 }
17919
17920 case CAST_EXPR:
17921 case REINTERPRET_CAST_EXPR:
17922 case CONST_CAST_EXPR:
17923 case DYNAMIC_CAST_EXPR:
17924 case STATIC_CAST_EXPR:
17925 {
17926 tree type;
17927 tree op, r = NULL_TREE;
17928
17929 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17930 if (integral_constant_expression_p
17931 && !cast_valid_in_integral_constant_expression_p (type))
17932 {
17933 if (complain & tf_error)
17934 error ("a cast to a type other than an integral or "
17935 "enumeration type cannot appear in a constant-expression");
17936 RETURN (error_mark_node);
17937 }
17938
17939 op = RECUR (TREE_OPERAND (t, 0));
17940
17941 warning_sentinel s(warn_useless_cast);
17942 warning_sentinel s2(warn_ignored_qualifiers);
17943 switch (TREE_CODE (t))
17944 {
17945 case CAST_EXPR:
17946 r = build_functional_cast (type, op, complain);
17947 break;
17948 case REINTERPRET_CAST_EXPR:
17949 r = build_reinterpret_cast (type, op, complain);
17950 break;
17951 case CONST_CAST_EXPR:
17952 r = build_const_cast (type, op, complain);
17953 break;
17954 case DYNAMIC_CAST_EXPR:
17955 r = build_dynamic_cast (type, op, complain);
17956 break;
17957 case STATIC_CAST_EXPR:
17958 r = build_static_cast (type, op, complain);
17959 break;
17960 default:
17961 gcc_unreachable ();
17962 }
17963
17964 RETURN (r);
17965 }
17966
17967 case POSTDECREMENT_EXPR:
17968 case POSTINCREMENT_EXPR:
17969 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17970 args, complain, in_decl);
17971 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17972 complain|decltype_flag));
17973
17974 case PREDECREMENT_EXPR:
17975 case PREINCREMENT_EXPR:
17976 case NEGATE_EXPR:
17977 case BIT_NOT_EXPR:
17978 case ABS_EXPR:
17979 case TRUTH_NOT_EXPR:
17980 case UNARY_PLUS_EXPR: /* Unary + */
17981 case REALPART_EXPR:
17982 case IMAGPART_EXPR:
17983 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17984 RECUR (TREE_OPERAND (t, 0)),
17985 complain|decltype_flag));
17986
17987 case FIX_TRUNC_EXPR:
17988 gcc_unreachable ();
17989
17990 case ADDR_EXPR:
17991 op1 = TREE_OPERAND (t, 0);
17992 if (TREE_CODE (op1) == LABEL_DECL)
17993 RETURN (finish_label_address_expr (DECL_NAME (op1),
17994 EXPR_LOCATION (op1)));
17995 if (TREE_CODE (op1) == SCOPE_REF)
17996 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17997 /*done=*/true, /*address_p=*/true);
17998 else
17999 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
18000 in_decl);
18001 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
18002 complain|decltype_flag));
18003
18004 case PLUS_EXPR:
18005 case MINUS_EXPR:
18006 case MULT_EXPR:
18007 case TRUNC_DIV_EXPR:
18008 case CEIL_DIV_EXPR:
18009 case FLOOR_DIV_EXPR:
18010 case ROUND_DIV_EXPR:
18011 case EXACT_DIV_EXPR:
18012 case BIT_AND_EXPR:
18013 case BIT_IOR_EXPR:
18014 case BIT_XOR_EXPR:
18015 case TRUNC_MOD_EXPR:
18016 case FLOOR_MOD_EXPR:
18017 case TRUTH_ANDIF_EXPR:
18018 case TRUTH_ORIF_EXPR:
18019 case TRUTH_AND_EXPR:
18020 case TRUTH_OR_EXPR:
18021 case RSHIFT_EXPR:
18022 case LSHIFT_EXPR:
18023 case RROTATE_EXPR:
18024 case LROTATE_EXPR:
18025 case EQ_EXPR:
18026 case NE_EXPR:
18027 case MAX_EXPR:
18028 case MIN_EXPR:
18029 case LE_EXPR:
18030 case GE_EXPR:
18031 case LT_EXPR:
18032 case GT_EXPR:
18033 case MEMBER_REF:
18034 case DOTSTAR_EXPR:
18035 {
18036 warning_sentinel s1(warn_type_limits);
18037 warning_sentinel s2(warn_div_by_zero);
18038 warning_sentinel s3(warn_logical_op);
18039 warning_sentinel s4(warn_tautological_compare);
18040 tree op0 = RECUR (TREE_OPERAND (t, 0));
18041 tree op1 = RECUR (TREE_OPERAND (t, 1));
18042 tree r = build_x_binary_op
18043 (input_location, TREE_CODE (t),
18044 op0,
18045 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
18046 ? ERROR_MARK
18047 : TREE_CODE (TREE_OPERAND (t, 0))),
18048 op1,
18049 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
18050 ? ERROR_MARK
18051 : TREE_CODE (TREE_OPERAND (t, 1))),
18052 /*overload=*/NULL,
18053 complain|decltype_flag);
18054 if (EXPR_P (r) && TREE_NO_WARNING (t))
18055 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18056
18057 RETURN (r);
18058 }
18059
18060 case POINTER_PLUS_EXPR:
18061 {
18062 tree op0 = RECUR (TREE_OPERAND (t, 0));
18063 tree op1 = RECUR (TREE_OPERAND (t, 1));
18064 RETURN (fold_build_pointer_plus (op0, op1));
18065 }
18066
18067 case SCOPE_REF:
18068 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
18069 /*address_p=*/false));
18070 case ARRAY_REF:
18071 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18072 args, complain, in_decl);
18073 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
18074 RECUR (TREE_OPERAND (t, 1)),
18075 complain|decltype_flag));
18076
18077 case SIZEOF_EXPR:
18078 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
18079 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
18080 RETURN (tsubst_copy (t, args, complain, in_decl));
18081 /* Fall through */
18082
18083 case ALIGNOF_EXPR:
18084 {
18085 tree r;
18086
18087 op1 = TREE_OPERAND (t, 0);
18088 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
18089 op1 = TREE_TYPE (op1);
18090 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
18091 && ALIGNOF_EXPR_STD_P (t));
18092 if (!args)
18093 {
18094 /* When there are no ARGS, we are trying to evaluate a
18095 non-dependent expression from the parser. Trying to do
18096 the substitutions may not work. */
18097 if (!TYPE_P (op1))
18098 op1 = TREE_TYPE (op1);
18099 }
18100 else
18101 {
18102 ++cp_unevaluated_operand;
18103 ++c_inhibit_evaluation_warnings;
18104 if (TYPE_P (op1))
18105 op1 = tsubst (op1, args, complain, in_decl);
18106 else
18107 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18108 /*function_p=*/false,
18109 /*integral_constant_expression_p=*/
18110 false);
18111 --cp_unevaluated_operand;
18112 --c_inhibit_evaluation_warnings;
18113 }
18114 if (TYPE_P (op1))
18115 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
18116 complain & tf_error);
18117 else
18118 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
18119 complain & tf_error);
18120 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
18121 {
18122 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
18123 {
18124 if (!processing_template_decl && TYPE_P (op1))
18125 {
18126 r = build_min (SIZEOF_EXPR, size_type_node,
18127 build1 (NOP_EXPR, op1, error_mark_node));
18128 SIZEOF_EXPR_TYPE_P (r) = 1;
18129 }
18130 else
18131 r = build_min (SIZEOF_EXPR, size_type_node, op1);
18132 TREE_SIDE_EFFECTS (r) = 0;
18133 TREE_READONLY (r) = 1;
18134 }
18135 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
18136 }
18137 RETURN (r);
18138 }
18139
18140 case AT_ENCODE_EXPR:
18141 {
18142 op1 = TREE_OPERAND (t, 0);
18143 ++cp_unevaluated_operand;
18144 ++c_inhibit_evaluation_warnings;
18145 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18146 /*function_p=*/false,
18147 /*integral_constant_expression_p=*/false);
18148 --cp_unevaluated_operand;
18149 --c_inhibit_evaluation_warnings;
18150 RETURN (objc_build_encode_expr (op1));
18151 }
18152
18153 case NOEXCEPT_EXPR:
18154 op1 = TREE_OPERAND (t, 0);
18155 ++cp_unevaluated_operand;
18156 ++c_inhibit_evaluation_warnings;
18157 ++cp_noexcept_operand;
18158 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18159 /*function_p=*/false,
18160 /*integral_constant_expression_p=*/false);
18161 --cp_unevaluated_operand;
18162 --c_inhibit_evaluation_warnings;
18163 --cp_noexcept_operand;
18164 RETURN (finish_noexcept_expr (op1, complain));
18165
18166 case MODOP_EXPR:
18167 {
18168 warning_sentinel s(warn_div_by_zero);
18169 tree lhs = RECUR (TREE_OPERAND (t, 0));
18170 tree rhs = RECUR (TREE_OPERAND (t, 2));
18171 tree r = build_x_modify_expr
18172 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
18173 complain|decltype_flag);
18174 /* TREE_NO_WARNING must be set if either the expression was
18175 parenthesized or it uses an operator such as >>= rather
18176 than plain assignment. In the former case, it was already
18177 set and must be copied. In the latter case,
18178 build_x_modify_expr sets it and it must not be reset
18179 here. */
18180 if (TREE_NO_WARNING (t))
18181 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18182
18183 RETURN (r);
18184 }
18185
18186 case ARROW_EXPR:
18187 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18188 args, complain, in_decl);
18189 /* Remember that there was a reference to this entity. */
18190 if (DECL_P (op1)
18191 && !mark_used (op1, complain) && !(complain & tf_error))
18192 RETURN (error_mark_node);
18193 RETURN (build_x_arrow (input_location, op1, complain));
18194
18195 case NEW_EXPR:
18196 {
18197 tree placement = RECUR (TREE_OPERAND (t, 0));
18198 tree init = RECUR (TREE_OPERAND (t, 3));
18199 vec<tree, va_gc> *placement_vec;
18200 vec<tree, va_gc> *init_vec;
18201 tree ret;
18202
18203 if (placement == NULL_TREE)
18204 placement_vec = NULL;
18205 else
18206 {
18207 placement_vec = make_tree_vector ();
18208 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
18209 vec_safe_push (placement_vec, TREE_VALUE (placement));
18210 }
18211
18212 /* If there was an initializer in the original tree, but it
18213 instantiated to an empty list, then we should pass a
18214 non-NULL empty vector to tell build_new that it was an
18215 empty initializer() rather than no initializer. This can
18216 only happen when the initializer is a pack expansion whose
18217 parameter packs are of length zero. */
18218 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
18219 init_vec = NULL;
18220 else
18221 {
18222 init_vec = make_tree_vector ();
18223 if (init == void_node)
18224 gcc_assert (init_vec != NULL);
18225 else
18226 {
18227 for (; init != NULL_TREE; init = TREE_CHAIN (init))
18228 vec_safe_push (init_vec, TREE_VALUE (init));
18229 }
18230 }
18231
18232 /* Avoid passing an enclosing decl to valid_array_size_p. */
18233 in_decl = NULL_TREE;
18234
18235 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
18236 tree op2 = RECUR (TREE_OPERAND (t, 2));
18237 ret = build_new (&placement_vec, op1, op2, &init_vec,
18238 NEW_EXPR_USE_GLOBAL (t),
18239 complain);
18240
18241 if (placement_vec != NULL)
18242 release_tree_vector (placement_vec);
18243 if (init_vec != NULL)
18244 release_tree_vector (init_vec);
18245
18246 RETURN (ret);
18247 }
18248
18249 case DELETE_EXPR:
18250 {
18251 tree op0 = RECUR (TREE_OPERAND (t, 0));
18252 tree op1 = RECUR (TREE_OPERAND (t, 1));
18253 RETURN (delete_sanity (op0, op1,
18254 DELETE_EXPR_USE_VEC (t),
18255 DELETE_EXPR_USE_GLOBAL (t),
18256 complain));
18257 }
18258
18259 case COMPOUND_EXPR:
18260 {
18261 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
18262 complain & ~tf_decltype, in_decl,
18263 /*function_p=*/false,
18264 integral_constant_expression_p);
18265 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
18266 op0,
18267 RECUR (TREE_OPERAND (t, 1)),
18268 complain|decltype_flag));
18269 }
18270
18271 case CALL_EXPR:
18272 {
18273 tree function;
18274 vec<tree, va_gc> *call_args;
18275 unsigned int nargs, i;
18276 bool qualified_p;
18277 bool koenig_p;
18278 tree ret;
18279
18280 function = CALL_EXPR_FN (t);
18281 /* Internal function with no arguments. */
18282 if (function == NULL_TREE && call_expr_nargs (t) == 0)
18283 RETURN (t);
18284
18285 /* When we parsed the expression, we determined whether or
18286 not Koenig lookup should be performed. */
18287 koenig_p = KOENIG_LOOKUP_P (t);
18288 if (function == NULL_TREE)
18289 {
18290 koenig_p = false;
18291 qualified_p = false;
18292 }
18293 else if (TREE_CODE (function) == SCOPE_REF)
18294 {
18295 qualified_p = true;
18296 function = tsubst_qualified_id (function, args, complain, in_decl,
18297 /*done=*/false,
18298 /*address_p=*/false);
18299 }
18300 else if (koenig_p && identifier_p (function))
18301 {
18302 /* Do nothing; calling tsubst_copy_and_build on an identifier
18303 would incorrectly perform unqualified lookup again.
18304
18305 Note that we can also have an IDENTIFIER_NODE if the earlier
18306 unqualified lookup found a member function; in that case
18307 koenig_p will be false and we do want to do the lookup
18308 again to find the instantiated member function.
18309
18310 FIXME but doing that causes c++/15272, so we need to stop
18311 using IDENTIFIER_NODE in that situation. */
18312 qualified_p = false;
18313 }
18314 else
18315 {
18316 if (TREE_CODE (function) == COMPONENT_REF)
18317 {
18318 tree op = TREE_OPERAND (function, 1);
18319
18320 qualified_p = (TREE_CODE (op) == SCOPE_REF
18321 || (BASELINK_P (op)
18322 && BASELINK_QUALIFIED_P (op)));
18323 }
18324 else
18325 qualified_p = false;
18326
18327 if (TREE_CODE (function) == ADDR_EXPR
18328 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
18329 /* Avoid error about taking the address of a constructor. */
18330 function = TREE_OPERAND (function, 0);
18331
18332 function = tsubst_copy_and_build (function, args, complain,
18333 in_decl,
18334 !qualified_p,
18335 integral_constant_expression_p);
18336
18337 if (BASELINK_P (function))
18338 qualified_p = true;
18339 }
18340
18341 nargs = call_expr_nargs (t);
18342 call_args = make_tree_vector ();
18343 for (i = 0; i < nargs; ++i)
18344 {
18345 tree arg = CALL_EXPR_ARG (t, i);
18346
18347 if (!PACK_EXPANSION_P (arg))
18348 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
18349 else
18350 {
18351 /* Expand the pack expansion and push each entry onto
18352 CALL_ARGS. */
18353 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
18354 if (TREE_CODE (arg) == TREE_VEC)
18355 {
18356 unsigned int len, j;
18357
18358 len = TREE_VEC_LENGTH (arg);
18359 for (j = 0; j < len; ++j)
18360 {
18361 tree value = TREE_VEC_ELT (arg, j);
18362 if (value != NULL_TREE)
18363 value = convert_from_reference (value);
18364 vec_safe_push (call_args, value);
18365 }
18366 }
18367 else
18368 {
18369 /* A partial substitution. Add one entry. */
18370 vec_safe_push (call_args, arg);
18371 }
18372 }
18373 }
18374
18375 /* We do not perform argument-dependent lookup if normal
18376 lookup finds a non-function, in accordance with the
18377 expected resolution of DR 218. */
18378 if (koenig_p
18379 && ((is_overloaded_fn (function)
18380 /* If lookup found a member function, the Koenig lookup is
18381 not appropriate, even if an unqualified-name was used
18382 to denote the function. */
18383 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
18384 || identifier_p (function))
18385 /* Only do this when substitution turns a dependent call
18386 into a non-dependent call. */
18387 && type_dependent_expression_p_push (t)
18388 && !any_type_dependent_arguments_p (call_args))
18389 function = perform_koenig_lookup (function, call_args, tf_none);
18390
18391 if (function != NULL_TREE
18392 && identifier_p (function)
18393 && !any_type_dependent_arguments_p (call_args))
18394 {
18395 if (koenig_p && (complain & tf_warning_or_error))
18396 {
18397 /* For backwards compatibility and good diagnostics, try
18398 the unqualified lookup again if we aren't in SFINAE
18399 context. */
18400 tree unq = (tsubst_copy_and_build
18401 (function, args, complain, in_decl, true,
18402 integral_constant_expression_p));
18403 if (unq == error_mark_node)
18404 {
18405 release_tree_vector (call_args);
18406 RETURN (error_mark_node);
18407 }
18408
18409 if (unq != function)
18410 {
18411 /* In a lambda fn, we have to be careful to not
18412 introduce new this captures. Legacy code can't
18413 be using lambdas anyway, so it's ok to be
18414 stricter. */
18415 bool in_lambda = (current_class_type
18416 && LAMBDA_TYPE_P (current_class_type));
18417 char const *const msg
18418 = G_("%qD was not declared in this scope, "
18419 "and no declarations were found by "
18420 "argument-dependent lookup at the point "
18421 "of instantiation");
18422
18423 bool diag = true;
18424 if (in_lambda)
18425 error_at (cp_expr_loc_or_loc (t, input_location),
18426 msg, function);
18427 else
18428 diag = permerror (cp_expr_loc_or_loc (t, input_location),
18429 msg, function);
18430 if (diag)
18431 {
18432 tree fn = unq;
18433
18434 if (INDIRECT_REF_P (fn))
18435 fn = TREE_OPERAND (fn, 0);
18436 if (is_overloaded_fn (fn))
18437 fn = get_first_fn (fn);
18438
18439 if (!DECL_P (fn))
18440 /* Can't say anything more. */;
18441 else if (DECL_CLASS_SCOPE_P (fn))
18442 {
18443 location_t loc = cp_expr_loc_or_loc (t,
18444 input_location);
18445 inform (loc,
18446 "declarations in dependent base %qT are "
18447 "not found by unqualified lookup",
18448 DECL_CLASS_CONTEXT (fn));
18449 if (current_class_ptr)
18450 inform (loc,
18451 "use %<this->%D%> instead", function);
18452 else
18453 inform (loc,
18454 "use %<%T::%D%> instead",
18455 current_class_name, function);
18456 }
18457 else
18458 inform (DECL_SOURCE_LOCATION (fn),
18459 "%qD declared here, later in the "
18460 "translation unit", fn);
18461 if (in_lambda)
18462 {
18463 release_tree_vector (call_args);
18464 RETURN (error_mark_node);
18465 }
18466 }
18467
18468 function = unq;
18469 }
18470 }
18471 if (identifier_p (function))
18472 {
18473 if (complain & tf_error)
18474 unqualified_name_lookup_error (function);
18475 release_tree_vector (call_args);
18476 RETURN (error_mark_node);
18477 }
18478 }
18479
18480 /* Remember that there was a reference to this entity. */
18481 if (function != NULL_TREE
18482 && DECL_P (function)
18483 && !mark_used (function, complain) && !(complain & tf_error))
18484 {
18485 release_tree_vector (call_args);
18486 RETURN (error_mark_node);
18487 }
18488
18489 /* Put back tf_decltype for the actual call. */
18490 complain |= decltype_flag;
18491
18492 if (function == NULL_TREE)
18493 switch (CALL_EXPR_IFN (t))
18494 {
18495 case IFN_LAUNDER:
18496 gcc_assert (nargs == 1);
18497 if (vec_safe_length (call_args) != 1)
18498 {
18499 error_at (cp_expr_loc_or_loc (t, input_location),
18500 "wrong number of arguments to "
18501 "%<__builtin_launder%>");
18502 ret = error_mark_node;
18503 }
18504 else
18505 ret = finish_builtin_launder (cp_expr_loc_or_loc (t,
18506 input_location),
18507 (*call_args)[0], complain);
18508 break;
18509
18510 default:
18511 /* Unsupported internal function with arguments. */
18512 gcc_unreachable ();
18513 }
18514 else if (TREE_CODE (function) == OFFSET_REF
18515 || TREE_CODE (function) == DOTSTAR_EXPR
18516 || TREE_CODE (function) == MEMBER_REF)
18517 ret = build_offset_ref_call_from_tree (function, &call_args,
18518 complain);
18519 else if (TREE_CODE (function) == COMPONENT_REF)
18520 {
18521 tree instance = TREE_OPERAND (function, 0);
18522 tree fn = TREE_OPERAND (function, 1);
18523
18524 if (processing_template_decl
18525 && (type_dependent_expression_p (instance)
18526 || (!BASELINK_P (fn)
18527 && TREE_CODE (fn) != FIELD_DECL)
18528 || type_dependent_expression_p (fn)
18529 || any_type_dependent_arguments_p (call_args)))
18530 ret = build_min_nt_call_vec (function, call_args);
18531 else if (!BASELINK_P (fn))
18532 ret = finish_call_expr (function, &call_args,
18533 /*disallow_virtual=*/false,
18534 /*koenig_p=*/false,
18535 complain);
18536 else
18537 ret = (build_new_method_call
18538 (instance, fn,
18539 &call_args, NULL_TREE,
18540 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
18541 /*fn_p=*/NULL,
18542 complain));
18543 }
18544 else
18545 ret = finish_call_expr (function, &call_args,
18546 /*disallow_virtual=*/qualified_p,
18547 koenig_p,
18548 complain);
18549
18550 release_tree_vector (call_args);
18551
18552 if (ret != error_mark_node)
18553 {
18554 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
18555 bool ord = CALL_EXPR_ORDERED_ARGS (t);
18556 bool rev = CALL_EXPR_REVERSE_ARGS (t);
18557 bool thk = CALL_FROM_THUNK_P (t);
18558 if (op || ord || rev || thk)
18559 {
18560 function = extract_call_expr (ret);
18561 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
18562 CALL_EXPR_ORDERED_ARGS (function) = ord;
18563 CALL_EXPR_REVERSE_ARGS (function) = rev;
18564 if (thk)
18565 {
18566 if (TREE_CODE (function) == CALL_EXPR)
18567 CALL_FROM_THUNK_P (function) = true;
18568 else
18569 AGGR_INIT_FROM_THUNK_P (function) = true;
18570 /* The thunk location is not interesting. */
18571 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
18572 }
18573 }
18574 }
18575
18576 RETURN (ret);
18577 }
18578
18579 case COND_EXPR:
18580 {
18581 tree cond = RECUR (TREE_OPERAND (t, 0));
18582 cond = mark_rvalue_use (cond);
18583 tree folded_cond = fold_non_dependent_expr (cond, complain);
18584 tree exp1, exp2;
18585
18586 if (TREE_CODE (folded_cond) == INTEGER_CST)
18587 {
18588 if (integer_zerop (folded_cond))
18589 {
18590 ++c_inhibit_evaluation_warnings;
18591 exp1 = RECUR (TREE_OPERAND (t, 1));
18592 --c_inhibit_evaluation_warnings;
18593 exp2 = RECUR (TREE_OPERAND (t, 2));
18594 }
18595 else
18596 {
18597 exp1 = RECUR (TREE_OPERAND (t, 1));
18598 ++c_inhibit_evaluation_warnings;
18599 exp2 = RECUR (TREE_OPERAND (t, 2));
18600 --c_inhibit_evaluation_warnings;
18601 }
18602 cond = folded_cond;
18603 }
18604 else
18605 {
18606 exp1 = RECUR (TREE_OPERAND (t, 1));
18607 exp2 = RECUR (TREE_OPERAND (t, 2));
18608 }
18609
18610 warning_sentinel s(warn_duplicated_branches);
18611 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
18612 cond, exp1, exp2, complain));
18613 }
18614
18615 case PSEUDO_DTOR_EXPR:
18616 {
18617 tree op0 = RECUR (TREE_OPERAND (t, 0));
18618 tree op1 = RECUR (TREE_OPERAND (t, 1));
18619 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
18620 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
18621 input_location));
18622 }
18623
18624 case TREE_LIST:
18625 {
18626 tree purpose, value, chain;
18627
18628 if (t == void_list_node)
18629 RETURN (t);
18630
18631 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
18632 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
18633 {
18634 /* We have pack expansions, so expand those and
18635 create a new list out of it. */
18636 tree purposevec = NULL_TREE;
18637 tree valuevec = NULL_TREE;
18638 tree chain;
18639 int i, len = -1;
18640
18641 /* Expand the argument expressions. */
18642 if (TREE_PURPOSE (t))
18643 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
18644 complain, in_decl);
18645 if (TREE_VALUE (t))
18646 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
18647 complain, in_decl);
18648
18649 /* Build the rest of the list. */
18650 chain = TREE_CHAIN (t);
18651 if (chain && chain != void_type_node)
18652 chain = RECUR (chain);
18653
18654 /* Determine the number of arguments. */
18655 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
18656 {
18657 len = TREE_VEC_LENGTH (purposevec);
18658 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18659 }
18660 else if (TREE_CODE (valuevec) == TREE_VEC)
18661 len = TREE_VEC_LENGTH (valuevec);
18662 else
18663 {
18664 /* Since we only performed a partial substitution into
18665 the argument pack, we only RETURN (a single list
18666 node. */
18667 if (purposevec == TREE_PURPOSE (t)
18668 && valuevec == TREE_VALUE (t)
18669 && chain == TREE_CHAIN (t))
18670 RETURN (t);
18671
18672 RETURN (tree_cons (purposevec, valuevec, chain));
18673 }
18674
18675 /* Convert the argument vectors into a TREE_LIST */
18676 i = len;
18677 while (i > 0)
18678 {
18679 /* Grab the Ith values. */
18680 i--;
18681 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
18682 : NULL_TREE;
18683 value
18684 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
18685 : NULL_TREE;
18686
18687 /* Build the list (backwards). */
18688 chain = tree_cons (purpose, value, chain);
18689 }
18690
18691 RETURN (chain);
18692 }
18693
18694 purpose = TREE_PURPOSE (t);
18695 if (purpose)
18696 purpose = RECUR (purpose);
18697 value = TREE_VALUE (t);
18698 if (value)
18699 value = RECUR (value);
18700 chain = TREE_CHAIN (t);
18701 if (chain && chain != void_type_node)
18702 chain = RECUR (chain);
18703 if (purpose == TREE_PURPOSE (t)
18704 && value == TREE_VALUE (t)
18705 && chain == TREE_CHAIN (t))
18706 RETURN (t);
18707 RETURN (tree_cons (purpose, value, chain));
18708 }
18709
18710 case COMPONENT_REF:
18711 {
18712 tree object;
18713 tree object_type;
18714 tree member;
18715 tree r;
18716
18717 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18718 args, complain, in_decl);
18719 /* Remember that there was a reference to this entity. */
18720 if (DECL_P (object)
18721 && !mark_used (object, complain) && !(complain & tf_error))
18722 RETURN (error_mark_node);
18723 object_type = TREE_TYPE (object);
18724
18725 member = TREE_OPERAND (t, 1);
18726 if (BASELINK_P (member))
18727 member = tsubst_baselink (member,
18728 non_reference (TREE_TYPE (object)),
18729 args, complain, in_decl);
18730 else
18731 member = tsubst_copy (member, args, complain, in_decl);
18732 if (member == error_mark_node)
18733 RETURN (error_mark_node);
18734
18735 if (TREE_CODE (member) == FIELD_DECL)
18736 {
18737 r = finish_non_static_data_member (member, object, NULL_TREE);
18738 if (TREE_CODE (r) == COMPONENT_REF)
18739 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18740 RETURN (r);
18741 }
18742 else if (type_dependent_expression_p (object))
18743 /* We can't do much here. */;
18744 else if (!CLASS_TYPE_P (object_type))
18745 {
18746 if (scalarish_type_p (object_type))
18747 {
18748 tree s = NULL_TREE;
18749 tree dtor = member;
18750
18751 if (TREE_CODE (dtor) == SCOPE_REF)
18752 {
18753 s = TREE_OPERAND (dtor, 0);
18754 dtor = TREE_OPERAND (dtor, 1);
18755 }
18756 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18757 {
18758 dtor = TREE_OPERAND (dtor, 0);
18759 if (TYPE_P (dtor))
18760 RETURN (finish_pseudo_destructor_expr
18761 (object, s, dtor, input_location));
18762 }
18763 }
18764 }
18765 else if (TREE_CODE (member) == SCOPE_REF
18766 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18767 {
18768 /* Lookup the template functions now that we know what the
18769 scope is. */
18770 tree scope = TREE_OPERAND (member, 0);
18771 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18772 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18773 member = lookup_qualified_name (scope, tmpl,
18774 /*is_type_p=*/false,
18775 /*complain=*/false);
18776 if (BASELINK_P (member))
18777 {
18778 BASELINK_FUNCTIONS (member)
18779 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18780 args);
18781 member = (adjust_result_of_qualified_name_lookup
18782 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18783 object_type));
18784 }
18785 else
18786 {
18787 qualified_name_lookup_error (scope, tmpl, member,
18788 input_location);
18789 RETURN (error_mark_node);
18790 }
18791 }
18792 else if (TREE_CODE (member) == SCOPE_REF
18793 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18794 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18795 {
18796 if (complain & tf_error)
18797 {
18798 if (TYPE_P (TREE_OPERAND (member, 0)))
18799 error ("%qT is not a class or namespace",
18800 TREE_OPERAND (member, 0));
18801 else
18802 error ("%qD is not a class or namespace",
18803 TREE_OPERAND (member, 0));
18804 }
18805 RETURN (error_mark_node);
18806 }
18807
18808 r = finish_class_member_access_expr (object, member,
18809 /*template_p=*/false,
18810 complain);
18811 if (TREE_CODE (r) == COMPONENT_REF)
18812 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18813 RETURN (r);
18814 }
18815
18816 case THROW_EXPR:
18817 RETURN (build_throw
18818 (RECUR (TREE_OPERAND (t, 0))));
18819
18820 case CONSTRUCTOR:
18821 {
18822 vec<constructor_elt, va_gc> *n;
18823 constructor_elt *ce;
18824 unsigned HOST_WIDE_INT idx;
18825 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18826 bool process_index_p;
18827 int newlen;
18828 bool need_copy_p = false;
18829 tree r;
18830
18831 if (type == error_mark_node)
18832 RETURN (error_mark_node);
18833
18834 /* We do not want to process the index of aggregate
18835 initializers as they are identifier nodes which will be
18836 looked up by digest_init. */
18837 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18838
18839 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18840 newlen = vec_safe_length (n);
18841 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18842 {
18843 if (ce->index && process_index_p
18844 /* An identifier index is looked up in the type
18845 being initialized, not the current scope. */
18846 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18847 ce->index = RECUR (ce->index);
18848
18849 if (PACK_EXPANSION_P (ce->value))
18850 {
18851 /* Substitute into the pack expansion. */
18852 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18853 in_decl);
18854
18855 if (ce->value == error_mark_node
18856 || PACK_EXPANSION_P (ce->value))
18857 ;
18858 else if (TREE_VEC_LENGTH (ce->value) == 1)
18859 /* Just move the argument into place. */
18860 ce->value = TREE_VEC_ELT (ce->value, 0);
18861 else
18862 {
18863 /* Update the length of the final CONSTRUCTOR
18864 arguments vector, and note that we will need to
18865 copy.*/
18866 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18867 need_copy_p = true;
18868 }
18869 }
18870 else
18871 ce->value = RECUR (ce->value);
18872 }
18873
18874 if (need_copy_p)
18875 {
18876 vec<constructor_elt, va_gc> *old_n = n;
18877
18878 vec_alloc (n, newlen);
18879 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18880 {
18881 if (TREE_CODE (ce->value) == TREE_VEC)
18882 {
18883 int i, len = TREE_VEC_LENGTH (ce->value);
18884 for (i = 0; i < len; ++i)
18885 CONSTRUCTOR_APPEND_ELT (n, 0,
18886 TREE_VEC_ELT (ce->value, i));
18887 }
18888 else
18889 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18890 }
18891 }
18892
18893 r = build_constructor (init_list_type_node, n);
18894 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18895
18896 if (TREE_HAS_CONSTRUCTOR (t))
18897 {
18898 fcl_t cl = fcl_functional;
18899 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18900 cl = fcl_c99;
18901 RETURN (finish_compound_literal (type, r, complain, cl));
18902 }
18903
18904 TREE_TYPE (r) = type;
18905 RETURN (r);
18906 }
18907
18908 case TYPEID_EXPR:
18909 {
18910 tree operand_0 = TREE_OPERAND (t, 0);
18911 if (TYPE_P (operand_0))
18912 {
18913 operand_0 = tsubst (operand_0, args, complain, in_decl);
18914 RETURN (get_typeid (operand_0, complain));
18915 }
18916 else
18917 {
18918 operand_0 = RECUR (operand_0);
18919 RETURN (build_typeid (operand_0, complain));
18920 }
18921 }
18922
18923 case VAR_DECL:
18924 if (!args)
18925 RETURN (t);
18926 /* Fall through */
18927
18928 case PARM_DECL:
18929 {
18930 tree r = tsubst_copy (t, args, complain, in_decl);
18931 /* ??? We're doing a subset of finish_id_expression here. */
18932 if (VAR_P (r)
18933 && !processing_template_decl
18934 && !cp_unevaluated_operand
18935 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18936 && CP_DECL_THREAD_LOCAL_P (r))
18937 {
18938 if (tree wrap = get_tls_wrapper_fn (r))
18939 /* Replace an evaluated use of the thread_local variable with
18940 a call to its wrapper. */
18941 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18942 }
18943 else if (outer_automatic_var_p (r))
18944 r = process_outer_var_ref (r, complain);
18945
18946 if (!TYPE_REF_P (TREE_TYPE (t)))
18947 /* If the original type was a reference, we'll be wrapped in
18948 the appropriate INDIRECT_REF. */
18949 r = convert_from_reference (r);
18950 RETURN (r);
18951 }
18952
18953 case VA_ARG_EXPR:
18954 {
18955 tree op0 = RECUR (TREE_OPERAND (t, 0));
18956 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18957 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18958 }
18959
18960 case OFFSETOF_EXPR:
18961 {
18962 tree object_ptr
18963 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18964 in_decl, /*function_p=*/false,
18965 /*integral_constant_expression_p=*/false);
18966 RETURN (finish_offsetof (object_ptr,
18967 RECUR (TREE_OPERAND (t, 0)),
18968 EXPR_LOCATION (t)));
18969 }
18970
18971 case ADDRESSOF_EXPR:
18972 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18973 RECUR (TREE_OPERAND (t, 0)), complain));
18974
18975 case TRAIT_EXPR:
18976 {
18977 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18978 complain, in_decl);
18979
18980 tree type2 = TRAIT_EXPR_TYPE2 (t);
18981 if (type2 && TREE_CODE (type2) == TREE_LIST)
18982 type2 = RECUR (type2);
18983 else if (type2)
18984 type2 = tsubst (type2, args, complain, in_decl);
18985
18986 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18987 }
18988
18989 case STMT_EXPR:
18990 {
18991 tree old_stmt_expr = cur_stmt_expr;
18992 tree stmt_expr = begin_stmt_expr ();
18993
18994 cur_stmt_expr = stmt_expr;
18995 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18996 integral_constant_expression_p);
18997 stmt_expr = finish_stmt_expr (stmt_expr, false);
18998 cur_stmt_expr = old_stmt_expr;
18999
19000 /* If the resulting list of expression statement is empty,
19001 fold it further into void_node. */
19002 if (empty_expr_stmt_p (stmt_expr))
19003 stmt_expr = void_node;
19004
19005 RETURN (stmt_expr);
19006 }
19007
19008 case LAMBDA_EXPR:
19009 {
19010 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
19011
19012 RETURN (build_lambda_object (r));
19013 }
19014
19015 case TARGET_EXPR:
19016 /* We can get here for a constant initializer of non-dependent type.
19017 FIXME stop folding in cp_parser_initializer_clause. */
19018 {
19019 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
19020 complain);
19021 RETURN (r);
19022 }
19023
19024 case TRANSACTION_EXPR:
19025 RETURN (tsubst_expr(t, args, complain, in_decl,
19026 integral_constant_expression_p));
19027
19028 case PAREN_EXPR:
19029 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
19030
19031 case VEC_PERM_EXPR:
19032 {
19033 tree op0 = RECUR (TREE_OPERAND (t, 0));
19034 tree op1 = RECUR (TREE_OPERAND (t, 1));
19035 tree op2 = RECUR (TREE_OPERAND (t, 2));
19036 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
19037 complain));
19038 }
19039
19040 case REQUIRES_EXPR:
19041 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
19042
19043 case NON_LVALUE_EXPR:
19044 case VIEW_CONVERT_EXPR:
19045 /* We should only see these for location wrapper nodes, or within
19046 instantiate_non_dependent_expr (when args is NULL_TREE). */
19047 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
19048 if (location_wrapper_p (t))
19049 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
19050 EXPR_LOCATION (t)));
19051 /* fallthrough. */
19052
19053 default:
19054 /* Handle Objective-C++ constructs, if appropriate. */
19055 {
19056 tree subst
19057 = objcp_tsubst_copy_and_build (t, args, complain,
19058 in_decl, /*function_p=*/false);
19059 if (subst)
19060 RETURN (subst);
19061 }
19062 RETURN (tsubst_copy (t, args, complain, in_decl));
19063 }
19064
19065 #undef RECUR
19066 #undef RETURN
19067 out:
19068 input_location = loc;
19069 return retval;
19070 }
19071
19072 /* Verify that the instantiated ARGS are valid. For type arguments,
19073 make sure that the type's linkage is ok. For non-type arguments,
19074 make sure they are constants if they are integral or enumerations.
19075 Emit an error under control of COMPLAIN, and return TRUE on error. */
19076
19077 static bool
19078 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
19079 {
19080 if (dependent_template_arg_p (t))
19081 return false;
19082 if (ARGUMENT_PACK_P (t))
19083 {
19084 tree vec = ARGUMENT_PACK_ARGS (t);
19085 int len = TREE_VEC_LENGTH (vec);
19086 bool result = false;
19087 int i;
19088
19089 for (i = 0; i < len; ++i)
19090 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
19091 result = true;
19092 return result;
19093 }
19094 else if (TYPE_P (t))
19095 {
19096 /* [basic.link]: A name with no linkage (notably, the name
19097 of a class or enumeration declared in a local scope)
19098 shall not be used to declare an entity with linkage.
19099 This implies that names with no linkage cannot be used as
19100 template arguments
19101
19102 DR 757 relaxes this restriction for C++0x. */
19103 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
19104 : no_linkage_check (t, /*relaxed_p=*/false));
19105
19106 if (nt)
19107 {
19108 /* DR 488 makes use of a type with no linkage cause
19109 type deduction to fail. */
19110 if (complain & tf_error)
19111 {
19112 if (TYPE_UNNAMED_P (nt))
19113 error ("%qT is/uses unnamed type", t);
19114 else
19115 error ("template argument for %qD uses local type %qT",
19116 tmpl, t);
19117 }
19118 return true;
19119 }
19120 /* In order to avoid all sorts of complications, we do not
19121 allow variably-modified types as template arguments. */
19122 else if (variably_modified_type_p (t, NULL_TREE))
19123 {
19124 if (complain & tf_error)
19125 error ("%qT is a variably modified type", t);
19126 return true;
19127 }
19128 }
19129 /* Class template and alias template arguments should be OK. */
19130 else if (DECL_TYPE_TEMPLATE_P (t))
19131 ;
19132 /* A non-type argument of integral or enumerated type must be a
19133 constant. */
19134 else if (TREE_TYPE (t)
19135 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
19136 && !REFERENCE_REF_P (t)
19137 && !TREE_CONSTANT (t))
19138 {
19139 if (complain & tf_error)
19140 error ("integral expression %qE is not constant", t);
19141 return true;
19142 }
19143 return false;
19144 }
19145
19146 static bool
19147 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
19148 {
19149 int ix, len = DECL_NTPARMS (tmpl);
19150 bool result = false;
19151
19152 for (ix = 0; ix != len; ix++)
19153 {
19154 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
19155 result = true;
19156 }
19157 if (result && (complain & tf_error))
19158 error (" trying to instantiate %qD", tmpl);
19159 return result;
19160 }
19161
19162 /* We're out of SFINAE context now, so generate diagnostics for the access
19163 errors we saw earlier when instantiating D from TMPL and ARGS. */
19164
19165 static void
19166 recheck_decl_substitution (tree d, tree tmpl, tree args)
19167 {
19168 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
19169 tree type = TREE_TYPE (pattern);
19170 location_t loc = input_location;
19171
19172 push_access_scope (d);
19173 push_deferring_access_checks (dk_no_deferred);
19174 input_location = DECL_SOURCE_LOCATION (pattern);
19175 tsubst (type, args, tf_warning_or_error, d);
19176 input_location = loc;
19177 pop_deferring_access_checks ();
19178 pop_access_scope (d);
19179 }
19180
19181 /* Instantiate the indicated variable, function, or alias template TMPL with
19182 the template arguments in TARG_PTR. */
19183
19184 static tree
19185 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
19186 {
19187 tree targ_ptr = orig_args;
19188 tree fndecl;
19189 tree gen_tmpl;
19190 tree spec;
19191 bool access_ok = true;
19192
19193 if (tmpl == error_mark_node)
19194 return error_mark_node;
19195
19196 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
19197
19198 /* If this function is a clone, handle it specially. */
19199 if (DECL_CLONED_FUNCTION_P (tmpl))
19200 {
19201 tree spec;
19202 tree clone;
19203
19204 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19205 DECL_CLONED_FUNCTION. */
19206 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
19207 targ_ptr, complain);
19208 if (spec == error_mark_node)
19209 return error_mark_node;
19210
19211 /* Look for the clone. */
19212 FOR_EACH_CLONE (clone, spec)
19213 if (DECL_NAME (clone) == DECL_NAME (tmpl))
19214 return clone;
19215 /* We should always have found the clone by now. */
19216 gcc_unreachable ();
19217 return NULL_TREE;
19218 }
19219
19220 if (targ_ptr == error_mark_node)
19221 return error_mark_node;
19222
19223 /* Check to see if we already have this specialization. */
19224 gen_tmpl = most_general_template (tmpl);
19225 if (TMPL_ARGS_DEPTH (targ_ptr)
19226 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
19227 /* targ_ptr only has the innermost template args, so add the outer ones
19228 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19229 the case of a non-dependent call within a template definition). */
19230 targ_ptr = (add_outermost_template_args
19231 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
19232 targ_ptr));
19233
19234 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19235 but it doesn't seem to be on the hot path. */
19236 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
19237
19238 gcc_assert (tmpl == gen_tmpl
19239 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
19240 == spec)
19241 || fndecl == NULL_TREE);
19242
19243 if (spec != NULL_TREE)
19244 {
19245 if (FNDECL_HAS_ACCESS_ERRORS (spec))
19246 {
19247 if (complain & tf_error)
19248 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
19249 return error_mark_node;
19250 }
19251 return spec;
19252 }
19253
19254 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
19255 complain))
19256 return error_mark_node;
19257
19258 /* We are building a FUNCTION_DECL, during which the access of its
19259 parameters and return types have to be checked. However this
19260 FUNCTION_DECL which is the desired context for access checking
19261 is not built yet. We solve this chicken-and-egg problem by
19262 deferring all checks until we have the FUNCTION_DECL. */
19263 push_deferring_access_checks (dk_deferred);
19264
19265 /* Instantiation of the function happens in the context of the function
19266 template, not the context of the overload resolution we're doing. */
19267 push_to_top_level ();
19268 /* If there are dependent arguments, e.g. because we're doing partial
19269 ordering, make sure processing_template_decl stays set. */
19270 if (uses_template_parms (targ_ptr))
19271 ++processing_template_decl;
19272 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19273 {
19274 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
19275 complain, gen_tmpl, true);
19276 push_nested_class (ctx);
19277 }
19278
19279 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
19280
19281 fndecl = NULL_TREE;
19282 if (VAR_P (pattern))
19283 {
19284 /* We need to determine if we're using a partial or explicit
19285 specialization now, because the type of the variable could be
19286 different. */
19287 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
19288 tree elt = most_specialized_partial_spec (tid, complain);
19289 if (elt == error_mark_node)
19290 pattern = error_mark_node;
19291 else if (elt)
19292 {
19293 tree partial_tmpl = TREE_VALUE (elt);
19294 tree partial_args = TREE_PURPOSE (elt);
19295 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
19296 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
19297 }
19298 }
19299
19300 /* Substitute template parameters to obtain the specialization. */
19301 if (fndecl == NULL_TREE)
19302 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
19303 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19304 pop_nested_class ();
19305 pop_from_top_level ();
19306
19307 if (fndecl == error_mark_node)
19308 {
19309 pop_deferring_access_checks ();
19310 return error_mark_node;
19311 }
19312
19313 /* The DECL_TI_TEMPLATE should always be the immediate parent
19314 template, not the most general template. */
19315 DECL_TI_TEMPLATE (fndecl) = tmpl;
19316 DECL_TI_ARGS (fndecl) = targ_ptr;
19317
19318 /* Now we know the specialization, compute access previously
19319 deferred. Do no access control for inheriting constructors,
19320 as we already checked access for the inherited constructor. */
19321 if (!(flag_new_inheriting_ctors
19322 && DECL_INHERITED_CTOR (fndecl)))
19323 {
19324 push_access_scope (fndecl);
19325 if (!perform_deferred_access_checks (complain))
19326 access_ok = false;
19327 pop_access_scope (fndecl);
19328 }
19329 pop_deferring_access_checks ();
19330
19331 /* If we've just instantiated the main entry point for a function,
19332 instantiate all the alternate entry points as well. We do this
19333 by cloning the instantiation of the main entry point, not by
19334 instantiating the template clones. */
19335 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
19336 clone_function_decl (fndecl, /*update_methods=*/false);
19337
19338 if (!access_ok)
19339 {
19340 if (!(complain & tf_error))
19341 {
19342 /* Remember to reinstantiate when we're out of SFINAE so the user
19343 can see the errors. */
19344 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
19345 }
19346 return error_mark_node;
19347 }
19348 return fndecl;
19349 }
19350
19351 /* Wrapper for instantiate_template_1. */
19352
19353 tree
19354 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
19355 {
19356 tree ret;
19357 timevar_push (TV_TEMPLATE_INST);
19358 ret = instantiate_template_1 (tmpl, orig_args, complain);
19359 timevar_pop (TV_TEMPLATE_INST);
19360 return ret;
19361 }
19362
19363 /* Instantiate the alias template TMPL with ARGS. Also push a template
19364 instantiation level, which instantiate_template doesn't do because
19365 functions and variables have sufficient context established by the
19366 callers. */
19367
19368 static tree
19369 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
19370 {
19371 if (tmpl == error_mark_node || args == error_mark_node)
19372 return error_mark_node;
19373 if (!push_tinst_level (tmpl, args))
19374 return error_mark_node;
19375
19376 args =
19377 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
19378 args, tmpl, complain,
19379 /*require_all_args=*/true,
19380 /*use_default_args=*/true);
19381
19382 tree r = instantiate_template (tmpl, args, complain);
19383 pop_tinst_level ();
19384
19385 return r;
19386 }
19387
19388 /* PARM is a template parameter pack for FN. Returns true iff
19389 PARM is used in a deducible way in the argument list of FN. */
19390
19391 static bool
19392 pack_deducible_p (tree parm, tree fn)
19393 {
19394 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
19395 for (; t; t = TREE_CHAIN (t))
19396 {
19397 tree type = TREE_VALUE (t);
19398 tree packs;
19399 if (!PACK_EXPANSION_P (type))
19400 continue;
19401 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
19402 packs; packs = TREE_CHAIN (packs))
19403 if (template_args_equal (TREE_VALUE (packs), parm))
19404 {
19405 /* The template parameter pack is used in a function parameter
19406 pack. If this is the end of the parameter list, the
19407 template parameter pack is deducible. */
19408 if (TREE_CHAIN (t) == void_list_node)
19409 return true;
19410 else
19411 /* Otherwise, not. Well, it could be deduced from
19412 a non-pack parameter, but doing so would end up with
19413 a deduction mismatch, so don't bother. */
19414 return false;
19415 }
19416 }
19417 /* The template parameter pack isn't used in any function parameter
19418 packs, but it might be used deeper, e.g. tuple<Args...>. */
19419 return true;
19420 }
19421
19422 /* Subroutine of fn_type_unification: check non-dependent parms for
19423 convertibility. */
19424
19425 static int
19426 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
19427 tree fn, unification_kind_t strict, int flags,
19428 struct conversion **convs, bool explain_p)
19429 {
19430 /* Non-constructor methods need to leave a conversion for 'this', which
19431 isn't included in nargs here. */
19432 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19433 && !DECL_CONSTRUCTOR_P (fn));
19434
19435 for (unsigned ia = 0;
19436 parms && parms != void_list_node && ia < nargs; )
19437 {
19438 tree parm = TREE_VALUE (parms);
19439
19440 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19441 && (!TREE_CHAIN (parms)
19442 || TREE_CHAIN (parms) == void_list_node))
19443 /* For a function parameter pack that occurs at the end of the
19444 parameter-declaration-list, the type A of each remaining
19445 argument of the call is compared with the type P of the
19446 declarator-id of the function parameter pack. */
19447 break;
19448
19449 parms = TREE_CHAIN (parms);
19450
19451 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19452 /* For a function parameter pack that does not occur at the
19453 end of the parameter-declaration-list, the type of the
19454 parameter pack is a non-deduced context. */
19455 continue;
19456
19457 if (!uses_template_parms (parm))
19458 {
19459 tree arg = args[ia];
19460 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
19461 int lflags = conv_flags (ia, nargs, fn, arg, flags);
19462
19463 if (check_non_deducible_conversion (parm, arg, strict, lflags,
19464 conv_p, explain_p))
19465 return 1;
19466 }
19467
19468 ++ia;
19469 }
19470
19471 return 0;
19472 }
19473
19474 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
19475 NARGS elements of the arguments that are being used when calling
19476 it. TARGS is a vector into which the deduced template arguments
19477 are placed.
19478
19479 Returns either a FUNCTION_DECL for the matching specialization of FN or
19480 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
19481 true, diagnostics will be printed to explain why it failed.
19482
19483 If FN is a conversion operator, or we are trying to produce a specific
19484 specialization, RETURN_TYPE is the return type desired.
19485
19486 The EXPLICIT_TARGS are explicit template arguments provided via a
19487 template-id.
19488
19489 The parameter STRICT is one of:
19490
19491 DEDUCE_CALL:
19492 We are deducing arguments for a function call, as in
19493 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
19494 deducing arguments for a call to the result of a conversion
19495 function template, as in [over.call.object].
19496
19497 DEDUCE_CONV:
19498 We are deducing arguments for a conversion function, as in
19499 [temp.deduct.conv].
19500
19501 DEDUCE_EXACT:
19502 We are deducing arguments when doing an explicit instantiation
19503 as in [temp.explicit], when determining an explicit specialization
19504 as in [temp.expl.spec], or when taking the address of a function
19505 template, as in [temp.deduct.funcaddr]. */
19506
19507 tree
19508 fn_type_unification (tree fn,
19509 tree explicit_targs,
19510 tree targs,
19511 const tree *args,
19512 unsigned int nargs,
19513 tree return_type,
19514 unification_kind_t strict,
19515 int flags,
19516 struct conversion **convs,
19517 bool explain_p,
19518 bool decltype_p)
19519 {
19520 tree parms;
19521 tree fntype;
19522 tree decl = NULL_TREE;
19523 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
19524 bool ok;
19525 static int deduction_depth;
19526
19527 tree orig_fn = fn;
19528 if (flag_new_inheriting_ctors)
19529 fn = strip_inheriting_ctors (fn);
19530
19531 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
19532 tree r = error_mark_node;
19533
19534 tree full_targs = targs;
19535 if (TMPL_ARGS_DEPTH (targs)
19536 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
19537 full_targs = (add_outermost_template_args
19538 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
19539 targs));
19540
19541 if (decltype_p)
19542 complain |= tf_decltype;
19543
19544 /* In C++0x, it's possible to have a function template whose type depends
19545 on itself recursively. This is most obvious with decltype, but can also
19546 occur with enumeration scope (c++/48969). So we need to catch infinite
19547 recursion and reject the substitution at deduction time; this function
19548 will return error_mark_node for any repeated substitution.
19549
19550 This also catches excessive recursion such as when f<N> depends on
19551 f<N-1> across all integers, and returns error_mark_node for all the
19552 substitutions back up to the initial one.
19553
19554 This is, of course, not reentrant. */
19555 if (excessive_deduction_depth)
19556 return error_mark_node;
19557 ++deduction_depth;
19558
19559 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
19560
19561 fntype = TREE_TYPE (fn);
19562 if (explicit_targs)
19563 {
19564 /* [temp.deduct]
19565
19566 The specified template arguments must match the template
19567 parameters in kind (i.e., type, nontype, template), and there
19568 must not be more arguments than there are parameters;
19569 otherwise type deduction fails.
19570
19571 Nontype arguments must match the types of the corresponding
19572 nontype template parameters, or must be convertible to the
19573 types of the corresponding nontype parameters as specified in
19574 _temp.arg.nontype_, otherwise type deduction fails.
19575
19576 All references in the function type of the function template
19577 to the corresponding template parameters are replaced by the
19578 specified template argument values. If a substitution in a
19579 template parameter or in the function type of the function
19580 template results in an invalid type, type deduction fails. */
19581 int i, len = TREE_VEC_LENGTH (tparms);
19582 location_t loc = input_location;
19583 bool incomplete = false;
19584
19585 if (explicit_targs == error_mark_node)
19586 goto fail;
19587
19588 if (TMPL_ARGS_DEPTH (explicit_targs)
19589 < TMPL_ARGS_DEPTH (full_targs))
19590 explicit_targs = add_outermost_template_args (full_targs,
19591 explicit_targs);
19592
19593 /* Adjust any explicit template arguments before entering the
19594 substitution context. */
19595 explicit_targs
19596 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
19597 complain,
19598 /*require_all_args=*/false,
19599 /*use_default_args=*/false));
19600 if (explicit_targs == error_mark_node)
19601 goto fail;
19602
19603 /* Substitute the explicit args into the function type. This is
19604 necessary so that, for instance, explicitly declared function
19605 arguments can match null pointed constants. If we were given
19606 an incomplete set of explicit args, we must not do semantic
19607 processing during substitution as we could create partial
19608 instantiations. */
19609 for (i = 0; i < len; i++)
19610 {
19611 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
19612 bool parameter_pack = false;
19613 tree targ = TREE_VEC_ELT (explicit_targs, i);
19614
19615 /* Dig out the actual parm. */
19616 if (TREE_CODE (parm) == TYPE_DECL
19617 || TREE_CODE (parm) == TEMPLATE_DECL)
19618 {
19619 parm = TREE_TYPE (parm);
19620 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
19621 }
19622 else if (TREE_CODE (parm) == PARM_DECL)
19623 {
19624 parm = DECL_INITIAL (parm);
19625 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
19626 }
19627
19628 if (!parameter_pack && targ == NULL_TREE)
19629 /* No explicit argument for this template parameter. */
19630 incomplete = true;
19631
19632 if (parameter_pack && pack_deducible_p (parm, fn))
19633 {
19634 /* Mark the argument pack as "incomplete". We could
19635 still deduce more arguments during unification.
19636 We remove this mark in type_unification_real. */
19637 if (targ)
19638 {
19639 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
19640 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
19641 = ARGUMENT_PACK_ARGS (targ);
19642 }
19643
19644 /* We have some incomplete argument packs. */
19645 incomplete = true;
19646 }
19647 }
19648
19649 if (!push_tinst_level (fn, explicit_targs))
19650 {
19651 excessive_deduction_depth = true;
19652 goto fail;
19653 }
19654 processing_template_decl += incomplete;
19655 input_location = DECL_SOURCE_LOCATION (fn);
19656 /* Ignore any access checks; we'll see them again in
19657 instantiate_template and they might have the wrong
19658 access path at this point. */
19659 push_deferring_access_checks (dk_deferred);
19660 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
19661 complain | tf_partial | tf_fndecl_type, NULL_TREE);
19662 pop_deferring_access_checks ();
19663 input_location = loc;
19664 processing_template_decl -= incomplete;
19665 pop_tinst_level ();
19666
19667 if (fntype == error_mark_node)
19668 goto fail;
19669
19670 /* Place the explicitly specified arguments in TARGS. */
19671 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
19672 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
19673 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
19674 }
19675
19676 /* Never do unification on the 'this' parameter. */
19677 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
19678
19679 if (return_type && strict == DEDUCE_CALL)
19680 {
19681 /* We're deducing for a call to the result of a template conversion
19682 function. The parms we really want are in return_type. */
19683 if (INDIRECT_TYPE_P (return_type))
19684 return_type = TREE_TYPE (return_type);
19685 parms = TYPE_ARG_TYPES (return_type);
19686 }
19687 else if (return_type)
19688 {
19689 tree *new_args;
19690
19691 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
19692 new_args = XALLOCAVEC (tree, nargs + 1);
19693 new_args[0] = return_type;
19694 memcpy (new_args + 1, args, nargs * sizeof (tree));
19695 args = new_args;
19696 ++nargs;
19697 }
19698
19699 /* We allow incomplete unification without an error message here
19700 because the standard doesn't seem to explicitly prohibit it. Our
19701 callers must be ready to deal with unification failures in any
19702 event. */
19703
19704 /* If we aren't explaining yet, push tinst context so we can see where
19705 any errors (e.g. from class instantiations triggered by instantiation
19706 of default template arguments) come from. If we are explaining, this
19707 context is redundant. */
19708 if (!explain_p && !push_tinst_level (fn, targs))
19709 {
19710 excessive_deduction_depth = true;
19711 goto fail;
19712 }
19713
19714 /* type_unification_real will pass back any access checks from default
19715 template argument substitution. */
19716 vec<deferred_access_check, va_gc> *checks;
19717 checks = NULL;
19718
19719 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19720 full_targs, parms, args, nargs, /*subr=*/0,
19721 strict, &checks, explain_p);
19722 if (!explain_p)
19723 pop_tinst_level ();
19724 if (!ok)
19725 goto fail;
19726
19727 /* Now that we have bindings for all of the template arguments,
19728 ensure that the arguments deduced for the template template
19729 parameters have compatible template parameter lists. We cannot
19730 check this property before we have deduced all template
19731 arguments, because the template parameter types of a template
19732 template parameter might depend on prior template parameters
19733 deduced after the template template parameter. The following
19734 ill-formed example illustrates this issue:
19735
19736 template<typename T, template<T> class C> void f(C<5>, T);
19737
19738 template<int N> struct X {};
19739
19740 void g() {
19741 f(X<5>(), 5l); // error: template argument deduction fails
19742 }
19743
19744 The template parameter list of 'C' depends on the template type
19745 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19746 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19747 time that we deduce 'C'. */
19748 if (!template_template_parm_bindings_ok_p
19749 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19750 {
19751 unify_inconsistent_template_template_parameters (explain_p);
19752 goto fail;
19753 }
19754
19755 /* DR 1391: All parameters have args, now check non-dependent parms for
19756 convertibility. */
19757 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
19758 convs, explain_p))
19759 goto fail;
19760
19761 /* All is well so far. Now, check:
19762
19763 [temp.deduct]
19764
19765 When all template arguments have been deduced, all uses of
19766 template parameters in nondeduced contexts are replaced with
19767 the corresponding deduced argument values. If the
19768 substitution results in an invalid type, as described above,
19769 type deduction fails. */
19770 if (!push_tinst_level (fn, targs))
19771 {
19772 excessive_deduction_depth = true;
19773 goto fail;
19774 }
19775
19776 /* Also collect access checks from the instantiation. */
19777 reopen_deferring_access_checks (checks);
19778
19779 decl = instantiate_template (fn, targs, complain);
19780
19781 checks = get_deferred_access_checks ();
19782 pop_deferring_access_checks ();
19783
19784 pop_tinst_level ();
19785
19786 if (decl == error_mark_node)
19787 goto fail;
19788
19789 /* Now perform any access checks encountered during substitution. */
19790 push_access_scope (decl);
19791 ok = perform_access_checks (checks, complain);
19792 pop_access_scope (decl);
19793 if (!ok)
19794 goto fail;
19795
19796 /* If we're looking for an exact match, check that what we got
19797 is indeed an exact match. It might not be if some template
19798 parameters are used in non-deduced contexts. But don't check
19799 for an exact match if we have dependent template arguments;
19800 in that case we're doing partial ordering, and we already know
19801 that we have two candidates that will provide the actual type. */
19802 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19803 {
19804 tree substed = TREE_TYPE (decl);
19805 unsigned int i;
19806
19807 tree sarg
19808 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19809 if (return_type)
19810 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19811 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19812 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19813 {
19814 unify_type_mismatch (explain_p, args[i],
19815 TREE_VALUE (sarg));
19816 goto fail;
19817 }
19818 }
19819
19820 /* After doing deduction with the inherited constructor, actually return an
19821 instantiation of the inheriting constructor. */
19822 if (orig_fn != fn)
19823 decl = instantiate_template (orig_fn, targs, complain);
19824
19825 r = decl;
19826
19827 fail:
19828 --deduction_depth;
19829 if (excessive_deduction_depth)
19830 {
19831 if (deduction_depth == 0)
19832 /* Reset once we're all the way out. */
19833 excessive_deduction_depth = false;
19834 }
19835
19836 return r;
19837 }
19838
19839 /* Adjust types before performing type deduction, as described in
19840 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19841 sections are symmetric. PARM is the type of a function parameter
19842 or the return type of the conversion function. ARG is the type of
19843 the argument passed to the call, or the type of the value
19844 initialized with the result of the conversion function.
19845 ARG_EXPR is the original argument expression, which may be null. */
19846
19847 static int
19848 maybe_adjust_types_for_deduction (unification_kind_t strict,
19849 tree* parm,
19850 tree* arg,
19851 tree arg_expr)
19852 {
19853 int result = 0;
19854
19855 switch (strict)
19856 {
19857 case DEDUCE_CALL:
19858 break;
19859
19860 case DEDUCE_CONV:
19861 /* Swap PARM and ARG throughout the remainder of this
19862 function; the handling is precisely symmetric since PARM
19863 will initialize ARG rather than vice versa. */
19864 std::swap (parm, arg);
19865 break;
19866
19867 case DEDUCE_EXACT:
19868 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19869 too, but here handle it by stripping the reference from PARM
19870 rather than by adding it to ARG. */
19871 if (TYPE_REF_P (*parm)
19872 && TYPE_REF_IS_RVALUE (*parm)
19873 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19874 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19875 && TYPE_REF_P (*arg)
19876 && !TYPE_REF_IS_RVALUE (*arg))
19877 *parm = TREE_TYPE (*parm);
19878 /* Nothing else to do in this case. */
19879 return 0;
19880
19881 default:
19882 gcc_unreachable ();
19883 }
19884
19885 if (!TYPE_REF_P (*parm))
19886 {
19887 /* [temp.deduct.call]
19888
19889 If P is not a reference type:
19890
19891 --If A is an array type, the pointer type produced by the
19892 array-to-pointer standard conversion (_conv.array_) is
19893 used in place of A for type deduction; otherwise,
19894
19895 --If A is a function type, the pointer type produced by
19896 the function-to-pointer standard conversion
19897 (_conv.func_) is used in place of A for type deduction;
19898 otherwise,
19899
19900 --If A is a cv-qualified type, the top level
19901 cv-qualifiers of A's type are ignored for type
19902 deduction. */
19903 if (TREE_CODE (*arg) == ARRAY_TYPE)
19904 *arg = build_pointer_type (TREE_TYPE (*arg));
19905 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19906 *arg = build_pointer_type (*arg);
19907 else
19908 *arg = TYPE_MAIN_VARIANT (*arg);
19909 }
19910
19911 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19912 reference to a cv-unqualified template parameter that does not represent a
19913 template parameter of a class template (during class template argument
19914 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19915 an lvalue, the type "lvalue reference to A" is used in place of A for type
19916 deduction. */
19917 if (TYPE_REF_P (*parm)
19918 && TYPE_REF_IS_RVALUE (*parm)
19919 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19920 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19921 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19922 && (arg_expr ? lvalue_p (arg_expr)
19923 /* try_one_overload doesn't provide an arg_expr, but
19924 functions are always lvalues. */
19925 : TREE_CODE (*arg) == FUNCTION_TYPE))
19926 *arg = build_reference_type (*arg);
19927
19928 /* [temp.deduct.call]
19929
19930 If P is a cv-qualified type, the top level cv-qualifiers
19931 of P's type are ignored for type deduction. If P is a
19932 reference type, the type referred to by P is used for
19933 type deduction. */
19934 *parm = TYPE_MAIN_VARIANT (*parm);
19935 if (TYPE_REF_P (*parm))
19936 {
19937 *parm = TREE_TYPE (*parm);
19938 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19939 }
19940
19941 /* DR 322. For conversion deduction, remove a reference type on parm
19942 too (which has been swapped into ARG). */
19943 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
19944 *arg = TREE_TYPE (*arg);
19945
19946 return result;
19947 }
19948
19949 /* Subroutine of fn_type_unification. PARM is a function parameter of a
19950 template which doesn't contain any deducible template parameters; check if
19951 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19952 unify_one_argument. */
19953
19954 static int
19955 check_non_deducible_conversion (tree parm, tree arg, int strict,
19956 int flags, struct conversion **conv_p,
19957 bool explain_p)
19958 {
19959 tree type;
19960
19961 if (!TYPE_P (arg))
19962 type = TREE_TYPE (arg);
19963 else
19964 type = arg;
19965
19966 if (same_type_p (parm, type))
19967 return unify_success (explain_p);
19968
19969 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
19970 if (strict == DEDUCE_CONV)
19971 {
19972 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
19973 return unify_success (explain_p);
19974 }
19975 else if (strict != DEDUCE_EXACT)
19976 {
19977 bool ok = false;
19978 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
19979 if (conv_p)
19980 /* Avoid recalculating this in add_function_candidate. */
19981 ok = (*conv_p
19982 = good_conversion (parm, type, conv_arg, flags, complain));
19983 else
19984 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
19985 if (ok)
19986 return unify_success (explain_p);
19987 }
19988
19989 if (strict == DEDUCE_EXACT)
19990 return unify_type_mismatch (explain_p, parm, arg);
19991 else
19992 return unify_arg_conversion (explain_p, parm, type, arg);
19993 }
19994
19995 static bool uses_deducible_template_parms (tree type);
19996
19997 /* Returns true iff the expression EXPR is one from which a template
19998 argument can be deduced. In other words, if it's an undecorated
19999 use of a template non-type parameter. */
20000
20001 static bool
20002 deducible_expression (tree expr)
20003 {
20004 /* Strip implicit conversions. */
20005 while (CONVERT_EXPR_P (expr))
20006 expr = TREE_OPERAND (expr, 0);
20007 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
20008 }
20009
20010 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20011 deducible way; that is, if it has a max value of <PARM> - 1. */
20012
20013 static bool
20014 deducible_array_bound (tree domain)
20015 {
20016 if (domain == NULL_TREE)
20017 return false;
20018
20019 tree max = TYPE_MAX_VALUE (domain);
20020 if (TREE_CODE (max) != MINUS_EXPR)
20021 return false;
20022
20023 return deducible_expression (TREE_OPERAND (max, 0));
20024 }
20025
20026 /* Returns true iff the template arguments ARGS use a template parameter
20027 in a deducible way. */
20028
20029 static bool
20030 deducible_template_args (tree args)
20031 {
20032 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
20033 {
20034 bool deducible;
20035 tree elt = TREE_VEC_ELT (args, i);
20036 if (ARGUMENT_PACK_P (elt))
20037 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
20038 else
20039 {
20040 if (PACK_EXPANSION_P (elt))
20041 elt = PACK_EXPANSION_PATTERN (elt);
20042 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
20043 deducible = true;
20044 else if (TYPE_P (elt))
20045 deducible = uses_deducible_template_parms (elt);
20046 else
20047 deducible = deducible_expression (elt);
20048 }
20049 if (deducible)
20050 return true;
20051 }
20052 return false;
20053 }
20054
20055 /* Returns true iff TYPE contains any deducible references to template
20056 parameters, as per 14.8.2.5. */
20057
20058 static bool
20059 uses_deducible_template_parms (tree type)
20060 {
20061 if (PACK_EXPANSION_P (type))
20062 type = PACK_EXPANSION_PATTERN (type);
20063
20064 /* T
20065 cv-list T
20066 TT<T>
20067 TT<i>
20068 TT<> */
20069 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20070 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20071 return true;
20072
20073 /* T*
20074 T&
20075 T&& */
20076 if (INDIRECT_TYPE_P (type))
20077 return uses_deducible_template_parms (TREE_TYPE (type));
20078
20079 /* T[integer-constant ]
20080 type [i] */
20081 if (TREE_CODE (type) == ARRAY_TYPE)
20082 return (uses_deducible_template_parms (TREE_TYPE (type))
20083 || deducible_array_bound (TYPE_DOMAIN (type)));
20084
20085 /* T type ::*
20086 type T::*
20087 T T::*
20088 T (type ::*)()
20089 type (T::*)()
20090 type (type ::*)(T)
20091 type (T::*)(T)
20092 T (type ::*)(T)
20093 T (T::*)()
20094 T (T::*)(T) */
20095 if (TYPE_PTRMEM_P (type))
20096 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
20097 || (uses_deducible_template_parms
20098 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
20099
20100 /* template-name <T> (where template-name refers to a class template)
20101 template-name <i> (where template-name refers to a class template) */
20102 if (CLASS_TYPE_P (type)
20103 && CLASSTYPE_TEMPLATE_INFO (type)
20104 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
20105 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20106 (CLASSTYPE_TI_ARGS (type)));
20107
20108 /* type (T)
20109 T()
20110 T(T) */
20111 if (TREE_CODE (type) == FUNCTION_TYPE
20112 || TREE_CODE (type) == METHOD_TYPE)
20113 {
20114 if (uses_deducible_template_parms (TREE_TYPE (type)))
20115 return true;
20116 tree parm = TYPE_ARG_TYPES (type);
20117 if (TREE_CODE (type) == METHOD_TYPE)
20118 parm = TREE_CHAIN (parm);
20119 for (; parm; parm = TREE_CHAIN (parm))
20120 if (uses_deducible_template_parms (TREE_VALUE (parm)))
20121 return true;
20122 }
20123
20124 return false;
20125 }
20126
20127 /* Subroutine of type_unification_real and unify_pack_expansion to
20128 handle unification of a single P/A pair. Parameters are as
20129 for those functions. */
20130
20131 static int
20132 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
20133 int subr, unification_kind_t strict,
20134 bool explain_p)
20135 {
20136 tree arg_expr = NULL_TREE;
20137 int arg_strict;
20138
20139 if (arg == error_mark_node || parm == error_mark_node)
20140 return unify_invalid (explain_p);
20141 if (arg == unknown_type_node)
20142 /* We can't deduce anything from this, but we might get all the
20143 template args from other function args. */
20144 return unify_success (explain_p);
20145
20146 /* Implicit conversions (Clause 4) will be performed on a function
20147 argument to convert it to the type of the corresponding function
20148 parameter if the parameter type contains no template-parameters that
20149 participate in template argument deduction. */
20150 if (strict != DEDUCE_EXACT
20151 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
20152 /* For function parameters with no deducible template parameters,
20153 just return. We'll check non-dependent conversions later. */
20154 return unify_success (explain_p);
20155
20156 switch (strict)
20157 {
20158 case DEDUCE_CALL:
20159 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
20160 | UNIFY_ALLOW_MORE_CV_QUAL
20161 | UNIFY_ALLOW_DERIVED);
20162 break;
20163
20164 case DEDUCE_CONV:
20165 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
20166 break;
20167
20168 case DEDUCE_EXACT:
20169 arg_strict = UNIFY_ALLOW_NONE;
20170 break;
20171
20172 default:
20173 gcc_unreachable ();
20174 }
20175
20176 /* We only do these transformations if this is the top-level
20177 parameter_type_list in a call or declaration matching; in other
20178 situations (nested function declarators, template argument lists) we
20179 won't be comparing a type to an expression, and we don't do any type
20180 adjustments. */
20181 if (!subr)
20182 {
20183 if (!TYPE_P (arg))
20184 {
20185 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
20186 if (type_unknown_p (arg))
20187 {
20188 /* [temp.deduct.type] A template-argument can be
20189 deduced from a pointer to function or pointer
20190 to member function argument if the set of
20191 overloaded functions does not contain function
20192 templates and at most one of a set of
20193 overloaded functions provides a unique
20194 match. */
20195 resolve_overloaded_unification (tparms, targs, parm,
20196 arg, strict,
20197 arg_strict, explain_p);
20198 /* If a unique match was not found, this is a
20199 non-deduced context, so we still succeed. */
20200 return unify_success (explain_p);
20201 }
20202
20203 arg_expr = arg;
20204 arg = unlowered_expr_type (arg);
20205 if (arg == error_mark_node)
20206 return unify_invalid (explain_p);
20207 }
20208
20209 arg_strict |=
20210 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
20211 }
20212 else
20213 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
20214 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
20215 return unify_template_argument_mismatch (explain_p, parm, arg);
20216
20217 /* For deduction from an init-list we need the actual list. */
20218 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
20219 arg = arg_expr;
20220 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
20221 }
20222
20223 /* for_each_template_parm callback that always returns 0. */
20224
20225 static int
20226 zero_r (tree, void *)
20227 {
20228 return 0;
20229 }
20230
20231 /* for_each_template_parm any_fn callback to handle deduction of a template
20232 type argument from the type of an array bound. */
20233
20234 static int
20235 array_deduction_r (tree t, void *data)
20236 {
20237 tree_pair_p d = (tree_pair_p)data;
20238 tree &tparms = d->purpose;
20239 tree &targs = d->value;
20240
20241 if (TREE_CODE (t) == ARRAY_TYPE)
20242 if (tree dom = TYPE_DOMAIN (t))
20243 if (tree max = TYPE_MAX_VALUE (dom))
20244 {
20245 if (TREE_CODE (max) == MINUS_EXPR)
20246 max = TREE_OPERAND (max, 0);
20247 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
20248 unify (tparms, targs, TREE_TYPE (max), size_type_node,
20249 UNIFY_ALLOW_NONE, /*explain*/false);
20250 }
20251
20252 /* Keep walking. */
20253 return 0;
20254 }
20255
20256 /* Try to deduce any not-yet-deduced template type arguments from the type of
20257 an array bound. This is handled separately from unify because 14.8.2.5 says
20258 "The type of a type parameter is only deduced from an array bound if it is
20259 not otherwise deduced." */
20260
20261 static void
20262 try_array_deduction (tree tparms, tree targs, tree parm)
20263 {
20264 tree_pair_s data = { tparms, targs };
20265 hash_set<tree> visited;
20266 for_each_template_parm (parm, zero_r, &data, &visited,
20267 /*nondeduced*/false, array_deduction_r);
20268 }
20269
20270 /* Most parms like fn_type_unification.
20271
20272 If SUBR is 1, we're being called recursively (to unify the
20273 arguments of a function or method parameter of a function
20274 template).
20275
20276 CHECKS is a pointer to a vector of access checks encountered while
20277 substituting default template arguments. */
20278
20279 static int
20280 type_unification_real (tree tparms,
20281 tree full_targs,
20282 tree xparms,
20283 const tree *xargs,
20284 unsigned int xnargs,
20285 int subr,
20286 unification_kind_t strict,
20287 vec<deferred_access_check, va_gc> **checks,
20288 bool explain_p)
20289 {
20290 tree parm, arg;
20291 int i;
20292 int ntparms = TREE_VEC_LENGTH (tparms);
20293 int saw_undeduced = 0;
20294 tree parms;
20295 const tree *args;
20296 unsigned int nargs;
20297 unsigned int ia;
20298
20299 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
20300 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
20301 gcc_assert (ntparms > 0);
20302
20303 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
20304
20305 /* Reset the number of non-defaulted template arguments contained
20306 in TARGS. */
20307 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
20308
20309 again:
20310 parms = xparms;
20311 args = xargs;
20312 nargs = xnargs;
20313
20314 ia = 0;
20315 while (parms && parms != void_list_node
20316 && ia < nargs)
20317 {
20318 parm = TREE_VALUE (parms);
20319
20320 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20321 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
20322 /* For a function parameter pack that occurs at the end of the
20323 parameter-declaration-list, the type A of each remaining
20324 argument of the call is compared with the type P of the
20325 declarator-id of the function parameter pack. */
20326 break;
20327
20328 parms = TREE_CHAIN (parms);
20329
20330 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20331 /* For a function parameter pack that does not occur at the
20332 end of the parameter-declaration-list, the type of the
20333 parameter pack is a non-deduced context. */
20334 continue;
20335
20336 arg = args[ia];
20337 ++ia;
20338
20339 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
20340 explain_p))
20341 return 1;
20342 }
20343
20344 if (parms
20345 && parms != void_list_node
20346 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
20347 {
20348 /* Unify the remaining arguments with the pack expansion type. */
20349 tree argvec;
20350 tree parmvec = make_tree_vec (1);
20351
20352 /* Allocate a TREE_VEC and copy in all of the arguments */
20353 argvec = make_tree_vec (nargs - ia);
20354 for (i = 0; ia < nargs; ++ia, ++i)
20355 TREE_VEC_ELT (argvec, i) = args[ia];
20356
20357 /* Copy the parameter into parmvec. */
20358 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
20359 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
20360 /*subr=*/subr, explain_p))
20361 return 1;
20362
20363 /* Advance to the end of the list of parameters. */
20364 parms = TREE_CHAIN (parms);
20365 }
20366
20367 /* Fail if we've reached the end of the parm list, and more args
20368 are present, and the parm list isn't variadic. */
20369 if (ia < nargs && parms == void_list_node)
20370 return unify_too_many_arguments (explain_p, nargs, ia);
20371 /* Fail if parms are left and they don't have default values and
20372 they aren't all deduced as empty packs (c++/57397). This is
20373 consistent with sufficient_parms_p. */
20374 if (parms && parms != void_list_node
20375 && TREE_PURPOSE (parms) == NULL_TREE)
20376 {
20377 unsigned int count = nargs;
20378 tree p = parms;
20379 bool type_pack_p;
20380 do
20381 {
20382 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
20383 if (!type_pack_p)
20384 count++;
20385 p = TREE_CHAIN (p);
20386 }
20387 while (p && p != void_list_node);
20388 if (count != nargs)
20389 return unify_too_few_arguments (explain_p, ia, count,
20390 type_pack_p);
20391 }
20392
20393 if (!subr)
20394 {
20395 tsubst_flags_t complain = (explain_p
20396 ? tf_warning_or_error
20397 : tf_none);
20398 bool tried_array_deduction = (cxx_dialect < cxx17);
20399
20400 for (i = 0; i < ntparms; i++)
20401 {
20402 tree targ = TREE_VEC_ELT (targs, i);
20403 tree tparm = TREE_VEC_ELT (tparms, i);
20404
20405 /* Clear the "incomplete" flags on all argument packs now so that
20406 substituting them into later default arguments works. */
20407 if (targ && ARGUMENT_PACK_P (targ))
20408 {
20409 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
20410 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
20411 }
20412
20413 if (targ || tparm == error_mark_node)
20414 continue;
20415 tparm = TREE_VALUE (tparm);
20416
20417 if (TREE_CODE (tparm) == TYPE_DECL
20418 && !tried_array_deduction)
20419 {
20420 try_array_deduction (tparms, targs, xparms);
20421 tried_array_deduction = true;
20422 if (TREE_VEC_ELT (targs, i))
20423 continue;
20424 }
20425
20426 /* If this is an undeduced nontype parameter that depends on
20427 a type parameter, try another pass; its type may have been
20428 deduced from a later argument than the one from which
20429 this parameter can be deduced. */
20430 if (TREE_CODE (tparm) == PARM_DECL
20431 && uses_template_parms (TREE_TYPE (tparm))
20432 && saw_undeduced < 2)
20433 {
20434 saw_undeduced = 1;
20435 continue;
20436 }
20437
20438 /* Core issue #226 (C++0x) [temp.deduct]:
20439
20440 If a template argument has not been deduced, its
20441 default template argument, if any, is used.
20442
20443 When we are in C++98 mode, TREE_PURPOSE will either
20444 be NULL_TREE or ERROR_MARK_NODE, so we do not need
20445 to explicitly check cxx_dialect here. */
20446 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
20447 /* OK, there is a default argument. Wait until after the
20448 conversion check to do substitution. */
20449 continue;
20450
20451 /* If the type parameter is a parameter pack, then it will
20452 be deduced to an empty parameter pack. */
20453 if (template_parameter_pack_p (tparm))
20454 {
20455 tree arg;
20456
20457 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
20458 {
20459 arg = make_node (NONTYPE_ARGUMENT_PACK);
20460 TREE_CONSTANT (arg) = 1;
20461 }
20462 else
20463 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
20464
20465 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
20466
20467 TREE_VEC_ELT (targs, i) = arg;
20468 continue;
20469 }
20470
20471 return unify_parameter_deduction_failure (explain_p, tparm);
20472 }
20473
20474 /* Now substitute into the default template arguments. */
20475 for (i = 0; i < ntparms; i++)
20476 {
20477 tree targ = TREE_VEC_ELT (targs, i);
20478 tree tparm = TREE_VEC_ELT (tparms, i);
20479
20480 if (targ || tparm == error_mark_node)
20481 continue;
20482 tree parm = TREE_VALUE (tparm);
20483 tree arg = TREE_PURPOSE (tparm);
20484 reopen_deferring_access_checks (*checks);
20485 location_t save_loc = input_location;
20486 if (DECL_P (parm))
20487 input_location = DECL_SOURCE_LOCATION (parm);
20488
20489 if (saw_undeduced == 1
20490 && TREE_CODE (parm) == PARM_DECL
20491 && uses_template_parms (TREE_TYPE (parm)))
20492 {
20493 /* The type of this non-type parameter depends on undeduced
20494 parameters. Don't try to use its default argument yet,
20495 since we might deduce an argument for it on the next pass,
20496 but do check whether the arguments we already have cause
20497 substitution failure, so that that happens before we try
20498 later default arguments (78489). */
20499 ++processing_template_decl;
20500 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
20501 NULL_TREE);
20502 --processing_template_decl;
20503 if (type == error_mark_node)
20504 arg = error_mark_node;
20505 else
20506 arg = NULL_TREE;
20507 }
20508 else
20509 {
20510 tree substed = NULL_TREE;
20511 if (saw_undeduced == 1 && processing_template_decl == 0)
20512 {
20513 /* First instatiate in template context, in case we still
20514 depend on undeduced template parameters. */
20515 ++processing_template_decl;
20516 substed = tsubst_template_arg (arg, full_targs, complain,
20517 NULL_TREE);
20518 --processing_template_decl;
20519 if (substed != error_mark_node
20520 && !uses_template_parms (substed))
20521 /* We replaced all the tparms, substitute again out of
20522 template context. */
20523 substed = NULL_TREE;
20524 }
20525 if (!substed)
20526 substed = tsubst_template_arg (arg, full_targs, complain,
20527 NULL_TREE);
20528
20529 if (!uses_template_parms (substed))
20530 arg = convert_template_argument (parm, substed, full_targs,
20531 complain, i, NULL_TREE);
20532 else if (saw_undeduced == 1)
20533 arg = NULL_TREE;
20534 else
20535 arg = error_mark_node;
20536 }
20537
20538 input_location = save_loc;
20539 *checks = get_deferred_access_checks ();
20540 pop_deferring_access_checks ();
20541
20542 if (arg == error_mark_node)
20543 return 1;
20544 else if (arg)
20545 {
20546 TREE_VEC_ELT (targs, i) = arg;
20547 /* The position of the first default template argument,
20548 is also the number of non-defaulted arguments in TARGS.
20549 Record that. */
20550 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20551 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
20552 }
20553 }
20554
20555 if (saw_undeduced++ == 1)
20556 goto again;
20557 }
20558
20559 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20560 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
20561
20562 return unify_success (explain_p);
20563 }
20564
20565 /* Subroutine of type_unification_real. Args are like the variables
20566 at the call site. ARG is an overloaded function (or template-id);
20567 we try deducing template args from each of the overloads, and if
20568 only one succeeds, we go with that. Modifies TARGS and returns
20569 true on success. */
20570
20571 static bool
20572 resolve_overloaded_unification (tree tparms,
20573 tree targs,
20574 tree parm,
20575 tree arg,
20576 unification_kind_t strict,
20577 int sub_strict,
20578 bool explain_p)
20579 {
20580 tree tempargs = copy_node (targs);
20581 int good = 0;
20582 tree goodfn = NULL_TREE;
20583 bool addr_p;
20584
20585 if (TREE_CODE (arg) == ADDR_EXPR)
20586 {
20587 arg = TREE_OPERAND (arg, 0);
20588 addr_p = true;
20589 }
20590 else
20591 addr_p = false;
20592
20593 if (TREE_CODE (arg) == COMPONENT_REF)
20594 /* Handle `&x' where `x' is some static or non-static member
20595 function name. */
20596 arg = TREE_OPERAND (arg, 1);
20597
20598 if (TREE_CODE (arg) == OFFSET_REF)
20599 arg = TREE_OPERAND (arg, 1);
20600
20601 /* Strip baselink information. */
20602 if (BASELINK_P (arg))
20603 arg = BASELINK_FUNCTIONS (arg);
20604
20605 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
20606 {
20607 /* If we got some explicit template args, we need to plug them into
20608 the affected templates before we try to unify, in case the
20609 explicit args will completely resolve the templates in question. */
20610
20611 int ok = 0;
20612 tree expl_subargs = TREE_OPERAND (arg, 1);
20613 arg = TREE_OPERAND (arg, 0);
20614
20615 for (lkp_iterator iter (arg); iter; ++iter)
20616 {
20617 tree fn = *iter;
20618 tree subargs, elem;
20619
20620 if (TREE_CODE (fn) != TEMPLATE_DECL)
20621 continue;
20622
20623 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20624 expl_subargs, NULL_TREE, tf_none,
20625 /*require_all_args=*/true,
20626 /*use_default_args=*/true);
20627 if (subargs != error_mark_node
20628 && !any_dependent_template_arguments_p (subargs))
20629 {
20630 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
20631 if (try_one_overload (tparms, targs, tempargs, parm,
20632 elem, strict, sub_strict, addr_p, explain_p)
20633 && (!goodfn || !same_type_p (goodfn, elem)))
20634 {
20635 goodfn = elem;
20636 ++good;
20637 }
20638 }
20639 else if (subargs)
20640 ++ok;
20641 }
20642 /* If no templates (or more than one) are fully resolved by the
20643 explicit arguments, this template-id is a non-deduced context; it
20644 could still be OK if we deduce all template arguments for the
20645 enclosing call through other arguments. */
20646 if (good != 1)
20647 good = ok;
20648 }
20649 else if (TREE_CODE (arg) != OVERLOAD
20650 && TREE_CODE (arg) != FUNCTION_DECL)
20651 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20652 -- but the deduction does not succeed because the expression is
20653 not just the function on its own. */
20654 return false;
20655 else
20656 for (lkp_iterator iter (arg); iter; ++iter)
20657 {
20658 tree fn = *iter;
20659 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
20660 strict, sub_strict, addr_p, explain_p)
20661 && (!goodfn || !decls_match (goodfn, fn)))
20662 {
20663 goodfn = fn;
20664 ++good;
20665 }
20666 }
20667
20668 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20669 to function or pointer to member function argument if the set of
20670 overloaded functions does not contain function templates and at most
20671 one of a set of overloaded functions provides a unique match.
20672
20673 So if we found multiple possibilities, we return success but don't
20674 deduce anything. */
20675
20676 if (good == 1)
20677 {
20678 int i = TREE_VEC_LENGTH (targs);
20679 for (; i--; )
20680 if (TREE_VEC_ELT (tempargs, i))
20681 {
20682 tree old = TREE_VEC_ELT (targs, i);
20683 tree new_ = TREE_VEC_ELT (tempargs, i);
20684 if (new_ && old && ARGUMENT_PACK_P (old)
20685 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
20686 /* Don't forget explicit template arguments in a pack. */
20687 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
20688 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
20689 TREE_VEC_ELT (targs, i) = new_;
20690 }
20691 }
20692 if (good)
20693 return true;
20694
20695 return false;
20696 }
20697
20698 /* Core DR 115: In contexts where deduction is done and fails, or in
20699 contexts where deduction is not done, if a template argument list is
20700 specified and it, along with any default template arguments, identifies
20701 a single function template specialization, then the template-id is an
20702 lvalue for the function template specialization. */
20703
20704 tree
20705 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
20706 {
20707 tree expr, offset, baselink;
20708 bool addr;
20709
20710 if (!type_unknown_p (orig_expr))
20711 return orig_expr;
20712
20713 expr = orig_expr;
20714 addr = false;
20715 offset = NULL_TREE;
20716 baselink = NULL_TREE;
20717
20718 if (TREE_CODE (expr) == ADDR_EXPR)
20719 {
20720 expr = TREE_OPERAND (expr, 0);
20721 addr = true;
20722 }
20723 if (TREE_CODE (expr) == OFFSET_REF)
20724 {
20725 offset = expr;
20726 expr = TREE_OPERAND (expr, 1);
20727 }
20728 if (BASELINK_P (expr))
20729 {
20730 baselink = expr;
20731 expr = BASELINK_FUNCTIONS (expr);
20732 }
20733
20734 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
20735 {
20736 int good = 0;
20737 tree goodfn = NULL_TREE;
20738
20739 /* If we got some explicit template args, we need to plug them into
20740 the affected templates before we try to unify, in case the
20741 explicit args will completely resolve the templates in question. */
20742
20743 tree expl_subargs = TREE_OPERAND (expr, 1);
20744 tree arg = TREE_OPERAND (expr, 0);
20745 tree badfn = NULL_TREE;
20746 tree badargs = NULL_TREE;
20747
20748 for (lkp_iterator iter (arg); iter; ++iter)
20749 {
20750 tree fn = *iter;
20751 tree subargs, elem;
20752
20753 if (TREE_CODE (fn) != TEMPLATE_DECL)
20754 continue;
20755
20756 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20757 expl_subargs, NULL_TREE, tf_none,
20758 /*require_all_args=*/true,
20759 /*use_default_args=*/true);
20760 if (subargs != error_mark_node
20761 && !any_dependent_template_arguments_p (subargs))
20762 {
20763 elem = instantiate_template (fn, subargs, tf_none);
20764 if (elem == error_mark_node)
20765 {
20766 badfn = fn;
20767 badargs = subargs;
20768 }
20769 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20770 {
20771 goodfn = elem;
20772 ++good;
20773 }
20774 }
20775 }
20776 if (good == 1)
20777 {
20778 mark_used (goodfn);
20779 expr = goodfn;
20780 if (baselink)
20781 expr = build_baselink (BASELINK_BINFO (baselink),
20782 BASELINK_ACCESS_BINFO (baselink),
20783 expr, BASELINK_OPTYPE (baselink));
20784 if (offset)
20785 {
20786 tree base
20787 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20788 expr = build_offset_ref (base, expr, addr, complain);
20789 }
20790 if (addr)
20791 expr = cp_build_addr_expr (expr, complain);
20792 return expr;
20793 }
20794 else if (good == 0 && badargs && (complain & tf_error))
20795 /* There were no good options and at least one bad one, so let the
20796 user know what the problem is. */
20797 instantiate_template (badfn, badargs, complain);
20798 }
20799 return orig_expr;
20800 }
20801
20802 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20803 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20804 different overloads deduce different arguments for a given parm.
20805 ADDR_P is true if the expression for which deduction is being
20806 performed was of the form "& fn" rather than simply "fn".
20807
20808 Returns 1 on success. */
20809
20810 static int
20811 try_one_overload (tree tparms,
20812 tree orig_targs,
20813 tree targs,
20814 tree parm,
20815 tree arg,
20816 unification_kind_t strict,
20817 int sub_strict,
20818 bool addr_p,
20819 bool explain_p)
20820 {
20821 int nargs;
20822 tree tempargs;
20823 int i;
20824
20825 if (arg == error_mark_node)
20826 return 0;
20827
20828 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20829 to function or pointer to member function argument if the set of
20830 overloaded functions does not contain function templates and at most
20831 one of a set of overloaded functions provides a unique match.
20832
20833 So if this is a template, just return success. */
20834
20835 if (uses_template_parms (arg))
20836 return 1;
20837
20838 if (TREE_CODE (arg) == METHOD_TYPE)
20839 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20840 else if (addr_p)
20841 arg = build_pointer_type (arg);
20842
20843 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20844
20845 /* We don't copy orig_targs for this because if we have already deduced
20846 some template args from previous args, unify would complain when we
20847 try to deduce a template parameter for the same argument, even though
20848 there isn't really a conflict. */
20849 nargs = TREE_VEC_LENGTH (targs);
20850 tempargs = make_tree_vec (nargs);
20851
20852 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20853 return 0;
20854
20855 /* First make sure we didn't deduce anything that conflicts with
20856 explicitly specified args. */
20857 for (i = nargs; i--; )
20858 {
20859 tree elt = TREE_VEC_ELT (tempargs, i);
20860 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20861
20862 if (!elt)
20863 /*NOP*/;
20864 else if (uses_template_parms (elt))
20865 /* Since we're unifying against ourselves, we will fill in
20866 template args used in the function parm list with our own
20867 template parms. Discard them. */
20868 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20869 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20870 {
20871 /* Check that the argument at each index of the deduced argument pack
20872 is equivalent to the corresponding explicitly specified argument.
20873 We may have deduced more arguments than were explicitly specified,
20874 and that's OK. */
20875
20876 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20877 that's wrong if we deduce the same argument pack from multiple
20878 function arguments: it's only incomplete the first time. */
20879
20880 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20881 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20882
20883 if (TREE_VEC_LENGTH (deduced_pack)
20884 < TREE_VEC_LENGTH (explicit_pack))
20885 return 0;
20886
20887 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20888 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20889 TREE_VEC_ELT (deduced_pack, j)))
20890 return 0;
20891 }
20892 else if (oldelt && !template_args_equal (oldelt, elt))
20893 return 0;
20894 }
20895
20896 for (i = nargs; i--; )
20897 {
20898 tree elt = TREE_VEC_ELT (tempargs, i);
20899
20900 if (elt)
20901 TREE_VEC_ELT (targs, i) = elt;
20902 }
20903
20904 return 1;
20905 }
20906
20907 /* PARM is a template class (perhaps with unbound template
20908 parameters). ARG is a fully instantiated type. If ARG can be
20909 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20910 TARGS are as for unify. */
20911
20912 static tree
20913 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20914 bool explain_p)
20915 {
20916 tree copy_of_targs;
20917
20918 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20919 return NULL_TREE;
20920 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20921 /* Matches anything. */;
20922 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20923 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20924 return NULL_TREE;
20925
20926 /* We need to make a new template argument vector for the call to
20927 unify. If we used TARGS, we'd clutter it up with the result of
20928 the attempted unification, even if this class didn't work out.
20929 We also don't want to commit ourselves to all the unifications
20930 we've already done, since unification is supposed to be done on
20931 an argument-by-argument basis. In other words, consider the
20932 following pathological case:
20933
20934 template <int I, int J, int K>
20935 struct S {};
20936
20937 template <int I, int J>
20938 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20939
20940 template <int I, int J, int K>
20941 void f(S<I, J, K>, S<I, I, I>);
20942
20943 void g() {
20944 S<0, 0, 0> s0;
20945 S<0, 1, 2> s2;
20946
20947 f(s0, s2);
20948 }
20949
20950 Now, by the time we consider the unification involving `s2', we
20951 already know that we must have `f<0, 0, 0>'. But, even though
20952 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20953 because there are two ways to unify base classes of S<0, 1, 2>
20954 with S<I, I, I>. If we kept the already deduced knowledge, we
20955 would reject the possibility I=1. */
20956 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20957
20958 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20959 {
20960 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20961 return NULL_TREE;
20962 return arg;
20963 }
20964
20965 /* If unification failed, we're done. */
20966 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20967 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20968 return NULL_TREE;
20969
20970 return arg;
20971 }
20972
20973 /* Given a template type PARM and a class type ARG, find the unique
20974 base type in ARG that is an instance of PARM. We do not examine
20975 ARG itself; only its base-classes. If there is not exactly one
20976 appropriate base class, return NULL_TREE. PARM may be the type of
20977 a partial specialization, as well as a plain template type. Used
20978 by unify. */
20979
20980 static enum template_base_result
20981 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20982 bool explain_p, tree *result)
20983 {
20984 tree rval = NULL_TREE;
20985 tree binfo;
20986
20987 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20988
20989 binfo = TYPE_BINFO (complete_type (arg));
20990 if (!binfo)
20991 {
20992 /* The type could not be completed. */
20993 *result = NULL_TREE;
20994 return tbr_incomplete_type;
20995 }
20996
20997 /* Walk in inheritance graph order. The search order is not
20998 important, and this avoids multiple walks of virtual bases. */
20999 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
21000 {
21001 tree r = try_class_unification (tparms, targs, parm,
21002 BINFO_TYPE (binfo), explain_p);
21003
21004 if (r)
21005 {
21006 /* If there is more than one satisfactory baseclass, then:
21007
21008 [temp.deduct.call]
21009
21010 If they yield more than one possible deduced A, the type
21011 deduction fails.
21012
21013 applies. */
21014 if (rval && !same_type_p (r, rval))
21015 {
21016 *result = NULL_TREE;
21017 return tbr_ambiguous_baseclass;
21018 }
21019
21020 rval = r;
21021 }
21022 }
21023
21024 *result = rval;
21025 return tbr_success;
21026 }
21027
21028 /* Returns the level of DECL, which declares a template parameter. */
21029
21030 static int
21031 template_decl_level (tree decl)
21032 {
21033 switch (TREE_CODE (decl))
21034 {
21035 case TYPE_DECL:
21036 case TEMPLATE_DECL:
21037 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
21038
21039 case PARM_DECL:
21040 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
21041
21042 default:
21043 gcc_unreachable ();
21044 }
21045 return 0;
21046 }
21047
21048 /* Decide whether ARG can be unified with PARM, considering only the
21049 cv-qualifiers of each type, given STRICT as documented for unify.
21050 Returns nonzero iff the unification is OK on that basis. */
21051
21052 static int
21053 check_cv_quals_for_unify (int strict, tree arg, tree parm)
21054 {
21055 int arg_quals = cp_type_quals (arg);
21056 int parm_quals = cp_type_quals (parm);
21057
21058 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21059 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21060 {
21061 /* Although a CVR qualifier is ignored when being applied to a
21062 substituted template parameter ([8.3.2]/1 for example), that
21063 does not allow us to unify "const T" with "int&" because both
21064 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21065 It is ok when we're allowing additional CV qualifiers
21066 at the outer level [14.8.2.1]/3,1st bullet. */
21067 if ((TYPE_REF_P (arg)
21068 || TREE_CODE (arg) == FUNCTION_TYPE
21069 || TREE_CODE (arg) == METHOD_TYPE)
21070 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
21071 return 0;
21072
21073 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
21074 && (parm_quals & TYPE_QUAL_RESTRICT))
21075 return 0;
21076 }
21077
21078 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21079 && (arg_quals & parm_quals) != parm_quals)
21080 return 0;
21081
21082 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
21083 && (parm_quals & arg_quals) != arg_quals)
21084 return 0;
21085
21086 return 1;
21087 }
21088
21089 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21090 void
21091 template_parm_level_and_index (tree parm, int* level, int* index)
21092 {
21093 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21094 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21095 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21096 {
21097 *index = TEMPLATE_TYPE_IDX (parm);
21098 *level = TEMPLATE_TYPE_LEVEL (parm);
21099 }
21100 else
21101 {
21102 *index = TEMPLATE_PARM_IDX (parm);
21103 *level = TEMPLATE_PARM_LEVEL (parm);
21104 }
21105 }
21106
21107 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21108 do { \
21109 if (unify (TP, TA, P, A, S, EP)) \
21110 return 1; \
21111 } while (0)
21112
21113 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21114 expansion at the end of PACKED_PARMS. Returns 0 if the type
21115 deduction succeeds, 1 otherwise. STRICT is the same as in
21116 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21117 function call argument list. We'll need to adjust the arguments to make them
21118 types. SUBR tells us if this is from a recursive call to
21119 type_unification_real, or for comparing two template argument
21120 lists. */
21121
21122 static int
21123 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
21124 tree packed_args, unification_kind_t strict,
21125 bool subr, bool explain_p)
21126 {
21127 tree parm
21128 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
21129 tree pattern = PACK_EXPANSION_PATTERN (parm);
21130 tree pack, packs = NULL_TREE;
21131 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
21132
21133 /* Add in any args remembered from an earlier partial instantiation. */
21134 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
21135 int levels = TMPL_ARGS_DEPTH (targs);
21136
21137 packed_args = expand_template_argument_pack (packed_args);
21138
21139 int len = TREE_VEC_LENGTH (packed_args);
21140
21141 /* Determine the parameter packs we will be deducing from the
21142 pattern, and record their current deductions. */
21143 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
21144 pack; pack = TREE_CHAIN (pack))
21145 {
21146 tree parm_pack = TREE_VALUE (pack);
21147 int idx, level;
21148
21149 /* Only template parameter packs can be deduced, not e.g. function
21150 parameter packs or __bases or __integer_pack. */
21151 if (!TEMPLATE_PARM_P (parm_pack))
21152 continue;
21153
21154 /* Determine the index and level of this parameter pack. */
21155 template_parm_level_and_index (parm_pack, &level, &idx);
21156 if (level < levels)
21157 continue;
21158
21159 /* Keep track of the parameter packs and their corresponding
21160 argument packs. */
21161 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
21162 TREE_TYPE (packs) = make_tree_vec (len - start);
21163 }
21164
21165 /* Loop through all of the arguments that have not yet been
21166 unified and unify each with the pattern. */
21167 for (i = start; i < len; i++)
21168 {
21169 tree parm;
21170 bool any_explicit = false;
21171 tree arg = TREE_VEC_ELT (packed_args, i);
21172
21173 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21174 or the element of its argument pack at the current index if
21175 this argument was explicitly specified. */
21176 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21177 {
21178 int idx, level;
21179 tree arg, pargs;
21180 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21181
21182 arg = NULL_TREE;
21183 if (TREE_VALUE (pack)
21184 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
21185 && (i - start < TREE_VEC_LENGTH (pargs)))
21186 {
21187 any_explicit = true;
21188 arg = TREE_VEC_ELT (pargs, i - start);
21189 }
21190 TMPL_ARG (targs, level, idx) = arg;
21191 }
21192
21193 /* If we had explicit template arguments, substitute them into the
21194 pattern before deduction. */
21195 if (any_explicit)
21196 {
21197 /* Some arguments might still be unspecified or dependent. */
21198 bool dependent;
21199 ++processing_template_decl;
21200 dependent = any_dependent_template_arguments_p (targs);
21201 if (!dependent)
21202 --processing_template_decl;
21203 parm = tsubst (pattern, targs,
21204 explain_p ? tf_warning_or_error : tf_none,
21205 NULL_TREE);
21206 if (dependent)
21207 --processing_template_decl;
21208 if (parm == error_mark_node)
21209 return 1;
21210 }
21211 else
21212 parm = pattern;
21213
21214 /* Unify the pattern with the current argument. */
21215 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
21216 explain_p))
21217 return 1;
21218
21219 /* For each parameter pack, collect the deduced value. */
21220 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21221 {
21222 int idx, level;
21223 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21224
21225 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
21226 TMPL_ARG (targs, level, idx);
21227 }
21228 }
21229
21230 /* Verify that the results of unification with the parameter packs
21231 produce results consistent with what we've seen before, and make
21232 the deduced argument packs available. */
21233 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21234 {
21235 tree old_pack = TREE_VALUE (pack);
21236 tree new_args = TREE_TYPE (pack);
21237 int i, len = TREE_VEC_LENGTH (new_args);
21238 int idx, level;
21239 bool nondeduced_p = false;
21240
21241 /* By default keep the original deduced argument pack.
21242 If necessary, more specific code is going to update the
21243 resulting deduced argument later down in this function. */
21244 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21245 TMPL_ARG (targs, level, idx) = old_pack;
21246
21247 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21248 actually deduce anything. */
21249 for (i = 0; i < len && !nondeduced_p; ++i)
21250 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
21251 nondeduced_p = true;
21252 if (nondeduced_p)
21253 continue;
21254
21255 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
21256 {
21257 /* If we had fewer function args than explicit template args,
21258 just use the explicits. */
21259 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21260 int explicit_len = TREE_VEC_LENGTH (explicit_args);
21261 if (len < explicit_len)
21262 new_args = explicit_args;
21263 }
21264
21265 if (!old_pack)
21266 {
21267 tree result;
21268 /* Build the deduced *_ARGUMENT_PACK. */
21269 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
21270 {
21271 result = make_node (NONTYPE_ARGUMENT_PACK);
21272 TREE_CONSTANT (result) = 1;
21273 }
21274 else
21275 result = cxx_make_type (TYPE_ARGUMENT_PACK);
21276
21277 SET_ARGUMENT_PACK_ARGS (result, new_args);
21278
21279 /* Note the deduced argument packs for this parameter
21280 pack. */
21281 TMPL_ARG (targs, level, idx) = result;
21282 }
21283 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
21284 && (ARGUMENT_PACK_ARGS (old_pack)
21285 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
21286 {
21287 /* We only had the explicitly-provided arguments before, but
21288 now we have a complete set of arguments. */
21289 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21290
21291 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
21292 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
21293 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
21294 }
21295 else
21296 {
21297 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
21298 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
21299
21300 if (!comp_template_args (old_args, new_args,
21301 &bad_old_arg, &bad_new_arg))
21302 /* Inconsistent unification of this parameter pack. */
21303 return unify_parameter_pack_inconsistent (explain_p,
21304 bad_old_arg,
21305 bad_new_arg);
21306 }
21307 }
21308
21309 return unify_success (explain_p);
21310 }
21311
21312 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21313 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21314 parameters and return value are as for unify. */
21315
21316 static int
21317 unify_array_domain (tree tparms, tree targs,
21318 tree parm_dom, tree arg_dom,
21319 bool explain_p)
21320 {
21321 tree parm_max;
21322 tree arg_max;
21323 bool parm_cst;
21324 bool arg_cst;
21325
21326 /* Our representation of array types uses "N - 1" as the
21327 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21328 not an integer constant. We cannot unify arbitrarily
21329 complex expressions, so we eliminate the MINUS_EXPRs
21330 here. */
21331 parm_max = TYPE_MAX_VALUE (parm_dom);
21332 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
21333 if (!parm_cst)
21334 {
21335 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
21336 parm_max = TREE_OPERAND (parm_max, 0);
21337 }
21338 arg_max = TYPE_MAX_VALUE (arg_dom);
21339 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
21340 if (!arg_cst)
21341 {
21342 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21343 trying to unify the type of a variable with the type
21344 of a template parameter. For example:
21345
21346 template <unsigned int N>
21347 void f (char (&) [N]);
21348 int g();
21349 void h(int i) {
21350 char a[g(i)];
21351 f(a);
21352 }
21353
21354 Here, the type of the ARG will be "int [g(i)]", and
21355 may be a SAVE_EXPR, etc. */
21356 if (TREE_CODE (arg_max) != MINUS_EXPR)
21357 return unify_vla_arg (explain_p, arg_dom);
21358 arg_max = TREE_OPERAND (arg_max, 0);
21359 }
21360
21361 /* If only one of the bounds used a MINUS_EXPR, compensate
21362 by adding one to the other bound. */
21363 if (parm_cst && !arg_cst)
21364 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
21365 integer_type_node,
21366 parm_max,
21367 integer_one_node);
21368 else if (arg_cst && !parm_cst)
21369 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
21370 integer_type_node,
21371 arg_max,
21372 integer_one_node);
21373
21374 return unify (tparms, targs, parm_max, arg_max,
21375 UNIFY_ALLOW_INTEGER, explain_p);
21376 }
21377
21378 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21379
21380 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
21381
21382 static pa_kind_t
21383 pa_kind (tree t)
21384 {
21385 if (PACK_EXPANSION_P (t))
21386 t = PACK_EXPANSION_PATTERN (t);
21387 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
21388 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
21389 || DECL_TYPE_TEMPLATE_P (t))
21390 return pa_tmpl;
21391 else if (TYPE_P (t))
21392 return pa_type;
21393 else
21394 return pa_expr;
21395 }
21396
21397 /* Deduce the value of template parameters. TPARMS is the (innermost)
21398 set of template parameters to a template. TARGS is the bindings
21399 for those template parameters, as determined thus far; TARGS may
21400 include template arguments for outer levels of template parameters
21401 as well. PARM is a parameter to a template function, or a
21402 subcomponent of that parameter; ARG is the corresponding argument.
21403 This function attempts to match PARM with ARG in a manner
21404 consistent with the existing assignments in TARGS. If more values
21405 are deduced, then TARGS is updated.
21406
21407 Returns 0 if the type deduction succeeds, 1 otherwise. The
21408 parameter STRICT is a bitwise or of the following flags:
21409
21410 UNIFY_ALLOW_NONE:
21411 Require an exact match between PARM and ARG.
21412 UNIFY_ALLOW_MORE_CV_QUAL:
21413 Allow the deduced ARG to be more cv-qualified (by qualification
21414 conversion) than ARG.
21415 UNIFY_ALLOW_LESS_CV_QUAL:
21416 Allow the deduced ARG to be less cv-qualified than ARG.
21417 UNIFY_ALLOW_DERIVED:
21418 Allow the deduced ARG to be a template base class of ARG,
21419 or a pointer to a template base class of the type pointed to by
21420 ARG.
21421 UNIFY_ALLOW_INTEGER:
21422 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
21423 case for more information.
21424 UNIFY_ALLOW_OUTER_LEVEL:
21425 This is the outermost level of a deduction. Used to determine validity
21426 of qualification conversions. A valid qualification conversion must
21427 have const qualified pointers leading up to the inner type which
21428 requires additional CV quals, except at the outer level, where const
21429 is not required [conv.qual]. It would be normal to set this flag in
21430 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
21431 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
21432 This is the outermost level of a deduction, and PARM can be more CV
21433 qualified at this point.
21434 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
21435 This is the outermost level of a deduction, and PARM can be less CV
21436 qualified at this point. */
21437
21438 static int
21439 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
21440 bool explain_p)
21441 {
21442 int idx;
21443 tree targ;
21444 tree tparm;
21445 int strict_in = strict;
21446 tsubst_flags_t complain = (explain_p
21447 ? tf_warning_or_error
21448 : tf_none);
21449
21450 /* I don't think this will do the right thing with respect to types.
21451 But the only case I've seen it in so far has been array bounds, where
21452 signedness is the only information lost, and I think that will be
21453 okay. */
21454 while (CONVERT_EXPR_P (parm))
21455 parm = TREE_OPERAND (parm, 0);
21456
21457 if (arg == error_mark_node)
21458 return unify_invalid (explain_p);
21459 if (arg == unknown_type_node
21460 || arg == init_list_type_node)
21461 /* We can't deduce anything from this, but we might get all the
21462 template args from other function args. */
21463 return unify_success (explain_p);
21464
21465 if (parm == any_targ_node || arg == any_targ_node)
21466 return unify_success (explain_p);
21467
21468 /* If PARM uses template parameters, then we can't bail out here,
21469 even if ARG == PARM, since we won't record unifications for the
21470 template parameters. We might need them if we're trying to
21471 figure out which of two things is more specialized. */
21472 if (arg == parm && !uses_template_parms (parm))
21473 return unify_success (explain_p);
21474
21475 /* Handle init lists early, so the rest of the function can assume
21476 we're dealing with a type. */
21477 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
21478 {
21479 tree elt, elttype;
21480 unsigned i;
21481 tree orig_parm = parm;
21482
21483 /* Replace T with std::initializer_list<T> for deduction. */
21484 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21485 && flag_deduce_init_list)
21486 parm = listify (parm);
21487
21488 if (!is_std_init_list (parm)
21489 && TREE_CODE (parm) != ARRAY_TYPE)
21490 /* We can only deduce from an initializer list argument if the
21491 parameter is std::initializer_list or an array; otherwise this
21492 is a non-deduced context. */
21493 return unify_success (explain_p);
21494
21495 if (TREE_CODE (parm) == ARRAY_TYPE)
21496 elttype = TREE_TYPE (parm);
21497 else
21498 {
21499 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
21500 /* Deduction is defined in terms of a single type, so just punt
21501 on the (bizarre) std::initializer_list<T...>. */
21502 if (PACK_EXPANSION_P (elttype))
21503 return unify_success (explain_p);
21504 }
21505
21506 if (strict != DEDUCE_EXACT
21507 && TYPE_P (elttype)
21508 && !uses_deducible_template_parms (elttype))
21509 /* If ELTTYPE has no deducible template parms, skip deduction from
21510 the list elements. */;
21511 else
21512 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
21513 {
21514 int elt_strict = strict;
21515
21516 if (elt == error_mark_node)
21517 return unify_invalid (explain_p);
21518
21519 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
21520 {
21521 tree type = TREE_TYPE (elt);
21522 if (type == error_mark_node)
21523 return unify_invalid (explain_p);
21524 /* It should only be possible to get here for a call. */
21525 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
21526 elt_strict |= maybe_adjust_types_for_deduction
21527 (DEDUCE_CALL, &elttype, &type, elt);
21528 elt = type;
21529 }
21530
21531 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
21532 explain_p);
21533 }
21534
21535 if (TREE_CODE (parm) == ARRAY_TYPE
21536 && deducible_array_bound (TYPE_DOMAIN (parm)))
21537 {
21538 /* Also deduce from the length of the initializer list. */
21539 tree max = size_int (CONSTRUCTOR_NELTS (arg));
21540 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
21541 if (idx == error_mark_node)
21542 return unify_invalid (explain_p);
21543 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21544 idx, explain_p);
21545 }
21546
21547 /* If the std::initializer_list<T> deduction worked, replace the
21548 deduced A with std::initializer_list<A>. */
21549 if (orig_parm != parm)
21550 {
21551 idx = TEMPLATE_TYPE_IDX (orig_parm);
21552 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21553 targ = listify (targ);
21554 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
21555 }
21556 return unify_success (explain_p);
21557 }
21558
21559 /* If parm and arg aren't the same kind of thing (template, type, or
21560 expression), fail early. */
21561 if (pa_kind (parm) != pa_kind (arg))
21562 return unify_invalid (explain_p);
21563
21564 /* Immediately reject some pairs that won't unify because of
21565 cv-qualification mismatches. */
21566 if (TREE_CODE (arg) == TREE_CODE (parm)
21567 && TYPE_P (arg)
21568 /* It is the elements of the array which hold the cv quals of an array
21569 type, and the elements might be template type parms. We'll check
21570 when we recurse. */
21571 && TREE_CODE (arg) != ARRAY_TYPE
21572 /* We check the cv-qualifiers when unifying with template type
21573 parameters below. We want to allow ARG `const T' to unify with
21574 PARM `T' for example, when computing which of two templates
21575 is more specialized, for example. */
21576 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
21577 && !check_cv_quals_for_unify (strict_in, arg, parm))
21578 return unify_cv_qual_mismatch (explain_p, parm, arg);
21579
21580 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
21581 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
21582 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
21583 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
21584 strict &= ~UNIFY_ALLOW_DERIVED;
21585 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21586 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
21587
21588 switch (TREE_CODE (parm))
21589 {
21590 case TYPENAME_TYPE:
21591 case SCOPE_REF:
21592 case UNBOUND_CLASS_TEMPLATE:
21593 /* In a type which contains a nested-name-specifier, template
21594 argument values cannot be deduced for template parameters used
21595 within the nested-name-specifier. */
21596 return unify_success (explain_p);
21597
21598 case TEMPLATE_TYPE_PARM:
21599 case TEMPLATE_TEMPLATE_PARM:
21600 case BOUND_TEMPLATE_TEMPLATE_PARM:
21601 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21602 if (error_operand_p (tparm))
21603 return unify_invalid (explain_p);
21604
21605 if (TEMPLATE_TYPE_LEVEL (parm)
21606 != template_decl_level (tparm))
21607 /* The PARM is not one we're trying to unify. Just check
21608 to see if it matches ARG. */
21609 {
21610 if (TREE_CODE (arg) == TREE_CODE (parm)
21611 && (is_auto (parm) ? is_auto (arg)
21612 : same_type_p (parm, arg)))
21613 return unify_success (explain_p);
21614 else
21615 return unify_type_mismatch (explain_p, parm, arg);
21616 }
21617 idx = TEMPLATE_TYPE_IDX (parm);
21618 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21619 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
21620 if (error_operand_p (tparm))
21621 return unify_invalid (explain_p);
21622
21623 /* Check for mixed types and values. */
21624 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21625 && TREE_CODE (tparm) != TYPE_DECL)
21626 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21627 && TREE_CODE (tparm) != TEMPLATE_DECL))
21628 gcc_unreachable ();
21629
21630 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21631 {
21632 if ((strict_in & UNIFY_ALLOW_DERIVED)
21633 && CLASS_TYPE_P (arg))
21634 {
21635 /* First try to match ARG directly. */
21636 tree t = try_class_unification (tparms, targs, parm, arg,
21637 explain_p);
21638 if (!t)
21639 {
21640 /* Otherwise, look for a suitable base of ARG, as below. */
21641 enum template_base_result r;
21642 r = get_template_base (tparms, targs, parm, arg,
21643 explain_p, &t);
21644 if (!t)
21645 return unify_no_common_base (explain_p, r, parm, arg);
21646 arg = t;
21647 }
21648 }
21649 /* ARG must be constructed from a template class or a template
21650 template parameter. */
21651 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
21652 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21653 return unify_template_deduction_failure (explain_p, parm, arg);
21654
21655 /* Deduce arguments T, i from TT<T> or TT<i>. */
21656 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
21657 return 1;
21658
21659 arg = TYPE_TI_TEMPLATE (arg);
21660
21661 /* Fall through to deduce template name. */
21662 }
21663
21664 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21665 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21666 {
21667 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21668
21669 /* Simple cases: Value already set, does match or doesn't. */
21670 if (targ != NULL_TREE && template_args_equal (targ, arg))
21671 return unify_success (explain_p);
21672 else if (targ)
21673 return unify_inconsistency (explain_p, parm, targ, arg);
21674 }
21675 else
21676 {
21677 /* If PARM is `const T' and ARG is only `int', we don't have
21678 a match unless we are allowing additional qualification.
21679 If ARG is `const int' and PARM is just `T' that's OK;
21680 that binds `const int' to `T'. */
21681 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
21682 arg, parm))
21683 return unify_cv_qual_mismatch (explain_p, parm, arg);
21684
21685 /* Consider the case where ARG is `const volatile int' and
21686 PARM is `const T'. Then, T should be `volatile int'. */
21687 arg = cp_build_qualified_type_real
21688 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
21689 if (arg == error_mark_node)
21690 return unify_invalid (explain_p);
21691
21692 /* Simple cases: Value already set, does match or doesn't. */
21693 if (targ != NULL_TREE && same_type_p (targ, arg))
21694 return unify_success (explain_p);
21695 else if (targ)
21696 return unify_inconsistency (explain_p, parm, targ, arg);
21697
21698 /* Make sure that ARG is not a variable-sized array. (Note
21699 that were talking about variable-sized arrays (like
21700 `int[n]'), rather than arrays of unknown size (like
21701 `int[]').) We'll get very confused by such a type since
21702 the bound of the array is not constant, and therefore
21703 not mangleable. Besides, such types are not allowed in
21704 ISO C++, so we can do as we please here. We do allow
21705 them for 'auto' deduction, since that isn't ABI-exposed. */
21706 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
21707 return unify_vla_arg (explain_p, arg);
21708
21709 /* Strip typedefs as in convert_template_argument. */
21710 arg = canonicalize_type_argument (arg, tf_none);
21711 }
21712
21713 /* If ARG is a parameter pack or an expansion, we cannot unify
21714 against it unless PARM is also a parameter pack. */
21715 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21716 && !template_parameter_pack_p (parm))
21717 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21718
21719 /* If the argument deduction results is a METHOD_TYPE,
21720 then there is a problem.
21721 METHOD_TYPE doesn't map to any real C++ type the result of
21722 the deduction can not be of that type. */
21723 if (TREE_CODE (arg) == METHOD_TYPE)
21724 return unify_method_type_error (explain_p, arg);
21725
21726 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21727 return unify_success (explain_p);
21728
21729 case TEMPLATE_PARM_INDEX:
21730 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21731 if (error_operand_p (tparm))
21732 return unify_invalid (explain_p);
21733
21734 if (TEMPLATE_PARM_LEVEL (parm)
21735 != template_decl_level (tparm))
21736 {
21737 /* The PARM is not one we're trying to unify. Just check
21738 to see if it matches ARG. */
21739 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
21740 && cp_tree_equal (parm, arg));
21741 if (result)
21742 unify_expression_unequal (explain_p, parm, arg);
21743 return result;
21744 }
21745
21746 idx = TEMPLATE_PARM_IDX (parm);
21747 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21748
21749 if (targ)
21750 {
21751 if ((strict & UNIFY_ALLOW_INTEGER)
21752 && TREE_TYPE (targ) && TREE_TYPE (arg)
21753 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
21754 /* We're deducing from an array bound, the type doesn't matter. */
21755 arg = fold_convert (TREE_TYPE (targ), arg);
21756 int x = !cp_tree_equal (targ, arg);
21757 if (x)
21758 unify_inconsistency (explain_p, parm, targ, arg);
21759 return x;
21760 }
21761
21762 /* [temp.deduct.type] If, in the declaration of a function template
21763 with a non-type template-parameter, the non-type
21764 template-parameter is used in an expression in the function
21765 parameter-list and, if the corresponding template-argument is
21766 deduced, the template-argument type shall match the type of the
21767 template-parameter exactly, except that a template-argument
21768 deduced from an array bound may be of any integral type.
21769 The non-type parameter might use already deduced type parameters. */
21770 tparm = TREE_TYPE (parm);
21771 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
21772 /* We don't have enough levels of args to do any substitution. This
21773 can happen in the context of -fnew-ttp-matching. */;
21774 else
21775 {
21776 ++processing_template_decl;
21777 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
21778 --processing_template_decl;
21779
21780 if (tree a = type_uses_auto (tparm))
21781 {
21782 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21783 if (tparm == error_mark_node)
21784 return 1;
21785 }
21786 }
21787
21788 if (!TREE_TYPE (arg))
21789 /* Template-parameter dependent expression. Just accept it for now.
21790 It will later be processed in convert_template_argument. */
21791 ;
21792 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21793 non_reference (tparm)))
21794 /* OK */;
21795 else if ((strict & UNIFY_ALLOW_INTEGER)
21796 && CP_INTEGRAL_TYPE_P (tparm))
21797 /* Convert the ARG to the type of PARM; the deduced non-type
21798 template argument must exactly match the types of the
21799 corresponding parameter. */
21800 arg = fold (build_nop (tparm, arg));
21801 else if (uses_template_parms (tparm))
21802 {
21803 /* We haven't deduced the type of this parameter yet. */
21804 if (cxx_dialect >= cxx17
21805 /* We deduce from array bounds in try_array_deduction. */
21806 && !(strict & UNIFY_ALLOW_INTEGER))
21807 {
21808 /* Deduce it from the non-type argument. */
21809 tree atype = TREE_TYPE (arg);
21810 RECUR_AND_CHECK_FAILURE (tparms, targs,
21811 tparm, atype,
21812 UNIFY_ALLOW_NONE, explain_p);
21813 }
21814 else
21815 /* Try again later. */
21816 return unify_success (explain_p);
21817 }
21818 else
21819 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21820
21821 /* If ARG is a parameter pack or an expansion, we cannot unify
21822 against it unless PARM is also a parameter pack. */
21823 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21824 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21825 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21826
21827 {
21828 bool removed_attr = false;
21829 arg = strip_typedefs_expr (arg, &removed_attr);
21830 }
21831 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21832 return unify_success (explain_p);
21833
21834 case PTRMEM_CST:
21835 {
21836 /* A pointer-to-member constant can be unified only with
21837 another constant. */
21838 if (TREE_CODE (arg) != PTRMEM_CST)
21839 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21840
21841 /* Just unify the class member. It would be useless (and possibly
21842 wrong, depending on the strict flags) to unify also
21843 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21844 arg refer to the same variable, even if through different
21845 classes. For instance:
21846
21847 struct A { int x; };
21848 struct B : A { };
21849
21850 Unification of &A::x and &B::x must succeed. */
21851 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21852 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21853 }
21854
21855 case POINTER_TYPE:
21856 {
21857 if (!TYPE_PTR_P (arg))
21858 return unify_type_mismatch (explain_p, parm, arg);
21859
21860 /* [temp.deduct.call]
21861
21862 A can be another pointer or pointer to member type that can
21863 be converted to the deduced A via a qualification
21864 conversion (_conv.qual_).
21865
21866 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21867 This will allow for additional cv-qualification of the
21868 pointed-to types if appropriate. */
21869
21870 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21871 /* The derived-to-base conversion only persists through one
21872 level of pointers. */
21873 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21874
21875 return unify (tparms, targs, TREE_TYPE (parm),
21876 TREE_TYPE (arg), strict, explain_p);
21877 }
21878
21879 case REFERENCE_TYPE:
21880 if (!TYPE_REF_P (arg))
21881 return unify_type_mismatch (explain_p, parm, arg);
21882 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21883 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21884
21885 case ARRAY_TYPE:
21886 if (TREE_CODE (arg) != ARRAY_TYPE)
21887 return unify_type_mismatch (explain_p, parm, arg);
21888 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21889 != (TYPE_DOMAIN (arg) == NULL_TREE))
21890 return unify_type_mismatch (explain_p, parm, arg);
21891 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21892 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21893 if (TYPE_DOMAIN (parm) != NULL_TREE)
21894 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21895 TYPE_DOMAIN (arg), explain_p);
21896 return unify_success (explain_p);
21897
21898 case REAL_TYPE:
21899 case COMPLEX_TYPE:
21900 case VECTOR_TYPE:
21901 case INTEGER_TYPE:
21902 case BOOLEAN_TYPE:
21903 case ENUMERAL_TYPE:
21904 case VOID_TYPE:
21905 case NULLPTR_TYPE:
21906 if (TREE_CODE (arg) != TREE_CODE (parm))
21907 return unify_type_mismatch (explain_p, parm, arg);
21908
21909 /* We have already checked cv-qualification at the top of the
21910 function. */
21911 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21912 return unify_type_mismatch (explain_p, parm, arg);
21913
21914 /* As far as unification is concerned, this wins. Later checks
21915 will invalidate it if necessary. */
21916 return unify_success (explain_p);
21917
21918 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21919 /* Type INTEGER_CST can come from ordinary constant template args. */
21920 case INTEGER_CST:
21921 while (CONVERT_EXPR_P (arg))
21922 arg = TREE_OPERAND (arg, 0);
21923
21924 if (TREE_CODE (arg) != INTEGER_CST)
21925 return unify_template_argument_mismatch (explain_p, parm, arg);
21926 return (tree_int_cst_equal (parm, arg)
21927 ? unify_success (explain_p)
21928 : unify_template_argument_mismatch (explain_p, parm, arg));
21929
21930 case TREE_VEC:
21931 {
21932 int i, len, argslen;
21933 int parm_variadic_p = 0;
21934
21935 if (TREE_CODE (arg) != TREE_VEC)
21936 return unify_template_argument_mismatch (explain_p, parm, arg);
21937
21938 len = TREE_VEC_LENGTH (parm);
21939 argslen = TREE_VEC_LENGTH (arg);
21940
21941 /* Check for pack expansions in the parameters. */
21942 for (i = 0; i < len; ++i)
21943 {
21944 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21945 {
21946 if (i == len - 1)
21947 /* We can unify against something with a trailing
21948 parameter pack. */
21949 parm_variadic_p = 1;
21950 else
21951 /* [temp.deduct.type]/9: If the template argument list of
21952 P contains a pack expansion that is not the last
21953 template argument, the entire template argument list
21954 is a non-deduced context. */
21955 return unify_success (explain_p);
21956 }
21957 }
21958
21959 /* If we don't have enough arguments to satisfy the parameters
21960 (not counting the pack expression at the end), or we have
21961 too many arguments for a parameter list that doesn't end in
21962 a pack expression, we can't unify. */
21963 if (parm_variadic_p
21964 ? argslen < len - parm_variadic_p
21965 : argslen != len)
21966 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21967
21968 /* Unify all of the parameters that precede the (optional)
21969 pack expression. */
21970 for (i = 0; i < len - parm_variadic_p; ++i)
21971 {
21972 RECUR_AND_CHECK_FAILURE (tparms, targs,
21973 TREE_VEC_ELT (parm, i),
21974 TREE_VEC_ELT (arg, i),
21975 UNIFY_ALLOW_NONE, explain_p);
21976 }
21977 if (parm_variadic_p)
21978 return unify_pack_expansion (tparms, targs, parm, arg,
21979 DEDUCE_EXACT,
21980 /*subr=*/true, explain_p);
21981 return unify_success (explain_p);
21982 }
21983
21984 case RECORD_TYPE:
21985 case UNION_TYPE:
21986 if (TREE_CODE (arg) != TREE_CODE (parm))
21987 return unify_type_mismatch (explain_p, parm, arg);
21988
21989 if (TYPE_PTRMEMFUNC_P (parm))
21990 {
21991 if (!TYPE_PTRMEMFUNC_P (arg))
21992 return unify_type_mismatch (explain_p, parm, arg);
21993
21994 return unify (tparms, targs,
21995 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21996 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21997 strict, explain_p);
21998 }
21999 else if (TYPE_PTRMEMFUNC_P (arg))
22000 return unify_type_mismatch (explain_p, parm, arg);
22001
22002 if (CLASSTYPE_TEMPLATE_INFO (parm))
22003 {
22004 tree t = NULL_TREE;
22005
22006 if (strict_in & UNIFY_ALLOW_DERIVED)
22007 {
22008 /* First, we try to unify the PARM and ARG directly. */
22009 t = try_class_unification (tparms, targs,
22010 parm, arg, explain_p);
22011
22012 if (!t)
22013 {
22014 /* Fallback to the special case allowed in
22015 [temp.deduct.call]:
22016
22017 If P is a class, and P has the form
22018 template-id, then A can be a derived class of
22019 the deduced A. Likewise, if P is a pointer to
22020 a class of the form template-id, A can be a
22021 pointer to a derived class pointed to by the
22022 deduced A. */
22023 enum template_base_result r;
22024 r = get_template_base (tparms, targs, parm, arg,
22025 explain_p, &t);
22026
22027 if (!t)
22028 {
22029 /* Don't give the derived diagnostic if we're
22030 already dealing with the same template. */
22031 bool same_template
22032 = (CLASSTYPE_TEMPLATE_INFO (arg)
22033 && (CLASSTYPE_TI_TEMPLATE (parm)
22034 == CLASSTYPE_TI_TEMPLATE (arg)));
22035 return unify_no_common_base (explain_p && !same_template,
22036 r, parm, arg);
22037 }
22038 }
22039 }
22040 else if (CLASSTYPE_TEMPLATE_INFO (arg)
22041 && (CLASSTYPE_TI_TEMPLATE (parm)
22042 == CLASSTYPE_TI_TEMPLATE (arg)))
22043 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22044 Then, we should unify `int' and `U'. */
22045 t = arg;
22046 else
22047 /* There's no chance of unification succeeding. */
22048 return unify_type_mismatch (explain_p, parm, arg);
22049
22050 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
22051 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
22052 }
22053 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
22054 return unify_type_mismatch (explain_p, parm, arg);
22055 return unify_success (explain_p);
22056
22057 case METHOD_TYPE:
22058 case FUNCTION_TYPE:
22059 {
22060 unsigned int nargs;
22061 tree *args;
22062 tree a;
22063 unsigned int i;
22064
22065 if (TREE_CODE (arg) != TREE_CODE (parm))
22066 return unify_type_mismatch (explain_p, parm, arg);
22067
22068 /* CV qualifications for methods can never be deduced, they must
22069 match exactly. We need to check them explicitly here,
22070 because type_unification_real treats them as any other
22071 cv-qualified parameter. */
22072 if (TREE_CODE (parm) == METHOD_TYPE
22073 && (!check_cv_quals_for_unify
22074 (UNIFY_ALLOW_NONE,
22075 class_of_this_parm (arg),
22076 class_of_this_parm (parm))))
22077 return unify_cv_qual_mismatch (explain_p, parm, arg);
22078 if (TREE_CODE (arg) == FUNCTION_TYPE
22079 && type_memfn_quals (parm) != type_memfn_quals (arg))
22080 return unify_cv_qual_mismatch (explain_p, parm, arg);
22081 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
22082 return unify_type_mismatch (explain_p, parm, arg);
22083
22084 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
22085 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
22086
22087 nargs = list_length (TYPE_ARG_TYPES (arg));
22088 args = XALLOCAVEC (tree, nargs);
22089 for (a = TYPE_ARG_TYPES (arg), i = 0;
22090 a != NULL_TREE && a != void_list_node;
22091 a = TREE_CHAIN (a), ++i)
22092 args[i] = TREE_VALUE (a);
22093 nargs = i;
22094
22095 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
22096 args, nargs, 1, DEDUCE_EXACT,
22097 NULL, explain_p))
22098 return 1;
22099
22100 if (flag_noexcept_type)
22101 {
22102 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
22103 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
22104 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
22105 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
22106 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
22107 && uses_template_parms (TREE_PURPOSE (pspec)))
22108 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
22109 TREE_PURPOSE (aspec),
22110 UNIFY_ALLOW_NONE, explain_p);
22111 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
22112 return unify_type_mismatch (explain_p, parm, arg);
22113 }
22114
22115 return 0;
22116 }
22117
22118 case OFFSET_TYPE:
22119 /* Unify a pointer to member with a pointer to member function, which
22120 deduces the type of the member as a function type. */
22121 if (TYPE_PTRMEMFUNC_P (arg))
22122 {
22123 /* Check top-level cv qualifiers */
22124 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
22125 return unify_cv_qual_mismatch (explain_p, parm, arg);
22126
22127 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22128 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
22129 UNIFY_ALLOW_NONE, explain_p);
22130
22131 /* Determine the type of the function we are unifying against. */
22132 tree fntype = static_fn_type (arg);
22133
22134 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
22135 }
22136
22137 if (TREE_CODE (arg) != OFFSET_TYPE)
22138 return unify_type_mismatch (explain_p, parm, arg);
22139 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22140 TYPE_OFFSET_BASETYPE (arg),
22141 UNIFY_ALLOW_NONE, explain_p);
22142 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22143 strict, explain_p);
22144
22145 case CONST_DECL:
22146 if (DECL_TEMPLATE_PARM_P (parm))
22147 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
22148 if (arg != scalar_constant_value (parm))
22149 return unify_template_argument_mismatch (explain_p, parm, arg);
22150 return unify_success (explain_p);
22151
22152 case FIELD_DECL:
22153 case TEMPLATE_DECL:
22154 /* Matched cases are handled by the ARG == PARM test above. */
22155 return unify_template_argument_mismatch (explain_p, parm, arg);
22156
22157 case VAR_DECL:
22158 /* We might get a variable as a non-type template argument in parm if the
22159 corresponding parameter is type-dependent. Make any necessary
22160 adjustments based on whether arg is a reference. */
22161 if (CONSTANT_CLASS_P (arg))
22162 parm = fold_non_dependent_expr (parm, complain);
22163 else if (REFERENCE_REF_P (arg))
22164 {
22165 tree sub = TREE_OPERAND (arg, 0);
22166 STRIP_NOPS (sub);
22167 if (TREE_CODE (sub) == ADDR_EXPR)
22168 arg = TREE_OPERAND (sub, 0);
22169 }
22170 /* Now use the normal expression code to check whether they match. */
22171 goto expr;
22172
22173 case TYPE_ARGUMENT_PACK:
22174 case NONTYPE_ARGUMENT_PACK:
22175 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
22176 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
22177
22178 case TYPEOF_TYPE:
22179 case DECLTYPE_TYPE:
22180 case UNDERLYING_TYPE:
22181 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22182 or UNDERLYING_TYPE nodes. */
22183 return unify_success (explain_p);
22184
22185 case ERROR_MARK:
22186 /* Unification fails if we hit an error node. */
22187 return unify_invalid (explain_p);
22188
22189 case INDIRECT_REF:
22190 if (REFERENCE_REF_P (parm))
22191 {
22192 bool pexp = PACK_EXPANSION_P (arg);
22193 if (pexp)
22194 arg = PACK_EXPANSION_PATTERN (arg);
22195 if (REFERENCE_REF_P (arg))
22196 arg = TREE_OPERAND (arg, 0);
22197 if (pexp)
22198 arg = make_pack_expansion (arg, complain);
22199 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
22200 strict, explain_p);
22201 }
22202 /* FALLTHRU */
22203
22204 default:
22205 /* An unresolved overload is a nondeduced context. */
22206 if (is_overloaded_fn (parm) || type_unknown_p (parm))
22207 return unify_success (explain_p);
22208 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
22209 expr:
22210 /* We must be looking at an expression. This can happen with
22211 something like:
22212
22213 template <int I>
22214 void foo(S<I>, S<I + 2>);
22215
22216 This is a "nondeduced context":
22217
22218 [deduct.type]
22219
22220 The nondeduced contexts are:
22221
22222 --A type that is a template-id in which one or more of
22223 the template-arguments is an expression that references
22224 a template-parameter.
22225
22226 In these cases, we assume deduction succeeded, but don't
22227 actually infer any unifications. */
22228
22229 if (!uses_template_parms (parm)
22230 && !template_args_equal (parm, arg))
22231 return unify_expression_unequal (explain_p, parm, arg);
22232 else
22233 return unify_success (explain_p);
22234 }
22235 }
22236 #undef RECUR_AND_CHECK_FAILURE
22237 \f
22238 /* Note that DECL can be defined in this translation unit, if
22239 required. */
22240
22241 static void
22242 mark_definable (tree decl)
22243 {
22244 tree clone;
22245 DECL_NOT_REALLY_EXTERN (decl) = 1;
22246 FOR_EACH_CLONE (clone, decl)
22247 DECL_NOT_REALLY_EXTERN (clone) = 1;
22248 }
22249
22250 /* Called if RESULT is explicitly instantiated, or is a member of an
22251 explicitly instantiated class. */
22252
22253 void
22254 mark_decl_instantiated (tree result, int extern_p)
22255 {
22256 SET_DECL_EXPLICIT_INSTANTIATION (result);
22257
22258 /* If this entity has already been written out, it's too late to
22259 make any modifications. */
22260 if (TREE_ASM_WRITTEN (result))
22261 return;
22262
22263 /* For anonymous namespace we don't need to do anything. */
22264 if (decl_anon_ns_mem_p (result))
22265 {
22266 gcc_assert (!TREE_PUBLIC (result));
22267 return;
22268 }
22269
22270 if (TREE_CODE (result) != FUNCTION_DECL)
22271 /* The TREE_PUBLIC flag for function declarations will have been
22272 set correctly by tsubst. */
22273 TREE_PUBLIC (result) = 1;
22274
22275 /* This might have been set by an earlier implicit instantiation. */
22276 DECL_COMDAT (result) = 0;
22277
22278 if (extern_p)
22279 DECL_NOT_REALLY_EXTERN (result) = 0;
22280 else
22281 {
22282 mark_definable (result);
22283 mark_needed (result);
22284 /* Always make artificials weak. */
22285 if (DECL_ARTIFICIAL (result) && flag_weak)
22286 comdat_linkage (result);
22287 /* For WIN32 we also want to put explicit instantiations in
22288 linkonce sections. */
22289 else if (TREE_PUBLIC (result))
22290 maybe_make_one_only (result);
22291 if (TREE_CODE (result) == FUNCTION_DECL
22292 && DECL_TEMPLATE_INSTANTIATED (result))
22293 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22294 since start_preparsed_function wouldn't have if we had an earlier
22295 extern explicit instantiation. */
22296 DECL_EXTERNAL (result) = 0;
22297 }
22298
22299 /* If EXTERN_P, then this function will not be emitted -- unless
22300 followed by an explicit instantiation, at which point its linkage
22301 will be adjusted. If !EXTERN_P, then this function will be
22302 emitted here. In neither circumstance do we want
22303 import_export_decl to adjust the linkage. */
22304 DECL_INTERFACE_KNOWN (result) = 1;
22305 }
22306
22307 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22308 important template arguments. If any are missing, we check whether
22309 they're important by using error_mark_node for substituting into any
22310 args that were used for partial ordering (the ones between ARGS and END)
22311 and seeing if it bubbles up. */
22312
22313 static bool
22314 check_undeduced_parms (tree targs, tree args, tree end)
22315 {
22316 bool found = false;
22317 int i;
22318 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
22319 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
22320 {
22321 found = true;
22322 TREE_VEC_ELT (targs, i) = error_mark_node;
22323 }
22324 if (found)
22325 {
22326 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
22327 if (substed == error_mark_node)
22328 return true;
22329 }
22330 return false;
22331 }
22332
22333 /* Given two function templates PAT1 and PAT2, return:
22334
22335 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22336 -1 if PAT2 is more specialized than PAT1.
22337 0 if neither is more specialized.
22338
22339 LEN indicates the number of parameters we should consider
22340 (defaulted parameters should not be considered).
22341
22342 The 1998 std underspecified function template partial ordering, and
22343 DR214 addresses the issue. We take pairs of arguments, one from
22344 each of the templates, and deduce them against each other. One of
22345 the templates will be more specialized if all the *other*
22346 template's arguments deduce against its arguments and at least one
22347 of its arguments *does* *not* deduce against the other template's
22348 corresponding argument. Deduction is done as for class templates.
22349 The arguments used in deduction have reference and top level cv
22350 qualifiers removed. Iff both arguments were originally reference
22351 types *and* deduction succeeds in both directions, an lvalue reference
22352 wins against an rvalue reference and otherwise the template
22353 with the more cv-qualified argument wins for that pairing (if
22354 neither is more cv-qualified, they both are equal). Unlike regular
22355 deduction, after all the arguments have been deduced in this way,
22356 we do *not* verify the deduced template argument values can be
22357 substituted into non-deduced contexts.
22358
22359 The logic can be a bit confusing here, because we look at deduce1 and
22360 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22361 can find template arguments for pat1 to make arg1 look like arg2, that
22362 means that arg2 is at least as specialized as arg1. */
22363
22364 int
22365 more_specialized_fn (tree pat1, tree pat2, int len)
22366 {
22367 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
22368 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
22369 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
22370 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
22371 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
22372 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
22373 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
22374 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
22375 tree origs1, origs2;
22376 bool lose1 = false;
22377 bool lose2 = false;
22378
22379 /* Remove the this parameter from non-static member functions. If
22380 one is a non-static member function and the other is not a static
22381 member function, remove the first parameter from that function
22382 also. This situation occurs for operator functions where we
22383 locate both a member function (with this pointer) and non-member
22384 operator (with explicit first operand). */
22385 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
22386 {
22387 len--; /* LEN is the number of significant arguments for DECL1 */
22388 args1 = TREE_CHAIN (args1);
22389 if (!DECL_STATIC_FUNCTION_P (decl2))
22390 args2 = TREE_CHAIN (args2);
22391 }
22392 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
22393 {
22394 args2 = TREE_CHAIN (args2);
22395 if (!DECL_STATIC_FUNCTION_P (decl1))
22396 {
22397 len--;
22398 args1 = TREE_CHAIN (args1);
22399 }
22400 }
22401
22402 /* If only one is a conversion operator, they are unordered. */
22403 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
22404 return 0;
22405
22406 /* Consider the return type for a conversion function */
22407 if (DECL_CONV_FN_P (decl1))
22408 {
22409 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
22410 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
22411 len++;
22412 }
22413
22414 processing_template_decl++;
22415
22416 origs1 = args1;
22417 origs2 = args2;
22418
22419 while (len--
22420 /* Stop when an ellipsis is seen. */
22421 && args1 != NULL_TREE && args2 != NULL_TREE)
22422 {
22423 tree arg1 = TREE_VALUE (args1);
22424 tree arg2 = TREE_VALUE (args2);
22425 int deduce1, deduce2;
22426 int quals1 = -1;
22427 int quals2 = -1;
22428 int ref1 = 0;
22429 int ref2 = 0;
22430
22431 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22432 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22433 {
22434 /* When both arguments are pack expansions, we need only
22435 unify the patterns themselves. */
22436 arg1 = PACK_EXPANSION_PATTERN (arg1);
22437 arg2 = PACK_EXPANSION_PATTERN (arg2);
22438
22439 /* This is the last comparison we need to do. */
22440 len = 0;
22441 }
22442
22443 /* DR 1847: If a particular P contains no template-parameters that
22444 participate in template argument deduction, that P is not used to
22445 determine the ordering. */
22446 if (!uses_deducible_template_parms (arg1)
22447 && !uses_deducible_template_parms (arg2))
22448 goto next;
22449
22450 if (TYPE_REF_P (arg1))
22451 {
22452 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
22453 arg1 = TREE_TYPE (arg1);
22454 quals1 = cp_type_quals (arg1);
22455 }
22456
22457 if (TYPE_REF_P (arg2))
22458 {
22459 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
22460 arg2 = TREE_TYPE (arg2);
22461 quals2 = cp_type_quals (arg2);
22462 }
22463
22464 arg1 = TYPE_MAIN_VARIANT (arg1);
22465 arg2 = TYPE_MAIN_VARIANT (arg2);
22466
22467 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
22468 {
22469 int i, len2 = remaining_arguments (args2);
22470 tree parmvec = make_tree_vec (1);
22471 tree argvec = make_tree_vec (len2);
22472 tree ta = args2;
22473
22474 /* Setup the parameter vector, which contains only ARG1. */
22475 TREE_VEC_ELT (parmvec, 0) = arg1;
22476
22477 /* Setup the argument vector, which contains the remaining
22478 arguments. */
22479 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
22480 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
22481
22482 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
22483 argvec, DEDUCE_EXACT,
22484 /*subr=*/true, /*explain_p=*/false)
22485 == 0);
22486
22487 /* We cannot deduce in the other direction, because ARG1 is
22488 a pack expansion but ARG2 is not. */
22489 deduce2 = 0;
22490 }
22491 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22492 {
22493 int i, len1 = remaining_arguments (args1);
22494 tree parmvec = make_tree_vec (1);
22495 tree argvec = make_tree_vec (len1);
22496 tree ta = args1;
22497
22498 /* Setup the parameter vector, which contains only ARG1. */
22499 TREE_VEC_ELT (parmvec, 0) = arg2;
22500
22501 /* Setup the argument vector, which contains the remaining
22502 arguments. */
22503 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
22504 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
22505
22506 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
22507 argvec, DEDUCE_EXACT,
22508 /*subr=*/true, /*explain_p=*/false)
22509 == 0);
22510
22511 /* We cannot deduce in the other direction, because ARG2 is
22512 a pack expansion but ARG1 is not.*/
22513 deduce1 = 0;
22514 }
22515
22516 else
22517 {
22518 /* The normal case, where neither argument is a pack
22519 expansion. */
22520 deduce1 = (unify (tparms1, targs1, arg1, arg2,
22521 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22522 == 0);
22523 deduce2 = (unify (tparms2, targs2, arg2, arg1,
22524 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22525 == 0);
22526 }
22527
22528 /* If we couldn't deduce arguments for tparms1 to make arg1 match
22529 arg2, then arg2 is not as specialized as arg1. */
22530 if (!deduce1)
22531 lose2 = true;
22532 if (!deduce2)
22533 lose1 = true;
22534
22535 /* "If, for a given type, deduction succeeds in both directions
22536 (i.e., the types are identical after the transformations above)
22537 and both P and A were reference types (before being replaced with
22538 the type referred to above):
22539 - if the type from the argument template was an lvalue reference and
22540 the type from the parameter template was not, the argument type is
22541 considered to be more specialized than the other; otherwise,
22542 - if the type from the argument template is more cv-qualified
22543 than the type from the parameter template (as described above),
22544 the argument type is considered to be more specialized than the other;
22545 otherwise,
22546 - neither type is more specialized than the other." */
22547
22548 if (deduce1 && deduce2)
22549 {
22550 if (ref1 && ref2 && ref1 != ref2)
22551 {
22552 if (ref1 > ref2)
22553 lose1 = true;
22554 else
22555 lose2 = true;
22556 }
22557 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
22558 {
22559 if ((quals1 & quals2) == quals2)
22560 lose2 = true;
22561 if ((quals1 & quals2) == quals1)
22562 lose1 = true;
22563 }
22564 }
22565
22566 if (lose1 && lose2)
22567 /* We've failed to deduce something in either direction.
22568 These must be unordered. */
22569 break;
22570
22571 next:
22572
22573 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22574 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22575 /* We have already processed all of the arguments in our
22576 handing of the pack expansion type. */
22577 len = 0;
22578
22579 args1 = TREE_CHAIN (args1);
22580 args2 = TREE_CHAIN (args2);
22581 }
22582
22583 /* "In most cases, all template parameters must have values in order for
22584 deduction to succeed, but for partial ordering purposes a template
22585 parameter may remain without a value provided it is not used in the
22586 types being used for partial ordering."
22587
22588 Thus, if we are missing any of the targs1 we need to substitute into
22589 origs1, then pat2 is not as specialized as pat1. This can happen when
22590 there is a nondeduced context. */
22591 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
22592 lose2 = true;
22593 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
22594 lose1 = true;
22595
22596 processing_template_decl--;
22597
22598 /* If both deductions succeed, the partial ordering selects the more
22599 constrained template. */
22600 if (!lose1 && !lose2)
22601 {
22602 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
22603 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
22604 lose1 = !subsumes_constraints (c1, c2);
22605 lose2 = !subsumes_constraints (c2, c1);
22606 }
22607
22608 /* All things being equal, if the next argument is a pack expansion
22609 for one function but not for the other, prefer the
22610 non-variadic function. FIXME this is bogus; see c++/41958. */
22611 if (lose1 == lose2
22612 && args1 && TREE_VALUE (args1)
22613 && args2 && TREE_VALUE (args2))
22614 {
22615 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
22616 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
22617 }
22618
22619 if (lose1 == lose2)
22620 return 0;
22621 else if (!lose1)
22622 return 1;
22623 else
22624 return -1;
22625 }
22626
22627 /* Determine which of two partial specializations of TMPL is more
22628 specialized.
22629
22630 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22631 to the first partial specialization. The TREE_PURPOSE is the
22632 innermost set of template parameters for the partial
22633 specialization. PAT2 is similar, but for the second template.
22634
22635 Return 1 if the first partial specialization is more specialized;
22636 -1 if the second is more specialized; 0 if neither is more
22637 specialized.
22638
22639 See [temp.class.order] for information about determining which of
22640 two templates is more specialized. */
22641
22642 static int
22643 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
22644 {
22645 tree targs;
22646 int winner = 0;
22647 bool any_deductions = false;
22648
22649 tree tmpl1 = TREE_VALUE (pat1);
22650 tree tmpl2 = TREE_VALUE (pat2);
22651 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
22652 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
22653
22654 /* Just like what happens for functions, if we are ordering between
22655 different template specializations, we may encounter dependent
22656 types in the arguments, and we need our dependency check functions
22657 to behave correctly. */
22658 ++processing_template_decl;
22659 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
22660 if (targs)
22661 {
22662 --winner;
22663 any_deductions = true;
22664 }
22665
22666 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
22667 if (targs)
22668 {
22669 ++winner;
22670 any_deductions = true;
22671 }
22672 --processing_template_decl;
22673
22674 /* If both deductions succeed, the partial ordering selects the more
22675 constrained template. */
22676 if (!winner && any_deductions)
22677 return more_constrained (tmpl1, tmpl2);
22678
22679 /* In the case of a tie where at least one of the templates
22680 has a parameter pack at the end, the template with the most
22681 non-packed parameters wins. */
22682 if (winner == 0
22683 && any_deductions
22684 && (template_args_variadic_p (TREE_PURPOSE (pat1))
22685 || template_args_variadic_p (TREE_PURPOSE (pat2))))
22686 {
22687 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
22688 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
22689 int len1 = TREE_VEC_LENGTH (args1);
22690 int len2 = TREE_VEC_LENGTH (args2);
22691
22692 /* We don't count the pack expansion at the end. */
22693 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
22694 --len1;
22695 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
22696 --len2;
22697
22698 if (len1 > len2)
22699 return 1;
22700 else if (len1 < len2)
22701 return -1;
22702 }
22703
22704 return winner;
22705 }
22706
22707 /* Return the template arguments that will produce the function signature
22708 DECL from the function template FN, with the explicit template
22709 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22710 also match. Return NULL_TREE if no satisfactory arguments could be
22711 found. */
22712
22713 static tree
22714 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
22715 {
22716 int ntparms = DECL_NTPARMS (fn);
22717 tree targs = make_tree_vec (ntparms);
22718 tree decl_type = TREE_TYPE (decl);
22719 tree decl_arg_types;
22720 tree *args;
22721 unsigned int nargs, ix;
22722 tree arg;
22723
22724 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
22725
22726 /* Never do unification on the 'this' parameter. */
22727 decl_arg_types = skip_artificial_parms_for (decl,
22728 TYPE_ARG_TYPES (decl_type));
22729
22730 nargs = list_length (decl_arg_types);
22731 args = XALLOCAVEC (tree, nargs);
22732 for (arg = decl_arg_types, ix = 0;
22733 arg != NULL_TREE && arg != void_list_node;
22734 arg = TREE_CHAIN (arg), ++ix)
22735 args[ix] = TREE_VALUE (arg);
22736
22737 if (fn_type_unification (fn, explicit_args, targs,
22738 args, ix,
22739 (check_rettype || DECL_CONV_FN_P (fn)
22740 ? TREE_TYPE (decl_type) : NULL_TREE),
22741 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
22742 /*explain_p=*/false,
22743 /*decltype*/false)
22744 == error_mark_node)
22745 return NULL_TREE;
22746
22747 return targs;
22748 }
22749
22750 /* Return the innermost template arguments that, when applied to a partial
22751 specialization SPEC_TMPL of TMPL, yield the ARGS.
22752
22753 For example, suppose we have:
22754
22755 template <class T, class U> struct S {};
22756 template <class T> struct S<T*, int> {};
22757
22758 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22759 partial specialization and the ARGS will be {double*, int}. The resulting
22760 vector will be {double}, indicating that `T' is bound to `double'. */
22761
22762 static tree
22763 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
22764 {
22765 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
22766 tree spec_args
22767 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
22768 int i, ntparms = TREE_VEC_LENGTH (tparms);
22769 tree deduced_args;
22770 tree innermost_deduced_args;
22771
22772 innermost_deduced_args = make_tree_vec (ntparms);
22773 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22774 {
22775 deduced_args = copy_node (args);
22776 SET_TMPL_ARGS_LEVEL (deduced_args,
22777 TMPL_ARGS_DEPTH (deduced_args),
22778 innermost_deduced_args);
22779 }
22780 else
22781 deduced_args = innermost_deduced_args;
22782
22783 bool tried_array_deduction = (cxx_dialect < cxx17);
22784 again:
22785 if (unify (tparms, deduced_args,
22786 INNERMOST_TEMPLATE_ARGS (spec_args),
22787 INNERMOST_TEMPLATE_ARGS (args),
22788 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22789 return NULL_TREE;
22790
22791 for (i = 0; i < ntparms; ++i)
22792 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22793 {
22794 if (!tried_array_deduction)
22795 {
22796 try_array_deduction (tparms, innermost_deduced_args,
22797 INNERMOST_TEMPLATE_ARGS (spec_args));
22798 tried_array_deduction = true;
22799 if (TREE_VEC_ELT (innermost_deduced_args, i))
22800 goto again;
22801 }
22802 return NULL_TREE;
22803 }
22804
22805 if (!push_tinst_level (spec_tmpl, deduced_args))
22806 {
22807 excessive_deduction_depth = true;
22808 return NULL_TREE;
22809 }
22810
22811 /* Verify that nondeduced template arguments agree with the type
22812 obtained from argument deduction.
22813
22814 For example:
22815
22816 struct A { typedef int X; };
22817 template <class T, class U> struct C {};
22818 template <class T> struct C<T, typename T::X> {};
22819
22820 Then with the instantiation `C<A, int>', we can deduce that
22821 `T' is `A' but unify () does not check whether `typename T::X'
22822 is `int'. */
22823 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22824
22825 if (spec_args != error_mark_node)
22826 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22827 INNERMOST_TEMPLATE_ARGS (spec_args),
22828 tmpl, tf_none, false, false);
22829
22830 pop_tinst_level ();
22831
22832 if (spec_args == error_mark_node
22833 /* We only need to check the innermost arguments; the other
22834 arguments will always agree. */
22835 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22836 INNERMOST_TEMPLATE_ARGS (args)))
22837 return NULL_TREE;
22838
22839 /* Now that we have bindings for all of the template arguments,
22840 ensure that the arguments deduced for the template template
22841 parameters have compatible template parameter lists. See the use
22842 of template_template_parm_bindings_ok_p in fn_type_unification
22843 for more information. */
22844 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22845 return NULL_TREE;
22846
22847 return deduced_args;
22848 }
22849
22850 // Compare two function templates T1 and T2 by deducing bindings
22851 // from one against the other. If both deductions succeed, compare
22852 // constraints to see which is more constrained.
22853 static int
22854 more_specialized_inst (tree t1, tree t2)
22855 {
22856 int fate = 0;
22857 int count = 0;
22858
22859 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22860 {
22861 --fate;
22862 ++count;
22863 }
22864
22865 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22866 {
22867 ++fate;
22868 ++count;
22869 }
22870
22871 // If both deductions succeed, then one may be more constrained.
22872 if (count == 2 && fate == 0)
22873 fate = more_constrained (t1, t2);
22874
22875 return fate;
22876 }
22877
22878 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22879 Return the TREE_LIST node with the most specialized template, if
22880 any. If there is no most specialized template, the error_mark_node
22881 is returned.
22882
22883 Note that this function does not look at, or modify, the
22884 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22885 returned is one of the elements of INSTANTIATIONS, callers may
22886 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22887 and retrieve it from the value returned. */
22888
22889 tree
22890 most_specialized_instantiation (tree templates)
22891 {
22892 tree fn, champ;
22893
22894 ++processing_template_decl;
22895
22896 champ = templates;
22897 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22898 {
22899 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22900 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22901 if (fate == -1)
22902 champ = fn;
22903 else if (!fate)
22904 {
22905 /* Equally specialized, move to next function. If there
22906 is no next function, nothing's most specialized. */
22907 fn = TREE_CHAIN (fn);
22908 champ = fn;
22909 if (!fn)
22910 break;
22911 }
22912 }
22913
22914 if (champ)
22915 /* Now verify that champ is better than everything earlier in the
22916 instantiation list. */
22917 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22918 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22919 {
22920 champ = NULL_TREE;
22921 break;
22922 }
22923 }
22924
22925 processing_template_decl--;
22926
22927 if (!champ)
22928 return error_mark_node;
22929
22930 return champ;
22931 }
22932
22933 /* If DECL is a specialization of some template, return the most
22934 general such template. Otherwise, returns NULL_TREE.
22935
22936 For example, given:
22937
22938 template <class T> struct S { template <class U> void f(U); };
22939
22940 if TMPL is `template <class U> void S<int>::f(U)' this will return
22941 the full template. This function will not trace past partial
22942 specializations, however. For example, given in addition:
22943
22944 template <class T> struct S<T*> { template <class U> void f(U); };
22945
22946 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22947 `template <class T> template <class U> S<T*>::f(U)'. */
22948
22949 tree
22950 most_general_template (tree decl)
22951 {
22952 if (TREE_CODE (decl) != TEMPLATE_DECL)
22953 {
22954 if (tree tinfo = get_template_info (decl))
22955 decl = TI_TEMPLATE (tinfo);
22956 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22957 template friend, or a FIELD_DECL for a capture pack. */
22958 if (TREE_CODE (decl) != TEMPLATE_DECL)
22959 return NULL_TREE;
22960 }
22961
22962 /* Look for more and more general templates. */
22963 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22964 {
22965 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22966 (See cp-tree.h for details.) */
22967 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22968 break;
22969
22970 if (CLASS_TYPE_P (TREE_TYPE (decl))
22971 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22972 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22973 break;
22974
22975 /* Stop if we run into an explicitly specialized class template. */
22976 if (!DECL_NAMESPACE_SCOPE_P (decl)
22977 && DECL_CONTEXT (decl)
22978 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22979 break;
22980
22981 decl = DECL_TI_TEMPLATE (decl);
22982 }
22983
22984 return decl;
22985 }
22986
22987 /* Return the most specialized of the template partial specializations
22988 which can produce TARGET, a specialization of some class or variable
22989 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22990 a TEMPLATE_DECL node corresponding to the partial specialization, while
22991 the TREE_PURPOSE is the set of template arguments that must be
22992 substituted into the template pattern in order to generate TARGET.
22993
22994 If the choice of partial specialization is ambiguous, a diagnostic
22995 is issued, and the error_mark_node is returned. If there are no
22996 partial specializations matching TARGET, then NULL_TREE is
22997 returned, indicating that the primary template should be used. */
22998
22999 static tree
23000 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
23001 {
23002 tree list = NULL_TREE;
23003 tree t;
23004 tree champ;
23005 int fate;
23006 bool ambiguous_p;
23007 tree outer_args = NULL_TREE;
23008 tree tmpl, args;
23009
23010 if (TYPE_P (target))
23011 {
23012 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
23013 tmpl = TI_TEMPLATE (tinfo);
23014 args = TI_ARGS (tinfo);
23015 }
23016 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
23017 {
23018 tmpl = TREE_OPERAND (target, 0);
23019 args = TREE_OPERAND (target, 1);
23020 }
23021 else if (VAR_P (target))
23022 {
23023 tree tinfo = DECL_TEMPLATE_INFO (target);
23024 tmpl = TI_TEMPLATE (tinfo);
23025 args = TI_ARGS (tinfo);
23026 }
23027 else
23028 gcc_unreachable ();
23029
23030 tree main_tmpl = most_general_template (tmpl);
23031
23032 /* For determining which partial specialization to use, only the
23033 innermost args are interesting. */
23034 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23035 {
23036 outer_args = strip_innermost_template_args (args, 1);
23037 args = INNERMOST_TEMPLATE_ARGS (args);
23038 }
23039
23040 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
23041 {
23042 tree spec_args;
23043 tree spec_tmpl = TREE_VALUE (t);
23044
23045 if (outer_args)
23046 {
23047 /* Substitute in the template args from the enclosing class. */
23048 ++processing_template_decl;
23049 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
23050 --processing_template_decl;
23051 }
23052
23053 if (spec_tmpl == error_mark_node)
23054 return error_mark_node;
23055
23056 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
23057 if (spec_args)
23058 {
23059 if (outer_args)
23060 spec_args = add_to_template_args (outer_args, spec_args);
23061
23062 /* Keep the candidate only if the constraints are satisfied,
23063 or if we're not compiling with concepts. */
23064 if (!flag_concepts
23065 || constraints_satisfied_p (spec_tmpl, spec_args))
23066 {
23067 list = tree_cons (spec_args, TREE_VALUE (t), list);
23068 TREE_TYPE (list) = TREE_TYPE (t);
23069 }
23070 }
23071 }
23072
23073 if (! list)
23074 return NULL_TREE;
23075
23076 ambiguous_p = false;
23077 t = list;
23078 champ = t;
23079 t = TREE_CHAIN (t);
23080 for (; t; t = TREE_CHAIN (t))
23081 {
23082 fate = more_specialized_partial_spec (tmpl, champ, t);
23083 if (fate == 1)
23084 ;
23085 else
23086 {
23087 if (fate == 0)
23088 {
23089 t = TREE_CHAIN (t);
23090 if (! t)
23091 {
23092 ambiguous_p = true;
23093 break;
23094 }
23095 }
23096 champ = t;
23097 }
23098 }
23099
23100 if (!ambiguous_p)
23101 for (t = list; t && t != champ; t = TREE_CHAIN (t))
23102 {
23103 fate = more_specialized_partial_spec (tmpl, champ, t);
23104 if (fate != 1)
23105 {
23106 ambiguous_p = true;
23107 break;
23108 }
23109 }
23110
23111 if (ambiguous_p)
23112 {
23113 const char *str;
23114 char *spaces = NULL;
23115 if (!(complain & tf_error))
23116 return error_mark_node;
23117 if (TYPE_P (target))
23118 error ("ambiguous template instantiation for %q#T", target);
23119 else
23120 error ("ambiguous template instantiation for %q#D", target);
23121 str = ngettext ("candidate is:", "candidates are:", list_length (list));
23122 for (t = list; t; t = TREE_CHAIN (t))
23123 {
23124 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
23125 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
23126 "%s %#qS", spaces ? spaces : str, subst);
23127 spaces = spaces ? spaces : get_spaces (str);
23128 }
23129 free (spaces);
23130 return error_mark_node;
23131 }
23132
23133 return champ;
23134 }
23135
23136 /* Explicitly instantiate DECL. */
23137
23138 void
23139 do_decl_instantiation (tree decl, tree storage)
23140 {
23141 tree result = NULL_TREE;
23142 int extern_p = 0;
23143
23144 if (!decl || decl == error_mark_node)
23145 /* An error occurred, for which grokdeclarator has already issued
23146 an appropriate message. */
23147 return;
23148 else if (! DECL_LANG_SPECIFIC (decl))
23149 {
23150 error ("explicit instantiation of non-template %q#D", decl);
23151 return;
23152 }
23153 else if (DECL_DECLARED_CONCEPT_P (decl))
23154 {
23155 if (VAR_P (decl))
23156 error ("explicit instantiation of variable concept %q#D", decl);
23157 else
23158 error ("explicit instantiation of function concept %q#D", decl);
23159 return;
23160 }
23161
23162 bool var_templ = (DECL_TEMPLATE_INFO (decl)
23163 && variable_template_p (DECL_TI_TEMPLATE (decl)));
23164
23165 if (VAR_P (decl) && !var_templ)
23166 {
23167 /* There is an asymmetry here in the way VAR_DECLs and
23168 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23169 the latter, the DECL we get back will be marked as a
23170 template instantiation, and the appropriate
23171 DECL_TEMPLATE_INFO will be set up. This does not happen for
23172 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23173 should handle VAR_DECLs as it currently handles
23174 FUNCTION_DECLs. */
23175 if (!DECL_CLASS_SCOPE_P (decl))
23176 {
23177 error ("%qD is not a static data member of a class template", decl);
23178 return;
23179 }
23180 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
23181 if (!result || !VAR_P (result))
23182 {
23183 error ("no matching template for %qD found", decl);
23184 return;
23185 }
23186 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
23187 {
23188 error ("type %qT for explicit instantiation %qD does not match "
23189 "declared type %qT", TREE_TYPE (result), decl,
23190 TREE_TYPE (decl));
23191 return;
23192 }
23193 }
23194 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
23195 {
23196 error ("explicit instantiation of %q#D", decl);
23197 return;
23198 }
23199 else
23200 result = decl;
23201
23202 /* Check for various error cases. Note that if the explicit
23203 instantiation is valid the RESULT will currently be marked as an
23204 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23205 until we get here. */
23206
23207 if (DECL_TEMPLATE_SPECIALIZATION (result))
23208 {
23209 /* DR 259 [temp.spec].
23210
23211 Both an explicit instantiation and a declaration of an explicit
23212 specialization shall not appear in a program unless the explicit
23213 instantiation follows a declaration of the explicit specialization.
23214
23215 For a given set of template parameters, if an explicit
23216 instantiation of a template appears after a declaration of an
23217 explicit specialization for that template, the explicit
23218 instantiation has no effect. */
23219 return;
23220 }
23221 else if (DECL_EXPLICIT_INSTANTIATION (result))
23222 {
23223 /* [temp.spec]
23224
23225 No program shall explicitly instantiate any template more
23226 than once.
23227
23228 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23229 the first instantiation was `extern' and the second is not,
23230 and EXTERN_P for the opposite case. */
23231 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
23232 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
23233 /* If an "extern" explicit instantiation follows an ordinary
23234 explicit instantiation, the template is instantiated. */
23235 if (extern_p)
23236 return;
23237 }
23238 else if (!DECL_IMPLICIT_INSTANTIATION (result))
23239 {
23240 error ("no matching template for %qD found", result);
23241 return;
23242 }
23243 else if (!DECL_TEMPLATE_INFO (result))
23244 {
23245 permerror (input_location, "explicit instantiation of non-template %q#D", result);
23246 return;
23247 }
23248
23249 if (storage == NULL_TREE)
23250 ;
23251 else if (storage == ridpointers[(int) RID_EXTERN])
23252 {
23253 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
23254 pedwarn (input_location, OPT_Wpedantic,
23255 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23256 "instantiations");
23257 extern_p = 1;
23258 }
23259 else
23260 error ("storage class %qD applied to template instantiation", storage);
23261
23262 check_explicit_instantiation_namespace (result);
23263 mark_decl_instantiated (result, extern_p);
23264 if (! extern_p)
23265 instantiate_decl (result, /*defer_ok=*/true,
23266 /*expl_inst_class_mem_p=*/false);
23267 }
23268
23269 static void
23270 mark_class_instantiated (tree t, int extern_p)
23271 {
23272 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
23273 SET_CLASSTYPE_INTERFACE_KNOWN (t);
23274 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
23275 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
23276 if (! extern_p)
23277 {
23278 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
23279 rest_of_type_compilation (t, 1);
23280 }
23281 }
23282
23283 /* Called from do_type_instantiation through binding_table_foreach to
23284 do recursive instantiation for the type bound in ENTRY. */
23285 static void
23286 bt_instantiate_type_proc (binding_entry entry, void *data)
23287 {
23288 tree storage = *(tree *) data;
23289
23290 if (MAYBE_CLASS_TYPE_P (entry->type)
23291 && CLASSTYPE_TEMPLATE_INFO (entry->type)
23292 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
23293 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
23294 }
23295
23296 /* Perform an explicit instantiation of template class T. STORAGE, if
23297 non-null, is the RID for extern, inline or static. COMPLAIN is
23298 nonzero if this is called from the parser, zero if called recursively,
23299 since the standard is unclear (as detailed below). */
23300
23301 void
23302 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
23303 {
23304 int extern_p = 0;
23305 int nomem_p = 0;
23306 int static_p = 0;
23307 int previous_instantiation_extern_p = 0;
23308
23309 if (TREE_CODE (t) == TYPE_DECL)
23310 t = TREE_TYPE (t);
23311
23312 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
23313 {
23314 tree tmpl =
23315 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
23316 if (tmpl)
23317 error ("explicit instantiation of non-class template %qD", tmpl);
23318 else
23319 error ("explicit instantiation of non-template type %qT", t);
23320 return;
23321 }
23322
23323 complete_type (t);
23324
23325 if (!COMPLETE_TYPE_P (t))
23326 {
23327 if (complain & tf_error)
23328 error ("explicit instantiation of %q#T before definition of template",
23329 t);
23330 return;
23331 }
23332
23333 if (storage != NULL_TREE)
23334 {
23335 if (!in_system_header_at (input_location))
23336 {
23337 if (storage == ridpointers[(int) RID_EXTERN])
23338 {
23339 if (cxx_dialect == cxx98)
23340 pedwarn (input_location, OPT_Wpedantic,
23341 "ISO C++ 1998 forbids the use of %<extern%> on "
23342 "explicit instantiations");
23343 }
23344 else
23345 pedwarn (input_location, OPT_Wpedantic,
23346 "ISO C++ forbids the use of %qE"
23347 " on explicit instantiations", storage);
23348 }
23349
23350 if (storage == ridpointers[(int) RID_INLINE])
23351 nomem_p = 1;
23352 else if (storage == ridpointers[(int) RID_EXTERN])
23353 extern_p = 1;
23354 else if (storage == ridpointers[(int) RID_STATIC])
23355 static_p = 1;
23356 else
23357 {
23358 error ("storage class %qD applied to template instantiation",
23359 storage);
23360 extern_p = 0;
23361 }
23362 }
23363
23364 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
23365 {
23366 /* DR 259 [temp.spec].
23367
23368 Both an explicit instantiation and a declaration of an explicit
23369 specialization shall not appear in a program unless the explicit
23370 instantiation follows a declaration of the explicit specialization.
23371
23372 For a given set of template parameters, if an explicit
23373 instantiation of a template appears after a declaration of an
23374 explicit specialization for that template, the explicit
23375 instantiation has no effect. */
23376 return;
23377 }
23378 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
23379 {
23380 /* [temp.spec]
23381
23382 No program shall explicitly instantiate any template more
23383 than once.
23384
23385 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23386 instantiation was `extern'. If EXTERN_P then the second is.
23387 These cases are OK. */
23388 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
23389
23390 if (!previous_instantiation_extern_p && !extern_p
23391 && (complain & tf_error))
23392 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
23393
23394 /* If we've already instantiated the template, just return now. */
23395 if (!CLASSTYPE_INTERFACE_ONLY (t))
23396 return;
23397 }
23398
23399 check_explicit_instantiation_namespace (TYPE_NAME (t));
23400 mark_class_instantiated (t, extern_p);
23401
23402 if (nomem_p)
23403 return;
23404
23405 /* In contrast to implicit instantiation, where only the
23406 declarations, and not the definitions, of members are
23407 instantiated, we have here:
23408
23409 [temp.explicit]
23410
23411 The explicit instantiation of a class template specialization
23412 implies the instantiation of all of its members not
23413 previously explicitly specialized in the translation unit
23414 containing the explicit instantiation.
23415
23416 Of course, we can't instantiate member template classes, since we
23417 don't have any arguments for them. Note that the standard is
23418 unclear on whether the instantiation of the members are
23419 *explicit* instantiations or not. However, the most natural
23420 interpretation is that it should be an explicit
23421 instantiation. */
23422 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
23423 if ((VAR_P (fld)
23424 || (TREE_CODE (fld) == FUNCTION_DECL
23425 && !static_p
23426 && user_provided_p (fld)))
23427 && DECL_TEMPLATE_INSTANTIATION (fld))
23428 {
23429 mark_decl_instantiated (fld, extern_p);
23430 if (! extern_p)
23431 instantiate_decl (fld, /*defer_ok=*/true,
23432 /*expl_inst_class_mem_p=*/true);
23433 }
23434
23435 if (CLASSTYPE_NESTED_UTDS (t))
23436 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
23437 bt_instantiate_type_proc, &storage);
23438 }
23439
23440 /* Given a function DECL, which is a specialization of TMPL, modify
23441 DECL to be a re-instantiation of TMPL with the same template
23442 arguments. TMPL should be the template into which tsubst'ing
23443 should occur for DECL, not the most general template.
23444
23445 One reason for doing this is a scenario like this:
23446
23447 template <class T>
23448 void f(const T&, int i);
23449
23450 void g() { f(3, 7); }
23451
23452 template <class T>
23453 void f(const T& t, const int i) { }
23454
23455 Note that when the template is first instantiated, with
23456 instantiate_template, the resulting DECL will have no name for the
23457 first parameter, and the wrong type for the second. So, when we go
23458 to instantiate the DECL, we regenerate it. */
23459
23460 static void
23461 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
23462 {
23463 /* The arguments used to instantiate DECL, from the most general
23464 template. */
23465 tree code_pattern;
23466
23467 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
23468
23469 /* Make sure that we can see identifiers, and compute access
23470 correctly. */
23471 push_access_scope (decl);
23472
23473 if (TREE_CODE (decl) == FUNCTION_DECL)
23474 {
23475 tree decl_parm;
23476 tree pattern_parm;
23477 tree specs;
23478 int args_depth;
23479 int parms_depth;
23480
23481 args_depth = TMPL_ARGS_DEPTH (args);
23482 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
23483 if (args_depth > parms_depth)
23484 args = get_innermost_template_args (args, parms_depth);
23485
23486 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
23487 args, tf_error, NULL_TREE,
23488 /*defer_ok*/false);
23489 if (specs && specs != error_mark_node)
23490 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
23491 specs);
23492
23493 /* Merge parameter declarations. */
23494 decl_parm = skip_artificial_parms_for (decl,
23495 DECL_ARGUMENTS (decl));
23496 pattern_parm
23497 = skip_artificial_parms_for (code_pattern,
23498 DECL_ARGUMENTS (code_pattern));
23499 while (decl_parm && !DECL_PACK_P (pattern_parm))
23500 {
23501 tree parm_type;
23502 tree attributes;
23503
23504 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23505 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
23506 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
23507 NULL_TREE);
23508 parm_type = type_decays_to (parm_type);
23509 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23510 TREE_TYPE (decl_parm) = parm_type;
23511 attributes = DECL_ATTRIBUTES (pattern_parm);
23512 if (DECL_ATTRIBUTES (decl_parm) != attributes)
23513 {
23514 DECL_ATTRIBUTES (decl_parm) = attributes;
23515 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
23516 }
23517 decl_parm = DECL_CHAIN (decl_parm);
23518 pattern_parm = DECL_CHAIN (pattern_parm);
23519 }
23520 /* Merge any parameters that match with the function parameter
23521 pack. */
23522 if (pattern_parm && DECL_PACK_P (pattern_parm))
23523 {
23524 int i, len;
23525 tree expanded_types;
23526 /* Expand the TYPE_PACK_EXPANSION that provides the types for
23527 the parameters in this function parameter pack. */
23528 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
23529 args, tf_error, NULL_TREE);
23530 len = TREE_VEC_LENGTH (expanded_types);
23531 for (i = 0; i < len; i++)
23532 {
23533 tree parm_type;
23534 tree attributes;
23535
23536 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23537 /* Rename the parameter to include the index. */
23538 DECL_NAME (decl_parm) =
23539 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
23540 parm_type = TREE_VEC_ELT (expanded_types, i);
23541 parm_type = type_decays_to (parm_type);
23542 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23543 TREE_TYPE (decl_parm) = parm_type;
23544 attributes = DECL_ATTRIBUTES (pattern_parm);
23545 if (DECL_ATTRIBUTES (decl_parm) != attributes)
23546 {
23547 DECL_ATTRIBUTES (decl_parm) = attributes;
23548 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
23549 }
23550 decl_parm = DECL_CHAIN (decl_parm);
23551 }
23552 }
23553 /* Merge additional specifiers from the CODE_PATTERN. */
23554 if (DECL_DECLARED_INLINE_P (code_pattern)
23555 && !DECL_DECLARED_INLINE_P (decl))
23556 DECL_DECLARED_INLINE_P (decl) = 1;
23557 }
23558 else if (VAR_P (decl))
23559 {
23560 start_lambda_scope (decl);
23561 DECL_INITIAL (decl) =
23562 tsubst_expr (DECL_INITIAL (code_pattern), args,
23563 tf_error, DECL_TI_TEMPLATE (decl),
23564 /*integral_constant_expression_p=*/false);
23565 finish_lambda_scope ();
23566 if (VAR_HAD_UNKNOWN_BOUND (decl))
23567 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
23568 tf_error, DECL_TI_TEMPLATE (decl));
23569 }
23570 else
23571 gcc_unreachable ();
23572
23573 pop_access_scope (decl);
23574 }
23575
23576 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
23577 substituted to get DECL. */
23578
23579 tree
23580 template_for_substitution (tree decl)
23581 {
23582 tree tmpl = DECL_TI_TEMPLATE (decl);
23583
23584 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
23585 for the instantiation. This is not always the most general
23586 template. Consider, for example:
23587
23588 template <class T>
23589 struct S { template <class U> void f();
23590 template <> void f<int>(); };
23591
23592 and an instantiation of S<double>::f<int>. We want TD to be the
23593 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23594 while (/* An instantiation cannot have a definition, so we need a
23595 more general template. */
23596 DECL_TEMPLATE_INSTANTIATION (tmpl)
23597 /* We must also deal with friend templates. Given:
23598
23599 template <class T> struct S {
23600 template <class U> friend void f() {};
23601 };
23602
23603 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23604 so far as the language is concerned, but that's still
23605 where we get the pattern for the instantiation from. On
23606 other hand, if the definition comes outside the class, say:
23607
23608 template <class T> struct S {
23609 template <class U> friend void f();
23610 };
23611 template <class U> friend void f() {}
23612
23613 we don't need to look any further. That's what the check for
23614 DECL_INITIAL is for. */
23615 || (TREE_CODE (decl) == FUNCTION_DECL
23616 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
23617 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
23618 {
23619 /* The present template, TD, should not be a definition. If it
23620 were a definition, we should be using it! Note that we
23621 cannot restructure the loop to just keep going until we find
23622 a template with a definition, since that might go too far if
23623 a specialization was declared, but not defined. */
23624
23625 /* Fetch the more general template. */
23626 tmpl = DECL_TI_TEMPLATE (tmpl);
23627 }
23628
23629 return tmpl;
23630 }
23631
23632 /* Returns true if we need to instantiate this template instance even if we
23633 know we aren't going to emit it. */
23634
23635 bool
23636 always_instantiate_p (tree decl)
23637 {
23638 /* We always instantiate inline functions so that we can inline them. An
23639 explicit instantiation declaration prohibits implicit instantiation of
23640 non-inline functions. With high levels of optimization, we would
23641 normally inline non-inline functions -- but we're not allowed to do
23642 that for "extern template" functions. Therefore, we check
23643 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23644 return ((TREE_CODE (decl) == FUNCTION_DECL
23645 && (DECL_DECLARED_INLINE_P (decl)
23646 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
23647 /* And we need to instantiate static data members so that
23648 their initializers are available in integral constant
23649 expressions. */
23650 || (VAR_P (decl)
23651 && decl_maybe_constant_var_p (decl)));
23652 }
23653
23654 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23655 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23656 error, true otherwise. */
23657
23658 bool
23659 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
23660 {
23661 tree fntype, spec, noex, clone;
23662
23663 /* Don't instantiate a noexcept-specification from template context. */
23664 if (processing_template_decl
23665 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
23666 return true;
23667
23668 if (DECL_CLONED_FUNCTION_P (fn))
23669 fn = DECL_CLONED_FUNCTION (fn);
23670 fntype = TREE_TYPE (fn);
23671 spec = TYPE_RAISES_EXCEPTIONS (fntype);
23672
23673 if (!spec || !TREE_PURPOSE (spec))
23674 return true;
23675
23676 noex = TREE_PURPOSE (spec);
23677
23678 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
23679 {
23680 static hash_set<tree>* fns = new hash_set<tree>;
23681 bool added = false;
23682 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
23683 spec = get_defaulted_eh_spec (fn, complain);
23684 else if (!(added = !fns->add (fn)))
23685 {
23686 /* If hash_set::add returns true, the element was already there. */
23687 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
23688 DECL_SOURCE_LOCATION (fn));
23689 error_at (loc,
23690 "exception specification of %qD depends on itself",
23691 fn);
23692 spec = noexcept_false_spec;
23693 }
23694 else if (push_tinst_level (fn))
23695 {
23696 push_access_scope (fn);
23697 push_deferring_access_checks (dk_no_deferred);
23698 input_location = DECL_SOURCE_LOCATION (fn);
23699 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
23700 DEFERRED_NOEXCEPT_ARGS (noex),
23701 tf_warning_or_error, fn,
23702 /*function_p=*/false,
23703 /*integral_constant_expression_p=*/true);
23704 spec = build_noexcept_spec (noex, tf_warning_or_error);
23705 pop_deferring_access_checks ();
23706 pop_access_scope (fn);
23707 pop_tinst_level ();
23708 if (spec == error_mark_node)
23709 spec = noexcept_false_spec;
23710 }
23711 else
23712 spec = noexcept_false_spec;
23713
23714 if (added)
23715 fns->remove (fn);
23716
23717 if (spec == error_mark_node)
23718 return false;
23719
23720 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
23721 }
23722
23723 FOR_EACH_CLONE (clone, fn)
23724 {
23725 if (TREE_TYPE (clone) == fntype)
23726 TREE_TYPE (clone) = TREE_TYPE (fn);
23727 else
23728 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
23729 }
23730
23731 return true;
23732 }
23733
23734 /* We're starting to process the function INST, an instantiation of PATTERN;
23735 add their parameters to local_specializations. */
23736
23737 static void
23738 register_parameter_specializations (tree pattern, tree inst)
23739 {
23740 tree tmpl_parm = DECL_ARGUMENTS (pattern);
23741 tree spec_parm = DECL_ARGUMENTS (inst);
23742 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
23743 {
23744 register_local_specialization (spec_parm, tmpl_parm);
23745 spec_parm = skip_artificial_parms_for (inst, spec_parm);
23746 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
23747 }
23748 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23749 {
23750 if (!DECL_PACK_P (tmpl_parm))
23751 {
23752 register_local_specialization (spec_parm, tmpl_parm);
23753 spec_parm = DECL_CHAIN (spec_parm);
23754 }
23755 else
23756 {
23757 /* Register the (value) argument pack as a specialization of
23758 TMPL_PARM, then move on. */
23759 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23760 register_local_specialization (argpack, tmpl_parm);
23761 }
23762 }
23763 gcc_assert (!spec_parm);
23764 }
23765
23766 /* Produce the definition of D, a _DECL generated from a template. If
23767 DEFER_OK is true, then we don't have to actually do the
23768 instantiation now; we just have to do it sometime. Normally it is
23769 an error if this is an explicit instantiation but D is undefined.
23770 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23771 instantiated class template. */
23772
23773 tree
23774 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
23775 {
23776 tree tmpl = DECL_TI_TEMPLATE (d);
23777 tree gen_args;
23778 tree args;
23779 tree td;
23780 tree code_pattern;
23781 tree spec;
23782 tree gen_tmpl;
23783 bool pattern_defined;
23784 location_t saved_loc = input_location;
23785 int saved_unevaluated_operand = cp_unevaluated_operand;
23786 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23787 bool external_p;
23788 bool deleted_p;
23789
23790 /* This function should only be used to instantiate templates for
23791 functions and static member variables. */
23792 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23793
23794 /* A concept is never instantiated. */
23795 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23796
23797 /* Variables are never deferred; if instantiation is required, they
23798 are instantiated right away. That allows for better code in the
23799 case that an expression refers to the value of the variable --
23800 if the variable has a constant value the referring expression can
23801 take advantage of that fact. */
23802 if (VAR_P (d))
23803 defer_ok = false;
23804
23805 /* Don't instantiate cloned functions. Instead, instantiate the
23806 functions they cloned. */
23807 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23808 d = DECL_CLONED_FUNCTION (d);
23809
23810 if (DECL_TEMPLATE_INSTANTIATED (d)
23811 || (TREE_CODE (d) == FUNCTION_DECL
23812 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23813 || DECL_TEMPLATE_SPECIALIZATION (d))
23814 /* D has already been instantiated or explicitly specialized, so
23815 there's nothing for us to do here.
23816
23817 It might seem reasonable to check whether or not D is an explicit
23818 instantiation, and, if so, stop here. But when an explicit
23819 instantiation is deferred until the end of the compilation,
23820 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23821 the instantiation. */
23822 return d;
23823
23824 /* Check to see whether we know that this template will be
23825 instantiated in some other file, as with "extern template"
23826 extension. */
23827 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23828
23829 /* In general, we do not instantiate such templates. */
23830 if (external_p && !always_instantiate_p (d))
23831 return d;
23832
23833 gen_tmpl = most_general_template (tmpl);
23834 gen_args = DECL_TI_ARGS (d);
23835
23836 if (tmpl != gen_tmpl)
23837 /* We should already have the extra args. */
23838 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23839 == TMPL_ARGS_DEPTH (gen_args));
23840 /* And what's in the hash table should match D. */
23841 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23842 || spec == NULL_TREE);
23843
23844 /* This needs to happen before any tsubsting. */
23845 if (! push_tinst_level (d))
23846 return d;
23847
23848 timevar_push (TV_TEMPLATE_INST);
23849
23850 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23851 for the instantiation. */
23852 td = template_for_substitution (d);
23853 args = gen_args;
23854
23855 if (VAR_P (d))
23856 {
23857 /* Look up an explicit specialization, if any. */
23858 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23859 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23860 if (elt && elt != error_mark_node)
23861 {
23862 td = TREE_VALUE (elt);
23863 args = TREE_PURPOSE (elt);
23864 }
23865 }
23866
23867 code_pattern = DECL_TEMPLATE_RESULT (td);
23868
23869 /* We should never be trying to instantiate a member of a class
23870 template or partial specialization. */
23871 gcc_assert (d != code_pattern);
23872
23873 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23874 || DECL_TEMPLATE_SPECIALIZATION (td))
23875 /* In the case of a friend template whose definition is provided
23876 outside the class, we may have too many arguments. Drop the
23877 ones we don't need. The same is true for specializations. */
23878 args = get_innermost_template_args
23879 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23880
23881 if (TREE_CODE (d) == FUNCTION_DECL)
23882 {
23883 deleted_p = DECL_DELETED_FN (code_pattern);
23884 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23885 && DECL_INITIAL (code_pattern) != error_mark_node)
23886 || DECL_DEFAULTED_FN (code_pattern)
23887 || deleted_p);
23888 }
23889 else
23890 {
23891 deleted_p = false;
23892 if (DECL_CLASS_SCOPE_P (code_pattern))
23893 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23894 || DECL_INLINE_VAR_P (code_pattern));
23895 else
23896 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23897 }
23898
23899 /* We may be in the middle of deferred access check. Disable it now. */
23900 push_deferring_access_checks (dk_no_deferred);
23901
23902 /* Unless an explicit instantiation directive has already determined
23903 the linkage of D, remember that a definition is available for
23904 this entity. */
23905 if (pattern_defined
23906 && !DECL_INTERFACE_KNOWN (d)
23907 && !DECL_NOT_REALLY_EXTERN (d))
23908 mark_definable (d);
23909
23910 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23911 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23912 input_location = DECL_SOURCE_LOCATION (d);
23913
23914 /* If D is a member of an explicitly instantiated class template,
23915 and no definition is available, treat it like an implicit
23916 instantiation. */
23917 if (!pattern_defined && expl_inst_class_mem_p
23918 && DECL_EXPLICIT_INSTANTIATION (d))
23919 {
23920 /* Leave linkage flags alone on instantiations with anonymous
23921 visibility. */
23922 if (TREE_PUBLIC (d))
23923 {
23924 DECL_NOT_REALLY_EXTERN (d) = 0;
23925 DECL_INTERFACE_KNOWN (d) = 0;
23926 }
23927 SET_DECL_IMPLICIT_INSTANTIATION (d);
23928 }
23929
23930 /* Defer all other templates, unless we have been explicitly
23931 forbidden from doing so. */
23932 if (/* If there is no definition, we cannot instantiate the
23933 template. */
23934 ! pattern_defined
23935 /* If it's OK to postpone instantiation, do so. */
23936 || defer_ok
23937 /* If this is a static data member that will be defined
23938 elsewhere, we don't want to instantiate the entire data
23939 member, but we do want to instantiate the initializer so that
23940 we can substitute that elsewhere. */
23941 || (external_p && VAR_P (d))
23942 /* Handle here a deleted function too, avoid generating
23943 its body (c++/61080). */
23944 || deleted_p)
23945 {
23946 /* The definition of the static data member is now required so
23947 we must substitute the initializer. */
23948 if (VAR_P (d)
23949 && !DECL_INITIAL (d)
23950 && DECL_INITIAL (code_pattern))
23951 {
23952 tree ns;
23953 tree init;
23954 bool const_init = false;
23955 bool enter_context = DECL_CLASS_SCOPE_P (d);
23956
23957 ns = decl_namespace_context (d);
23958 push_nested_namespace (ns);
23959 if (enter_context)
23960 push_nested_class (DECL_CONTEXT (d));
23961 init = tsubst_expr (DECL_INITIAL (code_pattern),
23962 args,
23963 tf_warning_or_error, NULL_TREE,
23964 /*integral_constant_expression_p=*/false);
23965 /* If instantiating the initializer involved instantiating this
23966 again, don't call cp_finish_decl twice. */
23967 if (!DECL_INITIAL (d))
23968 {
23969 /* Make sure the initializer is still constant, in case of
23970 circular dependency (template/instantiate6.C). */
23971 const_init
23972 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23973 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23974 /*asmspec_tree=*/NULL_TREE,
23975 LOOKUP_ONLYCONVERTING);
23976 }
23977 if (enter_context)
23978 pop_nested_class ();
23979 pop_nested_namespace (ns);
23980 }
23981
23982 /* We restore the source position here because it's used by
23983 add_pending_template. */
23984 input_location = saved_loc;
23985
23986 if (at_eof && !pattern_defined
23987 && DECL_EXPLICIT_INSTANTIATION (d)
23988 && DECL_NOT_REALLY_EXTERN (d))
23989 /* [temp.explicit]
23990
23991 The definition of a non-exported function template, a
23992 non-exported member function template, or a non-exported
23993 member function or static data member of a class template
23994 shall be present in every translation unit in which it is
23995 explicitly instantiated. */
23996 permerror (input_location, "explicit instantiation of %qD "
23997 "but no definition available", d);
23998
23999 /* If we're in unevaluated context, we just wanted to get the
24000 constant value; this isn't an odr use, so don't queue
24001 a full instantiation. */
24002 if (cp_unevaluated_operand != 0)
24003 goto out;
24004 /* ??? Historically, we have instantiated inline functions, even
24005 when marked as "extern template". */
24006 if (!(external_p && VAR_P (d)))
24007 add_pending_template (d);
24008 goto out;
24009 }
24010 /* Tell the repository that D is available in this translation unit
24011 -- and see if it is supposed to be instantiated here. */
24012 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
24013 {
24014 /* In a PCH file, despite the fact that the repository hasn't
24015 requested instantiation in the PCH it is still possible that
24016 an instantiation will be required in a file that includes the
24017 PCH. */
24018 if (pch_file)
24019 add_pending_template (d);
24020 /* Instantiate inline functions so that the inliner can do its
24021 job, even though we'll not be emitting a copy of this
24022 function. */
24023 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
24024 goto out;
24025 }
24026
24027 bool push_to_top, nested;
24028 tree fn_context;
24029 fn_context = decl_function_context (d);
24030 if (LAMBDA_FUNCTION_P (d))
24031 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24032 fn_context = NULL_TREE;
24033 nested = current_function_decl != NULL_TREE;
24034 push_to_top = !(nested && fn_context == current_function_decl);
24035
24036 vec<tree> omp_privatization_save;
24037 if (nested)
24038 save_omp_privatization_clauses (omp_privatization_save);
24039
24040 if (push_to_top)
24041 push_to_top_level ();
24042 else
24043 {
24044 gcc_assert (!processing_template_decl);
24045 push_function_context ();
24046 cp_unevaluated_operand = 0;
24047 c_inhibit_evaluation_warnings = 0;
24048 }
24049
24050 /* Mark D as instantiated so that recursive calls to
24051 instantiate_decl do not try to instantiate it again. */
24052 DECL_TEMPLATE_INSTANTIATED (d) = 1;
24053
24054 /* Regenerate the declaration in case the template has been modified
24055 by a subsequent redeclaration. */
24056 regenerate_decl_from_template (d, td, args);
24057
24058 /* We already set the file and line above. Reset them now in case
24059 they changed as a result of calling regenerate_decl_from_template. */
24060 input_location = DECL_SOURCE_LOCATION (d);
24061
24062 if (VAR_P (d))
24063 {
24064 tree init;
24065 bool const_init = false;
24066
24067 /* Clear out DECL_RTL; whatever was there before may not be right
24068 since we've reset the type of the declaration. */
24069 SET_DECL_RTL (d, NULL);
24070 DECL_IN_AGGR_P (d) = 0;
24071
24072 /* The initializer is placed in DECL_INITIAL by
24073 regenerate_decl_from_template so we don't need to
24074 push/pop_access_scope again here. Pull it out so that
24075 cp_finish_decl can process it. */
24076 init = DECL_INITIAL (d);
24077 DECL_INITIAL (d) = NULL_TREE;
24078 DECL_INITIALIZED_P (d) = 0;
24079
24080 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24081 initializer. That function will defer actual emission until
24082 we have a chance to determine linkage. */
24083 DECL_EXTERNAL (d) = 0;
24084
24085 /* Enter the scope of D so that access-checking works correctly. */
24086 bool enter_context = DECL_CLASS_SCOPE_P (d);
24087 if (enter_context)
24088 push_nested_class (DECL_CONTEXT (d));
24089
24090 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24091 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
24092
24093 if (enter_context)
24094 pop_nested_class ();
24095
24096 if (variable_template_p (gen_tmpl))
24097 note_variable_template_instantiation (d);
24098 }
24099 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
24100 synthesize_method (d);
24101 else if (TREE_CODE (d) == FUNCTION_DECL)
24102 {
24103 /* Set up the list of local specializations. */
24104 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
24105 tree block = NULL_TREE;
24106
24107 /* Set up context. */
24108 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24109 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24110 block = push_stmt_list ();
24111 else
24112 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
24113
24114 /* Some typedefs referenced from within the template code need to be
24115 access checked at template instantiation time, i.e now. These
24116 types were added to the template at parsing time. Let's get those
24117 and perform the access checks then. */
24118 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
24119 args);
24120
24121 /* Create substitution entries for the parameters. */
24122 register_parameter_specializations (code_pattern, d);
24123
24124 /* Substitute into the body of the function. */
24125 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24126 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
24127 tf_warning_or_error, tmpl);
24128 else
24129 {
24130 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
24131 tf_warning_or_error, tmpl,
24132 /*integral_constant_expression_p=*/false);
24133
24134 /* Set the current input_location to the end of the function
24135 so that finish_function knows where we are. */
24136 input_location
24137 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
24138
24139 /* Remember if we saw an infinite loop in the template. */
24140 current_function_infinite_loop
24141 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
24142 }
24143
24144 /* Finish the function. */
24145 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24146 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24147 DECL_SAVED_TREE (d) = pop_stmt_list (block);
24148 else
24149 {
24150 d = finish_function (/*inline_p=*/false);
24151 expand_or_defer_fn (d);
24152 }
24153
24154 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24155 cp_check_omp_declare_reduction (d);
24156 }
24157
24158 /* We're not deferring instantiation any more. */
24159 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
24160
24161 if (push_to_top)
24162 pop_from_top_level ();
24163 else
24164 pop_function_context ();
24165
24166 if (nested)
24167 restore_omp_privatization_clauses (omp_privatization_save);
24168
24169 out:
24170 pop_deferring_access_checks ();
24171 timevar_pop (TV_TEMPLATE_INST);
24172 pop_tinst_level ();
24173 input_location = saved_loc;
24174 cp_unevaluated_operand = saved_unevaluated_operand;
24175 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24176
24177 return d;
24178 }
24179
24180 /* Run through the list of templates that we wish we could
24181 instantiate, and instantiate any we can. RETRIES is the
24182 number of times we retry pending template instantiation. */
24183
24184 void
24185 instantiate_pending_templates (int retries)
24186 {
24187 int reconsider;
24188 location_t saved_loc = input_location;
24189
24190 /* Instantiating templates may trigger vtable generation. This in turn
24191 may require further template instantiations. We place a limit here
24192 to avoid infinite loop. */
24193 if (pending_templates && retries >= max_tinst_depth)
24194 {
24195 tree decl = pending_templates->tinst->maybe_get_node ();
24196
24197 fatal_error (input_location,
24198 "template instantiation depth exceeds maximum of %d"
24199 " instantiating %q+D, possibly from virtual table generation"
24200 " (use -ftemplate-depth= to increase the maximum)",
24201 max_tinst_depth, decl);
24202 if (TREE_CODE (decl) == FUNCTION_DECL)
24203 /* Pretend that we defined it. */
24204 DECL_INITIAL (decl) = error_mark_node;
24205 return;
24206 }
24207
24208 do
24209 {
24210 struct pending_template **t = &pending_templates;
24211 struct pending_template *last = NULL;
24212 reconsider = 0;
24213 while (*t)
24214 {
24215 tree instantiation = reopen_tinst_level ((*t)->tinst);
24216 bool complete = false;
24217
24218 if (TYPE_P (instantiation))
24219 {
24220 if (!COMPLETE_TYPE_P (instantiation))
24221 {
24222 instantiate_class_template (instantiation);
24223 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
24224 for (tree fld = TYPE_FIELDS (instantiation);
24225 fld; fld = TREE_CHAIN (fld))
24226 if ((VAR_P (fld)
24227 || (TREE_CODE (fld) == FUNCTION_DECL
24228 && !DECL_ARTIFICIAL (fld)))
24229 && DECL_TEMPLATE_INSTANTIATION (fld))
24230 instantiate_decl (fld,
24231 /*defer_ok=*/false,
24232 /*expl_inst_class_mem_p=*/false);
24233
24234 if (COMPLETE_TYPE_P (instantiation))
24235 reconsider = 1;
24236 }
24237
24238 complete = COMPLETE_TYPE_P (instantiation);
24239 }
24240 else
24241 {
24242 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
24243 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
24244 {
24245 instantiation
24246 = instantiate_decl (instantiation,
24247 /*defer_ok=*/false,
24248 /*expl_inst_class_mem_p=*/false);
24249 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
24250 reconsider = 1;
24251 }
24252
24253 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
24254 || DECL_TEMPLATE_INSTANTIATED (instantiation));
24255 }
24256
24257 if (complete)
24258 {
24259 /* If INSTANTIATION has been instantiated, then we don't
24260 need to consider it again in the future. */
24261 struct pending_template *drop = *t;
24262 *t = (*t)->next;
24263 set_refcount_ptr (drop->tinst);
24264 pending_template_freelist ().free (drop);
24265 }
24266 else
24267 {
24268 last = *t;
24269 t = &(*t)->next;
24270 }
24271 tinst_depth = 0;
24272 set_refcount_ptr (current_tinst_level);
24273 }
24274 last_pending_template = last;
24275 }
24276 while (reconsider);
24277
24278 input_location = saved_loc;
24279 }
24280
24281 /* Substitute ARGVEC into T, which is a list of initializers for
24282 either base class or a non-static data member. The TREE_PURPOSEs
24283 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24284 instantiate_decl. */
24285
24286 static tree
24287 tsubst_initializer_list (tree t, tree argvec)
24288 {
24289 tree inits = NULL_TREE;
24290 tree target_ctor = error_mark_node;
24291
24292 for (; t; t = TREE_CHAIN (t))
24293 {
24294 tree decl;
24295 tree init;
24296 tree expanded_bases = NULL_TREE;
24297 tree expanded_arguments = NULL_TREE;
24298 int i, len = 1;
24299
24300 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
24301 {
24302 tree expr;
24303 tree arg;
24304
24305 /* Expand the base class expansion type into separate base
24306 classes. */
24307 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
24308 tf_warning_or_error,
24309 NULL_TREE);
24310 if (expanded_bases == error_mark_node)
24311 continue;
24312
24313 /* We'll be building separate TREE_LISTs of arguments for
24314 each base. */
24315 len = TREE_VEC_LENGTH (expanded_bases);
24316 expanded_arguments = make_tree_vec (len);
24317 for (i = 0; i < len; i++)
24318 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
24319
24320 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24321 expand each argument in the TREE_VALUE of t. */
24322 expr = make_node (EXPR_PACK_EXPANSION);
24323 PACK_EXPANSION_LOCAL_P (expr) = true;
24324 PACK_EXPANSION_PARAMETER_PACKS (expr) =
24325 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
24326
24327 if (TREE_VALUE (t) == void_type_node)
24328 /* VOID_TYPE_NODE is used to indicate
24329 value-initialization. */
24330 {
24331 for (i = 0; i < len; i++)
24332 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
24333 }
24334 else
24335 {
24336 /* Substitute parameter packs into each argument in the
24337 TREE_LIST. */
24338 in_base_initializer = 1;
24339 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
24340 {
24341 tree expanded_exprs;
24342
24343 /* Expand the argument. */
24344 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
24345 expanded_exprs
24346 = tsubst_pack_expansion (expr, argvec,
24347 tf_warning_or_error,
24348 NULL_TREE);
24349 if (expanded_exprs == error_mark_node)
24350 continue;
24351
24352 /* Prepend each of the expanded expressions to the
24353 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
24354 for (i = 0; i < len; i++)
24355 {
24356 TREE_VEC_ELT (expanded_arguments, i) =
24357 tree_cons (NULL_TREE,
24358 TREE_VEC_ELT (expanded_exprs, i),
24359 TREE_VEC_ELT (expanded_arguments, i));
24360 }
24361 }
24362 in_base_initializer = 0;
24363
24364 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
24365 since we built them backwards. */
24366 for (i = 0; i < len; i++)
24367 {
24368 TREE_VEC_ELT (expanded_arguments, i) =
24369 nreverse (TREE_VEC_ELT (expanded_arguments, i));
24370 }
24371 }
24372 }
24373
24374 for (i = 0; i < len; ++i)
24375 {
24376 if (expanded_bases)
24377 {
24378 decl = TREE_VEC_ELT (expanded_bases, i);
24379 decl = expand_member_init (decl);
24380 init = TREE_VEC_ELT (expanded_arguments, i);
24381 }
24382 else
24383 {
24384 tree tmp;
24385 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
24386 tf_warning_or_error, NULL_TREE);
24387
24388 decl = expand_member_init (decl);
24389 if (decl && !DECL_P (decl))
24390 in_base_initializer = 1;
24391
24392 init = TREE_VALUE (t);
24393 tmp = init;
24394 if (init != void_type_node)
24395 init = tsubst_expr (init, argvec,
24396 tf_warning_or_error, NULL_TREE,
24397 /*integral_constant_expression_p=*/false);
24398 if (init == NULL_TREE && tmp != NULL_TREE)
24399 /* If we had an initializer but it instantiated to nothing,
24400 value-initialize the object. This will only occur when
24401 the initializer was a pack expansion where the parameter
24402 packs used in that expansion were of length zero. */
24403 init = void_type_node;
24404 in_base_initializer = 0;
24405 }
24406
24407 if (target_ctor != error_mark_node
24408 && init != error_mark_node)
24409 {
24410 error ("mem-initializer for %qD follows constructor delegation",
24411 decl);
24412 return inits;
24413 }
24414 /* Look for a target constructor. */
24415 if (init != error_mark_node
24416 && decl && CLASS_TYPE_P (decl)
24417 && same_type_p (decl, current_class_type))
24418 {
24419 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
24420 if (inits)
24421 {
24422 error ("constructor delegation follows mem-initializer for %qD",
24423 TREE_PURPOSE (inits));
24424 continue;
24425 }
24426 target_ctor = init;
24427 }
24428
24429 if (decl)
24430 {
24431 init = build_tree_list (decl, init);
24432 TREE_CHAIN (init) = inits;
24433 inits = init;
24434 }
24435 }
24436 }
24437 return inits;
24438 }
24439
24440 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
24441
24442 static void
24443 set_current_access_from_decl (tree decl)
24444 {
24445 if (TREE_PRIVATE (decl))
24446 current_access_specifier = access_private_node;
24447 else if (TREE_PROTECTED (decl))
24448 current_access_specifier = access_protected_node;
24449 else
24450 current_access_specifier = access_public_node;
24451 }
24452
24453 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
24454 is the instantiation (which should have been created with
24455 start_enum) and ARGS are the template arguments to use. */
24456
24457 static void
24458 tsubst_enum (tree tag, tree newtag, tree args)
24459 {
24460 tree e;
24461
24462 if (SCOPED_ENUM_P (newtag))
24463 begin_scope (sk_scoped_enum, newtag);
24464
24465 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
24466 {
24467 tree value;
24468 tree decl;
24469
24470 decl = TREE_VALUE (e);
24471 /* Note that in a template enum, the TREE_VALUE is the
24472 CONST_DECL, not the corresponding INTEGER_CST. */
24473 value = tsubst_expr (DECL_INITIAL (decl),
24474 args, tf_warning_or_error, NULL_TREE,
24475 /*integral_constant_expression_p=*/true);
24476
24477 /* Give this enumeration constant the correct access. */
24478 set_current_access_from_decl (decl);
24479
24480 /* Actually build the enumerator itself. Here we're assuming that
24481 enumerators can't have dependent attributes. */
24482 build_enumerator (DECL_NAME (decl), value, newtag,
24483 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
24484 }
24485
24486 if (SCOPED_ENUM_P (newtag))
24487 finish_scope ();
24488
24489 finish_enum_value_list (newtag);
24490 finish_enum (newtag);
24491
24492 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
24493 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
24494 }
24495
24496 /* DECL is a FUNCTION_DECL that is a template specialization. Return
24497 its type -- but without substituting the innermost set of template
24498 arguments. So, innermost set of template parameters will appear in
24499 the type. */
24500
24501 tree
24502 get_mostly_instantiated_function_type (tree decl)
24503 {
24504 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
24505 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
24506 }
24507
24508 /* Return truthvalue if we're processing a template different from
24509 the last one involved in diagnostics. */
24510 bool
24511 problematic_instantiation_changed (void)
24512 {
24513 return current_tinst_level != last_error_tinst_level;
24514 }
24515
24516 /* Remember current template involved in diagnostics. */
24517 void
24518 record_last_problematic_instantiation (void)
24519 {
24520 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
24521 }
24522
24523 struct tinst_level *
24524 current_instantiation (void)
24525 {
24526 return current_tinst_level;
24527 }
24528
24529 /* Return TRUE if current_function_decl is being instantiated, false
24530 otherwise. */
24531
24532 bool
24533 instantiating_current_function_p (void)
24534 {
24535 return (current_instantiation ()
24536 && (current_instantiation ()->maybe_get_node ()
24537 == current_function_decl));
24538 }
24539
24540 /* [temp.param] Check that template non-type parm TYPE is of an allowable
24541 type. Return false for ok, true for disallowed. Issue error and
24542 inform messages under control of COMPLAIN. */
24543
24544 static bool
24545 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
24546 {
24547 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
24548 return false;
24549 else if (TYPE_PTR_P (type))
24550 return false;
24551 else if (TYPE_REF_P (type)
24552 && !TYPE_REF_IS_RVALUE (type))
24553 return false;
24554 else if (TYPE_PTRMEM_P (type))
24555 return false;
24556 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
24557 return false;
24558 else if (TREE_CODE (type) == TYPENAME_TYPE)
24559 return false;
24560 else if (TREE_CODE (type) == DECLTYPE_TYPE)
24561 return false;
24562 else if (TREE_CODE (type) == NULLPTR_TYPE)
24563 return false;
24564 /* A bound template template parm could later be instantiated to have a valid
24565 nontype parm type via an alias template. */
24566 else if (cxx_dialect >= cxx11
24567 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24568 return false;
24569
24570 if (complain & tf_error)
24571 {
24572 if (type == error_mark_node)
24573 inform (input_location, "invalid template non-type parameter");
24574 else
24575 error ("%q#T is not a valid type for a template non-type parameter",
24576 type);
24577 }
24578 return true;
24579 }
24580
24581 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
24582 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
24583
24584 static bool
24585 dependent_type_p_r (tree type)
24586 {
24587 tree scope;
24588
24589 /* [temp.dep.type]
24590
24591 A type is dependent if it is:
24592
24593 -- a template parameter. Template template parameters are types
24594 for us (since TYPE_P holds true for them) so we handle
24595 them here. */
24596 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
24597 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
24598 return true;
24599 /* -- a qualified-id with a nested-name-specifier which contains a
24600 class-name that names a dependent type or whose unqualified-id
24601 names a dependent type. */
24602 if (TREE_CODE (type) == TYPENAME_TYPE)
24603 return true;
24604
24605 /* An alias template specialization can be dependent even if the
24606 resulting type is not. */
24607 if (dependent_alias_template_spec_p (type))
24608 return true;
24609
24610 /* -- a cv-qualified type where the cv-unqualified type is
24611 dependent.
24612 No code is necessary for this bullet; the code below handles
24613 cv-qualified types, and we don't want to strip aliases with
24614 TYPE_MAIN_VARIANT because of DR 1558. */
24615 /* -- a compound type constructed from any dependent type. */
24616 if (TYPE_PTRMEM_P (type))
24617 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
24618 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24619 (type)));
24620 else if (INDIRECT_TYPE_P (type))
24621 return dependent_type_p (TREE_TYPE (type));
24622 else if (TREE_CODE (type) == FUNCTION_TYPE
24623 || TREE_CODE (type) == METHOD_TYPE)
24624 {
24625 tree arg_type;
24626
24627 if (dependent_type_p (TREE_TYPE (type)))
24628 return true;
24629 for (arg_type = TYPE_ARG_TYPES (type);
24630 arg_type;
24631 arg_type = TREE_CHAIN (arg_type))
24632 if (dependent_type_p (TREE_VALUE (arg_type)))
24633 return true;
24634 if (cxx_dialect >= cxx17)
24635 /* A value-dependent noexcept-specifier makes the type dependent. */
24636 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
24637 if (tree noex = TREE_PURPOSE (spec))
24638 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24639 affect overload resolution and treating it as dependent breaks
24640 things. */
24641 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
24642 && value_dependent_expression_p (noex))
24643 return true;
24644 return false;
24645 }
24646 /* -- an array type constructed from any dependent type or whose
24647 size is specified by a constant expression that is
24648 value-dependent.
24649
24650 We checked for type- and value-dependence of the bounds in
24651 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24652 if (TREE_CODE (type) == ARRAY_TYPE)
24653 {
24654 if (TYPE_DOMAIN (type)
24655 && dependent_type_p (TYPE_DOMAIN (type)))
24656 return true;
24657 return dependent_type_p (TREE_TYPE (type));
24658 }
24659
24660 /* -- a template-id in which either the template name is a template
24661 parameter ... */
24662 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24663 return true;
24664 /* ... or any of the template arguments is a dependent type or
24665 an expression that is type-dependent or value-dependent. */
24666 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
24667 && (any_dependent_template_arguments_p
24668 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
24669 return true;
24670
24671 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24672 dependent; if the argument of the `typeof' expression is not
24673 type-dependent, then it should already been have resolved. */
24674 if (TREE_CODE (type) == TYPEOF_TYPE
24675 || TREE_CODE (type) == DECLTYPE_TYPE
24676 || TREE_CODE (type) == UNDERLYING_TYPE)
24677 return true;
24678
24679 /* A template argument pack is dependent if any of its packed
24680 arguments are. */
24681 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
24682 {
24683 tree args = ARGUMENT_PACK_ARGS (type);
24684 int i, len = TREE_VEC_LENGTH (args);
24685 for (i = 0; i < len; ++i)
24686 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24687 return true;
24688 }
24689
24690 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24691 be template parameters. */
24692 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
24693 return true;
24694
24695 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
24696 return true;
24697
24698 /* The standard does not specifically mention types that are local
24699 to template functions or local classes, but they should be
24700 considered dependent too. For example:
24701
24702 template <int I> void f() {
24703 enum E { a = I };
24704 S<sizeof (E)> s;
24705 }
24706
24707 The size of `E' cannot be known until the value of `I' has been
24708 determined. Therefore, `E' must be considered dependent. */
24709 scope = TYPE_CONTEXT (type);
24710 if (scope && TYPE_P (scope))
24711 return dependent_type_p (scope);
24712 /* Don't use type_dependent_expression_p here, as it can lead
24713 to infinite recursion trying to determine whether a lambda
24714 nested in a lambda is dependent (c++/47687). */
24715 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
24716 && DECL_LANG_SPECIFIC (scope)
24717 && DECL_TEMPLATE_INFO (scope)
24718 && (any_dependent_template_arguments_p
24719 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
24720 return true;
24721
24722 /* Other types are non-dependent. */
24723 return false;
24724 }
24725
24726 /* Returns TRUE if TYPE is dependent, in the sense of
24727 [temp.dep.type]. Note that a NULL type is considered dependent. */
24728
24729 bool
24730 dependent_type_p (tree type)
24731 {
24732 /* If there are no template parameters in scope, then there can't be
24733 any dependent types. */
24734 if (!processing_template_decl)
24735 {
24736 /* If we are not processing a template, then nobody should be
24737 providing us with a dependent type. */
24738 gcc_assert (type);
24739 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
24740 return false;
24741 }
24742
24743 /* If the type is NULL, we have not computed a type for the entity
24744 in question; in that case, the type is dependent. */
24745 if (!type)
24746 return true;
24747
24748 /* Erroneous types can be considered non-dependent. */
24749 if (type == error_mark_node)
24750 return false;
24751
24752 /* Getting here with global_type_node means we improperly called this
24753 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24754 gcc_checking_assert (type != global_type_node);
24755
24756 /* If we have not already computed the appropriate value for TYPE,
24757 do so now. */
24758 if (!TYPE_DEPENDENT_P_VALID (type))
24759 {
24760 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
24761 TYPE_DEPENDENT_P_VALID (type) = 1;
24762 }
24763
24764 return TYPE_DEPENDENT_P (type);
24765 }
24766
24767 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24768 lookup. In other words, a dependent type that is not the current
24769 instantiation. */
24770
24771 bool
24772 dependent_scope_p (tree scope)
24773 {
24774 return (scope && TYPE_P (scope) && dependent_type_p (scope)
24775 && !currently_open_class (scope));
24776 }
24777
24778 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24779 an unknown base of 'this' (and is therefore instantiation-dependent). */
24780
24781 static bool
24782 unknown_base_ref_p (tree t)
24783 {
24784 if (!current_class_ptr)
24785 return false;
24786
24787 tree mem = TREE_OPERAND (t, 1);
24788 if (shared_member_p (mem))
24789 return false;
24790
24791 tree cur = current_nonlambda_class_type ();
24792 if (!any_dependent_bases_p (cur))
24793 return false;
24794
24795 tree ctx = TREE_OPERAND (t, 0);
24796 if (DERIVED_FROM_P (ctx, cur))
24797 return false;
24798
24799 return true;
24800 }
24801
24802 /* T is a SCOPE_REF; return whether we need to consider it
24803 instantiation-dependent so that we can check access at instantiation
24804 time even though we know which member it resolves to. */
24805
24806 static bool
24807 instantiation_dependent_scope_ref_p (tree t)
24808 {
24809 if (DECL_P (TREE_OPERAND (t, 1))
24810 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
24811 && !unknown_base_ref_p (t)
24812 && accessible_in_template_p (TREE_OPERAND (t, 0),
24813 TREE_OPERAND (t, 1)))
24814 return false;
24815 else
24816 return true;
24817 }
24818
24819 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24820 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24821 expression. */
24822
24823 /* Note that this predicate is not appropriate for general expressions;
24824 only constant expressions (that satisfy potential_constant_expression)
24825 can be tested for value dependence. */
24826
24827 bool
24828 value_dependent_expression_p (tree expression)
24829 {
24830 if (!processing_template_decl || expression == NULL_TREE)
24831 return false;
24832
24833 /* A type-dependent expression is also value-dependent. */
24834 if (type_dependent_expression_p (expression))
24835 return true;
24836
24837 switch (TREE_CODE (expression))
24838 {
24839 case BASELINK:
24840 /* A dependent member function of the current instantiation. */
24841 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24842
24843 case FUNCTION_DECL:
24844 /* A dependent member function of the current instantiation. */
24845 if (DECL_CLASS_SCOPE_P (expression)
24846 && dependent_type_p (DECL_CONTEXT (expression)))
24847 return true;
24848 break;
24849
24850 case IDENTIFIER_NODE:
24851 /* A name that has not been looked up -- must be dependent. */
24852 return true;
24853
24854 case TEMPLATE_PARM_INDEX:
24855 /* A non-type template parm. */
24856 return true;
24857
24858 case CONST_DECL:
24859 /* A non-type template parm. */
24860 if (DECL_TEMPLATE_PARM_P (expression))
24861 return true;
24862 return value_dependent_expression_p (DECL_INITIAL (expression));
24863
24864 case VAR_DECL:
24865 /* A constant with literal type and is initialized
24866 with an expression that is value-dependent. */
24867 if (DECL_DEPENDENT_INIT_P (expression)
24868 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24869 || TYPE_REF_P (TREE_TYPE (expression)))
24870 return true;
24871 if (DECL_HAS_VALUE_EXPR_P (expression))
24872 {
24873 tree value_expr = DECL_VALUE_EXPR (expression);
24874 if (value_dependent_expression_p (value_expr))
24875 return true;
24876 }
24877 return false;
24878
24879 case DYNAMIC_CAST_EXPR:
24880 case STATIC_CAST_EXPR:
24881 case CONST_CAST_EXPR:
24882 case REINTERPRET_CAST_EXPR:
24883 case CAST_EXPR:
24884 case IMPLICIT_CONV_EXPR:
24885 /* These expressions are value-dependent if the type to which
24886 the cast occurs is dependent or the expression being casted
24887 is value-dependent. */
24888 {
24889 tree type = TREE_TYPE (expression);
24890
24891 if (dependent_type_p (type))
24892 return true;
24893
24894 /* A functional cast has a list of operands. */
24895 expression = TREE_OPERAND (expression, 0);
24896 if (!expression)
24897 {
24898 /* If there are no operands, it must be an expression such
24899 as "int()". This should not happen for aggregate types
24900 because it would form non-constant expressions. */
24901 gcc_assert (cxx_dialect >= cxx11
24902 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24903
24904 return false;
24905 }
24906
24907 if (TREE_CODE (expression) == TREE_LIST)
24908 return any_value_dependent_elements_p (expression);
24909
24910 return value_dependent_expression_p (expression);
24911 }
24912
24913 case SIZEOF_EXPR:
24914 if (SIZEOF_EXPR_TYPE_P (expression))
24915 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24916 /* FALLTHRU */
24917 case ALIGNOF_EXPR:
24918 case TYPEID_EXPR:
24919 /* A `sizeof' expression is value-dependent if the operand is
24920 type-dependent or is a pack expansion. */
24921 expression = TREE_OPERAND (expression, 0);
24922 if (PACK_EXPANSION_P (expression))
24923 return true;
24924 else if (TYPE_P (expression))
24925 return dependent_type_p (expression);
24926 return instantiation_dependent_uneval_expression_p (expression);
24927
24928 case AT_ENCODE_EXPR:
24929 /* An 'encode' expression is value-dependent if the operand is
24930 type-dependent. */
24931 expression = TREE_OPERAND (expression, 0);
24932 return dependent_type_p (expression);
24933
24934 case NOEXCEPT_EXPR:
24935 expression = TREE_OPERAND (expression, 0);
24936 return instantiation_dependent_uneval_expression_p (expression);
24937
24938 case SCOPE_REF:
24939 /* All instantiation-dependent expressions should also be considered
24940 value-dependent. */
24941 return instantiation_dependent_scope_ref_p (expression);
24942
24943 case COMPONENT_REF:
24944 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24945 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24946
24947 case NONTYPE_ARGUMENT_PACK:
24948 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24949 is value-dependent. */
24950 {
24951 tree values = ARGUMENT_PACK_ARGS (expression);
24952 int i, len = TREE_VEC_LENGTH (values);
24953
24954 for (i = 0; i < len; ++i)
24955 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24956 return true;
24957
24958 return false;
24959 }
24960
24961 case TRAIT_EXPR:
24962 {
24963 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24964
24965 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24966 return true;
24967
24968 if (!type2)
24969 return false;
24970
24971 if (TREE_CODE (type2) != TREE_LIST)
24972 return dependent_type_p (type2);
24973
24974 for (; type2; type2 = TREE_CHAIN (type2))
24975 if (dependent_type_p (TREE_VALUE (type2)))
24976 return true;
24977
24978 return false;
24979 }
24980
24981 case MODOP_EXPR:
24982 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24983 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24984
24985 case ARRAY_REF:
24986 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24987 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24988
24989 case ADDR_EXPR:
24990 {
24991 tree op = TREE_OPERAND (expression, 0);
24992 return (value_dependent_expression_p (op)
24993 || has_value_dependent_address (op));
24994 }
24995
24996 case REQUIRES_EXPR:
24997 /* Treat all requires-expressions as value-dependent so
24998 we don't try to fold them. */
24999 return true;
25000
25001 case TYPE_REQ:
25002 return dependent_type_p (TREE_OPERAND (expression, 0));
25003
25004 case CALL_EXPR:
25005 {
25006 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
25007 return true;
25008 tree fn = get_callee_fndecl (expression);
25009 int i, nargs;
25010 nargs = call_expr_nargs (expression);
25011 for (i = 0; i < nargs; ++i)
25012 {
25013 tree op = CALL_EXPR_ARG (expression, i);
25014 /* In a call to a constexpr member function, look through the
25015 implicit ADDR_EXPR on the object argument so that it doesn't
25016 cause the call to be considered value-dependent. We also
25017 look through it in potential_constant_expression. */
25018 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
25019 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
25020 && TREE_CODE (op) == ADDR_EXPR)
25021 op = TREE_OPERAND (op, 0);
25022 if (value_dependent_expression_p (op))
25023 return true;
25024 }
25025 return false;
25026 }
25027
25028 case TEMPLATE_ID_EXPR:
25029 return variable_concept_p (TREE_OPERAND (expression, 0));
25030
25031 case CONSTRUCTOR:
25032 {
25033 unsigned ix;
25034 tree val;
25035 if (dependent_type_p (TREE_TYPE (expression)))
25036 return true;
25037 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
25038 if (value_dependent_expression_p (val))
25039 return true;
25040 return false;
25041 }
25042
25043 case STMT_EXPR:
25044 /* Treat a GNU statement expression as dependent to avoid crashing
25045 under instantiate_non_dependent_expr; it can't be constant. */
25046 return true;
25047
25048 default:
25049 /* A constant expression is value-dependent if any subexpression is
25050 value-dependent. */
25051 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
25052 {
25053 case tcc_reference:
25054 case tcc_unary:
25055 case tcc_comparison:
25056 case tcc_binary:
25057 case tcc_expression:
25058 case tcc_vl_exp:
25059 {
25060 int i, len = cp_tree_operand_length (expression);
25061
25062 for (i = 0; i < len; i++)
25063 {
25064 tree t = TREE_OPERAND (expression, i);
25065
25066 /* In some cases, some of the operands may be missing.
25067 (For example, in the case of PREDECREMENT_EXPR, the
25068 amount to increment by may be missing.) That doesn't
25069 make the expression dependent. */
25070 if (t && value_dependent_expression_p (t))
25071 return true;
25072 }
25073 }
25074 break;
25075 default:
25076 break;
25077 }
25078 break;
25079 }
25080
25081 /* The expression is not value-dependent. */
25082 return false;
25083 }
25084
25085 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25086 [temp.dep.expr]. Note that an expression with no type is
25087 considered dependent. Other parts of the compiler arrange for an
25088 expression with type-dependent subexpressions to have no type, so
25089 this function doesn't have to be fully recursive. */
25090
25091 bool
25092 type_dependent_expression_p (tree expression)
25093 {
25094 if (!processing_template_decl)
25095 return false;
25096
25097 if (expression == NULL_TREE || expression == error_mark_node)
25098 return false;
25099
25100 STRIP_ANY_LOCATION_WRAPPER (expression);
25101
25102 /* An unresolved name is always dependent. */
25103 if (identifier_p (expression)
25104 || TREE_CODE (expression) == USING_DECL
25105 || TREE_CODE (expression) == WILDCARD_DECL)
25106 return true;
25107
25108 /* A fold expression is type-dependent. */
25109 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
25110 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
25111 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
25112 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
25113 return true;
25114
25115 /* Some expression forms are never type-dependent. */
25116 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
25117 || TREE_CODE (expression) == SIZEOF_EXPR
25118 || TREE_CODE (expression) == ALIGNOF_EXPR
25119 || TREE_CODE (expression) == AT_ENCODE_EXPR
25120 || TREE_CODE (expression) == NOEXCEPT_EXPR
25121 || TREE_CODE (expression) == TRAIT_EXPR
25122 || TREE_CODE (expression) == TYPEID_EXPR
25123 || TREE_CODE (expression) == DELETE_EXPR
25124 || TREE_CODE (expression) == VEC_DELETE_EXPR
25125 || TREE_CODE (expression) == THROW_EXPR
25126 || TREE_CODE (expression) == REQUIRES_EXPR)
25127 return false;
25128
25129 /* The types of these expressions depends only on the type to which
25130 the cast occurs. */
25131 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
25132 || TREE_CODE (expression) == STATIC_CAST_EXPR
25133 || TREE_CODE (expression) == CONST_CAST_EXPR
25134 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
25135 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
25136 || TREE_CODE (expression) == CAST_EXPR)
25137 return dependent_type_p (TREE_TYPE (expression));
25138
25139 /* The types of these expressions depends only on the type created
25140 by the expression. */
25141 if (TREE_CODE (expression) == NEW_EXPR
25142 || TREE_CODE (expression) == VEC_NEW_EXPR)
25143 {
25144 /* For NEW_EXPR tree nodes created inside a template, either
25145 the object type itself or a TREE_LIST may appear as the
25146 operand 1. */
25147 tree type = TREE_OPERAND (expression, 1);
25148 if (TREE_CODE (type) == TREE_LIST)
25149 /* This is an array type. We need to check array dimensions
25150 as well. */
25151 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
25152 || value_dependent_expression_p
25153 (TREE_OPERAND (TREE_VALUE (type), 1));
25154 else
25155 return dependent_type_p (type);
25156 }
25157
25158 if (TREE_CODE (expression) == SCOPE_REF)
25159 {
25160 tree scope = TREE_OPERAND (expression, 0);
25161 tree name = TREE_OPERAND (expression, 1);
25162
25163 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25164 contains an identifier associated by name lookup with one or more
25165 declarations declared with a dependent type, or...a
25166 nested-name-specifier or qualified-id that names a member of an
25167 unknown specialization. */
25168 return (type_dependent_expression_p (name)
25169 || dependent_scope_p (scope));
25170 }
25171
25172 if (TREE_CODE (expression) == TEMPLATE_DECL
25173 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
25174 return uses_outer_template_parms (expression);
25175
25176 if (TREE_CODE (expression) == STMT_EXPR)
25177 expression = stmt_expr_value_expr (expression);
25178
25179 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
25180 {
25181 tree elt;
25182 unsigned i;
25183
25184 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
25185 {
25186 if (type_dependent_expression_p (elt))
25187 return true;
25188 }
25189 return false;
25190 }
25191
25192 /* A static data member of the current instantiation with incomplete
25193 array type is type-dependent, as the definition and specializations
25194 can have different bounds. */
25195 if (VAR_P (expression)
25196 && DECL_CLASS_SCOPE_P (expression)
25197 && dependent_type_p (DECL_CONTEXT (expression))
25198 && VAR_HAD_UNKNOWN_BOUND (expression))
25199 return true;
25200
25201 /* An array of unknown bound depending on a variadic parameter, eg:
25202
25203 template<typename... Args>
25204 void foo (Args... args)
25205 {
25206 int arr[] = { args... };
25207 }
25208
25209 template<int... vals>
25210 void bar ()
25211 {
25212 int arr[] = { vals... };
25213 }
25214
25215 If the array has no length and has an initializer, it must be that
25216 we couldn't determine its length in cp_complete_array_type because
25217 it is dependent. */
25218 if (VAR_P (expression)
25219 && TREE_TYPE (expression) != NULL_TREE
25220 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
25221 && !TYPE_DOMAIN (TREE_TYPE (expression))
25222 && DECL_INITIAL (expression))
25223 return true;
25224
25225 /* A function or variable template-id is type-dependent if it has any
25226 dependent template arguments. */
25227 if (VAR_OR_FUNCTION_DECL_P (expression)
25228 && DECL_LANG_SPECIFIC (expression)
25229 && DECL_TEMPLATE_INFO (expression))
25230 {
25231 /* Consider the innermost template arguments, since those are the ones
25232 that come from the template-id; the template arguments for the
25233 enclosing class do not make it type-dependent unless they are used in
25234 the type of the decl. */
25235 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
25236 && (any_dependent_template_arguments_p
25237 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
25238 return true;
25239 }
25240
25241 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25242 type-dependent. Checking this is important for functions with auto return
25243 type, which looks like a dependent type. */
25244 if (TREE_CODE (expression) == FUNCTION_DECL
25245 && !(DECL_CLASS_SCOPE_P (expression)
25246 && dependent_type_p (DECL_CONTEXT (expression)))
25247 && !(DECL_LANG_SPECIFIC (expression)
25248 && DECL_FRIEND_P (expression)
25249 && (!DECL_FRIEND_CONTEXT (expression)
25250 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
25251 && !DECL_LOCAL_FUNCTION_P (expression))
25252 {
25253 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
25254 || undeduced_auto_decl (expression));
25255 return false;
25256 }
25257
25258 /* Always dependent, on the number of arguments if nothing else. */
25259 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
25260 return true;
25261
25262 if (TREE_TYPE (expression) == unknown_type_node)
25263 {
25264 if (TREE_CODE (expression) == ADDR_EXPR)
25265 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
25266 if (TREE_CODE (expression) == COMPONENT_REF
25267 || TREE_CODE (expression) == OFFSET_REF)
25268 {
25269 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
25270 return true;
25271 expression = TREE_OPERAND (expression, 1);
25272 if (identifier_p (expression))
25273 return false;
25274 }
25275 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25276 if (TREE_CODE (expression) == SCOPE_REF)
25277 return false;
25278
25279 if (BASELINK_P (expression))
25280 {
25281 if (BASELINK_OPTYPE (expression)
25282 && dependent_type_p (BASELINK_OPTYPE (expression)))
25283 return true;
25284 expression = BASELINK_FUNCTIONS (expression);
25285 }
25286
25287 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
25288 {
25289 if (any_dependent_template_arguments_p
25290 (TREE_OPERAND (expression, 1)))
25291 return true;
25292 expression = TREE_OPERAND (expression, 0);
25293 if (identifier_p (expression))
25294 return true;
25295 }
25296
25297 gcc_assert (TREE_CODE (expression) == OVERLOAD
25298 || TREE_CODE (expression) == FUNCTION_DECL);
25299
25300 for (lkp_iterator iter (expression); iter; ++iter)
25301 if (type_dependent_expression_p (*iter))
25302 return true;
25303
25304 return false;
25305 }
25306
25307 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
25308
25309 /* Dependent type attributes might not have made it from the decl to
25310 the type yet. */
25311 if (DECL_P (expression)
25312 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
25313 return true;
25314
25315 return (dependent_type_p (TREE_TYPE (expression)));
25316 }
25317
25318 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
25319 type-dependent if the expression refers to a member of the current
25320 instantiation and the type of the referenced member is dependent, or the
25321 class member access expression refers to a member of an unknown
25322 specialization.
25323
25324 This function returns true if the OBJECT in such a class member access
25325 expression is of an unknown specialization. */
25326
25327 bool
25328 type_dependent_object_expression_p (tree object)
25329 {
25330 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
25331 dependent. */
25332 if (TREE_CODE (object) == IDENTIFIER_NODE)
25333 return true;
25334 tree scope = TREE_TYPE (object);
25335 return (!scope || dependent_scope_p (scope));
25336 }
25337
25338 /* walk_tree callback function for instantiation_dependent_expression_p,
25339 below. Returns non-zero if a dependent subexpression is found. */
25340
25341 static tree
25342 instantiation_dependent_r (tree *tp, int *walk_subtrees,
25343 void * /*data*/)
25344 {
25345 if (TYPE_P (*tp))
25346 {
25347 /* We don't have to worry about decltype currently because decltype
25348 of an instantiation-dependent expr is a dependent type. This
25349 might change depending on the resolution of DR 1172. */
25350 *walk_subtrees = false;
25351 return NULL_TREE;
25352 }
25353 enum tree_code code = TREE_CODE (*tp);
25354 switch (code)
25355 {
25356 /* Don't treat an argument list as dependent just because it has no
25357 TREE_TYPE. */
25358 case TREE_LIST:
25359 case TREE_VEC:
25360 case NONTYPE_ARGUMENT_PACK:
25361 return NULL_TREE;
25362
25363 case TEMPLATE_PARM_INDEX:
25364 return *tp;
25365
25366 /* Handle expressions with type operands. */
25367 case SIZEOF_EXPR:
25368 case ALIGNOF_EXPR:
25369 case TYPEID_EXPR:
25370 case AT_ENCODE_EXPR:
25371 {
25372 tree op = TREE_OPERAND (*tp, 0);
25373 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
25374 op = TREE_TYPE (op);
25375 if (TYPE_P (op))
25376 {
25377 if (dependent_type_p (op))
25378 return *tp;
25379 else
25380 {
25381 *walk_subtrees = false;
25382 return NULL_TREE;
25383 }
25384 }
25385 break;
25386 }
25387
25388 case COMPONENT_REF:
25389 if (identifier_p (TREE_OPERAND (*tp, 1)))
25390 /* In a template, finish_class_member_access_expr creates a
25391 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
25392 type-dependent, so that we can check access control at
25393 instantiation time (PR 42277). See also Core issue 1273. */
25394 return *tp;
25395 break;
25396
25397 case SCOPE_REF:
25398 if (instantiation_dependent_scope_ref_p (*tp))
25399 return *tp;
25400 else
25401 break;
25402
25403 /* Treat statement-expressions as dependent. */
25404 case BIND_EXPR:
25405 return *tp;
25406
25407 /* Treat requires-expressions as dependent. */
25408 case REQUIRES_EXPR:
25409 return *tp;
25410
25411 case CALL_EXPR:
25412 /* Treat calls to function concepts as dependent. */
25413 if (function_concept_check_p (*tp))
25414 return *tp;
25415 break;
25416
25417 case TEMPLATE_ID_EXPR:
25418 /* And variable concepts. */
25419 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
25420 return *tp;
25421 break;
25422
25423 default:
25424 break;
25425 }
25426
25427 if (type_dependent_expression_p (*tp))
25428 return *tp;
25429 else
25430 return NULL_TREE;
25431 }
25432
25433 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
25434 sense defined by the ABI:
25435
25436 "An expression is instantiation-dependent if it is type-dependent
25437 or value-dependent, or it has a subexpression that is type-dependent
25438 or value-dependent."
25439
25440 Except don't actually check value-dependence for unevaluated expressions,
25441 because in sizeof(i) we don't care about the value of i. Checking
25442 type-dependence will in turn check value-dependence of array bounds/template
25443 arguments as needed. */
25444
25445 bool
25446 instantiation_dependent_uneval_expression_p (tree expression)
25447 {
25448 tree result;
25449
25450 if (!processing_template_decl)
25451 return false;
25452
25453 if (expression == error_mark_node)
25454 return false;
25455
25456 result = cp_walk_tree_without_duplicates (&expression,
25457 instantiation_dependent_r, NULL);
25458 return result != NULL_TREE;
25459 }
25460
25461 /* As above, but also check value-dependence of the expression as a whole. */
25462
25463 bool
25464 instantiation_dependent_expression_p (tree expression)
25465 {
25466 return (instantiation_dependent_uneval_expression_p (expression)
25467 || value_dependent_expression_p (expression));
25468 }
25469
25470 /* Like type_dependent_expression_p, but it also works while not processing
25471 a template definition, i.e. during substitution or mangling. */
25472
25473 bool
25474 type_dependent_expression_p_push (tree expr)
25475 {
25476 bool b;
25477 ++processing_template_decl;
25478 b = type_dependent_expression_p (expr);
25479 --processing_template_decl;
25480 return b;
25481 }
25482
25483 /* Returns TRUE if ARGS contains a type-dependent expression. */
25484
25485 bool
25486 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
25487 {
25488 unsigned int i;
25489 tree arg;
25490
25491 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
25492 {
25493 if (type_dependent_expression_p (arg))
25494 return true;
25495 }
25496 return false;
25497 }
25498
25499 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25500 expressions) contains any type-dependent expressions. */
25501
25502 bool
25503 any_type_dependent_elements_p (const_tree list)
25504 {
25505 for (; list; list = TREE_CHAIN (list))
25506 if (type_dependent_expression_p (TREE_VALUE (list)))
25507 return true;
25508
25509 return false;
25510 }
25511
25512 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25513 expressions) contains any value-dependent expressions. */
25514
25515 bool
25516 any_value_dependent_elements_p (const_tree list)
25517 {
25518 for (; list; list = TREE_CHAIN (list))
25519 if (value_dependent_expression_p (TREE_VALUE (list)))
25520 return true;
25521
25522 return false;
25523 }
25524
25525 /* Returns TRUE if the ARG (a template argument) is dependent. */
25526
25527 bool
25528 dependent_template_arg_p (tree arg)
25529 {
25530 if (!processing_template_decl)
25531 return false;
25532
25533 /* Assume a template argument that was wrongly written by the user
25534 is dependent. This is consistent with what
25535 any_dependent_template_arguments_p [that calls this function]
25536 does. */
25537 if (!arg || arg == error_mark_node)
25538 return true;
25539
25540 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
25541 arg = argument_pack_select_arg (arg);
25542
25543 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
25544 return true;
25545 if (TREE_CODE (arg) == TEMPLATE_DECL)
25546 {
25547 if (DECL_TEMPLATE_PARM_P (arg))
25548 return true;
25549 /* A member template of a dependent class is not necessarily
25550 type-dependent, but it is a dependent template argument because it
25551 will be a member of an unknown specialization to that template. */
25552 tree scope = CP_DECL_CONTEXT (arg);
25553 return TYPE_P (scope) && dependent_type_p (scope);
25554 }
25555 else if (ARGUMENT_PACK_P (arg))
25556 {
25557 tree args = ARGUMENT_PACK_ARGS (arg);
25558 int i, len = TREE_VEC_LENGTH (args);
25559 for (i = 0; i < len; ++i)
25560 {
25561 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25562 return true;
25563 }
25564
25565 return false;
25566 }
25567 else if (TYPE_P (arg))
25568 return dependent_type_p (arg);
25569 else
25570 return (type_dependent_expression_p (arg)
25571 || value_dependent_expression_p (arg));
25572 }
25573
25574 /* Returns true if ARGS (a collection of template arguments) contains
25575 any types that require structural equality testing. */
25576
25577 bool
25578 any_template_arguments_need_structural_equality_p (tree args)
25579 {
25580 int i;
25581 int j;
25582
25583 if (!args)
25584 return false;
25585 if (args == error_mark_node)
25586 return true;
25587
25588 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25589 {
25590 tree level = TMPL_ARGS_LEVEL (args, i + 1);
25591 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25592 {
25593 tree arg = TREE_VEC_ELT (level, j);
25594 tree packed_args = NULL_TREE;
25595 int k, len = 1;
25596
25597 if (ARGUMENT_PACK_P (arg))
25598 {
25599 /* Look inside the argument pack. */
25600 packed_args = ARGUMENT_PACK_ARGS (arg);
25601 len = TREE_VEC_LENGTH (packed_args);
25602 }
25603
25604 for (k = 0; k < len; ++k)
25605 {
25606 if (packed_args)
25607 arg = TREE_VEC_ELT (packed_args, k);
25608
25609 if (error_operand_p (arg))
25610 return true;
25611 else if (TREE_CODE (arg) == TEMPLATE_DECL)
25612 continue;
25613 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
25614 return true;
25615 else if (!TYPE_P (arg) && TREE_TYPE (arg)
25616 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
25617 return true;
25618 }
25619 }
25620 }
25621
25622 return false;
25623 }
25624
25625 /* Returns true if ARGS (a collection of template arguments) contains
25626 any dependent arguments. */
25627
25628 bool
25629 any_dependent_template_arguments_p (const_tree args)
25630 {
25631 int i;
25632 int j;
25633
25634 if (!args)
25635 return false;
25636 if (args == error_mark_node)
25637 return true;
25638
25639 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25640 {
25641 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25642 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25643 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
25644 return true;
25645 }
25646
25647 return false;
25648 }
25649
25650 /* Returns true if ARGS contains any errors. */
25651
25652 bool
25653 any_erroneous_template_args_p (const_tree args)
25654 {
25655 int i;
25656 int j;
25657
25658 if (args == error_mark_node)
25659 return true;
25660
25661 if (args && TREE_CODE (args) != TREE_VEC)
25662 {
25663 if (tree ti = get_template_info (args))
25664 args = TI_ARGS (ti);
25665 else
25666 args = NULL_TREE;
25667 }
25668
25669 if (!args)
25670 return false;
25671
25672 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25673 {
25674 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25675 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25676 if (error_operand_p (TREE_VEC_ELT (level, j)))
25677 return true;
25678 }
25679
25680 return false;
25681 }
25682
25683 /* Returns TRUE if the template TMPL is type-dependent. */
25684
25685 bool
25686 dependent_template_p (tree tmpl)
25687 {
25688 if (TREE_CODE (tmpl) == OVERLOAD)
25689 {
25690 for (lkp_iterator iter (tmpl); iter; ++iter)
25691 if (dependent_template_p (*iter))
25692 return true;
25693 return false;
25694 }
25695
25696 /* Template template parameters are dependent. */
25697 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
25698 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
25699 return true;
25700 /* So are names that have not been looked up. */
25701 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
25702 return true;
25703 return false;
25704 }
25705
25706 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25707
25708 bool
25709 dependent_template_id_p (tree tmpl, tree args)
25710 {
25711 return (dependent_template_p (tmpl)
25712 || any_dependent_template_arguments_p (args));
25713 }
25714
25715 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25716 are dependent. */
25717
25718 bool
25719 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
25720 {
25721 int i;
25722
25723 if (!processing_template_decl)
25724 return false;
25725
25726 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
25727 {
25728 tree decl = TREE_VEC_ELT (declv, i);
25729 tree init = TREE_VEC_ELT (initv, i);
25730 tree cond = TREE_VEC_ELT (condv, i);
25731 tree incr = TREE_VEC_ELT (incrv, i);
25732
25733 if (type_dependent_expression_p (decl)
25734 || TREE_CODE (decl) == SCOPE_REF)
25735 return true;
25736
25737 if (init && type_dependent_expression_p (init))
25738 return true;
25739
25740 if (type_dependent_expression_p (cond))
25741 return true;
25742
25743 if (COMPARISON_CLASS_P (cond)
25744 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
25745 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
25746 return true;
25747
25748 if (TREE_CODE (incr) == MODOP_EXPR)
25749 {
25750 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
25751 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
25752 return true;
25753 }
25754 else if (type_dependent_expression_p (incr))
25755 return true;
25756 else if (TREE_CODE (incr) == MODIFY_EXPR)
25757 {
25758 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
25759 return true;
25760 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
25761 {
25762 tree t = TREE_OPERAND (incr, 1);
25763 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
25764 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
25765 return true;
25766 }
25767 }
25768 }
25769
25770 return false;
25771 }
25772
25773 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25774 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25775 no such TYPE can be found. Note that this function peers inside
25776 uninstantiated templates and therefore should be used only in
25777 extremely limited situations. ONLY_CURRENT_P restricts this
25778 peering to the currently open classes hierarchy (which is required
25779 when comparing types). */
25780
25781 tree
25782 resolve_typename_type (tree type, bool only_current_p)
25783 {
25784 tree scope;
25785 tree name;
25786 tree decl;
25787 int quals;
25788 tree pushed_scope;
25789 tree result;
25790
25791 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
25792
25793 scope = TYPE_CONTEXT (type);
25794 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25795 gcc_checking_assert (uses_template_parms (scope));
25796
25797 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25798 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25799 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25800 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25801 identifier of the TYPENAME_TYPE anymore.
25802 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25803 TYPENAME_TYPE instead, we avoid messing up with a possible
25804 typedef variant case. */
25805 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
25806
25807 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25808 it first before we can figure out what NAME refers to. */
25809 if (TREE_CODE (scope) == TYPENAME_TYPE)
25810 {
25811 if (TYPENAME_IS_RESOLVING_P (scope))
25812 /* Given a class template A with a dependent base with nested type C,
25813 typedef typename A::C::C C will land us here, as trying to resolve
25814 the initial A::C leads to the local C typedef, which leads back to
25815 A::C::C. So we break the recursion now. */
25816 return type;
25817 else
25818 scope = resolve_typename_type (scope, only_current_p);
25819 }
25820 /* If we don't know what SCOPE refers to, then we cannot resolve the
25821 TYPENAME_TYPE. */
25822 if (!CLASS_TYPE_P (scope))
25823 return type;
25824 /* If this is a typedef, we don't want to look inside (c++/11987). */
25825 if (typedef_variant_p (type))
25826 return type;
25827 /* If SCOPE isn't the template itself, it will not have a valid
25828 TYPE_FIELDS list. */
25829 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
25830 /* scope is either the template itself or a compatible instantiation
25831 like X<T>, so look up the name in the original template. */
25832 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
25833 /* If scope has no fields, it can't be a current instantiation. Check this
25834 before currently_open_class to avoid infinite recursion (71515). */
25835 if (!TYPE_FIELDS (scope))
25836 return type;
25837 /* If the SCOPE is not the current instantiation, there's no reason
25838 to look inside it. */
25839 if (only_current_p && !currently_open_class (scope))
25840 return type;
25841 /* Enter the SCOPE so that name lookup will be resolved as if we
25842 were in the class definition. In particular, SCOPE will no
25843 longer be considered a dependent type. */
25844 pushed_scope = push_scope (scope);
25845 /* Look up the declaration. */
25846 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
25847 tf_warning_or_error);
25848
25849 result = NULL_TREE;
25850
25851 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25852 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25853 tree fullname = TYPENAME_TYPE_FULLNAME (type);
25854 if (!decl)
25855 /*nop*/;
25856 else if (identifier_p (fullname)
25857 && TREE_CODE (decl) == TYPE_DECL)
25858 {
25859 result = TREE_TYPE (decl);
25860 if (result == error_mark_node)
25861 result = NULL_TREE;
25862 }
25863 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25864 && DECL_CLASS_TEMPLATE_P (decl))
25865 {
25866 /* Obtain the template and the arguments. */
25867 tree tmpl = TREE_OPERAND (fullname, 0);
25868 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25869 {
25870 /* We get here with a plain identifier because a previous tentative
25871 parse of the nested-name-specifier as part of a ptr-operator saw
25872 ::template X<A>. The use of ::template is necessary in a
25873 ptr-operator, but wrong in a declarator-id.
25874
25875 [temp.names]: In a qualified-id of a declarator-id, the keyword
25876 template shall not appear at the top level. */
25877 pedwarn (cp_expr_loc_or_loc (fullname, input_location), OPT_Wpedantic,
25878 "keyword %<template%> not allowed in declarator-id");
25879 tmpl = decl;
25880 }
25881 tree args = TREE_OPERAND (fullname, 1);
25882 /* Instantiate the template. */
25883 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25884 /*entering_scope=*/true,
25885 tf_error | tf_user);
25886 if (result == error_mark_node)
25887 result = NULL_TREE;
25888 }
25889
25890 /* Leave the SCOPE. */
25891 if (pushed_scope)
25892 pop_scope (pushed_scope);
25893
25894 /* If we failed to resolve it, return the original typename. */
25895 if (!result)
25896 return type;
25897
25898 /* If lookup found a typename type, resolve that too. */
25899 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25900 {
25901 /* Ill-formed programs can cause infinite recursion here, so we
25902 must catch that. */
25903 TYPENAME_IS_RESOLVING_P (result) = 1;
25904 result = resolve_typename_type (result, only_current_p);
25905 TYPENAME_IS_RESOLVING_P (result) = 0;
25906 }
25907
25908 /* Qualify the resulting type. */
25909 quals = cp_type_quals (type);
25910 if (quals)
25911 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25912
25913 return result;
25914 }
25915
25916 /* EXPR is an expression which is not type-dependent. Return a proxy
25917 for EXPR that can be used to compute the types of larger
25918 expressions containing EXPR. */
25919
25920 tree
25921 build_non_dependent_expr (tree expr)
25922 {
25923 tree orig_expr = expr;
25924 tree inner_expr;
25925
25926 /* When checking, try to get a constant value for all non-dependent
25927 expressions in order to expose bugs in *_dependent_expression_p
25928 and constexpr. This can affect code generation, see PR70704, so
25929 only do this for -fchecking=2. */
25930 if (flag_checking > 1
25931 && cxx_dialect >= cxx11
25932 /* Don't do this during nsdmi parsing as it can lead to
25933 unexpected recursive instantiations. */
25934 && !parsing_nsdmi ()
25935 /* Don't do this during concept expansion either and for
25936 the same reason. */
25937 && !expanding_concept ())
25938 fold_non_dependent_expr (expr, tf_none);
25939
25940 STRIP_ANY_LOCATION_WRAPPER (expr);
25941
25942 /* Preserve OVERLOADs; the functions must be available to resolve
25943 types. */
25944 inner_expr = expr;
25945 if (TREE_CODE (inner_expr) == STMT_EXPR)
25946 inner_expr = stmt_expr_value_expr (inner_expr);
25947 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25948 inner_expr = TREE_OPERAND (inner_expr, 0);
25949 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25950 inner_expr = TREE_OPERAND (inner_expr, 1);
25951 if (is_overloaded_fn (inner_expr)
25952 || TREE_CODE (inner_expr) == OFFSET_REF)
25953 return orig_expr;
25954 /* There is no need to return a proxy for a variable or enumerator. */
25955 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
25956 return orig_expr;
25957 /* Preserve string constants; conversions from string constants to
25958 "char *" are allowed, even though normally a "const char *"
25959 cannot be used to initialize a "char *". */
25960 if (TREE_CODE (expr) == STRING_CST)
25961 return orig_expr;
25962 /* Preserve void and arithmetic constants, as an optimization -- there is no
25963 reason to create a new node. */
25964 if (TREE_CODE (expr) == VOID_CST
25965 || TREE_CODE (expr) == INTEGER_CST
25966 || TREE_CODE (expr) == REAL_CST)
25967 return orig_expr;
25968 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25969 There is at least one place where we want to know that a
25970 particular expression is a throw-expression: when checking a ?:
25971 expression, there are special rules if the second or third
25972 argument is a throw-expression. */
25973 if (TREE_CODE (expr) == THROW_EXPR)
25974 return orig_expr;
25975
25976 /* Don't wrap an initializer list, we need to be able to look inside. */
25977 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25978 return orig_expr;
25979
25980 /* Don't wrap a dummy object, we need to be able to test for it. */
25981 if (is_dummy_object (expr))
25982 return orig_expr;
25983
25984 if (TREE_CODE (expr) == COND_EXPR)
25985 return build3 (COND_EXPR,
25986 TREE_TYPE (expr),
25987 TREE_OPERAND (expr, 0),
25988 (TREE_OPERAND (expr, 1)
25989 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25990 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25991 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25992 if (TREE_CODE (expr) == COMPOUND_EXPR
25993 && !COMPOUND_EXPR_OVERLOADED (expr))
25994 return build2 (COMPOUND_EXPR,
25995 TREE_TYPE (expr),
25996 TREE_OPERAND (expr, 0),
25997 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25998
25999 /* If the type is unknown, it can't really be non-dependent */
26000 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
26001
26002 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26003 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
26004 TREE_TYPE (expr), expr);
26005 }
26006
26007 /* ARGS is a vector of expressions as arguments to a function call.
26008 Replace the arguments with equivalent non-dependent expressions.
26009 This modifies ARGS in place. */
26010
26011 void
26012 make_args_non_dependent (vec<tree, va_gc> *args)
26013 {
26014 unsigned int ix;
26015 tree arg;
26016
26017 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
26018 {
26019 tree newarg = build_non_dependent_expr (arg);
26020 if (newarg != arg)
26021 (*args)[ix] = newarg;
26022 }
26023 }
26024
26025 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26026 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26027 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26028
26029 static tree
26030 make_auto_1 (tree name, bool set_canonical)
26031 {
26032 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
26033 TYPE_NAME (au) = build_decl (input_location,
26034 TYPE_DECL, name, au);
26035 TYPE_STUB_DECL (au) = TYPE_NAME (au);
26036 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
26037 (0, processing_template_decl + 1, processing_template_decl + 1,
26038 TYPE_NAME (au), NULL_TREE);
26039 if (set_canonical)
26040 TYPE_CANONICAL (au) = canonical_type_parameter (au);
26041 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
26042 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
26043
26044 return au;
26045 }
26046
26047 tree
26048 make_decltype_auto (void)
26049 {
26050 return make_auto_1 (decltype_auto_identifier, true);
26051 }
26052
26053 tree
26054 make_auto (void)
26055 {
26056 return make_auto_1 (auto_identifier, true);
26057 }
26058
26059 /* Return a C++17 deduction placeholder for class template TMPL. */
26060
26061 tree
26062 make_template_placeholder (tree tmpl)
26063 {
26064 tree t = make_auto_1 (DECL_NAME (tmpl), true);
26065 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
26066 return t;
26067 }
26068
26069 /* True iff T is a C++17 class template deduction placeholder. */
26070
26071 bool
26072 template_placeholder_p (tree t)
26073 {
26074 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
26075 }
26076
26077 /* Make a "constrained auto" type-specifier. This is an
26078 auto type with constraints that must be associated after
26079 deduction. The constraint is formed from the given
26080 CONC and its optional sequence of arguments, which are
26081 non-null if written as partial-concept-id. */
26082
26083 tree
26084 make_constrained_auto (tree con, tree args)
26085 {
26086 tree type = make_auto_1 (auto_identifier, false);
26087
26088 /* Build the constraint. */
26089 tree tmpl = DECL_TI_TEMPLATE (con);
26090 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
26091 expr = build_concept_check (expr, type, args);
26092
26093 tree constr = normalize_expression (expr);
26094 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
26095
26096 /* Our canonical type depends on the constraint. */
26097 TYPE_CANONICAL (type) = canonical_type_parameter (type);
26098
26099 /* Attach the constraint to the type declaration. */
26100 tree decl = TYPE_NAME (type);
26101 return decl;
26102 }
26103
26104 /* Given type ARG, return std::initializer_list<ARG>. */
26105
26106 static tree
26107 listify (tree arg)
26108 {
26109 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
26110
26111 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
26112 {
26113 gcc_rich_location richloc (input_location);
26114 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
26115 error_at (&richloc,
26116 "deducing from brace-enclosed initializer list"
26117 " requires %<#include <initializer_list>%>");
26118
26119 return error_mark_node;
26120 }
26121 tree argvec = make_tree_vec (1);
26122 TREE_VEC_ELT (argvec, 0) = arg;
26123
26124 return lookup_template_class (std_init_list, argvec, NULL_TREE,
26125 NULL_TREE, 0, tf_warning_or_error);
26126 }
26127
26128 /* Replace auto in TYPE with std::initializer_list<auto>. */
26129
26130 static tree
26131 listify_autos (tree type, tree auto_node)
26132 {
26133 tree init_auto = listify (auto_node);
26134 tree argvec = make_tree_vec (1);
26135 TREE_VEC_ELT (argvec, 0) = init_auto;
26136 if (processing_template_decl)
26137 argvec = add_to_template_args (current_template_args (), argvec);
26138 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
26139 }
26140
26141 /* Hash traits for hashing possibly constrained 'auto'
26142 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26143
26144 struct auto_hash : default_hash_traits<tree>
26145 {
26146 static inline hashval_t hash (tree);
26147 static inline bool equal (tree, tree);
26148 };
26149
26150 /* Hash the 'auto' T. */
26151
26152 inline hashval_t
26153 auto_hash::hash (tree t)
26154 {
26155 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
26156 /* Matching constrained-type-specifiers denote the same template
26157 parameter, so hash the constraint. */
26158 return hash_placeholder_constraint (c);
26159 else
26160 /* But unconstrained autos are all separate, so just hash the pointer. */
26161 return iterative_hash_object (t, 0);
26162 }
26163
26164 /* Compare two 'auto's. */
26165
26166 inline bool
26167 auto_hash::equal (tree t1, tree t2)
26168 {
26169 if (t1 == t2)
26170 return true;
26171
26172 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
26173 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
26174
26175 /* Two unconstrained autos are distinct. */
26176 if (!c1 || !c2)
26177 return false;
26178
26179 return equivalent_placeholder_constraints (c1, c2);
26180 }
26181
26182 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26183 constrained) auto, add it to the vector. */
26184
26185 static int
26186 extract_autos_r (tree t, void *data)
26187 {
26188 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
26189 if (is_auto (t))
26190 {
26191 /* All the autos were built with index 0; fix that up now. */
26192 tree *p = hash.find_slot (t, INSERT);
26193 unsigned idx;
26194 if (*p)
26195 /* If this is a repeated constrained-type-specifier, use the index we
26196 chose before. */
26197 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
26198 else
26199 {
26200 /* Otherwise this is new, so use the current count. */
26201 *p = t;
26202 idx = hash.elements () - 1;
26203 }
26204 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
26205 }
26206
26207 /* Always keep walking. */
26208 return 0;
26209 }
26210
26211 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26212 says they can appear anywhere in the type. */
26213
26214 static tree
26215 extract_autos (tree type)
26216 {
26217 hash_set<tree> visited;
26218 hash_table<auto_hash> hash (2);
26219
26220 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
26221
26222 tree tree_vec = make_tree_vec (hash.elements());
26223 for (hash_table<auto_hash>::iterator iter = hash.begin();
26224 iter != hash.end(); ++iter)
26225 {
26226 tree elt = *iter;
26227 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
26228 TREE_VEC_ELT (tree_vec, i)
26229 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
26230 }
26231
26232 return tree_vec;
26233 }
26234
26235 /* The stem for deduction guide names. */
26236 const char *const dguide_base = "__dguide_";
26237
26238 /* Return the name for a deduction guide for class template TMPL. */
26239
26240 tree
26241 dguide_name (tree tmpl)
26242 {
26243 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
26244 tree tname = TYPE_IDENTIFIER (type);
26245 char *buf = (char *) alloca (1 + strlen (dguide_base)
26246 + IDENTIFIER_LENGTH (tname));
26247 memcpy (buf, dguide_base, strlen (dguide_base));
26248 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
26249 IDENTIFIER_LENGTH (tname) + 1);
26250 tree dname = get_identifier (buf);
26251 TREE_TYPE (dname) = type;
26252 return dname;
26253 }
26254
26255 /* True if NAME is the name of a deduction guide. */
26256
26257 bool
26258 dguide_name_p (tree name)
26259 {
26260 return (TREE_CODE (name) == IDENTIFIER_NODE
26261 && TREE_TYPE (name)
26262 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
26263 strlen (dguide_base)));
26264 }
26265
26266 /* True if FN is a deduction guide. */
26267
26268 bool
26269 deduction_guide_p (const_tree fn)
26270 {
26271 if (DECL_P (fn))
26272 if (tree name = DECL_NAME (fn))
26273 return dguide_name_p (name);
26274 return false;
26275 }
26276
26277 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26278
26279 bool
26280 copy_guide_p (const_tree fn)
26281 {
26282 gcc_assert (deduction_guide_p (fn));
26283 if (!DECL_ARTIFICIAL (fn))
26284 return false;
26285 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
26286 return (TREE_CHAIN (parms) == void_list_node
26287 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
26288 }
26289
26290 /* True if FN is a guide generated from a constructor template. */
26291
26292 bool
26293 template_guide_p (const_tree fn)
26294 {
26295 gcc_assert (deduction_guide_p (fn));
26296 if (!DECL_ARTIFICIAL (fn))
26297 return false;
26298 tree tmpl = DECL_TI_TEMPLATE (fn);
26299 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
26300 return PRIMARY_TEMPLATE_P (org);
26301 return false;
26302 }
26303
26304 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
26305 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
26306 template parameter types. Note that the handling of template template
26307 parameters relies on current_template_parms being set appropriately for the
26308 new template. */
26309
26310 static tree
26311 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
26312 tree tsubst_args, tsubst_flags_t complain)
26313 {
26314 if (olddecl == error_mark_node)
26315 return error_mark_node;
26316
26317 tree oldidx = get_template_parm_index (olddecl);
26318
26319 tree newtype;
26320 if (TREE_CODE (olddecl) == TYPE_DECL
26321 || TREE_CODE (olddecl) == TEMPLATE_DECL)
26322 {
26323 tree oldtype = TREE_TYPE (olddecl);
26324 newtype = cxx_make_type (TREE_CODE (oldtype));
26325 TYPE_MAIN_VARIANT (newtype) = newtype;
26326 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
26327 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
26328 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
26329 }
26330 else
26331 {
26332 newtype = TREE_TYPE (olddecl);
26333 if (type_uses_auto (newtype))
26334 {
26335 // Substitute once to fix references to other template parameters.
26336 newtype = tsubst (newtype, tsubst_args,
26337 complain|tf_partial, NULL_TREE);
26338 // Now substitute again to reduce the level of the auto.
26339 newtype = tsubst (newtype, current_template_args (),
26340 complain, NULL_TREE);
26341 }
26342 else
26343 newtype = tsubst (newtype, tsubst_args,
26344 complain, NULL_TREE);
26345 }
26346
26347 tree newdecl
26348 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
26349 DECL_NAME (olddecl), newtype);
26350 SET_DECL_TEMPLATE_PARM_P (newdecl);
26351
26352 tree newidx;
26353 if (TREE_CODE (olddecl) == TYPE_DECL
26354 || TREE_CODE (olddecl) == TEMPLATE_DECL)
26355 {
26356 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
26357 = build_template_parm_index (index, level, level,
26358 newdecl, newtype);
26359 TEMPLATE_PARM_PARAMETER_PACK (newidx)
26360 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
26361 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
26362 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
26363
26364 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
26365 {
26366 DECL_TEMPLATE_RESULT (newdecl)
26367 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
26368 DECL_NAME (olddecl), newtype);
26369 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
26370 // First create a copy (ttargs) of tsubst_args with an
26371 // additional level for the template template parameter's own
26372 // template parameters (ttparms).
26373 tree ttparms = (INNERMOST_TEMPLATE_PARMS
26374 (DECL_TEMPLATE_PARMS (olddecl)));
26375 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
26376 tree ttargs = make_tree_vec (depth + 1);
26377 for (int i = 0; i < depth; ++i)
26378 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
26379 TREE_VEC_ELT (ttargs, depth)
26380 = template_parms_level_to_args (ttparms);
26381 // Substitute ttargs into ttparms to fix references to
26382 // other template parameters.
26383 ttparms = tsubst_template_parms_level (ttparms, ttargs,
26384 complain|tf_partial);
26385 // Now substitute again with args based on tparms, to reduce
26386 // the level of the ttparms.
26387 ttargs = current_template_args ();
26388 ttparms = tsubst_template_parms_level (ttparms, ttargs,
26389 complain);
26390 // Finally, tack the adjusted parms onto tparms.
26391 ttparms = tree_cons (size_int (depth), ttparms,
26392 current_template_parms);
26393 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
26394 }
26395 }
26396 else
26397 {
26398 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
26399 tree newconst
26400 = build_decl (DECL_SOURCE_LOCATION (oldconst),
26401 TREE_CODE (oldconst),
26402 DECL_NAME (oldconst), newtype);
26403 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
26404 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
26405 SET_DECL_TEMPLATE_PARM_P (newconst);
26406 newidx = build_template_parm_index (index, level, level,
26407 newconst, newtype);
26408 TEMPLATE_PARM_PARAMETER_PACK (newidx)
26409 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
26410 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
26411 }
26412
26413 return newdecl;
26414 }
26415
26416 /* Returns a C++17 class deduction guide template based on the constructor
26417 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
26418 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
26419
26420 static tree
26421 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
26422 {
26423 tree type, tparms, targs, fparms, fargs, ci;
26424 bool memtmpl = false;
26425 bool explicit_p;
26426 location_t loc;
26427 tree fn_tmpl = NULL_TREE;
26428
26429 if (TYPE_P (ctor))
26430 {
26431 type = ctor;
26432 bool copy_p = TYPE_REF_P (type);
26433 if (copy_p)
26434 {
26435 type = TREE_TYPE (type);
26436 fparms = tree_cons (NULL_TREE, type, void_list_node);
26437 }
26438 else
26439 fparms = void_list_node;
26440
26441 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
26442 tparms = DECL_TEMPLATE_PARMS (ctmpl);
26443 targs = CLASSTYPE_TI_ARGS (type);
26444 ci = NULL_TREE;
26445 fargs = NULL_TREE;
26446 loc = DECL_SOURCE_LOCATION (ctmpl);
26447 explicit_p = false;
26448 }
26449 else
26450 {
26451 ++processing_template_decl;
26452 bool ok = true;
26453
26454 fn_tmpl
26455 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
26456 : DECL_TI_TEMPLATE (ctor));
26457 if (outer_args)
26458 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
26459 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
26460
26461 type = DECL_CONTEXT (ctor);
26462
26463 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
26464 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
26465 fully specialized args for the enclosing class. Strip those off, as
26466 the deduction guide won't have those template parameters. */
26467 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
26468 TMPL_PARMS_DEPTH (tparms));
26469 /* Discard the 'this' parameter. */
26470 fparms = FUNCTION_ARG_CHAIN (ctor);
26471 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
26472 ci = get_constraints (ctor);
26473 loc = DECL_SOURCE_LOCATION (ctor);
26474 explicit_p = DECL_NONCONVERTING_P (ctor);
26475
26476 if (PRIMARY_TEMPLATE_P (fn_tmpl))
26477 {
26478 memtmpl = true;
26479
26480 /* For a member template constructor, we need to flatten the two
26481 template parameter lists into one, and then adjust the function
26482 signature accordingly. This gets...complicated. */
26483 tree save_parms = current_template_parms;
26484
26485 /* For a member template we should have two levels of parms/args, one
26486 for the class and one for the constructor. We stripped
26487 specialized args for further enclosing classes above. */
26488 const int depth = 2;
26489 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
26490
26491 /* Template args for translating references to the two-level template
26492 parameters into references to the one-level template parameters we
26493 are creating. */
26494 tree tsubst_args = copy_node (targs);
26495 TMPL_ARGS_LEVEL (tsubst_args, depth)
26496 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
26497
26498 /* Template parms for the constructor template. */
26499 tree ftparms = TREE_VALUE (tparms);
26500 unsigned flen = TREE_VEC_LENGTH (ftparms);
26501 /* Template parms for the class template. */
26502 tparms = TREE_CHAIN (tparms);
26503 tree ctparms = TREE_VALUE (tparms);
26504 unsigned clen = TREE_VEC_LENGTH (ctparms);
26505 /* Template parms for the deduction guide start as a copy of the
26506 template parms for the class. We set current_template_parms for
26507 lookup_template_class_1. */
26508 current_template_parms = tparms = copy_node (tparms);
26509 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
26510 for (unsigned i = 0; i < clen; ++i)
26511 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
26512
26513 /* Now we need to rewrite the constructor parms to append them to the
26514 class parms. */
26515 for (unsigned i = 0; i < flen; ++i)
26516 {
26517 unsigned index = i + clen;
26518 unsigned level = 1;
26519 tree oldelt = TREE_VEC_ELT (ftparms, i);
26520 tree olddecl = TREE_VALUE (oldelt);
26521 tree newdecl = rewrite_template_parm (olddecl, index, level,
26522 tsubst_args, complain);
26523 if (newdecl == error_mark_node)
26524 ok = false;
26525 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
26526 tsubst_args, complain, ctor);
26527 tree list = build_tree_list (newdef, newdecl);
26528 TEMPLATE_PARM_CONSTRAINTS (list)
26529 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
26530 tsubst_args, complain, ctor);
26531 TREE_VEC_ELT (new_vec, index) = list;
26532 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
26533 }
26534
26535 /* Now we have a final set of template parms to substitute into the
26536 function signature. */
26537 targs = template_parms_to_args (tparms);
26538 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
26539 complain, ctor);
26540 fargs = tsubst (fargs, tsubst_args, complain, ctor);
26541 if (ci)
26542 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
26543
26544 current_template_parms = save_parms;
26545 }
26546
26547 --processing_template_decl;
26548 if (!ok)
26549 return error_mark_node;
26550 }
26551
26552 if (!memtmpl)
26553 {
26554 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
26555 tparms = copy_node (tparms);
26556 INNERMOST_TEMPLATE_PARMS (tparms)
26557 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
26558 }
26559
26560 tree fntype = build_function_type (type, fparms);
26561 tree ded_fn = build_lang_decl_loc (loc,
26562 FUNCTION_DECL,
26563 dguide_name (type), fntype);
26564 DECL_ARGUMENTS (ded_fn) = fargs;
26565 DECL_ARTIFICIAL (ded_fn) = true;
26566 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
26567 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
26568 DECL_ARTIFICIAL (ded_tmpl) = true;
26569 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
26570 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
26571 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
26572 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
26573 if (DECL_P (ctor))
26574 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
26575 if (ci)
26576 set_constraints (ded_tmpl, ci);
26577
26578 return ded_tmpl;
26579 }
26580
26581 /* Deduce template arguments for the class template placeholder PTYPE for
26582 template TMPL based on the initializer INIT, and return the resulting
26583 type. */
26584
26585 static tree
26586 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
26587 tsubst_flags_t complain)
26588 {
26589 if (!DECL_CLASS_TEMPLATE_P (tmpl))
26590 {
26591 /* We should have handled this in the caller. */
26592 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26593 return ptype;
26594 if (complain & tf_error)
26595 error ("non-class template %qT used without template arguments", tmpl);
26596 return error_mark_node;
26597 }
26598
26599 tree type = TREE_TYPE (tmpl);
26600
26601 bool try_list_ctor = false;
26602
26603 vec<tree,va_gc> *args;
26604 if (init == NULL_TREE
26605 || TREE_CODE (init) == TREE_LIST)
26606 args = make_tree_vector_from_list (init);
26607 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
26608 {
26609 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
26610 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
26611 {
26612 /* As an exception, the first phase in 16.3.1.7 (considering the
26613 initializer list as a single argument) is omitted if the
26614 initializer list consists of a single expression of type cv U,
26615 where U is a specialization of C or a class derived from a
26616 specialization of C. */
26617 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
26618 tree etype = TREE_TYPE (elt);
26619
26620 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
26621 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26622 int err = unify (tparms, targs, type, etype,
26623 UNIFY_ALLOW_DERIVED, /*explain*/false);
26624 if (err == 0)
26625 try_list_ctor = false;
26626 ggc_free (targs);
26627 }
26628 if (try_list_ctor || is_std_init_list (type))
26629 args = make_tree_vector_single (init);
26630 else
26631 args = make_tree_vector_from_ctor (init);
26632 }
26633 else
26634 args = make_tree_vector_single (init);
26635
26636 tree dname = dguide_name (tmpl);
26637 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
26638 /*type*/false, /*complain*/false,
26639 /*hidden*/false);
26640 bool elided = false;
26641 if (cands == error_mark_node)
26642 cands = NULL_TREE;
26643
26644 /* Prune explicit deduction guides in copy-initialization context. */
26645 if (flags & LOOKUP_ONLYCONVERTING)
26646 {
26647 for (lkp_iterator iter (cands); !elided && iter; ++iter)
26648 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26649 elided = true;
26650
26651 if (elided)
26652 {
26653 /* Found a nonconverting guide, prune the candidates. */
26654 tree pruned = NULL_TREE;
26655 for (lkp_iterator iter (cands); iter; ++iter)
26656 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26657 pruned = lookup_add (*iter, pruned);
26658
26659 cands = pruned;
26660 }
26661 }
26662
26663 tree outer_args = NULL_TREE;
26664 if (DECL_CLASS_SCOPE_P (tmpl)
26665 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
26666 {
26667 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
26668 type = TREE_TYPE (most_general_template (tmpl));
26669 }
26670
26671 bool saw_ctor = false;
26672 // FIXME cache artificial deduction guides
26673 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
26674 {
26675 /* Skip inherited constructors. */
26676 if (iter.using_p ())
26677 continue;
26678
26679 tree guide = build_deduction_guide (*iter, outer_args, complain);
26680 if (guide == error_mark_node)
26681 return error_mark_node;
26682 if ((flags & LOOKUP_ONLYCONVERTING)
26683 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
26684 elided = true;
26685 else
26686 cands = lookup_add (guide, cands);
26687
26688 saw_ctor = true;
26689 }
26690
26691 tree call = error_mark_node;
26692
26693 /* If this is list-initialization and the class has a list constructor, first
26694 try deducing from the list as a single argument, as [over.match.list]. */
26695 tree list_cands = NULL_TREE;
26696 if (try_list_ctor && cands)
26697 for (lkp_iterator iter (cands); iter; ++iter)
26698 {
26699 tree dg = *iter;
26700 if (is_list_ctor (dg))
26701 list_cands = lookup_add (dg, list_cands);
26702 }
26703 if (list_cands)
26704 {
26705 ++cp_unevaluated_operand;
26706 call = build_new_function_call (list_cands, &args, tf_decltype);
26707 --cp_unevaluated_operand;
26708
26709 if (call == error_mark_node)
26710 {
26711 /* That didn't work, now try treating the list as a sequence of
26712 arguments. */
26713 release_tree_vector (args);
26714 args = make_tree_vector_from_ctor (init);
26715 }
26716 }
26717
26718 /* Maybe generate an implicit deduction guide. */
26719 if (call == error_mark_node && args->length () < 2)
26720 {
26721 tree gtype = NULL_TREE;
26722
26723 if (args->length () == 1)
26724 /* Generate a copy guide. */
26725 gtype = build_reference_type (type);
26726 else if (!saw_ctor)
26727 /* Generate a default guide. */
26728 gtype = type;
26729
26730 if (gtype)
26731 {
26732 tree guide = build_deduction_guide (gtype, outer_args, complain);
26733 if (guide == error_mark_node)
26734 return error_mark_node;
26735 cands = lookup_add (guide, cands);
26736 }
26737 }
26738
26739 if (elided && !cands)
26740 {
26741 error ("cannot deduce template arguments for copy-initialization"
26742 " of %qT, as it has no non-explicit deduction guides or "
26743 "user-declared constructors", type);
26744 return error_mark_node;
26745 }
26746 else if (!cands && call == error_mark_node)
26747 {
26748 error ("cannot deduce template arguments of %qT, as it has no viable "
26749 "deduction guides", type);
26750 return error_mark_node;
26751 }
26752
26753 if (call == error_mark_node)
26754 {
26755 ++cp_unevaluated_operand;
26756 call = build_new_function_call (cands, &args, tf_decltype);
26757 --cp_unevaluated_operand;
26758 }
26759
26760 if (call == error_mark_node && (complain & tf_warning_or_error))
26761 {
26762 error ("class template argument deduction failed:");
26763
26764 ++cp_unevaluated_operand;
26765 call = build_new_function_call (cands, &args, complain | tf_decltype);
26766 --cp_unevaluated_operand;
26767
26768 if (elided)
26769 inform (input_location, "explicit deduction guides not considered "
26770 "for copy-initialization");
26771 }
26772
26773 release_tree_vector (args);
26774
26775 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
26776 }
26777
26778 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26779 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26780 The CONTEXT determines the context in which auto deduction is performed
26781 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26782 OUTER_TARGS are used during template argument deduction
26783 (context == adc_unify) to properly substitute the result, and is ignored
26784 in other contexts.
26785
26786 For partial-concept-ids, extra args may be appended to the list of deduced
26787 template arguments prior to determining constraint satisfaction. */
26788
26789 tree
26790 do_auto_deduction (tree type, tree init, tree auto_node,
26791 tsubst_flags_t complain, auto_deduction_context context,
26792 tree outer_targs, int flags)
26793 {
26794 tree targs;
26795
26796 if (init == error_mark_node)
26797 return error_mark_node;
26798
26799 if (init && type_dependent_expression_p (init)
26800 && context != adc_unify)
26801 /* Defining a subset of type-dependent expressions that we can deduce
26802 from ahead of time isn't worth the trouble. */
26803 return type;
26804
26805 /* Similarly, we can't deduce from another undeduced decl. */
26806 if (init && undeduced_auto_decl (init))
26807 return type;
26808
26809 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
26810 /* C++17 class template argument deduction. */
26811 return do_class_deduction (type, tmpl, init, flags, complain);
26812
26813 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
26814 /* Nothing we can do with this, even in deduction context. */
26815 return type;
26816
26817 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26818 with either a new invented type template parameter U or, if the
26819 initializer is a braced-init-list (8.5.4), with
26820 std::initializer_list<U>. */
26821 if (BRACE_ENCLOSED_INITIALIZER_P (init))
26822 {
26823 if (!DIRECT_LIST_INIT_P (init))
26824 type = listify_autos (type, auto_node);
26825 else if (CONSTRUCTOR_NELTS (init) == 1)
26826 init = CONSTRUCTOR_ELT (init, 0)->value;
26827 else
26828 {
26829 if (complain & tf_warning_or_error)
26830 {
26831 if (permerror (input_location, "direct-list-initialization of "
26832 "%<auto%> requires exactly one element"))
26833 inform (input_location,
26834 "for deduction to %<std::initializer_list%>, use copy-"
26835 "list-initialization (i.e. add %<=%> before the %<{%>)");
26836 }
26837 type = listify_autos (type, auto_node);
26838 }
26839 }
26840
26841 if (type == error_mark_node)
26842 return error_mark_node;
26843
26844 init = resolve_nondeduced_context (init, complain);
26845
26846 if (context == adc_decomp_type
26847 && auto_node == type
26848 && init != error_mark_node
26849 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
26850 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26851 and initializer has array type, deduce cv-qualified array type. */
26852 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
26853 complain);
26854 else if (AUTO_IS_DECLTYPE (auto_node))
26855 {
26856 bool id = (DECL_P (init)
26857 || ((TREE_CODE (init) == COMPONENT_REF
26858 || TREE_CODE (init) == SCOPE_REF)
26859 && !REF_PARENTHESIZED_P (init)));
26860 targs = make_tree_vec (1);
26861 TREE_VEC_ELT (targs, 0)
26862 = finish_decltype_type (init, id, tf_warning_or_error);
26863 if (type != auto_node)
26864 {
26865 if (complain & tf_error)
26866 error ("%qT as type rather than plain %<decltype(auto)%>", type);
26867 return error_mark_node;
26868 }
26869 }
26870 else
26871 {
26872 tree parms = build_tree_list (NULL_TREE, type);
26873 tree tparms;
26874
26875 if (flag_concepts)
26876 tparms = extract_autos (type);
26877 else
26878 {
26879 tparms = make_tree_vec (1);
26880 TREE_VEC_ELT (tparms, 0)
26881 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
26882 }
26883
26884 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26885 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
26886 DEDUCE_CALL,
26887 NULL, /*explain_p=*/false);
26888 if (val > 0)
26889 {
26890 if (processing_template_decl)
26891 /* Try again at instantiation time. */
26892 return type;
26893 if (type && type != error_mark_node
26894 && (complain & tf_error))
26895 /* If type is error_mark_node a diagnostic must have been
26896 emitted by now. Also, having a mention to '<type error>'
26897 in the diagnostic is not really useful to the user. */
26898 {
26899 if (cfun && auto_node == current_function_auto_return_pattern
26900 && LAMBDA_FUNCTION_P (current_function_decl))
26901 error ("unable to deduce lambda return type from %qE", init);
26902 else
26903 error ("unable to deduce %qT from %qE", type, init);
26904 type_unification_real (tparms, targs, parms, &init, 1, 0,
26905 DEDUCE_CALL,
26906 NULL, /*explain_p=*/true);
26907 }
26908 return error_mark_node;
26909 }
26910 }
26911
26912 /* Check any placeholder constraints against the deduced type. */
26913 if (flag_concepts && !processing_template_decl)
26914 if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
26915 {
26916 /* Use the deduced type to check the associated constraints. If we
26917 have a partial-concept-id, rebuild the argument list so that
26918 we check using the extra arguments. */
26919 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26920 tree cargs = CHECK_CONSTR_ARGS (constr);
26921 if (TREE_VEC_LENGTH (cargs) > 1)
26922 {
26923 cargs = copy_node (cargs);
26924 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26925 }
26926 else
26927 cargs = targs;
26928 if (!constraints_satisfied_p (constr, cargs))
26929 {
26930 if (complain & tf_warning_or_error)
26931 {
26932 auto_diagnostic_group d;
26933 switch (context)
26934 {
26935 case adc_unspecified:
26936 case adc_unify:
26937 error("placeholder constraints not satisfied");
26938 break;
26939 case adc_variable_type:
26940 case adc_decomp_type:
26941 error ("deduced initializer does not satisfy "
26942 "placeholder constraints");
26943 break;
26944 case adc_return_type:
26945 error ("deduced return type does not satisfy "
26946 "placeholder constraints");
26947 break;
26948 case adc_requirement:
26949 error ("deduced expression type does not satisfy "
26950 "placeholder constraints");
26951 break;
26952 }
26953 diagnose_constraints (input_location, constr, targs);
26954 }
26955 return error_mark_node;
26956 }
26957 }
26958
26959 if (processing_template_decl && context != adc_unify)
26960 outer_targs = current_template_args ();
26961 targs = add_to_template_args (outer_targs, targs);
26962 return tsubst (type, targs, complain, NULL_TREE);
26963 }
26964
26965 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26966 result. */
26967
26968 tree
26969 splice_late_return_type (tree type, tree late_return_type)
26970 {
26971 if (is_auto (type))
26972 {
26973 if (late_return_type)
26974 return late_return_type;
26975
26976 tree idx = get_template_parm_index (type);
26977 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26978 /* In an abbreviated function template we didn't know we were dealing
26979 with a function template when we saw the auto return type, so update
26980 it to have the correct level. */
26981 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26982 }
26983 return type;
26984 }
26985
26986 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26987 'decltype(auto)' or a deduced class template. */
26988
26989 bool
26990 is_auto (const_tree type)
26991 {
26992 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26993 && (TYPE_IDENTIFIER (type) == auto_identifier
26994 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26995 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26996 return true;
26997 else
26998 return false;
26999 }
27000
27001 /* for_each_template_parm callback for type_uses_auto. */
27002
27003 int
27004 is_auto_r (tree tp, void */*data*/)
27005 {
27006 return is_auto (tp);
27007 }
27008
27009 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27010 a use of `auto'. Returns NULL_TREE otherwise. */
27011
27012 tree
27013 type_uses_auto (tree type)
27014 {
27015 if (type == NULL_TREE)
27016 return NULL_TREE;
27017 else if (flag_concepts)
27018 {
27019 /* The Concepts TS allows multiple autos in one type-specifier; just
27020 return the first one we find, do_auto_deduction will collect all of
27021 them. */
27022 if (uses_template_parms (type))
27023 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
27024 /*visited*/NULL, /*nondeduced*/false);
27025 else
27026 return NULL_TREE;
27027 }
27028 else
27029 return find_type_usage (type, is_auto);
27030 }
27031
27032 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27033 concepts are enabled, auto is acceptable in template arguments, but
27034 only when TEMPL identifies a template class. Return TRUE if any
27035 such errors were reported. */
27036
27037 bool
27038 check_auto_in_tmpl_args (tree tmpl, tree args)
27039 {
27040 /* If there were previous errors, nevermind. */
27041 if (!args || TREE_CODE (args) != TREE_VEC)
27042 return false;
27043
27044 /* If TMPL is an identifier, we're parsing and we can't tell yet
27045 whether TMPL is supposed to be a type, a function or a variable.
27046 We'll only be able to tell during template substitution, so we
27047 expect to be called again then. If concepts are enabled and we
27048 know we have a type, we're ok. */
27049 if (flag_concepts
27050 && (identifier_p (tmpl)
27051 || (DECL_P (tmpl)
27052 && (DECL_TYPE_TEMPLATE_P (tmpl)
27053 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
27054 return false;
27055
27056 /* Quickly search for any occurrences of auto; usually there won't
27057 be any, and then we'll avoid allocating the vector. */
27058 if (!type_uses_auto (args))
27059 return false;
27060
27061 bool errors = false;
27062
27063 tree vec = extract_autos (args);
27064 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
27065 {
27066 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
27067 error_at (DECL_SOURCE_LOCATION (xauto),
27068 "invalid use of %qT in template argument", xauto);
27069 errors = true;
27070 }
27071
27072 return errors;
27073 }
27074
27075 /* For a given template T, return the vector of typedefs referenced
27076 in T for which access check is needed at T instantiation time.
27077 T is either a FUNCTION_DECL or a RECORD_TYPE.
27078 Those typedefs were added to T by the function
27079 append_type_to_template_for_access_check. */
27080
27081 vec<qualified_typedef_usage_t, va_gc> *
27082 get_types_needing_access_check (tree t)
27083 {
27084 tree ti;
27085 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
27086
27087 if (!t || t == error_mark_node)
27088 return NULL;
27089
27090 if (!(ti = get_template_info (t)))
27091 return NULL;
27092
27093 if (CLASS_TYPE_P (t)
27094 || TREE_CODE (t) == FUNCTION_DECL)
27095 {
27096 if (!TI_TEMPLATE (ti))
27097 return NULL;
27098
27099 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
27100 }
27101
27102 return result;
27103 }
27104
27105 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27106 tied to T. That list of typedefs will be access checked at
27107 T instantiation time.
27108 T is either a FUNCTION_DECL or a RECORD_TYPE.
27109 TYPE_DECL is a TYPE_DECL node representing a typedef.
27110 SCOPE is the scope through which TYPE_DECL is accessed.
27111 LOCATION is the location of the usage point of TYPE_DECL.
27112
27113 This function is a subroutine of
27114 append_type_to_template_for_access_check. */
27115
27116 static void
27117 append_type_to_template_for_access_check_1 (tree t,
27118 tree type_decl,
27119 tree scope,
27120 location_t location)
27121 {
27122 qualified_typedef_usage_t typedef_usage;
27123 tree ti;
27124
27125 if (!t || t == error_mark_node)
27126 return;
27127
27128 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
27129 || CLASS_TYPE_P (t))
27130 && type_decl
27131 && TREE_CODE (type_decl) == TYPE_DECL
27132 && scope);
27133
27134 if (!(ti = get_template_info (t)))
27135 return;
27136
27137 gcc_assert (TI_TEMPLATE (ti));
27138
27139 typedef_usage.typedef_decl = type_decl;
27140 typedef_usage.context = scope;
27141 typedef_usage.locus = location;
27142
27143 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
27144 }
27145
27146 /* Append TYPE_DECL to the template TEMPL.
27147 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27148 At TEMPL instanciation time, TYPE_DECL will be checked to see
27149 if it can be accessed through SCOPE.
27150 LOCATION is the location of the usage point of TYPE_DECL.
27151
27152 e.g. consider the following code snippet:
27153
27154 class C
27155 {
27156 typedef int myint;
27157 };
27158
27159 template<class U> struct S
27160 {
27161 C::myint mi; // <-- usage point of the typedef C::myint
27162 };
27163
27164 S<char> s;
27165
27166 At S<char> instantiation time, we need to check the access of C::myint
27167 In other words, we need to check the access of the myint typedef through
27168 the C scope. For that purpose, this function will add the myint typedef
27169 and the scope C through which its being accessed to a list of typedefs
27170 tied to the template S. That list will be walked at template instantiation
27171 time and access check performed on each typedefs it contains.
27172 Note that this particular code snippet should yield an error because
27173 myint is private to C. */
27174
27175 void
27176 append_type_to_template_for_access_check (tree templ,
27177 tree type_decl,
27178 tree scope,
27179 location_t location)
27180 {
27181 qualified_typedef_usage_t *iter;
27182 unsigned i;
27183
27184 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
27185
27186 /* Make sure we don't append the type to the template twice. */
27187 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
27188 if (iter->typedef_decl == type_decl && scope == iter->context)
27189 return;
27190
27191 append_type_to_template_for_access_check_1 (templ, type_decl,
27192 scope, location);
27193 }
27194
27195 /* Convert the generic type parameters in PARM that match the types given in the
27196 range [START_IDX, END_IDX) from the current_template_parms into generic type
27197 packs. */
27198
27199 tree
27200 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
27201 {
27202 tree current = current_template_parms;
27203 int depth = TMPL_PARMS_DEPTH (current);
27204 current = INNERMOST_TEMPLATE_PARMS (current);
27205 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
27206
27207 for (int i = 0; i < start_idx; ++i)
27208 TREE_VEC_ELT (replacement, i)
27209 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27210
27211 for (int i = start_idx; i < end_idx; ++i)
27212 {
27213 /* Create a distinct parameter pack type from the current parm and add it
27214 to the replacement args to tsubst below into the generic function
27215 parameter. */
27216
27217 tree o = TREE_TYPE (TREE_VALUE
27218 (TREE_VEC_ELT (current, i)));
27219 tree t = copy_type (o);
27220 TEMPLATE_TYPE_PARM_INDEX (t)
27221 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
27222 o, 0, 0, tf_none);
27223 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
27224 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
27225 TYPE_MAIN_VARIANT (t) = t;
27226 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
27227 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27228 TREE_VEC_ELT (replacement, i) = t;
27229 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
27230 }
27231
27232 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
27233 TREE_VEC_ELT (replacement, i)
27234 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27235
27236 /* If there are more levels then build up the replacement with the outer
27237 template parms. */
27238 if (depth > 1)
27239 replacement = add_to_template_args (template_parms_to_args
27240 (TREE_CHAIN (current_template_parms)),
27241 replacement);
27242
27243 return tsubst (parm, replacement, tf_none, NULL_TREE);
27244 }
27245
27246 /* Entries in the decl_constraint hash table. */
27247 struct GTY((for_user)) constr_entry
27248 {
27249 tree decl;
27250 tree ci;
27251 };
27252
27253 /* Hashing function and equality for constraint entries. */
27254 struct constr_hasher : ggc_ptr_hash<constr_entry>
27255 {
27256 static hashval_t hash (constr_entry *e)
27257 {
27258 return (hashval_t)DECL_UID (e->decl);
27259 }
27260
27261 static bool equal (constr_entry *e1, constr_entry *e2)
27262 {
27263 return e1->decl == e2->decl;
27264 }
27265 };
27266
27267 /* A mapping from declarations to constraint information. Note that
27268 both templates and their underlying declarations are mapped to the
27269 same constraint information.
27270
27271 FIXME: This is defined in pt.c because garbage collection
27272 code is not being generated for constraint.cc. */
27273
27274 static GTY (()) hash_table<constr_hasher> *decl_constraints;
27275
27276 /* Returns the template constraints of declaration T. If T is not
27277 constrained, return NULL_TREE. Note that T must be non-null. */
27278
27279 tree
27280 get_constraints (tree t)
27281 {
27282 if (!flag_concepts)
27283 return NULL_TREE;
27284
27285 gcc_assert (DECL_P (t));
27286 if (TREE_CODE (t) == TEMPLATE_DECL)
27287 t = DECL_TEMPLATE_RESULT (t);
27288 constr_entry elt = { t, NULL_TREE };
27289 constr_entry* found = decl_constraints->find (&elt);
27290 if (found)
27291 return found->ci;
27292 else
27293 return NULL_TREE;
27294 }
27295
27296 /* Associate the given constraint information CI with the declaration
27297 T. If T is a template, then the constraints are associated with
27298 its underlying declaration. Don't build associations if CI is
27299 NULL_TREE. */
27300
27301 void
27302 set_constraints (tree t, tree ci)
27303 {
27304 if (!ci)
27305 return;
27306 gcc_assert (t && flag_concepts);
27307 if (TREE_CODE (t) == TEMPLATE_DECL)
27308 t = DECL_TEMPLATE_RESULT (t);
27309 gcc_assert (!get_constraints (t));
27310 constr_entry elt = {t, ci};
27311 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
27312 constr_entry* entry = ggc_alloc<constr_entry> ();
27313 *entry = elt;
27314 *slot = entry;
27315 }
27316
27317 /* Remove the associated constraints of the declaration T. */
27318
27319 void
27320 remove_constraints (tree t)
27321 {
27322 gcc_assert (DECL_P (t));
27323 if (TREE_CODE (t) == TEMPLATE_DECL)
27324 t = DECL_TEMPLATE_RESULT (t);
27325
27326 constr_entry elt = {t, NULL_TREE};
27327 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
27328 if (slot)
27329 decl_constraints->clear_slot (slot);
27330 }
27331
27332 /* Memoized satisfaction results for declarations. This
27333 maps the pair (constraint_info, arguments) to the result computed
27334 by constraints_satisfied_p. */
27335
27336 struct GTY((for_user)) constraint_sat_entry
27337 {
27338 tree ci;
27339 tree args;
27340 tree result;
27341 };
27342
27343 /* Hashing function and equality for constraint entries. */
27344
27345 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
27346 {
27347 static hashval_t hash (constraint_sat_entry *e)
27348 {
27349 hashval_t val = iterative_hash_object(e->ci, 0);
27350 return iterative_hash_template_arg (e->args, val);
27351 }
27352
27353 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
27354 {
27355 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
27356 }
27357 };
27358
27359 /* Memoized satisfaction results for concept checks. */
27360
27361 struct GTY((for_user)) concept_spec_entry
27362 {
27363 tree tmpl;
27364 tree args;
27365 tree result;
27366 };
27367
27368 /* Hashing function and equality for constraint entries. */
27369
27370 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
27371 {
27372 static hashval_t hash (concept_spec_entry *e)
27373 {
27374 return hash_tmpl_and_args (e->tmpl, e->args);
27375 }
27376
27377 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
27378 {
27379 ++comparing_specializations;
27380 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
27381 --comparing_specializations;
27382 return eq;
27383 }
27384 };
27385
27386 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
27387 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
27388
27389 /* Search for a memoized satisfaction result. Returns one of the
27390 truth value nodes if previously memoized, or NULL_TREE otherwise. */
27391
27392 tree
27393 lookup_constraint_satisfaction (tree ci, tree args)
27394 {
27395 constraint_sat_entry elt = { ci, args, NULL_TREE };
27396 constraint_sat_entry* found = constraint_memos->find (&elt);
27397 if (found)
27398 return found->result;
27399 else
27400 return NULL_TREE;
27401 }
27402
27403 /* Memoize the result of a satisfication test. Returns the saved result. */
27404
27405 tree
27406 memoize_constraint_satisfaction (tree ci, tree args, tree result)
27407 {
27408 constraint_sat_entry elt = {ci, args, result};
27409 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
27410 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
27411 *entry = elt;
27412 *slot = entry;
27413 return result;
27414 }
27415
27416 /* Search for a memoized satisfaction result for a concept. */
27417
27418 tree
27419 lookup_concept_satisfaction (tree tmpl, tree args)
27420 {
27421 concept_spec_entry elt = { tmpl, args, NULL_TREE };
27422 concept_spec_entry* found = concept_memos->find (&elt);
27423 if (found)
27424 return found->result;
27425 else
27426 return NULL_TREE;
27427 }
27428
27429 /* Memoize the result of a concept check. Returns the saved result. */
27430
27431 tree
27432 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
27433 {
27434 concept_spec_entry elt = {tmpl, args, result};
27435 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
27436 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
27437 *entry = elt;
27438 *slot = entry;
27439 return result;
27440 }
27441
27442 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
27443
27444 /* Returns a prior concept specialization. This returns the substituted
27445 and normalized constraints defined by the concept. */
27446
27447 tree
27448 get_concept_expansion (tree tmpl, tree args)
27449 {
27450 concept_spec_entry elt = { tmpl, args, NULL_TREE };
27451 concept_spec_entry* found = concept_expansions->find (&elt);
27452 if (found)
27453 return found->result;
27454 else
27455 return NULL_TREE;
27456 }
27457
27458 /* Save a concept expansion for later. */
27459
27460 tree
27461 save_concept_expansion (tree tmpl, tree args, tree def)
27462 {
27463 concept_spec_entry elt = {tmpl, args, def};
27464 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
27465 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
27466 *entry = elt;
27467 *slot = entry;
27468 return def;
27469 }
27470
27471 static hashval_t
27472 hash_subsumption_args (tree t1, tree t2)
27473 {
27474 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
27475 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
27476 int val = 0;
27477 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
27478 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
27479 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
27480 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
27481 return val;
27482 }
27483
27484 /* Compare the constraints of two subsumption entries. The LEFT1 and
27485 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
27486 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
27487
27488 static bool
27489 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
27490 {
27491 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
27492 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
27493 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
27494 CHECK_CONSTR_ARGS (right1)))
27495 return comp_template_args (CHECK_CONSTR_ARGS (left2),
27496 CHECK_CONSTR_ARGS (right2));
27497 return false;
27498 }
27499
27500 /* Key/value pair for learning and memoizing subsumption results. This
27501 associates a pair of check constraints (including arguments) with
27502 a boolean value indicating the result. */
27503
27504 struct GTY((for_user)) subsumption_entry
27505 {
27506 tree t1;
27507 tree t2;
27508 bool result;
27509 };
27510
27511 /* Hashing function and equality for constraint entries. */
27512
27513 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
27514 {
27515 static hashval_t hash (subsumption_entry *e)
27516 {
27517 return hash_subsumption_args (e->t1, e->t2);
27518 }
27519
27520 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
27521 {
27522 ++comparing_specializations;
27523 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
27524 --comparing_specializations;
27525 return eq;
27526 }
27527 };
27528
27529 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
27530
27531 /* Search for a previously cached subsumption result. */
27532
27533 bool*
27534 lookup_subsumption_result (tree t1, tree t2)
27535 {
27536 subsumption_entry elt = { t1, t2, false };
27537 subsumption_entry* found = subsumption_table->find (&elt);
27538 if (found)
27539 return &found->result;
27540 else
27541 return 0;
27542 }
27543
27544 /* Save a subsumption result. */
27545
27546 bool
27547 save_subsumption_result (tree t1, tree t2, bool result)
27548 {
27549 subsumption_entry elt = {t1, t2, result};
27550 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
27551 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
27552 *entry = elt;
27553 *slot = entry;
27554 return result;
27555 }
27556
27557 /* Set up the hash table for constraint association. */
27558
27559 void
27560 init_constraint_processing (void)
27561 {
27562 if (!flag_concepts)
27563 return;
27564
27565 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
27566 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
27567 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
27568 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
27569 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
27570 }
27571
27572 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
27573 0..N-1. */
27574
27575 void
27576 declare_integer_pack (void)
27577 {
27578 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
27579 build_function_type_list (integer_type_node,
27580 integer_type_node,
27581 NULL_TREE),
27582 NULL_TREE, ECF_CONST);
27583 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
27584 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
27585 DECL_FUNCTION_CODE (ipfn)
27586 = (enum built_in_function) (int) CP_BUILT_IN_INTEGER_PACK;
27587 }
27588
27589 /* Set up the hash tables for template instantiations. */
27590
27591 void
27592 init_template_processing (void)
27593 {
27594 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
27595 type_specializations = hash_table<spec_hasher>::create_ggc (37);
27596
27597 if (cxx_dialect >= cxx11)
27598 declare_integer_pack ();
27599 }
27600
27601 /* Print stats about the template hash tables for -fstats. */
27602
27603 void
27604 print_template_statistics (void)
27605 {
27606 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
27607 "%f collisions\n", (long) decl_specializations->size (),
27608 (long) decl_specializations->elements (),
27609 decl_specializations->collisions ());
27610 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
27611 "%f collisions\n", (long) type_specializations->size (),
27612 (long) type_specializations->elements (),
27613 type_specializations->collisions ());
27614 }
27615
27616 #if CHECKING_P
27617
27618 namespace selftest {
27619
27620 /* Verify that build_non_dependent_expr () works, for various expressions,
27621 and that location wrappers don't affect the results. */
27622
27623 static void
27624 test_build_non_dependent_expr ()
27625 {
27626 location_t loc = BUILTINS_LOCATION;
27627
27628 /* Verify constants, without and with location wrappers. */
27629 tree int_cst = build_int_cst (integer_type_node, 42);
27630 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
27631
27632 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
27633 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
27634 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
27635
27636 tree string_lit = build_string (4, "foo");
27637 TREE_TYPE (string_lit) = char_array_type_node;
27638 string_lit = fix_string_type (string_lit);
27639 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
27640
27641 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
27642 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
27643 ASSERT_EQ (wrapped_string_lit,
27644 build_non_dependent_expr (wrapped_string_lit));
27645 }
27646
27647 /* Verify that type_dependent_expression_p () works correctly, even
27648 in the presence of location wrapper nodes. */
27649
27650 static void
27651 test_type_dependent_expression_p ()
27652 {
27653 location_t loc = BUILTINS_LOCATION;
27654
27655 tree name = get_identifier ("foo");
27656
27657 /* If no templates are involved, nothing is type-dependent. */
27658 gcc_assert (!processing_template_decl);
27659 ASSERT_FALSE (type_dependent_expression_p (name));
27660
27661 ++processing_template_decl;
27662
27663 /* Within a template, an unresolved name is always type-dependent. */
27664 ASSERT_TRUE (type_dependent_expression_p (name));
27665
27666 /* Ensure it copes with NULL_TREE and errors. */
27667 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
27668 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
27669
27670 /* A USING_DECL in a template should be type-dependent, even if wrapped
27671 with a location wrapper (PR c++/83799). */
27672 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
27673 TREE_TYPE (using_decl) = integer_type_node;
27674 ASSERT_TRUE (type_dependent_expression_p (using_decl));
27675 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
27676 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
27677 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
27678
27679 --processing_template_decl;
27680 }
27681
27682 /* Run all of the selftests within this file. */
27683
27684 void
27685 cp_pt_c_tests ()
27686 {
27687 test_build_non_dependent_expr ();
27688 test_type_dependent_expression_p ();
27689 }
27690
27691 } // namespace selftest
27692
27693 #endif /* #if CHECKING_P */
27694
27695 #include "gt-cp-pt.h"