[PR c++/84835] ICE with generic lambda in extern "C"
[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_1 (tree inner)
1138 {
1139 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1140 {
1141 tree arg = TREE_VEC_ELT (inner, i);
1142 if (TREE_CODE (arg) == TEMPLATE_DECL)
1143 /* OK */;
1144 else if (TYPE_P (arg))
1145 gcc_assert (strip_typedefs (arg, NULL) == arg);
1146 else if (ARGUMENT_PACK_P (arg))
1147 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1148 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1149 /* Allow typedefs on the type of a non-type argument, since a
1150 parameter can have them. */;
1151 else
1152 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1153 }
1154 }
1155
1156 static void
1157 verify_unstripped_args (tree args)
1158 {
1159 ++processing_template_decl;
1160 if (!any_dependent_template_arguments_p (args))
1161 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1162 --processing_template_decl;
1163 }
1164
1165 /* Retrieve the specialization (in the sense of [temp.spec] - a
1166 specialization is either an instantiation or an explicit
1167 specialization) of TMPL for the given template ARGS. If there is
1168 no such specialization, return NULL_TREE. The ARGS are a vector of
1169 arguments, or a vector of vectors of arguments, in the case of
1170 templates with more than one level of parameters.
1171
1172 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1173 then we search for a partial specialization matching ARGS. This
1174 parameter is ignored if TMPL is not a class template.
1175
1176 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1177 result is a NONTYPE_ARGUMENT_PACK. */
1178
1179 static tree
1180 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1181 {
1182 if (tmpl == NULL_TREE)
1183 return NULL_TREE;
1184
1185 if (args == error_mark_node)
1186 return NULL_TREE;
1187
1188 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1189 || TREE_CODE (tmpl) == FIELD_DECL);
1190
1191 /* There should be as many levels of arguments as there are
1192 levels of parameters. */
1193 gcc_assert (TMPL_ARGS_DEPTH (args)
1194 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1195 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1196 : template_class_depth (DECL_CONTEXT (tmpl))));
1197
1198 if (flag_checking)
1199 verify_unstripped_args (args);
1200
1201 /* Lambda functions in templates aren't instantiated normally, but through
1202 tsubst_lambda_expr. */
1203 if (lambda_fn_in_template_p (tmpl))
1204 return NULL_TREE;
1205
1206 if (optimize_specialization_lookup_p (tmpl))
1207 {
1208 /* The template arguments actually apply to the containing
1209 class. Find the class specialization with those
1210 arguments. */
1211 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1212 tree class_specialization
1213 = retrieve_specialization (class_template, args, 0);
1214 if (!class_specialization)
1215 return NULL_TREE;
1216
1217 /* Find the instance of TMPL. */
1218 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1219 for (ovl_iterator iter (fns); iter; ++iter)
1220 {
1221 tree fn = *iter;
1222 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1223 /* using-declarations can add base methods to the method vec,
1224 and we don't want those here. */
1225 && DECL_CONTEXT (fn) == class_specialization)
1226 return fn;
1227 }
1228 return NULL_TREE;
1229 }
1230 else
1231 {
1232 spec_entry *found;
1233 spec_entry elt;
1234 hash_table<spec_hasher> *specializations;
1235
1236 elt.tmpl = tmpl;
1237 elt.args = args;
1238 elt.spec = NULL_TREE;
1239
1240 if (DECL_CLASS_TEMPLATE_P (tmpl))
1241 specializations = type_specializations;
1242 else
1243 specializations = decl_specializations;
1244
1245 if (hash == 0)
1246 hash = spec_hasher::hash (&elt);
1247 found = specializations->find_with_hash (&elt, hash);
1248 if (found)
1249 return found->spec;
1250 }
1251
1252 return NULL_TREE;
1253 }
1254
1255 /* Like retrieve_specialization, but for local declarations. */
1256
1257 tree
1258 retrieve_local_specialization (tree tmpl)
1259 {
1260 if (local_specializations == NULL)
1261 return NULL_TREE;
1262
1263 tree *slot = local_specializations->get (tmpl);
1264 return slot ? *slot : NULL_TREE;
1265 }
1266
1267 /* Returns nonzero iff DECL is a specialization of TMPL. */
1268
1269 int
1270 is_specialization_of (tree decl, tree tmpl)
1271 {
1272 tree t;
1273
1274 if (TREE_CODE (decl) == FUNCTION_DECL)
1275 {
1276 for (t = decl;
1277 t != NULL_TREE;
1278 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1279 if (t == tmpl)
1280 return 1;
1281 }
1282 else
1283 {
1284 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1285
1286 for (t = TREE_TYPE (decl);
1287 t != NULL_TREE;
1288 t = CLASSTYPE_USE_TEMPLATE (t)
1289 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1290 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1291 return 1;
1292 }
1293
1294 return 0;
1295 }
1296
1297 /* Returns nonzero iff DECL is a specialization of friend declaration
1298 FRIEND_DECL according to [temp.friend]. */
1299
1300 bool
1301 is_specialization_of_friend (tree decl, tree friend_decl)
1302 {
1303 bool need_template = true;
1304 int template_depth;
1305
1306 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1307 || TREE_CODE (decl) == TYPE_DECL);
1308
1309 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1310 of a template class, we want to check if DECL is a specialization
1311 if this. */
1312 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1313 && DECL_TEMPLATE_INFO (friend_decl)
1314 && !DECL_USE_TEMPLATE (friend_decl))
1315 {
1316 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1317 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1318 need_template = false;
1319 }
1320 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1321 && !PRIMARY_TEMPLATE_P (friend_decl))
1322 need_template = false;
1323
1324 /* There is nothing to do if this is not a template friend. */
1325 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1326 return false;
1327
1328 if (is_specialization_of (decl, friend_decl))
1329 return true;
1330
1331 /* [temp.friend/6]
1332 A member of a class template may be declared to be a friend of a
1333 non-template class. In this case, the corresponding member of
1334 every specialization of the class template is a friend of the
1335 class granting friendship.
1336
1337 For example, given a template friend declaration
1338
1339 template <class T> friend void A<T>::f();
1340
1341 the member function below is considered a friend
1342
1343 template <> struct A<int> {
1344 void f();
1345 };
1346
1347 For this type of template friend, TEMPLATE_DEPTH below will be
1348 nonzero. To determine if DECL is a friend of FRIEND, we first
1349 check if the enclosing class is a specialization of another. */
1350
1351 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1352 if (template_depth
1353 && DECL_CLASS_SCOPE_P (decl)
1354 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1355 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1356 {
1357 /* Next, we check the members themselves. In order to handle
1358 a few tricky cases, such as when FRIEND_DECL's are
1359
1360 template <class T> friend void A<T>::g(T t);
1361 template <class T> template <T t> friend void A<T>::h();
1362
1363 and DECL's are
1364
1365 void A<int>::g(int);
1366 template <int> void A<int>::h();
1367
1368 we need to figure out ARGS, the template arguments from
1369 the context of DECL. This is required for template substitution
1370 of `T' in the function parameter of `g' and template parameter
1371 of `h' in the above examples. Here ARGS corresponds to `int'. */
1372
1373 tree context = DECL_CONTEXT (decl);
1374 tree args = NULL_TREE;
1375 int current_depth = 0;
1376
1377 while (current_depth < template_depth)
1378 {
1379 if (CLASSTYPE_TEMPLATE_INFO (context))
1380 {
1381 if (current_depth == 0)
1382 args = TYPE_TI_ARGS (context);
1383 else
1384 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1385 current_depth++;
1386 }
1387 context = TYPE_CONTEXT (context);
1388 }
1389
1390 if (TREE_CODE (decl) == FUNCTION_DECL)
1391 {
1392 bool is_template;
1393 tree friend_type;
1394 tree decl_type;
1395 tree friend_args_type;
1396 tree decl_args_type;
1397
1398 /* Make sure that both DECL and FRIEND_DECL are templates or
1399 non-templates. */
1400 is_template = DECL_TEMPLATE_INFO (decl)
1401 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1402 if (need_template ^ is_template)
1403 return false;
1404 else if (is_template)
1405 {
1406 /* If both are templates, check template parameter list. */
1407 tree friend_parms
1408 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1409 args, tf_none);
1410 if (!comp_template_parms
1411 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1412 friend_parms))
1413 return false;
1414
1415 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1416 }
1417 else
1418 decl_type = TREE_TYPE (decl);
1419
1420 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1421 tf_none, NULL_TREE);
1422 if (friend_type == error_mark_node)
1423 return false;
1424
1425 /* Check if return types match. */
1426 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1427 return false;
1428
1429 /* Check if function parameter types match, ignoring the
1430 `this' parameter. */
1431 friend_args_type = TYPE_ARG_TYPES (friend_type);
1432 decl_args_type = TYPE_ARG_TYPES (decl_type);
1433 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1434 friend_args_type = TREE_CHAIN (friend_args_type);
1435 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1436 decl_args_type = TREE_CHAIN (decl_args_type);
1437
1438 return compparms (decl_args_type, friend_args_type);
1439 }
1440 else
1441 {
1442 /* DECL is a TYPE_DECL */
1443 bool is_template;
1444 tree decl_type = TREE_TYPE (decl);
1445
1446 /* Make sure that both DECL and FRIEND_DECL are templates or
1447 non-templates. */
1448 is_template
1449 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1450 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1451
1452 if (need_template ^ is_template)
1453 return false;
1454 else if (is_template)
1455 {
1456 tree friend_parms;
1457 /* If both are templates, check the name of the two
1458 TEMPLATE_DECL's first because is_friend didn't. */
1459 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1460 != DECL_NAME (friend_decl))
1461 return false;
1462
1463 /* Now check template parameter list. */
1464 friend_parms
1465 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1466 args, tf_none);
1467 return comp_template_parms
1468 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1469 friend_parms);
1470 }
1471 else
1472 return (DECL_NAME (decl)
1473 == DECL_NAME (friend_decl));
1474 }
1475 }
1476 return false;
1477 }
1478
1479 /* Register the specialization SPEC as a specialization of TMPL with
1480 the indicated ARGS. IS_FRIEND indicates whether the specialization
1481 is actually just a friend declaration. ATTRLIST is the list of
1482 attributes that the specialization is declared with or NULL when
1483 it isn't. Returns SPEC, or an equivalent prior declaration, if
1484 available.
1485
1486 We also store instantiations of field packs in the hash table, even
1487 though they are not themselves templates, to make lookup easier. */
1488
1489 static tree
1490 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1491 hashval_t hash)
1492 {
1493 tree fn;
1494 spec_entry **slot = NULL;
1495 spec_entry elt;
1496
1497 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1498 || (TREE_CODE (tmpl) == FIELD_DECL
1499 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1500
1501 if (TREE_CODE (spec) == FUNCTION_DECL
1502 && uses_template_parms (DECL_TI_ARGS (spec)))
1503 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1504 register it; we want the corresponding TEMPLATE_DECL instead.
1505 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1506 the more obvious `uses_template_parms (spec)' to avoid problems
1507 with default function arguments. In particular, given
1508 something like this:
1509
1510 template <class T> void f(T t1, T t = T())
1511
1512 the default argument expression is not substituted for in an
1513 instantiation unless and until it is actually needed. */
1514 return spec;
1515
1516 if (optimize_specialization_lookup_p (tmpl))
1517 /* We don't put these specializations in the hash table, but we might
1518 want to give an error about a mismatch. */
1519 fn = retrieve_specialization (tmpl, args, 0);
1520 else
1521 {
1522 elt.tmpl = tmpl;
1523 elt.args = args;
1524 elt.spec = spec;
1525
1526 if (hash == 0)
1527 hash = spec_hasher::hash (&elt);
1528
1529 slot =
1530 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1531 if (*slot)
1532 fn = ((spec_entry *) *slot)->spec;
1533 else
1534 fn = NULL_TREE;
1535 }
1536
1537 /* We can sometimes try to re-register a specialization that we've
1538 already got. In particular, regenerate_decl_from_template calls
1539 duplicate_decls which will update the specialization list. But,
1540 we'll still get called again here anyhow. It's more convenient
1541 to simply allow this than to try to prevent it. */
1542 if (fn == spec)
1543 return spec;
1544 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1545 {
1546 if (DECL_TEMPLATE_INSTANTIATION (fn))
1547 {
1548 if (DECL_ODR_USED (fn)
1549 || DECL_EXPLICIT_INSTANTIATION (fn))
1550 {
1551 error ("specialization of %qD after instantiation",
1552 fn);
1553 return error_mark_node;
1554 }
1555 else
1556 {
1557 tree clone;
1558 /* This situation should occur only if the first
1559 specialization is an implicit instantiation, the
1560 second is an explicit specialization, and the
1561 implicit instantiation has not yet been used. That
1562 situation can occur if we have implicitly
1563 instantiated a member function and then specialized
1564 it later.
1565
1566 We can also wind up here if a friend declaration that
1567 looked like an instantiation turns out to be a
1568 specialization:
1569
1570 template <class T> void foo(T);
1571 class S { friend void foo<>(int) };
1572 template <> void foo(int);
1573
1574 We transform the existing DECL in place so that any
1575 pointers to it become pointers to the updated
1576 declaration.
1577
1578 If there was a definition for the template, but not
1579 for the specialization, we want this to look as if
1580 there were no definition, and vice versa. */
1581 DECL_INITIAL (fn) = NULL_TREE;
1582 duplicate_decls (spec, fn, is_friend);
1583 /* The call to duplicate_decls will have applied
1584 [temp.expl.spec]:
1585
1586 An explicit specialization of a function template
1587 is inline only if it is explicitly declared to be,
1588 and independently of whether its function template
1589 is.
1590
1591 to the primary function; now copy the inline bits to
1592 the various clones. */
1593 FOR_EACH_CLONE (clone, fn)
1594 {
1595 DECL_DECLARED_INLINE_P (clone)
1596 = DECL_DECLARED_INLINE_P (fn);
1597 DECL_SOURCE_LOCATION (clone)
1598 = DECL_SOURCE_LOCATION (fn);
1599 DECL_DELETED_FN (clone)
1600 = DECL_DELETED_FN (fn);
1601 }
1602 check_specialization_namespace (tmpl);
1603
1604 return fn;
1605 }
1606 }
1607 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1608 {
1609 tree dd = duplicate_decls (spec, fn, is_friend);
1610 if (dd == error_mark_node)
1611 /* We've already complained in duplicate_decls. */
1612 return error_mark_node;
1613
1614 if (dd == NULL_TREE && DECL_INITIAL (spec))
1615 /* Dup decl failed, but this is a new definition. Set the
1616 line number so any errors match this new
1617 definition. */
1618 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1619
1620 return fn;
1621 }
1622 }
1623 else if (fn)
1624 return duplicate_decls (spec, fn, is_friend);
1625
1626 /* A specialization must be declared in the same namespace as the
1627 template it is specializing. */
1628 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1629 && !check_specialization_namespace (tmpl))
1630 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1631
1632 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1633 {
1634 spec_entry *entry = ggc_alloc<spec_entry> ();
1635 gcc_assert (tmpl && args && spec);
1636 *entry = elt;
1637 *slot = entry;
1638 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1639 && PRIMARY_TEMPLATE_P (tmpl)
1640 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1641 || variable_template_p (tmpl))
1642 /* If TMPL is a forward declaration of a template function, keep a list
1643 of all specializations in case we need to reassign them to a friend
1644 template later in tsubst_friend_function.
1645
1646 Also keep a list of all variable template instantiations so that
1647 process_partial_specialization can check whether a later partial
1648 specialization would have used it. */
1649 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1650 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1651 }
1652
1653 return spec;
1654 }
1655
1656 /* Returns true iff two spec_entry nodes are equivalent. */
1657
1658 int comparing_specializations;
1659
1660 bool
1661 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1662 {
1663 int equal;
1664
1665 ++comparing_specializations;
1666 equal = (e1->tmpl == e2->tmpl
1667 && comp_template_args (e1->args, e2->args));
1668 if (equal && flag_concepts
1669 /* tmpl could be a FIELD_DECL for a capture pack. */
1670 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1671 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1672 && uses_template_parms (e1->args))
1673 {
1674 /* Partial specializations of a variable template can be distinguished by
1675 constraints. */
1676 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1677 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1678 equal = equivalent_constraints (c1, c2);
1679 }
1680 --comparing_specializations;
1681
1682 return equal;
1683 }
1684
1685 /* Returns a hash for a template TMPL and template arguments ARGS. */
1686
1687 static hashval_t
1688 hash_tmpl_and_args (tree tmpl, tree args)
1689 {
1690 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1691 return iterative_hash_template_arg (args, val);
1692 }
1693
1694 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1695 ignoring SPEC. */
1696
1697 hashval_t
1698 spec_hasher::hash (spec_entry *e)
1699 {
1700 return hash_tmpl_and_args (e->tmpl, e->args);
1701 }
1702
1703 /* Recursively calculate a hash value for a template argument ARG, for use
1704 in the hash tables of template specializations. */
1705
1706 hashval_t
1707 iterative_hash_template_arg (tree arg, hashval_t val)
1708 {
1709 unsigned HOST_WIDE_INT i;
1710 enum tree_code code;
1711 char tclass;
1712
1713 if (arg == NULL_TREE)
1714 return iterative_hash_object (arg, val);
1715
1716 if (!TYPE_P (arg))
1717 STRIP_NOPS (arg);
1718
1719 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1720 gcc_unreachable ();
1721
1722 code = TREE_CODE (arg);
1723 tclass = TREE_CODE_CLASS (code);
1724
1725 val = iterative_hash_object (code, val);
1726
1727 switch (code)
1728 {
1729 case ERROR_MARK:
1730 return val;
1731
1732 case IDENTIFIER_NODE:
1733 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1734
1735 case TREE_VEC:
1736 {
1737 int i, len = TREE_VEC_LENGTH (arg);
1738 for (i = 0; i < len; ++i)
1739 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1740 return val;
1741 }
1742
1743 case TYPE_PACK_EXPANSION:
1744 case EXPR_PACK_EXPANSION:
1745 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1746 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1747
1748 case TYPE_ARGUMENT_PACK:
1749 case NONTYPE_ARGUMENT_PACK:
1750 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1751
1752 case TREE_LIST:
1753 for (; arg; arg = TREE_CHAIN (arg))
1754 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1755 return val;
1756
1757 case OVERLOAD:
1758 for (lkp_iterator iter (arg); iter; ++iter)
1759 val = iterative_hash_template_arg (*iter, val);
1760 return val;
1761
1762 case CONSTRUCTOR:
1763 {
1764 tree field, value;
1765 iterative_hash_template_arg (TREE_TYPE (arg), val);
1766 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1767 {
1768 val = iterative_hash_template_arg (field, val);
1769 val = iterative_hash_template_arg (value, val);
1770 }
1771 return val;
1772 }
1773
1774 case PARM_DECL:
1775 if (!DECL_ARTIFICIAL (arg))
1776 {
1777 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1778 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1779 }
1780 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1781
1782 case TARGET_EXPR:
1783 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1784
1785 case PTRMEM_CST:
1786 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1787 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1788
1789 case TEMPLATE_PARM_INDEX:
1790 val = iterative_hash_template_arg
1791 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1792 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1793 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1794
1795 case TRAIT_EXPR:
1796 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1797 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1798 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1799
1800 case BASELINK:
1801 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1802 val);
1803 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1804 val);
1805
1806 case MODOP_EXPR:
1807 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1808 code = TREE_CODE (TREE_OPERAND (arg, 1));
1809 val = iterative_hash_object (code, val);
1810 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1811
1812 case LAMBDA_EXPR:
1813 /* A lambda can't appear in a template arg, but don't crash on
1814 erroneous input. */
1815 gcc_assert (seen_error ());
1816 return val;
1817
1818 case CAST_EXPR:
1819 case IMPLICIT_CONV_EXPR:
1820 case STATIC_CAST_EXPR:
1821 case REINTERPRET_CAST_EXPR:
1822 case CONST_CAST_EXPR:
1823 case DYNAMIC_CAST_EXPR:
1824 case NEW_EXPR:
1825 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1826 /* Now hash operands as usual. */
1827 break;
1828
1829 default:
1830 break;
1831 }
1832
1833 switch (tclass)
1834 {
1835 case tcc_type:
1836 if (alias_template_specialization_p (arg))
1837 {
1838 // We want an alias specialization that survived strip_typedefs
1839 // to hash differently from its TYPE_CANONICAL, to avoid hash
1840 // collisions that compare as different in template_args_equal.
1841 // These could be dependent specializations that strip_typedefs
1842 // left alone, or untouched specializations because
1843 // coerce_template_parms returns the unconverted template
1844 // arguments if it sees incomplete argument packs.
1845 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1846 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1847 }
1848 if (TYPE_CANONICAL (arg))
1849 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1850 val);
1851 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1852 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1853 /* Otherwise just compare the types during lookup. */
1854 return val;
1855
1856 case tcc_declaration:
1857 case tcc_constant:
1858 return iterative_hash_expr (arg, val);
1859
1860 default:
1861 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1862 {
1863 unsigned n = cp_tree_operand_length (arg);
1864 for (i = 0; i < n; ++i)
1865 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1866 return val;
1867 }
1868 }
1869 gcc_unreachable ();
1870 return 0;
1871 }
1872
1873 /* Unregister the specialization SPEC as a specialization of TMPL.
1874 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1875 if the SPEC was listed as a specialization of TMPL.
1876
1877 Note that SPEC has been ggc_freed, so we can't look inside it. */
1878
1879 bool
1880 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1881 {
1882 spec_entry *entry;
1883 spec_entry elt;
1884
1885 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1886 elt.args = TI_ARGS (tinfo);
1887 elt.spec = NULL_TREE;
1888
1889 entry = decl_specializations->find (&elt);
1890 if (entry != NULL)
1891 {
1892 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1893 gcc_assert (new_spec != NULL_TREE);
1894 entry->spec = new_spec;
1895 return 1;
1896 }
1897
1898 return 0;
1899 }
1900
1901 /* Like register_specialization, but for local declarations. We are
1902 registering SPEC, an instantiation of TMPL. */
1903
1904 void
1905 register_local_specialization (tree spec, tree tmpl)
1906 {
1907 gcc_assert (tmpl != spec);
1908 local_specializations->put (tmpl, spec);
1909 }
1910
1911 /* TYPE is a class type. Returns true if TYPE is an explicitly
1912 specialized class. */
1913
1914 bool
1915 explicit_class_specialization_p (tree type)
1916 {
1917 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1918 return false;
1919 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1920 }
1921
1922 /* Print the list of functions at FNS, going through all the overloads
1923 for each element of the list. Alternatively, FNS can not be a
1924 TREE_LIST, in which case it will be printed together with all the
1925 overloads.
1926
1927 MORE and *STR should respectively be FALSE and NULL when the function
1928 is called from the outside. They are used internally on recursive
1929 calls. print_candidates manages the two parameters and leaves NULL
1930 in *STR when it ends. */
1931
1932 static void
1933 print_candidates_1 (tree fns, char **str, bool more = false)
1934 {
1935 if (TREE_CODE (fns) == TREE_LIST)
1936 for (; fns; fns = TREE_CHAIN (fns))
1937 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1938 else
1939 for (lkp_iterator iter (fns); iter;)
1940 {
1941 tree cand = *iter;
1942 ++iter;
1943
1944 const char *pfx = *str;
1945 if (!pfx)
1946 {
1947 if (more || iter)
1948 pfx = _("candidates are:");
1949 else
1950 pfx = _("candidate is:");
1951 *str = get_spaces (pfx);
1952 }
1953 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1954 }
1955 }
1956
1957 /* Print the list of candidate FNS in an error message. FNS can also
1958 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1959
1960 void
1961 print_candidates (tree fns)
1962 {
1963 char *str = NULL;
1964 print_candidates_1 (fns, &str);
1965 free (str);
1966 }
1967
1968 /* Get a (possibly) constrained template declaration for the
1969 purpose of ordering candidates. */
1970 static tree
1971 get_template_for_ordering (tree list)
1972 {
1973 gcc_assert (TREE_CODE (list) == TREE_LIST);
1974 tree f = TREE_VALUE (list);
1975 if (tree ti = DECL_TEMPLATE_INFO (f))
1976 return TI_TEMPLATE (ti);
1977 return f;
1978 }
1979
1980 /* Among candidates having the same signature, return the
1981 most constrained or NULL_TREE if there is no best candidate.
1982 If the signatures of candidates vary (e.g., template
1983 specialization vs. member function), then there can be no
1984 most constrained.
1985
1986 Note that we don't compare constraints on the functions
1987 themselves, but rather those of their templates. */
1988 static tree
1989 most_constrained_function (tree candidates)
1990 {
1991 // Try to find the best candidate in a first pass.
1992 tree champ = candidates;
1993 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1994 {
1995 int winner = more_constrained (get_template_for_ordering (champ),
1996 get_template_for_ordering (c));
1997 if (winner == -1)
1998 champ = c; // The candidate is more constrained
1999 else if (winner == 0)
2000 return NULL_TREE; // Neither is more constrained
2001 }
2002
2003 // Verify that the champ is better than previous candidates.
2004 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2005 if (!more_constrained (get_template_for_ordering (champ),
2006 get_template_for_ordering (c)))
2007 return NULL_TREE;
2008 }
2009
2010 return champ;
2011 }
2012
2013
2014 /* Returns the template (one of the functions given by TEMPLATE_ID)
2015 which can be specialized to match the indicated DECL with the
2016 explicit template args given in TEMPLATE_ID. The DECL may be
2017 NULL_TREE if none is available. In that case, the functions in
2018 TEMPLATE_ID are non-members.
2019
2020 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2021 specialization of a member template.
2022
2023 The TEMPLATE_COUNT is the number of references to qualifying
2024 template classes that appeared in the name of the function. See
2025 check_explicit_specialization for a more accurate description.
2026
2027 TSK indicates what kind of template declaration (if any) is being
2028 declared. TSK_TEMPLATE indicates that the declaration given by
2029 DECL, though a FUNCTION_DECL, has template parameters, and is
2030 therefore a template function.
2031
2032 The template args (those explicitly specified and those deduced)
2033 are output in a newly created vector *TARGS_OUT.
2034
2035 If it is impossible to determine the result, an error message is
2036 issued. The error_mark_node is returned to indicate failure. */
2037
2038 static tree
2039 determine_specialization (tree template_id,
2040 tree decl,
2041 tree* targs_out,
2042 int need_member_template,
2043 int template_count,
2044 tmpl_spec_kind tsk)
2045 {
2046 tree fns;
2047 tree targs;
2048 tree explicit_targs;
2049 tree candidates = NULL_TREE;
2050
2051 /* A TREE_LIST of templates of which DECL may be a specialization.
2052 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2053 corresponding TREE_PURPOSE is the set of template arguments that,
2054 when used to instantiate the template, would produce a function
2055 with the signature of DECL. */
2056 tree templates = NULL_TREE;
2057 int header_count;
2058 cp_binding_level *b;
2059
2060 *targs_out = NULL_TREE;
2061
2062 if (template_id == error_mark_node || decl == error_mark_node)
2063 return error_mark_node;
2064
2065 /* We shouldn't be specializing a member template of an
2066 unspecialized class template; we already gave an error in
2067 check_specialization_scope, now avoid crashing. */
2068 if (!VAR_P (decl)
2069 && template_count && DECL_CLASS_SCOPE_P (decl)
2070 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2071 {
2072 gcc_assert (errorcount);
2073 return error_mark_node;
2074 }
2075
2076 fns = TREE_OPERAND (template_id, 0);
2077 explicit_targs = TREE_OPERAND (template_id, 1);
2078
2079 if (fns == error_mark_node)
2080 return error_mark_node;
2081
2082 /* Check for baselinks. */
2083 if (BASELINK_P (fns))
2084 fns = BASELINK_FUNCTIONS (fns);
2085
2086 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2087 {
2088 error ("%qD is not a function template", fns);
2089 return error_mark_node;
2090 }
2091 else if (VAR_P (decl) && !variable_template_p (fns))
2092 {
2093 error ("%qD is not a variable template", fns);
2094 return error_mark_node;
2095 }
2096
2097 /* Count the number of template headers specified for this
2098 specialization. */
2099 header_count = 0;
2100 for (b = current_binding_level;
2101 b->kind == sk_template_parms;
2102 b = b->level_chain)
2103 ++header_count;
2104
2105 tree orig_fns = fns;
2106
2107 if (variable_template_p (fns))
2108 {
2109 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2110 targs = coerce_template_parms (parms, explicit_targs, fns,
2111 tf_warning_or_error,
2112 /*req_all*/true, /*use_defarg*/true);
2113 if (targs != error_mark_node)
2114 templates = tree_cons (targs, fns, templates);
2115 }
2116 else for (lkp_iterator iter (fns); iter; ++iter)
2117 {
2118 tree fn = *iter;
2119
2120 if (TREE_CODE (fn) == TEMPLATE_DECL)
2121 {
2122 tree decl_arg_types;
2123 tree fn_arg_types;
2124 tree insttype;
2125
2126 /* In case of explicit specialization, we need to check if
2127 the number of template headers appearing in the specialization
2128 is correct. This is usually done in check_explicit_specialization,
2129 but the check done there cannot be exhaustive when specializing
2130 member functions. Consider the following code:
2131
2132 template <> void A<int>::f(int);
2133 template <> template <> void A<int>::f(int);
2134
2135 Assuming that A<int> is not itself an explicit specialization
2136 already, the first line specializes "f" which is a non-template
2137 member function, whilst the second line specializes "f" which
2138 is a template member function. So both lines are syntactically
2139 correct, and check_explicit_specialization does not reject
2140 them.
2141
2142 Here, we can do better, as we are matching the specialization
2143 against the declarations. We count the number of template
2144 headers, and we check if they match TEMPLATE_COUNT + 1
2145 (TEMPLATE_COUNT is the number of qualifying template classes,
2146 plus there must be another header for the member template
2147 itself).
2148
2149 Notice that if header_count is zero, this is not a
2150 specialization but rather a template instantiation, so there
2151 is no check we can perform here. */
2152 if (header_count && header_count != template_count + 1)
2153 continue;
2154
2155 /* Check that the number of template arguments at the
2156 innermost level for DECL is the same as for FN. */
2157 if (current_binding_level->kind == sk_template_parms
2158 && !current_binding_level->explicit_spec_p
2159 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2160 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2161 (current_template_parms))))
2162 continue;
2163
2164 /* DECL might be a specialization of FN. */
2165 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2166 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2167
2168 /* For a non-static member function, we need to make sure
2169 that the const qualification is the same. Since
2170 get_bindings does not try to merge the "this" parameter,
2171 we must do the comparison explicitly. */
2172 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2173 {
2174 if (!same_type_p (TREE_VALUE (fn_arg_types),
2175 TREE_VALUE (decl_arg_types)))
2176 continue;
2177
2178 /* And the ref-qualification. */
2179 if (type_memfn_rqual (TREE_TYPE (decl))
2180 != type_memfn_rqual (TREE_TYPE (fn)))
2181 continue;
2182 }
2183
2184 /* Skip the "this" parameter and, for constructors of
2185 classes with virtual bases, the VTT parameter. A
2186 full specialization of a constructor will have a VTT
2187 parameter, but a template never will. */
2188 decl_arg_types
2189 = skip_artificial_parms_for (decl, decl_arg_types);
2190 fn_arg_types
2191 = skip_artificial_parms_for (fn, fn_arg_types);
2192
2193 /* Function templates cannot be specializations; there are
2194 no partial specializations of functions. Therefore, if
2195 the type of DECL does not match FN, there is no
2196 match.
2197
2198 Note that it should never be the case that we have both
2199 candidates added here, and for regular member functions
2200 below. */
2201 if (tsk == tsk_template)
2202 {
2203 if (compparms (fn_arg_types, decl_arg_types))
2204 candidates = tree_cons (NULL_TREE, fn, candidates);
2205 continue;
2206 }
2207
2208 /* See whether this function might be a specialization of this
2209 template. Suppress access control because we might be trying
2210 to make this specialization a friend, and we have already done
2211 access control for the declaration of the specialization. */
2212 push_deferring_access_checks (dk_no_check);
2213 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2214 pop_deferring_access_checks ();
2215
2216 if (!targs)
2217 /* We cannot deduce template arguments that when used to
2218 specialize TMPL will produce DECL. */
2219 continue;
2220
2221 if (uses_template_parms (targs))
2222 /* We deduced something involving 'auto', which isn't a valid
2223 template argument. */
2224 continue;
2225
2226 /* Remove, from the set of candidates, all those functions
2227 whose constraints are not satisfied. */
2228 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2229 continue;
2230
2231 // Then, try to form the new function type.
2232 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2233 if (insttype == error_mark_node)
2234 continue;
2235 fn_arg_types
2236 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2237 if (!compparms (fn_arg_types, decl_arg_types))
2238 continue;
2239
2240 /* Save this template, and the arguments deduced. */
2241 templates = tree_cons (targs, fn, templates);
2242 }
2243 else if (need_member_template)
2244 /* FN is an ordinary member function, and we need a
2245 specialization of a member template. */
2246 ;
2247 else if (TREE_CODE (fn) != FUNCTION_DECL)
2248 /* We can get IDENTIFIER_NODEs here in certain erroneous
2249 cases. */
2250 ;
2251 else if (!DECL_FUNCTION_MEMBER_P (fn))
2252 /* This is just an ordinary non-member function. Nothing can
2253 be a specialization of that. */
2254 ;
2255 else if (DECL_ARTIFICIAL (fn))
2256 /* Cannot specialize functions that are created implicitly. */
2257 ;
2258 else
2259 {
2260 tree decl_arg_types;
2261
2262 /* This is an ordinary member function. However, since
2263 we're here, we can assume its enclosing class is a
2264 template class. For example,
2265
2266 template <typename T> struct S { void f(); };
2267 template <> void S<int>::f() {}
2268
2269 Here, S<int>::f is a non-template, but S<int> is a
2270 template class. If FN has the same type as DECL, we
2271 might be in business. */
2272
2273 if (!DECL_TEMPLATE_INFO (fn))
2274 /* Its enclosing class is an explicit specialization
2275 of a template class. This is not a candidate. */
2276 continue;
2277
2278 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2279 TREE_TYPE (TREE_TYPE (fn))))
2280 /* The return types differ. */
2281 continue;
2282
2283 /* Adjust the type of DECL in case FN is a static member. */
2284 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2285 if (DECL_STATIC_FUNCTION_P (fn)
2286 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2287 decl_arg_types = TREE_CHAIN (decl_arg_types);
2288
2289 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2290 decl_arg_types))
2291 continue;
2292
2293 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2294 && (type_memfn_rqual (TREE_TYPE (decl))
2295 != type_memfn_rqual (TREE_TYPE (fn))))
2296 continue;
2297
2298 // If the deduced arguments do not satisfy the constraints,
2299 // this is not a candidate.
2300 if (flag_concepts && !constraints_satisfied_p (fn))
2301 continue;
2302
2303 // Add the candidate.
2304 candidates = tree_cons (NULL_TREE, fn, candidates);
2305 }
2306 }
2307
2308 if (templates && TREE_CHAIN (templates))
2309 {
2310 /* We have:
2311
2312 [temp.expl.spec]
2313
2314 It is possible for a specialization with a given function
2315 signature to be instantiated from more than one function
2316 template. In such cases, explicit specification of the
2317 template arguments must be used to uniquely identify the
2318 function template specialization being specialized.
2319
2320 Note that here, there's no suggestion that we're supposed to
2321 determine which of the candidate templates is most
2322 specialized. However, we, also have:
2323
2324 [temp.func.order]
2325
2326 Partial ordering of overloaded function template
2327 declarations is used in the following contexts to select
2328 the function template to which a function template
2329 specialization refers:
2330
2331 -- when an explicit specialization refers to a function
2332 template.
2333
2334 So, we do use the partial ordering rules, at least for now.
2335 This extension can only serve to make invalid programs valid,
2336 so it's safe. And, there is strong anecdotal evidence that
2337 the committee intended the partial ordering rules to apply;
2338 the EDG front end has that behavior, and John Spicer claims
2339 that the committee simply forgot to delete the wording in
2340 [temp.expl.spec]. */
2341 tree tmpl = most_specialized_instantiation (templates);
2342 if (tmpl != error_mark_node)
2343 {
2344 templates = tmpl;
2345 TREE_CHAIN (templates) = NULL_TREE;
2346 }
2347 }
2348
2349 // Concepts allows multiple declarations of member functions
2350 // with the same signature. Like above, we need to rely on
2351 // on the partial ordering of those candidates to determine which
2352 // is the best.
2353 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2354 {
2355 if (tree cand = most_constrained_function (candidates))
2356 {
2357 candidates = cand;
2358 TREE_CHAIN (cand) = NULL_TREE;
2359 }
2360 }
2361
2362 if (templates == NULL_TREE && candidates == NULL_TREE)
2363 {
2364 error ("template-id %qD for %q+D does not match any template "
2365 "declaration", template_id, decl);
2366 if (header_count && header_count != template_count + 1)
2367 inform (input_location, "saw %d %<template<>%>, need %d for "
2368 "specializing a member function template",
2369 header_count, template_count + 1);
2370 else
2371 print_candidates (orig_fns);
2372 return error_mark_node;
2373 }
2374 else if ((templates && TREE_CHAIN (templates))
2375 || (candidates && TREE_CHAIN (candidates))
2376 || (templates && candidates))
2377 {
2378 error ("ambiguous template specialization %qD for %q+D",
2379 template_id, decl);
2380 candidates = chainon (candidates, templates);
2381 print_candidates (candidates);
2382 return error_mark_node;
2383 }
2384
2385 /* We have one, and exactly one, match. */
2386 if (candidates)
2387 {
2388 tree fn = TREE_VALUE (candidates);
2389 *targs_out = copy_node (DECL_TI_ARGS (fn));
2390
2391 // Propagate the candidate's constraints to the declaration.
2392 set_constraints (decl, get_constraints (fn));
2393
2394 /* DECL is a re-declaration or partial instantiation of a template
2395 function. */
2396 if (TREE_CODE (fn) == TEMPLATE_DECL)
2397 return fn;
2398 /* It was a specialization of an ordinary member function in a
2399 template class. */
2400 return DECL_TI_TEMPLATE (fn);
2401 }
2402
2403 /* It was a specialization of a template. */
2404 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2405 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2406 {
2407 *targs_out = copy_node (targs);
2408 SET_TMPL_ARGS_LEVEL (*targs_out,
2409 TMPL_ARGS_DEPTH (*targs_out),
2410 TREE_PURPOSE (templates));
2411 }
2412 else
2413 *targs_out = TREE_PURPOSE (templates);
2414 return TREE_VALUE (templates);
2415 }
2416
2417 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2418 but with the default argument values filled in from those in the
2419 TMPL_TYPES. */
2420
2421 static tree
2422 copy_default_args_to_explicit_spec_1 (tree spec_types,
2423 tree tmpl_types)
2424 {
2425 tree new_spec_types;
2426
2427 if (!spec_types)
2428 return NULL_TREE;
2429
2430 if (spec_types == void_list_node)
2431 return void_list_node;
2432
2433 /* Substitute into the rest of the list. */
2434 new_spec_types =
2435 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2436 TREE_CHAIN (tmpl_types));
2437
2438 /* Add the default argument for this parameter. */
2439 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2440 TREE_VALUE (spec_types),
2441 new_spec_types);
2442 }
2443
2444 /* DECL is an explicit specialization. Replicate default arguments
2445 from the template it specializes. (That way, code like:
2446
2447 template <class T> void f(T = 3);
2448 template <> void f(double);
2449 void g () { f (); }
2450
2451 works, as required.) An alternative approach would be to look up
2452 the correct default arguments at the call-site, but this approach
2453 is consistent with how implicit instantiations are handled. */
2454
2455 static void
2456 copy_default_args_to_explicit_spec (tree decl)
2457 {
2458 tree tmpl;
2459 tree spec_types;
2460 tree tmpl_types;
2461 tree new_spec_types;
2462 tree old_type;
2463 tree new_type;
2464 tree t;
2465 tree object_type = NULL_TREE;
2466 tree in_charge = NULL_TREE;
2467 tree vtt = NULL_TREE;
2468
2469 /* See if there's anything we need to do. */
2470 tmpl = DECL_TI_TEMPLATE (decl);
2471 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2472 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2473 if (TREE_PURPOSE (t))
2474 break;
2475 if (!t)
2476 return;
2477
2478 old_type = TREE_TYPE (decl);
2479 spec_types = TYPE_ARG_TYPES (old_type);
2480
2481 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2482 {
2483 /* Remove the this pointer, but remember the object's type for
2484 CV quals. */
2485 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2486 spec_types = TREE_CHAIN (spec_types);
2487 tmpl_types = TREE_CHAIN (tmpl_types);
2488
2489 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2490 {
2491 /* DECL may contain more parameters than TMPL due to the extra
2492 in-charge parameter in constructors and destructors. */
2493 in_charge = spec_types;
2494 spec_types = TREE_CHAIN (spec_types);
2495 }
2496 if (DECL_HAS_VTT_PARM_P (decl))
2497 {
2498 vtt = spec_types;
2499 spec_types = TREE_CHAIN (spec_types);
2500 }
2501 }
2502
2503 /* Compute the merged default arguments. */
2504 new_spec_types =
2505 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2506
2507 /* Compute the new FUNCTION_TYPE. */
2508 if (object_type)
2509 {
2510 if (vtt)
2511 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2512 TREE_VALUE (vtt),
2513 new_spec_types);
2514
2515 if (in_charge)
2516 /* Put the in-charge parameter back. */
2517 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2518 TREE_VALUE (in_charge),
2519 new_spec_types);
2520
2521 new_type = build_method_type_directly (object_type,
2522 TREE_TYPE (old_type),
2523 new_spec_types);
2524 }
2525 else
2526 new_type = build_function_type (TREE_TYPE (old_type),
2527 new_spec_types);
2528 new_type = cp_build_type_attribute_variant (new_type,
2529 TYPE_ATTRIBUTES (old_type));
2530 new_type = build_exception_variant (new_type,
2531 TYPE_RAISES_EXCEPTIONS (old_type));
2532
2533 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2534 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2535
2536 TREE_TYPE (decl) = new_type;
2537 }
2538
2539 /* Return the number of template headers we expect to see for a definition
2540 or specialization of CTYPE or one of its non-template members. */
2541
2542 int
2543 num_template_headers_for_class (tree ctype)
2544 {
2545 int num_templates = 0;
2546
2547 while (ctype && CLASS_TYPE_P (ctype))
2548 {
2549 /* You're supposed to have one `template <...>' for every
2550 template class, but you don't need one for a full
2551 specialization. For example:
2552
2553 template <class T> struct S{};
2554 template <> struct S<int> { void f(); };
2555 void S<int>::f () {}
2556
2557 is correct; there shouldn't be a `template <>' for the
2558 definition of `S<int>::f'. */
2559 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2560 /* If CTYPE does not have template information of any
2561 kind, then it is not a template, nor is it nested
2562 within a template. */
2563 break;
2564 if (explicit_class_specialization_p (ctype))
2565 break;
2566 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2567 ++num_templates;
2568
2569 ctype = TYPE_CONTEXT (ctype);
2570 }
2571
2572 return num_templates;
2573 }
2574
2575 /* Do a simple sanity check on the template headers that precede the
2576 variable declaration DECL. */
2577
2578 void
2579 check_template_variable (tree decl)
2580 {
2581 tree ctx = CP_DECL_CONTEXT (decl);
2582 int wanted = num_template_headers_for_class (ctx);
2583 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2584 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2585 {
2586 if (cxx_dialect < cxx14)
2587 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2588 "variable templates only available with "
2589 "-std=c++14 or -std=gnu++14");
2590
2591 // Namespace-scope variable templates should have a template header.
2592 ++wanted;
2593 }
2594 if (template_header_count > wanted)
2595 {
2596 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2597 "too many template headers for %qD "
2598 "(should be %d)",
2599 decl, wanted);
2600 if (warned && CLASS_TYPE_P (ctx)
2601 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2602 inform (DECL_SOURCE_LOCATION (decl),
2603 "members of an explicitly specialized class are defined "
2604 "without a template header");
2605 }
2606 }
2607
2608 /* An explicit specialization whose declarator-id or class-head-name is not
2609 qualified shall be declared in the nearest enclosing namespace of the
2610 template, or, if the namespace is inline (7.3.1), any namespace from its
2611 enclosing namespace set.
2612
2613 If the name declared in the explicit instantiation is an unqualified name,
2614 the explicit instantiation shall appear in the namespace where its template
2615 is declared or, if that namespace is inline (7.3.1), any namespace from its
2616 enclosing namespace set. */
2617
2618 void
2619 check_unqualified_spec_or_inst (tree t, location_t loc)
2620 {
2621 tree tmpl = most_general_template (t);
2622 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2623 && !is_nested_namespace (current_namespace,
2624 CP_DECL_CONTEXT (tmpl), true))
2625 {
2626 if (processing_specialization)
2627 permerror (loc, "explicit specialization of %qD outside its "
2628 "namespace must use a nested-name-specifier", tmpl);
2629 else if (processing_explicit_instantiation
2630 && cxx_dialect >= cxx11)
2631 /* This was allowed in C++98, so only pedwarn. */
2632 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2633 "outside its namespace must use a nested-name-"
2634 "specifier", tmpl);
2635 }
2636 }
2637
2638 /* Warn for a template specialization SPEC that is missing some of a set
2639 of function or type attributes that the template TEMPL is declared with.
2640 ATTRLIST is a list of additional attributes that SPEC should be taken
2641 to ultimately be declared with. */
2642
2643 static void
2644 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2645 {
2646 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2647 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2648
2649 if (TREE_CODE (tmpl) != FUNCTION_DECL)
2650 return;
2651
2652 /* Avoid warning if either declaration or its type is deprecated. */
2653 if (TREE_DEPRECATED (tmpl)
2654 || TREE_DEPRECATED (spec))
2655 return;
2656
2657 tree tmpl_type = TREE_TYPE (tmpl);
2658 tree spec_type = TREE_TYPE (spec);
2659
2660 if (TREE_DEPRECATED (tmpl_type)
2661 || TREE_DEPRECATED (spec_type)
2662 || TREE_DEPRECATED (TREE_TYPE (tmpl_type))
2663 || TREE_DEPRECATED (TREE_TYPE (spec_type)))
2664 return;
2665
2666 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpl_type) };
2667 tree spec_attrs[] = { DECL_ATTRIBUTES (spec), TYPE_ATTRIBUTES (spec_type) };
2668
2669 if (!spec_attrs[0])
2670 spec_attrs[0] = attrlist;
2671 else if (!spec_attrs[1])
2672 spec_attrs[1] = attrlist;
2673
2674 /* Avoid warning if the primary has no attributes. */
2675 if (!tmpl_attrs[0] && !tmpl_attrs[1])
2676 return;
2677
2678 /* Avoid warning if either declaration contains an attribute on
2679 the white list below. */
2680 const char* const whitelist[] = {
2681 "error", "warning"
2682 };
2683
2684 for (unsigned i = 0; i != 2; ++i)
2685 for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j)
2686 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
2687 || lookup_attribute (whitelist[j], spec_attrs[i]))
2688 return;
2689
2690 /* Avoid warning if the difference between the primary and
2691 the specialization is not in one of the attributes below. */
2692 const char* const blacklist[] = {
2693 "alloc_align", "alloc_size", "assume_aligned", "format",
2694 "format_arg", "malloc", "nonnull"
2695 };
2696
2697 /* Put together a list of the black listed attributes that the primary
2698 template is declared with that the specialization is not, in case
2699 it's not apparent from the most recent declaration of the primary. */
2700 unsigned nattrs = 0;
2701 pretty_printer str;
2702
2703 for (unsigned i = 0; i != sizeof blacklist / sizeof *blacklist; ++i)
2704 {
2705 for (unsigned j = 0; j != 2; ++j)
2706 {
2707 if (!lookup_attribute (blacklist[i], tmpl_attrs[j]))
2708 continue;
2709
2710 for (unsigned k = 0; k != 1 + !!spec_attrs[1]; ++k)
2711 {
2712 if (lookup_attribute (blacklist[i], spec_attrs[k]))
2713 break;
2714
2715 if (nattrs)
2716 pp_string (&str, ", ");
2717 pp_begin_quote (&str, pp_show_color (global_dc->printer));
2718 pp_string (&str, blacklist[i]);
2719 pp_end_quote (&str, pp_show_color (global_dc->printer));
2720 ++nattrs;
2721 }
2722 }
2723 }
2724
2725 if (!nattrs)
2726 return;
2727
2728 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2729 "explicit specialization %q#D may be missing attributes",
2730 spec))
2731 inform (DECL_SOURCE_LOCATION (tmpl),
2732 nattrs > 1
2733 ? G_("missing primary template attributes %s")
2734 : G_("missing primary template attribute %s"),
2735 pp_formatted_text (&str));
2736 }
2737
2738 /* Check to see if the function just declared, as indicated in
2739 DECLARATOR, and in DECL, is a specialization of a function
2740 template. We may also discover that the declaration is an explicit
2741 instantiation at this point.
2742
2743 Returns DECL, or an equivalent declaration that should be used
2744 instead if all goes well. Issues an error message if something is
2745 amiss. Returns error_mark_node if the error is not easily
2746 recoverable.
2747
2748 FLAGS is a bitmask consisting of the following flags:
2749
2750 2: The function has a definition.
2751 4: The function is a friend.
2752
2753 The TEMPLATE_COUNT is the number of references to qualifying
2754 template classes that appeared in the name of the function. For
2755 example, in
2756
2757 template <class T> struct S { void f(); };
2758 void S<int>::f();
2759
2760 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2761 classes are not counted in the TEMPLATE_COUNT, so that in
2762
2763 template <class T> struct S {};
2764 template <> struct S<int> { void f(); }
2765 template <> void S<int>::f();
2766
2767 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2768 invalid; there should be no template <>.)
2769
2770 If the function is a specialization, it is marked as such via
2771 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2772 is set up correctly, and it is added to the list of specializations
2773 for that template. */
2774
2775 tree
2776 check_explicit_specialization (tree declarator,
2777 tree decl,
2778 int template_count,
2779 int flags,
2780 tree attrlist)
2781 {
2782 int have_def = flags & 2;
2783 int is_friend = flags & 4;
2784 bool is_concept = flags & 8;
2785 int specialization = 0;
2786 int explicit_instantiation = 0;
2787 int member_specialization = 0;
2788 tree ctype = DECL_CLASS_CONTEXT (decl);
2789 tree dname = DECL_NAME (decl);
2790 tmpl_spec_kind tsk;
2791
2792 if (is_friend)
2793 {
2794 if (!processing_specialization)
2795 tsk = tsk_none;
2796 else
2797 tsk = tsk_excessive_parms;
2798 }
2799 else
2800 tsk = current_tmpl_spec_kind (template_count);
2801
2802 switch (tsk)
2803 {
2804 case tsk_none:
2805 if (processing_specialization && !VAR_P (decl))
2806 {
2807 specialization = 1;
2808 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2809 }
2810 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2811 {
2812 if (is_friend)
2813 /* This could be something like:
2814
2815 template <class T> void f(T);
2816 class S { friend void f<>(int); } */
2817 specialization = 1;
2818 else
2819 {
2820 /* This case handles bogus declarations like template <>
2821 template <class T> void f<int>(); */
2822
2823 error ("template-id %qD in declaration of primary template",
2824 declarator);
2825 return decl;
2826 }
2827 }
2828 break;
2829
2830 case tsk_invalid_member_spec:
2831 /* The error has already been reported in
2832 check_specialization_scope. */
2833 return error_mark_node;
2834
2835 case tsk_invalid_expl_inst:
2836 error ("template parameter list used in explicit instantiation");
2837
2838 /* Fall through. */
2839
2840 case tsk_expl_inst:
2841 if (have_def)
2842 error ("definition provided for explicit instantiation");
2843
2844 explicit_instantiation = 1;
2845 break;
2846
2847 case tsk_excessive_parms:
2848 case tsk_insufficient_parms:
2849 if (tsk == tsk_excessive_parms)
2850 error ("too many template parameter lists in declaration of %qD",
2851 decl);
2852 else if (template_header_count)
2853 error("too few template parameter lists in declaration of %qD", decl);
2854 else
2855 error("explicit specialization of %qD must be introduced by "
2856 "%<template <>%>", decl);
2857
2858 /* Fall through. */
2859 case tsk_expl_spec:
2860 if (is_concept)
2861 error ("explicit specialization declared %<concept%>");
2862
2863 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2864 /* In cases like template<> constexpr bool v = true;
2865 We'll give an error in check_template_variable. */
2866 break;
2867
2868 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2869 if (ctype)
2870 member_specialization = 1;
2871 else
2872 specialization = 1;
2873 break;
2874
2875 case tsk_template:
2876 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2877 {
2878 /* This case handles bogus declarations like template <>
2879 template <class T> void f<int>(); */
2880
2881 if (!uses_template_parms (declarator))
2882 error ("template-id %qD in declaration of primary template",
2883 declarator);
2884 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2885 {
2886 /* Partial specialization of variable template. */
2887 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2888 specialization = 1;
2889 goto ok;
2890 }
2891 else if (cxx_dialect < cxx14)
2892 error ("non-type partial specialization %qD "
2893 "is not allowed", declarator);
2894 else
2895 error ("non-class, non-variable partial specialization %qD "
2896 "is not allowed", declarator);
2897 return decl;
2898 ok:;
2899 }
2900
2901 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2902 /* This is a specialization of a member template, without
2903 specialization the containing class. Something like:
2904
2905 template <class T> struct S {
2906 template <class U> void f (U);
2907 };
2908 template <> template <class U> void S<int>::f(U) {}
2909
2910 That's a specialization -- but of the entire template. */
2911 specialization = 1;
2912 break;
2913
2914 default:
2915 gcc_unreachable ();
2916 }
2917
2918 if ((specialization || member_specialization)
2919 /* This doesn't apply to variable templates. */
2920 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2921 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2922 {
2923 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2924 for (; t; t = TREE_CHAIN (t))
2925 if (TREE_PURPOSE (t))
2926 {
2927 permerror (input_location,
2928 "default argument specified in explicit specialization");
2929 break;
2930 }
2931 }
2932
2933 if (specialization || member_specialization || explicit_instantiation)
2934 {
2935 tree tmpl = NULL_TREE;
2936 tree targs = NULL_TREE;
2937 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2938
2939 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2940 if (!was_template_id)
2941 {
2942 tree fns;
2943
2944 gcc_assert (identifier_p (declarator));
2945 if (ctype)
2946 fns = dname;
2947 else
2948 {
2949 /* If there is no class context, the explicit instantiation
2950 must be at namespace scope. */
2951 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2952
2953 /* Find the namespace binding, using the declaration
2954 context. */
2955 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2956 false, true);
2957 if (fns == error_mark_node)
2958 /* If lookup fails, look for a friend declaration so we can
2959 give a better diagnostic. */
2960 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2961 /*type*/false, /*complain*/true,
2962 /*hidden*/true);
2963
2964 if (fns == error_mark_node || !is_overloaded_fn (fns))
2965 {
2966 error ("%qD is not a template function", dname);
2967 fns = error_mark_node;
2968 }
2969 }
2970
2971 declarator = lookup_template_function (fns, NULL_TREE);
2972 }
2973
2974 if (declarator == error_mark_node)
2975 return error_mark_node;
2976
2977 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2978 {
2979 if (!explicit_instantiation)
2980 /* A specialization in class scope. This is invalid,
2981 but the error will already have been flagged by
2982 check_specialization_scope. */
2983 return error_mark_node;
2984 else
2985 {
2986 /* It's not valid to write an explicit instantiation in
2987 class scope, e.g.:
2988
2989 class C { template void f(); }
2990
2991 This case is caught by the parser. However, on
2992 something like:
2993
2994 template class C { void f(); };
2995
2996 (which is invalid) we can get here. The error will be
2997 issued later. */
2998 ;
2999 }
3000
3001 return decl;
3002 }
3003 else if (ctype != NULL_TREE
3004 && (identifier_p (TREE_OPERAND (declarator, 0))))
3005 {
3006 // We'll match variable templates in start_decl.
3007 if (VAR_P (decl))
3008 return decl;
3009
3010 /* Find the list of functions in ctype that have the same
3011 name as the declared function. */
3012 tree name = TREE_OPERAND (declarator, 0);
3013
3014 if (constructor_name_p (name, ctype))
3015 {
3016 if (DECL_CONSTRUCTOR_P (decl)
3017 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3018 : !CLASSTYPE_DESTRUCTOR (ctype))
3019 {
3020 /* From [temp.expl.spec]:
3021
3022 If such an explicit specialization for the member
3023 of a class template names an implicitly-declared
3024 special member function (clause _special_), the
3025 program is ill-formed.
3026
3027 Similar language is found in [temp.explicit]. */
3028 error ("specialization of implicitly-declared special member function");
3029 return error_mark_node;
3030 }
3031
3032 name = DECL_NAME (decl);
3033 }
3034
3035 /* For a type-conversion operator, We might be looking for
3036 `operator int' which will be a specialization of
3037 `operator T'. Grab all the conversion operators, and
3038 then select from them. */
3039 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3040 ? conv_op_identifier : name);
3041
3042 if (fns == NULL_TREE)
3043 {
3044 error ("no member function %qD declared in %qT", name, ctype);
3045 return error_mark_node;
3046 }
3047 else
3048 TREE_OPERAND (declarator, 0) = fns;
3049 }
3050
3051 /* Figure out what exactly is being specialized at this point.
3052 Note that for an explicit instantiation, even one for a
3053 member function, we cannot tell a priori whether the
3054 instantiation is for a member template, or just a member
3055 function of a template class. Even if a member template is
3056 being instantiated, the member template arguments may be
3057 elided if they can be deduced from the rest of the
3058 declaration. */
3059 tmpl = determine_specialization (declarator, decl,
3060 &targs,
3061 member_specialization,
3062 template_count,
3063 tsk);
3064
3065 if (!tmpl || tmpl == error_mark_node)
3066 /* We couldn't figure out what this declaration was
3067 specializing. */
3068 return error_mark_node;
3069 else
3070 {
3071 if (TREE_CODE (decl) == FUNCTION_DECL
3072 && DECL_HIDDEN_FRIEND_P (tmpl))
3073 {
3074 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3075 "friend declaration %qD is not visible to "
3076 "explicit specialization", tmpl))
3077 inform (DECL_SOURCE_LOCATION (tmpl),
3078 "friend declaration here");
3079 }
3080 else if (!ctype && !is_friend
3081 && CP_DECL_CONTEXT (decl) == current_namespace)
3082 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3083
3084 tree gen_tmpl = most_general_template (tmpl);
3085
3086 if (explicit_instantiation)
3087 {
3088 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3089 is done by do_decl_instantiation later. */
3090
3091 int arg_depth = TMPL_ARGS_DEPTH (targs);
3092 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3093
3094 if (arg_depth > parm_depth)
3095 {
3096 /* If TMPL is not the most general template (for
3097 example, if TMPL is a friend template that is
3098 injected into namespace scope), then there will
3099 be too many levels of TARGS. Remove some of them
3100 here. */
3101 int i;
3102 tree new_targs;
3103
3104 new_targs = make_tree_vec (parm_depth);
3105 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3106 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3107 = TREE_VEC_ELT (targs, i);
3108 targs = new_targs;
3109 }
3110
3111 return instantiate_template (tmpl, targs, tf_error);
3112 }
3113
3114 /* If we thought that the DECL was a member function, but it
3115 turns out to be specializing a static member function,
3116 make DECL a static member function as well. */
3117 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3118 && DECL_STATIC_FUNCTION_P (tmpl)
3119 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3120 revert_static_member_fn (decl);
3121
3122 /* If this is a specialization of a member template of a
3123 template class, we want to return the TEMPLATE_DECL, not
3124 the specialization of it. */
3125 if (tsk == tsk_template && !was_template_id)
3126 {
3127 tree result = DECL_TEMPLATE_RESULT (tmpl);
3128 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3129 DECL_INITIAL (result) = NULL_TREE;
3130 if (have_def)
3131 {
3132 tree parm;
3133 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3134 DECL_SOURCE_LOCATION (result)
3135 = DECL_SOURCE_LOCATION (decl);
3136 /* We want to use the argument list specified in the
3137 definition, not in the original declaration. */
3138 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3139 for (parm = DECL_ARGUMENTS (result); parm;
3140 parm = DECL_CHAIN (parm))
3141 DECL_CONTEXT (parm) = result;
3142 }
3143 return register_specialization (tmpl, gen_tmpl, targs,
3144 is_friend, 0);
3145 }
3146
3147 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3148 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3149
3150 if (was_template_id)
3151 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3152
3153 /* Inherit default function arguments from the template
3154 DECL is specializing. */
3155 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3156 copy_default_args_to_explicit_spec (decl);
3157
3158 /* This specialization has the same protection as the
3159 template it specializes. */
3160 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3161 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3162
3163 /* 7.1.1-1 [dcl.stc]
3164
3165 A storage-class-specifier shall not be specified in an
3166 explicit specialization...
3167
3168 The parser rejects these, so unless action is taken here,
3169 explicit function specializations will always appear with
3170 global linkage.
3171
3172 The action recommended by the C++ CWG in response to C++
3173 defect report 605 is to make the storage class and linkage
3174 of the explicit specialization match the templated function:
3175
3176 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3177 */
3178 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3179 {
3180 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3181 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3182
3183 /* A concept cannot be specialized. */
3184 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3185 {
3186 error ("explicit specialization of function concept %qD",
3187 gen_tmpl);
3188 return error_mark_node;
3189 }
3190
3191 /* This specialization has the same linkage and visibility as
3192 the function template it specializes. */
3193 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3194 if (! TREE_PUBLIC (decl))
3195 {
3196 DECL_INTERFACE_KNOWN (decl) = 1;
3197 DECL_NOT_REALLY_EXTERN (decl) = 1;
3198 }
3199 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3200 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3201 {
3202 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3203 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3204 }
3205 }
3206
3207 /* If DECL is a friend declaration, declared using an
3208 unqualified name, the namespace associated with DECL may
3209 have been set incorrectly. For example, in:
3210
3211 template <typename T> void f(T);
3212 namespace N {
3213 struct S { friend void f<int>(int); }
3214 }
3215
3216 we will have set the DECL_CONTEXT for the friend
3217 declaration to N, rather than to the global namespace. */
3218 if (DECL_NAMESPACE_SCOPE_P (decl))
3219 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3220
3221 if (is_friend && !have_def)
3222 /* This is not really a declaration of a specialization.
3223 It's just the name of an instantiation. But, it's not
3224 a request for an instantiation, either. */
3225 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3226 else if (TREE_CODE (decl) == FUNCTION_DECL)
3227 /* A specialization is not necessarily COMDAT. */
3228 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3229 && DECL_DECLARED_INLINE_P (decl));
3230 else if (VAR_P (decl))
3231 DECL_COMDAT (decl) = false;
3232
3233 /* If this is a full specialization, register it so that we can find
3234 it again. Partial specializations will be registered in
3235 process_partial_specialization. */
3236 if (!processing_template_decl)
3237 {
3238 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3239
3240 decl = register_specialization (decl, gen_tmpl, targs,
3241 is_friend, 0);
3242 }
3243
3244
3245 /* A 'structor should already have clones. */
3246 gcc_assert (decl == error_mark_node
3247 || variable_template_p (tmpl)
3248 || !(DECL_CONSTRUCTOR_P (decl)
3249 || DECL_DESTRUCTOR_P (decl))
3250 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3251 }
3252 }
3253
3254 return decl;
3255 }
3256
3257 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3258 parameters. These are represented in the same format used for
3259 DECL_TEMPLATE_PARMS. */
3260
3261 int
3262 comp_template_parms (const_tree parms1, const_tree parms2)
3263 {
3264 const_tree p1;
3265 const_tree p2;
3266
3267 if (parms1 == parms2)
3268 return 1;
3269
3270 for (p1 = parms1, p2 = parms2;
3271 p1 != NULL_TREE && p2 != NULL_TREE;
3272 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3273 {
3274 tree t1 = TREE_VALUE (p1);
3275 tree t2 = TREE_VALUE (p2);
3276 int i;
3277
3278 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3279 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3280
3281 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3282 return 0;
3283
3284 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3285 {
3286 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3287 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3288
3289 /* If either of the template parameters are invalid, assume
3290 they match for the sake of error recovery. */
3291 if (error_operand_p (parm1) || error_operand_p (parm2))
3292 return 1;
3293
3294 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3295 return 0;
3296
3297 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3298 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3299 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3300 continue;
3301 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3302 return 0;
3303 }
3304 }
3305
3306 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3307 /* One set of parameters has more parameters lists than the
3308 other. */
3309 return 0;
3310
3311 return 1;
3312 }
3313
3314 /* Determine whether PARM is a parameter pack. */
3315
3316 bool
3317 template_parameter_pack_p (const_tree parm)
3318 {
3319 /* Determine if we have a non-type template parameter pack. */
3320 if (TREE_CODE (parm) == PARM_DECL)
3321 return (DECL_TEMPLATE_PARM_P (parm)
3322 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3323 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3324 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3325
3326 /* If this is a list of template parameters, we could get a
3327 TYPE_DECL or a TEMPLATE_DECL. */
3328 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3329 parm = TREE_TYPE (parm);
3330
3331 /* Otherwise it must be a type template parameter. */
3332 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3333 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3334 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3335 }
3336
3337 /* Determine if T is a function parameter pack. */
3338
3339 bool
3340 function_parameter_pack_p (const_tree t)
3341 {
3342 if (t && TREE_CODE (t) == PARM_DECL)
3343 return DECL_PACK_P (t);
3344 return false;
3345 }
3346
3347 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3348 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3349
3350 tree
3351 get_function_template_decl (const_tree primary_func_tmpl_inst)
3352 {
3353 if (! primary_func_tmpl_inst
3354 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3355 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3356 return NULL;
3357
3358 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3359 }
3360
3361 /* Return true iff the function parameter PARAM_DECL was expanded
3362 from the function parameter pack PACK. */
3363
3364 bool
3365 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3366 {
3367 if (DECL_ARTIFICIAL (param_decl)
3368 || !function_parameter_pack_p (pack))
3369 return false;
3370
3371 /* The parameter pack and its pack arguments have the same
3372 DECL_PARM_INDEX. */
3373 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3374 }
3375
3376 /* Determine whether ARGS describes a variadic template args list,
3377 i.e., one that is terminated by a template argument pack. */
3378
3379 static bool
3380 template_args_variadic_p (tree args)
3381 {
3382 int nargs;
3383 tree last_parm;
3384
3385 if (args == NULL_TREE)
3386 return false;
3387
3388 args = INNERMOST_TEMPLATE_ARGS (args);
3389 nargs = TREE_VEC_LENGTH (args);
3390
3391 if (nargs == 0)
3392 return false;
3393
3394 last_parm = TREE_VEC_ELT (args, nargs - 1);
3395
3396 return ARGUMENT_PACK_P (last_parm);
3397 }
3398
3399 /* Generate a new name for the parameter pack name NAME (an
3400 IDENTIFIER_NODE) that incorporates its */
3401
3402 static tree
3403 make_ith_pack_parameter_name (tree name, int i)
3404 {
3405 /* Munge the name to include the parameter index. */
3406 #define NUMBUF_LEN 128
3407 char numbuf[NUMBUF_LEN];
3408 char* newname;
3409 int newname_len;
3410
3411 if (name == NULL_TREE)
3412 return name;
3413 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3414 newname_len = IDENTIFIER_LENGTH (name)
3415 + strlen (numbuf) + 2;
3416 newname = (char*)alloca (newname_len);
3417 snprintf (newname, newname_len,
3418 "%s#%i", IDENTIFIER_POINTER (name), i);
3419 return get_identifier (newname);
3420 }
3421
3422 /* Return true if T is a primary function, class or alias template
3423 specialization, not including the template pattern. */
3424
3425 bool
3426 primary_template_specialization_p (const_tree t)
3427 {
3428 if (!t)
3429 return false;
3430
3431 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3432 return (DECL_LANG_SPECIFIC (t)
3433 && DECL_USE_TEMPLATE (t)
3434 && DECL_TEMPLATE_INFO (t)
3435 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3436 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3437 return (CLASSTYPE_TEMPLATE_INFO (t)
3438 && CLASSTYPE_USE_TEMPLATE (t)
3439 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3440 else if (alias_template_specialization_p (t))
3441 return true;
3442 return false;
3443 }
3444
3445 /* Return true if PARM is a template template parameter. */
3446
3447 bool
3448 template_template_parameter_p (const_tree parm)
3449 {
3450 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3451 }
3452
3453 /* Return true iff PARM is a DECL representing a type template
3454 parameter. */
3455
3456 bool
3457 template_type_parameter_p (const_tree parm)
3458 {
3459 return (parm
3460 && (TREE_CODE (parm) == TYPE_DECL
3461 || TREE_CODE (parm) == TEMPLATE_DECL)
3462 && DECL_TEMPLATE_PARM_P (parm));
3463 }
3464
3465 /* Return the template parameters of T if T is a
3466 primary template instantiation, NULL otherwise. */
3467
3468 tree
3469 get_primary_template_innermost_parameters (const_tree t)
3470 {
3471 tree parms = NULL, template_info = NULL;
3472
3473 if ((template_info = get_template_info (t))
3474 && primary_template_specialization_p (t))
3475 parms = INNERMOST_TEMPLATE_PARMS
3476 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3477
3478 return parms;
3479 }
3480
3481 /* Return the template parameters of the LEVELth level from the full list
3482 of template parameters PARMS. */
3483
3484 tree
3485 get_template_parms_at_level (tree parms, int level)
3486 {
3487 tree p;
3488 if (!parms
3489 || TREE_CODE (parms) != TREE_LIST
3490 || level > TMPL_PARMS_DEPTH (parms))
3491 return NULL_TREE;
3492
3493 for (p = parms; p; p = TREE_CHAIN (p))
3494 if (TMPL_PARMS_DEPTH (p) == level)
3495 return p;
3496
3497 return NULL_TREE;
3498 }
3499
3500 /* Returns the template arguments of T if T is a template instantiation,
3501 NULL otherwise. */
3502
3503 tree
3504 get_template_innermost_arguments (const_tree t)
3505 {
3506 tree args = NULL, template_info = NULL;
3507
3508 if ((template_info = get_template_info (t))
3509 && TI_ARGS (template_info))
3510 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3511
3512 return args;
3513 }
3514
3515 /* Return the argument pack elements of T if T is a template argument pack,
3516 NULL otherwise. */
3517
3518 tree
3519 get_template_argument_pack_elems (const_tree t)
3520 {
3521 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3522 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3523 return NULL;
3524
3525 return ARGUMENT_PACK_ARGS (t);
3526 }
3527
3528 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3529 ARGUMENT_PACK_SELECT represents. */
3530
3531 static tree
3532 argument_pack_select_arg (tree t)
3533 {
3534 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3535 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3536
3537 /* If the selected argument is an expansion E, that most likely means we were
3538 called from gen_elem_of_pack_expansion_instantiation during the
3539 substituting of an argument pack (of which the Ith element is a pack
3540 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3541 In this case, the Ith element resulting from this substituting is going to
3542 be a pack expansion, which pattern is the pattern of E. Let's return the
3543 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3544 resulting pack expansion from it. */
3545 if (PACK_EXPANSION_P (arg))
3546 {
3547 /* Make sure we aren't throwing away arg info. */
3548 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3549 arg = PACK_EXPANSION_PATTERN (arg);
3550 }
3551
3552 return arg;
3553 }
3554
3555
3556 /* True iff FN is a function representing a built-in variadic parameter
3557 pack. */
3558
3559 bool
3560 builtin_pack_fn_p (tree fn)
3561 {
3562 if (!fn
3563 || TREE_CODE (fn) != FUNCTION_DECL
3564 || !DECL_IS_BUILTIN (fn))
3565 return false;
3566
3567 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3568 return true;
3569
3570 return false;
3571 }
3572
3573 /* True iff CALL is a call to a function representing a built-in variadic
3574 parameter pack. */
3575
3576 static bool
3577 builtin_pack_call_p (tree call)
3578 {
3579 if (TREE_CODE (call) != CALL_EXPR)
3580 return false;
3581 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3582 }
3583
3584 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3585
3586 static tree
3587 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3588 tree in_decl)
3589 {
3590 tree ohi = CALL_EXPR_ARG (call, 0);
3591 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3592 false/*fn*/, true/*int_cst*/);
3593
3594 if (value_dependent_expression_p (hi))
3595 {
3596 if (hi != ohi)
3597 {
3598 call = copy_node (call);
3599 CALL_EXPR_ARG (call, 0) = hi;
3600 }
3601 tree ex = make_pack_expansion (call, complain);
3602 tree vec = make_tree_vec (1);
3603 TREE_VEC_ELT (vec, 0) = ex;
3604 return vec;
3605 }
3606 else
3607 {
3608 hi = cxx_constant_value (hi);
3609 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3610
3611 /* Calculate the largest value of len that won't make the size of the vec
3612 overflow an int. The compiler will exceed resource limits long before
3613 this, but it seems a decent place to diagnose. */
3614 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3615
3616 if (len < 0 || len > max)
3617 {
3618 if ((complain & tf_error)
3619 && hi != error_mark_node)
3620 error ("argument to __integer_pack must be between 0 and %d", max);
3621 return error_mark_node;
3622 }
3623
3624 tree vec = make_tree_vec (len);
3625
3626 for (int i = 0; i < len; ++i)
3627 TREE_VEC_ELT (vec, i) = size_int (i);
3628
3629 return vec;
3630 }
3631 }
3632
3633 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3634 CALL. */
3635
3636 static tree
3637 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3638 tree in_decl)
3639 {
3640 if (!builtin_pack_call_p (call))
3641 return NULL_TREE;
3642
3643 tree fn = CALL_EXPR_FN (call);
3644
3645 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3646 return expand_integer_pack (call, args, complain, in_decl);
3647
3648 return NULL_TREE;
3649 }
3650
3651 /* Structure used to track the progress of find_parameter_packs_r. */
3652 struct find_parameter_pack_data
3653 {
3654 /* TREE_LIST that will contain all of the parameter packs found by
3655 the traversal. */
3656 tree* parameter_packs;
3657
3658 /* Set of AST nodes that have been visited by the traversal. */
3659 hash_set<tree> *visited;
3660
3661 /* True iff we're making a type pack expansion. */
3662 bool type_pack_expansion_p;
3663 };
3664
3665 /* Identifies all of the argument packs that occur in a template
3666 argument and appends them to the TREE_LIST inside DATA, which is a
3667 find_parameter_pack_data structure. This is a subroutine of
3668 make_pack_expansion and uses_parameter_packs. */
3669 static tree
3670 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3671 {
3672 tree t = *tp;
3673 struct find_parameter_pack_data* ppd =
3674 (struct find_parameter_pack_data*)data;
3675 bool parameter_pack_p = false;
3676
3677 /* Handle type aliases/typedefs. */
3678 if (TYPE_ALIAS_P (t))
3679 {
3680 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3681 cp_walk_tree (&TI_ARGS (tinfo),
3682 &find_parameter_packs_r,
3683 ppd, ppd->visited);
3684 *walk_subtrees = 0;
3685 return NULL_TREE;
3686 }
3687
3688 /* Identify whether this is a parameter pack or not. */
3689 switch (TREE_CODE (t))
3690 {
3691 case TEMPLATE_PARM_INDEX:
3692 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3693 parameter_pack_p = true;
3694 break;
3695
3696 case TEMPLATE_TYPE_PARM:
3697 t = TYPE_MAIN_VARIANT (t);
3698 /* FALLTHRU */
3699 case TEMPLATE_TEMPLATE_PARM:
3700 /* If the placeholder appears in the decl-specifier-seq of a function
3701 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3702 is a pack expansion, the invented template parameter is a template
3703 parameter pack. */
3704 if (ppd->type_pack_expansion_p && is_auto (t))
3705 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3706 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3707 parameter_pack_p = true;
3708 break;
3709
3710 case FIELD_DECL:
3711 case PARM_DECL:
3712 if (DECL_PACK_P (t))
3713 {
3714 /* We don't want to walk into the type of a PARM_DECL,
3715 because we don't want to see the type parameter pack. */
3716 *walk_subtrees = 0;
3717 parameter_pack_p = true;
3718 }
3719 break;
3720
3721 case VAR_DECL:
3722 if (DECL_PACK_P (t))
3723 {
3724 /* We don't want to walk into the type of a variadic capture proxy,
3725 because we don't want to see the type parameter pack. */
3726 *walk_subtrees = 0;
3727 parameter_pack_p = true;
3728 }
3729 else if (variable_template_specialization_p (t))
3730 {
3731 cp_walk_tree (&DECL_TI_ARGS (t),
3732 find_parameter_packs_r,
3733 ppd, ppd->visited);
3734 *walk_subtrees = 0;
3735 }
3736 break;
3737
3738 case CALL_EXPR:
3739 if (builtin_pack_call_p (t))
3740 parameter_pack_p = true;
3741 break;
3742
3743 case BASES:
3744 parameter_pack_p = true;
3745 break;
3746 default:
3747 /* Not a parameter pack. */
3748 break;
3749 }
3750
3751 if (parameter_pack_p)
3752 {
3753 /* Add this parameter pack to the list. */
3754 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3755 }
3756
3757 if (TYPE_P (t))
3758 cp_walk_tree (&TYPE_CONTEXT (t),
3759 &find_parameter_packs_r, ppd, ppd->visited);
3760
3761 /* This switch statement will return immediately if we don't find a
3762 parameter pack. */
3763 switch (TREE_CODE (t))
3764 {
3765 case TEMPLATE_PARM_INDEX:
3766 return NULL_TREE;
3767
3768 case BOUND_TEMPLATE_TEMPLATE_PARM:
3769 /* Check the template itself. */
3770 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3771 &find_parameter_packs_r, ppd, ppd->visited);
3772 /* Check the template arguments. */
3773 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3774 ppd->visited);
3775 *walk_subtrees = 0;
3776 return NULL_TREE;
3777
3778 case TEMPLATE_TYPE_PARM:
3779 case TEMPLATE_TEMPLATE_PARM:
3780 return NULL_TREE;
3781
3782 case PARM_DECL:
3783 return NULL_TREE;
3784
3785 case DECL_EXPR:
3786 /* Ignore the declaration of a capture proxy for a parameter pack. */
3787 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3788 *walk_subtrees = 0;
3789 return NULL_TREE;
3790
3791 case RECORD_TYPE:
3792 if (TYPE_PTRMEMFUNC_P (t))
3793 return NULL_TREE;
3794 /* Fall through. */
3795
3796 case UNION_TYPE:
3797 case ENUMERAL_TYPE:
3798 if (TYPE_TEMPLATE_INFO (t))
3799 cp_walk_tree (&TYPE_TI_ARGS (t),
3800 &find_parameter_packs_r, ppd, ppd->visited);
3801
3802 *walk_subtrees = 0;
3803 return NULL_TREE;
3804
3805 case TEMPLATE_DECL:
3806 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3807 return NULL_TREE;
3808 gcc_fallthrough();
3809
3810 case CONSTRUCTOR:
3811 cp_walk_tree (&TREE_TYPE (t),
3812 &find_parameter_packs_r, ppd, ppd->visited);
3813 return NULL_TREE;
3814
3815 case TYPENAME_TYPE:
3816 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3817 ppd, ppd->visited);
3818 *walk_subtrees = 0;
3819 return NULL_TREE;
3820
3821 case TYPE_PACK_EXPANSION:
3822 case EXPR_PACK_EXPANSION:
3823 *walk_subtrees = 0;
3824 return NULL_TREE;
3825
3826 case INTEGER_TYPE:
3827 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3828 ppd, ppd->visited);
3829 *walk_subtrees = 0;
3830 return NULL_TREE;
3831
3832 case IDENTIFIER_NODE:
3833 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3834 ppd->visited);
3835 *walk_subtrees = 0;
3836 return NULL_TREE;
3837
3838 case LAMBDA_EXPR:
3839 {
3840 /* Look at explicit captures. */
3841 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3842 cap; cap = TREE_CHAIN (cap))
3843 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3844 ppd->visited);
3845 /* Since we defer implicit capture, look in the body as well. */
3846 tree fn = lambda_function (t);
3847 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3848 ppd->visited);
3849 *walk_subtrees = 0;
3850 return NULL_TREE;
3851 }
3852
3853 case DECLTYPE_TYPE:
3854 {
3855 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3856 type_pack_expansion_p to false so that any placeholders
3857 within the expression don't get marked as parameter packs. */
3858 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3859 ppd->type_pack_expansion_p = false;
3860 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3861 ppd, ppd->visited);
3862 ppd->type_pack_expansion_p = type_pack_expansion_p;
3863 *walk_subtrees = 0;
3864 return NULL_TREE;
3865 }
3866
3867 default:
3868 return NULL_TREE;
3869 }
3870
3871 return NULL_TREE;
3872 }
3873
3874 /* Determines if the expression or type T uses any parameter packs. */
3875 bool
3876 uses_parameter_packs (tree t)
3877 {
3878 tree parameter_packs = NULL_TREE;
3879 struct find_parameter_pack_data ppd;
3880 ppd.parameter_packs = &parameter_packs;
3881 ppd.visited = new hash_set<tree>;
3882 ppd.type_pack_expansion_p = false;
3883 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3884 delete ppd.visited;
3885 return parameter_packs != NULL_TREE;
3886 }
3887
3888 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3889 representation a base-class initializer into a parameter pack
3890 expansion. If all goes well, the resulting node will be an
3891 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3892 respectively. */
3893 tree
3894 make_pack_expansion (tree arg, tsubst_flags_t complain)
3895 {
3896 tree result;
3897 tree parameter_packs = NULL_TREE;
3898 bool for_types = false;
3899 struct find_parameter_pack_data ppd;
3900
3901 if (!arg || arg == error_mark_node)
3902 return arg;
3903
3904 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3905 {
3906 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3907 class initializer. In this case, the TREE_PURPOSE will be a
3908 _TYPE node (representing the base class expansion we're
3909 initializing) and the TREE_VALUE will be a TREE_LIST
3910 containing the initialization arguments.
3911
3912 The resulting expansion looks somewhat different from most
3913 expansions. Rather than returning just one _EXPANSION, we
3914 return a TREE_LIST whose TREE_PURPOSE is a
3915 TYPE_PACK_EXPANSION containing the bases that will be
3916 initialized. The TREE_VALUE will be identical to the
3917 original TREE_VALUE, which is a list of arguments that will
3918 be passed to each base. We do not introduce any new pack
3919 expansion nodes into the TREE_VALUE (although it is possible
3920 that some already exist), because the TREE_PURPOSE and
3921 TREE_VALUE all need to be expanded together with the same
3922 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3923 resulting TREE_PURPOSE will mention the parameter packs in
3924 both the bases and the arguments to the bases. */
3925 tree purpose;
3926 tree value;
3927 tree parameter_packs = NULL_TREE;
3928
3929 /* Determine which parameter packs will be used by the base
3930 class expansion. */
3931 ppd.visited = new hash_set<tree>;
3932 ppd.parameter_packs = &parameter_packs;
3933 ppd.type_pack_expansion_p = true;
3934 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3935 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3936 &ppd, ppd.visited);
3937
3938 if (parameter_packs == NULL_TREE)
3939 {
3940 if (complain & tf_error)
3941 error ("base initializer expansion %qT contains no parameter packs",
3942 arg);
3943 delete ppd.visited;
3944 return error_mark_node;
3945 }
3946
3947 if (TREE_VALUE (arg) != void_type_node)
3948 {
3949 /* Collect the sets of parameter packs used in each of the
3950 initialization arguments. */
3951 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3952 {
3953 /* Determine which parameter packs will be expanded in this
3954 argument. */
3955 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3956 &ppd, ppd.visited);
3957 }
3958 }
3959
3960 delete ppd.visited;
3961
3962 /* Create the pack expansion type for the base type. */
3963 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3964 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3965 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3966 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3967
3968 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3969 they will rarely be compared to anything. */
3970 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3971
3972 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3973 }
3974
3975 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3976 for_types = true;
3977
3978 /* Build the PACK_EXPANSION_* node. */
3979 result = for_types
3980 ? cxx_make_type (TYPE_PACK_EXPANSION)
3981 : make_node (EXPR_PACK_EXPANSION);
3982 SET_PACK_EXPANSION_PATTERN (result, arg);
3983 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3984 {
3985 /* Propagate type and const-expression information. */
3986 TREE_TYPE (result) = TREE_TYPE (arg);
3987 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3988 /* Mark this read now, since the expansion might be length 0. */
3989 mark_exp_read (arg);
3990 }
3991 else
3992 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3993 they will rarely be compared to anything. */
3994 SET_TYPE_STRUCTURAL_EQUALITY (result);
3995
3996 /* Determine which parameter packs will be expanded. */
3997 ppd.parameter_packs = &parameter_packs;
3998 ppd.visited = new hash_set<tree>;
3999 ppd.type_pack_expansion_p = TYPE_P (arg);
4000 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4001 delete ppd.visited;
4002
4003 /* Make sure we found some parameter packs. */
4004 if (parameter_packs == NULL_TREE)
4005 {
4006 if (complain & tf_error)
4007 {
4008 if (TYPE_P (arg))
4009 error ("expansion pattern %qT contains no argument packs", arg);
4010 else
4011 error ("expansion pattern %qE contains no argument packs", arg);
4012 }
4013 return error_mark_node;
4014 }
4015 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4016
4017 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4018
4019 return result;
4020 }
4021
4022 /* Checks T for any "bare" parameter packs, which have not yet been
4023 expanded, and issues an error if any are found. This operation can
4024 only be done on full expressions or types (e.g., an expression
4025 statement, "if" condition, etc.), because we could have expressions like:
4026
4027 foo(f(g(h(args)))...)
4028
4029 where "args" is a parameter pack. check_for_bare_parameter_packs
4030 should not be called for the subexpressions args, h(args),
4031 g(h(args)), or f(g(h(args))), because we would produce erroneous
4032 error messages.
4033
4034 Returns TRUE and emits an error if there were bare parameter packs,
4035 returns FALSE otherwise. */
4036 bool
4037 check_for_bare_parameter_packs (tree t)
4038 {
4039 tree parameter_packs = NULL_TREE;
4040 struct find_parameter_pack_data ppd;
4041
4042 if (!processing_template_decl || !t || t == error_mark_node)
4043 return false;
4044
4045 /* A lambda might use a parameter pack from the containing context. */
4046 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4047 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4048 return false;
4049
4050 if (TREE_CODE (t) == TYPE_DECL)
4051 t = TREE_TYPE (t);
4052
4053 ppd.parameter_packs = &parameter_packs;
4054 ppd.visited = new hash_set<tree>;
4055 ppd.type_pack_expansion_p = false;
4056 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4057 delete ppd.visited;
4058
4059 if (parameter_packs)
4060 {
4061 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
4062 error_at (loc, "parameter packs not expanded with %<...%>:");
4063 while (parameter_packs)
4064 {
4065 tree pack = TREE_VALUE (parameter_packs);
4066 tree name = NULL_TREE;
4067
4068 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4069 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4070 name = TYPE_NAME (pack);
4071 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4072 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4073 else if (TREE_CODE (pack) == CALL_EXPR)
4074 name = DECL_NAME (CALL_EXPR_FN (pack));
4075 else
4076 name = DECL_NAME (pack);
4077
4078 if (name)
4079 inform (loc, " %qD", name);
4080 else
4081 inform (loc, " <anonymous>");
4082
4083 parameter_packs = TREE_CHAIN (parameter_packs);
4084 }
4085
4086 return true;
4087 }
4088
4089 return false;
4090 }
4091
4092 /* Expand any parameter packs that occur in the template arguments in
4093 ARGS. */
4094 tree
4095 expand_template_argument_pack (tree args)
4096 {
4097 if (args == error_mark_node)
4098 return error_mark_node;
4099
4100 tree result_args = NULL_TREE;
4101 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4102 int num_result_args = -1;
4103 int non_default_args_count = -1;
4104
4105 /* First, determine if we need to expand anything, and the number of
4106 slots we'll need. */
4107 for (in_arg = 0; in_arg < nargs; ++in_arg)
4108 {
4109 tree arg = TREE_VEC_ELT (args, in_arg);
4110 if (arg == NULL_TREE)
4111 return args;
4112 if (ARGUMENT_PACK_P (arg))
4113 {
4114 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4115 if (num_result_args < 0)
4116 num_result_args = in_arg + num_packed;
4117 else
4118 num_result_args += num_packed;
4119 }
4120 else
4121 {
4122 if (num_result_args >= 0)
4123 num_result_args++;
4124 }
4125 }
4126
4127 /* If no expansion is necessary, we're done. */
4128 if (num_result_args < 0)
4129 return args;
4130
4131 /* Expand arguments. */
4132 result_args = make_tree_vec (num_result_args);
4133 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4134 non_default_args_count =
4135 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4136 for (in_arg = 0; in_arg < nargs; ++in_arg)
4137 {
4138 tree arg = TREE_VEC_ELT (args, in_arg);
4139 if (ARGUMENT_PACK_P (arg))
4140 {
4141 tree packed = ARGUMENT_PACK_ARGS (arg);
4142 int i, num_packed = TREE_VEC_LENGTH (packed);
4143 for (i = 0; i < num_packed; ++i, ++out_arg)
4144 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4145 if (non_default_args_count > 0)
4146 non_default_args_count += num_packed - 1;
4147 }
4148 else
4149 {
4150 TREE_VEC_ELT (result_args, out_arg) = arg;
4151 ++out_arg;
4152 }
4153 }
4154 if (non_default_args_count >= 0)
4155 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4156 return result_args;
4157 }
4158
4159 /* Checks if DECL shadows a template parameter.
4160
4161 [temp.local]: A template-parameter shall not be redeclared within its
4162 scope (including nested scopes).
4163
4164 Emits an error and returns TRUE if the DECL shadows a parameter,
4165 returns FALSE otherwise. */
4166
4167 bool
4168 check_template_shadow (tree decl)
4169 {
4170 tree olddecl;
4171
4172 /* If we're not in a template, we can't possibly shadow a template
4173 parameter. */
4174 if (!current_template_parms)
4175 return true;
4176
4177 /* Figure out what we're shadowing. */
4178 decl = OVL_FIRST (decl);
4179 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4180
4181 /* If there's no previous binding for this name, we're not shadowing
4182 anything, let alone a template parameter. */
4183 if (!olddecl)
4184 return true;
4185
4186 /* If we're not shadowing a template parameter, we're done. Note
4187 that OLDDECL might be an OVERLOAD (or perhaps even an
4188 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4189 node. */
4190 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4191 return true;
4192
4193 /* We check for decl != olddecl to avoid bogus errors for using a
4194 name inside a class. We check TPFI to avoid duplicate errors for
4195 inline member templates. */
4196 if (decl == olddecl
4197 || (DECL_TEMPLATE_PARM_P (decl)
4198 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4199 return true;
4200
4201 /* Don't complain about the injected class name, as we've already
4202 complained about the class itself. */
4203 if (DECL_SELF_REFERENCE_P (decl))
4204 return false;
4205
4206 if (DECL_TEMPLATE_PARM_P (decl))
4207 error ("declaration of template parameter %q+D shadows "
4208 "template parameter", decl);
4209 else
4210 error ("declaration of %q+#D shadows template parameter", decl);
4211 inform (DECL_SOURCE_LOCATION (olddecl),
4212 "template parameter %qD declared here", olddecl);
4213 return false;
4214 }
4215
4216 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4217 ORIG_LEVEL, DECL, and TYPE. */
4218
4219 static tree
4220 build_template_parm_index (int index,
4221 int level,
4222 int orig_level,
4223 tree decl,
4224 tree type)
4225 {
4226 tree t = make_node (TEMPLATE_PARM_INDEX);
4227 TEMPLATE_PARM_IDX (t) = index;
4228 TEMPLATE_PARM_LEVEL (t) = level;
4229 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4230 TEMPLATE_PARM_DECL (t) = decl;
4231 TREE_TYPE (t) = type;
4232 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4233 TREE_READONLY (t) = TREE_READONLY (decl);
4234
4235 return t;
4236 }
4237
4238 /* Find the canonical type parameter for the given template type
4239 parameter. Returns the canonical type parameter, which may be TYPE
4240 if no such parameter existed. */
4241
4242 static tree
4243 canonical_type_parameter (tree type)
4244 {
4245 tree list;
4246 int idx = TEMPLATE_TYPE_IDX (type);
4247 if (!canonical_template_parms)
4248 vec_alloc (canonical_template_parms, idx + 1);
4249
4250 if (canonical_template_parms->length () <= (unsigned) idx)
4251 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4252
4253 list = (*canonical_template_parms)[idx];
4254 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4255 list = TREE_CHAIN (list);
4256
4257 if (list)
4258 return TREE_VALUE (list);
4259 else
4260 {
4261 (*canonical_template_parms)[idx]
4262 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4263 return type;
4264 }
4265 }
4266
4267 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4268 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4269 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4270 new one is created. */
4271
4272 static tree
4273 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4274 tsubst_flags_t complain)
4275 {
4276 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4277 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4278 != TEMPLATE_PARM_LEVEL (index) - levels)
4279 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4280 {
4281 tree orig_decl = TEMPLATE_PARM_DECL (index);
4282 tree decl, t;
4283
4284 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4285 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4286 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4287 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4288 DECL_ARTIFICIAL (decl) = 1;
4289 SET_DECL_TEMPLATE_PARM_P (decl);
4290
4291 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4292 TEMPLATE_PARM_LEVEL (index) - levels,
4293 TEMPLATE_PARM_ORIG_LEVEL (index),
4294 decl, type);
4295 TEMPLATE_PARM_DESCENDANTS (index) = t;
4296 TEMPLATE_PARM_PARAMETER_PACK (t)
4297 = TEMPLATE_PARM_PARAMETER_PACK (index);
4298
4299 /* Template template parameters need this. */
4300 if (TREE_CODE (decl) == TEMPLATE_DECL)
4301 {
4302 DECL_TEMPLATE_RESULT (decl)
4303 = build_decl (DECL_SOURCE_LOCATION (decl),
4304 TYPE_DECL, DECL_NAME (decl), type);
4305 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4306 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4307 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4308 }
4309 }
4310
4311 return TEMPLATE_PARM_DESCENDANTS (index);
4312 }
4313
4314 /* Process information from new template parameter PARM and append it
4315 to the LIST being built. This new parameter is a non-type
4316 parameter iff IS_NON_TYPE is true. This new parameter is a
4317 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4318 is in PARM_LOC. */
4319
4320 tree
4321 process_template_parm (tree list, location_t parm_loc, tree parm,
4322 bool is_non_type, bool is_parameter_pack)
4323 {
4324 tree decl = 0;
4325 int idx = 0;
4326
4327 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4328 tree defval = TREE_PURPOSE (parm);
4329 tree constr = TREE_TYPE (parm);
4330
4331 if (list)
4332 {
4333 tree p = tree_last (list);
4334
4335 if (p && TREE_VALUE (p) != error_mark_node)
4336 {
4337 p = TREE_VALUE (p);
4338 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4339 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4340 else
4341 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4342 }
4343
4344 ++idx;
4345 }
4346
4347 if (is_non_type)
4348 {
4349 parm = TREE_VALUE (parm);
4350
4351 SET_DECL_TEMPLATE_PARM_P (parm);
4352
4353 if (TREE_TYPE (parm) != error_mark_node)
4354 {
4355 /* [temp.param]
4356
4357 The top-level cv-qualifiers on the template-parameter are
4358 ignored when determining its type. */
4359 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4360 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4361 TREE_TYPE (parm) = error_mark_node;
4362 else if (uses_parameter_packs (TREE_TYPE (parm))
4363 && !is_parameter_pack
4364 /* If we're in a nested template parameter list, the template
4365 template parameter could be a parameter pack. */
4366 && processing_template_parmlist == 1)
4367 {
4368 /* This template parameter is not a parameter pack, but it
4369 should be. Complain about "bare" parameter packs. */
4370 check_for_bare_parameter_packs (TREE_TYPE (parm));
4371
4372 /* Recover by calling this a parameter pack. */
4373 is_parameter_pack = true;
4374 }
4375 }
4376
4377 /* A template parameter is not modifiable. */
4378 TREE_CONSTANT (parm) = 1;
4379 TREE_READONLY (parm) = 1;
4380 decl = build_decl (parm_loc,
4381 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4382 TREE_CONSTANT (decl) = 1;
4383 TREE_READONLY (decl) = 1;
4384 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4385 = build_template_parm_index (idx, processing_template_decl,
4386 processing_template_decl,
4387 decl, TREE_TYPE (parm));
4388
4389 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4390 = is_parameter_pack;
4391 }
4392 else
4393 {
4394 tree t;
4395 parm = TREE_VALUE (TREE_VALUE (parm));
4396
4397 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4398 {
4399 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4400 /* This is for distinguishing between real templates and template
4401 template parameters */
4402 TREE_TYPE (parm) = t;
4403 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4404 decl = parm;
4405 }
4406 else
4407 {
4408 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4409 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4410 decl = build_decl (parm_loc,
4411 TYPE_DECL, parm, t);
4412 }
4413
4414 TYPE_NAME (t) = decl;
4415 TYPE_STUB_DECL (t) = decl;
4416 parm = decl;
4417 TEMPLATE_TYPE_PARM_INDEX (t)
4418 = build_template_parm_index (idx, processing_template_decl,
4419 processing_template_decl,
4420 decl, TREE_TYPE (parm));
4421 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4422 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4423 }
4424 DECL_ARTIFICIAL (decl) = 1;
4425 SET_DECL_TEMPLATE_PARM_P (decl);
4426
4427 /* Build requirements for the type/template parameter.
4428 This must be done after SET_DECL_TEMPLATE_PARM_P or
4429 process_template_parm could fail. */
4430 tree reqs = finish_shorthand_constraint (parm, constr);
4431
4432 pushdecl (decl);
4433
4434 if (defval && TREE_CODE (defval) == OVERLOAD)
4435 lookup_keep (defval, true);
4436
4437 /* Build the parameter node linking the parameter declaration,
4438 its default argument (if any), and its constraints (if any). */
4439 parm = build_tree_list (defval, parm);
4440 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4441
4442 return chainon (list, parm);
4443 }
4444
4445 /* The end of a template parameter list has been reached. Process the
4446 tree list into a parameter vector, converting each parameter into a more
4447 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4448 as PARM_DECLs. */
4449
4450 tree
4451 end_template_parm_list (tree parms)
4452 {
4453 int nparms;
4454 tree parm, next;
4455 tree saved_parmlist = make_tree_vec (list_length (parms));
4456
4457 /* Pop the dummy parameter level and add the real one. */
4458 current_template_parms = TREE_CHAIN (current_template_parms);
4459
4460 current_template_parms
4461 = tree_cons (size_int (processing_template_decl),
4462 saved_parmlist, current_template_parms);
4463
4464 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4465 {
4466 next = TREE_CHAIN (parm);
4467 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4468 TREE_CHAIN (parm) = NULL_TREE;
4469 }
4470
4471 --processing_template_parmlist;
4472
4473 return saved_parmlist;
4474 }
4475
4476 // Explicitly indicate the end of the template parameter list. We assume
4477 // that the current template parameters have been constructed and/or
4478 // managed explicitly, as when creating new template template parameters
4479 // from a shorthand constraint.
4480 void
4481 end_template_parm_list ()
4482 {
4483 --processing_template_parmlist;
4484 }
4485
4486 /* end_template_decl is called after a template declaration is seen. */
4487
4488 void
4489 end_template_decl (void)
4490 {
4491 reset_specialization ();
4492
4493 if (! processing_template_decl)
4494 return;
4495
4496 /* This matches the pushlevel in begin_template_parm_list. */
4497 finish_scope ();
4498
4499 --processing_template_decl;
4500 current_template_parms = TREE_CHAIN (current_template_parms);
4501 }
4502
4503 /* Takes a TREE_LIST representing a template parameter and convert it
4504 into an argument suitable to be passed to the type substitution
4505 functions. Note that If the TREE_LIST contains an error_mark
4506 node, the returned argument is error_mark_node. */
4507
4508 tree
4509 template_parm_to_arg (tree t)
4510 {
4511
4512 if (t == NULL_TREE
4513 || TREE_CODE (t) != TREE_LIST)
4514 return t;
4515
4516 if (error_operand_p (TREE_VALUE (t)))
4517 return error_mark_node;
4518
4519 t = TREE_VALUE (t);
4520
4521 if (TREE_CODE (t) == TYPE_DECL
4522 || TREE_CODE (t) == TEMPLATE_DECL)
4523 {
4524 t = TREE_TYPE (t);
4525
4526 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4527 {
4528 /* Turn this argument into a TYPE_ARGUMENT_PACK
4529 with a single element, which expands T. */
4530 tree vec = make_tree_vec (1);
4531 if (CHECKING_P)
4532 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4533
4534 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4535
4536 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4537 SET_ARGUMENT_PACK_ARGS (t, vec);
4538 }
4539 }
4540 else
4541 {
4542 t = DECL_INITIAL (t);
4543
4544 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4545 {
4546 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4547 with a single element, which expands T. */
4548 tree vec = make_tree_vec (1);
4549 if (CHECKING_P)
4550 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4551
4552 t = convert_from_reference (t);
4553 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4554
4555 t = make_node (NONTYPE_ARGUMENT_PACK);
4556 SET_ARGUMENT_PACK_ARGS (t, vec);
4557 }
4558 else
4559 t = convert_from_reference (t);
4560 }
4561 return t;
4562 }
4563
4564 /* Given a single level of template parameters (a TREE_VEC), return it
4565 as a set of template arguments. */
4566
4567 static tree
4568 template_parms_level_to_args (tree parms)
4569 {
4570 tree a = copy_node (parms);
4571 TREE_TYPE (a) = NULL_TREE;
4572 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4573 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4574
4575 if (CHECKING_P)
4576 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4577
4578 return a;
4579 }
4580
4581 /* Given a set of template parameters, return them as a set of template
4582 arguments. The template parameters are represented as a TREE_VEC, in
4583 the form documented in cp-tree.h for template arguments. */
4584
4585 static tree
4586 template_parms_to_args (tree parms)
4587 {
4588 tree header;
4589 tree args = NULL_TREE;
4590 int length = TMPL_PARMS_DEPTH (parms);
4591 int l = length;
4592
4593 /* If there is only one level of template parameters, we do not
4594 create a TREE_VEC of TREE_VECs. Instead, we return a single
4595 TREE_VEC containing the arguments. */
4596 if (length > 1)
4597 args = make_tree_vec (length);
4598
4599 for (header = parms; header; header = TREE_CHAIN (header))
4600 {
4601 tree a = template_parms_level_to_args (TREE_VALUE (header));
4602
4603 if (length > 1)
4604 TREE_VEC_ELT (args, --l) = a;
4605 else
4606 args = a;
4607 }
4608
4609 return args;
4610 }
4611
4612 /* Within the declaration of a template, return the currently active
4613 template parameters as an argument TREE_VEC. */
4614
4615 static tree
4616 current_template_args (void)
4617 {
4618 return template_parms_to_args (current_template_parms);
4619 }
4620
4621 /* Update the declared TYPE by doing any lookups which were thought to be
4622 dependent, but are not now that we know the SCOPE of the declarator. */
4623
4624 tree
4625 maybe_update_decl_type (tree orig_type, tree scope)
4626 {
4627 tree type = orig_type;
4628
4629 if (type == NULL_TREE)
4630 return type;
4631
4632 if (TREE_CODE (orig_type) == TYPE_DECL)
4633 type = TREE_TYPE (type);
4634
4635 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4636 && dependent_type_p (type)
4637 /* Don't bother building up the args in this case. */
4638 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4639 {
4640 /* tsubst in the args corresponding to the template parameters,
4641 including auto if present. Most things will be unchanged, but
4642 make_typename_type and tsubst_qualified_id will resolve
4643 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4644 tree args = current_template_args ();
4645 tree auto_node = type_uses_auto (type);
4646 tree pushed;
4647 if (auto_node)
4648 {
4649 tree auto_vec = make_tree_vec (1);
4650 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4651 args = add_to_template_args (args, auto_vec);
4652 }
4653 pushed = push_scope (scope);
4654 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4655 if (pushed)
4656 pop_scope (scope);
4657 }
4658
4659 if (type == error_mark_node)
4660 return orig_type;
4661
4662 if (TREE_CODE (orig_type) == TYPE_DECL)
4663 {
4664 if (same_type_p (type, TREE_TYPE (orig_type)))
4665 type = orig_type;
4666 else
4667 type = TYPE_NAME (type);
4668 }
4669 return type;
4670 }
4671
4672 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4673 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4674 the new template is a member template. */
4675
4676 tree
4677 build_template_decl (tree decl, tree parms, bool member_template_p)
4678 {
4679 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4680 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4681 DECL_TEMPLATE_PARMS (tmpl) = parms;
4682 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4683 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4684 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4685
4686 return tmpl;
4687 }
4688
4689 struct template_parm_data
4690 {
4691 /* The level of the template parameters we are currently
4692 processing. */
4693 int level;
4694
4695 /* The index of the specialization argument we are currently
4696 processing. */
4697 int current_arg;
4698
4699 /* An array whose size is the number of template parameters. The
4700 elements are nonzero if the parameter has been used in any one
4701 of the arguments processed so far. */
4702 int* parms;
4703
4704 /* An array whose size is the number of template arguments. The
4705 elements are nonzero if the argument makes use of template
4706 parameters of this level. */
4707 int* arg_uses_template_parms;
4708 };
4709
4710 /* Subroutine of push_template_decl used to see if each template
4711 parameter in a partial specialization is used in the explicit
4712 argument list. If T is of the LEVEL given in DATA (which is
4713 treated as a template_parm_data*), then DATA->PARMS is marked
4714 appropriately. */
4715
4716 static int
4717 mark_template_parm (tree t, void* data)
4718 {
4719 int level;
4720 int idx;
4721 struct template_parm_data* tpd = (struct template_parm_data*) data;
4722
4723 template_parm_level_and_index (t, &level, &idx);
4724
4725 if (level == tpd->level)
4726 {
4727 tpd->parms[idx] = 1;
4728 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4729 }
4730
4731 /* In C++17 the type of a non-type argument is a deduced context. */
4732 if (cxx_dialect >= cxx17
4733 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4734 for_each_template_parm (TREE_TYPE (t),
4735 &mark_template_parm,
4736 data,
4737 NULL,
4738 /*include_nondeduced_p=*/false);
4739
4740 /* Return zero so that for_each_template_parm will continue the
4741 traversal of the tree; we want to mark *every* template parm. */
4742 return 0;
4743 }
4744
4745 /* Process the partial specialization DECL. */
4746
4747 static tree
4748 process_partial_specialization (tree decl)
4749 {
4750 tree type = TREE_TYPE (decl);
4751 tree tinfo = get_template_info (decl);
4752 tree maintmpl = TI_TEMPLATE (tinfo);
4753 tree specargs = TI_ARGS (tinfo);
4754 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4755 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4756 tree inner_parms;
4757 tree inst;
4758 int nargs = TREE_VEC_LENGTH (inner_args);
4759 int ntparms;
4760 int i;
4761 bool did_error_intro = false;
4762 struct template_parm_data tpd;
4763 struct template_parm_data tpd2;
4764
4765 gcc_assert (current_template_parms);
4766
4767 /* A concept cannot be specialized. */
4768 if (flag_concepts && variable_concept_p (maintmpl))
4769 {
4770 error ("specialization of variable concept %q#D", maintmpl);
4771 return error_mark_node;
4772 }
4773
4774 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4775 ntparms = TREE_VEC_LENGTH (inner_parms);
4776
4777 /* We check that each of the template parameters given in the
4778 partial specialization is used in the argument list to the
4779 specialization. For example:
4780
4781 template <class T> struct S;
4782 template <class T> struct S<T*>;
4783
4784 The second declaration is OK because `T*' uses the template
4785 parameter T, whereas
4786
4787 template <class T> struct S<int>;
4788
4789 is no good. Even trickier is:
4790
4791 template <class T>
4792 struct S1
4793 {
4794 template <class U>
4795 struct S2;
4796 template <class U>
4797 struct S2<T>;
4798 };
4799
4800 The S2<T> declaration is actually invalid; it is a
4801 full-specialization. Of course,
4802
4803 template <class U>
4804 struct S2<T (*)(U)>;
4805
4806 or some such would have been OK. */
4807 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4808 tpd.parms = XALLOCAVEC (int, ntparms);
4809 memset (tpd.parms, 0, sizeof (int) * ntparms);
4810
4811 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4812 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4813 for (i = 0; i < nargs; ++i)
4814 {
4815 tpd.current_arg = i;
4816 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4817 &mark_template_parm,
4818 &tpd,
4819 NULL,
4820 /*include_nondeduced_p=*/false);
4821 }
4822 for (i = 0; i < ntparms; ++i)
4823 if (tpd.parms[i] == 0)
4824 {
4825 /* One of the template parms was not used in a deduced context in the
4826 specialization. */
4827 if (!did_error_intro)
4828 {
4829 error ("template parameters not deducible in "
4830 "partial specialization:");
4831 did_error_intro = true;
4832 }
4833
4834 inform (input_location, " %qD",
4835 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4836 }
4837
4838 if (did_error_intro)
4839 return error_mark_node;
4840
4841 /* [temp.class.spec]
4842
4843 The argument list of the specialization shall not be identical to
4844 the implicit argument list of the primary template. */
4845 tree main_args
4846 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4847 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4848 && (!flag_concepts
4849 || !strictly_subsumes (current_template_constraints (),
4850 get_constraints (maintmpl))))
4851 {
4852 if (!flag_concepts)
4853 error ("partial specialization %q+D does not specialize "
4854 "any template arguments; to define the primary template, "
4855 "remove the template argument list", decl);
4856 else
4857 error ("partial specialization %q+D does not specialize any "
4858 "template arguments and is not more constrained than "
4859 "the primary template; to define the primary template, "
4860 "remove the template argument list", decl);
4861 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4862 }
4863
4864 /* A partial specialization that replaces multiple parameters of the
4865 primary template with a pack expansion is less specialized for those
4866 parameters. */
4867 if (nargs < DECL_NTPARMS (maintmpl))
4868 {
4869 error ("partial specialization is not more specialized than the "
4870 "primary template because it replaces multiple parameters "
4871 "with a pack expansion");
4872 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4873 /* Avoid crash in process_partial_specialization. */
4874 return decl;
4875 }
4876
4877 /* If we aren't in a dependent class, we can actually try deduction. */
4878 else if (tpd.level == 1
4879 /* FIXME we should be able to handle a partial specialization of a
4880 partial instantiation, but currently we can't (c++/41727). */
4881 && TMPL_ARGS_DEPTH (specargs) == 1
4882 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4883 {
4884 if (permerror (input_location, "partial specialization %qD is not "
4885 "more specialized than", decl))
4886 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4887 maintmpl);
4888 }
4889
4890 /* [temp.class.spec]
4891
4892 A partially specialized non-type argument expression shall not
4893 involve template parameters of the partial specialization except
4894 when the argument expression is a simple identifier.
4895
4896 The type of a template parameter corresponding to a specialized
4897 non-type argument shall not be dependent on a parameter of the
4898 specialization.
4899
4900 Also, we verify that pack expansions only occur at the
4901 end of the argument list. */
4902 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4903 tpd2.parms = 0;
4904 for (i = 0; i < nargs; ++i)
4905 {
4906 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4907 tree arg = TREE_VEC_ELT (inner_args, i);
4908 tree packed_args = NULL_TREE;
4909 int j, len = 1;
4910
4911 if (ARGUMENT_PACK_P (arg))
4912 {
4913 /* Extract the arguments from the argument pack. We'll be
4914 iterating over these in the following loop. */
4915 packed_args = ARGUMENT_PACK_ARGS (arg);
4916 len = TREE_VEC_LENGTH (packed_args);
4917 }
4918
4919 for (j = 0; j < len; j++)
4920 {
4921 if (packed_args)
4922 /* Get the Jth argument in the parameter pack. */
4923 arg = TREE_VEC_ELT (packed_args, j);
4924
4925 if (PACK_EXPANSION_P (arg))
4926 {
4927 /* Pack expansions must come at the end of the
4928 argument list. */
4929 if ((packed_args && j < len - 1)
4930 || (!packed_args && i < nargs - 1))
4931 {
4932 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4933 error ("parameter pack argument %qE must be at the "
4934 "end of the template argument list", arg);
4935 else
4936 error ("parameter pack argument %qT must be at the "
4937 "end of the template argument list", arg);
4938 }
4939 }
4940
4941 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4942 /* We only care about the pattern. */
4943 arg = PACK_EXPANSION_PATTERN (arg);
4944
4945 if (/* These first two lines are the `non-type' bit. */
4946 !TYPE_P (arg)
4947 && TREE_CODE (arg) != TEMPLATE_DECL
4948 /* This next two lines are the `argument expression is not just a
4949 simple identifier' condition and also the `specialized
4950 non-type argument' bit. */
4951 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4952 && !(REFERENCE_REF_P (arg)
4953 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4954 {
4955 if ((!packed_args && tpd.arg_uses_template_parms[i])
4956 || (packed_args && uses_template_parms (arg)))
4957 error ("template argument %qE involves template parameter(s)",
4958 arg);
4959 else
4960 {
4961 /* Look at the corresponding template parameter,
4962 marking which template parameters its type depends
4963 upon. */
4964 tree type = TREE_TYPE (parm);
4965
4966 if (!tpd2.parms)
4967 {
4968 /* We haven't yet initialized TPD2. Do so now. */
4969 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4970 /* The number of parameters here is the number in the
4971 main template, which, as checked in the assertion
4972 above, is NARGS. */
4973 tpd2.parms = XALLOCAVEC (int, nargs);
4974 tpd2.level =
4975 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4976 }
4977
4978 /* Mark the template parameters. But this time, we're
4979 looking for the template parameters of the main
4980 template, not in the specialization. */
4981 tpd2.current_arg = i;
4982 tpd2.arg_uses_template_parms[i] = 0;
4983 memset (tpd2.parms, 0, sizeof (int) * nargs);
4984 for_each_template_parm (type,
4985 &mark_template_parm,
4986 &tpd2,
4987 NULL,
4988 /*include_nondeduced_p=*/false);
4989
4990 if (tpd2.arg_uses_template_parms [i])
4991 {
4992 /* The type depended on some template parameters.
4993 If they are fully specialized in the
4994 specialization, that's OK. */
4995 int j;
4996 int count = 0;
4997 for (j = 0; j < nargs; ++j)
4998 if (tpd2.parms[j] != 0
4999 && tpd.arg_uses_template_parms [j])
5000 ++count;
5001 if (count != 0)
5002 error_n (input_location, count,
5003 "type %qT of template argument %qE depends "
5004 "on a template parameter",
5005 "type %qT of template argument %qE depends "
5006 "on template parameters",
5007 type,
5008 arg);
5009 }
5010 }
5011 }
5012 }
5013 }
5014
5015 /* We should only get here once. */
5016 if (TREE_CODE (decl) == TYPE_DECL)
5017 gcc_assert (!COMPLETE_TYPE_P (type));
5018
5019 // Build the template decl.
5020 tree tmpl = build_template_decl (decl, current_template_parms,
5021 DECL_MEMBER_TEMPLATE_P (maintmpl));
5022 TREE_TYPE (tmpl) = type;
5023 DECL_TEMPLATE_RESULT (tmpl) = decl;
5024 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5025 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5026 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5027
5028 /* Give template template parms a DECL_CONTEXT of the template
5029 for which they are a parameter. */
5030 for (i = 0; i < ntparms; ++i)
5031 {
5032 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5033 if (TREE_CODE (parm) == TEMPLATE_DECL)
5034 DECL_CONTEXT (parm) = tmpl;
5035 }
5036
5037 if (VAR_P (decl))
5038 /* We didn't register this in check_explicit_specialization so we could
5039 wait until the constraints were set. */
5040 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5041 else
5042 associate_classtype_constraints (type);
5043
5044 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5045 = tree_cons (specargs, tmpl,
5046 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5047 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5048
5049 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5050 inst = TREE_CHAIN (inst))
5051 {
5052 tree instance = TREE_VALUE (inst);
5053 if (TYPE_P (instance)
5054 ? (COMPLETE_TYPE_P (instance)
5055 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5056 : DECL_TEMPLATE_INSTANTIATION (instance))
5057 {
5058 tree spec = most_specialized_partial_spec (instance, tf_none);
5059 tree inst_decl = (DECL_P (instance)
5060 ? instance : TYPE_NAME (instance));
5061 if (!spec)
5062 /* OK */;
5063 else if (spec == error_mark_node)
5064 permerror (input_location,
5065 "declaration of %qD ambiguates earlier template "
5066 "instantiation for %qD", decl, inst_decl);
5067 else if (TREE_VALUE (spec) == tmpl)
5068 permerror (input_location,
5069 "partial specialization of %qD after instantiation "
5070 "of %qD", decl, inst_decl);
5071 }
5072 }
5073
5074 return decl;
5075 }
5076
5077 /* PARM is a template parameter of some form; return the corresponding
5078 TEMPLATE_PARM_INDEX. */
5079
5080 static tree
5081 get_template_parm_index (tree parm)
5082 {
5083 if (TREE_CODE (parm) == PARM_DECL
5084 || TREE_CODE (parm) == CONST_DECL)
5085 parm = DECL_INITIAL (parm);
5086 else if (TREE_CODE (parm) == TYPE_DECL
5087 || TREE_CODE (parm) == TEMPLATE_DECL)
5088 parm = TREE_TYPE (parm);
5089 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5090 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5091 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5092 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5093 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5094 return parm;
5095 }
5096
5097 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5098 parameter packs used by the template parameter PARM. */
5099
5100 static void
5101 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5102 {
5103 /* A type parm can't refer to another parm. */
5104 if (TREE_CODE (parm) == TYPE_DECL)
5105 return;
5106 else if (TREE_CODE (parm) == PARM_DECL)
5107 {
5108 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5109 ppd, ppd->visited);
5110 return;
5111 }
5112
5113 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5114
5115 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5116 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5117 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5118 }
5119
5120 /* PARM is a template parameter pack. Return any parameter packs used in
5121 its type or the type of any of its template parameters. If there are
5122 any such packs, it will be instantiated into a fixed template parameter
5123 list by partial instantiation rather than be fully deduced. */
5124
5125 tree
5126 fixed_parameter_pack_p (tree parm)
5127 {
5128 /* This can only be true in a member template. */
5129 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5130 return NULL_TREE;
5131 /* This can only be true for a parameter pack. */
5132 if (!template_parameter_pack_p (parm))
5133 return NULL_TREE;
5134 /* A type parm can't refer to another parm. */
5135 if (TREE_CODE (parm) == TYPE_DECL)
5136 return NULL_TREE;
5137
5138 tree parameter_packs = NULL_TREE;
5139 struct find_parameter_pack_data ppd;
5140 ppd.parameter_packs = &parameter_packs;
5141 ppd.visited = new hash_set<tree>;
5142 ppd.type_pack_expansion_p = false;
5143
5144 fixed_parameter_pack_p_1 (parm, &ppd);
5145
5146 delete ppd.visited;
5147 return parameter_packs;
5148 }
5149
5150 /* Check that a template declaration's use of default arguments and
5151 parameter packs is not invalid. Here, PARMS are the template
5152 parameters. IS_PRIMARY is true if DECL is the thing declared by
5153 a primary template. IS_PARTIAL is true if DECL is a partial
5154 specialization.
5155
5156 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5157 function template declaration or a friend class template
5158 declaration. In the function case, 1 indicates a declaration, 2
5159 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5160 emitted for extraneous default arguments.
5161
5162 Returns TRUE if there were no errors found, FALSE otherwise. */
5163
5164 bool
5165 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5166 bool is_partial, int is_friend_decl)
5167 {
5168 const char *msg;
5169 int last_level_to_check;
5170 tree parm_level;
5171 bool no_errors = true;
5172
5173 /* [temp.param]
5174
5175 A default template-argument shall not be specified in a
5176 function template declaration or a function template definition, nor
5177 in the template-parameter-list of the definition of a member of a
5178 class template. */
5179
5180 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5181 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5182 /* You can't have a function template declaration in a local
5183 scope, nor you can you define a member of a class template in a
5184 local scope. */
5185 return true;
5186
5187 if ((TREE_CODE (decl) == TYPE_DECL
5188 && TREE_TYPE (decl)
5189 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5190 || (TREE_CODE (decl) == FUNCTION_DECL
5191 && LAMBDA_FUNCTION_P (decl)))
5192 /* A lambda doesn't have an explicit declaration; don't complain
5193 about the parms of the enclosing class. */
5194 return true;
5195
5196 if (current_class_type
5197 && !TYPE_BEING_DEFINED (current_class_type)
5198 && DECL_LANG_SPECIFIC (decl)
5199 && DECL_DECLARES_FUNCTION_P (decl)
5200 /* If this is either a friend defined in the scope of the class
5201 or a member function. */
5202 && (DECL_FUNCTION_MEMBER_P (decl)
5203 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5204 : DECL_FRIEND_CONTEXT (decl)
5205 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5206 : false)
5207 /* And, if it was a member function, it really was defined in
5208 the scope of the class. */
5209 && (!DECL_FUNCTION_MEMBER_P (decl)
5210 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5211 /* We already checked these parameters when the template was
5212 declared, so there's no need to do it again now. This function
5213 was defined in class scope, but we're processing its body now
5214 that the class is complete. */
5215 return true;
5216
5217 /* Core issue 226 (C++0x only): the following only applies to class
5218 templates. */
5219 if (is_primary
5220 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5221 {
5222 /* [temp.param]
5223
5224 If a template-parameter has a default template-argument, all
5225 subsequent template-parameters shall have a default
5226 template-argument supplied. */
5227 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5228 {
5229 tree inner_parms = TREE_VALUE (parm_level);
5230 int ntparms = TREE_VEC_LENGTH (inner_parms);
5231 int seen_def_arg_p = 0;
5232 int i;
5233
5234 for (i = 0; i < ntparms; ++i)
5235 {
5236 tree parm = TREE_VEC_ELT (inner_parms, i);
5237
5238 if (parm == error_mark_node)
5239 continue;
5240
5241 if (TREE_PURPOSE (parm))
5242 seen_def_arg_p = 1;
5243 else if (seen_def_arg_p
5244 && !template_parameter_pack_p (TREE_VALUE (parm)))
5245 {
5246 error ("no default argument for %qD", TREE_VALUE (parm));
5247 /* For better subsequent error-recovery, we indicate that
5248 there should have been a default argument. */
5249 TREE_PURPOSE (parm) = error_mark_node;
5250 no_errors = false;
5251 }
5252 else if (!is_partial
5253 && !is_friend_decl
5254 /* Don't complain about an enclosing partial
5255 specialization. */
5256 && parm_level == parms
5257 && TREE_CODE (decl) == TYPE_DECL
5258 && i < ntparms - 1
5259 && template_parameter_pack_p (TREE_VALUE (parm))
5260 /* A fixed parameter pack will be partially
5261 instantiated into a fixed length list. */
5262 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5263 {
5264 /* A primary class template can only have one
5265 parameter pack, at the end of the template
5266 parameter list. */
5267
5268 error ("parameter pack %q+D must be at the end of the"
5269 " template parameter list", TREE_VALUE (parm));
5270
5271 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5272 = error_mark_node;
5273 no_errors = false;
5274 }
5275 }
5276 }
5277 }
5278
5279 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5280 || is_partial
5281 || !is_primary
5282 || is_friend_decl)
5283 /* For an ordinary class template, default template arguments are
5284 allowed at the innermost level, e.g.:
5285 template <class T = int>
5286 struct S {};
5287 but, in a partial specialization, they're not allowed even
5288 there, as we have in [temp.class.spec]:
5289
5290 The template parameter list of a specialization shall not
5291 contain default template argument values.
5292
5293 So, for a partial specialization, or for a function template
5294 (in C++98/C++03), we look at all of them. */
5295 ;
5296 else
5297 /* But, for a primary class template that is not a partial
5298 specialization we look at all template parameters except the
5299 innermost ones. */
5300 parms = TREE_CHAIN (parms);
5301
5302 /* Figure out what error message to issue. */
5303 if (is_friend_decl == 2)
5304 msg = G_("default template arguments may not be used in function template "
5305 "friend re-declaration");
5306 else if (is_friend_decl)
5307 msg = G_("default template arguments may not be used in template "
5308 "friend declarations");
5309 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5310 msg = G_("default template arguments may not be used in function templates "
5311 "without -std=c++11 or -std=gnu++11");
5312 else if (is_partial)
5313 msg = G_("default template arguments may not be used in "
5314 "partial specializations");
5315 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5316 msg = G_("default argument for template parameter for class enclosing %qD");
5317 else
5318 /* Per [temp.param]/9, "A default template-argument shall not be
5319 specified in the template-parameter-lists of the definition of
5320 a member of a class template that appears outside of the member's
5321 class.", thus if we aren't handling a member of a class template
5322 there is no need to examine the parameters. */
5323 return true;
5324
5325 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5326 /* If we're inside a class definition, there's no need to
5327 examine the parameters to the class itself. On the one
5328 hand, they will be checked when the class is defined, and,
5329 on the other, default arguments are valid in things like:
5330 template <class T = double>
5331 struct S { template <class U> void f(U); };
5332 Here the default argument for `S' has no bearing on the
5333 declaration of `f'. */
5334 last_level_to_check = template_class_depth (current_class_type) + 1;
5335 else
5336 /* Check everything. */
5337 last_level_to_check = 0;
5338
5339 for (parm_level = parms;
5340 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5341 parm_level = TREE_CHAIN (parm_level))
5342 {
5343 tree inner_parms = TREE_VALUE (parm_level);
5344 int i;
5345 int ntparms;
5346
5347 ntparms = TREE_VEC_LENGTH (inner_parms);
5348 for (i = 0; i < ntparms; ++i)
5349 {
5350 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5351 continue;
5352
5353 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5354 {
5355 if (msg)
5356 {
5357 no_errors = false;
5358 if (is_friend_decl == 2)
5359 return no_errors;
5360
5361 error (msg, decl);
5362 msg = 0;
5363 }
5364
5365 /* Clear out the default argument so that we are not
5366 confused later. */
5367 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5368 }
5369 }
5370
5371 /* At this point, if we're still interested in issuing messages,
5372 they must apply to classes surrounding the object declared. */
5373 if (msg)
5374 msg = G_("default argument for template parameter for class "
5375 "enclosing %qD");
5376 }
5377
5378 return no_errors;
5379 }
5380
5381 /* Worker for push_template_decl_real, called via
5382 for_each_template_parm. DATA is really an int, indicating the
5383 level of the parameters we are interested in. If T is a template
5384 parameter of that level, return nonzero. */
5385
5386 static int
5387 template_parm_this_level_p (tree t, void* data)
5388 {
5389 int this_level = *(int *)data;
5390 int level;
5391
5392 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5393 level = TEMPLATE_PARM_LEVEL (t);
5394 else
5395 level = TEMPLATE_TYPE_LEVEL (t);
5396 return level == this_level;
5397 }
5398
5399 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5400 DATA is really an int, indicating the innermost outer level of parameters.
5401 If T is a template parameter of that level or further out, return
5402 nonzero. */
5403
5404 static int
5405 template_parm_outer_level (tree t, void *data)
5406 {
5407 int this_level = *(int *)data;
5408 int level;
5409
5410 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5411 level = TEMPLATE_PARM_LEVEL (t);
5412 else
5413 level = TEMPLATE_TYPE_LEVEL (t);
5414 return level <= this_level;
5415 }
5416
5417 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5418 parameters given by current_template_args, or reuses a
5419 previously existing one, if appropriate. Returns the DECL, or an
5420 equivalent one, if it is replaced via a call to duplicate_decls.
5421
5422 If IS_FRIEND is true, DECL is a friend declaration. */
5423
5424 tree
5425 push_template_decl_real (tree decl, bool is_friend)
5426 {
5427 tree tmpl;
5428 tree args;
5429 tree info;
5430 tree ctx;
5431 bool is_primary;
5432 bool is_partial;
5433 int new_template_p = 0;
5434 /* True if the template is a member template, in the sense of
5435 [temp.mem]. */
5436 bool member_template_p = false;
5437
5438 if (decl == error_mark_node || !current_template_parms)
5439 return error_mark_node;
5440
5441 /* See if this is a partial specialization. */
5442 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5443 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5444 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5445 || (VAR_P (decl)
5446 && DECL_LANG_SPECIFIC (decl)
5447 && DECL_TEMPLATE_SPECIALIZATION (decl)
5448 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5449
5450 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5451 is_friend = true;
5452
5453 if (is_friend)
5454 /* For a friend, we want the context of the friend, not
5455 the type of which it is a friend. */
5456 ctx = CP_DECL_CONTEXT (decl);
5457 else if (CP_DECL_CONTEXT (decl)
5458 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5459 /* In the case of a virtual function, we want the class in which
5460 it is defined. */
5461 ctx = CP_DECL_CONTEXT (decl);
5462 else
5463 /* Otherwise, if we're currently defining some class, the DECL
5464 is assumed to be a member of the class. */
5465 ctx = current_scope ();
5466
5467 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5468 ctx = NULL_TREE;
5469
5470 if (!DECL_CONTEXT (decl))
5471 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5472
5473 /* See if this is a primary template. */
5474 if (is_friend && ctx
5475 && uses_template_parms_level (ctx, processing_template_decl))
5476 /* A friend template that specifies a class context, i.e.
5477 template <typename T> friend void A<T>::f();
5478 is not primary. */
5479 is_primary = false;
5480 else if (TREE_CODE (decl) == TYPE_DECL
5481 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5482 is_primary = false;
5483 else
5484 is_primary = template_parm_scope_p ();
5485
5486 if (is_primary)
5487 {
5488 warning (OPT_Wtemplates, "template %qD declared", decl);
5489
5490 if (DECL_CLASS_SCOPE_P (decl))
5491 member_template_p = true;
5492 if (TREE_CODE (decl) == TYPE_DECL
5493 && anon_aggrname_p (DECL_NAME (decl)))
5494 {
5495 error ("template class without a name");
5496 return error_mark_node;
5497 }
5498 else if (TREE_CODE (decl) == FUNCTION_DECL)
5499 {
5500 if (member_template_p)
5501 {
5502 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5503 error ("member template %qD may not have virt-specifiers", decl);
5504 }
5505 if (DECL_DESTRUCTOR_P (decl))
5506 {
5507 /* [temp.mem]
5508
5509 A destructor shall not be a member template. */
5510 error ("destructor %qD declared as member template", decl);
5511 return error_mark_node;
5512 }
5513 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5514 && (!prototype_p (TREE_TYPE (decl))
5515 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5516 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5517 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5518 == void_list_node)))
5519 {
5520 /* [basic.stc.dynamic.allocation]
5521
5522 An allocation function can be a function
5523 template. ... Template allocation functions shall
5524 have two or more parameters. */
5525 error ("invalid template declaration of %qD", decl);
5526 return error_mark_node;
5527 }
5528 }
5529 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5530 && CLASS_TYPE_P (TREE_TYPE (decl)))
5531 {
5532 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5533 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5534 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5535 {
5536 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5537 if (TREE_CODE (t) == TYPE_DECL)
5538 t = TREE_TYPE (t);
5539 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5540 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5541 }
5542 }
5543 else if (TREE_CODE (decl) == TYPE_DECL
5544 && TYPE_DECL_ALIAS_P (decl))
5545 /* alias-declaration */
5546 gcc_assert (!DECL_ARTIFICIAL (decl));
5547 else if (VAR_P (decl))
5548 /* C++14 variable template. */;
5549 else
5550 {
5551 error ("template declaration of %q#D", decl);
5552 return error_mark_node;
5553 }
5554 }
5555
5556 /* Check to see that the rules regarding the use of default
5557 arguments are not being violated. We check args for a friend
5558 functions when we know whether it's a definition, introducing
5559 declaration or re-declaration. */
5560 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5561 check_default_tmpl_args (decl, current_template_parms,
5562 is_primary, is_partial, is_friend);
5563
5564 /* Ensure that there are no parameter packs in the type of this
5565 declaration that have not been expanded. */
5566 if (TREE_CODE (decl) == FUNCTION_DECL)
5567 {
5568 /* Check each of the arguments individually to see if there are
5569 any bare parameter packs. */
5570 tree type = TREE_TYPE (decl);
5571 tree arg = DECL_ARGUMENTS (decl);
5572 tree argtype = TYPE_ARG_TYPES (type);
5573
5574 while (arg && argtype)
5575 {
5576 if (!DECL_PACK_P (arg)
5577 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5578 {
5579 /* This is a PARM_DECL that contains unexpanded parameter
5580 packs. We have already complained about this in the
5581 check_for_bare_parameter_packs call, so just replace
5582 these types with ERROR_MARK_NODE. */
5583 TREE_TYPE (arg) = error_mark_node;
5584 TREE_VALUE (argtype) = error_mark_node;
5585 }
5586
5587 arg = DECL_CHAIN (arg);
5588 argtype = TREE_CHAIN (argtype);
5589 }
5590
5591 /* Check for bare parameter packs in the return type and the
5592 exception specifiers. */
5593 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5594 /* Errors were already issued, set return type to int
5595 as the frontend doesn't expect error_mark_node as
5596 the return type. */
5597 TREE_TYPE (type) = integer_type_node;
5598 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5599 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5600 }
5601 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5602 && TYPE_DECL_ALIAS_P (decl))
5603 ? DECL_ORIGINAL_TYPE (decl)
5604 : TREE_TYPE (decl)))
5605 {
5606 TREE_TYPE (decl) = error_mark_node;
5607 return error_mark_node;
5608 }
5609
5610 if (is_partial)
5611 return process_partial_specialization (decl);
5612
5613 args = current_template_args ();
5614
5615 if (!ctx
5616 || TREE_CODE (ctx) == FUNCTION_DECL
5617 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5618 || (TREE_CODE (decl) == TYPE_DECL
5619 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5620 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5621 {
5622 if (DECL_LANG_SPECIFIC (decl)
5623 && DECL_TEMPLATE_INFO (decl)
5624 && DECL_TI_TEMPLATE (decl))
5625 tmpl = DECL_TI_TEMPLATE (decl);
5626 /* If DECL is a TYPE_DECL for a class-template, then there won't
5627 be DECL_LANG_SPECIFIC. The information equivalent to
5628 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5629 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5630 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5631 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5632 {
5633 /* Since a template declaration already existed for this
5634 class-type, we must be redeclaring it here. Make sure
5635 that the redeclaration is valid. */
5636 redeclare_class_template (TREE_TYPE (decl),
5637 current_template_parms,
5638 current_template_constraints ());
5639 /* We don't need to create a new TEMPLATE_DECL; just use the
5640 one we already had. */
5641 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5642 }
5643 else
5644 {
5645 tmpl = build_template_decl (decl, current_template_parms,
5646 member_template_p);
5647 new_template_p = 1;
5648
5649 if (DECL_LANG_SPECIFIC (decl)
5650 && DECL_TEMPLATE_SPECIALIZATION (decl))
5651 {
5652 /* A specialization of a member template of a template
5653 class. */
5654 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5655 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5656 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5657 }
5658 }
5659 }
5660 else
5661 {
5662 tree a, t, current, parms;
5663 int i;
5664 tree tinfo = get_template_info (decl);
5665
5666 if (!tinfo)
5667 {
5668 error ("template definition of non-template %q#D", decl);
5669 return error_mark_node;
5670 }
5671
5672 tmpl = TI_TEMPLATE (tinfo);
5673
5674 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5675 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5676 && DECL_TEMPLATE_SPECIALIZATION (decl)
5677 && DECL_MEMBER_TEMPLATE_P (tmpl))
5678 {
5679 tree new_tmpl;
5680
5681 /* The declaration is a specialization of a member
5682 template, declared outside the class. Therefore, the
5683 innermost template arguments will be NULL, so we
5684 replace them with the arguments determined by the
5685 earlier call to check_explicit_specialization. */
5686 args = DECL_TI_ARGS (decl);
5687
5688 new_tmpl
5689 = build_template_decl (decl, current_template_parms,
5690 member_template_p);
5691 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5692 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5693 DECL_TI_TEMPLATE (decl) = new_tmpl;
5694 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5695 DECL_TEMPLATE_INFO (new_tmpl)
5696 = build_template_info (tmpl, args);
5697
5698 register_specialization (new_tmpl,
5699 most_general_template (tmpl),
5700 args,
5701 is_friend, 0);
5702 return decl;
5703 }
5704
5705 /* Make sure the template headers we got make sense. */
5706
5707 parms = DECL_TEMPLATE_PARMS (tmpl);
5708 i = TMPL_PARMS_DEPTH (parms);
5709 if (TMPL_ARGS_DEPTH (args) != i)
5710 {
5711 error ("expected %d levels of template parms for %q#D, got %d",
5712 i, decl, TMPL_ARGS_DEPTH (args));
5713 DECL_INTERFACE_KNOWN (decl) = 1;
5714 return error_mark_node;
5715 }
5716 else
5717 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5718 {
5719 a = TMPL_ARGS_LEVEL (args, i);
5720 t = INNERMOST_TEMPLATE_PARMS (parms);
5721
5722 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5723 {
5724 if (current == decl)
5725 error ("got %d template parameters for %q#D",
5726 TREE_VEC_LENGTH (a), decl);
5727 else
5728 error ("got %d template parameters for %q#T",
5729 TREE_VEC_LENGTH (a), current);
5730 error (" but %d required", TREE_VEC_LENGTH (t));
5731 /* Avoid crash in import_export_decl. */
5732 DECL_INTERFACE_KNOWN (decl) = 1;
5733 return error_mark_node;
5734 }
5735
5736 if (current == decl)
5737 current = ctx;
5738 else if (current == NULL_TREE)
5739 /* Can happen in erroneous input. */
5740 break;
5741 else
5742 current = get_containing_scope (current);
5743 }
5744
5745 /* Check that the parms are used in the appropriate qualifying scopes
5746 in the declarator. */
5747 if (!comp_template_args
5748 (TI_ARGS (tinfo),
5749 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5750 {
5751 error ("template arguments to %qD do not match original "
5752 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5753 if (!uses_template_parms (TI_ARGS (tinfo)))
5754 inform (input_location, "use %<template<>%> for"
5755 " an explicit specialization");
5756 /* Avoid crash in import_export_decl. */
5757 DECL_INTERFACE_KNOWN (decl) = 1;
5758 return error_mark_node;
5759 }
5760 }
5761
5762 DECL_TEMPLATE_RESULT (tmpl) = decl;
5763 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5764
5765 /* Push template declarations for global functions and types. Note
5766 that we do not try to push a global template friend declared in a
5767 template class; such a thing may well depend on the template
5768 parameters of the class. */
5769 if (new_template_p && !ctx
5770 && !(is_friend && template_class_depth (current_class_type) > 0))
5771 {
5772 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5773 if (tmpl == error_mark_node)
5774 return error_mark_node;
5775
5776 /* Hide template friend classes that haven't been declared yet. */
5777 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5778 {
5779 DECL_ANTICIPATED (tmpl) = 1;
5780 DECL_FRIEND_P (tmpl) = 1;
5781 }
5782 }
5783
5784 if (is_primary)
5785 {
5786 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5787
5788 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5789
5790 /* Give template template parms a DECL_CONTEXT of the template
5791 for which they are a parameter. */
5792 parms = INNERMOST_TEMPLATE_PARMS (parms);
5793 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5794 {
5795 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5796 if (TREE_CODE (parm) == TEMPLATE_DECL)
5797 DECL_CONTEXT (parm) = tmpl;
5798 }
5799
5800 if (TREE_CODE (decl) == TYPE_DECL
5801 && TYPE_DECL_ALIAS_P (decl)
5802 && complex_alias_template_p (tmpl))
5803 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5804 }
5805
5806 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5807 back to its most general template. If TMPL is a specialization,
5808 ARGS may only have the innermost set of arguments. Add the missing
5809 argument levels if necessary. */
5810 if (DECL_TEMPLATE_INFO (tmpl))
5811 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5812
5813 info = build_template_info (tmpl, args);
5814
5815 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5816 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5817 else
5818 {
5819 if (is_primary)
5820 retrofit_lang_decl (decl);
5821 if (DECL_LANG_SPECIFIC (decl))
5822 DECL_TEMPLATE_INFO (decl) = info;
5823 }
5824
5825 if (flag_implicit_templates
5826 && !is_friend
5827 && TREE_PUBLIC (decl)
5828 && VAR_OR_FUNCTION_DECL_P (decl))
5829 /* Set DECL_COMDAT on template instantiations; if we force
5830 them to be emitted by explicit instantiation or -frepo,
5831 mark_needed will tell cgraph to do the right thing. */
5832 DECL_COMDAT (decl) = true;
5833
5834 return DECL_TEMPLATE_RESULT (tmpl);
5835 }
5836
5837 tree
5838 push_template_decl (tree decl)
5839 {
5840 return push_template_decl_real (decl, false);
5841 }
5842
5843 /* FN is an inheriting constructor that inherits from the constructor
5844 template INHERITED; turn FN into a constructor template with a matching
5845 template header. */
5846
5847 tree
5848 add_inherited_template_parms (tree fn, tree inherited)
5849 {
5850 tree inner_parms
5851 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5852 inner_parms = copy_node (inner_parms);
5853 tree parms
5854 = tree_cons (size_int (processing_template_decl + 1),
5855 inner_parms, current_template_parms);
5856 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5857 tree args = template_parms_to_args (parms);
5858 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5859 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5860 DECL_TEMPLATE_RESULT (tmpl) = fn;
5861 DECL_ARTIFICIAL (tmpl) = true;
5862 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5863 return tmpl;
5864 }
5865
5866 /* Called when a class template TYPE is redeclared with the indicated
5867 template PARMS, e.g.:
5868
5869 template <class T> struct S;
5870 template <class T> struct S {}; */
5871
5872 bool
5873 redeclare_class_template (tree type, tree parms, tree cons)
5874 {
5875 tree tmpl;
5876 tree tmpl_parms;
5877 int i;
5878
5879 if (!TYPE_TEMPLATE_INFO (type))
5880 {
5881 error ("%qT is not a template type", type);
5882 return false;
5883 }
5884
5885 tmpl = TYPE_TI_TEMPLATE (type);
5886 if (!PRIMARY_TEMPLATE_P (tmpl))
5887 /* The type is nested in some template class. Nothing to worry
5888 about here; there are no new template parameters for the nested
5889 type. */
5890 return true;
5891
5892 if (!parms)
5893 {
5894 error ("template specifiers not specified in declaration of %qD",
5895 tmpl);
5896 return false;
5897 }
5898
5899 parms = INNERMOST_TEMPLATE_PARMS (parms);
5900 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5901
5902 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5903 {
5904 error_n (input_location, TREE_VEC_LENGTH (parms),
5905 "redeclared with %d template parameter",
5906 "redeclared with %d template parameters",
5907 TREE_VEC_LENGTH (parms));
5908 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5909 "previous declaration %qD used %d template parameter",
5910 "previous declaration %qD used %d template parameters",
5911 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5912 return false;
5913 }
5914
5915 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5916 {
5917 tree tmpl_parm;
5918 tree parm;
5919 tree tmpl_default;
5920 tree parm_default;
5921
5922 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5923 || TREE_VEC_ELT (parms, i) == error_mark_node)
5924 continue;
5925
5926 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5927 if (error_operand_p (tmpl_parm))
5928 return false;
5929
5930 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5931 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5932 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5933
5934 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5935 TEMPLATE_DECL. */
5936 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5937 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5938 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5939 || (TREE_CODE (tmpl_parm) != PARM_DECL
5940 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5941 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5942 || (TREE_CODE (tmpl_parm) == PARM_DECL
5943 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5944 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5945 {
5946 error ("template parameter %q+#D", tmpl_parm);
5947 error ("redeclared here as %q#D", parm);
5948 return false;
5949 }
5950
5951 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5952 {
5953 /* We have in [temp.param]:
5954
5955 A template-parameter may not be given default arguments
5956 by two different declarations in the same scope. */
5957 error_at (input_location, "redefinition of default argument for %q#D", parm);
5958 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5959 "original definition appeared here");
5960 return false;
5961 }
5962
5963 if (parm_default != NULL_TREE)
5964 /* Update the previous template parameters (which are the ones
5965 that will really count) with the new default value. */
5966 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5967 else if (tmpl_default != NULL_TREE)
5968 /* Update the new parameters, too; they'll be used as the
5969 parameters for any members. */
5970 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5971
5972 /* Give each template template parm in this redeclaration a
5973 DECL_CONTEXT of the template for which they are a parameter. */
5974 if (TREE_CODE (parm) == TEMPLATE_DECL)
5975 {
5976 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5977 DECL_CONTEXT (parm) = tmpl;
5978 }
5979
5980 if (TREE_CODE (parm) == TYPE_DECL)
5981 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5982 }
5983
5984 // Cannot redeclare a class template with a different set of constraints.
5985 if (!equivalent_constraints (get_constraints (tmpl), cons))
5986 {
5987 error_at (input_location, "redeclaration %q#D with different "
5988 "constraints", tmpl);
5989 inform (DECL_SOURCE_LOCATION (tmpl),
5990 "original declaration appeared here");
5991 }
5992
5993 return true;
5994 }
5995
5996 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5997 to be used when the caller has already checked
5998 (processing_template_decl
5999 && !instantiation_dependent_expression_p (expr)
6000 && potential_constant_expression (expr))
6001 and cleared processing_template_decl. */
6002
6003 tree
6004 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6005 {
6006 return tsubst_copy_and_build (expr,
6007 /*args=*/NULL_TREE,
6008 complain,
6009 /*in_decl=*/NULL_TREE,
6010 /*function_p=*/false,
6011 /*integral_constant_expression_p=*/true);
6012 }
6013
6014 /* Simplify EXPR if it is a non-dependent expression. Returns the
6015 (possibly simplified) expression. */
6016
6017 tree
6018 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6019 {
6020 if (expr == NULL_TREE)
6021 return NULL_TREE;
6022
6023 /* If we're in a template, but EXPR isn't value dependent, simplify
6024 it. We're supposed to treat:
6025
6026 template <typename T> void f(T[1 + 1]);
6027 template <typename T> void f(T[2]);
6028
6029 as two declarations of the same function, for example. */
6030 if (processing_template_decl
6031 && is_nondependent_constant_expression (expr))
6032 {
6033 processing_template_decl_sentinel s;
6034 expr = instantiate_non_dependent_expr_internal (expr, complain);
6035 }
6036 return expr;
6037 }
6038
6039 tree
6040 instantiate_non_dependent_expr (tree expr)
6041 {
6042 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6043 }
6044
6045 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6046 an uninstantiated expression. */
6047
6048 tree
6049 instantiate_non_dependent_or_null (tree expr)
6050 {
6051 if (expr == NULL_TREE)
6052 return NULL_TREE;
6053 if (processing_template_decl)
6054 {
6055 if (!is_nondependent_constant_expression (expr))
6056 expr = NULL_TREE;
6057 else
6058 {
6059 processing_template_decl_sentinel s;
6060 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6061 }
6062 }
6063 return expr;
6064 }
6065
6066 /* True iff T is a specialization of a variable template. */
6067
6068 bool
6069 variable_template_specialization_p (tree t)
6070 {
6071 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6072 return false;
6073 tree tmpl = DECL_TI_TEMPLATE (t);
6074 return variable_template_p (tmpl);
6075 }
6076
6077 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6078 template declaration, or a TYPE_DECL for an alias declaration. */
6079
6080 bool
6081 alias_type_or_template_p (tree t)
6082 {
6083 if (t == NULL_TREE)
6084 return false;
6085 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6086 || (TYPE_P (t)
6087 && TYPE_NAME (t)
6088 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6089 || DECL_ALIAS_TEMPLATE_P (t));
6090 }
6091
6092 /* Return TRUE iff T is a specialization of an alias template. */
6093
6094 bool
6095 alias_template_specialization_p (const_tree t)
6096 {
6097 /* It's an alias template specialization if it's an alias and its
6098 TYPE_NAME is a specialization of a primary template. */
6099 if (TYPE_ALIAS_P (t))
6100 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6101 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6102
6103 return false;
6104 }
6105
6106 /* An alias template is complex from a SFINAE perspective if a template-id
6107 using that alias can be ill-formed when the expansion is not, as with
6108 the void_t template. We determine this by checking whether the
6109 expansion for the alias template uses all its template parameters. */
6110
6111 struct uses_all_template_parms_data
6112 {
6113 int level;
6114 bool *seen;
6115 };
6116
6117 static int
6118 uses_all_template_parms_r (tree t, void *data_)
6119 {
6120 struct uses_all_template_parms_data &data
6121 = *(struct uses_all_template_parms_data*)data_;
6122 tree idx = get_template_parm_index (t);
6123
6124 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6125 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6126 return 0;
6127 }
6128
6129 static bool
6130 complex_alias_template_p (const_tree tmpl)
6131 {
6132 struct uses_all_template_parms_data data;
6133 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6134 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6135 data.level = TMPL_PARMS_DEPTH (parms);
6136 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6137 data.seen = XALLOCAVEC (bool, len);
6138 for (int i = 0; i < len; ++i)
6139 data.seen[i] = false;
6140
6141 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6142 for (int i = 0; i < len; ++i)
6143 if (!data.seen[i])
6144 return true;
6145 return false;
6146 }
6147
6148 /* Return TRUE iff T is a specialization of a complex alias template with
6149 dependent template-arguments. */
6150
6151 bool
6152 dependent_alias_template_spec_p (const_tree t)
6153 {
6154 if (!alias_template_specialization_p (t))
6155 return false;
6156
6157 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6158 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6159 return false;
6160
6161 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6162 if (!any_dependent_template_arguments_p (args))
6163 return false;
6164
6165 return true;
6166 }
6167
6168 /* Return the number of innermost template parameters in TMPL. */
6169
6170 static int
6171 num_innermost_template_parms (tree tmpl)
6172 {
6173 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6174 return TREE_VEC_LENGTH (parms);
6175 }
6176
6177 /* Return either TMPL or another template that it is equivalent to under DR
6178 1286: An alias that just changes the name of a template is equivalent to
6179 the other template. */
6180
6181 static tree
6182 get_underlying_template (tree tmpl)
6183 {
6184 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6185 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6186 {
6187 /* Determine if the alias is equivalent to an underlying template. */
6188 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6189 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6190 if (!tinfo)
6191 break;
6192
6193 tree underlying = TI_TEMPLATE (tinfo);
6194 if (!PRIMARY_TEMPLATE_P (underlying)
6195 || (num_innermost_template_parms (tmpl)
6196 != num_innermost_template_parms (underlying)))
6197 break;
6198
6199 tree alias_args = INNERMOST_TEMPLATE_ARGS
6200 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6201 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6202 break;
6203
6204 /* Alias is equivalent. Strip it and repeat. */
6205 tmpl = underlying;
6206 }
6207
6208 return tmpl;
6209 }
6210
6211 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6212 must be a reference-to-function or a pointer-to-function type, as specified
6213 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6214 and check that the resulting function has external linkage. */
6215
6216 static tree
6217 convert_nontype_argument_function (tree type, tree expr,
6218 tsubst_flags_t complain)
6219 {
6220 tree fns = expr;
6221 tree fn, fn_no_ptr;
6222 linkage_kind linkage;
6223
6224 fn = instantiate_type (type, fns, tf_none);
6225 if (fn == error_mark_node)
6226 return error_mark_node;
6227
6228 if (value_dependent_expression_p (fn))
6229 goto accept;
6230
6231 fn_no_ptr = strip_fnptr_conv (fn);
6232 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6233 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6234 if (BASELINK_P (fn_no_ptr))
6235 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6236
6237 /* [temp.arg.nontype]/1
6238
6239 A template-argument for a non-type, non-template template-parameter
6240 shall be one of:
6241 [...]
6242 -- the address of an object or function with external [C++11: or
6243 internal] linkage. */
6244
6245 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6246 {
6247 if (complain & tf_error)
6248 {
6249 error ("%qE is not a valid template argument for type %qT",
6250 expr, type);
6251 if (TYPE_PTR_P (type))
6252 inform (input_location, "it must be the address of a function "
6253 "with external linkage");
6254 else
6255 inform (input_location, "it must be the name of a function with "
6256 "external linkage");
6257 }
6258 return NULL_TREE;
6259 }
6260
6261 linkage = decl_linkage (fn_no_ptr);
6262 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6263 {
6264 if (complain & tf_error)
6265 {
6266 if (cxx_dialect >= cxx11)
6267 error ("%qE is not a valid template argument for type %qT "
6268 "because %qD has no linkage",
6269 expr, type, fn_no_ptr);
6270 else
6271 error ("%qE is not a valid template argument for type %qT "
6272 "because %qD does not have external linkage",
6273 expr, type, fn_no_ptr);
6274 }
6275 return NULL_TREE;
6276 }
6277
6278 accept:
6279 if (TREE_CODE (type) == REFERENCE_TYPE)
6280 {
6281 if (REFERENCE_REF_P (fn))
6282 fn = TREE_OPERAND (fn, 0);
6283 else
6284 fn = build_address (fn);
6285 }
6286 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6287 fn = build_nop (type, fn);
6288
6289 return fn;
6290 }
6291
6292 /* Subroutine of convert_nontype_argument.
6293 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6294 Emit an error otherwise. */
6295
6296 static bool
6297 check_valid_ptrmem_cst_expr (tree type, tree expr,
6298 tsubst_flags_t complain)
6299 {
6300 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6301 tree orig_expr = expr;
6302 STRIP_NOPS (expr);
6303 if (null_ptr_cst_p (expr))
6304 return true;
6305 if (TREE_CODE (expr) == PTRMEM_CST
6306 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6307 PTRMEM_CST_CLASS (expr)))
6308 return true;
6309 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6310 return true;
6311 if (processing_template_decl
6312 && TREE_CODE (expr) == ADDR_EXPR
6313 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6314 return true;
6315 if (complain & tf_error)
6316 {
6317 error_at (loc, "%qE is not a valid template argument for type %qT",
6318 orig_expr, type);
6319 if (TREE_CODE (expr) != PTRMEM_CST)
6320 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6321 else
6322 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6323 }
6324 return false;
6325 }
6326
6327 /* Returns TRUE iff the address of OP is value-dependent.
6328
6329 14.6.2.4 [temp.dep.temp]:
6330 A non-integral non-type template-argument is dependent if its type is
6331 dependent or it has either of the following forms
6332 qualified-id
6333 & qualified-id
6334 and contains a nested-name-specifier which specifies a class-name that
6335 names a dependent type.
6336
6337 We generalize this to just say that the address of a member of a
6338 dependent class is value-dependent; the above doesn't cover the
6339 address of a static data member named with an unqualified-id. */
6340
6341 static bool
6342 has_value_dependent_address (tree op)
6343 {
6344 /* We could use get_inner_reference here, but there's no need;
6345 this is only relevant for template non-type arguments, which
6346 can only be expressed as &id-expression. */
6347 if (DECL_P (op))
6348 {
6349 tree ctx = CP_DECL_CONTEXT (op);
6350 if (TYPE_P (ctx) && dependent_type_p (ctx))
6351 return true;
6352 }
6353
6354 return false;
6355 }
6356
6357 /* The next set of functions are used for providing helpful explanatory
6358 diagnostics for failed overload resolution. Their messages should be
6359 indented by two spaces for consistency with the messages in
6360 call.c */
6361
6362 static int
6363 unify_success (bool /*explain_p*/)
6364 {
6365 return 0;
6366 }
6367
6368 /* Other failure functions should call this one, to provide a single function
6369 for setting a breakpoint on. */
6370
6371 static int
6372 unify_invalid (bool /*explain_p*/)
6373 {
6374 return 1;
6375 }
6376
6377 static int
6378 unify_parameter_deduction_failure (bool explain_p, tree parm)
6379 {
6380 if (explain_p)
6381 inform (input_location,
6382 " couldn't deduce template parameter %qD", parm);
6383 return unify_invalid (explain_p);
6384 }
6385
6386 static int
6387 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6388 {
6389 if (explain_p)
6390 inform (input_location,
6391 " types %qT and %qT have incompatible cv-qualifiers",
6392 parm, arg);
6393 return unify_invalid (explain_p);
6394 }
6395
6396 static int
6397 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6398 {
6399 if (explain_p)
6400 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6401 return unify_invalid (explain_p);
6402 }
6403
6404 static int
6405 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6406 {
6407 if (explain_p)
6408 inform (input_location,
6409 " template parameter %qD is not a parameter pack, but "
6410 "argument %qD is",
6411 parm, arg);
6412 return unify_invalid (explain_p);
6413 }
6414
6415 static int
6416 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6417 {
6418 if (explain_p)
6419 inform (input_location,
6420 " template argument %qE does not match "
6421 "pointer-to-member constant %qE",
6422 arg, parm);
6423 return unify_invalid (explain_p);
6424 }
6425
6426 static int
6427 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6428 {
6429 if (explain_p)
6430 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6431 return unify_invalid (explain_p);
6432 }
6433
6434 static int
6435 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6436 {
6437 if (explain_p)
6438 inform (input_location,
6439 " inconsistent parameter pack deduction with %qT and %qT",
6440 old_arg, new_arg);
6441 return unify_invalid (explain_p);
6442 }
6443
6444 static int
6445 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6446 {
6447 if (explain_p)
6448 {
6449 if (TYPE_P (parm))
6450 inform (input_location,
6451 " deduced conflicting types for parameter %qT (%qT and %qT)",
6452 parm, first, second);
6453 else
6454 inform (input_location,
6455 " deduced conflicting values for non-type parameter "
6456 "%qE (%qE and %qE)", parm, first, second);
6457 }
6458 return unify_invalid (explain_p);
6459 }
6460
6461 static int
6462 unify_vla_arg (bool explain_p, tree arg)
6463 {
6464 if (explain_p)
6465 inform (input_location,
6466 " variable-sized array type %qT is not "
6467 "a valid template argument",
6468 arg);
6469 return unify_invalid (explain_p);
6470 }
6471
6472 static int
6473 unify_method_type_error (bool explain_p, tree arg)
6474 {
6475 if (explain_p)
6476 inform (input_location,
6477 " member function type %qT is not a valid template argument",
6478 arg);
6479 return unify_invalid (explain_p);
6480 }
6481
6482 static int
6483 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6484 {
6485 if (explain_p)
6486 {
6487 if (least_p)
6488 inform_n (input_location, wanted,
6489 " candidate expects at least %d argument, %d provided",
6490 " candidate expects at least %d arguments, %d provided",
6491 wanted, have);
6492 else
6493 inform_n (input_location, wanted,
6494 " candidate expects %d argument, %d provided",
6495 " candidate expects %d arguments, %d provided",
6496 wanted, have);
6497 }
6498 return unify_invalid (explain_p);
6499 }
6500
6501 static int
6502 unify_too_many_arguments (bool explain_p, int have, int wanted)
6503 {
6504 return unify_arity (explain_p, have, wanted);
6505 }
6506
6507 static int
6508 unify_too_few_arguments (bool explain_p, int have, int wanted,
6509 bool least_p = false)
6510 {
6511 return unify_arity (explain_p, have, wanted, least_p);
6512 }
6513
6514 static int
6515 unify_arg_conversion (bool explain_p, tree to_type,
6516 tree from_type, tree arg)
6517 {
6518 if (explain_p)
6519 inform (EXPR_LOC_OR_LOC (arg, input_location),
6520 " cannot convert %qE (type %qT) to type %qT",
6521 arg, from_type, to_type);
6522 return unify_invalid (explain_p);
6523 }
6524
6525 static int
6526 unify_no_common_base (bool explain_p, enum template_base_result r,
6527 tree parm, tree arg)
6528 {
6529 if (explain_p)
6530 switch (r)
6531 {
6532 case tbr_ambiguous_baseclass:
6533 inform (input_location, " %qT is an ambiguous base class of %qT",
6534 parm, arg);
6535 break;
6536 default:
6537 inform (input_location, " %qT is not derived from %qT", arg, parm);
6538 break;
6539 }
6540 return unify_invalid (explain_p);
6541 }
6542
6543 static int
6544 unify_inconsistent_template_template_parameters (bool explain_p)
6545 {
6546 if (explain_p)
6547 inform (input_location,
6548 " template parameters of a template template argument are "
6549 "inconsistent with other deduced template arguments");
6550 return unify_invalid (explain_p);
6551 }
6552
6553 static int
6554 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6555 {
6556 if (explain_p)
6557 inform (input_location,
6558 " can't deduce a template for %qT from non-template type %qT",
6559 parm, arg);
6560 return unify_invalid (explain_p);
6561 }
6562
6563 static int
6564 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6565 {
6566 if (explain_p)
6567 inform (input_location,
6568 " template argument %qE does not match %qE", arg, parm);
6569 return unify_invalid (explain_p);
6570 }
6571
6572 /* Attempt to convert the non-type template parameter EXPR to the
6573 indicated TYPE. If the conversion is successful, return the
6574 converted value. If the conversion is unsuccessful, return
6575 NULL_TREE if we issued an error message, or error_mark_node if we
6576 did not. We issue error messages for out-and-out bad template
6577 parameters, but not simply because the conversion failed, since we
6578 might be just trying to do argument deduction. Both TYPE and EXPR
6579 must be non-dependent.
6580
6581 The conversion follows the special rules described in
6582 [temp.arg.nontype], and it is much more strict than an implicit
6583 conversion.
6584
6585 This function is called twice for each template argument (see
6586 lookup_template_class for a more accurate description of this
6587 problem). This means that we need to handle expressions which
6588 are not valid in a C++ source, but can be created from the
6589 first call (for instance, casts to perform conversions). These
6590 hacks can go away after we fix the double coercion problem. */
6591
6592 static tree
6593 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6594 {
6595 tree expr_type;
6596 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6597 tree orig_expr = expr;
6598
6599 /* Detect immediately string literals as invalid non-type argument.
6600 This special-case is not needed for correctness (we would easily
6601 catch this later), but only to provide better diagnostic for this
6602 common user mistake. As suggested by DR 100, we do not mention
6603 linkage issues in the diagnostic as this is not the point. */
6604 /* FIXME we're making this OK. */
6605 if (TREE_CODE (expr) == STRING_CST)
6606 {
6607 if (complain & tf_error)
6608 error ("%qE is not a valid template argument for type %qT "
6609 "because string literals can never be used in this context",
6610 expr, type);
6611 return NULL_TREE;
6612 }
6613
6614 /* Add the ADDR_EXPR now for the benefit of
6615 value_dependent_expression_p. */
6616 if (TYPE_PTROBV_P (type)
6617 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6618 {
6619 expr = decay_conversion (expr, complain);
6620 if (expr == error_mark_node)
6621 return error_mark_node;
6622 }
6623
6624 /* If we are in a template, EXPR may be non-dependent, but still
6625 have a syntactic, rather than semantic, form. For example, EXPR
6626 might be a SCOPE_REF, rather than the VAR_DECL to which the
6627 SCOPE_REF refers. Preserving the qualifying scope is necessary
6628 so that access checking can be performed when the template is
6629 instantiated -- but here we need the resolved form so that we can
6630 convert the argument. */
6631 bool non_dep = false;
6632 if (TYPE_REF_OBJ_P (type)
6633 && has_value_dependent_address (expr))
6634 /* If we want the address and it's value-dependent, don't fold. */;
6635 else if (processing_template_decl
6636 && is_nondependent_constant_expression (expr))
6637 non_dep = true;
6638 if (error_operand_p (expr))
6639 return error_mark_node;
6640 expr_type = TREE_TYPE (expr);
6641
6642 /* If the argument is non-dependent, perform any conversions in
6643 non-dependent context as well. */
6644 processing_template_decl_sentinel s (non_dep);
6645 if (non_dep)
6646 expr = instantiate_non_dependent_expr_internal (expr, complain);
6647
6648 if (value_dependent_expression_p (expr))
6649 expr = canonicalize_expr_argument (expr, complain);
6650
6651 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6652 to a non-type argument of "nullptr". */
6653 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6654 expr = fold_simple (convert (type, expr));
6655
6656 /* In C++11, integral or enumeration non-type template arguments can be
6657 arbitrary constant expressions. Pointer and pointer to
6658 member arguments can be general constant expressions that evaluate
6659 to a null value, but otherwise still need to be of a specific form. */
6660 if (cxx_dialect >= cxx11)
6661 {
6662 if (TREE_CODE (expr) == PTRMEM_CST)
6663 /* A PTRMEM_CST is already constant, and a valid template
6664 argument for a parameter of pointer to member type, we just want
6665 to leave it in that form rather than lower it to a
6666 CONSTRUCTOR. */;
6667 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6668 || cxx_dialect >= cxx17)
6669 {
6670 /* C++17: A template-argument for a non-type template-parameter shall
6671 be a converted constant expression (8.20) of the type of the
6672 template-parameter. */
6673 expr = build_converted_constant_expr (type, expr, complain);
6674 if (expr == error_mark_node)
6675 return error_mark_node;
6676 expr = maybe_constant_value (expr);
6677 expr = convert_from_reference (expr);
6678 }
6679 else if (TYPE_PTR_OR_PTRMEM_P (type))
6680 {
6681 tree folded = maybe_constant_value (expr);
6682 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6683 : null_member_pointer_value_p (folded))
6684 expr = folded;
6685 }
6686 }
6687
6688 if (TREE_CODE (type) == REFERENCE_TYPE)
6689 expr = mark_lvalue_use (expr);
6690 else
6691 expr = mark_rvalue_use (expr);
6692
6693 /* HACK: Due to double coercion, we can get a
6694 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6695 which is the tree that we built on the first call (see
6696 below when coercing to reference to object or to reference to
6697 function). We just strip everything and get to the arg.
6698 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6699 for examples. */
6700 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6701 {
6702 tree probe_type, probe = expr;
6703 if (REFERENCE_REF_P (probe))
6704 probe = TREE_OPERAND (probe, 0);
6705 probe_type = TREE_TYPE (probe);
6706 if (TREE_CODE (probe) == NOP_EXPR)
6707 {
6708 /* ??? Maybe we could use convert_from_reference here, but we
6709 would need to relax its constraints because the NOP_EXPR
6710 could actually change the type to something more cv-qualified,
6711 and this is not folded by convert_from_reference. */
6712 tree addr = TREE_OPERAND (probe, 0);
6713 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6714 && TREE_CODE (addr) == ADDR_EXPR
6715 && TYPE_PTR_P (TREE_TYPE (addr))
6716 && (same_type_ignoring_top_level_qualifiers_p
6717 (TREE_TYPE (probe_type),
6718 TREE_TYPE (TREE_TYPE (addr)))))
6719 {
6720 expr = TREE_OPERAND (addr, 0);
6721 expr_type = TREE_TYPE (probe_type);
6722 }
6723 }
6724 }
6725
6726 /* [temp.arg.nontype]/5, bullet 1
6727
6728 For a non-type template-parameter of integral or enumeration type,
6729 integral promotions (_conv.prom_) and integral conversions
6730 (_conv.integral_) are applied. */
6731 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6732 {
6733 if (cxx_dialect < cxx11)
6734 {
6735 tree t = build_converted_constant_expr (type, expr, complain);
6736 t = maybe_constant_value (t);
6737 if (t != error_mark_node)
6738 expr = t;
6739 }
6740
6741 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6742 return error_mark_node;
6743
6744 /* Notice that there are constant expressions like '4 % 0' which
6745 do not fold into integer constants. */
6746 if (TREE_CODE (expr) != INTEGER_CST
6747 && !value_dependent_expression_p (expr))
6748 {
6749 if (complain & tf_error)
6750 {
6751 int errs = errorcount, warns = warningcount + werrorcount;
6752 if (!require_potential_constant_expression (expr))
6753 expr = error_mark_node;
6754 else
6755 expr = cxx_constant_value (expr);
6756 if (errorcount > errs || warningcount + werrorcount > warns)
6757 inform (loc, "in template argument for type %qT", type);
6758 if (expr == error_mark_node)
6759 return NULL_TREE;
6760 /* else cxx_constant_value complained but gave us
6761 a real constant, so go ahead. */
6762 if (TREE_CODE (expr) != INTEGER_CST)
6763 {
6764 /* Some assemble time constant expressions like
6765 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6766 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6767 as we can emit them into .rodata initializers of
6768 variables, yet they can't fold into an INTEGER_CST at
6769 compile time. Refuse them here. */
6770 gcc_checking_assert (reduced_constant_expression_p (expr));
6771 error_at (loc, "template argument %qE for type %qT not "
6772 "a constant integer", expr, type);
6773 return NULL_TREE;
6774 }
6775 }
6776 else
6777 return NULL_TREE;
6778 }
6779
6780 /* Avoid typedef problems. */
6781 if (TREE_TYPE (expr) != type)
6782 expr = fold_convert (type, expr);
6783 }
6784 /* [temp.arg.nontype]/5, bullet 2
6785
6786 For a non-type template-parameter of type pointer to object,
6787 qualification conversions (_conv.qual_) and the array-to-pointer
6788 conversion (_conv.array_) are applied. */
6789 else if (TYPE_PTROBV_P (type))
6790 {
6791 tree decayed = expr;
6792
6793 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6794 decay_conversion or an explicit cast. If it's a problematic cast,
6795 we'll complain about it below. */
6796 if (TREE_CODE (expr) == NOP_EXPR)
6797 {
6798 tree probe = expr;
6799 STRIP_NOPS (probe);
6800 if (TREE_CODE (probe) == ADDR_EXPR
6801 && TYPE_PTR_P (TREE_TYPE (probe)))
6802 {
6803 expr = probe;
6804 expr_type = TREE_TYPE (expr);
6805 }
6806 }
6807
6808 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6809
6810 A template-argument for a non-type, non-template template-parameter
6811 shall be one of: [...]
6812
6813 -- the name of a non-type template-parameter;
6814 -- the address of an object or function with external linkage, [...]
6815 expressed as "& id-expression" where the & is optional if the name
6816 refers to a function or array, or if the corresponding
6817 template-parameter is a reference.
6818
6819 Here, we do not care about functions, as they are invalid anyway
6820 for a parameter of type pointer-to-object. */
6821
6822 if (value_dependent_expression_p (expr))
6823 /* Non-type template parameters are OK. */
6824 ;
6825 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6826 /* Null pointer values are OK in C++11. */;
6827 else if (TREE_CODE (expr) != ADDR_EXPR)
6828 {
6829 if (VAR_P (expr))
6830 {
6831 if (complain & tf_error)
6832 error ("%qD is not a valid template argument "
6833 "because %qD is a variable, not the address of "
6834 "a variable", orig_expr, expr);
6835 return NULL_TREE;
6836 }
6837 if (POINTER_TYPE_P (expr_type))
6838 {
6839 if (complain & tf_error)
6840 error ("%qE is not a valid template argument for %qT "
6841 "because it is not the address of a variable",
6842 orig_expr, type);
6843 return NULL_TREE;
6844 }
6845 /* Other values, like integer constants, might be valid
6846 non-type arguments of some other type. */
6847 return error_mark_node;
6848 }
6849 else
6850 {
6851 tree decl = TREE_OPERAND (expr, 0);
6852
6853 if (!VAR_P (decl))
6854 {
6855 if (complain & tf_error)
6856 error ("%qE is not a valid template argument of type %qT "
6857 "because %qE is not a variable", orig_expr, type, decl);
6858 return NULL_TREE;
6859 }
6860 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6861 {
6862 if (complain & tf_error)
6863 error ("%qE is not a valid template argument of type %qT "
6864 "because %qD does not have external linkage",
6865 orig_expr, type, decl);
6866 return NULL_TREE;
6867 }
6868 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6869 && decl_linkage (decl) == lk_none)
6870 {
6871 if (complain & tf_error)
6872 error ("%qE is not a valid template argument of type %qT "
6873 "because %qD has no linkage", orig_expr, type, decl);
6874 return NULL_TREE;
6875 }
6876 /* C++17: For a non-type template-parameter of reference or pointer
6877 type, the value of the constant expression shall not refer to (or
6878 for a pointer type, shall not be the address of):
6879 * a subobject (4.5),
6880 * a temporary object (15.2),
6881 * a string literal (5.13.5),
6882 * the result of a typeid expression (8.2.8), or
6883 * a predefined __func__ variable (11.4.1). */
6884 else if (DECL_ARTIFICIAL (decl))
6885 {
6886 if (complain & tf_error)
6887 error ("the address of %qD is not a valid template argument",
6888 decl);
6889 return NULL_TREE;
6890 }
6891 else if (!same_type_ignoring_top_level_qualifiers_p
6892 (strip_array_types (TREE_TYPE (type)),
6893 strip_array_types (TREE_TYPE (decl))))
6894 {
6895 if (complain & tf_error)
6896 error ("the address of the %qT subobject of %qD is not a "
6897 "valid template argument", TREE_TYPE (type), decl);
6898 return NULL_TREE;
6899 }
6900 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6901 {
6902 if (complain & tf_error)
6903 error ("the address of %qD is not a valid template argument "
6904 "because it does not have static storage duration",
6905 decl);
6906 return NULL_TREE;
6907 }
6908 }
6909
6910 expr = decayed;
6911
6912 expr = perform_qualification_conversions (type, expr);
6913 if (expr == error_mark_node)
6914 return error_mark_node;
6915 }
6916 /* [temp.arg.nontype]/5, bullet 3
6917
6918 For a non-type template-parameter of type reference to object, no
6919 conversions apply. The type referred to by the reference may be more
6920 cv-qualified than the (otherwise identical) type of the
6921 template-argument. The template-parameter is bound directly to the
6922 template-argument, which must be an lvalue. */
6923 else if (TYPE_REF_OBJ_P (type))
6924 {
6925 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6926 expr_type))
6927 return error_mark_node;
6928
6929 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6930 {
6931 if (complain & tf_error)
6932 error ("%qE is not a valid template argument for type %qT "
6933 "because of conflicts in cv-qualification", expr, type);
6934 return NULL_TREE;
6935 }
6936
6937 if (!lvalue_p (expr))
6938 {
6939 if (complain & tf_error)
6940 error ("%qE is not a valid template argument for type %qT "
6941 "because it is not an lvalue", expr, type);
6942 return NULL_TREE;
6943 }
6944
6945 /* [temp.arg.nontype]/1
6946
6947 A template-argument for a non-type, non-template template-parameter
6948 shall be one of: [...]
6949
6950 -- the address of an object or function with external linkage. */
6951 if (INDIRECT_REF_P (expr)
6952 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6953 {
6954 expr = TREE_OPERAND (expr, 0);
6955 if (DECL_P (expr))
6956 {
6957 if (complain & tf_error)
6958 error ("%q#D is not a valid template argument for type %qT "
6959 "because a reference variable does not have a constant "
6960 "address", expr, type);
6961 return NULL_TREE;
6962 }
6963 }
6964
6965 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6966 && value_dependent_expression_p (expr))
6967 /* OK, dependent reference. We don't want to ask whether a DECL is
6968 itself value-dependent, since what we want here is its address. */;
6969 else
6970 {
6971 if (!DECL_P (expr))
6972 {
6973 if (complain & tf_error)
6974 error ("%qE is not a valid template argument for type %qT "
6975 "because it is not an object with linkage",
6976 expr, type);
6977 return NULL_TREE;
6978 }
6979
6980 /* DR 1155 allows internal linkage in C++11 and up. */
6981 linkage_kind linkage = decl_linkage (expr);
6982 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6983 {
6984 if (complain & tf_error)
6985 error ("%qE is not a valid template argument for type %qT "
6986 "because object %qD does not have linkage",
6987 expr, type, expr);
6988 return NULL_TREE;
6989 }
6990
6991 expr = build_address (expr);
6992 }
6993
6994 if (!same_type_p (type, TREE_TYPE (expr)))
6995 expr = build_nop (type, expr);
6996 }
6997 /* [temp.arg.nontype]/5, bullet 4
6998
6999 For a non-type template-parameter of type pointer to function, only
7000 the function-to-pointer conversion (_conv.func_) is applied. If the
7001 template-argument represents a set of overloaded functions (or a
7002 pointer to such), the matching function is selected from the set
7003 (_over.over_). */
7004 else if (TYPE_PTRFN_P (type))
7005 {
7006 /* If the argument is a template-id, we might not have enough
7007 context information to decay the pointer. */
7008 if (!type_unknown_p (expr_type))
7009 {
7010 expr = decay_conversion (expr, complain);
7011 if (expr == error_mark_node)
7012 return error_mark_node;
7013 }
7014
7015 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7016 /* Null pointer values are OK in C++11. */
7017 return perform_qualification_conversions (type, expr);
7018
7019 expr = convert_nontype_argument_function (type, expr, complain);
7020 if (!expr || expr == error_mark_node)
7021 return expr;
7022 }
7023 /* [temp.arg.nontype]/5, bullet 5
7024
7025 For a non-type template-parameter of type reference to function, no
7026 conversions apply. If the template-argument represents a set of
7027 overloaded functions, the matching function is selected from the set
7028 (_over.over_). */
7029 else if (TYPE_REFFN_P (type))
7030 {
7031 if (TREE_CODE (expr) == ADDR_EXPR)
7032 {
7033 if (complain & tf_error)
7034 {
7035 error ("%qE is not a valid template argument for type %qT "
7036 "because it is a pointer", expr, type);
7037 inform (input_location, "try using %qE instead",
7038 TREE_OPERAND (expr, 0));
7039 }
7040 return NULL_TREE;
7041 }
7042
7043 expr = convert_nontype_argument_function (type, expr, complain);
7044 if (!expr || expr == error_mark_node)
7045 return expr;
7046 }
7047 /* [temp.arg.nontype]/5, bullet 6
7048
7049 For a non-type template-parameter of type pointer to member function,
7050 no conversions apply. If the template-argument represents a set of
7051 overloaded member functions, the matching member function is selected
7052 from the set (_over.over_). */
7053 else if (TYPE_PTRMEMFUNC_P (type))
7054 {
7055 expr = instantiate_type (type, expr, tf_none);
7056 if (expr == error_mark_node)
7057 return error_mark_node;
7058
7059 /* [temp.arg.nontype] bullet 1 says the pointer to member
7060 expression must be a pointer-to-member constant. */
7061 if (!value_dependent_expression_p (expr)
7062 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7063 return NULL_TREE;
7064
7065 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7066 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7067 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7068 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7069 }
7070 /* [temp.arg.nontype]/5, bullet 7
7071
7072 For a non-type template-parameter of type pointer to data member,
7073 qualification conversions (_conv.qual_) are applied. */
7074 else if (TYPE_PTRDATAMEM_P (type))
7075 {
7076 /* [temp.arg.nontype] bullet 1 says the pointer to member
7077 expression must be a pointer-to-member constant. */
7078 if (!value_dependent_expression_p (expr)
7079 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7080 return NULL_TREE;
7081
7082 expr = perform_qualification_conversions (type, expr);
7083 if (expr == error_mark_node)
7084 return expr;
7085 }
7086 else if (NULLPTR_TYPE_P (type))
7087 {
7088 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7089 {
7090 if (complain & tf_error)
7091 error ("%qE is not a valid template argument for type %qT "
7092 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7093 return NULL_TREE;
7094 }
7095 return expr;
7096 }
7097 /* A template non-type parameter must be one of the above. */
7098 else
7099 gcc_unreachable ();
7100
7101 /* Sanity check: did we actually convert the argument to the
7102 right type? */
7103 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7104 (type, TREE_TYPE (expr)));
7105 return convert_from_reference (expr);
7106 }
7107
7108 /* Subroutine of coerce_template_template_parms, which returns 1 if
7109 PARM_PARM and ARG_PARM match using the rule for the template
7110 parameters of template template parameters. Both PARM and ARG are
7111 template parameters; the rest of the arguments are the same as for
7112 coerce_template_template_parms.
7113 */
7114 static int
7115 coerce_template_template_parm (tree parm,
7116 tree arg,
7117 tsubst_flags_t complain,
7118 tree in_decl,
7119 tree outer_args)
7120 {
7121 if (arg == NULL_TREE || error_operand_p (arg)
7122 || parm == NULL_TREE || error_operand_p (parm))
7123 return 0;
7124
7125 if (TREE_CODE (arg) != TREE_CODE (parm))
7126 return 0;
7127
7128 switch (TREE_CODE (parm))
7129 {
7130 case TEMPLATE_DECL:
7131 /* We encounter instantiations of templates like
7132 template <template <template <class> class> class TT>
7133 class C; */
7134 {
7135 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7136 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7137
7138 if (!coerce_template_template_parms
7139 (parmparm, argparm, complain, in_decl, outer_args))
7140 return 0;
7141 }
7142 /* Fall through. */
7143
7144 case TYPE_DECL:
7145 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7146 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7147 /* Argument is a parameter pack but parameter is not. */
7148 return 0;
7149 break;
7150
7151 case PARM_DECL:
7152 /* The tsubst call is used to handle cases such as
7153
7154 template <int> class C {};
7155 template <class T, template <T> class TT> class D {};
7156 D<int, C> d;
7157
7158 i.e. the parameter list of TT depends on earlier parameters. */
7159 if (!uses_template_parms (TREE_TYPE (arg)))
7160 {
7161 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7162 if (!uses_template_parms (t)
7163 && !same_type_p (t, TREE_TYPE (arg)))
7164 return 0;
7165 }
7166
7167 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7168 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7169 /* Argument is a parameter pack but parameter is not. */
7170 return 0;
7171
7172 break;
7173
7174 default:
7175 gcc_unreachable ();
7176 }
7177
7178 return 1;
7179 }
7180
7181 /* Coerce template argument list ARGLIST for use with template
7182 template-parameter TEMPL. */
7183
7184 static tree
7185 coerce_template_args_for_ttp (tree templ, tree arglist,
7186 tsubst_flags_t complain)
7187 {
7188 /* Consider an example where a template template parameter declared as
7189
7190 template <class T, class U = std::allocator<T> > class TT
7191
7192 The template parameter level of T and U are one level larger than
7193 of TT. To proper process the default argument of U, say when an
7194 instantiation `TT<int>' is seen, we need to build the full
7195 arguments containing {int} as the innermost level. Outer levels,
7196 available when not appearing as default template argument, can be
7197 obtained from the arguments of the enclosing template.
7198
7199 Suppose that TT is later substituted with std::vector. The above
7200 instantiation is `TT<int, std::allocator<T> >' with TT at
7201 level 1, and T at level 2, while the template arguments at level 1
7202 becomes {std::vector} and the inner level 2 is {int}. */
7203
7204 tree outer = DECL_CONTEXT (templ);
7205 if (outer)
7206 {
7207 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7208 /* We want arguments for the partial specialization, not arguments for
7209 the primary template. */
7210 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7211 else
7212 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7213 }
7214 else if (current_template_parms)
7215 {
7216 /* This is an argument of the current template, so we haven't set
7217 DECL_CONTEXT yet. */
7218 tree relevant_template_parms;
7219
7220 /* Parameter levels that are greater than the level of the given
7221 template template parm are irrelevant. */
7222 relevant_template_parms = current_template_parms;
7223 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7224 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7225 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7226
7227 outer = template_parms_to_args (relevant_template_parms);
7228 }
7229
7230 if (outer)
7231 arglist = add_to_template_args (outer, arglist);
7232
7233 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7234 return coerce_template_parms (parmlist, arglist, templ,
7235 complain,
7236 /*require_all_args=*/true,
7237 /*use_default_args=*/true);
7238 }
7239
7240 /* A cache of template template parameters with match-all default
7241 arguments. */
7242 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7243 static void
7244 store_defaulted_ttp (tree v, tree t)
7245 {
7246 if (!defaulted_ttp_cache)
7247 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7248 defaulted_ttp_cache->put (v, t);
7249 }
7250 static tree
7251 lookup_defaulted_ttp (tree v)
7252 {
7253 if (defaulted_ttp_cache)
7254 if (tree *p = defaulted_ttp_cache->get (v))
7255 return *p;
7256 return NULL_TREE;
7257 }
7258
7259 /* T is a bound template template-parameter. Copy its arguments into default
7260 arguments of the template template-parameter's template parameters. */
7261
7262 static tree
7263 add_defaults_to_ttp (tree otmpl)
7264 {
7265 if (tree c = lookup_defaulted_ttp (otmpl))
7266 return c;
7267
7268 tree ntmpl = copy_node (otmpl);
7269
7270 tree ntype = copy_node (TREE_TYPE (otmpl));
7271 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7272 TYPE_MAIN_VARIANT (ntype) = ntype;
7273 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7274 TYPE_NAME (ntype) = ntmpl;
7275 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7276
7277 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7278 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7279 TEMPLATE_PARM_DECL (idx) = ntmpl;
7280 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7281
7282 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7283 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7284 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7285 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7286 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7287 {
7288 tree o = TREE_VEC_ELT (vec, i);
7289 if (!template_parameter_pack_p (TREE_VALUE (o)))
7290 {
7291 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7292 TREE_PURPOSE (n) = any_targ_node;
7293 }
7294 }
7295
7296 store_defaulted_ttp (otmpl, ntmpl);
7297 return ntmpl;
7298 }
7299
7300 /* ARG is a bound potential template template-argument, and PARGS is a list
7301 of arguments for the corresponding template template-parameter. Adjust
7302 PARGS as appropriate for application to ARG's template, and if ARG is a
7303 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7304 arguments to the template template parameter. */
7305
7306 static tree
7307 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7308 {
7309 ++processing_template_decl;
7310 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7311 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7312 {
7313 /* When comparing two template template-parameters in partial ordering,
7314 rewrite the one currently being used as an argument to have default
7315 arguments for all parameters. */
7316 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7317 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7318 if (pargs != error_mark_node)
7319 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7320 TYPE_TI_ARGS (arg));
7321 }
7322 else
7323 {
7324 tree aparms
7325 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7326 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7327 /*require_all*/true,
7328 /*use_default*/true);
7329 }
7330 --processing_template_decl;
7331 return pargs;
7332 }
7333
7334 /* Subroutine of unify for the case when PARM is a
7335 BOUND_TEMPLATE_TEMPLATE_PARM. */
7336
7337 static int
7338 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7339 bool explain_p)
7340 {
7341 tree parmvec = TYPE_TI_ARGS (parm);
7342 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7343
7344 /* The template template parm might be variadic and the argument
7345 not, so flatten both argument lists. */
7346 parmvec = expand_template_argument_pack (parmvec);
7347 argvec = expand_template_argument_pack (argvec);
7348
7349 if (flag_new_ttp)
7350 {
7351 /* In keeping with P0522R0, adjust P's template arguments
7352 to apply to A's template; then flatten it again. */
7353 tree nparmvec = parmvec;
7354 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7355 nparmvec = expand_template_argument_pack (nparmvec);
7356
7357 if (unify (tparms, targs, nparmvec, argvec,
7358 UNIFY_ALLOW_NONE, explain_p))
7359 return 1;
7360
7361 /* If the P0522 adjustment eliminated a pack expansion, deduce
7362 empty packs. */
7363 if (flag_new_ttp
7364 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7365 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7366 DEDUCE_EXACT, /*sub*/true, explain_p))
7367 return 1;
7368 }
7369 else
7370 {
7371 /* Deduce arguments T, i from TT<T> or TT<i>.
7372 We check each element of PARMVEC and ARGVEC individually
7373 rather than the whole TREE_VEC since they can have
7374 different number of elements, which is allowed under N2555. */
7375
7376 int len = TREE_VEC_LENGTH (parmvec);
7377
7378 /* Check if the parameters end in a pack, making them
7379 variadic. */
7380 int parm_variadic_p = 0;
7381 if (len > 0
7382 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7383 parm_variadic_p = 1;
7384
7385 for (int i = 0; i < len - parm_variadic_p; ++i)
7386 /* If the template argument list of P contains a pack
7387 expansion that is not the last template argument, the
7388 entire template argument list is a non-deduced
7389 context. */
7390 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7391 return unify_success (explain_p);
7392
7393 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7394 return unify_too_few_arguments (explain_p,
7395 TREE_VEC_LENGTH (argvec), len);
7396
7397 for (int i = 0; i < len - parm_variadic_p; ++i)
7398 if (unify (tparms, targs,
7399 TREE_VEC_ELT (parmvec, i),
7400 TREE_VEC_ELT (argvec, i),
7401 UNIFY_ALLOW_NONE, explain_p))
7402 return 1;
7403
7404 if (parm_variadic_p
7405 && unify_pack_expansion (tparms, targs,
7406 parmvec, argvec,
7407 DEDUCE_EXACT,
7408 /*subr=*/true, explain_p))
7409 return 1;
7410 }
7411
7412 return 0;
7413 }
7414
7415 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7416 template template parameters. Both PARM_PARMS and ARG_PARMS are
7417 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7418 or PARM_DECL.
7419
7420 Consider the example:
7421 template <class T> class A;
7422 template<template <class U> class TT> class B;
7423
7424 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7425 the parameters to A, and OUTER_ARGS contains A. */
7426
7427 static int
7428 coerce_template_template_parms (tree parm_parms,
7429 tree arg_parms,
7430 tsubst_flags_t complain,
7431 tree in_decl,
7432 tree outer_args)
7433 {
7434 int nparms, nargs, i;
7435 tree parm, arg;
7436 int variadic_p = 0;
7437
7438 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7439 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7440
7441 nparms = TREE_VEC_LENGTH (parm_parms);
7442 nargs = TREE_VEC_LENGTH (arg_parms);
7443
7444 if (flag_new_ttp)
7445 {
7446 /* P0522R0: A template template-parameter P is at least as specialized as
7447 a template template-argument A if, given the following rewrite to two
7448 function templates, the function template corresponding to P is at
7449 least as specialized as the function template corresponding to A
7450 according to the partial ordering rules for function templates
7451 ([temp.func.order]). Given an invented class template X with the
7452 template parameter list of A (including default arguments):
7453
7454 * Each of the two function templates has the same template parameters,
7455 respectively, as P or A.
7456
7457 * Each function template has a single function parameter whose type is
7458 a specialization of X with template arguments corresponding to the
7459 template parameters from the respective function template where, for
7460 each template parameter PP in the template parameter list of the
7461 function template, a corresponding template argument AA is formed. If
7462 PP declares a parameter pack, then AA is the pack expansion
7463 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7464
7465 If the rewrite produces an invalid type, then P is not at least as
7466 specialized as A. */
7467
7468 /* So coerce P's args to apply to A's parms, and then deduce between A's
7469 args and the converted args. If that succeeds, A is at least as
7470 specialized as P, so they match.*/
7471 tree pargs = template_parms_level_to_args (parm_parms);
7472 ++processing_template_decl;
7473 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7474 /*require_all*/true, /*use_default*/true);
7475 --processing_template_decl;
7476 if (pargs != error_mark_node)
7477 {
7478 tree targs = make_tree_vec (nargs);
7479 tree aargs = template_parms_level_to_args (arg_parms);
7480 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7481 /*explain*/false))
7482 return 1;
7483 }
7484 }
7485
7486 /* Determine whether we have a parameter pack at the end of the
7487 template template parameter's template parameter list. */
7488 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7489 {
7490 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7491
7492 if (error_operand_p (parm))
7493 return 0;
7494
7495 switch (TREE_CODE (parm))
7496 {
7497 case TEMPLATE_DECL:
7498 case TYPE_DECL:
7499 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7500 variadic_p = 1;
7501 break;
7502
7503 case PARM_DECL:
7504 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7505 variadic_p = 1;
7506 break;
7507
7508 default:
7509 gcc_unreachable ();
7510 }
7511 }
7512
7513 if (nargs != nparms
7514 && !(variadic_p && nargs >= nparms - 1))
7515 return 0;
7516
7517 /* Check all of the template parameters except the parameter pack at
7518 the end (if any). */
7519 for (i = 0; i < nparms - variadic_p; ++i)
7520 {
7521 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7522 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7523 continue;
7524
7525 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7526 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7527
7528 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7529 outer_args))
7530 return 0;
7531
7532 }
7533
7534 if (variadic_p)
7535 {
7536 /* Check each of the template parameters in the template
7537 argument against the template parameter pack at the end of
7538 the template template parameter. */
7539 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7540 return 0;
7541
7542 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7543
7544 for (; i < nargs; ++i)
7545 {
7546 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7547 continue;
7548
7549 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7550
7551 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7552 outer_args))
7553 return 0;
7554 }
7555 }
7556
7557 return 1;
7558 }
7559
7560 /* Verifies that the deduced template arguments (in TARGS) for the
7561 template template parameters (in TPARMS) represent valid bindings,
7562 by comparing the template parameter list of each template argument
7563 to the template parameter list of its corresponding template
7564 template parameter, in accordance with DR150. This
7565 routine can only be called after all template arguments have been
7566 deduced. It will return TRUE if all of the template template
7567 parameter bindings are okay, FALSE otherwise. */
7568 bool
7569 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7570 {
7571 int i, ntparms = TREE_VEC_LENGTH (tparms);
7572 bool ret = true;
7573
7574 /* We're dealing with template parms in this process. */
7575 ++processing_template_decl;
7576
7577 targs = INNERMOST_TEMPLATE_ARGS (targs);
7578
7579 for (i = 0; i < ntparms; ++i)
7580 {
7581 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7582 tree targ = TREE_VEC_ELT (targs, i);
7583
7584 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7585 {
7586 tree packed_args = NULL_TREE;
7587 int idx, len = 1;
7588
7589 if (ARGUMENT_PACK_P (targ))
7590 {
7591 /* Look inside the argument pack. */
7592 packed_args = ARGUMENT_PACK_ARGS (targ);
7593 len = TREE_VEC_LENGTH (packed_args);
7594 }
7595
7596 for (idx = 0; idx < len; ++idx)
7597 {
7598 tree targ_parms = NULL_TREE;
7599
7600 if (packed_args)
7601 /* Extract the next argument from the argument
7602 pack. */
7603 targ = TREE_VEC_ELT (packed_args, idx);
7604
7605 if (PACK_EXPANSION_P (targ))
7606 /* Look at the pattern of the pack expansion. */
7607 targ = PACK_EXPANSION_PATTERN (targ);
7608
7609 /* Extract the template parameters from the template
7610 argument. */
7611 if (TREE_CODE (targ) == TEMPLATE_DECL)
7612 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7613 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7614 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7615
7616 /* Verify that we can coerce the template template
7617 parameters from the template argument to the template
7618 parameter. This requires an exact match. */
7619 if (targ_parms
7620 && !coerce_template_template_parms
7621 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7622 targ_parms,
7623 tf_none,
7624 tparm,
7625 targs))
7626 {
7627 ret = false;
7628 goto out;
7629 }
7630 }
7631 }
7632 }
7633
7634 out:
7635
7636 --processing_template_decl;
7637 return ret;
7638 }
7639
7640 /* Since type attributes aren't mangled, we need to strip them from
7641 template type arguments. */
7642
7643 static tree
7644 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7645 {
7646 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7647 return arg;
7648 bool removed_attributes = false;
7649 tree canon = strip_typedefs (arg, &removed_attributes);
7650 if (removed_attributes
7651 && (complain & tf_warning))
7652 warning (OPT_Wignored_attributes,
7653 "ignoring attributes on template argument %qT", arg);
7654 return canon;
7655 }
7656
7657 /* And from inside dependent non-type arguments like sizeof(Type). */
7658
7659 static tree
7660 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7661 {
7662 if (!arg || arg == error_mark_node)
7663 return arg;
7664 bool removed_attributes = false;
7665 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7666 if (removed_attributes
7667 && (complain & tf_warning))
7668 warning (OPT_Wignored_attributes,
7669 "ignoring attributes in template argument %qE", arg);
7670 return canon;
7671 }
7672
7673 // A template declaration can be substituted for a constrained
7674 // template template parameter only when the argument is more
7675 // constrained than the parameter.
7676 static bool
7677 is_compatible_template_arg (tree parm, tree arg)
7678 {
7679 tree parm_cons = get_constraints (parm);
7680
7681 /* For now, allow constrained template template arguments
7682 and unconstrained template template parameters. */
7683 if (parm_cons == NULL_TREE)
7684 return true;
7685
7686 tree arg_cons = get_constraints (arg);
7687
7688 // If the template parameter is constrained, we need to rewrite its
7689 // constraints in terms of the ARG's template parameters. This ensures
7690 // that all of the template parameter types will have the same depth.
7691 //
7692 // Note that this is only valid when coerce_template_template_parm is
7693 // true for the innermost template parameters of PARM and ARG. In other
7694 // words, because coercion is successful, this conversion will be valid.
7695 if (parm_cons)
7696 {
7697 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7698 parm_cons = tsubst_constraint_info (parm_cons,
7699 INNERMOST_TEMPLATE_ARGS (args),
7700 tf_none, NULL_TREE);
7701 if (parm_cons == error_mark_node)
7702 return false;
7703 }
7704
7705 return subsumes (parm_cons, arg_cons);
7706 }
7707
7708 // Convert a placeholder argument into a binding to the original
7709 // parameter. The original parameter is saved as the TREE_TYPE of
7710 // ARG.
7711 static inline tree
7712 convert_wildcard_argument (tree parm, tree arg)
7713 {
7714 TREE_TYPE (arg) = parm;
7715 return arg;
7716 }
7717
7718 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7719 because one of them is dependent. But we need to represent the
7720 conversion for the benefit of cp_tree_equal. */
7721
7722 static tree
7723 maybe_convert_nontype_argument (tree type, tree arg)
7724 {
7725 /* Auto parms get no conversion. */
7726 if (type_uses_auto (type))
7727 return arg;
7728 /* We don't need or want to add this conversion now if we're going to use the
7729 argument for deduction. */
7730 if (value_dependent_expression_p (arg))
7731 return arg;
7732
7733 type = cv_unqualified (type);
7734 tree argtype = TREE_TYPE (arg);
7735 if (same_type_p (type, argtype))
7736 return arg;
7737
7738 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7739 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7740 return arg;
7741 }
7742
7743 /* Convert the indicated template ARG as necessary to match the
7744 indicated template PARM. Returns the converted ARG, or
7745 error_mark_node if the conversion was unsuccessful. Error and
7746 warning messages are issued under control of COMPLAIN. This
7747 conversion is for the Ith parameter in the parameter list. ARGS is
7748 the full set of template arguments deduced so far. */
7749
7750 static tree
7751 convert_template_argument (tree parm,
7752 tree arg,
7753 tree args,
7754 tsubst_flags_t complain,
7755 int i,
7756 tree in_decl)
7757 {
7758 tree orig_arg;
7759 tree val;
7760 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7761
7762 if (parm == error_mark_node)
7763 return error_mark_node;
7764
7765 /* Trivially convert placeholders. */
7766 if (TREE_CODE (arg) == WILDCARD_DECL)
7767 return convert_wildcard_argument (parm, arg);
7768
7769 if (arg == any_targ_node)
7770 return arg;
7771
7772 if (TREE_CODE (arg) == TREE_LIST
7773 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7774 {
7775 /* The template argument was the name of some
7776 member function. That's usually
7777 invalid, but static members are OK. In any
7778 case, grab the underlying fields/functions
7779 and issue an error later if required. */
7780 orig_arg = TREE_VALUE (arg);
7781 TREE_TYPE (arg) = unknown_type_node;
7782 }
7783
7784 orig_arg = arg;
7785
7786 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7787 requires_type = (TREE_CODE (parm) == TYPE_DECL
7788 || requires_tmpl_type);
7789
7790 /* When determining whether an argument pack expansion is a template,
7791 look at the pattern. */
7792 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7793 arg = PACK_EXPANSION_PATTERN (arg);
7794
7795 /* Deal with an injected-class-name used as a template template arg. */
7796 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7797 {
7798 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7799 if (TREE_CODE (t) == TEMPLATE_DECL)
7800 {
7801 if (cxx_dialect >= cxx11)
7802 /* OK under DR 1004. */;
7803 else if (complain & tf_warning_or_error)
7804 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7805 " used as template template argument", TYPE_NAME (arg));
7806 else if (flag_pedantic_errors)
7807 t = arg;
7808
7809 arg = t;
7810 }
7811 }
7812
7813 is_tmpl_type =
7814 ((TREE_CODE (arg) == TEMPLATE_DECL
7815 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7816 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7817 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7818 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7819
7820 if (is_tmpl_type
7821 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7822 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7823 arg = TYPE_STUB_DECL (arg);
7824
7825 is_type = TYPE_P (arg) || is_tmpl_type;
7826
7827 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7828 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7829 {
7830 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7831 {
7832 if (complain & tf_error)
7833 error ("invalid use of destructor %qE as a type", orig_arg);
7834 return error_mark_node;
7835 }
7836
7837 permerror (input_location,
7838 "to refer to a type member of a template parameter, "
7839 "use %<typename %E%>", orig_arg);
7840
7841 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7842 TREE_OPERAND (arg, 1),
7843 typename_type,
7844 complain);
7845 arg = orig_arg;
7846 is_type = 1;
7847 }
7848 if (is_type != requires_type)
7849 {
7850 if (in_decl)
7851 {
7852 if (complain & tf_error)
7853 {
7854 error ("type/value mismatch at argument %d in template "
7855 "parameter list for %qD",
7856 i + 1, in_decl);
7857 if (is_type)
7858 inform (input_location,
7859 " expected a constant of type %qT, got %qT",
7860 TREE_TYPE (parm),
7861 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7862 else if (requires_tmpl_type)
7863 inform (input_location,
7864 " expected a class template, got %qE", orig_arg);
7865 else
7866 inform (input_location,
7867 " expected a type, got %qE", orig_arg);
7868 }
7869 }
7870 return error_mark_node;
7871 }
7872 if (is_tmpl_type ^ requires_tmpl_type)
7873 {
7874 if (in_decl && (complain & tf_error))
7875 {
7876 error ("type/value mismatch at argument %d in template "
7877 "parameter list for %qD",
7878 i + 1, in_decl);
7879 if (is_tmpl_type)
7880 inform (input_location,
7881 " expected a type, got %qT", DECL_NAME (arg));
7882 else
7883 inform (input_location,
7884 " expected a class template, got %qT", orig_arg);
7885 }
7886 return error_mark_node;
7887 }
7888
7889 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7890 /* We already did the appropriate conversion when packing args. */
7891 val = orig_arg;
7892 else if (is_type)
7893 {
7894 if (requires_tmpl_type)
7895 {
7896 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7897 /* The number of argument required is not known yet.
7898 Just accept it for now. */
7899 val = orig_arg;
7900 else
7901 {
7902 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7903 tree argparm;
7904
7905 /* Strip alias templates that are equivalent to another
7906 template. */
7907 arg = get_underlying_template (arg);
7908 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7909
7910 if (coerce_template_template_parms (parmparm, argparm,
7911 complain, in_decl,
7912 args))
7913 {
7914 val = arg;
7915
7916 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7917 TEMPLATE_DECL. */
7918 if (val != error_mark_node)
7919 {
7920 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7921 val = TREE_TYPE (val);
7922 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7923 val = make_pack_expansion (val, complain);
7924 }
7925 }
7926 else
7927 {
7928 if (in_decl && (complain & tf_error))
7929 {
7930 error ("type/value mismatch at argument %d in "
7931 "template parameter list for %qD",
7932 i + 1, in_decl);
7933 inform (input_location,
7934 " expected a template of type %qD, got %qT",
7935 parm, orig_arg);
7936 }
7937
7938 val = error_mark_node;
7939 }
7940
7941 // Check that the constraints are compatible before allowing the
7942 // substitution.
7943 if (val != error_mark_node)
7944 if (!is_compatible_template_arg (parm, arg))
7945 {
7946 if (in_decl && (complain & tf_error))
7947 {
7948 error ("constraint mismatch at argument %d in "
7949 "template parameter list for %qD",
7950 i + 1, in_decl);
7951 inform (input_location, " expected %qD but got %qD",
7952 parm, arg);
7953 }
7954 val = error_mark_node;
7955 }
7956 }
7957 }
7958 else
7959 val = orig_arg;
7960 /* We only form one instance of each template specialization.
7961 Therefore, if we use a non-canonical variant (i.e., a
7962 typedef), any future messages referring to the type will use
7963 the typedef, which is confusing if those future uses do not
7964 themselves also use the typedef. */
7965 if (TYPE_P (val))
7966 val = canonicalize_type_argument (val, complain);
7967 }
7968 else
7969 {
7970 tree t = TREE_TYPE (parm);
7971
7972 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
7973 > TMPL_ARGS_DEPTH (args))
7974 /* We don't have enough levels of args to do any substitution. This
7975 can happen in the context of -fnew-ttp-matching. */;
7976 else if (tree a = type_uses_auto (t))
7977 {
7978 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7979 if (t == error_mark_node)
7980 return error_mark_node;
7981 }
7982 else
7983 t = tsubst (t, args, complain, in_decl);
7984
7985 if (invalid_nontype_parm_type_p (t, complain))
7986 return error_mark_node;
7987
7988 if (!type_dependent_expression_p (orig_arg)
7989 && !uses_template_parms (t))
7990 /* We used to call digest_init here. However, digest_init
7991 will report errors, which we don't want when complain
7992 is zero. More importantly, digest_init will try too
7993 hard to convert things: for example, `0' should not be
7994 converted to pointer type at this point according to
7995 the standard. Accepting this is not merely an
7996 extension, since deciding whether or not these
7997 conversions can occur is part of determining which
7998 function template to call, or whether a given explicit
7999 argument specification is valid. */
8000 val = convert_nontype_argument (t, orig_arg, complain);
8001 else
8002 {
8003 val = canonicalize_expr_argument (orig_arg, complain);
8004 val = maybe_convert_nontype_argument (t, val);
8005 }
8006
8007
8008 if (val == NULL_TREE)
8009 val = error_mark_node;
8010 else if (val == error_mark_node && (complain & tf_error))
8011 error ("could not convert template argument %qE from %qT to %qT",
8012 orig_arg, TREE_TYPE (orig_arg), t);
8013
8014 if (INDIRECT_REF_P (val))
8015 {
8016 /* Reject template arguments that are references to built-in
8017 functions with no library fallbacks. */
8018 const_tree inner = TREE_OPERAND (val, 0);
8019 const_tree innertype = TREE_TYPE (inner);
8020 if (innertype
8021 && TREE_CODE (innertype) == REFERENCE_TYPE
8022 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8023 && TREE_OPERAND_LENGTH (inner) > 0
8024 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8025 return error_mark_node;
8026 }
8027
8028 if (TREE_CODE (val) == SCOPE_REF)
8029 {
8030 /* Strip typedefs from the SCOPE_REF. */
8031 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8032 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8033 complain);
8034 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8035 QUALIFIED_NAME_IS_TEMPLATE (val));
8036 }
8037 }
8038
8039 return val;
8040 }
8041
8042 /* Coerces the remaining template arguments in INNER_ARGS (from
8043 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8044 Returns the coerced argument pack. PARM_IDX is the position of this
8045 parameter in the template parameter list. ARGS is the original
8046 template argument list. */
8047 static tree
8048 coerce_template_parameter_pack (tree parms,
8049 int parm_idx,
8050 tree args,
8051 tree inner_args,
8052 int arg_idx,
8053 tree new_args,
8054 int* lost,
8055 tree in_decl,
8056 tsubst_flags_t complain)
8057 {
8058 tree parm = TREE_VEC_ELT (parms, parm_idx);
8059 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8060 tree packed_args;
8061 tree argument_pack;
8062 tree packed_parms = NULL_TREE;
8063
8064 if (arg_idx > nargs)
8065 arg_idx = nargs;
8066
8067 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8068 {
8069 /* When the template parameter is a non-type template parameter pack
8070 or template template parameter pack whose type or template
8071 parameters use parameter packs, we know exactly how many arguments
8072 we are looking for. Build a vector of the instantiated decls for
8073 these template parameters in PACKED_PARMS. */
8074 /* We can't use make_pack_expansion here because it would interpret a
8075 _DECL as a use rather than a declaration. */
8076 tree decl = TREE_VALUE (parm);
8077 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8078 SET_PACK_EXPANSION_PATTERN (exp, decl);
8079 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8080 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8081
8082 TREE_VEC_LENGTH (args)--;
8083 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8084 TREE_VEC_LENGTH (args)++;
8085
8086 if (packed_parms == error_mark_node)
8087 return error_mark_node;
8088
8089 /* If we're doing a partial instantiation of a member template,
8090 verify that all of the types used for the non-type
8091 template parameter pack are, in fact, valid for non-type
8092 template parameters. */
8093 if (arg_idx < nargs
8094 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8095 {
8096 int j, len = TREE_VEC_LENGTH (packed_parms);
8097 for (j = 0; j < len; ++j)
8098 {
8099 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8100 if (invalid_nontype_parm_type_p (t, complain))
8101 return error_mark_node;
8102 }
8103 /* We don't know how many args we have yet, just
8104 use the unconverted ones for now. */
8105 return NULL_TREE;
8106 }
8107
8108 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8109 }
8110 /* Check if we have a placeholder pack, which indicates we're
8111 in the context of a introduction list. In that case we want
8112 to match this pack to the single placeholder. */
8113 else if (arg_idx < nargs
8114 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8115 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8116 {
8117 nargs = arg_idx + 1;
8118 packed_args = make_tree_vec (1);
8119 }
8120 else
8121 packed_args = make_tree_vec (nargs - arg_idx);
8122
8123 /* Convert the remaining arguments, which will be a part of the
8124 parameter pack "parm". */
8125 int first_pack_arg = arg_idx;
8126 for (; arg_idx < nargs; ++arg_idx)
8127 {
8128 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8129 tree actual_parm = TREE_VALUE (parm);
8130 int pack_idx = arg_idx - first_pack_arg;
8131
8132 if (packed_parms)
8133 {
8134 /* Once we've packed as many args as we have types, stop. */
8135 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8136 break;
8137 else if (PACK_EXPANSION_P (arg))
8138 /* We don't know how many args we have yet, just
8139 use the unconverted ones for now. */
8140 return NULL_TREE;
8141 else
8142 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8143 }
8144
8145 if (arg == error_mark_node)
8146 {
8147 if (complain & tf_error)
8148 error ("template argument %d is invalid", arg_idx + 1);
8149 }
8150 else
8151 arg = convert_template_argument (actual_parm,
8152 arg, new_args, complain, parm_idx,
8153 in_decl);
8154 if (arg == error_mark_node)
8155 (*lost)++;
8156 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8157 }
8158
8159 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8160 && TREE_VEC_LENGTH (packed_args) > 0)
8161 {
8162 if (complain & tf_error)
8163 error ("wrong number of template arguments (%d, should be %d)",
8164 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8165 return error_mark_node;
8166 }
8167
8168 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8169 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8170 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8171 else
8172 {
8173 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8174 TREE_CONSTANT (argument_pack) = 1;
8175 }
8176
8177 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8178 if (CHECKING_P)
8179 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8180 TREE_VEC_LENGTH (packed_args));
8181 return argument_pack;
8182 }
8183
8184 /* Returns the number of pack expansions in the template argument vector
8185 ARGS. */
8186
8187 static int
8188 pack_expansion_args_count (tree args)
8189 {
8190 int i;
8191 int count = 0;
8192 if (args)
8193 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8194 {
8195 tree elt = TREE_VEC_ELT (args, i);
8196 if (elt && PACK_EXPANSION_P (elt))
8197 ++count;
8198 }
8199 return count;
8200 }
8201
8202 /* Convert all template arguments to their appropriate types, and
8203 return a vector containing the innermost resulting template
8204 arguments. If any error occurs, return error_mark_node. Error and
8205 warning messages are issued under control of COMPLAIN.
8206
8207 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8208 for arguments not specified in ARGS. Otherwise, if
8209 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8210 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8211 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8212 ARGS. */
8213
8214 static tree
8215 coerce_template_parms (tree parms,
8216 tree args,
8217 tree in_decl,
8218 tsubst_flags_t complain,
8219 bool require_all_args,
8220 bool use_default_args)
8221 {
8222 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8223 tree orig_inner_args;
8224 tree inner_args;
8225 tree new_args;
8226 tree new_inner_args;
8227 int saved_unevaluated_operand;
8228 int saved_inhibit_evaluation_warnings;
8229
8230 /* When used as a boolean value, indicates whether this is a
8231 variadic template parameter list. Since it's an int, we can also
8232 subtract it from nparms to get the number of non-variadic
8233 parameters. */
8234 int variadic_p = 0;
8235 int variadic_args_p = 0;
8236 int post_variadic_parms = 0;
8237
8238 /* Likewise for parameters with default arguments. */
8239 int default_p = 0;
8240
8241 if (args == error_mark_node)
8242 return error_mark_node;
8243
8244 nparms = TREE_VEC_LENGTH (parms);
8245
8246 /* Determine if there are any parameter packs or default arguments. */
8247 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8248 {
8249 tree parm = TREE_VEC_ELT (parms, parm_idx);
8250 if (variadic_p)
8251 ++post_variadic_parms;
8252 if (template_parameter_pack_p (TREE_VALUE (parm)))
8253 ++variadic_p;
8254 if (TREE_PURPOSE (parm))
8255 ++default_p;
8256 }
8257
8258 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8259 /* If there are no parameters that follow a parameter pack, we need to
8260 expand any argument packs so that we can deduce a parameter pack from
8261 some non-packed args followed by an argument pack, as in variadic85.C.
8262 If there are such parameters, we need to leave argument packs intact
8263 so the arguments are assigned properly. This can happen when dealing
8264 with a nested class inside a partial specialization of a class
8265 template, as in variadic92.C, or when deducing a template parameter pack
8266 from a sub-declarator, as in variadic114.C. */
8267 if (!post_variadic_parms)
8268 inner_args = expand_template_argument_pack (inner_args);
8269
8270 /* Count any pack expansion args. */
8271 variadic_args_p = pack_expansion_args_count (inner_args);
8272
8273 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8274 if ((nargs - variadic_args_p > nparms && !variadic_p)
8275 || (nargs < nparms - variadic_p
8276 && require_all_args
8277 && !variadic_args_p
8278 && (!use_default_args
8279 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8280 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8281 {
8282 if (complain & tf_error)
8283 {
8284 if (variadic_p || default_p)
8285 {
8286 nparms -= variadic_p + default_p;
8287 error ("wrong number of template arguments "
8288 "(%d, should be at least %d)", nargs, nparms);
8289 }
8290 else
8291 error ("wrong number of template arguments "
8292 "(%d, should be %d)", nargs, nparms);
8293
8294 if (in_decl)
8295 inform (DECL_SOURCE_LOCATION (in_decl),
8296 "provided for %qD", in_decl);
8297 }
8298
8299 return error_mark_node;
8300 }
8301 /* We can't pass a pack expansion to a non-pack parameter of an alias
8302 template (DR 1430). */
8303 else if (in_decl
8304 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8305 || concept_template_p (in_decl))
8306 && variadic_args_p
8307 && nargs - variadic_args_p < nparms - variadic_p)
8308 {
8309 if (complain & tf_error)
8310 {
8311 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8312 {
8313 tree arg = TREE_VEC_ELT (inner_args, i);
8314 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8315
8316 if (PACK_EXPANSION_P (arg)
8317 && !template_parameter_pack_p (parm))
8318 {
8319 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8320 error_at (location_of (arg),
8321 "pack expansion argument for non-pack parameter "
8322 "%qD of alias template %qD", parm, in_decl);
8323 else
8324 error_at (location_of (arg),
8325 "pack expansion argument for non-pack parameter "
8326 "%qD of concept %qD", parm, in_decl);
8327 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8328 goto found;
8329 }
8330 }
8331 gcc_unreachable ();
8332 found:;
8333 }
8334 return error_mark_node;
8335 }
8336
8337 /* We need to evaluate the template arguments, even though this
8338 template-id may be nested within a "sizeof". */
8339 saved_unevaluated_operand = cp_unevaluated_operand;
8340 cp_unevaluated_operand = 0;
8341 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8342 c_inhibit_evaluation_warnings = 0;
8343 new_inner_args = make_tree_vec (nparms);
8344 new_args = add_outermost_template_args (args, new_inner_args);
8345 int pack_adjust = 0;
8346 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8347 {
8348 tree arg;
8349 tree parm;
8350
8351 /* Get the Ith template parameter. */
8352 parm = TREE_VEC_ELT (parms, parm_idx);
8353
8354 if (parm == error_mark_node)
8355 {
8356 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8357 continue;
8358 }
8359
8360 /* Calculate the next argument. */
8361 if (arg_idx < nargs)
8362 arg = TREE_VEC_ELT (inner_args, arg_idx);
8363 else
8364 arg = NULL_TREE;
8365
8366 if (template_parameter_pack_p (TREE_VALUE (parm))
8367 && !(arg && ARGUMENT_PACK_P (arg)))
8368 {
8369 /* Some arguments will be placed in the
8370 template parameter pack PARM. */
8371 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8372 inner_args, arg_idx,
8373 new_args, &lost,
8374 in_decl, complain);
8375
8376 if (arg == NULL_TREE)
8377 {
8378 /* We don't know how many args we have yet, just use the
8379 unconverted (and still packed) ones for now. */
8380 new_inner_args = orig_inner_args;
8381 arg_idx = nargs;
8382 break;
8383 }
8384
8385 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8386
8387 /* Store this argument. */
8388 if (arg == error_mark_node)
8389 {
8390 lost++;
8391 /* We are done with all of the arguments. */
8392 arg_idx = nargs;
8393 }
8394 else
8395 {
8396 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8397 arg_idx += pack_adjust;
8398 }
8399
8400 continue;
8401 }
8402 else if (arg)
8403 {
8404 if (PACK_EXPANSION_P (arg))
8405 {
8406 /* "If every valid specialization of a variadic template
8407 requires an empty template parameter pack, the template is
8408 ill-formed, no diagnostic required." So check that the
8409 pattern works with this parameter. */
8410 tree pattern = PACK_EXPANSION_PATTERN (arg);
8411 tree conv = convert_template_argument (TREE_VALUE (parm),
8412 pattern, new_args,
8413 complain, parm_idx,
8414 in_decl);
8415 if (conv == error_mark_node)
8416 {
8417 if (complain & tf_error)
8418 inform (input_location, "so any instantiation with a "
8419 "non-empty parameter pack would be ill-formed");
8420 ++lost;
8421 }
8422 else if (TYPE_P (conv) && !TYPE_P (pattern))
8423 /* Recover from missing typename. */
8424 TREE_VEC_ELT (inner_args, arg_idx)
8425 = make_pack_expansion (conv, complain);
8426
8427 /* We don't know how many args we have yet, just
8428 use the unconverted ones for now. */
8429 new_inner_args = inner_args;
8430 arg_idx = nargs;
8431 break;
8432 }
8433 }
8434 else if (require_all_args)
8435 {
8436 /* There must be a default arg in this case. */
8437 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8438 complain, in_decl);
8439 /* The position of the first default template argument,
8440 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8441 Record that. */
8442 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8443 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8444 arg_idx - pack_adjust);
8445 }
8446 else
8447 break;
8448
8449 if (arg == error_mark_node)
8450 {
8451 if (complain & tf_error)
8452 error ("template argument %d is invalid", arg_idx + 1);
8453 }
8454 else if (!arg)
8455 /* This only occurs if there was an error in the template
8456 parameter list itself (which we would already have
8457 reported) that we are trying to recover from, e.g., a class
8458 template with a parameter list such as
8459 template<typename..., typename>. */
8460 ++lost;
8461 else
8462 arg = convert_template_argument (TREE_VALUE (parm),
8463 arg, new_args, complain,
8464 parm_idx, in_decl);
8465
8466 if (arg == error_mark_node)
8467 lost++;
8468 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8469 }
8470 cp_unevaluated_operand = saved_unevaluated_operand;
8471 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8472
8473 if (variadic_p && arg_idx < nargs)
8474 {
8475 if (complain & tf_error)
8476 {
8477 error ("wrong number of template arguments "
8478 "(%d, should be %d)", nargs, arg_idx);
8479 if (in_decl)
8480 error ("provided for %q+D", in_decl);
8481 }
8482 return error_mark_node;
8483 }
8484
8485 if (lost)
8486 return error_mark_node;
8487
8488 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8489 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8490 TREE_VEC_LENGTH (new_inner_args));
8491
8492 return new_inner_args;
8493 }
8494
8495 /* Convert all template arguments to their appropriate types, and
8496 return a vector containing the innermost resulting template
8497 arguments. If any error occurs, return error_mark_node. Error and
8498 warning messages are not issued.
8499
8500 Note that no function argument deduction is performed, and default
8501 arguments are used to fill in unspecified arguments. */
8502 tree
8503 coerce_template_parms (tree parms, tree args, tree in_decl)
8504 {
8505 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8506 }
8507
8508 /* Convert all template arguments to their appropriate type, and
8509 instantiate default arguments as needed. This returns a vector
8510 containing the innermost resulting template arguments, or
8511 error_mark_node if unsuccessful. */
8512 tree
8513 coerce_template_parms (tree parms, tree args, tree in_decl,
8514 tsubst_flags_t complain)
8515 {
8516 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8517 }
8518
8519 /* Like coerce_template_parms. If PARMS represents all template
8520 parameters levels, this function returns a vector of vectors
8521 representing all the resulting argument levels. Note that in this
8522 case, only the innermost arguments are coerced because the
8523 outermost ones are supposed to have been coerced already.
8524
8525 Otherwise, if PARMS represents only (the innermost) vector of
8526 parameters, this function returns a vector containing just the
8527 innermost resulting arguments. */
8528
8529 static tree
8530 coerce_innermost_template_parms (tree parms,
8531 tree args,
8532 tree in_decl,
8533 tsubst_flags_t complain,
8534 bool require_all_args,
8535 bool use_default_args)
8536 {
8537 int parms_depth = TMPL_PARMS_DEPTH (parms);
8538 int args_depth = TMPL_ARGS_DEPTH (args);
8539 tree coerced_args;
8540
8541 if (parms_depth > 1)
8542 {
8543 coerced_args = make_tree_vec (parms_depth);
8544 tree level;
8545 int cur_depth;
8546
8547 for (level = parms, cur_depth = parms_depth;
8548 parms_depth > 0 && level != NULL_TREE;
8549 level = TREE_CHAIN (level), --cur_depth)
8550 {
8551 tree l;
8552 if (cur_depth == args_depth)
8553 l = coerce_template_parms (TREE_VALUE (level),
8554 args, in_decl, complain,
8555 require_all_args,
8556 use_default_args);
8557 else
8558 l = TMPL_ARGS_LEVEL (args, cur_depth);
8559
8560 if (l == error_mark_node)
8561 return error_mark_node;
8562
8563 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8564 }
8565 }
8566 else
8567 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8568 args, in_decl, complain,
8569 require_all_args,
8570 use_default_args);
8571 return coerced_args;
8572 }
8573
8574 /* Returns 1 if template args OT and NT are equivalent. */
8575
8576 int
8577 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8578 {
8579 if (nt == ot)
8580 return 1;
8581 if (nt == NULL_TREE || ot == NULL_TREE)
8582 return false;
8583 if (nt == any_targ_node || ot == any_targ_node)
8584 return true;
8585
8586 if (TREE_CODE (nt) == TREE_VEC)
8587 /* For member templates */
8588 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8589 else if (PACK_EXPANSION_P (ot))
8590 return (PACK_EXPANSION_P (nt)
8591 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8592 PACK_EXPANSION_PATTERN (nt))
8593 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8594 PACK_EXPANSION_EXTRA_ARGS (nt)));
8595 else if (ARGUMENT_PACK_P (ot))
8596 {
8597 int i, len;
8598 tree opack, npack;
8599
8600 if (!ARGUMENT_PACK_P (nt))
8601 return 0;
8602
8603 opack = ARGUMENT_PACK_ARGS (ot);
8604 npack = ARGUMENT_PACK_ARGS (nt);
8605 len = TREE_VEC_LENGTH (opack);
8606 if (TREE_VEC_LENGTH (npack) != len)
8607 return 0;
8608 for (i = 0; i < len; ++i)
8609 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8610 TREE_VEC_ELT (npack, i)))
8611 return 0;
8612 return 1;
8613 }
8614 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8615 gcc_unreachable ();
8616 else if (TYPE_P (nt))
8617 {
8618 if (!TYPE_P (ot))
8619 return false;
8620 /* Don't treat an alias template specialization with dependent
8621 arguments as equivalent to its underlying type when used as a
8622 template argument; we need them to be distinct so that we
8623 substitute into the specialization arguments at instantiation
8624 time. And aliases can't be equivalent without being ==, so
8625 we don't need to look any deeper.
8626
8627 During partial ordering, however, we need to treat them normally so
8628 that we can order uses of the same alias with different
8629 cv-qualification (79960). */
8630 if (!partial_order
8631 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8632 return false;
8633 else
8634 return same_type_p (ot, nt);
8635 }
8636 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8637 return 0;
8638 else
8639 {
8640 /* Try to treat a template non-type argument that has been converted
8641 to the parameter type as equivalent to one that hasn't yet. */
8642 for (enum tree_code code1 = TREE_CODE (ot);
8643 CONVERT_EXPR_CODE_P (code1)
8644 || code1 == NON_LVALUE_EXPR;
8645 code1 = TREE_CODE (ot))
8646 ot = TREE_OPERAND (ot, 0);
8647 for (enum tree_code code2 = TREE_CODE (nt);
8648 CONVERT_EXPR_CODE_P (code2)
8649 || code2 == NON_LVALUE_EXPR;
8650 code2 = TREE_CODE (nt))
8651 nt = TREE_OPERAND (nt, 0);
8652
8653 return cp_tree_equal (ot, nt);
8654 }
8655 }
8656
8657 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8658 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8659 NEWARG_PTR with the offending arguments if they are non-NULL. */
8660
8661 int
8662 comp_template_args (tree oldargs, tree newargs,
8663 tree *oldarg_ptr, tree *newarg_ptr,
8664 bool partial_order)
8665 {
8666 int i;
8667
8668 if (oldargs == newargs)
8669 return 1;
8670
8671 if (!oldargs || !newargs)
8672 return 0;
8673
8674 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8675 return 0;
8676
8677 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8678 {
8679 tree nt = TREE_VEC_ELT (newargs, i);
8680 tree ot = TREE_VEC_ELT (oldargs, i);
8681
8682 if (! template_args_equal (ot, nt, partial_order))
8683 {
8684 if (oldarg_ptr != NULL)
8685 *oldarg_ptr = ot;
8686 if (newarg_ptr != NULL)
8687 *newarg_ptr = nt;
8688 return 0;
8689 }
8690 }
8691 return 1;
8692 }
8693
8694 inline bool
8695 comp_template_args_porder (tree oargs, tree nargs)
8696 {
8697 return comp_template_args (oargs, nargs, NULL, NULL, true);
8698 }
8699
8700 static void
8701 add_pending_template (tree d)
8702 {
8703 tree ti = (TYPE_P (d)
8704 ? CLASSTYPE_TEMPLATE_INFO (d)
8705 : DECL_TEMPLATE_INFO (d));
8706 struct pending_template *pt;
8707 int level;
8708
8709 if (TI_PENDING_TEMPLATE_FLAG (ti))
8710 return;
8711
8712 /* We are called both from instantiate_decl, where we've already had a
8713 tinst_level pushed, and instantiate_template, where we haven't.
8714 Compensate. */
8715 level = !current_tinst_level || current_tinst_level->decl != d;
8716
8717 if (level)
8718 push_tinst_level (d);
8719
8720 pt = ggc_alloc<pending_template> ();
8721 pt->next = NULL;
8722 pt->tinst = current_tinst_level;
8723 if (last_pending_template)
8724 last_pending_template->next = pt;
8725 else
8726 pending_templates = pt;
8727
8728 last_pending_template = pt;
8729
8730 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8731
8732 if (level)
8733 pop_tinst_level ();
8734 }
8735
8736
8737 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8738 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8739 documentation for TEMPLATE_ID_EXPR. */
8740
8741 tree
8742 lookup_template_function (tree fns, tree arglist)
8743 {
8744 tree type;
8745
8746 if (fns == error_mark_node || arglist == error_mark_node)
8747 return error_mark_node;
8748
8749 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8750
8751 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8752 {
8753 error ("%q#D is not a function template", fns);
8754 return error_mark_node;
8755 }
8756
8757 if (BASELINK_P (fns))
8758 {
8759 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8760 unknown_type_node,
8761 BASELINK_FUNCTIONS (fns),
8762 arglist);
8763 return fns;
8764 }
8765
8766 type = TREE_TYPE (fns);
8767 if (TREE_CODE (fns) == OVERLOAD || !type)
8768 type = unknown_type_node;
8769
8770 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8771 }
8772
8773 /* Within the scope of a template class S<T>, the name S gets bound
8774 (in build_self_reference) to a TYPE_DECL for the class, not a
8775 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8776 or one of its enclosing classes, and that type is a template,
8777 return the associated TEMPLATE_DECL. Otherwise, the original
8778 DECL is returned.
8779
8780 Also handle the case when DECL is a TREE_LIST of ambiguous
8781 injected-class-names from different bases. */
8782
8783 tree
8784 maybe_get_template_decl_from_type_decl (tree decl)
8785 {
8786 if (decl == NULL_TREE)
8787 return decl;
8788
8789 /* DR 176: A lookup that finds an injected-class-name (10.2
8790 [class.member.lookup]) can result in an ambiguity in certain cases
8791 (for example, if it is found in more than one base class). If all of
8792 the injected-class-names that are found refer to specializations of
8793 the same class template, and if the name is followed by a
8794 template-argument-list, the reference refers to the class template
8795 itself and not a specialization thereof, and is not ambiguous. */
8796 if (TREE_CODE (decl) == TREE_LIST)
8797 {
8798 tree t, tmpl = NULL_TREE;
8799 for (t = decl; t; t = TREE_CHAIN (t))
8800 {
8801 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8802 if (!tmpl)
8803 tmpl = elt;
8804 else if (tmpl != elt)
8805 break;
8806 }
8807 if (tmpl && t == NULL_TREE)
8808 return tmpl;
8809 else
8810 return decl;
8811 }
8812
8813 return (decl != NULL_TREE
8814 && DECL_SELF_REFERENCE_P (decl)
8815 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8816 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8817 }
8818
8819 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8820 parameters, find the desired type.
8821
8822 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8823
8824 IN_DECL, if non-NULL, is the template declaration we are trying to
8825 instantiate.
8826
8827 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8828 the class we are looking up.
8829
8830 Issue error and warning messages under control of COMPLAIN.
8831
8832 If the template class is really a local class in a template
8833 function, then the FUNCTION_CONTEXT is the function in which it is
8834 being instantiated.
8835
8836 ??? Note that this function is currently called *twice* for each
8837 template-id: the first time from the parser, while creating the
8838 incomplete type (finish_template_type), and the second type during the
8839 real instantiation (instantiate_template_class). This is surely something
8840 that we want to avoid. It also causes some problems with argument
8841 coercion (see convert_nontype_argument for more information on this). */
8842
8843 static tree
8844 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8845 int entering_scope, tsubst_flags_t complain)
8846 {
8847 tree templ = NULL_TREE, parmlist;
8848 tree t;
8849 spec_entry **slot;
8850 spec_entry *entry;
8851 spec_entry elt;
8852 hashval_t hash;
8853
8854 if (identifier_p (d1))
8855 {
8856 tree value = innermost_non_namespace_value (d1);
8857 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8858 templ = value;
8859 else
8860 {
8861 if (context)
8862 push_decl_namespace (context);
8863 templ = lookup_name (d1);
8864 templ = maybe_get_template_decl_from_type_decl (templ);
8865 if (context)
8866 pop_decl_namespace ();
8867 }
8868 if (templ)
8869 context = DECL_CONTEXT (templ);
8870 }
8871 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8872 {
8873 tree type = TREE_TYPE (d1);
8874
8875 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8876 an implicit typename for the second A. Deal with it. */
8877 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8878 type = TREE_TYPE (type);
8879
8880 if (CLASSTYPE_TEMPLATE_INFO (type))
8881 {
8882 templ = CLASSTYPE_TI_TEMPLATE (type);
8883 d1 = DECL_NAME (templ);
8884 }
8885 }
8886 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8887 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8888 {
8889 templ = TYPE_TI_TEMPLATE (d1);
8890 d1 = DECL_NAME (templ);
8891 }
8892 else if (DECL_TYPE_TEMPLATE_P (d1))
8893 {
8894 templ = d1;
8895 d1 = DECL_NAME (templ);
8896 context = DECL_CONTEXT (templ);
8897 }
8898 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8899 {
8900 templ = d1;
8901 d1 = DECL_NAME (templ);
8902 }
8903
8904 /* Issue an error message if we didn't find a template. */
8905 if (! templ)
8906 {
8907 if (complain & tf_error)
8908 error ("%qT is not a template", d1);
8909 return error_mark_node;
8910 }
8911
8912 if (TREE_CODE (templ) != TEMPLATE_DECL
8913 /* Make sure it's a user visible template, if it was named by
8914 the user. */
8915 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8916 && !PRIMARY_TEMPLATE_P (templ)))
8917 {
8918 if (complain & tf_error)
8919 {
8920 error ("non-template type %qT used as a template", d1);
8921 if (in_decl)
8922 error ("for template declaration %q+D", in_decl);
8923 }
8924 return error_mark_node;
8925 }
8926
8927 complain &= ~tf_user;
8928
8929 /* An alias that just changes the name of a template is equivalent to the
8930 other template, so if any of the arguments are pack expansions, strip
8931 the alias to avoid problems with a pack expansion passed to a non-pack
8932 alias template parameter (DR 1430). */
8933 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8934 templ = get_underlying_template (templ);
8935
8936 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8937 {
8938 tree parm;
8939 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8940 if (arglist2 == error_mark_node
8941 || (!uses_template_parms (arglist2)
8942 && check_instantiated_args (templ, arglist2, complain)))
8943 return error_mark_node;
8944
8945 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8946 return parm;
8947 }
8948 else
8949 {
8950 tree template_type = TREE_TYPE (templ);
8951 tree gen_tmpl;
8952 tree type_decl;
8953 tree found = NULL_TREE;
8954 int arg_depth;
8955 int parm_depth;
8956 int is_dependent_type;
8957 int use_partial_inst_tmpl = false;
8958
8959 if (template_type == error_mark_node)
8960 /* An error occurred while building the template TEMPL, and a
8961 diagnostic has most certainly been emitted for that
8962 already. Let's propagate that error. */
8963 return error_mark_node;
8964
8965 gen_tmpl = most_general_template (templ);
8966 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8967 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8968 arg_depth = TMPL_ARGS_DEPTH (arglist);
8969
8970 if (arg_depth == 1 && parm_depth > 1)
8971 {
8972 /* We've been given an incomplete set of template arguments.
8973 For example, given:
8974
8975 template <class T> struct S1 {
8976 template <class U> struct S2 {};
8977 template <class U> struct S2<U*> {};
8978 };
8979
8980 we will be called with an ARGLIST of `U*', but the
8981 TEMPLATE will be `template <class T> template
8982 <class U> struct S1<T>::S2'. We must fill in the missing
8983 arguments. */
8984 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8985 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8986 arg_depth = TMPL_ARGS_DEPTH (arglist);
8987 }
8988
8989 /* Now we should have enough arguments. */
8990 gcc_assert (parm_depth == arg_depth);
8991
8992 /* From here on, we're only interested in the most general
8993 template. */
8994
8995 /* Calculate the BOUND_ARGS. These will be the args that are
8996 actually tsubst'd into the definition to create the
8997 instantiation. */
8998 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8999 complain,
9000 /*require_all_args=*/true,
9001 /*use_default_args=*/true);
9002
9003 if (arglist == error_mark_node)
9004 /* We were unable to bind the arguments. */
9005 return error_mark_node;
9006
9007 /* In the scope of a template class, explicit references to the
9008 template class refer to the type of the template, not any
9009 instantiation of it. For example, in:
9010
9011 template <class T> class C { void f(C<T>); }
9012
9013 the `C<T>' is just the same as `C'. Outside of the
9014 class, however, such a reference is an instantiation. */
9015 if (entering_scope
9016 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9017 || currently_open_class (template_type))
9018 {
9019 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9020
9021 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9022 return template_type;
9023 }
9024
9025 /* If we already have this specialization, return it. */
9026 elt.tmpl = gen_tmpl;
9027 elt.args = arglist;
9028 elt.spec = NULL_TREE;
9029 hash = spec_hasher::hash (&elt);
9030 entry = type_specializations->find_with_hash (&elt, hash);
9031
9032 if (entry)
9033 return entry->spec;
9034
9035 /* If the the template's constraints are not satisfied,
9036 then we cannot form a valid type.
9037
9038 Note that the check is deferred until after the hash
9039 lookup. This prevents redundant checks on previously
9040 instantiated specializations. */
9041 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9042 {
9043 if (complain & tf_error)
9044 {
9045 error ("template constraint failure");
9046 diagnose_constraints (input_location, gen_tmpl, arglist);
9047 }
9048 return error_mark_node;
9049 }
9050
9051 is_dependent_type = uses_template_parms (arglist);
9052
9053 /* If the deduced arguments are invalid, then the binding
9054 failed. */
9055 if (!is_dependent_type
9056 && check_instantiated_args (gen_tmpl,
9057 INNERMOST_TEMPLATE_ARGS (arglist),
9058 complain))
9059 return error_mark_node;
9060
9061 if (!is_dependent_type
9062 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9063 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9064 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9065 {
9066 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9067 DECL_NAME (gen_tmpl),
9068 /*tag_scope=*/ts_global);
9069 return found;
9070 }
9071
9072 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
9073 complain, in_decl);
9074 if (context == error_mark_node)
9075 return error_mark_node;
9076
9077 if (!context)
9078 context = global_namespace;
9079
9080 /* Create the type. */
9081 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9082 {
9083 /* The user referred to a specialization of an alias
9084 template represented by GEN_TMPL.
9085
9086 [temp.alias]/2 says:
9087
9088 When a template-id refers to the specialization of an
9089 alias template, it is equivalent to the associated
9090 type obtained by substitution of its
9091 template-arguments for the template-parameters in the
9092 type-id of the alias template. */
9093
9094 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9095 /* Note that the call above (by indirectly calling
9096 register_specialization in tsubst_decl) registers the
9097 TYPE_DECL representing the specialization of the alias
9098 template. So next time someone substitutes ARGLIST for
9099 the template parms into the alias template (GEN_TMPL),
9100 she'll get that TYPE_DECL back. */
9101
9102 if (t == error_mark_node)
9103 return t;
9104 }
9105 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9106 {
9107 if (!is_dependent_type)
9108 {
9109 set_current_access_from_decl (TYPE_NAME (template_type));
9110 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9111 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9112 arglist, complain, in_decl),
9113 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9114 arglist, complain, in_decl),
9115 SCOPED_ENUM_P (template_type), NULL);
9116
9117 if (t == error_mark_node)
9118 return t;
9119 }
9120 else
9121 {
9122 /* We don't want to call start_enum for this type, since
9123 the values for the enumeration constants may involve
9124 template parameters. And, no one should be interested
9125 in the enumeration constants for such a type. */
9126 t = cxx_make_type (ENUMERAL_TYPE);
9127 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9128 }
9129 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9130 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9131 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9132 }
9133 else if (CLASS_TYPE_P (template_type))
9134 {
9135 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9136 instantiated here. */
9137 gcc_assert (!LAMBDA_TYPE_P (template_type));
9138
9139 t = make_class_type (TREE_CODE (template_type));
9140 CLASSTYPE_DECLARED_CLASS (t)
9141 = CLASSTYPE_DECLARED_CLASS (template_type);
9142 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9143
9144 /* A local class. Make sure the decl gets registered properly. */
9145 if (context == current_function_decl)
9146 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
9147
9148 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9149 /* This instantiation is another name for the primary
9150 template type. Set the TYPE_CANONICAL field
9151 appropriately. */
9152 TYPE_CANONICAL (t) = template_type;
9153 else if (any_template_arguments_need_structural_equality_p (arglist))
9154 /* Some of the template arguments require structural
9155 equality testing, so this template class requires
9156 structural equality testing. */
9157 SET_TYPE_STRUCTURAL_EQUALITY (t);
9158 }
9159 else
9160 gcc_unreachable ();
9161
9162 /* If we called start_enum or pushtag above, this information
9163 will already be set up. */
9164 if (!TYPE_NAME (t))
9165 {
9166 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9167
9168 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9169 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9170 DECL_SOURCE_LOCATION (type_decl)
9171 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9172 }
9173 else
9174 type_decl = TYPE_NAME (t);
9175
9176 if (CLASS_TYPE_P (template_type))
9177 {
9178 TREE_PRIVATE (type_decl)
9179 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9180 TREE_PROTECTED (type_decl)
9181 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9182 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9183 {
9184 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9185 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9186 }
9187 }
9188
9189 if (OVERLOAD_TYPE_P (t)
9190 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9191 {
9192 static const char *tags[] = {"abi_tag", "may_alias"};
9193
9194 for (unsigned ix = 0; ix != 2; ix++)
9195 {
9196 tree attributes
9197 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9198
9199 if (attributes)
9200 TYPE_ATTRIBUTES (t)
9201 = tree_cons (TREE_PURPOSE (attributes),
9202 TREE_VALUE (attributes),
9203 TYPE_ATTRIBUTES (t));
9204 }
9205 }
9206
9207 /* Let's consider the explicit specialization of a member
9208 of a class template specialization that is implicitly instantiated,
9209 e.g.:
9210 template<class T>
9211 struct S
9212 {
9213 template<class U> struct M {}; //#0
9214 };
9215
9216 template<>
9217 template<>
9218 struct S<int>::M<char> //#1
9219 {
9220 int i;
9221 };
9222 [temp.expl.spec]/4 says this is valid.
9223
9224 In this case, when we write:
9225 S<int>::M<char> m;
9226
9227 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9228 the one of #0.
9229
9230 When we encounter #1, we want to store the partial instantiation
9231 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9232
9233 For all cases other than this "explicit specialization of member of a
9234 class template", we just want to store the most general template into
9235 the CLASSTYPE_TI_TEMPLATE of M.
9236
9237 This case of "explicit specialization of member of a class template"
9238 only happens when:
9239 1/ the enclosing class is an instantiation of, and therefore not
9240 the same as, the context of the most general template, and
9241 2/ we aren't looking at the partial instantiation itself, i.e.
9242 the innermost arguments are not the same as the innermost parms of
9243 the most general template.
9244
9245 So it's only when 1/ and 2/ happens that we want to use the partial
9246 instantiation of the member template in lieu of its most general
9247 template. */
9248
9249 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9250 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9251 /* the enclosing class must be an instantiation... */
9252 && CLASS_TYPE_P (context)
9253 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9254 {
9255 TREE_VEC_LENGTH (arglist)--;
9256 ++processing_template_decl;
9257 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9258 tree partial_inst_args =
9259 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9260 arglist, complain, NULL_TREE);
9261 --processing_template_decl;
9262 TREE_VEC_LENGTH (arglist)++;
9263 if (partial_inst_args == error_mark_node)
9264 return error_mark_node;
9265 use_partial_inst_tmpl =
9266 /*...and we must not be looking at the partial instantiation
9267 itself. */
9268 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9269 partial_inst_args);
9270 }
9271
9272 if (!use_partial_inst_tmpl)
9273 /* This case is easy; there are no member templates involved. */
9274 found = gen_tmpl;
9275 else
9276 {
9277 /* This is a full instantiation of a member template. Find
9278 the partial instantiation of which this is an instance. */
9279
9280 /* Temporarily reduce by one the number of levels in the ARGLIST
9281 so as to avoid comparing the last set of arguments. */
9282 TREE_VEC_LENGTH (arglist)--;
9283 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9284 TREE_VEC_LENGTH (arglist)++;
9285 /* FOUND is either a proper class type, or an alias
9286 template specialization. In the later case, it's a
9287 TYPE_DECL, resulting from the substituting of arguments
9288 for parameters in the TYPE_DECL of the alias template
9289 done earlier. So be careful while getting the template
9290 of FOUND. */
9291 found = (TREE_CODE (found) == TEMPLATE_DECL
9292 ? found
9293 : (TREE_CODE (found) == TYPE_DECL
9294 ? DECL_TI_TEMPLATE (found)
9295 : CLASSTYPE_TI_TEMPLATE (found)));
9296 }
9297
9298 // Build template info for the new specialization.
9299 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9300
9301 elt.spec = t;
9302 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9303 entry = ggc_alloc<spec_entry> ();
9304 *entry = elt;
9305 *slot = entry;
9306
9307 /* Note this use of the partial instantiation so we can check it
9308 later in maybe_process_partial_specialization. */
9309 DECL_TEMPLATE_INSTANTIATIONS (found)
9310 = tree_cons (arglist, t,
9311 DECL_TEMPLATE_INSTANTIATIONS (found));
9312
9313 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9314 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9315 /* Now that the type has been registered on the instantiations
9316 list, we set up the enumerators. Because the enumeration
9317 constants may involve the enumeration type itself, we make
9318 sure to register the type first, and then create the
9319 constants. That way, doing tsubst_expr for the enumeration
9320 constants won't result in recursive calls here; we'll find
9321 the instantiation and exit above. */
9322 tsubst_enum (template_type, t, arglist);
9323
9324 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9325 /* If the type makes use of template parameters, the
9326 code that generates debugging information will crash. */
9327 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9328
9329 /* Possibly limit visibility based on template args. */
9330 TREE_PUBLIC (type_decl) = 1;
9331 determine_visibility (type_decl);
9332
9333 inherit_targ_abi_tags (t);
9334
9335 return t;
9336 }
9337 }
9338
9339 /* Wrapper for lookup_template_class_1. */
9340
9341 tree
9342 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9343 int entering_scope, tsubst_flags_t complain)
9344 {
9345 tree ret;
9346 timevar_push (TV_TEMPLATE_INST);
9347 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9348 entering_scope, complain);
9349 timevar_pop (TV_TEMPLATE_INST);
9350 return ret;
9351 }
9352
9353 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9354
9355 tree
9356 lookup_template_variable (tree templ, tree arglist)
9357 {
9358 /* The type of the expression is NULL_TREE since the template-id could refer
9359 to an explicit or partial specialization. */
9360 tree type = NULL_TREE;
9361 if (flag_concepts && variable_concept_p (templ))
9362 /* Except that concepts are always bool. */
9363 type = boolean_type_node;
9364 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9365 }
9366
9367 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9368
9369 tree
9370 finish_template_variable (tree var, tsubst_flags_t complain)
9371 {
9372 tree templ = TREE_OPERAND (var, 0);
9373 tree arglist = TREE_OPERAND (var, 1);
9374
9375 /* We never want to return a VAR_DECL for a variable concept, since they
9376 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9377 bool concept_p = flag_concepts && variable_concept_p (templ);
9378 if (concept_p && processing_template_decl)
9379 return var;
9380
9381 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9382 arglist = add_outermost_template_args (tmpl_args, arglist);
9383
9384 templ = most_general_template (templ);
9385 tree parms = DECL_TEMPLATE_PARMS (templ);
9386 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9387 /*req_all*/true,
9388 /*use_default*/true);
9389
9390 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9391 {
9392 if (complain & tf_error)
9393 {
9394 error ("use of invalid variable template %qE", var);
9395 diagnose_constraints (location_of (var), templ, arglist);
9396 }
9397 return error_mark_node;
9398 }
9399
9400 /* If a template-id refers to a specialization of a variable
9401 concept, then the expression is true if and only if the
9402 concept's constraints are satisfied by the given template
9403 arguments.
9404
9405 NOTE: This is an extension of Concepts Lite TS that
9406 allows constraints to be used in expressions. */
9407 if (concept_p)
9408 {
9409 tree decl = DECL_TEMPLATE_RESULT (templ);
9410 return evaluate_variable_concept (decl, arglist);
9411 }
9412
9413 return instantiate_template (templ, arglist, complain);
9414 }
9415
9416 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9417 TARGS template args, and instantiate it if it's not dependent. */
9418
9419 tree
9420 lookup_and_finish_template_variable (tree templ, tree targs,
9421 tsubst_flags_t complain)
9422 {
9423 templ = lookup_template_variable (templ, targs);
9424 if (!any_dependent_template_arguments_p (targs))
9425 {
9426 templ = finish_template_variable (templ, complain);
9427 mark_used (templ);
9428 }
9429
9430 return convert_from_reference (templ);
9431 }
9432
9433 \f
9434 struct pair_fn_data
9435 {
9436 tree_fn_t fn;
9437 tree_fn_t any_fn;
9438 void *data;
9439 /* True when we should also visit template parameters that occur in
9440 non-deduced contexts. */
9441 bool include_nondeduced_p;
9442 hash_set<tree> *visited;
9443 };
9444
9445 /* Called from for_each_template_parm via walk_tree. */
9446
9447 static tree
9448 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9449 {
9450 tree t = *tp;
9451 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9452 tree_fn_t fn = pfd->fn;
9453 void *data = pfd->data;
9454 tree result = NULL_TREE;
9455
9456 #define WALK_SUBTREE(NODE) \
9457 do \
9458 { \
9459 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9460 pfd->include_nondeduced_p, \
9461 pfd->any_fn); \
9462 if (result) goto out; \
9463 } \
9464 while (0)
9465
9466 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9467 return t;
9468
9469 if (TYPE_P (t)
9470 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9471 WALK_SUBTREE (TYPE_CONTEXT (t));
9472
9473 switch (TREE_CODE (t))
9474 {
9475 case RECORD_TYPE:
9476 if (TYPE_PTRMEMFUNC_P (t))
9477 break;
9478 /* Fall through. */
9479
9480 case UNION_TYPE:
9481 case ENUMERAL_TYPE:
9482 if (!TYPE_TEMPLATE_INFO (t))
9483 *walk_subtrees = 0;
9484 else
9485 WALK_SUBTREE (TYPE_TI_ARGS (t));
9486 break;
9487
9488 case INTEGER_TYPE:
9489 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9490 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9491 break;
9492
9493 case METHOD_TYPE:
9494 /* Since we're not going to walk subtrees, we have to do this
9495 explicitly here. */
9496 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9497 /* Fall through. */
9498
9499 case FUNCTION_TYPE:
9500 /* Check the return type. */
9501 WALK_SUBTREE (TREE_TYPE (t));
9502
9503 /* Check the parameter types. Since default arguments are not
9504 instantiated until they are needed, the TYPE_ARG_TYPES may
9505 contain expressions that involve template parameters. But,
9506 no-one should be looking at them yet. And, once they're
9507 instantiated, they don't contain template parameters, so
9508 there's no point in looking at them then, either. */
9509 {
9510 tree parm;
9511
9512 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9513 WALK_SUBTREE (TREE_VALUE (parm));
9514
9515 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9516 want walk_tree walking into them itself. */
9517 *walk_subtrees = 0;
9518 }
9519
9520 if (flag_noexcept_type)
9521 {
9522 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9523 if (spec)
9524 WALK_SUBTREE (TREE_PURPOSE (spec));
9525 }
9526 break;
9527
9528 case TYPEOF_TYPE:
9529 case UNDERLYING_TYPE:
9530 if (pfd->include_nondeduced_p
9531 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9532 pfd->visited,
9533 pfd->include_nondeduced_p,
9534 pfd->any_fn))
9535 return error_mark_node;
9536 break;
9537
9538 case FUNCTION_DECL:
9539 case VAR_DECL:
9540 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9541 WALK_SUBTREE (DECL_TI_ARGS (t));
9542 /* Fall through. */
9543
9544 case PARM_DECL:
9545 case CONST_DECL:
9546 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9547 WALK_SUBTREE (DECL_INITIAL (t));
9548 if (DECL_CONTEXT (t)
9549 && pfd->include_nondeduced_p)
9550 WALK_SUBTREE (DECL_CONTEXT (t));
9551 break;
9552
9553 case BOUND_TEMPLATE_TEMPLATE_PARM:
9554 /* Record template parameters such as `T' inside `TT<T>'. */
9555 WALK_SUBTREE (TYPE_TI_ARGS (t));
9556 /* Fall through. */
9557
9558 case TEMPLATE_TEMPLATE_PARM:
9559 case TEMPLATE_TYPE_PARM:
9560 case TEMPLATE_PARM_INDEX:
9561 if (fn && (*fn)(t, data))
9562 return t;
9563 else if (!fn)
9564 return t;
9565 break;
9566
9567 case TEMPLATE_DECL:
9568 /* A template template parameter is encountered. */
9569 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9570 WALK_SUBTREE (TREE_TYPE (t));
9571
9572 /* Already substituted template template parameter */
9573 *walk_subtrees = 0;
9574 break;
9575
9576 case TYPENAME_TYPE:
9577 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9578 partial instantiation. */
9579 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9580 break;
9581
9582 case CONSTRUCTOR:
9583 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9584 && pfd->include_nondeduced_p)
9585 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9586 break;
9587
9588 case INDIRECT_REF:
9589 case COMPONENT_REF:
9590 /* If there's no type, then this thing must be some expression
9591 involving template parameters. */
9592 if (!fn && !TREE_TYPE (t))
9593 return error_mark_node;
9594 break;
9595
9596 case MODOP_EXPR:
9597 case CAST_EXPR:
9598 case IMPLICIT_CONV_EXPR:
9599 case REINTERPRET_CAST_EXPR:
9600 case CONST_CAST_EXPR:
9601 case STATIC_CAST_EXPR:
9602 case DYNAMIC_CAST_EXPR:
9603 case ARROW_EXPR:
9604 case DOTSTAR_EXPR:
9605 case TYPEID_EXPR:
9606 case PSEUDO_DTOR_EXPR:
9607 if (!fn)
9608 return error_mark_node;
9609 break;
9610
9611 default:
9612 break;
9613 }
9614
9615 #undef WALK_SUBTREE
9616
9617 /* We didn't find any template parameters we liked. */
9618 out:
9619 return result;
9620 }
9621
9622 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9623 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9624 call FN with the parameter and the DATA.
9625 If FN returns nonzero, the iteration is terminated, and
9626 for_each_template_parm returns 1. Otherwise, the iteration
9627 continues. If FN never returns a nonzero value, the value
9628 returned by for_each_template_parm is 0. If FN is NULL, it is
9629 considered to be the function which always returns 1.
9630
9631 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9632 parameters that occur in non-deduced contexts. When false, only
9633 visits those template parameters that can be deduced. */
9634
9635 static tree
9636 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9637 hash_set<tree> *visited,
9638 bool include_nondeduced_p,
9639 tree_fn_t any_fn)
9640 {
9641 struct pair_fn_data pfd;
9642 tree result;
9643
9644 /* Set up. */
9645 pfd.fn = fn;
9646 pfd.any_fn = any_fn;
9647 pfd.data = data;
9648 pfd.include_nondeduced_p = include_nondeduced_p;
9649
9650 /* Walk the tree. (Conceptually, we would like to walk without
9651 duplicates, but for_each_template_parm_r recursively calls
9652 for_each_template_parm, so we would need to reorganize a fair
9653 bit to use walk_tree_without_duplicates, so we keep our own
9654 visited list.) */
9655 if (visited)
9656 pfd.visited = visited;
9657 else
9658 pfd.visited = new hash_set<tree>;
9659 result = cp_walk_tree (&t,
9660 for_each_template_parm_r,
9661 &pfd,
9662 pfd.visited);
9663
9664 /* Clean up. */
9665 if (!visited)
9666 {
9667 delete pfd.visited;
9668 pfd.visited = 0;
9669 }
9670
9671 return result;
9672 }
9673
9674 /* Returns true if T depends on any template parameter. */
9675
9676 int
9677 uses_template_parms (tree t)
9678 {
9679 if (t == NULL_TREE)
9680 return false;
9681
9682 bool dependent_p;
9683 int saved_processing_template_decl;
9684
9685 saved_processing_template_decl = processing_template_decl;
9686 if (!saved_processing_template_decl)
9687 processing_template_decl = 1;
9688 if (TYPE_P (t))
9689 dependent_p = dependent_type_p (t);
9690 else if (TREE_CODE (t) == TREE_VEC)
9691 dependent_p = any_dependent_template_arguments_p (t);
9692 else if (TREE_CODE (t) == TREE_LIST)
9693 dependent_p = (uses_template_parms (TREE_VALUE (t))
9694 || uses_template_parms (TREE_CHAIN (t)));
9695 else if (TREE_CODE (t) == TYPE_DECL)
9696 dependent_p = dependent_type_p (TREE_TYPE (t));
9697 else if (DECL_P (t)
9698 || EXPR_P (t)
9699 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9700 || TREE_CODE (t) == OVERLOAD
9701 || BASELINK_P (t)
9702 || identifier_p (t)
9703 || TREE_CODE (t) == TRAIT_EXPR
9704 || TREE_CODE (t) == CONSTRUCTOR
9705 || CONSTANT_CLASS_P (t))
9706 dependent_p = (type_dependent_expression_p (t)
9707 || value_dependent_expression_p (t));
9708 else
9709 {
9710 gcc_assert (t == error_mark_node);
9711 dependent_p = false;
9712 }
9713
9714 processing_template_decl = saved_processing_template_decl;
9715
9716 return dependent_p;
9717 }
9718
9719 /* Returns true iff current_function_decl is an incompletely instantiated
9720 template. Useful instead of processing_template_decl because the latter
9721 is set to 0 during instantiate_non_dependent_expr. */
9722
9723 bool
9724 in_template_function (void)
9725 {
9726 tree fn = current_function_decl;
9727 bool ret;
9728 ++processing_template_decl;
9729 ret = (fn && DECL_LANG_SPECIFIC (fn)
9730 && DECL_TEMPLATE_INFO (fn)
9731 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9732 --processing_template_decl;
9733 return ret;
9734 }
9735
9736 /* Returns true if T depends on any template parameter with level LEVEL. */
9737
9738 bool
9739 uses_template_parms_level (tree t, int level)
9740 {
9741 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9742 /*include_nondeduced_p=*/true);
9743 }
9744
9745 /* Returns true if the signature of DECL depends on any template parameter from
9746 its enclosing class. */
9747
9748 bool
9749 uses_outer_template_parms (tree decl)
9750 {
9751 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9752 if (depth == 0)
9753 return false;
9754 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9755 &depth, NULL, /*include_nondeduced_p=*/true))
9756 return true;
9757 if (PRIMARY_TEMPLATE_P (decl)
9758 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9759 (DECL_TEMPLATE_PARMS (decl)),
9760 template_parm_outer_level,
9761 &depth, NULL, /*include_nondeduced_p=*/true))
9762 return true;
9763 tree ci = get_constraints (decl);
9764 if (ci)
9765 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9766 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9767 &depth, NULL, /*nondeduced*/true))
9768 return true;
9769 return false;
9770 }
9771
9772 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9773 ill-formed translation unit, i.e. a variable or function that isn't
9774 usable in a constant expression. */
9775
9776 static inline bool
9777 neglectable_inst_p (tree d)
9778 {
9779 return (DECL_P (d)
9780 && !undeduced_auto_decl (d)
9781 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9782 : decl_maybe_constant_var_p (d)));
9783 }
9784
9785 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9786 neglectable and instantiated from within an erroneous instantiation. */
9787
9788 static bool
9789 limit_bad_template_recursion (tree decl)
9790 {
9791 struct tinst_level *lev = current_tinst_level;
9792 int errs = errorcount + sorrycount;
9793 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9794 return false;
9795
9796 for (; lev; lev = lev->next)
9797 if (neglectable_inst_p (lev->decl))
9798 break;
9799
9800 return (lev && errs > lev->errors);
9801 }
9802
9803 static int tinst_depth;
9804 extern int max_tinst_depth;
9805 int depth_reached;
9806
9807 static GTY(()) struct tinst_level *last_error_tinst_level;
9808
9809 /* We're starting to instantiate D; record the template instantiation context
9810 for diagnostics and to restore it later. */
9811
9812 bool
9813 push_tinst_level (tree d)
9814 {
9815 return push_tinst_level_loc (d, input_location);
9816 }
9817
9818 /* We're starting to instantiate D; record the template instantiation context
9819 at LOC for diagnostics and to restore it later. */
9820
9821 bool
9822 push_tinst_level_loc (tree d, location_t loc)
9823 {
9824 struct tinst_level *new_level;
9825
9826 if (tinst_depth >= max_tinst_depth)
9827 {
9828 /* Tell error.c not to try to instantiate any templates. */
9829 at_eof = 2;
9830 fatal_error (input_location,
9831 "template instantiation depth exceeds maximum of %d"
9832 " (use -ftemplate-depth= to increase the maximum)",
9833 max_tinst_depth);
9834 return false;
9835 }
9836
9837 /* If the current instantiation caused problems, don't let it instantiate
9838 anything else. Do allow deduction substitution and decls usable in
9839 constant expressions. */
9840 if (limit_bad_template_recursion (d))
9841 return false;
9842
9843 /* When not -quiet, dump template instantiations other than functions, since
9844 announce_function will take care of those. */
9845 if (!quiet_flag
9846 && TREE_CODE (d) != TREE_LIST
9847 && TREE_CODE (d) != FUNCTION_DECL)
9848 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9849
9850 new_level = ggc_alloc<tinst_level> ();
9851 new_level->decl = d;
9852 new_level->locus = loc;
9853 new_level->errors = errorcount+sorrycount;
9854 new_level->in_system_header_p = in_system_header_at (input_location);
9855 new_level->next = current_tinst_level;
9856 current_tinst_level = new_level;
9857
9858 ++tinst_depth;
9859 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9860 depth_reached = tinst_depth;
9861
9862 return true;
9863 }
9864
9865 /* We're done instantiating this template; return to the instantiation
9866 context. */
9867
9868 void
9869 pop_tinst_level (void)
9870 {
9871 /* Restore the filename and line number stashed away when we started
9872 this instantiation. */
9873 input_location = current_tinst_level->locus;
9874 current_tinst_level = current_tinst_level->next;
9875 --tinst_depth;
9876 }
9877
9878 /* We're instantiating a deferred template; restore the template
9879 instantiation context in which the instantiation was requested, which
9880 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9881
9882 static tree
9883 reopen_tinst_level (struct tinst_level *level)
9884 {
9885 struct tinst_level *t;
9886
9887 tinst_depth = 0;
9888 for (t = level; t; t = t->next)
9889 ++tinst_depth;
9890
9891 current_tinst_level = level;
9892 pop_tinst_level ();
9893 if (current_tinst_level)
9894 current_tinst_level->errors = errorcount+sorrycount;
9895 return level->decl;
9896 }
9897
9898 /* Returns the TINST_LEVEL which gives the original instantiation
9899 context. */
9900
9901 struct tinst_level *
9902 outermost_tinst_level (void)
9903 {
9904 struct tinst_level *level = current_tinst_level;
9905 if (level)
9906 while (level->next)
9907 level = level->next;
9908 return level;
9909 }
9910
9911 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9912 vector of template arguments, as for tsubst.
9913
9914 Returns an appropriate tsubst'd friend declaration. */
9915
9916 static tree
9917 tsubst_friend_function (tree decl, tree args)
9918 {
9919 tree new_friend;
9920
9921 if (TREE_CODE (decl) == FUNCTION_DECL
9922 && DECL_TEMPLATE_INSTANTIATION (decl)
9923 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9924 /* This was a friend declared with an explicit template
9925 argument list, e.g.:
9926
9927 friend void f<>(T);
9928
9929 to indicate that f was a template instantiation, not a new
9930 function declaration. Now, we have to figure out what
9931 instantiation of what template. */
9932 {
9933 tree template_id, arglist, fns;
9934 tree new_args;
9935 tree tmpl;
9936 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9937
9938 /* Friend functions are looked up in the containing namespace scope.
9939 We must enter that scope, to avoid finding member functions of the
9940 current class with same name. */
9941 push_nested_namespace (ns);
9942 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9943 tf_warning_or_error, NULL_TREE,
9944 /*integral_constant_expression_p=*/false);
9945 pop_nested_namespace (ns);
9946 arglist = tsubst (DECL_TI_ARGS (decl), args,
9947 tf_warning_or_error, NULL_TREE);
9948 template_id = lookup_template_function (fns, arglist);
9949
9950 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9951 tmpl = determine_specialization (template_id, new_friend,
9952 &new_args,
9953 /*need_member_template=*/0,
9954 TREE_VEC_LENGTH (args),
9955 tsk_none);
9956 return instantiate_template (tmpl, new_args, tf_error);
9957 }
9958
9959 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9960
9961 /* The NEW_FRIEND will look like an instantiation, to the
9962 compiler, but is not an instantiation from the point of view of
9963 the language. For example, we might have had:
9964
9965 template <class T> struct S {
9966 template <class U> friend void f(T, U);
9967 };
9968
9969 Then, in S<int>, template <class U> void f(int, U) is not an
9970 instantiation of anything. */
9971 if (new_friend == error_mark_node)
9972 return error_mark_node;
9973
9974 DECL_USE_TEMPLATE (new_friend) = 0;
9975 if (TREE_CODE (decl) == TEMPLATE_DECL)
9976 {
9977 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9978 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9979 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9980 }
9981
9982 /* The mangled name for the NEW_FRIEND is incorrect. The function
9983 is not a template instantiation and should not be mangled like
9984 one. Therefore, we forget the mangling here; we'll recompute it
9985 later if we need it. */
9986 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9987 {
9988 SET_DECL_RTL (new_friend, NULL);
9989 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9990 }
9991
9992 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9993 {
9994 tree old_decl;
9995 tree new_friend_template_info;
9996 tree new_friend_result_template_info;
9997 tree ns;
9998 int new_friend_is_defn;
9999
10000 /* We must save some information from NEW_FRIEND before calling
10001 duplicate decls since that function will free NEW_FRIEND if
10002 possible. */
10003 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10004 new_friend_is_defn =
10005 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10006 (template_for_substitution (new_friend)))
10007 != NULL_TREE);
10008 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10009 {
10010 /* This declaration is a `primary' template. */
10011 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10012
10013 new_friend_result_template_info
10014 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10015 }
10016 else
10017 new_friend_result_template_info = NULL_TREE;
10018
10019 /* Inside pushdecl_namespace_level, we will push into the
10020 current namespace. However, the friend function should go
10021 into the namespace of the template. */
10022 ns = decl_namespace_context (new_friend);
10023 push_nested_namespace (ns);
10024 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10025 pop_nested_namespace (ns);
10026
10027 if (old_decl == error_mark_node)
10028 return error_mark_node;
10029
10030 if (old_decl != new_friend)
10031 {
10032 /* This new friend declaration matched an existing
10033 declaration. For example, given:
10034
10035 template <class T> void f(T);
10036 template <class U> class C {
10037 template <class T> friend void f(T) {}
10038 };
10039
10040 the friend declaration actually provides the definition
10041 of `f', once C has been instantiated for some type. So,
10042 old_decl will be the out-of-class template declaration,
10043 while new_friend is the in-class definition.
10044
10045 But, if `f' was called before this point, the
10046 instantiation of `f' will have DECL_TI_ARGS corresponding
10047 to `T' but not to `U', references to which might appear
10048 in the definition of `f'. Previously, the most general
10049 template for an instantiation of `f' was the out-of-class
10050 version; now it is the in-class version. Therefore, we
10051 run through all specialization of `f', adding to their
10052 DECL_TI_ARGS appropriately. In particular, they need a
10053 new set of outer arguments, corresponding to the
10054 arguments for this class instantiation.
10055
10056 The same situation can arise with something like this:
10057
10058 friend void f(int);
10059 template <class T> class C {
10060 friend void f(T) {}
10061 };
10062
10063 when `C<int>' is instantiated. Now, `f(int)' is defined
10064 in the class. */
10065
10066 if (!new_friend_is_defn)
10067 /* On the other hand, if the in-class declaration does
10068 *not* provide a definition, then we don't want to alter
10069 existing definitions. We can just leave everything
10070 alone. */
10071 ;
10072 else
10073 {
10074 tree new_template = TI_TEMPLATE (new_friend_template_info);
10075 tree new_args = TI_ARGS (new_friend_template_info);
10076
10077 /* Overwrite whatever template info was there before, if
10078 any, with the new template information pertaining to
10079 the declaration. */
10080 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10081
10082 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10083 {
10084 /* We should have called reregister_specialization in
10085 duplicate_decls. */
10086 gcc_assert (retrieve_specialization (new_template,
10087 new_args, 0)
10088 == old_decl);
10089
10090 /* Instantiate it if the global has already been used. */
10091 if (DECL_ODR_USED (old_decl))
10092 instantiate_decl (old_decl, /*defer_ok=*/true,
10093 /*expl_inst_class_mem_p=*/false);
10094 }
10095 else
10096 {
10097 tree t;
10098
10099 /* Indicate that the old function template is a partial
10100 instantiation. */
10101 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10102 = new_friend_result_template_info;
10103
10104 gcc_assert (new_template
10105 == most_general_template (new_template));
10106 gcc_assert (new_template != old_decl);
10107
10108 /* Reassign any specializations already in the hash table
10109 to the new more general template, and add the
10110 additional template args. */
10111 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10112 t != NULL_TREE;
10113 t = TREE_CHAIN (t))
10114 {
10115 tree spec = TREE_VALUE (t);
10116 spec_entry elt;
10117
10118 elt.tmpl = old_decl;
10119 elt.args = DECL_TI_ARGS (spec);
10120 elt.spec = NULL_TREE;
10121
10122 decl_specializations->remove_elt (&elt);
10123
10124 DECL_TI_ARGS (spec)
10125 = add_outermost_template_args (new_args,
10126 DECL_TI_ARGS (spec));
10127
10128 register_specialization
10129 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10130
10131 }
10132 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10133 }
10134 }
10135
10136 /* The information from NEW_FRIEND has been merged into OLD_DECL
10137 by duplicate_decls. */
10138 new_friend = old_decl;
10139 }
10140 }
10141 else
10142 {
10143 tree context = DECL_CONTEXT (new_friend);
10144 bool dependent_p;
10145
10146 /* In the code
10147 template <class T> class C {
10148 template <class U> friend void C1<U>::f (); // case 1
10149 friend void C2<T>::f (); // case 2
10150 };
10151 we only need to make sure CONTEXT is a complete type for
10152 case 2. To distinguish between the two cases, we note that
10153 CONTEXT of case 1 remains dependent type after tsubst while
10154 this isn't true for case 2. */
10155 ++processing_template_decl;
10156 dependent_p = dependent_type_p (context);
10157 --processing_template_decl;
10158
10159 if (!dependent_p
10160 && !complete_type_or_else (context, NULL_TREE))
10161 return error_mark_node;
10162
10163 if (COMPLETE_TYPE_P (context))
10164 {
10165 tree fn = new_friend;
10166 /* do_friend adds the TEMPLATE_DECL for any member friend
10167 template even if it isn't a member template, i.e.
10168 template <class T> friend A<T>::f();
10169 Look through it in that case. */
10170 if (TREE_CODE (fn) == TEMPLATE_DECL
10171 && !PRIMARY_TEMPLATE_P (fn))
10172 fn = DECL_TEMPLATE_RESULT (fn);
10173 /* Check to see that the declaration is really present, and,
10174 possibly obtain an improved declaration. */
10175 fn = check_classfn (context, fn, NULL_TREE);
10176
10177 if (fn)
10178 new_friend = fn;
10179 }
10180 }
10181
10182 return new_friend;
10183 }
10184
10185 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10186 template arguments, as for tsubst.
10187
10188 Returns an appropriate tsubst'd friend type or error_mark_node on
10189 failure. */
10190
10191 static tree
10192 tsubst_friend_class (tree friend_tmpl, tree args)
10193 {
10194 tree tmpl;
10195
10196 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10197 {
10198 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10199 return TREE_TYPE (tmpl);
10200 }
10201
10202 tree context = CP_DECL_CONTEXT (friend_tmpl);
10203 if (TREE_CODE (context) == NAMESPACE_DECL)
10204 push_nested_namespace (context);
10205 else
10206 push_nested_class (context);
10207
10208 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10209 /*non_class=*/false, /*block_p=*/false,
10210 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10211
10212 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10213 {
10214 /* The friend template has already been declared. Just
10215 check to see that the declarations match, and install any new
10216 default parameters. We must tsubst the default parameters,
10217 of course. We only need the innermost template parameters
10218 because that is all that redeclare_class_template will look
10219 at. */
10220 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10221 > TMPL_ARGS_DEPTH (args))
10222 {
10223 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10224 args, tf_warning_or_error);
10225 location_t saved_input_location = input_location;
10226 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10227 tree cons = get_constraints (tmpl);
10228 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10229 input_location = saved_input_location;
10230 }
10231 }
10232 else
10233 {
10234 /* The friend template has not already been declared. In this
10235 case, the instantiation of the template class will cause the
10236 injection of this template into the namespace scope. */
10237 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10238
10239 if (tmpl != error_mark_node)
10240 {
10241 /* The new TMPL is not an instantiation of anything, so we
10242 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10243 for the new type because that is supposed to be the
10244 corresponding template decl, i.e., TMPL. */
10245 DECL_USE_TEMPLATE (tmpl) = 0;
10246 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10247 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10248 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10249 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10250
10251 /* It is hidden. */
10252 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10253 DECL_ANTICIPATED (tmpl)
10254 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10255
10256 /* Inject this template into the enclosing namspace scope. */
10257 tmpl = pushdecl_namespace_level (tmpl, true);
10258 }
10259 }
10260
10261 if (TREE_CODE (context) == NAMESPACE_DECL)
10262 pop_nested_namespace (context);
10263 else
10264 pop_nested_class ();
10265
10266 return TREE_TYPE (tmpl);
10267 }
10268
10269 /* Returns zero if TYPE cannot be completed later due to circularity.
10270 Otherwise returns one. */
10271
10272 static int
10273 can_complete_type_without_circularity (tree type)
10274 {
10275 if (type == NULL_TREE || type == error_mark_node)
10276 return 0;
10277 else if (COMPLETE_TYPE_P (type))
10278 return 1;
10279 else if (TREE_CODE (type) == ARRAY_TYPE)
10280 return can_complete_type_without_circularity (TREE_TYPE (type));
10281 else if (CLASS_TYPE_P (type)
10282 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10283 return 0;
10284 else
10285 return 1;
10286 }
10287
10288 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10289 tsubst_flags_t, tree);
10290
10291 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10292 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10293
10294 static tree
10295 tsubst_attribute (tree t, tree *decl_p, tree args,
10296 tsubst_flags_t complain, tree in_decl)
10297 {
10298 gcc_assert (ATTR_IS_DEPENDENT (t));
10299
10300 tree val = TREE_VALUE (t);
10301 if (val == NULL_TREE)
10302 /* Nothing to do. */;
10303 else if ((flag_openmp || flag_openmp_simd)
10304 && is_attribute_p ("omp declare simd",
10305 get_attribute_name (t)))
10306 {
10307 tree clauses = TREE_VALUE (val);
10308 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10309 complain, in_decl);
10310 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10311 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10312 tree parms = DECL_ARGUMENTS (*decl_p);
10313 clauses
10314 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10315 if (clauses)
10316 val = build_tree_list (NULL_TREE, clauses);
10317 else
10318 val = NULL_TREE;
10319 }
10320 /* If the first attribute argument is an identifier, don't
10321 pass it through tsubst. Attributes like mode, format,
10322 cleanup and several target specific attributes expect it
10323 unmodified. */
10324 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10325 {
10326 tree chain
10327 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10328 /*integral_constant_expression_p=*/false);
10329 if (chain != TREE_CHAIN (val))
10330 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10331 }
10332 else if (PACK_EXPANSION_P (val))
10333 {
10334 /* An attribute pack expansion. */
10335 tree purp = TREE_PURPOSE (t);
10336 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10337 if (pack == error_mark_node)
10338 return error_mark_node;
10339 int len = TREE_VEC_LENGTH (pack);
10340 tree list = NULL_TREE;
10341 tree *q = &list;
10342 for (int i = 0; i < len; ++i)
10343 {
10344 tree elt = TREE_VEC_ELT (pack, i);
10345 *q = build_tree_list (purp, elt);
10346 q = &TREE_CHAIN (*q);
10347 }
10348 return list;
10349 }
10350 else
10351 val = tsubst_expr (val, args, complain, in_decl,
10352 /*integral_constant_expression_p=*/false);
10353
10354 if (val != TREE_VALUE (t))
10355 return build_tree_list (TREE_PURPOSE (t), val);
10356 return t;
10357 }
10358
10359 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10360 unchanged or a new TREE_LIST chain. */
10361
10362 static tree
10363 tsubst_attributes (tree attributes, tree args,
10364 tsubst_flags_t complain, tree in_decl)
10365 {
10366 tree last_dep = NULL_TREE;
10367
10368 for (tree t = attributes; t; t = TREE_CHAIN (t))
10369 if (ATTR_IS_DEPENDENT (t))
10370 {
10371 last_dep = t;
10372 attributes = copy_list (attributes);
10373 break;
10374 }
10375
10376 if (last_dep)
10377 for (tree *p = &attributes; *p; )
10378 {
10379 tree t = *p;
10380 if (ATTR_IS_DEPENDENT (t))
10381 {
10382 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10383 if (subst != t)
10384 {
10385 *p = subst;
10386 while (*p)
10387 p = &TREE_CHAIN (*p);
10388 *p = TREE_CHAIN (t);
10389 continue;
10390 }
10391 }
10392 p = &TREE_CHAIN (*p);
10393 }
10394
10395 return attributes;
10396 }
10397
10398 /* Apply any attributes which had to be deferred until instantiation
10399 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10400 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10401
10402 static void
10403 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10404 tree args, tsubst_flags_t complain, tree in_decl)
10405 {
10406 tree last_dep = NULL_TREE;
10407 tree t;
10408 tree *p;
10409
10410 if (attributes == NULL_TREE)
10411 return;
10412
10413 if (DECL_P (*decl_p))
10414 {
10415 if (TREE_TYPE (*decl_p) == error_mark_node)
10416 return;
10417 p = &DECL_ATTRIBUTES (*decl_p);
10418 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10419 to our attributes parameter. */
10420 gcc_assert (*p == attributes);
10421 }
10422 else
10423 {
10424 p = &TYPE_ATTRIBUTES (*decl_p);
10425 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10426 lookup_template_class_1, and should be preserved. */
10427 gcc_assert (*p != attributes);
10428 while (*p)
10429 p = &TREE_CHAIN (*p);
10430 }
10431
10432 for (t = attributes; t; t = TREE_CHAIN (t))
10433 if (ATTR_IS_DEPENDENT (t))
10434 {
10435 last_dep = t;
10436 attributes = copy_list (attributes);
10437 break;
10438 }
10439
10440 *p = attributes;
10441 if (last_dep)
10442 {
10443 tree late_attrs = NULL_TREE;
10444 tree *q = &late_attrs;
10445
10446 for (; *p; )
10447 {
10448 t = *p;
10449 if (ATTR_IS_DEPENDENT (t))
10450 {
10451 *p = TREE_CHAIN (t);
10452 TREE_CHAIN (t) = NULL_TREE;
10453 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10454 while (*q)
10455 q = &TREE_CHAIN (*q);
10456 }
10457 else
10458 p = &TREE_CHAIN (t);
10459 }
10460
10461 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10462 }
10463 }
10464
10465 /* Perform (or defer) access check for typedefs that were referenced
10466 from within the template TMPL code.
10467 This is a subroutine of instantiate_decl and instantiate_class_template.
10468 TMPL is the template to consider and TARGS is the list of arguments of
10469 that template. */
10470
10471 static void
10472 perform_typedefs_access_check (tree tmpl, tree targs)
10473 {
10474 location_t saved_location;
10475 unsigned i;
10476 qualified_typedef_usage_t *iter;
10477
10478 if (!tmpl
10479 || (!CLASS_TYPE_P (tmpl)
10480 && TREE_CODE (tmpl) != FUNCTION_DECL))
10481 return;
10482
10483 saved_location = input_location;
10484 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10485 {
10486 tree type_decl = iter->typedef_decl;
10487 tree type_scope = iter->context;
10488
10489 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10490 continue;
10491
10492 if (uses_template_parms (type_decl))
10493 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10494 if (uses_template_parms (type_scope))
10495 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10496
10497 /* Make access check error messages point to the location
10498 of the use of the typedef. */
10499 input_location = iter->locus;
10500 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10501 type_decl, type_decl,
10502 tf_warning_or_error);
10503 }
10504 input_location = saved_location;
10505 }
10506
10507 static tree
10508 instantiate_class_template_1 (tree type)
10509 {
10510 tree templ, args, pattern, t, member;
10511 tree typedecl;
10512 tree pbinfo;
10513 tree base_list;
10514 unsigned int saved_maximum_field_alignment;
10515 tree fn_context;
10516
10517 if (type == error_mark_node)
10518 return error_mark_node;
10519
10520 if (COMPLETE_OR_OPEN_TYPE_P (type)
10521 || uses_template_parms (type))
10522 return type;
10523
10524 /* Figure out which template is being instantiated. */
10525 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10526 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10527
10528 /* Mark the type as in the process of being defined. */
10529 TYPE_BEING_DEFINED (type) = 1;
10530
10531 /* Determine what specialization of the original template to
10532 instantiate. */
10533 t = most_specialized_partial_spec (type, tf_warning_or_error);
10534 if (t == error_mark_node)
10535 return error_mark_node;
10536 else if (t)
10537 {
10538 /* This TYPE is actually an instantiation of a partial
10539 specialization. We replace the innermost set of ARGS with
10540 the arguments appropriate for substitution. For example,
10541 given:
10542
10543 template <class T> struct S {};
10544 template <class T> struct S<T*> {};
10545
10546 and supposing that we are instantiating S<int*>, ARGS will
10547 presently be {int*} -- but we need {int}. */
10548 pattern = TREE_TYPE (t);
10549 args = TREE_PURPOSE (t);
10550 }
10551 else
10552 {
10553 pattern = TREE_TYPE (templ);
10554 args = CLASSTYPE_TI_ARGS (type);
10555 }
10556
10557 /* If the template we're instantiating is incomplete, then clearly
10558 there's nothing we can do. */
10559 if (!COMPLETE_TYPE_P (pattern))
10560 {
10561 /* We can try again later. */
10562 TYPE_BEING_DEFINED (type) = 0;
10563 return type;
10564 }
10565
10566 /* If we've recursively instantiated too many templates, stop. */
10567 if (! push_tinst_level (type))
10568 return type;
10569
10570 /* We may be in the middle of deferred access check. Disable
10571 it now. */
10572 push_deferring_access_checks (dk_no_deferred);
10573
10574 int saved_unevaluated_operand = cp_unevaluated_operand;
10575 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10576
10577 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10578 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10579 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10580 fn_context = error_mark_node;
10581 if (!fn_context)
10582 push_to_top_level ();
10583 else
10584 {
10585 cp_unevaluated_operand = 0;
10586 c_inhibit_evaluation_warnings = 0;
10587 }
10588 /* Use #pragma pack from the template context. */
10589 saved_maximum_field_alignment = maximum_field_alignment;
10590 maximum_field_alignment = TYPE_PRECISION (pattern);
10591
10592 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10593
10594 /* Set the input location to the most specialized template definition.
10595 This is needed if tsubsting causes an error. */
10596 typedecl = TYPE_MAIN_DECL (pattern);
10597 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10598 DECL_SOURCE_LOCATION (typedecl);
10599
10600 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10601 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10602 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10603 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10604 if (ANON_AGGR_TYPE_P (pattern))
10605 SET_ANON_AGGR_TYPE_P (type);
10606 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10607 {
10608 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10609 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10610 /* Adjust visibility for template arguments. */
10611 determine_visibility (TYPE_MAIN_DECL (type));
10612 }
10613 if (CLASS_TYPE_P (type))
10614 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10615
10616 pbinfo = TYPE_BINFO (pattern);
10617
10618 /* We should never instantiate a nested class before its enclosing
10619 class; we need to look up the nested class by name before we can
10620 instantiate it, and that lookup should instantiate the enclosing
10621 class. */
10622 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10623 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10624
10625 base_list = NULL_TREE;
10626 if (BINFO_N_BASE_BINFOS (pbinfo))
10627 {
10628 tree pbase_binfo;
10629 tree pushed_scope;
10630 int i;
10631
10632 /* We must enter the scope containing the type, as that is where
10633 the accessibility of types named in dependent bases are
10634 looked up from. */
10635 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10636
10637 /* Substitute into each of the bases to determine the actual
10638 basetypes. */
10639 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10640 {
10641 tree base;
10642 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10643 tree expanded_bases = NULL_TREE;
10644 int idx, len = 1;
10645
10646 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10647 {
10648 expanded_bases =
10649 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10650 args, tf_error, NULL_TREE);
10651 if (expanded_bases == error_mark_node)
10652 continue;
10653
10654 len = TREE_VEC_LENGTH (expanded_bases);
10655 }
10656
10657 for (idx = 0; idx < len; idx++)
10658 {
10659 if (expanded_bases)
10660 /* Extract the already-expanded base class. */
10661 base = TREE_VEC_ELT (expanded_bases, idx);
10662 else
10663 /* Substitute to figure out the base class. */
10664 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10665 NULL_TREE);
10666
10667 if (base == error_mark_node)
10668 continue;
10669
10670 base_list = tree_cons (access, base, base_list);
10671 if (BINFO_VIRTUAL_P (pbase_binfo))
10672 TREE_TYPE (base_list) = integer_type_node;
10673 }
10674 }
10675
10676 /* The list is now in reverse order; correct that. */
10677 base_list = nreverse (base_list);
10678
10679 if (pushed_scope)
10680 pop_scope (pushed_scope);
10681 }
10682 /* Now call xref_basetypes to set up all the base-class
10683 information. */
10684 xref_basetypes (type, base_list);
10685
10686 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10687 (int) ATTR_FLAG_TYPE_IN_PLACE,
10688 args, tf_error, NULL_TREE);
10689 fixup_attribute_variants (type);
10690
10691 /* Now that our base classes are set up, enter the scope of the
10692 class, so that name lookups into base classes, etc. will work
10693 correctly. This is precisely analogous to what we do in
10694 begin_class_definition when defining an ordinary non-template
10695 class, except we also need to push the enclosing classes. */
10696 push_nested_class (type);
10697
10698 /* Now members are processed in the order of declaration. */
10699 for (member = CLASSTYPE_DECL_LIST (pattern);
10700 member; member = TREE_CHAIN (member))
10701 {
10702 tree t = TREE_VALUE (member);
10703
10704 if (TREE_PURPOSE (member))
10705 {
10706 if (TYPE_P (t))
10707 {
10708 if (LAMBDA_TYPE_P (t))
10709 /* A closure type for a lambda in an NSDMI or default argument.
10710 Ignore it; it will be regenerated when needed. */
10711 continue;
10712
10713 /* Build new CLASSTYPE_NESTED_UTDS. */
10714
10715 tree newtag;
10716 bool class_template_p;
10717
10718 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10719 && TYPE_LANG_SPECIFIC (t)
10720 && CLASSTYPE_IS_TEMPLATE (t));
10721 /* If the member is a class template, then -- even after
10722 substitution -- there may be dependent types in the
10723 template argument list for the class. We increment
10724 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10725 that function will assume that no types are dependent
10726 when outside of a template. */
10727 if (class_template_p)
10728 ++processing_template_decl;
10729 newtag = tsubst (t, args, tf_error, NULL_TREE);
10730 if (class_template_p)
10731 --processing_template_decl;
10732 if (newtag == error_mark_node)
10733 continue;
10734
10735 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10736 {
10737 tree name = TYPE_IDENTIFIER (t);
10738
10739 if (class_template_p)
10740 /* Unfortunately, lookup_template_class sets
10741 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10742 instantiation (i.e., for the type of a member
10743 template class nested within a template class.)
10744 This behavior is required for
10745 maybe_process_partial_specialization to work
10746 correctly, but is not accurate in this case;
10747 the TAG is not an instantiation of anything.
10748 (The corresponding TEMPLATE_DECL is an
10749 instantiation, but the TYPE is not.) */
10750 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10751
10752 /* Now, we call pushtag to put this NEWTAG into the scope of
10753 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10754 pushtag calling push_template_decl. We don't have to do
10755 this for enums because it will already have been done in
10756 tsubst_enum. */
10757 if (name)
10758 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10759 pushtag (name, newtag, /*tag_scope=*/ts_current);
10760 }
10761 }
10762 else if (DECL_DECLARES_FUNCTION_P (t))
10763 {
10764 tree r;
10765
10766 if (TREE_CODE (t) == TEMPLATE_DECL)
10767 ++processing_template_decl;
10768 r = tsubst (t, args, tf_error, NULL_TREE);
10769 if (TREE_CODE (t) == TEMPLATE_DECL)
10770 --processing_template_decl;
10771 set_current_access_from_decl (r);
10772 finish_member_declaration (r);
10773 /* Instantiate members marked with attribute used. */
10774 if (r != error_mark_node && DECL_PRESERVE_P (r))
10775 mark_used (r);
10776 if (TREE_CODE (r) == FUNCTION_DECL
10777 && DECL_OMP_DECLARE_REDUCTION_P (r))
10778 cp_check_omp_declare_reduction (r);
10779 }
10780 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
10781 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10782 /* A closure type for a lambda in an NSDMI or default argument.
10783 Ignore it; it will be regenerated when needed. */;
10784 else
10785 {
10786 /* Build new TYPE_FIELDS. */
10787 if (TREE_CODE (t) == STATIC_ASSERT)
10788 {
10789 tree condition;
10790
10791 ++c_inhibit_evaluation_warnings;
10792 condition =
10793 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10794 tf_warning_or_error, NULL_TREE,
10795 /*integral_constant_expression_p=*/true);
10796 --c_inhibit_evaluation_warnings;
10797
10798 finish_static_assert (condition,
10799 STATIC_ASSERT_MESSAGE (t),
10800 STATIC_ASSERT_SOURCE_LOCATION (t),
10801 /*member_p=*/true);
10802 }
10803 else if (TREE_CODE (t) != CONST_DECL)
10804 {
10805 tree r;
10806 tree vec = NULL_TREE;
10807 int len = 1;
10808
10809 /* The file and line for this declaration, to
10810 assist in error message reporting. Since we
10811 called push_tinst_level above, we don't need to
10812 restore these. */
10813 input_location = DECL_SOURCE_LOCATION (t);
10814
10815 if (TREE_CODE (t) == TEMPLATE_DECL)
10816 ++processing_template_decl;
10817 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10818 if (TREE_CODE (t) == TEMPLATE_DECL)
10819 --processing_template_decl;
10820
10821 if (TREE_CODE (r) == TREE_VEC)
10822 {
10823 /* A capture pack became multiple fields. */
10824 vec = r;
10825 len = TREE_VEC_LENGTH (vec);
10826 }
10827
10828 for (int i = 0; i < len; ++i)
10829 {
10830 if (vec)
10831 r = TREE_VEC_ELT (vec, i);
10832 if (VAR_P (r))
10833 {
10834 /* In [temp.inst]:
10835
10836 [t]he initialization (and any associated
10837 side-effects) of a static data member does
10838 not occur unless the static data member is
10839 itself used in a way that requires the
10840 definition of the static data member to
10841 exist.
10842
10843 Therefore, we do not substitute into the
10844 initialized for the static data member here. */
10845 finish_static_data_member_decl
10846 (r,
10847 /*init=*/NULL_TREE,
10848 /*init_const_expr_p=*/false,
10849 /*asmspec_tree=*/NULL_TREE,
10850 /*flags=*/0);
10851 /* Instantiate members marked with attribute used. */
10852 if (r != error_mark_node && DECL_PRESERVE_P (r))
10853 mark_used (r);
10854 }
10855 else if (TREE_CODE (r) == FIELD_DECL)
10856 {
10857 /* Determine whether R has a valid type and can be
10858 completed later. If R is invalid, then its type
10859 is replaced by error_mark_node. */
10860 tree rtype = TREE_TYPE (r);
10861 if (can_complete_type_without_circularity (rtype))
10862 complete_type (rtype);
10863
10864 if (!complete_or_array_type_p (rtype))
10865 {
10866 /* If R's type couldn't be completed and
10867 it isn't a flexible array member (whose
10868 type is incomplete by definition) give
10869 an error. */
10870 cxx_incomplete_type_error (r, rtype);
10871 TREE_TYPE (r) = error_mark_node;
10872 }
10873 }
10874
10875 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10876 such a thing will already have been added to the field
10877 list by tsubst_enum in finish_member_declaration in the
10878 CLASSTYPE_NESTED_UTDS case above. */
10879 if (!(TREE_CODE (r) == TYPE_DECL
10880 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10881 && DECL_ARTIFICIAL (r)))
10882 {
10883 set_current_access_from_decl (r);
10884 finish_member_declaration (r);
10885 }
10886 }
10887 }
10888 }
10889 }
10890 else
10891 {
10892 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10893 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10894 {
10895 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10896
10897 tree friend_type = t;
10898 bool adjust_processing_template_decl = false;
10899
10900 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10901 {
10902 /* template <class T> friend class C; */
10903 friend_type = tsubst_friend_class (friend_type, args);
10904 adjust_processing_template_decl = true;
10905 }
10906 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10907 {
10908 /* template <class T> friend class C::D; */
10909 friend_type = tsubst (friend_type, args,
10910 tf_warning_or_error, NULL_TREE);
10911 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10912 friend_type = TREE_TYPE (friend_type);
10913 adjust_processing_template_decl = true;
10914 }
10915 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10916 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10917 {
10918 /* This could be either
10919
10920 friend class T::C;
10921
10922 when dependent_type_p is false or
10923
10924 template <class U> friend class T::C;
10925
10926 otherwise. */
10927 /* Bump processing_template_decl in case this is something like
10928 template <class T> friend struct A<T>::B. */
10929 ++processing_template_decl;
10930 friend_type = tsubst (friend_type, args,
10931 tf_warning_or_error, NULL_TREE);
10932 if (dependent_type_p (friend_type))
10933 adjust_processing_template_decl = true;
10934 --processing_template_decl;
10935 }
10936 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10937 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10938 && TYPE_HIDDEN_P (friend_type))
10939 {
10940 /* friend class C;
10941
10942 where C hasn't been declared yet. Let's lookup name
10943 from namespace scope directly, bypassing any name that
10944 come from dependent base class. */
10945 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10946
10947 /* The call to xref_tag_from_type does injection for friend
10948 classes. */
10949 push_nested_namespace (ns);
10950 friend_type =
10951 xref_tag_from_type (friend_type, NULL_TREE,
10952 /*tag_scope=*/ts_current);
10953 pop_nested_namespace (ns);
10954 }
10955 else if (uses_template_parms (friend_type))
10956 /* friend class C<T>; */
10957 friend_type = tsubst (friend_type, args,
10958 tf_warning_or_error, NULL_TREE);
10959 /* Otherwise it's
10960
10961 friend class C;
10962
10963 where C is already declared or
10964
10965 friend class C<int>;
10966
10967 We don't have to do anything in these cases. */
10968
10969 if (adjust_processing_template_decl)
10970 /* Trick make_friend_class into realizing that the friend
10971 we're adding is a template, not an ordinary class. It's
10972 important that we use make_friend_class since it will
10973 perform some error-checking and output cross-reference
10974 information. */
10975 ++processing_template_decl;
10976
10977 if (friend_type != error_mark_node)
10978 make_friend_class (type, friend_type, /*complain=*/false);
10979
10980 if (adjust_processing_template_decl)
10981 --processing_template_decl;
10982 }
10983 else
10984 {
10985 /* Build new DECL_FRIENDLIST. */
10986 tree r;
10987
10988 /* The file and line for this declaration, to
10989 assist in error message reporting. Since we
10990 called push_tinst_level above, we don't need to
10991 restore these. */
10992 input_location = DECL_SOURCE_LOCATION (t);
10993
10994 if (TREE_CODE (t) == TEMPLATE_DECL)
10995 {
10996 ++processing_template_decl;
10997 push_deferring_access_checks (dk_no_check);
10998 }
10999
11000 r = tsubst_friend_function (t, args);
11001 add_friend (type, r, /*complain=*/false);
11002 if (TREE_CODE (t) == TEMPLATE_DECL)
11003 {
11004 pop_deferring_access_checks ();
11005 --processing_template_decl;
11006 }
11007 }
11008 }
11009 }
11010
11011 if (fn_context)
11012 {
11013 /* Restore these before substituting into the lambda capture
11014 initializers. */
11015 cp_unevaluated_operand = saved_unevaluated_operand;
11016 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11017 }
11018
11019 /* Set the file and line number information to whatever is given for
11020 the class itself. This puts error messages involving generated
11021 implicit functions at a predictable point, and the same point
11022 that would be used for non-template classes. */
11023 input_location = DECL_SOURCE_LOCATION (typedecl);
11024
11025 unreverse_member_declarations (type);
11026 finish_struct_1 (type);
11027 TYPE_BEING_DEFINED (type) = 0;
11028
11029 /* We don't instantiate default arguments for member functions. 14.7.1:
11030
11031 The implicit instantiation of a class template specialization causes
11032 the implicit instantiation of the declarations, but not of the
11033 definitions or default arguments, of the class member functions,
11034 member classes, static data members and member templates.... */
11035
11036 /* Some typedefs referenced from within the template code need to be access
11037 checked at template instantiation time, i.e now. These types were
11038 added to the template at parsing time. Let's get those and perform
11039 the access checks then. */
11040 perform_typedefs_access_check (pattern, args);
11041 perform_deferred_access_checks (tf_warning_or_error);
11042 pop_nested_class ();
11043 maximum_field_alignment = saved_maximum_field_alignment;
11044 if (!fn_context)
11045 pop_from_top_level ();
11046 pop_deferring_access_checks ();
11047 pop_tinst_level ();
11048
11049 /* The vtable for a template class can be emitted in any translation
11050 unit in which the class is instantiated. When there is no key
11051 method, however, finish_struct_1 will already have added TYPE to
11052 the keyed_classes. */
11053 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11054 vec_safe_push (keyed_classes, type);
11055
11056 return type;
11057 }
11058
11059 /* Wrapper for instantiate_class_template_1. */
11060
11061 tree
11062 instantiate_class_template (tree type)
11063 {
11064 tree ret;
11065 timevar_push (TV_TEMPLATE_INST);
11066 ret = instantiate_class_template_1 (type);
11067 timevar_pop (TV_TEMPLATE_INST);
11068 return ret;
11069 }
11070
11071 static tree
11072 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11073 {
11074 tree r;
11075
11076 if (!t)
11077 r = t;
11078 else if (TYPE_P (t))
11079 r = tsubst (t, args, complain, in_decl);
11080 else
11081 {
11082 if (!(complain & tf_warning))
11083 ++c_inhibit_evaluation_warnings;
11084 r = tsubst_expr (t, args, complain, in_decl,
11085 /*integral_constant_expression_p=*/true);
11086 if (!(complain & tf_warning))
11087 --c_inhibit_evaluation_warnings;
11088 }
11089 return r;
11090 }
11091
11092 /* Given a function parameter pack TMPL_PARM and some function parameters
11093 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11094 and set *SPEC_P to point at the next point in the list. */
11095
11096 tree
11097 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11098 {
11099 /* Collect all of the extra "packed" parameters into an
11100 argument pack. */
11101 tree parmvec;
11102 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11103 tree spec_parm = *spec_p;
11104 int i, len;
11105
11106 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11107 if (tmpl_parm
11108 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11109 break;
11110
11111 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11112 parmvec = make_tree_vec (len);
11113 spec_parm = *spec_p;
11114 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11115 {
11116 tree elt = spec_parm;
11117 if (DECL_PACK_P (elt))
11118 elt = make_pack_expansion (elt);
11119 TREE_VEC_ELT (parmvec, i) = elt;
11120 }
11121
11122 /* Build the argument packs. */
11123 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11124 *spec_p = spec_parm;
11125
11126 return argpack;
11127 }
11128
11129 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11130 NONTYPE_ARGUMENT_PACK. */
11131
11132 static tree
11133 make_fnparm_pack (tree spec_parm)
11134 {
11135 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11136 }
11137
11138 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11139 pack expansion with no extra args, 2 if it has extra args, or 0
11140 if it is not a pack expansion. */
11141
11142 static int
11143 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11144 {
11145 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11146 if (i >= TREE_VEC_LENGTH (vec))
11147 return 0;
11148 tree elt = TREE_VEC_ELT (vec, i);
11149 if (DECL_P (elt))
11150 /* A decl pack is itself an expansion. */
11151 elt = TREE_TYPE (elt);
11152 if (!PACK_EXPANSION_P (elt))
11153 return 0;
11154 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11155 return 2;
11156 return 1;
11157 }
11158
11159
11160 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11161
11162 static tree
11163 make_argument_pack_select (tree arg_pack, unsigned index)
11164 {
11165 tree aps = make_node (ARGUMENT_PACK_SELECT);
11166
11167 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11168 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11169
11170 return aps;
11171 }
11172
11173 /* This is a subroutine of tsubst_pack_expansion.
11174
11175 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11176 mechanism to store the (non complete list of) arguments of the
11177 substitution and return a non substituted pack expansion, in order
11178 to wait for when we have enough arguments to really perform the
11179 substitution. */
11180
11181 static bool
11182 use_pack_expansion_extra_args_p (tree parm_packs,
11183 int arg_pack_len,
11184 bool has_empty_arg)
11185 {
11186 /* If one pack has an expansion and another pack has a normal
11187 argument or if one pack has an empty argument and an another
11188 one hasn't then tsubst_pack_expansion cannot perform the
11189 substitution and need to fall back on the
11190 PACK_EXPANSION_EXTRA mechanism. */
11191 if (parm_packs == NULL_TREE)
11192 return false;
11193 else if (has_empty_arg)
11194 return true;
11195
11196 bool has_expansion_arg = false;
11197 for (int i = 0 ; i < arg_pack_len; ++i)
11198 {
11199 bool has_non_expansion_arg = false;
11200 for (tree parm_pack = parm_packs;
11201 parm_pack;
11202 parm_pack = TREE_CHAIN (parm_pack))
11203 {
11204 tree arg = TREE_VALUE (parm_pack);
11205
11206 int exp = argument_pack_element_is_expansion_p (arg, i);
11207 if (exp == 2)
11208 /* We can't substitute a pack expansion with extra args into
11209 our pattern. */
11210 return true;
11211 else if (exp)
11212 has_expansion_arg = true;
11213 else
11214 has_non_expansion_arg = true;
11215 }
11216
11217 if (has_expansion_arg && has_non_expansion_arg)
11218 return true;
11219 }
11220 return false;
11221 }
11222
11223 /* [temp.variadic]/6 says that:
11224
11225 The instantiation of a pack expansion [...]
11226 produces a list E1,E2, ..., En, where N is the number of elements
11227 in the pack expansion parameters.
11228
11229 This subroutine of tsubst_pack_expansion produces one of these Ei.
11230
11231 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11232 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11233 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11234 INDEX is the index 'i' of the element Ei to produce. ARGS,
11235 COMPLAIN, and IN_DECL are the same parameters as for the
11236 tsubst_pack_expansion function.
11237
11238 The function returns the resulting Ei upon successful completion,
11239 or error_mark_node.
11240
11241 Note that this function possibly modifies the ARGS parameter, so
11242 it's the responsibility of the caller to restore it. */
11243
11244 static tree
11245 gen_elem_of_pack_expansion_instantiation (tree pattern,
11246 tree parm_packs,
11247 unsigned index,
11248 tree args /* This parm gets
11249 modified. */,
11250 tsubst_flags_t complain,
11251 tree in_decl)
11252 {
11253 tree t;
11254 bool ith_elem_is_expansion = false;
11255
11256 /* For each parameter pack, change the substitution of the parameter
11257 pack to the ith argument in its argument pack, then expand the
11258 pattern. */
11259 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11260 {
11261 tree parm = TREE_PURPOSE (pack);
11262 tree arg_pack = TREE_VALUE (pack);
11263 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11264
11265 ith_elem_is_expansion |=
11266 argument_pack_element_is_expansion_p (arg_pack, index);
11267
11268 /* Select the Ith argument from the pack. */
11269 if (TREE_CODE (parm) == PARM_DECL
11270 || VAR_P (parm)
11271 || TREE_CODE (parm) == FIELD_DECL)
11272 {
11273 if (index == 0)
11274 {
11275 aps = make_argument_pack_select (arg_pack, index);
11276 if (!mark_used (parm, complain) && !(complain & tf_error))
11277 return error_mark_node;
11278 register_local_specialization (aps, parm);
11279 }
11280 else
11281 aps = retrieve_local_specialization (parm);
11282 }
11283 else
11284 {
11285 int idx, level;
11286 template_parm_level_and_index (parm, &level, &idx);
11287
11288 if (index == 0)
11289 {
11290 aps = make_argument_pack_select (arg_pack, index);
11291 /* Update the corresponding argument. */
11292 TMPL_ARG (args, level, idx) = aps;
11293 }
11294 else
11295 /* Re-use the ARGUMENT_PACK_SELECT. */
11296 aps = TMPL_ARG (args, level, idx);
11297 }
11298 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11299 }
11300
11301 /* Substitute into the PATTERN with the (possibly altered)
11302 arguments. */
11303 if (pattern == in_decl)
11304 /* Expanding a fixed parameter pack from
11305 coerce_template_parameter_pack. */
11306 t = tsubst_decl (pattern, args, complain);
11307 else if (pattern == error_mark_node)
11308 t = error_mark_node;
11309 else if (constraint_p (pattern))
11310 {
11311 if (processing_template_decl)
11312 t = tsubst_constraint (pattern, args, complain, in_decl);
11313 else
11314 t = (constraints_satisfied_p (pattern, args)
11315 ? boolean_true_node : boolean_false_node);
11316 }
11317 else if (!TYPE_P (pattern))
11318 t = tsubst_expr (pattern, args, complain, in_decl,
11319 /*integral_constant_expression_p=*/false);
11320 else
11321 t = tsubst (pattern, args, complain, in_decl);
11322
11323 /* If the Ith argument pack element is a pack expansion, then
11324 the Ith element resulting from the substituting is going to
11325 be a pack expansion as well. */
11326 if (ith_elem_is_expansion)
11327 t = make_pack_expansion (t, complain);
11328
11329 return t;
11330 }
11331
11332 /* When the unexpanded parameter pack in a fold expression expands to an empty
11333 sequence, the value of the expression is as follows; the program is
11334 ill-formed if the operator is not listed in this table.
11335
11336 && true
11337 || false
11338 , void() */
11339
11340 tree
11341 expand_empty_fold (tree t, tsubst_flags_t complain)
11342 {
11343 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11344 if (!FOLD_EXPR_MODIFY_P (t))
11345 switch (code)
11346 {
11347 case TRUTH_ANDIF_EXPR:
11348 return boolean_true_node;
11349 case TRUTH_ORIF_EXPR:
11350 return boolean_false_node;
11351 case COMPOUND_EXPR:
11352 return void_node;
11353 default:
11354 break;
11355 }
11356
11357 if (complain & tf_error)
11358 error_at (location_of (t),
11359 "fold of empty expansion over %O", code);
11360 return error_mark_node;
11361 }
11362
11363 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11364 form an expression that combines the two terms using the
11365 operator of T. */
11366
11367 static tree
11368 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11369 {
11370 tree op = FOLD_EXPR_OP (t);
11371 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11372
11373 // Handle compound assignment operators.
11374 if (FOLD_EXPR_MODIFY_P (t))
11375 return build_x_modify_expr (input_location, left, code, right, complain);
11376
11377 switch (code)
11378 {
11379 case COMPOUND_EXPR:
11380 return build_x_compound_expr (input_location, left, right, complain);
11381 case DOTSTAR_EXPR:
11382 return build_m_component_ref (left, right, complain);
11383 default:
11384 return build_x_binary_op (input_location, code,
11385 left, TREE_CODE (left),
11386 right, TREE_CODE (right),
11387 /*overload=*/NULL,
11388 complain);
11389 }
11390 }
11391
11392 /* Substitute ARGS into the pack of a fold expression T. */
11393
11394 static inline tree
11395 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11396 {
11397 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11398 }
11399
11400 /* Substitute ARGS into the pack of a fold expression T. */
11401
11402 static inline tree
11403 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11404 {
11405 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11406 }
11407
11408 /* Expand a PACK of arguments into a grouped as left fold.
11409 Given a pack containing elements A0, A1, ..., An and an
11410 operator @, this builds the expression:
11411
11412 ((A0 @ A1) @ A2) ... @ An
11413
11414 Note that PACK must not be empty.
11415
11416 The operator is defined by the original fold expression T. */
11417
11418 static tree
11419 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11420 {
11421 tree left = TREE_VEC_ELT (pack, 0);
11422 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11423 {
11424 tree right = TREE_VEC_ELT (pack, i);
11425 left = fold_expression (t, left, right, complain);
11426 }
11427 return left;
11428 }
11429
11430 /* Substitute into a unary left fold expression. */
11431
11432 static tree
11433 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11434 tree in_decl)
11435 {
11436 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11437 if (pack == error_mark_node)
11438 return error_mark_node;
11439 if (PACK_EXPANSION_P (pack))
11440 {
11441 tree r = copy_node (t);
11442 FOLD_EXPR_PACK (r) = pack;
11443 return r;
11444 }
11445 if (TREE_VEC_LENGTH (pack) == 0)
11446 return expand_empty_fold (t, complain);
11447 else
11448 return expand_left_fold (t, pack, complain);
11449 }
11450
11451 /* Substitute into a binary left fold expression.
11452
11453 Do ths by building a single (non-empty) vector of argumnts and
11454 building the expression from those elements. */
11455
11456 static tree
11457 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11458 tree in_decl)
11459 {
11460 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11461 if (pack == error_mark_node)
11462 return error_mark_node;
11463 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11464 if (init == error_mark_node)
11465 return error_mark_node;
11466
11467 if (PACK_EXPANSION_P (pack))
11468 {
11469 tree r = copy_node (t);
11470 FOLD_EXPR_PACK (r) = pack;
11471 FOLD_EXPR_INIT (r) = init;
11472 return r;
11473 }
11474
11475 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11476 TREE_VEC_ELT (vec, 0) = init;
11477 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11478 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11479
11480 return expand_left_fold (t, vec, complain);
11481 }
11482
11483 /* Expand a PACK of arguments into a grouped as right fold.
11484 Given a pack containing elementns A0, A1, ..., and an
11485 operator @, this builds the expression:
11486
11487 A0@ ... (An-2 @ (An-1 @ An))
11488
11489 Note that PACK must not be empty.
11490
11491 The operator is defined by the original fold expression T. */
11492
11493 tree
11494 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11495 {
11496 // Build the expression.
11497 int n = TREE_VEC_LENGTH (pack);
11498 tree right = TREE_VEC_ELT (pack, n - 1);
11499 for (--n; n != 0; --n)
11500 {
11501 tree left = TREE_VEC_ELT (pack, n - 1);
11502 right = fold_expression (t, left, right, complain);
11503 }
11504 return right;
11505 }
11506
11507 /* Substitute into a unary right fold expression. */
11508
11509 static tree
11510 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11511 tree in_decl)
11512 {
11513 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11514 if (pack == error_mark_node)
11515 return error_mark_node;
11516 if (PACK_EXPANSION_P (pack))
11517 {
11518 tree r = copy_node (t);
11519 FOLD_EXPR_PACK (r) = pack;
11520 return r;
11521 }
11522 if (TREE_VEC_LENGTH (pack) == 0)
11523 return expand_empty_fold (t, complain);
11524 else
11525 return expand_right_fold (t, pack, complain);
11526 }
11527
11528 /* Substitute into a binary right fold expression.
11529
11530 Do ths by building a single (non-empty) vector of arguments and
11531 building the expression from those elements. */
11532
11533 static tree
11534 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11535 tree in_decl)
11536 {
11537 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11538 if (pack == error_mark_node)
11539 return error_mark_node;
11540 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11541 if (init == error_mark_node)
11542 return error_mark_node;
11543
11544 if (PACK_EXPANSION_P (pack))
11545 {
11546 tree r = copy_node (t);
11547 FOLD_EXPR_PACK (r) = pack;
11548 FOLD_EXPR_INIT (r) = init;
11549 return r;
11550 }
11551
11552 int n = TREE_VEC_LENGTH (pack);
11553 tree vec = make_tree_vec (n + 1);
11554 for (int i = 0; i < n; ++i)
11555 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11556 TREE_VEC_ELT (vec, n) = init;
11557
11558 return expand_right_fold (t, vec, complain);
11559 }
11560
11561 /* Walk through the pattern of a pack expansion, adding everything in
11562 local_specializations to a list. */
11563
11564 struct el_data
11565 {
11566 tree extra;
11567 tsubst_flags_t complain;
11568 };
11569 static tree
11570 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11571 {
11572 el_data &data = *reinterpret_cast<el_data*>(data_);
11573 tree *extra = &data.extra;
11574 tsubst_flags_t complain = data.complain;
11575 if (tree spec = retrieve_local_specialization (*tp))
11576 {
11577 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11578 {
11579 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11580 tree args = ARGUMENT_PACK_ARGS (spec);
11581 if (TREE_VEC_LENGTH (args) == 1)
11582 {
11583 tree elt = TREE_VEC_ELT (args, 0);
11584 if (PACK_EXPANSION_P (elt))
11585 elt = PACK_EXPANSION_PATTERN (elt);
11586 if (DECL_PACK_P (elt))
11587 spec = elt;
11588 }
11589 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11590 {
11591 /* Handle lambda capture here, since we aren't doing any
11592 substitution now, and so tsubst_copy won't call
11593 process_outer_var_ref. */
11594 tree args = ARGUMENT_PACK_ARGS (spec);
11595 int len = TREE_VEC_LENGTH (args);
11596 for (int i = 0; i < len; ++i)
11597 {
11598 tree arg = TREE_VEC_ELT (args, i);
11599 tree carg = arg;
11600 if (outer_automatic_var_p (arg))
11601 carg = process_outer_var_ref (arg, complain);
11602 if (carg != arg)
11603 {
11604 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11605 proxies. */
11606 if (i == 0)
11607 {
11608 spec = copy_node (spec);
11609 args = copy_node (args);
11610 SET_ARGUMENT_PACK_ARGS (spec, args);
11611 register_local_specialization (spec, *tp);
11612 }
11613 TREE_VEC_ELT (args, i) = carg;
11614 }
11615 }
11616 }
11617 }
11618 if (outer_automatic_var_p (spec))
11619 spec = process_outer_var_ref (spec, complain);
11620 *extra = tree_cons (*tp, spec, *extra);
11621 }
11622 return NULL_TREE;
11623 }
11624 static tree
11625 extract_local_specs (tree pattern, tsubst_flags_t complain)
11626 {
11627 el_data data = { NULL_TREE, complain };
11628 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
11629 return data.extra;
11630 }
11631
11632 /* Substitute ARGS into T, which is an pack expansion
11633 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11634 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11635 (if only a partial substitution could be performed) or
11636 ERROR_MARK_NODE if there was an error. */
11637 tree
11638 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11639 tree in_decl)
11640 {
11641 tree pattern;
11642 tree pack, packs = NULL_TREE;
11643 bool unsubstituted_packs = false;
11644 bool unsubstituted_fn_pack = false;
11645 int i, len = -1;
11646 tree result;
11647 hash_map<tree, tree> *saved_local_specializations = NULL;
11648 bool need_local_specializations = false;
11649 int levels;
11650
11651 gcc_assert (PACK_EXPANSION_P (t));
11652 pattern = PACK_EXPANSION_PATTERN (t);
11653
11654 /* Add in any args remembered from an earlier partial instantiation. */
11655 tree extra = PACK_EXPANSION_EXTRA_ARGS (t);
11656 if (extra && TREE_CODE (extra) == TREE_LIST)
11657 {
11658 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
11659 {
11660 /* The partial instantiation involved local declarations collected in
11661 extract_local_specs; map from the general template to our local
11662 context. */
11663 tree gen = TREE_PURPOSE (elt);
11664 tree inst = TREE_VALUE (elt);
11665 if (DECL_P (inst))
11666 if (tree local = retrieve_local_specialization (inst))
11667 inst = local;
11668 /* else inst is already a full instantiation of the pack. */
11669 register_local_specialization (inst, gen);
11670 }
11671 gcc_assert (!TREE_PURPOSE (extra));
11672 extra = TREE_VALUE (extra);
11673 }
11674 args = add_to_template_args (extra, args);
11675
11676 levels = TMPL_ARGS_DEPTH (args);
11677
11678 /* Determine the argument packs that will instantiate the parameter
11679 packs used in the expansion expression. While we're at it,
11680 compute the number of arguments to be expanded and make sure it
11681 is consistent. */
11682 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11683 pack = TREE_CHAIN (pack))
11684 {
11685 tree parm_pack = TREE_VALUE (pack);
11686 tree arg_pack = NULL_TREE;
11687 tree orig_arg = NULL_TREE;
11688 int level = 0;
11689
11690 if (TREE_CODE (parm_pack) == BASES)
11691 {
11692 gcc_assert (parm_pack == pattern);
11693 if (BASES_DIRECT (parm_pack))
11694 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11695 args, complain, in_decl, false));
11696 else
11697 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11698 args, complain, in_decl, false));
11699 }
11700 else if (builtin_pack_call_p (parm_pack))
11701 {
11702 /* ??? Support use in other patterns. */
11703 gcc_assert (parm_pack == pattern);
11704 return expand_builtin_pack_call (parm_pack, args,
11705 complain, in_decl);
11706 }
11707 else if (TREE_CODE (parm_pack) == PARM_DECL)
11708 {
11709 /* We know we have correct local_specializations if this
11710 expansion is at function scope, or if we're dealing with a
11711 local parameter in a requires expression; for the latter,
11712 tsubst_requires_expr set it up appropriately. */
11713 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11714 arg_pack = retrieve_local_specialization (parm_pack);
11715 else
11716 /* We can't rely on local_specializations for a parameter
11717 name used later in a function declaration (such as in a
11718 late-specified return type). Even if it exists, it might
11719 have the wrong value for a recursive call. */
11720 need_local_specializations = true;
11721
11722 if (!arg_pack)
11723 {
11724 /* This parameter pack was used in an unevaluated context. Just
11725 make a dummy decl, since it's only used for its type. */
11726 ++cp_unevaluated_operand;
11727 arg_pack = tsubst_decl (parm_pack, args, complain);
11728 --cp_unevaluated_operand;
11729 if (arg_pack && DECL_PACK_P (arg_pack))
11730 /* Partial instantiation of the parm_pack, we can't build
11731 up an argument pack yet. */
11732 arg_pack = NULL_TREE;
11733 else
11734 arg_pack = make_fnparm_pack (arg_pack);
11735 }
11736 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
11737 /* This argument pack isn't fully instantiated yet. We set this
11738 flag rather than clear arg_pack because we do want to do the
11739 optimization below, and we don't want to substitute directly
11740 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11741 where it isn't expected). */
11742 unsubstituted_fn_pack = true;
11743 }
11744 else if (is_normal_capture_proxy (parm_pack))
11745 {
11746 arg_pack = retrieve_local_specialization (parm_pack);
11747 if (argument_pack_element_is_expansion_p (arg_pack, 0))
11748 unsubstituted_fn_pack = true;
11749 }
11750 else
11751 {
11752 int idx;
11753 template_parm_level_and_index (parm_pack, &level, &idx);
11754
11755 if (level <= levels)
11756 arg_pack = TMPL_ARG (args, level, idx);
11757 }
11758
11759 orig_arg = arg_pack;
11760 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11761 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11762
11763 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11764 /* This can only happen if we forget to expand an argument
11765 pack somewhere else. Just return an error, silently. */
11766 {
11767 result = make_tree_vec (1);
11768 TREE_VEC_ELT (result, 0) = error_mark_node;
11769 return result;
11770 }
11771
11772 if (arg_pack)
11773 {
11774 int my_len =
11775 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11776
11777 /* Don't bother trying to do a partial substitution with
11778 incomplete packs; we'll try again after deduction. */
11779 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11780 return t;
11781
11782 if (len < 0)
11783 len = my_len;
11784 else if (len != my_len
11785 && !unsubstituted_fn_pack)
11786 {
11787 if (!(complain & tf_error))
11788 /* Fail quietly. */;
11789 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11790 error ("mismatched argument pack lengths while expanding %qT",
11791 pattern);
11792 else
11793 error ("mismatched argument pack lengths while expanding %qE",
11794 pattern);
11795 return error_mark_node;
11796 }
11797
11798 /* Keep track of the parameter packs and their corresponding
11799 argument packs. */
11800 packs = tree_cons (parm_pack, arg_pack, packs);
11801 TREE_TYPE (packs) = orig_arg;
11802 }
11803 else
11804 {
11805 /* We can't substitute for this parameter pack. We use a flag as
11806 well as the missing_level counter because function parameter
11807 packs don't have a level. */
11808 gcc_assert (processing_template_decl);
11809 unsubstituted_packs = true;
11810 }
11811 }
11812
11813 /* If the expansion is just T..., return the matching argument pack, unless
11814 we need to call convert_from_reference on all the elements. This is an
11815 important optimization; see c++/68422. */
11816 if (!unsubstituted_packs
11817 && TREE_PURPOSE (packs) == pattern)
11818 {
11819 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11820
11821 /* If the argument pack is a single pack expansion, pull it out. */
11822 if (TREE_VEC_LENGTH (args) == 1
11823 && pack_expansion_args_count (args))
11824 return TREE_VEC_ELT (args, 0);
11825
11826 /* Types need no adjustment, nor does sizeof..., and if we still have
11827 some pack expansion args we won't do anything yet. */
11828 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11829 || PACK_EXPANSION_SIZEOF_P (t)
11830 || pack_expansion_args_count (args))
11831 return args;
11832 /* Also optimize expression pack expansions if we can tell that the
11833 elements won't have reference type. */
11834 tree type = TREE_TYPE (pattern);
11835 if (type && TREE_CODE (type) != REFERENCE_TYPE
11836 && !PACK_EXPANSION_P (type)
11837 && !WILDCARD_TYPE_P (type))
11838 return args;
11839 /* Otherwise use the normal path so we get convert_from_reference. */
11840 }
11841
11842 /* We cannot expand this expansion expression, because we don't have
11843 all of the argument packs we need. */
11844 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11845 {
11846 /* We got some full packs, but we can't substitute them in until we
11847 have values for all the packs. So remember these until then. */
11848
11849 t = make_pack_expansion (pattern, complain);
11850 tree extra = args;
11851 if (local_specializations)
11852 if (tree locals = extract_local_specs (pattern, complain))
11853 extra = tree_cons (NULL_TREE, extra, locals);
11854 PACK_EXPANSION_EXTRA_ARGS (t) = extra;
11855 return t;
11856 }
11857 else if (unsubstituted_packs)
11858 {
11859 /* There were no real arguments, we're just replacing a parameter
11860 pack with another version of itself. Substitute into the
11861 pattern and return a PACK_EXPANSION_*. The caller will need to
11862 deal with that. */
11863 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11864 t = tsubst_expr (pattern, args, complain, in_decl,
11865 /*integral_constant_expression_p=*/false);
11866 else
11867 t = tsubst (pattern, args, complain, in_decl);
11868 t = make_pack_expansion (t, complain);
11869 return t;
11870 }
11871
11872 gcc_assert (len >= 0);
11873
11874 if (need_local_specializations)
11875 {
11876 /* We're in a late-specified return type, so create our own local
11877 specializations map; the current map is either NULL or (in the
11878 case of recursive unification) might have bindings that we don't
11879 want to use or alter. */
11880 saved_local_specializations = local_specializations;
11881 local_specializations = new hash_map<tree, tree>;
11882 }
11883
11884 /* For each argument in each argument pack, substitute into the
11885 pattern. */
11886 result = make_tree_vec (len);
11887 tree elem_args = copy_template_args (args);
11888 for (i = 0; i < len; ++i)
11889 {
11890 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11891 i,
11892 elem_args, complain,
11893 in_decl);
11894 TREE_VEC_ELT (result, i) = t;
11895 if (t == error_mark_node)
11896 {
11897 result = error_mark_node;
11898 break;
11899 }
11900 }
11901
11902 /* Update ARGS to restore the substitution from parameter packs to
11903 their argument packs. */
11904 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11905 {
11906 tree parm = TREE_PURPOSE (pack);
11907
11908 if (TREE_CODE (parm) == PARM_DECL
11909 || VAR_P (parm)
11910 || TREE_CODE (parm) == FIELD_DECL)
11911 register_local_specialization (TREE_TYPE (pack), parm);
11912 else
11913 {
11914 int idx, level;
11915
11916 if (TREE_VALUE (pack) == NULL_TREE)
11917 continue;
11918
11919 template_parm_level_and_index (parm, &level, &idx);
11920
11921 /* Update the corresponding argument. */
11922 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11923 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11924 TREE_TYPE (pack);
11925 else
11926 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11927 }
11928 }
11929
11930 if (need_local_specializations)
11931 {
11932 delete local_specializations;
11933 local_specializations = saved_local_specializations;
11934 }
11935
11936 /* If the dependent pack arguments were such that we end up with only a
11937 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11938 if (len == 1 && TREE_CODE (result) == TREE_VEC
11939 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11940 return TREE_VEC_ELT (result, 0);
11941
11942 return result;
11943 }
11944
11945 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11946 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11947 parameter packs; all parms generated from a function parameter pack will
11948 have the same DECL_PARM_INDEX. */
11949
11950 tree
11951 get_pattern_parm (tree parm, tree tmpl)
11952 {
11953 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11954 tree patparm;
11955
11956 if (DECL_ARTIFICIAL (parm))
11957 {
11958 for (patparm = DECL_ARGUMENTS (pattern);
11959 patparm; patparm = DECL_CHAIN (patparm))
11960 if (DECL_ARTIFICIAL (patparm)
11961 && DECL_NAME (parm) == DECL_NAME (patparm))
11962 break;
11963 }
11964 else
11965 {
11966 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11967 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11968 gcc_assert (DECL_PARM_INDEX (patparm)
11969 == DECL_PARM_INDEX (parm));
11970 }
11971
11972 return patparm;
11973 }
11974
11975 /* Make an argument pack out of the TREE_VEC VEC. */
11976
11977 static tree
11978 make_argument_pack (tree vec)
11979 {
11980 tree pack;
11981 tree elt = TREE_VEC_ELT (vec, 0);
11982 if (TYPE_P (elt))
11983 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11984 else
11985 {
11986 pack = make_node (NONTYPE_ARGUMENT_PACK);
11987 TREE_CONSTANT (pack) = 1;
11988 }
11989 SET_ARGUMENT_PACK_ARGS (pack, vec);
11990 return pack;
11991 }
11992
11993 /* Return an exact copy of template args T that can be modified
11994 independently. */
11995
11996 static tree
11997 copy_template_args (tree t)
11998 {
11999 if (t == error_mark_node)
12000 return t;
12001
12002 int len = TREE_VEC_LENGTH (t);
12003 tree new_vec = make_tree_vec (len);
12004
12005 for (int i = 0; i < len; ++i)
12006 {
12007 tree elt = TREE_VEC_ELT (t, i);
12008 if (elt && TREE_CODE (elt) == TREE_VEC)
12009 elt = copy_template_args (elt);
12010 TREE_VEC_ELT (new_vec, i) = elt;
12011 }
12012
12013 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12014 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12015
12016 return new_vec;
12017 }
12018
12019 /* Substitute ARGS into the vector or list of template arguments T. */
12020
12021 static tree
12022 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12023 {
12024 tree orig_t = t;
12025 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12026 tree *elts;
12027
12028 if (t == error_mark_node)
12029 return error_mark_node;
12030
12031 len = TREE_VEC_LENGTH (t);
12032 elts = XALLOCAVEC (tree, len);
12033
12034 for (i = 0; i < len; i++)
12035 {
12036 tree orig_arg = TREE_VEC_ELT (t, i);
12037 tree new_arg;
12038
12039 if (TREE_CODE (orig_arg) == TREE_VEC)
12040 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12041 else if (PACK_EXPANSION_P (orig_arg))
12042 {
12043 /* Substitute into an expansion expression. */
12044 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12045
12046 if (TREE_CODE (new_arg) == TREE_VEC)
12047 /* Add to the expanded length adjustment the number of
12048 expanded arguments. We subtract one from this
12049 measurement, because the argument pack expression
12050 itself is already counted as 1 in
12051 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12052 the argument pack is empty. */
12053 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12054 }
12055 else if (ARGUMENT_PACK_P (orig_arg))
12056 {
12057 /* Substitute into each of the arguments. */
12058 new_arg = TYPE_P (orig_arg)
12059 ? cxx_make_type (TREE_CODE (orig_arg))
12060 : make_node (TREE_CODE (orig_arg));
12061
12062 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12063 args, complain, in_decl);
12064 if (pack_args == error_mark_node)
12065 new_arg = error_mark_node;
12066 else
12067 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12068
12069 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12070 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12071 }
12072 else
12073 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12074
12075 if (new_arg == error_mark_node)
12076 return error_mark_node;
12077
12078 elts[i] = new_arg;
12079 if (new_arg != orig_arg)
12080 need_new = 1;
12081 }
12082
12083 if (!need_new)
12084 return t;
12085
12086 /* Make space for the expanded arguments coming from template
12087 argument packs. */
12088 t = make_tree_vec (len + expanded_len_adjust);
12089 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12090 arguments for a member template.
12091 In that case each TREE_VEC in ORIG_T represents a level of template
12092 arguments, and ORIG_T won't carry any non defaulted argument count.
12093 It will rather be the nested TREE_VECs that will carry one.
12094 In other words, ORIG_T carries a non defaulted argument count only
12095 if it doesn't contain any nested TREE_VEC. */
12096 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12097 {
12098 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12099 count += expanded_len_adjust;
12100 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12101 }
12102 for (i = 0, out = 0; i < len; i++)
12103 {
12104 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12105 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12106 && TREE_CODE (elts[i]) == TREE_VEC)
12107 {
12108 int idx;
12109
12110 /* Now expand the template argument pack "in place". */
12111 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12112 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12113 }
12114 else
12115 {
12116 TREE_VEC_ELT (t, out) = elts[i];
12117 out++;
12118 }
12119 }
12120
12121 return t;
12122 }
12123
12124 /* Substitute ARGS into one level PARMS of template parameters. */
12125
12126 static tree
12127 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12128 {
12129 if (parms == error_mark_node)
12130 return error_mark_node;
12131
12132 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12133
12134 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12135 {
12136 tree tuple = TREE_VEC_ELT (parms, i);
12137
12138 if (tuple == error_mark_node)
12139 continue;
12140
12141 TREE_VEC_ELT (new_vec, i) =
12142 tsubst_template_parm (tuple, args, complain);
12143 }
12144
12145 return new_vec;
12146 }
12147
12148 /* Return the result of substituting ARGS into the template parameters
12149 given by PARMS. If there are m levels of ARGS and m + n levels of
12150 PARMS, then the result will contain n levels of PARMS. For
12151 example, if PARMS is `template <class T> template <class U>
12152 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12153 result will be `template <int*, double, class V>'. */
12154
12155 static tree
12156 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12157 {
12158 tree r = NULL_TREE;
12159 tree* new_parms;
12160
12161 /* When substituting into a template, we must set
12162 PROCESSING_TEMPLATE_DECL as the template parameters may be
12163 dependent if they are based on one-another, and the dependency
12164 predicates are short-circuit outside of templates. */
12165 ++processing_template_decl;
12166
12167 for (new_parms = &r;
12168 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12169 new_parms = &(TREE_CHAIN (*new_parms)),
12170 parms = TREE_CHAIN (parms))
12171 {
12172 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12173 args, complain);
12174 *new_parms =
12175 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12176 - TMPL_ARGS_DEPTH (args)),
12177 new_vec, NULL_TREE);
12178 }
12179
12180 --processing_template_decl;
12181
12182 return r;
12183 }
12184
12185 /* Return the result of substituting ARGS into one template parameter
12186 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12187 parameter and which TREE_PURPOSE is the default argument of the
12188 template parameter. */
12189
12190 static tree
12191 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12192 {
12193 tree default_value, parm_decl;
12194
12195 if (args == NULL_TREE
12196 || t == NULL_TREE
12197 || t == error_mark_node)
12198 return t;
12199
12200 gcc_assert (TREE_CODE (t) == TREE_LIST);
12201
12202 default_value = TREE_PURPOSE (t);
12203 parm_decl = TREE_VALUE (t);
12204
12205 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12206 if (TREE_CODE (parm_decl) == PARM_DECL
12207 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12208 parm_decl = error_mark_node;
12209 default_value = tsubst_template_arg (default_value, args,
12210 complain, NULL_TREE);
12211
12212 return build_tree_list (default_value, parm_decl);
12213 }
12214
12215 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12216 type T. If T is not an aggregate or enumeration type, it is
12217 handled as if by tsubst. IN_DECL is as for tsubst. If
12218 ENTERING_SCOPE is nonzero, T is the context for a template which
12219 we are presently tsubst'ing. Return the substituted value. */
12220
12221 static tree
12222 tsubst_aggr_type (tree t,
12223 tree args,
12224 tsubst_flags_t complain,
12225 tree in_decl,
12226 int entering_scope)
12227 {
12228 if (t == NULL_TREE)
12229 return NULL_TREE;
12230
12231 switch (TREE_CODE (t))
12232 {
12233 case RECORD_TYPE:
12234 if (TYPE_PTRMEMFUNC_P (t))
12235 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12236
12237 /* Fall through. */
12238 case ENUMERAL_TYPE:
12239 case UNION_TYPE:
12240 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12241 {
12242 tree argvec;
12243 tree context;
12244 tree r;
12245 int saved_unevaluated_operand;
12246 int saved_inhibit_evaluation_warnings;
12247
12248 /* In "sizeof(X<I>)" we need to evaluate "I". */
12249 saved_unevaluated_operand = cp_unevaluated_operand;
12250 cp_unevaluated_operand = 0;
12251 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12252 c_inhibit_evaluation_warnings = 0;
12253
12254 /* First, determine the context for the type we are looking
12255 up. */
12256 context = TYPE_CONTEXT (t);
12257 if (context && TYPE_P (context))
12258 {
12259 context = tsubst_aggr_type (context, args, complain,
12260 in_decl, /*entering_scope=*/1);
12261 /* If context is a nested class inside a class template,
12262 it may still need to be instantiated (c++/33959). */
12263 context = complete_type (context);
12264 }
12265
12266 /* Then, figure out what arguments are appropriate for the
12267 type we are trying to find. For example, given:
12268
12269 template <class T> struct S;
12270 template <class T, class U> void f(T, U) { S<U> su; }
12271
12272 and supposing that we are instantiating f<int, double>,
12273 then our ARGS will be {int, double}, but, when looking up
12274 S we only want {double}. */
12275 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12276 complain, in_decl);
12277 if (argvec == error_mark_node)
12278 r = error_mark_node;
12279 else
12280 {
12281 r = lookup_template_class (t, argvec, in_decl, context,
12282 entering_scope, complain);
12283 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12284 }
12285
12286 cp_unevaluated_operand = saved_unevaluated_operand;
12287 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12288
12289 return r;
12290 }
12291 else
12292 /* This is not a template type, so there's nothing to do. */
12293 return t;
12294
12295 default:
12296 return tsubst (t, args, complain, in_decl);
12297 }
12298 }
12299
12300 static GTY((cache)) tree_cache_map *defarg_inst;
12301
12302 /* Substitute into the default argument ARG (a default argument for
12303 FN), which has the indicated TYPE. */
12304
12305 tree
12306 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12307 tsubst_flags_t complain)
12308 {
12309 tree saved_class_ptr = NULL_TREE;
12310 tree saved_class_ref = NULL_TREE;
12311 int errs = errorcount + sorrycount;
12312
12313 /* This can happen in invalid code. */
12314 if (TREE_CODE (arg) == DEFAULT_ARG)
12315 return arg;
12316
12317 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12318 parm = chain_index (parmnum, parm);
12319 tree parmtype = TREE_TYPE (parm);
12320 if (DECL_BY_REFERENCE (parm))
12321 parmtype = TREE_TYPE (parmtype);
12322 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12323
12324 tree *slot;
12325 if (defarg_inst && (slot = defarg_inst->get (parm)))
12326 return *slot;
12327
12328 /* This default argument came from a template. Instantiate the
12329 default argument here, not in tsubst. In the case of
12330 something like:
12331
12332 template <class T>
12333 struct S {
12334 static T t();
12335 void f(T = t());
12336 };
12337
12338 we must be careful to do name lookup in the scope of S<T>,
12339 rather than in the current class. */
12340 push_access_scope (fn);
12341 /* The "this" pointer is not valid in a default argument. */
12342 if (cfun)
12343 {
12344 saved_class_ptr = current_class_ptr;
12345 cp_function_chain->x_current_class_ptr = NULL_TREE;
12346 saved_class_ref = current_class_ref;
12347 cp_function_chain->x_current_class_ref = NULL_TREE;
12348 }
12349
12350 start_lambda_scope (parm);
12351
12352 push_deferring_access_checks(dk_no_deferred);
12353 /* The default argument expression may cause implicitly defined
12354 member functions to be synthesized, which will result in garbage
12355 collection. We must treat this situation as if we were within
12356 the body of function so as to avoid collecting live data on the
12357 stack. */
12358 ++function_depth;
12359 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12360 complain, NULL_TREE,
12361 /*integral_constant_expression_p=*/false);
12362 --function_depth;
12363 pop_deferring_access_checks();
12364
12365 finish_lambda_scope ();
12366
12367 /* Restore the "this" pointer. */
12368 if (cfun)
12369 {
12370 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12371 cp_function_chain->x_current_class_ref = saved_class_ref;
12372 }
12373
12374 if (errorcount+sorrycount > errs
12375 && (complain & tf_warning_or_error))
12376 inform (input_location,
12377 " when instantiating default argument for call to %qD", fn);
12378
12379 /* Make sure the default argument is reasonable. */
12380 arg = check_default_argument (type, arg, complain);
12381
12382 pop_access_scope (fn);
12383
12384 if (arg != error_mark_node && !cp_unevaluated_operand)
12385 {
12386 if (!defarg_inst)
12387 defarg_inst = tree_cache_map::create_ggc (37);
12388 defarg_inst->put (parm, arg);
12389 }
12390
12391 return arg;
12392 }
12393
12394 /* Substitute into all the default arguments for FN. */
12395
12396 static void
12397 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12398 {
12399 tree arg;
12400 tree tmpl_args;
12401
12402 tmpl_args = DECL_TI_ARGS (fn);
12403
12404 /* If this function is not yet instantiated, we certainly don't need
12405 its default arguments. */
12406 if (uses_template_parms (tmpl_args))
12407 return;
12408 /* Don't do this again for clones. */
12409 if (DECL_CLONED_FUNCTION_P (fn))
12410 return;
12411
12412 int i = 0;
12413 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12414 arg;
12415 arg = TREE_CHAIN (arg), ++i)
12416 if (TREE_PURPOSE (arg))
12417 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12418 TREE_VALUE (arg),
12419 TREE_PURPOSE (arg),
12420 complain);
12421 }
12422
12423 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12424
12425 static tree
12426 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12427 tree lambda_fntype)
12428 {
12429 tree gen_tmpl, argvec;
12430 hashval_t hash = 0;
12431 tree in_decl = t;
12432
12433 /* Nobody should be tsubst'ing into non-template functions. */
12434 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12435
12436 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12437 {
12438 /* If T is not dependent, just return it. */
12439 if (!uses_template_parms (DECL_TI_ARGS (t)))
12440 return t;
12441
12442 /* Calculate the most general template of which R is a
12443 specialization. */
12444 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12445
12446 /* We're substituting a lambda function under tsubst_lambda_expr but not
12447 directly from it; find the matching function we're already inside.
12448 But don't do this if T is a generic lambda with a single level of
12449 template parms, as in that case we're doing a normal instantiation. */
12450 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12451 && (!generic_lambda_fn_p (t)
12452 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12453 return enclosing_instantiation_of (t);
12454
12455 /* Calculate the complete set of arguments used to
12456 specialize R. */
12457 argvec = tsubst_template_args (DECL_TI_ARGS
12458 (DECL_TEMPLATE_RESULT
12459 (DECL_TI_TEMPLATE (t))),
12460 args, complain, in_decl);
12461 if (argvec == error_mark_node)
12462 return error_mark_node;
12463
12464 /* Check to see if we already have this specialization. */
12465 if (!lambda_fntype)
12466 {
12467 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12468 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12469 return spec;
12470 }
12471
12472 /* We can see more levels of arguments than parameters if
12473 there was a specialization of a member template, like
12474 this:
12475
12476 template <class T> struct S { template <class U> void f(); }
12477 template <> template <class U> void S<int>::f(U);
12478
12479 Here, we'll be substituting into the specialization,
12480 because that's where we can find the code we actually
12481 want to generate, but we'll have enough arguments for
12482 the most general template.
12483
12484 We also deal with the peculiar case:
12485
12486 template <class T> struct S {
12487 template <class U> friend void f();
12488 };
12489 template <class U> void f() {}
12490 template S<int>;
12491 template void f<double>();
12492
12493 Here, the ARGS for the instantiation of will be {int,
12494 double}. But, we only need as many ARGS as there are
12495 levels of template parameters in CODE_PATTERN. We are
12496 careful not to get fooled into reducing the ARGS in
12497 situations like:
12498
12499 template <class T> struct S { template <class U> void f(U); }
12500 template <class T> template <> void S<T>::f(int) {}
12501
12502 which we can spot because the pattern will be a
12503 specialization in this case. */
12504 int args_depth = TMPL_ARGS_DEPTH (args);
12505 int parms_depth =
12506 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12507
12508 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12509 args = get_innermost_template_args (args, parms_depth);
12510 }
12511 else
12512 {
12513 /* This special case arises when we have something like this:
12514
12515 template <class T> struct S {
12516 friend void f<int>(int, double);
12517 };
12518
12519 Here, the DECL_TI_TEMPLATE for the friend declaration
12520 will be an IDENTIFIER_NODE. We are being called from
12521 tsubst_friend_function, and we want only to create a
12522 new decl (R) with appropriate types so that we can call
12523 determine_specialization. */
12524 gen_tmpl = NULL_TREE;
12525 argvec = NULL_TREE;
12526 }
12527
12528 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12529 : NULL_TREE);
12530 tree ctx = closure ? closure : DECL_CONTEXT (t);
12531 bool member = ctx && TYPE_P (ctx);
12532
12533 if (member && !closure)
12534 ctx = tsubst_aggr_type (ctx, args,
12535 complain, t, /*entering_scope=*/1);
12536
12537 tree type = (lambda_fntype ? lambda_fntype
12538 : tsubst (TREE_TYPE (t), args,
12539 complain | tf_fndecl_type, in_decl));
12540 if (type == error_mark_node)
12541 return error_mark_node;
12542
12543 /* If we hit excessive deduction depth, the type is bogus even if
12544 it isn't error_mark_node, so don't build a decl. */
12545 if (excessive_deduction_depth)
12546 return error_mark_node;
12547
12548 /* We do NOT check for matching decls pushed separately at this
12549 point, as they may not represent instantiations of this
12550 template, and in any case are considered separate under the
12551 discrete model. */
12552 tree r = copy_decl (t);
12553 DECL_USE_TEMPLATE (r) = 0;
12554 TREE_TYPE (r) = type;
12555 /* Clear out the mangled name and RTL for the instantiation. */
12556 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12557 SET_DECL_RTL (r, NULL);
12558 /* Leave DECL_INITIAL set on deleted instantiations. */
12559 if (!DECL_DELETED_FN (r))
12560 DECL_INITIAL (r) = NULL_TREE;
12561 DECL_CONTEXT (r) = ctx;
12562
12563 /* OpenMP UDRs have the only argument a reference to the declared
12564 type. We want to diagnose if the declared type is a reference,
12565 which is invalid, but as references to references are usually
12566 quietly merged, diagnose it here. */
12567 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12568 {
12569 tree argtype
12570 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12571 argtype = tsubst (argtype, args, complain, in_decl);
12572 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12573 error_at (DECL_SOURCE_LOCATION (t),
12574 "reference type %qT in "
12575 "%<#pragma omp declare reduction%>", argtype);
12576 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12577 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12578 argtype);
12579 }
12580
12581 if (member && DECL_CONV_FN_P (r))
12582 /* Type-conversion operator. Reconstruct the name, in
12583 case it's the name of one of the template's parameters. */
12584 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12585
12586 tree parms = DECL_ARGUMENTS (t);
12587 if (closure)
12588 parms = DECL_CHAIN (parms);
12589 parms = tsubst (parms, args, complain, t);
12590 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12591 DECL_CONTEXT (parm) = r;
12592 if (closure)
12593 {
12594 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12595 DECL_CHAIN (tparm) = parms;
12596 parms = tparm;
12597 }
12598 DECL_ARGUMENTS (r) = parms;
12599 DECL_RESULT (r) = NULL_TREE;
12600
12601 TREE_STATIC (r) = 0;
12602 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12603 DECL_EXTERNAL (r) = 1;
12604 /* If this is an instantiation of a function with internal
12605 linkage, we already know what object file linkage will be
12606 assigned to the instantiation. */
12607 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12608 DECL_DEFER_OUTPUT (r) = 0;
12609 DECL_CHAIN (r) = NULL_TREE;
12610 DECL_PENDING_INLINE_INFO (r) = 0;
12611 DECL_PENDING_INLINE_P (r) = 0;
12612 DECL_SAVED_TREE (r) = NULL_TREE;
12613 DECL_STRUCT_FUNCTION (r) = NULL;
12614 TREE_USED (r) = 0;
12615 /* We'll re-clone as appropriate in instantiate_template. */
12616 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12617
12618 /* If we aren't complaining now, return on error before we register
12619 the specialization so that we'll complain eventually. */
12620 if ((complain & tf_error) == 0
12621 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12622 && !grok_op_properties (r, /*complain=*/false))
12623 return error_mark_node;
12624
12625 /* When instantiating a constrained member, substitute
12626 into the constraints to create a new constraint. */
12627 if (tree ci = get_constraints (t))
12628 if (member)
12629 {
12630 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12631 set_constraints (r, ci);
12632 }
12633
12634 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12635 this in the special friend case mentioned above where
12636 GEN_TMPL is NULL. */
12637 if (gen_tmpl && !closure)
12638 {
12639 DECL_TEMPLATE_INFO (r)
12640 = build_template_info (gen_tmpl, argvec);
12641 SET_DECL_IMPLICIT_INSTANTIATION (r);
12642
12643 tree new_r
12644 = register_specialization (r, gen_tmpl, argvec, false, hash);
12645 if (new_r != r)
12646 /* We instantiated this while substituting into
12647 the type earlier (template/friend54.C). */
12648 return new_r;
12649
12650 /* We're not supposed to instantiate default arguments
12651 until they are called, for a template. But, for a
12652 declaration like:
12653
12654 template <class T> void f ()
12655 { extern void g(int i = T()); }
12656
12657 we should do the substitution when the template is
12658 instantiated. We handle the member function case in
12659 instantiate_class_template since the default arguments
12660 might refer to other members of the class. */
12661 if (!member
12662 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12663 && !uses_template_parms (argvec))
12664 tsubst_default_arguments (r, complain);
12665 }
12666 else
12667 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12668
12669 /* Copy the list of befriending classes. */
12670 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12671 *friends;
12672 friends = &TREE_CHAIN (*friends))
12673 {
12674 *friends = copy_node (*friends);
12675 TREE_VALUE (*friends)
12676 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12677 }
12678
12679 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12680 {
12681 maybe_retrofit_in_chrg (r);
12682 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12683 return error_mark_node;
12684 /* If this is an instantiation of a member template, clone it.
12685 If it isn't, that'll be handled by
12686 clone_constructors_and_destructors. */
12687 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12688 clone_function_decl (r, /*update_methods=*/false);
12689 }
12690 else if ((complain & tf_error) != 0
12691 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12692 && !grok_op_properties (r, /*complain=*/true))
12693 return error_mark_node;
12694
12695 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12696 SET_DECL_FRIEND_CONTEXT (r,
12697 tsubst (DECL_FRIEND_CONTEXT (t),
12698 args, complain, in_decl));
12699
12700 /* Possibly limit visibility based on template args. */
12701 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12702 if (DECL_VISIBILITY_SPECIFIED (t))
12703 {
12704 DECL_VISIBILITY_SPECIFIED (r) = 0;
12705 DECL_ATTRIBUTES (r)
12706 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12707 }
12708 determine_visibility (r);
12709 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12710 && !processing_template_decl)
12711 defaulted_late_check (r);
12712
12713 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12714 args, complain, in_decl);
12715 return r;
12716 }
12717
12718 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12719
12720 static tree
12721 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12722 tree lambda_fntype)
12723 {
12724 /* We can get here when processing a member function template,
12725 member class template, or template template parameter. */
12726 tree decl = DECL_TEMPLATE_RESULT (t);
12727 tree in_decl = t;
12728 tree spec;
12729 tree tmpl_args;
12730 tree full_args;
12731 tree r;
12732 hashval_t hash = 0;
12733
12734 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12735 {
12736 /* Template template parameter is treated here. */
12737 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12738 if (new_type == error_mark_node)
12739 r = error_mark_node;
12740 /* If we get a real template back, return it. This can happen in
12741 the context of most_specialized_partial_spec. */
12742 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12743 r = new_type;
12744 else
12745 /* The new TEMPLATE_DECL was built in
12746 reduce_template_parm_level. */
12747 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12748 return r;
12749 }
12750
12751 if (!lambda_fntype)
12752 {
12753 /* We might already have an instance of this template.
12754 The ARGS are for the surrounding class type, so the
12755 full args contain the tsubst'd args for the context,
12756 plus the innermost args from the template decl. */
12757 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12758 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12759 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12760 /* Because this is a template, the arguments will still be
12761 dependent, even after substitution. If
12762 PROCESSING_TEMPLATE_DECL is not set, the dependency
12763 predicates will short-circuit. */
12764 ++processing_template_decl;
12765 full_args = tsubst_template_args (tmpl_args, args,
12766 complain, in_decl);
12767 --processing_template_decl;
12768 if (full_args == error_mark_node)
12769 return error_mark_node;
12770
12771 /* If this is a default template template argument,
12772 tsubst might not have changed anything. */
12773 if (full_args == tmpl_args)
12774 return t;
12775
12776 hash = hash_tmpl_and_args (t, full_args);
12777 spec = retrieve_specialization (t, full_args, hash);
12778 if (spec != NULL_TREE)
12779 return spec;
12780 }
12781
12782 /* Make a new template decl. It will be similar to the
12783 original, but will record the current template arguments.
12784 We also create a new function declaration, which is just
12785 like the old one, but points to this new template, rather
12786 than the old one. */
12787 r = copy_decl (t);
12788 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12789 DECL_CHAIN (r) = NULL_TREE;
12790
12791 // Build new template info linking to the original template decl.
12792 if (!lambda_fntype)
12793 {
12794 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12795 SET_DECL_IMPLICIT_INSTANTIATION (r);
12796 }
12797 else
12798 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12799
12800 /* The template parameters for this new template are all the
12801 template parameters for the old template, except the
12802 outermost level of parameters. */
12803 DECL_TEMPLATE_PARMS (r)
12804 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12805 complain);
12806
12807 if (TREE_CODE (decl) == TYPE_DECL
12808 && !TYPE_DECL_ALIAS_P (decl))
12809 {
12810 tree new_type;
12811 ++processing_template_decl;
12812 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12813 --processing_template_decl;
12814 if (new_type == error_mark_node)
12815 return error_mark_node;
12816
12817 TREE_TYPE (r) = new_type;
12818 /* For a partial specialization, we need to keep pointing to
12819 the primary template. */
12820 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12821 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12822 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12823 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12824 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12825 }
12826 else
12827 {
12828 tree new_decl;
12829 ++processing_template_decl;
12830 if (TREE_CODE (decl) == FUNCTION_DECL)
12831 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12832 else
12833 new_decl = tsubst (decl, args, complain, in_decl);
12834 --processing_template_decl;
12835 if (new_decl == error_mark_node)
12836 return error_mark_node;
12837
12838 DECL_TEMPLATE_RESULT (r) = new_decl;
12839 TREE_TYPE (r) = TREE_TYPE (new_decl);
12840 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12841 if (lambda_fntype)
12842 {
12843 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12844 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12845 }
12846 else
12847 {
12848 DECL_TI_TEMPLATE (new_decl) = r;
12849 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12850 }
12851 }
12852
12853 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12854 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12855
12856 if (PRIMARY_TEMPLATE_P (t))
12857 DECL_PRIMARY_TEMPLATE (r) = r;
12858
12859 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12860 && !lambda_fntype)
12861 /* Record this non-type partial instantiation. */
12862 register_specialization (r, t,
12863 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12864 false, hash);
12865
12866 return r;
12867 }
12868
12869 /* True if FN is the op() for a lambda in an uninstantiated template. */
12870
12871 bool
12872 lambda_fn_in_template_p (tree fn)
12873 {
12874 if (!fn || !LAMBDA_FUNCTION_P (fn))
12875 return false;
12876 tree closure = DECL_CONTEXT (fn);
12877 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12878 }
12879
12880 /* We're instantiating a variable from template function TCTX. Return the
12881 corresponding current enclosing scope. This gets complicated because lambda
12882 functions in templates are regenerated rather than instantiated, but generic
12883 lambda functions are subsequently instantiated. */
12884
12885 static tree
12886 enclosing_instantiation_of (tree otctx)
12887 {
12888 tree tctx = otctx;
12889 tree fn = current_function_decl;
12890 int lambda_count = 0;
12891
12892 for (; tctx && lambda_fn_in_template_p (tctx);
12893 tctx = decl_function_context (tctx))
12894 ++lambda_count;
12895 for (; fn; fn = decl_function_context (fn))
12896 {
12897 tree ofn = fn;
12898 int flambda_count = 0;
12899 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
12900 fn = decl_function_context (fn))
12901 ++flambda_count;
12902 if (DECL_TEMPLATE_INFO (fn)
12903 ? most_general_template (fn) != most_general_template (tctx)
12904 : fn != tctx)
12905 continue;
12906 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
12907 || DECL_CONV_FN_P (ofn));
12908 return ofn;
12909 }
12910 gcc_unreachable ();
12911 }
12912
12913 /* Substitute the ARGS into the T, which is a _DECL. Return the
12914 result of the substitution. Issue error and warning messages under
12915 control of COMPLAIN. */
12916
12917 static tree
12918 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12919 {
12920 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12921 location_t saved_loc;
12922 tree r = NULL_TREE;
12923 tree in_decl = t;
12924 hashval_t hash = 0;
12925
12926 /* Set the filename and linenumber to improve error-reporting. */
12927 saved_loc = input_location;
12928 input_location = DECL_SOURCE_LOCATION (t);
12929
12930 switch (TREE_CODE (t))
12931 {
12932 case TEMPLATE_DECL:
12933 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12934 break;
12935
12936 case FUNCTION_DECL:
12937 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12938 break;
12939
12940 case PARM_DECL:
12941 {
12942 tree type = NULL_TREE;
12943 int i, len = 1;
12944 tree expanded_types = NULL_TREE;
12945 tree prev_r = NULL_TREE;
12946 tree first_r = NULL_TREE;
12947
12948 if (DECL_PACK_P (t))
12949 {
12950 /* If there is a local specialization that isn't a
12951 parameter pack, it means that we're doing a "simple"
12952 substitution from inside tsubst_pack_expansion. Just
12953 return the local specialization (which will be a single
12954 parm). */
12955 tree spec = retrieve_local_specialization (t);
12956 if (spec
12957 && TREE_CODE (spec) == PARM_DECL
12958 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12959 RETURN (spec);
12960
12961 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12962 the parameters in this function parameter pack. */
12963 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12964 complain, in_decl);
12965 if (TREE_CODE (expanded_types) == TREE_VEC)
12966 {
12967 len = TREE_VEC_LENGTH (expanded_types);
12968
12969 /* Zero-length parameter packs are boring. Just substitute
12970 into the chain. */
12971 if (len == 0)
12972 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12973 TREE_CHAIN (t)));
12974 }
12975 else
12976 {
12977 /* All we did was update the type. Make a note of that. */
12978 type = expanded_types;
12979 expanded_types = NULL_TREE;
12980 }
12981 }
12982
12983 /* Loop through all of the parameters we'll build. When T is
12984 a function parameter pack, LEN is the number of expanded
12985 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12986 r = NULL_TREE;
12987 for (i = 0; i < len; ++i)
12988 {
12989 prev_r = r;
12990 r = copy_node (t);
12991 if (DECL_TEMPLATE_PARM_P (t))
12992 SET_DECL_TEMPLATE_PARM_P (r);
12993
12994 if (expanded_types)
12995 /* We're on the Ith parameter of the function parameter
12996 pack. */
12997 {
12998 /* Get the Ith type. */
12999 type = TREE_VEC_ELT (expanded_types, i);
13000
13001 /* Rename the parameter to include the index. */
13002 DECL_NAME (r)
13003 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13004 }
13005 else if (!type)
13006 /* We're dealing with a normal parameter. */
13007 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13008
13009 type = type_decays_to (type);
13010 TREE_TYPE (r) = type;
13011 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13012
13013 if (DECL_INITIAL (r))
13014 {
13015 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13016 DECL_INITIAL (r) = TREE_TYPE (r);
13017 else
13018 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13019 complain, in_decl);
13020 }
13021
13022 DECL_CONTEXT (r) = NULL_TREE;
13023
13024 if (!DECL_TEMPLATE_PARM_P (r))
13025 DECL_ARG_TYPE (r) = type_passed_as (type);
13026
13027 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13028 args, complain, in_decl);
13029
13030 /* Keep track of the first new parameter we
13031 generate. That's what will be returned to the
13032 caller. */
13033 if (!first_r)
13034 first_r = r;
13035
13036 /* Build a proper chain of parameters when substituting
13037 into a function parameter pack. */
13038 if (prev_r)
13039 DECL_CHAIN (prev_r) = r;
13040 }
13041
13042 /* If cp_unevaluated_operand is set, we're just looking for a
13043 single dummy parameter, so don't keep going. */
13044 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13045 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13046 complain, DECL_CHAIN (t));
13047
13048 /* FIRST_R contains the start of the chain we've built. */
13049 r = first_r;
13050 }
13051 break;
13052
13053 case FIELD_DECL:
13054 {
13055 tree type = NULL_TREE;
13056 tree vec = NULL_TREE;
13057 tree expanded_types = NULL_TREE;
13058 int len = 1;
13059
13060 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13061 {
13062 /* This field is a lambda capture pack. Return a TREE_VEC of
13063 the expanded fields to instantiate_class_template_1. */
13064 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13065 complain, in_decl);
13066 if (TREE_CODE (expanded_types) == TREE_VEC)
13067 {
13068 len = TREE_VEC_LENGTH (expanded_types);
13069 vec = make_tree_vec (len);
13070 }
13071 else
13072 {
13073 /* All we did was update the type. Make a note of that. */
13074 type = expanded_types;
13075 expanded_types = NULL_TREE;
13076 }
13077 }
13078
13079 for (int i = 0; i < len; ++i)
13080 {
13081 r = copy_decl (t);
13082 if (expanded_types)
13083 {
13084 type = TREE_VEC_ELT (expanded_types, i);
13085 DECL_NAME (r)
13086 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13087 }
13088 else if (!type)
13089 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13090
13091 if (type == error_mark_node)
13092 RETURN (error_mark_node);
13093 TREE_TYPE (r) = type;
13094 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13095
13096 if (DECL_C_BIT_FIELD (r))
13097 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13098 number of bits. */
13099 DECL_BIT_FIELD_REPRESENTATIVE (r)
13100 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13101 complain, in_decl,
13102 /*integral_constant_expression_p=*/true);
13103 if (DECL_INITIAL (t))
13104 {
13105 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13106 NSDMI in perform_member_init. Still set DECL_INITIAL
13107 so that we know there is one. */
13108 DECL_INITIAL (r) = void_node;
13109 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13110 retrofit_lang_decl (r);
13111 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13112 }
13113 /* We don't have to set DECL_CONTEXT here; it is set by
13114 finish_member_declaration. */
13115 DECL_CHAIN (r) = NULL_TREE;
13116
13117 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13118 args, complain, in_decl);
13119
13120 if (vec)
13121 TREE_VEC_ELT (vec, i) = r;
13122 }
13123
13124 if (vec)
13125 r = vec;
13126 }
13127 break;
13128
13129 case USING_DECL:
13130 /* We reach here only for member using decls. We also need to check
13131 uses_template_parms because DECL_DEPENDENT_P is not set for a
13132 using-declaration that designates a member of the current
13133 instantiation (c++/53549). */
13134 if (DECL_DEPENDENT_P (t)
13135 || uses_template_parms (USING_DECL_SCOPE (t)))
13136 {
13137 tree scope = USING_DECL_SCOPE (t);
13138 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13139 if (PACK_EXPANSION_P (scope))
13140 {
13141 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13142 int len = TREE_VEC_LENGTH (vec);
13143 r = make_tree_vec (len);
13144 for (int i = 0; i < len; ++i)
13145 {
13146 tree escope = TREE_VEC_ELT (vec, i);
13147 tree elt = do_class_using_decl (escope, name);
13148 if (!elt)
13149 {
13150 r = error_mark_node;
13151 break;
13152 }
13153 else
13154 {
13155 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13156 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13157 }
13158 TREE_VEC_ELT (r, i) = elt;
13159 }
13160 }
13161 else
13162 {
13163 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13164 complain, in_decl);
13165 r = do_class_using_decl (inst_scope, name);
13166 if (!r)
13167 r = error_mark_node;
13168 else
13169 {
13170 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13171 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13172 }
13173 }
13174 }
13175 else
13176 {
13177 r = copy_node (t);
13178 DECL_CHAIN (r) = NULL_TREE;
13179 }
13180 break;
13181
13182 case TYPE_DECL:
13183 case VAR_DECL:
13184 {
13185 tree argvec = NULL_TREE;
13186 tree gen_tmpl = NULL_TREE;
13187 tree spec;
13188 tree tmpl = NULL_TREE;
13189 tree ctx;
13190 tree type = NULL_TREE;
13191 bool local_p;
13192
13193 if (TREE_TYPE (t) == error_mark_node)
13194 RETURN (error_mark_node);
13195
13196 if (TREE_CODE (t) == TYPE_DECL
13197 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13198 {
13199 /* If this is the canonical decl, we don't have to
13200 mess with instantiations, and often we can't (for
13201 typename, template type parms and such). Note that
13202 TYPE_NAME is not correct for the above test if
13203 we've copied the type for a typedef. */
13204 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13205 if (type == error_mark_node)
13206 RETURN (error_mark_node);
13207 r = TYPE_NAME (type);
13208 break;
13209 }
13210
13211 /* Check to see if we already have the specialization we
13212 need. */
13213 spec = NULL_TREE;
13214 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13215 {
13216 /* T is a static data member or namespace-scope entity.
13217 We have to substitute into namespace-scope variables
13218 (not just variable templates) because of cases like:
13219
13220 template <class T> void f() { extern T t; }
13221
13222 where the entity referenced is not known until
13223 instantiation time. */
13224 local_p = false;
13225 ctx = DECL_CONTEXT (t);
13226 if (DECL_CLASS_SCOPE_P (t))
13227 {
13228 ctx = tsubst_aggr_type (ctx, args,
13229 complain,
13230 in_decl, /*entering_scope=*/1);
13231 /* If CTX is unchanged, then T is in fact the
13232 specialization we want. That situation occurs when
13233 referencing a static data member within in its own
13234 class. We can use pointer equality, rather than
13235 same_type_p, because DECL_CONTEXT is always
13236 canonical... */
13237 if (ctx == DECL_CONTEXT (t)
13238 /* ... unless T is a member template; in which
13239 case our caller can be willing to create a
13240 specialization of that template represented
13241 by T. */
13242 && !(DECL_TI_TEMPLATE (t)
13243 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13244 spec = t;
13245 }
13246
13247 if (!spec)
13248 {
13249 tmpl = DECL_TI_TEMPLATE (t);
13250 gen_tmpl = most_general_template (tmpl);
13251 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13252 if (argvec != error_mark_node)
13253 argvec = (coerce_innermost_template_parms
13254 (DECL_TEMPLATE_PARMS (gen_tmpl),
13255 argvec, t, complain,
13256 /*all*/true, /*defarg*/true));
13257 if (argvec == error_mark_node)
13258 RETURN (error_mark_node);
13259 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13260 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13261 }
13262 }
13263 else
13264 {
13265 /* A local variable. */
13266 local_p = true;
13267 /* Subsequent calls to pushdecl will fill this in. */
13268 ctx = NULL_TREE;
13269 /* Unless this is a reference to a static variable from an
13270 enclosing function, in which case we need to fill it in now. */
13271 if (TREE_STATIC (t))
13272 {
13273 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13274 if (fn != current_function_decl)
13275 ctx = fn;
13276 }
13277 spec = retrieve_local_specialization (t);
13278 }
13279 /* If we already have the specialization we need, there is
13280 nothing more to do. */
13281 if (spec)
13282 {
13283 r = spec;
13284 break;
13285 }
13286
13287 /* Create a new node for the specialization we need. */
13288 r = copy_decl (t);
13289 if (type == NULL_TREE)
13290 {
13291 if (is_typedef_decl (t))
13292 type = DECL_ORIGINAL_TYPE (t);
13293 else
13294 type = TREE_TYPE (t);
13295 if (VAR_P (t)
13296 && VAR_HAD_UNKNOWN_BOUND (t)
13297 && type != error_mark_node)
13298 type = strip_array_domain (type);
13299 tree sub_args = args;
13300 if (tree auto_node = type_uses_auto (type))
13301 {
13302 /* Mask off any template args past the variable's context so we
13303 don't replace the auto with an unrelated argument. */
13304 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13305 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13306 if (extra > 0)
13307 /* This should never happen with the new lambda instantiation
13308 model, but keep the handling just in case. */
13309 gcc_assert (!CHECKING_P),
13310 sub_args = strip_innermost_template_args (args, extra);
13311 }
13312 type = tsubst (type, sub_args, complain, in_decl);
13313 }
13314 if (VAR_P (r))
13315 {
13316 /* Even if the original location is out of scope, the
13317 newly substituted one is not. */
13318 DECL_DEAD_FOR_LOCAL (r) = 0;
13319 DECL_INITIALIZED_P (r) = 0;
13320 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13321 if (type == error_mark_node)
13322 RETURN (error_mark_node);
13323 if (TREE_CODE (type) == FUNCTION_TYPE)
13324 {
13325 /* It may seem that this case cannot occur, since:
13326
13327 typedef void f();
13328 void g() { f x; }
13329
13330 declares a function, not a variable. However:
13331
13332 typedef void f();
13333 template <typename T> void g() { T t; }
13334 template void g<f>();
13335
13336 is an attempt to declare a variable with function
13337 type. */
13338 error ("variable %qD has function type",
13339 /* R is not yet sufficiently initialized, so we
13340 just use its name. */
13341 DECL_NAME (r));
13342 RETURN (error_mark_node);
13343 }
13344 type = complete_type (type);
13345 /* Wait until cp_finish_decl to set this again, to handle
13346 circular dependency (template/instantiate6.C). */
13347 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13348 type = check_var_type (DECL_NAME (r), type);
13349
13350 if (DECL_HAS_VALUE_EXPR_P (t))
13351 {
13352 tree ve = DECL_VALUE_EXPR (t);
13353 ve = tsubst_expr (ve, args, complain, in_decl,
13354 /*constant_expression_p=*/false);
13355 if (REFERENCE_REF_P (ve))
13356 {
13357 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13358 ve = TREE_OPERAND (ve, 0);
13359 }
13360 SET_DECL_VALUE_EXPR (r, ve);
13361 }
13362 if (CP_DECL_THREAD_LOCAL_P (r)
13363 && !processing_template_decl)
13364 set_decl_tls_model (r, decl_default_tls_model (r));
13365 }
13366 else if (DECL_SELF_REFERENCE_P (t))
13367 SET_DECL_SELF_REFERENCE_P (r);
13368 TREE_TYPE (r) = type;
13369 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13370 DECL_CONTEXT (r) = ctx;
13371 /* Clear out the mangled name and RTL for the instantiation. */
13372 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13373 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13374 SET_DECL_RTL (r, NULL);
13375 /* The initializer must not be expanded until it is required;
13376 see [temp.inst]. */
13377 DECL_INITIAL (r) = NULL_TREE;
13378 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13379 if (VAR_P (r))
13380 {
13381 if (DECL_LANG_SPECIFIC (r))
13382 SET_DECL_DEPENDENT_INIT_P (r, false);
13383
13384 SET_DECL_MODE (r, VOIDmode);
13385
13386 /* Possibly limit visibility based on template args. */
13387 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13388 if (DECL_VISIBILITY_SPECIFIED (t))
13389 {
13390 DECL_VISIBILITY_SPECIFIED (r) = 0;
13391 DECL_ATTRIBUTES (r)
13392 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13393 }
13394 determine_visibility (r);
13395 }
13396
13397 if (!local_p)
13398 {
13399 /* A static data member declaration is always marked
13400 external when it is declared in-class, even if an
13401 initializer is present. We mimic the non-template
13402 processing here. */
13403 DECL_EXTERNAL (r) = 1;
13404 if (DECL_NAMESPACE_SCOPE_P (t))
13405 DECL_NOT_REALLY_EXTERN (r) = 1;
13406
13407 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13408 SET_DECL_IMPLICIT_INSTANTIATION (r);
13409 register_specialization (r, gen_tmpl, argvec, false, hash);
13410 }
13411 else
13412 {
13413 if (DECL_LANG_SPECIFIC (r))
13414 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13415 if (!cp_unevaluated_operand)
13416 register_local_specialization (r, t);
13417 }
13418
13419 DECL_CHAIN (r) = NULL_TREE;
13420
13421 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13422 /*flags=*/0,
13423 args, complain, in_decl);
13424
13425 /* Preserve a typedef that names a type. */
13426 if (is_typedef_decl (r) && type != error_mark_node)
13427 {
13428 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13429 set_underlying_type (r);
13430 if (TYPE_DECL_ALIAS_P (r))
13431 /* An alias template specialization can be dependent
13432 even if its underlying type is not. */
13433 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13434 }
13435
13436 layout_decl (r, 0);
13437 }
13438 break;
13439
13440 default:
13441 gcc_unreachable ();
13442 }
13443 #undef RETURN
13444
13445 out:
13446 /* Restore the file and line information. */
13447 input_location = saved_loc;
13448
13449 return r;
13450 }
13451
13452 /* Substitute into the ARG_TYPES of a function type.
13453 If END is a TREE_CHAIN, leave it and any following types
13454 un-substituted. */
13455
13456 static tree
13457 tsubst_arg_types (tree arg_types,
13458 tree args,
13459 tree end,
13460 tsubst_flags_t complain,
13461 tree in_decl)
13462 {
13463 tree remaining_arg_types;
13464 tree type = NULL_TREE;
13465 int i = 1;
13466 tree expanded_args = NULL_TREE;
13467 tree default_arg;
13468
13469 if (!arg_types || arg_types == void_list_node || arg_types == end)
13470 return arg_types;
13471
13472 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13473 args, end, complain, in_decl);
13474 if (remaining_arg_types == error_mark_node)
13475 return error_mark_node;
13476
13477 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13478 {
13479 /* For a pack expansion, perform substitution on the
13480 entire expression. Later on, we'll handle the arguments
13481 one-by-one. */
13482 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13483 args, complain, in_decl);
13484
13485 if (TREE_CODE (expanded_args) == TREE_VEC)
13486 /* So that we'll spin through the parameters, one by one. */
13487 i = TREE_VEC_LENGTH (expanded_args);
13488 else
13489 {
13490 /* We only partially substituted into the parameter
13491 pack. Our type is TYPE_PACK_EXPANSION. */
13492 type = expanded_args;
13493 expanded_args = NULL_TREE;
13494 }
13495 }
13496
13497 while (i > 0) {
13498 --i;
13499
13500 if (expanded_args)
13501 type = TREE_VEC_ELT (expanded_args, i);
13502 else if (!type)
13503 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13504
13505 if (type == error_mark_node)
13506 return error_mark_node;
13507 if (VOID_TYPE_P (type))
13508 {
13509 if (complain & tf_error)
13510 {
13511 error ("invalid parameter type %qT", type);
13512 if (in_decl)
13513 error ("in declaration %q+D", in_decl);
13514 }
13515 return error_mark_node;
13516 }
13517 /* DR 657. */
13518 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13519 return error_mark_node;
13520
13521 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13522 top-level qualifiers as required. */
13523 type = cv_unqualified (type_decays_to (type));
13524
13525 /* We do not substitute into default arguments here. The standard
13526 mandates that they be instantiated only when needed, which is
13527 done in build_over_call. */
13528 default_arg = TREE_PURPOSE (arg_types);
13529
13530 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13531 since the new op() won't have any associated template arguments for us
13532 to refer to later. */
13533 if (lambda_fn_in_template_p (in_decl))
13534 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13535 false/*fn*/, false/*constexpr*/);
13536
13537 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13538 {
13539 /* We've instantiated a template before its default arguments
13540 have been parsed. This can happen for a nested template
13541 class, and is not an error unless we require the default
13542 argument in a call of this function. */
13543 remaining_arg_types =
13544 tree_cons (default_arg, type, remaining_arg_types);
13545 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13546 }
13547 else
13548 remaining_arg_types =
13549 hash_tree_cons (default_arg, type, remaining_arg_types);
13550 }
13551
13552 return remaining_arg_types;
13553 }
13554
13555 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13556 *not* handle the exception-specification for FNTYPE, because the
13557 initial substitution of explicitly provided template parameters
13558 during argument deduction forbids substitution into the
13559 exception-specification:
13560
13561 [temp.deduct]
13562
13563 All references in the function type of the function template to the
13564 corresponding template parameters are replaced by the specified tem-
13565 plate argument values. If a substitution in a template parameter or
13566 in the function type of the function template results in an invalid
13567 type, type deduction fails. [Note: The equivalent substitution in
13568 exception specifications is done only when the function is instanti-
13569 ated, at which point a program is ill-formed if the substitution
13570 results in an invalid type.] */
13571
13572 static tree
13573 tsubst_function_type (tree t,
13574 tree args,
13575 tsubst_flags_t complain,
13576 tree in_decl)
13577 {
13578 tree return_type;
13579 tree arg_types = NULL_TREE;
13580 tree fntype;
13581
13582 /* The TYPE_CONTEXT is not used for function/method types. */
13583 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13584
13585 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13586 failure. */
13587 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13588
13589 if (late_return_type_p)
13590 {
13591 /* Substitute the argument types. */
13592 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13593 complain, in_decl);
13594 if (arg_types == error_mark_node)
13595 return error_mark_node;
13596
13597 tree save_ccp = current_class_ptr;
13598 tree save_ccr = current_class_ref;
13599 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13600 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13601 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13602 if (do_inject)
13603 {
13604 /* DR 1207: 'this' is in scope in the trailing return type. */
13605 inject_this_parameter (this_type, cp_type_quals (this_type));
13606 }
13607
13608 /* Substitute the return type. */
13609 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13610
13611 if (do_inject)
13612 {
13613 current_class_ptr = save_ccp;
13614 current_class_ref = save_ccr;
13615 }
13616 }
13617 else
13618 /* Substitute the return type. */
13619 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13620
13621 if (return_type == error_mark_node)
13622 return error_mark_node;
13623 /* DR 486 clarifies that creation of a function type with an
13624 invalid return type is a deduction failure. */
13625 if (TREE_CODE (return_type) == ARRAY_TYPE
13626 || TREE_CODE (return_type) == FUNCTION_TYPE)
13627 {
13628 if (complain & tf_error)
13629 {
13630 if (TREE_CODE (return_type) == ARRAY_TYPE)
13631 error ("function returning an array");
13632 else
13633 error ("function returning a function");
13634 }
13635 return error_mark_node;
13636 }
13637 /* And DR 657. */
13638 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13639 return error_mark_node;
13640
13641 if (!late_return_type_p)
13642 {
13643 /* Substitute the argument types. */
13644 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13645 complain, in_decl);
13646 if (arg_types == error_mark_node)
13647 return error_mark_node;
13648 }
13649
13650 /* Construct a new type node and return it. */
13651 if (TREE_CODE (t) == FUNCTION_TYPE)
13652 {
13653 fntype = build_function_type (return_type, arg_types);
13654 fntype = apply_memfn_quals (fntype,
13655 type_memfn_quals (t),
13656 type_memfn_rqual (t));
13657 }
13658 else
13659 {
13660 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13661 /* Don't pick up extra function qualifiers from the basetype. */
13662 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13663 if (! MAYBE_CLASS_TYPE_P (r))
13664 {
13665 /* [temp.deduct]
13666
13667 Type deduction may fail for any of the following
13668 reasons:
13669
13670 -- Attempting to create "pointer to member of T" when T
13671 is not a class type. */
13672 if (complain & tf_error)
13673 error ("creating pointer to member function of non-class type %qT",
13674 r);
13675 return error_mark_node;
13676 }
13677
13678 fntype = build_method_type_directly (r, return_type,
13679 TREE_CHAIN (arg_types));
13680 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13681 }
13682 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13683
13684 if (late_return_type_p)
13685 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13686
13687 return fntype;
13688 }
13689
13690 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13691 ARGS into that specification, and return the substituted
13692 specification. If there is no specification, return NULL_TREE. */
13693
13694 static tree
13695 tsubst_exception_specification (tree fntype,
13696 tree args,
13697 tsubst_flags_t complain,
13698 tree in_decl,
13699 bool defer_ok)
13700 {
13701 tree specs;
13702 tree new_specs;
13703
13704 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13705 new_specs = NULL_TREE;
13706 if (specs && TREE_PURPOSE (specs))
13707 {
13708 /* A noexcept-specifier. */
13709 tree expr = TREE_PURPOSE (specs);
13710 if (TREE_CODE (expr) == INTEGER_CST)
13711 new_specs = expr;
13712 else if (defer_ok)
13713 {
13714 /* Defer instantiation of noexcept-specifiers to avoid
13715 excessive instantiations (c++/49107). */
13716 new_specs = make_node (DEFERRED_NOEXCEPT);
13717 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13718 {
13719 /* We already partially instantiated this member template,
13720 so combine the new args with the old. */
13721 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13722 = DEFERRED_NOEXCEPT_PATTERN (expr);
13723 DEFERRED_NOEXCEPT_ARGS (new_specs)
13724 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13725 }
13726 else
13727 {
13728 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13729 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13730 }
13731 }
13732 else
13733 new_specs = tsubst_copy_and_build
13734 (expr, args, complain, in_decl, /*function_p=*/false,
13735 /*integral_constant_expression_p=*/true);
13736 new_specs = build_noexcept_spec (new_specs, complain);
13737 }
13738 else if (specs)
13739 {
13740 if (! TREE_VALUE (specs))
13741 new_specs = specs;
13742 else
13743 while (specs)
13744 {
13745 tree spec;
13746 int i, len = 1;
13747 tree expanded_specs = NULL_TREE;
13748
13749 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13750 {
13751 /* Expand the pack expansion type. */
13752 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13753 args, complain,
13754 in_decl);
13755
13756 if (expanded_specs == error_mark_node)
13757 return error_mark_node;
13758 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13759 len = TREE_VEC_LENGTH (expanded_specs);
13760 else
13761 {
13762 /* We're substituting into a member template, so
13763 we got a TYPE_PACK_EXPANSION back. Add that
13764 expansion and move on. */
13765 gcc_assert (TREE_CODE (expanded_specs)
13766 == TYPE_PACK_EXPANSION);
13767 new_specs = add_exception_specifier (new_specs,
13768 expanded_specs,
13769 complain);
13770 specs = TREE_CHAIN (specs);
13771 continue;
13772 }
13773 }
13774
13775 for (i = 0; i < len; ++i)
13776 {
13777 if (expanded_specs)
13778 spec = TREE_VEC_ELT (expanded_specs, i);
13779 else
13780 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13781 if (spec == error_mark_node)
13782 return spec;
13783 new_specs = add_exception_specifier (new_specs, spec,
13784 complain);
13785 }
13786
13787 specs = TREE_CHAIN (specs);
13788 }
13789 }
13790 return new_specs;
13791 }
13792
13793 /* Take the tree structure T and replace template parameters used
13794 therein with the argument vector ARGS. IN_DECL is an associated
13795 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13796 Issue error and warning messages under control of COMPLAIN. Note
13797 that we must be relatively non-tolerant of extensions here, in
13798 order to preserve conformance; if we allow substitutions that
13799 should not be allowed, we may allow argument deductions that should
13800 not succeed, and therefore report ambiguous overload situations
13801 where there are none. In theory, we could allow the substitution,
13802 but indicate that it should have failed, and allow our caller to
13803 make sure that the right thing happens, but we don't try to do this
13804 yet.
13805
13806 This function is used for dealing with types, decls and the like;
13807 for expressions, use tsubst_expr or tsubst_copy. */
13808
13809 tree
13810 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13811 {
13812 enum tree_code code;
13813 tree type, r = NULL_TREE;
13814
13815 if (t == NULL_TREE || t == error_mark_node
13816 || t == integer_type_node
13817 || t == void_type_node
13818 || t == char_type_node
13819 || t == unknown_type_node
13820 || TREE_CODE (t) == NAMESPACE_DECL
13821 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13822 return t;
13823
13824 if (DECL_P (t))
13825 return tsubst_decl (t, args, complain);
13826
13827 if (args == NULL_TREE)
13828 return t;
13829
13830 code = TREE_CODE (t);
13831
13832 if (code == IDENTIFIER_NODE)
13833 type = IDENTIFIER_TYPE_VALUE (t);
13834 else
13835 type = TREE_TYPE (t);
13836
13837 gcc_assert (type != unknown_type_node);
13838
13839 /* Reuse typedefs. We need to do this to handle dependent attributes,
13840 such as attribute aligned. */
13841 if (TYPE_P (t)
13842 && typedef_variant_p (t))
13843 {
13844 tree decl = TYPE_NAME (t);
13845
13846 if (alias_template_specialization_p (t))
13847 {
13848 /* DECL represents an alias template and we want to
13849 instantiate it. */
13850 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13851 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13852 r = instantiate_alias_template (tmpl, gen_args, complain);
13853 }
13854 else if (DECL_CLASS_SCOPE_P (decl)
13855 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13856 && uses_template_parms (DECL_CONTEXT (decl)))
13857 {
13858 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13859 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13860 r = retrieve_specialization (tmpl, gen_args, 0);
13861 }
13862 else if (DECL_FUNCTION_SCOPE_P (decl)
13863 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13864 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13865 r = retrieve_local_specialization (decl);
13866 else
13867 /* The typedef is from a non-template context. */
13868 return t;
13869
13870 if (r)
13871 {
13872 r = TREE_TYPE (r);
13873 r = cp_build_qualified_type_real
13874 (r, cp_type_quals (t) | cp_type_quals (r),
13875 complain | tf_ignore_bad_quals);
13876 return r;
13877 }
13878 else
13879 {
13880 /* We don't have an instantiation yet, so drop the typedef. */
13881 int quals = cp_type_quals (t);
13882 t = DECL_ORIGINAL_TYPE (decl);
13883 t = cp_build_qualified_type_real (t, quals,
13884 complain | tf_ignore_bad_quals);
13885 }
13886 }
13887
13888 bool fndecl_type = (complain & tf_fndecl_type);
13889 complain &= ~tf_fndecl_type;
13890
13891 if (type
13892 && code != TYPENAME_TYPE
13893 && code != TEMPLATE_TYPE_PARM
13894 && code != TEMPLATE_PARM_INDEX
13895 && code != IDENTIFIER_NODE
13896 && code != FUNCTION_TYPE
13897 && code != METHOD_TYPE)
13898 type = tsubst (type, args, complain, in_decl);
13899 if (type == error_mark_node)
13900 return error_mark_node;
13901
13902 switch (code)
13903 {
13904 case RECORD_TYPE:
13905 case UNION_TYPE:
13906 case ENUMERAL_TYPE:
13907 return tsubst_aggr_type (t, args, complain, in_decl,
13908 /*entering_scope=*/0);
13909
13910 case ERROR_MARK:
13911 case IDENTIFIER_NODE:
13912 case VOID_TYPE:
13913 case REAL_TYPE:
13914 case COMPLEX_TYPE:
13915 case VECTOR_TYPE:
13916 case BOOLEAN_TYPE:
13917 case NULLPTR_TYPE:
13918 case LANG_TYPE:
13919 return t;
13920
13921 case INTEGER_TYPE:
13922 if (t == integer_type_node)
13923 return t;
13924
13925 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13926 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13927 return t;
13928
13929 {
13930 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13931
13932 max = tsubst_expr (omax, args, complain, in_decl,
13933 /*integral_constant_expression_p=*/false);
13934
13935 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13936 needed. */
13937 if (TREE_CODE (max) == NOP_EXPR
13938 && TREE_SIDE_EFFECTS (omax)
13939 && !TREE_TYPE (max))
13940 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13941
13942 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13943 with TREE_SIDE_EFFECTS that indicates this is not an integral
13944 constant expression. */
13945 if (processing_template_decl
13946 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13947 {
13948 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13949 TREE_SIDE_EFFECTS (max) = 1;
13950 }
13951
13952 return compute_array_index_type (NULL_TREE, max, complain);
13953 }
13954
13955 case TEMPLATE_TYPE_PARM:
13956 case TEMPLATE_TEMPLATE_PARM:
13957 case BOUND_TEMPLATE_TEMPLATE_PARM:
13958 case TEMPLATE_PARM_INDEX:
13959 {
13960 int idx;
13961 int level;
13962 int levels;
13963 tree arg = NULL_TREE;
13964
13965 /* Early in template argument deduction substitution, we don't
13966 want to reduce the level of 'auto', or it will be confused
13967 with a normal template parm in subsequent deduction. */
13968 if (is_auto (t) && (complain & tf_partial))
13969 return t;
13970
13971 r = NULL_TREE;
13972
13973 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13974 template_parm_level_and_index (t, &level, &idx);
13975
13976 levels = TMPL_ARGS_DEPTH (args);
13977 if (level <= levels
13978 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13979 {
13980 arg = TMPL_ARG (args, level, idx);
13981
13982 /* See through ARGUMENT_PACK_SELECT arguments. */
13983 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13984 arg = argument_pack_select_arg (arg);
13985 }
13986
13987 if (arg == error_mark_node)
13988 return error_mark_node;
13989 else if (arg != NULL_TREE)
13990 {
13991 if (ARGUMENT_PACK_P (arg))
13992 /* If ARG is an argument pack, we don't actually want to
13993 perform a substitution here, because substitutions
13994 for argument packs are only done
13995 element-by-element. We can get to this point when
13996 substituting the type of a non-type template
13997 parameter pack, when that type actually contains
13998 template parameter packs from an outer template, e.g.,
13999
14000 template<typename... Types> struct A {
14001 template<Types... Values> struct B { };
14002 }; */
14003 return t;
14004
14005 if (code == TEMPLATE_TYPE_PARM)
14006 {
14007 int quals;
14008 gcc_assert (TYPE_P (arg));
14009
14010 quals = cp_type_quals (arg) | cp_type_quals (t);
14011
14012 return cp_build_qualified_type_real
14013 (arg, quals, complain | tf_ignore_bad_quals);
14014 }
14015 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14016 {
14017 /* We are processing a type constructed from a
14018 template template parameter. */
14019 tree argvec = tsubst (TYPE_TI_ARGS (t),
14020 args, complain, in_decl);
14021 if (argvec == error_mark_node)
14022 return error_mark_node;
14023
14024 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14025 || TREE_CODE (arg) == TEMPLATE_DECL
14026 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14027
14028 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14029 /* Consider this code:
14030
14031 template <template <class> class Template>
14032 struct Internal {
14033 template <class Arg> using Bind = Template<Arg>;
14034 };
14035
14036 template <template <class> class Template, class Arg>
14037 using Instantiate = Template<Arg>; //#0
14038
14039 template <template <class> class Template,
14040 class Argument>
14041 using Bind =
14042 Instantiate<Internal<Template>::template Bind,
14043 Argument>; //#1
14044
14045 When #1 is parsed, the
14046 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14047 parameter `Template' in #0 matches the
14048 UNBOUND_CLASS_TEMPLATE representing the argument
14049 `Internal<Template>::template Bind'; We then want
14050 to assemble the type `Bind<Argument>' that can't
14051 be fully created right now, because
14052 `Internal<Template>' not being complete, the Bind
14053 template cannot be looked up in that context. So
14054 we need to "store" `Bind<Argument>' for later
14055 when the context of Bind becomes complete. Let's
14056 store that in a TYPENAME_TYPE. */
14057 return make_typename_type (TYPE_CONTEXT (arg),
14058 build_nt (TEMPLATE_ID_EXPR,
14059 TYPE_IDENTIFIER (arg),
14060 argvec),
14061 typename_type,
14062 complain);
14063
14064 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14065 are resolving nested-types in the signature of a
14066 member function templates. Otherwise ARG is a
14067 TEMPLATE_DECL and is the real template to be
14068 instantiated. */
14069 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14070 arg = TYPE_NAME (arg);
14071
14072 r = lookup_template_class (arg,
14073 argvec, in_decl,
14074 DECL_CONTEXT (arg),
14075 /*entering_scope=*/0,
14076 complain);
14077 return cp_build_qualified_type_real
14078 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14079 }
14080 else if (code == TEMPLATE_TEMPLATE_PARM)
14081 return arg;
14082 else
14083 /* TEMPLATE_PARM_INDEX. */
14084 return convert_from_reference (unshare_expr (arg));
14085 }
14086
14087 if (level == 1)
14088 /* This can happen during the attempted tsubst'ing in
14089 unify. This means that we don't yet have any information
14090 about the template parameter in question. */
14091 return t;
14092
14093 /* If we get here, we must have been looking at a parm for a
14094 more deeply nested template. Make a new version of this
14095 template parameter, but with a lower level. */
14096 switch (code)
14097 {
14098 case TEMPLATE_TYPE_PARM:
14099 case TEMPLATE_TEMPLATE_PARM:
14100 case BOUND_TEMPLATE_TEMPLATE_PARM:
14101 if (cp_type_quals (t))
14102 {
14103 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14104 r = cp_build_qualified_type_real
14105 (r, cp_type_quals (t),
14106 complain | (code == TEMPLATE_TYPE_PARM
14107 ? tf_ignore_bad_quals : 0));
14108 }
14109 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14110 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14111 && (r = (TEMPLATE_PARM_DESCENDANTS
14112 (TEMPLATE_TYPE_PARM_INDEX (t))))
14113 && (r = TREE_TYPE (r))
14114 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14115 /* Break infinite recursion when substituting the constraints
14116 of a constrained placeholder. */;
14117 else
14118 {
14119 r = copy_type (t);
14120 TEMPLATE_TYPE_PARM_INDEX (r)
14121 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14122 r, levels, args, complain);
14123 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14124 TYPE_MAIN_VARIANT (r) = r;
14125 TYPE_POINTER_TO (r) = NULL_TREE;
14126 TYPE_REFERENCE_TO (r) = NULL_TREE;
14127
14128 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14129 {
14130 /* Propagate constraints on placeholders. */
14131 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14132 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14133 = tsubst_constraint (constr, args, complain, in_decl);
14134 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14135 {
14136 pl = tsubst_copy (pl, args, complain, in_decl);
14137 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14138 }
14139 }
14140
14141 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14142 /* We have reduced the level of the template
14143 template parameter, but not the levels of its
14144 template parameters, so canonical_type_parameter
14145 will not be able to find the canonical template
14146 template parameter for this level. Thus, we
14147 require structural equality checking to compare
14148 TEMPLATE_TEMPLATE_PARMs. */
14149 SET_TYPE_STRUCTURAL_EQUALITY (r);
14150 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14151 SET_TYPE_STRUCTURAL_EQUALITY (r);
14152 else
14153 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14154
14155 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14156 {
14157 tree tinfo = TYPE_TEMPLATE_INFO (t);
14158 /* We might need to substitute into the types of non-type
14159 template parameters. */
14160 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14161 complain, in_decl);
14162 if (tmpl == error_mark_node)
14163 return error_mark_node;
14164 tree argvec = tsubst (TI_ARGS (tinfo), args,
14165 complain, in_decl);
14166 if (argvec == error_mark_node)
14167 return error_mark_node;
14168
14169 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14170 = build_template_info (tmpl, argvec);
14171 }
14172 }
14173 break;
14174
14175 case TEMPLATE_PARM_INDEX:
14176 /* OK, now substitute the type of the non-type parameter. We
14177 couldn't do it earlier because it might be an auto parameter,
14178 and we wouldn't need to if we had an argument. */
14179 type = tsubst (type, args, complain, in_decl);
14180 if (type == error_mark_node)
14181 return error_mark_node;
14182 r = reduce_template_parm_level (t, type, levels, args, complain);
14183 break;
14184
14185 default:
14186 gcc_unreachable ();
14187 }
14188
14189 return r;
14190 }
14191
14192 case TREE_LIST:
14193 {
14194 tree purpose, value, chain;
14195
14196 if (t == void_list_node)
14197 return t;
14198
14199 purpose = TREE_PURPOSE (t);
14200 if (purpose)
14201 {
14202 purpose = tsubst (purpose, args, complain, in_decl);
14203 if (purpose == error_mark_node)
14204 return error_mark_node;
14205 }
14206 value = TREE_VALUE (t);
14207 if (value)
14208 {
14209 value = tsubst (value, args, complain, in_decl);
14210 if (value == error_mark_node)
14211 return error_mark_node;
14212 }
14213 chain = TREE_CHAIN (t);
14214 if (chain && chain != void_type_node)
14215 {
14216 chain = tsubst (chain, args, complain, in_decl);
14217 if (chain == error_mark_node)
14218 return error_mark_node;
14219 }
14220 if (purpose == TREE_PURPOSE (t)
14221 && value == TREE_VALUE (t)
14222 && chain == TREE_CHAIN (t))
14223 return t;
14224 return hash_tree_cons (purpose, value, chain);
14225 }
14226
14227 case TREE_BINFO:
14228 /* We should never be tsubsting a binfo. */
14229 gcc_unreachable ();
14230
14231 case TREE_VEC:
14232 /* A vector of template arguments. */
14233 gcc_assert (!type);
14234 return tsubst_template_args (t, args, complain, in_decl);
14235
14236 case POINTER_TYPE:
14237 case REFERENCE_TYPE:
14238 {
14239 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14240 return t;
14241
14242 /* [temp.deduct]
14243
14244 Type deduction may fail for any of the following
14245 reasons:
14246
14247 -- Attempting to create a pointer to reference type.
14248 -- Attempting to create a reference to a reference type or
14249 a reference to void.
14250
14251 Core issue 106 says that creating a reference to a reference
14252 during instantiation is no longer a cause for failure. We
14253 only enforce this check in strict C++98 mode. */
14254 if ((TREE_CODE (type) == REFERENCE_TYPE
14255 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14256 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14257 {
14258 static location_t last_loc;
14259
14260 /* We keep track of the last time we issued this error
14261 message to avoid spewing a ton of messages during a
14262 single bad template instantiation. */
14263 if (complain & tf_error
14264 && last_loc != input_location)
14265 {
14266 if (VOID_TYPE_P (type))
14267 error ("forming reference to void");
14268 else if (code == POINTER_TYPE)
14269 error ("forming pointer to reference type %qT", type);
14270 else
14271 error ("forming reference to reference type %qT", type);
14272 last_loc = input_location;
14273 }
14274
14275 return error_mark_node;
14276 }
14277 else if (TREE_CODE (type) == FUNCTION_TYPE
14278 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14279 || type_memfn_rqual (type) != REF_QUAL_NONE))
14280 {
14281 if (complain & tf_error)
14282 {
14283 if (code == POINTER_TYPE)
14284 error ("forming pointer to qualified function type %qT",
14285 type);
14286 else
14287 error ("forming reference to qualified function type %qT",
14288 type);
14289 }
14290 return error_mark_node;
14291 }
14292 else if (code == POINTER_TYPE)
14293 {
14294 r = build_pointer_type (type);
14295 if (TREE_CODE (type) == METHOD_TYPE)
14296 r = build_ptrmemfunc_type (r);
14297 }
14298 else if (TREE_CODE (type) == REFERENCE_TYPE)
14299 /* In C++0x, during template argument substitution, when there is an
14300 attempt to create a reference to a reference type, reference
14301 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14302
14303 "If a template-argument for a template-parameter T names a type
14304 that is a reference to a type A, an attempt to create the type
14305 'lvalue reference to cv T' creates the type 'lvalue reference to
14306 A,' while an attempt to create the type type rvalue reference to
14307 cv T' creates the type T"
14308 */
14309 r = cp_build_reference_type
14310 (TREE_TYPE (type),
14311 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14312 else
14313 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14314 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14315
14316 if (r != error_mark_node)
14317 /* Will this ever be needed for TYPE_..._TO values? */
14318 layout_type (r);
14319
14320 return r;
14321 }
14322 case OFFSET_TYPE:
14323 {
14324 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14325 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14326 {
14327 /* [temp.deduct]
14328
14329 Type deduction may fail for any of the following
14330 reasons:
14331
14332 -- Attempting to create "pointer to member of T" when T
14333 is not a class type. */
14334 if (complain & tf_error)
14335 error ("creating pointer to member of non-class type %qT", r);
14336 return error_mark_node;
14337 }
14338 if (TREE_CODE (type) == REFERENCE_TYPE)
14339 {
14340 if (complain & tf_error)
14341 error ("creating pointer to member reference type %qT", type);
14342 return error_mark_node;
14343 }
14344 if (VOID_TYPE_P (type))
14345 {
14346 if (complain & tf_error)
14347 error ("creating pointer to member of type void");
14348 return error_mark_node;
14349 }
14350 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14351 if (TREE_CODE (type) == FUNCTION_TYPE)
14352 {
14353 /* The type of the implicit object parameter gets its
14354 cv-qualifiers from the FUNCTION_TYPE. */
14355 tree memptr;
14356 tree method_type
14357 = build_memfn_type (type, r, type_memfn_quals (type),
14358 type_memfn_rqual (type));
14359 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14360 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14361 complain);
14362 }
14363 else
14364 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14365 cp_type_quals (t),
14366 complain);
14367 }
14368 case FUNCTION_TYPE:
14369 case METHOD_TYPE:
14370 {
14371 tree fntype;
14372 tree specs;
14373 fntype = tsubst_function_type (t, args, complain, in_decl);
14374 if (fntype == error_mark_node)
14375 return error_mark_node;
14376
14377 /* Substitute the exception specification. */
14378 specs = tsubst_exception_specification (t, args, complain, in_decl,
14379 /*defer_ok*/fndecl_type);
14380 if (specs == error_mark_node)
14381 return error_mark_node;
14382 if (specs)
14383 fntype = build_exception_variant (fntype, specs);
14384 return fntype;
14385 }
14386 case ARRAY_TYPE:
14387 {
14388 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14389 if (domain == error_mark_node)
14390 return error_mark_node;
14391
14392 /* As an optimization, we avoid regenerating the array type if
14393 it will obviously be the same as T. */
14394 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14395 return t;
14396
14397 /* These checks should match the ones in create_array_type_for_decl.
14398
14399 [temp.deduct]
14400
14401 The deduction may fail for any of the following reasons:
14402
14403 -- Attempting to create an array with an element type that
14404 is void, a function type, or a reference type, or [DR337]
14405 an abstract class type. */
14406 if (VOID_TYPE_P (type)
14407 || TREE_CODE (type) == FUNCTION_TYPE
14408 || (TREE_CODE (type) == ARRAY_TYPE
14409 && TYPE_DOMAIN (type) == NULL_TREE)
14410 || TREE_CODE (type) == REFERENCE_TYPE)
14411 {
14412 if (complain & tf_error)
14413 error ("creating array of %qT", type);
14414 return error_mark_node;
14415 }
14416
14417 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14418 return error_mark_node;
14419
14420 r = build_cplus_array_type (type, domain);
14421
14422 if (TYPE_USER_ALIGN (t))
14423 {
14424 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14425 TYPE_USER_ALIGN (r) = 1;
14426 }
14427
14428 return r;
14429 }
14430
14431 case TYPENAME_TYPE:
14432 {
14433 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14434 in_decl, /*entering_scope=*/1);
14435 if (ctx == error_mark_node)
14436 return error_mark_node;
14437
14438 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14439 complain, in_decl);
14440 if (f == error_mark_node)
14441 return error_mark_node;
14442
14443 if (!MAYBE_CLASS_TYPE_P (ctx))
14444 {
14445 if (complain & tf_error)
14446 error ("%qT is not a class, struct, or union type", ctx);
14447 return error_mark_node;
14448 }
14449 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14450 {
14451 /* Normally, make_typename_type does not require that the CTX
14452 have complete type in order to allow things like:
14453
14454 template <class T> struct S { typename S<T>::X Y; };
14455
14456 But, such constructs have already been resolved by this
14457 point, so here CTX really should have complete type, unless
14458 it's a partial instantiation. */
14459 ctx = complete_type (ctx);
14460 if (!COMPLETE_TYPE_P (ctx))
14461 {
14462 if (complain & tf_error)
14463 cxx_incomplete_type_error (NULL_TREE, ctx);
14464 return error_mark_node;
14465 }
14466 }
14467
14468 f = make_typename_type (ctx, f, typename_type,
14469 complain | tf_keep_type_decl);
14470 if (f == error_mark_node)
14471 return f;
14472 if (TREE_CODE (f) == TYPE_DECL)
14473 {
14474 complain |= tf_ignore_bad_quals;
14475 f = TREE_TYPE (f);
14476 }
14477
14478 if (TREE_CODE (f) != TYPENAME_TYPE)
14479 {
14480 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14481 {
14482 if (complain & tf_error)
14483 error ("%qT resolves to %qT, which is not an enumeration type",
14484 t, f);
14485 else
14486 return error_mark_node;
14487 }
14488 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14489 {
14490 if (complain & tf_error)
14491 error ("%qT resolves to %qT, which is is not a class type",
14492 t, f);
14493 else
14494 return error_mark_node;
14495 }
14496 }
14497
14498 return cp_build_qualified_type_real
14499 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14500 }
14501
14502 case UNBOUND_CLASS_TEMPLATE:
14503 {
14504 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14505 in_decl, /*entering_scope=*/1);
14506 tree name = TYPE_IDENTIFIER (t);
14507 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14508
14509 if (ctx == error_mark_node || name == error_mark_node)
14510 return error_mark_node;
14511
14512 if (parm_list)
14513 parm_list = tsubst_template_parms (parm_list, args, complain);
14514 return make_unbound_class_template (ctx, name, parm_list, complain);
14515 }
14516
14517 case TYPEOF_TYPE:
14518 {
14519 tree type;
14520
14521 ++cp_unevaluated_operand;
14522 ++c_inhibit_evaluation_warnings;
14523
14524 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14525 complain, in_decl,
14526 /*integral_constant_expression_p=*/false);
14527
14528 --cp_unevaluated_operand;
14529 --c_inhibit_evaluation_warnings;
14530
14531 type = finish_typeof (type);
14532 return cp_build_qualified_type_real (type,
14533 cp_type_quals (t)
14534 | cp_type_quals (type),
14535 complain);
14536 }
14537
14538 case DECLTYPE_TYPE:
14539 {
14540 tree type;
14541
14542 ++cp_unevaluated_operand;
14543 ++c_inhibit_evaluation_warnings;
14544
14545 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14546 complain|tf_decltype, in_decl,
14547 /*function_p*/false,
14548 /*integral_constant_expression*/false);
14549
14550 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14551 {
14552 if (type == NULL_TREE)
14553 {
14554 if (complain & tf_error)
14555 error ("empty initializer in lambda init-capture");
14556 type = error_mark_node;
14557 }
14558 else if (TREE_CODE (type) == TREE_LIST)
14559 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14560 }
14561
14562 --cp_unevaluated_operand;
14563 --c_inhibit_evaluation_warnings;
14564
14565 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14566 type = lambda_capture_field_type (type,
14567 DECLTYPE_FOR_INIT_CAPTURE (t),
14568 DECLTYPE_FOR_REF_CAPTURE (t));
14569 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14570 type = lambda_proxy_type (type);
14571 else
14572 {
14573 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14574 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14575 && EXPR_P (type))
14576 /* In a template ~id could be either a complement expression
14577 or an unqualified-id naming a destructor; if instantiating
14578 it produces an expression, it's not an id-expression or
14579 member access. */
14580 id = false;
14581 type = finish_decltype_type (type, id, complain);
14582 }
14583 return cp_build_qualified_type_real (type,
14584 cp_type_quals (t)
14585 | cp_type_quals (type),
14586 complain | tf_ignore_bad_quals);
14587 }
14588
14589 case UNDERLYING_TYPE:
14590 {
14591 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14592 complain, in_decl);
14593 return finish_underlying_type (type);
14594 }
14595
14596 case TYPE_ARGUMENT_PACK:
14597 case NONTYPE_ARGUMENT_PACK:
14598 {
14599 tree r;
14600
14601 if (code == NONTYPE_ARGUMENT_PACK)
14602 r = make_node (code);
14603 else
14604 r = cxx_make_type (code);
14605
14606 tree pack_args = ARGUMENT_PACK_ARGS (t);
14607 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14608 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14609
14610 return r;
14611 }
14612
14613 case VOID_CST:
14614 case INTEGER_CST:
14615 case REAL_CST:
14616 case STRING_CST:
14617 case PLUS_EXPR:
14618 case MINUS_EXPR:
14619 case NEGATE_EXPR:
14620 case NOP_EXPR:
14621 case INDIRECT_REF:
14622 case ADDR_EXPR:
14623 case CALL_EXPR:
14624 case ARRAY_REF:
14625 case SCOPE_REF:
14626 /* We should use one of the expression tsubsts for these codes. */
14627 gcc_unreachable ();
14628
14629 default:
14630 sorry ("use of %qs in template", get_tree_code_name (code));
14631 return error_mark_node;
14632 }
14633 }
14634
14635 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14636 expression on the left-hand side of the "." or "->" operator. We
14637 only do the lookup if we had a dependent BASELINK. Otherwise we
14638 adjust it onto the instantiated heirarchy. */
14639
14640 static tree
14641 tsubst_baselink (tree baselink, tree object_type,
14642 tree args, tsubst_flags_t complain, tree in_decl)
14643 {
14644 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
14645 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14646 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14647
14648 tree optype = BASELINK_OPTYPE (baselink);
14649 optype = tsubst (optype, args, complain, in_decl);
14650
14651 tree template_args = NULL_TREE;
14652 bool template_id_p = false;
14653 tree fns = BASELINK_FUNCTIONS (baselink);
14654 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14655 {
14656 template_id_p = true;
14657 template_args = TREE_OPERAND (fns, 1);
14658 fns = TREE_OPERAND (fns, 0);
14659 if (template_args)
14660 template_args = tsubst_template_args (template_args, args,
14661 complain, in_decl);
14662 }
14663
14664 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
14665 binfo_type = tsubst (binfo_type, args, complain, in_decl);
14666 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
14667
14668 if (dependent_p)
14669 {
14670 tree name = OVL_NAME (fns);
14671 if (IDENTIFIER_CONV_OP_P (name))
14672 name = make_conv_op_name (optype);
14673
14674 if (name == complete_dtor_identifier)
14675 /* Treat as-if non-dependent below. */
14676 dependent_p = false;
14677
14678 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14679 if (!baselink)
14680 {
14681 if ((complain & tf_error)
14682 && constructor_name_p (name, qualifying_scope))
14683 error ("cannot call constructor %<%T::%D%> directly",
14684 qualifying_scope, name);
14685 return error_mark_node;
14686 }
14687
14688 if (BASELINK_P (baselink))
14689 fns = BASELINK_FUNCTIONS (baselink);
14690 }
14691 else
14692 /* We're going to overwrite pieces below, make a duplicate. */
14693 baselink = copy_node (baselink);
14694
14695 /* If lookup found a single function, mark it as used at this point.
14696 (If lookup found multiple functions the one selected later by
14697 overload resolution will be marked as used at that point.) */
14698 if (!template_id_p && !really_overloaded_fn (fns))
14699 {
14700 tree fn = OVL_FIRST (fns);
14701 bool ok = mark_used (fn, complain);
14702 if (!ok && !(complain & tf_error))
14703 return error_mark_node;
14704 if (ok && BASELINK_P (baselink))
14705 /* We might have instantiated an auto function. */
14706 TREE_TYPE (baselink) = TREE_TYPE (fn);
14707 }
14708
14709 if (BASELINK_P (baselink))
14710 {
14711 /* Add back the template arguments, if present. */
14712 if (template_id_p)
14713 BASELINK_FUNCTIONS (baselink)
14714 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
14715
14716 /* Update the conversion operator type. */
14717 BASELINK_OPTYPE (baselink) = optype;
14718 }
14719
14720 if (!object_type)
14721 object_type = current_class_type;
14722
14723 if (qualified_p || !dependent_p)
14724 {
14725 baselink = adjust_result_of_qualified_name_lookup (baselink,
14726 qualifying_scope,
14727 object_type);
14728 if (!qualified_p)
14729 /* We need to call adjust_result_of_qualified_name_lookup in case the
14730 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14731 so that we still get virtual function binding. */
14732 BASELINK_QUALIFIED_P (baselink) = false;
14733 }
14734
14735 return baselink;
14736 }
14737
14738 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14739 true if the qualified-id will be a postfix-expression in-and-of
14740 itself; false if more of the postfix-expression follows the
14741 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14742 of "&". */
14743
14744 static tree
14745 tsubst_qualified_id (tree qualified_id, tree args,
14746 tsubst_flags_t complain, tree in_decl,
14747 bool done, bool address_p)
14748 {
14749 tree expr;
14750 tree scope;
14751 tree name;
14752 bool is_template;
14753 tree template_args;
14754 location_t loc = UNKNOWN_LOCATION;
14755
14756 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14757
14758 /* Figure out what name to look up. */
14759 name = TREE_OPERAND (qualified_id, 1);
14760 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14761 {
14762 is_template = true;
14763 loc = EXPR_LOCATION (name);
14764 template_args = TREE_OPERAND (name, 1);
14765 if (template_args)
14766 template_args = tsubst_template_args (template_args, args,
14767 complain, in_decl);
14768 if (template_args == error_mark_node)
14769 return error_mark_node;
14770 name = TREE_OPERAND (name, 0);
14771 }
14772 else
14773 {
14774 is_template = false;
14775 template_args = NULL_TREE;
14776 }
14777
14778 /* Substitute into the qualifying scope. When there are no ARGS, we
14779 are just trying to simplify a non-dependent expression. In that
14780 case the qualifying scope may be dependent, and, in any case,
14781 substituting will not help. */
14782 scope = TREE_OPERAND (qualified_id, 0);
14783 if (args)
14784 {
14785 scope = tsubst (scope, args, complain, in_decl);
14786 expr = tsubst_copy (name, args, complain, in_decl);
14787 }
14788 else
14789 expr = name;
14790
14791 if (dependent_scope_p (scope))
14792 {
14793 if (is_template)
14794 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14795 tree r = build_qualified_name (NULL_TREE, scope, expr,
14796 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14797 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14798 return r;
14799 }
14800
14801 if (!BASELINK_P (name) && !DECL_P (expr))
14802 {
14803 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14804 {
14805 /* A BIT_NOT_EXPR is used to represent a destructor. */
14806 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14807 {
14808 error ("qualifying type %qT does not match destructor name ~%qT",
14809 scope, TREE_OPERAND (expr, 0));
14810 expr = error_mark_node;
14811 }
14812 else
14813 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14814 /*is_type_p=*/0, false);
14815 }
14816 else
14817 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14818 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14819 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14820 {
14821 if (complain & tf_error)
14822 {
14823 error ("dependent-name %qE is parsed as a non-type, but "
14824 "instantiation yields a type", qualified_id);
14825 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14826 }
14827 return error_mark_node;
14828 }
14829 }
14830
14831 if (DECL_P (expr))
14832 {
14833 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14834 scope);
14835 /* Remember that there was a reference to this entity. */
14836 if (!mark_used (expr, complain) && !(complain & tf_error))
14837 return error_mark_node;
14838 }
14839
14840 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14841 {
14842 if (complain & tf_error)
14843 qualified_name_lookup_error (scope,
14844 TREE_OPERAND (qualified_id, 1),
14845 expr, input_location);
14846 return error_mark_node;
14847 }
14848
14849 if (is_template)
14850 {
14851 if (variable_template_p (expr))
14852 expr = lookup_and_finish_template_variable (expr, template_args,
14853 complain);
14854 else
14855 expr = lookup_template_function (expr, template_args);
14856 }
14857
14858 if (expr == error_mark_node && complain & tf_error)
14859 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14860 expr, input_location);
14861 else if (TYPE_P (scope))
14862 {
14863 expr = (adjust_result_of_qualified_name_lookup
14864 (expr, scope, current_nonlambda_class_type ()));
14865 expr = (finish_qualified_id_expr
14866 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14867 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14868 /*template_arg_p=*/false, complain));
14869 }
14870
14871 /* Expressions do not generally have reference type. */
14872 if (TREE_CODE (expr) != SCOPE_REF
14873 /* However, if we're about to form a pointer-to-member, we just
14874 want the referenced member referenced. */
14875 && TREE_CODE (expr) != OFFSET_REF)
14876 expr = convert_from_reference (expr);
14877
14878 if (REF_PARENTHESIZED_P (qualified_id))
14879 expr = force_paren_expr (expr);
14880
14881 return expr;
14882 }
14883
14884 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14885 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14886 for tsubst. */
14887
14888 static tree
14889 tsubst_init (tree init, tree decl, tree args,
14890 tsubst_flags_t complain, tree in_decl)
14891 {
14892 if (!init)
14893 return NULL_TREE;
14894
14895 init = tsubst_expr (init, args, complain, in_decl, false);
14896
14897 if (!init && TREE_TYPE (decl) != error_mark_node)
14898 {
14899 /* If we had an initializer but it
14900 instantiated to nothing,
14901 value-initialize the object. This will
14902 only occur when the initializer was a
14903 pack expansion where the parameter packs
14904 used in that expansion were of length
14905 zero. */
14906 init = build_value_init (TREE_TYPE (decl),
14907 complain);
14908 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14909 init = get_target_expr_sfinae (init, complain);
14910 if (TREE_CODE (init) == TARGET_EXPR)
14911 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14912 }
14913
14914 return init;
14915 }
14916
14917 /* Like tsubst, but deals with expressions. This function just replaces
14918 template parms; to finish processing the resultant expression, use
14919 tsubst_copy_and_build or tsubst_expr. */
14920
14921 static tree
14922 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14923 {
14924 enum tree_code code;
14925 tree r;
14926
14927 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14928 return t;
14929
14930 code = TREE_CODE (t);
14931
14932 switch (code)
14933 {
14934 case PARM_DECL:
14935 r = retrieve_local_specialization (t);
14936
14937 if (r == NULL_TREE)
14938 {
14939 /* We get here for a use of 'this' in an NSDMI. */
14940 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14941 return current_class_ptr;
14942
14943 /* This can happen for a parameter name used later in a function
14944 declaration (such as in a late-specified return type). Just
14945 make a dummy decl, since it's only used for its type. */
14946 gcc_assert (cp_unevaluated_operand != 0);
14947 r = tsubst_decl (t, args, complain);
14948 /* Give it the template pattern as its context; its true context
14949 hasn't been instantiated yet and this is good enough for
14950 mangling. */
14951 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14952 }
14953
14954 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14955 r = argument_pack_select_arg (r);
14956 if (!mark_used (r, complain) && !(complain & tf_error))
14957 return error_mark_node;
14958 return r;
14959
14960 case CONST_DECL:
14961 {
14962 tree enum_type;
14963 tree v;
14964
14965 if (DECL_TEMPLATE_PARM_P (t))
14966 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14967 /* There is no need to substitute into namespace-scope
14968 enumerators. */
14969 if (DECL_NAMESPACE_SCOPE_P (t))
14970 return t;
14971 /* If ARGS is NULL, then T is known to be non-dependent. */
14972 if (args == NULL_TREE)
14973 return scalar_constant_value (t);
14974
14975 /* Unfortunately, we cannot just call lookup_name here.
14976 Consider:
14977
14978 template <int I> int f() {
14979 enum E { a = I };
14980 struct S { void g() { E e = a; } };
14981 };
14982
14983 When we instantiate f<7>::S::g(), say, lookup_name is not
14984 clever enough to find f<7>::a. */
14985 enum_type
14986 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14987 /*entering_scope=*/0);
14988
14989 for (v = TYPE_VALUES (enum_type);
14990 v != NULL_TREE;
14991 v = TREE_CHAIN (v))
14992 if (TREE_PURPOSE (v) == DECL_NAME (t))
14993 return TREE_VALUE (v);
14994
14995 /* We didn't find the name. That should never happen; if
14996 name-lookup found it during preliminary parsing, we
14997 should find it again here during instantiation. */
14998 gcc_unreachable ();
14999 }
15000 return t;
15001
15002 case FIELD_DECL:
15003 if (DECL_CONTEXT (t))
15004 {
15005 tree ctx;
15006
15007 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15008 /*entering_scope=*/1);
15009 if (ctx != DECL_CONTEXT (t))
15010 {
15011 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15012 if (!r)
15013 {
15014 if (complain & tf_error)
15015 error ("using invalid field %qD", t);
15016 return error_mark_node;
15017 }
15018 return r;
15019 }
15020 }
15021
15022 return t;
15023
15024 case VAR_DECL:
15025 case FUNCTION_DECL:
15026 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15027 r = tsubst (t, args, complain, in_decl);
15028 else if (local_variable_p (t)
15029 && uses_template_parms (DECL_CONTEXT (t)))
15030 {
15031 r = retrieve_local_specialization (t);
15032 if (r == NULL_TREE)
15033 {
15034 /* First try name lookup to find the instantiation. */
15035 r = lookup_name (DECL_NAME (t));
15036 if (r && !is_capture_proxy (r))
15037 {
15038 /* Make sure that the one we found is the one we want. */
15039 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15040 if (ctx != DECL_CONTEXT (r))
15041 r = NULL_TREE;
15042 }
15043
15044 if (r)
15045 /* OK */;
15046 else
15047 {
15048 /* This can happen for a variable used in a
15049 late-specified return type of a local lambda, or for a
15050 local static or constant. Building a new VAR_DECL
15051 should be OK in all those cases. */
15052 r = tsubst_decl (t, args, complain);
15053 if (local_specializations)
15054 /* Avoid infinite recursion (79640). */
15055 register_local_specialization (r, t);
15056 if (decl_maybe_constant_var_p (r))
15057 {
15058 /* We can't call cp_finish_decl, so handle the
15059 initializer by hand. */
15060 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15061 complain, in_decl);
15062 if (!processing_template_decl)
15063 init = maybe_constant_init (init);
15064 if (processing_template_decl
15065 ? potential_constant_expression (init)
15066 : reduced_constant_expression_p (init))
15067 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15068 = TREE_CONSTANT (r) = true;
15069 DECL_INITIAL (r) = init;
15070 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15071 TREE_TYPE (r)
15072 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15073 complain, adc_variable_type);
15074 }
15075 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15076 || decl_constant_var_p (r)
15077 || errorcount || sorrycount);
15078 if (!processing_template_decl
15079 && !TREE_STATIC (r))
15080 r = process_outer_var_ref (r, complain);
15081 }
15082 /* Remember this for subsequent uses. */
15083 if (local_specializations)
15084 register_local_specialization (r, t);
15085 }
15086 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15087 r = argument_pack_select_arg (r);
15088 }
15089 else
15090 r = t;
15091 if (!mark_used (r, complain))
15092 return error_mark_node;
15093 return r;
15094
15095 case NAMESPACE_DECL:
15096 return t;
15097
15098 case OVERLOAD:
15099 /* An OVERLOAD will always be a non-dependent overload set; an
15100 overload set from function scope will just be represented with an
15101 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15102 gcc_assert (!uses_template_parms (t));
15103 /* We must have marked any lookups as persistent. */
15104 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
15105 return t;
15106
15107 case BASELINK:
15108 return tsubst_baselink (t, current_nonlambda_class_type (),
15109 args, complain, in_decl);
15110
15111 case TEMPLATE_DECL:
15112 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15113 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15114 args, complain, in_decl);
15115 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15116 return tsubst (t, args, complain, in_decl);
15117 else if (DECL_CLASS_SCOPE_P (t)
15118 && uses_template_parms (DECL_CONTEXT (t)))
15119 {
15120 /* Template template argument like the following example need
15121 special treatment:
15122
15123 template <template <class> class TT> struct C {};
15124 template <class T> struct D {
15125 template <class U> struct E {};
15126 C<E> c; // #1
15127 };
15128 D<int> d; // #2
15129
15130 We are processing the template argument `E' in #1 for
15131 the template instantiation #2. Originally, `E' is a
15132 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15133 have to substitute this with one having context `D<int>'. */
15134
15135 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15136 if (dependent_scope_p (context))
15137 {
15138 /* When rewriting a constructor into a deduction guide, a
15139 non-dependent name can become dependent, so memtmpl<args>
15140 becomes context::template memtmpl<args>. */
15141 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15142 return build_qualified_name (type, context, DECL_NAME (t),
15143 /*template*/true);
15144 }
15145 return lookup_field (context, DECL_NAME(t), 0, false);
15146 }
15147 else
15148 /* Ordinary template template argument. */
15149 return t;
15150
15151 case NON_LVALUE_EXPR:
15152 case VIEW_CONVERT_EXPR:
15153 {
15154 /* Handle location wrappers by substituting the wrapped node
15155 first, *then* reusing the resulting type. Doing the type
15156 first ensures that we handle template parameters and
15157 parameter pack expansions. */
15158 gcc_assert (location_wrapper_p (t));
15159 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15160 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15161 }
15162
15163 case CAST_EXPR:
15164 case REINTERPRET_CAST_EXPR:
15165 case CONST_CAST_EXPR:
15166 case STATIC_CAST_EXPR:
15167 case DYNAMIC_CAST_EXPR:
15168 case IMPLICIT_CONV_EXPR:
15169 case CONVERT_EXPR:
15170 case NOP_EXPR:
15171 {
15172 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15173 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15174 return build1 (code, type, op0);
15175 }
15176
15177 case SIZEOF_EXPR:
15178 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15179 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15180 {
15181 tree expanded, op = TREE_OPERAND (t, 0);
15182 int len = 0;
15183
15184 if (SIZEOF_EXPR_TYPE_P (t))
15185 op = TREE_TYPE (op);
15186
15187 ++cp_unevaluated_operand;
15188 ++c_inhibit_evaluation_warnings;
15189 /* We only want to compute the number of arguments. */
15190 if (PACK_EXPANSION_P (op))
15191 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15192 else
15193 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15194 args, complain, in_decl);
15195 --cp_unevaluated_operand;
15196 --c_inhibit_evaluation_warnings;
15197
15198 if (TREE_CODE (expanded) == TREE_VEC)
15199 {
15200 len = TREE_VEC_LENGTH (expanded);
15201 /* Set TREE_USED for the benefit of -Wunused. */
15202 for (int i = 0; i < len; i++)
15203 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15204 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15205 }
15206
15207 if (expanded == error_mark_node)
15208 return error_mark_node;
15209 else if (PACK_EXPANSION_P (expanded)
15210 || (TREE_CODE (expanded) == TREE_VEC
15211 && pack_expansion_args_count (expanded)))
15212
15213 {
15214 if (PACK_EXPANSION_P (expanded))
15215 /* OK. */;
15216 else if (TREE_VEC_LENGTH (expanded) == 1)
15217 expanded = TREE_VEC_ELT (expanded, 0);
15218 else
15219 expanded = make_argument_pack (expanded);
15220
15221 if (TYPE_P (expanded))
15222 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15223 complain & tf_error);
15224 else
15225 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15226 complain & tf_error);
15227 }
15228 else
15229 return build_int_cst (size_type_node, len);
15230 }
15231 if (SIZEOF_EXPR_TYPE_P (t))
15232 {
15233 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15234 args, complain, in_decl);
15235 r = build1 (NOP_EXPR, r, error_mark_node);
15236 r = build1 (SIZEOF_EXPR,
15237 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15238 SIZEOF_EXPR_TYPE_P (r) = 1;
15239 return r;
15240 }
15241 /* Fall through */
15242
15243 case INDIRECT_REF:
15244 case NEGATE_EXPR:
15245 case TRUTH_NOT_EXPR:
15246 case BIT_NOT_EXPR:
15247 case ADDR_EXPR:
15248 case UNARY_PLUS_EXPR: /* Unary + */
15249 case ALIGNOF_EXPR:
15250 case AT_ENCODE_EXPR:
15251 case ARROW_EXPR:
15252 case THROW_EXPR:
15253 case TYPEID_EXPR:
15254 case REALPART_EXPR:
15255 case IMAGPART_EXPR:
15256 case PAREN_EXPR:
15257 {
15258 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15259 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15260 return build1 (code, type, op0);
15261 }
15262
15263 case COMPONENT_REF:
15264 {
15265 tree object;
15266 tree name;
15267
15268 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15269 name = TREE_OPERAND (t, 1);
15270 if (TREE_CODE (name) == BIT_NOT_EXPR)
15271 {
15272 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15273 complain, in_decl);
15274 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15275 }
15276 else if (TREE_CODE (name) == SCOPE_REF
15277 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15278 {
15279 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15280 complain, in_decl);
15281 name = TREE_OPERAND (name, 1);
15282 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15283 complain, in_decl);
15284 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15285 name = build_qualified_name (/*type=*/NULL_TREE,
15286 base, name,
15287 /*template_p=*/false);
15288 }
15289 else if (BASELINK_P (name))
15290 name = tsubst_baselink (name,
15291 non_reference (TREE_TYPE (object)),
15292 args, complain,
15293 in_decl);
15294 else
15295 name = tsubst_copy (name, args, complain, in_decl);
15296 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15297 }
15298
15299 case PLUS_EXPR:
15300 case MINUS_EXPR:
15301 case MULT_EXPR:
15302 case TRUNC_DIV_EXPR:
15303 case CEIL_DIV_EXPR:
15304 case FLOOR_DIV_EXPR:
15305 case ROUND_DIV_EXPR:
15306 case EXACT_DIV_EXPR:
15307 case BIT_AND_EXPR:
15308 case BIT_IOR_EXPR:
15309 case BIT_XOR_EXPR:
15310 case TRUNC_MOD_EXPR:
15311 case FLOOR_MOD_EXPR:
15312 case TRUTH_ANDIF_EXPR:
15313 case TRUTH_ORIF_EXPR:
15314 case TRUTH_AND_EXPR:
15315 case TRUTH_OR_EXPR:
15316 case RSHIFT_EXPR:
15317 case LSHIFT_EXPR:
15318 case RROTATE_EXPR:
15319 case LROTATE_EXPR:
15320 case EQ_EXPR:
15321 case NE_EXPR:
15322 case MAX_EXPR:
15323 case MIN_EXPR:
15324 case LE_EXPR:
15325 case GE_EXPR:
15326 case LT_EXPR:
15327 case GT_EXPR:
15328 case COMPOUND_EXPR:
15329 case DOTSTAR_EXPR:
15330 case MEMBER_REF:
15331 case PREDECREMENT_EXPR:
15332 case PREINCREMENT_EXPR:
15333 case POSTDECREMENT_EXPR:
15334 case POSTINCREMENT_EXPR:
15335 {
15336 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15337 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15338 return build_nt (code, op0, op1);
15339 }
15340
15341 case SCOPE_REF:
15342 {
15343 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15344 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15345 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15346 QUALIFIED_NAME_IS_TEMPLATE (t));
15347 }
15348
15349 case ARRAY_REF:
15350 {
15351 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15352 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15353 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15354 }
15355
15356 case CALL_EXPR:
15357 {
15358 int n = VL_EXP_OPERAND_LENGTH (t);
15359 tree result = build_vl_exp (CALL_EXPR, n);
15360 int i;
15361 for (i = 0; i < n; i++)
15362 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15363 complain, in_decl);
15364 return result;
15365 }
15366
15367 case COND_EXPR:
15368 case MODOP_EXPR:
15369 case PSEUDO_DTOR_EXPR:
15370 case VEC_PERM_EXPR:
15371 {
15372 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15373 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15374 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15375 r = build_nt (code, op0, op1, op2);
15376 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15377 return r;
15378 }
15379
15380 case NEW_EXPR:
15381 {
15382 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15383 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15384 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15385 r = build_nt (code, op0, op1, op2);
15386 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15387 return r;
15388 }
15389
15390 case DELETE_EXPR:
15391 {
15392 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15393 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15394 r = build_nt (code, op0, op1);
15395 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15396 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15397 return r;
15398 }
15399
15400 case TEMPLATE_ID_EXPR:
15401 {
15402 /* Substituted template arguments */
15403 tree fn = TREE_OPERAND (t, 0);
15404 tree targs = TREE_OPERAND (t, 1);
15405
15406 fn = tsubst_copy (fn, args, complain, in_decl);
15407 if (targs)
15408 targs = tsubst_template_args (targs, args, complain, in_decl);
15409
15410 return lookup_template_function (fn, targs);
15411 }
15412
15413 case TREE_LIST:
15414 {
15415 tree purpose, value, chain;
15416
15417 if (t == void_list_node)
15418 return t;
15419
15420 purpose = TREE_PURPOSE (t);
15421 if (purpose)
15422 purpose = tsubst_copy (purpose, args, complain, in_decl);
15423 value = TREE_VALUE (t);
15424 if (value)
15425 value = tsubst_copy (value, args, complain, in_decl);
15426 chain = TREE_CHAIN (t);
15427 if (chain && chain != void_type_node)
15428 chain = tsubst_copy (chain, args, complain, in_decl);
15429 if (purpose == TREE_PURPOSE (t)
15430 && value == TREE_VALUE (t)
15431 && chain == TREE_CHAIN (t))
15432 return t;
15433 return tree_cons (purpose, value, chain);
15434 }
15435
15436 case RECORD_TYPE:
15437 case UNION_TYPE:
15438 case ENUMERAL_TYPE:
15439 case INTEGER_TYPE:
15440 case TEMPLATE_TYPE_PARM:
15441 case TEMPLATE_TEMPLATE_PARM:
15442 case BOUND_TEMPLATE_TEMPLATE_PARM:
15443 case TEMPLATE_PARM_INDEX:
15444 case POINTER_TYPE:
15445 case REFERENCE_TYPE:
15446 case OFFSET_TYPE:
15447 case FUNCTION_TYPE:
15448 case METHOD_TYPE:
15449 case ARRAY_TYPE:
15450 case TYPENAME_TYPE:
15451 case UNBOUND_CLASS_TEMPLATE:
15452 case TYPEOF_TYPE:
15453 case DECLTYPE_TYPE:
15454 case TYPE_DECL:
15455 return tsubst (t, args, complain, in_decl);
15456
15457 case USING_DECL:
15458 t = DECL_NAME (t);
15459 /* Fall through. */
15460 case IDENTIFIER_NODE:
15461 if (IDENTIFIER_CONV_OP_P (t))
15462 {
15463 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15464 return make_conv_op_name (new_type);
15465 }
15466 else
15467 return t;
15468
15469 case CONSTRUCTOR:
15470 /* This is handled by tsubst_copy_and_build. */
15471 gcc_unreachable ();
15472
15473 case VA_ARG_EXPR:
15474 {
15475 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15476 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15477 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15478 }
15479
15480 case CLEANUP_POINT_EXPR:
15481 /* We shouldn't have built any of these during initial template
15482 generation. Instead, they should be built during instantiation
15483 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15484 gcc_unreachable ();
15485
15486 case OFFSET_REF:
15487 {
15488 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15489 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15490 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15491 r = build2 (code, type, op0, op1);
15492 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15493 if (!mark_used (TREE_OPERAND (r, 1), complain)
15494 && !(complain & tf_error))
15495 return error_mark_node;
15496 return r;
15497 }
15498
15499 case EXPR_PACK_EXPANSION:
15500 error ("invalid use of pack expansion expression");
15501 return error_mark_node;
15502
15503 case NONTYPE_ARGUMENT_PACK:
15504 error ("use %<...%> to expand argument pack");
15505 return error_mark_node;
15506
15507 case VOID_CST:
15508 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15509 return t;
15510
15511 case INTEGER_CST:
15512 case REAL_CST:
15513 case STRING_CST:
15514 case COMPLEX_CST:
15515 {
15516 /* Instantiate any typedefs in the type. */
15517 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15518 r = fold_convert (type, t);
15519 gcc_assert (TREE_CODE (r) == code);
15520 return r;
15521 }
15522
15523 case PTRMEM_CST:
15524 /* These can sometimes show up in a partial instantiation, but never
15525 involve template parms. */
15526 gcc_assert (!uses_template_parms (t));
15527 return t;
15528
15529 case UNARY_LEFT_FOLD_EXPR:
15530 return tsubst_unary_left_fold (t, args, complain, in_decl);
15531 case UNARY_RIGHT_FOLD_EXPR:
15532 return tsubst_unary_right_fold (t, args, complain, in_decl);
15533 case BINARY_LEFT_FOLD_EXPR:
15534 return tsubst_binary_left_fold (t, args, complain, in_decl);
15535 case BINARY_RIGHT_FOLD_EXPR:
15536 return tsubst_binary_right_fold (t, args, complain, in_decl);
15537 case PREDICT_EXPR:
15538 return t;
15539
15540 case DEBUG_BEGIN_STMT:
15541 /* ??? There's no point in copying it for now, but maybe some
15542 day it will contain more information, such as a pointer back
15543 to the containing function, inlined copy or so. */
15544 return t;
15545
15546 default:
15547 /* We shouldn't get here, but keep going if !flag_checking. */
15548 if (flag_checking)
15549 gcc_unreachable ();
15550 return t;
15551 }
15552 }
15553
15554 /* Helper function for tsubst_omp_clauses, used for instantiation of
15555 OMP_CLAUSE_DECL of clauses. */
15556
15557 static tree
15558 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15559 tree in_decl)
15560 {
15561 if (decl == NULL_TREE)
15562 return NULL_TREE;
15563
15564 /* Handle an OpenMP array section represented as a TREE_LIST (or
15565 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15566 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15567 TREE_LIST. We can handle it exactly the same as an array section
15568 (purpose, value, and a chain), even though the nomenclature
15569 (low_bound, length, etc) is different. */
15570 if (TREE_CODE (decl) == TREE_LIST)
15571 {
15572 tree low_bound
15573 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15574 /*integral_constant_expression_p=*/false);
15575 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15576 /*integral_constant_expression_p=*/false);
15577 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15578 in_decl);
15579 if (TREE_PURPOSE (decl) == low_bound
15580 && TREE_VALUE (decl) == length
15581 && TREE_CHAIN (decl) == chain)
15582 return decl;
15583 tree ret = tree_cons (low_bound, length, chain);
15584 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15585 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15586 return ret;
15587 }
15588 tree ret = tsubst_expr (decl, args, complain, in_decl,
15589 /*integral_constant_expression_p=*/false);
15590 /* Undo convert_from_reference tsubst_expr could have called. */
15591 if (decl
15592 && REFERENCE_REF_P (ret)
15593 && !REFERENCE_REF_P (decl))
15594 ret = TREE_OPERAND (ret, 0);
15595 return ret;
15596 }
15597
15598 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15599
15600 static tree
15601 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15602 tree args, tsubst_flags_t complain, tree in_decl)
15603 {
15604 tree new_clauses = NULL_TREE, nc, oc;
15605 tree linear_no_step = NULL_TREE;
15606
15607 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15608 {
15609 nc = copy_node (oc);
15610 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15611 new_clauses = nc;
15612
15613 switch (OMP_CLAUSE_CODE (nc))
15614 {
15615 case OMP_CLAUSE_LASTPRIVATE:
15616 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15617 {
15618 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15619 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15620 in_decl, /*integral_constant_expression_p=*/false);
15621 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15622 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15623 }
15624 /* FALLTHRU */
15625 case OMP_CLAUSE_PRIVATE:
15626 case OMP_CLAUSE_SHARED:
15627 case OMP_CLAUSE_FIRSTPRIVATE:
15628 case OMP_CLAUSE_COPYIN:
15629 case OMP_CLAUSE_COPYPRIVATE:
15630 case OMP_CLAUSE_UNIFORM:
15631 case OMP_CLAUSE_DEPEND:
15632 case OMP_CLAUSE_FROM:
15633 case OMP_CLAUSE_TO:
15634 case OMP_CLAUSE_MAP:
15635 case OMP_CLAUSE_USE_DEVICE_PTR:
15636 case OMP_CLAUSE_IS_DEVICE_PTR:
15637 OMP_CLAUSE_DECL (nc)
15638 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15639 in_decl);
15640 break;
15641 case OMP_CLAUSE_TILE:
15642 case OMP_CLAUSE_IF:
15643 case OMP_CLAUSE_NUM_THREADS:
15644 case OMP_CLAUSE_SCHEDULE:
15645 case OMP_CLAUSE_COLLAPSE:
15646 case OMP_CLAUSE_FINAL:
15647 case OMP_CLAUSE_DEVICE:
15648 case OMP_CLAUSE_DIST_SCHEDULE:
15649 case OMP_CLAUSE_NUM_TEAMS:
15650 case OMP_CLAUSE_THREAD_LIMIT:
15651 case OMP_CLAUSE_SAFELEN:
15652 case OMP_CLAUSE_SIMDLEN:
15653 case OMP_CLAUSE_NUM_TASKS:
15654 case OMP_CLAUSE_GRAINSIZE:
15655 case OMP_CLAUSE_PRIORITY:
15656 case OMP_CLAUSE_ORDERED:
15657 case OMP_CLAUSE_HINT:
15658 case OMP_CLAUSE_NUM_GANGS:
15659 case OMP_CLAUSE_NUM_WORKERS:
15660 case OMP_CLAUSE_VECTOR_LENGTH:
15661 case OMP_CLAUSE_WORKER:
15662 case OMP_CLAUSE_VECTOR:
15663 case OMP_CLAUSE_ASYNC:
15664 case OMP_CLAUSE_WAIT:
15665 OMP_CLAUSE_OPERAND (nc, 0)
15666 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15667 in_decl, /*integral_constant_expression_p=*/false);
15668 break;
15669 case OMP_CLAUSE_REDUCTION:
15670 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15671 {
15672 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15673 if (TREE_CODE (placeholder) == SCOPE_REF)
15674 {
15675 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15676 complain, in_decl);
15677 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15678 = build_qualified_name (NULL_TREE, scope,
15679 TREE_OPERAND (placeholder, 1),
15680 false);
15681 }
15682 else
15683 gcc_assert (identifier_p (placeholder));
15684 }
15685 OMP_CLAUSE_DECL (nc)
15686 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15687 in_decl);
15688 break;
15689 case OMP_CLAUSE_GANG:
15690 case OMP_CLAUSE_ALIGNED:
15691 OMP_CLAUSE_DECL (nc)
15692 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15693 in_decl);
15694 OMP_CLAUSE_OPERAND (nc, 1)
15695 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15696 in_decl, /*integral_constant_expression_p=*/false);
15697 break;
15698 case OMP_CLAUSE_LINEAR:
15699 OMP_CLAUSE_DECL (nc)
15700 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15701 in_decl);
15702 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15703 {
15704 gcc_assert (!linear_no_step);
15705 linear_no_step = nc;
15706 }
15707 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15708 OMP_CLAUSE_LINEAR_STEP (nc)
15709 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15710 complain, in_decl);
15711 else
15712 OMP_CLAUSE_LINEAR_STEP (nc)
15713 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15714 in_decl,
15715 /*integral_constant_expression_p=*/false);
15716 break;
15717 case OMP_CLAUSE_NOWAIT:
15718 case OMP_CLAUSE_DEFAULT:
15719 case OMP_CLAUSE_UNTIED:
15720 case OMP_CLAUSE_MERGEABLE:
15721 case OMP_CLAUSE_INBRANCH:
15722 case OMP_CLAUSE_NOTINBRANCH:
15723 case OMP_CLAUSE_PROC_BIND:
15724 case OMP_CLAUSE_FOR:
15725 case OMP_CLAUSE_PARALLEL:
15726 case OMP_CLAUSE_SECTIONS:
15727 case OMP_CLAUSE_TASKGROUP:
15728 case OMP_CLAUSE_NOGROUP:
15729 case OMP_CLAUSE_THREADS:
15730 case OMP_CLAUSE_SIMD:
15731 case OMP_CLAUSE_DEFAULTMAP:
15732 case OMP_CLAUSE_INDEPENDENT:
15733 case OMP_CLAUSE_AUTO:
15734 case OMP_CLAUSE_SEQ:
15735 break;
15736 default:
15737 gcc_unreachable ();
15738 }
15739 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15740 switch (OMP_CLAUSE_CODE (nc))
15741 {
15742 case OMP_CLAUSE_SHARED:
15743 case OMP_CLAUSE_PRIVATE:
15744 case OMP_CLAUSE_FIRSTPRIVATE:
15745 case OMP_CLAUSE_LASTPRIVATE:
15746 case OMP_CLAUSE_COPYPRIVATE:
15747 case OMP_CLAUSE_LINEAR:
15748 case OMP_CLAUSE_REDUCTION:
15749 case OMP_CLAUSE_USE_DEVICE_PTR:
15750 case OMP_CLAUSE_IS_DEVICE_PTR:
15751 /* tsubst_expr on SCOPE_REF results in returning
15752 finish_non_static_data_member result. Undo that here. */
15753 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15754 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15755 == IDENTIFIER_NODE))
15756 {
15757 tree t = OMP_CLAUSE_DECL (nc);
15758 tree v = t;
15759 while (v)
15760 switch (TREE_CODE (v))
15761 {
15762 case COMPONENT_REF:
15763 case MEM_REF:
15764 case INDIRECT_REF:
15765 CASE_CONVERT:
15766 case POINTER_PLUS_EXPR:
15767 v = TREE_OPERAND (v, 0);
15768 continue;
15769 case PARM_DECL:
15770 if (DECL_CONTEXT (v) == current_function_decl
15771 && DECL_ARTIFICIAL (v)
15772 && DECL_NAME (v) == this_identifier)
15773 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15774 /* FALLTHRU */
15775 default:
15776 v = NULL_TREE;
15777 break;
15778 }
15779 }
15780 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15781 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15782 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15783 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15784 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15785 {
15786 tree decl = OMP_CLAUSE_DECL (nc);
15787 if (VAR_P (decl))
15788 {
15789 retrofit_lang_decl (decl);
15790 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15791 }
15792 }
15793 break;
15794 default:
15795 break;
15796 }
15797 }
15798
15799 new_clauses = nreverse (new_clauses);
15800 if (ort != C_ORT_OMP_DECLARE_SIMD)
15801 {
15802 new_clauses = finish_omp_clauses (new_clauses, ort);
15803 if (linear_no_step)
15804 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15805 if (nc == linear_no_step)
15806 {
15807 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15808 break;
15809 }
15810 }
15811 return new_clauses;
15812 }
15813
15814 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15815
15816 static tree
15817 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15818 tree in_decl)
15819 {
15820 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15821
15822 tree purpose, value, chain;
15823
15824 if (t == NULL)
15825 return t;
15826
15827 if (TREE_CODE (t) != TREE_LIST)
15828 return tsubst_copy_and_build (t, args, complain, in_decl,
15829 /*function_p=*/false,
15830 /*integral_constant_expression_p=*/false);
15831
15832 if (t == void_list_node)
15833 return t;
15834
15835 purpose = TREE_PURPOSE (t);
15836 if (purpose)
15837 purpose = RECUR (purpose);
15838 value = TREE_VALUE (t);
15839 if (value)
15840 {
15841 if (TREE_CODE (value) != LABEL_DECL)
15842 value = RECUR (value);
15843 else
15844 {
15845 value = lookup_label (DECL_NAME (value));
15846 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15847 TREE_USED (value) = 1;
15848 }
15849 }
15850 chain = TREE_CHAIN (t);
15851 if (chain && chain != void_type_node)
15852 chain = RECUR (chain);
15853 return tree_cons (purpose, value, chain);
15854 #undef RECUR
15855 }
15856
15857 /* Used to temporarily communicate the list of #pragma omp parallel
15858 clauses to #pragma omp for instantiation if they are combined
15859 together. */
15860
15861 static tree *omp_parallel_combined_clauses;
15862
15863 /* Substitute one OMP_FOR iterator. */
15864
15865 static void
15866 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15867 tree initv, tree condv, tree incrv, tree *clauses,
15868 tree args, tsubst_flags_t complain, tree in_decl,
15869 bool integral_constant_expression_p)
15870 {
15871 #define RECUR(NODE) \
15872 tsubst_expr ((NODE), args, complain, in_decl, \
15873 integral_constant_expression_p)
15874 tree decl, init, cond, incr;
15875
15876 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15877 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15878
15879 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15880 {
15881 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15882 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15883 }
15884
15885 decl = TREE_OPERAND (init, 0);
15886 init = TREE_OPERAND (init, 1);
15887 tree decl_expr = NULL_TREE;
15888 if (init && TREE_CODE (init) == DECL_EXPR)
15889 {
15890 /* We need to jump through some hoops to handle declarations in the
15891 init-statement, since we might need to handle auto deduction,
15892 but we need to keep control of initialization. */
15893 decl_expr = init;
15894 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15895 decl = tsubst_decl (decl, args, complain);
15896 }
15897 else
15898 {
15899 if (TREE_CODE (decl) == SCOPE_REF)
15900 {
15901 decl = RECUR (decl);
15902 if (TREE_CODE (decl) == COMPONENT_REF)
15903 {
15904 tree v = decl;
15905 while (v)
15906 switch (TREE_CODE (v))
15907 {
15908 case COMPONENT_REF:
15909 case MEM_REF:
15910 case INDIRECT_REF:
15911 CASE_CONVERT:
15912 case POINTER_PLUS_EXPR:
15913 v = TREE_OPERAND (v, 0);
15914 continue;
15915 case PARM_DECL:
15916 if (DECL_CONTEXT (v) == current_function_decl
15917 && DECL_ARTIFICIAL (v)
15918 && DECL_NAME (v) == this_identifier)
15919 {
15920 decl = TREE_OPERAND (decl, 1);
15921 decl = omp_privatize_field (decl, false);
15922 }
15923 /* FALLTHRU */
15924 default:
15925 v = NULL_TREE;
15926 break;
15927 }
15928 }
15929 }
15930 else
15931 decl = RECUR (decl);
15932 }
15933 init = RECUR (init);
15934
15935 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15936 if (auto_node && init)
15937 TREE_TYPE (decl)
15938 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
15939
15940 gcc_assert (!type_dependent_expression_p (decl));
15941
15942 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15943 {
15944 if (decl_expr)
15945 {
15946 /* Declare the variable, but don't let that initialize it. */
15947 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15948 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15949 RECUR (decl_expr);
15950 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15951 }
15952
15953 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15954 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15955 if (TREE_CODE (incr) == MODIFY_EXPR)
15956 {
15957 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15958 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15959 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15960 NOP_EXPR, rhs, complain);
15961 }
15962 else
15963 incr = RECUR (incr);
15964 TREE_VEC_ELT (declv, i) = decl;
15965 TREE_VEC_ELT (initv, i) = init;
15966 TREE_VEC_ELT (condv, i) = cond;
15967 TREE_VEC_ELT (incrv, i) = incr;
15968 return;
15969 }
15970
15971 if (decl_expr)
15972 {
15973 /* Declare and initialize the variable. */
15974 RECUR (decl_expr);
15975 init = NULL_TREE;
15976 }
15977 else if (init)
15978 {
15979 tree *pc;
15980 int j;
15981 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15982 {
15983 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15984 {
15985 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15986 && OMP_CLAUSE_DECL (*pc) == decl)
15987 break;
15988 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15989 && OMP_CLAUSE_DECL (*pc) == decl)
15990 {
15991 if (j)
15992 break;
15993 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15994 tree c = *pc;
15995 *pc = OMP_CLAUSE_CHAIN (c);
15996 OMP_CLAUSE_CHAIN (c) = *clauses;
15997 *clauses = c;
15998 }
15999 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16000 && OMP_CLAUSE_DECL (*pc) == decl)
16001 {
16002 error ("iteration variable %qD should not be firstprivate",
16003 decl);
16004 *pc = OMP_CLAUSE_CHAIN (*pc);
16005 }
16006 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16007 && OMP_CLAUSE_DECL (*pc) == decl)
16008 {
16009 error ("iteration variable %qD should not be reduction",
16010 decl);
16011 *pc = OMP_CLAUSE_CHAIN (*pc);
16012 }
16013 else
16014 pc = &OMP_CLAUSE_CHAIN (*pc);
16015 }
16016 if (*pc)
16017 break;
16018 }
16019 if (*pc == NULL_TREE)
16020 {
16021 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16022 OMP_CLAUSE_DECL (c) = decl;
16023 c = finish_omp_clauses (c, C_ORT_OMP);
16024 if (c)
16025 {
16026 OMP_CLAUSE_CHAIN (c) = *clauses;
16027 *clauses = c;
16028 }
16029 }
16030 }
16031 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16032 if (COMPARISON_CLASS_P (cond))
16033 {
16034 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16035 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16036 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16037 }
16038 else
16039 cond = RECUR (cond);
16040 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16041 switch (TREE_CODE (incr))
16042 {
16043 case PREINCREMENT_EXPR:
16044 case PREDECREMENT_EXPR:
16045 case POSTINCREMENT_EXPR:
16046 case POSTDECREMENT_EXPR:
16047 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16048 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16049 break;
16050 case MODIFY_EXPR:
16051 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16052 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16053 {
16054 tree rhs = TREE_OPERAND (incr, 1);
16055 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16056 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16057 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16058 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16059 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16060 rhs0, rhs1));
16061 }
16062 else
16063 incr = RECUR (incr);
16064 break;
16065 case MODOP_EXPR:
16066 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16067 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16068 {
16069 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16070 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16071 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16072 TREE_TYPE (decl), lhs,
16073 RECUR (TREE_OPERAND (incr, 2))));
16074 }
16075 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16076 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16077 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16078 {
16079 tree rhs = TREE_OPERAND (incr, 2);
16080 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16081 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16082 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16083 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16084 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16085 rhs0, rhs1));
16086 }
16087 else
16088 incr = RECUR (incr);
16089 break;
16090 default:
16091 incr = RECUR (incr);
16092 break;
16093 }
16094
16095 TREE_VEC_ELT (declv, i) = decl;
16096 TREE_VEC_ELT (initv, i) = init;
16097 TREE_VEC_ELT (condv, i) = cond;
16098 TREE_VEC_ELT (incrv, i) = incr;
16099 #undef RECUR
16100 }
16101
16102 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16103 of OMP_TARGET's body. */
16104
16105 static tree
16106 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16107 {
16108 *walk_subtrees = 0;
16109 switch (TREE_CODE (*tp))
16110 {
16111 case OMP_TEAMS:
16112 return *tp;
16113 case BIND_EXPR:
16114 case STATEMENT_LIST:
16115 *walk_subtrees = 1;
16116 break;
16117 default:
16118 break;
16119 }
16120 return NULL_TREE;
16121 }
16122
16123 /* Helper function for tsubst_expr. For decomposition declaration
16124 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16125 also the corresponding decls representing the identifiers
16126 of the decomposition declaration. Return DECL if successful
16127 or error_mark_node otherwise, set *FIRST to the first decl
16128 in the list chained through DECL_CHAIN and *CNT to the number
16129 of such decls. */
16130
16131 static tree
16132 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16133 tsubst_flags_t complain, tree in_decl, tree *first,
16134 unsigned int *cnt)
16135 {
16136 tree decl2, decl3, prev = decl;
16137 *cnt = 0;
16138 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16139 for (decl2 = DECL_CHAIN (pattern_decl);
16140 decl2
16141 && VAR_P (decl2)
16142 && DECL_DECOMPOSITION_P (decl2)
16143 && DECL_NAME (decl2);
16144 decl2 = DECL_CHAIN (decl2))
16145 {
16146 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16147 {
16148 gcc_assert (errorcount);
16149 return error_mark_node;
16150 }
16151 (*cnt)++;
16152 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16153 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16154 tree v = DECL_VALUE_EXPR (decl2);
16155 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16156 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16157 decl3 = tsubst (decl2, args, complain, in_decl);
16158 SET_DECL_VALUE_EXPR (decl2, v);
16159 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16160 if (VAR_P (decl3))
16161 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16162 maybe_push_decl (decl3);
16163 if (error_operand_p (decl3))
16164 decl = error_mark_node;
16165 else if (decl != error_mark_node
16166 && DECL_CHAIN (decl3) != prev)
16167 {
16168 gcc_assert (errorcount);
16169 decl = error_mark_node;
16170 }
16171 else
16172 prev = decl3;
16173 }
16174 *first = prev;
16175 return decl;
16176 }
16177
16178 /* Like tsubst_copy for expressions, etc. but also does semantic
16179 processing. */
16180
16181 tree
16182 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16183 bool integral_constant_expression_p)
16184 {
16185 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16186 #define RECUR(NODE) \
16187 tsubst_expr ((NODE), args, complain, in_decl, \
16188 integral_constant_expression_p)
16189
16190 tree stmt, tmp;
16191 tree r;
16192 location_t loc;
16193
16194 if (t == NULL_TREE || t == error_mark_node)
16195 return t;
16196
16197 loc = input_location;
16198 if (EXPR_HAS_LOCATION (t))
16199 input_location = EXPR_LOCATION (t);
16200 if (STATEMENT_CODE_P (TREE_CODE (t)))
16201 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16202
16203 switch (TREE_CODE (t))
16204 {
16205 case STATEMENT_LIST:
16206 {
16207 tree_stmt_iterator i;
16208 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16209 RECUR (tsi_stmt (i));
16210 break;
16211 }
16212
16213 case CTOR_INITIALIZER:
16214 finish_mem_initializers (tsubst_initializer_list
16215 (TREE_OPERAND (t, 0), args));
16216 break;
16217
16218 case RETURN_EXPR:
16219 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16220 break;
16221
16222 case EXPR_STMT:
16223 tmp = RECUR (EXPR_STMT_EXPR (t));
16224 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16225 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16226 else
16227 finish_expr_stmt (tmp);
16228 break;
16229
16230 case USING_STMT:
16231 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16232 /*attribs=*/NULL_TREE);
16233 break;
16234
16235 case DECL_EXPR:
16236 {
16237 tree decl, pattern_decl;
16238 tree init;
16239
16240 pattern_decl = decl = DECL_EXPR_DECL (t);
16241 if (TREE_CODE (decl) == LABEL_DECL)
16242 finish_label_decl (DECL_NAME (decl));
16243 else if (TREE_CODE (decl) == USING_DECL)
16244 {
16245 tree scope = USING_DECL_SCOPE (decl);
16246 tree name = DECL_NAME (decl);
16247
16248 scope = tsubst (scope, args, complain, in_decl);
16249 decl = lookup_qualified_name (scope, name,
16250 /*is_type_p=*/false,
16251 /*complain=*/false);
16252 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16253 qualified_name_lookup_error (scope, name, decl, input_location);
16254 else
16255 finish_local_using_decl (decl, scope, name);
16256 }
16257 else if (is_capture_proxy (decl)
16258 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16259 {
16260 /* We're in tsubst_lambda_expr, we've already inserted a new
16261 capture proxy, so look it up and register it. */
16262 tree inst;
16263 if (DECL_PACK_P (decl))
16264 {
16265 inst = (retrieve_local_specialization
16266 (DECL_CAPTURED_VARIABLE (decl)));
16267 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16268 }
16269 else
16270 {
16271 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16272 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16273 gcc_assert (inst != decl && is_capture_proxy (inst));
16274 }
16275 register_local_specialization (inst, decl);
16276 break;
16277 }
16278 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16279 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16280 /* Don't copy the old closure; we'll create a new one in
16281 tsubst_lambda_expr. */
16282 break;
16283 else
16284 {
16285 init = DECL_INITIAL (decl);
16286 decl = tsubst (decl, args, complain, in_decl);
16287 if (decl != error_mark_node)
16288 {
16289 /* By marking the declaration as instantiated, we avoid
16290 trying to instantiate it. Since instantiate_decl can't
16291 handle local variables, and since we've already done
16292 all that needs to be done, that's the right thing to
16293 do. */
16294 if (VAR_P (decl))
16295 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16296 if (VAR_P (decl)
16297 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16298 /* Anonymous aggregates are a special case. */
16299 finish_anon_union (decl);
16300 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16301 {
16302 DECL_CONTEXT (decl) = current_function_decl;
16303 if (DECL_NAME (decl) == this_identifier)
16304 {
16305 tree lam = DECL_CONTEXT (current_function_decl);
16306 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16307 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16308 }
16309 insert_capture_proxy (decl);
16310 }
16311 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16312 /* We already did a pushtag. */;
16313 else if (TREE_CODE (decl) == FUNCTION_DECL
16314 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16315 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16316 {
16317 DECL_CONTEXT (decl) = NULL_TREE;
16318 pushdecl (decl);
16319 DECL_CONTEXT (decl) = current_function_decl;
16320 cp_check_omp_declare_reduction (decl);
16321 }
16322 else
16323 {
16324 int const_init = false;
16325 maybe_push_decl (decl);
16326 if (VAR_P (decl)
16327 && DECL_PRETTY_FUNCTION_P (decl))
16328 {
16329 /* For __PRETTY_FUNCTION__ we have to adjust the
16330 initializer. */
16331 const char *const name
16332 = cxx_printable_name (current_function_decl, 2);
16333 init = cp_fname_init (name, &TREE_TYPE (decl));
16334 }
16335 else
16336 init = tsubst_init (init, decl, args, complain, in_decl);
16337
16338 if (VAR_P (decl))
16339 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16340 (pattern_decl));
16341 if (VAR_P (decl)
16342 && DECL_DECOMPOSITION_P (decl)
16343 && TREE_TYPE (pattern_decl) != error_mark_node)
16344 {
16345 unsigned int cnt;
16346 tree first;
16347 tree ndecl
16348 = tsubst_decomp_names (decl, pattern_decl, args,
16349 complain, in_decl, &first, &cnt);
16350 if (ndecl != error_mark_node)
16351 cp_maybe_mangle_decomp (ndecl, first, cnt);
16352 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16353 if (ndecl != error_mark_node)
16354 cp_finish_decomp (ndecl, first, cnt);
16355 }
16356 else
16357 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16358 }
16359 }
16360 }
16361
16362 break;
16363 }
16364
16365 case FOR_STMT:
16366 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16367 RECUR (FOR_INIT_STMT (t));
16368 finish_init_stmt (stmt);
16369 tmp = RECUR (FOR_COND (t));
16370 finish_for_cond (tmp, stmt, false, 0);
16371 tmp = RECUR (FOR_EXPR (t));
16372 finish_for_expr (tmp, stmt);
16373 {
16374 bool prev = note_iteration_stmt_body_start ();
16375 RECUR (FOR_BODY (t));
16376 note_iteration_stmt_body_end (prev);
16377 }
16378 finish_for_stmt (stmt);
16379 break;
16380
16381 case RANGE_FOR_STMT:
16382 {
16383 /* Construct another range_for, if this is not a final
16384 substitution (for inside inside a generic lambda of a
16385 template). Otherwise convert to a regular for. */
16386 tree decl, expr;
16387 stmt = (processing_template_decl
16388 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16389 : begin_for_stmt (NULL_TREE, NULL_TREE));
16390 decl = RANGE_FOR_DECL (t);
16391 decl = tsubst (decl, args, complain, in_decl);
16392 maybe_push_decl (decl);
16393 expr = RECUR (RANGE_FOR_EXPR (t));
16394
16395 tree decomp_first = NULL_TREE;
16396 unsigned decomp_cnt = 0;
16397 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16398 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16399 complain, in_decl,
16400 &decomp_first, &decomp_cnt);
16401
16402 if (processing_template_decl)
16403 {
16404 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16405 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16406 finish_range_for_decl (stmt, decl, expr);
16407 }
16408 else
16409 {
16410 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16411 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16412 stmt = cp_convert_range_for (stmt, decl, expr,
16413 decomp_first, decomp_cnt,
16414 RANGE_FOR_IVDEP (t), unroll);
16415 }
16416
16417 bool prev = note_iteration_stmt_body_start ();
16418 RECUR (RANGE_FOR_BODY (t));
16419 note_iteration_stmt_body_end (prev);
16420 finish_for_stmt (stmt);
16421 }
16422 break;
16423
16424 case WHILE_STMT:
16425 stmt = begin_while_stmt ();
16426 tmp = RECUR (WHILE_COND (t));
16427 finish_while_stmt_cond (tmp, stmt, false, 0);
16428 {
16429 bool prev = note_iteration_stmt_body_start ();
16430 RECUR (WHILE_BODY (t));
16431 note_iteration_stmt_body_end (prev);
16432 }
16433 finish_while_stmt (stmt);
16434 break;
16435
16436 case DO_STMT:
16437 stmt = begin_do_stmt ();
16438 {
16439 bool prev = note_iteration_stmt_body_start ();
16440 RECUR (DO_BODY (t));
16441 note_iteration_stmt_body_end (prev);
16442 }
16443 finish_do_body (stmt);
16444 tmp = RECUR (DO_COND (t));
16445 finish_do_stmt (tmp, stmt, false, 0);
16446 break;
16447
16448 case IF_STMT:
16449 stmt = begin_if_stmt ();
16450 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16451 tmp = RECUR (IF_COND (t));
16452 tmp = finish_if_stmt_cond (tmp, stmt);
16453 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16454 /* Don't instantiate the THEN_CLAUSE. */;
16455 else
16456 {
16457 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16458 if (inhibit)
16459 ++c_inhibit_evaluation_warnings;
16460 RECUR (THEN_CLAUSE (t));
16461 if (inhibit)
16462 --c_inhibit_evaluation_warnings;
16463 }
16464 finish_then_clause (stmt);
16465
16466 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16467 /* Don't instantiate the ELSE_CLAUSE. */;
16468 else if (ELSE_CLAUSE (t))
16469 {
16470 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16471 begin_else_clause (stmt);
16472 if (inhibit)
16473 ++c_inhibit_evaluation_warnings;
16474 RECUR (ELSE_CLAUSE (t));
16475 if (inhibit)
16476 --c_inhibit_evaluation_warnings;
16477 finish_else_clause (stmt);
16478 }
16479
16480 finish_if_stmt (stmt);
16481 break;
16482
16483 case BIND_EXPR:
16484 if (BIND_EXPR_BODY_BLOCK (t))
16485 stmt = begin_function_body ();
16486 else
16487 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16488 ? BCS_TRY_BLOCK : 0);
16489
16490 RECUR (BIND_EXPR_BODY (t));
16491
16492 if (BIND_EXPR_BODY_BLOCK (t))
16493 finish_function_body (stmt);
16494 else
16495 finish_compound_stmt (stmt);
16496 break;
16497
16498 case BREAK_STMT:
16499 finish_break_stmt ();
16500 break;
16501
16502 case CONTINUE_STMT:
16503 finish_continue_stmt ();
16504 break;
16505
16506 case SWITCH_STMT:
16507 stmt = begin_switch_stmt ();
16508 tmp = RECUR (SWITCH_STMT_COND (t));
16509 finish_switch_cond (tmp, stmt);
16510 RECUR (SWITCH_STMT_BODY (t));
16511 finish_switch_stmt (stmt);
16512 break;
16513
16514 case CASE_LABEL_EXPR:
16515 {
16516 tree low = RECUR (CASE_LOW (t));
16517 tree high = RECUR (CASE_HIGH (t));
16518 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16519 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16520 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16521 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16522 }
16523 break;
16524
16525 case LABEL_EXPR:
16526 {
16527 tree decl = LABEL_EXPR_LABEL (t);
16528 tree label;
16529
16530 label = finish_label_stmt (DECL_NAME (decl));
16531 if (TREE_CODE (label) == LABEL_DECL)
16532 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16533 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16534 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16535 }
16536 break;
16537
16538 case GOTO_EXPR:
16539 tmp = GOTO_DESTINATION (t);
16540 if (TREE_CODE (tmp) != LABEL_DECL)
16541 /* Computed goto's must be tsubst'd into. On the other hand,
16542 non-computed gotos must not be; the identifier in question
16543 will have no binding. */
16544 tmp = RECUR (tmp);
16545 else
16546 tmp = DECL_NAME (tmp);
16547 finish_goto_stmt (tmp);
16548 break;
16549
16550 case ASM_EXPR:
16551 {
16552 tree string = RECUR (ASM_STRING (t));
16553 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16554 complain, in_decl);
16555 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16556 complain, in_decl);
16557 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16558 complain, in_decl);
16559 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16560 complain, in_decl);
16561 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16562 clobbers, labels);
16563 tree asm_expr = tmp;
16564 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16565 asm_expr = TREE_OPERAND (asm_expr, 0);
16566 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16567 }
16568 break;
16569
16570 case TRY_BLOCK:
16571 if (CLEANUP_P (t))
16572 {
16573 stmt = begin_try_block ();
16574 RECUR (TRY_STMTS (t));
16575 finish_cleanup_try_block (stmt);
16576 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16577 }
16578 else
16579 {
16580 tree compound_stmt = NULL_TREE;
16581
16582 if (FN_TRY_BLOCK_P (t))
16583 stmt = begin_function_try_block (&compound_stmt);
16584 else
16585 stmt = begin_try_block ();
16586
16587 RECUR (TRY_STMTS (t));
16588
16589 if (FN_TRY_BLOCK_P (t))
16590 finish_function_try_block (stmt);
16591 else
16592 finish_try_block (stmt);
16593
16594 RECUR (TRY_HANDLERS (t));
16595 if (FN_TRY_BLOCK_P (t))
16596 finish_function_handler_sequence (stmt, compound_stmt);
16597 else
16598 finish_handler_sequence (stmt);
16599 }
16600 break;
16601
16602 case HANDLER:
16603 {
16604 tree decl = HANDLER_PARMS (t);
16605
16606 if (decl)
16607 {
16608 decl = tsubst (decl, args, complain, in_decl);
16609 /* Prevent instantiate_decl from trying to instantiate
16610 this variable. We've already done all that needs to be
16611 done. */
16612 if (decl != error_mark_node)
16613 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16614 }
16615 stmt = begin_handler ();
16616 finish_handler_parms (decl, stmt);
16617 RECUR (HANDLER_BODY (t));
16618 finish_handler (stmt);
16619 }
16620 break;
16621
16622 case TAG_DEFN:
16623 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16624 if (CLASS_TYPE_P (tmp))
16625 {
16626 /* Local classes are not independent templates; they are
16627 instantiated along with their containing function. And this
16628 way we don't have to deal with pushing out of one local class
16629 to instantiate a member of another local class. */
16630 /* Closures are handled by the LAMBDA_EXPR. */
16631 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16632 complete_type (tmp);
16633 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16634 if ((VAR_P (fld)
16635 || (TREE_CODE (fld) == FUNCTION_DECL
16636 && !DECL_ARTIFICIAL (fld)))
16637 && DECL_TEMPLATE_INSTANTIATION (fld))
16638 instantiate_decl (fld, /*defer_ok=*/false,
16639 /*expl_inst_class=*/false);
16640 }
16641 break;
16642
16643 case STATIC_ASSERT:
16644 {
16645 tree condition;
16646
16647 ++c_inhibit_evaluation_warnings;
16648 condition =
16649 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16650 args,
16651 complain, in_decl,
16652 /*integral_constant_expression_p=*/true);
16653 --c_inhibit_evaluation_warnings;
16654
16655 finish_static_assert (condition,
16656 STATIC_ASSERT_MESSAGE (t),
16657 STATIC_ASSERT_SOURCE_LOCATION (t),
16658 /*member_p=*/false);
16659 }
16660 break;
16661
16662 case OACC_KERNELS:
16663 case OACC_PARALLEL:
16664 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16665 in_decl);
16666 stmt = begin_omp_parallel ();
16667 RECUR (OMP_BODY (t));
16668 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16669 break;
16670
16671 case OMP_PARALLEL:
16672 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16673 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16674 complain, in_decl);
16675 if (OMP_PARALLEL_COMBINED (t))
16676 omp_parallel_combined_clauses = &tmp;
16677 stmt = begin_omp_parallel ();
16678 RECUR (OMP_PARALLEL_BODY (t));
16679 gcc_assert (omp_parallel_combined_clauses == NULL);
16680 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16681 = OMP_PARALLEL_COMBINED (t);
16682 pop_omp_privatization_clauses (r);
16683 break;
16684
16685 case OMP_TASK:
16686 r = push_omp_privatization_clauses (false);
16687 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16688 complain, in_decl);
16689 stmt = begin_omp_task ();
16690 RECUR (OMP_TASK_BODY (t));
16691 finish_omp_task (tmp, stmt);
16692 pop_omp_privatization_clauses (r);
16693 break;
16694
16695 case OMP_FOR:
16696 case OMP_SIMD:
16697 case OMP_DISTRIBUTE:
16698 case OMP_TASKLOOP:
16699 case OACC_LOOP:
16700 {
16701 tree clauses, body, pre_body;
16702 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16703 tree orig_declv = NULL_TREE;
16704 tree incrv = NULL_TREE;
16705 enum c_omp_region_type ort = C_ORT_OMP;
16706 int i;
16707
16708 if (TREE_CODE (t) == OACC_LOOP)
16709 ort = C_ORT_ACC;
16710
16711 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16712 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16713 in_decl);
16714 if (OMP_FOR_INIT (t) != NULL_TREE)
16715 {
16716 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16717 if (OMP_FOR_ORIG_DECLS (t))
16718 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16719 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16720 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16721 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16722 }
16723
16724 stmt = begin_omp_structured_block ();
16725
16726 pre_body = push_stmt_list ();
16727 RECUR (OMP_FOR_PRE_BODY (t));
16728 pre_body = pop_stmt_list (pre_body);
16729
16730 if (OMP_FOR_INIT (t) != NULL_TREE)
16731 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16732 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16733 incrv, &clauses, args, complain, in_decl,
16734 integral_constant_expression_p);
16735 omp_parallel_combined_clauses = NULL;
16736
16737 body = push_stmt_list ();
16738 RECUR (OMP_FOR_BODY (t));
16739 body = pop_stmt_list (body);
16740
16741 if (OMP_FOR_INIT (t) != NULL_TREE)
16742 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16743 orig_declv, initv, condv, incrv, body, pre_body,
16744 NULL, clauses);
16745 else
16746 {
16747 t = make_node (TREE_CODE (t));
16748 TREE_TYPE (t) = void_type_node;
16749 OMP_FOR_BODY (t) = body;
16750 OMP_FOR_PRE_BODY (t) = pre_body;
16751 OMP_FOR_CLAUSES (t) = clauses;
16752 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16753 add_stmt (t);
16754 }
16755
16756 add_stmt (finish_omp_structured_block (stmt));
16757 pop_omp_privatization_clauses (r);
16758 }
16759 break;
16760
16761 case OMP_SECTIONS:
16762 omp_parallel_combined_clauses = NULL;
16763 /* FALLTHRU */
16764 case OMP_SINGLE:
16765 case OMP_TEAMS:
16766 case OMP_CRITICAL:
16767 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16768 && OMP_TEAMS_COMBINED (t));
16769 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16770 in_decl);
16771 stmt = push_stmt_list ();
16772 RECUR (OMP_BODY (t));
16773 stmt = pop_stmt_list (stmt);
16774
16775 t = copy_node (t);
16776 OMP_BODY (t) = stmt;
16777 OMP_CLAUSES (t) = tmp;
16778 add_stmt (t);
16779 pop_omp_privatization_clauses (r);
16780 break;
16781
16782 case OACC_DATA:
16783 case OMP_TARGET_DATA:
16784 case OMP_TARGET:
16785 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16786 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16787 in_decl);
16788 keep_next_level (true);
16789 stmt = begin_omp_structured_block ();
16790
16791 RECUR (OMP_BODY (t));
16792 stmt = finish_omp_structured_block (stmt);
16793
16794 t = copy_node (t);
16795 OMP_BODY (t) = stmt;
16796 OMP_CLAUSES (t) = tmp;
16797 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16798 {
16799 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16800 if (teams)
16801 {
16802 /* For combined target teams, ensure the num_teams and
16803 thread_limit clause expressions are evaluated on the host,
16804 before entering the target construct. */
16805 tree c;
16806 for (c = OMP_TEAMS_CLAUSES (teams);
16807 c; c = OMP_CLAUSE_CHAIN (c))
16808 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16809 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16810 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16811 {
16812 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16813 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16814 if (expr == error_mark_node)
16815 continue;
16816 tmp = TARGET_EXPR_SLOT (expr);
16817 add_stmt (expr);
16818 OMP_CLAUSE_OPERAND (c, 0) = expr;
16819 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16820 OMP_CLAUSE_FIRSTPRIVATE);
16821 OMP_CLAUSE_DECL (tc) = tmp;
16822 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16823 OMP_TARGET_CLAUSES (t) = tc;
16824 }
16825 }
16826 }
16827 add_stmt (t);
16828 break;
16829
16830 case OACC_DECLARE:
16831 t = copy_node (t);
16832 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16833 complain, in_decl);
16834 OACC_DECLARE_CLAUSES (t) = tmp;
16835 add_stmt (t);
16836 break;
16837
16838 case OMP_TARGET_UPDATE:
16839 case OMP_TARGET_ENTER_DATA:
16840 case OMP_TARGET_EXIT_DATA:
16841 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16842 complain, in_decl);
16843 t = copy_node (t);
16844 OMP_STANDALONE_CLAUSES (t) = tmp;
16845 add_stmt (t);
16846 break;
16847
16848 case OACC_ENTER_DATA:
16849 case OACC_EXIT_DATA:
16850 case OACC_UPDATE:
16851 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16852 complain, in_decl);
16853 t = copy_node (t);
16854 OMP_STANDALONE_CLAUSES (t) = tmp;
16855 add_stmt (t);
16856 break;
16857
16858 case OMP_ORDERED:
16859 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16860 complain, in_decl);
16861 stmt = push_stmt_list ();
16862 RECUR (OMP_BODY (t));
16863 stmt = pop_stmt_list (stmt);
16864
16865 t = copy_node (t);
16866 OMP_BODY (t) = stmt;
16867 OMP_ORDERED_CLAUSES (t) = tmp;
16868 add_stmt (t);
16869 break;
16870
16871 case OMP_SECTION:
16872 case OMP_MASTER:
16873 case OMP_TASKGROUP:
16874 stmt = push_stmt_list ();
16875 RECUR (OMP_BODY (t));
16876 stmt = pop_stmt_list (stmt);
16877
16878 t = copy_node (t);
16879 OMP_BODY (t) = stmt;
16880 add_stmt (t);
16881 break;
16882
16883 case OMP_ATOMIC:
16884 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16885 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16886 {
16887 tree op1 = TREE_OPERAND (t, 1);
16888 tree rhs1 = NULL_TREE;
16889 tree lhs, rhs;
16890 if (TREE_CODE (op1) == COMPOUND_EXPR)
16891 {
16892 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16893 op1 = TREE_OPERAND (op1, 1);
16894 }
16895 lhs = RECUR (TREE_OPERAND (op1, 0));
16896 rhs = RECUR (TREE_OPERAND (op1, 1));
16897 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16898 NULL_TREE, NULL_TREE, rhs1,
16899 OMP_ATOMIC_SEQ_CST (t));
16900 }
16901 else
16902 {
16903 tree op1 = TREE_OPERAND (t, 1);
16904 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16905 tree rhs1 = NULL_TREE;
16906 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16907 enum tree_code opcode = NOP_EXPR;
16908 if (code == OMP_ATOMIC_READ)
16909 {
16910 v = RECUR (TREE_OPERAND (op1, 0));
16911 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16912 }
16913 else if (code == OMP_ATOMIC_CAPTURE_OLD
16914 || code == OMP_ATOMIC_CAPTURE_NEW)
16915 {
16916 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16917 v = RECUR (TREE_OPERAND (op1, 0));
16918 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16919 if (TREE_CODE (op11) == COMPOUND_EXPR)
16920 {
16921 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16922 op11 = TREE_OPERAND (op11, 1);
16923 }
16924 lhs = RECUR (TREE_OPERAND (op11, 0));
16925 rhs = RECUR (TREE_OPERAND (op11, 1));
16926 opcode = TREE_CODE (op11);
16927 if (opcode == MODIFY_EXPR)
16928 opcode = NOP_EXPR;
16929 }
16930 else
16931 {
16932 code = OMP_ATOMIC;
16933 lhs = RECUR (TREE_OPERAND (op1, 0));
16934 rhs = RECUR (TREE_OPERAND (op1, 1));
16935 }
16936 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16937 OMP_ATOMIC_SEQ_CST (t));
16938 }
16939 break;
16940
16941 case TRANSACTION_EXPR:
16942 {
16943 int flags = 0;
16944 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16945 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16946
16947 if (TRANSACTION_EXPR_IS_STMT (t))
16948 {
16949 tree body = TRANSACTION_EXPR_BODY (t);
16950 tree noex = NULL_TREE;
16951 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16952 {
16953 noex = MUST_NOT_THROW_COND (body);
16954 if (noex == NULL_TREE)
16955 noex = boolean_true_node;
16956 body = TREE_OPERAND (body, 0);
16957 }
16958 stmt = begin_transaction_stmt (input_location, NULL, flags);
16959 RECUR (body);
16960 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16961 }
16962 else
16963 {
16964 stmt = build_transaction_expr (EXPR_LOCATION (t),
16965 RECUR (TRANSACTION_EXPR_BODY (t)),
16966 flags, NULL_TREE);
16967 RETURN (stmt);
16968 }
16969 }
16970 break;
16971
16972 case MUST_NOT_THROW_EXPR:
16973 {
16974 tree op0 = RECUR (TREE_OPERAND (t, 0));
16975 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16976 RETURN (build_must_not_throw_expr (op0, cond));
16977 }
16978
16979 case EXPR_PACK_EXPANSION:
16980 error ("invalid use of pack expansion expression");
16981 RETURN (error_mark_node);
16982
16983 case NONTYPE_ARGUMENT_PACK:
16984 error ("use %<...%> to expand argument pack");
16985 RETURN (error_mark_node);
16986
16987 case COMPOUND_EXPR:
16988 tmp = RECUR (TREE_OPERAND (t, 0));
16989 if (tmp == NULL_TREE)
16990 /* If the first operand was a statement, we're done with it. */
16991 RETURN (RECUR (TREE_OPERAND (t, 1)));
16992 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16993 RECUR (TREE_OPERAND (t, 1)),
16994 complain));
16995
16996 case ANNOTATE_EXPR:
16997 tmp = RECUR (TREE_OPERAND (t, 0));
16998 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16999 TREE_TYPE (tmp), tmp,
17000 RECUR (TREE_OPERAND (t, 1)),
17001 RECUR (TREE_OPERAND (t, 2))));
17002
17003 default:
17004 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17005
17006 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17007 /*function_p=*/false,
17008 integral_constant_expression_p));
17009 }
17010
17011 RETURN (NULL_TREE);
17012 out:
17013 input_location = loc;
17014 return r;
17015 #undef RECUR
17016 #undef RETURN
17017 }
17018
17019 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17020 function. For description of the body see comment above
17021 cp_parser_omp_declare_reduction_exprs. */
17022
17023 static void
17024 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17025 {
17026 if (t == NULL_TREE || t == error_mark_node)
17027 return;
17028
17029 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17030
17031 tree_stmt_iterator tsi;
17032 int i;
17033 tree stmts[7];
17034 memset (stmts, 0, sizeof stmts);
17035 for (i = 0, tsi = tsi_start (t);
17036 i < 7 && !tsi_end_p (tsi);
17037 i++, tsi_next (&tsi))
17038 stmts[i] = tsi_stmt (tsi);
17039 gcc_assert (tsi_end_p (tsi));
17040
17041 if (i >= 3)
17042 {
17043 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17044 && TREE_CODE (stmts[1]) == DECL_EXPR);
17045 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17046 args, complain, in_decl);
17047 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17048 args, complain, in_decl);
17049 DECL_CONTEXT (omp_out) = current_function_decl;
17050 DECL_CONTEXT (omp_in) = current_function_decl;
17051 keep_next_level (true);
17052 tree block = begin_omp_structured_block ();
17053 tsubst_expr (stmts[2], args, complain, in_decl, false);
17054 block = finish_omp_structured_block (block);
17055 block = maybe_cleanup_point_expr_void (block);
17056 add_decl_expr (omp_out);
17057 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17058 TREE_NO_WARNING (omp_out) = 1;
17059 add_decl_expr (omp_in);
17060 finish_expr_stmt (block);
17061 }
17062 if (i >= 6)
17063 {
17064 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17065 && TREE_CODE (stmts[4]) == DECL_EXPR);
17066 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17067 args, complain, in_decl);
17068 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17069 args, complain, in_decl);
17070 DECL_CONTEXT (omp_priv) = current_function_decl;
17071 DECL_CONTEXT (omp_orig) = current_function_decl;
17072 keep_next_level (true);
17073 tree block = begin_omp_structured_block ();
17074 tsubst_expr (stmts[5], args, complain, in_decl, false);
17075 block = finish_omp_structured_block (block);
17076 block = maybe_cleanup_point_expr_void (block);
17077 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17078 add_decl_expr (omp_priv);
17079 add_decl_expr (omp_orig);
17080 finish_expr_stmt (block);
17081 if (i == 7)
17082 add_decl_expr (omp_orig);
17083 }
17084 }
17085
17086 /* T is a postfix-expression that is not being used in a function
17087 call. Return the substituted version of T. */
17088
17089 static tree
17090 tsubst_non_call_postfix_expression (tree t, tree args,
17091 tsubst_flags_t complain,
17092 tree in_decl)
17093 {
17094 if (TREE_CODE (t) == SCOPE_REF)
17095 t = tsubst_qualified_id (t, args, complain, in_decl,
17096 /*done=*/false, /*address_p=*/false);
17097 else
17098 t = tsubst_copy_and_build (t, args, complain, in_decl,
17099 /*function_p=*/false,
17100 /*integral_constant_expression_p=*/false);
17101
17102 return t;
17103 }
17104
17105 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17106 instantiation context. Instantiating a pack expansion containing a lambda
17107 might result in multiple lambdas all based on the same lambda in the
17108 template. */
17109
17110 tree
17111 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17112 {
17113 tree oldfn = lambda_function (t);
17114 in_decl = oldfn;
17115
17116 tree r = build_lambda_expr ();
17117
17118 LAMBDA_EXPR_LOCATION (r)
17119 = LAMBDA_EXPR_LOCATION (t);
17120 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17121 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17122 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17123
17124 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
17125 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
17126 else
17127 record_lambda_scope (r);
17128
17129 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17130 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17131
17132 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
17133 cap = TREE_CHAIN (cap))
17134 {
17135 tree field = TREE_PURPOSE (cap);
17136 if (PACK_EXPANSION_P (field))
17137 field = PACK_EXPANSION_PATTERN (field);
17138 field = tsubst_decl (field, args, complain);
17139
17140 if (field == error_mark_node)
17141 return error_mark_node;
17142
17143 tree init = TREE_VALUE (cap);
17144 if (PACK_EXPANSION_P (init))
17145 init = tsubst_pack_expansion (init, args, complain, in_decl);
17146 else
17147 init = tsubst_copy_and_build (init, args, complain, in_decl,
17148 /*fn*/false, /*constexpr*/false);
17149
17150 if (TREE_CODE (field) == TREE_VEC)
17151 {
17152 int len = TREE_VEC_LENGTH (field);
17153 gcc_assert (TREE_CODE (init) == TREE_VEC
17154 && TREE_VEC_LENGTH (init) == len);
17155 for (int i = 0; i < len; ++i)
17156 LAMBDA_EXPR_CAPTURE_LIST (r)
17157 = tree_cons (TREE_VEC_ELT (field, i),
17158 TREE_VEC_ELT (init, i),
17159 LAMBDA_EXPR_CAPTURE_LIST (r));
17160 }
17161 else
17162 {
17163 LAMBDA_EXPR_CAPTURE_LIST (r)
17164 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17165
17166 if (id_equal (DECL_NAME (field), "__this"))
17167 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17168 }
17169 }
17170
17171 tree type = begin_lambda_type (r);
17172
17173 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17174 determine_visibility (TYPE_NAME (type));
17175
17176 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17177
17178 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17179 ? DECL_TI_TEMPLATE (oldfn)
17180 : NULL_TREE);
17181
17182 tree fntype = static_fn_type (oldfn);
17183 if (oldtmpl)
17184 ++processing_template_decl;
17185 fntype = tsubst (fntype, args, complain, in_decl);
17186 if (oldtmpl)
17187 --processing_template_decl;
17188
17189 if (fntype == error_mark_node)
17190 r = error_mark_node;
17191 else
17192 {
17193 /* Fix the type of 'this'. */
17194 fntype = build_memfn_type (fntype, type,
17195 type_memfn_quals (fntype),
17196 type_memfn_rqual (fntype));
17197 tree fn, tmpl;
17198 if (oldtmpl)
17199 {
17200 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17201 fn = DECL_TEMPLATE_RESULT (tmpl);
17202 finish_member_declaration (tmpl);
17203 }
17204 else
17205 {
17206 tmpl = NULL_TREE;
17207 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17208 finish_member_declaration (fn);
17209 }
17210
17211 /* Let finish_function set this. */
17212 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17213
17214 bool nested = cfun;
17215 if (nested)
17216 push_function_context ();
17217 else
17218 /* Still increment function_depth so that we don't GC in the
17219 middle of an expression. */
17220 ++function_depth;
17221
17222 local_specialization_stack s (lss_copy);
17223
17224 tree body = start_lambda_function (fn, r);
17225
17226 register_parameter_specializations (oldfn, fn);
17227
17228 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17229 /*constexpr*/false);
17230
17231 finish_lambda_function (body);
17232
17233 if (nested)
17234 pop_function_context ();
17235 else
17236 --function_depth;
17237
17238 /* The capture list was built up in reverse order; fix that now. */
17239 LAMBDA_EXPR_CAPTURE_LIST (r)
17240 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17241
17242 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17243
17244 maybe_add_lambda_conv_op (type);
17245 }
17246
17247 finish_struct (type, /*attr*/NULL_TREE);
17248
17249 insert_pending_capture_proxies ();
17250
17251 return r;
17252 }
17253
17254 /* Like tsubst but deals with expressions and performs semantic
17255 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17256
17257 tree
17258 tsubst_copy_and_build (tree t,
17259 tree args,
17260 tsubst_flags_t complain,
17261 tree in_decl,
17262 bool function_p,
17263 bool integral_constant_expression_p)
17264 {
17265 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17266 #define RECUR(NODE) \
17267 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17268 /*function_p=*/false, \
17269 integral_constant_expression_p)
17270
17271 tree retval, op1;
17272 location_t loc;
17273
17274 if (t == NULL_TREE || t == error_mark_node)
17275 return t;
17276
17277 loc = input_location;
17278 if (EXPR_HAS_LOCATION (t))
17279 input_location = EXPR_LOCATION (t);
17280
17281 /* N3276 decltype magic only applies to calls at the top level or on the
17282 right side of a comma. */
17283 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17284 complain &= ~tf_decltype;
17285
17286 switch (TREE_CODE (t))
17287 {
17288 case USING_DECL:
17289 t = DECL_NAME (t);
17290 /* Fall through. */
17291 case IDENTIFIER_NODE:
17292 {
17293 tree decl;
17294 cp_id_kind idk;
17295 bool non_integral_constant_expression_p;
17296 const char *error_msg;
17297
17298 if (IDENTIFIER_CONV_OP_P (t))
17299 {
17300 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17301 t = make_conv_op_name (new_type);
17302 }
17303
17304 /* Look up the name. */
17305 decl = lookup_name (t);
17306
17307 /* By convention, expressions use ERROR_MARK_NODE to indicate
17308 failure, not NULL_TREE. */
17309 if (decl == NULL_TREE)
17310 decl = error_mark_node;
17311
17312 decl = finish_id_expression (t, decl, NULL_TREE,
17313 &idk,
17314 integral_constant_expression_p,
17315 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17316 &non_integral_constant_expression_p,
17317 /*template_p=*/false,
17318 /*done=*/true,
17319 /*address_p=*/false,
17320 /*template_arg_p=*/false,
17321 &error_msg,
17322 input_location);
17323 if (error_msg)
17324 error (error_msg);
17325 if (!function_p && identifier_p (decl))
17326 {
17327 if (complain & tf_error)
17328 unqualified_name_lookup_error (decl);
17329 decl = error_mark_node;
17330 }
17331 RETURN (decl);
17332 }
17333
17334 case TEMPLATE_ID_EXPR:
17335 {
17336 tree object;
17337 tree templ = RECUR (TREE_OPERAND (t, 0));
17338 tree targs = TREE_OPERAND (t, 1);
17339
17340 if (targs)
17341 targs = tsubst_template_args (targs, args, complain, in_decl);
17342 if (targs == error_mark_node)
17343 RETURN (error_mark_node);
17344
17345 if (TREE_CODE (templ) == SCOPE_REF)
17346 {
17347 tree name = TREE_OPERAND (templ, 1);
17348 tree tid = lookup_template_function (name, targs);
17349 TREE_OPERAND (templ, 1) = tid;
17350 RETURN (templ);
17351 }
17352
17353 if (variable_template_p (templ))
17354 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17355
17356 if (TREE_CODE (templ) == COMPONENT_REF)
17357 {
17358 object = TREE_OPERAND (templ, 0);
17359 templ = TREE_OPERAND (templ, 1);
17360 }
17361 else
17362 object = NULL_TREE;
17363 templ = lookup_template_function (templ, targs);
17364
17365 if (object)
17366 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17367 object, templ, NULL_TREE));
17368 else
17369 RETURN (baselink_for_fns (templ));
17370 }
17371
17372 case INDIRECT_REF:
17373 {
17374 tree r = RECUR (TREE_OPERAND (t, 0));
17375
17376 if (REFERENCE_REF_P (t))
17377 {
17378 /* A type conversion to reference type will be enclosed in
17379 such an indirect ref, but the substitution of the cast
17380 will have also added such an indirect ref. */
17381 r = convert_from_reference (r);
17382 }
17383 else
17384 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17385 complain|decltype_flag);
17386
17387 if (REF_PARENTHESIZED_P (t))
17388 r = force_paren_expr (r);
17389
17390 RETURN (r);
17391 }
17392
17393 case NOP_EXPR:
17394 {
17395 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17396 tree op0 = RECUR (TREE_OPERAND (t, 0));
17397 RETURN (build_nop (type, op0));
17398 }
17399
17400 case IMPLICIT_CONV_EXPR:
17401 {
17402 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17403 tree expr = RECUR (TREE_OPERAND (t, 0));
17404 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17405 {
17406 retval = copy_node (t);
17407 TREE_TYPE (retval) = type;
17408 TREE_OPERAND (retval, 0) = expr;
17409 RETURN (retval);
17410 }
17411 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17412 /* We'll pass this to convert_nontype_argument again, we don't need
17413 to actually perform any conversion here. */
17414 RETURN (expr);
17415 int flags = LOOKUP_IMPLICIT;
17416 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17417 flags = LOOKUP_NORMAL;
17418 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17419 flags));
17420 }
17421
17422 case CONVERT_EXPR:
17423 {
17424 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17425 tree op0 = RECUR (TREE_OPERAND (t, 0));
17426 if (op0 == error_mark_node)
17427 RETURN (error_mark_node);
17428 RETURN (build1 (CONVERT_EXPR, type, op0));
17429 }
17430
17431 case CAST_EXPR:
17432 case REINTERPRET_CAST_EXPR:
17433 case CONST_CAST_EXPR:
17434 case DYNAMIC_CAST_EXPR:
17435 case STATIC_CAST_EXPR:
17436 {
17437 tree type;
17438 tree op, r = NULL_TREE;
17439
17440 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17441 if (integral_constant_expression_p
17442 && !cast_valid_in_integral_constant_expression_p (type))
17443 {
17444 if (complain & tf_error)
17445 error ("a cast to a type other than an integral or "
17446 "enumeration type cannot appear in a constant-expression");
17447 RETURN (error_mark_node);
17448 }
17449
17450 op = RECUR (TREE_OPERAND (t, 0));
17451
17452 warning_sentinel s(warn_useless_cast);
17453 warning_sentinel s2(warn_ignored_qualifiers);
17454 switch (TREE_CODE (t))
17455 {
17456 case CAST_EXPR:
17457 r = build_functional_cast (type, op, complain);
17458 break;
17459 case REINTERPRET_CAST_EXPR:
17460 r = build_reinterpret_cast (type, op, complain);
17461 break;
17462 case CONST_CAST_EXPR:
17463 r = build_const_cast (type, op, complain);
17464 break;
17465 case DYNAMIC_CAST_EXPR:
17466 r = build_dynamic_cast (type, op, complain);
17467 break;
17468 case STATIC_CAST_EXPR:
17469 r = build_static_cast (type, op, complain);
17470 break;
17471 default:
17472 gcc_unreachable ();
17473 }
17474
17475 RETURN (r);
17476 }
17477
17478 case POSTDECREMENT_EXPR:
17479 case POSTINCREMENT_EXPR:
17480 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17481 args, complain, in_decl);
17482 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17483 complain|decltype_flag));
17484
17485 case PREDECREMENT_EXPR:
17486 case PREINCREMENT_EXPR:
17487 case NEGATE_EXPR:
17488 case BIT_NOT_EXPR:
17489 case ABS_EXPR:
17490 case TRUTH_NOT_EXPR:
17491 case UNARY_PLUS_EXPR: /* Unary + */
17492 case REALPART_EXPR:
17493 case IMAGPART_EXPR:
17494 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17495 RECUR (TREE_OPERAND (t, 0)),
17496 complain|decltype_flag));
17497
17498 case FIX_TRUNC_EXPR:
17499 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17500 false, complain));
17501
17502 case ADDR_EXPR:
17503 op1 = TREE_OPERAND (t, 0);
17504 if (TREE_CODE (op1) == LABEL_DECL)
17505 RETURN (finish_label_address_expr (DECL_NAME (op1),
17506 EXPR_LOCATION (op1)));
17507 if (TREE_CODE (op1) == SCOPE_REF)
17508 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17509 /*done=*/true, /*address_p=*/true);
17510 else
17511 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17512 in_decl);
17513 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17514 complain|decltype_flag));
17515
17516 case PLUS_EXPR:
17517 case MINUS_EXPR:
17518 case MULT_EXPR:
17519 case TRUNC_DIV_EXPR:
17520 case CEIL_DIV_EXPR:
17521 case FLOOR_DIV_EXPR:
17522 case ROUND_DIV_EXPR:
17523 case EXACT_DIV_EXPR:
17524 case BIT_AND_EXPR:
17525 case BIT_IOR_EXPR:
17526 case BIT_XOR_EXPR:
17527 case TRUNC_MOD_EXPR:
17528 case FLOOR_MOD_EXPR:
17529 case TRUTH_ANDIF_EXPR:
17530 case TRUTH_ORIF_EXPR:
17531 case TRUTH_AND_EXPR:
17532 case TRUTH_OR_EXPR:
17533 case RSHIFT_EXPR:
17534 case LSHIFT_EXPR:
17535 case RROTATE_EXPR:
17536 case LROTATE_EXPR:
17537 case EQ_EXPR:
17538 case NE_EXPR:
17539 case MAX_EXPR:
17540 case MIN_EXPR:
17541 case LE_EXPR:
17542 case GE_EXPR:
17543 case LT_EXPR:
17544 case GT_EXPR:
17545 case MEMBER_REF:
17546 case DOTSTAR_EXPR:
17547 {
17548 warning_sentinel s1(warn_type_limits);
17549 warning_sentinel s2(warn_div_by_zero);
17550 warning_sentinel s3(warn_logical_op);
17551 warning_sentinel s4(warn_tautological_compare);
17552 tree op0 = RECUR (TREE_OPERAND (t, 0));
17553 tree op1 = RECUR (TREE_OPERAND (t, 1));
17554 tree r = build_x_binary_op
17555 (input_location, TREE_CODE (t),
17556 op0,
17557 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17558 ? ERROR_MARK
17559 : TREE_CODE (TREE_OPERAND (t, 0))),
17560 op1,
17561 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17562 ? ERROR_MARK
17563 : TREE_CODE (TREE_OPERAND (t, 1))),
17564 /*overload=*/NULL,
17565 complain|decltype_flag);
17566 if (EXPR_P (r) && TREE_NO_WARNING (t))
17567 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17568
17569 RETURN (r);
17570 }
17571
17572 case POINTER_PLUS_EXPR:
17573 {
17574 tree op0 = RECUR (TREE_OPERAND (t, 0));
17575 tree op1 = RECUR (TREE_OPERAND (t, 1));
17576 RETURN (fold_build_pointer_plus (op0, op1));
17577 }
17578
17579 case SCOPE_REF:
17580 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17581 /*address_p=*/false));
17582 case ARRAY_REF:
17583 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17584 args, complain, in_decl);
17585 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17586 RECUR (TREE_OPERAND (t, 1)),
17587 complain|decltype_flag));
17588
17589 case SIZEOF_EXPR:
17590 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17591 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17592 RETURN (tsubst_copy (t, args, complain, in_decl));
17593 /* Fall through */
17594
17595 case ALIGNOF_EXPR:
17596 {
17597 tree r;
17598
17599 op1 = TREE_OPERAND (t, 0);
17600 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17601 op1 = TREE_TYPE (op1);
17602 if (!args)
17603 {
17604 /* When there are no ARGS, we are trying to evaluate a
17605 non-dependent expression from the parser. Trying to do
17606 the substitutions may not work. */
17607 if (!TYPE_P (op1))
17608 op1 = TREE_TYPE (op1);
17609 }
17610 else
17611 {
17612 ++cp_unevaluated_operand;
17613 ++c_inhibit_evaluation_warnings;
17614 if (TYPE_P (op1))
17615 op1 = tsubst (op1, args, complain, in_decl);
17616 else
17617 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17618 /*function_p=*/false,
17619 /*integral_constant_expression_p=*/
17620 false);
17621 --cp_unevaluated_operand;
17622 --c_inhibit_evaluation_warnings;
17623 }
17624 if (TYPE_P (op1))
17625 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17626 complain & tf_error);
17627 else
17628 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17629 complain & tf_error);
17630 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17631 {
17632 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17633 {
17634 if (!processing_template_decl && TYPE_P (op1))
17635 {
17636 r = build_min (SIZEOF_EXPR, size_type_node,
17637 build1 (NOP_EXPR, op1, error_mark_node));
17638 SIZEOF_EXPR_TYPE_P (r) = 1;
17639 }
17640 else
17641 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17642 TREE_SIDE_EFFECTS (r) = 0;
17643 TREE_READONLY (r) = 1;
17644 }
17645 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17646 }
17647 RETURN (r);
17648 }
17649
17650 case AT_ENCODE_EXPR:
17651 {
17652 op1 = TREE_OPERAND (t, 0);
17653 ++cp_unevaluated_operand;
17654 ++c_inhibit_evaluation_warnings;
17655 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17656 /*function_p=*/false,
17657 /*integral_constant_expression_p=*/false);
17658 --cp_unevaluated_operand;
17659 --c_inhibit_evaluation_warnings;
17660 RETURN (objc_build_encode_expr (op1));
17661 }
17662
17663 case NOEXCEPT_EXPR:
17664 op1 = TREE_OPERAND (t, 0);
17665 ++cp_unevaluated_operand;
17666 ++c_inhibit_evaluation_warnings;
17667 ++cp_noexcept_operand;
17668 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17669 /*function_p=*/false,
17670 /*integral_constant_expression_p=*/false);
17671 --cp_unevaluated_operand;
17672 --c_inhibit_evaluation_warnings;
17673 --cp_noexcept_operand;
17674 RETURN (finish_noexcept_expr (op1, complain));
17675
17676 case MODOP_EXPR:
17677 {
17678 warning_sentinel s(warn_div_by_zero);
17679 tree lhs = RECUR (TREE_OPERAND (t, 0));
17680 tree rhs = RECUR (TREE_OPERAND (t, 2));
17681 tree r = build_x_modify_expr
17682 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17683 complain|decltype_flag);
17684 /* TREE_NO_WARNING must be set if either the expression was
17685 parenthesized or it uses an operator such as >>= rather
17686 than plain assignment. In the former case, it was already
17687 set and must be copied. In the latter case,
17688 build_x_modify_expr sets it and it must not be reset
17689 here. */
17690 if (TREE_NO_WARNING (t))
17691 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17692
17693 RETURN (r);
17694 }
17695
17696 case ARROW_EXPR:
17697 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17698 args, complain, in_decl);
17699 /* Remember that there was a reference to this entity. */
17700 if (DECL_P (op1)
17701 && !mark_used (op1, complain) && !(complain & tf_error))
17702 RETURN (error_mark_node);
17703 RETURN (build_x_arrow (input_location, op1, complain));
17704
17705 case NEW_EXPR:
17706 {
17707 tree placement = RECUR (TREE_OPERAND (t, 0));
17708 tree init = RECUR (TREE_OPERAND (t, 3));
17709 vec<tree, va_gc> *placement_vec;
17710 vec<tree, va_gc> *init_vec;
17711 tree ret;
17712
17713 if (placement == NULL_TREE)
17714 placement_vec = NULL;
17715 else
17716 {
17717 placement_vec = make_tree_vector ();
17718 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17719 vec_safe_push (placement_vec, TREE_VALUE (placement));
17720 }
17721
17722 /* If there was an initializer in the original tree, but it
17723 instantiated to an empty list, then we should pass a
17724 non-NULL empty vector to tell build_new that it was an
17725 empty initializer() rather than no initializer. This can
17726 only happen when the initializer is a pack expansion whose
17727 parameter packs are of length zero. */
17728 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17729 init_vec = NULL;
17730 else
17731 {
17732 init_vec = make_tree_vector ();
17733 if (init == void_node)
17734 gcc_assert (init_vec != NULL);
17735 else
17736 {
17737 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17738 vec_safe_push (init_vec, TREE_VALUE (init));
17739 }
17740 }
17741
17742 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17743 tree op2 = RECUR (TREE_OPERAND (t, 2));
17744 ret = build_new (&placement_vec, op1, op2, &init_vec,
17745 NEW_EXPR_USE_GLOBAL (t),
17746 complain);
17747
17748 if (placement_vec != NULL)
17749 release_tree_vector (placement_vec);
17750 if (init_vec != NULL)
17751 release_tree_vector (init_vec);
17752
17753 RETURN (ret);
17754 }
17755
17756 case DELETE_EXPR:
17757 {
17758 tree op0 = RECUR (TREE_OPERAND (t, 0));
17759 tree op1 = RECUR (TREE_OPERAND (t, 1));
17760 RETURN (delete_sanity (op0, op1,
17761 DELETE_EXPR_USE_VEC (t),
17762 DELETE_EXPR_USE_GLOBAL (t),
17763 complain));
17764 }
17765
17766 case COMPOUND_EXPR:
17767 {
17768 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17769 complain & ~tf_decltype, in_decl,
17770 /*function_p=*/false,
17771 integral_constant_expression_p);
17772 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17773 op0,
17774 RECUR (TREE_OPERAND (t, 1)),
17775 complain|decltype_flag));
17776 }
17777
17778 case CALL_EXPR:
17779 {
17780 tree function;
17781 vec<tree, va_gc> *call_args;
17782 unsigned int nargs, i;
17783 bool qualified_p;
17784 bool koenig_p;
17785 tree ret;
17786
17787 function = CALL_EXPR_FN (t);
17788 /* Internal function with no arguments. */
17789 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17790 RETURN (t);
17791
17792 /* When we parsed the expression, we determined whether or
17793 not Koenig lookup should be performed. */
17794 koenig_p = KOENIG_LOOKUP_P (t);
17795 if (function == NULL_TREE)
17796 {
17797 koenig_p = false;
17798 qualified_p = false;
17799 }
17800 else if (TREE_CODE (function) == SCOPE_REF)
17801 {
17802 qualified_p = true;
17803 function = tsubst_qualified_id (function, args, complain, in_decl,
17804 /*done=*/false,
17805 /*address_p=*/false);
17806 }
17807 else if (koenig_p && identifier_p (function))
17808 {
17809 /* Do nothing; calling tsubst_copy_and_build on an identifier
17810 would incorrectly perform unqualified lookup again.
17811
17812 Note that we can also have an IDENTIFIER_NODE if the earlier
17813 unqualified lookup found a member function; in that case
17814 koenig_p will be false and we do want to do the lookup
17815 again to find the instantiated member function.
17816
17817 FIXME but doing that causes c++/15272, so we need to stop
17818 using IDENTIFIER_NODE in that situation. */
17819 qualified_p = false;
17820 }
17821 else
17822 {
17823 if (TREE_CODE (function) == COMPONENT_REF)
17824 {
17825 tree op = TREE_OPERAND (function, 1);
17826
17827 qualified_p = (TREE_CODE (op) == SCOPE_REF
17828 || (BASELINK_P (op)
17829 && BASELINK_QUALIFIED_P (op)));
17830 }
17831 else
17832 qualified_p = false;
17833
17834 if (TREE_CODE (function) == ADDR_EXPR
17835 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17836 /* Avoid error about taking the address of a constructor. */
17837 function = TREE_OPERAND (function, 0);
17838
17839 function = tsubst_copy_and_build (function, args, complain,
17840 in_decl,
17841 !qualified_p,
17842 integral_constant_expression_p);
17843
17844 if (BASELINK_P (function))
17845 qualified_p = true;
17846 }
17847
17848 nargs = call_expr_nargs (t);
17849 call_args = make_tree_vector ();
17850 for (i = 0; i < nargs; ++i)
17851 {
17852 tree arg = CALL_EXPR_ARG (t, i);
17853
17854 if (!PACK_EXPANSION_P (arg))
17855 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17856 else
17857 {
17858 /* Expand the pack expansion and push each entry onto
17859 CALL_ARGS. */
17860 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17861 if (TREE_CODE (arg) == TREE_VEC)
17862 {
17863 unsigned int len, j;
17864
17865 len = TREE_VEC_LENGTH (arg);
17866 for (j = 0; j < len; ++j)
17867 {
17868 tree value = TREE_VEC_ELT (arg, j);
17869 if (value != NULL_TREE)
17870 value = convert_from_reference (value);
17871 vec_safe_push (call_args, value);
17872 }
17873 }
17874 else
17875 {
17876 /* A partial substitution. Add one entry. */
17877 vec_safe_push (call_args, arg);
17878 }
17879 }
17880 }
17881
17882 /* We do not perform argument-dependent lookup if normal
17883 lookup finds a non-function, in accordance with the
17884 expected resolution of DR 218. */
17885 if (koenig_p
17886 && ((is_overloaded_fn (function)
17887 /* If lookup found a member function, the Koenig lookup is
17888 not appropriate, even if an unqualified-name was used
17889 to denote the function. */
17890 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17891 || identifier_p (function))
17892 /* Only do this when substitution turns a dependent call
17893 into a non-dependent call. */
17894 && type_dependent_expression_p_push (t)
17895 && !any_type_dependent_arguments_p (call_args))
17896 function = perform_koenig_lookup (function, call_args, tf_none);
17897
17898 if (function != NULL_TREE
17899 && identifier_p (function)
17900 && !any_type_dependent_arguments_p (call_args))
17901 {
17902 if (koenig_p && (complain & tf_warning_or_error))
17903 {
17904 /* For backwards compatibility and good diagnostics, try
17905 the unqualified lookup again if we aren't in SFINAE
17906 context. */
17907 tree unq = (tsubst_copy_and_build
17908 (function, args, complain, in_decl, true,
17909 integral_constant_expression_p));
17910 if (unq == error_mark_node)
17911 {
17912 release_tree_vector (call_args);
17913 RETURN (error_mark_node);
17914 }
17915
17916 if (unq != function)
17917 {
17918 /* In a lambda fn, we have to be careful to not
17919 introduce new this captures. Legacy code can't
17920 be using lambdas anyway, so it's ok to be
17921 stricter. */
17922 bool in_lambda = (current_class_type
17923 && LAMBDA_TYPE_P (current_class_type));
17924 char const *const msg
17925 = G_("%qD was not declared in this scope, "
17926 "and no declarations were found by "
17927 "argument-dependent lookup at the point "
17928 "of instantiation");
17929
17930 bool diag = true;
17931 if (in_lambda)
17932 error_at (EXPR_LOC_OR_LOC (t, input_location),
17933 msg, function);
17934 else
17935 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17936 msg, function);
17937 if (diag)
17938 {
17939 tree fn = unq;
17940
17941 if (INDIRECT_REF_P (fn))
17942 fn = TREE_OPERAND (fn, 0);
17943 if (is_overloaded_fn (fn))
17944 fn = get_first_fn (fn);
17945
17946 if (!DECL_P (fn))
17947 /* Can't say anything more. */;
17948 else if (DECL_CLASS_SCOPE_P (fn))
17949 {
17950 location_t loc = EXPR_LOC_OR_LOC (t,
17951 input_location);
17952 inform (loc,
17953 "declarations in dependent base %qT are "
17954 "not found by unqualified lookup",
17955 DECL_CLASS_CONTEXT (fn));
17956 if (current_class_ptr)
17957 inform (loc,
17958 "use %<this->%D%> instead", function);
17959 else
17960 inform (loc,
17961 "use %<%T::%D%> instead",
17962 current_class_name, function);
17963 }
17964 else
17965 inform (DECL_SOURCE_LOCATION (fn),
17966 "%qD declared here, later in the "
17967 "translation unit", fn);
17968 if (in_lambda)
17969 {
17970 release_tree_vector (call_args);
17971 RETURN (error_mark_node);
17972 }
17973 }
17974
17975 function = unq;
17976 }
17977 }
17978 if (identifier_p (function))
17979 {
17980 if (complain & tf_error)
17981 unqualified_name_lookup_error (function);
17982 release_tree_vector (call_args);
17983 RETURN (error_mark_node);
17984 }
17985 }
17986
17987 /* Remember that there was a reference to this entity. */
17988 if (function != NULL_TREE
17989 && DECL_P (function)
17990 && !mark_used (function, complain) && !(complain & tf_error))
17991 {
17992 release_tree_vector (call_args);
17993 RETURN (error_mark_node);
17994 }
17995
17996 /* Put back tf_decltype for the actual call. */
17997 complain |= decltype_flag;
17998
17999 if (function == NULL_TREE)
18000 switch (CALL_EXPR_IFN (t))
18001 {
18002 case IFN_LAUNDER:
18003 gcc_assert (nargs == 1);
18004 if (vec_safe_length (call_args) != 1)
18005 {
18006 error_at (EXPR_LOC_OR_LOC (t, input_location),
18007 "wrong number of arguments to "
18008 "%<__builtin_launder%>");
18009 ret = error_mark_node;
18010 }
18011 else
18012 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
18013 input_location),
18014 (*call_args)[0], complain);
18015 break;
18016
18017 default:
18018 /* Unsupported internal function with arguments. */
18019 gcc_unreachable ();
18020 }
18021 else if (TREE_CODE (function) == OFFSET_REF)
18022 ret = build_offset_ref_call_from_tree (function, &call_args,
18023 complain);
18024 else if (TREE_CODE (function) == COMPONENT_REF)
18025 {
18026 tree instance = TREE_OPERAND (function, 0);
18027 tree fn = TREE_OPERAND (function, 1);
18028
18029 if (processing_template_decl
18030 && (type_dependent_expression_p (instance)
18031 || (!BASELINK_P (fn)
18032 && TREE_CODE (fn) != FIELD_DECL)
18033 || type_dependent_expression_p (fn)
18034 || any_type_dependent_arguments_p (call_args)))
18035 ret = build_min_nt_call_vec (function, call_args);
18036 else if (!BASELINK_P (fn))
18037 ret = finish_call_expr (function, &call_args,
18038 /*disallow_virtual=*/false,
18039 /*koenig_p=*/false,
18040 complain);
18041 else
18042 ret = (build_new_method_call
18043 (instance, fn,
18044 &call_args, NULL_TREE,
18045 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
18046 /*fn_p=*/NULL,
18047 complain));
18048 }
18049 else
18050 ret = finish_call_expr (function, &call_args,
18051 /*disallow_virtual=*/qualified_p,
18052 koenig_p,
18053 complain);
18054
18055 release_tree_vector (call_args);
18056
18057 if (ret != error_mark_node)
18058 {
18059 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
18060 bool ord = CALL_EXPR_ORDERED_ARGS (t);
18061 bool rev = CALL_EXPR_REVERSE_ARGS (t);
18062 bool thk = CALL_FROM_THUNK_P (t);
18063 if (op || ord || rev || thk)
18064 {
18065 function = extract_call_expr (ret);
18066 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
18067 CALL_EXPR_ORDERED_ARGS (function) = ord;
18068 CALL_EXPR_REVERSE_ARGS (function) = rev;
18069 if (thk)
18070 {
18071 if (TREE_CODE (function) == CALL_EXPR)
18072 CALL_FROM_THUNK_P (function) = true;
18073 else
18074 AGGR_INIT_FROM_THUNK_P (function) = true;
18075 /* The thunk location is not interesting. */
18076 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
18077 }
18078 }
18079 }
18080
18081 RETURN (ret);
18082 }
18083
18084 case COND_EXPR:
18085 {
18086 tree cond = RECUR (TREE_OPERAND (t, 0));
18087 tree folded_cond = fold_non_dependent_expr (cond);
18088 tree exp1, exp2;
18089
18090 if (TREE_CODE (folded_cond) == INTEGER_CST)
18091 {
18092 if (integer_zerop (folded_cond))
18093 {
18094 ++c_inhibit_evaluation_warnings;
18095 exp1 = RECUR (TREE_OPERAND (t, 1));
18096 --c_inhibit_evaluation_warnings;
18097 exp2 = RECUR (TREE_OPERAND (t, 2));
18098 }
18099 else
18100 {
18101 exp1 = RECUR (TREE_OPERAND (t, 1));
18102 ++c_inhibit_evaluation_warnings;
18103 exp2 = RECUR (TREE_OPERAND (t, 2));
18104 --c_inhibit_evaluation_warnings;
18105 }
18106 cond = folded_cond;
18107 }
18108 else
18109 {
18110 exp1 = RECUR (TREE_OPERAND (t, 1));
18111 exp2 = RECUR (TREE_OPERAND (t, 2));
18112 }
18113
18114 warning_sentinel s(warn_duplicated_branches);
18115 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
18116 cond, exp1, exp2, complain));
18117 }
18118
18119 case PSEUDO_DTOR_EXPR:
18120 {
18121 tree op0 = RECUR (TREE_OPERAND (t, 0));
18122 tree op1 = RECUR (TREE_OPERAND (t, 1));
18123 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
18124 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
18125 input_location));
18126 }
18127
18128 case TREE_LIST:
18129 {
18130 tree purpose, value, chain;
18131
18132 if (t == void_list_node)
18133 RETURN (t);
18134
18135 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
18136 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
18137 {
18138 /* We have pack expansions, so expand those and
18139 create a new list out of it. */
18140 tree purposevec = NULL_TREE;
18141 tree valuevec = NULL_TREE;
18142 tree chain;
18143 int i, len = -1;
18144
18145 /* Expand the argument expressions. */
18146 if (TREE_PURPOSE (t))
18147 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
18148 complain, in_decl);
18149 if (TREE_VALUE (t))
18150 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
18151 complain, in_decl);
18152
18153 /* Build the rest of the list. */
18154 chain = TREE_CHAIN (t);
18155 if (chain && chain != void_type_node)
18156 chain = RECUR (chain);
18157
18158 /* Determine the number of arguments. */
18159 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
18160 {
18161 len = TREE_VEC_LENGTH (purposevec);
18162 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18163 }
18164 else if (TREE_CODE (valuevec) == TREE_VEC)
18165 len = TREE_VEC_LENGTH (valuevec);
18166 else
18167 {
18168 /* Since we only performed a partial substitution into
18169 the argument pack, we only RETURN (a single list
18170 node. */
18171 if (purposevec == TREE_PURPOSE (t)
18172 && valuevec == TREE_VALUE (t)
18173 && chain == TREE_CHAIN (t))
18174 RETURN (t);
18175
18176 RETURN (tree_cons (purposevec, valuevec, chain));
18177 }
18178
18179 /* Convert the argument vectors into a TREE_LIST */
18180 i = len;
18181 while (i > 0)
18182 {
18183 /* Grab the Ith values. */
18184 i--;
18185 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
18186 : NULL_TREE;
18187 value
18188 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
18189 : NULL_TREE;
18190
18191 /* Build the list (backwards). */
18192 chain = tree_cons (purpose, value, chain);
18193 }
18194
18195 RETURN (chain);
18196 }
18197
18198 purpose = TREE_PURPOSE (t);
18199 if (purpose)
18200 purpose = RECUR (purpose);
18201 value = TREE_VALUE (t);
18202 if (value)
18203 value = RECUR (value);
18204 chain = TREE_CHAIN (t);
18205 if (chain && chain != void_type_node)
18206 chain = RECUR (chain);
18207 if (purpose == TREE_PURPOSE (t)
18208 && value == TREE_VALUE (t)
18209 && chain == TREE_CHAIN (t))
18210 RETURN (t);
18211 RETURN (tree_cons (purpose, value, chain));
18212 }
18213
18214 case COMPONENT_REF:
18215 {
18216 tree object;
18217 tree object_type;
18218 tree member;
18219 tree r;
18220
18221 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18222 args, complain, in_decl);
18223 /* Remember that there was a reference to this entity. */
18224 if (DECL_P (object)
18225 && !mark_used (object, complain) && !(complain & tf_error))
18226 RETURN (error_mark_node);
18227 object_type = TREE_TYPE (object);
18228
18229 member = TREE_OPERAND (t, 1);
18230 if (BASELINK_P (member))
18231 member = tsubst_baselink (member,
18232 non_reference (TREE_TYPE (object)),
18233 args, complain, in_decl);
18234 else
18235 member = tsubst_copy (member, args, complain, in_decl);
18236 if (member == error_mark_node)
18237 RETURN (error_mark_node);
18238
18239 if (TREE_CODE (member) == FIELD_DECL)
18240 {
18241 r = finish_non_static_data_member (member, object, NULL_TREE);
18242 if (TREE_CODE (r) == COMPONENT_REF)
18243 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18244 RETURN (r);
18245 }
18246 else if (type_dependent_expression_p (object))
18247 /* We can't do much here. */;
18248 else if (!CLASS_TYPE_P (object_type))
18249 {
18250 if (scalarish_type_p (object_type))
18251 {
18252 tree s = NULL_TREE;
18253 tree dtor = member;
18254
18255 if (TREE_CODE (dtor) == SCOPE_REF)
18256 {
18257 s = TREE_OPERAND (dtor, 0);
18258 dtor = TREE_OPERAND (dtor, 1);
18259 }
18260 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18261 {
18262 dtor = TREE_OPERAND (dtor, 0);
18263 if (TYPE_P (dtor))
18264 RETURN (finish_pseudo_destructor_expr
18265 (object, s, dtor, input_location));
18266 }
18267 }
18268 }
18269 else if (TREE_CODE (member) == SCOPE_REF
18270 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18271 {
18272 /* Lookup the template functions now that we know what the
18273 scope is. */
18274 tree scope = TREE_OPERAND (member, 0);
18275 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18276 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18277 member = lookup_qualified_name (scope, tmpl,
18278 /*is_type_p=*/false,
18279 /*complain=*/false);
18280 if (BASELINK_P (member))
18281 {
18282 BASELINK_FUNCTIONS (member)
18283 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18284 args);
18285 member = (adjust_result_of_qualified_name_lookup
18286 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18287 object_type));
18288 }
18289 else
18290 {
18291 qualified_name_lookup_error (scope, tmpl, member,
18292 input_location);
18293 RETURN (error_mark_node);
18294 }
18295 }
18296 else if (TREE_CODE (member) == SCOPE_REF
18297 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18298 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18299 {
18300 if (complain & tf_error)
18301 {
18302 if (TYPE_P (TREE_OPERAND (member, 0)))
18303 error ("%qT is not a class or namespace",
18304 TREE_OPERAND (member, 0));
18305 else
18306 error ("%qD is not a class or namespace",
18307 TREE_OPERAND (member, 0));
18308 }
18309 RETURN (error_mark_node);
18310 }
18311
18312 r = finish_class_member_access_expr (object, member,
18313 /*template_p=*/false,
18314 complain);
18315 if (TREE_CODE (r) == COMPONENT_REF)
18316 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18317 RETURN (r);
18318 }
18319
18320 case THROW_EXPR:
18321 RETURN (build_throw
18322 (RECUR (TREE_OPERAND (t, 0))));
18323
18324 case CONSTRUCTOR:
18325 {
18326 vec<constructor_elt, va_gc> *n;
18327 constructor_elt *ce;
18328 unsigned HOST_WIDE_INT idx;
18329 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18330 bool process_index_p;
18331 int newlen;
18332 bool need_copy_p = false;
18333 tree r;
18334
18335 if (type == error_mark_node)
18336 RETURN (error_mark_node);
18337
18338 /* We do not want to process the index of aggregate
18339 initializers as they are identifier nodes which will be
18340 looked up by digest_init. */
18341 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18342
18343 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18344 newlen = vec_safe_length (n);
18345 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18346 {
18347 if (ce->index && process_index_p
18348 /* An identifier index is looked up in the type
18349 being initialized, not the current scope. */
18350 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18351 ce->index = RECUR (ce->index);
18352
18353 if (PACK_EXPANSION_P (ce->value))
18354 {
18355 /* Substitute into the pack expansion. */
18356 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18357 in_decl);
18358
18359 if (ce->value == error_mark_node
18360 || PACK_EXPANSION_P (ce->value))
18361 ;
18362 else if (TREE_VEC_LENGTH (ce->value) == 1)
18363 /* Just move the argument into place. */
18364 ce->value = TREE_VEC_ELT (ce->value, 0);
18365 else
18366 {
18367 /* Update the length of the final CONSTRUCTOR
18368 arguments vector, and note that we will need to
18369 copy.*/
18370 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18371 need_copy_p = true;
18372 }
18373 }
18374 else
18375 ce->value = RECUR (ce->value);
18376 }
18377
18378 if (need_copy_p)
18379 {
18380 vec<constructor_elt, va_gc> *old_n = n;
18381
18382 vec_alloc (n, newlen);
18383 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18384 {
18385 if (TREE_CODE (ce->value) == TREE_VEC)
18386 {
18387 int i, len = TREE_VEC_LENGTH (ce->value);
18388 for (i = 0; i < len; ++i)
18389 CONSTRUCTOR_APPEND_ELT (n, 0,
18390 TREE_VEC_ELT (ce->value, i));
18391 }
18392 else
18393 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18394 }
18395 }
18396
18397 r = build_constructor (init_list_type_node, n);
18398 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18399
18400 if (TREE_HAS_CONSTRUCTOR (t))
18401 {
18402 fcl_t cl = fcl_functional;
18403 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18404 cl = fcl_c99;
18405 RETURN (finish_compound_literal (type, r, complain, cl));
18406 }
18407
18408 TREE_TYPE (r) = type;
18409 RETURN (r);
18410 }
18411
18412 case TYPEID_EXPR:
18413 {
18414 tree operand_0 = TREE_OPERAND (t, 0);
18415 if (TYPE_P (operand_0))
18416 {
18417 operand_0 = tsubst (operand_0, args, complain, in_decl);
18418 RETURN (get_typeid (operand_0, complain));
18419 }
18420 else
18421 {
18422 operand_0 = RECUR (operand_0);
18423 RETURN (build_typeid (operand_0, complain));
18424 }
18425 }
18426
18427 case VAR_DECL:
18428 if (!args)
18429 RETURN (t);
18430 /* Fall through */
18431
18432 case PARM_DECL:
18433 {
18434 tree r = tsubst_copy (t, args, complain, in_decl);
18435 /* ??? We're doing a subset of finish_id_expression here. */
18436 if (VAR_P (r)
18437 && !processing_template_decl
18438 && !cp_unevaluated_operand
18439 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18440 && CP_DECL_THREAD_LOCAL_P (r))
18441 {
18442 if (tree wrap = get_tls_wrapper_fn (r))
18443 /* Replace an evaluated use of the thread_local variable with
18444 a call to its wrapper. */
18445 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18446 }
18447 else if (outer_automatic_var_p (r))
18448 r = process_outer_var_ref (r, complain);
18449
18450 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18451 /* If the original type was a reference, we'll be wrapped in
18452 the appropriate INDIRECT_REF. */
18453 r = convert_from_reference (r);
18454 RETURN (r);
18455 }
18456
18457 case VA_ARG_EXPR:
18458 {
18459 tree op0 = RECUR (TREE_OPERAND (t, 0));
18460 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18461 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18462 }
18463
18464 case OFFSETOF_EXPR:
18465 {
18466 tree object_ptr
18467 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18468 in_decl, /*function_p=*/false,
18469 /*integral_constant_expression_p=*/false);
18470 RETURN (finish_offsetof (object_ptr,
18471 RECUR (TREE_OPERAND (t, 0)),
18472 EXPR_LOCATION (t)));
18473 }
18474
18475 case ADDRESSOF_EXPR:
18476 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18477 RECUR (TREE_OPERAND (t, 0)), complain));
18478
18479 case TRAIT_EXPR:
18480 {
18481 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18482 complain, in_decl);
18483
18484 tree type2 = TRAIT_EXPR_TYPE2 (t);
18485 if (type2 && TREE_CODE (type2) == TREE_LIST)
18486 type2 = RECUR (type2);
18487 else if (type2)
18488 type2 = tsubst (type2, args, complain, in_decl);
18489
18490 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18491 }
18492
18493 case STMT_EXPR:
18494 {
18495 tree old_stmt_expr = cur_stmt_expr;
18496 tree stmt_expr = begin_stmt_expr ();
18497
18498 cur_stmt_expr = stmt_expr;
18499 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18500 integral_constant_expression_p);
18501 stmt_expr = finish_stmt_expr (stmt_expr, false);
18502 cur_stmt_expr = old_stmt_expr;
18503
18504 /* If the resulting list of expression statement is empty,
18505 fold it further into void_node. */
18506 if (empty_expr_stmt_p (stmt_expr))
18507 stmt_expr = void_node;
18508
18509 RETURN (stmt_expr);
18510 }
18511
18512 case LAMBDA_EXPR:
18513 {
18514 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18515
18516 RETURN (build_lambda_object (r));
18517 }
18518
18519 case TARGET_EXPR:
18520 /* We can get here for a constant initializer of non-dependent type.
18521 FIXME stop folding in cp_parser_initializer_clause. */
18522 {
18523 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18524 complain);
18525 RETURN (r);
18526 }
18527
18528 case TRANSACTION_EXPR:
18529 RETURN (tsubst_expr(t, args, complain, in_decl,
18530 integral_constant_expression_p));
18531
18532 case PAREN_EXPR:
18533 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18534
18535 case VEC_PERM_EXPR:
18536 {
18537 tree op0 = RECUR (TREE_OPERAND (t, 0));
18538 tree op1 = RECUR (TREE_OPERAND (t, 1));
18539 tree op2 = RECUR (TREE_OPERAND (t, 2));
18540 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18541 complain));
18542 }
18543
18544 case REQUIRES_EXPR:
18545 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18546
18547 case NON_LVALUE_EXPR:
18548 case VIEW_CONVERT_EXPR:
18549 /* We should only see these for location wrapper nodes, or within
18550 instantiate_non_dependent_expr (when args is NULL_TREE). */
18551 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
18552 if (location_wrapper_p (t))
18553 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
18554 EXPR_LOCATION (t)));
18555 /* fallthrough. */
18556
18557 default:
18558 /* Handle Objective-C++ constructs, if appropriate. */
18559 {
18560 tree subst
18561 = objcp_tsubst_copy_and_build (t, args, complain,
18562 in_decl, /*function_p=*/false);
18563 if (subst)
18564 RETURN (subst);
18565 }
18566 RETURN (tsubst_copy (t, args, complain, in_decl));
18567 }
18568
18569 #undef RECUR
18570 #undef RETURN
18571 out:
18572 input_location = loc;
18573 return retval;
18574 }
18575
18576 /* Verify that the instantiated ARGS are valid. For type arguments,
18577 make sure that the type's linkage is ok. For non-type arguments,
18578 make sure they are constants if they are integral or enumerations.
18579 Emit an error under control of COMPLAIN, and return TRUE on error. */
18580
18581 static bool
18582 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18583 {
18584 if (dependent_template_arg_p (t))
18585 return false;
18586 if (ARGUMENT_PACK_P (t))
18587 {
18588 tree vec = ARGUMENT_PACK_ARGS (t);
18589 int len = TREE_VEC_LENGTH (vec);
18590 bool result = false;
18591 int i;
18592
18593 for (i = 0; i < len; ++i)
18594 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18595 result = true;
18596 return result;
18597 }
18598 else if (TYPE_P (t))
18599 {
18600 /* [basic.link]: A name with no linkage (notably, the name
18601 of a class or enumeration declared in a local scope)
18602 shall not be used to declare an entity with linkage.
18603 This implies that names with no linkage cannot be used as
18604 template arguments
18605
18606 DR 757 relaxes this restriction for C++0x. */
18607 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18608 : no_linkage_check (t, /*relaxed_p=*/false));
18609
18610 if (nt)
18611 {
18612 /* DR 488 makes use of a type with no linkage cause
18613 type deduction to fail. */
18614 if (complain & tf_error)
18615 {
18616 if (TYPE_UNNAMED_P (nt))
18617 error ("%qT is/uses unnamed type", t);
18618 else
18619 error ("template argument for %qD uses local type %qT",
18620 tmpl, t);
18621 }
18622 return true;
18623 }
18624 /* In order to avoid all sorts of complications, we do not
18625 allow variably-modified types as template arguments. */
18626 else if (variably_modified_type_p (t, NULL_TREE))
18627 {
18628 if (complain & tf_error)
18629 error ("%qT is a variably modified type", t);
18630 return true;
18631 }
18632 }
18633 /* Class template and alias template arguments should be OK. */
18634 else if (DECL_TYPE_TEMPLATE_P (t))
18635 ;
18636 /* A non-type argument of integral or enumerated type must be a
18637 constant. */
18638 else if (TREE_TYPE (t)
18639 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18640 && !REFERENCE_REF_P (t)
18641 && !TREE_CONSTANT (t))
18642 {
18643 if (complain & tf_error)
18644 error ("integral expression %qE is not constant", t);
18645 return true;
18646 }
18647 return false;
18648 }
18649
18650 static bool
18651 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18652 {
18653 int ix, len = DECL_NTPARMS (tmpl);
18654 bool result = false;
18655
18656 for (ix = 0; ix != len; ix++)
18657 {
18658 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18659 result = true;
18660 }
18661 if (result && (complain & tf_error))
18662 error (" trying to instantiate %qD", tmpl);
18663 return result;
18664 }
18665
18666 /* We're out of SFINAE context now, so generate diagnostics for the access
18667 errors we saw earlier when instantiating D from TMPL and ARGS. */
18668
18669 static void
18670 recheck_decl_substitution (tree d, tree tmpl, tree args)
18671 {
18672 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18673 tree type = TREE_TYPE (pattern);
18674 location_t loc = input_location;
18675
18676 push_access_scope (d);
18677 push_deferring_access_checks (dk_no_deferred);
18678 input_location = DECL_SOURCE_LOCATION (pattern);
18679 tsubst (type, args, tf_warning_or_error, d);
18680 input_location = loc;
18681 pop_deferring_access_checks ();
18682 pop_access_scope (d);
18683 }
18684
18685 /* Instantiate the indicated variable, function, or alias template TMPL with
18686 the template arguments in TARG_PTR. */
18687
18688 static tree
18689 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18690 {
18691 tree targ_ptr = orig_args;
18692 tree fndecl;
18693 tree gen_tmpl;
18694 tree spec;
18695 bool access_ok = true;
18696
18697 if (tmpl == error_mark_node)
18698 return error_mark_node;
18699
18700 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18701
18702 /* If this function is a clone, handle it specially. */
18703 if (DECL_CLONED_FUNCTION_P (tmpl))
18704 {
18705 tree spec;
18706 tree clone;
18707
18708 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18709 DECL_CLONED_FUNCTION. */
18710 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18711 targ_ptr, complain);
18712 if (spec == error_mark_node)
18713 return error_mark_node;
18714
18715 /* Look for the clone. */
18716 FOR_EACH_CLONE (clone, spec)
18717 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18718 return clone;
18719 /* We should always have found the clone by now. */
18720 gcc_unreachable ();
18721 return NULL_TREE;
18722 }
18723
18724 if (targ_ptr == error_mark_node)
18725 return error_mark_node;
18726
18727 /* Check to see if we already have this specialization. */
18728 gen_tmpl = most_general_template (tmpl);
18729 if (TMPL_ARGS_DEPTH (targ_ptr)
18730 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18731 /* targ_ptr only has the innermost template args, so add the outer ones
18732 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18733 the case of a non-dependent call within a template definition). */
18734 targ_ptr = (add_outermost_template_args
18735 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18736 targ_ptr));
18737
18738 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18739 but it doesn't seem to be on the hot path. */
18740 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18741
18742 gcc_assert (tmpl == gen_tmpl
18743 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18744 == spec)
18745 || fndecl == NULL_TREE);
18746
18747 if (spec != NULL_TREE)
18748 {
18749 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18750 {
18751 if (complain & tf_error)
18752 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18753 return error_mark_node;
18754 }
18755 return spec;
18756 }
18757
18758 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18759 complain))
18760 return error_mark_node;
18761
18762 /* We are building a FUNCTION_DECL, during which the access of its
18763 parameters and return types have to be checked. However this
18764 FUNCTION_DECL which is the desired context for access checking
18765 is not built yet. We solve this chicken-and-egg problem by
18766 deferring all checks until we have the FUNCTION_DECL. */
18767 push_deferring_access_checks (dk_deferred);
18768
18769 /* Instantiation of the function happens in the context of the function
18770 template, not the context of the overload resolution we're doing. */
18771 push_to_top_level ();
18772 /* If there are dependent arguments, e.g. because we're doing partial
18773 ordering, make sure processing_template_decl stays set. */
18774 if (uses_template_parms (targ_ptr))
18775 ++processing_template_decl;
18776 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18777 {
18778 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18779 complain, gen_tmpl, true);
18780 push_nested_class (ctx);
18781 }
18782
18783 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18784
18785 fndecl = NULL_TREE;
18786 if (VAR_P (pattern))
18787 {
18788 /* We need to determine if we're using a partial or explicit
18789 specialization now, because the type of the variable could be
18790 different. */
18791 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18792 tree elt = most_specialized_partial_spec (tid, complain);
18793 if (elt == error_mark_node)
18794 pattern = error_mark_node;
18795 else if (elt)
18796 {
18797 tree partial_tmpl = TREE_VALUE (elt);
18798 tree partial_args = TREE_PURPOSE (elt);
18799 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18800 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18801 }
18802 }
18803
18804 /* Substitute template parameters to obtain the specialization. */
18805 if (fndecl == NULL_TREE)
18806 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18807 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18808 pop_nested_class ();
18809 pop_from_top_level ();
18810
18811 if (fndecl == error_mark_node)
18812 {
18813 pop_deferring_access_checks ();
18814 return error_mark_node;
18815 }
18816
18817 /* The DECL_TI_TEMPLATE should always be the immediate parent
18818 template, not the most general template. */
18819 DECL_TI_TEMPLATE (fndecl) = tmpl;
18820 DECL_TI_ARGS (fndecl) = targ_ptr;
18821
18822 /* Now we know the specialization, compute access previously
18823 deferred. Do no access control for inheriting constructors,
18824 as we already checked access for the inherited constructor. */
18825 if (!(flag_new_inheriting_ctors
18826 && DECL_INHERITED_CTOR (fndecl)))
18827 {
18828 push_access_scope (fndecl);
18829 if (!perform_deferred_access_checks (complain))
18830 access_ok = false;
18831 pop_access_scope (fndecl);
18832 }
18833 pop_deferring_access_checks ();
18834
18835 /* If we've just instantiated the main entry point for a function,
18836 instantiate all the alternate entry points as well. We do this
18837 by cloning the instantiation of the main entry point, not by
18838 instantiating the template clones. */
18839 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18840 clone_function_decl (fndecl, /*update_methods=*/false);
18841
18842 if (!access_ok)
18843 {
18844 if (!(complain & tf_error))
18845 {
18846 /* Remember to reinstantiate when we're out of SFINAE so the user
18847 can see the errors. */
18848 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18849 }
18850 return error_mark_node;
18851 }
18852 return fndecl;
18853 }
18854
18855 /* Wrapper for instantiate_template_1. */
18856
18857 tree
18858 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18859 {
18860 tree ret;
18861 timevar_push (TV_TEMPLATE_INST);
18862 ret = instantiate_template_1 (tmpl, orig_args, complain);
18863 timevar_pop (TV_TEMPLATE_INST);
18864 return ret;
18865 }
18866
18867 /* Instantiate the alias template TMPL with ARGS. Also push a template
18868 instantiation level, which instantiate_template doesn't do because
18869 functions and variables have sufficient context established by the
18870 callers. */
18871
18872 static tree
18873 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18874 {
18875 struct pending_template *old_last_pend = last_pending_template;
18876 struct tinst_level *old_error_tinst = last_error_tinst_level;
18877 if (tmpl == error_mark_node || args == error_mark_node)
18878 return error_mark_node;
18879 tree tinst = build_tree_list (tmpl, args);
18880 if (!push_tinst_level (tinst))
18881 {
18882 ggc_free (tinst);
18883 return error_mark_node;
18884 }
18885
18886 args =
18887 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18888 args, tmpl, complain,
18889 /*require_all_args=*/true,
18890 /*use_default_args=*/true);
18891
18892 tree r = instantiate_template (tmpl, args, complain);
18893 pop_tinst_level ();
18894 /* We can't free this if a pending_template entry or last_error_tinst_level
18895 is pointing at it. */
18896 if (last_pending_template == old_last_pend
18897 && last_error_tinst_level == old_error_tinst)
18898 ggc_free (tinst);
18899
18900 return r;
18901 }
18902
18903 /* PARM is a template parameter pack for FN. Returns true iff
18904 PARM is used in a deducible way in the argument list of FN. */
18905
18906 static bool
18907 pack_deducible_p (tree parm, tree fn)
18908 {
18909 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18910 for (; t; t = TREE_CHAIN (t))
18911 {
18912 tree type = TREE_VALUE (t);
18913 tree packs;
18914 if (!PACK_EXPANSION_P (type))
18915 continue;
18916 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18917 packs; packs = TREE_CHAIN (packs))
18918 if (template_args_equal (TREE_VALUE (packs), parm))
18919 {
18920 /* The template parameter pack is used in a function parameter
18921 pack. If this is the end of the parameter list, the
18922 template parameter pack is deducible. */
18923 if (TREE_CHAIN (t) == void_list_node)
18924 return true;
18925 else
18926 /* Otherwise, not. Well, it could be deduced from
18927 a non-pack parameter, but doing so would end up with
18928 a deduction mismatch, so don't bother. */
18929 return false;
18930 }
18931 }
18932 /* The template parameter pack isn't used in any function parameter
18933 packs, but it might be used deeper, e.g. tuple<Args...>. */
18934 return true;
18935 }
18936
18937 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18938 NARGS elements of the arguments that are being used when calling
18939 it. TARGS is a vector into which the deduced template arguments
18940 are placed.
18941
18942 Returns either a FUNCTION_DECL for the matching specialization of FN or
18943 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18944 true, diagnostics will be printed to explain why it failed.
18945
18946 If FN is a conversion operator, or we are trying to produce a specific
18947 specialization, RETURN_TYPE is the return type desired.
18948
18949 The EXPLICIT_TARGS are explicit template arguments provided via a
18950 template-id.
18951
18952 The parameter STRICT is one of:
18953
18954 DEDUCE_CALL:
18955 We are deducing arguments for a function call, as in
18956 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18957 deducing arguments for a call to the result of a conversion
18958 function template, as in [over.call.object].
18959
18960 DEDUCE_CONV:
18961 We are deducing arguments for a conversion function, as in
18962 [temp.deduct.conv].
18963
18964 DEDUCE_EXACT:
18965 We are deducing arguments when doing an explicit instantiation
18966 as in [temp.explicit], when determining an explicit specialization
18967 as in [temp.expl.spec], or when taking the address of a function
18968 template, as in [temp.deduct.funcaddr]. */
18969
18970 tree
18971 fn_type_unification (tree fn,
18972 tree explicit_targs,
18973 tree targs,
18974 const tree *args,
18975 unsigned int nargs,
18976 tree return_type,
18977 unification_kind_t strict,
18978 int flags,
18979 bool explain_p,
18980 bool decltype_p)
18981 {
18982 tree parms;
18983 tree fntype;
18984 tree decl = NULL_TREE;
18985 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18986 bool ok;
18987 static int deduction_depth;
18988 struct pending_template *old_last_pend = last_pending_template;
18989 struct tinst_level *old_error_tinst = last_error_tinst_level;
18990
18991 tree orig_fn = fn;
18992 if (flag_new_inheriting_ctors)
18993 fn = strip_inheriting_ctors (fn);
18994
18995 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18996 tree tinst;
18997 tree r = error_mark_node;
18998
18999 tree full_targs = targs;
19000 if (TMPL_ARGS_DEPTH (targs)
19001 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
19002 full_targs = (add_outermost_template_args
19003 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
19004 targs));
19005
19006 if (decltype_p)
19007 complain |= tf_decltype;
19008
19009 /* In C++0x, it's possible to have a function template whose type depends
19010 on itself recursively. This is most obvious with decltype, but can also
19011 occur with enumeration scope (c++/48969). So we need to catch infinite
19012 recursion and reject the substitution at deduction time; this function
19013 will return error_mark_node for any repeated substitution.
19014
19015 This also catches excessive recursion such as when f<N> depends on
19016 f<N-1> across all integers, and returns error_mark_node for all the
19017 substitutions back up to the initial one.
19018
19019 This is, of course, not reentrant. */
19020 if (excessive_deduction_depth)
19021 return error_mark_node;
19022 tinst = build_tree_list (fn, NULL_TREE);
19023 ++deduction_depth;
19024
19025 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
19026
19027 fntype = TREE_TYPE (fn);
19028 if (explicit_targs)
19029 {
19030 /* [temp.deduct]
19031
19032 The specified template arguments must match the template
19033 parameters in kind (i.e., type, nontype, template), and there
19034 must not be more arguments than there are parameters;
19035 otherwise type deduction fails.
19036
19037 Nontype arguments must match the types of the corresponding
19038 nontype template parameters, or must be convertible to the
19039 types of the corresponding nontype parameters as specified in
19040 _temp.arg.nontype_, otherwise type deduction fails.
19041
19042 All references in the function type of the function template
19043 to the corresponding template parameters are replaced by the
19044 specified template argument values. If a substitution in a
19045 template parameter or in the function type of the function
19046 template results in an invalid type, type deduction fails. */
19047 int i, len = TREE_VEC_LENGTH (tparms);
19048 location_t loc = input_location;
19049 bool incomplete = false;
19050
19051 if (explicit_targs == error_mark_node)
19052 goto fail;
19053
19054 if (TMPL_ARGS_DEPTH (explicit_targs)
19055 < TMPL_ARGS_DEPTH (full_targs))
19056 explicit_targs = add_outermost_template_args (full_targs,
19057 explicit_targs);
19058
19059 /* Adjust any explicit template arguments before entering the
19060 substitution context. */
19061 explicit_targs
19062 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
19063 complain,
19064 /*require_all_args=*/false,
19065 /*use_default_args=*/false));
19066 if (explicit_targs == error_mark_node)
19067 goto fail;
19068
19069 /* Substitute the explicit args into the function type. This is
19070 necessary so that, for instance, explicitly declared function
19071 arguments can match null pointed constants. If we were given
19072 an incomplete set of explicit args, we must not do semantic
19073 processing during substitution as we could create partial
19074 instantiations. */
19075 for (i = 0; i < len; i++)
19076 {
19077 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
19078 bool parameter_pack = false;
19079 tree targ = TREE_VEC_ELT (explicit_targs, i);
19080
19081 /* Dig out the actual parm. */
19082 if (TREE_CODE (parm) == TYPE_DECL
19083 || TREE_CODE (parm) == TEMPLATE_DECL)
19084 {
19085 parm = TREE_TYPE (parm);
19086 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
19087 }
19088 else if (TREE_CODE (parm) == PARM_DECL)
19089 {
19090 parm = DECL_INITIAL (parm);
19091 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
19092 }
19093
19094 if (!parameter_pack && targ == NULL_TREE)
19095 /* No explicit argument for this template parameter. */
19096 incomplete = true;
19097
19098 if (parameter_pack && pack_deducible_p (parm, fn))
19099 {
19100 /* Mark the argument pack as "incomplete". We could
19101 still deduce more arguments during unification.
19102 We remove this mark in type_unification_real. */
19103 if (targ)
19104 {
19105 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
19106 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
19107 = ARGUMENT_PACK_ARGS (targ);
19108 }
19109
19110 /* We have some incomplete argument packs. */
19111 incomplete = true;
19112 }
19113 }
19114
19115 TREE_VALUE (tinst) = explicit_targs;
19116 if (!push_tinst_level (tinst))
19117 {
19118 excessive_deduction_depth = true;
19119 goto fail;
19120 }
19121 processing_template_decl += incomplete;
19122 input_location = DECL_SOURCE_LOCATION (fn);
19123 /* Ignore any access checks; we'll see them again in
19124 instantiate_template and they might have the wrong
19125 access path at this point. */
19126 push_deferring_access_checks (dk_deferred);
19127 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
19128 complain | tf_partial | tf_fndecl_type, NULL_TREE);
19129 pop_deferring_access_checks ();
19130 input_location = loc;
19131 processing_template_decl -= incomplete;
19132 pop_tinst_level ();
19133
19134 if (fntype == error_mark_node)
19135 goto fail;
19136
19137 /* Place the explicitly specified arguments in TARGS. */
19138 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
19139 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
19140 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
19141 }
19142
19143 /* Never do unification on the 'this' parameter. */
19144 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
19145
19146 if (return_type && strict == DEDUCE_CALL)
19147 {
19148 /* We're deducing for a call to the result of a template conversion
19149 function. The parms we really want are in return_type. */
19150 if (POINTER_TYPE_P (return_type))
19151 return_type = TREE_TYPE (return_type);
19152 parms = TYPE_ARG_TYPES (return_type);
19153 }
19154 else if (return_type)
19155 {
19156 tree *new_args;
19157
19158 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
19159 new_args = XALLOCAVEC (tree, nargs + 1);
19160 new_args[0] = return_type;
19161 memcpy (new_args + 1, args, nargs * sizeof (tree));
19162 args = new_args;
19163 ++nargs;
19164 }
19165
19166 /* We allow incomplete unification without an error message here
19167 because the standard doesn't seem to explicitly prohibit it. Our
19168 callers must be ready to deal with unification failures in any
19169 event. */
19170
19171 TREE_VALUE (tinst) = targs;
19172 /* If we aren't explaining yet, push tinst context so we can see where
19173 any errors (e.g. from class instantiations triggered by instantiation
19174 of default template arguments) come from. If we are explaining, this
19175 context is redundant. */
19176 if (!explain_p && !push_tinst_level (tinst))
19177 {
19178 excessive_deduction_depth = true;
19179 goto fail;
19180 }
19181
19182 /* type_unification_real will pass back any access checks from default
19183 template argument substitution. */
19184 vec<deferred_access_check, va_gc> *checks;
19185 checks = NULL;
19186
19187 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19188 full_targs, parms, args, nargs, /*subr=*/0,
19189 strict, flags, &checks, explain_p);
19190 if (!explain_p)
19191 pop_tinst_level ();
19192 if (!ok)
19193 goto fail;
19194
19195 /* Now that we have bindings for all of the template arguments,
19196 ensure that the arguments deduced for the template template
19197 parameters have compatible template parameter lists. We cannot
19198 check this property before we have deduced all template
19199 arguments, because the template parameter types of a template
19200 template parameter might depend on prior template parameters
19201 deduced after the template template parameter. The following
19202 ill-formed example illustrates this issue:
19203
19204 template<typename T, template<T> class C> void f(C<5>, T);
19205
19206 template<int N> struct X {};
19207
19208 void g() {
19209 f(X<5>(), 5l); // error: template argument deduction fails
19210 }
19211
19212 The template parameter list of 'C' depends on the template type
19213 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19214 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19215 time that we deduce 'C'. */
19216 if (!template_template_parm_bindings_ok_p
19217 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19218 {
19219 unify_inconsistent_template_template_parameters (explain_p);
19220 goto fail;
19221 }
19222
19223 /* All is well so far. Now, check:
19224
19225 [temp.deduct]
19226
19227 When all template arguments have been deduced, all uses of
19228 template parameters in nondeduced contexts are replaced with
19229 the corresponding deduced argument values. If the
19230 substitution results in an invalid type, as described above,
19231 type deduction fails. */
19232 TREE_VALUE (tinst) = targs;
19233 if (!push_tinst_level (tinst))
19234 {
19235 excessive_deduction_depth = true;
19236 goto fail;
19237 }
19238
19239 /* Also collect access checks from the instantiation. */
19240 reopen_deferring_access_checks (checks);
19241
19242 decl = instantiate_template (fn, targs, complain);
19243
19244 checks = get_deferred_access_checks ();
19245 pop_deferring_access_checks ();
19246
19247 pop_tinst_level ();
19248
19249 if (decl == error_mark_node)
19250 goto fail;
19251
19252 /* Now perform any access checks encountered during substitution. */
19253 push_access_scope (decl);
19254 ok = perform_access_checks (checks, complain);
19255 pop_access_scope (decl);
19256 if (!ok)
19257 goto fail;
19258
19259 /* If we're looking for an exact match, check that what we got
19260 is indeed an exact match. It might not be if some template
19261 parameters are used in non-deduced contexts. But don't check
19262 for an exact match if we have dependent template arguments;
19263 in that case we're doing partial ordering, and we already know
19264 that we have two candidates that will provide the actual type. */
19265 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19266 {
19267 tree substed = TREE_TYPE (decl);
19268 unsigned int i;
19269
19270 tree sarg
19271 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19272 if (return_type)
19273 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19274 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19275 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19276 {
19277 unify_type_mismatch (explain_p, args[i],
19278 TREE_VALUE (sarg));
19279 goto fail;
19280 }
19281 }
19282
19283 /* After doing deduction with the inherited constructor, actually return an
19284 instantiation of the inheriting constructor. */
19285 if (orig_fn != fn)
19286 decl = instantiate_template (orig_fn, targs, complain);
19287
19288 r = decl;
19289
19290 fail:
19291 --deduction_depth;
19292 if (excessive_deduction_depth)
19293 {
19294 if (deduction_depth == 0)
19295 /* Reset once we're all the way out. */
19296 excessive_deduction_depth = false;
19297 }
19298
19299 /* We can't free this if a pending_template entry or last_error_tinst_level
19300 is pointing at it. */
19301 if (last_pending_template == old_last_pend
19302 && last_error_tinst_level == old_error_tinst)
19303 ggc_free (tinst);
19304
19305 return r;
19306 }
19307
19308 /* Adjust types before performing type deduction, as described in
19309 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19310 sections are symmetric. PARM is the type of a function parameter
19311 or the return type of the conversion function. ARG is the type of
19312 the argument passed to the call, or the type of the value
19313 initialized with the result of the conversion function.
19314 ARG_EXPR is the original argument expression, which may be null. */
19315
19316 static int
19317 maybe_adjust_types_for_deduction (unification_kind_t strict,
19318 tree* parm,
19319 tree* arg,
19320 tree arg_expr)
19321 {
19322 int result = 0;
19323
19324 switch (strict)
19325 {
19326 case DEDUCE_CALL:
19327 break;
19328
19329 case DEDUCE_CONV:
19330 /* Swap PARM and ARG throughout the remainder of this
19331 function; the handling is precisely symmetric since PARM
19332 will initialize ARG rather than vice versa. */
19333 std::swap (parm, arg);
19334 break;
19335
19336 case DEDUCE_EXACT:
19337 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19338 too, but here handle it by stripping the reference from PARM
19339 rather than by adding it to ARG. */
19340 if (TREE_CODE (*parm) == REFERENCE_TYPE
19341 && TYPE_REF_IS_RVALUE (*parm)
19342 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19343 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19344 && TREE_CODE (*arg) == REFERENCE_TYPE
19345 && !TYPE_REF_IS_RVALUE (*arg))
19346 *parm = TREE_TYPE (*parm);
19347 /* Nothing else to do in this case. */
19348 return 0;
19349
19350 default:
19351 gcc_unreachable ();
19352 }
19353
19354 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19355 {
19356 /* [temp.deduct.call]
19357
19358 If P is not a reference type:
19359
19360 --If A is an array type, the pointer type produced by the
19361 array-to-pointer standard conversion (_conv.array_) is
19362 used in place of A for type deduction; otherwise,
19363
19364 --If A is a function type, the pointer type produced by
19365 the function-to-pointer standard conversion
19366 (_conv.func_) is used in place of A for type deduction;
19367 otherwise,
19368
19369 --If A is a cv-qualified type, the top level
19370 cv-qualifiers of A's type are ignored for type
19371 deduction. */
19372 if (TREE_CODE (*arg) == ARRAY_TYPE)
19373 *arg = build_pointer_type (TREE_TYPE (*arg));
19374 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19375 *arg = build_pointer_type (*arg);
19376 else
19377 *arg = TYPE_MAIN_VARIANT (*arg);
19378 }
19379
19380 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19381 reference to a cv-unqualified template parameter that does not represent a
19382 template parameter of a class template (during class template argument
19383 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19384 an lvalue, the type "lvalue reference to A" is used in place of A for type
19385 deduction. */
19386 if (TREE_CODE (*parm) == REFERENCE_TYPE
19387 && TYPE_REF_IS_RVALUE (*parm)
19388 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19389 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19390 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19391 && (arg_expr ? lvalue_p (arg_expr)
19392 /* try_one_overload doesn't provide an arg_expr, but
19393 functions are always lvalues. */
19394 : TREE_CODE (*arg) == FUNCTION_TYPE))
19395 *arg = build_reference_type (*arg);
19396
19397 /* [temp.deduct.call]
19398
19399 If P is a cv-qualified type, the top level cv-qualifiers
19400 of P's type are ignored for type deduction. If P is a
19401 reference type, the type referred to by P is used for
19402 type deduction. */
19403 *parm = TYPE_MAIN_VARIANT (*parm);
19404 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19405 {
19406 *parm = TREE_TYPE (*parm);
19407 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19408 }
19409
19410 /* DR 322. For conversion deduction, remove a reference type on parm
19411 too (which has been swapped into ARG). */
19412 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19413 *arg = TREE_TYPE (*arg);
19414
19415 return result;
19416 }
19417
19418 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19419 template which does contain any deducible template parameters; check if
19420 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19421 unify_one_argument. */
19422
19423 static int
19424 check_non_deducible_conversion (tree parm, tree arg, int strict,
19425 int flags, bool explain_p)
19426 {
19427 tree type;
19428
19429 if (!TYPE_P (arg))
19430 type = TREE_TYPE (arg);
19431 else
19432 type = arg;
19433
19434 if (same_type_p (parm, type))
19435 return unify_success (explain_p);
19436
19437 if (strict == DEDUCE_CONV)
19438 {
19439 if (can_convert_arg (type, parm, NULL_TREE, flags,
19440 explain_p ? tf_warning_or_error : tf_none))
19441 return unify_success (explain_p);
19442 }
19443 else if (strict != DEDUCE_EXACT)
19444 {
19445 if (can_convert_arg (parm, type,
19446 TYPE_P (arg) ? NULL_TREE : arg,
19447 flags, explain_p ? tf_warning_or_error : tf_none))
19448 return unify_success (explain_p);
19449 }
19450
19451 if (strict == DEDUCE_EXACT)
19452 return unify_type_mismatch (explain_p, parm, arg);
19453 else
19454 return unify_arg_conversion (explain_p, parm, type, arg);
19455 }
19456
19457 static bool uses_deducible_template_parms (tree type);
19458
19459 /* Returns true iff the expression EXPR is one from which a template
19460 argument can be deduced. In other words, if it's an undecorated
19461 use of a template non-type parameter. */
19462
19463 static bool
19464 deducible_expression (tree expr)
19465 {
19466 /* Strip implicit conversions. */
19467 while (CONVERT_EXPR_P (expr))
19468 expr = TREE_OPERAND (expr, 0);
19469 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19470 }
19471
19472 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19473 deducible way; that is, if it has a max value of <PARM> - 1. */
19474
19475 static bool
19476 deducible_array_bound (tree domain)
19477 {
19478 if (domain == NULL_TREE)
19479 return false;
19480
19481 tree max = TYPE_MAX_VALUE (domain);
19482 if (TREE_CODE (max) != MINUS_EXPR)
19483 return false;
19484
19485 return deducible_expression (TREE_OPERAND (max, 0));
19486 }
19487
19488 /* Returns true iff the template arguments ARGS use a template parameter
19489 in a deducible way. */
19490
19491 static bool
19492 deducible_template_args (tree args)
19493 {
19494 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19495 {
19496 bool deducible;
19497 tree elt = TREE_VEC_ELT (args, i);
19498 if (ARGUMENT_PACK_P (elt))
19499 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19500 else
19501 {
19502 if (PACK_EXPANSION_P (elt))
19503 elt = PACK_EXPANSION_PATTERN (elt);
19504 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19505 deducible = true;
19506 else if (TYPE_P (elt))
19507 deducible = uses_deducible_template_parms (elt);
19508 else
19509 deducible = deducible_expression (elt);
19510 }
19511 if (deducible)
19512 return true;
19513 }
19514 return false;
19515 }
19516
19517 /* Returns true iff TYPE contains any deducible references to template
19518 parameters, as per 14.8.2.5. */
19519
19520 static bool
19521 uses_deducible_template_parms (tree type)
19522 {
19523 if (PACK_EXPANSION_P (type))
19524 type = PACK_EXPANSION_PATTERN (type);
19525
19526 /* T
19527 cv-list T
19528 TT<T>
19529 TT<i>
19530 TT<> */
19531 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19532 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19533 return true;
19534
19535 /* T*
19536 T&
19537 T&& */
19538 if (POINTER_TYPE_P (type))
19539 return uses_deducible_template_parms (TREE_TYPE (type));
19540
19541 /* T[integer-constant ]
19542 type [i] */
19543 if (TREE_CODE (type) == ARRAY_TYPE)
19544 return (uses_deducible_template_parms (TREE_TYPE (type))
19545 || deducible_array_bound (TYPE_DOMAIN (type)));
19546
19547 /* T type ::*
19548 type T::*
19549 T T::*
19550 T (type ::*)()
19551 type (T::*)()
19552 type (type ::*)(T)
19553 type (T::*)(T)
19554 T (type ::*)(T)
19555 T (T::*)()
19556 T (T::*)(T) */
19557 if (TYPE_PTRMEM_P (type))
19558 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19559 || (uses_deducible_template_parms
19560 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19561
19562 /* template-name <T> (where template-name refers to a class template)
19563 template-name <i> (where template-name refers to a class template) */
19564 if (CLASS_TYPE_P (type)
19565 && CLASSTYPE_TEMPLATE_INFO (type)
19566 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19567 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19568 (CLASSTYPE_TI_ARGS (type)));
19569
19570 /* type (T)
19571 T()
19572 T(T) */
19573 if (TREE_CODE (type) == FUNCTION_TYPE
19574 || TREE_CODE (type) == METHOD_TYPE)
19575 {
19576 if (uses_deducible_template_parms (TREE_TYPE (type)))
19577 return true;
19578 tree parm = TYPE_ARG_TYPES (type);
19579 if (TREE_CODE (type) == METHOD_TYPE)
19580 parm = TREE_CHAIN (parm);
19581 for (; parm; parm = TREE_CHAIN (parm))
19582 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19583 return true;
19584 }
19585
19586 return false;
19587 }
19588
19589 /* Subroutine of type_unification_real and unify_pack_expansion to
19590 handle unification of a single P/A pair. Parameters are as
19591 for those functions. */
19592
19593 static int
19594 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19595 int subr, unification_kind_t strict,
19596 bool explain_p)
19597 {
19598 tree arg_expr = NULL_TREE;
19599 int arg_strict;
19600
19601 if (arg == error_mark_node || parm == error_mark_node)
19602 return unify_invalid (explain_p);
19603 if (arg == unknown_type_node)
19604 /* We can't deduce anything from this, but we might get all the
19605 template args from other function args. */
19606 return unify_success (explain_p);
19607
19608 /* Implicit conversions (Clause 4) will be performed on a function
19609 argument to convert it to the type of the corresponding function
19610 parameter if the parameter type contains no template-parameters that
19611 participate in template argument deduction. */
19612 if (strict != DEDUCE_EXACT
19613 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19614 /* For function parameters with no deducible template parameters,
19615 just return. We'll check non-dependent conversions later. */
19616 return unify_success (explain_p);
19617
19618 switch (strict)
19619 {
19620 case DEDUCE_CALL:
19621 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19622 | UNIFY_ALLOW_MORE_CV_QUAL
19623 | UNIFY_ALLOW_DERIVED);
19624 break;
19625
19626 case DEDUCE_CONV:
19627 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19628 break;
19629
19630 case DEDUCE_EXACT:
19631 arg_strict = UNIFY_ALLOW_NONE;
19632 break;
19633
19634 default:
19635 gcc_unreachable ();
19636 }
19637
19638 /* We only do these transformations if this is the top-level
19639 parameter_type_list in a call or declaration matching; in other
19640 situations (nested function declarators, template argument lists) we
19641 won't be comparing a type to an expression, and we don't do any type
19642 adjustments. */
19643 if (!subr)
19644 {
19645 if (!TYPE_P (arg))
19646 {
19647 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19648 if (type_unknown_p (arg))
19649 {
19650 /* [temp.deduct.type] A template-argument can be
19651 deduced from a pointer to function or pointer
19652 to member function argument if the set of
19653 overloaded functions does not contain function
19654 templates and at most one of a set of
19655 overloaded functions provides a unique
19656 match. */
19657 resolve_overloaded_unification (tparms, targs, parm,
19658 arg, strict,
19659 arg_strict, explain_p);
19660 /* If a unique match was not found, this is a
19661 non-deduced context, so we still succeed. */
19662 return unify_success (explain_p);
19663 }
19664
19665 arg_expr = arg;
19666 arg = unlowered_expr_type (arg);
19667 if (arg == error_mark_node)
19668 return unify_invalid (explain_p);
19669 }
19670
19671 arg_strict |=
19672 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19673 }
19674 else
19675 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19676 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19677 return unify_template_argument_mismatch (explain_p, parm, arg);
19678
19679 /* For deduction from an init-list we need the actual list. */
19680 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19681 arg = arg_expr;
19682 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19683 }
19684
19685 /* for_each_template_parm callback that always returns 0. */
19686
19687 static int
19688 zero_r (tree, void *)
19689 {
19690 return 0;
19691 }
19692
19693 /* for_each_template_parm any_fn callback to handle deduction of a template
19694 type argument from the type of an array bound. */
19695
19696 static int
19697 array_deduction_r (tree t, void *data)
19698 {
19699 tree_pair_p d = (tree_pair_p)data;
19700 tree &tparms = d->purpose;
19701 tree &targs = d->value;
19702
19703 if (TREE_CODE (t) == ARRAY_TYPE)
19704 if (tree dom = TYPE_DOMAIN (t))
19705 if (tree max = TYPE_MAX_VALUE (dom))
19706 {
19707 if (TREE_CODE (max) == MINUS_EXPR)
19708 max = TREE_OPERAND (max, 0);
19709 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19710 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19711 UNIFY_ALLOW_NONE, /*explain*/false);
19712 }
19713
19714 /* Keep walking. */
19715 return 0;
19716 }
19717
19718 /* Try to deduce any not-yet-deduced template type arguments from the type of
19719 an array bound. This is handled separately from unify because 14.8.2.5 says
19720 "The type of a type parameter is only deduced from an array bound if it is
19721 not otherwise deduced." */
19722
19723 static void
19724 try_array_deduction (tree tparms, tree targs, tree parm)
19725 {
19726 tree_pair_s data = { tparms, targs };
19727 hash_set<tree> visited;
19728 for_each_template_parm (parm, zero_r, &data, &visited,
19729 /*nondeduced*/false, array_deduction_r);
19730 }
19731
19732 /* Most parms like fn_type_unification.
19733
19734 If SUBR is 1, we're being called recursively (to unify the
19735 arguments of a function or method parameter of a function
19736 template).
19737
19738 CHECKS is a pointer to a vector of access checks encountered while
19739 substituting default template arguments. */
19740
19741 static int
19742 type_unification_real (tree tparms,
19743 tree full_targs,
19744 tree xparms,
19745 const tree *xargs,
19746 unsigned int xnargs,
19747 int subr,
19748 unification_kind_t strict,
19749 int flags,
19750 vec<deferred_access_check, va_gc> **checks,
19751 bool explain_p)
19752 {
19753 tree parm, arg;
19754 int i;
19755 int ntparms = TREE_VEC_LENGTH (tparms);
19756 int saw_undeduced = 0;
19757 tree parms;
19758 const tree *args;
19759 unsigned int nargs;
19760 unsigned int ia;
19761
19762 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19763 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19764 gcc_assert (ntparms > 0);
19765
19766 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19767
19768 /* Reset the number of non-defaulted template arguments contained
19769 in TARGS. */
19770 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19771
19772 again:
19773 parms = xparms;
19774 args = xargs;
19775 nargs = xnargs;
19776
19777 ia = 0;
19778 while (parms && parms != void_list_node
19779 && ia < nargs)
19780 {
19781 parm = TREE_VALUE (parms);
19782
19783 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19784 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19785 /* For a function parameter pack that occurs at the end of the
19786 parameter-declaration-list, the type A of each remaining
19787 argument of the call is compared with the type P of the
19788 declarator-id of the function parameter pack. */
19789 break;
19790
19791 parms = TREE_CHAIN (parms);
19792
19793 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19794 /* For a function parameter pack that does not occur at the
19795 end of the parameter-declaration-list, the type of the
19796 parameter pack is a non-deduced context. */
19797 continue;
19798
19799 arg = args[ia];
19800 ++ia;
19801
19802 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19803 explain_p))
19804 return 1;
19805 }
19806
19807 if (parms
19808 && parms != void_list_node
19809 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19810 {
19811 /* Unify the remaining arguments with the pack expansion type. */
19812 tree argvec;
19813 tree parmvec = make_tree_vec (1);
19814
19815 /* Allocate a TREE_VEC and copy in all of the arguments */
19816 argvec = make_tree_vec (nargs - ia);
19817 for (i = 0; ia < nargs; ++ia, ++i)
19818 TREE_VEC_ELT (argvec, i) = args[ia];
19819
19820 /* Copy the parameter into parmvec. */
19821 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19822 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19823 /*subr=*/subr, explain_p))
19824 return 1;
19825
19826 /* Advance to the end of the list of parameters. */
19827 parms = TREE_CHAIN (parms);
19828 }
19829
19830 /* Fail if we've reached the end of the parm list, and more args
19831 are present, and the parm list isn't variadic. */
19832 if (ia < nargs && parms == void_list_node)
19833 return unify_too_many_arguments (explain_p, nargs, ia);
19834 /* Fail if parms are left and they don't have default values and
19835 they aren't all deduced as empty packs (c++/57397). This is
19836 consistent with sufficient_parms_p. */
19837 if (parms && parms != void_list_node
19838 && TREE_PURPOSE (parms) == NULL_TREE)
19839 {
19840 unsigned int count = nargs;
19841 tree p = parms;
19842 bool type_pack_p;
19843 do
19844 {
19845 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19846 if (!type_pack_p)
19847 count++;
19848 p = TREE_CHAIN (p);
19849 }
19850 while (p && p != void_list_node);
19851 if (count != nargs)
19852 return unify_too_few_arguments (explain_p, ia, count,
19853 type_pack_p);
19854 }
19855
19856 if (!subr)
19857 {
19858 tsubst_flags_t complain = (explain_p
19859 ? tf_warning_or_error
19860 : tf_none);
19861 bool tried_array_deduction = (cxx_dialect < cxx17);
19862
19863 for (i = 0; i < ntparms; i++)
19864 {
19865 tree targ = TREE_VEC_ELT (targs, i);
19866 tree tparm = TREE_VEC_ELT (tparms, i);
19867
19868 /* Clear the "incomplete" flags on all argument packs now so that
19869 substituting them into later default arguments works. */
19870 if (targ && ARGUMENT_PACK_P (targ))
19871 {
19872 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19873 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19874 }
19875
19876 if (targ || tparm == error_mark_node)
19877 continue;
19878 tparm = TREE_VALUE (tparm);
19879
19880 if (TREE_CODE (tparm) == TYPE_DECL
19881 && !tried_array_deduction)
19882 {
19883 try_array_deduction (tparms, targs, xparms);
19884 tried_array_deduction = true;
19885 if (TREE_VEC_ELT (targs, i))
19886 continue;
19887 }
19888
19889 /* If this is an undeduced nontype parameter that depends on
19890 a type parameter, try another pass; its type may have been
19891 deduced from a later argument than the one from which
19892 this parameter can be deduced. */
19893 if (TREE_CODE (tparm) == PARM_DECL
19894 && uses_template_parms (TREE_TYPE (tparm))
19895 && saw_undeduced < 2)
19896 {
19897 saw_undeduced = 1;
19898 continue;
19899 }
19900
19901 /* Core issue #226 (C++0x) [temp.deduct]:
19902
19903 If a template argument has not been deduced, its
19904 default template argument, if any, is used.
19905
19906 When we are in C++98 mode, TREE_PURPOSE will either
19907 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19908 to explicitly check cxx_dialect here. */
19909 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19910 /* OK, there is a default argument. Wait until after the
19911 conversion check to do substitution. */
19912 continue;
19913
19914 /* If the type parameter is a parameter pack, then it will
19915 be deduced to an empty parameter pack. */
19916 if (template_parameter_pack_p (tparm))
19917 {
19918 tree arg;
19919
19920 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19921 {
19922 arg = make_node (NONTYPE_ARGUMENT_PACK);
19923 TREE_CONSTANT (arg) = 1;
19924 }
19925 else
19926 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19927
19928 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19929
19930 TREE_VEC_ELT (targs, i) = arg;
19931 continue;
19932 }
19933
19934 return unify_parameter_deduction_failure (explain_p, tparm);
19935 }
19936
19937 /* DR 1391: All parameters have args, now check non-dependent parms for
19938 convertibility. */
19939 if (saw_undeduced < 2)
19940 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19941 parms && parms != void_list_node && ia < nargs; )
19942 {
19943 parm = TREE_VALUE (parms);
19944
19945 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19946 && (!TREE_CHAIN (parms)
19947 || TREE_CHAIN (parms) == void_list_node))
19948 /* For a function parameter pack that occurs at the end of the
19949 parameter-declaration-list, the type A of each remaining
19950 argument of the call is compared with the type P of the
19951 declarator-id of the function parameter pack. */
19952 break;
19953
19954 parms = TREE_CHAIN (parms);
19955
19956 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19957 /* For a function parameter pack that does not occur at the
19958 end of the parameter-declaration-list, the type of the
19959 parameter pack is a non-deduced context. */
19960 continue;
19961
19962 arg = args[ia];
19963 ++ia;
19964
19965 if (uses_template_parms (parm))
19966 continue;
19967 if (check_non_deducible_conversion (parm, arg, strict, flags,
19968 explain_p))
19969 return 1;
19970 }
19971
19972 /* Now substitute into the default template arguments. */
19973 for (i = 0; i < ntparms; i++)
19974 {
19975 tree targ = TREE_VEC_ELT (targs, i);
19976 tree tparm = TREE_VEC_ELT (tparms, i);
19977
19978 if (targ || tparm == error_mark_node)
19979 continue;
19980 tree parm = TREE_VALUE (tparm);
19981
19982 tsubst_flags_t fcomplain = complain;
19983 if (saw_undeduced == 1)
19984 {
19985 /* When saw_undeduced == 1, substitution into parm and arg might
19986 fail or not replace all template parameters, and that's
19987 fine. */
19988 fcomplain = tf_none;
19989 if (TREE_CODE (parm) == PARM_DECL
19990 && uses_template_parms (TREE_TYPE (parm)))
19991 continue;
19992 }
19993
19994 tree arg = TREE_PURPOSE (tparm);
19995 reopen_deferring_access_checks (*checks);
19996 location_t save_loc = input_location;
19997 if (DECL_P (parm))
19998 input_location = DECL_SOURCE_LOCATION (parm);
19999
20000 if (saw_undeduced == 1)
20001 ++processing_template_decl;
20002 arg = tsubst_template_arg (arg, full_targs, fcomplain, NULL_TREE);
20003 if (saw_undeduced == 1)
20004 --processing_template_decl;
20005
20006 if (arg != error_mark_node && !uses_template_parms (arg))
20007 arg = convert_template_argument (parm, arg, full_targs, complain,
20008 i, NULL_TREE);
20009 else if (saw_undeduced == 1)
20010 arg = NULL_TREE;
20011 else
20012 arg = error_mark_node;
20013 input_location = save_loc;
20014 *checks = get_deferred_access_checks ();
20015 pop_deferring_access_checks ();
20016 if (arg == error_mark_node)
20017 return 1;
20018 else if (arg)
20019 {
20020 TREE_VEC_ELT (targs, i) = arg;
20021 /* The position of the first default template argument,
20022 is also the number of non-defaulted arguments in TARGS.
20023 Record that. */
20024 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20025 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
20026 }
20027 }
20028
20029 if (saw_undeduced++ == 1)
20030 goto again;
20031 }
20032
20033 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20034 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
20035
20036 return unify_success (explain_p);
20037 }
20038
20039 /* Subroutine of type_unification_real. Args are like the variables
20040 at the call site. ARG is an overloaded function (or template-id);
20041 we try deducing template args from each of the overloads, and if
20042 only one succeeds, we go with that. Modifies TARGS and returns
20043 true on success. */
20044
20045 static bool
20046 resolve_overloaded_unification (tree tparms,
20047 tree targs,
20048 tree parm,
20049 tree arg,
20050 unification_kind_t strict,
20051 int sub_strict,
20052 bool explain_p)
20053 {
20054 tree tempargs = copy_node (targs);
20055 int good = 0;
20056 tree goodfn = NULL_TREE;
20057 bool addr_p;
20058
20059 if (TREE_CODE (arg) == ADDR_EXPR)
20060 {
20061 arg = TREE_OPERAND (arg, 0);
20062 addr_p = true;
20063 }
20064 else
20065 addr_p = false;
20066
20067 if (TREE_CODE (arg) == COMPONENT_REF)
20068 /* Handle `&x' where `x' is some static or non-static member
20069 function name. */
20070 arg = TREE_OPERAND (arg, 1);
20071
20072 if (TREE_CODE (arg) == OFFSET_REF)
20073 arg = TREE_OPERAND (arg, 1);
20074
20075 /* Strip baselink information. */
20076 if (BASELINK_P (arg))
20077 arg = BASELINK_FUNCTIONS (arg);
20078
20079 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
20080 {
20081 /* If we got some explicit template args, we need to plug them into
20082 the affected templates before we try to unify, in case the
20083 explicit args will completely resolve the templates in question. */
20084
20085 int ok = 0;
20086 tree expl_subargs = TREE_OPERAND (arg, 1);
20087 arg = TREE_OPERAND (arg, 0);
20088
20089 for (lkp_iterator iter (arg); iter; ++iter)
20090 {
20091 tree fn = *iter;
20092 tree subargs, elem;
20093
20094 if (TREE_CODE (fn) != TEMPLATE_DECL)
20095 continue;
20096
20097 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20098 expl_subargs, NULL_TREE, tf_none,
20099 /*require_all_args=*/true,
20100 /*use_default_args=*/true);
20101 if (subargs != error_mark_node
20102 && !any_dependent_template_arguments_p (subargs))
20103 {
20104 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
20105 if (try_one_overload (tparms, targs, tempargs, parm,
20106 elem, strict, sub_strict, addr_p, explain_p)
20107 && (!goodfn || !same_type_p (goodfn, elem)))
20108 {
20109 goodfn = elem;
20110 ++good;
20111 }
20112 }
20113 else if (subargs)
20114 ++ok;
20115 }
20116 /* If no templates (or more than one) are fully resolved by the
20117 explicit arguments, this template-id is a non-deduced context; it
20118 could still be OK if we deduce all template arguments for the
20119 enclosing call through other arguments. */
20120 if (good != 1)
20121 good = ok;
20122 }
20123 else if (TREE_CODE (arg) != OVERLOAD
20124 && TREE_CODE (arg) != FUNCTION_DECL)
20125 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20126 -- but the deduction does not succeed because the expression is
20127 not just the function on its own. */
20128 return false;
20129 else
20130 for (lkp_iterator iter (arg); iter; ++iter)
20131 {
20132 tree fn = *iter;
20133 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
20134 strict, sub_strict, addr_p, explain_p)
20135 && (!goodfn || !decls_match (goodfn, fn)))
20136 {
20137 goodfn = fn;
20138 ++good;
20139 }
20140 }
20141
20142 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20143 to function or pointer to member function argument if the set of
20144 overloaded functions does not contain function templates and at most
20145 one of a set of overloaded functions provides a unique match.
20146
20147 So if we found multiple possibilities, we return success but don't
20148 deduce anything. */
20149
20150 if (good == 1)
20151 {
20152 int i = TREE_VEC_LENGTH (targs);
20153 for (; i--; )
20154 if (TREE_VEC_ELT (tempargs, i))
20155 {
20156 tree old = TREE_VEC_ELT (targs, i);
20157 tree new_ = TREE_VEC_ELT (tempargs, i);
20158 if (new_ && old && ARGUMENT_PACK_P (old)
20159 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
20160 /* Don't forget explicit template arguments in a pack. */
20161 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
20162 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
20163 TREE_VEC_ELT (targs, i) = new_;
20164 }
20165 }
20166 if (good)
20167 return true;
20168
20169 return false;
20170 }
20171
20172 /* Core DR 115: In contexts where deduction is done and fails, or in
20173 contexts where deduction is not done, if a template argument list is
20174 specified and it, along with any default template arguments, identifies
20175 a single function template specialization, then the template-id is an
20176 lvalue for the function template specialization. */
20177
20178 tree
20179 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
20180 {
20181 tree expr, offset, baselink;
20182 bool addr;
20183
20184 if (!type_unknown_p (orig_expr))
20185 return orig_expr;
20186
20187 expr = orig_expr;
20188 addr = false;
20189 offset = NULL_TREE;
20190 baselink = NULL_TREE;
20191
20192 if (TREE_CODE (expr) == ADDR_EXPR)
20193 {
20194 expr = TREE_OPERAND (expr, 0);
20195 addr = true;
20196 }
20197 if (TREE_CODE (expr) == OFFSET_REF)
20198 {
20199 offset = expr;
20200 expr = TREE_OPERAND (expr, 1);
20201 }
20202 if (BASELINK_P (expr))
20203 {
20204 baselink = expr;
20205 expr = BASELINK_FUNCTIONS (expr);
20206 }
20207
20208 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
20209 {
20210 int good = 0;
20211 tree goodfn = NULL_TREE;
20212
20213 /* If we got some explicit template args, we need to plug them into
20214 the affected templates before we try to unify, in case the
20215 explicit args will completely resolve the templates in question. */
20216
20217 tree expl_subargs = TREE_OPERAND (expr, 1);
20218 tree arg = TREE_OPERAND (expr, 0);
20219 tree badfn = NULL_TREE;
20220 tree badargs = NULL_TREE;
20221
20222 for (lkp_iterator iter (arg); iter; ++iter)
20223 {
20224 tree fn = *iter;
20225 tree subargs, elem;
20226
20227 if (TREE_CODE (fn) != TEMPLATE_DECL)
20228 continue;
20229
20230 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20231 expl_subargs, NULL_TREE, tf_none,
20232 /*require_all_args=*/true,
20233 /*use_default_args=*/true);
20234 if (subargs != error_mark_node
20235 && !any_dependent_template_arguments_p (subargs))
20236 {
20237 elem = instantiate_template (fn, subargs, tf_none);
20238 if (elem == error_mark_node)
20239 {
20240 badfn = fn;
20241 badargs = subargs;
20242 }
20243 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20244 {
20245 goodfn = elem;
20246 ++good;
20247 }
20248 }
20249 }
20250 if (good == 1)
20251 {
20252 mark_used (goodfn);
20253 expr = goodfn;
20254 if (baselink)
20255 expr = build_baselink (BASELINK_BINFO (baselink),
20256 BASELINK_ACCESS_BINFO (baselink),
20257 expr, BASELINK_OPTYPE (baselink));
20258 if (offset)
20259 {
20260 tree base
20261 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20262 expr = build_offset_ref (base, expr, addr, complain);
20263 }
20264 if (addr)
20265 expr = cp_build_addr_expr (expr, complain);
20266 return expr;
20267 }
20268 else if (good == 0 && badargs && (complain & tf_error))
20269 /* There were no good options and at least one bad one, so let the
20270 user know what the problem is. */
20271 instantiate_template (badfn, badargs, complain);
20272 }
20273 return orig_expr;
20274 }
20275
20276 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20277 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20278 different overloads deduce different arguments for a given parm.
20279 ADDR_P is true if the expression for which deduction is being
20280 performed was of the form "& fn" rather than simply "fn".
20281
20282 Returns 1 on success. */
20283
20284 static int
20285 try_one_overload (tree tparms,
20286 tree orig_targs,
20287 tree targs,
20288 tree parm,
20289 tree arg,
20290 unification_kind_t strict,
20291 int sub_strict,
20292 bool addr_p,
20293 bool explain_p)
20294 {
20295 int nargs;
20296 tree tempargs;
20297 int i;
20298
20299 if (arg == error_mark_node)
20300 return 0;
20301
20302 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20303 to function or pointer to member function argument if the set of
20304 overloaded functions does not contain function templates and at most
20305 one of a set of overloaded functions provides a unique match.
20306
20307 So if this is a template, just return success. */
20308
20309 if (uses_template_parms (arg))
20310 return 1;
20311
20312 if (TREE_CODE (arg) == METHOD_TYPE)
20313 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20314 else if (addr_p)
20315 arg = build_pointer_type (arg);
20316
20317 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20318
20319 /* We don't copy orig_targs for this because if we have already deduced
20320 some template args from previous args, unify would complain when we
20321 try to deduce a template parameter for the same argument, even though
20322 there isn't really a conflict. */
20323 nargs = TREE_VEC_LENGTH (targs);
20324 tempargs = make_tree_vec (nargs);
20325
20326 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20327 return 0;
20328
20329 /* First make sure we didn't deduce anything that conflicts with
20330 explicitly specified args. */
20331 for (i = nargs; i--; )
20332 {
20333 tree elt = TREE_VEC_ELT (tempargs, i);
20334 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20335
20336 if (!elt)
20337 /*NOP*/;
20338 else if (uses_template_parms (elt))
20339 /* Since we're unifying against ourselves, we will fill in
20340 template args used in the function parm list with our own
20341 template parms. Discard them. */
20342 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20343 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20344 {
20345 /* Check that the argument at each index of the deduced argument pack
20346 is equivalent to the corresponding explicitly specified argument.
20347 We may have deduced more arguments than were explicitly specified,
20348 and that's OK. */
20349
20350 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20351 that's wrong if we deduce the same argument pack from multiple
20352 function arguments: it's only incomplete the first time. */
20353
20354 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20355 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20356
20357 if (TREE_VEC_LENGTH (deduced_pack)
20358 < TREE_VEC_LENGTH (explicit_pack))
20359 return 0;
20360
20361 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20362 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20363 TREE_VEC_ELT (deduced_pack, j)))
20364 return 0;
20365 }
20366 else if (oldelt && !template_args_equal (oldelt, elt))
20367 return 0;
20368 }
20369
20370 for (i = nargs; i--; )
20371 {
20372 tree elt = TREE_VEC_ELT (tempargs, i);
20373
20374 if (elt)
20375 TREE_VEC_ELT (targs, i) = elt;
20376 }
20377
20378 return 1;
20379 }
20380
20381 /* PARM is a template class (perhaps with unbound template
20382 parameters). ARG is a fully instantiated type. If ARG can be
20383 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20384 TARGS are as for unify. */
20385
20386 static tree
20387 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20388 bool explain_p)
20389 {
20390 tree copy_of_targs;
20391
20392 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20393 return NULL_TREE;
20394 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20395 /* Matches anything. */;
20396 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20397 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20398 return NULL_TREE;
20399
20400 /* We need to make a new template argument vector for the call to
20401 unify. If we used TARGS, we'd clutter it up with the result of
20402 the attempted unification, even if this class didn't work out.
20403 We also don't want to commit ourselves to all the unifications
20404 we've already done, since unification is supposed to be done on
20405 an argument-by-argument basis. In other words, consider the
20406 following pathological case:
20407
20408 template <int I, int J, int K>
20409 struct S {};
20410
20411 template <int I, int J>
20412 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20413
20414 template <int I, int J, int K>
20415 void f(S<I, J, K>, S<I, I, I>);
20416
20417 void g() {
20418 S<0, 0, 0> s0;
20419 S<0, 1, 2> s2;
20420
20421 f(s0, s2);
20422 }
20423
20424 Now, by the time we consider the unification involving `s2', we
20425 already know that we must have `f<0, 0, 0>'. But, even though
20426 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20427 because there are two ways to unify base classes of S<0, 1, 2>
20428 with S<I, I, I>. If we kept the already deduced knowledge, we
20429 would reject the possibility I=1. */
20430 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20431
20432 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20433 {
20434 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20435 return NULL_TREE;
20436 return arg;
20437 }
20438
20439 /* If unification failed, we're done. */
20440 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20441 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20442 return NULL_TREE;
20443
20444 return arg;
20445 }
20446
20447 /* Given a template type PARM and a class type ARG, find the unique
20448 base type in ARG that is an instance of PARM. We do not examine
20449 ARG itself; only its base-classes. If there is not exactly one
20450 appropriate base class, return NULL_TREE. PARM may be the type of
20451 a partial specialization, as well as a plain template type. Used
20452 by unify. */
20453
20454 static enum template_base_result
20455 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20456 bool explain_p, tree *result)
20457 {
20458 tree rval = NULL_TREE;
20459 tree binfo;
20460
20461 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20462
20463 binfo = TYPE_BINFO (complete_type (arg));
20464 if (!binfo)
20465 {
20466 /* The type could not be completed. */
20467 *result = NULL_TREE;
20468 return tbr_incomplete_type;
20469 }
20470
20471 /* Walk in inheritance graph order. The search order is not
20472 important, and this avoids multiple walks of virtual bases. */
20473 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20474 {
20475 tree r = try_class_unification (tparms, targs, parm,
20476 BINFO_TYPE (binfo), explain_p);
20477
20478 if (r)
20479 {
20480 /* If there is more than one satisfactory baseclass, then:
20481
20482 [temp.deduct.call]
20483
20484 If they yield more than one possible deduced A, the type
20485 deduction fails.
20486
20487 applies. */
20488 if (rval && !same_type_p (r, rval))
20489 {
20490 *result = NULL_TREE;
20491 return tbr_ambiguous_baseclass;
20492 }
20493
20494 rval = r;
20495 }
20496 }
20497
20498 *result = rval;
20499 return tbr_success;
20500 }
20501
20502 /* Returns the level of DECL, which declares a template parameter. */
20503
20504 static int
20505 template_decl_level (tree decl)
20506 {
20507 switch (TREE_CODE (decl))
20508 {
20509 case TYPE_DECL:
20510 case TEMPLATE_DECL:
20511 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20512
20513 case PARM_DECL:
20514 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20515
20516 default:
20517 gcc_unreachable ();
20518 }
20519 return 0;
20520 }
20521
20522 /* Decide whether ARG can be unified with PARM, considering only the
20523 cv-qualifiers of each type, given STRICT as documented for unify.
20524 Returns nonzero iff the unification is OK on that basis. */
20525
20526 static int
20527 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20528 {
20529 int arg_quals = cp_type_quals (arg);
20530 int parm_quals = cp_type_quals (parm);
20531
20532 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20533 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20534 {
20535 /* Although a CVR qualifier is ignored when being applied to a
20536 substituted template parameter ([8.3.2]/1 for example), that
20537 does not allow us to unify "const T" with "int&" because both
20538 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20539 It is ok when we're allowing additional CV qualifiers
20540 at the outer level [14.8.2.1]/3,1st bullet. */
20541 if ((TREE_CODE (arg) == REFERENCE_TYPE
20542 || TREE_CODE (arg) == FUNCTION_TYPE
20543 || TREE_CODE (arg) == METHOD_TYPE)
20544 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20545 return 0;
20546
20547 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20548 && (parm_quals & TYPE_QUAL_RESTRICT))
20549 return 0;
20550 }
20551
20552 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20553 && (arg_quals & parm_quals) != parm_quals)
20554 return 0;
20555
20556 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20557 && (parm_quals & arg_quals) != arg_quals)
20558 return 0;
20559
20560 return 1;
20561 }
20562
20563 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20564 void
20565 template_parm_level_and_index (tree parm, int* level, int* index)
20566 {
20567 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20568 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20569 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20570 {
20571 *index = TEMPLATE_TYPE_IDX (parm);
20572 *level = TEMPLATE_TYPE_LEVEL (parm);
20573 }
20574 else
20575 {
20576 *index = TEMPLATE_PARM_IDX (parm);
20577 *level = TEMPLATE_PARM_LEVEL (parm);
20578 }
20579 }
20580
20581 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20582 do { \
20583 if (unify (TP, TA, P, A, S, EP)) \
20584 return 1; \
20585 } while (0)
20586
20587 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20588 expansion at the end of PACKED_PARMS. Returns 0 if the type
20589 deduction succeeds, 1 otherwise. STRICT is the same as in
20590 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20591 function call argument list. We'll need to adjust the arguments to make them
20592 types. SUBR tells us if this is from a recursive call to
20593 type_unification_real, or for comparing two template argument
20594 lists. */
20595
20596 static int
20597 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20598 tree packed_args, unification_kind_t strict,
20599 bool subr, bool explain_p)
20600 {
20601 tree parm
20602 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20603 tree pattern = PACK_EXPANSION_PATTERN (parm);
20604 tree pack, packs = NULL_TREE;
20605 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20606
20607 /* Add in any args remembered from an earlier partial instantiation. */
20608 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20609 int levels = TMPL_ARGS_DEPTH (targs);
20610
20611 packed_args = expand_template_argument_pack (packed_args);
20612
20613 int len = TREE_VEC_LENGTH (packed_args);
20614
20615 /* Determine the parameter packs we will be deducing from the
20616 pattern, and record their current deductions. */
20617 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20618 pack; pack = TREE_CHAIN (pack))
20619 {
20620 tree parm_pack = TREE_VALUE (pack);
20621 int idx, level;
20622
20623 /* Determine the index and level of this parameter pack. */
20624 template_parm_level_and_index (parm_pack, &level, &idx);
20625 if (level < levels)
20626 continue;
20627
20628 /* Keep track of the parameter packs and their corresponding
20629 argument packs. */
20630 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20631 TREE_TYPE (packs) = make_tree_vec (len - start);
20632 }
20633
20634 /* Loop through all of the arguments that have not yet been
20635 unified and unify each with the pattern. */
20636 for (i = start; i < len; i++)
20637 {
20638 tree parm;
20639 bool any_explicit = false;
20640 tree arg = TREE_VEC_ELT (packed_args, i);
20641
20642 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20643 or the element of its argument pack at the current index if
20644 this argument was explicitly specified. */
20645 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20646 {
20647 int idx, level;
20648 tree arg, pargs;
20649 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20650
20651 arg = NULL_TREE;
20652 if (TREE_VALUE (pack)
20653 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20654 && (i - start < TREE_VEC_LENGTH (pargs)))
20655 {
20656 any_explicit = true;
20657 arg = TREE_VEC_ELT (pargs, i - start);
20658 }
20659 TMPL_ARG (targs, level, idx) = arg;
20660 }
20661
20662 /* If we had explicit template arguments, substitute them into the
20663 pattern before deduction. */
20664 if (any_explicit)
20665 {
20666 /* Some arguments might still be unspecified or dependent. */
20667 bool dependent;
20668 ++processing_template_decl;
20669 dependent = any_dependent_template_arguments_p (targs);
20670 if (!dependent)
20671 --processing_template_decl;
20672 parm = tsubst (pattern, targs,
20673 explain_p ? tf_warning_or_error : tf_none,
20674 NULL_TREE);
20675 if (dependent)
20676 --processing_template_decl;
20677 if (parm == error_mark_node)
20678 return 1;
20679 }
20680 else
20681 parm = pattern;
20682
20683 /* Unify the pattern with the current argument. */
20684 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20685 explain_p))
20686 return 1;
20687
20688 /* For each parameter pack, collect the deduced value. */
20689 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20690 {
20691 int idx, level;
20692 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20693
20694 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20695 TMPL_ARG (targs, level, idx);
20696 }
20697 }
20698
20699 /* Verify that the results of unification with the parameter packs
20700 produce results consistent with what we've seen before, and make
20701 the deduced argument packs available. */
20702 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20703 {
20704 tree old_pack = TREE_VALUE (pack);
20705 tree new_args = TREE_TYPE (pack);
20706 int i, len = TREE_VEC_LENGTH (new_args);
20707 int idx, level;
20708 bool nondeduced_p = false;
20709
20710 /* By default keep the original deduced argument pack.
20711 If necessary, more specific code is going to update the
20712 resulting deduced argument later down in this function. */
20713 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20714 TMPL_ARG (targs, level, idx) = old_pack;
20715
20716 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20717 actually deduce anything. */
20718 for (i = 0; i < len && !nondeduced_p; ++i)
20719 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20720 nondeduced_p = true;
20721 if (nondeduced_p)
20722 continue;
20723
20724 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20725 {
20726 /* If we had fewer function args than explicit template args,
20727 just use the explicits. */
20728 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20729 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20730 if (len < explicit_len)
20731 new_args = explicit_args;
20732 }
20733
20734 if (!old_pack)
20735 {
20736 tree result;
20737 /* Build the deduced *_ARGUMENT_PACK. */
20738 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20739 {
20740 result = make_node (NONTYPE_ARGUMENT_PACK);
20741 TREE_CONSTANT (result) = 1;
20742 }
20743 else
20744 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20745
20746 SET_ARGUMENT_PACK_ARGS (result, new_args);
20747
20748 /* Note the deduced argument packs for this parameter
20749 pack. */
20750 TMPL_ARG (targs, level, idx) = result;
20751 }
20752 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20753 && (ARGUMENT_PACK_ARGS (old_pack)
20754 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20755 {
20756 /* We only had the explicitly-provided arguments before, but
20757 now we have a complete set of arguments. */
20758 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20759
20760 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20761 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20762 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20763 }
20764 else
20765 {
20766 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20767 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20768
20769 if (!comp_template_args (old_args, new_args,
20770 &bad_old_arg, &bad_new_arg))
20771 /* Inconsistent unification of this parameter pack. */
20772 return unify_parameter_pack_inconsistent (explain_p,
20773 bad_old_arg,
20774 bad_new_arg);
20775 }
20776 }
20777
20778 return unify_success (explain_p);
20779 }
20780
20781 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20782 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20783 parameters and return value are as for unify. */
20784
20785 static int
20786 unify_array_domain (tree tparms, tree targs,
20787 tree parm_dom, tree arg_dom,
20788 bool explain_p)
20789 {
20790 tree parm_max;
20791 tree arg_max;
20792 bool parm_cst;
20793 bool arg_cst;
20794
20795 /* Our representation of array types uses "N - 1" as the
20796 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20797 not an integer constant. We cannot unify arbitrarily
20798 complex expressions, so we eliminate the MINUS_EXPRs
20799 here. */
20800 parm_max = TYPE_MAX_VALUE (parm_dom);
20801 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20802 if (!parm_cst)
20803 {
20804 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20805 parm_max = TREE_OPERAND (parm_max, 0);
20806 }
20807 arg_max = TYPE_MAX_VALUE (arg_dom);
20808 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20809 if (!arg_cst)
20810 {
20811 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20812 trying to unify the type of a variable with the type
20813 of a template parameter. For example:
20814
20815 template <unsigned int N>
20816 void f (char (&) [N]);
20817 int g();
20818 void h(int i) {
20819 char a[g(i)];
20820 f(a);
20821 }
20822
20823 Here, the type of the ARG will be "int [g(i)]", and
20824 may be a SAVE_EXPR, etc. */
20825 if (TREE_CODE (arg_max) != MINUS_EXPR)
20826 return unify_vla_arg (explain_p, arg_dom);
20827 arg_max = TREE_OPERAND (arg_max, 0);
20828 }
20829
20830 /* If only one of the bounds used a MINUS_EXPR, compensate
20831 by adding one to the other bound. */
20832 if (parm_cst && !arg_cst)
20833 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20834 integer_type_node,
20835 parm_max,
20836 integer_one_node);
20837 else if (arg_cst && !parm_cst)
20838 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20839 integer_type_node,
20840 arg_max,
20841 integer_one_node);
20842
20843 return unify (tparms, targs, parm_max, arg_max,
20844 UNIFY_ALLOW_INTEGER, explain_p);
20845 }
20846
20847 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20848
20849 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20850
20851 static pa_kind_t
20852 pa_kind (tree t)
20853 {
20854 if (PACK_EXPANSION_P (t))
20855 t = PACK_EXPANSION_PATTERN (t);
20856 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20857 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20858 || DECL_TYPE_TEMPLATE_P (t))
20859 return pa_tmpl;
20860 else if (TYPE_P (t))
20861 return pa_type;
20862 else
20863 return pa_expr;
20864 }
20865
20866 /* Deduce the value of template parameters. TPARMS is the (innermost)
20867 set of template parameters to a template. TARGS is the bindings
20868 for those template parameters, as determined thus far; TARGS may
20869 include template arguments for outer levels of template parameters
20870 as well. PARM is a parameter to a template function, or a
20871 subcomponent of that parameter; ARG is the corresponding argument.
20872 This function attempts to match PARM with ARG in a manner
20873 consistent with the existing assignments in TARGS. If more values
20874 are deduced, then TARGS is updated.
20875
20876 Returns 0 if the type deduction succeeds, 1 otherwise. The
20877 parameter STRICT is a bitwise or of the following flags:
20878
20879 UNIFY_ALLOW_NONE:
20880 Require an exact match between PARM and ARG.
20881 UNIFY_ALLOW_MORE_CV_QUAL:
20882 Allow the deduced ARG to be more cv-qualified (by qualification
20883 conversion) than ARG.
20884 UNIFY_ALLOW_LESS_CV_QUAL:
20885 Allow the deduced ARG to be less cv-qualified than ARG.
20886 UNIFY_ALLOW_DERIVED:
20887 Allow the deduced ARG to be a template base class of ARG,
20888 or a pointer to a template base class of the type pointed to by
20889 ARG.
20890 UNIFY_ALLOW_INTEGER:
20891 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20892 case for more information.
20893 UNIFY_ALLOW_OUTER_LEVEL:
20894 This is the outermost level of a deduction. Used to determine validity
20895 of qualification conversions. A valid qualification conversion must
20896 have const qualified pointers leading up to the inner type which
20897 requires additional CV quals, except at the outer level, where const
20898 is not required [conv.qual]. It would be normal to set this flag in
20899 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20900 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20901 This is the outermost level of a deduction, and PARM can be more CV
20902 qualified at this point.
20903 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20904 This is the outermost level of a deduction, and PARM can be less CV
20905 qualified at this point. */
20906
20907 static int
20908 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20909 bool explain_p)
20910 {
20911 int idx;
20912 tree targ;
20913 tree tparm;
20914 int strict_in = strict;
20915 tsubst_flags_t complain = (explain_p
20916 ? tf_warning_or_error
20917 : tf_none);
20918
20919 /* I don't think this will do the right thing with respect to types.
20920 But the only case I've seen it in so far has been array bounds, where
20921 signedness is the only information lost, and I think that will be
20922 okay. */
20923 while (CONVERT_EXPR_P (parm))
20924 parm = TREE_OPERAND (parm, 0);
20925
20926 if (arg == error_mark_node)
20927 return unify_invalid (explain_p);
20928 if (arg == unknown_type_node
20929 || arg == init_list_type_node)
20930 /* We can't deduce anything from this, but we might get all the
20931 template args from other function args. */
20932 return unify_success (explain_p);
20933
20934 if (parm == any_targ_node || arg == any_targ_node)
20935 return unify_success (explain_p);
20936
20937 /* If PARM uses template parameters, then we can't bail out here,
20938 even if ARG == PARM, since we won't record unifications for the
20939 template parameters. We might need them if we're trying to
20940 figure out which of two things is more specialized. */
20941 if (arg == parm && !uses_template_parms (parm))
20942 return unify_success (explain_p);
20943
20944 /* Handle init lists early, so the rest of the function can assume
20945 we're dealing with a type. */
20946 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20947 {
20948 tree elt, elttype;
20949 unsigned i;
20950 tree orig_parm = parm;
20951
20952 /* Replace T with std::initializer_list<T> for deduction. */
20953 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20954 && flag_deduce_init_list)
20955 parm = listify (parm);
20956
20957 if (!is_std_init_list (parm)
20958 && TREE_CODE (parm) != ARRAY_TYPE)
20959 /* We can only deduce from an initializer list argument if the
20960 parameter is std::initializer_list or an array; otherwise this
20961 is a non-deduced context. */
20962 return unify_success (explain_p);
20963
20964 if (TREE_CODE (parm) == ARRAY_TYPE)
20965 elttype = TREE_TYPE (parm);
20966 else
20967 {
20968 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20969 /* Deduction is defined in terms of a single type, so just punt
20970 on the (bizarre) std::initializer_list<T...>. */
20971 if (PACK_EXPANSION_P (elttype))
20972 return unify_success (explain_p);
20973 }
20974
20975 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20976 {
20977 int elt_strict = strict;
20978
20979 if (elt == error_mark_node)
20980 return unify_invalid (explain_p);
20981
20982 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20983 {
20984 tree type = TREE_TYPE (elt);
20985 if (type == error_mark_node)
20986 return unify_invalid (explain_p);
20987 /* It should only be possible to get here for a call. */
20988 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20989 elt_strict |= maybe_adjust_types_for_deduction
20990 (DEDUCE_CALL, &elttype, &type, elt);
20991 elt = type;
20992 }
20993
20994 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20995 explain_p);
20996 }
20997
20998 if (TREE_CODE (parm) == ARRAY_TYPE
20999 && deducible_array_bound (TYPE_DOMAIN (parm)))
21000 {
21001 /* Also deduce from the length of the initializer list. */
21002 tree max = size_int (CONSTRUCTOR_NELTS (arg));
21003 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
21004 if (idx == error_mark_node)
21005 return unify_invalid (explain_p);
21006 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21007 idx, explain_p);
21008 }
21009
21010 /* If the std::initializer_list<T> deduction worked, replace the
21011 deduced A with std::initializer_list<A>. */
21012 if (orig_parm != parm)
21013 {
21014 idx = TEMPLATE_TYPE_IDX (orig_parm);
21015 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21016 targ = listify (targ);
21017 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
21018 }
21019 return unify_success (explain_p);
21020 }
21021
21022 /* If parm and arg aren't the same kind of thing (template, type, or
21023 expression), fail early. */
21024 if (pa_kind (parm) != pa_kind (arg))
21025 return unify_invalid (explain_p);
21026
21027 /* Immediately reject some pairs that won't unify because of
21028 cv-qualification mismatches. */
21029 if (TREE_CODE (arg) == TREE_CODE (parm)
21030 && TYPE_P (arg)
21031 /* It is the elements of the array which hold the cv quals of an array
21032 type, and the elements might be template type parms. We'll check
21033 when we recurse. */
21034 && TREE_CODE (arg) != ARRAY_TYPE
21035 /* We check the cv-qualifiers when unifying with template type
21036 parameters below. We want to allow ARG `const T' to unify with
21037 PARM `T' for example, when computing which of two templates
21038 is more specialized, for example. */
21039 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
21040 && !check_cv_quals_for_unify (strict_in, arg, parm))
21041 return unify_cv_qual_mismatch (explain_p, parm, arg);
21042
21043 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
21044 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
21045 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
21046 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
21047 strict &= ~UNIFY_ALLOW_DERIVED;
21048 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21049 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
21050
21051 switch (TREE_CODE (parm))
21052 {
21053 case TYPENAME_TYPE:
21054 case SCOPE_REF:
21055 case UNBOUND_CLASS_TEMPLATE:
21056 /* In a type which contains a nested-name-specifier, template
21057 argument values cannot be deduced for template parameters used
21058 within the nested-name-specifier. */
21059 return unify_success (explain_p);
21060
21061 case TEMPLATE_TYPE_PARM:
21062 case TEMPLATE_TEMPLATE_PARM:
21063 case BOUND_TEMPLATE_TEMPLATE_PARM:
21064 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21065 if (error_operand_p (tparm))
21066 return unify_invalid (explain_p);
21067
21068 if (TEMPLATE_TYPE_LEVEL (parm)
21069 != template_decl_level (tparm))
21070 /* The PARM is not one we're trying to unify. Just check
21071 to see if it matches ARG. */
21072 {
21073 if (TREE_CODE (arg) == TREE_CODE (parm)
21074 && (is_auto (parm) ? is_auto (arg)
21075 : same_type_p (parm, arg)))
21076 return unify_success (explain_p);
21077 else
21078 return unify_type_mismatch (explain_p, parm, arg);
21079 }
21080 idx = TEMPLATE_TYPE_IDX (parm);
21081 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21082 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
21083 if (error_operand_p (tparm))
21084 return unify_invalid (explain_p);
21085
21086 /* Check for mixed types and values. */
21087 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21088 && TREE_CODE (tparm) != TYPE_DECL)
21089 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21090 && TREE_CODE (tparm) != TEMPLATE_DECL))
21091 gcc_unreachable ();
21092
21093 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21094 {
21095 if ((strict_in & UNIFY_ALLOW_DERIVED)
21096 && CLASS_TYPE_P (arg))
21097 {
21098 /* First try to match ARG directly. */
21099 tree t = try_class_unification (tparms, targs, parm, arg,
21100 explain_p);
21101 if (!t)
21102 {
21103 /* Otherwise, look for a suitable base of ARG, as below. */
21104 enum template_base_result r;
21105 r = get_template_base (tparms, targs, parm, arg,
21106 explain_p, &t);
21107 if (!t)
21108 return unify_no_common_base (explain_p, r, parm, arg);
21109 arg = t;
21110 }
21111 }
21112 /* ARG must be constructed from a template class or a template
21113 template parameter. */
21114 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
21115 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21116 return unify_template_deduction_failure (explain_p, parm, arg);
21117
21118 /* Deduce arguments T, i from TT<T> or TT<i>. */
21119 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
21120 return 1;
21121
21122 arg = TYPE_TI_TEMPLATE (arg);
21123
21124 /* Fall through to deduce template name. */
21125 }
21126
21127 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21128 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21129 {
21130 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21131
21132 /* Simple cases: Value already set, does match or doesn't. */
21133 if (targ != NULL_TREE && template_args_equal (targ, arg))
21134 return unify_success (explain_p);
21135 else if (targ)
21136 return unify_inconsistency (explain_p, parm, targ, arg);
21137 }
21138 else
21139 {
21140 /* If PARM is `const T' and ARG is only `int', we don't have
21141 a match unless we are allowing additional qualification.
21142 If ARG is `const int' and PARM is just `T' that's OK;
21143 that binds `const int' to `T'. */
21144 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
21145 arg, parm))
21146 return unify_cv_qual_mismatch (explain_p, parm, arg);
21147
21148 /* Consider the case where ARG is `const volatile int' and
21149 PARM is `const T'. Then, T should be `volatile int'. */
21150 arg = cp_build_qualified_type_real
21151 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
21152 if (arg == error_mark_node)
21153 return unify_invalid (explain_p);
21154
21155 /* Simple cases: Value already set, does match or doesn't. */
21156 if (targ != NULL_TREE && same_type_p (targ, arg))
21157 return unify_success (explain_p);
21158 else if (targ)
21159 return unify_inconsistency (explain_p, parm, targ, arg);
21160
21161 /* Make sure that ARG is not a variable-sized array. (Note
21162 that were talking about variable-sized arrays (like
21163 `int[n]'), rather than arrays of unknown size (like
21164 `int[]').) We'll get very confused by such a type since
21165 the bound of the array is not constant, and therefore
21166 not mangleable. Besides, such types are not allowed in
21167 ISO C++, so we can do as we please here. We do allow
21168 them for 'auto' deduction, since that isn't ABI-exposed. */
21169 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
21170 return unify_vla_arg (explain_p, arg);
21171
21172 /* Strip typedefs as in convert_template_argument. */
21173 arg = canonicalize_type_argument (arg, tf_none);
21174 }
21175
21176 /* If ARG is a parameter pack or an expansion, we cannot unify
21177 against it unless PARM is also a parameter pack. */
21178 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21179 && !template_parameter_pack_p (parm))
21180 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21181
21182 /* If the argument deduction results is a METHOD_TYPE,
21183 then there is a problem.
21184 METHOD_TYPE doesn't map to any real C++ type the result of
21185 the deduction can not be of that type. */
21186 if (TREE_CODE (arg) == METHOD_TYPE)
21187 return unify_method_type_error (explain_p, arg);
21188
21189 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21190 return unify_success (explain_p);
21191
21192 case TEMPLATE_PARM_INDEX:
21193 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21194 if (error_operand_p (tparm))
21195 return unify_invalid (explain_p);
21196
21197 if (TEMPLATE_PARM_LEVEL (parm)
21198 != template_decl_level (tparm))
21199 {
21200 /* The PARM is not one we're trying to unify. Just check
21201 to see if it matches ARG. */
21202 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
21203 && cp_tree_equal (parm, arg));
21204 if (result)
21205 unify_expression_unequal (explain_p, parm, arg);
21206 return result;
21207 }
21208
21209 idx = TEMPLATE_PARM_IDX (parm);
21210 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21211
21212 if (targ)
21213 {
21214 if ((strict & UNIFY_ALLOW_INTEGER)
21215 && TREE_TYPE (targ) && TREE_TYPE (arg)
21216 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
21217 /* We're deducing from an array bound, the type doesn't matter. */
21218 arg = fold_convert (TREE_TYPE (targ), arg);
21219 int x = !cp_tree_equal (targ, arg);
21220 if (x)
21221 unify_inconsistency (explain_p, parm, targ, arg);
21222 return x;
21223 }
21224
21225 /* [temp.deduct.type] If, in the declaration of a function template
21226 with a non-type template-parameter, the non-type
21227 template-parameter is used in an expression in the function
21228 parameter-list and, if the corresponding template-argument is
21229 deduced, the template-argument type shall match the type of the
21230 template-parameter exactly, except that a template-argument
21231 deduced from an array bound may be of any integral type.
21232 The non-type parameter might use already deduced type parameters. */
21233 tparm = TREE_TYPE (parm);
21234 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
21235 /* We don't have enough levels of args to do any substitution. This
21236 can happen in the context of -fnew-ttp-matching. */;
21237 else
21238 {
21239 ++processing_template_decl;
21240 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
21241 --processing_template_decl;
21242
21243 if (tree a = type_uses_auto (tparm))
21244 {
21245 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21246 if (tparm == error_mark_node)
21247 return 1;
21248 }
21249 }
21250
21251 if (!TREE_TYPE (arg))
21252 /* Template-parameter dependent expression. Just accept it for now.
21253 It will later be processed in convert_template_argument. */
21254 ;
21255 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21256 non_reference (tparm)))
21257 /* OK */;
21258 else if ((strict & UNIFY_ALLOW_INTEGER)
21259 && CP_INTEGRAL_TYPE_P (tparm))
21260 /* Convert the ARG to the type of PARM; the deduced non-type
21261 template argument must exactly match the types of the
21262 corresponding parameter. */
21263 arg = fold (build_nop (tparm, arg));
21264 else if (uses_template_parms (tparm))
21265 {
21266 /* We haven't deduced the type of this parameter yet. */
21267 if (cxx_dialect >= cxx17
21268 /* We deduce from array bounds in try_array_deduction. */
21269 && !(strict & UNIFY_ALLOW_INTEGER))
21270 {
21271 /* Deduce it from the non-type argument. */
21272 tree atype = TREE_TYPE (arg);
21273 RECUR_AND_CHECK_FAILURE (tparms, targs,
21274 tparm, atype,
21275 UNIFY_ALLOW_NONE, explain_p);
21276 }
21277 else
21278 /* Try again later. */
21279 return unify_success (explain_p);
21280 }
21281 else
21282 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21283
21284 /* If ARG is a parameter pack or an expansion, we cannot unify
21285 against it unless PARM is also a parameter pack. */
21286 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21287 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21288 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21289
21290 {
21291 bool removed_attr = false;
21292 arg = strip_typedefs_expr (arg, &removed_attr);
21293 }
21294 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21295 return unify_success (explain_p);
21296
21297 case PTRMEM_CST:
21298 {
21299 /* A pointer-to-member constant can be unified only with
21300 another constant. */
21301 if (TREE_CODE (arg) != PTRMEM_CST)
21302 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21303
21304 /* Just unify the class member. It would be useless (and possibly
21305 wrong, depending on the strict flags) to unify also
21306 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21307 arg refer to the same variable, even if through different
21308 classes. For instance:
21309
21310 struct A { int x; };
21311 struct B : A { };
21312
21313 Unification of &A::x and &B::x must succeed. */
21314 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21315 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21316 }
21317
21318 case POINTER_TYPE:
21319 {
21320 if (!TYPE_PTR_P (arg))
21321 return unify_type_mismatch (explain_p, parm, arg);
21322
21323 /* [temp.deduct.call]
21324
21325 A can be another pointer or pointer to member type that can
21326 be converted to the deduced A via a qualification
21327 conversion (_conv.qual_).
21328
21329 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21330 This will allow for additional cv-qualification of the
21331 pointed-to types if appropriate. */
21332
21333 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21334 /* The derived-to-base conversion only persists through one
21335 level of pointers. */
21336 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21337
21338 return unify (tparms, targs, TREE_TYPE (parm),
21339 TREE_TYPE (arg), strict, explain_p);
21340 }
21341
21342 case REFERENCE_TYPE:
21343 if (TREE_CODE (arg) != REFERENCE_TYPE)
21344 return unify_type_mismatch (explain_p, parm, arg);
21345 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21346 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21347
21348 case ARRAY_TYPE:
21349 if (TREE_CODE (arg) != ARRAY_TYPE)
21350 return unify_type_mismatch (explain_p, parm, arg);
21351 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21352 != (TYPE_DOMAIN (arg) == NULL_TREE))
21353 return unify_type_mismatch (explain_p, parm, arg);
21354 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21355 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21356 if (TYPE_DOMAIN (parm) != NULL_TREE)
21357 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21358 TYPE_DOMAIN (arg), explain_p);
21359 return unify_success (explain_p);
21360
21361 case REAL_TYPE:
21362 case COMPLEX_TYPE:
21363 case VECTOR_TYPE:
21364 case INTEGER_TYPE:
21365 case BOOLEAN_TYPE:
21366 case ENUMERAL_TYPE:
21367 case VOID_TYPE:
21368 case NULLPTR_TYPE:
21369 if (TREE_CODE (arg) != TREE_CODE (parm))
21370 return unify_type_mismatch (explain_p, parm, arg);
21371
21372 /* We have already checked cv-qualification at the top of the
21373 function. */
21374 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21375 return unify_type_mismatch (explain_p, parm, arg);
21376
21377 /* As far as unification is concerned, this wins. Later checks
21378 will invalidate it if necessary. */
21379 return unify_success (explain_p);
21380
21381 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21382 /* Type INTEGER_CST can come from ordinary constant template args. */
21383 case INTEGER_CST:
21384 while (CONVERT_EXPR_P (arg))
21385 arg = TREE_OPERAND (arg, 0);
21386
21387 if (TREE_CODE (arg) != INTEGER_CST)
21388 return unify_template_argument_mismatch (explain_p, parm, arg);
21389 return (tree_int_cst_equal (parm, arg)
21390 ? unify_success (explain_p)
21391 : unify_template_argument_mismatch (explain_p, parm, arg));
21392
21393 case TREE_VEC:
21394 {
21395 int i, len, argslen;
21396 int parm_variadic_p = 0;
21397
21398 if (TREE_CODE (arg) != TREE_VEC)
21399 return unify_template_argument_mismatch (explain_p, parm, arg);
21400
21401 len = TREE_VEC_LENGTH (parm);
21402 argslen = TREE_VEC_LENGTH (arg);
21403
21404 /* Check for pack expansions in the parameters. */
21405 for (i = 0; i < len; ++i)
21406 {
21407 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21408 {
21409 if (i == len - 1)
21410 /* We can unify against something with a trailing
21411 parameter pack. */
21412 parm_variadic_p = 1;
21413 else
21414 /* [temp.deduct.type]/9: If the template argument list of
21415 P contains a pack expansion that is not the last
21416 template argument, the entire template argument list
21417 is a non-deduced context. */
21418 return unify_success (explain_p);
21419 }
21420 }
21421
21422 /* If we don't have enough arguments to satisfy the parameters
21423 (not counting the pack expression at the end), or we have
21424 too many arguments for a parameter list that doesn't end in
21425 a pack expression, we can't unify. */
21426 if (parm_variadic_p
21427 ? argslen < len - parm_variadic_p
21428 : argslen != len)
21429 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21430
21431 /* Unify all of the parameters that precede the (optional)
21432 pack expression. */
21433 for (i = 0; i < len - parm_variadic_p; ++i)
21434 {
21435 RECUR_AND_CHECK_FAILURE (tparms, targs,
21436 TREE_VEC_ELT (parm, i),
21437 TREE_VEC_ELT (arg, i),
21438 UNIFY_ALLOW_NONE, explain_p);
21439 }
21440 if (parm_variadic_p)
21441 return unify_pack_expansion (tparms, targs, parm, arg,
21442 DEDUCE_EXACT,
21443 /*subr=*/true, explain_p);
21444 return unify_success (explain_p);
21445 }
21446
21447 case RECORD_TYPE:
21448 case UNION_TYPE:
21449 if (TREE_CODE (arg) != TREE_CODE (parm))
21450 return unify_type_mismatch (explain_p, parm, arg);
21451
21452 if (TYPE_PTRMEMFUNC_P (parm))
21453 {
21454 if (!TYPE_PTRMEMFUNC_P (arg))
21455 return unify_type_mismatch (explain_p, parm, arg);
21456
21457 return unify (tparms, targs,
21458 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21459 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21460 strict, explain_p);
21461 }
21462 else if (TYPE_PTRMEMFUNC_P (arg))
21463 return unify_type_mismatch (explain_p, parm, arg);
21464
21465 if (CLASSTYPE_TEMPLATE_INFO (parm))
21466 {
21467 tree t = NULL_TREE;
21468
21469 if (strict_in & UNIFY_ALLOW_DERIVED)
21470 {
21471 /* First, we try to unify the PARM and ARG directly. */
21472 t = try_class_unification (tparms, targs,
21473 parm, arg, explain_p);
21474
21475 if (!t)
21476 {
21477 /* Fallback to the special case allowed in
21478 [temp.deduct.call]:
21479
21480 If P is a class, and P has the form
21481 template-id, then A can be a derived class of
21482 the deduced A. Likewise, if P is a pointer to
21483 a class of the form template-id, A can be a
21484 pointer to a derived class pointed to by the
21485 deduced A. */
21486 enum template_base_result r;
21487 r = get_template_base (tparms, targs, parm, arg,
21488 explain_p, &t);
21489
21490 if (!t)
21491 {
21492 /* Don't give the derived diagnostic if we're
21493 already dealing with the same template. */
21494 bool same_template
21495 = (CLASSTYPE_TEMPLATE_INFO (arg)
21496 && (CLASSTYPE_TI_TEMPLATE (parm)
21497 == CLASSTYPE_TI_TEMPLATE (arg)));
21498 return unify_no_common_base (explain_p && !same_template,
21499 r, parm, arg);
21500 }
21501 }
21502 }
21503 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21504 && (CLASSTYPE_TI_TEMPLATE (parm)
21505 == CLASSTYPE_TI_TEMPLATE (arg)))
21506 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21507 Then, we should unify `int' and `U'. */
21508 t = arg;
21509 else
21510 /* There's no chance of unification succeeding. */
21511 return unify_type_mismatch (explain_p, parm, arg);
21512
21513 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21514 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21515 }
21516 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21517 return unify_type_mismatch (explain_p, parm, arg);
21518 return unify_success (explain_p);
21519
21520 case METHOD_TYPE:
21521 case FUNCTION_TYPE:
21522 {
21523 unsigned int nargs;
21524 tree *args;
21525 tree a;
21526 unsigned int i;
21527
21528 if (TREE_CODE (arg) != TREE_CODE (parm))
21529 return unify_type_mismatch (explain_p, parm, arg);
21530
21531 /* CV qualifications for methods can never be deduced, they must
21532 match exactly. We need to check them explicitly here,
21533 because type_unification_real treats them as any other
21534 cv-qualified parameter. */
21535 if (TREE_CODE (parm) == METHOD_TYPE
21536 && (!check_cv_quals_for_unify
21537 (UNIFY_ALLOW_NONE,
21538 class_of_this_parm (arg),
21539 class_of_this_parm (parm))))
21540 return unify_cv_qual_mismatch (explain_p, parm, arg);
21541 if (TREE_CODE (arg) == FUNCTION_TYPE
21542 && type_memfn_quals (parm) != type_memfn_quals (arg))
21543 return unify_cv_qual_mismatch (explain_p, parm, arg);
21544 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21545 return unify_type_mismatch (explain_p, parm, arg);
21546
21547 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21548 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21549
21550 nargs = list_length (TYPE_ARG_TYPES (arg));
21551 args = XALLOCAVEC (tree, nargs);
21552 for (a = TYPE_ARG_TYPES (arg), i = 0;
21553 a != NULL_TREE && a != void_list_node;
21554 a = TREE_CHAIN (a), ++i)
21555 args[i] = TREE_VALUE (a);
21556 nargs = i;
21557
21558 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21559 args, nargs, 1, DEDUCE_EXACT,
21560 LOOKUP_NORMAL, NULL, explain_p))
21561 return 1;
21562
21563 if (flag_noexcept_type)
21564 {
21565 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21566 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21567 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21568 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21569 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21570 && uses_template_parms (TREE_PURPOSE (pspec)))
21571 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21572 TREE_PURPOSE (aspec),
21573 UNIFY_ALLOW_NONE, explain_p);
21574 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21575 return unify_type_mismatch (explain_p, parm, arg);
21576 }
21577
21578 return 0;
21579 }
21580
21581 case OFFSET_TYPE:
21582 /* Unify a pointer to member with a pointer to member function, which
21583 deduces the type of the member as a function type. */
21584 if (TYPE_PTRMEMFUNC_P (arg))
21585 {
21586 /* Check top-level cv qualifiers */
21587 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21588 return unify_cv_qual_mismatch (explain_p, parm, arg);
21589
21590 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21591 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21592 UNIFY_ALLOW_NONE, explain_p);
21593
21594 /* Determine the type of the function we are unifying against. */
21595 tree fntype = static_fn_type (arg);
21596
21597 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21598 }
21599
21600 if (TREE_CODE (arg) != OFFSET_TYPE)
21601 return unify_type_mismatch (explain_p, parm, arg);
21602 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21603 TYPE_OFFSET_BASETYPE (arg),
21604 UNIFY_ALLOW_NONE, explain_p);
21605 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21606 strict, explain_p);
21607
21608 case CONST_DECL:
21609 if (DECL_TEMPLATE_PARM_P (parm))
21610 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21611 if (arg != scalar_constant_value (parm))
21612 return unify_template_argument_mismatch (explain_p, parm, arg);
21613 return unify_success (explain_p);
21614
21615 case FIELD_DECL:
21616 case TEMPLATE_DECL:
21617 /* Matched cases are handled by the ARG == PARM test above. */
21618 return unify_template_argument_mismatch (explain_p, parm, arg);
21619
21620 case VAR_DECL:
21621 /* We might get a variable as a non-type template argument in parm if the
21622 corresponding parameter is type-dependent. Make any necessary
21623 adjustments based on whether arg is a reference. */
21624 if (CONSTANT_CLASS_P (arg))
21625 parm = fold_non_dependent_expr (parm);
21626 else if (REFERENCE_REF_P (arg))
21627 {
21628 tree sub = TREE_OPERAND (arg, 0);
21629 STRIP_NOPS (sub);
21630 if (TREE_CODE (sub) == ADDR_EXPR)
21631 arg = TREE_OPERAND (sub, 0);
21632 }
21633 /* Now use the normal expression code to check whether they match. */
21634 goto expr;
21635
21636 case TYPE_ARGUMENT_PACK:
21637 case NONTYPE_ARGUMENT_PACK:
21638 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21639 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21640
21641 case TYPEOF_TYPE:
21642 case DECLTYPE_TYPE:
21643 case UNDERLYING_TYPE:
21644 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21645 or UNDERLYING_TYPE nodes. */
21646 return unify_success (explain_p);
21647
21648 case ERROR_MARK:
21649 /* Unification fails if we hit an error node. */
21650 return unify_invalid (explain_p);
21651
21652 case INDIRECT_REF:
21653 if (REFERENCE_REF_P (parm))
21654 {
21655 bool pexp = PACK_EXPANSION_P (arg);
21656 if (pexp)
21657 arg = PACK_EXPANSION_PATTERN (arg);
21658 if (REFERENCE_REF_P (arg))
21659 arg = TREE_OPERAND (arg, 0);
21660 if (pexp)
21661 arg = make_pack_expansion (arg, complain);
21662 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21663 strict, explain_p);
21664 }
21665 /* FALLTHRU */
21666
21667 default:
21668 /* An unresolved overload is a nondeduced context. */
21669 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21670 return unify_success (explain_p);
21671 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21672 expr:
21673 /* We must be looking at an expression. This can happen with
21674 something like:
21675
21676 template <int I>
21677 void foo(S<I>, S<I + 2>);
21678
21679 This is a "nondeduced context":
21680
21681 [deduct.type]
21682
21683 The nondeduced contexts are:
21684
21685 --A type that is a template-id in which one or more of
21686 the template-arguments is an expression that references
21687 a template-parameter.
21688
21689 In these cases, we assume deduction succeeded, but don't
21690 actually infer any unifications. */
21691
21692 if (!uses_template_parms (parm)
21693 && !template_args_equal (parm, arg))
21694 return unify_expression_unequal (explain_p, parm, arg);
21695 else
21696 return unify_success (explain_p);
21697 }
21698 }
21699 #undef RECUR_AND_CHECK_FAILURE
21700 \f
21701 /* Note that DECL can be defined in this translation unit, if
21702 required. */
21703
21704 static void
21705 mark_definable (tree decl)
21706 {
21707 tree clone;
21708 DECL_NOT_REALLY_EXTERN (decl) = 1;
21709 FOR_EACH_CLONE (clone, decl)
21710 DECL_NOT_REALLY_EXTERN (clone) = 1;
21711 }
21712
21713 /* Called if RESULT is explicitly instantiated, or is a member of an
21714 explicitly instantiated class. */
21715
21716 void
21717 mark_decl_instantiated (tree result, int extern_p)
21718 {
21719 SET_DECL_EXPLICIT_INSTANTIATION (result);
21720
21721 /* If this entity has already been written out, it's too late to
21722 make any modifications. */
21723 if (TREE_ASM_WRITTEN (result))
21724 return;
21725
21726 /* For anonymous namespace we don't need to do anything. */
21727 if (decl_anon_ns_mem_p (result))
21728 {
21729 gcc_assert (!TREE_PUBLIC (result));
21730 return;
21731 }
21732
21733 if (TREE_CODE (result) != FUNCTION_DECL)
21734 /* The TREE_PUBLIC flag for function declarations will have been
21735 set correctly by tsubst. */
21736 TREE_PUBLIC (result) = 1;
21737
21738 /* This might have been set by an earlier implicit instantiation. */
21739 DECL_COMDAT (result) = 0;
21740
21741 if (extern_p)
21742 DECL_NOT_REALLY_EXTERN (result) = 0;
21743 else
21744 {
21745 mark_definable (result);
21746 mark_needed (result);
21747 /* Always make artificials weak. */
21748 if (DECL_ARTIFICIAL (result) && flag_weak)
21749 comdat_linkage (result);
21750 /* For WIN32 we also want to put explicit instantiations in
21751 linkonce sections. */
21752 else if (TREE_PUBLIC (result))
21753 maybe_make_one_only (result);
21754 }
21755
21756 /* If EXTERN_P, then this function will not be emitted -- unless
21757 followed by an explicit instantiation, at which point its linkage
21758 will be adjusted. If !EXTERN_P, then this function will be
21759 emitted here. In neither circumstance do we want
21760 import_export_decl to adjust the linkage. */
21761 DECL_INTERFACE_KNOWN (result) = 1;
21762 }
21763
21764 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21765 important template arguments. If any are missing, we check whether
21766 they're important by using error_mark_node for substituting into any
21767 args that were used for partial ordering (the ones between ARGS and END)
21768 and seeing if it bubbles up. */
21769
21770 static bool
21771 check_undeduced_parms (tree targs, tree args, tree end)
21772 {
21773 bool found = false;
21774 int i;
21775 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21776 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21777 {
21778 found = true;
21779 TREE_VEC_ELT (targs, i) = error_mark_node;
21780 }
21781 if (found)
21782 {
21783 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21784 if (substed == error_mark_node)
21785 return true;
21786 }
21787 return false;
21788 }
21789
21790 /* Given two function templates PAT1 and PAT2, return:
21791
21792 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21793 -1 if PAT2 is more specialized than PAT1.
21794 0 if neither is more specialized.
21795
21796 LEN indicates the number of parameters we should consider
21797 (defaulted parameters should not be considered).
21798
21799 The 1998 std underspecified function template partial ordering, and
21800 DR214 addresses the issue. We take pairs of arguments, one from
21801 each of the templates, and deduce them against each other. One of
21802 the templates will be more specialized if all the *other*
21803 template's arguments deduce against its arguments and at least one
21804 of its arguments *does* *not* deduce against the other template's
21805 corresponding argument. Deduction is done as for class templates.
21806 The arguments used in deduction have reference and top level cv
21807 qualifiers removed. Iff both arguments were originally reference
21808 types *and* deduction succeeds in both directions, an lvalue reference
21809 wins against an rvalue reference and otherwise the template
21810 with the more cv-qualified argument wins for that pairing (if
21811 neither is more cv-qualified, they both are equal). Unlike regular
21812 deduction, after all the arguments have been deduced in this way,
21813 we do *not* verify the deduced template argument values can be
21814 substituted into non-deduced contexts.
21815
21816 The logic can be a bit confusing here, because we look at deduce1 and
21817 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21818 can find template arguments for pat1 to make arg1 look like arg2, that
21819 means that arg2 is at least as specialized as arg1. */
21820
21821 int
21822 more_specialized_fn (tree pat1, tree pat2, int len)
21823 {
21824 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21825 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21826 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21827 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21828 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21829 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21830 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21831 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21832 tree origs1, origs2;
21833 bool lose1 = false;
21834 bool lose2 = false;
21835
21836 /* Remove the this parameter from non-static member functions. If
21837 one is a non-static member function and the other is not a static
21838 member function, remove the first parameter from that function
21839 also. This situation occurs for operator functions where we
21840 locate both a member function (with this pointer) and non-member
21841 operator (with explicit first operand). */
21842 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21843 {
21844 len--; /* LEN is the number of significant arguments for DECL1 */
21845 args1 = TREE_CHAIN (args1);
21846 if (!DECL_STATIC_FUNCTION_P (decl2))
21847 args2 = TREE_CHAIN (args2);
21848 }
21849 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21850 {
21851 args2 = TREE_CHAIN (args2);
21852 if (!DECL_STATIC_FUNCTION_P (decl1))
21853 {
21854 len--;
21855 args1 = TREE_CHAIN (args1);
21856 }
21857 }
21858
21859 /* If only one is a conversion operator, they are unordered. */
21860 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21861 return 0;
21862
21863 /* Consider the return type for a conversion function */
21864 if (DECL_CONV_FN_P (decl1))
21865 {
21866 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21867 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21868 len++;
21869 }
21870
21871 processing_template_decl++;
21872
21873 origs1 = args1;
21874 origs2 = args2;
21875
21876 while (len--
21877 /* Stop when an ellipsis is seen. */
21878 && args1 != NULL_TREE && args2 != NULL_TREE)
21879 {
21880 tree arg1 = TREE_VALUE (args1);
21881 tree arg2 = TREE_VALUE (args2);
21882 int deduce1, deduce2;
21883 int quals1 = -1;
21884 int quals2 = -1;
21885 int ref1 = 0;
21886 int ref2 = 0;
21887
21888 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21889 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21890 {
21891 /* When both arguments are pack expansions, we need only
21892 unify the patterns themselves. */
21893 arg1 = PACK_EXPANSION_PATTERN (arg1);
21894 arg2 = PACK_EXPANSION_PATTERN (arg2);
21895
21896 /* This is the last comparison we need to do. */
21897 len = 0;
21898 }
21899
21900 /* DR 1847: If a particular P contains no template-parameters that
21901 participate in template argument deduction, that P is not used to
21902 determine the ordering. */
21903 if (!uses_deducible_template_parms (arg1)
21904 && !uses_deducible_template_parms (arg2))
21905 goto next;
21906
21907 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21908 {
21909 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21910 arg1 = TREE_TYPE (arg1);
21911 quals1 = cp_type_quals (arg1);
21912 }
21913
21914 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21915 {
21916 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21917 arg2 = TREE_TYPE (arg2);
21918 quals2 = cp_type_quals (arg2);
21919 }
21920
21921 arg1 = TYPE_MAIN_VARIANT (arg1);
21922 arg2 = TYPE_MAIN_VARIANT (arg2);
21923
21924 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21925 {
21926 int i, len2 = remaining_arguments (args2);
21927 tree parmvec = make_tree_vec (1);
21928 tree argvec = make_tree_vec (len2);
21929 tree ta = args2;
21930
21931 /* Setup the parameter vector, which contains only ARG1. */
21932 TREE_VEC_ELT (parmvec, 0) = arg1;
21933
21934 /* Setup the argument vector, which contains the remaining
21935 arguments. */
21936 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21937 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21938
21939 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21940 argvec, DEDUCE_EXACT,
21941 /*subr=*/true, /*explain_p=*/false)
21942 == 0);
21943
21944 /* We cannot deduce in the other direction, because ARG1 is
21945 a pack expansion but ARG2 is not. */
21946 deduce2 = 0;
21947 }
21948 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21949 {
21950 int i, len1 = remaining_arguments (args1);
21951 tree parmvec = make_tree_vec (1);
21952 tree argvec = make_tree_vec (len1);
21953 tree ta = args1;
21954
21955 /* Setup the parameter vector, which contains only ARG1. */
21956 TREE_VEC_ELT (parmvec, 0) = arg2;
21957
21958 /* Setup the argument vector, which contains the remaining
21959 arguments. */
21960 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21961 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21962
21963 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21964 argvec, DEDUCE_EXACT,
21965 /*subr=*/true, /*explain_p=*/false)
21966 == 0);
21967
21968 /* We cannot deduce in the other direction, because ARG2 is
21969 a pack expansion but ARG1 is not.*/
21970 deduce1 = 0;
21971 }
21972
21973 else
21974 {
21975 /* The normal case, where neither argument is a pack
21976 expansion. */
21977 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21978 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21979 == 0);
21980 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21981 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21982 == 0);
21983 }
21984
21985 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21986 arg2, then arg2 is not as specialized as arg1. */
21987 if (!deduce1)
21988 lose2 = true;
21989 if (!deduce2)
21990 lose1 = true;
21991
21992 /* "If, for a given type, deduction succeeds in both directions
21993 (i.e., the types are identical after the transformations above)
21994 and both P and A were reference types (before being replaced with
21995 the type referred to above):
21996 - if the type from the argument template was an lvalue reference and
21997 the type from the parameter template was not, the argument type is
21998 considered to be more specialized than the other; otherwise,
21999 - if the type from the argument template is more cv-qualified
22000 than the type from the parameter template (as described above),
22001 the argument type is considered to be more specialized than the other;
22002 otherwise,
22003 - neither type is more specialized than the other." */
22004
22005 if (deduce1 && deduce2)
22006 {
22007 if (ref1 && ref2 && ref1 != ref2)
22008 {
22009 if (ref1 > ref2)
22010 lose1 = true;
22011 else
22012 lose2 = true;
22013 }
22014 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
22015 {
22016 if ((quals1 & quals2) == quals2)
22017 lose2 = true;
22018 if ((quals1 & quals2) == quals1)
22019 lose1 = true;
22020 }
22021 }
22022
22023 if (lose1 && lose2)
22024 /* We've failed to deduce something in either direction.
22025 These must be unordered. */
22026 break;
22027
22028 next:
22029
22030 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22031 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22032 /* We have already processed all of the arguments in our
22033 handing of the pack expansion type. */
22034 len = 0;
22035
22036 args1 = TREE_CHAIN (args1);
22037 args2 = TREE_CHAIN (args2);
22038 }
22039
22040 /* "In most cases, all template parameters must have values in order for
22041 deduction to succeed, but for partial ordering purposes a template
22042 parameter may remain without a value provided it is not used in the
22043 types being used for partial ordering."
22044
22045 Thus, if we are missing any of the targs1 we need to substitute into
22046 origs1, then pat2 is not as specialized as pat1. This can happen when
22047 there is a nondeduced context. */
22048 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
22049 lose2 = true;
22050 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
22051 lose1 = true;
22052
22053 processing_template_decl--;
22054
22055 /* If both deductions succeed, the partial ordering selects the more
22056 constrained template. */
22057 if (!lose1 && !lose2)
22058 {
22059 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
22060 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
22061 lose1 = !subsumes_constraints (c1, c2);
22062 lose2 = !subsumes_constraints (c2, c1);
22063 }
22064
22065 /* All things being equal, if the next argument is a pack expansion
22066 for one function but not for the other, prefer the
22067 non-variadic function. FIXME this is bogus; see c++/41958. */
22068 if (lose1 == lose2
22069 && args1 && TREE_VALUE (args1)
22070 && args2 && TREE_VALUE (args2))
22071 {
22072 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
22073 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
22074 }
22075
22076 if (lose1 == lose2)
22077 return 0;
22078 else if (!lose1)
22079 return 1;
22080 else
22081 return -1;
22082 }
22083
22084 /* Determine which of two partial specializations of TMPL is more
22085 specialized.
22086
22087 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22088 to the first partial specialization. The TREE_PURPOSE is the
22089 innermost set of template parameters for the partial
22090 specialization. PAT2 is similar, but for the second template.
22091
22092 Return 1 if the first partial specialization is more specialized;
22093 -1 if the second is more specialized; 0 if neither is more
22094 specialized.
22095
22096 See [temp.class.order] for information about determining which of
22097 two templates is more specialized. */
22098
22099 static int
22100 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
22101 {
22102 tree targs;
22103 int winner = 0;
22104 bool any_deductions = false;
22105
22106 tree tmpl1 = TREE_VALUE (pat1);
22107 tree tmpl2 = TREE_VALUE (pat2);
22108 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
22109 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
22110
22111 /* Just like what happens for functions, if we are ordering between
22112 different template specializations, we may encounter dependent
22113 types in the arguments, and we need our dependency check functions
22114 to behave correctly. */
22115 ++processing_template_decl;
22116 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
22117 if (targs)
22118 {
22119 --winner;
22120 any_deductions = true;
22121 }
22122
22123 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
22124 if (targs)
22125 {
22126 ++winner;
22127 any_deductions = true;
22128 }
22129 --processing_template_decl;
22130
22131 /* If both deductions succeed, the partial ordering selects the more
22132 constrained template. */
22133 if (!winner && any_deductions)
22134 return more_constrained (tmpl1, tmpl2);
22135
22136 /* In the case of a tie where at least one of the templates
22137 has a parameter pack at the end, the template with the most
22138 non-packed parameters wins. */
22139 if (winner == 0
22140 && any_deductions
22141 && (template_args_variadic_p (TREE_PURPOSE (pat1))
22142 || template_args_variadic_p (TREE_PURPOSE (pat2))))
22143 {
22144 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
22145 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
22146 int len1 = TREE_VEC_LENGTH (args1);
22147 int len2 = TREE_VEC_LENGTH (args2);
22148
22149 /* We don't count the pack expansion at the end. */
22150 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
22151 --len1;
22152 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
22153 --len2;
22154
22155 if (len1 > len2)
22156 return 1;
22157 else if (len1 < len2)
22158 return -1;
22159 }
22160
22161 return winner;
22162 }
22163
22164 /* Return the template arguments that will produce the function signature
22165 DECL from the function template FN, with the explicit template
22166 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22167 also match. Return NULL_TREE if no satisfactory arguments could be
22168 found. */
22169
22170 static tree
22171 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
22172 {
22173 int ntparms = DECL_NTPARMS (fn);
22174 tree targs = make_tree_vec (ntparms);
22175 tree decl_type = TREE_TYPE (decl);
22176 tree decl_arg_types;
22177 tree *args;
22178 unsigned int nargs, ix;
22179 tree arg;
22180
22181 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
22182
22183 /* Never do unification on the 'this' parameter. */
22184 decl_arg_types = skip_artificial_parms_for (decl,
22185 TYPE_ARG_TYPES (decl_type));
22186
22187 nargs = list_length (decl_arg_types);
22188 args = XALLOCAVEC (tree, nargs);
22189 for (arg = decl_arg_types, ix = 0;
22190 arg != NULL_TREE && arg != void_list_node;
22191 arg = TREE_CHAIN (arg), ++ix)
22192 args[ix] = TREE_VALUE (arg);
22193
22194 if (fn_type_unification (fn, explicit_args, targs,
22195 args, ix,
22196 (check_rettype || DECL_CONV_FN_P (fn)
22197 ? TREE_TYPE (decl_type) : NULL_TREE),
22198 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
22199 /*decltype*/false)
22200 == error_mark_node)
22201 return NULL_TREE;
22202
22203 return targs;
22204 }
22205
22206 /* Return the innermost template arguments that, when applied to a partial
22207 specialization SPEC_TMPL of TMPL, yield the ARGS.
22208
22209 For example, suppose we have:
22210
22211 template <class T, class U> struct S {};
22212 template <class T> struct S<T*, int> {};
22213
22214 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22215 partial specialization and the ARGS will be {double*, int}. The resulting
22216 vector will be {double}, indicating that `T' is bound to `double'. */
22217
22218 static tree
22219 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
22220 {
22221 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
22222 tree spec_args
22223 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
22224 int i, ntparms = TREE_VEC_LENGTH (tparms);
22225 tree deduced_args;
22226 tree innermost_deduced_args;
22227
22228 innermost_deduced_args = make_tree_vec (ntparms);
22229 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22230 {
22231 deduced_args = copy_node (args);
22232 SET_TMPL_ARGS_LEVEL (deduced_args,
22233 TMPL_ARGS_DEPTH (deduced_args),
22234 innermost_deduced_args);
22235 }
22236 else
22237 deduced_args = innermost_deduced_args;
22238
22239 bool tried_array_deduction = (cxx_dialect < cxx17);
22240 again:
22241 if (unify (tparms, deduced_args,
22242 INNERMOST_TEMPLATE_ARGS (spec_args),
22243 INNERMOST_TEMPLATE_ARGS (args),
22244 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22245 return NULL_TREE;
22246
22247 for (i = 0; i < ntparms; ++i)
22248 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22249 {
22250 if (!tried_array_deduction)
22251 {
22252 try_array_deduction (tparms, innermost_deduced_args,
22253 INNERMOST_TEMPLATE_ARGS (spec_args));
22254 tried_array_deduction = true;
22255 if (TREE_VEC_ELT (innermost_deduced_args, i))
22256 goto again;
22257 }
22258 return NULL_TREE;
22259 }
22260
22261 tree tinst = build_tree_list (spec_tmpl, deduced_args);
22262 if (!push_tinst_level (tinst))
22263 {
22264 excessive_deduction_depth = true;
22265 return NULL_TREE;
22266 }
22267
22268 /* Verify that nondeduced template arguments agree with the type
22269 obtained from argument deduction.
22270
22271 For example:
22272
22273 struct A { typedef int X; };
22274 template <class T, class U> struct C {};
22275 template <class T> struct C<T, typename T::X> {};
22276
22277 Then with the instantiation `C<A, int>', we can deduce that
22278 `T' is `A' but unify () does not check whether `typename T::X'
22279 is `int'. */
22280 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22281
22282 if (spec_args != error_mark_node)
22283 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22284 INNERMOST_TEMPLATE_ARGS (spec_args),
22285 tmpl, tf_none, false, false);
22286
22287 pop_tinst_level ();
22288
22289 if (spec_args == error_mark_node
22290 /* We only need to check the innermost arguments; the other
22291 arguments will always agree. */
22292 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22293 INNERMOST_TEMPLATE_ARGS (args)))
22294 return NULL_TREE;
22295
22296 /* Now that we have bindings for all of the template arguments,
22297 ensure that the arguments deduced for the template template
22298 parameters have compatible template parameter lists. See the use
22299 of template_template_parm_bindings_ok_p in fn_type_unification
22300 for more information. */
22301 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22302 return NULL_TREE;
22303
22304 return deduced_args;
22305 }
22306
22307 // Compare two function templates T1 and T2 by deducing bindings
22308 // from one against the other. If both deductions succeed, compare
22309 // constraints to see which is more constrained.
22310 static int
22311 more_specialized_inst (tree t1, tree t2)
22312 {
22313 int fate = 0;
22314 int count = 0;
22315
22316 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22317 {
22318 --fate;
22319 ++count;
22320 }
22321
22322 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22323 {
22324 ++fate;
22325 ++count;
22326 }
22327
22328 // If both deductions succeed, then one may be more constrained.
22329 if (count == 2 && fate == 0)
22330 fate = more_constrained (t1, t2);
22331
22332 return fate;
22333 }
22334
22335 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22336 Return the TREE_LIST node with the most specialized template, if
22337 any. If there is no most specialized template, the error_mark_node
22338 is returned.
22339
22340 Note that this function does not look at, or modify, the
22341 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22342 returned is one of the elements of INSTANTIATIONS, callers may
22343 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22344 and retrieve it from the value returned. */
22345
22346 tree
22347 most_specialized_instantiation (tree templates)
22348 {
22349 tree fn, champ;
22350
22351 ++processing_template_decl;
22352
22353 champ = templates;
22354 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22355 {
22356 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22357 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22358 if (fate == -1)
22359 champ = fn;
22360 else if (!fate)
22361 {
22362 /* Equally specialized, move to next function. If there
22363 is no next function, nothing's most specialized. */
22364 fn = TREE_CHAIN (fn);
22365 champ = fn;
22366 if (!fn)
22367 break;
22368 }
22369 }
22370
22371 if (champ)
22372 /* Now verify that champ is better than everything earlier in the
22373 instantiation list. */
22374 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22375 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22376 {
22377 champ = NULL_TREE;
22378 break;
22379 }
22380 }
22381
22382 processing_template_decl--;
22383
22384 if (!champ)
22385 return error_mark_node;
22386
22387 return champ;
22388 }
22389
22390 /* If DECL is a specialization of some template, return the most
22391 general such template. Otherwise, returns NULL_TREE.
22392
22393 For example, given:
22394
22395 template <class T> struct S { template <class U> void f(U); };
22396
22397 if TMPL is `template <class U> void S<int>::f(U)' this will return
22398 the full template. This function will not trace past partial
22399 specializations, however. For example, given in addition:
22400
22401 template <class T> struct S<T*> { template <class U> void f(U); };
22402
22403 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22404 `template <class T> template <class U> S<T*>::f(U)'. */
22405
22406 tree
22407 most_general_template (tree decl)
22408 {
22409 if (TREE_CODE (decl) != TEMPLATE_DECL)
22410 {
22411 if (tree tinfo = get_template_info (decl))
22412 decl = TI_TEMPLATE (tinfo);
22413 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22414 template friend, or a FIELD_DECL for a capture pack. */
22415 if (TREE_CODE (decl) != TEMPLATE_DECL)
22416 return NULL_TREE;
22417 }
22418
22419 /* Look for more and more general templates. */
22420 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22421 {
22422 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22423 (See cp-tree.h for details.) */
22424 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22425 break;
22426
22427 if (CLASS_TYPE_P (TREE_TYPE (decl))
22428 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22429 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22430 break;
22431
22432 /* Stop if we run into an explicitly specialized class template. */
22433 if (!DECL_NAMESPACE_SCOPE_P (decl)
22434 && DECL_CONTEXT (decl)
22435 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22436 break;
22437
22438 decl = DECL_TI_TEMPLATE (decl);
22439 }
22440
22441 return decl;
22442 }
22443
22444 /* Return the most specialized of the template partial specializations
22445 which can produce TARGET, a specialization of some class or variable
22446 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22447 a TEMPLATE_DECL node corresponding to the partial specialization, while
22448 the TREE_PURPOSE is the set of template arguments that must be
22449 substituted into the template pattern in order to generate TARGET.
22450
22451 If the choice of partial specialization is ambiguous, a diagnostic
22452 is issued, and the error_mark_node is returned. If there are no
22453 partial specializations matching TARGET, then NULL_TREE is
22454 returned, indicating that the primary template should be used. */
22455
22456 static tree
22457 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22458 {
22459 tree list = NULL_TREE;
22460 tree t;
22461 tree champ;
22462 int fate;
22463 bool ambiguous_p;
22464 tree outer_args = NULL_TREE;
22465 tree tmpl, args;
22466
22467 if (TYPE_P (target))
22468 {
22469 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22470 tmpl = TI_TEMPLATE (tinfo);
22471 args = TI_ARGS (tinfo);
22472 }
22473 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22474 {
22475 tmpl = TREE_OPERAND (target, 0);
22476 args = TREE_OPERAND (target, 1);
22477 }
22478 else if (VAR_P (target))
22479 {
22480 tree tinfo = DECL_TEMPLATE_INFO (target);
22481 tmpl = TI_TEMPLATE (tinfo);
22482 args = TI_ARGS (tinfo);
22483 }
22484 else
22485 gcc_unreachable ();
22486
22487 tree main_tmpl = most_general_template (tmpl);
22488
22489 /* For determining which partial specialization to use, only the
22490 innermost args are interesting. */
22491 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22492 {
22493 outer_args = strip_innermost_template_args (args, 1);
22494 args = INNERMOST_TEMPLATE_ARGS (args);
22495 }
22496
22497 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22498 {
22499 tree spec_args;
22500 tree spec_tmpl = TREE_VALUE (t);
22501
22502 if (outer_args)
22503 {
22504 /* Substitute in the template args from the enclosing class. */
22505 ++processing_template_decl;
22506 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22507 --processing_template_decl;
22508 }
22509
22510 if (spec_tmpl == error_mark_node)
22511 return error_mark_node;
22512
22513 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22514 if (spec_args)
22515 {
22516 if (outer_args)
22517 spec_args = add_to_template_args (outer_args, spec_args);
22518
22519 /* Keep the candidate only if the constraints are satisfied,
22520 or if we're not compiling with concepts. */
22521 if (!flag_concepts
22522 || constraints_satisfied_p (spec_tmpl, spec_args))
22523 {
22524 list = tree_cons (spec_args, TREE_VALUE (t), list);
22525 TREE_TYPE (list) = TREE_TYPE (t);
22526 }
22527 }
22528 }
22529
22530 if (! list)
22531 return NULL_TREE;
22532
22533 ambiguous_p = false;
22534 t = list;
22535 champ = t;
22536 t = TREE_CHAIN (t);
22537 for (; t; t = TREE_CHAIN (t))
22538 {
22539 fate = more_specialized_partial_spec (tmpl, champ, t);
22540 if (fate == 1)
22541 ;
22542 else
22543 {
22544 if (fate == 0)
22545 {
22546 t = TREE_CHAIN (t);
22547 if (! t)
22548 {
22549 ambiguous_p = true;
22550 break;
22551 }
22552 }
22553 champ = t;
22554 }
22555 }
22556
22557 if (!ambiguous_p)
22558 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22559 {
22560 fate = more_specialized_partial_spec (tmpl, champ, t);
22561 if (fate != 1)
22562 {
22563 ambiguous_p = true;
22564 break;
22565 }
22566 }
22567
22568 if (ambiguous_p)
22569 {
22570 const char *str;
22571 char *spaces = NULL;
22572 if (!(complain & tf_error))
22573 return error_mark_node;
22574 if (TYPE_P (target))
22575 error ("ambiguous template instantiation for %q#T", target);
22576 else
22577 error ("ambiguous template instantiation for %q#D", target);
22578 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22579 for (t = list; t; t = TREE_CHAIN (t))
22580 {
22581 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22582 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22583 "%s %#qS", spaces ? spaces : str, subst);
22584 spaces = spaces ? spaces : get_spaces (str);
22585 }
22586 free (spaces);
22587 return error_mark_node;
22588 }
22589
22590 return champ;
22591 }
22592
22593 /* Explicitly instantiate DECL. */
22594
22595 void
22596 do_decl_instantiation (tree decl, tree storage)
22597 {
22598 tree result = NULL_TREE;
22599 int extern_p = 0;
22600
22601 if (!decl || decl == error_mark_node)
22602 /* An error occurred, for which grokdeclarator has already issued
22603 an appropriate message. */
22604 return;
22605 else if (! DECL_LANG_SPECIFIC (decl))
22606 {
22607 error ("explicit instantiation of non-template %q#D", decl);
22608 return;
22609 }
22610
22611 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22612 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22613
22614 if (VAR_P (decl) && !var_templ)
22615 {
22616 /* There is an asymmetry here in the way VAR_DECLs and
22617 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22618 the latter, the DECL we get back will be marked as a
22619 template instantiation, and the appropriate
22620 DECL_TEMPLATE_INFO will be set up. This does not happen for
22621 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22622 should handle VAR_DECLs as it currently handles
22623 FUNCTION_DECLs. */
22624 if (!DECL_CLASS_SCOPE_P (decl))
22625 {
22626 error ("%qD is not a static data member of a class template", decl);
22627 return;
22628 }
22629 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22630 if (!result || !VAR_P (result))
22631 {
22632 error ("no matching template for %qD found", decl);
22633 return;
22634 }
22635 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22636 {
22637 error ("type %qT for explicit instantiation %qD does not match "
22638 "declared type %qT", TREE_TYPE (result), decl,
22639 TREE_TYPE (decl));
22640 return;
22641 }
22642 }
22643 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22644 {
22645 error ("explicit instantiation of %q#D", decl);
22646 return;
22647 }
22648 else
22649 result = decl;
22650
22651 /* Check for various error cases. Note that if the explicit
22652 instantiation is valid the RESULT will currently be marked as an
22653 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22654 until we get here. */
22655
22656 if (DECL_TEMPLATE_SPECIALIZATION (result))
22657 {
22658 /* DR 259 [temp.spec].
22659
22660 Both an explicit instantiation and a declaration of an explicit
22661 specialization shall not appear in a program unless the explicit
22662 instantiation follows a declaration of the explicit specialization.
22663
22664 For a given set of template parameters, if an explicit
22665 instantiation of a template appears after a declaration of an
22666 explicit specialization for that template, the explicit
22667 instantiation has no effect. */
22668 return;
22669 }
22670 else if (DECL_EXPLICIT_INSTANTIATION (result))
22671 {
22672 /* [temp.spec]
22673
22674 No program shall explicitly instantiate any template more
22675 than once.
22676
22677 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22678 the first instantiation was `extern' and the second is not,
22679 and EXTERN_P for the opposite case. */
22680 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22681 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22682 /* If an "extern" explicit instantiation follows an ordinary
22683 explicit instantiation, the template is instantiated. */
22684 if (extern_p)
22685 return;
22686 }
22687 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22688 {
22689 error ("no matching template for %qD found", result);
22690 return;
22691 }
22692 else if (!DECL_TEMPLATE_INFO (result))
22693 {
22694 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22695 return;
22696 }
22697
22698 if (storage == NULL_TREE)
22699 ;
22700 else if (storage == ridpointers[(int) RID_EXTERN])
22701 {
22702 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22703 pedwarn (input_location, OPT_Wpedantic,
22704 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22705 "instantiations");
22706 extern_p = 1;
22707 }
22708 else
22709 error ("storage class %qD applied to template instantiation", storage);
22710
22711 check_explicit_instantiation_namespace (result);
22712 mark_decl_instantiated (result, extern_p);
22713 if (! extern_p)
22714 instantiate_decl (result, /*defer_ok=*/true,
22715 /*expl_inst_class_mem_p=*/false);
22716 }
22717
22718 static void
22719 mark_class_instantiated (tree t, int extern_p)
22720 {
22721 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22722 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22723 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22724 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22725 if (! extern_p)
22726 {
22727 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22728 rest_of_type_compilation (t, 1);
22729 }
22730 }
22731
22732 /* Called from do_type_instantiation through binding_table_foreach to
22733 do recursive instantiation for the type bound in ENTRY. */
22734 static void
22735 bt_instantiate_type_proc (binding_entry entry, void *data)
22736 {
22737 tree storage = *(tree *) data;
22738
22739 if (MAYBE_CLASS_TYPE_P (entry->type)
22740 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22741 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22742 }
22743
22744 /* Perform an explicit instantiation of template class T. STORAGE, if
22745 non-null, is the RID for extern, inline or static. COMPLAIN is
22746 nonzero if this is called from the parser, zero if called recursively,
22747 since the standard is unclear (as detailed below). */
22748
22749 void
22750 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22751 {
22752 int extern_p = 0;
22753 int nomem_p = 0;
22754 int static_p = 0;
22755 int previous_instantiation_extern_p = 0;
22756
22757 if (TREE_CODE (t) == TYPE_DECL)
22758 t = TREE_TYPE (t);
22759
22760 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22761 {
22762 tree tmpl =
22763 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22764 if (tmpl)
22765 error ("explicit instantiation of non-class template %qD", tmpl);
22766 else
22767 error ("explicit instantiation of non-template type %qT", t);
22768 return;
22769 }
22770
22771 complete_type (t);
22772
22773 if (!COMPLETE_TYPE_P (t))
22774 {
22775 if (complain & tf_error)
22776 error ("explicit instantiation of %q#T before definition of template",
22777 t);
22778 return;
22779 }
22780
22781 if (storage != NULL_TREE)
22782 {
22783 if (!in_system_header_at (input_location))
22784 {
22785 if (storage == ridpointers[(int) RID_EXTERN])
22786 {
22787 if (cxx_dialect == cxx98)
22788 pedwarn (input_location, OPT_Wpedantic,
22789 "ISO C++ 1998 forbids the use of %<extern%> on "
22790 "explicit instantiations");
22791 }
22792 else
22793 pedwarn (input_location, OPT_Wpedantic,
22794 "ISO C++ forbids the use of %qE"
22795 " on explicit instantiations", storage);
22796 }
22797
22798 if (storage == ridpointers[(int) RID_INLINE])
22799 nomem_p = 1;
22800 else if (storage == ridpointers[(int) RID_EXTERN])
22801 extern_p = 1;
22802 else if (storage == ridpointers[(int) RID_STATIC])
22803 static_p = 1;
22804 else
22805 {
22806 error ("storage class %qD applied to template instantiation",
22807 storage);
22808 extern_p = 0;
22809 }
22810 }
22811
22812 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22813 {
22814 /* DR 259 [temp.spec].
22815
22816 Both an explicit instantiation and a declaration of an explicit
22817 specialization shall not appear in a program unless the explicit
22818 instantiation follows a declaration of the explicit specialization.
22819
22820 For a given set of template parameters, if an explicit
22821 instantiation of a template appears after a declaration of an
22822 explicit specialization for that template, the explicit
22823 instantiation has no effect. */
22824 return;
22825 }
22826 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22827 {
22828 /* [temp.spec]
22829
22830 No program shall explicitly instantiate any template more
22831 than once.
22832
22833 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22834 instantiation was `extern'. If EXTERN_P then the second is.
22835 These cases are OK. */
22836 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22837
22838 if (!previous_instantiation_extern_p && !extern_p
22839 && (complain & tf_error))
22840 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22841
22842 /* If we've already instantiated the template, just return now. */
22843 if (!CLASSTYPE_INTERFACE_ONLY (t))
22844 return;
22845 }
22846
22847 check_explicit_instantiation_namespace (TYPE_NAME (t));
22848 mark_class_instantiated (t, extern_p);
22849
22850 if (nomem_p)
22851 return;
22852
22853 /* In contrast to implicit instantiation, where only the
22854 declarations, and not the definitions, of members are
22855 instantiated, we have here:
22856
22857 [temp.explicit]
22858
22859 The explicit instantiation of a class template specialization
22860 implies the instantiation of all of its members not
22861 previously explicitly specialized in the translation unit
22862 containing the explicit instantiation.
22863
22864 Of course, we can't instantiate member template classes, since we
22865 don't have any arguments for them. Note that the standard is
22866 unclear on whether the instantiation of the members are
22867 *explicit* instantiations or not. However, the most natural
22868 interpretation is that it should be an explicit
22869 instantiation. */
22870 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22871 if ((VAR_P (fld)
22872 || (TREE_CODE (fld) == FUNCTION_DECL
22873 && !static_p
22874 && user_provided_p (fld)))
22875 && DECL_TEMPLATE_INSTANTIATION (fld))
22876 {
22877 mark_decl_instantiated (fld, extern_p);
22878 if (! extern_p)
22879 instantiate_decl (fld, /*defer_ok=*/true,
22880 /*expl_inst_class_mem_p=*/true);
22881 }
22882
22883 if (CLASSTYPE_NESTED_UTDS (t))
22884 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22885 bt_instantiate_type_proc, &storage);
22886 }
22887
22888 /* Given a function DECL, which is a specialization of TMPL, modify
22889 DECL to be a re-instantiation of TMPL with the same template
22890 arguments. TMPL should be the template into which tsubst'ing
22891 should occur for DECL, not the most general template.
22892
22893 One reason for doing this is a scenario like this:
22894
22895 template <class T>
22896 void f(const T&, int i);
22897
22898 void g() { f(3, 7); }
22899
22900 template <class T>
22901 void f(const T& t, const int i) { }
22902
22903 Note that when the template is first instantiated, with
22904 instantiate_template, the resulting DECL will have no name for the
22905 first parameter, and the wrong type for the second. So, when we go
22906 to instantiate the DECL, we regenerate it. */
22907
22908 static void
22909 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22910 {
22911 /* The arguments used to instantiate DECL, from the most general
22912 template. */
22913 tree code_pattern;
22914
22915 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22916
22917 /* Make sure that we can see identifiers, and compute access
22918 correctly. */
22919 push_access_scope (decl);
22920
22921 if (TREE_CODE (decl) == FUNCTION_DECL)
22922 {
22923 tree decl_parm;
22924 tree pattern_parm;
22925 tree specs;
22926 int args_depth;
22927 int parms_depth;
22928
22929 args_depth = TMPL_ARGS_DEPTH (args);
22930 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22931 if (args_depth > parms_depth)
22932 args = get_innermost_template_args (args, parms_depth);
22933
22934 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22935 args, tf_error, NULL_TREE,
22936 /*defer_ok*/false);
22937 if (specs && specs != error_mark_node)
22938 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22939 specs);
22940
22941 /* Merge parameter declarations. */
22942 decl_parm = skip_artificial_parms_for (decl,
22943 DECL_ARGUMENTS (decl));
22944 pattern_parm
22945 = skip_artificial_parms_for (code_pattern,
22946 DECL_ARGUMENTS (code_pattern));
22947 while (decl_parm && !DECL_PACK_P (pattern_parm))
22948 {
22949 tree parm_type;
22950 tree attributes;
22951
22952 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22953 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22954 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22955 NULL_TREE);
22956 parm_type = type_decays_to (parm_type);
22957 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22958 TREE_TYPE (decl_parm) = parm_type;
22959 attributes = DECL_ATTRIBUTES (pattern_parm);
22960 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22961 {
22962 DECL_ATTRIBUTES (decl_parm) = attributes;
22963 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22964 }
22965 decl_parm = DECL_CHAIN (decl_parm);
22966 pattern_parm = DECL_CHAIN (pattern_parm);
22967 }
22968 /* Merge any parameters that match with the function parameter
22969 pack. */
22970 if (pattern_parm && DECL_PACK_P (pattern_parm))
22971 {
22972 int i, len;
22973 tree expanded_types;
22974 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22975 the parameters in this function parameter pack. */
22976 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22977 args, tf_error, NULL_TREE);
22978 len = TREE_VEC_LENGTH (expanded_types);
22979 for (i = 0; i < len; i++)
22980 {
22981 tree parm_type;
22982 tree attributes;
22983
22984 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22985 /* Rename the parameter to include the index. */
22986 DECL_NAME (decl_parm) =
22987 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22988 parm_type = TREE_VEC_ELT (expanded_types, i);
22989 parm_type = type_decays_to (parm_type);
22990 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22991 TREE_TYPE (decl_parm) = parm_type;
22992 attributes = DECL_ATTRIBUTES (pattern_parm);
22993 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22994 {
22995 DECL_ATTRIBUTES (decl_parm) = attributes;
22996 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22997 }
22998 decl_parm = DECL_CHAIN (decl_parm);
22999 }
23000 }
23001 /* Merge additional specifiers from the CODE_PATTERN. */
23002 if (DECL_DECLARED_INLINE_P (code_pattern)
23003 && !DECL_DECLARED_INLINE_P (decl))
23004 DECL_DECLARED_INLINE_P (decl) = 1;
23005 }
23006 else if (VAR_P (decl))
23007 {
23008 start_lambda_scope (decl);
23009 DECL_INITIAL (decl) =
23010 tsubst_expr (DECL_INITIAL (code_pattern), args,
23011 tf_error, DECL_TI_TEMPLATE (decl),
23012 /*integral_constant_expression_p=*/false);
23013 finish_lambda_scope ();
23014 if (VAR_HAD_UNKNOWN_BOUND (decl))
23015 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
23016 tf_error, DECL_TI_TEMPLATE (decl));
23017 }
23018 else
23019 gcc_unreachable ();
23020
23021 pop_access_scope (decl);
23022 }
23023
23024 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
23025 substituted to get DECL. */
23026
23027 tree
23028 template_for_substitution (tree decl)
23029 {
23030 tree tmpl = DECL_TI_TEMPLATE (decl);
23031
23032 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
23033 for the instantiation. This is not always the most general
23034 template. Consider, for example:
23035
23036 template <class T>
23037 struct S { template <class U> void f();
23038 template <> void f<int>(); };
23039
23040 and an instantiation of S<double>::f<int>. We want TD to be the
23041 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23042 while (/* An instantiation cannot have a definition, so we need a
23043 more general template. */
23044 DECL_TEMPLATE_INSTANTIATION (tmpl)
23045 /* We must also deal with friend templates. Given:
23046
23047 template <class T> struct S {
23048 template <class U> friend void f() {};
23049 };
23050
23051 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23052 so far as the language is concerned, but that's still
23053 where we get the pattern for the instantiation from. On
23054 other hand, if the definition comes outside the class, say:
23055
23056 template <class T> struct S {
23057 template <class U> friend void f();
23058 };
23059 template <class U> friend void f() {}
23060
23061 we don't need to look any further. That's what the check for
23062 DECL_INITIAL is for. */
23063 || (TREE_CODE (decl) == FUNCTION_DECL
23064 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
23065 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
23066 {
23067 /* The present template, TD, should not be a definition. If it
23068 were a definition, we should be using it! Note that we
23069 cannot restructure the loop to just keep going until we find
23070 a template with a definition, since that might go too far if
23071 a specialization was declared, but not defined. */
23072
23073 /* Fetch the more general template. */
23074 tmpl = DECL_TI_TEMPLATE (tmpl);
23075 }
23076
23077 return tmpl;
23078 }
23079
23080 /* Returns true if we need to instantiate this template instance even if we
23081 know we aren't going to emit it. */
23082
23083 bool
23084 always_instantiate_p (tree decl)
23085 {
23086 /* We always instantiate inline functions so that we can inline them. An
23087 explicit instantiation declaration prohibits implicit instantiation of
23088 non-inline functions. With high levels of optimization, we would
23089 normally inline non-inline functions -- but we're not allowed to do
23090 that for "extern template" functions. Therefore, we check
23091 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23092 return ((TREE_CODE (decl) == FUNCTION_DECL
23093 && (DECL_DECLARED_INLINE_P (decl)
23094 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
23095 /* And we need to instantiate static data members so that
23096 their initializers are available in integral constant
23097 expressions. */
23098 || (VAR_P (decl)
23099 && decl_maybe_constant_var_p (decl)));
23100 }
23101
23102 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23103 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23104 error, true otherwise. */
23105
23106 bool
23107 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
23108 {
23109 tree fntype, spec, noex, clone;
23110
23111 /* Don't instantiate a noexcept-specification from template context. */
23112 if (processing_template_decl)
23113 return true;
23114
23115 if (DECL_CLONED_FUNCTION_P (fn))
23116 fn = DECL_CLONED_FUNCTION (fn);
23117 fntype = TREE_TYPE (fn);
23118 spec = TYPE_RAISES_EXCEPTIONS (fntype);
23119
23120 if (!spec || !TREE_PURPOSE (spec))
23121 return true;
23122
23123 noex = TREE_PURPOSE (spec);
23124
23125 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
23126 {
23127 static hash_set<tree>* fns = new hash_set<tree>;
23128 bool added = false;
23129 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
23130 spec = get_defaulted_eh_spec (fn, complain);
23131 else if (!(added = !fns->add (fn)))
23132 {
23133 /* If hash_set::add returns true, the element was already there. */
23134 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
23135 DECL_SOURCE_LOCATION (fn));
23136 error_at (loc,
23137 "exception specification of %qD depends on itself",
23138 fn);
23139 spec = noexcept_false_spec;
23140 }
23141 else if (push_tinst_level (fn))
23142 {
23143 push_access_scope (fn);
23144 push_deferring_access_checks (dk_no_deferred);
23145 input_location = DECL_SOURCE_LOCATION (fn);
23146 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
23147 DEFERRED_NOEXCEPT_ARGS (noex),
23148 tf_warning_or_error, fn,
23149 /*function_p=*/false,
23150 /*integral_constant_expression_p=*/true);
23151 pop_deferring_access_checks ();
23152 pop_access_scope (fn);
23153 pop_tinst_level ();
23154 spec = build_noexcept_spec (noex, tf_warning_or_error);
23155 if (spec == error_mark_node)
23156 spec = noexcept_false_spec;
23157 }
23158 else
23159 spec = noexcept_false_spec;
23160
23161 if (added)
23162 fns->remove (fn);
23163
23164 if (spec == error_mark_node)
23165 return false;
23166
23167 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
23168 }
23169
23170 FOR_EACH_CLONE (clone, fn)
23171 {
23172 if (TREE_TYPE (clone) == fntype)
23173 TREE_TYPE (clone) = TREE_TYPE (fn);
23174 else
23175 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
23176 }
23177
23178 return true;
23179 }
23180
23181 /* We're starting to process the function INST, an instantiation of PATTERN;
23182 add their parameters to local_specializations. */
23183
23184 static void
23185 register_parameter_specializations (tree pattern, tree inst)
23186 {
23187 tree tmpl_parm = DECL_ARGUMENTS (pattern);
23188 tree spec_parm = DECL_ARGUMENTS (inst);
23189 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
23190 {
23191 register_local_specialization (spec_parm, tmpl_parm);
23192 spec_parm = skip_artificial_parms_for (inst, spec_parm);
23193 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
23194 }
23195 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23196 {
23197 if (!DECL_PACK_P (tmpl_parm))
23198 {
23199 register_local_specialization (spec_parm, tmpl_parm);
23200 spec_parm = DECL_CHAIN (spec_parm);
23201 }
23202 else
23203 {
23204 /* Register the (value) argument pack as a specialization of
23205 TMPL_PARM, then move on. */
23206 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23207 register_local_specialization (argpack, tmpl_parm);
23208 }
23209 }
23210 gcc_assert (!spec_parm);
23211 }
23212
23213 /* Produce the definition of D, a _DECL generated from a template. If
23214 DEFER_OK is true, then we don't have to actually do the
23215 instantiation now; we just have to do it sometime. Normally it is
23216 an error if this is an explicit instantiation but D is undefined.
23217 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23218 instantiated class template. */
23219
23220 tree
23221 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
23222 {
23223 tree tmpl = DECL_TI_TEMPLATE (d);
23224 tree gen_args;
23225 tree args;
23226 tree td;
23227 tree code_pattern;
23228 tree spec;
23229 tree gen_tmpl;
23230 bool pattern_defined;
23231 location_t saved_loc = input_location;
23232 int saved_unevaluated_operand = cp_unevaluated_operand;
23233 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23234 bool external_p;
23235 bool deleted_p;
23236
23237 /* This function should only be used to instantiate templates for
23238 functions and static member variables. */
23239 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23240
23241 /* A concept is never instantiated. */
23242 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23243
23244 /* Variables are never deferred; if instantiation is required, they
23245 are instantiated right away. That allows for better code in the
23246 case that an expression refers to the value of the variable --
23247 if the variable has a constant value the referring expression can
23248 take advantage of that fact. */
23249 if (VAR_P (d))
23250 defer_ok = false;
23251
23252 /* Don't instantiate cloned functions. Instead, instantiate the
23253 functions they cloned. */
23254 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23255 d = DECL_CLONED_FUNCTION (d);
23256
23257 if (DECL_TEMPLATE_INSTANTIATED (d)
23258 || (TREE_CODE (d) == FUNCTION_DECL
23259 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23260 || DECL_TEMPLATE_SPECIALIZATION (d))
23261 /* D has already been instantiated or explicitly specialized, so
23262 there's nothing for us to do here.
23263
23264 It might seem reasonable to check whether or not D is an explicit
23265 instantiation, and, if so, stop here. But when an explicit
23266 instantiation is deferred until the end of the compilation,
23267 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23268 the instantiation. */
23269 return d;
23270
23271 /* Check to see whether we know that this template will be
23272 instantiated in some other file, as with "extern template"
23273 extension. */
23274 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23275
23276 /* In general, we do not instantiate such templates. */
23277 if (external_p && !always_instantiate_p (d))
23278 return d;
23279
23280 gen_tmpl = most_general_template (tmpl);
23281 gen_args = DECL_TI_ARGS (d);
23282
23283 if (tmpl != gen_tmpl)
23284 /* We should already have the extra args. */
23285 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23286 == TMPL_ARGS_DEPTH (gen_args));
23287 /* And what's in the hash table should match D. */
23288 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23289 || spec == NULL_TREE);
23290
23291 /* This needs to happen before any tsubsting. */
23292 if (! push_tinst_level (d))
23293 return d;
23294
23295 timevar_push (TV_TEMPLATE_INST);
23296
23297 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23298 for the instantiation. */
23299 td = template_for_substitution (d);
23300 args = gen_args;
23301
23302 if (VAR_P (d))
23303 {
23304 /* Look up an explicit specialization, if any. */
23305 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23306 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23307 if (elt && elt != error_mark_node)
23308 {
23309 td = TREE_VALUE (elt);
23310 args = TREE_PURPOSE (elt);
23311 }
23312 }
23313
23314 code_pattern = DECL_TEMPLATE_RESULT (td);
23315
23316 /* We should never be trying to instantiate a member of a class
23317 template or partial specialization. */
23318 gcc_assert (d != code_pattern);
23319
23320 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23321 || DECL_TEMPLATE_SPECIALIZATION (td))
23322 /* In the case of a friend template whose definition is provided
23323 outside the class, we may have too many arguments. Drop the
23324 ones we don't need. The same is true for specializations. */
23325 args = get_innermost_template_args
23326 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23327
23328 if (TREE_CODE (d) == FUNCTION_DECL)
23329 {
23330 deleted_p = DECL_DELETED_FN (code_pattern);
23331 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23332 && DECL_INITIAL (code_pattern) != error_mark_node)
23333 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23334 || deleted_p);
23335 }
23336 else
23337 {
23338 deleted_p = false;
23339 if (DECL_CLASS_SCOPE_P (code_pattern))
23340 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23341 || DECL_INLINE_VAR_P (code_pattern));
23342 else
23343 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23344 }
23345
23346 /* We may be in the middle of deferred access check. Disable it now. */
23347 push_deferring_access_checks (dk_no_deferred);
23348
23349 /* Unless an explicit instantiation directive has already determined
23350 the linkage of D, remember that a definition is available for
23351 this entity. */
23352 if (pattern_defined
23353 && !DECL_INTERFACE_KNOWN (d)
23354 && !DECL_NOT_REALLY_EXTERN (d))
23355 mark_definable (d);
23356
23357 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23358 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23359 input_location = DECL_SOURCE_LOCATION (d);
23360
23361 /* If D is a member of an explicitly instantiated class template,
23362 and no definition is available, treat it like an implicit
23363 instantiation. */
23364 if (!pattern_defined && expl_inst_class_mem_p
23365 && DECL_EXPLICIT_INSTANTIATION (d))
23366 {
23367 /* Leave linkage flags alone on instantiations with anonymous
23368 visibility. */
23369 if (TREE_PUBLIC (d))
23370 {
23371 DECL_NOT_REALLY_EXTERN (d) = 0;
23372 DECL_INTERFACE_KNOWN (d) = 0;
23373 }
23374 SET_DECL_IMPLICIT_INSTANTIATION (d);
23375 }
23376
23377 /* Defer all other templates, unless we have been explicitly
23378 forbidden from doing so. */
23379 if (/* If there is no definition, we cannot instantiate the
23380 template. */
23381 ! pattern_defined
23382 /* If it's OK to postpone instantiation, do so. */
23383 || defer_ok
23384 /* If this is a static data member that will be defined
23385 elsewhere, we don't want to instantiate the entire data
23386 member, but we do want to instantiate the initializer so that
23387 we can substitute that elsewhere. */
23388 || (external_p && VAR_P (d))
23389 /* Handle here a deleted function too, avoid generating
23390 its body (c++/61080). */
23391 || deleted_p)
23392 {
23393 /* The definition of the static data member is now required so
23394 we must substitute the initializer. */
23395 if (VAR_P (d)
23396 && !DECL_INITIAL (d)
23397 && DECL_INITIAL (code_pattern))
23398 {
23399 tree ns;
23400 tree init;
23401 bool const_init = false;
23402 bool enter_context = DECL_CLASS_SCOPE_P (d);
23403
23404 ns = decl_namespace_context (d);
23405 push_nested_namespace (ns);
23406 if (enter_context)
23407 push_nested_class (DECL_CONTEXT (d));
23408 init = tsubst_expr (DECL_INITIAL (code_pattern),
23409 args,
23410 tf_warning_or_error, NULL_TREE,
23411 /*integral_constant_expression_p=*/false);
23412 /* If instantiating the initializer involved instantiating this
23413 again, don't call cp_finish_decl twice. */
23414 if (!DECL_INITIAL (d))
23415 {
23416 /* Make sure the initializer is still constant, in case of
23417 circular dependency (template/instantiate6.C). */
23418 const_init
23419 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23420 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23421 /*asmspec_tree=*/NULL_TREE,
23422 LOOKUP_ONLYCONVERTING);
23423 }
23424 if (enter_context)
23425 pop_nested_class ();
23426 pop_nested_namespace (ns);
23427 }
23428
23429 /* We restore the source position here because it's used by
23430 add_pending_template. */
23431 input_location = saved_loc;
23432
23433 if (at_eof && !pattern_defined
23434 && DECL_EXPLICIT_INSTANTIATION (d)
23435 && DECL_NOT_REALLY_EXTERN (d))
23436 /* [temp.explicit]
23437
23438 The definition of a non-exported function template, a
23439 non-exported member function template, or a non-exported
23440 member function or static data member of a class template
23441 shall be present in every translation unit in which it is
23442 explicitly instantiated. */
23443 permerror (input_location, "explicit instantiation of %qD "
23444 "but no definition available", d);
23445
23446 /* If we're in unevaluated context, we just wanted to get the
23447 constant value; this isn't an odr use, so don't queue
23448 a full instantiation. */
23449 if (cp_unevaluated_operand != 0)
23450 goto out;
23451 /* ??? Historically, we have instantiated inline functions, even
23452 when marked as "extern template". */
23453 if (!(external_p && VAR_P (d)))
23454 add_pending_template (d);
23455 goto out;
23456 }
23457 /* Tell the repository that D is available in this translation unit
23458 -- and see if it is supposed to be instantiated here. */
23459 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23460 {
23461 /* In a PCH file, despite the fact that the repository hasn't
23462 requested instantiation in the PCH it is still possible that
23463 an instantiation will be required in a file that includes the
23464 PCH. */
23465 if (pch_file)
23466 add_pending_template (d);
23467 /* Instantiate inline functions so that the inliner can do its
23468 job, even though we'll not be emitting a copy of this
23469 function. */
23470 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23471 goto out;
23472 }
23473
23474 bool push_to_top, nested;
23475 tree fn_context;
23476 fn_context = decl_function_context (d);
23477 if (LAMBDA_FUNCTION_P (d))
23478 /* tsubst_lambda_expr resolved any references to enclosing functions. */
23479 fn_context = NULL_TREE;
23480 nested = current_function_decl != NULL_TREE;
23481 push_to_top = !(nested && fn_context == current_function_decl);
23482
23483 vec<tree> omp_privatization_save;
23484 if (nested)
23485 save_omp_privatization_clauses (omp_privatization_save);
23486
23487 if (push_to_top)
23488 push_to_top_level ();
23489 else
23490 {
23491 push_function_context ();
23492 cp_unevaluated_operand = 0;
23493 c_inhibit_evaluation_warnings = 0;
23494 }
23495
23496 /* Mark D as instantiated so that recursive calls to
23497 instantiate_decl do not try to instantiate it again. */
23498 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23499
23500 /* Regenerate the declaration in case the template has been modified
23501 by a subsequent redeclaration. */
23502 regenerate_decl_from_template (d, td, args);
23503
23504 /* We already set the file and line above. Reset them now in case
23505 they changed as a result of calling regenerate_decl_from_template. */
23506 input_location = DECL_SOURCE_LOCATION (d);
23507
23508 if (VAR_P (d))
23509 {
23510 tree init;
23511 bool const_init = false;
23512
23513 /* Clear out DECL_RTL; whatever was there before may not be right
23514 since we've reset the type of the declaration. */
23515 SET_DECL_RTL (d, NULL);
23516 DECL_IN_AGGR_P (d) = 0;
23517
23518 /* The initializer is placed in DECL_INITIAL by
23519 regenerate_decl_from_template so we don't need to
23520 push/pop_access_scope again here. Pull it out so that
23521 cp_finish_decl can process it. */
23522 init = DECL_INITIAL (d);
23523 DECL_INITIAL (d) = NULL_TREE;
23524 DECL_INITIALIZED_P (d) = 0;
23525
23526 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23527 initializer. That function will defer actual emission until
23528 we have a chance to determine linkage. */
23529 DECL_EXTERNAL (d) = 0;
23530
23531 /* Enter the scope of D so that access-checking works correctly. */
23532 bool enter_context = DECL_CLASS_SCOPE_P (d);
23533 if (enter_context)
23534 push_nested_class (DECL_CONTEXT (d));
23535
23536 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23537 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23538
23539 if (enter_context)
23540 pop_nested_class ();
23541
23542 if (variable_template_p (gen_tmpl))
23543 note_variable_template_instantiation (d);
23544 }
23545 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23546 synthesize_method (d);
23547 else if (TREE_CODE (d) == FUNCTION_DECL)
23548 {
23549 /* Set up the list of local specializations. */
23550 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23551 tree block = NULL_TREE;
23552
23553 /* Set up context. */
23554 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23555 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23556 block = push_stmt_list ();
23557 else
23558 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23559
23560 /* Some typedefs referenced from within the template code need to be
23561 access checked at template instantiation time, i.e now. These
23562 types were added to the template at parsing time. Let's get those
23563 and perform the access checks then. */
23564 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23565 args);
23566
23567 /* Create substitution entries for the parameters. */
23568 register_parameter_specializations (code_pattern, d);
23569
23570 /* Substitute into the body of the function. */
23571 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23572 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23573 tf_warning_or_error, tmpl);
23574 else
23575 {
23576 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23577 tf_warning_or_error, tmpl,
23578 /*integral_constant_expression_p=*/false);
23579
23580 /* Set the current input_location to the end of the function
23581 so that finish_function knows where we are. */
23582 input_location
23583 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23584
23585 /* Remember if we saw an infinite loop in the template. */
23586 current_function_infinite_loop
23587 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23588 }
23589
23590 /* Finish the function. */
23591 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23592 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23593 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23594 else
23595 {
23596 d = finish_function (/*inline_p=*/false);
23597 expand_or_defer_fn (d);
23598 }
23599
23600 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23601 cp_check_omp_declare_reduction (d);
23602 }
23603
23604 /* We're not deferring instantiation any more. */
23605 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23606
23607 if (push_to_top)
23608 pop_from_top_level ();
23609 else
23610 pop_function_context ();
23611
23612 if (nested)
23613 restore_omp_privatization_clauses (omp_privatization_save);
23614
23615 out:
23616 pop_deferring_access_checks ();
23617 timevar_pop (TV_TEMPLATE_INST);
23618 pop_tinst_level ();
23619 input_location = saved_loc;
23620 cp_unevaluated_operand = saved_unevaluated_operand;
23621 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23622
23623 return d;
23624 }
23625
23626 /* Run through the list of templates that we wish we could
23627 instantiate, and instantiate any we can. RETRIES is the
23628 number of times we retry pending template instantiation. */
23629
23630 void
23631 instantiate_pending_templates (int retries)
23632 {
23633 int reconsider;
23634 location_t saved_loc = input_location;
23635
23636 /* Instantiating templates may trigger vtable generation. This in turn
23637 may require further template instantiations. We place a limit here
23638 to avoid infinite loop. */
23639 if (pending_templates && retries >= max_tinst_depth)
23640 {
23641 tree decl = pending_templates->tinst->decl;
23642
23643 fatal_error (input_location,
23644 "template instantiation depth exceeds maximum of %d"
23645 " instantiating %q+D, possibly from virtual table generation"
23646 " (use -ftemplate-depth= to increase the maximum)",
23647 max_tinst_depth, decl);
23648 if (TREE_CODE (decl) == FUNCTION_DECL)
23649 /* Pretend that we defined it. */
23650 DECL_INITIAL (decl) = error_mark_node;
23651 return;
23652 }
23653
23654 do
23655 {
23656 struct pending_template **t = &pending_templates;
23657 struct pending_template *last = NULL;
23658 reconsider = 0;
23659 while (*t)
23660 {
23661 tree instantiation = reopen_tinst_level ((*t)->tinst);
23662 bool complete = false;
23663
23664 if (TYPE_P (instantiation))
23665 {
23666 if (!COMPLETE_TYPE_P (instantiation))
23667 {
23668 instantiate_class_template (instantiation);
23669 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23670 for (tree fld = TYPE_FIELDS (instantiation);
23671 fld; fld = TREE_CHAIN (fld))
23672 if ((VAR_P (fld)
23673 || (TREE_CODE (fld) == FUNCTION_DECL
23674 && !DECL_ARTIFICIAL (fld)))
23675 && DECL_TEMPLATE_INSTANTIATION (fld))
23676 instantiate_decl (fld,
23677 /*defer_ok=*/false,
23678 /*expl_inst_class_mem_p=*/false);
23679
23680 if (COMPLETE_TYPE_P (instantiation))
23681 reconsider = 1;
23682 }
23683
23684 complete = COMPLETE_TYPE_P (instantiation);
23685 }
23686 else
23687 {
23688 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23689 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23690 {
23691 instantiation
23692 = instantiate_decl (instantiation,
23693 /*defer_ok=*/false,
23694 /*expl_inst_class_mem_p=*/false);
23695 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23696 reconsider = 1;
23697 }
23698
23699 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23700 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23701 }
23702
23703 if (complete)
23704 /* If INSTANTIATION has been instantiated, then we don't
23705 need to consider it again in the future. */
23706 *t = (*t)->next;
23707 else
23708 {
23709 last = *t;
23710 t = &(*t)->next;
23711 }
23712 tinst_depth = 0;
23713 current_tinst_level = NULL;
23714 }
23715 last_pending_template = last;
23716 }
23717 while (reconsider);
23718
23719 input_location = saved_loc;
23720 }
23721
23722 /* Substitute ARGVEC into T, which is a list of initializers for
23723 either base class or a non-static data member. The TREE_PURPOSEs
23724 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23725 instantiate_decl. */
23726
23727 static tree
23728 tsubst_initializer_list (tree t, tree argvec)
23729 {
23730 tree inits = NULL_TREE;
23731 tree target_ctor = error_mark_node;
23732
23733 for (; t; t = TREE_CHAIN (t))
23734 {
23735 tree decl;
23736 tree init;
23737 tree expanded_bases = NULL_TREE;
23738 tree expanded_arguments = NULL_TREE;
23739 int i, len = 1;
23740
23741 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23742 {
23743 tree expr;
23744 tree arg;
23745
23746 /* Expand the base class expansion type into separate base
23747 classes. */
23748 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23749 tf_warning_or_error,
23750 NULL_TREE);
23751 if (expanded_bases == error_mark_node)
23752 continue;
23753
23754 /* We'll be building separate TREE_LISTs of arguments for
23755 each base. */
23756 len = TREE_VEC_LENGTH (expanded_bases);
23757 expanded_arguments = make_tree_vec (len);
23758 for (i = 0; i < len; i++)
23759 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23760
23761 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23762 expand each argument in the TREE_VALUE of t. */
23763 expr = make_node (EXPR_PACK_EXPANSION);
23764 PACK_EXPANSION_LOCAL_P (expr) = true;
23765 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23766 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23767
23768 if (TREE_VALUE (t) == void_type_node)
23769 /* VOID_TYPE_NODE is used to indicate
23770 value-initialization. */
23771 {
23772 for (i = 0; i < len; i++)
23773 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23774 }
23775 else
23776 {
23777 /* Substitute parameter packs into each argument in the
23778 TREE_LIST. */
23779 in_base_initializer = 1;
23780 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23781 {
23782 tree expanded_exprs;
23783
23784 /* Expand the argument. */
23785 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23786 expanded_exprs
23787 = tsubst_pack_expansion (expr, argvec,
23788 tf_warning_or_error,
23789 NULL_TREE);
23790 if (expanded_exprs == error_mark_node)
23791 continue;
23792
23793 /* Prepend each of the expanded expressions to the
23794 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23795 for (i = 0; i < len; i++)
23796 {
23797 TREE_VEC_ELT (expanded_arguments, i) =
23798 tree_cons (NULL_TREE,
23799 TREE_VEC_ELT (expanded_exprs, i),
23800 TREE_VEC_ELT (expanded_arguments, i));
23801 }
23802 }
23803 in_base_initializer = 0;
23804
23805 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23806 since we built them backwards. */
23807 for (i = 0; i < len; i++)
23808 {
23809 TREE_VEC_ELT (expanded_arguments, i) =
23810 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23811 }
23812 }
23813 }
23814
23815 for (i = 0; i < len; ++i)
23816 {
23817 if (expanded_bases)
23818 {
23819 decl = TREE_VEC_ELT (expanded_bases, i);
23820 decl = expand_member_init (decl);
23821 init = TREE_VEC_ELT (expanded_arguments, i);
23822 }
23823 else
23824 {
23825 tree tmp;
23826 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23827 tf_warning_or_error, NULL_TREE);
23828
23829 decl = expand_member_init (decl);
23830 if (decl && !DECL_P (decl))
23831 in_base_initializer = 1;
23832
23833 init = TREE_VALUE (t);
23834 tmp = init;
23835 if (init != void_type_node)
23836 init = tsubst_expr (init, argvec,
23837 tf_warning_or_error, NULL_TREE,
23838 /*integral_constant_expression_p=*/false);
23839 if (init == NULL_TREE && tmp != NULL_TREE)
23840 /* If we had an initializer but it instantiated to nothing,
23841 value-initialize the object. This will only occur when
23842 the initializer was a pack expansion where the parameter
23843 packs used in that expansion were of length zero. */
23844 init = void_type_node;
23845 in_base_initializer = 0;
23846 }
23847
23848 if (target_ctor != error_mark_node
23849 && init != error_mark_node)
23850 {
23851 error ("mem-initializer for %qD follows constructor delegation",
23852 decl);
23853 return inits;
23854 }
23855 /* Look for a target constructor. */
23856 if (init != error_mark_node
23857 && decl && CLASS_TYPE_P (decl)
23858 && same_type_p (decl, current_class_type))
23859 {
23860 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
23861 if (inits)
23862 {
23863 error ("constructor delegation follows mem-initializer for %qD",
23864 TREE_PURPOSE (inits));
23865 continue;
23866 }
23867 target_ctor = init;
23868 }
23869
23870 if (decl)
23871 {
23872 init = build_tree_list (decl, init);
23873 TREE_CHAIN (init) = inits;
23874 inits = init;
23875 }
23876 }
23877 }
23878 return inits;
23879 }
23880
23881 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23882
23883 static void
23884 set_current_access_from_decl (tree decl)
23885 {
23886 if (TREE_PRIVATE (decl))
23887 current_access_specifier = access_private_node;
23888 else if (TREE_PROTECTED (decl))
23889 current_access_specifier = access_protected_node;
23890 else
23891 current_access_specifier = access_public_node;
23892 }
23893
23894 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23895 is the instantiation (which should have been created with
23896 start_enum) and ARGS are the template arguments to use. */
23897
23898 static void
23899 tsubst_enum (tree tag, tree newtag, tree args)
23900 {
23901 tree e;
23902
23903 if (SCOPED_ENUM_P (newtag))
23904 begin_scope (sk_scoped_enum, newtag);
23905
23906 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23907 {
23908 tree value;
23909 tree decl;
23910
23911 decl = TREE_VALUE (e);
23912 /* Note that in a template enum, the TREE_VALUE is the
23913 CONST_DECL, not the corresponding INTEGER_CST. */
23914 value = tsubst_expr (DECL_INITIAL (decl),
23915 args, tf_warning_or_error, NULL_TREE,
23916 /*integral_constant_expression_p=*/true);
23917
23918 /* Give this enumeration constant the correct access. */
23919 set_current_access_from_decl (decl);
23920
23921 /* Actually build the enumerator itself. Here we're assuming that
23922 enumerators can't have dependent attributes. */
23923 build_enumerator (DECL_NAME (decl), value, newtag,
23924 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23925 }
23926
23927 if (SCOPED_ENUM_P (newtag))
23928 finish_scope ();
23929
23930 finish_enum_value_list (newtag);
23931 finish_enum (newtag);
23932
23933 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23934 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23935 }
23936
23937 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23938 its type -- but without substituting the innermost set of template
23939 arguments. So, innermost set of template parameters will appear in
23940 the type. */
23941
23942 tree
23943 get_mostly_instantiated_function_type (tree decl)
23944 {
23945 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23946 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23947 }
23948
23949 /* Return truthvalue if we're processing a template different from
23950 the last one involved in diagnostics. */
23951 bool
23952 problematic_instantiation_changed (void)
23953 {
23954 return current_tinst_level != last_error_tinst_level;
23955 }
23956
23957 /* Remember current template involved in diagnostics. */
23958 void
23959 record_last_problematic_instantiation (void)
23960 {
23961 last_error_tinst_level = current_tinst_level;
23962 }
23963
23964 struct tinst_level *
23965 current_instantiation (void)
23966 {
23967 return current_tinst_level;
23968 }
23969
23970 /* Return TRUE if current_function_decl is being instantiated, false
23971 otherwise. */
23972
23973 bool
23974 instantiating_current_function_p (void)
23975 {
23976 return (current_instantiation ()
23977 && current_instantiation ()->decl == current_function_decl);
23978 }
23979
23980 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23981 type. Return false for ok, true for disallowed. Issue error and
23982 inform messages under control of COMPLAIN. */
23983
23984 static bool
23985 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23986 {
23987 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23988 return false;
23989 else if (TYPE_PTR_P (type))
23990 return false;
23991 else if (TREE_CODE (type) == REFERENCE_TYPE
23992 && !TYPE_REF_IS_RVALUE (type))
23993 return false;
23994 else if (TYPE_PTRMEM_P (type))
23995 return false;
23996 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23997 return false;
23998 else if (TREE_CODE (type) == TYPENAME_TYPE)
23999 return false;
24000 else if (TREE_CODE (type) == DECLTYPE_TYPE)
24001 return false;
24002 else if (TREE_CODE (type) == NULLPTR_TYPE)
24003 return false;
24004 /* A bound template template parm could later be instantiated to have a valid
24005 nontype parm type via an alias template. */
24006 else if (cxx_dialect >= cxx11
24007 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24008 return false;
24009
24010 if (complain & tf_error)
24011 {
24012 if (type == error_mark_node)
24013 inform (input_location, "invalid template non-type parameter");
24014 else
24015 error ("%q#T is not a valid type for a template non-type parameter",
24016 type);
24017 }
24018 return true;
24019 }
24020
24021 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
24022 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
24023
24024 static bool
24025 dependent_type_p_r (tree type)
24026 {
24027 tree scope;
24028
24029 /* [temp.dep.type]
24030
24031 A type is dependent if it is:
24032
24033 -- a template parameter. Template template parameters are types
24034 for us (since TYPE_P holds true for them) so we handle
24035 them here. */
24036 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
24037 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
24038 return true;
24039 /* -- a qualified-id with a nested-name-specifier which contains a
24040 class-name that names a dependent type or whose unqualified-id
24041 names a dependent type. */
24042 if (TREE_CODE (type) == TYPENAME_TYPE)
24043 return true;
24044
24045 /* An alias template specialization can be dependent even if the
24046 resulting type is not. */
24047 if (dependent_alias_template_spec_p (type))
24048 return true;
24049
24050 /* -- a cv-qualified type where the cv-unqualified type is
24051 dependent.
24052 No code is necessary for this bullet; the code below handles
24053 cv-qualified types, and we don't want to strip aliases with
24054 TYPE_MAIN_VARIANT because of DR 1558. */
24055 /* -- a compound type constructed from any dependent type. */
24056 if (TYPE_PTRMEM_P (type))
24057 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
24058 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24059 (type)));
24060 else if (TYPE_PTR_P (type)
24061 || TREE_CODE (type) == REFERENCE_TYPE)
24062 return dependent_type_p (TREE_TYPE (type));
24063 else if (TREE_CODE (type) == FUNCTION_TYPE
24064 || TREE_CODE (type) == METHOD_TYPE)
24065 {
24066 tree arg_type;
24067
24068 if (dependent_type_p (TREE_TYPE (type)))
24069 return true;
24070 for (arg_type = TYPE_ARG_TYPES (type);
24071 arg_type;
24072 arg_type = TREE_CHAIN (arg_type))
24073 if (dependent_type_p (TREE_VALUE (arg_type)))
24074 return true;
24075 if (cxx_dialect >= cxx17)
24076 /* A value-dependent noexcept-specifier makes the type dependent. */
24077 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
24078 if (tree noex = TREE_PURPOSE (spec))
24079 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24080 affect overload resolution and treating it as dependent breaks
24081 things. */
24082 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
24083 && value_dependent_expression_p (noex))
24084 return true;
24085 return false;
24086 }
24087 /* -- an array type constructed from any dependent type or whose
24088 size is specified by a constant expression that is
24089 value-dependent.
24090
24091 We checked for type- and value-dependence of the bounds in
24092 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24093 if (TREE_CODE (type) == ARRAY_TYPE)
24094 {
24095 if (TYPE_DOMAIN (type)
24096 && dependent_type_p (TYPE_DOMAIN (type)))
24097 return true;
24098 return dependent_type_p (TREE_TYPE (type));
24099 }
24100
24101 /* -- a template-id in which either the template name is a template
24102 parameter ... */
24103 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24104 return true;
24105 /* ... or any of the template arguments is a dependent type or
24106 an expression that is type-dependent or value-dependent. */
24107 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
24108 && (any_dependent_template_arguments_p
24109 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
24110 return true;
24111
24112 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24113 dependent; if the argument of the `typeof' expression is not
24114 type-dependent, then it should already been have resolved. */
24115 if (TREE_CODE (type) == TYPEOF_TYPE
24116 || TREE_CODE (type) == DECLTYPE_TYPE
24117 || TREE_CODE (type) == UNDERLYING_TYPE)
24118 return true;
24119
24120 /* A template argument pack is dependent if any of its packed
24121 arguments are. */
24122 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
24123 {
24124 tree args = ARGUMENT_PACK_ARGS (type);
24125 int i, len = TREE_VEC_LENGTH (args);
24126 for (i = 0; i < len; ++i)
24127 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24128 return true;
24129 }
24130
24131 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24132 be template parameters. */
24133 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
24134 return true;
24135
24136 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
24137 return true;
24138
24139 /* The standard does not specifically mention types that are local
24140 to template functions or local classes, but they should be
24141 considered dependent too. For example:
24142
24143 template <int I> void f() {
24144 enum E { a = I };
24145 S<sizeof (E)> s;
24146 }
24147
24148 The size of `E' cannot be known until the value of `I' has been
24149 determined. Therefore, `E' must be considered dependent. */
24150 scope = TYPE_CONTEXT (type);
24151 if (scope && TYPE_P (scope))
24152 return dependent_type_p (scope);
24153 /* Don't use type_dependent_expression_p here, as it can lead
24154 to infinite recursion trying to determine whether a lambda
24155 nested in a lambda is dependent (c++/47687). */
24156 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
24157 && DECL_LANG_SPECIFIC (scope)
24158 && DECL_TEMPLATE_INFO (scope)
24159 && (any_dependent_template_arguments_p
24160 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
24161 return true;
24162
24163 /* Other types are non-dependent. */
24164 return false;
24165 }
24166
24167 /* Returns TRUE if TYPE is dependent, in the sense of
24168 [temp.dep.type]. Note that a NULL type is considered dependent. */
24169
24170 bool
24171 dependent_type_p (tree type)
24172 {
24173 /* If there are no template parameters in scope, then there can't be
24174 any dependent types. */
24175 if (!processing_template_decl)
24176 {
24177 /* If we are not processing a template, then nobody should be
24178 providing us with a dependent type. */
24179 gcc_assert (type);
24180 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
24181 return false;
24182 }
24183
24184 /* If the type is NULL, we have not computed a type for the entity
24185 in question; in that case, the type is dependent. */
24186 if (!type)
24187 return true;
24188
24189 /* Erroneous types can be considered non-dependent. */
24190 if (type == error_mark_node)
24191 return false;
24192
24193 /* Getting here with global_type_node means we improperly called this
24194 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24195 gcc_checking_assert (type != global_type_node);
24196
24197 /* If we have not already computed the appropriate value for TYPE,
24198 do so now. */
24199 if (!TYPE_DEPENDENT_P_VALID (type))
24200 {
24201 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
24202 TYPE_DEPENDENT_P_VALID (type) = 1;
24203 }
24204
24205 return TYPE_DEPENDENT_P (type);
24206 }
24207
24208 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24209 lookup. In other words, a dependent type that is not the current
24210 instantiation. */
24211
24212 bool
24213 dependent_scope_p (tree scope)
24214 {
24215 return (scope && TYPE_P (scope) && dependent_type_p (scope)
24216 && !currently_open_class (scope));
24217 }
24218
24219 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24220 an unknown base of 'this' (and is therefore instantiation-dependent). */
24221
24222 static bool
24223 unknown_base_ref_p (tree t)
24224 {
24225 if (!current_class_ptr)
24226 return false;
24227
24228 tree mem = TREE_OPERAND (t, 1);
24229 if (shared_member_p (mem))
24230 return false;
24231
24232 tree cur = current_nonlambda_class_type ();
24233 if (!any_dependent_bases_p (cur))
24234 return false;
24235
24236 tree ctx = TREE_OPERAND (t, 0);
24237 if (DERIVED_FROM_P (ctx, cur))
24238 return false;
24239
24240 return true;
24241 }
24242
24243 /* T is a SCOPE_REF; return whether we need to consider it
24244 instantiation-dependent so that we can check access at instantiation
24245 time even though we know which member it resolves to. */
24246
24247 static bool
24248 instantiation_dependent_scope_ref_p (tree t)
24249 {
24250 if (DECL_P (TREE_OPERAND (t, 1))
24251 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
24252 && !unknown_base_ref_p (t)
24253 && accessible_in_template_p (TREE_OPERAND (t, 0),
24254 TREE_OPERAND (t, 1)))
24255 return false;
24256 else
24257 return true;
24258 }
24259
24260 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24261 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24262 expression. */
24263
24264 /* Note that this predicate is not appropriate for general expressions;
24265 only constant expressions (that satisfy potential_constant_expression)
24266 can be tested for value dependence. */
24267
24268 bool
24269 value_dependent_expression_p (tree expression)
24270 {
24271 if (!processing_template_decl || expression == NULL_TREE)
24272 return false;
24273
24274 /* A type-dependent expression is also value-dependent. */
24275 if (type_dependent_expression_p (expression))
24276 return true;
24277
24278 switch (TREE_CODE (expression))
24279 {
24280 case BASELINK:
24281 /* A dependent member function of the current instantiation. */
24282 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24283
24284 case FUNCTION_DECL:
24285 /* A dependent member function of the current instantiation. */
24286 if (DECL_CLASS_SCOPE_P (expression)
24287 && dependent_type_p (DECL_CONTEXT (expression)))
24288 return true;
24289 break;
24290
24291 case IDENTIFIER_NODE:
24292 /* A name that has not been looked up -- must be dependent. */
24293 return true;
24294
24295 case TEMPLATE_PARM_INDEX:
24296 /* A non-type template parm. */
24297 return true;
24298
24299 case CONST_DECL:
24300 /* A non-type template parm. */
24301 if (DECL_TEMPLATE_PARM_P (expression))
24302 return true;
24303 return value_dependent_expression_p (DECL_INITIAL (expression));
24304
24305 case VAR_DECL:
24306 /* A constant with literal type and is initialized
24307 with an expression that is value-dependent. */
24308 if (DECL_DEPENDENT_INIT_P (expression)
24309 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24310 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
24311 return true;
24312 if (DECL_HAS_VALUE_EXPR_P (expression))
24313 {
24314 tree value_expr = DECL_VALUE_EXPR (expression);
24315 if (value_dependent_expression_p (value_expr))
24316 return true;
24317 }
24318 return false;
24319
24320 case DYNAMIC_CAST_EXPR:
24321 case STATIC_CAST_EXPR:
24322 case CONST_CAST_EXPR:
24323 case REINTERPRET_CAST_EXPR:
24324 case CAST_EXPR:
24325 case IMPLICIT_CONV_EXPR:
24326 /* These expressions are value-dependent if the type to which
24327 the cast occurs is dependent or the expression being casted
24328 is value-dependent. */
24329 {
24330 tree type = TREE_TYPE (expression);
24331
24332 if (dependent_type_p (type))
24333 return true;
24334
24335 /* A functional cast has a list of operands. */
24336 expression = TREE_OPERAND (expression, 0);
24337 if (!expression)
24338 {
24339 /* If there are no operands, it must be an expression such
24340 as "int()". This should not happen for aggregate types
24341 because it would form non-constant expressions. */
24342 gcc_assert (cxx_dialect >= cxx11
24343 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24344
24345 return false;
24346 }
24347
24348 if (TREE_CODE (expression) == TREE_LIST)
24349 return any_value_dependent_elements_p (expression);
24350
24351 return value_dependent_expression_p (expression);
24352 }
24353
24354 case SIZEOF_EXPR:
24355 if (SIZEOF_EXPR_TYPE_P (expression))
24356 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24357 /* FALLTHRU */
24358 case ALIGNOF_EXPR:
24359 case TYPEID_EXPR:
24360 /* A `sizeof' expression is value-dependent if the operand is
24361 type-dependent or is a pack expansion. */
24362 expression = TREE_OPERAND (expression, 0);
24363 if (PACK_EXPANSION_P (expression))
24364 return true;
24365 else if (TYPE_P (expression))
24366 return dependent_type_p (expression);
24367 return instantiation_dependent_uneval_expression_p (expression);
24368
24369 case AT_ENCODE_EXPR:
24370 /* An 'encode' expression is value-dependent if the operand is
24371 type-dependent. */
24372 expression = TREE_OPERAND (expression, 0);
24373 return dependent_type_p (expression);
24374
24375 case NOEXCEPT_EXPR:
24376 expression = TREE_OPERAND (expression, 0);
24377 return instantiation_dependent_uneval_expression_p (expression);
24378
24379 case SCOPE_REF:
24380 /* All instantiation-dependent expressions should also be considered
24381 value-dependent. */
24382 return instantiation_dependent_scope_ref_p (expression);
24383
24384 case COMPONENT_REF:
24385 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24386 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24387
24388 case NONTYPE_ARGUMENT_PACK:
24389 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24390 is value-dependent. */
24391 {
24392 tree values = ARGUMENT_PACK_ARGS (expression);
24393 int i, len = TREE_VEC_LENGTH (values);
24394
24395 for (i = 0; i < len; ++i)
24396 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24397 return true;
24398
24399 return false;
24400 }
24401
24402 case TRAIT_EXPR:
24403 {
24404 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24405
24406 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24407 return true;
24408
24409 if (!type2)
24410 return false;
24411
24412 if (TREE_CODE (type2) != TREE_LIST)
24413 return dependent_type_p (type2);
24414
24415 for (; type2; type2 = TREE_CHAIN (type2))
24416 if (dependent_type_p (TREE_VALUE (type2)))
24417 return true;
24418
24419 return false;
24420 }
24421
24422 case MODOP_EXPR:
24423 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24424 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24425
24426 case ARRAY_REF:
24427 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24428 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24429
24430 case ADDR_EXPR:
24431 {
24432 tree op = TREE_OPERAND (expression, 0);
24433 return (value_dependent_expression_p (op)
24434 || has_value_dependent_address (op));
24435 }
24436
24437 case REQUIRES_EXPR:
24438 /* Treat all requires-expressions as value-dependent so
24439 we don't try to fold them. */
24440 return true;
24441
24442 case TYPE_REQ:
24443 return dependent_type_p (TREE_OPERAND (expression, 0));
24444
24445 case CALL_EXPR:
24446 {
24447 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24448 return true;
24449 tree fn = get_callee_fndecl (expression);
24450 int i, nargs;
24451 nargs = call_expr_nargs (expression);
24452 for (i = 0; i < nargs; ++i)
24453 {
24454 tree op = CALL_EXPR_ARG (expression, i);
24455 /* In a call to a constexpr member function, look through the
24456 implicit ADDR_EXPR on the object argument so that it doesn't
24457 cause the call to be considered value-dependent. We also
24458 look through it in potential_constant_expression. */
24459 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24460 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24461 && TREE_CODE (op) == ADDR_EXPR)
24462 op = TREE_OPERAND (op, 0);
24463 if (value_dependent_expression_p (op))
24464 return true;
24465 }
24466 return false;
24467 }
24468
24469 case TEMPLATE_ID_EXPR:
24470 return variable_concept_p (TREE_OPERAND (expression, 0));
24471
24472 case CONSTRUCTOR:
24473 {
24474 unsigned ix;
24475 tree val;
24476 if (dependent_type_p (TREE_TYPE (expression)))
24477 return true;
24478 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24479 if (value_dependent_expression_p (val))
24480 return true;
24481 return false;
24482 }
24483
24484 case STMT_EXPR:
24485 /* Treat a GNU statement expression as dependent to avoid crashing
24486 under instantiate_non_dependent_expr; it can't be constant. */
24487 return true;
24488
24489 default:
24490 /* A constant expression is value-dependent if any subexpression is
24491 value-dependent. */
24492 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24493 {
24494 case tcc_reference:
24495 case tcc_unary:
24496 case tcc_comparison:
24497 case tcc_binary:
24498 case tcc_expression:
24499 case tcc_vl_exp:
24500 {
24501 int i, len = cp_tree_operand_length (expression);
24502
24503 for (i = 0; i < len; i++)
24504 {
24505 tree t = TREE_OPERAND (expression, i);
24506
24507 /* In some cases, some of the operands may be missing.
24508 (For example, in the case of PREDECREMENT_EXPR, the
24509 amount to increment by may be missing.) That doesn't
24510 make the expression dependent. */
24511 if (t && value_dependent_expression_p (t))
24512 return true;
24513 }
24514 }
24515 break;
24516 default:
24517 break;
24518 }
24519 break;
24520 }
24521
24522 /* The expression is not value-dependent. */
24523 return false;
24524 }
24525
24526 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24527 [temp.dep.expr]. Note that an expression with no type is
24528 considered dependent. Other parts of the compiler arrange for an
24529 expression with type-dependent subexpressions to have no type, so
24530 this function doesn't have to be fully recursive. */
24531
24532 bool
24533 type_dependent_expression_p (tree expression)
24534 {
24535 if (!processing_template_decl)
24536 return false;
24537
24538 if (expression == NULL_TREE || expression == error_mark_node)
24539 return false;
24540
24541 STRIP_ANY_LOCATION_WRAPPER (expression);
24542
24543 /* An unresolved name is always dependent. */
24544 if (identifier_p (expression)
24545 || TREE_CODE (expression) == USING_DECL
24546 || TREE_CODE (expression) == WILDCARD_DECL)
24547 return true;
24548
24549 /* A fold expression is type-dependent. */
24550 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24551 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24552 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24553 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24554 return true;
24555
24556 /* Some expression forms are never type-dependent. */
24557 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24558 || TREE_CODE (expression) == SIZEOF_EXPR
24559 || TREE_CODE (expression) == ALIGNOF_EXPR
24560 || TREE_CODE (expression) == AT_ENCODE_EXPR
24561 || TREE_CODE (expression) == NOEXCEPT_EXPR
24562 || TREE_CODE (expression) == TRAIT_EXPR
24563 || TREE_CODE (expression) == TYPEID_EXPR
24564 || TREE_CODE (expression) == DELETE_EXPR
24565 || TREE_CODE (expression) == VEC_DELETE_EXPR
24566 || TREE_CODE (expression) == THROW_EXPR
24567 || TREE_CODE (expression) == REQUIRES_EXPR)
24568 return false;
24569
24570 /* The types of these expressions depends only on the type to which
24571 the cast occurs. */
24572 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24573 || TREE_CODE (expression) == STATIC_CAST_EXPR
24574 || TREE_CODE (expression) == CONST_CAST_EXPR
24575 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24576 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24577 || TREE_CODE (expression) == CAST_EXPR)
24578 return dependent_type_p (TREE_TYPE (expression));
24579
24580 /* The types of these expressions depends only on the type created
24581 by the expression. */
24582 if (TREE_CODE (expression) == NEW_EXPR
24583 || TREE_CODE (expression) == VEC_NEW_EXPR)
24584 {
24585 /* For NEW_EXPR tree nodes created inside a template, either
24586 the object type itself or a TREE_LIST may appear as the
24587 operand 1. */
24588 tree type = TREE_OPERAND (expression, 1);
24589 if (TREE_CODE (type) == TREE_LIST)
24590 /* This is an array type. We need to check array dimensions
24591 as well. */
24592 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24593 || value_dependent_expression_p
24594 (TREE_OPERAND (TREE_VALUE (type), 1));
24595 else
24596 return dependent_type_p (type);
24597 }
24598
24599 if (TREE_CODE (expression) == SCOPE_REF)
24600 {
24601 tree scope = TREE_OPERAND (expression, 0);
24602 tree name = TREE_OPERAND (expression, 1);
24603
24604 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24605 contains an identifier associated by name lookup with one or more
24606 declarations declared with a dependent type, or...a
24607 nested-name-specifier or qualified-id that names a member of an
24608 unknown specialization. */
24609 return (type_dependent_expression_p (name)
24610 || dependent_scope_p (scope));
24611 }
24612
24613 if (TREE_CODE (expression) == TEMPLATE_DECL
24614 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24615 return uses_outer_template_parms (expression);
24616
24617 if (TREE_CODE (expression) == STMT_EXPR)
24618 expression = stmt_expr_value_expr (expression);
24619
24620 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24621 {
24622 tree elt;
24623 unsigned i;
24624
24625 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24626 {
24627 if (type_dependent_expression_p (elt))
24628 return true;
24629 }
24630 return false;
24631 }
24632
24633 /* A static data member of the current instantiation with incomplete
24634 array type is type-dependent, as the definition and specializations
24635 can have different bounds. */
24636 if (VAR_P (expression)
24637 && DECL_CLASS_SCOPE_P (expression)
24638 && dependent_type_p (DECL_CONTEXT (expression))
24639 && VAR_HAD_UNKNOWN_BOUND (expression))
24640 return true;
24641
24642 /* An array of unknown bound depending on a variadic parameter, eg:
24643
24644 template<typename... Args>
24645 void foo (Args... args)
24646 {
24647 int arr[] = { args... };
24648 }
24649
24650 template<int... vals>
24651 void bar ()
24652 {
24653 int arr[] = { vals... };
24654 }
24655
24656 If the array has no length and has an initializer, it must be that
24657 we couldn't determine its length in cp_complete_array_type because
24658 it is dependent. */
24659 if (VAR_P (expression)
24660 && TREE_TYPE (expression) != NULL_TREE
24661 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24662 && !TYPE_DOMAIN (TREE_TYPE (expression))
24663 && DECL_INITIAL (expression))
24664 return true;
24665
24666 /* A function or variable template-id is type-dependent if it has any
24667 dependent template arguments. */
24668 if (VAR_OR_FUNCTION_DECL_P (expression)
24669 && DECL_LANG_SPECIFIC (expression)
24670 && DECL_TEMPLATE_INFO (expression))
24671 {
24672 /* Consider the innermost template arguments, since those are the ones
24673 that come from the template-id; the template arguments for the
24674 enclosing class do not make it type-dependent unless they are used in
24675 the type of the decl. */
24676 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24677 && (any_dependent_template_arguments_p
24678 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24679 return true;
24680 }
24681
24682 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24683 type-dependent. Checking this is important for functions with auto return
24684 type, which looks like a dependent type. */
24685 if (TREE_CODE (expression) == FUNCTION_DECL
24686 && !(DECL_CLASS_SCOPE_P (expression)
24687 && dependent_type_p (DECL_CONTEXT (expression)))
24688 && !(DECL_LANG_SPECIFIC (expression)
24689 && DECL_FRIEND_P (expression)
24690 && (!DECL_FRIEND_CONTEXT (expression)
24691 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24692 && !DECL_LOCAL_FUNCTION_P (expression))
24693 {
24694 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24695 || undeduced_auto_decl (expression));
24696 return false;
24697 }
24698
24699 /* Always dependent, on the number of arguments if nothing else. */
24700 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24701 return true;
24702
24703 if (TREE_TYPE (expression) == unknown_type_node)
24704 {
24705 if (TREE_CODE (expression) == ADDR_EXPR)
24706 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24707 if (TREE_CODE (expression) == COMPONENT_REF
24708 || TREE_CODE (expression) == OFFSET_REF)
24709 {
24710 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24711 return true;
24712 expression = TREE_OPERAND (expression, 1);
24713 if (identifier_p (expression))
24714 return false;
24715 }
24716 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24717 if (TREE_CODE (expression) == SCOPE_REF)
24718 return false;
24719
24720 if (BASELINK_P (expression))
24721 {
24722 if (BASELINK_OPTYPE (expression)
24723 && dependent_type_p (BASELINK_OPTYPE (expression)))
24724 return true;
24725 expression = BASELINK_FUNCTIONS (expression);
24726 }
24727
24728 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24729 {
24730 if (any_dependent_template_arguments_p
24731 (TREE_OPERAND (expression, 1)))
24732 return true;
24733 expression = TREE_OPERAND (expression, 0);
24734 if (identifier_p (expression))
24735 return true;
24736 }
24737
24738 gcc_assert (TREE_CODE (expression) == OVERLOAD
24739 || TREE_CODE (expression) == FUNCTION_DECL);
24740
24741 for (lkp_iterator iter (expression); iter; ++iter)
24742 if (type_dependent_expression_p (*iter))
24743 return true;
24744
24745 return false;
24746 }
24747
24748 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24749
24750 /* Dependent type attributes might not have made it from the decl to
24751 the type yet. */
24752 if (DECL_P (expression)
24753 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24754 return true;
24755
24756 return (dependent_type_p (TREE_TYPE (expression)));
24757 }
24758
24759 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24760 type-dependent if the expression refers to a member of the current
24761 instantiation and the type of the referenced member is dependent, or the
24762 class member access expression refers to a member of an unknown
24763 specialization.
24764
24765 This function returns true if the OBJECT in such a class member access
24766 expression is of an unknown specialization. */
24767
24768 bool
24769 type_dependent_object_expression_p (tree object)
24770 {
24771 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24772 dependent. */
24773 if (TREE_CODE (object) == IDENTIFIER_NODE)
24774 return true;
24775 tree scope = TREE_TYPE (object);
24776 return (!scope || dependent_scope_p (scope));
24777 }
24778
24779 /* walk_tree callback function for instantiation_dependent_expression_p,
24780 below. Returns non-zero if a dependent subexpression is found. */
24781
24782 static tree
24783 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24784 void * /*data*/)
24785 {
24786 if (TYPE_P (*tp))
24787 {
24788 /* We don't have to worry about decltype currently because decltype
24789 of an instantiation-dependent expr is a dependent type. This
24790 might change depending on the resolution of DR 1172. */
24791 *walk_subtrees = false;
24792 return NULL_TREE;
24793 }
24794 enum tree_code code = TREE_CODE (*tp);
24795 switch (code)
24796 {
24797 /* Don't treat an argument list as dependent just because it has no
24798 TREE_TYPE. */
24799 case TREE_LIST:
24800 case TREE_VEC:
24801 return NULL_TREE;
24802
24803 case TEMPLATE_PARM_INDEX:
24804 return *tp;
24805
24806 /* Handle expressions with type operands. */
24807 case SIZEOF_EXPR:
24808 case ALIGNOF_EXPR:
24809 case TYPEID_EXPR:
24810 case AT_ENCODE_EXPR:
24811 {
24812 tree op = TREE_OPERAND (*tp, 0);
24813 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24814 op = TREE_TYPE (op);
24815 if (TYPE_P (op))
24816 {
24817 if (dependent_type_p (op))
24818 return *tp;
24819 else
24820 {
24821 *walk_subtrees = false;
24822 return NULL_TREE;
24823 }
24824 }
24825 break;
24826 }
24827
24828 case COMPONENT_REF:
24829 if (identifier_p (TREE_OPERAND (*tp, 1)))
24830 /* In a template, finish_class_member_access_expr creates a
24831 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24832 type-dependent, so that we can check access control at
24833 instantiation time (PR 42277). See also Core issue 1273. */
24834 return *tp;
24835 break;
24836
24837 case SCOPE_REF:
24838 if (instantiation_dependent_scope_ref_p (*tp))
24839 return *tp;
24840 else
24841 break;
24842
24843 /* Treat statement-expressions as dependent. */
24844 case BIND_EXPR:
24845 return *tp;
24846
24847 /* Treat requires-expressions as dependent. */
24848 case REQUIRES_EXPR:
24849 return *tp;
24850
24851 case CALL_EXPR:
24852 /* Treat calls to function concepts as dependent. */
24853 if (function_concept_check_p (*tp))
24854 return *tp;
24855 break;
24856
24857 case TEMPLATE_ID_EXPR:
24858 /* And variable concepts. */
24859 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24860 return *tp;
24861 break;
24862
24863 default:
24864 break;
24865 }
24866
24867 if (type_dependent_expression_p (*tp))
24868 return *tp;
24869 else
24870 return NULL_TREE;
24871 }
24872
24873 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24874 sense defined by the ABI:
24875
24876 "An expression is instantiation-dependent if it is type-dependent
24877 or value-dependent, or it has a subexpression that is type-dependent
24878 or value-dependent."
24879
24880 Except don't actually check value-dependence for unevaluated expressions,
24881 because in sizeof(i) we don't care about the value of i. Checking
24882 type-dependence will in turn check value-dependence of array bounds/template
24883 arguments as needed. */
24884
24885 bool
24886 instantiation_dependent_uneval_expression_p (tree expression)
24887 {
24888 tree result;
24889
24890 if (!processing_template_decl)
24891 return false;
24892
24893 if (expression == error_mark_node)
24894 return false;
24895
24896 result = cp_walk_tree_without_duplicates (&expression,
24897 instantiation_dependent_r, NULL);
24898 return result != NULL_TREE;
24899 }
24900
24901 /* As above, but also check value-dependence of the expression as a whole. */
24902
24903 bool
24904 instantiation_dependent_expression_p (tree expression)
24905 {
24906 return (instantiation_dependent_uneval_expression_p (expression)
24907 || value_dependent_expression_p (expression));
24908 }
24909
24910 /* Like type_dependent_expression_p, but it also works while not processing
24911 a template definition, i.e. during substitution or mangling. */
24912
24913 bool
24914 type_dependent_expression_p_push (tree expr)
24915 {
24916 bool b;
24917 ++processing_template_decl;
24918 b = type_dependent_expression_p (expr);
24919 --processing_template_decl;
24920 return b;
24921 }
24922
24923 /* Returns TRUE if ARGS contains a type-dependent expression. */
24924
24925 bool
24926 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24927 {
24928 unsigned int i;
24929 tree arg;
24930
24931 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24932 {
24933 if (type_dependent_expression_p (arg))
24934 return true;
24935 }
24936 return false;
24937 }
24938
24939 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24940 expressions) contains any type-dependent expressions. */
24941
24942 bool
24943 any_type_dependent_elements_p (const_tree list)
24944 {
24945 for (; list; list = TREE_CHAIN (list))
24946 if (type_dependent_expression_p (TREE_VALUE (list)))
24947 return true;
24948
24949 return false;
24950 }
24951
24952 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24953 expressions) contains any value-dependent expressions. */
24954
24955 bool
24956 any_value_dependent_elements_p (const_tree list)
24957 {
24958 for (; list; list = TREE_CHAIN (list))
24959 if (value_dependent_expression_p (TREE_VALUE (list)))
24960 return true;
24961
24962 return false;
24963 }
24964
24965 /* Returns TRUE if the ARG (a template argument) is dependent. */
24966
24967 bool
24968 dependent_template_arg_p (tree arg)
24969 {
24970 if (!processing_template_decl)
24971 return false;
24972
24973 /* Assume a template argument that was wrongly written by the user
24974 is dependent. This is consistent with what
24975 any_dependent_template_arguments_p [that calls this function]
24976 does. */
24977 if (!arg || arg == error_mark_node)
24978 return true;
24979
24980 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24981 arg = argument_pack_select_arg (arg);
24982
24983 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24984 return true;
24985 if (TREE_CODE (arg) == TEMPLATE_DECL)
24986 {
24987 if (DECL_TEMPLATE_PARM_P (arg))
24988 return true;
24989 /* A member template of a dependent class is not necessarily
24990 type-dependent, but it is a dependent template argument because it
24991 will be a member of an unknown specialization to that template. */
24992 tree scope = CP_DECL_CONTEXT (arg);
24993 return TYPE_P (scope) && dependent_type_p (scope);
24994 }
24995 else if (ARGUMENT_PACK_P (arg))
24996 {
24997 tree args = ARGUMENT_PACK_ARGS (arg);
24998 int i, len = TREE_VEC_LENGTH (args);
24999 for (i = 0; i < len; ++i)
25000 {
25001 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25002 return true;
25003 }
25004
25005 return false;
25006 }
25007 else if (TYPE_P (arg))
25008 return dependent_type_p (arg);
25009 else
25010 return (type_dependent_expression_p (arg)
25011 || value_dependent_expression_p (arg));
25012 }
25013
25014 /* Returns true if ARGS (a collection of template arguments) contains
25015 any types that require structural equality testing. */
25016
25017 bool
25018 any_template_arguments_need_structural_equality_p (tree args)
25019 {
25020 int i;
25021 int j;
25022
25023 if (!args)
25024 return false;
25025 if (args == error_mark_node)
25026 return true;
25027
25028 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25029 {
25030 tree level = TMPL_ARGS_LEVEL (args, i + 1);
25031 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25032 {
25033 tree arg = TREE_VEC_ELT (level, j);
25034 tree packed_args = NULL_TREE;
25035 int k, len = 1;
25036
25037 if (ARGUMENT_PACK_P (arg))
25038 {
25039 /* Look inside the argument pack. */
25040 packed_args = ARGUMENT_PACK_ARGS (arg);
25041 len = TREE_VEC_LENGTH (packed_args);
25042 }
25043
25044 for (k = 0; k < len; ++k)
25045 {
25046 if (packed_args)
25047 arg = TREE_VEC_ELT (packed_args, k);
25048
25049 if (error_operand_p (arg))
25050 return true;
25051 else if (TREE_CODE (arg) == TEMPLATE_DECL)
25052 continue;
25053 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
25054 return true;
25055 else if (!TYPE_P (arg) && TREE_TYPE (arg)
25056 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
25057 return true;
25058 }
25059 }
25060 }
25061
25062 return false;
25063 }
25064
25065 /* Returns true if ARGS (a collection of template arguments) contains
25066 any dependent arguments. */
25067
25068 bool
25069 any_dependent_template_arguments_p (const_tree args)
25070 {
25071 int i;
25072 int j;
25073
25074 if (!args)
25075 return false;
25076 if (args == error_mark_node)
25077 return true;
25078
25079 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25080 {
25081 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25082 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25083 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
25084 return true;
25085 }
25086
25087 return false;
25088 }
25089
25090 /* Returns true if ARGS contains any errors. */
25091
25092 bool
25093 any_erroneous_template_args_p (const_tree args)
25094 {
25095 int i;
25096 int j;
25097
25098 if (args == error_mark_node)
25099 return true;
25100
25101 if (args && TREE_CODE (args) != TREE_VEC)
25102 {
25103 if (tree ti = get_template_info (args))
25104 args = TI_ARGS (ti);
25105 else
25106 args = NULL_TREE;
25107 }
25108
25109 if (!args)
25110 return false;
25111
25112 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25113 {
25114 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25115 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25116 if (error_operand_p (TREE_VEC_ELT (level, j)))
25117 return true;
25118 }
25119
25120 return false;
25121 }
25122
25123 /* Returns TRUE if the template TMPL is type-dependent. */
25124
25125 bool
25126 dependent_template_p (tree tmpl)
25127 {
25128 if (TREE_CODE (tmpl) == OVERLOAD)
25129 {
25130 for (lkp_iterator iter (tmpl); iter; ++iter)
25131 if (dependent_template_p (*iter))
25132 return true;
25133 return false;
25134 }
25135
25136 /* Template template parameters are dependent. */
25137 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
25138 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
25139 return true;
25140 /* So are names that have not been looked up. */
25141 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
25142 return true;
25143 return false;
25144 }
25145
25146 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25147
25148 bool
25149 dependent_template_id_p (tree tmpl, tree args)
25150 {
25151 return (dependent_template_p (tmpl)
25152 || any_dependent_template_arguments_p (args));
25153 }
25154
25155 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25156 are dependent. */
25157
25158 bool
25159 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
25160 {
25161 int i;
25162
25163 if (!processing_template_decl)
25164 return false;
25165
25166 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
25167 {
25168 tree decl = TREE_VEC_ELT (declv, i);
25169 tree init = TREE_VEC_ELT (initv, i);
25170 tree cond = TREE_VEC_ELT (condv, i);
25171 tree incr = TREE_VEC_ELT (incrv, i);
25172
25173 if (type_dependent_expression_p (decl)
25174 || TREE_CODE (decl) == SCOPE_REF)
25175 return true;
25176
25177 if (init && type_dependent_expression_p (init))
25178 return true;
25179
25180 if (type_dependent_expression_p (cond))
25181 return true;
25182
25183 if (COMPARISON_CLASS_P (cond)
25184 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
25185 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
25186 return true;
25187
25188 if (TREE_CODE (incr) == MODOP_EXPR)
25189 {
25190 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
25191 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
25192 return true;
25193 }
25194 else if (type_dependent_expression_p (incr))
25195 return true;
25196 else if (TREE_CODE (incr) == MODIFY_EXPR)
25197 {
25198 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
25199 return true;
25200 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
25201 {
25202 tree t = TREE_OPERAND (incr, 1);
25203 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
25204 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
25205 return true;
25206 }
25207 }
25208 }
25209
25210 return false;
25211 }
25212
25213 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25214 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25215 no such TYPE can be found. Note that this function peers inside
25216 uninstantiated templates and therefore should be used only in
25217 extremely limited situations. ONLY_CURRENT_P restricts this
25218 peering to the currently open classes hierarchy (which is required
25219 when comparing types). */
25220
25221 tree
25222 resolve_typename_type (tree type, bool only_current_p)
25223 {
25224 tree scope;
25225 tree name;
25226 tree decl;
25227 int quals;
25228 tree pushed_scope;
25229 tree result;
25230
25231 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
25232
25233 scope = TYPE_CONTEXT (type);
25234 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25235 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25236 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25237 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25238 identifier of the TYPENAME_TYPE anymore.
25239 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25240 TYPENAME_TYPE instead, we avoid messing up with a possible
25241 typedef variant case. */
25242 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
25243
25244 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25245 it first before we can figure out what NAME refers to. */
25246 if (TREE_CODE (scope) == TYPENAME_TYPE)
25247 {
25248 if (TYPENAME_IS_RESOLVING_P (scope))
25249 /* Given a class template A with a dependent base with nested type C,
25250 typedef typename A::C::C C will land us here, as trying to resolve
25251 the initial A::C leads to the local C typedef, which leads back to
25252 A::C::C. So we break the recursion now. */
25253 return type;
25254 else
25255 scope = resolve_typename_type (scope, only_current_p);
25256 }
25257 /* If we don't know what SCOPE refers to, then we cannot resolve the
25258 TYPENAME_TYPE. */
25259 if (!CLASS_TYPE_P (scope))
25260 return type;
25261 /* If this is a typedef, we don't want to look inside (c++/11987). */
25262 if (typedef_variant_p (type))
25263 return type;
25264 /* If SCOPE isn't the template itself, it will not have a valid
25265 TYPE_FIELDS list. */
25266 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
25267 /* scope is either the template itself or a compatible instantiation
25268 like X<T>, so look up the name in the original template. */
25269 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
25270 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25271 gcc_checking_assert (uses_template_parms (scope));
25272 /* If scope has no fields, it can't be a current instantiation. Check this
25273 before currently_open_class to avoid infinite recursion (71515). */
25274 if (!TYPE_FIELDS (scope))
25275 return type;
25276 /* If the SCOPE is not the current instantiation, there's no reason
25277 to look inside it. */
25278 if (only_current_p && !currently_open_class (scope))
25279 return type;
25280 /* Enter the SCOPE so that name lookup will be resolved as if we
25281 were in the class definition. In particular, SCOPE will no
25282 longer be considered a dependent type. */
25283 pushed_scope = push_scope (scope);
25284 /* Look up the declaration. */
25285 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
25286 tf_warning_or_error);
25287
25288 result = NULL_TREE;
25289
25290 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25291 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25292 tree fullname = TYPENAME_TYPE_FULLNAME (type);
25293 if (!decl)
25294 /*nop*/;
25295 else if (identifier_p (fullname)
25296 && TREE_CODE (decl) == TYPE_DECL)
25297 {
25298 result = TREE_TYPE (decl);
25299 if (result == error_mark_node)
25300 result = NULL_TREE;
25301 }
25302 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25303 && DECL_CLASS_TEMPLATE_P (decl))
25304 {
25305 /* Obtain the template and the arguments. */
25306 tree tmpl = TREE_OPERAND (fullname, 0);
25307 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25308 {
25309 /* We get here with a plain identifier because a previous tentative
25310 parse of the nested-name-specifier as part of a ptr-operator saw
25311 ::template X<A>. The use of ::template is necessary in a
25312 ptr-operator, but wrong in a declarator-id.
25313
25314 [temp.names]: In a qualified-id of a declarator-id, the keyword
25315 template shall not appear at the top level. */
25316 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
25317 "keyword %<template%> not allowed in declarator-id");
25318 tmpl = decl;
25319 }
25320 tree args = TREE_OPERAND (fullname, 1);
25321 /* Instantiate the template. */
25322 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25323 /*entering_scope=*/true,
25324 tf_error | tf_user);
25325 if (result == error_mark_node)
25326 result = NULL_TREE;
25327 }
25328
25329 /* Leave the SCOPE. */
25330 if (pushed_scope)
25331 pop_scope (pushed_scope);
25332
25333 /* If we failed to resolve it, return the original typename. */
25334 if (!result)
25335 return type;
25336
25337 /* If lookup found a typename type, resolve that too. */
25338 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25339 {
25340 /* Ill-formed programs can cause infinite recursion here, so we
25341 must catch that. */
25342 TYPENAME_IS_RESOLVING_P (result) = 1;
25343 result = resolve_typename_type (result, only_current_p);
25344 TYPENAME_IS_RESOLVING_P (result) = 0;
25345 }
25346
25347 /* Qualify the resulting type. */
25348 quals = cp_type_quals (type);
25349 if (quals)
25350 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25351
25352 return result;
25353 }
25354
25355 /* EXPR is an expression which is not type-dependent. Return a proxy
25356 for EXPR that can be used to compute the types of larger
25357 expressions containing EXPR. */
25358
25359 tree
25360 build_non_dependent_expr (tree expr)
25361 {
25362 tree orig_expr = expr;
25363 tree inner_expr;
25364
25365 /* When checking, try to get a constant value for all non-dependent
25366 expressions in order to expose bugs in *_dependent_expression_p
25367 and constexpr. This can affect code generation, see PR70704, so
25368 only do this for -fchecking=2. */
25369 if (flag_checking > 1
25370 && cxx_dialect >= cxx11
25371 /* Don't do this during nsdmi parsing as it can lead to
25372 unexpected recursive instantiations. */
25373 && !parsing_nsdmi ()
25374 /* Don't do this during concept expansion either and for
25375 the same reason. */
25376 && !expanding_concept ())
25377 fold_non_dependent_expr (expr);
25378
25379 STRIP_ANY_LOCATION_WRAPPER (expr);
25380
25381 /* Preserve OVERLOADs; the functions must be available to resolve
25382 types. */
25383 inner_expr = expr;
25384 if (TREE_CODE (inner_expr) == STMT_EXPR)
25385 inner_expr = stmt_expr_value_expr (inner_expr);
25386 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25387 inner_expr = TREE_OPERAND (inner_expr, 0);
25388 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25389 inner_expr = TREE_OPERAND (inner_expr, 1);
25390 if (is_overloaded_fn (inner_expr)
25391 || TREE_CODE (inner_expr) == OFFSET_REF)
25392 return orig_expr;
25393 /* There is no need to return a proxy for a variable. */
25394 if (VAR_P (expr))
25395 return orig_expr;
25396 /* Preserve string constants; conversions from string constants to
25397 "char *" are allowed, even though normally a "const char *"
25398 cannot be used to initialize a "char *". */
25399 if (TREE_CODE (expr) == STRING_CST)
25400 return orig_expr;
25401 /* Preserve void and arithmetic constants, as an optimization -- there is no
25402 reason to create a new node. */
25403 if (TREE_CODE (expr) == VOID_CST
25404 || TREE_CODE (expr) == INTEGER_CST
25405 || TREE_CODE (expr) == REAL_CST)
25406 return orig_expr;
25407 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25408 There is at least one place where we want to know that a
25409 particular expression is a throw-expression: when checking a ?:
25410 expression, there are special rules if the second or third
25411 argument is a throw-expression. */
25412 if (TREE_CODE (expr) == THROW_EXPR)
25413 return orig_expr;
25414
25415 /* Don't wrap an initializer list, we need to be able to look inside. */
25416 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25417 return orig_expr;
25418
25419 /* Don't wrap a dummy object, we need to be able to test for it. */
25420 if (is_dummy_object (expr))
25421 return orig_expr;
25422
25423 if (TREE_CODE (expr) == COND_EXPR)
25424 return build3 (COND_EXPR,
25425 TREE_TYPE (expr),
25426 TREE_OPERAND (expr, 0),
25427 (TREE_OPERAND (expr, 1)
25428 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25429 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25430 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25431 if (TREE_CODE (expr) == COMPOUND_EXPR
25432 && !COMPOUND_EXPR_OVERLOADED (expr))
25433 return build2 (COMPOUND_EXPR,
25434 TREE_TYPE (expr),
25435 TREE_OPERAND (expr, 0),
25436 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25437
25438 /* If the type is unknown, it can't really be non-dependent */
25439 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25440
25441 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25442 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25443 }
25444
25445 /* ARGS is a vector of expressions as arguments to a function call.
25446 Replace the arguments with equivalent non-dependent expressions.
25447 This modifies ARGS in place. */
25448
25449 void
25450 make_args_non_dependent (vec<tree, va_gc> *args)
25451 {
25452 unsigned int ix;
25453 tree arg;
25454
25455 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25456 {
25457 tree newarg = build_non_dependent_expr (arg);
25458 if (newarg != arg)
25459 (*args)[ix] = newarg;
25460 }
25461 }
25462
25463 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25464 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25465 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25466
25467 static tree
25468 make_auto_1 (tree name, bool set_canonical)
25469 {
25470 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25471 TYPE_NAME (au) = build_decl (input_location,
25472 TYPE_DECL, name, au);
25473 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25474 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25475 (0, processing_template_decl + 1, processing_template_decl + 1,
25476 TYPE_NAME (au), NULL_TREE);
25477 if (set_canonical)
25478 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25479 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25480 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25481
25482 return au;
25483 }
25484
25485 tree
25486 make_decltype_auto (void)
25487 {
25488 return make_auto_1 (decltype_auto_identifier, true);
25489 }
25490
25491 tree
25492 make_auto (void)
25493 {
25494 return make_auto_1 (auto_identifier, true);
25495 }
25496
25497 /* Return a C++17 deduction placeholder for class template TMPL. */
25498
25499 tree
25500 make_template_placeholder (tree tmpl)
25501 {
25502 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25503 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25504 return t;
25505 }
25506
25507 /* True iff T is a C++17 class template deduction placeholder. */
25508
25509 bool
25510 template_placeholder_p (tree t)
25511 {
25512 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25513 }
25514
25515 /* Make a "constrained auto" type-specifier. This is an
25516 auto type with constraints that must be associated after
25517 deduction. The constraint is formed from the given
25518 CONC and its optional sequence of arguments, which are
25519 non-null if written as partial-concept-id. */
25520
25521 tree
25522 make_constrained_auto (tree con, tree args)
25523 {
25524 tree type = make_auto_1 (auto_identifier, false);
25525
25526 /* Build the constraint. */
25527 tree tmpl = DECL_TI_TEMPLATE (con);
25528 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25529 expr = build_concept_check (expr, type, args);
25530
25531 tree constr = normalize_expression (expr);
25532 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25533
25534 /* Our canonical type depends on the constraint. */
25535 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25536
25537 /* Attach the constraint to the type declaration. */
25538 tree decl = TYPE_NAME (type);
25539 return decl;
25540 }
25541
25542 /* Given type ARG, return std::initializer_list<ARG>. */
25543
25544 static tree
25545 listify (tree arg)
25546 {
25547 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25548
25549 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25550 {
25551 gcc_rich_location richloc (input_location);
25552 maybe_add_include_fixit (&richloc, "<initializer_list>");
25553 error_at (&richloc,
25554 "deducing from brace-enclosed initializer list"
25555 " requires %<#include <initializer_list>%>");
25556
25557 return error_mark_node;
25558 }
25559 tree argvec = make_tree_vec (1);
25560 TREE_VEC_ELT (argvec, 0) = arg;
25561
25562 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25563 NULL_TREE, 0, tf_warning_or_error);
25564 }
25565
25566 /* Replace auto in TYPE with std::initializer_list<auto>. */
25567
25568 static tree
25569 listify_autos (tree type, tree auto_node)
25570 {
25571 tree init_auto = listify (auto_node);
25572 tree argvec = make_tree_vec (1);
25573 TREE_VEC_ELT (argvec, 0) = init_auto;
25574 if (processing_template_decl)
25575 argvec = add_to_template_args (current_template_args (), argvec);
25576 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25577 }
25578
25579 /* Hash traits for hashing possibly constrained 'auto'
25580 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25581
25582 struct auto_hash : default_hash_traits<tree>
25583 {
25584 static inline hashval_t hash (tree);
25585 static inline bool equal (tree, tree);
25586 };
25587
25588 /* Hash the 'auto' T. */
25589
25590 inline hashval_t
25591 auto_hash::hash (tree t)
25592 {
25593 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25594 /* Matching constrained-type-specifiers denote the same template
25595 parameter, so hash the constraint. */
25596 return hash_placeholder_constraint (c);
25597 else
25598 /* But unconstrained autos are all separate, so just hash the pointer. */
25599 return iterative_hash_object (t, 0);
25600 }
25601
25602 /* Compare two 'auto's. */
25603
25604 inline bool
25605 auto_hash::equal (tree t1, tree t2)
25606 {
25607 if (t1 == t2)
25608 return true;
25609
25610 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25611 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25612
25613 /* Two unconstrained autos are distinct. */
25614 if (!c1 || !c2)
25615 return false;
25616
25617 return equivalent_placeholder_constraints (c1, c2);
25618 }
25619
25620 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25621 constrained) auto, add it to the vector. */
25622
25623 static int
25624 extract_autos_r (tree t, void *data)
25625 {
25626 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25627 if (is_auto (t))
25628 {
25629 /* All the autos were built with index 0; fix that up now. */
25630 tree *p = hash.find_slot (t, INSERT);
25631 unsigned idx;
25632 if (*p)
25633 /* If this is a repeated constrained-type-specifier, use the index we
25634 chose before. */
25635 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25636 else
25637 {
25638 /* Otherwise this is new, so use the current count. */
25639 *p = t;
25640 idx = hash.elements () - 1;
25641 }
25642 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25643 }
25644
25645 /* Always keep walking. */
25646 return 0;
25647 }
25648
25649 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25650 says they can appear anywhere in the type. */
25651
25652 static tree
25653 extract_autos (tree type)
25654 {
25655 hash_set<tree> visited;
25656 hash_table<auto_hash> hash (2);
25657
25658 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25659
25660 tree tree_vec = make_tree_vec (hash.elements());
25661 for (hash_table<auto_hash>::iterator iter = hash.begin();
25662 iter != hash.end(); ++iter)
25663 {
25664 tree elt = *iter;
25665 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25666 TREE_VEC_ELT (tree_vec, i)
25667 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25668 }
25669
25670 return tree_vec;
25671 }
25672
25673 /* The stem for deduction guide names. */
25674 const char *const dguide_base = "__dguide_";
25675
25676 /* Return the name for a deduction guide for class template TMPL. */
25677
25678 tree
25679 dguide_name (tree tmpl)
25680 {
25681 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25682 tree tname = TYPE_IDENTIFIER (type);
25683 char *buf = (char *) alloca (1 + strlen (dguide_base)
25684 + IDENTIFIER_LENGTH (tname));
25685 memcpy (buf, dguide_base, strlen (dguide_base));
25686 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25687 IDENTIFIER_LENGTH (tname) + 1);
25688 tree dname = get_identifier (buf);
25689 TREE_TYPE (dname) = type;
25690 return dname;
25691 }
25692
25693 /* True if NAME is the name of a deduction guide. */
25694
25695 bool
25696 dguide_name_p (tree name)
25697 {
25698 return (TREE_CODE (name) == IDENTIFIER_NODE
25699 && TREE_TYPE (name)
25700 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25701 strlen (dguide_base)));
25702 }
25703
25704 /* True if FN is a deduction guide. */
25705
25706 bool
25707 deduction_guide_p (const_tree fn)
25708 {
25709 if (DECL_P (fn))
25710 if (tree name = DECL_NAME (fn))
25711 return dguide_name_p (name);
25712 return false;
25713 }
25714
25715 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25716
25717 bool
25718 copy_guide_p (const_tree fn)
25719 {
25720 gcc_assert (deduction_guide_p (fn));
25721 if (!DECL_ARTIFICIAL (fn))
25722 return false;
25723 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25724 return (TREE_CHAIN (parms) == void_list_node
25725 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25726 }
25727
25728 /* True if FN is a guide generated from a constructor template. */
25729
25730 bool
25731 template_guide_p (const_tree fn)
25732 {
25733 gcc_assert (deduction_guide_p (fn));
25734 if (!DECL_ARTIFICIAL (fn))
25735 return false;
25736 tree tmpl = DECL_TI_TEMPLATE (fn);
25737 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25738 return PRIMARY_TEMPLATE_P (org);
25739 return false;
25740 }
25741
25742 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25743 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25744 template parameter types. Note that the handling of template template
25745 parameters relies on current_template_parms being set appropriately for the
25746 new template. */
25747
25748 static tree
25749 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25750 tree tsubst_args, tsubst_flags_t complain)
25751 {
25752 tree oldidx = get_template_parm_index (olddecl);
25753
25754 tree newtype;
25755 if (TREE_CODE (olddecl) == TYPE_DECL
25756 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25757 {
25758 tree oldtype = TREE_TYPE (olddecl);
25759 newtype = cxx_make_type (TREE_CODE (oldtype));
25760 TYPE_MAIN_VARIANT (newtype) = newtype;
25761 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25762 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25763 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25764 }
25765 else
25766 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25767 complain, NULL_TREE);
25768
25769 tree newdecl
25770 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25771 DECL_NAME (olddecl), newtype);
25772 SET_DECL_TEMPLATE_PARM_P (newdecl);
25773
25774 tree newidx;
25775 if (TREE_CODE (olddecl) == TYPE_DECL
25776 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25777 {
25778 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25779 = build_template_parm_index (index, level, level,
25780 newdecl, newtype);
25781 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25782 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25783 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25784 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25785
25786 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25787 {
25788 DECL_TEMPLATE_RESULT (newdecl)
25789 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25790 DECL_NAME (olddecl), newtype);
25791 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25792 // First create a copy (ttargs) of tsubst_args with an
25793 // additional level for the template template parameter's own
25794 // template parameters (ttparms).
25795 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25796 (DECL_TEMPLATE_PARMS (olddecl)));
25797 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25798 tree ttargs = make_tree_vec (depth + 1);
25799 for (int i = 0; i < depth; ++i)
25800 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25801 TREE_VEC_ELT (ttargs, depth)
25802 = template_parms_level_to_args (ttparms);
25803 // Substitute ttargs into ttparms to fix references to
25804 // other template parameters.
25805 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25806 complain|tf_partial);
25807 // Now substitute again with args based on tparms, to reduce
25808 // the level of the ttparms.
25809 ttargs = current_template_args ();
25810 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25811 complain);
25812 // Finally, tack the adjusted parms onto tparms.
25813 ttparms = tree_cons (size_int (depth), ttparms,
25814 current_template_parms);
25815 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25816 }
25817 }
25818 else
25819 {
25820 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25821 tree newconst
25822 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25823 TREE_CODE (oldconst),
25824 DECL_NAME (oldconst), newtype);
25825 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25826 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25827 SET_DECL_TEMPLATE_PARM_P (newconst);
25828 newidx = build_template_parm_index (index, level, level,
25829 newconst, newtype);
25830 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25831 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25832 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25833 }
25834
25835 return newdecl;
25836 }
25837
25838 /* Returns a C++17 class deduction guide template based on the constructor
25839 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25840 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25841
25842 static tree
25843 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25844 {
25845 tree type, tparms, targs, fparms, fargs, ci;
25846 bool memtmpl = false;
25847 bool explicit_p;
25848 location_t loc;
25849 tree fn_tmpl = NULL_TREE;
25850
25851 if (TYPE_P (ctor))
25852 {
25853 type = ctor;
25854 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25855 if (copy_p)
25856 {
25857 type = TREE_TYPE (type);
25858 fparms = tree_cons (NULL_TREE, type, void_list_node);
25859 }
25860 else
25861 fparms = void_list_node;
25862
25863 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25864 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25865 targs = CLASSTYPE_TI_ARGS (type);
25866 ci = NULL_TREE;
25867 fargs = NULL_TREE;
25868 loc = DECL_SOURCE_LOCATION (ctmpl);
25869 explicit_p = false;
25870 }
25871 else
25872 {
25873 ++processing_template_decl;
25874
25875 fn_tmpl
25876 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25877 : DECL_TI_TEMPLATE (ctor));
25878 if (outer_args)
25879 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25880 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25881
25882 type = DECL_CONTEXT (ctor);
25883
25884 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25885 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25886 fully specialized args for the enclosing class. Strip those off, as
25887 the deduction guide won't have those template parameters. */
25888 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25889 TMPL_PARMS_DEPTH (tparms));
25890 /* Discard the 'this' parameter. */
25891 fparms = FUNCTION_ARG_CHAIN (ctor);
25892 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25893 ci = get_constraints (ctor);
25894 loc = DECL_SOURCE_LOCATION (ctor);
25895 explicit_p = DECL_NONCONVERTING_P (ctor);
25896
25897 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25898 {
25899 memtmpl = true;
25900
25901 /* For a member template constructor, we need to flatten the two
25902 template parameter lists into one, and then adjust the function
25903 signature accordingly. This gets...complicated. */
25904 tree save_parms = current_template_parms;
25905
25906 /* For a member template we should have two levels of parms/args, one
25907 for the class and one for the constructor. We stripped
25908 specialized args for further enclosing classes above. */
25909 const int depth = 2;
25910 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25911
25912 /* Template args for translating references to the two-level template
25913 parameters into references to the one-level template parameters we
25914 are creating. */
25915 tree tsubst_args = copy_node (targs);
25916 TMPL_ARGS_LEVEL (tsubst_args, depth)
25917 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25918
25919 /* Template parms for the constructor template. */
25920 tree ftparms = TREE_VALUE (tparms);
25921 unsigned flen = TREE_VEC_LENGTH (ftparms);
25922 /* Template parms for the class template. */
25923 tparms = TREE_CHAIN (tparms);
25924 tree ctparms = TREE_VALUE (tparms);
25925 unsigned clen = TREE_VEC_LENGTH (ctparms);
25926 /* Template parms for the deduction guide start as a copy of the
25927 template parms for the class. We set current_template_parms for
25928 lookup_template_class_1. */
25929 current_template_parms = tparms = copy_node (tparms);
25930 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25931 for (unsigned i = 0; i < clen; ++i)
25932 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25933
25934 /* Now we need to rewrite the constructor parms to append them to the
25935 class parms. */
25936 for (unsigned i = 0; i < flen; ++i)
25937 {
25938 unsigned index = i + clen;
25939 unsigned level = 1;
25940 tree oldelt = TREE_VEC_ELT (ftparms, i);
25941 tree olddecl = TREE_VALUE (oldelt);
25942 tree newdecl = rewrite_template_parm (olddecl, index, level,
25943 tsubst_args, complain);
25944 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25945 tsubst_args, complain, ctor);
25946 tree list = build_tree_list (newdef, newdecl);
25947 TEMPLATE_PARM_CONSTRAINTS (list)
25948 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25949 tsubst_args, complain, ctor);
25950 TREE_VEC_ELT (new_vec, index) = list;
25951 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25952 }
25953
25954 /* Now we have a final set of template parms to substitute into the
25955 function signature. */
25956 targs = template_parms_to_args (tparms);
25957 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25958 complain, ctor);
25959 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25960 if (ci)
25961 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25962
25963 current_template_parms = save_parms;
25964 }
25965 --processing_template_decl;
25966 }
25967
25968 if (!memtmpl)
25969 {
25970 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25971 tparms = copy_node (tparms);
25972 INNERMOST_TEMPLATE_PARMS (tparms)
25973 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25974 }
25975
25976 tree fntype = build_function_type (type, fparms);
25977 tree ded_fn = build_lang_decl_loc (loc,
25978 FUNCTION_DECL,
25979 dguide_name (type), fntype);
25980 DECL_ARGUMENTS (ded_fn) = fargs;
25981 DECL_ARTIFICIAL (ded_fn) = true;
25982 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25983 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25984 DECL_ARTIFICIAL (ded_tmpl) = true;
25985 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25986 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25987 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25988 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25989 if (DECL_P (ctor))
25990 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25991 if (ci)
25992 set_constraints (ded_tmpl, ci);
25993
25994 return ded_tmpl;
25995 }
25996
25997 /* Deduce template arguments for the class template placeholder PTYPE for
25998 template TMPL based on the initializer INIT, and return the resulting
25999 type. */
26000
26001 static tree
26002 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
26003 tsubst_flags_t complain)
26004 {
26005 if (!DECL_CLASS_TEMPLATE_P (tmpl))
26006 {
26007 /* We should have handled this in the caller. */
26008 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26009 return ptype;
26010 if (complain & tf_error)
26011 error ("non-class template %qT used without template arguments", tmpl);
26012 return error_mark_node;
26013 }
26014
26015 tree type = TREE_TYPE (tmpl);
26016
26017 bool try_list_ctor = false;
26018
26019 vec<tree,va_gc> *args;
26020 if (init == NULL_TREE
26021 || TREE_CODE (init) == TREE_LIST)
26022 args = make_tree_vector_from_list (init);
26023 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
26024 {
26025 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
26026 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
26027 {
26028 /* As an exception, the first phase in 16.3.1.7 (considering the
26029 initializer list as a single argument) is omitted if the
26030 initializer list consists of a single expression of type cv U,
26031 where U is a specialization of C or a class derived from a
26032 specialization of C. */
26033 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
26034 tree etype = TREE_TYPE (elt);
26035
26036 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
26037 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26038 int err = unify (tparms, targs, type, etype,
26039 UNIFY_ALLOW_DERIVED, /*explain*/false);
26040 if (err == 0)
26041 try_list_ctor = false;
26042 ggc_free (targs);
26043 }
26044 if (try_list_ctor || is_std_init_list (type))
26045 args = make_tree_vector_single (init);
26046 else
26047 args = make_tree_vector_from_ctor (init);
26048 }
26049 else
26050 args = make_tree_vector_single (init);
26051
26052 tree dname = dguide_name (tmpl);
26053 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
26054 /*type*/false, /*complain*/false,
26055 /*hidden*/false);
26056 bool elided = false;
26057 if (cands == error_mark_node)
26058 cands = NULL_TREE;
26059
26060 /* Prune explicit deduction guides in copy-initialization context. */
26061 if (flags & LOOKUP_ONLYCONVERTING)
26062 {
26063 for (lkp_iterator iter (cands); !elided && iter; ++iter)
26064 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26065 elided = true;
26066
26067 if (elided)
26068 {
26069 /* Found a nonconverting guide, prune the candidates. */
26070 tree pruned = NULL_TREE;
26071 for (lkp_iterator iter (cands); iter; ++iter)
26072 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26073 pruned = lookup_add (*iter, pruned);
26074
26075 cands = pruned;
26076 }
26077 }
26078
26079 tree outer_args = NULL_TREE;
26080 if (DECL_CLASS_SCOPE_P (tmpl)
26081 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
26082 {
26083 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
26084 type = TREE_TYPE (most_general_template (tmpl));
26085 }
26086
26087 bool saw_ctor = false;
26088 // FIXME cache artificial deduction guides
26089 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
26090 {
26091 tree guide = build_deduction_guide (*iter, outer_args, complain);
26092 if ((flags & LOOKUP_ONLYCONVERTING)
26093 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
26094 elided = true;
26095 else
26096 cands = lookup_add (guide, cands);
26097
26098 saw_ctor = true;
26099 }
26100
26101 tree call = error_mark_node;
26102
26103 /* If this is list-initialization and the class has a list constructor, first
26104 try deducing from the list as a single argument, as [over.match.list]. */
26105 tree list_cands = NULL_TREE;
26106 if (try_list_ctor && cands)
26107 for (lkp_iterator iter (cands); iter; ++iter)
26108 {
26109 tree dg = *iter;
26110 if (is_list_ctor (dg))
26111 list_cands = lookup_add (dg, list_cands);
26112 }
26113 if (list_cands)
26114 {
26115 ++cp_unevaluated_operand;
26116 call = build_new_function_call (list_cands, &args, tf_decltype);
26117 --cp_unevaluated_operand;
26118
26119 if (call == error_mark_node)
26120 {
26121 /* That didn't work, now try treating the list as a sequence of
26122 arguments. */
26123 release_tree_vector (args);
26124 args = make_tree_vector_from_ctor (init);
26125 }
26126 }
26127
26128 /* Maybe generate an implicit deduction guide. */
26129 if (call == error_mark_node && args->length () < 2)
26130 {
26131 tree gtype = NULL_TREE;
26132
26133 if (args->length () == 1)
26134 /* Generate a copy guide. */
26135 gtype = build_reference_type (type);
26136 else if (!saw_ctor)
26137 /* Generate a default guide. */
26138 gtype = type;
26139
26140 if (gtype)
26141 {
26142 tree guide = build_deduction_guide (gtype, outer_args, complain);
26143 cands = lookup_add (guide, cands);
26144 }
26145 }
26146
26147 if (elided && !cands)
26148 {
26149 error ("cannot deduce template arguments for copy-initialization"
26150 " of %qT, as it has no non-explicit deduction guides or "
26151 "user-declared constructors", type);
26152 return error_mark_node;
26153 }
26154 else if (!cands && call == error_mark_node)
26155 {
26156 error ("cannot deduce template arguments of %qT, as it has no viable "
26157 "deduction guides", type);
26158 return error_mark_node;
26159 }
26160
26161 if (call == error_mark_node)
26162 {
26163 ++cp_unevaluated_operand;
26164 call = build_new_function_call (cands, &args, tf_decltype);
26165 --cp_unevaluated_operand;
26166 }
26167
26168 if (call == error_mark_node && (complain & tf_warning_or_error))
26169 {
26170 error ("class template argument deduction failed:");
26171
26172 ++cp_unevaluated_operand;
26173 call = build_new_function_call (cands, &args, complain | tf_decltype);
26174 --cp_unevaluated_operand;
26175
26176 if (elided)
26177 inform (input_location, "explicit deduction guides not considered "
26178 "for copy-initialization");
26179 }
26180
26181 release_tree_vector (args);
26182
26183 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
26184 }
26185
26186 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26187 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26188 The CONTEXT determines the context in which auto deduction is performed
26189 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26190 OUTER_TARGS are used during template argument deduction
26191 (context == adc_unify) to properly substitute the result, and is ignored
26192 in other contexts.
26193
26194 For partial-concept-ids, extra args may be appended to the list of deduced
26195 template arguments prior to determining constraint satisfaction. */
26196
26197 tree
26198 do_auto_deduction (tree type, tree init, tree auto_node,
26199 tsubst_flags_t complain, auto_deduction_context context,
26200 tree outer_targs, int flags)
26201 {
26202 tree targs;
26203
26204 if (init == error_mark_node)
26205 return error_mark_node;
26206
26207 if (init && type_dependent_expression_p (init)
26208 && context != adc_unify)
26209 /* Defining a subset of type-dependent expressions that we can deduce
26210 from ahead of time isn't worth the trouble. */
26211 return type;
26212
26213 /* Similarly, we can't deduce from another undeduced decl. */
26214 if (init && undeduced_auto_decl (init))
26215 return type;
26216
26217 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
26218 /* C++17 class template argument deduction. */
26219 return do_class_deduction (type, tmpl, init, flags, complain);
26220
26221 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
26222 /* Nothing we can do with this, even in deduction context. */
26223 return type;
26224
26225 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26226 with either a new invented type template parameter U or, if the
26227 initializer is a braced-init-list (8.5.4), with
26228 std::initializer_list<U>. */
26229 if (BRACE_ENCLOSED_INITIALIZER_P (init))
26230 {
26231 if (!DIRECT_LIST_INIT_P (init))
26232 type = listify_autos (type, auto_node);
26233 else if (CONSTRUCTOR_NELTS (init) == 1)
26234 init = CONSTRUCTOR_ELT (init, 0)->value;
26235 else
26236 {
26237 if (complain & tf_warning_or_error)
26238 {
26239 if (permerror (input_location, "direct-list-initialization of "
26240 "%<auto%> requires exactly one element"))
26241 inform (input_location,
26242 "for deduction to %<std::initializer_list%>, use copy-"
26243 "list-initialization (i.e. add %<=%> before the %<{%>)");
26244 }
26245 type = listify_autos (type, auto_node);
26246 }
26247 }
26248
26249 if (type == error_mark_node)
26250 return error_mark_node;
26251
26252 init = resolve_nondeduced_context (init, complain);
26253
26254 if (context == adc_decomp_type
26255 && auto_node == type
26256 && init != error_mark_node
26257 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
26258 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26259 and initializer has array type, deduce cv-qualified array type. */
26260 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
26261 complain);
26262 else if (AUTO_IS_DECLTYPE (auto_node))
26263 {
26264 bool id = (DECL_P (init)
26265 || ((TREE_CODE (init) == COMPONENT_REF
26266 || TREE_CODE (init) == SCOPE_REF)
26267 && !REF_PARENTHESIZED_P (init)));
26268 targs = make_tree_vec (1);
26269 TREE_VEC_ELT (targs, 0)
26270 = finish_decltype_type (init, id, tf_warning_or_error);
26271 if (type != auto_node)
26272 {
26273 if (complain & tf_error)
26274 error ("%qT as type rather than plain %<decltype(auto)%>", type);
26275 return error_mark_node;
26276 }
26277 }
26278 else
26279 {
26280 tree parms = build_tree_list (NULL_TREE, type);
26281 tree tparms;
26282
26283 if (flag_concepts)
26284 tparms = extract_autos (type);
26285 else
26286 {
26287 tparms = make_tree_vec (1);
26288 TREE_VEC_ELT (tparms, 0)
26289 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
26290 }
26291
26292 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26293 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
26294 DEDUCE_CALL, LOOKUP_NORMAL,
26295 NULL, /*explain_p=*/false);
26296 if (val > 0)
26297 {
26298 if (processing_template_decl)
26299 /* Try again at instantiation time. */
26300 return type;
26301 if (type && type != error_mark_node
26302 && (complain & tf_error))
26303 /* If type is error_mark_node a diagnostic must have been
26304 emitted by now. Also, having a mention to '<type error>'
26305 in the diagnostic is not really useful to the user. */
26306 {
26307 if (cfun && auto_node == current_function_auto_return_pattern
26308 && LAMBDA_FUNCTION_P (current_function_decl))
26309 error ("unable to deduce lambda return type from %qE", init);
26310 else
26311 error ("unable to deduce %qT from %qE", type, init);
26312 type_unification_real (tparms, targs, parms, &init, 1, 0,
26313 DEDUCE_CALL, LOOKUP_NORMAL,
26314 NULL, /*explain_p=*/true);
26315 }
26316 return error_mark_node;
26317 }
26318 }
26319
26320 /* Check any placeholder constraints against the deduced type. */
26321 if (flag_concepts && !processing_template_decl)
26322 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26323 {
26324 /* Use the deduced type to check the associated constraints. If we
26325 have a partial-concept-id, rebuild the argument list so that
26326 we check using the extra arguments. */
26327 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26328 tree cargs = CHECK_CONSTR_ARGS (constr);
26329 if (TREE_VEC_LENGTH (cargs) > 1)
26330 {
26331 cargs = copy_node (cargs);
26332 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26333 }
26334 else
26335 cargs = targs;
26336 if (!constraints_satisfied_p (constr, cargs))
26337 {
26338 if (complain & tf_warning_or_error)
26339 {
26340 switch (context)
26341 {
26342 case adc_unspecified:
26343 case adc_unify:
26344 error("placeholder constraints not satisfied");
26345 break;
26346 case adc_variable_type:
26347 case adc_decomp_type:
26348 error ("deduced initializer does not satisfy "
26349 "placeholder constraints");
26350 break;
26351 case adc_return_type:
26352 error ("deduced return type does not satisfy "
26353 "placeholder constraints");
26354 break;
26355 case adc_requirement:
26356 error ("deduced expression type does not satisfy "
26357 "placeholder constraints");
26358 break;
26359 }
26360 diagnose_constraints (input_location, constr, targs);
26361 }
26362 return error_mark_node;
26363 }
26364 }
26365
26366 if (processing_template_decl && context != adc_unify)
26367 outer_targs = current_template_args ();
26368 targs = add_to_template_args (outer_targs, targs);
26369 return tsubst (type, targs, complain, NULL_TREE);
26370 }
26371
26372 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26373 result. */
26374
26375 tree
26376 splice_late_return_type (tree type, tree late_return_type)
26377 {
26378 if (is_auto (type))
26379 {
26380 if (late_return_type)
26381 return late_return_type;
26382
26383 tree idx = get_template_parm_index (type);
26384 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26385 /* In an abbreviated function template we didn't know we were dealing
26386 with a function template when we saw the auto return type, so update
26387 it to have the correct level. */
26388 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26389 }
26390 return type;
26391 }
26392
26393 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26394 'decltype(auto)' or a deduced class template. */
26395
26396 bool
26397 is_auto (const_tree type)
26398 {
26399 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26400 && (TYPE_IDENTIFIER (type) == auto_identifier
26401 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26402 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26403 return true;
26404 else
26405 return false;
26406 }
26407
26408 /* for_each_template_parm callback for type_uses_auto. */
26409
26410 int
26411 is_auto_r (tree tp, void */*data*/)
26412 {
26413 return is_auto (tp);
26414 }
26415
26416 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26417 a use of `auto'. Returns NULL_TREE otherwise. */
26418
26419 tree
26420 type_uses_auto (tree type)
26421 {
26422 if (type == NULL_TREE)
26423 return NULL_TREE;
26424 else if (flag_concepts)
26425 {
26426 /* The Concepts TS allows multiple autos in one type-specifier; just
26427 return the first one we find, do_auto_deduction will collect all of
26428 them. */
26429 if (uses_template_parms (type))
26430 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26431 /*visited*/NULL, /*nondeduced*/true);
26432 else
26433 return NULL_TREE;
26434 }
26435 else
26436 return find_type_usage (type, is_auto);
26437 }
26438
26439 /* For a given template T, return the vector of typedefs referenced
26440 in T for which access check is needed at T instantiation time.
26441 T is either a FUNCTION_DECL or a RECORD_TYPE.
26442 Those typedefs were added to T by the function
26443 append_type_to_template_for_access_check. */
26444
26445 vec<qualified_typedef_usage_t, va_gc> *
26446 get_types_needing_access_check (tree t)
26447 {
26448 tree ti;
26449 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26450
26451 if (!t || t == error_mark_node)
26452 return NULL;
26453
26454 if (!(ti = get_template_info (t)))
26455 return NULL;
26456
26457 if (CLASS_TYPE_P (t)
26458 || TREE_CODE (t) == FUNCTION_DECL)
26459 {
26460 if (!TI_TEMPLATE (ti))
26461 return NULL;
26462
26463 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26464 }
26465
26466 return result;
26467 }
26468
26469 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26470 tied to T. That list of typedefs will be access checked at
26471 T instantiation time.
26472 T is either a FUNCTION_DECL or a RECORD_TYPE.
26473 TYPE_DECL is a TYPE_DECL node representing a typedef.
26474 SCOPE is the scope through which TYPE_DECL is accessed.
26475 LOCATION is the location of the usage point of TYPE_DECL.
26476
26477 This function is a subroutine of
26478 append_type_to_template_for_access_check. */
26479
26480 static void
26481 append_type_to_template_for_access_check_1 (tree t,
26482 tree type_decl,
26483 tree scope,
26484 location_t location)
26485 {
26486 qualified_typedef_usage_t typedef_usage;
26487 tree ti;
26488
26489 if (!t || t == error_mark_node)
26490 return;
26491
26492 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26493 || CLASS_TYPE_P (t))
26494 && type_decl
26495 && TREE_CODE (type_decl) == TYPE_DECL
26496 && scope);
26497
26498 if (!(ti = get_template_info (t)))
26499 return;
26500
26501 gcc_assert (TI_TEMPLATE (ti));
26502
26503 typedef_usage.typedef_decl = type_decl;
26504 typedef_usage.context = scope;
26505 typedef_usage.locus = location;
26506
26507 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26508 }
26509
26510 /* Append TYPE_DECL to the template TEMPL.
26511 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26512 At TEMPL instanciation time, TYPE_DECL will be checked to see
26513 if it can be accessed through SCOPE.
26514 LOCATION is the location of the usage point of TYPE_DECL.
26515
26516 e.g. consider the following code snippet:
26517
26518 class C
26519 {
26520 typedef int myint;
26521 };
26522
26523 template<class U> struct S
26524 {
26525 C::myint mi; // <-- usage point of the typedef C::myint
26526 };
26527
26528 S<char> s;
26529
26530 At S<char> instantiation time, we need to check the access of C::myint
26531 In other words, we need to check the access of the myint typedef through
26532 the C scope. For that purpose, this function will add the myint typedef
26533 and the scope C through which its being accessed to a list of typedefs
26534 tied to the template S. That list will be walked at template instantiation
26535 time and access check performed on each typedefs it contains.
26536 Note that this particular code snippet should yield an error because
26537 myint is private to C. */
26538
26539 void
26540 append_type_to_template_for_access_check (tree templ,
26541 tree type_decl,
26542 tree scope,
26543 location_t location)
26544 {
26545 qualified_typedef_usage_t *iter;
26546 unsigned i;
26547
26548 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26549
26550 /* Make sure we don't append the type to the template twice. */
26551 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26552 if (iter->typedef_decl == type_decl && scope == iter->context)
26553 return;
26554
26555 append_type_to_template_for_access_check_1 (templ, type_decl,
26556 scope, location);
26557 }
26558
26559 /* Convert the generic type parameters in PARM that match the types given in the
26560 range [START_IDX, END_IDX) from the current_template_parms into generic type
26561 packs. */
26562
26563 tree
26564 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26565 {
26566 tree current = current_template_parms;
26567 int depth = TMPL_PARMS_DEPTH (current);
26568 current = INNERMOST_TEMPLATE_PARMS (current);
26569 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26570
26571 for (int i = 0; i < start_idx; ++i)
26572 TREE_VEC_ELT (replacement, i)
26573 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26574
26575 for (int i = start_idx; i < end_idx; ++i)
26576 {
26577 /* Create a distinct parameter pack type from the current parm and add it
26578 to the replacement args to tsubst below into the generic function
26579 parameter. */
26580
26581 tree o = TREE_TYPE (TREE_VALUE
26582 (TREE_VEC_ELT (current, i)));
26583 tree t = copy_type (o);
26584 TEMPLATE_TYPE_PARM_INDEX (t)
26585 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26586 o, 0, 0, tf_none);
26587 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26588 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26589 TYPE_MAIN_VARIANT (t) = t;
26590 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26591 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26592 TREE_VEC_ELT (replacement, i) = t;
26593 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26594 }
26595
26596 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26597 TREE_VEC_ELT (replacement, i)
26598 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26599
26600 /* If there are more levels then build up the replacement with the outer
26601 template parms. */
26602 if (depth > 1)
26603 replacement = add_to_template_args (template_parms_to_args
26604 (TREE_CHAIN (current_template_parms)),
26605 replacement);
26606
26607 return tsubst (parm, replacement, tf_none, NULL_TREE);
26608 }
26609
26610 /* Entries in the decl_constraint hash table. */
26611 struct GTY((for_user)) constr_entry
26612 {
26613 tree decl;
26614 tree ci;
26615 };
26616
26617 /* Hashing function and equality for constraint entries. */
26618 struct constr_hasher : ggc_ptr_hash<constr_entry>
26619 {
26620 static hashval_t hash (constr_entry *e)
26621 {
26622 return (hashval_t)DECL_UID (e->decl);
26623 }
26624
26625 static bool equal (constr_entry *e1, constr_entry *e2)
26626 {
26627 return e1->decl == e2->decl;
26628 }
26629 };
26630
26631 /* A mapping from declarations to constraint information. Note that
26632 both templates and their underlying declarations are mapped to the
26633 same constraint information.
26634
26635 FIXME: This is defined in pt.c because garbage collection
26636 code is not being generated for constraint.cc. */
26637
26638 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26639
26640 /* Returns the template constraints of declaration T. If T is not
26641 constrained, return NULL_TREE. Note that T must be non-null. */
26642
26643 tree
26644 get_constraints (tree t)
26645 {
26646 if (!flag_concepts)
26647 return NULL_TREE;
26648
26649 gcc_assert (DECL_P (t));
26650 if (TREE_CODE (t) == TEMPLATE_DECL)
26651 t = DECL_TEMPLATE_RESULT (t);
26652 constr_entry elt = { t, NULL_TREE };
26653 constr_entry* found = decl_constraints->find (&elt);
26654 if (found)
26655 return found->ci;
26656 else
26657 return NULL_TREE;
26658 }
26659
26660 /* Associate the given constraint information CI with the declaration
26661 T. If T is a template, then the constraints are associated with
26662 its underlying declaration. Don't build associations if CI is
26663 NULL_TREE. */
26664
26665 void
26666 set_constraints (tree t, tree ci)
26667 {
26668 if (!ci)
26669 return;
26670 gcc_assert (t && flag_concepts);
26671 if (TREE_CODE (t) == TEMPLATE_DECL)
26672 t = DECL_TEMPLATE_RESULT (t);
26673 gcc_assert (!get_constraints (t));
26674 constr_entry elt = {t, ci};
26675 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26676 constr_entry* entry = ggc_alloc<constr_entry> ();
26677 *entry = elt;
26678 *slot = entry;
26679 }
26680
26681 /* Remove the associated constraints of the declaration T. */
26682
26683 void
26684 remove_constraints (tree t)
26685 {
26686 gcc_assert (DECL_P (t));
26687 if (TREE_CODE (t) == TEMPLATE_DECL)
26688 t = DECL_TEMPLATE_RESULT (t);
26689
26690 constr_entry elt = {t, NULL_TREE};
26691 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26692 if (slot)
26693 decl_constraints->clear_slot (slot);
26694 }
26695
26696 /* Memoized satisfaction results for declarations. This
26697 maps the pair (constraint_info, arguments) to the result computed
26698 by constraints_satisfied_p. */
26699
26700 struct GTY((for_user)) constraint_sat_entry
26701 {
26702 tree ci;
26703 tree args;
26704 tree result;
26705 };
26706
26707 /* Hashing function and equality for constraint entries. */
26708
26709 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26710 {
26711 static hashval_t hash (constraint_sat_entry *e)
26712 {
26713 hashval_t val = iterative_hash_object(e->ci, 0);
26714 return iterative_hash_template_arg (e->args, val);
26715 }
26716
26717 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26718 {
26719 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26720 }
26721 };
26722
26723 /* Memoized satisfaction results for concept checks. */
26724
26725 struct GTY((for_user)) concept_spec_entry
26726 {
26727 tree tmpl;
26728 tree args;
26729 tree result;
26730 };
26731
26732 /* Hashing function and equality for constraint entries. */
26733
26734 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26735 {
26736 static hashval_t hash (concept_spec_entry *e)
26737 {
26738 return hash_tmpl_and_args (e->tmpl, e->args);
26739 }
26740
26741 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26742 {
26743 ++comparing_specializations;
26744 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26745 --comparing_specializations;
26746 return eq;
26747 }
26748 };
26749
26750 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26751 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26752
26753 /* Search for a memoized satisfaction result. Returns one of the
26754 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26755
26756 tree
26757 lookup_constraint_satisfaction (tree ci, tree args)
26758 {
26759 constraint_sat_entry elt = { ci, args, NULL_TREE };
26760 constraint_sat_entry* found = constraint_memos->find (&elt);
26761 if (found)
26762 return found->result;
26763 else
26764 return NULL_TREE;
26765 }
26766
26767 /* Memoize the result of a satisfication test. Returns the saved result. */
26768
26769 tree
26770 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26771 {
26772 constraint_sat_entry elt = {ci, args, result};
26773 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26774 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26775 *entry = elt;
26776 *slot = entry;
26777 return result;
26778 }
26779
26780 /* Search for a memoized satisfaction result for a concept. */
26781
26782 tree
26783 lookup_concept_satisfaction (tree tmpl, tree args)
26784 {
26785 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26786 concept_spec_entry* found = concept_memos->find (&elt);
26787 if (found)
26788 return found->result;
26789 else
26790 return NULL_TREE;
26791 }
26792
26793 /* Memoize the result of a concept check. Returns the saved result. */
26794
26795 tree
26796 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26797 {
26798 concept_spec_entry elt = {tmpl, args, result};
26799 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26800 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26801 *entry = elt;
26802 *slot = entry;
26803 return result;
26804 }
26805
26806 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26807
26808 /* Returns a prior concept specialization. This returns the substituted
26809 and normalized constraints defined by the concept. */
26810
26811 tree
26812 get_concept_expansion (tree tmpl, tree args)
26813 {
26814 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26815 concept_spec_entry* found = concept_expansions->find (&elt);
26816 if (found)
26817 return found->result;
26818 else
26819 return NULL_TREE;
26820 }
26821
26822 /* Save a concept expansion for later. */
26823
26824 tree
26825 save_concept_expansion (tree tmpl, tree args, tree def)
26826 {
26827 concept_spec_entry elt = {tmpl, args, def};
26828 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26829 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26830 *entry = elt;
26831 *slot = entry;
26832 return def;
26833 }
26834
26835 static hashval_t
26836 hash_subsumption_args (tree t1, tree t2)
26837 {
26838 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26839 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26840 int val = 0;
26841 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26842 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26843 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26844 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26845 return val;
26846 }
26847
26848 /* Compare the constraints of two subsumption entries. The LEFT1 and
26849 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26850 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26851
26852 static bool
26853 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26854 {
26855 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26856 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26857 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26858 CHECK_CONSTR_ARGS (right1)))
26859 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26860 CHECK_CONSTR_ARGS (right2));
26861 return false;
26862 }
26863
26864 /* Key/value pair for learning and memoizing subsumption results. This
26865 associates a pair of check constraints (including arguments) with
26866 a boolean value indicating the result. */
26867
26868 struct GTY((for_user)) subsumption_entry
26869 {
26870 tree t1;
26871 tree t2;
26872 bool result;
26873 };
26874
26875 /* Hashing function and equality for constraint entries. */
26876
26877 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26878 {
26879 static hashval_t hash (subsumption_entry *e)
26880 {
26881 return hash_subsumption_args (e->t1, e->t2);
26882 }
26883
26884 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26885 {
26886 ++comparing_specializations;
26887 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26888 --comparing_specializations;
26889 return eq;
26890 }
26891 };
26892
26893 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26894
26895 /* Search for a previously cached subsumption result. */
26896
26897 bool*
26898 lookup_subsumption_result (tree t1, tree t2)
26899 {
26900 subsumption_entry elt = { t1, t2, false };
26901 subsumption_entry* found = subsumption_table->find (&elt);
26902 if (found)
26903 return &found->result;
26904 else
26905 return 0;
26906 }
26907
26908 /* Save a subsumption result. */
26909
26910 bool
26911 save_subsumption_result (tree t1, tree t2, bool result)
26912 {
26913 subsumption_entry elt = {t1, t2, result};
26914 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26915 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26916 *entry = elt;
26917 *slot = entry;
26918 return result;
26919 }
26920
26921 /* Set up the hash table for constraint association. */
26922
26923 void
26924 init_constraint_processing (void)
26925 {
26926 if (!flag_concepts)
26927 return;
26928
26929 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26930 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26931 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26932 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26933 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26934 }
26935
26936 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26937 0..N-1. */
26938
26939 void
26940 declare_integer_pack (void)
26941 {
26942 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26943 build_function_type_list (integer_type_node,
26944 integer_type_node,
26945 NULL_TREE),
26946 NULL_TREE, ECF_CONST);
26947 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26948 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26949 }
26950
26951 /* Set up the hash tables for template instantiations. */
26952
26953 void
26954 init_template_processing (void)
26955 {
26956 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26957 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26958
26959 if (cxx_dialect >= cxx11)
26960 declare_integer_pack ();
26961 }
26962
26963 /* Print stats about the template hash tables for -fstats. */
26964
26965 void
26966 print_template_statistics (void)
26967 {
26968 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26969 "%f collisions\n", (long) decl_specializations->size (),
26970 (long) decl_specializations->elements (),
26971 decl_specializations->collisions ());
26972 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26973 "%f collisions\n", (long) type_specializations->size (),
26974 (long) type_specializations->elements (),
26975 type_specializations->collisions ());
26976 }
26977
26978 #if CHECKING_P
26979
26980 namespace selftest {
26981
26982 /* Verify that build_non_dependent_expr () works, for various expressions,
26983 and that location wrappers don't affect the results. */
26984
26985 static void
26986 test_build_non_dependent_expr ()
26987 {
26988 location_t loc = BUILTINS_LOCATION;
26989
26990 /* Verify constants, without and with location wrappers. */
26991 tree int_cst = build_int_cst (integer_type_node, 42);
26992 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
26993
26994 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
26995 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
26996 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
26997
26998 tree string_lit = build_string (4, "foo");
26999 TREE_TYPE (string_lit) = char_array_type_node;
27000 string_lit = fix_string_type (string_lit);
27001 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
27002
27003 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
27004 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
27005 ASSERT_EQ (wrapped_string_lit,
27006 build_non_dependent_expr (wrapped_string_lit));
27007 }
27008
27009 /* Verify that type_dependent_expression_p () works correctly, even
27010 in the presence of location wrapper nodes. */
27011
27012 static void
27013 test_type_dependent_expression_p ()
27014 {
27015 location_t loc = BUILTINS_LOCATION;
27016
27017 tree name = get_identifier ("foo");
27018
27019 /* If no templates are involved, nothing is type-dependent. */
27020 gcc_assert (!processing_template_decl);
27021 ASSERT_FALSE (type_dependent_expression_p (name));
27022
27023 ++processing_template_decl;
27024
27025 /* Within a template, an unresolved name is always type-dependent. */
27026 ASSERT_TRUE (type_dependent_expression_p (name));
27027
27028 /* Ensure it copes with NULL_TREE and errors. */
27029 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
27030 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
27031
27032 /* A USING_DECL in a template should be type-dependent, even if wrapped
27033 with a location wrapper (PR c++/83799). */
27034 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
27035 TREE_TYPE (using_decl) = integer_type_node;
27036 ASSERT_TRUE (type_dependent_expression_p (using_decl));
27037 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
27038 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
27039 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
27040
27041 --processing_template_decl;
27042 }
27043
27044 /* Run all of the selftests within this file. */
27045
27046 void
27047 cp_pt_c_tests ()
27048 {
27049 test_build_non_dependent_expr ();
27050 test_type_dependent_expression_p ();
27051 }
27052
27053 } // namespace selftest
27054
27055 #endif /* #if CHECKING_P */
27056
27057 #include "gt-cp-pt.h"