Fix MIPS16 ICE.
[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 struct pending_template *next;
55 struct tinst_level *tinst;
56 };
57
58 static GTY(()) struct pending_template *pending_templates;
59 static GTY(()) struct pending_template *last_pending_template;
60
61 int processing_template_parmlist;
62 static int template_header_count;
63
64 static GTY(()) tree saved_trees;
65 static vec<int> inline_parm_levels;
66
67 static GTY(()) struct tinst_level *current_tinst_level;
68
69 static GTY(()) tree saved_access_scope;
70
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr;
75
76 // -------------------------------------------------------------------------- //
77 // Local Specialization Stack
78 //
79 // Implementation of the RAII helper for creating new local
80 // specializations.
81 local_specialization_stack::local_specialization_stack (lss_policy policy)
82 : saved (local_specializations)
83 {
84 if (policy == lss_blank || !saved)
85 local_specializations = new hash_map<tree, tree>;
86 else
87 local_specializations = new hash_map<tree, tree>(*saved);
88 }
89
90 local_specialization_stack::~local_specialization_stack ()
91 {
92 delete local_specializations;
93 local_specializations = saved;
94 }
95
96 /* True if we've recursed into fn_type_unification too many times. */
97 static bool excessive_deduction_depth;
98
99 struct GTY((for_user)) spec_entry
100 {
101 tree tmpl;
102 tree args;
103 tree spec;
104 };
105
106 struct spec_hasher : ggc_ptr_hash<spec_entry>
107 {
108 static hashval_t hash (spec_entry *);
109 static bool equal (spec_entry *, spec_entry *);
110 };
111
112 static GTY (()) hash_table<spec_hasher> *decl_specializations;
113
114 static GTY (()) hash_table<spec_hasher> *type_specializations;
115
116 /* Contains canonical template parameter types. The vector is indexed by
117 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
118 TREE_LIST, whose TREE_VALUEs contain the canonical template
119 parameters of various types and levels. */
120 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
121
122 #define UNIFY_ALLOW_NONE 0
123 #define UNIFY_ALLOW_MORE_CV_QUAL 1
124 #define UNIFY_ALLOW_LESS_CV_QUAL 2
125 #define UNIFY_ALLOW_DERIVED 4
126 #define UNIFY_ALLOW_INTEGER 8
127 #define UNIFY_ALLOW_OUTER_LEVEL 16
128 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
129 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
130
131 enum template_base_result {
132 tbr_incomplete_type,
133 tbr_ambiguous_baseclass,
134 tbr_success
135 };
136
137 static void push_access_scope (tree);
138 static void pop_access_scope (tree);
139 static bool resolve_overloaded_unification (tree, tree, tree, tree,
140 unification_kind_t, int,
141 bool);
142 static int try_one_overload (tree, tree, tree, tree, tree,
143 unification_kind_t, int, bool, bool);
144 static int unify (tree, tree, tree, tree, int, bool);
145 static void add_pending_template (tree);
146 static tree reopen_tinst_level (struct tinst_level *);
147 static tree tsubst_initializer_list (tree, tree);
148 static tree get_partial_spec_bindings (tree, tree, tree);
149 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
150 bool, bool);
151 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
152 bool, bool);
153 static void tsubst_enum (tree, tree, tree);
154 static tree add_to_template_args (tree, tree);
155 static tree add_outermost_template_args (tree, tree);
156 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
157 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
158 tree);
159 static int type_unification_real (tree, tree, tree, const tree *,
160 unsigned int, int, unification_kind_t, int,
161 vec<deferred_access_check, va_gc> **,
162 bool);
163 static void note_template_header (int);
164 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
165 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
166 static tree convert_template_argument (tree, tree, tree,
167 tsubst_flags_t, int, tree);
168 static tree for_each_template_parm (tree, tree_fn_t, void*,
169 hash_set<tree> *, bool, tree_fn_t = NULL);
170 static tree expand_template_argument_pack (tree);
171 static tree build_template_parm_index (int, int, int, tree, tree);
172 static bool inline_needs_template_parms (tree, bool);
173 static void push_inline_template_parms_recursive (tree, int);
174 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
175 static int mark_template_parm (tree, void *);
176 static int template_parm_this_level_p (tree, void *);
177 static tree tsubst_friend_function (tree, tree);
178 static tree tsubst_friend_class (tree, tree);
179 static int can_complete_type_without_circularity (tree);
180 static tree get_bindings (tree, tree, tree, bool);
181 static int template_decl_level (tree);
182 static int check_cv_quals_for_unify (int, tree, tree);
183 static void template_parm_level_and_index (tree, int*, int*);
184 static int unify_pack_expansion (tree, tree, tree,
185 tree, unification_kind_t, bool, bool);
186 static tree copy_template_args (tree);
187 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
190 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
191 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
192 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
193 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
194 static bool check_specialization_scope (void);
195 static tree process_partial_specialization (tree);
196 static void set_current_access_from_decl (tree);
197 static enum template_base_result get_template_base (tree, tree, tree, tree,
198 bool , tree *);
199 static tree try_class_unification (tree, tree, tree, tree, bool);
200 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
201 tree, tree);
202 static bool template_template_parm_bindings_ok_p (tree, tree);
203 static void tsubst_default_arguments (tree, tsubst_flags_t);
204 static tree for_each_template_parm_r (tree *, int *, void *);
205 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
206 static void copy_default_args_to_explicit_spec (tree);
207 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
208 static bool dependent_template_arg_p (tree);
209 static bool any_template_arguments_need_structural_equality_p (tree);
210 static bool dependent_type_p_r (tree);
211 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
212 static tree tsubst_decl (tree, tree, tsubst_flags_t);
213 static void perform_typedefs_access_check (tree tmpl, tree targs);
214 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
215 location_t);
216 static tree listify (tree);
217 static tree listify_autos (tree, tree);
218 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
219 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
220 static bool complex_alias_template_p (const_tree tmpl);
221 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
222 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
223 static tree make_argument_pack (tree);
224 static void register_parameter_specializations (tree, tree);
225 static tree enclosing_instantiation_of (tree tctx);
226
227 /* Make the current scope suitable for access checking when we are
228 processing T. T can be FUNCTION_DECL for instantiated function
229 template, VAR_DECL for static member variable, or TYPE_DECL for
230 alias template (needed by instantiate_decl). */
231
232 static void
233 push_access_scope (tree t)
234 {
235 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
236 || TREE_CODE (t) == TYPE_DECL);
237
238 if (DECL_FRIEND_CONTEXT (t))
239 push_nested_class (DECL_FRIEND_CONTEXT (t));
240 else if (DECL_CLASS_SCOPE_P (t))
241 push_nested_class (DECL_CONTEXT (t));
242 else
243 push_to_top_level ();
244
245 if (TREE_CODE (t) == FUNCTION_DECL)
246 {
247 saved_access_scope = tree_cons
248 (NULL_TREE, current_function_decl, saved_access_scope);
249 current_function_decl = t;
250 }
251 }
252
253 /* Restore the scope set up by push_access_scope. T is the node we
254 are processing. */
255
256 static void
257 pop_access_scope (tree t)
258 {
259 if (TREE_CODE (t) == FUNCTION_DECL)
260 {
261 current_function_decl = TREE_VALUE (saved_access_scope);
262 saved_access_scope = TREE_CHAIN (saved_access_scope);
263 }
264
265 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
266 pop_nested_class ();
267 else
268 pop_from_top_level ();
269 }
270
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
275
276 tree
277 finish_member_template_decl (tree decl)
278 {
279 if (decl == error_mark_node)
280 return error_mark_node;
281
282 gcc_assert (DECL_P (decl));
283
284 if (TREE_CODE (decl) == TYPE_DECL)
285 {
286 tree type;
287
288 type = TREE_TYPE (decl);
289 if (type == error_mark_node)
290 return error_mark_node;
291 if (MAYBE_CLASS_TYPE_P (type)
292 && CLASSTYPE_TEMPLATE_INFO (type)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
294 {
295 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
296 check_member_template (tmpl);
297 return tmpl;
298 }
299 return NULL_TREE;
300 }
301 else if (TREE_CODE (decl) == FIELD_DECL)
302 error ("data member %qD cannot be a member template", decl);
303 else if (DECL_TEMPLATE_INFO (decl))
304 {
305 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
306 {
307 check_member_template (DECL_TI_TEMPLATE (decl));
308 return DECL_TI_TEMPLATE (decl);
309 }
310 else
311 return decl;
312 }
313 else
314 error ("invalid member template declaration %qD", decl);
315
316 return error_mark_node;
317 }
318
319 /* Create a template info node. */
320
321 tree
322 build_template_info (tree template_decl, tree template_args)
323 {
324 tree result = make_node (TEMPLATE_INFO);
325 TI_TEMPLATE (result) = template_decl;
326 TI_ARGS (result) = template_args;
327 return result;
328 }
329
330 /* Return the template info node corresponding to T, whatever T is. */
331
332 tree
333 get_template_info (const_tree t)
334 {
335 tree tinfo = NULL_TREE;
336
337 if (!t || t == error_mark_node)
338 return NULL;
339
340 if (TREE_CODE (t) == NAMESPACE_DECL
341 || TREE_CODE (t) == PARM_DECL)
342 return NULL;
343
344 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
345 tinfo = DECL_TEMPLATE_INFO (t);
346
347 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
348 t = TREE_TYPE (t);
349
350 if (OVERLOAD_TYPE_P (t))
351 tinfo = TYPE_TEMPLATE_INFO (t);
352 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
353 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
354
355 return tinfo;
356 }
357
358 /* Returns the template nesting level of the indicated class TYPE.
359
360 For example, in:
361 template <class T>
362 struct A
363 {
364 template <class U>
365 struct B {};
366 };
367
368 A<T>::B<U> has depth two, while A<T> has depth one.
369 Both A<T>::B<int> and A<int>::B<U> have depth one, if
370 they are instantiations, not specializations.
371
372 This function is guaranteed to return 0 if passed NULL_TREE so
373 that, for example, `template_class_depth (current_class_type)' is
374 always safe. */
375
376 int
377 template_class_depth (tree type)
378 {
379 int depth;
380
381 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
382 {
383 tree tinfo = get_template_info (type);
384
385 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
386 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
387 ++depth;
388
389 if (DECL_P (type))
390 type = CP_DECL_CONTEXT (type);
391 else if (LAMBDA_TYPE_P (type))
392 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
393 else
394 type = CP_TYPE_CONTEXT (type);
395 }
396
397 return depth;
398 }
399
400 /* Subroutine of maybe_begin_member_template_processing.
401 Returns true if processing DECL needs us to push template parms. */
402
403 static bool
404 inline_needs_template_parms (tree decl, bool nsdmi)
405 {
406 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
407 return false;
408
409 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
410 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
411 }
412
413 /* Subroutine of maybe_begin_member_template_processing.
414 Push the template parms in PARMS, starting from LEVELS steps into the
415 chain, and ending at the beginning, since template parms are listed
416 innermost first. */
417
418 static void
419 push_inline_template_parms_recursive (tree parmlist, int levels)
420 {
421 tree parms = TREE_VALUE (parmlist);
422 int i;
423
424 if (levels > 1)
425 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
426
427 ++processing_template_decl;
428 current_template_parms
429 = tree_cons (size_int (processing_template_decl),
430 parms, current_template_parms);
431 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
432
433 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
434 NULL);
435 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
436 {
437 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
438
439 if (error_operand_p (parm))
440 continue;
441
442 gcc_assert (DECL_P (parm));
443
444 switch (TREE_CODE (parm))
445 {
446 case TYPE_DECL:
447 case TEMPLATE_DECL:
448 pushdecl (parm);
449 break;
450
451 case PARM_DECL:
452 /* Push the CONST_DECL. */
453 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
454 break;
455
456 default:
457 gcc_unreachable ();
458 }
459 }
460 }
461
462 /* Restore the template parameter context for a member template, a
463 friend template defined in a class definition, or a non-template
464 member of template class. */
465
466 void
467 maybe_begin_member_template_processing (tree decl)
468 {
469 tree parms;
470 int levels = 0;
471 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
472
473 if (nsdmi)
474 {
475 tree ctx = DECL_CONTEXT (decl);
476 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
477 /* Disregard full specializations (c++/60999). */
478 && uses_template_parms (ctx)
479 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
480 }
481
482 if (inline_needs_template_parms (decl, nsdmi))
483 {
484 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
485 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
486
487 if (DECL_TEMPLATE_SPECIALIZATION (decl))
488 {
489 --levels;
490 parms = TREE_CHAIN (parms);
491 }
492
493 push_inline_template_parms_recursive (parms, levels);
494 }
495
496 /* Remember how many levels of template parameters we pushed so that
497 we can pop them later. */
498 inline_parm_levels.safe_push (levels);
499 }
500
501 /* Undo the effects of maybe_begin_member_template_processing. */
502
503 void
504 maybe_end_member_template_processing (void)
505 {
506 int i;
507 int last;
508
509 if (inline_parm_levels.length () == 0)
510 return;
511
512 last = inline_parm_levels.pop ();
513 for (i = 0; i < last; ++i)
514 {
515 --processing_template_decl;
516 current_template_parms = TREE_CHAIN (current_template_parms);
517 poplevel (0, 0, 0);
518 }
519 }
520
521 /* Return a new template argument vector which contains all of ARGS,
522 but has as its innermost set of arguments the EXTRA_ARGS. */
523
524 static tree
525 add_to_template_args (tree args, tree extra_args)
526 {
527 tree new_args;
528 int extra_depth;
529 int i;
530 int j;
531
532 if (args == NULL_TREE || extra_args == error_mark_node)
533 return extra_args;
534
535 extra_depth = TMPL_ARGS_DEPTH (extra_args);
536 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
537
538 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
539 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
540
541 for (j = 1; j <= extra_depth; ++j, ++i)
542 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
543
544 return new_args;
545 }
546
547 /* Like add_to_template_args, but only the outermost ARGS are added to
548 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
549 (EXTRA_ARGS) levels are added. This function is used to combine
550 the template arguments from a partial instantiation with the
551 template arguments used to attain the full instantiation from the
552 partial instantiation. */
553
554 static tree
555 add_outermost_template_args (tree args, tree extra_args)
556 {
557 tree new_args;
558
559 /* If there are more levels of EXTRA_ARGS than there are ARGS,
560 something very fishy is going on. */
561 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
562
563 /* If *all* the new arguments will be the EXTRA_ARGS, just return
564 them. */
565 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
566 return extra_args;
567
568 /* For the moment, we make ARGS look like it contains fewer levels. */
569 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
570
571 new_args = add_to_template_args (args, extra_args);
572
573 /* Now, we restore ARGS to its full dimensions. */
574 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
575
576 return new_args;
577 }
578
579 /* Return the N levels of innermost template arguments from the ARGS. */
580
581 tree
582 get_innermost_template_args (tree args, int n)
583 {
584 tree new_args;
585 int extra_levels;
586 int i;
587
588 gcc_assert (n >= 0);
589
590 /* If N is 1, just return the innermost set of template arguments. */
591 if (n == 1)
592 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
593
594 /* If we're not removing anything, just return the arguments we were
595 given. */
596 extra_levels = TMPL_ARGS_DEPTH (args) - n;
597 gcc_assert (extra_levels >= 0);
598 if (extra_levels == 0)
599 return args;
600
601 /* Make a new set of arguments, not containing the outer arguments. */
602 new_args = make_tree_vec (n);
603 for (i = 1; i <= n; ++i)
604 SET_TMPL_ARGS_LEVEL (new_args, i,
605 TMPL_ARGS_LEVEL (args, i + extra_levels));
606
607 return new_args;
608 }
609
610 /* The inverse of get_innermost_template_args: Return all but the innermost
611 EXTRA_LEVELS levels of template arguments from the ARGS. */
612
613 static tree
614 strip_innermost_template_args (tree args, int extra_levels)
615 {
616 tree new_args;
617 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
618 int i;
619
620 gcc_assert (n >= 0);
621
622 /* If N is 1, just return the outermost set of template arguments. */
623 if (n == 1)
624 return TMPL_ARGS_LEVEL (args, 1);
625
626 /* If we're not removing anything, just return the arguments we were
627 given. */
628 gcc_assert (extra_levels >= 0);
629 if (extra_levels == 0)
630 return args;
631
632 /* Make a new set of arguments, not containing the inner arguments. */
633 new_args = make_tree_vec (n);
634 for (i = 1; i <= n; ++i)
635 SET_TMPL_ARGS_LEVEL (new_args, i,
636 TMPL_ARGS_LEVEL (args, i));
637
638 return new_args;
639 }
640
641 /* We've got a template header coming up; push to a new level for storing
642 the parms. */
643
644 void
645 begin_template_parm_list (void)
646 {
647 /* We use a non-tag-transparent scope here, which causes pushtag to
648 put tags in this scope, rather than in the enclosing class or
649 namespace scope. This is the right thing, since we want
650 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
651 global template class, push_template_decl handles putting the
652 TEMPLATE_DECL into top-level scope. For a nested template class,
653 e.g.:
654
655 template <class T> struct S1 {
656 template <class T> struct S2 {};
657 };
658
659 pushtag contains special code to insert the TEMPLATE_DECL for S2
660 at the right scope. */
661 begin_scope (sk_template_parms, NULL);
662 ++processing_template_decl;
663 ++processing_template_parmlist;
664 note_template_header (0);
665
666 /* Add a dummy parameter level while we process the parameter list. */
667 current_template_parms
668 = tree_cons (size_int (processing_template_decl),
669 make_tree_vec (0),
670 current_template_parms);
671 }
672
673 /* This routine is called when a specialization is declared. If it is
674 invalid to declare a specialization here, an error is reported and
675 false is returned, otherwise this routine will return true. */
676
677 static bool
678 check_specialization_scope (void)
679 {
680 tree scope = current_scope ();
681
682 /* [temp.expl.spec]
683
684 An explicit specialization shall be declared in the namespace of
685 which the template is a member, or, for member templates, in the
686 namespace of which the enclosing class or enclosing class
687 template is a member. An explicit specialization of a member
688 function, member class or static data member of a class template
689 shall be declared in the namespace of which the class template
690 is a member. */
691 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
692 {
693 error ("explicit specialization in non-namespace scope %qD", scope);
694 return false;
695 }
696
697 /* [temp.expl.spec]
698
699 In an explicit specialization declaration for a member of a class
700 template or a member template that appears in namespace scope,
701 the member template and some of its enclosing class templates may
702 remain unspecialized, except that the declaration shall not
703 explicitly specialize a class member template if its enclosing
704 class templates are not explicitly specialized as well. */
705 if (current_template_parms)
706 {
707 error ("enclosing class templates are not explicitly specialized");
708 return false;
709 }
710
711 return true;
712 }
713
714 /* We've just seen template <>. */
715
716 bool
717 begin_specialization (void)
718 {
719 begin_scope (sk_template_spec, NULL);
720 note_template_header (1);
721 return check_specialization_scope ();
722 }
723
724 /* Called at then end of processing a declaration preceded by
725 template<>. */
726
727 void
728 end_specialization (void)
729 {
730 finish_scope ();
731 reset_specialization ();
732 }
733
734 /* Any template <>'s that we have seen thus far are not referring to a
735 function specialization. */
736
737 void
738 reset_specialization (void)
739 {
740 processing_specialization = 0;
741 template_header_count = 0;
742 }
743
744 /* We've just seen a template header. If SPECIALIZATION is nonzero,
745 it was of the form template <>. */
746
747 static void
748 note_template_header (int specialization)
749 {
750 processing_specialization = specialization;
751 template_header_count++;
752 }
753
754 /* We're beginning an explicit instantiation. */
755
756 void
757 begin_explicit_instantiation (void)
758 {
759 gcc_assert (!processing_explicit_instantiation);
760 processing_explicit_instantiation = true;
761 }
762
763
764 void
765 end_explicit_instantiation (void)
766 {
767 gcc_assert (processing_explicit_instantiation);
768 processing_explicit_instantiation = false;
769 }
770
771 /* An explicit specialization or partial specialization of TMPL is being
772 declared. Check that the namespace in which the specialization is
773 occurring is permissible. Returns false iff it is invalid to
774 specialize TMPL in the current namespace. */
775
776 static bool
777 check_specialization_namespace (tree tmpl)
778 {
779 tree tpl_ns = decl_namespace_context (tmpl);
780
781 /* [tmpl.expl.spec]
782
783 An explicit specialization shall be declared in a namespace enclosing the
784 specialized template. An explicit specialization whose declarator-id is
785 not qualified shall be declared in the nearest enclosing namespace of the
786 template, or, if the namespace is inline (7.3.1), any namespace from its
787 enclosing namespace set. */
788 if (current_scope() != DECL_CONTEXT (tmpl)
789 && !at_namespace_scope_p ())
790 {
791 error ("specialization of %qD must appear at namespace scope", tmpl);
792 return false;
793 }
794
795 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
796 /* Same or enclosing namespace. */
797 return true;
798 else
799 {
800 permerror (input_location,
801 "specialization of %qD in different namespace", tmpl);
802 inform (DECL_SOURCE_LOCATION (tmpl),
803 " from definition of %q#D", tmpl);
804 return false;
805 }
806 }
807
808 /* SPEC is an explicit instantiation. Check that it is valid to
809 perform this explicit instantiation in the current namespace. */
810
811 static void
812 check_explicit_instantiation_namespace (tree spec)
813 {
814 tree ns;
815
816 /* DR 275: An explicit instantiation shall appear in an enclosing
817 namespace of its template. */
818 ns = decl_namespace_context (spec);
819 if (!is_nested_namespace (current_namespace, ns))
820 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
821 "(which does not enclose namespace %qD)",
822 spec, current_namespace, ns);
823 }
824
825 // Returns the type of a template specialization only if that
826 // specialization needs to be defined. Otherwise (e.g., if the type has
827 // already been defined), the function returns NULL_TREE.
828 static tree
829 maybe_new_partial_specialization (tree type)
830 {
831 // An implicit instantiation of an incomplete type implies
832 // the definition of a new class template.
833 //
834 // template<typename T>
835 // struct S;
836 //
837 // template<typename T>
838 // struct S<T*>;
839 //
840 // Here, S<T*> is an implicit instantiation of S whose type
841 // is incomplete.
842 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
843 return type;
844
845 // It can also be the case that TYPE is a completed specialization.
846 // Continuing the previous example, suppose we also declare:
847 //
848 // template<typename T>
849 // requires Integral<T>
850 // struct S<T*>;
851 //
852 // Here, S<T*> refers to the specialization S<T*> defined
853 // above. However, we need to differentiate definitions because
854 // we intend to define a new partial specialization. In this case,
855 // we rely on the fact that the constraints are different for
856 // this declaration than that above.
857 //
858 // Note that we also get here for injected class names and
859 // late-parsed template definitions. We must ensure that we
860 // do not create new type declarations for those cases.
861 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
862 {
863 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
864 tree args = CLASSTYPE_TI_ARGS (type);
865
866 // If there are no template parameters, this cannot be a new
867 // partial template specializtion?
868 if (!current_template_parms)
869 return NULL_TREE;
870
871 // The injected-class-name is not a new partial specialization.
872 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
873 return NULL_TREE;
874
875 // If the constraints are not the same as those of the primary
876 // then, we can probably create a new specialization.
877 tree type_constr = current_template_constraints ();
878
879 if (type == TREE_TYPE (tmpl))
880 {
881 tree main_constr = get_constraints (tmpl);
882 if (equivalent_constraints (type_constr, main_constr))
883 return NULL_TREE;
884 }
885
886 // Also, if there's a pre-existing specialization with matching
887 // constraints, then this also isn't new.
888 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
889 while (specs)
890 {
891 tree spec_tmpl = TREE_VALUE (specs);
892 tree spec_args = TREE_PURPOSE (specs);
893 tree spec_constr = get_constraints (spec_tmpl);
894 if (comp_template_args (args, spec_args)
895 && equivalent_constraints (type_constr, spec_constr))
896 return NULL_TREE;
897 specs = TREE_CHAIN (specs);
898 }
899
900 // Create a new type node (and corresponding type decl)
901 // for the newly declared specialization.
902 tree t = make_class_type (TREE_CODE (type));
903 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
904 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
905
906 /* We only need a separate type node for storing the definition of this
907 partial specialization; uses of S<T*> are unconstrained, so all are
908 equivalent. So keep TYPE_CANONICAL the same. */
909 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
910
911 // Build the corresponding type decl.
912 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
913 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
914 DECL_SOURCE_LOCATION (d) = input_location;
915
916 return t;
917 }
918
919 return NULL_TREE;
920 }
921
922 /* The TYPE is being declared. If it is a template type, that means it
923 is a partial specialization. Do appropriate error-checking. */
924
925 tree
926 maybe_process_partial_specialization (tree type)
927 {
928 tree context;
929
930 if (type == error_mark_node)
931 return error_mark_node;
932
933 /* A lambda that appears in specialization context is not itself a
934 specialization. */
935 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
936 return type;
937
938 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
939 {
940 error ("name of class shadows template template parameter %qD",
941 TYPE_NAME (type));
942 return error_mark_node;
943 }
944
945 context = TYPE_CONTEXT (type);
946
947 if (TYPE_ALIAS_P (type))
948 {
949 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
950
951 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
952 error ("specialization of alias template %qD",
953 TI_TEMPLATE (tinfo));
954 else
955 error ("explicit specialization of non-template %qT", type);
956 return error_mark_node;
957 }
958 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
959 {
960 /* This is for ordinary explicit specialization and partial
961 specialization of a template class such as:
962
963 template <> class C<int>;
964
965 or:
966
967 template <class T> class C<T*>;
968
969 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
970
971 if (tree t = maybe_new_partial_specialization (type))
972 {
973 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
974 && !at_namespace_scope_p ())
975 return error_mark_node;
976 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
977 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
978 if (processing_template_decl)
979 {
980 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
981 if (decl == error_mark_node)
982 return error_mark_node;
983 return TREE_TYPE (decl);
984 }
985 }
986 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
987 error ("specialization of %qT after instantiation", type);
988 else if (errorcount && !processing_specialization
989 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
990 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
991 /* Trying to define a specialization either without a template<> header
992 or in an inappropriate place. We've already given an error, so just
993 bail now so we don't actually define the specialization. */
994 return error_mark_node;
995 }
996 else if (CLASS_TYPE_P (type)
997 && !CLASSTYPE_USE_TEMPLATE (type)
998 && CLASSTYPE_TEMPLATE_INFO (type)
999 && context && CLASS_TYPE_P (context)
1000 && CLASSTYPE_TEMPLATE_INFO (context))
1001 {
1002 /* This is for an explicit specialization of member class
1003 template according to [temp.expl.spec/18]:
1004
1005 template <> template <class U> class C<int>::D;
1006
1007 The context `C<int>' must be an implicit instantiation.
1008 Otherwise this is just a member class template declared
1009 earlier like:
1010
1011 template <> class C<int> { template <class U> class D; };
1012 template <> template <class U> class C<int>::D;
1013
1014 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1015 while in the second case, `C<int>::D' is a primary template
1016 and `C<T>::D' may not exist. */
1017
1018 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1019 && !COMPLETE_TYPE_P (type))
1020 {
1021 tree t;
1022 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1023
1024 if (current_namespace
1025 != decl_namespace_context (tmpl))
1026 {
1027 permerror (input_location,
1028 "specializing %q#T in different namespace", type);
1029 permerror (DECL_SOURCE_LOCATION (tmpl),
1030 " from definition of %q#D", tmpl);
1031 }
1032
1033 /* Check for invalid specialization after instantiation:
1034
1035 template <> template <> class C<int>::D<int>;
1036 template <> template <class U> class C<int>::D; */
1037
1038 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1039 t; t = TREE_CHAIN (t))
1040 {
1041 tree inst = TREE_VALUE (t);
1042 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1043 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1044 {
1045 /* We already have a full specialization of this partial
1046 instantiation, or a full specialization has been
1047 looked up but not instantiated. Reassign it to the
1048 new member specialization template. */
1049 spec_entry elt;
1050 spec_entry *entry;
1051
1052 elt.tmpl = most_general_template (tmpl);
1053 elt.args = CLASSTYPE_TI_ARGS (inst);
1054 elt.spec = inst;
1055
1056 type_specializations->remove_elt (&elt);
1057
1058 elt.tmpl = tmpl;
1059 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1060
1061 spec_entry **slot
1062 = type_specializations->find_slot (&elt, INSERT);
1063 entry = ggc_alloc<spec_entry> ();
1064 *entry = elt;
1065 *slot = entry;
1066 }
1067 else
1068 /* But if we've had an implicit instantiation, that's a
1069 problem ([temp.expl.spec]/6). */
1070 error ("specialization %qT after instantiation %qT",
1071 type, inst);
1072 }
1073
1074 /* Mark TYPE as a specialization. And as a result, we only
1075 have one level of template argument for the innermost
1076 class template. */
1077 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1078 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1079 CLASSTYPE_TI_ARGS (type)
1080 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1081 }
1082 }
1083 else if (processing_specialization)
1084 {
1085 /* Someday C++0x may allow for enum template specialization. */
1086 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1087 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1088 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1089 "of %qD not allowed by ISO C++", type);
1090 else
1091 {
1092 error ("explicit specialization of non-template %qT", type);
1093 return error_mark_node;
1094 }
1095 }
1096
1097 return type;
1098 }
1099
1100 /* Returns nonzero if we can optimize the retrieval of specializations
1101 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1102 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1103
1104 static inline bool
1105 optimize_specialization_lookup_p (tree tmpl)
1106 {
1107 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1108 && DECL_CLASS_SCOPE_P (tmpl)
1109 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1110 parameter. */
1111 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1112 /* The optimized lookup depends on the fact that the
1113 template arguments for the member function template apply
1114 purely to the containing class, which is not true if the
1115 containing class is an explicit or partial
1116 specialization. */
1117 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1118 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1119 && !DECL_CONV_FN_P (tmpl)
1120 /* It is possible to have a template that is not a member
1121 template and is not a member of a template class:
1122
1123 template <typename T>
1124 struct S { friend A::f(); };
1125
1126 Here, the friend function is a template, but the context does
1127 not have template information. The optimized lookup relies
1128 on having ARGS be the template arguments for both the class
1129 and the function template. */
1130 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1131 }
1132
1133 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1134 gone through coerce_template_parms by now. */
1135
1136 static void
1137 verify_unstripped_args (tree args)
1138 {
1139 ++processing_template_decl;
1140 if (!any_dependent_template_arguments_p (args))
1141 {
1142 tree inner = INNERMOST_TEMPLATE_ARGS (args);
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 (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1151 /* Allow typedefs on the type of a non-type argument, since a
1152 parameter can have them. */;
1153 else
1154 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1155 }
1156 }
1157 --processing_template_decl;
1158 }
1159
1160 /* Retrieve the specialization (in the sense of [temp.spec] - a
1161 specialization is either an instantiation or an explicit
1162 specialization) of TMPL for the given template ARGS. If there is
1163 no such specialization, return NULL_TREE. The ARGS are a vector of
1164 arguments, or a vector of vectors of arguments, in the case of
1165 templates with more than one level of parameters.
1166
1167 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1168 then we search for a partial specialization matching ARGS. This
1169 parameter is ignored if TMPL is not a class template.
1170
1171 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1172 result is a NONTYPE_ARGUMENT_PACK. */
1173
1174 static tree
1175 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1176 {
1177 if (tmpl == NULL_TREE)
1178 return NULL_TREE;
1179
1180 if (args == error_mark_node)
1181 return NULL_TREE;
1182
1183 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1184 || TREE_CODE (tmpl) == FIELD_DECL);
1185
1186 /* There should be as many levels of arguments as there are
1187 levels of parameters. */
1188 gcc_assert (TMPL_ARGS_DEPTH (args)
1189 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1190 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1191 : template_class_depth (DECL_CONTEXT (tmpl))));
1192
1193 if (flag_checking)
1194 verify_unstripped_args (args);
1195
1196 /* Lambda functions in templates aren't instantiated normally, but through
1197 tsubst_lambda_expr. */
1198 if (lambda_fn_in_template_p (tmpl))
1199 return NULL_TREE;
1200
1201 if (optimize_specialization_lookup_p (tmpl))
1202 {
1203 /* The template arguments actually apply to the containing
1204 class. Find the class specialization with those
1205 arguments. */
1206 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1207 tree class_specialization
1208 = retrieve_specialization (class_template, args, 0);
1209 if (!class_specialization)
1210 return NULL_TREE;
1211
1212 /* Find the instance of TMPL. */
1213 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1214 for (ovl_iterator iter (fns); iter; ++iter)
1215 {
1216 tree fn = *iter;
1217 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1218 /* using-declarations can add base methods to the method vec,
1219 and we don't want those here. */
1220 && DECL_CONTEXT (fn) == class_specialization)
1221 return fn;
1222 }
1223 return NULL_TREE;
1224 }
1225 else
1226 {
1227 spec_entry *found;
1228 spec_entry elt;
1229 hash_table<spec_hasher> *specializations;
1230
1231 elt.tmpl = tmpl;
1232 elt.args = args;
1233 elt.spec = NULL_TREE;
1234
1235 if (DECL_CLASS_TEMPLATE_P (tmpl))
1236 specializations = type_specializations;
1237 else
1238 specializations = decl_specializations;
1239
1240 if (hash == 0)
1241 hash = spec_hasher::hash (&elt);
1242 found = specializations->find_with_hash (&elt, hash);
1243 if (found)
1244 return found->spec;
1245 }
1246
1247 return NULL_TREE;
1248 }
1249
1250 /* Like retrieve_specialization, but for local declarations. */
1251
1252 tree
1253 retrieve_local_specialization (tree tmpl)
1254 {
1255 if (local_specializations == NULL)
1256 return NULL_TREE;
1257
1258 tree *slot = local_specializations->get (tmpl);
1259 return slot ? *slot : NULL_TREE;
1260 }
1261
1262 /* Returns nonzero iff DECL is a specialization of TMPL. */
1263
1264 int
1265 is_specialization_of (tree decl, tree tmpl)
1266 {
1267 tree t;
1268
1269 if (TREE_CODE (decl) == FUNCTION_DECL)
1270 {
1271 for (t = decl;
1272 t != NULL_TREE;
1273 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1274 if (t == tmpl)
1275 return 1;
1276 }
1277 else
1278 {
1279 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1280
1281 for (t = TREE_TYPE (decl);
1282 t != NULL_TREE;
1283 t = CLASSTYPE_USE_TEMPLATE (t)
1284 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1285 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1286 return 1;
1287 }
1288
1289 return 0;
1290 }
1291
1292 /* Returns nonzero iff DECL is a specialization of friend declaration
1293 FRIEND_DECL according to [temp.friend]. */
1294
1295 bool
1296 is_specialization_of_friend (tree decl, tree friend_decl)
1297 {
1298 bool need_template = true;
1299 int template_depth;
1300
1301 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1302 || TREE_CODE (decl) == TYPE_DECL);
1303
1304 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1305 of a template class, we want to check if DECL is a specialization
1306 if this. */
1307 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1308 && DECL_TEMPLATE_INFO (friend_decl)
1309 && !DECL_USE_TEMPLATE (friend_decl))
1310 {
1311 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1312 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1313 need_template = false;
1314 }
1315 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1316 && !PRIMARY_TEMPLATE_P (friend_decl))
1317 need_template = false;
1318
1319 /* There is nothing to do if this is not a template friend. */
1320 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1321 return false;
1322
1323 if (is_specialization_of (decl, friend_decl))
1324 return true;
1325
1326 /* [temp.friend/6]
1327 A member of a class template may be declared to be a friend of a
1328 non-template class. In this case, the corresponding member of
1329 every specialization of the class template is a friend of the
1330 class granting friendship.
1331
1332 For example, given a template friend declaration
1333
1334 template <class T> friend void A<T>::f();
1335
1336 the member function below is considered a friend
1337
1338 template <> struct A<int> {
1339 void f();
1340 };
1341
1342 For this type of template friend, TEMPLATE_DEPTH below will be
1343 nonzero. To determine if DECL is a friend of FRIEND, we first
1344 check if the enclosing class is a specialization of another. */
1345
1346 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1347 if (template_depth
1348 && DECL_CLASS_SCOPE_P (decl)
1349 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1350 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1351 {
1352 /* Next, we check the members themselves. In order to handle
1353 a few tricky cases, such as when FRIEND_DECL's are
1354
1355 template <class T> friend void A<T>::g(T t);
1356 template <class T> template <T t> friend void A<T>::h();
1357
1358 and DECL's are
1359
1360 void A<int>::g(int);
1361 template <int> void A<int>::h();
1362
1363 we need to figure out ARGS, the template arguments from
1364 the context of DECL. This is required for template substitution
1365 of `T' in the function parameter of `g' and template parameter
1366 of `h' in the above examples. Here ARGS corresponds to `int'. */
1367
1368 tree context = DECL_CONTEXT (decl);
1369 tree args = NULL_TREE;
1370 int current_depth = 0;
1371
1372 while (current_depth < template_depth)
1373 {
1374 if (CLASSTYPE_TEMPLATE_INFO (context))
1375 {
1376 if (current_depth == 0)
1377 args = TYPE_TI_ARGS (context);
1378 else
1379 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1380 current_depth++;
1381 }
1382 context = TYPE_CONTEXT (context);
1383 }
1384
1385 if (TREE_CODE (decl) == FUNCTION_DECL)
1386 {
1387 bool is_template;
1388 tree friend_type;
1389 tree decl_type;
1390 tree friend_args_type;
1391 tree decl_args_type;
1392
1393 /* Make sure that both DECL and FRIEND_DECL are templates or
1394 non-templates. */
1395 is_template = DECL_TEMPLATE_INFO (decl)
1396 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1397 if (need_template ^ is_template)
1398 return false;
1399 else if (is_template)
1400 {
1401 /* If both are templates, check template parameter list. */
1402 tree friend_parms
1403 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1404 args, tf_none);
1405 if (!comp_template_parms
1406 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1407 friend_parms))
1408 return false;
1409
1410 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1411 }
1412 else
1413 decl_type = TREE_TYPE (decl);
1414
1415 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1416 tf_none, NULL_TREE);
1417 if (friend_type == error_mark_node)
1418 return false;
1419
1420 /* Check if return types match. */
1421 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1422 return false;
1423
1424 /* Check if function parameter types match, ignoring the
1425 `this' parameter. */
1426 friend_args_type = TYPE_ARG_TYPES (friend_type);
1427 decl_args_type = TYPE_ARG_TYPES (decl_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1429 friend_args_type = TREE_CHAIN (friend_args_type);
1430 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1431 decl_args_type = TREE_CHAIN (decl_args_type);
1432
1433 return compparms (decl_args_type, friend_args_type);
1434 }
1435 else
1436 {
1437 /* DECL is a TYPE_DECL */
1438 bool is_template;
1439 tree decl_type = TREE_TYPE (decl);
1440
1441 /* Make sure that both DECL and FRIEND_DECL are templates or
1442 non-templates. */
1443 is_template
1444 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1445 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1446
1447 if (need_template ^ is_template)
1448 return false;
1449 else if (is_template)
1450 {
1451 tree friend_parms;
1452 /* If both are templates, check the name of the two
1453 TEMPLATE_DECL's first because is_friend didn't. */
1454 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1455 != DECL_NAME (friend_decl))
1456 return false;
1457
1458 /* Now check template parameter list. */
1459 friend_parms
1460 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1461 args, tf_none);
1462 return comp_template_parms
1463 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1464 friend_parms);
1465 }
1466 else
1467 return (DECL_NAME (decl)
1468 == DECL_NAME (friend_decl));
1469 }
1470 }
1471 return false;
1472 }
1473
1474 /* Register the specialization SPEC as a specialization of TMPL with
1475 the indicated ARGS. IS_FRIEND indicates whether the specialization
1476 is actually just a friend declaration. ATTRLIST is the list of
1477 attributes that the specialization is declared with or NULL when
1478 it isn't. Returns SPEC, or an equivalent prior declaration, if
1479 available.
1480
1481 We also store instantiations of field packs in the hash table, even
1482 though they are not themselves templates, to make lookup easier. */
1483
1484 static tree
1485 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1486 hashval_t hash)
1487 {
1488 tree fn;
1489 spec_entry **slot = NULL;
1490 spec_entry elt;
1491
1492 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1493 || (TREE_CODE (tmpl) == FIELD_DECL
1494 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1495
1496 if (TREE_CODE (spec) == FUNCTION_DECL
1497 && uses_template_parms (DECL_TI_ARGS (spec)))
1498 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1499 register it; we want the corresponding TEMPLATE_DECL instead.
1500 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1501 the more obvious `uses_template_parms (spec)' to avoid problems
1502 with default function arguments. In particular, given
1503 something like this:
1504
1505 template <class T> void f(T t1, T t = T())
1506
1507 the default argument expression is not substituted for in an
1508 instantiation unless and until it is actually needed. */
1509 return spec;
1510
1511 if (optimize_specialization_lookup_p (tmpl))
1512 /* We don't put these specializations in the hash table, but we might
1513 want to give an error about a mismatch. */
1514 fn = retrieve_specialization (tmpl, args, 0);
1515 else
1516 {
1517 elt.tmpl = tmpl;
1518 elt.args = args;
1519 elt.spec = spec;
1520
1521 if (hash == 0)
1522 hash = spec_hasher::hash (&elt);
1523
1524 slot =
1525 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1526 if (*slot)
1527 fn = ((spec_entry *) *slot)->spec;
1528 else
1529 fn = NULL_TREE;
1530 }
1531
1532 /* We can sometimes try to re-register a specialization that we've
1533 already got. In particular, regenerate_decl_from_template calls
1534 duplicate_decls which will update the specialization list. But,
1535 we'll still get called again here anyhow. It's more convenient
1536 to simply allow this than to try to prevent it. */
1537 if (fn == spec)
1538 return spec;
1539 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1540 {
1541 if (DECL_TEMPLATE_INSTANTIATION (fn))
1542 {
1543 if (DECL_ODR_USED (fn)
1544 || DECL_EXPLICIT_INSTANTIATION (fn))
1545 {
1546 error ("specialization of %qD after instantiation",
1547 fn);
1548 return error_mark_node;
1549 }
1550 else
1551 {
1552 tree clone;
1553 /* This situation should occur only if the first
1554 specialization is an implicit instantiation, the
1555 second is an explicit specialization, and the
1556 implicit instantiation has not yet been used. That
1557 situation can occur if we have implicitly
1558 instantiated a member function and then specialized
1559 it later.
1560
1561 We can also wind up here if a friend declaration that
1562 looked like an instantiation turns out to be a
1563 specialization:
1564
1565 template <class T> void foo(T);
1566 class S { friend void foo<>(int) };
1567 template <> void foo(int);
1568
1569 We transform the existing DECL in place so that any
1570 pointers to it become pointers to the updated
1571 declaration.
1572
1573 If there was a definition for the template, but not
1574 for the specialization, we want this to look as if
1575 there were no definition, and vice versa. */
1576 DECL_INITIAL (fn) = NULL_TREE;
1577 duplicate_decls (spec, fn, is_friend);
1578 /* The call to duplicate_decls will have applied
1579 [temp.expl.spec]:
1580
1581 An explicit specialization of a function template
1582 is inline only if it is explicitly declared to be,
1583 and independently of whether its function template
1584 is.
1585
1586 to the primary function; now copy the inline bits to
1587 the various clones. */
1588 FOR_EACH_CLONE (clone, fn)
1589 {
1590 DECL_DECLARED_INLINE_P (clone)
1591 = DECL_DECLARED_INLINE_P (fn);
1592 DECL_SOURCE_LOCATION (clone)
1593 = DECL_SOURCE_LOCATION (fn);
1594 DECL_DELETED_FN (clone)
1595 = DECL_DELETED_FN (fn);
1596 }
1597 check_specialization_namespace (tmpl);
1598
1599 return fn;
1600 }
1601 }
1602 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1603 {
1604 tree dd = duplicate_decls (spec, fn, is_friend);
1605 if (dd == error_mark_node)
1606 /* We've already complained in duplicate_decls. */
1607 return error_mark_node;
1608
1609 if (dd == NULL_TREE && DECL_INITIAL (spec))
1610 /* Dup decl failed, but this is a new definition. Set the
1611 line number so any errors match this new
1612 definition. */
1613 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1614
1615 return fn;
1616 }
1617 }
1618 else if (fn)
1619 return duplicate_decls (spec, fn, is_friend);
1620
1621 /* A specialization must be declared in the same namespace as the
1622 template it is specializing. */
1623 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1624 && !check_specialization_namespace (tmpl))
1625 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1626
1627 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1628 {
1629 spec_entry *entry = ggc_alloc<spec_entry> ();
1630 gcc_assert (tmpl && args && spec);
1631 *entry = elt;
1632 *slot = entry;
1633 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1634 && PRIMARY_TEMPLATE_P (tmpl)
1635 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1636 || variable_template_p (tmpl))
1637 /* If TMPL is a forward declaration of a template function, keep a list
1638 of all specializations in case we need to reassign them to a friend
1639 template later in tsubst_friend_function.
1640
1641 Also keep a list of all variable template instantiations so that
1642 process_partial_specialization can check whether a later partial
1643 specialization would have used it. */
1644 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1645 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1646 }
1647
1648 return spec;
1649 }
1650
1651 /* Returns true iff two spec_entry nodes are equivalent. */
1652
1653 int comparing_specializations;
1654
1655 bool
1656 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1657 {
1658 int equal;
1659
1660 ++comparing_specializations;
1661 equal = (e1->tmpl == e2->tmpl
1662 && comp_template_args (e1->args, e2->args));
1663 if (equal && flag_concepts
1664 /* tmpl could be a FIELD_DECL for a capture pack. */
1665 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1666 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1667 && uses_template_parms (e1->args))
1668 {
1669 /* Partial specializations of a variable template can be distinguished by
1670 constraints. */
1671 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1672 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1673 equal = equivalent_constraints (c1, c2);
1674 }
1675 --comparing_specializations;
1676
1677 return equal;
1678 }
1679
1680 /* Returns a hash for a template TMPL and template arguments ARGS. */
1681
1682 static hashval_t
1683 hash_tmpl_and_args (tree tmpl, tree args)
1684 {
1685 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1686 return iterative_hash_template_arg (args, val);
1687 }
1688
1689 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1690 ignoring SPEC. */
1691
1692 hashval_t
1693 spec_hasher::hash (spec_entry *e)
1694 {
1695 return hash_tmpl_and_args (e->tmpl, e->args);
1696 }
1697
1698 /* Recursively calculate a hash value for a template argument ARG, for use
1699 in the hash tables of template specializations. */
1700
1701 hashval_t
1702 iterative_hash_template_arg (tree arg, hashval_t val)
1703 {
1704 unsigned HOST_WIDE_INT i;
1705 enum tree_code code;
1706 char tclass;
1707
1708 if (arg == NULL_TREE)
1709 return iterative_hash_object (arg, val);
1710
1711 if (!TYPE_P (arg))
1712 STRIP_NOPS (arg);
1713
1714 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1715 gcc_unreachable ();
1716
1717 code = TREE_CODE (arg);
1718 tclass = TREE_CODE_CLASS (code);
1719
1720 val = iterative_hash_object (code, val);
1721
1722 switch (code)
1723 {
1724 case ERROR_MARK:
1725 return val;
1726
1727 case IDENTIFIER_NODE:
1728 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1729
1730 case TREE_VEC:
1731 {
1732 int i, len = TREE_VEC_LENGTH (arg);
1733 for (i = 0; i < len; ++i)
1734 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1735 return val;
1736 }
1737
1738 case TYPE_PACK_EXPANSION:
1739 case EXPR_PACK_EXPANSION:
1740 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1741 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1742
1743 case TYPE_ARGUMENT_PACK:
1744 case NONTYPE_ARGUMENT_PACK:
1745 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1746
1747 case TREE_LIST:
1748 for (; arg; arg = TREE_CHAIN (arg))
1749 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1750 return val;
1751
1752 case OVERLOAD:
1753 for (lkp_iterator iter (arg); iter; ++iter)
1754 val = iterative_hash_template_arg (*iter, val);
1755 return val;
1756
1757 case CONSTRUCTOR:
1758 {
1759 tree field, value;
1760 iterative_hash_template_arg (TREE_TYPE (arg), val);
1761 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1762 {
1763 val = iterative_hash_template_arg (field, val);
1764 val = iterative_hash_template_arg (value, val);
1765 }
1766 return val;
1767 }
1768
1769 case PARM_DECL:
1770 if (!DECL_ARTIFICIAL (arg))
1771 {
1772 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1773 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1774 }
1775 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1776
1777 case TARGET_EXPR:
1778 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1779
1780 case PTRMEM_CST:
1781 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1782 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1783
1784 case TEMPLATE_PARM_INDEX:
1785 val = iterative_hash_template_arg
1786 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1787 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1788 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1789
1790 case TRAIT_EXPR:
1791 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1792 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1793 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1794
1795 case BASELINK:
1796 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1797 val);
1798 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1799 val);
1800
1801 case MODOP_EXPR:
1802 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1803 code = TREE_CODE (TREE_OPERAND (arg, 1));
1804 val = iterative_hash_object (code, val);
1805 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1806
1807 case LAMBDA_EXPR:
1808 /* A lambda can't appear in a template arg, but don't crash on
1809 erroneous input. */
1810 gcc_assert (seen_error ());
1811 return val;
1812
1813 case CAST_EXPR:
1814 case IMPLICIT_CONV_EXPR:
1815 case STATIC_CAST_EXPR:
1816 case REINTERPRET_CAST_EXPR:
1817 case CONST_CAST_EXPR:
1818 case DYNAMIC_CAST_EXPR:
1819 case NEW_EXPR:
1820 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1821 /* Now hash operands as usual. */
1822 break;
1823
1824 default:
1825 break;
1826 }
1827
1828 switch (tclass)
1829 {
1830 case tcc_type:
1831 if (alias_template_specialization_p (arg))
1832 {
1833 // We want an alias specialization that survived strip_typedefs
1834 // to hash differently from its TYPE_CANONICAL, to avoid hash
1835 // collisions that compare as different in template_args_equal.
1836 // These could be dependent specializations that strip_typedefs
1837 // left alone, or untouched specializations because
1838 // coerce_template_parms returns the unconverted template
1839 // arguments if it sees incomplete argument packs.
1840 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1841 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1842 }
1843 if (TYPE_CANONICAL (arg))
1844 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1845 val);
1846 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1847 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1848 /* Otherwise just compare the types during lookup. */
1849 return val;
1850
1851 case tcc_declaration:
1852 case tcc_constant:
1853 return iterative_hash_expr (arg, val);
1854
1855 default:
1856 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1857 {
1858 unsigned n = cp_tree_operand_length (arg);
1859 for (i = 0; i < n; ++i)
1860 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1861 return val;
1862 }
1863 }
1864 gcc_unreachable ();
1865 return 0;
1866 }
1867
1868 /* Unregister the specialization SPEC as a specialization of TMPL.
1869 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1870 if the SPEC was listed as a specialization of TMPL.
1871
1872 Note that SPEC has been ggc_freed, so we can't look inside it. */
1873
1874 bool
1875 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1876 {
1877 spec_entry *entry;
1878 spec_entry elt;
1879
1880 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1881 elt.args = TI_ARGS (tinfo);
1882 elt.spec = NULL_TREE;
1883
1884 entry = decl_specializations->find (&elt);
1885 if (entry != NULL)
1886 {
1887 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1888 gcc_assert (new_spec != NULL_TREE);
1889 entry->spec = new_spec;
1890 return 1;
1891 }
1892
1893 return 0;
1894 }
1895
1896 /* Like register_specialization, but for local declarations. We are
1897 registering SPEC, an instantiation of TMPL. */
1898
1899 void
1900 register_local_specialization (tree spec, tree tmpl)
1901 {
1902 gcc_assert (tmpl != spec);
1903 local_specializations->put (tmpl, spec);
1904 }
1905
1906 /* TYPE is a class type. Returns true if TYPE is an explicitly
1907 specialized class. */
1908
1909 bool
1910 explicit_class_specialization_p (tree type)
1911 {
1912 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1913 return false;
1914 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1915 }
1916
1917 /* Print the list of functions at FNS, going through all the overloads
1918 for each element of the list. Alternatively, FNS can not be a
1919 TREE_LIST, in which case it will be printed together with all the
1920 overloads.
1921
1922 MORE and *STR should respectively be FALSE and NULL when the function
1923 is called from the outside. They are used internally on recursive
1924 calls. print_candidates manages the two parameters and leaves NULL
1925 in *STR when it ends. */
1926
1927 static void
1928 print_candidates_1 (tree fns, char **str, bool more = false)
1929 {
1930 if (TREE_CODE (fns) == TREE_LIST)
1931 for (; fns; fns = TREE_CHAIN (fns))
1932 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1933 else
1934 for (lkp_iterator iter (fns); iter;)
1935 {
1936 tree cand = *iter;
1937 ++iter;
1938
1939 const char *pfx = *str;
1940 if (!pfx)
1941 {
1942 if (more || iter)
1943 pfx = _("candidates are:");
1944 else
1945 pfx = _("candidate is:");
1946 *str = get_spaces (pfx);
1947 }
1948 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1949 }
1950 }
1951
1952 /* Print the list of candidate FNS in an error message. FNS can also
1953 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1954
1955 void
1956 print_candidates (tree fns)
1957 {
1958 char *str = NULL;
1959 print_candidates_1 (fns, &str);
1960 free (str);
1961 }
1962
1963 /* Get a (possibly) constrained template declaration for the
1964 purpose of ordering candidates. */
1965 static tree
1966 get_template_for_ordering (tree list)
1967 {
1968 gcc_assert (TREE_CODE (list) == TREE_LIST);
1969 tree f = TREE_VALUE (list);
1970 if (tree ti = DECL_TEMPLATE_INFO (f))
1971 return TI_TEMPLATE (ti);
1972 return f;
1973 }
1974
1975 /* Among candidates having the same signature, return the
1976 most constrained or NULL_TREE if there is no best candidate.
1977 If the signatures of candidates vary (e.g., template
1978 specialization vs. member function), then there can be no
1979 most constrained.
1980
1981 Note that we don't compare constraints on the functions
1982 themselves, but rather those of their templates. */
1983 static tree
1984 most_constrained_function (tree candidates)
1985 {
1986 // Try to find the best candidate in a first pass.
1987 tree champ = candidates;
1988 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1989 {
1990 int winner = more_constrained (get_template_for_ordering (champ),
1991 get_template_for_ordering (c));
1992 if (winner == -1)
1993 champ = c; // The candidate is more constrained
1994 else if (winner == 0)
1995 return NULL_TREE; // Neither is more constrained
1996 }
1997
1998 // Verify that the champ is better than previous candidates.
1999 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2000 if (!more_constrained (get_template_for_ordering (champ),
2001 get_template_for_ordering (c)))
2002 return NULL_TREE;
2003 }
2004
2005 return champ;
2006 }
2007
2008
2009 /* Returns the template (one of the functions given by TEMPLATE_ID)
2010 which can be specialized to match the indicated DECL with the
2011 explicit template args given in TEMPLATE_ID. The DECL may be
2012 NULL_TREE if none is available. In that case, the functions in
2013 TEMPLATE_ID are non-members.
2014
2015 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2016 specialization of a member template.
2017
2018 The TEMPLATE_COUNT is the number of references to qualifying
2019 template classes that appeared in the name of the function. See
2020 check_explicit_specialization for a more accurate description.
2021
2022 TSK indicates what kind of template declaration (if any) is being
2023 declared. TSK_TEMPLATE indicates that the declaration given by
2024 DECL, though a FUNCTION_DECL, has template parameters, and is
2025 therefore a template function.
2026
2027 The template args (those explicitly specified and those deduced)
2028 are output in a newly created vector *TARGS_OUT.
2029
2030 If it is impossible to determine the result, an error message is
2031 issued. The error_mark_node is returned to indicate failure. */
2032
2033 static tree
2034 determine_specialization (tree template_id,
2035 tree decl,
2036 tree* targs_out,
2037 int need_member_template,
2038 int template_count,
2039 tmpl_spec_kind tsk)
2040 {
2041 tree fns;
2042 tree targs;
2043 tree explicit_targs;
2044 tree candidates = NULL_TREE;
2045
2046 /* A TREE_LIST of templates of which DECL may be a specialization.
2047 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2048 corresponding TREE_PURPOSE is the set of template arguments that,
2049 when used to instantiate the template, would produce a function
2050 with the signature of DECL. */
2051 tree templates = NULL_TREE;
2052 int header_count;
2053 cp_binding_level *b;
2054
2055 *targs_out = NULL_TREE;
2056
2057 if (template_id == error_mark_node || decl == error_mark_node)
2058 return error_mark_node;
2059
2060 /* We shouldn't be specializing a member template of an
2061 unspecialized class template; we already gave an error in
2062 check_specialization_scope, now avoid crashing. */
2063 if (!VAR_P (decl)
2064 && template_count && DECL_CLASS_SCOPE_P (decl)
2065 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2066 {
2067 gcc_assert (errorcount);
2068 return error_mark_node;
2069 }
2070
2071 fns = TREE_OPERAND (template_id, 0);
2072 explicit_targs = TREE_OPERAND (template_id, 1);
2073
2074 if (fns == error_mark_node)
2075 return error_mark_node;
2076
2077 /* Check for baselinks. */
2078 if (BASELINK_P (fns))
2079 fns = BASELINK_FUNCTIONS (fns);
2080
2081 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2082 {
2083 error ("%qD is not a function template", fns);
2084 return error_mark_node;
2085 }
2086 else if (VAR_P (decl) && !variable_template_p (fns))
2087 {
2088 error ("%qD is not a variable template", fns);
2089 return error_mark_node;
2090 }
2091
2092 /* Count the number of template headers specified for this
2093 specialization. */
2094 header_count = 0;
2095 for (b = current_binding_level;
2096 b->kind == sk_template_parms;
2097 b = b->level_chain)
2098 ++header_count;
2099
2100 tree orig_fns = fns;
2101
2102 if (variable_template_p (fns))
2103 {
2104 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2105 targs = coerce_template_parms (parms, explicit_targs, fns,
2106 tf_warning_or_error,
2107 /*req_all*/true, /*use_defarg*/true);
2108 if (targs != error_mark_node)
2109 templates = tree_cons (targs, fns, templates);
2110 }
2111 else for (lkp_iterator iter (fns); iter; ++iter)
2112 {
2113 tree fn = *iter;
2114
2115 if (TREE_CODE (fn) == TEMPLATE_DECL)
2116 {
2117 tree decl_arg_types;
2118 tree fn_arg_types;
2119 tree insttype;
2120
2121 /* In case of explicit specialization, we need to check if
2122 the number of template headers appearing in the specialization
2123 is correct. This is usually done in check_explicit_specialization,
2124 but the check done there cannot be exhaustive when specializing
2125 member functions. Consider the following code:
2126
2127 template <> void A<int>::f(int);
2128 template <> template <> void A<int>::f(int);
2129
2130 Assuming that A<int> is not itself an explicit specialization
2131 already, the first line specializes "f" which is a non-template
2132 member function, whilst the second line specializes "f" which
2133 is a template member function. So both lines are syntactically
2134 correct, and check_explicit_specialization does not reject
2135 them.
2136
2137 Here, we can do better, as we are matching the specialization
2138 against the declarations. We count the number of template
2139 headers, and we check if they match TEMPLATE_COUNT + 1
2140 (TEMPLATE_COUNT is the number of qualifying template classes,
2141 plus there must be another header for the member template
2142 itself).
2143
2144 Notice that if header_count is zero, this is not a
2145 specialization but rather a template instantiation, so there
2146 is no check we can perform here. */
2147 if (header_count && header_count != template_count + 1)
2148 continue;
2149
2150 /* Check that the number of template arguments at the
2151 innermost level for DECL is the same as for FN. */
2152 if (current_binding_level->kind == sk_template_parms
2153 && !current_binding_level->explicit_spec_p
2154 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2155 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2156 (current_template_parms))))
2157 continue;
2158
2159 /* DECL might be a specialization of FN. */
2160 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2161 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2162
2163 /* For a non-static member function, we need to make sure
2164 that the const qualification is the same. Since
2165 get_bindings does not try to merge the "this" parameter,
2166 we must do the comparison explicitly. */
2167 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2168 {
2169 if (!same_type_p (TREE_VALUE (fn_arg_types),
2170 TREE_VALUE (decl_arg_types)))
2171 continue;
2172
2173 /* And the ref-qualification. */
2174 if (type_memfn_rqual (TREE_TYPE (decl))
2175 != type_memfn_rqual (TREE_TYPE (fn)))
2176 continue;
2177 }
2178
2179 /* Skip the "this" parameter and, for constructors of
2180 classes with virtual bases, the VTT parameter. A
2181 full specialization of a constructor will have a VTT
2182 parameter, but a template never will. */
2183 decl_arg_types
2184 = skip_artificial_parms_for (decl, decl_arg_types);
2185 fn_arg_types
2186 = skip_artificial_parms_for (fn, fn_arg_types);
2187
2188 /* Function templates cannot be specializations; there are
2189 no partial specializations of functions. Therefore, if
2190 the type of DECL does not match FN, there is no
2191 match.
2192
2193 Note that it should never be the case that we have both
2194 candidates added here, and for regular member functions
2195 below. */
2196 if (tsk == tsk_template)
2197 {
2198 if (compparms (fn_arg_types, decl_arg_types))
2199 candidates = tree_cons (NULL_TREE, fn, candidates);
2200 continue;
2201 }
2202
2203 /* See whether this function might be a specialization of this
2204 template. Suppress access control because we might be trying
2205 to make this specialization a friend, and we have already done
2206 access control for the declaration of the specialization. */
2207 push_deferring_access_checks (dk_no_check);
2208 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2209 pop_deferring_access_checks ();
2210
2211 if (!targs)
2212 /* We cannot deduce template arguments that when used to
2213 specialize TMPL will produce DECL. */
2214 continue;
2215
2216 if (uses_template_parms (targs))
2217 /* We deduced something involving 'auto', which isn't a valid
2218 template argument. */
2219 continue;
2220
2221 /* Remove, from the set of candidates, all those functions
2222 whose constraints are not satisfied. */
2223 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2224 continue;
2225
2226 // Then, try to form the new function type.
2227 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2228 if (insttype == error_mark_node)
2229 continue;
2230 fn_arg_types
2231 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2232 if (!compparms (fn_arg_types, decl_arg_types))
2233 continue;
2234
2235 /* Save this template, and the arguments deduced. */
2236 templates = tree_cons (targs, fn, templates);
2237 }
2238 else if (need_member_template)
2239 /* FN is an ordinary member function, and we need a
2240 specialization of a member template. */
2241 ;
2242 else if (TREE_CODE (fn) != FUNCTION_DECL)
2243 /* We can get IDENTIFIER_NODEs here in certain erroneous
2244 cases. */
2245 ;
2246 else if (!DECL_FUNCTION_MEMBER_P (fn))
2247 /* This is just an ordinary non-member function. Nothing can
2248 be a specialization of that. */
2249 ;
2250 else if (DECL_ARTIFICIAL (fn))
2251 /* Cannot specialize functions that are created implicitly. */
2252 ;
2253 else
2254 {
2255 tree decl_arg_types;
2256
2257 /* This is an ordinary member function. However, since
2258 we're here, we can assume its enclosing class is a
2259 template class. For example,
2260
2261 template <typename T> struct S { void f(); };
2262 template <> void S<int>::f() {}
2263
2264 Here, S<int>::f is a non-template, but S<int> is a
2265 template class. If FN has the same type as DECL, we
2266 might be in business. */
2267
2268 if (!DECL_TEMPLATE_INFO (fn))
2269 /* Its enclosing class is an explicit specialization
2270 of a template class. This is not a candidate. */
2271 continue;
2272
2273 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2274 TREE_TYPE (TREE_TYPE (fn))))
2275 /* The return types differ. */
2276 continue;
2277
2278 /* Adjust the type of DECL in case FN is a static member. */
2279 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2280 if (DECL_STATIC_FUNCTION_P (fn)
2281 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2282 decl_arg_types = TREE_CHAIN (decl_arg_types);
2283
2284 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2285 decl_arg_types))
2286 continue;
2287
2288 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2289 && (type_memfn_rqual (TREE_TYPE (decl))
2290 != type_memfn_rqual (TREE_TYPE (fn))))
2291 continue;
2292
2293 // If the deduced arguments do not satisfy the constraints,
2294 // this is not a candidate.
2295 if (flag_concepts && !constraints_satisfied_p (fn))
2296 continue;
2297
2298 // Add the candidate.
2299 candidates = tree_cons (NULL_TREE, fn, candidates);
2300 }
2301 }
2302
2303 if (templates && TREE_CHAIN (templates))
2304 {
2305 /* We have:
2306
2307 [temp.expl.spec]
2308
2309 It is possible for a specialization with a given function
2310 signature to be instantiated from more than one function
2311 template. In such cases, explicit specification of the
2312 template arguments must be used to uniquely identify the
2313 function template specialization being specialized.
2314
2315 Note that here, there's no suggestion that we're supposed to
2316 determine which of the candidate templates is most
2317 specialized. However, we, also have:
2318
2319 [temp.func.order]
2320
2321 Partial ordering of overloaded function template
2322 declarations is used in the following contexts to select
2323 the function template to which a function template
2324 specialization refers:
2325
2326 -- when an explicit specialization refers to a function
2327 template.
2328
2329 So, we do use the partial ordering rules, at least for now.
2330 This extension can only serve to make invalid programs valid,
2331 so it's safe. And, there is strong anecdotal evidence that
2332 the committee intended the partial ordering rules to apply;
2333 the EDG front end has that behavior, and John Spicer claims
2334 that the committee simply forgot to delete the wording in
2335 [temp.expl.spec]. */
2336 tree tmpl = most_specialized_instantiation (templates);
2337 if (tmpl != error_mark_node)
2338 {
2339 templates = tmpl;
2340 TREE_CHAIN (templates) = NULL_TREE;
2341 }
2342 }
2343
2344 // Concepts allows multiple declarations of member functions
2345 // with the same signature. Like above, we need to rely on
2346 // on the partial ordering of those candidates to determine which
2347 // is the best.
2348 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2349 {
2350 if (tree cand = most_constrained_function (candidates))
2351 {
2352 candidates = cand;
2353 TREE_CHAIN (cand) = NULL_TREE;
2354 }
2355 }
2356
2357 if (templates == NULL_TREE && candidates == NULL_TREE)
2358 {
2359 error ("template-id %qD for %q+D does not match any template "
2360 "declaration", template_id, decl);
2361 if (header_count && header_count != template_count + 1)
2362 inform (input_location, "saw %d %<template<>%>, need %d for "
2363 "specializing a member function template",
2364 header_count, template_count + 1);
2365 else
2366 print_candidates (orig_fns);
2367 return error_mark_node;
2368 }
2369 else if ((templates && TREE_CHAIN (templates))
2370 || (candidates && TREE_CHAIN (candidates))
2371 || (templates && candidates))
2372 {
2373 error ("ambiguous template specialization %qD for %q+D",
2374 template_id, decl);
2375 candidates = chainon (candidates, templates);
2376 print_candidates (candidates);
2377 return error_mark_node;
2378 }
2379
2380 /* We have one, and exactly one, match. */
2381 if (candidates)
2382 {
2383 tree fn = TREE_VALUE (candidates);
2384 *targs_out = copy_node (DECL_TI_ARGS (fn));
2385
2386 // Propagate the candidate's constraints to the declaration.
2387 set_constraints (decl, get_constraints (fn));
2388
2389 /* DECL is a re-declaration or partial instantiation of a template
2390 function. */
2391 if (TREE_CODE (fn) == TEMPLATE_DECL)
2392 return fn;
2393 /* It was a specialization of an ordinary member function in a
2394 template class. */
2395 return DECL_TI_TEMPLATE (fn);
2396 }
2397
2398 /* It was a specialization of a template. */
2399 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2400 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2401 {
2402 *targs_out = copy_node (targs);
2403 SET_TMPL_ARGS_LEVEL (*targs_out,
2404 TMPL_ARGS_DEPTH (*targs_out),
2405 TREE_PURPOSE (templates));
2406 }
2407 else
2408 *targs_out = TREE_PURPOSE (templates);
2409 return TREE_VALUE (templates);
2410 }
2411
2412 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2413 but with the default argument values filled in from those in the
2414 TMPL_TYPES. */
2415
2416 static tree
2417 copy_default_args_to_explicit_spec_1 (tree spec_types,
2418 tree tmpl_types)
2419 {
2420 tree new_spec_types;
2421
2422 if (!spec_types)
2423 return NULL_TREE;
2424
2425 if (spec_types == void_list_node)
2426 return void_list_node;
2427
2428 /* Substitute into the rest of the list. */
2429 new_spec_types =
2430 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2431 TREE_CHAIN (tmpl_types));
2432
2433 /* Add the default argument for this parameter. */
2434 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2435 TREE_VALUE (spec_types),
2436 new_spec_types);
2437 }
2438
2439 /* DECL is an explicit specialization. Replicate default arguments
2440 from the template it specializes. (That way, code like:
2441
2442 template <class T> void f(T = 3);
2443 template <> void f(double);
2444 void g () { f (); }
2445
2446 works, as required.) An alternative approach would be to look up
2447 the correct default arguments at the call-site, but this approach
2448 is consistent with how implicit instantiations are handled. */
2449
2450 static void
2451 copy_default_args_to_explicit_spec (tree decl)
2452 {
2453 tree tmpl;
2454 tree spec_types;
2455 tree tmpl_types;
2456 tree new_spec_types;
2457 tree old_type;
2458 tree new_type;
2459 tree t;
2460 tree object_type = NULL_TREE;
2461 tree in_charge = NULL_TREE;
2462 tree vtt = NULL_TREE;
2463
2464 /* See if there's anything we need to do. */
2465 tmpl = DECL_TI_TEMPLATE (decl);
2466 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2467 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2468 if (TREE_PURPOSE (t))
2469 break;
2470 if (!t)
2471 return;
2472
2473 old_type = TREE_TYPE (decl);
2474 spec_types = TYPE_ARG_TYPES (old_type);
2475
2476 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2477 {
2478 /* Remove the this pointer, but remember the object's type for
2479 CV quals. */
2480 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2481 spec_types = TREE_CHAIN (spec_types);
2482 tmpl_types = TREE_CHAIN (tmpl_types);
2483
2484 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2485 {
2486 /* DECL may contain more parameters than TMPL due to the extra
2487 in-charge parameter in constructors and destructors. */
2488 in_charge = spec_types;
2489 spec_types = TREE_CHAIN (spec_types);
2490 }
2491 if (DECL_HAS_VTT_PARM_P (decl))
2492 {
2493 vtt = spec_types;
2494 spec_types = TREE_CHAIN (spec_types);
2495 }
2496 }
2497
2498 /* Compute the merged default arguments. */
2499 new_spec_types =
2500 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2501
2502 /* Compute the new FUNCTION_TYPE. */
2503 if (object_type)
2504 {
2505 if (vtt)
2506 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2507 TREE_VALUE (vtt),
2508 new_spec_types);
2509
2510 if (in_charge)
2511 /* Put the in-charge parameter back. */
2512 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2513 TREE_VALUE (in_charge),
2514 new_spec_types);
2515
2516 new_type = build_method_type_directly (object_type,
2517 TREE_TYPE (old_type),
2518 new_spec_types);
2519 }
2520 else
2521 new_type = build_function_type (TREE_TYPE (old_type),
2522 new_spec_types);
2523 new_type = cp_build_type_attribute_variant (new_type,
2524 TYPE_ATTRIBUTES (old_type));
2525 new_type = build_exception_variant (new_type,
2526 TYPE_RAISES_EXCEPTIONS (old_type));
2527
2528 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2529 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2530
2531 TREE_TYPE (decl) = new_type;
2532 }
2533
2534 /* Return the number of template headers we expect to see for a definition
2535 or specialization of CTYPE or one of its non-template members. */
2536
2537 int
2538 num_template_headers_for_class (tree ctype)
2539 {
2540 int num_templates = 0;
2541
2542 while (ctype && CLASS_TYPE_P (ctype))
2543 {
2544 /* You're supposed to have one `template <...>' for every
2545 template class, but you don't need one for a full
2546 specialization. For example:
2547
2548 template <class T> struct S{};
2549 template <> struct S<int> { void f(); };
2550 void S<int>::f () {}
2551
2552 is correct; there shouldn't be a `template <>' for the
2553 definition of `S<int>::f'. */
2554 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2555 /* If CTYPE does not have template information of any
2556 kind, then it is not a template, nor is it nested
2557 within a template. */
2558 break;
2559 if (explicit_class_specialization_p (ctype))
2560 break;
2561 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2562 ++num_templates;
2563
2564 ctype = TYPE_CONTEXT (ctype);
2565 }
2566
2567 return num_templates;
2568 }
2569
2570 /* Do a simple sanity check on the template headers that precede the
2571 variable declaration DECL. */
2572
2573 void
2574 check_template_variable (tree decl)
2575 {
2576 tree ctx = CP_DECL_CONTEXT (decl);
2577 int wanted = num_template_headers_for_class (ctx);
2578 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2579 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2580 {
2581 if (cxx_dialect < cxx14)
2582 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2583 "variable templates only available with "
2584 "-std=c++14 or -std=gnu++14");
2585
2586 // Namespace-scope variable templates should have a template header.
2587 ++wanted;
2588 }
2589 if (template_header_count > wanted)
2590 {
2591 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2592 "too many template headers for %qD "
2593 "(should be %d)",
2594 decl, wanted);
2595 if (warned && CLASS_TYPE_P (ctx)
2596 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2597 inform (DECL_SOURCE_LOCATION (decl),
2598 "members of an explicitly specialized class are defined "
2599 "without a template header");
2600 }
2601 }
2602
2603 /* An explicit specialization whose declarator-id or class-head-name is not
2604 qualified shall be declared in the nearest enclosing namespace of the
2605 template, or, if the namespace is inline (7.3.1), any namespace from its
2606 enclosing namespace set.
2607
2608 If the name declared in the explicit instantiation is an unqualified name,
2609 the explicit instantiation shall appear in the namespace where its template
2610 is declared or, if that namespace is inline (7.3.1), any namespace from its
2611 enclosing namespace set. */
2612
2613 void
2614 check_unqualified_spec_or_inst (tree t, location_t loc)
2615 {
2616 tree tmpl = most_general_template (t);
2617 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2618 && !is_nested_namespace (current_namespace,
2619 CP_DECL_CONTEXT (tmpl), true))
2620 {
2621 if (processing_specialization)
2622 permerror (loc, "explicit specialization of %qD outside its "
2623 "namespace must use a nested-name-specifier", tmpl);
2624 else if (processing_explicit_instantiation
2625 && cxx_dialect >= cxx11)
2626 /* This was allowed in C++98, so only pedwarn. */
2627 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2628 "outside its namespace must use a nested-name-"
2629 "specifier", tmpl);
2630 }
2631 }
2632
2633 /* Warn for a template specialization SPEC that is missing some of a set
2634 of function or type attributes that the template TEMPL is declared with.
2635 ATTRLIST is a list of additional attributes that SPEC should be taken
2636 to ultimately be declared with. */
2637
2638 static void
2639 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2640 {
2641 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2642 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2643
2644 if (TREE_CODE (tmpl) != FUNCTION_DECL)
2645 return;
2646
2647 /* Avoid warning if either declaration or its type is deprecated. */
2648 if (TREE_DEPRECATED (tmpl)
2649 || TREE_DEPRECATED (spec))
2650 return;
2651
2652 tree tmpl_type = TREE_TYPE (tmpl);
2653 tree spec_type = TREE_TYPE (spec);
2654
2655 if (TREE_DEPRECATED (tmpl_type)
2656 || TREE_DEPRECATED (spec_type)
2657 || TREE_DEPRECATED (TREE_TYPE (tmpl_type))
2658 || TREE_DEPRECATED (TREE_TYPE (spec_type)))
2659 return;
2660
2661 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpl_type) };
2662 tree spec_attrs[] = { DECL_ATTRIBUTES (spec), TYPE_ATTRIBUTES (spec_type) };
2663
2664 if (!spec_attrs[0])
2665 spec_attrs[0] = attrlist;
2666 else if (!spec_attrs[1])
2667 spec_attrs[1] = attrlist;
2668
2669 /* Avoid warning if the primary has no attributes. */
2670 if (!tmpl_attrs[0] && !tmpl_attrs[1])
2671 return;
2672
2673 /* Avoid warning if either declaration contains an attribute on
2674 the white list below. */
2675 const char* const whitelist[] = {
2676 "error", "warning"
2677 };
2678
2679 for (unsigned i = 0; i != 2; ++i)
2680 for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j)
2681 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
2682 || lookup_attribute (whitelist[j], spec_attrs[i]))
2683 return;
2684
2685 /* Avoid warning if the difference between the primary and
2686 the specialization is not in one of the attributes below. */
2687 const char* const blacklist[] = {
2688 "alloc_align", "alloc_size", "assume_aligned", "format",
2689 "format_arg", "malloc", "nonnull"
2690 };
2691
2692 /* Put together a list of the black listed attributes that the primary
2693 template is declared with that the specialization is not, in case
2694 it's not apparent from the most recent declaration of the primary. */
2695 unsigned nattrs = 0;
2696 pretty_printer str;
2697
2698 for (unsigned i = 0; i != sizeof blacklist / sizeof *blacklist; ++i)
2699 {
2700 for (unsigned j = 0; j != 2; ++j)
2701 {
2702 if (!lookup_attribute (blacklist[i], tmpl_attrs[j]))
2703 continue;
2704
2705 for (unsigned k = 0; k != 1 + !!spec_attrs[1]; ++k)
2706 {
2707 if (lookup_attribute (blacklist[i], spec_attrs[k]))
2708 break;
2709
2710 if (nattrs)
2711 pp_string (&str, ", ");
2712 pp_begin_quote (&str, pp_show_color (global_dc->printer));
2713 pp_string (&str, blacklist[i]);
2714 pp_end_quote (&str, pp_show_color (global_dc->printer));
2715 ++nattrs;
2716 }
2717 }
2718 }
2719
2720 if (!nattrs)
2721 return;
2722
2723 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2724 "explicit specialization %q#D may be missing attributes",
2725 spec))
2726 inform (DECL_SOURCE_LOCATION (tmpl),
2727 nattrs > 1
2728 ? G_("missing primary template attributes %s")
2729 : G_("missing primary template attribute %s"),
2730 pp_formatted_text (&str));
2731 }
2732
2733 /* Check to see if the function just declared, as indicated in
2734 DECLARATOR, and in DECL, is a specialization of a function
2735 template. We may also discover that the declaration is an explicit
2736 instantiation at this point.
2737
2738 Returns DECL, or an equivalent declaration that should be used
2739 instead if all goes well. Issues an error message if something is
2740 amiss. Returns error_mark_node if the error is not easily
2741 recoverable.
2742
2743 FLAGS is a bitmask consisting of the following flags:
2744
2745 2: The function has a definition.
2746 4: The function is a friend.
2747
2748 The TEMPLATE_COUNT is the number of references to qualifying
2749 template classes that appeared in the name of the function. For
2750 example, in
2751
2752 template <class T> struct S { void f(); };
2753 void S<int>::f();
2754
2755 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2756 classes are not counted in the TEMPLATE_COUNT, so that in
2757
2758 template <class T> struct S {};
2759 template <> struct S<int> { void f(); }
2760 template <> void S<int>::f();
2761
2762 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2763 invalid; there should be no template <>.)
2764
2765 If the function is a specialization, it is marked as such via
2766 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2767 is set up correctly, and it is added to the list of specializations
2768 for that template. */
2769
2770 tree
2771 check_explicit_specialization (tree declarator,
2772 tree decl,
2773 int template_count,
2774 int flags,
2775 tree attrlist)
2776 {
2777 int have_def = flags & 2;
2778 int is_friend = flags & 4;
2779 bool is_concept = flags & 8;
2780 int specialization = 0;
2781 int explicit_instantiation = 0;
2782 int member_specialization = 0;
2783 tree ctype = DECL_CLASS_CONTEXT (decl);
2784 tree dname = DECL_NAME (decl);
2785 tmpl_spec_kind tsk;
2786
2787 if (is_friend)
2788 {
2789 if (!processing_specialization)
2790 tsk = tsk_none;
2791 else
2792 tsk = tsk_excessive_parms;
2793 }
2794 else
2795 tsk = current_tmpl_spec_kind (template_count);
2796
2797 switch (tsk)
2798 {
2799 case tsk_none:
2800 if (processing_specialization && !VAR_P (decl))
2801 {
2802 specialization = 1;
2803 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2804 }
2805 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2806 {
2807 if (is_friend)
2808 /* This could be something like:
2809
2810 template <class T> void f(T);
2811 class S { friend void f<>(int); } */
2812 specialization = 1;
2813 else
2814 {
2815 /* This case handles bogus declarations like template <>
2816 template <class T> void f<int>(); */
2817
2818 error ("template-id %qD in declaration of primary template",
2819 declarator);
2820 return decl;
2821 }
2822 }
2823 break;
2824
2825 case tsk_invalid_member_spec:
2826 /* The error has already been reported in
2827 check_specialization_scope. */
2828 return error_mark_node;
2829
2830 case tsk_invalid_expl_inst:
2831 error ("template parameter list used in explicit instantiation");
2832
2833 /* Fall through. */
2834
2835 case tsk_expl_inst:
2836 if (have_def)
2837 error ("definition provided for explicit instantiation");
2838
2839 explicit_instantiation = 1;
2840 break;
2841
2842 case tsk_excessive_parms:
2843 case tsk_insufficient_parms:
2844 if (tsk == tsk_excessive_parms)
2845 error ("too many template parameter lists in declaration of %qD",
2846 decl);
2847 else if (template_header_count)
2848 error("too few template parameter lists in declaration of %qD", decl);
2849 else
2850 error("explicit specialization of %qD must be introduced by "
2851 "%<template <>%>", decl);
2852
2853 /* Fall through. */
2854 case tsk_expl_spec:
2855 if (is_concept)
2856 error ("explicit specialization declared %<concept%>");
2857
2858 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2859 /* In cases like template<> constexpr bool v = true;
2860 We'll give an error in check_template_variable. */
2861 break;
2862
2863 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2864 if (ctype)
2865 member_specialization = 1;
2866 else
2867 specialization = 1;
2868 break;
2869
2870 case tsk_template:
2871 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2872 {
2873 /* This case handles bogus declarations like template <>
2874 template <class T> void f<int>(); */
2875
2876 if (!uses_template_parms (declarator))
2877 error ("template-id %qD in declaration of primary template",
2878 declarator);
2879 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2880 {
2881 /* Partial specialization of variable template. */
2882 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2883 specialization = 1;
2884 goto ok;
2885 }
2886 else if (cxx_dialect < cxx14)
2887 error ("non-type partial specialization %qD "
2888 "is not allowed", declarator);
2889 else
2890 error ("non-class, non-variable partial specialization %qD "
2891 "is not allowed", declarator);
2892 return decl;
2893 ok:;
2894 }
2895
2896 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2897 /* This is a specialization of a member template, without
2898 specialization the containing class. Something like:
2899
2900 template <class T> struct S {
2901 template <class U> void f (U);
2902 };
2903 template <> template <class U> void S<int>::f(U) {}
2904
2905 That's a specialization -- but of the entire template. */
2906 specialization = 1;
2907 break;
2908
2909 default:
2910 gcc_unreachable ();
2911 }
2912
2913 if ((specialization || member_specialization)
2914 /* This doesn't apply to variable templates. */
2915 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2916 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2917 {
2918 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2919 for (; t; t = TREE_CHAIN (t))
2920 if (TREE_PURPOSE (t))
2921 {
2922 permerror (input_location,
2923 "default argument specified in explicit specialization");
2924 break;
2925 }
2926 }
2927
2928 if (specialization || member_specialization || explicit_instantiation)
2929 {
2930 tree tmpl = NULL_TREE;
2931 tree targs = NULL_TREE;
2932 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2933
2934 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2935 if (!was_template_id)
2936 {
2937 tree fns;
2938
2939 gcc_assert (identifier_p (declarator));
2940 if (ctype)
2941 fns = dname;
2942 else
2943 {
2944 /* If there is no class context, the explicit instantiation
2945 must be at namespace scope. */
2946 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2947
2948 /* Find the namespace binding, using the declaration
2949 context. */
2950 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2951 false, true);
2952 if (fns == error_mark_node)
2953 /* If lookup fails, look for a friend declaration so we can
2954 give a better diagnostic. */
2955 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2956 /*type*/false, /*complain*/true,
2957 /*hidden*/true);
2958
2959 if (fns == error_mark_node || !is_overloaded_fn (fns))
2960 {
2961 error ("%qD is not a template function", dname);
2962 fns = error_mark_node;
2963 }
2964 }
2965
2966 declarator = lookup_template_function (fns, NULL_TREE);
2967 }
2968
2969 if (declarator == error_mark_node)
2970 return error_mark_node;
2971
2972 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2973 {
2974 if (!explicit_instantiation)
2975 /* A specialization in class scope. This is invalid,
2976 but the error will already have been flagged by
2977 check_specialization_scope. */
2978 return error_mark_node;
2979 else
2980 {
2981 /* It's not valid to write an explicit instantiation in
2982 class scope, e.g.:
2983
2984 class C { template void f(); }
2985
2986 This case is caught by the parser. However, on
2987 something like:
2988
2989 template class C { void f(); };
2990
2991 (which is invalid) we can get here. The error will be
2992 issued later. */
2993 ;
2994 }
2995
2996 return decl;
2997 }
2998 else if (ctype != NULL_TREE
2999 && (identifier_p (TREE_OPERAND (declarator, 0))))
3000 {
3001 // We'll match variable templates in start_decl.
3002 if (VAR_P (decl))
3003 return decl;
3004
3005 /* Find the list of functions in ctype that have the same
3006 name as the declared function. */
3007 tree name = TREE_OPERAND (declarator, 0);
3008
3009 if (constructor_name_p (name, ctype))
3010 {
3011 if (DECL_CONSTRUCTOR_P (decl)
3012 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3013 : !CLASSTYPE_DESTRUCTOR (ctype))
3014 {
3015 /* From [temp.expl.spec]:
3016
3017 If such an explicit specialization for the member
3018 of a class template names an implicitly-declared
3019 special member function (clause _special_), the
3020 program is ill-formed.
3021
3022 Similar language is found in [temp.explicit]. */
3023 error ("specialization of implicitly-declared special member function");
3024 return error_mark_node;
3025 }
3026
3027 name = DECL_NAME (decl);
3028 }
3029
3030 /* For a type-conversion operator, We might be looking for
3031 `operator int' which will be a specialization of
3032 `operator T'. Grab all the conversion operators, and
3033 then select from them. */
3034 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3035 ? conv_op_identifier : name);
3036
3037 if (fns == NULL_TREE)
3038 {
3039 error ("no member function %qD declared in %qT", name, ctype);
3040 return error_mark_node;
3041 }
3042 else
3043 TREE_OPERAND (declarator, 0) = fns;
3044 }
3045
3046 /* Figure out what exactly is being specialized at this point.
3047 Note that for an explicit instantiation, even one for a
3048 member function, we cannot tell a priori whether the
3049 instantiation is for a member template, or just a member
3050 function of a template class. Even if a member template is
3051 being instantiated, the member template arguments may be
3052 elided if they can be deduced from the rest of the
3053 declaration. */
3054 tmpl = determine_specialization (declarator, decl,
3055 &targs,
3056 member_specialization,
3057 template_count,
3058 tsk);
3059
3060 if (!tmpl || tmpl == error_mark_node)
3061 /* We couldn't figure out what this declaration was
3062 specializing. */
3063 return error_mark_node;
3064 else
3065 {
3066 if (TREE_CODE (decl) == FUNCTION_DECL
3067 && DECL_HIDDEN_FRIEND_P (tmpl))
3068 {
3069 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3070 "friend declaration %qD is not visible to "
3071 "explicit specialization", tmpl))
3072 inform (DECL_SOURCE_LOCATION (tmpl),
3073 "friend declaration here");
3074 }
3075 else if (!ctype && !is_friend
3076 && CP_DECL_CONTEXT (decl) == current_namespace)
3077 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3078
3079 tree gen_tmpl = most_general_template (tmpl);
3080
3081 if (explicit_instantiation)
3082 {
3083 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3084 is done by do_decl_instantiation later. */
3085
3086 int arg_depth = TMPL_ARGS_DEPTH (targs);
3087 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3088
3089 if (arg_depth > parm_depth)
3090 {
3091 /* If TMPL is not the most general template (for
3092 example, if TMPL is a friend template that is
3093 injected into namespace scope), then there will
3094 be too many levels of TARGS. Remove some of them
3095 here. */
3096 int i;
3097 tree new_targs;
3098
3099 new_targs = make_tree_vec (parm_depth);
3100 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3101 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3102 = TREE_VEC_ELT (targs, i);
3103 targs = new_targs;
3104 }
3105
3106 return instantiate_template (tmpl, targs, tf_error);
3107 }
3108
3109 /* If we thought that the DECL was a member function, but it
3110 turns out to be specializing a static member function,
3111 make DECL a static member function as well. */
3112 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3113 && DECL_STATIC_FUNCTION_P (tmpl)
3114 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3115 revert_static_member_fn (decl);
3116
3117 /* If this is a specialization of a member template of a
3118 template class, we want to return the TEMPLATE_DECL, not
3119 the specialization of it. */
3120 if (tsk == tsk_template && !was_template_id)
3121 {
3122 tree result = DECL_TEMPLATE_RESULT (tmpl);
3123 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3124 DECL_INITIAL (result) = NULL_TREE;
3125 if (have_def)
3126 {
3127 tree parm;
3128 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3129 DECL_SOURCE_LOCATION (result)
3130 = DECL_SOURCE_LOCATION (decl);
3131 /* We want to use the argument list specified in the
3132 definition, not in the original declaration. */
3133 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3134 for (parm = DECL_ARGUMENTS (result); parm;
3135 parm = DECL_CHAIN (parm))
3136 DECL_CONTEXT (parm) = result;
3137 }
3138 return register_specialization (tmpl, gen_tmpl, targs,
3139 is_friend, 0);
3140 }
3141
3142 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3143 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3144
3145 if (was_template_id)
3146 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3147
3148 /* Inherit default function arguments from the template
3149 DECL is specializing. */
3150 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3151 copy_default_args_to_explicit_spec (decl);
3152
3153 /* This specialization has the same protection as the
3154 template it specializes. */
3155 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3156 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3157
3158 /* 7.1.1-1 [dcl.stc]
3159
3160 A storage-class-specifier shall not be specified in an
3161 explicit specialization...
3162
3163 The parser rejects these, so unless action is taken here,
3164 explicit function specializations will always appear with
3165 global linkage.
3166
3167 The action recommended by the C++ CWG in response to C++
3168 defect report 605 is to make the storage class and linkage
3169 of the explicit specialization match the templated function:
3170
3171 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3172 */
3173 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3174 {
3175 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3176 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3177
3178 /* A concept cannot be specialized. */
3179 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3180 {
3181 error ("explicit specialization of function concept %qD",
3182 gen_tmpl);
3183 return error_mark_node;
3184 }
3185
3186 /* This specialization has the same linkage and visibility as
3187 the function template it specializes. */
3188 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3189 if (! TREE_PUBLIC (decl))
3190 {
3191 DECL_INTERFACE_KNOWN (decl) = 1;
3192 DECL_NOT_REALLY_EXTERN (decl) = 1;
3193 }
3194 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3195 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3196 {
3197 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3198 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3199 }
3200 }
3201
3202 /* If DECL is a friend declaration, declared using an
3203 unqualified name, the namespace associated with DECL may
3204 have been set incorrectly. For example, in:
3205
3206 template <typename T> void f(T);
3207 namespace N {
3208 struct S { friend void f<int>(int); }
3209 }
3210
3211 we will have set the DECL_CONTEXT for the friend
3212 declaration to N, rather than to the global namespace. */
3213 if (DECL_NAMESPACE_SCOPE_P (decl))
3214 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3215
3216 if (is_friend && !have_def)
3217 /* This is not really a declaration of a specialization.
3218 It's just the name of an instantiation. But, it's not
3219 a request for an instantiation, either. */
3220 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3221 else if (TREE_CODE (decl) == FUNCTION_DECL)
3222 /* A specialization is not necessarily COMDAT. */
3223 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3224 && DECL_DECLARED_INLINE_P (decl));
3225 else if (VAR_P (decl))
3226 DECL_COMDAT (decl) = false;
3227
3228 /* If this is a full specialization, register it so that we can find
3229 it again. Partial specializations will be registered in
3230 process_partial_specialization. */
3231 if (!processing_template_decl)
3232 {
3233 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3234
3235 decl = register_specialization (decl, gen_tmpl, targs,
3236 is_friend, 0);
3237 }
3238
3239
3240 /* A 'structor should already have clones. */
3241 gcc_assert (decl == error_mark_node
3242 || variable_template_p (tmpl)
3243 || !(DECL_CONSTRUCTOR_P (decl)
3244 || DECL_DESTRUCTOR_P (decl))
3245 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3246 }
3247 }
3248
3249 return decl;
3250 }
3251
3252 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3253 parameters. These are represented in the same format used for
3254 DECL_TEMPLATE_PARMS. */
3255
3256 int
3257 comp_template_parms (const_tree parms1, const_tree parms2)
3258 {
3259 const_tree p1;
3260 const_tree p2;
3261
3262 if (parms1 == parms2)
3263 return 1;
3264
3265 for (p1 = parms1, p2 = parms2;
3266 p1 != NULL_TREE && p2 != NULL_TREE;
3267 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3268 {
3269 tree t1 = TREE_VALUE (p1);
3270 tree t2 = TREE_VALUE (p2);
3271 int i;
3272
3273 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3274 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3275
3276 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3277 return 0;
3278
3279 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3280 {
3281 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3282 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3283
3284 /* If either of the template parameters are invalid, assume
3285 they match for the sake of error recovery. */
3286 if (error_operand_p (parm1) || error_operand_p (parm2))
3287 return 1;
3288
3289 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3290 return 0;
3291
3292 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3293 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3294 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3295 continue;
3296 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3297 return 0;
3298 }
3299 }
3300
3301 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3302 /* One set of parameters has more parameters lists than the
3303 other. */
3304 return 0;
3305
3306 return 1;
3307 }
3308
3309 /* Determine whether PARM is a parameter pack. */
3310
3311 bool
3312 template_parameter_pack_p (const_tree parm)
3313 {
3314 /* Determine if we have a non-type template parameter pack. */
3315 if (TREE_CODE (parm) == PARM_DECL)
3316 return (DECL_TEMPLATE_PARM_P (parm)
3317 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3318 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3319 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3320
3321 /* If this is a list of template parameters, we could get a
3322 TYPE_DECL or a TEMPLATE_DECL. */
3323 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3324 parm = TREE_TYPE (parm);
3325
3326 /* Otherwise it must be a type template parameter. */
3327 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3328 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3329 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3330 }
3331
3332 /* Determine if T is a function parameter pack. */
3333
3334 bool
3335 function_parameter_pack_p (const_tree t)
3336 {
3337 if (t && TREE_CODE (t) == PARM_DECL)
3338 return DECL_PACK_P (t);
3339 return false;
3340 }
3341
3342 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3343 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3344
3345 tree
3346 get_function_template_decl (const_tree primary_func_tmpl_inst)
3347 {
3348 if (! primary_func_tmpl_inst
3349 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3350 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3351 return NULL;
3352
3353 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3354 }
3355
3356 /* Return true iff the function parameter PARAM_DECL was expanded
3357 from the function parameter pack PACK. */
3358
3359 bool
3360 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3361 {
3362 if (DECL_ARTIFICIAL (param_decl)
3363 || !function_parameter_pack_p (pack))
3364 return false;
3365
3366 /* The parameter pack and its pack arguments have the same
3367 DECL_PARM_INDEX. */
3368 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3369 }
3370
3371 /* Determine whether ARGS describes a variadic template args list,
3372 i.e., one that is terminated by a template argument pack. */
3373
3374 static bool
3375 template_args_variadic_p (tree args)
3376 {
3377 int nargs;
3378 tree last_parm;
3379
3380 if (args == NULL_TREE)
3381 return false;
3382
3383 args = INNERMOST_TEMPLATE_ARGS (args);
3384 nargs = TREE_VEC_LENGTH (args);
3385
3386 if (nargs == 0)
3387 return false;
3388
3389 last_parm = TREE_VEC_ELT (args, nargs - 1);
3390
3391 return ARGUMENT_PACK_P (last_parm);
3392 }
3393
3394 /* Generate a new name for the parameter pack name NAME (an
3395 IDENTIFIER_NODE) that incorporates its */
3396
3397 static tree
3398 make_ith_pack_parameter_name (tree name, int i)
3399 {
3400 /* Munge the name to include the parameter index. */
3401 #define NUMBUF_LEN 128
3402 char numbuf[NUMBUF_LEN];
3403 char* newname;
3404 int newname_len;
3405
3406 if (name == NULL_TREE)
3407 return name;
3408 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3409 newname_len = IDENTIFIER_LENGTH (name)
3410 + strlen (numbuf) + 2;
3411 newname = (char*)alloca (newname_len);
3412 snprintf (newname, newname_len,
3413 "%s#%i", IDENTIFIER_POINTER (name), i);
3414 return get_identifier (newname);
3415 }
3416
3417 /* Return true if T is a primary function, class or alias template
3418 specialization, not including the template pattern. */
3419
3420 bool
3421 primary_template_specialization_p (const_tree t)
3422 {
3423 if (!t)
3424 return false;
3425
3426 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3427 return (DECL_LANG_SPECIFIC (t)
3428 && DECL_USE_TEMPLATE (t)
3429 && DECL_TEMPLATE_INFO (t)
3430 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3431 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3432 return (CLASSTYPE_TEMPLATE_INFO (t)
3433 && CLASSTYPE_USE_TEMPLATE (t)
3434 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3435 else if (alias_template_specialization_p (t))
3436 return true;
3437 return false;
3438 }
3439
3440 /* Return true if PARM is a template template parameter. */
3441
3442 bool
3443 template_template_parameter_p (const_tree parm)
3444 {
3445 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3446 }
3447
3448 /* Return true iff PARM is a DECL representing a type template
3449 parameter. */
3450
3451 bool
3452 template_type_parameter_p (const_tree parm)
3453 {
3454 return (parm
3455 && (TREE_CODE (parm) == TYPE_DECL
3456 || TREE_CODE (parm) == TEMPLATE_DECL)
3457 && DECL_TEMPLATE_PARM_P (parm));
3458 }
3459
3460 /* Return the template parameters of T if T is a
3461 primary template instantiation, NULL otherwise. */
3462
3463 tree
3464 get_primary_template_innermost_parameters (const_tree t)
3465 {
3466 tree parms = NULL, template_info = NULL;
3467
3468 if ((template_info = get_template_info (t))
3469 && primary_template_specialization_p (t))
3470 parms = INNERMOST_TEMPLATE_PARMS
3471 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3472
3473 return parms;
3474 }
3475
3476 /* Return the template parameters of the LEVELth level from the full list
3477 of template parameters PARMS. */
3478
3479 tree
3480 get_template_parms_at_level (tree parms, int level)
3481 {
3482 tree p;
3483 if (!parms
3484 || TREE_CODE (parms) != TREE_LIST
3485 || level > TMPL_PARMS_DEPTH (parms))
3486 return NULL_TREE;
3487
3488 for (p = parms; p; p = TREE_CHAIN (p))
3489 if (TMPL_PARMS_DEPTH (p) == level)
3490 return p;
3491
3492 return NULL_TREE;
3493 }
3494
3495 /* Returns the template arguments of T if T is a template instantiation,
3496 NULL otherwise. */
3497
3498 tree
3499 get_template_innermost_arguments (const_tree t)
3500 {
3501 tree args = NULL, template_info = NULL;
3502
3503 if ((template_info = get_template_info (t))
3504 && TI_ARGS (template_info))
3505 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3506
3507 return args;
3508 }
3509
3510 /* Return the argument pack elements of T if T is a template argument pack,
3511 NULL otherwise. */
3512
3513 tree
3514 get_template_argument_pack_elems (const_tree t)
3515 {
3516 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3517 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3518 return NULL;
3519
3520 return ARGUMENT_PACK_ARGS (t);
3521 }
3522
3523 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3524 ARGUMENT_PACK_SELECT represents. */
3525
3526 static tree
3527 argument_pack_select_arg (tree t)
3528 {
3529 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3530 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3531
3532 /* If the selected argument is an expansion E, that most likely means we were
3533 called from gen_elem_of_pack_expansion_instantiation during the
3534 substituting of an argument pack (of which the Ith element is a pack
3535 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3536 In this case, the Ith element resulting from this substituting is going to
3537 be a pack expansion, which pattern is the pattern of E. Let's return the
3538 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3539 resulting pack expansion from it. */
3540 if (PACK_EXPANSION_P (arg))
3541 {
3542 /* Make sure we aren't throwing away arg info. */
3543 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3544 arg = PACK_EXPANSION_PATTERN (arg);
3545 }
3546
3547 return arg;
3548 }
3549
3550
3551 /* True iff FN is a function representing a built-in variadic parameter
3552 pack. */
3553
3554 bool
3555 builtin_pack_fn_p (tree fn)
3556 {
3557 if (!fn
3558 || TREE_CODE (fn) != FUNCTION_DECL
3559 || !DECL_IS_BUILTIN (fn))
3560 return false;
3561
3562 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3563 return true;
3564
3565 return false;
3566 }
3567
3568 /* True iff CALL is a call to a function representing a built-in variadic
3569 parameter pack. */
3570
3571 static bool
3572 builtin_pack_call_p (tree call)
3573 {
3574 if (TREE_CODE (call) != CALL_EXPR)
3575 return false;
3576 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3577 }
3578
3579 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3580
3581 static tree
3582 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3583 tree in_decl)
3584 {
3585 tree ohi = CALL_EXPR_ARG (call, 0);
3586 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3587 false/*fn*/, true/*int_cst*/);
3588
3589 if (value_dependent_expression_p (hi))
3590 {
3591 if (hi != ohi)
3592 {
3593 call = copy_node (call);
3594 CALL_EXPR_ARG (call, 0) = hi;
3595 }
3596 tree ex = make_pack_expansion (call, complain);
3597 tree vec = make_tree_vec (1);
3598 TREE_VEC_ELT (vec, 0) = ex;
3599 return vec;
3600 }
3601 else
3602 {
3603 hi = cxx_constant_value (hi);
3604 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3605
3606 /* Calculate the largest value of len that won't make the size of the vec
3607 overflow an int. The compiler will exceed resource limits long before
3608 this, but it seems a decent place to diagnose. */
3609 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3610
3611 if (len < 0 || len > max)
3612 {
3613 if ((complain & tf_error)
3614 && hi != error_mark_node)
3615 error ("argument to __integer_pack must be between 0 and %d", max);
3616 return error_mark_node;
3617 }
3618
3619 tree vec = make_tree_vec (len);
3620
3621 for (int i = 0; i < len; ++i)
3622 TREE_VEC_ELT (vec, i) = size_int (i);
3623
3624 return vec;
3625 }
3626 }
3627
3628 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3629 CALL. */
3630
3631 static tree
3632 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3633 tree in_decl)
3634 {
3635 if (!builtin_pack_call_p (call))
3636 return NULL_TREE;
3637
3638 tree fn = CALL_EXPR_FN (call);
3639
3640 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3641 return expand_integer_pack (call, args, complain, in_decl);
3642
3643 return NULL_TREE;
3644 }
3645
3646 /* Structure used to track the progress of find_parameter_packs_r. */
3647 struct find_parameter_pack_data
3648 {
3649 /* TREE_LIST that will contain all of the parameter packs found by
3650 the traversal. */
3651 tree* parameter_packs;
3652
3653 /* Set of AST nodes that have been visited by the traversal. */
3654 hash_set<tree> *visited;
3655
3656 /* True iff we're making a type pack expansion. */
3657 bool type_pack_expansion_p;
3658 };
3659
3660 /* Identifies all of the argument packs that occur in a template
3661 argument and appends them to the TREE_LIST inside DATA, which is a
3662 find_parameter_pack_data structure. This is a subroutine of
3663 make_pack_expansion and uses_parameter_packs. */
3664 static tree
3665 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3666 {
3667 tree t = *tp;
3668 struct find_parameter_pack_data* ppd =
3669 (struct find_parameter_pack_data*)data;
3670 bool parameter_pack_p = false;
3671
3672 /* Handle type aliases/typedefs. */
3673 if (TYPE_ALIAS_P (t))
3674 {
3675 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3676 cp_walk_tree (&TI_ARGS (tinfo),
3677 &find_parameter_packs_r,
3678 ppd, ppd->visited);
3679 *walk_subtrees = 0;
3680 return NULL_TREE;
3681 }
3682
3683 /* Identify whether this is a parameter pack or not. */
3684 switch (TREE_CODE (t))
3685 {
3686 case TEMPLATE_PARM_INDEX:
3687 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3688 parameter_pack_p = true;
3689 break;
3690
3691 case TEMPLATE_TYPE_PARM:
3692 t = TYPE_MAIN_VARIANT (t);
3693 /* FALLTHRU */
3694 case TEMPLATE_TEMPLATE_PARM:
3695 /* If the placeholder appears in the decl-specifier-seq of a function
3696 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3697 is a pack expansion, the invented template parameter is a template
3698 parameter pack. */
3699 if (ppd->type_pack_expansion_p && is_auto (t))
3700 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3701 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3702 parameter_pack_p = true;
3703 break;
3704
3705 case FIELD_DECL:
3706 case PARM_DECL:
3707 if (DECL_PACK_P (t))
3708 {
3709 /* We don't want to walk into the type of a PARM_DECL,
3710 because we don't want to see the type parameter pack. */
3711 *walk_subtrees = 0;
3712 parameter_pack_p = true;
3713 }
3714 break;
3715
3716 case VAR_DECL:
3717 if (DECL_PACK_P (t))
3718 {
3719 /* We don't want to walk into the type of a variadic capture proxy,
3720 because we don't want to see the type parameter pack. */
3721 *walk_subtrees = 0;
3722 parameter_pack_p = true;
3723 }
3724 else if (variable_template_specialization_p (t))
3725 {
3726 cp_walk_tree (&DECL_TI_ARGS (t),
3727 find_parameter_packs_r,
3728 ppd, ppd->visited);
3729 *walk_subtrees = 0;
3730 }
3731 break;
3732
3733 case CALL_EXPR:
3734 if (builtin_pack_call_p (t))
3735 parameter_pack_p = true;
3736 break;
3737
3738 case BASES:
3739 parameter_pack_p = true;
3740 break;
3741 default:
3742 /* Not a parameter pack. */
3743 break;
3744 }
3745
3746 if (parameter_pack_p)
3747 {
3748 /* Add this parameter pack to the list. */
3749 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3750 }
3751
3752 if (TYPE_P (t))
3753 cp_walk_tree (&TYPE_CONTEXT (t),
3754 &find_parameter_packs_r, ppd, ppd->visited);
3755
3756 /* This switch statement will return immediately if we don't find a
3757 parameter pack. */
3758 switch (TREE_CODE (t))
3759 {
3760 case TEMPLATE_PARM_INDEX:
3761 return NULL_TREE;
3762
3763 case BOUND_TEMPLATE_TEMPLATE_PARM:
3764 /* Check the template itself. */
3765 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3766 &find_parameter_packs_r, ppd, ppd->visited);
3767 /* Check the template arguments. */
3768 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3769 ppd->visited);
3770 *walk_subtrees = 0;
3771 return NULL_TREE;
3772
3773 case TEMPLATE_TYPE_PARM:
3774 case TEMPLATE_TEMPLATE_PARM:
3775 return NULL_TREE;
3776
3777 case PARM_DECL:
3778 return NULL_TREE;
3779
3780 case DECL_EXPR:
3781 /* Ignore the declaration of a capture proxy for a parameter pack. */
3782 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3783 *walk_subtrees = 0;
3784 return NULL_TREE;
3785
3786 case RECORD_TYPE:
3787 if (TYPE_PTRMEMFUNC_P (t))
3788 return NULL_TREE;
3789 /* Fall through. */
3790
3791 case UNION_TYPE:
3792 case ENUMERAL_TYPE:
3793 if (TYPE_TEMPLATE_INFO (t))
3794 cp_walk_tree (&TYPE_TI_ARGS (t),
3795 &find_parameter_packs_r, ppd, ppd->visited);
3796
3797 *walk_subtrees = 0;
3798 return NULL_TREE;
3799
3800 case TEMPLATE_DECL:
3801 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3802 return NULL_TREE;
3803 gcc_fallthrough();
3804
3805 case CONSTRUCTOR:
3806 cp_walk_tree (&TREE_TYPE (t),
3807 &find_parameter_packs_r, ppd, ppd->visited);
3808 return NULL_TREE;
3809
3810 case TYPENAME_TYPE:
3811 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3812 ppd, ppd->visited);
3813 *walk_subtrees = 0;
3814 return NULL_TREE;
3815
3816 case TYPE_PACK_EXPANSION:
3817 case EXPR_PACK_EXPANSION:
3818 *walk_subtrees = 0;
3819 return NULL_TREE;
3820
3821 case INTEGER_TYPE:
3822 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3823 ppd, ppd->visited);
3824 *walk_subtrees = 0;
3825 return NULL_TREE;
3826
3827 case IDENTIFIER_NODE:
3828 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3829 ppd->visited);
3830 *walk_subtrees = 0;
3831 return NULL_TREE;
3832
3833 case LAMBDA_EXPR:
3834 {
3835 /* Look at explicit captures. */
3836 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3837 cap; cap = TREE_CHAIN (cap))
3838 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3839 ppd->visited);
3840 /* Since we defer implicit capture, look in the body as well. */
3841 tree fn = lambda_function (t);
3842 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3843 ppd->visited);
3844 *walk_subtrees = 0;
3845 return NULL_TREE;
3846 }
3847
3848 case DECLTYPE_TYPE:
3849 {
3850 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3851 type_pack_expansion_p to false so that any placeholders
3852 within the expression don't get marked as parameter packs. */
3853 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3854 ppd->type_pack_expansion_p = false;
3855 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3856 ppd, ppd->visited);
3857 ppd->type_pack_expansion_p = type_pack_expansion_p;
3858 *walk_subtrees = 0;
3859 return NULL_TREE;
3860 }
3861
3862 default:
3863 return NULL_TREE;
3864 }
3865
3866 return NULL_TREE;
3867 }
3868
3869 /* Determines if the expression or type T uses any parameter packs. */
3870 bool
3871 uses_parameter_packs (tree t)
3872 {
3873 tree parameter_packs = NULL_TREE;
3874 struct find_parameter_pack_data ppd;
3875 ppd.parameter_packs = &parameter_packs;
3876 ppd.visited = new hash_set<tree>;
3877 ppd.type_pack_expansion_p = false;
3878 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3879 delete ppd.visited;
3880 return parameter_packs != NULL_TREE;
3881 }
3882
3883 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3884 representation a base-class initializer into a parameter pack
3885 expansion. If all goes well, the resulting node will be an
3886 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3887 respectively. */
3888 tree
3889 make_pack_expansion (tree arg, tsubst_flags_t complain)
3890 {
3891 tree result;
3892 tree parameter_packs = NULL_TREE;
3893 bool for_types = false;
3894 struct find_parameter_pack_data ppd;
3895
3896 if (!arg || arg == error_mark_node)
3897 return arg;
3898
3899 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3900 {
3901 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3902 class initializer. In this case, the TREE_PURPOSE will be a
3903 _TYPE node (representing the base class expansion we're
3904 initializing) and the TREE_VALUE will be a TREE_LIST
3905 containing the initialization arguments.
3906
3907 The resulting expansion looks somewhat different from most
3908 expansions. Rather than returning just one _EXPANSION, we
3909 return a TREE_LIST whose TREE_PURPOSE is a
3910 TYPE_PACK_EXPANSION containing the bases that will be
3911 initialized. The TREE_VALUE will be identical to the
3912 original TREE_VALUE, which is a list of arguments that will
3913 be passed to each base. We do not introduce any new pack
3914 expansion nodes into the TREE_VALUE (although it is possible
3915 that some already exist), because the TREE_PURPOSE and
3916 TREE_VALUE all need to be expanded together with the same
3917 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3918 resulting TREE_PURPOSE will mention the parameter packs in
3919 both the bases and the arguments to the bases. */
3920 tree purpose;
3921 tree value;
3922 tree parameter_packs = NULL_TREE;
3923
3924 /* Determine which parameter packs will be used by the base
3925 class expansion. */
3926 ppd.visited = new hash_set<tree>;
3927 ppd.parameter_packs = &parameter_packs;
3928 ppd.type_pack_expansion_p = true;
3929 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3930 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3931 &ppd, ppd.visited);
3932
3933 if (parameter_packs == NULL_TREE)
3934 {
3935 if (complain & tf_error)
3936 error ("base initializer expansion %qT contains no parameter packs",
3937 arg);
3938 delete ppd.visited;
3939 return error_mark_node;
3940 }
3941
3942 if (TREE_VALUE (arg) != void_type_node)
3943 {
3944 /* Collect the sets of parameter packs used in each of the
3945 initialization arguments. */
3946 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3947 {
3948 /* Determine which parameter packs will be expanded in this
3949 argument. */
3950 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3951 &ppd, ppd.visited);
3952 }
3953 }
3954
3955 delete ppd.visited;
3956
3957 /* Create the pack expansion type for the base type. */
3958 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3959 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3960 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3961 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3962
3963 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3964 they will rarely be compared to anything. */
3965 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3966
3967 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3968 }
3969
3970 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3971 for_types = true;
3972
3973 /* Build the PACK_EXPANSION_* node. */
3974 result = for_types
3975 ? cxx_make_type (TYPE_PACK_EXPANSION)
3976 : make_node (EXPR_PACK_EXPANSION);
3977 SET_PACK_EXPANSION_PATTERN (result, arg);
3978 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3979 {
3980 /* Propagate type and const-expression information. */
3981 TREE_TYPE (result) = TREE_TYPE (arg);
3982 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3983 /* Mark this read now, since the expansion might be length 0. */
3984 mark_exp_read (arg);
3985 }
3986 else
3987 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3988 they will rarely be compared to anything. */
3989 SET_TYPE_STRUCTURAL_EQUALITY (result);
3990
3991 /* Determine which parameter packs will be expanded. */
3992 ppd.parameter_packs = &parameter_packs;
3993 ppd.visited = new hash_set<tree>;
3994 ppd.type_pack_expansion_p = TYPE_P (arg);
3995 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3996 delete ppd.visited;
3997
3998 /* Make sure we found some parameter packs. */
3999 if (parameter_packs == NULL_TREE)
4000 {
4001 if (complain & tf_error)
4002 {
4003 if (TYPE_P (arg))
4004 error ("expansion pattern %qT contains no argument packs", arg);
4005 else
4006 error ("expansion pattern %qE contains no argument packs", arg);
4007 }
4008 return error_mark_node;
4009 }
4010 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4011
4012 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4013
4014 return result;
4015 }
4016
4017 /* Checks T for any "bare" parameter packs, which have not yet been
4018 expanded, and issues an error if any are found. This operation can
4019 only be done on full expressions or types (e.g., an expression
4020 statement, "if" condition, etc.), because we could have expressions like:
4021
4022 foo(f(g(h(args)))...)
4023
4024 where "args" is a parameter pack. check_for_bare_parameter_packs
4025 should not be called for the subexpressions args, h(args),
4026 g(h(args)), or f(g(h(args))), because we would produce erroneous
4027 error messages.
4028
4029 Returns TRUE and emits an error if there were bare parameter packs,
4030 returns FALSE otherwise. */
4031 bool
4032 check_for_bare_parameter_packs (tree t)
4033 {
4034 tree parameter_packs = NULL_TREE;
4035 struct find_parameter_pack_data ppd;
4036
4037 if (!processing_template_decl || !t || t == error_mark_node)
4038 return false;
4039
4040 /* A lambda might use a parameter pack from the containing context. */
4041 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
4042 return false;
4043
4044 if (TREE_CODE (t) == TYPE_DECL)
4045 t = TREE_TYPE (t);
4046
4047 ppd.parameter_packs = &parameter_packs;
4048 ppd.visited = new hash_set<tree>;
4049 ppd.type_pack_expansion_p = false;
4050 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4051 delete ppd.visited;
4052
4053 if (parameter_packs)
4054 {
4055 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
4056 error_at (loc, "parameter packs not expanded with %<...%>:");
4057 while (parameter_packs)
4058 {
4059 tree pack = TREE_VALUE (parameter_packs);
4060 tree name = NULL_TREE;
4061
4062 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4063 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4064 name = TYPE_NAME (pack);
4065 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4066 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4067 else if (TREE_CODE (pack) == CALL_EXPR)
4068 name = DECL_NAME (CALL_EXPR_FN (pack));
4069 else
4070 name = DECL_NAME (pack);
4071
4072 if (name)
4073 inform (loc, " %qD", name);
4074 else
4075 inform (loc, " <anonymous>");
4076
4077 parameter_packs = TREE_CHAIN (parameter_packs);
4078 }
4079
4080 return true;
4081 }
4082
4083 return false;
4084 }
4085
4086 /* Expand any parameter packs that occur in the template arguments in
4087 ARGS. */
4088 tree
4089 expand_template_argument_pack (tree args)
4090 {
4091 if (args == error_mark_node)
4092 return error_mark_node;
4093
4094 tree result_args = NULL_TREE;
4095 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4096 int num_result_args = -1;
4097 int non_default_args_count = -1;
4098
4099 /* First, determine if we need to expand anything, and the number of
4100 slots we'll need. */
4101 for (in_arg = 0; in_arg < nargs; ++in_arg)
4102 {
4103 tree arg = TREE_VEC_ELT (args, in_arg);
4104 if (arg == NULL_TREE)
4105 return args;
4106 if (ARGUMENT_PACK_P (arg))
4107 {
4108 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4109 if (num_result_args < 0)
4110 num_result_args = in_arg + num_packed;
4111 else
4112 num_result_args += num_packed;
4113 }
4114 else
4115 {
4116 if (num_result_args >= 0)
4117 num_result_args++;
4118 }
4119 }
4120
4121 /* If no expansion is necessary, we're done. */
4122 if (num_result_args < 0)
4123 return args;
4124
4125 /* Expand arguments. */
4126 result_args = make_tree_vec (num_result_args);
4127 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4128 non_default_args_count =
4129 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4130 for (in_arg = 0; in_arg < nargs; ++in_arg)
4131 {
4132 tree arg = TREE_VEC_ELT (args, in_arg);
4133 if (ARGUMENT_PACK_P (arg))
4134 {
4135 tree packed = ARGUMENT_PACK_ARGS (arg);
4136 int i, num_packed = TREE_VEC_LENGTH (packed);
4137 for (i = 0; i < num_packed; ++i, ++out_arg)
4138 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4139 if (non_default_args_count > 0)
4140 non_default_args_count += num_packed - 1;
4141 }
4142 else
4143 {
4144 TREE_VEC_ELT (result_args, out_arg) = arg;
4145 ++out_arg;
4146 }
4147 }
4148 if (non_default_args_count >= 0)
4149 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4150 return result_args;
4151 }
4152
4153 /* Checks if DECL shadows a template parameter.
4154
4155 [temp.local]: A template-parameter shall not be redeclared within its
4156 scope (including nested scopes).
4157
4158 Emits an error and returns TRUE if the DECL shadows a parameter,
4159 returns FALSE otherwise. */
4160
4161 bool
4162 check_template_shadow (tree decl)
4163 {
4164 tree olddecl;
4165
4166 /* If we're not in a template, we can't possibly shadow a template
4167 parameter. */
4168 if (!current_template_parms)
4169 return true;
4170
4171 /* Figure out what we're shadowing. */
4172 decl = OVL_FIRST (decl);
4173 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4174
4175 /* If there's no previous binding for this name, we're not shadowing
4176 anything, let alone a template parameter. */
4177 if (!olddecl)
4178 return true;
4179
4180 /* If we're not shadowing a template parameter, we're done. Note
4181 that OLDDECL might be an OVERLOAD (or perhaps even an
4182 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4183 node. */
4184 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4185 return true;
4186
4187 /* We check for decl != olddecl to avoid bogus errors for using a
4188 name inside a class. We check TPFI to avoid duplicate errors for
4189 inline member templates. */
4190 if (decl == olddecl
4191 || (DECL_TEMPLATE_PARM_P (decl)
4192 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4193 return true;
4194
4195 /* Don't complain about the injected class name, as we've already
4196 complained about the class itself. */
4197 if (DECL_SELF_REFERENCE_P (decl))
4198 return false;
4199
4200 if (DECL_TEMPLATE_PARM_P (decl))
4201 error ("declaration of template parameter %q+D shadows "
4202 "template parameter", decl);
4203 else
4204 error ("declaration of %q+#D shadows template parameter", decl);
4205 inform (DECL_SOURCE_LOCATION (olddecl),
4206 "template parameter %qD declared here", olddecl);
4207 return false;
4208 }
4209
4210 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4211 ORIG_LEVEL, DECL, and TYPE. */
4212
4213 static tree
4214 build_template_parm_index (int index,
4215 int level,
4216 int orig_level,
4217 tree decl,
4218 tree type)
4219 {
4220 tree t = make_node (TEMPLATE_PARM_INDEX);
4221 TEMPLATE_PARM_IDX (t) = index;
4222 TEMPLATE_PARM_LEVEL (t) = level;
4223 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4224 TEMPLATE_PARM_DECL (t) = decl;
4225 TREE_TYPE (t) = type;
4226 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4227 TREE_READONLY (t) = TREE_READONLY (decl);
4228
4229 return t;
4230 }
4231
4232 /* Find the canonical type parameter for the given template type
4233 parameter. Returns the canonical type parameter, which may be TYPE
4234 if no such parameter existed. */
4235
4236 static tree
4237 canonical_type_parameter (tree type)
4238 {
4239 tree list;
4240 int idx = TEMPLATE_TYPE_IDX (type);
4241 if (!canonical_template_parms)
4242 vec_alloc (canonical_template_parms, idx + 1);
4243
4244 if (canonical_template_parms->length () <= (unsigned) idx)
4245 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4246
4247 list = (*canonical_template_parms)[idx];
4248 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4249 list = TREE_CHAIN (list);
4250
4251 if (list)
4252 return TREE_VALUE (list);
4253 else
4254 {
4255 (*canonical_template_parms)[idx]
4256 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4257 return type;
4258 }
4259 }
4260
4261 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4262 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4263 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4264 new one is created. */
4265
4266 static tree
4267 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4268 tsubst_flags_t complain)
4269 {
4270 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4271 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4272 != TEMPLATE_PARM_LEVEL (index) - levels)
4273 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4274 {
4275 tree orig_decl = TEMPLATE_PARM_DECL (index);
4276 tree decl, t;
4277
4278 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4279 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4280 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4281 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4282 DECL_ARTIFICIAL (decl) = 1;
4283 SET_DECL_TEMPLATE_PARM_P (decl);
4284
4285 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4286 TEMPLATE_PARM_LEVEL (index) - levels,
4287 TEMPLATE_PARM_ORIG_LEVEL (index),
4288 decl, type);
4289 TEMPLATE_PARM_DESCENDANTS (index) = t;
4290 TEMPLATE_PARM_PARAMETER_PACK (t)
4291 = TEMPLATE_PARM_PARAMETER_PACK (index);
4292
4293 /* Template template parameters need this. */
4294 if (TREE_CODE (decl) == TEMPLATE_DECL)
4295 {
4296 DECL_TEMPLATE_RESULT (decl)
4297 = build_decl (DECL_SOURCE_LOCATION (decl),
4298 TYPE_DECL, DECL_NAME (decl), type);
4299 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4300 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4301 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4302 }
4303 }
4304
4305 return TEMPLATE_PARM_DESCENDANTS (index);
4306 }
4307
4308 /* Process information from new template parameter PARM and append it
4309 to the LIST being built. This new parameter is a non-type
4310 parameter iff IS_NON_TYPE is true. This new parameter is a
4311 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4312 is in PARM_LOC. */
4313
4314 tree
4315 process_template_parm (tree list, location_t parm_loc, tree parm,
4316 bool is_non_type, bool is_parameter_pack)
4317 {
4318 tree decl = 0;
4319 int idx = 0;
4320
4321 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4322 tree defval = TREE_PURPOSE (parm);
4323 tree constr = TREE_TYPE (parm);
4324
4325 if (list)
4326 {
4327 tree p = tree_last (list);
4328
4329 if (p && TREE_VALUE (p) != error_mark_node)
4330 {
4331 p = TREE_VALUE (p);
4332 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4333 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4334 else
4335 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4336 }
4337
4338 ++idx;
4339 }
4340
4341 if (is_non_type)
4342 {
4343 parm = TREE_VALUE (parm);
4344
4345 SET_DECL_TEMPLATE_PARM_P (parm);
4346
4347 if (TREE_TYPE (parm) != error_mark_node)
4348 {
4349 /* [temp.param]
4350
4351 The top-level cv-qualifiers on the template-parameter are
4352 ignored when determining its type. */
4353 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4354 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4355 TREE_TYPE (parm) = error_mark_node;
4356 else if (uses_parameter_packs (TREE_TYPE (parm))
4357 && !is_parameter_pack
4358 /* If we're in a nested template parameter list, the template
4359 template parameter could be a parameter pack. */
4360 && processing_template_parmlist == 1)
4361 {
4362 /* This template parameter is not a parameter pack, but it
4363 should be. Complain about "bare" parameter packs. */
4364 check_for_bare_parameter_packs (TREE_TYPE (parm));
4365
4366 /* Recover by calling this a parameter pack. */
4367 is_parameter_pack = true;
4368 }
4369 }
4370
4371 /* A template parameter is not modifiable. */
4372 TREE_CONSTANT (parm) = 1;
4373 TREE_READONLY (parm) = 1;
4374 decl = build_decl (parm_loc,
4375 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4376 TREE_CONSTANT (decl) = 1;
4377 TREE_READONLY (decl) = 1;
4378 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4379 = build_template_parm_index (idx, processing_template_decl,
4380 processing_template_decl,
4381 decl, TREE_TYPE (parm));
4382
4383 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4384 = is_parameter_pack;
4385 }
4386 else
4387 {
4388 tree t;
4389 parm = TREE_VALUE (TREE_VALUE (parm));
4390
4391 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4392 {
4393 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4394 /* This is for distinguishing between real templates and template
4395 template parameters */
4396 TREE_TYPE (parm) = t;
4397 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4398 decl = parm;
4399 }
4400 else
4401 {
4402 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4403 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4404 decl = build_decl (parm_loc,
4405 TYPE_DECL, parm, t);
4406 }
4407
4408 TYPE_NAME (t) = decl;
4409 TYPE_STUB_DECL (t) = decl;
4410 parm = decl;
4411 TEMPLATE_TYPE_PARM_INDEX (t)
4412 = build_template_parm_index (idx, processing_template_decl,
4413 processing_template_decl,
4414 decl, TREE_TYPE (parm));
4415 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4416 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4417 }
4418 DECL_ARTIFICIAL (decl) = 1;
4419 SET_DECL_TEMPLATE_PARM_P (decl);
4420
4421 /* Build requirements for the type/template parameter.
4422 This must be done after SET_DECL_TEMPLATE_PARM_P or
4423 process_template_parm could fail. */
4424 tree reqs = finish_shorthand_constraint (parm, constr);
4425
4426 pushdecl (decl);
4427
4428 /* Build the parameter node linking the parameter declaration,
4429 its default argument (if any), and its constraints (if any). */
4430 parm = build_tree_list (defval, parm);
4431 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4432
4433 return chainon (list, parm);
4434 }
4435
4436 /* The end of a template parameter list has been reached. Process the
4437 tree list into a parameter vector, converting each parameter into a more
4438 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4439 as PARM_DECLs. */
4440
4441 tree
4442 end_template_parm_list (tree parms)
4443 {
4444 int nparms;
4445 tree parm, next;
4446 tree saved_parmlist = make_tree_vec (list_length (parms));
4447
4448 /* Pop the dummy parameter level and add the real one. */
4449 current_template_parms = TREE_CHAIN (current_template_parms);
4450
4451 current_template_parms
4452 = tree_cons (size_int (processing_template_decl),
4453 saved_parmlist, current_template_parms);
4454
4455 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4456 {
4457 next = TREE_CHAIN (parm);
4458 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4459 TREE_CHAIN (parm) = NULL_TREE;
4460 }
4461
4462 --processing_template_parmlist;
4463
4464 return saved_parmlist;
4465 }
4466
4467 // Explicitly indicate the end of the template parameter list. We assume
4468 // that the current template parameters have been constructed and/or
4469 // managed explicitly, as when creating new template template parameters
4470 // from a shorthand constraint.
4471 void
4472 end_template_parm_list ()
4473 {
4474 --processing_template_parmlist;
4475 }
4476
4477 /* end_template_decl is called after a template declaration is seen. */
4478
4479 void
4480 end_template_decl (void)
4481 {
4482 reset_specialization ();
4483
4484 if (! processing_template_decl)
4485 return;
4486
4487 /* This matches the pushlevel in begin_template_parm_list. */
4488 finish_scope ();
4489
4490 --processing_template_decl;
4491 current_template_parms = TREE_CHAIN (current_template_parms);
4492 }
4493
4494 /* Takes a TREE_LIST representing a template parameter and convert it
4495 into an argument suitable to be passed to the type substitution
4496 functions. Note that If the TREE_LIST contains an error_mark
4497 node, the returned argument is error_mark_node. */
4498
4499 tree
4500 template_parm_to_arg (tree t)
4501 {
4502
4503 if (t == NULL_TREE
4504 || TREE_CODE (t) != TREE_LIST)
4505 return t;
4506
4507 if (error_operand_p (TREE_VALUE (t)))
4508 return error_mark_node;
4509
4510 t = TREE_VALUE (t);
4511
4512 if (TREE_CODE (t) == TYPE_DECL
4513 || TREE_CODE (t) == TEMPLATE_DECL)
4514 {
4515 t = TREE_TYPE (t);
4516
4517 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4518 {
4519 /* Turn this argument into a TYPE_ARGUMENT_PACK
4520 with a single element, which expands T. */
4521 tree vec = make_tree_vec (1);
4522 if (CHECKING_P)
4523 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4524
4525 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4526
4527 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4528 SET_ARGUMENT_PACK_ARGS (t, vec);
4529 }
4530 }
4531 else
4532 {
4533 t = DECL_INITIAL (t);
4534
4535 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4536 {
4537 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4538 with a single element, which expands T. */
4539 tree vec = make_tree_vec (1);
4540 if (CHECKING_P)
4541 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4542
4543 t = convert_from_reference (t);
4544 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4545
4546 t = make_node (NONTYPE_ARGUMENT_PACK);
4547 SET_ARGUMENT_PACK_ARGS (t, vec);
4548 }
4549 else
4550 t = convert_from_reference (t);
4551 }
4552 return t;
4553 }
4554
4555 /* Given a single level of template parameters (a TREE_VEC), return it
4556 as a set of template arguments. */
4557
4558 static tree
4559 template_parms_level_to_args (tree parms)
4560 {
4561 tree a = copy_node (parms);
4562 TREE_TYPE (a) = NULL_TREE;
4563 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4564 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4565
4566 if (CHECKING_P)
4567 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4568
4569 return a;
4570 }
4571
4572 /* Given a set of template parameters, return them as a set of template
4573 arguments. The template parameters are represented as a TREE_VEC, in
4574 the form documented in cp-tree.h for template arguments. */
4575
4576 static tree
4577 template_parms_to_args (tree parms)
4578 {
4579 tree header;
4580 tree args = NULL_TREE;
4581 int length = TMPL_PARMS_DEPTH (parms);
4582 int l = length;
4583
4584 /* If there is only one level of template parameters, we do not
4585 create a TREE_VEC of TREE_VECs. Instead, we return a single
4586 TREE_VEC containing the arguments. */
4587 if (length > 1)
4588 args = make_tree_vec (length);
4589
4590 for (header = parms; header; header = TREE_CHAIN (header))
4591 {
4592 tree a = template_parms_level_to_args (TREE_VALUE (header));
4593
4594 if (length > 1)
4595 TREE_VEC_ELT (args, --l) = a;
4596 else
4597 args = a;
4598 }
4599
4600 return args;
4601 }
4602
4603 /* Within the declaration of a template, return the currently active
4604 template parameters as an argument TREE_VEC. */
4605
4606 static tree
4607 current_template_args (void)
4608 {
4609 return template_parms_to_args (current_template_parms);
4610 }
4611
4612 /* Update the declared TYPE by doing any lookups which were thought to be
4613 dependent, but are not now that we know the SCOPE of the declarator. */
4614
4615 tree
4616 maybe_update_decl_type (tree orig_type, tree scope)
4617 {
4618 tree type = orig_type;
4619
4620 if (type == NULL_TREE)
4621 return type;
4622
4623 if (TREE_CODE (orig_type) == TYPE_DECL)
4624 type = TREE_TYPE (type);
4625
4626 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4627 && dependent_type_p (type)
4628 /* Don't bother building up the args in this case. */
4629 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4630 {
4631 /* tsubst in the args corresponding to the template parameters,
4632 including auto if present. Most things will be unchanged, but
4633 make_typename_type and tsubst_qualified_id will resolve
4634 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4635 tree args = current_template_args ();
4636 tree auto_node = type_uses_auto (type);
4637 tree pushed;
4638 if (auto_node)
4639 {
4640 tree auto_vec = make_tree_vec (1);
4641 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4642 args = add_to_template_args (args, auto_vec);
4643 }
4644 pushed = push_scope (scope);
4645 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4646 if (pushed)
4647 pop_scope (scope);
4648 }
4649
4650 if (type == error_mark_node)
4651 return orig_type;
4652
4653 if (TREE_CODE (orig_type) == TYPE_DECL)
4654 {
4655 if (same_type_p (type, TREE_TYPE (orig_type)))
4656 type = orig_type;
4657 else
4658 type = TYPE_NAME (type);
4659 }
4660 return type;
4661 }
4662
4663 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4664 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4665 the new template is a member template. */
4666
4667 tree
4668 build_template_decl (tree decl, tree parms, bool member_template_p)
4669 {
4670 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4671 DECL_TEMPLATE_PARMS (tmpl) = parms;
4672 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4673 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4674 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4675
4676 return tmpl;
4677 }
4678
4679 struct template_parm_data
4680 {
4681 /* The level of the template parameters we are currently
4682 processing. */
4683 int level;
4684
4685 /* The index of the specialization argument we are currently
4686 processing. */
4687 int current_arg;
4688
4689 /* An array whose size is the number of template parameters. The
4690 elements are nonzero if the parameter has been used in any one
4691 of the arguments processed so far. */
4692 int* parms;
4693
4694 /* An array whose size is the number of template arguments. The
4695 elements are nonzero if the argument makes use of template
4696 parameters of this level. */
4697 int* arg_uses_template_parms;
4698 };
4699
4700 /* Subroutine of push_template_decl used to see if each template
4701 parameter in a partial specialization is used in the explicit
4702 argument list. If T is of the LEVEL given in DATA (which is
4703 treated as a template_parm_data*), then DATA->PARMS is marked
4704 appropriately. */
4705
4706 static int
4707 mark_template_parm (tree t, void* data)
4708 {
4709 int level;
4710 int idx;
4711 struct template_parm_data* tpd = (struct template_parm_data*) data;
4712
4713 template_parm_level_and_index (t, &level, &idx);
4714
4715 if (level == tpd->level)
4716 {
4717 tpd->parms[idx] = 1;
4718 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4719 }
4720
4721 /* In C++17 the type of a non-type argument is a deduced context. */
4722 if (cxx_dialect >= cxx17
4723 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4724 for_each_template_parm (TREE_TYPE (t),
4725 &mark_template_parm,
4726 data,
4727 NULL,
4728 /*include_nondeduced_p=*/false);
4729
4730 /* Return zero so that for_each_template_parm will continue the
4731 traversal of the tree; we want to mark *every* template parm. */
4732 return 0;
4733 }
4734
4735 /* Process the partial specialization DECL. */
4736
4737 static tree
4738 process_partial_specialization (tree decl)
4739 {
4740 tree type = TREE_TYPE (decl);
4741 tree tinfo = get_template_info (decl);
4742 tree maintmpl = TI_TEMPLATE (tinfo);
4743 tree specargs = TI_ARGS (tinfo);
4744 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4745 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4746 tree inner_parms;
4747 tree inst;
4748 int nargs = TREE_VEC_LENGTH (inner_args);
4749 int ntparms;
4750 int i;
4751 bool did_error_intro = false;
4752 struct template_parm_data tpd;
4753 struct template_parm_data tpd2;
4754
4755 gcc_assert (current_template_parms);
4756
4757 /* A concept cannot be specialized. */
4758 if (flag_concepts && variable_concept_p (maintmpl))
4759 {
4760 error ("specialization of variable concept %q#D", maintmpl);
4761 return error_mark_node;
4762 }
4763
4764 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4765 ntparms = TREE_VEC_LENGTH (inner_parms);
4766
4767 /* We check that each of the template parameters given in the
4768 partial specialization is used in the argument list to the
4769 specialization. For example:
4770
4771 template <class T> struct S;
4772 template <class T> struct S<T*>;
4773
4774 The second declaration is OK because `T*' uses the template
4775 parameter T, whereas
4776
4777 template <class T> struct S<int>;
4778
4779 is no good. Even trickier is:
4780
4781 template <class T>
4782 struct S1
4783 {
4784 template <class U>
4785 struct S2;
4786 template <class U>
4787 struct S2<T>;
4788 };
4789
4790 The S2<T> declaration is actually invalid; it is a
4791 full-specialization. Of course,
4792
4793 template <class U>
4794 struct S2<T (*)(U)>;
4795
4796 or some such would have been OK. */
4797 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4798 tpd.parms = XALLOCAVEC (int, ntparms);
4799 memset (tpd.parms, 0, sizeof (int) * ntparms);
4800
4801 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4802 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4803 for (i = 0; i < nargs; ++i)
4804 {
4805 tpd.current_arg = i;
4806 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4807 &mark_template_parm,
4808 &tpd,
4809 NULL,
4810 /*include_nondeduced_p=*/false);
4811 }
4812 for (i = 0; i < ntparms; ++i)
4813 if (tpd.parms[i] == 0)
4814 {
4815 /* One of the template parms was not used in a deduced context in the
4816 specialization. */
4817 if (!did_error_intro)
4818 {
4819 error ("template parameters not deducible in "
4820 "partial specialization:");
4821 did_error_intro = true;
4822 }
4823
4824 inform (input_location, " %qD",
4825 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4826 }
4827
4828 if (did_error_intro)
4829 return error_mark_node;
4830
4831 /* [temp.class.spec]
4832
4833 The argument list of the specialization shall not be identical to
4834 the implicit argument list of the primary template. */
4835 tree main_args
4836 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4837 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4838 && (!flag_concepts
4839 || !strictly_subsumes (current_template_constraints (),
4840 get_constraints (maintmpl))))
4841 {
4842 if (!flag_concepts)
4843 error ("partial specialization %q+D does not specialize "
4844 "any template arguments; to define the primary template, "
4845 "remove the template argument list", decl);
4846 else
4847 error ("partial specialization %q+D does not specialize any "
4848 "template arguments and is not more constrained than "
4849 "the primary template; to define the primary template, "
4850 "remove the template argument list", decl);
4851 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4852 }
4853
4854 /* A partial specialization that replaces multiple parameters of the
4855 primary template with a pack expansion is less specialized for those
4856 parameters. */
4857 if (nargs < DECL_NTPARMS (maintmpl))
4858 {
4859 error ("partial specialization is not more specialized than the "
4860 "primary template because it replaces multiple parameters "
4861 "with a pack expansion");
4862 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4863 /* Avoid crash in process_partial_specialization. */
4864 return decl;
4865 }
4866
4867 /* If we aren't in a dependent class, we can actually try deduction. */
4868 else if (tpd.level == 1
4869 /* FIXME we should be able to handle a partial specialization of a
4870 partial instantiation, but currently we can't (c++/41727). */
4871 && TMPL_ARGS_DEPTH (specargs) == 1
4872 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4873 {
4874 if (permerror (input_location, "partial specialization %qD is not "
4875 "more specialized than", decl))
4876 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4877 maintmpl);
4878 }
4879
4880 /* [temp.class.spec]
4881
4882 A partially specialized non-type argument expression shall not
4883 involve template parameters of the partial specialization except
4884 when the argument expression is a simple identifier.
4885
4886 The type of a template parameter corresponding to a specialized
4887 non-type argument shall not be dependent on a parameter of the
4888 specialization.
4889
4890 Also, we verify that pack expansions only occur at the
4891 end of the argument list. */
4892 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4893 tpd2.parms = 0;
4894 for (i = 0; i < nargs; ++i)
4895 {
4896 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4897 tree arg = TREE_VEC_ELT (inner_args, i);
4898 tree packed_args = NULL_TREE;
4899 int j, len = 1;
4900
4901 if (ARGUMENT_PACK_P (arg))
4902 {
4903 /* Extract the arguments from the argument pack. We'll be
4904 iterating over these in the following loop. */
4905 packed_args = ARGUMENT_PACK_ARGS (arg);
4906 len = TREE_VEC_LENGTH (packed_args);
4907 }
4908
4909 for (j = 0; j < len; j++)
4910 {
4911 if (packed_args)
4912 /* Get the Jth argument in the parameter pack. */
4913 arg = TREE_VEC_ELT (packed_args, j);
4914
4915 if (PACK_EXPANSION_P (arg))
4916 {
4917 /* Pack expansions must come at the end of the
4918 argument list. */
4919 if ((packed_args && j < len - 1)
4920 || (!packed_args && i < nargs - 1))
4921 {
4922 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4923 error ("parameter pack argument %qE must be at the "
4924 "end of the template argument list", arg);
4925 else
4926 error ("parameter pack argument %qT must be at the "
4927 "end of the template argument list", arg);
4928 }
4929 }
4930
4931 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4932 /* We only care about the pattern. */
4933 arg = PACK_EXPANSION_PATTERN (arg);
4934
4935 if (/* These first two lines are the `non-type' bit. */
4936 !TYPE_P (arg)
4937 && TREE_CODE (arg) != TEMPLATE_DECL
4938 /* This next two lines are the `argument expression is not just a
4939 simple identifier' condition and also the `specialized
4940 non-type argument' bit. */
4941 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4942 && !(REFERENCE_REF_P (arg)
4943 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4944 {
4945 if ((!packed_args && tpd.arg_uses_template_parms[i])
4946 || (packed_args && uses_template_parms (arg)))
4947 error ("template argument %qE involves template parameter(s)",
4948 arg);
4949 else
4950 {
4951 /* Look at the corresponding template parameter,
4952 marking which template parameters its type depends
4953 upon. */
4954 tree type = TREE_TYPE (parm);
4955
4956 if (!tpd2.parms)
4957 {
4958 /* We haven't yet initialized TPD2. Do so now. */
4959 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4960 /* The number of parameters here is the number in the
4961 main template, which, as checked in the assertion
4962 above, is NARGS. */
4963 tpd2.parms = XALLOCAVEC (int, nargs);
4964 tpd2.level =
4965 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4966 }
4967
4968 /* Mark the template parameters. But this time, we're
4969 looking for the template parameters of the main
4970 template, not in the specialization. */
4971 tpd2.current_arg = i;
4972 tpd2.arg_uses_template_parms[i] = 0;
4973 memset (tpd2.parms, 0, sizeof (int) * nargs);
4974 for_each_template_parm (type,
4975 &mark_template_parm,
4976 &tpd2,
4977 NULL,
4978 /*include_nondeduced_p=*/false);
4979
4980 if (tpd2.arg_uses_template_parms [i])
4981 {
4982 /* The type depended on some template parameters.
4983 If they are fully specialized in the
4984 specialization, that's OK. */
4985 int j;
4986 int count = 0;
4987 for (j = 0; j < nargs; ++j)
4988 if (tpd2.parms[j] != 0
4989 && tpd.arg_uses_template_parms [j])
4990 ++count;
4991 if (count != 0)
4992 error_n (input_location, count,
4993 "type %qT of template argument %qE depends "
4994 "on a template parameter",
4995 "type %qT of template argument %qE depends "
4996 "on template parameters",
4997 type,
4998 arg);
4999 }
5000 }
5001 }
5002 }
5003 }
5004
5005 /* We should only get here once. */
5006 if (TREE_CODE (decl) == TYPE_DECL)
5007 gcc_assert (!COMPLETE_TYPE_P (type));
5008
5009 // Build the template decl.
5010 tree tmpl = build_template_decl (decl, current_template_parms,
5011 DECL_MEMBER_TEMPLATE_P (maintmpl));
5012 TREE_TYPE (tmpl) = type;
5013 DECL_TEMPLATE_RESULT (tmpl) = decl;
5014 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5015 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5016 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5017
5018 /* Give template template parms a DECL_CONTEXT of the template
5019 for which they are a parameter. */
5020 for (i = 0; i < ntparms; ++i)
5021 {
5022 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5023 if (TREE_CODE (parm) == TEMPLATE_DECL)
5024 DECL_CONTEXT (parm) = tmpl;
5025 }
5026
5027 if (VAR_P (decl))
5028 /* We didn't register this in check_explicit_specialization so we could
5029 wait until the constraints were set. */
5030 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5031 else
5032 associate_classtype_constraints (type);
5033
5034 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5035 = tree_cons (specargs, tmpl,
5036 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5037 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5038
5039 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5040 inst = TREE_CHAIN (inst))
5041 {
5042 tree instance = TREE_VALUE (inst);
5043 if (TYPE_P (instance)
5044 ? (COMPLETE_TYPE_P (instance)
5045 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5046 : DECL_TEMPLATE_INSTANTIATION (instance))
5047 {
5048 tree spec = most_specialized_partial_spec (instance, tf_none);
5049 tree inst_decl = (DECL_P (instance)
5050 ? instance : TYPE_NAME (instance));
5051 if (!spec)
5052 /* OK */;
5053 else if (spec == error_mark_node)
5054 permerror (input_location,
5055 "declaration of %qD ambiguates earlier template "
5056 "instantiation for %qD", decl, inst_decl);
5057 else if (TREE_VALUE (spec) == tmpl)
5058 permerror (input_location,
5059 "partial specialization of %qD after instantiation "
5060 "of %qD", decl, inst_decl);
5061 }
5062 }
5063
5064 return decl;
5065 }
5066
5067 /* PARM is a template parameter of some form; return the corresponding
5068 TEMPLATE_PARM_INDEX. */
5069
5070 static tree
5071 get_template_parm_index (tree parm)
5072 {
5073 if (TREE_CODE (parm) == PARM_DECL
5074 || TREE_CODE (parm) == CONST_DECL)
5075 parm = DECL_INITIAL (parm);
5076 else if (TREE_CODE (parm) == TYPE_DECL
5077 || TREE_CODE (parm) == TEMPLATE_DECL)
5078 parm = TREE_TYPE (parm);
5079 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5080 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5081 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5082 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5083 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5084 return parm;
5085 }
5086
5087 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5088 parameter packs used by the template parameter PARM. */
5089
5090 static void
5091 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5092 {
5093 /* A type parm can't refer to another parm. */
5094 if (TREE_CODE (parm) == TYPE_DECL)
5095 return;
5096 else if (TREE_CODE (parm) == PARM_DECL)
5097 {
5098 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5099 ppd, ppd->visited);
5100 return;
5101 }
5102
5103 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5104
5105 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5106 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5107 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5108 }
5109
5110 /* PARM is a template parameter pack. Return any parameter packs used in
5111 its type or the type of any of its template parameters. If there are
5112 any such packs, it will be instantiated into a fixed template parameter
5113 list by partial instantiation rather than be fully deduced. */
5114
5115 tree
5116 fixed_parameter_pack_p (tree parm)
5117 {
5118 /* This can only be true in a member template. */
5119 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5120 return NULL_TREE;
5121 /* This can only be true for a parameter pack. */
5122 if (!template_parameter_pack_p (parm))
5123 return NULL_TREE;
5124 /* A type parm can't refer to another parm. */
5125 if (TREE_CODE (parm) == TYPE_DECL)
5126 return NULL_TREE;
5127
5128 tree parameter_packs = NULL_TREE;
5129 struct find_parameter_pack_data ppd;
5130 ppd.parameter_packs = &parameter_packs;
5131 ppd.visited = new hash_set<tree>;
5132 ppd.type_pack_expansion_p = false;
5133
5134 fixed_parameter_pack_p_1 (parm, &ppd);
5135
5136 delete ppd.visited;
5137 return parameter_packs;
5138 }
5139
5140 /* Check that a template declaration's use of default arguments and
5141 parameter packs is not invalid. Here, PARMS are the template
5142 parameters. IS_PRIMARY is true if DECL is the thing declared by
5143 a primary template. IS_PARTIAL is true if DECL is a partial
5144 specialization.
5145
5146 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5147 function template declaration or a friend class template
5148 declaration. In the function case, 1 indicates a declaration, 2
5149 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5150 emitted for extraneous default arguments.
5151
5152 Returns TRUE if there were no errors found, FALSE otherwise. */
5153
5154 bool
5155 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5156 bool is_partial, int is_friend_decl)
5157 {
5158 const char *msg;
5159 int last_level_to_check;
5160 tree parm_level;
5161 bool no_errors = true;
5162
5163 /* [temp.param]
5164
5165 A default template-argument shall not be specified in a
5166 function template declaration or a function template definition, nor
5167 in the template-parameter-list of the definition of a member of a
5168 class template. */
5169
5170 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5171 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5172 /* You can't have a function template declaration in a local
5173 scope, nor you can you define a member of a class template in a
5174 local scope. */
5175 return true;
5176
5177 if ((TREE_CODE (decl) == TYPE_DECL
5178 && TREE_TYPE (decl)
5179 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5180 || (TREE_CODE (decl) == FUNCTION_DECL
5181 && LAMBDA_FUNCTION_P (decl)))
5182 /* A lambda doesn't have an explicit declaration; don't complain
5183 about the parms of the enclosing class. */
5184 return true;
5185
5186 if (current_class_type
5187 && !TYPE_BEING_DEFINED (current_class_type)
5188 && DECL_LANG_SPECIFIC (decl)
5189 && DECL_DECLARES_FUNCTION_P (decl)
5190 /* If this is either a friend defined in the scope of the class
5191 or a member function. */
5192 && (DECL_FUNCTION_MEMBER_P (decl)
5193 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5194 : DECL_FRIEND_CONTEXT (decl)
5195 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5196 : false)
5197 /* And, if it was a member function, it really was defined in
5198 the scope of the class. */
5199 && (!DECL_FUNCTION_MEMBER_P (decl)
5200 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5201 /* We already checked these parameters when the template was
5202 declared, so there's no need to do it again now. This function
5203 was defined in class scope, but we're processing its body now
5204 that the class is complete. */
5205 return true;
5206
5207 /* Core issue 226 (C++0x only): the following only applies to class
5208 templates. */
5209 if (is_primary
5210 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5211 {
5212 /* [temp.param]
5213
5214 If a template-parameter has a default template-argument, all
5215 subsequent template-parameters shall have a default
5216 template-argument supplied. */
5217 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5218 {
5219 tree inner_parms = TREE_VALUE (parm_level);
5220 int ntparms = TREE_VEC_LENGTH (inner_parms);
5221 int seen_def_arg_p = 0;
5222 int i;
5223
5224 for (i = 0; i < ntparms; ++i)
5225 {
5226 tree parm = TREE_VEC_ELT (inner_parms, i);
5227
5228 if (parm == error_mark_node)
5229 continue;
5230
5231 if (TREE_PURPOSE (parm))
5232 seen_def_arg_p = 1;
5233 else if (seen_def_arg_p
5234 && !template_parameter_pack_p (TREE_VALUE (parm)))
5235 {
5236 error ("no default argument for %qD", TREE_VALUE (parm));
5237 /* For better subsequent error-recovery, we indicate that
5238 there should have been a default argument. */
5239 TREE_PURPOSE (parm) = error_mark_node;
5240 no_errors = false;
5241 }
5242 else if (!is_partial
5243 && !is_friend_decl
5244 /* Don't complain about an enclosing partial
5245 specialization. */
5246 && parm_level == parms
5247 && TREE_CODE (decl) == TYPE_DECL
5248 && i < ntparms - 1
5249 && template_parameter_pack_p (TREE_VALUE (parm))
5250 /* A fixed parameter pack will be partially
5251 instantiated into a fixed length list. */
5252 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5253 {
5254 /* A primary class template can only have one
5255 parameter pack, at the end of the template
5256 parameter list. */
5257
5258 error ("parameter pack %q+D must be at the end of the"
5259 " template parameter list", TREE_VALUE (parm));
5260
5261 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5262 = error_mark_node;
5263 no_errors = false;
5264 }
5265 }
5266 }
5267 }
5268
5269 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5270 || is_partial
5271 || !is_primary
5272 || is_friend_decl)
5273 /* For an ordinary class template, default template arguments are
5274 allowed at the innermost level, e.g.:
5275 template <class T = int>
5276 struct S {};
5277 but, in a partial specialization, they're not allowed even
5278 there, as we have in [temp.class.spec]:
5279
5280 The template parameter list of a specialization shall not
5281 contain default template argument values.
5282
5283 So, for a partial specialization, or for a function template
5284 (in C++98/C++03), we look at all of them. */
5285 ;
5286 else
5287 /* But, for a primary class template that is not a partial
5288 specialization we look at all template parameters except the
5289 innermost ones. */
5290 parms = TREE_CHAIN (parms);
5291
5292 /* Figure out what error message to issue. */
5293 if (is_friend_decl == 2)
5294 msg = G_("default template arguments may not be used in function template "
5295 "friend re-declaration");
5296 else if (is_friend_decl)
5297 msg = G_("default template arguments may not be used in template "
5298 "friend declarations");
5299 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5300 msg = G_("default template arguments may not be used in function templates "
5301 "without -std=c++11 or -std=gnu++11");
5302 else if (is_partial)
5303 msg = G_("default template arguments may not be used in "
5304 "partial specializations");
5305 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5306 msg = G_("default argument for template parameter for class enclosing %qD");
5307 else
5308 /* Per [temp.param]/9, "A default template-argument shall not be
5309 specified in the template-parameter-lists of the definition of
5310 a member of a class template that appears outside of the member's
5311 class.", thus if we aren't handling a member of a class template
5312 there is no need to examine the parameters. */
5313 return true;
5314
5315 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5316 /* If we're inside a class definition, there's no need to
5317 examine the parameters to the class itself. On the one
5318 hand, they will be checked when the class is defined, and,
5319 on the other, default arguments are valid in things like:
5320 template <class T = double>
5321 struct S { template <class U> void f(U); };
5322 Here the default argument for `S' has no bearing on the
5323 declaration of `f'. */
5324 last_level_to_check = template_class_depth (current_class_type) + 1;
5325 else
5326 /* Check everything. */
5327 last_level_to_check = 0;
5328
5329 for (parm_level = parms;
5330 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5331 parm_level = TREE_CHAIN (parm_level))
5332 {
5333 tree inner_parms = TREE_VALUE (parm_level);
5334 int i;
5335 int ntparms;
5336
5337 ntparms = TREE_VEC_LENGTH (inner_parms);
5338 for (i = 0; i < ntparms; ++i)
5339 {
5340 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5341 continue;
5342
5343 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5344 {
5345 if (msg)
5346 {
5347 no_errors = false;
5348 if (is_friend_decl == 2)
5349 return no_errors;
5350
5351 error (msg, decl);
5352 msg = 0;
5353 }
5354
5355 /* Clear out the default argument so that we are not
5356 confused later. */
5357 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5358 }
5359 }
5360
5361 /* At this point, if we're still interested in issuing messages,
5362 they must apply to classes surrounding the object declared. */
5363 if (msg)
5364 msg = G_("default argument for template parameter for class "
5365 "enclosing %qD");
5366 }
5367
5368 return no_errors;
5369 }
5370
5371 /* Worker for push_template_decl_real, called via
5372 for_each_template_parm. DATA is really an int, indicating the
5373 level of the parameters we are interested in. If T is a template
5374 parameter of that level, return nonzero. */
5375
5376 static int
5377 template_parm_this_level_p (tree t, void* data)
5378 {
5379 int this_level = *(int *)data;
5380 int level;
5381
5382 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5383 level = TEMPLATE_PARM_LEVEL (t);
5384 else
5385 level = TEMPLATE_TYPE_LEVEL (t);
5386 return level == this_level;
5387 }
5388
5389 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5390 DATA is really an int, indicating the innermost outer level of parameters.
5391 If T is a template parameter of that level or further out, return
5392 nonzero. */
5393
5394 static int
5395 template_parm_outer_level (tree t, void *data)
5396 {
5397 int this_level = *(int *)data;
5398 int level;
5399
5400 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5401 level = TEMPLATE_PARM_LEVEL (t);
5402 else
5403 level = TEMPLATE_TYPE_LEVEL (t);
5404 return level <= this_level;
5405 }
5406
5407 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5408 parameters given by current_template_args, or reuses a
5409 previously existing one, if appropriate. Returns the DECL, or an
5410 equivalent one, if it is replaced via a call to duplicate_decls.
5411
5412 If IS_FRIEND is true, DECL is a friend declaration. */
5413
5414 tree
5415 push_template_decl_real (tree decl, bool is_friend)
5416 {
5417 tree tmpl;
5418 tree args;
5419 tree info;
5420 tree ctx;
5421 bool is_primary;
5422 bool is_partial;
5423 int new_template_p = 0;
5424 /* True if the template is a member template, in the sense of
5425 [temp.mem]. */
5426 bool member_template_p = false;
5427
5428 if (decl == error_mark_node || !current_template_parms)
5429 return error_mark_node;
5430
5431 /* See if this is a partial specialization. */
5432 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5433 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5434 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5435 || (VAR_P (decl)
5436 && DECL_LANG_SPECIFIC (decl)
5437 && DECL_TEMPLATE_SPECIALIZATION (decl)
5438 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5439
5440 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5441 is_friend = true;
5442
5443 if (is_friend)
5444 /* For a friend, we want the context of the friend, not
5445 the type of which it is a friend. */
5446 ctx = CP_DECL_CONTEXT (decl);
5447 else if (CP_DECL_CONTEXT (decl)
5448 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5449 /* In the case of a virtual function, we want the class in which
5450 it is defined. */
5451 ctx = CP_DECL_CONTEXT (decl);
5452 else
5453 /* Otherwise, if we're currently defining some class, the DECL
5454 is assumed to be a member of the class. */
5455 ctx = current_scope ();
5456
5457 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5458 ctx = NULL_TREE;
5459
5460 if (!DECL_CONTEXT (decl))
5461 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5462
5463 /* See if this is a primary template. */
5464 if (is_friend && ctx
5465 && uses_template_parms_level (ctx, processing_template_decl))
5466 /* A friend template that specifies a class context, i.e.
5467 template <typename T> friend void A<T>::f();
5468 is not primary. */
5469 is_primary = false;
5470 else if (TREE_CODE (decl) == TYPE_DECL
5471 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5472 is_primary = false;
5473 else
5474 is_primary = template_parm_scope_p ();
5475
5476 if (is_primary)
5477 {
5478 warning (OPT_Wtemplates, "template %qD declared", decl);
5479
5480 if (DECL_CLASS_SCOPE_P (decl))
5481 member_template_p = true;
5482 if (TREE_CODE (decl) == TYPE_DECL
5483 && anon_aggrname_p (DECL_NAME (decl)))
5484 {
5485 error ("template class without a name");
5486 return error_mark_node;
5487 }
5488 else if (TREE_CODE (decl) == FUNCTION_DECL)
5489 {
5490 if (member_template_p)
5491 {
5492 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5493 error ("member template %qD may not have virt-specifiers", decl);
5494 }
5495 if (DECL_DESTRUCTOR_P (decl))
5496 {
5497 /* [temp.mem]
5498
5499 A destructor shall not be a member template. */
5500 error ("destructor %qD declared as member template", decl);
5501 return error_mark_node;
5502 }
5503 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5504 && (!prototype_p (TREE_TYPE (decl))
5505 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5506 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5507 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5508 == void_list_node)))
5509 {
5510 /* [basic.stc.dynamic.allocation]
5511
5512 An allocation function can be a function
5513 template. ... Template allocation functions shall
5514 have two or more parameters. */
5515 error ("invalid template declaration of %qD", decl);
5516 return error_mark_node;
5517 }
5518 }
5519 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5520 && CLASS_TYPE_P (TREE_TYPE (decl)))
5521 {
5522 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5523 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5524 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5525 {
5526 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5527 if (TREE_CODE (t) == TYPE_DECL)
5528 t = TREE_TYPE (t);
5529 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5530 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5531 }
5532 }
5533 else if (TREE_CODE (decl) == TYPE_DECL
5534 && TYPE_DECL_ALIAS_P (decl))
5535 /* alias-declaration */
5536 gcc_assert (!DECL_ARTIFICIAL (decl));
5537 else if (VAR_P (decl))
5538 /* C++14 variable template. */;
5539 else
5540 {
5541 error ("template declaration of %q#D", decl);
5542 return error_mark_node;
5543 }
5544 }
5545
5546 /* Check to see that the rules regarding the use of default
5547 arguments are not being violated. We check args for a friend
5548 functions when we know whether it's a definition, introducing
5549 declaration or re-declaration. */
5550 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5551 check_default_tmpl_args (decl, current_template_parms,
5552 is_primary, is_partial, is_friend);
5553
5554 /* Ensure that there are no parameter packs in the type of this
5555 declaration that have not been expanded. */
5556 if (TREE_CODE (decl) == FUNCTION_DECL)
5557 {
5558 /* Check each of the arguments individually to see if there are
5559 any bare parameter packs. */
5560 tree type = TREE_TYPE (decl);
5561 tree arg = DECL_ARGUMENTS (decl);
5562 tree argtype = TYPE_ARG_TYPES (type);
5563
5564 while (arg && argtype)
5565 {
5566 if (!DECL_PACK_P (arg)
5567 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5568 {
5569 /* This is a PARM_DECL that contains unexpanded parameter
5570 packs. We have already complained about this in the
5571 check_for_bare_parameter_packs call, so just replace
5572 these types with ERROR_MARK_NODE. */
5573 TREE_TYPE (arg) = error_mark_node;
5574 TREE_VALUE (argtype) = error_mark_node;
5575 }
5576
5577 arg = DECL_CHAIN (arg);
5578 argtype = TREE_CHAIN (argtype);
5579 }
5580
5581 /* Check for bare parameter packs in the return type and the
5582 exception specifiers. */
5583 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5584 /* Errors were already issued, set return type to int
5585 as the frontend doesn't expect error_mark_node as
5586 the return type. */
5587 TREE_TYPE (type) = integer_type_node;
5588 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5589 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5590 }
5591 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5592 && TYPE_DECL_ALIAS_P (decl))
5593 ? DECL_ORIGINAL_TYPE (decl)
5594 : TREE_TYPE (decl)))
5595 {
5596 TREE_TYPE (decl) = error_mark_node;
5597 return error_mark_node;
5598 }
5599
5600 if (is_partial)
5601 return process_partial_specialization (decl);
5602
5603 args = current_template_args ();
5604
5605 if (!ctx
5606 || TREE_CODE (ctx) == FUNCTION_DECL
5607 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5608 || (TREE_CODE (decl) == TYPE_DECL
5609 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5610 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5611 {
5612 if (DECL_LANG_SPECIFIC (decl)
5613 && DECL_TEMPLATE_INFO (decl)
5614 && DECL_TI_TEMPLATE (decl))
5615 tmpl = DECL_TI_TEMPLATE (decl);
5616 /* If DECL is a TYPE_DECL for a class-template, then there won't
5617 be DECL_LANG_SPECIFIC. The information equivalent to
5618 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5619 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5620 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5621 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5622 {
5623 /* Since a template declaration already existed for this
5624 class-type, we must be redeclaring it here. Make sure
5625 that the redeclaration is valid. */
5626 redeclare_class_template (TREE_TYPE (decl),
5627 current_template_parms,
5628 current_template_constraints ());
5629 /* We don't need to create a new TEMPLATE_DECL; just use the
5630 one we already had. */
5631 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5632 }
5633 else
5634 {
5635 tmpl = build_template_decl (decl, current_template_parms,
5636 member_template_p);
5637 new_template_p = 1;
5638
5639 if (DECL_LANG_SPECIFIC (decl)
5640 && DECL_TEMPLATE_SPECIALIZATION (decl))
5641 {
5642 /* A specialization of a member template of a template
5643 class. */
5644 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5645 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5646 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5647 }
5648 }
5649 }
5650 else
5651 {
5652 tree a, t, current, parms;
5653 int i;
5654 tree tinfo = get_template_info (decl);
5655
5656 if (!tinfo)
5657 {
5658 error ("template definition of non-template %q#D", decl);
5659 return error_mark_node;
5660 }
5661
5662 tmpl = TI_TEMPLATE (tinfo);
5663
5664 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5665 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5666 && DECL_TEMPLATE_SPECIALIZATION (decl)
5667 && DECL_MEMBER_TEMPLATE_P (tmpl))
5668 {
5669 tree new_tmpl;
5670
5671 /* The declaration is a specialization of a member
5672 template, declared outside the class. Therefore, the
5673 innermost template arguments will be NULL, so we
5674 replace them with the arguments determined by the
5675 earlier call to check_explicit_specialization. */
5676 args = DECL_TI_ARGS (decl);
5677
5678 new_tmpl
5679 = build_template_decl (decl, current_template_parms,
5680 member_template_p);
5681 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5682 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5683 DECL_TI_TEMPLATE (decl) = new_tmpl;
5684 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5685 DECL_TEMPLATE_INFO (new_tmpl)
5686 = build_template_info (tmpl, args);
5687
5688 register_specialization (new_tmpl,
5689 most_general_template (tmpl),
5690 args,
5691 is_friend, 0);
5692 return decl;
5693 }
5694
5695 /* Make sure the template headers we got make sense. */
5696
5697 parms = DECL_TEMPLATE_PARMS (tmpl);
5698 i = TMPL_PARMS_DEPTH (parms);
5699 if (TMPL_ARGS_DEPTH (args) != i)
5700 {
5701 error ("expected %d levels of template parms for %q#D, got %d",
5702 i, decl, TMPL_ARGS_DEPTH (args));
5703 DECL_INTERFACE_KNOWN (decl) = 1;
5704 return error_mark_node;
5705 }
5706 else
5707 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5708 {
5709 a = TMPL_ARGS_LEVEL (args, i);
5710 t = INNERMOST_TEMPLATE_PARMS (parms);
5711
5712 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5713 {
5714 if (current == decl)
5715 error ("got %d template parameters for %q#D",
5716 TREE_VEC_LENGTH (a), decl);
5717 else
5718 error ("got %d template parameters for %q#T",
5719 TREE_VEC_LENGTH (a), current);
5720 error (" but %d required", TREE_VEC_LENGTH (t));
5721 /* Avoid crash in import_export_decl. */
5722 DECL_INTERFACE_KNOWN (decl) = 1;
5723 return error_mark_node;
5724 }
5725
5726 if (current == decl)
5727 current = ctx;
5728 else if (current == NULL_TREE)
5729 /* Can happen in erroneous input. */
5730 break;
5731 else
5732 current = get_containing_scope (current);
5733 }
5734
5735 /* Check that the parms are used in the appropriate qualifying scopes
5736 in the declarator. */
5737 if (!comp_template_args
5738 (TI_ARGS (tinfo),
5739 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5740 {
5741 error ("template arguments to %qD do not match original "
5742 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5743 if (!uses_template_parms (TI_ARGS (tinfo)))
5744 inform (input_location, "use %<template<>%> for"
5745 " an explicit specialization");
5746 /* Avoid crash in import_export_decl. */
5747 DECL_INTERFACE_KNOWN (decl) = 1;
5748 return error_mark_node;
5749 }
5750 }
5751
5752 DECL_TEMPLATE_RESULT (tmpl) = decl;
5753 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5754
5755 /* Push template declarations for global functions and types. Note
5756 that we do not try to push a global template friend declared in a
5757 template class; such a thing may well depend on the template
5758 parameters of the class. */
5759 if (new_template_p && !ctx
5760 && !(is_friend && template_class_depth (current_class_type) > 0))
5761 {
5762 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5763 if (tmpl == error_mark_node)
5764 return error_mark_node;
5765
5766 /* Hide template friend classes that haven't been declared yet. */
5767 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5768 {
5769 DECL_ANTICIPATED (tmpl) = 1;
5770 DECL_FRIEND_P (tmpl) = 1;
5771 }
5772 }
5773
5774 if (is_primary)
5775 {
5776 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5777
5778 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5779
5780 /* Give template template parms a DECL_CONTEXT of the template
5781 for which they are a parameter. */
5782 parms = INNERMOST_TEMPLATE_PARMS (parms);
5783 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5784 {
5785 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5786 if (TREE_CODE (parm) == TEMPLATE_DECL)
5787 DECL_CONTEXT (parm) = tmpl;
5788 }
5789
5790 if (TREE_CODE (decl) == TYPE_DECL
5791 && TYPE_DECL_ALIAS_P (decl)
5792 && complex_alias_template_p (tmpl))
5793 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5794 }
5795
5796 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5797 back to its most general template. If TMPL is a specialization,
5798 ARGS may only have the innermost set of arguments. Add the missing
5799 argument levels if necessary. */
5800 if (DECL_TEMPLATE_INFO (tmpl))
5801 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5802
5803 info = build_template_info (tmpl, args);
5804
5805 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5806 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5807 else
5808 {
5809 if (is_primary)
5810 retrofit_lang_decl (decl);
5811 if (DECL_LANG_SPECIFIC (decl))
5812 DECL_TEMPLATE_INFO (decl) = info;
5813 }
5814
5815 if (flag_implicit_templates
5816 && !is_friend
5817 && TREE_PUBLIC (decl)
5818 && VAR_OR_FUNCTION_DECL_P (decl))
5819 /* Set DECL_COMDAT on template instantiations; if we force
5820 them to be emitted by explicit instantiation or -frepo,
5821 mark_needed will tell cgraph to do the right thing. */
5822 DECL_COMDAT (decl) = true;
5823
5824 return DECL_TEMPLATE_RESULT (tmpl);
5825 }
5826
5827 tree
5828 push_template_decl (tree decl)
5829 {
5830 return push_template_decl_real (decl, false);
5831 }
5832
5833 /* FN is an inheriting constructor that inherits from the constructor
5834 template INHERITED; turn FN into a constructor template with a matching
5835 template header. */
5836
5837 tree
5838 add_inherited_template_parms (tree fn, tree inherited)
5839 {
5840 tree inner_parms
5841 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5842 inner_parms = copy_node (inner_parms);
5843 tree parms
5844 = tree_cons (size_int (processing_template_decl + 1),
5845 inner_parms, current_template_parms);
5846 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5847 tree args = template_parms_to_args (parms);
5848 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5849 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5850 DECL_TEMPLATE_RESULT (tmpl) = fn;
5851 DECL_ARTIFICIAL (tmpl) = true;
5852 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5853 return tmpl;
5854 }
5855
5856 /* Called when a class template TYPE is redeclared with the indicated
5857 template PARMS, e.g.:
5858
5859 template <class T> struct S;
5860 template <class T> struct S {}; */
5861
5862 bool
5863 redeclare_class_template (tree type, tree parms, tree cons)
5864 {
5865 tree tmpl;
5866 tree tmpl_parms;
5867 int i;
5868
5869 if (!TYPE_TEMPLATE_INFO (type))
5870 {
5871 error ("%qT is not a template type", type);
5872 return false;
5873 }
5874
5875 tmpl = TYPE_TI_TEMPLATE (type);
5876 if (!PRIMARY_TEMPLATE_P (tmpl))
5877 /* The type is nested in some template class. Nothing to worry
5878 about here; there are no new template parameters for the nested
5879 type. */
5880 return true;
5881
5882 if (!parms)
5883 {
5884 error ("template specifiers not specified in declaration of %qD",
5885 tmpl);
5886 return false;
5887 }
5888
5889 parms = INNERMOST_TEMPLATE_PARMS (parms);
5890 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5891
5892 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5893 {
5894 error_n (input_location, TREE_VEC_LENGTH (parms),
5895 "redeclared with %d template parameter",
5896 "redeclared with %d template parameters",
5897 TREE_VEC_LENGTH (parms));
5898 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5899 "previous declaration %qD used %d template parameter",
5900 "previous declaration %qD used %d template parameters",
5901 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5902 return false;
5903 }
5904
5905 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5906 {
5907 tree tmpl_parm;
5908 tree parm;
5909 tree tmpl_default;
5910 tree parm_default;
5911
5912 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5913 || TREE_VEC_ELT (parms, i) == error_mark_node)
5914 continue;
5915
5916 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5917 if (error_operand_p (tmpl_parm))
5918 return false;
5919
5920 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5921 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5922 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5923
5924 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5925 TEMPLATE_DECL. */
5926 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5927 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5928 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5929 || (TREE_CODE (tmpl_parm) != PARM_DECL
5930 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5931 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5932 || (TREE_CODE (tmpl_parm) == PARM_DECL
5933 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5934 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5935 {
5936 error ("template parameter %q+#D", tmpl_parm);
5937 error ("redeclared here as %q#D", parm);
5938 return false;
5939 }
5940
5941 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5942 {
5943 /* We have in [temp.param]:
5944
5945 A template-parameter may not be given default arguments
5946 by two different declarations in the same scope. */
5947 error_at (input_location, "redefinition of default argument for %q#D", parm);
5948 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5949 "original definition appeared here");
5950 return false;
5951 }
5952
5953 if (parm_default != NULL_TREE)
5954 /* Update the previous template parameters (which are the ones
5955 that will really count) with the new default value. */
5956 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5957 else if (tmpl_default != NULL_TREE)
5958 /* Update the new parameters, too; they'll be used as the
5959 parameters for any members. */
5960 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5961
5962 /* Give each template template parm in this redeclaration a
5963 DECL_CONTEXT of the template for which they are a parameter. */
5964 if (TREE_CODE (parm) == TEMPLATE_DECL)
5965 {
5966 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5967 DECL_CONTEXT (parm) = tmpl;
5968 }
5969
5970 if (TREE_CODE (parm) == TYPE_DECL)
5971 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5972 }
5973
5974 // Cannot redeclare a class template with a different set of constraints.
5975 if (!equivalent_constraints (get_constraints (tmpl), cons))
5976 {
5977 error_at (input_location, "redeclaration %q#D with different "
5978 "constraints", tmpl);
5979 inform (DECL_SOURCE_LOCATION (tmpl),
5980 "original declaration appeared here");
5981 }
5982
5983 return true;
5984 }
5985
5986 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5987 to be used when the caller has already checked
5988 (processing_template_decl
5989 && !instantiation_dependent_expression_p (expr)
5990 && potential_constant_expression (expr))
5991 and cleared processing_template_decl. */
5992
5993 tree
5994 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5995 {
5996 return tsubst_copy_and_build (expr,
5997 /*args=*/NULL_TREE,
5998 complain,
5999 /*in_decl=*/NULL_TREE,
6000 /*function_p=*/false,
6001 /*integral_constant_expression_p=*/true);
6002 }
6003
6004 /* Simplify EXPR if it is a non-dependent expression. Returns the
6005 (possibly simplified) expression. */
6006
6007 tree
6008 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6009 {
6010 if (expr == NULL_TREE)
6011 return NULL_TREE;
6012
6013 /* If we're in a template, but EXPR isn't value dependent, simplify
6014 it. We're supposed to treat:
6015
6016 template <typename T> void f(T[1 + 1]);
6017 template <typename T> void f(T[2]);
6018
6019 as two declarations of the same function, for example. */
6020 if (processing_template_decl
6021 && is_nondependent_constant_expression (expr))
6022 {
6023 processing_template_decl_sentinel s;
6024 expr = instantiate_non_dependent_expr_internal (expr, complain);
6025 }
6026 return expr;
6027 }
6028
6029 tree
6030 instantiate_non_dependent_expr (tree expr)
6031 {
6032 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6033 }
6034
6035 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6036 an uninstantiated expression. */
6037
6038 tree
6039 instantiate_non_dependent_or_null (tree expr)
6040 {
6041 if (expr == NULL_TREE)
6042 return NULL_TREE;
6043 if (processing_template_decl)
6044 {
6045 if (!is_nondependent_constant_expression (expr))
6046 expr = NULL_TREE;
6047 else
6048 {
6049 processing_template_decl_sentinel s;
6050 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6051 }
6052 }
6053 return expr;
6054 }
6055
6056 /* True iff T is a specialization of a variable template. */
6057
6058 bool
6059 variable_template_specialization_p (tree t)
6060 {
6061 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6062 return false;
6063 tree tmpl = DECL_TI_TEMPLATE (t);
6064 return variable_template_p (tmpl);
6065 }
6066
6067 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6068 template declaration, or a TYPE_DECL for an alias declaration. */
6069
6070 bool
6071 alias_type_or_template_p (tree t)
6072 {
6073 if (t == NULL_TREE)
6074 return false;
6075 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6076 || (TYPE_P (t)
6077 && TYPE_NAME (t)
6078 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6079 || DECL_ALIAS_TEMPLATE_P (t));
6080 }
6081
6082 /* Return TRUE iff T is a specialization of an alias template. */
6083
6084 bool
6085 alias_template_specialization_p (const_tree t)
6086 {
6087 /* It's an alias template specialization if it's an alias and its
6088 TYPE_NAME is a specialization of a primary template. */
6089 if (TYPE_ALIAS_P (t))
6090 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6091 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6092
6093 return false;
6094 }
6095
6096 /* An alias template is complex from a SFINAE perspective if a template-id
6097 using that alias can be ill-formed when the expansion is not, as with
6098 the void_t template. We determine this by checking whether the
6099 expansion for the alias template uses all its template parameters. */
6100
6101 struct uses_all_template_parms_data
6102 {
6103 int level;
6104 bool *seen;
6105 };
6106
6107 static int
6108 uses_all_template_parms_r (tree t, void *data_)
6109 {
6110 struct uses_all_template_parms_data &data
6111 = *(struct uses_all_template_parms_data*)data_;
6112 tree idx = get_template_parm_index (t);
6113
6114 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6115 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6116 return 0;
6117 }
6118
6119 static bool
6120 complex_alias_template_p (const_tree tmpl)
6121 {
6122 struct uses_all_template_parms_data data;
6123 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6124 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6125 data.level = TMPL_PARMS_DEPTH (parms);
6126 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6127 data.seen = XALLOCAVEC (bool, len);
6128 for (int i = 0; i < len; ++i)
6129 data.seen[i] = false;
6130
6131 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6132 for (int i = 0; i < len; ++i)
6133 if (!data.seen[i])
6134 return true;
6135 return false;
6136 }
6137
6138 /* Return TRUE iff T is a specialization of a complex alias template with
6139 dependent template-arguments. */
6140
6141 bool
6142 dependent_alias_template_spec_p (const_tree t)
6143 {
6144 if (!alias_template_specialization_p (t))
6145 return false;
6146
6147 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6148 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6149 return false;
6150
6151 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6152 if (!any_dependent_template_arguments_p (args))
6153 return false;
6154
6155 return true;
6156 }
6157
6158 /* Return the number of innermost template parameters in TMPL. */
6159
6160 static int
6161 num_innermost_template_parms (tree tmpl)
6162 {
6163 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6164 return TREE_VEC_LENGTH (parms);
6165 }
6166
6167 /* Return either TMPL or another template that it is equivalent to under DR
6168 1286: An alias that just changes the name of a template is equivalent to
6169 the other template. */
6170
6171 static tree
6172 get_underlying_template (tree tmpl)
6173 {
6174 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6175 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6176 {
6177 /* Determine if the alias is equivalent to an underlying template. */
6178 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6179 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6180 if (!tinfo)
6181 break;
6182
6183 tree underlying = TI_TEMPLATE (tinfo);
6184 if (!PRIMARY_TEMPLATE_P (underlying)
6185 || (num_innermost_template_parms (tmpl)
6186 != num_innermost_template_parms (underlying)))
6187 break;
6188
6189 tree alias_args = INNERMOST_TEMPLATE_ARGS
6190 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6191 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6192 break;
6193
6194 /* Alias is equivalent. Strip it and repeat. */
6195 tmpl = underlying;
6196 }
6197
6198 return tmpl;
6199 }
6200
6201 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6202 must be a reference-to-function or a pointer-to-function type, as specified
6203 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6204 and check that the resulting function has external linkage. */
6205
6206 static tree
6207 convert_nontype_argument_function (tree type, tree expr,
6208 tsubst_flags_t complain)
6209 {
6210 tree fns = expr;
6211 tree fn, fn_no_ptr;
6212 linkage_kind linkage;
6213
6214 fn = instantiate_type (type, fns, tf_none);
6215 if (fn == error_mark_node)
6216 return error_mark_node;
6217
6218 if (value_dependent_expression_p (fn))
6219 goto accept;
6220
6221 fn_no_ptr = strip_fnptr_conv (fn);
6222 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6223 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6224 if (BASELINK_P (fn_no_ptr))
6225 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6226
6227 /* [temp.arg.nontype]/1
6228
6229 A template-argument for a non-type, non-template template-parameter
6230 shall be one of:
6231 [...]
6232 -- the address of an object or function with external [C++11: or
6233 internal] linkage. */
6234
6235 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6236 {
6237 if (complain & tf_error)
6238 {
6239 error ("%qE is not a valid template argument for type %qT",
6240 expr, type);
6241 if (TYPE_PTR_P (type))
6242 inform (input_location, "it must be the address of a function "
6243 "with external linkage");
6244 else
6245 inform (input_location, "it must be the name of a function with "
6246 "external linkage");
6247 }
6248 return NULL_TREE;
6249 }
6250
6251 linkage = decl_linkage (fn_no_ptr);
6252 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6253 {
6254 if (complain & tf_error)
6255 {
6256 if (cxx_dialect >= cxx11)
6257 error ("%qE is not a valid template argument for type %qT "
6258 "because %qD has no linkage",
6259 expr, type, fn_no_ptr);
6260 else
6261 error ("%qE is not a valid template argument for type %qT "
6262 "because %qD does not have external linkage",
6263 expr, type, fn_no_ptr);
6264 }
6265 return NULL_TREE;
6266 }
6267
6268 accept:
6269 if (TREE_CODE (type) == REFERENCE_TYPE)
6270 {
6271 if (REFERENCE_REF_P (fn))
6272 fn = TREE_OPERAND (fn, 0);
6273 else
6274 fn = build_address (fn);
6275 }
6276 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6277 fn = build_nop (type, fn);
6278
6279 return fn;
6280 }
6281
6282 /* Subroutine of convert_nontype_argument.
6283 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6284 Emit an error otherwise. */
6285
6286 static bool
6287 check_valid_ptrmem_cst_expr (tree type, tree expr,
6288 tsubst_flags_t complain)
6289 {
6290 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6291 tree orig_expr = expr;
6292 STRIP_NOPS (expr);
6293 if (null_ptr_cst_p (expr))
6294 return true;
6295 if (TREE_CODE (expr) == PTRMEM_CST
6296 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6297 PTRMEM_CST_CLASS (expr)))
6298 return true;
6299 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6300 return true;
6301 if (processing_template_decl
6302 && TREE_CODE (expr) == ADDR_EXPR
6303 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6304 return true;
6305 if (complain & tf_error)
6306 {
6307 error_at (loc, "%qE is not a valid template argument for type %qT",
6308 orig_expr, type);
6309 if (TREE_CODE (expr) != PTRMEM_CST)
6310 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6311 else
6312 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6313 }
6314 return false;
6315 }
6316
6317 /* Returns TRUE iff the address of OP is value-dependent.
6318
6319 14.6.2.4 [temp.dep.temp]:
6320 A non-integral non-type template-argument is dependent if its type is
6321 dependent or it has either of the following forms
6322 qualified-id
6323 & qualified-id
6324 and contains a nested-name-specifier which specifies a class-name that
6325 names a dependent type.
6326
6327 We generalize this to just say that the address of a member of a
6328 dependent class is value-dependent; the above doesn't cover the
6329 address of a static data member named with an unqualified-id. */
6330
6331 static bool
6332 has_value_dependent_address (tree op)
6333 {
6334 /* We could use get_inner_reference here, but there's no need;
6335 this is only relevant for template non-type arguments, which
6336 can only be expressed as &id-expression. */
6337 if (DECL_P (op))
6338 {
6339 tree ctx = CP_DECL_CONTEXT (op);
6340 if (TYPE_P (ctx) && dependent_type_p (ctx))
6341 return true;
6342 }
6343
6344 return false;
6345 }
6346
6347 /* The next set of functions are used for providing helpful explanatory
6348 diagnostics for failed overload resolution. Their messages should be
6349 indented by two spaces for consistency with the messages in
6350 call.c */
6351
6352 static int
6353 unify_success (bool /*explain_p*/)
6354 {
6355 return 0;
6356 }
6357
6358 /* Other failure functions should call this one, to provide a single function
6359 for setting a breakpoint on. */
6360
6361 static int
6362 unify_invalid (bool /*explain_p*/)
6363 {
6364 return 1;
6365 }
6366
6367 static int
6368 unify_parameter_deduction_failure (bool explain_p, tree parm)
6369 {
6370 if (explain_p)
6371 inform (input_location,
6372 " couldn't deduce template parameter %qD", parm);
6373 return unify_invalid (explain_p);
6374 }
6375
6376 static int
6377 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6378 {
6379 if (explain_p)
6380 inform (input_location,
6381 " types %qT and %qT have incompatible cv-qualifiers",
6382 parm, arg);
6383 return unify_invalid (explain_p);
6384 }
6385
6386 static int
6387 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6388 {
6389 if (explain_p)
6390 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6391 return unify_invalid (explain_p);
6392 }
6393
6394 static int
6395 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6396 {
6397 if (explain_p)
6398 inform (input_location,
6399 " template parameter %qD is not a parameter pack, but "
6400 "argument %qD is",
6401 parm, arg);
6402 return unify_invalid (explain_p);
6403 }
6404
6405 static int
6406 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6407 {
6408 if (explain_p)
6409 inform (input_location,
6410 " template argument %qE does not match "
6411 "pointer-to-member constant %qE",
6412 arg, parm);
6413 return unify_invalid (explain_p);
6414 }
6415
6416 static int
6417 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6418 {
6419 if (explain_p)
6420 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6421 return unify_invalid (explain_p);
6422 }
6423
6424 static int
6425 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6426 {
6427 if (explain_p)
6428 inform (input_location,
6429 " inconsistent parameter pack deduction with %qT and %qT",
6430 old_arg, new_arg);
6431 return unify_invalid (explain_p);
6432 }
6433
6434 static int
6435 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6436 {
6437 if (explain_p)
6438 {
6439 if (TYPE_P (parm))
6440 inform (input_location,
6441 " deduced conflicting types for parameter %qT (%qT and %qT)",
6442 parm, first, second);
6443 else
6444 inform (input_location,
6445 " deduced conflicting values for non-type parameter "
6446 "%qE (%qE and %qE)", parm, first, second);
6447 }
6448 return unify_invalid (explain_p);
6449 }
6450
6451 static int
6452 unify_vla_arg (bool explain_p, tree arg)
6453 {
6454 if (explain_p)
6455 inform (input_location,
6456 " variable-sized array type %qT is not "
6457 "a valid template argument",
6458 arg);
6459 return unify_invalid (explain_p);
6460 }
6461
6462 static int
6463 unify_method_type_error (bool explain_p, tree arg)
6464 {
6465 if (explain_p)
6466 inform (input_location,
6467 " member function type %qT is not a valid template argument",
6468 arg);
6469 return unify_invalid (explain_p);
6470 }
6471
6472 static int
6473 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6474 {
6475 if (explain_p)
6476 {
6477 if (least_p)
6478 inform_n (input_location, wanted,
6479 " candidate expects at least %d argument, %d provided",
6480 " candidate expects at least %d arguments, %d provided",
6481 wanted, have);
6482 else
6483 inform_n (input_location, wanted,
6484 " candidate expects %d argument, %d provided",
6485 " candidate expects %d arguments, %d provided",
6486 wanted, have);
6487 }
6488 return unify_invalid (explain_p);
6489 }
6490
6491 static int
6492 unify_too_many_arguments (bool explain_p, int have, int wanted)
6493 {
6494 return unify_arity (explain_p, have, wanted);
6495 }
6496
6497 static int
6498 unify_too_few_arguments (bool explain_p, int have, int wanted,
6499 bool least_p = false)
6500 {
6501 return unify_arity (explain_p, have, wanted, least_p);
6502 }
6503
6504 static int
6505 unify_arg_conversion (bool explain_p, tree to_type,
6506 tree from_type, tree arg)
6507 {
6508 if (explain_p)
6509 inform (EXPR_LOC_OR_LOC (arg, input_location),
6510 " cannot convert %qE (type %qT) to type %qT",
6511 arg, from_type, to_type);
6512 return unify_invalid (explain_p);
6513 }
6514
6515 static int
6516 unify_no_common_base (bool explain_p, enum template_base_result r,
6517 tree parm, tree arg)
6518 {
6519 if (explain_p)
6520 switch (r)
6521 {
6522 case tbr_ambiguous_baseclass:
6523 inform (input_location, " %qT is an ambiguous base class of %qT",
6524 parm, arg);
6525 break;
6526 default:
6527 inform (input_location, " %qT is not derived from %qT", arg, parm);
6528 break;
6529 }
6530 return unify_invalid (explain_p);
6531 }
6532
6533 static int
6534 unify_inconsistent_template_template_parameters (bool explain_p)
6535 {
6536 if (explain_p)
6537 inform (input_location,
6538 " template parameters of a template template argument are "
6539 "inconsistent with other deduced template arguments");
6540 return unify_invalid (explain_p);
6541 }
6542
6543 static int
6544 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6545 {
6546 if (explain_p)
6547 inform (input_location,
6548 " can't deduce a template for %qT from non-template type %qT",
6549 parm, arg);
6550 return unify_invalid (explain_p);
6551 }
6552
6553 static int
6554 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6555 {
6556 if (explain_p)
6557 inform (input_location,
6558 " template argument %qE does not match %qE", arg, parm);
6559 return unify_invalid (explain_p);
6560 }
6561
6562 /* Attempt to convert the non-type template parameter EXPR to the
6563 indicated TYPE. If the conversion is successful, return the
6564 converted value. If the conversion is unsuccessful, return
6565 NULL_TREE if we issued an error message, or error_mark_node if we
6566 did not. We issue error messages for out-and-out bad template
6567 parameters, but not simply because the conversion failed, since we
6568 might be just trying to do argument deduction. Both TYPE and EXPR
6569 must be non-dependent.
6570
6571 The conversion follows the special rules described in
6572 [temp.arg.nontype], and it is much more strict than an implicit
6573 conversion.
6574
6575 This function is called twice for each template argument (see
6576 lookup_template_class for a more accurate description of this
6577 problem). This means that we need to handle expressions which
6578 are not valid in a C++ source, but can be created from the
6579 first call (for instance, casts to perform conversions). These
6580 hacks can go away after we fix the double coercion problem. */
6581
6582 static tree
6583 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6584 {
6585 tree expr_type;
6586 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6587 tree orig_expr = expr;
6588
6589 /* Detect immediately string literals as invalid non-type argument.
6590 This special-case is not needed for correctness (we would easily
6591 catch this later), but only to provide better diagnostic for this
6592 common user mistake. As suggested by DR 100, we do not mention
6593 linkage issues in the diagnostic as this is not the point. */
6594 /* FIXME we're making this OK. */
6595 if (TREE_CODE (expr) == STRING_CST)
6596 {
6597 if (complain & tf_error)
6598 error ("%qE is not a valid template argument for type %qT "
6599 "because string literals can never be used in this context",
6600 expr, type);
6601 return NULL_TREE;
6602 }
6603
6604 /* Add the ADDR_EXPR now for the benefit of
6605 value_dependent_expression_p. */
6606 if (TYPE_PTROBV_P (type)
6607 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6608 {
6609 expr = decay_conversion (expr, complain);
6610 if (expr == error_mark_node)
6611 return error_mark_node;
6612 }
6613
6614 /* If we are in a template, EXPR may be non-dependent, but still
6615 have a syntactic, rather than semantic, form. For example, EXPR
6616 might be a SCOPE_REF, rather than the VAR_DECL to which the
6617 SCOPE_REF refers. Preserving the qualifying scope is necessary
6618 so that access checking can be performed when the template is
6619 instantiated -- but here we need the resolved form so that we can
6620 convert the argument. */
6621 bool non_dep = false;
6622 if (TYPE_REF_OBJ_P (type)
6623 && has_value_dependent_address (expr))
6624 /* If we want the address and it's value-dependent, don't fold. */;
6625 else if (processing_template_decl
6626 && is_nondependent_constant_expression (expr))
6627 non_dep = true;
6628 if (error_operand_p (expr))
6629 return error_mark_node;
6630 expr_type = TREE_TYPE (expr);
6631
6632 /* If the argument is non-dependent, perform any conversions in
6633 non-dependent context as well. */
6634 processing_template_decl_sentinel s (non_dep);
6635 if (non_dep)
6636 expr = instantiate_non_dependent_expr_internal (expr, complain);
6637
6638 if (value_dependent_expression_p (expr))
6639 expr = canonicalize_expr_argument (expr, complain);
6640
6641 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6642 to a non-type argument of "nullptr". */
6643 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6644 expr = fold_simple (convert (type, expr));
6645
6646 /* In C++11, integral or enumeration non-type template arguments can be
6647 arbitrary constant expressions. Pointer and pointer to
6648 member arguments can be general constant expressions that evaluate
6649 to a null value, but otherwise still need to be of a specific form. */
6650 if (cxx_dialect >= cxx11)
6651 {
6652 if (TREE_CODE (expr) == PTRMEM_CST)
6653 /* A PTRMEM_CST is already constant, and a valid template
6654 argument for a parameter of pointer to member type, we just want
6655 to leave it in that form rather than lower it to a
6656 CONSTRUCTOR. */;
6657 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6658 || cxx_dialect >= cxx17)
6659 {
6660 /* C++17: A template-argument for a non-type template-parameter shall
6661 be a converted constant expression (8.20) of the type of the
6662 template-parameter. */
6663 expr = build_converted_constant_expr (type, expr, complain);
6664 if (expr == error_mark_node)
6665 return error_mark_node;
6666 expr = maybe_constant_value (expr);
6667 expr = convert_from_reference (expr);
6668 }
6669 else if (TYPE_PTR_OR_PTRMEM_P (type))
6670 {
6671 tree folded = maybe_constant_value (expr);
6672 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6673 : null_member_pointer_value_p (folded))
6674 expr = folded;
6675 }
6676 }
6677
6678 if (TREE_CODE (type) == REFERENCE_TYPE)
6679 expr = mark_lvalue_use (expr);
6680 else
6681 expr = mark_rvalue_use (expr);
6682
6683 /* HACK: Due to double coercion, we can get a
6684 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6685 which is the tree that we built on the first call (see
6686 below when coercing to reference to object or to reference to
6687 function). We just strip everything and get to the arg.
6688 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6689 for examples. */
6690 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6691 {
6692 tree probe_type, probe = expr;
6693 if (REFERENCE_REF_P (probe))
6694 probe = TREE_OPERAND (probe, 0);
6695 probe_type = TREE_TYPE (probe);
6696 if (TREE_CODE (probe) == NOP_EXPR)
6697 {
6698 /* ??? Maybe we could use convert_from_reference here, but we
6699 would need to relax its constraints because the NOP_EXPR
6700 could actually change the type to something more cv-qualified,
6701 and this is not folded by convert_from_reference. */
6702 tree addr = TREE_OPERAND (probe, 0);
6703 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6704 && TREE_CODE (addr) == ADDR_EXPR
6705 && TYPE_PTR_P (TREE_TYPE (addr))
6706 && (same_type_ignoring_top_level_qualifiers_p
6707 (TREE_TYPE (probe_type),
6708 TREE_TYPE (TREE_TYPE (addr)))))
6709 {
6710 expr = TREE_OPERAND (addr, 0);
6711 expr_type = TREE_TYPE (probe_type);
6712 }
6713 }
6714 }
6715
6716 /* [temp.arg.nontype]/5, bullet 1
6717
6718 For a non-type template-parameter of integral or enumeration type,
6719 integral promotions (_conv.prom_) and integral conversions
6720 (_conv.integral_) are applied. */
6721 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6722 {
6723 if (cxx_dialect < cxx11)
6724 {
6725 tree t = build_converted_constant_expr (type, expr, complain);
6726 t = maybe_constant_value (t);
6727 if (t != error_mark_node)
6728 expr = t;
6729 }
6730
6731 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6732 return error_mark_node;
6733
6734 /* Notice that there are constant expressions like '4 % 0' which
6735 do not fold into integer constants. */
6736 if (TREE_CODE (expr) != INTEGER_CST
6737 && !value_dependent_expression_p (expr))
6738 {
6739 if (complain & tf_error)
6740 {
6741 int errs = errorcount, warns = warningcount + werrorcount;
6742 if (!require_potential_constant_expression (expr))
6743 expr = error_mark_node;
6744 else
6745 expr = cxx_constant_value (expr);
6746 if (errorcount > errs || warningcount + werrorcount > warns)
6747 inform (loc, "in template argument for type %qT ", type);
6748 if (expr == error_mark_node)
6749 return NULL_TREE;
6750 /* else cxx_constant_value complained but gave us
6751 a real constant, so go ahead. */
6752 if (TREE_CODE (expr) != INTEGER_CST)
6753 {
6754 /* Some assemble time constant expressions like
6755 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6756 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6757 as we can emit them into .rodata initializers of
6758 variables, yet they can't fold into an INTEGER_CST at
6759 compile time. Refuse them here. */
6760 gcc_checking_assert (reduced_constant_expression_p (expr));
6761 error_at (loc, "template argument %qE for type %qT not "
6762 "a constant integer", expr, type);
6763 return NULL_TREE;
6764 }
6765 }
6766 else
6767 return NULL_TREE;
6768 }
6769
6770 /* Avoid typedef problems. */
6771 if (TREE_TYPE (expr) != type)
6772 expr = fold_convert (type, expr);
6773 }
6774 /* [temp.arg.nontype]/5, bullet 2
6775
6776 For a non-type template-parameter of type pointer to object,
6777 qualification conversions (_conv.qual_) and the array-to-pointer
6778 conversion (_conv.array_) are applied. */
6779 else if (TYPE_PTROBV_P (type))
6780 {
6781 tree decayed = expr;
6782
6783 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6784 decay_conversion or an explicit cast. If it's a problematic cast,
6785 we'll complain about it below. */
6786 if (TREE_CODE (expr) == NOP_EXPR)
6787 {
6788 tree probe = expr;
6789 STRIP_NOPS (probe);
6790 if (TREE_CODE (probe) == ADDR_EXPR
6791 && TYPE_PTR_P (TREE_TYPE (probe)))
6792 {
6793 expr = probe;
6794 expr_type = TREE_TYPE (expr);
6795 }
6796 }
6797
6798 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6799
6800 A template-argument for a non-type, non-template template-parameter
6801 shall be one of: [...]
6802
6803 -- the name of a non-type template-parameter;
6804 -- the address of an object or function with external linkage, [...]
6805 expressed as "& id-expression" where the & is optional if the name
6806 refers to a function or array, or if the corresponding
6807 template-parameter is a reference.
6808
6809 Here, we do not care about functions, as they are invalid anyway
6810 for a parameter of type pointer-to-object. */
6811
6812 if (value_dependent_expression_p (expr))
6813 /* Non-type template parameters are OK. */
6814 ;
6815 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6816 /* Null pointer values are OK in C++11. */;
6817 else if (TREE_CODE (expr) != ADDR_EXPR)
6818 {
6819 if (VAR_P (expr))
6820 {
6821 if (complain & tf_error)
6822 error ("%qD is not a valid template argument "
6823 "because %qD is a variable, not the address of "
6824 "a variable", orig_expr, expr);
6825 return NULL_TREE;
6826 }
6827 if (POINTER_TYPE_P (expr_type))
6828 {
6829 if (complain & tf_error)
6830 error ("%qE is not a valid template argument for %qT "
6831 "because it is not the address of a variable",
6832 orig_expr, type);
6833 return NULL_TREE;
6834 }
6835 /* Other values, like integer constants, might be valid
6836 non-type arguments of some other type. */
6837 return error_mark_node;
6838 }
6839 else
6840 {
6841 tree decl = TREE_OPERAND (expr, 0);
6842
6843 if (!VAR_P (decl))
6844 {
6845 if (complain & tf_error)
6846 error ("%qE is not a valid template argument of type %qT "
6847 "because %qE is not a variable", orig_expr, type, decl);
6848 return NULL_TREE;
6849 }
6850 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6851 {
6852 if (complain & tf_error)
6853 error ("%qE is not a valid template argument of type %qT "
6854 "because %qD does not have external linkage",
6855 orig_expr, type, decl);
6856 return NULL_TREE;
6857 }
6858 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6859 && decl_linkage (decl) == lk_none)
6860 {
6861 if (complain & tf_error)
6862 error ("%qE is not a valid template argument of type %qT "
6863 "because %qD has no linkage", orig_expr, type, decl);
6864 return NULL_TREE;
6865 }
6866 /* C++17: For a non-type template-parameter of reference or pointer
6867 type, the value of the constant expression shall not refer to (or
6868 for a pointer type, shall not be the address of):
6869 * a subobject (4.5),
6870 * a temporary object (15.2),
6871 * a string literal (5.13.5),
6872 * the result of a typeid expression (8.2.8), or
6873 * a predefined __func__ variable (11.4.1). */
6874 else if (DECL_ARTIFICIAL (decl))
6875 {
6876 if (complain & tf_error)
6877 error ("the address of %qD is not a valid template argument",
6878 decl);
6879 return NULL_TREE;
6880 }
6881 else if (!same_type_ignoring_top_level_qualifiers_p
6882 (strip_array_types (TREE_TYPE (type)),
6883 strip_array_types (TREE_TYPE (decl))))
6884 {
6885 if (complain & tf_error)
6886 error ("the address of the %qT subobject of %qD is not a "
6887 "valid template argument", TREE_TYPE (type), decl);
6888 return NULL_TREE;
6889 }
6890 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6891 {
6892 if (complain & tf_error)
6893 error ("the address of %qD is not a valid template argument "
6894 "because it does not have static storage duration",
6895 decl);
6896 return NULL_TREE;
6897 }
6898 }
6899
6900 expr = decayed;
6901
6902 expr = perform_qualification_conversions (type, expr);
6903 if (expr == error_mark_node)
6904 return error_mark_node;
6905 }
6906 /* [temp.arg.nontype]/5, bullet 3
6907
6908 For a non-type template-parameter of type reference to object, no
6909 conversions apply. The type referred to by the reference may be more
6910 cv-qualified than the (otherwise identical) type of the
6911 template-argument. The template-parameter is bound directly to the
6912 template-argument, which must be an lvalue. */
6913 else if (TYPE_REF_OBJ_P (type))
6914 {
6915 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6916 expr_type))
6917 return error_mark_node;
6918
6919 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6920 {
6921 if (complain & tf_error)
6922 error ("%qE is not a valid template argument for type %qT "
6923 "because of conflicts in cv-qualification", expr, type);
6924 return NULL_TREE;
6925 }
6926
6927 if (!lvalue_p (expr))
6928 {
6929 if (complain & tf_error)
6930 error ("%qE is not a valid template argument for type %qT "
6931 "because it is not an lvalue", expr, type);
6932 return NULL_TREE;
6933 }
6934
6935 /* [temp.arg.nontype]/1
6936
6937 A template-argument for a non-type, non-template template-parameter
6938 shall be one of: [...]
6939
6940 -- the address of an object or function with external linkage. */
6941 if (INDIRECT_REF_P (expr)
6942 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6943 {
6944 expr = TREE_OPERAND (expr, 0);
6945 if (DECL_P (expr))
6946 {
6947 if (complain & tf_error)
6948 error ("%q#D is not a valid template argument for type %qT "
6949 "because a reference variable does not have a constant "
6950 "address", expr, type);
6951 return NULL_TREE;
6952 }
6953 }
6954
6955 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6956 && value_dependent_expression_p (expr))
6957 /* OK, dependent reference. We don't want to ask whether a DECL is
6958 itself value-dependent, since what we want here is its address. */;
6959 else
6960 {
6961 if (!DECL_P (expr))
6962 {
6963 if (complain & tf_error)
6964 error ("%qE is not a valid template argument for type %qT "
6965 "because it is not an object with linkage",
6966 expr, type);
6967 return NULL_TREE;
6968 }
6969
6970 /* DR 1155 allows internal linkage in C++11 and up. */
6971 linkage_kind linkage = decl_linkage (expr);
6972 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6973 {
6974 if (complain & tf_error)
6975 error ("%qE is not a valid template argument for type %qT "
6976 "because object %qD does not have linkage",
6977 expr, type, expr);
6978 return NULL_TREE;
6979 }
6980
6981 expr = build_address (expr);
6982 }
6983
6984 if (!same_type_p (type, TREE_TYPE (expr)))
6985 expr = build_nop (type, expr);
6986 }
6987 /* [temp.arg.nontype]/5, bullet 4
6988
6989 For a non-type template-parameter of type pointer to function, only
6990 the function-to-pointer conversion (_conv.func_) is applied. If the
6991 template-argument represents a set of overloaded functions (or a
6992 pointer to such), the matching function is selected from the set
6993 (_over.over_). */
6994 else if (TYPE_PTRFN_P (type))
6995 {
6996 /* If the argument is a template-id, we might not have enough
6997 context information to decay the pointer. */
6998 if (!type_unknown_p (expr_type))
6999 {
7000 expr = decay_conversion (expr, complain);
7001 if (expr == error_mark_node)
7002 return error_mark_node;
7003 }
7004
7005 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7006 /* Null pointer values are OK in C++11. */
7007 return perform_qualification_conversions (type, expr);
7008
7009 expr = convert_nontype_argument_function (type, expr, complain);
7010 if (!expr || expr == error_mark_node)
7011 return expr;
7012 }
7013 /* [temp.arg.nontype]/5, bullet 5
7014
7015 For a non-type template-parameter of type reference to function, no
7016 conversions apply. If the template-argument represents a set of
7017 overloaded functions, the matching function is selected from the set
7018 (_over.over_). */
7019 else if (TYPE_REFFN_P (type))
7020 {
7021 if (TREE_CODE (expr) == ADDR_EXPR)
7022 {
7023 if (complain & tf_error)
7024 {
7025 error ("%qE is not a valid template argument for type %qT "
7026 "because it is a pointer", expr, type);
7027 inform (input_location, "try using %qE instead",
7028 TREE_OPERAND (expr, 0));
7029 }
7030 return NULL_TREE;
7031 }
7032
7033 expr = convert_nontype_argument_function (type, expr, complain);
7034 if (!expr || expr == error_mark_node)
7035 return expr;
7036 }
7037 /* [temp.arg.nontype]/5, bullet 6
7038
7039 For a non-type template-parameter of type pointer to member function,
7040 no conversions apply. If the template-argument represents a set of
7041 overloaded member functions, the matching member function is selected
7042 from the set (_over.over_). */
7043 else if (TYPE_PTRMEMFUNC_P (type))
7044 {
7045 expr = instantiate_type (type, expr, tf_none);
7046 if (expr == error_mark_node)
7047 return error_mark_node;
7048
7049 /* [temp.arg.nontype] bullet 1 says the pointer to member
7050 expression must be a pointer-to-member constant. */
7051 if (!value_dependent_expression_p (expr)
7052 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7053 return NULL_TREE;
7054
7055 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7056 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7057 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7058 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7059 }
7060 /* [temp.arg.nontype]/5, bullet 7
7061
7062 For a non-type template-parameter of type pointer to data member,
7063 qualification conversions (_conv.qual_) are applied. */
7064 else if (TYPE_PTRDATAMEM_P (type))
7065 {
7066 /* [temp.arg.nontype] bullet 1 says the pointer to member
7067 expression must be a pointer-to-member constant. */
7068 if (!value_dependent_expression_p (expr)
7069 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7070 return NULL_TREE;
7071
7072 expr = perform_qualification_conversions (type, expr);
7073 if (expr == error_mark_node)
7074 return expr;
7075 }
7076 else if (NULLPTR_TYPE_P (type))
7077 {
7078 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7079 {
7080 if (complain & tf_error)
7081 error ("%qE is not a valid template argument for type %qT "
7082 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7083 return NULL_TREE;
7084 }
7085 return expr;
7086 }
7087 /* A template non-type parameter must be one of the above. */
7088 else
7089 gcc_unreachable ();
7090
7091 /* Sanity check: did we actually convert the argument to the
7092 right type? */
7093 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7094 (type, TREE_TYPE (expr)));
7095 return convert_from_reference (expr);
7096 }
7097
7098 /* Subroutine of coerce_template_template_parms, which returns 1 if
7099 PARM_PARM and ARG_PARM match using the rule for the template
7100 parameters of template template parameters. Both PARM and ARG are
7101 template parameters; the rest of the arguments are the same as for
7102 coerce_template_template_parms.
7103 */
7104 static int
7105 coerce_template_template_parm (tree parm,
7106 tree arg,
7107 tsubst_flags_t complain,
7108 tree in_decl,
7109 tree outer_args)
7110 {
7111 if (arg == NULL_TREE || error_operand_p (arg)
7112 || parm == NULL_TREE || error_operand_p (parm))
7113 return 0;
7114
7115 if (TREE_CODE (arg) != TREE_CODE (parm))
7116 return 0;
7117
7118 switch (TREE_CODE (parm))
7119 {
7120 case TEMPLATE_DECL:
7121 /* We encounter instantiations of templates like
7122 template <template <template <class> class> class TT>
7123 class C; */
7124 {
7125 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7126 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7127
7128 if (!coerce_template_template_parms
7129 (parmparm, argparm, complain, in_decl, outer_args))
7130 return 0;
7131 }
7132 /* Fall through. */
7133
7134 case TYPE_DECL:
7135 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7136 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7137 /* Argument is a parameter pack but parameter is not. */
7138 return 0;
7139 break;
7140
7141 case PARM_DECL:
7142 /* The tsubst call is used to handle cases such as
7143
7144 template <int> class C {};
7145 template <class T, template <T> class TT> class D {};
7146 D<int, C> d;
7147
7148 i.e. the parameter list of TT depends on earlier parameters. */
7149 if (!uses_template_parms (TREE_TYPE (arg)))
7150 {
7151 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7152 if (!uses_template_parms (t)
7153 && !same_type_p (t, TREE_TYPE (arg)))
7154 return 0;
7155 }
7156
7157 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7158 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7159 /* Argument is a parameter pack but parameter is not. */
7160 return 0;
7161
7162 break;
7163
7164 default:
7165 gcc_unreachable ();
7166 }
7167
7168 return 1;
7169 }
7170
7171 /* Coerce template argument list ARGLIST for use with template
7172 template-parameter TEMPL. */
7173
7174 static tree
7175 coerce_template_args_for_ttp (tree templ, tree arglist,
7176 tsubst_flags_t complain)
7177 {
7178 /* Consider an example where a template template parameter declared as
7179
7180 template <class T, class U = std::allocator<T> > class TT
7181
7182 The template parameter level of T and U are one level larger than
7183 of TT. To proper process the default argument of U, say when an
7184 instantiation `TT<int>' is seen, we need to build the full
7185 arguments containing {int} as the innermost level. Outer levels,
7186 available when not appearing as default template argument, can be
7187 obtained from the arguments of the enclosing template.
7188
7189 Suppose that TT is later substituted with std::vector. The above
7190 instantiation is `TT<int, std::allocator<T> >' with TT at
7191 level 1, and T at level 2, while the template arguments at level 1
7192 becomes {std::vector} and the inner level 2 is {int}. */
7193
7194 tree outer = DECL_CONTEXT (templ);
7195 if (outer)
7196 {
7197 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7198 /* We want arguments for the partial specialization, not arguments for
7199 the primary template. */
7200 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7201 else
7202 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7203 }
7204 else if (current_template_parms)
7205 {
7206 /* This is an argument of the current template, so we haven't set
7207 DECL_CONTEXT yet. */
7208 tree relevant_template_parms;
7209
7210 /* Parameter levels that are greater than the level of the given
7211 template template parm are irrelevant. */
7212 relevant_template_parms = current_template_parms;
7213 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7214 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7215 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7216
7217 outer = template_parms_to_args (relevant_template_parms);
7218 }
7219
7220 if (outer)
7221 arglist = add_to_template_args (outer, arglist);
7222
7223 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7224 return coerce_template_parms (parmlist, arglist, templ,
7225 complain,
7226 /*require_all_args=*/true,
7227 /*use_default_args=*/true);
7228 }
7229
7230 /* A cache of template template parameters with match-all default
7231 arguments. */
7232 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7233 static void
7234 store_defaulted_ttp (tree v, tree t)
7235 {
7236 if (!defaulted_ttp_cache)
7237 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7238 defaulted_ttp_cache->put (v, t);
7239 }
7240 static tree
7241 lookup_defaulted_ttp (tree v)
7242 {
7243 if (defaulted_ttp_cache)
7244 if (tree *p = defaulted_ttp_cache->get (v))
7245 return *p;
7246 return NULL_TREE;
7247 }
7248
7249 /* T is a bound template template-parameter. Copy its arguments into default
7250 arguments of the template template-parameter's template parameters. */
7251
7252 static tree
7253 add_defaults_to_ttp (tree otmpl)
7254 {
7255 if (tree c = lookup_defaulted_ttp (otmpl))
7256 return c;
7257
7258 tree ntmpl = copy_node (otmpl);
7259
7260 tree ntype = copy_node (TREE_TYPE (otmpl));
7261 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7262 TYPE_MAIN_VARIANT (ntype) = ntype;
7263 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7264 TYPE_NAME (ntype) = ntmpl;
7265 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7266
7267 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7268 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7269 TEMPLATE_PARM_DECL (idx) = ntmpl;
7270 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7271
7272 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7273 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7274 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7275 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7276 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7277 {
7278 tree o = TREE_VEC_ELT (vec, i);
7279 if (!template_parameter_pack_p (TREE_VALUE (o)))
7280 {
7281 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7282 TREE_PURPOSE (n) = any_targ_node;
7283 }
7284 }
7285
7286 store_defaulted_ttp (otmpl, ntmpl);
7287 return ntmpl;
7288 }
7289
7290 /* ARG is a bound potential template template-argument, and PARGS is a list
7291 of arguments for the corresponding template template-parameter. Adjust
7292 PARGS as appropriate for application to ARG's template, and if ARG is a
7293 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7294 arguments to the template template parameter. */
7295
7296 static tree
7297 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7298 {
7299 ++processing_template_decl;
7300 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7301 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7302 {
7303 /* When comparing two template template-parameters in partial ordering,
7304 rewrite the one currently being used as an argument to have default
7305 arguments for all parameters. */
7306 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7307 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7308 if (pargs != error_mark_node)
7309 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7310 TYPE_TI_ARGS (arg));
7311 }
7312 else
7313 {
7314 tree aparms
7315 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7316 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7317 /*require_all*/true,
7318 /*use_default*/true);
7319 }
7320 --processing_template_decl;
7321 return pargs;
7322 }
7323
7324 /* Subroutine of unify for the case when PARM is a
7325 BOUND_TEMPLATE_TEMPLATE_PARM. */
7326
7327 static int
7328 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7329 bool explain_p)
7330 {
7331 tree parmvec = TYPE_TI_ARGS (parm);
7332 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7333
7334 /* The template template parm might be variadic and the argument
7335 not, so flatten both argument lists. */
7336 parmvec = expand_template_argument_pack (parmvec);
7337 argvec = expand_template_argument_pack (argvec);
7338
7339 if (flag_new_ttp)
7340 {
7341 /* In keeping with P0522R0, adjust P's template arguments
7342 to apply to A's template; then flatten it again. */
7343 tree nparmvec = parmvec;
7344 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7345 nparmvec = expand_template_argument_pack (nparmvec);
7346
7347 if (unify (tparms, targs, nparmvec, argvec,
7348 UNIFY_ALLOW_NONE, explain_p))
7349 return 1;
7350
7351 /* If the P0522 adjustment eliminated a pack expansion, deduce
7352 empty packs. */
7353 if (flag_new_ttp
7354 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7355 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7356 DEDUCE_EXACT, /*sub*/true, explain_p))
7357 return 1;
7358 }
7359 else
7360 {
7361 /* Deduce arguments T, i from TT<T> or TT<i>.
7362 We check each element of PARMVEC and ARGVEC individually
7363 rather than the whole TREE_VEC since they can have
7364 different number of elements, which is allowed under N2555. */
7365
7366 int len = TREE_VEC_LENGTH (parmvec);
7367
7368 /* Check if the parameters end in a pack, making them
7369 variadic. */
7370 int parm_variadic_p = 0;
7371 if (len > 0
7372 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7373 parm_variadic_p = 1;
7374
7375 for (int i = 0; i < len - parm_variadic_p; ++i)
7376 /* If the template argument list of P contains a pack
7377 expansion that is not the last template argument, the
7378 entire template argument list is a non-deduced
7379 context. */
7380 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7381 return unify_success (explain_p);
7382
7383 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7384 return unify_too_few_arguments (explain_p,
7385 TREE_VEC_LENGTH (argvec), len);
7386
7387 for (int i = 0; i < len - parm_variadic_p; ++i)
7388 if (unify (tparms, targs,
7389 TREE_VEC_ELT (parmvec, i),
7390 TREE_VEC_ELT (argvec, i),
7391 UNIFY_ALLOW_NONE, explain_p))
7392 return 1;
7393
7394 if (parm_variadic_p
7395 && unify_pack_expansion (tparms, targs,
7396 parmvec, argvec,
7397 DEDUCE_EXACT,
7398 /*subr=*/true, explain_p))
7399 return 1;
7400 }
7401
7402 return 0;
7403 }
7404
7405 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7406 template template parameters. Both PARM_PARMS and ARG_PARMS are
7407 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7408 or PARM_DECL.
7409
7410 Consider the example:
7411 template <class T> class A;
7412 template<template <class U> class TT> class B;
7413
7414 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7415 the parameters to A, and OUTER_ARGS contains A. */
7416
7417 static int
7418 coerce_template_template_parms (tree parm_parms,
7419 tree arg_parms,
7420 tsubst_flags_t complain,
7421 tree in_decl,
7422 tree outer_args)
7423 {
7424 int nparms, nargs, i;
7425 tree parm, arg;
7426 int variadic_p = 0;
7427
7428 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7429 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7430
7431 nparms = TREE_VEC_LENGTH (parm_parms);
7432 nargs = TREE_VEC_LENGTH (arg_parms);
7433
7434 if (flag_new_ttp)
7435 {
7436 /* P0522R0: A template template-parameter P is at least as specialized as
7437 a template template-argument A if, given the following rewrite to two
7438 function templates, the function template corresponding to P is at
7439 least as specialized as the function template corresponding to A
7440 according to the partial ordering rules for function templates
7441 ([temp.func.order]). Given an invented class template X with the
7442 template parameter list of A (including default arguments):
7443
7444 * Each of the two function templates has the same template parameters,
7445 respectively, as P or A.
7446
7447 * Each function template has a single function parameter whose type is
7448 a specialization of X with template arguments corresponding to the
7449 template parameters from the respective function template where, for
7450 each template parameter PP in the template parameter list of the
7451 function template, a corresponding template argument AA is formed. If
7452 PP declares a parameter pack, then AA is the pack expansion
7453 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7454
7455 If the rewrite produces an invalid type, then P is not at least as
7456 specialized as A. */
7457
7458 /* So coerce P's args to apply to A's parms, and then deduce between A's
7459 args and the converted args. If that succeeds, A is at least as
7460 specialized as P, so they match.*/
7461 tree pargs = template_parms_level_to_args (parm_parms);
7462 ++processing_template_decl;
7463 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7464 /*require_all*/true, /*use_default*/true);
7465 --processing_template_decl;
7466 if (pargs != error_mark_node)
7467 {
7468 tree targs = make_tree_vec (nargs);
7469 tree aargs = template_parms_level_to_args (arg_parms);
7470 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7471 /*explain*/false))
7472 return 1;
7473 }
7474 }
7475
7476 /* Determine whether we have a parameter pack at the end of the
7477 template template parameter's template parameter list. */
7478 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7479 {
7480 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7481
7482 if (error_operand_p (parm))
7483 return 0;
7484
7485 switch (TREE_CODE (parm))
7486 {
7487 case TEMPLATE_DECL:
7488 case TYPE_DECL:
7489 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7490 variadic_p = 1;
7491 break;
7492
7493 case PARM_DECL:
7494 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7495 variadic_p = 1;
7496 break;
7497
7498 default:
7499 gcc_unreachable ();
7500 }
7501 }
7502
7503 if (nargs != nparms
7504 && !(variadic_p && nargs >= nparms - 1))
7505 return 0;
7506
7507 /* Check all of the template parameters except the parameter pack at
7508 the end (if any). */
7509 for (i = 0; i < nparms - variadic_p; ++i)
7510 {
7511 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7512 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7513 continue;
7514
7515 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7516 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7517
7518 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7519 outer_args))
7520 return 0;
7521
7522 }
7523
7524 if (variadic_p)
7525 {
7526 /* Check each of the template parameters in the template
7527 argument against the template parameter pack at the end of
7528 the template template parameter. */
7529 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7530 return 0;
7531
7532 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7533
7534 for (; i < nargs; ++i)
7535 {
7536 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7537 continue;
7538
7539 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7540
7541 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7542 outer_args))
7543 return 0;
7544 }
7545 }
7546
7547 return 1;
7548 }
7549
7550 /* Verifies that the deduced template arguments (in TARGS) for the
7551 template template parameters (in TPARMS) represent valid bindings,
7552 by comparing the template parameter list of each template argument
7553 to the template parameter list of its corresponding template
7554 template parameter, in accordance with DR150. This
7555 routine can only be called after all template arguments have been
7556 deduced. It will return TRUE if all of the template template
7557 parameter bindings are okay, FALSE otherwise. */
7558 bool
7559 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7560 {
7561 int i, ntparms = TREE_VEC_LENGTH (tparms);
7562 bool ret = true;
7563
7564 /* We're dealing with template parms in this process. */
7565 ++processing_template_decl;
7566
7567 targs = INNERMOST_TEMPLATE_ARGS (targs);
7568
7569 for (i = 0; i < ntparms; ++i)
7570 {
7571 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7572 tree targ = TREE_VEC_ELT (targs, i);
7573
7574 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7575 {
7576 tree packed_args = NULL_TREE;
7577 int idx, len = 1;
7578
7579 if (ARGUMENT_PACK_P (targ))
7580 {
7581 /* Look inside the argument pack. */
7582 packed_args = ARGUMENT_PACK_ARGS (targ);
7583 len = TREE_VEC_LENGTH (packed_args);
7584 }
7585
7586 for (idx = 0; idx < len; ++idx)
7587 {
7588 tree targ_parms = NULL_TREE;
7589
7590 if (packed_args)
7591 /* Extract the next argument from the argument
7592 pack. */
7593 targ = TREE_VEC_ELT (packed_args, idx);
7594
7595 if (PACK_EXPANSION_P (targ))
7596 /* Look at the pattern of the pack expansion. */
7597 targ = PACK_EXPANSION_PATTERN (targ);
7598
7599 /* Extract the template parameters from the template
7600 argument. */
7601 if (TREE_CODE (targ) == TEMPLATE_DECL)
7602 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7603 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7604 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7605
7606 /* Verify that we can coerce the template template
7607 parameters from the template argument to the template
7608 parameter. This requires an exact match. */
7609 if (targ_parms
7610 && !coerce_template_template_parms
7611 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7612 targ_parms,
7613 tf_none,
7614 tparm,
7615 targs))
7616 {
7617 ret = false;
7618 goto out;
7619 }
7620 }
7621 }
7622 }
7623
7624 out:
7625
7626 --processing_template_decl;
7627 return ret;
7628 }
7629
7630 /* Since type attributes aren't mangled, we need to strip them from
7631 template type arguments. */
7632
7633 static tree
7634 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7635 {
7636 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7637 return arg;
7638 bool removed_attributes = false;
7639 tree canon = strip_typedefs (arg, &removed_attributes);
7640 if (removed_attributes
7641 && (complain & tf_warning))
7642 warning (OPT_Wignored_attributes,
7643 "ignoring attributes on template argument %qT", arg);
7644 return canon;
7645 }
7646
7647 /* And from inside dependent non-type arguments like sizeof(Type). */
7648
7649 static tree
7650 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7651 {
7652 if (!arg || arg == error_mark_node)
7653 return arg;
7654 bool removed_attributes = false;
7655 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7656 if (removed_attributes
7657 && (complain & tf_warning))
7658 warning (OPT_Wignored_attributes,
7659 "ignoring attributes in template argument %qE", arg);
7660 return canon;
7661 }
7662
7663 // A template declaration can be substituted for a constrained
7664 // template template parameter only when the argument is more
7665 // constrained than the parameter.
7666 static bool
7667 is_compatible_template_arg (tree parm, tree arg)
7668 {
7669 tree parm_cons = get_constraints (parm);
7670
7671 /* For now, allow constrained template template arguments
7672 and unconstrained template template parameters. */
7673 if (parm_cons == NULL_TREE)
7674 return true;
7675
7676 tree arg_cons = get_constraints (arg);
7677
7678 // If the template parameter is constrained, we need to rewrite its
7679 // constraints in terms of the ARG's template parameters. This ensures
7680 // that all of the template parameter types will have the same depth.
7681 //
7682 // Note that this is only valid when coerce_template_template_parm is
7683 // true for the innermost template parameters of PARM and ARG. In other
7684 // words, because coercion is successful, this conversion will be valid.
7685 if (parm_cons)
7686 {
7687 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7688 parm_cons = tsubst_constraint_info (parm_cons,
7689 INNERMOST_TEMPLATE_ARGS (args),
7690 tf_none, NULL_TREE);
7691 if (parm_cons == error_mark_node)
7692 return false;
7693 }
7694
7695 return subsumes (parm_cons, arg_cons);
7696 }
7697
7698 // Convert a placeholder argument into a binding to the original
7699 // parameter. The original parameter is saved as the TREE_TYPE of
7700 // ARG.
7701 static inline tree
7702 convert_wildcard_argument (tree parm, tree arg)
7703 {
7704 TREE_TYPE (arg) = parm;
7705 return arg;
7706 }
7707
7708 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7709 because one of them is dependent. But we need to represent the
7710 conversion for the benefit of cp_tree_equal. */
7711
7712 static tree
7713 maybe_convert_nontype_argument (tree type, tree arg)
7714 {
7715 /* Auto parms get no conversion. */
7716 if (type_uses_auto (type))
7717 return arg;
7718 /* We don't need or want to add this conversion now if we're going to use the
7719 argument for deduction. */
7720 if (value_dependent_expression_p (arg))
7721 return arg;
7722
7723 type = cv_unqualified (type);
7724 tree argtype = TREE_TYPE (arg);
7725 if (same_type_p (type, argtype))
7726 return arg;
7727
7728 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7729 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7730 return arg;
7731 }
7732
7733 /* Convert the indicated template ARG as necessary to match the
7734 indicated template PARM. Returns the converted ARG, or
7735 error_mark_node if the conversion was unsuccessful. Error and
7736 warning messages are issued under control of COMPLAIN. This
7737 conversion is for the Ith parameter in the parameter list. ARGS is
7738 the full set of template arguments deduced so far. */
7739
7740 static tree
7741 convert_template_argument (tree parm,
7742 tree arg,
7743 tree args,
7744 tsubst_flags_t complain,
7745 int i,
7746 tree in_decl)
7747 {
7748 tree orig_arg;
7749 tree val;
7750 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7751
7752 if (parm == error_mark_node)
7753 return error_mark_node;
7754
7755 /* Trivially convert placeholders. */
7756 if (TREE_CODE (arg) == WILDCARD_DECL)
7757 return convert_wildcard_argument (parm, arg);
7758
7759 if (arg == any_targ_node)
7760 return arg;
7761
7762 if (TREE_CODE (arg) == TREE_LIST
7763 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7764 {
7765 /* The template argument was the name of some
7766 member function. That's usually
7767 invalid, but static members are OK. In any
7768 case, grab the underlying fields/functions
7769 and issue an error later if required. */
7770 orig_arg = TREE_VALUE (arg);
7771 TREE_TYPE (arg) = unknown_type_node;
7772 }
7773
7774 orig_arg = arg;
7775
7776 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7777 requires_type = (TREE_CODE (parm) == TYPE_DECL
7778 || requires_tmpl_type);
7779
7780 /* When determining whether an argument pack expansion is a template,
7781 look at the pattern. */
7782 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7783 arg = PACK_EXPANSION_PATTERN (arg);
7784
7785 /* Deal with an injected-class-name used as a template template arg. */
7786 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7787 {
7788 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7789 if (TREE_CODE (t) == TEMPLATE_DECL)
7790 {
7791 if (cxx_dialect >= cxx11)
7792 /* OK under DR 1004. */;
7793 else if (complain & tf_warning_or_error)
7794 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7795 " used as template template argument", TYPE_NAME (arg));
7796 else if (flag_pedantic_errors)
7797 t = arg;
7798
7799 arg = t;
7800 }
7801 }
7802
7803 is_tmpl_type =
7804 ((TREE_CODE (arg) == TEMPLATE_DECL
7805 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7806 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7807 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7808 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7809
7810 if (is_tmpl_type
7811 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7812 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7813 arg = TYPE_STUB_DECL (arg);
7814
7815 is_type = TYPE_P (arg) || is_tmpl_type;
7816
7817 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7818 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7819 {
7820 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7821 {
7822 if (complain & tf_error)
7823 error ("invalid use of destructor %qE as a type", orig_arg);
7824 return error_mark_node;
7825 }
7826
7827 permerror (input_location,
7828 "to refer to a type member of a template parameter, "
7829 "use %<typename %E%>", orig_arg);
7830
7831 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7832 TREE_OPERAND (arg, 1),
7833 typename_type,
7834 complain);
7835 arg = orig_arg;
7836 is_type = 1;
7837 }
7838 if (is_type != requires_type)
7839 {
7840 if (in_decl)
7841 {
7842 if (complain & tf_error)
7843 {
7844 error ("type/value mismatch at argument %d in template "
7845 "parameter list for %qD",
7846 i + 1, in_decl);
7847 if (is_type)
7848 inform (input_location,
7849 " expected a constant of type %qT, got %qT",
7850 TREE_TYPE (parm),
7851 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7852 else if (requires_tmpl_type)
7853 inform (input_location,
7854 " expected a class template, got %qE", orig_arg);
7855 else
7856 inform (input_location,
7857 " expected a type, got %qE", orig_arg);
7858 }
7859 }
7860 return error_mark_node;
7861 }
7862 if (is_tmpl_type ^ requires_tmpl_type)
7863 {
7864 if (in_decl && (complain & tf_error))
7865 {
7866 error ("type/value mismatch at argument %d in template "
7867 "parameter list for %qD",
7868 i + 1, in_decl);
7869 if (is_tmpl_type)
7870 inform (input_location,
7871 " expected a type, got %qT", DECL_NAME (arg));
7872 else
7873 inform (input_location,
7874 " expected a class template, got %qT", orig_arg);
7875 }
7876 return error_mark_node;
7877 }
7878
7879 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7880 /* We already did the appropriate conversion when packing args. */
7881 val = orig_arg;
7882 else if (is_type)
7883 {
7884 if (requires_tmpl_type)
7885 {
7886 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7887 /* The number of argument required is not known yet.
7888 Just accept it for now. */
7889 val = orig_arg;
7890 else
7891 {
7892 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7893 tree argparm;
7894
7895 /* Strip alias templates that are equivalent to another
7896 template. */
7897 arg = get_underlying_template (arg);
7898 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7899
7900 if (coerce_template_template_parms (parmparm, argparm,
7901 complain, in_decl,
7902 args))
7903 {
7904 val = arg;
7905
7906 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7907 TEMPLATE_DECL. */
7908 if (val != error_mark_node)
7909 {
7910 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7911 val = TREE_TYPE (val);
7912 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7913 val = make_pack_expansion (val, complain);
7914 }
7915 }
7916 else
7917 {
7918 if (in_decl && (complain & tf_error))
7919 {
7920 error ("type/value mismatch at argument %d in "
7921 "template parameter list for %qD",
7922 i + 1, in_decl);
7923 inform (input_location,
7924 " expected a template of type %qD, got %qT",
7925 parm, orig_arg);
7926 }
7927
7928 val = error_mark_node;
7929 }
7930
7931 // Check that the constraints are compatible before allowing the
7932 // substitution.
7933 if (val != error_mark_node)
7934 if (!is_compatible_template_arg (parm, arg))
7935 {
7936 if (in_decl && (complain & tf_error))
7937 {
7938 error ("constraint mismatch at argument %d in "
7939 "template parameter list for %qD",
7940 i + 1, in_decl);
7941 inform (input_location, " expected %qD but got %qD",
7942 parm, arg);
7943 }
7944 val = error_mark_node;
7945 }
7946 }
7947 }
7948 else
7949 val = orig_arg;
7950 /* We only form one instance of each template specialization.
7951 Therefore, if we use a non-canonical variant (i.e., a
7952 typedef), any future messages referring to the type will use
7953 the typedef, which is confusing if those future uses do not
7954 themselves also use the typedef. */
7955 if (TYPE_P (val))
7956 val = canonicalize_type_argument (val, complain);
7957 }
7958 else
7959 {
7960 tree t = TREE_TYPE (parm);
7961
7962 if (tree a = type_uses_auto (t))
7963 {
7964 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7965 if (t == error_mark_node)
7966 return error_mark_node;
7967 }
7968 else
7969 t = tsubst (t, args, complain, in_decl);
7970
7971 if (invalid_nontype_parm_type_p (t, complain))
7972 return error_mark_node;
7973
7974 if (!type_dependent_expression_p (orig_arg)
7975 && !uses_template_parms (t))
7976 /* We used to call digest_init here. However, digest_init
7977 will report errors, which we don't want when complain
7978 is zero. More importantly, digest_init will try too
7979 hard to convert things: for example, `0' should not be
7980 converted to pointer type at this point according to
7981 the standard. Accepting this is not merely an
7982 extension, since deciding whether or not these
7983 conversions can occur is part of determining which
7984 function template to call, or whether a given explicit
7985 argument specification is valid. */
7986 val = convert_nontype_argument (t, orig_arg, complain);
7987 else
7988 {
7989 val = canonicalize_expr_argument (orig_arg, complain);
7990 val = maybe_convert_nontype_argument (t, val);
7991 }
7992
7993
7994 if (val == NULL_TREE)
7995 val = error_mark_node;
7996 else if (val == error_mark_node && (complain & tf_error))
7997 error ("could not convert template argument %qE from %qT to %qT",
7998 orig_arg, TREE_TYPE (orig_arg), t);
7999
8000 if (INDIRECT_REF_P (val))
8001 {
8002 /* Reject template arguments that are references to built-in
8003 functions with no library fallbacks. */
8004 const_tree inner = TREE_OPERAND (val, 0);
8005 const_tree innertype = TREE_TYPE (inner);
8006 if (innertype
8007 && TREE_CODE (innertype) == REFERENCE_TYPE
8008 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8009 && TREE_OPERAND_LENGTH (inner) > 0
8010 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8011 return error_mark_node;
8012 }
8013
8014 if (TREE_CODE (val) == SCOPE_REF)
8015 {
8016 /* Strip typedefs from the SCOPE_REF. */
8017 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8018 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8019 complain);
8020 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8021 QUALIFIED_NAME_IS_TEMPLATE (val));
8022 }
8023 }
8024
8025 return val;
8026 }
8027
8028 /* Coerces the remaining template arguments in INNER_ARGS (from
8029 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8030 Returns the coerced argument pack. PARM_IDX is the position of this
8031 parameter in the template parameter list. ARGS is the original
8032 template argument list. */
8033 static tree
8034 coerce_template_parameter_pack (tree parms,
8035 int parm_idx,
8036 tree args,
8037 tree inner_args,
8038 int arg_idx,
8039 tree new_args,
8040 int* lost,
8041 tree in_decl,
8042 tsubst_flags_t complain)
8043 {
8044 tree parm = TREE_VEC_ELT (parms, parm_idx);
8045 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8046 tree packed_args;
8047 tree argument_pack;
8048 tree packed_parms = NULL_TREE;
8049
8050 if (arg_idx > nargs)
8051 arg_idx = nargs;
8052
8053 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8054 {
8055 /* When the template parameter is a non-type template parameter pack
8056 or template template parameter pack whose type or template
8057 parameters use parameter packs, we know exactly how many arguments
8058 we are looking for. Build a vector of the instantiated decls for
8059 these template parameters in PACKED_PARMS. */
8060 /* We can't use make_pack_expansion here because it would interpret a
8061 _DECL as a use rather than a declaration. */
8062 tree decl = TREE_VALUE (parm);
8063 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8064 SET_PACK_EXPANSION_PATTERN (exp, decl);
8065 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8066 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8067
8068 TREE_VEC_LENGTH (args)--;
8069 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8070 TREE_VEC_LENGTH (args)++;
8071
8072 if (packed_parms == error_mark_node)
8073 return error_mark_node;
8074
8075 /* If we're doing a partial instantiation of a member template,
8076 verify that all of the types used for the non-type
8077 template parameter pack are, in fact, valid for non-type
8078 template parameters. */
8079 if (arg_idx < nargs
8080 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8081 {
8082 int j, len = TREE_VEC_LENGTH (packed_parms);
8083 for (j = 0; j < len; ++j)
8084 {
8085 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8086 if (invalid_nontype_parm_type_p (t, complain))
8087 return error_mark_node;
8088 }
8089 /* We don't know how many args we have yet, just
8090 use the unconverted ones for now. */
8091 return NULL_TREE;
8092 }
8093
8094 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8095 }
8096 /* Check if we have a placeholder pack, which indicates we're
8097 in the context of a introduction list. In that case we want
8098 to match this pack to the single placeholder. */
8099 else if (arg_idx < nargs
8100 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8101 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8102 {
8103 nargs = arg_idx + 1;
8104 packed_args = make_tree_vec (1);
8105 }
8106 else
8107 packed_args = make_tree_vec (nargs - arg_idx);
8108
8109 /* Convert the remaining arguments, which will be a part of the
8110 parameter pack "parm". */
8111 int first_pack_arg = arg_idx;
8112 for (; arg_idx < nargs; ++arg_idx)
8113 {
8114 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8115 tree actual_parm = TREE_VALUE (parm);
8116 int pack_idx = arg_idx - first_pack_arg;
8117
8118 if (packed_parms)
8119 {
8120 /* Once we've packed as many args as we have types, stop. */
8121 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8122 break;
8123 else if (PACK_EXPANSION_P (arg))
8124 /* We don't know how many args we have yet, just
8125 use the unconverted ones for now. */
8126 return NULL_TREE;
8127 else
8128 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8129 }
8130
8131 if (arg == error_mark_node)
8132 {
8133 if (complain & tf_error)
8134 error ("template argument %d is invalid", arg_idx + 1);
8135 }
8136 else
8137 arg = convert_template_argument (actual_parm,
8138 arg, new_args, complain, parm_idx,
8139 in_decl);
8140 if (arg == error_mark_node)
8141 (*lost)++;
8142 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8143 }
8144
8145 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8146 && TREE_VEC_LENGTH (packed_args) > 0)
8147 {
8148 if (complain & tf_error)
8149 error ("wrong number of template arguments (%d, should be %d)",
8150 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8151 return error_mark_node;
8152 }
8153
8154 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8155 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8156 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8157 else
8158 {
8159 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8160 TREE_CONSTANT (argument_pack) = 1;
8161 }
8162
8163 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8164 if (CHECKING_P)
8165 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8166 TREE_VEC_LENGTH (packed_args));
8167 return argument_pack;
8168 }
8169
8170 /* Returns the number of pack expansions in the template argument vector
8171 ARGS. */
8172
8173 static int
8174 pack_expansion_args_count (tree args)
8175 {
8176 int i;
8177 int count = 0;
8178 if (args)
8179 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8180 {
8181 tree elt = TREE_VEC_ELT (args, i);
8182 if (elt && PACK_EXPANSION_P (elt))
8183 ++count;
8184 }
8185 return count;
8186 }
8187
8188 /* Convert all template arguments to their appropriate types, and
8189 return a vector containing the innermost resulting template
8190 arguments. If any error occurs, return error_mark_node. Error and
8191 warning messages are issued under control of COMPLAIN.
8192
8193 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8194 for arguments not specified in ARGS. Otherwise, if
8195 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8196 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8197 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8198 ARGS. */
8199
8200 static tree
8201 coerce_template_parms (tree parms,
8202 tree args,
8203 tree in_decl,
8204 tsubst_flags_t complain,
8205 bool require_all_args,
8206 bool use_default_args)
8207 {
8208 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8209 tree orig_inner_args;
8210 tree inner_args;
8211 tree new_args;
8212 tree new_inner_args;
8213 int saved_unevaluated_operand;
8214 int saved_inhibit_evaluation_warnings;
8215
8216 /* When used as a boolean value, indicates whether this is a
8217 variadic template parameter list. Since it's an int, we can also
8218 subtract it from nparms to get the number of non-variadic
8219 parameters. */
8220 int variadic_p = 0;
8221 int variadic_args_p = 0;
8222 int post_variadic_parms = 0;
8223
8224 /* Likewise for parameters with default arguments. */
8225 int default_p = 0;
8226
8227 if (args == error_mark_node)
8228 return error_mark_node;
8229
8230 nparms = TREE_VEC_LENGTH (parms);
8231
8232 /* Determine if there are any parameter packs or default arguments. */
8233 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8234 {
8235 tree parm = TREE_VEC_ELT (parms, parm_idx);
8236 if (variadic_p)
8237 ++post_variadic_parms;
8238 if (template_parameter_pack_p (TREE_VALUE (parm)))
8239 ++variadic_p;
8240 if (TREE_PURPOSE (parm))
8241 ++default_p;
8242 }
8243
8244 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8245 /* If there are no parameters that follow a parameter pack, we need to
8246 expand any argument packs so that we can deduce a parameter pack from
8247 some non-packed args followed by an argument pack, as in variadic85.C.
8248 If there are such parameters, we need to leave argument packs intact
8249 so the arguments are assigned properly. This can happen when dealing
8250 with a nested class inside a partial specialization of a class
8251 template, as in variadic92.C, or when deducing a template parameter pack
8252 from a sub-declarator, as in variadic114.C. */
8253 if (!post_variadic_parms)
8254 inner_args = expand_template_argument_pack (inner_args);
8255
8256 /* Count any pack expansion args. */
8257 variadic_args_p = pack_expansion_args_count (inner_args);
8258
8259 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8260 if ((nargs - variadic_args_p > nparms && !variadic_p)
8261 || (nargs < nparms - variadic_p
8262 && require_all_args
8263 && !variadic_args_p
8264 && (!use_default_args
8265 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8266 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8267 {
8268 if (complain & tf_error)
8269 {
8270 if (variadic_p || default_p)
8271 {
8272 nparms -= variadic_p + default_p;
8273 error ("wrong number of template arguments "
8274 "(%d, should be at least %d)", nargs, nparms);
8275 }
8276 else
8277 error ("wrong number of template arguments "
8278 "(%d, should be %d)", nargs, nparms);
8279
8280 if (in_decl)
8281 inform (DECL_SOURCE_LOCATION (in_decl),
8282 "provided for %qD", in_decl);
8283 }
8284
8285 return error_mark_node;
8286 }
8287 /* We can't pass a pack expansion to a non-pack parameter of an alias
8288 template (DR 1430). */
8289 else if (in_decl
8290 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8291 || concept_template_p (in_decl))
8292 && variadic_args_p
8293 && nargs - variadic_args_p < nparms - variadic_p)
8294 {
8295 if (complain & tf_error)
8296 {
8297 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8298 {
8299 tree arg = TREE_VEC_ELT (inner_args, i);
8300 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8301
8302 if (PACK_EXPANSION_P (arg)
8303 && !template_parameter_pack_p (parm))
8304 {
8305 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8306 error_at (location_of (arg),
8307 "pack expansion argument for non-pack parameter "
8308 "%qD of alias template %qD", parm, in_decl);
8309 else
8310 error_at (location_of (arg),
8311 "pack expansion argument for non-pack parameter "
8312 "%qD of concept %qD", parm, in_decl);
8313 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8314 goto found;
8315 }
8316 }
8317 gcc_unreachable ();
8318 found:;
8319 }
8320 return error_mark_node;
8321 }
8322
8323 /* We need to evaluate the template arguments, even though this
8324 template-id may be nested within a "sizeof". */
8325 saved_unevaluated_operand = cp_unevaluated_operand;
8326 cp_unevaluated_operand = 0;
8327 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8328 c_inhibit_evaluation_warnings = 0;
8329 new_inner_args = make_tree_vec (nparms);
8330 new_args = add_outermost_template_args (args, new_inner_args);
8331 int pack_adjust = 0;
8332 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8333 {
8334 tree arg;
8335 tree parm;
8336
8337 /* Get the Ith template parameter. */
8338 parm = TREE_VEC_ELT (parms, parm_idx);
8339
8340 if (parm == error_mark_node)
8341 {
8342 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8343 continue;
8344 }
8345
8346 /* Calculate the next argument. */
8347 if (arg_idx < nargs)
8348 arg = TREE_VEC_ELT (inner_args, arg_idx);
8349 else
8350 arg = NULL_TREE;
8351
8352 if (template_parameter_pack_p (TREE_VALUE (parm))
8353 && !(arg && ARGUMENT_PACK_P (arg)))
8354 {
8355 /* Some arguments will be placed in the
8356 template parameter pack PARM. */
8357 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8358 inner_args, arg_idx,
8359 new_args, &lost,
8360 in_decl, complain);
8361
8362 if (arg == NULL_TREE)
8363 {
8364 /* We don't know how many args we have yet, just use the
8365 unconverted (and still packed) ones for now. */
8366 new_inner_args = orig_inner_args;
8367 arg_idx = nargs;
8368 break;
8369 }
8370
8371 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8372
8373 /* Store this argument. */
8374 if (arg == error_mark_node)
8375 {
8376 lost++;
8377 /* We are done with all of the arguments. */
8378 arg_idx = nargs;
8379 }
8380 else
8381 {
8382 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8383 arg_idx += pack_adjust;
8384 }
8385
8386 continue;
8387 }
8388 else if (arg)
8389 {
8390 if (PACK_EXPANSION_P (arg))
8391 {
8392 /* "If every valid specialization of a variadic template
8393 requires an empty template parameter pack, the template is
8394 ill-formed, no diagnostic required." So check that the
8395 pattern works with this parameter. */
8396 tree pattern = PACK_EXPANSION_PATTERN (arg);
8397 tree conv = convert_template_argument (TREE_VALUE (parm),
8398 pattern, new_args,
8399 complain, parm_idx,
8400 in_decl);
8401 if (conv == error_mark_node)
8402 {
8403 if (complain & tf_error)
8404 inform (input_location, "so any instantiation with a "
8405 "non-empty parameter pack would be ill-formed");
8406 ++lost;
8407 }
8408 else if (TYPE_P (conv) && !TYPE_P (pattern))
8409 /* Recover from missing typename. */
8410 TREE_VEC_ELT (inner_args, arg_idx)
8411 = make_pack_expansion (conv, complain);
8412
8413 /* We don't know how many args we have yet, just
8414 use the unconverted ones for now. */
8415 new_inner_args = inner_args;
8416 arg_idx = nargs;
8417 break;
8418 }
8419 }
8420 else if (require_all_args)
8421 {
8422 /* There must be a default arg in this case. */
8423 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8424 complain, in_decl);
8425 /* The position of the first default template argument,
8426 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8427 Record that. */
8428 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8429 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8430 arg_idx - pack_adjust);
8431 }
8432 else
8433 break;
8434
8435 if (arg == error_mark_node)
8436 {
8437 if (complain & tf_error)
8438 error ("template argument %d is invalid", arg_idx + 1);
8439 }
8440 else if (!arg)
8441 /* This only occurs if there was an error in the template
8442 parameter list itself (which we would already have
8443 reported) that we are trying to recover from, e.g., a class
8444 template with a parameter list such as
8445 template<typename..., typename>. */
8446 ++lost;
8447 else
8448 arg = convert_template_argument (TREE_VALUE (parm),
8449 arg, new_args, complain,
8450 parm_idx, in_decl);
8451
8452 if (arg == error_mark_node)
8453 lost++;
8454 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8455 }
8456 cp_unevaluated_operand = saved_unevaluated_operand;
8457 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8458
8459 if (variadic_p && arg_idx < nargs)
8460 {
8461 if (complain & tf_error)
8462 {
8463 error ("wrong number of template arguments "
8464 "(%d, should be %d)", nargs, arg_idx);
8465 if (in_decl)
8466 error ("provided for %q+D", in_decl);
8467 }
8468 return error_mark_node;
8469 }
8470
8471 if (lost)
8472 return error_mark_node;
8473
8474 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8475 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8476 TREE_VEC_LENGTH (new_inner_args));
8477
8478 return new_inner_args;
8479 }
8480
8481 /* Convert all template arguments to their appropriate types, and
8482 return a vector containing the innermost resulting template
8483 arguments. If any error occurs, return error_mark_node. Error and
8484 warning messages are not issued.
8485
8486 Note that no function argument deduction is performed, and default
8487 arguments are used to fill in unspecified arguments. */
8488 tree
8489 coerce_template_parms (tree parms, tree args, tree in_decl)
8490 {
8491 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8492 }
8493
8494 /* Convert all template arguments to their appropriate type, and
8495 instantiate default arguments as needed. This returns a vector
8496 containing the innermost resulting template arguments, or
8497 error_mark_node if unsuccessful. */
8498 tree
8499 coerce_template_parms (tree parms, tree args, tree in_decl,
8500 tsubst_flags_t complain)
8501 {
8502 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8503 }
8504
8505 /* Like coerce_template_parms. If PARMS represents all template
8506 parameters levels, this function returns a vector of vectors
8507 representing all the resulting argument levels. Note that in this
8508 case, only the innermost arguments are coerced because the
8509 outermost ones are supposed to have been coerced already.
8510
8511 Otherwise, if PARMS represents only (the innermost) vector of
8512 parameters, this function returns a vector containing just the
8513 innermost resulting arguments. */
8514
8515 static tree
8516 coerce_innermost_template_parms (tree parms,
8517 tree args,
8518 tree in_decl,
8519 tsubst_flags_t complain,
8520 bool require_all_args,
8521 bool use_default_args)
8522 {
8523 int parms_depth = TMPL_PARMS_DEPTH (parms);
8524 int args_depth = TMPL_ARGS_DEPTH (args);
8525 tree coerced_args;
8526
8527 if (parms_depth > 1)
8528 {
8529 coerced_args = make_tree_vec (parms_depth);
8530 tree level;
8531 int cur_depth;
8532
8533 for (level = parms, cur_depth = parms_depth;
8534 parms_depth > 0 && level != NULL_TREE;
8535 level = TREE_CHAIN (level), --cur_depth)
8536 {
8537 tree l;
8538 if (cur_depth == args_depth)
8539 l = coerce_template_parms (TREE_VALUE (level),
8540 args, in_decl, complain,
8541 require_all_args,
8542 use_default_args);
8543 else
8544 l = TMPL_ARGS_LEVEL (args, cur_depth);
8545
8546 if (l == error_mark_node)
8547 return error_mark_node;
8548
8549 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8550 }
8551 }
8552 else
8553 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8554 args, in_decl, complain,
8555 require_all_args,
8556 use_default_args);
8557 return coerced_args;
8558 }
8559
8560 /* Returns 1 if template args OT and NT are equivalent. */
8561
8562 int
8563 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8564 {
8565 if (nt == ot)
8566 return 1;
8567 if (nt == NULL_TREE || ot == NULL_TREE)
8568 return false;
8569 if (nt == any_targ_node || ot == any_targ_node)
8570 return true;
8571
8572 if (TREE_CODE (nt) == TREE_VEC)
8573 /* For member templates */
8574 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8575 else if (PACK_EXPANSION_P (ot))
8576 return (PACK_EXPANSION_P (nt)
8577 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8578 PACK_EXPANSION_PATTERN (nt))
8579 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8580 PACK_EXPANSION_EXTRA_ARGS (nt)));
8581 else if (ARGUMENT_PACK_P (ot))
8582 {
8583 int i, len;
8584 tree opack, npack;
8585
8586 if (!ARGUMENT_PACK_P (nt))
8587 return 0;
8588
8589 opack = ARGUMENT_PACK_ARGS (ot);
8590 npack = ARGUMENT_PACK_ARGS (nt);
8591 len = TREE_VEC_LENGTH (opack);
8592 if (TREE_VEC_LENGTH (npack) != len)
8593 return 0;
8594 for (i = 0; i < len; ++i)
8595 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8596 TREE_VEC_ELT (npack, i)))
8597 return 0;
8598 return 1;
8599 }
8600 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8601 gcc_unreachable ();
8602 else if (TYPE_P (nt))
8603 {
8604 if (!TYPE_P (ot))
8605 return false;
8606 /* Don't treat an alias template specialization with dependent
8607 arguments as equivalent to its underlying type when used as a
8608 template argument; we need them to be distinct so that we
8609 substitute into the specialization arguments at instantiation
8610 time. And aliases can't be equivalent without being ==, so
8611 we don't need to look any deeper.
8612
8613 During partial ordering, however, we need to treat them normally so
8614 that we can order uses of the same alias with different
8615 cv-qualification (79960). */
8616 if (!partial_order
8617 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8618 return false;
8619 else
8620 return same_type_p (ot, nt);
8621 }
8622 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8623 return 0;
8624 else
8625 {
8626 /* Try to treat a template non-type argument that has been converted
8627 to the parameter type as equivalent to one that hasn't yet. */
8628 for (enum tree_code code1 = TREE_CODE (ot);
8629 CONVERT_EXPR_CODE_P (code1)
8630 || code1 == NON_LVALUE_EXPR;
8631 code1 = TREE_CODE (ot))
8632 ot = TREE_OPERAND (ot, 0);
8633 for (enum tree_code code2 = TREE_CODE (nt);
8634 CONVERT_EXPR_CODE_P (code2)
8635 || code2 == NON_LVALUE_EXPR;
8636 code2 = TREE_CODE (nt))
8637 nt = TREE_OPERAND (nt, 0);
8638
8639 return cp_tree_equal (ot, nt);
8640 }
8641 }
8642
8643 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8644 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8645 NEWARG_PTR with the offending arguments if they are non-NULL. */
8646
8647 int
8648 comp_template_args (tree oldargs, tree newargs,
8649 tree *oldarg_ptr, tree *newarg_ptr,
8650 bool partial_order)
8651 {
8652 int i;
8653
8654 if (oldargs == newargs)
8655 return 1;
8656
8657 if (!oldargs || !newargs)
8658 return 0;
8659
8660 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8661 return 0;
8662
8663 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8664 {
8665 tree nt = TREE_VEC_ELT (newargs, i);
8666 tree ot = TREE_VEC_ELT (oldargs, i);
8667
8668 if (! template_args_equal (ot, nt, partial_order))
8669 {
8670 if (oldarg_ptr != NULL)
8671 *oldarg_ptr = ot;
8672 if (newarg_ptr != NULL)
8673 *newarg_ptr = nt;
8674 return 0;
8675 }
8676 }
8677 return 1;
8678 }
8679
8680 inline bool
8681 comp_template_args_porder (tree oargs, tree nargs)
8682 {
8683 return comp_template_args (oargs, nargs, NULL, NULL, true);
8684 }
8685
8686 static void
8687 add_pending_template (tree d)
8688 {
8689 tree ti = (TYPE_P (d)
8690 ? CLASSTYPE_TEMPLATE_INFO (d)
8691 : DECL_TEMPLATE_INFO (d));
8692 struct pending_template *pt;
8693 int level;
8694
8695 if (TI_PENDING_TEMPLATE_FLAG (ti))
8696 return;
8697
8698 /* We are called both from instantiate_decl, where we've already had a
8699 tinst_level pushed, and instantiate_template, where we haven't.
8700 Compensate. */
8701 level = !current_tinst_level || current_tinst_level->decl != d;
8702
8703 if (level)
8704 push_tinst_level (d);
8705
8706 pt = ggc_alloc<pending_template> ();
8707 pt->next = NULL;
8708 pt->tinst = current_tinst_level;
8709 if (last_pending_template)
8710 last_pending_template->next = pt;
8711 else
8712 pending_templates = pt;
8713
8714 last_pending_template = pt;
8715
8716 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8717
8718 if (level)
8719 pop_tinst_level ();
8720 }
8721
8722
8723 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8724 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8725 documentation for TEMPLATE_ID_EXPR. */
8726
8727 tree
8728 lookup_template_function (tree fns, tree arglist)
8729 {
8730 tree type;
8731
8732 if (fns == error_mark_node || arglist == error_mark_node)
8733 return error_mark_node;
8734
8735 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8736
8737 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8738 {
8739 error ("%q#D is not a function template", fns);
8740 return error_mark_node;
8741 }
8742
8743 if (BASELINK_P (fns))
8744 {
8745 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8746 unknown_type_node,
8747 BASELINK_FUNCTIONS (fns),
8748 arglist);
8749 return fns;
8750 }
8751
8752 type = TREE_TYPE (fns);
8753 if (TREE_CODE (fns) == OVERLOAD || !type)
8754 type = unknown_type_node;
8755
8756 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8757 }
8758
8759 /* Within the scope of a template class S<T>, the name S gets bound
8760 (in build_self_reference) to a TYPE_DECL for the class, not a
8761 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8762 or one of its enclosing classes, and that type is a template,
8763 return the associated TEMPLATE_DECL. Otherwise, the original
8764 DECL is returned.
8765
8766 Also handle the case when DECL is a TREE_LIST of ambiguous
8767 injected-class-names from different bases. */
8768
8769 tree
8770 maybe_get_template_decl_from_type_decl (tree decl)
8771 {
8772 if (decl == NULL_TREE)
8773 return decl;
8774
8775 /* DR 176: A lookup that finds an injected-class-name (10.2
8776 [class.member.lookup]) can result in an ambiguity in certain cases
8777 (for example, if it is found in more than one base class). If all of
8778 the injected-class-names that are found refer to specializations of
8779 the same class template, and if the name is followed by a
8780 template-argument-list, the reference refers to the class template
8781 itself and not a specialization thereof, and is not ambiguous. */
8782 if (TREE_CODE (decl) == TREE_LIST)
8783 {
8784 tree t, tmpl = NULL_TREE;
8785 for (t = decl; t; t = TREE_CHAIN (t))
8786 {
8787 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8788 if (!tmpl)
8789 tmpl = elt;
8790 else if (tmpl != elt)
8791 break;
8792 }
8793 if (tmpl && t == NULL_TREE)
8794 return tmpl;
8795 else
8796 return decl;
8797 }
8798
8799 return (decl != NULL_TREE
8800 && DECL_SELF_REFERENCE_P (decl)
8801 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8802 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8803 }
8804
8805 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8806 parameters, find the desired type.
8807
8808 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8809
8810 IN_DECL, if non-NULL, is the template declaration we are trying to
8811 instantiate.
8812
8813 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8814 the class we are looking up.
8815
8816 Issue error and warning messages under control of COMPLAIN.
8817
8818 If the template class is really a local class in a template
8819 function, then the FUNCTION_CONTEXT is the function in which it is
8820 being instantiated.
8821
8822 ??? Note that this function is currently called *twice* for each
8823 template-id: the first time from the parser, while creating the
8824 incomplete type (finish_template_type), and the second type during the
8825 real instantiation (instantiate_template_class). This is surely something
8826 that we want to avoid. It also causes some problems with argument
8827 coercion (see convert_nontype_argument for more information on this). */
8828
8829 static tree
8830 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8831 int entering_scope, tsubst_flags_t complain)
8832 {
8833 tree templ = NULL_TREE, parmlist;
8834 tree t;
8835 spec_entry **slot;
8836 spec_entry *entry;
8837 spec_entry elt;
8838 hashval_t hash;
8839
8840 if (identifier_p (d1))
8841 {
8842 tree value = innermost_non_namespace_value (d1);
8843 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8844 templ = value;
8845 else
8846 {
8847 if (context)
8848 push_decl_namespace (context);
8849 templ = lookup_name (d1);
8850 templ = maybe_get_template_decl_from_type_decl (templ);
8851 if (context)
8852 pop_decl_namespace ();
8853 }
8854 if (templ)
8855 context = DECL_CONTEXT (templ);
8856 }
8857 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8858 {
8859 tree type = TREE_TYPE (d1);
8860
8861 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8862 an implicit typename for the second A. Deal with it. */
8863 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8864 type = TREE_TYPE (type);
8865
8866 if (CLASSTYPE_TEMPLATE_INFO (type))
8867 {
8868 templ = CLASSTYPE_TI_TEMPLATE (type);
8869 d1 = DECL_NAME (templ);
8870 }
8871 }
8872 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8873 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8874 {
8875 templ = TYPE_TI_TEMPLATE (d1);
8876 d1 = DECL_NAME (templ);
8877 }
8878 else if (DECL_TYPE_TEMPLATE_P (d1))
8879 {
8880 templ = d1;
8881 d1 = DECL_NAME (templ);
8882 context = DECL_CONTEXT (templ);
8883 }
8884 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8885 {
8886 templ = d1;
8887 d1 = DECL_NAME (templ);
8888 }
8889
8890 /* Issue an error message if we didn't find a template. */
8891 if (! templ)
8892 {
8893 if (complain & tf_error)
8894 error ("%qT is not a template", d1);
8895 return error_mark_node;
8896 }
8897
8898 if (TREE_CODE (templ) != TEMPLATE_DECL
8899 /* Make sure it's a user visible template, if it was named by
8900 the user. */
8901 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8902 && !PRIMARY_TEMPLATE_P (templ)))
8903 {
8904 if (complain & tf_error)
8905 {
8906 error ("non-template type %qT used as a template", d1);
8907 if (in_decl)
8908 error ("for template declaration %q+D", in_decl);
8909 }
8910 return error_mark_node;
8911 }
8912
8913 complain &= ~tf_user;
8914
8915 /* An alias that just changes the name of a template is equivalent to the
8916 other template, so if any of the arguments are pack expansions, strip
8917 the alias to avoid problems with a pack expansion passed to a non-pack
8918 alias template parameter (DR 1430). */
8919 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8920 templ = get_underlying_template (templ);
8921
8922 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8923 {
8924 tree parm;
8925 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8926 if (arglist2 == error_mark_node
8927 || (!uses_template_parms (arglist2)
8928 && check_instantiated_args (templ, arglist2, complain)))
8929 return error_mark_node;
8930
8931 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8932 return parm;
8933 }
8934 else
8935 {
8936 tree template_type = TREE_TYPE (templ);
8937 tree gen_tmpl;
8938 tree type_decl;
8939 tree found = NULL_TREE;
8940 int arg_depth;
8941 int parm_depth;
8942 int is_dependent_type;
8943 int use_partial_inst_tmpl = false;
8944
8945 if (template_type == error_mark_node)
8946 /* An error occurred while building the template TEMPL, and a
8947 diagnostic has most certainly been emitted for that
8948 already. Let's propagate that error. */
8949 return error_mark_node;
8950
8951 gen_tmpl = most_general_template (templ);
8952 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8953 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8954 arg_depth = TMPL_ARGS_DEPTH (arglist);
8955
8956 if (arg_depth == 1 && parm_depth > 1)
8957 {
8958 /* We've been given an incomplete set of template arguments.
8959 For example, given:
8960
8961 template <class T> struct S1 {
8962 template <class U> struct S2 {};
8963 template <class U> struct S2<U*> {};
8964 };
8965
8966 we will be called with an ARGLIST of `U*', but the
8967 TEMPLATE will be `template <class T> template
8968 <class U> struct S1<T>::S2'. We must fill in the missing
8969 arguments. */
8970 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8971 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8972 arg_depth = TMPL_ARGS_DEPTH (arglist);
8973 }
8974
8975 /* Now we should have enough arguments. */
8976 gcc_assert (parm_depth == arg_depth);
8977
8978 /* From here on, we're only interested in the most general
8979 template. */
8980
8981 /* Calculate the BOUND_ARGS. These will be the args that are
8982 actually tsubst'd into the definition to create the
8983 instantiation. */
8984 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8985 complain,
8986 /*require_all_args=*/true,
8987 /*use_default_args=*/true);
8988
8989 if (arglist == error_mark_node)
8990 /* We were unable to bind the arguments. */
8991 return error_mark_node;
8992
8993 /* In the scope of a template class, explicit references to the
8994 template class refer to the type of the template, not any
8995 instantiation of it. For example, in:
8996
8997 template <class T> class C { void f(C<T>); }
8998
8999 the `C<T>' is just the same as `C'. Outside of the
9000 class, however, such a reference is an instantiation. */
9001 if (entering_scope
9002 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9003 || currently_open_class (template_type))
9004 {
9005 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9006
9007 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9008 return template_type;
9009 }
9010
9011 /* If we already have this specialization, return it. */
9012 elt.tmpl = gen_tmpl;
9013 elt.args = arglist;
9014 elt.spec = NULL_TREE;
9015 hash = spec_hasher::hash (&elt);
9016 entry = type_specializations->find_with_hash (&elt, hash);
9017
9018 if (entry)
9019 return entry->spec;
9020
9021 /* If the the template's constraints are not satisfied,
9022 then we cannot form a valid type.
9023
9024 Note that the check is deferred until after the hash
9025 lookup. This prevents redundant checks on previously
9026 instantiated specializations. */
9027 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9028 {
9029 if (complain & tf_error)
9030 {
9031 error ("template constraint failure");
9032 diagnose_constraints (input_location, gen_tmpl, arglist);
9033 }
9034 return error_mark_node;
9035 }
9036
9037 is_dependent_type = uses_template_parms (arglist);
9038
9039 /* If the deduced arguments are invalid, then the binding
9040 failed. */
9041 if (!is_dependent_type
9042 && check_instantiated_args (gen_tmpl,
9043 INNERMOST_TEMPLATE_ARGS (arglist),
9044 complain))
9045 return error_mark_node;
9046
9047 if (!is_dependent_type
9048 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9049 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9050 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9051 {
9052 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9053 DECL_NAME (gen_tmpl),
9054 /*tag_scope=*/ts_global);
9055 return found;
9056 }
9057
9058 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
9059 complain, in_decl);
9060 if (context == error_mark_node)
9061 return error_mark_node;
9062
9063 if (!context)
9064 context = global_namespace;
9065
9066 /* Create the type. */
9067 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9068 {
9069 /* The user referred to a specialization of an alias
9070 template represented by GEN_TMPL.
9071
9072 [temp.alias]/2 says:
9073
9074 When a template-id refers to the specialization of an
9075 alias template, it is equivalent to the associated
9076 type obtained by substitution of its
9077 template-arguments for the template-parameters in the
9078 type-id of the alias template. */
9079
9080 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9081 /* Note that the call above (by indirectly calling
9082 register_specialization in tsubst_decl) registers the
9083 TYPE_DECL representing the specialization of the alias
9084 template. So next time someone substitutes ARGLIST for
9085 the template parms into the alias template (GEN_TMPL),
9086 she'll get that TYPE_DECL back. */
9087
9088 if (t == error_mark_node)
9089 return t;
9090 }
9091 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9092 {
9093 if (!is_dependent_type)
9094 {
9095 set_current_access_from_decl (TYPE_NAME (template_type));
9096 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9097 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9098 arglist, complain, in_decl),
9099 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9100 arglist, complain, in_decl),
9101 SCOPED_ENUM_P (template_type), NULL);
9102
9103 if (t == error_mark_node)
9104 return t;
9105 }
9106 else
9107 {
9108 /* We don't want to call start_enum for this type, since
9109 the values for the enumeration constants may involve
9110 template parameters. And, no one should be interested
9111 in the enumeration constants for such a type. */
9112 t = cxx_make_type (ENUMERAL_TYPE);
9113 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9114 }
9115 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9116 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9117 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9118 }
9119 else if (CLASS_TYPE_P (template_type))
9120 {
9121 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9122 instantiated here. */
9123 gcc_assert (!LAMBDA_TYPE_P (template_type));
9124
9125 t = make_class_type (TREE_CODE (template_type));
9126 CLASSTYPE_DECLARED_CLASS (t)
9127 = CLASSTYPE_DECLARED_CLASS (template_type);
9128 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9129
9130 /* A local class. Make sure the decl gets registered properly. */
9131 if (context == current_function_decl)
9132 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
9133
9134 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9135 /* This instantiation is another name for the primary
9136 template type. Set the TYPE_CANONICAL field
9137 appropriately. */
9138 TYPE_CANONICAL (t) = template_type;
9139 else if (any_template_arguments_need_structural_equality_p (arglist))
9140 /* Some of the template arguments require structural
9141 equality testing, so this template class requires
9142 structural equality testing. */
9143 SET_TYPE_STRUCTURAL_EQUALITY (t);
9144 }
9145 else
9146 gcc_unreachable ();
9147
9148 /* If we called start_enum or pushtag above, this information
9149 will already be set up. */
9150 if (!TYPE_NAME (t))
9151 {
9152 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9153
9154 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9155 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9156 DECL_SOURCE_LOCATION (type_decl)
9157 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9158 }
9159 else
9160 type_decl = TYPE_NAME (t);
9161
9162 if (CLASS_TYPE_P (template_type))
9163 {
9164 TREE_PRIVATE (type_decl)
9165 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9166 TREE_PROTECTED (type_decl)
9167 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9168 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9169 {
9170 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9171 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9172 }
9173 }
9174
9175 if (OVERLOAD_TYPE_P (t)
9176 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9177 {
9178 static const char *tags[] = {"abi_tag", "may_alias"};
9179
9180 for (unsigned ix = 0; ix != 2; ix++)
9181 {
9182 tree attributes
9183 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9184
9185 if (attributes)
9186 TYPE_ATTRIBUTES (t)
9187 = tree_cons (TREE_PURPOSE (attributes),
9188 TREE_VALUE (attributes),
9189 TYPE_ATTRIBUTES (t));
9190 }
9191 }
9192
9193 /* Let's consider the explicit specialization of a member
9194 of a class template specialization that is implicitly instantiated,
9195 e.g.:
9196 template<class T>
9197 struct S
9198 {
9199 template<class U> struct M {}; //#0
9200 };
9201
9202 template<>
9203 template<>
9204 struct S<int>::M<char> //#1
9205 {
9206 int i;
9207 };
9208 [temp.expl.spec]/4 says this is valid.
9209
9210 In this case, when we write:
9211 S<int>::M<char> m;
9212
9213 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9214 the one of #0.
9215
9216 When we encounter #1, we want to store the partial instantiation
9217 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9218
9219 For all cases other than this "explicit specialization of member of a
9220 class template", we just want to store the most general template into
9221 the CLASSTYPE_TI_TEMPLATE of M.
9222
9223 This case of "explicit specialization of member of a class template"
9224 only happens when:
9225 1/ the enclosing class is an instantiation of, and therefore not
9226 the same as, the context of the most general template, and
9227 2/ we aren't looking at the partial instantiation itself, i.e.
9228 the innermost arguments are not the same as the innermost parms of
9229 the most general template.
9230
9231 So it's only when 1/ and 2/ happens that we want to use the partial
9232 instantiation of the member template in lieu of its most general
9233 template. */
9234
9235 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9236 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9237 /* the enclosing class must be an instantiation... */
9238 && CLASS_TYPE_P (context)
9239 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9240 {
9241 TREE_VEC_LENGTH (arglist)--;
9242 ++processing_template_decl;
9243 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9244 tree partial_inst_args =
9245 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9246 arglist, complain, NULL_TREE);
9247 --processing_template_decl;
9248 TREE_VEC_LENGTH (arglist)++;
9249 if (partial_inst_args == error_mark_node)
9250 return error_mark_node;
9251 use_partial_inst_tmpl =
9252 /*...and we must not be looking at the partial instantiation
9253 itself. */
9254 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9255 partial_inst_args);
9256 }
9257
9258 if (!use_partial_inst_tmpl)
9259 /* This case is easy; there are no member templates involved. */
9260 found = gen_tmpl;
9261 else
9262 {
9263 /* This is a full instantiation of a member template. Find
9264 the partial instantiation of which this is an instance. */
9265
9266 /* Temporarily reduce by one the number of levels in the ARGLIST
9267 so as to avoid comparing the last set of arguments. */
9268 TREE_VEC_LENGTH (arglist)--;
9269 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9270 TREE_VEC_LENGTH (arglist)++;
9271 /* FOUND is either a proper class type, or an alias
9272 template specialization. In the later case, it's a
9273 TYPE_DECL, resulting from the substituting of arguments
9274 for parameters in the TYPE_DECL of the alias template
9275 done earlier. So be careful while getting the template
9276 of FOUND. */
9277 found = (TREE_CODE (found) == TEMPLATE_DECL
9278 ? found
9279 : (TREE_CODE (found) == TYPE_DECL
9280 ? DECL_TI_TEMPLATE (found)
9281 : CLASSTYPE_TI_TEMPLATE (found)));
9282 }
9283
9284 // Build template info for the new specialization.
9285 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9286
9287 elt.spec = t;
9288 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9289 entry = ggc_alloc<spec_entry> ();
9290 *entry = elt;
9291 *slot = entry;
9292
9293 /* Note this use of the partial instantiation so we can check it
9294 later in maybe_process_partial_specialization. */
9295 DECL_TEMPLATE_INSTANTIATIONS (found)
9296 = tree_cons (arglist, t,
9297 DECL_TEMPLATE_INSTANTIATIONS (found));
9298
9299 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9300 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9301 /* Now that the type has been registered on the instantiations
9302 list, we set up the enumerators. Because the enumeration
9303 constants may involve the enumeration type itself, we make
9304 sure to register the type first, and then create the
9305 constants. That way, doing tsubst_expr for the enumeration
9306 constants won't result in recursive calls here; we'll find
9307 the instantiation and exit above. */
9308 tsubst_enum (template_type, t, arglist);
9309
9310 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9311 /* If the type makes use of template parameters, the
9312 code that generates debugging information will crash. */
9313 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9314
9315 /* Possibly limit visibility based on template args. */
9316 TREE_PUBLIC (type_decl) = 1;
9317 determine_visibility (type_decl);
9318
9319 inherit_targ_abi_tags (t);
9320
9321 return t;
9322 }
9323 }
9324
9325 /* Wrapper for lookup_template_class_1. */
9326
9327 tree
9328 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9329 int entering_scope, tsubst_flags_t complain)
9330 {
9331 tree ret;
9332 timevar_push (TV_TEMPLATE_INST);
9333 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9334 entering_scope, complain);
9335 timevar_pop (TV_TEMPLATE_INST);
9336 return ret;
9337 }
9338
9339 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9340
9341 tree
9342 lookup_template_variable (tree templ, tree arglist)
9343 {
9344 /* The type of the expression is NULL_TREE since the template-id could refer
9345 to an explicit or partial specialization. */
9346 tree type = NULL_TREE;
9347 if (flag_concepts && variable_concept_p (templ))
9348 /* Except that concepts are always bool. */
9349 type = boolean_type_node;
9350 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9351 }
9352
9353 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9354
9355 tree
9356 finish_template_variable (tree var, tsubst_flags_t complain)
9357 {
9358 tree templ = TREE_OPERAND (var, 0);
9359 tree arglist = TREE_OPERAND (var, 1);
9360
9361 /* We never want to return a VAR_DECL for a variable concept, since they
9362 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9363 bool concept_p = flag_concepts && variable_concept_p (templ);
9364 if (concept_p && processing_template_decl)
9365 return var;
9366
9367 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9368 arglist = add_outermost_template_args (tmpl_args, arglist);
9369
9370 templ = most_general_template (templ);
9371 tree parms = DECL_TEMPLATE_PARMS (templ);
9372 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9373 /*req_all*/true,
9374 /*use_default*/true);
9375
9376 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9377 {
9378 if (complain & tf_error)
9379 {
9380 error ("use of invalid variable template %qE", var);
9381 diagnose_constraints (location_of (var), templ, arglist);
9382 }
9383 return error_mark_node;
9384 }
9385
9386 /* If a template-id refers to a specialization of a variable
9387 concept, then the expression is true if and only if the
9388 concept's constraints are satisfied by the given template
9389 arguments.
9390
9391 NOTE: This is an extension of Concepts Lite TS that
9392 allows constraints to be used in expressions. */
9393 if (concept_p)
9394 {
9395 tree decl = DECL_TEMPLATE_RESULT (templ);
9396 return evaluate_variable_concept (decl, arglist);
9397 }
9398
9399 return instantiate_template (templ, arglist, complain);
9400 }
9401
9402 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9403 TARGS template args, and instantiate it if it's not dependent. */
9404
9405 tree
9406 lookup_and_finish_template_variable (tree templ, tree targs,
9407 tsubst_flags_t complain)
9408 {
9409 templ = lookup_template_variable (templ, targs);
9410 if (!any_dependent_template_arguments_p (targs))
9411 {
9412 templ = finish_template_variable (templ, complain);
9413 mark_used (templ);
9414 }
9415
9416 return convert_from_reference (templ);
9417 }
9418
9419 \f
9420 struct pair_fn_data
9421 {
9422 tree_fn_t fn;
9423 tree_fn_t any_fn;
9424 void *data;
9425 /* True when we should also visit template parameters that occur in
9426 non-deduced contexts. */
9427 bool include_nondeduced_p;
9428 hash_set<tree> *visited;
9429 };
9430
9431 /* Called from for_each_template_parm via walk_tree. */
9432
9433 static tree
9434 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9435 {
9436 tree t = *tp;
9437 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9438 tree_fn_t fn = pfd->fn;
9439 void *data = pfd->data;
9440 tree result = NULL_TREE;
9441
9442 #define WALK_SUBTREE(NODE) \
9443 do \
9444 { \
9445 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9446 pfd->include_nondeduced_p, \
9447 pfd->any_fn); \
9448 if (result) goto out; \
9449 } \
9450 while (0)
9451
9452 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9453 return t;
9454
9455 if (TYPE_P (t)
9456 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9457 WALK_SUBTREE (TYPE_CONTEXT (t));
9458
9459 switch (TREE_CODE (t))
9460 {
9461 case RECORD_TYPE:
9462 if (TYPE_PTRMEMFUNC_P (t))
9463 break;
9464 /* Fall through. */
9465
9466 case UNION_TYPE:
9467 case ENUMERAL_TYPE:
9468 if (!TYPE_TEMPLATE_INFO (t))
9469 *walk_subtrees = 0;
9470 else
9471 WALK_SUBTREE (TYPE_TI_ARGS (t));
9472 break;
9473
9474 case INTEGER_TYPE:
9475 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9476 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9477 break;
9478
9479 case METHOD_TYPE:
9480 /* Since we're not going to walk subtrees, we have to do this
9481 explicitly here. */
9482 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9483 /* Fall through. */
9484
9485 case FUNCTION_TYPE:
9486 /* Check the return type. */
9487 WALK_SUBTREE (TREE_TYPE (t));
9488
9489 /* Check the parameter types. Since default arguments are not
9490 instantiated until they are needed, the TYPE_ARG_TYPES may
9491 contain expressions that involve template parameters. But,
9492 no-one should be looking at them yet. And, once they're
9493 instantiated, they don't contain template parameters, so
9494 there's no point in looking at them then, either. */
9495 {
9496 tree parm;
9497
9498 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9499 WALK_SUBTREE (TREE_VALUE (parm));
9500
9501 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9502 want walk_tree walking into them itself. */
9503 *walk_subtrees = 0;
9504 }
9505
9506 if (flag_noexcept_type)
9507 {
9508 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9509 if (spec)
9510 WALK_SUBTREE (TREE_PURPOSE (spec));
9511 }
9512 break;
9513
9514 case TYPEOF_TYPE:
9515 case UNDERLYING_TYPE:
9516 if (pfd->include_nondeduced_p
9517 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9518 pfd->visited,
9519 pfd->include_nondeduced_p,
9520 pfd->any_fn))
9521 return error_mark_node;
9522 break;
9523
9524 case FUNCTION_DECL:
9525 case VAR_DECL:
9526 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9527 WALK_SUBTREE (DECL_TI_ARGS (t));
9528 /* Fall through. */
9529
9530 case PARM_DECL:
9531 case CONST_DECL:
9532 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9533 WALK_SUBTREE (DECL_INITIAL (t));
9534 if (DECL_CONTEXT (t)
9535 && pfd->include_nondeduced_p)
9536 WALK_SUBTREE (DECL_CONTEXT (t));
9537 break;
9538
9539 case BOUND_TEMPLATE_TEMPLATE_PARM:
9540 /* Record template parameters such as `T' inside `TT<T>'. */
9541 WALK_SUBTREE (TYPE_TI_ARGS (t));
9542 /* Fall through. */
9543
9544 case TEMPLATE_TEMPLATE_PARM:
9545 case TEMPLATE_TYPE_PARM:
9546 case TEMPLATE_PARM_INDEX:
9547 if (fn && (*fn)(t, data))
9548 return t;
9549 else if (!fn)
9550 return t;
9551 break;
9552
9553 case TEMPLATE_DECL:
9554 /* A template template parameter is encountered. */
9555 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9556 WALK_SUBTREE (TREE_TYPE (t));
9557
9558 /* Already substituted template template parameter */
9559 *walk_subtrees = 0;
9560 break;
9561
9562 case TYPENAME_TYPE:
9563 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9564 partial instantiation. */
9565 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9566 break;
9567
9568 case CONSTRUCTOR:
9569 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9570 && pfd->include_nondeduced_p)
9571 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9572 break;
9573
9574 case INDIRECT_REF:
9575 case COMPONENT_REF:
9576 /* If there's no type, then this thing must be some expression
9577 involving template parameters. */
9578 if (!fn && !TREE_TYPE (t))
9579 return error_mark_node;
9580 break;
9581
9582 case MODOP_EXPR:
9583 case CAST_EXPR:
9584 case IMPLICIT_CONV_EXPR:
9585 case REINTERPRET_CAST_EXPR:
9586 case CONST_CAST_EXPR:
9587 case STATIC_CAST_EXPR:
9588 case DYNAMIC_CAST_EXPR:
9589 case ARROW_EXPR:
9590 case DOTSTAR_EXPR:
9591 case TYPEID_EXPR:
9592 case PSEUDO_DTOR_EXPR:
9593 if (!fn)
9594 return error_mark_node;
9595 break;
9596
9597 default:
9598 break;
9599 }
9600
9601 #undef WALK_SUBTREE
9602
9603 /* We didn't find any template parameters we liked. */
9604 out:
9605 return result;
9606 }
9607
9608 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9609 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9610 call FN with the parameter and the DATA.
9611 If FN returns nonzero, the iteration is terminated, and
9612 for_each_template_parm returns 1. Otherwise, the iteration
9613 continues. If FN never returns a nonzero value, the value
9614 returned by for_each_template_parm is 0. If FN is NULL, it is
9615 considered to be the function which always returns 1.
9616
9617 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9618 parameters that occur in non-deduced contexts. When false, only
9619 visits those template parameters that can be deduced. */
9620
9621 static tree
9622 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9623 hash_set<tree> *visited,
9624 bool include_nondeduced_p,
9625 tree_fn_t any_fn)
9626 {
9627 struct pair_fn_data pfd;
9628 tree result;
9629
9630 /* Set up. */
9631 pfd.fn = fn;
9632 pfd.any_fn = any_fn;
9633 pfd.data = data;
9634 pfd.include_nondeduced_p = include_nondeduced_p;
9635
9636 /* Walk the tree. (Conceptually, we would like to walk without
9637 duplicates, but for_each_template_parm_r recursively calls
9638 for_each_template_parm, so we would need to reorganize a fair
9639 bit to use walk_tree_without_duplicates, so we keep our own
9640 visited list.) */
9641 if (visited)
9642 pfd.visited = visited;
9643 else
9644 pfd.visited = new hash_set<tree>;
9645 result = cp_walk_tree (&t,
9646 for_each_template_parm_r,
9647 &pfd,
9648 pfd.visited);
9649
9650 /* Clean up. */
9651 if (!visited)
9652 {
9653 delete pfd.visited;
9654 pfd.visited = 0;
9655 }
9656
9657 return result;
9658 }
9659
9660 /* Returns true if T depends on any template parameter. */
9661
9662 int
9663 uses_template_parms (tree t)
9664 {
9665 if (t == NULL_TREE)
9666 return false;
9667
9668 bool dependent_p;
9669 int saved_processing_template_decl;
9670
9671 saved_processing_template_decl = processing_template_decl;
9672 if (!saved_processing_template_decl)
9673 processing_template_decl = 1;
9674 if (TYPE_P (t))
9675 dependent_p = dependent_type_p (t);
9676 else if (TREE_CODE (t) == TREE_VEC)
9677 dependent_p = any_dependent_template_arguments_p (t);
9678 else if (TREE_CODE (t) == TREE_LIST)
9679 dependent_p = (uses_template_parms (TREE_VALUE (t))
9680 || uses_template_parms (TREE_CHAIN (t)));
9681 else if (TREE_CODE (t) == TYPE_DECL)
9682 dependent_p = dependent_type_p (TREE_TYPE (t));
9683 else if (DECL_P (t)
9684 || EXPR_P (t)
9685 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9686 || TREE_CODE (t) == OVERLOAD
9687 || BASELINK_P (t)
9688 || identifier_p (t)
9689 || TREE_CODE (t) == TRAIT_EXPR
9690 || TREE_CODE (t) == CONSTRUCTOR
9691 || CONSTANT_CLASS_P (t))
9692 dependent_p = (type_dependent_expression_p (t)
9693 || value_dependent_expression_p (t));
9694 else
9695 {
9696 gcc_assert (t == error_mark_node);
9697 dependent_p = false;
9698 }
9699
9700 processing_template_decl = saved_processing_template_decl;
9701
9702 return dependent_p;
9703 }
9704
9705 /* Returns true iff current_function_decl is an incompletely instantiated
9706 template. Useful instead of processing_template_decl because the latter
9707 is set to 0 during instantiate_non_dependent_expr. */
9708
9709 bool
9710 in_template_function (void)
9711 {
9712 tree fn = current_function_decl;
9713 bool ret;
9714 ++processing_template_decl;
9715 ret = (fn && DECL_LANG_SPECIFIC (fn)
9716 && DECL_TEMPLATE_INFO (fn)
9717 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9718 --processing_template_decl;
9719 return ret;
9720 }
9721
9722 /* Returns true if T depends on any template parameter with level LEVEL. */
9723
9724 bool
9725 uses_template_parms_level (tree t, int level)
9726 {
9727 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9728 /*include_nondeduced_p=*/true);
9729 }
9730
9731 /* Returns true if the signature of DECL depends on any template parameter from
9732 its enclosing class. */
9733
9734 bool
9735 uses_outer_template_parms (tree decl)
9736 {
9737 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9738 if (depth == 0)
9739 return false;
9740 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9741 &depth, NULL, /*include_nondeduced_p=*/true))
9742 return true;
9743 if (PRIMARY_TEMPLATE_P (decl)
9744 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9745 (DECL_TEMPLATE_PARMS (decl)),
9746 template_parm_outer_level,
9747 &depth, NULL, /*include_nondeduced_p=*/true))
9748 return true;
9749 tree ci = get_constraints (decl);
9750 if (ci)
9751 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9752 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9753 &depth, NULL, /*nondeduced*/true))
9754 return true;
9755 return false;
9756 }
9757
9758 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9759 ill-formed translation unit, i.e. a variable or function that isn't
9760 usable in a constant expression. */
9761
9762 static inline bool
9763 neglectable_inst_p (tree d)
9764 {
9765 return (DECL_P (d)
9766 && !undeduced_auto_decl (d)
9767 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9768 : decl_maybe_constant_var_p (d)));
9769 }
9770
9771 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9772 neglectable and instantiated from within an erroneous instantiation. */
9773
9774 static bool
9775 limit_bad_template_recursion (tree decl)
9776 {
9777 struct tinst_level *lev = current_tinst_level;
9778 int errs = errorcount + sorrycount;
9779 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9780 return false;
9781
9782 for (; lev; lev = lev->next)
9783 if (neglectable_inst_p (lev->decl))
9784 break;
9785
9786 return (lev && errs > lev->errors);
9787 }
9788
9789 static int tinst_depth;
9790 extern int max_tinst_depth;
9791 int depth_reached;
9792
9793 static GTY(()) struct tinst_level *last_error_tinst_level;
9794
9795 /* We're starting to instantiate D; record the template instantiation context
9796 for diagnostics and to restore it later. */
9797
9798 bool
9799 push_tinst_level (tree d)
9800 {
9801 return push_tinst_level_loc (d, input_location);
9802 }
9803
9804 /* We're starting to instantiate D; record the template instantiation context
9805 at LOC for diagnostics and to restore it later. */
9806
9807 bool
9808 push_tinst_level_loc (tree d, location_t loc)
9809 {
9810 struct tinst_level *new_level;
9811
9812 if (tinst_depth >= max_tinst_depth)
9813 {
9814 /* Tell error.c not to try to instantiate any templates. */
9815 at_eof = 2;
9816 fatal_error (input_location,
9817 "template instantiation depth exceeds maximum of %d"
9818 " (use -ftemplate-depth= to increase the maximum)",
9819 max_tinst_depth);
9820 return false;
9821 }
9822
9823 /* If the current instantiation caused problems, don't let it instantiate
9824 anything else. Do allow deduction substitution and decls usable in
9825 constant expressions. */
9826 if (limit_bad_template_recursion (d))
9827 return false;
9828
9829 /* When not -quiet, dump template instantiations other than functions, since
9830 announce_function will take care of those. */
9831 if (!quiet_flag
9832 && TREE_CODE (d) != TREE_LIST
9833 && TREE_CODE (d) != FUNCTION_DECL)
9834 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9835
9836 new_level = ggc_alloc<tinst_level> ();
9837 new_level->decl = d;
9838 new_level->locus = loc;
9839 new_level->errors = errorcount+sorrycount;
9840 new_level->in_system_header_p = in_system_header_at (input_location);
9841 new_level->next = current_tinst_level;
9842 current_tinst_level = new_level;
9843
9844 ++tinst_depth;
9845 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9846 depth_reached = tinst_depth;
9847
9848 return true;
9849 }
9850
9851 /* We're done instantiating this template; return to the instantiation
9852 context. */
9853
9854 void
9855 pop_tinst_level (void)
9856 {
9857 /* Restore the filename and line number stashed away when we started
9858 this instantiation. */
9859 input_location = current_tinst_level->locus;
9860 current_tinst_level = current_tinst_level->next;
9861 --tinst_depth;
9862 }
9863
9864 /* We're instantiating a deferred template; restore the template
9865 instantiation context in which the instantiation was requested, which
9866 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9867
9868 static tree
9869 reopen_tinst_level (struct tinst_level *level)
9870 {
9871 struct tinst_level *t;
9872
9873 tinst_depth = 0;
9874 for (t = level; t; t = t->next)
9875 ++tinst_depth;
9876
9877 current_tinst_level = level;
9878 pop_tinst_level ();
9879 if (current_tinst_level)
9880 current_tinst_level->errors = errorcount+sorrycount;
9881 return level->decl;
9882 }
9883
9884 /* Returns the TINST_LEVEL which gives the original instantiation
9885 context. */
9886
9887 struct tinst_level *
9888 outermost_tinst_level (void)
9889 {
9890 struct tinst_level *level = current_tinst_level;
9891 if (level)
9892 while (level->next)
9893 level = level->next;
9894 return level;
9895 }
9896
9897 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9898 vector of template arguments, as for tsubst.
9899
9900 Returns an appropriate tsubst'd friend declaration. */
9901
9902 static tree
9903 tsubst_friend_function (tree decl, tree args)
9904 {
9905 tree new_friend;
9906
9907 if (TREE_CODE (decl) == FUNCTION_DECL
9908 && DECL_TEMPLATE_INSTANTIATION (decl)
9909 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9910 /* This was a friend declared with an explicit template
9911 argument list, e.g.:
9912
9913 friend void f<>(T);
9914
9915 to indicate that f was a template instantiation, not a new
9916 function declaration. Now, we have to figure out what
9917 instantiation of what template. */
9918 {
9919 tree template_id, arglist, fns;
9920 tree new_args;
9921 tree tmpl;
9922 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9923
9924 /* Friend functions are looked up in the containing namespace scope.
9925 We must enter that scope, to avoid finding member functions of the
9926 current class with same name. */
9927 push_nested_namespace (ns);
9928 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9929 tf_warning_or_error, NULL_TREE,
9930 /*integral_constant_expression_p=*/false);
9931 pop_nested_namespace (ns);
9932 arglist = tsubst (DECL_TI_ARGS (decl), args,
9933 tf_warning_or_error, NULL_TREE);
9934 template_id = lookup_template_function (fns, arglist);
9935
9936 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9937 tmpl = determine_specialization (template_id, new_friend,
9938 &new_args,
9939 /*need_member_template=*/0,
9940 TREE_VEC_LENGTH (args),
9941 tsk_none);
9942 return instantiate_template (tmpl, new_args, tf_error);
9943 }
9944
9945 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9946
9947 /* The NEW_FRIEND will look like an instantiation, to the
9948 compiler, but is not an instantiation from the point of view of
9949 the language. For example, we might have had:
9950
9951 template <class T> struct S {
9952 template <class U> friend void f(T, U);
9953 };
9954
9955 Then, in S<int>, template <class U> void f(int, U) is not an
9956 instantiation of anything. */
9957 if (new_friend == error_mark_node)
9958 return error_mark_node;
9959
9960 DECL_USE_TEMPLATE (new_friend) = 0;
9961 if (TREE_CODE (decl) == TEMPLATE_DECL)
9962 {
9963 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9964 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9965 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9966 }
9967
9968 /* The mangled name for the NEW_FRIEND is incorrect. The function
9969 is not a template instantiation and should not be mangled like
9970 one. Therefore, we forget the mangling here; we'll recompute it
9971 later if we need it. */
9972 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9973 {
9974 SET_DECL_RTL (new_friend, NULL);
9975 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9976 }
9977
9978 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9979 {
9980 tree old_decl;
9981 tree new_friend_template_info;
9982 tree new_friend_result_template_info;
9983 tree ns;
9984 int new_friend_is_defn;
9985
9986 /* We must save some information from NEW_FRIEND before calling
9987 duplicate decls since that function will free NEW_FRIEND if
9988 possible. */
9989 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9990 new_friend_is_defn =
9991 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9992 (template_for_substitution (new_friend)))
9993 != NULL_TREE);
9994 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9995 {
9996 /* This declaration is a `primary' template. */
9997 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9998
9999 new_friend_result_template_info
10000 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10001 }
10002 else
10003 new_friend_result_template_info = NULL_TREE;
10004
10005 /* Inside pushdecl_namespace_level, we will push into the
10006 current namespace. However, the friend function should go
10007 into the namespace of the template. */
10008 ns = decl_namespace_context (new_friend);
10009 push_nested_namespace (ns);
10010 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10011 pop_nested_namespace (ns);
10012
10013 if (old_decl == error_mark_node)
10014 return error_mark_node;
10015
10016 if (old_decl != new_friend)
10017 {
10018 /* This new friend declaration matched an existing
10019 declaration. For example, given:
10020
10021 template <class T> void f(T);
10022 template <class U> class C {
10023 template <class T> friend void f(T) {}
10024 };
10025
10026 the friend declaration actually provides the definition
10027 of `f', once C has been instantiated for some type. So,
10028 old_decl will be the out-of-class template declaration,
10029 while new_friend is the in-class definition.
10030
10031 But, if `f' was called before this point, the
10032 instantiation of `f' will have DECL_TI_ARGS corresponding
10033 to `T' but not to `U', references to which might appear
10034 in the definition of `f'. Previously, the most general
10035 template for an instantiation of `f' was the out-of-class
10036 version; now it is the in-class version. Therefore, we
10037 run through all specialization of `f', adding to their
10038 DECL_TI_ARGS appropriately. In particular, they need a
10039 new set of outer arguments, corresponding to the
10040 arguments for this class instantiation.
10041
10042 The same situation can arise with something like this:
10043
10044 friend void f(int);
10045 template <class T> class C {
10046 friend void f(T) {}
10047 };
10048
10049 when `C<int>' is instantiated. Now, `f(int)' is defined
10050 in the class. */
10051
10052 if (!new_friend_is_defn)
10053 /* On the other hand, if the in-class declaration does
10054 *not* provide a definition, then we don't want to alter
10055 existing definitions. We can just leave everything
10056 alone. */
10057 ;
10058 else
10059 {
10060 tree new_template = TI_TEMPLATE (new_friend_template_info);
10061 tree new_args = TI_ARGS (new_friend_template_info);
10062
10063 /* Overwrite whatever template info was there before, if
10064 any, with the new template information pertaining to
10065 the declaration. */
10066 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10067
10068 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10069 {
10070 /* We should have called reregister_specialization in
10071 duplicate_decls. */
10072 gcc_assert (retrieve_specialization (new_template,
10073 new_args, 0)
10074 == old_decl);
10075
10076 /* Instantiate it if the global has already been used. */
10077 if (DECL_ODR_USED (old_decl))
10078 instantiate_decl (old_decl, /*defer_ok=*/true,
10079 /*expl_inst_class_mem_p=*/false);
10080 }
10081 else
10082 {
10083 tree t;
10084
10085 /* Indicate that the old function template is a partial
10086 instantiation. */
10087 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10088 = new_friend_result_template_info;
10089
10090 gcc_assert (new_template
10091 == most_general_template (new_template));
10092 gcc_assert (new_template != old_decl);
10093
10094 /* Reassign any specializations already in the hash table
10095 to the new more general template, and add the
10096 additional template args. */
10097 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10098 t != NULL_TREE;
10099 t = TREE_CHAIN (t))
10100 {
10101 tree spec = TREE_VALUE (t);
10102 spec_entry elt;
10103
10104 elt.tmpl = old_decl;
10105 elt.args = DECL_TI_ARGS (spec);
10106 elt.spec = NULL_TREE;
10107
10108 decl_specializations->remove_elt (&elt);
10109
10110 DECL_TI_ARGS (spec)
10111 = add_outermost_template_args (new_args,
10112 DECL_TI_ARGS (spec));
10113
10114 register_specialization
10115 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10116
10117 }
10118 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10119 }
10120 }
10121
10122 /* The information from NEW_FRIEND has been merged into OLD_DECL
10123 by duplicate_decls. */
10124 new_friend = old_decl;
10125 }
10126 }
10127 else
10128 {
10129 tree context = DECL_CONTEXT (new_friend);
10130 bool dependent_p;
10131
10132 /* In the code
10133 template <class T> class C {
10134 template <class U> friend void C1<U>::f (); // case 1
10135 friend void C2<T>::f (); // case 2
10136 };
10137 we only need to make sure CONTEXT is a complete type for
10138 case 2. To distinguish between the two cases, we note that
10139 CONTEXT of case 1 remains dependent type after tsubst while
10140 this isn't true for case 2. */
10141 ++processing_template_decl;
10142 dependent_p = dependent_type_p (context);
10143 --processing_template_decl;
10144
10145 if (!dependent_p
10146 && !complete_type_or_else (context, NULL_TREE))
10147 return error_mark_node;
10148
10149 if (COMPLETE_TYPE_P (context))
10150 {
10151 tree fn = new_friend;
10152 /* do_friend adds the TEMPLATE_DECL for any member friend
10153 template even if it isn't a member template, i.e.
10154 template <class T> friend A<T>::f();
10155 Look through it in that case. */
10156 if (TREE_CODE (fn) == TEMPLATE_DECL
10157 && !PRIMARY_TEMPLATE_P (fn))
10158 fn = DECL_TEMPLATE_RESULT (fn);
10159 /* Check to see that the declaration is really present, and,
10160 possibly obtain an improved declaration. */
10161 fn = check_classfn (context, fn, NULL_TREE);
10162
10163 if (fn)
10164 new_friend = fn;
10165 }
10166 }
10167
10168 return new_friend;
10169 }
10170
10171 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10172 template arguments, as for tsubst.
10173
10174 Returns an appropriate tsubst'd friend type or error_mark_node on
10175 failure. */
10176
10177 static tree
10178 tsubst_friend_class (tree friend_tmpl, tree args)
10179 {
10180 tree tmpl;
10181
10182 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10183 {
10184 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10185 return TREE_TYPE (tmpl);
10186 }
10187
10188 tree context = CP_DECL_CONTEXT (friend_tmpl);
10189 if (TREE_CODE (context) == NAMESPACE_DECL)
10190 push_nested_namespace (context);
10191 else
10192 push_nested_class (context);
10193
10194 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10195 /*non_class=*/false, /*block_p=*/false,
10196 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10197
10198 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10199 {
10200 /* The friend template has already been declared. Just
10201 check to see that the declarations match, and install any new
10202 default parameters. We must tsubst the default parameters,
10203 of course. We only need the innermost template parameters
10204 because that is all that redeclare_class_template will look
10205 at. */
10206 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10207 > TMPL_ARGS_DEPTH (args))
10208 {
10209 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10210 args, tf_warning_or_error);
10211 location_t saved_input_location = input_location;
10212 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10213 tree cons = get_constraints (tmpl);
10214 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10215 input_location = saved_input_location;
10216 }
10217 }
10218 else
10219 {
10220 /* The friend template has not already been declared. In this
10221 case, the instantiation of the template class will cause the
10222 injection of this template into the namespace scope. */
10223 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10224
10225 if (tmpl != error_mark_node)
10226 {
10227 /* The new TMPL is not an instantiation of anything, so we
10228 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10229 for the new type because that is supposed to be the
10230 corresponding template decl, i.e., TMPL. */
10231 DECL_USE_TEMPLATE (tmpl) = 0;
10232 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10233 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10234 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10235 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10236
10237 /* It is hidden. */
10238 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10239 DECL_ANTICIPATED (tmpl)
10240 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10241
10242 /* Inject this template into the enclosing namspace scope. */
10243 tmpl = pushdecl_namespace_level (tmpl, true);
10244 }
10245 }
10246
10247 if (TREE_CODE (context) == NAMESPACE_DECL)
10248 pop_nested_namespace (context);
10249 else
10250 pop_nested_class ();
10251
10252 return TREE_TYPE (tmpl);
10253 }
10254
10255 /* Returns zero if TYPE cannot be completed later due to circularity.
10256 Otherwise returns one. */
10257
10258 static int
10259 can_complete_type_without_circularity (tree type)
10260 {
10261 if (type == NULL_TREE || type == error_mark_node)
10262 return 0;
10263 else if (COMPLETE_TYPE_P (type))
10264 return 1;
10265 else if (TREE_CODE (type) == ARRAY_TYPE)
10266 return can_complete_type_without_circularity (TREE_TYPE (type));
10267 else if (CLASS_TYPE_P (type)
10268 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10269 return 0;
10270 else
10271 return 1;
10272 }
10273
10274 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10275 tsubst_flags_t, tree);
10276
10277 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10278 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10279
10280 static tree
10281 tsubst_attribute (tree t, tree *decl_p, tree args,
10282 tsubst_flags_t complain, tree in_decl)
10283 {
10284 gcc_assert (ATTR_IS_DEPENDENT (t));
10285
10286 tree val = TREE_VALUE (t);
10287 if (val == NULL_TREE)
10288 /* Nothing to do. */;
10289 else if ((flag_openmp || flag_openmp_simd)
10290 && is_attribute_p ("omp declare simd",
10291 get_attribute_name (t)))
10292 {
10293 tree clauses = TREE_VALUE (val);
10294 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10295 complain, in_decl);
10296 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10297 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10298 tree parms = DECL_ARGUMENTS (*decl_p);
10299 clauses
10300 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10301 if (clauses)
10302 val = build_tree_list (NULL_TREE, clauses);
10303 else
10304 val = NULL_TREE;
10305 }
10306 /* If the first attribute argument is an identifier, don't
10307 pass it through tsubst. Attributes like mode, format,
10308 cleanup and several target specific attributes expect it
10309 unmodified. */
10310 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10311 {
10312 tree chain
10313 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10314 /*integral_constant_expression_p=*/false);
10315 if (chain != TREE_CHAIN (val))
10316 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10317 }
10318 else if (PACK_EXPANSION_P (val))
10319 {
10320 /* An attribute pack expansion. */
10321 tree purp = TREE_PURPOSE (t);
10322 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10323 if (pack == error_mark_node)
10324 return error_mark_node;
10325 int len = TREE_VEC_LENGTH (pack);
10326 tree list = NULL_TREE;
10327 tree *q = &list;
10328 for (int i = 0; i < len; ++i)
10329 {
10330 tree elt = TREE_VEC_ELT (pack, i);
10331 *q = build_tree_list (purp, elt);
10332 q = &TREE_CHAIN (*q);
10333 }
10334 return list;
10335 }
10336 else
10337 val = tsubst_expr (val, args, complain, in_decl,
10338 /*integral_constant_expression_p=*/false);
10339
10340 if (val != TREE_VALUE (t))
10341 return build_tree_list (TREE_PURPOSE (t), val);
10342 return t;
10343 }
10344
10345 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10346 unchanged or a new TREE_LIST chain. */
10347
10348 static tree
10349 tsubst_attributes (tree attributes, tree args,
10350 tsubst_flags_t complain, tree in_decl)
10351 {
10352 tree last_dep = NULL_TREE;
10353
10354 for (tree t = attributes; t; t = TREE_CHAIN (t))
10355 if (ATTR_IS_DEPENDENT (t))
10356 {
10357 last_dep = t;
10358 attributes = copy_list (attributes);
10359 break;
10360 }
10361
10362 if (last_dep)
10363 for (tree *p = &attributes; *p; )
10364 {
10365 tree t = *p;
10366 if (ATTR_IS_DEPENDENT (t))
10367 {
10368 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10369 if (subst != t)
10370 {
10371 *p = subst;
10372 while (*p)
10373 p = &TREE_CHAIN (*p);
10374 *p = TREE_CHAIN (t);
10375 continue;
10376 }
10377 }
10378 p = &TREE_CHAIN (*p);
10379 }
10380
10381 return attributes;
10382 }
10383
10384 /* Apply any attributes which had to be deferred until instantiation
10385 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10386 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10387
10388 static void
10389 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10390 tree args, tsubst_flags_t complain, tree in_decl)
10391 {
10392 tree last_dep = NULL_TREE;
10393 tree t;
10394 tree *p;
10395
10396 if (attributes == NULL_TREE)
10397 return;
10398
10399 if (DECL_P (*decl_p))
10400 {
10401 if (TREE_TYPE (*decl_p) == error_mark_node)
10402 return;
10403 p = &DECL_ATTRIBUTES (*decl_p);
10404 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10405 to our attributes parameter. */
10406 gcc_assert (*p == attributes);
10407 }
10408 else
10409 {
10410 p = &TYPE_ATTRIBUTES (*decl_p);
10411 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10412 lookup_template_class_1, and should be preserved. */
10413 gcc_assert (*p != attributes);
10414 while (*p)
10415 p = &TREE_CHAIN (*p);
10416 }
10417
10418 for (t = attributes; t; t = TREE_CHAIN (t))
10419 if (ATTR_IS_DEPENDENT (t))
10420 {
10421 last_dep = t;
10422 attributes = copy_list (attributes);
10423 break;
10424 }
10425
10426 *p = attributes;
10427 if (last_dep)
10428 {
10429 tree late_attrs = NULL_TREE;
10430 tree *q = &late_attrs;
10431
10432 for (; *p; )
10433 {
10434 t = *p;
10435 if (ATTR_IS_DEPENDENT (t))
10436 {
10437 *p = TREE_CHAIN (t);
10438 TREE_CHAIN (t) = NULL_TREE;
10439 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10440 while (*q)
10441 q = &TREE_CHAIN (*q);
10442 }
10443 else
10444 p = &TREE_CHAIN (t);
10445 }
10446
10447 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10448 }
10449 }
10450
10451 /* Perform (or defer) access check for typedefs that were referenced
10452 from within the template TMPL code.
10453 This is a subroutine of instantiate_decl and instantiate_class_template.
10454 TMPL is the template to consider and TARGS is the list of arguments of
10455 that template. */
10456
10457 static void
10458 perform_typedefs_access_check (tree tmpl, tree targs)
10459 {
10460 location_t saved_location;
10461 unsigned i;
10462 qualified_typedef_usage_t *iter;
10463
10464 if (!tmpl
10465 || (!CLASS_TYPE_P (tmpl)
10466 && TREE_CODE (tmpl) != FUNCTION_DECL))
10467 return;
10468
10469 saved_location = input_location;
10470 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10471 {
10472 tree type_decl = iter->typedef_decl;
10473 tree type_scope = iter->context;
10474
10475 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10476 continue;
10477
10478 if (uses_template_parms (type_decl))
10479 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10480 if (uses_template_parms (type_scope))
10481 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10482
10483 /* Make access check error messages point to the location
10484 of the use of the typedef. */
10485 input_location = iter->locus;
10486 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10487 type_decl, type_decl,
10488 tf_warning_or_error);
10489 }
10490 input_location = saved_location;
10491 }
10492
10493 static tree
10494 instantiate_class_template_1 (tree type)
10495 {
10496 tree templ, args, pattern, t, member;
10497 tree typedecl;
10498 tree pbinfo;
10499 tree base_list;
10500 unsigned int saved_maximum_field_alignment;
10501 tree fn_context;
10502
10503 if (type == error_mark_node)
10504 return error_mark_node;
10505
10506 if (COMPLETE_OR_OPEN_TYPE_P (type)
10507 || uses_template_parms (type))
10508 return type;
10509
10510 /* Figure out which template is being instantiated. */
10511 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10512 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10513
10514 /* Mark the type as in the process of being defined. */
10515 TYPE_BEING_DEFINED (type) = 1;
10516
10517 /* Determine what specialization of the original template to
10518 instantiate. */
10519 t = most_specialized_partial_spec (type, tf_warning_or_error);
10520 if (t == error_mark_node)
10521 return error_mark_node;
10522 else if (t)
10523 {
10524 /* This TYPE is actually an instantiation of a partial
10525 specialization. We replace the innermost set of ARGS with
10526 the arguments appropriate for substitution. For example,
10527 given:
10528
10529 template <class T> struct S {};
10530 template <class T> struct S<T*> {};
10531
10532 and supposing that we are instantiating S<int*>, ARGS will
10533 presently be {int*} -- but we need {int}. */
10534 pattern = TREE_TYPE (t);
10535 args = TREE_PURPOSE (t);
10536 }
10537 else
10538 {
10539 pattern = TREE_TYPE (templ);
10540 args = CLASSTYPE_TI_ARGS (type);
10541 }
10542
10543 /* If the template we're instantiating is incomplete, then clearly
10544 there's nothing we can do. */
10545 if (!COMPLETE_TYPE_P (pattern))
10546 {
10547 /* We can try again later. */
10548 TYPE_BEING_DEFINED (type) = 0;
10549 return type;
10550 }
10551
10552 /* If we've recursively instantiated too many templates, stop. */
10553 if (! push_tinst_level (type))
10554 return type;
10555
10556 /* We may be in the middle of deferred access check. Disable
10557 it now. */
10558 push_deferring_access_checks (dk_no_deferred);
10559
10560 int saved_unevaluated_operand = cp_unevaluated_operand;
10561 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10562
10563 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10564 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10565 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10566 fn_context = error_mark_node;
10567 if (!fn_context)
10568 push_to_top_level ();
10569 else
10570 {
10571 cp_unevaluated_operand = 0;
10572 c_inhibit_evaluation_warnings = 0;
10573 }
10574 /* Use #pragma pack from the template context. */
10575 saved_maximum_field_alignment = maximum_field_alignment;
10576 maximum_field_alignment = TYPE_PRECISION (pattern);
10577
10578 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10579
10580 /* Set the input location to the most specialized template definition.
10581 This is needed if tsubsting causes an error. */
10582 typedecl = TYPE_MAIN_DECL (pattern);
10583 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10584 DECL_SOURCE_LOCATION (typedecl);
10585
10586 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10587 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10588 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10589 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10590 if (ANON_AGGR_TYPE_P (pattern))
10591 SET_ANON_AGGR_TYPE_P (type);
10592 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10593 {
10594 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10595 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10596 /* Adjust visibility for template arguments. */
10597 determine_visibility (TYPE_MAIN_DECL (type));
10598 }
10599 if (CLASS_TYPE_P (type))
10600 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10601
10602 pbinfo = TYPE_BINFO (pattern);
10603
10604 /* We should never instantiate a nested class before its enclosing
10605 class; we need to look up the nested class by name before we can
10606 instantiate it, and that lookup should instantiate the enclosing
10607 class. */
10608 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10609 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10610
10611 base_list = NULL_TREE;
10612 if (BINFO_N_BASE_BINFOS (pbinfo))
10613 {
10614 tree pbase_binfo;
10615 tree pushed_scope;
10616 int i;
10617
10618 /* We must enter the scope containing the type, as that is where
10619 the accessibility of types named in dependent bases are
10620 looked up from. */
10621 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10622
10623 /* Substitute into each of the bases to determine the actual
10624 basetypes. */
10625 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10626 {
10627 tree base;
10628 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10629 tree expanded_bases = NULL_TREE;
10630 int idx, len = 1;
10631
10632 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10633 {
10634 expanded_bases =
10635 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10636 args, tf_error, NULL_TREE);
10637 if (expanded_bases == error_mark_node)
10638 continue;
10639
10640 len = TREE_VEC_LENGTH (expanded_bases);
10641 }
10642
10643 for (idx = 0; idx < len; idx++)
10644 {
10645 if (expanded_bases)
10646 /* Extract the already-expanded base class. */
10647 base = TREE_VEC_ELT (expanded_bases, idx);
10648 else
10649 /* Substitute to figure out the base class. */
10650 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10651 NULL_TREE);
10652
10653 if (base == error_mark_node)
10654 continue;
10655
10656 base_list = tree_cons (access, base, base_list);
10657 if (BINFO_VIRTUAL_P (pbase_binfo))
10658 TREE_TYPE (base_list) = integer_type_node;
10659 }
10660 }
10661
10662 /* The list is now in reverse order; correct that. */
10663 base_list = nreverse (base_list);
10664
10665 if (pushed_scope)
10666 pop_scope (pushed_scope);
10667 }
10668 /* Now call xref_basetypes to set up all the base-class
10669 information. */
10670 xref_basetypes (type, base_list);
10671
10672 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10673 (int) ATTR_FLAG_TYPE_IN_PLACE,
10674 args, tf_error, NULL_TREE);
10675 fixup_attribute_variants (type);
10676
10677 /* Now that our base classes are set up, enter the scope of the
10678 class, so that name lookups into base classes, etc. will work
10679 correctly. This is precisely analogous to what we do in
10680 begin_class_definition when defining an ordinary non-template
10681 class, except we also need to push the enclosing classes. */
10682 push_nested_class (type);
10683
10684 /* Now members are processed in the order of declaration. */
10685 for (member = CLASSTYPE_DECL_LIST (pattern);
10686 member; member = TREE_CHAIN (member))
10687 {
10688 tree t = TREE_VALUE (member);
10689
10690 if (TREE_PURPOSE (member))
10691 {
10692 if (TYPE_P (t))
10693 {
10694 if (LAMBDA_TYPE_P (t))
10695 /* A closure type for a lambda in an NSDMI or default argument.
10696 Ignore it; it will be regenerated when needed. */
10697 continue;
10698
10699 /* Build new CLASSTYPE_NESTED_UTDS. */
10700
10701 tree newtag;
10702 bool class_template_p;
10703
10704 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10705 && TYPE_LANG_SPECIFIC (t)
10706 && CLASSTYPE_IS_TEMPLATE (t));
10707 /* If the member is a class template, then -- even after
10708 substitution -- there may be dependent types in the
10709 template argument list for the class. We increment
10710 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10711 that function will assume that no types are dependent
10712 when outside of a template. */
10713 if (class_template_p)
10714 ++processing_template_decl;
10715 newtag = tsubst (t, args, tf_error, NULL_TREE);
10716 if (class_template_p)
10717 --processing_template_decl;
10718 if (newtag == error_mark_node)
10719 continue;
10720
10721 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10722 {
10723 tree name = TYPE_IDENTIFIER (t);
10724
10725 if (class_template_p)
10726 /* Unfortunately, lookup_template_class sets
10727 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10728 instantiation (i.e., for the type of a member
10729 template class nested within a template class.)
10730 This behavior is required for
10731 maybe_process_partial_specialization to work
10732 correctly, but is not accurate in this case;
10733 the TAG is not an instantiation of anything.
10734 (The corresponding TEMPLATE_DECL is an
10735 instantiation, but the TYPE is not.) */
10736 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10737
10738 /* Now, we call pushtag to put this NEWTAG into the scope of
10739 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10740 pushtag calling push_template_decl. We don't have to do
10741 this for enums because it will already have been done in
10742 tsubst_enum. */
10743 if (name)
10744 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10745 pushtag (name, newtag, /*tag_scope=*/ts_current);
10746 }
10747 }
10748 else if (DECL_DECLARES_FUNCTION_P (t))
10749 {
10750 tree r;
10751
10752 if (TREE_CODE (t) == TEMPLATE_DECL)
10753 ++processing_template_decl;
10754 r = tsubst (t, args, tf_error, NULL_TREE);
10755 if (TREE_CODE (t) == TEMPLATE_DECL)
10756 --processing_template_decl;
10757 set_current_access_from_decl (r);
10758 finish_member_declaration (r);
10759 /* Instantiate members marked with attribute used. */
10760 if (r != error_mark_node && DECL_PRESERVE_P (r))
10761 mark_used (r);
10762 if (TREE_CODE (r) == FUNCTION_DECL
10763 && DECL_OMP_DECLARE_REDUCTION_P (r))
10764 cp_check_omp_declare_reduction (r);
10765 }
10766 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
10767 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10768 /* A closure type for a lambda in an NSDMI or default argument.
10769 Ignore it; it will be regenerated when needed. */;
10770 else
10771 {
10772 /* Build new TYPE_FIELDS. */
10773 if (TREE_CODE (t) == STATIC_ASSERT)
10774 {
10775 tree condition;
10776
10777 ++c_inhibit_evaluation_warnings;
10778 condition =
10779 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10780 tf_warning_or_error, NULL_TREE,
10781 /*integral_constant_expression_p=*/true);
10782 --c_inhibit_evaluation_warnings;
10783
10784 finish_static_assert (condition,
10785 STATIC_ASSERT_MESSAGE (t),
10786 STATIC_ASSERT_SOURCE_LOCATION (t),
10787 /*member_p=*/true);
10788 }
10789 else if (TREE_CODE (t) != CONST_DECL)
10790 {
10791 tree r;
10792 tree vec = NULL_TREE;
10793 int len = 1;
10794
10795 /* The file and line for this declaration, to
10796 assist in error message reporting. Since we
10797 called push_tinst_level above, we don't need to
10798 restore these. */
10799 input_location = DECL_SOURCE_LOCATION (t);
10800
10801 if (TREE_CODE (t) == TEMPLATE_DECL)
10802 ++processing_template_decl;
10803 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10804 if (TREE_CODE (t) == TEMPLATE_DECL)
10805 --processing_template_decl;
10806
10807 if (TREE_CODE (r) == TREE_VEC)
10808 {
10809 /* A capture pack became multiple fields. */
10810 vec = r;
10811 len = TREE_VEC_LENGTH (vec);
10812 }
10813
10814 for (int i = 0; i < len; ++i)
10815 {
10816 if (vec)
10817 r = TREE_VEC_ELT (vec, i);
10818 if (VAR_P (r))
10819 {
10820 /* In [temp.inst]:
10821
10822 [t]he initialization (and any associated
10823 side-effects) of a static data member does
10824 not occur unless the static data member is
10825 itself used in a way that requires the
10826 definition of the static data member to
10827 exist.
10828
10829 Therefore, we do not substitute into the
10830 initialized for the static data member here. */
10831 finish_static_data_member_decl
10832 (r,
10833 /*init=*/NULL_TREE,
10834 /*init_const_expr_p=*/false,
10835 /*asmspec_tree=*/NULL_TREE,
10836 /*flags=*/0);
10837 /* Instantiate members marked with attribute used. */
10838 if (r != error_mark_node && DECL_PRESERVE_P (r))
10839 mark_used (r);
10840 }
10841 else if (TREE_CODE (r) == FIELD_DECL)
10842 {
10843 /* Determine whether R has a valid type and can be
10844 completed later. If R is invalid, then its type
10845 is replaced by error_mark_node. */
10846 tree rtype = TREE_TYPE (r);
10847 if (can_complete_type_without_circularity (rtype))
10848 complete_type (rtype);
10849
10850 if (!complete_or_array_type_p (rtype))
10851 {
10852 /* If R's type couldn't be completed and
10853 it isn't a flexible array member (whose
10854 type is incomplete by definition) give
10855 an error. */
10856 cxx_incomplete_type_error (r, rtype);
10857 TREE_TYPE (r) = error_mark_node;
10858 }
10859 }
10860
10861 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10862 such a thing will already have been added to the field
10863 list by tsubst_enum in finish_member_declaration in the
10864 CLASSTYPE_NESTED_UTDS case above. */
10865 if (!(TREE_CODE (r) == TYPE_DECL
10866 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10867 && DECL_ARTIFICIAL (r)))
10868 {
10869 set_current_access_from_decl (r);
10870 finish_member_declaration (r);
10871 }
10872 }
10873 }
10874 }
10875 }
10876 else
10877 {
10878 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10879 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10880 {
10881 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10882
10883 tree friend_type = t;
10884 bool adjust_processing_template_decl = false;
10885
10886 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10887 {
10888 /* template <class T> friend class C; */
10889 friend_type = tsubst_friend_class (friend_type, args);
10890 adjust_processing_template_decl = true;
10891 }
10892 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10893 {
10894 /* template <class T> friend class C::D; */
10895 friend_type = tsubst (friend_type, args,
10896 tf_warning_or_error, NULL_TREE);
10897 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10898 friend_type = TREE_TYPE (friend_type);
10899 adjust_processing_template_decl = true;
10900 }
10901 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10902 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10903 {
10904 /* This could be either
10905
10906 friend class T::C;
10907
10908 when dependent_type_p is false or
10909
10910 template <class U> friend class T::C;
10911
10912 otherwise. */
10913 /* Bump processing_template_decl in case this is something like
10914 template <class T> friend struct A<T>::B. */
10915 ++processing_template_decl;
10916 friend_type = tsubst (friend_type, args,
10917 tf_warning_or_error, NULL_TREE);
10918 if (dependent_type_p (friend_type))
10919 adjust_processing_template_decl = true;
10920 --processing_template_decl;
10921 }
10922 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10923 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10924 && TYPE_HIDDEN_P (friend_type))
10925 {
10926 /* friend class C;
10927
10928 where C hasn't been declared yet. Let's lookup name
10929 from namespace scope directly, bypassing any name that
10930 come from dependent base class. */
10931 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10932
10933 /* The call to xref_tag_from_type does injection for friend
10934 classes. */
10935 push_nested_namespace (ns);
10936 friend_type =
10937 xref_tag_from_type (friend_type, NULL_TREE,
10938 /*tag_scope=*/ts_current);
10939 pop_nested_namespace (ns);
10940 }
10941 else if (uses_template_parms (friend_type))
10942 /* friend class C<T>; */
10943 friend_type = tsubst (friend_type, args,
10944 tf_warning_or_error, NULL_TREE);
10945 /* Otherwise it's
10946
10947 friend class C;
10948
10949 where C is already declared or
10950
10951 friend class C<int>;
10952
10953 We don't have to do anything in these cases. */
10954
10955 if (adjust_processing_template_decl)
10956 /* Trick make_friend_class into realizing that the friend
10957 we're adding is a template, not an ordinary class. It's
10958 important that we use make_friend_class since it will
10959 perform some error-checking and output cross-reference
10960 information. */
10961 ++processing_template_decl;
10962
10963 if (friend_type != error_mark_node)
10964 make_friend_class (type, friend_type, /*complain=*/false);
10965
10966 if (adjust_processing_template_decl)
10967 --processing_template_decl;
10968 }
10969 else
10970 {
10971 /* Build new DECL_FRIENDLIST. */
10972 tree r;
10973
10974 /* The file and line for this declaration, to
10975 assist in error message reporting. Since we
10976 called push_tinst_level above, we don't need to
10977 restore these. */
10978 input_location = DECL_SOURCE_LOCATION (t);
10979
10980 if (TREE_CODE (t) == TEMPLATE_DECL)
10981 {
10982 ++processing_template_decl;
10983 push_deferring_access_checks (dk_no_check);
10984 }
10985
10986 r = tsubst_friend_function (t, args);
10987 add_friend (type, r, /*complain=*/false);
10988 if (TREE_CODE (t) == TEMPLATE_DECL)
10989 {
10990 pop_deferring_access_checks ();
10991 --processing_template_decl;
10992 }
10993 }
10994 }
10995 }
10996
10997 if (fn_context)
10998 {
10999 /* Restore these before substituting into the lambda capture
11000 initializers. */
11001 cp_unevaluated_operand = saved_unevaluated_operand;
11002 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11003 }
11004
11005 /* Set the file and line number information to whatever is given for
11006 the class itself. This puts error messages involving generated
11007 implicit functions at a predictable point, and the same point
11008 that would be used for non-template classes. */
11009 input_location = DECL_SOURCE_LOCATION (typedecl);
11010
11011 unreverse_member_declarations (type);
11012 finish_struct_1 (type);
11013 TYPE_BEING_DEFINED (type) = 0;
11014
11015 /* We don't instantiate default arguments for member functions. 14.7.1:
11016
11017 The implicit instantiation of a class template specialization causes
11018 the implicit instantiation of the declarations, but not of the
11019 definitions or default arguments, of the class member functions,
11020 member classes, static data members and member templates.... */
11021
11022 /* Some typedefs referenced from within the template code need to be access
11023 checked at template instantiation time, i.e now. These types were
11024 added to the template at parsing time. Let's get those and perform
11025 the access checks then. */
11026 perform_typedefs_access_check (pattern, args);
11027 perform_deferred_access_checks (tf_warning_or_error);
11028 pop_nested_class ();
11029 maximum_field_alignment = saved_maximum_field_alignment;
11030 if (!fn_context)
11031 pop_from_top_level ();
11032 pop_deferring_access_checks ();
11033 pop_tinst_level ();
11034
11035 /* The vtable for a template class can be emitted in any translation
11036 unit in which the class is instantiated. When there is no key
11037 method, however, finish_struct_1 will already have added TYPE to
11038 the keyed_classes. */
11039 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11040 vec_safe_push (keyed_classes, type);
11041
11042 return type;
11043 }
11044
11045 /* Wrapper for instantiate_class_template_1. */
11046
11047 tree
11048 instantiate_class_template (tree type)
11049 {
11050 tree ret;
11051 timevar_push (TV_TEMPLATE_INST);
11052 ret = instantiate_class_template_1 (type);
11053 timevar_pop (TV_TEMPLATE_INST);
11054 return ret;
11055 }
11056
11057 static tree
11058 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11059 {
11060 tree r;
11061
11062 if (!t)
11063 r = t;
11064 else if (TYPE_P (t))
11065 r = tsubst (t, args, complain, in_decl);
11066 else
11067 {
11068 if (!(complain & tf_warning))
11069 ++c_inhibit_evaluation_warnings;
11070 r = tsubst_expr (t, args, complain, in_decl,
11071 /*integral_constant_expression_p=*/true);
11072 if (!(complain & tf_warning))
11073 --c_inhibit_evaluation_warnings;
11074 }
11075 return r;
11076 }
11077
11078 /* Given a function parameter pack TMPL_PARM and some function parameters
11079 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11080 and set *SPEC_P to point at the next point in the list. */
11081
11082 tree
11083 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11084 {
11085 /* Collect all of the extra "packed" parameters into an
11086 argument pack. */
11087 tree parmvec;
11088 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11089 tree spec_parm = *spec_p;
11090 int i, len;
11091
11092 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11093 if (tmpl_parm
11094 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11095 break;
11096
11097 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11098 parmvec = make_tree_vec (len);
11099 spec_parm = *spec_p;
11100 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11101 {
11102 tree elt = spec_parm;
11103 if (DECL_PACK_P (elt))
11104 elt = make_pack_expansion (elt);
11105 TREE_VEC_ELT (parmvec, i) = elt;
11106 }
11107
11108 /* Build the argument packs. */
11109 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11110 *spec_p = spec_parm;
11111
11112 return argpack;
11113 }
11114
11115 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11116 NONTYPE_ARGUMENT_PACK. */
11117
11118 static tree
11119 make_fnparm_pack (tree spec_parm)
11120 {
11121 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11122 }
11123
11124 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11125 pack expansion with no extra args, 2 if it has extra args, or 0
11126 if it is not a pack expansion. */
11127
11128 static int
11129 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11130 {
11131 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11132 if (i >= TREE_VEC_LENGTH (vec))
11133 return 0;
11134 tree elt = TREE_VEC_ELT (vec, i);
11135 if (DECL_P (elt))
11136 /* A decl pack is itself an expansion. */
11137 elt = TREE_TYPE (elt);
11138 if (!PACK_EXPANSION_P (elt))
11139 return 0;
11140 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11141 return 2;
11142 return 1;
11143 }
11144
11145
11146 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11147
11148 static tree
11149 make_argument_pack_select (tree arg_pack, unsigned index)
11150 {
11151 tree aps = make_node (ARGUMENT_PACK_SELECT);
11152
11153 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11154 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11155
11156 return aps;
11157 }
11158
11159 /* This is a subroutine of tsubst_pack_expansion.
11160
11161 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11162 mechanism to store the (non complete list of) arguments of the
11163 substitution and return a non substituted pack expansion, in order
11164 to wait for when we have enough arguments to really perform the
11165 substitution. */
11166
11167 static bool
11168 use_pack_expansion_extra_args_p (tree parm_packs,
11169 int arg_pack_len,
11170 bool has_empty_arg)
11171 {
11172 /* If one pack has an expansion and another pack has a normal
11173 argument or if one pack has an empty argument and an another
11174 one hasn't then tsubst_pack_expansion cannot perform the
11175 substitution and need to fall back on the
11176 PACK_EXPANSION_EXTRA mechanism. */
11177 if (parm_packs == NULL_TREE)
11178 return false;
11179 else if (has_empty_arg)
11180 return true;
11181
11182 bool has_expansion_arg = false;
11183 for (int i = 0 ; i < arg_pack_len; ++i)
11184 {
11185 bool has_non_expansion_arg = false;
11186 for (tree parm_pack = parm_packs;
11187 parm_pack;
11188 parm_pack = TREE_CHAIN (parm_pack))
11189 {
11190 tree arg = TREE_VALUE (parm_pack);
11191
11192 int exp = argument_pack_element_is_expansion_p (arg, i);
11193 if (exp == 2)
11194 /* We can't substitute a pack expansion with extra args into
11195 our pattern. */
11196 return true;
11197 else if (exp)
11198 has_expansion_arg = true;
11199 else
11200 has_non_expansion_arg = true;
11201 }
11202
11203 if (has_expansion_arg && has_non_expansion_arg)
11204 return true;
11205 }
11206 return false;
11207 }
11208
11209 /* [temp.variadic]/6 says that:
11210
11211 The instantiation of a pack expansion [...]
11212 produces a list E1,E2, ..., En, where N is the number of elements
11213 in the pack expansion parameters.
11214
11215 This subroutine of tsubst_pack_expansion produces one of these Ei.
11216
11217 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11218 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11219 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11220 INDEX is the index 'i' of the element Ei to produce. ARGS,
11221 COMPLAIN, and IN_DECL are the same parameters as for the
11222 tsubst_pack_expansion function.
11223
11224 The function returns the resulting Ei upon successful completion,
11225 or error_mark_node.
11226
11227 Note that this function possibly modifies the ARGS parameter, so
11228 it's the responsibility of the caller to restore it. */
11229
11230 static tree
11231 gen_elem_of_pack_expansion_instantiation (tree pattern,
11232 tree parm_packs,
11233 unsigned index,
11234 tree args /* This parm gets
11235 modified. */,
11236 tsubst_flags_t complain,
11237 tree in_decl)
11238 {
11239 tree t;
11240 bool ith_elem_is_expansion = false;
11241
11242 /* For each parameter pack, change the substitution of the parameter
11243 pack to the ith argument in its argument pack, then expand the
11244 pattern. */
11245 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11246 {
11247 tree parm = TREE_PURPOSE (pack);
11248 tree arg_pack = TREE_VALUE (pack);
11249 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11250
11251 ith_elem_is_expansion |=
11252 argument_pack_element_is_expansion_p (arg_pack, index);
11253
11254 /* Select the Ith argument from the pack. */
11255 if (TREE_CODE (parm) == PARM_DECL
11256 || VAR_P (parm)
11257 || TREE_CODE (parm) == FIELD_DECL)
11258 {
11259 if (index == 0)
11260 {
11261 aps = make_argument_pack_select (arg_pack, index);
11262 if (!mark_used (parm, complain) && !(complain & tf_error))
11263 return error_mark_node;
11264 register_local_specialization (aps, parm);
11265 }
11266 else
11267 aps = retrieve_local_specialization (parm);
11268 }
11269 else
11270 {
11271 int idx, level;
11272 template_parm_level_and_index (parm, &level, &idx);
11273
11274 if (index == 0)
11275 {
11276 aps = make_argument_pack_select (arg_pack, index);
11277 /* Update the corresponding argument. */
11278 TMPL_ARG (args, level, idx) = aps;
11279 }
11280 else
11281 /* Re-use the ARGUMENT_PACK_SELECT. */
11282 aps = TMPL_ARG (args, level, idx);
11283 }
11284 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11285 }
11286
11287 /* Substitute into the PATTERN with the (possibly altered)
11288 arguments. */
11289 if (pattern == in_decl)
11290 /* Expanding a fixed parameter pack from
11291 coerce_template_parameter_pack. */
11292 t = tsubst_decl (pattern, args, complain);
11293 else if (pattern == error_mark_node)
11294 t = error_mark_node;
11295 else if (constraint_p (pattern))
11296 {
11297 if (processing_template_decl)
11298 t = tsubst_constraint (pattern, args, complain, in_decl);
11299 else
11300 t = (constraints_satisfied_p (pattern, args)
11301 ? boolean_true_node : boolean_false_node);
11302 }
11303 else if (!TYPE_P (pattern))
11304 t = tsubst_expr (pattern, args, complain, in_decl,
11305 /*integral_constant_expression_p=*/false);
11306 else
11307 t = tsubst (pattern, args, complain, in_decl);
11308
11309 /* If the Ith argument pack element is a pack expansion, then
11310 the Ith element resulting from the substituting is going to
11311 be a pack expansion as well. */
11312 if (ith_elem_is_expansion)
11313 t = make_pack_expansion (t, complain);
11314
11315 return t;
11316 }
11317
11318 /* When the unexpanded parameter pack in a fold expression expands to an empty
11319 sequence, the value of the expression is as follows; the program is
11320 ill-formed if the operator is not listed in this table.
11321
11322 && true
11323 || false
11324 , void() */
11325
11326 tree
11327 expand_empty_fold (tree t, tsubst_flags_t complain)
11328 {
11329 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11330 if (!FOLD_EXPR_MODIFY_P (t))
11331 switch (code)
11332 {
11333 case TRUTH_ANDIF_EXPR:
11334 return boolean_true_node;
11335 case TRUTH_ORIF_EXPR:
11336 return boolean_false_node;
11337 case COMPOUND_EXPR:
11338 return void_node;
11339 default:
11340 break;
11341 }
11342
11343 if (complain & tf_error)
11344 error_at (location_of (t),
11345 "fold of empty expansion over %O", code);
11346 return error_mark_node;
11347 }
11348
11349 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11350 form an expression that combines the two terms using the
11351 operator of T. */
11352
11353 static tree
11354 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11355 {
11356 tree op = FOLD_EXPR_OP (t);
11357 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11358
11359 // Handle compound assignment operators.
11360 if (FOLD_EXPR_MODIFY_P (t))
11361 return build_x_modify_expr (input_location, left, code, right, complain);
11362
11363 switch (code)
11364 {
11365 case COMPOUND_EXPR:
11366 return build_x_compound_expr (input_location, left, right, complain);
11367 case DOTSTAR_EXPR:
11368 return build_m_component_ref (left, right, complain);
11369 default:
11370 return build_x_binary_op (input_location, code,
11371 left, TREE_CODE (left),
11372 right, TREE_CODE (right),
11373 /*overload=*/NULL,
11374 complain);
11375 }
11376 }
11377
11378 /* Substitute ARGS into the pack of a fold expression T. */
11379
11380 static inline tree
11381 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11382 {
11383 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11384 }
11385
11386 /* Substitute ARGS into the pack of a fold expression T. */
11387
11388 static inline tree
11389 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11390 {
11391 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11392 }
11393
11394 /* Expand a PACK of arguments into a grouped as left fold.
11395 Given a pack containing elements A0, A1, ..., An and an
11396 operator @, this builds the expression:
11397
11398 ((A0 @ A1) @ A2) ... @ An
11399
11400 Note that PACK must not be empty.
11401
11402 The operator is defined by the original fold expression T. */
11403
11404 static tree
11405 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11406 {
11407 tree left = TREE_VEC_ELT (pack, 0);
11408 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11409 {
11410 tree right = TREE_VEC_ELT (pack, i);
11411 left = fold_expression (t, left, right, complain);
11412 }
11413 return left;
11414 }
11415
11416 /* Substitute into a unary left fold expression. */
11417
11418 static tree
11419 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11420 tree in_decl)
11421 {
11422 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11423 if (pack == error_mark_node)
11424 return error_mark_node;
11425 if (PACK_EXPANSION_P (pack))
11426 {
11427 tree r = copy_node (t);
11428 FOLD_EXPR_PACK (r) = pack;
11429 return r;
11430 }
11431 if (TREE_VEC_LENGTH (pack) == 0)
11432 return expand_empty_fold (t, complain);
11433 else
11434 return expand_left_fold (t, pack, complain);
11435 }
11436
11437 /* Substitute into a binary left fold expression.
11438
11439 Do ths by building a single (non-empty) vector of argumnts and
11440 building the expression from those elements. */
11441
11442 static tree
11443 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11444 tree in_decl)
11445 {
11446 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11447 if (pack == error_mark_node)
11448 return error_mark_node;
11449 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11450 if (init == error_mark_node)
11451 return error_mark_node;
11452
11453 if (PACK_EXPANSION_P (pack))
11454 {
11455 tree r = copy_node (t);
11456 FOLD_EXPR_PACK (r) = pack;
11457 FOLD_EXPR_INIT (r) = init;
11458 return r;
11459 }
11460
11461 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11462 TREE_VEC_ELT (vec, 0) = init;
11463 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11464 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11465
11466 return expand_left_fold (t, vec, complain);
11467 }
11468
11469 /* Expand a PACK of arguments into a grouped as right fold.
11470 Given a pack containing elementns A0, A1, ..., and an
11471 operator @, this builds the expression:
11472
11473 A0@ ... (An-2 @ (An-1 @ An))
11474
11475 Note that PACK must not be empty.
11476
11477 The operator is defined by the original fold expression T. */
11478
11479 tree
11480 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11481 {
11482 // Build the expression.
11483 int n = TREE_VEC_LENGTH (pack);
11484 tree right = TREE_VEC_ELT (pack, n - 1);
11485 for (--n; n != 0; --n)
11486 {
11487 tree left = TREE_VEC_ELT (pack, n - 1);
11488 right = fold_expression (t, left, right, complain);
11489 }
11490 return right;
11491 }
11492
11493 /* Substitute into a unary right fold expression. */
11494
11495 static tree
11496 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11497 tree in_decl)
11498 {
11499 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11500 if (pack == error_mark_node)
11501 return error_mark_node;
11502 if (PACK_EXPANSION_P (pack))
11503 {
11504 tree r = copy_node (t);
11505 FOLD_EXPR_PACK (r) = pack;
11506 return r;
11507 }
11508 if (TREE_VEC_LENGTH (pack) == 0)
11509 return expand_empty_fold (t, complain);
11510 else
11511 return expand_right_fold (t, pack, complain);
11512 }
11513
11514 /* Substitute into a binary right fold expression.
11515
11516 Do ths by building a single (non-empty) vector of arguments and
11517 building the expression from those elements. */
11518
11519 static tree
11520 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11521 tree in_decl)
11522 {
11523 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11524 if (pack == error_mark_node)
11525 return error_mark_node;
11526 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11527 if (init == error_mark_node)
11528 return error_mark_node;
11529
11530 if (PACK_EXPANSION_P (pack))
11531 {
11532 tree r = copy_node (t);
11533 FOLD_EXPR_PACK (r) = pack;
11534 FOLD_EXPR_INIT (r) = init;
11535 return r;
11536 }
11537
11538 int n = TREE_VEC_LENGTH (pack);
11539 tree vec = make_tree_vec (n + 1);
11540 for (int i = 0; i < n; ++i)
11541 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11542 TREE_VEC_ELT (vec, n) = init;
11543
11544 return expand_right_fold (t, vec, complain);
11545 }
11546
11547 /* Walk through the pattern of a pack expansion, adding everything in
11548 local_specializations to a list. */
11549
11550 struct el_data
11551 {
11552 tree extra;
11553 tsubst_flags_t complain;
11554 };
11555 static tree
11556 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11557 {
11558 el_data &data = *reinterpret_cast<el_data*>(data_);
11559 tree *extra = &data.extra;
11560 tsubst_flags_t complain = data.complain;
11561 if (tree spec = retrieve_local_specialization (*tp))
11562 {
11563 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11564 {
11565 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11566 tree args = ARGUMENT_PACK_ARGS (spec);
11567 if (TREE_VEC_LENGTH (args) == 1)
11568 {
11569 tree elt = TREE_VEC_ELT (args, 0);
11570 if (PACK_EXPANSION_P (elt))
11571 elt = PACK_EXPANSION_PATTERN (elt);
11572 if (DECL_PACK_P (elt))
11573 spec = elt;
11574 }
11575 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11576 {
11577 /* Handle lambda capture here, since we aren't doing any
11578 substitution now, and so tsubst_copy won't call
11579 process_outer_var_ref. */
11580 tree args = ARGUMENT_PACK_ARGS (spec);
11581 int len = TREE_VEC_LENGTH (args);
11582 for (int i = 0; i < len; ++i)
11583 {
11584 tree arg = TREE_VEC_ELT (args, i);
11585 tree carg = arg;
11586 if (outer_automatic_var_p (arg))
11587 carg = process_outer_var_ref (arg, complain);
11588 if (carg != arg)
11589 {
11590 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11591 proxies. */
11592 if (i == 0)
11593 {
11594 spec = copy_node (spec);
11595 args = copy_node (args);
11596 SET_ARGUMENT_PACK_ARGS (spec, args);
11597 register_local_specialization (spec, *tp);
11598 }
11599 TREE_VEC_ELT (args, i) = carg;
11600 }
11601 }
11602 }
11603 }
11604 if (outer_automatic_var_p (spec))
11605 spec = process_outer_var_ref (spec, complain);
11606 *extra = tree_cons (*tp, spec, *extra);
11607 }
11608 return NULL_TREE;
11609 }
11610 static tree
11611 extract_local_specs (tree pattern, tsubst_flags_t complain)
11612 {
11613 el_data data = { NULL_TREE, complain };
11614 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
11615 return data.extra;
11616 }
11617
11618 /* Substitute ARGS into T, which is an pack expansion
11619 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11620 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11621 (if only a partial substitution could be performed) or
11622 ERROR_MARK_NODE if there was an error. */
11623 tree
11624 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11625 tree in_decl)
11626 {
11627 tree pattern;
11628 tree pack, packs = NULL_TREE;
11629 bool unsubstituted_packs = false;
11630 bool unsubstituted_fn_pack = false;
11631 int i, len = -1;
11632 tree result;
11633 hash_map<tree, tree> *saved_local_specializations = NULL;
11634 bool need_local_specializations = false;
11635 int levels;
11636
11637 gcc_assert (PACK_EXPANSION_P (t));
11638 pattern = PACK_EXPANSION_PATTERN (t);
11639
11640 /* Add in any args remembered from an earlier partial instantiation. */
11641 tree extra = PACK_EXPANSION_EXTRA_ARGS (t);
11642 if (extra && TREE_CODE (extra) == TREE_LIST)
11643 {
11644 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
11645 {
11646 /* The partial instantiation involved local declarations collected in
11647 extract_local_specs; map from the general template to our local
11648 context. */
11649 tree gen = TREE_PURPOSE (elt);
11650 tree inst = TREE_VALUE (elt);
11651 if (DECL_P (inst))
11652 if (tree local = retrieve_local_specialization (inst))
11653 inst = local;
11654 /* else inst is already a full instantiation of the pack. */
11655 register_local_specialization (inst, gen);
11656 }
11657 gcc_assert (!TREE_PURPOSE (extra));
11658 extra = TREE_VALUE (extra);
11659 }
11660 args = add_to_template_args (extra, args);
11661
11662 levels = TMPL_ARGS_DEPTH (args);
11663
11664 /* Determine the argument packs that will instantiate the parameter
11665 packs used in the expansion expression. While we're at it,
11666 compute the number of arguments to be expanded and make sure it
11667 is consistent. */
11668 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11669 pack = TREE_CHAIN (pack))
11670 {
11671 tree parm_pack = TREE_VALUE (pack);
11672 tree arg_pack = NULL_TREE;
11673 tree orig_arg = NULL_TREE;
11674 int level = 0;
11675
11676 if (TREE_CODE (parm_pack) == BASES)
11677 {
11678 gcc_assert (parm_pack == pattern);
11679 if (BASES_DIRECT (parm_pack))
11680 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11681 args, complain, in_decl, false));
11682 else
11683 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11684 args, complain, in_decl, false));
11685 }
11686 else if (builtin_pack_call_p (parm_pack))
11687 {
11688 /* ??? Support use in other patterns. */
11689 gcc_assert (parm_pack == pattern);
11690 return expand_builtin_pack_call (parm_pack, args,
11691 complain, in_decl);
11692 }
11693 else if (TREE_CODE (parm_pack) == PARM_DECL)
11694 {
11695 /* We know we have correct local_specializations if this
11696 expansion is at function scope, or if we're dealing with a
11697 local parameter in a requires expression; for the latter,
11698 tsubst_requires_expr set it up appropriately. */
11699 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11700 arg_pack = retrieve_local_specialization (parm_pack);
11701 else
11702 /* We can't rely on local_specializations for a parameter
11703 name used later in a function declaration (such as in a
11704 late-specified return type). Even if it exists, it might
11705 have the wrong value for a recursive call. */
11706 need_local_specializations = true;
11707
11708 if (!arg_pack)
11709 {
11710 /* This parameter pack was used in an unevaluated context. Just
11711 make a dummy decl, since it's only used for its type. */
11712 arg_pack = tsubst_decl (parm_pack, args, complain);
11713 if (arg_pack && DECL_PACK_P (arg_pack))
11714 /* Partial instantiation of the parm_pack, we can't build
11715 up an argument pack yet. */
11716 arg_pack = NULL_TREE;
11717 else
11718 arg_pack = make_fnparm_pack (arg_pack);
11719 }
11720 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
11721 /* This argument pack isn't fully instantiated yet. We set this
11722 flag rather than clear arg_pack because we do want to do the
11723 optimization below, and we don't want to substitute directly
11724 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11725 where it isn't expected). */
11726 unsubstituted_fn_pack = true;
11727 }
11728 else if (is_normal_capture_proxy (parm_pack))
11729 {
11730 arg_pack = retrieve_local_specialization (parm_pack);
11731 if (argument_pack_element_is_expansion_p (arg_pack, 0))
11732 unsubstituted_fn_pack = true;
11733 }
11734 else
11735 {
11736 int idx;
11737 template_parm_level_and_index (parm_pack, &level, &idx);
11738
11739 if (level <= levels)
11740 arg_pack = TMPL_ARG (args, level, idx);
11741 }
11742
11743 orig_arg = arg_pack;
11744 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11745 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11746
11747 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11748 /* This can only happen if we forget to expand an argument
11749 pack somewhere else. Just return an error, silently. */
11750 {
11751 result = make_tree_vec (1);
11752 TREE_VEC_ELT (result, 0) = error_mark_node;
11753 return result;
11754 }
11755
11756 if (arg_pack)
11757 {
11758 int my_len =
11759 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11760
11761 /* Don't bother trying to do a partial substitution with
11762 incomplete packs; we'll try again after deduction. */
11763 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11764 return t;
11765
11766 if (len < 0)
11767 len = my_len;
11768 else if (len != my_len
11769 && !unsubstituted_fn_pack)
11770 {
11771 if (!(complain & tf_error))
11772 /* Fail quietly. */;
11773 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11774 error ("mismatched argument pack lengths while expanding %qT",
11775 pattern);
11776 else
11777 error ("mismatched argument pack lengths while expanding %qE",
11778 pattern);
11779 return error_mark_node;
11780 }
11781
11782 /* Keep track of the parameter packs and their corresponding
11783 argument packs. */
11784 packs = tree_cons (parm_pack, arg_pack, packs);
11785 TREE_TYPE (packs) = orig_arg;
11786 }
11787 else
11788 {
11789 /* We can't substitute for this parameter pack. We use a flag as
11790 well as the missing_level counter because function parameter
11791 packs don't have a level. */
11792 gcc_assert (processing_template_decl);
11793 unsubstituted_packs = true;
11794 }
11795 }
11796
11797 /* If the expansion is just T..., return the matching argument pack, unless
11798 we need to call convert_from_reference on all the elements. This is an
11799 important optimization; see c++/68422. */
11800 if (!unsubstituted_packs
11801 && TREE_PURPOSE (packs) == pattern)
11802 {
11803 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11804
11805 /* If the argument pack is a single pack expansion, pull it out. */
11806 if (TREE_VEC_LENGTH (args) == 1
11807 && pack_expansion_args_count (args))
11808 return TREE_VEC_ELT (args, 0);
11809
11810 /* Types need no adjustment, nor does sizeof..., and if we still have
11811 some pack expansion args we won't do anything yet. */
11812 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11813 || PACK_EXPANSION_SIZEOF_P (t)
11814 || pack_expansion_args_count (args))
11815 return args;
11816 /* Also optimize expression pack expansions if we can tell that the
11817 elements won't have reference type. */
11818 tree type = TREE_TYPE (pattern);
11819 if (type && TREE_CODE (type) != REFERENCE_TYPE
11820 && !PACK_EXPANSION_P (type)
11821 && !WILDCARD_TYPE_P (type))
11822 return args;
11823 /* Otherwise use the normal path so we get convert_from_reference. */
11824 }
11825
11826 /* We cannot expand this expansion expression, because we don't have
11827 all of the argument packs we need. */
11828 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11829 {
11830 /* We got some full packs, but we can't substitute them in until we
11831 have values for all the packs. So remember these until then. */
11832
11833 t = make_pack_expansion (pattern, complain);
11834 tree extra = args;
11835 if (local_specializations)
11836 if (tree locals = extract_local_specs (pattern, complain))
11837 extra = tree_cons (NULL_TREE, extra, locals);
11838 PACK_EXPANSION_EXTRA_ARGS (t) = extra;
11839 return t;
11840 }
11841 else if (unsubstituted_packs)
11842 {
11843 /* There were no real arguments, we're just replacing a parameter
11844 pack with another version of itself. Substitute into the
11845 pattern and return a PACK_EXPANSION_*. The caller will need to
11846 deal with that. */
11847 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11848 t = tsubst_expr (pattern, args, complain, in_decl,
11849 /*integral_constant_expression_p=*/false);
11850 else
11851 t = tsubst (pattern, args, complain, in_decl);
11852 t = make_pack_expansion (t, complain);
11853 return t;
11854 }
11855
11856 gcc_assert (len >= 0);
11857
11858 if (need_local_specializations)
11859 {
11860 /* We're in a late-specified return type, so create our own local
11861 specializations map; the current map is either NULL or (in the
11862 case of recursive unification) might have bindings that we don't
11863 want to use or alter. */
11864 saved_local_specializations = local_specializations;
11865 local_specializations = new hash_map<tree, tree>;
11866 }
11867
11868 /* For each argument in each argument pack, substitute into the
11869 pattern. */
11870 result = make_tree_vec (len);
11871 tree elem_args = copy_template_args (args);
11872 for (i = 0; i < len; ++i)
11873 {
11874 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11875 i,
11876 elem_args, complain,
11877 in_decl);
11878 TREE_VEC_ELT (result, i) = t;
11879 if (t == error_mark_node)
11880 {
11881 result = error_mark_node;
11882 break;
11883 }
11884 }
11885
11886 /* Update ARGS to restore the substitution from parameter packs to
11887 their argument packs. */
11888 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11889 {
11890 tree parm = TREE_PURPOSE (pack);
11891
11892 if (TREE_CODE (parm) == PARM_DECL
11893 || VAR_P (parm)
11894 || TREE_CODE (parm) == FIELD_DECL)
11895 register_local_specialization (TREE_TYPE (pack), parm);
11896 else
11897 {
11898 int idx, level;
11899
11900 if (TREE_VALUE (pack) == NULL_TREE)
11901 continue;
11902
11903 template_parm_level_and_index (parm, &level, &idx);
11904
11905 /* Update the corresponding argument. */
11906 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11907 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11908 TREE_TYPE (pack);
11909 else
11910 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11911 }
11912 }
11913
11914 if (need_local_specializations)
11915 {
11916 delete local_specializations;
11917 local_specializations = saved_local_specializations;
11918 }
11919
11920 /* If the dependent pack arguments were such that we end up with only a
11921 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11922 if (len == 1 && TREE_CODE (result) == TREE_VEC
11923 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11924 return TREE_VEC_ELT (result, 0);
11925
11926 return result;
11927 }
11928
11929 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11930 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11931 parameter packs; all parms generated from a function parameter pack will
11932 have the same DECL_PARM_INDEX. */
11933
11934 tree
11935 get_pattern_parm (tree parm, tree tmpl)
11936 {
11937 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11938 tree patparm;
11939
11940 if (DECL_ARTIFICIAL (parm))
11941 {
11942 for (patparm = DECL_ARGUMENTS (pattern);
11943 patparm; patparm = DECL_CHAIN (patparm))
11944 if (DECL_ARTIFICIAL (patparm)
11945 && DECL_NAME (parm) == DECL_NAME (patparm))
11946 break;
11947 }
11948 else
11949 {
11950 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11951 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11952 gcc_assert (DECL_PARM_INDEX (patparm)
11953 == DECL_PARM_INDEX (parm));
11954 }
11955
11956 return patparm;
11957 }
11958
11959 /* Make an argument pack out of the TREE_VEC VEC. */
11960
11961 static tree
11962 make_argument_pack (tree vec)
11963 {
11964 tree pack;
11965 tree elt = TREE_VEC_ELT (vec, 0);
11966 if (TYPE_P (elt))
11967 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11968 else
11969 {
11970 pack = make_node (NONTYPE_ARGUMENT_PACK);
11971 TREE_CONSTANT (pack) = 1;
11972 }
11973 SET_ARGUMENT_PACK_ARGS (pack, vec);
11974 return pack;
11975 }
11976
11977 /* Return an exact copy of template args T that can be modified
11978 independently. */
11979
11980 static tree
11981 copy_template_args (tree t)
11982 {
11983 if (t == error_mark_node)
11984 return t;
11985
11986 int len = TREE_VEC_LENGTH (t);
11987 tree new_vec = make_tree_vec (len);
11988
11989 for (int i = 0; i < len; ++i)
11990 {
11991 tree elt = TREE_VEC_ELT (t, i);
11992 if (elt && TREE_CODE (elt) == TREE_VEC)
11993 elt = copy_template_args (elt);
11994 TREE_VEC_ELT (new_vec, i) = elt;
11995 }
11996
11997 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11998 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11999
12000 return new_vec;
12001 }
12002
12003 /* Substitute ARGS into the vector or list of template arguments T. */
12004
12005 static tree
12006 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12007 {
12008 tree orig_t = t;
12009 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12010 tree *elts;
12011
12012 if (t == error_mark_node)
12013 return error_mark_node;
12014
12015 len = TREE_VEC_LENGTH (t);
12016 elts = XALLOCAVEC (tree, len);
12017
12018 for (i = 0; i < len; i++)
12019 {
12020 tree orig_arg = TREE_VEC_ELT (t, i);
12021 tree new_arg;
12022
12023 if (TREE_CODE (orig_arg) == TREE_VEC)
12024 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12025 else if (PACK_EXPANSION_P (orig_arg))
12026 {
12027 /* Substitute into an expansion expression. */
12028 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12029
12030 if (TREE_CODE (new_arg) == TREE_VEC)
12031 /* Add to the expanded length adjustment the number of
12032 expanded arguments. We subtract one from this
12033 measurement, because the argument pack expression
12034 itself is already counted as 1 in
12035 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12036 the argument pack is empty. */
12037 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12038 }
12039 else if (ARGUMENT_PACK_P (orig_arg))
12040 {
12041 /* Substitute into each of the arguments. */
12042 new_arg = TYPE_P (orig_arg)
12043 ? cxx_make_type (TREE_CODE (orig_arg))
12044 : make_node (TREE_CODE (orig_arg));
12045
12046 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12047 args, complain, in_decl);
12048 if (pack_args == error_mark_node)
12049 new_arg = error_mark_node;
12050 else
12051 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12052
12053 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12054 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12055 }
12056 else
12057 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12058
12059 if (new_arg == error_mark_node)
12060 return error_mark_node;
12061
12062 elts[i] = new_arg;
12063 if (new_arg != orig_arg)
12064 need_new = 1;
12065 }
12066
12067 if (!need_new)
12068 return t;
12069
12070 /* Make space for the expanded arguments coming from template
12071 argument packs. */
12072 t = make_tree_vec (len + expanded_len_adjust);
12073 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12074 arguments for a member template.
12075 In that case each TREE_VEC in ORIG_T represents a level of template
12076 arguments, and ORIG_T won't carry any non defaulted argument count.
12077 It will rather be the nested TREE_VECs that will carry one.
12078 In other words, ORIG_T carries a non defaulted argument count only
12079 if it doesn't contain any nested TREE_VEC. */
12080 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12081 {
12082 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12083 count += expanded_len_adjust;
12084 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12085 }
12086 for (i = 0, out = 0; i < len; i++)
12087 {
12088 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12089 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12090 && TREE_CODE (elts[i]) == TREE_VEC)
12091 {
12092 int idx;
12093
12094 /* Now expand the template argument pack "in place". */
12095 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12096 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12097 }
12098 else
12099 {
12100 TREE_VEC_ELT (t, out) = elts[i];
12101 out++;
12102 }
12103 }
12104
12105 return t;
12106 }
12107
12108 /* Substitute ARGS into one level PARMS of template parameters. */
12109
12110 static tree
12111 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12112 {
12113 if (parms == error_mark_node)
12114 return error_mark_node;
12115
12116 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12117
12118 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12119 {
12120 tree tuple = TREE_VEC_ELT (parms, i);
12121
12122 if (tuple == error_mark_node)
12123 continue;
12124
12125 TREE_VEC_ELT (new_vec, i) =
12126 tsubst_template_parm (tuple, args, complain);
12127 }
12128
12129 return new_vec;
12130 }
12131
12132 /* Return the result of substituting ARGS into the template parameters
12133 given by PARMS. If there are m levels of ARGS and m + n levels of
12134 PARMS, then the result will contain n levels of PARMS. For
12135 example, if PARMS is `template <class T> template <class U>
12136 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12137 result will be `template <int*, double, class V>'. */
12138
12139 static tree
12140 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12141 {
12142 tree r = NULL_TREE;
12143 tree* new_parms;
12144
12145 /* When substituting into a template, we must set
12146 PROCESSING_TEMPLATE_DECL as the template parameters may be
12147 dependent if they are based on one-another, and the dependency
12148 predicates are short-circuit outside of templates. */
12149 ++processing_template_decl;
12150
12151 for (new_parms = &r;
12152 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12153 new_parms = &(TREE_CHAIN (*new_parms)),
12154 parms = TREE_CHAIN (parms))
12155 {
12156 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12157 args, complain);
12158 *new_parms =
12159 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12160 - TMPL_ARGS_DEPTH (args)),
12161 new_vec, NULL_TREE);
12162 }
12163
12164 --processing_template_decl;
12165
12166 return r;
12167 }
12168
12169 /* Return the result of substituting ARGS into one template parameter
12170 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12171 parameter and which TREE_PURPOSE is the default argument of the
12172 template parameter. */
12173
12174 static tree
12175 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12176 {
12177 tree default_value, parm_decl;
12178
12179 if (args == NULL_TREE
12180 || t == NULL_TREE
12181 || t == error_mark_node)
12182 return t;
12183
12184 gcc_assert (TREE_CODE (t) == TREE_LIST);
12185
12186 default_value = TREE_PURPOSE (t);
12187 parm_decl = TREE_VALUE (t);
12188
12189 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12190 if (TREE_CODE (parm_decl) == PARM_DECL
12191 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12192 parm_decl = error_mark_node;
12193 default_value = tsubst_template_arg (default_value, args,
12194 complain, NULL_TREE);
12195
12196 return build_tree_list (default_value, parm_decl);
12197 }
12198
12199 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12200 type T. If T is not an aggregate or enumeration type, it is
12201 handled as if by tsubst. IN_DECL is as for tsubst. If
12202 ENTERING_SCOPE is nonzero, T is the context for a template which
12203 we are presently tsubst'ing. Return the substituted value. */
12204
12205 static tree
12206 tsubst_aggr_type (tree t,
12207 tree args,
12208 tsubst_flags_t complain,
12209 tree in_decl,
12210 int entering_scope)
12211 {
12212 if (t == NULL_TREE)
12213 return NULL_TREE;
12214
12215 switch (TREE_CODE (t))
12216 {
12217 case RECORD_TYPE:
12218 if (TYPE_PTRMEMFUNC_P (t))
12219 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12220
12221 /* Fall through. */
12222 case ENUMERAL_TYPE:
12223 case UNION_TYPE:
12224 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12225 {
12226 tree argvec;
12227 tree context;
12228 tree r;
12229 int saved_unevaluated_operand;
12230 int saved_inhibit_evaluation_warnings;
12231
12232 /* In "sizeof(X<I>)" we need to evaluate "I". */
12233 saved_unevaluated_operand = cp_unevaluated_operand;
12234 cp_unevaluated_operand = 0;
12235 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12236 c_inhibit_evaluation_warnings = 0;
12237
12238 /* First, determine the context for the type we are looking
12239 up. */
12240 context = TYPE_CONTEXT (t);
12241 if (context && TYPE_P (context))
12242 {
12243 context = tsubst_aggr_type (context, args, complain,
12244 in_decl, /*entering_scope=*/1);
12245 /* If context is a nested class inside a class template,
12246 it may still need to be instantiated (c++/33959). */
12247 context = complete_type (context);
12248 }
12249
12250 /* Then, figure out what arguments are appropriate for the
12251 type we are trying to find. For example, given:
12252
12253 template <class T> struct S;
12254 template <class T, class U> void f(T, U) { S<U> su; }
12255
12256 and supposing that we are instantiating f<int, double>,
12257 then our ARGS will be {int, double}, but, when looking up
12258 S we only want {double}. */
12259 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12260 complain, in_decl);
12261 if (argvec == error_mark_node)
12262 r = error_mark_node;
12263 else
12264 {
12265 r = lookup_template_class (t, argvec, in_decl, context,
12266 entering_scope, complain);
12267 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12268 }
12269
12270 cp_unevaluated_operand = saved_unevaluated_operand;
12271 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12272
12273 return r;
12274 }
12275 else
12276 /* This is not a template type, so there's nothing to do. */
12277 return t;
12278
12279 default:
12280 return tsubst (t, args, complain, in_decl);
12281 }
12282 }
12283
12284 static GTY((cache)) tree_cache_map *defarg_inst;
12285
12286 /* Substitute into the default argument ARG (a default argument for
12287 FN), which has the indicated TYPE. */
12288
12289 tree
12290 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12291 tsubst_flags_t complain)
12292 {
12293 tree saved_class_ptr = NULL_TREE;
12294 tree saved_class_ref = NULL_TREE;
12295 int errs = errorcount + sorrycount;
12296
12297 /* This can happen in invalid code. */
12298 if (TREE_CODE (arg) == DEFAULT_ARG)
12299 return arg;
12300
12301 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12302 parm = chain_index (parmnum, parm);
12303 tree parmtype = TREE_TYPE (parm);
12304 if (DECL_BY_REFERENCE (parm))
12305 parmtype = TREE_TYPE (parmtype);
12306 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12307
12308 tree *slot;
12309 if (defarg_inst && (slot = defarg_inst->get (parm)))
12310 return *slot;
12311
12312 /* This default argument came from a template. Instantiate the
12313 default argument here, not in tsubst. In the case of
12314 something like:
12315
12316 template <class T>
12317 struct S {
12318 static T t();
12319 void f(T = t());
12320 };
12321
12322 we must be careful to do name lookup in the scope of S<T>,
12323 rather than in the current class. */
12324 push_access_scope (fn);
12325 /* The "this" pointer is not valid in a default argument. */
12326 if (cfun)
12327 {
12328 saved_class_ptr = current_class_ptr;
12329 cp_function_chain->x_current_class_ptr = NULL_TREE;
12330 saved_class_ref = current_class_ref;
12331 cp_function_chain->x_current_class_ref = NULL_TREE;
12332 }
12333
12334 start_lambda_scope (parm);
12335
12336 push_deferring_access_checks(dk_no_deferred);
12337 /* The default argument expression may cause implicitly defined
12338 member functions to be synthesized, which will result in garbage
12339 collection. We must treat this situation as if we were within
12340 the body of function so as to avoid collecting live data on the
12341 stack. */
12342 ++function_depth;
12343 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12344 complain, NULL_TREE,
12345 /*integral_constant_expression_p=*/false);
12346 --function_depth;
12347 pop_deferring_access_checks();
12348
12349 finish_lambda_scope ();
12350
12351 /* Restore the "this" pointer. */
12352 if (cfun)
12353 {
12354 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12355 cp_function_chain->x_current_class_ref = saved_class_ref;
12356 }
12357
12358 if (errorcount+sorrycount > errs
12359 && (complain & tf_warning_or_error))
12360 inform (input_location,
12361 " when instantiating default argument for call to %qD", fn);
12362
12363 /* Make sure the default argument is reasonable. */
12364 arg = check_default_argument (type, arg, complain);
12365
12366 pop_access_scope (fn);
12367
12368 if (arg != error_mark_node && !cp_unevaluated_operand)
12369 {
12370 if (!defarg_inst)
12371 defarg_inst = tree_cache_map::create_ggc (37);
12372 defarg_inst->put (parm, arg);
12373 }
12374
12375 return arg;
12376 }
12377
12378 /* Substitute into all the default arguments for FN. */
12379
12380 static void
12381 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12382 {
12383 tree arg;
12384 tree tmpl_args;
12385
12386 tmpl_args = DECL_TI_ARGS (fn);
12387
12388 /* If this function is not yet instantiated, we certainly don't need
12389 its default arguments. */
12390 if (uses_template_parms (tmpl_args))
12391 return;
12392 /* Don't do this again for clones. */
12393 if (DECL_CLONED_FUNCTION_P (fn))
12394 return;
12395
12396 int i = 0;
12397 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12398 arg;
12399 arg = TREE_CHAIN (arg), ++i)
12400 if (TREE_PURPOSE (arg))
12401 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12402 TREE_VALUE (arg),
12403 TREE_PURPOSE (arg),
12404 complain);
12405 }
12406
12407 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12408
12409 static tree
12410 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12411 tree lambda_fntype)
12412 {
12413 tree gen_tmpl, argvec;
12414 hashval_t hash = 0;
12415 tree in_decl = t;
12416
12417 /* Nobody should be tsubst'ing into non-template functions. */
12418 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12419
12420 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12421 {
12422 /* If T is not dependent, just return it. */
12423 if (!uses_template_parms (DECL_TI_ARGS (t)))
12424 return t;
12425
12426 /* Calculate the most general template of which R is a
12427 specialization. */
12428 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12429
12430 /* We're substituting a lambda function under tsubst_lambda_expr but not
12431 directly from it; find the matching function we're already inside.
12432 But don't do this if T is a generic lambda with a single level of
12433 template parms, as in that case we're doing a normal instantiation. */
12434 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12435 && (!generic_lambda_fn_p (t)
12436 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12437 return enclosing_instantiation_of (t);
12438
12439 /* Calculate the complete set of arguments used to
12440 specialize R. */
12441 argvec = tsubst_template_args (DECL_TI_ARGS
12442 (DECL_TEMPLATE_RESULT
12443 (DECL_TI_TEMPLATE (t))),
12444 args, complain, in_decl);
12445 if (argvec == error_mark_node)
12446 return error_mark_node;
12447
12448 /* Check to see if we already have this specialization. */
12449 if (!lambda_fntype)
12450 {
12451 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12452 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12453 return spec;
12454 }
12455
12456 /* We can see more levels of arguments than parameters if
12457 there was a specialization of a member template, like
12458 this:
12459
12460 template <class T> struct S { template <class U> void f(); }
12461 template <> template <class U> void S<int>::f(U);
12462
12463 Here, we'll be substituting into the specialization,
12464 because that's where we can find the code we actually
12465 want to generate, but we'll have enough arguments for
12466 the most general template.
12467
12468 We also deal with the peculiar case:
12469
12470 template <class T> struct S {
12471 template <class U> friend void f();
12472 };
12473 template <class U> void f() {}
12474 template S<int>;
12475 template void f<double>();
12476
12477 Here, the ARGS for the instantiation of will be {int,
12478 double}. But, we only need as many ARGS as there are
12479 levels of template parameters in CODE_PATTERN. We are
12480 careful not to get fooled into reducing the ARGS in
12481 situations like:
12482
12483 template <class T> struct S { template <class U> void f(U); }
12484 template <class T> template <> void S<T>::f(int) {}
12485
12486 which we can spot because the pattern will be a
12487 specialization in this case. */
12488 int args_depth = TMPL_ARGS_DEPTH (args);
12489 int parms_depth =
12490 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12491
12492 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12493 args = get_innermost_template_args (args, parms_depth);
12494 }
12495 else
12496 {
12497 /* This special case arises when we have something like this:
12498
12499 template <class T> struct S {
12500 friend void f<int>(int, double);
12501 };
12502
12503 Here, the DECL_TI_TEMPLATE for the friend declaration
12504 will be an IDENTIFIER_NODE. We are being called from
12505 tsubst_friend_function, and we want only to create a
12506 new decl (R) with appropriate types so that we can call
12507 determine_specialization. */
12508 gen_tmpl = NULL_TREE;
12509 argvec = NULL_TREE;
12510 }
12511
12512 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12513 : NULL_TREE);
12514 tree ctx = closure ? closure : DECL_CONTEXT (t);
12515 bool member = ctx && TYPE_P (ctx);
12516
12517 if (member && !closure)
12518 ctx = tsubst_aggr_type (ctx, args,
12519 complain, t, /*entering_scope=*/1);
12520
12521 tree type = (lambda_fntype ? lambda_fntype
12522 : tsubst (TREE_TYPE (t), args,
12523 complain | tf_fndecl_type, in_decl));
12524 if (type == error_mark_node)
12525 return error_mark_node;
12526
12527 /* If we hit excessive deduction depth, the type is bogus even if
12528 it isn't error_mark_node, so don't build a decl. */
12529 if (excessive_deduction_depth)
12530 return error_mark_node;
12531
12532 /* We do NOT check for matching decls pushed separately at this
12533 point, as they may not represent instantiations of this
12534 template, and in any case are considered separate under the
12535 discrete model. */
12536 tree r = copy_decl (t);
12537 DECL_USE_TEMPLATE (r) = 0;
12538 TREE_TYPE (r) = type;
12539 /* Clear out the mangled name and RTL for the instantiation. */
12540 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12541 SET_DECL_RTL (r, NULL);
12542 /* Leave DECL_INITIAL set on deleted instantiations. */
12543 if (!DECL_DELETED_FN (r))
12544 DECL_INITIAL (r) = NULL_TREE;
12545 DECL_CONTEXT (r) = ctx;
12546
12547 /* OpenMP UDRs have the only argument a reference to the declared
12548 type. We want to diagnose if the declared type is a reference,
12549 which is invalid, but as references to references are usually
12550 quietly merged, diagnose it here. */
12551 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12552 {
12553 tree argtype
12554 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12555 argtype = tsubst (argtype, args, complain, in_decl);
12556 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12557 error_at (DECL_SOURCE_LOCATION (t),
12558 "reference type %qT in "
12559 "%<#pragma omp declare reduction%>", argtype);
12560 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12561 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12562 argtype);
12563 }
12564
12565 if (member && DECL_CONV_FN_P (r))
12566 /* Type-conversion operator. Reconstruct the name, in
12567 case it's the name of one of the template's parameters. */
12568 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12569
12570 tree parms = DECL_ARGUMENTS (t);
12571 if (closure)
12572 parms = DECL_CHAIN (parms);
12573 parms = tsubst (parms, args, complain, t);
12574 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12575 DECL_CONTEXT (parm) = r;
12576 if (closure)
12577 {
12578 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12579 DECL_CHAIN (tparm) = parms;
12580 parms = tparm;
12581 }
12582 DECL_ARGUMENTS (r) = parms;
12583 DECL_RESULT (r) = NULL_TREE;
12584
12585 TREE_STATIC (r) = 0;
12586 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12587 DECL_EXTERNAL (r) = 1;
12588 /* If this is an instantiation of a function with internal
12589 linkage, we already know what object file linkage will be
12590 assigned to the instantiation. */
12591 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12592 DECL_DEFER_OUTPUT (r) = 0;
12593 DECL_CHAIN (r) = NULL_TREE;
12594 DECL_PENDING_INLINE_INFO (r) = 0;
12595 DECL_PENDING_INLINE_P (r) = 0;
12596 DECL_SAVED_TREE (r) = NULL_TREE;
12597 DECL_STRUCT_FUNCTION (r) = NULL;
12598 TREE_USED (r) = 0;
12599 /* We'll re-clone as appropriate in instantiate_template. */
12600 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12601
12602 /* If we aren't complaining now, return on error before we register
12603 the specialization so that we'll complain eventually. */
12604 if ((complain & tf_error) == 0
12605 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12606 && !grok_op_properties (r, /*complain=*/false))
12607 return error_mark_node;
12608
12609 /* When instantiating a constrained member, substitute
12610 into the constraints to create a new constraint. */
12611 if (tree ci = get_constraints (t))
12612 if (member)
12613 {
12614 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12615 set_constraints (r, ci);
12616 }
12617
12618 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12619 this in the special friend case mentioned above where
12620 GEN_TMPL is NULL. */
12621 if (gen_tmpl && !closure)
12622 {
12623 DECL_TEMPLATE_INFO (r)
12624 = build_template_info (gen_tmpl, argvec);
12625 SET_DECL_IMPLICIT_INSTANTIATION (r);
12626
12627 tree new_r
12628 = register_specialization (r, gen_tmpl, argvec, false, hash);
12629 if (new_r != r)
12630 /* We instantiated this while substituting into
12631 the type earlier (template/friend54.C). */
12632 return new_r;
12633
12634 /* We're not supposed to instantiate default arguments
12635 until they are called, for a template. But, for a
12636 declaration like:
12637
12638 template <class T> void f ()
12639 { extern void g(int i = T()); }
12640
12641 we should do the substitution when the template is
12642 instantiated. We handle the member function case in
12643 instantiate_class_template since the default arguments
12644 might refer to other members of the class. */
12645 if (!member
12646 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12647 && !uses_template_parms (argvec))
12648 tsubst_default_arguments (r, complain);
12649 }
12650 else
12651 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12652
12653 /* Copy the list of befriending classes. */
12654 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12655 *friends;
12656 friends = &TREE_CHAIN (*friends))
12657 {
12658 *friends = copy_node (*friends);
12659 TREE_VALUE (*friends)
12660 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12661 }
12662
12663 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12664 {
12665 maybe_retrofit_in_chrg (r);
12666 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12667 return error_mark_node;
12668 /* If this is an instantiation of a member template, clone it.
12669 If it isn't, that'll be handled by
12670 clone_constructors_and_destructors. */
12671 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12672 clone_function_decl (r, /*update_methods=*/false);
12673 }
12674 else if ((complain & tf_error) != 0
12675 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12676 && !grok_op_properties (r, /*complain=*/true))
12677 return error_mark_node;
12678
12679 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12680 SET_DECL_FRIEND_CONTEXT (r,
12681 tsubst (DECL_FRIEND_CONTEXT (t),
12682 args, complain, in_decl));
12683
12684 /* Possibly limit visibility based on template args. */
12685 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12686 if (DECL_VISIBILITY_SPECIFIED (t))
12687 {
12688 DECL_VISIBILITY_SPECIFIED (r) = 0;
12689 DECL_ATTRIBUTES (r)
12690 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12691 }
12692 determine_visibility (r);
12693 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12694 && !processing_template_decl)
12695 defaulted_late_check (r);
12696
12697 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12698 args, complain, in_decl);
12699 return r;
12700 }
12701
12702 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12703
12704 static tree
12705 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12706 tree lambda_fntype)
12707 {
12708 /* We can get here when processing a member function template,
12709 member class template, or template template parameter. */
12710 tree decl = DECL_TEMPLATE_RESULT (t);
12711 tree in_decl = t;
12712 tree spec;
12713 tree tmpl_args;
12714 tree full_args;
12715 tree r;
12716 hashval_t hash = 0;
12717
12718 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12719 {
12720 /* Template template parameter is treated here. */
12721 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12722 if (new_type == error_mark_node)
12723 r = error_mark_node;
12724 /* If we get a real template back, return it. This can happen in
12725 the context of most_specialized_partial_spec. */
12726 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12727 r = new_type;
12728 else
12729 /* The new TEMPLATE_DECL was built in
12730 reduce_template_parm_level. */
12731 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12732 return r;
12733 }
12734
12735 if (!lambda_fntype)
12736 {
12737 /* We might already have an instance of this template.
12738 The ARGS are for the surrounding class type, so the
12739 full args contain the tsubst'd args for the context,
12740 plus the innermost args from the template decl. */
12741 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12742 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12743 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12744 /* Because this is a template, the arguments will still be
12745 dependent, even after substitution. If
12746 PROCESSING_TEMPLATE_DECL is not set, the dependency
12747 predicates will short-circuit. */
12748 ++processing_template_decl;
12749 full_args = tsubst_template_args (tmpl_args, args,
12750 complain, in_decl);
12751 --processing_template_decl;
12752 if (full_args == error_mark_node)
12753 return error_mark_node;
12754
12755 /* If this is a default template template argument,
12756 tsubst might not have changed anything. */
12757 if (full_args == tmpl_args)
12758 return t;
12759
12760 hash = hash_tmpl_and_args (t, full_args);
12761 spec = retrieve_specialization (t, full_args, hash);
12762 if (spec != NULL_TREE)
12763 return spec;
12764 }
12765
12766 /* Make a new template decl. It will be similar to the
12767 original, but will record the current template arguments.
12768 We also create a new function declaration, which is just
12769 like the old one, but points to this new template, rather
12770 than the old one. */
12771 r = copy_decl (t);
12772 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12773 DECL_CHAIN (r) = NULL_TREE;
12774
12775 // Build new template info linking to the original template decl.
12776 if (!lambda_fntype)
12777 {
12778 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12779 SET_DECL_IMPLICIT_INSTANTIATION (r);
12780 }
12781 else
12782 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12783
12784 /* The template parameters for this new template are all the
12785 template parameters for the old template, except the
12786 outermost level of parameters. */
12787 DECL_TEMPLATE_PARMS (r)
12788 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12789 complain);
12790
12791 if (TREE_CODE (decl) == TYPE_DECL
12792 && !TYPE_DECL_ALIAS_P (decl))
12793 {
12794 tree new_type;
12795 ++processing_template_decl;
12796 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12797 --processing_template_decl;
12798 if (new_type == error_mark_node)
12799 return error_mark_node;
12800
12801 TREE_TYPE (r) = new_type;
12802 /* For a partial specialization, we need to keep pointing to
12803 the primary template. */
12804 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12805 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12806 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12807 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12808 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12809 }
12810 else
12811 {
12812 tree new_decl;
12813 ++processing_template_decl;
12814 if (TREE_CODE (decl) == FUNCTION_DECL)
12815 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12816 else
12817 new_decl = tsubst (decl, args, complain, in_decl);
12818 --processing_template_decl;
12819 if (new_decl == error_mark_node)
12820 return error_mark_node;
12821
12822 DECL_TEMPLATE_RESULT (r) = new_decl;
12823 TREE_TYPE (r) = TREE_TYPE (new_decl);
12824 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12825 if (lambda_fntype)
12826 {
12827 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12828 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12829 }
12830 else
12831 {
12832 DECL_TI_TEMPLATE (new_decl) = r;
12833 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12834 }
12835 }
12836
12837 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12838 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12839
12840 if (PRIMARY_TEMPLATE_P (t))
12841 DECL_PRIMARY_TEMPLATE (r) = r;
12842
12843 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12844 && !lambda_fntype)
12845 /* Record this non-type partial instantiation. */
12846 register_specialization (r, t,
12847 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12848 false, hash);
12849
12850 return r;
12851 }
12852
12853 /* True if FN is the op() for a lambda in an uninstantiated template. */
12854
12855 bool
12856 lambda_fn_in_template_p (tree fn)
12857 {
12858 if (!fn || !LAMBDA_FUNCTION_P (fn))
12859 return false;
12860 tree closure = DECL_CONTEXT (fn);
12861 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12862 }
12863
12864 /* We're instantiating a variable from template function TCTX. Return the
12865 corresponding current enclosing scope. This gets complicated because lambda
12866 functions in templates are regenerated rather than instantiated, but generic
12867 lambda functions are subsequently instantiated. */
12868
12869 static tree
12870 enclosing_instantiation_of (tree otctx)
12871 {
12872 tree tctx = otctx;
12873 tree fn = current_function_decl;
12874 int lambda_count = 0;
12875
12876 for (; tctx && lambda_fn_in_template_p (tctx);
12877 tctx = decl_function_context (tctx))
12878 ++lambda_count;
12879 for (; fn; fn = decl_function_context (fn))
12880 {
12881 tree ofn = fn;
12882 int flambda_count = 0;
12883 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
12884 fn = decl_function_context (fn))
12885 ++flambda_count;
12886 if (DECL_TEMPLATE_INFO (fn)
12887 ? most_general_template (fn) != most_general_template (tctx)
12888 : fn != tctx)
12889 continue;
12890 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
12891 || DECL_CONV_FN_P (ofn));
12892 return ofn;
12893 }
12894 gcc_unreachable ();
12895 }
12896
12897 /* Substitute the ARGS into the T, which is a _DECL. Return the
12898 result of the substitution. Issue error and warning messages under
12899 control of COMPLAIN. */
12900
12901 static tree
12902 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12903 {
12904 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12905 location_t saved_loc;
12906 tree r = NULL_TREE;
12907 tree in_decl = t;
12908 hashval_t hash = 0;
12909
12910 /* Set the filename and linenumber to improve error-reporting. */
12911 saved_loc = input_location;
12912 input_location = DECL_SOURCE_LOCATION (t);
12913
12914 switch (TREE_CODE (t))
12915 {
12916 case TEMPLATE_DECL:
12917 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12918 break;
12919
12920 case FUNCTION_DECL:
12921 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12922 break;
12923
12924 case PARM_DECL:
12925 {
12926 tree type = NULL_TREE;
12927 int i, len = 1;
12928 tree expanded_types = NULL_TREE;
12929 tree prev_r = NULL_TREE;
12930 tree first_r = NULL_TREE;
12931
12932 if (DECL_PACK_P (t))
12933 {
12934 /* If there is a local specialization that isn't a
12935 parameter pack, it means that we're doing a "simple"
12936 substitution from inside tsubst_pack_expansion. Just
12937 return the local specialization (which will be a single
12938 parm). */
12939 tree spec = retrieve_local_specialization (t);
12940 if (spec
12941 && TREE_CODE (spec) == PARM_DECL
12942 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12943 RETURN (spec);
12944
12945 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12946 the parameters in this function parameter pack. */
12947 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12948 complain, in_decl);
12949 if (TREE_CODE (expanded_types) == TREE_VEC)
12950 {
12951 len = TREE_VEC_LENGTH (expanded_types);
12952
12953 /* Zero-length parameter packs are boring. Just substitute
12954 into the chain. */
12955 if (len == 0)
12956 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12957 TREE_CHAIN (t)));
12958 }
12959 else
12960 {
12961 /* All we did was update the type. Make a note of that. */
12962 type = expanded_types;
12963 expanded_types = NULL_TREE;
12964 }
12965 }
12966
12967 /* Loop through all of the parameters we'll build. When T is
12968 a function parameter pack, LEN is the number of expanded
12969 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12970 r = NULL_TREE;
12971 for (i = 0; i < len; ++i)
12972 {
12973 prev_r = r;
12974 r = copy_node (t);
12975 if (DECL_TEMPLATE_PARM_P (t))
12976 SET_DECL_TEMPLATE_PARM_P (r);
12977
12978 if (expanded_types)
12979 /* We're on the Ith parameter of the function parameter
12980 pack. */
12981 {
12982 /* Get the Ith type. */
12983 type = TREE_VEC_ELT (expanded_types, i);
12984
12985 /* Rename the parameter to include the index. */
12986 DECL_NAME (r)
12987 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12988 }
12989 else if (!type)
12990 /* We're dealing with a normal parameter. */
12991 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12992
12993 type = type_decays_to (type);
12994 TREE_TYPE (r) = type;
12995 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12996
12997 if (DECL_INITIAL (r))
12998 {
12999 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13000 DECL_INITIAL (r) = TREE_TYPE (r);
13001 else
13002 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13003 complain, in_decl);
13004 }
13005
13006 DECL_CONTEXT (r) = NULL_TREE;
13007
13008 if (!DECL_TEMPLATE_PARM_P (r))
13009 DECL_ARG_TYPE (r) = type_passed_as (type);
13010
13011 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13012 args, complain, in_decl);
13013
13014 /* Keep track of the first new parameter we
13015 generate. That's what will be returned to the
13016 caller. */
13017 if (!first_r)
13018 first_r = r;
13019
13020 /* Build a proper chain of parameters when substituting
13021 into a function parameter pack. */
13022 if (prev_r)
13023 DECL_CHAIN (prev_r) = r;
13024 }
13025
13026 /* If cp_unevaluated_operand is set, we're just looking for a
13027 single dummy parameter, so don't keep going. */
13028 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13029 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13030 complain, DECL_CHAIN (t));
13031
13032 /* FIRST_R contains the start of the chain we've built. */
13033 r = first_r;
13034 }
13035 break;
13036
13037 case FIELD_DECL:
13038 {
13039 tree type = NULL_TREE;
13040 tree vec = NULL_TREE;
13041 tree expanded_types = NULL_TREE;
13042 int len = 1;
13043
13044 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13045 {
13046 /* This field is a lambda capture pack. Return a TREE_VEC of
13047 the expanded fields to instantiate_class_template_1. */
13048 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13049 complain, in_decl);
13050 if (TREE_CODE (expanded_types) == TREE_VEC)
13051 {
13052 len = TREE_VEC_LENGTH (expanded_types);
13053 vec = make_tree_vec (len);
13054 }
13055 else
13056 {
13057 /* All we did was update the type. Make a note of that. */
13058 type = expanded_types;
13059 expanded_types = NULL_TREE;
13060 }
13061 }
13062
13063 for (int i = 0; i < len; ++i)
13064 {
13065 r = copy_decl (t);
13066 if (expanded_types)
13067 {
13068 type = TREE_VEC_ELT (expanded_types, i);
13069 DECL_NAME (r)
13070 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13071 }
13072 else if (!type)
13073 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13074
13075 if (type == error_mark_node)
13076 RETURN (error_mark_node);
13077 TREE_TYPE (r) = type;
13078 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13079
13080 if (DECL_C_BIT_FIELD (r))
13081 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13082 number of bits. */
13083 DECL_BIT_FIELD_REPRESENTATIVE (r)
13084 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13085 complain, in_decl,
13086 /*integral_constant_expression_p=*/true);
13087 if (DECL_INITIAL (t))
13088 {
13089 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13090 NSDMI in perform_member_init. Still set DECL_INITIAL
13091 so that we know there is one. */
13092 DECL_INITIAL (r) = void_node;
13093 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13094 retrofit_lang_decl (r);
13095 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13096 }
13097 /* We don't have to set DECL_CONTEXT here; it is set by
13098 finish_member_declaration. */
13099 DECL_CHAIN (r) = NULL_TREE;
13100
13101 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13102 args, complain, in_decl);
13103
13104 if (vec)
13105 TREE_VEC_ELT (vec, i) = r;
13106 }
13107
13108 if (vec)
13109 r = vec;
13110 }
13111 break;
13112
13113 case USING_DECL:
13114 /* We reach here only for member using decls. We also need to check
13115 uses_template_parms because DECL_DEPENDENT_P is not set for a
13116 using-declaration that designates a member of the current
13117 instantiation (c++/53549). */
13118 if (DECL_DEPENDENT_P (t)
13119 || uses_template_parms (USING_DECL_SCOPE (t)))
13120 {
13121 tree scope = USING_DECL_SCOPE (t);
13122 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13123 if (PACK_EXPANSION_P (scope))
13124 {
13125 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13126 int len = TREE_VEC_LENGTH (vec);
13127 r = make_tree_vec (len);
13128 for (int i = 0; i < len; ++i)
13129 {
13130 tree escope = TREE_VEC_ELT (vec, i);
13131 tree elt = do_class_using_decl (escope, name);
13132 if (!elt)
13133 {
13134 r = error_mark_node;
13135 break;
13136 }
13137 else
13138 {
13139 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13140 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13141 }
13142 TREE_VEC_ELT (r, i) = elt;
13143 }
13144 }
13145 else
13146 {
13147 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13148 complain, in_decl);
13149 r = do_class_using_decl (inst_scope, name);
13150 if (!r)
13151 r = error_mark_node;
13152 else
13153 {
13154 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13155 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13156 }
13157 }
13158 }
13159 else
13160 {
13161 r = copy_node (t);
13162 DECL_CHAIN (r) = NULL_TREE;
13163 }
13164 break;
13165
13166 case TYPE_DECL:
13167 case VAR_DECL:
13168 {
13169 tree argvec = NULL_TREE;
13170 tree gen_tmpl = NULL_TREE;
13171 tree spec;
13172 tree tmpl = NULL_TREE;
13173 tree ctx;
13174 tree type = NULL_TREE;
13175 bool local_p;
13176
13177 if (TREE_TYPE (t) == error_mark_node)
13178 RETURN (error_mark_node);
13179
13180 if (TREE_CODE (t) == TYPE_DECL
13181 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13182 {
13183 /* If this is the canonical decl, we don't have to
13184 mess with instantiations, and often we can't (for
13185 typename, template type parms and such). Note that
13186 TYPE_NAME is not correct for the above test if
13187 we've copied the type for a typedef. */
13188 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13189 if (type == error_mark_node)
13190 RETURN (error_mark_node);
13191 r = TYPE_NAME (type);
13192 break;
13193 }
13194
13195 /* Check to see if we already have the specialization we
13196 need. */
13197 spec = NULL_TREE;
13198 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13199 {
13200 /* T is a static data member or namespace-scope entity.
13201 We have to substitute into namespace-scope variables
13202 (not just variable templates) because of cases like:
13203
13204 template <class T> void f() { extern T t; }
13205
13206 where the entity referenced is not known until
13207 instantiation time. */
13208 local_p = false;
13209 ctx = DECL_CONTEXT (t);
13210 if (DECL_CLASS_SCOPE_P (t))
13211 {
13212 ctx = tsubst_aggr_type (ctx, args,
13213 complain,
13214 in_decl, /*entering_scope=*/1);
13215 /* If CTX is unchanged, then T is in fact the
13216 specialization we want. That situation occurs when
13217 referencing a static data member within in its own
13218 class. We can use pointer equality, rather than
13219 same_type_p, because DECL_CONTEXT is always
13220 canonical... */
13221 if (ctx == DECL_CONTEXT (t)
13222 /* ... unless T is a member template; in which
13223 case our caller can be willing to create a
13224 specialization of that template represented
13225 by T. */
13226 && !(DECL_TI_TEMPLATE (t)
13227 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13228 spec = t;
13229 }
13230
13231 if (!spec)
13232 {
13233 tmpl = DECL_TI_TEMPLATE (t);
13234 gen_tmpl = most_general_template (tmpl);
13235 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13236 if (argvec != error_mark_node)
13237 argvec = (coerce_innermost_template_parms
13238 (DECL_TEMPLATE_PARMS (gen_tmpl),
13239 argvec, t, complain,
13240 /*all*/true, /*defarg*/true));
13241 if (argvec == error_mark_node)
13242 RETURN (error_mark_node);
13243 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13244 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13245 }
13246 }
13247 else
13248 {
13249 /* A local variable. */
13250 local_p = true;
13251 /* Subsequent calls to pushdecl will fill this in. */
13252 ctx = NULL_TREE;
13253 /* Unless this is a reference to a static variable from an
13254 enclosing function, in which case we need to fill it in now. */
13255 if (TREE_STATIC (t))
13256 {
13257 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13258 if (fn != current_function_decl)
13259 ctx = fn;
13260 }
13261 spec = retrieve_local_specialization (t);
13262 }
13263 /* If we already have the specialization we need, there is
13264 nothing more to do. */
13265 if (spec)
13266 {
13267 r = spec;
13268 break;
13269 }
13270
13271 /* Create a new node for the specialization we need. */
13272 r = copy_decl (t);
13273 if (type == NULL_TREE)
13274 {
13275 if (is_typedef_decl (t))
13276 type = DECL_ORIGINAL_TYPE (t);
13277 else
13278 type = TREE_TYPE (t);
13279 if (VAR_P (t)
13280 && VAR_HAD_UNKNOWN_BOUND (t)
13281 && type != error_mark_node)
13282 type = strip_array_domain (type);
13283 tree sub_args = args;
13284 if (tree auto_node = type_uses_auto (type))
13285 {
13286 /* Mask off any template args past the variable's context so we
13287 don't replace the auto with an unrelated argument. */
13288 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13289 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13290 if (extra > 0)
13291 /* This should never happen with the new lambda instantiation
13292 model, but keep the handling just in case. */
13293 gcc_assert (!CHECKING_P),
13294 sub_args = strip_innermost_template_args (args, extra);
13295 }
13296 type = tsubst (type, sub_args, complain, in_decl);
13297 }
13298 if (VAR_P (r))
13299 {
13300 /* Even if the original location is out of scope, the
13301 newly substituted one is not. */
13302 DECL_DEAD_FOR_LOCAL (r) = 0;
13303 DECL_INITIALIZED_P (r) = 0;
13304 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13305 if (type == error_mark_node)
13306 RETURN (error_mark_node);
13307 if (TREE_CODE (type) == FUNCTION_TYPE)
13308 {
13309 /* It may seem that this case cannot occur, since:
13310
13311 typedef void f();
13312 void g() { f x; }
13313
13314 declares a function, not a variable. However:
13315
13316 typedef void f();
13317 template <typename T> void g() { T t; }
13318 template void g<f>();
13319
13320 is an attempt to declare a variable with function
13321 type. */
13322 error ("variable %qD has function type",
13323 /* R is not yet sufficiently initialized, so we
13324 just use its name. */
13325 DECL_NAME (r));
13326 RETURN (error_mark_node);
13327 }
13328 type = complete_type (type);
13329 /* Wait until cp_finish_decl to set this again, to handle
13330 circular dependency (template/instantiate6.C). */
13331 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13332 type = check_var_type (DECL_NAME (r), type);
13333
13334 if (DECL_HAS_VALUE_EXPR_P (t))
13335 {
13336 tree ve = DECL_VALUE_EXPR (t);
13337 ve = tsubst_expr (ve, args, complain, in_decl,
13338 /*constant_expression_p=*/false);
13339 if (REFERENCE_REF_P (ve))
13340 {
13341 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13342 ve = TREE_OPERAND (ve, 0);
13343 }
13344 SET_DECL_VALUE_EXPR (r, ve);
13345 }
13346 if (CP_DECL_THREAD_LOCAL_P (r)
13347 && !processing_template_decl)
13348 set_decl_tls_model (r, decl_default_tls_model (r));
13349 }
13350 else if (DECL_SELF_REFERENCE_P (t))
13351 SET_DECL_SELF_REFERENCE_P (r);
13352 TREE_TYPE (r) = type;
13353 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13354 DECL_CONTEXT (r) = ctx;
13355 /* Clear out the mangled name and RTL for the instantiation. */
13356 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13357 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13358 SET_DECL_RTL (r, NULL);
13359 /* The initializer must not be expanded until it is required;
13360 see [temp.inst]. */
13361 DECL_INITIAL (r) = NULL_TREE;
13362 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13363 if (VAR_P (r))
13364 {
13365 if (DECL_LANG_SPECIFIC (r))
13366 SET_DECL_DEPENDENT_INIT_P (r, false);
13367
13368 SET_DECL_MODE (r, VOIDmode);
13369
13370 /* Possibly limit visibility based on template args. */
13371 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13372 if (DECL_VISIBILITY_SPECIFIED (t))
13373 {
13374 DECL_VISIBILITY_SPECIFIED (r) = 0;
13375 DECL_ATTRIBUTES (r)
13376 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13377 }
13378 determine_visibility (r);
13379 }
13380
13381 if (!local_p)
13382 {
13383 /* A static data member declaration is always marked
13384 external when it is declared in-class, even if an
13385 initializer is present. We mimic the non-template
13386 processing here. */
13387 DECL_EXTERNAL (r) = 1;
13388 if (DECL_NAMESPACE_SCOPE_P (t))
13389 DECL_NOT_REALLY_EXTERN (r) = 1;
13390
13391 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13392 SET_DECL_IMPLICIT_INSTANTIATION (r);
13393 register_specialization (r, gen_tmpl, argvec, false, hash);
13394 }
13395 else
13396 {
13397 if (DECL_LANG_SPECIFIC (r))
13398 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13399 if (!cp_unevaluated_operand)
13400 register_local_specialization (r, t);
13401 }
13402
13403 DECL_CHAIN (r) = NULL_TREE;
13404
13405 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13406 /*flags=*/0,
13407 args, complain, in_decl);
13408
13409 /* Preserve a typedef that names a type. */
13410 if (is_typedef_decl (r) && type != error_mark_node)
13411 {
13412 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13413 set_underlying_type (r);
13414 if (TYPE_DECL_ALIAS_P (r))
13415 /* An alias template specialization can be dependent
13416 even if its underlying type is not. */
13417 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13418 }
13419
13420 layout_decl (r, 0);
13421 }
13422 break;
13423
13424 default:
13425 gcc_unreachable ();
13426 }
13427 #undef RETURN
13428
13429 out:
13430 /* Restore the file and line information. */
13431 input_location = saved_loc;
13432
13433 return r;
13434 }
13435
13436 /* Substitute into the ARG_TYPES of a function type.
13437 If END is a TREE_CHAIN, leave it and any following types
13438 un-substituted. */
13439
13440 static tree
13441 tsubst_arg_types (tree arg_types,
13442 tree args,
13443 tree end,
13444 tsubst_flags_t complain,
13445 tree in_decl)
13446 {
13447 tree remaining_arg_types;
13448 tree type = NULL_TREE;
13449 int i = 1;
13450 tree expanded_args = NULL_TREE;
13451 tree default_arg;
13452
13453 if (!arg_types || arg_types == void_list_node || arg_types == end)
13454 return arg_types;
13455
13456 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13457 args, end, complain, in_decl);
13458 if (remaining_arg_types == error_mark_node)
13459 return error_mark_node;
13460
13461 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13462 {
13463 /* For a pack expansion, perform substitution on the
13464 entire expression. Later on, we'll handle the arguments
13465 one-by-one. */
13466 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13467 args, complain, in_decl);
13468
13469 if (TREE_CODE (expanded_args) == TREE_VEC)
13470 /* So that we'll spin through the parameters, one by one. */
13471 i = TREE_VEC_LENGTH (expanded_args);
13472 else
13473 {
13474 /* We only partially substituted into the parameter
13475 pack. Our type is TYPE_PACK_EXPANSION. */
13476 type = expanded_args;
13477 expanded_args = NULL_TREE;
13478 }
13479 }
13480
13481 while (i > 0) {
13482 --i;
13483
13484 if (expanded_args)
13485 type = TREE_VEC_ELT (expanded_args, i);
13486 else if (!type)
13487 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13488
13489 if (type == error_mark_node)
13490 return error_mark_node;
13491 if (VOID_TYPE_P (type))
13492 {
13493 if (complain & tf_error)
13494 {
13495 error ("invalid parameter type %qT", type);
13496 if (in_decl)
13497 error ("in declaration %q+D", in_decl);
13498 }
13499 return error_mark_node;
13500 }
13501 /* DR 657. */
13502 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13503 return error_mark_node;
13504
13505 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13506 top-level qualifiers as required. */
13507 type = cv_unqualified (type_decays_to (type));
13508
13509 /* We do not substitute into default arguments here. The standard
13510 mandates that they be instantiated only when needed, which is
13511 done in build_over_call. */
13512 default_arg = TREE_PURPOSE (arg_types);
13513
13514 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13515 since the new op() won't have any associated template arguments for us
13516 to refer to later. */
13517 if (lambda_fn_in_template_p (in_decl))
13518 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13519 false/*fn*/, false/*constexpr*/);
13520
13521 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13522 {
13523 /* We've instantiated a template before its default arguments
13524 have been parsed. This can happen for a nested template
13525 class, and is not an error unless we require the default
13526 argument in a call of this function. */
13527 remaining_arg_types =
13528 tree_cons (default_arg, type, remaining_arg_types);
13529 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13530 }
13531 else
13532 remaining_arg_types =
13533 hash_tree_cons (default_arg, type, remaining_arg_types);
13534 }
13535
13536 return remaining_arg_types;
13537 }
13538
13539 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13540 *not* handle the exception-specification for FNTYPE, because the
13541 initial substitution of explicitly provided template parameters
13542 during argument deduction forbids substitution into the
13543 exception-specification:
13544
13545 [temp.deduct]
13546
13547 All references in the function type of the function template to the
13548 corresponding template parameters are replaced by the specified tem-
13549 plate argument values. If a substitution in a template parameter or
13550 in the function type of the function template results in an invalid
13551 type, type deduction fails. [Note: The equivalent substitution in
13552 exception specifications is done only when the function is instanti-
13553 ated, at which point a program is ill-formed if the substitution
13554 results in an invalid type.] */
13555
13556 static tree
13557 tsubst_function_type (tree t,
13558 tree args,
13559 tsubst_flags_t complain,
13560 tree in_decl)
13561 {
13562 tree return_type;
13563 tree arg_types = NULL_TREE;
13564 tree fntype;
13565
13566 /* The TYPE_CONTEXT is not used for function/method types. */
13567 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13568
13569 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13570 failure. */
13571 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13572
13573 if (late_return_type_p)
13574 {
13575 /* Substitute the argument types. */
13576 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13577 complain, in_decl);
13578 if (arg_types == error_mark_node)
13579 return error_mark_node;
13580
13581 tree save_ccp = current_class_ptr;
13582 tree save_ccr = current_class_ref;
13583 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13584 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13585 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13586 if (do_inject)
13587 {
13588 /* DR 1207: 'this' is in scope in the trailing return type. */
13589 inject_this_parameter (this_type, cp_type_quals (this_type));
13590 }
13591
13592 /* Substitute the return type. */
13593 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13594
13595 if (do_inject)
13596 {
13597 current_class_ptr = save_ccp;
13598 current_class_ref = save_ccr;
13599 }
13600 }
13601 else
13602 /* Substitute the return type. */
13603 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13604
13605 if (return_type == error_mark_node)
13606 return error_mark_node;
13607 /* DR 486 clarifies that creation of a function type with an
13608 invalid return type is a deduction failure. */
13609 if (TREE_CODE (return_type) == ARRAY_TYPE
13610 || TREE_CODE (return_type) == FUNCTION_TYPE)
13611 {
13612 if (complain & tf_error)
13613 {
13614 if (TREE_CODE (return_type) == ARRAY_TYPE)
13615 error ("function returning an array");
13616 else
13617 error ("function returning a function");
13618 }
13619 return error_mark_node;
13620 }
13621 /* And DR 657. */
13622 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13623 return error_mark_node;
13624
13625 if (!late_return_type_p)
13626 {
13627 /* Substitute the argument types. */
13628 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13629 complain, in_decl);
13630 if (arg_types == error_mark_node)
13631 return error_mark_node;
13632 }
13633
13634 /* Construct a new type node and return it. */
13635 if (TREE_CODE (t) == FUNCTION_TYPE)
13636 {
13637 fntype = build_function_type (return_type, arg_types);
13638 fntype = apply_memfn_quals (fntype,
13639 type_memfn_quals (t),
13640 type_memfn_rqual (t));
13641 }
13642 else
13643 {
13644 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13645 /* Don't pick up extra function qualifiers from the basetype. */
13646 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13647 if (! MAYBE_CLASS_TYPE_P (r))
13648 {
13649 /* [temp.deduct]
13650
13651 Type deduction may fail for any of the following
13652 reasons:
13653
13654 -- Attempting to create "pointer to member of T" when T
13655 is not a class type. */
13656 if (complain & tf_error)
13657 error ("creating pointer to member function of non-class type %qT",
13658 r);
13659 return error_mark_node;
13660 }
13661
13662 fntype = build_method_type_directly (r, return_type,
13663 TREE_CHAIN (arg_types));
13664 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13665 }
13666 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13667
13668 if (late_return_type_p)
13669 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13670
13671 return fntype;
13672 }
13673
13674 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13675 ARGS into that specification, and return the substituted
13676 specification. If there is no specification, return NULL_TREE. */
13677
13678 static tree
13679 tsubst_exception_specification (tree fntype,
13680 tree args,
13681 tsubst_flags_t complain,
13682 tree in_decl,
13683 bool defer_ok)
13684 {
13685 tree specs;
13686 tree new_specs;
13687
13688 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13689 new_specs = NULL_TREE;
13690 if (specs && TREE_PURPOSE (specs))
13691 {
13692 /* A noexcept-specifier. */
13693 tree expr = TREE_PURPOSE (specs);
13694 if (TREE_CODE (expr) == INTEGER_CST)
13695 new_specs = expr;
13696 else if (defer_ok)
13697 {
13698 /* Defer instantiation of noexcept-specifiers to avoid
13699 excessive instantiations (c++/49107). */
13700 new_specs = make_node (DEFERRED_NOEXCEPT);
13701 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13702 {
13703 /* We already partially instantiated this member template,
13704 so combine the new args with the old. */
13705 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13706 = DEFERRED_NOEXCEPT_PATTERN (expr);
13707 DEFERRED_NOEXCEPT_ARGS (new_specs)
13708 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13709 }
13710 else
13711 {
13712 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13713 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13714 }
13715 }
13716 else
13717 new_specs = tsubst_copy_and_build
13718 (expr, args, complain, in_decl, /*function_p=*/false,
13719 /*integral_constant_expression_p=*/true);
13720 new_specs = build_noexcept_spec (new_specs, complain);
13721 }
13722 else if (specs)
13723 {
13724 if (! TREE_VALUE (specs))
13725 new_specs = specs;
13726 else
13727 while (specs)
13728 {
13729 tree spec;
13730 int i, len = 1;
13731 tree expanded_specs = NULL_TREE;
13732
13733 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13734 {
13735 /* Expand the pack expansion type. */
13736 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13737 args, complain,
13738 in_decl);
13739
13740 if (expanded_specs == error_mark_node)
13741 return error_mark_node;
13742 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13743 len = TREE_VEC_LENGTH (expanded_specs);
13744 else
13745 {
13746 /* We're substituting into a member template, so
13747 we got a TYPE_PACK_EXPANSION back. Add that
13748 expansion and move on. */
13749 gcc_assert (TREE_CODE (expanded_specs)
13750 == TYPE_PACK_EXPANSION);
13751 new_specs = add_exception_specifier (new_specs,
13752 expanded_specs,
13753 complain);
13754 specs = TREE_CHAIN (specs);
13755 continue;
13756 }
13757 }
13758
13759 for (i = 0; i < len; ++i)
13760 {
13761 if (expanded_specs)
13762 spec = TREE_VEC_ELT (expanded_specs, i);
13763 else
13764 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13765 if (spec == error_mark_node)
13766 return spec;
13767 new_specs = add_exception_specifier (new_specs, spec,
13768 complain);
13769 }
13770
13771 specs = TREE_CHAIN (specs);
13772 }
13773 }
13774 return new_specs;
13775 }
13776
13777 /* Take the tree structure T and replace template parameters used
13778 therein with the argument vector ARGS. IN_DECL is an associated
13779 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13780 Issue error and warning messages under control of COMPLAIN. Note
13781 that we must be relatively non-tolerant of extensions here, in
13782 order to preserve conformance; if we allow substitutions that
13783 should not be allowed, we may allow argument deductions that should
13784 not succeed, and therefore report ambiguous overload situations
13785 where there are none. In theory, we could allow the substitution,
13786 but indicate that it should have failed, and allow our caller to
13787 make sure that the right thing happens, but we don't try to do this
13788 yet.
13789
13790 This function is used for dealing with types, decls and the like;
13791 for expressions, use tsubst_expr or tsubst_copy. */
13792
13793 tree
13794 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13795 {
13796 enum tree_code code;
13797 tree type, r = NULL_TREE;
13798
13799 if (t == NULL_TREE || t == error_mark_node
13800 || t == integer_type_node
13801 || t == void_type_node
13802 || t == char_type_node
13803 || t == unknown_type_node
13804 || TREE_CODE (t) == NAMESPACE_DECL
13805 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13806 return t;
13807
13808 if (DECL_P (t))
13809 return tsubst_decl (t, args, complain);
13810
13811 if (args == NULL_TREE)
13812 return t;
13813
13814 code = TREE_CODE (t);
13815
13816 if (code == IDENTIFIER_NODE)
13817 type = IDENTIFIER_TYPE_VALUE (t);
13818 else
13819 type = TREE_TYPE (t);
13820
13821 gcc_assert (type != unknown_type_node);
13822
13823 /* Reuse typedefs. We need to do this to handle dependent attributes,
13824 such as attribute aligned. */
13825 if (TYPE_P (t)
13826 && typedef_variant_p (t))
13827 {
13828 tree decl = TYPE_NAME (t);
13829
13830 if (alias_template_specialization_p (t))
13831 {
13832 /* DECL represents an alias template and we want to
13833 instantiate it. */
13834 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13835 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13836 r = instantiate_alias_template (tmpl, gen_args, complain);
13837 }
13838 else if (DECL_CLASS_SCOPE_P (decl)
13839 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13840 && uses_template_parms (DECL_CONTEXT (decl)))
13841 {
13842 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13843 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13844 r = retrieve_specialization (tmpl, gen_args, 0);
13845 }
13846 else if (DECL_FUNCTION_SCOPE_P (decl)
13847 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13848 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13849 r = retrieve_local_specialization (decl);
13850 else
13851 /* The typedef is from a non-template context. */
13852 return t;
13853
13854 if (r)
13855 {
13856 r = TREE_TYPE (r);
13857 r = cp_build_qualified_type_real
13858 (r, cp_type_quals (t) | cp_type_quals (r),
13859 complain | tf_ignore_bad_quals);
13860 return r;
13861 }
13862 else
13863 {
13864 /* We don't have an instantiation yet, so drop the typedef. */
13865 int quals = cp_type_quals (t);
13866 t = DECL_ORIGINAL_TYPE (decl);
13867 t = cp_build_qualified_type_real (t, quals,
13868 complain | tf_ignore_bad_quals);
13869 }
13870 }
13871
13872 bool fndecl_type = (complain & tf_fndecl_type);
13873 complain &= ~tf_fndecl_type;
13874
13875 if (type
13876 && code != TYPENAME_TYPE
13877 && code != TEMPLATE_TYPE_PARM
13878 && code != TEMPLATE_PARM_INDEX
13879 && code != IDENTIFIER_NODE
13880 && code != FUNCTION_TYPE
13881 && code != METHOD_TYPE)
13882 type = tsubst (type, args, complain, in_decl);
13883 if (type == error_mark_node)
13884 return error_mark_node;
13885
13886 switch (code)
13887 {
13888 case RECORD_TYPE:
13889 case UNION_TYPE:
13890 case ENUMERAL_TYPE:
13891 return tsubst_aggr_type (t, args, complain, in_decl,
13892 /*entering_scope=*/0);
13893
13894 case ERROR_MARK:
13895 case IDENTIFIER_NODE:
13896 case VOID_TYPE:
13897 case REAL_TYPE:
13898 case COMPLEX_TYPE:
13899 case VECTOR_TYPE:
13900 case BOOLEAN_TYPE:
13901 case NULLPTR_TYPE:
13902 case LANG_TYPE:
13903 return t;
13904
13905 case INTEGER_TYPE:
13906 if (t == integer_type_node)
13907 return t;
13908
13909 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13910 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13911 return t;
13912
13913 {
13914 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13915
13916 max = tsubst_expr (omax, args, complain, in_decl,
13917 /*integral_constant_expression_p=*/false);
13918
13919 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13920 needed. */
13921 if (TREE_CODE (max) == NOP_EXPR
13922 && TREE_SIDE_EFFECTS (omax)
13923 && !TREE_TYPE (max))
13924 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13925
13926 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13927 with TREE_SIDE_EFFECTS that indicates this is not an integral
13928 constant expression. */
13929 if (processing_template_decl
13930 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13931 {
13932 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13933 TREE_SIDE_EFFECTS (max) = 1;
13934 }
13935
13936 return compute_array_index_type (NULL_TREE, max, complain);
13937 }
13938
13939 case TEMPLATE_TYPE_PARM:
13940 case TEMPLATE_TEMPLATE_PARM:
13941 case BOUND_TEMPLATE_TEMPLATE_PARM:
13942 case TEMPLATE_PARM_INDEX:
13943 {
13944 int idx;
13945 int level;
13946 int levels;
13947 tree arg = NULL_TREE;
13948
13949 /* Early in template argument deduction substitution, we don't
13950 want to reduce the level of 'auto', or it will be confused
13951 with a normal template parm in subsequent deduction. */
13952 if (is_auto (t) && (complain & tf_partial))
13953 return t;
13954
13955 r = NULL_TREE;
13956
13957 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13958 template_parm_level_and_index (t, &level, &idx);
13959
13960 levels = TMPL_ARGS_DEPTH (args);
13961 if (level <= levels
13962 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13963 {
13964 arg = TMPL_ARG (args, level, idx);
13965
13966 /* See through ARGUMENT_PACK_SELECT arguments. */
13967 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13968 arg = argument_pack_select_arg (arg);
13969 }
13970
13971 if (arg == error_mark_node)
13972 return error_mark_node;
13973 else if (arg != NULL_TREE)
13974 {
13975 if (ARGUMENT_PACK_P (arg))
13976 /* If ARG is an argument pack, we don't actually want to
13977 perform a substitution here, because substitutions
13978 for argument packs are only done
13979 element-by-element. We can get to this point when
13980 substituting the type of a non-type template
13981 parameter pack, when that type actually contains
13982 template parameter packs from an outer template, e.g.,
13983
13984 template<typename... Types> struct A {
13985 template<Types... Values> struct B { };
13986 }; */
13987 return t;
13988
13989 if (code == TEMPLATE_TYPE_PARM)
13990 {
13991 int quals;
13992 gcc_assert (TYPE_P (arg));
13993
13994 quals = cp_type_quals (arg) | cp_type_quals (t);
13995
13996 return cp_build_qualified_type_real
13997 (arg, quals, complain | tf_ignore_bad_quals);
13998 }
13999 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14000 {
14001 /* We are processing a type constructed from a
14002 template template parameter. */
14003 tree argvec = tsubst (TYPE_TI_ARGS (t),
14004 args, complain, in_decl);
14005 if (argvec == error_mark_node)
14006 return error_mark_node;
14007
14008 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14009 || TREE_CODE (arg) == TEMPLATE_DECL
14010 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14011
14012 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14013 /* Consider this code:
14014
14015 template <template <class> class Template>
14016 struct Internal {
14017 template <class Arg> using Bind = Template<Arg>;
14018 };
14019
14020 template <template <class> class Template, class Arg>
14021 using Instantiate = Template<Arg>; //#0
14022
14023 template <template <class> class Template,
14024 class Argument>
14025 using Bind =
14026 Instantiate<Internal<Template>::template Bind,
14027 Argument>; //#1
14028
14029 When #1 is parsed, the
14030 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14031 parameter `Template' in #0 matches the
14032 UNBOUND_CLASS_TEMPLATE representing the argument
14033 `Internal<Template>::template Bind'; We then want
14034 to assemble the type `Bind<Argument>' that can't
14035 be fully created right now, because
14036 `Internal<Template>' not being complete, the Bind
14037 template cannot be looked up in that context. So
14038 we need to "store" `Bind<Argument>' for later
14039 when the context of Bind becomes complete. Let's
14040 store that in a TYPENAME_TYPE. */
14041 return make_typename_type (TYPE_CONTEXT (arg),
14042 build_nt (TEMPLATE_ID_EXPR,
14043 TYPE_IDENTIFIER (arg),
14044 argvec),
14045 typename_type,
14046 complain);
14047
14048 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14049 are resolving nested-types in the signature of a
14050 member function templates. Otherwise ARG is a
14051 TEMPLATE_DECL and is the real template to be
14052 instantiated. */
14053 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14054 arg = TYPE_NAME (arg);
14055
14056 r = lookup_template_class (arg,
14057 argvec, in_decl,
14058 DECL_CONTEXT (arg),
14059 /*entering_scope=*/0,
14060 complain);
14061 return cp_build_qualified_type_real
14062 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14063 }
14064 else if (code == TEMPLATE_TEMPLATE_PARM)
14065 return arg;
14066 else
14067 /* TEMPLATE_PARM_INDEX. */
14068 return convert_from_reference (unshare_expr (arg));
14069 }
14070
14071 if (level == 1)
14072 /* This can happen during the attempted tsubst'ing in
14073 unify. This means that we don't yet have any information
14074 about the template parameter in question. */
14075 return t;
14076
14077 /* If we get here, we must have been looking at a parm for a
14078 more deeply nested template. Make a new version of this
14079 template parameter, but with a lower level. */
14080 switch (code)
14081 {
14082 case TEMPLATE_TYPE_PARM:
14083 case TEMPLATE_TEMPLATE_PARM:
14084 case BOUND_TEMPLATE_TEMPLATE_PARM:
14085 if (cp_type_quals (t))
14086 {
14087 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14088 r = cp_build_qualified_type_real
14089 (r, cp_type_quals (t),
14090 complain | (code == TEMPLATE_TYPE_PARM
14091 ? tf_ignore_bad_quals : 0));
14092 }
14093 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14094 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14095 && (r = (TEMPLATE_PARM_DESCENDANTS
14096 (TEMPLATE_TYPE_PARM_INDEX (t))))
14097 && (r = TREE_TYPE (r))
14098 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14099 /* Break infinite recursion when substituting the constraints
14100 of a constrained placeholder. */;
14101 else
14102 {
14103 r = copy_type (t);
14104 TEMPLATE_TYPE_PARM_INDEX (r)
14105 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14106 r, levels, args, complain);
14107 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14108 TYPE_MAIN_VARIANT (r) = r;
14109 TYPE_POINTER_TO (r) = NULL_TREE;
14110 TYPE_REFERENCE_TO (r) = NULL_TREE;
14111
14112 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14113 {
14114 /* Propagate constraints on placeholders. */
14115 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14116 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14117 = tsubst_constraint (constr, args, complain, in_decl);
14118 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14119 {
14120 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
14121 pl = tsubst (pl, args, complain, in_decl);
14122 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14123 }
14124 }
14125
14126 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14127 /* We have reduced the level of the template
14128 template parameter, but not the levels of its
14129 template parameters, so canonical_type_parameter
14130 will not be able to find the canonical template
14131 template parameter for this level. Thus, we
14132 require structural equality checking to compare
14133 TEMPLATE_TEMPLATE_PARMs. */
14134 SET_TYPE_STRUCTURAL_EQUALITY (r);
14135 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14136 SET_TYPE_STRUCTURAL_EQUALITY (r);
14137 else
14138 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14139
14140 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14141 {
14142 tree tinfo = TYPE_TEMPLATE_INFO (t);
14143 /* We might need to substitute into the types of non-type
14144 template parameters. */
14145 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14146 complain, in_decl);
14147 if (tmpl == error_mark_node)
14148 return error_mark_node;
14149 tree argvec = tsubst (TI_ARGS (tinfo), args,
14150 complain, in_decl);
14151 if (argvec == error_mark_node)
14152 return error_mark_node;
14153
14154 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14155 = build_template_info (tmpl, argvec);
14156 }
14157 }
14158 break;
14159
14160 case TEMPLATE_PARM_INDEX:
14161 /* OK, now substitute the type of the non-type parameter. We
14162 couldn't do it earlier because it might be an auto parameter,
14163 and we wouldn't need to if we had an argument. */
14164 type = tsubst (type, args, complain, in_decl);
14165 if (type == error_mark_node)
14166 return error_mark_node;
14167 r = reduce_template_parm_level (t, type, levels, args, complain);
14168 break;
14169
14170 default:
14171 gcc_unreachable ();
14172 }
14173
14174 return r;
14175 }
14176
14177 case TREE_LIST:
14178 {
14179 tree purpose, value, chain;
14180
14181 if (t == void_list_node)
14182 return t;
14183
14184 purpose = TREE_PURPOSE (t);
14185 if (purpose)
14186 {
14187 purpose = tsubst (purpose, args, complain, in_decl);
14188 if (purpose == error_mark_node)
14189 return error_mark_node;
14190 }
14191 value = TREE_VALUE (t);
14192 if (value)
14193 {
14194 value = tsubst (value, args, complain, in_decl);
14195 if (value == error_mark_node)
14196 return error_mark_node;
14197 }
14198 chain = TREE_CHAIN (t);
14199 if (chain && chain != void_type_node)
14200 {
14201 chain = tsubst (chain, args, complain, in_decl);
14202 if (chain == error_mark_node)
14203 return error_mark_node;
14204 }
14205 if (purpose == TREE_PURPOSE (t)
14206 && value == TREE_VALUE (t)
14207 && chain == TREE_CHAIN (t))
14208 return t;
14209 return hash_tree_cons (purpose, value, chain);
14210 }
14211
14212 case TREE_BINFO:
14213 /* We should never be tsubsting a binfo. */
14214 gcc_unreachable ();
14215
14216 case TREE_VEC:
14217 /* A vector of template arguments. */
14218 gcc_assert (!type);
14219 return tsubst_template_args (t, args, complain, in_decl);
14220
14221 case POINTER_TYPE:
14222 case REFERENCE_TYPE:
14223 {
14224 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14225 return t;
14226
14227 /* [temp.deduct]
14228
14229 Type deduction may fail for any of the following
14230 reasons:
14231
14232 -- Attempting to create a pointer to reference type.
14233 -- Attempting to create a reference to a reference type or
14234 a reference to void.
14235
14236 Core issue 106 says that creating a reference to a reference
14237 during instantiation is no longer a cause for failure. We
14238 only enforce this check in strict C++98 mode. */
14239 if ((TREE_CODE (type) == REFERENCE_TYPE
14240 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14241 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14242 {
14243 static location_t last_loc;
14244
14245 /* We keep track of the last time we issued this error
14246 message to avoid spewing a ton of messages during a
14247 single bad template instantiation. */
14248 if (complain & tf_error
14249 && last_loc != input_location)
14250 {
14251 if (VOID_TYPE_P (type))
14252 error ("forming reference to void");
14253 else if (code == POINTER_TYPE)
14254 error ("forming pointer to reference type %qT", type);
14255 else
14256 error ("forming reference to reference type %qT", type);
14257 last_loc = input_location;
14258 }
14259
14260 return error_mark_node;
14261 }
14262 else if (TREE_CODE (type) == FUNCTION_TYPE
14263 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14264 || type_memfn_rqual (type) != REF_QUAL_NONE))
14265 {
14266 if (complain & tf_error)
14267 {
14268 if (code == POINTER_TYPE)
14269 error ("forming pointer to qualified function type %qT",
14270 type);
14271 else
14272 error ("forming reference to qualified function type %qT",
14273 type);
14274 }
14275 return error_mark_node;
14276 }
14277 else if (code == POINTER_TYPE)
14278 {
14279 r = build_pointer_type (type);
14280 if (TREE_CODE (type) == METHOD_TYPE)
14281 r = build_ptrmemfunc_type (r);
14282 }
14283 else if (TREE_CODE (type) == REFERENCE_TYPE)
14284 /* In C++0x, during template argument substitution, when there is an
14285 attempt to create a reference to a reference type, reference
14286 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14287
14288 "If a template-argument for a template-parameter T names a type
14289 that is a reference to a type A, an attempt to create the type
14290 'lvalue reference to cv T' creates the type 'lvalue reference to
14291 A,' while an attempt to create the type type rvalue reference to
14292 cv T' creates the type T"
14293 */
14294 r = cp_build_reference_type
14295 (TREE_TYPE (type),
14296 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14297 else
14298 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14299 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14300
14301 if (r != error_mark_node)
14302 /* Will this ever be needed for TYPE_..._TO values? */
14303 layout_type (r);
14304
14305 return r;
14306 }
14307 case OFFSET_TYPE:
14308 {
14309 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14310 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14311 {
14312 /* [temp.deduct]
14313
14314 Type deduction may fail for any of the following
14315 reasons:
14316
14317 -- Attempting to create "pointer to member of T" when T
14318 is not a class type. */
14319 if (complain & tf_error)
14320 error ("creating pointer to member of non-class type %qT", r);
14321 return error_mark_node;
14322 }
14323 if (TREE_CODE (type) == REFERENCE_TYPE)
14324 {
14325 if (complain & tf_error)
14326 error ("creating pointer to member reference type %qT", type);
14327 return error_mark_node;
14328 }
14329 if (VOID_TYPE_P (type))
14330 {
14331 if (complain & tf_error)
14332 error ("creating pointer to member of type void");
14333 return error_mark_node;
14334 }
14335 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14336 if (TREE_CODE (type) == FUNCTION_TYPE)
14337 {
14338 /* The type of the implicit object parameter gets its
14339 cv-qualifiers from the FUNCTION_TYPE. */
14340 tree memptr;
14341 tree method_type
14342 = build_memfn_type (type, r, type_memfn_quals (type),
14343 type_memfn_rqual (type));
14344 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14345 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14346 complain);
14347 }
14348 else
14349 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14350 cp_type_quals (t),
14351 complain);
14352 }
14353 case FUNCTION_TYPE:
14354 case METHOD_TYPE:
14355 {
14356 tree fntype;
14357 tree specs;
14358 fntype = tsubst_function_type (t, args, complain, in_decl);
14359 if (fntype == error_mark_node)
14360 return error_mark_node;
14361
14362 /* Substitute the exception specification. */
14363 specs = tsubst_exception_specification (t, args, complain, in_decl,
14364 /*defer_ok*/fndecl_type);
14365 if (specs == error_mark_node)
14366 return error_mark_node;
14367 if (specs)
14368 fntype = build_exception_variant (fntype, specs);
14369 return fntype;
14370 }
14371 case ARRAY_TYPE:
14372 {
14373 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14374 if (domain == error_mark_node)
14375 return error_mark_node;
14376
14377 /* As an optimization, we avoid regenerating the array type if
14378 it will obviously be the same as T. */
14379 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14380 return t;
14381
14382 /* These checks should match the ones in create_array_type_for_decl.
14383
14384 [temp.deduct]
14385
14386 The deduction may fail for any of the following reasons:
14387
14388 -- Attempting to create an array with an element type that
14389 is void, a function type, or a reference type, or [DR337]
14390 an abstract class type. */
14391 if (VOID_TYPE_P (type)
14392 || TREE_CODE (type) == FUNCTION_TYPE
14393 || (TREE_CODE (type) == ARRAY_TYPE
14394 && TYPE_DOMAIN (type) == NULL_TREE)
14395 || TREE_CODE (type) == REFERENCE_TYPE)
14396 {
14397 if (complain & tf_error)
14398 error ("creating array of %qT", type);
14399 return error_mark_node;
14400 }
14401
14402 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14403 return error_mark_node;
14404
14405 r = build_cplus_array_type (type, domain);
14406
14407 if (TYPE_USER_ALIGN (t))
14408 {
14409 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14410 TYPE_USER_ALIGN (r) = 1;
14411 }
14412
14413 return r;
14414 }
14415
14416 case TYPENAME_TYPE:
14417 {
14418 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14419 in_decl, /*entering_scope=*/1);
14420 if (ctx == error_mark_node)
14421 return error_mark_node;
14422
14423 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14424 complain, in_decl);
14425 if (f == error_mark_node)
14426 return error_mark_node;
14427
14428 if (!MAYBE_CLASS_TYPE_P (ctx))
14429 {
14430 if (complain & tf_error)
14431 error ("%qT is not a class, struct, or union type", ctx);
14432 return error_mark_node;
14433 }
14434 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14435 {
14436 /* Normally, make_typename_type does not require that the CTX
14437 have complete type in order to allow things like:
14438
14439 template <class T> struct S { typename S<T>::X Y; };
14440
14441 But, such constructs have already been resolved by this
14442 point, so here CTX really should have complete type, unless
14443 it's a partial instantiation. */
14444 ctx = complete_type (ctx);
14445 if (!COMPLETE_TYPE_P (ctx))
14446 {
14447 if (complain & tf_error)
14448 cxx_incomplete_type_error (NULL_TREE, ctx);
14449 return error_mark_node;
14450 }
14451 }
14452
14453 f = make_typename_type (ctx, f, typename_type,
14454 complain | tf_keep_type_decl);
14455 if (f == error_mark_node)
14456 return f;
14457 if (TREE_CODE (f) == TYPE_DECL)
14458 {
14459 complain |= tf_ignore_bad_quals;
14460 f = TREE_TYPE (f);
14461 }
14462
14463 if (TREE_CODE (f) != TYPENAME_TYPE)
14464 {
14465 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14466 {
14467 if (complain & tf_error)
14468 error ("%qT resolves to %qT, which is not an enumeration type",
14469 t, f);
14470 else
14471 return error_mark_node;
14472 }
14473 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14474 {
14475 if (complain & tf_error)
14476 error ("%qT resolves to %qT, which is is not a class type",
14477 t, f);
14478 else
14479 return error_mark_node;
14480 }
14481 }
14482
14483 return cp_build_qualified_type_real
14484 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14485 }
14486
14487 case UNBOUND_CLASS_TEMPLATE:
14488 {
14489 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14490 in_decl, /*entering_scope=*/1);
14491 tree name = TYPE_IDENTIFIER (t);
14492 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14493
14494 if (ctx == error_mark_node || name == error_mark_node)
14495 return error_mark_node;
14496
14497 if (parm_list)
14498 parm_list = tsubst_template_parms (parm_list, args, complain);
14499 return make_unbound_class_template (ctx, name, parm_list, complain);
14500 }
14501
14502 case TYPEOF_TYPE:
14503 {
14504 tree type;
14505
14506 ++cp_unevaluated_operand;
14507 ++c_inhibit_evaluation_warnings;
14508
14509 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14510 complain, in_decl,
14511 /*integral_constant_expression_p=*/false);
14512
14513 --cp_unevaluated_operand;
14514 --c_inhibit_evaluation_warnings;
14515
14516 type = finish_typeof (type);
14517 return cp_build_qualified_type_real (type,
14518 cp_type_quals (t)
14519 | cp_type_quals (type),
14520 complain);
14521 }
14522
14523 case DECLTYPE_TYPE:
14524 {
14525 tree type;
14526
14527 ++cp_unevaluated_operand;
14528 ++c_inhibit_evaluation_warnings;
14529
14530 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14531 complain|tf_decltype, in_decl,
14532 /*function_p*/false,
14533 /*integral_constant_expression*/false);
14534
14535 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14536 {
14537 if (type == NULL_TREE)
14538 {
14539 if (complain & tf_error)
14540 error ("empty initializer in lambda init-capture");
14541 type = error_mark_node;
14542 }
14543 else if (TREE_CODE (type) == TREE_LIST)
14544 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14545 }
14546
14547 --cp_unevaluated_operand;
14548 --c_inhibit_evaluation_warnings;
14549
14550 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14551 type = lambda_capture_field_type (type,
14552 DECLTYPE_FOR_INIT_CAPTURE (t),
14553 DECLTYPE_FOR_REF_CAPTURE (t));
14554 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14555 type = lambda_proxy_type (type);
14556 else
14557 {
14558 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14559 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14560 && EXPR_P (type))
14561 /* In a template ~id could be either a complement expression
14562 or an unqualified-id naming a destructor; if instantiating
14563 it produces an expression, it's not an id-expression or
14564 member access. */
14565 id = false;
14566 type = finish_decltype_type (type, id, complain);
14567 }
14568 return cp_build_qualified_type_real (type,
14569 cp_type_quals (t)
14570 | cp_type_quals (type),
14571 complain | tf_ignore_bad_quals);
14572 }
14573
14574 case UNDERLYING_TYPE:
14575 {
14576 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14577 complain, in_decl);
14578 return finish_underlying_type (type);
14579 }
14580
14581 case TYPE_ARGUMENT_PACK:
14582 case NONTYPE_ARGUMENT_PACK:
14583 {
14584 tree r;
14585
14586 if (code == NONTYPE_ARGUMENT_PACK)
14587 r = make_node (code);
14588 else
14589 r = cxx_make_type (code);
14590
14591 tree pack_args = ARGUMENT_PACK_ARGS (t);
14592 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14593 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14594
14595 return r;
14596 }
14597
14598 case VOID_CST:
14599 case INTEGER_CST:
14600 case REAL_CST:
14601 case STRING_CST:
14602 case PLUS_EXPR:
14603 case MINUS_EXPR:
14604 case NEGATE_EXPR:
14605 case NOP_EXPR:
14606 case INDIRECT_REF:
14607 case ADDR_EXPR:
14608 case CALL_EXPR:
14609 case ARRAY_REF:
14610 case SCOPE_REF:
14611 /* We should use one of the expression tsubsts for these codes. */
14612 gcc_unreachable ();
14613
14614 default:
14615 sorry ("use of %qs in template", get_tree_code_name (code));
14616 return error_mark_node;
14617 }
14618 }
14619
14620 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14621 expression on the left-hand side of the "." or "->" operator. We
14622 only do the lookup if we had a dependent BASELINK. Otherwise we
14623 adjust it onto the instantiated heirarchy. */
14624
14625 static tree
14626 tsubst_baselink (tree baselink, tree object_type,
14627 tree args, tsubst_flags_t complain, tree in_decl)
14628 {
14629 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
14630 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14631 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14632
14633 tree optype = BASELINK_OPTYPE (baselink);
14634 optype = tsubst (optype, args, complain, in_decl);
14635
14636 tree template_args = NULL_TREE;
14637 bool template_id_p = false;
14638 tree fns = BASELINK_FUNCTIONS (baselink);
14639 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14640 {
14641 template_id_p = true;
14642 template_args = TREE_OPERAND (fns, 1);
14643 fns = TREE_OPERAND (fns, 0);
14644 if (template_args)
14645 template_args = tsubst_template_args (template_args, args,
14646 complain, in_decl);
14647 }
14648
14649 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
14650 binfo_type = tsubst (binfo_type, args, complain, in_decl);
14651 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
14652
14653 if (dependent_p)
14654 {
14655 tree name = OVL_NAME (fns);
14656 if (IDENTIFIER_CONV_OP_P (name))
14657 name = make_conv_op_name (optype);
14658
14659 if (name == complete_dtor_identifier)
14660 /* Treat as-if non-dependent below. */
14661 dependent_p = false;
14662
14663 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14664 if (!baselink)
14665 {
14666 if ((complain & tf_error)
14667 && constructor_name_p (name, qualifying_scope))
14668 error ("cannot call constructor %<%T::%D%> directly",
14669 qualifying_scope, name);
14670 return error_mark_node;
14671 }
14672
14673 if (BASELINK_P (baselink))
14674 fns = BASELINK_FUNCTIONS (baselink);
14675 }
14676 else
14677 /* We're going to overwrite pieces below, make a duplicate. */
14678 baselink = copy_node (baselink);
14679
14680 /* If lookup found a single function, mark it as used at this point.
14681 (If lookup found multiple functions the one selected later by
14682 overload resolution will be marked as used at that point.) */
14683 if (!template_id_p && !really_overloaded_fn (fns)
14684 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14685 return error_mark_node;
14686
14687 if (BASELINK_P (baselink))
14688 {
14689 /* Add back the template arguments, if present. */
14690 if (template_id_p)
14691 BASELINK_FUNCTIONS (baselink)
14692 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
14693
14694 /* Update the conversion operator type. */
14695 BASELINK_OPTYPE (baselink) = optype;
14696 }
14697
14698 if (!object_type)
14699 object_type = current_class_type;
14700
14701 if (qualified_p || !dependent_p)
14702 {
14703 baselink = adjust_result_of_qualified_name_lookup (baselink,
14704 qualifying_scope,
14705 object_type);
14706 if (!qualified_p)
14707 /* We need to call adjust_result_of_qualified_name_lookup in case the
14708 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14709 so that we still get virtual function binding. */
14710 BASELINK_QUALIFIED_P (baselink) = false;
14711 }
14712
14713 return baselink;
14714 }
14715
14716 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14717 true if the qualified-id will be a postfix-expression in-and-of
14718 itself; false if more of the postfix-expression follows the
14719 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14720 of "&". */
14721
14722 static tree
14723 tsubst_qualified_id (tree qualified_id, tree args,
14724 tsubst_flags_t complain, tree in_decl,
14725 bool done, bool address_p)
14726 {
14727 tree expr;
14728 tree scope;
14729 tree name;
14730 bool is_template;
14731 tree template_args;
14732 location_t loc = UNKNOWN_LOCATION;
14733
14734 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14735
14736 /* Figure out what name to look up. */
14737 name = TREE_OPERAND (qualified_id, 1);
14738 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14739 {
14740 is_template = true;
14741 loc = EXPR_LOCATION (name);
14742 template_args = TREE_OPERAND (name, 1);
14743 if (template_args)
14744 template_args = tsubst_template_args (template_args, args,
14745 complain, in_decl);
14746 if (template_args == error_mark_node)
14747 return error_mark_node;
14748 name = TREE_OPERAND (name, 0);
14749 }
14750 else
14751 {
14752 is_template = false;
14753 template_args = NULL_TREE;
14754 }
14755
14756 /* Substitute into the qualifying scope. When there are no ARGS, we
14757 are just trying to simplify a non-dependent expression. In that
14758 case the qualifying scope may be dependent, and, in any case,
14759 substituting will not help. */
14760 scope = TREE_OPERAND (qualified_id, 0);
14761 if (args)
14762 {
14763 scope = tsubst (scope, args, complain, in_decl);
14764 expr = tsubst_copy (name, args, complain, in_decl);
14765 }
14766 else
14767 expr = name;
14768
14769 if (dependent_scope_p (scope))
14770 {
14771 if (is_template)
14772 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14773 tree r = build_qualified_name (NULL_TREE, scope, expr,
14774 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14775 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14776 return r;
14777 }
14778
14779 if (!BASELINK_P (name) && !DECL_P (expr))
14780 {
14781 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14782 {
14783 /* A BIT_NOT_EXPR is used to represent a destructor. */
14784 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14785 {
14786 error ("qualifying type %qT does not match destructor name ~%qT",
14787 scope, TREE_OPERAND (expr, 0));
14788 expr = error_mark_node;
14789 }
14790 else
14791 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14792 /*is_type_p=*/0, false);
14793 }
14794 else
14795 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14796 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14797 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14798 {
14799 if (complain & tf_error)
14800 {
14801 error ("dependent-name %qE is parsed as a non-type, but "
14802 "instantiation yields a type", qualified_id);
14803 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14804 }
14805 return error_mark_node;
14806 }
14807 }
14808
14809 if (DECL_P (expr))
14810 {
14811 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14812 scope);
14813 /* Remember that there was a reference to this entity. */
14814 if (!mark_used (expr, complain) && !(complain & tf_error))
14815 return error_mark_node;
14816 }
14817
14818 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14819 {
14820 if (complain & tf_error)
14821 qualified_name_lookup_error (scope,
14822 TREE_OPERAND (qualified_id, 1),
14823 expr, input_location);
14824 return error_mark_node;
14825 }
14826
14827 if (is_template)
14828 {
14829 if (variable_template_p (expr))
14830 expr = lookup_and_finish_template_variable (expr, template_args,
14831 complain);
14832 else
14833 expr = lookup_template_function (expr, template_args);
14834 }
14835
14836 if (expr == error_mark_node && complain & tf_error)
14837 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14838 expr, input_location);
14839 else if (TYPE_P (scope))
14840 {
14841 expr = (adjust_result_of_qualified_name_lookup
14842 (expr, scope, current_nonlambda_class_type ()));
14843 expr = (finish_qualified_id_expr
14844 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14845 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14846 /*template_arg_p=*/false, complain));
14847 }
14848
14849 /* Expressions do not generally have reference type. */
14850 if (TREE_CODE (expr) != SCOPE_REF
14851 /* However, if we're about to form a pointer-to-member, we just
14852 want the referenced member referenced. */
14853 && TREE_CODE (expr) != OFFSET_REF)
14854 expr = convert_from_reference (expr);
14855
14856 if (REF_PARENTHESIZED_P (qualified_id))
14857 expr = force_paren_expr (expr);
14858
14859 return expr;
14860 }
14861
14862 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14863 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14864 for tsubst. */
14865
14866 static tree
14867 tsubst_init (tree init, tree decl, tree args,
14868 tsubst_flags_t complain, tree in_decl)
14869 {
14870 if (!init)
14871 return NULL_TREE;
14872
14873 init = tsubst_expr (init, args, complain, in_decl, false);
14874
14875 if (!init && TREE_TYPE (decl) != error_mark_node)
14876 {
14877 /* If we had an initializer but it
14878 instantiated to nothing,
14879 value-initialize the object. This will
14880 only occur when the initializer was a
14881 pack expansion where the parameter packs
14882 used in that expansion were of length
14883 zero. */
14884 init = build_value_init (TREE_TYPE (decl),
14885 complain);
14886 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14887 init = get_target_expr_sfinae (init, complain);
14888 if (TREE_CODE (init) == TARGET_EXPR)
14889 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14890 }
14891
14892 return init;
14893 }
14894
14895 /* Like tsubst, but deals with expressions. This function just replaces
14896 template parms; to finish processing the resultant expression, use
14897 tsubst_copy_and_build or tsubst_expr. */
14898
14899 static tree
14900 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14901 {
14902 enum tree_code code;
14903 tree r;
14904
14905 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14906 return t;
14907
14908 code = TREE_CODE (t);
14909
14910 switch (code)
14911 {
14912 case PARM_DECL:
14913 r = retrieve_local_specialization (t);
14914
14915 if (r == NULL_TREE)
14916 {
14917 /* We get here for a use of 'this' in an NSDMI. */
14918 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14919 return current_class_ptr;
14920
14921 /* This can happen for a parameter name used later in a function
14922 declaration (such as in a late-specified return type). Just
14923 make a dummy decl, since it's only used for its type. */
14924 gcc_assert (cp_unevaluated_operand != 0);
14925 r = tsubst_decl (t, args, complain);
14926 /* Give it the template pattern as its context; its true context
14927 hasn't been instantiated yet and this is good enough for
14928 mangling. */
14929 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14930 }
14931
14932 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14933 r = argument_pack_select_arg (r);
14934 if (!mark_used (r, complain) && !(complain & tf_error))
14935 return error_mark_node;
14936 return r;
14937
14938 case CONST_DECL:
14939 {
14940 tree enum_type;
14941 tree v;
14942
14943 if (DECL_TEMPLATE_PARM_P (t))
14944 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14945 /* There is no need to substitute into namespace-scope
14946 enumerators. */
14947 if (DECL_NAMESPACE_SCOPE_P (t))
14948 return t;
14949 /* If ARGS is NULL, then T is known to be non-dependent. */
14950 if (args == NULL_TREE)
14951 return scalar_constant_value (t);
14952
14953 /* Unfortunately, we cannot just call lookup_name here.
14954 Consider:
14955
14956 template <int I> int f() {
14957 enum E { a = I };
14958 struct S { void g() { E e = a; } };
14959 };
14960
14961 When we instantiate f<7>::S::g(), say, lookup_name is not
14962 clever enough to find f<7>::a. */
14963 enum_type
14964 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14965 /*entering_scope=*/0);
14966
14967 for (v = TYPE_VALUES (enum_type);
14968 v != NULL_TREE;
14969 v = TREE_CHAIN (v))
14970 if (TREE_PURPOSE (v) == DECL_NAME (t))
14971 return TREE_VALUE (v);
14972
14973 /* We didn't find the name. That should never happen; if
14974 name-lookup found it during preliminary parsing, we
14975 should find it again here during instantiation. */
14976 gcc_unreachable ();
14977 }
14978 return t;
14979
14980 case FIELD_DECL:
14981 if (DECL_CONTEXT (t))
14982 {
14983 tree ctx;
14984
14985 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14986 /*entering_scope=*/1);
14987 if (ctx != DECL_CONTEXT (t))
14988 {
14989 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14990 if (!r)
14991 {
14992 if (complain & tf_error)
14993 error ("using invalid field %qD", t);
14994 return error_mark_node;
14995 }
14996 return r;
14997 }
14998 }
14999
15000 return t;
15001
15002 case VAR_DECL:
15003 case FUNCTION_DECL:
15004 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15005 r = tsubst (t, args, complain, in_decl);
15006 else if (local_variable_p (t)
15007 && uses_template_parms (DECL_CONTEXT (t)))
15008 {
15009 r = retrieve_local_specialization (t);
15010 if (r == NULL_TREE)
15011 {
15012 /* First try name lookup to find the instantiation. */
15013 r = lookup_name (DECL_NAME (t));
15014 if (r && !is_capture_proxy (r))
15015 {
15016 /* Make sure that the one we found is the one we want. */
15017 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15018 if (ctx != DECL_CONTEXT (r))
15019 r = NULL_TREE;
15020 }
15021
15022 if (r)
15023 /* OK */;
15024 else
15025 {
15026 /* This can happen for a variable used in a
15027 late-specified return type of a local lambda, or for a
15028 local static or constant. Building a new VAR_DECL
15029 should be OK in all those cases. */
15030 r = tsubst_decl (t, args, complain);
15031 if (local_specializations)
15032 /* Avoid infinite recursion (79640). */
15033 register_local_specialization (r, t);
15034 if (decl_maybe_constant_var_p (r))
15035 {
15036 /* We can't call cp_finish_decl, so handle the
15037 initializer by hand. */
15038 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15039 complain, in_decl);
15040 if (!processing_template_decl)
15041 init = maybe_constant_init (init);
15042 if (processing_template_decl
15043 ? potential_constant_expression (init)
15044 : reduced_constant_expression_p (init))
15045 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15046 = TREE_CONSTANT (r) = true;
15047 DECL_INITIAL (r) = init;
15048 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15049 TREE_TYPE (r)
15050 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15051 complain, adc_variable_type);
15052 }
15053 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15054 || decl_constant_var_p (r)
15055 || errorcount || sorrycount);
15056 if (!processing_template_decl
15057 && !TREE_STATIC (r))
15058 r = process_outer_var_ref (r, complain);
15059 }
15060 /* Remember this for subsequent uses. */
15061 if (local_specializations)
15062 register_local_specialization (r, t);
15063 }
15064 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15065 r = argument_pack_select_arg (r);
15066 }
15067 else
15068 r = t;
15069 if (!mark_used (r, complain))
15070 return error_mark_node;
15071 return r;
15072
15073 case NAMESPACE_DECL:
15074 return t;
15075
15076 case OVERLOAD:
15077 /* An OVERLOAD will always be a non-dependent overload set; an
15078 overload set from function scope will just be represented with an
15079 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15080 gcc_assert (!uses_template_parms (t));
15081 /* We must have marked any lookups as persistent. */
15082 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
15083 return t;
15084
15085 case BASELINK:
15086 return tsubst_baselink (t, current_nonlambda_class_type (),
15087 args, complain, in_decl);
15088
15089 case TEMPLATE_DECL:
15090 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15091 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15092 args, complain, in_decl);
15093 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15094 return tsubst (t, args, complain, in_decl);
15095 else if (DECL_CLASS_SCOPE_P (t)
15096 && uses_template_parms (DECL_CONTEXT (t)))
15097 {
15098 /* Template template argument like the following example need
15099 special treatment:
15100
15101 template <template <class> class TT> struct C {};
15102 template <class T> struct D {
15103 template <class U> struct E {};
15104 C<E> c; // #1
15105 };
15106 D<int> d; // #2
15107
15108 We are processing the template argument `E' in #1 for
15109 the template instantiation #2. Originally, `E' is a
15110 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15111 have to substitute this with one having context `D<int>'. */
15112
15113 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15114 if (dependent_scope_p (context))
15115 {
15116 /* When rewriting a constructor into a deduction guide, a
15117 non-dependent name can become dependent, so memtmpl<args>
15118 becomes context::template memtmpl<args>. */
15119 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15120 return build_qualified_name (type, context, DECL_NAME (t),
15121 /*template*/true);
15122 }
15123 return lookup_field (context, DECL_NAME(t), 0, false);
15124 }
15125 else
15126 /* Ordinary template template argument. */
15127 return t;
15128
15129 case NON_LVALUE_EXPR:
15130 case VIEW_CONVERT_EXPR:
15131 {
15132 /* Handle location wrappers by substituting the wrapped node
15133 first, *then* reusing the resulting type. Doing the type
15134 first ensures that we handle template parameters and
15135 parameter pack expansions. */
15136 gcc_assert (location_wrapper_p (t));
15137 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15138 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15139 }
15140
15141 case CAST_EXPR:
15142 case REINTERPRET_CAST_EXPR:
15143 case CONST_CAST_EXPR:
15144 case STATIC_CAST_EXPR:
15145 case DYNAMIC_CAST_EXPR:
15146 case IMPLICIT_CONV_EXPR:
15147 case CONVERT_EXPR:
15148 case NOP_EXPR:
15149 {
15150 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15151 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15152 return build1 (code, type, op0);
15153 }
15154
15155 case SIZEOF_EXPR:
15156 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15157 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15158 {
15159 tree expanded, op = TREE_OPERAND (t, 0);
15160 int len = 0;
15161
15162 if (SIZEOF_EXPR_TYPE_P (t))
15163 op = TREE_TYPE (op);
15164
15165 ++cp_unevaluated_operand;
15166 ++c_inhibit_evaluation_warnings;
15167 /* We only want to compute the number of arguments. */
15168 if (PACK_EXPANSION_P (op))
15169 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15170 else
15171 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15172 args, complain, in_decl);
15173 --cp_unevaluated_operand;
15174 --c_inhibit_evaluation_warnings;
15175
15176 if (TREE_CODE (expanded) == TREE_VEC)
15177 {
15178 len = TREE_VEC_LENGTH (expanded);
15179 /* Set TREE_USED for the benefit of -Wunused. */
15180 for (int i = 0; i < len; i++)
15181 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15182 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15183 }
15184
15185 if (expanded == error_mark_node)
15186 return error_mark_node;
15187 else if (PACK_EXPANSION_P (expanded)
15188 || (TREE_CODE (expanded) == TREE_VEC
15189 && pack_expansion_args_count (expanded)))
15190
15191 {
15192 if (PACK_EXPANSION_P (expanded))
15193 /* OK. */;
15194 else if (TREE_VEC_LENGTH (expanded) == 1)
15195 expanded = TREE_VEC_ELT (expanded, 0);
15196 else
15197 expanded = make_argument_pack (expanded);
15198
15199 if (TYPE_P (expanded))
15200 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15201 complain & tf_error);
15202 else
15203 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15204 complain & tf_error);
15205 }
15206 else
15207 return build_int_cst (size_type_node, len);
15208 }
15209 if (SIZEOF_EXPR_TYPE_P (t))
15210 {
15211 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15212 args, complain, in_decl);
15213 r = build1 (NOP_EXPR, r, error_mark_node);
15214 r = build1 (SIZEOF_EXPR,
15215 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15216 SIZEOF_EXPR_TYPE_P (r) = 1;
15217 return r;
15218 }
15219 /* Fall through */
15220
15221 case INDIRECT_REF:
15222 case NEGATE_EXPR:
15223 case TRUTH_NOT_EXPR:
15224 case BIT_NOT_EXPR:
15225 case ADDR_EXPR:
15226 case UNARY_PLUS_EXPR: /* Unary + */
15227 case ALIGNOF_EXPR:
15228 case AT_ENCODE_EXPR:
15229 case ARROW_EXPR:
15230 case THROW_EXPR:
15231 case TYPEID_EXPR:
15232 case REALPART_EXPR:
15233 case IMAGPART_EXPR:
15234 case PAREN_EXPR:
15235 {
15236 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15237 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15238 return build1 (code, type, op0);
15239 }
15240
15241 case COMPONENT_REF:
15242 {
15243 tree object;
15244 tree name;
15245
15246 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15247 name = TREE_OPERAND (t, 1);
15248 if (TREE_CODE (name) == BIT_NOT_EXPR)
15249 {
15250 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15251 complain, in_decl);
15252 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15253 }
15254 else if (TREE_CODE (name) == SCOPE_REF
15255 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15256 {
15257 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15258 complain, in_decl);
15259 name = TREE_OPERAND (name, 1);
15260 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15261 complain, in_decl);
15262 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15263 name = build_qualified_name (/*type=*/NULL_TREE,
15264 base, name,
15265 /*template_p=*/false);
15266 }
15267 else if (BASELINK_P (name))
15268 name = tsubst_baselink (name,
15269 non_reference (TREE_TYPE (object)),
15270 args, complain,
15271 in_decl);
15272 else
15273 name = tsubst_copy (name, args, complain, in_decl);
15274 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15275 }
15276
15277 case PLUS_EXPR:
15278 case MINUS_EXPR:
15279 case MULT_EXPR:
15280 case TRUNC_DIV_EXPR:
15281 case CEIL_DIV_EXPR:
15282 case FLOOR_DIV_EXPR:
15283 case ROUND_DIV_EXPR:
15284 case EXACT_DIV_EXPR:
15285 case BIT_AND_EXPR:
15286 case BIT_IOR_EXPR:
15287 case BIT_XOR_EXPR:
15288 case TRUNC_MOD_EXPR:
15289 case FLOOR_MOD_EXPR:
15290 case TRUTH_ANDIF_EXPR:
15291 case TRUTH_ORIF_EXPR:
15292 case TRUTH_AND_EXPR:
15293 case TRUTH_OR_EXPR:
15294 case RSHIFT_EXPR:
15295 case LSHIFT_EXPR:
15296 case RROTATE_EXPR:
15297 case LROTATE_EXPR:
15298 case EQ_EXPR:
15299 case NE_EXPR:
15300 case MAX_EXPR:
15301 case MIN_EXPR:
15302 case LE_EXPR:
15303 case GE_EXPR:
15304 case LT_EXPR:
15305 case GT_EXPR:
15306 case COMPOUND_EXPR:
15307 case DOTSTAR_EXPR:
15308 case MEMBER_REF:
15309 case PREDECREMENT_EXPR:
15310 case PREINCREMENT_EXPR:
15311 case POSTDECREMENT_EXPR:
15312 case POSTINCREMENT_EXPR:
15313 {
15314 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15315 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15316 return build_nt (code, op0, op1);
15317 }
15318
15319 case SCOPE_REF:
15320 {
15321 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15322 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15323 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15324 QUALIFIED_NAME_IS_TEMPLATE (t));
15325 }
15326
15327 case ARRAY_REF:
15328 {
15329 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15330 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15331 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15332 }
15333
15334 case CALL_EXPR:
15335 {
15336 int n = VL_EXP_OPERAND_LENGTH (t);
15337 tree result = build_vl_exp (CALL_EXPR, n);
15338 int i;
15339 for (i = 0; i < n; i++)
15340 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15341 complain, in_decl);
15342 return result;
15343 }
15344
15345 case COND_EXPR:
15346 case MODOP_EXPR:
15347 case PSEUDO_DTOR_EXPR:
15348 case VEC_PERM_EXPR:
15349 {
15350 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15351 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15352 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15353 r = build_nt (code, op0, op1, op2);
15354 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15355 return r;
15356 }
15357
15358 case NEW_EXPR:
15359 {
15360 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15361 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15362 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15363 r = build_nt (code, op0, op1, op2);
15364 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15365 return r;
15366 }
15367
15368 case DELETE_EXPR:
15369 {
15370 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15371 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15372 r = build_nt (code, op0, op1);
15373 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15374 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15375 return r;
15376 }
15377
15378 case TEMPLATE_ID_EXPR:
15379 {
15380 /* Substituted template arguments */
15381 tree fn = TREE_OPERAND (t, 0);
15382 tree targs = TREE_OPERAND (t, 1);
15383
15384 fn = tsubst_copy (fn, args, complain, in_decl);
15385 if (targs)
15386 targs = tsubst_template_args (targs, args, complain, in_decl);
15387
15388 return lookup_template_function (fn, targs);
15389 }
15390
15391 case TREE_LIST:
15392 {
15393 tree purpose, value, chain;
15394
15395 if (t == void_list_node)
15396 return t;
15397
15398 purpose = TREE_PURPOSE (t);
15399 if (purpose)
15400 purpose = tsubst_copy (purpose, args, complain, in_decl);
15401 value = TREE_VALUE (t);
15402 if (value)
15403 value = tsubst_copy (value, args, complain, in_decl);
15404 chain = TREE_CHAIN (t);
15405 if (chain && chain != void_type_node)
15406 chain = tsubst_copy (chain, args, complain, in_decl);
15407 if (purpose == TREE_PURPOSE (t)
15408 && value == TREE_VALUE (t)
15409 && chain == TREE_CHAIN (t))
15410 return t;
15411 return tree_cons (purpose, value, chain);
15412 }
15413
15414 case RECORD_TYPE:
15415 case UNION_TYPE:
15416 case ENUMERAL_TYPE:
15417 case INTEGER_TYPE:
15418 case TEMPLATE_TYPE_PARM:
15419 case TEMPLATE_TEMPLATE_PARM:
15420 case BOUND_TEMPLATE_TEMPLATE_PARM:
15421 case TEMPLATE_PARM_INDEX:
15422 case POINTER_TYPE:
15423 case REFERENCE_TYPE:
15424 case OFFSET_TYPE:
15425 case FUNCTION_TYPE:
15426 case METHOD_TYPE:
15427 case ARRAY_TYPE:
15428 case TYPENAME_TYPE:
15429 case UNBOUND_CLASS_TEMPLATE:
15430 case TYPEOF_TYPE:
15431 case DECLTYPE_TYPE:
15432 case TYPE_DECL:
15433 return tsubst (t, args, complain, in_decl);
15434
15435 case USING_DECL:
15436 t = DECL_NAME (t);
15437 /* Fall through. */
15438 case IDENTIFIER_NODE:
15439 if (IDENTIFIER_CONV_OP_P (t))
15440 {
15441 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15442 return make_conv_op_name (new_type);
15443 }
15444 else
15445 return t;
15446
15447 case CONSTRUCTOR:
15448 /* This is handled by tsubst_copy_and_build. */
15449 gcc_unreachable ();
15450
15451 case VA_ARG_EXPR:
15452 {
15453 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15454 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15455 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15456 }
15457
15458 case CLEANUP_POINT_EXPR:
15459 /* We shouldn't have built any of these during initial template
15460 generation. Instead, they should be built during instantiation
15461 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15462 gcc_unreachable ();
15463
15464 case OFFSET_REF:
15465 {
15466 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15467 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15468 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15469 r = build2 (code, type, op0, op1);
15470 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15471 if (!mark_used (TREE_OPERAND (r, 1), complain)
15472 && !(complain & tf_error))
15473 return error_mark_node;
15474 return r;
15475 }
15476
15477 case EXPR_PACK_EXPANSION:
15478 error ("invalid use of pack expansion expression");
15479 return error_mark_node;
15480
15481 case NONTYPE_ARGUMENT_PACK:
15482 error ("use %<...%> to expand argument pack");
15483 return error_mark_node;
15484
15485 case VOID_CST:
15486 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15487 return t;
15488
15489 case INTEGER_CST:
15490 case REAL_CST:
15491 case STRING_CST:
15492 case COMPLEX_CST:
15493 {
15494 /* Instantiate any typedefs in the type. */
15495 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15496 r = fold_convert (type, t);
15497 gcc_assert (TREE_CODE (r) == code);
15498 return r;
15499 }
15500
15501 case PTRMEM_CST:
15502 /* These can sometimes show up in a partial instantiation, but never
15503 involve template parms. */
15504 gcc_assert (!uses_template_parms (t));
15505 return t;
15506
15507 case UNARY_LEFT_FOLD_EXPR:
15508 return tsubst_unary_left_fold (t, args, complain, in_decl);
15509 case UNARY_RIGHT_FOLD_EXPR:
15510 return tsubst_unary_right_fold (t, args, complain, in_decl);
15511 case BINARY_LEFT_FOLD_EXPR:
15512 return tsubst_binary_left_fold (t, args, complain, in_decl);
15513 case BINARY_RIGHT_FOLD_EXPR:
15514 return tsubst_binary_right_fold (t, args, complain, in_decl);
15515 case PREDICT_EXPR:
15516 return t;
15517
15518 case DEBUG_BEGIN_STMT:
15519 /* ??? There's no point in copying it for now, but maybe some
15520 day it will contain more information, such as a pointer back
15521 to the containing function, inlined copy or so. */
15522 return t;
15523
15524 default:
15525 /* We shouldn't get here, but keep going if !flag_checking. */
15526 if (flag_checking)
15527 gcc_unreachable ();
15528 return t;
15529 }
15530 }
15531
15532 /* Helper function for tsubst_omp_clauses, used for instantiation of
15533 OMP_CLAUSE_DECL of clauses. */
15534
15535 static tree
15536 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15537 tree in_decl)
15538 {
15539 if (decl == NULL_TREE)
15540 return NULL_TREE;
15541
15542 /* Handle an OpenMP array section represented as a TREE_LIST (or
15543 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15544 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15545 TREE_LIST. We can handle it exactly the same as an array section
15546 (purpose, value, and a chain), even though the nomenclature
15547 (low_bound, length, etc) is different. */
15548 if (TREE_CODE (decl) == TREE_LIST)
15549 {
15550 tree low_bound
15551 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15552 /*integral_constant_expression_p=*/false);
15553 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15554 /*integral_constant_expression_p=*/false);
15555 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15556 in_decl);
15557 if (TREE_PURPOSE (decl) == low_bound
15558 && TREE_VALUE (decl) == length
15559 && TREE_CHAIN (decl) == chain)
15560 return decl;
15561 tree ret = tree_cons (low_bound, length, chain);
15562 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15563 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15564 return ret;
15565 }
15566 tree ret = tsubst_expr (decl, args, complain, in_decl,
15567 /*integral_constant_expression_p=*/false);
15568 /* Undo convert_from_reference tsubst_expr could have called. */
15569 if (decl
15570 && REFERENCE_REF_P (ret)
15571 && !REFERENCE_REF_P (decl))
15572 ret = TREE_OPERAND (ret, 0);
15573 return ret;
15574 }
15575
15576 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15577
15578 static tree
15579 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15580 tree args, tsubst_flags_t complain, tree in_decl)
15581 {
15582 tree new_clauses = NULL_TREE, nc, oc;
15583 tree linear_no_step = NULL_TREE;
15584
15585 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15586 {
15587 nc = copy_node (oc);
15588 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15589 new_clauses = nc;
15590
15591 switch (OMP_CLAUSE_CODE (nc))
15592 {
15593 case OMP_CLAUSE_LASTPRIVATE:
15594 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15595 {
15596 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15597 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15598 in_decl, /*integral_constant_expression_p=*/false);
15599 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15600 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15601 }
15602 /* FALLTHRU */
15603 case OMP_CLAUSE_PRIVATE:
15604 case OMP_CLAUSE_SHARED:
15605 case OMP_CLAUSE_FIRSTPRIVATE:
15606 case OMP_CLAUSE_COPYIN:
15607 case OMP_CLAUSE_COPYPRIVATE:
15608 case OMP_CLAUSE_UNIFORM:
15609 case OMP_CLAUSE_DEPEND:
15610 case OMP_CLAUSE_FROM:
15611 case OMP_CLAUSE_TO:
15612 case OMP_CLAUSE_MAP:
15613 case OMP_CLAUSE_USE_DEVICE_PTR:
15614 case OMP_CLAUSE_IS_DEVICE_PTR:
15615 OMP_CLAUSE_DECL (nc)
15616 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15617 in_decl);
15618 break;
15619 case OMP_CLAUSE_TILE:
15620 case OMP_CLAUSE_IF:
15621 case OMP_CLAUSE_NUM_THREADS:
15622 case OMP_CLAUSE_SCHEDULE:
15623 case OMP_CLAUSE_COLLAPSE:
15624 case OMP_CLAUSE_FINAL:
15625 case OMP_CLAUSE_DEVICE:
15626 case OMP_CLAUSE_DIST_SCHEDULE:
15627 case OMP_CLAUSE_NUM_TEAMS:
15628 case OMP_CLAUSE_THREAD_LIMIT:
15629 case OMP_CLAUSE_SAFELEN:
15630 case OMP_CLAUSE_SIMDLEN:
15631 case OMP_CLAUSE_NUM_TASKS:
15632 case OMP_CLAUSE_GRAINSIZE:
15633 case OMP_CLAUSE_PRIORITY:
15634 case OMP_CLAUSE_ORDERED:
15635 case OMP_CLAUSE_HINT:
15636 case OMP_CLAUSE_NUM_GANGS:
15637 case OMP_CLAUSE_NUM_WORKERS:
15638 case OMP_CLAUSE_VECTOR_LENGTH:
15639 case OMP_CLAUSE_WORKER:
15640 case OMP_CLAUSE_VECTOR:
15641 case OMP_CLAUSE_ASYNC:
15642 case OMP_CLAUSE_WAIT:
15643 OMP_CLAUSE_OPERAND (nc, 0)
15644 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15645 in_decl, /*integral_constant_expression_p=*/false);
15646 break;
15647 case OMP_CLAUSE_REDUCTION:
15648 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15649 {
15650 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15651 if (TREE_CODE (placeholder) == SCOPE_REF)
15652 {
15653 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15654 complain, in_decl);
15655 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15656 = build_qualified_name (NULL_TREE, scope,
15657 TREE_OPERAND (placeholder, 1),
15658 false);
15659 }
15660 else
15661 gcc_assert (identifier_p (placeholder));
15662 }
15663 OMP_CLAUSE_DECL (nc)
15664 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15665 in_decl);
15666 break;
15667 case OMP_CLAUSE_GANG:
15668 case OMP_CLAUSE_ALIGNED:
15669 OMP_CLAUSE_DECL (nc)
15670 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15671 in_decl);
15672 OMP_CLAUSE_OPERAND (nc, 1)
15673 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15674 in_decl, /*integral_constant_expression_p=*/false);
15675 break;
15676 case OMP_CLAUSE_LINEAR:
15677 OMP_CLAUSE_DECL (nc)
15678 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15679 in_decl);
15680 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15681 {
15682 gcc_assert (!linear_no_step);
15683 linear_no_step = nc;
15684 }
15685 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15686 OMP_CLAUSE_LINEAR_STEP (nc)
15687 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15688 complain, in_decl);
15689 else
15690 OMP_CLAUSE_LINEAR_STEP (nc)
15691 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15692 in_decl,
15693 /*integral_constant_expression_p=*/false);
15694 break;
15695 case OMP_CLAUSE_NOWAIT:
15696 case OMP_CLAUSE_DEFAULT:
15697 case OMP_CLAUSE_UNTIED:
15698 case OMP_CLAUSE_MERGEABLE:
15699 case OMP_CLAUSE_INBRANCH:
15700 case OMP_CLAUSE_NOTINBRANCH:
15701 case OMP_CLAUSE_PROC_BIND:
15702 case OMP_CLAUSE_FOR:
15703 case OMP_CLAUSE_PARALLEL:
15704 case OMP_CLAUSE_SECTIONS:
15705 case OMP_CLAUSE_TASKGROUP:
15706 case OMP_CLAUSE_NOGROUP:
15707 case OMP_CLAUSE_THREADS:
15708 case OMP_CLAUSE_SIMD:
15709 case OMP_CLAUSE_DEFAULTMAP:
15710 case OMP_CLAUSE_INDEPENDENT:
15711 case OMP_CLAUSE_AUTO:
15712 case OMP_CLAUSE_SEQ:
15713 break;
15714 default:
15715 gcc_unreachable ();
15716 }
15717 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15718 switch (OMP_CLAUSE_CODE (nc))
15719 {
15720 case OMP_CLAUSE_SHARED:
15721 case OMP_CLAUSE_PRIVATE:
15722 case OMP_CLAUSE_FIRSTPRIVATE:
15723 case OMP_CLAUSE_LASTPRIVATE:
15724 case OMP_CLAUSE_COPYPRIVATE:
15725 case OMP_CLAUSE_LINEAR:
15726 case OMP_CLAUSE_REDUCTION:
15727 case OMP_CLAUSE_USE_DEVICE_PTR:
15728 case OMP_CLAUSE_IS_DEVICE_PTR:
15729 /* tsubst_expr on SCOPE_REF results in returning
15730 finish_non_static_data_member result. Undo that here. */
15731 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15732 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15733 == IDENTIFIER_NODE))
15734 {
15735 tree t = OMP_CLAUSE_DECL (nc);
15736 tree v = t;
15737 while (v)
15738 switch (TREE_CODE (v))
15739 {
15740 case COMPONENT_REF:
15741 case MEM_REF:
15742 case INDIRECT_REF:
15743 CASE_CONVERT:
15744 case POINTER_PLUS_EXPR:
15745 v = TREE_OPERAND (v, 0);
15746 continue;
15747 case PARM_DECL:
15748 if (DECL_CONTEXT (v) == current_function_decl
15749 && DECL_ARTIFICIAL (v)
15750 && DECL_NAME (v) == this_identifier)
15751 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15752 /* FALLTHRU */
15753 default:
15754 v = NULL_TREE;
15755 break;
15756 }
15757 }
15758 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15759 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15760 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15761 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15762 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15763 {
15764 tree decl = OMP_CLAUSE_DECL (nc);
15765 if (VAR_P (decl))
15766 {
15767 retrofit_lang_decl (decl);
15768 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15769 }
15770 }
15771 break;
15772 default:
15773 break;
15774 }
15775 }
15776
15777 new_clauses = nreverse (new_clauses);
15778 if (ort != C_ORT_OMP_DECLARE_SIMD)
15779 {
15780 new_clauses = finish_omp_clauses (new_clauses, ort);
15781 if (linear_no_step)
15782 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15783 if (nc == linear_no_step)
15784 {
15785 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15786 break;
15787 }
15788 }
15789 return new_clauses;
15790 }
15791
15792 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15793
15794 static tree
15795 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15796 tree in_decl)
15797 {
15798 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15799
15800 tree purpose, value, chain;
15801
15802 if (t == NULL)
15803 return t;
15804
15805 if (TREE_CODE (t) != TREE_LIST)
15806 return tsubst_copy_and_build (t, args, complain, in_decl,
15807 /*function_p=*/false,
15808 /*integral_constant_expression_p=*/false);
15809
15810 if (t == void_list_node)
15811 return t;
15812
15813 purpose = TREE_PURPOSE (t);
15814 if (purpose)
15815 purpose = RECUR (purpose);
15816 value = TREE_VALUE (t);
15817 if (value)
15818 {
15819 if (TREE_CODE (value) != LABEL_DECL)
15820 value = RECUR (value);
15821 else
15822 {
15823 value = lookup_label (DECL_NAME (value));
15824 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15825 TREE_USED (value) = 1;
15826 }
15827 }
15828 chain = TREE_CHAIN (t);
15829 if (chain && chain != void_type_node)
15830 chain = RECUR (chain);
15831 return tree_cons (purpose, value, chain);
15832 #undef RECUR
15833 }
15834
15835 /* Used to temporarily communicate the list of #pragma omp parallel
15836 clauses to #pragma omp for instantiation if they are combined
15837 together. */
15838
15839 static tree *omp_parallel_combined_clauses;
15840
15841 /* Substitute one OMP_FOR iterator. */
15842
15843 static void
15844 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15845 tree initv, tree condv, tree incrv, tree *clauses,
15846 tree args, tsubst_flags_t complain, tree in_decl,
15847 bool integral_constant_expression_p)
15848 {
15849 #define RECUR(NODE) \
15850 tsubst_expr ((NODE), args, complain, in_decl, \
15851 integral_constant_expression_p)
15852 tree decl, init, cond, incr;
15853
15854 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15855 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15856
15857 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15858 {
15859 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15860 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15861 }
15862
15863 decl = TREE_OPERAND (init, 0);
15864 init = TREE_OPERAND (init, 1);
15865 tree decl_expr = NULL_TREE;
15866 if (init && TREE_CODE (init) == DECL_EXPR)
15867 {
15868 /* We need to jump through some hoops to handle declarations in the
15869 init-statement, since we might need to handle auto deduction,
15870 but we need to keep control of initialization. */
15871 decl_expr = init;
15872 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15873 decl = tsubst_decl (decl, args, complain);
15874 }
15875 else
15876 {
15877 if (TREE_CODE (decl) == SCOPE_REF)
15878 {
15879 decl = RECUR (decl);
15880 if (TREE_CODE (decl) == COMPONENT_REF)
15881 {
15882 tree v = decl;
15883 while (v)
15884 switch (TREE_CODE (v))
15885 {
15886 case COMPONENT_REF:
15887 case MEM_REF:
15888 case INDIRECT_REF:
15889 CASE_CONVERT:
15890 case POINTER_PLUS_EXPR:
15891 v = TREE_OPERAND (v, 0);
15892 continue;
15893 case PARM_DECL:
15894 if (DECL_CONTEXT (v) == current_function_decl
15895 && DECL_ARTIFICIAL (v)
15896 && DECL_NAME (v) == this_identifier)
15897 {
15898 decl = TREE_OPERAND (decl, 1);
15899 decl = omp_privatize_field (decl, false);
15900 }
15901 /* FALLTHRU */
15902 default:
15903 v = NULL_TREE;
15904 break;
15905 }
15906 }
15907 }
15908 else
15909 decl = RECUR (decl);
15910 }
15911 init = RECUR (init);
15912
15913 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15914 if (auto_node && init)
15915 TREE_TYPE (decl)
15916 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
15917
15918 gcc_assert (!type_dependent_expression_p (decl));
15919
15920 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15921 {
15922 if (decl_expr)
15923 {
15924 /* Declare the variable, but don't let that initialize it. */
15925 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15926 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15927 RECUR (decl_expr);
15928 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15929 }
15930
15931 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15932 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15933 if (TREE_CODE (incr) == MODIFY_EXPR)
15934 {
15935 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15936 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15937 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15938 NOP_EXPR, rhs, complain);
15939 }
15940 else
15941 incr = RECUR (incr);
15942 TREE_VEC_ELT (declv, i) = decl;
15943 TREE_VEC_ELT (initv, i) = init;
15944 TREE_VEC_ELT (condv, i) = cond;
15945 TREE_VEC_ELT (incrv, i) = incr;
15946 return;
15947 }
15948
15949 if (decl_expr)
15950 {
15951 /* Declare and initialize the variable. */
15952 RECUR (decl_expr);
15953 init = NULL_TREE;
15954 }
15955 else if (init)
15956 {
15957 tree *pc;
15958 int j;
15959 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15960 {
15961 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15962 {
15963 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15964 && OMP_CLAUSE_DECL (*pc) == decl)
15965 break;
15966 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15967 && OMP_CLAUSE_DECL (*pc) == decl)
15968 {
15969 if (j)
15970 break;
15971 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15972 tree c = *pc;
15973 *pc = OMP_CLAUSE_CHAIN (c);
15974 OMP_CLAUSE_CHAIN (c) = *clauses;
15975 *clauses = c;
15976 }
15977 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15978 && OMP_CLAUSE_DECL (*pc) == decl)
15979 {
15980 error ("iteration variable %qD should not be firstprivate",
15981 decl);
15982 *pc = OMP_CLAUSE_CHAIN (*pc);
15983 }
15984 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15985 && OMP_CLAUSE_DECL (*pc) == decl)
15986 {
15987 error ("iteration variable %qD should not be reduction",
15988 decl);
15989 *pc = OMP_CLAUSE_CHAIN (*pc);
15990 }
15991 else
15992 pc = &OMP_CLAUSE_CHAIN (*pc);
15993 }
15994 if (*pc)
15995 break;
15996 }
15997 if (*pc == NULL_TREE)
15998 {
15999 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16000 OMP_CLAUSE_DECL (c) = decl;
16001 c = finish_omp_clauses (c, C_ORT_OMP);
16002 if (c)
16003 {
16004 OMP_CLAUSE_CHAIN (c) = *clauses;
16005 *clauses = c;
16006 }
16007 }
16008 }
16009 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16010 if (COMPARISON_CLASS_P (cond))
16011 {
16012 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16013 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16014 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16015 }
16016 else
16017 cond = RECUR (cond);
16018 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16019 switch (TREE_CODE (incr))
16020 {
16021 case PREINCREMENT_EXPR:
16022 case PREDECREMENT_EXPR:
16023 case POSTINCREMENT_EXPR:
16024 case POSTDECREMENT_EXPR:
16025 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16026 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16027 break;
16028 case MODIFY_EXPR:
16029 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16030 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16031 {
16032 tree rhs = TREE_OPERAND (incr, 1);
16033 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16034 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16035 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16036 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16037 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16038 rhs0, rhs1));
16039 }
16040 else
16041 incr = RECUR (incr);
16042 break;
16043 case MODOP_EXPR:
16044 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16045 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16046 {
16047 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16048 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16049 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16050 TREE_TYPE (decl), lhs,
16051 RECUR (TREE_OPERAND (incr, 2))));
16052 }
16053 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16054 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16055 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16056 {
16057 tree rhs = TREE_OPERAND (incr, 2);
16058 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16059 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16060 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16061 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16062 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16063 rhs0, rhs1));
16064 }
16065 else
16066 incr = RECUR (incr);
16067 break;
16068 default:
16069 incr = RECUR (incr);
16070 break;
16071 }
16072
16073 TREE_VEC_ELT (declv, i) = decl;
16074 TREE_VEC_ELT (initv, i) = init;
16075 TREE_VEC_ELT (condv, i) = cond;
16076 TREE_VEC_ELT (incrv, i) = incr;
16077 #undef RECUR
16078 }
16079
16080 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16081 of OMP_TARGET's body. */
16082
16083 static tree
16084 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16085 {
16086 *walk_subtrees = 0;
16087 switch (TREE_CODE (*tp))
16088 {
16089 case OMP_TEAMS:
16090 return *tp;
16091 case BIND_EXPR:
16092 case STATEMENT_LIST:
16093 *walk_subtrees = 1;
16094 break;
16095 default:
16096 break;
16097 }
16098 return NULL_TREE;
16099 }
16100
16101 /* Helper function for tsubst_expr. For decomposition declaration
16102 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16103 also the corresponding decls representing the identifiers
16104 of the decomposition declaration. Return DECL if successful
16105 or error_mark_node otherwise, set *FIRST to the first decl
16106 in the list chained through DECL_CHAIN and *CNT to the number
16107 of such decls. */
16108
16109 static tree
16110 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16111 tsubst_flags_t complain, tree in_decl, tree *first,
16112 unsigned int *cnt)
16113 {
16114 tree decl2, decl3, prev = decl;
16115 *cnt = 0;
16116 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16117 for (decl2 = DECL_CHAIN (pattern_decl);
16118 decl2
16119 && VAR_P (decl2)
16120 && DECL_DECOMPOSITION_P (decl2)
16121 && DECL_NAME (decl2);
16122 decl2 = DECL_CHAIN (decl2))
16123 {
16124 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16125 {
16126 gcc_assert (errorcount);
16127 return error_mark_node;
16128 }
16129 (*cnt)++;
16130 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16131 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16132 tree v = DECL_VALUE_EXPR (decl2);
16133 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16134 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16135 decl3 = tsubst (decl2, args, complain, in_decl);
16136 SET_DECL_VALUE_EXPR (decl2, v);
16137 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16138 if (VAR_P (decl3))
16139 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16140 maybe_push_decl (decl3);
16141 if (error_operand_p (decl3))
16142 decl = error_mark_node;
16143 else if (decl != error_mark_node
16144 && DECL_CHAIN (decl3) != prev)
16145 {
16146 gcc_assert (errorcount);
16147 decl = error_mark_node;
16148 }
16149 else
16150 prev = decl3;
16151 }
16152 *first = prev;
16153 return decl;
16154 }
16155
16156 /* Like tsubst_copy for expressions, etc. but also does semantic
16157 processing. */
16158
16159 tree
16160 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16161 bool integral_constant_expression_p)
16162 {
16163 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16164 #define RECUR(NODE) \
16165 tsubst_expr ((NODE), args, complain, in_decl, \
16166 integral_constant_expression_p)
16167
16168 tree stmt, tmp;
16169 tree r;
16170 location_t loc;
16171
16172 if (t == NULL_TREE || t == error_mark_node)
16173 return t;
16174
16175 loc = input_location;
16176 if (EXPR_HAS_LOCATION (t))
16177 input_location = EXPR_LOCATION (t);
16178 if (STATEMENT_CODE_P (TREE_CODE (t)))
16179 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16180
16181 switch (TREE_CODE (t))
16182 {
16183 case STATEMENT_LIST:
16184 {
16185 tree_stmt_iterator i;
16186 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16187 RECUR (tsi_stmt (i));
16188 break;
16189 }
16190
16191 case CTOR_INITIALIZER:
16192 finish_mem_initializers (tsubst_initializer_list
16193 (TREE_OPERAND (t, 0), args));
16194 break;
16195
16196 case RETURN_EXPR:
16197 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16198 break;
16199
16200 case EXPR_STMT:
16201 tmp = RECUR (EXPR_STMT_EXPR (t));
16202 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16203 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16204 else
16205 finish_expr_stmt (tmp);
16206 break;
16207
16208 case USING_STMT:
16209 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16210 /*attribs=*/NULL_TREE);
16211 break;
16212
16213 case DECL_EXPR:
16214 {
16215 tree decl, pattern_decl;
16216 tree init;
16217
16218 pattern_decl = decl = DECL_EXPR_DECL (t);
16219 if (TREE_CODE (decl) == LABEL_DECL)
16220 finish_label_decl (DECL_NAME (decl));
16221 else if (TREE_CODE (decl) == USING_DECL)
16222 {
16223 tree scope = USING_DECL_SCOPE (decl);
16224 tree name = DECL_NAME (decl);
16225
16226 scope = tsubst (scope, args, complain, in_decl);
16227 decl = lookup_qualified_name (scope, name,
16228 /*is_type_p=*/false,
16229 /*complain=*/false);
16230 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16231 qualified_name_lookup_error (scope, name, decl, input_location);
16232 else
16233 finish_local_using_decl (decl, scope, name);
16234 }
16235 else if (is_capture_proxy (decl)
16236 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16237 {
16238 /* We're in tsubst_lambda_expr, we've already inserted a new
16239 capture proxy, so look it up and register it. */
16240 tree inst;
16241 if (DECL_PACK_P (decl))
16242 {
16243 inst = (retrieve_local_specialization
16244 (DECL_CAPTURED_VARIABLE (decl)));
16245 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16246 }
16247 else
16248 {
16249 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16250 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16251 gcc_assert (inst != decl && is_capture_proxy (inst));
16252 }
16253 register_local_specialization (inst, decl);
16254 break;
16255 }
16256 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16257 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16258 /* Don't copy the old closure; we'll create a new one in
16259 tsubst_lambda_expr. */
16260 break;
16261 else
16262 {
16263 init = DECL_INITIAL (decl);
16264 decl = tsubst (decl, args, complain, in_decl);
16265 if (decl != error_mark_node)
16266 {
16267 /* By marking the declaration as instantiated, we avoid
16268 trying to instantiate it. Since instantiate_decl can't
16269 handle local variables, and since we've already done
16270 all that needs to be done, that's the right thing to
16271 do. */
16272 if (VAR_P (decl))
16273 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16274 if (VAR_P (decl)
16275 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16276 /* Anonymous aggregates are a special case. */
16277 finish_anon_union (decl);
16278 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16279 {
16280 DECL_CONTEXT (decl) = current_function_decl;
16281 if (DECL_NAME (decl) == this_identifier)
16282 {
16283 tree lam = DECL_CONTEXT (current_function_decl);
16284 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16285 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16286 }
16287 insert_capture_proxy (decl);
16288 }
16289 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16290 /* We already did a pushtag. */;
16291 else if (TREE_CODE (decl) == FUNCTION_DECL
16292 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16293 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16294 {
16295 DECL_CONTEXT (decl) = NULL_TREE;
16296 pushdecl (decl);
16297 DECL_CONTEXT (decl) = current_function_decl;
16298 cp_check_omp_declare_reduction (decl);
16299 }
16300 else
16301 {
16302 int const_init = false;
16303 maybe_push_decl (decl);
16304 if (VAR_P (decl)
16305 && DECL_PRETTY_FUNCTION_P (decl))
16306 {
16307 /* For __PRETTY_FUNCTION__ we have to adjust the
16308 initializer. */
16309 const char *const name
16310 = cxx_printable_name (current_function_decl, 2);
16311 init = cp_fname_init (name, &TREE_TYPE (decl));
16312 }
16313 else
16314 init = tsubst_init (init, decl, args, complain, in_decl);
16315
16316 if (VAR_P (decl))
16317 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16318 (pattern_decl));
16319 if (VAR_P (decl)
16320 && DECL_DECOMPOSITION_P (decl)
16321 && TREE_TYPE (pattern_decl) != error_mark_node)
16322 {
16323 unsigned int cnt;
16324 tree first;
16325 tree ndecl
16326 = tsubst_decomp_names (decl, pattern_decl, args,
16327 complain, in_decl, &first, &cnt);
16328 if (ndecl != error_mark_node)
16329 cp_maybe_mangle_decomp (ndecl, first, cnt);
16330 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16331 if (ndecl != error_mark_node)
16332 cp_finish_decomp (ndecl, first, cnt);
16333 }
16334 else
16335 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16336 }
16337 }
16338 }
16339
16340 break;
16341 }
16342
16343 case FOR_STMT:
16344 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16345 RECUR (FOR_INIT_STMT (t));
16346 finish_init_stmt (stmt);
16347 tmp = RECUR (FOR_COND (t));
16348 finish_for_cond (tmp, stmt, false, 0);
16349 tmp = RECUR (FOR_EXPR (t));
16350 finish_for_expr (tmp, stmt);
16351 {
16352 bool prev = note_iteration_stmt_body_start ();
16353 RECUR (FOR_BODY (t));
16354 note_iteration_stmt_body_end (prev);
16355 }
16356 finish_for_stmt (stmt);
16357 break;
16358
16359 case RANGE_FOR_STMT:
16360 {
16361 /* Construct another range_for, if this is not a final
16362 substitution (for inside inside a generic lambda of a
16363 template). Otherwise convert to a regular for. */
16364 tree decl, expr;
16365 stmt = (processing_template_decl
16366 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16367 : begin_for_stmt (NULL_TREE, NULL_TREE));
16368 decl = RANGE_FOR_DECL (t);
16369 decl = tsubst (decl, args, complain, in_decl);
16370 maybe_push_decl (decl);
16371 expr = RECUR (RANGE_FOR_EXPR (t));
16372
16373 tree decomp_first = NULL_TREE;
16374 unsigned decomp_cnt = 0;
16375 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16376 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16377 complain, in_decl,
16378 &decomp_first, &decomp_cnt);
16379
16380 if (processing_template_decl)
16381 {
16382 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16383 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16384 finish_range_for_decl (stmt, decl, expr);
16385 }
16386 else
16387 {
16388 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16389 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16390 stmt = cp_convert_range_for (stmt, decl, expr,
16391 decomp_first, decomp_cnt,
16392 RANGE_FOR_IVDEP (t), unroll);
16393 }
16394
16395 bool prev = note_iteration_stmt_body_start ();
16396 RECUR (RANGE_FOR_BODY (t));
16397 note_iteration_stmt_body_end (prev);
16398 finish_for_stmt (stmt);
16399 }
16400 break;
16401
16402 case WHILE_STMT:
16403 stmt = begin_while_stmt ();
16404 tmp = RECUR (WHILE_COND (t));
16405 finish_while_stmt_cond (tmp, stmt, false, 0);
16406 {
16407 bool prev = note_iteration_stmt_body_start ();
16408 RECUR (WHILE_BODY (t));
16409 note_iteration_stmt_body_end (prev);
16410 }
16411 finish_while_stmt (stmt);
16412 break;
16413
16414 case DO_STMT:
16415 stmt = begin_do_stmt ();
16416 {
16417 bool prev = note_iteration_stmt_body_start ();
16418 RECUR (DO_BODY (t));
16419 note_iteration_stmt_body_end (prev);
16420 }
16421 finish_do_body (stmt);
16422 tmp = RECUR (DO_COND (t));
16423 finish_do_stmt (tmp, stmt, false, 0);
16424 break;
16425
16426 case IF_STMT:
16427 stmt = begin_if_stmt ();
16428 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16429 tmp = RECUR (IF_COND (t));
16430 tmp = finish_if_stmt_cond (tmp, stmt);
16431 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16432 /* Don't instantiate the THEN_CLAUSE. */;
16433 else
16434 {
16435 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16436 if (inhibit)
16437 ++c_inhibit_evaluation_warnings;
16438 RECUR (THEN_CLAUSE (t));
16439 if (inhibit)
16440 --c_inhibit_evaluation_warnings;
16441 }
16442 finish_then_clause (stmt);
16443
16444 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16445 /* Don't instantiate the ELSE_CLAUSE. */;
16446 else if (ELSE_CLAUSE (t))
16447 {
16448 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16449 begin_else_clause (stmt);
16450 if (inhibit)
16451 ++c_inhibit_evaluation_warnings;
16452 RECUR (ELSE_CLAUSE (t));
16453 if (inhibit)
16454 --c_inhibit_evaluation_warnings;
16455 finish_else_clause (stmt);
16456 }
16457
16458 finish_if_stmt (stmt);
16459 break;
16460
16461 case BIND_EXPR:
16462 if (BIND_EXPR_BODY_BLOCK (t))
16463 stmt = begin_function_body ();
16464 else
16465 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16466 ? BCS_TRY_BLOCK : 0);
16467
16468 RECUR (BIND_EXPR_BODY (t));
16469
16470 if (BIND_EXPR_BODY_BLOCK (t))
16471 finish_function_body (stmt);
16472 else
16473 finish_compound_stmt (stmt);
16474 break;
16475
16476 case BREAK_STMT:
16477 finish_break_stmt ();
16478 break;
16479
16480 case CONTINUE_STMT:
16481 finish_continue_stmt ();
16482 break;
16483
16484 case SWITCH_STMT:
16485 stmt = begin_switch_stmt ();
16486 tmp = RECUR (SWITCH_STMT_COND (t));
16487 finish_switch_cond (tmp, stmt);
16488 RECUR (SWITCH_STMT_BODY (t));
16489 finish_switch_stmt (stmt);
16490 break;
16491
16492 case CASE_LABEL_EXPR:
16493 {
16494 tree low = RECUR (CASE_LOW (t));
16495 tree high = RECUR (CASE_HIGH (t));
16496 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16497 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16498 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16499 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16500 }
16501 break;
16502
16503 case LABEL_EXPR:
16504 {
16505 tree decl = LABEL_EXPR_LABEL (t);
16506 tree label;
16507
16508 label = finish_label_stmt (DECL_NAME (decl));
16509 if (TREE_CODE (label) == LABEL_DECL)
16510 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16511 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16512 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16513 }
16514 break;
16515
16516 case GOTO_EXPR:
16517 tmp = GOTO_DESTINATION (t);
16518 if (TREE_CODE (tmp) != LABEL_DECL)
16519 /* Computed goto's must be tsubst'd into. On the other hand,
16520 non-computed gotos must not be; the identifier in question
16521 will have no binding. */
16522 tmp = RECUR (tmp);
16523 else
16524 tmp = DECL_NAME (tmp);
16525 finish_goto_stmt (tmp);
16526 break;
16527
16528 case ASM_EXPR:
16529 {
16530 tree string = RECUR (ASM_STRING (t));
16531 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16532 complain, in_decl);
16533 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16534 complain, in_decl);
16535 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16536 complain, in_decl);
16537 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16538 complain, in_decl);
16539 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16540 clobbers, labels);
16541 tree asm_expr = tmp;
16542 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16543 asm_expr = TREE_OPERAND (asm_expr, 0);
16544 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16545 }
16546 break;
16547
16548 case TRY_BLOCK:
16549 if (CLEANUP_P (t))
16550 {
16551 stmt = begin_try_block ();
16552 RECUR (TRY_STMTS (t));
16553 finish_cleanup_try_block (stmt);
16554 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16555 }
16556 else
16557 {
16558 tree compound_stmt = NULL_TREE;
16559
16560 if (FN_TRY_BLOCK_P (t))
16561 stmt = begin_function_try_block (&compound_stmt);
16562 else
16563 stmt = begin_try_block ();
16564
16565 RECUR (TRY_STMTS (t));
16566
16567 if (FN_TRY_BLOCK_P (t))
16568 finish_function_try_block (stmt);
16569 else
16570 finish_try_block (stmt);
16571
16572 RECUR (TRY_HANDLERS (t));
16573 if (FN_TRY_BLOCK_P (t))
16574 finish_function_handler_sequence (stmt, compound_stmt);
16575 else
16576 finish_handler_sequence (stmt);
16577 }
16578 break;
16579
16580 case HANDLER:
16581 {
16582 tree decl = HANDLER_PARMS (t);
16583
16584 if (decl)
16585 {
16586 decl = tsubst (decl, args, complain, in_decl);
16587 /* Prevent instantiate_decl from trying to instantiate
16588 this variable. We've already done all that needs to be
16589 done. */
16590 if (decl != error_mark_node)
16591 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16592 }
16593 stmt = begin_handler ();
16594 finish_handler_parms (decl, stmt);
16595 RECUR (HANDLER_BODY (t));
16596 finish_handler (stmt);
16597 }
16598 break;
16599
16600 case TAG_DEFN:
16601 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16602 if (CLASS_TYPE_P (tmp))
16603 {
16604 /* Local classes are not independent templates; they are
16605 instantiated along with their containing function. And this
16606 way we don't have to deal with pushing out of one local class
16607 to instantiate a member of another local class. */
16608 /* Closures are handled by the LAMBDA_EXPR. */
16609 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16610 complete_type (tmp);
16611 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16612 if ((VAR_P (fld)
16613 || (TREE_CODE (fld) == FUNCTION_DECL
16614 && !DECL_ARTIFICIAL (fld)))
16615 && DECL_TEMPLATE_INSTANTIATION (fld))
16616 instantiate_decl (fld, /*defer_ok=*/false,
16617 /*expl_inst_class=*/false);
16618 }
16619 break;
16620
16621 case STATIC_ASSERT:
16622 {
16623 tree condition;
16624
16625 ++c_inhibit_evaluation_warnings;
16626 condition =
16627 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16628 args,
16629 complain, in_decl,
16630 /*integral_constant_expression_p=*/true);
16631 --c_inhibit_evaluation_warnings;
16632
16633 finish_static_assert (condition,
16634 STATIC_ASSERT_MESSAGE (t),
16635 STATIC_ASSERT_SOURCE_LOCATION (t),
16636 /*member_p=*/false);
16637 }
16638 break;
16639
16640 case OACC_KERNELS:
16641 case OACC_PARALLEL:
16642 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16643 in_decl);
16644 stmt = begin_omp_parallel ();
16645 RECUR (OMP_BODY (t));
16646 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16647 break;
16648
16649 case OMP_PARALLEL:
16650 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16651 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16652 complain, in_decl);
16653 if (OMP_PARALLEL_COMBINED (t))
16654 omp_parallel_combined_clauses = &tmp;
16655 stmt = begin_omp_parallel ();
16656 RECUR (OMP_PARALLEL_BODY (t));
16657 gcc_assert (omp_parallel_combined_clauses == NULL);
16658 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16659 = OMP_PARALLEL_COMBINED (t);
16660 pop_omp_privatization_clauses (r);
16661 break;
16662
16663 case OMP_TASK:
16664 r = push_omp_privatization_clauses (false);
16665 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16666 complain, in_decl);
16667 stmt = begin_omp_task ();
16668 RECUR (OMP_TASK_BODY (t));
16669 finish_omp_task (tmp, stmt);
16670 pop_omp_privatization_clauses (r);
16671 break;
16672
16673 case OMP_FOR:
16674 case OMP_SIMD:
16675 case OMP_DISTRIBUTE:
16676 case OMP_TASKLOOP:
16677 case OACC_LOOP:
16678 {
16679 tree clauses, body, pre_body;
16680 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16681 tree orig_declv = NULL_TREE;
16682 tree incrv = NULL_TREE;
16683 enum c_omp_region_type ort = C_ORT_OMP;
16684 int i;
16685
16686 if (TREE_CODE (t) == OACC_LOOP)
16687 ort = C_ORT_ACC;
16688
16689 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16690 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16691 in_decl);
16692 if (OMP_FOR_INIT (t) != NULL_TREE)
16693 {
16694 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16695 if (OMP_FOR_ORIG_DECLS (t))
16696 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16697 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16698 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16699 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16700 }
16701
16702 stmt = begin_omp_structured_block ();
16703
16704 pre_body = push_stmt_list ();
16705 RECUR (OMP_FOR_PRE_BODY (t));
16706 pre_body = pop_stmt_list (pre_body);
16707
16708 if (OMP_FOR_INIT (t) != NULL_TREE)
16709 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16710 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16711 incrv, &clauses, args, complain, in_decl,
16712 integral_constant_expression_p);
16713 omp_parallel_combined_clauses = NULL;
16714
16715 body = push_stmt_list ();
16716 RECUR (OMP_FOR_BODY (t));
16717 body = pop_stmt_list (body);
16718
16719 if (OMP_FOR_INIT (t) != NULL_TREE)
16720 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16721 orig_declv, initv, condv, incrv, body, pre_body,
16722 NULL, clauses);
16723 else
16724 {
16725 t = make_node (TREE_CODE (t));
16726 TREE_TYPE (t) = void_type_node;
16727 OMP_FOR_BODY (t) = body;
16728 OMP_FOR_PRE_BODY (t) = pre_body;
16729 OMP_FOR_CLAUSES (t) = clauses;
16730 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16731 add_stmt (t);
16732 }
16733
16734 add_stmt (finish_omp_structured_block (stmt));
16735 pop_omp_privatization_clauses (r);
16736 }
16737 break;
16738
16739 case OMP_SECTIONS:
16740 omp_parallel_combined_clauses = NULL;
16741 /* FALLTHRU */
16742 case OMP_SINGLE:
16743 case OMP_TEAMS:
16744 case OMP_CRITICAL:
16745 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16746 && OMP_TEAMS_COMBINED (t));
16747 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16748 in_decl);
16749 stmt = push_stmt_list ();
16750 RECUR (OMP_BODY (t));
16751 stmt = pop_stmt_list (stmt);
16752
16753 t = copy_node (t);
16754 OMP_BODY (t) = stmt;
16755 OMP_CLAUSES (t) = tmp;
16756 add_stmt (t);
16757 pop_omp_privatization_clauses (r);
16758 break;
16759
16760 case OACC_DATA:
16761 case OMP_TARGET_DATA:
16762 case OMP_TARGET:
16763 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16764 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16765 in_decl);
16766 keep_next_level (true);
16767 stmt = begin_omp_structured_block ();
16768
16769 RECUR (OMP_BODY (t));
16770 stmt = finish_omp_structured_block (stmt);
16771
16772 t = copy_node (t);
16773 OMP_BODY (t) = stmt;
16774 OMP_CLAUSES (t) = tmp;
16775 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16776 {
16777 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16778 if (teams)
16779 {
16780 /* For combined target teams, ensure the num_teams and
16781 thread_limit clause expressions are evaluated on the host,
16782 before entering the target construct. */
16783 tree c;
16784 for (c = OMP_TEAMS_CLAUSES (teams);
16785 c; c = OMP_CLAUSE_CHAIN (c))
16786 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16787 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16788 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16789 {
16790 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16791 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16792 if (expr == error_mark_node)
16793 continue;
16794 tmp = TARGET_EXPR_SLOT (expr);
16795 add_stmt (expr);
16796 OMP_CLAUSE_OPERAND (c, 0) = expr;
16797 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16798 OMP_CLAUSE_FIRSTPRIVATE);
16799 OMP_CLAUSE_DECL (tc) = tmp;
16800 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16801 OMP_TARGET_CLAUSES (t) = tc;
16802 }
16803 }
16804 }
16805 add_stmt (t);
16806 break;
16807
16808 case OACC_DECLARE:
16809 t = copy_node (t);
16810 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16811 complain, in_decl);
16812 OACC_DECLARE_CLAUSES (t) = tmp;
16813 add_stmt (t);
16814 break;
16815
16816 case OMP_TARGET_UPDATE:
16817 case OMP_TARGET_ENTER_DATA:
16818 case OMP_TARGET_EXIT_DATA:
16819 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16820 complain, in_decl);
16821 t = copy_node (t);
16822 OMP_STANDALONE_CLAUSES (t) = tmp;
16823 add_stmt (t);
16824 break;
16825
16826 case OACC_ENTER_DATA:
16827 case OACC_EXIT_DATA:
16828 case OACC_UPDATE:
16829 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16830 complain, in_decl);
16831 t = copy_node (t);
16832 OMP_STANDALONE_CLAUSES (t) = tmp;
16833 add_stmt (t);
16834 break;
16835
16836 case OMP_ORDERED:
16837 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16838 complain, in_decl);
16839 stmt = push_stmt_list ();
16840 RECUR (OMP_BODY (t));
16841 stmt = pop_stmt_list (stmt);
16842
16843 t = copy_node (t);
16844 OMP_BODY (t) = stmt;
16845 OMP_ORDERED_CLAUSES (t) = tmp;
16846 add_stmt (t);
16847 break;
16848
16849 case OMP_SECTION:
16850 case OMP_MASTER:
16851 case OMP_TASKGROUP:
16852 stmt = push_stmt_list ();
16853 RECUR (OMP_BODY (t));
16854 stmt = pop_stmt_list (stmt);
16855
16856 t = copy_node (t);
16857 OMP_BODY (t) = stmt;
16858 add_stmt (t);
16859 break;
16860
16861 case OMP_ATOMIC:
16862 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16863 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16864 {
16865 tree op1 = TREE_OPERAND (t, 1);
16866 tree rhs1 = NULL_TREE;
16867 tree lhs, rhs;
16868 if (TREE_CODE (op1) == COMPOUND_EXPR)
16869 {
16870 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16871 op1 = TREE_OPERAND (op1, 1);
16872 }
16873 lhs = RECUR (TREE_OPERAND (op1, 0));
16874 rhs = RECUR (TREE_OPERAND (op1, 1));
16875 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16876 NULL_TREE, NULL_TREE, rhs1,
16877 OMP_ATOMIC_SEQ_CST (t));
16878 }
16879 else
16880 {
16881 tree op1 = TREE_OPERAND (t, 1);
16882 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16883 tree rhs1 = NULL_TREE;
16884 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16885 enum tree_code opcode = NOP_EXPR;
16886 if (code == OMP_ATOMIC_READ)
16887 {
16888 v = RECUR (TREE_OPERAND (op1, 0));
16889 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16890 }
16891 else if (code == OMP_ATOMIC_CAPTURE_OLD
16892 || code == OMP_ATOMIC_CAPTURE_NEW)
16893 {
16894 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16895 v = RECUR (TREE_OPERAND (op1, 0));
16896 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16897 if (TREE_CODE (op11) == COMPOUND_EXPR)
16898 {
16899 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16900 op11 = TREE_OPERAND (op11, 1);
16901 }
16902 lhs = RECUR (TREE_OPERAND (op11, 0));
16903 rhs = RECUR (TREE_OPERAND (op11, 1));
16904 opcode = TREE_CODE (op11);
16905 if (opcode == MODIFY_EXPR)
16906 opcode = NOP_EXPR;
16907 }
16908 else
16909 {
16910 code = OMP_ATOMIC;
16911 lhs = RECUR (TREE_OPERAND (op1, 0));
16912 rhs = RECUR (TREE_OPERAND (op1, 1));
16913 }
16914 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16915 OMP_ATOMIC_SEQ_CST (t));
16916 }
16917 break;
16918
16919 case TRANSACTION_EXPR:
16920 {
16921 int flags = 0;
16922 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16923 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16924
16925 if (TRANSACTION_EXPR_IS_STMT (t))
16926 {
16927 tree body = TRANSACTION_EXPR_BODY (t);
16928 tree noex = NULL_TREE;
16929 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16930 {
16931 noex = MUST_NOT_THROW_COND (body);
16932 if (noex == NULL_TREE)
16933 noex = boolean_true_node;
16934 body = TREE_OPERAND (body, 0);
16935 }
16936 stmt = begin_transaction_stmt (input_location, NULL, flags);
16937 RECUR (body);
16938 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16939 }
16940 else
16941 {
16942 stmt = build_transaction_expr (EXPR_LOCATION (t),
16943 RECUR (TRANSACTION_EXPR_BODY (t)),
16944 flags, NULL_TREE);
16945 RETURN (stmt);
16946 }
16947 }
16948 break;
16949
16950 case MUST_NOT_THROW_EXPR:
16951 {
16952 tree op0 = RECUR (TREE_OPERAND (t, 0));
16953 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16954 RETURN (build_must_not_throw_expr (op0, cond));
16955 }
16956
16957 case EXPR_PACK_EXPANSION:
16958 error ("invalid use of pack expansion expression");
16959 RETURN (error_mark_node);
16960
16961 case NONTYPE_ARGUMENT_PACK:
16962 error ("use %<...%> to expand argument pack");
16963 RETURN (error_mark_node);
16964
16965 case COMPOUND_EXPR:
16966 tmp = RECUR (TREE_OPERAND (t, 0));
16967 if (tmp == NULL_TREE)
16968 /* If the first operand was a statement, we're done with it. */
16969 RETURN (RECUR (TREE_OPERAND (t, 1)));
16970 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16971 RECUR (TREE_OPERAND (t, 1)),
16972 complain));
16973
16974 case ANNOTATE_EXPR:
16975 tmp = RECUR (TREE_OPERAND (t, 0));
16976 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16977 TREE_TYPE (tmp), tmp,
16978 RECUR (TREE_OPERAND (t, 1)),
16979 RECUR (TREE_OPERAND (t, 2))));
16980
16981 default:
16982 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16983
16984 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16985 /*function_p=*/false,
16986 integral_constant_expression_p));
16987 }
16988
16989 RETURN (NULL_TREE);
16990 out:
16991 input_location = loc;
16992 return r;
16993 #undef RECUR
16994 #undef RETURN
16995 }
16996
16997 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16998 function. For description of the body see comment above
16999 cp_parser_omp_declare_reduction_exprs. */
17000
17001 static void
17002 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17003 {
17004 if (t == NULL_TREE || t == error_mark_node)
17005 return;
17006
17007 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17008
17009 tree_stmt_iterator tsi;
17010 int i;
17011 tree stmts[7];
17012 memset (stmts, 0, sizeof stmts);
17013 for (i = 0, tsi = tsi_start (t);
17014 i < 7 && !tsi_end_p (tsi);
17015 i++, tsi_next (&tsi))
17016 stmts[i] = tsi_stmt (tsi);
17017 gcc_assert (tsi_end_p (tsi));
17018
17019 if (i >= 3)
17020 {
17021 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17022 && TREE_CODE (stmts[1]) == DECL_EXPR);
17023 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17024 args, complain, in_decl);
17025 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17026 args, complain, in_decl);
17027 DECL_CONTEXT (omp_out) = current_function_decl;
17028 DECL_CONTEXT (omp_in) = current_function_decl;
17029 keep_next_level (true);
17030 tree block = begin_omp_structured_block ();
17031 tsubst_expr (stmts[2], args, complain, in_decl, false);
17032 block = finish_omp_structured_block (block);
17033 block = maybe_cleanup_point_expr_void (block);
17034 add_decl_expr (omp_out);
17035 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17036 TREE_NO_WARNING (omp_out) = 1;
17037 add_decl_expr (omp_in);
17038 finish_expr_stmt (block);
17039 }
17040 if (i >= 6)
17041 {
17042 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17043 && TREE_CODE (stmts[4]) == DECL_EXPR);
17044 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17045 args, complain, in_decl);
17046 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17047 args, complain, in_decl);
17048 DECL_CONTEXT (omp_priv) = current_function_decl;
17049 DECL_CONTEXT (omp_orig) = current_function_decl;
17050 keep_next_level (true);
17051 tree block = begin_omp_structured_block ();
17052 tsubst_expr (stmts[5], args, complain, in_decl, false);
17053 block = finish_omp_structured_block (block);
17054 block = maybe_cleanup_point_expr_void (block);
17055 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17056 add_decl_expr (omp_priv);
17057 add_decl_expr (omp_orig);
17058 finish_expr_stmt (block);
17059 if (i == 7)
17060 add_decl_expr (omp_orig);
17061 }
17062 }
17063
17064 /* T is a postfix-expression that is not being used in a function
17065 call. Return the substituted version of T. */
17066
17067 static tree
17068 tsubst_non_call_postfix_expression (tree t, tree args,
17069 tsubst_flags_t complain,
17070 tree in_decl)
17071 {
17072 if (TREE_CODE (t) == SCOPE_REF)
17073 t = tsubst_qualified_id (t, args, complain, in_decl,
17074 /*done=*/false, /*address_p=*/false);
17075 else
17076 t = tsubst_copy_and_build (t, args, complain, in_decl,
17077 /*function_p=*/false,
17078 /*integral_constant_expression_p=*/false);
17079
17080 return t;
17081 }
17082
17083 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17084 instantiation context. Instantiating a pack expansion containing a lambda
17085 might result in multiple lambdas all based on the same lambda in the
17086 template. */
17087
17088 tree
17089 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17090 {
17091 tree oldfn = lambda_function (t);
17092 in_decl = oldfn;
17093
17094 tree r = build_lambda_expr ();
17095
17096 LAMBDA_EXPR_LOCATION (r)
17097 = LAMBDA_EXPR_LOCATION (t);
17098 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17099 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17100 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17101
17102 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
17103 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
17104 else
17105 record_lambda_scope (r);
17106
17107 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17108 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17109
17110 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
17111 cap = TREE_CHAIN (cap))
17112 {
17113 tree field = TREE_PURPOSE (cap);
17114 if (PACK_EXPANSION_P (field))
17115 field = PACK_EXPANSION_PATTERN (field);
17116 field = tsubst_decl (field, args, complain);
17117
17118 if (field == error_mark_node)
17119 return error_mark_node;
17120
17121 tree init = TREE_VALUE (cap);
17122 if (PACK_EXPANSION_P (init))
17123 init = tsubst_pack_expansion (init, args, complain, in_decl);
17124 else
17125 init = tsubst_copy_and_build (init, args, complain, in_decl,
17126 /*fn*/false, /*constexpr*/false);
17127
17128 if (TREE_CODE (field) == TREE_VEC)
17129 {
17130 int len = TREE_VEC_LENGTH (field);
17131 gcc_assert (TREE_CODE (init) == TREE_VEC
17132 && TREE_VEC_LENGTH (init) == len);
17133 for (int i = 0; i < len; ++i)
17134 LAMBDA_EXPR_CAPTURE_LIST (r)
17135 = tree_cons (TREE_VEC_ELT (field, i),
17136 TREE_VEC_ELT (init, i),
17137 LAMBDA_EXPR_CAPTURE_LIST (r));
17138 }
17139 else
17140 {
17141 LAMBDA_EXPR_CAPTURE_LIST (r)
17142 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17143
17144 if (id_equal (DECL_NAME (field), "__this"))
17145 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17146 }
17147 }
17148
17149 tree type = begin_lambda_type (r);
17150
17151 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17152 determine_visibility (TYPE_NAME (type));
17153
17154 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17155
17156 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17157 ? DECL_TI_TEMPLATE (oldfn)
17158 : NULL_TREE);
17159
17160 tree fntype = static_fn_type (oldfn);
17161 if (oldtmpl)
17162 ++processing_template_decl;
17163 fntype = tsubst (fntype, args, complain, in_decl);
17164 if (oldtmpl)
17165 --processing_template_decl;
17166
17167 if (fntype == error_mark_node)
17168 r = error_mark_node;
17169 else
17170 {
17171 /* Fix the type of 'this'. */
17172 fntype = build_memfn_type (fntype, type,
17173 type_memfn_quals (fntype),
17174 type_memfn_rqual (fntype));
17175 tree fn, tmpl;
17176 if (oldtmpl)
17177 {
17178 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17179 fn = DECL_TEMPLATE_RESULT (tmpl);
17180 finish_member_declaration (tmpl);
17181 }
17182 else
17183 {
17184 tmpl = NULL_TREE;
17185 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17186 finish_member_declaration (fn);
17187 }
17188
17189 /* Let finish_function set this. */
17190 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17191
17192 bool nested = cfun;
17193 if (nested)
17194 push_function_context ();
17195 else
17196 /* Still increment function_depth so that we don't GC in the
17197 middle of an expression. */
17198 ++function_depth;
17199
17200 local_specialization_stack s (lss_copy);
17201
17202 tree body = start_lambda_function (fn, r);
17203
17204 register_parameter_specializations (oldfn, fn);
17205
17206 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17207 /*constexpr*/false);
17208
17209 finish_lambda_function (body);
17210
17211 if (nested)
17212 pop_function_context ();
17213 else
17214 --function_depth;
17215
17216 /* The capture list was built up in reverse order; fix that now. */
17217 LAMBDA_EXPR_CAPTURE_LIST (r)
17218 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17219
17220 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17221
17222 maybe_add_lambda_conv_op (type);
17223 }
17224
17225 finish_struct (type, /*attr*/NULL_TREE);
17226
17227 insert_pending_capture_proxies ();
17228
17229 return r;
17230 }
17231
17232 /* Like tsubst but deals with expressions and performs semantic
17233 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17234
17235 tree
17236 tsubst_copy_and_build (tree t,
17237 tree args,
17238 tsubst_flags_t complain,
17239 tree in_decl,
17240 bool function_p,
17241 bool integral_constant_expression_p)
17242 {
17243 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17244 #define RECUR(NODE) \
17245 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17246 /*function_p=*/false, \
17247 integral_constant_expression_p)
17248
17249 tree retval, op1;
17250 location_t loc;
17251
17252 if (t == NULL_TREE || t == error_mark_node)
17253 return t;
17254
17255 loc = input_location;
17256 if (EXPR_HAS_LOCATION (t))
17257 input_location = EXPR_LOCATION (t);
17258
17259 /* N3276 decltype magic only applies to calls at the top level or on the
17260 right side of a comma. */
17261 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17262 complain &= ~tf_decltype;
17263
17264 switch (TREE_CODE (t))
17265 {
17266 case USING_DECL:
17267 t = DECL_NAME (t);
17268 /* Fall through. */
17269 case IDENTIFIER_NODE:
17270 {
17271 tree decl;
17272 cp_id_kind idk;
17273 bool non_integral_constant_expression_p;
17274 const char *error_msg;
17275
17276 if (IDENTIFIER_CONV_OP_P (t))
17277 {
17278 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17279 t = make_conv_op_name (new_type);
17280 }
17281
17282 /* Look up the name. */
17283 decl = lookup_name (t);
17284
17285 /* By convention, expressions use ERROR_MARK_NODE to indicate
17286 failure, not NULL_TREE. */
17287 if (decl == NULL_TREE)
17288 decl = error_mark_node;
17289
17290 decl = finish_id_expression (t, decl, NULL_TREE,
17291 &idk,
17292 integral_constant_expression_p,
17293 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17294 &non_integral_constant_expression_p,
17295 /*template_p=*/false,
17296 /*done=*/true,
17297 /*address_p=*/false,
17298 /*template_arg_p=*/false,
17299 &error_msg,
17300 input_location);
17301 if (error_msg)
17302 error (error_msg);
17303 if (!function_p && identifier_p (decl))
17304 {
17305 if (complain & tf_error)
17306 unqualified_name_lookup_error (decl);
17307 decl = error_mark_node;
17308 }
17309 RETURN (decl);
17310 }
17311
17312 case TEMPLATE_ID_EXPR:
17313 {
17314 tree object;
17315 tree templ = RECUR (TREE_OPERAND (t, 0));
17316 tree targs = TREE_OPERAND (t, 1);
17317
17318 if (targs)
17319 targs = tsubst_template_args (targs, args, complain, in_decl);
17320 if (targs == error_mark_node)
17321 return error_mark_node;
17322
17323 if (TREE_CODE (templ) == SCOPE_REF)
17324 {
17325 tree name = TREE_OPERAND (templ, 1);
17326 tree tid = lookup_template_function (name, targs);
17327 TREE_OPERAND (templ, 1) = tid;
17328 return templ;
17329 }
17330
17331 if (variable_template_p (templ))
17332 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17333
17334 if (TREE_CODE (templ) == COMPONENT_REF)
17335 {
17336 object = TREE_OPERAND (templ, 0);
17337 templ = TREE_OPERAND (templ, 1);
17338 }
17339 else
17340 object = NULL_TREE;
17341 templ = lookup_template_function (templ, targs);
17342
17343 if (object)
17344 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17345 object, templ, NULL_TREE));
17346 else
17347 RETURN (baselink_for_fns (templ));
17348 }
17349
17350 case INDIRECT_REF:
17351 {
17352 tree r = RECUR (TREE_OPERAND (t, 0));
17353
17354 if (REFERENCE_REF_P (t))
17355 {
17356 /* A type conversion to reference type will be enclosed in
17357 such an indirect ref, but the substitution of the cast
17358 will have also added such an indirect ref. */
17359 r = convert_from_reference (r);
17360 }
17361 else
17362 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17363 complain|decltype_flag);
17364
17365 if (REF_PARENTHESIZED_P (t))
17366 r = force_paren_expr (r);
17367
17368 RETURN (r);
17369 }
17370
17371 case NOP_EXPR:
17372 {
17373 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17374 tree op0 = RECUR (TREE_OPERAND (t, 0));
17375 RETURN (build_nop (type, op0));
17376 }
17377
17378 case IMPLICIT_CONV_EXPR:
17379 {
17380 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17381 tree expr = RECUR (TREE_OPERAND (t, 0));
17382 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17383 {
17384 retval = copy_node (t);
17385 TREE_TYPE (retval) = type;
17386 TREE_OPERAND (retval, 0) = expr;
17387 RETURN (retval);
17388 }
17389 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17390 /* We'll pass this to convert_nontype_argument again, we don't need
17391 to actually perform any conversion here. */
17392 RETURN (expr);
17393 int flags = LOOKUP_IMPLICIT;
17394 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17395 flags = LOOKUP_NORMAL;
17396 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17397 flags));
17398 }
17399
17400 case CONVERT_EXPR:
17401 {
17402 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17403 tree op0 = RECUR (TREE_OPERAND (t, 0));
17404 RETURN (build1 (CONVERT_EXPR, type, op0));
17405 }
17406
17407 case CAST_EXPR:
17408 case REINTERPRET_CAST_EXPR:
17409 case CONST_CAST_EXPR:
17410 case DYNAMIC_CAST_EXPR:
17411 case STATIC_CAST_EXPR:
17412 {
17413 tree type;
17414 tree op, r = NULL_TREE;
17415
17416 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17417 if (integral_constant_expression_p
17418 && !cast_valid_in_integral_constant_expression_p (type))
17419 {
17420 if (complain & tf_error)
17421 error ("a cast to a type other than an integral or "
17422 "enumeration type cannot appear in a constant-expression");
17423 RETURN (error_mark_node);
17424 }
17425
17426 op = RECUR (TREE_OPERAND (t, 0));
17427
17428 warning_sentinel s(warn_useless_cast);
17429 warning_sentinel s2(warn_ignored_qualifiers);
17430 switch (TREE_CODE (t))
17431 {
17432 case CAST_EXPR:
17433 r = build_functional_cast (type, op, complain);
17434 break;
17435 case REINTERPRET_CAST_EXPR:
17436 r = build_reinterpret_cast (type, op, complain);
17437 break;
17438 case CONST_CAST_EXPR:
17439 r = build_const_cast (type, op, complain);
17440 break;
17441 case DYNAMIC_CAST_EXPR:
17442 r = build_dynamic_cast (type, op, complain);
17443 break;
17444 case STATIC_CAST_EXPR:
17445 r = build_static_cast (type, op, complain);
17446 break;
17447 default:
17448 gcc_unreachable ();
17449 }
17450
17451 RETURN (r);
17452 }
17453
17454 case POSTDECREMENT_EXPR:
17455 case POSTINCREMENT_EXPR:
17456 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17457 args, complain, in_decl);
17458 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17459 complain|decltype_flag));
17460
17461 case PREDECREMENT_EXPR:
17462 case PREINCREMENT_EXPR:
17463 case NEGATE_EXPR:
17464 case BIT_NOT_EXPR:
17465 case ABS_EXPR:
17466 case TRUTH_NOT_EXPR:
17467 case UNARY_PLUS_EXPR: /* Unary + */
17468 case REALPART_EXPR:
17469 case IMAGPART_EXPR:
17470 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17471 RECUR (TREE_OPERAND (t, 0)),
17472 complain|decltype_flag));
17473
17474 case FIX_TRUNC_EXPR:
17475 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17476 false, complain));
17477
17478 case ADDR_EXPR:
17479 op1 = TREE_OPERAND (t, 0);
17480 if (TREE_CODE (op1) == LABEL_DECL)
17481 RETURN (finish_label_address_expr (DECL_NAME (op1),
17482 EXPR_LOCATION (op1)));
17483 if (TREE_CODE (op1) == SCOPE_REF)
17484 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17485 /*done=*/true, /*address_p=*/true);
17486 else
17487 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17488 in_decl);
17489 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17490 complain|decltype_flag));
17491
17492 case PLUS_EXPR:
17493 case MINUS_EXPR:
17494 case MULT_EXPR:
17495 case TRUNC_DIV_EXPR:
17496 case CEIL_DIV_EXPR:
17497 case FLOOR_DIV_EXPR:
17498 case ROUND_DIV_EXPR:
17499 case EXACT_DIV_EXPR:
17500 case BIT_AND_EXPR:
17501 case BIT_IOR_EXPR:
17502 case BIT_XOR_EXPR:
17503 case TRUNC_MOD_EXPR:
17504 case FLOOR_MOD_EXPR:
17505 case TRUTH_ANDIF_EXPR:
17506 case TRUTH_ORIF_EXPR:
17507 case TRUTH_AND_EXPR:
17508 case TRUTH_OR_EXPR:
17509 case RSHIFT_EXPR:
17510 case LSHIFT_EXPR:
17511 case RROTATE_EXPR:
17512 case LROTATE_EXPR:
17513 case EQ_EXPR:
17514 case NE_EXPR:
17515 case MAX_EXPR:
17516 case MIN_EXPR:
17517 case LE_EXPR:
17518 case GE_EXPR:
17519 case LT_EXPR:
17520 case GT_EXPR:
17521 case MEMBER_REF:
17522 case DOTSTAR_EXPR:
17523 {
17524 warning_sentinel s1(warn_type_limits);
17525 warning_sentinel s2(warn_div_by_zero);
17526 warning_sentinel s3(warn_logical_op);
17527 warning_sentinel s4(warn_tautological_compare);
17528 tree op0 = RECUR (TREE_OPERAND (t, 0));
17529 tree op1 = RECUR (TREE_OPERAND (t, 1));
17530 tree r = build_x_binary_op
17531 (input_location, TREE_CODE (t),
17532 op0,
17533 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17534 ? ERROR_MARK
17535 : TREE_CODE (TREE_OPERAND (t, 0))),
17536 op1,
17537 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17538 ? ERROR_MARK
17539 : TREE_CODE (TREE_OPERAND (t, 1))),
17540 /*overload=*/NULL,
17541 complain|decltype_flag);
17542 if (EXPR_P (r) && TREE_NO_WARNING (t))
17543 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17544
17545 RETURN (r);
17546 }
17547
17548 case POINTER_PLUS_EXPR:
17549 {
17550 tree op0 = RECUR (TREE_OPERAND (t, 0));
17551 tree op1 = RECUR (TREE_OPERAND (t, 1));
17552 return fold_build_pointer_plus (op0, op1);
17553 }
17554
17555 case SCOPE_REF:
17556 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17557 /*address_p=*/false));
17558 case ARRAY_REF:
17559 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17560 args, complain, in_decl);
17561 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17562 RECUR (TREE_OPERAND (t, 1)),
17563 complain|decltype_flag));
17564
17565 case SIZEOF_EXPR:
17566 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17567 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17568 RETURN (tsubst_copy (t, args, complain, in_decl));
17569 /* Fall through */
17570
17571 case ALIGNOF_EXPR:
17572 {
17573 tree r;
17574
17575 op1 = TREE_OPERAND (t, 0);
17576 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17577 op1 = TREE_TYPE (op1);
17578 if (!args)
17579 {
17580 /* When there are no ARGS, we are trying to evaluate a
17581 non-dependent expression from the parser. Trying to do
17582 the substitutions may not work. */
17583 if (!TYPE_P (op1))
17584 op1 = TREE_TYPE (op1);
17585 }
17586 else
17587 {
17588 ++cp_unevaluated_operand;
17589 ++c_inhibit_evaluation_warnings;
17590 if (TYPE_P (op1))
17591 op1 = tsubst (op1, args, complain, in_decl);
17592 else
17593 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17594 /*function_p=*/false,
17595 /*integral_constant_expression_p=*/
17596 false);
17597 --cp_unevaluated_operand;
17598 --c_inhibit_evaluation_warnings;
17599 }
17600 if (TYPE_P (op1))
17601 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17602 complain & tf_error);
17603 else
17604 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17605 complain & tf_error);
17606 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17607 {
17608 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17609 {
17610 if (!processing_template_decl && TYPE_P (op1))
17611 {
17612 r = build_min (SIZEOF_EXPR, size_type_node,
17613 build1 (NOP_EXPR, op1, error_mark_node));
17614 SIZEOF_EXPR_TYPE_P (r) = 1;
17615 }
17616 else
17617 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17618 TREE_SIDE_EFFECTS (r) = 0;
17619 TREE_READONLY (r) = 1;
17620 }
17621 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17622 }
17623 RETURN (r);
17624 }
17625
17626 case AT_ENCODE_EXPR:
17627 {
17628 op1 = TREE_OPERAND (t, 0);
17629 ++cp_unevaluated_operand;
17630 ++c_inhibit_evaluation_warnings;
17631 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17632 /*function_p=*/false,
17633 /*integral_constant_expression_p=*/false);
17634 --cp_unevaluated_operand;
17635 --c_inhibit_evaluation_warnings;
17636 RETURN (objc_build_encode_expr (op1));
17637 }
17638
17639 case NOEXCEPT_EXPR:
17640 op1 = TREE_OPERAND (t, 0);
17641 ++cp_unevaluated_operand;
17642 ++c_inhibit_evaluation_warnings;
17643 ++cp_noexcept_operand;
17644 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17645 /*function_p=*/false,
17646 /*integral_constant_expression_p=*/false);
17647 --cp_unevaluated_operand;
17648 --c_inhibit_evaluation_warnings;
17649 --cp_noexcept_operand;
17650 RETURN (finish_noexcept_expr (op1, complain));
17651
17652 case MODOP_EXPR:
17653 {
17654 warning_sentinel s(warn_div_by_zero);
17655 tree lhs = RECUR (TREE_OPERAND (t, 0));
17656 tree rhs = RECUR (TREE_OPERAND (t, 2));
17657 tree r = build_x_modify_expr
17658 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17659 complain|decltype_flag);
17660 /* TREE_NO_WARNING must be set if either the expression was
17661 parenthesized or it uses an operator such as >>= rather
17662 than plain assignment. In the former case, it was already
17663 set and must be copied. In the latter case,
17664 build_x_modify_expr sets it and it must not be reset
17665 here. */
17666 if (TREE_NO_WARNING (t))
17667 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17668
17669 RETURN (r);
17670 }
17671
17672 case ARROW_EXPR:
17673 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17674 args, complain, in_decl);
17675 /* Remember that there was a reference to this entity. */
17676 if (DECL_P (op1)
17677 && !mark_used (op1, complain) && !(complain & tf_error))
17678 RETURN (error_mark_node);
17679 RETURN (build_x_arrow (input_location, op1, complain));
17680
17681 case NEW_EXPR:
17682 {
17683 tree placement = RECUR (TREE_OPERAND (t, 0));
17684 tree init = RECUR (TREE_OPERAND (t, 3));
17685 vec<tree, va_gc> *placement_vec;
17686 vec<tree, va_gc> *init_vec;
17687 tree ret;
17688
17689 if (placement == NULL_TREE)
17690 placement_vec = NULL;
17691 else
17692 {
17693 placement_vec = make_tree_vector ();
17694 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17695 vec_safe_push (placement_vec, TREE_VALUE (placement));
17696 }
17697
17698 /* If there was an initializer in the original tree, but it
17699 instantiated to an empty list, then we should pass a
17700 non-NULL empty vector to tell build_new that it was an
17701 empty initializer() rather than no initializer. This can
17702 only happen when the initializer is a pack expansion whose
17703 parameter packs are of length zero. */
17704 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17705 init_vec = NULL;
17706 else
17707 {
17708 init_vec = make_tree_vector ();
17709 if (init == void_node)
17710 gcc_assert (init_vec != NULL);
17711 else
17712 {
17713 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17714 vec_safe_push (init_vec, TREE_VALUE (init));
17715 }
17716 }
17717
17718 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17719 tree op2 = RECUR (TREE_OPERAND (t, 2));
17720 ret = build_new (&placement_vec, op1, op2, &init_vec,
17721 NEW_EXPR_USE_GLOBAL (t),
17722 complain);
17723
17724 if (placement_vec != NULL)
17725 release_tree_vector (placement_vec);
17726 if (init_vec != NULL)
17727 release_tree_vector (init_vec);
17728
17729 RETURN (ret);
17730 }
17731
17732 case DELETE_EXPR:
17733 {
17734 tree op0 = RECUR (TREE_OPERAND (t, 0));
17735 tree op1 = RECUR (TREE_OPERAND (t, 1));
17736 RETURN (delete_sanity (op0, op1,
17737 DELETE_EXPR_USE_VEC (t),
17738 DELETE_EXPR_USE_GLOBAL (t),
17739 complain));
17740 }
17741
17742 case COMPOUND_EXPR:
17743 {
17744 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17745 complain & ~tf_decltype, in_decl,
17746 /*function_p=*/false,
17747 integral_constant_expression_p);
17748 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17749 op0,
17750 RECUR (TREE_OPERAND (t, 1)),
17751 complain|decltype_flag));
17752 }
17753
17754 case CALL_EXPR:
17755 {
17756 tree function;
17757 vec<tree, va_gc> *call_args;
17758 unsigned int nargs, i;
17759 bool qualified_p;
17760 bool koenig_p;
17761 tree ret;
17762
17763 function = CALL_EXPR_FN (t);
17764 /* Internal function with no arguments. */
17765 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17766 RETURN (t);
17767
17768 /* When we parsed the expression, we determined whether or
17769 not Koenig lookup should be performed. */
17770 koenig_p = KOENIG_LOOKUP_P (t);
17771 if (function == NULL_TREE)
17772 {
17773 koenig_p = false;
17774 qualified_p = false;
17775 }
17776 else if (TREE_CODE (function) == SCOPE_REF)
17777 {
17778 qualified_p = true;
17779 function = tsubst_qualified_id (function, args, complain, in_decl,
17780 /*done=*/false,
17781 /*address_p=*/false);
17782 }
17783 else if (koenig_p && identifier_p (function))
17784 {
17785 /* Do nothing; calling tsubst_copy_and_build on an identifier
17786 would incorrectly perform unqualified lookup again.
17787
17788 Note that we can also have an IDENTIFIER_NODE if the earlier
17789 unqualified lookup found a member function; in that case
17790 koenig_p will be false and we do want to do the lookup
17791 again to find the instantiated member function.
17792
17793 FIXME but doing that causes c++/15272, so we need to stop
17794 using IDENTIFIER_NODE in that situation. */
17795 qualified_p = false;
17796 }
17797 else
17798 {
17799 if (TREE_CODE (function) == COMPONENT_REF)
17800 {
17801 tree op = TREE_OPERAND (function, 1);
17802
17803 qualified_p = (TREE_CODE (op) == SCOPE_REF
17804 || (BASELINK_P (op)
17805 && BASELINK_QUALIFIED_P (op)));
17806 }
17807 else
17808 qualified_p = false;
17809
17810 if (TREE_CODE (function) == ADDR_EXPR
17811 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17812 /* Avoid error about taking the address of a constructor. */
17813 function = TREE_OPERAND (function, 0);
17814
17815 function = tsubst_copy_and_build (function, args, complain,
17816 in_decl,
17817 !qualified_p,
17818 integral_constant_expression_p);
17819
17820 if (BASELINK_P (function))
17821 qualified_p = true;
17822 }
17823
17824 nargs = call_expr_nargs (t);
17825 call_args = make_tree_vector ();
17826 for (i = 0; i < nargs; ++i)
17827 {
17828 tree arg = CALL_EXPR_ARG (t, i);
17829
17830 if (!PACK_EXPANSION_P (arg))
17831 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17832 else
17833 {
17834 /* Expand the pack expansion and push each entry onto
17835 CALL_ARGS. */
17836 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17837 if (TREE_CODE (arg) == TREE_VEC)
17838 {
17839 unsigned int len, j;
17840
17841 len = TREE_VEC_LENGTH (arg);
17842 for (j = 0; j < len; ++j)
17843 {
17844 tree value = TREE_VEC_ELT (arg, j);
17845 if (value != NULL_TREE)
17846 value = convert_from_reference (value);
17847 vec_safe_push (call_args, value);
17848 }
17849 }
17850 else
17851 {
17852 /* A partial substitution. Add one entry. */
17853 vec_safe_push (call_args, arg);
17854 }
17855 }
17856 }
17857
17858 /* We do not perform argument-dependent lookup if normal
17859 lookup finds a non-function, in accordance with the
17860 expected resolution of DR 218. */
17861 if (koenig_p
17862 && ((is_overloaded_fn (function)
17863 /* If lookup found a member function, the Koenig lookup is
17864 not appropriate, even if an unqualified-name was used
17865 to denote the function. */
17866 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17867 || identifier_p (function))
17868 /* Only do this when substitution turns a dependent call
17869 into a non-dependent call. */
17870 && type_dependent_expression_p_push (t)
17871 && !any_type_dependent_arguments_p (call_args))
17872 function = perform_koenig_lookup (function, call_args, tf_none);
17873
17874 if (function != NULL_TREE
17875 && identifier_p (function)
17876 && !any_type_dependent_arguments_p (call_args))
17877 {
17878 if (koenig_p && (complain & tf_warning_or_error))
17879 {
17880 /* For backwards compatibility and good diagnostics, try
17881 the unqualified lookup again if we aren't in SFINAE
17882 context. */
17883 tree unq = (tsubst_copy_and_build
17884 (function, args, complain, in_decl, true,
17885 integral_constant_expression_p));
17886 if (unq == error_mark_node)
17887 {
17888 release_tree_vector (call_args);
17889 RETURN (error_mark_node);
17890 }
17891
17892 if (unq != function)
17893 {
17894 /* In a lambda fn, we have to be careful to not
17895 introduce new this captures. Legacy code can't
17896 be using lambdas anyway, so it's ok to be
17897 stricter. */
17898 bool in_lambda = (current_class_type
17899 && LAMBDA_TYPE_P (current_class_type));
17900 char const *const msg
17901 = G_("%qD was not declared in this scope, "
17902 "and no declarations were found by "
17903 "argument-dependent lookup at the point "
17904 "of instantiation");
17905
17906 bool diag = true;
17907 if (in_lambda)
17908 error_at (EXPR_LOC_OR_LOC (t, input_location),
17909 msg, function);
17910 else
17911 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17912 msg, function);
17913 if (diag)
17914 {
17915 tree fn = unq;
17916
17917 if (INDIRECT_REF_P (fn))
17918 fn = TREE_OPERAND (fn, 0);
17919 if (is_overloaded_fn (fn))
17920 fn = get_first_fn (fn);
17921
17922 if (!DECL_P (fn))
17923 /* Can't say anything more. */;
17924 else if (DECL_CLASS_SCOPE_P (fn))
17925 {
17926 location_t loc = EXPR_LOC_OR_LOC (t,
17927 input_location);
17928 inform (loc,
17929 "declarations in dependent base %qT are "
17930 "not found by unqualified lookup",
17931 DECL_CLASS_CONTEXT (fn));
17932 if (current_class_ptr)
17933 inform (loc,
17934 "use %<this->%D%> instead", function);
17935 else
17936 inform (loc,
17937 "use %<%T::%D%> instead",
17938 current_class_name, function);
17939 }
17940 else
17941 inform (DECL_SOURCE_LOCATION (fn),
17942 "%qD declared here, later in the "
17943 "translation unit", fn);
17944 if (in_lambda)
17945 {
17946 release_tree_vector (call_args);
17947 RETURN (error_mark_node);
17948 }
17949 }
17950
17951 function = unq;
17952 }
17953 }
17954 if (identifier_p (function))
17955 {
17956 if (complain & tf_error)
17957 unqualified_name_lookup_error (function);
17958 release_tree_vector (call_args);
17959 RETURN (error_mark_node);
17960 }
17961 }
17962
17963 /* Remember that there was a reference to this entity. */
17964 if (function != NULL_TREE
17965 && DECL_P (function)
17966 && !mark_used (function, complain) && !(complain & tf_error))
17967 {
17968 release_tree_vector (call_args);
17969 RETURN (error_mark_node);
17970 }
17971
17972 /* Put back tf_decltype for the actual call. */
17973 complain |= decltype_flag;
17974
17975 if (function == NULL_TREE)
17976 switch (CALL_EXPR_IFN (t))
17977 {
17978 case IFN_LAUNDER:
17979 gcc_assert (nargs == 1);
17980 if (vec_safe_length (call_args) != 1)
17981 {
17982 error_at (EXPR_LOC_OR_LOC (t, input_location),
17983 "wrong number of arguments to "
17984 "%<__builtin_launder%>");
17985 ret = error_mark_node;
17986 }
17987 else
17988 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17989 input_location),
17990 (*call_args)[0], complain);
17991 break;
17992
17993 default:
17994 /* Unsupported internal function with arguments. */
17995 gcc_unreachable ();
17996 }
17997 else if (TREE_CODE (function) == OFFSET_REF)
17998 ret = build_offset_ref_call_from_tree (function, &call_args,
17999 complain);
18000 else if (TREE_CODE (function) == COMPONENT_REF)
18001 {
18002 tree instance = TREE_OPERAND (function, 0);
18003 tree fn = TREE_OPERAND (function, 1);
18004
18005 if (processing_template_decl
18006 && (type_dependent_expression_p (instance)
18007 || (!BASELINK_P (fn)
18008 && TREE_CODE (fn) != FIELD_DECL)
18009 || type_dependent_expression_p (fn)
18010 || any_type_dependent_arguments_p (call_args)))
18011 ret = build_min_nt_call_vec (function, call_args);
18012 else if (!BASELINK_P (fn))
18013 ret = finish_call_expr (function, &call_args,
18014 /*disallow_virtual=*/false,
18015 /*koenig_p=*/false,
18016 complain);
18017 else
18018 ret = (build_new_method_call
18019 (instance, fn,
18020 &call_args, NULL_TREE,
18021 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
18022 /*fn_p=*/NULL,
18023 complain));
18024 }
18025 else
18026 ret = finish_call_expr (function, &call_args,
18027 /*disallow_virtual=*/qualified_p,
18028 koenig_p,
18029 complain);
18030
18031 release_tree_vector (call_args);
18032
18033 if (ret != error_mark_node)
18034 {
18035 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
18036 bool ord = CALL_EXPR_ORDERED_ARGS (t);
18037 bool rev = CALL_EXPR_REVERSE_ARGS (t);
18038 bool thk = CALL_FROM_THUNK_P (t);
18039 if (op || ord || rev || thk)
18040 {
18041 function = extract_call_expr (ret);
18042 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
18043 CALL_EXPR_ORDERED_ARGS (function) = ord;
18044 CALL_EXPR_REVERSE_ARGS (function) = rev;
18045 if (thk)
18046 {
18047 if (TREE_CODE (function) == CALL_EXPR)
18048 CALL_FROM_THUNK_P (function) = true;
18049 else
18050 AGGR_INIT_FROM_THUNK_P (function) = true;
18051 /* The thunk location is not interesting. */
18052 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
18053 }
18054 }
18055 }
18056
18057 RETURN (ret);
18058 }
18059
18060 case COND_EXPR:
18061 {
18062 tree cond = RECUR (TREE_OPERAND (t, 0));
18063 tree folded_cond = fold_non_dependent_expr (cond);
18064 tree exp1, exp2;
18065
18066 if (TREE_CODE (folded_cond) == INTEGER_CST)
18067 {
18068 if (integer_zerop (folded_cond))
18069 {
18070 ++c_inhibit_evaluation_warnings;
18071 exp1 = RECUR (TREE_OPERAND (t, 1));
18072 --c_inhibit_evaluation_warnings;
18073 exp2 = RECUR (TREE_OPERAND (t, 2));
18074 }
18075 else
18076 {
18077 exp1 = RECUR (TREE_OPERAND (t, 1));
18078 ++c_inhibit_evaluation_warnings;
18079 exp2 = RECUR (TREE_OPERAND (t, 2));
18080 --c_inhibit_evaluation_warnings;
18081 }
18082 cond = folded_cond;
18083 }
18084 else
18085 {
18086 exp1 = RECUR (TREE_OPERAND (t, 1));
18087 exp2 = RECUR (TREE_OPERAND (t, 2));
18088 }
18089
18090 warning_sentinel s(warn_duplicated_branches);
18091 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
18092 cond, exp1, exp2, complain));
18093 }
18094
18095 case PSEUDO_DTOR_EXPR:
18096 {
18097 tree op0 = RECUR (TREE_OPERAND (t, 0));
18098 tree op1 = RECUR (TREE_OPERAND (t, 1));
18099 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
18100 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
18101 input_location));
18102 }
18103
18104 case TREE_LIST:
18105 {
18106 tree purpose, value, chain;
18107
18108 if (t == void_list_node)
18109 RETURN (t);
18110
18111 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
18112 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
18113 {
18114 /* We have pack expansions, so expand those and
18115 create a new list out of it. */
18116 tree purposevec = NULL_TREE;
18117 tree valuevec = NULL_TREE;
18118 tree chain;
18119 int i, len = -1;
18120
18121 /* Expand the argument expressions. */
18122 if (TREE_PURPOSE (t))
18123 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
18124 complain, in_decl);
18125 if (TREE_VALUE (t))
18126 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
18127 complain, in_decl);
18128
18129 /* Build the rest of the list. */
18130 chain = TREE_CHAIN (t);
18131 if (chain && chain != void_type_node)
18132 chain = RECUR (chain);
18133
18134 /* Determine the number of arguments. */
18135 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
18136 {
18137 len = TREE_VEC_LENGTH (purposevec);
18138 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18139 }
18140 else if (TREE_CODE (valuevec) == TREE_VEC)
18141 len = TREE_VEC_LENGTH (valuevec);
18142 else
18143 {
18144 /* Since we only performed a partial substitution into
18145 the argument pack, we only RETURN (a single list
18146 node. */
18147 if (purposevec == TREE_PURPOSE (t)
18148 && valuevec == TREE_VALUE (t)
18149 && chain == TREE_CHAIN (t))
18150 RETURN (t);
18151
18152 RETURN (tree_cons (purposevec, valuevec, chain));
18153 }
18154
18155 /* Convert the argument vectors into a TREE_LIST */
18156 i = len;
18157 while (i > 0)
18158 {
18159 /* Grab the Ith values. */
18160 i--;
18161 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
18162 : NULL_TREE;
18163 value
18164 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
18165 : NULL_TREE;
18166
18167 /* Build the list (backwards). */
18168 chain = tree_cons (purpose, value, chain);
18169 }
18170
18171 RETURN (chain);
18172 }
18173
18174 purpose = TREE_PURPOSE (t);
18175 if (purpose)
18176 purpose = RECUR (purpose);
18177 value = TREE_VALUE (t);
18178 if (value)
18179 value = RECUR (value);
18180 chain = TREE_CHAIN (t);
18181 if (chain && chain != void_type_node)
18182 chain = RECUR (chain);
18183 if (purpose == TREE_PURPOSE (t)
18184 && value == TREE_VALUE (t)
18185 && chain == TREE_CHAIN (t))
18186 RETURN (t);
18187 RETURN (tree_cons (purpose, value, chain));
18188 }
18189
18190 case COMPONENT_REF:
18191 {
18192 tree object;
18193 tree object_type;
18194 tree member;
18195 tree r;
18196
18197 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18198 args, complain, in_decl);
18199 /* Remember that there was a reference to this entity. */
18200 if (DECL_P (object)
18201 && !mark_used (object, complain) && !(complain & tf_error))
18202 RETURN (error_mark_node);
18203 object_type = TREE_TYPE (object);
18204
18205 member = TREE_OPERAND (t, 1);
18206 if (BASELINK_P (member))
18207 member = tsubst_baselink (member,
18208 non_reference (TREE_TYPE (object)),
18209 args, complain, in_decl);
18210 else
18211 member = tsubst_copy (member, args, complain, in_decl);
18212 if (member == error_mark_node)
18213 RETURN (error_mark_node);
18214
18215 if (TREE_CODE (member) == FIELD_DECL)
18216 {
18217 r = finish_non_static_data_member (member, object, NULL_TREE);
18218 if (TREE_CODE (r) == COMPONENT_REF)
18219 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18220 RETURN (r);
18221 }
18222 else if (type_dependent_expression_p (object))
18223 /* We can't do much here. */;
18224 else if (!CLASS_TYPE_P (object_type))
18225 {
18226 if (scalarish_type_p (object_type))
18227 {
18228 tree s = NULL_TREE;
18229 tree dtor = member;
18230
18231 if (TREE_CODE (dtor) == SCOPE_REF)
18232 {
18233 s = TREE_OPERAND (dtor, 0);
18234 dtor = TREE_OPERAND (dtor, 1);
18235 }
18236 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18237 {
18238 dtor = TREE_OPERAND (dtor, 0);
18239 if (TYPE_P (dtor))
18240 RETURN (finish_pseudo_destructor_expr
18241 (object, s, dtor, input_location));
18242 }
18243 }
18244 }
18245 else if (TREE_CODE (member) == SCOPE_REF
18246 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18247 {
18248 /* Lookup the template functions now that we know what the
18249 scope is. */
18250 tree scope = TREE_OPERAND (member, 0);
18251 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18252 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18253 member = lookup_qualified_name (scope, tmpl,
18254 /*is_type_p=*/false,
18255 /*complain=*/false);
18256 if (BASELINK_P (member))
18257 {
18258 BASELINK_FUNCTIONS (member)
18259 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18260 args);
18261 member = (adjust_result_of_qualified_name_lookup
18262 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18263 object_type));
18264 }
18265 else
18266 {
18267 qualified_name_lookup_error (scope, tmpl, member,
18268 input_location);
18269 RETURN (error_mark_node);
18270 }
18271 }
18272 else if (TREE_CODE (member) == SCOPE_REF
18273 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18274 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18275 {
18276 if (complain & tf_error)
18277 {
18278 if (TYPE_P (TREE_OPERAND (member, 0)))
18279 error ("%qT is not a class or namespace",
18280 TREE_OPERAND (member, 0));
18281 else
18282 error ("%qD is not a class or namespace",
18283 TREE_OPERAND (member, 0));
18284 }
18285 RETURN (error_mark_node);
18286 }
18287
18288 r = finish_class_member_access_expr (object, member,
18289 /*template_p=*/false,
18290 complain);
18291 if (TREE_CODE (r) == COMPONENT_REF)
18292 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18293 RETURN (r);
18294 }
18295
18296 case THROW_EXPR:
18297 RETURN (build_throw
18298 (RECUR (TREE_OPERAND (t, 0))));
18299
18300 case CONSTRUCTOR:
18301 {
18302 vec<constructor_elt, va_gc> *n;
18303 constructor_elt *ce;
18304 unsigned HOST_WIDE_INT idx;
18305 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18306 bool process_index_p;
18307 int newlen;
18308 bool need_copy_p = false;
18309 tree r;
18310
18311 if (type == error_mark_node)
18312 RETURN (error_mark_node);
18313
18314 /* We do not want to process the index of aggregate
18315 initializers as they are identifier nodes which will be
18316 looked up by digest_init. */
18317 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18318
18319 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18320 newlen = vec_safe_length (n);
18321 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18322 {
18323 if (ce->index && process_index_p
18324 /* An identifier index is looked up in the type
18325 being initialized, not the current scope. */
18326 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18327 ce->index = RECUR (ce->index);
18328
18329 if (PACK_EXPANSION_P (ce->value))
18330 {
18331 /* Substitute into the pack expansion. */
18332 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18333 in_decl);
18334
18335 if (ce->value == error_mark_node
18336 || PACK_EXPANSION_P (ce->value))
18337 ;
18338 else if (TREE_VEC_LENGTH (ce->value) == 1)
18339 /* Just move the argument into place. */
18340 ce->value = TREE_VEC_ELT (ce->value, 0);
18341 else
18342 {
18343 /* Update the length of the final CONSTRUCTOR
18344 arguments vector, and note that we will need to
18345 copy.*/
18346 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18347 need_copy_p = true;
18348 }
18349 }
18350 else
18351 ce->value = RECUR (ce->value);
18352 }
18353
18354 if (need_copy_p)
18355 {
18356 vec<constructor_elt, va_gc> *old_n = n;
18357
18358 vec_alloc (n, newlen);
18359 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18360 {
18361 if (TREE_CODE (ce->value) == TREE_VEC)
18362 {
18363 int i, len = TREE_VEC_LENGTH (ce->value);
18364 for (i = 0; i < len; ++i)
18365 CONSTRUCTOR_APPEND_ELT (n, 0,
18366 TREE_VEC_ELT (ce->value, i));
18367 }
18368 else
18369 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18370 }
18371 }
18372
18373 r = build_constructor (init_list_type_node, n);
18374 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18375
18376 if (TREE_HAS_CONSTRUCTOR (t))
18377 {
18378 fcl_t cl = fcl_functional;
18379 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18380 cl = fcl_c99;
18381 RETURN (finish_compound_literal (type, r, complain, cl));
18382 }
18383
18384 TREE_TYPE (r) = type;
18385 RETURN (r);
18386 }
18387
18388 case TYPEID_EXPR:
18389 {
18390 tree operand_0 = TREE_OPERAND (t, 0);
18391 if (TYPE_P (operand_0))
18392 {
18393 operand_0 = tsubst (operand_0, args, complain, in_decl);
18394 RETURN (get_typeid (operand_0, complain));
18395 }
18396 else
18397 {
18398 operand_0 = RECUR (operand_0);
18399 RETURN (build_typeid (operand_0, complain));
18400 }
18401 }
18402
18403 case VAR_DECL:
18404 if (!args)
18405 RETURN (t);
18406 /* Fall through */
18407
18408 case PARM_DECL:
18409 {
18410 tree r = tsubst_copy (t, args, complain, in_decl);
18411 /* ??? We're doing a subset of finish_id_expression here. */
18412 if (VAR_P (r)
18413 && !processing_template_decl
18414 && !cp_unevaluated_operand
18415 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18416 && CP_DECL_THREAD_LOCAL_P (r))
18417 {
18418 if (tree wrap = get_tls_wrapper_fn (r))
18419 /* Replace an evaluated use of the thread_local variable with
18420 a call to its wrapper. */
18421 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18422 }
18423 else if (outer_automatic_var_p (r))
18424 r = process_outer_var_ref (r, complain);
18425
18426 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18427 /* If the original type was a reference, we'll be wrapped in
18428 the appropriate INDIRECT_REF. */
18429 r = convert_from_reference (r);
18430 RETURN (r);
18431 }
18432
18433 case VA_ARG_EXPR:
18434 {
18435 tree op0 = RECUR (TREE_OPERAND (t, 0));
18436 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18437 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18438 }
18439
18440 case OFFSETOF_EXPR:
18441 {
18442 tree object_ptr
18443 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18444 in_decl, /*function_p=*/false,
18445 /*integral_constant_expression_p=*/false);
18446 RETURN (finish_offsetof (object_ptr,
18447 RECUR (TREE_OPERAND (t, 0)),
18448 EXPR_LOCATION (t)));
18449 }
18450
18451 case ADDRESSOF_EXPR:
18452 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18453 RECUR (TREE_OPERAND (t, 0)), complain));
18454
18455 case TRAIT_EXPR:
18456 {
18457 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18458 complain, in_decl);
18459
18460 tree type2 = TRAIT_EXPR_TYPE2 (t);
18461 if (type2 && TREE_CODE (type2) == TREE_LIST)
18462 type2 = RECUR (type2);
18463 else if (type2)
18464 type2 = tsubst (type2, args, complain, in_decl);
18465
18466 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18467 }
18468
18469 case STMT_EXPR:
18470 {
18471 tree old_stmt_expr = cur_stmt_expr;
18472 tree stmt_expr = begin_stmt_expr ();
18473
18474 cur_stmt_expr = stmt_expr;
18475 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18476 integral_constant_expression_p);
18477 stmt_expr = finish_stmt_expr (stmt_expr, false);
18478 cur_stmt_expr = old_stmt_expr;
18479
18480 /* If the resulting list of expression statement is empty,
18481 fold it further into void_node. */
18482 if (empty_expr_stmt_p (stmt_expr))
18483 stmt_expr = void_node;
18484
18485 RETURN (stmt_expr);
18486 }
18487
18488 case LAMBDA_EXPR:
18489 {
18490 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18491
18492 RETURN (build_lambda_object (r));
18493 }
18494
18495 case TARGET_EXPR:
18496 /* We can get here for a constant initializer of non-dependent type.
18497 FIXME stop folding in cp_parser_initializer_clause. */
18498 {
18499 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18500 complain);
18501 RETURN (r);
18502 }
18503
18504 case TRANSACTION_EXPR:
18505 RETURN (tsubst_expr(t, args, complain, in_decl,
18506 integral_constant_expression_p));
18507
18508 case PAREN_EXPR:
18509 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18510
18511 case VEC_PERM_EXPR:
18512 {
18513 tree op0 = RECUR (TREE_OPERAND (t, 0));
18514 tree op1 = RECUR (TREE_OPERAND (t, 1));
18515 tree op2 = RECUR (TREE_OPERAND (t, 2));
18516 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18517 complain));
18518 }
18519
18520 case REQUIRES_EXPR:
18521 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18522
18523 case NON_LVALUE_EXPR:
18524 case VIEW_CONVERT_EXPR:
18525 /* We should only see these for location wrapper nodes, or within
18526 instantiate_non_dependent_expr (when args is NULL_TREE). */
18527 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
18528 if (location_wrapper_p (t))
18529 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
18530 EXPR_LOCATION (t)));
18531 /* fallthrough. */
18532
18533 default:
18534 /* Handle Objective-C++ constructs, if appropriate. */
18535 {
18536 tree subst
18537 = objcp_tsubst_copy_and_build (t, args, complain,
18538 in_decl, /*function_p=*/false);
18539 if (subst)
18540 RETURN (subst);
18541 }
18542 RETURN (tsubst_copy (t, args, complain, in_decl));
18543 }
18544
18545 #undef RECUR
18546 #undef RETURN
18547 out:
18548 input_location = loc;
18549 return retval;
18550 }
18551
18552 /* Verify that the instantiated ARGS are valid. For type arguments,
18553 make sure that the type's linkage is ok. For non-type arguments,
18554 make sure they are constants if they are integral or enumerations.
18555 Emit an error under control of COMPLAIN, and return TRUE on error. */
18556
18557 static bool
18558 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18559 {
18560 if (dependent_template_arg_p (t))
18561 return false;
18562 if (ARGUMENT_PACK_P (t))
18563 {
18564 tree vec = ARGUMENT_PACK_ARGS (t);
18565 int len = TREE_VEC_LENGTH (vec);
18566 bool result = false;
18567 int i;
18568
18569 for (i = 0; i < len; ++i)
18570 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18571 result = true;
18572 return result;
18573 }
18574 else if (TYPE_P (t))
18575 {
18576 /* [basic.link]: A name with no linkage (notably, the name
18577 of a class or enumeration declared in a local scope)
18578 shall not be used to declare an entity with linkage.
18579 This implies that names with no linkage cannot be used as
18580 template arguments
18581
18582 DR 757 relaxes this restriction for C++0x. */
18583 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18584 : no_linkage_check (t, /*relaxed_p=*/false));
18585
18586 if (nt)
18587 {
18588 /* DR 488 makes use of a type with no linkage cause
18589 type deduction to fail. */
18590 if (complain & tf_error)
18591 {
18592 if (TYPE_UNNAMED_P (nt))
18593 error ("%qT is/uses unnamed type", t);
18594 else
18595 error ("template argument for %qD uses local type %qT",
18596 tmpl, t);
18597 }
18598 return true;
18599 }
18600 /* In order to avoid all sorts of complications, we do not
18601 allow variably-modified types as template arguments. */
18602 else if (variably_modified_type_p (t, NULL_TREE))
18603 {
18604 if (complain & tf_error)
18605 error ("%qT is a variably modified type", t);
18606 return true;
18607 }
18608 }
18609 /* Class template and alias template arguments should be OK. */
18610 else if (DECL_TYPE_TEMPLATE_P (t))
18611 ;
18612 /* A non-type argument of integral or enumerated type must be a
18613 constant. */
18614 else if (TREE_TYPE (t)
18615 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18616 && !REFERENCE_REF_P (t)
18617 && !TREE_CONSTANT (t))
18618 {
18619 if (complain & tf_error)
18620 error ("integral expression %qE is not constant", t);
18621 return true;
18622 }
18623 return false;
18624 }
18625
18626 static bool
18627 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18628 {
18629 int ix, len = DECL_NTPARMS (tmpl);
18630 bool result = false;
18631
18632 for (ix = 0; ix != len; ix++)
18633 {
18634 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18635 result = true;
18636 }
18637 if (result && (complain & tf_error))
18638 error (" trying to instantiate %qD", tmpl);
18639 return result;
18640 }
18641
18642 /* We're out of SFINAE context now, so generate diagnostics for the access
18643 errors we saw earlier when instantiating D from TMPL and ARGS. */
18644
18645 static void
18646 recheck_decl_substitution (tree d, tree tmpl, tree args)
18647 {
18648 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18649 tree type = TREE_TYPE (pattern);
18650 location_t loc = input_location;
18651
18652 push_access_scope (d);
18653 push_deferring_access_checks (dk_no_deferred);
18654 input_location = DECL_SOURCE_LOCATION (pattern);
18655 tsubst (type, args, tf_warning_or_error, d);
18656 input_location = loc;
18657 pop_deferring_access_checks ();
18658 pop_access_scope (d);
18659 }
18660
18661 /* Instantiate the indicated variable, function, or alias template TMPL with
18662 the template arguments in TARG_PTR. */
18663
18664 static tree
18665 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18666 {
18667 tree targ_ptr = orig_args;
18668 tree fndecl;
18669 tree gen_tmpl;
18670 tree spec;
18671 bool access_ok = true;
18672
18673 if (tmpl == error_mark_node)
18674 return error_mark_node;
18675
18676 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18677
18678 /* If this function is a clone, handle it specially. */
18679 if (DECL_CLONED_FUNCTION_P (tmpl))
18680 {
18681 tree spec;
18682 tree clone;
18683
18684 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18685 DECL_CLONED_FUNCTION. */
18686 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18687 targ_ptr, complain);
18688 if (spec == error_mark_node)
18689 return error_mark_node;
18690
18691 /* Look for the clone. */
18692 FOR_EACH_CLONE (clone, spec)
18693 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18694 return clone;
18695 /* We should always have found the clone by now. */
18696 gcc_unreachable ();
18697 return NULL_TREE;
18698 }
18699
18700 if (targ_ptr == error_mark_node)
18701 return error_mark_node;
18702
18703 /* Check to see if we already have this specialization. */
18704 gen_tmpl = most_general_template (tmpl);
18705 if (TMPL_ARGS_DEPTH (targ_ptr)
18706 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18707 /* targ_ptr only has the innermost template args, so add the outer ones
18708 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18709 the case of a non-dependent call within a template definition). */
18710 targ_ptr = (add_outermost_template_args
18711 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18712 targ_ptr));
18713
18714 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18715 but it doesn't seem to be on the hot path. */
18716 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18717
18718 gcc_assert (tmpl == gen_tmpl
18719 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18720 == spec)
18721 || fndecl == NULL_TREE);
18722
18723 if (spec != NULL_TREE)
18724 {
18725 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18726 {
18727 if (complain & tf_error)
18728 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18729 return error_mark_node;
18730 }
18731 return spec;
18732 }
18733
18734 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18735 complain))
18736 return error_mark_node;
18737
18738 /* We are building a FUNCTION_DECL, during which the access of its
18739 parameters and return types have to be checked. However this
18740 FUNCTION_DECL which is the desired context for access checking
18741 is not built yet. We solve this chicken-and-egg problem by
18742 deferring all checks until we have the FUNCTION_DECL. */
18743 push_deferring_access_checks (dk_deferred);
18744
18745 /* Instantiation of the function happens in the context of the function
18746 template, not the context of the overload resolution we're doing. */
18747 push_to_top_level ();
18748 /* If there are dependent arguments, e.g. because we're doing partial
18749 ordering, make sure processing_template_decl stays set. */
18750 if (uses_template_parms (targ_ptr))
18751 ++processing_template_decl;
18752 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18753 {
18754 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18755 complain, gen_tmpl, true);
18756 push_nested_class (ctx);
18757 }
18758
18759 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18760
18761 fndecl = NULL_TREE;
18762 if (VAR_P (pattern))
18763 {
18764 /* We need to determine if we're using a partial or explicit
18765 specialization now, because the type of the variable could be
18766 different. */
18767 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18768 tree elt = most_specialized_partial_spec (tid, complain);
18769 if (elt == error_mark_node)
18770 pattern = error_mark_node;
18771 else if (elt)
18772 {
18773 tree partial_tmpl = TREE_VALUE (elt);
18774 tree partial_args = TREE_PURPOSE (elt);
18775 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18776 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18777 }
18778 }
18779
18780 /* Substitute template parameters to obtain the specialization. */
18781 if (fndecl == NULL_TREE)
18782 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18783 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18784 pop_nested_class ();
18785 pop_from_top_level ();
18786
18787 if (fndecl == error_mark_node)
18788 {
18789 pop_deferring_access_checks ();
18790 return error_mark_node;
18791 }
18792
18793 /* The DECL_TI_TEMPLATE should always be the immediate parent
18794 template, not the most general template. */
18795 DECL_TI_TEMPLATE (fndecl) = tmpl;
18796 DECL_TI_ARGS (fndecl) = targ_ptr;
18797
18798 /* Now we know the specialization, compute access previously
18799 deferred. Do no access control for inheriting constructors,
18800 as we already checked access for the inherited constructor. */
18801 if (!(flag_new_inheriting_ctors
18802 && DECL_INHERITED_CTOR (fndecl)))
18803 {
18804 push_access_scope (fndecl);
18805 if (!perform_deferred_access_checks (complain))
18806 access_ok = false;
18807 pop_access_scope (fndecl);
18808 }
18809 pop_deferring_access_checks ();
18810
18811 /* If we've just instantiated the main entry point for a function,
18812 instantiate all the alternate entry points as well. We do this
18813 by cloning the instantiation of the main entry point, not by
18814 instantiating the template clones. */
18815 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18816 clone_function_decl (fndecl, /*update_methods=*/false);
18817
18818 if (!access_ok)
18819 {
18820 if (!(complain & tf_error))
18821 {
18822 /* Remember to reinstantiate when we're out of SFINAE so the user
18823 can see the errors. */
18824 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18825 }
18826 return error_mark_node;
18827 }
18828 return fndecl;
18829 }
18830
18831 /* Wrapper for instantiate_template_1. */
18832
18833 tree
18834 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18835 {
18836 tree ret;
18837 timevar_push (TV_TEMPLATE_INST);
18838 ret = instantiate_template_1 (tmpl, orig_args, complain);
18839 timevar_pop (TV_TEMPLATE_INST);
18840 return ret;
18841 }
18842
18843 /* Instantiate the alias template TMPL with ARGS. Also push a template
18844 instantiation level, which instantiate_template doesn't do because
18845 functions and variables have sufficient context established by the
18846 callers. */
18847
18848 static tree
18849 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18850 {
18851 struct pending_template *old_last_pend = last_pending_template;
18852 struct tinst_level *old_error_tinst = last_error_tinst_level;
18853 if (tmpl == error_mark_node || args == error_mark_node)
18854 return error_mark_node;
18855 tree tinst = build_tree_list (tmpl, args);
18856 if (!push_tinst_level (tinst))
18857 {
18858 ggc_free (tinst);
18859 return error_mark_node;
18860 }
18861
18862 args =
18863 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18864 args, tmpl, complain,
18865 /*require_all_args=*/true,
18866 /*use_default_args=*/true);
18867
18868 tree r = instantiate_template (tmpl, args, complain);
18869 pop_tinst_level ();
18870 /* We can't free this if a pending_template entry or last_error_tinst_level
18871 is pointing at it. */
18872 if (last_pending_template == old_last_pend
18873 && last_error_tinst_level == old_error_tinst)
18874 ggc_free (tinst);
18875
18876 return r;
18877 }
18878
18879 /* PARM is a template parameter pack for FN. Returns true iff
18880 PARM is used in a deducible way in the argument list of FN. */
18881
18882 static bool
18883 pack_deducible_p (tree parm, tree fn)
18884 {
18885 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18886 for (; t; t = TREE_CHAIN (t))
18887 {
18888 tree type = TREE_VALUE (t);
18889 tree packs;
18890 if (!PACK_EXPANSION_P (type))
18891 continue;
18892 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18893 packs; packs = TREE_CHAIN (packs))
18894 if (template_args_equal (TREE_VALUE (packs), parm))
18895 {
18896 /* The template parameter pack is used in a function parameter
18897 pack. If this is the end of the parameter list, the
18898 template parameter pack is deducible. */
18899 if (TREE_CHAIN (t) == void_list_node)
18900 return true;
18901 else
18902 /* Otherwise, not. Well, it could be deduced from
18903 a non-pack parameter, but doing so would end up with
18904 a deduction mismatch, so don't bother. */
18905 return false;
18906 }
18907 }
18908 /* The template parameter pack isn't used in any function parameter
18909 packs, but it might be used deeper, e.g. tuple<Args...>. */
18910 return true;
18911 }
18912
18913 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18914 NARGS elements of the arguments that are being used when calling
18915 it. TARGS is a vector into which the deduced template arguments
18916 are placed.
18917
18918 Returns either a FUNCTION_DECL for the matching specialization of FN or
18919 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18920 true, diagnostics will be printed to explain why it failed.
18921
18922 If FN is a conversion operator, or we are trying to produce a specific
18923 specialization, RETURN_TYPE is the return type desired.
18924
18925 The EXPLICIT_TARGS are explicit template arguments provided via a
18926 template-id.
18927
18928 The parameter STRICT is one of:
18929
18930 DEDUCE_CALL:
18931 We are deducing arguments for a function call, as in
18932 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18933 deducing arguments for a call to the result of a conversion
18934 function template, as in [over.call.object].
18935
18936 DEDUCE_CONV:
18937 We are deducing arguments for a conversion function, as in
18938 [temp.deduct.conv].
18939
18940 DEDUCE_EXACT:
18941 We are deducing arguments when doing an explicit instantiation
18942 as in [temp.explicit], when determining an explicit specialization
18943 as in [temp.expl.spec], or when taking the address of a function
18944 template, as in [temp.deduct.funcaddr]. */
18945
18946 tree
18947 fn_type_unification (tree fn,
18948 tree explicit_targs,
18949 tree targs,
18950 const tree *args,
18951 unsigned int nargs,
18952 tree return_type,
18953 unification_kind_t strict,
18954 int flags,
18955 bool explain_p,
18956 bool decltype_p)
18957 {
18958 tree parms;
18959 tree fntype;
18960 tree decl = NULL_TREE;
18961 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18962 bool ok;
18963 static int deduction_depth;
18964 struct pending_template *old_last_pend = last_pending_template;
18965 struct tinst_level *old_error_tinst = last_error_tinst_level;
18966
18967 tree orig_fn = fn;
18968 if (flag_new_inheriting_ctors)
18969 fn = strip_inheriting_ctors (fn);
18970
18971 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18972 tree tinst;
18973 tree r = error_mark_node;
18974
18975 tree full_targs = targs;
18976 if (TMPL_ARGS_DEPTH (targs)
18977 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18978 full_targs = (add_outermost_template_args
18979 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18980 targs));
18981
18982 if (decltype_p)
18983 complain |= tf_decltype;
18984
18985 /* In C++0x, it's possible to have a function template whose type depends
18986 on itself recursively. This is most obvious with decltype, but can also
18987 occur with enumeration scope (c++/48969). So we need to catch infinite
18988 recursion and reject the substitution at deduction time; this function
18989 will return error_mark_node for any repeated substitution.
18990
18991 This also catches excessive recursion such as when f<N> depends on
18992 f<N-1> across all integers, and returns error_mark_node for all the
18993 substitutions back up to the initial one.
18994
18995 This is, of course, not reentrant. */
18996 if (excessive_deduction_depth)
18997 return error_mark_node;
18998 tinst = build_tree_list (fn, NULL_TREE);
18999 ++deduction_depth;
19000
19001 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
19002
19003 fntype = TREE_TYPE (fn);
19004 if (explicit_targs)
19005 {
19006 /* [temp.deduct]
19007
19008 The specified template arguments must match the template
19009 parameters in kind (i.e., type, nontype, template), and there
19010 must not be more arguments than there are parameters;
19011 otherwise type deduction fails.
19012
19013 Nontype arguments must match the types of the corresponding
19014 nontype template parameters, or must be convertible to the
19015 types of the corresponding nontype parameters as specified in
19016 _temp.arg.nontype_, otherwise type deduction fails.
19017
19018 All references in the function type of the function template
19019 to the corresponding template parameters are replaced by the
19020 specified template argument values. If a substitution in a
19021 template parameter or in the function type of the function
19022 template results in an invalid type, type deduction fails. */
19023 int i, len = TREE_VEC_LENGTH (tparms);
19024 location_t loc = input_location;
19025 bool incomplete = false;
19026
19027 if (explicit_targs == error_mark_node)
19028 goto fail;
19029
19030 if (TMPL_ARGS_DEPTH (explicit_targs)
19031 < TMPL_ARGS_DEPTH (full_targs))
19032 explicit_targs = add_outermost_template_args (full_targs,
19033 explicit_targs);
19034
19035 /* Adjust any explicit template arguments before entering the
19036 substitution context. */
19037 explicit_targs
19038 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
19039 complain,
19040 /*require_all_args=*/false,
19041 /*use_default_args=*/false));
19042 if (explicit_targs == error_mark_node)
19043 goto fail;
19044
19045 /* Substitute the explicit args into the function type. This is
19046 necessary so that, for instance, explicitly declared function
19047 arguments can match null pointed constants. If we were given
19048 an incomplete set of explicit args, we must not do semantic
19049 processing during substitution as we could create partial
19050 instantiations. */
19051 for (i = 0; i < len; i++)
19052 {
19053 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
19054 bool parameter_pack = false;
19055 tree targ = TREE_VEC_ELT (explicit_targs, i);
19056
19057 /* Dig out the actual parm. */
19058 if (TREE_CODE (parm) == TYPE_DECL
19059 || TREE_CODE (parm) == TEMPLATE_DECL)
19060 {
19061 parm = TREE_TYPE (parm);
19062 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
19063 }
19064 else if (TREE_CODE (parm) == PARM_DECL)
19065 {
19066 parm = DECL_INITIAL (parm);
19067 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
19068 }
19069
19070 if (!parameter_pack && targ == NULL_TREE)
19071 /* No explicit argument for this template parameter. */
19072 incomplete = true;
19073
19074 if (parameter_pack && pack_deducible_p (parm, fn))
19075 {
19076 /* Mark the argument pack as "incomplete". We could
19077 still deduce more arguments during unification.
19078 We remove this mark in type_unification_real. */
19079 if (targ)
19080 {
19081 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
19082 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
19083 = ARGUMENT_PACK_ARGS (targ);
19084 }
19085
19086 /* We have some incomplete argument packs. */
19087 incomplete = true;
19088 }
19089 }
19090
19091 TREE_VALUE (tinst) = explicit_targs;
19092 if (!push_tinst_level (tinst))
19093 {
19094 excessive_deduction_depth = true;
19095 goto fail;
19096 }
19097 processing_template_decl += incomplete;
19098 input_location = DECL_SOURCE_LOCATION (fn);
19099 /* Ignore any access checks; we'll see them again in
19100 instantiate_template and they might have the wrong
19101 access path at this point. */
19102 push_deferring_access_checks (dk_deferred);
19103 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
19104 complain | tf_partial | tf_fndecl_type, NULL_TREE);
19105 pop_deferring_access_checks ();
19106 input_location = loc;
19107 processing_template_decl -= incomplete;
19108 pop_tinst_level ();
19109
19110 if (fntype == error_mark_node)
19111 goto fail;
19112
19113 /* Place the explicitly specified arguments in TARGS. */
19114 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
19115 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
19116 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
19117 }
19118
19119 /* Never do unification on the 'this' parameter. */
19120 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
19121
19122 if (return_type && strict == DEDUCE_CALL)
19123 {
19124 /* We're deducing for a call to the result of a template conversion
19125 function. The parms we really want are in return_type. */
19126 if (POINTER_TYPE_P (return_type))
19127 return_type = TREE_TYPE (return_type);
19128 parms = TYPE_ARG_TYPES (return_type);
19129 }
19130 else if (return_type)
19131 {
19132 tree *new_args;
19133
19134 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
19135 new_args = XALLOCAVEC (tree, nargs + 1);
19136 new_args[0] = return_type;
19137 memcpy (new_args + 1, args, nargs * sizeof (tree));
19138 args = new_args;
19139 ++nargs;
19140 }
19141
19142 /* We allow incomplete unification without an error message here
19143 because the standard doesn't seem to explicitly prohibit it. Our
19144 callers must be ready to deal with unification failures in any
19145 event. */
19146
19147 TREE_VALUE (tinst) = targs;
19148 /* If we aren't explaining yet, push tinst context so we can see where
19149 any errors (e.g. from class instantiations triggered by instantiation
19150 of default template arguments) come from. If we are explaining, this
19151 context is redundant. */
19152 if (!explain_p && !push_tinst_level (tinst))
19153 {
19154 excessive_deduction_depth = true;
19155 goto fail;
19156 }
19157
19158 /* type_unification_real will pass back any access checks from default
19159 template argument substitution. */
19160 vec<deferred_access_check, va_gc> *checks;
19161 checks = NULL;
19162
19163 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19164 full_targs, parms, args, nargs, /*subr=*/0,
19165 strict, flags, &checks, explain_p);
19166 if (!explain_p)
19167 pop_tinst_level ();
19168 if (!ok)
19169 goto fail;
19170
19171 /* Now that we have bindings for all of the template arguments,
19172 ensure that the arguments deduced for the template template
19173 parameters have compatible template parameter lists. We cannot
19174 check this property before we have deduced all template
19175 arguments, because the template parameter types of a template
19176 template parameter might depend on prior template parameters
19177 deduced after the template template parameter. The following
19178 ill-formed example illustrates this issue:
19179
19180 template<typename T, template<T> class C> void f(C<5>, T);
19181
19182 template<int N> struct X {};
19183
19184 void g() {
19185 f(X<5>(), 5l); // error: template argument deduction fails
19186 }
19187
19188 The template parameter list of 'C' depends on the template type
19189 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19190 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19191 time that we deduce 'C'. */
19192 if (!template_template_parm_bindings_ok_p
19193 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19194 {
19195 unify_inconsistent_template_template_parameters (explain_p);
19196 goto fail;
19197 }
19198
19199 /* All is well so far. Now, check:
19200
19201 [temp.deduct]
19202
19203 When all template arguments have been deduced, all uses of
19204 template parameters in nondeduced contexts are replaced with
19205 the corresponding deduced argument values. If the
19206 substitution results in an invalid type, as described above,
19207 type deduction fails. */
19208 TREE_VALUE (tinst) = targs;
19209 if (!push_tinst_level (tinst))
19210 {
19211 excessive_deduction_depth = true;
19212 goto fail;
19213 }
19214
19215 /* Also collect access checks from the instantiation. */
19216 reopen_deferring_access_checks (checks);
19217
19218 decl = instantiate_template (fn, targs, complain);
19219
19220 checks = get_deferred_access_checks ();
19221 pop_deferring_access_checks ();
19222
19223 pop_tinst_level ();
19224
19225 if (decl == error_mark_node)
19226 goto fail;
19227
19228 /* Now perform any access checks encountered during substitution. */
19229 push_access_scope (decl);
19230 ok = perform_access_checks (checks, complain);
19231 pop_access_scope (decl);
19232 if (!ok)
19233 goto fail;
19234
19235 /* If we're looking for an exact match, check that what we got
19236 is indeed an exact match. It might not be if some template
19237 parameters are used in non-deduced contexts. But don't check
19238 for an exact match if we have dependent template arguments;
19239 in that case we're doing partial ordering, and we already know
19240 that we have two candidates that will provide the actual type. */
19241 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19242 {
19243 tree substed = TREE_TYPE (decl);
19244 unsigned int i;
19245
19246 tree sarg
19247 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19248 if (return_type)
19249 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19250 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19251 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19252 {
19253 unify_type_mismatch (explain_p, args[i],
19254 TREE_VALUE (sarg));
19255 goto fail;
19256 }
19257 }
19258
19259 /* After doing deduction with the inherited constructor, actually return an
19260 instantiation of the inheriting constructor. */
19261 if (orig_fn != fn)
19262 decl = instantiate_template (orig_fn, targs, complain);
19263
19264 r = decl;
19265
19266 fail:
19267 --deduction_depth;
19268 if (excessive_deduction_depth)
19269 {
19270 if (deduction_depth == 0)
19271 /* Reset once we're all the way out. */
19272 excessive_deduction_depth = false;
19273 }
19274
19275 /* We can't free this if a pending_template entry or last_error_tinst_level
19276 is pointing at it. */
19277 if (last_pending_template == old_last_pend
19278 && last_error_tinst_level == old_error_tinst)
19279 ggc_free (tinst);
19280
19281 return r;
19282 }
19283
19284 /* Adjust types before performing type deduction, as described in
19285 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19286 sections are symmetric. PARM is the type of a function parameter
19287 or the return type of the conversion function. ARG is the type of
19288 the argument passed to the call, or the type of the value
19289 initialized with the result of the conversion function.
19290 ARG_EXPR is the original argument expression, which may be null. */
19291
19292 static int
19293 maybe_adjust_types_for_deduction (unification_kind_t strict,
19294 tree* parm,
19295 tree* arg,
19296 tree arg_expr)
19297 {
19298 int result = 0;
19299
19300 switch (strict)
19301 {
19302 case DEDUCE_CALL:
19303 break;
19304
19305 case DEDUCE_CONV:
19306 /* Swap PARM and ARG throughout the remainder of this
19307 function; the handling is precisely symmetric since PARM
19308 will initialize ARG rather than vice versa. */
19309 std::swap (parm, arg);
19310 break;
19311
19312 case DEDUCE_EXACT:
19313 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19314 too, but here handle it by stripping the reference from PARM
19315 rather than by adding it to ARG. */
19316 if (TREE_CODE (*parm) == REFERENCE_TYPE
19317 && TYPE_REF_IS_RVALUE (*parm)
19318 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19319 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19320 && TREE_CODE (*arg) == REFERENCE_TYPE
19321 && !TYPE_REF_IS_RVALUE (*arg))
19322 *parm = TREE_TYPE (*parm);
19323 /* Nothing else to do in this case. */
19324 return 0;
19325
19326 default:
19327 gcc_unreachable ();
19328 }
19329
19330 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19331 {
19332 /* [temp.deduct.call]
19333
19334 If P is not a reference type:
19335
19336 --If A is an array type, the pointer type produced by the
19337 array-to-pointer standard conversion (_conv.array_) is
19338 used in place of A for type deduction; otherwise,
19339
19340 --If A is a function type, the pointer type produced by
19341 the function-to-pointer standard conversion
19342 (_conv.func_) is used in place of A for type deduction;
19343 otherwise,
19344
19345 --If A is a cv-qualified type, the top level
19346 cv-qualifiers of A's type are ignored for type
19347 deduction. */
19348 if (TREE_CODE (*arg) == ARRAY_TYPE)
19349 *arg = build_pointer_type (TREE_TYPE (*arg));
19350 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19351 *arg = build_pointer_type (*arg);
19352 else
19353 *arg = TYPE_MAIN_VARIANT (*arg);
19354 }
19355
19356 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19357 reference to a cv-unqualified template parameter that does not represent a
19358 template parameter of a class template (during class template argument
19359 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19360 an lvalue, the type "lvalue reference to A" is used in place of A for type
19361 deduction. */
19362 if (TREE_CODE (*parm) == REFERENCE_TYPE
19363 && TYPE_REF_IS_RVALUE (*parm)
19364 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19365 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19366 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19367 && (arg_expr ? lvalue_p (arg_expr)
19368 /* try_one_overload doesn't provide an arg_expr, but
19369 functions are always lvalues. */
19370 : TREE_CODE (*arg) == FUNCTION_TYPE))
19371 *arg = build_reference_type (*arg);
19372
19373 /* [temp.deduct.call]
19374
19375 If P is a cv-qualified type, the top level cv-qualifiers
19376 of P's type are ignored for type deduction. If P is a
19377 reference type, the type referred to by P is used for
19378 type deduction. */
19379 *parm = TYPE_MAIN_VARIANT (*parm);
19380 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19381 {
19382 *parm = TREE_TYPE (*parm);
19383 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19384 }
19385
19386 /* DR 322. For conversion deduction, remove a reference type on parm
19387 too (which has been swapped into ARG). */
19388 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19389 *arg = TREE_TYPE (*arg);
19390
19391 return result;
19392 }
19393
19394 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19395 template which does contain any deducible template parameters; check if
19396 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19397 unify_one_argument. */
19398
19399 static int
19400 check_non_deducible_conversion (tree parm, tree arg, int strict,
19401 int flags, bool explain_p)
19402 {
19403 tree type;
19404
19405 if (!TYPE_P (arg))
19406 type = TREE_TYPE (arg);
19407 else
19408 type = arg;
19409
19410 if (same_type_p (parm, type))
19411 return unify_success (explain_p);
19412
19413 if (strict == DEDUCE_CONV)
19414 {
19415 if (can_convert_arg (type, parm, NULL_TREE, flags,
19416 explain_p ? tf_warning_or_error : tf_none))
19417 return unify_success (explain_p);
19418 }
19419 else if (strict != DEDUCE_EXACT)
19420 {
19421 if (can_convert_arg (parm, type,
19422 TYPE_P (arg) ? NULL_TREE : arg,
19423 flags, explain_p ? tf_warning_or_error : tf_none))
19424 return unify_success (explain_p);
19425 }
19426
19427 if (strict == DEDUCE_EXACT)
19428 return unify_type_mismatch (explain_p, parm, arg);
19429 else
19430 return unify_arg_conversion (explain_p, parm, type, arg);
19431 }
19432
19433 static bool uses_deducible_template_parms (tree type);
19434
19435 /* Returns true iff the expression EXPR is one from which a template
19436 argument can be deduced. In other words, if it's an undecorated
19437 use of a template non-type parameter. */
19438
19439 static bool
19440 deducible_expression (tree expr)
19441 {
19442 /* Strip implicit conversions. */
19443 while (CONVERT_EXPR_P (expr))
19444 expr = TREE_OPERAND (expr, 0);
19445 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19446 }
19447
19448 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19449 deducible way; that is, if it has a max value of <PARM> - 1. */
19450
19451 static bool
19452 deducible_array_bound (tree domain)
19453 {
19454 if (domain == NULL_TREE)
19455 return false;
19456
19457 tree max = TYPE_MAX_VALUE (domain);
19458 if (TREE_CODE (max) != MINUS_EXPR)
19459 return false;
19460
19461 return deducible_expression (TREE_OPERAND (max, 0));
19462 }
19463
19464 /* Returns true iff the template arguments ARGS use a template parameter
19465 in a deducible way. */
19466
19467 static bool
19468 deducible_template_args (tree args)
19469 {
19470 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19471 {
19472 bool deducible;
19473 tree elt = TREE_VEC_ELT (args, i);
19474 if (ARGUMENT_PACK_P (elt))
19475 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19476 else
19477 {
19478 if (PACK_EXPANSION_P (elt))
19479 elt = PACK_EXPANSION_PATTERN (elt);
19480 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19481 deducible = true;
19482 else if (TYPE_P (elt))
19483 deducible = uses_deducible_template_parms (elt);
19484 else
19485 deducible = deducible_expression (elt);
19486 }
19487 if (deducible)
19488 return true;
19489 }
19490 return false;
19491 }
19492
19493 /* Returns true iff TYPE contains any deducible references to template
19494 parameters, as per 14.8.2.5. */
19495
19496 static bool
19497 uses_deducible_template_parms (tree type)
19498 {
19499 if (PACK_EXPANSION_P (type))
19500 type = PACK_EXPANSION_PATTERN (type);
19501
19502 /* T
19503 cv-list T
19504 TT<T>
19505 TT<i>
19506 TT<> */
19507 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19508 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19509 return true;
19510
19511 /* T*
19512 T&
19513 T&& */
19514 if (POINTER_TYPE_P (type))
19515 return uses_deducible_template_parms (TREE_TYPE (type));
19516
19517 /* T[integer-constant ]
19518 type [i] */
19519 if (TREE_CODE (type) == ARRAY_TYPE)
19520 return (uses_deducible_template_parms (TREE_TYPE (type))
19521 || deducible_array_bound (TYPE_DOMAIN (type)));
19522
19523 /* T type ::*
19524 type T::*
19525 T T::*
19526 T (type ::*)()
19527 type (T::*)()
19528 type (type ::*)(T)
19529 type (T::*)(T)
19530 T (type ::*)(T)
19531 T (T::*)()
19532 T (T::*)(T) */
19533 if (TYPE_PTRMEM_P (type))
19534 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19535 || (uses_deducible_template_parms
19536 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19537
19538 /* template-name <T> (where template-name refers to a class template)
19539 template-name <i> (where template-name refers to a class template) */
19540 if (CLASS_TYPE_P (type)
19541 && CLASSTYPE_TEMPLATE_INFO (type)
19542 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19543 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19544 (CLASSTYPE_TI_ARGS (type)));
19545
19546 /* type (T)
19547 T()
19548 T(T) */
19549 if (TREE_CODE (type) == FUNCTION_TYPE
19550 || TREE_CODE (type) == METHOD_TYPE)
19551 {
19552 if (uses_deducible_template_parms (TREE_TYPE (type)))
19553 return true;
19554 tree parm = TYPE_ARG_TYPES (type);
19555 if (TREE_CODE (type) == METHOD_TYPE)
19556 parm = TREE_CHAIN (parm);
19557 for (; parm; parm = TREE_CHAIN (parm))
19558 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19559 return true;
19560 }
19561
19562 return false;
19563 }
19564
19565 /* Subroutine of type_unification_real and unify_pack_expansion to
19566 handle unification of a single P/A pair. Parameters are as
19567 for those functions. */
19568
19569 static int
19570 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19571 int subr, unification_kind_t strict,
19572 bool explain_p)
19573 {
19574 tree arg_expr = NULL_TREE;
19575 int arg_strict;
19576
19577 if (arg == error_mark_node || parm == error_mark_node)
19578 return unify_invalid (explain_p);
19579 if (arg == unknown_type_node)
19580 /* We can't deduce anything from this, but we might get all the
19581 template args from other function args. */
19582 return unify_success (explain_p);
19583
19584 /* Implicit conversions (Clause 4) will be performed on a function
19585 argument to convert it to the type of the corresponding function
19586 parameter if the parameter type contains no template-parameters that
19587 participate in template argument deduction. */
19588 if (strict != DEDUCE_EXACT
19589 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19590 /* For function parameters with no deducible template parameters,
19591 just return. We'll check non-dependent conversions later. */
19592 return unify_success (explain_p);
19593
19594 switch (strict)
19595 {
19596 case DEDUCE_CALL:
19597 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19598 | UNIFY_ALLOW_MORE_CV_QUAL
19599 | UNIFY_ALLOW_DERIVED);
19600 break;
19601
19602 case DEDUCE_CONV:
19603 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19604 break;
19605
19606 case DEDUCE_EXACT:
19607 arg_strict = UNIFY_ALLOW_NONE;
19608 break;
19609
19610 default:
19611 gcc_unreachable ();
19612 }
19613
19614 /* We only do these transformations if this is the top-level
19615 parameter_type_list in a call or declaration matching; in other
19616 situations (nested function declarators, template argument lists) we
19617 won't be comparing a type to an expression, and we don't do any type
19618 adjustments. */
19619 if (!subr)
19620 {
19621 if (!TYPE_P (arg))
19622 {
19623 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19624 if (type_unknown_p (arg))
19625 {
19626 /* [temp.deduct.type] A template-argument can be
19627 deduced from a pointer to function or pointer
19628 to member function argument if the set of
19629 overloaded functions does not contain function
19630 templates and at most one of a set of
19631 overloaded functions provides a unique
19632 match. */
19633 resolve_overloaded_unification (tparms, targs, parm,
19634 arg, strict,
19635 arg_strict, explain_p);
19636 /* If a unique match was not found, this is a
19637 non-deduced context, so we still succeed. */
19638 return unify_success (explain_p);
19639 }
19640
19641 arg_expr = arg;
19642 arg = unlowered_expr_type (arg);
19643 if (arg == error_mark_node)
19644 return unify_invalid (explain_p);
19645 }
19646
19647 arg_strict |=
19648 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19649 }
19650 else
19651 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19652 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19653 return unify_template_argument_mismatch (explain_p, parm, arg);
19654
19655 /* For deduction from an init-list we need the actual list. */
19656 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19657 arg = arg_expr;
19658 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19659 }
19660
19661 /* for_each_template_parm callback that always returns 0. */
19662
19663 static int
19664 zero_r (tree, void *)
19665 {
19666 return 0;
19667 }
19668
19669 /* for_each_template_parm any_fn callback to handle deduction of a template
19670 type argument from the type of an array bound. */
19671
19672 static int
19673 array_deduction_r (tree t, void *data)
19674 {
19675 tree_pair_p d = (tree_pair_p)data;
19676 tree &tparms = d->purpose;
19677 tree &targs = d->value;
19678
19679 if (TREE_CODE (t) == ARRAY_TYPE)
19680 if (tree dom = TYPE_DOMAIN (t))
19681 if (tree max = TYPE_MAX_VALUE (dom))
19682 {
19683 if (TREE_CODE (max) == MINUS_EXPR)
19684 max = TREE_OPERAND (max, 0);
19685 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19686 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19687 UNIFY_ALLOW_NONE, /*explain*/false);
19688 }
19689
19690 /* Keep walking. */
19691 return 0;
19692 }
19693
19694 /* Try to deduce any not-yet-deduced template type arguments from the type of
19695 an array bound. This is handled separately from unify because 14.8.2.5 says
19696 "The type of a type parameter is only deduced from an array bound if it is
19697 not otherwise deduced." */
19698
19699 static void
19700 try_array_deduction (tree tparms, tree targs, tree parm)
19701 {
19702 tree_pair_s data = { tparms, targs };
19703 hash_set<tree> visited;
19704 for_each_template_parm (parm, zero_r, &data, &visited,
19705 /*nondeduced*/false, array_deduction_r);
19706 }
19707
19708 /* Most parms like fn_type_unification.
19709
19710 If SUBR is 1, we're being called recursively (to unify the
19711 arguments of a function or method parameter of a function
19712 template).
19713
19714 CHECKS is a pointer to a vector of access checks encountered while
19715 substituting default template arguments. */
19716
19717 static int
19718 type_unification_real (tree tparms,
19719 tree full_targs,
19720 tree xparms,
19721 const tree *xargs,
19722 unsigned int xnargs,
19723 int subr,
19724 unification_kind_t strict,
19725 int flags,
19726 vec<deferred_access_check, va_gc> **checks,
19727 bool explain_p)
19728 {
19729 tree parm, arg;
19730 int i;
19731 int ntparms = TREE_VEC_LENGTH (tparms);
19732 int saw_undeduced = 0;
19733 tree parms;
19734 const tree *args;
19735 unsigned int nargs;
19736 unsigned int ia;
19737
19738 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19739 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19740 gcc_assert (ntparms > 0);
19741
19742 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19743
19744 /* Reset the number of non-defaulted template arguments contained
19745 in TARGS. */
19746 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19747
19748 again:
19749 parms = xparms;
19750 args = xargs;
19751 nargs = xnargs;
19752
19753 ia = 0;
19754 while (parms && parms != void_list_node
19755 && ia < nargs)
19756 {
19757 parm = TREE_VALUE (parms);
19758
19759 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19760 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19761 /* For a function parameter pack that occurs at the end of the
19762 parameter-declaration-list, the type A of each remaining
19763 argument of the call is compared with the type P of the
19764 declarator-id of the function parameter pack. */
19765 break;
19766
19767 parms = TREE_CHAIN (parms);
19768
19769 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19770 /* For a function parameter pack that does not occur at the
19771 end of the parameter-declaration-list, the type of the
19772 parameter pack is a non-deduced context. */
19773 continue;
19774
19775 arg = args[ia];
19776 ++ia;
19777
19778 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19779 explain_p))
19780 return 1;
19781 }
19782
19783 if (parms
19784 && parms != void_list_node
19785 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19786 {
19787 /* Unify the remaining arguments with the pack expansion type. */
19788 tree argvec;
19789 tree parmvec = make_tree_vec (1);
19790
19791 /* Allocate a TREE_VEC and copy in all of the arguments */
19792 argvec = make_tree_vec (nargs - ia);
19793 for (i = 0; ia < nargs; ++ia, ++i)
19794 TREE_VEC_ELT (argvec, i) = args[ia];
19795
19796 /* Copy the parameter into parmvec. */
19797 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19798 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19799 /*subr=*/subr, explain_p))
19800 return 1;
19801
19802 /* Advance to the end of the list of parameters. */
19803 parms = TREE_CHAIN (parms);
19804 }
19805
19806 /* Fail if we've reached the end of the parm list, and more args
19807 are present, and the parm list isn't variadic. */
19808 if (ia < nargs && parms == void_list_node)
19809 return unify_too_many_arguments (explain_p, nargs, ia);
19810 /* Fail if parms are left and they don't have default values and
19811 they aren't all deduced as empty packs (c++/57397). This is
19812 consistent with sufficient_parms_p. */
19813 if (parms && parms != void_list_node
19814 && TREE_PURPOSE (parms) == NULL_TREE)
19815 {
19816 unsigned int count = nargs;
19817 tree p = parms;
19818 bool type_pack_p;
19819 do
19820 {
19821 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19822 if (!type_pack_p)
19823 count++;
19824 p = TREE_CHAIN (p);
19825 }
19826 while (p && p != void_list_node);
19827 if (count != nargs)
19828 return unify_too_few_arguments (explain_p, ia, count,
19829 type_pack_p);
19830 }
19831
19832 if (!subr)
19833 {
19834 tsubst_flags_t complain = (explain_p
19835 ? tf_warning_or_error
19836 : tf_none);
19837 bool tried_array_deduction = (cxx_dialect < cxx17);
19838
19839 for (i = 0; i < ntparms; i++)
19840 {
19841 tree targ = TREE_VEC_ELT (targs, i);
19842 tree tparm = TREE_VEC_ELT (tparms, i);
19843
19844 /* Clear the "incomplete" flags on all argument packs now so that
19845 substituting them into later default arguments works. */
19846 if (targ && ARGUMENT_PACK_P (targ))
19847 {
19848 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19849 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19850 }
19851
19852 if (targ || tparm == error_mark_node)
19853 continue;
19854 tparm = TREE_VALUE (tparm);
19855
19856 if (TREE_CODE (tparm) == TYPE_DECL
19857 && !tried_array_deduction)
19858 {
19859 try_array_deduction (tparms, targs, xparms);
19860 tried_array_deduction = true;
19861 if (TREE_VEC_ELT (targs, i))
19862 continue;
19863 }
19864
19865 /* If this is an undeduced nontype parameter that depends on
19866 a type parameter, try another pass; its type may have been
19867 deduced from a later argument than the one from which
19868 this parameter can be deduced. */
19869 if (TREE_CODE (tparm) == PARM_DECL
19870 && uses_template_parms (TREE_TYPE (tparm))
19871 && saw_undeduced < 2)
19872 {
19873 saw_undeduced = 1;
19874 continue;
19875 }
19876
19877 /* Core issue #226 (C++0x) [temp.deduct]:
19878
19879 If a template argument has not been deduced, its
19880 default template argument, if any, is used.
19881
19882 When we are in C++98 mode, TREE_PURPOSE will either
19883 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19884 to explicitly check cxx_dialect here. */
19885 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19886 /* OK, there is a default argument. Wait until after the
19887 conversion check to do substitution. */
19888 continue;
19889
19890 /* If the type parameter is a parameter pack, then it will
19891 be deduced to an empty parameter pack. */
19892 if (template_parameter_pack_p (tparm))
19893 {
19894 tree arg;
19895
19896 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19897 {
19898 arg = make_node (NONTYPE_ARGUMENT_PACK);
19899 TREE_CONSTANT (arg) = 1;
19900 }
19901 else
19902 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19903
19904 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19905
19906 TREE_VEC_ELT (targs, i) = arg;
19907 continue;
19908 }
19909
19910 return unify_parameter_deduction_failure (explain_p, tparm);
19911 }
19912
19913 /* DR 1391: All parameters have args, now check non-dependent parms for
19914 convertibility. */
19915 if (saw_undeduced < 2)
19916 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19917 parms && parms != void_list_node && ia < nargs; )
19918 {
19919 parm = TREE_VALUE (parms);
19920
19921 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19922 && (!TREE_CHAIN (parms)
19923 || TREE_CHAIN (parms) == void_list_node))
19924 /* For a function parameter pack that occurs at the end of the
19925 parameter-declaration-list, the type A of each remaining
19926 argument of the call is compared with the type P of the
19927 declarator-id of the function parameter pack. */
19928 break;
19929
19930 parms = TREE_CHAIN (parms);
19931
19932 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19933 /* For a function parameter pack that does not occur at the
19934 end of the parameter-declaration-list, the type of the
19935 parameter pack is a non-deduced context. */
19936 continue;
19937
19938 arg = args[ia];
19939 ++ia;
19940
19941 if (uses_template_parms (parm))
19942 continue;
19943 if (check_non_deducible_conversion (parm, arg, strict, flags,
19944 explain_p))
19945 return 1;
19946 }
19947
19948 /* Now substitute into the default template arguments. */
19949 for (i = 0; i < ntparms; i++)
19950 {
19951 tree targ = TREE_VEC_ELT (targs, i);
19952 tree tparm = TREE_VEC_ELT (tparms, i);
19953
19954 if (targ || tparm == error_mark_node)
19955 continue;
19956 tree parm = TREE_VALUE (tparm);
19957
19958 tsubst_flags_t fcomplain = complain;
19959 if (saw_undeduced == 1)
19960 {
19961 /* When saw_undeduced == 1, substitution into parm and arg might
19962 fail or not replace all template parameters, and that's
19963 fine. */
19964 fcomplain = tf_none;
19965 if (TREE_CODE (parm) == PARM_DECL
19966 && uses_template_parms (TREE_TYPE (parm)))
19967 continue;
19968 }
19969
19970 tree arg = TREE_PURPOSE (tparm);
19971 reopen_deferring_access_checks (*checks);
19972 location_t save_loc = input_location;
19973 if (DECL_P (parm))
19974 input_location = DECL_SOURCE_LOCATION (parm);
19975 arg = tsubst_template_arg (arg, full_targs, fcomplain, NULL_TREE);
19976 if (arg != error_mark_node && !uses_template_parms (arg))
19977 arg = convert_template_argument (parm, arg, full_targs, complain,
19978 i, NULL_TREE);
19979 else if (saw_undeduced == 1)
19980 arg = NULL_TREE;
19981 else
19982 arg = error_mark_node;
19983 input_location = save_loc;
19984 *checks = get_deferred_access_checks ();
19985 pop_deferring_access_checks ();
19986 if (arg == error_mark_node)
19987 return 1;
19988 else if (arg)
19989 {
19990 TREE_VEC_ELT (targs, i) = arg;
19991 /* The position of the first default template argument,
19992 is also the number of non-defaulted arguments in TARGS.
19993 Record that. */
19994 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19995 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19996 }
19997 }
19998
19999 if (saw_undeduced++ == 1)
20000 goto again;
20001 }
20002
20003 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20004 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
20005
20006 return unify_success (explain_p);
20007 }
20008
20009 /* Subroutine of type_unification_real. Args are like the variables
20010 at the call site. ARG is an overloaded function (or template-id);
20011 we try deducing template args from each of the overloads, and if
20012 only one succeeds, we go with that. Modifies TARGS and returns
20013 true on success. */
20014
20015 static bool
20016 resolve_overloaded_unification (tree tparms,
20017 tree targs,
20018 tree parm,
20019 tree arg,
20020 unification_kind_t strict,
20021 int sub_strict,
20022 bool explain_p)
20023 {
20024 tree tempargs = copy_node (targs);
20025 int good = 0;
20026 tree goodfn = NULL_TREE;
20027 bool addr_p;
20028
20029 if (TREE_CODE (arg) == ADDR_EXPR)
20030 {
20031 arg = TREE_OPERAND (arg, 0);
20032 addr_p = true;
20033 }
20034 else
20035 addr_p = false;
20036
20037 if (TREE_CODE (arg) == COMPONENT_REF)
20038 /* Handle `&x' where `x' is some static or non-static member
20039 function name. */
20040 arg = TREE_OPERAND (arg, 1);
20041
20042 if (TREE_CODE (arg) == OFFSET_REF)
20043 arg = TREE_OPERAND (arg, 1);
20044
20045 /* Strip baselink information. */
20046 if (BASELINK_P (arg))
20047 arg = BASELINK_FUNCTIONS (arg);
20048
20049 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
20050 {
20051 /* If we got some explicit template args, we need to plug them into
20052 the affected templates before we try to unify, in case the
20053 explicit args will completely resolve the templates in question. */
20054
20055 int ok = 0;
20056 tree expl_subargs = TREE_OPERAND (arg, 1);
20057 arg = TREE_OPERAND (arg, 0);
20058
20059 for (lkp_iterator iter (arg); iter; ++iter)
20060 {
20061 tree fn = *iter;
20062 tree subargs, elem;
20063
20064 if (TREE_CODE (fn) != TEMPLATE_DECL)
20065 continue;
20066
20067 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20068 expl_subargs, NULL_TREE, tf_none,
20069 /*require_all_args=*/true,
20070 /*use_default_args=*/true);
20071 if (subargs != error_mark_node
20072 && !any_dependent_template_arguments_p (subargs))
20073 {
20074 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
20075 if (try_one_overload (tparms, targs, tempargs, parm,
20076 elem, strict, sub_strict, addr_p, explain_p)
20077 && (!goodfn || !same_type_p (goodfn, elem)))
20078 {
20079 goodfn = elem;
20080 ++good;
20081 }
20082 }
20083 else if (subargs)
20084 ++ok;
20085 }
20086 /* If no templates (or more than one) are fully resolved by the
20087 explicit arguments, this template-id is a non-deduced context; it
20088 could still be OK if we deduce all template arguments for the
20089 enclosing call through other arguments. */
20090 if (good != 1)
20091 good = ok;
20092 }
20093 else if (TREE_CODE (arg) != OVERLOAD
20094 && TREE_CODE (arg) != FUNCTION_DECL)
20095 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20096 -- but the deduction does not succeed because the expression is
20097 not just the function on its own. */
20098 return false;
20099 else
20100 for (lkp_iterator iter (arg); iter; ++iter)
20101 {
20102 tree fn = *iter;
20103 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
20104 strict, sub_strict, addr_p, explain_p)
20105 && (!goodfn || !decls_match (goodfn, fn)))
20106 {
20107 goodfn = fn;
20108 ++good;
20109 }
20110 }
20111
20112 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20113 to function or pointer to member function argument if the set of
20114 overloaded functions does not contain function templates and at most
20115 one of a set of overloaded functions provides a unique match.
20116
20117 So if we found multiple possibilities, we return success but don't
20118 deduce anything. */
20119
20120 if (good == 1)
20121 {
20122 int i = TREE_VEC_LENGTH (targs);
20123 for (; i--; )
20124 if (TREE_VEC_ELT (tempargs, i))
20125 {
20126 tree old = TREE_VEC_ELT (targs, i);
20127 tree new_ = TREE_VEC_ELT (tempargs, i);
20128 if (new_ && old && ARGUMENT_PACK_P (old)
20129 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
20130 /* Don't forget explicit template arguments in a pack. */
20131 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
20132 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
20133 TREE_VEC_ELT (targs, i) = new_;
20134 }
20135 }
20136 if (good)
20137 return true;
20138
20139 return false;
20140 }
20141
20142 /* Core DR 115: In contexts where deduction is done and fails, or in
20143 contexts where deduction is not done, if a template argument list is
20144 specified and it, along with any default template arguments, identifies
20145 a single function template specialization, then the template-id is an
20146 lvalue for the function template specialization. */
20147
20148 tree
20149 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
20150 {
20151 tree expr, offset, baselink;
20152 bool addr;
20153
20154 if (!type_unknown_p (orig_expr))
20155 return orig_expr;
20156
20157 expr = orig_expr;
20158 addr = false;
20159 offset = NULL_TREE;
20160 baselink = NULL_TREE;
20161
20162 if (TREE_CODE (expr) == ADDR_EXPR)
20163 {
20164 expr = TREE_OPERAND (expr, 0);
20165 addr = true;
20166 }
20167 if (TREE_CODE (expr) == OFFSET_REF)
20168 {
20169 offset = expr;
20170 expr = TREE_OPERAND (expr, 1);
20171 }
20172 if (BASELINK_P (expr))
20173 {
20174 baselink = expr;
20175 expr = BASELINK_FUNCTIONS (expr);
20176 }
20177
20178 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
20179 {
20180 int good = 0;
20181 tree goodfn = NULL_TREE;
20182
20183 /* If we got some explicit template args, we need to plug them into
20184 the affected templates before we try to unify, in case the
20185 explicit args will completely resolve the templates in question. */
20186
20187 tree expl_subargs = TREE_OPERAND (expr, 1);
20188 tree arg = TREE_OPERAND (expr, 0);
20189 tree badfn = NULL_TREE;
20190 tree badargs = NULL_TREE;
20191
20192 for (lkp_iterator iter (arg); iter; ++iter)
20193 {
20194 tree fn = *iter;
20195 tree subargs, elem;
20196
20197 if (TREE_CODE (fn) != TEMPLATE_DECL)
20198 continue;
20199
20200 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20201 expl_subargs, NULL_TREE, tf_none,
20202 /*require_all_args=*/true,
20203 /*use_default_args=*/true);
20204 if (subargs != error_mark_node
20205 && !any_dependent_template_arguments_p (subargs))
20206 {
20207 elem = instantiate_template (fn, subargs, tf_none);
20208 if (elem == error_mark_node)
20209 {
20210 badfn = fn;
20211 badargs = subargs;
20212 }
20213 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20214 {
20215 goodfn = elem;
20216 ++good;
20217 }
20218 }
20219 }
20220 if (good == 1)
20221 {
20222 mark_used (goodfn);
20223 expr = goodfn;
20224 if (baselink)
20225 expr = build_baselink (BASELINK_BINFO (baselink),
20226 BASELINK_ACCESS_BINFO (baselink),
20227 expr, BASELINK_OPTYPE (baselink));
20228 if (offset)
20229 {
20230 tree base
20231 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20232 expr = build_offset_ref (base, expr, addr, complain);
20233 }
20234 if (addr)
20235 expr = cp_build_addr_expr (expr, complain);
20236 return expr;
20237 }
20238 else if (good == 0 && badargs && (complain & tf_error))
20239 /* There were no good options and at least one bad one, so let the
20240 user know what the problem is. */
20241 instantiate_template (badfn, badargs, complain);
20242 }
20243 return orig_expr;
20244 }
20245
20246 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20247 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20248 different overloads deduce different arguments for a given parm.
20249 ADDR_P is true if the expression for which deduction is being
20250 performed was of the form "& fn" rather than simply "fn".
20251
20252 Returns 1 on success. */
20253
20254 static int
20255 try_one_overload (tree tparms,
20256 tree orig_targs,
20257 tree targs,
20258 tree parm,
20259 tree arg,
20260 unification_kind_t strict,
20261 int sub_strict,
20262 bool addr_p,
20263 bool explain_p)
20264 {
20265 int nargs;
20266 tree tempargs;
20267 int i;
20268
20269 if (arg == error_mark_node)
20270 return 0;
20271
20272 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20273 to function or pointer to member function argument if the set of
20274 overloaded functions does not contain function templates and at most
20275 one of a set of overloaded functions provides a unique match.
20276
20277 So if this is a template, just return success. */
20278
20279 if (uses_template_parms (arg))
20280 return 1;
20281
20282 if (TREE_CODE (arg) == METHOD_TYPE)
20283 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20284 else if (addr_p)
20285 arg = build_pointer_type (arg);
20286
20287 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20288
20289 /* We don't copy orig_targs for this because if we have already deduced
20290 some template args from previous args, unify would complain when we
20291 try to deduce a template parameter for the same argument, even though
20292 there isn't really a conflict. */
20293 nargs = TREE_VEC_LENGTH (targs);
20294 tempargs = make_tree_vec (nargs);
20295
20296 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20297 return 0;
20298
20299 /* First make sure we didn't deduce anything that conflicts with
20300 explicitly specified args. */
20301 for (i = nargs; i--; )
20302 {
20303 tree elt = TREE_VEC_ELT (tempargs, i);
20304 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20305
20306 if (!elt)
20307 /*NOP*/;
20308 else if (uses_template_parms (elt))
20309 /* Since we're unifying against ourselves, we will fill in
20310 template args used in the function parm list with our own
20311 template parms. Discard them. */
20312 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20313 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20314 {
20315 /* Check that the argument at each index of the deduced argument pack
20316 is equivalent to the corresponding explicitly specified argument.
20317 We may have deduced more arguments than were explicitly specified,
20318 and that's OK. */
20319
20320 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20321 that's wrong if we deduce the same argument pack from multiple
20322 function arguments: it's only incomplete the first time. */
20323
20324 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20325 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20326
20327 if (TREE_VEC_LENGTH (deduced_pack)
20328 < TREE_VEC_LENGTH (explicit_pack))
20329 return 0;
20330
20331 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20332 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20333 TREE_VEC_ELT (deduced_pack, j)))
20334 return 0;
20335 }
20336 else if (oldelt && !template_args_equal (oldelt, elt))
20337 return 0;
20338 }
20339
20340 for (i = nargs; i--; )
20341 {
20342 tree elt = TREE_VEC_ELT (tempargs, i);
20343
20344 if (elt)
20345 TREE_VEC_ELT (targs, i) = elt;
20346 }
20347
20348 return 1;
20349 }
20350
20351 /* PARM is a template class (perhaps with unbound template
20352 parameters). ARG is a fully instantiated type. If ARG can be
20353 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20354 TARGS are as for unify. */
20355
20356 static tree
20357 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20358 bool explain_p)
20359 {
20360 tree copy_of_targs;
20361
20362 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20363 return NULL_TREE;
20364 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20365 /* Matches anything. */;
20366 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20367 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20368 return NULL_TREE;
20369
20370 /* We need to make a new template argument vector for the call to
20371 unify. If we used TARGS, we'd clutter it up with the result of
20372 the attempted unification, even if this class didn't work out.
20373 We also don't want to commit ourselves to all the unifications
20374 we've already done, since unification is supposed to be done on
20375 an argument-by-argument basis. In other words, consider the
20376 following pathological case:
20377
20378 template <int I, int J, int K>
20379 struct S {};
20380
20381 template <int I, int J>
20382 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20383
20384 template <int I, int J, int K>
20385 void f(S<I, J, K>, S<I, I, I>);
20386
20387 void g() {
20388 S<0, 0, 0> s0;
20389 S<0, 1, 2> s2;
20390
20391 f(s0, s2);
20392 }
20393
20394 Now, by the time we consider the unification involving `s2', we
20395 already know that we must have `f<0, 0, 0>'. But, even though
20396 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20397 because there are two ways to unify base classes of S<0, 1, 2>
20398 with S<I, I, I>. If we kept the already deduced knowledge, we
20399 would reject the possibility I=1. */
20400 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20401
20402 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20403 {
20404 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20405 return NULL_TREE;
20406 return arg;
20407 }
20408
20409 /* If unification failed, we're done. */
20410 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20411 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20412 return NULL_TREE;
20413
20414 return arg;
20415 }
20416
20417 /* Given a template type PARM and a class type ARG, find the unique
20418 base type in ARG that is an instance of PARM. We do not examine
20419 ARG itself; only its base-classes. If there is not exactly one
20420 appropriate base class, return NULL_TREE. PARM may be the type of
20421 a partial specialization, as well as a plain template type. Used
20422 by unify. */
20423
20424 static enum template_base_result
20425 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20426 bool explain_p, tree *result)
20427 {
20428 tree rval = NULL_TREE;
20429 tree binfo;
20430
20431 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20432
20433 binfo = TYPE_BINFO (complete_type (arg));
20434 if (!binfo)
20435 {
20436 /* The type could not be completed. */
20437 *result = NULL_TREE;
20438 return tbr_incomplete_type;
20439 }
20440
20441 /* Walk in inheritance graph order. The search order is not
20442 important, and this avoids multiple walks of virtual bases. */
20443 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20444 {
20445 tree r = try_class_unification (tparms, targs, parm,
20446 BINFO_TYPE (binfo), explain_p);
20447
20448 if (r)
20449 {
20450 /* If there is more than one satisfactory baseclass, then:
20451
20452 [temp.deduct.call]
20453
20454 If they yield more than one possible deduced A, the type
20455 deduction fails.
20456
20457 applies. */
20458 if (rval && !same_type_p (r, rval))
20459 {
20460 *result = NULL_TREE;
20461 return tbr_ambiguous_baseclass;
20462 }
20463
20464 rval = r;
20465 }
20466 }
20467
20468 *result = rval;
20469 return tbr_success;
20470 }
20471
20472 /* Returns the level of DECL, which declares a template parameter. */
20473
20474 static int
20475 template_decl_level (tree decl)
20476 {
20477 switch (TREE_CODE (decl))
20478 {
20479 case TYPE_DECL:
20480 case TEMPLATE_DECL:
20481 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20482
20483 case PARM_DECL:
20484 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20485
20486 default:
20487 gcc_unreachable ();
20488 }
20489 return 0;
20490 }
20491
20492 /* Decide whether ARG can be unified with PARM, considering only the
20493 cv-qualifiers of each type, given STRICT as documented for unify.
20494 Returns nonzero iff the unification is OK on that basis. */
20495
20496 static int
20497 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20498 {
20499 int arg_quals = cp_type_quals (arg);
20500 int parm_quals = cp_type_quals (parm);
20501
20502 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20503 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20504 {
20505 /* Although a CVR qualifier is ignored when being applied to a
20506 substituted template parameter ([8.3.2]/1 for example), that
20507 does not allow us to unify "const T" with "int&" because both
20508 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20509 It is ok when we're allowing additional CV qualifiers
20510 at the outer level [14.8.2.1]/3,1st bullet. */
20511 if ((TREE_CODE (arg) == REFERENCE_TYPE
20512 || TREE_CODE (arg) == FUNCTION_TYPE
20513 || TREE_CODE (arg) == METHOD_TYPE)
20514 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20515 return 0;
20516
20517 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20518 && (parm_quals & TYPE_QUAL_RESTRICT))
20519 return 0;
20520 }
20521
20522 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20523 && (arg_quals & parm_quals) != parm_quals)
20524 return 0;
20525
20526 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20527 && (parm_quals & arg_quals) != arg_quals)
20528 return 0;
20529
20530 return 1;
20531 }
20532
20533 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20534 void
20535 template_parm_level_and_index (tree parm, int* level, int* index)
20536 {
20537 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20538 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20539 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20540 {
20541 *index = TEMPLATE_TYPE_IDX (parm);
20542 *level = TEMPLATE_TYPE_LEVEL (parm);
20543 }
20544 else
20545 {
20546 *index = TEMPLATE_PARM_IDX (parm);
20547 *level = TEMPLATE_PARM_LEVEL (parm);
20548 }
20549 }
20550
20551 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20552 do { \
20553 if (unify (TP, TA, P, A, S, EP)) \
20554 return 1; \
20555 } while (0)
20556
20557 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20558 expansion at the end of PACKED_PARMS. Returns 0 if the type
20559 deduction succeeds, 1 otherwise. STRICT is the same as in
20560 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20561 function call argument list. We'll need to adjust the arguments to make them
20562 types. SUBR tells us if this is from a recursive call to
20563 type_unification_real, or for comparing two template argument
20564 lists. */
20565
20566 static int
20567 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20568 tree packed_args, unification_kind_t strict,
20569 bool subr, bool explain_p)
20570 {
20571 tree parm
20572 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20573 tree pattern = PACK_EXPANSION_PATTERN (parm);
20574 tree pack, packs = NULL_TREE;
20575 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20576
20577 /* Add in any args remembered from an earlier partial instantiation. */
20578 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20579 int levels = TMPL_ARGS_DEPTH (targs);
20580
20581 packed_args = expand_template_argument_pack (packed_args);
20582
20583 int len = TREE_VEC_LENGTH (packed_args);
20584
20585 /* Determine the parameter packs we will be deducing from the
20586 pattern, and record their current deductions. */
20587 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20588 pack; pack = TREE_CHAIN (pack))
20589 {
20590 tree parm_pack = TREE_VALUE (pack);
20591 int idx, level;
20592
20593 /* Determine the index and level of this parameter pack. */
20594 template_parm_level_and_index (parm_pack, &level, &idx);
20595 if (level < levels)
20596 continue;
20597
20598 /* Keep track of the parameter packs and their corresponding
20599 argument packs. */
20600 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20601 TREE_TYPE (packs) = make_tree_vec (len - start);
20602 }
20603
20604 /* Loop through all of the arguments that have not yet been
20605 unified and unify each with the pattern. */
20606 for (i = start; i < len; i++)
20607 {
20608 tree parm;
20609 bool any_explicit = false;
20610 tree arg = TREE_VEC_ELT (packed_args, i);
20611
20612 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20613 or the element of its argument pack at the current index if
20614 this argument was explicitly specified. */
20615 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20616 {
20617 int idx, level;
20618 tree arg, pargs;
20619 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20620
20621 arg = NULL_TREE;
20622 if (TREE_VALUE (pack)
20623 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20624 && (i - start < TREE_VEC_LENGTH (pargs)))
20625 {
20626 any_explicit = true;
20627 arg = TREE_VEC_ELT (pargs, i - start);
20628 }
20629 TMPL_ARG (targs, level, idx) = arg;
20630 }
20631
20632 /* If we had explicit template arguments, substitute them into the
20633 pattern before deduction. */
20634 if (any_explicit)
20635 {
20636 /* Some arguments might still be unspecified or dependent. */
20637 bool dependent;
20638 ++processing_template_decl;
20639 dependent = any_dependent_template_arguments_p (targs);
20640 if (!dependent)
20641 --processing_template_decl;
20642 parm = tsubst (pattern, targs,
20643 explain_p ? tf_warning_or_error : tf_none,
20644 NULL_TREE);
20645 if (dependent)
20646 --processing_template_decl;
20647 if (parm == error_mark_node)
20648 return 1;
20649 }
20650 else
20651 parm = pattern;
20652
20653 /* Unify the pattern with the current argument. */
20654 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20655 explain_p))
20656 return 1;
20657
20658 /* For each parameter pack, collect the deduced value. */
20659 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20660 {
20661 int idx, level;
20662 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20663
20664 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20665 TMPL_ARG (targs, level, idx);
20666 }
20667 }
20668
20669 /* Verify that the results of unification with the parameter packs
20670 produce results consistent with what we've seen before, and make
20671 the deduced argument packs available. */
20672 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20673 {
20674 tree old_pack = TREE_VALUE (pack);
20675 tree new_args = TREE_TYPE (pack);
20676 int i, len = TREE_VEC_LENGTH (new_args);
20677 int idx, level;
20678 bool nondeduced_p = false;
20679
20680 /* By default keep the original deduced argument pack.
20681 If necessary, more specific code is going to update the
20682 resulting deduced argument later down in this function. */
20683 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20684 TMPL_ARG (targs, level, idx) = old_pack;
20685
20686 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20687 actually deduce anything. */
20688 for (i = 0; i < len && !nondeduced_p; ++i)
20689 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20690 nondeduced_p = true;
20691 if (nondeduced_p)
20692 continue;
20693
20694 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20695 {
20696 /* If we had fewer function args than explicit template args,
20697 just use the explicits. */
20698 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20699 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20700 if (len < explicit_len)
20701 new_args = explicit_args;
20702 }
20703
20704 if (!old_pack)
20705 {
20706 tree result;
20707 /* Build the deduced *_ARGUMENT_PACK. */
20708 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20709 {
20710 result = make_node (NONTYPE_ARGUMENT_PACK);
20711 TREE_CONSTANT (result) = 1;
20712 }
20713 else
20714 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20715
20716 SET_ARGUMENT_PACK_ARGS (result, new_args);
20717
20718 /* Note the deduced argument packs for this parameter
20719 pack. */
20720 TMPL_ARG (targs, level, idx) = result;
20721 }
20722 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20723 && (ARGUMENT_PACK_ARGS (old_pack)
20724 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20725 {
20726 /* We only had the explicitly-provided arguments before, but
20727 now we have a complete set of arguments. */
20728 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20729
20730 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20731 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20732 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20733 }
20734 else
20735 {
20736 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20737 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20738
20739 if (!comp_template_args (old_args, new_args,
20740 &bad_old_arg, &bad_new_arg))
20741 /* Inconsistent unification of this parameter pack. */
20742 return unify_parameter_pack_inconsistent (explain_p,
20743 bad_old_arg,
20744 bad_new_arg);
20745 }
20746 }
20747
20748 return unify_success (explain_p);
20749 }
20750
20751 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20752 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20753 parameters and return value are as for unify. */
20754
20755 static int
20756 unify_array_domain (tree tparms, tree targs,
20757 tree parm_dom, tree arg_dom,
20758 bool explain_p)
20759 {
20760 tree parm_max;
20761 tree arg_max;
20762 bool parm_cst;
20763 bool arg_cst;
20764
20765 /* Our representation of array types uses "N - 1" as the
20766 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20767 not an integer constant. We cannot unify arbitrarily
20768 complex expressions, so we eliminate the MINUS_EXPRs
20769 here. */
20770 parm_max = TYPE_MAX_VALUE (parm_dom);
20771 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20772 if (!parm_cst)
20773 {
20774 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20775 parm_max = TREE_OPERAND (parm_max, 0);
20776 }
20777 arg_max = TYPE_MAX_VALUE (arg_dom);
20778 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20779 if (!arg_cst)
20780 {
20781 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20782 trying to unify the type of a variable with the type
20783 of a template parameter. For example:
20784
20785 template <unsigned int N>
20786 void f (char (&) [N]);
20787 int g();
20788 void h(int i) {
20789 char a[g(i)];
20790 f(a);
20791 }
20792
20793 Here, the type of the ARG will be "int [g(i)]", and
20794 may be a SAVE_EXPR, etc. */
20795 if (TREE_CODE (arg_max) != MINUS_EXPR)
20796 return unify_vla_arg (explain_p, arg_dom);
20797 arg_max = TREE_OPERAND (arg_max, 0);
20798 }
20799
20800 /* If only one of the bounds used a MINUS_EXPR, compensate
20801 by adding one to the other bound. */
20802 if (parm_cst && !arg_cst)
20803 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20804 integer_type_node,
20805 parm_max,
20806 integer_one_node);
20807 else if (arg_cst && !parm_cst)
20808 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20809 integer_type_node,
20810 arg_max,
20811 integer_one_node);
20812
20813 return unify (tparms, targs, parm_max, arg_max,
20814 UNIFY_ALLOW_INTEGER, explain_p);
20815 }
20816
20817 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20818
20819 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20820
20821 static pa_kind_t
20822 pa_kind (tree t)
20823 {
20824 if (PACK_EXPANSION_P (t))
20825 t = PACK_EXPANSION_PATTERN (t);
20826 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20827 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20828 || DECL_TYPE_TEMPLATE_P (t))
20829 return pa_tmpl;
20830 else if (TYPE_P (t))
20831 return pa_type;
20832 else
20833 return pa_expr;
20834 }
20835
20836 /* Deduce the value of template parameters. TPARMS is the (innermost)
20837 set of template parameters to a template. TARGS is the bindings
20838 for those template parameters, as determined thus far; TARGS may
20839 include template arguments for outer levels of template parameters
20840 as well. PARM is a parameter to a template function, or a
20841 subcomponent of that parameter; ARG is the corresponding argument.
20842 This function attempts to match PARM with ARG in a manner
20843 consistent with the existing assignments in TARGS. If more values
20844 are deduced, then TARGS is updated.
20845
20846 Returns 0 if the type deduction succeeds, 1 otherwise. The
20847 parameter STRICT is a bitwise or of the following flags:
20848
20849 UNIFY_ALLOW_NONE:
20850 Require an exact match between PARM and ARG.
20851 UNIFY_ALLOW_MORE_CV_QUAL:
20852 Allow the deduced ARG to be more cv-qualified (by qualification
20853 conversion) than ARG.
20854 UNIFY_ALLOW_LESS_CV_QUAL:
20855 Allow the deduced ARG to be less cv-qualified than ARG.
20856 UNIFY_ALLOW_DERIVED:
20857 Allow the deduced ARG to be a template base class of ARG,
20858 or a pointer to a template base class of the type pointed to by
20859 ARG.
20860 UNIFY_ALLOW_INTEGER:
20861 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20862 case for more information.
20863 UNIFY_ALLOW_OUTER_LEVEL:
20864 This is the outermost level of a deduction. Used to determine validity
20865 of qualification conversions. A valid qualification conversion must
20866 have const qualified pointers leading up to the inner type which
20867 requires additional CV quals, except at the outer level, where const
20868 is not required [conv.qual]. It would be normal to set this flag in
20869 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20870 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20871 This is the outermost level of a deduction, and PARM can be more CV
20872 qualified at this point.
20873 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20874 This is the outermost level of a deduction, and PARM can be less CV
20875 qualified at this point. */
20876
20877 static int
20878 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20879 bool explain_p)
20880 {
20881 int idx;
20882 tree targ;
20883 tree tparm;
20884 int strict_in = strict;
20885 tsubst_flags_t complain = (explain_p
20886 ? tf_warning_or_error
20887 : tf_none);
20888
20889 /* I don't think this will do the right thing with respect to types.
20890 But the only case I've seen it in so far has been array bounds, where
20891 signedness is the only information lost, and I think that will be
20892 okay. */
20893 while (CONVERT_EXPR_P (parm))
20894 parm = TREE_OPERAND (parm, 0);
20895
20896 if (arg == error_mark_node)
20897 return unify_invalid (explain_p);
20898 if (arg == unknown_type_node
20899 || arg == init_list_type_node)
20900 /* We can't deduce anything from this, but we might get all the
20901 template args from other function args. */
20902 return unify_success (explain_p);
20903
20904 if (parm == any_targ_node || arg == any_targ_node)
20905 return unify_success (explain_p);
20906
20907 /* If PARM uses template parameters, then we can't bail out here,
20908 even if ARG == PARM, since we won't record unifications for the
20909 template parameters. We might need them if we're trying to
20910 figure out which of two things is more specialized. */
20911 if (arg == parm && !uses_template_parms (parm))
20912 return unify_success (explain_p);
20913
20914 /* Handle init lists early, so the rest of the function can assume
20915 we're dealing with a type. */
20916 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20917 {
20918 tree elt, elttype;
20919 unsigned i;
20920 tree orig_parm = parm;
20921
20922 /* Replace T with std::initializer_list<T> for deduction. */
20923 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20924 && flag_deduce_init_list)
20925 parm = listify (parm);
20926
20927 if (!is_std_init_list (parm)
20928 && TREE_CODE (parm) != ARRAY_TYPE)
20929 /* We can only deduce from an initializer list argument if the
20930 parameter is std::initializer_list or an array; otherwise this
20931 is a non-deduced context. */
20932 return unify_success (explain_p);
20933
20934 if (TREE_CODE (parm) == ARRAY_TYPE)
20935 elttype = TREE_TYPE (parm);
20936 else
20937 {
20938 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20939 /* Deduction is defined in terms of a single type, so just punt
20940 on the (bizarre) std::initializer_list<T...>. */
20941 if (PACK_EXPANSION_P (elttype))
20942 return unify_success (explain_p);
20943 }
20944
20945 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20946 {
20947 int elt_strict = strict;
20948
20949 if (elt == error_mark_node)
20950 return unify_invalid (explain_p);
20951
20952 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20953 {
20954 tree type = TREE_TYPE (elt);
20955 if (type == error_mark_node)
20956 return unify_invalid (explain_p);
20957 /* It should only be possible to get here for a call. */
20958 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20959 elt_strict |= maybe_adjust_types_for_deduction
20960 (DEDUCE_CALL, &elttype, &type, elt);
20961 elt = type;
20962 }
20963
20964 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20965 explain_p);
20966 }
20967
20968 if (TREE_CODE (parm) == ARRAY_TYPE
20969 && deducible_array_bound (TYPE_DOMAIN (parm)))
20970 {
20971 /* Also deduce from the length of the initializer list. */
20972 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20973 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20974 if (idx == error_mark_node)
20975 return unify_invalid (explain_p);
20976 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20977 idx, explain_p);
20978 }
20979
20980 /* If the std::initializer_list<T> deduction worked, replace the
20981 deduced A with std::initializer_list<A>. */
20982 if (orig_parm != parm)
20983 {
20984 idx = TEMPLATE_TYPE_IDX (orig_parm);
20985 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20986 targ = listify (targ);
20987 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20988 }
20989 return unify_success (explain_p);
20990 }
20991
20992 /* If parm and arg aren't the same kind of thing (template, type, or
20993 expression), fail early. */
20994 if (pa_kind (parm) != pa_kind (arg))
20995 return unify_invalid (explain_p);
20996
20997 /* Immediately reject some pairs that won't unify because of
20998 cv-qualification mismatches. */
20999 if (TREE_CODE (arg) == TREE_CODE (parm)
21000 && TYPE_P (arg)
21001 /* It is the elements of the array which hold the cv quals of an array
21002 type, and the elements might be template type parms. We'll check
21003 when we recurse. */
21004 && TREE_CODE (arg) != ARRAY_TYPE
21005 /* We check the cv-qualifiers when unifying with template type
21006 parameters below. We want to allow ARG `const T' to unify with
21007 PARM `T' for example, when computing which of two templates
21008 is more specialized, for example. */
21009 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
21010 && !check_cv_quals_for_unify (strict_in, arg, parm))
21011 return unify_cv_qual_mismatch (explain_p, parm, arg);
21012
21013 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
21014 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
21015 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
21016 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
21017 strict &= ~UNIFY_ALLOW_DERIVED;
21018 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21019 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
21020
21021 switch (TREE_CODE (parm))
21022 {
21023 case TYPENAME_TYPE:
21024 case SCOPE_REF:
21025 case UNBOUND_CLASS_TEMPLATE:
21026 /* In a type which contains a nested-name-specifier, template
21027 argument values cannot be deduced for template parameters used
21028 within the nested-name-specifier. */
21029 return unify_success (explain_p);
21030
21031 case TEMPLATE_TYPE_PARM:
21032 case TEMPLATE_TEMPLATE_PARM:
21033 case BOUND_TEMPLATE_TEMPLATE_PARM:
21034 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21035 if (error_operand_p (tparm))
21036 return unify_invalid (explain_p);
21037
21038 if (TEMPLATE_TYPE_LEVEL (parm)
21039 != template_decl_level (tparm))
21040 /* The PARM is not one we're trying to unify. Just check
21041 to see if it matches ARG. */
21042 {
21043 if (TREE_CODE (arg) == TREE_CODE (parm)
21044 && (is_auto (parm) ? is_auto (arg)
21045 : same_type_p (parm, arg)))
21046 return unify_success (explain_p);
21047 else
21048 return unify_type_mismatch (explain_p, parm, arg);
21049 }
21050 idx = TEMPLATE_TYPE_IDX (parm);
21051 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21052 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
21053 if (error_operand_p (tparm))
21054 return unify_invalid (explain_p);
21055
21056 /* Check for mixed types and values. */
21057 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21058 && TREE_CODE (tparm) != TYPE_DECL)
21059 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21060 && TREE_CODE (tparm) != TEMPLATE_DECL))
21061 gcc_unreachable ();
21062
21063 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21064 {
21065 if ((strict_in & UNIFY_ALLOW_DERIVED)
21066 && CLASS_TYPE_P (arg))
21067 {
21068 /* First try to match ARG directly. */
21069 tree t = try_class_unification (tparms, targs, parm, arg,
21070 explain_p);
21071 if (!t)
21072 {
21073 /* Otherwise, look for a suitable base of ARG, as below. */
21074 enum template_base_result r;
21075 r = get_template_base (tparms, targs, parm, arg,
21076 explain_p, &t);
21077 if (!t)
21078 return unify_no_common_base (explain_p, r, parm, arg);
21079 arg = t;
21080 }
21081 }
21082 /* ARG must be constructed from a template class or a template
21083 template parameter. */
21084 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
21085 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21086 return unify_template_deduction_failure (explain_p, parm, arg);
21087
21088 /* Deduce arguments T, i from TT<T> or TT<i>. */
21089 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
21090 return 1;
21091
21092 arg = TYPE_TI_TEMPLATE (arg);
21093
21094 /* Fall through to deduce template name. */
21095 }
21096
21097 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21098 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21099 {
21100 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21101
21102 /* Simple cases: Value already set, does match or doesn't. */
21103 if (targ != NULL_TREE && template_args_equal (targ, arg))
21104 return unify_success (explain_p);
21105 else if (targ)
21106 return unify_inconsistency (explain_p, parm, targ, arg);
21107 }
21108 else
21109 {
21110 /* If PARM is `const T' and ARG is only `int', we don't have
21111 a match unless we are allowing additional qualification.
21112 If ARG is `const int' and PARM is just `T' that's OK;
21113 that binds `const int' to `T'. */
21114 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
21115 arg, parm))
21116 return unify_cv_qual_mismatch (explain_p, parm, arg);
21117
21118 /* Consider the case where ARG is `const volatile int' and
21119 PARM is `const T'. Then, T should be `volatile int'. */
21120 arg = cp_build_qualified_type_real
21121 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
21122 if (arg == error_mark_node)
21123 return unify_invalid (explain_p);
21124
21125 /* Simple cases: Value already set, does match or doesn't. */
21126 if (targ != NULL_TREE && same_type_p (targ, arg))
21127 return unify_success (explain_p);
21128 else if (targ)
21129 return unify_inconsistency (explain_p, parm, targ, arg);
21130
21131 /* Make sure that ARG is not a variable-sized array. (Note
21132 that were talking about variable-sized arrays (like
21133 `int[n]'), rather than arrays of unknown size (like
21134 `int[]').) We'll get very confused by such a type since
21135 the bound of the array is not constant, and therefore
21136 not mangleable. Besides, such types are not allowed in
21137 ISO C++, so we can do as we please here. We do allow
21138 them for 'auto' deduction, since that isn't ABI-exposed. */
21139 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
21140 return unify_vla_arg (explain_p, arg);
21141
21142 /* Strip typedefs as in convert_template_argument. */
21143 arg = canonicalize_type_argument (arg, tf_none);
21144 }
21145
21146 /* If ARG is a parameter pack or an expansion, we cannot unify
21147 against it unless PARM is also a parameter pack. */
21148 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21149 && !template_parameter_pack_p (parm))
21150 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21151
21152 /* If the argument deduction results is a METHOD_TYPE,
21153 then there is a problem.
21154 METHOD_TYPE doesn't map to any real C++ type the result of
21155 the deduction can not be of that type. */
21156 if (TREE_CODE (arg) == METHOD_TYPE)
21157 return unify_method_type_error (explain_p, arg);
21158
21159 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21160 return unify_success (explain_p);
21161
21162 case TEMPLATE_PARM_INDEX:
21163 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21164 if (error_operand_p (tparm))
21165 return unify_invalid (explain_p);
21166
21167 if (TEMPLATE_PARM_LEVEL (parm)
21168 != template_decl_level (tparm))
21169 {
21170 /* The PARM is not one we're trying to unify. Just check
21171 to see if it matches ARG. */
21172 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
21173 && cp_tree_equal (parm, arg));
21174 if (result)
21175 unify_expression_unequal (explain_p, parm, arg);
21176 return result;
21177 }
21178
21179 idx = TEMPLATE_PARM_IDX (parm);
21180 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21181
21182 if (targ)
21183 {
21184 if ((strict & UNIFY_ALLOW_INTEGER)
21185 && TREE_TYPE (targ) && TREE_TYPE (arg)
21186 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
21187 /* We're deducing from an array bound, the type doesn't matter. */
21188 arg = fold_convert (TREE_TYPE (targ), arg);
21189 int x = !cp_tree_equal (targ, arg);
21190 if (x)
21191 unify_inconsistency (explain_p, parm, targ, arg);
21192 return x;
21193 }
21194
21195 /* [temp.deduct.type] If, in the declaration of a function template
21196 with a non-type template-parameter, the non-type
21197 template-parameter is used in an expression in the function
21198 parameter-list and, if the corresponding template-argument is
21199 deduced, the template-argument type shall match the type of the
21200 template-parameter exactly, except that a template-argument
21201 deduced from an array bound may be of any integral type.
21202 The non-type parameter might use already deduced type parameters. */
21203 ++processing_template_decl;
21204 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
21205 --processing_template_decl;
21206 if (tree a = type_uses_auto (tparm))
21207 {
21208 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21209 if (tparm == error_mark_node)
21210 return 1;
21211 }
21212
21213 if (!TREE_TYPE (arg))
21214 /* Template-parameter dependent expression. Just accept it for now.
21215 It will later be processed in convert_template_argument. */
21216 ;
21217 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21218 non_reference (tparm)))
21219 /* OK */;
21220 else if ((strict & UNIFY_ALLOW_INTEGER)
21221 && CP_INTEGRAL_TYPE_P (tparm))
21222 /* Convert the ARG to the type of PARM; the deduced non-type
21223 template argument must exactly match the types of the
21224 corresponding parameter. */
21225 arg = fold (build_nop (tparm, arg));
21226 else if (uses_template_parms (tparm))
21227 {
21228 /* We haven't deduced the type of this parameter yet. */
21229 if (cxx_dialect >= cxx17
21230 /* We deduce from array bounds in try_array_deduction. */
21231 && !(strict & UNIFY_ALLOW_INTEGER))
21232 {
21233 /* Deduce it from the non-type argument. */
21234 tree atype = TREE_TYPE (arg);
21235 RECUR_AND_CHECK_FAILURE (tparms, targs,
21236 tparm, atype,
21237 UNIFY_ALLOW_NONE, explain_p);
21238 }
21239 else
21240 /* Try again later. */
21241 return unify_success (explain_p);
21242 }
21243 else
21244 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21245
21246 /* If ARG is a parameter pack or an expansion, we cannot unify
21247 against it unless PARM is also a parameter pack. */
21248 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21249 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21250 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21251
21252 {
21253 bool removed_attr = false;
21254 arg = strip_typedefs_expr (arg, &removed_attr);
21255 }
21256 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21257 return unify_success (explain_p);
21258
21259 case PTRMEM_CST:
21260 {
21261 /* A pointer-to-member constant can be unified only with
21262 another constant. */
21263 if (TREE_CODE (arg) != PTRMEM_CST)
21264 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21265
21266 /* Just unify the class member. It would be useless (and possibly
21267 wrong, depending on the strict flags) to unify also
21268 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21269 arg refer to the same variable, even if through different
21270 classes. For instance:
21271
21272 struct A { int x; };
21273 struct B : A { };
21274
21275 Unification of &A::x and &B::x must succeed. */
21276 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21277 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21278 }
21279
21280 case POINTER_TYPE:
21281 {
21282 if (!TYPE_PTR_P (arg))
21283 return unify_type_mismatch (explain_p, parm, arg);
21284
21285 /* [temp.deduct.call]
21286
21287 A can be another pointer or pointer to member type that can
21288 be converted to the deduced A via a qualification
21289 conversion (_conv.qual_).
21290
21291 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21292 This will allow for additional cv-qualification of the
21293 pointed-to types if appropriate. */
21294
21295 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21296 /* The derived-to-base conversion only persists through one
21297 level of pointers. */
21298 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21299
21300 return unify (tparms, targs, TREE_TYPE (parm),
21301 TREE_TYPE (arg), strict, explain_p);
21302 }
21303
21304 case REFERENCE_TYPE:
21305 if (TREE_CODE (arg) != REFERENCE_TYPE)
21306 return unify_type_mismatch (explain_p, parm, arg);
21307 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21308 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21309
21310 case ARRAY_TYPE:
21311 if (TREE_CODE (arg) != ARRAY_TYPE)
21312 return unify_type_mismatch (explain_p, parm, arg);
21313 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21314 != (TYPE_DOMAIN (arg) == NULL_TREE))
21315 return unify_type_mismatch (explain_p, parm, arg);
21316 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21317 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21318 if (TYPE_DOMAIN (parm) != NULL_TREE)
21319 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21320 TYPE_DOMAIN (arg), explain_p);
21321 return unify_success (explain_p);
21322
21323 case REAL_TYPE:
21324 case COMPLEX_TYPE:
21325 case VECTOR_TYPE:
21326 case INTEGER_TYPE:
21327 case BOOLEAN_TYPE:
21328 case ENUMERAL_TYPE:
21329 case VOID_TYPE:
21330 case NULLPTR_TYPE:
21331 if (TREE_CODE (arg) != TREE_CODE (parm))
21332 return unify_type_mismatch (explain_p, parm, arg);
21333
21334 /* We have already checked cv-qualification at the top of the
21335 function. */
21336 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21337 return unify_type_mismatch (explain_p, parm, arg);
21338
21339 /* As far as unification is concerned, this wins. Later checks
21340 will invalidate it if necessary. */
21341 return unify_success (explain_p);
21342
21343 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21344 /* Type INTEGER_CST can come from ordinary constant template args. */
21345 case INTEGER_CST:
21346 while (CONVERT_EXPR_P (arg))
21347 arg = TREE_OPERAND (arg, 0);
21348
21349 if (TREE_CODE (arg) != INTEGER_CST)
21350 return unify_template_argument_mismatch (explain_p, parm, arg);
21351 return (tree_int_cst_equal (parm, arg)
21352 ? unify_success (explain_p)
21353 : unify_template_argument_mismatch (explain_p, parm, arg));
21354
21355 case TREE_VEC:
21356 {
21357 int i, len, argslen;
21358 int parm_variadic_p = 0;
21359
21360 if (TREE_CODE (arg) != TREE_VEC)
21361 return unify_template_argument_mismatch (explain_p, parm, arg);
21362
21363 len = TREE_VEC_LENGTH (parm);
21364 argslen = TREE_VEC_LENGTH (arg);
21365
21366 /* Check for pack expansions in the parameters. */
21367 for (i = 0; i < len; ++i)
21368 {
21369 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21370 {
21371 if (i == len - 1)
21372 /* We can unify against something with a trailing
21373 parameter pack. */
21374 parm_variadic_p = 1;
21375 else
21376 /* [temp.deduct.type]/9: If the template argument list of
21377 P contains a pack expansion that is not the last
21378 template argument, the entire template argument list
21379 is a non-deduced context. */
21380 return unify_success (explain_p);
21381 }
21382 }
21383
21384 /* If we don't have enough arguments to satisfy the parameters
21385 (not counting the pack expression at the end), or we have
21386 too many arguments for a parameter list that doesn't end in
21387 a pack expression, we can't unify. */
21388 if (parm_variadic_p
21389 ? argslen < len - parm_variadic_p
21390 : argslen != len)
21391 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21392
21393 /* Unify all of the parameters that precede the (optional)
21394 pack expression. */
21395 for (i = 0; i < len - parm_variadic_p; ++i)
21396 {
21397 RECUR_AND_CHECK_FAILURE (tparms, targs,
21398 TREE_VEC_ELT (parm, i),
21399 TREE_VEC_ELT (arg, i),
21400 UNIFY_ALLOW_NONE, explain_p);
21401 }
21402 if (parm_variadic_p)
21403 return unify_pack_expansion (tparms, targs, parm, arg,
21404 DEDUCE_EXACT,
21405 /*subr=*/true, explain_p);
21406 return unify_success (explain_p);
21407 }
21408
21409 case RECORD_TYPE:
21410 case UNION_TYPE:
21411 if (TREE_CODE (arg) != TREE_CODE (parm))
21412 return unify_type_mismatch (explain_p, parm, arg);
21413
21414 if (TYPE_PTRMEMFUNC_P (parm))
21415 {
21416 if (!TYPE_PTRMEMFUNC_P (arg))
21417 return unify_type_mismatch (explain_p, parm, arg);
21418
21419 return unify (tparms, targs,
21420 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21421 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21422 strict, explain_p);
21423 }
21424 else if (TYPE_PTRMEMFUNC_P (arg))
21425 return unify_type_mismatch (explain_p, parm, arg);
21426
21427 if (CLASSTYPE_TEMPLATE_INFO (parm))
21428 {
21429 tree t = NULL_TREE;
21430
21431 if (strict_in & UNIFY_ALLOW_DERIVED)
21432 {
21433 /* First, we try to unify the PARM and ARG directly. */
21434 t = try_class_unification (tparms, targs,
21435 parm, arg, explain_p);
21436
21437 if (!t)
21438 {
21439 /* Fallback to the special case allowed in
21440 [temp.deduct.call]:
21441
21442 If P is a class, and P has the form
21443 template-id, then A can be a derived class of
21444 the deduced A. Likewise, if P is a pointer to
21445 a class of the form template-id, A can be a
21446 pointer to a derived class pointed to by the
21447 deduced A. */
21448 enum template_base_result r;
21449 r = get_template_base (tparms, targs, parm, arg,
21450 explain_p, &t);
21451
21452 if (!t)
21453 {
21454 /* Don't give the derived diagnostic if we're
21455 already dealing with the same template. */
21456 bool same_template
21457 = (CLASSTYPE_TEMPLATE_INFO (arg)
21458 && (CLASSTYPE_TI_TEMPLATE (parm)
21459 == CLASSTYPE_TI_TEMPLATE (arg)));
21460 return unify_no_common_base (explain_p && !same_template,
21461 r, parm, arg);
21462 }
21463 }
21464 }
21465 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21466 && (CLASSTYPE_TI_TEMPLATE (parm)
21467 == CLASSTYPE_TI_TEMPLATE (arg)))
21468 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21469 Then, we should unify `int' and `U'. */
21470 t = arg;
21471 else
21472 /* There's no chance of unification succeeding. */
21473 return unify_type_mismatch (explain_p, parm, arg);
21474
21475 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21476 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21477 }
21478 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21479 return unify_type_mismatch (explain_p, parm, arg);
21480 return unify_success (explain_p);
21481
21482 case METHOD_TYPE:
21483 case FUNCTION_TYPE:
21484 {
21485 unsigned int nargs;
21486 tree *args;
21487 tree a;
21488 unsigned int i;
21489
21490 if (TREE_CODE (arg) != TREE_CODE (parm))
21491 return unify_type_mismatch (explain_p, parm, arg);
21492
21493 /* CV qualifications for methods can never be deduced, they must
21494 match exactly. We need to check them explicitly here,
21495 because type_unification_real treats them as any other
21496 cv-qualified parameter. */
21497 if (TREE_CODE (parm) == METHOD_TYPE
21498 && (!check_cv_quals_for_unify
21499 (UNIFY_ALLOW_NONE,
21500 class_of_this_parm (arg),
21501 class_of_this_parm (parm))))
21502 return unify_cv_qual_mismatch (explain_p, parm, arg);
21503 if (TREE_CODE (arg) == FUNCTION_TYPE
21504 && type_memfn_quals (parm) != type_memfn_quals (arg))
21505 return unify_cv_qual_mismatch (explain_p, parm, arg);
21506 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21507 return unify_type_mismatch (explain_p, parm, arg);
21508
21509 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21510 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21511
21512 nargs = list_length (TYPE_ARG_TYPES (arg));
21513 args = XALLOCAVEC (tree, nargs);
21514 for (a = TYPE_ARG_TYPES (arg), i = 0;
21515 a != NULL_TREE && a != void_list_node;
21516 a = TREE_CHAIN (a), ++i)
21517 args[i] = TREE_VALUE (a);
21518 nargs = i;
21519
21520 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21521 args, nargs, 1, DEDUCE_EXACT,
21522 LOOKUP_NORMAL, NULL, explain_p))
21523 return 1;
21524
21525 if (flag_noexcept_type)
21526 {
21527 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21528 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21529 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21530 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21531 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21532 && uses_template_parms (TREE_PURPOSE (pspec)))
21533 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21534 TREE_PURPOSE (aspec),
21535 UNIFY_ALLOW_NONE, explain_p);
21536 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21537 return unify_type_mismatch (explain_p, parm, arg);
21538 }
21539
21540 return 0;
21541 }
21542
21543 case OFFSET_TYPE:
21544 /* Unify a pointer to member with a pointer to member function, which
21545 deduces the type of the member as a function type. */
21546 if (TYPE_PTRMEMFUNC_P (arg))
21547 {
21548 /* Check top-level cv qualifiers */
21549 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21550 return unify_cv_qual_mismatch (explain_p, parm, arg);
21551
21552 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21553 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21554 UNIFY_ALLOW_NONE, explain_p);
21555
21556 /* Determine the type of the function we are unifying against. */
21557 tree fntype = static_fn_type (arg);
21558
21559 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21560 }
21561
21562 if (TREE_CODE (arg) != OFFSET_TYPE)
21563 return unify_type_mismatch (explain_p, parm, arg);
21564 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21565 TYPE_OFFSET_BASETYPE (arg),
21566 UNIFY_ALLOW_NONE, explain_p);
21567 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21568 strict, explain_p);
21569
21570 case CONST_DECL:
21571 if (DECL_TEMPLATE_PARM_P (parm))
21572 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21573 if (arg != scalar_constant_value (parm))
21574 return unify_template_argument_mismatch (explain_p, parm, arg);
21575 return unify_success (explain_p);
21576
21577 case FIELD_DECL:
21578 case TEMPLATE_DECL:
21579 /* Matched cases are handled by the ARG == PARM test above. */
21580 return unify_template_argument_mismatch (explain_p, parm, arg);
21581
21582 case VAR_DECL:
21583 /* We might get a variable as a non-type template argument in parm if the
21584 corresponding parameter is type-dependent. Make any necessary
21585 adjustments based on whether arg is a reference. */
21586 if (CONSTANT_CLASS_P (arg))
21587 parm = fold_non_dependent_expr (parm);
21588 else if (REFERENCE_REF_P (arg))
21589 {
21590 tree sub = TREE_OPERAND (arg, 0);
21591 STRIP_NOPS (sub);
21592 if (TREE_CODE (sub) == ADDR_EXPR)
21593 arg = TREE_OPERAND (sub, 0);
21594 }
21595 /* Now use the normal expression code to check whether they match. */
21596 goto expr;
21597
21598 case TYPE_ARGUMENT_PACK:
21599 case NONTYPE_ARGUMENT_PACK:
21600 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21601 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21602
21603 case TYPEOF_TYPE:
21604 case DECLTYPE_TYPE:
21605 case UNDERLYING_TYPE:
21606 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21607 or UNDERLYING_TYPE nodes. */
21608 return unify_success (explain_p);
21609
21610 case ERROR_MARK:
21611 /* Unification fails if we hit an error node. */
21612 return unify_invalid (explain_p);
21613
21614 case INDIRECT_REF:
21615 if (REFERENCE_REF_P (parm))
21616 {
21617 bool pexp = PACK_EXPANSION_P (arg);
21618 if (pexp)
21619 arg = PACK_EXPANSION_PATTERN (arg);
21620 if (REFERENCE_REF_P (arg))
21621 arg = TREE_OPERAND (arg, 0);
21622 if (pexp)
21623 arg = make_pack_expansion (arg, complain);
21624 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21625 strict, explain_p);
21626 }
21627 /* FALLTHRU */
21628
21629 default:
21630 /* An unresolved overload is a nondeduced context. */
21631 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21632 return unify_success (explain_p);
21633 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21634 expr:
21635 /* We must be looking at an expression. This can happen with
21636 something like:
21637
21638 template <int I>
21639 void foo(S<I>, S<I + 2>);
21640
21641 This is a "nondeduced context":
21642
21643 [deduct.type]
21644
21645 The nondeduced contexts are:
21646
21647 --A type that is a template-id in which one or more of
21648 the template-arguments is an expression that references
21649 a template-parameter.
21650
21651 In these cases, we assume deduction succeeded, but don't
21652 actually infer any unifications. */
21653
21654 if (!uses_template_parms (parm)
21655 && !template_args_equal (parm, arg))
21656 return unify_expression_unequal (explain_p, parm, arg);
21657 else
21658 return unify_success (explain_p);
21659 }
21660 }
21661 #undef RECUR_AND_CHECK_FAILURE
21662 \f
21663 /* Note that DECL can be defined in this translation unit, if
21664 required. */
21665
21666 static void
21667 mark_definable (tree decl)
21668 {
21669 tree clone;
21670 DECL_NOT_REALLY_EXTERN (decl) = 1;
21671 FOR_EACH_CLONE (clone, decl)
21672 DECL_NOT_REALLY_EXTERN (clone) = 1;
21673 }
21674
21675 /* Called if RESULT is explicitly instantiated, or is a member of an
21676 explicitly instantiated class. */
21677
21678 void
21679 mark_decl_instantiated (tree result, int extern_p)
21680 {
21681 SET_DECL_EXPLICIT_INSTANTIATION (result);
21682
21683 /* If this entity has already been written out, it's too late to
21684 make any modifications. */
21685 if (TREE_ASM_WRITTEN (result))
21686 return;
21687
21688 /* For anonymous namespace we don't need to do anything. */
21689 if (decl_anon_ns_mem_p (result))
21690 {
21691 gcc_assert (!TREE_PUBLIC (result));
21692 return;
21693 }
21694
21695 if (TREE_CODE (result) != FUNCTION_DECL)
21696 /* The TREE_PUBLIC flag for function declarations will have been
21697 set correctly by tsubst. */
21698 TREE_PUBLIC (result) = 1;
21699
21700 /* This might have been set by an earlier implicit instantiation. */
21701 DECL_COMDAT (result) = 0;
21702
21703 if (extern_p)
21704 DECL_NOT_REALLY_EXTERN (result) = 0;
21705 else
21706 {
21707 mark_definable (result);
21708 mark_needed (result);
21709 /* Always make artificials weak. */
21710 if (DECL_ARTIFICIAL (result) && flag_weak)
21711 comdat_linkage (result);
21712 /* For WIN32 we also want to put explicit instantiations in
21713 linkonce sections. */
21714 else if (TREE_PUBLIC (result))
21715 maybe_make_one_only (result);
21716 }
21717
21718 /* If EXTERN_P, then this function will not be emitted -- unless
21719 followed by an explicit instantiation, at which point its linkage
21720 will be adjusted. If !EXTERN_P, then this function will be
21721 emitted here. In neither circumstance do we want
21722 import_export_decl to adjust the linkage. */
21723 DECL_INTERFACE_KNOWN (result) = 1;
21724 }
21725
21726 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21727 important template arguments. If any are missing, we check whether
21728 they're important by using error_mark_node for substituting into any
21729 args that were used for partial ordering (the ones between ARGS and END)
21730 and seeing if it bubbles up. */
21731
21732 static bool
21733 check_undeduced_parms (tree targs, tree args, tree end)
21734 {
21735 bool found = false;
21736 int i;
21737 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21738 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21739 {
21740 found = true;
21741 TREE_VEC_ELT (targs, i) = error_mark_node;
21742 }
21743 if (found)
21744 {
21745 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21746 if (substed == error_mark_node)
21747 return true;
21748 }
21749 return false;
21750 }
21751
21752 /* Given two function templates PAT1 and PAT2, return:
21753
21754 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21755 -1 if PAT2 is more specialized than PAT1.
21756 0 if neither is more specialized.
21757
21758 LEN indicates the number of parameters we should consider
21759 (defaulted parameters should not be considered).
21760
21761 The 1998 std underspecified function template partial ordering, and
21762 DR214 addresses the issue. We take pairs of arguments, one from
21763 each of the templates, and deduce them against each other. One of
21764 the templates will be more specialized if all the *other*
21765 template's arguments deduce against its arguments and at least one
21766 of its arguments *does* *not* deduce against the other template's
21767 corresponding argument. Deduction is done as for class templates.
21768 The arguments used in deduction have reference and top level cv
21769 qualifiers removed. Iff both arguments were originally reference
21770 types *and* deduction succeeds in both directions, an lvalue reference
21771 wins against an rvalue reference and otherwise the template
21772 with the more cv-qualified argument wins for that pairing (if
21773 neither is more cv-qualified, they both are equal). Unlike regular
21774 deduction, after all the arguments have been deduced in this way,
21775 we do *not* verify the deduced template argument values can be
21776 substituted into non-deduced contexts.
21777
21778 The logic can be a bit confusing here, because we look at deduce1 and
21779 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21780 can find template arguments for pat1 to make arg1 look like arg2, that
21781 means that arg2 is at least as specialized as arg1. */
21782
21783 int
21784 more_specialized_fn (tree pat1, tree pat2, int len)
21785 {
21786 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21787 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21788 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21789 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21790 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21791 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21792 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21793 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21794 tree origs1, origs2;
21795 bool lose1 = false;
21796 bool lose2 = false;
21797
21798 /* Remove the this parameter from non-static member functions. If
21799 one is a non-static member function and the other is not a static
21800 member function, remove the first parameter from that function
21801 also. This situation occurs for operator functions where we
21802 locate both a member function (with this pointer) and non-member
21803 operator (with explicit first operand). */
21804 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21805 {
21806 len--; /* LEN is the number of significant arguments for DECL1 */
21807 args1 = TREE_CHAIN (args1);
21808 if (!DECL_STATIC_FUNCTION_P (decl2))
21809 args2 = TREE_CHAIN (args2);
21810 }
21811 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21812 {
21813 args2 = TREE_CHAIN (args2);
21814 if (!DECL_STATIC_FUNCTION_P (decl1))
21815 {
21816 len--;
21817 args1 = TREE_CHAIN (args1);
21818 }
21819 }
21820
21821 /* If only one is a conversion operator, they are unordered. */
21822 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21823 return 0;
21824
21825 /* Consider the return type for a conversion function */
21826 if (DECL_CONV_FN_P (decl1))
21827 {
21828 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21829 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21830 len++;
21831 }
21832
21833 processing_template_decl++;
21834
21835 origs1 = args1;
21836 origs2 = args2;
21837
21838 while (len--
21839 /* Stop when an ellipsis is seen. */
21840 && args1 != NULL_TREE && args2 != NULL_TREE)
21841 {
21842 tree arg1 = TREE_VALUE (args1);
21843 tree arg2 = TREE_VALUE (args2);
21844 int deduce1, deduce2;
21845 int quals1 = -1;
21846 int quals2 = -1;
21847 int ref1 = 0;
21848 int ref2 = 0;
21849
21850 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21851 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21852 {
21853 /* When both arguments are pack expansions, we need only
21854 unify the patterns themselves. */
21855 arg1 = PACK_EXPANSION_PATTERN (arg1);
21856 arg2 = PACK_EXPANSION_PATTERN (arg2);
21857
21858 /* This is the last comparison we need to do. */
21859 len = 0;
21860 }
21861
21862 /* DR 1847: If a particular P contains no template-parameters that
21863 participate in template argument deduction, that P is not used to
21864 determine the ordering. */
21865 if (!uses_deducible_template_parms (arg1)
21866 && !uses_deducible_template_parms (arg2))
21867 goto next;
21868
21869 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21870 {
21871 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21872 arg1 = TREE_TYPE (arg1);
21873 quals1 = cp_type_quals (arg1);
21874 }
21875
21876 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21877 {
21878 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21879 arg2 = TREE_TYPE (arg2);
21880 quals2 = cp_type_quals (arg2);
21881 }
21882
21883 arg1 = TYPE_MAIN_VARIANT (arg1);
21884 arg2 = TYPE_MAIN_VARIANT (arg2);
21885
21886 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21887 {
21888 int i, len2 = remaining_arguments (args2);
21889 tree parmvec = make_tree_vec (1);
21890 tree argvec = make_tree_vec (len2);
21891 tree ta = args2;
21892
21893 /* Setup the parameter vector, which contains only ARG1. */
21894 TREE_VEC_ELT (parmvec, 0) = arg1;
21895
21896 /* Setup the argument vector, which contains the remaining
21897 arguments. */
21898 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21899 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21900
21901 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21902 argvec, DEDUCE_EXACT,
21903 /*subr=*/true, /*explain_p=*/false)
21904 == 0);
21905
21906 /* We cannot deduce in the other direction, because ARG1 is
21907 a pack expansion but ARG2 is not. */
21908 deduce2 = 0;
21909 }
21910 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21911 {
21912 int i, len1 = remaining_arguments (args1);
21913 tree parmvec = make_tree_vec (1);
21914 tree argvec = make_tree_vec (len1);
21915 tree ta = args1;
21916
21917 /* Setup the parameter vector, which contains only ARG1. */
21918 TREE_VEC_ELT (parmvec, 0) = arg2;
21919
21920 /* Setup the argument vector, which contains the remaining
21921 arguments. */
21922 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21923 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21924
21925 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21926 argvec, DEDUCE_EXACT,
21927 /*subr=*/true, /*explain_p=*/false)
21928 == 0);
21929
21930 /* We cannot deduce in the other direction, because ARG2 is
21931 a pack expansion but ARG1 is not.*/
21932 deduce1 = 0;
21933 }
21934
21935 else
21936 {
21937 /* The normal case, where neither argument is a pack
21938 expansion. */
21939 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21940 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21941 == 0);
21942 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21943 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21944 == 0);
21945 }
21946
21947 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21948 arg2, then arg2 is not as specialized as arg1. */
21949 if (!deduce1)
21950 lose2 = true;
21951 if (!deduce2)
21952 lose1 = true;
21953
21954 /* "If, for a given type, deduction succeeds in both directions
21955 (i.e., the types are identical after the transformations above)
21956 and both P and A were reference types (before being replaced with
21957 the type referred to above):
21958 - if the type from the argument template was an lvalue reference and
21959 the type from the parameter template was not, the argument type is
21960 considered to be more specialized than the other; otherwise,
21961 - if the type from the argument template is more cv-qualified
21962 than the type from the parameter template (as described above),
21963 the argument type is considered to be more specialized than the other;
21964 otherwise,
21965 - neither type is more specialized than the other." */
21966
21967 if (deduce1 && deduce2)
21968 {
21969 if (ref1 && ref2 && ref1 != ref2)
21970 {
21971 if (ref1 > ref2)
21972 lose1 = true;
21973 else
21974 lose2 = true;
21975 }
21976 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21977 {
21978 if ((quals1 & quals2) == quals2)
21979 lose2 = true;
21980 if ((quals1 & quals2) == quals1)
21981 lose1 = true;
21982 }
21983 }
21984
21985 if (lose1 && lose2)
21986 /* We've failed to deduce something in either direction.
21987 These must be unordered. */
21988 break;
21989
21990 next:
21991
21992 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21993 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21994 /* We have already processed all of the arguments in our
21995 handing of the pack expansion type. */
21996 len = 0;
21997
21998 args1 = TREE_CHAIN (args1);
21999 args2 = TREE_CHAIN (args2);
22000 }
22001
22002 /* "In most cases, all template parameters must have values in order for
22003 deduction to succeed, but for partial ordering purposes a template
22004 parameter may remain without a value provided it is not used in the
22005 types being used for partial ordering."
22006
22007 Thus, if we are missing any of the targs1 we need to substitute into
22008 origs1, then pat2 is not as specialized as pat1. This can happen when
22009 there is a nondeduced context. */
22010 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
22011 lose2 = true;
22012 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
22013 lose1 = true;
22014
22015 processing_template_decl--;
22016
22017 /* If both deductions succeed, the partial ordering selects the more
22018 constrained template. */
22019 if (!lose1 && !lose2)
22020 {
22021 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
22022 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
22023 lose1 = !subsumes_constraints (c1, c2);
22024 lose2 = !subsumes_constraints (c2, c1);
22025 }
22026
22027 /* All things being equal, if the next argument is a pack expansion
22028 for one function but not for the other, prefer the
22029 non-variadic function. FIXME this is bogus; see c++/41958. */
22030 if (lose1 == lose2
22031 && args1 && TREE_VALUE (args1)
22032 && args2 && TREE_VALUE (args2))
22033 {
22034 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
22035 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
22036 }
22037
22038 if (lose1 == lose2)
22039 return 0;
22040 else if (!lose1)
22041 return 1;
22042 else
22043 return -1;
22044 }
22045
22046 /* Determine which of two partial specializations of TMPL is more
22047 specialized.
22048
22049 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22050 to the first partial specialization. The TREE_PURPOSE is the
22051 innermost set of template parameters for the partial
22052 specialization. PAT2 is similar, but for the second template.
22053
22054 Return 1 if the first partial specialization is more specialized;
22055 -1 if the second is more specialized; 0 if neither is more
22056 specialized.
22057
22058 See [temp.class.order] for information about determining which of
22059 two templates is more specialized. */
22060
22061 static int
22062 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
22063 {
22064 tree targs;
22065 int winner = 0;
22066 bool any_deductions = false;
22067
22068 tree tmpl1 = TREE_VALUE (pat1);
22069 tree tmpl2 = TREE_VALUE (pat2);
22070 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
22071 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
22072
22073 /* Just like what happens for functions, if we are ordering between
22074 different template specializations, we may encounter dependent
22075 types in the arguments, and we need our dependency check functions
22076 to behave correctly. */
22077 ++processing_template_decl;
22078 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
22079 if (targs)
22080 {
22081 --winner;
22082 any_deductions = true;
22083 }
22084
22085 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
22086 if (targs)
22087 {
22088 ++winner;
22089 any_deductions = true;
22090 }
22091 --processing_template_decl;
22092
22093 /* If both deductions succeed, the partial ordering selects the more
22094 constrained template. */
22095 if (!winner && any_deductions)
22096 return more_constrained (tmpl1, tmpl2);
22097
22098 /* In the case of a tie where at least one of the templates
22099 has a parameter pack at the end, the template with the most
22100 non-packed parameters wins. */
22101 if (winner == 0
22102 && any_deductions
22103 && (template_args_variadic_p (TREE_PURPOSE (pat1))
22104 || template_args_variadic_p (TREE_PURPOSE (pat2))))
22105 {
22106 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
22107 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
22108 int len1 = TREE_VEC_LENGTH (args1);
22109 int len2 = TREE_VEC_LENGTH (args2);
22110
22111 /* We don't count the pack expansion at the end. */
22112 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
22113 --len1;
22114 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
22115 --len2;
22116
22117 if (len1 > len2)
22118 return 1;
22119 else if (len1 < len2)
22120 return -1;
22121 }
22122
22123 return winner;
22124 }
22125
22126 /* Return the template arguments that will produce the function signature
22127 DECL from the function template FN, with the explicit template
22128 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22129 also match. Return NULL_TREE if no satisfactory arguments could be
22130 found. */
22131
22132 static tree
22133 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
22134 {
22135 int ntparms = DECL_NTPARMS (fn);
22136 tree targs = make_tree_vec (ntparms);
22137 tree decl_type = TREE_TYPE (decl);
22138 tree decl_arg_types;
22139 tree *args;
22140 unsigned int nargs, ix;
22141 tree arg;
22142
22143 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
22144
22145 /* Never do unification on the 'this' parameter. */
22146 decl_arg_types = skip_artificial_parms_for (decl,
22147 TYPE_ARG_TYPES (decl_type));
22148
22149 nargs = list_length (decl_arg_types);
22150 args = XALLOCAVEC (tree, nargs);
22151 for (arg = decl_arg_types, ix = 0;
22152 arg != NULL_TREE && arg != void_list_node;
22153 arg = TREE_CHAIN (arg), ++ix)
22154 args[ix] = TREE_VALUE (arg);
22155
22156 if (fn_type_unification (fn, explicit_args, targs,
22157 args, ix,
22158 (check_rettype || DECL_CONV_FN_P (fn)
22159 ? TREE_TYPE (decl_type) : NULL_TREE),
22160 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
22161 /*decltype*/false)
22162 == error_mark_node)
22163 return NULL_TREE;
22164
22165 return targs;
22166 }
22167
22168 /* Return the innermost template arguments that, when applied to a partial
22169 specialization SPEC_TMPL of TMPL, yield the ARGS.
22170
22171 For example, suppose we have:
22172
22173 template <class T, class U> struct S {};
22174 template <class T> struct S<T*, int> {};
22175
22176 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22177 partial specialization and the ARGS will be {double*, int}. The resulting
22178 vector will be {double}, indicating that `T' is bound to `double'. */
22179
22180 static tree
22181 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
22182 {
22183 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
22184 tree spec_args
22185 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
22186 int i, ntparms = TREE_VEC_LENGTH (tparms);
22187 tree deduced_args;
22188 tree innermost_deduced_args;
22189
22190 innermost_deduced_args = make_tree_vec (ntparms);
22191 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22192 {
22193 deduced_args = copy_node (args);
22194 SET_TMPL_ARGS_LEVEL (deduced_args,
22195 TMPL_ARGS_DEPTH (deduced_args),
22196 innermost_deduced_args);
22197 }
22198 else
22199 deduced_args = innermost_deduced_args;
22200
22201 bool tried_array_deduction = (cxx_dialect < cxx17);
22202 again:
22203 if (unify (tparms, deduced_args,
22204 INNERMOST_TEMPLATE_ARGS (spec_args),
22205 INNERMOST_TEMPLATE_ARGS (args),
22206 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22207 return NULL_TREE;
22208
22209 for (i = 0; i < ntparms; ++i)
22210 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22211 {
22212 if (!tried_array_deduction)
22213 {
22214 try_array_deduction (tparms, innermost_deduced_args,
22215 INNERMOST_TEMPLATE_ARGS (spec_args));
22216 tried_array_deduction = true;
22217 if (TREE_VEC_ELT (innermost_deduced_args, i))
22218 goto again;
22219 }
22220 return NULL_TREE;
22221 }
22222
22223 tree tinst = build_tree_list (spec_tmpl, deduced_args);
22224 if (!push_tinst_level (tinst))
22225 {
22226 excessive_deduction_depth = true;
22227 return NULL_TREE;
22228 }
22229
22230 /* Verify that nondeduced template arguments agree with the type
22231 obtained from argument deduction.
22232
22233 For example:
22234
22235 struct A { typedef int X; };
22236 template <class T, class U> struct C {};
22237 template <class T> struct C<T, typename T::X> {};
22238
22239 Then with the instantiation `C<A, int>', we can deduce that
22240 `T' is `A' but unify () does not check whether `typename T::X'
22241 is `int'. */
22242 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22243
22244 if (spec_args != error_mark_node)
22245 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22246 INNERMOST_TEMPLATE_ARGS (spec_args),
22247 tmpl, tf_none, false, false);
22248
22249 pop_tinst_level ();
22250
22251 if (spec_args == error_mark_node
22252 /* We only need to check the innermost arguments; the other
22253 arguments will always agree. */
22254 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22255 INNERMOST_TEMPLATE_ARGS (args)))
22256 return NULL_TREE;
22257
22258 /* Now that we have bindings for all of the template arguments,
22259 ensure that the arguments deduced for the template template
22260 parameters have compatible template parameter lists. See the use
22261 of template_template_parm_bindings_ok_p in fn_type_unification
22262 for more information. */
22263 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22264 return NULL_TREE;
22265
22266 return deduced_args;
22267 }
22268
22269 // Compare two function templates T1 and T2 by deducing bindings
22270 // from one against the other. If both deductions succeed, compare
22271 // constraints to see which is more constrained.
22272 static int
22273 more_specialized_inst (tree t1, tree t2)
22274 {
22275 int fate = 0;
22276 int count = 0;
22277
22278 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22279 {
22280 --fate;
22281 ++count;
22282 }
22283
22284 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22285 {
22286 ++fate;
22287 ++count;
22288 }
22289
22290 // If both deductions succeed, then one may be more constrained.
22291 if (count == 2 && fate == 0)
22292 fate = more_constrained (t1, t2);
22293
22294 return fate;
22295 }
22296
22297 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22298 Return the TREE_LIST node with the most specialized template, if
22299 any. If there is no most specialized template, the error_mark_node
22300 is returned.
22301
22302 Note that this function does not look at, or modify, the
22303 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22304 returned is one of the elements of INSTANTIATIONS, callers may
22305 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22306 and retrieve it from the value returned. */
22307
22308 tree
22309 most_specialized_instantiation (tree templates)
22310 {
22311 tree fn, champ;
22312
22313 ++processing_template_decl;
22314
22315 champ = templates;
22316 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22317 {
22318 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22319 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22320 if (fate == -1)
22321 champ = fn;
22322 else if (!fate)
22323 {
22324 /* Equally specialized, move to next function. If there
22325 is no next function, nothing's most specialized. */
22326 fn = TREE_CHAIN (fn);
22327 champ = fn;
22328 if (!fn)
22329 break;
22330 }
22331 }
22332
22333 if (champ)
22334 /* Now verify that champ is better than everything earlier in the
22335 instantiation list. */
22336 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22337 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22338 {
22339 champ = NULL_TREE;
22340 break;
22341 }
22342 }
22343
22344 processing_template_decl--;
22345
22346 if (!champ)
22347 return error_mark_node;
22348
22349 return champ;
22350 }
22351
22352 /* If DECL is a specialization of some template, return the most
22353 general such template. Otherwise, returns NULL_TREE.
22354
22355 For example, given:
22356
22357 template <class T> struct S { template <class U> void f(U); };
22358
22359 if TMPL is `template <class U> void S<int>::f(U)' this will return
22360 the full template. This function will not trace past partial
22361 specializations, however. For example, given in addition:
22362
22363 template <class T> struct S<T*> { template <class U> void f(U); };
22364
22365 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22366 `template <class T> template <class U> S<T*>::f(U)'. */
22367
22368 tree
22369 most_general_template (tree decl)
22370 {
22371 if (TREE_CODE (decl) != TEMPLATE_DECL)
22372 {
22373 if (tree tinfo = get_template_info (decl))
22374 decl = TI_TEMPLATE (tinfo);
22375 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22376 template friend, or a FIELD_DECL for a capture pack. */
22377 if (TREE_CODE (decl) != TEMPLATE_DECL)
22378 return NULL_TREE;
22379 }
22380
22381 /* Look for more and more general templates. */
22382 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22383 {
22384 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22385 (See cp-tree.h for details.) */
22386 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22387 break;
22388
22389 if (CLASS_TYPE_P (TREE_TYPE (decl))
22390 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22391 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22392 break;
22393
22394 /* Stop if we run into an explicitly specialized class template. */
22395 if (!DECL_NAMESPACE_SCOPE_P (decl)
22396 && DECL_CONTEXT (decl)
22397 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22398 break;
22399
22400 decl = DECL_TI_TEMPLATE (decl);
22401 }
22402
22403 return decl;
22404 }
22405
22406 /* Return the most specialized of the template partial specializations
22407 which can produce TARGET, a specialization of some class or variable
22408 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22409 a TEMPLATE_DECL node corresponding to the partial specialization, while
22410 the TREE_PURPOSE is the set of template arguments that must be
22411 substituted into the template pattern in order to generate TARGET.
22412
22413 If the choice of partial specialization is ambiguous, a diagnostic
22414 is issued, and the error_mark_node is returned. If there are no
22415 partial specializations matching TARGET, then NULL_TREE is
22416 returned, indicating that the primary template should be used. */
22417
22418 static tree
22419 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22420 {
22421 tree list = NULL_TREE;
22422 tree t;
22423 tree champ;
22424 int fate;
22425 bool ambiguous_p;
22426 tree outer_args = NULL_TREE;
22427 tree tmpl, args;
22428
22429 if (TYPE_P (target))
22430 {
22431 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22432 tmpl = TI_TEMPLATE (tinfo);
22433 args = TI_ARGS (tinfo);
22434 }
22435 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22436 {
22437 tmpl = TREE_OPERAND (target, 0);
22438 args = TREE_OPERAND (target, 1);
22439 }
22440 else if (VAR_P (target))
22441 {
22442 tree tinfo = DECL_TEMPLATE_INFO (target);
22443 tmpl = TI_TEMPLATE (tinfo);
22444 args = TI_ARGS (tinfo);
22445 }
22446 else
22447 gcc_unreachable ();
22448
22449 tree main_tmpl = most_general_template (tmpl);
22450
22451 /* For determining which partial specialization to use, only the
22452 innermost args are interesting. */
22453 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22454 {
22455 outer_args = strip_innermost_template_args (args, 1);
22456 args = INNERMOST_TEMPLATE_ARGS (args);
22457 }
22458
22459 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22460 {
22461 tree spec_args;
22462 tree spec_tmpl = TREE_VALUE (t);
22463
22464 if (outer_args)
22465 {
22466 /* Substitute in the template args from the enclosing class. */
22467 ++processing_template_decl;
22468 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22469 --processing_template_decl;
22470 }
22471
22472 if (spec_tmpl == error_mark_node)
22473 return error_mark_node;
22474
22475 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22476 if (spec_args)
22477 {
22478 if (outer_args)
22479 spec_args = add_to_template_args (outer_args, spec_args);
22480
22481 /* Keep the candidate only if the constraints are satisfied,
22482 or if we're not compiling with concepts. */
22483 if (!flag_concepts
22484 || constraints_satisfied_p (spec_tmpl, spec_args))
22485 {
22486 list = tree_cons (spec_args, TREE_VALUE (t), list);
22487 TREE_TYPE (list) = TREE_TYPE (t);
22488 }
22489 }
22490 }
22491
22492 if (! list)
22493 return NULL_TREE;
22494
22495 ambiguous_p = false;
22496 t = list;
22497 champ = t;
22498 t = TREE_CHAIN (t);
22499 for (; t; t = TREE_CHAIN (t))
22500 {
22501 fate = more_specialized_partial_spec (tmpl, champ, t);
22502 if (fate == 1)
22503 ;
22504 else
22505 {
22506 if (fate == 0)
22507 {
22508 t = TREE_CHAIN (t);
22509 if (! t)
22510 {
22511 ambiguous_p = true;
22512 break;
22513 }
22514 }
22515 champ = t;
22516 }
22517 }
22518
22519 if (!ambiguous_p)
22520 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22521 {
22522 fate = more_specialized_partial_spec (tmpl, champ, t);
22523 if (fate != 1)
22524 {
22525 ambiguous_p = true;
22526 break;
22527 }
22528 }
22529
22530 if (ambiguous_p)
22531 {
22532 const char *str;
22533 char *spaces = NULL;
22534 if (!(complain & tf_error))
22535 return error_mark_node;
22536 if (TYPE_P (target))
22537 error ("ambiguous template instantiation for %q#T", target);
22538 else
22539 error ("ambiguous template instantiation for %q#D", target);
22540 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22541 for (t = list; t; t = TREE_CHAIN (t))
22542 {
22543 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22544 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22545 "%s %#qS", spaces ? spaces : str, subst);
22546 spaces = spaces ? spaces : get_spaces (str);
22547 }
22548 free (spaces);
22549 return error_mark_node;
22550 }
22551
22552 return champ;
22553 }
22554
22555 /* Explicitly instantiate DECL. */
22556
22557 void
22558 do_decl_instantiation (tree decl, tree storage)
22559 {
22560 tree result = NULL_TREE;
22561 int extern_p = 0;
22562
22563 if (!decl || decl == error_mark_node)
22564 /* An error occurred, for which grokdeclarator has already issued
22565 an appropriate message. */
22566 return;
22567 else if (! DECL_LANG_SPECIFIC (decl))
22568 {
22569 error ("explicit instantiation of non-template %q#D", decl);
22570 return;
22571 }
22572
22573 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22574 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22575
22576 if (VAR_P (decl) && !var_templ)
22577 {
22578 /* There is an asymmetry here in the way VAR_DECLs and
22579 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22580 the latter, the DECL we get back will be marked as a
22581 template instantiation, and the appropriate
22582 DECL_TEMPLATE_INFO will be set up. This does not happen for
22583 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22584 should handle VAR_DECLs as it currently handles
22585 FUNCTION_DECLs. */
22586 if (!DECL_CLASS_SCOPE_P (decl))
22587 {
22588 error ("%qD is not a static data member of a class template", decl);
22589 return;
22590 }
22591 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22592 if (!result || !VAR_P (result))
22593 {
22594 error ("no matching template for %qD found", decl);
22595 return;
22596 }
22597 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22598 {
22599 error ("type %qT for explicit instantiation %qD does not match "
22600 "declared type %qT", TREE_TYPE (result), decl,
22601 TREE_TYPE (decl));
22602 return;
22603 }
22604 }
22605 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22606 {
22607 error ("explicit instantiation of %q#D", decl);
22608 return;
22609 }
22610 else
22611 result = decl;
22612
22613 /* Check for various error cases. Note that if the explicit
22614 instantiation is valid the RESULT will currently be marked as an
22615 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22616 until we get here. */
22617
22618 if (DECL_TEMPLATE_SPECIALIZATION (result))
22619 {
22620 /* DR 259 [temp.spec].
22621
22622 Both an explicit instantiation and a declaration of an explicit
22623 specialization shall not appear in a program unless the explicit
22624 instantiation follows a declaration of the explicit specialization.
22625
22626 For a given set of template parameters, if an explicit
22627 instantiation of a template appears after a declaration of an
22628 explicit specialization for that template, the explicit
22629 instantiation has no effect. */
22630 return;
22631 }
22632 else if (DECL_EXPLICIT_INSTANTIATION (result))
22633 {
22634 /* [temp.spec]
22635
22636 No program shall explicitly instantiate any template more
22637 than once.
22638
22639 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22640 the first instantiation was `extern' and the second is not,
22641 and EXTERN_P for the opposite case. */
22642 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22643 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22644 /* If an "extern" explicit instantiation follows an ordinary
22645 explicit instantiation, the template is instantiated. */
22646 if (extern_p)
22647 return;
22648 }
22649 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22650 {
22651 error ("no matching template for %qD found", result);
22652 return;
22653 }
22654 else if (!DECL_TEMPLATE_INFO (result))
22655 {
22656 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22657 return;
22658 }
22659
22660 if (storage == NULL_TREE)
22661 ;
22662 else if (storage == ridpointers[(int) RID_EXTERN])
22663 {
22664 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22665 pedwarn (input_location, OPT_Wpedantic,
22666 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22667 "instantiations");
22668 extern_p = 1;
22669 }
22670 else
22671 error ("storage class %qD applied to template instantiation", storage);
22672
22673 check_explicit_instantiation_namespace (result);
22674 mark_decl_instantiated (result, extern_p);
22675 if (! extern_p)
22676 instantiate_decl (result, /*defer_ok=*/true,
22677 /*expl_inst_class_mem_p=*/false);
22678 }
22679
22680 static void
22681 mark_class_instantiated (tree t, int extern_p)
22682 {
22683 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22684 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22685 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22686 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22687 if (! extern_p)
22688 {
22689 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22690 rest_of_type_compilation (t, 1);
22691 }
22692 }
22693
22694 /* Called from do_type_instantiation through binding_table_foreach to
22695 do recursive instantiation for the type bound in ENTRY. */
22696 static void
22697 bt_instantiate_type_proc (binding_entry entry, void *data)
22698 {
22699 tree storage = *(tree *) data;
22700
22701 if (MAYBE_CLASS_TYPE_P (entry->type)
22702 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22703 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22704 }
22705
22706 /* Perform an explicit instantiation of template class T. STORAGE, if
22707 non-null, is the RID for extern, inline or static. COMPLAIN is
22708 nonzero if this is called from the parser, zero if called recursively,
22709 since the standard is unclear (as detailed below). */
22710
22711 void
22712 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22713 {
22714 int extern_p = 0;
22715 int nomem_p = 0;
22716 int static_p = 0;
22717 int previous_instantiation_extern_p = 0;
22718
22719 if (TREE_CODE (t) == TYPE_DECL)
22720 t = TREE_TYPE (t);
22721
22722 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22723 {
22724 tree tmpl =
22725 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22726 if (tmpl)
22727 error ("explicit instantiation of non-class template %qD", tmpl);
22728 else
22729 error ("explicit instantiation of non-template type %qT", t);
22730 return;
22731 }
22732
22733 complete_type (t);
22734
22735 if (!COMPLETE_TYPE_P (t))
22736 {
22737 if (complain & tf_error)
22738 error ("explicit instantiation of %q#T before definition of template",
22739 t);
22740 return;
22741 }
22742
22743 if (storage != NULL_TREE)
22744 {
22745 if (!in_system_header_at (input_location))
22746 {
22747 if (storage == ridpointers[(int) RID_EXTERN])
22748 {
22749 if (cxx_dialect == cxx98)
22750 pedwarn (input_location, OPT_Wpedantic,
22751 "ISO C++ 1998 forbids the use of %<extern%> on "
22752 "explicit instantiations");
22753 }
22754 else
22755 pedwarn (input_location, OPT_Wpedantic,
22756 "ISO C++ forbids the use of %qE"
22757 " on explicit instantiations", storage);
22758 }
22759
22760 if (storage == ridpointers[(int) RID_INLINE])
22761 nomem_p = 1;
22762 else if (storage == ridpointers[(int) RID_EXTERN])
22763 extern_p = 1;
22764 else if (storage == ridpointers[(int) RID_STATIC])
22765 static_p = 1;
22766 else
22767 {
22768 error ("storage class %qD applied to template instantiation",
22769 storage);
22770 extern_p = 0;
22771 }
22772 }
22773
22774 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22775 {
22776 /* DR 259 [temp.spec].
22777
22778 Both an explicit instantiation and a declaration of an explicit
22779 specialization shall not appear in a program unless the explicit
22780 instantiation follows a declaration of the explicit specialization.
22781
22782 For a given set of template parameters, if an explicit
22783 instantiation of a template appears after a declaration of an
22784 explicit specialization for that template, the explicit
22785 instantiation has no effect. */
22786 return;
22787 }
22788 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22789 {
22790 /* [temp.spec]
22791
22792 No program shall explicitly instantiate any template more
22793 than once.
22794
22795 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22796 instantiation was `extern'. If EXTERN_P then the second is.
22797 These cases are OK. */
22798 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22799
22800 if (!previous_instantiation_extern_p && !extern_p
22801 && (complain & tf_error))
22802 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22803
22804 /* If we've already instantiated the template, just return now. */
22805 if (!CLASSTYPE_INTERFACE_ONLY (t))
22806 return;
22807 }
22808
22809 check_explicit_instantiation_namespace (TYPE_NAME (t));
22810 mark_class_instantiated (t, extern_p);
22811
22812 if (nomem_p)
22813 return;
22814
22815 /* In contrast to implicit instantiation, where only the
22816 declarations, and not the definitions, of members are
22817 instantiated, we have here:
22818
22819 [temp.explicit]
22820
22821 The explicit instantiation of a class template specialization
22822 implies the instantiation of all of its members not
22823 previously explicitly specialized in the translation unit
22824 containing the explicit instantiation.
22825
22826 Of course, we can't instantiate member template classes, since we
22827 don't have any arguments for them. Note that the standard is
22828 unclear on whether the instantiation of the members are
22829 *explicit* instantiations or not. However, the most natural
22830 interpretation is that it should be an explicit
22831 instantiation. */
22832 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22833 if ((VAR_P (fld)
22834 || (TREE_CODE (fld) == FUNCTION_DECL
22835 && !static_p
22836 && user_provided_p (fld)))
22837 && DECL_TEMPLATE_INSTANTIATION (fld))
22838 {
22839 mark_decl_instantiated (fld, extern_p);
22840 if (! extern_p)
22841 instantiate_decl (fld, /*defer_ok=*/true,
22842 /*expl_inst_class_mem_p=*/true);
22843 }
22844
22845 if (CLASSTYPE_NESTED_UTDS (t))
22846 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22847 bt_instantiate_type_proc, &storage);
22848 }
22849
22850 /* Given a function DECL, which is a specialization of TMPL, modify
22851 DECL to be a re-instantiation of TMPL with the same template
22852 arguments. TMPL should be the template into which tsubst'ing
22853 should occur for DECL, not the most general template.
22854
22855 One reason for doing this is a scenario like this:
22856
22857 template <class T>
22858 void f(const T&, int i);
22859
22860 void g() { f(3, 7); }
22861
22862 template <class T>
22863 void f(const T& t, const int i) { }
22864
22865 Note that when the template is first instantiated, with
22866 instantiate_template, the resulting DECL will have no name for the
22867 first parameter, and the wrong type for the second. So, when we go
22868 to instantiate the DECL, we regenerate it. */
22869
22870 static void
22871 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22872 {
22873 /* The arguments used to instantiate DECL, from the most general
22874 template. */
22875 tree code_pattern;
22876
22877 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22878
22879 /* Make sure that we can see identifiers, and compute access
22880 correctly. */
22881 push_access_scope (decl);
22882
22883 if (TREE_CODE (decl) == FUNCTION_DECL)
22884 {
22885 tree decl_parm;
22886 tree pattern_parm;
22887 tree specs;
22888 int args_depth;
22889 int parms_depth;
22890
22891 args_depth = TMPL_ARGS_DEPTH (args);
22892 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22893 if (args_depth > parms_depth)
22894 args = get_innermost_template_args (args, parms_depth);
22895
22896 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22897 args, tf_error, NULL_TREE,
22898 /*defer_ok*/false);
22899 if (specs && specs != error_mark_node)
22900 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22901 specs);
22902
22903 /* Merge parameter declarations. */
22904 decl_parm = skip_artificial_parms_for (decl,
22905 DECL_ARGUMENTS (decl));
22906 pattern_parm
22907 = skip_artificial_parms_for (code_pattern,
22908 DECL_ARGUMENTS (code_pattern));
22909 while (decl_parm && !DECL_PACK_P (pattern_parm))
22910 {
22911 tree parm_type;
22912 tree attributes;
22913
22914 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22915 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22916 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22917 NULL_TREE);
22918 parm_type = type_decays_to (parm_type);
22919 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22920 TREE_TYPE (decl_parm) = parm_type;
22921 attributes = DECL_ATTRIBUTES (pattern_parm);
22922 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22923 {
22924 DECL_ATTRIBUTES (decl_parm) = attributes;
22925 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22926 }
22927 decl_parm = DECL_CHAIN (decl_parm);
22928 pattern_parm = DECL_CHAIN (pattern_parm);
22929 }
22930 /* Merge any parameters that match with the function parameter
22931 pack. */
22932 if (pattern_parm && DECL_PACK_P (pattern_parm))
22933 {
22934 int i, len;
22935 tree expanded_types;
22936 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22937 the parameters in this function parameter pack. */
22938 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22939 args, tf_error, NULL_TREE);
22940 len = TREE_VEC_LENGTH (expanded_types);
22941 for (i = 0; i < len; i++)
22942 {
22943 tree parm_type;
22944 tree attributes;
22945
22946 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22947 /* Rename the parameter to include the index. */
22948 DECL_NAME (decl_parm) =
22949 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22950 parm_type = TREE_VEC_ELT (expanded_types, i);
22951 parm_type = type_decays_to (parm_type);
22952 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22953 TREE_TYPE (decl_parm) = parm_type;
22954 attributes = DECL_ATTRIBUTES (pattern_parm);
22955 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22956 {
22957 DECL_ATTRIBUTES (decl_parm) = attributes;
22958 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22959 }
22960 decl_parm = DECL_CHAIN (decl_parm);
22961 }
22962 }
22963 /* Merge additional specifiers from the CODE_PATTERN. */
22964 if (DECL_DECLARED_INLINE_P (code_pattern)
22965 && !DECL_DECLARED_INLINE_P (decl))
22966 DECL_DECLARED_INLINE_P (decl) = 1;
22967 }
22968 else if (VAR_P (decl))
22969 {
22970 start_lambda_scope (decl);
22971 DECL_INITIAL (decl) =
22972 tsubst_expr (DECL_INITIAL (code_pattern), args,
22973 tf_error, DECL_TI_TEMPLATE (decl),
22974 /*integral_constant_expression_p=*/false);
22975 finish_lambda_scope ();
22976 if (VAR_HAD_UNKNOWN_BOUND (decl))
22977 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22978 tf_error, DECL_TI_TEMPLATE (decl));
22979 }
22980 else
22981 gcc_unreachable ();
22982
22983 pop_access_scope (decl);
22984 }
22985
22986 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22987 substituted to get DECL. */
22988
22989 tree
22990 template_for_substitution (tree decl)
22991 {
22992 tree tmpl = DECL_TI_TEMPLATE (decl);
22993
22994 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22995 for the instantiation. This is not always the most general
22996 template. Consider, for example:
22997
22998 template <class T>
22999 struct S { template <class U> void f();
23000 template <> void f<int>(); };
23001
23002 and an instantiation of S<double>::f<int>. We want TD to be the
23003 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23004 while (/* An instantiation cannot have a definition, so we need a
23005 more general template. */
23006 DECL_TEMPLATE_INSTANTIATION (tmpl)
23007 /* We must also deal with friend templates. Given:
23008
23009 template <class T> struct S {
23010 template <class U> friend void f() {};
23011 };
23012
23013 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23014 so far as the language is concerned, but that's still
23015 where we get the pattern for the instantiation from. On
23016 other hand, if the definition comes outside the class, say:
23017
23018 template <class T> struct S {
23019 template <class U> friend void f();
23020 };
23021 template <class U> friend void f() {}
23022
23023 we don't need to look any further. That's what the check for
23024 DECL_INITIAL is for. */
23025 || (TREE_CODE (decl) == FUNCTION_DECL
23026 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
23027 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
23028 {
23029 /* The present template, TD, should not be a definition. If it
23030 were a definition, we should be using it! Note that we
23031 cannot restructure the loop to just keep going until we find
23032 a template with a definition, since that might go too far if
23033 a specialization was declared, but not defined. */
23034
23035 /* Fetch the more general template. */
23036 tmpl = DECL_TI_TEMPLATE (tmpl);
23037 }
23038
23039 return tmpl;
23040 }
23041
23042 /* Returns true if we need to instantiate this template instance even if we
23043 know we aren't going to emit it. */
23044
23045 bool
23046 always_instantiate_p (tree decl)
23047 {
23048 /* We always instantiate inline functions so that we can inline them. An
23049 explicit instantiation declaration prohibits implicit instantiation of
23050 non-inline functions. With high levels of optimization, we would
23051 normally inline non-inline functions -- but we're not allowed to do
23052 that for "extern template" functions. Therefore, we check
23053 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23054 return ((TREE_CODE (decl) == FUNCTION_DECL
23055 && (DECL_DECLARED_INLINE_P (decl)
23056 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
23057 /* And we need to instantiate static data members so that
23058 their initializers are available in integral constant
23059 expressions. */
23060 || (VAR_P (decl)
23061 && decl_maybe_constant_var_p (decl)));
23062 }
23063
23064 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23065 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23066 error, true otherwise. */
23067
23068 bool
23069 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
23070 {
23071 tree fntype, spec, noex, clone;
23072
23073 /* Don't instantiate a noexcept-specification from template context. */
23074 if (processing_template_decl)
23075 return true;
23076
23077 if (DECL_CLONED_FUNCTION_P (fn))
23078 fn = DECL_CLONED_FUNCTION (fn);
23079 fntype = TREE_TYPE (fn);
23080 spec = TYPE_RAISES_EXCEPTIONS (fntype);
23081
23082 if (!spec || !TREE_PURPOSE (spec))
23083 return true;
23084
23085 noex = TREE_PURPOSE (spec);
23086
23087 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
23088 {
23089 static hash_set<tree>* fns = new hash_set<tree>;
23090 bool added = false;
23091 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
23092 spec = get_defaulted_eh_spec (fn, complain);
23093 else if (!(added = !fns->add (fn)))
23094 {
23095 /* If hash_set::add returns true, the element was already there. */
23096 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
23097 DECL_SOURCE_LOCATION (fn));
23098 error_at (loc,
23099 "exception specification of %qD depends on itself",
23100 fn);
23101 spec = noexcept_false_spec;
23102 }
23103 else if (push_tinst_level (fn))
23104 {
23105 push_access_scope (fn);
23106 push_deferring_access_checks (dk_no_deferred);
23107 input_location = DECL_SOURCE_LOCATION (fn);
23108 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
23109 DEFERRED_NOEXCEPT_ARGS (noex),
23110 tf_warning_or_error, fn,
23111 /*function_p=*/false,
23112 /*integral_constant_expression_p=*/true);
23113 pop_deferring_access_checks ();
23114 pop_access_scope (fn);
23115 pop_tinst_level ();
23116 spec = build_noexcept_spec (noex, tf_warning_or_error);
23117 if (spec == error_mark_node)
23118 spec = noexcept_false_spec;
23119 }
23120 else
23121 spec = noexcept_false_spec;
23122
23123 if (added)
23124 fns->remove (fn);
23125
23126 if (spec == error_mark_node)
23127 return false;
23128
23129 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
23130 }
23131
23132 FOR_EACH_CLONE (clone, fn)
23133 {
23134 if (TREE_TYPE (clone) == fntype)
23135 TREE_TYPE (clone) = TREE_TYPE (fn);
23136 else
23137 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
23138 }
23139
23140 return true;
23141 }
23142
23143 /* We're starting to process the function INST, an instantiation of PATTERN;
23144 add their parameters to local_specializations. */
23145
23146 static void
23147 register_parameter_specializations (tree pattern, tree inst)
23148 {
23149 tree tmpl_parm = DECL_ARGUMENTS (pattern);
23150 tree spec_parm = DECL_ARGUMENTS (inst);
23151 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
23152 {
23153 register_local_specialization (spec_parm, tmpl_parm);
23154 spec_parm = skip_artificial_parms_for (inst, spec_parm);
23155 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
23156 }
23157 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23158 {
23159 if (!DECL_PACK_P (tmpl_parm))
23160 {
23161 register_local_specialization (spec_parm, tmpl_parm);
23162 spec_parm = DECL_CHAIN (spec_parm);
23163 }
23164 else
23165 {
23166 /* Register the (value) argument pack as a specialization of
23167 TMPL_PARM, then move on. */
23168 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23169 register_local_specialization (argpack, tmpl_parm);
23170 }
23171 }
23172 gcc_assert (!spec_parm);
23173 }
23174
23175 /* Produce the definition of D, a _DECL generated from a template. If
23176 DEFER_OK is true, then we don't have to actually do the
23177 instantiation now; we just have to do it sometime. Normally it is
23178 an error if this is an explicit instantiation but D is undefined.
23179 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23180 instantiated class template. */
23181
23182 tree
23183 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
23184 {
23185 tree tmpl = DECL_TI_TEMPLATE (d);
23186 tree gen_args;
23187 tree args;
23188 tree td;
23189 tree code_pattern;
23190 tree spec;
23191 tree gen_tmpl;
23192 bool pattern_defined;
23193 location_t saved_loc = input_location;
23194 int saved_unevaluated_operand = cp_unevaluated_operand;
23195 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23196 bool external_p;
23197 bool deleted_p;
23198
23199 /* This function should only be used to instantiate templates for
23200 functions and static member variables. */
23201 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23202
23203 /* A concept is never instantiated. */
23204 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23205
23206 /* Variables are never deferred; if instantiation is required, they
23207 are instantiated right away. That allows for better code in the
23208 case that an expression refers to the value of the variable --
23209 if the variable has a constant value the referring expression can
23210 take advantage of that fact. */
23211 if (VAR_P (d))
23212 defer_ok = false;
23213
23214 /* Don't instantiate cloned functions. Instead, instantiate the
23215 functions they cloned. */
23216 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23217 d = DECL_CLONED_FUNCTION (d);
23218
23219 if (DECL_TEMPLATE_INSTANTIATED (d)
23220 || (TREE_CODE (d) == FUNCTION_DECL
23221 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23222 || DECL_TEMPLATE_SPECIALIZATION (d))
23223 /* D has already been instantiated or explicitly specialized, so
23224 there's nothing for us to do here.
23225
23226 It might seem reasonable to check whether or not D is an explicit
23227 instantiation, and, if so, stop here. But when an explicit
23228 instantiation is deferred until the end of the compilation,
23229 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23230 the instantiation. */
23231 return d;
23232
23233 /* Check to see whether we know that this template will be
23234 instantiated in some other file, as with "extern template"
23235 extension. */
23236 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23237
23238 /* In general, we do not instantiate such templates. */
23239 if (external_p && !always_instantiate_p (d))
23240 return d;
23241
23242 gen_tmpl = most_general_template (tmpl);
23243 gen_args = DECL_TI_ARGS (d);
23244
23245 if (tmpl != gen_tmpl)
23246 /* We should already have the extra args. */
23247 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23248 == TMPL_ARGS_DEPTH (gen_args));
23249 /* And what's in the hash table should match D. */
23250 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23251 || spec == NULL_TREE);
23252
23253 /* This needs to happen before any tsubsting. */
23254 if (! push_tinst_level (d))
23255 return d;
23256
23257 timevar_push (TV_TEMPLATE_INST);
23258
23259 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23260 for the instantiation. */
23261 td = template_for_substitution (d);
23262 args = gen_args;
23263
23264 if (VAR_P (d))
23265 {
23266 /* Look up an explicit specialization, if any. */
23267 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23268 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23269 if (elt && elt != error_mark_node)
23270 {
23271 td = TREE_VALUE (elt);
23272 args = TREE_PURPOSE (elt);
23273 }
23274 }
23275
23276 code_pattern = DECL_TEMPLATE_RESULT (td);
23277
23278 /* We should never be trying to instantiate a member of a class
23279 template or partial specialization. */
23280 gcc_assert (d != code_pattern);
23281
23282 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23283 || DECL_TEMPLATE_SPECIALIZATION (td))
23284 /* In the case of a friend template whose definition is provided
23285 outside the class, we may have too many arguments. Drop the
23286 ones we don't need. The same is true for specializations. */
23287 args = get_innermost_template_args
23288 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23289
23290 if (TREE_CODE (d) == FUNCTION_DECL)
23291 {
23292 deleted_p = DECL_DELETED_FN (code_pattern);
23293 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23294 && DECL_INITIAL (code_pattern) != error_mark_node)
23295 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23296 || deleted_p);
23297 }
23298 else
23299 {
23300 deleted_p = false;
23301 if (DECL_CLASS_SCOPE_P (code_pattern))
23302 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23303 || DECL_INLINE_VAR_P (code_pattern));
23304 else
23305 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23306 }
23307
23308 /* We may be in the middle of deferred access check. Disable it now. */
23309 push_deferring_access_checks (dk_no_deferred);
23310
23311 /* Unless an explicit instantiation directive has already determined
23312 the linkage of D, remember that a definition is available for
23313 this entity. */
23314 if (pattern_defined
23315 && !DECL_INTERFACE_KNOWN (d)
23316 && !DECL_NOT_REALLY_EXTERN (d))
23317 mark_definable (d);
23318
23319 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23320 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23321 input_location = DECL_SOURCE_LOCATION (d);
23322
23323 /* If D is a member of an explicitly instantiated class template,
23324 and no definition is available, treat it like an implicit
23325 instantiation. */
23326 if (!pattern_defined && expl_inst_class_mem_p
23327 && DECL_EXPLICIT_INSTANTIATION (d))
23328 {
23329 /* Leave linkage flags alone on instantiations with anonymous
23330 visibility. */
23331 if (TREE_PUBLIC (d))
23332 {
23333 DECL_NOT_REALLY_EXTERN (d) = 0;
23334 DECL_INTERFACE_KNOWN (d) = 0;
23335 }
23336 SET_DECL_IMPLICIT_INSTANTIATION (d);
23337 }
23338
23339 /* Defer all other templates, unless we have been explicitly
23340 forbidden from doing so. */
23341 if (/* If there is no definition, we cannot instantiate the
23342 template. */
23343 ! pattern_defined
23344 /* If it's OK to postpone instantiation, do so. */
23345 || defer_ok
23346 /* If this is a static data member that will be defined
23347 elsewhere, we don't want to instantiate the entire data
23348 member, but we do want to instantiate the initializer so that
23349 we can substitute that elsewhere. */
23350 || (external_p && VAR_P (d))
23351 /* Handle here a deleted function too, avoid generating
23352 its body (c++/61080). */
23353 || deleted_p)
23354 {
23355 /* The definition of the static data member is now required so
23356 we must substitute the initializer. */
23357 if (VAR_P (d)
23358 && !DECL_INITIAL (d)
23359 && DECL_INITIAL (code_pattern))
23360 {
23361 tree ns;
23362 tree init;
23363 bool const_init = false;
23364 bool enter_context = DECL_CLASS_SCOPE_P (d);
23365
23366 ns = decl_namespace_context (d);
23367 push_nested_namespace (ns);
23368 if (enter_context)
23369 push_nested_class (DECL_CONTEXT (d));
23370 init = tsubst_expr (DECL_INITIAL (code_pattern),
23371 args,
23372 tf_warning_or_error, NULL_TREE,
23373 /*integral_constant_expression_p=*/false);
23374 /* If instantiating the initializer involved instantiating this
23375 again, don't call cp_finish_decl twice. */
23376 if (!DECL_INITIAL (d))
23377 {
23378 /* Make sure the initializer is still constant, in case of
23379 circular dependency (template/instantiate6.C). */
23380 const_init
23381 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23382 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23383 /*asmspec_tree=*/NULL_TREE,
23384 LOOKUP_ONLYCONVERTING);
23385 }
23386 if (enter_context)
23387 pop_nested_class ();
23388 pop_nested_namespace (ns);
23389 }
23390
23391 /* We restore the source position here because it's used by
23392 add_pending_template. */
23393 input_location = saved_loc;
23394
23395 if (at_eof && !pattern_defined
23396 && DECL_EXPLICIT_INSTANTIATION (d)
23397 && DECL_NOT_REALLY_EXTERN (d))
23398 /* [temp.explicit]
23399
23400 The definition of a non-exported function template, a
23401 non-exported member function template, or a non-exported
23402 member function or static data member of a class template
23403 shall be present in every translation unit in which it is
23404 explicitly instantiated. */
23405 permerror (input_location, "explicit instantiation of %qD "
23406 "but no definition available", d);
23407
23408 /* If we're in unevaluated context, we just wanted to get the
23409 constant value; this isn't an odr use, so don't queue
23410 a full instantiation. */
23411 if (cp_unevaluated_operand != 0)
23412 goto out;
23413 /* ??? Historically, we have instantiated inline functions, even
23414 when marked as "extern template". */
23415 if (!(external_p && VAR_P (d)))
23416 add_pending_template (d);
23417 goto out;
23418 }
23419 /* Tell the repository that D is available in this translation unit
23420 -- and see if it is supposed to be instantiated here. */
23421 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23422 {
23423 /* In a PCH file, despite the fact that the repository hasn't
23424 requested instantiation in the PCH it is still possible that
23425 an instantiation will be required in a file that includes the
23426 PCH. */
23427 if (pch_file)
23428 add_pending_template (d);
23429 /* Instantiate inline functions so that the inliner can do its
23430 job, even though we'll not be emitting a copy of this
23431 function. */
23432 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23433 goto out;
23434 }
23435
23436 bool push_to_top, nested;
23437 tree fn_context;
23438 fn_context = decl_function_context (d);
23439 nested = current_function_decl != NULL_TREE;
23440 push_to_top = !(nested && fn_context == current_function_decl);
23441
23442 vec<tree> omp_privatization_save;
23443 if (nested)
23444 save_omp_privatization_clauses (omp_privatization_save);
23445
23446 if (push_to_top)
23447 push_to_top_level ();
23448 else
23449 {
23450 push_function_context ();
23451 cp_unevaluated_operand = 0;
23452 c_inhibit_evaluation_warnings = 0;
23453 }
23454
23455 /* Mark D as instantiated so that recursive calls to
23456 instantiate_decl do not try to instantiate it again. */
23457 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23458
23459 /* Regenerate the declaration in case the template has been modified
23460 by a subsequent redeclaration. */
23461 regenerate_decl_from_template (d, td, args);
23462
23463 /* We already set the file and line above. Reset them now in case
23464 they changed as a result of calling regenerate_decl_from_template. */
23465 input_location = DECL_SOURCE_LOCATION (d);
23466
23467 if (VAR_P (d))
23468 {
23469 tree init;
23470 bool const_init = false;
23471
23472 /* Clear out DECL_RTL; whatever was there before may not be right
23473 since we've reset the type of the declaration. */
23474 SET_DECL_RTL (d, NULL);
23475 DECL_IN_AGGR_P (d) = 0;
23476
23477 /* The initializer is placed in DECL_INITIAL by
23478 regenerate_decl_from_template so we don't need to
23479 push/pop_access_scope again here. Pull it out so that
23480 cp_finish_decl can process it. */
23481 init = DECL_INITIAL (d);
23482 DECL_INITIAL (d) = NULL_TREE;
23483 DECL_INITIALIZED_P (d) = 0;
23484
23485 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23486 initializer. That function will defer actual emission until
23487 we have a chance to determine linkage. */
23488 DECL_EXTERNAL (d) = 0;
23489
23490 /* Enter the scope of D so that access-checking works correctly. */
23491 bool enter_context = DECL_CLASS_SCOPE_P (d);
23492 if (enter_context)
23493 push_nested_class (DECL_CONTEXT (d));
23494
23495 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23496 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23497
23498 if (enter_context)
23499 pop_nested_class ();
23500
23501 if (variable_template_p (gen_tmpl))
23502 note_variable_template_instantiation (d);
23503 }
23504 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23505 synthesize_method (d);
23506 else if (TREE_CODE (d) == FUNCTION_DECL)
23507 {
23508 /* Set up the list of local specializations. */
23509 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23510 tree block = NULL_TREE;
23511
23512 /* Set up context. */
23513 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23514 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23515 block = push_stmt_list ();
23516 else
23517 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23518
23519 /* Some typedefs referenced from within the template code need to be
23520 access checked at template instantiation time, i.e now. These
23521 types were added to the template at parsing time. Let's get those
23522 and perform the access checks then. */
23523 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23524 args);
23525
23526 /* Create substitution entries for the parameters. */
23527 register_parameter_specializations (code_pattern, d);
23528
23529 /* Substitute into the body of the function. */
23530 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23531 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23532 tf_warning_or_error, tmpl);
23533 else
23534 {
23535 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23536 tf_warning_or_error, tmpl,
23537 /*integral_constant_expression_p=*/false);
23538
23539 /* Set the current input_location to the end of the function
23540 so that finish_function knows where we are. */
23541 input_location
23542 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23543
23544 /* Remember if we saw an infinite loop in the template. */
23545 current_function_infinite_loop
23546 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23547 }
23548
23549 /* Finish the function. */
23550 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23551 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23552 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23553 else
23554 {
23555 d = finish_function (/*inline_p=*/false);
23556 expand_or_defer_fn (d);
23557 }
23558
23559 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23560 cp_check_omp_declare_reduction (d);
23561 }
23562
23563 /* We're not deferring instantiation any more. */
23564 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23565
23566 if (push_to_top)
23567 pop_from_top_level ();
23568 else
23569 pop_function_context ();
23570
23571 if (nested)
23572 restore_omp_privatization_clauses (omp_privatization_save);
23573
23574 out:
23575 pop_deferring_access_checks ();
23576 timevar_pop (TV_TEMPLATE_INST);
23577 pop_tinst_level ();
23578 input_location = saved_loc;
23579 cp_unevaluated_operand = saved_unevaluated_operand;
23580 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23581
23582 return d;
23583 }
23584
23585 /* Run through the list of templates that we wish we could
23586 instantiate, and instantiate any we can. RETRIES is the
23587 number of times we retry pending template instantiation. */
23588
23589 void
23590 instantiate_pending_templates (int retries)
23591 {
23592 int reconsider;
23593 location_t saved_loc = input_location;
23594
23595 /* Instantiating templates may trigger vtable generation. This in turn
23596 may require further template instantiations. We place a limit here
23597 to avoid infinite loop. */
23598 if (pending_templates && retries >= max_tinst_depth)
23599 {
23600 tree decl = pending_templates->tinst->decl;
23601
23602 fatal_error (input_location,
23603 "template instantiation depth exceeds maximum of %d"
23604 " instantiating %q+D, possibly from virtual table generation"
23605 " (use -ftemplate-depth= to increase the maximum)",
23606 max_tinst_depth, decl);
23607 if (TREE_CODE (decl) == FUNCTION_DECL)
23608 /* Pretend that we defined it. */
23609 DECL_INITIAL (decl) = error_mark_node;
23610 return;
23611 }
23612
23613 do
23614 {
23615 struct pending_template **t = &pending_templates;
23616 struct pending_template *last = NULL;
23617 reconsider = 0;
23618 while (*t)
23619 {
23620 tree instantiation = reopen_tinst_level ((*t)->tinst);
23621 bool complete = false;
23622
23623 if (TYPE_P (instantiation))
23624 {
23625 if (!COMPLETE_TYPE_P (instantiation))
23626 {
23627 instantiate_class_template (instantiation);
23628 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23629 for (tree fld = TYPE_FIELDS (instantiation);
23630 fld; fld = TREE_CHAIN (fld))
23631 if ((VAR_P (fld)
23632 || (TREE_CODE (fld) == FUNCTION_DECL
23633 && !DECL_ARTIFICIAL (fld)))
23634 && DECL_TEMPLATE_INSTANTIATION (fld))
23635 instantiate_decl (fld,
23636 /*defer_ok=*/false,
23637 /*expl_inst_class_mem_p=*/false);
23638
23639 if (COMPLETE_TYPE_P (instantiation))
23640 reconsider = 1;
23641 }
23642
23643 complete = COMPLETE_TYPE_P (instantiation);
23644 }
23645 else
23646 {
23647 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23648 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23649 {
23650 instantiation
23651 = instantiate_decl (instantiation,
23652 /*defer_ok=*/false,
23653 /*expl_inst_class_mem_p=*/false);
23654 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23655 reconsider = 1;
23656 }
23657
23658 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23659 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23660 }
23661
23662 if (complete)
23663 /* If INSTANTIATION has been instantiated, then we don't
23664 need to consider it again in the future. */
23665 *t = (*t)->next;
23666 else
23667 {
23668 last = *t;
23669 t = &(*t)->next;
23670 }
23671 tinst_depth = 0;
23672 current_tinst_level = NULL;
23673 }
23674 last_pending_template = last;
23675 }
23676 while (reconsider);
23677
23678 input_location = saved_loc;
23679 }
23680
23681 /* Substitute ARGVEC into T, which is a list of initializers for
23682 either base class or a non-static data member. The TREE_PURPOSEs
23683 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23684 instantiate_decl. */
23685
23686 static tree
23687 tsubst_initializer_list (tree t, tree argvec)
23688 {
23689 tree inits = NULL_TREE;
23690 tree target_ctor = error_mark_node;
23691
23692 for (; t; t = TREE_CHAIN (t))
23693 {
23694 tree decl;
23695 tree init;
23696 tree expanded_bases = NULL_TREE;
23697 tree expanded_arguments = NULL_TREE;
23698 int i, len = 1;
23699
23700 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23701 {
23702 tree expr;
23703 tree arg;
23704
23705 /* Expand the base class expansion type into separate base
23706 classes. */
23707 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23708 tf_warning_or_error,
23709 NULL_TREE);
23710 if (expanded_bases == error_mark_node)
23711 continue;
23712
23713 /* We'll be building separate TREE_LISTs of arguments for
23714 each base. */
23715 len = TREE_VEC_LENGTH (expanded_bases);
23716 expanded_arguments = make_tree_vec (len);
23717 for (i = 0; i < len; i++)
23718 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23719
23720 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23721 expand each argument in the TREE_VALUE of t. */
23722 expr = make_node (EXPR_PACK_EXPANSION);
23723 PACK_EXPANSION_LOCAL_P (expr) = true;
23724 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23725 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23726
23727 if (TREE_VALUE (t) == void_type_node)
23728 /* VOID_TYPE_NODE is used to indicate
23729 value-initialization. */
23730 {
23731 for (i = 0; i < len; i++)
23732 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23733 }
23734 else
23735 {
23736 /* Substitute parameter packs into each argument in the
23737 TREE_LIST. */
23738 in_base_initializer = 1;
23739 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23740 {
23741 tree expanded_exprs;
23742
23743 /* Expand the argument. */
23744 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23745 expanded_exprs
23746 = tsubst_pack_expansion (expr, argvec,
23747 tf_warning_or_error,
23748 NULL_TREE);
23749 if (expanded_exprs == error_mark_node)
23750 continue;
23751
23752 /* Prepend each of the expanded expressions to the
23753 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23754 for (i = 0; i < len; i++)
23755 {
23756 TREE_VEC_ELT (expanded_arguments, i) =
23757 tree_cons (NULL_TREE,
23758 TREE_VEC_ELT (expanded_exprs, i),
23759 TREE_VEC_ELT (expanded_arguments, i));
23760 }
23761 }
23762 in_base_initializer = 0;
23763
23764 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23765 since we built them backwards. */
23766 for (i = 0; i < len; i++)
23767 {
23768 TREE_VEC_ELT (expanded_arguments, i) =
23769 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23770 }
23771 }
23772 }
23773
23774 for (i = 0; i < len; ++i)
23775 {
23776 if (expanded_bases)
23777 {
23778 decl = TREE_VEC_ELT (expanded_bases, i);
23779 decl = expand_member_init (decl);
23780 init = TREE_VEC_ELT (expanded_arguments, i);
23781 }
23782 else
23783 {
23784 tree tmp;
23785 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23786 tf_warning_or_error, NULL_TREE);
23787
23788 decl = expand_member_init (decl);
23789 if (decl && !DECL_P (decl))
23790 in_base_initializer = 1;
23791
23792 init = TREE_VALUE (t);
23793 tmp = init;
23794 if (init != void_type_node)
23795 init = tsubst_expr (init, argvec,
23796 tf_warning_or_error, NULL_TREE,
23797 /*integral_constant_expression_p=*/false);
23798 if (init == NULL_TREE && tmp != NULL_TREE)
23799 /* If we had an initializer but it instantiated to nothing,
23800 value-initialize the object. This will only occur when
23801 the initializer was a pack expansion where the parameter
23802 packs used in that expansion were of length zero. */
23803 init = void_type_node;
23804 in_base_initializer = 0;
23805 }
23806
23807 if (target_ctor != error_mark_node
23808 && init != error_mark_node)
23809 {
23810 error ("mem-initializer for %qD follows constructor delegation",
23811 decl);
23812 return inits;
23813 }
23814 /* Look for a target constructor. */
23815 if (init != error_mark_node
23816 && decl && CLASS_TYPE_P (decl)
23817 && same_type_p (decl, current_class_type))
23818 {
23819 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
23820 if (inits)
23821 {
23822 error ("constructor delegation follows mem-initializer for %qD",
23823 TREE_PURPOSE (inits));
23824 continue;
23825 }
23826 target_ctor = init;
23827 }
23828
23829 if (decl)
23830 {
23831 init = build_tree_list (decl, init);
23832 TREE_CHAIN (init) = inits;
23833 inits = init;
23834 }
23835 }
23836 }
23837 return inits;
23838 }
23839
23840 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23841
23842 static void
23843 set_current_access_from_decl (tree decl)
23844 {
23845 if (TREE_PRIVATE (decl))
23846 current_access_specifier = access_private_node;
23847 else if (TREE_PROTECTED (decl))
23848 current_access_specifier = access_protected_node;
23849 else
23850 current_access_specifier = access_public_node;
23851 }
23852
23853 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23854 is the instantiation (which should have been created with
23855 start_enum) and ARGS are the template arguments to use. */
23856
23857 static void
23858 tsubst_enum (tree tag, tree newtag, tree args)
23859 {
23860 tree e;
23861
23862 if (SCOPED_ENUM_P (newtag))
23863 begin_scope (sk_scoped_enum, newtag);
23864
23865 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23866 {
23867 tree value;
23868 tree decl;
23869
23870 decl = TREE_VALUE (e);
23871 /* Note that in a template enum, the TREE_VALUE is the
23872 CONST_DECL, not the corresponding INTEGER_CST. */
23873 value = tsubst_expr (DECL_INITIAL (decl),
23874 args, tf_warning_or_error, NULL_TREE,
23875 /*integral_constant_expression_p=*/true);
23876
23877 /* Give this enumeration constant the correct access. */
23878 set_current_access_from_decl (decl);
23879
23880 /* Actually build the enumerator itself. Here we're assuming that
23881 enumerators can't have dependent attributes. */
23882 build_enumerator (DECL_NAME (decl), value, newtag,
23883 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23884 }
23885
23886 if (SCOPED_ENUM_P (newtag))
23887 finish_scope ();
23888
23889 finish_enum_value_list (newtag);
23890 finish_enum (newtag);
23891
23892 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23893 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23894 }
23895
23896 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23897 its type -- but without substituting the innermost set of template
23898 arguments. So, innermost set of template parameters will appear in
23899 the type. */
23900
23901 tree
23902 get_mostly_instantiated_function_type (tree decl)
23903 {
23904 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23905 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23906 }
23907
23908 /* Return truthvalue if we're processing a template different from
23909 the last one involved in diagnostics. */
23910 bool
23911 problematic_instantiation_changed (void)
23912 {
23913 return current_tinst_level != last_error_tinst_level;
23914 }
23915
23916 /* Remember current template involved in diagnostics. */
23917 void
23918 record_last_problematic_instantiation (void)
23919 {
23920 last_error_tinst_level = current_tinst_level;
23921 }
23922
23923 struct tinst_level *
23924 current_instantiation (void)
23925 {
23926 return current_tinst_level;
23927 }
23928
23929 /* Return TRUE if current_function_decl is being instantiated, false
23930 otherwise. */
23931
23932 bool
23933 instantiating_current_function_p (void)
23934 {
23935 return (current_instantiation ()
23936 && current_instantiation ()->decl == current_function_decl);
23937 }
23938
23939 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23940 type. Return false for ok, true for disallowed. Issue error and
23941 inform messages under control of COMPLAIN. */
23942
23943 static bool
23944 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23945 {
23946 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23947 return false;
23948 else if (POINTER_TYPE_P (type))
23949 return false;
23950 else if (TYPE_PTRMEM_P (type))
23951 return false;
23952 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23953 return false;
23954 else if (TREE_CODE (type) == TYPENAME_TYPE)
23955 return false;
23956 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23957 return false;
23958 else if (TREE_CODE (type) == NULLPTR_TYPE)
23959 return false;
23960 /* A bound template template parm could later be instantiated to have a valid
23961 nontype parm type via an alias template. */
23962 else if (cxx_dialect >= cxx11
23963 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23964 return false;
23965
23966 if (complain & tf_error)
23967 {
23968 if (type == error_mark_node)
23969 inform (input_location, "invalid template non-type parameter");
23970 else
23971 error ("%q#T is not a valid type for a template non-type parameter",
23972 type);
23973 }
23974 return true;
23975 }
23976
23977 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23978 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23979
23980 static bool
23981 dependent_type_p_r (tree type)
23982 {
23983 tree scope;
23984
23985 /* [temp.dep.type]
23986
23987 A type is dependent if it is:
23988
23989 -- a template parameter. Template template parameters are types
23990 for us (since TYPE_P holds true for them) so we handle
23991 them here. */
23992 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23993 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23994 return true;
23995 /* -- a qualified-id with a nested-name-specifier which contains a
23996 class-name that names a dependent type or whose unqualified-id
23997 names a dependent type. */
23998 if (TREE_CODE (type) == TYPENAME_TYPE)
23999 return true;
24000
24001 /* An alias template specialization can be dependent even if the
24002 resulting type is not. */
24003 if (dependent_alias_template_spec_p (type))
24004 return true;
24005
24006 /* -- a cv-qualified type where the cv-unqualified type is
24007 dependent.
24008 No code is necessary for this bullet; the code below handles
24009 cv-qualified types, and we don't want to strip aliases with
24010 TYPE_MAIN_VARIANT because of DR 1558. */
24011 /* -- a compound type constructed from any dependent type. */
24012 if (TYPE_PTRMEM_P (type))
24013 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
24014 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24015 (type)));
24016 else if (TYPE_PTR_P (type)
24017 || TREE_CODE (type) == REFERENCE_TYPE)
24018 return dependent_type_p (TREE_TYPE (type));
24019 else if (TREE_CODE (type) == FUNCTION_TYPE
24020 || TREE_CODE (type) == METHOD_TYPE)
24021 {
24022 tree arg_type;
24023
24024 if (dependent_type_p (TREE_TYPE (type)))
24025 return true;
24026 for (arg_type = TYPE_ARG_TYPES (type);
24027 arg_type;
24028 arg_type = TREE_CHAIN (arg_type))
24029 if (dependent_type_p (TREE_VALUE (arg_type)))
24030 return true;
24031 if (cxx_dialect >= cxx17)
24032 /* A value-dependent noexcept-specifier makes the type dependent. */
24033 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
24034 if (tree noex = TREE_PURPOSE (spec))
24035 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24036 affect overload resolution and treating it as dependent breaks
24037 things. */
24038 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
24039 && value_dependent_expression_p (noex))
24040 return true;
24041 return false;
24042 }
24043 /* -- an array type constructed from any dependent type or whose
24044 size is specified by a constant expression that is
24045 value-dependent.
24046
24047 We checked for type- and value-dependence of the bounds in
24048 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24049 if (TREE_CODE (type) == ARRAY_TYPE)
24050 {
24051 if (TYPE_DOMAIN (type)
24052 && dependent_type_p (TYPE_DOMAIN (type)))
24053 return true;
24054 return dependent_type_p (TREE_TYPE (type));
24055 }
24056
24057 /* -- a template-id in which either the template name is a template
24058 parameter ... */
24059 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24060 return true;
24061 /* ... or any of the template arguments is a dependent type or
24062 an expression that is type-dependent or value-dependent. */
24063 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
24064 && (any_dependent_template_arguments_p
24065 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
24066 return true;
24067
24068 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24069 dependent; if the argument of the `typeof' expression is not
24070 type-dependent, then it should already been have resolved. */
24071 if (TREE_CODE (type) == TYPEOF_TYPE
24072 || TREE_CODE (type) == DECLTYPE_TYPE
24073 || TREE_CODE (type) == UNDERLYING_TYPE)
24074 return true;
24075
24076 /* A template argument pack is dependent if any of its packed
24077 arguments are. */
24078 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
24079 {
24080 tree args = ARGUMENT_PACK_ARGS (type);
24081 int i, len = TREE_VEC_LENGTH (args);
24082 for (i = 0; i < len; ++i)
24083 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24084 return true;
24085 }
24086
24087 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24088 be template parameters. */
24089 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
24090 return true;
24091
24092 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
24093 return true;
24094
24095 /* The standard does not specifically mention types that are local
24096 to template functions or local classes, but they should be
24097 considered dependent too. For example:
24098
24099 template <int I> void f() {
24100 enum E { a = I };
24101 S<sizeof (E)> s;
24102 }
24103
24104 The size of `E' cannot be known until the value of `I' has been
24105 determined. Therefore, `E' must be considered dependent. */
24106 scope = TYPE_CONTEXT (type);
24107 if (scope && TYPE_P (scope))
24108 return dependent_type_p (scope);
24109 /* Don't use type_dependent_expression_p here, as it can lead
24110 to infinite recursion trying to determine whether a lambda
24111 nested in a lambda is dependent (c++/47687). */
24112 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
24113 && DECL_LANG_SPECIFIC (scope)
24114 && DECL_TEMPLATE_INFO (scope)
24115 && (any_dependent_template_arguments_p
24116 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
24117 return true;
24118
24119 /* Other types are non-dependent. */
24120 return false;
24121 }
24122
24123 /* Returns TRUE if TYPE is dependent, in the sense of
24124 [temp.dep.type]. Note that a NULL type is considered dependent. */
24125
24126 bool
24127 dependent_type_p (tree type)
24128 {
24129 /* If there are no template parameters in scope, then there can't be
24130 any dependent types. */
24131 if (!processing_template_decl)
24132 {
24133 /* If we are not processing a template, then nobody should be
24134 providing us with a dependent type. */
24135 gcc_assert (type);
24136 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
24137 return false;
24138 }
24139
24140 /* If the type is NULL, we have not computed a type for the entity
24141 in question; in that case, the type is dependent. */
24142 if (!type)
24143 return true;
24144
24145 /* Erroneous types can be considered non-dependent. */
24146 if (type == error_mark_node)
24147 return false;
24148
24149 /* Getting here with global_type_node means we improperly called this
24150 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24151 gcc_checking_assert (type != global_type_node);
24152
24153 /* If we have not already computed the appropriate value for TYPE,
24154 do so now. */
24155 if (!TYPE_DEPENDENT_P_VALID (type))
24156 {
24157 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
24158 TYPE_DEPENDENT_P_VALID (type) = 1;
24159 }
24160
24161 return TYPE_DEPENDENT_P (type);
24162 }
24163
24164 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24165 lookup. In other words, a dependent type that is not the current
24166 instantiation. */
24167
24168 bool
24169 dependent_scope_p (tree scope)
24170 {
24171 return (scope && TYPE_P (scope) && dependent_type_p (scope)
24172 && !currently_open_class (scope));
24173 }
24174
24175 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24176 an unknown base of 'this' (and is therefore instantiation-dependent). */
24177
24178 static bool
24179 unknown_base_ref_p (tree t)
24180 {
24181 if (!current_class_ptr)
24182 return false;
24183
24184 tree mem = TREE_OPERAND (t, 1);
24185 if (shared_member_p (mem))
24186 return false;
24187
24188 tree cur = current_nonlambda_class_type ();
24189 if (!any_dependent_bases_p (cur))
24190 return false;
24191
24192 tree ctx = TREE_OPERAND (t, 0);
24193 if (DERIVED_FROM_P (ctx, cur))
24194 return false;
24195
24196 return true;
24197 }
24198
24199 /* T is a SCOPE_REF; return whether we need to consider it
24200 instantiation-dependent so that we can check access at instantiation
24201 time even though we know which member it resolves to. */
24202
24203 static bool
24204 instantiation_dependent_scope_ref_p (tree t)
24205 {
24206 if (DECL_P (TREE_OPERAND (t, 1))
24207 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
24208 && !unknown_base_ref_p (t)
24209 && accessible_in_template_p (TREE_OPERAND (t, 0),
24210 TREE_OPERAND (t, 1)))
24211 return false;
24212 else
24213 return true;
24214 }
24215
24216 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24217 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24218 expression. */
24219
24220 /* Note that this predicate is not appropriate for general expressions;
24221 only constant expressions (that satisfy potential_constant_expression)
24222 can be tested for value dependence. */
24223
24224 bool
24225 value_dependent_expression_p (tree expression)
24226 {
24227 if (!processing_template_decl || expression == NULL_TREE)
24228 return false;
24229
24230 /* A type-dependent expression is also value-dependent. */
24231 if (type_dependent_expression_p (expression))
24232 return true;
24233
24234 switch (TREE_CODE (expression))
24235 {
24236 case BASELINK:
24237 /* A dependent member function of the current instantiation. */
24238 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24239
24240 case FUNCTION_DECL:
24241 /* A dependent member function of the current instantiation. */
24242 if (DECL_CLASS_SCOPE_P (expression)
24243 && dependent_type_p (DECL_CONTEXT (expression)))
24244 return true;
24245 break;
24246
24247 case IDENTIFIER_NODE:
24248 /* A name that has not been looked up -- must be dependent. */
24249 return true;
24250
24251 case TEMPLATE_PARM_INDEX:
24252 /* A non-type template parm. */
24253 return true;
24254
24255 case CONST_DECL:
24256 /* A non-type template parm. */
24257 if (DECL_TEMPLATE_PARM_P (expression))
24258 return true;
24259 return value_dependent_expression_p (DECL_INITIAL (expression));
24260
24261 case VAR_DECL:
24262 /* A constant with literal type and is initialized
24263 with an expression that is value-dependent. */
24264 if (DECL_DEPENDENT_INIT_P (expression)
24265 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24266 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
24267 return true;
24268 if (DECL_HAS_VALUE_EXPR_P (expression))
24269 {
24270 tree value_expr = DECL_VALUE_EXPR (expression);
24271 if (value_dependent_expression_p (value_expr))
24272 return true;
24273 }
24274 return false;
24275
24276 case DYNAMIC_CAST_EXPR:
24277 case STATIC_CAST_EXPR:
24278 case CONST_CAST_EXPR:
24279 case REINTERPRET_CAST_EXPR:
24280 case CAST_EXPR:
24281 case IMPLICIT_CONV_EXPR:
24282 /* These expressions are value-dependent if the type to which
24283 the cast occurs is dependent or the expression being casted
24284 is value-dependent. */
24285 {
24286 tree type = TREE_TYPE (expression);
24287
24288 if (dependent_type_p (type))
24289 return true;
24290
24291 /* A functional cast has a list of operands. */
24292 expression = TREE_OPERAND (expression, 0);
24293 if (!expression)
24294 {
24295 /* If there are no operands, it must be an expression such
24296 as "int()". This should not happen for aggregate types
24297 because it would form non-constant expressions. */
24298 gcc_assert (cxx_dialect >= cxx11
24299 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24300
24301 return false;
24302 }
24303
24304 if (TREE_CODE (expression) == TREE_LIST)
24305 return any_value_dependent_elements_p (expression);
24306
24307 return value_dependent_expression_p (expression);
24308 }
24309
24310 case SIZEOF_EXPR:
24311 if (SIZEOF_EXPR_TYPE_P (expression))
24312 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24313 /* FALLTHRU */
24314 case ALIGNOF_EXPR:
24315 case TYPEID_EXPR:
24316 /* A `sizeof' expression is value-dependent if the operand is
24317 type-dependent or is a pack expansion. */
24318 expression = TREE_OPERAND (expression, 0);
24319 if (PACK_EXPANSION_P (expression))
24320 return true;
24321 else if (TYPE_P (expression))
24322 return dependent_type_p (expression);
24323 return instantiation_dependent_uneval_expression_p (expression);
24324
24325 case AT_ENCODE_EXPR:
24326 /* An 'encode' expression is value-dependent if the operand is
24327 type-dependent. */
24328 expression = TREE_OPERAND (expression, 0);
24329 return dependent_type_p (expression);
24330
24331 case NOEXCEPT_EXPR:
24332 expression = TREE_OPERAND (expression, 0);
24333 return instantiation_dependent_uneval_expression_p (expression);
24334
24335 case SCOPE_REF:
24336 /* All instantiation-dependent expressions should also be considered
24337 value-dependent. */
24338 return instantiation_dependent_scope_ref_p (expression);
24339
24340 case COMPONENT_REF:
24341 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24342 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24343
24344 case NONTYPE_ARGUMENT_PACK:
24345 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24346 is value-dependent. */
24347 {
24348 tree values = ARGUMENT_PACK_ARGS (expression);
24349 int i, len = TREE_VEC_LENGTH (values);
24350
24351 for (i = 0; i < len; ++i)
24352 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24353 return true;
24354
24355 return false;
24356 }
24357
24358 case TRAIT_EXPR:
24359 {
24360 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24361
24362 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24363 return true;
24364
24365 if (!type2)
24366 return false;
24367
24368 if (TREE_CODE (type2) != TREE_LIST)
24369 return dependent_type_p (type2);
24370
24371 for (; type2; type2 = TREE_CHAIN (type2))
24372 if (dependent_type_p (TREE_VALUE (type2)))
24373 return true;
24374
24375 return false;
24376 }
24377
24378 case MODOP_EXPR:
24379 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24380 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24381
24382 case ARRAY_REF:
24383 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24384 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24385
24386 case ADDR_EXPR:
24387 {
24388 tree op = TREE_OPERAND (expression, 0);
24389 return (value_dependent_expression_p (op)
24390 || has_value_dependent_address (op));
24391 }
24392
24393 case REQUIRES_EXPR:
24394 /* Treat all requires-expressions as value-dependent so
24395 we don't try to fold them. */
24396 return true;
24397
24398 case TYPE_REQ:
24399 return dependent_type_p (TREE_OPERAND (expression, 0));
24400
24401 case CALL_EXPR:
24402 {
24403 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24404 return true;
24405 tree fn = get_callee_fndecl (expression);
24406 int i, nargs;
24407 nargs = call_expr_nargs (expression);
24408 for (i = 0; i < nargs; ++i)
24409 {
24410 tree op = CALL_EXPR_ARG (expression, i);
24411 /* In a call to a constexpr member function, look through the
24412 implicit ADDR_EXPR on the object argument so that it doesn't
24413 cause the call to be considered value-dependent. We also
24414 look through it in potential_constant_expression. */
24415 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24416 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24417 && TREE_CODE (op) == ADDR_EXPR)
24418 op = TREE_OPERAND (op, 0);
24419 if (value_dependent_expression_p (op))
24420 return true;
24421 }
24422 return false;
24423 }
24424
24425 case TEMPLATE_ID_EXPR:
24426 return variable_concept_p (TREE_OPERAND (expression, 0));
24427
24428 case CONSTRUCTOR:
24429 {
24430 unsigned ix;
24431 tree val;
24432 if (dependent_type_p (TREE_TYPE (expression)))
24433 return true;
24434 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24435 if (value_dependent_expression_p (val))
24436 return true;
24437 return false;
24438 }
24439
24440 case STMT_EXPR:
24441 /* Treat a GNU statement expression as dependent to avoid crashing
24442 under instantiate_non_dependent_expr; it can't be constant. */
24443 return true;
24444
24445 default:
24446 /* A constant expression is value-dependent if any subexpression is
24447 value-dependent. */
24448 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24449 {
24450 case tcc_reference:
24451 case tcc_unary:
24452 case tcc_comparison:
24453 case tcc_binary:
24454 case tcc_expression:
24455 case tcc_vl_exp:
24456 {
24457 int i, len = cp_tree_operand_length (expression);
24458
24459 for (i = 0; i < len; i++)
24460 {
24461 tree t = TREE_OPERAND (expression, i);
24462
24463 /* In some cases, some of the operands may be missing.
24464 (For example, in the case of PREDECREMENT_EXPR, the
24465 amount to increment by may be missing.) That doesn't
24466 make the expression dependent. */
24467 if (t && value_dependent_expression_p (t))
24468 return true;
24469 }
24470 }
24471 break;
24472 default:
24473 break;
24474 }
24475 break;
24476 }
24477
24478 /* The expression is not value-dependent. */
24479 return false;
24480 }
24481
24482 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24483 [temp.dep.expr]. Note that an expression with no type is
24484 considered dependent. Other parts of the compiler arrange for an
24485 expression with type-dependent subexpressions to have no type, so
24486 this function doesn't have to be fully recursive. */
24487
24488 bool
24489 type_dependent_expression_p (tree expression)
24490 {
24491 if (!processing_template_decl)
24492 return false;
24493
24494 if (expression == NULL_TREE || expression == error_mark_node)
24495 return false;
24496
24497 STRIP_ANY_LOCATION_WRAPPER (expression);
24498
24499 /* An unresolved name is always dependent. */
24500 if (identifier_p (expression)
24501 || TREE_CODE (expression) == USING_DECL
24502 || TREE_CODE (expression) == WILDCARD_DECL)
24503 return true;
24504
24505 /* A fold expression is type-dependent. */
24506 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24507 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24508 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24509 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24510 return true;
24511
24512 /* Some expression forms are never type-dependent. */
24513 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24514 || TREE_CODE (expression) == SIZEOF_EXPR
24515 || TREE_CODE (expression) == ALIGNOF_EXPR
24516 || TREE_CODE (expression) == AT_ENCODE_EXPR
24517 || TREE_CODE (expression) == NOEXCEPT_EXPR
24518 || TREE_CODE (expression) == TRAIT_EXPR
24519 || TREE_CODE (expression) == TYPEID_EXPR
24520 || TREE_CODE (expression) == DELETE_EXPR
24521 || TREE_CODE (expression) == VEC_DELETE_EXPR
24522 || TREE_CODE (expression) == THROW_EXPR
24523 || TREE_CODE (expression) == REQUIRES_EXPR)
24524 return false;
24525
24526 /* The types of these expressions depends only on the type to which
24527 the cast occurs. */
24528 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24529 || TREE_CODE (expression) == STATIC_CAST_EXPR
24530 || TREE_CODE (expression) == CONST_CAST_EXPR
24531 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24532 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24533 || TREE_CODE (expression) == CAST_EXPR)
24534 return dependent_type_p (TREE_TYPE (expression));
24535
24536 /* The types of these expressions depends only on the type created
24537 by the expression. */
24538 if (TREE_CODE (expression) == NEW_EXPR
24539 || TREE_CODE (expression) == VEC_NEW_EXPR)
24540 {
24541 /* For NEW_EXPR tree nodes created inside a template, either
24542 the object type itself or a TREE_LIST may appear as the
24543 operand 1. */
24544 tree type = TREE_OPERAND (expression, 1);
24545 if (TREE_CODE (type) == TREE_LIST)
24546 /* This is an array type. We need to check array dimensions
24547 as well. */
24548 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24549 || value_dependent_expression_p
24550 (TREE_OPERAND (TREE_VALUE (type), 1));
24551 else
24552 return dependent_type_p (type);
24553 }
24554
24555 if (TREE_CODE (expression) == SCOPE_REF)
24556 {
24557 tree scope = TREE_OPERAND (expression, 0);
24558 tree name = TREE_OPERAND (expression, 1);
24559
24560 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24561 contains an identifier associated by name lookup with one or more
24562 declarations declared with a dependent type, or...a
24563 nested-name-specifier or qualified-id that names a member of an
24564 unknown specialization. */
24565 return (type_dependent_expression_p (name)
24566 || dependent_scope_p (scope));
24567 }
24568
24569 if (TREE_CODE (expression) == TEMPLATE_DECL
24570 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24571 return uses_outer_template_parms (expression);
24572
24573 if (TREE_CODE (expression) == STMT_EXPR)
24574 expression = stmt_expr_value_expr (expression);
24575
24576 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24577 {
24578 tree elt;
24579 unsigned i;
24580
24581 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24582 {
24583 if (type_dependent_expression_p (elt))
24584 return true;
24585 }
24586 return false;
24587 }
24588
24589 /* A static data member of the current instantiation with incomplete
24590 array type is type-dependent, as the definition and specializations
24591 can have different bounds. */
24592 if (VAR_P (expression)
24593 && DECL_CLASS_SCOPE_P (expression)
24594 && dependent_type_p (DECL_CONTEXT (expression))
24595 && VAR_HAD_UNKNOWN_BOUND (expression))
24596 return true;
24597
24598 /* An array of unknown bound depending on a variadic parameter, eg:
24599
24600 template<typename... Args>
24601 void foo (Args... args)
24602 {
24603 int arr[] = { args... };
24604 }
24605
24606 template<int... vals>
24607 void bar ()
24608 {
24609 int arr[] = { vals... };
24610 }
24611
24612 If the array has no length and has an initializer, it must be that
24613 we couldn't determine its length in cp_complete_array_type because
24614 it is dependent. */
24615 if (VAR_P (expression)
24616 && TREE_TYPE (expression) != NULL_TREE
24617 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24618 && !TYPE_DOMAIN (TREE_TYPE (expression))
24619 && DECL_INITIAL (expression))
24620 return true;
24621
24622 /* A function or variable template-id is type-dependent if it has any
24623 dependent template arguments. */
24624 if (VAR_OR_FUNCTION_DECL_P (expression)
24625 && DECL_LANG_SPECIFIC (expression)
24626 && DECL_TEMPLATE_INFO (expression))
24627 {
24628 /* Consider the innermost template arguments, since those are the ones
24629 that come from the template-id; the template arguments for the
24630 enclosing class do not make it type-dependent unless they are used in
24631 the type of the decl. */
24632 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24633 && (any_dependent_template_arguments_p
24634 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24635 return true;
24636 }
24637
24638 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24639 type-dependent. Checking this is important for functions with auto return
24640 type, which looks like a dependent type. */
24641 if (TREE_CODE (expression) == FUNCTION_DECL
24642 && !(DECL_CLASS_SCOPE_P (expression)
24643 && dependent_type_p (DECL_CONTEXT (expression)))
24644 && !(DECL_LANG_SPECIFIC (expression)
24645 && DECL_FRIEND_P (expression)
24646 && (!DECL_FRIEND_CONTEXT (expression)
24647 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24648 && !DECL_LOCAL_FUNCTION_P (expression))
24649 {
24650 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24651 || undeduced_auto_decl (expression));
24652 return false;
24653 }
24654
24655 /* Always dependent, on the number of arguments if nothing else. */
24656 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24657 return true;
24658
24659 if (TREE_TYPE (expression) == unknown_type_node)
24660 {
24661 if (TREE_CODE (expression) == ADDR_EXPR)
24662 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24663 if (TREE_CODE (expression) == COMPONENT_REF
24664 || TREE_CODE (expression) == OFFSET_REF)
24665 {
24666 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24667 return true;
24668 expression = TREE_OPERAND (expression, 1);
24669 if (identifier_p (expression))
24670 return false;
24671 }
24672 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24673 if (TREE_CODE (expression) == SCOPE_REF)
24674 return false;
24675
24676 if (BASELINK_P (expression))
24677 {
24678 if (BASELINK_OPTYPE (expression)
24679 && dependent_type_p (BASELINK_OPTYPE (expression)))
24680 return true;
24681 expression = BASELINK_FUNCTIONS (expression);
24682 }
24683
24684 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24685 {
24686 if (any_dependent_template_arguments_p
24687 (TREE_OPERAND (expression, 1)))
24688 return true;
24689 expression = TREE_OPERAND (expression, 0);
24690 if (identifier_p (expression))
24691 return true;
24692 }
24693
24694 gcc_assert (TREE_CODE (expression) == OVERLOAD
24695 || TREE_CODE (expression) == FUNCTION_DECL);
24696
24697 for (lkp_iterator iter (expression); iter; ++iter)
24698 if (type_dependent_expression_p (*iter))
24699 return true;
24700
24701 return false;
24702 }
24703
24704 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24705
24706 /* Dependent type attributes might not have made it from the decl to
24707 the type yet. */
24708 if (DECL_P (expression)
24709 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24710 return true;
24711
24712 return (dependent_type_p (TREE_TYPE (expression)));
24713 }
24714
24715 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24716 type-dependent if the expression refers to a member of the current
24717 instantiation and the type of the referenced member is dependent, or the
24718 class member access expression refers to a member of an unknown
24719 specialization.
24720
24721 This function returns true if the OBJECT in such a class member access
24722 expression is of an unknown specialization. */
24723
24724 bool
24725 type_dependent_object_expression_p (tree object)
24726 {
24727 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24728 dependent. */
24729 if (TREE_CODE (object) == IDENTIFIER_NODE)
24730 return true;
24731 tree scope = TREE_TYPE (object);
24732 return (!scope || dependent_scope_p (scope));
24733 }
24734
24735 /* walk_tree callback function for instantiation_dependent_expression_p,
24736 below. Returns non-zero if a dependent subexpression is found. */
24737
24738 static tree
24739 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24740 void * /*data*/)
24741 {
24742 if (TYPE_P (*tp))
24743 {
24744 /* We don't have to worry about decltype currently because decltype
24745 of an instantiation-dependent expr is a dependent type. This
24746 might change depending on the resolution of DR 1172. */
24747 *walk_subtrees = false;
24748 return NULL_TREE;
24749 }
24750 enum tree_code code = TREE_CODE (*tp);
24751 switch (code)
24752 {
24753 /* Don't treat an argument list as dependent just because it has no
24754 TREE_TYPE. */
24755 case TREE_LIST:
24756 case TREE_VEC:
24757 return NULL_TREE;
24758
24759 case TEMPLATE_PARM_INDEX:
24760 return *tp;
24761
24762 /* Handle expressions with type operands. */
24763 case SIZEOF_EXPR:
24764 case ALIGNOF_EXPR:
24765 case TYPEID_EXPR:
24766 case AT_ENCODE_EXPR:
24767 {
24768 tree op = TREE_OPERAND (*tp, 0);
24769 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24770 op = TREE_TYPE (op);
24771 if (TYPE_P (op))
24772 {
24773 if (dependent_type_p (op))
24774 return *tp;
24775 else
24776 {
24777 *walk_subtrees = false;
24778 return NULL_TREE;
24779 }
24780 }
24781 break;
24782 }
24783
24784 case COMPONENT_REF:
24785 if (identifier_p (TREE_OPERAND (*tp, 1)))
24786 /* In a template, finish_class_member_access_expr creates a
24787 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24788 type-dependent, so that we can check access control at
24789 instantiation time (PR 42277). See also Core issue 1273. */
24790 return *tp;
24791 break;
24792
24793 case SCOPE_REF:
24794 if (instantiation_dependent_scope_ref_p (*tp))
24795 return *tp;
24796 else
24797 break;
24798
24799 /* Treat statement-expressions as dependent. */
24800 case BIND_EXPR:
24801 return *tp;
24802
24803 /* Treat requires-expressions as dependent. */
24804 case REQUIRES_EXPR:
24805 return *tp;
24806
24807 case CALL_EXPR:
24808 /* Treat calls to function concepts as dependent. */
24809 if (function_concept_check_p (*tp))
24810 return *tp;
24811 break;
24812
24813 case TEMPLATE_ID_EXPR:
24814 /* And variable concepts. */
24815 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24816 return *tp;
24817 break;
24818
24819 default:
24820 break;
24821 }
24822
24823 if (type_dependent_expression_p (*tp))
24824 return *tp;
24825 else
24826 return NULL_TREE;
24827 }
24828
24829 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24830 sense defined by the ABI:
24831
24832 "An expression is instantiation-dependent if it is type-dependent
24833 or value-dependent, or it has a subexpression that is type-dependent
24834 or value-dependent."
24835
24836 Except don't actually check value-dependence for unevaluated expressions,
24837 because in sizeof(i) we don't care about the value of i. Checking
24838 type-dependence will in turn check value-dependence of array bounds/template
24839 arguments as needed. */
24840
24841 bool
24842 instantiation_dependent_uneval_expression_p (tree expression)
24843 {
24844 tree result;
24845
24846 if (!processing_template_decl)
24847 return false;
24848
24849 if (expression == error_mark_node)
24850 return false;
24851
24852 result = cp_walk_tree_without_duplicates (&expression,
24853 instantiation_dependent_r, NULL);
24854 return result != NULL_TREE;
24855 }
24856
24857 /* As above, but also check value-dependence of the expression as a whole. */
24858
24859 bool
24860 instantiation_dependent_expression_p (tree expression)
24861 {
24862 return (instantiation_dependent_uneval_expression_p (expression)
24863 || value_dependent_expression_p (expression));
24864 }
24865
24866 /* Like type_dependent_expression_p, but it also works while not processing
24867 a template definition, i.e. during substitution or mangling. */
24868
24869 bool
24870 type_dependent_expression_p_push (tree expr)
24871 {
24872 bool b;
24873 ++processing_template_decl;
24874 b = type_dependent_expression_p (expr);
24875 --processing_template_decl;
24876 return b;
24877 }
24878
24879 /* Returns TRUE if ARGS contains a type-dependent expression. */
24880
24881 bool
24882 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24883 {
24884 unsigned int i;
24885 tree arg;
24886
24887 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24888 {
24889 if (type_dependent_expression_p (arg))
24890 return true;
24891 }
24892 return false;
24893 }
24894
24895 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24896 expressions) contains any type-dependent expressions. */
24897
24898 bool
24899 any_type_dependent_elements_p (const_tree list)
24900 {
24901 for (; list; list = TREE_CHAIN (list))
24902 if (type_dependent_expression_p (TREE_VALUE (list)))
24903 return true;
24904
24905 return false;
24906 }
24907
24908 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24909 expressions) contains any value-dependent expressions. */
24910
24911 bool
24912 any_value_dependent_elements_p (const_tree list)
24913 {
24914 for (; list; list = TREE_CHAIN (list))
24915 if (value_dependent_expression_p (TREE_VALUE (list)))
24916 return true;
24917
24918 return false;
24919 }
24920
24921 /* Returns TRUE if the ARG (a template argument) is dependent. */
24922
24923 bool
24924 dependent_template_arg_p (tree arg)
24925 {
24926 if (!processing_template_decl)
24927 return false;
24928
24929 /* Assume a template argument that was wrongly written by the user
24930 is dependent. This is consistent with what
24931 any_dependent_template_arguments_p [that calls this function]
24932 does. */
24933 if (!arg || arg == error_mark_node)
24934 return true;
24935
24936 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24937 arg = argument_pack_select_arg (arg);
24938
24939 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24940 return true;
24941 if (TREE_CODE (arg) == TEMPLATE_DECL)
24942 {
24943 if (DECL_TEMPLATE_PARM_P (arg))
24944 return true;
24945 /* A member template of a dependent class is not necessarily
24946 type-dependent, but it is a dependent template argument because it
24947 will be a member of an unknown specialization to that template. */
24948 tree scope = CP_DECL_CONTEXT (arg);
24949 return TYPE_P (scope) && dependent_type_p (scope);
24950 }
24951 else if (ARGUMENT_PACK_P (arg))
24952 {
24953 tree args = ARGUMENT_PACK_ARGS (arg);
24954 int i, len = TREE_VEC_LENGTH (args);
24955 for (i = 0; i < len; ++i)
24956 {
24957 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24958 return true;
24959 }
24960
24961 return false;
24962 }
24963 else if (TYPE_P (arg))
24964 return dependent_type_p (arg);
24965 else
24966 return (type_dependent_expression_p (arg)
24967 || value_dependent_expression_p (arg));
24968 }
24969
24970 /* Returns true if ARGS (a collection of template arguments) contains
24971 any types that require structural equality testing. */
24972
24973 bool
24974 any_template_arguments_need_structural_equality_p (tree args)
24975 {
24976 int i;
24977 int j;
24978
24979 if (!args)
24980 return false;
24981 if (args == error_mark_node)
24982 return true;
24983
24984 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24985 {
24986 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24987 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24988 {
24989 tree arg = TREE_VEC_ELT (level, j);
24990 tree packed_args = NULL_TREE;
24991 int k, len = 1;
24992
24993 if (ARGUMENT_PACK_P (arg))
24994 {
24995 /* Look inside the argument pack. */
24996 packed_args = ARGUMENT_PACK_ARGS (arg);
24997 len = TREE_VEC_LENGTH (packed_args);
24998 }
24999
25000 for (k = 0; k < len; ++k)
25001 {
25002 if (packed_args)
25003 arg = TREE_VEC_ELT (packed_args, k);
25004
25005 if (error_operand_p (arg))
25006 return true;
25007 else if (TREE_CODE (arg) == TEMPLATE_DECL)
25008 continue;
25009 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
25010 return true;
25011 else if (!TYPE_P (arg) && TREE_TYPE (arg)
25012 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
25013 return true;
25014 }
25015 }
25016 }
25017
25018 return false;
25019 }
25020
25021 /* Returns true if ARGS (a collection of template arguments) contains
25022 any dependent arguments. */
25023
25024 bool
25025 any_dependent_template_arguments_p (const_tree args)
25026 {
25027 int i;
25028 int j;
25029
25030 if (!args)
25031 return false;
25032 if (args == error_mark_node)
25033 return true;
25034
25035 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25036 {
25037 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25038 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25039 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
25040 return true;
25041 }
25042
25043 return false;
25044 }
25045
25046 /* Returns TRUE if the template TMPL is type-dependent. */
25047
25048 bool
25049 dependent_template_p (tree tmpl)
25050 {
25051 if (TREE_CODE (tmpl) == OVERLOAD)
25052 {
25053 for (lkp_iterator iter (tmpl); iter; ++iter)
25054 if (dependent_template_p (*iter))
25055 return true;
25056 return false;
25057 }
25058
25059 /* Template template parameters are dependent. */
25060 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
25061 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
25062 return true;
25063 /* So are names that have not been looked up. */
25064 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
25065 return true;
25066 return false;
25067 }
25068
25069 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25070
25071 bool
25072 dependent_template_id_p (tree tmpl, tree args)
25073 {
25074 return (dependent_template_p (tmpl)
25075 || any_dependent_template_arguments_p (args));
25076 }
25077
25078 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25079 are dependent. */
25080
25081 bool
25082 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
25083 {
25084 int i;
25085
25086 if (!processing_template_decl)
25087 return false;
25088
25089 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
25090 {
25091 tree decl = TREE_VEC_ELT (declv, i);
25092 tree init = TREE_VEC_ELT (initv, i);
25093 tree cond = TREE_VEC_ELT (condv, i);
25094 tree incr = TREE_VEC_ELT (incrv, i);
25095
25096 if (type_dependent_expression_p (decl)
25097 || TREE_CODE (decl) == SCOPE_REF)
25098 return true;
25099
25100 if (init && type_dependent_expression_p (init))
25101 return true;
25102
25103 if (type_dependent_expression_p (cond))
25104 return true;
25105
25106 if (COMPARISON_CLASS_P (cond)
25107 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
25108 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
25109 return true;
25110
25111 if (TREE_CODE (incr) == MODOP_EXPR)
25112 {
25113 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
25114 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
25115 return true;
25116 }
25117 else if (type_dependent_expression_p (incr))
25118 return true;
25119 else if (TREE_CODE (incr) == MODIFY_EXPR)
25120 {
25121 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
25122 return true;
25123 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
25124 {
25125 tree t = TREE_OPERAND (incr, 1);
25126 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
25127 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
25128 return true;
25129 }
25130 }
25131 }
25132
25133 return false;
25134 }
25135
25136 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25137 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25138 no such TYPE can be found. Note that this function peers inside
25139 uninstantiated templates and therefore should be used only in
25140 extremely limited situations. ONLY_CURRENT_P restricts this
25141 peering to the currently open classes hierarchy (which is required
25142 when comparing types). */
25143
25144 tree
25145 resolve_typename_type (tree type, bool only_current_p)
25146 {
25147 tree scope;
25148 tree name;
25149 tree decl;
25150 int quals;
25151 tree pushed_scope;
25152 tree result;
25153
25154 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
25155
25156 scope = TYPE_CONTEXT (type);
25157 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25158 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25159 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25160 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25161 identifier of the TYPENAME_TYPE anymore.
25162 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25163 TYPENAME_TYPE instead, we avoid messing up with a possible
25164 typedef variant case. */
25165 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
25166
25167 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25168 it first before we can figure out what NAME refers to. */
25169 if (TREE_CODE (scope) == TYPENAME_TYPE)
25170 {
25171 if (TYPENAME_IS_RESOLVING_P (scope))
25172 /* Given a class template A with a dependent base with nested type C,
25173 typedef typename A::C::C C will land us here, as trying to resolve
25174 the initial A::C leads to the local C typedef, which leads back to
25175 A::C::C. So we break the recursion now. */
25176 return type;
25177 else
25178 scope = resolve_typename_type (scope, only_current_p);
25179 }
25180 /* If we don't know what SCOPE refers to, then we cannot resolve the
25181 TYPENAME_TYPE. */
25182 if (!CLASS_TYPE_P (scope))
25183 return type;
25184 /* If this is a typedef, we don't want to look inside (c++/11987). */
25185 if (typedef_variant_p (type))
25186 return type;
25187 /* If SCOPE isn't the template itself, it will not have a valid
25188 TYPE_FIELDS list. */
25189 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
25190 /* scope is either the template itself or a compatible instantiation
25191 like X<T>, so look up the name in the original template. */
25192 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
25193 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25194 gcc_checking_assert (uses_template_parms (scope));
25195 /* If scope has no fields, it can't be a current instantiation. Check this
25196 before currently_open_class to avoid infinite recursion (71515). */
25197 if (!TYPE_FIELDS (scope))
25198 return type;
25199 /* If the SCOPE is not the current instantiation, there's no reason
25200 to look inside it. */
25201 if (only_current_p && !currently_open_class (scope))
25202 return type;
25203 /* Enter the SCOPE so that name lookup will be resolved as if we
25204 were in the class definition. In particular, SCOPE will no
25205 longer be considered a dependent type. */
25206 pushed_scope = push_scope (scope);
25207 /* Look up the declaration. */
25208 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
25209 tf_warning_or_error);
25210
25211 result = NULL_TREE;
25212
25213 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25214 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25215 tree fullname = TYPENAME_TYPE_FULLNAME (type);
25216 if (!decl)
25217 /*nop*/;
25218 else if (identifier_p (fullname)
25219 && TREE_CODE (decl) == TYPE_DECL)
25220 {
25221 result = TREE_TYPE (decl);
25222 if (result == error_mark_node)
25223 result = NULL_TREE;
25224 }
25225 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25226 && DECL_CLASS_TEMPLATE_P (decl))
25227 {
25228 /* Obtain the template and the arguments. */
25229 tree tmpl = TREE_OPERAND (fullname, 0);
25230 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25231 {
25232 /* We get here with a plain identifier because a previous tentative
25233 parse of the nested-name-specifier as part of a ptr-operator saw
25234 ::template X<A>. The use of ::template is necessary in a
25235 ptr-operator, but wrong in a declarator-id.
25236
25237 [temp.names]: In a qualified-id of a declarator-id, the keyword
25238 template shall not appear at the top level. */
25239 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
25240 "keyword %<template%> not allowed in declarator-id");
25241 tmpl = decl;
25242 }
25243 tree args = TREE_OPERAND (fullname, 1);
25244 /* Instantiate the template. */
25245 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25246 /*entering_scope=*/true,
25247 tf_error | tf_user);
25248 if (result == error_mark_node)
25249 result = NULL_TREE;
25250 }
25251
25252 /* Leave the SCOPE. */
25253 if (pushed_scope)
25254 pop_scope (pushed_scope);
25255
25256 /* If we failed to resolve it, return the original typename. */
25257 if (!result)
25258 return type;
25259
25260 /* If lookup found a typename type, resolve that too. */
25261 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25262 {
25263 /* Ill-formed programs can cause infinite recursion here, so we
25264 must catch that. */
25265 TYPENAME_IS_RESOLVING_P (result) = 1;
25266 result = resolve_typename_type (result, only_current_p);
25267 TYPENAME_IS_RESOLVING_P (result) = 0;
25268 }
25269
25270 /* Qualify the resulting type. */
25271 quals = cp_type_quals (type);
25272 if (quals)
25273 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25274
25275 return result;
25276 }
25277
25278 /* EXPR is an expression which is not type-dependent. Return a proxy
25279 for EXPR that can be used to compute the types of larger
25280 expressions containing EXPR. */
25281
25282 tree
25283 build_non_dependent_expr (tree expr)
25284 {
25285 tree orig_expr = expr;
25286 tree inner_expr;
25287
25288 /* When checking, try to get a constant value for all non-dependent
25289 expressions in order to expose bugs in *_dependent_expression_p
25290 and constexpr. This can affect code generation, see PR70704, so
25291 only do this for -fchecking=2. */
25292 if (flag_checking > 1
25293 && cxx_dialect >= cxx11
25294 /* Don't do this during nsdmi parsing as it can lead to
25295 unexpected recursive instantiations. */
25296 && !parsing_nsdmi ()
25297 /* Don't do this during concept expansion either and for
25298 the same reason. */
25299 && !expanding_concept ())
25300 fold_non_dependent_expr (expr);
25301
25302 STRIP_ANY_LOCATION_WRAPPER (expr);
25303
25304 /* Preserve OVERLOADs; the functions must be available to resolve
25305 types. */
25306 inner_expr = expr;
25307 if (TREE_CODE (inner_expr) == STMT_EXPR)
25308 inner_expr = stmt_expr_value_expr (inner_expr);
25309 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25310 inner_expr = TREE_OPERAND (inner_expr, 0);
25311 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25312 inner_expr = TREE_OPERAND (inner_expr, 1);
25313 if (is_overloaded_fn (inner_expr)
25314 || TREE_CODE (inner_expr) == OFFSET_REF)
25315 return orig_expr;
25316 /* There is no need to return a proxy for a variable. */
25317 if (VAR_P (expr))
25318 return orig_expr;
25319 /* Preserve string constants; conversions from string constants to
25320 "char *" are allowed, even though normally a "const char *"
25321 cannot be used to initialize a "char *". */
25322 if (TREE_CODE (expr) == STRING_CST)
25323 return orig_expr;
25324 /* Preserve void and arithmetic constants, as an optimization -- there is no
25325 reason to create a new node. */
25326 if (TREE_CODE (expr) == VOID_CST
25327 || TREE_CODE (expr) == INTEGER_CST
25328 || TREE_CODE (expr) == REAL_CST)
25329 return orig_expr;
25330 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25331 There is at least one place where we want to know that a
25332 particular expression is a throw-expression: when checking a ?:
25333 expression, there are special rules if the second or third
25334 argument is a throw-expression. */
25335 if (TREE_CODE (expr) == THROW_EXPR)
25336 return orig_expr;
25337
25338 /* Don't wrap an initializer list, we need to be able to look inside. */
25339 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25340 return orig_expr;
25341
25342 /* Don't wrap a dummy object, we need to be able to test for it. */
25343 if (is_dummy_object (expr))
25344 return orig_expr;
25345
25346 if (TREE_CODE (expr) == COND_EXPR)
25347 return build3 (COND_EXPR,
25348 TREE_TYPE (expr),
25349 TREE_OPERAND (expr, 0),
25350 (TREE_OPERAND (expr, 1)
25351 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25352 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25353 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25354 if (TREE_CODE (expr) == COMPOUND_EXPR
25355 && !COMPOUND_EXPR_OVERLOADED (expr))
25356 return build2 (COMPOUND_EXPR,
25357 TREE_TYPE (expr),
25358 TREE_OPERAND (expr, 0),
25359 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25360
25361 /* If the type is unknown, it can't really be non-dependent */
25362 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25363
25364 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25365 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25366 }
25367
25368 /* ARGS is a vector of expressions as arguments to a function call.
25369 Replace the arguments with equivalent non-dependent expressions.
25370 This modifies ARGS in place. */
25371
25372 void
25373 make_args_non_dependent (vec<tree, va_gc> *args)
25374 {
25375 unsigned int ix;
25376 tree arg;
25377
25378 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25379 {
25380 tree newarg = build_non_dependent_expr (arg);
25381 if (newarg != arg)
25382 (*args)[ix] = newarg;
25383 }
25384 }
25385
25386 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25387 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25388 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25389
25390 static tree
25391 make_auto_1 (tree name, bool set_canonical)
25392 {
25393 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25394 TYPE_NAME (au) = build_decl (input_location,
25395 TYPE_DECL, name, au);
25396 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25397 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25398 (0, processing_template_decl + 1, processing_template_decl + 1,
25399 TYPE_NAME (au), NULL_TREE);
25400 if (set_canonical)
25401 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25402 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25403 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25404
25405 return au;
25406 }
25407
25408 tree
25409 make_decltype_auto (void)
25410 {
25411 return make_auto_1 (decltype_auto_identifier, true);
25412 }
25413
25414 tree
25415 make_auto (void)
25416 {
25417 return make_auto_1 (auto_identifier, true);
25418 }
25419
25420 /* Return a C++17 deduction placeholder for class template TMPL. */
25421
25422 tree
25423 make_template_placeholder (tree tmpl)
25424 {
25425 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25426 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25427 return t;
25428 }
25429
25430 /* True iff T is a C++17 class template deduction placeholder. */
25431
25432 bool
25433 template_placeholder_p (tree t)
25434 {
25435 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25436 }
25437
25438 /* Make a "constrained auto" type-specifier. This is an
25439 auto type with constraints that must be associated after
25440 deduction. The constraint is formed from the given
25441 CONC and its optional sequence of arguments, which are
25442 non-null if written as partial-concept-id. */
25443
25444 tree
25445 make_constrained_auto (tree con, tree args)
25446 {
25447 tree type = make_auto_1 (auto_identifier, false);
25448
25449 /* Build the constraint. */
25450 tree tmpl = DECL_TI_TEMPLATE (con);
25451 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25452 expr = build_concept_check (expr, type, args);
25453
25454 tree constr = normalize_expression (expr);
25455 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25456
25457 /* Our canonical type depends on the constraint. */
25458 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25459
25460 /* Attach the constraint to the type declaration. */
25461 tree decl = TYPE_NAME (type);
25462 return decl;
25463 }
25464
25465 /* Given type ARG, return std::initializer_list<ARG>. */
25466
25467 static tree
25468 listify (tree arg)
25469 {
25470 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25471
25472 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25473 {
25474 gcc_rich_location richloc (input_location);
25475 maybe_add_include_fixit (&richloc, "<initializer_list>");
25476 error_at (&richloc,
25477 "deducing from brace-enclosed initializer list"
25478 " requires %<#include <initializer_list>%>");
25479
25480 return error_mark_node;
25481 }
25482 tree argvec = make_tree_vec (1);
25483 TREE_VEC_ELT (argvec, 0) = arg;
25484
25485 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25486 NULL_TREE, 0, tf_warning_or_error);
25487 }
25488
25489 /* Replace auto in TYPE with std::initializer_list<auto>. */
25490
25491 static tree
25492 listify_autos (tree type, tree auto_node)
25493 {
25494 tree init_auto = listify (auto_node);
25495 tree argvec = make_tree_vec (1);
25496 TREE_VEC_ELT (argvec, 0) = init_auto;
25497 if (processing_template_decl)
25498 argvec = add_to_template_args (current_template_args (), argvec);
25499 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25500 }
25501
25502 /* Hash traits for hashing possibly constrained 'auto'
25503 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25504
25505 struct auto_hash : default_hash_traits<tree>
25506 {
25507 static inline hashval_t hash (tree);
25508 static inline bool equal (tree, tree);
25509 };
25510
25511 /* Hash the 'auto' T. */
25512
25513 inline hashval_t
25514 auto_hash::hash (tree t)
25515 {
25516 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25517 /* Matching constrained-type-specifiers denote the same template
25518 parameter, so hash the constraint. */
25519 return hash_placeholder_constraint (c);
25520 else
25521 /* But unconstrained autos are all separate, so just hash the pointer. */
25522 return iterative_hash_object (t, 0);
25523 }
25524
25525 /* Compare two 'auto's. */
25526
25527 inline bool
25528 auto_hash::equal (tree t1, tree t2)
25529 {
25530 if (t1 == t2)
25531 return true;
25532
25533 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25534 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25535
25536 /* Two unconstrained autos are distinct. */
25537 if (!c1 || !c2)
25538 return false;
25539
25540 return equivalent_placeholder_constraints (c1, c2);
25541 }
25542
25543 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25544 constrained) auto, add it to the vector. */
25545
25546 static int
25547 extract_autos_r (tree t, void *data)
25548 {
25549 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25550 if (is_auto (t))
25551 {
25552 /* All the autos were built with index 0; fix that up now. */
25553 tree *p = hash.find_slot (t, INSERT);
25554 unsigned idx;
25555 if (*p)
25556 /* If this is a repeated constrained-type-specifier, use the index we
25557 chose before. */
25558 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25559 else
25560 {
25561 /* Otherwise this is new, so use the current count. */
25562 *p = t;
25563 idx = hash.elements () - 1;
25564 }
25565 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25566 }
25567
25568 /* Always keep walking. */
25569 return 0;
25570 }
25571
25572 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25573 says they can appear anywhere in the type. */
25574
25575 static tree
25576 extract_autos (tree type)
25577 {
25578 hash_set<tree> visited;
25579 hash_table<auto_hash> hash (2);
25580
25581 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25582
25583 tree tree_vec = make_tree_vec (hash.elements());
25584 for (hash_table<auto_hash>::iterator iter = hash.begin();
25585 iter != hash.end(); ++iter)
25586 {
25587 tree elt = *iter;
25588 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25589 TREE_VEC_ELT (tree_vec, i)
25590 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25591 }
25592
25593 return tree_vec;
25594 }
25595
25596 /* The stem for deduction guide names. */
25597 const char *const dguide_base = "__dguide_";
25598
25599 /* Return the name for a deduction guide for class template TMPL. */
25600
25601 tree
25602 dguide_name (tree tmpl)
25603 {
25604 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25605 tree tname = TYPE_IDENTIFIER (type);
25606 char *buf = (char *) alloca (1 + strlen (dguide_base)
25607 + IDENTIFIER_LENGTH (tname));
25608 memcpy (buf, dguide_base, strlen (dguide_base));
25609 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25610 IDENTIFIER_LENGTH (tname) + 1);
25611 tree dname = get_identifier (buf);
25612 TREE_TYPE (dname) = type;
25613 return dname;
25614 }
25615
25616 /* True if NAME is the name of a deduction guide. */
25617
25618 bool
25619 dguide_name_p (tree name)
25620 {
25621 return (TREE_CODE (name) == IDENTIFIER_NODE
25622 && TREE_TYPE (name)
25623 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25624 strlen (dguide_base)));
25625 }
25626
25627 /* True if FN is a deduction guide. */
25628
25629 bool
25630 deduction_guide_p (const_tree fn)
25631 {
25632 if (DECL_P (fn))
25633 if (tree name = DECL_NAME (fn))
25634 return dguide_name_p (name);
25635 return false;
25636 }
25637
25638 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25639
25640 bool
25641 copy_guide_p (const_tree fn)
25642 {
25643 gcc_assert (deduction_guide_p (fn));
25644 if (!DECL_ARTIFICIAL (fn))
25645 return false;
25646 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25647 return (TREE_CHAIN (parms) == void_list_node
25648 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25649 }
25650
25651 /* True if FN is a guide generated from a constructor template. */
25652
25653 bool
25654 template_guide_p (const_tree fn)
25655 {
25656 gcc_assert (deduction_guide_p (fn));
25657 if (!DECL_ARTIFICIAL (fn))
25658 return false;
25659 tree tmpl = DECL_TI_TEMPLATE (fn);
25660 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25661 return PRIMARY_TEMPLATE_P (org);
25662 return false;
25663 }
25664
25665 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25666 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25667 template parameter types. Note that the handling of template template
25668 parameters relies on current_template_parms being set appropriately for the
25669 new template. */
25670
25671 static tree
25672 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25673 tree tsubst_args, tsubst_flags_t complain)
25674 {
25675 tree oldidx = get_template_parm_index (olddecl);
25676
25677 tree newtype;
25678 if (TREE_CODE (olddecl) == TYPE_DECL
25679 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25680 {
25681 tree oldtype = TREE_TYPE (olddecl);
25682 newtype = cxx_make_type (TREE_CODE (oldtype));
25683 TYPE_MAIN_VARIANT (newtype) = newtype;
25684 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25685 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25686 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25687 }
25688 else
25689 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25690 complain, NULL_TREE);
25691
25692 tree newdecl
25693 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25694 DECL_NAME (olddecl), newtype);
25695 SET_DECL_TEMPLATE_PARM_P (newdecl);
25696
25697 tree newidx;
25698 if (TREE_CODE (olddecl) == TYPE_DECL
25699 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25700 {
25701 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25702 = build_template_parm_index (index, level, level,
25703 newdecl, newtype);
25704 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25705 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25706 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25707 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25708
25709 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25710 {
25711 DECL_TEMPLATE_RESULT (newdecl)
25712 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25713 DECL_NAME (olddecl), newtype);
25714 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25715 // First create a copy (ttargs) of tsubst_args with an
25716 // additional level for the template template parameter's own
25717 // template parameters (ttparms).
25718 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25719 (DECL_TEMPLATE_PARMS (olddecl)));
25720 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25721 tree ttargs = make_tree_vec (depth + 1);
25722 for (int i = 0; i < depth; ++i)
25723 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25724 TREE_VEC_ELT (ttargs, depth)
25725 = template_parms_level_to_args (ttparms);
25726 // Substitute ttargs into ttparms to fix references to
25727 // other template parameters.
25728 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25729 complain|tf_partial);
25730 // Now substitute again with args based on tparms, to reduce
25731 // the level of the ttparms.
25732 ttargs = current_template_args ();
25733 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25734 complain);
25735 // Finally, tack the adjusted parms onto tparms.
25736 ttparms = tree_cons (size_int (depth), ttparms,
25737 current_template_parms);
25738 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25739 }
25740 }
25741 else
25742 {
25743 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25744 tree newconst
25745 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25746 TREE_CODE (oldconst),
25747 DECL_NAME (oldconst), newtype);
25748 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25749 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25750 SET_DECL_TEMPLATE_PARM_P (newconst);
25751 newidx = build_template_parm_index (index, level, level,
25752 newconst, newtype);
25753 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25754 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25755 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25756 }
25757
25758 return newdecl;
25759 }
25760
25761 /* Returns a C++17 class deduction guide template based on the constructor
25762 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25763 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25764
25765 static tree
25766 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25767 {
25768 tree type, tparms, targs, fparms, fargs, ci;
25769 bool memtmpl = false;
25770 bool explicit_p;
25771 location_t loc;
25772 tree fn_tmpl = NULL_TREE;
25773
25774 if (TYPE_P (ctor))
25775 {
25776 type = ctor;
25777 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25778 if (copy_p)
25779 {
25780 type = TREE_TYPE (type);
25781 fparms = tree_cons (NULL_TREE, type, void_list_node);
25782 }
25783 else
25784 fparms = void_list_node;
25785
25786 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25787 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25788 targs = CLASSTYPE_TI_ARGS (type);
25789 ci = NULL_TREE;
25790 fargs = NULL_TREE;
25791 loc = DECL_SOURCE_LOCATION (ctmpl);
25792 explicit_p = false;
25793 }
25794 else
25795 {
25796 ++processing_template_decl;
25797
25798 fn_tmpl
25799 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25800 : DECL_TI_TEMPLATE (ctor));
25801 if (outer_args)
25802 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25803 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25804
25805 type = DECL_CONTEXT (ctor);
25806
25807 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25808 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25809 fully specialized args for the enclosing class. Strip those off, as
25810 the deduction guide won't have those template parameters. */
25811 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25812 TMPL_PARMS_DEPTH (tparms));
25813 /* Discard the 'this' parameter. */
25814 fparms = FUNCTION_ARG_CHAIN (ctor);
25815 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25816 ci = get_constraints (ctor);
25817 loc = DECL_SOURCE_LOCATION (ctor);
25818 explicit_p = DECL_NONCONVERTING_P (ctor);
25819
25820 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25821 {
25822 memtmpl = true;
25823
25824 /* For a member template constructor, we need to flatten the two
25825 template parameter lists into one, and then adjust the function
25826 signature accordingly. This gets...complicated. */
25827 tree save_parms = current_template_parms;
25828
25829 /* For a member template we should have two levels of parms/args, one
25830 for the class and one for the constructor. We stripped
25831 specialized args for further enclosing classes above. */
25832 const int depth = 2;
25833 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25834
25835 /* Template args for translating references to the two-level template
25836 parameters into references to the one-level template parameters we
25837 are creating. */
25838 tree tsubst_args = copy_node (targs);
25839 TMPL_ARGS_LEVEL (tsubst_args, depth)
25840 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25841
25842 /* Template parms for the constructor template. */
25843 tree ftparms = TREE_VALUE (tparms);
25844 unsigned flen = TREE_VEC_LENGTH (ftparms);
25845 /* Template parms for the class template. */
25846 tparms = TREE_CHAIN (tparms);
25847 tree ctparms = TREE_VALUE (tparms);
25848 unsigned clen = TREE_VEC_LENGTH (ctparms);
25849 /* Template parms for the deduction guide start as a copy of the
25850 template parms for the class. We set current_template_parms for
25851 lookup_template_class_1. */
25852 current_template_parms = tparms = copy_node (tparms);
25853 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25854 for (unsigned i = 0; i < clen; ++i)
25855 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25856
25857 /* Now we need to rewrite the constructor parms to append them to the
25858 class parms. */
25859 for (unsigned i = 0; i < flen; ++i)
25860 {
25861 unsigned index = i + clen;
25862 unsigned level = 1;
25863 tree oldelt = TREE_VEC_ELT (ftparms, i);
25864 tree olddecl = TREE_VALUE (oldelt);
25865 tree newdecl = rewrite_template_parm (olddecl, index, level,
25866 tsubst_args, complain);
25867 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25868 tsubst_args, complain, ctor);
25869 tree list = build_tree_list (newdef, newdecl);
25870 TEMPLATE_PARM_CONSTRAINTS (list)
25871 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25872 tsubst_args, complain, ctor);
25873 TREE_VEC_ELT (new_vec, index) = list;
25874 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25875 }
25876
25877 /* Now we have a final set of template parms to substitute into the
25878 function signature. */
25879 targs = template_parms_to_args (tparms);
25880 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25881 complain, ctor);
25882 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25883 if (ci)
25884 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25885
25886 current_template_parms = save_parms;
25887 }
25888 --processing_template_decl;
25889 }
25890
25891 if (!memtmpl)
25892 {
25893 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25894 tparms = copy_node (tparms);
25895 INNERMOST_TEMPLATE_PARMS (tparms)
25896 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25897 }
25898
25899 tree fntype = build_function_type (type, fparms);
25900 tree ded_fn = build_lang_decl_loc (loc,
25901 FUNCTION_DECL,
25902 dguide_name (type), fntype);
25903 DECL_ARGUMENTS (ded_fn) = fargs;
25904 DECL_ARTIFICIAL (ded_fn) = true;
25905 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25906 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25907 DECL_ARTIFICIAL (ded_tmpl) = true;
25908 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25909 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25910 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25911 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25912 if (DECL_P (ctor))
25913 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25914 if (ci)
25915 set_constraints (ded_tmpl, ci);
25916
25917 return ded_tmpl;
25918 }
25919
25920 /* Deduce template arguments for the class template placeholder PTYPE for
25921 template TMPL based on the initializer INIT, and return the resulting
25922 type. */
25923
25924 static tree
25925 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25926 tsubst_flags_t complain)
25927 {
25928 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25929 {
25930 /* We should have handled this in the caller. */
25931 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25932 return ptype;
25933 if (complain & tf_error)
25934 error ("non-class template %qT used without template arguments", tmpl);
25935 return error_mark_node;
25936 }
25937
25938 tree type = TREE_TYPE (tmpl);
25939
25940 bool try_list_ctor = false;
25941
25942 vec<tree,va_gc> *args;
25943 if (init == NULL_TREE
25944 || TREE_CODE (init) == TREE_LIST)
25945 args = make_tree_vector_from_list (init);
25946 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25947 {
25948 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25949 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25950 {
25951 /* As an exception, the first phase in 16.3.1.7 (considering the
25952 initializer list as a single argument) is omitted if the
25953 initializer list consists of a single expression of type cv U,
25954 where U is a specialization of C or a class derived from a
25955 specialization of C. */
25956 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25957 tree etype = TREE_TYPE (elt);
25958
25959 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25960 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25961 int err = unify (tparms, targs, type, etype,
25962 UNIFY_ALLOW_DERIVED, /*explain*/false);
25963 if (err == 0)
25964 try_list_ctor = false;
25965 ggc_free (targs);
25966 }
25967 if (try_list_ctor || is_std_init_list (type))
25968 args = make_tree_vector_single (init);
25969 else
25970 args = make_tree_vector_from_ctor (init);
25971 }
25972 else
25973 args = make_tree_vector_single (init);
25974
25975 tree dname = dguide_name (tmpl);
25976 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25977 /*type*/false, /*complain*/false,
25978 /*hidden*/false);
25979 bool elided = false;
25980 if (cands == error_mark_node)
25981 cands = NULL_TREE;
25982
25983 /* Prune explicit deduction guides in copy-initialization context. */
25984 if (flags & LOOKUP_ONLYCONVERTING)
25985 {
25986 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25987 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25988 elided = true;
25989
25990 if (elided)
25991 {
25992 /* Found a nonconverting guide, prune the candidates. */
25993 tree pruned = NULL_TREE;
25994 for (lkp_iterator iter (cands); iter; ++iter)
25995 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25996 pruned = lookup_add (*iter, pruned);
25997
25998 cands = pruned;
25999 }
26000 }
26001
26002 tree outer_args = NULL_TREE;
26003 if (DECL_CLASS_SCOPE_P (tmpl)
26004 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
26005 {
26006 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
26007 type = TREE_TYPE (most_general_template (tmpl));
26008 }
26009
26010 bool saw_ctor = false;
26011 // FIXME cache artificial deduction guides
26012 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
26013 {
26014 tree guide = build_deduction_guide (*iter, outer_args, complain);
26015 if ((flags & LOOKUP_ONLYCONVERTING)
26016 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
26017 elided = true;
26018 else
26019 cands = lookup_add (guide, cands);
26020
26021 saw_ctor = true;
26022 }
26023
26024 tree call = error_mark_node;
26025
26026 /* If this is list-initialization and the class has a list constructor, first
26027 try deducing from the list as a single argument, as [over.match.list]. */
26028 tree list_cands = NULL_TREE;
26029 if (try_list_ctor && cands)
26030 for (lkp_iterator iter (cands); iter; ++iter)
26031 {
26032 tree dg = *iter;
26033 if (is_list_ctor (dg))
26034 list_cands = lookup_add (dg, list_cands);
26035 }
26036 if (list_cands)
26037 {
26038 ++cp_unevaluated_operand;
26039 call = build_new_function_call (list_cands, &args, tf_decltype);
26040 --cp_unevaluated_operand;
26041
26042 if (call == error_mark_node)
26043 {
26044 /* That didn't work, now try treating the list as a sequence of
26045 arguments. */
26046 release_tree_vector (args);
26047 args = make_tree_vector_from_ctor (init);
26048 }
26049 }
26050
26051 /* Maybe generate an implicit deduction guide. */
26052 if (call == error_mark_node && args->length () < 2)
26053 {
26054 tree gtype = NULL_TREE;
26055
26056 if (args->length () == 1)
26057 /* Generate a copy guide. */
26058 gtype = build_reference_type (type);
26059 else if (!saw_ctor)
26060 /* Generate a default guide. */
26061 gtype = type;
26062
26063 if (gtype)
26064 {
26065 tree guide = build_deduction_guide (gtype, outer_args, complain);
26066 cands = lookup_add (guide, cands);
26067 }
26068 }
26069
26070 if (elided && !cands)
26071 {
26072 error ("cannot deduce template arguments for copy-initialization"
26073 " of %qT, as it has no non-explicit deduction guides or "
26074 "user-declared constructors", type);
26075 return error_mark_node;
26076 }
26077 else if (!cands && call == error_mark_node)
26078 {
26079 error ("cannot deduce template arguments of %qT, as it has no viable "
26080 "deduction guides", type);
26081 return error_mark_node;
26082 }
26083
26084 if (call == error_mark_node)
26085 {
26086 ++cp_unevaluated_operand;
26087 call = build_new_function_call (cands, &args, tf_decltype);
26088 --cp_unevaluated_operand;
26089 }
26090
26091 if (call == error_mark_node && (complain & tf_warning_or_error))
26092 {
26093 error ("class template argument deduction failed:");
26094
26095 ++cp_unevaluated_operand;
26096 call = build_new_function_call (cands, &args, complain | tf_decltype);
26097 --cp_unevaluated_operand;
26098
26099 if (elided)
26100 inform (input_location, "explicit deduction guides not considered "
26101 "for copy-initialization");
26102 }
26103
26104 release_tree_vector (args);
26105
26106 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
26107 }
26108
26109 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26110 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26111 The CONTEXT determines the context in which auto deduction is performed
26112 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26113 OUTER_TARGS are used during template argument deduction
26114 (context == adc_unify) to properly substitute the result, and is ignored
26115 in other contexts.
26116
26117 For partial-concept-ids, extra args may be appended to the list of deduced
26118 template arguments prior to determining constraint satisfaction. */
26119
26120 tree
26121 do_auto_deduction (tree type, tree init, tree auto_node,
26122 tsubst_flags_t complain, auto_deduction_context context,
26123 tree outer_targs, int flags)
26124 {
26125 tree targs;
26126
26127 if (init == error_mark_node)
26128 return error_mark_node;
26129
26130 if (init && type_dependent_expression_p (init)
26131 && context != adc_unify)
26132 /* Defining a subset of type-dependent expressions that we can deduce
26133 from ahead of time isn't worth the trouble. */
26134 return type;
26135
26136 /* Similarly, we can't deduce from another undeduced decl. */
26137 if (init && undeduced_auto_decl (init))
26138 return type;
26139
26140 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
26141 /* C++17 class template argument deduction. */
26142 return do_class_deduction (type, tmpl, init, flags, complain);
26143
26144 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
26145 /* Nothing we can do with this, even in deduction context. */
26146 return type;
26147
26148 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26149 with either a new invented type template parameter U or, if the
26150 initializer is a braced-init-list (8.5.4), with
26151 std::initializer_list<U>. */
26152 if (BRACE_ENCLOSED_INITIALIZER_P (init))
26153 {
26154 if (!DIRECT_LIST_INIT_P (init))
26155 type = listify_autos (type, auto_node);
26156 else if (CONSTRUCTOR_NELTS (init) == 1)
26157 init = CONSTRUCTOR_ELT (init, 0)->value;
26158 else
26159 {
26160 if (complain & tf_warning_or_error)
26161 {
26162 if (permerror (input_location, "direct-list-initialization of "
26163 "%<auto%> requires exactly one element"))
26164 inform (input_location,
26165 "for deduction to %<std::initializer_list%>, use copy-"
26166 "list-initialization (i.e. add %<=%> before the %<{%>)");
26167 }
26168 type = listify_autos (type, auto_node);
26169 }
26170 }
26171
26172 if (type == error_mark_node)
26173 return error_mark_node;
26174
26175 init = resolve_nondeduced_context (init, complain);
26176
26177 if (context == adc_decomp_type
26178 && auto_node == type
26179 && init != error_mark_node
26180 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
26181 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26182 and initializer has array type, deduce cv-qualified array type. */
26183 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
26184 complain);
26185 else if (AUTO_IS_DECLTYPE (auto_node))
26186 {
26187 bool id = (DECL_P (init)
26188 || ((TREE_CODE (init) == COMPONENT_REF
26189 || TREE_CODE (init) == SCOPE_REF)
26190 && !REF_PARENTHESIZED_P (init)));
26191 targs = make_tree_vec (1);
26192 TREE_VEC_ELT (targs, 0)
26193 = finish_decltype_type (init, id, tf_warning_or_error);
26194 if (type != auto_node)
26195 {
26196 if (complain & tf_error)
26197 error ("%qT as type rather than plain %<decltype(auto)%>", type);
26198 return error_mark_node;
26199 }
26200 }
26201 else
26202 {
26203 tree parms = build_tree_list (NULL_TREE, type);
26204 tree tparms;
26205
26206 if (flag_concepts)
26207 tparms = extract_autos (type);
26208 else
26209 {
26210 tparms = make_tree_vec (1);
26211 TREE_VEC_ELT (tparms, 0)
26212 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
26213 }
26214
26215 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26216 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
26217 DEDUCE_CALL, LOOKUP_NORMAL,
26218 NULL, /*explain_p=*/false);
26219 if (val > 0)
26220 {
26221 if (processing_template_decl)
26222 /* Try again at instantiation time. */
26223 return type;
26224 if (type && type != error_mark_node
26225 && (complain & tf_error))
26226 /* If type is error_mark_node a diagnostic must have been
26227 emitted by now. Also, having a mention to '<type error>'
26228 in the diagnostic is not really useful to the user. */
26229 {
26230 if (cfun && auto_node == current_function_auto_return_pattern
26231 && LAMBDA_FUNCTION_P (current_function_decl))
26232 error ("unable to deduce lambda return type from %qE", init);
26233 else
26234 error ("unable to deduce %qT from %qE", type, init);
26235 type_unification_real (tparms, targs, parms, &init, 1, 0,
26236 DEDUCE_CALL, LOOKUP_NORMAL,
26237 NULL, /*explain_p=*/true);
26238 }
26239 return error_mark_node;
26240 }
26241 }
26242
26243 /* Check any placeholder constraints against the deduced type. */
26244 if (flag_concepts && !processing_template_decl)
26245 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26246 {
26247 /* Use the deduced type to check the associated constraints. If we
26248 have a partial-concept-id, rebuild the argument list so that
26249 we check using the extra arguments. */
26250 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26251 tree cargs = CHECK_CONSTR_ARGS (constr);
26252 if (TREE_VEC_LENGTH (cargs) > 1)
26253 {
26254 cargs = copy_node (cargs);
26255 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26256 }
26257 else
26258 cargs = targs;
26259 if (!constraints_satisfied_p (constr, cargs))
26260 {
26261 if (complain & tf_warning_or_error)
26262 {
26263 switch (context)
26264 {
26265 case adc_unspecified:
26266 case adc_unify:
26267 error("placeholder constraints not satisfied");
26268 break;
26269 case adc_variable_type:
26270 case adc_decomp_type:
26271 error ("deduced initializer does not satisfy "
26272 "placeholder constraints");
26273 break;
26274 case adc_return_type:
26275 error ("deduced return type does not satisfy "
26276 "placeholder constraints");
26277 break;
26278 case adc_requirement:
26279 error ("deduced expression type does not satisfy "
26280 "placeholder constraints");
26281 break;
26282 }
26283 diagnose_constraints (input_location, constr, targs);
26284 }
26285 return error_mark_node;
26286 }
26287 }
26288
26289 if (processing_template_decl && context != adc_unify)
26290 outer_targs = current_template_args ();
26291 targs = add_to_template_args (outer_targs, targs);
26292 return tsubst (type, targs, complain, NULL_TREE);
26293 }
26294
26295 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26296 result. */
26297
26298 tree
26299 splice_late_return_type (tree type, tree late_return_type)
26300 {
26301 if (is_auto (type))
26302 {
26303 if (late_return_type)
26304 return late_return_type;
26305
26306 tree idx = get_template_parm_index (type);
26307 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26308 /* In an abbreviated function template we didn't know we were dealing
26309 with a function template when we saw the auto return type, so update
26310 it to have the correct level. */
26311 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26312 }
26313 return type;
26314 }
26315
26316 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26317 'decltype(auto)' or a deduced class template. */
26318
26319 bool
26320 is_auto (const_tree type)
26321 {
26322 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26323 && (TYPE_IDENTIFIER (type) == auto_identifier
26324 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26325 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26326 return true;
26327 else
26328 return false;
26329 }
26330
26331 /* for_each_template_parm callback for type_uses_auto. */
26332
26333 int
26334 is_auto_r (tree tp, void */*data*/)
26335 {
26336 return is_auto (tp);
26337 }
26338
26339 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26340 a use of `auto'. Returns NULL_TREE otherwise. */
26341
26342 tree
26343 type_uses_auto (tree type)
26344 {
26345 if (type == NULL_TREE)
26346 return NULL_TREE;
26347 else if (flag_concepts)
26348 {
26349 /* The Concepts TS allows multiple autos in one type-specifier; just
26350 return the first one we find, do_auto_deduction will collect all of
26351 them. */
26352 if (uses_template_parms (type))
26353 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26354 /*visited*/NULL, /*nondeduced*/true);
26355 else
26356 return NULL_TREE;
26357 }
26358 else
26359 return find_type_usage (type, is_auto);
26360 }
26361
26362 /* For a given template T, return the vector of typedefs referenced
26363 in T for which access check is needed at T instantiation time.
26364 T is either a FUNCTION_DECL or a RECORD_TYPE.
26365 Those typedefs were added to T by the function
26366 append_type_to_template_for_access_check. */
26367
26368 vec<qualified_typedef_usage_t, va_gc> *
26369 get_types_needing_access_check (tree t)
26370 {
26371 tree ti;
26372 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26373
26374 if (!t || t == error_mark_node)
26375 return NULL;
26376
26377 if (!(ti = get_template_info (t)))
26378 return NULL;
26379
26380 if (CLASS_TYPE_P (t)
26381 || TREE_CODE (t) == FUNCTION_DECL)
26382 {
26383 if (!TI_TEMPLATE (ti))
26384 return NULL;
26385
26386 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26387 }
26388
26389 return result;
26390 }
26391
26392 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26393 tied to T. That list of typedefs will be access checked at
26394 T instantiation time.
26395 T is either a FUNCTION_DECL or a RECORD_TYPE.
26396 TYPE_DECL is a TYPE_DECL node representing a typedef.
26397 SCOPE is the scope through which TYPE_DECL is accessed.
26398 LOCATION is the location of the usage point of TYPE_DECL.
26399
26400 This function is a subroutine of
26401 append_type_to_template_for_access_check. */
26402
26403 static void
26404 append_type_to_template_for_access_check_1 (tree t,
26405 tree type_decl,
26406 tree scope,
26407 location_t location)
26408 {
26409 qualified_typedef_usage_t typedef_usage;
26410 tree ti;
26411
26412 if (!t || t == error_mark_node)
26413 return;
26414
26415 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26416 || CLASS_TYPE_P (t))
26417 && type_decl
26418 && TREE_CODE (type_decl) == TYPE_DECL
26419 && scope);
26420
26421 if (!(ti = get_template_info (t)))
26422 return;
26423
26424 gcc_assert (TI_TEMPLATE (ti));
26425
26426 typedef_usage.typedef_decl = type_decl;
26427 typedef_usage.context = scope;
26428 typedef_usage.locus = location;
26429
26430 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26431 }
26432
26433 /* Append TYPE_DECL to the template TEMPL.
26434 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26435 At TEMPL instanciation time, TYPE_DECL will be checked to see
26436 if it can be accessed through SCOPE.
26437 LOCATION is the location of the usage point of TYPE_DECL.
26438
26439 e.g. consider the following code snippet:
26440
26441 class C
26442 {
26443 typedef int myint;
26444 };
26445
26446 template<class U> struct S
26447 {
26448 C::myint mi; // <-- usage point of the typedef C::myint
26449 };
26450
26451 S<char> s;
26452
26453 At S<char> instantiation time, we need to check the access of C::myint
26454 In other words, we need to check the access of the myint typedef through
26455 the C scope. For that purpose, this function will add the myint typedef
26456 and the scope C through which its being accessed to a list of typedefs
26457 tied to the template S. That list will be walked at template instantiation
26458 time and access check performed on each typedefs it contains.
26459 Note that this particular code snippet should yield an error because
26460 myint is private to C. */
26461
26462 void
26463 append_type_to_template_for_access_check (tree templ,
26464 tree type_decl,
26465 tree scope,
26466 location_t location)
26467 {
26468 qualified_typedef_usage_t *iter;
26469 unsigned i;
26470
26471 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26472
26473 /* Make sure we don't append the type to the template twice. */
26474 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26475 if (iter->typedef_decl == type_decl && scope == iter->context)
26476 return;
26477
26478 append_type_to_template_for_access_check_1 (templ, type_decl,
26479 scope, location);
26480 }
26481
26482 /* Convert the generic type parameters in PARM that match the types given in the
26483 range [START_IDX, END_IDX) from the current_template_parms into generic type
26484 packs. */
26485
26486 tree
26487 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26488 {
26489 tree current = current_template_parms;
26490 int depth = TMPL_PARMS_DEPTH (current);
26491 current = INNERMOST_TEMPLATE_PARMS (current);
26492 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26493
26494 for (int i = 0; i < start_idx; ++i)
26495 TREE_VEC_ELT (replacement, i)
26496 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26497
26498 for (int i = start_idx; i < end_idx; ++i)
26499 {
26500 /* Create a distinct parameter pack type from the current parm and add it
26501 to the replacement args to tsubst below into the generic function
26502 parameter. */
26503
26504 tree o = TREE_TYPE (TREE_VALUE
26505 (TREE_VEC_ELT (current, i)));
26506 tree t = copy_type (o);
26507 TEMPLATE_TYPE_PARM_INDEX (t)
26508 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26509 o, 0, 0, tf_none);
26510 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26511 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26512 TYPE_MAIN_VARIANT (t) = t;
26513 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26514 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26515 TREE_VEC_ELT (replacement, i) = t;
26516 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26517 }
26518
26519 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26520 TREE_VEC_ELT (replacement, i)
26521 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26522
26523 /* If there are more levels then build up the replacement with the outer
26524 template parms. */
26525 if (depth > 1)
26526 replacement = add_to_template_args (template_parms_to_args
26527 (TREE_CHAIN (current_template_parms)),
26528 replacement);
26529
26530 return tsubst (parm, replacement, tf_none, NULL_TREE);
26531 }
26532
26533 /* Entries in the decl_constraint hash table. */
26534 struct GTY((for_user)) constr_entry
26535 {
26536 tree decl;
26537 tree ci;
26538 };
26539
26540 /* Hashing function and equality for constraint entries. */
26541 struct constr_hasher : ggc_ptr_hash<constr_entry>
26542 {
26543 static hashval_t hash (constr_entry *e)
26544 {
26545 return (hashval_t)DECL_UID (e->decl);
26546 }
26547
26548 static bool equal (constr_entry *e1, constr_entry *e2)
26549 {
26550 return e1->decl == e2->decl;
26551 }
26552 };
26553
26554 /* A mapping from declarations to constraint information. Note that
26555 both templates and their underlying declarations are mapped to the
26556 same constraint information.
26557
26558 FIXME: This is defined in pt.c because garbage collection
26559 code is not being generated for constraint.cc. */
26560
26561 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26562
26563 /* Returns the template constraints of declaration T. If T is not
26564 constrained, return NULL_TREE. Note that T must be non-null. */
26565
26566 tree
26567 get_constraints (tree t)
26568 {
26569 if (!flag_concepts)
26570 return NULL_TREE;
26571
26572 gcc_assert (DECL_P (t));
26573 if (TREE_CODE (t) == TEMPLATE_DECL)
26574 t = DECL_TEMPLATE_RESULT (t);
26575 constr_entry elt = { t, NULL_TREE };
26576 constr_entry* found = decl_constraints->find (&elt);
26577 if (found)
26578 return found->ci;
26579 else
26580 return NULL_TREE;
26581 }
26582
26583 /* Associate the given constraint information CI with the declaration
26584 T. If T is a template, then the constraints are associated with
26585 its underlying declaration. Don't build associations if CI is
26586 NULL_TREE. */
26587
26588 void
26589 set_constraints (tree t, tree ci)
26590 {
26591 if (!ci)
26592 return;
26593 gcc_assert (t && flag_concepts);
26594 if (TREE_CODE (t) == TEMPLATE_DECL)
26595 t = DECL_TEMPLATE_RESULT (t);
26596 gcc_assert (!get_constraints (t));
26597 constr_entry elt = {t, ci};
26598 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26599 constr_entry* entry = ggc_alloc<constr_entry> ();
26600 *entry = elt;
26601 *slot = entry;
26602 }
26603
26604 /* Remove the associated constraints of the declaration T. */
26605
26606 void
26607 remove_constraints (tree t)
26608 {
26609 gcc_assert (DECL_P (t));
26610 if (TREE_CODE (t) == TEMPLATE_DECL)
26611 t = DECL_TEMPLATE_RESULT (t);
26612
26613 constr_entry elt = {t, NULL_TREE};
26614 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26615 if (slot)
26616 decl_constraints->clear_slot (slot);
26617 }
26618
26619 /* Memoized satisfaction results for declarations. This
26620 maps the pair (constraint_info, arguments) to the result computed
26621 by constraints_satisfied_p. */
26622
26623 struct GTY((for_user)) constraint_sat_entry
26624 {
26625 tree ci;
26626 tree args;
26627 tree result;
26628 };
26629
26630 /* Hashing function and equality for constraint entries. */
26631
26632 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26633 {
26634 static hashval_t hash (constraint_sat_entry *e)
26635 {
26636 hashval_t val = iterative_hash_object(e->ci, 0);
26637 return iterative_hash_template_arg (e->args, val);
26638 }
26639
26640 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26641 {
26642 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26643 }
26644 };
26645
26646 /* Memoized satisfaction results for concept checks. */
26647
26648 struct GTY((for_user)) concept_spec_entry
26649 {
26650 tree tmpl;
26651 tree args;
26652 tree result;
26653 };
26654
26655 /* Hashing function and equality for constraint entries. */
26656
26657 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26658 {
26659 static hashval_t hash (concept_spec_entry *e)
26660 {
26661 return hash_tmpl_and_args (e->tmpl, e->args);
26662 }
26663
26664 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26665 {
26666 ++comparing_specializations;
26667 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26668 --comparing_specializations;
26669 return eq;
26670 }
26671 };
26672
26673 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26674 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26675
26676 /* Search for a memoized satisfaction result. Returns one of the
26677 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26678
26679 tree
26680 lookup_constraint_satisfaction (tree ci, tree args)
26681 {
26682 constraint_sat_entry elt = { ci, args, NULL_TREE };
26683 constraint_sat_entry* found = constraint_memos->find (&elt);
26684 if (found)
26685 return found->result;
26686 else
26687 return NULL_TREE;
26688 }
26689
26690 /* Memoize the result of a satisfication test. Returns the saved result. */
26691
26692 tree
26693 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26694 {
26695 constraint_sat_entry elt = {ci, args, result};
26696 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26697 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26698 *entry = elt;
26699 *slot = entry;
26700 return result;
26701 }
26702
26703 /* Search for a memoized satisfaction result for a concept. */
26704
26705 tree
26706 lookup_concept_satisfaction (tree tmpl, tree args)
26707 {
26708 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26709 concept_spec_entry* found = concept_memos->find (&elt);
26710 if (found)
26711 return found->result;
26712 else
26713 return NULL_TREE;
26714 }
26715
26716 /* Memoize the result of a concept check. Returns the saved result. */
26717
26718 tree
26719 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26720 {
26721 concept_spec_entry elt = {tmpl, args, result};
26722 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26723 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26724 *entry = elt;
26725 *slot = entry;
26726 return result;
26727 }
26728
26729 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26730
26731 /* Returns a prior concept specialization. This returns the substituted
26732 and normalized constraints defined by the concept. */
26733
26734 tree
26735 get_concept_expansion (tree tmpl, tree args)
26736 {
26737 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26738 concept_spec_entry* found = concept_expansions->find (&elt);
26739 if (found)
26740 return found->result;
26741 else
26742 return NULL_TREE;
26743 }
26744
26745 /* Save a concept expansion for later. */
26746
26747 tree
26748 save_concept_expansion (tree tmpl, tree args, tree def)
26749 {
26750 concept_spec_entry elt = {tmpl, args, def};
26751 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26752 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26753 *entry = elt;
26754 *slot = entry;
26755 return def;
26756 }
26757
26758 static hashval_t
26759 hash_subsumption_args (tree t1, tree t2)
26760 {
26761 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26762 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26763 int val = 0;
26764 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26765 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26766 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26767 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26768 return val;
26769 }
26770
26771 /* Compare the constraints of two subsumption entries. The LEFT1 and
26772 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26773 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26774
26775 static bool
26776 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26777 {
26778 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26779 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26780 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26781 CHECK_CONSTR_ARGS (right1)))
26782 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26783 CHECK_CONSTR_ARGS (right2));
26784 return false;
26785 }
26786
26787 /* Key/value pair for learning and memoizing subsumption results. This
26788 associates a pair of check constraints (including arguments) with
26789 a boolean value indicating the result. */
26790
26791 struct GTY((for_user)) subsumption_entry
26792 {
26793 tree t1;
26794 tree t2;
26795 bool result;
26796 };
26797
26798 /* Hashing function and equality for constraint entries. */
26799
26800 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26801 {
26802 static hashval_t hash (subsumption_entry *e)
26803 {
26804 return hash_subsumption_args (e->t1, e->t2);
26805 }
26806
26807 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26808 {
26809 ++comparing_specializations;
26810 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26811 --comparing_specializations;
26812 return eq;
26813 }
26814 };
26815
26816 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26817
26818 /* Search for a previously cached subsumption result. */
26819
26820 bool*
26821 lookup_subsumption_result (tree t1, tree t2)
26822 {
26823 subsumption_entry elt = { t1, t2, false };
26824 subsumption_entry* found = subsumption_table->find (&elt);
26825 if (found)
26826 return &found->result;
26827 else
26828 return 0;
26829 }
26830
26831 /* Save a subsumption result. */
26832
26833 bool
26834 save_subsumption_result (tree t1, tree t2, bool result)
26835 {
26836 subsumption_entry elt = {t1, t2, result};
26837 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26838 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26839 *entry = elt;
26840 *slot = entry;
26841 return result;
26842 }
26843
26844 /* Set up the hash table for constraint association. */
26845
26846 void
26847 init_constraint_processing (void)
26848 {
26849 if (!flag_concepts)
26850 return;
26851
26852 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26853 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26854 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26855 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26856 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26857 }
26858
26859 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26860 0..N-1. */
26861
26862 void
26863 declare_integer_pack (void)
26864 {
26865 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26866 build_function_type_list (integer_type_node,
26867 integer_type_node,
26868 NULL_TREE),
26869 NULL_TREE, ECF_CONST);
26870 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26871 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26872 }
26873
26874 /* Set up the hash tables for template instantiations. */
26875
26876 void
26877 init_template_processing (void)
26878 {
26879 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26880 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26881
26882 if (cxx_dialect >= cxx11)
26883 declare_integer_pack ();
26884 }
26885
26886 /* Print stats about the template hash tables for -fstats. */
26887
26888 void
26889 print_template_statistics (void)
26890 {
26891 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26892 "%f collisions\n", (long) decl_specializations->size (),
26893 (long) decl_specializations->elements (),
26894 decl_specializations->collisions ());
26895 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26896 "%f collisions\n", (long) type_specializations->size (),
26897 (long) type_specializations->elements (),
26898 type_specializations->collisions ());
26899 }
26900
26901 #if CHECKING_P
26902
26903 namespace selftest {
26904
26905 /* Verify that build_non_dependent_expr () works, for various expressions,
26906 and that location wrappers don't affect the results. */
26907
26908 static void
26909 test_build_non_dependent_expr ()
26910 {
26911 location_t loc = BUILTINS_LOCATION;
26912
26913 /* Verify constants, without and with location wrappers. */
26914 tree int_cst = build_int_cst (integer_type_node, 42);
26915 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
26916
26917 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
26918 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
26919 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
26920
26921 tree string_lit = build_string (4, "foo");
26922 TREE_TYPE (string_lit) = char_array_type_node;
26923 string_lit = fix_string_type (string_lit);
26924 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
26925
26926 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
26927 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
26928 ASSERT_EQ (wrapped_string_lit,
26929 build_non_dependent_expr (wrapped_string_lit));
26930 }
26931
26932 /* Verify that type_dependent_expression_p () works correctly, even
26933 in the presence of location wrapper nodes. */
26934
26935 static void
26936 test_type_dependent_expression_p ()
26937 {
26938 location_t loc = BUILTINS_LOCATION;
26939
26940 tree name = get_identifier ("foo");
26941
26942 /* If no templates are involved, nothing is type-dependent. */
26943 gcc_assert (!processing_template_decl);
26944 ASSERT_FALSE (type_dependent_expression_p (name));
26945
26946 ++processing_template_decl;
26947
26948 /* Within a template, an unresolved name is always type-dependent. */
26949 ASSERT_TRUE (type_dependent_expression_p (name));
26950
26951 /* Ensure it copes with NULL_TREE and errors. */
26952 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
26953 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
26954
26955 /* A USING_DECL in a template should be type-dependent, even if wrapped
26956 with a location wrapper (PR c++/83799). */
26957 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
26958 TREE_TYPE (using_decl) = integer_type_node;
26959 ASSERT_TRUE (type_dependent_expression_p (using_decl));
26960 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
26961 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
26962 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
26963
26964 --processing_template_decl;
26965 }
26966
26967 /* Run all of the selftests within this file. */
26968
26969 void
26970 cp_pt_c_tests ()
26971 {
26972 test_build_non_dependent_expr ();
26973 test_type_dependent_expression_p ();
26974 }
26975
26976 } // namespace selftest
26977
26978 #endif /* #if CHECKING_P */
26979
26980 #include "gt-cp-pt.h"